Skip to content

Commit 27089e5

Browse files
committed
fix(state): re-anchor copied default-layout registry rows instead of deleting them
Resolve the ClawSweeper P1 by preserving genuine external default-layout registrations, re-anchoring copied rows when an in-root counterpart exists, and deleting only dual default-layout conflicts.
1 parent 2b79cf9 commit 27089e5

3 files changed

Lines changed: 87 additions & 10 deletions

File tree

‎docs/reference/database-schemas.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Version 3 was an unshipped development step folded into version 4.
9191

9292
### State schema 9
9393

94-
Schema 9 stores an `agent_databases.path` value relative to the state directory when the registered agent database is inside that directory. Explicit external database paths remain absolute. This keeps a copied state directory self-contained: its registry resolves agent databases beneath the copied root instead of continuing to discover files beneath the source root.
94+
Schema 9 stores an `agent_databases.path` value relative to the state directory when the registered agent database is inside that directory. During migration, a foreign default-layout row is re-anchored to the in-root counterpart when that file exists. It is deleted only when the same agent already holds its in-root registration, because dual default-layout registrations cannot produce a valid combined session list. Otherwise, the absolute row is preserved, so genuine external registrations are never deleted. This keeps a copied state directory self-contained without dropping supported external database paths.
9595

9696
## Integrity checks
9797

‎src/state/openclaw-state-db-schema-repair.ts‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import {
3030
} from "./openclaw-state-db-schema-helpers.js";
3131
import { OpenClawStateDatabaseSchemaMigrationRequiredError } from "./openclaw-state-db-schema-migration-required.js";
3232
import * as sessionWatchMigration from "./openclaw-state-db-session-watch-migration.js";
33-
import { resolveOpenClawAgentDatabaseStoredPath } from "./openclaw-state-db.paths.js";
33+
import {
34+
resolveOpenClawAgentDatabaseStoredPath,
35+
resolveOpenClawStateDirForDatabasePath,
36+
} from "./openclaw-state-db.paths.js";
3437
import { OPENCLAW_STATE_SCHEMA_SQL } from "./openclaw-state-schema.js";
3538

