Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

104 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { addHeldSecretScrubber, HELD_SECRET_MASK } from "../../shared/redact.ts";

import { checkInvariants } from "./invariants.ts";
import { PROGRESS_WRITE_MS, RERUN_BURST, RERUN_GAP_MS, RERUN_HELD, RERUN_SUPERSEDED, Runtime, type BeginRequest } from "./runtime.ts";
import { openRuntimeStore, type RuntimeStore } from "./store.ts";
import { CUT_TWICE, INTERRUPTED_OUTWARD, loopStop, nextGuard, outwardDetail } from "./tree.ts";
import type { Assignment, Clock, EnqueueRequest, Limits, RunInput, RunResult, TurnRunner } from "./types.ts";

const flush = () => new Promise<void>(resolve => setImmediate(resolve));

interface FakeClock extends Clock { t: number; advance(ms: number): void }
const fakeClock = (start = 1_000_000): FakeClock => ({ t: start, now() { return this.t; }, monotonic() { return this.t; }, advance(ms) { this.t += ms; } });

type Script = (a: Assignment, input: RunInput) => RunResult | "hold" | undefined;

interface Fake extends TurnRunner {
  runs: Array<{ a: Assignment; input: RunInput; signal: AbortSignal }>;
  reports: Array<{ a: Assignment; parent?: Assignment }>;
  parks: Array<{ a: Assignment; question: string }>;
  changes: Assignment[][];
  held: Map<string, (result: RunResult) => void>;
  script: Script;
  canStartAnswer: true | false | string | (() => true | false | string);
  validateAnswer: string | undefined;
  reportThrows: number;
  release(id: string, result: RunResult): Promise<void>;
  live(): { running: Set<string>; parked: Set<string> };
}

function fakeRunner(script: Script = () => undefined): Fake {
  const fake: Fake = {
    runs: [], reports: [], parks: [], changes: [], held: new Map(), script, canStartAnswer: true, validateAnswer: undefined, reportThrows: 0,
    validate: () => fake.validateAnswer,
    canStart: () => typeof fake.canStartAnswer === "function" ? fake.canStartAnswer() : fake.canStartAnswer,
    run(a, input, signal) {
      fake.runs.push({ a, input, signal });
      const result = fake.script(a, input);
      if (result === "hold") return new Promise<RunResult>(resolve => fake.held.set(a.id, resolve));
      return Promise.resolve(result ?? { kind: "completed", text: `${a.assignee.botId} done` });
    },
    park: (a, question) => { fake.parks.push({ a, question }); },
    report(a, parent) {
      if (fake.reportThrows > 0) { fake.reportThrows -= 1; throw new Error("report failed"); }
      fake.reports.push({ a, ...(parent ? { parent } : {}) });
    },
    changed: rows => { fake.changes.push([...rows]); },
    names: a => ({ requester: a.requester.botId ?? "Milind", assignee: a.assignee.botId, person: "Milind" }),
    async release(id, result) {
      const resolve = fake.held.get(id);
      if (!resolve) throw new Error(`no held turn ${id}`);
      fake.held.delete(id);
      resolve(result);
      await flush();
    },
    live() {
      const running = new Set(fake.runs.filter(r => fake.held.has(r.a.id)).map(r => r.a.id));
      const parked = new Set(fake.parks.map(p => p.a.id));
      return { running, parked };
    },
  };
  return fake;
}

const chief = { botId: "chief", destination: { kind: "standing_thread" as const, threadId: "chief-chat" } };
const beginReq = (patch: Partial<BeginRequest> = {}): BeginRequest => ({
  origin: { kind: "person", source: "chat", quote: "Ship the CSV export" },
  requester: { kind: "person", threadId: "chief-chat", chief: true },
  assignee: chief, brief: "Ship the CSV export", level: "auto", ...patch,
});
const toBot = (botId: string, parentId: string, brief = `Build it, ${botId}`, patch: Partial<EnqueueRequest> = {}): EnqueueRequest => ({
  parentId, origin: { kind: "person", source: "chat" },
  requester: { kind: "bot_turn", botId: "chief", threadId: "chief-chat", chief: true },
  assignee: { botId, destination: { kind: "work_thread", threadId: `${botId}-work` } },
  brief, levels: { own: "ask", chief: "auto" }, driverKind: "claudeAgent", ...patch,
});

interface World { store: RuntimeStore; runner: Fake; clock: FakeClock; runtime: Runtime; logs: string[]; limits: Partial<Limits> }
function world(script?: Script, limits: Partial<Limits> = {}, store = openRuntimeStore(":memory:"), ports: Pick<TurnRunner, "outward"> = {}): World {
  const runner = fakeRunner(script);
  if (ports.outward) runner.outward = ports.outward;
  const clock = fakeClock();
  const logs: string[] = [];
  const runtime = new Runtime(store, runner, { clock, limits, log: line => logs.push(line) });
  runtime.open();
  return { store, runner, clock, runtime, logs, limits };
}
const ok = (a: EnqueueResult): Assignment => { if (!a.ok) throw new Error(a.reason); return a.assignment; };
type EnqueueResult = ReturnType<Runtime["enqueue"]>;
const invariants = (w: World) => expect(checkInvariants(w.store, w.runner.live())).toEqual([]);

