Skip to content

Commit 203024e

Browse files
authored
fix(agents): preserve survivor-owned databases offline (#129085)
Prepare surviving agent-directory ownership before filtering journaled SQLite paths, addressing the post-merge review of #129017. Refs #129014
1 parent 876f2de commit 203024e

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import {
55
formatSharedAuthStoreOwnerDeleteError,
66
isSharedAuthStoreOwner,
77
} from "../agents/agent-delete-safety.js";
8-
import { normalizeAgentDirRegistryPath } from "../agents/agent-dir-registry.js";
8+
import {
9+
isPathOwnedByAnotherRegisteredAgent,
10+
normalizeAgentDirRegistryPath,
11+
} from "../agents/agent-dir-registry.js";
912
import {
1013
beginAgentDeletion,
1114
claimCompletedAgentDeletion,
1215
} from "../agents/agent-lifecycle-registry.js";
1316
import {
17+
listAgentIds,
1418
resolveAgentDir,
1519
resolveAgentWorkspaceDir,
1620
tryResolveSoleAgentId,
@@ -358,6 +362,11 @@ export async function agentsDeleteCommand(
358362
}
359363
}
360364
if (deleteFiles) {
365+
// Directory ownership is process-local; prepare every survivor before filtering journal paths,
366+
// or a deleted agent's database nested beneath a survivor's agentDir could be trashed.
367+
for (const survivingAgentId of listAgentIds(result.config)) {
368+
resolveAgentDir(result.config, survivingAgentId);
369+
}
361370
const canonicalAgentDir = normalizeAgentDirRegistryPath(agentDir);
362371
const survivingDatabasePaths = new Set(
363372
listOpenClawRegisteredAgentDatabases()
@@ -370,6 +379,7 @@ export async function agentsDeleteCommand(
370379
return (
371380
!isPathInside(canonicalAgentDir, canonicalPath) &&
372381
!survivingDatabasePaths.has(canonicalPath) &&
382+
!isPathOwnedByAnotherRegisteredAgent({ agentId, pathname }) &&
373383
findOverlappingWorkspaceAgentIds(result.config, agentId, canonicalPath).length === 0
374384
);
375385
});

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,15 @@ describe("agents delete command", () => {
675675

676676
it("deregisters the agent database after offline deletion", async () => {
677677
await withStateDirEnv("openclaw-agents-delete-registry-", async ({ tempRoot, stateDir }) => {
678+
const mainAgentDir = path.join(tempRoot, "main-agent");
678679
const cfg: OpenClawConfig = {
679680
agents: {
680681
list: [
681-
{ id: "main", workspace: path.join(stateDir, "workspace-main") },
682+
{
683+
id: "main",
684+
agentDir: mainAgentDir,
685+
workspace: path.join(stateDir, "workspace-main"),
686+
},
682687
{ id: "ops", workspace: path.join(stateDir, "workspace-ops") },
683688
],
684689
},
@@ -687,23 +692,27 @@ describe("agents delete command", () => {
687692
const databasePath = path.join(stateDir, "agents", "ops", "agent", "openclaw-agent.sqlite");
688693
const externalDatabaseDir = path.join(tempRoot, "external-databases");
689694
await fs.mkdir(externalDatabaseDir);
695+
await fs.mkdir(mainAgentDir);
690696
const externalDatabasePath = path.join(externalDatabaseDir, "ops.sqlite");
691697
const sharedDatabasePath = path.join(externalDatabaseDir, "shared.sqlite");
698+
const survivorOwnedDatabasePath = path.join(mainAgentDir, "ops.sqlite");
692699
const externalDatabasePaths = [
693700
externalDatabasePath,
694701
`${externalDatabasePath}-wal`,
695702
`${externalDatabasePath}-shm`,
696703
`${externalDatabasePath}-journal`,
697704
];
698705
await Promise.all(
699-
[...externalDatabasePaths, sharedDatabasePath].map((sqlitePath) =>
700-
fs.writeFile(sqlitePath, ""),
706+
[...externalDatabasePaths, sharedDatabasePath, survivorOwnedDatabasePath].map(
707+
(sqlitePath) => fs.writeFile(sqlitePath, ""),
701708
),
702709
);
703710
const canonicalExternalDatabaseDir = await fs.realpath(externalDatabaseDir);
711+
const canonicalMainAgentDir = await fs.realpath(mainAgentDir);
704712
registerOpenClawAgentDatabase({ agentId: "ops", path: databasePath });
705713
registerOpenClawAgentDatabase({ agentId: "ops", path: externalDatabasePath });
706714
registerOpenClawAgentDatabase({ agentId: "ops", path: sharedDatabasePath });
715+
registerOpenClawAgentDatabase({ agentId: "ops", path: survivorOwnedDatabasePath });
707716
registerOpenClawAgentDatabase({ agentId: "main", path: sharedDatabasePath });
708717
recordAgentProvenance("ops", { createdVia: "operator" });
709718
recordAgentProvenance("child", { createdVia: "agent", creatorAgentId: "ops" });
@@ -726,6 +735,11 @@ describe("agents delete command", () => {
726735
path.join(canonicalExternalDatabaseDir, path.basename(sharedDatabasePath)),
727736
expect.anything(),
728737
);
738+
expect(fsSafeMocks.movePathToTrash).not.toHaveBeenCalledWith(
739+
path.join(canonicalMainAgentDir, path.basename(survivorOwnedDatabasePath)),
740+
expect.anything(),
741+
);
742+
expect((await fs.stat(survivorOwnedDatabasePath)).isFile()).toBe(true);
729743
const registeredDatabases = listOpenClawRegisteredAgentDatabases();
730744
expect(registeredDatabases.map((entry) => entry.agentId)).not.toContain("ops");
731745
expect(registeredDatabases).toEqual(

0 commit comments

Comments
 (0)