Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

30.7 KB
Raw Blame
import { test } from "node:test";
import assert from "node:assert/strict";
import {
  AGENT_OPENED_TEXT,
  INTERRUPTED_TOOL_RESULT,
  recordedMessageTimestamps,
  planColdStartSeed,
  reconstructMessagesFromHistory,
  renderOverheard,
  replayPreamble,
  seedPriorTurns,
  selectOverheardToImport,
} from "../src/harness/replay.ts";
import type { ConversationTurn, OverheardMessage, SessionEntry } from "../src/types.ts";

function entry(type: SessionEntry["type"], text: string): SessionEntry {
  return {
    sessionId: "s",
    seq: 0,
    parentSeq: null,
    type,
    payload: { text },
    scopeLabel: "org:default-org",
    createdAt: 0,
  };
}

function overheardEntry(ts: string, name: string, text: string, files?: string[]): SessionEntry {
  return {
    sessionId: "s",
    seq: 0,
    parentSeq: null,
    type: "user",
    payload: { overheard: true, ts, name, text, ...(files ? { files } : {}) },
    scopeLabel: "org:default-org",
    createdAt: Number(ts) || 0,
  };
}

function ent(type: SessionEntry["type"], payload: unknown, seq = 0): SessionEntry {
  return { sessionId: "s", seq, parentSeq: null, type, payload, scopeLabel: "org:default-org", createdAt: seq };
}

function assertValidWire(msgs: ReturnType<typeof reconstructMessagesFromHistory>): void {
  if (!msgs.length) return;
  assert.equal(msgs[0]!.role, "user", "first message must be user-side");
  const open = new Set<string>();
  for (const m of msgs) {
    if (m.role === "assistant") for (const b of m.content) if (b.type === "toolCall") open.add(b.id);
    if (m.role === "toolResult") {
      assert.ok(open.has(m.toolCallId), "tool_result pairs an open tool_use");
      open.delete(m.toolCallId);
    }
  }
  assert.equal(open.size, 0, "every tool_use has a matching tool_result");
}

test("replayPreamble renders user and assistant entries", () => {
  const out = replayPreamble([entry("user", "send me a pirate flag"), entry("assistant", "done!")]);
  assert.match(out, /User: send me a pirate flag/);
  assert.match(out, /Assistant: done!/);
});

test("replayPreamble is empty when there is nothing to replay", () => {
  assert.equal(replayPreamble([]), "");
});

test("replayPreamble EXCLUDES thinking from cold-start context (signature is stored but never replayed)", () => {
  const thinking: SessionEntry = {
    sessionId: "s",
    seq: 1,
    parentSeq: null,
    type: "thinking",
    payload: { thinking: "secret chain of thought", thinkingSignature: "OPAQUE-SIG-BYTES" },
    scopeLabel: "org:default-org",
    createdAt: 0,
  };
  const out = replayPreamble([entry("user", "hi"), thinking, entry("assistant", "hello")]);
  assert.match(out, /User: hi/);
  assert.match(out, /Assistant: hello/);
  assert.doesNotMatch(out, /secret chain of thought/);
  assert.doesNotMatch(out, /OPAQUE-SIG-BYTES/);
});

test("reconstructMessagesFromHistory rebuilds a faithful tool round — a fact in a tool result survives", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "sign me up for HN" }, 1),
    ent("tool_call", { tool: "execute", command: "browse signup", callId: "c1" }, 2),
    ent(
      "tool_result",
      { tool: "execute", callId: "c1", result: "username: agent42\npassword: hunter2", isError: false },
      3,
    ),
    ent("assistant", { text: "Done — account created." }, 4),
  ];
  const msgs = reconstructMessagesFromHistory(history);
  assertValidWire(msgs);
  assert.deepEqual(
    msgs.map((m) => m.role),
    ["user", "assistant", "toolResult", "assistant"],
  );
  const call = msgs[1] as {
    role: "assistant";
    content: Array<{ type: string; id?: string; name?: string; arguments?: unknown }>;
  };
  assert.equal(call.content[0]!.type, "toolCall");
  assert.equal(call.content[0]!.id, "c1");
  assert.equal(call.content[0]!.name, "execute");
  assert.deepEqual(call.content[0]!.arguments, { command: "browse signup" });
  const result = msgs[2] as {
    role: "toolResult";
    toolCallId: string;
    content: Array<{ text: string }>;
    isError: boolean;
  };
  assert.equal(result.toolCallId, "c1");
  assert.match(result.content[0]!.text, /password: hunter2/);
  assert.equal(result.isError, false);
});

