import fs from 'node:fs'; import path from 'node:path'; import { randomUUID } from 'node:crypto'; import { Store } from '../../src/server/store.mjs'; import { Runtime } from '../../src/server/runtime.mjs'; import { Provider } from '../../src/server/provider.mjs'; const [directory, phase] = process.argv.slice(2); const store = new Store(directory, { faultInjector: point => { if (phase === 'migration' && point === 'migration_records') process.exit(66); } }); const session = [...store.sessions.values()][0]; const project = store.project(session.projectId); const call = { id: 'crash-' + phase, type: 'function', function: { name: 'run_command', arguments: JSON.stringify({ command: 'node worker-effect.mjs', reason: 'Controlled crash fixture' }) } }; if (phase === 'after_state' || phase === 'after_event') { store.storage.faultInjector = point => { if (point === phase) process.exit(66); }; store.message(session, 'user', 'This transaction must roll back.'); throw new Error('Transaction crash point was not reached.'); } if (phase === 'accepted' || phase === 'queued') { const run = store.beginRun(session, 'Crash fixture task', false, store.config); if (phase === 'queued') { session.providerMessages.push({ role: 'assistant', content: '', tool_calls: [call] }); const event = { id: randomUUID(), kind: 'tool', phase: 'queued', tool: 'run_command', toolCallId: call.id, runId: run.id, taskId: run.taskId, label: 'Controlled fixture', createdAt: new Date().toISOString() }; session.events.push(event); store.registerInvocation(session, call, run, event); store.save(session); } process.exit(66); } const provider = new Provider(); let requests = 0; provider.complete = async () => { requests++; if (requests > 1 && phase === 'before_next_model') process.exit(66); return { message: requests === 1 ? { role: 'assistant', content: '', tool_calls: [call] } : { role: 'assistant', content: 'Done.' }, usage: { prompt_tokens: 20, completion_tokens: 10 } }; }; const runtime = new Runtime(store, provider); if (phase === 'approved') runtime.executeCommand = () => process.exit(66); const finish = store.finishInvocation.bind(store); store.finishInvocation = (...args) => { if (phase === 'effect_unrecorded') process.exit(66); const result = finish(...args); if (phase === 'result_committed') process.exit(66); return result; }; runtime.start(session.id, 'Crash fixture task'); const poll = setInterval(() => { const approval = session.approvals.find(value => value.status === 'pending'); if (!approval) return; if (phase === 'approval_wait') process.exit(66); runtime.decide(session.id, approval.id, true); }, 10); await runtime.active.get(session.id).promise; clearInterval(poll); store.close(); throw new Error('Runtime crash point was not reached: ' + phase + ' at ' + path.basename(project.root));