Skip to content

Commit 298b3ac

Browse files
committed
fix(agents): clean external databases on offline delete
Move journaled external SQLite files and sidecars to Trash while preserving databases still owned by surviving agents. Refs #129014
1 parent ebc70fa commit 298b3ac

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

‎src/commands/agents.commands.delete.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
formatSharedAuthStoreOwnerDeleteError,
66
isSharedAuthStoreOwner,
77
} from "../agents/agent-delete-safety.js";
8+
import { normalizeAgentDirRegistryPath } from "../agents/agent-dir-registry.js";
89
import {
910
beginAgentDeletion,
1011
claimCompletedAgentDeletion,
@@ -44,10 +45,15 @@ import {
4445
isGatewayTransportError,
4546
} from "../gateway/call.js";
4647
import { withAgentExecApprovalsRemoved } from "../infra/exec-approvals.js";
48+
import { isPathInside } from "../infra/path-guards.js";
49+
import { resolveSqliteDatabaseFilePaths } from "../infra/sqlite-files.js";
4750
import { normalizeAgentId, normalizeAgentIdStrict } from "../routing/session-key.js";
4851
import { defaultRuntime, type RuntimeEnv, writeRuntimeJson } from "../runtime.js";
4952
import { readAgentDeletionJournal } from "../state/agent-deletion-journal.js";
50-
import { unregisterOpenClawAgentDatabases } from "../state/openclaw-agent-db-registry.js";
53+
import {
54+
listOpenClawRegisteredAgentDatabases,
55+
unregisterOpenClawAgentDatabases,
56+
} from "../state/openclaw-agent-db-registry.js";
5157
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js";
5258
import { createClackPrompter } from "../wizard/clack-prompter.js";
5359
import { createQuietRuntime } from "./agents.command-shared.js";
@@ -352,8 +358,26 @@ export async function agentsDeleteCommand(
352358
}
353359
}
354360
if (deleteFiles) {
361+
const canonicalAgentDir = normalizeAgentDirRegistryPath(agentDir);
362+
const survivingDatabasePaths = new Set(
363+
listOpenClawRegisteredAgentDatabases()
364+
.filter((entry) => normalizeAgentId(entry.agentId) !== agentId)
365+
.flatMap((entry) => resolveSqliteDatabaseFilePaths(entry.path))
366+
.map((pathname) => normalizeAgentDirRegistryPath(pathname)),
367+
);
368+
const databasePaths = deletion.entry.databasePaths.filter((pathname) => {
369+
const canonicalPath = normalizeAgentDirRegistryPath(pathname);
370+
return (
371+
!isPathInside(canonicalAgentDir, canonicalPath) &&
372+
!survivingDatabasePaths.has(canonicalPath) &&
373+
findOverlappingWorkspaceAgentIds(result.config, agentId, canonicalPath).length === 0
374+
);
375+
});
355376
await removePath(agentDir);
356377
await removePath(sessionsDir);
378+
for (const databasePath of databasePaths) {
379+
await removePath(databasePath);
380+
}
357381
}
358382
if (workspaceCleanupError) {
359383
throw workspaceCleanupError;

‎src/commands/agents.delete.test.ts‎

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ describe("agents delete command", () => {
674674
});
675675

676676
it("deregisters the agent database after offline deletion", async () => {
677-
await withStateDirEnv("openclaw-agents-delete-registry-", async ({ stateDir }) => {
677+
await withStateDirEnv("openclaw-agents-delete-registry-", async ({ tempRoot, stateDir }) => {
678678
const cfg: OpenClawConfig = {
679679
agents: {
680680
list: [
@@ -685,15 +685,53 @@ describe("agents delete command", () => {
685685
};
686686
await arrangeAgentsDeleteTest({ stateDir, cfg, sessions: {} });
687687
const databasePath = path.join(stateDir, "agents", "ops", "agent", "openclaw-agent.sqlite");
688+
const externalDatabaseDir = path.join(tempRoot, "external-databases");
689+
await fs.mkdir(externalDatabaseDir);
690+
const externalDatabasePath = path.join(externalDatabaseDir, "ops.sqlite");
691+
const sharedDatabasePath = path.join(externalDatabaseDir, "shared.sqlite");
692+
const externalDatabasePaths = [
693+
externalDatabasePath,
694+
`${externalDatabasePath}-wal`,
695+
`${externalDatabasePath}-shm`,
696+
`${externalDatabasePath}-journal`,
697+
];
698+
await Promise.all(
699+
[...externalDatabasePaths, sharedDatabasePath].map((sqlitePath) =>
700+
fs.writeFile(sqlitePath, ""),
701+
),
702+
);
703+
const canonicalExternalDatabaseDir = await fs.realpath(externalDatabaseDir);
688704
registerOpenClawAgentDatabase({ agentId: "ops", path: databasePath });
705+
registerOpenClawAgentDatabase({ agentId: "ops", path: externalDatabasePath });
706+
registerOpenClawAgentDatabase({ agentId: "ops", path: sharedDatabasePath });
707+
registerOpenClawAgentDatabase({ agentId: "main", path: sharedDatabasePath });
689708
recordAgentProvenance("ops", { createdVia: "operator" });
690709
recordAgentProvenance("child", { createdVia: "agent", creatorAgentId: "ops" });
691710
expect(listOpenClawRegisteredAgentDatabases().map((entry) => entry.agentId)).toContain("ops");
692711

693712
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
694713

695-
expect(listOpenClawRegisteredAgentDatabases().map((entry) => entry.agentId)).not.toContain(
696-
"ops",
714+
for (const sqlitePath of externalDatabasePaths) {
715+
expect(fsSafeMocks.movePathToTrash).toHaveBeenCalledWith(
716+
path.join(canonicalExternalDatabaseDir, path.basename(sqlitePath)),
717+
{ allowedRoots: [canonicalExternalDatabaseDir] },
718+
);
719+
}
720+
expect(readJsonLogs()[0]?.removed).toEqual(
721+
expect.arrayContaining(
722+
externalDatabasePaths.map((sqlitePath) => ({ path: sqlitePath, method: "trash" })),
723+
),
724+
);
725+
expect(fsSafeMocks.movePathToTrash).not.toHaveBeenCalledWith(
726+
path.join(canonicalExternalDatabaseDir, path.basename(sharedDatabasePath)),
727+
expect.anything(),
728+
);
729+
const registeredDatabases = listOpenClawRegisteredAgentDatabases();
730+
expect(registeredDatabases.map((entry) => entry.agentId)).not.toContain("ops");
731+
expect(registeredDatabases).toEqual(
732+
expect.arrayContaining([
733+
expect.objectContaining({ agentId: "main", path: sharedDatabasePath }),
734+
]),
697735
);
698736
expect(readAgentDeletionJournal("ops")?.cleanupCompleted).toBe(true);
699737
expect(readAgentProvenance("ops")).toBeUndefined();

0 commit comments

Comments
 (0)