Repository navigation
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
7ae499f
027505a
078965f
bff47ed
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) | ||
|
|
||
| /** 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)) | ||
|
Check warning on line 25 in src/core/task/ReasoningLoopDetector.ts
|
||
| 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,56 @@ | ||
| 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 ordinary long reasoning made of distinct lines", () => { | ||
| 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) | ||
| }) | ||
|
|
||
| it("does not flag a short phrase repeated a few times", () => { | ||
| const detector = new ReasoningLoopDetector() | ||
|
|
||
| expect(detector.add("Let me read the files.\n".repeat(10))).toBe(false) | ||
| }) | ||
|
|
||
| // A cycle of `length` characters that is not periodic itself, so only its own length can match. | ||
| const cycleOf = (length: number) => | ||
| Array.from({ length: 200 }, (_, index) => `w${index} `) | ||
| .join("") | ||
| .slice(0, length) | ||
|
|
||
| // MIN_PATTERN_LENGTH is 80 and REQUIRED_REPETITIONS is 5. | ||
| it.each([ | ||
| { length: 80, repetitions: 5, flagged: true }, | ||
| { length: 80, repetitions: 4, flagged: false }, | ||
| { length: 81, repetitions: 5, flagged: true }, | ||
| { length: 81, repetitions: 4, flagged: false }, | ||
| { length: 79, repetitions: 5, flagged: false }, | ||
| // 9 repeats hold only 4.5 copies of the doubled 158-character cycle, which is still below the threshold. | ||
| { length: 79, repetitions: 9, flagged: false }, | ||
| // 10 repeats are 5 copies of the doubled cycle, so a short cycle is still caught once it persists. | ||
| { length: 79, repetitions: 10, flagged: true }, | ||
| ])("flags a $length-character cycle repeated $repetitions times: $flagged", ({ length, repetitions, flagged }) => { | ||
| const detector = new ReasoningLoopDetector() | ||
|
|
||
| expect(detector.add(cycleOf(length).repeat(repetitions))).toBe(flagged) | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.