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
fix(glyphgrid): stable dimensions object, loud restore failure, leaf-checked internals
enes6005352f5838
diff --git a/src/renderer/glyphgrid/addon.test.ts b/src/renderer/glyphgrid/addon.test.tsindex f326f0551..c0efbbccf 100644--- a/src/renderer/glyphgrid/addon.test.ts+++ b/src/renderer/glyphgrid/addon.test.ts@@ -2,7 +2,11 @@ import { describe, expect, it } from 'vitest'import type { GlyphAtlas } from './atlas'import { CELL_STRIDE, FLAG_CURSOR, FLAG_SELECTED, packColor, readCell } from './cells'import type { GridHandle } from './engine'-import { GlyphGridRendererAddonCore, type TermInternals } from './addon'+import {+ GlyphGridRendererAddonCore,+ type DeviceMetrics,+ type TermInternals+} from './addon'import type { CellView, ThemeLanes } from './feed'const THEME: ThemeLanes = {@@ -225,7 +229,7 @@ describe('GlyphGridRendererAddonCore.handleResize', () => {expect(handle.rows[0].cells.length).toBe(8 * CELL_STRIDE)})- it('rebuilds dimensions from the new grid size', () => {+ it('updates dimensions from the new grid size', () => {const { core } = make({ cols: 4, rows: 6 })core.handleResize(10, 5)expect(core.dimensions.device.canvas.width).toBe(16 * 10)@@ -236,6 +240,30 @@ describe('GlyphGridRendererAddonCore.handleResize', () => {})describe('GlyphGridRendererAddonCore.dimensions', () => {+ it('is ONE object, mutated in place — xterm Viewport caches it by reference', () => {+ const { core, term } = make({ cols: 4, rows: 6 })+ const dims = core.dimensions+ const cssCell = dims.css.cell+ const deviceChar = dims.device.char++ core.handleResize(10, 5)+ expect(core.dimensions).toBe(dims)+ expect(core.dimensions.css.cell).toBe(cssCell)+ expect(cssCell.width).toBe(8)+ expect(dims.device.canvas.width).toBe(160)++ // A font-size change reaches us as new device metrics + handleCharSizeChanged.+ term.deviceMetrics = (): DeviceMetrics => ({ charW: 20, charH: 40, cellW: 20, cellH: 40 })+ core.handleCharSizeChanged()+ expect(core.dimensions).toBe(dims)+ expect(deviceChar.width).toBe(20)+ expect(dims.device.cell.height).toBe(40)+ expect(cssCell.height).toBe(20) // round(40 * 5 / dpr 2) / 5++ core.handleDevicePixelRatioChange()+ expect(core.dimensions).toBe(dims)+ })+it('mirrors xterm dimension shape at construction', () => {const { core } = make({ cols: 4, rows: 6 })expect(core.dimensions).toEqual({diff --git a/src/renderer/glyphgrid/addon.ts b/src/renderer/glyphgrid/addon.tsindex 0fa0ca48f..339ab9835 100644--- a/src/renderer/glyphgrid/addon.ts+++ b/src/renderer/glyphgrid/addon.ts@@ -74,9 +74,22 @@ function orderPoints(a: SelPoint, b: SelPoint): [SelPoint, SelPoint] {}export class GlyphGridRendererAddonCore {- /** Rebuilt on resize / char-size / dpr change; never reassigned in place, so a consumer holding- * the old object sees a consistent snapshot rather than a half-updated one. */- dimensions: RendererDims+ /** Allocated ONCE and mutated in place on resize / char-size / dpr change — never reassigned.+ *+ * xterm's `Viewport` caches this object BY REFERENCE (it keeps whatever `onDimensionsChange`+ * handed it) and re-reads only on the next dimensions event — which fires BEFORE our+ * `handleResize` runs, since RenderService registered its own listeners first. Hand out a FRESH+ * object and the Viewport goes on computing row height and scroll-area geometry from the+ * previous font size, with its `syncScrollArea` guard comparing stale to stale so it never+ * notices. Identity is the contract here, not an optimization. */+ readonly dimensions: RendererDims = {+ css: { canvas: { width: 0, height: 0 }, cell: { width: 0, height: 0 } },+ device: {+ canvas: { width: 0, height: 0 },+ cell: { width: 0, height: 0 },+ char: { width: 0, height: 0, left: 0, top: 0 }+ }+ }private cols: numberprivate rows: number@@ -118,7 +131,7 @@ export class GlyphGridRendererAddonCore {this.workCell = internals.makeWorkCell()this.focused = internals.hasFocus()this.lastCursorRow = this.cursorViewportRow()- this.dimensions = this.buildDims()+ this.updateDims()}// ---------------------------------------------------------------- xterm renderer surface@@ -148,7 +161,7 @@ export class GlyphGridRendererAddonCore {// The grid must be the new size BEFORE the first row of the new frame reaches it — an// updateRow of the new width against the old grid is rejected outright.this.handle.resize(cols, rows)- this.dimensions = this.buildDims()+ this.updateDims()this.packRows(0, rows - 1)}@@ -157,12 +170,12 @@ export class GlyphGridRendererAddonCore {* re-registers every node (see the shared layer), so there is nothing to repack here. */handleCharSizeChanged(): void {if (this.disposed) return- this.dimensions = this.buildDims()+ this.updateDims()}handleDevicePixelRatioChange(): void {if (this.disposed) return- this.dimensions = this.buildDims()+ this.updateDims()}/** Absolute buffer coords, straight from xterm's selection service.@@ -331,28 +344,29 @@ export class GlyphGridRendererAddonCore {/** Exactly xterm's own dimension math (DomRenderer._updateDimensions), which is why the shell* hands over DEVICE metrics rather than css ones: css.cell is derived from the ROUNDED css- * canvas, and xterm maps mouse coordinates through it. */- private buildDims(): RendererDims {+ * canvas, and xterm maps mouse coordinates through it.+ *+ * Every leaf is assigned IN PLACE — see the `dimensions` field. xterm's own renderers write+ * their dimensions object the same way, for the same reason. */+ private updateDims(): void {const m = this.internals.deviceMetrics()const dpr = this.internals.dpr() || 1const cols = Math.max(0, this.cols)const rows = Math.max(0, this.rows)- const deviceCanvasW = m.cellW * cols- const deviceCanvasH = m.cellH * rows- const cssCanvasW = Math.round(deviceCanvasW / dpr)- const cssCanvasH = Math.round(deviceCanvasH / dpr)- return {- css: {- canvas: { width: cssCanvasW, height: cssCanvasH },- // A zero-column grid would make these NaN, which propagates into xterm's mouse math.- cell: { width: cols > 0 ? cssCanvasW / cols : 0, height: rows > 0 ? cssCanvasH / rows : 0 }- },- device: {- canvas: { width: deviceCanvasW, height: deviceCanvasH },- cell: { width: m.cellW, height: m.cellH },- char: { width: m.charW, height: m.charH, left: 0, top: 0 }- }- }+ const d = this.dimensions+ d.device.char.width = m.charW+ d.device.char.height = m.charH+ d.device.char.left = 0+ d.device.char.top = 0+ d.device.cell.width = m.cellW+ d.device.cell.height = m.cellH+ d.device.canvas.width = m.cellW * cols+ d.device.canvas.height = m.cellH * rows+ d.css.canvas.width = Math.round(d.device.canvas.width / dpr)+ d.css.canvas.height = Math.round(d.device.canvas.height / dpr)+ // A zero-column grid would make these NaN, which propagates into xterm's mouse math.+ d.css.cell.width = cols > 0 ? d.css.canvas.width / cols : 0+ d.css.cell.height = rows > 0 ? d.css.canvas.height / rows : 0}private emitRedraw(start: number, end: number): void {diff --git a/src/renderer/terminal/glyphgrid-attach.ts b/src/renderer/terminal/glyphgrid-attach.tsindex b1b85b9a3..354534ec9 100644--- a/src/renderer/terminal/glyphgrid-attach.ts+++ b/src/renderer/terminal/glyphgrid-attach.ts@@ -54,15 +54,27 @@ interface XtermCore {/** Every internal read in this module starts here. Presence-checked rather than cast, so a bumped* xterm that dropped or renamed one of these degrades to "no glyphgrid" instead of throwing- * somewhere inside a render tick. */+ * somewhere inside a render tick.+ *+ * The LEAVES are checked too, not just the services holding them: `_charSizeService.width/height`+ * and `_coreBrowserService.dpr` are ARITHMETIC inputs, and a service that exists but reports+ * `undefined` (renamed field, not-yet-measured stub) would sail through a service-level check and+ * poison every dimension with NaN — the one thing `updateDims` guards its own math against. Same+ * for `buffer.lines.get`, which is called per cell inside a render tick. A partially-shaped+ * internal must produce the contracted null, not a broken terminal. */function coreOf(term: Terminal): XtermCore | null {try {const c = (term as unknown as { _core?: Partial<XtermCore> })._coreif (!c) return nullif (typeof c._createRenderer !== 'function') return nullif (!c._renderService || typeof c._renderService.setRenderer !== 'function') return null+ if (typeof c._renderService.handleResize !== 'function') return nullif (!c._themeService?.colors || typeof c._themeService.onChangeColors !== 'function') return null- if (!c._charSizeService || !c._coreBrowserService || !c._bufferService?.buffer) return null+ if (!Array.isArray(c._themeService.colors.ansi)) return null+ const cs = c._charSizeService+ if (typeof cs?.width !== 'number' || typeof cs?.height !== 'number') return null+ if (typeof c._coreBrowserService?.dpr !== 'number') return null+ if (typeof c._bufferService?.buffer?.lines?.get !== 'function') return nullif (!c.coreService) return nullreturn c as XtermCore} catch {@@ -119,13 +131,24 @@ function restoreDomRenderer(term: Terminal): boolean {}}+/** What a successful attach hands back. `dispose()` returns whether xterm is BACK ON ITS OWN DOM+ * RENDERER — false means the terminal is now painting nothing and needs an escalation (respawn),+ * the same call TerminalNode's stray-canvas heal makes. Do not ignore it. */+export interface GlyphGridAttachment {+ dispose(): boolean+}++function warnRestoreFailed(context: string): void {+ console.warn(`[glyphgrid] DOM renderer restore failed (${context}) — terminal needs a refresh`)+}+/** Point `term` at the shared glyph grid. Returns null — having touched nothing — when the* internals are not what we expect, so the caller simply stays on the DOM renderer. */export function attachGlyphGrid(term: Terminal,handle: GridHandle,atlas: GlyphAtlas-): { dispose(): void } | null {+): GlyphGridAttachment | null {const core = coreOf(term)if (!core) return nulllet theme = themeLanes(core._themeService.colors)@@ -166,7 +189,7 @@ export function attachGlyphGrid(} catch {// A throw here can leave xterm holding a half-installed renderer (setRenderer disposed the DOM// one first), so restore before reporting failure rather than just returning.- restoreDomRenderer(term)+ if (!restoreDomRenderer(term)) warnRestoreFailed('attach')return null}@@ -179,19 +202,25 @@ export function attachGlyphGrid(})let disposed = false+ let restored = falsereturn {- dispose(): void {- if (disposed) return+ dispose(): boolean {+ if (disposed) return restoreddisposed = truetry {themeSub.dispose()} catch {// already torn down with the terminal — fine}- restoreDomRenderer(term)+ restored = restoreDomRenderer(term)// Belt and braces: `setRenderer` disposes the addon for us, but if the restore failed the// addon must still go inert so it can never write into a grid the node is about to drop.addon.dispose()+ // NOT silent: a failed restore means xterm is holding a disposed renderer and the terminal+ // will paint nothing at all — invisible in a log-free teardown, and indistinguishable from+ // "the pty died" to the user staring at a blank node.+ if (!restored) warnRestoreFailed('dispose')+ return restored}}}