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.

fix(glyphgrid): selection-elevated grid z, announce every disposal, one failure funnel, no post-dispose frame

enesd1565424fbe6
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 959fc52ec..8fc3eb301 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -4967,9 +4967,12 @@ export function Canvas() {
// false for every user today, the signature memo short-circuits on it, and setSharedGlyphCamera
// is a field write plus a null check when no engine exists.
const glyphLayerActive = useSharedGlyphActive()
- // The terminal nodes' paint order IS the grids' z order (array order = React Flow's own DOM
- // paint order). One string so the effect below fires on a real reorder only — a drag, an edit
- // or a selection rebuilds `nodes` many times per second with the order untouched.
+ // The terminal nodes' paint order IS the grids' z order — array order, with SELECTED nodes
+ // elevated above unselected ones, mirroring React Flow's own `elevateNodesOnSelect` (on by
+ // default). `nodeOrderSig` owns that rule; its comment explains why a grid z that ignored
+ // selection punches a hole through the selected node's text. One string so the effect below
+ // fires on a real order change only — a drag or an edit rebuilds `nodes` many times per second
+ // with the order untouched.
const glyphOrderSig = useMemo(
() => (glyphLayerActive ? nodeOrderSig(nodes) : ''),
[glyphLayerActive, nodes]
diff --git a/src/renderer/canvas/SharedGlyphLayer.test.ts b/src/renderer/canvas/SharedGlyphLayer.test.ts
index fb9663318..6083c59f3 100644
--- a/src/renderer/canvas/SharedGlyphLayer.test.ts
+++ b/src/renderer/canvas/SharedGlyphLayer.test.ts
@@ -1,5 +1,6 @@
-import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
+ failSharedGlyph,
getSharedGlyphContext,
idsFromOrderSig,
nodeOrderSig,
@@ -56,6 +57,34 @@ describe('nodeOrderSig', () => {
expect(a).not.toBe(b)
})
+ it('elevates a SELECTED terminal above the unselected ones, wherever it sits in the array', () => {
+ // React Flow's elevateNodesOnSelect (default on) lifts the selected node's DOM to z 1000; a
+ // grid z that followed the array alone would leave its text under the other grid's plate.
+ const sig = nodeOrderSig([
+ { id: 'a', type: 'terminal' },
+ { id: 'b', type: 'terminal', selected: true },
+ { id: 'c', type: 'terminal' }
+ ])
+ expect(idsFromOrderSig(sig)).toEqual(['a', 'c', 'b'])
+ })
+
+ it('keeps selected nodes in their own relative order (a multi-select is stable)', () => {
+ const sig = nodeOrderSig([
+ { id: 'a', type: 'terminal', selected: true },
+ { id: 'b', type: 'terminal' },
+ { id: 'c', type: 'terminal', selected: true }
+ ])
+ expect(idsFromOrderSig(sig)).toEqual(['b', 'a', 'c'])
+ })
+
+ it('an all-selected canvas keeps plain array order', () => {
+ const sig = nodeOrderSig([
+ { id: 'a', type: 'terminal', selected: true },
+ { id: 'b', type: 'terminal', selected: true }
+ ])
+ expect(idsFromOrderSig(sig)).toEqual(['a', 'b'])
+ })
+
it('an untyped node is not a terminal (React Flow defaults type to "default")', () => {
expect(nodeOrderSig([{ id: 'a' }])).toBe('')
})
@@ -68,6 +97,15 @@ describe('nodeOrderSig', () => {
})
describe('useSharedGlyph store', () => {
+ // The failure funnel warns by design; silence it here and assert the once-ness explicitly.
+ let warn: ReturnType<typeof vi.spyOn>
+ beforeEach(() => {
+ warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ })
+ afterEach(() => {
+ warn.mockRestore()
+ })
+
it('starts off, ungenerated and unfailed — the default-mode user must see nothing', () => {
expect(useSharedGlyph.getState()).toMatchObject({ enabled: false, generation: 0, failed: false })
expect(sharedGlyphActive()).toBe(false)
@@ -85,10 +123,32 @@ describe('useSharedGlyph store', () => {
expect(useSharedGlyph.getState().generation).toBe(1)
})
- it('markFailed is idempotent — a second failure must not re-notify', () => {
+ it('markFailed is idempotent — a second failure must not re-notify or re-log', () => {
useSharedGlyph.getState().markFailed()
useSharedGlyph.getState().markFailed()
expect(useSharedGlyph.getState().generation).toBe(1)
+ expect(warn).toHaveBeenCalledTimes(1)
+ })
+
+ it('markFailed goes through the SAME funnel as failSharedGlyph (no half-failed state)', () => {
+ // The store action must not be a shortcut that flips the flag while the GPU context stays
+ // held: it delegates, so the two entries are interchangeable and the second one is a no-op.
+ failSharedGlyph('test')
+ useSharedGlyph.getState().markFailed()
+ expect(useSharedGlyph.getState()).toMatchObject({ failed: true, generation: 1 })
+ expect(warn).toHaveBeenCalledTimes(1)
+ })
+
+ it('disabling the mode bumps the generation — a disposed context must be announced', () => {
+ // setEnabled(false) drops the context; every registered grid is now holding an inert handle
+ // and would stay blank until it remounted without this signal.
+ useSharedGlyph.getState().setEnabled(true)
+ expect(useSharedGlyph.getState().generation).toBe(0)
+ useSharedGlyph.getState().setEnabled(false)
+ expect(useSharedGlyph.getState().generation).toBe(1)
+ // Change-gated: a repeated disable disposes nothing, so it announces nothing.
+ useSharedGlyph.getState().setEnabled(false)
+ expect(useSharedGlyph.getState().generation).toBe(1)
})
it('sharedGlyphActive is enabled AND not failed', () => {
@@ -167,6 +227,14 @@ describe('setNodeZOrder', () => {
})
describe('graceful degrade without a GPU', () => {
+ let warn: ReturnType<typeof vi.spyOn>
+ beforeEach(() => {
+ warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ })
+ afterEach(() => {
+ warn.mockRestore()
+ })
+
it('getSharedGlyphContext returns null while the shared mode is off — no context is acquired', () => {
expect(getSharedGlyphContext()).toBeNull()
})
diff --git a/src/renderer/canvas/SharedGlyphLayer.tsx b/src/renderer/canvas/SharedGlyphLayer.tsx
index 0052db45b..a1c84b398 100644
--- a/src/renderer/canvas/SharedGlyphLayer.tsx
+++ b/src/renderer/canvas/SharedGlyphLayer.tsx
@@ -61,8 +61,10 @@ interface SharedGlyphState {
/** Resolved renderer mode is 'shared'. Set by App.tsx (T6); false for every user today, which
* is what keeps this whole module inert. */
enabled: boolean
- /** Bumped whenever every registered grid must be re-registered: the context was rebuilt (font
- * change) or the session failed. TerminalNode subscribes to exactly this number. */
+ /** Bumped whenever every registered grid must let go and re-evaluate: the context was rebuilt
+ * (font change), the mode was turned off, or the session failed. TerminalNode subscribes to
+ * exactly this number — the context it then asks for answers what to do next (a new engine, or
+ * null = back to the DOM renderer). */
generation: number
/** Session-level "the shared renderer is off for good" — set by the rAF catch or a lost
* context. Deliberately NOT auto-cleared: a GPU that just threw at us gets one chance per app
@@ -80,24 +82,27 @@ export const useSharedGlyph = create<SharedGlyphState>((set, get) => ({
setEnabled(on) {
if (get().enabled === on) return
set({ enabled: on })
+ if (on) return
// Turning the mode OFF hands the GPU context back. The whole point of the shared renderer is
// that contexts are scarce (see terminal/webgl-budget.ts), so leaving one — plus a 16 MB
// atlas — parked for a mode the user just left would be the exact cost this feature exists to
// remove. Turning it ON creates nothing: the first terminal that asks does that, lazily.
- if (!on) disposeContext()
+ disposeContext()
+ // ...and the disposal MUST be announced. Every registered grid is now holding an inert handle;
+ // without the bump a terminal that subscribes to `generation` alone would keep writing rows
+ // into nothing and stay blank until it remounted. Same one-signal contract the failure path
+ // honors — never dispose the context without bumping.
+ set({ generation: get().generation + 1 })
},
bumpGeneration() {
set({ generation: get().generation + 1 })
},
markFailed() {
- // Idempotent: a lost context typically arrives as BOTH a throwing frame and a
- // `webglcontextlost` event, and a second state write would ask every terminal to re-register
- // into a context that is already gone.
- if (get().failed) return
- // The generation rides along so a node that only subscribes to `generation` still wakes up —
- // it will ask for the context, get null, and stay on the DOM renderer. One signal, one
- // subscription.
- set({ failed: true, generation: get().generation + 1 })
+ // Delegates so that "the session failed" has exactly ONE implementation: warn once, drop the
+ // context, then flip the flag. A store action that only flipped the flag would be a reachable
+ // half-failure — the flag set, the GPU context still held — and it is the shape a caller
+ // naturally reaches for.
+ failSharedGlyph('marked failed')
}
}))
@@ -124,19 +129,35 @@ let zOrder = new Map<string, number>()
let zOrderSig = ''
const zListeners = new Set<() => void>()
-/** The paint order of the TERMINAL nodes, as one string. Canvas recomputes it per nodes change
- * and only pushes when it differs — a string compare instead of an array diff, the same trick
- * the data-signature effect next to it uses.
+/**
+ * The paint order of the TERMINAL nodes, as one string. Canvas recomputes it per nodes change and
+ * only pushes when it differs — a string compare instead of an array diff, the same trick the
+ * data-signature effect next to it uses.
*
- * Only terminals: they are the only kind that registers a grid, and mixing other kinds in would
- * churn the signature on every sticky edit. */
-export function nodeOrderSig(nodes: readonly { id: string; type?: string }[]): string {
- let sig = ''
+ * Only terminals: they are the only kind that registers a grid, and mixing other kinds in would
+ * churn the signature on every sticky edit.
+ *
+ * **The rule is array order, with SELECTED nodes elevated above unselected ones** (stable within
+ * each group) — not array order alone. React Flow's `elevateNodesOnSelect` defaults to true, so
+ * selecting a node lifts its DOM z-index to 1000 regardless of where it sits in the array. With a
+ * grid z that only followed the array, clicking a terminal that overlaps another would raise its
+ * chrome while its TEXT stayed underneath the other grid's opaque plate — the node visibly on top
+ * with a hole punched through its glyphs. Mirroring the elevation here is what keeps the canvas and
+ * the DOM telling the same story.
+ */
+export function nodeOrderSig(
+ nodes: readonly { id: string; type?: string; selected?: boolean }[]
+): string {
+ let base = ''
+ let elevated = ''
for (const n of nodes) {
if (n.type !== 'terminal') continue
- sig = sig === '' ? n.id : sig + ORDER_SEP + n.id
+ if (n.selected) elevated = elevated === '' ? n.id : elevated + ORDER_SEP + n.id
+ else base = base === '' ? n.id : base + ORDER_SEP + n.id
}
- return sig
+ if (base === '') return elevated
+ if (elevated === '') return base
+ return base + ORDER_SEP + elevated
}
export function idsFromOrderSig(sig: string): string[] {
@@ -187,6 +208,12 @@ interface LiveContext extends SharedGlyphContext {
gl: GlyphGL
/** Font settings this context was rasterized for — a change tears it down. */
fontKey: string
+ /** Set by `disposeContext`, read by the rAF driver, which holds this object across a frame.
+ * Teardown is SYNCHRONOUS (a settings change, the mode being switched off) and can land
+ * between a scheduled tick and its callback — a frame submitted after `gl.dispose()` would
+ * throw against deleted GPU objects, and the driver's catch would read that as a GPU failure
+ * and burn the whole session. A font-size change must not be able to disable the renderer. */
+ disposed: boolean
}
let live: LiveContext | null = null
@@ -275,7 +302,7 @@ function createContext(): LiveContext | null {
const atlas = new GlyphAtlas(raster, ATLAS_PAGE_PX)
const engine = new GlyphGridEngine(gl, atlas)
engine.setCamera(lastCamera)
- return { engine, atlas, canvas, gl, fontKey: fontKeyOf(fontFamily, fontSize) }
+ return { engine, atlas, canvas, gl, fontKey: fontKeyOf(fontFamily, fontSize), disposed: false }
}
/** Tear the context down and free the GPU objects. Every registered grid is left holding an
@@ -286,6 +313,9 @@ function disposeContext(): void {
live = null
creationAttempted = false
if (!ctx) return
+ // BEFORE any GL call: a rAF tick already scheduled with this context must skip its frame rather
+ // than submit against deleted objects (see `LiveContext.disposed`).
+ ctx.disposed = true
try {
ctx.engine.disposeAll()
ctx.gl.dispose()
@@ -301,14 +331,20 @@ function disposeContext(): void {
ctx.canvas.remove()
}
-/** Warn, drop the context, and put the session on the DOM renderer. The ONE failure funnel. */
+/** Warn, drop the context, and put the session on the DOM renderer. The ONE failure funnel — the
+ * store's `markFailed()` delegates here, and this writes the flag DIRECTLY (calling back into the
+ * action would recurse). */
export function failSharedGlyph(reason: string, err?: unknown): void {
// Checked before the warn so a lost context that arrives twice (throwing frame + event) logs
- // once.
- if (useSharedGlyph.getState().failed) return
+ // once, and so a second failure never re-notifies the registrants.
+ const store = useSharedGlyph.getState()
+ if (store.failed) return
console.warn(`[glyphgrid] shared renderer disabled for this session (${reason})`, err)
disposeContext()
- useSharedGlyph.getState().markFailed()
+ // The generation rides along so a node that only subscribes to `generation` still wakes up —
+ // it will ask for the context, get null, and stay on the DOM renderer. One signal, one
+ // subscription.
+ useSharedGlyph.setState({ failed: true, generation: useSharedGlyph.getState().generation + 1 })
}
/** Rebuild on a terminal font change: the atlas is rasterized for one font, so a new one means a
@@ -407,6 +443,11 @@ export function SharedGlyphLayer(): JSX.Element {
let raf = 0
const tick = (): void => {
+ // The context can be torn down synchronously between the schedule and this callback (font
+ // change, mode switched off). Skipping the frame — and NOT rescheduling — leaves the loop to
+ // the effect run that the accompanying generation bump triggers; the cleanup below cancels
+ // this handle either way.
+ if (ctx.disposed) return
try {
ctx.engine.frame()
} catch (err) {