Skip to content

Commit c92a3cb

Browse files
committed
test(cli): prove qualified global resume crosses the gateway boundary
1 parent a669d61 commit c92a3cb

3 files changed

Lines changed: 35 additions & 13 deletions

File tree

‎docs/cli/resume.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ configured Gateway is unavailable, start or repair it and rerun the command.
4545
`resume` resolves configured Gateway auth SecretRefs for token/password auth
4646
when possible (`env`/`file`/`exec` providers).
4747

48-
With no explicit URL or port, `resume` follows the active local Gateway port
49-
recorded by the running Gateway. Explicit `--url`, `OPENCLAW_GATEWAY_URL`,
50-
`OPENCLAW_GATEWAY_PORT`, and remote Gateway config keep precedence.
48+
Gateway target precedence is explicit `--url`, then `OPENCLAW_GATEWAY_URL`,
49+
then `gateway.remote.url` when `gateway.mode` is `remote`, then the local
50+
loopback Gateway. For that local Gateway, `OPENCLAW_GATEWAY_PORT` takes
51+
precedence over the active port recorded by a running Gateway, which takes
52+
precedence over the configured or default `gateway.port`.
5153

5254
## Examples
5355

‎src/cli/resume-cli.runtime.ts‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ function requireInteractiveResumeTerminal() {
2323
}
2424
}
2525

26-
async function fetchResumeSessions(opts: ResumeCliOptions) {
26+
async function fetchResumeSessions(
27+
opts: ResumeCliOptions,
28+
options: { agentId?: string; includeGlobal?: boolean } = {},
29+
) {
2730
const { GatewayChatClient } = await import("../tui/gateway-chat.js");
2831
const client = await GatewayChatClient.connect(opts);
2932
try {
@@ -42,7 +45,7 @@ async function fetchResumeSessions(opts: ResumeCliOptions) {
4245
finish(() => reject(new Error(reason || "Gateway connection closed")));
4346
client.start();
4447
});
45-
return await loadRecentSessions(client);
48+
return await loadRecentSessions(client, options);
4649
} catch (error) {
4750
const [{ formatTuiErrorMessage }, { resolveGatewayDisconnectState }] = await Promise.all([
4851
import("../tui/tui-formatters.js"),
@@ -115,20 +118,29 @@ function formatResumeCandidate(candidate: SessionPickerChoice): string {
115118
return label === key ? key : `${label} [${key}]`;
116119
}
117120

118-
function resolveExplicitGlobalSessionKey(query: string | undefined): string | undefined {
121+
function resolveExplicitGlobalSessionKey(
122+
query: string | undefined,
123+
): { agentId: string; key: string } | undefined {
119124
const parsed = parseAgentSessionKey(query);
120-
return parsed?.rest === "global" ? `agent:${parsed.agentId}:global` : undefined;
125+
return parsed?.rest === "global"
126+
? { agentId: parsed.agentId, key: `agent:${parsed.agentId}:global` }
127+
: undefined;
121128
}
122129

123130
/** Resolve or select one session and run the existing Gateway-backed TUI. */
124131
export async function runResumeCommand(query: string | undefined, opts: ResumeCliOptions) {
125132
requireInteractiveResumeTerminal();
126133
const trimmedQuery = query?.trim();
127-
const explicitGlobalSessionKey = resolveExplicitGlobalSessionKey(trimmedQuery);
128-
const sessions = explicitGlobalSessionKey ? [] : await fetchResumeSessions(opts);
134+
const explicitGlobalSession = resolveExplicitGlobalSessionKey(trimmedQuery);
135+
const sessions = await fetchResumeSessions(
136+
opts,
137+
explicitGlobalSession
138+
? { agentId: explicitGlobalSession.agentId, includeGlobal: true }
139+
: undefined,
140+
);
129141
let sessionKey: string | null;
130-
if (explicitGlobalSessionKey) {
131-
sessionKey = explicitGlobalSessionKey;
142+
if (explicitGlobalSession) {
143+
sessionKey = explicitGlobalSession.key;
132144
} else if (trimmedQuery) {
133145
const resolution = resolveResumeSession(sessions, trimmedQuery);
134146
if (resolution.kind !== "match") {

‎src/cli/resume-cli.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { parseAgentSessionKey } from "../routing/session-key.js";
23
import type { TuiSessionList } from "../tui/tui-backend.js";
34
import { resolveResumeSession } from "../tui/tui-session-picker.js";
45
import { runResumeCommand } from "./resume-cli.runtime.js";
@@ -181,16 +182,23 @@ describe("runResumeCommand", () => {
181182
it("preserves an explicitly agent-qualified global session", async () => {
182183
Object.defineProperty(process.stdin, "isTTY", { configurable: true, value: true });
183184
Object.defineProperty(process.stdout, "isTTY", { configurable: true, value: true });
185+
const client = createGatewayClient([{ key: "global", displayName: "global" }]);
184186

185-
await runResumeCommand("AGENT:Work:GLOBAL", {});
187+
await runResumeCommand("agent:work:global", {});
186188

187-
expect(gatewayMocks.connect).not.toHaveBeenCalled();
189+
expect(client.listSessions).toHaveBeenCalledWith(
190+
expect.objectContaining({ agentId: "work", includeGlobal: true }),
191+
);
188192
expect(gatewayMocks.runTui).toHaveBeenCalledWith(
189193
expect.objectContaining({
190194
session: "agent:work:global",
191195
forceProcessExitOnReturn: true,
192196
}),
193197
);
198+
expect(parseAgentSessionKey(gatewayMocks.runTui.mock.lastCall?.[0]?.session)).toEqual({
199+
agentId: "work",
200+
rest: "global",
201+
});
194202
});
195203

196204
it("rejects a non-interactive queried resume before connecting or launching the TUI", async () => {

0 commit comments

Comments
 (0)