describe("Runtime: one tree, one lane", () => {
  it("runs a child, reports it once, then wakes the root with the settled children in one resume turn", async () => {
    const w = world();
    const root = w.runtime.begin(beginReq());
    expect(root.status).toBe("running");
    expect(root.executions).toBe(1);
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    expect(child).toMatchObject({ status: "queued", depth: 1, rootId: root.id, parentId: root.id, level: "auto", levelSource: "chief" });
    expect(w.runtime.liveEdges()).toEqual([{ sourceBotId: "chief", targetBotId: "builder", state: "queued", threadId: "builder-work" }]);
    w.runtime.tick();
    expect(w.runner.runs).toHaveLength(1);
    expect(w.runner.runs[0]!.input).toMatchObject({ resumed: false, restartNote: "", level: "auto", mode: "work" });
    expect(w.runner.runs[0]!.input.envelope).toContain(child.id);
    expect(w.store.get(child.id)?.status).toBe("running");
    await flush();
    expect(w.store.get(child.id)).toMatchObject({ status: "completed", outcome: "done", result: "builder done" });
    expect(w.runner.reports).toHaveLength(1);
    expect(w.runner.reports[0]).toMatchObject({ a: { id: child.id }, parent: { id: root.id } });
    const inbox = w.store.inbox({ botId: "chief", threadId: "chief-chat" });
    expect(inbox).toHaveLength(1);
    expect(inbox[0]).toMatchObject({ assignmentId: child.id, kind: "settled" });
    expect(inbox[0]?.deliveredAt).toBeUndefined();
    expect(w.runtime.outstanding({ threadId: "chief-chat" })).toEqual([]);
    w.runtime.end(root.id, { ok: true, text: "Handed to builder" });
    expect(w.store.get(root.id)?.status).toBe("waiting");
    w.runtime.tick();
    expect(w.runner.runs).toHaveLength(2);
    const resume = w.runner.runs[1]!;
    expect(resume.a.id).toBe(root.id);
    expect(resume.input.resumed).toBe(true);
    expect(resume.input.settled?.map(s => s.id)).toEqual([child.id]);
    expect(w.store.inbox({ botId: "chief", threadId: "chief-chat" })[0]?.deliveredAt).toBe(w.clock.t);
    await flush();
    const done = w.store.get(root.id)!;
    expect(done).toMatchObject({ status: "completed", outcome: "done", result: "chief done", executions: 2 });
    expect(w.runner.reports).toHaveLength(1);
    expect(done.reportedAt).toBeDefined();
    expect(w.store.inbox({ threadId: "chief-chat" }).map(i => i.assignmentId)).toEqual([child.id]);
    expect(w.store.events(root.id).map(e => e.kind)).toEqual(["enqueued", "started", "waiting", "resume_due", "admitted", "started", "settled", "reported"]);
    invariants(w);
  });

  it("writes running before it runs, and a failed write starts nothing", async () => {
    const inner = openRuntimeStore(":memory:");
    let failOnce = true;
    const flaky: RuntimeStore = { ...inner, update(a, event) {
      if (event?.kind === "started" && a.parentId && failOnce) { failOnce = false; throw new Error("disk full"); }
      inner.update(a, event);
    } };
    const w = world(undefined, {}, flaky);
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick();
    expect(w.runner.runs).toHaveLength(0);
    expect(w.logs.some(l => /not written, nothing started/.test(l))).toBe(true);
    const row = inner.get(child.id)!;
    expect(row).toMatchObject({ status: "queued", executions: 0 });
    expect(inner.events(child.id).map(e => e.kind)).toEqual(["enqueued"]);
    expect(inner.get(root.id)?.budgets?.executions).toBe(1);
    w.runtime.tick();
    expect(w.runner.runs).toHaveLength(1);
    expect(w.runner.runs[0]!.a.status).toBe("running");
    expect(inner.events(child.id).map(e => e.kind)).toEqual(["enqueued", "admitted", "started"]);
    await flush();
    invariants(w);
  });

  it("dedupes the same brief under one parent and runs it again only with rework", async () => {
    const w = world();
    const root = w.runtime.begin(beginReq());
    const first = ok(w.runtime.enqueue(toBot("builder", root.id)));
    const again = w.runtime.enqueue(toBot("builder", root.id));
    expect(again).toMatchObject({ ok: true, duplicate: true, assignment: { id: first.id } });
    expect(w.store.get(root.id)?.budgets?.requests).toBe(1);
    w.runtime.tick(); await flush();
    expect(w.store.get(first.id)?.status).toBe("completed");
    expect(w.runtime.enqueue(toBot("builder", root.id))).toMatchObject({ ok: true, duplicate: true, assignment: { id: first.id } });
    const rework = ok(w.runtime.enqueue(toBot("builder", root.id, undefined, { rework: true })));
    expect(rework.id).not.toBe(first.id);
    expect(rework.rework).toBe(true);
    const elsewhere = ok(w.runtime.enqueue(toBot("builder", root.id, undefined,
      { assignee: { botId: "builder", destination: { kind: "room_thread", threadId: "room-1", groupId: "g1" } } })));
    expect(elsewhere.id).not.toBe(rework.id);
    w.runtime.tick(); await flush();
    invariants(w);
  });

  it("refuses with typed reasons: a parent that is not running, an ancestor, depth, the request budget, the host", () => {
    const w = world(undefined, { depth: 2, requests: 3 });
    const root = w.runtime.begin(beginReq());
    expect(w.runtime.enqueue(toBot("builder", "nope"))).toMatchObject({ ok: false, outcome: "refused", reason: /no longer exists/ });
    expect(w.runtime.enqueue(toBot("chief", root.id))).toMatchObject({ ok: false, outcome: "refused", reason: /ancestor/ });
    const lead = ok(w.runtime.enqueue(toBot("lead", root.id)));
    expect(w.runtime.enqueue(toBot("builder", lead.id))).toMatchObject({ ok: false, outcome: "refused", reason: /not running/ });
    w.runtime.tick();
    expect(w.store.get(lead.id)?.status).toBe("running");
    const deep = ok(w.runtime.enqueue({ ...toBot("builder", lead.id), requester: { kind: "bot_turn", botId: "lead", threadId: "lead-work" } }));
    expect(deep.depth).toBe(2);
    expect(w.runtime.enqueue({ ...toBot("tester", deep.id), requester: { kind: "bot_turn", botId: "builder", threadId: "builder-work" } }))
      .toMatchObject({ ok: false, outcome: "refused", reason: /not running/ });
    w.runtime.tick();
    expect(w.runtime.enqueue({ ...toBot("tester", deep.id), requester: { kind: "bot_turn", botId: "builder", threadId: "builder-work" } }))
      .toMatchObject({ ok: false, outcome: "refused", reason: /depth/ });
    w.runner.validateAnswer = "No route to that bot";
    expect(w.runtime.enqueue(toBot("ops", root.id))).toMatchObject({ ok: false, outcome: "refused", reason: "No route to that bot" });
    w.runner.validateAnswer = undefined;
    expect(w.runtime.enqueue(toBot("reviewer", root.id)).ok).toBe(true);
    expect(w.runtime.enqueue(toBot("writer", root.id))).toMatchObject({ ok: false, outcome: "budget", reason: /Request budget/ });
    expect(w.store.events(root.id).some(e => e.kind === "budget")).toBe(true);
    expect(w.store.all()).toHaveLength(4);
  });

  it("lets work come back to an ancestor only when its reply cannot wait on that ancestor", async () => {
    const w = world(() => "hold");
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick();
    expect(w.store.get(child.id)?.status).toBe("running");
    const builder = { kind: "bot_turn" as const, botId: "builder", threadId: "builder-work" };
    const ask = ok(w.runtime.enqueue({ ...toBot("chief", child.id), requester: builder, reply: "poll" }));
    expect(ask).toMatchObject({ parentId: child.id, reply: "poll", assignee: { botId: "chief" } });
    const notice = ok(w.runtime.enqueue({ ...toBot("chief", child.id, "A one-way notice"), requester: builder, mode: "notify" }));
    expect(notice).toMatchObject({ parentId: child.id, reply: "none" });
    const refused = "Cannot assign work back to an ancestor; results return automatically";
    expect(w.runtime.enqueue({ ...toBot("chief", child.id, "Wait on me"), requester: builder }))
      .toMatchObject({ ok: false, outcome: "refused", reason: refused });
    expect(w.runtime.enqueue({ ...toBot("chief", child.id, "Wait on me too"), requester: builder, reply: "resume" }))
      .toMatchObject({ ok: false, outcome: "refused", reason: refused });
    invariants(w);
    await w.runner.release(child.id, { kind: "completed", text: "builder done" });
    w.runtime.tick();
    expect(w.store.get(ask.id)?.status).toBe("running");
    await w.runner.release(ask.id, { kind: "completed", text: "chief answered" });
    expect(w.store.get(ask.id)).toMatchObject({ status: "completed", outcome: "done" });
    expect(w.store.inbox({ botId: "builder", threadId: "builder-work" }).map(i => i.assignmentId)).toEqual([ask.id]);
    invariants(w);
  });

  it("renews the root's budgets on progress, and never refuses the owed resume turn", async () => {
    const w = world(undefined, { requests: 2, executions: 2 });
    const root = w.runtime.begin(beginReq());
    const a = ok(w.runtime.enqueue(toBot("a", root.id)));
    const b = ok(w.runtime.enqueue(toBot("b", root.id)));
    expect(w.runtime.enqueue(toBot("c", root.id))).toMatchObject({ ok: false, outcome: "budget" });
    w.runtime.tick();
    expect(w.store.get(a.id)?.status).toBe("running");
    expect(w.store.get(b.id)).toMatchObject({ status: "failed", outcome: "budget" });
    await flush();
    expect(w.store.get(root.id)?.budgets).toMatchObject({ requests: 0, executions: 0, wakes: 1 });
    expect(w.runtime.enqueue(toBot("c", root.id)).ok).toBe(true);
    w.runtime.end(root.id, { ok: true, text: "waiting" });
    w.runtime.tick(); await flush(); w.runtime.tick();
    expect(w.store.get(root.id)?.status).toBe("running");
    expect(w.runner.runs.at(-1)?.a.id).toBe(root.id);
    await flush();
    expect(w.store.get(root.id)?.status).toBe("completed");
    invariants(w);
  });

  it("work held for a place that was away is not expired by the look that finds the place back: its queue window starts then", () => {
    const w = world(undefined, { queueMs: 1_000, stallMs: 1e9 });
    let away = true;
    w.runner.away = a => a.assignee.botId === "far" && away;
    w.runner.canStartAnswer = "far is not ready";
    const root = w.runtime.begin(beginReq());
    const held = ok(w.runtime.enqueue(toBot("far", root.id)));
    w.runtime.tick();
    w.clock.advance(50_000);
    w.runtime.tick();
    expect(w.store.get(held.id)).toMatchObject({ status: "queued" });
    // The place comes back, and the next look comes later than the whole queue window.
    w.clock.advance(5_000);
    away = false;
    w.runtime.tick();
    expect(w.store.get(held.id)).toMatchObject({ status: "queued" });
    w.clock.advance(999);
    w.runtime.tick();
    expect(w.store.get(held.id)).toMatchObject({ status: "queued" });
    // From then on it is ordinary queued work, and the queue window is its bound again.
    w.clock.advance(2);
    w.runtime.tick();
    expect(w.store.get(held.id)).toMatchObject({ status: "failed", outcome: "expired", outcomeDetail: "never started" });
  });

  it("expires a queue entry that never started and a question nobody answered, never as completed", async () => {
    const w = world(undefined, { queueMs: 1_000, inputWaitMs: 5_000 });
    const root = w.runtime.begin(beginReq());
    w.runner.canStartAnswer = "builder is busy on another thread";
    const stuck = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick(); w.runtime.tick();
    expect(w.store.events(stuck.id).filter(e => e.kind === "held")).toHaveLength(1);
    expect(w.store.events(stuck.id).filter(e => e.kind === "admitted")).toHaveLength(0);
    expect(w.store.events(stuck.id).at(-1)?.detail).toBe("builder is busy on another thread");
    w.runner.canStartAnswer = "builder's room turn is running";
    w.runtime.tick();
    expect(w.store.events(stuck.id).filter(e => e.kind === "held").map(e => e.detail)).toEqual(["builder is busy on another thread", "builder's room turn is running"]);
    w.clock.advance(1_001);
    w.runtime.tick();
    expect(w.store.get(stuck.id)).toMatchObject({ status: "failed", outcome: "expired", outcomeDetail: "never started" });
    expect(w.runner.reports.map(r => r.a.id)).toEqual([stuck.id]);
    w.runner.canStartAnswer = true;
    w.runner.script = () => ({ kind: "needs_input", question: "Which bucket?" });
    const asks = ok(w.runtime.enqueue(toBot("ops", root.id)));
    w.runtime.tick(); await flush();
    expect(w.store.get(asks.id)).toMatchObject({ status: "needs_input", question: "Which bucket?" });
    expect(w.runner.parks).toEqual([{ a: expect.objectContaining({ id: asks.id }), question: "Which bucket?" }]);
    expect(w.store.inbox({ threadId: "chief-chat" }).map(i => [i.assignmentId, i.kind])).toEqual([[stuck.id, "settled"], [asks.id, "needs_input"]]);
    w.clock.advance(4_999);
    w.runtime.tick();
    expect(w.store.get(asks.id)?.status).toBe("needs_input");
    w.clock.advance(2);
    w.runtime.tick();
    expect(w.store.get(asks.id)).toMatchObject({ status: "failed", outcome: "expired", outcomeDetail: "the person did not answer" });
    expect(w.runner.reports.map(r => r.a.id)).toEqual([stuck.id, asks.id]);
    w.runtime.end(root.id, { ok: true, text: "" });
    w.runtime.tick(); await flush();
    invariants(w);
  });

  it("answers a parked question into the next turn and delivers what settled meanwhile", async () => {
    const w = world();
    let helperId = "";
    w.runner.script = a => {
      if (a.assignee.botId !== "lead" || a.executions > 1) return undefined;
      helperId = ok(w.runtime.enqueue({ ...toBot("helper", a.id), requester: { kind: "bot_turn", botId: "lead", threadId: "lead-work" } })).id;
      return { kind: "needs_input", question: "Prod or staging?" };
    };
    const root = w.runtime.begin(beginReq());
    const lead = ok(w.runtime.enqueue(toBot("lead", root.id)));
    w.runtime.tick(); await flush();
    expect(w.store.get(lead.id)?.status).toBe("needs_input");
    expect(w.runtime.answer("nope", "x")).toBe(false);
    w.runtime.tick(); await flush();
    expect(w.store.get(helperId)?.status).toBe("completed");
    expect(w.store.get(lead.id)?.status).toBe("needs_input");
    expect(w.runtime.answer(lead.id, "Staging")).toBe(true);
    expect(w.store.get(lead.id)).toMatchObject({ status: "queued", answer: "Staging" });
    expect(w.store.get(lead.id)?.question).toBeUndefined();
    w.runtime.tick();
    const turn = w.runner.runs.find(r => r.a.id === lead.id && r.input.answer === "Staging")!;
    expect(turn.input.envelope).toContain("Staging");
    expect(turn.input.settled?.map(s => s.id)).toEqual([helperId]);
    expect(w.store.inbox({ botId: "lead", threadId: "lead-work" }).map(i => i.deliveredAt !== undefined)).toEqual([true]);
    await flush();
    expect(w.store.get(lead.id)).toMatchObject({ status: "completed", executions: 2 });
    expect(w.store.get(lead.id)?.answer).toBeUndefined();
    invariants(w);
  });

  it("masks a held secret, even one with spaces, before an over-long answer is clipped", async () => {
    const secret = "alpha bravo charlie";
    const removeScrubber = addHeldSecretScrubber((text) => text.replaceAll(secret, HELD_SECRET_MASK));
    try {
      const w = world();
      w.runner.script = a => a.assignee.botId === "lead" && a.executions === 1 ? { kind: "needs_input", question: "Done?" } : undefined;
      const root = w.runtime.begin(beginReq());
      const lead = ok(w.runtime.enqueue(toBot("lead", root.id)));
      w.runtime.tick(); await flush();
      const leadIn = "x".repeat(11_990);
      expect(w.runtime.answer(lead.id, `${leadIn}${secret} and more`)).toBe(true);
      const stored = w.store.get(lead.id)?.answer ?? "";
      expect(stored.length).toBeLessThanOrEqual(12_000);
      expect(stored).not.toContain("alpha");
      expect(stored).toContain(HELD_SECRET_MASK);
      expect(w.runtime.answer(lead.id, "short")).toBe(false);
    } finally {
      removeScrubber();
    }
  });

  it("stalls a turn with no activity on the monotonic clock, and a tree with no progress, but never a parked tree", async () => {
    const w = world(() => "hold", { turnStallMs: 1_000, stallMs: 10_000 });
    const root = w.runtime.begin(beginReq());
    const a = ok(w.runtime.enqueue(toBot("a", root.id)));
    w.runtime.tick();
    const signal = w.runner.runs[0]!.signal;
    w.clock.advance(900);
    w.runtime.progress(a.id);
    w.clock.advance(900);
    w.runtime.tick();
    expect(w.store.get(a.id)?.status).toBe("running");
    w.clock.advance(200);
    w.runtime.tick();
    expect(w.store.get(a.id)).toMatchObject({ status: "failed", outcome: "stalled" });
    expect(signal.aborted).toBe(true);
    expect(w.store.inbox({ threadId: "chief-chat" }).map(i => i.kind)).toEqual(["stalled"]);
    await w.runner.release(a.id, { kind: "completed", text: "too late" });
    expect(w.store.get(a.id)?.result).not.toBe("too late");
    w.runner.script = n => n.assignee.botId === "b" ? { kind: "needs_input", question: "?" } : undefined;
    const b = ok(w.runtime.enqueue(toBot("b", root.id)));
    w.runtime.tick(); await flush();
    w.runtime.end(root.id, { ok: true, text: "" });
    w.clock.advance(10_001);
    w.runtime.tick(); await flush();
    expect(w.store.get(root.id)?.status).toBe("waiting");
    expect(w.runner.runs.filter(r => r.a.id === root.id).map(r => r.input.asked?.map(c => c.id))).toEqual([[b.id]]);
    w.runtime.answer(b.id, "yes");
    w.runner.canStartAnswer = false;
    w.clock.advance(10_001);
    w.runtime.tick();
    expect(w.store.get(root.id)).toMatchObject({ status: "failed", outcome: "stalled" });
    expect(w.store.get(b.id)).toMatchObject({ status: "failed", outcome: "stalled" });
    expect(w.store.events(root.id).some(e => e.kind === "stalled")).toBe(true);
    expect(w.runner.reports.map(r => r.a.id)).toEqual([a.id, b.id, root.id]);
    invariants(w);
  });

  it("re-stamps a running node's lastActivityAt on real activity, on a coarse cadence and without event rows", () => {
    const w = world(() => "hold");
    const root = w.runtime.begin(beginReq());
    const a = ok(w.runtime.enqueue(toBot("a", root.id)));
    w.runtime.tick();
    const dispatched = w.store.get(a.id)!.lastActivityAt;
    const rootDispatched = w.store.get(root.id)!.lastActivityAt;
    const eventsBefore = w.store.events(a.id).length;
    // Inside the cadence the stall windows still move, but the row keeps its dispatch stamp.
    w.clock.advance(PROGRESS_WRITE_MS - 1);
    w.runtime.progress(a.id);
    expect(w.store.get(a.id)!.lastActivityAt).toBe(dispatched);
    // Past it, one progress call durably re-stamps the node and its root.
    w.clock.advance(1);
    w.runtime.progress(a.id);
    const stamped = w.store.get(a.id)!.lastActivityAt;
    expect(stamped).toBe(w.clock.t);
    expect(stamped).toBeGreaterThan(rootDispatched);
    expect(w.store.get(root.id)!.lastActivityAt).toBe(stamped);
    // The next event inside the new window leaves the row alone, and none of this wrote event rows.
    w.runtime.progress(a.id);
    expect(w.store.get(a.id)!.lastActivityAt).toBe(stamped);
    expect(w.store.events(a.id).length).toBe(eventsBefore);
    invariants(w);
  });

  it("writes the root's activity stamp at most once per cadence, however many children report", () => {
    const w = world(() => "hold");
    const root = w.runtime.begin(beginReq());
    const a = ok(w.runtime.enqueue(toBot("a", root.id)));
    w.runtime.tick();
    const b = ok(w.runtime.enqueue(toBot("b", root.id)));
    w.runtime.tick();
    // Both dispatched at T. a reports at T+10s: the node and the root are stamped.
    w.clock.advance(PROGRESS_WRITE_MS);
    w.runtime.progress(a.id);
    const rootStamp = w.store.get(root.id)!.lastActivityAt;
    expect(rootStamp).toBe(w.clock.t);
    // b reports at T+11s: its own row is still due, but the root is not — it stays at T+10s.
    w.clock.advance(1_000);
    w.runtime.progress(b.id);
    expect(w.store.get(b.id)!.lastActivityAt).toBe(w.clock.t);
    expect(w.store.get(root.id)!.lastActivityAt).toBe(rootStamp);
    // Once the root's window has passed, the next report moves it again.
    w.clock.advance(PROGRESS_WRITE_MS);
    w.runtime.progress(b.id);
    expect(w.store.get(root.id)!.lastActivityAt).toBe(w.clock.t);
    invariants(w);
  });

  it("reconciles a restart from stored data: rerun once, cut after handing out work, re-park, fail a rerun cut again", async () => {
    const store = openRuntimeStore(":memory:");
    const first = world(() => "hold", {}, store);
    const root = first.runtime.begin(beginReq());
    const solo = ok(first.runtime.enqueue(toBot("solo", root.id)));
    const lead = ok(first.runtime.enqueue(toBot("lead", root.id)));
    const asker = ok(first.runtime.enqueue(toBot("asker", root.id)));
    first.runtime.tick();
    expect(first.runner.runs).toHaveLength(3);
    const helper = ok(first.runtime.enqueue({ ...toBot("helper", lead.id), requester: { kind: "bot_turn", botId: "lead", threadId: "lead-work" } }));
    await first.runner.release(asker.id, { kind: "needs_input", question: "Region?" });
    first.runtime.close();
    first.runtime.tick();
    await first.runner.release(lead.id, { kind: "failed", outcome: "provider_error", text: "killed" });
    expect(store.get(lead.id)?.status).toBe("running");

    const second = world(undefined, {}, store);
    expect(store.get(solo.id)).toMatchObject({ status: "queued", restart: "rerun", restarts: 1 });
    expect(store.get(lead.id)).toMatchObject({ status: "waiting", restart: "cut", restarts: 0 });
    expect(store.get(asker.id)?.status).toBe("needs_input");
    expect(second.runner.parks).toEqual([{ a: expect.objectContaining({ id: asker.id }), question: "Region?" }]);
    expect(store.get(root.id)).toMatchObject({ status: "waiting", restart: "cut", restarts: 0 });
    expect(store.get(helper.id)?.status).toBe("queued");
    expect(store.events(solo.id).at(-1)).toMatchObject({ kind: "restart_reconciled", detail: "queued: rerun 1" });
    expect(store.events(lead.id).at(-1)).toMatchObject({ kind: "restart_reconciled", detail: expect.stringMatching(/^waiting: cut/) });
    second.runtime.tick();
    const rerun = second.runner.runs.find(r => r.a.id === solo.id)!;
    expect(rerun.input.resumed).toBe(true);
    expect(rerun.input.restartNote).toMatch(/restarted while your previous turn/);
    await flush();
    expect(store.get(solo.id)?.status).toBe("completed");
    expect(store.get(solo.id)?.restart).toBeUndefined();
    second.runtime.tick(); await flush();
    expect(store.get(helper.id)?.status).toBe("completed");
    second.runtime.tick();
    const leadResume = second.runner.runs.find(r => r.a.id === lead.id)!;
    expect(leadResume.input.restartNote).toMatch(/restarted before your previous turn here finished/);
    expect(leadResume.input.settled?.map(s => s.id)).toEqual([helper.id]);
    await flush();
    second.runtime.answer(asker.id, "eu");
    second.runtime.tick(); await flush();
    second.runtime.tick();
    const [rootAsked, rootResume] = second.runner.runs.filter(r => r.a.id === root.id);
    expect(rootAsked!.input.asked?.map(s => s.id)).toEqual([asker.id]);
    expect(rootAsked!.input.restartNote).toBe("");
    expect(rootResume!.input.resumed).toBe(true);
    expect(rootResume!.input.asked).toBeUndefined();
    expect(rootResume!.input.settled?.map(s => s.id)).toEqual([solo.id, lead.id, asker.id]);
    expect(rootResume!.input.restartNote).toMatch(/restarted before your previous turn here finished.*A restart cut these teammates' turns: solo \(resumed once in place/);
    await flush();
    expect(store.get(root.id)?.status).toBe("completed");
    invariants(second);

    const third = world(() => "hold", {}, store);
    const root2 = third.runtime.begin(beginReq());
    const twice = ok(third.runtime.enqueue(toBot("twice", root2.id)));
    third.runtime.tick();
    third.runtime.close();
    const fourth = world(() => "hold", {}, store);
    expect(store.get(twice.id)).toMatchObject({ status: "queued", restarts: 1 });
    fourth.runtime.tick();
    fourth.runtime.close();
    const fifth = world(undefined, {}, store);
    expect(store.get(twice.id)).toMatchObject({ status: "failed", outcome: "interrupted", restarts: 1, result: CUT_TWICE });
    expect(fifth.runner.reports.map(r => r.a.id)).toEqual([twice.id]);
    expect(store.get(root2.id)).toMatchObject({ status: "waiting", restart: "cut", restarts: 0 });
    fifth.runtime.tick();
    expect(fifth.runner.runs[0]!.input.restartNote).toMatch(/cut again while resuming/);
    await flush();
    expect(store.get(root2.id)?.status).toBe("completed");
    invariants(fifth);
    store.close();
  });

  it("records an expired question's reason on the node that asked it, never on the work under it", async () => {
    const w = world(() => "hold", { inputWaitMs: 5_000 });
    const root = w.runtime.begin(beginReq());
    const lead = ok(w.runtime.enqueue(toBot("lead", root.id)));
    w.runtime.tick();
    const helper = ok(w.runtime.enqueue({ ...toBot("helper", lead.id), requester: { kind: "bot_turn", botId: "lead", threadId: "lead-work" } }));
    w.runtime.tick();
    await w.runner.release(lead.id, { kind: "needs_input", question: "Which bucket?" });
    expect(w.store.get(lead.id)?.status).toBe("needs_input");
    w.clock.advance(5_001);
    w.runtime.tick();
    expect(w.store.get(lead.id)).toMatchObject({ status: "failed", outcome: "expired", outcomeDetail: "the person did not answer" });
    expect(w.store.get(helper.id)).toMatchObject({ status: "failed", outcome: "expired", outcomeDetail: "Stopped with the request it was for" });
    expect(w.runner.reports.filter(r => r.a.outcomeDetail === "the person did not answer").map(r => r.a.id)).toEqual([lead.id]);
  });

  it("charges the request budget once per fan-out, and still refuses the next call", () => {
    const w = world(() => "hold", { requests: 1 });
    const root = w.runtime.begin(beginReq());
    ok(w.runtime.enqueue({ ...toBot("builder", root.id), fanOut: "call-1" }));
    ok(w.runtime.enqueue({ ...toBot("tester", root.id), fanOut: "call-1" }));
    expect(w.store.get(root.id)?.budgets?.requests).toBe(1);
    expect(w.store.children(root.id).map(c => c.fanOut)).toEqual(["call-1", "call-1"]);
    expect(w.runtime.enqueue({ ...toBot("scout", root.id), fanOut: "call-2" })).toMatchObject({ ok: false, outcome: "budget" });
    expect(w.runtime.enqueue(toBot("scout", root.id))).toMatchObject({ ok: false, outcome: "budget" });
  });

  it("cancels down the tree, aborts running turns and reports each stop", async () => {
    const w = world(() => "hold");
    const root = w.runtime.begin(beginReq());
    const lead = ok(w.runtime.enqueue(toBot("lead", root.id)));
    w.runtime.tick();
    const helper = ok(w.runtime.enqueue({ ...toBot("helper", lead.id), requester: { kind: "bot_turn", botId: "lead", threadId: "lead-work" } }));
    w.runtime.tick();
    const signals = w.runner.runs.map(r => r.signal);
    w.runtime.cancel(lead.id);
    expect(w.store.get(helper.id)).toMatchObject({ status: "cancelled", outcome: "stopped", outcomeDetail: "Stopped with the request it was for" });
    expect(w.store.get(lead.id)).toMatchObject({ status: "cancelled", outcome: "stopped", outcomeDetail: "Stopped by user" });
    expect(signals.every(s => s.aborted)).toBe(true);
    // The stopped node reports first, so the report of what it handed on finds whose stop it was up the line.
    expect(w.runner.reports.map(r => r.a.id)).toEqual([lead.id, helper.id]);
    expect(w.runtime.liveEdges()).toEqual([]);
    const other = ok(w.runtime.enqueue(toBot("other", root.id)));
    w.runtime.cancel(other.id, "Provider reloaded", "failed");
    expect(w.store.get(other.id)).toMatchObject({ status: "failed", outcome: "interrupted", outcomeDetail: "Provider reloaded" });
    w.runtime.cancel(root.id);
    expect(w.store.get(root.id)?.status).toBe("cancelled");
    expect(w.runner.reports.at(-1)?.a.id).toBe(root.id);
    invariants(w);
  });

  it("stops awaiting, and stop means stop: queued work is dropped and a teammate mid-turn is stopped too", async () => {
    const w = world(() => "hold");
    const root = w.runtime.begin(beginReq());
    const running = ok(w.runtime.enqueue(toBot("running", root.id)));
    w.runtime.tick();
    w.runner.canStartAnswer = false;
    const queued = ok(w.runtime.enqueue(toBot("queued", root.id)));
    w.runtime.tick();
    w.runtime.stopRequester({ threadId: "chief-chat" });
    expect(w.store.get(queued.id)).toMatchObject({ status: "cancelled", outcomeDetail: "Stopped before it started" });
    expect(w.store.get(running.id)).toMatchObject({ status: "cancelled", outcome: "stopped", outcomeDetail: "Stopped with the request it was for" });
    expect(w.store.get(root.id)).toMatchObject({ status: "cancelled", outcome: "stopped" });
    expect(w.runner.runs[0]!.signal.aborted).toBe(true);
    await w.runner.release(running.id, { kind: "completed", text: "finished anyway" });
    expect(w.store.get(running.id)).toMatchObject({ status: "cancelled", result: "Stopped with the request it was for" });
    expect(w.runner.reports.map(r => r.a.id).sort()).toEqual([queued.id, running.id, root.id].sort());
    w.runtime.tick();
    expect(w.store.get(root.id)?.status).toBe("cancelled");
    expect(w.runner.runs).toHaveLength(1);
    expect(w.store.all({ nonTerminal: true })).toEqual([]);
    invariants(w);
  });

  it("takes no turn for a teammate's reply that only acknowledged, and keeps it in the report", async () => {
    const w = world(a => a.assignee.botId === "builder" ? { kind: "completed", text: "Noted.", statusOnly: true } : undefined);
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.end(root.id, { ok: true, text: "I told the builder." });
    w.runtime.tick(); await flush(); w.runtime.tick();
    expect(w.runner.runs.map(r => r.a.id)).toEqual([child.id]);
    expect(w.store.get(root.id)).toMatchObject({ status: "completed", outcome: "done", result: "I told the builder.", executions: 1 });
    expect(w.runner.reports.map(r => r.a.id)).toEqual([child.id, root.id]);
    invariants(w);
  });

  it("wakes the requester for a reply nobody marked, or a failure even if marked", async () => {
    for (const result of [{ kind: "completed", text: "The host is imap.example.com." }, { kind: "failed", outcome: "provider_error", text: "It broke." }] as RunResult[]) {
      const w = world(a => a.assignee.botId === "builder" ? result : undefined);
      const root = w.runtime.begin(beginReq());
      const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
      w.runtime.end(root.id, { ok: true, text: "I asked." });
      w.runtime.tick(); await flush(); w.runtime.tick();
      expect(w.runner.runs.map(r => r.a.id)).toEqual([child.id, root.id]);
      expect(w.runner.runs[1]!.input.resumed).toBe(true);
    }
  });

  it("wakes the requester when any new result needs it, and it reads the marked one too", async () => {
    const w = world(a => a.assignee.botId === "builder" ? { kind: "completed", text: "Noted.", statusOnly: true } : a.assignee.botId === "tester" ? { kind: "completed", text: "Two tests fail." } : undefined);
    const root = w.runtime.begin(beginReq());
    const noted = ok(w.runtime.enqueue(toBot("builder", root.id)));
    const real = ok(w.runtime.enqueue(toBot("tester", root.id)));
    w.runtime.end(root.id, { ok: true, text: "Asked both." });
    w.runtime.tick(); await flush(); w.runtime.tick();
    const resume = w.runner.runs.find(r => r.a.id === root.id)!;
    expect(resume.input.resumed).toBe(true);
    expect(resume.input.settled?.map(s => s.id).sort()).toEqual([noted.id, real.id].sort());
  });

  it("wakes a requester that owes an answer: an assignment under someone, or a routine's run", async () => {
    let leadTurns = 0;
    const lead = world(a => {
      if (a.assignee.botId === "builder") return { kind: "completed", text: "Noted.", statusOnly: true };
      return a.assignee.botId === "lead" && ++leadTurns === 1 ? "hold" : undefined;
    });
    const root = lead.runtime.begin(beginReq());
    const middle = ok(lead.runtime.enqueue(toBot("lead", root.id)));
    lead.runtime.end(root.id, { ok: true, text: "Asked the lead." });
    lead.runtime.tick();
    const below = ok(lead.runtime.enqueue(toBot("builder", middle.id, "FYI", { requester: { kind: "bot_turn", botId: "lead", threadId: "lead-work" } })));
    await lead.runner.release(middle.id, { kind: "completed", text: "Told the builder." });
    lead.runtime.tick(); await flush(); lead.runtime.tick();
    expect(lead.runner.runs.map(r => r.a.id).slice(0, 3)).toEqual([middle.id, below.id, middle.id]);

    const routine = world(a => a.assignee.botId === "builder" ? { kind: "completed", text: "Noted.", statusOnly: true } : undefined);
    const run = routine.runtime.begin(beginReq({ requester: { kind: "routine_run", runId: "run-1", botId: "chief", threadId: "chief-chat" }, origin: { kind: "routine", source: "routine" } }));
    const asked = ok(routine.runtime.enqueue(toBot("builder", run.id)));
    routine.runtime.end(run.id, { ok: true, text: "Asked." });
    routine.runtime.tick(); await flush(); routine.runtime.tick();
    expect(routine.runner.runs.map(r => r.a.id)).toEqual([asked.id, run.id]);
  });

  it("takes no third turn for an acknowledgement of what its second turn said, though its first result was real", async () => {
    let thanked: Assignment | undefined;
    const w = world((a, input) => {
      if (a.assignee.botId === "tester") return { kind: "completed", text: "Two tests fail." };
      if (a.assignee.botId === "builder") return { kind: "completed", text: "Noted.", statusOnly: true };
      return input.resumed ? "hold" : undefined;
    });
    const root = w.runtime.begin(beginReq());
    ok(w.runtime.enqueue(toBot("tester", root.id)));
    w.runtime.end(root.id, { ok: true, text: "Asked the tester." });
    w.runtime.tick(); await flush(); w.runtime.tick();
    expect(w.runner.runs.at(-1)!.a.id).toBe(root.id);
    thanked = ok(w.runtime.enqueue(toBot("builder", root.id, "FYI: two tests fail.")));
    await w.runner.release(root.id, { kind: "completed", text: "Told the builder. Two tests fail." });
    w.runtime.tick(); await flush(); w.runtime.tick();
    expect(w.runner.runs.filter(r => r.a.id === root.id)).toHaveLength(1);
    expect(w.runner.runs.map(r => r.a.id)).toContain(thanked.id);
    expect(w.store.get(root.id)).toMatchObject({ status: "completed", result: "Told the builder. Two tests fail." });
  });

  it("completes a poll root without a turn and hands its children's lane to poll()", async () => {
    const w = world();
    const ext = { kind: "external_runtime" as const, runId: "run-1" };
    const root = w.runtime.begin(beginReq({ requester: ext, origin: { kind: "outside", source: "external_runtime" }, reply: "poll" }));
    const a = ok(w.runtime.enqueue(toBot("a", root.id, "Do A", { requester: ext, reply: "poll" })));
    const b = ok(w.runtime.enqueue(toBot("b", root.id, "Do B", { requester: ext, reply: "poll" })));
    expect([a.origin.kind, b.level, b.levelSource]).toEqual(["outside", "ask", "own"]);
    expect(w.runtime.outstanding({ runId: "run-1" }).map(x => x.id)).toEqual([root.id]);
    w.runtime.end(root.id, { ok: true, text: "started two" });
    w.runtime.tick(); await flush(); w.runtime.tick();
    expect(w.store.get(root.id)).toMatchObject({ status: "completed", outcome: "done" });
    expect(w.runner.runs.map(r => r.a.id).sort()).toEqual([a.id, b.id].sort());
    expect(w.runner.reports.map(r => r.a.id)).toEqual([a.id, b.id, root.id]);
    expect(w.runtime.outstanding({ runId: "run-1" })).toEqual([]);
    const polled = w.runtime.poll({ runId: "run-1" });
    expect(polled.items.map(i => [i.assignmentId, i.kind])).toEqual([[a.id, "settled"], [b.id, "settled"]]);
    expect(polled.assignments.map(x => x.id)).toEqual([a.id, b.id]);
    expect(w.runtime.poll({ runId: "run-1" }).items).toEqual([]);
    expect(w.runtime.status(root.id)?.inbox.map(i => i.deliveredAt !== undefined)).toEqual([true, true]);
    invariants(w);
  });

  it("reads a host that throws as not now, and reports exactly once after a report that failed", async () => {
    const w = world();
    const root = w.runtime.begin(beginReq());
    const a = ok(w.runtime.enqueue(toBot("a", root.id)));
    w.runner.canStartAnswer = () => { throw new Error("slots unavailable"); };
    w.runtime.tick();
    expect(w.store.get(a.id)?.status).toBe("queued");
    expect(w.logs.some(l => /canStart threw/.test(l))).toBe(true);
    w.runner.canStartAnswer = true;
    w.runner.reportThrows = 1;
    w.runtime.tick(); await flush();
    expect(w.store.get(a.id)?.status).toBe("completed");
    expect(w.store.get(a.id)?.reportedAt).toBeUndefined();
    expect(w.store.inbox({ threadId: "chief-chat" })).toEqual([]);
    w.runtime.tick();
    expect(w.runner.reports.map(r => r.a.id)).toEqual([a.id]);
    expect(w.store.get(a.id)?.reportedAt).toBeDefined();
    expect(w.store.inbox({ threadId: "chief-chat" })).toHaveLength(1);
    w.runtime.tick();
    expect(w.runner.reports).toHaveLength(1);
    w.runner.script = () => { throw new Error("engine gone"); };
    const b = ok(w.runtime.enqueue(toBot("b", root.id)));
    w.runtime.tick(); await flush();
    expect(w.store.get(b.id)).toMatchObject({ status: "failed", outcome: "provider_error", result: "Error: engine gone" });
    w.runtime.end(root.id, { ok: true, text: "" });
    w.runner.script = () => undefined;
    w.runtime.tick(); await flush();
    invariants(w);
  });

  it("never overlaps turns in one conversation, waits on a running same-room speaker, and batches change notices per tick", async () => {
    const w = world(() => "hold");
    const root = w.runtime.begin(beginReq());
    const sameThread = { botId: "builder", destination: { kind: "work_thread" as const, threadId: "builder-work" } };
    const one = ok(w.runtime.enqueue(toBot("builder", root.id, "One", { assignee: sameThread })));
    const two = ok(w.runtime.enqueue(toBot("builder", root.id, "Two", { assignee: sameThread })));
    const notice = ok(w.runtime.enqueue(toBot("ops", root.id, "FYI", { mode: "notify" })));
    expect(notice.reply).toBe("none");
    const before = w.runner.changes.length;
    w.runtime.tick();
    expect(w.runner.changes.length - before).toBe(1);
    expect(w.runner.runs.map(r => r.a.id)).toEqual([one.id, notice.id]);
    expect(w.store.events(two.id).filter(e => e.kind === "held").map(e => e.detail)).toEqual(["an earlier turn in its conversation is running"]);
    await w.runner.release(one.id, { kind: "completed", text: "one" });
    w.runtime.tick();
    expect(w.runner.runs.map(r => r.a.id)).toEqual([one.id, notice.id, two.id]);
    await w.runner.release(two.id, { kind: "completed", text: "two" });
    w.runtime.end(root.id, { ok: true, text: "" });
    w.runtime.tick();
    expect(w.store.get(root.id)?.status).toBe("running");
    expect(w.runner.runs.at(-1)?.input.settled?.map(s => s.id)).toEqual([one.id, two.id]);
    await w.runner.release(root.id, { kind: "completed", text: "synth" });
    expect(w.store.get(root.id)?.status).toBe("completed");
    await w.runner.release(notice.id, { kind: "completed", text: "noted" });
    expect(w.store.inbox({ botId: "chief", threadId: "chief-chat" }).map(i => i.assignmentId)).toEqual([one.id, two.id]);
    invariants(w);
  });
});

describe("Runtime: host-named roots and request marks", () => {
  it("begins a root under the id the host names, or a fresh uuid", () => {
    const w = world();
    const named = w.runtime.begin(beginReq({ id: "gen-1" }));
    expect(named).toMatchObject({ id: "gen-1", rootId: "gen-1", status: "running" });
    expect(w.store.get("gen-1")?.id).toBe("gen-1");
    const fresh = w.runtime.begin(beginReq());
    expect(fresh.id).toMatch(/^[0-9a-f-]{36}$/);
    expect(fresh.rootId).toBe(fresh.id);
  });

  it("copies approved and batchKey onto the row and keeps them through the store", async () => {
    const w = world();
    const root = w.runtime.begin(beginReq({ id: "gen-2" }));
    const marked = ok(w.runtime.enqueue(toBot("builder", root.id, undefined, { approved: true, batchKey: "batch-1" })));
    expect(marked).toMatchObject({ approved: true, batchKey: "batch-1" });
    expect(w.store.get(marked.id)).toMatchObject({ approved: true, batchKey: "batch-1" });
    const plain = ok(w.runtime.enqueue(toBot("tester", root.id)));
    expect(plain.approved).toBeUndefined();
    expect(plain.batchKey).toBeUndefined();
    expect("approved" in w.store.get(plain.id)!).toBe(false);
    w.runtime.tick(); await flush();
    expect(w.store.get(marked.id)).toMatchObject({ status: "completed", approved: true, batchKey: "batch-1" });
    invariants(w);
  });

  it("ends a root the host's watchdog stopped as stalled, any other failed root as a provider error", () => {
    const w = world();
    w.runtime.begin(beginReq({ id: "gen-3" }));
    w.runtime.end("gen-3", { ok: false, text: "No activity for 20 minutes — the turn was stopped.", stalled: true });
    expect(w.store.get("gen-3")).toMatchObject({ status: "failed", outcome: "stalled" });
    w.runtime.begin(beginReq({ id: "gen-4" }));
    w.runtime.end("gen-4", { ok: false, text: "The engine exited" });
    expect(w.store.get("gen-4")).toMatchObject({ status: "failed", outcome: "provider_error" });
    invariants(w);
  });
});

describe("access rechecked while work runs", () => {
  it("fails a running child whose route closes, drops its words, and the resume carries only the reason", async () => {
    const w = world(() => "hold");
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick();
    expect(w.store.get(child.id)?.status).toBe("running");
    const signal = w.runner.runs[0]!.signal;
    w.runner.validate = a => a.id === child.id ? "Destination room setup is unfinished" : undefined;
    w.runtime.tick();
    expect(w.store.get(child.id)).toMatchObject({ status: "failed", outcome: "refused", result: "Destination room setup is unfinished" });
    expect(signal.aborted).toBe(true);
    await w.runner.release(child.id, { kind: "completed", text: "PRIVATE_RESULT" });
    expect(w.store.get(child.id)?.result).not.toContain("PRIVATE");
    w.runtime.end(root.id, { ok: true, text: "handed out" });
    w.runtime.tick();
    const resume = w.runner.runs.find(r => r.a.id === root.id && r.input.resumed);
    expect(resume?.input.settled?.map(s => s.result)).toEqual(["Destination room setup is unfinished"]);
    invariants(w);
  });

  it("drops a reply that lands after the route closed, before any tick could notice", async () => {
    const w = world(() => "hold");
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick();
    w.runner.validate = a => a.id === child.id ? "The recipient is not an allowed peer of the sender" : undefined;
    await w.runner.release(child.id, { kind: "completed", text: "PRIVATE_RESULT" });
    expect(w.store.get(child.id)).toMatchObject({ status: "failed", outcome: "refused", result: "The recipient is not an allowed peer of the sender" });
    expect(w.runner.reports.map(r => r.a.id)).toEqual([child.id]);
    invariants(w);
  });
});

describe("a resume turn a restart cut", () => {
  it("runs again with the same settled results, as a rerun, not as a fresh resume", async () => {
    const store = openRuntimeStore(":memory:");
    const first = world(() => "hold", {}, store);
    const root = first.runtime.begin(beginReq());
    const child = ok(first.runtime.enqueue(toBot("builder", root.id)));
    first.runtime.tick();
    await first.runner.release(child.id, { kind: "completed", text: "built" });
    first.runtime.end(root.id, { ok: true, text: "handed out" });
    first.runtime.tick();
    const resume = first.runner.runs.find(r => r.a.id === root.id)!;
    expect(resume.input.turn).toBe("resume");
    expect(resume.input.settled?.map(s => s.id)).toEqual([child.id]);
    first.runtime.close();
    const second = world(() => "hold", {}, store);
    expect(store.get(root.id)).toMatchObject({ status: "resume", restart: "rerun", restarts: 1 });
    second.runtime.tick();
    const rerun = second.runner.runs.find(r => r.a.id === root.id)!;
    expect(rerun.input.turn).toBe("resume");
    expect(rerun.input.settled?.map(s => s.id)).toEqual([child.id]);
    expect(rerun.input.restartNote).toContain("this turn continues it");
    await second.runner.release(root.id, { kind: "completed", text: "reviewed" });
    expect(store.get(root.id)).toMatchObject({ status: "completed", executions: 3 });
    invariants(second);
  });
});

describe("Runtime: what the host relies on for the new modes and openers", () => {
  it("status(root) carries its children's inbox items in the resume lane too; status(child) shows its own", async () => {
    const w = world();
    const root = w.runtime.begin(beginReq());
    const a = ok(w.runtime.enqueue(toBot("a", root.id)));
    const b = ok(w.runtime.enqueue(toBot("b", root.id)));
    w.runtime.tick(); await flush();
    expect(w.runtime.status(root.id)?.inbox.map(i => [i.assignmentId, i.kind, i.deliveredAt])).toEqual([[a.id, "settled", undefined], [b.id, "settled", undefined]]);
    expect(w.runtime.status(root.id)?.children.map(c => c.id)).toEqual([a.id, b.id]);
    expect(w.runtime.status(a.id)).toMatchObject({ assignment: { id: a.id, status: "completed" }, children: [], inbox: [expect.objectContaining({ assignmentId: a.id })] });
    expect(w.runtime.status("nope")).toBeUndefined();
    w.runtime.end(root.id, { ok: true, text: "" });
    w.runtime.tick(); await flush();
    invariants(w);
  });

  it("poll and stopRequester work by run id alone, for a root whose requester has nothing else", async () => {
    const w = world(() => "hold");
    const ext = { kind: "external_runtime" as const, runId: "run-9" };
    const root = w.runtime.begin(beginReq({ requester: ext, origin: { kind: "outside", source: "external_runtime" }, reply: "poll" }));
    const a = ok(w.runtime.enqueue(toBot("a", root.id, "Do A", { requester: { kind: "bot_turn", botId: "chief", threadId: "chief-chat", runId: "run-9" }, reply: "poll" })));
    const b = ok(w.runtime.enqueue(toBot("b", root.id, "Do B", { requester: { kind: "bot_turn", botId: "chief", threadId: "chief-chat", runId: "run-9" }, reply: "poll" })));
    w.runner.canStartAnswer = () => w.runner.runs.length === 0;
    w.runtime.tick();
    expect(w.runner.runs.map(r => r.a.id)).toEqual([a.id]);
    await w.runner.release(a.id, { kind: "completed", text: "A done" });
    expect(w.runtime.poll({ runId: "run-9" }).items.map(i => i.assignmentId)).toEqual([a.id]);
    expect(w.runtime.poll({ runId: "run-9" }).items).toEqual([]);
    expect(w.store.inbox({ threadId: "chief-chat" }).map(i => i.assignmentId)).toEqual([a.id]);
    expect(w.runtime.outstanding({ runId: "run-9" }).map(x => x.id)).toEqual([root.id]);
    w.runtime.stopRequester({ runId: "run-9" });
    expect(w.store.get(b.id)).toMatchObject({ status: "cancelled", outcomeDetail: "Stopped before it started" });
    expect(w.store.get(root.id)).toMatchObject({ status: "cancelled", outcome: "stopped" });
    expect(w.runtime.outstanding({ runId: "run-9" })).toEqual([]);
    invariants(w);
  });

  it("a notify child never holds its parent: waiting → resume → completed while the notice still runs, and it writes no inbox item", async () => {
    const w = world(a => a.mode === "notify" ? "hold" : undefined);
    const root = w.runtime.begin(beginReq());
    w.runner.canStartAnswer = false;
    const work = ok(w.runtime.enqueue(toBot("builder", root.id)));
    const notice = ok(w.runtime.enqueue(toBot("ops", root.id, "FYI", { mode: "notify" })));
    expect(notice).toMatchObject({ mode: "notify", reply: "none" });
    expect(w.runtime.outstanding({ threadId: "chief-chat" }).map(x => x.id)).toEqual([work.id]);
    w.runtime.end(root.id, { ok: true, text: "handed out" });
    expect(w.store.get(root.id)?.status).toBe("waiting");
    w.runner.canStartAnswer = true;
    w.runtime.tick(); await flush();
    expect(w.store.get(work.id)?.status).toBe("completed");
    expect(w.store.get(notice.id)?.status).toBe("running");
    w.runtime.tick();
    expect(w.store.get(root.id)).toMatchObject({ status: "running", executions: 2 });
    expect(w.runner.runs.at(-1)?.input.settled?.map(s => s.id)).toEqual([work.id]);
    await flush();
    expect(w.store.get(root.id)?.status).toBe("completed");
    await w.runner.release(notice.id, { kind: "completed", text: "noted" });
    expect(w.store.get(notice.id)).toMatchObject({ status: "completed", reportedAt: expect.any(Number) });
    expect(w.store.inbox({ threadId: "chief-chat" }).map(i => i.assignmentId)).toEqual([work.id]);
    expect(w.store.events(notice.id).some(e => e.kind === "woken")).toBe(false);
    invariants(w);
  });

  it("an external poll root whose only children are notices completes at end()", async () => {
    const w = world(() => "hold");
    const ext = { kind: "external_runtime" as const, runId: "run-3" };
    const root = w.runtime.begin(beginReq({ requester: ext, origin: { kind: "outside", source: "external_runtime" }, reply: "poll" }));
    const notice = ok(w.runtime.enqueue(toBot("ops", root.id, "FYI", { mode: "notify", requester: { kind: "bot_turn", botId: "chief", threadId: "chief-chat", runId: "run-3" } })));
    expect(notice.reply).toBe("none");
    w.runtime.end(root.id, { ok: true, text: "sent a notice" });
    expect(w.store.get(root.id)).toMatchObject({ status: "completed", outcome: "done", reportedAt: expect.any(Number) });
    expect(w.runtime.outstanding({ runId: "run-3" })).toEqual([]);
    w.runtime.tick();
    expect(w.store.get(notice.id)?.status).toBe("running");
    await w.runner.release(notice.id, { kind: "completed", text: "noted" });
    expect(w.runtime.poll({ runId: "run-3" }).items).toEqual([]);
    invariants(w);
  });

  it("the same words as work and as a consultation are two requests", async () => {
    const w = world();
    const root = w.runtime.begin(beginReq());
    const work = ok(w.runtime.enqueue(toBot("builder", root.id, "Is the export done?")));
    const consult = ok(w.runtime.enqueue(toBot("builder", root.id, "Is the export done?", { mode: "consult" })));
    expect(consult.id).not.toBe(work.id);
    expect(consult).toMatchObject({ mode: "consult", reply: "resume" });
    expect(w.runtime.enqueue(toBot("builder", root.id, "Is the export done?", { mode: "consult" }))).toMatchObject({ ok: true, duplicate: true, assignment: { id: consult.id } });
    w.runtime.tick(); await flush(); w.runtime.tick(); await flush();
    w.runtime.end(root.id, { ok: true, text: "" });
    w.runtime.tick(); await flush();
    invariants(w);
  });
});

describe("Runtime: a root's later executions (every turn a root)", () => {
  const cards = { question: "Connect GitHub", cards: ["msg-1", "msg-2"] };

  it("end() with needsInput parks the root with its cards through the store; answer() queues the answered turn; dispatch clears the cards", async () => {
    const w = world();
    const root = w.runtime.begin(beginReq());
    w.runtime.end(root.id, { ok: true, text: "Connect it.", needsInput: cards });
    expect(w.store.get(root.id)).toMatchObject({ status: "needs_input", question: "Connect GitHub", cards: ["msg-1", "msg-2"], executions: 1 });
    expect(w.runner.parks).toEqual([{ a: expect.objectContaining({ id: root.id, cards: ["msg-1", "msg-2"] }), question: "Connect GitHub" }]);
    expect(w.store.inbox({ threadId: "chief-chat" })).toEqual([]);
    w.runtime.tick();
    expect(w.runner.runs).toHaveLength(0);
    expect(w.runtime.answer(root.id, "The user connected GitHub.")).toBe(true);
    expect(w.store.get(root.id)).toMatchObject({ status: "queued", answer: "The user connected GitHub.", cards: ["msg-1", "msg-2"] });
    expect(w.store.get(root.id)?.question).toBeUndefined();
    w.runtime.tick();
    expect(w.runner.runs).toHaveLength(1);
    expect(w.runner.runs[0]!.input).toMatchObject({ turn: "answered", resumed: true, restartNote: "", answer: "The user connected GitHub.", level: "auto", mode: "work" });
    expect(w.runner.runs[0]!.a.cards).toBeUndefined();
    expect(w.store.get(root.id)?.cards).toBeUndefined();
    await flush();
    expect(w.store.get(root.id)).toMatchObject({ status: "completed", result: "chief done", executions: 2 });
    expect(w.runner.reports).toEqual([]);
    expect(w.store.events(root.id).map(e => e.kind)).toEqual(["enqueued", "started", "parked", "answered", "admitted", "started", "settled", "reported"]);
    invariants(w);
  });

  it("end() with continue queues the continued turn; the count survives the dispatch that consumed the continuation and the budget is not renewed", async () => {
    const w = world((_a, input) => input.turn === "continued" && input.continuation?.note === "Cap 1."
      ? { kind: "continue", text: "second half", note: "Cap 2.", handoffPath: "/h/2.md" } : undefined, { executions: 10 });
    const root = w.runtime.begin(beginReq());
    expect(w.store.get(root.id)?.budgets?.executions).toBe(1);
    w.runtime.end(root.id, { ok: true, text: "first half", continue: { note: "Cap 1.", handoffPath: "/h/1.md" }, evidence: { commands: 2 } });
    expect(w.store.get(root.id)).toMatchObject({ status: "queued", result: "first half", evidence: { commands: 2 },
      continuation: { note: "Cap 1.", handoffPath: "/h/1.md", count: 1 }, budgets: { executions: 1 } });
    w.runtime.tick();
    expect(w.runner.runs[0]!.input).toMatchObject({ turn: "continued", resumed: true, continuation: { note: "Cap 1.", handoffPath: "/h/1.md" } });
    expect(w.runner.runs[0]!.a.continuation).toBeUndefined();
    expect(w.store.get(root.id)?.budgets?.executions).toBe(2);
    await flush();
    expect(w.store.get(root.id)).toMatchObject({ status: "queued", result: "second half", continuation: { note: "Cap 2.", handoffPath: "/h/2.md", count: 2 }, budgets: { executions: 2 } });
    w.runtime.tick();
    expect(w.runner.runs[1]!.input).toMatchObject({ turn: "continued", continuation: { note: "Cap 2.", handoffPath: "/h/2.md" } });
    await flush();
    expect(w.store.get(root.id)).toMatchObject({ status: "completed", executions: 3, budgets: { executions: 3 } });
    expect(w.store.events(root.id).filter(e => e.kind === "continued").map(e => e.detail)).toEqual(["queued: Cap 1.", "queued: Cap 2."]);
    invariants(w);
  });

  it("a host that ends a runtime-dispatched root turn through end() is heard once: the run() result that follows is ignored and never settles the next execution", async () => {
    const w = world(() => "hold");
    const root = w.runtime.begin(beginReq());
    w.runtime.end(root.id, { ok: true, text: "", needsInput: cards });
    w.runtime.answer(root.id, "connected");
    w.runtime.tick();
    expect(w.runner.held.has(root.id)).toBe(true);
    const first = w.runner.runs[0]!;
    w.runtime.end(root.id, { ok: true, text: "half", continue: { note: "Cap." } });
    expect(first.signal.aborted).toBe(false);
    expect(w.store.get(root.id)).toMatchObject({ status: "queued", executions: 2, continuation: { note: "Cap.", count: 1 } });
    const staleResolve = w.runner.held.get(root.id)!;
    w.runtime.tick();
    expect(w.runner.runs).toHaveLength(2);
    expect(w.store.get(root.id)).toMatchObject({ status: "running", executions: 3 });
    const second = w.runner.runs[1]!;
    staleResolve({ kind: "completed", text: "stale" });
    await flush();
    expect(w.store.get(root.id)).toMatchObject({ status: "running", executions: 3 });
    expect(second.signal.aborted).toBe(false);
    await w.runner.release(root.id, { kind: "completed", text: "fresh" });
    expect(w.store.get(root.id)).toMatchObject({ status: "completed", result: "fresh", executions: 3 });
    invariants(w);
  });

  it("end() is for roots only and never throws: a child, an unknown id and a settled root are logged and left as they are", async () => {
    const w = world(() => "hold");
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick();
    expect(w.store.get(child.id)?.status).toBe("running");
    expect(() => w.runtime.end(child.id, { ok: true, text: "done" })).not.toThrow();
    expect(w.store.get(child.id)?.status).toBe("running");
    expect(() => w.runtime.end("ghost", { ok: true, text: "" })).not.toThrow();
    w.runtime.end(root.id, { ok: true, text: "" });
    expect(w.store.get(root.id)?.status).toBe("waiting");
    expect(() => w.runtime.end(root.id, { ok: false, text: "twice" })).not.toThrow();
    expect(w.store.get(root.id)).toMatchObject({ status: "waiting", result: "" });
    expect(w.logs.filter(line => /not a running root/.test(line))).toHaveLength(3);
    invariants(w);
  });

  it("reminds the person once when handed-off work has waited, then closes it, across a restart and on the wall clock", async () => {
    const store = openRuntimeStore(":memory:");
    const w = world((a) => a.assignee.botId === "builder" ? { kind: "needs_input", question: "Region?" } : undefined, { inputRemindMs: 1_000, inputWaitMs: 3_000 }, store);
    const reminded: Array<{ id: string; closesAt: number }> = [];
    w.runner.remind = (a, closesAt) => { reminded.push({ id: a.id, closesAt }); };
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick(); await flush();
    w.runtime.end(root.id, { ok: true, text: "" });
    const parkedAt = w.store.get(child.id)!.lastActivityAt;
    w.clock.advance(1_000);
    w.runtime.tick();
    expect(reminded).toEqual([]);
    w.clock.advance(1);
    w.runtime.tick(); w.runtime.tick();
    expect(reminded).toEqual([{ id: child.id, closesAt: parkedAt + 3_001 }]);
    expect(w.store.events(child.id).filter(e => e.kind === "reminded")).toHaveLength(1);

    const again = new Runtime(store, w.runner, { clock: w.clock, limits: { inputRemindMs: 1_000, inputWaitMs: 3_000 }, log: () => {} });
    again.open();
    w.clock.advance(1_999);
    again.tick();
    expect(reminded).toHaveLength(1);
    expect(store.get(child.id)?.status).toBe("needs_input");
    w.clock.advance(2);
    again.tick();
    expect(store.get(child.id)).toMatchObject({ status: "failed", outcome: "expired", outcomeDetail: "the person did not answer" });
  });

  it("reminds late work first and still gives it the rest of the window after a long downtime", async () => {
    const w = world((a) => a.assignee.botId === "builder" ? { kind: "needs_input", question: "Region?" } : undefined, { inputRemindMs: 1_000, inputWaitMs: 3_000 });
    const reminded: number[] = [];
    w.runner.remind = (_a, closesAt) => { reminded.push(closesAt); };
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick(); await flush();
    w.runtime.end(root.id, { ok: true, text: "" });
    w.clock.advance(50_000);
    w.runtime.tick();
    expect(reminded).toEqual([w.clock.t + 2_000]);
    expect(w.store.get(child.id)?.status).toBe("needs_input");
    w.clock.advance(2_000);
    w.runtime.tick();
    expect(w.store.get(child.id)?.status).toBe("needs_input");
    w.clock.advance(1);
    w.runtime.tick();
    expect(w.store.get(child.id)).toMatchObject({ status: "failed", outcome: "expired" });
  });

  it("a newer question restarts the wait and its reminder; a question its requester decides reminds nobody", async () => {
    const asks: RunResult = { kind: "needs_input", question: "Region?" };
    const w = world((a) => a.assignee.botId === "builder" ? asks : undefined, { inputRemindMs: 1_000, inputWaitMs: 3_000 });
    const reminded: string[] = [];
    w.runner.remind = (a) => { reminded.push(a.id); };
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick(); await flush();
    w.runtime.end(root.id, { ok: true, text: "" });
    w.clock.advance(1_001);
    w.runtime.tick();
    expect(reminded).toEqual([child.id]);
    expect(w.runtime.answer(child.id, "eu-west")).toBe(true);
    w.runtime.tick(); await flush();
    expect(w.store.get(child.id)).toMatchObject({ status: "needs_input" });
    expect(w.store.get(child.id)?.remindedAt).toBeUndefined();
    w.clock.advance(1_001);
    w.runtime.tick();
    expect(reminded).toEqual([child.id, child.id]);

    const quiet = world((a) => a.assignee.botId === "builder" ? { kind: "needs_input", question: "Region?", decide: {} } : undefined, { inputRemindMs: 1_000, inputWaitMs: 3_000 });
    const told: string[] = [];
    quiet.runner.remind = (a) => { told.push(a.id); };
    const root2 = quiet.runtime.begin(beginReq());
    const decided = ok(quiet.runtime.enqueue(toBot("builder", root2.id)));
    quiet.runtime.tick(); await flush();
    expect(quiet.store.get(decided.id)?.decide).toEqual({});
    quiet.clock.advance(3_001);
    quiet.runtime.tick();
    expect(told).toEqual([]);
    expect(quiet.store.get(decided.id)).toMatchObject({ status: "failed", outcome: "expired" });
  });

  it("a root parked on cards never expires; a child's question does; a non-finite inputWaitMs never expires anything", async () => {
    const w = world((a) => a.assignee.botId === "builder" ? { kind: "needs_input", question: "Region?" } : undefined, { inputWaitMs: 1_000 });
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick(); await flush();
    w.runtime.end(root.id, { ok: true, text: "", needsInput: cards });
    expect(w.store.get(child.id)?.status).toBe("needs_input");
    w.clock.advance(30 * 24 * 60 * 60_000);
    w.runtime.tick();
    expect(w.store.get(child.id)).toMatchObject({ status: "failed", outcome: "expired" });
    expect(w.store.get(root.id)).toMatchObject({ status: "needs_input", cards: ["msg-1", "msg-2"] });
    invariants(w);

    const v = world((a) => a.assignee.botId === "builder" ? { kind: "needs_input", question: "Region?" } : undefined, { inputWaitMs: Number.POSITIVE_INFINITY });
    const root2 = v.runtime.begin(beginReq());
    const child2 = ok(v.runtime.enqueue(toBot("builder", root2.id)));
    v.runtime.tick(); await flush();
    v.runtime.end(root2.id, { ok: true, text: "" });
    v.clock.advance(400 * 24 * 60 * 60_000);
    v.runtime.tick(); await flush();
    expect(v.store.get(child2.id)?.status).toBe("needs_input");
    expect(v.store.get(root2.id)?.status).toBe("waiting");
    invariants(v);
  });

  it("open() re-parks a root on its cards without a new item, and reruns a cut root once before failing it interrupted", async () => {
    const store = openRuntimeStore(":memory:");
    const first = world(undefined, {}, store);
    const parked = first.runtime.begin(beginReq({ id: "root-parked" }));
    first.runtime.end(parked.id, { ok: true, text: "", needsInput: cards });
    const cut = first.runtime.begin(beginReq({ id: "root-cut", assignee: { botId: "chief", destination: { kind: "standing_thread", threadId: "other-chat" } } }));
    first.runtime.close();

    const second = world(() => "hold", {}, store);
    expect(second.runner.parks).toEqual([{ a: expect.objectContaining({ id: "root-parked", cards: ["msg-1", "msg-2"] }), question: "Connect GitHub" }]);
    expect(store.get("root-parked")).toMatchObject({ status: "needs_input", restarts: 0 });
    expect(store.get("root-cut")).toMatchObject({ status: "queued", restart: "rerun", restarts: 1, executions: 1 });
    expect(store.inbox({ threadId: "chief-chat" })).toEqual([]);
    second.runtime.tick();
    expect(second.runner.runs.map(r => r.a.id)).toEqual(["root-cut"]);
    expect(second.runner.runs[0]!.input).toMatchObject({ turn: "rerun", resumed: true, restartNote: expect.stringMatching(/^OpenMausBot restarted while your previous turn here was running/) });
    expect(store.get("root-cut")).toMatchObject({ status: "running", executions: 2 });
    second.runtime.close();

    const third = world(undefined, {}, store);
    expect(store.get("root-cut")).toMatchObject({ status: "failed", outcome: "interrupted", restarts: 1, result: CUT_TWICE });
    expect(store.get("root-cut")?.restart).toBeUndefined();
    expect(third.runner.reports.map(r => [r.a.id, r.parent])).toEqual([["root-cut", undefined]]);
    expect(store.get("root-parked")?.status).toBe("needs_input");
    expect(third.runner.parks.map(p => p.a.id)).toEqual(["root-parked"]);
    third.runtime.tick();
    expect(third.runner.runs).toEqual([]);
    invariants(third);
    expect(cut.id).toBe("root-cut");
    store.close();
  });

  it("prune() removes terminal trees older than before and leaves a parked root's tree alone", async () => {
    const w = world();
    const old = w.runtime.begin(beginReq({ id: "old" }));
    w.runtime.end(old.id, { ok: true, text: "done" });
    const parked = w.runtime.begin(beginReq({ id: "parked", assignee: { botId: "chief", destination: { kind: "standing_thread", threadId: "other-chat" } } }));
    w.runtime.end(parked.id, { ok: true, text: "", needsInput: cards });
    w.clock.advance(1_000);
    expect(w.runtime.prune(w.clock.t)).toBe(1);
    expect(w.store.get("old")).toBeUndefined();
    expect(w.store.get("parked")).toMatchObject({ status: "needs_input", cards: ["msg-1", "msg-2"] });
    expect(w.runtime.prune(w.clock.t)).toBe(0);
    invariants(w);
  });
});

describe("Runtime: a cut turn that already acted outward (contract §5)", () => {
  it("open() asks the host once per cut node, ends one that acted outward failed(interrupted) with what it did, and the requester's note names it", async () => {
    const store = openRuntimeStore(":memory:");
    const first = world(() => "hold", {}, store);
    const root = first.runtime.begin(beginReq());
    const sender = ok(first.runtime.enqueue(toBot("sender", root.id)));
    const quiet = ok(first.runtime.enqueue(toBot("quiet", root.id)));
    first.runtime.tick();
    expect(first.runner.runs).toHaveLength(2);
    first.runtime.end(root.id, { ok: true, text: "handed out" });
    first.runtime.close();

    const asked: string[] = [];
    const second = world(undefined, {}, store, { outward: a => { asked.push(a.assignee.botId); return a.assignee.botId === "sender" ? ["GMAIL_SEND_EMAIL"] : []; } });
    expect(asked.sort()).toEqual(["quiet", "sender"]);
    expect(store.get(sender.id)).toMatchObject({ status: "failed", outcome: "interrupted", restarts: 0, executions: 1,
      outcomeDetail: `${INTERRUPTED_OUTWARD} sent an email (GMAIL_SEND_EMAIL); not resumed, so nothing is done twice.` });
    expect(store.get(sender.id)?.restart).toBeUndefined();
    expect(store.events(sender.id).map(e => e.kind)).toEqual(["enqueued", "admitted", "started", "restart_reconciled", "settled", "reported", "woken"]);
    expect(store.events(sender.id).find(e => e.kind === "restart_reconciled")?.detail).toBe("failed: outward action done");
    expect(store.get(quiet.id)).toMatchObject({ status: "queued", restart: "rerun", restarts: 1 });
    expect(second.runner.reports.map(r => [r.a.id, r.parent?.id])).toEqual([[sender.id, root.id]]);
    expect(store.get(root.id)).toMatchObject({ status: "waiting", restart: "teammates" });
    invariants(second);

    second.runtime.tick(); await flush();
    expect(store.get(quiet.id)).toMatchObject({ status: "completed", restarts: 1 });
    second.runtime.tick();
    const resume = second.runner.runs.find(r => r.a.id === root.id)!;
    expect(resume.input.turn).toBe("resume");
    expect(resume.input.settled?.map(s => [s.id, s.outcome])).toEqual([[sender.id, "interrupted"], [quiet.id, "done"]]);
    expect(resume.input.restartNote).toBe("A restart cut these teammates' turns: sender (cut after an outward action, not resumed: see its result); quiet (resumed once in place and told to check what it had already done). Check their results as usual.\n");
    await flush();
    expect(store.get(root.id)?.status).toBe("completed");
    invariants(second);
    store.close();
  });

  it("a root cut during its own turn that acted outward ends alone, reported into its own conversation, and is never run", async () => {
    const store = openRuntimeStore(":memory:");
    const first = world(undefined, {}, store);
    const root = first.runtime.begin(beginReq());
    first.runtime.close();
    const second = world(undefined, {}, store, { outward: () => ["git push"] });
    expect(store.get(root.id)).toMatchObject({ status: "failed", outcome: "interrupted", restarts: 0, executions: 1, result: `${INTERRUPTED_OUTWARD} pushed (git push); not resumed, so nothing is done twice.` });
    expect(second.runner.reports).toEqual([{ a: expect.objectContaining({ id: root.id, outcome: "interrupted" }) }]);
    expect(store.inbox({ threadId: "chief-chat" })).toEqual([]);
    second.runtime.tick(); await flush();
    expect(second.runner.runs).toEqual([]);
    invariants(second);
    store.close();
  });
});

describe("tree.ts: the outward wording and the loop guard", () => {
  it("outwardDetail dedupes, trims and clips the labels, names at most four as verb phrases, and keeps the exact prefix and close", () => {
    expect(outwardDetail(["git push", "git push", "  GMAIL_SEND_EMAIL  "]))
      .toBe("Interrupted by an OpenMausBot restart after it already pushed (git push), sent an email (GMAIL_SEND_EMAIL); not resumed, so nothing is done twice.");
    expect(outwardDetail(["git commit", "STRIPE_CREATE_REFUND", "SLACK_SEND_MESSAGE", "TWITTER_CREATION_OF_A_POST", "COMPOSIO_PROXY_EXECUTE", "x"]))
      .toBe(`${INTERRUPTED_OUTWARD} committed (git commit), made a payment (STRIPE_CREATE_REFUND), sent a message (SLACK_SEND_MESSAGE), posted (TWITTER_CREATION_OF_A_POST) and 2 more; not resumed, so nothing is done twice.`);
    expect(outwardDetail(["COMPOSIO_PROXY_EXECUTE"])).toContain("completed an outward action (COMPOSIO_PROXY_EXECUTE)");
    const long = outwardDetail([`git push ${"origin/".repeat(20)}`, "", "   "]);
    expect(long).toMatch(/^Interrupted by an OpenMausBot restart after it already pushed \(git push [^)]{0,40}\); not resumed, so nothing is done twice\.$/);
    expect(long.length).toBeLessThan(200);
    expect(outwardDetail(["a\nb\tc"])).toContain("(a b c)");
  });

  it("nextGuard counts consecutive no-progress continuations, resets on progress, leaves unknown evidence alone, and keeps the last three errors clipped", () => {
    expect(nextGuard(undefined, undefined)).toEqual({ stale: 0, errors: [] });
    expect(nextGuard(undefined, { okToolCalls: 0 })).toEqual({ stale: 1, errors: [] });
    expect(nextGuard({ stale: 1, errors: [] }, { okToolCalls: 0 })).toEqual({ stale: 2, errors: [] });
    expect(nextGuard({ stale: 1, errors: ["E"] }, { okToolCalls: 2 })).toEqual({ stale: 0, errors: ["E"] });
    expect(nextGuard({ stale: 1, errors: ["E"] }, { commands: 3 })).toEqual({ stale: 1, errors: ["E"] });
    expect(nextGuard({ stale: 1, errors: ["E"] }, undefined)).toEqual({ stale: 1, errors: ["E"] });
    expect(nextGuard({ stale: 0, errors: ["A", "B"] }, { errors: ["C", "D"] })).toEqual({ stale: 0, errors: ["B", "C", "D"] });
    expect(nextGuard(undefined, { errors: ["", "  ", " E "] })).toEqual({ stale: 0, errors: ["E"] });
    expect(nextGuard(undefined, { errors: ["x".repeat(300)] }).errors[0]).toHaveLength(200);
  });

  it("loopStop stops on two stale continuations or three identical errors, with the reason for the event and the detail for the outcome", () => {
    expect(loopStop({ stale: 0, errors: [] }, "x")).toBeUndefined();
    expect(loopStop({ stale: 1, errors: ["E", "E"] }, "x")).toBeUndefined();
    expect(loopStop({ stale: 0, errors: ["E", "F", "E"] }, "x")).toBeUndefined();
    expect(loopStop({ stale: 2, errors: [] }, ` still going ${"y".repeat(400)}`))
      .toEqual({ reason: "2 continuations without a successful tool call", detail: `Stopped after 2 continuations without progress: ${`still going ${"y".repeat(400)}`.slice(0, 300)}` });
    expect(loopStop({ stale: 3, errors: [] }, "x")?.detail).toBe("Stopped after 3 continuations without progress: x");
    expect(loopStop({ stale: 0, errors: ["E", "E", "E"] }, "x")).toEqual({ reason: "the same error 3 times", detail: "Stopped after 3 identical errors: E" });
    expect(loopStop({ stale: 2, errors: ["E", "E", "E"] }, "x")?.reason).toBe("2 continuations without a successful tool call");
  });
});

