// 集成测试用的「假 worker」:走完整 outbox 协议,但【不调用 SDK / 不建 worktree】,零 token。 // 用法:daemon 通过 MAESTRO_WORKER_CMD='npx tsx test/fixtures/fake-worker.ts' spawn 它,argv[2]=runId。 // 行为:emit started + 持续刷心跳;轮询 runs//finish 哨兵文件出现后 emit result+done 退出。 // 收到 SIGTERM → emit failed+done 退出。模拟一个「在跑的真进程」,供测试验证 spawn / 心跳 / re-adopt / ingest。 import { existsSync } from 'node:fs'; import { join } from 'node:path'; import { appendOutbox, readJobSpec, touchHeartbeat, runDir } from '../../src/executor/protocol.js'; const runId = process.argv[2]; if (!runId) { process.exit(2); } const job = readJobSpec(runId); touchHeartbeat(runId); appendOutbox(runId, { type: 'started', pid: process.pid, worktree: job.worktreeDir, branch: job.branch, model: 'fake' }); const hb = setInterval(() => touchHeartbeat(runId), 300); const finishFile = join(runDir(runId), 'finish'); const poll = setInterval(() => { if (!existsSync(finishFile)) return; clearInterval(hb); clearInterval(poll); appendOutbox(runId, { type: 'result', branch: job.branch, worktree: job.worktreeDir, diffSummary: ' demo.txt | 1 +', commits: ['abc1234 fake commit'], executor: { transcriptRef: null, sessionId: 'sess-fake' }, code: { summary: 'code ok', verdict: 'approve', transcriptRef: null }, security: { summary: 'sec ok', verdict: 'approve', transcriptRef: null }, }); appendOutbox(runId, { type: 'done' }); process.exit(0); }, 80); process.on('SIGTERM', () => { clearInterval(hb); clearInterval(poll); appendOutbox(runId, { type: 'failed', error: 'worker 收到 SIGTERM', transcriptRef: null, sessionId: null }); appendOutbox(runId, { type: 'done' }); process.exit(0); });