Skip to content

Durable coding-agent harnesses (Claude Code, Codex, OpenCode, Pi, fx) on a Workflow SDK World

41.9 KB
Raw Blame
import { mkdtemp } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { createWorld, type LocalWorld } from '@workflow/world-local';
import { currentLane, type DriverOptions, deliver, durableSessions, type Harness, type HandoffOutput, isMark, type Lane, liveStreamName, QUEUE_PREFIX, rawSessions, readJsonLines, Yield } from 'harness-world';
import { createFxEngine } from 'libfx';
import { hostProcess } from 'harness-world/testing';
import { afterEach, describe, expect, it } from 'vitest';
import { type CreateFxEngine, type FxHarnessSettings, type FxTool, fxHarness, isDelta, steersCarriedBy } from './index.ts';

/** What a test changes about the harness: its settings, or the engine factory. */
type Extra = Partial<FxHarnessSettings> & { createFxEngine?: CreateFxEngine };

async function until<T>(fn: () => Promise<T | undefined>, timeoutMs = 20_000): Promise<T> {
  const start = Date.now();
  for (;;) {
    const value = await fn();
    if (value !== undefined) return value;
    if (Date.now() - start > timeoutMs) throw new Error('timed out');
    await new Promise((r) => setTimeout(r, 50));
  }
}

/**
 * Play a delivery the way the queue would. A delivery always acks now
 * (anything left goes out as a continuation message), so this delivers once;
 * the loop is kept for a delivery that answers `{ timeoutSeconds }`.
 */
async function deliverAll(options: DriverOptions, runId: string): Promise<void> {
  for (let i = 0; i < 20; i++) {
    const result = await deliver(options, runId);
    if (!result) return;
    await new Promise((r) => setTimeout(r, result.timeoutSeconds * 1000));
  }
  throw new Error('delivery never settled');
}

type Part = Record<string, unknown>;
const sse = (parts: Part[]) =>
  new Response([...parts.map((p) => `data: ${JSON.stringify(p)}`), 'data: [DONE]', ''].join('\n\n'), {
    status: 200,
    headers: { 'content-type': 'text/event-stream' },
  });
const toolCall = (id: string, name: string, input: unknown): Part[] => [
  { type: 'tool-call', toolCallId: id, toolName: name, input },
  { type: 'finish', finishReason: { unified: 'tool-calls', raw: 'tool-calls' }, usage: { inputTokens: { total: 1 }, outputTokens: { total: 1 } } },
];
const text = (t: string): Part[] => [
  { type: 'text-delta', delta: t },
  { type: 'finish', finishReason: { unified: 'stop', raw: 'stop' }, usage: { inputTokens: { total: 1 }, outputTokens: { total: 1 } } },
];

/** A scripted AI Gateway: each model request gets the next response. */
function fakeGateway(script: ((body: any) => Part[])[]) {
  const requests: any[] = [];
  const fetchImpl: typeof fetch = async (_url, init) => {
    if ((init?.method ?? 'GET') === 'GET') return Response.json({ object: 'list', data: [] });
    const body = JSON.parse(String(init?.body));
    requests.push(body);
    const next = script.shift();
    if (!next) throw new Error('unexpected model request');
    return sse(next(body));
  };
  return { fetch: fetchImpl, requests };
}

function shout(onRun: () => Promise<void> = async () => {}): FxTool {
  return {
    name: 'shout',
    description: 'Uppercase some text.',
    inputSchema: { type: 'object', properties: { text: { type: 'string' } }, required: ['text'] },
    replay: 'never',
    async execute(input: { text: string }, { progress }) {
      progress('working');
      await onRun();
      return input.text.toUpperCase();
    },
  };
}

const worlds: LocalWorld[] = [];
afterEach(async () => {
  for (const w of worlds.splice(0)) await w.close?.();
});

function harness(gateway: ReturnType<typeof fakeGateway>, tools: FxTool[], extra: Extra = {}) {
  return fxHarness({
    createFxEngine: createFxEngine as never,
    apiKey: 'test',
    model: 'test/model',
    tools,
    fetch: gateway.fetch,
    ...extra,
  });
}

describe('fx timing marks', () => {
  it('marks the first model output of each request, durable and with durability off', async () => {
    const script = () => [() => toolCall('call_1', 'shout', { text: 'hi' }), () => text('I shouted HI.')];
    const world = createWorld({ dataDir: await mkdtemp(join(tmpdir(), 'hw-fx-')), recoverActiveRuns: false });
    worlds.push(world);
    const sessions = durableSessions({ world, harness: harness(fakeGateway(script()), [shout()]) });
    world.registerHandler(QUEUE_PREFIX, sessions.handler);
    const { sessionId } = await sessions.start({ prompt: 'shout hi' });
    const lines = await until(async () => {
      const l = await readJsonLines(world, sessionId, liveStreamName(sessionId));
      return l.some((e) => isMark(e) && e.name === 'idle') ? l : undefined;
    });
    const firsts = lines.filter((e) => isMark(e) && e.name === 'model.first_token').map((e) => (e as { key?: string }).key);
    expect(firsts).toEqual(['model:0:0', 'model:0:1']);

    // Durability off: the same harness, no World, the same marks and tape shape.
    const raw = rawSessions({ harness: harness(fakeGateway(script()), [shout()]) });
    const r = await raw.start();
    const seen: unknown[] = [];
    raw.subscribe(r.sessionId, (e) => seen.push(e));
    await raw.send(r.sessionId, 'shout hi');
    await raw.idle(r.sessionId);
    const state = await raw.inspect(r.sessionId);
    expect(state.effectOrder.map((e) => e.name)).toEqual(['turn', 'model', 'tool.shout', 'model', 'checkpoint']);
    expect((state.effects.get('model:0:1')!.output as { text: string }).text).toBe('I shouted HI.');
    expect(seen.filter((e) => isMark(e) && e.name === 'model.first_token').map((e) => (e as { key?: string }).key)).toEqual(['model:0:0', 'model:0:1']);
    // Nothing durable: no acks.
    expect(seen.some((e) => isMark(e) && e.name === 'ack')).toBe(false);
  });
});

