Skip to content

Commit 28ec823

Browse files
authored
fix(wizard): health-check failures no longer kill the configure/onboard/doctor flow (#126758)
* fix(wizard): keep embedded health-check failures visible instead of exiting mid-flow healthCommand's reachable-gateway auth diagnostic paths call runtime.exit(1), which with defaultRuntime hard-kills the hosting configure wizard, onboarding finalize, or doctor daemon flow mid-render — dropping the failure framing, docs guidance, and outro. Add healthCommandNonExiting, which traps that CLI-style exit into ExitError so the host flow owns the outcome, and use it at every embedded call site. Also fix the doctor e2e harness createConfigIO mock missing configPath, which broke doctor.runs-legacy-state-migrations on current main. * fix(onboard): reflect a failed health check in the finalize outro A reachable gateway whose health check failed still ended onboarding with the plain success outro because completion gating only read the earlier reachability probe. Record the health outcome as its own fact and end with a dedicated outro pointing at openclaw health. (ClawSweeper P1 on #126758.)
1 parent c6c598c commit 28ec823

15 files changed

Lines changed: 168 additions & 46 deletions

‎src/commands/configure.wizard.gateway.test.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures";
33
import { beforeEach, describe, expect, it, vi } from "vitest";
44
import type { OpenClawConfig } from "../config/config.js";
5-
import type { RuntimeEnv } from "../runtime.js";
5+
import { ExitError, type RuntimeEnv } from "../runtime.js";
66
import { withEnvAsync } from "../test-utils/env.js";
77
import {
88
createWizardTestRuntime,
@@ -166,7 +166,7 @@ vi.mock("./onboard-helpers.js", () => ({
166166
}));
167167

168168
vi.mock("./health.js", () => ({
169-
healthCommand: mocks.healthCommand,
169+
healthCommandNonExiting: mocks.healthCommand,
170170
}));
171171

172172
vi.mock("./health-format.js", () => ({
@@ -225,6 +225,7 @@ vi.mock("../config/mutate.js", async () => {
225225
import { WizardCancelledError } from "../wizard/prompts.js";
226226
import { maybeInstallDaemon } from "./configure.daemon.js";
227227
import { runConfigureWizard } from "./configure.wizard.js";
228+
import { formatHealthCheckFailure } from "./health-format.js";
228229

229230
const createRuntime = createWizardTestRuntime;
230231

@@ -411,15 +412,23 @@ describe("runConfigureWizard", () => {
411412
);
412413
});
413414

414-
it.each([false, true])("reports failed remote health checks (reachable: %s)", async (probeOk) => {
415+
it.each([
416+
["unreachable gateway", false, new Error("health request failed")],
417+
["health request failure", true, new Error("health request failed")],
418+
["trapped health CLI exit", true, new ExitError(1)],
419+
])("reports failed remote health checks (%s)", async (_reason, probeOk, error) => {
415420
setupBaseWizardState();
416421
queueWizardPrompts({ select: ["remote"], confirm: [] });
417422
mocks.waitForGatewayReachable.mockResolvedValueOnce({ ok: probeOk });
418-
mocks.healthCommand.mockRejectedValueOnce(new Error("health request failed"));
423+
mocks.healthCommand.mockRejectedValueOnce(error);
419424

420425
await runConfigureWizard({ command: "configure", sections: ["health"] }, createRuntime());
421426

422427
expect(mocks.clackOutro).toHaveBeenCalledWith(expect.stringContaining("health check failed"));
428+
if (error instanceof ExitError) {
429+
// healthCommand already printed its diagnostic before the trapped exit.
430+
expect(formatHealthCheckFailure).not.toHaveBeenCalled();
431+
}
423432
});
424433

425434
it("skips remote health when a configured SecretRef is unresolved", async () => {

‎src/commands/configure.wizard.persistence.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ vi.mock("./configure.channels.js", () => ({ removeChannelConfigWizard: vi.fn() }
7777
vi.mock("./configure.daemon.js", () => ({ maybeInstallDaemon: mocks.maybeInstallDaemon }));
7878
vi.mock("./configure.gateway-auth.js", () => ({ promptAuthConfig: vi.fn() }));
7979
vi.mock("./configure.gateway.js", () => ({ promptGatewayConfig: vi.fn() }));
80-
vi.mock("./health.js", () => ({ healthCommand: mocks.healthCommand }));
80+
vi.mock("./health.js", () => ({ healthCommandNonExiting: mocks.healthCommand }));
8181
vi.mock("./onboard-channels.js", () => ({ setupChannels: vi.fn() }));
8282
vi.mock("./onboard-remote.js", () => ({ promptRemoteGatewayConfig: vi.fn() }));
8383
vi.mock("./onboard-skills.js", () => ({ setupSkills: vi.fn() }));

‎src/commands/configure.wizard.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ vi.mock("./onboard-helpers.js", () => ({
168168
}));
169169

170170
vi.mock("./health.js", () => ({
171-
healthCommand: mocks.healthCommand,
171+
healthCommandNonExiting: mocks.healthCommand,
172172
}));
173173

174174
vi.mock("./health-format.js", () => ({

‎src/commands/configure.wizard.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { commitConfigWithPendingPluginInstalls } from "../plugins/install-record
1818
import { resolvePluginContributionOwners } from "../plugins/plugin-registry.js";
1919
import { normalizeAgentId } from "../routing/session-key.js";
2020
import type { RuntimeEnv } from "../runtime.js";
21-
import { defaultRuntime } from "../runtime.js";
21+
import { defaultRuntime, ExitError } from "../runtime.js";
2222
import { createLazyImportLoader } from "../shared/lazy-promise.js";
2323
import { resolveUserPath } from "../utils.js";
2424
import { createClackPrompter } from "../wizard/clack-prompter.js";
@@ -43,7 +43,7 @@ import {
4343
text,
4444
} from "./configure.shared.js";
4545
import { formatHealthCheckFailure } from "./health-format.js";
46-
import { healthCommand } from "./health.js";
46+
import { healthCommandNonExiting } from "./health.js";
4747
import {
4848
ensureOnboardingAgentWorkspace,
4949
resolveOnboardingAgentTarget,
@@ -179,7 +179,7 @@ async function runGatewayHealthCheck(params: {
179179
if (!gatewayProbe.ok) {
180180
throw new Error(gatewayProbe.detail ?? `gateway did not become reachable at ${wsUrl}`);
181181
}
182-
await healthCommand(
182+
await healthCommandNonExiting(
183183
{
184184
json: false,
185185
timeoutMs: 10_000,
@@ -193,7 +193,11 @@ async function runGatewayHealthCheck(params: {
193193
params.runtime,
194194
);
195195
} catch (err) {
196-
params.runtime.error(formatHealthCheckFailure(err));
196+
// A trapped ExitError means healthCommand already printed its own
197+
// reachable-gateway diagnostic; re-formatting it would only add noise.
198+
if (!(err instanceof ExitError)) {
199+
params.runtime.error(formatHealthCheckFailure(err));
200+
}
197201
note(
198202
[
199203
"Docs:",

‎src/commands/doctor-gateway-daemon-flow.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ vi.mock("./health-format.js", () => ({
152152
}));
153153

154154
vi.mock("./health.js", () => ({
155-
healthCommand,
155+
healthCommandNonExiting: healthCommand,
156156
}));
157157

158158
describe("maybeRepairGatewayDaemon", () => {

‎src/commands/doctor-gateway-daemon-flow.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
readGatewayRestartHandoffSync,
3535
} from "../infra/restart-handoff.js";
3636
import { isWSL } from "../infra/wsl.js";
37-
import type { RuntimeEnv } from "../runtime.js";
37+
import { ExitError, type RuntimeEnv } from "../runtime.js";
3838
import { sleep } from "../utils.js";
3939
import { buildGatewayInstallPlan, gatewayInstallErrorHint } from "./daemon-install-helpers.js";
4040
import {
@@ -53,7 +53,7 @@ import {
5353
} from "./doctor-service-repair-policy.js";
5454
import { resolveGatewayInstallToken } from "./gateway-install-token.js";
5555
import { formatGatewayClosedDiagnostic, formatHealthCheckFailure } from "./health-format.js";
56-
import { healthCommand } from "./health.js";
56+
import { healthCommandNonExiting } from "./health.js";
5757

5858
type LaunchAgentBootstrapDoctorOutcome =
5959
| { status: "skipped" }
@@ -491,7 +491,7 @@ export async function maybeRepairGatewayDaemon(params: {
491491
const recentRestart = readGatewayRestartHandoffSync(serviceEnv);
492492
if (recentRestart) {
493493
try {
494-
await healthCommand({ json: false, timeoutMs: 10_000 }, params.runtime);
494+
await healthCommandNonExiting({ json: false, timeoutMs: 10_000 }, params.runtime);
495495
note("Gateway is healthy after recent restart; skipping restart prompt.", "Gateway");
496496
return;
497497
} catch {
@@ -523,8 +523,13 @@ export async function maybeRepairGatewayDaemon(params: {
523523
}
524524
await sleep(1500);
525525
try {
526-
await healthCommand({ json: false, timeoutMs: 10_000 }, params.runtime);
526+
await healthCommandNonExiting({ json: false, timeoutMs: 10_000 }, params.runtime);
527527
} catch (err) {
528+
// A trapped ExitError means healthCommand already printed its own
529+
// reachable-gateway diagnostic; re-formatting it would only add noise.
530+
if (err instanceof ExitError) {
531+
return;
532+
}
528533
const closedDiagnostic = formatGatewayClosedDiagnostic(err);
529534
if (closedDiagnostic) {
530535
note(closedDiagnostic, "Gateway");

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const legacyReadConfigFileSnapshot = defineMockFn(
6969
);
7070
const createConfigIO = defineMockFn(
7171
vi.fn(() => ({
72+
configPath: "/tmp/openclaw.json",
7273
readConfigFileSnapshot: legacyReadConfigFileSnapshot,
7374
})),
7475
);
@@ -546,15 +547,19 @@ vi.mock("../pairing/pairing-store.js", () => ({
546547
upsertChannelPairingRequest: vi.fn().mockResolvedValue({ code: "000000", created: false }),
547548
}));
548549

549-
vi.mock("../runtime.js", () => ({
550-
defaultRuntime: {
551-
log: () => {},
552-
error: () => {},
553-
exit: () => {
554-
throw new Error("exit");
550+
vi.mock("../runtime.js", async () => {
551+
const actual = await vi.importActual<typeof import("../runtime.js")>("../runtime.js");
552+
return {
553+
ExitError: actual.ExitError,
554+
defaultRuntime: {
555+
log: () => {},
556+
error: () => {},
557+
exit: () => {
558+
throw new Error("exit");
559+
},
555560
},
556-
},
557-
}));
561+
};
562+
});
558563

559564
vi.mock("../utils.js", async () => {
560565
const actual = await vi.importActual<typeof import("../utils.js")>("../utils.js");
@@ -567,6 +572,7 @@ vi.mock("../utils.js", async () => {
567572

568573
vi.mock("./health.js", () => ({
569574
healthCommand: vi.fn().mockResolvedValue(undefined),
575+
healthCommandNonExiting: vi.fn().mockResolvedValue(undefined),
570576
}));
571577

572578
vi.mock("./onboard-helpers.js", () => ({
@@ -630,6 +636,7 @@ beforeEach(() => {
630636
noteMemoryRecallHealth.mockReset().mockResolvedValue(undefined);
631637
legacyReadConfigFileSnapshot.mockReset().mockResolvedValue(createLegacyConfigSnapshot());
632638
createConfigIO.mockReset().mockImplementation(() => ({
639+
configPath: "/tmp/openclaw.json",
633640
readConfigFileSnapshot: legacyReadConfigFileSnapshot,
634641
}));
635642
runExec.mockReset().mockResolvedValue({ stdout: "", stderr: "" });

‎src/commands/health.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { beforeEach, describe, expect, it, vi } from "vitest";
33
import { GatewayClientRequestError } from "../../packages/gateway-client/src/index.js";
44
import { stripAnsi } from "../../packages/terminal-core/src/ansi.js";
5+
import { ExitError } from "../runtime.js";
56
import {
67
buildCredentialsRequiredHealthDiagnostic,
78
buildRateLimitedHealthDiagnostic,
@@ -16,6 +17,7 @@ import {
1617
formatContextEngineHealthLine,
1718
formatDeliveryQueueHealthLine,
1819
healthCommand,
20+
healthCommandNonExiting,
1921
} from "./health.js";
2022

2123
const runtime = {
@@ -715,6 +717,32 @@ describe("healthCommand", () => {
715717
]);
716718
expect(runtime.error).not.toHaveBeenCalled();
717719
});
720+
721+
it("throws ExitError from healthCommandNonExiting instead of exiting the host runtime", async () => {
722+
const error = new Error("gateway.auth.password is unavailable");
723+
callGatewayMock.mockRejectedValueOnce(error);
724+
isGatewaySecretRefUnavailableErrorMock.mockReturnValueOnce(true);
725+
probeGatewayStatusMock.mockResolvedValueOnce({
726+
ok: false,
727+
kind: "connect",
728+
error: TEST_AUTH_CLOSE_ERROR,
729+
});
730+
731+
await expect(
732+
healthCommandNonExiting(
733+
{ json: false, timeoutMs: 5000, config: {}, ignoreEnvUrlOverride: true },
734+
runtime as never,
735+
),
736+
).rejects.toBeInstanceOf(ExitError);
737+
738+
// The embedded wizard/doctor host keeps running: its own exit is never invoked
739+
// and the diagnostic was still printed through its log sink.
740+
expect(runtime.exit).not.toHaveBeenCalled();
741+
expect(runtime.log.mock.calls).toEqual([
742+
[GATEWAY_HEALTH_REACHABLE_LINE],
743+
[GATEWAY_HEALTH_CREDENTIALS_REQUIRED_MESSAGE],
744+
]);
745+
});
718746
});
719747

720748
describe("formatContextEngineHealthLine", () => {

‎src/commands/health.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
import { resolveHeartbeatSummaryForAgent } from "../infra/heartbeat-summary.js";
3535
import { createSubsystemLogger } from "../logging/subsystem.js";
3636
import { buildChannelAccountBindings, resolvePreferredAccountId } from "../routing/bindings.js";
37-
import { type RuntimeEnv, writeRuntimeJson } from "../runtime.js";
37+
import { ExitError, type RuntimeEnv, writeRuntimeJson } from "../runtime.js";
3838
import {
3939
buildCredentialsRequiredHealthDiagnostic,
4040
buildRateLimitedHealthDiagnostic,
@@ -563,6 +563,23 @@ export async function healthCommand(
563563
}
564564
}
565565

566+
/**
567+
* Runs `healthCommand` inside a host flow (wizard/onboard/doctor). The command's
568+
* CLI-style `runtime.exit(1)` diagnostic paths surface as a thrown `ExitError`,
569+
* so the host reports the failure and keeps running instead of dying mid-flow.
570+
*/
571+
export async function healthCommandNonExiting(
572+
opts: Parameters<typeof healthCommand>[0],
573+
runtime: RuntimeEnv,
574+
): Promise<void> {
575+
await healthCommand(opts, {
576+
...runtime,
577+
exit: (code) => {
578+
throw new ExitError(code);
579+
},
580+
});
581+
}
582+
566583
export async function readNonObservingHealthConfig(): Promise<OpenClawConfig> {
567584
const { readConfigFileSnapshot } = await loadConfigRuntime();
568585
const snapshot = await readConfigFileSnapshot({

‎src/wizard/i18n/locales/en.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,8 @@ export const en = {
11621162
outroDashboardLink: "Onboarding complete. Use the dashboard link above to control OpenClaw.",
11631163
outroDashboardOpened:
11641164
"Onboarding complete. Dashboard opened; keep that tab to control OpenClaw.",
1165+
outroHealthCheckFailed:
1166+
"Onboarding complete, but the gateway health check failed. Fix the issue above, then verify with {command}.",
11651167
outroSeeded:
11661168
"Onboarding complete. Web UI seeded in the background; open it anytime with the dashboard link above.",
11671169
quickstartNodeRuntime: "QuickStart uses Node for the Gateway service (stable + supported).",

0 commit comments

Comments
 (0)