Skip to content

Commit 9f63e58

Browse files
LilacLilac
authored andcommitted
fix(infra): tolerate equivalent plugin install migrations
1 parent 47967b0 commit 9f63e58

2 files changed

Lines changed: 215 additions & 1 deletion

File tree

‎src/commands/doctor-state-migrations.test.ts‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,136 @@ describe("doctor legacy state migrations", () => {
13531353
});
13541354
});
13551355

1356+
it("archives equivalent legacy npm install records after spec pinning", async () => {
1357+
const root = await makeTempRoot();
1358+
await writePersistedInstalledPluginIndex(
1359+
{
1360+
version: 1,
1361+
hostContractVersion: "test",
1362+
compatRegistryVersion: "test",
1363+
migrationVersion: 1,
1364+
policyHash: "test",
1365+
generatedAtMs: 1,
1366+
installRecords: {
1367+
codex: {
1368+
source: "npm",
1369+
spec: "@openclaw/[email protected]",
1370+
installPath: "/tmp/openclaw-plugins/codex",
1371+
version: "2026.5.31",
1372+
resolvedName: "@openclaw/codex",
1373+
resolvedVersion: "2026.5.31",
1374+
resolvedSpec: "@openclaw/[email protected]",
1375+
integrity: "sha512-codex",
1376+
},
1377+
},
1378+
plugins: [],
1379+
diagnostics: [],
1380+
},
1381+
{ stateDir: root },
1382+
);
1383+
const sourcePath = path.join(root, "plugins", "installs.json");
1384+
fs.mkdirSync(path.dirname(sourcePath), { recursive: true });
1385+
fs.writeFileSync(
1386+
sourcePath,
1387+
JSON.stringify({
1388+
records: {
1389+
codex: {
1390+
source: "npm",
1391+
spec: "@openclaw/codex",
1392+
installPath: "/tmp/openclaw-plugins/codex",
1393+
version: "2026.5.31",
1394+
resolvedName: "@openclaw/codex",
1395+
resolvedVersion: "2026.5.31",
1396+
resolvedSpec: "@openclaw/[email protected]",
1397+
integrity: "sha512-codex",
1398+
},
1399+
},
1400+
}),
1401+
"utf8",
1402+
);
1403+
1404+
const detected = await detectLegacyStateMigrations({
1405+
cfg: {},
1406+
env: { OPENCLAW_STATE_DIR: root } as NodeJS.ProcessEnv,
1407+
});
1408+
const result = await runLegacyStateMigrations({ detected });
1409+
1410+
expect(result.warnings).toStrictEqual([]);
1411+
expect(result.changes).toContain(
1412+
`Archived plugin install index legacy source → ${sourcePath}.migrated`,
1413+
);
1414+
expect(fs.existsSync(sourcePath)).toBe(false);
1415+
expect(fs.existsSync(`${sourcePath}.migrated`)).toBe(true);
1416+
await expect(readPersistedInstalledPluginIndex({ stateDir: root })).resolves.toMatchObject({
1417+
installRecords: {
1418+
codex: {
1419+
source: "npm",
1420+
spec: "@openclaw/[email protected]",
1421+
resolvedVersion: "2026.5.31",
1422+
},
1423+
},
1424+
});
1425+
});
1426+
1427+
it("keeps the legacy plugin install index when npm artifact versions differ", async () => {
1428+
const root = await makeTempRoot();
1429+
await writePersistedInstalledPluginIndex(
1430+
{
1431+
version: 1,
1432+
hostContractVersion: "test",
1433+
compatRegistryVersion: "test",
1434+
migrationVersion: 1,
1435+
policyHash: "test",
1436+
generatedAtMs: 1,
1437+
installRecords: {
1438+
codex: {
1439+
source: "npm",
1440+
spec: "@openclaw/[email protected]",
1441+
installPath: "/tmp/openclaw-plugins/codex",
1442+
version: "2026.5.31",
1443+
resolvedName: "@openclaw/codex",
1444+
resolvedVersion: "2026.5.31",
1445+
resolvedSpec: "@openclaw/[email protected]",
1446+
},
1447+
},
1448+
plugins: [],
1449+
diagnostics: [],
1450+
},
1451+
{ stateDir: root },
1452+
);
1453+
const sourcePath = path.join(root, "plugins", "installs.json");
1454+
fs.mkdirSync(path.dirname(sourcePath), { recursive: true });
1455+
fs.writeFileSync(
1456+
sourcePath,
1457+
JSON.stringify({
1458+
records: {
1459+
codex: {
1460+
source: "npm",
1461+
spec: "@openclaw/codex",
1462+
installPath: "/tmp/openclaw-plugins/codex",
1463+
version: "2026.5.30",
1464+
resolvedName: "@openclaw/codex",
1465+
resolvedVersion: "2026.5.30",
1466+
resolvedSpec: "@openclaw/[email protected]",
1467+
},
1468+
},
1469+
}),
1470+
"utf8",
1471+
);
1472+
1473+
const detected = await detectLegacyStateMigrations({
1474+
cfg: {},
1475+
env: { OPENCLAW_STATE_DIR: root } as NodeJS.ProcessEnv,
1476+
});
1477+
const result = await runLegacyStateMigrations({ detected });
1478+
1479+
expect(result.warnings).toStrictEqual([
1480+
"Left plugin install index in place because shared SQLite state has conflicting plugin install metadata for: codex",
1481+
]);
1482+
expect(fs.existsSync(sourcePath)).toBe(true);
1483+
expect(fs.existsSync(`${sourcePath}.migrated`)).toBe(false);
1484+
});
1485+
13561486
it("auto-migrates the shipped plugin-state SQLite sidecar by itself", async () => {
13571487
const root = await makeTempRoot();
13581488
const sourcePath = writeLegacyPluginStateSidecar(root);

‎src/infra/state-migrations.ts‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
INSTALLED_PLUGIN_INDEX_MIGRATION_VERSION,
4545
INSTALLED_PLUGIN_INDEX_VERSION,
4646
type InstalledPluginIndex,
47+
type InstalledPluginInstallRecordInfo,
4748
} from "../plugins/installed-plugin-index.js";
4849
import {
4950
buildAgentMainSessionKey,
@@ -63,6 +64,7 @@ import {
6364
getNodeSqliteKysely,
6465
} from "./kysely-sync.js";
6566
import { requireNodeSqlite } from "./node-sqlite.js";
67+
import { parseRegistryNpmSpec } from "./npm-registry-spec.js";
6668
import { isWithinDir } from "./path-safety.js";
6769
import {
6870
ensureDir,
@@ -374,6 +376,86 @@ function legacyInstalledPluginIndexMatches(
374376
);
375377
}
376378

379+
function readInstallRecordString(
380+
record: InstalledPluginInstallRecordInfo,
381+
key: keyof InstalledPluginInstallRecordInfo,
382+
): string | undefined {
383+
const value = record[key];
384+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
385+
}
386+
387+
function resolveNpmRecordPackageName(record: InstalledPluginInstallRecordInfo): string | undefined {
388+
const resolvedName = normalizeLowercaseStringOrEmpty(
389+
readInstallRecordString(record, "resolvedName"),
390+
);
391+
if (resolvedName) {
392+
return resolvedName;
393+
}
394+
const specName = parseRegistryNpmSpec(readInstallRecordString(record, "spec") ?? "")?.name;
395+
if (specName) {
396+
return specName;
397+
}
398+
return parseRegistryNpmSpec(readInstallRecordString(record, "resolvedSpec") ?? "")?.name;
399+
}
400+
401+
function resolveExactRegistryNpmSpecVersion(spec: string | undefined): string | undefined {
402+
const parsed = spec ? parseRegistryNpmSpec(spec) : null;
403+
return parsed?.selectorKind === "exact-version" ? parsed.selector : undefined;
404+
}
405+
406+
function resolveNpmRecordArtifactVersion(
407+
record: InstalledPluginInstallRecordInfo,
408+
): string | undefined {
409+
return (
410+
readInstallRecordString(record, "resolvedVersion") ??
411+
resolveExactRegistryNpmSpecVersion(readInstallRecordString(record, "resolvedSpec")) ??
412+
resolveExactRegistryNpmSpecVersion(readInstallRecordString(record, "spec")) ??
413+
readInstallRecordString(record, "version")
414+
);
415+
}
416+
417+
function installRecordStringFieldMatches(params: {
418+
current: InstalledPluginInstallRecordInfo;
419+
legacy: InstalledPluginInstallRecordInfo;
420+
key: keyof InstalledPluginInstallRecordInfo;
421+
}): boolean {
422+
const current = readInstallRecordString(params.current, params.key);
423+
const legacy = readInstallRecordString(params.legacy, params.key);
424+
return !current || !legacy || current === legacy;
425+
}
426+
427+
function npmInstallRecordsReferToSameArtifact(params: {
428+
current: InstalledPluginInstallRecordInfo;
429+
legacy: InstalledPluginInstallRecordInfo;
430+
}): boolean {
431+
if (params.current.source !== "npm" || params.legacy.source !== "npm") {
432+
return false;
433+
}
434+
const currentName = resolveNpmRecordPackageName(params.current);
435+
const legacyName = resolveNpmRecordPackageName(params.legacy);
436+
if (!currentName || currentName !== legacyName) {
437+
return false;
438+
}
439+
const currentVersion = resolveNpmRecordArtifactVersion(params.current);
440+
const legacyVersion = resolveNpmRecordArtifactVersion(params.legacy);
441+
if (!currentVersion || currentVersion !== legacyVersion) {
442+
return false;
443+
}
444+
return (["installPath", "integrity", "shasum"] as const).every((key) =>
445+
installRecordStringFieldMatches({ ...params, key }),
446+
);
447+
}
448+
449+
function installRecordsMatchForLegacyIndexMigration(params: {
450+
current: InstalledPluginInstallRecordInfo;
451+
legacy: InstalledPluginInstallRecordInfo;
452+
}): boolean {
453+
if (JSON.stringify(params.current) === JSON.stringify(params.legacy)) {
454+
return true;
455+
}
456+
return npmInstallRecordsReferToSameArtifact(params);
457+
}
458+
377459
function mergeLegacyInstalledPluginIndexRecords(
378460
current: InstalledPluginIndex,
379461
legacy: InstalledPluginIndex,
@@ -388,7 +470,9 @@ function mergeLegacyInstalledPluginIndexRecords(
388470
addedCount += 1;
389471
continue;
390472
}
391-
if (JSON.stringify(currentRecord) !== JSON.stringify(legacyRecord)) {
473+
if (
474+
!installRecordsMatchForLegacyIndexMigration({ current: currentRecord, legacy: legacyRecord })
475+
) {
392476
conflicts.push(pluginId);
393477
}
394478
}

0 commit comments

Comments
 (0)