describe('fx on world-local', () => {
  it('records the kernel tape and restores the session from its checkpoint', async () => {
    const gateway = fakeGateway([
      () => toolCall('call_1', 'shout', { text: 'hi' }),
      () => text('I shouted HI.'),
      (body) => text(`history has ${body.prompt.length} messages`),
    ]);
    const world = createWorld({ dataDir: await mkdtemp(join(tmpdir(), 'hw-fx-')), recoverActiveRuns: false });
    worlds.push(world);
    const sessions = durableSessions({ world, harness: harness(gateway, [shout()]) });
    world.registerHandler(QUEUE_PREFIX, sessions.handler);

    const { sessionId } = await sessions.start({ prompt: 'shout hi' });
    const first = await until(async () => {
      const s = await sessions.inspect(sessionId);
      return s.effects.get('checkpoint:0')?.status === 'completed' ? s : undefined;
    });
    expect(first.effectOrder.map((e) => e.name)).toEqual(['turn', 'model', 'tool.shout', 'model', 'checkpoint']);
    const answer = first.effectOrder[3]!.output as { text: string };
    expect(answer.text).toBe('I shouted HI.');

    // A second delivery builds a fresh kernel from the checkpoint alone.
    await sessions.send(sessionId, 'how long is our history?');
    const second = await until(async () => {
      const s = await sessions.inspect(sessionId);
      return s.effects.get('checkpoint:1')?.status === 'completed' ? s : undefined;
    });
    const last = second.effects.get('model:1:0')!.output as { text: string };
    expect(last.text).toMatch(/history has [4-9] messages/);
    expect(gateway.requests).toHaveLength(3);
  });

  it('re-runs an interrupted turn against the tape after a crash', async () => {
    const dataDir = await mkdtemp(join(tmpdir(), 'hw-fx-'));
    const gatewayA = fakeGateway([() => toolCall('call_1', 'shout', { text: 'hi' })]);
    const worldA = createWorld({ dataDir, recoverActiveRuns: false });
    worlds.push(worldA);
    const a = durableSessions({ world: worldA, harness: harness(gatewayA, [shout(() => new Promise(() => {}))]) });
    worldA.registerHandler(QUEUE_PREFIX, a.handler);
    const { sessionId } = await a.start({ prompt: 'shout hi' });
    await until(async () => {
      const s = await a.inspect(sessionId);
      return s.effectOrder.find((e) => e.name === 'tool.shout')?.status === 'running' ? true : undefined;
    });
    await worldA.close?.();

    let toolResult = '';
    const gatewayB = fakeGateway([
      (body) => {
        toolResult = JSON.stringify(body.prompt.at(-1));
        return text('The shout may not have happened.');
      },
    ]);
    await new Promise((r) => setTimeout(r, 400));
    const worldB = createWorld({ dataDir, recoverActiveRuns: true });
    worlds.push(worldB);
    const b = durableSessions({ world: worldB, harness: harness(gatewayB, [shout()]) });
    worldB.registerHandler(QUEUE_PREFIX, b.handler);
    await worldB.start?.();

    const state = await until(async () => {
      const s = await b.inspect(sessionId);
      return s.effects.get('checkpoint:0')?.status === 'completed' ? s : undefined;
    });
    expect(state.effectOrder.map((e) => [e.name, e.status])).toEqual([
      ['turn', 'completed'],
      ['model', 'completed'],
      ['tool.shout', 'failed'],
      // The new process fenced the old one out before resolving its intent.
      ['recover', 'completed'],
      ['model', 'completed'],
      ['checkpoint', 'completed'],
    ]);
    // The first model response came from the tape: the new process made one request.
    expect(gatewayB.requests).toHaveLength(1);
    expect(toolResult).toContain('interrupted');
  });
});

