Skip to content

Commit 17baa65

Browse files
committed
fix: share managed Gateway startup timing across setup
1 parent 03bc0ea commit 17baa65

12 files changed

Lines changed: 190 additions & 85 deletions

‎docs/cli/configure.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +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 allows a short startup grace period 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.
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.
6060
- 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.
6161
- 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.
6262

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

Lines changed: 52 additions & 6 deletions
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,
@@ -163,6 +164,40 @@ describe("runConfigureWizard", () => {
163164
},
164165
);
165166

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+
166201
it("observes fresh startup after an interactive health check followed by daemon setup", async () => {
167202
setupBaseWizardState({ gateway: { mode: "local" } });
168203
queueWizardPrompts({ select: ["local", "health", "daemon", "__continue"], confirm: [] });
@@ -171,13 +206,22 @@ describe("runConfigureWizard", () => {
171206
.mockResolvedValueOnce({ ok: true })
172207
.mockResolvedValueOnce({ ok: true });
173208

174-
await runConfigureWizard({ command: "configure" }, createRuntime());
209+
await withMockedPlatform("linux", () =>
210+
runConfigureWizard({ command: "configure" }, createRuntime()),
211+
);
175212

176213
expect(mocks.note).toHaveBeenCalledWith(
177214
expect.stringContaining("Gateway: reachable"),
178215
"Control UI",
179216
);
180217
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+
);
181225
});
182226

183227
it("keeps remote password health when the configured token ref is unresolved", async () => {
@@ -388,11 +432,13 @@ describe("runConfigureWizard", () => {
388432
expect(mocks.waitForGatewayReachable).toHaveBeenCalledWith(
389433
expect.objectContaining({ token: "configured-token" }),
390434
);
391-
expect(mocks.waitForGatewayReachable).toHaveBeenLastCalledWith({
392-
url: "ws://127.0.0.1:18789",
393-
token: "configured-token",
394-
password: undefined,
395-
});
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+
);
396442
expect(mocks.healthCommand).toHaveBeenCalledWith(
397443
expect.objectContaining({ token: "configured-token" }),
398444
expect.anything(),

‎src/commands/configure.wizard.ts‎

Lines changed: 7 additions & 1 deletion
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>>;
@@ -912,6 +917,7 @@ export async function runConfigureWizard(
912917
let gatewayProbe = probeAuth.warning
913918
? { ok: false, detail: "auth unavailable; probe skipped" }
914919
: await probe({
920+
...(daemonSetupOutcome === "succeeded" ? resolveGatewayStartupTiming() : {}),
915921
url: probeLinks.wsUrl,
916922
token: probeAuth.auth.token,
917923
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);

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

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { resolveConfiguredSecretInputWithFallback } from "../../gateway/resolve-
1414
import { formatErrorMessage } from "../../infra/errors.js";
1515
import { ExitError, type RuntimeEnv } from "../../runtime.js";
1616
import { DEFAULT_GATEWAY_DAEMON_RUNTIME } from "../daemon-runtime.js";
17+
import { resolveGatewayStartupTiming } from "../gateway-startup-timing.js";
1718
import {
1819
ensureOnboardingAgentWorkspace,
1920
resolveOnboardingAgentTarget,
@@ -44,34 +45,6 @@ import {
4445
import { applyNonInteractiveSkillsConfig } from "./local/skills-config.js";
4546
import { resolveNonInteractiveWorkspaceDir } from "./local/workspace.js";
4647

47-
const INSTALL_DAEMON_HEALTH_DEADLINE_MS = 45_000;
48-
const ATTACH_EXISTING_GATEWAY_HEALTH_DEADLINE_MS = 15_000;
49-
const INSTALL_DAEMON_HEALTH_PROBE_TIMEOUT_MS = 10_000;
50-
const WINDOWS_INSTALL_DAEMON_HEALTH_DEADLINE_MS = 90_000;
51-
const WINDOWS_INSTALL_DAEMON_HEALTH_PROBE_TIMEOUT_MS = 15_000;
52-
const INSTALL_DAEMON_HEALTH_COMMAND_TIMEOUT_MS = 10_000;
53-
const WINDOWS_INSTALL_DAEMON_HEALTH_COMMAND_TIMEOUT_MS = 90_000;
54-
55-
/** Returns platform-specific health timing for managed daemon installs. */
56-
function resolveInstallDaemonGatewayHealthTiming(platform: NodeJS.Platform = process.platform): {
57-
deadlineMs: number;
58-
probeTimeoutMs: number;
59-
healthCommandTimeoutMs: number;
60-
} {
61-
if (platform === "win32") {
62-
return {
63-
deadlineMs: WINDOWS_INSTALL_DAEMON_HEALTH_DEADLINE_MS,
64-
probeTimeoutMs: WINDOWS_INSTALL_DAEMON_HEALTH_PROBE_TIMEOUT_MS,
65-
healthCommandTimeoutMs: WINDOWS_INSTALL_DAEMON_HEALTH_COMMAND_TIMEOUT_MS,
66-
};
67-
}
68-
return {
69-
deadlineMs: INSTALL_DAEMON_HEALTH_DEADLINE_MS,
70-
probeTimeoutMs: INSTALL_DAEMON_HEALTH_PROBE_TIMEOUT_MS,
71-
healthCommandTimeoutMs: INSTALL_DAEMON_HEALTH_COMMAND_TIMEOUT_MS,
72-
};
73-
}
74-
7548
async function collectGatewayHealthFailureDiagnostics(): Promise<
7649
GatewayHealthFailureDiagnostics | undefined
7750
> {
@@ -158,7 +131,6 @@ if (process.env.VITEST || process.env.NODE_ENV === "test") {
158131
Symbol.for("openclaw.onboardNonInteractiveLocalTestApi")
159132
] = {
160133
resolveGatewayHealthProbeToken,
161-
resolveInstallDaemonGatewayHealthTiming,
162134
};
163135
}
164136

@@ -370,18 +342,15 @@ export async function runNonInteractiveLocalSetup(params: {
370342
basePath: undefined,
371343
tlsEnabled: nextConfig.gateway?.tls?.enabled === true,
372344
});
373-
const installDaemonGatewayHealthTiming = resolveInstallDaemonGatewayHealthTiming();
345+
const startupTiming = opts.installDaemon
346+
? resolveGatewayStartupTiming()
347+
: { deadlineMs: 15_000 };
374348
const probeAuth = await resolveGatewayHealthProbeToken(nextConfig);
375349
const probe = await waitForGatewayReachable({
376350
url: links.wsUrl,
377351
token: probeAuth.token,
378352
password: probeAuth.password,
379-
deadlineMs: opts.installDaemon
380-
? installDaemonGatewayHealthTiming.deadlineMs
381-
: ATTACH_EXISTING_GATEWAY_HEALTH_DEADLINE_MS,
382-
probeTimeoutMs: opts.installDaemon
383-
? installDaemonGatewayHealthTiming.probeTimeoutMs
384-
: undefined,
353+
...startupTiming,
385354
});
386355
if (!probe.ok) {
387356
// Non-daemon setup attaches to an existing gateway, so collect expensive
@@ -450,9 +419,7 @@ export async function runNonInteractiveLocalSetup(params: {
450419
await healthCommandNonExiting(
451420
{
452421
json: false,
453-
timeoutMs: opts.installDaemon
454-
? installDaemonGatewayHealthTiming.healthCommandTimeoutMs
455-
: 10_000,
422+
timeoutMs: opts.installDaemon && process.platform === "win32" ? 90_000 : 10_000,
456423
config: nextConfig,
457424
token: probeAuth.token,
458425
password: probeAuth.password,

‎src/system-agent/setup-apply.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
1818
import * as configModule from "../config/config.js";
1919
import type { OpenClawConfig } from "../config/types.openclaw.js";
2020
import { withEnvAsync } from "../test-utils/env.js";
21+
import { withMockedPlatform } from "../test-utils/vitest-spies.js";
2122
import { projectDefaultInferenceRoute } from "./inference-route.js";
2223
import { applySystemAgentSetup } from "./setup-apply.js";
2324

@@ -956,6 +957,34 @@ describe("applySystemAgentSetup transaction boundaries", () => {
956957
expect(mocks.waitForGatewayReachable).not.toHaveBeenCalled();
957958
});
958959

960+
it.each(
961+
(["linux", "win32"] as const).flatMap((platform) =>
962+
(["installed", "started", "restarted", "restart-scheduled", "reused"] as const).map(
963+
(action) => ({ platform, action }),
964+
),
965+
),
966+
)("uses the $platform readiness budget after service $action", async ({ platform, action }) => {
967+
await withMockedPlatform(platform, async () => {
968+
const gateway = { status: "ready", action } as const;
969+
mocks.ensureGatewayService.mockResolvedValueOnce({ gateway });
970+
971+
const result = await applySystemAgentSetup(baseParams({ surface: "cli" }));
972+
973+
expect(result.gateway).toEqual(gateway);
974+
expect(mocks.waitForGatewayReachable).toHaveBeenCalledOnce();
975+
expect(mocks.waitForGatewayReachable).toHaveBeenCalledWith(
976+
expect.objectContaining(
977+
action === "reused"
978+
? { deadlineMs: 15_000 }
979+
: {
980+
deadlineMs: platform === "win32" ? 90_000 : 45_000,
981+
probeTimeoutMs: platform === "win32" ? 15_000 : 10_000,
982+
},
983+
),
984+
);
985+
});
986+
});
987+
959988
it("keeps setup incomplete when the installed gateway never becomes reachable", async () => {
960989
mocks.ensureGatewayService.mockResolvedValueOnce({
961990
gateway: { status: "ready", action: "installed" },

0 commit comments

Comments
 (0)