Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(subagent): recover delete cleanup after crash

yushen77260796488a
diff --git a/src/agent/subagent/registry-recovery.test.ts b/src/agent/subagent/registry-recovery.test.ts
new file mode 100644
index 0000000000..046db13f8d
--- /dev/null
+++ b/src/agent/subagent/registry-recovery.test.ts
@@ -0,0 +1,75 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import type { SubagentRunRecord } from "./types.js";
+
+const loadSubagentRunsMock = vi.fn<() => Map<string, SubagentRunRecord>>();
+const saveSubagentRunsMock = vi.fn();
+const readLatestAssistantReplyMock = vi.fn();
+const runCoalescedAnnounceFlowMock = vi.fn(() => false);
+const resolveSessionDirMock = vi.fn((sessionId: string) => `/tmp/${sessionId}`);
+const closeAgentMock = vi.fn();
+const getHubMock = vi.fn(() => ({ closeAgent: closeAgentMock }));
+const rmSyncMock = vi.fn();
+
+vi.mock("./registry-store.js", () => ({
+ loadSubagentRuns: loadSubagentRunsMock,
+ saveSubagentRuns: saveSubagentRunsMock,
+}));
+
+vi.mock("./announce.js", () => ({
+ readLatestAssistantReply: readLatestAssistantReplyMock,
+ runCoalescedAnnounceFlow: runCoalescedAnnounceFlowMock,
+}));
+
+vi.mock("../session/storage.js", () => ({
+ resolveSessionDir: resolveSessionDirMock,
+}));
+
+vi.mock("../../hub/hub-singleton.js", () => ({
+ getHub: getHubMock,
+ isHubInitialized: vi.fn(() => false),
+}));
+
+vi.mock("node:fs", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("node:fs")>();
+ return {
+ ...actual,
+ rmSync: rmSyncMock,
+ };
+});
+
+describe("subagent registry recovery cleanup", () => {
+ beforeEach(() => {
+ vi.resetModules();
+ vi.clearAllMocks();
+ loadSubagentRunsMock.mockReturnValue(new Map());
+ runCoalescedAnnounceFlowMock.mockReturnValue(false);
+ });
+
+ it("deletes child session on recovery even when findings were already captured", async () => {
+ const now = Date.now();
+ const record: SubagentRunRecord = {
+ runId: "run-1",
+ childSessionId: "child-1",
+ requesterSessionId: "parent-1",
+ task: "task",
+ cleanup: "delete",
+ createdAt: now - 1000,
+ startedAt: now - 900,
+ endedAt: now - 100,
+ outcome: { status: "ok" },
+ findings: "done",
+ findingsCaptured: true,
+ cleanupHandled: false,
+ announced: false,
+ };
+
+ loadSubagentRunsMock.mockReturnValue(new Map([["run-1", record]]));
+
+ const registry = await import("./registry.js");
+ registry.initSubagentRegistry();
+
+ expect(readLatestAssistantReplyMock).not.toHaveBeenCalled();
+ expect(resolveSessionDirMock).toHaveBeenCalledWith("child-1");
+ expect(rmSyncMock).toHaveBeenCalledWith("/tmp/child-1", { recursive: true, force: true });
+ });
+});
diff --git a/src/agent/subagent/registry.ts b/src/agent/subagent/registry.ts
index 980c27936a..0dbfa971aa 100644
--- a/src/agent/subagent/registry.ts
+++ b/src/agent/subagent/registry.ts
@@ -60,9 +60,12 @@ export function initSubagentRegistry(): void {
if (!record.findingsCaptured) {
captureFindings(record);
- if (record.cleanup === "delete") {
- deleteChildSession(record.childSessionId);
- }
+ }
+
+ // Recovery cleanup must be independent from findings capture:
+ // the process may crash after captureFindings() persisted but before deletion.
+ if (record.cleanup === "delete" && !record.cleanupHandled) {
+ deleteChildSession(record.childSessionId);
}
affectedRequesters.add(record.requesterSessionId);