/** * Non-TTY verdict IPC — the two act:5335d1e2 / act:31b7af4c contracts: * * 1. A verdict supplied over the file IPC (stdin is not a TTY, so a * program — not the operator — owns the channel) is recorded as * `unverified:`, never `human:`, whatever `source` the * responder claims. * 2. A response must name the check it answers. A missing or mismatched * checkId is refused (logged as a `verdict-refused` progress event) * and the harness keeps waiting on the current pause — so a late * answer for pause N can never be consumed as pause N+1's verdict. * * Drives the REAL askHumanVerdict with a fake Playwright page and a * scripted responder. The IPC timeout is shortened via * CABINET_VERIFY_VERDICT_TIMEOUT_MS so the race plays out in seconds. */ import { test } from 'node:test'; import assert from 'node:assert/strict'; import * as fs from 'node:fs'; import * as fsp from 'node:fs/promises'; import * as os from 'node:os'; import * as path from 'node:path'; import type { Page } from '@playwright/test'; import { startRun, setScenarioContext, endRun, getCurrentRows } from '../src/verdict-recorder.js'; import { askHumanVerdict, verdictTimeoutMs } from '../src/human-verdict.js'; import { initProgress } from '../src/progress.js'; const fakePage = { screenshot: async ({ path: p }: { path: string }) => { fs.writeFileSync(p, Buffer.alloc(4096, 1)); return Buffer.alloc(0); }, } as unknown as Page; function forceNonTty(): void { Object.defineProperty(process.stdin, 'isTTY', { value: false, configurable: true, writable: true }); } async function inTmpRun(fn: (dir: string) => Promise): Promise { const dir = await fsp.mkdtemp(path.join(os.tmpdir(), 'cabinet-verify-ipc-')); const cwd = process.cwd(); process.chdir(dir); forceNonTty(); initProgress(dir); try { return await fn(dir); } finally { try { await endRun(); } catch { /* already ended */ } process.chdir(cwd); await fsp.rm(dir, { recursive: true, force: true }); } } const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); function progressEvents(dir: string): Array> { return fs .readFileSync(path.join(dir, '.verify-progress.jsonl'), 'utf8') .split('\n') .filter(Boolean) .map((l) => JSON.parse(l)); } /** Wait until the pending file names `checkId`, then write a response. */ async function respondWhenPending(dir: string, checkId: string, body: Record, delayMs = 0): Promise { const pending = path.join(dir, '.verdict-pending.json'); for (let i = 0; i < 200; i++) { try { const p = JSON.parse(fs.readFileSync(pending, 'utf8')); if (p.checkId === checkId) break; } catch { /* not yet */ } await sleep(25); } if (delayMs) await sleep(delayMs); fs.writeFileSync(path.join(dir, '.verdict-response.json'), JSON.stringify(body)); } test('verdictTimeoutMs: default 10 min; env override; garbage falls back', () => { assert.equal(verdictTimeoutMs({}), 600_000); assert.equal(verdictTimeoutMs({ CABINET_VERIFY_VERDICT_TIMEOUT_MS: '2500' }), 2500); assert.equal(verdictTimeoutMs({ CABINET_VERIFY_VERDICT_TIMEOUT_MS: '0' }), 600_000); assert.equal(verdictTimeoutMs({ CABINET_VERIFY_VERDICT_TIMEOUT_MS: 'soon' }), 600_000); }); test('non-TTY: a response claiming source=human is recorded unverified, never human:P', async () => { await inTmpRun(async (dir) => { await startRun(); setScenarioContext('features/x.feature', 'IPC', 'user'); const responder = respondWhenPending(dir, '1.01', { checkId: '1.01', verdict: 'P', notes: 'looks great', source: 'human' }); const result = await askHumanVerdict(fakePage, '1.01', 'does it look right?'); await responder; assert.equal(result.verdict, 'P'); const row = getCurrentRows().at(-1)!; assert.equal(row.verdict, 'unverified:P'); assert.equal(row.source, 'unverified'); assert.notEqual(row.verdict, 'human:P'); assert.match(row.notes ?? '', /non-TTY/); assert.match(row.notes ?? '', /source=human/); assert.match(row.notes ?? '', /looks great/); }); }); test('non-TTY: a response claiming source=claude is ALSO recorded unverified (the other half of the guard)', async () => { await inTmpRun(async (dir) => { await startRun(); setScenarioContext('features/x.feature', 'IPC', 'user'); const responder = respondWhenPending(dir, '1.02', { checkId: '1.02', verdict: 'I', notes: '[claude-auto, confidence:high] button clipped', source: 'claude' }); await askHumanVerdict(fakePage, '1.02', 'is the button whole?'); await responder; const row = getCurrentRows().at(-1)!; assert.equal(row.verdict, 'unverified:I'); assert.equal(row.source, 'unverified'); assert.match(row.notes ?? '', /source=claude/); }); }); test('non-TTY: a response with NO checkId is refused and the harness keeps waiting', async () => { await inTmpRun(async (dir) => { process.env.CABINET_VERIFY_VERDICT_TIMEOUT_MS = '8000'; try { await startRun(); setScenarioContext('features/x.feature', 'IPC', 'user'); const responder = (async () => { await respondWhenPending(dir, '2.01', { verdict: 'P', notes: 'no id', source: 'human' }); await sleep(1200); // long enough for the poll to see it fs.writeFileSync(path.join(dir, '.verdict-response.json'), JSON.stringify({ checkId: '2.01', verdict: 'N', notes: 'with id', source: 'human' })); })(); const result = await askHumanVerdict(fakePage, '2.01', 'q'); await responder; assert.equal(result.verdict, 'N', 'the id-less response must not have been consumed'); const row = getCurrentRows().at(-1)!; assert.equal(row.verdict, 'unverified:N'); assert.match(row.notes ?? '', /with id/); const refused = progressEvents(dir).filter((e) => e.event === 'verdict-refused'); assert.equal(refused.length, 1); assert.equal(refused[0].checkId, '2.01'); assert.equal(refused[0].reason, 'check-id-missing'); } finally { delete process.env.CABINET_VERIFY_VERDICT_TIMEOUT_MS; } }); }); test('non-TTY: a response for a DIFFERENT checkId is refused, not recorded against the current pause', async () => { await inTmpRun(async (dir) => { process.env.CABINET_VERIFY_VERDICT_TIMEOUT_MS = '8000'; try { await startRun(); setScenarioContext('features/x.feature', 'IPC', 'user'); const responder = (async () => { await respondWhenPending(dir, '3.02', { checkId: '3.01', verdict: 'P', notes: 'meant for 3.01', source: 'human' }); await sleep(1200); fs.writeFileSync(path.join(dir, '.verdict-response.json'), JSON.stringify({ checkId: '3.02', verdict: 'P', notes: 'meant for 3.02', source: 'human' })); })(); await askHumanVerdict(fakePage, '3.02', 'q'); await responder; const row = getCurrentRows().at(-1)!; assert.equal(row.checkId, '3.02'); assert.match(row.notes ?? '', /meant for 3\.02/); assert.doesNotMatch(row.notes ?? '', /meant for 3\.01/); const refused = progressEvents(dir).filter((e) => e.event === 'verdict-refused'); assert.equal(refused.length, 1); assert.equal(refused[0].reason, 'check-id-mismatch'); assert.equal(refused[0].responseCheckId, '3.01'); assert.ok(!fs.existsSync(path.join(dir, '.verdict-response.json')), 'refused response file is removed'); } finally { delete process.env.CABINET_VERIFY_VERDICT_TIMEOUT_MS; } }); }); test('act:31b7af4c reproduction: one late answer must not shift every later verdict onto the next check', async () => { await inTmpRun(async (dir) => { const timeoutMs = 1500; process.env.CABINET_VERIFY_VERDICT_TIMEOUT_MS = String(timeoutMs); try { await startRun(); setScenarioContext('features/x.feature', 'Race', 'user'); const pauses = ['5.11', '5.12', '5.13', '5.14']; // The scripted driving agent: answers each pause it is notified of, // naming the check it believes it is answering. Pause 5.12 is // answered LATE — after the harness's timeout — the way a session // blocked on the operator does. It processes notifications // serially with a realistic per-turn latency. const agent = (async () => { const seen = new Set(); for (;;) { const evs = fs.existsSync(path.join(dir, '.verify-progress.jsonl')) ? progressEvents(dir) : []; const next = evs.find((e) => e.event === 'verdict-pending' && !seen.has(String(e.checkId))); if (next) { const id = String(next.checkId); seen.add(id); await sleep(id === '5.12' ? timeoutMs + 800 : 300); fs.writeFileSync(path.join(dir, '.verdict-response.json'), JSON.stringify({ checkId: id, verdict: 'P', notes: `answered for ${id}`, source: 'human' })); } if (seen.size === pauses.length) return; await sleep(50); } })(); for (const id of pauses) await askHumanVerdict(fakePage, id, `check ${id}`); await agent; const rows = getCurrentRows().filter((r) => r.source !== 'auto'); assert.equal(rows.length, pauses.length); const table = rows.map((r) => ({ check: r.checkId, verdict: r.verdict, notes: r.notes })); // 5.12 timed out (nobody answered in time) — recorded as a skip. assert.equal(table[1].verdict, 'human:S', JSON.stringify(table)); // Every ANSWERED row carries the answer meant for ITSELF. Before the // fix, 5.13 held "answered for 5.12", 5.14 held "answered for 5.13". for (const t of table) { if (t.verdict === 'human:S') continue; assert.match(t.notes ?? '', new RegExp(`answered for ${t.check.replace('.', '\\.')}$`), JSON.stringify(table)); } const refused = progressEvents(dir).filter((e) => e.event === 'verdict-refused'); assert.equal(refused.length, 1, 'exactly the late 5.12 answer was refused'); assert.equal(refused[0].responseCheckId, '5.12'); assert.equal(refused[0].checkId, '5.13'); } finally { delete process.env.CABINET_VERIFY_VERDICT_TIMEOUT_MS; } }); });