Skip to content

Commit aaadcd7

Browse files
committed
fix(cli): honor gateway --port on the status leaf
1 parent fe2af30 commit aaadcd7

11 files changed

Lines changed: 263 additions & 138 deletions

File tree

‎docs/cli/daemon.md‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@ openclaw daemon uninstall
2323

2424
## Subcommands and options
2525

26-
| Subcommand | Options |
27-
| ----------- | ------------------------------------------------------------------------------------------------ |
28-
| `status` | `--url`, `--token`, `--password`, `--timeout`, `--no-probe`, `--require-rpc`, `--deep`, `--json` |
29-
| `install` | `--port`, `--runtime <node\|bun>`, `--token`, `--wrapper <path>`, `--force`, `--json` |
30-
| `uninstall` | `--json` |
31-
| `start` | `--json` |
32-
| `stop` | `--force`, `--json`, `--disable` (launchd only: suppress KeepAlive/RunAtLoad until next start) |
33-
| `restart` | `--force`, `--safe`, `--skip-deferral`, `--wait <duration>`, `--json` |
26+
| Subcommand | Options |
27+
| ----------- | ---------------------------------------------------------------------------------------------------------- |
28+
| `status` | `--url`, `--port`, `--token`, `--password`, `--timeout`, `--no-probe`, `--require-rpc`, `--deep`, `--json` |
29+
| `install` | `--port`, `--runtime <node\|bun>`, `--token`, `--wrapper <path>`, `--force`, `--json` |
30+
| `uninstall` | `--json` |
31+
| `start` | `--json` |
32+
| `stop` | `--force`, `--json`, `--disable` (launchd only: suppress KeepAlive/RunAtLoad until next start) |
33+
| `restart` | `--force`, `--safe`, `--skip-deferral`, `--wait <duration>`, `--json` |
3434

3535
`--json` is accepted before or after every subcommand (for example, `daemon --json status` and `daemon status --json`).
3636

3737
- `status`: shows service install state (launchd/systemd/schtasks) and probes Gateway health.
38+
- `status --port <port>`: selects a local Gateway using the invoking CLI config for auth and TLS. Cannot combine with `--url`; native service details remain diagnostic-only.
3839
- `install`: installs the service; `--force` reinstalls/overwrites an existing install.
3940
- Node is the primary, default, and recommended service runtime. Bun 1.4+ with WAL-reset-safe `node:sqlite` is available as an explicit opt-in with `install --runtime bun`.
4041
- `restart --safe`: asks the running Gateway to preflight active work and schedule one coalesced restart after work drains, bounded to 5 minutes. When that budget expires, the restart is forced anyway. Plain `restart` uses the service manager directly; `--force` is the immediate override.

