Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

test(mcp): add black-box tests for the mod contract and pane sessions

Jack Rudenko8785691057df
diff --git a/packages/cli/src/mcp-blackbox/channel-sessions.blackbox.test.ts b/packages/cli/src/mcp-blackbox/channel-sessions.blackbox.test.ts
new file mode 100644
index 00000000..c98ea7bc
--- /dev/null
+++ b/packages/cli/src/mcp-blackbox/channel-sessions.blackbox.test.ts
@@ -0,0 +1,418 @@
+/**
+ * BLACK-BOX: the channel tools (create_session, send_input, get_output, cancel_session,
+ * list_sessions, get_diagnostics, capture_session) through the REAL claudish MCP server,
+ * every session the pane fake (marker mode, `contract-fake-model`) in a real headless magmux.
+ *
+ * Written from spec.md (FR2, FR5, FR7, FR9 B–D, FR10, NFR1) and the frozen mod contract v1
+ * (§8 B–E, architecture §4.2, §5). No implementation file was read.
+ *
+ * The fake answers a typed line `ANSWER <model> <sha1(line)[0..8]>`, so a test can prove the
+ * exact text it sent is the text that was answered.
+ */
+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
+import { createHash } from "node:crypto";
+import { waitNoOrphans } from "../pane/test-helpers/hermetic-env.js";
+import { useBlackboxEnv } from "../test-helpers/blackbox-env.js";
+import {
+ FAKE_MODEL,
+ MAGMUX_AVAILABLE,
+ McpServer,
+ NO_MAGMUX_MESSAGE,
+ type ToolResult,
+ findField,
+ paneOrphans,
+ paneRootOf,
+ serverEnv,
+} from "../test-helpers/contract-mcp.js";
+import { type TempLayout, makeTempLayout, waitFor } from "../test-helpers/contract-records.js";
+
+useBlackboxEnv();
+
+const T_TEST = 90_000;
+type Json = Record<string, unknown>;
+
+const CAPABILITIES = ["list", "status", "cancel", "capture", "capture_since_seq", "capture_spans"];
+const SLOT_STATES = [
+ "STARTING",
+ "RUNNING",
+ "AWAITING_INPUT",
+ "AWAITING_PERMISSION",
+ "COMPLETED",
+ "FAILED",
+ "CANCELLED",
+ "TIMEOUT",
+ "EMPTY",
+];
+const TERMINAL_STATES = ["COMPLETED", "FAILED", "CANCELLED", "TIMEOUT", "EMPTY"];
+const FAILURE_REASONS = [
+ "cancelled",
+ "timeout",
+ "boot_timeout",
+ "boot_blocked",
+ "first_run_dialog",
+ "agent_rejected",
+ "prompt_not_accepted",
+ "prompt_not_read",
+ "child_exited",
+ "pane_lost",
+ "blocked",
+ "api_error",
+ "refused",
+ "empty_output",
+ "shape_mismatch",
+];
+const SLOT_ROW_KEYS = [
+ "activity",
+ "cost_usd",
+ "idle_seconds",
+ "last_activity_at",
+ "model",
+ "pane",
+ "provider",
+ "reason",
+ "slot",
+ "state",
+ "tokens_in",
+ "tokens_out",
+ "tool_calls",
+ "turns_completed",
+];
+const SESSION_ROW_KEYS = [
+ ...SLOT_ROW_KEYS,
+ "completed_at",
+ "elapsed_seconds",
+ "session_id",
+ "started_at",
+].sort();
+const CAPTURE_KEYS = ["cols", "cursor", "final", "lines", "rows", "seq"].sort();
+const SESSION_CANCEL_KEYS = ["changed", "session_id", "state"];
+
+const keys = (v: unknown): string[] => Object.keys(v as Json).sort();
+const isIso = (v: unknown): boolean =>
+ typeof v === "string" && !Number.isNaN(Date.parse(v)) && /^\d{4}-\d{2}-\d{2}T/.test(v);
+const sha8 = (s: string): string => createHash("sha1").update(s).digest("hex").slice(0, 8);
+
+let layout: TempLayout;
+let server: McpServer;
+
+if (!MAGMUX_AVAILABLE) console.warn(NO_MAGMUX_MESSAGE);
+
+beforeEach(async () => {
+ layout = makeTempLayout("bbchan");
+ server = await McpServer.start({
+ env: serverEnv(layout, { CONTRACT_FAKE_MAX_MS: "80000" }),
+ cwd: layout.cwd,
+ });
+});
+
+afterEach(async () => {
+ await server.close();
+ const report = await paneOrphans(layout, 15_000);
+ layout.cleanup();
+ expect(report).toEqual({ processes: [], files: [] });
+});
+
+function ok(r: ToolResult): Json {
+ if (r.isError) throw new Error(`expected success, got error: ${r.text}`);
+ expect(r.json).toBeDefined();
+ return r.json as Json;
+}
+
+function contractError(r: ToolResult, code: string): void {
+ expect(r.isError).toBe(true);
+ const body = JSON.parse(r.text) as Json;
+ expect(keys(body)).toEqual(["error"]);
+ expect(keys(body.error)).toEqual(["code", "message"]);
+ expect((body.error as Json).code).toBe(code);
+ expect(typeof (body.error as Json).message).toBe("string");
+}
+
+/** §8 SessionRow: SlotRow + session fields, types and state-dependent rules. */
+function expectSessionRowTyped(row: Json): void {
+ expect(keys(row)).toEqual(SESSION_ROW_KEYS);
+ expect(typeof row.session_id).toBe("string");
+ expect(row.slot).toBe(row.session_id);
+ expect(typeof row.model).toBe("string");
+ expect(row.provider === null || typeof row.provider === "string").toBe(true);
+ expect(SLOT_STATES).toContain(row.state as string);
+ expect(row.reason === null || FAILURE_REASONS.includes(row.reason as string)).toBe(true);
+ for (const k of ["tokens_in", "tokens_out", "cost_usd", "idle_seconds"])
+ expect(row[k] === null || typeof row[k] === "number").toBe(true);
+ expect(Number.isInteger(row.tool_calls)).toBe(true);
+ expect(Number.isInteger(row.turns_completed)).toBe(true);
+ expect(row.last_activity_at === null || isIso(row.last_activity_at)).toBe(true);
+ expect(row.activity === null || typeof row.activity === "string").toBe(true);
+ expect(row.pane === null || typeof row.pane === "string").toBe(true);
+ expect(isIso(row.started_at)).toBe(true);
+ expect(row.completed_at === null || isIso(row.completed_at)).toBe(true);
+ expect(typeof row.elapsed_seconds).toBe("number");
+ expect(row.elapsed_seconds as number).toBeGreaterThanOrEqual(0);
+
+ const terminal = TERMINAL_STATES.includes(row.state as string);
+ if (terminal) {
+ expect(row.idle_seconds).toBeNull();
+ expect(row.activity).toBeNull();
+ expect(isIso(row.completed_at)).toBe(true);
+ } else {
+ expect(row.completed_at).toBeNull();
+ }
+ if (["FAILED", "EMPTY", "CANCELLED", "TIMEOUT"].includes(row.state as string))
+ expect(row.reason).not.toBeNull();
+ else expect(row.reason).toBeNull();
+}
+
+async function sessionRow(id: string): Promise<Json | undefined> {
+ const list = ok(await server.callTool("list_sessions", { include_completed: true }));
+ expect(list.contract_version).toBe(1);
+ expect(list.capabilities).toEqual(CAPABILITIES);
+ return (list.sessions as Json[]).find((s) => s.session_id === id);
+}
+
+async function waitState(id: string, states: string[], timeoutMs = 30_000): Promise<Json> {
+ return waitFor(
+ async () => {
+ const row = await sessionRow(id);
+ return row && states.includes(row.state as string) ? row : undefined;
+ },
+ { what: `session ${id} to reach ${states.join("|")}`, timeoutMs, intervalMs: 100 }
+ );
+}
+
+async function create(args: Json): Promise<string> {
+ const created = ok(await server.callTool("create_session", { model: FAKE_MODEL, ...args }));
+ expect(keys(created)).toEqual(["session_id", "state"]);
+ expect(created.state).toBe("STARTING");
+ return created.session_id as string;
+}
+
+/* ─────────────── tests ─────────────── */
+
+describe.skipIf(!MAGMUX_AVAILABLE)("create_session WITH a prompt is one-shot (FR2)", () => {
+ test(
+ "it answers, turns COMPLETED, closes its pane and reaps everything; get_output has the answer",
+ async () => {
+ const prompt = "Reply with exactly PEAR";
+ const id = await create({ prompt });
+ const row = await waitState(id, TERMINAL_STATES);
+ expectSessionRowTyped(row);
+ expect(row.state).toBe("COMPLETED");
+ expect(row.reason).toBeNull();
+ expect(row.model).toBe(FAKE_MODEL);
+ expect(row.turns_completed).toBe(1);
+
+ // FR5: the answer from the transcript, the exact text that was typed
+ const out = await server.callTool("get_output", { session_id: id });
+ expect(out.isError).toBe(false);
+ expect(out.text).toContain(`ANSWER ${FAKE_MODEL} ${sha8(prompt)}`);
+
+ // one-shot: the pane is closed → capture's last screen, final:true
+ const cap = await waitFor(
+ async () => {
+ const c = ok(await server.callTool("capture_session", { session_id: id }));
+ return c.final === true ? c : undefined;
+ },
+ { what: "the one-shot pane's final screen", timeoutMs: 15_000, intervalMs: 200 }
+ );
+ expect(keys(cap)).toEqual(CAPTURE_KEYS);
+ expect((cap.lines as string[]).length).toBe(50);
+
+ // NFR1: nothing outlives the finished session — checked while the server still runs
+ const report = await waitNoOrphans({ sockRoot: paneRootOf(layout), ids: [id] }, 15_000);
+ expect(report.processes).toEqual([]);
+ expect(report.files.filter((f) => f.endsWith(".sock"))).toEqual([]);
+
+ // terminal is absorbing: a later send is refused with its reason and the state
+ const late = ok(await server.callTool("send_input", { session_id: id, text: "more" }));
+ expect(late).toEqual({ success: false, reason: "terminal", state: "COMPLETED" });
+ const cancel = ok(await server.callTool("cancel_session", { session_id: id }));
+ expect(keys(cancel)).toEqual(SESSION_CANCEL_KEYS);
+ expect(cancel).toEqual({ session_id: id, state: "COMPLETED", changed: false });
+ expect((await sessionRow(id))?.state).toBe("COMPLETED");
+ },
+ T_TEST
+ );
+});
+
+describe.skipIf(!MAGMUX_AVAILABLE)(
+ "create_session WITHOUT a prompt stays open between turns",
+ () => {
+ test(
+ "AWAITING_INPUT → send_input → answer → AWAITING_INPUT (never STARTING again) → cancel",
+ async () => {
+ const id = await create({});
+ const idle = await waitState(id, ["AWAITING_INPUT"]);
+ expectSessionRowTyped(idle);
+ expect(idle.turns_completed).toBe(0);
+ expect(idle.activity).toBeNull();
+ expect(typeof idle.pane).toBe("string");
+
+ const text = "What is two plus two";
+ const sent = ok(await server.callTool("send_input", { session_id: id, text }));
+ expect(sent.success).toBe(true);
+ expect(typeof sent.queued).toBe("number");
+
+ // poll the turn; record every state seen until it settles back to idle
+ const seen: string[] = [];
+ const settled = await waitFor(
+ async () => {
+ const row = await sessionRow(id);
+ if (!row) return undefined;
+ seen.push(row.state as string);
+ return row.state === "AWAITING_INPUT" && row.turns_completed === 1 ? row : undefined;
+ },
+ { what: "the first turn to settle", timeoutMs: 30_000, intervalMs: 50 }
+ );
+ // "a promptless session's first send_input goes AWAITING_INPUT → RUNNING, never back to STARTING"
+ expect(seen).not.toContain("STARTING");
+ for (const s of seen) expect(TERMINAL_STATES).not.toContain(s);
+ expectSessionRowTyped(settled);
+ expect(settled.reason).toBeNull();
+ expect(typeof settled.idle_seconds).toBe("number");
+
+ const out = await server.callTool("get_output", { session_id: id });
+ expect(out.isError).toBe(false);
+ expect(out.text).toContain(`ANSWER ${FAKE_MODEL} ${sha8(text)}`);
+
+ const first = ok(await server.callTool("cancel_session", { session_id: id }));
+ expect(first).toEqual({ session_id: id, state: "CANCELLED", changed: true });
+ // returns once the state has turned CANCELLED
+ const row = (await sessionRow(id)) as Json;
+ expect(row.state).toBe("CANCELLED");
+ expect(row.reason).toBe("cancelled");
+ expectSessionRowTyped(row);
+ const second = ok(await server.callTool("cancel_session", { session_id: id }));
+ expect(second).toEqual({ session_id: id, state: "CANCELLED", changed: false });
+
+ const report = await waitNoOrphans({ sockRoot: paneRootOf(layout), ids: [id] }, 15_000);
+ expect(report.processes).toEqual([]);
+ expect(report.files.filter((f) => f.endsWith(".sock"))).toEqual([]);
+ },
+ T_TEST
+ );
+
+ test(
+ "send_input is accepted while a turn runs and queued: two sends → two settled turns, both answered",
+ async () => {
+ const id = await create({});
+ await waitState(id, ["AWAITING_INPUT"]);
+ const a = "first question alpha";
+ const b = "second question bravo";
+ const sa = ok(await server.callTool("send_input", { session_id: id, text: a }));
+ const sb = ok(await server.callTool("send_input", { session_id: id, text: b }));
+ expect(sa.success).toBe(true);
+ expect(sb.success).toBe(true);
+ const done = await waitFor(
+ async () => {
+ const row = await sessionRow(id);
+ return row?.state === "AWAITING_INPUT" && row.turns_completed === 2 ? row : undefined;
+ },
+ { what: "both queued turns to settle", timeoutMs: 40_000, intervalMs: 100 }
+ );
+ expectSessionRowTyped(done);
+ const out = await server.callTool("get_output", { session_id: id });
+ expect(out.text).toContain(`ANSWER ${FAKE_MODEL} ${sha8(a)}`);
+ expect(out.text).toContain(`ANSWER ${FAKE_MODEL} ${sha8(b)}`);
+ expect(out.text.indexOf(sha8(a))).toBeLessThan(out.text.indexOf(sha8(b)));
+ ok(await server.callTool("cancel_session", { session_id: id }));
+ },
+ T_TEST
+ );
+ }
+);
+
+describe.skipIf(!MAGMUX_AVAILABLE)("failure and timeout states (FR7)", () => {
+ test(
+ "an unknown agent → FAILED agent_rejected",
+ async () => {
+ const id = await create({ prompt: "Reply with exactly PEAR", agent: "zzz-not-real" });
+ const row = await waitState(id, TERMINAL_STATES);
+ expectSessionRowTyped(row);
+ expect(row.state).toBe("FAILED");
+ expect(row.reason).toBe("agent_rejected");
+ const late = ok(await server.callTool("send_input", { session_id: id, text: "x" }));
+ expect(late).toEqual({ success: false, reason: "terminal", state: "FAILED" });
+ },
+ T_TEST
+ );
+
+ test(
+ "timeout_seconds elapsing on a hung turn → TIMEOUT timeout, pane closed",
+ async () => {
+ const id = await create({ prompt: "hold on @@HANG@@", timeout_seconds: 3 });
+ const row = await waitState(id, TERMINAL_STATES, 30_000);
+ expectSessionRowTyped(row);
+ expect(row.state).toBe("TIMEOUT");
+ expect(row.reason).toBe("timeout");
+ const cancel = ok(await server.callTool("cancel_session", { session_id: id }));
+ expect(cancel).toEqual({ session_id: id, state: "TIMEOUT", changed: false });
+ const cap = await waitFor(
+ async () => {
+ const c = ok(await server.callTool("capture_session", { session_id: id }));
+ return c.final === true ? c : undefined;
+ },
+ { what: "final:true after timeout", timeoutMs: 15_000, intervalMs: 200 }
+ );
+ expect(keys(cap)).toEqual(CAPTURE_KEYS);
+ },
+ T_TEST
+ );
+});
+
+describe.skipIf(!MAGMUX_AVAILABLE)("diagnostics, channel frames, error forms", () => {
+ test(
+ "get_diagnostics names the state and the capture/turn source; channel frames fire for the session",
+ async () => {
+ const id = await create({ prompt: "Reply with exactly PEAR" });
+ await waitState(id, TERMINAL_STATES);
+ const diag = await server.callTool("get_diagnostics", { session_id: id });
+ expect(diag.isError).toBe(false);
+ expect(diag.json).toBeDefined();
+ // §4.2: `event` beside `state` (SlotState); FR5: which source was used is recorded
+ expect(SLOT_STATES).toContain(findField(diag.json, "state") as string);
+ expect(findField(diag.json, "state")).toBe("COMPLETED");
+ expect(typeof findField(diag.json, "event")).toBe("string");
+ for (const k of ["captureSource", "turnSource", "pane", "anomalies", "transcriptPath"])
+ expect(findField(diag.json, k)).not.toBeUndefined();
+
+ // FR10/E: notifications/claude/channel frames fire on state transitions
+ const frames = await waitFor(
+ () => {
+ const f = server.notifications.filter(
+ (n) =>
+ n.method === "notifications/claude/channel" && JSON.stringify(n.params).includes(id)
+ );
+ return f.length > 0 ? f : undefined;
+ },
+ { what: "a channel frame for the session", timeoutMs: 5_000 }
+ );
+ expect(frames.length).toBeGreaterThan(0);
+ },
+ T_TEST
+ );
+
+ test(
+ "unknown session ids: send_input answers unknown_session; get_output/get_diagnostics answer Error text",
+ async () => {
+ const send = ok(await server.callTool("send_input", { session_id: "deadbeef", text: "x" }));
+ expect(send.success).toBe(false);
+ expect(send.reason).toBe("unknown_session");
+ const out = await server.callTool("get_output", { session_id: "deadbeef" });
+ expect(out.isError).toBe(true);
+ expect(out.text.startsWith("Error")).toBe(true);
+ const diag = await server.callTool("get_diagnostics", { session_id: "deadbeef" });
+ expect(diag.isError).toBe(true);
+ expect(diag.text.startsWith("Error")).toBe(true);
+ // the §8 verbs answer a wrongly TYPED argument with an invalid_args ContractError
+ contractError(await server.callTool("cancel_session", { session_id: 42 }), "invalid_args");
+ contractError(
+ await server.callTool("capture_session", { session_id: 42, since_seq: 1 }),
+ "invalid_args"
+ );
+ contractError(
+ await server.callTool("list_sessions", { include_completed: "yes" }),
+ "invalid_args"
+ );
+ },
+ T_TEST
+ );
+});
diff --git a/packages/cli/src/mcp-blackbox/team-contract.blackbox.test.ts b/packages/cli/src/mcp-blackbox/team-contract.blackbox.test.ts
new file mode 100644
index 00000000..350f8c04
--- /dev/null
+++ b/packages/cli/src/mcp-blackbox/team-contract.blackbox.test.ts
@@ -0,0 +1,707 @@
+/**
+ * BLACK-BOX: the `team` tool through the REAL claudish MCP server (JSON-RPC over stdio),
+ * every slot the pane fake inside a real headless magmux.
+ *
+ * Written from spec.md (FR1, FR6, FR7, FR9 A–D, NFR1, NFR2) and the frozen mod contract
+ * v1 (§8 A–E, architecture §4.1, §4.3, §5) only. No implementation file was read; every
+ * literal below is copied from the contract text so a failure names the rule it broke.
+ *
+ * Fake child models used (CLAUDISH_BIN = the fake):
+ * - `fake-answer` answers `ANSWER fake-answer <sha8>` and settles;
+ * - `fake-no_shape` answers prose without the `ANSWER` shape;
+ * - `contract-fake-model` marker mode: `@@HANG@@` = accepted, never answered.
+ */
+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
+import { existsSync, rmSync } from "node:fs";
+import { join } from "node:path";
+import { waitNoOrphans } from "../pane/test-helpers/hermetic-env.js";
+import { useBlackboxEnv } from "../test-helpers/blackbox-env.js";
+import {
+ MAGMUX_AVAILABLE,
+ McpServer,
+ NO_MAGMUX_MESSAGE,
+ type ToolResult,
+ paneOrphans,
+ paneRootOf,
+ serverEnv,
+} from "../test-helpers/contract-mcp.js";
+import { type TempLayout, makeTempLayout, waitFor } from "../test-helpers/contract-records.js";
+
+useBlackboxEnv();
+
+const T_TEST = 90_000;
+type Json = Record<string, unknown>;
+
+/* ─────────────── literals from the frozen contract (§8 Types) ─────────────── */
+
+const CAPABILITIES = ["list", "status", "cancel", "capture", "capture_since_seq", "capture_spans"];
+const SLOT_STATES = [
+ "STARTING",
+ "RUNNING",
+ "AWAITING_INPUT",
+ "AWAITING_PERMISSION",
+ "COMPLETED",
+ "FAILED",
+ "CANCELLED",
+ "TIMEOUT",
+ "EMPTY",
+];
+const TERMINAL_STATES = ["COMPLETED", "FAILED", "CANCELLED", "TIMEOUT", "EMPTY"];
+const FAILURE_REASONS = [
+ "cancelled",
+ "timeout",
+ "boot_timeout",
+ "boot_blocked",
+ "first_run_dialog",
+ "agent_rejected",
+ "prompt_not_accepted",
+ "prompt_not_read",
+ "child_exited",
+ "pane_lost",
+ "blocked",
+ "api_error",
+ "refused",
+ "empty_output",
+ "shape_mismatch",
+];
+const SLOT_ROW_KEYS = [
+ "activity",
+ "cost_usd",
+ "idle_seconds",
+ "last_activity_at",
+ "model",
+ "pane",
+ "provider",
+ "reason",
+ "slot",
+ "state",
+ "tokens_in",
+ "tokens_out",
+ "tool_calls",
+ "turns_completed",
+].sort();
+const TEAM_RUN_ROW_KEYS = [
+ "finished_at",
+ "kind",
+ "outcome",
+ "path",
+ "run_id",
+ "slots",
+ "started_at",
+ "state",
+].sort();
+const CAPTURE_KEYS = ["cols", "cursor", "final", "lines", "rows", "seq"].sort();
+const UNCHANGED_KEYS = ["final", "seq", "unchanged"];
+const TEAM_CANCEL_KEYS = ["path", "results", "run_id"];
+/** §4.1 `run` row of the table: the start answer's keys. */
+const RUN_START_KEYS = [
+ "started",
+ "run_id",
+ "team_session_id",
+ "session_path",
+ "monitor_record",
+ "slots",
+ "run",
+ "next",
+ "note",
+];
+
+const HANG = "stay busy @@HANG@@";
+const PEAR = "Reply with exactly PEAR";
+
+/* ─────────────── helpers ─────────────── */
+
+const keys = (v: unknown): string[] => Object.keys(v as Json).sort();
+const isIso = (v: unknown): boolean =>
+ typeof v === "string" && !Number.isNaN(Date.parse(v)) && /^\d{4}-\d{2}-\d{2}T/.test(v);
+const isNumOrNull = (v: unknown): boolean => v === null || typeof v === "number";
+const isStrOrNull = (v: unknown): boolean => v === null || typeof v === "string";
+
+let layout: TempLayout;
+let server: McpServer;
+
+if (!MAGMUX_AVAILABLE) console.warn(NO_MAGMUX_MESSAGE);
+
+async function startServer(): Promise<McpServer> {
+ return McpServer.start({
+ // the fake's safety exit must not end a @@HANG@@ slot mid-test
+ env: serverEnv(layout, { CONTRACT_FAKE_MAX_MS: "80000" }),
+ cwd: layout.cwd,
+ });
+}
+
+beforeEach(async () => {
+ layout = makeTempLayout("bbteam");
+ server = await startServer();
+});
+
+afterEach(async () => {
+ await server.close();
+ const report = await paneOrphans(layout, 15_000);
+ layout.cleanup();
+ expect(report).toEqual({ processes: [], files: [] });
+});
+
+function ok(r: ToolResult): Json {
+ if (r.isError) throw new Error(`expected success, got error: ${r.text}`);
+ expect(r.json).toBeDefined();
+ return r.json as Json;
+}
+
+function contractError(r: ToolResult, code: string): void {
+ expect(r.isError).toBe(true);
+ const body = JSON.parse(r.text) as Json;
+ expect(keys(body)).toEqual(["error"]);
+ expect(keys(body.error)).toEqual(["code", "message"]);
+ expect((body.error as Json).code).toBe(code);
+ expect(typeof (body.error as Json).message).toBe("string");
+ expect(((body.error as Json).message as string).length).toBeGreaterThan(0);
+}
+
+function team(args: Json): Promise<ToolResult> {
+ return server.callTool("team", args);
+}
+
+/** Every field of a §8 SlotRow, by key set, type and the state-dependent rules. */
+function expectSlotRowTyped(row: Json): void {
+ expect(keys(row)).toEqual(SLOT_ROW_KEYS);
+ expect(typeof row.slot).toBe("string");
+ expect(typeof row.model).toBe("string");
+ expect(isStrOrNull(row.provider)).toBe(true);
+ expect(SLOT_STATES).toContain(row.state as string);
+ expect(row.reason === null || FAILURE_REASONS.includes(row.reason as string)).toBe(true);
+ expect(isNumOrNull(row.tokens_in)).toBe(true);
+ expect(isNumOrNull(row.tokens_out)).toBe(true);
+ expect(isNumOrNull(row.cost_usd)).toBe(true);
+ expect(Number.isInteger(row.tool_calls)).toBe(true);
+ expect(row.tool_calls as number).toBeGreaterThanOrEqual(0);
+ expect(Number.isInteger(row.turns_completed)).toBe(true);
+ expect(row.turns_completed as number).toBeGreaterThanOrEqual(0);
+ expect(row.last_activity_at === null || isIso(row.last_activity_at)).toBe(true);
+ expect(isNumOrNull(row.idle_seconds)).toBe(true);
+ if (typeof row.idle_seconds === "number") expect(row.idle_seconds).toBeGreaterThanOrEqual(0);
+ expect(isStrOrNull(row.activity)).toBe(true);
+ expect(isStrOrNull(row.pane)).toBe(true);
+
+ const terminal = TERMINAL_STATES.includes(row.state as string);
+ if (terminal) {
+ // "idle_seconds: null in terminal states"; activity has a value only while busy/blocked
+ expect(row.idle_seconds).toBeNull();
+ expect(row.activity).toBeNull();
+ }
+ if (["FAILED", "EMPTY", "CANCELLED", "TIMEOUT"].includes(row.state as string)) {
+ expect(row.reason).not.toBeNull();
+ } else {
+ // "null otherwise (incl. COMPLETED)"
+ expect(row.reason).toBeNull();
+ }
+}
+
+function expectRunRowTyped(run: Json): void {
+ expect(keys(run)).toEqual(TEAM_RUN_ROW_KEYS);
+ expect(typeof run.run_id).toBe("string");
+ expect(typeof run.path).toBe("string");
+ expect((run.path as string).startsWith("/")).toBe(true);
+ expect(["run", "judge"]).toContain(run.kind as string);
+ expect(isIso(run.started_at)).toBe(true);
+ expect(["ACTIVE", "SETTLED"]).toContain(run.state as string);
+ const slots = run.slots as Json[];
+ expect(Array.isArray(slots)).toBe(true);
+ for (const s of slots) expectSlotRowTyped(s);
+ // "sorted by slot id"
+ const ids = slots.map((s) => s.slot as string);
+ expect(ids).toEqual([...ids].sort());
+ // "SETTLED ⇔ every slot terminal"
+ const allTerminal = slots.every((s) => TERMINAL_STATES.includes(s.state as string));
+ expect(run.state === "SETTLED").toBe(allTerminal);
+ if (run.state === "ACTIVE") {
+ expect(run.outcome).toBeNull();
+ expect(run.finished_at).toBeNull();
+ } else {
+ expect(["ok", "partial", "all-failed"]).toContain(run.outcome as string);
+ expect(isIso(run.finished_at)).toBe(true);
+ }
+}
+
+function expectContractMeta(body: Json): void {
+ expect(body.contract_version).toBe(1);
+ expect(body.capabilities).toEqual(CAPABILITIES);
+}
+
+async function runTeam(args: Json): Promise<Json> {
+ return ok(await team({ mode: "run", ...args }));
+}
+
+async function waitSettled(path: string, runId?: string, timeoutMs = 45_000): Promise<Json> {
+ return waitFor(
+ async () => {
+ const s = ok(await team({ mode: "status", path, ...(runId ? { run_id: runId } : {}) }));
+ return (s.run as Json).state === "SETTLED" ? s : undefined;
+ },
+ { what: `the run at ${path} to settle`, timeoutMs, intervalMs: 200 }
+ );
+}
+
+function slotRow(status: Json, slot: string): Json {
+ const row = ((status.run as Json).slots as Json[]).find((s) => s.slot === slot);
+ expect(row).toBeDefined();
+ return row as Json;
+}
+
+/* ─────────────── tests ─────────────── */
+
+describe.skipIf(!MAGMUX_AVAILABLE)(
+ "team run → COMPLETED: shapes, types, terminal semantics",
+ () => {
+ test(
+ "a settled run carries typed §8 rows, keeps its terminal state, and cancel is a no-op on it",
+ async () => {
+ const path = join(layout.cwd, "done-run");
+ const started = await runTeam({ path, models: ["fake-answer"], input: PEAR });
+
+ // §4.1: the start answer's keys, run_id top level and inside `run`
+ for (const k of RUN_START_KEYS) expect(Object.keys(started)).toContain(k);
+ const runId = started.run_id as string;
+ expect(typeof runId).toBe("string");
+ expect((started.run as Json).run_id).toBe(runId);
+ expectRunRowTyped(started.run as Json);
+ expect((started.run as Json).kind).toBe("run");
+ expect((started.run as Json).path).toBe(path);
+ // CA-13: `<team_session_id>-<base36 start ms>-<6 hex>`
+ expect(runId.startsWith(`${started.team_session_id as string}-`)).toBe(true);
+ expect(runId).toMatch(/-[0-9a-z]+-[0-9a-f]{6}$/);
+ // the model→slot map
+ const slots = started.slots as Record<string, string>;
+ expect(Object.keys(slots)).toEqual(["fake-answer"]);
+ const slot = slots["fake-answer"] as string;
+ expect(slot).toBe("01");
+
+ const settled = await waitSettled(path);
+ expectContractMeta(settled);
+ expectRunRowTyped(settled.run as Json);
+ const run = settled.run as Json;
+ expect(run.run_id).toBe(runId);
+ expect(run.outcome).toBe("ok");
+ const row = slotRow(settled, slot);
+ expect(row.state).toBe("COMPLETED");
+ expect(row.reason).toBeNull();
+ expect(row.model).toBe("fake-answer");
+ expect(row.turns_completed).toBe(1);
+ expect(typeof row.pane).toBe("string"); // a pane was spawned
+ // legacy keys kept beside the contract keys (§8 B)
+ for (const k of ["startedAt", "models", "summary"])
+ expect(Object.keys(settled)).toContain(k);
+ expect(((settled.models as Json)[slot] as Json).state).toBe("COMPLETED");
+
+ // §8 A: the run is listed, SETTLED, with its own run_id
+ const list = ok(await team({ mode: "list" }));
+ expect(keys(list)).toEqual(["capabilities", "contract_version", "runs"]);
+ expectContractMeta(list);
+ const listed = (list.runs as Json[]).find((r) => r.run_id === runId) as Json;
+ expect(listed).toBeDefined();
+ expectRunRowTyped(listed);
+ expect(listed.state).toBe("SETTLED");
+ expect(listed.finished_at).toBe(run.finished_at);
+
+ // Terminal states are absorbing: cancel answers changed:false with the SAME state
+ const cancel = ok(await team({ mode: "cancel", path }));
+ expect(keys(cancel)).toEqual(TEAM_CANCEL_KEYS);
+ expect(cancel).toEqual({
+ run_id: runId,
+ path,
+ results: [{ slot, state: "COMPLETED", changed: false }],
+ });
+ const cancelOne = ok(await team({ mode: "cancel", path, slot, run_id: runId }));
+ expect(cancelOne.results).toEqual([{ slot, state: "COMPLETED", changed: false }]);
+
+ // …and later polls still report COMPLETED with the same finished_at
+ await Bun.sleep(1_000);
+ const later = ok(await team({ mode: "status", path }));
+ expect(slotRow(later, slot).state).toBe("COMPLETED");
+ expect((later.run as Json).finished_at).toBe(run.finished_at);
+
+ // A closed pane answers its last screen with final:true; equal since_seq → unchanged
+ const final = await waitFor(
+ async () => {
+ const c = ok(await team({ mode: "capture", path, slot }));
+ return c.final === true ? c : undefined;
+ },
+ { what: "the closed pane's final screen", timeoutMs: 15_000, intervalMs: 200 }
+ );
+ expect(keys(final)).toEqual(CAPTURE_KEYS);
+ expect((final.lines as string[]).length).toBe(50);
+ const same = ok(
+ await team({ mode: "capture", path, slot, since_seq: final.seq as number })
+ );
+ expect(same).toEqual({ unchanged: true, seq: final.seq, final: true });
+ },
+ T_TEST
+ );
+ }
+);
+
+describe.skipIf(!MAGMUX_AVAILABLE)("require_pattern and min_output_bytes (FR6)", () => {
+ test(
+ "require_pattern NEGATIVE control → EMPTY shape_mismatch, POSITIVE control → COMPLETED, mixed → partial",
+ async () => {
+ // negative: a pattern the fake's answer can never contain
+ const neg = join(layout.cwd, "pattern-negative");
+ const negStart = await runTeam({
+ path: neg,
+ models: ["fake-answer"],
+ input: PEAR,
+ require_pattern: "ZZZ_NEVER_IN_ANY_ANSWER_[0-9]{40}",
+ });
+ const negSlot = (negStart.slots as Record<string, string>)["fake-answer"] as string;
+ const negDone = await waitSettled(neg);
+ const negRow = slotRow(negDone, negSlot);
+ expect(negRow.state).toBe("EMPTY");
+ expect(negRow.reason).toBe("shape_mismatch");
+ expect((negDone.run as Json).outcome).not.toBe("ok");
+
+ // positive: the shape the fake does produce, anchored to the answer's start
+ const pos = join(layout.cwd, "pattern-positive");
+ const posStart = await runTeam({
+ path: pos,
+ models: ["fake-answer"],
+ input: PEAR,
+ require_pattern: "^ANSWER fake-answer [0-9a-f]{8}",
+ });
+ const posSlot = (posStart.slots as Record<string, string>)["fake-answer"] as string;
+ const posDone = await waitSettled(pos);
+ const posRow = slotRow(posDone, posSlot);
+ expect(posRow.state).toBe("COMPLETED");
+ expect(posRow.reason).toBeNull();
+ expect((posDone.run as Json).outcome).toBe("ok");
+
+ // mixed: one model matches, one does not → per-slot verdicts through the blind slot map
+ const mixed = join(layout.cwd, "pattern-mixed");
+ const mixStart = await runTeam({
+ path: mixed,
+ models: ["fake-answer", "fake-no_shape"],
+ input: PEAR,
+ require_pattern: "ANSWER",
+ });
+ const map = mixStart.slots as Record<string, string>;
+ expect(Object.keys(map).sort()).toEqual(["fake-answer", "fake-no_shape"]);
+ expect(Object.values(map).sort()).toEqual(["01", "02"]);
+ const mixDone = await waitSettled(mixed);
+ expectRunRowTyped(mixDone.run as Json);
+ const good = slotRow(mixDone, map["fake-answer"] as string);
+ const bad = slotRow(mixDone, map["fake-no_shape"] as string);
+ expect(good.model).toBe("fake-answer");
+ expect(bad.model).toBe("fake-no_shape");
+ expect(good.state).toBe("COMPLETED");
+ expect(bad.state).toBe("EMPTY");
+ expect(bad.reason).toBe("shape_mismatch");
+ expect((mixDone.run as Json).outcome).toBe("partial");
+ },
+ T_TEST
+ );
+
+ test(
+ "min_output_bytes above the answer's length → EMPTY empty_output; below it → COMPLETED",
+ async () => {
+ const big = join(layout.cwd, "min-bytes-high");
+ const bigStart = await runTeam({
+ path: big,
+ models: ["fake-answer"],
+ input: PEAR,
+ min_output_bytes: 100_000,
+ });
+ const bigSlot = (bigStart.slots as Record<string, string>)["fake-answer"] as string;
+ const bigRow = slotRow(await waitSettled(big), bigSlot);
+ expect(bigRow.state).toBe("EMPTY");
+ // the only FailureReason that names "below min_output_bytes" is empty_output
+ expect(bigRow.reason).toBe("empty_output");
+
+ const small = join(layout.cwd, "min-bytes-low");
+ const smallStart = await runTeam({
+ path: small,
+ models: ["fake-answer"],
+ input: PEAR,
+ min_output_bytes: 5,
+ });
+ const smallSlot = (smallStart.slots as Record<string, string>)["fake-answer"] as string;
+ const smallRow = slotRow(await waitSettled(small), smallSlot);
+ expect(smallRow.state).toBe("COMPLETED");
+ expect(smallRow.reason).toBeNull();
+ },
+ T_TEST
+ );
+
+ test(
+ "an invalid require_pattern is refused before anything starts (no session dir, no run listed)",
+ async () => {
+ const path = join(layout.cwd, "bad-pattern");
+ const r = await team({
+ mode: "run",
+ path,
+ models: ["fake-answer"],
+ input: PEAR,
+ require_pattern: "(",
+ });
+ expect(r.isError).toBe(true);
+ expect(r.text.startsWith("Error:")).toBe(true);
+ expect(r.text).toContain("invalid_args");
+ expect(existsSync(path)).toBe(false);
+ const list = ok(await team({ mode: "list" }));
+ expect(list.runs).toEqual([]);
+ },
+ T_TEST
+ );
+});
+
+describe.skipIf(!MAGMUX_AVAILABLE)("unknown agent (FR7)", () => {
+ test(
+ "team run with an unknown agent → slot FAILED agent_rejected, run SETTLED all-failed",
+ async () => {
+ const path = join(layout.cwd, "bad-agent");
+ const started = await team({
+ mode: "run",
+ path,
+ models: ["fake-answer"],
+ input: PEAR,
+ agent: "zzz-not-real",
+ });
+ // Either the run is refused outright, or it starts and the slot fails — never COMPLETED.
+ if (started.isError) {
+ expect(started.text).toContain("zzz-not-real");
+ return;
+ }
+ const slot = ((started.json as Json).slots as Record<string, string>)[
+ "fake-answer"
+ ] as string;
+ const done = await waitSettled(path);
+ const row = slotRow(done, slot);
+ expect(row.state).toBe("FAILED");
+ expect(row.reason).toBe("agent_rejected");
+ expect((done.run as Json).outcome).toBe("all-failed");
+ expectRunRowTyped(done.run as Json);
+ },
+ T_TEST
+ );
+});
+
+describe.skipIf(!MAGMUX_AVAILABLE)("run_id addressing (CA-13)", () => {
+ test(
+ "a reused path mints a distinct run_id per run; the old one stays addressable; one ACTIVE run per path",
+ async () => {
+ const path = join(layout.cwd, "review");
+ const first = await runTeam({ path, models: ["fake-answer"], input: PEAR });
+ await waitSettled(path);
+ // The next round reuses the path, cleared as /dev:dev clears it
+ rmSync(path, { recursive: true, force: true });
+ const second = await runTeam({ path, models: ["contract-fake-model"], input: HANG });
+ const id1 = first.run_id as string;
+ const id2 = second.run_id as string;
+ expect(id1).not.toBe(id2);
+
+ // one ACTIVE run per path: a third start is refused with the usual text error
+ const refused = await team({ mode: "run", path, models: ["fake-answer"], input: PEAR });
+ expect(refused.isError).toBe(true);
+ expect(refused.text.startsWith("Error: invalid_args:")).toBe(true);
+
+ // both listed under their own run_id; ACTIVE first
+ const list = ok(await team({ mode: "list" }));
+ const runs = list.runs as Json[];
+ expect(runs.map((r) => r.run_id)).toEqual([id2, id1]);
+ expect(runs.map((r) => r.state)).toEqual(["ACTIVE", "SETTLED"]);
+ for (const r of runs) {
+ expectRunRowTyped(r);
+ expect(r.path).toBe(path);
+ }
+
+ // status by the OLD run_id: that run, contract keys only, never the newer run's data
+ const old = ok(await team({ mode: "status", path, run_id: id1 }));
+ expect(keys(old)).toEqual(["capabilities", "contract_version", "run"]);
+ expect((old.run as Json).run_id).toBe(id1);
+ expect((old.run as Json).state).toBe("SETTLED");
+ // status by path: the newest run
+ const newest = ok(await team({ mode: "status", path }));
+ expect((newest.run as Json).run_id).toBe(id2);
+ expect((newest.run as Json).state).toBe("ACTIVE");
+
+ // capture of the superseded run's slot: its retained final screen
+ const cap = ok(await team({ mode: "capture", path, slot: "01", run_id: id1 }));
+ expect(cap.final).toBe(true);
+ // cancel of the superseded run: changed:false, its own state
+ const oldCancel = ok(await team({ mode: "cancel", path, run_id: id1 }));
+ expect(oldCancel).toEqual({
+ run_id: id1,
+ path,
+ results: [{ slot: "01", state: "COMPLETED", changed: false }],
+ });
+ // the newest is untouched by that
+ expect(slotRow(ok(await team({ mode: "status", path })), "01").state).not.toBe("CANCELLED");
+
+ // a well-formed run_id this server never minted → unknown_run
+ const neverMinted = `${second.team_session_id as string}-zzzzzzzz-abcdef`;
+ contractError(await team({ mode: "status", path, run_id: neverMinted }), "unknown_run");
+ contractError(await team({ mode: "cancel", path, run_id: neverMinted }), "unknown_run");
+ contractError(
+ await team({ mode: "capture", path, slot: "01", run_id: neverMinted }),
+ "unknown_run"
+ );
+
+ ok(await team({ mode: "cancel", path, run_id: id2 }));
+ },
+ T_TEST
+ );
+
+ test(
+ "after a server restart the old run_id is stale → unknown_run; status by path reads status.json",
+ async () => {
+ const path = join(layout.cwd, "restart");
+ const started = await runTeam({ path, models: ["fake-answer"], input: PEAR });
+ const runId = started.run_id as string;
+ const slot = (started.slots as Record<string, string>)["fake-answer"] as string;
+ await waitSettled(path);
+
+ await server.close();
+ server = await startServer();
+
+ // "A server restart forgets every run"
+ const list = ok(await team({ mode: "list" }));
+ expectContractMeta(list);
+ expect(list.runs).toEqual([]);
+ // a run_id no longer retained → unknown_run, never disk data carrying that id
+ contractError(await team({ mode: "status", path, run_id: runId }), "unknown_run");
+ contractError(await team({ mode: "cancel", path, run_id: runId }), "unknown_run");
+ contractError(await team({ mode: "capture", path, slot, run_id: runId }), "unknown_run");
+ // without run_id: status is built from status.json, rows idle_seconds:null, activity:null
+ const fromDisk = ok(await team({ mode: "status", path }));
+ expectContractMeta(fromDisk);
+ const run = fromDisk.run as Json;
+ expect(keys(run)).toEqual(TEAM_RUN_ROW_KEYS);
+ expect(run.state).toBe("SETTLED");
+ const row = slotRow(fromDisk, slot);
+ expect(keys(row)).toEqual(SLOT_ROW_KEYS);
+ expect(row.state).toBe("COMPLETED");
+ expect(row.idle_seconds).toBeNull();
+ expect(row.activity).toBeNull();
+ // capture of a run not retained → unknown_run
+ contractError(await team({ mode: "capture", path, slot }), "unknown_run");
+ },
+ T_TEST
+ );
+});
+
+describe.skipIf(!MAGMUX_AVAILABLE)("cancel (§8 C, NFR1)", () => {
+ test(
+ "cancel turns a RUNNING slot CANCELLED at once, is idempotent, and leaves no process or socket",
+ async () => {
+ const path = join(layout.cwd, "cancel-me");
+ const started = await runTeam({ path, models: ["contract-fake-model"], input: HANG });
+ const runId = started.run_id as string;
+ const live = ok(await team({ mode: "status", path }));
+ const liveRow = slotRow(live, "01");
+ expect(TERMINAL_STATES).not.toContain(liveRow.state as string);
+ expect(typeof liveRow.pane).toBe("string");
+
+ const first = ok(await team({ mode: "cancel", path }));
+ expect(first).toEqual({
+ run_id: runId,
+ path,
+ results: [{ slot: "01", state: "CANCELLED", changed: true }],
+ });
+ // "Terminal states reach list and status in the same tick they are decided"
+ const after = ok(await team({ mode: "status", path }));
+ const row = slotRow(after, "01");
+ expect(row.state).toBe("CANCELLED");
+ expect(row.reason).toBe("cancelled");
+ expectRunRowTyped(after.run as Json);
+ expect((after.run as Json).state).toBe("SETTLED");
+ expect((after.run as Json).outcome).toBe("all-failed");
+
+ const second = ok(await team({ mode: "cancel", path, run_id: runId }));
+ expect(second.results).toEqual([{ slot: "01", state: "CANCELLED", changed: false }]);
+ const third = ok(await team({ mode: "cancel", path, slot: "01" }));
+ expect(third.results).toEqual([{ slot: "01", state: "CANCELLED", changed: false }]);
+
+ // the reap finishes in the background within ~12 s — WITH THE SERVER STILL RUNNING,
+ // so this proves the cancel reaped, not the shutdown
+ const report = await waitNoOrphans({ sockRoot: paneRootOf(layout) }, 15_000);
+ expect(report.processes).toEqual([]);
+ expect(report.files.filter((f) => f.endsWith(".sock"))).toEqual([]);
+ expect(report.files.filter((f) => f.includes(liveRow.pane as string))).toEqual([]);
+
+ // the closed pane is still capturable as its final screen
+ const cap = ok(await team({ mode: "capture", path, slot: "01" }));
+ expect(cap.final).toBe(true);
+ },
+ T_TEST
+ );
+});
+
+describe.skipIf(!MAGMUX_AVAILABLE)("capture (§8 D, NFR2)", () => {
+ test(
+ "seq never goes backwards, a seq from elsewhere returns the full screen, lines are right-trimmed",
+ async () => {
+ const path = join(layout.cwd, "capture");
+ await runTeam({ path, models: ["contract-fake-model"], input: HANG });
+
+ const first = await waitFor(
+ async () => {
+ const c = ok(await team({ mode: "capture", path, slot: "01" }));
+ return (c.seq as number) >= 1 ? c : undefined;
+ },
+ { what: "a first frame", timeoutMs: 15_000, intervalMs: 100 }
+ );
+ let prev = first.seq as number;
+ for (let i = 0; i < 5; i++) {
+ const started = Date.now();
+ const c = ok(await team({ mode: "capture", path, slot: "01" }));
+ // NFR2: a capture never blocks on the child
+ expect(Date.now() - started).toBeLessThan(5_000);
+ expect(keys(c)).toEqual(CAPTURE_KEYS);
+ expect(c.seq as number).toBeGreaterThanOrEqual(prev);
+ prev = c.seq as number;
+ const lines = c.lines as string[];
+ expect(lines.length).toBe(c.rows as number);
+ for (const l of lines) {
+ expect(typeof l).toBe("string");
+ expect(l).toBe(l.replace(/\s+$/, ""));
+ }
+ const cur = c.cursor as Json;
+ expect(Number.isInteger(cur.x) && Number.isInteger(cur.y)).toBe(true);
+ expect(cur.x as number).toBeGreaterThanOrEqual(0);
+ expect(cur.y as number).toBeGreaterThanOrEqual(0);
+ expect(cur.x as number).toBeLessThanOrEqual(c.cols as number);
+ expect(cur.y as number).toBeLessThan(c.rows as number);
+ await Bun.sleep(150);
+ }
+ // visible UI: the screen is not blank once a frame arrived
+ const now = ok(await team({ mode: "capture", path, slot: "01" }));
+ expect((now.lines as string[]).some((l) => l.trim() !== "")).toBe(true);
+
+ // a since_seq HIGHER than the current one (e.g. from a previous server) → full screen
+ const higher = ok(
+ await team({ mode: "capture", path, slot: "01", since_seq: (now.seq as number) + 1000 })
+ );
+ expect(keys(higher)).toEqual(CAPTURE_KEYS);
+ // the current seq → unchanged, or a newer full screen if it moved in between
+ const same = ok(
+ await team({ mode: "capture", path, slot: "01", since_seq: higher.seq as number })
+ );
+ if (same.unchanged === true) {
+ expect(keys(same)).toEqual(UNCHANGED_KEYS);
+ expect(same).toEqual({ unchanged: true, seq: higher.seq, final: false });
+ } else {
+ expect(same.seq as number).toBeGreaterThan(higher.seq as number);
+ }
+
+ // a terminal state is reflected by capture eventually: final:true after cancel
+ ok(await team({ mode: "cancel", path }));
+ const final = await waitFor(
+ async () => {
+ const c = ok(await team({ mode: "capture", path, slot: "01" }));
+ return c.final === true ? c : undefined;
+ },
+ { what: "final:true after cancel", timeoutMs: 15_000, intervalMs: 200 }
+ );
+ expect(final.seq as number).toBeGreaterThanOrEqual(prev);
+ },
+ T_TEST
+ );
+});
diff --git a/packages/cli/src/mcp-blackbox/tool-surface.blackbox.test.ts b/packages/cli/src/mcp-blackbox/tool-surface.blackbox.test.ts
new file mode 100644
index 00000000..4d16a203
--- /dev/null
+++ b/packages/cli/src/mcp-blackbox/tool-surface.blackbox.test.ts
@@ -0,0 +1,188 @@
+/**
+ * BLACK-BOX: the MCP tool surface and the text/JSON error forms that need no pane
+ * (FR9 A–E, FR11, architecture §4, §4.3). Real claudish MCP server over stdio.
+ *
+ * Written from spec.md and the frozen mod contract v1 only. No implementation file was read.
+ */
+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
+import { existsSync } from "node:fs";
+import { join } from "node:path";
+import { useBlackboxEnv } from "../test-helpers/blackbox-env.js";
+import {
+ McpServer,
+ type ToolResult,
+ paneOrphans,
+ serverEnv,
+} from "../test-helpers/contract-mcp.js";
+import { type TempLayout, makeTempLayout } from "../test-helpers/contract-records.js";
+
+useBlackboxEnv();
+
+type Json = Record<string, unknown>;
+const keys = (v: unknown): string[] => Object.keys(v as Json).sort();
+const CAPABILITIES = ["list", "status", "cancel", "capture", "capture_since_seq", "capture_spans"];
+
+let layout: TempLayout;
+let server: McpServer;
+
+beforeEach(async () => {
+ layout = makeTempLayout("bbsurface");
+ server = await McpServer.start({ env: serverEnv(layout), cwd: layout.cwd });
+});
+
+afterEach(async () => {
+ await server.close();
+ const report = await paneOrphans(layout);
+ layout.cleanup();
+ expect(report).toEqual({ processes: [], files: [] });
+});
+
+function contractError(r: ToolResult, code: string): void {
+ expect(r.isError).toBe(true);
+ const body = JSON.parse(r.text) as Json;
+ expect(keys(body)).toEqual(["error"]);
+ expect(keys(body.error)).toEqual(["code", "message"]);
+ expect((body.error as Json).code).toBe(code);
+ expect(typeof (body.error as Json).message).toBe("string");
+}
+
+describe("tool list (FR11, §4)", () => {
+ test("14 tools, capture_session among them, team modes are exactly the seven", async () => {
+ const tools = await server.listTools();
+ const names = tools.map((t) => t.name);
+ expect(names.length).toBe(14);
+ expect(new Set(names).size).toBe(14);
+ for (const n of [
+ "team",
+ "create_session",
+ "send_input",
+ "get_output",
+ "cancel_session",
+ "list_sessions",
+ "get_diagnostics",
+ "capture_session",
+ ])
+ expect(names).toContain(n);
+ // FR8: run_prompt / compare_models are out of scope and stay
+ expect(names).toContain("run_prompt");
+ expect(names).toContain("compare_models");
+
+ const teamTool = tools.find((t) => t.name === "team");
+ const props = (teamTool?.inputSchema?.properties ?? {}) as Record<string, Json>;
+ expect(((props.mode as Json).enum as string[]).slice().sort()).toEqual(
+ ["run", "judge", "run-and-judge", "status", "cancel", "list", "capture"].sort()
+ );
+ expect(teamTool?.inputSchema?.required).toEqual(["mode"]);
+ for (const p of [
+ "since_seq",
+ "spans",
+ "run_id",
+ "slot",
+ "path",
+ "require_pattern",
+ "min_output_bytes",
+ ])
+ expect(Object.keys(props)).toContain(p);
+
+ const cap = tools.find((t) => t.name === "capture_session");
+ const capProps = Object.keys(cap?.inputSchema?.properties ?? {}).sort();
+ expect(capProps).toEqual(["session_id", "since_seq", "spans"]);
+ expect(cap?.inputSchema?.required).toEqual(["session_id"]);
+ });
+});
+
+describe("team: mode and argument errors", () => {
+ test("an unknown team mode answers the text `Error: Unknown mode: <mode>`", async () => {
+ const r = await server.callTool("team", { mode: "bogus-mode", path: join(layout.cwd, "x") });
+ expect(r.isError).toBe(true);
+ expect(r.text.startsWith("Error: Unknown mode: bogus-mode")).toBe(true);
+ });
+
+ test("list needs no path and answers an empty run list with contract meta on a fresh server", async () => {
+ const r = await server.callTool("team", { mode: "list" });
+ expect(r.isError).toBe(false);
+ const body = r.json as Json;
+ expect(keys(body)).toEqual(["capabilities", "contract_version", "runs"]);
+ expect(body.contract_version).toBe(1);
+ expect(body.capabilities).toEqual(CAPABILITIES);
+ expect(body.runs).toEqual([]);
+ });
+
+ test("§8 team verbs answer bad arguments with an invalid_args ContractError", async () => {
+ const path = join(layout.cwd, "none");
+ // path is required by every mode except list
+ contractError(await server.callTool("team", { mode: "cancel", slot: "01" }), "invalid_args");
+ contractError(
+ await server.callTool("team", { mode: "capture", slot: "01", run_id: "a-1-abcdef" }),
+ "invalid_args"
+ );
+ // a path outside the working directory
+ contractError(await server.callTool("team", { mode: "cancel", path: "/etc" }), "invalid_args");
+ contractError(
+ await server.callTool("team", { mode: "capture", path: "/etc", slot: "01" }),
+ "invalid_args"
+ );
+ // wrongly typed arguments
+ contractError(
+ await server.callTool("team", { mode: "status", path, run_id: 12345 }),
+ "invalid_args"
+ );
+ // unknown run at a valid path
+ contractError(await server.callTool("team", { mode: "status", path }), "unknown_run");
+ contractError(
+ await server.callTool("team", { mode: "cancel", path, slot: "01" }),
+ "unknown_run"
+ );
+ // spans is a boolean (§4.1); a string is the caller's error, refused before the lookup
+ // the same way a string since_seq is
+ contractError(
+ await server.callTool("team", { mode: "capture", path, slot: "01", spans: "yes" }),
+ "invalid_args"
+ );
+ });
+
+ test("spawning modes keep their text `Error:` form for bad input (§4.3) and start nothing", async () => {
+ const path = join(layout.cwd, "spawn-errors");
+ // `input` and `input_file` together: "Passing both is an error"
+ const both = await server.callTool("team", {
+ mode: "run",
+ path,
+ models: ["fake-answer"],
+ input: "x",
+ input_file: "input.md",
+ });
+ expect(both.isError).toBe(true);
+ expect(both.text.startsWith("Error")).toBe(true);
+ expect(() => JSON.parse(both.text)).toThrow();
+ // `run` without models
+ const noModels = await server.callTool("team", { mode: "run", path, input: "x" });
+ expect(noModels.isError).toBe(true);
+ expect(noModels.text.startsWith("Error")).toBe(true);
+ // a print-mode flag is refused (the pane is interactive)
+ const printFlag = await server.callTool("team", {
+ mode: "run",
+ path,
+ models: ["fake-answer"],
+ input: "x",
+ claude_flags: "-p",
+ });
+ expect(printFlag.isError).toBe(true);
+ expect(printFlag.text.startsWith("Error")).toBe(true);
+ const list = await server.callTool("team", { mode: "list" });
+ expect((list.json as Json).runs).toEqual([]);
+ expect(existsSync(join(path, "status.json"))).toBe(false);
+ });
+});
+
+describe("channel: argument errors that need no pane", () => {
+ test("create_session refuses a print-mode flag with text `Error: invalid_args:`", async () => {
+ const r = await server.callTool("create_session", {
+ model: "contract-fake-model",
+ claude_flags: "--output-format stream-json",
+ });
+ expect(r.isError).toBe(true);
+ expect(r.text.startsWith("Error: invalid_args:")).toBe(true);
+ const list = await server.callTool("list_sessions", { include_completed: true });
+ expect((list.json as Json).sessions).toEqual([]);
+ });
+});