Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(team): keep a team run record open when its meta.json write fails
Jack Rudenkod3bb55405e82
diff --git a/ai-docs/architecture/team-lifecycle.md b/ai-docs/architecture/team-lifecycle.mdindex a4c51383..2899ca84 100644--- a/ai-docs/architecture/team-lifecycle.md+++ b/ai-docs/architecture/team-lifecycle.md@@ -165,7 +165,8 @@ directory the magus `claudish` plugin monitor already watches:- `<sessionsDir>/team-<8 hex>/meta.json` — `{kind:"team", status, startedAt,completedAt, elapsedSeconds, slots, ok, failed, cancelled}` plus`reason: "start-failed"` when the run never started; written atomically by- `finishTeamRun`, which is first-call-wins and never throws.+ `finishTeamRun`, which never throws and keeps the first outcome it managed to+ write — a failed write does not use up the record's one end.**The end is wired INTO `startModels`, not after it.** `TeamRunOptions.onSettled`is called from `done`'s `finally`, after the settled `status.txt` render, insidediff --git a/packages/cli/src/channel/session-manager.ts b/packages/cli/src/channel/session-manager.tsindex e071edf7..f7582600 100644--- a/packages/cli/src/channel/session-manager.ts+++ b/packages/cli/src/channel/session-manager.ts@@ -778,7 +778,7 @@ export class SessionManager {private readonly _launcherPid: number | undefined;/** Team run record id → its `startedAt`, until `finishTeamRun` writes its end. */private readonly teamRunStarts = new Map<string, string>();- /** Team run records already ended. First `finishTeamRun` call wins. */+ /** Team run records whose end is on disk. The first `finishTeamRun` write to succeed wins. */private readonly settledTeamRuns = new Set<string>();constructor(options?: SessionManagerOptions) {@@ -855,18 +855,19 @@ export class SessionManager {* End a team run's record: write `<record dir>/meta.json` atomically, at most* once.*- * First call wins; a later call writes nothing and reports one stderr line.- * It NEVER throws: a write failure is reported on stderr, so a caller's- * `catch` always rethrows its ORIGINAL error rather than a disk error that- * masked it. A lost write leaves a record with no end, which an observer- * reports from the writer's liveness — never a false verdict.+ * The first call whose write SUCCEEDS wins; a later call writes nothing and+ * reports one stderr line. A failed write does not count, so a retry can+ * still end the record. It NEVER throws: a write failure is reported on+ * stderr, so a caller's `catch` always rethrows its ORIGINAL error rather+ * than a disk error that masked it. A lost write leaves a record with no+ * end, which an observer reports from the writer's liveness — never a false+ * verdict.*/finishTeamRun(record: string, outcome: TeamRunOutcome): void {if (this.settledTeamRuns.has(record)) {process.stderr.write(`[claudish] team run record ${record} already ended; ignoring\n`);return;}- this.settledTeamRuns.add(record);try {const dir = this.diskSessionDir(record);if (dir === null) throw new Error("not a record id");@@ -888,13 +889,14 @@ export class SessionManager {cancelled: outcome.cancelled,...(outcome.reason ? { reason: outcome.reason } : {}),});+ // Only now: the end is on disk. `startedAt` stays for a retry until then.+ this.settledTeamRuns.add(record);+ this.teamRunStarts.delete(record);} catch (err) {process.stderr.write(`[claudish] could not write the end of team run record ${record}: ` +`${err instanceof Error ? err.message : String(err)}\n`);- } finally {- this.teamRunStarts.delete(record);}}diff --git a/packages/cli/src/team-run-settles-once.test.ts b/packages/cli/src/team-run-settles-once.test.tsindex a01ea8d7..48c3cb64 100644--- a/packages/cli/src/team-run-settles-once.test.ts+++ b/packages/cli/src/team-run-settles-once.test.ts@@ -18,7 +18,15 @@import { afterEach, beforeEach, describe, expect, it } from "bun:test";import { type ChildProcess, spawn } from "node:child_process";-import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";+import {+ chmodSync,+ mkdirSync,+ mkdtempSync,+ readFileSync,+ rmSync,+ statSync,+ writeFileSync,+} from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";@@ -155,4 +163,26 @@ describe("team run record settles once", () => {await manager.shutdownAll();}});++ it("does not spend the record's one end on a write that failed", async () => {+ const sessionsDir = join(tempRoot, "sessions");+ const manager = new SessionManager({ hostPid: 1, sessionsDir });+ const outcome = { status: "completed", slots: 1, ok: 1, failed: 0, cancelled: 0 } as const;+ try {+ const record = manager.recordTeamRun({ teamPath: tempRoot, slots: 1 });+ const metaPath = join(sessionsDir, record, "meta.json");+ // A non-empty directory where meta.json goes: the rename onto it fails.+ mkdirSync(join(metaPath, "blocker"), { recursive: true });+ manager.finishTeamRun(record, outcome);+ expect(statSync(metaPath).isDirectory()).toBe(true);++ rmSync(metaPath, { recursive: true, force: true });+ manager.finishTeamRun(record, outcome);+ const meta = JSON.parse(readFileSync(metaPath, "utf-8"));+ expect(meta.status).toBe("completed");+ expect(meta.ok).toBe(1);+ } finally {+ await manager.shutdownAll();+ }+ });});