Skip to content

Commit 5f714ef

Browse files
fix(postinstall): keep registry migration doctor-owned
Remove install-time SQLite migration from package postinstall and keep registry repair under explicit Doctor ownership. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 6ec9f4f commit 5f714ef

6 files changed

Lines changed: 127 additions & 192 deletions

File tree

‎scripts/postinstall-bundled-plugins.mjs‎

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -533,62 +533,6 @@ export function pruneInstalledPackageDist(params = {}) {
533533
return removed;
534534
}
535535

536-
function resolveDistModuleUrl(packageRoot, distPath) {
537-
return pathToFileURL(join(packageRoot, distPath)).href;
538-
}
539-
540-
async function importInstalledDistModule(params, distPath) {
541-
const packageRoot = params.packageRoot ?? DEFAULT_PACKAGE_ROOT;
542-
const pathExists = params.existsSync ?? existsSync;
543-
const modulePath = join(packageRoot, distPath);
544-
if (!pathExists(modulePath)) {
545-
return null;
546-
}
547-
const importModule = params.importModule ?? ((specifier) => import(specifier));
548-
return await importModule(resolveDistModuleUrl(packageRoot, distPath));
549-
}
550-
551-
export async function runPluginRegistryPostinstallMigration(params = {}) {
552-
const log = params.log ?? console;
553-
const packageRoot = params.packageRoot ?? DEFAULT_PACKAGE_ROOT;
554-
const env = params.env ?? process.env;
555-
const pathExists = params.existsSync ?? existsSync;
556-
557-
// Registry migration belongs to installed-package upgrades. Source checkouts
558-
// can contain stale dist from a different build and must not touch operator state.
559-
if (isSourceCheckoutRoot({ packageRoot, existsSync: pathExists })) {
560-
return { status: "skipped", reason: "source-checkout" };
561-
}
562-
563-
try {
564-
const migrationModule = await importInstalledDistModule(
565-
{ ...params, existsSync: pathExists },
566-
"dist/commands/doctor/shared/plugin-registry-migration.js",
567-
);
568-
if (!migrationModule) {
569-
return { status: "skipped", reason: "missing-dist-entry" };
570-
}
571-
if (typeof migrationModule.migratePluginRegistryForInstall !== "function") {
572-
return { status: "skipped", reason: "missing-dist-contract" };
573-
}
574-
575-
const result = await migrationModule.migratePluginRegistryForInstall({
576-
env,
577-
packageRoot,
578-
});
579-
if (result.migrated) {
580-
log.log(
581-
`[postinstall] migrated plugin registry: ${result.current.plugins.length} plugin(s) indexed`,
582-
);
583-
}
584-
return result;
585-
} catch (error) {
586-
const message = error instanceof Error ? error.message : String(error);
587-
log.warn(`[postinstall] could not migrate plugin registry: ${message}`);
588-
return { status: "failed", error: message };
589-
}
590-
}
591-
592536
export function isSourceCheckoutRoot(params) {
593537
const pathExists = params.existsSync ?? existsSync;
594538
const hasPostinstallInventory = pathExists(join(params.packageRoot, DIST_INVENTORY_PATH));
@@ -650,5 +594,4 @@ export function isDirectPostinstallInvocation(params = {}) {
650594

651595
if (isDirectPostinstallInvocation()) {
652596
runBundledPluginPostinstall();
653-
await runPluginRegistryPostinstallMigration();
654597
}

‎src/commands/doctor-plugin-registry.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ import {
4141
import type { DoctorPrompter } from "./doctor-prompter.js";
4242
import {
4343
InvalidPluginInstallRecordStateError,
44-
migratePluginRegistryForInstall,
45-
preflightPluginRegistryInstallMigration,
46-
type PluginRegistryInstallMigrationParams,
44+
migratePluginRegistryForDoctor,
45+
preflightPluginRegistryDoctorMigration,
46+
type PluginRegistryDoctorMigrationParams,
4747
} from "./doctor/shared/plugin-registry-migration.js";
4848

49-
type PluginRegistryDoctorRepairParams = Omit<PluginRegistryInstallMigrationParams, "config"> &
49+
type PluginRegistryDoctorRepairParams = Omit<PluginRegistryDoctorMigrationParams, "config"> &
5050
InstalledPluginIndexRecordStoreOptions & {
5151
config: OpenClawConfig;
5252
prompter: Pick<DoctorPrompter, "shouldRepair">;
@@ -397,7 +397,7 @@ async function loadInstallRecordsWithoutPluginIds(
397397
export async function detectPluginRegistryHealthIssues(
398398
params: PluginRegistryDoctorRepairParams,
399399
): Promise<PluginRegistryHealthIssue[]> {
400-
const preflight = preflightPluginRegistryInstallMigration(params);
400+
const preflight = preflightPluginRegistryDoctorMigration(params);
401401
const issues: PluginRegistryHealthIssue[] = [];
402402
if (preflight.action === "migrate") {
403403
issues.push({
@@ -605,9 +605,9 @@ function assertNeverPluginRegistryIssue(issue: never): never {
605605
export async function maybeRepairPluginRegistryState(
606606
params: PluginRegistryDoctorRepairParams,
607607
): Promise<PluginRegistryDoctorRepairResult> {
608-
let preflight: ReturnType<typeof preflightPluginRegistryInstallMigration>;
608+
let preflight: ReturnType<typeof preflightPluginRegistryDoctorMigration>;
609609
try {
610-
preflight = preflightPluginRegistryInstallMigration(params);
610+
preflight = preflightPluginRegistryDoctorMigration(params);
611611
} catch (error) {
612612
if (!(error instanceof InvalidPluginInstallRecordStateError)) {
613613
throw error;
@@ -648,7 +648,7 @@ export async function maybeRepairPluginRegistryState(
648648
}
649649

650650
if (preflight.action !== "skip-existing") {
651-
const result = await migratePluginRegistryForInstall({
651+
const result = await migratePluginRegistryForDoctor({
652652
...migrationParams,
653653
...(shouldPersistRepairedInstallRecords
654654
? {

‎src/commands/doctor/shared/plugin-registry-migration.test.ts‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from "../../../plugins/test-helpers/fs-fixtures.js";
1919
import * as stateDbReadOnly from "../../../state/openclaw-state-db-readonly.js";
2020
import { runOpenClawStateWriteTransaction } from "../../../state/openclaw-state-db.js";
21-
import { migratePluginRegistryForInstall } from "./plugin-registry-migration.js";
21+
import { migratePluginRegistryForDoctor } from "./plugin-registry-migration.js";
2222
const tempDirs: string[] = [];
2323

2424
afterEach(() => {
@@ -103,7 +103,7 @@ function expectSha256(value: unknown) {
103103
}
104104

105105
function requireMigratedIndex(
106-
result: Awaited<ReturnType<typeof migratePluginRegistryForInstall>>,
106+
result: Awaited<ReturnType<typeof migratePluginRegistryForDoctor>>,
107107
): InstalledPluginIndex {
108108
if (result.status !== "migrated") {
109109
throw new Error(`Expected migration result to be migrated, got ${result.status}`);
@@ -148,7 +148,7 @@ function insertStalePersistedIndexRow(stateDir: string, installRecordsJson = "{}
148148
);
149149
}
150150

151-
describe("plugin registry install migration", () => {
151+
describe("doctor plugin registry migration", () => {
152152
it("stops migration with the original error when the shared registry read fails", async () => {
153153
const stateDir = makeTempDir();
154154
const records = { authoritative: { source: "npm", spec: "[email protected]" } };
@@ -164,7 +164,7 @@ describe("plugin registry install migration", () => {
164164
const readConfig = vi.fn(() => ({}));
165165
await expect
166166
.soft(
167-
migratePluginRegistryForInstall({
167+
migratePluginRegistryForDoctor({
168168
stateDir,
169169
readConfig,
170170
candidates: [],
@@ -185,7 +185,7 @@ describe("plugin registry install migration", () => {
185185
{ env: { OPENCLAW_STATE_DIR: stateDir } },
186186
);
187187
expect(row).toMatchObject({ updated_at_ms: 123 });
188-
const restored = await migratePluginRegistryForInstall({
188+
const restored = await migratePluginRegistryForDoctor({
189189
stateDir,
190190
readConfig,
191191
candidates: [],
@@ -200,7 +200,7 @@ describe("plugin registry install migration", () => {
200200
await writePersistedInstalledPluginIndex(createCurrentIndex(), { stateDir });
201201
const readConfig = vi.fn(async () => ({}));
202202

203-
const result = await migratePluginRegistryForInstall({
203+
const result = await migratePluginRegistryForDoctor({
204204
stateDir,
205205
readConfig,
206206
env: hermeticEnv(),
@@ -222,7 +222,7 @@ describe("plugin registry install migration", () => {
222222
fs.mkdirSync(pluginDir, { recursive: true });
223223
insertStalePersistedIndexRow(stateDir);
224224

225-
const result = await migratePluginRegistryForInstall({
225+
const result = await migratePluginRegistryForDoctor({
226226
stateDir,
227227
candidates: [createCandidate(pluginDir)],
228228
readConfig: async () => ({}),
@@ -246,7 +246,7 @@ describe("plugin registry install migration", () => {
246246
insertStalePersistedIndexRow(stateDir, installRecordsJson);
247247

248248
await expect(
249-
migratePluginRegistryForInstall({
249+
migratePluginRegistryForDoctor({
250250
stateDir,
251251
readConfig: async () => ({}),
252252
env: hermeticEnv(),
@@ -283,7 +283,7 @@ describe("plugin registry install migration", () => {
283283
) as OpenClawConfig;
284284

285285
await expect(
286-
migratePluginRegistryForInstall({
286+
migratePluginRegistryForDoctor({
287287
stateDir,
288288
readConfig: async () => invalidConfig,
289289
env: hermeticEnv(),
@@ -303,7 +303,7 @@ describe("plugin registry install migration", () => {
303303
fs.mkdirSync(disabledDir, { recursive: true });
304304
fs.mkdirSync(unusedBundledDir, { recursive: true });
305305

306-
const result = await migratePluginRegistryForInstall({
306+
const result = await migratePluginRegistryForDoctor({
307307
stateDir,
308308
candidates: [
309309
createCandidate(enabledDir, "enabled-demo"),
@@ -344,7 +344,7 @@ describe("plugin registry install migration", () => {
344344
fs.mkdirSync(openaiDir, { recursive: true });
345345
fs.mkdirSync(unusedBundledDir, { recursive: true });
346346

347-
const result = await migratePluginRegistryForInstall({
347+
const result = await migratePluginRegistryForDoctor({
348348
stateDir,
349349
candidates: [
350350
createCandidate(openaiDir, "openai", "bundled", { enabledByDefault: true }),
@@ -370,7 +370,7 @@ describe("plugin registry install migration", () => {
370370
fs.mkdirSync(migrationDir, { recursive: true });
371371
fs.mkdirSync(unusedBundledDir, { recursive: true });
372372

373-
const result = await migratePluginRegistryForInstall({
373+
const result = await migratePluginRegistryForDoctor({
374374
stateDir,
375375
candidates: [
376376
createCandidate(migrationDir, "migrate-demo", "bundled", {
@@ -399,7 +399,7 @@ describe("plugin registry install migration", () => {
399399
fs.mkdirSync(openaiDir, { recursive: true });
400400
fs.mkdirSync(unusedBundledDir, { recursive: true });
401401

402-
const result = await migratePluginRegistryForInstall({
402+
const result = await migratePluginRegistryForDoctor({
403403
stateDir,
404404
candidates: [
405405
createCandidate(openaiDir, "openai", "bundled"),
@@ -431,7 +431,7 @@ describe("plugin registry install migration", () => {
431431
fs.mkdirSync(memoryDir, { recursive: true });
432432
fs.mkdirSync(unusedBundledDir, { recursive: true });
433433

434-
const result = await migratePluginRegistryForInstall({
434+
const result = await migratePluginRegistryForDoctor({
435435
stateDir,
436436
candidates: [
437437
createCandidate(memoryDir, "memory-core", "bundled", {
@@ -467,7 +467,7 @@ describe("plugin registry install migration", () => {
467467
const stateDir = makeTempDir();
468468
const readConfig = vi.fn(async () => ({}));
469469

470-
const result = await migratePluginRegistryForInstall({
470+
const result = await migratePluginRegistryForDoctor({
471471
stateDir,
472472
dryRun: true,
473473
readConfig,
@@ -488,7 +488,7 @@ describe("plugin registry install migration", () => {
488488
fs.mkdirSync(pluginDir, { recursive: true });
489489
const candidate = createCandidate(pluginDir);
490490

491-
const result = await migratePluginRegistryForInstall({
491+
const result = await migratePluginRegistryForDoctor({
492492
stateDir,
493493
candidates: [candidate],
494494
config: {},
@@ -514,7 +514,7 @@ describe("plugin registry install migration", () => {
514514
const pluginDir = path.join(stateDir, "plugins", "demo");
515515
fs.mkdirSync(pluginDir, { recursive: true });
516516

517-
const result = await migratePluginRegistryForInstall({
517+
const result = await migratePluginRegistryForDoctor({
518518
stateDir,
519519
candidates: [createCandidate(pluginDir, "demo", "global", { installOwner: "demo" })],
520520
readConfig: async () => ({
@@ -561,7 +561,7 @@ describe("plugin registry install migration", () => {
561561
const stateDir = makeTempDir();
562562
const pluginDir = path.join(stateDir, "plugins", "missing");
563563

564-
const result = await migratePluginRegistryForInstall({
564+
const result = await migratePluginRegistryForDoctor({
565565
stateDir,
566566
candidates: [],
567567
readConfig: async () => ({

‎src/commands/doctor/shared/plugin-registry-migration.ts‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const DOCTOR_PLUGIN_ID_ALIASES: Readonly<Record<string, readonly string[]>> = {
3232
openai: ["openai-codex"],
3333
};
3434

35-
type PluginRegistryInstallMigrationPreflight =
35+
type PluginRegistryDoctorMigrationPreflight =
3636
| {
3737
/** Migration action selected before reading or writing registry state. */
3838
action: "skip-existing";
@@ -46,16 +46,16 @@ type PluginRegistryInstallMigrationPreflight =
4646
filePath: string;
4747
};
4848

49-
type PluginRegistryInstallMigrationResult =
49+
type PluginRegistryDoctorMigrationResult =
5050
| {
5151
status: "skip-existing" | "dry-run";
5252
migrated: false;
53-
preflight: PluginRegistryInstallMigrationPreflight;
53+
preflight: PluginRegistryDoctorMigrationPreflight;
5454
}
5555
| {
5656
status: "migrated";
5757
migrated: true;
58-
preflight: PluginRegistryInstallMigrationPreflight;
58+
preflight: PluginRegistryDoctorMigrationPreflight;
5959
current: InstalledPluginIndex;
6060
};
6161

@@ -71,17 +71,17 @@ function invalidPersistedInstallRecordMessage(filePath: string): string {
7171
const INVALID_CONFIG_INSTALL_RECORD_MESSAGE =
7272
"plugins.installs contains invalid records. Back up openclaw.json, correct or remove the invalid retired plugins.installs record, then rerun `openclaw doctor --fix`.";
7373

74-
export type PluginRegistryInstallMigrationParams = LoadInstalledPluginIndexParams &
74+
export type PluginRegistryDoctorMigrationParams = LoadInstalledPluginIndexParams &
7575
InstalledPluginIndexStoreOptions & {
7676
dryRun?: boolean;
7777
existsSync?: (path: string) => boolean;
7878
readConfig?: () => Promise<OpenClawConfig> | OpenClawConfig;
7979
};
8080

81-
/** Decide whether plugin install registry migration should run for this environment. */
82-
export function preflightPluginRegistryInstallMigration(
83-
params: PluginRegistryInstallMigrationParams = {},
84-
): PluginRegistryInstallMigrationPreflight {
81+
/** Decide whether Doctor should migrate the plugin registry in this environment. */
82+
export function preflightPluginRegistryDoctorMigration(
83+
params: PluginRegistryDoctorMigrationParams = {},
84+
): PluginRegistryDoctorMigrationPreflight {
8585
const filePath = resolveInstalledPluginIndexStorePath(params);
8686
const persistedState = inspectPersistedInstalledPluginIndexInstallRecordsSync(params);
8787
if (persistedState.status === "invalid") {
@@ -120,7 +120,7 @@ export function preflightPluginRegistryInstallMigration(
120120
}
121121

122122
async function readMigrationConfig(
123-
params: PluginRegistryInstallMigrationParams,
123+
params: PluginRegistryDoctorMigrationParams,
124124
): Promise<OpenClawConfig> {
125125
if (params.config) {
126126
return params.config;
@@ -297,11 +297,11 @@ function listMigrationRelevantPluginRecords(params: {
297297
});
298298
}
299299

300-
/** Persist a migrated plugin install registry from legacy config/install records when needed. */
301-
export async function migratePluginRegistryForInstall(
302-
params: PluginRegistryInstallMigrationParams = {},
303-
): Promise<PluginRegistryInstallMigrationResult> {
304-
const preflight = preflightPluginRegistryInstallMigration(params);
300+
/** Persist Doctor's migrated plugin registry from legacy config/install records when needed. */
301+
export async function migratePluginRegistryForDoctor(
302+
params: PluginRegistryDoctorMigrationParams = {},
303+
): Promise<PluginRegistryDoctorMigrationResult> {
304+
const preflight = preflightPluginRegistryDoctorMigration(params);
305305
if (preflight.action === "skip-existing") {
306306
return { status: "skip-existing", migrated: false, preflight };
307307
}

0 commit comments

Comments
 (0)