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
refactor(scrollback): move the xterm scrollback clamp into src/shared
review86ba7e56d085
diff --git a/src/renderer/terminal/terminal-config.test.ts b/src/renderer/terminal/terminal-config.test.tsindex 2541f3d6f..09b09345f 100644--- a/src/renderer/terminal/terminal-config.test.ts+++ b/src/renderer/terminal/terminal-config.test.ts@@ -1131,3 +1131,13 @@ describe('mergeProjectVisuals (a project\'s own theme / font over the global set})})})++describe('scrollback bounds are the shared rule (session host and renderer cannot drift)', () => {+ it('re-exports the one definition from @shared/scrollback', async () => {+ const shared = await import('@shared/scrollback')+ const config = await import('./terminal-config')+ expect(config.xtermScrollback).toBe(shared.xtermScrollback)+ expect(config.XTERM_SCROLLBACK_MAX).toBe(shared.XTERM_SCROLLBACK_MAX)+ expect(config.XTERM_SCROLLBACK_MIN).toBe(shared.XTERM_SCROLLBACK_MIN)+ })+})diff --git a/src/renderer/terminal/terminal-config.ts b/src/renderer/terminal/terminal-config.tsindex 75720e21f..bd34458a2 100644--- a/src/renderer/terminal/terminal-config.ts+++ b/src/renderer/terminal/terminal-config.ts@@ -7,6 +7,7 @@ import type {TerminalCursorStyle} from '@shared/types'import { isWindowsPlatform } from '@shared/platform-utils'+import { xtermScrollback } from '@shared/scrollback'import { isKnownTerminalThemeId, resolveTerminalTheme } from './themes'/**@@ -145,25 +146,16 @@ export function shouldApplyResync(screen: string | null | undefined): screen isreturn typeof screen === 'string' && screen.length > 0}-/** Hard cap on xterm's in-memory scrollback: the cost is per node and one canvas holds many. */-export const XTERM_SCROLLBACK_MAX = 10000--/**- * Floor, mirroring the tmux conf's own `history-limit ${Math.max(1000, scrollback)}`, so the two- * buffers never disagree at the low end.- */-export const XTERM_SCROLLBACK_MIN = 1000-/**- * How many scrollback lines xterm keeps. The WHEEL scrolls tmux (its mouse is on, and the pane is+ * Scrollback bounds now live in `@shared/scrollback` (the Windows session host's emulator uses the+ * same cap). Re-exported so existing renderer imports keep working.+ *+ * How many scrollback lines xterm keeps: the WHEEL scrolls tmux (its mouse is on, and the pane is* on the alternate screen), so this buffer is not what the user scrolls in a tmux session — it is- * the fallback for a session with no tmux (a plain shell) and the room a cold-restore snapshot- * replay needs. xterm's default of 1000 lines is too small for the latter, so it follows the same- * `settings.tmuxScrollback` the user picked for tmux's history-limit, floored and capped.+ * the fallback for a session with no tmux (a plain shell, the Windows session host) and the room a+ * cold-restore snapshot replay needs. It follows `settings.tmuxScrollback`, floored and capped.*/-export function xtermScrollback(tmuxScrollback: number): number {- return Math.min(Math.max(XTERM_SCROLLBACK_MIN, tmuxScrollback), XTERM_SCROLLBACK_MAX)-}+export { XTERM_SCROLLBACK_MAX, XTERM_SCROLLBACK_MIN, xtermScrollback } from '@shared/scrollback'/* ------------------------------------------------------------------------------------------- ** Terminal appearance — ONE source for both xterm call sites.diff --git a/src/shared/scrollback.test.ts b/src/shared/scrollback.test.tsnew file mode 100644index 000000000..695b6fe6f--- /dev/null+++ b/src/shared/scrollback.test.ts@@ -0,0 +1,32 @@+import { describe, expect, it } from 'vitest'+import { clampEmulatorScrollback, xtermScrollback } from './scrollback'++describe('xtermScrollback', () => {+ it.each([+ [50000, 10000],+ [10000, 10000],+ [5000, 5000],+ [1000, 1000],+ [500, 1000],+ [0, 1000]+ ])('maps tmuxScrollback %i to %i', (input, expected) => {+ expect(xtermScrollback(input)).toBe(expected)+ })+})++describe('clampEmulatorScrollback (host-side bound)', () => {+ it.each([+ [50000, 10000],+ [10000, 10000],+ [2000, 2000],+ // No floor: the protocol-v1 warm-only sentinel sends 1 and must not be reinterpreted.+ [1, 1],+ [0, 0],+ [-5, 0],+ [123.9, 123],+ [Number.NaN, 10000],+ [Number.POSITIVE_INFINITY, 10000]+ ])('clamps %s to %i', (input, expected) => {+ expect(clampEmulatorScrollback(input)).toBe(expected)+ })+})diff --git a/src/shared/scrollback.ts b/src/shared/scrollback.tsnew file mode 100644index 000000000..f5257c2e4--- /dev/null+++ b/src/shared/scrollback.ts@@ -0,0 +1,40 @@+/**+ * Scrollback bounds for every xterm-shaped buffer this app holds: the renderer's xterm AND the+ * Windows session host's `@xterm/headless` emulator (plus the main-process `NativeWindowsPane`+ * mirror). One module so the two ends of a session-host attach cannot disagree: anything the host+ * keeps above the renderer's cap is serialized, shipped and then thrown away on every warm attach.+ *+ * tmux's own `history-limit` is NOT bounded here — it keeps the raw `settings.tmuxScrollback`.+ * tmux stores only the cells a line actually uses, in C; xterm allocates `cols × 12 B` for EVERY+ * buffered line (`BufferLine` = `Uint32Array(cols * 3)`), so 50,000 lines at 120 cols is ~72 MB per+ * session in xterm and a few MB in tmux. See docs/windows-session-host.md ("Memory bound").+ */++/** Hard cap on an xterm buffer's scrollback: the cost is per node and one canvas holds many. */+export const XTERM_SCROLLBACK_MAX = 10000++/**+ * Floor, mirroring the tmux conf's own `history-limit ${Math.max(1000, scrollback)}`, so the two+ * buffers never disagree at the low end.+ */+export const XTERM_SCROLLBACK_MIN = 1000++/**+ * How many scrollback lines an xterm buffer keeps for a given `settings.tmuxScrollback`: floored+ * and capped. The renderer's xterm and the session host's emulator both use this, so the host+ * never holds history the renderer could not show.+ */+export function xtermScrollback(tmuxScrollback: number): number {+ return Math.min(Math.max(XTERM_SCROLLBACK_MIN, tmuxScrollback), XTERM_SCROLLBACK_MAX)+}++/**+ * The emulator-side bound, applied INSIDE the session host (and `TerminalEmulator`) to whatever a+ * client asked for — an older client still sends the raw 50,000. Deliberately no floor: the+ * protocol-v1 warm-only sentinel sends `1`, and a host must not reinterpret a request it did not+ * get. A non-finite value (hand-built frame) falls to the cap rather than reaching xterm as NaN.+ */+export function clampEmulatorScrollback(scrollback: number): number {+ if (!Number.isFinite(scrollback)) return XTERM_SCROLLBACK_MAX+ return Math.min(Math.max(0, Math.floor(scrollback)), XTERM_SCROLLBACK_MAX)+}