Skip to content

Commit 94fec60

Browse files
committed
fix(doctor): refresh metadata after repairs
1 parent 0826ac6 commit 94fec60

14 files changed

Lines changed: 114 additions & 26 deletions

‎src/commands/doctor-config-flow.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,7 @@ describe("doctor config flow", () => {
19291929
expect.objectContaining({ preparePluginMetadataSnapshot: true }),
19301930
);
19311931
expect(result.runWithPluginMetadataSnapshot).toEqual(expect.any(Function));
1932+
expect(result.invalidatePluginMetadataSnapshot).toEqual(expect.any(Function));
19321933
expect(collectDoctorPreviewNotesParamsMock).toHaveBeenLastCalledWith(
19331934
expect.objectContaining({
19341935
runWithPluginMetadataSnapshot: result.runWithPluginMetadataSnapshot,
@@ -1961,6 +1962,8 @@ describe("doctor config flow", () => {
19611962
};
19621963
expect(scopeParams.getBaseSnapshot()).toBe(refreshedSnapshot);
19631964
expect(scopeParams.getBaseSnapshot()?.index.installRecords).not.toHaveProperty("google-meet");
1965+
result.invalidatePluginMetadataSnapshot();
1966+
expect(scopeParams.getBaseSnapshot()).toBeUndefined();
19641967
});
19651968

19661969
it("collects plugin blocker previews from the pre-auto-enable config", async () => {

‎src/commands/doctor-config-flow.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ export async function loadAndMaybeMigrateDoctorConfig(params: {
168168
getBaseSnapshot: () => pluginMetadataSnapshotState.current,
169169
env: process.env,
170170
});
171+
const invalidatePluginMetadataSnapshot = () => {
172+
// Filesystem/install repairs replace authoritative plugin metadata. Clearing the
173+
// base makes the next Doctor scope rebuild once instead of reusing stale facts.
174+
pluginMetadataSnapshotState.current = undefined;
175+
};
171176
const runWithCurrentPluginMetadata = <T>(config: OpenClawConfig, run: () => T): T =>
172177
runWithPluginMetadataSnapshot(
173178
{
@@ -582,5 +587,6 @@ export async function loadAndMaybeMigrateDoctorConfig(params: {
582587
? { pluginMetadataSnapshot: pluginMetadataSnapshotState.current }
583588
: {}),
584589
runWithPluginMetadataSnapshot,
590+
invalidatePluginMetadataSnapshot,
585591
};
586592
}

‎src/commands/doctor-plugin-manifests.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ describe("doctor plugin manifest legacy contract repair", () => {
220220
configSchema: { type: "object" },
221221
});
222222

223-
await maybeRepairLegacyPluginManifestContracts({
223+
const changed = await maybeRepairLegacyPluginManifestContracts({
224224
config: configWithPluginLoadPath(pluginsRoot),
225225
env: {
226226
...process.env,
@@ -230,6 +230,7 @@ describe("doctor plugin manifest legacy contract repair", () => {
230230
prompter: createPrompter(),
231231
note: vi.fn(),
232232
});
233+
expect(changed).toBe(true);
233234

234235
const next = JSON.parse(fs.readFileSync(path.join(root, "openclaw.plugin.json"), "utf-8")) as {
235236
speechProviders?: string[];

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,15 @@ export async function maybeRepairLegacyPluginManifestContracts(params: {
182182
runtime: RuntimeEnv;
183183
prompter: DoctorPrompter;
184184
note?: typeof note;
185-
}): Promise<void> {
185+
}): Promise<boolean> {
186186
const migrations = collectLegacyPluginManifestContractMigrations({
187187
...(params.config ? { config: params.config } : {}),
188188
...(params.env ? { env: params.env } : {}),
189189
...(params.manifestRoots ? { manifestRoots: params.manifestRoots } : {}),
190190
...(params.workspaceDir ? { workspaceDir: params.workspaceDir } : {}),
191191
});
192192
if (migrations.length === 0) {
193-
return;
193+
return false;
194194
}
195195

196196
const emitNote = params.note ?? note;
@@ -209,7 +209,7 @@ export async function maybeRepairLegacyPluginManifestContracts(params: {
209209
initialValue: true,
210210
}));
211211
if (!shouldRepair) {
212-
return;
212+
return false;
213213
}
214214

215215
const applied: string[] = [];
@@ -227,4 +227,5 @@ export async function maybeRepairLegacyPluginManifestContracts(params: {
227227
if (applied.length > 0) {
228228
emitNote(applied.join("\n"), "Doctor changes");
229229
}
230+
return applied.length > 0;
230231
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,16 +450,17 @@ describe("maybeRepairPluginRegistryState", () => {
450450
const pluginDir = path.join(stateDir, "plugins", "demo");
451451
fs.mkdirSync(pluginDir, { recursive: true });
452452
await writePersistedInstalledPluginIndex(createCurrentIndex(), { stateDir });
453+
const candidate = createCandidate(pluginDir);
453454

454455
const nextConfig = await maybeRepairPluginRegistryState({
455456
stateDir,
456-
candidates: [createCandidate(pluginDir)],
457+
candidates: [candidate],
457458
env: hermeticEnv(),
458459
config: {},
459460
prompter: { shouldRepair: true },
460461
});
461462

462-
expect(nextConfig).toStrictEqual({});
463+
expect(nextConfig).toStrictEqual({ config: {}, pluginInventoryChanged: true });
463464
const persisted = await readRequiredPersistedInstalledPluginIndex(stateDir);
464465
expect(persisted.refreshReason).toBe("migration");
465466
expect(persisted.plugins).toStrictEqual([
@@ -469,6 +470,15 @@ describe("maybeRepairPluginRegistryState", () => {
469470
origin: "global",
470471
}),
471472
]);
473+
await expect(
474+
maybeRepairPluginRegistryState({
475+
stateDir,
476+
candidates: [candidate],
477+
env: hermeticEnv(),
478+
config: {},
479+
prompter: { shouldRepair: true },
480+
}),
481+
).resolves.toStrictEqual({ config: {} });
472482
});
473483

474484
it("warns about stale managed npm packages that shadow bundled plugins", async () => {
@@ -931,7 +941,7 @@ describe("maybeRepairPluginRegistryState", () => {
931941
config: {},
932942
prompter: { shouldRepair: false },
933943
}),
934-
).resolves.toEqual({});
944+
).resolves.toEqual({ config: {} });
935945

936946
const notes = vi.mocked(note).mock.calls.join("\n");
937947
expect(notes).toContain("Managed npm plugin packages could not be inspected");

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "../plugins/installed-plugin-index-records.js";
1616
import { loadInstalledPluginIndex } from "../plugins/installed-plugin-index.js";
1717
import { hasRetainedManagedNpmInstallMarker } from "../plugins/managed-npm-retention.js";
18+
import { resolveInstalledManifestRegistryIndexFingerprint } from "../plugins/manifest-registry-installed.js";
1819
import { refreshPluginRegistry } from "../plugins/plugin-registry.js";
1920
import {
2021
listStaleLocalBundledPluginInstallRecords,
@@ -47,6 +48,11 @@ type PluginRegistryDoctorRepairParams = Omit<PluginRegistryInstallMigrationParam
4748
prompter: Pick<DoctorPrompter, "shouldRepair">;
4849
};
4950

51+
type PluginRegistryDoctorRepairResult = {
52+
config: OpenClawConfig;
53+
pluginInventoryChanged?: true;
54+
};
55+
5056
type StaleManagedNpmBundledPlugin = {
5157
pluginId: string;
5258
packageName: string;
@@ -577,7 +583,7 @@ function assertNeverPluginRegistryIssue(issue: never): never {
577583
*/
578584
export async function maybeRepairPluginRegistryState(
579585
params: PluginRegistryDoctorRepairParams,
580-
): Promise<OpenClawConfig> {
586+
): Promise<PluginRegistryDoctorRepairResult> {
581587
const preflight = preflightPluginRegistryInstallMigration(params);
582588

583589
const migrationParams = {
@@ -611,7 +617,7 @@ export async function maybeRepairPluginRegistryState(
611617
"Plugin registry",
612618
);
613619
}
614-
return params.config;
620+
return { config: params.config };
615621
}
616622

617623
if (preflight.action === "migrate") {
@@ -634,7 +640,10 @@ export async function maybeRepairPluginRegistryState(
634640
"Plugin registry",
635641
);
636642
}
637-
return params.config;
643+
return {
644+
config: params.config,
645+
...(result.migrated ? { pluginInventoryChanged: true as const } : {}),
646+
};
638647
}
639648

640649
if (
@@ -662,7 +671,16 @@ export async function maybeRepairPluginRegistryState(
662671
`Plugin registry refreshed: ${enabled}/${total} enabled plugins indexed.`,
663672
"Plugin registry",
664673
);
674+
const indexChanged =
675+
resolveInstalledManifestRegistryIndexFingerprint(preflight.current) !==
676+
resolveInstalledManifestRegistryIndexFingerprint(index);
677+
return {
678+
config: params.config,
679+
...(indexChanged || repairedPluginOpenClawHostLinks
680+
? { pluginInventoryChanged: true as const }
681+
: {}),
682+
};
665683
}
666684

667-
return params.config;
685+
return { config: params.config };
668686
}

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ const DOCTOR_PLUGIN_ID_ALIASES: Readonly<Record<string, readonly string[]>> = {
2828
openai: ["openai-codex"],
2929
};
3030

31-
type PluginRegistryInstallMigrationPreflightAction = "skip-existing" | "migrate";
32-
33-
type PluginRegistryInstallMigrationPreflight = {
34-
/** Migration action selected before reading or writing registry state. */
35-
action: PluginRegistryInstallMigrationPreflightAction;
36-
/** Persisted plugin index path that migration will inspect or write. */
37-
filePath: string;
38-
};
31+
type PluginRegistryInstallMigrationPreflight =
32+
| {
33+
/** Migration action selected before reading or writing registry state. */
34+
action: "skip-existing";
35+
/** Persisted plugin index path that migration will inspect or write. */
36+
filePath: string;
37+
/** Authoritative pre-repair generation used to detect a real inventory change. */
38+
current: InstalledPluginIndex;
39+
}
40+
| {
41+
action: "migrate";
42+
filePath: string;
43+
};
3944

4045
type PluginRegistryInstallMigrationResult =
4146
| {
@@ -70,6 +75,7 @@ export function preflightPluginRegistryInstallMigration(
7075
return {
7176
action: "skip-existing",
7277
filePath,
78+
current: currentRegistry,
7379
};
7480
}
7581
}

‎src/commands/doctor/shared/release-configured-plugin-installs.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ describe("configured plugin install release step", () => {
550550
mocks.repairMissingPluginInstallsForIds.mockResolvedValue({
551551
changes: ['Installed missing configured plugin "codex".'],
552552
warnings: [],
553+
pluginInventoryChanged: true,
553554
});
554555
const result = await maybeRunConfiguredPluginInstallReleaseStep({
555556
cfg: {
@@ -571,6 +572,7 @@ describe("configured plugin install release step", () => {
571572
expect(repairCall.env).toEqual({});
572573
expect(result.touchedConfig).toBe(true);
573574
expect(result.completed).toBe(true);
575+
expect(result.pluginInventoryChanged).toBe(true);
574576
});
575577

576578
it("surfaces non-fatal repair notices without blocking release repair completion", async () => {

‎src/commands/doctor/shared/release-configured-plugin-installs.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ export async function maybeRunConfiguredPluginInstallReleaseStep(params: {
350350
warnings: string[];
351351
completed: boolean;
352352
touchedConfig: boolean;
353+
pluginInventoryChanged?: true;
353354
postInstallDoctorResult?: UpdatePostInstallDoctorResult;
354355
}> {
355356
const env = params.env ?? process.env;
@@ -381,6 +382,7 @@ export async function maybeRunConfiguredPluginInstallReleaseStep(params: {
381382
warnings,
382383
completed: repaired.warnings.length === 0,
383384
touchedConfig: false,
385+
...(repaired.pluginInventoryChanged ? { pluginInventoryChanged: true as const } : {}),
384386
...(postInstallDoctorResult ? { postInstallDoctorResult } : {}),
385387
};
386388
}
@@ -406,6 +408,7 @@ export async function maybeRunConfiguredPluginInstallReleaseStep(params: {
406408
warnings,
407409
completed,
408410
touchedConfig: completed,
411+
...(repaired.pluginInventoryChanged ? { pluginInventoryChanged: true as const } : {}),
409412
...(postInstallDoctorResult ? { postInstallDoctorResult } : {}),
410413
};
411414
}

‎src/flows/doctor-health-contribution-runners.state.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,28 @@ const loadDoctorStateIntegrityModule = async () =>
88
export async function runLegacyPluginManifestHealth(ctx: DoctorHealthFlowContext): Promise<void> {
99
const { maybeRepairLegacyPluginManifestContracts } =
1010
await import("../commands/doctor-plugin-manifests.js");
11-
await maybeRepairLegacyPluginManifestContracts({
11+
const pluginInventoryChanged = await maybeRepairLegacyPluginManifestContracts({
1212
config: ctx.cfg,
1313
env: process.env,
1414
runtime: ctx.runtime,
1515
prompter: ctx.prompter,
1616
});
17+
if (pluginInventoryChanged) {
18+
ctx.invalidatePluginMetadataSnapshot?.();
19+
}
1720
}
1821

1922
export async function runPluginRegistryHealth(ctx: DoctorHealthFlowContext): Promise<void> {
2023
const { maybeRepairPluginRegistryState } = await import("../commands/doctor-plugin-registry.js");
21-
ctx.cfg = await maybeRepairPluginRegistryState({
24+
const result = await maybeRepairPluginRegistryState({
2225
config: ctx.cfg,
2326
env: process.env,
2427
prompter: ctx.prompter,
2528
});
29+
ctx.cfg = result.config;
30+
if (result.pluginInventoryChanged) {
31+
ctx.invalidatePluginMetadataSnapshot?.();
32+
}
2633
}
2734

2835
export async function runReleaseConfiguredPluginInstallsHealth(
@@ -40,6 +47,9 @@ export async function runReleaseConfiguredPluginInstallsHealth(
4047
env: ctx.env ?? process.env,
4148
touchedVersion: ctx.configResult.sourceLastTouchedVersion ?? ctx.cfg.meta?.lastTouchedVersion,
4249
});
50+
if (result.pluginInventoryChanged) {
51+
ctx.invalidatePluginMetadataSnapshot?.();
52+
}
4353
if (result.postInstallDoctorResult) {
4454
ctx.postInstallDoctorResult = result.postInstallDoctorResult;
4555
}

0 commit comments

Comments
 (0)