fix: clean up attachments for killed subagent runs

This commit is contained in:
Tak Hoffman 2026-03-24 09:14:06 -05:00
parent 807daf54fe
commit dd11bdd003
No known key found for this signature in database
2 changed files with 37 additions and 0 deletions

View File

@ -1,3 +1,6 @@
import { promises as fs } from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const noop = () => {};
@ -431,4 +434,34 @@ describe("subagent registry seam flow", () => {
});
});
});
it("removes attachments for killed delete-mode runs", async () => {
const attachmentsRootDir = await fs.mkdtemp(
path.join(os.tmpdir(), "openclaw-kill-attachments-"),
);
const attachmentsDir = path.join(attachmentsRootDir, "child");
await fs.mkdir(attachmentsDir, { recursive: true });
await fs.writeFile(path.join(attachmentsDir, "artifact.txt"), "artifact");
mod.registerSubagentRun({
runId: "run-killed-delete-attachments",
childSessionKey: "agent:main:subagent:killed-delete-attachments",
requesterSessionKey: "agent:main:main",
requesterDisplayKey: "main",
task: "kill and delete attachments",
cleanup: "delete",
attachmentsDir,
attachmentsRootDir,
});
const updated = mod.markSubagentRunTerminated({
runId: "run-killed-delete-attachments",
reason: "manual kill",
});
expect(updated).toBe(1);
await vi.waitFor(async () => {
await expect(fs.access(attachmentsDir)).rejects.toMatchObject({ code: "ENOENT" });
});
});
});

View File

@ -1654,6 +1654,10 @@ export function markSubagentRunTerminated(params: {
childSessionKey: entry.childSessionKey,
});
});
const shouldDeleteAttachments = entry.cleanup === "delete" || !entry.retainAttachmentsOnKeep;
if (shouldDeleteAttachments) {
void safeRemoveAttachmentsDir(entry);
}
completeCleanupBookkeeping({
runId: entry.runId,
entry,