Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(mcp): read the input box before retyping a send whose reply was lost
Jack Rudenko5330afe6a315
diff --git a/packages/cli/src/pane/magmux-client.test.ts b/packages/cli/src/pane/magmux-client.test.tsindex a220bc71..0e587b65 100644--- a/packages/cli/src/pane/magmux-client.test.ts+++ b/packages/cli/src/pane/magmux-client.test.ts@@ -82,7 +82,7 @@ describe.skipIf(!MAGMUX)(c.close();});- test("EOF resolves every pending request client_closed and emits disconnected; it is not a death", async () => {+ test("EOF resolves every pending request client_lost and emits disconnected; it is not a death", async () => {const m = startMagmux("sleep 30");const c = await MagmuxClient.connect(m.sock);const disc = new Promise<{ sawShutdown: boolean }>((r) => c.on("disconnected", r));@@ -90,7 +90,7 @@ describe.skipIf(!MAGMUX)(const pending = c.request({ type: "capabilities" }, 5000);m.proc.kill("SIGKILL");const [r, d] = await Promise.all([pending, disc]);- expect(r.ok === true || (r.ok === false && r.code === "client_closed")).toBe(true);+ expect(r.ok === true || (r.ok === false && r.code === "client_lost")).toBe(true);expect(d.sawShutdown).toBe(false);expect(await c.request({ type: "list" })).toMatchObject({ ok: false, code: "client_closed" });});diff --git a/packages/cli/src/pane/magmux-client.ts b/packages/cli/src/pane/magmux-client.tsindex 093f07fe..ba5208c7 100644--- a/packages/cli/src/pane/magmux-client.ts+++ b/packages/cli/src/pane/magmux-client.ts@@ -206,7 +206,8 @@ export class MagmuxClient {this.closed = true;for (const [id, p] of this.pending) {clearTimeout(p.timer);- p.resolve({ ok: false, code: "client_closed", error: "connection closed" });+ // the request WAS written: magmux may have acted on it, only the reply is lost+ p.resolve({ ok: false, code: "client_lost", error: "connection closed before the reply" });this.pending.delete(id);}this.emit("disconnected", { sawShutdown: this.sawShutdown });diff --git a/packages/cli/src/pane/pane-session.integration.test.ts b/packages/cli/src/pane/pane-session.integration.test.tsindex 21d0e0aa..802aa392 100644--- a/packages/cli/src/pane/pane-session.integration.test.ts+++ b/packages/cli/src/pane/pane-session.integration.test.ts@@ -859,6 +859,39 @@ describe.skipIf(!MAGMUX)(T);+ test(+ "a send whose reply is lost after magmux typed it is not typed twice (§2.9 step 4)",+ async () => {+ const r = await start("answer", {+ shape: "interactive",+ initialPrompt: undefined,+ decide: () => "continue",+ });+ await r.s.ready;+ type Req = (m: Record<string, unknown>, ms?: number) => Promise<unknown>;+ const client = (r.s as unknown as { client: { request: Req } }).client;+ const real = client.request.bind(client);+ let lost = 0;+ client.request = async (m, ms) => {+ const reply = await real(m, ms);+ if (m.type === "send" && typeof m.text === "string" && lost++ === 0)+ return {+ ok: false,+ code: "client_lost",+ error: "connection closed before the reply",+ };+ return reply;+ };+ r.s.send("/model haiku");+ await until(r, (s) => s.turnsCompleted === 1 && s.state === "AWAITING_INPUT");+ expect(r.turns[0]?.answer).toBe("Set model to `haiku`"); // once, not "haiku/model haiku"+ expect(r.s.snapshot().anomalies).toContain("send_reply_lost");+ r.s.cancel();+ await finish(r);+ },+ T+ );+test("send validation: /clear and /resume are unsupported; a terminal session refuses",async () => {diff --git a/packages/cli/src/pane/pane-session.ts b/packages/cli/src/pane/pane-session.tsindex 8917f2a0..4cd21fe1 100644--- a/packages/cli/src/pane/pane-session.ts+++ b/packages/cli/src/pane/pane-session.ts@@ -870,6 +870,12 @@ export class PaneSessionImpl implements PaneSession, RegisteredPane {adm.deliveredAt = Date.now();}+ /**+ * §2.9 step 4: retry only a send magmux refused (`busy`, `pane_*`) or one never+ * written (`client_closed`). A send whose REPLY was lost (`client_timeout`,+ * `client_lost`) may already have been typed, so the screen decides: retyping it+ * blindly put the line in the box twice and no witness could ever match.+ */private async typeLine(text: string, enter: boolean): Promise<boolean> {let why = "disconnected";for (let attempt = 0; attempt < 2; attempt++) {@@ -878,6 +884,9 @@ export class PaneSessionImpl implements PaneSession, RegisteredPane {const r = await c.request({ type: "send", pane: 0, text, typed: true, enter }, 10_000);if (r.ok) return true;why = r.code;+ if (r.code === "client_timeout" || r.code === "client_lost") {+ if (await this.landedAfterLostReply(text, enter)) return true;+ }}await Bun.sleep(500);}@@ -885,6 +894,24 @@ export class PaneSessionImpl implements PaneSession, RegisteredPane {return false;}+ /** After a lost reply: the line is in the box (typed), or already submitted (echo, witness). */+ private async landedAfterLostReply(text: string, enter: boolean): Promise<boolean> {+ await this.nextFrame(1000);+ const head = text.slice(0, 40);+ const box = inputBox(this.screen);+ let landed = !!box && box.text.startsWith(head);+ if (landed && enter) await this.key("enter");+ if (!landed && enter) {+ this.follower.poll();+ const cur = this.follower.view().current;+ landed =+ (!!cur && cur.index === this.turnIndex && cur.acceptedAt !== null) ||+ this.screen.lines.some((l) => l.startsWith(`❯ ${head}`));+ }+ if (landed) this.anomaly("send_reply_lost");+ return landed;+ }+private async key(k: string): Promise<boolean> {const c = this.client;if (!c) return false;