describe('fx across lanes', () => {
  /** A tool that must run in the function lane, recording where it ran. */
  const backend = (ran: Lane[], lane: Lane): FxTool => ({
    name: 'lookup',
    description: 'Look something up on the backend.',
    inputSchema: { type: 'object', properties: { q: { type: 'string' } }, required: ['q'] },
    replay: 'safe',
    requires: 'function',
    async execute(input: { q: string }) {
      ran.push(lane);
      return `found ${input.q} in the ${lane} lane`;
    },
  });

  async function setup(script: ((body: any) => Part[])[], extra: Extra = {}, fnExtra = extra) {
    const world = createWorld({ dataDir: await mkdtemp(join(tmpdir(), 'hw-fx-lanes-')), recoverActiveRuns: false });
    worlds.push(world);
    // Every lane's delivery is played by hand; swallow the default wakes.
    world.registerHandler(QUEUE_PREFIX, async () => Response.json({ ok: true }));
    let lane: Lane = 'browser';
    const calls: Lane[] = [];
    const gateway = fakeGateway(script.map((f) => (body: any) => {
      calls.push(lane);
      return f(body);
    }));
    const ran: Lane[] = [];
    const queue: Lane[] = [];
    const logs: string[] = [];
    const driver = (serves: Lane, opts: Extra): DriverOptions => ({
      world,
      harness: harness(gateway, [shout(), backend(ran, serves)], opts),
      lanes: { serves: [serves], settle: 'browser', async wake(_runId, to) { queue.push(to); } },
      log: (m) => logs.push(m),
    });
    const drivers: Record<Lane, DriverOptions> = {
      browser: driver('browser', extra),
      function: driver('function', fnExtra),
      sandbox: driver('sandbox', fnExtra),
    };
    const sessions = durableSessions({ world, harness: drivers.browser.harness });
    const drive = async (runId: string) => {
      queue.push('browser');
      while (queue.length) {
        lane = queue.shift()!;
        await deliver(drivers[lane], runId);
      }
    };
    return { world, sessions, drive, calls, ran, gateway, logs, lane: () => lane };
  }

  it('escalates a tool call mid-turn and finishes the turn from the tape in the next lane', async () => {
    const t = await setup([
      () => [...toolCall('c1', 'shout', { text: 'hi' }).slice(0, 1), ...toolCall('c2', 'lookup', { q: 'acme' })],
      () => text('HI, and acme is on the backend.'),
      (body) => text(`second turn sees ${body.prompt.length} messages`),
    ]);
    const { sessionId } = await t.sessions.start({ prompt: 'shout hi, then look up acme' });
    await t.drive(sessionId);
    let state = await t.sessions.inspect(sessionId);
    const steps = state.effectOrder.map((e) => e.name);
    expect(steps).toEqual(['turn', 'model', 'tool.shout', 'handoff', 'tool.lookup', 'model', 'checkpoint', 'handoff']);
    const [up, down] = state.effectOrder.filter((e) => e.name === 'handoff').map((e) => e.output as HandoffOutput);
    expect(up).toMatchObject({ from: 'browser', to: 'function', forKey: state.effectOrder[4]!.key });
    expect(down).toMatchObject({ from: 'function', to: 'browser', reason: 'idle' });
    // The browser decided on the call; the function lane replayed that
    // response from the tape and made one new model request.
    expect(t.calls).toEqual(['browser', 'function']);
    expect(t.ran).toEqual(['function']);
    expect(state.effectOrder.every((e) => e.status === 'completed')).toBe(true);
    expect((state.effectOrder[4]!.output as string)).toContain('function lane');
    // Both lanes sent the same tape request for model:0:0.
    expect((state.effects.get('model:0:0')!.input as { model?: string }).model).toBe('test/model');

    // Back in the browser: the next turn restores the function lane's checkpoint.
    await t.sessions.send(sessionId, 'and now?');
    await t.drive(sessionId);
    state = await t.sessions.inspect(sessionId);
    expect(currentLane(state)).toBe('browser');
    expect(t.calls).toEqual(['browser', 'function', 'browser']);
    expect((state.effects.get('model:1:0')!.output as { text: string }).text).toMatch(/second turn sees [5-9] messages/);
  });

  // On libfx 0.0.14 the kernel places this steer differently from the tape, and the replay diverges. Open in
  // docs/followups.md with the steer below: the kernel lane's steering against libfx 0.0.14's.
  it.skip('does not place a steer on a request that is already on the tape', async () => {
    const bodies: string[] = [];
    const t = await setup(
      [
        () => toolCall('c1', 'shout', { text: 'hi' }),
        () => toolCall('c2', 'lookup', { q: 'acme' }),
        (body) => (bodies.push(JSON.stringify(body.prompt)), text('done')),
        (body) => (bodies.push(JSON.stringify(body.prompt)), text('hello!')),
      ],
      { steeringPollMs: 5 }
    );
    const { sessionId } = await t.sessions.start({ prompt: 'shout hi, then look up acme' });
    // The browser runs shout, then escalates lookup: two model requests on the tape.
    const woke: Lane[] = [];
    await deliver(
      {
        world: t.world,
        harness: harness(t.gateway, [shout(), backend(t.ran, 'browser')], { steeringPollMs: 5 }),
        lanes: { serves: ['browser'], settle: 'browser', async wake(_r, to) { woke.push(to); } },
      },
      sessionId
    );
    expect(woke).toEqual(['function']);
    // A steer arrives while the session is between lanes.
    await t.sessions.send(sessionId, 'also say hello', 'steer');
    // The function lane replays model:0:0, the memoized shout, and model:0:1
    // from the tape. The replay is slowed so many steering ticks land inside
    // it: a steer placed there would change model:0:1's request and wedge
    // the session (every later replay would diverge at the same spot).
    const base = harness(t.gateway, [shout(), backend(t.ran, 'function')], { steeringPollMs: 5 });
    const slowed: typeof base = {
      ...base,
      drive: (ctx) =>
        base.drive({
          ...ctx,
          effect: async (key, opts, fn) => {
            if (ctx.isMemoized(key)) await new Promise((r) => setTimeout(r, 60));
            return ctx.effect(key, opts, fn);
          },
        }),
    };
    const logs: string[] = [];
    await deliver(
      { world: t.world, harness: slowed, lanes: { serves: ['function'], settle: 'browser', async wake() {} }, log: (m) => logs.push(m) },
      sessionId
    );
    const state = await t.sessions.inspect(sessionId);
    expect(logs.filter((m) => m.includes('restart'))).toEqual([]);
    expect(t.ran).toEqual(['function']);
    expect(state.effects.get('checkpoint:0')?.status).toBe('completed');
    const steer = state.effectOrder.find((e) => e.name === 'steer');
    if (steer) {
      // Placed mid-turn: never before a request that was on the tape, and the
      // model step that first carried it says so.
      expect((steer.input as { beforeRequest: number }).beforeRequest).toBeGreaterThanOrEqual(2);
      const carrier = state.effectOrder.find((e) => e.name === 'model' && (e.input as { steers?: string[] }).steers?.includes(steer.key))!;
      expect(Number(carrier.key.split(':')[2])).toBeGreaterThanOrEqual(2);
      expect(JSON.stringify(carrier.input)).toContain('also say hello');
    } else {
      // Or it became the next turn.
      expect(state.effectOrder.filter((e) => e.name === 'turn')).toHaveLength(2);
    }
    expect(state.submissions.every((s) => state.placed.has(s.id))).toBe(true);
    expect(bodies.some((b) => b.includes('also say hello'))).toBe(true);

    // The whole turn, steer included, replays against the tape with no
    // divergence and no network: pretend the checkpoint was never written.
    const requestsBefore = t.gateway.requests.length;
    const replay = harness(t.gateway, [shout(), backend(t.ran, 'browser')]);
    const replayLogs: string[] = [];
    await deliver(
      {
        world: t.world,
        harness: {
          ...replay,
          drive: (ctx) =>
            replay.drive({
              ...ctx,
              state: () => {
                const st = ctx.state();
                return { ...st, effectOrder: st.effectOrder.filter((e) => e.name !== 'checkpoint') };
              },
            }),
        },
        lanes: { serves: ['browser'], settle: 'browser', async wake() {} },
        log: (m) => replayLogs.push(m),
      },
      sessionId
    );
    expect(replayLogs.filter((m) => m.includes('restart'))).toEqual([]);
    expect(t.gateway.requests.length).toBe(requestsBefore);
  });

  it('fails the session, rather than redelivering forever, when a lane is configured with a different model', async () => {
    const t = await setup([() => toolCall('c1', 'lookup', { q: 'acme' }), () => text('never reached')], {}, { model: 'test/other' });
    const { sessionId } = await t.sessions.start({ prompt: 'look up acme' });
    await t.drive(sessionId);
    const state = await t.sessions.inspect(sessionId);
    expect(state.status).toBe('failed');
    expect(state.failure?.message).toMatch(/different fx configuration/);
    expect(state.effectOrder.map((e) => e.name)).toEqual(['turn', 'model', 'handoff']);
    expect(t.ran).toEqual([]);
    expect(t.calls).toEqual(['browser']);
    // A failed session is not driven again.
    await t.drive(sessionId);
    expect(t.calls).toEqual(['browser']);
  });

  it('restarts once on a divergence, then fails the session with the reason', async () => {
    // Same configuration, but the function lane's kernel sends a different
    // body for the recorded request: a divergence no config hash can see.
    const skewed = (async (o: Record<string, unknown>) => {
      const inner = o.fetch as typeof fetch;
      return (createFxEngine as never as (o: unknown) => Promise<unknown>)({
        ...o,
        fetch: (u: RequestInfo | URL, init?: RequestInit) =>
          inner(u, init?.method === 'POST' ? { ...init, body: `${String(init.body)} ` } : init),
      });
    }) as never;
    const t = await setup([() => toolCall('c1', 'lookup', { q: 'acme' }), () => text('never reached')], {}, { createFxEngine: skewed });
    const { sessionId } = await t.sessions.start({ prompt: 'look up acme' });
    await t.drive(sessionId);
    const state = await t.sessions.inspect(sessionId);
    expect(t.logs.filter((m) => m.startsWith('restart: fx diverged'))).toHaveLength(1);
    expect(state.status).toBe('failed');
    expect(state.failure?.message).toMatch(/diverged from its tape at model:0:0.*again on a fresh read/);
    expect(t.ran).toEqual([]);
  });
});

