Skip to content

Commit 7068006

Browse files
steipeteampagent
andauthored
1 parent 11ebdfc commit 7068006

7 files changed

Lines changed: 153 additions & 37 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe("doctor --fix with a validation-blocked candidate", () => {
6767
// The write must refuse gracefully — no throw, no change panel, no file write.
6868
await expect(runWriteConfigHealth(ctx)).resolves.toBeUndefined();
6969

70-
expect(ctx.configWriteBlockedByValidation).toBe(true);
70+
expect(ctx.configWriteRefusal).toBe("validation");
7171
expect(ctx.configResultWriteCommitted).not.toBe(true);
7272
expect(noteMock.mock.calls.some(([, title]) => title === "Doctor changes")).toBe(false);
7373
const warning = noteMock.mock.calls.find(

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ export async function runWriteConfigHealth(
3131
ctx: DoctorHealthFlowContext,
3232
options: { runPostWriteRepairs?: boolean } = {},
3333
): Promise<void> {
34-
if (ctx.configWriteDeferredByCronOwnership === true) {
35-
return;
36-
}
37-
if (ctx.configWriteBlockedByValidation === true) {
38-
// The initial write already reported the validation refusal; retrying the
34+
if (ctx.configWriteRefusal) {
35+
// The initial write already reported the refusal; retrying the
3936
// same candidate would fail identically and duplicate the warning.
4037
return;
4138
}
@@ -103,7 +100,7 @@ export async function runWriteConfigHealth(
103100
].join("\n"),
104101
"Doctor warnings",
105102
);
106-
ctx.configWriteBlockedByValidation = true;
103+
ctx.configWriteRefusal = "validation";
107104
return;
108105
}
109106
const { isCronOwnerWriteRefusalError } = await import("../config/io.cron-owner-refusal.js");
@@ -119,7 +116,7 @@ export async function runWriteConfigHealth(
119116
].join("\n"),
120117
"Doctor warnings",
121118
);
122-
ctx.configWriteDeferredByCronOwnership = true;
119+
ctx.configWriteRefusal = "cron-owner-safety";
123120
return;
124121
}
125122
// The atomic write committed: repair panels queued by the config flow are now

‎src/flows/doctor-health-contribution-types.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ export type DoctorHealthFlowContext = {
3939
cfgForPersistence: OpenClawConfig;
4040
/** The finalized config-flow candidate crossed the atomic writer boundary. */
4141
configResultWriteCommitted?: boolean;
42-
/** Cron ownership could not be made safe, so every config write remains deferred this run. */
43-
configWriteDeferredByCronOwnership?: true;
44-
/** The repaired candidate failed write validation; nothing was persisted this run. */
45-
configWriteBlockedByValidation?: true;
42+
/** The requested config write was refused; later repairs must not consume its candidate. */
43+
configWriteRefusal?: "validation" | "cron-owner-safety";
4644
/** One-shot repairs that require a durable config write have completed. */
4745
postConfigWriteRepairsCommitted?: boolean;
4846
sourceConfigValid: boolean;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ describe("doctor health contributions", () => {
11651165
// candidate config, so the loop stops before they persist state derived
11661166
// from a config that never reached disk (same invariant as cron deferral).
11671167
expect(laterRun).not.toHaveBeenCalled();
1168-
expect(ctx.configWriteBlockedByValidation).toBe(true);
1168+
expect(ctx.configWriteRefusal).toBe("validation");
11691169
expect(ctx.configResultWriteCommitted).not.toBe(true);
11701170
expect(ctx.cfgForPersistence).toEqual(cfg);
11711171
// Never print "Doctor changes" for changes that were not persisted.
@@ -1225,7 +1225,7 @@ describe("doctor health contributions", () => {
12251225
);
12261226
await requireDoctorContribution("doctor:write-config").run(ctx);
12271227

1228-
expect(ctx.configWriteBlockedByValidation).toBe(true);
1228+
expect(ctx.configWriteRefusal).toBe("validation");
12291229
expect(mocks.note).toHaveBeenCalledWith(
12301230
expect.stringContaining("Earlier config fixes were already saved"),
12311231
"Doctor warnings",
@@ -1302,7 +1302,7 @@ describe("doctor health contributions", () => {
13021302
expect(mocks.replaceConfigFile).toHaveBeenCalledOnce();
13031303
expect(laterRun).not.toHaveBeenCalled();
13041304
expect(ctx.configResultWriteCommitted).not.toBe(true);
1305-
expect(ctx.configWriteDeferredByCronOwnership).toBe(true);
1305+
expect(ctx.configWriteRefusal).toBe("cron-owner-safety");
13061306
expect(ctx.cfgForPersistence).toEqual(cfg);
13071307
expect(mocks.note).toHaveBeenCalledWith(
13081308
expect.stringContaining("preserving any retained legacy owner"),

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,9 @@ async function runDoctorHealthContributionList(
449449
contribution.run(ctx),
450450
);
451451
}
452-
if (ctx.configWriteDeferredByCronOwnership === true) {
453-
// Later repairs consume the candidate config. Stop before they persist state under an
454-
// ownership topology that the config writer deliberately left non-durable.
455-
return;
456-
}
457-
if (ctx.configWriteBlockedByValidation === true) {
458-
// Same invariant for a validation-refused write: the candidate never reached
459-
// disk, so later repairs must not persist state derived from it.
452+
if (ctx.configWriteRefusal) {
453+
// Later repairs consume the candidate. Stop before they persist state
454+
// derived from config that the writer deliberately left non-durable.
460455
return;
461456
}
462457
} catch (error) {

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

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { runDoctorHealthFlow } from "./doctor-health.js";
3+
4+
const mocks = vi.hoisted(() => ({
5+
outro: vi.fn(),
6+
writeUpdatePostInstallDoctorResult: vi.fn(),
7+
}));
8+
9+
vi.mock("@clack/prompts", () => ({
10+
intro: vi.fn(),
11+
outro: mocks.outro,
12+
}));
13+
14+
vi.mock("../config/paths.js", () => ({
15+
resolveStateDir: () => "/tmp/openclaw-doctor-state-does-not-exist",
16+
}));
17+
18+
vi.mock("../state/openclaw-database-preflight.js", () => ({
19+
OpenClawDatabaseSchemaPreflightError: class extends Error {},
20+
preflightOpenClawDatabaseSchemas: () => ({ incompatible: [] }),
21+
}));
22+
23+
vi.mock("../state/openclaw-agent-db.js", () => ({
24+
OPENCLAW_AGENT_SCHEMA_VERSION: 1,
25+
}));
26+
27+
vi.mock("../state/openclaw-state-db.js", () => ({
28+
OPENCLAW_STATE_SCHEMA_VERSION: 1,
29+
}));
30+
31+
vi.mock("../commands/doctor-prompter.js", () => ({
32+
createDoctorPrompter: () => ({}),
33+
}));
34+
35+
vi.mock("../infra/openclaw-root.js", () => ({
36+
resolveOpenClawPackageRoot: async () => undefined,
37+
}));
38+
39+
vi.mock("../commands/doctor-update.js", () => ({
40+
maybeOfferUpdateBeforeDoctor: async () => ({ handled: false }),
41+
}));
42+
43+
vi.mock("../commands/doctor-ui.js", () => ({
44+
maybeRepairUiProtocolFreshness: async () => undefined,
45+
}));
46+
47+
vi.mock("../commands/doctor-install.js", () => ({
48+
noteSourceInstallIssues: () => undefined,
49+
}));
50+
51+
vi.mock("../commands/doctor/shared/plugin-runtime-symlinks.js", () => ({
52+
noteStalePluginRuntimeSymlinks: async () => undefined,
53+
}));
54+
55+
vi.mock("../commands/doctor-platform-notes.js", () => ({
56+
noteStartupOptimizationHints: () => undefined,
57+
}));
58+
59+
vi.mock("../commands/doctor-config-flow.js", () => ({
60+
loadAndMaybeMigrateDoctorConfig: async () => ({ cfg: {}, shouldWriteConfig: true }),
61+
}));
62+
63+
vi.mock("../config/config.js", () => ({
64+
CONFIG_PATH: "/tmp/openclaw.json",
65+
}));
66+
67+
vi.mock("../infra/update-doctor-result.js", () => ({
68+
UPDATE_POST_INSTALL_DOCTOR_ADVISORY_EXIT_CODE: 86,
69+
UPDATE_POST_INSTALL_DOCTOR_RESULT_PATH_ENV: "OPENCLAW_UPDATE_POST_INSTALL_DOCTOR_RESULT_PATH",
70+
writeUpdatePostInstallDoctorResult: mocks.writeUpdatePostInstallDoctorResult,
71+
}));
72+
73+
vi.mock("./doctor-health-contributions.js", () => ({
74+
runDoctorHealthContributions: async (ctx: {
75+
configWriteRefusal?: "cron-owner-safety";
76+
postInstallDoctorResult?: {
77+
status: "advisory";
78+
advisory: {
79+
kind: "package-post-install-doctor";
80+
message: string;
81+
reason: "deferred-configured-plugin-repair";
82+
details: string[];
83+
};
84+
};
85+
}) => {
86+
ctx.configWriteRefusal = "cron-owner-safety";
87+
ctx.postInstallDoctorResult = {
88+
status: "advisory",
89+
advisory: {
90+
kind: "package-post-install-doctor",
91+
message: "recoverable plugin repair",
92+
reason: "deferred-configured-plugin-repair",
93+
details: ["plugin repair deferred"],
94+
},
95+
};
96+
},
97+
}));
98+
99+
describe("runDoctorHealthFlow", () => {
100+
beforeEach(() => {
101+
mocks.outro.mockClear();
102+
mocks.writeUpdatePostInstallDoctorResult.mockClear();
103+
});
104+
105+
it("reports a cron ownership refusal instead of a recoverable post-install advisory", async () => {
106+
const runtime = {
107+
log: vi.fn(),
108+
error: vi.fn(),
109+
exit: vi.fn(),
110+
};
111+
vi.stubEnv(
112+
"OPENCLAW_UPDATE_POST_INSTALL_DOCTOR_RESULT_PATH",
113+
"/tmp/openclaw-update-doctor-result.json",
114+
);
115+
116+
try {
117+
await runDoctorHealthFlow(runtime, {});
118+
} finally {
119+
vi.unstubAllEnvs();
120+
}
121+
122+
expect(mocks.outro).toHaveBeenCalledWith("Doctor finished, but config fixes were not applied.");
123+
expect(mocks.outro).not.toHaveBeenCalledWith("Doctor complete.");
124+
expect(runtime.exit).toHaveBeenCalledWith(1);
125+
expect(runtime.exit).not.toHaveBeenCalledWith(86);
126+
expect(mocks.writeUpdatePostInstallDoctorResult).not.toHaveBeenCalled();
127+
});
128+
});

‎src/flows/doctor-health.ts‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ export async function runDoctorHealthFlow(runtime?: RuntimeEnv, options: DoctorO
114114
};
115115
const { runDoctorHealthContributions } = await import("./doctor-health-contributions.js");
116116
await runDoctorHealthContributions(ctx);
117+
if (ctx.configWriteRefusal) {
118+
// Config fixes were computed but refused by the writer; the warning above
119+
// already lists the manual work. This failure outranks a recoverable
120+
// post-install advisory because the run did not converge.
121+
outro(
122+
ctx.configResultWriteCommitted === true
123+
? "Doctor finished, but some config fixes were not applied."
124+
: "Doctor finished, but config fixes were not applied.",
125+
);
126+
effectiveRuntime.exit(1);
127+
return;
128+
}
117129
if (ctx.postInstallDoctorResult) {
118130
const {
119131
UPDATE_POST_INSTALL_DOCTOR_ADVISORY_EXIT_CODE,
@@ -130,19 +142,5 @@ export async function runDoctorHealthFlow(runtime?: RuntimeEnv, options: DoctorO
130142
return;
131143
}
132144
}
133-
134-
if (ctx.configWriteBlockedByValidation) {
135-
// Config fixes were computed but refused by write validation; the warning
136-
// above already lists the manual work. When an earlier pass committed, only
137-
// the later fixes are missing. Exit non-zero so --fix callers see that the
138-
// run did not converge.
139-
outro(
140-
ctx.configResultWriteCommitted === true
141-
? "Doctor finished, but some config fixes were not applied."
142-
: "Doctor finished, but config fixes were not applied.",
143-
);
144-
effectiveRuntime.exit(1);
145-
return;
146-
}
147145
outro("Doctor complete.");
148146
}

0 commit comments

Comments
 (0)