Skip to content

Commit 0e0b896

Browse files
authored
fix(memory): stop legacy warnings for canonical database links
Closes #133981 ## What Problem This Solves Fixes an issue where operators with `memory/<agent>.sqlite` compatibility links to the modern per-agent database received one false legacy-memory migration warning per agent during Doctor and startup checks. ## Why This Change Was Made The Memory Core migration collector followed each symbolic link and classified the populated canonical database as a separate legacy sidecar. It now excludes only a proven link to the same agent's canonical database when no legacy companion files exist. Unknown, corrupt, different-target, and data-bearing legacy files keep the existing warning and preservation behavior. The issue opener [corrected the reported file shape](#133981 (comment)): the apparent 54–60-byte size was the symbolic-link path length, and 9 of 10 per-agent paths linked to populated canonical databases. ## User Impact Healthy modern multi-agent installations no longer show repeated false legacy-memory warnings. Existing compatibility links and canonical memory data remain unchanged. ## Evidence - Current-main red at `cd0f6f487e1819b006b6c11968233ed76523754e`: isolated `openclaw doctor --non-interactive --yes` emitted one false schema warning for each of two agents whose legacy path linked to its populated canonical database. - Exact candidate source green at `5f79c9ae83e3c92d036838243e8816aeb841683c`: [Blacksmith Testbox run](https://github.com/openclaw/openclaw/actions/runs/33391687950) reported 2 agents, 0 false previews, 0 false warnings, 2 links preserved, and 2 populated canonical rows preserved. - `node scripts/run-vitest.mjs extensions/memory-core/doctor-contract-api.test.ts`: 63 passed. - Focused alias, corrupt-file, data-bearing-file, empty-file, and unknown-companion cases: 7 passed. - Targeted formatter, lint, and `git diff --check`: passed. - Autoreview: clean; no accepted or actionable findings. - Build, type checks, and full continuous integration are delegated to GitHub. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 9ac61cd commit 0e0b896

2 files changed

Lines changed: 116 additions & 1 deletion

File tree

‎extensions/memory-core/doctor-contract-api.test.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,79 @@ describe("memory-core doctor dreaming migration", () => {
14661466
await expect(fs.access(legacyPath)).rejects.toMatchObject({ code: "ENOENT" });
14671467
});
14681468