test("reconstructMessagesFromHistory SKIPS legacy tool entries with no callId (degrades to no tool replay)", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "hi" }, 1),
    ent("tool_call", { tool: "read", path: "a.txt" }, 2),
    ent("tool_result", { tool: "read", path: "a.txt", found: true }, 3),
    ent("assistant", { text: "ok" }, 4),
  ];
  const msgs = reconstructMessagesFromHistory(history);
  assertValidWire(msgs);
  assert.deepEqual(
    msgs.map((m) => m.role),
    ["user", "assistant"],
  );
  assert.ok(!msgs.some((m) => m.role === "assistant" && m.content.some((c) => c.type === "toolCall")));
});

test("a tool call with no recorded result replays with a synthetic interrupted result (turn died mid-call)", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "build and push the release" }, 1),
    ent("tool_call", { tool: "execute", command: "make build", callId: "c1" }, 2),
    ent("tool_result", { tool: "execute", callId: "c1", result: "built ok", isError: false }, 3),
    ent("tool_call", { tool: "execute", command: "git push", callId: "c2" }, 4),
  ];
  const msgs = reconstructMessagesFromHistory(history);
  assertValidWire(msgs);
  assert.deepEqual(
    msgs.map((m) => m.role),
    ["user", "assistant", "toolResult", "assistant", "toolResult"],
  );
  const synthetic = msgs[4] as {
    role: "toolResult";
    toolCallId: string;
    content: Array<{ text: string }>;
    isError: boolean;
  };
  assert.equal(synthetic.toolCallId, "c2");
  assert.equal(synthetic.isError, true);
  assert.equal(synthetic.content[0]!.text, INTERRUPTED_TOOL_RESULT);
});

test("a user turn that lands right after a tool result stays adjacent — no words put in the assistant's mouth", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "check the build" }, 1),
    ent("tool_call", { tool: "execute", command: "make build", callId: "c1" }, 2),
    ent("tool_result", { tool: "execute", callId: "c1", result: "built ok", isError: false }, 3),
    ent("user", { text: "actually, ship it" }, 4),
  ];
  const msgs = reconstructMessagesFromHistory(history);
  assertValidWire(msgs);
  assert.deepEqual(
    msgs.map((m) => m.role),
    ["user", "assistant", "toolResult", "user"],
  );
  const assistantText = msgs
    .filter((m) => m.role === "assistant")
    .flatMap((m) => m.content)
    .filter((c) => c.type === "text")
    .map((c) => (c as { text: string }).text);
  assert.deepEqual(assistantText, []);
});

test("reconstructMessagesFromHistory replays a delivery as the agent's deliver call and keeps legacy file notes", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "make a flag" }, 1),
    ent("assistant", { text: "here you go" }, 2),
    ent("delivery", { text: "flag.png (image/png, 100 bytes)" }, 3),
    ent("tool_call", { tool: "deliver", callId: "cron:1", text: "flag.png is ready", via: "cron" }, 4),
    ent("tool_result", { tool: "deliver", callId: "cron:1", result: "delivered" }, 5),
    ent("user", { text: "thanks" }, 6),
  ];
  const msgs = reconstructMessagesFromHistory(history);
  assertValidWire(msgs);
  assert.deepEqual(
    msgs.map((m) => m.role),
    ["user", "assistant", "user", "assistant", "toolResult", "user"],
  );
  assert.deepEqual(msgs[2]!.content, [
    { type: "text", text: "[files delivered to the conversation: flag.png (image/png, 100 bytes)]" },
  ]);
  assert.deepEqual(msgs[3]!.content, [
    { type: "toolCall", id: "cron:1", name: "deliver", arguments: { text: "flag.png is ready", via: "cron" } },
  ]);
  assert.deepEqual(msgs[4]!.content, [{ type: "text", text: "delivered" }]);
});