describe("Runtime: a teammate's question reaches the requester waiting on it", () => {
  const asks: Script = (a, input) => a.assignee.botId === "builder" && input.answer === undefined ? { kind: "needs_input", question: "Which region?" }
    : a.assignee.botId === "tester" ? "hold" : undefined;
  const runsOf = (w: World, id: string) => w.runner.runs.filter(r => r.a.id === id);

  it("resumes the requester once with the question, keeps the sibling awaited, and resumes it again only when everything settled", async () => {
    const w = world(asks);
    const root = w.runtime.begin(beginReq());
    const builder = ok(w.runtime.enqueue(toBot("builder", root.id)));
    const tester = ok(w.runtime.enqueue(toBot("tester", root.id)));
    w.runtime.end(root.id, { ok: true, text: "Handed out" });
    w.runtime.tick(); await flush();
    expect(w.store.get(builder.id)).toMatchObject({ status: "needs_input", question: "Which region?" });
    expect(w.store.get(root.id)?.status).toBe("waiting");

    w.runtime.tick();
    expect(runsOf(w, root.id)).toHaveLength(1);
    const resume = runsOf(w, root.id)[0]!.input;
    expect(resume).toMatchObject({ turn: "resume", resumed: true });
    expect(resume.asked?.map(a => [a.id, a.question])).toEqual([[builder.id, "Which region?"]]);
    expect(resume.settled).toBeUndefined();
    expect(w.store.inbox({ threadId: "chief-chat" }, true)).toEqual([]);
    await flush();
    expect(w.store.get(root.id)?.status).toBe("waiting");
    expect(w.store.get(tester.id)?.status).toBe("running");
    w.runtime.tick(); await flush(); w.runtime.tick(); await flush();
    expect(runsOf(w, root.id)).toHaveLength(1);

    expect(w.runtime.answer(builder.id, "eu-west")).toBe(true);
    w.runtime.tick(); await flush();
    expect(runsOf(w, builder.id).at(-1)?.input).toMatchObject({ turn: "answered", answer: "eu-west" });
    expect(w.store.get(builder.id)?.status).toBe("completed");
    w.runtime.tick(); await flush();
    expect(runsOf(w, root.id)).toHaveLength(1);
    expect(w.store.get(root.id)?.status).toBe("waiting");

    await w.runner.release(tester.id, { kind: "completed", text: "tested" });
    w.runtime.tick();
    const final = runsOf(w, root.id)[1]!.input;
    expect(final.settled?.map(a => a.id)).toEqual([builder.id, tester.id]);
    expect(final.asked).toBeUndefined();
    await flush();
    expect(w.store.get(root.id)?.status).toBe("completed");
    expect(w.store.events(root.id).filter(e => e.kind === "resume_due").map(e => e.detail)).toEqual(["resume: a teammate asks", "resume"]);
    invariants(w);
  });

  it("a question parked before a restart resumes the requester once after it, and not again after the next restart", async () => {
    const store = openRuntimeStore(":memory:");
    const first = world(asks, {}, store);
    const root = first.runtime.begin(beginReq());
    const builder = ok(first.runtime.enqueue(toBot("builder", root.id)));
    ok(first.runtime.enqueue(toBot("tester", root.id)));
    first.runtime.end(root.id, { ok: true, text: "Handed out" });
    first.runtime.tick(); await flush();
    expect(store.get(builder.id)?.status).toBe("needs_input");
    first.runtime.close();

    const second = world(asks, {}, store);
    second.runtime.tick();
    expect(runsOf(second, root.id).map(r => r.input.asked?.map(a => a.id))).toEqual([[builder.id]]);
    await flush();
    expect(store.get(root.id)?.status).toBe("waiting");
    second.runtime.close();

    const third = world(asks, {}, store);
    third.runtime.tick(); await flush(); third.runtime.tick(); await flush();
    expect(runsOf(third, root.id)).toEqual([]);
    expect(store.get(root.id)?.status).toBe("waiting");
    expect(store.get(builder.id)?.status).toBe("needs_input");
    expect(third.runner.parks.map(p => p.a.id)).toEqual([builder.id]);
    invariants(third);
  });

  it("an unanswered question waits for the person however long they take, and a late answer still carries the work on", async () => {
    const w = world((a, input) => a.assignee.botId === "builder" && input.answer === undefined ? { kind: "needs_input", question: "Which region?" } : undefined);
    const root = w.runtime.begin(beginReq());
    const builder = ok(w.runtime.enqueue(toBot("builder", root.id)));
    const tester = ok(w.runtime.enqueue(toBot("tester", root.id)));
    w.runtime.end(root.id, { ok: true, text: "Handed out" });
    w.runtime.tick(); await flush();
    w.runtime.tick();
    expect(runsOf(w, root.id).map(r => [r.input.asked?.map(a => a.id), r.input.settled?.map(a => a.id)])).toEqual([[[builder.id], [tester.id]]]);
    await flush();
    w.clock.advance(30 * 60_000 + 1);
    w.runtime.tick(); await flush();
    w.clock.advance(7 * 24 * 60 * 60_000);
    w.runtime.tick(); await flush();
    expect(w.store.get(builder.id)?.status).toBe("needs_input");
    expect(w.store.get(root.id)?.status).toBe("waiting");
    expect(runsOf(w, root.id)).toHaveLength(1);
    expect(w.runtime.answer(builder.id, "eu-west")).toBe(true);
    w.runtime.tick(); await flush();
    expect(w.store.get(builder.id)).toMatchObject({ status: "completed", outcome: "done" });
    w.runtime.tick(); await flush();
    expect(runsOf(w, root.id)).toHaveLength(2);
    expect(runsOf(w, root.id)[1]!.input.settled?.map(a => a.id)).toEqual([builder.id, tester.id]);
    expect(w.store.get(root.id)?.status).toBe("completed");
    invariants(w);
  });

  it("a teammate parked on cards neither resumes the requester nor expires", async () => {
    const w = world(a => a.assignee.botId === "builder" ? { kind: "needs_input", question: "Connect GitHub", cards: ["msg-1"] } : undefined);
    const root = w.runtime.begin(beginReq());
    const builder = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.end(root.id, { ok: true, text: "Handed out" });
    w.runtime.tick(); await flush();
    w.clock.advance(400 * 24 * 60 * 60_000);
    w.runtime.tick(); await flush(); w.runtime.tick();
    expect(runsOf(w, root.id)).toEqual([]);
    expect(w.store.get(root.id)?.status).toBe("waiting");
    expect(w.store.get(builder.id)).toMatchObject({ status: "needs_input", cards: ["msg-1"] });
    expect(w.store.inbox({ threadId: "chief-chat" }, true).map(i => [i.assignmentId, i.kind])).toEqual([[builder.id, "needs_input"]]);
    invariants(w);
  });

  it("a question answered while the requester's thread was busy does not resume it with nothing to review", async () => {
    const w = world(asks);
    const root = w.runtime.begin(beginReq());
    const builder = ok(w.runtime.enqueue(toBot("builder", root.id)));
    ok(w.runtime.enqueue(toBot("tester", root.id)));
    w.runtime.end(root.id, { ok: true, text: "Handed out" });
    w.runtime.tick(); await flush();
    w.runner.canStartAnswer = false;
    w.runtime.tick();
    expect(w.store.get(root.id)?.status).toBe("resume");
    w.runtime.answer(builder.id, "eu-west");
    w.runtime.tick();
    expect(w.store.get(root.id)?.status).toBe("waiting");
    expect(w.store.events(root.id).at(-1)).toMatchObject({ kind: "waiting", detail: "waiting: nothing left to resume for" });
    w.runner.canStartAnswer = true;
    w.runtime.tick(); await flush(); w.runtime.tick(); await flush();
    expect(w.store.get(builder.id)?.status).toBe("completed");
    expect(runsOf(w, root.id)).toEqual([]);
    expect(w.store.get(root.id)?.status).toBe("waiting");
    invariants(w);
  });

  it("results that settle while the requester's question turn runs reach it in one final resume", async () => {
    const w = world((a, input) => a.assignee.botId === "chief" && input.asked ? "hold" : asks(a, input));
    const root = w.runtime.begin(beginReq());
    const builder = ok(w.runtime.enqueue(toBot("builder", root.id)));
    const tester = ok(w.runtime.enqueue(toBot("tester", root.id)));
    w.runtime.end(root.id, { ok: true, text: "Handed out" });
    w.runtime.tick(); await flush();
    w.runtime.tick(); await flush();
    expect(runsOf(w, root.id).map(r => r.input.asked?.map(a => a.id))).toEqual([[builder.id]]);
    expect(w.store.get(root.id)?.status).toBe("running");

    w.runtime.answer(builder.id, "eu-west");
    w.runtime.tick(); await flush();
    expect(w.store.get(builder.id)?.status).toBe("completed");
    await w.runner.release(tester.id, { kind: "completed", text: "tested" });
    w.runtime.tick(); await flush();
    expect(runsOf(w, root.id)).toHaveLength(1);
    await w.runner.release(root.id, { kind: "completed", text: "Passed the question on" });
    expect(w.store.get(root.id)?.status).toBe("waiting");

    w.runtime.tick();
    const final = runsOf(w, root.id)[1]?.input;
    expect(final).toMatchObject({ turn: "resume", resumed: true });
    expect(final?.settled?.map(a => a.id)).toEqual([builder.id, tester.id]);
    expect(final?.asked).toBeUndefined();
    expect(w.store.inbox({ threadId: "chief-chat" }, true)).toEqual([]);
    await flush();
    expect(w.store.get(root.id)?.status).toBe("completed");
    expect(runsOf(w, root.id)).toHaveLength(2);
    invariants(w);
  });

  it("a restart during the question turn reruns it once with the question, and no later turn carries it again", async () => {
    const store = openRuntimeStore(":memory:");
    const first = world((a, input) => a.assignee.botId === "chief" && input.asked ? "hold" : asks(a, input), {}, store);
    const root = first.runtime.begin(beginReq());
    const builder = ok(first.runtime.enqueue(toBot("builder", root.id)));
    const tester = ok(first.runtime.enqueue(toBot("tester", root.id)));
    first.runtime.end(root.id, { ok: true, text: "Handed out" });
    first.runtime.tick(); await flush();
    first.runtime.tick(); await flush();
    expect(runsOf(first, root.id).map(r => r.input.asked?.map(a => a.id))).toEqual([[builder.id]]);
    first.runtime.close();

    const second = world(asks, {}, store);
    second.runtime.tick(); await flush();
    second.runtime.tick(); await flush(); second.runtime.tick(); await flush();
    const reruns = runsOf(second, root.id);
    expect(reruns.map(r => [r.input.turn, r.input.asked?.map(a => a.id)])).toEqual([["resume", [builder.id]]]);
    expect(reruns[0]!.input.restartNote).toMatch(/restarted while your previous turn here was running and cut it off; this turn continues it/);
    expect(store.get(root.id)).toMatchObject({ status: "waiting", restarts: 1 });
    expect(store.get(root.id)?.carrying).toBeUndefined();

    second.runtime.answer(builder.id, "eu-west");
    second.runtime.tick(); await flush();
    await second.runner.release(tester.id, { kind: "completed", text: "tested" });
    second.runtime.tick(); await flush();
    const after = runsOf(second, root.id).slice(1);
    expect(after.map(r => [r.input.turn, r.input.asked, r.input.settled?.map(a => a.id)])).toEqual([["resume", undefined, [builder.id, tester.id]]]);
    expect(store.get(root.id)?.status).toBe("completed");
    invariants(second);
  });

  it("the requester's own later turns do not carry a question its question turn already passed on", async () => {
    const w = world((a, input) => a.assignee.botId === "chief" && input.asked ? { kind: "needs_input", question: "Which region for builder?" } : asks(a, input));
    const root = w.runtime.begin(beginReq());
    const builder = ok(w.runtime.enqueue(toBot("builder", root.id)));
    ok(w.runtime.enqueue(toBot("tester", root.id)));
    w.runtime.end(root.id, { ok: true, text: "Handed out" });
    w.runtime.tick(); await flush();
    w.runtime.tick(); await flush();
    expect(runsOf(w, root.id).map(r => r.input.asked?.map(a => a.id))).toEqual([[builder.id]]);
    expect(w.store.get(root.id)?.status).toBe("needs_input");

    expect(w.runtime.answer(root.id, "eu-west")).toBe(true);
    w.runtime.tick(); await flush();
    const answered = runsOf(w, root.id)[1]?.input;
    expect(answered).toMatchObject({ turn: "answered", answer: "eu-west" });
    expect(answered?.asked).toBeUndefined();
    expect(w.store.get(builder.id)?.status).toBe("needs_input");
    expect(w.store.get(root.id)?.status).toBe("waiting");
    w.runtime.tick(); await flush();
    expect(runsOf(w, root.id)).toHaveLength(2);
    invariants(w);
  });
});

