Skip to content

Commit a2e27de

Browse files
authored
fix(agents): sweep leaked files at prepared-absent paths during agent deletion (#124461)
Gateway agents.delete closed the agent database (unlinking -wal/-shm) during preparation, then awaited config/cron/session-purge work that reopened the database and recreated the sidecars. The sweep classified those recreated files as foreign replacements ("cleanup path appeared after deletion preparation"), preserved them, cascaded ancestor protection over the whole agent directory, and finished the deletion journal with exit 0 while ~157 files survived under agents/<id>/. The deletion journal fence already blocks legitimate claims beneath prepared paths, so a file that appears at a prepared-absent path can only be leaked deleted-agent state: adopt its identity and sweep it instead of preserving it. Prepared-present paths keep the existing identity-mismatch protection for genuine operator replacements. Regression tests: recreated WAL sidecars between preparation and cleanup are trashed with the agent directory, and recovery sweeps a file that appeared at a journaled prepared-absent path.
1 parent 063ce57 commit a2e27de

2 files changed

Lines changed: 56 additions & 12 deletions

File tree

‎src/gateway/server-methods/agents-mutate.test.ts‎

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ describe("agents.delete", () => {
16461646
expectRespondOk(recreated.respond, { ok: true, agentId: "test-agent" });
16471647
});
16481648

1649-
it("preserves a replacement whose identity differs from the journal", async () => {
1649+
it("sweeps leaked files that appeared at prepared-absent journal paths", async () => {
16501650
const workspaceDir = "/journal/workspace";
16511651
const workspaceAlias = "/journal/workspace-alias";
16521652
const journal = {
@@ -1715,12 +1715,13 @@ describe("agents.delete", () => {
17151715
await promise;
17161716

17171717
expectRespondOk(respond, { failed: [] });
1718-
expect(mocks.movePathToTrash).not.toHaveBeenCalledWith(workspaceDir);
1719-
const replacementRecord = journal.cleanupPaths.find((entry) => entry.path === workspaceDir);
1720-
expect(replacementRecord).toMatchObject({
1721-
done: true,
1722-
note: "cleanup path appeared after deletion preparation",
1723-
});
1718+
// The journal fence blocked legitimate claims while this path was prepared
1719+
// absent, so the appeared occupant is leaked deleted-agent state: sweep it
1720+
// instead of preserving it and finishing over a surviving tree.
1721+
expect(mocks.movePathToTrash).toHaveBeenCalledWith(workspaceDir);
1722+
const appearedRecord = journal.cleanupPaths.find((entry) => entry.path === workspaceDir);
1723+
expect(appearedRecord).toMatchObject({ done: true });
1724+
expect(appearedRecord).not.toHaveProperty("note");
17241725
expect(mocks.beginAgentDeletionFinish).toHaveBeenCalledOnce();
17251726
});
17261727

@@ -2829,6 +2830,49 @@ describe("agents.delete", () => {
28292830
expect(mocks.movePathToTrash).toHaveBeenCalled();
28302831
});
28312832

2833+
it("sweeps WAL sidecars recreated between deletion preparation and cleanup", async () => {
2834+
const agentDir = "/agents/test-agent";
2835+
const walPath = `${agentDir}/openclaw-agent.sqlite-wal`;
2836+
const shmPath = `${agentDir}/openclaw-agent.sqlite-shm`;
2837+
const presentStats = new Map<string, { dev: number; ino: number; file: boolean }>([
2838+
[agentDir, { dev: 1, ino: 10, file: false }],
2839+
["/workspace/test-agent", { dev: 1, ino: 20, file: false }],
2840+
["/transcripts/test-agent", { dev: 1, ino: 30, file: false }],
2841+
]);
2842+
mocks.fsLstat.mockImplementation(async (pathname: unknown) => {
2843+
const stat = presentStats.get(String(pathname));
2844+
if (!stat) {
2845+
throw createEnoentError();
2846+
}
2847+
return {
2848+
dev: stat.dev,
2849+
ino: stat.ino,
2850+
isFile: () => stat.file,
2851+
isSymbolicLink: () => false,
2852+
nlink: 1,
2853+
} as unknown as import("node:fs").Stats;
2854+
});
2855+
// The live runtime reopens the agent database while deletion awaits config,
2856+
// cron, and session-purge work, recreating -wal/-shm after preparation.
2857+
mocks.cronRemoveAgentJobsTransactional.mockImplementation(
2858+
async (_agentId: string, commit: () => Promise<unknown>) => {
2859+
presentStats.set(walPath, { dev: 1, ino: 40, file: true });
2860+
presentStats.set(shmPath, { dev: 1, ino: 41, file: true });
2861+
return await commit();
2862+
},
2863+
);
2864+
2865+
const { respond, promise } = makeCall("agents.delete", { agentId: "test-agent" });
2866+
await promise;
2867+
2868+
expectRespondOk(respond, { failed: [] });
2869+
const trashedPaths = mocks.movePathToTrash.mock.calls.map(([pathname]) => pathname);
2870+
expect(trashedPaths).toContain(walPath);
2871+
expect(trashedPaths).toContain(shmPath);
2872+
expect(trashedPaths).toContain(agentDir);
2873+
expect(mocks.beginAgentDeletionFinish).toHaveBeenCalledOnce();
2874+
});
2875+
28322876
it("deletes workspace state after removing the last owner's workspace", async () => {
28332877
const { respond, promise } = makeCall("agents.delete", {
28342878
agentId: "test-agent",

‎src/gateway/server-methods/agents.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,11 @@ async function statAgentCleanupPath(cleanupPath: AgentDeleteCleanupPath) {
394394
}
395395
const identity = cleanupPathIdentity(stat);
396396
if (cleanupPath.preparedIdentity === null) {
397-
if (identity !== null) {
398-
throw new AgentCleanupIdentityMismatchError(
399-
"cleanup path appeared after deletion preparation",
400-
);
401-
}
397+
// The journal fence blocks legitimate claims on prepared-absent paths, so a
398+
// file that appeared here is leaked deleted-agent state (recreated WAL
399+
// sidecars, runtime home rewrites). Adopt it and sweep it; preserving it
400+
// cascades ancestor protection and finishes over a surviving tree.
401+
cleanupPath.preparedIdentity = identity;
402402
} else if (
403403
identity === null ||
404404
identity.dev !== cleanupPath.preparedIdentity.dev ||

0 commit comments

Comments
 (0)