test("reconstructMessagesFromHistory opens with a fixed user frame when the agent spoke first", () => {
  const msgs = reconstructMessagesFromHistory([
    ent("assistant", { text: "daily digest", deliveryKey: "cron:9" }, 1),
    ent("user", { text: "thanks" }, 2),
  ]);
  assertValidWire(msgs);
  assert.deepEqual(
    msgs.map((m) => [m.role, m.content]),
    [
      ["user", [{ type: "text", text: AGENT_OPENED_TEXT }]],
      ["assistant", [{ type: "text", text: "daily digest" }]],
      ["user", [{ type: "text", text: "── Wednesday, December 31, 1969 ──\nthanks" }]],
    ],
  );
});

test("transcript replay can start at recorded assistant work without a synthetic user", () => {
  const messages = reconstructMessagesFromHistory([entry("assistant", "Already worked")], { requireUserStart: false });
  assert.deepEqual(
    messages.map((message) => message.role),
    ["assistant"],
  );
});

test("reconstructMessagesFromHistory coalesces consecutive assistant text entries to keep alternation", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "hi" }, 1),
    ent("assistant", { text: "part one" }, 2),
    ent("assistant", { text: "part two" }, 3),
    ent("user", { text: "ok" }, 4),
  ];
  const msgs = reconstructMessagesFromHistory(history);
  assertValidWire(msgs);
  assert.deepEqual(
    msgs.map((m) => m.role),
    ["user", "assistant", "user"],
  );
  const assistantText = (msgs[1] as { content: Array<{ text: string }> }).content.map((c) => c.text).join(" ");
  assert.match(assistantText, /part one/);
  assert.match(assistantText, /part two/);
});

test("reconstructMessagesFromHistory replays a context summary as a user turn and stays valid across two tool turns", () => {
  const history: SessionEntry[] = [
    ent("system", { kind: "context_summary", throughSeq: 0, text: "Earlier: set up the project." }, 1),
    ent("user", { text: "now deploy" }, 2),
    ent("tool_call", { tool: "execute", command: "deploy", callId: "d1" }, 3),
    ent("tool_result", { tool: "execute", callId: "d1", result: "deployed v4", isError: false }, 4),
    ent("assistant", { text: "shipped v4" }, 5),
    ent("user", { text: "and the logs?" }, 6),
    ent("tool_call", { tool: "execute", command: "logs", callId: "d2" }, 7),
    ent("tool_result", { tool: "execute", callId: "d2", result: "all green", isError: false }, 8),
    ent("assistant", { text: "logs are green" }, 9),
  ];
  const msgs = reconstructMessagesFromHistory(history);
  assertValidWire(msgs);
  assert.equal(msgs[0]!.role, "user");
  assert.match((msgs[0] as { content: Array<{ text: string }> }).content[0]!.text, /Earlier: set up the project/);
  assert.equal(msgs.filter((m) => m.role === "toolResult").length, 2);
});

test("reconstructMessagesFromHistory is empty for empty history (fresh session — nothing to replay)", () => {
  assert.deepEqual(reconstructMessagesFromHistory([]), []);
});

test("planColdStartSeed prefers the faithful rebuild; priorTurns only bootstraps an empty durable log", () => {
  const oneMsg = [{ role: "user" as const, content: [{ type: "text" as const, text: "hi" }], timestamp: 0 }];
  assert.equal(planColdStartSeed(oneMsg, true), "structured");
  assert.equal(planColdStartSeed(oneMsg, false), "structured");
  assert.equal(planColdStartSeed([], true), "priorTurns");
  assert.equal(planColdStartSeed(null, true), "priorTurns");
  assert.equal(planColdStartSeed(null, false), "preamble");
  assert.equal(planColdStartSeed([], false), "none");
});

test("seedPriorTurns renders thread history as author-attributed <message> tags (own = from agent)", () => {
  const turns: ConversationTurn[] = [
    { role: "user", name: "alice", text: "[~abc] can you deploy?" },
    { role: "assistant", text: "on it" },
  ];
  const seeded = seedPriorTurns(turns);
  assert.equal(seeded.length, 1);
  assert.equal(seeded[0]!.role, "user");
  assert.match(seeded[0]!.text, /<message from="human" author="alice">\[~abc\] can you deploy\?<\/message>/);
  assert.match(seeded[0]!.text, /<message from="agent">on it<\/message>/);
});

