Skip to content

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

fix(mcp): end a pane whose watcher or record could not be created

Jack Rudenko8c08a61de74e
diff --git a/packages/cli/src/pane/pane-registry.test.ts b/packages/cli/src/pane/pane-registry.test.ts
index 6dcf11a1..5df504e4 100644
--- a/packages/cli/src/pane/pane-registry.test.ts
+++ b/packages/cli/src/pane/pane-registry.test.ts
@@ -7,7 +7,15 @@
import { afterAll, describe, expect, test } from "bun:test";
import { type ChildProcess, execFileSync, spawn } from "node:child_process";
-import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import {
+ chmodSync,
+ existsSync,
+ mkdirSync,
+ readFileSync,
+ readdirSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
import { join } from "node:path";
import { waitForExit } from "../process-tree.js";
import { assertMagmuxAvailable, ensureSockRoot, spawnPaneWatcher } from "./pane-launch.js";
@@ -464,6 +472,38 @@ describe.skipIf(!MAGMUX)(`pane registry, real magmux (${MAGMUX ? "" : NO_MAGMUX_
files: [],
});
}, 20_000);
+
+ test("a record that cannot be written refuses the start and leaves no launch dir", async () => {
+ const t = env();
+ const root = ensureSockRoot(t.sockRoot);
+ chmodSync(join(root, "panes"), 0o500); // still private, but unwritable
+ try {
+ const uuid = crypto.randomUUID();
+ await expect(
+ startPaneSession({
+ kind: "t",
+ label: "01",
+ callerFlags: [],
+ spawnModel: "fake-answer",
+ cwd: t.cwd,
+ sessionUuid: uuid,
+ transcriptPath: t.transcriptPathFor(uuid),
+ slotEnv: {},
+ shape: "one-shot",
+ initialPrompt: "x",
+ readAvailable: true,
+ parentEnv: t.env,
+ sockRoot: root,
+ decide: () => ({ state: "COMPLETED" }),
+ onBlocked: () => "wait",
+ })
+ ).rejects.toThrow(/cannot write the pane record/);
+ expect(readdirSync(root).filter((n) => n.startsWith("launch-"))).toEqual([]);
+ expect(livePaneCount(root)).toBe(0);
+ } finally {
+ chmodSync(join(root, "panes"), 0o700);
+ }
+ }, 20_000);
});
describe("records", () => {
diff --git a/packages/cli/src/pane/pane-session.ts b/packages/cli/src/pane/pane-session.ts
index 085fd4b7..8abdb960 100644
--- a/packages/cli/src/pane/pane-session.ts
+++ b/packages/cli/src/pane/pane-session.ts
@@ -384,15 +384,21 @@ export class PaneSessionImpl implements PaneSession, RegisteredPane {
cwd: string;
}): Promise<void> {
const recordPath = recordPathOf(this.root, this.paneId);
- this.watcher = spawnPaneWatcher({
- paneId: this.paneId,
- sessionUuid: this.o.sessionUuid,
- ctlDir: this.ctlDir,
- sockPath: this.sockPath,
- recordPath,
- turnDir: this.turnDir,
- sockRoot: this.root,
- });
+ try {
+ this.watcher = spawnPaneWatcher({
+ paneId: this.paneId,
+ sessionUuid: this.o.sessionUuid,
+ ctlDir: this.ctlDir,
+ sockPath: this.sockPath,
+ recordPath,
+ turnDir: this.turnDir,
+ sockRoot: this.root,
+ });
+ } catch (e) {
+ // a registered session must still reach a terminal state, or it stays counted
+ // (livePaneCount) with its record and dirs on disk until the owner exits
+ return this.spawnFailed(e);
+ }
updateRecord(this.root, this.paneId, { watcherPid: this.watcher.pid ?? null });
let proc: ChildProcess | null = null;
try {
@@ -1779,19 +1785,30 @@ export async function startPaneSession(o: PaneSessionOptions): Promise<PaneSessi
throw e;
}
const p = preparePane(o, root, parentEnv);
- writeRecord(root, {
- paneId: p.paneId,
- ownerPid: process.pid,
- ownerStart: ownerStartOfSelf(),
- watcherPid: null,
- magmuxPid: null,
- panePid: null,
- sessionUuid: o.sessionUuid,
- sockPath: p.sockPath,
- launcherDir: p.ctlDir,
- turnDir: p.turnDir,
- createdAt: new Date().toISOString(),
- });
+ try {
+ writeRecord(root, {
+ paneId: p.paneId,
+ ownerPid: process.pid,
+ ownerStart: ownerStartOfSelf(),
+ watcherPid: null,
+ magmuxPid: null,
+ panePid: null,
+ sessionUuid: o.sessionUuid,
+ sockPath: p.sockPath,
+ launcherDir: p.ctlDir,
+ turnDir: p.turnDir,
+ createdAt: new Date().toISOString(),
+ });
+ } catch (e) {
+ // nothing was spawned: remove the dirs preparePane made (validated paths only)
+ removePaneFiles(root, {
+ paneId: p.paneId,
+ sockPath: p.sockPath,
+ launcherDir: p.ctlDir,
+ turnDir: p.turnDir,
+ });
+ throw new PaneStartError("pane_lost", `cannot write the pane record: ${String(e)}`);
+ }
const session = new PaneSessionImpl(o, {
paneId: p.paneId,
sockPath: p.sockPath,