From ba464e9f816b674ef55f6b04c6eb5c6aea188393 Mon Sep 17 00:00:00 2001 From: Brendan Date: Thu, 17 Sep 2026 04:31:10 +0000 Subject: [PATCH] GEN-2053 finish runs with deleted cwd --- packages/app/src/recovery.ts | 6 +++++- packages/app/test/recovery.test.ts | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/app/src/recovery.ts b/packages/app/src/recovery.ts index 757c3949..f75a60ee 100644 --- a/packages/app/src/recovery.ts +++ b/packages/app/src/recovery.ts @@ -34,7 +34,11 @@ export function matchesLineage(saved: Partial, live: HerdrIdentit export function safeToAdopt(saved: Partial, live: HerdrIdentity, claimedSessions: Set): boolean { return matchesLineage(saved, live) && (!saved.kind || saved.kind === live.kind) && (!saved.cwd || saved.cwd === live.cwd) && !claimedSessions.has(live.sessionValue); } /** Native sessions and aliases may rotate; terminal + worktree own the pane. */ export function ownsPane(saved: Partial, live: HerdrIdentity, worktreePath: string): boolean { - return Boolean(saved.terminalId && saved.terminalId === live.terminalId && worktreePath && live.cwd === worktreePath && (!saved.paneId || saved.paneId === live.paneId)); + // Linux appends " (deleted)" to /proc cwd links after the run directory is + // removed. The terminal still identifies the same owned pane in that case; + // rejecting it traps an already-idle agent in the recovery loop forever. + const ownsWorktree = live.cwd === worktreePath || live.cwd === `${worktreePath} (deleted)`; + return Boolean(saved.terminalId && saved.terminalId === live.terminalId && worktreePath && ownsWorktree && (!saved.paneId || saved.paneId === live.paneId)); } export function findOwnedAgent(saved: Partial, agents: HerdrIdentity[], worktreePath: string, expectedName: string, expectedKind: string, runId: string): HerdrIdentity | undefined { const runPath = (cwd: string) => cwd.includes(`/.factorize-runs/${runId}`) || cwd.includes(`/.factorize-worktrees/${runId}`); diff --git a/packages/app/test/recovery.test.ts b/packages/app/test/recovery.test.ts index 0e59803e..5d4ce845 100644 --- a/packages/app/test/recovery.test.ts +++ b/packages/app/test/recovery.test.ts @@ -20,6 +20,12 @@ describe("Herdr 0.9 recovery identity", () => { expect(ownsPane({ terminalId: "term-other", paneId: "pane-2" }, moved, "/repo")).toBe(false); }); + it("keeps pane ownership after Linux marks the run cwd as deleted", () => { + const completed = { ...moved, status: "idle", cwd: "/repo/.factorize-runs/run-1 (deleted)" }; + expect(ownsPane({ terminalId: "term-1", paneId: "pane-2" }, completed, "/repo/.factorize-runs/run-1")).toBe(true); + expect(ownsPane({ terminalId: "term-1", paneId: "pane-2" }, { ...completed, cwd: "/repo/other (deleted)" }, "/repo/.factorize-runs/run-1")).toBe(false); + }); + it("parses agent list arrays", () => { const agents = parseAgentList(JSON.stringify({ result: { agents: [{ name: "flow-1", kind: "codex", pane_id: "pane-1", session: { id: "abc" } }] } })); expect(agents).toHaveLength(1);