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(terminal): resync repaint re-enters tmux's alt screen and mouse modes
enes978c71919818
diff --git a/CLAUDE.md b/CLAUDE.mdindex e47622937..96ad1dc24 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -1205,8 +1205,16 @@ seed** — the cases are:resync has superseded the seed, since that repaint may already be on screen). Without it arenderer reload left every terminal on the normal buffer, piling up to 10k lines of scrollbackand forcing a layout per output frame — measured 16.1% vs 7.3% CPU for one terminal at- 20 lines/s, 313 vs 1 forced layouts per 20 s. Note `repaintResync`'s `term.reset()` also drops a- terminal back to the normal buffer; that is not addressed here.+ 20 lines/s, 313 vs 1 forced layouts per 20 s. **A resync repaint loses the same two modes**:+ `repaintResync`'s `term.reset()` drops ANY terminal — the solo spawn too, not only a joiner —+ back to the normal buffer and clears mouse tracking, and resyncs happen under backpressure, i.e.+ on exactly the streaming terminals the alt switch exists for. So every create now reports+ `PtyCreateResult.tmuxClient` (same `tmuxBacked && !sessionHost` gate, set on spawn AND join), and+ `repaintResync` writes `CO_ATTACH_ALT_SCREEN_SEQ + CO_ATTACH_MOUSE_SEQ` between the reset and the+ paint when it is set. Optional on purpose: an older core or relay peer omits it and gets the old+ behavior (nothing re-applied), never a guess. The recycle banner ("session restarted by another+ user") is written AFTER the joiner's seed paint for the same reason — written before the alt+ switch, it sat in a buffer nobody could see.xterm's own `scrollback` (`xtermScrollback(settings.tmuxScrollback)`, floored at 1000, capped at`XTERM_SCROLLBACK_MAX` = 10000) is kept for the sessions tmux does *not* back (a plain shell whendiff --git a/src/core/pty-coattach.test.ts b/src/core/pty-coattach.test.tsindex ff36b8fa4..323cb3eed 100644--- a/src/core/pty-coattach.test.ts+++ b/src/core/pty-coattach.test.ts@@ -1267,6 +1267,7 @@ describe('a tmux-backed joiner is told to enable mouse tracking', () => {screen?: stringcoAttachMouse?: booleancoAttachAltScreen?: boolean+ tmuxClient?: boolean}>it('sets coAttachMouse on a joiner whose grid EQUALS the pty (the screen-painted branch)', async () => {@@ -1341,6 +1342,30 @@ describe('a tmux-backed joiner is told to enable mouse tracking', () => {expect(a.coAttachAltScreen).toBeUndefined()})+ // A resync's `term.reset()` drops ANY tmux client (the solo spawn too, not only a joiner) out of+ // the alternate buffer and clears its mouse tracking. `tmuxClient` tells the renderer to put+ // both back — so it rides EVERY tmux-backed create, never a plain shell's.+ it('sets tmuxClient on the SOLO tmux spawn and on a tmux join', async () => {+ const m = await tmuxManager()+ vi.spyOn(m, 'captureForResync').mockResolvedValue('current screen')+ const a = await create(ALICE, 80, 24)+ expect(a.tmuxClient).toBe(true)+ const b = await create(BOB, 80, 24)+ expect(b.tmuxClient).toBe(true)+ })++ it('never sets tmuxClient for a plain shell (spawn or join)', async () => {+ const { PtyManager } = await import('./pty-manager')+ const m = new PtyManager()+ m.registerIpc()+ vi.spyOn(m, 'captureForResync').mockResolvedValue('current screen')+ const a = await create(ALICE, 80, 24)+ expect(a.tmuxClient).toBeUndefined()+ const b = await create(BOB, 80, 24)+ expect(b.fresh).toBe(false)+ expect(b.tmuxClient).toBeUndefined()+ })+it('does NOT set coAttachAltScreen on a plain-shell join (its scrollback is the only history)', async () => {const { PtyManager } = await import('./pty-manager')const m = new PtyManager()diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.tsindex 5c6a22792..456915823 100644--- a/src/core/pty-manager.ts+++ b/src/core/pty-manager.ts@@ -2176,6 +2176,9 @@ export class PtyManager {// Same source, different question (and different consumer): a joiner needs to know whether the// session it landed on survives losing a client, because its own unmount may park it.const persistent = !!existing.persistKey+ // The resync repaint's question, asked of every session (PtyCreateResult.tmuxClient) — for a+ // join it is the alt-screen gate verbatim.+ const tmuxClient = coAttachAltScreenconst base: PtyCreateResult = existing.accountFallback? {sessionId: existingId,@@ -2183,9 +2186,10 @@ export class PtyManager {accountFallback: true,coAttachMouse,coAttachAltScreen,+ tmuxClient,persistent}- : { sessionId: existingId, fresh: false, coAttachMouse, coAttachAltScreen, persistent }+ : { sessionId: existingId, fresh: false, coAttachMouse, coAttachAltScreen, tmuxClient, persistent }if (resized) return Promise.resolve(base) // tmux is redrawing this client — do not paint twice// An empty capture (plain shell — no tmux to capture; a tmux/ssh blip) is OMITTED, never sent// as '': the renderer must not reset a terminal for nothing. A plain-shell joiner therefore@@ -2421,6 +2425,9 @@ export class PtyManager {...(freshUnverified && !fresh ? { freshUnverified: true as const } : {}),...(accountFallback ? { accountFallback } : {}),...(spawned?.sessionHost ? { sessionHost: true as const } : {}),+ // A tmux client (local or remote), so a resync's `term.reset()` must re-apply the modes+ // tmux emitted at attach — see PtyCreateResult.tmuxClient. Same gate as the join's alt screen.+ ...(spawned?.tmuxBacked && !spawned.sessionHost ? { tmuxClient: true as const } : {}),...(staleCwd ? { staleCwd: true as const } : {}),...(screen ? { screen } : {})}diff --git a/src/core/pty-session-host.test.ts b/src/core/pty-session-host.test.tsindex b2c8e4423..3774abde6 100644--- a/src/core/pty-session-host.test.ts+++ b/src/core/pty-session-host.test.ts@@ -631,10 +631,17 @@ describe('PtyManager session-host contracts', () => {const create = host.handlers[IPC.ptyCreate]const options = { cols: 80, rows: 24, persistKey: 'node-alt-screen' }- await create(7, options)- const joined = (await create(8, options)) as { fresh: boolean; coAttachAltScreen?: boolean }+ const spawned = (await create(7, options)) as { tmuxClient?: boolean }+ // Not a tmux client either, so a resync repaint re-applies no alt screen / tmux mouse.+ expect(spawned.tmuxClient).toBeUndefined()+ const joined = (await create(8, options)) as {+ fresh: boolean+ coAttachAltScreen?: boolean+ tmuxClient?: boolean+ }expect(joined.fresh).toBe(false) // it did join the live session…expect(joined.coAttachAltScreen).toBeUndefined() // …but there is no tmux alt screen to restore+ expect(joined.tmuxClient).toBeUndefined()})it('leaves the live generation joinable when the confirmed backend kill rejects', async () => {diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsxindex 677d65ca2..13cd51d26 100644--- a/src/renderer/nodes/TerminalNode.tsx+++ b/src/renderer/nodes/TerminalNode.tsx@@ -3178,6 +3178,7 @@ export function TerminalNode({cursor,coAttachMouse,coAttachAltScreen,+ tmuxClient,persistent,sessionHost,unavailable@@ -3363,7 +3364,7 @@ export function TerminalNode({if (!shouldApplyResync(resyncScreen)) returnsuperseded = truerelieve(gate.reset())- repaintResync(term, resyncScreen, () => !life.dead)+ repaintResync(term, resyncScreen, () => !life.dead, tmuxClient === true)}))}diff --git a/src/renderer/terminal/terminal-config.test.ts b/src/renderer/terminal/terminal-config.test.tsindex 7be6a7c18..2541f3d6f 100644--- a/src/renderer/terminal/terminal-config.test.ts+++ b/src/renderer/terminal/terminal-config.test.ts@@ -30,6 +30,8 @@ import {terminalLineHeight,xtermOptionsFromSettings,RESYNC_NOTICE,+ CO_ATTACH_ALT_SCREEN_SEQ,+ CO_ATTACH_MOUSE_SEQ,SHIFT_ENTER_SEQ,TERMINAL_LETTER_SPACING_MAX,TERMINAL_LETTER_SPACING_MIN,@@ -804,6 +806,29 @@ describe('repaintResync', () => {])})+ // `reset()` drops a tmux client out of the alternate buffer and clears its mouse tracking, and+ // tmux re-sends neither — so a streaming terminal that was resynced would pile its output into+ // the normal buffer's scrollback and lose wheel scrolling for good.+ it('re-enters the alt buffer and re-enables the mouse for a tmux client, between reset and paint', () => {+ const term = fakeTerm()+ repaintResync(term, 'FRESH', () => true, true)+ term.parse()+ expect(term.ops).toEqual([+ 'write:',+ 'reset',+ `write:${CO_ATTACH_ALT_SCREEN_SEQ}${CO_ATTACH_MOUSE_SEQ}`,+ 'write:FRESH',+ `write:${RESYNC_NOTICE}`+ ])+ })++ it('re-applies nothing for a non-tmux session (a plain shell keeps its normal-buffer history)', () => {+ const term = fakeTerm()+ repaintResync(term, 'FRESH', () => true, false)+ term.parse()+ expect(term.ops).toEqual(['write:', 'reset', 'write:FRESH', `write:${RESYNC_NOTICE}`])+ })+it('coalescing is per terminal and per round (a later, separate resync still paints)', () => {const term = fakeTerm()repaintResync(term, 'ONE')diff --git a/src/renderer/terminal/terminal-config.ts b/src/renderer/terminal/terminal-config.tsindex 86043ac95..75720e21f 100644--- a/src/renderer/terminal/terminal-config.ts+++ b/src/renderer/terminal/terminal-config.ts@@ -590,14 +590,28 @@ const resyncGeneration = new WeakMap<ResyncTarget, number>()* parsed into yet, so #1 parses onto the fresh screen and #2 stacks below it. Only the LATEST* capture is painted; superseded ones write nothing (they are strictly older screens of the same* terminal, so there is nothing in them to lose).- */-export function repaintResync(term: ResyncTarget, screen: string, alive?: () => boolean): void {+ *+ * `tmuxClient` (PtyCreateResult.tmuxClient): the reset also drops a tmux client out of the alternate+ * buffer and clears its mouse tracking; set, both are re-applied between the reset and the paint.+ * Omitted, nothing is re-applied (a plain shell / session host, or a core that does not say).+ */+export function repaintResync(+ term: ResyncTarget,+ screen: string,+ alive?: () => boolean,+ tmuxClient = false+): void {const generation = (resyncGeneration.get(term) ?? 0) + 1resyncGeneration.set(term, generation)term.write('', () => {if (alive && !alive()) returnif (resyncGeneration.get(term) !== generation) return // a newer capture supersedes this oneterm.reset()+ // `reset()` also dropped us to the NORMAL buffer and cleared mouse tracking, and tmux will not+ // re-send either (it emits them once, at attach). For a tmux client, re-enter the alternate+ // buffer and re-enable the mouse BEFORE the paint — entering the alt buffer clears the display.+ // See PtyCreateResult.tmuxClient.+ if (tmuxClient) term.write(CO_ATTACH_ALT_SCREEN_SEQ + CO_ATTACH_MOUSE_SEQ)term.write(toXtermText(screen))term.write(RESYNC_NOTICE)})diff --git a/src/shared/types.ts b/src/shared/types.tsindex 5cd158a13..06271d656 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -282,6 +282,17 @@ export interface PtyCreateResult {* normal-buffer scrollback is the only history it has.*/coAttachAltScreen?: boolean+ /**+ * This session's client is a real TMUX client (local or remote; never a session-host session or a+ * plain shell) — set on EVERY create, the solo spawn as well as a join. A `pty:resync` repaint+ * (`repaintResync`) calls `term.reset()`, which drops the emulator back to the NORMAL buffer and+ * clears mouse tracking; tmux does not re-send either (it emitted them once, at attach), so the+ * renderer re-applies `CO_ATTACH_ALT_SCREEN_SEQ` + `CO_ATTACH_MOUSE_SEQ` after the reset when+ * this is set. Same condition as `coAttachAltScreen`, asked of every session rather than only a+ * joiner. Absent = unknown (an older core or relay peer) ⇒ the renderer re-applies nothing, the+ * pre-field behavior.+ */+ tmuxClient?: boolean/*** This session is TMUX-BACKED (local or remote) — it survives losing this client, so killing our* pty client only detaches us and everything running in the session keeps going.