Skip to content

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

feat(mcp): add the pane contract types and slot lifecycle table

Jack Rudenkoac17b1c86467
diff --git a/packages/cli/src/pane/contract.ts b/packages/cli/src/pane/contract.ts
new file mode 100644
index 00000000..91f72664
--- /dev/null
+++ b/packages/cli/src/pane/contract.ts
@@ -0,0 +1,249 @@
+/**
+ * The frozen mod contract, version 1 — the single source of the wire types that the
+ * peer's Claude Code mod reads from `team(mode="list"|"status"|"cancel"|"capture")`,
+ * `list_sessions`, `cancel_session` and `capture_session`.
+ *
+ * Pasted verbatim from the session architecture §8 (including CA-12 `contract_version`
+ * / `capabilities` and CA-13 `run_id`). Fields never change meaning or type within
+ * version 1; new fields may only be ADDED. `FailureReason` lives here because both
+ * owners (team and the channel) produce it.
+ */
+
+export const CONTRACT_VERSION = 1;
+
+/** v1 announces exactly these. */
+export const CAPABILITIES = [
+ "list",
+ "status",
+ "cancel",
+ "capture",
+ "capture_since_seq",
+ "capture_spans",
+] as const;
+
+export interface ContractMeta {
+ contract_version: 1;
+ /** v1: exactly CAPABILITIES */
+ capabilities: string[];
+}
+
+/** Closed set. The mod colours by it. */
+export type SlotState =
+ /** Pane spawned; Claude Code booting, or the prompt the session was CREATED with delivered but not yet
+ * accepted. A promptless session's first send_input goes AWAITING_INPUT → RUNNING, never back to
+ * STARTING. Bounded: leaves within 90 s + 30 s. Non-terminal. */
+ | "STARTING"
+ /** A turn is in progress: prompt accepted; the model thinking, a tool running, Stop hooks or background
+ * agents still finishing. Never ended by a timer (team). Non-terminal. */
+ | "RUNNING"
+ /** Waiting for text: an interactive create_session between turns (activity null), or a turn blocked on
+ * a question (activity "AskUserQuestion"). A send_input during a question does NOT answer it: claudish
+ * presses Esc, which declines the question and interrupts the turn, and the text becomes the next
+ * prompt. Team slots never stay here. Non-terminal. */
+ | "AWAITING_INPUT"
+ /** A permission / plan-approval dialog is showing (only when caller flags override -y). A send_input
+ * declines it the same way. Same team rule as above. Non-terminal. */
+ | "AWAITING_PERMISSION"
+ /** Turn settled with a usable answer (team: passed require_pattern / min_output_bytes), or an
+ * interactive session whose child exited 0 while idle after /exit or after ≥ 1 settled turn. Terminal. */
+ | "COMPLETED"
+ /** The session broke: boot failure, prompt never accepted, task file not fully read, child exited
+ * (incl. unknown agent), API error, blocked (team), pane lost. Terminal. */
+ | "FAILED"
+ /** Stopped by team(mode="cancel") or cancel_session. Terminal. */
+ | "CANCELLED"
+ /** create_session timeout_seconds elapsed (team slots never time out). Terminal. */
+ | "TIMEOUT"
+ /** Turn settled but the answer is unusable: empty, refused, below min_output_bytes, or
+ * shape_mismatch. Terminal. */
+ | "EMPTY";
+
+export const SLOT_STATES: readonly SlotState[] = [
+ "STARTING",
+ "RUNNING",
+ "AWAITING_INPUT",
+ "AWAITING_PERMISSION",
+ "COMPLETED",
+ "FAILED",
+ "CANCELLED",
+ "TIMEOUT",
+ "EMPTY",
+];
+
+export const TERMINAL_STATES: readonly SlotState[] = [
+ "COMPLETED",
+ "FAILED",
+ "CANCELLED",
+ "TIMEOUT",
+ "EMPTY",
+];
+
+export function isTerminalState(s: SlotState): boolean {
+ return TERMINAL_STATES.includes(s);
+}
+
+export type FailureReason =
+ | "cancelled" // the caller stopped it — a decision, not a fault
+ | "timeout" // create_session timeout_seconds; team-grid (CLI) pane end
+ | "boot_timeout" // no REPL prompt within the boot deadline, screen still changing (screen tail in detail)
+ | "boot_blocked" // a static non-REPL screen that is not a named dialog blocked boot (screen text in detail)
+ | "first_run_dialog" // a named first-run dialog blocked boot (dialog text in detail)
+ | "agent_rejected" // the child refused --agent (its own "not found. Available agents" line)
+ | "prompt_not_accepted" // the first prompt never produced an acceptance witness within the admission bound
+ | "prompt_not_read" // a file-delivered turn settled without Read returning every line of its task file
+ | "child_exited" // pane child exited before the turn settled (exit code + last line + screen tail)
+ | "pane_lost" // the magmux PROCESS died without reporting the pane's exit
+ | "blocked" // team slot stopped on a question/permission dialog team cannot answer (question in detail)
+ | "api_error" // the settled turn is an isApiErrorMessage entry (or a screen-mode error row)
+ | "refused" // the settled turn's stop_reason is "refusal"
+ | "empty_output"
+ | "shape_mismatch";
+
+export const FAILURE_REASONS: readonly FailureReason[] = [
+ "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",
+];
+
+/** B — one row per team slot, and the base of every list_sessions row. */
+export interface SlotRow {
+ /** team: anonymised slot id ("01"); session: the session_id */
+ slot: string;
+ /** as requested */
+ model: string;
+ /** display name of the claudish provider serving it; null until known */
+ provider: string | null;
+ state: SlotState;
+ /** set on FAILED / EMPTY / CANCELLED / TIMEOUT; null otherwise (incl. COMPLETED). One exception: a
+ * pre-contract state read from disk is FAILED with null (§B) */
+ reason: FailureReason | null;
+ /** billed input incl. cache reads/writes, main conversation + subagents; null = no source yet */
+ tokens_in: number | null;
+ tokens_out: number | null;
+ /** null = unknown (always null for native Claude routes) */
+ cost_usd: number | null;
+ /** includes the child's Read call(s) of a file-delivered prompt */
+ tool_calls: number;
+ /** prompts whose turn settled (one per prompt, however many internal loops) */
+ turns_completed: number;
+ /** ISO 8601: last screen change or transcript append */
+ last_activity_at: string | null;
+ /** seconds since last_activity_at; null in terminal states */
+ idle_seconds: number | null;
+ /** RUNNING: tool name, "thinking", "background" (waiting on background agents), or "finishing" (the
+ * model's message ended; waiting for Claude Code's end-of-turn record, typically while Stop hooks run);
+ * AWAITING_PERMISSION: blocked tool; AWAITING_INPUT on a question: "AskUserQuestion"; otherwise null.
+ * Treat unknown values as "busy". */
+ activity: string | null;
+ /** informational only (do not parse, no verb takes it); null if no pane was ever spawned */
+ pane: string | null;
+}
+
+/** list_sessions row = SlotRow + session fields. `slot` equals `session_id`. */
+export interface SessionRow extends SlotRow {
+ session_id: string;
+ /** ISO */
+ started_at: string;
+ /** ISO */
+ completed_at: string | null;
+ elapsed_seconds: number;
+}
+
+/** A — one team run this server knows. */
+export interface TeamRunRow {
+ /** [CA-13] unique per team(mode="run") start, distinct even when `path` is reused (opaque; format
+ * `<team_session_id>-<base36 start ms>-<6 hex>`). Same value as the start answer's `run_id`. A judge
+ * sub-run has its own run_id. */
+ run_id: string;
+ /** absolute session directory (the `path` every other team mode takes) */
+ path: string;
+ /** "judge" = the judging/ sub-run of a judge or run-and-judge */
+ kind: "run" | "judge";
+ /** ISO */
+ started_at: string;
+ /** ISO, set when the last slot turned terminal */
+ finished_at: string | null;
+ /** run-level, distinct from SlotState: SETTLED ⇔ every slot terminal */
+ state: "ACTIVE" | "SETTLED";
+ /** null while ACTIVE; same words as the result card */
+ outcome: "ok" | "partial" | "all-failed" | null;
+ /** sorted by slot id */
+ slots: SlotRow[];
+}
+
+/** [CA-12] an object, not a bare array */
+export interface TeamListResult extends ContractMeta {
+ runs: TeamRunRow[];
+}
+export interface SessionListResult extends ContractMeta {
+ sessions: SessionRow[];
+}
+/** team(mode="status") adds these keys to its existing payload. */
+export interface TeamStatusAdditions extends ContractMeta {
+ run: TeamRunRow;
+}
+
+/** C */
+export interface TeamCancelResult {
+ /** [CA-13] */
+ run_id: string;
+ path: string;
+ /** changed = this call moved it to CANCELLED */
+ results: Array<{ slot: string; state: SlotState; changed: boolean }>;
+}
+export interface SessionCancelResult {
+ session_id: string;
+ state: SlotState;
+ changed: boolean;
+}
+
+/** One magmux span run: [col, len, fg, bg, attr]. Colour -1 = default, 0..255 indexed,
+ * ≥ 16777216 truecolor (`c & 0xFFFFFF`); attr bitmask, 1 = bold. */
+export type SpanRun = [col: number, len: number, fg: number, bg: number, attr: number];
+
+/** D */
+export interface CaptureResult {
+ /** claudish-owned. 0 = no frame received yet (lines all ""); ≥ 1 after the first frame; +1 per visible
+ * change (text, colours, cursor, alt screen); never resets for a pane's life */
+ seq: number;
+ /** 160 */
+ cols: number;
+ /** 50 */
+ rows: number;
+ cursor: { x: number; y: number };
+ /** exactly `rows` entries, plain text, right-trimmed; Claude Code's screen (no scrollback) */
+ lines: string[];
+ /** true = the pane is closed and this is its last screen */
+ final: boolean;
+ /** only when spans:true was asked */
+ spans?: SpanRun[][];
+}
+export interface CaptureUnchanged {
+ unchanged: true;
+ seq: number;
+ final: boolean;
+}
+
+/** E */
+export interface ContractError {
+ error: {
+ code: "unknown_run" | "unknown_slot" | "unknown_session" | "invalid_args";
+ message: string;
+ };
+}
+
+export function contractMeta(): ContractMeta {
+ return { contract_version: CONTRACT_VERSION, capabilities: [...CAPABILITIES] };
+}
diff --git a/packages/cli/src/pane/slot-state.test.ts b/packages/cli/src/pane/slot-state.test.ts
new file mode 100644
index 00000000..1700078a
--- /dev/null
+++ b/packages/cli/src/pane/slot-state.test.ts
@@ -0,0 +1,132 @@
+import { describe, expect, test } from "bun:test";
+import { SLOT_STATES, TERMINAL_STATES } from "./contract.js";
+import {
+ PHASES,
+ type Phase,
+ type PhaseEvent,
+ TERMINAL_PHASES,
+ TRANSITIONS,
+ nextPhase,
+ wireState,
+} from "./slot-state.js";
+
+const ALL_EVENTS: PhaseEvent[] = [
+ "boot_ready_idle",
+ "boot_ready_admit",
+ "boot_dialog",
+ "boot_blocked",
+ "boot_deadline",
+ "admit",
+ "prompt_accepted",
+ "admit_deadline",
+ "admit_abandoned",
+ "blocked_question",
+ "blocked_permission",
+ "unblocked",
+ "turn_continue",
+ "rewake",
+ "verdict_completed",
+ "verdict_empty",
+ "verdict_failed",
+ "exit_clean",
+ "pane_exit",
+ "pane_lost",
+ "cancel",
+ "timeout",
+];
+
+describe("slot-state transition table (§5)", () => {
+ const legal: Array<[Phase, PhaseEvent, Phase]> = [
+ ["BOOTING", "boot_ready_idle", "IDLE"],
+ ["BOOTING", "boot_ready_admit", "ADMITTING"],
+ ["BOOTING", "boot_dialog", "FAILED"],
+ ["BOOTING", "boot_blocked", "FAILED"],
+ ["BOOTING", "boot_deadline", "FAILED"],
+ ["ADMITTING", "prompt_accepted", "RUNNING"],
+ ["ADMITTING", "admit_deadline", "FAILED"],
+ ["ADMITTING", "admit_abandoned", "IDLE"],
+ ["RUNNING", "blocked_question", "QUESTION"],
+ ["RUNNING", "blocked_permission", "PERMISSION"],
+ ["RUNNING", "turn_continue", "IDLE"],
+ ["RUNNING", "verdict_completed", "COMPLETED"],
+ ["RUNNING", "verdict_empty", "EMPTY"],
+ ["RUNNING", "verdict_failed", "FAILED"],
+ ["IDLE", "admit", "ADMITTING"],
+ ["IDLE", "rewake", "RUNNING"],
+ ["IDLE", "exit_clean", "COMPLETED"],
+ ["QUESTION", "unblocked", "RUNNING"],
+ ["QUESTION", "verdict_failed", "FAILED"],
+ ["PERMISSION", "unblocked", "RUNNING"],
+ ["PERMISSION", "verdict_failed", "FAILED"],
+ ];
+ for (const [from, ev, to] of legal) {
+ test(`${from} --${ev}--> ${to}`, () => expect(nextPhase(from, ev)).toBe(to));
+ }
+
+ test("every live phase accepts pane_exit, pane_lost, cancel and timeout", () => {
+ for (const p of PHASES.filter((x) => !TERMINAL_PHASES.includes(x))) {
+ expect(nextPhase(p, "pane_exit")).toBe("FAILED");
+ expect(nextPhase(p, "pane_lost")).toBe("FAILED");
+ expect(nextPhase(p, "cancel")).toBe("CANCELLED");
+ expect(nextPhase(p, "timeout")).toBe("TIMEOUT");
+ }
+ });
+
+ test("terminals absorb every event (a late exit never overturns CANCELLED or TIMEOUT)", () => {
+ for (const p of TERMINAL_PHASES)
+ for (const ev of ALL_EVENTS) expect(nextPhase(p, ev)).toBeNull();
+ });
+
+ test("an edge missing from the table is illegal (null), never a throw", () => {
+ expect(nextPhase("BOOTING", "prompt_accepted")).toBeNull();
+ expect(nextPhase("IDLE", "verdict_completed")).toBeNull();
+ expect(nextPhase("QUESTION", "blocked_permission")).toBeNull();
+ expect(nextPhase("PERMISSION", "blocked_question")).toBeNull();
+ expect(nextPhase("RUNNING", "admit")).toBeNull();
+ });
+
+ test("the table has exactly the listed legal edges plus the four any-live edges", () => {
+ let count = 0;
+ for (const p of PHASES) count += Object.keys(TRANSITIONS[p]).length;
+ expect(count).toBe(legal.length + 4 * 6);
+ });
+
+ test("no wait turns into another wait: no QUESTION/PERMISSION/IDLE edge between them", () => {
+ const waits: Phase[] = ["IDLE", "QUESTION", "PERMISSION"];
+ for (const a of waits)
+ for (const ev of ALL_EVENTS) {
+ const b = nextPhase(a, ev);
+ if (b && b !== a) expect(waits.includes(b)).toBe(false);
+ }
+ });
+});
+
+describe("wireState", () => {
+ test("maps every phase into the closed nine-state set", () => {
+ for (const p of PHASES) {
+ expect(SLOT_STATES).toContain(wireState(p, true));
+ expect(SLOT_STATES).toContain(wireState(p, false));
+ }
+ });
+
+ test("BOOTING is STARTING; ADMITTING is STARTING only for the initial delivery (X-M1)", () => {
+ expect(wireState("BOOTING", false)).toBe("STARTING");
+ expect(wireState("ADMITTING", true)).toBe("STARTING");
+ expect(wireState("ADMITTING", false)).toBe("RUNNING");
+ });
+
+ test("a promptless session's first send goes AWAITING_INPUT → RUNNING, never back to STARTING", () => {
+ const idle = wireState("IDLE", false);
+ const admitting = wireState(nextPhase("IDLE", "admit")!, false);
+ expect([idle, admitting]).toEqual(["AWAITING_INPUT", "RUNNING"]);
+ });
+
+ test("IDLE and QUESTION are AWAITING_INPUT, PERMISSION is AWAITING_PERMISSION, terminals map to themselves", () => {
+ expect(wireState("RUNNING", false)).toBe("RUNNING");
+ expect(wireState("IDLE", false)).toBe("AWAITING_INPUT");
+ expect(wireState("QUESTION", false)).toBe("AWAITING_INPUT");
+ expect(wireState("PERMISSION", false)).toBe("AWAITING_PERMISSION");
+ for (const p of TERMINAL_PHASES) expect<string>(wireState(p, false)).toBe(p);
+ expect<string[]>([...TERMINAL_PHASES].sort()).toEqual([...TERMINAL_STATES].sort());
+ });
+});
diff --git a/packages/cli/src/pane/slot-state.ts b/packages/cli/src/pane/slot-state.ts
new file mode 100644
index 00000000..ef1f2de4
--- /dev/null
+++ b/packages/cli/src/pane/slot-state.ts
@@ -0,0 +1,146 @@
+/**
+ * The pane lifecycle as a transition table over INTERNAL phases (architecture §2.2,
+ * D7), plus the one function that turns a phase into a contract `SlotState`.
+ *
+ * Phases make "booting vs. prompt in flight" and "idle vs. blocked on a question" legal-
+ * transition distinctions instead of flags, while the wire keeps the frozen nine states.
+ * Illegal transitions are refused and recorded by the caller — `nextPhase` returns null
+ * and never throws — and terminals absorb: a late pane `exit` can never overturn a
+ * CANCELLED or TIMEOUT already recorded.
+ */
+
+import type { SlotState } from "./contract.js";
+
+export type Phase =
+ | "BOOTING" // pane spawned, Claude Code not yet at an empty input box
+ | "ADMITTING" // a prompt is being delivered / awaiting its acceptance witness
+ | "RUNNING" // accepted turn in progress (incl. Stop hooks and background agents)
+ | "IDLE" // interactive session between turns, input box empty
+ | "QUESTION" // turn blocked on a pending AskUserQuestion
+ | "PERMISSION" // turn blocked on a permission / plan-approval dialog
+ | "COMPLETED"
+ | "FAILED"
+ | "CANCELLED"
+ | "TIMEOUT"
+ | "EMPTY";
+
+export type PhaseEvent =
+ | "boot_ready_idle"
+ | "boot_ready_admit"
+ | "boot_dialog"
+ | "boot_blocked"
+ | "boot_deadline"
+ | "admit" // pump starts delivering a queued prompt (IDLE only)
+ | "prompt_accepted" // witness found after the turn's offset
+ | "admit_deadline" // first prompt never accepted → FAILED prompt_not_accepted
+ | "admit_abandoned" // a later prompt never accepted → back to IDLE (anomaly, frame meta)
+ | "blocked_question"
+ | "blocked_permission"
+ | "unblocked"
+ | "turn_continue" // decide() returned "continue"
+ | "rewake" // interactive IDLE: a background notification woke the model (D23)
+ | "verdict_completed"
+ | "verdict_empty"
+ | "verdict_failed"
+ | "exit_clean" // interactive child exited 0 while IDLE after a requested /exit or ≥ 1 settled turn
+ | "pane_exit"
+ | "pane_lost"
+ | "cancel"
+ | "timeout";
+
+export const PHASES: readonly Phase[] = [
+ "BOOTING",
+ "ADMITTING",
+ "RUNNING",
+ "IDLE",
+ "QUESTION",
+ "PERMISSION",
+ "COMPLETED",
+ "FAILED",
+ "CANCELLED",
+ "TIMEOUT",
+ "EMPTY",
+];
+
+export const TERMINAL_PHASES: readonly Phase[] = [
+ "COMPLETED",
+ "FAILED",
+ "CANCELLED",
+ "TIMEOUT",
+ "EMPTY",
+];
+
+const ANY_LIVE = {
+ pane_exit: "FAILED",
+ pane_lost: "FAILED",
+ cancel: "CANCELLED",
+ timeout: "TIMEOUT",
+} as const;
+
+export const TRANSITIONS = {
+ BOOTING: {
+ boot_ready_idle: "IDLE",
+ boot_ready_admit: "ADMITTING",
+ boot_dialog: "FAILED",
+ boot_blocked: "FAILED",
+ boot_deadline: "FAILED",
+ ...ANY_LIVE,
+ },
+ ADMITTING: {
+ prompt_accepted: "RUNNING",
+ admit_deadline: "FAILED",
+ admit_abandoned: "IDLE",
+ ...ANY_LIVE,
+ },
+ RUNNING: {
+ blocked_question: "QUESTION",
+ blocked_permission: "PERMISSION",
+ turn_continue: "IDLE",
+ verdict_completed: "COMPLETED",
+ verdict_empty: "EMPTY",
+ verdict_failed: "FAILED",
+ ...ANY_LIVE,
+ },
+ IDLE: { admit: "ADMITTING", rewake: "RUNNING", exit_clean: "COMPLETED", ...ANY_LIVE },
+ QUESTION: { unblocked: "RUNNING", verdict_failed: "FAILED", ...ANY_LIVE },
+ PERMISSION: { unblocked: "RUNNING", verdict_failed: "FAILED", ...ANY_LIVE },
+ COMPLETED: {},
+ FAILED: {},
+ CANCELLED: {},
+ TIMEOUT: {},
+ EMPTY: {},
+} as const satisfies Record<Phase, Partial<Record<PhaseEvent, Phase>>>;
+
+/** The phase `ev` leads to from `from`, or null when the table has no such edge. */
+export function nextPhase(from: Phase, ev: PhaseEvent): Phase | null {
+ const row = TRANSITIONS[from] as Partial<Record<PhaseEvent, Phase>>;
+ return row[ev] ?? null;
+}
+
+export function isTerminalPhase(p: Phase): boolean {
+ return TERMINAL_PHASES.includes(p);
+}
+
+/**
+ * The only place a phase becomes a contract SlotState. Keyed on the delivery, not the
+ * turn index (X-M1): `initialDelivery` is true only while the prompt the session was
+ * CREATED with is being admitted, so a promptless session's first send goes
+ * AWAITING_INPUT → RUNNING and never back to STARTING.
+ */
+export function wireState(p: Phase, initialDelivery: boolean): SlotState {
+ switch (p) {
+ case "BOOTING":
+ return "STARTING";
+ case "ADMITTING":
+ return initialDelivery ? "STARTING" : "RUNNING";
+ case "RUNNING":
+ return "RUNNING";
+ case "IDLE":
+ case "QUESTION":
+ return "AWAITING_INPUT";
+ case "PERMISSION":
+ return "AWAITING_PERMISSION";
+ default:
+ return p;
+ }
+}