Skip to content

Commit 83208eb

Browse files
steipetePeter Steinberger
andauthored
fix(doctor): preserve unresolved per-check repair findings (#117448)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent b83a966 commit 83208eb

2 files changed

Lines changed: 117 additions & 5 deletions

File tree

‎src/flows/doctor-repair-flow.test.ts‎

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
44
import { runDoctorHealthRepairs } from "./doctor-repair-flow.js";
55
import { normalizeHealthCheck } from "./health-check-adapter.js";
66
import type { RunnableHealthCheck, SplitHealthCheckInput } from "./health-check-runner-types.js";
7-
import type { HealthCheck, HealthRepairContext } from "./health-checks.js";
7+
import type { HealthCheck, HealthFinding, HealthRepairContext } from "./health-checks.js";
88

99
function ctx(cfg: OpenClawConfig): HealthRepairContext {
1010
return {
@@ -18,6 +18,34 @@ function ctx(cfg: OpenClawConfig): HealthRepairContext {
1818
};
1919
}
2020

21+
function warningFinding(checkId: string): HealthFinding {
22+
return {
23+
checkId,
24+
severity: "warning",
25+
message: `Unresolved finding from ${checkId}.`,
26+
path: checkId,
27+
};
28+
}
29+
30+
function successfullyRepairedCheck(): HealthCheck {
31+
return normalizeHealthCheck({
32+
id: "test/repaired-first",
33+
kind: "core",
34+
description: "repairs before the unresolved check",
35+
async detect(checkContext) {
36+
return checkContext.cfg.gateway?.mode === "local"
37+
? []
38+
: [warningFinding("test/repaired-first")];
39+
},
40+
async repair(checkContext) {
41+
return {
42+
config: { ...checkContext.cfg, gateway: { ...checkContext.cfg.gateway, mode: "local" } },
43+
changes: ["Set gateway.mode to local."],
44+
};
45+
},
46+
});
47+
}
48+
2149
describe("runDoctorHealthRepairs", () => {
2250
it("repairs single-run checks and validates through lint mode", async () => {
2351
const runModes: string[] = [];
@@ -114,7 +142,7 @@ describe("runDoctorHealthRepairs", () => {
114142
})).not.toMatchTypeOf<SplitRepair>();
115143
});
116144

117-
it("leaves non-repairable checks for legacy doctor behavior", async () => {
145+
it("retains non-repairable findings for the legacy doctor owner", async () => {
118146
const checks: HealthCheck[] = [
119147
normalizeHealthCheck({
120148
id: "test/legacy-only",
@@ -136,7 +164,7 @@ describe("runDoctorHealthRepairs", () => {
136164

137165
expect(result.config).toEqual({});
138166
expect(result.findings).toHaveLength(1);
139-
expect(result.remainingFindings).toEqual([]);
167+
expect(result.remainingFindings).toEqual(result.findings);
140168
expect(result.changes).toEqual([]);
141169
expect(result.checksRepaired).toBe(0);
142170
expect(result.checksValidated).toBe(0);
@@ -173,6 +201,7 @@ describe("runDoctorHealthRepairs", () => {
173201
},
174202
]);
175203
expect(result.warnings).toEqual(["test/repair-throws repair failed: repair exploded"]);
204+
expect(result.remainingFindings).toEqual(result.findings);
176205
expect(result.checksRepaired).toBe(0);
177206
expect(result.checksValidated).toBe(0);
178207
});
@@ -285,11 +314,90 @@ describe("runDoctorHealthRepairs", () => {
285314
expect(validationCalls).toBe(1);
286315
expect(result.checksRepaired).toBe(0);
287316
expect(result.checksValidated).toBe(0);
288-
expect(result.remainingFindings).toEqual([]);
317+
expect(result.remainingFindings).toEqual(result.findings);
289318
expect(result.changes).toEqual(["Review required before changing gateway.mode."]);
290319
expect(result.warnings).toEqual(["test/skipped repair skipped: manual confirmation required"]);
291320
});
292321

322+
it.each(["skipped", "failed", "throws", "unavailable", "validation-failed"] as const)(
323+
"preserves unresolved split findings after a successful sibling repair (%s)",
324+
async (outcome) => {
325+
const unresolvedId = `test/split-${outcome}`;
326+
const unresolvedCheck = normalizeHealthCheck({
327+
id: unresolvedId,
328+
kind: "core",
329+
description: "unresolved split check",
330+
async detect(_checkContext, scope) {
331+
if (outcome === "validation-failed" && scope !== undefined) {
332+
throw new Error("validation unavailable");
333+
}
334+
return [warningFinding(unresolvedId)];
335+
},
336+
...(outcome === "unavailable"
337+
? {}
338+
: {
339+
async repair() {
340+
if (outcome === "throws") {
341+
throw new Error("repair unavailable");
342+
}
343+
if (outcome === "skipped" || outcome === "failed") {
344+
return { status: outcome, reason: "manual repair required", changes: [] };
345+
}
346+
return { changes: ["Attempted unresolved repair."] };
347+
},
348+
}),
349+
});
350+
351+
const result = await runDoctorHealthRepairs(ctx({}), {
352+
checks: [successfullyRepairedCheck(), unresolvedCheck],
353+
});
354+
355+
expect(result.config.gateway?.mode).toBe("local");
356+
expect(result.findings.map((finding) => finding.checkId)).toEqual([
357+
"test/repaired-first",
358+
unresolvedId,
359+
]);
360+
expect(result.remainingFindings).toEqual([warningFinding(unresolvedId)]);
361+
expect(result.checksRun).toBe(2);
362+
expect(result.checksRepaired).toBe(outcome === "validation-failed" ? 2 : 1);
363+
expect(result.checksValidated).toBe(1);
364+
},
365+
);
366+
367+
it.each(["skipped", "failed", "unavailable", "validation-failed"] as const)(
368+
"preserves unresolved runnable findings after a successful split sibling repair (%s)",
369+
async (outcome) => {
370+
const unresolvedId = `test/runnable-${outcome}`;
371+
const runnable: RunnableHealthCheck = {
372+
id: unresolvedId,
373+
kind: "core",
374+
description: "unresolved runnable check",
375+
async run(checkContext) {
376+
if (checkContext.mode === "lint") {
377+
throw new Error("validation unavailable");
378+
}
379+
const findings = [warningFinding(unresolvedId)];
380+
if (outcome === "skipped" || outcome === "failed") {
381+
return { status: outcome, reason: "manual repair required", findings };
382+
}
383+
return outcome === "validation-failed"
384+
? { findings, changes: ["Attempted unresolved repair."] }
385+
: { findings };
386+
},
387+
};
388+
389+
const result = await runDoctorHealthRepairs(ctx({}), {
390+
checks: [successfullyRepairedCheck(), normalizeHealthCheck(runnable)],
391+
});
392+
393+
expect(result.config.gateway?.mode).toBe("local");
394+
expect(result.remainingFindings).toEqual([warningFinding(unresolvedId)]);
395+
expect(result.checksRun).toBe(2);
396+
expect(result.checksRepaired).toBe(outcome === "validation-failed" ? 2 : 1);
397+
expect(result.checksValidated).toBe(1);
398+
},
399+
);
400+
293401
it("supports dry-run repairs without applying returned config or validating", async () => {
294402
const repairContexts: HealthRepairContext[] = [];
295403
let detectCalls = 0;

‎src/flows/doctor-repair-flow.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ export async function runDoctorHealthRepairs(
6060
const runResult = await runHealthCheck(check, detectCtx, opts);
6161
cfg = runResult.config;
6262
findings.push(...runResult.findings);
63-
remainingFindings.push(...runResult.remainingFindings);
63+
// Only a completed validation can replace this check's original findings;
64+
// skipped, failed, and unvalidated siblings must remain visible in mixed batches.
65+
remainingFindings.push(
66+
...(runResult.checksValidated > 0 ? runResult.remainingFindings : runResult.findings),
67+
);
6468
changes.push(...runResult.changes);
6569
warnings.push(...runResult.warnings);
6670
diffs.push(...runResult.diffs);

0 commit comments

Comments
 (0)