// The hook judges a call, finds no way to ask a person, starts the approval // daemon and judges the same call again (check.ts, cli.ts). The second // judgement must not put the call in the loop window a second time. import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import fs from 'fs'; import os from 'os'; import path from 'path'; import { _resetConfigCache } from '../config'; import { authorizeHeadless, _resetConfigCache as _resetCore } from '../core.js'; let origHome: string | undefined; let origUserProfile: string | undefined; let tmpHome: string; const stateFile = () => path.join(tmpHome, '.node9', 'loop-state.json'); const recorded = (): unknown[] => fs.existsSync(stateFile()) ? (JSON.parse(fs.readFileSync(stateFile(), 'utf-8')) as unknown[]) : []; beforeEach(() => { origHome = process.env.HOME; origUserProfile = process.env.USERPROFILE; tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'node9-loop-rejudged-')); fs.mkdirSync(path.join(tmpHome, '.node9'), { recursive: true }); process.env.HOME = tmpHome; process.env.USERPROFILE = tmpHome; delete process.env.NODE9_API_KEY; // Standard mode with no approver: a review has nobody to ask, which is the // state the hook is in when it starts the daemon and asks again. fs.writeFileSync( path.join(tmpHome, '.node9', 'config.json'), JSON.stringify({ settings: { mode: 'standard', approvalTimeoutMs: 0, approvers: { native: false, browser: false, cloud: false, terminal: false }, }, policy: {}, }) ); _resetConfigCache(); _resetCore(); }); afterEach(() => { if (origHome !== undefined) process.env.HOME = origHome; else delete process.env.HOME; if (origUserProfile !== undefined) process.env.USERPROFILE = origUserProfile; else delete process.env.USERPROFILE; fs.rmSync(tmpHome, { recursive: true, force: true }); _resetConfigCache(); _resetCore(); }); describe('a call judged twice by one hook run is one call in the loop window', () => { const tool = 'Bash'; const args = { command: 'git push --force origin main' }; const meta = { agent: 'Claude Code', sessionId: 'session-1' }; it('the first judgement finds nobody to ask and records the call once', async () => { const first = await authorizeHeadless(tool, args, meta); expect(first.noApprovalMechanism).toBe(true); expect(recorded()).toHaveLength(1); }); it('the second judgement of the same call adds nothing', async () => { await authorizeHeadless(tool, args, meta); const retry = await authorizeHeadless(tool, args, meta, { rejudged: true }); expect(retry.blockedBy).not.toBe('loop-detection'); expect(recorded()).toHaveLength(1); }); it('three such calls stay three repeats, under the threshold of five', async () => { // Counted twice each, the third call was the fifth and sixth repeat: denied as a loop. for (let i = 0; i < 3; i++) { const first = await authorizeHeadless(tool, args, meta); expect(first.blockedBy).not.toBe('loop-detection'); const retry = await authorizeHeadless(tool, args, meta, { rejudged: true }); expect(retry.blockedBy).not.toBe('loop-detection'); } expect(recorded()).toHaveLength(3); }); it('a new call by the same session is still counted', async () => { await authorizeHeadless(tool, args, meta); await authorizeHeadless(tool, args, meta); expect(recorded()).toHaveLength(2); }); });