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
49 changes: 49 additions & 0 deletions packages/core/src/core/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
GeminiEventType,
Turn,
type ChatCompressionInfo,
type ServerGeminiStreamEvent,
} from './turn.js';
import { getCoreSystemPrompt } from './prompts.js';
import { DEFAULT_GEMINI_MODEL_AUTO } from '../config/models.js';
Expand Down Expand Up @@ -1118,6 +1119,54 @@ ${JSON.stringify(
// The actual token calculation is unit tested in tokenCalculation.test.ts
});

it('should cleanly abort and return Turn on LoopDetected without unhandled promise rejections', async () => {
// Arrange
const mockStream = (async function* () {
// Yield an event that will trigger the loop detector
yield { type: 'content', value: 'Looping content' };
})();
mockTurnRunFn.mockReturnValue(mockStream);

const mockChat: Partial<GeminiChat> = {
addHistory: vi.fn(),
setTools: vi.fn(),
getHistory: vi.fn().mockReturnValue([]),
getLastPromptTokenCount: vi.fn(),
};
client['chat'] = mockChat as GeminiChat;

// Mock loop detector to return count > 1 on the first event (loop detected)
vi.spyOn(client['loopDetector'], 'addAndCheck').mockReturnValue({
count: 2,
});

const abortSpy = vi.spyOn(AbortController.prototype, 'abort');

// Act
const stream = client.sendMessageStream(
[{ text: 'Hi' }],
new AbortController().signal,
'prompt-id-1',
);

const events: ServerGeminiStreamEvent[] = [];
let finalResult: Turn | undefined;

while (true) {
const result = await stream.next();
if (result.done) {
finalResult = result.value;
break;
}
events.push(result.value);
}

// Assert
expect(events).toContainEqual({ type: GeminiEventType.LoopDetected });
expect(abortSpy).toHaveBeenCalled();
expect(finalResult).toBeInstanceOf(Turn);
});

it('should return the turn instance after the stream is complete', async () => {
// Arrange
const mockStream = (async function* () {
Expand Down
38 changes: 25 additions & 13 deletions packages/core/src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,27 +708,22 @@ export class GeminiClient {
let isError = false;
let isInvalidStream = false;

let loopDetectedAbort = false;
let loopRecoverResult: { detail?: string } | undefined;
for await (const event of resultStream) {
const loopResult = this.loopDetector.addAndCheck(event);
if (loopResult.count > 1) {
yield { type: GeminiEventType.LoopDetected };
controller.abort();
return turn;
loopDetectedAbort = true;
break;
} else if (loopResult.count === 1) {
if (boundedTurns <= 1) {
yield { type: GeminiEventType.MaxSessionTurns };
controller.abort();
return turn;
loopDetectedAbort = true;
break;
}
return yield* this._recoverFromLoop(
loopResult,
signal,
prompt_id,
boundedTurns,
isInvalidStreamRetry,
displayContent,
controller,
);
loopRecoverResult = loopResult;
break;
}
yield event;

Expand All @@ -742,6 +737,23 @@ export class GeminiClient {
}
}

if (loopDetectedAbort) {
controller.abort();
return turn;
}

if (loopRecoverResult) {
return yield* this._recoverFromLoop(
loopRecoverResult,
signal,
prompt_id,
boundedTurns,
isInvalidStreamRetry,
displayContent,
controller,
);
}

if (isError) {
return turn;
}
Expand Down
Loading