describe("Runtime: at Full access a question is its requester's to answer, and only what the person alone can do waits on them", () => {
  const runsOf = (w: World, id: string) => w.runner.runs.filter(r => r.a.id === id);
  const full = (botId: string, parentId: string, patch: Partial<EnqueueRequest> = {}) =>
    toBot(botId, parentId, `Build it, ${botId}`, { levels: { own: "full" }, ...patch });
  const decides: Script = (a, input) => a.assignee.botId === "builder" && input.answer === undefined
    ? { kind: "needs_input", question: "CSV or Excel?\nDefault: CSV", decide: { default: "CSV" } } : undefined;

  it("resumes the requester with the question; unanswered when that turn ends, the asker goes on with its default", async () => {
    const w = world(decides);
    const root = w.runtime.begin(beginReq());
    const builder = ok(w.runtime.enqueue(full("builder", root.id)));
    w.runtime.end(root.id, { ok: true, text: "Handed out" });
    w.runtime.tick(); await flush();
    expect(w.store.get(builder.id)).toMatchObject({ status: "needs_input", level: "full", decide: { default: "CSV" } });
    expect(w.runner.parks.map(p => p.a.decide)).toEqual([{ default: "CSV" }]);

    w.runtime.tick();
    expect(runsOf(w, root.id).map(r => r.input.asked?.map(a => a.id))).toEqual([[builder.id]]);
    await flush();
    expect(w.store.get(builder.id)).toMatchObject({ status: "queued", answer: "CSV", answeredBy: { kind: "default" } });
    expect(w.store.get(builder.id)?.decide).toBeUndefined();
    expect(w.store.events(builder.id).map(e => e.detail)).toContain("queued: answered with its default");

    w.runtime.tick(); await flush();
    expect(runsOf(w, builder.id).at(-1)?.input).toMatchObject({ turn: "answered", answer: "CSV" });
    expect(runsOf(w, builder.id).at(-1)?.input.envelope).toContain('Nobody was asked, at this level: go on with your default, "CSV", and say in one plain line what you chose.');
    expect(w.store.get(builder.id)).toMatchObject({ status: "completed" });
    expect(w.store.get(builder.id)?.answeredBy).toBeUndefined();
    w.runtime.tick(); await flush();
    expect(runsOf(w, root.id).at(-1)?.input.settled?.map(a => a.id)).toEqual([builder.id]);
    expect(w.store.get(root.id)?.status).toBe("completed");
    invariants(w);
  });

  it("takes the requester's own answer, given while its turn carries the question, over the default", async () => {
    const w = world((a, input) => a.assignee.botId === "chief" && input.asked?.length ? "hold" : decides(a, input));
    const root = w.runtime.begin(beginReq());
    const builder = ok(w.runtime.enqueue(full("builder", root.id)));
    w.runtime.end(root.id, { ok: true, text: "Handed out" });
    w.runtime.tick(); await flush();
    w.runtime.tick();
    expect(w.runtime.answer(builder.id, "Excel", { kind: "bot", botId: "chief" })).toBe(true);
    await w.runner.release(root.id, { kind: "completed", text: "Told the builder: Excel" });
    expect(w.store.get(builder.id)).toMatchObject({ status: "queued", answer: "Excel", answeredBy: { kind: "bot", botId: "chief" } });
    w.runtime.tick(); await flush();
    expect(runsOf(w, builder.id).at(-1)?.input.envelope).toContain('@chief answered: "Excel".');
    w.runtime.tick(); await flush();
    expect(w.store.get(root.id)?.status).toBe("completed");
    invariants(w);
  });

  it("goes on with the default at once when nothing would take a turn for it: a one-way notice, or a poll root", async () => {
    const w = world(decides);
    const root = w.runtime.begin(beginReq());
    const notice = ok(w.runtime.enqueue(full("builder", root.id, { mode: "notify", reply: "none" })));
    w.runtime.end(root.id, { ok: true, text: "Told the builder" });
    w.runtime.tick(); await flush();
    expect(w.store.get(notice.id)).toMatchObject({ answeredBy: { kind: "default" }, answer: "CSV" });
    w.runtime.tick(); await flush();
    expect(w.store.get(notice.id)?.status).toBe("completed");

    const v = world(decides);
    const polled = v.runtime.begin(beginReq({ reply: "poll", requester: { kind: "external_runtime", runId: "run-1" } }));
    const child = ok(v.runtime.enqueue(full("builder", polled.id)));
    v.runtime.end(polled.id, { ok: true, text: "" });
    v.runtime.tick(); await flush();
    expect(v.store.get(child.id)).toMatchObject({ status: "queued", answeredBy: { kind: "default" } });
    invariants(w); invariants(v);
  });

  it("goes on with the default when the requester's carrying turn is cut by a restart after handing out more work", async () => {
    const store = openRuntimeStore(":memory:");
    const first = world((a, input) => a.assignee.botId === "chief" && input.asked?.length ? "hold" : a.assignee.botId === "tester" ? "hold" : decides(a, input), {}, store);
    const root = first.runtime.begin(beginReq());
    const builder = ok(first.runtime.enqueue(full("builder", root.id)));
    first.runtime.end(root.id, { ok: true, text: "Handed out" });
    first.runtime.tick(); await flush();
    first.runtime.tick();
    expect(store.get(root.id)?.carrying).toEqual([builder.id]);
    ok(first.runtime.enqueue(toBot("tester", root.id)));
    first.runtime.close();

    const second = world(() => "hold", {}, store);
    expect(store.get(root.id)).toMatchObject({ status: "waiting", restart: "cut" });
    expect(store.get(builder.id)).toMatchObject({ status: "queued", answeredBy: { kind: "default" }, answer: "CSV" });
    expect(second.runner.parks.map(p => p.a.id)).toEqual([builder.id]);
  });

  it("hears at once of what only the person can do and carries on with the rest; the card's work resumes when they act", async () => {
    const w = world((a, input) => a.assignee.botId === "builder" && input.answer === undefined
      ? { kind: "needs_input", question: "Waiting on the person: sign in to notion.so", cards: ["msg-1"] } : undefined);
    const root = w.runtime.begin(beginReq());
    const builder = ok(w.runtime.enqueue(full("builder", root.id)));
    const tester = ok(w.runtime.enqueue(full("tester", root.id)));
    w.runtime.end(root.id, { ok: true, text: "Handed out" });
    w.runtime.tick(); await flush();
    expect(w.store.get(builder.id)).toMatchObject({ status: "needs_input", cards: ["msg-1"] });
    expect(w.store.get(builder.id)?.decide).toBeUndefined();
    w.runtime.tick(); await flush();
    expect(runsOf(w, root.id).map(r => [r.input.asked?.map(a => a.id), r.input.settled?.map(a => a.id)])).toEqual([[[builder.id], [tester.id]]]);
    expect(w.store.get(root.id)?.status).toBe("waiting");
    expect(w.store.get(builder.id)?.status).toBe("needs_input");
    w.clock.advance(400 * 24 * 60 * 60_000);
    w.runtime.tick(); await flush();
    expect(runsOf(w, root.id)).toHaveLength(1);

    expect(w.runtime.answer(builder.id, "The person finished signing in to notion.so.")).toBe(true);
    w.runtime.tick(); await flush(); w.runtime.tick(); await flush();
    expect(runsOf(w, root.id)).toHaveLength(2);
    expect(runsOf(w, root.id)[1]!.input.settled?.map(a => a.id)).toEqual([builder.id, tester.id]);
    expect(w.store.get(root.id)?.status).toBe("completed");
    invariants(w);
  });
});

