Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/cli/src/acp/acpSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,41 @@ describe('Session', () => {
expect(result).toMatchObject({ stopReason: 'end_turn' });
});

it.each([
{ type: 'MAX_TOKENS_EXCEEDED', reason: 'MAX_TOKENS' },
{ type: 'SAFETY_BLOCKED', reason: 'SAFETY' },
{ type: 'RECITATION_BLOCKED', reason: 'RECITATION' },
{ type: 'OTHER_BLOCKED', reason: 'OTHER' },
{ type: 'THINKING_ONLY_RESPONSE', reason: 'STOP' },
])(
'should gracefully handle InvalidStreamError with type $type in ACP session',
async ({ type, reason }) => {
const error = new InvalidStreamError(
`Stream failed with ${reason}`,
type as InvalidStreamError['type'],
);
mockSendMessageStream.mockImplementation(() => {
async function* errorGen(): AsyncGenerator<
ServerGeminiStreamEvent,
void,
unknown
> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
yield* [] as any;
throw error;
}
return errorGen();
});

const result = await session.prompt({
sessionId: 'session-1',
prompt: [{ type: 'text', text: 'Hi' }],
});

expect(result).toMatchObject({ stopReason: 'end_turn' });
},
);

it('should handle /memory command', async () => {
const handleCommandSpy = vi
.spyOn(
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/acp/acpSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,12 @@ export class Session {
(error.type === 'NO_RESPONSE_TEXT' ||
error.type === 'NO_FINISH_REASON' ||
error.type === 'MALFORMED_FUNCTION_CALL' ||
error.type === 'UNEXPECTED_TOOL_CALL'))
error.type === 'UNEXPECTED_TOOL_CALL' ||
error.type === 'MAX_TOKENS_EXCEEDED' ||
error.type === 'SAFETY_BLOCKED' ||
error.type === 'RECITATION_BLOCKED' ||
error.type === 'OTHER_BLOCKED' ||
error.type === 'THINKING_ONLY_RESPONSE'))
) {
// The stream ended with an empty response or malformed tool call.
// Treat this as a graceful end to the model's turn rather than a crash.
Expand Down
65 changes: 55 additions & 10 deletions packages/cli/src/nonInteractiveCli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
CoreEvent,
CoreToolCallStatus,
JsonStreamEventType,
TRUE_EMPTY_RESPONSE_MESSAGE,
} from '@google/gemini-cli-core';
import type { Part } from '@google/genai';
import { runNonInteractive } from './nonInteractiveCli.js';
Expand Down Expand Up @@ -78,6 +79,7 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
ChatRecordingService: MockChatRecordingService,
uiTelemetryService: {
getMetrics: vi.fn(),
recordSemanticValidationError: vi.fn(),
},
coreEvents: mockCoreEvents,
createWorkingStdio: vi.fn(() => ({
Expand Down Expand Up @@ -110,6 +112,7 @@ describe('runNonInteractive', () => {
sendMessageStream: Mock;
resumeChat: Mock;
getChatRecordingService: Mock;
getCurrentSequenceModel: Mock;
};
const MOCK_SESSION_METRICS: SessionMetrics = {
models: {},
Expand Down Expand Up @@ -165,6 +168,7 @@ describe('runNonInteractive', () => {
recordMessageTokens: vi.fn(),
recordToolCalls: vi.fn(),
})),
getCurrentSequenceModel: vi.fn().mockReturnValue('gemini-2.5-flash'),
};

mockConfig = {
Expand Down Expand Up @@ -193,6 +197,7 @@ describe('runNonInteractive', () => {
getRawOutput: vi.fn().mockReturnValue(false),
getAcceptRawOutputRisk: vi.fn().mockReturnValue(false),
getAgentSessionNoninteractiveEnabled: vi.fn().mockReturnValue(false),
getUsageStatisticsEnabled: vi.fn().mockReturnValue(false),
} as unknown as Config;

mockSettings = {
Expand Down Expand Up @@ -1820,7 +1825,6 @@ describe('runNonInteractive', () => {
};
// @ts-expect-error - Mocking internal structure
mockGeminiClient.getChat = vi.fn().mockReturnValue(mockChat);
// @ts-expect-error - Mocking internal structure
mockGeminiClient.getCurrentSequenceModel = vi
.fn()
.mockReturnValue('model-1');
Expand Down Expand Up @@ -2298,7 +2302,13 @@ describe('runNonInteractive', () => {

it('should handle InvalidStream event gracefully in TEXT mode', async () => {
const events: ServerGeminiStreamEvent[] = [
{ type: GeminiEventType.InvalidStream },
{
type: GeminiEventType.InvalidStream,
value: {
type: 'NO_RESPONSE_TEXT',
message: 'Empty response',
},
},
];
mockGeminiClient.sendMessageStream.mockReturnValue(
createStreamFromEvents(events),
Expand All @@ -2312,7 +2322,7 @@ describe('runNonInteractive', () => {
});

expect(processStderrSpy).toHaveBeenCalledWith(
'[ERROR] Invalid stream: The model returned an empty response or malformed tool call.\n',
`[ERROR] ${TRUE_EMPTY_RESPONSE_MESSAGE}\n`,
);
expect(mockGeminiClient.sendMessageStream).toHaveBeenCalledTimes(1);
});
Expand All @@ -2325,7 +2335,13 @@ describe('runNonInteractive', () => {
OutputFormat.STREAM_JSON,
);
const events: ServerGeminiStreamEvent[] = [
{ type: GeminiEventType.InvalidStream },
{
type: GeminiEventType.InvalidStream,
value: {
type: 'NO_RESPONSE_TEXT',
message: 'Empty response',
},
},
];
mockGeminiClient.sendMessageStream.mockReturnValue(
createStreamFromEvents(events),
Expand All @@ -2341,9 +2357,7 @@ describe('runNonInteractive', () => {
const output = getWrittenOutput();
expect(output).toContain('"type":"error"');
expect(output).toContain('"severity":"error"');
expect(output).toContain(
'Invalid stream: The model returned an empty response or malformed tool call.',
);
expect(output).toContain(TRUE_EMPTY_RESPONSE_MESSAGE);
expect(mockGeminiClient.sendMessageStream).toHaveBeenCalledTimes(1);
});

Expand All @@ -2355,7 +2369,13 @@ describe('runNonInteractive', () => {
OutputFormat.JSON,
);
const events: ServerGeminiStreamEvent[] = [
{ type: GeminiEventType.InvalidStream },
{
type: GeminiEventType.InvalidStream,
value: {
type: 'NO_RESPONSE_TEXT',
message: 'Empty response',
},
},
];
mockGeminiClient.sendMessageStream.mockReturnValue(
createStreamFromEvents(events),
Expand All @@ -2371,8 +2391,33 @@ describe('runNonInteractive', () => {
const output = getWrittenOutput();
expect(output).toContain('"error": {');
expect(output).toContain('"type": "INVALID_STREAM"');
expect(output).toContain(
'Invalid stream: The model returned an empty response or malformed tool call.',
expect(output).toContain(TRUE_EMPTY_RESPONSE_MESSAGE);
expect(mockGeminiClient.sendMessageStream).toHaveBeenCalledTimes(1);
});

it('should handle non-NO_RESPONSE_TEXT InvalidStream event gracefully and use message from eventValue', async () => {
const events: ServerGeminiStreamEvent[] = [
{
type: GeminiEventType.InvalidStream,
value: {
type: 'MALFORMED_FUNCTION_CALL',
message: 'Custom malformed function call message',
},
},
];
mockGeminiClient.sendMessageStream.mockReturnValue(
createStreamFromEvents(events),
);

await runNonInteractive({
config: mockConfig,
settings: mockSettings,
input: 'test invalid stream malformed',
prompt_id: 'prompt-id-invalid-malformed',
});

expect(processStderrSpy).toHaveBeenCalledWith(
'[ERROR] Custom malformed function call message\n',
);
expect(mockGeminiClient.sendMessageStream).toHaveBeenCalledTimes(1);
});
Expand Down
33 changes: 31 additions & 2 deletions packages/cli/src/nonInteractiveCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ import {
ToolErrorType,
Scheduler,
ROOT_SCHEDULER_ID,
THINKING_ONLY_COMPRESS_SUGGESTION,
MAX_TOKENS_EXCEEDED_SUGGESTION,
SAFETY_BLOCKED_MESSAGE,
RECITATION_BLOCKED_MESSAGE,
OTHER_BLOCKED_MESSAGE,
TRUE_EMPTY_RESPONSE_MESSAGE,
} from '@google/gemini-cli-core';

import type { Content, Part } from '@google/genai';
Expand Down Expand Up @@ -433,8 +439,31 @@ export async function runNonInteractive(
}
warnings.push(blockMessage);
} else if (event.type === GeminiEventType.InvalidStream) {
invalidStreamError =
'Invalid stream: The model returned an empty response or malformed tool call.';
const eventValue = event.value;
if (eventValue?.type === 'NO_RESPONSE_TEXT') {
invalidStreamError = TRUE_EMPTY_RESPONSE_MESSAGE;
} else if (eventValue?.type === 'THINKING_ONLY_RESPONSE') {
invalidStreamError = THINKING_ONLY_COMPRESS_SUGGESTION;
} else if (eventValue?.type === 'MAX_TOKENS_EXCEEDED') {
invalidStreamError = MAX_TOKENS_EXCEEDED_SUGGESTION;
} else if (eventValue?.type === 'SAFETY_BLOCKED') {
invalidStreamError = SAFETY_BLOCKED_MESSAGE;
} else if (eventValue?.type === 'RECITATION_BLOCKED') {
invalidStreamError = RECITATION_BLOCKED_MESSAGE;
} else if (eventValue?.type === 'OTHER_BLOCKED') {
invalidStreamError = OTHER_BLOCKED_MESSAGE;
} else {
invalidStreamError =
eventValue?.message?.trim() ||
'Invalid stream: The model returned an empty response or malformed tool call.';
}

// Log semantic error telemetry without double-counting requests
uiTelemetryService.recordSemanticValidationError(
geminiClient.getCurrentSequenceModel() ?? config.getModel(),
eventValue?.type || 'INVALID_STREAM',
);

if (streamFormatter) {
streamFormatter.emitEvent({
type: JsonStreamEventType.ERROR,
Expand Down
Loading
Loading