test("seedPriorTurns DEDUPS a turn already present in the session's in-session history", () => {
  const turns: ConversationTurn[] = [
    { role: "user", name: "alice", text: "first message" },
    { role: "assistant", text: "my reply" },
    { role: "user", name: "alice", text: "second message" },
  ];
  const seeded = seedPriorTurns(turns, ["my   reply"]);
  assert.equal(seeded.length, 1);
  assert.equal(seeded[0]!.role, "user");
  assert.match(seeded[0]!.text, /<message from="human" author="alice">first message<\/message>/);
  assert.match(seeded[0]!.text, /<message from="human" author="alice">second message<\/message>/);
  assert.doesNotMatch(seeded[0]!.text, /my reply/);
});

test("seedPriorTurns RECONCILES identity: a named assistant turn (cron/other-instance) is NEVER seeded as assistant", () => {
  const turns: ConversationTurn[] = [
    { role: "user", name: "alice", text: "did the nightly run finish?" },
    { role: "assistant", name: "agent (cron)", text: "nightly backup complete" },
  ];
  const seeded = seedPriorTurns(turns);
  assert.ok(!seeded.some((m) => m.role === "assistant"), "the cron post is never claimed as an assistant turn");
  assert.equal(seeded.length, 1);
  assert.equal(seeded[0]!.role, "user");
  assert.match(seeded[0]!.text, /<message from="human" author="alice">did the nightly run finish\?<\/message>/);
  assert.match(seeded[0]!.text, /<message from="agent" via="agent \(cron\)">nightly backup complete<\/message>/);
});

test("seedPriorTurns renders a lone named assistant turn as a from=agent <message> with via (never assistant)", () => {
  const seeded = seedPriorTurns([{ role: "assistant", name: "agent (cron)", text: "deploy shipped v4" }]);
  assert.deepEqual(seeded, [
    { role: "user", text: '<message from="agent" via="agent (cron)">deploy shipped v4</message>' },
  ]);
});

test("seedPriorTurns re-merges after a downgrade so alternation holds (no two consecutive user turns)", () => {
  const turns: ConversationTurn[] = [
    { role: "user", name: "alice", text: "status?" },
    { role: "assistant", name: "agent (cron)", text: "deploy shipped" },
  ];
  const seeded = seedPriorTurns(turns);
  assert.equal(seeded.length, 1);
  assert.equal(seeded[0]!.role, "user");
  assert.match(seeded[0]!.text, /status\?/);
  assert.match(seeded[0]!.text, /<message from="agent" via="agent \(cron\)">deploy shipped<\/message>/);
});

test("seedPriorTurns skips empty turns and returns [] for an empty input", () => {
  assert.deepEqual(seedPriorTurns([]), []);
  assert.deepEqual(seedPriorTurns([{ role: "user", name: "x", text: "   " }]), []);
});

function triggerEntry(ts: string, text: string): SessionEntry {
  return {
    sessionId: "s",
    seq: 0,
    parentSeq: null,
    type: "user",
    payload: { text, ts },
    scopeLabel: "org:default-org",
    createdAt: Number(ts) || 0,
  };
}

test("recordedMessageTimestamps collects ts from BOTH overheard imports and stamped triggers", () => {
  assert.deepEqual([...recordedMessageTimestamps([])], []);
  const history = [
    triggerEntry("100.002", "what did you ship?"),
    overheardEntry("100.001", "Alice", "deploying now"),
    entry("user", "a legacy entry with no ts is ignored"),
    entry("assistant", "shipping it"),
    overheardEntry("100.003", "Bob", "lgtm"),
  ];
  assert.deepEqual([...recordedMessageTimestamps(history)].sort(), ["100.001", "100.002", "100.003"]);
});

test("a message answered as a trigger is NOT re-imported as overheard next turn (the duplication fix)", () => {
  const history = [triggerEntry("100.002", "run it now on the first 20 emails")];
  const incoming: OverheardMessage[] = [
    { ts: "100.002", role: "user", name: "Avery", text: "run it now on the first 20 emails" },
    { ts: "100.005", role: "user", name: "Avery", text: "I don't see an approval prompt" },
  ];
  const picked = selectOverheardToImport(incoming, recordedMessageTimestamps(history));
  assert.deepEqual(
    picked.map((p) => p.ts),
    ["100.005"],
  );
});