describe("Runtime: turns a restart cut start again a few at a time", () => {
  it("starts the first few at once and the rest one per gap, and a second restart cuts only the ones that had started", async () => {
    const store = openRuntimeStore(":memory:");
    const first = world(() => "hold", {}, store);
    const roots = Array.from({ length: RERUN_BURST + 3 }, (_, i) => first.runtime.begin(beginReq({
      requester: { kind: "person", threadId: `chat-${i}`, chief: true }, assignee: { botId: `bot-${i}`, destination: { kind: "standing_thread", threadId: `chat-${i}` } } })));
    first.runtime.close();

    const second = world(() => "hold", {}, store);
    second.runtime.tick(); await flush();
    expect(second.runner.runs.map(r => r.input.turn)).toEqual(Array(RERUN_BURST).fill("rerun"));
    const waiting = roots.filter(root => store.get(root.id)?.status === "queued");
    expect(waiting).toHaveLength(3);
    for (const root of waiting) expect(store.events(root.id).at(-1)).toMatchObject({ kind: "held", detail: RERUN_HELD });
    second.clock.advance(RERUN_GAP_MS - 1);
    second.runtime.tick(); await flush();
    expect(second.runner.runs).toHaveLength(RERUN_BURST);
    second.clock.advance(1);
    second.runtime.tick(); await flush();
    expect(second.runner.runs).toHaveLength(RERUN_BURST + 1);
    second.runtime.close();

    // Down again during the reruns: those that had started fail as cut twice; the two still waiting are not cut again.
    const third = world(() => "hold", {}, store);
    const cutTwice = roots.filter(root => store.get(root.id)?.status === "failed");
    expect(cutTwice).toHaveLength(RERUN_BURST + 1);
    for (const root of cutTwice) expect(store.get(root.id)).toMatchObject({ outcome: "interrupted", result: CUT_TWICE });
    third.runtime.tick(); await flush();
    expect(third.runner.runs.map(r => r.input.turn)).toEqual(["rerun", "rerun"]);
  });

  it("does not run a cut turn again after newer work in its conversation started first: it fails with a line saying so", async () => {
    const store = openRuntimeStore(":memory:");
    const first = world(() => "hold", {}, store);
    const chat = (i: number) => ({ requester: { kind: "person" as const, threadId: `chat-${i}`, chief: true }, assignee: { botId: `bot-${i}`, destination: { kind: "standing_thread" as const, threadId: `chat-${i}` } } });
    const roots = Array.from({ length: RERUN_BURST + 1 }, (_, i) => first.runtime.begin(beginReq(chat(i))));
    first.runtime.close();

    const second = world(() => "hold", {}, store);
    second.runtime.tick(); await flush();
    const late = roots[RERUN_BURST]!;
    expect(store.get(late.id)?.status).toBe("queued");
    // The person writes in that chat while its cut turn waits its turn: the new request runs, and the old one does not.
    const newer = second.runtime.begin(beginReq(chat(RERUN_BURST)));
    second.clock.advance(RERUN_GAP_MS);
    second.runtime.tick(); await flush();
    expect(store.get(late.id)).toMatchObject({ status: "failed", outcome: "interrupted", result: RERUN_SUPERSEDED });
    expect(second.runner.runs.filter(r => r.a.id === late.id)).toHaveLength(0);
    expect(store.get(newer.id)?.status).toBe("running");
  });
});

