Skip to content

Commit 826786b

Browse files
committed
feat: add control UI responsiveness diagnostics
1 parent fbf9132 commit 826786b

6 files changed

Lines changed: 303 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Docs: https://docs.openclaw.ai
1212
### Changes
1313

1414
- Gateway/startup: keep model-catalog test helpers, run-session lookup code, QR pairing helpers, and TypeBox memory-tool schema construction out of hot startup import paths, reducing default gateway benchmark plugin-load and memory pressure.
15+
- Control UI/performance: record browser long animation frame or long task entries in the debug event log when supported, making slow dashboard renders easier to attribute from the UI.
1516
- Channels/streaming: add unified `streaming.mode: "progress"` drafts with auto single-word status labels and shared progress configuration across Discord, Telegram, Matrix, Slack, and Microsoft Teams.
1617
- Slack/streaming: add `streaming.progress.render: "rich"` for Block Kit progress drafts backed by structured progress line data.
1718
- Slack/streaming: keep the newest rich progress lines when Block Kit limits trim long progress drafts. Thanks @vincentkoc.

‎docs/web/control-ui.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ Imported themes are stored only in the current browser profile. They are not wri
127127
</Accordion>
128128
<Accordion title="Debug, logs, update">
129129
- Debug: status/health/models snapshots + event log + manual RPC calls (`status`, `health`, `models.list`).
130+
- The event log includes Control UI refresh/RPC timings plus browser responsiveness entries for long animation frames or long tasks when the browser exposes those PerformanceObserver entry types.
130131
- Logs: live tail of gateway file logs with filter/export (`logs.tail`).
131132
- Update: run a package/git update + restart (`update.run`) with a restart report, then poll `update.status` after reconnect to verify the running gateway version.
132133

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
syncTabWithLocation,
1616
syncThemeWithSettings,
1717
} from "./app-settings.ts";
18+
import { startControlUiResponsivenessObserver } from "./control-ui-performance.ts";
1819
import { loadControlUiBootstrapConfig } from "./controllers/control-ui-bootstrap.ts";
1920
import type { Tab } from "./navigation.ts";
2021

@@ -52,6 +53,7 @@ type LifecycleHost = {
5253
chatScrollTimeout?: number | null;
5354
logsScrollFrame?: number | null;
5455
controlUiTabPaintSeq?: number;
56+
controlUiResponsivenessObserver?: { disconnect: () => void } | null;
5557
popStateHandler: () => void;
5658
topbarObserver: ResizeObserver | null;
5759
};
@@ -77,6 +79,9 @@ export function handleConnected(host: LifecycleHost) {
7779
if (host.tab === "debug") {
7880
startDebugPolling(host as unknown as Parameters<typeof startDebugPolling>[0]);
7981
}
82+
host.controlUiResponsivenessObserver ??= startControlUiResponsivenessObserver(
83+
host as unknown as Parameters<typeof startControlUiResponsivenessObserver>[0],
84+
);
8085
}
8186