test("selectOverheardToImport keeps every not-yet-imported message, in order, retaining agent authorship", () => {
  const incoming: OverheardMessage[] = [
    { ts: "100.001", role: "user", name: "Alice", text: "already imported" },
    { ts: "100.005", role: "user", name: "Bob", text: "fresh from bob" },
    { ts: "100.004", role: "self", text: "my own reply" },
    { ts: "100.006", role: "user", name: "Carol", text: "fresh from carol" },
  ];
  const picked = selectOverheardToImport(incoming, new Set(["100.001", "100.003"]));
  assert.deepEqual(
    picked.map((p) => p.ts),
    ["100.004", "100.005", "100.006"],
  );
  assert.equal(picked[0]!.sourceRole, "agent");
  assert.match(renderOverheard(picked[0]!), /from="agent"/);
  assert.equal(picked[1]!.name, "Bob");
  assert.equal(picked[2]!.name, "Carol");
  assert.ok(picked.every((p) => p.overheard === true));
});

test("selectOverheardToImport imports an OUT-OF-ORDER older message on first sight (set, not high-water mark)", () => {
  const incoming: OverheardMessage[] = [
    { ts: "100.005", role: "user", name: "Bob", text: "imported earlier" },
    { ts: "100.002", role: "user", name: "Alice", text: "late-arriving older post" },
  ];
  const picked = selectOverheardToImport(incoming, new Set(["100.005"]));
  assert.deepEqual(
    picked.map((p) => p.ts),
    ["100.002"],
  );
  assert.equal(picked[0]!.name, "Alice");
});

test("selectOverheardToImport drops content-less messages but keeps a bare file caption", () => {
  const incoming: OverheardMessage[] = [
    { ts: "1", role: "user", name: "A", text: "   " },
    { ts: "2", role: "user", name: "B", text: "", files: ["selfie.jpg"] },
  ];
  const picked = selectOverheardToImport(incoming, new Set());
  assert.deepEqual(
    picked.map((p) => p.ts),
    ["2"],
  );
  assert.deepEqual(picked[0]!.files, ["selfie.jpg"]);
});

