-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(core): Add infinite loop protection to client #2793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
da127b3
928c165
6df60ef
ac74b9b
db7aef4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -219,7 +219,9 @@ export class GeminiClient { | |||||
signal: AbortSignal, | ||||||
turns: number = this.MAX_TURNS, | ||||||
): AsyncGenerator<ServerGeminiStreamEvent, Turn> { | ||||||
if (!turns) { | ||||||
// Ensure turns never exceeds MAX_TURNS to prevent infinite loops | ||||||
const boundedTurns = Math.min(turns, this.MAX_TURNS); | ||||||
if (!boundedTurns) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check
Suggested change
|
||||||
return new Turn(this.getChat()); | ||||||
} | ||||||
|
||||||
|
@@ -242,7 +244,7 @@ export class GeminiClient { | |||||
const nextRequest = [{ text: 'Please continue.' }]; | ||||||
// This recursive call's events will be yielded out, but the final | ||||||
// turn object will be from the top-level call. | ||||||
yield* this.sendMessageStream(nextRequest, signal, turns - 1); | ||||||
yield* this.sendMessageStream(nextRequest, signal, boundedTurns - 1); | ||||||
} | ||||||
} | ||||||
return turn; | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertions in this test are not as strict as they could be, and the conditional logic makes the test's intent unclear. Given the test setup, where
checkNextSpeaker
is mocked to always trigger recursion, the number of calls should be exactly100
(the value ofMAX_TURNS
).The current logic with
if/else if/else
andconsole.log
statements suggests uncertainty about the outcome, but a good test should have a predictable result. A more direct assertion would make the test stronger and easier to understand.