describe("Runtime: a turn cut by the restart after a memory freeze", () => {
  it("ends with the given line instead of starting again, and other cut turns start again as before", () => {
    const store = openRuntimeStore(":memory:");
    const first = world(undefined, {}, store);
    first.runtime.begin(beginReq({ id: "root-browsed", assignee: { botId: "chief", destination: { kind: "standing_thread", threadId: "browsed-chat" } } }));
    first.runtime.begin(beginReq({ id: "root-other", assignee: { botId: "chief", destination: { kind: "standing_thread", threadId: "other-chat" } } }));
    first.runtime.close();

    const asked: string[] = [];
    const line = "Ran out of room, so I closed the browser to keep everything else running.";
    const runner = fakeRunner(() => "hold");
    const runtime = new Runtime(store, runner, { clock: fakeClock(), restartCut: (threadId, startedAt) => {
      asked.push(`${threadId} ${typeof startedAt}`);
      return threadId === "browsed-chat" ? line : null;
    } });
    runtime.open();
    expect(asked.sort()).toEqual(["browsed-chat number", "other-chat number"]);
    expect(store.get("root-browsed")).toMatchObject({ status: "failed", outcome: "interrupted", result: line });
    expect(store.get("root-other")).toMatchObject({ status: "queued", restart: "rerun", restarts: 1 });
    expect(runner.reports.map(r => r.a.id)).toEqual(["root-browsed"]);
    runtime.tick();
    expect(runner.runs.map(r => r.a.id)).toEqual(["root-other"]);
    expect(checkInvariants(store, runner.live())).toEqual([]);
    runtime.close();
    store.close();
  });
});