1469+
it("ignores a legacy sidecar symlink to the populated canonical agent database", async () => {
1470+
const stateDir = path.join(rootDir, "state");
1471+
const legacyPath = path.join(stateDir, "memory", "main.sqlite");
1472+
const agentPath = path.join(stateDir, "agents", "main", "agent", "openclaw-agent.sqlite");
1473+
await createCanonicalMemoryIndex(agentPath, "canonical memory remains authoritative");
1474+
await fs.mkdir(path.dirname(legacyPath), { recursive: true });
1475+
await fs.symlink(path.relative(path.dirname(legacyPath), agentPath), legacyPath);
1476+
1477+
const migration = legacyMemoryIndexMigration();
1478+
await expect(migration.detectLegacyState(migrationParams())).resolves.toBeNull();
1479+
await expect(migration.migrateLegacyState(migrationParams())).resolves.toEqual({
1480+
changes: [],
1481+
warnings: [],
1482+
});
1483+
1484+
expect((await fs.lstat(legacyPath)).isSymbolicLink()).toBe(true);
1485+
await expect(fs.realpath(legacyPath)).resolves.toBe(await fs.realpath(agentPath));
1486+
expect(readMemoryRows(agentPath).chunks).toEqual([
1487+
{ id: "canonical-chunk", text: "canonical memory remains authoritative" },
1488+
]);
1489+
});
1490+
1491+
it("keeps the warning for a legacy sidecar symlink to a different data-bearing database", async () => {
1492+
const stateDir = path.join(rootDir, "state");
1493+
const legacyPath = path.join(stateDir, "memory", "main.sqlite");
1494+
const unrelatedPath = path.join(rootDir, "unrelated.sqlite");
1495+
const db = new DatabaseSync(unrelatedPath);
1496+
try {
1497+
db.exec("CREATE TABLE unrelated (value TEXT)");
1498+
db.prepare("INSERT INTO unrelated (value) VALUES (?)").run("preserve me");
1499+
} finally {
1500+
db.close();
1501+
}
1502+
await fs.mkdir(path.dirname(legacyPath), { recursive: true });
1503+
await fs.symlink(path.relative(path.dirname(legacyPath), unrelatedPath), legacyPath);
1504+
1505+
const result = await legacyMemoryIndexMigration().migrateLegacyState(migrationParams());
1506+
1507+
expect(result.changes).toEqual([]);
1508+
expect(result.warnings).toEqual([
1509+
"Skipped Memory Core legacy memory index import for agent main because the sidecar schema is not a legacy memory index",
1510+
]);
1511+
expect((await fs.lstat(legacyPath)).isSymbolicLink()).toBe(true);
1512+
await expect(fs.realpath(legacyPath)).resolves.toBe(await fs.realpath(unrelatedPath));
1513+
const preserved = new DatabaseSync(unrelatedPath, { readOnly: true });
1514+
try {
1515+
expect(preserved.prepare("SELECT value FROM unrelated").get()).toEqual({
1516+
value: "preserve me",
1517+
});
1518+
} finally {
1519+
preserved.close();
1520+
}
1521+
});
1522+
1523+
it("keeps a corrupt legacy sidecar with an import warning", async () => {
1524+
const stateDir = path.join(rootDir, "state");
1525+
const legacyPath = path.join(stateDir, "memory", "main.sqlite");
1526+
const corruptBytes = Buffer.from("not a sqlite database");
1527+
await fs.mkdir(path.dirname(legacyPath), { recursive: true });
1528+
await fs.writeFile(legacyPath, corruptBytes);
1529+
1530+
const result = await legacyMemoryIndexMigration().migrateLegacyState(migrationParams());
1531+
1532+
expect(result.changes).toEqual([]);
1533+
expect(result.warnings).toEqual([
1534+
expect.stringContaining(
1535+
"Skipped Memory Core legacy memory index import for agent main because legacy rows could not be imported:",
1536+
),
1537+
]);
1538+
await expect(fs.readFile(legacyPath)).resolves.toEqual(corruptBytes);
1539+
await expect(fs.access(`${legacyPath}.migrated`)).rejects.toMatchObject({ code: "ENOENT" });
1540+
});
1541+
14691542
it("preserves the main sidecar when a companion stat fails with ELOOP", async () => {
14701543
const stateDir = path.join(rootDir, "state");
14711544
const legacyPath = path.join(stateDir, "memory", "main.sqlite");

‎extensions/memory-core/src/migration/doctor-memory-sidecar.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,36 @@ function readMemorySearchFtsTokenizer(
134134
return raw === "unicode61" || raw === "trigram" ? raw : undefined;
135135
}
136136

137+
async function isCanonicalAgentDatabaseSymlink(params: {
138+
legacyPath: string;
139+
agentDatabasePath: string;
140+
}): Promise<boolean> {
141+
try {
142+
if (!(await fs.lstat(params.legacyPath)).isSymbolicLink()) {
143+
return false;
144+
}
145+
for (const suffix of LEGACY_MEMORY_SIDECAR_SUFFIXES.slice(1)) {
146+
try {
147+
await fs.lstat(`${params.legacyPath}${suffix}`);
148+
return false;
149+
} catch (err: unknown) {
150+
if (!err || typeof err !== "object" || !("code" in err) || err.code !== "ENOENT") {
151+
return false;
152+
}
153+
}
154+
}
155+
const [legacyTarget, canonicalTarget] = await Promise.all([
156+
fs.realpath(params.legacyPath),
157+
fs.realpath(params.agentDatabasePath),
158+
]);
159+
return legacyTarget === canonicalTarget;
160+
} catch {
161+
// Only the exact compatibility alias is known non-legacy state. Any unresolved
162+
// target remains visible so Doctor cannot hide data it failed to classify.
163+
return false;
164+
}
165+
}
166+
137167
async function collectLegacyMemorySidecarSources(params: {
138168
config: unknown;
139169
env: NodeJS.ProcessEnv;
@@ -169,11 +199,23 @@ async function collectLegacyMemorySidecarSources(params: {
169199
return;
170200
}
171201
seen.add(key);
202+
const agentDatabasePath = resolveOpenClawAgentSqlitePath({
203+
agentId,
204+
env: migrationEnv,
205+
});
206+
if (
207+
await isCanonicalAgentDatabaseSymlink({
208+
legacyPath: normalizedPath,
209+
agentDatabasePath,
210+
})
211+
) {
212+
return;
213+
}
172214
sources.push({
173215
agentId,
174216
legacyPath: normalizedPath,
175217
stateDir: params.stateDir,
176-
agentDatabasePath: resolveOpenClawAgentSqlitePath({ agentId, env: migrationEnv }),
218+
agentDatabasePath,
177219
});
178220
}
179221
for (const agentId of agentIds) {

0 commit comments

Comments
 (0)