Skip to content

Commit 4c2d8d8

Browse files
authored
fix(doctor): migrate legacy agent databases and bound registry writes to the active state dir (#120906)
* fix(doctor): migrate legacy agent databases discovered on disk and bound registry writes to the active state dir * fix(doctor): discover configured agent databases without registry rows * fix(doctor): preserve filesystem and configured database identity * fix(doctor): prioritize configured agent database identity * fix(doctor): prefer recorded agent database ownership
1 parent 1fc9d15 commit 4c2d8d8

10 files changed

Lines changed: 732 additions & 102 deletions

‎src/flows/doctor-health-contributions.test-support.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ type DoctorHealthContributionTestApi = {
3434
run?: (ctx: DoctorHealthFlowContext) => Promise<void>;
3535
}): DoctorHealthContribution;
3636
resolveDoctorHealthContributions(): DoctorHealthContribution[];
37+
runDoctorHealthContributionList(
38+
ctx: DoctorHealthFlowContext,
39+
contributions: readonly DoctorHealthContribution[],
40+
): Promise<void>;
3741
};
3842

3943
function getTestApi(): DoctorHealthContributionTestApi {
@@ -55,3 +59,10 @@ export function createDoctorHealthContribution(
5559
export function resolveDoctorHealthContributions(): DoctorHealthContribution[] {
5660
return getTestApi().resolveDoctorHealthContributions();
5761
}
62+
63+
export async function runDoctorHealthContributionList(
64+
ctx: DoctorHealthFlowContext,
65+
contributions: readonly DoctorHealthContribution[],
66+
): Promise<void> {
67+
await getTestApi().runDoctorHealthContributionList(ctx, contributions);
68+
}

‎src/flows/doctor-health-contributions.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { resolveDoctorContributionHealthChecks } from "./doctor-health-contribut
1313
import {
1414
createDoctorHealthContribution,
1515
resolveDoctorHealthContributions,
16+
runDoctorHealthContributionList,
1617
} from "./doctor-health-contributions.test-support.js";
1718
import { runDoctorLintChecks } from "./doctor-lint-flow.js";
1819
import type { HealthCheck, HealthFinding } from "./health-checks.js";
@@ -807,6 +808,43 @@ describe("doctor health contributions", () => {
807808
vi.restoreAllMocks();
808809
});
809810

811+
it("continues after one doctor contribution throws", async () => {
812+
const laterRun = vi.fn(async () => undefined);
813+
const contributions = [
814+
createDoctorHealthContribution({
815+
id: "doctor:test-failure",
816+
label: "Test failure",
817+
run: async () => {
818+
throw new Error("media migration required");
819+
},
820+
}),
821+
createDoctorHealthContribution({
822+
id: "doctor:test-later",
823+
label: "Test later",
824+
run: laterRun,
825+
}),
826+
];
827+
const ctx = {
828+
cfg: {},
829+
cfgForPersistence: {},
830+
configResult: { cfg: {} },
831+
configPath: "/tmp/fake-openclaw.json",
832+
sourceConfigValid: true,
833+
prompter: buildDoctorPrompter(true),
834+
runtime: { log: vi.fn(), error: vi.fn(), exit: vi.fn() },
835+
options: {},
836+
env: {},
837+
} as DoctorContributionRunContext;
838+
839+
await runDoctorHealthContributionList(ctx, contributions);
840+
841+
expect(laterRun).toHaveBeenCalledOnce();
842+
expect(mocks.note).toHaveBeenCalledWith(
843+
"doctor:test-failure run failed: media migration required",
844+
"Doctor warnings",
845+
);
846+
});
847+
810848
it("keeps legacy plugin manifest lint opt-in for structured findings", async () => {
811849
const contribution = requireDoctorContribution("doctor:legacy-plugin-manifests");
812850
const check = contribution.healthChecks[0] as HealthCheck & { defaultEnabled?: boolean };

‎src/flows/doctor-health-contributions.ts‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Doctor health contributions preserve the ordered interactive doctor flow while
22
// exposing the same checks to structured lint and repair commands.
33
import fs from "node:fs";
4+
import { scrubDoctorErrorMessage } from "./doctor-error-message.js";
45
import { hasActiveGatewayExecCredential } from "./doctor-gateway-exec-credential.js";
56
import {
67
runCoreContributionHealth,
@@ -414,31 +415,44 @@ export async function resolveDoctorContributionHealthChecks(): Promise<readonly
414415
return checks;
415416
}
416417

417-
export async function runDoctorHealthContributions(ctx: DoctorHealthFlowContext): Promise<void> {
418+
async function runDoctorHealthContributionList(
419+
ctx: DoctorHealthFlowContext,
420+
contributions: readonly DoctorHealthContribution[],
421+
): Promise<void> {
418422
const runWithPluginMetadataSnapshot = ctx.runWithPluginMetadataSnapshot;
419-
if (!runWithPluginMetadataSnapshot) {
420-
for (const contribution of resolveDoctorHealthContributions()) {
421-
await contribution.run(ctx);
423+
for (const contribution of contributions) {
424+
try {
425+
if (!runWithPluginMetadataSnapshot) {
426+
await contribution.run(ctx);
427+
continue;
428+
}
429+
const { resolveAgentWorkspaceDir, resolveDefaultAgentId } =
430+
await import("../agents/agent-scope.js");
431+
const workspaceDir = resolveAgentWorkspaceDir(
432+
ctx.cfg,
433+
resolveDefaultAgentId(ctx.cfg),
434+
ctx.env ?? process.env,
435+
);
436+
await runWithPluginMetadataSnapshot({ config: ctx.cfg, workspaceDir }, () =>
437+
contribution.run(ctx),
438+
);
439+
} catch (error) {
440+
const { note } = await loadNoteModule();
441+
note(`${contribution.id} run failed: ${scrubDoctorErrorMessage(error)}`, "Doctor warnings");
422442
}
423-
return;
424443
}
444+
}
425445

426-
const { resolveAgentWorkspaceDir, resolveDefaultAgentId } =
427-
await import("../agents/agent-scope.js");
428-
for (const contribution of resolveDoctorHealthContributions()) {
429-
const workspaceDir = resolveAgentWorkspaceDir(
430-
ctx.cfg,
431-
resolveDefaultAgentId(ctx.cfg),
432-
ctx.env ?? process.env,
433-
);
434-
await runWithPluginMetadataSnapshot({ config: ctx.cfg, workspaceDir }, () =>
435-
contribution.run(ctx),
436-
);
437-
}
446+
export async function runDoctorHealthContributions(ctx: DoctorHealthFlowContext): Promise<void> {
447+
await runDoctorHealthContributionList(ctx, resolveDoctorHealthContributions());
438448
}
439449

440450
if (process.env.VITEST || process.env.NODE_ENV === "test") {
441451
(globalThis as Record<PropertyKey, unknown>)[
442452
Symbol.for("openclaw.doctorHealthContributionsTestApi")
443-
] = { createDoctorHealthContribution, resolveDoctorHealthContributions };
453+
] = {
454+
createDoctorHealthContribution,
455+
resolveDoctorHealthContributions,
456+
runDoctorHealthContributionList,
457+
};
444458
}

‎src/infra/state-migrations.doctor.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { resolveChannelDefaultAccountId } from "../channels/plugins/helpers.js";
1212
import { getChannelPlugin } from "../channels/plugins/registry.js";
1313
import type { ChannelId } from "../channels/plugins/types.public.js";
1414
import { resolveOAuthDir, resolveStateDir } from "../config/paths.js";
15+
import { resolveSqliteTargetFromSessionStorePath } from "../config/sessions/session-sqlite-target.js";
16+
import { resolveSessionStoreTargets } from "../config/sessions/targets.js";
1517
import type { OpenClawConfig } from "../config/types.openclaw.js";
1618
import { createSubsystemLogger } from "../logging/subsystem.js";
1719
import {
@@ -1298,7 +1300,21 @@ export async function autoMigrateLegacyState(params: {
12981300
}
12991301
const mediaPersistence =
13001302
params.doctorOnlyStateMigrations === true
1301-
? migrateLegacyMediaPersistence({ env: { ...env, OPENCLAW_STATE_DIR: stateDir } })
1303+
? migrateLegacyMediaPersistence({
1304+
configuredAgentDatabaseTargets: resolveSessionStoreTargets(
1305+
params.cfg,
1306+
{ allAgents: true },
1307+
{ env },
1308+
).map((target) => ({
1309+
agentId: target.agentId,
1310+
path: resolveSqliteTargetFromSessionStorePath(target.storePath, {
1311+
agentId: target.agentId,
1312+
defaultAgentId: resolveDefaultAgentId(params.cfg),
1313+
env,
1314+
}).path,
1315+
})),
1316+
env: { ...env, OPENCLAW_STATE_DIR: stateDir },
1317+
})
13021318
: { changes: [], warnings: [] };
13031319
if (mediaPersistence.warnings.length > 0) {
13041320
return {

0 commit comments

Comments
 (0)