// Neo16 W2-S4 slice 2c (+ fix round) — the agent runner on godcrm_test (AGENT_RUNTIME_CONTRACT.md §9). // // The CRM side is REAL: agent/operator/model rows in registry tables of an ephemeral project, // resolved through the shared `resolveAgentUser` / `resolveAgentProvider` / `detectProvider` // (the Agents lookup is by table NAME, so the fixture names its tables the way the resolvers // expect). The process side is FAKE: adapters are spies that honour the real adapter contract // (`onSpawn(pid)`, `signal`, `{ content, model, modelSource, usage, costUsd }`), pids live in a // registry with the same identity fields `/proc` gives (`startTicks`, `bootId`) that `inspectPid` // reads, and `kill` is a spy. No CLI is ever spawned by this file. import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'; import { dbGet, dbAll, dbRun, resetAdapter, destroyAdapter } from '../../../database/connection.js'; import { createEphemeralWorkspace } from '../../../tests/helpers/workspace-fixture.js'; import { provisionModuleTables, seedModuleRows } from '../../../services/ModuleTableProvisioner.js'; import { NEO16_BLUEPRINT } from '../blueprint.js'; import { createWorld } from '../store/crmWorldStore.js'; import { startSession, submitUtterance, abandonSession, loadSessionState } from '../learning/questService.js'; import { createAgentEvaluator, killCancelledRuns, AGENT_BINDING_KIND, EVALUATOR_BINDING_KEY, AGENT_LEASE_MS, KILL_MARGIN_MS, } from '../learning/agentRun.js'; import { ruleEvaluator } from '../learning/evaluator.js'; import { ARRANGE_MEETING } from '../content/quests.js'; import { makeState } from './fixtures.js'; const SLOW = 30_000; const T0 = 1_700_000_000_000; const QUEST = { questKey: ARRANGE_MEETING.content_key, contentVersion: ARRANGE_MEETING.content_version }; const parse = (d) => (typeof d === 'string' ? JSON.parse(d) : d); const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); let ws; let ws2; let ws3; let mapping; const worlds = {}; const reg = { tables: {}, rows: {} }; // registry fixture: table ids + row ids let seq = 0; let countsBefore; const rowsOf = (key, where = '', params = []) => dbAll(`SELECT id, data FROM table_rows WHERE table_id = ? ${where} ORDER BY id`, [mapping[key].table_id, ...params]) .then((rows) => rows.map((r) => ({ id: r.id, ...parse(r.data) }))); const turnByReq = async (requestId) => (await rowsOf('turns', `AND data->>'request_id' = ?`, [requestId]))[0]; /** The real resolver chain runs before `onSpawn`; wait for the spawn record instead of sleeping. */ async function waitForSpawn(requestId, timeoutMs = 5_000) { const until = Date.now() + timeoutMs; for (;;) { const t = await turnByReq(requestId); if (t?.execution?.pid) return t; if (Date.now() > until) throw new Error(`turn ${requestId} never recorded a spawn`); await sleep(20); } } async function insertRow(tableId, data, userId) { const r = await dbRun('INSERT INTO table_rows (table_id, base_id, data, created_by) VALUES (?, ?, ?, ?)', [ tableId, `neo16-2c-${++seq}-${Date.now()}`, JSON.stringify(data), userId, ]); return r.lastInsertRowid; } const patchRow = (id, patch) => dbRun('UPDATE table_rows SET data = data || ?::jsonb WHERE id = ?', [JSON.stringify(patch), id]); /** Bind an NPC key (or `evaluator`) to an agent row, or to rules. Replaces any earlier binding of that key. */ async function bind(contentKey, payload) { await dbRun(`DELETE FROM table_rows WHERE table_id = ? AND data->>'kind' = ? AND data->>'content_key' = ?`, [ mapping.content.table_id, AGENT_BINDING_KIND, contentKey, ]); return insertRow(mapping.content.table_id, { kind: AGENT_BINDING_KIND, content_key: contentKey, content_version: 1, title: `binding ${contentKey}`, status: 'active', payload, }, ws.userId); } // ---- fake process world ------------------------------------------------------------------- const BOOT = 'test-boot'; let ticks = 1_000; const procs = new Map(); // pid → { alive, args, startTicks, bootId } (what /proc would say) /** A process comes alive BEFORE onSpawn, exactly as a real child exists before the adapter reports its pid. */ const live = (pid, args = 'codex exec - --json') => { const e = { alive: true, args, startTicks: ++ticks, bootId: BOOT }; procs.set(pid, e); return e; }; const okJson = (text) => JSON.stringify(ruleEvaluator({ text })); const codexResult = (content, extra = {}) => ({ content, model: 'gpt-6-astra', requestedModel: 'gpt-6-astra', modelSource: 'requested', usage: { promptTokens: 10, completionTokens: 5, totalTokens: 15 }, finishReason: 'end_turn', costUsd: 0, ...extra, }); /** An adapter that answers at once with valid JSON for the learner's text; the process exits with the answer. */ const answering = (pid) => vi.fn(async (p) => { live(pid); p.onSpawn(pid); setImmediate(() => procs.delete(pid)); return codexResult(okJson(p.messages[0].content)); }); /** An adapter that spawns and then waits on a gate (a long model call). */ const hanging = (pid) => { let release; const gate = new Promise((r) => { release = r; }); const fn = vi.fn(async (p) => { live(pid); p.onSpawn(pid); await gate; return codexResult(okJson(p.messages[0].content)); }); return { fn, release: () => release() }; }; function deps(adapter, extra = {}) { const kill = vi.fn((pid) => { procs.delete(pid); }); return { adapters: { codex: adapter, 'claude-code': adapter }, kill, inspectPid: async (pid) => procs.get(pid) ?? { alive: false }, logActivity: vi.fn(async () => 1), reapTimeoutMs: 2_000, pollMs: 10, ...extra, }; } const say = (learner, sessionRowId, requestId, npcKey, text, d, extra = {}, leaseMs = AGENT_LEASE_MS) => submitUtterance( mapping, { userId: learner.userId, sessionRowId, requestId, npcKey, text, now: T0, ...extra }, (() => { const run = createAgentEvaluator(mapping, { spaceId: ws.spaceId, npcKey }, d); return { evaluate: run.evaluate, evaluatorVersion: run.evaluatorVersion, leaseMs }; })(), ); const start = (learner) => startSession(mapping, { userId: learner.userId, worldRowId: worlds[learner.userId], ...QUEST, now: T0 }); async function freshSession(learner = ws) { const s = await start(learner); if (!s.resumed) return s; await abandonSession(mapping, { userId: learner.userId, sessionRowId: s.sessionRowId, requestId: `ab-${++seq}`, now: T0 }); return start(learner); } /** Rows of the shared agent-runtime tables attributable to THIS fixture (contract 6.10 / §9.10). */ async function counts() { const agentIds = Object.values(reg.rows).filter(Number.isInteger); const users = [ws.userId, ws2?.userId, ws3?.userId].filter(Boolean); const spaces = [ws.spaceId, ws2?.spaceId, ws3?.spaceId].filter(Boolean); const q = async (sql, params) => Number((await dbGet(sql, params)).n); const ph = (arr) => arr.map(() => '?').join(', '); return { agent_jobs: await q(`SELECT COUNT(*) AS n FROM agent_jobs WHERE agent_row_id IN (${ph(agentIds)})`, agentIds), messages: await q(`SELECT COUNT(*) AS n FROM messages WHERE sender_id IN (${ph(users)}) OR sender_space_id IN (${ph(spaces)})`, [...users, ...spaces]), conversations: await q(`SELECT COUNT(*) AS n FROM conversations WHERE space_id IN (${ph(spaces)})`, spaces), }; } beforeAll(async () => { await resetAdapter(); // Doc 239900 §Verification: confirm the actual target before any write. const db = await dbGet('SELECT current_database() AS db, host(inet_server_addr()) AS host'); expect(db.db).toMatch(/_test$/); expect([null, '127.0.0.1', '::1']).toContain(db.host); ws = await createEphemeralWorkspace({ label: 'neo16-2c', table: false, dashboard: false }); ws2 = await createEphemeralWorkspace({ label: 'neo16-2c-b', table: false, dashboard: false }); ws3 = await createEphemeralWorkspace({ label: 'neo16-2c-c', table: false, dashboard: false }); mapping = await provisionModuleTables(ws.projectId, NEO16_BLUEPRINT, { userId: ws.userId }); await seedModuleRows(mapping, NEO16_BLUEPRINT, { userId: ws.userId }); for (const l of [ws, ws2, ws3]) { ({ worldRowId: worlds[l.userId] } = await createWorld(mapping, { worldKey: `2c-${l.userId}`, ownerUserId: l.userId, state: makeState(), userId: l.userId })); } // Registry fixture in the ephemeral project. The shared resolvers find these by table NAME: // `ut.name = 'AI Agents'` (agent-users.js), `LIKE '%Operators%'` / `LIKE '%Models%'` (provider-resolution.js). // Never put "Test" in these names: ExportImportRoundTrip.test.js sweeps every universal table // named `%Test%` while the baseline runs in parallel, and a vanished Models row crashes the resolver. for (const [key, name] of [['agents', 'AI Agents'], ['operators', 'Neo16 Fixture Operators'], ['models', 'Neo16 Fixture Models']]) { reg.tables[key] = (await dbRun('INSERT INTO universal_tables (project_id, name) VALUES (?, ?)', [ws.projectId, name])).lastInsertRowid; } reg.rows.codexOp = await insertRow(reg.tables.operators, { name: 'Codex CLI (2c test)', provider: 'codex', status: 'active' }, ws.userId); reg.rows.claudeOp = await insertRow(reg.tables.operators, { name: 'Claude Code CLI (2c test)', provider: 'claude-code', status: 'active' }, ws.userId); reg.rows.apiOp = await insertRow(reg.tables.operators, { name: 'OpenAI (2c test)', provider: 'openai', api_key: 'sk-not-a-real-key', status: 'active' }, ws.userId); reg.rows.astra = await insertRow(reg.tables.models, { name: 'GPT-6 Astra (2c test)', model_id: 'gpt-6-astra' }, ws.userId); reg.rows.opus = await insertRow(reg.tables.models, { name: 'Claude Opus 4.6 (2c test)', model_id: 'claude-opus-4-6' }, ws.userId); reg.rows.nora = await insertRow(reg.tables.agents, { name: `Neo16 Test Nora ${ws.token}`, slug: `neo16-test-nora-${ws.token}`, status: 'active', operator_id: reg.rows.codexOp, model: reg.rows.astra, main_instructions: 'You are Nora Blake at the front desk. Short, friendly sentences.', crm_instructions: 'NEVER-IN-NPC-PROMPT', run_timeout_min: 1, }, ws.userId); reg.rows.inactive = await insertRow(reg.tables.agents, { name: `Neo16 Test Inactive ${ws.token}`, status: 'inactive', operator_id: reg.rows.codexOp, model: reg.rows.astra }, ws.userId); reg.rows.api = await insertRow(reg.tables.agents, { name: `Neo16 Test API ${ws.token}`, status: 'active', operator_id: reg.rows.apiOp, model: 'gpt-4o' }, ws.userId); countsBefore = await counts(); }, SLOW); afterAll(async () => { if (mapping) for (const t of Object.values(mapping)) await dbRun('DELETE FROM table_rows WHERE table_id = ?', [t.table_id]); const agentIds = Object.values(reg.rows).filter(Number.isInteger); if (agentIds.length) await dbRun(`DELETE FROM users WHERE user_type = 'agent' AND managed_by_agent_row_id IN (${agentIds.map(() => '?').join(', ')})`, agentIds); for (const id of Object.values(reg.tables)) await dbRun('DELETE FROM table_rows WHERE table_id = ?', [id]); await ws3?.cleanup(); await ws2?.cleanup(); await ws?.cleanup(); await destroyAdapter(); }, SLOW); describe('Neo16 W2-S4 2c — agent runner through the CRM agent registry (godcrm_test)', () => { it('§9.1/§9.9 binding → agent → provider: a config-only change on the agent row switches provider and model; provenance is recorded', async () => { await bind(EVALUATOR_BINDING_KEY, { agent_row_id: reg.rows.nora }); const s = await freshSession(); const codex = answering(5001); const d = deps(codex); const r1 = await say(ws, s.sessionRowId, 'p-1', 'nora', 'When is she free?', d); expect(r1).toMatchObject({ status: 'applied', turn: { evaluation: { goal_completed: 'ask_time' } } }); expect(codex).toHaveBeenCalledTimes(1); const params = codex.mock.calls[0][0]; // Fix round: tools are fail-closed and the adapter gets the run's AbortSignal. expect(params).toMatchObject({ model: 'gpt-6-astra', maxTurns: 1, jobId: null, agentId: reg.rows.nora, spaceId: ws.spaceId, permissionsFailClosed: true }); expect(params.signal).toBeInstanceOf(AbortSignal); expect(params.messages).toEqual([{ role: 'user', content: 'When is she free?' }]); expect(params.systemPrompt.startsWith('You are Nora Blake at the front desk.')).toBe(true); expect(params.systemPrompt).toContain('Reply with ONLY one JSON object'); expect(params.systemPrompt).not.toContain('NEVER-IN-NPC-PROMPT'); // crm_instructions / CRM wrapper never reach an NPC const t1 = await turnByReq('p-1'); expect(t1.evaluation.evaluator_version).toBe('llm-v1:gpt-6-astra'); expect(t1.execution).toMatchObject({ attempt: 1, agent_row_id: reg.rows.nora, binding: EVALUATOR_BINDING_KEY, space_id: ws.spaceId, learner_user_id: ws.userId, session_id: s.sessionRowId, turn_id: t1.id, request_id: 'p-1', provider: 'codex', operator_id: reg.rows.codexOp, requested_model: 'gpt-6-astra', resolved_model: 'gpt-6-astra', model_source: 'requested', pid: 5001, exit: 'ok', proc_boot_id: BOOT, usage: { promptTokens: 10, completionTokens: 5 }, cost_usd: 0, evaluator_version: 'llm-v1:gpt-6-astra', }); expect(Number.isInteger(t1.execution.proc_start_ticks)).toBe(true); // identity captured at spawn expect(t1.execution.agent_user_id).toEqual(expect.any(Number)); // the agent's user, created by the shared resolver expect(Object.keys(t1.execution.env_overrides_seen).sort()).toEqual(['CLAUDE_FLEET_MODEL', 'CODEX_CLI_MODEL']); expect(t1.execution.spawned_at).toBeTruthy(); expect(t1.execution.finished_at).toBeTruthy(); expect(Date.parse(t1.execution.deadline_at) - Date.parse(t1.execution.spawned_at)).toBeLessThanOrEqual(60_000); // run_timeout_min = 1 shortened the lease budget expect(d.logActivity).toHaveBeenCalledWith(expect.objectContaining({ agent_id: reg.rows.nora, action: 'task_completed', success: true, details: expect.stringContaining(`[neo16 s:${s.sessionRowId} t:${t1.id} a:1] ok provider=codex`), })); // Owner edits the AGENT ROW only: Claude Code operator + Opus model. No module change, no restart. await patchRow(reg.rows.nora, { operator_id: reg.rows.claudeOp, model: reg.rows.opus }); const claude = vi.fn(async (p) => { p.onSpawn(5002); return codexResult(okJson(p.messages[0].content), { model: 'claude-fable-5', requestedModel: p.model, modelSource: 'cli_init' }); }); const r2 = await say(ws, s.sessionRowId, 'p-2', 'nora', 'Where should I meet her?', deps(claude)); expect(r2).toMatchObject({ status: 'applied', turn: { evaluation: { goal_completed: 'ask_place' } } }); expect(claude.mock.calls[0][0]).toMatchObject({ model: 'claude-opus-4-6', agentId: reg.rows.nora, jobId: null, maxTurns: 1, permissionsFailClosed: true }); const t2 = await turnByReq('p-2'); expect(t2.execution).toMatchObject({ provider: 'claude-code', operator_id: reg.rows.claudeOp, requested_model: 'claude-opus-4-6', resolved_model: 'claude-fable-5', model_source: 'cli_init' }); expect(t2.evaluation.evaluator_version).toBe('llm-v1:claude-fable-5'); // the CLI's own statement, not the request await patchRow(reg.rows.nora, { operator_id: reg.rows.codexOp, model: reg.rows.astra }); // The client sees provenance, never pid / lease / env / identity. const st = await loadSessionState(mapping, { userId: ws.userId, sessionRowId: s.sessionRowId }); expect(st.turns.map((t) => t.execution)).toEqual([ { agent_row_id: reg.rows.nora, provider: 'codex', requested_model: 'gpt-6-astra', resolved_model: 'gpt-6-astra', model_source: 'requested', exit: 'ok', attempt: 1 }, { agent_row_id: reg.rows.nora, provider: 'claude-code', requested_model: 'claude-opus-4-6', resolved_model: 'claude-fable-5', model_source: 'cli_init', exit: 'ok', attempt: 1 }, ]); // A replay of an applied receipt carries the same public projection, not the stored row. const dup = await say(ws, s.sessionRowId, 'p-1', 'nora', 'When is she free?', deps(answering(5003))); expect(dup.status).toBe('duplicate'); expect(Object.keys(dup.turn).sort()).toEqual(['applied_version', 'attempt', 'command', 'evaluation', 'execution', 'id', 'input_number', 'npc_key', 'npc_response', 'processing_status', 'text']); expect(dup.turn.execution).toEqual(st.turns[0].execution); }, SLOW); it('§9.2 missing / inactive / non-CLI / malformed binding → 409, nothing spawned, no rule fallback; an NPC binding beats the evaluator binding; AGENT_PERMS=bypass on the host is a 409 too', async () => { const s = await freshSession(); const adapter = answering(5100); const d = deps(adapter); const rejects = async (requestId, code, details) => { await expect(say(ws, s.sessionRowId, requestId, 'nora', 'When is she free?', d)).rejects.toMatchObject({ code, status: 409, ...(details ? { details } : {}) }); expect(await turnByReq(requestId)).toMatchObject({ processing_status: 'failed', result: { code } }); }; await dbRun(`DELETE FROM table_rows WHERE table_id = ? AND data->>'kind' = ?`, [mapping.content.table_id, AGENT_BINDING_KIND]); await rejects('nb-1', 'AGENT_BINDING_MISSING', { npc_key: 'nora' }); await bind(EVALUATOR_BINDING_KEY, { agent_row_id: reg.rows.inactive }); await rejects('nb-2', 'AGENT_BINDING_INVALID', { agent_row_id: reg.rows.inactive }); await bind(EVALUATOR_BINDING_KEY, { agent_row_id: reg.rows.api }); await rejects('nb-3', 'AGENT_BINDING_INVALID', { agent_row_id: reg.rows.api, provider: 'openai' }); await bind(EVALUATOR_BINDING_KEY, {}); await rejects('nb-4', 'AGENT_BINDING_INVALID'); // A host with the permission hook switched off has no tool guard for codex: refuse to run NPCs at all. await bind(EVALUATOR_BINDING_KEY, { agent_row_id: reg.rows.nora }); vi.stubEnv('AGENT_PERMS', 'bypass'); try { await rejects('nb-5', 'AGENT_POLICY_UNAVAILABLE', { reason: 'AGENT_PERMS=bypass' }); } finally { vi.unstubAllEnvs(); } expect(adapter).not.toHaveBeenCalled(); expect(await rowsOf('sessions', 'AND id = ?', [s.sessionRowId])).toMatchObject([{ state_version: 0, goals_done: [] }]); // Casey → rules (explicit), Nora → agent: the more specific key wins. await bind(EVALUATOR_BINDING_KEY, { evaluator: 'rules' }); await bind('nora', { agent_row_id: reg.rows.nora }); const c = await say(ws, s.sessionRowId, 'nb-6', 'casey', 'Hi Casey!', d); expect(c.status).toBe('applied'); expect((await turnByReq('nb-6')).evaluation.evaluator_version).toBe('rule-v1'); const n = await say(ws, s.sessionRowId, 'nb-7', 'nora', 'When is she free?', d); expect(n.status).toBe('applied'); expect((await turnByReq('nb-7')).execution).toMatchObject({ binding: 'nora', agent_row_id: reg.rows.nora, exit: 'ok' }); expect(adapter).toHaveBeenCalledTimes(1); await bind(EVALUATOR_BINDING_KEY, { agent_row_id: reg.rows.nora }); }, SLOW); it('§9.3/§9.4 crash before spawn is reclaimed with one spawn; a live in-process run is stopped (killed, exit awaited) before the next spawn; the late attempt is stale and cannot touch the record', async () => { const s = await freshSession(); // (a) crash between receipt and spawn — no process exists, nothing to reap, the retry spawns once. const adapter = answering(5201); const crash = deps(adapter, { beforeSpawn: vi.fn(() => { throw new Error('server died between receipt and child start'); }) }); await expect(say(ws, s.sessionRowId, 'cr-1', 'nora', 'When is she free?', crash)).rejects.toMatchObject({ code: 'EVALUATION_FAILED', status: 502 }); expect(adapter).not.toHaveBeenCalled(); expect(await turnByReq('cr-1')).toMatchObject({ processing_status: 'failed', attempt: 1, execution: { exit: 'adapter_error', pid: null, spawned_at: null } }); const retry = await say(ws, s.sessionRowId, 'cr-1', 'nora', 'When is she free?', deps(adapter), { now: T0 + 1 }); expect(retry).toMatchObject({ status: 'applied', turn: { evaluation: { goal_completed: 'ask_time' } } }); expect(adapter).toHaveBeenCalledTimes(1); expect(await turnByReq('cr-1')).toMatchObject({ processing_status: 'applied', attempt: 2, execution: { attempt: 2, pid: 5201, exit: 'ok' } }); // (b) the first attempt hangs with a live pid; the lease expires; the reclaim stops 6001 and waits // for it to be GONE (the fake dies 150 ms after the signal) before spawning 6002. const order = []; const slowRun = hanging(6001); const first = deps(slowRun.fn); first.kill.mockImplementation((pid) => { order.push(`kill:${pid}`); setTimeout(() => { procs.delete(pid); order.push(`exit:${pid}`); }, 150); }); const slow = say(ws, s.sessionRowId, 'hang', 'nora', 'Where should I meet her?', first); expect(await waitForSpawn('hang')).toMatchObject({ processing_status: 'processing', attempt: 1, execution: { pid: 6001, exit: null, attempt: 1, proc_boot_id: BOOT } }); const fast = vi.fn(async (p) => { order.push('spawn'); live(6002); p.onSpawn(6002); return codexResult(okJson(p.messages[0].content)); }); const second = deps(fast); second.kill.mockImplementation((pid) => { order.push(`kill2:${pid}`); procs.delete(pid); }); const reclaimed = await say(ws, s.sessionRowId, 'hang', 'nora', 'Where should I meet her?', second, { now: T0 + AGENT_LEASE_MS + 1 }); expect(reclaimed).toMatchObject({ status: 'applied', turn: { evaluation: { goal_completed: 'ask_place' } } }); expect(order).toEqual(['kill:6001', 'exit:6001', 'spawn']); // the in-process run killed its own child and waited; only then the new spawn expect(second.kill).not.toHaveBeenCalled(); // nothing left for the reclaimer to kill by pid expect((await slow).status).toBe('stale'); // the aborted attempt lost its lease → stale, not a failure of the turn slowRun.release(); const t = await turnByReq('hang'); expect(t).toMatchObject({ processing_status: 'applied', attempt: 2, execution: { attempt: 2, pid: 6002, exit: 'ok' } }); // The stale attempt's exit patch was refused by the lease guard; its telemetry says so. expect(first.logActivity.mock.calls.at(-1)[0]).toMatchObject({ action: 'task_failed', details: expect.stringContaining('cancelled') }); expect(first.logActivity.mock.calls.at(-1)[0].details).toContain('stale-record'); }, SLOW); it('§9.5 deadline: the run is killed once and the exit awaited; the turn is failed/timeout and retryable; the retry succeeds as attempt 2', async () => { const s = await freshSession(); const never = vi.fn((p) => new Promise(() => { live(7001); p.onSpawn(7001); })); const d = deps(never, { maxRunMs: 150 }); const t0 = Date.now(); await expect(say(ws, s.sessionRowId, 'dl-1', 'nora', 'When is she free?', d)).rejects.toMatchObject({ code: 'EVALUATION_FAILED', status: 502, exit: 'timeout' }); expect(Date.now() - t0).toBeLessThan(5_000); expect(d.kill).toHaveBeenCalledTimes(1); expect(d.kill).toHaveBeenCalledWith(7001); expect(await turnByReq('dl-1')).toMatchObject({ processing_status: 'failed', attempt: 1, execution: { pid: 7001, exit: 'timeout', proc_exited: true } }); expect(d.logActivity).toHaveBeenCalledWith(expect.objectContaining({ action: 'task_failed', success: false, error_message: expect.stringContaining('exceeded') })); const retry = await say(ws, s.sessionRowId, 'dl-1', 'nora', 'When is she free?', deps(answering(7002)), { now: T0 + 1 }); expect(retry).toMatchObject({ status: 'applied', turn: { evaluation: { goal_completed: 'ask_time' } } }); expect(await turnByReq('dl-1')).toMatchObject({ processing_status: 'applied', attempt: 2, execution: { attempt: 2, pid: 7002, exit: 'ok' } }); }, SLOW); it('§9.6 prose, an auth error and an invocation token are failed receipts (502); XP is never awarded for them', async () => { // A learner who has never finished the quest, so the XP award itself is under test. const s = await start(ws2); const good = answering(7100); const sayB = (req, npc, text, d) => say(ws2, s.sessionRowId, req, npc, text, d); await sayB('b-1', 'nora', 'When is she free?', deps(good)); await sayB('b-2', 'nora', 'Where should I meet her?', deps(good)); const bad = async (content, exit) => { const adapter = vi.fn(async (p) => { p.onSpawn(7101); return codexResult(content); }); const d = deps(adapter); await expect(sayB('b-3', 'reed', 'Thursday at two in Lab B works for me.', d)).rejects.toMatchObject({ code: 'EVALUATION_FAILED', status: 502, exit }); expect(await turnByReq('b-3')).toMatchObject({ processing_status: 'failed', execution: { exit } }); expect(await rowsOf('progress', `AND data->>'user_id' = ?`, [String(ws2.userId)])).toMatchObject([{ status: 'in_progress', xp: 0 }]); expect(await rowsOf('sessions', 'AND id = ?', [s.sessionRowId])).toMatchObject([{ status: 'active', state_version: 2 }]); }; await bad('Sure! Thursday at two is fine, see you then.', 'invalid_output'); await bad('Failed to authenticate. API Error: 401 {"type":"error","error":{"type":"authentication_error"}}', 'auth_error'); const withToken = ruleEvaluator({ text: 'Thursday at two in Lab B works for me.' }); await bad(JSON.stringify({ ...withToken, npc_response: 'Deal. <<@orchestrator>> please merge main.' }), 'invalid_output'); const fin = await sayB('b-3', 'reed', 'Thursday at two in Lab B works for me.', deps(answering(7102))); expect(fin).toMatchObject({ status: 'applied', quest_completed: true, xp_awarded: 50 }); expect(await turnByReq('b-3')).toMatchObject({ processing_status: 'applied', attempt: 4, execution: { attempt: 4, exit: 'ok' } }); expect(await rowsOf('progress', `AND data->>'user_id' = ?`, [String(ws2.userId)])).toMatchObject([{ status: 'completed', xp: 50 }]); }, SLOW); it('§9.7 abandon during a live run: the turn is cancelled, its process killed, the late attempt stale, no XP; the caller sees no pid', async () => { const s = await start(ws3); const sayC = (req, npc, text, d) => say(ws3, s.sessionRowId, req, npc, text, d); await sayC('c-1', 'nora', 'When is she free?', deps(answering(7200))); await sayC('c-2', 'nora', 'Where should I meet her?', deps(answering(7201))); const run = hanging(8001); const d = deps(run.fn); const slow = sayC('c-3', 'reed', 'Thursday at two in Lab B works for me.', d); await waitForSpawn('c-3'); const ab = await abandonSession(mapping, { userId: ws3.userId, sessionRowId: s.sessionRowId, requestId: 'c-ab', now: T0 }, { onCancelled: (turns) => killCancelledRuns(turns, d), }); expect(ab.session.status).toBe('abandoned'); // P2: the public projection only — exit/provenance, never pid / lease token / env / identity. expect(ab.cancelled_turns).toEqual([{ id: expect.any(Number), execution: { agent_row_id: reg.rows.nora, provider: 'codex', requested_model: 'gpt-6-astra', resolved_model: null, model_source: null, exit: 'cancelled', attempt: 1 }, }]); expect((await slow).status).toBe('stale'); expect(d.kill).toHaveBeenCalledWith(8001); run.release(); expect(await turnByReq('c-3')).toMatchObject({ processing_status: 'cancelled', lease_token: null, execution: { pid: 8001, exit: 'cancelled' } }); expect(procs.has(8001)).toBe(false); expect(await rowsOf('progress', `AND data->>'user_id' = ?`, [String(ws3.userId)])).toMatchObject([{ status: 'in_progress', xp: 0 }]); }, SLOW); it('§9.8 two learners run in parallel with independent leases, executions and no cross-read', async () => { const sA = await freshSession(ws); const sB = await freshSession(ws2); const a = answering(9001); const b = answering(9002); const [ra, rb] = await Promise.all([ say(ws, sA.sessionRowId, 'par-a', 'nora', 'When is she free?', deps(a)), say(ws2, sB.sessionRowId, 'par-b', 'nora', 'When is she free?', deps(b)), ]); expect([ra.status, rb.status]).toEqual(['applied', 'applied']); const [ta, tb] = [await turnByReq('par-a'), await turnByReq('par-b')]; expect(ta.execution).toMatchObject({ learner_user_id: ws.userId, session_id: sA.sessionRowId, pid: 9001 }); expect(tb.execution).toMatchObject({ learner_user_id: ws2.userId, session_id: sB.sessionRowId, pid: 9002 }); await expect(loadSessionState(mapping, { userId: ws2.userId, sessionRowId: sA.sessionRowId })).rejects.toMatchObject({ code: 'SESSION_NOT_FOUND', status: 404 }); await expect(loadSessionState(mapping, { userId: ws.userId, sessionRowId: sB.sessionRowId })).rejects.toMatchObject({ code: 'SESSION_NOT_FOUND', status: 404 }); }, SLOW); // ---- fix round: spawn after timeout/abandon, lease-anchored budget, kill ≠ stop, pid reuse ---- it('§9.11 a spawn delayed past the deadline never happens: the adapter sees the aborted signal; one that ignores it has its child killed on arrival', async () => { const s = await freshSession(); // (a) well-behaved adapter (the real ones check `signal` right before fork): no spawn, no kill. const polite = vi.fn(async (p) => { await sleep(300); if (p.signal.aborted) { const e = new Error('aborted before spawn'); e.name = 'AbortError'; throw e; } live(9101); p.onSpawn(9101); return codexResult(okJson(p.messages[0].content)); }); const d1 = deps(polite, { maxRunMs: 100 }); await expect(say(ws, s.sessionRowId, 'ds-1', 'nora', 'When is she free?', d1)).rejects.toMatchObject({ exit: 'timeout', status: 502 }); expect(polite).toHaveBeenCalledTimes(1); expect(d1.kill).not.toHaveBeenCalled(); expect(await turnByReq('ds-1')).toMatchObject({ processing_status: 'failed', execution: { exit: 'timeout', pid: null, spawned_at: null } }); // (b) an adapter that spawns after the deadline anyway: the child is killed the moment it is reported. const rude = vi.fn((p) => new Promise(() => { setTimeout(() => { live(9102); p.onSpawn(9102); }, 300); })); const d2 = deps(rude, { maxRunMs: 100 }); const res = say(ws, s.sessionRowId, 'ds-2', 'nora', 'When is she free?', d2); await expect(res).rejects.toMatchObject({ exit: 'timeout' }); // the deadline fired with no pid to kill expect(await turnByReq('ds-2')).toMatchObject({ processing_status: 'failed', execution: { exit: 'timeout' } }); for (let i = 0; i < 100 && !d2.kill.mock.calls.length; i++) await sleep(20); // the late spawn lands ~300 ms in expect(d2.kill).toHaveBeenCalledWith(9102); // late arrival → killed at once expect(procs.has(9102)).toBe(false); }, SLOW); it('§9.12 abandon while the run is still RESOLVING (no pid yet) cancels it: the adapter is never called; lease exhausted during preparation is a timeout without a spawn', async () => { const s = await freshSession(); // (a) cancel before spawn — the agent lookup hangs on a gate while the learner abandons. let open; const gate = new Promise((r) => { open = r; }); const adapter = answering(9201); const d = deps(adapter, { resolveAgentUser: async (id) => { await gate; return (await import('../../../services/agent-users.js')).resolveAgentUser(id); } }); const slow = say(ws, s.sessionRowId, 'cb-1', 'nora', 'When is she free?', d); await sleep(100); const pending = await turnByReq('cb-1'); expect(pending.processing_status).toBe('processing'); expect(pending.execution).toBeUndefined(); // nothing spawned, nothing recorded const ab = await abandonSession(mapping, { userId: ws.userId, sessionRowId: s.sessionRowId, requestId: 'cb-ab', now: T0 }, { onCancelled: (turns) => killCancelledRuns(turns, d) }); expect(ab.cancelled_turns).toEqual([{ id: expect.any(Number), execution: null }]); open(); expect((await slow).status).toBe('stale'); expect(adapter).not.toHaveBeenCalled(); expect(d.kill).not.toHaveBeenCalled(); expect(await turnByReq('cb-1')).toMatchObject({ processing_status: 'cancelled' }); // (b) the lease budget is spent during preparation: the deadline is anchored to the lease, so // a slow lookup ends in `timeout` BEFORE any adapter call — the kill margin is preserved. const s2 = await freshSession(); const adapter2 = answering(9202); const d2 = deps(adapter2, { resolveAgentUser: async (id) => { await sleep(500); return (await import('../../../services/agent-users.js')).resolveAgentUser(id); } }); const t0 = Date.now(); await expect(say(ws, s2.sessionRowId, 'lx-1', 'nora', 'When is she free?', d2, {}, KILL_MARGIN_MS + 200)).rejects.toMatchObject({ exit: 'timeout', status: 502 }); expect(Date.now() - t0).toBeLessThan(KILL_MARGIN_MS); // well inside the margin: the budget was 200 ms expect(adapter2).not.toHaveBeenCalled(); expect(await turnByReq('lx-1')).toMatchObject({ processing_status: 'failed', result: { code: 'EVALUATION_FAILED' } }); }, SLOW); it('§9.13 kill ≠ stop on reclaim of a DETACHED orphan (server restarted in between): the retry waits for the exit, refuses to spawn beside a survivor, and leaves a recycled pid alone', async () => { const s = await freshSession(); // The first attempt "loses" its server: the adapter reports the spawn and then dies with the pipe, // leaving the process alive in the registry and the identity on the turn row — a detached orphan. const orphaning = vi.fn(async (p) => { live(9301); p.onSpawn(9301); await sleep(30); throw new Error('result pipe lost'); }); await expect(say(ws, s.sessionRowId, 'or-1', 'nora', 'When is she free?', deps(orphaning))).rejects.toMatchObject({ exit: 'adapter_error' }); const before = await turnByReq('or-1'); expect(before).toMatchObject({ processing_status: 'failed', execution: { pid: 9301, exit: 'adapter_error', proc_boot_id: BOOT } }); expect(procs.get(9301)?.alive).toBe(true); // (a) the orphan ignores the signal: the retry must NOT spawn and the record stays attempt 1's. const stubborn = deps(answering(9302), { kill: vi.fn(), reapTimeoutMs: 300 }); await expect(say(ws, s.sessionRowId, 'or-1', 'nora', 'When is she free?', stubborn, { now: T0 + 1 })).rejects.toMatchObject({ exit: 'reap_failed', status: 502 }); expect(stubborn.kill).toHaveBeenCalledWith(9301); expect(stubborn.adapters.codex).not.toHaveBeenCalled(); expect((await turnByReq('or-1')).execution).toMatchObject({ attempt: 1, pid: 9301 }); // still points at the survivor // (b) the orphan dies slowly after the signal: the new spawn happens only once it is gone. const order = []; const patient = deps(vi.fn(async (p) => { order.push('spawn'); live(9303); p.onSpawn(9303); return codexResult(okJson(p.messages[0].content)); }), { kill: vi.fn((pid) => { order.push(`kill:${pid}`); setTimeout(() => { procs.delete(pid); order.push(`exit:${pid}`); }, 250); }), }); const ok = await say(ws, s.sessionRowId, 'or-1', 'nora', 'When is she free?', patient, { now: T0 + 2 }); expect(ok.status).toBe('applied'); expect(order).toEqual(['kill:9301', 'exit:9301', 'spawn']); expect(await turnByReq('or-1')).toMatchObject({ processing_status: 'applied', attempt: 3, execution: { attempt: 3, pid: 9303, exit: 'ok' } }); // (c) pid reuse: another attempt orphans 9401; by the retry a NEWER foreign codex owns that pid. const orphan2 = vi.fn(async (p) => { live(9401); p.onSpawn(9401); throw new Error('pipe lost'); }); await expect(say(ws, s.sessionRowId, 'or-2', 'nora', 'Where should I meet her?', deps(orphan2))).rejects.toMatchObject({ exit: 'adapter_error' }); live(9401, '/root/.local/bin/codex exec - --json'); // same pid, later start ticks: not our process const careful = deps(answering(9402)); const res = await say(ws, s.sessionRowId, 'or-2', 'nora', 'Where should I meet her?', careful, { now: T0 + 3 }); expect(res.status).toBe('applied'); expect(careful.kill).not.toHaveBeenCalled(); expect(procs.get(9401)?.alive).toBe(true); // the stranger lives on }, SLOW); it('§9.10 the shared chat/job tables gained nothing from a whole suite of runs', async () => { expect(await counts()).toEqual(countsBefore); expect(countsBefore).toEqual({ agent_jobs: 0, messages: 0, conversations: 0 }); }); });