Skip to content

Commit 58ab589

Browse files
authored
fix(ui): stale tabs restore terminals without owners (#123660)
* fix(ui): fence stale same-version clients by build * test(ui): satisfy build identity lint guards * fix(ui): complete build identity fencing * chore: leave release notes to release prep * test(ui): align build identity fixtures * fix(ui): fence reconnects on pending worker * test(ui): stabilize cross-platform update proof * test(ui): satisfy service worker lint * test(ui): preserve offline socket cohorts
1 parent 15bf43f commit 58ab589

32 files changed

Lines changed: 694 additions & 29 deletions

‎packages/gateway-client/src/client.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ export type GatewayClientOptions = {
258258
clientName?: GatewayClientName;
259259
clientDisplayName?: string;
260260
clientVersion?: string;
261+
clientBuildId?: string;
261262
platform?: string;
262263
deviceFamily?: string;
263264
mode?: GatewayClientMode;
@@ -760,6 +761,7 @@ export class GatewayClient {
760761
id: clientId,
761762
displayName: this.opts.clientDisplayName,
762763
version: this.opts.clientVersion ?? DEFAULT_CLIENT_VERSION,
764+
buildId: this.opts.clientBuildId,
763765
platform,
764766
deviceFamily,
765767
mode: clientMode,

‎packages/gateway-protocol/src/client-info.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export type GatewayClientInfo = {
6363
displayName?: string;
6464
/** Client app or package version reported by the connecting process. */
6565
version: string;
66+
/** Exact immutable artifact identity when the client can report one. */
67+
buildId?: string;
6668
/** Runtime platform string, such as `darwin`, `ios`, `android`, or `web`. */
6769
platform: string;
6870
/** Optional device family used by native clients for display and routing hints. */
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it } from "vitest";
2+
import { validateConnectParams } from "../index.js";
3+
4+
describe("gateway client build identity", () => {
5+
const connect = {
6+
minProtocol: 1,
7+
maxProtocol: 1,
8+
client: { id: "test" as const, version: "1.0.0", platform: "test", mode: "test" as const },
9+
};
10+
11+
it("accepts the bounded artifact id and rejects oversized input", () => {
12+
expect(
13+
validateConnectParams({
14+
...connect,
15+
client: { ...connect.client, buildId: "b".repeat(96) },
16+
}),
17+
).toBe(true);
18+
expect(
19+
validateConnectParams({
20+
...connect,
21+
client: { ...connect.client, buildId: "b".repeat(97) },
22+
}),
23+
).toBe(false);
24+
});
25+
});

‎packages/gateway-protocol/src/schema/frames.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const ConnectParamsSchema = closedObject({
4040
id: GatewayClientIdSchema,
4141
displayName: Type.Optional(NonEmptyString),
4242
version: NonEmptyString,
43+
buildId: Type.Optional(Type.String({ minLength: 1, maxLength: 96 })),
4344
platform: NonEmptyString,
4445
deviceFamily: Type.Optional(NonEmptyString),
4546
modelIdentifier: Type.Optional(NonEmptyString),
@@ -85,6 +86,10 @@ export const HelloOkSchema = closedObject({
8586
protocol: Type.Integer({ minimum: 1 }),
8687
server: closedObject({
8788
version: NonEmptyString,
89+
buildId: Type.Optional(Type.String({ minLength: 1, maxLength: 96 })),
90+
controlUiBuildSource: Type.Optional(
91+
Type.Union([Type.Literal("bundled"), Type.Literal("configured")]),
92+
),
8893
connId: NonEmptyString,
8994
}),
9095
features: closedObject({

‎scripts/write-build-info.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { execFileSync } from "node:child_process";
33
import fs from "node:fs";
44
import path from "node:path";
55
import { fileURLToPath, pathToFileURL } from "node:url";
6+
import { normalizeControlUiBuildInfo } from "../ui/src/build-info-normalizers.ts";
67

78
const defaultRootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
89
const FULL_GIT_COMMIT_RE = /^[0-9a-f]{40}$/iu;
@@ -22,6 +23,7 @@ export type BuildInfo = {
2223
version: string | null;
2324
commit: string | null;
2425
builtAt: string;
26+
buildId: string;
2527
};
2628

2729
type ResolveBuildInfoOptions = {
@@ -106,11 +108,23 @@ export function resolveBuildInfo(options: ResolveBuildInfoOptions = {}): BuildIn
106108
const builtAt = explicitTimestamp
107109
? normalizeBuildTimestamp(explicitTimestamp)
108110
: (options.now ?? (() => new Date()))().toISOString();
111+
const releaseFlag = env.OPENCLAW_CONTROL_UI_RELEASE_BUILD?.trim();
112+
if (releaseFlag && releaseFlag !== "1") {
113+
throw new Error("OPENCLAW_CONTROL_UI_RELEASE_BUILD must be 1 when set");
114+
}
115+
const buildId = normalizeControlUiBuildInfo({
116+
version: readPackageVersion(rootDir),
117+
commit,
118+
builtAt,
119+
release: releaseFlag === "1",
120+
buildId: env.OPENCLAW_CONTROL_UI_BUILD_ID,
121+
}).buildId;
109122

110123
return {
111124
version: readPackageVersion(rootDir),
112125
commit,
113126
builtAt,
127+
buildId,
114128
};
115129
}
116130

‎src/gateway/control-ui-contract.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ export type ControlUiBootstrapConfig = {
164164
assistantAvatarReason?: string | null;
165165
assistantAgentId?: string;
166166
serverVersion?: string;
167+
/** Exact immutable build serving this Control UI when available. */
168+
serverBuildId?: string;
167169
/**
168170
* Git branch of a source-checkout (non-release) gateway install. Omitted for
169171
* package installs and mainline (main/master) checkouts so the UI only flags

‎src/gateway/control-ui.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { extractOriginalFilename } from "../media/store.js";
3737
import { safeEqualSecret } from "../security/secret-equal.js";
3838
import { AVATAR_MAX_BYTES, resolveAvatarMime } from "../shared/avatar-policy.js";
3939
import { resolveUserPath } from "../utils.js";
40-
import { resolveRuntimeServiceVersion } from "../version.js";
40+
import { resolveRuntimeServiceBuildId, resolveRuntimeServiceVersion } from "../version.js";
4141
import { openGatewayAssistantAvatar, resolveGatewayAssistantAvatar } from "./assistant-avatar.js";
4242
import { DEFAULT_ASSISTANT_IDENTITY, resolveAssistantIdentity } from "./assistant-identity.js";
4343
import { buildAssistantMediaContentDisposition } from "./assistant-media-content-disposition.js";
@@ -1110,6 +1110,10 @@ export async function handleControlUiHttpRequest(
11101110
assistantAvatarReason: avatarMeta.avatarReason,
11111111
...(assistantAgentId ? { assistantAgentId } : {}),
11121112
serverVersion: resolveRuntimeServiceVersion(process.env),
1113+
serverBuildId:
1114+
config?.gateway?.controlUi?.root === undefined
1115+
? (resolveRuntimeServiceBuildId() ?? undefined)
1116+
: undefined,
11131117
devGitBranch: (await resolveDevInstallGitBranch()) ?? undefined,
11141118
localMediaPreviewRoots: [...getAgentScopedMediaLocalRoots(config ?? {}, assistantAgentId)],
11151119
embedSandbox:

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
recordPairedNodeConnection,
1414
} from "../../../infra/device-pairing-node.js";
1515
import { hasMultipleSessionSharingIdentities } from "../../../state/user-profiles.js";
16-
import { resolveRuntimeServiceVersion } from "../../../version.js";
16+
import { resolveRuntimeServiceBuildId, resolveRuntimeServiceVersion } from "../../../version.js";
1717
import { resolveChatAttachmentPolicy } from "../../chat-attachment-policy.js";
1818
import {
1919
listControlUiPluginTabs,
@@ -113,12 +113,20 @@ export async function sendGatewayHello(
113113
requireGatewayAuthGrant: resolvedAuth.mode !== "none",
114114
});
115115
const controlUiWidgetKinds = listControlUiPluginWidgetKinds(scopes);
116+
// A configured UI root can be built independently from the Gateway. Exact
117+
// comparison is authoritative only for the package-owned bundled artifact.
118+
const controlUiBuildSource = context.configSnapshot.gateway?.controlUi?.root
119+
? ("configured" as const)
120+
: ("bundled" as const);
121+
const serverBuildId = controlUiBuildSource === "bundled" ? resolveRuntimeServiceBuildId() : null;
116122
const helloOk = {
117123
type: "hello-ok",
118124
// Admission already verified range overlap; this field reports the server's current protocol.
119125
protocol: PROTOCOL_VERSION,
120126
server: {
121127
version: resolveRuntimeServiceVersion(process.env),
128+
...(serverBuildId ? { buildId: serverBuildId } : {}),
129+
controlUiBuildSource,
122130
connId,
123131
},
124132
features: {

‎src/gateway/server/ws-connection/connect-hello.update-scope.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ vi.mock("./connect-auth-security.js", () => ({
113113
emitGatewayAuthSecurityEvent: emitGatewayAuthSecurityEventMock,
114114
}));
115115

116+
vi.mock("../../../version.js", async (importOriginal) => ({
117+
...(await importOriginal<typeof import("../../../version.js")>()),
118+
resolveRuntimeServiceBuildId: () => "build-a",
119+
}));
120+
116121
import { sendGatewayHello } from "./connect-hello.js";
117122

118123
function makeContext(role: "operator" | "node", scopes: string[]) {
@@ -224,6 +229,18 @@ describe("sendGatewayHello update detail scope", () => {
224229
},
225230
}),
226231
);
232+
expect(helloPayload(context)?.server.buildId).toBe("build-a");
233+
expect(helloPayload(context)?.server.controlUiBuildSource).toBe("bundled");
234+
});
235+
236+
it("omits package build identity for independently built configured UI roots", async () => {
237+
const context = makeContext("operator", ["operator.read"]);
238+
context.configSnapshot = { gateway: { controlUi: { root: "/custom/ui" } } };
239+
240+
await sendGatewayHello(context as never, makeState("operator", ["operator.read"]) as never, {});
241+
242+
expect(helloPayload(context)?.server.buildId).toBeUndefined();
243+
expect(helloPayload(context)?.server.controlUiBuildSource).toBe("configured");
227244
});
228245

229246
it("keeps hello projection and telemetry at effective scopes", async () => {

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
isBrowserCopilotClient,
3131
isEphemeralGatewayClient,
3232
} from "../../../utils/message-channel.js";
33-
import { resolveRuntimeServiceVersion } from "../../../version.js";
33+
import { resolveRuntimeServiceBuildId, resolveRuntimeServiceVersion } from "../../../version.js";
3434
import { verifyAgentRuntimeIdentityToken } from "../../agent-runtime-identity-token.js";
3535
import { buildAuthenticatedPresenceUser } from "../../authenticated-presence-user.js";
3636
import {
@@ -490,8 +490,20 @@ export async function attachAuthenticatedGatewayConnect(
490490
}
491491

492492
if (isWebchatConnect(connectParams)) {
493+
const gatewayBuildId = resolveRuntimeServiceBuildId();
494+
const clientBuildId = connectParams.client.buildId?.trim();
495+
if (
496+
connectParams.client.id === GATEWAY_CLIENT_IDS.CONTROL_UI &&
497+
!context.configSnapshot.gateway?.controlUi?.root &&
498+
gatewayBuildId &&
499+
clientBuildId !== gatewayBuildId
500+
) {
501+
logWsControl.warn(
502+
`control ui build mismatch conn=${connId} clientBuild=${formatForLog(clientBuildId ?? "legacy")} gatewayBuild=${formatForLog(gatewayBuildId)}; reload required`,
503+
);
504+
}
493505
logWsControl.info(
494-
`webchat connected conn=${connId} remote=${remoteAddr ?? "?"} client=${clientLabel} ${connectParams.client.mode} v${connectParams.client.version}`,
506+
`webchat connected conn=${connId} remote=${remoteAddr ?? "?"} client=${clientLabel} ${connectParams.client.mode} v${connectParams.client.version} build=${formatForLog(clientBuildId ?? "legacy")}`,
495507
);
496508
}
497509

0 commit comments

Comments
 (0)