Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

perf(minimap): transform-only projection on pan/zoom frames

enes798b278ab840
diff --git a/CLAUDE.md b/CLAUDE.md
index a173ecd82..66f709f08 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -1641,6 +1641,12 @@ the wire never see any of it):
are untouched. The real MiniMap regression tests cover ghost/live transitions, empty bounds,
removals, grouped geometry and camera interaction. When upgrading React Flow, keep those
tests: the projection deliberately mirrors the state fields consumed by MiniMap.
+ **A pan/zoom frame takes a transform-only fast path**: when `transform` changed and the node
+ collections did not, only the camera fields are copied and a full re-projection follows
+ `MINIMAP_FULL_SYNC_DEBOUNCE_MS` after the move settles (rebuilding them per frame re-rendered
+ every rectangle; measured 402 forced layouts per 20 s pan vs 1 with the map hidden). Identity
+ checks alone cannot replace the full path: xyflow's `updateNodeInternals` mutates `nodeLookup`
+ IN PLACE and then calls `set({})`, so any update that leaves `transform` alone syncs fully.
- **Memory bounds** (same posture as park/WebGL: a lever must not end live work): a ghost is
hidden, so the existing Browser Memory Saver discards its guest after `BROWSER_DISCARD_MS`
unless loading/audible/agent-driven — `onGuestDiscarded` then drops the entry (a husk would hold
diff --git a/src/renderer/canvas/VisibleMiniMap.test.tsx b/src/renderer/canvas/VisibleMiniMap.test.tsx
index c5796725c..f93e2c6d5 100644
--- a/src/renderer/canvas/VisibleMiniMap.test.tsx
+++ b/src/renderer/canvas/VisibleMiniMap.test.tsx
@@ -3,7 +3,7 @@ import { act, useLayoutEffect } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { applyNodeChanges, MiniMap, ReactFlowProvider, useStoreApi, type Node } from '@xyflow/react'
import { afterEach, expect, it, vi } from 'vitest'
-import { VisibleMiniMap } from './VisibleMiniMap'
+import { MINIMAP_FULL_SYNC_DEBOUNCE_MS, MiniMapProjection, VisibleMiniMap } from './VisibleMiniMap'
import { mergeWithKeepAlive, retireIntoPool } from '../lib/webviewKeepAlive'
import type { CanvasNode } from '../state/workspace'
@@ -131,3 +131,84 @@ it('routes wheel zoom, drag and map clicks through the original camera', () => {
act(() => svg.dispatchEvent(new MouseEvent('click', { clientX: 50, clientY: 60, bubbles: true })))
expect(mapClicked).toHaveBeenCalledTimes(1)
})
+
+// The projection's own store, read directly: the map's rendered output cannot tell a rebuilt
+// node array from a reused one, and reuse on pan frames is the whole point of the fast path.
+let target: ReturnType<typeof useStoreApi>
+function CaptureTarget() {
+ const api = useStoreApi()
+ useLayoutEffect(() => { target = api }, [api])
+ return null
+}
+function ProjectionHarness() {
+ const source = useStoreApi()
+ return (
+ <ReactFlowProvider>
+ <MiniMapProjection source={source} />
+ <CaptureTarget />
+ </ReactFlowProvider>
+ )
+}
+function mountProjection() {
+ host = document.createElement('div')
+ document.body.append(host)
+ root = createRoot(host)
+ act(() => root.render(
+ <ReactFlowProvider>
+ <Capture />
+ <ProjectionHarness />
+ </ReactFlowProvider>
+ ))
+ act(() => store.setState({ width: 800, height: 600, transform: [-2000, -2000, 1] }))
+ setNodes([node('a'), node('b', 'terminal', 2400)])
+}
+const targetState = () => target.getState()
+
+it('a transform-only change updates transform without rebuilding nodes/nodeLookup', () => {
+ vi.useFakeTimers()
+ mountProjection()
+ const before = targetState()
+ expect(before.nodes).toHaveLength(2)
+ act(() => store.setState({ transform: [10, 20, 1] }))
+ const after = targetState()
+ expect(after.transform).toEqual([10, 20, 1])
+ expect(after.nodes).toBe(before.nodes) // same array identity — no rebuild
+ expect(after.nodeLookup).toBe(before.nodeLookup)
+})
+
+it('a transform-only change is followed by a full sync after the debounce', () => {
+ vi.useFakeTimers()
+ mountProjection()
+ const before = targetState()
+ act(() => store.setState({ transform: [10, 20, 1] }))
+ act(() => vi.advanceTimersByTime(MINIMAP_FULL_SYNC_DEBOUNCE_MS - 1))
+ expect(targetState().nodes).toBe(before.nodes) // still inside the move
+ act(() => vi.advanceTimersByTime(2))
+ expect(targetState().nodes).not.toBe(before.nodes) // rebuilt once the move settles
+ expect(targetState().nodes).toHaveLength(2)
+})
+
+it('an in-place internals update (set({}) with the same nodeLookup) still does a full sync immediately', () => {
+ vi.useFakeTimers()
+ mountProjection()
+ const before = targetState()
+ const lookup = store.getState().nodeLookup
+ const [id, n] = [...lookup][0]
+ lookup.set(id, { ...n, measured: { width: 999, height: 999 } }) // xyflow mutates in place
+ act(() => store.setState({})) // …then set({})
+ const after = targetState()
+ expect(after.nodes).not.toBe(before.nodes)
+ expect(after.nodeLookup.get(id)?.measured?.width).toBe(999)
+})
+
+it('a camera update carries the viewport size with it, and a nodes change riding it syncs at once', () => {
+ vi.useFakeTimers()
+ mountProjection()
+ act(() => store.setState({ transform: [1, 2, 1], width: 1000, height: 500 }))
+ expect(targetState()).toMatchObject({ width: 1000, height: 500 })
+ const before = targetState()
+ const nodes = store.getState().nodes.slice(0, 1)
+ act(() => store.setState({ transform: [3, 4, 1], nodes }))
+ expect(targetState().nodes).not.toBe(before.nodes)
+ expect(targetState().nodes.map((x) => x.id)).toEqual(['a'])
+})
diff --git a/src/renderer/canvas/VisibleMiniMap.tsx b/src/renderer/canvas/VisibleMiniMap.tsx
index a4c430821..b2ba84fcb 100644
--- a/src/renderer/canvas/VisibleMiniMap.tsx
+++ b/src/renderer/canvas/VisibleMiniMap.tsx
@@ -3,6 +3,10 @@ import { MiniMap, ReactFlowProvider, useStoreApi, type MiniMapProps } from '@xyf
type FlowStore = ReturnType<typeof useStoreApi>
+/** After a transform-only (pan/zoom) update, one full projection follows once the move settles —
+ * covers any internals change that happened to ride the same store update. */
+export const MINIMAP_FULL_SYNC_DEBOUNCE_MS = 150
+
/** MiniMap has no node filter: CSS-hidden keep-alive guests otherwise paint rectangles AND
* enlarge its bounds. Give only the map a filtered store, keeping the canvas's mounted guests
* untouched. `hidden: true` on the real nodes would unmount their webviews and lose page state.
@@ -18,30 +22,66 @@ export function VisibleMiniMap(props: MiniMapProps) {
)
}
-function MiniMapProjection({ source }: { source: FlowStore }) {
+/** Exported for tests only: the projected store is not reachable through the rendered map. */
+export function MiniMapProjection({ source }: { source: FlowStore }) {
const target = useStoreApi()
useLayoutEffect(() => {
- const sync = () => {
+ const visible = (n: { hidden?: boolean; data: Record<string, unknown> }) =>
+ !n.hidden && n.data.ghost !== true
+ // Everything the map reads except the node collections: cheap scalars/references, so the
+ // fast path can copy them all and a camera update that also resizes the pane stays exact.
+ const camera = (s: ReturnType<FlowStore['getState']>) => ({
+ width: s.width,
+ height: s.height,
+ transform: s.transform,
+ panZoom: s.panZoom,
+ translateExtent: s.translateExtent,
+ rfId: s.rfId,
+ ariaLabelConfig: s.ariaLabelConfig,
+ userSelectionActive: s.userSelectionActive
+ })
+ let prev = source.getState()
+ const full = () => {
const s = source.getState()
- const visible = (n: { hidden?: boolean; data: Record<string, unknown> }) =>
- !n.hidden && n.data.ghost !== true
+ prev = s
// Internal nodes carry absolute group coordinates and measured sizes. Rebuilding them
// from serialized/project nodes would lose both and lag live drag/resize/removal updates.
target.setState({
nodes: s.nodes.filter(visible),
nodeLookup: new Map([...s.nodeLookup].filter(([, n]) => visible(n))),
- width: s.width,
- height: s.height,
- transform: s.transform,
- panZoom: s.panZoom,
- translateExtent: s.translateExtent,
- rfId: s.rfId,
- ariaLabelConfig: s.ariaLabelConfig,
- userSelectionActive: s.userSelectionActive
+ ...camera(s)
})
}
- sync()
- return source.subscribe(sync)
+ let trailing: ReturnType<typeof setTimeout> | null = null
+ // Pan/zoom frames change ONLY `transform`; rebuilding the filtered node collections on each
+ // one re-rendered every minimap rectangle and forced ~20 layouts/s while panning. Identity
+ // checks alone cannot detect a measurement update: xyflow mutates `nodeLookup` IN PLACE and
+ // then calls set({}), so the fast path is taken only when `transform` itself changed (and the
+ // node collections did not), and a trailing full sync always follows.
+ const sync = () => {
+ const s = source.getState()
+ const fast =
+ s.transform !== prev.transform && s.nodes === prev.nodes && s.nodeLookup === prev.nodeLookup
+ if (!fast) {
+ if (trailing) clearTimeout(trailing)
+ trailing = null
+ full()
+ return
+ }
+ prev = s
+ target.setState(camera(s))
+ if (trailing) clearTimeout(trailing)
+ trailing = setTimeout(() => {
+ trailing = null
+ full()
+ }, MINIMAP_FULL_SYNC_DEBOUNCE_MS)
+ }
+ full()
+ const unsub = source.subscribe(sync)
+ return () => {
+ unsub()
+ if (trailing) clearTimeout(trailing)
+ }
}, [source, target])
return null
}