/** A model response that streams `deltas`, then waits for `release` before it finishes. */
function gatedStream(deltas: string[], release: Promise<void>, gapMs = 0): Response {
  const enc = new TextEncoder();
  const event = (p: Part) => enc.encode(`data: ${JSON.stringify(p)}\n\n`);
  return new Response(
    new ReadableStream<Uint8Array>({
      async start(c) {
        c.enqueue(event({ type: 'text-start', id: 't' }));
        for (const d of deltas) {
          c.enqueue(event({ type: 'text-delta', id: 't', delta: d }));
          if (gapMs) await new Promise((r) => setTimeout(r, gapMs));
        }
        await release;
        c.enqueue(event({ type: 'finish', finishReason: { unified: 'stop', raw: 'stop' }, usage: { inputTokens: { total: 1 }, outputTokens: { total: 1 } } }));
        c.enqueue(enc.encode('data: [DONE]\n\n'));
        c.close();
      },
    }),
    { status: 200, headers: { 'content-type': 'text/event-stream' } }
  );
}

const steerKeyOf = (m: { input: unknown }) => (m.input as { steers?: string[] }).steers ?? [];

/** The same drive with every memoized effect slowed, so steering ticks land inside a replay. */
const slowReplay = (base: Harness, ms = 60): Harness => ({
  ...base,
  drive: (ctx) =>
    base.drive({
      ...ctx,
      effect: async (key, o, fn) => {
        if (ctx.isMemoized(key)) await new Promise((r) => setTimeout(r, ms));
        return ctx.effect(key, o, fn);
      },
    }),
});

