From 4ce856a4cb74970ba97be1d62857bc2b3e4ffe3c Mon Sep 17 00:00:00 2001 From: Leo Li Date: Tue, 3 Nov 2026 10:56:08 +0000 Subject: [PATCH 1/3] fix(chat): keep equal height reports out of dirty placement IDs Co-authored-by: Codex <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com> Agent-Session: https://cmux.com/deeplink/agent?id=aid-d43daca7ac1e4cde2f3d --- webviews/src/agent-session/acpmux/App.tsx | 4 +- webviews/test/drawn-height-priority.test.tsx | 189 +++++++++++++++++++ 2 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 webviews/test/drawn-height-priority.test.tsx diff --git a/webviews/src/agent-session/acpmux/App.tsx b/webviews/src/agent-session/acpmux/App.tsx index 6e297b43dac6..8b81285bbb97 100644 --- a/webviews/src/agent-session/acpmux/App.tsx +++ b/webviews/src/agent-session/acpmux/App.tsx @@ -3438,11 +3438,13 @@ export function VirtualTranscript({ if (!pendingDrawn.current.size) return; const updates = pendingDrawn.current; pendingDrawn.current = new Map(); - for (const id of updates.keys()) changedDrawn.current.add(id); + // An equal report can correct a different height still queued at a lower priority. + // Compare in the updater, against the state React rebases it onto, not drawnRef. setDrawn((current) => { let next: Map | undefined; for (const [id, entry] of updates) { if (sameDrawn(current.get(id), entry)) continue; + changedDrawn.current.add(id); next ??= new Map(current); next.set(id, entry); } diff --git a/webviews/test/drawn-height-priority.test.tsx b/webviews/test/drawn-height-priority.test.tsx new file mode 100644 index 000000000000..1f4faede03f4 --- /dev/null +++ b/webviews/test/drawn-height-priority.test.tsx @@ -0,0 +1,189 @@ +import { afterEach, expect, test } from "bun:test"; +import { readFileSync } from "node:fs"; +import { act, startTransition, useCallback, useLayoutEffect, useRef, useState } from "react"; +import { flushSync } from "react-dom"; +import { createRoot, type Root } from "react-dom/client"; +import { JSDOM } from "jsdom"; + +// Exercise the production callback with React's real priority queues, without mounting the +// whole agent pane. In particular, an updater is not interchangeable with its eager result. +const source = readFileSync( + process.env.CMUX_TEST_DRAWN_SOURCE ?? new URL("../src/agent-session/acpmux/App.tsx", import.meta.url), + "utf8", +); +const comparison = source.slice( + source.indexOf("function sameDrawn("), + source.indexOf("/// RowFrame redraws"), +); +const productionCallback = source.slice( + source.indexOf(" const flushDrawn = useCallback("), + source.indexOf( + " const observer = useMemo(", + source.indexOf(" const flushDrawn = useCallback("), + ), +); +// The mutation run must fail the priority regression: rendered state alone misses queued work. +const callback = + process.env.CMUX_TEST_NAIVE_DRAWN_GUARD === "1" + ? productionCallback.replace( + " setDrawn((current) => {", + " if (!changesDrawn(drawnRef.current, updates)) return;\n setDrawn((current) => {", + ) + : productionCallback; +type Height = { + sessionId: string; + version: number; + width: number; + height: number; + actions?: boolean; + status?: string; +}; +type Heights = Map; +type Ref = { current: T }; +type Inputs = { + drawnRef: Ref; + pendingDrawn: Ref; + changedDrawn: Ref>; + setDrawn: (update: (current: Heights) => Heights) => void; +}; +const useFlush = new Function( + "useCallback", + "useRef", + "useLayoutEffect", + new Bun.Transpiler({ loader: "ts" }).transformSync(` +${comparison} +function useFlush({ drawnRef, pendingDrawn, changedDrawn, setDrawn }) { +${callback} +return flushDrawn; +}`) + "\nreturn useFlush;", +)(useCallback, useRef, useLayoutEffect) as (inputs: Inputs) => () => void; + +let root: Root | undefined; +let dom: JSDOM | undefined; +const scope = globalThis as Record; +const saved = new Map(); +afterEach(async () => { + await act(async () => root?.unmount()); + root = undefined; + dom?.window.close(); + dom = undefined; + for (const [key, value] of saved) { + if (value === undefined) delete scope[key]; + else Object.defineProperty(scope, key, { configurable: true, writable: true, value }); + } + saved.clear(); +}); + +async function setup() { + dom = new JSDOM("
"); + for (const [key, value] of Object.entries({ + window: dom.window, + document: dom.window.document, + navigator: dom.window.navigator, + HTMLElement: dom.window.HTMLElement, + IS_REACT_ACT_ENVIRONMENT: true, + })) { + saved.set(key, scope[key]); + Object.defineProperty(scope, key, { configurable: true, writable: true, value }); + } + const h1: Height = { sessionId: "chat", version: 1, width: 600, height: 100 }; + const control = { + calls: 0, + renders: 0, + painted: [] as number[], + dirty: new Set(), + report: (_entries: Heights) => {}, + tick: () => {}, + }; + function Probe() { + control.renders += 1; + const [drawn, setDrawn] = useState( + () => + new Map([ + ["row", h1], + ["unchanged", h1], + ]), + ); + const [, setTick] = useState(0); + const drawnRef = useRef(drawn); + drawnRef.current = drawn; + const pendingDrawn = useRef(new Map()); + const changedDrawn = useRef(control.dirty); + const flush = useFlush({ + drawnRef, + pendingDrawn, + changedDrawn, + setDrawn: (update) => { + control.calls += 1; + setDrawn(update); + }, + }); + control.report = (entries) => { + pendingDrawn.current = entries; + flush(); + }; + control.tick = () => setTick((value) => value + 1); + useLayoutEffect(() => { + control.painted.push(drawn.get("row")!.height); + }); + return
; + } + root = createRoot(dom.window.document.getElementById("root")!); + await act(async () => root!.render()); + return { control, h1 }; +} + +test("an equal correction supersedes a queued lower-priority height", async () => { + const { control, h1 } = await setup(); + await act(async () => { + startTransition(() => control.report(new Map([["row", { ...h1, height: 200 }]]))); + flushSync(() => { + control.tick(); + control.report(new Map([["row", { ...h1 }]])); + }); + expect(dom!.window.document.querySelector("[data-height]")!.getAttribute("data-height")).toBe( + "100", + ); + }); + expect(dom!.window.document.querySelector("[data-height]")!.getAttribute("data-height")).toBe( + "100", + ); + expect(control.painted).not.toContain(200); +}); + +test("idle equal-height confirmations leave no dirty placement IDs", async () => { + const { control, h1 } = await setup(); + const before = control.renders; + let dirtyIds = 0; + for (let tick = 0; tick < 120; tick += 1) { + await act(async () => control.report(new Map([["row", { ...h1 }]]))); + dirtyIds += control.dirty.size; + control.dirty.clear(); + } + console.log( + `drawn-height idle confirmations: ${JSON.stringify({ ticks: 120, setDrawn: control.calls, renders: control.renders - before, dirtyIds })}`, + ); + // Keep the updater: an equal entry may supersede an update queued at another priority. + expect(control.calls).toBe(120); + expect(dirtyIds).toBe(0); + expect(control.renders).toBe(before); + expect([...control.dirty]).toEqual([]); +}); + +test("a mixed batch dirties only changed rows and applies fractional corrections", async () => { + const { control, h1 } = await setup(); + await act(async () => { + flushSync(() => + control.report( + new Map([ + ["row", { ...h1, height: 100.25 }], + ["unchanged", { ...h1 }], + ]), + ), + ); + expect(dom!.window.document.querySelector("[data-height]")!.getAttribute("data-height")).toBe( + "100.25", + ); + }); + expect([...control.dirty]).toEqual(["row"]); +}); From d8795363fda67f5a47d9ce52cc76075cef89bee1 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Tue, 3 Nov 2026 11:00:36 +0000 Subject: [PATCH 2/3] style(test): follow shared formatter width Co-authored-by: Codex <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com> Agent-Session: https://cmux.com/deeplink/agent?id=aid-d43daca7ac1e4cde2f3d --- webviews/test/drawn-height-priority.test.tsx | 22 +++++--------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/webviews/test/drawn-height-priority.test.tsx b/webviews/test/drawn-height-priority.test.tsx index 1f4faede03f4..1f55ec74afb7 100644 --- a/webviews/test/drawn-height-priority.test.tsx +++ b/webviews/test/drawn-height-priority.test.tsx @@ -11,16 +11,10 @@ const source = readFileSync( process.env.CMUX_TEST_DRAWN_SOURCE ?? new URL("../src/agent-session/acpmux/App.tsx", import.meta.url), "utf8", ); -const comparison = source.slice( - source.indexOf("function sameDrawn("), - source.indexOf("/// RowFrame redraws"), -); +const comparison = source.slice(source.indexOf("function sameDrawn("), source.indexOf("/// RowFrame redraws")); const productionCallback = source.slice( source.indexOf(" const flushDrawn = useCallback("), - source.indexOf( - " const observer = useMemo(", - source.indexOf(" const flushDrawn = useCallback("), - ), + source.indexOf(" const observer = useMemo(", source.indexOf(" const flushDrawn = useCallback(")), ); // The mutation run must fail the priority regression: rendered state alone misses queued work. const callback = @@ -141,13 +135,9 @@ test("an equal correction supersedes a queued lower-priority height", async () = control.tick(); control.report(new Map([["row", { ...h1 }]])); }); - expect(dom!.window.document.querySelector("[data-height]")!.getAttribute("data-height")).toBe( - "100", - ); + expect(dom!.window.document.querySelector("[data-height]")!.getAttribute("data-height")).toBe("100"); }); - expect(dom!.window.document.querySelector("[data-height]")!.getAttribute("data-height")).toBe( - "100", - ); + expect(dom!.window.document.querySelector("[data-height]")!.getAttribute("data-height")).toBe("100"); expect(control.painted).not.toContain(200); }); @@ -181,9 +171,7 @@ test("a mixed batch dirties only changed rows and applies fractional corrections ]), ), ); - expect(dom!.window.document.querySelector("[data-height]")!.getAttribute("data-height")).toBe( - "100.25", - ); + expect(dom!.window.document.querySelector("[data-height]")!.getAttribute("data-height")).toBe("100.25"); }); expect([...control.dirty]).toEqual(["row"]); }); From 62e8a7a2a5f7cc6ce469688bbc272a8dcae7cf23 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Tue, 3 Nov 2026 11:15:09 +0000 Subject: [PATCH 3/3] test(chat): compile the production callback through node vm Co-authored-by: Codex <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com> Agent-Session: https://cmux.com/deeplink/agent?id=aid-d43daca7ac1e4cde2f3d --- webviews/test/drawn-height-priority.test.tsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/webviews/test/drawn-height-priority.test.tsx b/webviews/test/drawn-height-priority.test.tsx index 1f55ec74afb7..33dbe9676802 100644 --- a/webviews/test/drawn-height-priority.test.tsx +++ b/webviews/test/drawn-height-priority.test.tsx @@ -1,5 +1,6 @@ import { afterEach, expect, test } from "bun:test"; import { readFileSync } from "node:fs"; +import { compileFunction } from "node:vm"; import { act, startTransition, useCallback, useLayoutEffect, useRef, useState } from "react"; import { flushSync } from "react-dom"; import { createRoot, type Root } from "react-dom/client"; @@ -40,17 +41,15 @@ type Inputs = { changedDrawn: Ref>; setDrawn: (update: (current: Heights) => Heights) => void; }; -const useFlush = new Function( - "useCallback", - "useRef", - "useLayoutEffect", +const useFlush = compileFunction( new Bun.Transpiler({ loader: "ts" }).transformSync(` ${comparison} function useFlush({ drawnRef, pendingDrawn, changedDrawn, setDrawn }) { ${callback} return flushDrawn; }`) + "\nreturn useFlush;", -)(useCallback, useRef, useLayoutEffect) as (inputs: Inputs) => () => void; + ["useCallback"], +)(useCallback) as (inputs: Inputs) => () => void; let root: Root | undefined; let dom: JSDOM | undefined;