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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
feat(glyphgrid): shared layer — engine singleton, rAF driver, camera/z feed, DOM fallback paths
enes8fb80dd165d3
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsxindex e10101dd6..959fc52ec 100644--- a/src/renderer/canvas/Canvas.tsx+++ b/src/renderer/canvas/Canvas.tsx@@ -28,6 +28,14 @@ import {disposeParkedTerminal} from '../nodes/TerminalNode'import { solveFitPadding } from './fit-view'+import {+ SharedGlyphLayer,+ idsFromOrderSig,+ nodeOrderSig,+ setNodeZOrder,+ setSharedGlyphCamera,+ useSharedGlyphActive+} from './SharedGlyphLayer'import { SshReconnector } from '../lib/sshReconnect'import { terminalKey } from '../terminal/terminal-config'import { setWebglGesture, setWebglZoom, WEBGL_GESTURE_SETTLE_MS } from '../terminal/webgl-budget'@@ -1538,6 +1546,10 @@ export function Canvas() {// A project can load already zoomed way out (saved viewport) — seed the WebGL zoom gate// before the mount-time IntersectionObserver reports make every node request a context.setWebglZoom(project.viewport.zoom)+ // Seed the shared glyph camera from the same viewport: `onMove` only fires once the user+ // actually pans, so without this a project that loads scrolled away would draw its grids+ // against the previous project's camera until the first gesture.+ setSharedGlyphCamera(project.viewport)}// Let load-induced changes settle before we start tracking edits as dirty.const t = setTimeout(() => {@@ -4950,6 +4962,22 @@ export function Canvas() {}}, [nodes, markDirty])+ // ---- glyphgrid (experimental shared renderer) ----+ // Both feeds below are INERT unless the resolved renderer mode is 'shared': the hook returns+ // 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.+ const glyphOrderSig = useMemo(+ () => (glyphLayerActive ? nodeOrderSig(nodes) : ''),+ [glyphLayerActive, nodes]+ )+ useEffect(() => {+ setNodeZOrder(idsFromOrderSig(glyphOrderSig))+ }, [glyphOrderSig])+const zoomRafRef = useRef<number | null>(null)const gestureSettleRef = useRef<ReturnType<typeof setTimeout> | null>(null)const onMove = useCallback(@@ -4977,6 +5005,9 @@ export function Canvas() {// Feed the WebGL budget's zoom gate (suspend GPU rendering when zoomed way out).// Idempotent + hysteresis inside; per-frame call cost is a float compare.setWebglZoom(viewportRef.current.zoom)+ // Same coalesced frame feeds the shared glyph canvas: its camera and the DOM nodes'+ // transform must move together, or the text lags the node bodies by a frame.+ setSharedGlyphCamera(viewportRef.current)})}},@@ -7458,6 +7489,11 @@ export function Canvas() {size={2.5}color="#4a4a4a"/>+ {/* The shared glyph canvas: a <ReactFlow> child (so it is a sibling of the background+ and of the node renderer) at z-index 0 — above the dot grid, below every node. Only+ mounted in the experimental 'shared' renderer mode; nothing about it exists for the+ default modes. */}+ {glyphLayerActive && <SharedGlyphLayer />}<Controls showInteractive={false} position="bottom-left" onFitView={fitAll}><ControlButtonclassName={`canvas-lock-btn${canvasLocked ? ' locked' : ''}`}diff --git a/src/renderer/canvas/SharedGlyphLayer.test.ts b/src/renderer/canvas/SharedGlyphLayer.test.tsnew file mode 100644index 000000000..fb9663318--- /dev/null+++ b/src/renderer/canvas/SharedGlyphLayer.test.ts@@ -0,0 +1,188 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'+import {+ getSharedGlyphContext,+ idsFromOrderSig,+ nodeOrderSig,+ nodeZFor,+ setNodeZOrder,+ setSharedGlyphCamera,+ sharedGlyphActive,+ subscribeNodeZOrder,+ useSharedGlyph+} from './SharedGlyphLayer'++// Only the PURE parts are unit-testable here: there is no WebGL2, no OffscreenCanvas and no+// layout in the node test environment, so the component, the rAF driver and the GL singleton are+// device-verified (T6 checklist). What IS covered is everything the other tasks build on: the+// order signature Canvas derives, the store transitions T5/T6 read, and the z-order map + its+// notification seam.++beforeEach(() => {+ // The store and the z map are module singletons; reset them explicitly so the tests are+ // order-independent.+ useSharedGlyph.setState({ enabled: false, generation: 0, failed: false })+ setNodeZOrder([])+})++describe('nodeOrderSig', () => {+ it('joins TERMINAL node ids in array order', () => {+ const sig = nodeOrderSig([+ { id: 'a', type: 'terminal' },+ { id: 'b', type: 'terminal' }+ ])+ expect(idsFromOrderSig(sig)).toEqual(['a', 'b'])+ })++ it('ignores every other node kind — only terminals own a grid', () => {+ const sig = nodeOrderSig([+ { id: 's1', type: 'sticky' },+ { id: 't1', type: 'terminal' },+ { id: 'g1', type: 'group' },+ { id: 't2', type: 'terminal' },+ { id: 'sub', type: 'subagent' }+ ])+ expect(idsFromOrderSig(sig)).toEqual(['t1', 't2'])+ })++ it('is order sensitive — a reorder must produce a different signature', () => {+ const a = nodeOrderSig([+ { id: 'x', type: 'terminal' },+ { id: 'y', type: 'terminal' }+ ])+ const b = nodeOrderSig([+ { id: 'y', type: 'terminal' },+ { id: 'x', type: 'terminal' }+ ])+ expect(a).not.toBe(b)+ })++ it('an untyped node is not a terminal (React Flow defaults type to "default")', () => {+ expect(nodeOrderSig([{ id: 'a' }])).toBe('')+ })++ it('round-trips the empty canvas as an EMPTY list, not a one-element list of ""', () => {+ const sig = nodeOrderSig([])+ expect(sig).toBe('')+ expect(idsFromOrderSig(sig)).toEqual([])+ })+})++describe('useSharedGlyph store', () => {+ 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)+ })++ it('bumpGeneration increments (the re-register signal)', () => {+ useSharedGlyph.getState().bumpGeneration()+ useSharedGlyph.getState().bumpGeneration()+ expect(useSharedGlyph.getState().generation).toBe(2)+ })++ it('markFailed flips failed AND bumps the generation, so one subscription wakes every node', () => {+ useSharedGlyph.getState().markFailed()+ expect(useSharedGlyph.getState().failed).toBe(true)+ expect(useSharedGlyph.getState().generation).toBe(1)+ })++ it('markFailed is idempotent — a second failure must not re-notify', () => {+ useSharedGlyph.getState().markFailed()+ useSharedGlyph.getState().markFailed()+ expect(useSharedGlyph.getState().generation).toBe(1)+ })++ it('sharedGlyphActive is enabled AND not failed', () => {+ useSharedGlyph.getState().setEnabled(true)+ expect(sharedGlyphActive()).toBe(true)+ useSharedGlyph.getState().markFailed()+ expect(sharedGlyphActive()).toBe(false)+ })++ it('setEnabled is change-gated and survives a disable/enable round trip', () => {+ // Disabling also drops the GPU context; with none created (node env) that is a no-op, and it+ // must not throw or disturb the flags.+ useSharedGlyph.getState().setEnabled(true)+ useSharedGlyph.getState().setEnabled(true)+ expect(() => useSharedGlyph.getState().setEnabled(false)).not.toThrow()+ expect(sharedGlyphActive()).toBe(false)+ useSharedGlyph.getState().setEnabled(true)+ expect(sharedGlyphActive()).toBe(true)+ })++ it('setEnabled(false) after a failure keeps the session failed (no silent retry)', () => {+ useSharedGlyph.getState().setEnabled(true)+ useSharedGlyph.getState().markFailed()+ useSharedGlyph.getState().setEnabled(false)+ useSharedGlyph.getState().setEnabled(true)+ expect(useSharedGlyph.getState().failed).toBe(true)+ expect(sharedGlyphActive()).toBe(false)+ })+})++describe('setNodeZOrder', () => {+ it('maps each id to its index — array order IS the paint order', () => {+ setNodeZOrder(['a', 'b', 'c'])+ expect(nodeZFor('a')).toBe(0)+ expect(nodeZFor('b')).toBe(1)+ expect(nodeZFor('c')).toBe(2)+ })++ it('an id it has never seen lands on TOP, not at 0', () => {+ // A node created between two order pushes is appended last on the canvas, so "topmost" is the+ // answer that matches what the user sees; 0 would flash it under every other terminal.+ setNodeZOrder(['a', 'b'])+ expect(nodeZFor('fresh')).toBe(2)+ })++ it('notifies subscribers when the order changes', () => {+ const seen = vi.fn()+ const unsub = subscribeNodeZOrder(seen)+ setNodeZOrder(['a'])+ setNodeZOrder(['a', 'b'])+ expect(seen).toHaveBeenCalledTimes(2)+ unsub()+ })++ it('does NOT notify when the same order is pushed again', () => {+ setNodeZOrder(['a', 'b'])+ const seen = vi.fn()+ const unsub = subscribeNodeZOrder(seen)+ setNodeZOrder(['a', 'b'])+ expect(seen).not.toHaveBeenCalled()+ unsub()+ })++ it('stops notifying after unsubscribe', () => {+ const seen = vi.fn()+ subscribeNodeZOrder(seen)()+ setNodeZOrder(['z'])+ expect(seen).not.toHaveBeenCalled()+ })++ it('an emptied canvas clears the map', () => {+ setNodeZOrder(['a', 'b'])+ setNodeZOrder([])+ expect(nodeZFor('a')).toBe(0)+ })+})++describe('graceful degrade without a GPU', () => {+ it('getSharedGlyphContext returns null while the shared mode is off — no context is acquired', () => {+ expect(getSharedGlyphContext()).toBeNull()+ })++ it('returns null (rather than throwing) when WebGL2/OffscreenCanvas are unavailable', () => {+ useSharedGlyph.getState().setEnabled(true)+ expect(getSharedGlyphContext()).toBeNull()+ })++ it('returns null once the session has failed', () => {+ useSharedGlyph.getState().setEnabled(true)+ useSharedGlyph.getState().markFailed()+ expect(getSharedGlyphContext()).toBeNull()+ })++ it('setSharedGlyphCamera is inert without a context', () => {+ expect(() => setSharedGlyphCamera({ x: 10, y: 20, zoom: 2 })).not.toThrow()+ })+})diff --git a/src/renderer/canvas/SharedGlyphLayer.tsx b/src/renderer/canvas/SharedGlyphLayer.tsxnew file mode 100644index 000000000..0052db45b--- /dev/null+++ b/src/renderer/canvas/SharedGlyphLayer.tsx@@ -0,0 +1,446 @@+/**+ * The MOUNTING SURFACE of the glyphgrid renderer (Phase 1b): the one WebGL2 canvas every+ * terminal on the board paints into, the rAF driver that submits its frames, and the two feeds+ * the canvas owns — the camera and the node paint order.+ *+ * Three things live here and nowhere else:+ *+ * 1. **The engine singleton.** ONE GL context for the whole canvas is the entire point of the+ * shared renderer (the per-terminal path fights Chromium's ~16-context cap through+ * `terminal/webgl-budget.ts`; see the WebGL section of CLAUDE.md). So the context is a module+ * singleton, not component state: it must survive `<SharedGlyphLayer/>` remounting, and it is+ * created LAZILY — a user who never turns the experimental mode on must never cost a GPU+ * context, which is why `getSharedGlyphContext()` returns null while the mode is off.+ * 2. **The failure path.** `GlyphGridEngine.frame()` RETHROWS on a GL error by contract (it+ * restores its damage first), and a lost context is not recoverable in 1b. Both land on+ * `failSharedGlyph()`: warn once, tear the context down, and flip the store's `failed` — the+ * session-level "everyone back to the DOM renderer" signal that TerminalNode reads.+ * 3. **The seams the rest of the integration reads**: `useSharedGlyph` (mode + generation ++ * failure), `setNodeZOrder`/`nodeZFor`/`subscribeNodeZOrder` (paint order), and+ * `setSharedGlyphCamera` (pan/zoom). Every one of them is a no-op while no context exists, so+ * Canvas can call them unconditionally and a default-mode user pays a null check.+ *+ * The component and the GL singleton have NO unit tests by design — there is no WebGL2, no+ * OffscreenCanvas and no layout in vitest's node environment. The pure parts (order signature,+ * store transitions, z map) are tested; the rest is on the T6 device checklist. That split is the+ * same one `terminal/glyphgrid-attach.ts` makes for xterm's internals.+ */++import { useEffect, useRef } from 'react'+import { create } from 'zustand'+import { GlyphAtlas } from '../glyphgrid/atlas'+import type { Camera } from '../glyphgrid/camera'+import { GlyphGridEngine } from '../glyphgrid/engine'+import type { GlyphGL } from '../glyphgrid/gl'+import { createWebgl2GL } from '../glyphgrid/gl-webgl2'+import { createCanvasRasterizer } from '../glyphgrid/raster'+import { useSettings } from '../state/settings'++/** Atlas page edge, in DEVICE pixels. 2048 (not Phase 0's 1024) because the atlas cells are+ * device-sized: at dpr 2 a 13px terminal font is roughly a 16x38 device cell, so a 1024 page+ * holds ~1600 glyphs — enough for ASCII plus a little, and nothing like enough for a session+ * that also shows box drawing, powerline glyphs and CJK. A full page degrades to the blank slot+ * (GlyphAtlas never throws), i.e. missing text, so the page is sized for the pathological+ * session rather than the average one. 2048² RGBA = 16 MB of VRAM, once, for the whole canvas. */+const ATLAS_PAGE_PX = 2048++/** Fallback cell metrics as a fraction of the font size, used only when the 2D context cannot+ * measure (no OffscreenCanvas → we bail before this, or a font that reports nothing). Roughly a+ * monospace advance and line box. */+const FALLBACK_ADVANCE_RATIO = 0.6+const FALLBACK_LINE_RATIO = 1.2+/** A cell larger than this would make the atlas page hold almost nothing; clamped rather than+ * trusted, since the ratio comes from a user-typed font family. */+const MAX_CELL_PX = 256++// ---------------------------------------------------------------------------------------------+// Store — the reactive half of the seam+// ---------------------------------------------------------------------------------------------++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. */+ 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+ * run, and a retry loop is how you turn a bad driver into a flicker. */+ failed: boolean+ setEnabled(on: boolean): void+ bumpGeneration(): void+ markFailed(): void+}++export const useSharedGlyph = create<SharedGlyphState>((set, get) => ({+ enabled: false,+ generation: 0,+ failed: false,+ setEnabled(on) {+ if (get().enabled === on) return+ set({ enabled: on })+ // 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()+ },+ 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 })+ }+}))++/** Non-reactive gate for the imperative call sites (TerminalNode's register path). */+export function sharedGlyphActive(): boolean {+ const s = useSharedGlyph.getState()+ return s.enabled && !s.failed+}++/** Reactive form of `sharedGlyphActive()` — Canvas mounts the layer on this. */+export function useSharedGlyphActive(): boolean {+ return useSharedGlyph((s) => s.enabled && !s.failed)+}++// ---------------------------------------------------------------------------------------------+// Node paint order+// ---------------------------------------------------------------------------------------------++/** The separator for the order signature. NUL cannot occur in a node id (they are nanoid/uuid+ * shaped), so `split` is an exact inverse of `join`. */+const ORDER_SEP = '\u0000'++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.+ *+ * 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 = ''+ for (const n of nodes) {+ if (n.type !== 'terminal') continue+ sig = sig === '' ? n.id : sig + ORDER_SEP + n.id+ }+ return sig+}++export function idsFromOrderSig(sig: string): string[] {+ return sig === '' ? [] : sig.split(ORDER_SEP)+}++/** Publish the terminal paint order. Change-gated on the joined signature: the registered grids+ * re-read their z from the notification, and re-pushing an unchanged order would walk every+ * terminal on the canvas for nothing. */+export function setNodeZOrder(ids: readonly string[]): void {+ const sig = ids.join(ORDER_SEP)+ if (sig === zOrderSig) return+ zOrderSig = sig+ const next = new Map<string, number>()+ ids.forEach((id, i) => next.set(id, i))+ zOrder = next+ for (const fn of zListeners) fn()+}++/** The z a grid should carry. An id we have not been told about yet lands on TOP: a node created+ * between two order pushes is appended last on the canvas, so "topmost" matches what the user+ * sees, while 0 would flash it underneath every other terminal for a frame. */+export function nodeZFor(id: string): number {+ const z = zOrder.get(id)+ return z === undefined ? zOrder.size : z+}++/** Grid owners subscribe here and re-read `nodeZFor` — the engine change-gates `setZ`, so a+ * no-op reorder costs one comparison per grid. */+export function subscribeNodeZOrder(fn: () => void): () => void {+ zListeners.add(fn)+ return () => {+ zListeners.delete(fn)+ }+}++// ---------------------------------------------------------------------------------------------+// The engine singleton+// ---------------------------------------------------------------------------------------------++export interface SharedGlyphContext {+ engine: GlyphGridEngine+ atlas: GlyphAtlas+}++interface LiveContext extends SharedGlyphContext {+ canvas: HTMLCanvasElement+ gl: GlyphGL+ /** Font settings this context was rasterized for — a change tears it down. */+ fontKey: string+}++let live: LiveContext | null = null+/** One creation attempt per context lifetime (reset by `disposeContext`). Without it every+ * terminal mount would re-probe a machine that has already said "no WebGL2", and each probe is a+ * real `getContext` call. */+let creationAttempted = false+let settingsUnsub: (() => void) | null = null+/** The camera survives teardown so a rebuilt context opens where the user is looking, and so+ * Canvas can feed the camera before the layer has ever mounted (the project-load seed runs+ * first). Recording it is three number writes — cheaper than the null check it replaces. */+let lastCamera: Camera = { x: 0, y: 0, zoom: 1 }++function fontKeyOf(fontFamily: string, fontSize: number): string {+ return `${fontFamily}|${fontSize}`+}++/**+ * The atlas cell, in DEVICE pixels.+ *+ * Deliberately NOT xterm's measurement. xterm measures the advance of 'W' in a live DOM span per+ * terminal (`_charSizeService`), and the addon feeds those exact numbers to the ENGINE as the+ * grid's cell size — so cell GEOMETRY on screen is always xterm's own, and mouse coordinates+ * stay aligned. The number computed here decides only how many texels a glyph is rasterized+ * into; a fraction of a pixel of disagreement rescales the bitmap slightly, it can never move a+ * cell. Measuring here through the DOM instead would mean creating and laying out a probe+ * element from a module singleton before any terminal exists, for a strictly cosmetic gain.+ *+ * Rounded UP because every draw is clipped to its slot rect (see raster.ts): a slot a hair too+ * small clips the glyph, a slot a hair too big wastes atlas area.+ */+function measureCellDevicePx(family: string, sizePx: number): { cellW: number; cellH: number } {+ const fallback = {+ cellW: Math.max(1, Math.ceil(sizePx * FALLBACK_ADVANCE_RATIO)),+ cellH: Math.max(1, Math.ceil(sizePx * FALLBACK_LINE_RATIO))+ }+ try {+ const ctx = new OffscreenCanvas(8, 8).getContext('2d')+ if (!ctx) return fallback+ ctx.font = `${sizePx}px ${family}`+ const m = ctx.measureText('W')+ // `fontBoundingBox*` is the FONT's line box, not this glyph's ink box — the right metric for+ // a cell, and the one that keeps descenders inside their slot. Guarded because it is+ // optional in the spec (and undefined in some engines).+ const ascent = m.fontBoundingBoxAscent+ const descent = m.fontBoundingBoxDescent+ const w = Math.ceil(m.width)+ const h = Number.isFinite(ascent) && Number.isFinite(descent) ? Math.ceil(ascent + descent) : 0+ return {+ cellW: w > 0 ? Math.min(w, MAX_CELL_PX) : fallback.cellW,+ cellH: h > 0 ? Math.min(h, MAX_CELL_PX) : fallback.cellH+ }+ } catch {+ return fallback+ }+}++function createContext(): LiveContext | null {+ if (typeof document === 'undefined' || typeof OffscreenCanvas === 'undefined') return null+ const { fontFamily, fontSize } = useSettings.getState().settings+ const dpr = typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1+ // The atlas is rasterized at DEVICE resolution: the addon reports device-pixel cells, and a+ // CSS-sized atlas would be visibly soft on every retina display. The dpr is read ONCE, and+ // deliberately does not join the font key: moving the window to a monitor with a different dpr+ // leaves the glyphs rasterized for the old one (slightly soft or slightly over-sampled), which+ // is a Phase-2 sharpness question — rebuilding the atlas there would drop and re-register every+ // grid on the canvas for a cosmetic gain. The drawing BUFFER does follow the dpr, on every+ // resize (see the component's `pushViewport`), so geometry is never wrong.+ const devicePx = Math.max(1, fontSize * dpr)+ const { cellW, cellH } = measureCellDevicePx(fontFamily, devicePx)+ // Rasterizer FIRST: it needs no GPU, so a missing OffscreenCanvas bails out before a WebGL+ // context has been created (the harness learned this the expensive way — a context acquired+ // and then dropped unreferenced is exactly the leak this whole layer exists to avoid).+ const raster = createCanvasRasterizer(+ { family: fontFamily, sizePx: devicePx, cellW, cellH },+ ATLAS_PAGE_PX+ )+ if (!raster) return null+ const canvas = document.createElement('canvas')+ canvas.className = 'glyphgrid-canvas'+ // The GL layer sizes the BACKING STORE (canvas.width/height in device px); the CSS box is ours.+ canvas.style.cssText =+ 'position:absolute;inset:0;width:100%;height:100%;display:block;pointer-events:none'+ const gl = createWebgl2GL(canvas)+ if (!gl) return 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) }+}++/** Tear the context down and free the GPU objects. Every registered grid is left holding an+ * INERT handle (`disposeAll`'s contract), so a terminal that writes one more row during+ * teardown is a no-op rather than a throw. */+function disposeContext(): void {+ const ctx = live+ live = null+ creationAttempted = false+ if (!ctx) return+ try {+ ctx.engine.disposeAll()+ ctx.gl.dispose()+ // `gl.dispose()` frees the buffers/texture/program but NOT the context itself, and the+ // browser's live-context cap counts contexts, not objects. A rebuild creates a fresh canvas+ // (a canvas hands back the same context forever), so the old one must be explicitly lost or+ // every font change permanently spends one of the ~16 slots the whole app shares.+ const raw = ctx.canvas.getContext('webgl2')+ raw?.getExtension('WEBGL_lose_context')?.loseContext()+ } catch (err) {+ console.warn('[glyphgrid] teardown error (continuing)', err)+ }+ ctx.canvas.remove()+}++/** Warn, drop the context, and put the session on the DOM renderer. The ONE failure funnel. */+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+ console.warn(`[glyphgrid] shared renderer disabled for this session (${reason})`, err)+ disposeContext()+ useSharedGlyph.getState().markFailed()+}++/** Rebuild on a terminal font change: the atlas is rasterized for one font, so a new one means a+ * new atlas — and the engine's grids are re-registered by their owners on the generation bump.+ *+ * Never unsubscribed, deliberately: the context it watches is a module singleton with no owner+ * to outlive, and a subscription dropped at teardown would miss the font change that happens+ * while the mode is momentarily off. It is one closure for the life of the renderer process. */+function ensureSettingsSubscription(): void {+ if (settingsUnsub) return+ settingsUnsub = useSettings.subscribe((s) => {+ if (!live) return+ if (live.fontKey === fontKeyOf(s.settings.fontFamily, s.settings.fontSize)) return+ disposeContext()+ useSharedGlyph.getState().bumpGeneration()+ })+}++/** The singleton, created on first use. Internal because it also exposes the canvas + GL, which+ * only this file's component may touch. */+function ensureLiveContext(): LiveContext | null {+ if (!sharedGlyphActive()) return null+ if (live) return live+ if (creationAttempted) return null+ creationAttempted = true+ ensureSettingsSubscription()+ live = createContext()+ return live+}++/**+ * The shared engine + atlas, created on first use. Null means "stay on the DOM renderer": the+ * mode is off, the session has failed, or this machine has no WebGL2/OffscreenCanvas.+ */+export function getSharedGlyphContext(): SharedGlyphContext | null {+ return ensureLiveContext()+}++/** Camera feed. Always records (so a context created later opens at the right place) and only+ * reaches the engine — which change-gates it — when one exists. */+export function setSharedGlyphCamera(cam: Camera): void {+ lastCamera = { x: cam.x, y: cam.y, zoom: cam.zoom }+ live?.engine.setCamera(lastCamera)+}++// ---------------------------------------------------------------------------------------------+// The component+// ---------------------------------------------------------------------------------------------++/**+ * Hosts the shared canvas inside the React Flow pane and drives its frames.+ *+ * **Stacking.** React Flow's own layers, from `@xyflow/react`'s base.css: `.react-flow__background`+ * is z-index -1 and `.react-flow__renderer` (which contains the pane and the transformed viewport+ * holding every node) is z-index 4, all inside the `.react-flow` wrapper's own stacking context.+ * Children passed to `<ReactFlow>` — this component, like `<Background/>` — render as siblings of+ * both. z-index 0 therefore puts the glyph canvas ABOVE the dot grid and BELOW the nodes, which is+ * where it has to be: node bodies stay opaque in v1, so the DOM keeps owning node chrome and+ * stacking, and the canvas only orders grids among themselves.+ *+ * **Not inside the viewport.** The canvas is screen-space — the engine applies the camera itself+ * in the shader — so it must not sit under React Flow's CSS transform, which would scale its+ * pixels instead.+ */+export function SharedGlyphLayer(): JSX.Element {+ const hostRef = useRef<HTMLDivElement>(null)+ const generation = useSharedGlyph((s) => s.generation)+ const failed = useSharedGlyph((s) => s.failed)++ useEffect(() => {+ const host = hostRef.current+ if (!host || failed) return+ const ctx = ensureLiveContext()+ if (!ctx) return+ const canvas = ctx.canvas+ host.appendChild(canvas)++ const pushViewport = (): void => {+ // The engine change-gates all three inputs, so calling this from every observer tick is+ // free when nothing actually moved.+ ctx.engine.setViewport(+ host.clientWidth,+ host.clientHeight,+ typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1+ )+ }+ pushViewport()++ // ResizeObserver, not just window resize: the pane also changes size when a drawer or the+ // sessions sidebar opens, and a stale viewport culls the wrong grids.+ const ro = new ResizeObserver(pushViewport)+ ro.observe(host)+ // ...and window resize on top of it, because a DPR change (dragging the window to a second+ // monitor) leaves the CSS box identical while the backing store must change.+ window.addEventListener('resize', pushViewport)++ let raf = 0+ const tick = (): void => {+ try {+ ctx.engine.frame()+ } catch (err) {+ // `frame()` rethrows by contract, having restored its own damage. There is no recovery in+ // 1b: the whole session goes back to the DOM renderer.+ failSharedGlyph('frame threw', err)+ return // no reschedule — the cleanup below still cancels the (already fired) handle+ }+ raf = requestAnimationFrame(tick)+ }+ raf = requestAnimationFrame(tick)++ // 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+ // "please fire webglcontextrestored" request, and we have nothing to restore into.+ const onLost = (): void => failSharedGlyph('context lost')+ canvas.addEventListener('webglcontextlost', onLost)++ return () => {+ cancelAnimationFrame(raf)+ ro.disconnect()+ window.removeEventListener('resize', pushViewport)+ canvas.removeEventListener('webglcontextlost', onLost)+ // The canvas leaves the DOM with the host div; the CONTEXT stays alive for the next mount+ // (a project switch must not cost a context rebuild). Only `failSharedGlyph` and a font+ // change dispose it.+ }+ }, [generation, failed])++ return (+ <div+ ref={hostRef}+ className="glyphgrid-layer"+ style={{ position: 'absolute', inset: 0, zIndex: 0, pointerEvents: 'none' }}+ />+ )+}