Multiplayer agent harness for work.
perf: keep multiview streaming responsive (#1620)
Joshua France2ef6a5ccb957
diff --git a/plugins/web-ui/src/chat.ts b/plugins/web-ui/src/chat.tsindex 8ec2325f2..433fb4541 100644--- a/plugins/web-ui/src/chat.ts+++ b/plugins/web-ui/src/chat.ts@@ -1395,6 +1395,10 @@ export function createChatSurface(function drawActiveChat(agent = chatState.agent, opts: { forceScroll?: boolean } = {}): void {if (!agent || agent !== chatState.agent || !chatState.host || appState.currentView !== "chats") return;+ if (!ctx.visible()) {+ postCurrentPaneState();+ return;+ }transcriptViewport.beforeRender();const currentMessages = visibleMessages(agent);if (preserveConnectionScroll) {diff --git a/plugins/web-ui/src/shell.css b/plugins/web-ui/src/shell.cssindex 8f8f7aa55..36a730737 100644--- a/plugins/web-ui/src/shell.css+++ b/plugins/web-ui/src/shell.css@@ -1688,7 +1688,7 @@ a.chat-row-open {flex-direction: column;align-items: flex-end;}-.message-stack .user-row:not(:has(~ .user-row)) {+.message-stack .user-row.latest-prompt {position: sticky;top: var(--chat-sticky-top, 0px);z-index: 3;@@ -1704,7 +1704,7 @@ a.chat-row-open {top: auto;}-.message-stack .user-row:not(:has(~ .user-row)) > .user-bubble {+.message-stack .user-row.latest-prompt > .user-bubble {pointer-events: auto;transition: box-shadow 120ms ease;}@@ -1890,6 +1890,7 @@ button.subagent-chip:hover {}}.assistant-row {+ contain: layout style;display: block;}.assistant-body {@@ -2021,14 +2022,14 @@ qm-markdown .tok-in {.user-bubble :is(markdown-block, qm-markdown) p {line-height: inherit;}-.message-stack .user-row:not(:has(~ .user-row)):not(.pin-expanded) .user-bubble > .pin-content {+.message-stack .user-row.latest-prompt:not(.pin-expanded) .user-bubble > .pin-content {display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 6;overflow: hidden;}.message-stack- .user-row:not(:has(~ .user-row)):not(.pin-expanded)+ .user-row.latest-prompt:not(.pin-expanded).user-bubble> .pin-content:has(code-block, pre, table, img, svg, math, .katex-display, video, iframe) {max-height: 6lh;@@ -2065,7 +2066,7 @@ qm-markdown .tok-in {pointer-events: auto;cursor: pointer;}-.message-stack .user-row:not(:has(~ .user-row)) > .user-bubble > .pin-toggle:not([hidden]) {+.message-stack .user-row.latest-prompt > .user-bubble > .pin-toggle:not([hidden]) {display: flex;align-items: center;gap: 2px;@@ -4702,15 +4703,20 @@ summary.stopped-head:focus-visible {justify-content: center;}.wl-swell {+ display: inline-block;+ flex: none;color: var(--muted-foreground);overflow: hidden;}.wl-swell .wl-row {+ display: block;+ overflow: visible;+ will-change: transform;animation: wl-swell-scroll 1.6s linear infinite;}@keyframes wl-swell-scroll {to {- transform: translateX(-24px);+ transform: translateX(var(--wl-shift));}}@media (prefers-reduced-motion: reduce) {@@ -8595,6 +8601,7 @@ h3.ambient-field-label {.split-pane-content {container: split-pane / size;+ contain: layout style;position: relative;width: 100%;height: 100%;diff --git a/plugins/web-ui/src/split.ts b/plugins/web-ui/src/split.tsindex d15ab6b0d..b943d3972 100644--- a/plugins/web-ui/src/split.ts+++ b/plugins/web-ui/src/split.ts@@ -928,6 +928,7 @@ class PaneContent implements IContentRenderer {private loaded = false;private disposed = false;private redrawOnResize: Array<() => void> = [];+ private visible = false;constructor() {this.element = document.createElement("div");@@ -947,13 +948,13 @@ class PaneContent implements IContentRenderer {ownsUrl: false,container: () => this.chatEl,claimContainer: () => this.chatEl,- visible: () => splitState.active && appState.currentView === "chats",+ visible: () => splitState.active && appState.currentView === "chats" && this.visible,density: () => this.density,onDensityChange: (handler) => this.redrawOnResize.push(handler),ensureDeliveryStream,onState: (paneState) => {notePaneSession(this.panelId, paneState.sessionId, paneState.threadRef);- refreshHeaders();+ notifyPanesChanged();},onExpand: () => {const panel = dockApi?.getPanel(this.panelId);@@ -965,6 +966,7 @@ class PaneContent implements IContentRenderer {init(p: GroupPanelPartInitParameters): void {this.panelId = p.api.id;+ this.visible = p.api.isVisible;this.panel = p.containerApi.getPanel(p.api.id) ?? null;this.params = (p.params ?? {}) as PaneParams;this.element.dataset.paneId = this.panelId;@@ -974,12 +976,14 @@ class PaneContent implements IContentRenderer {this.syncZones();p.api.onDidDimensionsChange(() => this.syncDensity());p.api.onDidVisibilityChange((e) => {+ this.visible = e.isVisible;if (!e.isVisible) return;if (!this.loaded) {void this.load();return;}this.syncDensity();+ this.conversation?.redraw();this.conversation?.scrollToBottom();});if (p.api.isVisible) void this.load();diff --git a/plugins/web-ui/src/transcript-viewport.ts b/plugins/web-ui/src/transcript-viewport.tsindex 9ba9a5308..3e530b6b2 100644--- a/plugins/web-ui/src/transcript-viewport.ts+++ b/plugins/web-ui/src/transcript-viewport.ts@@ -64,7 +64,7 @@ export function createTranscriptViewport() {function clearPrompt(): void {if (content) content.scrollTop = 0;- prompt?.classList.remove("stuck", "sticky-disabled", "pin-expanded", "pin-condensed");+ prompt?.classList.remove("stuck", "sticky-disabled", "pin-expanded", "pin-condensed", "latest-prompt");prompt?.style.removeProperty("--pin-expanded-max");prompt?.style.removeProperty("--pin-rest-height");prompt?.style.removeProperty("--pin-content-max");@@ -305,7 +305,8 @@ export function createTranscriptViewport() {if (stack) observer?.observe(stack);}const nextPins = scroller?.querySelector<HTMLElement>(".pinned-strip") ?? null;- const nextPrompt = scroller?.querySelector<HTMLElement>(".message-stack .user-row:not(:has(~ .user-row))") ?? null;+ const prompts = stack?.querySelectorAll<HTMLElement>(".user-row");+ const nextPrompt = prompts?.item(prompts.length - 1) ?? null;if (pins !== nextPins) {changed = true;if (pins) observer?.unobserve(pins);@@ -320,6 +321,7 @@ export function createTranscriptViewport() {promptKey = prompt?.dataset.index;if (prompt) observer?.observe(prompt);}+ prompt?.classList.add("latest-prompt");const nextContent = prompt?.querySelector<HTMLElement>(".pin-content") ?? null;if (content !== nextContent) {changed = true;diff --git a/plugins/web-ui/src/ui.ts b/plugins/web-ui/src/ui.tsindex a0180f6ea..173c72803 100644--- a/plugins/web-ui/src/ui.ts+++ b/plugins/web-ui/src/ui.ts@@ -1,3 +1,4 @@+import { unsafeSVG } from "lit/directives/unsafe-svg.js";import { html, nothing, type TemplateResult } from "lit";import { live } from "lit/directives/live.js";import { Check, ChevronDown, Download, createElement, type IconNode } from "lucide";@@ -20,20 +21,28 @@ export function waveLoader(o: { width?: number; height?: number; viewBox?: string; label?: string; cls?: string } = {},): TemplateResult {const width = o.width ?? 24.5;- return html`<svg+ const height = o.height ?? width * 1.25;+ const viewBox = o.viewBox ?? SWELL_VIEWBOX;+ const [, , viewWidth, viewHeight] = viewBox.split(/\s+/).map(Number);+ const shift = -24 * Math.min(width / viewWidth!, height / viewHeight!);+ return html`<spanclass="wl wl-swell ${o.cls ?? ""}"- width=${width}- height=${o.height ?? width * 1.25}- viewBox=${o.viewBox ?? SWELL_VIEWBOX}- fill="none"+ style=${`width:${width}px;height:${height}px;--wl-shift:${shift}px`}role="img"aria-label=${o.label ?? "Loading"}- xmlns="http://www.w3.org/2000/svg">- <g class="wl-row">+ <svg+ class="wl-row"+ width=${width}+ height=${height}+ viewBox=${viewBox}+ fill="none"+ aria-hidden="true"+ xmlns="http://www.w3.org/2000/svg"+ ><path d=${SWELL_PATH} fill="none" stroke="currentColor" stroke-linecap="round" stroke-width="2.6" />- </g>- </svg>`;+ </svg>+ </span>`;}export function workingWave(): TemplateResult {@@ -196,8 +205,20 @@ export function modelMark(key: string, size = 16): TemplateResult | null {</svg>`;}-export function icon(node: IconNode, size = 18): SVGElement {- const el = createElement(node, {+const iconTemplates = new WeakMap<IconNode, Map<number, TemplateResult>>();++export function icon(node: IconNode, size = 18): TemplateResult {+ let sizes = iconTemplates.get(node);+ if (!sizes) iconTemplates.set(node, (sizes = new Map()));+ const cached = sizes.get(size);+ if (cached) return cached;+ const template = html`${unsafeSVG(iconElement(node, size).outerHTML)}`;+ sizes.set(size, template);+ return template;+}++function iconElement(node: IconNode, size: number): SVGElement {+ return createElement(node, {class: "icon",width: size,height: size,@@ -205,7 +226,6 @@ export function icon(node: IconNode, size = 18): SVGElement {focusable: "false","stroke-width": 1.9,});- return el;}export function fieldSelect(props: {@@ -436,7 +456,7 @@ export function setFormMenuValue(control: HTMLElement | null, value: string, labconst activeOption = Array.from(control.querySelectorAll<HTMLButtonElement>(".menu-option")).find((option) =>option.classList.contains("active"),);- if (activeOption) activeOption.append(icon(Check, 15));+ if (activeOption) activeOption.append(iconElement(Check, 15));}export function chipBadge(diff --git a/plugins/web-ui/test/icon-pointer-events.test.ts b/plugins/web-ui/test/icon-pointer-events.test.tsindex b6068d947..262ff3135 100644--- a/plugins/web-ui/test/icon-pointer-events.test.ts+++ b/plugins/web-ui/test/icon-pointer-events.test.ts@@ -42,8 +42,8 @@ for (const [label, glyph] of [render(view(), host);const after = button.querySelector("svg")!;assert.equal(host.querySelector("button"), button);- assert.notEqual(after, before);- assert.equal(before.isConnected, false);+ assert.equal(after, before);+ assert.equal(before.isConnected, true);assert.equal(dom.window.getComputedStyle(after).pointerEvents, "none");for (const child of after.children) assert.equal(dom.window.getComputedStyle(child).pointerEvents, "none");assert.notEqual(dom.window.getComputedStyle(button).pointerEvents, "none");diff --git a/plugins/web-ui/test/pinned-composer-polish.test.ts b/plugins/web-ui/test/pinned-composer-polish.test.tsindex 516cfa392..9d7aba790 100644--- a/plugins/web-ui/test/pinned-composer-polish.test.ts+++ b/plugins/web-ui/test/pinned-composer-polish.test.ts@@ -7,7 +7,7 @@ const chat = readFileSync(new URL("../src/chat.ts", import.meta.url), "utf8");test("transcript top spacing and prompt gap stay compact at every density", () => {const values = [...css.matchAll(/--chat-scroll-pad-top: (\d+)px/g)].map((m) => Number(m[1]));assert.ok(values.length >= 3 && values.every((v) => v === 8));- const prompt = css.match(/\.message-stack \.user-row:not\(:has\(~ \.user-row\)\) \{[^}]*\}/)?.[0] ?? "";+ const prompt = css.match(/\.message-stack \.user-row\.latest-prompt \{[^}]*\}/)?.[0] ?? "";assert.match(prompt, /padding-top: 8px;/);assert.match(prompt, /margin-top: -8px;/);assert.match(prompt, /margin-bottom: 12px;/);@@ -54,7 +54,7 @@ test("queued cards tuck beneath the next card just as the queue tucks beneath th});test("collapsed prompt content uses a readable six-line cutoff", () => {- const selector = ".message-stack .user-row:not(:has(~ .user-row)):not(.pin-expanded) .user-bubble > .pin-content";+ const selector = ".message-stack .user-row.latest-prompt:not(.pin-expanded) .user-bubble > .pin-content";const rule = css.slice(css.indexOf(`${selector} {`)).split("}")[0] ?? "";assert.match(rule, /display: -webkit-box;/);assert.match(rule, /-webkit-box-orient: vertical;/);diff --git a/plugins/web-ui/test/pinned-prompt-collapse.test.ts b/plugins/web-ui/test/pinned-prompt-collapse.test.tsindex 300e42653..ac1e29b0f 100644--- a/plugins/web-ui/test/pinned-prompt-collapse.test.ts+++ b/plugins/web-ui/test/pinned-prompt-collapse.test.ts@@ -407,7 +407,7 @@ test("the condensed strip keeps the resting line layout, with a scroll-driven he);const rest =css.match(- /\.message-stack \.user-row:not\(:has\(~ \.user-row\)\):not\(\.pin-expanded\) \.user-bubble > \.pin-content \{[^}]*\}/,+ /\.message-stack \.user-row\.latest-prompt:not\(\.pin-expanded\) \.user-bubble > \.pin-content \{[^}]*\}/,)?.[0] ?? "";assert.match(rest, /-webkit-line-clamp: 6/);assert.doesNotMatch(rest, /max-height|transition/);diff --git a/plugins/web-ui/test/split-canvas-entry.test.ts b/plugins/web-ui/test/split-canvas-entry.test.tsindex 3ec408d80..1c6345115 100644--- a/plugins/web-ui/test/split-canvas-entry.test.ts+++ b/plugins/web-ui/test/split-canvas-entry.test.ts@@ -62,7 +62,10 @@ test("a pane is an element in this document — never a second copy of the app","a pane loads its transcript once, and never after it closes",);assert.match(load, /if \(this\.disposed\) return;/, "and drops the continuation if the pane closed mid-load");- assert.match(split, /onDidVisibilityChange\(\(e\) => \{\s*\n\s*if \(!e\.isVisible\) return;/);+ assert.match(+ split,+ /onDidVisibilityChange\(\(e\) => \{\s*this\.visible = e\.isVisible;\s*if \(!e\.isVisible\) return;/,+ );});test("a conversation dropped on a pane's tab strip joins that pane — and only there", () => {@@ -177,3 +180,20 @@ test("adopting a remote layout normalizes the mirrored timestamp to the server r"the local mirror must carry the server-clamped timestamp, not the value's inner claim",);});++test("hidden panes retain agent state without rendering, and repaint when activated", () => {+ const chat = readFileSync(new URL("../src/chat.ts", import.meta.url), "utf8");+ const draw = chat.slice(chat.indexOf(" function drawActiveChat("), chat.indexOf(" function sessionTopbar("));+ assert.match(draw, /if \(!ctx\.visible\(\)\) \{\s*postCurrentPaneState\(\);\s*return;/);+ assert.ok(draw.indexOf("if (!ctx.visible())") < draw.indexOf("transcriptViewport.beforeRender()"));+ assert.match(split, /this\.visible = p\.api\.isVisible;/);+ assert.match(split, /visible: \(\) => splitState\.active && appState\.currentView === "chats" && this\.visible/);+ assert.match(+ split,+ /this\.syncDensity\(\);\s*this\.conversation\?\.redraw\(\);\s*this\.conversation\?\.scrollToBottom\(\)/,+ );+ assert.match(+ split,+ /notePaneSession\(this\.panelId, paneState\.sessionId, paneState\.threadRef\);\s*notifyPanesChanged\(\);/,+ );+});diff --git a/plugins/web-ui/test/transcript-viewport.test.ts b/plugins/web-ui/test/transcript-viewport.test.tsindex 3c5854e85..b1bf9e17c 100644--- a/plugins/web-ui/test/transcript-viewport.test.ts+++ b/plugins/web-ui/test/transcript-viewport.test.ts@@ -6,8 +6,7 @@ const css = readFileSync(new URL("../src/shell.css", import.meta.url), "utf8");const chat = readFileSync(new URL("../src/chat.ts", import.meta.url), "utf8");test("only an actually stuck prompt gets elevation", () => {- const normal =- css.match(/\.message-stack \.user-row:not\(:has\(~ \.user-row\)\) > \.user-bubble \{[^}]*\}/)?.[0] ?? "";+ const normal = css.match(/\.message-stack \.user-row\.latest-prompt > \.user-bubble \{[^}]*\}/)?.[0] ?? "";assert.doesNotMatch(normal, /box-shadow: var/);assert.match(css, /\.user-row\.stuck > \.user-bubble/);});@@ -913,3 +912,24 @@ test("revealing a message cancels pending and future bottom following", () => {f.close();}});++test("the latest-prompt marker moves between rows and is restored after a template update", () => {+ const f = fixture();+ try {+ assert.equal(f.prompt.classList.contains("latest-prompt"), true);+ f.prompt.classList.remove("latest-prompt");+ f.viewport.sync(f.s);+ assert.equal(f.prompt.classList.contains("latest-prompt"), true);+ const next = f.prompt.cloneNode(true) as HTMLElement;+ next.className = "user-row";+ next.dataset.index = "2";+ f.prompt.after(next);+ f.viewport.sync(f.s);+ assert.equal(f.prompt.classList.contains("latest-prompt"), false);+ assert.equal(next.classList.contains("latest-prompt"), true);+ f.viewport.dispose();+ assert.equal(next.classList.contains("latest-prompt"), false);+ } finally {+ f.close();+ }+});diff --git a/plugins/web-ui/test/ui-render-stability.test.ts b/plugins/web-ui/test/ui-render-stability.test.tsnew file mode 100644index 000000000..9dfccbb48--- /dev/null+++ b/plugins/web-ui/test/ui-render-stability.test.ts@@ -0,0 +1,63 @@+import assert from "node:assert/strict";+import test from "node:test";+import { JSDOM } from "jsdom";+import { Check, X } from "lucide";++const dom = new JSDOM("<!doctype html><body></body>", { pretendToBeVisual: true });+for (const key of [+ "window",+ "document",+ "HTMLElement",+ "Element",+ "Node",+ "Document",+ "CSSStyleSheet",+ "ShadowRoot",+ "customElements",+] as const) {+ Object.defineProperty(globalThis, key, { value: dom.window[key], configurable: true });+}+const { html, render } = await import("lit");+const { icon, waveLoader, workingWave, setFormMenuValue } = await import("../src/ui.ts");++test("unchanged icon templates keep their DOM while separate placements remain independent", () => {+ const host = document.createElement("div");+ const draw = (node = Check, size = 18) =>+ render(html`<button>${icon(node, size)}</button><button>${icon(Check, 18)}</button>`, host);+ draw();+ const first = host.querySelector("svg")!;+ const second = host.querySelectorAll("svg")[1]!;+ assert.notEqual(first, second);+ draw();+ assert.equal(host.querySelector("svg"), first);+ assert.equal(host.querySelectorAll("svg")[1], second);+ draw(X, 14);+ assert.notEqual(host.querySelector("svg"), first);+ assert.equal(host.querySelector("svg")!.getAttribute("width"), "14");+ assert.equal(host.querySelectorAll("svg")[1], second);+});++test("imperative menu checks remain real SVG nodes", () => {+ const control = document.createElement("div");+ control.innerHTML =+ '<input type="hidden"><span class="menu-label"></span><button class="menu-option" data-value="a">A</button><button class="menu-option" data-value="b">B</button>';+ setFormMenuValue(control, "a", "A");+ setFormMenuValue(control, "b", "B");+ assert.equal(control.querySelectorAll("svg").length, 1);+ assert.equal(control.querySelector(".active")?.getAttribute("data-value"), "b");+});++test("wave animation moves a root SVG inside a fixed-size accessible clip", () => {+ const host = document.createElement("div");+ render(workingWave(), host);+ const clip = host.querySelector<HTMLElement>(".wl")!;+ assert.equal(clip.localName, "span");+ assert.equal(clip.style.width, "13.6px");+ assert.equal(clip.style.height, "5.7px");+ assert.equal(clip.style.getPropertyValue("--wl-shift"), "-8.5px");+ assert.equal(clip.getAttribute("aria-label"), "Agent is working");+ assert.equal(clip.querySelector(".wl-row")?.localName, "svg");+ assert.equal(clip.querySelector("g"), null);+ render(waveLoader(), host);+ assert.equal(host.querySelector<HTMLElement>(".wl")!.style.width, "24.5px");+});diff --git a/plugins/web-ui/test/user-message-cap.test.ts b/plugins/web-ui/test/user-message-cap.test.tsindex 698b5265a..1fd974189 100644--- a/plugins/web-ui/test/user-message-cap.test.ts+++ b/plugins/web-ui/test/user-message-cap.test.ts@@ -9,7 +9,7 @@ const shared = readFileSync(new URL("../src/shared-session.ts", import.meta.url)test("only the collapsed pinned prompt is capped and overflow clips instead of nesting scrollbars", () => {const bubble =css.match(- /\.message-stack\s+\.user-row:not\(:has\(~ \.user-row\)\):not\(\.pin-expanded\)\s+\.user-bubble\s+>\s+\.pin-content \{[^}]*\}/,+ /\.message-stack\s+\.user-row\.latest-prompt:not\(\.pin-expanded\)\s+\.user-bubble\s+>\s+\.pin-content \{[^}]*\}/,)?.[0] ?? "";assert.match(bubble, /-webkit-line-clamp: 6;/);assert.match(bubble, /overflow: hidden;/);