test("renderOverheard frames the line as a structured overheard <message>, author-labeled, with files", () => {
  const line = renderOverheard({
    overheard: true,
    ts: "1",
    name: "Taylor",
    text: "here's the logo",
    files: ["logo.png"],
  });
  assert.match(line, /^<message overheard="true"/);
  assert.match(line, /author="Taylor"/);
  assert.match(line, /id="1"/);
  assert.match(line, /here's the logo/);
  assert.match(line, /files="logo\.png"/);
});

test("renderOverheard adds a mentions attr when the payload carries resolved mentions", () => {
  const line = renderOverheard({
    overheard: true,
    ts: "2",
    name: "Avery",
    text: "cc @jordan",
    mentions: { U1: "jordan", U2: "avery" },
  });
  assert.match(line, /mentions="U1=jordan,U2=avery"/);
  const bare = renderOverheard({ overheard: true, ts: "3", name: "Avery", text: "no mentions" });
  assert.doesNotMatch(bare, /mentions=/);
});

test("reconstructMessagesFromHistory renders overheard entries as overheard user lines (cold-start survival)", () => {
  const msgs = reconstructMessagesFromHistory([
    overheardEntry("100.001", "Alice", "I posted a photo", ["cat.jpg"]),
    entry("user", "who has a cat?"),
    entry("assistant", "Alice does — she shared cat.jpg"),
  ]);
  assertValidWire(msgs);
  const flat = msgs.map((m) => m.content.map((c) => (c.type === "text" ? c.text : "")).join("")).join("\n");
  assert.match(flat, /<message overheard="true"[^>]*author="Alice"/);
  assert.match(flat, /I posted a photo/);
  assert.match(flat, /files="cat\.jpg"/);
});

test("reconstructMessagesFromHistory skips an empty overheard marker (no blank line)", () => {
  const msgs = reconstructMessagesFromHistory([overheardEntry("100.001", "", ""), entry("user", "hi")]);
  assertValidWire(msgs);
  const texts = msgs.flatMap((m) => m.content.map((c) => (c.type === "text" ? c.text : "")));
  assert.ok(texts.some((t) => /hi/.test(t)));
  assert.ok(!texts.some((t) => /overheard/.test(t)));
});

test("replayPreamble renders an overheard entry distinctly from a normal user line", () => {
  const out = replayPreamble([overheardEntry("1", "Bob", "saw this go by"), entry("user", "thanks")]);
  assert.match(out, /Overheard \(Bob\): saw this go by/);
  assert.match(out, /User: thanks/);
});

test("a bytes-only surface call (legacy post / reach) replays with stand-in text, never {action,bytes}", () => {
  const msgs = reconstructMessagesFromHistory([
    ent("user", { text: "hi" }, 0),
    ent("tool_call", { tool: "web", action: "post", bytes: 873, callId: "c1" }, 1),
    ent("tool_result", { tool: "web", action: "post", ok: true, callId: "c1", isError: false, result: "[sent]" }, 2),
    ent("assistant", { text: "Replied in thread." }, 3),
  ]);
  assertValidWire(msgs);
  const call = msgs.flatMap((m) => (m.role === "assistant" ? m.content : [])).find((b) => b.type === "toolCall");
  assert.ok(call && call.type === "toolCall");
  const args = call.arguments as Record<string, unknown>;
  assert.equal(args.action, "post");
  assert.equal(args.text, "[sent earlier — text not retained]");
  assert.ok(!("bytes" in args), "the schema-invalid bytes summary must never replay as the model's own call");
});

test("a text-carrying post call replays verbatim (ts/broadcast preserved)", () => {
  const msgs = reconstructMessagesFromHistory([
    ent("user", { text: "hi" }, 0),
    ent("tool_call", { tool: "web", action: "post", broadcast: true, text: "the actual reply", callId: "c1" }, 1),
    ent("tool_result", { tool: "web", action: "post", ok: true, callId: "c1", isError: false, result: "[sent]" }, 2),
    ent("assistant", { text: "Replied." }, 3),
  ]);
  assertValidWire(msgs);
  const call = msgs.flatMap((m) => (m.role === "assistant" ? m.content : [])).find((b) => b.type === "toolCall");
  assert.ok(call && call.type === "toolCall");
  const args = call.arguments as Record<string, unknown>;
  assert.equal(args.text, "the actual reply");
  assert.equal(args.broadcast, true);
});

test("a legacy call with a numeric files count replays without it (schema wants string[])", () => {
  const msgs = reconstructMessagesFromHistory([
    ent("user", { text: "hi" }, 0),
    ent("tool_call", { tool: "web", action: "reach", recipient: "bob", bytes: 100, files: 2, callId: "c1" }, 1),
    ent("tool_result", { tool: "web", action: "reach", ok: true, callId: "c1", isError: false, result: "[sent]" }, 2),
    ent("assistant", { text: "Sent." }, 3),
  ]);
  assertValidWire(msgs);
  const call = msgs.flatMap((m) => (m.role === "assistant" ? m.content : [])).find((b) => b.type === "toolCall");
  assert.ok(call && call.type === "toolCall");
  const args = call.arguments as Record<string, unknown>;
  assert.equal(args.recipient, "bob");
  assert.ok(!("files" in args), "numeric files count must not replay");
  assert.ok(!("bytes" in args));
});

test("reconstructMessagesFromHistory replays the environment note the user message was sent with", () => {
  const history = [
    ent("user", { text: "what's on today?", environment: "<environment>\nIt is Monday 9am\n</environment>" }, 1),
    ent("assistant", { text: "Nothing yet." }, 2),
  ];
  const [user] = reconstructMessagesFromHistory(history);
  assert.deepEqual(user!.content, [
    {
      type: "text",
      text: "── Wednesday, December 31, 1969 ──\nwhat's on today?\n\n<environment>\nIt is Monday 9am\n</environment>",
    },
  ]);
});

test("overheard reconciliation deduplicates IDs within a batch but preserves identical text at different IDs", () => {
  const incoming: OverheardMessage[] = [
    { ts: "100.001", role: "self", text: "Shall I proceed?" },
    { ts: "100.001", role: "self", text: "Shall I proceed?" },
    { ts: "100.002", role: "user", name: "Alex", text: "yes" },
    { ts: "100.003", role: "user", name: "Blair", text: "yes" },
  ];
  const picked = selectOverheardToImport(incoming, new Set());
  assert.deepEqual(
    picked.map((p) => p.ts),
    ["100.001", "100.002", "100.003"],
  );
  assert.deepEqual(selectOverheardToImport(incoming, new Set(picked.map((p) => p.ts))), []);
});

test("a tool_result marked interrupted replays as the interrupted placeholder, not as a real result", () => {
  const entries: SessionEntry[] = [
    {
      sessionId: "s",
      seq: 1,
      parentSeq: null,
      type: "user",
      payload: { text: "run it" },
      scopeLabel: "org:o",
      createdAt: 1,
    },
    {
      sessionId: "s",
      seq: 2,
      parentSeq: null,
      type: "tool_call",
      payload: { tool: "execute", callId: "c1", command: "make" },
      scopeLabel: "org:o",
      createdAt: 2,
    },
    {
      sessionId: "s",
      seq: 3,
      parentSeq: null,
      type: "tool_result",
      payload: { callId: "c1", result: "[exit 143]", interrupted: true },
      scopeLabel: "org:o",
      createdAt: 3,
    },
  ];
  const result = reconstructMessagesFromHistory(entries).find((m) => m.role === "toolResult")!;
  assert.equal(result.role, "toolResult");
  if (result.role !== "toolResult") return;
  assert.deepEqual(result.content, [{ type: "text", text: INTERRUPTED_TOOL_RESULT }]);
  assert.equal(result.isError, true);
});

test("reconstructMessagesFromHistory keeps parallel tool calls in one assistant message", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "check both" }, 1),
    ent("tool_call", { tool: "read", callId: "a", path: "/a" }, 2),
    ent("tool_call", { tool: "read", callId: "b", path: "/b" }, 3),
    ent("tool_result", { tool: "read", callId: "a", result: "A" }, 4),
    ent("tool_result", { tool: "read", callId: "b", result: "B" }, 5),
    ent("tool_call", { tool: "read", callId: "c", path: "/c" }, 6),
    ent("tool_result", { tool: "read", callId: "c", result: "C" }, 7),
    ent("assistant", { text: "done" }, 8),
  ];
  const msgs = reconstructMessagesFromHistory(history);
  assertValidWire(msgs);
  assert.deepEqual(
    msgs.map((m) =>
      m.role === "assistant" ? m.content.map((c) => (c.type === "toolCall" ? c.id : "text")).join(",") : m.role,
    ),
    ["user", "a,b", "toolResult", "toolResult", "c", "toolResult", "text"],
  );
});