/** Deliver again with the checkpoints hidden: the whole last turn re-runs against its tape. */
async function replayWithoutCheckpoint(world: LocalWorld, h: Harness, runId: string) {
  const logs: string[] = [];
  await deliver(
    {
      world,
      harness: {
        ...h,
        drive: (ctx) =>
          h.drive({
            ...ctx,
            state: () => {
              const st = ctx.state();
              return { ...st, effectOrder: st.effectOrder.filter((e) => e.name !== 'checkpoint') };
            },
          }),
      },
      log: (m) => logs.push(m),
    },
    runId
  );
  return logs;
}

describe('fx steering across crashes', () => {
  const STEER = 'STEER-TEXT-XYZ';

  // On libfx 0.0.14 a steer can join the retried request, depending on timing (CI under vitest 4). Open in
  // docs/followups.md with the steers below: the kernel lane's steering against libfx 0.0.14's.
  it.skip('keeps a retried model request exactly as its first attempt recorded it, so a steer cannot wedge the turn', async () => {
    const dataDir = await mkdtemp(join(tmpdir(), 'hw-fx-retry-'));
    const worldA = createWorld({ dataDir, recoverActiveRuns: false });
    worlds.push(worldA);
    worldA.registerHandler(QUEUE_PREFIX, async () => Response.json({ ok: true }));
    const bodies: string[] = [];
    let n = 0;
    let hang = true;
    const gateway = {
      requests: [] as unknown[],
      fetch: (async (_u, init) => {
        if ((init?.method ?? 'GET') === 'GET') return Response.json({ object: 'list', data: [] });
        bodies.push(String(init?.body));
        n++;
        if (n === 1) return sse(toolCall('c1', 'shout', { text: 'a' }));
        if (n === 2) return new Promise<Response>(() => {}); // model:0:1 dies mid-request
        if (n === 3) return sse(toolCall('c2', 'shout', { text: 'b' }));
        if (n === 4) return sse(toolCall('c3', 'hang', {}));
        return sse(text('done'));
      }) as typeof fetch,
    };
    const safeShout: FxTool = { ...shout(), replay: 'safe' };
    const hangTool: FxTool = {
      name: 'hang',
      description: 'Hangs.',
      inputSchema: { type: 'object', properties: {} },
      replay: 'never',
      async execute() {
        if (hang) await new Promise(() => {});
        return 'ok';
      },
    };
    const logs: string[] = [];
    const make = (poll: number) => harness(gateway as never, [safeShout, hangTool], { steeringPollMs: poll });
    const a = durableSessions({ world: worldA, harness: make(1e9) });
    const { sessionId } = await a.start({ prompt: 'go' });
    void deliver({ world: worldA, harness: make(1e9) }, sessionId);
    await until(async () => ((await a.inspect(sessionId)).effects.get('model:0:1')?.status === 'running' ? true : undefined));
    await worldA.close?.();
    await new Promise((r) => setTimeout(r, 400));

    // The user steers while the session is down; recovery retries model:0:1.
    const world = createWorld({ dataDir, recoverActiveRuns: false });
    worlds.push(world);
    world.registerHandler(QUEUE_PREFIX, async () => Response.json({ ok: true }));
    const sessions = durableSessions({ world, harness: make(5) });
    await sessions.send(sessionId, STEER, 'steer');
    // This drive runs in a process that dies in the hang (its own view of the World).
    const dying = hostProcess(world);
    void deliverAll({ world: dying.world, harness: slowReplay(make(5)), log: (m) => logs.push(m) }, sessionId);
    await until(async () => ((await sessions.inspect(sessionId)).effectOrder.some((e) => e.name === 'tool.hang' && e.status === 'running') ? true : undefined));
    const mid = await sessions.inspect(sessionId);
    const steer = mid.effectOrder.find((e) => e.name === 'steer')!;
    // The retry went out without the steer and matches its first attempt.
    expect(bodies[2]).toBe(bodies[1]);
    expect(steerKeyOf(mid.effects.get('model:0:1')!)).toEqual([]);
    expect((steer.input as { beforeRequest: number }).beforeRequest).toBeGreaterThanOrEqual(2);
    const carrier = mid.effectOrder.find((e) => e.name === 'model' && steerKeyOf(e).includes(steer.key))!;
    expect(bodies[Number(carrier.key.split(':')[2]) + 1]).toContain(STEER);

    // The hang dies with that process too; the next replay of the turn has
    // to reproduce every recorded request, steer included, to finish.
    dying.kill();
    hang = false;
    await deliverAll({ world, harness: make(5), log: (m) => logs.push(m) }, sessionId);
    const done = await sessions.inspect(sessionId);
    expect(logs.filter((m) => m.includes('restart'))).toEqual([]);
    expect(done.status).toBe('running');
    expect(done.effects.get('checkpoint:0')?.status).toBe('completed');
  });

  // libfx 0.0.14's engine does not send a steer re-offered while a crashed turn replays its interrupted request
  // (1432's did). Open in docs/followups.md: the kernel lane's steering across crashes, on libfx 0.0.14.
  it.skip('delivers a steer taken while the request it followed was interrupted', async () => {
    const dataDir = await mkdtemp(join(tmpdir(), 'hw-fx-steer-crash-'));
    const worldA = createWorld({ dataDir, recoverActiveRuns: false });
    worlds.push(worldA);
    worldA.registerHandler(QUEUE_PREFIX, async () => Response.json({ ok: true }));
    const hung = fakeGateway([]);
    hung.fetch = (async (_u, init) => {
      if ((init?.method ?? 'GET') === 'GET') return Response.json({ object: 'list', data: [] });
      return gatedStream(['thinking'], new Promise(() => {}));
    }) as typeof fetch;
    const a = durableSessions({ world: worldA, harness: harness(hung, []) });
    const { sessionId } = await a.start({ prompt: 'write a poem' });
    void deliver({ world: worldA, harness: harness(hung, [], { steeringPollMs: 5 }) }, sessionId);
    await until(async () => ((await a.inspect(sessionId)).effects.get('model:0:0')?.status === 'running' ? true : undefined));
    // Steered mid-stream: taken, and handed to the kernel for the next request...
    await a.send(sessionId, STEER, 'steer');
    await until(async () => ((await a.inspect(sessionId)).effectOrder.find((e) => e.name === 'steer')?.status === 'completed' ? true : undefined));
    // ...which never happens: the process dies with model:0:0 still streaming.
    await worldA.close?.();
    await new Promise((r) => setTimeout(r, 400));

    const gateway = fakeGateway([() => text('A poem.'), (body) => text(JSON.stringify(body.prompt).includes(STEER) ? 'Steered.' : 'Never saw it.')]);
    const world = createWorld({ dataDir, recoverActiveRuns: false });
    worlds.push(world);
    world.registerHandler(QUEUE_PREFIX, async () => Response.json({ ok: true }));
    const sessions = durableSessions({ world, harness: harness(gateway, []) });
    const logs: string[] = [];
    await deliverAll({ world, harness: harness(gateway, [], { steeringPollMs: 5 }), log: (m) => logs.push(m) }, sessionId);
    const state = await sessions.inspect(sessionId);
    expect(state.effects.get('checkpoint:0')?.status).toBe('completed');
    const steer = state.effectOrder.find((e) => e.name === 'steer')!;
    expect(gateway.requests.map((b) => JSON.stringify(b.prompt).includes(STEER))).toEqual([false, true]);
    expect(steerKeyOf(state.effects.get('model:0:1')!)).toEqual([steer.key]);
    expect((state.effects.get('model:0:1')!.output as { text: string }).text).toBe('Steered.');
    expect(logs.filter((m) => m.includes('restart'))).toEqual([]);

    // And it replays: the same two requests, from the tape, no divergence.
    const before = gateway.requests.length;
    const replayLogs = await replayWithoutCheckpoint(world, harness(gateway, []), sessionId);
    expect(replayLogs.filter((m) => m.includes('restart'))).toEqual([]);
    expect(gateway.requests.length).toBe(before);
  });

  it('takes a steer that was waiting when the drive started at its first live request, not at the next poll', async () => {
    const world = createWorld({ dataDir: await mkdtemp(join(tmpdir(), 'hw-fx-steer-waiting-')), recoverActiveRuns: false });
    worlds.push(world);
    world.registerHandler(QUEUE_PREFIX, async () => Response.json({ ok: true }));
    let n = 0;
    const gateway = fakeGateway([]);
    gateway.fetch = (async (_u, init) => {
      if ((init?.method ?? 'GET') === 'GET') return Response.json({ object: 'list', data: [] });
      const body = JSON.parse(String(init?.body));
      gateway.requests.push(body);
      n++;
      if (n === 1) return gatedStream(['half'], new Promise(() => {})); // the first drive halts mid-stream
      if (n === 2) return sse(toolCall('c1', 'shout', { text: 'a' }));
      return sse(text(JSON.stringify(body.prompt).includes(STEER) ? 'Steered.' : 'Never saw it.'));
    }) as typeof fetch;
    const safeShout: FxTool = { ...shout(), replay: 'safe' };
    // The first drive loses the session as it takes the steer, so the steer
    // stays in the inbox, unplaced, with turn 0 already recorded without it.
    const first = harness(gateway, [safeShout], { steeringPollMs: 5 });
    const losing: Harness = {
      ...first,
      drive: (ctx) =>
        first.drive({
          ...ctx,
          effect: async (key, o, fn) => {
            if (key.startsWith('steer:')) throw new Yield('contended', 1);
            return ctx.effect(key, o, fn);
          },
        }),
    };
    const sessions = durableSessions({ world, harness: first });
    const { sessionId } = await sessions.start({ prompt: 'go' });
    const halted = deliver({ world, harness: losing }, sessionId);
    await until(async () => ((await sessions.inspect(sessionId)).effects.get('model:0:0')?.status === 'running' ? true : undefined));
    await sessions.send(sessionId, STEER, 'steer');
    // It halts and hands the session to a continuation (never `{ timeoutSeconds }`).
    expect(await halted).toBeUndefined();

    // The next drive starts after the steer's hint, and polls too rarely to
    // matter: it has to look at the inbox itself once a request is live.
    await deliverAll({ world, harness: harness(gateway, [safeShout], { steeringPollMs: 1e9 }) }, sessionId);
    const state = await sessions.inspect(sessionId);
    expect(state.effects.get('checkpoint:0')?.status).toBe('completed');
    // Landed in turn 0's next request, rather than becoming a turn of its own once turn 0 ended.
    expect(gateway.requests.slice(1).map((b) => JSON.stringify(b.prompt).includes(STEER))).toEqual([false, true]);
    expect((state.effects.get('model:0:1')!.output as { text: string }).text).toBe('Steered.');
    expect(state.effects.has('turn:1')).toBe(false);
  });

  it('releases a streaming model step when the drive halts, instead of recording it as failed', async () => {
    const world = createWorld({ dataDir: await mkdtemp(join(tmpdir(), 'hw-fx-halt-')), recoverActiveRuns: false });
    worlds.push(world);
    world.registerHandler(QUEUE_PREFIX, async () => Response.json({ ok: true }));
    let calls = 0;
    const gateway = fakeGateway([]);
    gateway.fetch = (async (_u, init) => {
      if ((init?.method ?? 'GET') === 'GET') return Response.json({ object: 'list', data: [] });
      calls++;
      return calls === 1 ? gatedStream(['half'], new Promise(() => {})) : sse(text('whole answer'));
    }) as typeof fetch;
    const base = harness(gateway, [], { steeringPollMs: 5 });
    // A steer effect that loses the session (another driver wrote meanwhile).
    const losing: Harness = {
      ...base,
      drive: (ctx) =>
        base.drive({
          ...ctx,
          effect: async (key, o, fn) => {
            if (key.startsWith('steer:')) throw new Yield('contended', 1);
            return ctx.effect(key, o, fn);
          },
        }),
    };
    const sessions = durableSessions({ world, harness: base });
    const { sessionId } = await sessions.start({ prompt: 'answer' });
    const first = deliver({ world, harness: losing }, sessionId);
    await until(async () => ((await sessions.inspect(sessionId)).effects.get('model:0:0')?.status === 'running' ? true : undefined));
    await sessions.send(sessionId, 'be brief', 'steer');
    expect(await first).toBeUndefined();
    let state = await sessions.inspect(sessionId);
    // Released for a retry, not failed: a failed model step would be
    // rethrown to the kernel on every later delivery.
    expect(state.effects.get('model:0:0')?.status).toBe('pending');

    const logs: string[] = [];
    await deliver({ world, harness: base, log: (m) => logs.push(m) }, sessionId);
    state = await sessions.inspect(sessionId);
    expect(state.effectOrder.filter((e) => e.status === 'failed')).toEqual([]);
    expect(state.effects.get('model:0:0')?.status).toBe('completed');
    expect(state.effectOrder.filter((e) => e.name === 'checkpoint').every((e) => e.status === 'completed')).toBe(true);
    expect(state.submissions.every((s) => state.placed.has(s.id))).toBe(true);
    // The steer was not lost: a request carried it, or it became the next turn.
    const delivered = state.effectOrder.some(
      (e) => (e.name === 'model' && steerKeyOf(e).length > 0) || (e.name === 'turn' && JSON.stringify(e.input).includes('be brief'))
    );
    expect(delivered).toBe(true);
  });

  it('runs a message that arrives mid-turn as the next turn, in the same delivery', async () => {
    const world = createWorld({ dataDir: await mkdtemp(join(tmpdir(), 'hw-fx-next-')), recoverActiveRuns: false });
    worlds.push(world);
    world.registerHandler(QUEUE_PREFIX, async () => Response.json({ ok: true }));
    let release!: () => void;
    const prompts: string[] = [];
    const gateway = fakeGateway([]);
    gateway.fetch = (async (_u, init) => {
      if ((init?.method ?? 'GET') === 'GET') return Response.json({ object: 'list', data: [] });
      const body = JSON.parse(String(init?.body));
      prompts.push(JSON.stringify(body.prompt.at(-1)));
      return prompts.length === 1 ? gatedStream(['one'], new Promise<void>((r) => (release = r))) : sse(text('two'));
    }) as typeof fetch;
    const base = harness(gateway, []);
    const sessions = durableSessions({ world, harness: base });
    const { sessionId } = await sessions.start({ prompt: 'first' });
    const done = deliver({ world, harness: base }, sessionId);
    await until(async () => ((await sessions.inspect(sessionId)).effects.get('model:0:0')?.status === 'running' ? true : undefined));
    await sessions.send(sessionId, 'second');
    release();
    await done;
    const state = await sessions.inspect(sessionId);
    expect(state.effectOrder.map((e) => e.key.split(':').slice(0, 2).join(':'))).toEqual([
      'turn:0', 'model:0', 'checkpoint:0', 'turn:1', 'model:1', 'checkpoint:1',
    ]);
    expect(prompts[1]).toContain('second');
  });

  it('defers a steer that arrives after the budget ran out, rather than cutting off the stream', async () => {
    const world = createWorld({ dataDir: await mkdtemp(join(tmpdir(), 'hw-fx-budget-')), recoverActiveRuns: false });
    worlds.push(world);
    const woke: unknown[] = [];
    world.registerHandler(QUEUE_PREFIX, async (req) => {
      woke.push(req);
      return Response.json({ ok: true });
    });
    let release!: () => void;
    const gateway = fakeGateway([]);
    gateway.fetch = (async (_u, init) => {
      if ((init?.method ?? 'GET') === 'GET') return Response.json({ object: 'list', data: [] });
      return gatedStream(['a', 'b'], new Promise<void>((r) => (release = r)));
    }) as typeof fetch;
    const base = harness(gateway, [], { steeringPollMs: 5 });
    const sessions = durableSessions({ world, harness: base });
    const { sessionId } = await sessions.start({ prompt: 'answer' });
    const first = deliver({ world, harness: base, maxInvocationMs: 300, deadlineMarginMs: 0 }, sessionId);
    await until(async () => ((await sessions.inspect(sessionId)).effects.get('model:0:0')?.status === 'running' ? true : undefined));
    await new Promise((r) => setTimeout(r, 400)); // budget spent, stream still open
    await sessions.send(sessionId, 'be brief', 'steer');
    await new Promise((r) => setTimeout(r, 100));
    release();
    await first;
    const state = await sessions.inspect(sessionId);
    expect(state.effects.get('model:0:0')?.status).toBe('completed');
    expect(state.effectOrder.some((e) => e.name === 'steer')).toBe(false);
  });

  it('ends a turn whose model call failed, so the session takes the next prompt', async () => {
    const world = createWorld({ dataDir: await mkdtemp(join(tmpdir(), 'hw-fx-fail-')), recoverActiveRuns: false });
    worlds.push(world);
    world.registerHandler(QUEUE_PREFIX, async () => Response.json({ ok: true }));
    let calls = 0;
    const gateway = fakeGateway([]);
    gateway.fetch = (async (_u, init) => {
      if ((init?.method ?? 'GET') === 'GET') return Response.json({ object: 'list', data: [] });
      calls++;
      return calls === 1 ? new Response('bad request', { status: 400 }) : sse(text('fine now'));
    }) as typeof fetch;
    const base = harness(gateway, []);
    const sessions = durableSessions({ world, harness: base });
    const { sessionId } = await sessions.start({ prompt: 'one' });
    await deliver({ world, harness: base }, sessionId);
    // A redelivery replays the failure and ends the turn the same way.
    await deliver({ world, harness: base }, sessionId);
    let state = await sessions.inspect(sessionId);
    expect(state.effects.get('model:0:0')?.status).toBe('failed');
    expect((state.effects.get('checkpoint:0')?.output as { error?: string }).error).toBeTruthy();
    await sessions.send(sessionId, 'two');
    await deliver({ world, harness: base }, sessionId);
    state = await sessions.inspect(sessionId);
    expect((state.effects.get('model:1:0')?.output as { text: string }).text).toBe('fine now');
    expect(calls).toBe(2);
  });

  it('fails the session with a clear reason when a checkpoint came from a libfx this process cannot restore', async () => {
    const world = createWorld({ dataDir: await mkdtemp(join(tmpdir(), 'hw-fx-kernel-')), recoverActiveRuns: false });
    worlds.push(world);
    world.registerHandler(QUEUE_PREFIX, async () => Response.json({ ok: true }));
    const gateway = fakeGateway([() => text('hi')]);
    const old = harness(gateway, [], { kernelVersion: '0.0.10' });
    const sessions = durableSessions({ world, harness: old });
    const { sessionId } = await sessions.start({ prompt: 'one' });
    await deliver({ world, harness: old }, sessionId);
    await sessions.send(sessionId, 'two');
    const picky = (async (o: Record<string, unknown>) => {
      if (o.checkpoint) throw new Error('unsupported checkpoint schema');
      return (createFxEngine as never as (o: unknown) => Promise<unknown>)(o);
    }) as never;
    await deliver({ world, harness: harness(gateway, [], { kernelVersion: '0.0.11', createFxEngine: picky }) }, sessionId);
    const state = await sessions.inspect(sessionId);
    expect(state.status).toBe('failed');
    expect(state.failure?.message).toMatch(/libfx 0\.0\.10.*libfx 0\.0\.11.*unsupported checkpoint schema/);
  });
});

