Skip to content

Commit 2fd16c2

Browse files
authored
fix: share managed Gateway startup readiness across setup (#130682)
* fix: wait for Gateway startup after configure daemon setup * fix: share managed Gateway startup timing across setup
1 parent 1d872fe commit 2fd16c2

13 files changed

Lines changed: 245 additions & 80 deletions

‎docs/cli/configure.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ When configure starts from a provider auth choice, the default-model and model-p
5656
- After local config writes, configure installs selected downloadable plugins when the chosen setup path requires them. Remote gateway config does not install local plugin packages.
5757
- Channel-oriented services (Slack/Discord/Matrix/Microsoft Teams) prompt for channel/room allowlists during setup. You can enter names or IDs; the wizard resolves names to IDs when possible.
5858
- Choosing **Reinstall** keeps the existing Gateway service in place while you select its runtime and configure validates authentication and prepares the replacement. Cancelling or failing during preparation leaves the existing service installed.
59+
- After successful daemon setup, the final Gateway status uses the same platform-specific startup grace period as onboarding before reporting reachability. Service installation and Gateway reachability are separate outcomes; if the Gateway is still not detected, run `openclaw health` to check it again.
5960
- If you run the daemon install step, token auth requires a token. If `gateway.auth.token` is SecretRef-managed, configure validates the SecretRef but does not persist resolved plaintext token values into supervisor service environment metadata; if the SecretRef is unresolved, configure blocks daemon install with actionable remediation guidance.
6061
- If both `gateway.auth.token` and `gateway.auth.password` are configured and `gateway.auth.mode` is unset, configure blocks daemon install until you set the mode explicitly.
6162

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

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
44
import type { OpenClawConfig } from "../config/config.js";
55
import { ExitError, type RuntimeEnv } from "../runtime.js";
66
import { withEnvAsync } from "../test-utils/env.js";
7+
import { withMockedPlatform } from "../test-utils/vitest-spies.js";
78
import { WizardCancelledError } from "../wizard/prompts.js";
89
import {
910
createWizardTestRuntime as createRuntime,
@@ -133,6 +134,96 @@ describe("runConfigureWizard", () => {
133134
expect(mocks.clackText).not.toHaveBeenCalled();
134135
});
135136

137+
it.each([
138+
{ outcome: "succeeded", reachable: true, completion: "Daemon setup completed." },
139+
{ outcome: "succeeded", reachable: false, completion: "Daemon setup completed." },
140+
{
141+
outcome: "failed",
142+
reachable: false,
143+
completion: "Configuration unchanged, but daemon setup failed.",
144+
},
145+
{ outcome: "skipped", reachable: false, completion: "Daemon setup skipped." },
146+
] as const)(
147+
"reports startup reachability after daemon $outcome ($reachable)",
148+
async ({ outcome, reachable, completion }) => {
149+
setupBaseWizardState({ gateway: { mode: "local" } });
150+
queueWizardPrompts({ select: ["local"], confirm: [] });
151+
maybeInstallDaemon.mockResolvedValueOnce(outcome);
152+
mocks.waitForGatewayReachable.mockResolvedValueOnce({ ok: reachable });
153+
154+
await runConfigureWizard({ command: "configure", sections: ["daemon"] }, createRuntime());
155+
156+
expect(mocks.note).toHaveBeenCalledWith(
157+
expect.stringContaining(reachable ? "Gateway: reachable" : "Gateway: not detected"),
158+
"Control UI",
159+
);
160+
expect(mocks.clackOutro).toHaveBeenCalledWith(completion);
161+
if (outcome !== "succeeded") {
162+
expect(mocks.waitForGatewayReachable).not.toHaveBeenCalled();
163+
}
164+
},
165+
);
166+
167+
it.each([
168+
{ platform: "linux", deadlineMs: 45_000, probeTimeoutMs: 10_000 },
169+
{ platform: "darwin", deadlineMs: 45_000, probeTimeoutMs: 10_000 },
170+
{ platform: "win32", deadlineMs: 90_000, probeTimeoutMs: 15_000 },
171+
] as const)(
172+
"allows managed daemon startup before health on $platform",
173+
async ({ platform, ...timing }) => {
174+
setupBaseWizardState({ gateway: { mode: "local" } });
175+
queueWizardPrompts({ select: ["local"], confirm: [] });
176+
maybeInstallDaemon.mockResolvedValueOnce("succeeded");
177+
mocks.waitForGatewayReachable.mockResolvedValue({ ok: true });
178+
179+
await withMockedPlatform(platform, () =>
180+
runConfigureWizard(
181+
{ command: "configure", sections: ["daemon", "health"] },
182+
createRuntime(),
183+
),
184+
);
185+
186+
expect(mocks.waitForGatewayReachable).toHaveBeenNthCalledWith(1, {
187+
url: "ws://127.0.0.1:18789",
188+
token: undefined,
189+
password: undefined,
190+
...timing,
191+
});
192+
expect(mocks.waitForGatewayReachable).toHaveBeenLastCalledWith({
193+
url: "ws://127.0.0.1:18789",
194+
token: undefined,
195+
password: undefined,
196+
...timing,
197+
});
198+
},
199+
);
200+
201+
it("observes fresh startup after an interactive health check followed by daemon setup", async () => {
202+
setupBaseWizardState({ gateway: { mode: "local" } });
203+
queueWizardPrompts({ select: ["local", "health", "daemon", "__continue"], confirm: [] });
204+
maybeInstallDaemon.mockResolvedValueOnce("succeeded");
205+
mocks.waitForGatewayReachable
206+
.mockResolvedValueOnce({ ok: true })
207+
.mockResolvedValueOnce({ ok: true });
208+
209+
await withMockedPlatform("linux", () =>
210+
runConfigureWizard({ command: "configure" }, createRuntime()),
211+
);
212+
213+
expect(mocks.note).toHaveBeenCalledWith(
214+
expect.stringContaining("Gateway: reachable"),
215+
"Control UI",
216+
);
217+
expect(mocks.waitForGatewayReachable).toHaveBeenCalledTimes(2);
218+
expect(mocks.waitForGatewayReachable).toHaveBeenNthCalledWith(
219+
1,
220+
expect.objectContaining({ deadlineMs: 15_000 }),
221+
);
222+
expect(mocks.waitForGatewayReachable).toHaveBeenLastCalledWith(
223+
expect.objectContaining({ deadlineMs: 45_000, probeTimeoutMs: 10_000 }),
224+
);
225+
});
226+
136227
it("keeps remote password health when the configured token ref is unresolved", async () => {
137228
const remotePassword = "remote-password"; // pragma: allowlist secret
138229
const remoteConfig: OpenClawConfig = {
@@ -324,12 +415,13 @@ describe("runConfigureWizard", () => {
324415
},
325416
});
326417
queueWizardPrompts({ select: ["local"], confirm: [] });
418+
maybeInstallDaemon.mockResolvedValueOnce("succeeded");
327419

328420
await withEnvAsync(
329421
{ OPENCLAW_GATEWAY_TOKEN: "ambient-token", WIZARD_GATEWAY_TOKEN: "configured-token" },
330422
() =>
331423
runConfigureWizard(
332-
{ command: "configure", sections: ["gateway", "health"] },
424+
{ command: "configure", sections: ["gateway", "daemon", "health"] },
333425
createRuntime(),
334426
),
335427
);
@@ -340,6 +432,13 @@ describe("runConfigureWizard", () => {
340432
expect(mocks.waitForGatewayReachable).toHaveBeenCalledWith(
341433
expect.objectContaining({ token: "configured-token" }),
342434
);
435+
expect(mocks.waitForGatewayReachable).toHaveBeenLastCalledWith(
436+
expect.objectContaining({
437+
url: "ws://127.0.0.1:18789",
438+
token: "configured-token",
439+
password: undefined,
440+
}),
441+
);
343442
expect(mocks.healthCommand).toHaveBeenCalledWith(
344443
expect.objectContaining({ token: "configured-token" }),
345444
expect.anything(),
@@ -468,6 +567,7 @@ describe("runConfigureWizard", () => {
468567
expect(mocks.probeGatewayReachable).toHaveBeenCalledWith(
469568
expect.objectContaining({ url: "ws://127.0.0.1:18789" }),
470569
);
570+
expect(mocks.waitForGatewayReachable).not.toHaveBeenCalled();
471571
expect(mocks.note).toHaveBeenCalledWith(
472572
expect.stringContaining("Web UI: http://10.211.55.3:18789/"),
473573
"Control UI",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ describe("configure wizard persistence before local side effects", () => {
106106
},
107107
});
108108
mocks.probeGatewayReachable.mockResolvedValue({ ok: false });
109+
mocks.waitForGatewayReachable.mockResolvedValue({ ok: false });
109110
mocks.text.mockResolvedValue("18789");
110111
});
111112

‎src/commands/configure.wizard.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
select,
4242
text,
4343
} from "./configure.shared.js";
44+
import { resolveGatewayStartupTiming } from "./gateway-startup-timing.js";
4445
import { formatHealthCheckFailure } from "./health-format.js";
4546
import { healthCommandNonExiting } from "./health.js";
4647
import {
@@ -88,6 +89,7 @@ async function runGatewayHealthCheck(params: {
8889
cfg: OpenClawConfig;
8990
runtime: RuntimeEnv;
9091
port: number;
92+
daemonSetupOutcome?: DaemonSetupOutcome;
9193
}): Promise<GatewayHealthCheckOutcome> {
9294
const localLinks = resolveLocalControlUiProbeLinks({
9395
bind: params.cfg.gateway?.bind ?? "loopback",
@@ -161,7 +163,9 @@ async function runGatewayHealthCheck(params: {
161163
url: wsUrl,
162164
token,
163165
password,
164-
deadlineMs: 15_000,
166+
...(params.daemonSetupOutcome === "succeeded"
167+
? resolveGatewayStartupTiming()
168+
: { deadlineMs: 15_000 }),
165169
});
166170
if (!gatewayProbe.ok) {
167171
throw new Error(gatewayProbe.detail ?? `gateway did not become reachable at ${wsUrl}`);
@@ -790,6 +794,7 @@ export async function runConfigureWizard(
790794
cfg: nextConfig,
791795
runtime,
792796
port: gatewayPort,
797+
daemonSetupOutcome,
793798
});
794799
},
795800
} satisfies Record<WizardSection, () => Promise<void>>;
@@ -905,9 +910,14 @@ export async function runConfigureWizard(
905910
mode: "local",
906911
localPrecedence: "env-first",
907912
});
913+
// Service activation can precede the listener; only a successful daemon action
914+
// earns the startup grace period, not failed/skipped or config-only work.
915+
const probe =
916+
daemonSetupOutcome === "succeeded" ? waitForGatewayReachable : probeGatewayReachable;
908917
let gatewayProbe = probeAuth.warning
909918
? { ok: false, detail: "auth unavailable; probe skipped" }
910-
: await probeGatewayReachable({
919+
: await probe({
920+
...(daemonSetupOutcome === "succeeded" ? resolveGatewayStartupTiming() : {}),
911921
url: probeLinks.wsUrl,
912922
token: probeAuth.auth.token,
913923
password: probeAuth.auth.password,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Service activation precedes cold-start loading and the authenticated handshake.
2+
// All managed setup observers share this platform-specific startup allowance.
3+
export function resolveGatewayStartupTiming() {
4+
const windows = process.platform === "win32";
5+
return {
6+
deadlineMs: windows ? 90_000 : 45_000,
7+
probeTimeoutMs: windows ? 15_000 : 10_000,
8+
};
9+
}

‎src/commands/onboard-non-interactive.gateway.test-mocks.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,10 @@ vi.mock("../daemon/diagnostics.js", () => ({
196196
}));
197197

198198
export let runNonInteractiveSetup: typeof import("./onboard-non-interactive.js").runNonInteractiveSetup;
199-
export let resolveInstallDaemonGatewayHealthTiming: typeof import("./onboard-non-interactive/local.test-support.js").resolveInstallDaemonGatewayHealthTiming;
200199

201200
export async function loadGatewayOnboardModules(): Promise<void> {
202201
vi.resetModules();
203202
({ runNonInteractiveSetup } = await import("./onboard-non-interactive.js"));
204-
({ resolveInstallDaemonGatewayHealthTiming } =
205-
await import("./onboard-non-interactive/local.test-support.js"));
206203
}
207204

208205
export const getPseudoPort = (base: number): number => base + (process.pid % 1000);

‎src/commands/onboard-non-interactive.gateway.test.ts‎

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
77
import type { RuntimeEnv } from "../runtime.js";
88
import { makeTempWorkspace } from "../test-helpers/workspace.js";
99
import { setTestEnvValue, withEnv, withEnvAsync } from "../test-utils/env.js";
10+
import { withMockedPlatform } from "../test-utils/vitest-spies.js";
1011
import {
1112
capturedReplaceConfigFileCalls,
1213
configWritePluginLeaseDepths,
@@ -20,7 +21,6 @@ import {
2021
gatewayOnboardConfigSnapshotMock as readConfigFileSnapshotMock,
2122
readLastGatewayErrorLineMock,
2223
readTestConfig,
23-
resolveInstallDaemonGatewayHealthTiming,
2424
resolveTestConfigPath,
2525
runNonInteractiveSetup,
2626
gatewayOnboardRuntime as runtime,
@@ -717,24 +717,37 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
717717
});
718718
}, 60_000);
719719

720-
it("uses a longer health deadline when daemon install was requested", async () => {
721-
await withStateDir("state-local-daemon-health-", async (stateDir) => {
722-
const captured = createOnboardGatewayTimeoutCapture();
723-
gatewayReachableState.mock = captured.mock;
724-
725-
await runOnboardLocalDaemonSetup({ runSetup: runNonInteractiveSetup, stateDir, runtime });
720+
it.each([
721+
{ platform: "linux", deadlineMs: 45_000, probeTimeoutMs: 10_000, healthTimeoutMs: 10_000 },
722+
{ platform: "win32", deadlineMs: 90_000, probeTimeoutMs: 15_000, healthTimeoutMs: 90_000 },
723+
] as const)(
724+
"uses managed daemon health timing on $platform",
725+
async ({ platform, deadlineMs, probeTimeoutMs, healthTimeoutMs }) => {
726+
await withStateDir("state-local-daemon-health-", async (stateDir) => {
727+
const captured = createOnboardGatewayTimeoutCapture();
728+
gatewayReachableState.mock = captured.mock;
729+
730+
await withMockedPlatform(platform, () =>
731+
runOnboardLocalDaemonSetup({ runSetup: runNonInteractiveSetup, stateDir, runtime }),
732+
);
726733

727-
const cfg = readTestConfig() as {
728-
gateway?: { mode?: string; bind?: string };
729-
};
734+
const cfg = readTestConfig() as {
735+
gateway?: { mode?: string; bind?: string };
736+
};
730737

731-
expect(cfg?.gateway?.mode).toBe("local");
732-
expect(cfg?.gateway?.bind).toBe("loopback");
733-
expect(installGatewayDaemonNonInteractiveMock).toHaveBeenCalledTimes(1);
734-
expect(captured.deadlineMs).toBe(45_000);
735-
expect(captured.probeTimeoutMs).toBe(10_000);
736-
});
737-
}, 60_000);
738+
expect(cfg?.gateway?.mode).toBe("local");
739+
expect(cfg?.gateway?.bind).toBe("loopback");
740+
expect(installGatewayDaemonNonInteractiveMock).toHaveBeenCalledTimes(1);
741+
expect(captured.deadlineMs).toBe(deadlineMs);
742+
expect(captured.probeTimeoutMs).toBe(probeTimeoutMs);
743+
expect(healthCommandMock).toHaveBeenCalledWith(
744+
expect.objectContaining({ timeoutMs: healthTimeoutMs }),
745+
expect.anything(),
746+
);
747+
});
748+
},
749+
60_000,
750+
);
738751

739752
it("passes pinned gateway auth through non-interactive health checks", async () => {
740753
await withStateDir("state-local-daemon-health-auth-", async (stateDir) => {
@@ -768,14 +781,6 @@ describe("onboard (non-interactive): gateway and remote auth", () => {
768781
});
769782
}, 60_000);
770783

771-
it("uses longer Windows health timings for daemon install probes", () => {
772-
expect(resolveInstallDaemonGatewayHealthTiming("win32")).toEqual({
773-
deadlineMs: 90_000,
774-
probeTimeoutMs: 15_000,
775-
healthCommandTimeoutMs: 90_000,
776-
});
777-
});
778-
779784
it.each([false, true])(
780785
"emits a daemon-install failure when Linux user systemd is unavailable (skipHealth: %s)",
781786
async (skipHealth) => {

‎src/commands/onboard-non-interactive/local.test-support.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ type GatewayHealthProbeAuth = {
99

1010
type TestApi = {
1111
resolveGatewayHealthProbeToken(nextConfig: OpenClawConfig): Promise<GatewayHealthProbeAuth>;
12-
resolveInstallDaemonGatewayHealthTiming(platform?: NodeJS.Platform): {
13-
deadlineMs: number;
14-
probeTimeoutMs: number;
15-
healthCommandTimeoutMs: number;
16-
};
1712
};
1813

1914
function getTestApi(): TestApi {
@@ -25,6 +20,3 @@ function getTestApi(): TestApi {
2520
export const resolveGatewayHealthProbeToken: TestApi["resolveGatewayHealthProbeToken"] = (
2621
nextConfig,
2722
) => getTestApi().resolveGatewayHealthProbeToken(nextConfig);
28-
29-
export const resolveInstallDaemonGatewayHealthTiming: TestApi["resolveInstallDaemonGatewayHealthTiming"] =
30-
(platform) => getTestApi().resolveInstallDaemonGatewayHealthTiming(platform);

0 commit comments

Comments
 (0)