Skip to content

Commit fbf8460

Browse files
authored
refactor(ui): consolidate presence payload parsing (#129029)
* refactor(ui): consolidate presence payload parsing * chore(ui): tighten presence assertion safety baselines
1 parent 2d0ac79 commit fbf8460

6 files changed

Lines changed: 12 additions & 41 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,7 +4062,6 @@ ui/src/components/terminal/terminal-panel-upload.ts 2
40624062
ui/src/components/terminal/terminal-panel.ts 2
40634063
ui/src/components/terminal/terminal-pending-actions.ts 1
40644064
ui/src/components/theme-mode-toggle.ts 1
4065-
ui/src/components/viewer-facepile.ts 2
40664065
ui/src/components/web-awesome.ts 2
40674066
ui/src/components/wizard-step-controls.ts 4
40684067
ui/src/i18n/lib/registry.ts 2
@@ -4228,7 +4227,7 @@ ui/src/pages/custodian/custodian-session-store.ts 1
42284227
ui/src/pages/custodian/custodian-surface.ts 2
42294228
ui/src/pages/custodian/session-lifecycle.ts 1
42304229
ui/src/pages/debug/view.ts 3
4231-
ui/src/pages/devices/devices-page.ts 3
4230+
ui/src/pages/devices/devices-page.ts 1
42324231
ui/src/pages/devices/view-exec-approvals.ts 7
42334232
ui/src/pages/devices/view.ts 5
42344233
ui/src/pages/labs/labs-registry.ts 6
@@ -4243,7 +4242,7 @@ ui/src/pages/new-session/composer.ts 1
42434242
ui/src/pages/new-session/detail-chip.ts 2
42444243
ui/src/pages/new-session/discovery.ts 3
42454244
ui/src/pages/new-session/draft-place-browser.ts 4
4246-
ui/src/pages/new-session/new-session-runtime.ts 3
4245+
ui/src/pages/new-session/new-session-runtime.ts 1
42474246
ui/src/pages/new-session/place-browser.ts 1
42484247
ui/src/pages/new-session/place-facts.ts 1
42494248
ui/src/pages/new-session/preferences.ts 1

‎ui/src/components/viewer-facepile.ts‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { html, nothing } from "lit";
22
import { property } from "lit/decorators.js";
3-
import type { PresenceEntry } from "../api/types.ts";
3+
import { readPresenceEntries } from "../app/user-profile.ts";
44
import {
55
presenceViewerLabel,
66
projectPresenceEntries,
@@ -20,14 +20,6 @@ import {
2020
} from "./person-activity-link.ts";
2121
import "./tooltip.ts";
2222

23-
function readPresenceEntries(value: unknown): PresenceEntry[] {
24-
if (!value || typeof value !== "object") {
25-
return [];
26-
}
27-
const presence = (value as { presence?: unknown }).presence;
28-
return Array.isArray(presence) ? (presence as PresenceEntry[]) : [];
29-
}
30-
3123
function normalized(value: string | null | undefined): string | undefined {
3224
const trimmed = value?.trim();
3325
return trimmed ? trimmed : undefined;
@@ -92,7 +84,7 @@ class ViewerFacepile extends OpenClawLightDomContentsElement {
9284

9385
override render() {
9486
const projection = projectPresenceEntries(
95-
readPresenceEntries(this.presencePayload),
87+
readPresenceEntries(this.presencePayload) ?? [],
9688
this.selfUserId,
9789
this.selfInstanceId,
9890
);

‎ui/src/lib/presence-users.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { PresenceEntry } from "../api/types.ts";
2+
import { readPresenceEntries } from "../app/user-profile.ts";
23

34
export type PresenceViewer = {
45
id: string;
@@ -24,16 +25,6 @@ function firstSorted(values: Iterable<string | null | undefined>): string | unde
2425
.toSorted()[0];
2526
}
2627

27-
function readPresenceEntries(value: unknown): PresenceEntry[] {
28-
if (!value || typeof value !== "object") {
29-
return [];
30-
}
31-
// SAFETY: Gateway snapshots are protocol-validated before reaching UI projections.
32-
const presence = (value as { presence?: unknown }).presence;
33-
// SAFETY: The validated presence array carries PresenceEntry protocol records.
34-
return Array.isArray(presence) ? (presence as PresenceEntry[]) : [];
35-
}
36-
3728
function presenceEntrySortKey(entry: PresenceEntry): string {
3829
return [
3930
normalized(entry.host) ?? "",
@@ -119,7 +110,7 @@ export function projectPresencePayload(
119110
cachedAuthenticatedSelfUserId = authenticatedSelfUserId;
120111
cachedSelfInstanceId = selfInstanceId;
121112
cachedPresenceProjection = projectPresenceViewers(
122-
readPresenceEntries(value),
113+
readPresenceEntries(value) ?? [],
123114
authenticatedSelfUserId,
124115
selfInstanceId,
125116
);

‎ui/src/pages/devices/devices-page.ts‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
type ApplicationGatewaySnapshot,
1212
} from "../../app/context.ts";
1313
import { hasOperatorAdminAccess, hasOperatorPairingAccess } from "../../app/operator-access.ts";
14+
import { readPresenceEntries } from "../../app/user-profile.ts";
1415
import { showConfirmDialog, type ConfirmDialogOptions } from "../../components/confirm-dialog.ts";
1516
import { showSecretRevealDialog } from "../../components/secret-reveal-dialog.ts";
1617
import { renderDocsLink } from "../../components/settings-ui.ts";
@@ -62,12 +63,6 @@ type InventoryRemovalPrompt =
6263
| { kind: "entry"; entry: InventoryRemovalRequest }
6364
| { kind: "stale"; entries: InventoryRemovalRequest[] };
6465

65-
function readPresence(value: unknown): PresenceEntry[] | null {
66-
const presence =
67-
value && typeof value === "object" ? (value as { presence?: unknown }).presence : null;
68-
return Array.isArray(presence) ? (presence as PresenceEntry[]) : null;
69-
}
70-
7166
function presenceConnectivitySignature(entries: PresenceEntry[]): string {
7267
const states = new Map<string, "connected" | "offline">();
7368
for (const entry of entries) {
@@ -154,7 +149,7 @@ class DevicesPage extends OpenClawLightDomElement {
154149
if (this.gateway.gateway !== gateway || this.context.gateway !== gateway) {
155150
return;
156151
}
157-
const presence = event.event === "presence" ? readPresence(event.payload) : null;
152+
const presence = event.event === "presence" ? readPresenceEntries(event.payload) : null;
158153
if (presence) {
159154
const connectivityChanged =
160155
presenceConnectivitySignature(presence) !==
@@ -226,7 +221,7 @@ class DevicesPage extends OpenClawLightDomElement {
226221
snapshot.client &&
227222
(change.identityChanged || change.connectionChanged)
228223
) {
229-
const initialPresence = readPresence(snapshot.hello?.snapshot);
224+
const initialPresence = readPresenceEntries(snapshot.hello?.snapshot);
230225
this.presence = initialPresence ?? [];
231226
void this.loadPresence();
232227
}
@@ -250,7 +245,7 @@ class DevicesPage extends OpenClawLightDomElement {
250245
const snapshot = this.context.gateway.snapshot;
251246
if (!this.gateway.isRouteDataCurrent(data)) {
252247
this.resetServerState(snapshot);
253-
this.presence = readPresence(snapshot.hello?.snapshot) ?? [];
248+
this.presence = readPresenceEntries(snapshot.hello?.snapshot) ?? [];
254249
void this.loadPresence();
255250
this.ensureInitialData();
256251
return;
@@ -261,7 +256,7 @@ class DevicesPage extends OpenClawLightDomElement {
261256
connected: snapshot.phase === "connected",
262257
requestGeneration: this.gateway.epoch,
263258
};
264-
const initialPresence = readPresence(snapshot.hello?.snapshot);
259+
const initialPresence = readPresenceEntries(snapshot.hello?.snapshot);
265260
if (initialPresence) {
266261
this.presence = initialPresence;
267262
}

‎ui/src/pages/new-session/new-session-page.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { selectApplicationSession } from "../../app/agent-selection.ts";
55
import { applicationContext, type ApplicationContext } from "../../app/context.ts";
66
import { beginNativeWindowDragFromTopInset } from "../../app/native-window-drag.ts";
77
import { loadSettings } from "../../app/settings.ts";
8+
import { readPresenceEntries } from "../../app/user-profile.ts";
89
import type { ImageLightboxItem } from "../../components/image-lightbox.ts";
910
import { t } from "../../i18n/index.ts";
1011
import "../../components/web-awesome-popover.ts";
@@ -39,7 +40,6 @@ import {
3940
handleSessionPickerEvent,
4041
isPlaceTopologyEvent,
4142
presenceStateSignature,
42-
readPresenceEntries,
4343
} from "./new-session-runtime.ts";
4444
import { renderProjectChip, resolveProjectChip } from "./project-chip.ts";
4545
import type { SubmissionOutcomeReason } from "./session-placement-recovery-state.ts";

‎ui/src/pages/new-session/new-session-runtime.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ export function isPlaceTopologyEvent(event: string): boolean {
1616
return PLACE_TOPOLOGY_EVENTS.has(event);
1717
}
1818

19-
export function readPresenceEntries(value: unknown): PresenceEntry[] | null {
20-
const presence =
21-
value && typeof value === "object" ? (value as { presence?: unknown }).presence : null;
22-
return Array.isArray(presence) ? (presence as PresenceEntry[]) : null;
23-
}
24-
2519
export function presenceStateSignature(entries: PresenceEntry[]): string {
2620
const states = new Map<string, "connected" | "offline">();
2721
for (const entry of entries) {

0 commit comments

Comments
 (0)