Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(mcp): accept /compact at its typed record, not after compaction
Jack Rudenko4311d6f45b50
diff --git a/ai-docs/architecture/pane-session.md b/ai-docs/architecture/pane-session.mdindex 226edf1f..01b8ed5c 100644--- a/ai-docs/architecture/pane-session.md+++ b/ai-docs/architecture/pane-session.md@@ -72,7 +72,15 @@ offset and before turn N+1's. Evidence from an earlier turn can therefore neverone. The turn is ACCEPTED when its **witness** appears after the offset: a main-chain user recordwhose text equals the typed line (`origin.kind:"human"`), or a `<command-name>/x</command-name>`record for a slash command — a user record, or (2.1.287+, `/color yellow` captured on 2.1.291) a-`system/local_command` record, which a user-record-only witness never accepts. Measured: the witness lands 370–470 ms after a typed send; with a+`system/local_command` record, which a user-record-only witness never accepts. A slash command is+also witnessed by a non-`isMeta` user record whose text equals the typed line: `/compact` writes+one at submit (captured on 2.1.290 and 2.1.291, `"content":"/compact"`, no `origin`) but APPENDS its+`<command-name>` record only when compaction finishes — 10.4 s for a two-turn conversation, minutes+on a real context. Waiting for `<command-name>` outlived the 30 s admission bound: the turn was+abandoned (`send_not_accepted`) while Claude Code compacted, and the pump typed the next queued+prompt into a busy REPL. Accepted at the typed record, compaction is a RUNNING turn with no timer+(D10), and the prompts queued behind it wait for path L. `/model` and `/pear` write no such record+and are still witnessed by `<command-name>`. Measured: the witness lands 370–470 ms after a typed send; with a`sleep 12` UserPromptSubmit hook the box clears at once but the witness arrives 12,501 ms later.The follower polls on every frame (debounced 250 ms) and on a 1 s backstop.diff --git a/packages/cli/src/pane/pane-session.integration.test.ts b/packages/cli/src/pane/pane-session.integration.test.tsindex 4b418143..401d4128 100644--- a/packages/cli/src/pane/pane-session.integration.test.ts+++ b/packages/cli/src/pane/pane-session.integration.test.ts@@ -463,6 +463,37 @@ describe.skipIf(!MAGMUX)(},T);++ test(+ "a /compact that outlasts the admission bound stays admitted; the prompt queued behind it is answered",+ async () => {+ // 2.1.291 writes <command-name>/compact only when compaction FINISHES; a real one+ // takes minutes. Compaction here outlasts the admission bound by 2.5 s.+ const r = await start("answer", {+ shape: "interactive",+ initialPrompt: undefined,+ decide: () => "continue",+ admitTimeoutMs: 1500,+ env: { FAKE_GAP_MS_COMPACT: "4000" },+ });+ await r.s.ready;+ expect(r.s.send("/compact").ok).toBe(true);+ expect(r.s.send("Reply with exactly KIWI.").ok).toBe(true);+ await until(r, (s) => s.state === "RUNNING");+ await Bun.sleep(2500); // past the admission bound, mid-compaction+ expect(r.s.snapshot()).toMatchObject({ state: "RUNNING", pendingInputs: 1 });+ await until(r, (s) => s.turnsCompleted === 2 && s.state === "AWAITING_INPUT", 20_000);+ expect(r.turns.map((t) => t.settledBy)).toEqual(["local_command", "turn_duration"]);+ expect(r.turns[0]?.answer).toBe("Compacted (ctrl+o to see full summary)");+ expect(r.turns[1]?.answer).toBe(`ANSWER fake-answer ${sha8("Reply with exactly KIWI.")}`);+ expect(r.s.snapshot().anomalies.filter((a) => a.startsWith("send_not_accepted"))).toEqual(+ []+ );+ r.s.cancel();+ await finish(r);+ },+ T+ );});describe.concurrent("command turns with a file", () => {diff --git a/packages/cli/src/pane/prompt-delivery.test.ts b/packages/cli/src/pane/prompt-delivery.test.tsindex d2b24d72..1076a268 100644--- a/packages/cli/src/pane/prompt-delivery.test.ts+++ b/packages/cli/src/pane/prompt-delivery.test.ts@@ -61,11 +61,11 @@ describe("plain lines are typed", () => {});describe("slash commands", () => {- test("a short single-line command is typed and witnessed by its command name", () => {+ test("a short single-line command is typed and witnessed by its command name or its typed line", () => {expect(planDelivery("/pear", TURN_DIR, 1, true)).toEqual({mode: "command",line: "/pear",- witness: { kind: "command", name: "pear" },+ witness: { kind: "command", name: "pear", line: "/pear" },});expect(planDelivery("/review\tthe diff", TURN_DIR, 1, true)).toMatchObject({mode: "command",diff --git a/packages/cli/src/pane/prompt-delivery.ts b/packages/cli/src/pane/prompt-delivery.tsindex 04acb6e2..586d3d76 100644--- a/packages/cli/src/pane/prompt-delivery.ts+++ b/packages/cli/src/pane/prompt-delivery.ts@@ -173,7 +173,11 @@ export function planDelivery(const normalised = stripControl(firstLine.trimStart().replace(/\t/g, " ")).trimEnd();const more = text.slice(firstLine.length).trim().length > 0;if (!more && normalised.length <= MAX_TYPED_LINE) {- return { mode: "command", line: normalised, witness: { kind: "command", name } };+ return {+ mode: "command",+ line: normalised,+ witness: { kind: "command", name, line: normalised },+ };}if (!readAvailable)return {@@ -182,10 +186,11 @@ export function planDelivery(};const file = turnFilePath(turnDir, turnIndex);const tf = renderTurnFile(text);+ const line = commandPointer(name, file);return {mode: "command",- line: commandPointer(name, file),- witness: { kind: "command", name },+ line,+ witness: { kind: "command", name, line },file,fileContent: tf.content,lines: tf.lines,diff --git a/packages/cli/src/pane/transcript-follower.test.ts b/packages/cli/src/pane/transcript-follower.test.tsindex bb7a0a15..93089873 100644--- a/packages/cli/src/pane/transcript-follower.test.ts+++ b/packages/cli/src/pane/transcript-follower.test.ts@@ -1,5 +1,6 @@import { afterEach, describe, expect, test } from "bun:test";import { join } from "node:path";+import { planDelivery } from "./prompt-delivery.js";import { FIXTURES } from "./test-helpers/fixtures.js";import {type Rec,@@ -657,6 +658,54 @@ describe("local commands", () => {expect(cur(v2[0]).localCommandOutput).toBe("Compacted (ctrl+o to see full summary)");});+ test("/compact (2.1.291) is accepted at its typed record, before the compaction it starts", () => {+ // The <command-name>/compact record is APPENDED only after the boundary and the summary,+ // i.e. when compaction finishes (10.4 s here, minutes on a real context); waiting for it+ // outlived the 30 s admission bound. The plain "/compact" record is written at submit.+ const n = "local-commands";+ const recs = transcriptRecords(n);+ const typed = recs.findIndex((r) => r.type === "user" && textOf(r) === "/compact");+ const boundary = recs.findIndex((r) => r.subtype === "compact_boundary");+ const named = recs.findIndex((r) =>+ textOf(r).includes("<command-name>/compact</command-name>")+ );+ expect(typed).toBeLessThan(boundary);+ expect(boundary).toBeLessThan(named);+ const witness = (planDelivery("/compact", "/tmp/unused", 1, true) as { witness: Witness })+ .witness;+ // mid-compaction: the boundary is written, the <command-name> record is not+ const mid = run(n, [{ at: typed, witness }], { upTo: boundary + 1 });+ expect(cur(mid.views[0]).acceptedAt).toBe(recs[typed]!.timestamp);+ expect(cur(mid.views[0]).localCommandOutput).toBeNull();+ const byNameOnly = run(n, [{ at: typed, witness: { kind: "command", name: "compact" } }], {+ upTo: boundary + 1,+ });+ expect(cur(byNameOnly.views[0]).acceptedAt).toBeNull();+ // finished: the stdout settles it through path L, and the summary counts as a compaction+ const kiwi = recs.findIndex((r) => textOf(r).startsWith("Reply with exactly KIWI"));+ const done = cur(run(n, [{ at: typed, witness }], { upTo: kiwi }).views[0]);+ expect(done.localCommandOutput).toBe("Compacted (ctrl+o to see full summary)");+ expect(done.compactions).toBe(1);+ expect(done.lastAssistant).toBeNull();+ });++ test("a typed-line witness never matches an isMeta record or another command's line", () => {+ const n = "local-commands";+ const recs = transcriptRecords(n);+ const typed = recs.findIndex((r) => r.type === "user" && textOf(r) === "/compact");+ const other = run(n, [{ at: typed, witness: { kind: "command", name: "x", line: "/x" } }], {+ upTo: typed + 1,+ });+ expect(cur(other.views[0]).acceptedAt).toBeNull();+ const caveat = recs.findIndex((r) => r.isMeta === true && textOf(r).includes("caveat"));+ const meta = run(+ n,+ [{ at: caveat, witness: { kind: "command", name: "x", line: textOf(recs[caveat]!) } }],+ { upTo: caveat + 1 }+ );+ expect(cur(meta.views[0]).acceptedAt).toBeNull();+ });+test("/color (2.1.291): the witness and the stdout are system/local_command records", () => {const n = "local-commands";const recs = transcriptRecords(n);diff --git a/packages/cli/src/pane/transcript-follower.ts b/packages/cli/src/pane/transcript-follower.tsindex 894d669f..94c0bf90 100644--- a/packages/cli/src/pane/transcript-follower.ts+++ b/packages/cli/src/pane/transcript-follower.ts@@ -13,7 +13,8 @@* Record shapes are pinned by the phase-2 captures of Claude Code 2.1.290* (`ai-docs/reports/mcp-magmux-panes/phase2-captures.md`), notably:* - the witness is a main-chain user record whose text equals the typed line- * (`origin.kind:"human"`), or a `<command-name>/x</command-name>` record;+ * (`origin.kind:"human"`; for `/compact` with no `origin`), or a+ * `<command-name>/x</command-name>` record;* - between the last assistant record and `turn_duration` Claude Code writes* `attachment/prompt_snapshot` and state records, so "directly followed" means "no* `stop_hook_summary` in between";@@ -41,7 +42,8 @@ import { join } from "node:path";export type Witness =| { kind: "text"; text: string } // typed plain line or the file-reference instruction (exact)- | { kind: "command"; name: string }; // slash command: its <command-name> record+ /** slash command: its `<command-name>` record, or the plain user record of the typed `line` */+ | { kind: "command"; name: string; line?: string };export interface TurnDelivery {/** absolute path of the turn file the child must Read */@@ -243,6 +245,10 @@ function matchesWitness(r: Rec, w: Witness): boolean {if (!isMainUser(r) || hasToolResult(r)) return false;const text = userText(r);if (w.kind === "text") return !r.isMeta && text.trim() === w.text.trim();+ // `/compact` writes its <command-name> record only AFTER compaction finishes, which takes+ // minutes on a real context; the plain record of the typed line is written at submit+ // (captured: 2.1.290 and 2.1.291, `"content":"/compact"`, no isMeta), so it is the witness.+ if (w.line !== undefined && !r.isMeta && text.trim() === w.line.trim()) return true;return text.includes(`<command-name>/${w.name}</command-name>`);}