Skip to content

Commit c3ba4f2

Browse files
committed
fix(loop-detector): test now validates old loops are ignored
Create actual loop pattern (3 identical calls) in oldHistory to properly test that loops outside the recent history window are not detected. Uses varied tools in filler to avoid triggering alternating pattern. Addresses cubic-dev-ai review on PR code-yeongyu#1188
1 parent ade47ed commit c3ba4f2

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/hooks/loop-detector/index.test.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,26 @@ describe("loop-detector", () => {
9292

9393
test("only considers recent history window", () => {
9494
//#given
95-
const oldHistory: ToolCallRecord[] = Array.from({ length: 20 }, (_, i) => ({
96-
tool: "read",
97-
args: { path: `/old-${i}.ts` },
98-
timestamp: i,
99-
}))
100-
const recentHistory: ToolCallRecord[] = [
101-
{ tool: "read", args: { path: "/new.ts" }, timestamp: 21 },
102-
{ tool: "edit", args: { path: "/new.ts" }, timestamp: 22 },
95+
// Create a loop pattern in old history (3 identical calls = would trigger detection)
96+
const loopInOldHistory: ToolCallRecord[] = [
97+
{ tool: "read", args: { path: "/looped.ts" }, timestamp: 1 },
98+
{ tool: "read", args: { path: "/looped.ts" }, timestamp: 2 },
99+
{ tool: "read", args: { path: "/looped.ts" }, timestamp: 3 },
103100
]
104-
const history = [...oldHistory, ...recentHistory]
101+
// Add enough unique calls (varied tools) to push the loop outside the history window
102+
const tools = ["glob", "grep", "bash", "write", "lsp_diagnostics"]
103+
const fillerHistory: ToolCallRecord[] = Array.from({ length: 12 }, (_, i) => ({
104+
tool: tools[i % tools.length],
105+
args: { path: `/filler-${i}.ts` },
106+
timestamp: 10 + i,
107+
}))
108+
const history = [...loopInOldHistory, ...fillerHistory]
105109

106110
//#when
107111
const result = detectLoop(history)
108112

109113
//#then
110-
// Should not detect loop from old repeated reads
114+
// Loop in old history (outside window) should be ignored
111115
expect(result).toBeNull()
112116
})
113117
})

0 commit comments

Comments
 (0)