‎docs/cli/gateway.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,14 @@ Shows the Gateway service (launchd/systemd/schtasks) plus an optional connectivi
344344
openclaw gateway status
345345
openclaw gateway status --json
346346
openclaw gateway status --require-rpc
347+
openclaw gateway status --port 19001
347348
```
348349

349350
<ParamField path="--url <url>" type="string">
350-
Add an explicit probe target. Configured remote + localhost are still probed.
351+
Probe this explicit WebSocket URL instead of the service-derived target. Cannot combine with `--port`.
352+
</ParamField>
353+
<ParamField path="--port <port>" type="number">
354+
Select a local Gateway port using the invoking CLI config for auth and TLS. Accepts `gateway --port 19001 status` and `gateway status --port 19001`; an explicit status port wins. Native service details remain visible as diagnostics but do not select the probe target.
351355
</ParamField>
352356
<ParamField path="--token <token>" type="string">
353357
Token auth for the probe.
@@ -592,7 +596,7 @@ openclaw gateway restart
592596

593597
<AccordionGroup>
594598
<Accordion title="Command options">
595-
- `gateway status`: `--url`, `--token`, `--password`, `--timeout`, `--no-probe`, `--require-rpc`, `--deep`, `--json`
599+
- `gateway status`: `--url`, `--port`, `--token`, `--password`, `--timeout`, `--no-probe`, `--require-rpc`, `--deep`, `--json`
596600
- `gateway install`: `--port`, `--runtime <node|bun>` (default: `node`), `--token`, `--wrapper <path>`, `--force`, `--json`
597601
- `gateway restart`: `--safe`, `--skip-deferral`, `--force`, `--wait <duration>`, `--json`
598602
- `gateway uninstall|start`: `--json`

‎src/cli/daemon-cli/probe.test.ts‎

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -274,58 +274,63 @@ describe("probeGatewayStatus", () => {
274274
expect(result.version).toBe("2026.5.6");
275275
});
276276

277-
it("uses a real status RPC when requireRpc is enabled", async () => {
278-
callGatewayMock.mockReset();
279-
probeGatewayMock.mockReset();
280-
callGatewayMock.mockImplementationOnce(async (opts) => {
281-
opts.onHelloOk?.({
282-
server: { version: "2026.8.1", buildId: "build-1", connId: "conn-1" },
283-
auth: { role: "operator", scopes: ["operator.admin"] },
277+
it.each([undefined, 19191])(
278+
"uses a status RPC with local port override %s when requireRpc is enabled",
279+
async (localPortOverride) => {
280+
callGatewayMock.mockReset();
281+
probeGatewayMock.mockReset();
282+
callGatewayMock.mockImplementationOnce(async (opts) => {
283+
opts.onHelloOk?.({
284+
server: { version: "2026.8.1", buildId: "build-1", connId: "conn-1" },
285+
auth: { role: "operator", scopes: ["operator.admin"] },
286+
});
287+
return { runtimeVersion: "2026.8.1", status: "ok" };
284288
});
285-
return { runtimeVersion: "2026.8.1", status: "ok" };
286-
});
287289

288-
const result = await probeGatewayStatus({
289-
url: "ws://127.0.0.1:19191",
290-
token: "temp-token",
291-
tlsFingerprint: "abc123",
292-
timeoutMs: 5_000,
293-
json: true,
294-
requireRpc: true,
295-
configPath: "/tmp/openclaw-daemon/openclaw.json",
296-
});
290+
const result = await probeGatewayStatus({
291+
url: "ws://127.0.0.1:19191",
292+
token: "temp-token",
293+
tlsFingerprint: "abc123",
294+
timeoutMs: 5_000,
295+
json: true,
296+
requireRpc: true,
297+
localPortOverride,
298+
configPath: "/tmp/openclaw-daemon/openclaw.json",
299+
});
297300

298-
expect(result).toEqual({
299-
ok: true,
300-
kind: "read",
301-
capability: "admin_capable",
302-
auth: {
303-
role: "operator",
304-
scopes: ["operator.admin"],
301+
expect(result).toEqual({
302+
ok: true,
303+
kind: "read",
305304
capability: "admin_capable",
306-
},
307-
server: {
305+
auth: {
306+
role: "operator",
307+
scopes: ["operator.admin"],
308+
capability: "admin_capable",
309+
},
310+
server: {
311+
version: "2026.8.1",
312+
buildId: "build-1",
313+
connId: "conn-1",
314+
},
308315
version: "2026.8.1",
309-
buildId: "build-1",
310-
connId: "conn-1",
311-
},
312-
version: "2026.8.1",
313-
});
314-
expect(probeGatewayMock).not.toHaveBeenCalled();
315-
expect(callGatewayMock).toHaveBeenCalledOnce();
316-
expect(callGatewayMock).toHaveBeenCalledWith({
317-
url: "ws://127.0.0.1:19191",
318-
token: "temp-token",
319-
password: undefined,
320-
tlsFingerprint: "abc123",
321-
preauthHandshakeTimeoutMs: undefined,
322-
method: "status",
323-
timeoutMs: 5_000,
324-
sharedStateMode: "read-only",
325-
configPath: "/tmp/openclaw-daemon/openclaw.json",
326-
onHelloOk: expect.any(Function),
327-
});
328-
});
316+
});
317+
expect(probeGatewayMock).not.toHaveBeenCalled();
318+
expect(callGatewayMock).toHaveBeenCalledOnce();
319+
expect(callGatewayMock).toHaveBeenCalledWith({
320+
url: "ws://127.0.0.1:19191",
321+
localPortOverride,
322+
token: "temp-token",
323+
password: undefined,
324+
tlsFingerprint: "abc123",
325+
preauthHandshakeTimeoutMs: undefined,
326+
method: "status",
327+
timeoutMs: 5_000,
328+
sharedStateMode: "read-only",
329+
configPath: "/tmp/openclaw-daemon/openclaw.json",
330+
onHelloOk: expect.any(Function),
331+
});
332+
},
333+
);
329334

330335
it("keeps required status to one timeout-bound RPC", async () => {
331336
callGatewayMock.mockReset();
@@ -351,6 +356,7 @@ describe("probeGatewayStatus", () => {
351356
tlsFingerprint: undefined,
352357
preauthHandshakeTimeoutMs: 30_000,
353358
config,
359+
localPortOverride: undefined,
354360
method: "status",
355361
timeoutMs: 30_000,
356362
sharedStateMode: "read-only",
@@ -403,6 +409,7 @@ describe("probeGatewayStatus", () => {
403409
timeoutMs: 5_000,
404410
sharedStateMode: "read-only",
405411
onHelloOk: expect.any(Function),
412+
localPortOverride: undefined,
406413
});
407414
});
408415

‎src/cli/daemon-cli/probe.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function projectGatewayConnectFailure(params: {
5252
/** Probe Gateway connectivity or read-capability status with optional RPC verification. */
5353
export async function probeGatewayStatus(opts: {
5454
url: string;
55+
localPortOverride?: number;
5556
token?: string;
5657
password?: string;
5758
config?: OpenClawConfig;
@@ -85,6 +86,7 @@ export async function probeGatewayStatus(opts: {
8586
let server: GatewayProbeServerSummary | undefined;
8687
await callGateway({
8788
url: opts.url,
89+
localPortOverride: opts.localPortOverride,
8890
token: opts.token,
8991
password: opts.password,
9092
tlsFingerprint: opts.tlsFingerprint,

‎src/cli/daemon-cli/register-service-commands.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,30 @@ describe("addGatewayServiceCommands", () => {
144144

145145
expect(expectSingleDaemonCall(runner).json).toBe(true);
146146
});
147+
148+
it("inherits an explicit parent port instead of a status leaf default", async () => {
149+
const gateway = createGatewayParentLikeCommand().enablePositionalOptions();
150+
const status = gateway.commands.find((command) => command.name() === "status")!;
151+
status.setOptionValueWithSource("port", "19003", "default");
152+
153+
await gateway.parseAsync(["--port", "19002", "status"], { from: "user" });
154+
155+
expect(expectSingleDaemonCall(runDaemonStatus).rpc).toMatchObject({
156+
port: "19002",
157+
localPortOverride: 19002,
158+
});
159+
});
160+
161+
it.each([
162+
{ argv: ["status", "--port", "0"], error: "--port must be an integer between 1 and 65535." },
163+
{
164+
argv: ["--port", "19002", "status", "--url", "ws://localhost:19002"],
165+
error: "Use either --url or --port, not both.",
166+
},
167+
])("rejects invalid status options $argv", async ({ argv, error }) => {
168+
const gateway = createGatewayParentLikeCommand().enablePositionalOptions().exitOverride();
169+
170+
await expect(gateway.parseAsync(argv, { from: "user" })).rejects.toThrow(error);
171+
expect(runDaemonStatus).not.toHaveBeenCalled();
172+
});
147173
});

‎src/cli/daemon-cli/register-service-commands.ts‎

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,13 @@
22
import type { Command } from "commander";
33
import { createLazyImportLoader } from "../../shared/lazy-promise.js";
44
import { inheritOptionFromParent } from "../command-options.js";
5-
import type { DaemonInstallOptions, DaemonLifecycleOptions, GatewayRpcOpts } from "./types.js";
5+
import { resolveGatewayRpcOptionsWithLocalPort } from "../gateway-rpc.js";
6+
import type { DaemonInstallOptions, DaemonLifecycleOptions } from "./types.js";
67

78
const daemonInstallModuleLoader = createLazyImportLoader(() => import("./install.runtime.js"));
89
const daemonLifecycleModuleLoader = createLazyImportLoader(() => import("./lifecycle.runtime.js"));
910
const daemonStatusModuleLoader = createLazyImportLoader(() => import("./status.runtime.js"));
1011

11-
function loadDaemonInstallModule() {
12-
return daemonInstallModuleLoader.load();
13-
}
14-
15-
function loadDaemonLifecycleModule() {
16-
return daemonLifecycleModuleLoader.load();
17-
}
18-
19-
function loadDaemonStatusModule() {
20-
return daemonStatusModuleLoader.load();
21-
}
22-
2312
function resolveJsonOption(cmdOpts: { json?: boolean }, command?: Command): boolean {
2413
const parentJson = inheritOptionFromParent<boolean>(command, "json", "cli");
2514
return Boolean(cmdOpts.json || parentJson);
@@ -41,16 +30,6 @@ function resolveInstallOptions(
4130
};
4231
}
4332

44-
function resolveRpcOptions(cmdOpts: GatewayRpcOpts, command?: Command): GatewayRpcOpts {
45-
const parentToken = inheritOptionFromParent<string>(command, "token");
46-
const parentPassword = inheritOptionFromParent<string>(command, "password");
47-
return {
48-
...cmdOpts,
49-
token: cmdOpts.token ?? parentToken,
50-
password: cmdOpts.password ?? parentPassword,
51-
};
52-
}
53-
5433
function resolveRestartOptions(cmdOpts: DaemonLifecycleOptions, command?: Command) {
5534
const parentForce = inheritOptionFromParent<boolean>(command, "force");
5635
return {
@@ -78,6 +57,7 @@ export function addGatewayServiceCommands(parent: Command, opts?: { statusDescri
7857
opts?.statusDescription ?? "Show gateway service status + probe connectivity/capability",
7958
)
8059
.option("--url <url>", "Gateway WebSocket URL (defaults to config/remote/local)")
60+
.option("--port <port>", "Local Gateway port")
8161
.option("--token <token>", "Gateway token (if required)")
8262
.option("--password <password>", "Gateway password (password auth)")
8363
.option("--timeout <ms>", "Timeout in ms", "10000")
@@ -86,9 +66,9 @@ export function addGatewayServiceCommands(parent: Command, opts?: { statusDescri
8666
.option("--deep", "Scan system-level services", false)
8767
.option("--json", "Output JSON", false)
8868
.action(async (cmdOpts, command) => {
89-
const { runDaemonStatus } = await loadDaemonStatusModule();
69+
const { runDaemonStatus } = await daemonStatusModuleLoader.load();
9070
await runDaemonStatus({
91-
rpc: resolveRpcOptions(cmdOpts, command),
71+
rpc: resolveGatewayRpcOptionsWithLocalPort(cmdOpts, command),
9272
probe: Boolean(cmdOpts.probe),
9373
requireRpc: Boolean(cmdOpts.requireRpc),
9474
deep: Boolean(cmdOpts.deep),
@@ -106,7 +86,7 @@ export function addGatewayServiceCommands(parent: Command, opts?: { statusDescri
10686
.option("--force", "Reinstall/overwrite if already installed", false)
10787
.option("--json", "Output JSON", false)
10888
.action(async (cmdOpts, command) => {
109-
const { runDaemonInstall } = await loadDaemonInstallModule();
89+
const { runDaemonInstall } = await daemonInstallModuleLoader.load();
11090
await runDaemonInstall(resolveInstallOptions(cmdOpts, command));
11191
});
11292

@@ -115,7 +95,7 @@ export function addGatewayServiceCommands(parent: Command, opts?: { statusDescri
11595
.description("Uninstall the Gateway service (launchd/systemd/schtasks)")
11696
.option("--json", "Output JSON", false)
11797
.action(async (cmdOpts, command) => {
118-
const { runDaemonUninstall } = await loadDaemonLifecycleModule();
98+
const { runDaemonUninstall } = await daemonLifecycleModuleLoader.load();
11999
await runDaemonUninstall({ ...cmdOpts, json: resolveJsonOption(cmdOpts, command) });
120100
});
121101

@@ -124,7 +104,7 @@ export function addGatewayServiceCommands(parent: Command, opts?: { statusDescri
124104
.description("Start the Gateway service (launchd/systemd/schtasks)")
125105
.option("--json", "Output JSON", false)
126106
.action(async (cmdOpts, command) => {
127-
const { runDaemonStart } = await loadDaemonLifecycleModule();
107+
const { runDaemonStart } = await daemonLifecycleModuleLoader.load();
128108
await runDaemonStart({ ...cmdOpts, json: resolveJsonOption(cmdOpts, command) });
129109
});
130110

@@ -139,7 +119,7 @@ export function addGatewayServiceCommands(parent: Command, opts?: { statusDescri
139119
false,
140120
)
141121
.action(async (cmdOpts, command) => {
142-
const { runDaemonStop } = await loadDaemonLifecycleModule();
122+
const { runDaemonStop } = await daemonLifecycleModuleLoader.load();
143123
await runDaemonStop(resolveStopOptions(cmdOpts, command));
144124
});
145125

@@ -165,7 +145,7 @@ export function addGatewayServiceCommands(parent: Command, opts?: { statusDescri
165145
)
166146
.option("--json", "Output JSON", false)
167147
.action(async (cmdOpts, command) => {
168-
const { runDaemonRestart } = await loadDaemonLifecycleModule();
148+
const { runDaemonRestart } = await daemonLifecycleModuleLoader.load();
169149
await runDaemonRestart(resolveRestartOptions(cmdOpts, command));
170150
});
171151
}

0 commit comments

Comments
 (0)