Skip to content

Commit b5ba210

Browse files
authored
fix: gate dashboard on gateway readiness
1 parent 9dea44e commit b5ba210

8 files changed

Lines changed: 645 additions & 4 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Docs: https://docs.openclaw.ai
1717
### Fixes
1818

1919
- CLI/status: show plain empty-state messages instead of empty Channels and Sessions tables when no channels or sessions exist.
20+
- CLI/dashboard: probe Gateway readiness before handing out the dashboard URL, prompting to start or install the managed service when the Gateway is stopped and printing recovery commands instead of opening a dead browser tab.
2021
- CLI/context engines: bootstrap and finalize non-legacy context engines for CLI turns while preserving transcript snapshots and deferred maintenance ownership. (#81869) Thanks @sahilsatralkar.
2122
- Telegram: persist polling updates through restart replay so queued same-topic messages resume in order instead of losing context after a gateway restart. (#82256) Thanks @VACInc.
2223
- Gateway/Gmail: abort in-flight Gmail watcher startup and hot-reload restarts before shutdown so reloads cannot spawn `gog serve` after the Gateway is closing. Thanks @frankekn.

‎src/cli/program/register.maintenance.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ export function registerMaintenanceCommands(program: Command) {
4949
`\n${theme.muted("Docs:")} ${formatDocsLink("/cli/dashboard", "docs.openclaw.ai/cli/dashboard")}\n`,
5050
)
5151
.option("--no-open", "Print URL but do not launch a browser")
52+
.option("--yes", "Start/install the gateway without prompting when needed", false)
5253
.action(async (opts) => {
5354
await runCommandWithRuntime(defaultRuntime, async () => {
5455
await dashboardCommand(defaultRuntime, {
5556
noOpen: opts.open === false,
57+
yes: Boolean(opts.yes),
5658
});
5759
});
5860
});

‎src/cli/qr-dashboard.integration.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const loadConfigMock = vi.hoisted(() => vi.fn());
77
const readConfigFileSnapshotMock = vi.hoisted(() => vi.fn());
88
const resolveGatewayPortMock = vi.hoisted(() => vi.fn(() => 18789));
99
const copyToClipboardMock = vi.hoisted(() => vi.fn(async () => false));
10+
const ensureGatewayReadyForOperationMock = vi.hoisted(() => vi.fn());
1011
const {
1112
runtimeLogs,
1213
runtimeErrors,
@@ -30,6 +31,10 @@ vi.mock("../infra/clipboard.js", () => ({
3031
copyToClipboard: copyToClipboardMock,
3132
}));
3233

34+
vi.mock("../commands/gateway-readiness.js", () => ({
35+
ensureGatewayReadyForOperation: ensureGatewayReadyForOperationMock,
36+
}));
37+
3338
vi.mock("../infra/device-bootstrap.js", () => ({
3439
issueDeviceBootstrapToken: vi.fn(async () => ({
3540
token: "bootstrap-123",
@@ -120,6 +125,11 @@ describe("cli integration: qr + dashboard token SecretRef", () => {
120125
beforeEach(() => {
121126
resetRuntimeCapture();
122127
vi.clearAllMocks();
128+
ensureGatewayReadyForOperationMock.mockResolvedValue({
129+
ready: true,
130+
status: {},
131+
recovered: false,
132+
});
123133
runtimeExit.mockImplementation(() => {});
124134
delete process.env.OPENCLAW_GATEWAY_TOKEN;
125135
delete process.env.OPENCLAW_GATEWAY_PASSWORD;

‎src/commands/dashboard.links.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const openUrlMock = vi.hoisted(() => vi.fn());
99
const formatControlUiSshHintMock = vi.hoisted(() => vi.fn());
1010
const copyToClipboardMock = vi.hoisted(() => vi.fn());
1111
const resolveSecretRefValuesMock = vi.hoisted(() => vi.fn());
12+
const ensureGatewayReadyForOperationMock = vi.hoisted(() => vi.fn());
1213

1314
vi.mock("../config/config.js", () => ({
1415
readConfigFileSnapshot: readConfigFileSnapshotMock,
@@ -26,6 +27,10 @@ vi.mock("../infra/clipboard.js", () => ({
2627
copyToClipboard: copyToClipboardMock,
2728
}));
2829

30+
vi.mock("./gateway-readiness.js", () => ({
31+
ensureGatewayReadyForOperation: ensureGatewayReadyForOperationMock,
32+
}));
33+
2934
vi.mock("../secrets/resolve.js", () => ({
3035
resolveSecretRefValues: resolveSecretRefValuesMock,
3136
}));
@@ -83,6 +88,12 @@ describe("dashboardCommand", () => {
8388
openUrlMock.mockClear();
8489
formatControlUiSshHintMock.mockClear();
8590
copyToClipboardMock.mockClear();
91+
ensureGatewayReadyForOperationMock.mockReset();
92+
ensureGatewayReadyForOperationMock.mockResolvedValue({
93+
ready: true,
94+
status: {},
95+
recovered: false,
96+
});
8697
delete process.env.OPENCLAW_GATEWAY_TOKEN;
8798
delete process.env.CUSTOM_GATEWAY_TOKEN;
8899
});
@@ -95,6 +106,13 @@ describe("dashboardCommand", () => {
95106

96107
await dashboardCommand(runtime);
97108

109+
expect(ensureGatewayReadyForOperationMock).toHaveBeenCalledWith({
110+
runtime,
111+
operation: "open the dashboard",
112+
yes: undefined,
113+
probeUrl: "ws://127.0.0.1:18789",
114+
readyWhenReachable: true,
115+
});
98116
expect(resolveControlUiLinksMock).toHaveBeenCalledWith({
99117
port: 18789,
100118
bind: "loopback",
@@ -291,4 +309,20 @@ describe("dashboardCommand", () => {
291309
);
292310
expectNoLogWith("Token auto-auth is disabled for SecretRef-managed");
293311
});
312+
313+
it("does not copy or open when gateway readiness fails", async () => {
314+
mockSnapshot("abc");
315+
ensureGatewayReadyForOperationMock.mockResolvedValueOnce({
316+
ready: false,
317+
status: {},
318+
reason: "Gateway is not running.",
319+
recoverable: true,
320+
});
321+
322+
await dashboardCommand(runtime);
323+
324+
expect(readConfigFileSnapshotMock).toHaveBeenCalledTimes(1);
325+
expect(copyToClipboardMock).not.toHaveBeenCalled();
326+
expect(openUrlMock).not.toHaveBeenCalled();
327+
});
294328
});

‎src/commands/dashboard.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const mocks = vi.hoisted(() => ({
77
resolveGatewayPort: vi.fn(),
88
resolveControlUiLinks: vi.fn(),
99
copyToClipboard: vi.fn(),
10+
ensureGatewayReadyForOperation: vi.fn(),
1011
}));
1112

1213
vi.mock("../config/config.js", () => ({
@@ -25,6 +26,10 @@ vi.mock("../infra/clipboard.js", () => ({
2526
copyToClipboard: mocks.copyToClipboard,
2627
}));
2728

29+
vi.mock("./gateway-readiness.js", () => ({
30+
ensureGatewayReadyForOperation: mocks.ensureGatewayReadyForOperation,
31+
}));
32+
2833
const runtime = {
2934
log: vi.fn(),
3035
error: vi.fn(),
@@ -67,6 +72,12 @@ describe("dashboardCommand bind selection", () => {
6772
mocks.resolveGatewayPort.mockClear();
6873
mocks.resolveControlUiLinks.mockClear();
6974
mocks.copyToClipboard.mockClear();
75+
mocks.ensureGatewayReadyForOperation.mockReset();
76+
mocks.ensureGatewayReadyForOperation.mockResolvedValue({
77+
ready: true,
78+
status: {},
79+
recovered: false,
80+
});
7081
runtime.log.mockClear();
7182
runtime.error.mockClear();
7283
runtime.exit.mockClear();

‎src/commands/dashboard.ts‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { resolveGatewayAuthToken } from "../gateway/auth-token-resolution.js";
33
import { copyToClipboard } from "../infra/clipboard.js";
44
import type { RuntimeEnv } from "../runtime.js";
55
import { defaultRuntime } from "../runtime.js";
6+
import { ensureGatewayReadyForOperation } from "./gateway-readiness.js";
67
import {
78
detectBrowserOpenSupport,
89
formatControlUiSshHint,
@@ -12,12 +13,10 @@ import {
1213

1314
type DashboardOptions = {
1415
noOpen?: boolean;
16+
yes?: boolean;
1517
};
1618

17-
export async function dashboardCommand(
18-
runtime: RuntimeEnv = defaultRuntime,
19-
options: DashboardOptions = {},
20-
) {
19+
async function resolveDashboardTarget() {
2120
const snapshot = await readConfigFileSnapshot();
2221
const cfg = snapshot.valid ? (snapshot.sourceConfig ?? snapshot.config) : {};
2322
const port = resolveGatewayPort(cfg);
@@ -47,6 +46,36 @@ export async function dashboardCommand(
4746
? `${links.httpUrl}#token=${encodeURIComponent(token)}`
4847
: links.httpUrl;
4948

49+
return {
50+
port,
51+
basePath,
52+
links,
53+
resolvedToken,
54+
token,
55+
includeTokenInUrl,
56+
dashboardUrl,
57+
};
58+
}
59+
60+
export async function dashboardCommand(
61+
runtime: RuntimeEnv = defaultRuntime,
62+
options: DashboardOptions = {},
63+
) {
64+
const initialTarget = await resolveDashboardTarget();
65+
const readiness = await ensureGatewayReadyForOperation({
66+
runtime,
67+
operation: "open the dashboard",
68+
yes: options.yes,
69+
probeUrl: initialTarget.links.wsUrl,
70+
readyWhenReachable: true,
71+
});
72+
if (!readiness.ready) {
73+
return;
74+
}
75+
76+
const target = readiness.recovered ? await resolveDashboardTarget() : initialTarget;
77+
const { port, basePath, links, resolvedToken, token, includeTokenInUrl, dashboardUrl } = target;
78+
5079
runtime.log(`Dashboard URL: ${links.httpUrl}`);
5180
if (includeTokenInUrl) {
5281
runtime.log("Token auto-auth included in browser/clipboard URL.");

0 commit comments

Comments
 (0)