// The live gate's rule (evaluateStuckLoop) and the history scan's // (evaluateLoopWindow), driven with call sequences shaped like real sessions. import { describe, it, expect } from 'vitest'; import { evaluateLoopWindow, evaluateStuckLoop, LOOP_MAX_RECORDS, type ToolCallRecord } from '.'; const THRESHOLD = 5; const WINDOW_MS = 120_000; const T0 = 1_800_000_000_000; interface Call { tool: string; args: unknown; session?: string; } /** Runs the calls three seconds apart; returns the 1-based numbers of the calls flagged as looping. */ function flagged( rule: 'stuck' | 'window', calls: Call[], opts: { stepMs?: number; records?: ToolCallRecord[] } = {} ): number[] { let records = opts.records ?? []; const out: number[] = []; calls.forEach((c, i) => { const now = T0 + i * (opts.stepMs ?? 3_000); const r = rule === 'stuck' ? evaluateStuckLoop(records, c.tool, c.args, THRESHOLD, WINDOW_MS, now, c.session) : evaluateLoopWindow(records, c.tool, c.args, THRESHOLD, WINDOW_MS, now); records = r.nextRecords; if (r.looping) out.push(i + 1); }); return out; } const bash = (command: string, session?: string): Call => ({ tool: 'Bash', args: { command }, session, }); const edit = (n: number): Call => ({ tool: 'Edit', args: { file_path: '/repo/src/app.ts', old_string: `v${n}`, new_string: `v${n + 1}` }, }); // A browser agent as Playwright MCP sends it: navigate and click carry // arguments that change, browser_snapshot carries none. const navigate = (i: number): Call => ({ tool: 'mcp__playwright__browser_navigate', args: { url: `https://shop.example/page/${i}` }, }); const click = (i: number): Call => ({ tool: 'mcp__playwright__browser_click', args: { element: `Result ${i}`, ref: `e${i}` }, }); const snapshot: Call = { tool: 'mcp__playwright__browser_snapshot', args: {} }; const browsing = (rounds: number): Call[] => Array.from({ length: rounds }, (_, i) => [navigate(i), snapshot, click(i), snapshot]).flat(); describe('evaluateStuckLoop: ordinary work is not a loop', () => { it('lets a browser agent snapshot between every navigation and click', () => { // REGRESSION (2026-11-27, an outside user on 2.30.1): nine "Loop Detected" // denials on browser_snapshot in one browsing session. The snapshot has no // arguments, so the fifth one in two minutes was denied whatever ran between. expect(flagged('stuck', browsing(10))).toEqual([]); }); it('the recurrence count used before does deny that session', () => { // The same sequence through the old rule: the bug this file pins. expect(flagged('window', browsing(10)).length).toBeGreaterThan(0); expect(flagged('window', browsing(10))[0]).toBe(10); // the fifth snapshot }); it('lets a fix cycle run the same test after every new edit', () => { const calls = Array.from({ length: 8 }, (_, i) => [edit(i), bash('npm test')]).flat(); expect(flagged('stuck', calls)).toEqual([]); }); it('lets a repeated status read through while other calls are new', () => { const status: Call = { tool: 'mcp__ccd_pr__get_status', args: {} }; const calls = Array.from({ length: 8 }, (_, i) => [status, bash(`step ${i}`)]).flat(); expect(flagged('stuck', calls)).toEqual([]); }); }); describe('evaluateStuckLoop: a stuck agent is still stopped', () => { it('flags the fifth identical call in a row, and every one after it', () => { expect(flagged('stuck', Array(7).fill(bash('npm test')))).toEqual([5, 6, 7]); }); it('flags a two-call cycle with identical arguments', () => { const calls = Array.from({ length: 8 }, () => [bash('npm test'), bash('cat out.log')]).flat(); // Round 1 is new; the fifth repeat after it is call 10. expect(flagged('stuck', calls)[0]).toBe(10); }); it('flags a three-call cycle with identical arguments', () => { const calls = Array.from({ length: 8 }, () => [bash('a'), bash('b'), bash('c')]).flat(); expect(flagged('stuck', calls)[0]).toBe(15); }); it('flags the same edit and the same test repeated', () => { const calls = Array.from({ length: 8 }, () => [edit(1), bash('npm test')]).flat(); expect(flagged('stuck', calls)[0]).toBe(10); }); it('one new call inside a loop only delays the block', () => { const t = bash('npm test'); const calls = [t, t, bash('ls'), t, t, t, t, t]; // Counted from the new call on: the block lands on the fifth repeat after it. expect(flagged('stuck', calls)).toEqual([8]); }); it('a call that already ran in the window is not new, so it does not reset the count', () => { const t = bash('npm test'); const look = bash('git status'); const calls = [look, t, t, look, t, t, look, t]; expect(flagged('stuck', calls)).toEqual([8]); }); it('reports the repeats since the latest new call as the count', () => { let records: ToolCallRecord[] = []; const seq = [bash('npm test'), bash('npm test'), bash('ls'), bash('npm test')]; let count = 0; seq.forEach((c, i) => { const r = evaluateStuckLoop(records, c.tool, c.args, THRESHOLD, WINDOW_MS, T0 + i * 1000); records = r.nextRecords; count = r.count; }); expect(count).toBe(1); }); }); describe('evaluateStuckLoop: the window', () => { it('forgets calls older than the window', () => { const calls = Array(6).fill(bash('npm test')); // 50 s apart: never more than three inside 120 s. expect(flagged('stuck', calls, { stepMs: 50_000 })).toEqual([]); }); it('a call whose earlier run left the window counts as new again', () => { let records: ToolCallRecord[] = []; const run = (c: Call, at: number) => { const r = evaluateStuckLoop(records, c.tool, c.args, THRESHOLD, WINDOW_MS, at); records = r.nextRecords; return r; }; run(bash('ls'), T0); for (let i = 1; i <= 4; i++) run(bash('npm test'), T0 + i * 1000); // `ls` left the window, so it is new here and the count starts over. run(bash('ls'), T0 + WINDOW_MS + 500); expect(run(bash('npm test'), T0 + WINDOW_MS + 1500).looping).toBe(false); }); it('caps the records it hands back', () => { let records: ToolCallRecord[] = []; for (let i = 0; i < LOOP_MAX_RECORDS + 50; i++) { records = evaluateStuckLoop( records, 'Bash', { command: `c${i}` }, THRESHOLD, WINDOW_MS, T0 + i ).nextRecords; } expect(records).toHaveLength(LOOP_MAX_RECORDS); }); }); describe('evaluateStuckLoop: one window per session', () => { it("another session's new calls do not hide a stuck session", () => { const calls = Array.from({ length: 6 }, (_, i) => [ bash('npm test', 'stuck-session'), bash(`work ${i}`, 'busy-session'), ]).flat(); // The stuck session's fifth identical call is call 9 of the mixed stream. expect(flagged('stuck', calls)).toEqual([9, 11]); }); it('the same command in two sessions is not one loop', () => { const calls = Array.from({ length: 4 }, () => [ bash('git status', 'session-a'), bash('git status', 'session-b'), ]).flat(); expect(flagged('stuck', calls)).toEqual([]); }); it('calls with no session id share one bucket', () => { expect(flagged('stuck', Array(5).fill(bash('npm test')))).toEqual([5]); }); it("keeps the other sessions' records in what it hands back", () => { const a = evaluateStuckLoop([], 'Bash', { command: 'x' }, THRESHOLD, WINDOW_MS, T0, 'a'); const b = evaluateStuckLoop( a.nextRecords, 'Bash', { command: 'x' }, THRESHOLD, WINDOW_MS, T0, 'b' ); expect(b.nextRecords.map((r) => r.s)).toEqual(['a', 'b']); expect(b.count).toBe(1); }); }); describe('evaluateStuckLoop: a state file written before the rule changed', () => { it('counts records with no new-call flag as plain repeats', () => { const h = evaluateStuckLoop([], 'Bash', { command: 'npm test' }, 1, 1, T0).nextRecords[0].h; const old: ToolCallRecord[] = Array.from({ length: 4 }, (_, i) => ({ t: 'Bash', h, ts: T0 - 10_000 + i, })); const r = evaluateStuckLoop(old, 'Bash', { command: 'npm test' }, THRESHOLD, WINDOW_MS, T0); expect(r.count).toBe(5); expect(r.looping).toBe(true); }); }); describe('evaluateLoopWindow: the history scan keeps counting every recurrence', () => { it('counts a call repeated between other calls', () => { // The report's "ran this N times" number is not the live gate's question. const calls = Array.from({ length: 6 }, (_, i) => [bash('npm test'), bash(`step ${i}`)]).flat(); expect(flagged('window', calls)).toEqual([9, 11]); expect(flagged('stuck', calls)).toEqual([]); }); });