describe("Runtime: a settlement the store refused is retried, not lost", () => {
  it("keeps a completed result whose settle threw, settles it on the next tick, and never expires the tree meanwhile", async () => {
    const w = world(() => "hold", { turnStallMs: 1_000, stallMs: 10_000 });
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick();
    expect(w.store.get(child.id)?.status).toBe("running");
    // One throw from the store's transaction stands in for a transient SQLite failure during settle.
    const real = w.store.transaction.bind(w.store);
    let refuse = true;
    (w.store as { transaction: (fn: () => unknown) => unknown }).transaction = fn => {
      if (refuse) { refuse = false; throw new Error("disk hiccup"); }
      return real(fn);
    };
    await w.runner.release(child.id, { kind: "completed", text: "all done" });
    expect(w.store.get(child.id)).toMatchObject({ status: "running" });
    expect(w.logs.some(l => l.includes("settling") && l.includes("will retry"))).toBe(true);
    // Longer than both windows: a pending settlement must not be read as a stall.
    w.clock.advance(60_000);
    w.runtime.tick();
    expect(w.store.get(child.id)).toMatchObject({ status: "completed", outcome: "done", result: "all done" });
    expect(w.store.events(child.id).some(e => e.kind === "stalled")).toBe(false);
    expect(checkInvariants(w.store, w.runner.live())).toEqual([]);
  });


  it("never replays a refused settlement onto a node the user cancelled meanwhile", async () => {
    const w = world(() => "hold", { turnStallMs: 1_000, stallMs: 10_000 });
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick();
    const real = w.store.transaction.bind(w.store);
    let refuse = true;
    (w.store as { transaction: (fn: () => unknown) => unknown }).transaction = fn => {
      if (refuse) { refuse = false; throw new Error("disk hiccup"); }
      return real(fn);
    };
    await w.runner.release(child.id, { kind: "completed", text: "all done" });
    expect(w.store.get(child.id)?.status).toBe("running");
    // The user stops the tree while the settlement is still pending.
    w.runtime.cancel(child.id);
    w.runtime.tick();
    expect(w.store.get(child.id)).toMatchObject({ status: "cancelled", outcome: "stopped" });
    // The refused settlement must not overwrite the user's stop with the completed result.
    expect(w.store.get(child.id)?.result).toBe("Stopped by user");
    expect(checkInvariants(w.store, w.runner.live())).toEqual([]);
  });

  it("retries a refused end() the same way", async () => {
    const w = world(() => "hold", { turnStallMs: 1_000, stallMs: 10_000 });
    const root = w.runtime.begin(beginReq());
    w.runtime.tick();
    const real = w.store.transaction.bind(w.store);
    let refuse = true;
    (w.store as { transaction: (fn: () => unknown) => unknown }).transaction = fn => {
      if (refuse) { refuse = false; throw new Error("disk hiccup"); }
      return real(fn);
    };
    w.runtime.end(root.id, { ok: true, text: "Handed off" });
    expect(w.store.get(root.id)?.status).toBe("running");
    w.runtime.tick();
    expect(w.store.get(root.id)).toMatchObject({ status: "completed", outcome: "done", result: "Handed off" });
  });

  it("gives up a settlement the store keeps refusing after a few ticks, so the tree's own expiry ends it", async () => {
    const w = world(() => "hold", { turnStallMs: 10_000_000, stallMs: 10_000 });
    const root = w.runtime.begin(beginReq());
    const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
    w.runtime.tick();
    const real = w.store.transaction.bind(w.store);
    let refuse = true;
    (w.store as { transaction: (fn: () => unknown) => unknown }).transaction = fn => {
      if (refuse) throw new Error("disk full");
      return real(fn);
    };
    await w.runner.release(child.id, { kind: "completed", text: "all done" });
    // The refusing store fails the tick's other writes too; each tick still retries the settlement first.
    for (let at = 0; at < 5; at += 1) { try { w.runtime.tick(); } catch { /* the same refusal */ } }
    expect(w.logs.some(l => l.includes(`settling ${child.id}`) && l.includes("giving up after 5 retries"))).toBe(true);
    refuse = false;
    w.clock.advance(60_000);
    w.runtime.tick();
    expect(w.store.events(root.id).some(e => e.kind === "stalled")).toBe(true);
    expect(w.store.get(child.id)?.status).not.toBe("running");
    expect(w.store.get(child.id)?.result).not.toBe("all done");
  });
});