test("reconstructMessagesFromHistory replays a stopped turn as a system note in user voice, after the real partial", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "write the report" }, 1),
    ent("assistant", { text: "Draft so far", stopped: true }, 2),
    ent("system", { kind: "turn_stopped", reason: "by the parent agent" }, 3),
    ent("user", { text: "carry on" }, 4),
  ];
  const msgs = reconstructMessagesFromHistory(history);
  assertValidWire(msgs);
  assert.deepEqual(
    msgs.map((m) => m.role),
    ["user", "assistant", "user", "user"],
  );
  assert.deepEqual(msgs[1]!.content, [{ type: "text", text: "Draft so far" }]);
  assert.deepEqual(msgs[2]!.content, [{ type: "text", text: "[system] Turn stopped by the parent agent." }]);
});

test("a turn stopped while a tool call ran explains the stop in that call's result, once", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "deploy it" }, 1),
    ent("tool_call", { tool: "execute", callId: "c1", command: "make deploy" }, 2),
    ent("system", { kind: "turn_stopped", reason: "by user" }, 3),
    ent("user", { text: "what happened?" }, 4),
  ];
  const msgs = reconstructMessagesFromHistory(history);
  assertValidWire(msgs);
  assert.deepEqual(
    msgs.map((m) => m.role),
    ["user", "assistant", "toolResult", "user"],
  );
  assert.match(JSON.stringify(msgs[2]!.content), /Turn stopped by user while this call was running/);
  assert.ok(!JSON.stringify(msgs).includes("[system] Turn stopped by user."), "no separate stop note");
});

