Skip to content

Commit 486b272

Browse files
authored
fix(ui): stop stale clients from acting connected (#124772)
* fix(ui): stop stale clients from acting connected Carry reload metadata on terminal Control UI build rejections, render an explicit refresh-required recovery state, fence reconnect-only actions, and surface disconnected approval failures with accessible modal controls. * fix(ui): preserve reconnecting session drafts * fix(ui): preserve offline preference intent * fix(ui): return owned fallback digest bytes * test(ui): follow passive approval presentation * fix(ui): distinguish stale builds from protocol mismatches * style(ui): format gateway recovery phase
1 parent 8277cb2 commit 486b272

21 files changed

Lines changed: 284 additions & 37 deletions

‎packages/gateway-protocol/src/connect-error-details.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ describe("readConnectErrorDetailCode", () => {
4545
});
4646

4747
describe("readControlUiBuildMismatchId", () => {
48-
it("returns a bounded reload target", () => {
48+
it.each([
49+
ConnectErrorDetailCodes.PROTOCOL_MISMATCH,
50+
ConnectErrorDetailCodes.CONTROL_UI_BUILD_MISMATCH,
51+
])("returns a bounded reload target for %s", (code) => {
4952
expect(
5053
readControlUiBuildMismatchId({
51-
code: ConnectErrorDetailCodes.CONTROL_UI_BUILD_MISMATCH,
54+
code,
5255
gatewayBuildId: "gateway-build",
5356
reloadRequired: true,
5457
}),

‎packages/gateway-protocol/src/connect-error-details.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ export function readConnectErrorDetailCode(details: unknown): string | null {
232232

233233
/** Read the exact target artifact from an untrusted reload-required rejection. */
234234
export function readControlUiBuildMismatchId(details: unknown): string | null {
235-
if (readConnectErrorDetailCode(details) !== ConnectErrorDetailCodes.CONTROL_UI_BUILD_MISMATCH) {
235+
const code = readConnectErrorDetailCode(details);
236+
if (
237+
code !== ConnectErrorDetailCodes.PROTOCOL_MISMATCH &&
238+
code !== ConnectErrorDetailCodes.CONTROL_UI_BUILD_MISMATCH
239+
) {
236240
return null;
237241
}
238242
const raw = details as { gatewayBuildId?: unknown; reloadRequired?: unknown };

‎src/gateway/server/ws-connection/connect-session.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,11 @@ export async function attachAuthenticatedGatewayConnect(
332332
});
333333
sendHandshakeErrorResponse(ErrorCodes.UNAVAILABLE, message, {
334334
retryable: false,
335-
details: { code: ConnectErrorDetailCodes.PROTOCOL_MISMATCH },
335+
details: {
336+
code: ConnectErrorDetailCodes.PROTOCOL_MISMATCH,
337+
gatewayBuildId: controlUiBuildMismatch.gatewayBuildId,
338+
reloadRequired: true,
339+
},
336340
});
337341
logWsControl.warn(
338342
`control ui build rejected conn=${connId} clientBuild=${formatForLog(controlUiBuildMismatch.clientBuildId ?? "legacy")} gatewayBuild=${formatForLog(controlUiBuildMismatch.gatewayBuildId)}; reload required`,

‎src/gateway/server/ws-connection/message-handler.control-ui-build-admission.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { tmpdir } from "node:os";
55
import path from "node:path";
66
import { afterEach, describe, expect, it, vi } from "vitest";
77
import { WebSocket, WebSocketServer } from "ws";
8-
import { ConnectErrorDetailCodes } from "../../../../packages/gateway-protocol/src/connect-error-details.js";
8+
import {
9+
ConnectErrorDetailCodes,
10+
readControlUiBuildMismatchId,
11+
} from "../../../../packages/gateway-protocol/src/connect-error-details.js";
912
import { ErrorCodes, PROTOCOL_VERSION } from "../../../../packages/gateway-protocol/src/index.js";
1013
import { rawDataToString } from "../../../infra/ws.js";
1114
import type { GatewayRequestContext } from "../../server-methods/types.js";
@@ -290,9 +293,18 @@ describe("Control UI build admission over WebSocket", () => {
290293
code: ErrorCodes.UNAVAILABLE,
291294
message: "protocol mismatch: Control UI updated; reload this page to continue",
292295
retryable: false,
293-
details: { code: ConnectErrorDetailCodes.PROTOCOL_MISMATCH },
296+
details: {
297+
code: ConnectErrorDetailCodes.PROTOCOL_MISMATCH,
298+
gatewayBuildId: "gateway-build",
299+
reloadRequired: true,
300+
},
294301
},
295302
});
303+
expect(
304+
readControlUiBuildMismatchId(
305+
(rejection.error as { details?: unknown } | undefined)?.details,
306+
),
307+
).toBe("gateway-build");
296308
ws.send(
297309
JSON.stringify({
298310
type: "req",

‎ui/src/app/app-root.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,9 @@ export class OpenClawApp extends OpenClawLightDomElement {
287287
</openclaw-tooltip-provider>
288288
`;
289289
}
290-
const showLoginGate =
291-
!gatewayConnected && (this.loginGatePinned || gatewaySnapshot.phase !== "reconnecting");
290+
const shellOwnsRecovery =
291+
gatewaySnapshot.phase === "reconnecting" || gatewaySnapshot.phase === "reload-required";
292+
const showLoginGate = !gatewayConnected && !shellOwnsRecovery;
292293
if (showLoginGate) {
293294
return html`
294295
<openclaw-tooltip-provider>

‎ui/src/app/app-shell-view.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ export function renderApplicationShell(host: ShellViewHost) {
274274
const custodianPanelAvailable =
275275
gatewayConnected && isGatewayMethodAdvertised(gatewaySnapshot, "openclaw.chat") === true;
276276
const activeRoute = host.routeState.routeId ?? "chat";
277+
// Chat has an offline outbox, New Session keeps a local draft, and Appearance
278+
// persists local preference intent for replay. Their server actions are
279+
// independently gated; other pages cannot submit useful disconnected work.
280+
const pageActionsBlocked =
281+
gatewaySnapshot.phase === "reload-required" ||
282+
(!gatewayConnected &&
283+
activeRoute !== "chat" &&
284+
activeRoute !== "new-session" &&
285+
activeRoute !== "appearance");
277286
// Plugin tabs share one route; the search picks the active item.
278287
const activePluginRef =
279288
activeRoute === "plugin"
@@ -595,7 +604,14 @@ export function renderApplicationShell(host: ShellViewHost) {
595604
onRefresh: () => host.refreshControlUi(),
596605
onHoldUpdate: () => context.overlays.holdUpdate(),
597606
})}
607+
${pageActionsBlocked && gatewaySnapshot.phase !== "reload-required"
608+
? html`<div class="connection-action-block" role="status" aria-live="polite">
609+
${t("connection.actionsUnavailable")}
610+
</div>`
611+
: nothing}
598612
<openclaw-router-outlet
613+
?inert=${pageActionsBlocked}
614+
aria-disabled=${pageActionsBlocked ? "true" : nothing}
599615
.router=${runtime.router}
600616
.retryContext=${context}
601617
.onNotFound=${() => host.replaceChatWithCurrentSession()}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,51 @@ describe("createApplicationGateway connection phase", () => {
547547
expect(gateway.snapshot.phase).toBe("offline");
548548
});
549549

550+
it("schedules the guarded reload and publishes a terminal phase for a stale build", () => {
551+
const { gateway, current } = createStore();
552+
gateway.start();
553+
554+
current().opts.onClose?.({
555+
code: 1008,
556+
reason: "protocol mismatch: Control UI updated; reload this page to continue",
557+
error: {
558+
code: "UNAVAILABLE",
559+
message: "protocol mismatch: Control UI updated; reload this page to continue",
560+
details: {
561+
code: ConnectErrorDetailCodes.PROTOCOL_MISMATCH,
562+
gatewayBuildId: "replacement-build",
563+
reloadRequired: true,
564+
},
565+
},
566+
willRetry: false,
567+
});
568+
569+
expect(scheduleStaleChunkReloadMock).toHaveBeenCalledExactlyOnceWith({
570+
buildId: "replacement-build",
571+
});
572+
expect(gateway.snapshot.phase).toBe("reload-required");
573+
});
574+
575+
it("keeps a bare protocol mismatch in the login-gate path", () => {
576+
const { gateway, current } = createStore();
577+
gateway.start();
578+
579+
current().opts.onClose?.({
580+
code: 1008,
581+
reason: "protocol mismatch",
582+
error: {
583+
code: "INVALID_REQUEST",
584+
message: "protocol mismatch",
585+
details: { code: ConnectErrorDetailCodes.PROTOCOL_MISMATCH },
586+
},
587+
willRetry: false,
588+
});
589+
590+
expect(scheduleStaleChunkReloadMock).not.toHaveBeenCalled();
591+
expect(gateway.snapshot.phase).toBe("stopped");
592+
expect(gateway.snapshot.lastErrorCode).toBe(ConnectErrorDetailCodes.PROTOCOL_MISMATCH);
593+
});
594+
550595
it("keeps reconnecting across event-gap recovery with a fresh client", () => {
551596
const { gateway, clients, current } = createStore();
552597
gateway.start();

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -437,23 +437,27 @@ export function createApplicationGateway(
437437
if (mismatchedBuildId) {
438438
void scheduleStaleChunkReload({ buildId: mismatchedBuildId });
439439
}
440+
const lastErrorCode = resolveGatewayErrorDetailCode(error) ?? error?.code ?? null;
440441
setSnapshot({
441442
...snapshot,
442443
client: nextClient,
443-
phase: everConnected
444-
? willRetry
445-
? "reconnecting"
446-
: "offline"
447-
: willRetry
448-
? "connecting"
449-
: "stopped",
444+
phase:
445+
mismatchedBuildId !== null
446+
? "reload-required"
447+
: everConnected
448+
? willRetry
449+
? "reconnecting"
450+
: "offline"
451+
: willRetry
452+
? "connecting"
453+
: "stopped",
450454
hello: null,
451455
canvasPluginSurfaceUrl: null,
452456
selfUser: null,
453457
lastError: error?.message
454458
? formatUiError(error.message)
455459
: `disconnected (${code}): ${formatUiExternalText(reason, t("common.unknown"))}`,
456-
lastErrorCode: resolveGatewayErrorDetailCode(error) ?? error?.code ?? null,
460+
lastErrorCode,
457461
});
458462
},
459463
onGap: ({ expected, received }) => {

‎ui/src/app/gateway.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type ApplicationGatewayPhase =
88
| "connecting"
99
| "connected"
1010
| "reconnecting"
11+
| "reload-required"
1112
| "offline";
1213

1314
export type ApplicationGatewaySnapshot = {

‎ui/src/app/overlays-access.test-support.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ export function createGatewayHarness(
121121
},
122122
gateway,
123123
connect,
124+
replaceSnapshotWithoutPublishing(next: Partial<ApplicationGatewaySnapshot>) {
125+
snapshot = { ...snapshot, ...next };
126+
},
124127
update(next: Partial<ApplicationGatewaySnapshot>) {
125128
snapshot = { ...snapshot, ...next };
126129
for (const listener of snapshotListeners) {

0 commit comments

Comments
 (0)