3639
export function dropLegacyStateTables(db: DatabaseSync): void {
@@ -475,6 +478,9 @@ export function migrateAgentDatabaseRelativePaths(
475478
"UPDATE agent_databases SET path = ? WHERE agent_id = ? AND path = ?",
476479
);
477480
const deletePath = db.prepare("DELETE FROM agent_databases WHERE agent_id = ? AND path = ?");
481+
const hasPath = db.prepare(
482+
"SELECT 1 FROM agent_databases WHERE agent_id = ? AND path = ? LIMIT 1",
483+
);
478484
let changed = false;
479485
for (const row of rows) {
480486
const agentId = row.agent_id;
@@ -485,18 +491,48 @@ export function migrateAgentDatabaseRelativePaths(
485491
if (!path.isAbsolute(registeredPath)) {
486492
continue;
487493
}
488-
const absolutePath = path.resolve(registeredPath);
489494
const storedPath = resolveOpenClawAgentDatabaseStoredPath(databasePath, registeredPath);
490495
if (!path.isAbsolute(storedPath)) {
491496
updatePath.run(storedPath, agentId, registeredPath);
492497
changed = true;
498+
}
499+
}
500+
const stateDir = resolveOpenClawStateDirForDatabasePath(databasePath);
501+
for (const row of rows) {
502+
const agentId = row.agent_id;
503+
const registeredPath = row.path;
504+
if (
505+
typeof agentId !== "string" ||
506+
typeof registeredPath !== "string" ||
507+
!path.isAbsolute(registeredPath) ||
508+
!path.isAbsolute(resolveOpenClawAgentDatabaseStoredPath(databasePath, registeredPath))
509+
) {
493510
continue;
494511
}
512+
const absolutePath = path.resolve(registeredPath);
495513
if (isDefaultAgentDatabasePath(absolutePath, agentId)) {
496-
// Copied registries can retain another state root's default-layout rows.
497-
// They are discovery metadata and self-heal when that agent database next opens.
498-
deletePath.run(agentId, registeredPath);
499-
changed = true;
514+
const counterpartAbsolute = path.join(
515+
stateDir,
516+
"agents",
517+
agentId,
518+
"agent",
519+
"openclaw-agent.sqlite",
520+
);
521+
const counterpartStored = resolveOpenClawAgentDatabaseStoredPath(
522+
databasePath,
523+
counterpartAbsolute,
524+
);
525+
if (hasPath.get(agentId, counterpartStored)) {
526+
// The same agent already owns its in-root canonical registration. Keeping a second
527+
// default-layout registration guarantees duplicate canonical session keys on every list.
528+
deletePath.run(agentId, registeredPath);
529+
changed = true;
530+
} else if (existsSync(counterpartAbsolute)) {
531+
// Re-anchor a copied or moved state directory onto its copied database instead of
532+
// deleting the registration or leaving it dangling at the source root.
533+
updatePath.run(counterpartStored, agentId, registeredPath);
534+
changed = true;
535+
}
500536
}
501537
}
502538
return changed;

‎src/state/openclaw-state-db.test.ts‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,14 +1583,40 @@ describe("openclaw state database", () => {
15831583
const env = { OPENCLAW_STATE_DIR: stateDir };
15841584
const databasePath = materializeCurrentStateDatabase(stateDir);
15851585
const inRootPath = path.join(stateDir, "agents", "main", "agent", "openclaw-agent.sqlite");
1586-
const foreignDefaultPath = path.join(
1586+
const dualInRootPath = path.join(stateDir, "agents", "dual", "agent", "openclaw-agent.sqlite");
1587+
const dualForeignPath = path.join(
15871588
foreignStateDir,
15881589
"agents",
1589-
"foreign",
1590+
"dual",
1591+
"agent",
1592+
"openclaw-agent.sqlite",
1593+
);
1594+
const copiedForeignPath = path.join(
1595+
foreignStateDir,
1596+
"agents",
1597+
"copied",
1598+
"agent",
1599+
"openclaw-agent.sqlite",
1600+
);
1601+
const copiedInRootPath = path.join(
1602+
stateDir,
1603+
"agents",
1604+
"copied",
1605+
"agent",
1606+
"openclaw-agent.sqlite",
1607+
);
1608+
const preservedDefaultPath = path.join(
1609+
foreignStateDir,
1610+
"agents",
1611+
"preserved",
15901612
"agent",
15911613
"openclaw-agent.sqlite",
15921614
);
15931615
const externalPath = path.join(foreignStateDir, "explicit", "external.sqlite");
1616+
fs.mkdirSync(path.dirname(dualInRootPath), { recursive: true });
1617+
fs.writeFileSync(dualInRootPath, "");
1618+
fs.mkdirSync(path.dirname(copiedInRootPath), { recursive: true });
1619+
fs.writeFileSync(copiedInRootPath, "");
15941620
const { DatabaseSync } = requireNodeSqlite();
15951621
const legacy = new DatabaseSync(databasePath);
15961622
const insert = legacy.prepare(
@@ -1599,7 +1625,10 @@ describe("openclaw state database", () => {
15991625
) VALUES (?, ?, 17, 1, NULL)`,
16001626
);
16011627
insert.run("main", inRootPath);
1602-
insert.run("foreign", foreignDefaultPath);
1628+
insert.run("dual", dualInRootPath);
1629+
insert.run("dual", dualForeignPath);
1630+
insert.run("copied", copiedForeignPath);
1631+
insert.run("preserved", preservedDefaultPath);
16031632
insert.run("external", externalPath);
16041633
legacy.exec(`
16051634
PRAGMA user_version = 8;
@@ -1616,15 +1645,27 @@ describe("openclaw state database", () => {
16161645
expect(
16171646
migrated.db.prepare("SELECT agent_id, path FROM agent_databases ORDER BY agent_id").all(),
16181647
).toEqual([
1648+
{
1649+
agent_id: "copied",
1650+
path: path.join("agents", "copied", "agent", "openclaw-agent.sqlite"),
1651+
},
1652+
{
1653+
agent_id: "dual",
1654+
path: path.join("agents", "dual", "agent", "openclaw-agent.sqlite"),
1655+
},
16191656
{ agent_id: "external", path: externalPath },
16201657
{
16211658
agent_id: "main",
16221659
path: path.join("agents", "main", "agent", "openclaw-agent.sqlite"),
16231660
},
1661+
{ agent_id: "preserved", path: preservedDefaultPath },
16241662
]);
16251663
expect(listOpenClawRegisteredAgentDatabases({ env })).toEqual([
1664+
expect.objectContaining({ agentId: "copied", path: copiedInRootPath }),
1665+
expect.objectContaining({ agentId: "dual", path: dualInRootPath }),
16261666
expect.objectContaining({ agentId: "external", path: externalPath }),
16271667
expect.objectContaining({ agentId: "main", path: inRootPath }),
1668+
expect.objectContaining({ agentId: "preserved", path: preservedDefaultPath }),
16281669
]);
16291670
});
16301671

0 commit comments

Comments
 (0)