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.

feat(glyphgrid): inert disposed handles, gated setViewport, damage-safe frame errors

enes2f1a3bf53266
diff --git a/src/renderer/glyphgrid/engine.test.ts b/src/renderer/glyphgrid/engine.test.ts
index 31cdd2a3f..16bed332b 100644
--- a/src/renderer/glyphgrid/engine.test.ts
+++ b/src/renderer/glyphgrid/engine.test.ts
@@ -249,3 +249,56 @@ describe('GlyphGridEngine', () => {
expect(e.frame()).toBe(true)
})
})
+
+describe('lifecycle hardening', () => {
+ it('a disposed handle is inert: writes do nothing and create no damage', () => {
+ const e = new GlyphGridEngine(fakeGL(), atlas())
+ e.setViewport(800, 600, 1)
+ e.setCamera({ x: 0, y: 0, zoom: 1 })
+ const h = e.register(spec('a', 0))
+ e.frame() // consume registration damage
+ h.dispose()
+ e.frame() // consume dispose damage
+ h.updateRow(0, new Uint32Array(2 * CELL_STRIDE))
+ h.setOrigin(99, 99)
+ h.setZ(42)
+ h.resize(4, 4)
+ expect(e.frame()).toBe(false) // nothing woke the engine
+ })
+
+ it('setViewport with identical (w, h, dpr) is a no-op; a dpr change alone dirties', () => {
+ const gl = fakeGL()
+ const e = new GlyphGridEngine(gl, atlas())
+ e.setViewport(800, 600, 2)
+ e.frame()
+ e.setViewport(800, 600, 2)
+ expect(e.frame()).toBe(false)
+ e.setViewport(800, 600, 3) // dpr-only change must still resize + dirty
+ expect(e.frame()).toBe(true)
+ })
+
+ it('same-shape resize is a no-op (content preserved, no damage)', () => {
+ const e = new GlyphGridEngine(fakeGL(), atlas())
+ e.setViewport(800, 600, 1)
+ e.setCamera({ x: 0, y: 0, zoom: 1 })
+ const h = e.register(spec('a', 0))
+ e.frame()
+ h.resize(2, 1) // same shape as spec()
+ expect(e.frame()).toBe(false)
+ })
+
+ it('a throwing GL submission does not lose damage', () => {
+ const gl = fakeGL()
+ const e = new GlyphGridEngine(gl, atlas())
+ e.setViewport(800, 600, 1)
+ e.setCamera({ x: 0, y: 0, zoom: 1 })
+ e.register(spec('a', 0))
+ const boom = new Error('context lost mid-frame')
+ ;(gl as { beginFrame: unknown }).beginFrame = () => {
+ throw boom
+ }
+ expect(() => e.frame()).toThrow(boom)
+ ;(gl as { beginFrame: unknown }).beginFrame = () => undefined
+ expect(e.frame()).toBe(true) // damage was restored, next frame redraws
+ })
+})
diff --git a/src/renderer/glyphgrid/engine.ts b/src/renderer/glyphgrid/engine.ts
index ddd244283..386f49bd2 100644
--- a/src/renderer/glyphgrid/engine.ts
+++ b/src/renderer/glyphgrid/engine.ts
@@ -35,6 +35,9 @@ export interface GridHandle {
setOrigin(x: number, y: number): void
setZ(z: number): void
resize(cols: number, rows: number): void
+ /** Drops the grid. After this the handle is INERT — every mutator above becomes a silent
+ * no-op rather than a throw: a torn-down owner delivering one last write is a teardown
+ * race, not a bug, and it must neither mutate a dead grid nor un-idle the shared canvas. */
dispose(): void
}
@@ -64,6 +67,9 @@ export class GlyphGridEngine {
private camera: Camera = { x: 0, y: 0, zoom: 1 }
private viewW = 1
private viewH = 1
+ /** Stored so setViewport can change-gate on all THREE inputs. 0 = never sized, so the first
+ * setViewport always reaches the GL surface even if it passes the current w/h. */
+ private viewDpr = 0
private dirty = false
private seq = 0
/** False until an atlas source has actually reached the GPU — see `atlasUploadPending`. */
@@ -85,8 +91,15 @@ export class GlyphGridEngine {
this.grids.set(spec.id, grid)
this.dirty = true
const engine = this
+ // Per-handle, not per-grid: once THIS handle is disposed every mutator below is a silent
+ // no-op. The constraint is that a stale handle must not un-idle the shared canvas — a
+ // Phase-1b terminal is torn down while its last row write may still be in flight, and a
+ // write that dirtied the engine after dispose would keep one canvas redrawing forever for
+ // a grid nobody draws. Inert rather than throwing: the race is expected at teardown.
+ let disposed = false
return {
updateRow(row, cells) {
+ if (disposed) return
if (row < 0 || row >= grid.rows)
throw new Error(`glyphgrid: row ${row} out of range (rows=${grid.rows})`)
if (cells.length !== grid.cols * CELL_STRIDE)
@@ -95,17 +108,20 @@ export class GlyphGridEngine {
engine.dirty = true
},
setOrigin(x, y) {
+ if (disposed) return
if (grid.originX === x && grid.originY === y) return
grid.originX = x
grid.originY = y
engine.dirty = true
},
setZ(z) {
+ if (disposed) return
if (grid.z === z) return
grid.z = z
engine.dirty = true
},
resize(cols, rows) {
+ if (disposed) return
// A same-shape resize is a no-op, not a realloc: resize callers are size observers
// that fire on every layout tick, and reallocating + dirtying there would keep the
// canvas redrawing forever.
@@ -118,6 +134,9 @@ export class GlyphGridEngine {
engine.dirty = true
},
dispose() {
+ // The handle goes inert unconditionally — this is its owner declaring teardown, and it
+ // holds whether or not the map still points at this grid.
+ disposed = true
// Identity-checked: only drop the map entry if it is still THIS grid, so a stale
// handle can never evict a grid that re-registered under the same id.
if (engine.grids.get(grid.id) !== grid) return
@@ -134,8 +153,14 @@ export class GlyphGridEngine {
}
setViewport(w: number, h: number, dpr: number): void {
+ // Change-gated like setCamera, and on the dpr too: the caller is a resize observer that
+ // fires on every layout tick, so an unconditional dirty here would keep the canvas
+ // redrawing forever. A dpr-only change is a real change — same CSS box, different backing
+ // store — so it must still resize and dirty.
+ if (w === this.viewW && h === this.viewH && dpr === this.viewDpr) return
this.viewW = w
this.viewH = h
+ this.viewDpr = dpr
this.gl.resize(w, h, dpr)
this.dirty = true
}
@@ -170,29 +195,43 @@ export class GlyphGridEngine {
const uploadAtlas = this.atlasUploadPending()
if (!this.dirty && !uploadAtlas) return false
this.dirty = false
- if (uploadAtlas && this.atlas.source) {
- // BEFORE beginFrame, always (it reads back the atlas metrics as uniforms): see the
- // class contract.
- this.gl.uploadAtlas(this.atlas.source, this.atlas.sizePx, this.atlas.cellW, this.atlas.cellH)
- this.atlas.clearDirty()
- this.atlasUploaded = true
- }
- this.gl.beginFrame(this.camera)
- for (const id of this.drawOrder()) {
- const g = this.grids.get(id)
- if (!g) continue
- this.gl.drawGrid({
- cells: g.cells,
- cols: g.cols,
- rows: g.rows,
- cellW: g.cellW,
- cellH: g.cellH,
- originX: g.originX,
- originY: g.originY,
- bgColor: g.bgColor
- })
+ // Damage must survive a throwing submission: the dirty flag was already cleared, so a GL
+ // call that throws mid-frame (context lost, a driver error) would otherwise leave the
+ // engine idle on a half-drawn canvas until the next unrelated input. Restore the damage and
+ // RETHROW — the caller owns the error policy, and swallowing here would hide GPU errors.
+ try {
+ if (uploadAtlas && this.atlas.source) {
+ // BEFORE beginFrame, always (it reads back the atlas metrics as uniforms): see the
+ // class contract.
+ this.gl.uploadAtlas(
+ this.atlas.source,
+ this.atlas.sizePx,
+ this.atlas.cellW,
+ this.atlas.cellH
+ )
+ this.atlas.clearDirty()
+ this.atlasUploaded = true
+ }
+ this.gl.beginFrame(this.camera)
+ for (const id of this.drawOrder()) {
+ const g = this.grids.get(id)
+ if (!g) continue
+ this.gl.drawGrid({
+ cells: g.cells,
+ cols: g.cols,
+ rows: g.rows,
+ cellW: g.cellW,
+ cellH: g.cellH,
+ originX: g.originX,
+ originY: g.originY,
+ bgColor: g.bgColor
+ })
+ }
+ this.gl.endFrame()
+ } catch (err) {
+ this.dirty = true
+ throw err
}
- this.gl.endFrame()
return true
}
}
diff --git a/src/renderer/glyphgrid/harness/GlyphGridHarness.tsx b/src/renderer/glyphgrid/harness/GlyphGridHarness.tsx
index e535592a7..8bde0ce68 100644
--- a/src/renderer/glyphgrid/harness/GlyphGridHarness.tsx
+++ b/src/renderer/glyphgrid/harness/GlyphGridHarness.tsx
@@ -84,8 +84,9 @@ export function GlyphGridHarness() {
// A FRESH engine per effect run: `register` throws on a duplicate id, so reusing an engine
// across runs (StrictMode double-mount, HMR) would throw on the second pass.
const engine = new GlyphGridEngine(gl, atlas)
- // Viewport is set on mount and on resize ONLY — setViewport is not change-gated and resets
- // the drawing buffer, so calling it per rAF tick would reallocate the backing store 60x/s.
+ // Viewport is set on mount and on resize ONLY. setViewport is change-gated on (w, h, dpr)
+ // now, but the harness must not lean on that: an unchanged call is cheap, a changed one
+ // resets the drawing buffer, so driving it per rAF tick stays the wrong shape.
const applyViewport = (): void =>
engine.setViewport(window.innerWidth, window.innerHeight, window.devicePixelRatio)
applyViewport()