Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(diagnostics): make a UI stall name the code that caused it

Ruben Lütke644b6c5881fd
diff --git a/jarvis/ui/web/frontend/src/lib/uiStallWatch.test.ts b/jarvis/ui/web/frontend/src/lib/uiStallWatch.test.ts
index 15c626eebe..162de713cd 100644
--- a/jarvis/ui/web/frontend/src/lib/uiStallWatch.test.ts
+++ b/jarvis/ui/web/frontend/src/lib/uiStallWatch.test.ts
@@ -132,4 +132,180 @@ describe("watchUiStalls", () => {
stop();
expect(state.disconnected).toBe(1);
});
+
+ describe("long animation frames", () => {
+ /** A long animation frame, as Chromium reports one. */
+ function loaf(
+ duration: number,
+ extra: Partial<{
+ startTime: number;
+ renderStart: number;
+ styleAndLayoutStart: number;
+ blockingDuration: number;
+ scripts: unknown[];
+ }> = {},
+ ): PerformanceEntry {
+ return {
+ entryType: "long-animation-frame",
+ name: "long-animation-frame",
+ startTime: 0,
+ duration,
+ ...extra,
+ } as never;
+ }
+
+ it("names the function and file that blocked the thread", () => {
+ // The whole reason this path exists: `longtask` could only ever say
+ // "unknown/window", which named nothing and left the bug unfixable.
+ const state = installObserver();
+ const report = vi.fn();
+ watchUiStalls(report);
+
+ state.cb?.({
+ getEntries: () => [
+ loaf(2400, {
+ scripts: [
+ {
+ duration: 120,
+ sourceFunctionName: "tick",
+ sourceURL: "http://127.0.0.1:47821/assets/small-abc.js",
+ },
+ {
+ duration: 2200,
+ sourceFunctionName: "handlePaneOutput",
+ sourceURL: "http://127.0.0.1:47821/assets/AgenticTerminal-xY9.js?v=2",
+ invokerType: "event-listener",
+ forcedStyleAndLayoutDuration: 310,
+ },
+ ],
+ }),
+ ],
+ });
+
+ const detail = report.mock.calls[0][0].detail;
+ // The worst script wins, not merely the first one reported.
+ expect(detail).toContain("[email protected]");
+ expect(detail).toContain("2200ms");
+ expect(detail).toContain("via=event-listener");
+ expect(detail).toContain("forced-layout=310ms");
+ expect(detail).toContain("+1more");
+ // The query string is not part of the file's identity.
+ expect(detail).not.toContain("?v=2");
+ });
+
+ it("splits the frame into work, render and layout", () => {
+ // With several terminal panes mounted, "one handler ran long" and "layout
+ // re-ran over a huge DOM" feel identical to the user but need opposite
+ // fixes — so the phase split is the point, not a decoration.
+ const state = installObserver();
+ const report = vi.fn();
+ watchUiStalls(report);
+
+ state.cb?.({
+ getEntries: () => [
+ loaf(3000, {
+ startTime: 1000,
+ renderStart: 3200,
+ styleAndLayoutStart: 3500,
+ blockingDuration: 2950,
+ }),
+ ],
+ });
+
+ const detail = report.mock.calls[0][0].detail;
+ expect(detail).toContain("work=2200ms"); // 3200 - 1000
+ expect(detail).toContain("render=300ms"); // 3500 - 3200
+ expect(detail).toContain("layout=500ms"); // (1000 + 3000) - 3500
+ expect(detail).toContain("blocking=2950ms");
+ });
+
+ it("never sends the invoker string, which can carry DOM ids", () => {
+ const state = installObserver();
+ const report = vi.fn();
+ watchUiStalls(report);
+
+ state.cb?.({
+ getEntries: () => [
+ loaf(2000, {
+ scripts: [
+ {
+ duration: 2000,
+ sourceFunctionName: "onData",
+ sourceURL: "/assets/x.js",
+ invokerType: "event-listener",
+ // A real terminal's uuid arrives here; it is page content.
+ invoker: "DIV#agentic-terminal-59c1e52e-679d-483b.onmessage",
+ },
+ ],
+ }),
+ ],
+ });
+
+ const detail = report.mock.calls[0][0].detail;
+ expect(detail).not.toContain("59c1e52e");
+ expect(detail).not.toContain("agentic-terminal");
+ expect(detail.length).toBeLessThanOrEqual(200);
+ });
+
+ it("prefers the attributed entry when both types fire for one block", () => {
+ const state = installObserver();
+ const report = vi.fn();
+ watchUiStalls(report);
+
+ // The long task is LONGER, so a naive "worst first" would pick it and
+ // throw the attribution away.
+ state.cb?.({
+ getEntries: () => [
+ entry(5000, [{ name: "unknown", containerType: "window" }]),
+ loaf(4000, {
+ scripts: [{ duration: 3900, sourceFunctionName: "reflowAll", sourceURL: "/a/p.js" }],
+ }),
+ ],
+ });
+
+ expect(report).toHaveBeenCalledTimes(1);
+ expect(report.mock.calls[0][0].detail).toContain("[email protected]");
+ expect(report.mock.calls[0][0].blocked_ms).toBe(4000);
+ });
+
+ it("survives a frame with no scripts at all", () => {
+ // A frame blocked purely by layout or GC reports no scripts.
+ const state = installObserver();
+ const report = vi.fn();
+ watchUiStalls(report);
+
+ state.cb?.({ getEntries: () => [loaf(1500)] });
+
+ expect(report).toHaveBeenCalledTimes(1);
+ expect(typeof report.mock.calls[0][0].detail).toBe("string");
+ });
+
+ it("still watches long tasks where animation frames are unsupported", () => {
+ // Older WebView builds have `longtask` but not `long-animation-frame`;
+ // losing the reporter entirely there would be a regression.
+ const state: { cb: ObserverCallback | null; observed: string[] } = {
+ cb: null,
+ observed: [],
+ };
+ class PartialObserver {
+ constructor(fn: ObserverCallback) {
+ state.cb = fn;
+ }
+ observe(options: { type: string }) {
+ if (options.type === "long-animation-frame") {
+ throw new TypeError("unsupported entry type");
+ }
+ state.observed.push(options.type);
+ }
+ disconnect() {}
+ }
+ vi.stubGlobal("PerformanceObserver", PartialObserver);
+ const report = vi.fn();
+ watchUiStalls(report);
+
+ expect(state.observed).toEqual(["longtask"]);
+ state.cb?.({ getEntries: () => [entry(2000)] });
+ expect(report).toHaveBeenCalledTimes(1);
+ });
+ });
});
diff --git a/jarvis/ui/web/frontend/src/lib/uiStallWatch.ts b/jarvis/ui/web/frontend/src/lib/uiStallWatch.ts
index 8cb4ef6381..88ff202b97 100644
--- a/jarvis/ui/web/frontend/src/lib/uiStallWatch.ts
+++ b/jarvis/ui/web/frontend/src/lib/uiStallWatch.ts
@@ -41,8 +41,98 @@ const REPORT_OVER_MS = 1000;
*/
const MIN_GAP_MS = 60_000;
+/** Upper bound on the attribution string, so one report stays one log line. */
+const MAX_DETAIL = 200;
+
+/**
+ * One script that ran inside a long animation frame.
+ *
+ * Not in TypeScript's DOM types yet — the Long Animation Frames API is newer
+ * than the bundled lib. WebView2 is Chromium, so it is present in the app even
+ * though the type definitions are not.
+ */
+interface LoafScript {
+ duration?: number;
+ sourceURL?: string;
+ sourceFunctionName?: string;
+ invokerType?: string;
+ forcedStyleAndLayoutDuration?: number;
+}
+
+interface LoafEntry extends PerformanceEntry {
+ renderStart?: number;
+ styleAndLayoutStart?: number;
+ blockingDuration?: number;
+ scripts?: LoafScript[];
+}
+
+/** ``…/assets/AgenticIdeView-xTOr0.js?x=1`` → ``AgenticIdeView-xTOr0.js``. */
+function fileOf(url: string | undefined): string {
+ if (!url) return "";
+ const withoutQuery = url.split(/[?#]/)[0];
+ const last = withoutQuery.split("/").pop() || "";
+ return last.slice(0, 60);
+}
+
+/**
+ * Name the code that blocked the thread, from a long *animation frame*.
+ *
+ * This is the field that decides the whole diagnosis, and the older `longtask`
+ * entry cannot supply it: its `attribution` can only ever point at a container
+ * (an iframe, or "window" for everything in the page itself), which is why
+ * every stall this app has recorded reads `unknown/window` and names nothing.
+ *
+ * A long animation frame instead reports the individual scripts that ran, with
+ * function name and source file, plus where the frame's time actually went —
+ * script, style-and-layout, or the rest. That distinction matters here: with
+ * several terminal panes mounted, "one handler ran for two seconds" and "layout
+ * re-ran over a huge DOM" look identical to the user and need opposite fixes.
+ *
+ * Privacy is unchanged from the `longtask` path: code identity only — function
+ * name, bundle file name, and the fixed `invokerType` enum. The `invoker`
+ * string is deliberately NOT sent, because it can carry DOM ids (a terminal's
+ * uuid, for instance), and no page content or user text belongs in a log line.
+ */
+function describeLoaf(entry: LoafEntry): string {
+ const parts: string[] = [];
+ const scripts = [...(entry.scripts ?? [])].sort(
+ (a, b) => (b.duration ?? 0) - (a.duration ?? 0),
+ );
+ const worst = scripts[0];
+ if (worst) {
+ const where = [worst.sourceFunctionName, fileOf(worst.sourceURL)]
+ .filter(Boolean)
+ .join("@");
+ parts.push(`script=${where || "anonymous"} ${Math.round(worst.duration ?? 0)}ms`);
+ if (worst.invokerType) parts.push(`via=${worst.invokerType}`);
+ const forced = Math.round(worst.forcedStyleAndLayoutDuration ?? 0);
+ if (forced > 0) parts.push(`forced-layout=${forced}ms`);
+ if (scripts.length > 1) parts.push(`+${scripts.length - 1}more`);
+ }
+ // Where the frame's time went. renderStart/styleAndLayoutStart are absolute
+ // timestamps; 0 means that phase never began, so it is not a duration.
+ const { startTime, duration, renderStart, styleAndLayoutStart } = entry;
+ if (renderStart) {
+ parts.push(`work=${Math.round(renderStart - startTime)}ms`);
+ const renderEnd = startTime + duration;
+ if (styleAndLayoutStart) {
+ parts.push(`render=${Math.round(styleAndLayoutStart - renderStart)}ms`);
+ parts.push(`layout=${Math.round(renderEnd - styleAndLayoutStart)}ms`);
+ } else {
+ parts.push(`render=${Math.round(renderEnd - renderStart)}ms`);
+ }
+ }
+ if (entry.blockingDuration) {
+ parts.push(`blocking=${Math.round(entry.blockingDuration)}ms`);
+ }
+ return parts.join(" ").slice(0, MAX_DETAIL);
+}
+
/** Fixed labels only — no page content, no user text, ever leaves here. */
function describe(entry: PerformanceEntry): string {
+ if (entry.entryType === "long-animation-frame") {
+ return describeLoaf(entry as LoafEntry);
+ }
const attribution = (entry as PerformanceEntry & {
attribution?: Array<{ name?: string; containerType?: string; containerName?: string }>;
}).attribution;
@@ -82,24 +172,45 @@ export function watchUiStalls(
let observer: PerformanceObserver;
try {
observer = new PerformanceObserver((list) => {
- for (const entry of list.getEntries()) {
- if (entry.duration < REPORT_OVER_MS) continue;
- const now = Date.now();
- if (now - lastReportAt < MIN_GAP_MS) continue;
- lastReportAt = now;
- report({
- blocked_ms: Math.round(entry.duration),
- // The route the app is on, not its contents — enough to tell a stall
- // in the Agentic IDE apart from one anywhere else.
- at: (location.hash || location.pathname || "").slice(0, 60),
- panes: paneCount(),
- detail: describe(entry),
- });
- }
+ const blocking = list.getEntries().filter((e) => e.duration >= REPORT_OVER_MS);
+ if (!blocking.length) return;
+ const now = Date.now();
+ if (now - lastReportAt < MIN_GAP_MS) return;
+ // One block can surface as both a long task and a long animation frame.
+ // Report once, choosing the entry that can actually name the culprit —
+ // otherwise the attributed version loses a coin toss to the useless one.
+ const best = blocking.sort((a, b) => {
+ const named =
+ Number(b.entryType === "long-animation-frame") -
+ Number(a.entryType === "long-animation-frame");
+ return named !== 0 ? named : b.duration - a.duration;
+ })[0];
+ lastReportAt = now;
+ report({
+ blocked_ms: Math.round(best.duration),
+ // The route the app is on, not its contents — enough to tell a stall
+ // in the Agentic IDE apart from one anywhere else.
+ at: (location.hash || location.pathname || "").slice(0, 60),
+ panes: paneCount(),
+ detail: describe(best),
+ });
});
- observer.observe({ type: "longtask", buffered: true });
+ // Long animation frames first: they carry the script attribution. `longtask`
+ // stays because it is the older, more widely supported type and it also
+ // catches a block that never reached a rendering update. An engine missing
+ // either one throws only for that one.
+ let watching = 0;
+ for (const type of ["long-animation-frame", "longtask"]) {
+ try {
+ observer.observe({ type, buffered: true });
+ watching += 1;
+ } catch {
+ // This engine lacks this entry type; the other may still work.
+ }
+ }
+ if (watching === 0) throw new TypeError("no supported stall entry type");
} catch {
- // Entry type unsupported in this engine — nothing to watch, nothing broken.
+ // Nothing observable in this engine — nothing to watch, nothing broken.
return () => {};
}