Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/app/src/recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export function matchesLineage(saved: Partial<HerdrIdentity>, live: HerdrIdentit
export function safeToAdopt(saved: Partial<HerdrIdentity>, live: HerdrIdentity, claimedSessions: Set<string>): 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<HerdrIdentity>, 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<HerdrIdentity>, 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}`);
Expand Down
6 changes: 6 additions & 0 deletions packages/app/test/recovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down