test("overheard traffic between a cut-off call and the stop does not split the stop from that call", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "deploy it" }, 1),
    ent("tool_call", { tool: "execute", callId: "c1", command: "make deploy" }, 2),
    ent("user", { overheard: true, ts: "3", name: "Alice", text: "lunch?" }, 3),
    ent("system", { kind: "turn_stopped", reason: "by user" }, 4),
  ];
  const text = JSON.stringify(reconstructMessagesFromHistory(history));
  assert.match(text, /Turn stopped by user while this call was running/);
  assert.ok(!text.includes("[system] Turn stopped by user."), "no separate stop note");
});

test("a call killed by a Stop keeps its output and carries the stop in its result, once", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "deploy it" }, 1),
    ent("tool_call", { tool: "execute", callId: "c1", command: "make deploy" }, 2),
    ent("tool_result", { callId: "c1", result: "[stderr]\nKilled\n\n[exit 137]", stopped: true }, 3),
    ent("system", { kind: "turn_stopped", reason: "by user" }, 4),
    ent("user", { text: "what happened?" }, 5),
  ];
  const msgs = reconstructMessagesFromHistory(history);
  assertValidWire(msgs);
  assert.deepEqual(
    msgs.map((m) => m.role),
    ["user", "assistant", "toolResult", "user"],
  );
  const result = msgs[2] as { content: unknown[] };
  assert.deepEqual(
    result.content.map((c) => (c as { text: string }).text),
    ["[stderr]\nKilled\n\n[exit 137]", "[system] Turn stopped by user."],
    "the recorded output is kept and the stop is part of that call's result",
  );
  assert.equal(JSON.stringify(msgs).split("Turn stopped by user").length, 2, "the stop appears once");
});

test("a result recorded while a Stop fired keeps its text when no stop was recorded", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "deploy it" }, 1),
    ent("tool_call", { tool: "execute", callId: "c1", command: "make deploy" }, 2),
    ent("tool_result", { callId: "c1", result: "[exit 137]", stopped: true }, 3),
  ];
  assert.match(JSON.stringify(reconstructMessagesFromHistory(history)), /\[exit 137\]/);
});

test("a tool result cut off by a deploy says so in its replayed result", () => {
  const history: SessionEntry[] = [
    ent("user", { text: "deploy it" }, 1),
    ent("tool_call", { tool: "execute", callId: "c1", command: "make deploy" }, 2),
    ent("tool_result", { callId: "c1", result: "[exit 143]", interrupted: true, stop: { kind: "shutdown" } }, 3),
  ];
  const text = JSON.stringify(reconstructMessagesFromHistory(history));
  assert.match(text, /A routine platform deploy interrupted this call/);
});

test("replayed context marks each Pacific calendar day once, dating overheard Slack messages by when they were sent", () => {
  const imported = Date.parse("2026-10-19T18:00:00Z");
  const overheard = (iso: string, text: string): SessionEntry => ({
    ...overheardEntry(String(Date.parse(iso) / 1000), "Dana", text),
    createdAt: imported,
  });
  const msgs = reconstructMessagesFromHistory([
    overheard("2026-10-15T20:00:00Z", "kickoff today"),
    overheard("2026-10-16T16:00:00Z", "the deadline is Friday"),
    overheard("2026-10-17T05:30:00Z", "still Friday night here"),
    overheard("2026-10-17T08:00:00Z", "saturday early"),
    { ...ent("user", { text: "has the deadline passed?" }, 5), createdAt: imported },
  ]);
  const lines = msgs.flatMap((m) =>
    m.content.flatMap((b) => (b.type === "text" ? b.text.split("\n").filter((l) => l.startsWith("──")) : [])),
  );
  assert.deepEqual(lines, [
    "── Thursday, October 15, 2026 ──",
    "── Friday, October 16, 2026 ──",
    "── Saturday, October 17, 2026 ──",
    "── Monday, October 19, 2026 ──",
  ]);
  assert.match((msgs[2]!.content[0] as { text: string }).text, /^<message overheard="true"/);
  assert.equal(
    (msgs[4]!.content[0] as { text: string }).text,
    "── Monday, October 19, 2026 ──\nhas the deadline passed?",
  );
});