describe('steersCarriedBy', () => {
  const steer = (t: string) => ({ role: 'user', content: [{ type: 'text', text: `<user_steering>\nApply this live user update to the current task.\n\n${t}\n</user_steering>` }] });
  const body = (tail: unknown[]) => ({
    prompt: [
      { role: 'system', content: 'Say ok when done. Use lookup_customer.' },
      { role: 'user', content: [{ type: 'text', text: 'ok' }] },
      { role: 'assistant', content: [{ type: 'tool-call', toolCallId: 'c1', toolName: 'lookup_customer', input: { q: 'ok' } }] },
      ...tail,
    ],
    tools: [{ name: 'lookup_customer', description: 'look it up, ok?' }],
  });
  const candidates = [{ key: 'steer:a', text: 'ok' }];

  it('ignores a steer whose text only appears elsewhere in the request', () => {
    const tool = { role: 'tool', content: [{ type: 'tool-result', toolCallId: 'c1', output: { type: 'text', value: 'ok' } }] };
    expect(steersCarriedBy(body([tool]), 1, candidates)).toEqual([]);
  });

  it('finds a steer the request appended after the last response', () => {
    const tool = { role: 'tool', content: [{ type: 'tool-result', toolCallId: 'c1', output: { type: 'text', value: 'x' } }] };
    expect(steersCarriedBy(body([tool, steer('ok')]), 1, candidates)).toEqual(['steer:a']);
    // Two steers with the same text land one per message, in commit order.
    const two = [{ key: 'steer:a', text: 'ok' }, { key: 'steer:b', text: 'ok' }];
    expect(steersCarriedBy(body([tool, steer('ok')]), 1, two)).toEqual(['steer:a']);
    expect(steersCarriedBy(body([tool, steer('ok'), steer('ok')]), 1, two)).toEqual(['steer:a', 'steer:b']);
  });

  it("does not count a turn's own prompt as a steer", () => {
    const prompt = { role: 'user', content: [{ type: 'text', text: 'ok' }] };
    expect(steersCarriedBy(body([prompt]), 0, candidates)).toEqual([]);
    expect(steersCarriedBy(body([prompt, steer('ok')]), 0, candidates)).toEqual(['steer:a']);
  });
});

describe('isDelta (what reaches the kernel before the model step commits)', () => {
  const sse = (part: object) => `data: ${JSON.stringify(part)}\n\n`;
  it('forwards the stream preamble and text, so text streams', () => {
    for (const type of ['stream-start', 'response-metadata', 'text-start', 'text-delta', 'text-end', 'reasoning-delta']) expect(isDelta(sse({ type }))).toBe(true);
  });
  it('holds tool calls and the finish', () => {
    for (const type of ['tool-input-start', 'tool-call', 'finish']) expect(isDelta(sse({ type }))).toBe(false);
    expect(isDelta('data: [DONE]\n\n')).toBe(false);
  });
});