Skip to content

Commit a4c07d0

Browse files
fix(doctor): preserve update before schema refusal
1 parent 2aba1b2 commit a4c07d0

3 files changed

Lines changed: 58 additions & 8 deletions

File tree

‎src/commands/doctor.e2e-harness.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const confirm = vi.fn().mockResolvedValue(true) as unknown as MockFn;
6666
const select = vi.fn().mockResolvedValue("node") as unknown as MockFn;
6767
const note = vi.fn() as unknown as MockFn;
6868
export const writeConfigFile = vi.fn().mockResolvedValue(undefined) as unknown as MockFn;
69-
const resolveOpenClawPackageRoot = vi.fn().mockResolvedValue(null) as unknown as MockFn;
69+
export const resolveOpenClawPackageRoot = vi.fn().mockResolvedValue(null) as unknown as MockFn;
7070
export const runGatewayUpdate = vi
7171
.fn()
7272
.mockResolvedValue(createGatewayUpdateResult()) as unknown as MockFn;
@@ -87,7 +87,7 @@ const runExec = vi.fn().mockResolvedValue({
8787
stdout: "",
8888
stderr: "",
8989
}) as unknown as MockFn;
90-
const runCommandWithTimeout = vi
90+
export const runCommandWithTimeout = vi
9191
.fn()
9292
.mockResolvedValue(createCommandWithTimeoutResult()) as unknown as MockFn;
9393

‎src/commands/doctor.refuses-newer-database-schemas.e2e.test.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
createDoctorRuntime,
1010
mockDoctorConfigSnapshot,
1111
readConfigFileSnapshot,
12+
resolveOpenClawPackageRoot,
13+
runCommandWithTimeout,
1214
runGatewayUpdate,
1315
} from "./doctor.e2e-harness.js";
1416

@@ -21,7 +23,33 @@ describe("doctor database schema preflight", () => {
2123
vi.clearAllMocks();
2224
});
2325

24-
it("refuses a newer state schema before update and config repair flows", async () => {
26+
it("lets a successful interactive update replace the stale doctor", async () => {
27+
writeStateSchemaVersion(OPENCLAW_STATE_SCHEMA_VERSION + 1);
28+
mockDoctorConfigSnapshot();
29+
mockInteractiveGitUpdate("ok");
30+
31+
await expect(doctorCommand(createDoctorRuntime())).resolves.toBeUndefined();
32+
33+
expect(runGatewayUpdate).toHaveBeenCalledOnce();
34+
expect(autoMigrateLegacyStateDir).not.toHaveBeenCalled();
35+
expect(readConfigFileSnapshot).not.toHaveBeenCalled();
36+
});
37+
38+
it("refuses after an interactive update does not handle doctor", async () => {
39+
writeStateSchemaVersion(OPENCLAW_STATE_SCHEMA_VERSION + 1);
40+
mockDoctorConfigSnapshot();
41+
mockInteractiveGitUpdate("skipped");
42+
43+
await expect(doctorCommand(createDoctorRuntime())).rejects.toThrow(
44+
/Doctor refused to continue.*database schema.*newer than this build/iu,
45+
);
46+
47+
expect(runGatewayUpdate).toHaveBeenCalledOnce();
48+
expect(autoMigrateLegacyStateDir).not.toHaveBeenCalled();
49+
expect(readConfigFileSnapshot).not.toHaveBeenCalled();
50+
});
51+
52+
it("refuses before config repair flows when updates are disabled", async () => {
2553
writeStateSchemaVersion(OPENCLAW_STATE_SCHEMA_VERSION + 1);
2654
mockDoctorConfigSnapshot();
2755

@@ -35,6 +63,25 @@ describe("doctor database schema preflight", () => {
3563
});
3664
});
3765

66+
function mockInteractiveGitUpdate(status: "ok" | "skipped"): void {
67+
delete process.env.OPENCLAW_UPDATE_IN_PROGRESS;
68+
resolveOpenClawPackageRoot.mockResolvedValue("/repo");
69+
runCommandWithTimeout.mockResolvedValue({
70+
stdout: "/repo\n",
71+
stderr: "",
72+
code: 0,
73+
signal: null,
74+
killed: false,
75+
});
76+
runGatewayUpdate.mockResolvedValue({
77+
status,
78+
mode: "git",
79+
root: "/repo",
80+
steps: [],
81+
durationMs: 0,
82+
});
83+
}
84+
3885
function writeStateSchemaVersion(version: number): void {
3986
const statePath = resolveOpenClawStateSqlitePath(process.env);
4087
fs.mkdirSync(path.dirname(statePath), { recursive: true });

‎src/flows/doctor-health.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ export async function doctorCommand(runtime?: RuntimeEnv, options: DoctorOptions
4949
// Preserve the entry fact so doctor can report that automatic initialization.
5050
const stateDirExistedAtStart = stateDirectoryExistsAtDoctorStart();
5151
intro("OpenClaw doctor");
52-
await assertDoctorDatabaseSchemasCompatible();
53-
if (options.repair === true || options.yes === true || options.generateGatewayToken === true) {
54-
const { assertConfigWriteAllowedInCurrentMode } = await loadConfigModule();
55-
assertConfigWriteAllowedInCurrentMode();
56-
}
5752

5853
const { createDoctorPrompter } = await import("../commands/doctor-prompter.js");
5954
const prompter = createDoctorPrompter({ runtime: effectiveRuntime, options });
@@ -77,6 +72,14 @@ export async function doctorCommand(runtime?: RuntimeEnv, options: DoctorOptions
7772
return;
7873
}
7974

75+
// A stale source checkout may update itself, but no diagnostic or repair may
76+
// touch state until the surviving build proves it understands every database.
77+
await assertDoctorDatabaseSchemasCompatible();
78+
if (options.repair === true || options.yes === true || options.generateGatewayToken === true) {
79+
const { assertConfigWriteAllowedInCurrentMode } = await loadConfigModule();
80+
assertConfigWriteAllowedInCurrentMode();
81+
}
82+
8083
// Keep side-effect-heavy legacy checks before structured contributions until fully migrated.
8184
const { maybeRepairUiProtocolFreshness } = await import("../commands/doctor-ui.js");
8285
const { noteSourceInstallIssues } = await import("../commands/doctor-install.js");

0 commit comments

Comments
 (0)