Skip to content

Commit 5cebe96

Browse files
fix(doctor): archive superseded plugin install index conflicts (#93648)
Co-authored-by: openclaw-clownfish[bot] <280122609+openclaw-clownfish[bot]@users.noreply.github.com>
1 parent c45c87a commit 5cebe96

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,71 @@ describe("doctor legacy state migrations", () => {
18291829
});
18301830
});
18311831

1832+
it("archives exact legacy npm install record when SQLite has authoritative resolved metadata", async () => {
1833+
const root = await makeTempRoot();
1834+
await writeExistingPluginInstallIndex(root, {
1835+
discord: {
1836+
source: "npm",
1837+
spec: "@openclaw/discord@latest",
1838+
resolvedName: "@openclaw/discord",
1839+
resolvedVersion: "2026.6.16",
1840+
integrity: "sha512-current",
1841+
installedAt: "2026-06-16T12:00:00.000Z",
1842+
},
1843+
});
1844+
const sourcePath = writeLegacyPluginInstallIndex(root, {
1845+
discord: {
1846+
source: "npm",
1847+
spec: "@openclaw/[email protected]",
1848+
version: "2026.6.16",
1849+
installedAt: "2026-06-01T12:00:00.000Z",
1850+
},
1851+
});
1852+
1853+
const result = await runLegacyStateMigrationsForRoot(root);
1854+
1855+
expect(result.warnings).toStrictEqual([]);
1856+
expect(fs.existsSync(sourcePath)).toBe(false);
1857+
expect(fs.existsSync(`${sourcePath}.migrated`)).toBe(true);
1858+
await expect(readPersistedInstalledPluginIndex({ stateDir: root })).resolves.toMatchObject({
1859+
installRecords: {
1860+
discord: {
1861+
source: "npm",
1862+
spec: "@openclaw/discord@latest",
1863+
resolvedName: "@openclaw/discord",
1864+
resolvedVersion: "2026.6.16",
1865+
integrity: "sha512-current",
1866+
},
1867+
},
1868+
});
1869+
});
1870+
1871+
it("keeps exact legacy npm install record when SQLite lacks authoritative package identity", async () => {
1872+
const root = await makeTempRoot();
1873+
await writeExistingPluginInstallIndex(root, {
1874+
demo: {
1875+
source: "npm",
1876+
spec: "demo@latest",
1877+
version: "1.0.0",
1878+
},
1879+
});
1880+
const sourcePath = writeLegacyPluginInstallIndex(root, {
1881+
demo: {
1882+
source: "npm",
1883+
1884+
version: "1.0.0",
1885+
},
1886+
});
1887+
1888+
const result = await runLegacyStateMigrationsForRoot(root);
1889+
1890+
expect(result.warnings).toStrictEqual([
1891+
"Left plugin install index in place because shared SQLite state has conflicting plugin install metadata for: demo",
1892+
]);
1893+
expect(fs.existsSync(sourcePath)).toBe(true);
1894+
expect(fs.existsSync(`${sourcePath}.migrated`)).toBe(false);
1895+
});
1896+
18321897
for (const fixture of [
18331898
{
18341899
label: "name different packages",

‎src/infra/state-migrations.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {
6969
getNodeSqliteKysely,
7070
} from "./kysely-sync.js";
7171
import { requireNodeSqlite } from "./node-sqlite.js";
72+
import { parseRegistryNpmSpec } from "./npm-registry-spec.js";
7273
import { isWithinDir } from "./path-safety.js";
7374
import {
7475
ensureDir,
@@ -472,13 +473,54 @@ function legacyInstallRecordHasCurrentResolvedIdentity(params: {
472473
return Boolean(legacyResolvedSpec && currentResolvedSpec === legacyResolvedSpec);
473474
}
474475

476+
function readAuthoritativeCurrentNpmIdentity(
477+
record: InstalledPluginIndex["installRecords"][string],
478+
): { name: string; version: string } | null {
479+
const resolvedName = readInstallRecordStringField(record, "resolvedName");
480+
const resolvedVersion = readInstallRecordStringField(record, "resolvedVersion");
481+
if (resolvedName && resolvedVersion) {
482+
return { name: resolvedName, version: resolvedVersion };
483+
}
484+
const resolvedSpec = readInstallRecordStringField(record, "resolvedSpec");
485+
const parsed = resolvedSpec ? parseRegistryNpmSpec(resolvedSpec) : null;
486+
if (parsed?.selectorKind === "exact-version" && parsed.selector) {
487+
return { name: parsed.name, version: parsed.selector };
488+
}
489+
return null;
490+
}
491+
492+
function legacyNpmInstallRecordSupersededByCurrent(params: {
493+
currentRecord: InstalledPluginIndex["installRecords"][string];
494+
legacyRecord: InstalledPluginIndex["installRecords"][string];
495+
}): boolean {
496+
const { currentRecord, legacyRecord } = params;
497+
if (currentRecord.source !== "npm" || legacyRecord.source !== "npm") {
498+
return false;
499+
}
500+
const legacySpec = readInstallRecordStringField(legacyRecord, "spec");
501+
const legacyParsedSpec = legacySpec ? parseRegistryNpmSpec(legacySpec) : null;
502+
if (legacyParsedSpec?.selectorKind !== "exact-version") {
503+
return false;
504+
}
505+
const currentIdentity = readAuthoritativeCurrentNpmIdentity(currentRecord);
506+
return Boolean(
507+
currentIdentity &&
508+
legacyParsedSpec.selector &&
509+
currentIdentity.name === legacyParsedSpec.name &&
510+
currentIdentity.version === legacyParsedSpec.selector,
511+
);
512+
}
513+
475514
function legacyInstallRecordCoveredByCurrent(
476515
currentRecord: InstalledPluginIndex["installRecords"][string],
477516
legacyRecord: InstalledPluginIndex["installRecords"][string],
478517
): boolean {
479518
if (currentRecord.source !== legacyRecord.source) {
480519
return false;
481520
}
521+
if (legacyNpmInstallRecordSupersededByCurrent({ currentRecord, legacyRecord })) {
522+
return true;
523+
}
482524
for (const key of Object.keys(legacyRecord).toSorted()) {
483525
const currentValue = readInstallRecordField(currentRecord, key);
484526
if (currentValue === readInstallRecordField(legacyRecord, key)) {

0 commit comments

Comments
 (0)