Skip to content

Commit dd11bdd

Browse files
committed
fix: clean up attachments for killed subagent runs
1 parent 807daf5 commit dd11bdd

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/agents/subagent-registry.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { promises as fs } from "node:fs";
2+
import os from "node:os";
3+
import path from "node:path";
14
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
25

36
const noop = () => {};
@@ -431,4 +434,34 @@ describe("subagent registry seam flow", () => {
431434
});
432435
});
433436
});
437+
438+
it("removes attachments for killed delete-mode runs", async () => {
439+
const attachmentsRootDir = await fs.mkdtemp(
440+
path.join(os.tmpdir(), "openclaw-kill-attachments-"),
441+
);
442+
const attachmentsDir = path.join(attachmentsRootDir, "child");
443+
await fs.mkdir(attachmentsDir, { recursive: true });
444+
await fs.writeFile(path.join(attachmentsDir, "artifact.txt"), "artifact");
445+
446+
mod.registerSubagentRun({
447+
runId: "run-killed-delete-attachments",
448+
childSessionKey: "agent:main:subagent:killed-delete-attachments",
449+
requesterSessionKey: "agent:main:main",
450+
requesterDisplayKey: "main",
451+
task: "kill and delete attachments",
452+
cleanup: "delete",
453+
attachmentsDir,
454+
attachmentsRootDir,
455+
});
456+
457+
const updated = mod.markSubagentRunTerminated({
458+
runId: "run-killed-delete-attachments",
459+
reason: "manual kill",
460+
});
461+
462+
expect(updated).toBe(1);
463+
await vi.waitFor(async () => {
464+
await expect(fs.access(attachmentsDir)).rejects.toMatchObject({ code: "ENOENT" });
465+
});
466+
});
434467
});

src/agents/subagent-registry.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,10 @@ export function markSubagentRunTerminated(params: {
16541654
childSessionKey: entry.childSessionKey,
16551655
});
16561656
});
1657+
const shouldDeleteAttachments = entry.cleanup === "delete" || !entry.retainAttachmentsOnKeep;
1658+
if (shouldDeleteAttachments) {
1659+
void safeRemoveAttachmentsDir(entry);
1660+
}
16571661
completeCleanupBookkeeping({
16581662
runId: entry.runId,
16591663
entry,

0 commit comments

Comments
 (0)