-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: detect and abort repetitive reasoning loops (#11337) #11338
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
Draft
roomote
wants to merge
1
commit into
main
Choose a base branch
from
fix/reasoning-repetition-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+445
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /** | ||
| * Detects repetitive patterns in model reasoning/thinking output during streaming. | ||
| * | ||
| * Some models (particularly Gemini) can get stuck in a loop where their | ||
| * thinking/reasoning output repeats the same lines over and over, e.g.: | ||
| * "I'll mention that I verified with tests." | ||
| * "I'll mention that I reverted the tests." | ||
| * "I'll mention that I verified with tests." | ||
| * "I'll mention that I reverted the tests." | ||
| * ... | ||
| * | ||
| * This detector tracks lines as they stream in and flags when any single | ||
| * line has been repeated more than the configured threshold. | ||
| */ | ||
| export class ReasoningRepetitionDetector { | ||
| private lineCounts: Map<string, number> = new Map() | ||
| private buffer: string = "" | ||
| private readonly repetitionThreshold: number | ||
| private readonly minLineLength: number | ||
|
|
||
| /** | ||
| * @param repetitionThreshold Number of times a line must repeat to be considered a loop (default: 5) | ||
| * @param minLineLength Minimum line length to track - short lines are ignored (default: 20) | ||
| */ | ||
| constructor(repetitionThreshold: number = 5, minLineLength: number = 20) { | ||
| this.repetitionThreshold = repetitionThreshold | ||
| this.minLineLength = minLineLength | ||
| } | ||
|
|
||
| /** | ||
| * Feed a new chunk of reasoning text and check for repetition. | ||
| * | ||
| * @param chunk A new piece of reasoning/thinking text from the stream | ||
| * @returns true if repetitive looping has been detected | ||
| */ | ||
| public addChunk(chunk: string): boolean { | ||
| this.buffer += chunk | ||
|
|
||
| // Split buffer into complete lines (keeping incomplete last line in buffer) | ||
| const lines = this.buffer.split("\n") | ||
|
|
||
| // Keep the last element as the buffer (it may be an incomplete line) | ||
| this.buffer = lines.pop() ?? "" | ||
|
|
||
| for (const rawLine of lines) { | ||
| const line = this.normalizeLine(rawLine) | ||
|
|
||
| if (line.length < this.minLineLength) { | ||
| continue | ||
| } | ||
|
|
||
| const count = (this.lineCounts.get(line) ?? 0) + 1 | ||
| this.lineCounts.set(line, count) | ||
|
|
||
| if (count >= this.repetitionThreshold) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| /** | ||
| * Check if any line in the accumulated reasoning has hit the repetition threshold. | ||
| * Useful for checking after a stream is complete but before tool processing. | ||
| */ | ||
| public isRepetitive(): boolean { | ||
| // Also process any remaining buffer content | ||
| if (this.buffer.length > 0) { | ||
| const line = this.normalizeLine(this.buffer) | ||
| if (line.length >= this.minLineLength) { | ||
| const count = (this.lineCounts.get(line) ?? 0) + 1 | ||
| this.lineCounts.set(line, count) | ||
| if (count >= this.repetitionThreshold) { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const count of this.lineCounts.values()) { | ||
| if (count >= this.repetitionThreshold) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| /** | ||
| * Get the most repeated line and its count, useful for diagnostics. | ||
| */ | ||
| public getMostRepeatedLine(): { line: string; count: number } | undefined { | ||
| let maxLine: string | undefined | ||
| let maxCount = 0 | ||
|
|
||
| for (const [line, count] of this.lineCounts.entries()) { | ||
| if (count > maxCount) { | ||
| maxCount = count | ||
| maxLine = line | ||
| } | ||
| } | ||
|
|
||
| if (maxLine !== undefined) { | ||
| return { line: maxLine, count: maxCount } | ||
| } | ||
|
|
||
| return undefined | ||
| } | ||
|
|
||
| /** | ||
| * Reset the detector state. Called at the start of each new API request. | ||
| */ | ||
| public reset(): void { | ||
| this.lineCounts.clear() | ||
| this.buffer = "" | ||
| } | ||
|
|
||
| /** | ||
| * Normalize a line for comparison: trim whitespace, collapse internal | ||
| * whitespace, and lowercase. | ||
| */ | ||
| private normalizeLine(line: string): string { | ||
| return line.trim().replace(/\s+/g, " ").toLowerCase() | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
isRepetitive()mutateslineCountsas a side effect: each call increments the count for whatever is sitting in the buffer. If this method is called more than once without an interveningaddChunk()orreset(), the buffered line's count keeps climbing, and can cross the threshold even though the line only appeared once in the actual stream. Although the current Task.ts integration only usesaddChunk(), this is a public query method whose semantics suggest it should be idempotent.One fix: process the buffer into a local variable instead of writing back to
lineCounts, or clear the buffer after processing it.Fix it with Roo Code or mention @roomote and request a fix.