-
Notifications
You must be signed in to change notification settings - Fork 302
Stop repetitive reasoning streams before provider timeout #1904
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| const MIN_PATTERN_LENGTH = 80 | ||
| const MAX_PATTERN_LENGTH = 2_048 | ||
| const REQUIRED_REPETITIONS = 5 | ||
| const CHECK_INTERVAL = 64 | ||
| const BUFFER_LENGTH = MAX_PATTERN_LENGTH * (REQUIRED_REPETITIONS + 1) | ||
|
Check warning on line 5 in src/core/task/ReasoningLoopDetector.ts
|
||
|
|
||
| /** Detects sustained, exact repetition in streamed model reasoning. */ | ||
| export class ReasoningLoopDetector { | ||
| private buffer = "" | ||
|
Check warning on line 9 in src/core/task/ReasoningLoopDetector.ts
|
||
| private uncheckedCharacters = 0 | ||
|
|
||
| add(text: string): boolean { | ||
| if (!text) { | ||
|
Check warning on line 13 in src/core/task/ReasoningLoopDetector.ts
|
||
| return false | ||
|
Check warning on line 14 in src/core/task/ReasoningLoopDetector.ts
|
||
| } | ||
|
|
||
| this.buffer = (this.buffer + text).slice(-BUFFER_LENGTH) | ||
|
Check warning on line 17 in src/core/task/ReasoningLoopDetector.ts
|
||
| this.uncheckedCharacters += text.length | ||
|
|
||
| if (this.uncheckedCharacters < CHECK_INTERVAL) { | ||
|
Check warning on line 20 in src/core/task/ReasoningLoopDetector.ts
|
||
| return false | ||
|
Check warning on line 21 in src/core/task/ReasoningLoopDetector.ts
|
||
| } | ||
| this.uncheckedCharacters = 0 | ||
|
|
||
| const maxPatternLength = Math.min(MAX_PATTERN_LENGTH, Math.floor(this.buffer.length / REQUIRED_REPETITIONS)) | ||
| for (let patternLength = MIN_PATTERN_LENGTH; patternLength <= maxPatternLength; patternLength++) { | ||
| const patternStart = this.buffer.length - patternLength | ||
| const pattern = this.buffer.slice(patternStart) | ||
| let repeats = true | ||
|
|
||
| for (let repetition = 2; repetition <= REQUIRED_REPETITIONS; repetition++) { | ||
| const start = this.buffer.length - patternLength * repetition | ||
| if (this.buffer.slice(start, start + patternLength) !== pattern) { | ||
| repeats = false | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if (repeats) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
| } | ||
|
|
||
| export class RepetitiveReasoningError extends Error { | ||
| constructor() { | ||
| super( | ||
| "Repetitive reasoning detected. The model appears to be stuck in a loop, so Zoo Code stopped the request. Retry with less context or a different model.", | ||
| ) | ||
| this.name = "RepetitiveReasoningError" | ||
| Object.setPrototypeOf(this, RepetitiveReasoningError.prototype) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { ReasoningLoopDetector } from "../ReasoningLoopDetector" | ||
|
|
||
| describe("ReasoningLoopDetector", () => { | ||
| it("detects the reported reasoning loop across arbitrary stream chunks", () => { | ||
| const detector = new ReasoningLoopDetector() | ||
| const cycle = "OK.\n\nHmm. Let me read them.\n\nOK.\n\nHmm. Let me just do it.\n\nLet me read the files.\n\n" | ||
| const output = `I will inspect the implementation first.\n${cycle.repeat(8)}` | ||
| let detected = false | ||
|
|
||
| for (let offset = 0; offset < output.length; offset += 17) { | ||
| detected ||= detector.add(output.slice(offset, offset + 17)) | ||
| } | ||
|
|
||
| expect(detected).toBe(true) | ||
| }) | ||
|
|
||
| it("does not flag repeated short phrases or ordinary long reasoning", () => { | ||
| const detector = new ReasoningLoopDetector() | ||
| const reasoning = Array.from( | ||
| { length: 40 }, | ||
| (_, index) => | ||
| `Step ${index}: inspect file ${index}, compare its behavior, and record the distinct result. OK.\n`, | ||
| ).join("") | ||
|
|
||
| expect(detector.add(reasoning)).toBe(false) | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.