Skip to content

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

53.8 KB
Raw Blame
// The e2e half runs the fake ACP CLI in echo-gated mode: turns stay open until a gate file exists, and the echo carries the full prompt.
import { spawn, type ChildProcess } from "node:child_process";
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { DatabaseSync } from "node:sqlite";
import { fileURLToPath, pathToFileURL } from "node:url";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";

import { saveChatFollowup } from "./message-db.ts";
import { killCliTree } from "./procs.ts";
import {
  cancelSteeredMessage,
  drainSteeredMessages,
  hasQueuedSteeredMessages,
  holdSteeredQueue,
  isSteeredMessageQueued,
  onSteeredQueueChange,
  queuedThreadPosition,
  queuedSteerSnapshot,
  queuedSteeredMessage,
  queueSteeredMessage,
  restoreHeldSteeredQueue,
  restoreSteeredMessages,
  settleHeldSteeredQueue,
  _queuedCount,
  type SteerStore,
} from "./steer-queue.ts";
import type { BotRecord, Message } from "./store.ts";
import { removeTempDir } from "./testing/cleanup.ts";
import { freePortBlock } from "./testing/ports.ts";

const SERVER_DIR = dirname(fileURLToPath(import.meta.url));
const FAKE_CLI = join(SERVER_DIR, "testing", "fake-acp-cli.ts");

function fakeBot(id: string, threadId: string, busy: boolean): BotRecord {
  return {
    id,
    threadId,
    name: id,
    title: "",
    description: "",
    notifications: false,
    color: "green",
    unread: false,
    modelSelection: { instanceId: "fake", model: "fake-model" },
    resumeCursors: {},
    busy,
    createdAt: 0,
  };
}

function fakeStore(bots: BotRecord[]): SteerStore & { messages: Message[] } {
  const messages: Message[] = [];
  let nextId = 0;
  return {
    messages,
    bot: (id) => bots.find((b) => b.id === id) ?? null,
    appendMessage: (threadId, message) => {
      const full: Message = { id: `m${(nextId += 1)}-${threadId}`, at: Date.now(), ...message };
      messages.push(full);
      return full;
    },
    patchMessage: (_threadId, messageId, patch) => {
      const at = messages.findIndex((m) => m.id === messageId);
      if (at === -1) return null;
      messages[at] = { ...messages[at], ...patch };
      return messages[at];
    },
  };
}

