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(glyphgrid): no frames while the kanban board covers the canvas

enes2eac56cbe96a
diff --git a/docs/superpowers/plans/2026-08-03-phase1b-device-checklist.md b/docs/superpowers/plans/2026-08-03-phase1b-device-checklist.md
index 0b8eeb518..82d577e15 100644
--- a/docs/superpowers/plans/2026-08-03-phase1b-device-checklist.md
+++ b/docs/superpowers/plans/2026-08-03-phase1b-device-checklist.md
@@ -400,9 +400,20 @@ Auto after you switched builds, that is why: re-select Shared and carry on.
after a move — that is a grid registering against a measurement taken on the display we left
(see `deviceCellOf`), and it means the rebuild adopted the OLD cell — and whether the SECOND
move rebuilds again or the fix latches after the first one.
-- [ ] **4.16 Kanban board.** Open the board over a shared-mode project: the board is fully opaque
- (no glyphs showing through), and the idle rAF cost is not noticeable (check CPU/GPU while
- the board is up with terminals idle).
+- [ ] **4.16 Kanban board — no frames are drawn under it.** Open the board over a shared-mode
+ project: it is fully opaque (no glyphs showing through). Then the part this item exists for,
+ which needs BUSY terminals, not idle ones — the idle park already covered idle.
+ **How to run it.** With half a dozen terminals streaming (`yes`, a build, a busy agent),
+ note the process CPU on the canvas, then open the board and watch it for ~30 s. Expected: it
+ DROPS to roughly what the board costs on its own, and stays there — the loop is stopped, so
+ the streaming rows cost nothing to draw. A canvas-level cost that stays flat when the board
+ goes up means the gate never fired.
+ **Then close the board, and watch the first paint.** Every terminal must show its CURRENT
+ screen immediately — not the rows it had when the board went up, and not a blank body that
+ fills in when the next line arrives. Stopping the loop stops drawing only; the grids keep
+ receiving rows the whole time, so this is a repaint, and anything stale here is the bug.
+ Also switch PROJECTS while the board is up (to a canvas-view project and back): the gate
+ follows the active project, not just the view toggle.
- [ ] **4.17 Card modal.** Open a session's card modal while the canvas terminal is on the shared
canvas: the modal's own terminal renders through xterm's DOM renderer (known limitation L8)
and both views stay live and correctly sized.
@@ -517,9 +528,6 @@ Auto after you switched builds, that is why: re-select Shared and carry on.
- **L9 — A failure is permanent for the session.** A GL error or a lost context disables the shared
renderer until the app is relaunched; there is no restore path in Phase 1b (deliberate — a retry
loop on a bad driver is how you turn one failure into a flicker).
-- **L11 — Grids keep drawing while the kanban board covers the canvas.** The board overlay is
- opaque, so nothing is visible; it is wasted work, measured by item 4.16 and closed in Phase 2 if
- it shows up.
- **L13 — The block cursor on a double-width glyph paints only the left half-cell.** The cursor rect
is emitted at one cell's width, while a CJK/emoji cell occupies two columns, so the block covers
the left column and the right half of the glyph stays uncovered. Cosmetic and position-correct
diff --git a/src/renderer/canvas/SharedGlyphLayer.test.ts b/src/renderer/canvas/SharedGlyphLayer.test.ts
index 26fa010cd..12983d521 100644
--- a/src/renderer/canvas/SharedGlyphLayer.test.ts
+++ b/src/renderer/canvas/SharedGlyphLayer.test.ts
@@ -1,6 +1,8 @@
import { adoptUserNodes } from '@xyflow/system'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { createFrameLoop, type FrameLoop, type FrameLoopHost } from '../glyphgrid/frame-driver'
import {
+ createBoardFrameGate,
failSharedGlyph,
getSharedGlyphContext,
effectiveStackOrder,
@@ -1043,6 +1045,174 @@ describe('syncAtlasPixelRatio', () => {
})
})
+/**
+ * The board gate. The kanban board is a fully opaque overlay over the whole canvas, so every frame
+ * drawn under it is invisible work — and the idle park cannot help, because a canvas of STREAMING
+ * terminals under the board never goes quiet.
+ *
+ * The real `createFrameLoop` is driven here rather than a mock of it: what is being asserted is
+ * that a stopped loop really stops (a wake cannot resurrect it) and that a reopened canvas really
+ * draws, and both of those are the frame driver's contract, not this file's.
+ */
+describe('createBoardFrameGate', () => {
+ function harness(): {
+ host: FrameLoopHost
+ /** Run every pending rAF callback. */
+ flushFrames(): number
+ pendingFrames(): number
+ drew(): number
+ loopsMade(): number
+ makeLoop(): FrameLoop
+ /** Rows arriving from a terminal: the engine goes dirty whether or not anyone draws it. */
+ damage(): void
+ } {
+ const frames = new Map<number, () => void>()
+ const timers = new Map<number, () => void>()
+ let handle = 1
+ let dirty = false
+ let drew = 0
+ let loops = 0
+ const host: FrameLoopHost = {
+ frame: () => {
+ if (!dirty) return false
+ dirty = false
+ drew++
+ return true
+ },
+ alive: () => true,
+ onError: () => {},
+ requestFrame: (cb) => {
+ const h = handle++
+ frames.set(h, cb)
+ return h
+ },
+ cancelFrame: (h) => {
+ frames.delete(h)
+ },
+ setTimer: (cb) => {
+ const h = handle++
+ timers.set(h, cb)
+ return h
+ },
+ clearTimer: (h) => {
+ timers.delete(h)
+ }
+ }
+ return {
+ host,
+ flushFrames(): number {
+ const due = [...frames.entries()]
+ frames.clear()
+ for (const [, cb] of due) cb()
+ return due.length
+ },
+ pendingFrames: () => frames.size,
+ drew: () => drew,
+ loopsMade: () => loops,
+ makeLoop: () => {
+ loops++
+ return createFrameLoop(host)
+ },
+ damage: () => {
+ dirty = true
+ }
+ }
+ }
+
+ it('starts drawing when the board is closed', () => {
+ const h = harness()
+ createBoardFrameGate(() => false, h.makeLoop)
+ expect(h.pendingFrames()).toBe(1)
+ })
+
+ it('schedules NOTHING while the board is up, damage included', () => {
+ // The point of the whole gate: rows keep arriving under the board and none of them costs a
+ // frame.
+ const h = harness()
+ const gate = createBoardFrameGate(() => true, h.makeLoop)
+ expect(h.pendingFrames()).toBe(0)
+ h.damage()
+ gate.loop().wake()
+ expect(h.pendingFrames()).toBe(0)
+ expect(h.drew()).toBe(0)
+ })
+
+ it('stops drawing when the board OPENS, and drops the frame already scheduled', () => {
+ const h = harness()
+ let open = false
+ const gate = createBoardFrameGate(() => open, h.makeLoop)
+ expect(h.pendingFrames()).toBe(1)
+ open = true
+ gate.sync()
+ expect(h.pendingFrames()).toBe(0)
+ h.damage()
+ gate.loop().wake()
+ expect(h.pendingFrames()).toBe(0)
+ })
+
+ it('draws ONE frame immediately when the board closes — not on the next damage', () => {
+ // Everything that arrived under the board is still damage on the engine, so the first frame
+ // after the board closes paints the CURRENT screen. Waiting for the next write would leave a
+ // finished agent's terminal showing the rows it had when the board went up.
+ const h = harness()
+ let open = true
+ const gate = createBoardFrameGate(() => open, h.makeLoop)
+ h.damage()
+ expect(h.pendingFrames()).toBe(0)
+ open = false
+ gate.sync()
+ expect(h.pendingFrames()).toBe(1)
+ h.flushFrames()
+ expect(h.drew()).toBe(1)
+ })
+
+ it('hands out the CURRENT loop, so damage wakes the one that is running', () => {
+ // `createFrameLoop.stop()` is terminal by contract, so leaving the board builds a FRESH loop.
+ // A damage subscription that captured the original would be waking a corpse for the rest of
+ // the session — the frozen-canvas failure this gate must not introduce.
+ const h = harness()
+ let open = false
+ const gate = createBoardFrameGate(() => open, h.makeLoop)
+ const first = gate.loop()
+ open = true
+ gate.sync()
+ open = false
+ gate.sync()
+ expect(gate.loop()).not.toBe(first)
+ // The stopped loop adds nothing to what the fresh one has scheduled…
+ const scheduled = h.pendingFrames()
+ first.wake()
+ expect(h.pendingFrames()).toBe(scheduled)
+ // …while the one the gate hands out really is the one drawing.
+ h.damage()
+ h.flushFrames()
+ expect(h.drew()).toBe(1)
+ })
+
+ it('a sync with no change costs nothing — no new loop, no lost frame', () => {
+ const h = harness()
+ const gate = createBoardFrameGate(() => false, h.makeLoop)
+ gate.sync()
+ gate.sync()
+ expect(h.loopsMade()).toBe(1)
+ expect(h.pendingFrames()).toBe(1)
+ })
+
+ it('stop() ends it, and a later sync cannot resurrect it', () => {
+ // Teardown ordering is not guaranteed to beat a store notification that is already in flight.
+ const h = harness()
+ let open = true
+ const gate = createBoardFrameGate(() => open, h.makeLoop)
+ gate.stop()
+ open = false
+ gate.sync()
+ expect(h.pendingFrames()).toBe(0)
+ h.damage()
+ gate.loop().wake()
+ expect(h.pendingFrames()).toBe(0)
+ })
+})
+
describe('installAtlasResetLog', () => {
/** The atlas surface the log reads, with a manual trigger. */
function fakeAtlas(): {
diff --git a/src/renderer/canvas/SharedGlyphLayer.tsx b/src/renderer/canvas/SharedGlyphLayer.tsx
index 1ad7538be..655792df6 100644
--- a/src/renderer/canvas/SharedGlyphLayer.tsx
+++ b/src/renderer/canvas/SharedGlyphLayer.tsx
@@ -38,11 +38,13 @@ import {
} from '../glyphgrid/atlas'
import type { Camera } from '../glyphgrid/camera'
import { GlyphGridEngine } from '../glyphgrid/engine'
-import { createFrameLoop } from '../glyphgrid/frame-driver'
+import { createFrameLoop, type FrameLoop } from '../glyphgrid/frame-driver'
import type { GlyphGL } from '../glyphgrid/gl'
import { createWebgl2GL } from '../glyphgrid/gl-webgl2'
import { createCanvasRasterizer } from '../glyphgrid/raster'
+import { useProjects } from '../state/projects'
import { useSettings } from '../state/settings'
+import { useViewMode, viewFor } from '../state/viewMode'
/** Atlas page edge, in DEVICE pixels. 2048 (not Phase 0's 1024) because the atlas cells are
* device-sized AND the page is now keyed by COLOUR: at dpr 2 a 13px terminal font is roughly a
@@ -1088,6 +1090,95 @@ export function setSharedGlyphCamera(cam: Camera): void {
live?.engine.setCamera(lastCamera)
}
+// ---------------------------------------------------------------------------------------------
+// The board gate — no frames under an opaque overlay
+// ---------------------------------------------------------------------------------------------
+
+/**
+ * Is the kanban board currently covering the canvas?
+ *
+ * Read imperatively from both stores because the answer needs both: the board is a PER-PROJECT
+ * view, so switching to a project that is on the board changes it with no view change at all
+ * (`TerminalNode` gets away with reading the project id once because a project switch remounts it;
+ * this layer stays mounted across switches).
+ */
+export function boardCoversCanvas(): boolean {
+ return viewFor(useViewMode.getState(), useProjects.getState().activeProjectId ?? '') === 'kanban'
+}
+
+export interface BoardFrameGate {
+ /** The loop that is currently in charge — always call through this, never capture the result. */
+ loop(): FrameLoop
+ /** Re-read the board flag and act on a change. Cheap and change-gated: a store notification that
+ * did not move the board costs one comparison. */
+ sync(): void
+ /** Teardown. Stops the current loop for good and makes any later `sync` inert. */
+ stop(): void
+}
+
+/**
+ * Gate the frame loop on the kanban board.
+ *
+ * The board is a FULLY OPAQUE overlay over the whole canvas, so every frame drawn under it is
+ * invisible work. Phase 1c's idle park does not cover this case at all: a canvas of streaming
+ * terminals under the board never goes quiet, so it repaints at the display's refresh rate for
+ * nobody.
+ *
+ * **STOPPING THE LOOP DOES NOT STOP THE FEED, and that distinction is the whole safety of this.**
+ * Nothing about the terminals changes: their PTYs keep running, the addons keep writing rows into
+ * the engine's grid buffers, and `markDirty` keeps recording the damage. The ONLY thing that stops
+ * is the submission of frames. So the buffers stay current the entire time the board is up, and
+ * closing it is a REPAINT of what is already there — not a rebuild, not a re-registration, and not
+ * a screen that has to be re-fed from tmux. Nothing is lost by not drawing it.
+ *
+ * That is also why one frame is drawn IMMEDIATELY on close rather than waiting for the next damage:
+ * the engine is still dirty from everything that arrived under the board, and the next write on an
+ * idle canvas may be minutes away — a finished agent's terminal would sit there showing the rows it
+ * had when the board went up. `start()` schedules that frame; `wake()` alongside it is belt and
+ * braces, and free (it only reschedules a frame that draws nothing when there is nothing to draw).
+ *
+ * **A stopped loop is stopped FOR GOOD** — `createFrameLoop`'s contract, so that a late damage can
+ * never resurrect a loop whose context is gone. Leaving the board therefore builds a FRESH loop
+ * rather than restarting the old one, which is why every caller must reach the loop through
+ * `loop()`: a damage subscription that captured the original would be waking a corpse for the rest
+ * of the session, and a frozen canvas is a far worse failure than a wasted frame.
+ */
+export function createBoardFrameGate(
+ boardOpen: () => boolean,
+ makeLoop: () => FrameLoop
+): BoardFrameGate {
+ let covered = boardOpen()
+ let loop = makeLoop()
+ let torn = false
+ // Mounting WHILE the board is up (a project that opens on the board, a remount behind it) must
+ // not start drawing either — the gate's state is read at construction, not assumed to be "open
+ // canvas". And a loop that is merely NEVER STARTED is not inert: `wake()` starts one, so the
+ // damage subscription would schedule frames under the board through the back door. It has to be
+ // STOPPED, which is also what makes the close path build a fresh one from either entry.
+ if (covered) loop.stop()
+ else loop.start()
+ return {
+ loop: () => loop,
+ sync(): void {
+ if (torn) return
+ const now = boardOpen()
+ if (now === covered) return
+ covered = now
+ if (now) {
+ loop.stop()
+ return
+ }
+ loop = makeLoop()
+ loop.start()
+ loop.wake()
+ },
+ stop(): void {
+ torn = true
+ loop.stop()
+ }
+ }
+}
+
// ---------------------------------------------------------------------------------------------
// The component
// ---------------------------------------------------------------------------------------------
@@ -1154,34 +1245,47 @@ export function SharedGlyphLayer(): JSX.Element {
// whole frame pipeline ticking at the display's refresh rate to draw nothing. All the
// scheduling and the park/heartbeat policy live in `createFrameLoop` (unit-tested there); this
// effect only supplies the seams and the wake signals.
- const loop = createFrameLoop({
- // The context can be torn down synchronously between a schedule and its callback (font
- // change, mode switched off); the loop stops itself rather than rescheduling, and the
- // cleanup below is idempotent either way.
- alive: () => !ctx.disposed,
- frame: () => ctx.engine.frame(),
- // `frame()` rethrows by contract, having restored its own damage. There is no recovery in
- // 1b: the whole session goes back to the DOM renderer.
- onError: (err) => failSharedGlyph('frame threw', err),
- requestFrame: (cb) => requestAnimationFrame(cb),
- cancelFrame: (h) => cancelAnimationFrame(h),
- setTimer: (cb, ms) => window.setTimeout(cb, ms),
- clearTimer: (h) => window.clearTimeout(h)
- })
+ //
+ // The gate around it answers the case the park cannot: the kanban board covering the canvas.
+ // It owns starting and stopping (including the "mounted while the board is already up" case),
+ // which is why there is no `loop.start()` here — see `createBoardFrameGate`.
+ const gate = createBoardFrameGate(boardCoversCanvas, () =>
+ createFrameLoop({
+ // The context can be torn down synchronously between a schedule and its callback (font
+ // change, dpr change, mode switched off); the loop stops itself rather than rescheduling,
+ // and the cleanup below is idempotent either way.
+ alive: () => !ctx.disposed,
+ frame: () => ctx.engine.frame(),
+ // `frame()` rethrows by contract, having restored its own damage. There is no recovery in
+ // 1b: the whole session goes back to the DOM renderer.
+ onError: (err) => failSharedGlyph('frame threw', err),
+ requestFrame: (cb) => requestAnimationFrame(cb),
+ cancelFrame: (h) => cancelAnimationFrame(h),
+ setTimer: (cb, ms) => window.setTimeout(cb, ms),
+ clearTimer: (h) => window.clearTimeout(h)
+ })
+ )
// THE wake mechanism: the engine funnels every damage write through `markDirty`, which calls
- // out on the clean→dirty edge. A parked loop resumes on the very next damaging write.
- const damage = ctx.engine.onDamage(() => loop.wake())
+ // out on the clean→dirty edge. A parked loop resumes on the very next damaging write. Reached
+ // through `gate.loop()` and never captured — leaving the board builds a fresh loop, and a
+ // subscription pinned to the stopped one would freeze the canvas for the rest of the session.
+ const damage = ctx.engine.onDamage(() => gate.loop().wake())
// Belt and braces for the background-throttling case (Task 9): a park entered while the window
// was occluded must not outlive the return, and a throttled/coalesced rAF can leave the loop
// parked with damage pending. Both are cheap — `wake()` only schedules a frame that draws
- // nothing when there is nothing to draw.
+ // nothing when there is nothing to draw, and it is inert while the board is up.
const onVisible = (): void => {
- if (!document.hidden) loop.wake()
+ if (!document.hidden) gate.loop().wake()
}
- const onFocus = (): void => loop.wake()
+ const onFocus = (): void => gate.loop().wake()
window.addEventListener('focus', onFocus)
document.addEventListener('visibilitychange', onVisible)
- loop.start()
+ // Both stores, because either can move the board over this canvas: the view toggle, and a
+ // switch to a project that is already on it. `sync` is change-gated, so the projects store's
+ // chattier notifications (it is rewritten on every node serialization) cost a comparison.
+ const syncBoard = (): void => gate.sync()
+ const unsubView = useViewMode.subscribe(syncBoard)
+ const unsubProjects = useProjects.subscribe(syncBoard)
// Phase 2 owns real context restoration; here a lost context is simply the end of the shared
// renderer for this session. `preventDefault()` is deliberately NOT called — that is the
@@ -1191,11 +1295,15 @@ export function SharedGlyphLayer(): JSX.Element {
return () => {
// Everything the loop owns goes at once — the pending frame, the heartbeat TIMER and the
- // damage subscription. A surviving timer would keep calling `frame()` on a context this
- // effect run has let go of, and a surviving subscription would keep a dead loop reachable
- // from the engine. `stop()` is idempotent, so the `ctx.disposed` and `failSharedGlyph` paths
- // (which stop the loop themselves) land here safely too.
- loop.stop()
+ // damage subscription — plus the two store subscriptions that can hand it a new one. A
+ // surviving timer would keep calling `frame()` on a context this effect run has let go of, a
+ // surviving damage subscription would keep a dead loop reachable from the engine, and a
+ // surviving board subscription would BUILD a loop against a disposed context. `gate.stop()`
+ // is idempotent and also makes a `sync` still in flight inert, so the `ctx.disposed` and
+ // `failSharedGlyph` paths (which stop the loop themselves) land here safely too.
+ gate.stop()
+ unsubView()
+ unsubProjects()
damage.dispose()
window.removeEventListener('focus', onFocus)
document.removeEventListener('visibilitychange', onVisible)