Skip to content

Commit 3c81eb4

Browse files
steipetePeter Steinberger
andauthored
fix(ui): scope Gateway reconnect lineage to its target (#117125)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent cbdbf68 commit 3c81eb4

3 files changed

Lines changed: 167 additions & 1 deletion

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* @vitest-environment jsdom */
2+
3+
import { afterEach, describe, expect, it, vi } from "vitest";
4+
import type {
5+
GatewayBrowserClient,
6+
GatewayBrowserClientOptions,
7+
GatewayHelloOk,
8+
} from "../api/gateway.ts";
9+
import { createStorageMock } from "../test-helpers/storage.ts";
10+
import "./app-host.ts";
11+
import type { ApplicationContext, ApplicationGateway } from "./context.ts";
12+
import { createApplicationGateway } from "./gateway-store.ts";
13+
import { loadSettings } from "./settings.ts";
14+
15+
const HELLO: GatewayHelloOk = {
16+
type: "hello-ok",
17+
protocol: 1,
18+
auth: { role: "operator", scopes: [] },
19+
};
20+
21+
function createGatewayHarness() {
22+
vi.stubGlobal("localStorage", createStorageMock());
23+
vi.stubGlobal("sessionStorage", createStorageMock());
24+
const clients: Array<{ opts: GatewayBrowserClientOptions }> = [];
25+
const gateway = createApplicationGateway(loadSettings(), "", "", (opts) => {
26+
const client = {
27+
opts,
28+
instanceId: opts.instanceId,
29+
start: vi.fn(),
30+
stop: vi.fn(),
31+
request: vi.fn(),
32+
};
33+
clients.push(client);
34+
return client as unknown as GatewayBrowserClient;
35+
});
36+
return { gateway, clients };
37+
}
38+
39+
function renderGatewaySurface(gateway: ApplicationGateway): string {
40+
const app = document.createElement("openclaw-app") as unknown as {
41+
runtime: { context: ApplicationContext };
42+
render: () => { strings: readonly string[] };
43+
synchronizeGateway: (gateway: ApplicationGateway) => void;
44+
};
45+
app.runtime = {
46+
context: {
47+
gateway,
48+
basePath: "",
49+
config: { current: { terminalEnabled: false } },
50+
} as unknown as ApplicationContext,
51+
};
52+
app.synchronizeGateway(gateway);
53+
return app.render().strings.join("");
54+
}
55+
56+
afterEach(() => {
57+
vi.unstubAllGlobals();
58+
vi.restoreAllMocks();
59+
});
60+
61+
describe("Control UI Gateway target lineage", () => {
62+
it("returns to the login gate when a newly selected Gateway's first attempt fails", () => {
63+
const { gateway, clients } = createGatewayHarness();
64+
gateway.start();
65+
clients[0]?.opts.onHello?.(HELLO);
66+
gateway.connect({ gatewayUrl: "wss://other-gateway.example.test" });
67+
clients[1]?.opts.onClose?.({ code: 1006, reason: "remote refused", willRetry: true });
68+
69+
const surface = renderGatewaySurface(gateway);
70+
71+
expect(surface).toContain("<openclaw-login-gate");
72+
expect(surface).not.toContain("<openclaw-app-shell");
73+
});
74+
75+
it("keeps an established Gateway's dashboard mounted during its own retry", () => {
76+
const { gateway, clients } = createGatewayHarness();
77+
gateway.start();
78+
clients[0]?.opts.onHello?.(HELLO);
79+
clients[0]?.opts.onClose?.({ code: 1006, reason: "same gateway blip", willRetry: true });
80+
81+
const surface = renderGatewaySurface(gateway);
82+
83+
expect(surface).toContain("<openclaw-app-shell");
84+
expect(surface).not.toContain("<openclaw-login-gate");
85+
});
86+
87+
it("retains a replacement Gateway's dashboard after its own successful hello", () => {
88+
const { gateway, clients } = createGatewayHarness();
89+
gateway.start();
90+
clients[0]?.opts.onHello?.(HELLO);
91+
gateway.connect({ gatewayUrl: "wss://other-gateway.example.test" });
92+
clients[1]?.opts.onHello?.(HELLO);
93+
clients[1]?.opts.onClose?.({ code: 1006, reason: "replacement blip", willRetry: true });
94+
95+
const surface = renderGatewaySurface(gateway);
96+
97+
expect(surface).toContain("<openclaw-app-shell");
98+
expect(surface).not.toContain("<openclaw-login-gate");
99+
});
100+
});

‎ui/src/app/gateway-store.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,68 @@ describe("createApplicationGateway connection phase", () => {
224224
expect(gateway.snapshot.lastError).toContain("4008");
225225
});
226226

227+
it("starts a newly selected Gateway as a fresh connection", () => {
228+
const { gateway, clients, current } = createStore();
229+
gateway.start();
230+
current().opts.onHello?.(HELLO);
231+
232+
gateway.connect({ gatewayUrl: "wss://other-gateway.example.test", token: "other-token" });
233+
234+
expect(clients[0]?.stopped).toBe(1);
235+
expect(current().opts.url).toBe("wss://other-gateway.example.test");
236+
expect(current().opts.token).toBe("other-token");
237+
expect(gateway.snapshot.phase).toBe("connecting");
238+
});
239+
240+
it("keeps a newly selected Gateway's first retry at the login gate", () => {
241+
const { gateway, current } = createStore();
242+
gateway.start();
243+
current().opts.onHello?.(HELLO);
244+
gateway.connect({ gatewayUrl: "wss://other-gateway.example.test" });
245+
246+
current().opts.onClose?.({ code: 1006, reason: "remote refused", willRetry: true });
247+
248+
expect(gateway.snapshot.phase).toBe("connecting");
249+
expect(gateway.snapshot.lastError).toBe("disconnected (1006): remote refused");
250+
});
251+
252+
it("treats a newly selected Gateway's first terminal close as never connected", () => {
253+
const { gateway, current } = createStore();
254+
gateway.start();
255+
current().opts.onHello?.(HELLO);
256+
gateway.connect({ gatewayUrl: "wss://other-gateway.example.test" });
257+
258+
current().opts.onClose?.({ code: 4008, reason: "remote rejected", willRetry: false });
259+
260+
expect(gateway.snapshot.phase).toBe("stopped");
261+
expect(gateway.snapshot.lastError).toBe("disconnected (4008): remote rejected");
262+
});
263+
264+
it("retains a newly selected Gateway's shell after its own successful hello", () => {
265+
const { gateway, current } = createStore();
266+
gateway.start();
267+
current().opts.onHello?.(HELLO);
268+
gateway.connect({ gatewayUrl: "wss://other-gateway.example.test" });
269+
current().opts.onHello?.(HELLO);
270+
271+
current().opts.onClose?.({ code: 1006, reason: "remote blip", willRetry: true });
272+
273+
expect(gateway.snapshot.phase).toBe("reconnecting");
274+
});
275+
276+
it("preserves an established Gateway's lineage when its unchanged URL is resubmitted", () => {
277+
const { gateway, current } = createStore();
278+
gateway.start();
279+
current().opts.onHello?.(HELLO);
280+
const gatewayUrl = gateway.connection.gatewayUrl;
281+
282+
gateway.connect({ gatewayUrl, token: "replacement-token", sessionKey: "agent:main:other" });
283+
284+
expect(gateway.snapshot.phase).toBe("reconnecting");
285+
expect(current().opts.token).toBe("replacement-token");
286+
expect(gateway.snapshot.sessionKey).toBe("agent:main:other");
287+
});
288+
227289
it.each(["stopped", "connecting", "connected", "reconnecting", "offline"] as const)(
228290
"stop() resets %s to stopped",
229291
(phase) => {

‎ui/src/app/gateway-store.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export function createApplicationGateway(
9494
let canvasSurfaceLeaseClient: GatewayBrowserClient | null = null;
9595
let canvasSurfaceLeaseStarted = false;
9696
let canvasSurfaceLeaseGeneration = 0;
97-
// Session lineage for this page lifetime: once a hello succeeded, later
97+
// Session lineage belongs to the selected Gateway: once its hello succeeds,
9898
// transport drops render as "reconnecting" (shell + banner) instead of
9999
// kicking the operator back to the login gate.
100100
let everConnected = false;
@@ -273,6 +273,10 @@ export function createApplicationGateway(
273273
const gatewayUrlChanged =
274274
connectionOverrides.gatewayUrl !== undefined &&
275275
connectionOverrides.gatewayUrl !== connection.gatewayUrl;
276+
// A different Gateway has no established session to keep mounted on failure.
277+
if (gatewayUrlChanged) {
278+
everConnected = false;
279+
}
276280
connection = nextConnection;
277281
// Trust the connected gateway's origin for avatar route resolution so
278282
// split-origin Control UI deployments load uploaded/proxied avatars.

0 commit comments

Comments
 (0)