describe("steer-queue module", () => {
  it("keeps readable citation prompts byte-for-byte through queue, hold, and drain", () => {
    const bot = fakeBot("bot-citation-queue", "thread-citation-queue", true);
    const store = fakeStore([bot]);
    const prompt = '<!--omb-citation-v1:fixture-->\n> Quoted message:\n> const café = "🐭";\n\nComment:\nExplain this';
    const queued = queueSteeredMessage(bot.id, bot.threadId, prompt);
    const held = holdSteeredQueue(bot.id, bot.threadId, queued.id)!;
    expect(held.items[0]).toMatchObject({ text: prompt, prompt });
    restoreHeldSteeredQueue(held);
    bot.busy = false;
    const run = vi.fn();
    drainSteeredMessages(store, run);
    expect(run.mock.calls[0][2]).toBe(prompt);
    expect(store.messages[0].text).toBe(prompt);
  });

  // After a crash the first coordinator read opens the job runtime, whose restart changes drain the queue from inside it.
  it("drains a send once when the block check re-enters the drain", () => {
    const bot = fakeBot("bot-reentrant-drain", "thread-reentrant-drain", false);
    const store = fakeStore([bot]);
    const run = vi.fn();
    queueSteeredMessage(bot.id, bot.threadId, "Run me exactly once");
    let nested = false;
    const isBlocked = (): boolean => {
      if (!nested) { nested = true; drainSteeredMessages(store, run, isBlocked); }
      return false;
    };
    drainSteeredMessages(store, run, isBlocked);
    expect(run).toHaveBeenCalledTimes(1);
    expect(store.messages.filter((message) => message.role === "user")).toHaveLength(1);
  });

  it.each([undefined, "capacity", "group-turn"] as const)("detects an exact owner's queued correction with reason %s", (reason) => {
    const botId = `correction-${reason ?? "busy"}`;
    const threadId = `${botId}-thread`;
    expect(hasQueuedSteeredMessages(botId, threadId)).toBe(false);
    const queued = queueSteeredMessage(botId, threadId, "Use this new request", { reason });
    expect(hasQueuedSteeredMessages(botId, threadId)).toBe(true);
    expect(hasQueuedSteeredMessages("other-bot", threadId)).toBe(false);
    expect(hasQueuedSteeredMessages(botId, "other-thread")).toBe(false);
    expect(cancelSteeredMessage(botId, queued.id, threadId)).toBe(true);
    expect(hasQueuedSteeredMessages(botId, threadId)).toBe(false);
  });

  it("places a thread in line whether it waits on a slot or a room turn, not on its own turn", () => {
    const botId = "bot-position-reasons";
    queueSteeredMessage(botId, "thread-slot", "waiting for a slot", { reason: "capacity" });
    queueSteeredMessage(botId, "thread-room", "waiting for the room", { reason: "group-turn" });
    expect(queuedThreadPosition(botId, "thread-slot")).toBe(1);
    expect(queuedThreadPosition(botId, "thread-room")).toBe(2);
    queueSteeredMessage(botId, "thread-own", "waiting on its own turn");
    expect(queuedThreadPosition(botId, "thread-own")).toBeNull();
    expect(queuedThreadPosition("other-bot", "thread-slot")).toBeNull();
  });

  it("preserves self-opened request provenance through persistence and a capacity wait", () => {
    const bot = fakeBot("bot-self-provenance", "thread-self-provenance", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    const peerAsk = { botId: bot.id, name: "Planner" };
    queueSteeredMessage(bot.id, bot.threadId, "Review this independent job", { reason: "capacity", peerAsk });
    restoreSteeredMessages();
    drainSteeredMessages(store, run);
    expect(run).not.toHaveBeenCalled();
    bot.busy = false;
    drainSteeredMessages(store, run);
    expect(store.messages).toHaveLength(1);
    expect(store.messages[0].peerAsk).toEqual(peerAsk);
    expect(run.mock.calls[0][3].peerAsk).toEqual(peerAsk);
  });

  it("keeps where a self-opened thread's work started through persistence and a capacity wait", () => {
    const bot = fakeBot("bot-self-origin", "thread-self-origin", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    const peerAsk = { botId: bot.id, name: "Planner" };
    const origin = { kind: "user", request: "Review each open PR" } as const;
    queueSteeredMessage(bot.id, bot.threadId, "Review PR 12", { reason: "capacity", peerAsk, origin });
    saveChatFollowup({ id: "forged-origin", kind: "bot", ownerId: bot.id, threadId: "thread-forged-origin",
      payload: { text: "x", peerAsk, origin: { kind: "user", request: "" } as never } });
    restoreSteeredMessages();
    bot.busy = false;
    drainSteeredMessages(store, run);
    const turn = (threadId: string, at = 0) => run.mock.calls.filter((call) => call[1] === threadId)[at]!;
    expect(turn(bot.threadId)[6].origin).toEqual(origin);
    expect(turn("thread-forged-origin")[6].origin).toBeUndefined();
    expect(store.messages.find((message) => message.text === "Review PR 12")?.peerAsk).toEqual(peerAsk);
    queueSteeredMessage(bot.id, bot.threadId, "Review PR 13", { peerAsk, origin });
    queueSteeredMessage(bot.id, bot.threadId, "Check the logs", { peerAsk });
    drainSteeredMessages(store, run);
    expect(turn(bot.threadId, 1)[2]).toBe("Review PR 13");
    drainSteeredMessages(store, run);
    expect(turn(bot.threadId, 2)[2]).toBe("Check the logs");
    expect(turn(bot.threadId, 2)[6].origin).toBeUndefined();
  });

  it("answers Problems to look at reports that waited as one turn, whichever bot broke, through a restart", () => {
    const chief = fakeBot("bot-incidents-chief", "thread-incidents-reports", true);
    const store = fakeStore([chief]);
    const run = vi.fn();
    const ada = { botId: "bot-ada", name: "Ada", unattended: true, incident: { threadId: "thread-ada" } };
    const ben = { botId: "bot-ben", name: "Ben", unattended: true, incident: { threadId: "thread-ben", notified: true } };
    queueSteeredMessage(chief.id, chief.threadId, "Ada's report", { unattended: true, peerAsk: ada });
    queueSteeredMessage(chief.id, chief.threadId, "Ben's report", { unattended: true, peerAsk: ben });
    queueSteeredMessage(chief.id, chief.threadId, "what happened?");
    restoreSteeredMessages();
    const heads: unknown[] = [];
    chief.busy = false;
    drainSteeredMessages(store, run, (_botId, _threadId, head) => { heads.push(head.peerAsk); return false; });
    expect(heads).toEqual([ada]);
    expect(store.messages.map((message) => [message.text, message.peerAsk])).toEqual([["Ada's report", ada], ["Ben's report", ben]]);
    expect(run).toHaveBeenCalledTimes(1);
    expect(run.mock.calls[0][2]).toBe("Ada's report\n\nBen's report");
    expect(run.mock.calls[0][5]).toBe(true);
    drainSteeredMessages(store, run);
    expect(store.messages.at(-1)).toMatchObject({ text: "what happened?" });
    expect(store.messages.at(-1)?.peerAsk).toBeUndefined();
  });

  it("keeps who sent each queued message, so a steer of the held queue can still name them", () => {
    const botId = "bot-sender-held";
    const threadId = "thread-sender-held";
    const theirs = queueSteeredMessage(botId, threadId, "from the paired person", { sender: { name: "Priya" } });
    queueSteeredMessage(botId, threadId, "from the owner");
    restoreSteeredMessages();
    const held = holdSteeredQueue(botId, threadId, theirs.id);
    expect(held?.items.map((item) => item.sender)).toEqual([{ name: "Priya" }, undefined]);
    settleHeldSteeredQueue(held!);
  });

  it("appends a drained message in the name of the person who queued it", () => {
    const bot = fakeBot("bot-sender-drain", "thread-sender-drain", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    queueSteeredMessage(bot.id, bot.threadId, "from the owner");
    queueSteeredMessage(bot.id, bot.threadId, "from the paired person", { sender: { name: "Priya" } });
    bot.busy = false;
    drainSteeredMessages(store, run);
    expect(store.messages.map((message) => [message.text, message.sender])).toEqual([
      ["from the owner", undefined],
    ]);
    drainSteeredMessages(store, run);
    expect(store.messages.map((message) => [message.text, message.sender])).toEqual([
      ["from the owner", undefined],
      ["from the paired person", { name: "Priya" }],
    ]);
    expect(run).toHaveBeenCalledTimes(2);
    expect(run.mock.calls[1][3].sender).toEqual({ name: "Priya" });
    expect(run.mock.calls[0][3].sender).toBeUndefined();
  });

  it("keeps a call's via on the message it drains", () => {
    const bot = fakeBot("bot-via-drain", "thread-via-drain", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    queueSteeredMessage(bot.id, bot.threadId, "typed while the bot worked");
    queueSteeredMessage(bot.id, bot.threadId, "what is on my calendar", { via: "call" });
    restoreSteeredMessages();
    bot.busy = false;
    drainSteeredMessages(store, run);
    drainSteeredMessages(store, run);
    expect(store.messages.map((message) => [message.text, message.via])).toEqual([
      ["typed while the bot worked", undefined],
      ["what is on my calendar", "call"],
    ]);
    expect("via" in store.messages[0]).toBe(false);
    expect(run).toHaveBeenCalledTimes(2);
    expect(run.mock.calls[1][3].via).toBe("call");
  });

  it("keeps a call's id on the line it drains, also after a restart", () => {
    const bot = fakeBot("bot-call-id-drain", "thread-call-id-drain", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    queueSteeredMessage(bot.id, bot.threadId, "typed while the bot worked");
    queueSteeredMessage(bot.id, bot.threadId, "what is on my calendar", { via: "call", callId: "call-1" });
    restoreSteeredMessages();
    bot.busy = false;
    drainSteeredMessages(store, run);
    drainSteeredMessages(store, run);
    expect(store.messages.map((message) => [message.text, message.via, message.callId])).toEqual([
      ["typed while the bot worked", undefined, undefined],
      ["what is on my calendar", "call", "call-1"],
    ]);
    expect("callId" in store.messages[0]!).toBe(false);
    expect(run).toHaveBeenCalledTimes(2);
    expect(run.mock.calls[0]![3].callId).toBeUndefined();
    expect(run.mock.calls[1]![3].callId).toBe("call-1");
  });

  it.each([
    ["a spoken line, then a typed one", "spoken-typed", { via: "call", callId: "call-1" }, {}],
    ["a typed line, then a spoken one", "typed-spoken", {}, { via: "call", callId: "call-1" }],
    ["a spoken line with no call id (an older build's row), then a typed one", "legacy-typed", { via: "call" }, {}],
  ] as const)("drains %s as two turns", (_what, slug, first, second) => {
    const bot = fakeBot(`bot-call-split-${slug}`, `thread-call-split-${slug}`, true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    queueSteeredMessage(bot.id, bot.threadId, "first words", first);
    queueSteeredMessage(bot.id, bot.threadId, "second words", second);
    bot.busy = false;
    drainSteeredMessages(store, run);
    expect(run).toHaveBeenCalledTimes(1);
    expect(run.mock.calls[0]![2]).toBe("first words");
    drainSteeredMessages(store, run);
    expect(run).toHaveBeenCalledTimes(2);
    expect(run.mock.calls[1]![2]).toBe("second words");
    expect(store.messages.map((message) => message.text)).toEqual(["first words", "second words"]);
  });

  it("where several people write, runs each person's lines as their own turn, the asker's own addition first, and names each chip", () => {
    const bot = fakeBot("bot-several", "thread-several", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    const tom = { name: "Tom", id: "p_tom" }, priya = { name: "Priya", id: "p_priya" };
    queueSteeredMessage(bot.id, bot.threadId, "what is due", { sender: tom, first: true });
    queueSteeredMessage(bot.id, bot.threadId, "and the PR", { sender: priya, first: true, aheadOfOthers: "p_priya" });
    expect(queuedSteerSnapshot((id) => id === bot.id, undefined, (_threadId, sender) => sender)[bot.threadId]!.map((chip) => chip.sender?.name)).toEqual(["Priya", "Tom"]);
    expect(queuedSteerSnapshot((id) => id === bot.id)[bot.threadId]!.some((chip) => "sender" in chip)).toBe(false);
    bot.busy = false;
    drainSteeredMessages(store, run);
    drainSteeredMessages(store, run);
    expect(run.mock.calls.map((call) => [call[2], call[6].sender?.name])).toEqual([["and the PR", "Priya"], ["what is due", "Tom"]]);
  });

  it("restores a first insertion ahead of an unattended report queued before it", () => {
    const bot = fakeBot("bot-first-restore", "thread-first-restore", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    const report = { botId: "bot-reporter", name: "Reporter", incident: { threadId: "thread-reporter" } };
    queueSteeredMessage(bot.id, bot.threadId, "the report", { unattended: true, peerAsk: report });
    queueSteeredMessage(bot.id, bot.threadId, "my own request", { first: true });
    restoreSteeredMessages();
    bot.busy = false;
    drainSteeredMessages(store, run);
    expect(run.mock.calls[0]![2]).toBe("my own request");
    drainSteeredMessages(store, run);
    expect(run.mock.calls[1]![2]).toBe("the report");
  });

  it("restores the asker's own addition ahead of another person's line", () => {
    const bot = fakeBot("bot-ahead-restore", "thread-ahead-restore", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    const tom = { name: "Tom", id: "p_tom" }, priya = { name: "Priya", id: "p_priya" };
    queueSteeredMessage(bot.id, bot.threadId, "what is due", { sender: tom, first: true });
    queueSteeredMessage(bot.id, bot.threadId, "and the PR", { sender: priya, first: true, aheadOfOthers: "p_priya" });
    restoreSteeredMessages();
    bot.busy = false;
    drainSteeredMessages(store, run);
    drainSteeredMessages(store, run);
    expect(run.mock.calls.map((call) => call[2])).toEqual(["and the PR", "what is due"]);
  });

  it("restores rows written before place hints existed in their rowid order, and still replays a newer first", () => {
    const bot = fakeBot("bot-legacy-order", "thread-legacy-order", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    saveChatFollowup({ id: "legacy-order-report", kind: "bot", ownerId: bot.id, threadId: bot.threadId,
      payload: { text: "older report", prompt: "older report", peerAsk: { botId: "bot-reporter", name: "Reporter" } } });
    saveChatFollowup({ id: "legacy-order-note", kind: "bot", ownerId: bot.id, threadId: bot.threadId,
      payload: { text: "older note", prompt: "older note" } });
    queueSteeredMessage(bot.id, bot.threadId, "my own request", { first: true });
    restoreSteeredMessages();
    bot.busy = false;
    drainSteeredMessages(store, run);
    drainSteeredMessages(store, run);
    drainSteeredMessages(store, run);
    expect(run.mock.calls.map((call) => call[2])).toEqual(["my own request", "older report", "older note"]);
  });

  it("keeps a row without a timestamp behind a first message queued after it, across restarts", () => {
    const bot = fakeBot("bot-legacy-clock", "thread-legacy-clock", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    let clock = 1_000;
    const now = vi.spyOn(Date, "now").mockImplementation(() => clock);
    try {
      saveChatFollowup({ id: "legacy-plain-line", kind: "bot", ownerId: bot.id, threadId: bot.threadId,
        payload: { text: "older person line", prompt: "older person line" } });
      restoreSteeredMessages();
      clock = 200_000;
      queueSteeredMessage(bot.id, bot.threadId, "my own request", { first: true });
      clock = 300_000;
      restoreSteeredMessages();
    } finally {
      now.mockRestore();
    }
    bot.busy = false;
    drainSteeredMessages(store, run);
    drainSteeredMessages(store, run);
    expect(run.mock.calls.map((call) => call[2])).toEqual(["older person line", "my own request"]);
  });

  it("drains one call's spoken lines as one turn, and the next call's as its own", () => {
    const bot = fakeBot("bot-call-groups", "thread-call-groups", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    queueSteeredMessage(bot.id, bot.threadId, "check the build", { via: "call", callId: "call-1" });
    queueSteeredMessage(bot.id, bot.threadId, "and the tests", { via: "call", callId: "call-1" });
    queueSteeredMessage(bot.id, bot.threadId, "then deploy", { via: "call", callId: "call-2" });
    bot.busy = false;
    drainSteeredMessages(store, run);
    drainSteeredMessages(store, run);
    expect(run.mock.calls.map((call) => call[2])).toEqual(["check the build\n\nand the tests", "then deploy"]);
    expect(store.messages.map((message) => [message.text, message.callId])).toEqual([
      ["check the build", "call-1"],
      ["and the tests", "call-1"],
      ["then deploy", "call-2"],
    ]);
  });

  it("never puts a call id on a line that was not spoken", () => {
    const bot = fakeBot("bot-call-id-stray", "thread-call-id-stray", true);
    const store = fakeStore([bot]);
    const run = vi.fn();
    queueSteeredMessage(bot.id, bot.threadId, "typed with a stray id", { callId: "call-stray" });
    saveChatFollowup({
      id: "followup-stray-call-id", kind: "bot", ownerId: bot.id, threadId: bot.threadId,
      payload: { text: "typed by another build", prompt: "typed by another build", callId: "call-stray", queuedAt: Date.now() },
    });
    restoreSteeredMessages();
    bot.busy = false;
    drainSteeredMessages(store, run);
    expect(store.messages.map((message) => [message.text, message.via, message.callId])).toEqual([
      ["typed with a stray id", undefined, undefined],
      ["typed by another build", undefined, undefined],
    ]);
  });

  it("still loads and drains a durable row written before senders were kept", () => {
    const bot = fakeBot("bot-sender-legacy", "thread-sender-legacy", false);
    const store = fakeStore([bot]);
    const run = vi.fn();
    saveChatFollowup({
      id: "legacy-followup-without-sender", kind: "bot", ownerId: bot.id, threadId: bot.threadId,
      payload: { text: "queued by an older build", prompt: "queued by an older build" },
    });
    expect(() => restoreSteeredMessages()).not.toThrow();
    drainSteeredMessages(store, run);
    expect(store.messages).toEqual([expect.objectContaining({
      text: "queued by an older build", queueId: "legacy-followup-without-sender",
    })]);
    expect(store.messages[0].sender).toBeUndefined();
  });

  it("keeps queue operations and other listeners working when a listener throws", () => {
    const bot = fakeBot("bot-listener-error", "thread-listener-error", false);
    const store = fakeStore([bot]);
    const run = vi.fn();
    const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
    const unsubscribeBroken = onSteeredQueueChange(() => { throw new Error("notification failed"); });
    const listener = vi.fn();
    const unsubscribeHealthy = onSteeredQueueChange(listener);
    try {
      const keep = queueSteeredMessage(bot.id, bot.threadId, "still dispatch");
      const cancel = queueSteeredMessage(bot.id, bot.threadId, "cancel me");
      expect(cancelSteeredMessage(bot.id, cancel.id)).toBe(true);
      expect(() => drainSteeredMessages(store, run)).not.toThrow();
      expect(store.messages).toEqual([expect.objectContaining({ text: "still dispatch", queueId: keep.id })]);
      expect(run).toHaveBeenCalledTimes(1);
      expect(_queuedCount(bot.threadId)).toBe(0);
      expect(listener).toHaveBeenCalledTimes(4);
      expect(warn).toHaveBeenCalledTimes(4);
    } finally {
      unsubscribeBroken(); unsubscribeHealthy(); warn.mockRestore();
    }
  });

  it("exports owned public receipts without sharing internal queue state", () => {
    const owned = queueSteeredMessage("bot-public", "thread-public", "public words", {
      prompt: "private provider context", replyToId: "private-reply", sendId: "private-send", reason: "capacity",
    });
    const orphan = queueSteeredMessage("bot-orphan", "thread-orphan", "deleted task");
    try {
      const ownsThread = (botId: string, threadId: string) => botId === "bot-public" && threadId === "thread-public";
      const snapshot = queuedSteerSnapshot(ownsThread);
      expect(snapshot).toEqual({ "thread-public": [{ queueId: owned.id, text: "public words", reason: "capacity" }] });
      snapshot["thread-public"][0].text = "client mutation";
      snapshot["thread-public"].pop();
      expect(queuedSteerSnapshot(ownsThread)["thread-public"]).toEqual([{ queueId: owned.id, text: "public words", reason: "capacity" }]);
    } finally {
      cancelSteeredMessage("bot-public", owned.id);
      cancelSteeredMessage("bot-orphan", orphan.id);
    }
  });

  it("holds only the owning bot's queue by one of its own ids, and a held queue cannot drain", () => {
    const bot = fakeBot("bot-hold", "thread-hold", false);
    const store = fakeStore([bot]);
    const run = vi.fn();
    const queued = queueSteeredMessage(bot.id, bot.threadId, "steer me");
    queueSteeredMessage("bot-hold", "thread-hold", "second");
    try {
      expect(holdSteeredQueue("other-bot", bot.threadId, queued.id)).toBeNull();
      expect(holdSteeredQueue(bot.id, bot.threadId, "not-a-queue-id")).toBeNull();
      expect(holdSteeredQueue(bot.id, "thread-elsewhere", queued.id)).toBeNull();
      expect(_queuedCount(bot.threadId)).toBe(2);

      const held = holdSteeredQueue(bot.id, bot.threadId, queued.id);
      expect(held?.items.map((item) => item.text)).toEqual(["steer me", "second"]);
      expect(_queuedCount(bot.threadId)).toBe(0);
      drainSteeredMessages(store, run);
      expect(run).not.toHaveBeenCalled();

      restoreHeldSteeredQueue(held!);
      expect(_queuedCount(bot.threadId)).toBe(2);
      drainSteeredMessages(store, run);
      expect(run).toHaveBeenCalledTimes(1);
    } finally {
      cancelSteeredMessage(bot.id, queued.id);
    }
  });

  it("restores a held queue behind words queued while it was held", () => {
    const queued = queueSteeredMessage("bot-hold-merge", "thread-hold-merge", "held words");
    try {
      const held = holdSteeredQueue("bot-hold-merge", "thread-hold-merge", queued.id)!;
      const later = queueSteeredMessage("bot-hold-merge", "thread-hold-merge", "queued during the hold");
      restoreHeldSteeredQueue(held);
      const snapshot = queuedSteerSnapshot(() => true);
      expect(snapshot["thread-hold-merge"].map((item) => item.text)).toEqual([
        "held words",
        "queued during the hold",
      ]);
      cancelSteeredMessage("bot-hold-merge", later.id);
    } finally {
      cancelSteeredMessage("bot-hold-merge", queued.id);
    }
  });

  it("settling a held queue marks its durable rows delivered: a restart does not replay them", () => {
    const bot = fakeBot("bot-hold-settle", "thread-hold-settle", true);
    const first = queueSteeredMessage(bot.id, bot.threadId, "folded into the running turn");
    const second = queueSteeredMessage(bot.id, bot.threadId, "also folded");
    const held = holdSteeredQueue(bot.id, bot.threadId, first.id)!;
    settleHeldSteeredQueue(held);
    restoreSteeredMessages();
    expect(_queuedCount(bot.threadId)).toBe(0);
    expect(queuedSteerSnapshot(() => true)).toEqual({});
    cancelSteeredMessage(bot.id, second.id);
  });

  it("publishes enqueue/cancel/drain snapshots before a failed dispatch can leave stale chips", () => {
    const bot = fakeBot("bot-publish", "thread-publish", true);
    const snapshots: unknown[] = [];
    const unsubscribe = onSteeredQueueChange(() => snapshots.push(queuedSteerSnapshot((id) => id === bot.id)));
    try {
      const first = queueSteeredMessage(bot.id, bot.threadId, "keep");
      const second = queueSteeredMessage(bot.id, bot.threadId, "cancel");
      expect(cancelSteeredMessage("other-bot", second.id)).toBe(false);
      drainSteeredMessages(fakeStore([bot]), vi.fn());
      expect(snapshots).toHaveLength(2);
      expect(cancelSteeredMessage(bot.id, second.id)).toBe(true);
      bot.busy = false;
      expect(() => drainSteeredMessages(fakeStore([bot]), () => { throw new Error("failed dispatch"); })).not.toThrow();
      expect(snapshots).toEqual([
        { [bot.threadId]: [{ queueId: first.id, text: "keep" }] },
        { [bot.threadId]: [{ queueId: first.id, text: "keep" }, { queueId: second.id, text: "cancel" }] },
        { [bot.threadId]: [{ queueId: first.id, text: "keep" }] },
        {},
      ]);
    } finally { unsubscribe(); }
    const later = queueSteeredMessage(bot.id, bot.threadId, "unsubscribed");
    cancelSteeredMessage(bot.id, later.id);
    expect(snapshots).toHaveLength(4);
  });

  it("publishes removal when an orphaned queue is discarded", () => {
    queueSteeredMessage("bot-publish-orphan", "thread-publish-orphan", "gone");
    const listener = vi.fn();
    const unsubscribe = onSteeredQueueChange(listener);
    try {
      drainSteeredMessages(fakeStore([]), vi.fn());
      expect(listener).toHaveBeenCalledTimes(1);
      expect(queuedSteerSnapshot(() => true)).toEqual({});
    } finally { unsubscribe(); }
  });

  it("retains an idle task's queue until its runtime dispatch claim clears", () => {
    const bot = fakeBot("bot-handshake", "thread-handshake", false);
    const store = fakeStore([bot]);
    const run = vi.fn();
    const blocked = vi.fn(() => true);
    queueSteeredMessage(bot.id, bot.threadId, "after handshake");
    drainSteeredMessages(store, run, blocked);
    expect(blocked).toHaveBeenCalledWith(bot.id, bot.threadId, expect.anything());
    expect(run).not.toHaveBeenCalled();
    expect(store.messages).toHaveLength(0);
    expect(_queuedCount(bot.threadId)).toBe(1);
    blocked.mockReturnValue(false);
    drainSteeredMessages(store, run, blocked);
    expect(run).toHaveBeenCalledTimes(1);
    expect(store.messages.map((message) => message.text)).toEqual(["after handshake"]);
  });

  it("does not append a queued user message until drain", () => {
    const bot = fakeBot("bot-a", "thread-a", true);
    const store = fakeStore([bot]);
    const queued = queueSteeredMessage(bot.id, bot.threadId, "hold that thought");
    expect(queued).toMatchObject({ id: expect.any(String) });
    expect(store.messages).toHaveLength(0);
    expect(_queuedCount("thread-a")).toBe(1);

    bot.busy = false;
    const run = vi.fn();
    drainSteeredMessages(store, run);
    expect(store.messages).toHaveLength(1);
    expect(store.messages[0]).toMatchObject({
      role: "user",
      kind: "text",
      text: "hold that thought",
      queueId: queued.id,
    });
    expect(store.messages[0]!.queued).toBeUndefined();
    expect(run).toHaveBeenCalledTimes(1);
    expect(_queuedCount("thread-a")).toBe(0);
  });

  it("keeps a stable client receipt while a send waits to drain", () => {
    const bot = fakeBot("bot-receipt", "thread-receipt", true);
    const store = fakeStore([bot]);
    const queued = queueSteeredMessage(bot.id, bot.threadId, "retry safely", {
      replyToId: "reply-1",
      sendId: "send_1234567890123456",
    });

    expect(queuedSteeredMessage(bot.id, bot.threadId, "send_1234567890123456")).toEqual({
      id: queued.id,
      text: "retry safely",
      replyToId: "reply-1",
    });
    expect(queuedSteeredMessage("other-bot", bot.threadId, "send_1234567890123456")).toBeNull();

    bot.busy = false;
    drainSteeredMessages(store, vi.fn());
    expect(store.messages[0]).toMatchObject({
      text: "retry safely",
      sendId: "send_1234567890123456",
      queueId: queued.id,
    });
    expect(queuedSteeredMessage(bot.id, bot.threadId, "send_1234567890123456")).toBeNull();
  });

  it("keeps a pictures hold on the queued receipt so a retried send can say why", () => {
    const bot = fakeBot("bot-images-receipt", "thread-images-receipt", true);
    const queued = queueSteeredMessage(bot.id, bot.threadId, "see this", {
      sendId: "send_images_held_123456",
      blockedBy: "images",
    });
    expect(queuedSteeredMessage(bot.id, bot.threadId, "send_images_held_123456")).toEqual({
      id: queued.id,
      text: "see this",
      blockedBy: "images",
    });
  });

  it("holds the queue while the bot is busy and drains it once when idle", () => {
    const bot = fakeBot("bot-b", "thread-b", true);
    const store = fakeStore([bot]);
    const first = queueSteeredMessage(bot.id, bot.threadId, "first note");
    const second = queueSteeredMessage(bot.id, bot.threadId, "second note");
    const run = vi.fn();

    drainSteeredMessages(store, run);
    expect(run).not.toHaveBeenCalled();
    expect(store.messages).toHaveLength(0);
    expect(_queuedCount("thread-b")).toBe(2);

    bot.busy = false;
    drainSteeredMessages(store, run);
    expect(run).toHaveBeenCalledTimes(1);
    const [botId, threadId, prompt, userMessage] = run.mock.calls[0];
    expect(botId).toBe("bot-b");
    expect(threadId).toBe("thread-b");
    expect(prompt).toBe("first note\n\nsecond note");
    expect(store.messages.map((m) => m.text)).toEqual(["first note", "second note"]);
    expect(store.messages.map((m) => m.queueId)).toEqual([first.id, second.id]);
    expect(userMessage.text).toBe("second note");
    expect(run.mock.calls[0][4]).toEqual(store.messages.map((m) => m.id));
    expect(store.messages.every((m) => !m.queued)).toBe(true);
    expect(_queuedCount("thread-b")).toBe(0);

    drainSteeredMessages(store, run);
    expect(run).toHaveBeenCalledTimes(1);
  });

  it("tells whether one send still waits in its thread's queue", () => {
    const bot = fakeBot("bot-waiting", "thread-waiting", true);
    const store = fakeStore([bot]);
    const kept = queueSteeredMessage(bot.id, bot.threadId, "keep waiting");
    const edited = queueSteeredMessage(bot.id, bot.threadId, "edited away");
    expect(isSteeredMessageQueued(bot.id, bot.threadId, kept.id)).toBe(true);
    expect(isSteeredMessageQueued("other-bot", bot.threadId, kept.id)).toBe(false);
    expect(isSteeredMessageQueued(bot.id, "other-thread", kept.id)).toBe(false);
    expect(cancelSteeredMessage(bot.id, edited.id, bot.threadId)).toBe(true);
    expect(isSteeredMessageQueued(bot.id, bot.threadId, edited.id)).toBe(false);
    expect(isSteeredMessageQueued(bot.id, bot.threadId, kept.id)).toBe(true);
    bot.busy = false;
    drainSteeredMessages(store, vi.fn());
    expect(store.messages.map((m) => m.queueId)).toEqual([kept.id]);
    expect(isSteeredMessageQueued(bot.id, bot.threadId, kept.id)).toBe(false);
  });

  it("drops a cancelled message so drain does not send it", () => {
    const bot = fakeBot("bot-cancel", "thread-cancel", true);
    const store = fakeStore([bot]);
    const first = queueSteeredMessage(bot.id, bot.threadId, "keep me");
    const second = queueSteeredMessage(bot.id, bot.threadId, "drop me");
    expect(cancelSteeredMessage(bot.id, second.id)).toBe(true);
    expect(cancelSteeredMessage(bot.id, "missing")).toBe(false);
    expect(_queuedCount("thread-cancel")).toBe(1);

    bot.busy = false;
    const run = vi.fn();
    drainSteeredMessages(store, run);
    expect(run).toHaveBeenCalledTimes(1);
    expect(run.mock.calls[0][2]).toBe("keep me");
    expect(store.messages.map((m) => m.queueId)).toEqual([first.id]);
    expect(_queuedCount("thread-cancel")).toBe(0);
  });

  it("keeps reply metadata and the provider-facing reply prompt while queued", () => {
    const bot = fakeBot("bot-reply", "thread-reply", true);
    const store = fakeStore([bot]);
    queueSteeredMessage(bot.id, bot.threadId, "That part", {
      replyToId: "original-message",
      prompt: "Reply context\nThat part",
    });
    bot.busy = false;
    const run = vi.fn();
    drainSteeredMessages(store, run);
    expect(store.messages[0]).toMatchObject({ text: "That part", replyToId: "original-message" });
    expect(run.mock.calls[0][2]).toBe("Reply context\nThat part");
  });

  it("cancels a pinned-task queue after the bot switches without crossing bot ownership", () => {
    const bot = fakeBot("bot-switch-cancel", "thread-original-cancel", true);
    const queued = queueSteeredMessage(bot.id, bot.threadId, "cancel on the old task");

    bot.threadId = "thread-new-cancel";
    expect(cancelSteeredMessage("some-other-bot", queued.id)).toBe(false);
    expect(cancelSteeredMessage(bot.id, queued.id, bot.threadId)).toBe(false);
    expect(_queuedCount("thread-original-cancel")).toBe(1);
    expect(cancelSteeredMessage(bot.id, queued.id, "thread-original-cancel")).toBe(true);
    expect(_queuedCount("thread-original-cancel")).toBe(0);
  });

  it("fires nothing when nothing is queued", () => {
    const run = vi.fn();
    drainSteeredMessages(fakeStore([fakeBot("bot-c", "thread-c", false)]), run);
    expect(run).not.toHaveBeenCalled();
  });

  it("drops the queue of a deleted bot without running it", () => {
    const bot = fakeBot("bot-d", "thread-d", true);
    queueSteeredMessage(bot.id, bot.threadId, "orphaned");
    const run = vi.fn();
    drainSteeredMessages(fakeStore([]), run);
    expect(run).not.toHaveBeenCalled();
    expect(_queuedCount("thread-d")).toBe(0);
  });

  it("drains an idle task B while the same bot's task A stays busy", () => {
    const bot = fakeBot("bot-independent", "thread-independent-a", true);
    const taskA = { ...bot };
    const taskB = { ...bot, threadId: "thread-independent-b", busy: false };
    const store = fakeStore([bot]);
    store.projectBotForTask = (_botId, threadId) => threadId === taskA.threadId ? taskA : taskB;
    queueSteeredMessage(bot.id, taskA.threadId, "wait for A");
    queueSteeredMessage(bot.id, taskB.threadId, "run B");
    const run = vi.fn();

    drainSteeredMessages(store, run);
    expect(run).toHaveBeenCalledTimes(1);
    expect(run.mock.calls[0].slice(0, 3)).toEqual([bot.id, taskB.threadId, "run B"]);
    expect(_queuedCount(taskA.threadId)).toBe(1);
    expect(_queuedCount(taskB.threadId)).toBe(0);

    taskA.busy = false;
    drainSteeredMessages(store, run);
    expect(run).toHaveBeenCalledTimes(2);
    expect(run.mock.calls[1].slice(0, 3)).toEqual([bot.id, taskA.threadId, "wait for A"]);
  });

  it("drops a deleted task's queue even when its bot still exists", () => {
    const bot = fakeBot("bot-deleted-task", "thread-deleted-task", false);
    const store = fakeStore([bot]);
    store.projectBotForTask = () => null;
    queueSteeredMessage(bot.id, bot.threadId, "do not resurrect this task");
    const run = vi.fn();

    drainSteeredMessages(store, run);
    expect(run).not.toHaveBeenCalled();
    expect(store.messages).toHaveLength(0);
    expect(_queuedCount(bot.threadId)).toBe(0);
  });

});

describe("steer-queue e2e (fake ACP fleet)", () => {
  let child: ChildProcess;
  let home: string;
  let base = "";
  let stderr = "";
  let drainGate: string;
  let stopGate: string;
  let stopRpcDump: string;
  let earlyGate: string;
  let receiptGate: string;
  let dispatchGate: string;
  let roomGate: string;
  let roomRpcAppend: string;
  const evidence: unknown[] = [];
  let evidencePath: string;

  type ApiBody = Record<
    string,
    | string
    | boolean
    | string[]
    | { instanceId: string; model: string }
    | { bulletin: string; defaultResponder: { kind: string; botId: string } }
  >;

  const api = async (method: string, path: string, body?: ApiBody): Promise<{ status: number; body: any }> => {
    const res = await fetch(`${base}${path}`, {
      method,
      headers: body ? { "content-type": "application/json" } : undefined,
      body: body ? JSON.stringify(body) : undefined,
    });
    const result = { status: res.status, body: await res.json() };
    if (method !== "GET") evidence.push({ method, path, body, result });
    return result;
  };

  const botById = async (id: string) =>
    (await api("GET", "/api/bots")).body.bots.find((b: any) => b.id === id);

  const groupById = async (id: string) =>
    (await api("GET", "/api/bots?messages=0")).body.groups.find((g: any) => g.id === id);

  const echoes = (bot: any): any[] =>
    bot.messages.filter((m: any) => m.role === "bot" && m.kind === "text" && m.text?.startsWith("echo: "));

  const until = async (probe: () => Promise<boolean>, what: string, timeoutMs = 30_000) => {
    const deadline = Date.now() + timeoutMs;
    for (;;) {
      if (await probe()) return;
      if (Date.now() > deadline) throw new Error(`${what} never happened. stderr: ${stderr.slice(-2000)}`);
      await new Promise((r) => setTimeout(r, 200));
    }
  };

  const newBot = async (instanceId: string, name: string) => {
    return (await api("POST", "/api/bots", { name, modelSelection: { instanceId, model: "fake-model" } })).body.bot;
  };

  beforeAll(async () => {
    chmodSync(FAKE_CLI, 0o755);
    home = mkdtempSync(join(tmpdir(), "omb-steer-test-"));
    mkdirSync(join(home, ".openmausbot"), { recursive: true });
    mkdirSync(join(home, "gates"), { recursive: true });
    drainGate = join(home, "gates", "drain.gate");
    stopGate = join(home, "gates", "stop.gate");
    stopRpcDump = join(home, "gates", "stop.rpc");
    earlyGate = join(home, "gates", "early-provider.gate");
    receiptGate = join(home, "gates", "receipt-provider.gate");
    dispatchGate = join(home, "gates", "early-dispatch.gate");
    roomGate = join(home, "gates", "room.gate");
    roomRpcAppend = join(home, "gates", "room.rpc-append");
    evidencePath = join(tmpdir(), `omb-steer-evidence-${Date.now()}-${process.pid}.json`);
    // Delay only the adapter's returned acknowledgment, reproducing completion before sendTurn resolves.
    const prelude = join(home, "delayed-dispatch.mjs");
    writeFileSync(prelude, [
      `import { ProviderRegistry } from ${JSON.stringify(pathToFileURL(join(SERVER_DIR, "harness/registry.ts")).href)};`,
      `import { EventBus } from ${JSON.stringify(pathToFileURL(join(SERVER_DIR, "harness/bus.ts")).href)};`,
      'import { existsSync, writeFileSync } from "node:fs";',
      'import { randomUUID } from "node:crypto";',
      'let oldCompletion, receiptBus;',
      'const publish = EventBus.prototype.publish;',
      'EventBus.prototype.publish = function(event) {',
      '  if (event.providerInstanceId === "steerReceipt" && event.type === "turn.completed" && !oldCompletion) { oldCompletion = event; receiptBus = this; }',
      '  return publish.call(this, event);',
      '};',
      'const load = ProviderRegistry.prototype.load;',
      'ProviderRegistry.prototype.load = async function(configs) {',
      '  await load.call(this, configs);',
      '  const instance = this.get("steerEarly");',
      '  if (!instance) return;',
      '  const send = instance.adapter.sendTurn;',
      '  instance.adapter.sendTurn = async (turn) => {',
      '    const result = await send(turn);',
      `    writeFileSync(${JSON.stringify(`${dispatchGate}.entered`)}, "entered");`,
      `    while (!existsSync(${JSON.stringify(dispatchGate)})) await new Promise(resolve => setTimeout(resolve, 20));`,
      '    return result;',
      '  };',
      '  const receipt = this.get("steerReceipt");',
      '  if (!receipt) return;',
      '  const sendReceipt = receipt.adapter.sendTurn;',
      '  let calls = 0;',
      '  receipt.adapter.sendTurn = async (turn) => {',
      '    const replacement = ++calls === 2;',
      '    if (replacement) {',
      // Replay a completion from the old provider turn after the replacement installed its generation but before its ACK.
      '      publish.call(receiptBus, { ...oldCompletion, eventId: randomUUID(), createdAt: new Date().toISOString() });',
      `      writeFileSync(${JSON.stringify(`${receiptGate}.late`)}, "old completion delivered");`,
      `      while (!existsSync(${JSON.stringify(`${receiptGate}.dispatch`)})) await new Promise(resolve => setTimeout(resolve, 20));`,
      '    }',
      '    const result = await sendReceipt(turn);',
      `    while (replacement && !existsSync(${JSON.stringify(`${receiptGate}.ack`)})) await new Promise(resolve => setTimeout(resolve, 20));`,
      '    return result;',
      '  };',
      '};',
    ].join("\n"));
    writeFileSync(
      join(home, ".openmausbot", "config.json"),
      JSON.stringify({
        instances: {
          steer: {
            driver: "grokAgent",
            environment: { FAKE_ACP_MODE: "echo-gated", FAKE_ACP_GATE_FILE: drainGate },
            config: { cli: FAKE_CLI, fullAuto: true },
          },
          steerEarly: {
            driver: "grokAgent",
            environment: { FAKE_ACP_MODE: "echo-gated", FAKE_ACP_GATE_FILE: earlyGate },
            config: { cli: FAKE_CLI, fullAuto: true },
          },
          steerReceipt: {
            driver: "grokAgent",
            environment: { FAKE_ACP_MODE: "echo-gated", FAKE_ACP_GATE_FILE: receiptGate },
            config: { cli: FAKE_CLI, fullAuto: true },
          },
          // Wait for session/prompt to be in flight: an earlier interrupt is a no-op on a turn the driver has not registered.
          steerStop: {
            driver: "grokAgent",
            environment: {
              FAKE_ACP_MODE: "echo-gated",
              FAKE_ACP_GATE_FILE: stopGate,
              FAKE_ACP_RPC_DUMP: stopRpcDump,
            },
            config: { cli: FAKE_CLI, fullAuto: true },
          },
          // One gate holds the room's turn open while a 1:1 message arrives.
          steerRoom: {
            driver: "grokAgent",
            environment: { FAKE_ACP_MODE: "echo-gated", FAKE_ACP_GATE_FILE: roomGate, FAKE_ACP_RPC_APPEND_FILE: roomRpcAppend },
            config: { cli: FAKE_CLI, fullAuto: true },
          },
        },
      }),
    );

    const port = await freePortBlock([0, 1]);
    base = `http://127.0.0.1:${port}`;
    const env: NodeJS.ProcessEnv = {
      HOME: home,
      USERPROFILE: home,
      OMB_PORT: String(port),
    };
    if (process.env.PATH) env.PATH = process.env.PATH;
    // Without SystemRoot, winsock fails to initialize in the child.
    if (process.env.SystemRoot) env.SystemRoot = process.env.SystemRoot;
    child = spawn(process.execPath, ["--import", pathToFileURL(prelude).href, join(SERVER_DIR, "index.ts")], {
      cwd: join(SERVER_DIR, ".."),
      env,
      stdio: ["ignore", "pipe", "pipe"],
    });
    child.stderr!.on("data", (c) => (stderr += c));

    const deadline = Date.now() + 20_000;
    for (;;) {
      try {
        const res = await fetch(`${base}/api/health`);
        if (res.ok) break;
      } catch {
      }
      if (Date.now() > deadline) throw new Error(`server never came up. stderr:\n${stderr}`);
      if (child.exitCode !== null) throw new Error(`server exited ${child.exitCode}. stderr:\n${stderr}`);
      await new Promise((r) => setTimeout(r, 150));
    }
  }, 30_000);

  afterAll(async () => {
    // Windows' SIGTERM ends only the server, so its engines would keep their working folders in the home open:
    // the whole tree goes, and is gone, before the home is deleted.
    if (child) await killCliTree(child, 15_000);
    writeFileSync(evidencePath, JSON.stringify({ url: base, fixtureHome: home, requests: evidence, stderr }, null, 2));
    console.info(JSON.stringify({ evidencePath }));
    if (home) await removeTempDir(home);
  });

  it("drains exactly once after completion precedes the dispatch acknowledgment", async () => {
    const bot = await newBot("steerEarly", "Early completion");
    expect((await api("POST", `/api/bots/${bot.id}/messages`, { text: "first before acknowledgment" })).status).toBe(202);
    await until(async () => existsSync(`${dispatchGate}.entered`), "the pending dispatch acknowledgment");
    expect((await api("POST", `/api/bots/${bot.id}/messages`, { text: "queued after acknowledgment" })).body.queued).toBe(true);
    writeFileSync(earlyGate, "complete provider turn");
    await until(async () => echoes(await botById(bot.id)).length === 1, "completion before acknowledgment");
    const pending = await botById(bot.id);
    expect(pending.messages.filter((message: any) => message.role === "user").map((message: any) => message.text))
      .toEqual(["first before acknowledgment"]);
    expect(pending.messages.some((message: any) => message.tool?.name?.includes("queued message could not start"))).toBe(false);
    writeFileSync(dispatchGate, "acknowledge provider dispatch");
    await until(async () => echoes(await botById(bot.id)).length === 2, "the queued turn after acknowledgment");
    const final = await botById(bot.id);
    expect(final.messages.filter((message: any) => message.role === "user").map((message: any) => message.text))
      .toEqual(["first before acknowledgment", "queued after acknowledgment"]);
    expect(echoes(final)[1].text).toContain("queued after acknowledgment");
    expect(final.messages.some((message: any) => message.tool?.name?.includes("queued message could not start"))).toBe(false);
    evidence.push({ earlyCompletionQueue: { pending, final } });
  }, 30_000);

  it("retains a replacement receipt through stale completion and settles its own completion before ACK", async () => {
    const bot = await newBot("steerReceipt", "Receipt generation");
    expect((await api("POST", `/api/bots/${bot.id}/messages`, { text: "first receipt turn" })).status).toBe(202);
    const body = { threadId: bot.threadId, text: "queued replacement", sendId: "replacement_receipt_123456" };
    const queued = await api("POST", `/api/bots/${bot.id}/messages`, body);
    expect(queued.body).toMatchObject({ queued: true, queueId: expect.any(String) });
    const receipt = () => {
      const database = new DatabaseSync(join(home, ".openmausbot", "messages.db"), { readOnly: true });
      try { return database.prepare("SELECT status FROM chat_followups WHERE id = ?").get(queued.body.queueId); }
      finally { database.close(); }
    };
    writeFileSync(receiptGate, "finish the old turn");
    await until(async () => existsSync(`${receiptGate}.late`), "old completion during replacement setup");
    expect(receipt()).toEqual({ status: "dispatching" });
    writeFileSync(`${receiptGate}.dispatch`, "start replacement provider");
    await until(async () => echoes(await botById(bot.id)).length === 2, "replacement completion before its ACK");
    expect(receipt()).toEqual({ status: "dispatching" });
    writeFileSync(`${receiptGate}.ack`, "acknowledge replacement");
    await until(async () => receipt() === undefined, "receipt settlement by the replacement's exact provider turn");
    const retried = await api("POST", `/api/bots/${bot.id}/messages`, body);
    expect(retried.body.message).toMatchObject({ queueId: queued.body.queueId, sendId: body.sendId });
    expect(echoes(await botById(bot.id))).toHaveLength(2);
    evidence.push({ receiptGeneration: { queued, retried, final: await botById(bot.id) } });
  }, 30_000);

  it(
    "queues sends while busy and drains them into exactly one attended turn",
    async () => {
      const bot = await newBot("steer", "Steerable");

      const first = await api("POST", `/api/bots/${bot.id}/messages`, { text: "first task please" });
      expect(first.status).toBe(202);
      expect(first.body.queued).toBeUndefined();
      expect((await botById(bot.id)).busy).toBe(true);

      const second = await api("POST", `/api/bots/${bot.id}/messages`, { text: "steer two" });
      expect(second.status).toBe(202);
      expect(second.body).toMatchObject({ ok: true, queued: true });
      const third = await api("POST", `/api/bots/${bot.id}/messages`, { text: "steer three" });
      expect(third.body.queued).toBe(true);

      let snapshot = await botById(bot.id);
      expect(snapshot.busy).toBe(true);
      expect(snapshot.messages.filter((m: any) => m.role === "user").map((m: any) => m.text)).toEqual([
        "first task please",
      ]);
      expect(echoes(snapshot)).toHaveLength(0);

      writeFileSync(drainGate, "open");
      await until(async () => {
        snapshot = await botById(bot.id);
        return !snapshot.busy && echoes(snapshot).length >= 2;
      }, "the queued turn");

      const replies = echoes(snapshot);
      expect(replies).toHaveLength(2);
      expect(replies[0].text).toContain("first task please");
      expect(replies[1].text).toContain("steer two\n\nsteer three");
      expect(replies[1].text).not.toContain("authenticated outside event");
      expect(replies[1].text).not.toContain("[The user rewound");
      const userTexts = snapshot.messages.filter((m: any) => m.role === "user").map((m: any) => m.text);
      expect(userTexts).toEqual(["first task please", "steer two", "steer three"]);
      expect(snapshot.messages.some((m: any) => m.queued)).toBe(false);

      const followUp = await api("POST", `/api/bots/${bot.id}/messages`, { text: "plain follow-up" });
      expect(followUp.body.queued).toBeUndefined();
      await until(async () => {
        snapshot = await botById(bot.id);
        return !snapshot.busy && echoes(snapshot).length >= 3;
      }, "the follow-up turn");
      expect(echoes(snapshot)).toHaveLength(3);
    },
    60_000,
  );

  it(
    "drains the queue after an interrupt — stop-then-steer",
    async () => {
      const bot = await newBot("steerStop", "Stoppable");

      const first = await api("POST", `/api/bots/${bot.id}/messages`, { text: "long job" });
      expect(first.status).toBe(202);
      expect((await botById(bot.id)).busy).toBe(true);

      const queued = await api("POST", `/api/bots/${bot.id}/messages`, { text: "after stop please" });
      expect(queued.body.queued).toBe(true);

      await until(async () => {
        try {
          return readFileSync(stopRpcDump, "utf8").includes("session/prompt");
        } catch {
          return false;
        }
      }, "the hung prompt");
      expect((await api("POST", `/api/bots/${bot.id}/interrupt`)).status).toBe(200);

      await until(async () => {
        const snapshot = await botById(bot.id);
        const message = snapshot.messages.find((m: any) => m.text === "after stop please");
        return Boolean(message) && !message.queued;
      }, "the post-interrupt drain");

      writeFileSync(stopGate, "open");
      let snapshot: any;
      await until(async () => {
        snapshot = await botById(bot.id);
        return !snapshot.busy && echoes(snapshot).length >= 1;
      }, "the steered turn");

      const replies = echoes(snapshot);
      expect(replies).toHaveLength(1);
      expect(replies[0].text).toContain("after stop please");
    },
    60_000,
  );

  it(
    "starts a person's 1:1 turn while the bot's room turn is still open",
    async () => {
      const bot = await newBot("steerRoom", "RoomBusy");
      const roomPrompts = () => {
        try {
          return readFileSync(roomRpcAppend, "utf8").trim().split("\n")
            .map((line) => JSON.parse(line))
            .filter((entry: any) => entry.method === "session/prompt");
        } catch {
          return [];
        }
      };

      // The room turn stays open until the gate exists, so the room holds the bot.
      const room = (await api("POST", "/api/groups", {
        name: "Ops Room",
        memberIds: [bot.id],
        setup: { bulletin: "", defaultResponder: { kind: "member", botId: bot.id } },
      })).body.group;
      expect((await api("POST", `/api/groups/${room.id}/messages`, { text: "room work" })).status).toBe(202);
      await until(async () => (await groupById(room.id))?.working === true, "the room turn to start");
      await until(() => Promise.resolve(roomPrompts().length === 1), "the room prompt in flight");

      const direct = await api("POST", `/api/bots/${bot.id}/messages`, { text: "meanwhile, direct words" });
      expect(direct.status).toBe(202);
      expect(direct.body.queued).toBeUndefined();
      // A second prompt is in flight on the same provider while the room turn is still open.
      await until(() => Promise.resolve(roomPrompts().length === 2), "the 1:1 prompt in flight beside the room turn");
      expect((await groupById(room.id))?.working).toBe(true);
      const during = await botById(bot.id);
      expect(during.tasks.find((task: any) => task.threadId === bot.threadId)?.busy).toBe(true);

      writeFileSync(roomGate, "open");
      await until(async () => (await groupById(room.id))?.working === false, "the room turn to finish");
      await until(async () => {
        const after = await botById(bot.id);
        return !after.busy && echoes(after).some((reply) => reply.text.includes("meanwhile, direct words"));
      }, "the 1:1 turn");

      const after = await botById(bot.id);
      const directEchoes = echoes(after).filter((reply) => reply.text.includes("meanwhile, direct words"));
      expect(directEchoes).toHaveLength(1);
      expect(after.messages.filter((message: any) => message.role === "user").map((message: any) => message.text))
        .toEqual(["meanwhile, direct words"]);
      evidence.push({ roomTurnOverlap: { direct, during, after } });
    },
    60_000,
  );
});