8287
export function handleFirstUpdated(host: LifecycleHost) {
@@ -120,6 +125,8 @@ export function handleDisconnected(host: LifecycleHost) {
120125
detachThemeListener(host as unknown as Parameters<typeof detachThemeListener>[0]);
121126
host.topbarObserver?.disconnect();
122127
host.topbarObserver = null;
128+
host.controlUiResponsivenessObserver?.disconnect();
129+
host.controlUiResponsivenessObserver = null;
123130
}
124131

125132
export function handleUpdated(host: LifecycleHost, changed: Map<PropertyKey, unknown>) {

‎ui/src/ui/app.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ export class OpenClawApp extends LitElement {
564564
private logsPollInterval: number | null = null;
565565
private debugPollInterval: number | null = null;
566566
private logsScrollFrame: number | null = null;
567+
private controlUiResponsivenessObserver: { disconnect: () => void } | null = null;
567568
private toolStreamById = new Map<string, ToolStreamEntry>();
568569
private toolStreamOrder: string[] = [];
569570
refreshSessionsAfterChat = new Set<string>();
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import {
3+
recordControlUiPerformanceEvent,
4+
startControlUiResponsivenessObserver,
5+
} from "./control-ui-performance.ts";
6+
7+
const originalPerformanceObserver = globalThis.PerformanceObserver;
8+
9+
type ObserverCallback = ConstructorParameters<typeof PerformanceObserver>[0];
10+
11+
function installPerformanceObserverMock(options: {
12+
supportedEntryTypes: string[];
13+
observe?: (options: PerformanceObserverInit) => void;
14+
}) {
15+
let callback: ObserverCallback | null = null;
16+
const disconnect = vi.fn();
17+
class MockPerformanceObserver {
18+
static supportedEntryTypes = options.supportedEntryTypes;
19+
constructor(nextCallback: ObserverCallback) {
20+
callback = nextCallback;
21+
}
22+
observe(observeOptions: PerformanceObserverInit) {
23+
options.observe?.(observeOptions);
24+
}
25+
disconnect() {
26+
disconnect();
27+
}
28+
}
29+
Object.defineProperty(globalThis, "PerformanceObserver", {
30+
configurable: true,
31+
value: MockPerformanceObserver,
32+
});
33+
return {
34+
disconnect,
35+
emit(entries: PerformanceEntry[]) {
36+
callback?.(
37+
{
38+
getEntries: () => entries,
39+
} as PerformanceObserverEntryList,
40+
{} as PerformanceObserver,
41+
);
42+
},
43+
};
44+
}
45+
46+
function createHost() {
47+
return {
48+
tab: "chat" as const,
49+
eventLog: [] as Array<{ payload: Record<string, unknown> }>,
50+
eventLogBuffer: [] as Array<{ payload: Record<string, unknown> }>,
51+
};
52+
}
53+
54+
afterEach(() => {
55+
Object.defineProperty(globalThis, "PerformanceObserver", {
56+
configurable: true,
57+
value: originalPerformanceObserver,
58+
});
59+
});
60+
61+
describe("recordControlUiPerformanceEvent", () => {
62+
it("keeps the performance event buffer bounded", () => {
63+
const host = createHost();
64+
65+
for (let i = 0; i < 260; i += 1) {
66+
recordControlUiPerformanceEvent(host, "control-ui.test", { i }, { console: false });
67+
}
68+
69+
expect(host.eventLogBuffer).toHaveLength(250);
70+
expect(host.eventLogBuffer[0]?.payload).toEqual({ i: 259 });
71+
expect(host.eventLogBuffer.at(-1)?.payload).toEqual({ i: 10 });
72+
});
73+
});
74+
75+
describe("startControlUiResponsivenessObserver", () => {
76+
it("records long animation frames with script attribution", () => {
77+
const observe = vi.fn();
78+
const mock = installPerformanceObserverMock({
79+
supportedEntryTypes: ["longtask", "long-animation-frame"],
80+
observe,
81+
});
82+
const host = createHost();
83+
84+
const observer = startControlUiResponsivenessObserver(host);
85+
mock.emit([
86+
{
87+
name: "long-frame",
88+
startTime: 12.4,
89+
duration: 83.6,
90+
blockingDuration: 42.2,
91+
scripts: [
92+
{
93+
duration: 12.1,
94+
sourceURL: "http://localhost/assets/a.js?token=redacted",
95+
},
96+
{
97+
duration: 50.8,
98+
invoker: "event-listener",
99+
sourceURL: "http://localhost/assets/app.js?token=redacted#hash",
100+
sourceFunctionName: "renderApp",
101+
},
102+
],
103+
} as unknown as PerformanceEntry,
104+
]);
105+
observer?.disconnect();
106+
107+
expect(observe).toHaveBeenCalledWith({ type: "long-animation-frame", buffered: true });
108+
expect(mock.disconnect).toHaveBeenCalledOnce();
109+
expect(host.eventLogBuffer).toEqual([
110+
expect.objectContaining({
111+
event: "control-ui.long-animation-frame",
112+
payload: expect.objectContaining({
113+
tab: "chat",
114+
name: "long-frame",
115+
startTimeMs: 12,
116+
durationMs: 84,
117+
blockingDurationMs: 42,
118+
scriptCount: 2,
119+
topScript: {
120+
durationMs: 51,
121+
invoker: "event-listener",
122+
sourceUrl: "/assets/app.js",
123+
sourceFunctionName: "renderApp",
124+
},
125+
}),
126+
}),
127+
]);
128+
});
129+
130+
it("falls back to long task entries when long animation frames are unavailable", () => {
131+
const observe = vi.fn();
132+
const mock = installPerformanceObserverMock({
133+
supportedEntryTypes: ["longtask"],
134+
observe,
135+
});
136+
const host = createHost();
137+
138+
startControlUiResponsivenessObserver(host);
139+
mock.emit([
140+
{
141+
name: "self",
142+
startTime: 5,
143+
duration: 51,
144+
} as unknown as PerformanceEntry,
145+
{
146+
name: "small",
147+
startTime: 10,
148+
duration: 49,
149+
} as unknown as PerformanceEntry,
150+
]);
151+
152+
expect(observe).toHaveBeenCalledWith({ type: "longtask", buffered: true });
153+
expect(host.eventLogBuffer).toEqual([
154+
expect.objectContaining({
155+
event: "control-ui.longtask",
156+
payload: expect.objectContaining({
157+
name: "self",
158+
durationMs: 51,
159+
}),
160+
}),
161+
]);
162+
});
163+
164+
it("returns null when responsiveness entries are unsupported or observe fails", () => {
165+
installPerformanceObserverMock({ supportedEntryTypes: [] });
166+
expect(startControlUiResponsivenessObserver(createHost())).toBeNull();
167+
168+
installPerformanceObserverMock({
169+
supportedEntryTypes: ["longtask"],
170+
observe: () => {
171+
throw new Error("unsupported");
172+
},
173+
});
174+
expect(startControlUiResponsivenessObserver(createHost())).toBeNull();
175+
});
176+
});

‎ui/src/ui/control-ui-performance.ts‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ export type ControlUiRefreshRun = {
2121

2222
const EVENT_LOG_LIMIT = 250;
2323
const SLOW_RPC_MS = 1_000;
24+
const RESPONSIVENESS_ENTRY_MS = 50;
25+
26+
type ControlUiResponsivenessObserver = {
27+
disconnect: () => void;
28+
};
29+
30+
type PerformanceObserverCtor = {
31+
readonly supportedEntryTypes?: readonly string[];
32+
new (callback: PerformanceObserverCallback): PerformanceObserver;
33+
};
34+
35+
type LongAnimationFrameScriptTiming = {
36+
duration?: number;
37+
invoker?: string;
38+
sourceURL?: string;
39+
sourceFunctionName?: string;
40+
};
41+
42+
type ResponsivenessPerformanceEntry = PerformanceEntry & {
43+
blockingDuration?: number;
44+
scripts?: LongAnimationFrameScriptTiming[];
45+
};
2446

2547
export function controlUiNowMs(): number {
2648
return typeof performance !== "undefined" && typeof performance.now === "function"
@@ -169,3 +191,98 @@ export function recordControlUiRpcTiming(
169191
{ warn },
170192
);
171193
}
194+
195+
function getPerformanceObserverCtor(): PerformanceObserverCtor | null {
196+
const observer = globalThis.PerformanceObserver;
197+
return typeof observer === "function" ? (observer as PerformanceObserverCtor) : null;
198+
}
199+
200+
function normalizeScriptSourceUrl(sourceUrl: string | undefined): string | undefined {
201+
if (!sourceUrl) {
202+
return undefined;
203+
}
204+
try {
205+
const url = new URL(sourceUrl, globalThis.location?.href);
206+
return url.pathname;
207+
} catch {
208+
return sourceUrl.split(/[?#]/, 1)[0];
209+
}
210+
}
211+
212+
function getTopLongAnimationFrameScript(
213+
scripts: LongAnimationFrameScriptTiming[] | undefined,
214+
): Record<string, unknown> | undefined {
215+
if (!Array.isArray(scripts) || scripts.length === 0) {
216+
return undefined;
217+
}
218+
let topScript: LongAnimationFrameScriptTiming | undefined;
219+
for (const script of scripts) {
220+
if (!topScript || (script.duration ?? 0) > (topScript.duration ?? 0)) {
221+
topScript = script;
222+
}
223+
}
224+
if (!topScript) {
225+
return undefined;
226+
}
227+
return {
228+
durationMs: roundedControlUiDurationMs(topScript.duration ?? 0),
229+
invoker: topScript.invoker,
230+
sourceUrl: normalizeScriptSourceUrl(topScript.sourceURL),
231+
sourceFunctionName: topScript.sourceFunctionName,
232+
};
233+
}
234+
235+
function recordResponsivenessEntry(
236+
host: ControlUiPerformanceHost,
237+
entryType: "long-animation-frame" | "longtask",
238+
entry: ResponsivenessPerformanceEntry,
239+
) {
240+
const durationMs = roundedControlUiDurationMs(entry.duration);
241+
if (durationMs < RESPONSIVENESS_ENTRY_MS) {
242+
return;
243+
}
244+
recordControlUiPerformanceEvent(
245+
host,
246+
`control-ui.${entryType}`,
247+
{
248+
tab: host.tab,
249+
name: entry.name,
250+
startTimeMs: roundedControlUiDurationMs(entry.startTime),
251+
durationMs,
252+
blockingDurationMs:
253+
typeof entry.blockingDuration === "number"
254+
? roundedControlUiDurationMs(entry.blockingDuration)
255+
: undefined,
256+
scriptCount: Array.isArray(entry.scripts) ? entry.scripts.length : undefined,
257+
topScript: getTopLongAnimationFrameScript(entry.scripts),
258+
},
259+
{ warn: true },
260+
);
261+
}
262+
263+
export function startControlUiResponsivenessObserver(
264+
host: ControlUiPerformanceHost,
265+
): ControlUiResponsivenessObserver | null {
266+
const Observer = getPerformanceObserverCtor();
267+
const supportedEntryTypes = Observer?.supportedEntryTypes ?? [];
268+
const entryType = supportedEntryTypes.includes("long-animation-frame")
269+
? "long-animation-frame"
270+
: supportedEntryTypes.includes("longtask")
271+
? "longtask"
272+
: null;
273+
if (!Observer || !entryType) {
274+
return null;
275+
}
276+
277+
const observer = new Observer((list) => {
278+
for (const entry of list.getEntries() as ResponsivenessPerformanceEntry[]) {
279+
recordResponsivenessEntry(host, entryType, entry);
280+
}
281+
});
282+
try {
283+
observer.observe({ type: entryType, buffered: true });
284+
} catch {
285+
return null;
286+
}
287+
return observer;
288+
}

0 commit comments

Comments
 (0)