Skip to content

Commit 7439e20

Browse files
terisukeclaude
andcommitted
refactor(session): extract detectRepetition to shared util
Move detectRepetition function and constants from processor.ts to a dedicated repetition.ts module. Test now imports production code instead of reimplementing the algorithm. Closes #69 (partial) Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent e04eef8 commit 7439e20

3 files changed

Lines changed: 24 additions & 46 deletions

File tree

‎packages/opencode/src/session/processor.ts‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ import { SessionStatus } from "./status"
1919
import { SessionSummary } from "./summary"
2020
import type { Provider } from "@/provider/provider"
2121
import { Question } from "@/question"
22+
import { detectRepetition, REPETITION_THRESHOLD } from "./repetition"
2223

2324
export namespace SessionProcessor {
2425
const DOOM_LOOP_THRESHOLD = 3
25-
// 50+ consecutive repeats of a 4-200 char pattern in the last 8KB indicates a model generation loop
26-
const REPETITION_THRESHOLD = 50
27-
const REPETITION_WINDOW = 8000
2826
const log = Log.create({ service: "session.processor" })
2927

3028
class RepetitionError extends Error {
@@ -34,24 +32,6 @@ export namespace SessionProcessor {
3432
}
3533
}
3634

37-
function detectRepetition(text: string): boolean {
38-
if (text.length < REPETITION_WINDOW) return false
39-
const tail = text.slice(-REPETITION_WINDOW)
40-
for (let len = 4; len <= 200; len++) {
41-
const pattern = tail.slice(-len)
42-
let count = 0
43-
let pos = tail.length - len
44-
while (pos >= 0) {
45-
if (tail.slice(pos, pos + len) === pattern) {
46-
count++
47-
pos -= len
48-
} else break
49-
}
50-
if (count >= REPETITION_THRESHOLD) return true
51-
}
52-
return false
53-
}
54-
5535
export type Result = "compact" | "stop" | "continue"
5636

5737
export type Event = LLM.Event
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 50+ consecutive repeats of a 4-200 char pattern in the last 8KB indicates a model generation loop
2+
export const REPETITION_THRESHOLD = 50
3+
export const REPETITION_WINDOW = 8000
4+
5+
export function detectRepetition(text: string): boolean {
6+
if (text.length < REPETITION_WINDOW) return false
7+
const tail = text.slice(-REPETITION_WINDOW)
8+
for (let len = 4; len <= 200; len++) {
9+
const pattern = tail.slice(-len)
10+
let count = 0
11+
let pos = tail.length - len
12+
while (pos >= 0) {
13+
if (tail.slice(pos, pos + len) === pattern) {
14+
count++
15+
pos -= len
16+
} else break
17+
}
18+
if (count >= REPETITION_THRESHOLD) return true
19+
}
20+
return false
21+
}

‎packages/opencode/test/session/repetition.test.ts‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
11
import { describe, test, expect } from "bun:test"
2-
3-
// detectRepetition is a namespace-internal function in SessionProcessor.
4-
// We replicate the algorithm here to test it in isolation.
5-
// Constants match processor.ts: REPETITION_THRESHOLD=50, REPETITION_WINDOW=8000
6-
7-
const REPETITION_THRESHOLD = 50
8-
const REPETITION_WINDOW = 8000
9-
10-
function detectRepetition(text: string): boolean {
11-
if (text.length < REPETITION_WINDOW) return false
12-
const tail = text.slice(-REPETITION_WINDOW)
13-
for (let len = 4; len <= 200; len++) {
14-
const pattern = tail.slice(-len)
15-
let count = 0
16-
let pos = tail.length - len
17-
while (pos >= 0) {
18-
if (tail.slice(pos, pos + len) === pattern) {
19-
count++
20-
pos -= len
21-
} else break
22-
}
23-
if (count >= REPETITION_THRESHOLD) return true
24-
}
25-
return false
26-
}
2+
import { detectRepetition } from "../../src/session/repetition"
3+
import { REPETITION_THRESHOLD, REPETITION_WINDOW } from "../../src/session/repetition"
274

285
describe("session.detectRepetition", () => {
296
test("no repetition returns false", () => {

0 commit comments

Comments
 (0)