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.

perf(terminal): park xterm in renderer instead of tmux client in main

enes228d17101977
diff --git a/src/main/pty-manager.ts b/src/main/pty-manager.ts
index 15edb2f36..d3350a60f 100644
--- a/src/main/pty-manager.ts
+++ b/src/main/pty-manager.ts
@@ -244,11 +244,6 @@ interface Session {
/** True when this node had an `accountId` but its config dir was gone at spawn, so we fell back
* to the system account. `create()` surfaces it to the renderer (warning chip). */
accountFallback?: boolean
- /** Parked: the renderer detached (node unmount / project switch) but the client process is kept
- * attached for PARK_KEEP_MS so a quick remount reuses it — no respawn, no attach round-trip.
- * Output is discarded while parked; `parkTimer` fires the real detach. */
- parked?: boolean
- parkTimer?: ReturnType<typeof setTimeout> | null
}
/** Sinks for a detached session whose output is served somewhere other than the renderer
@@ -264,13 +259,6 @@ export interface DetachedSinks {
const FLUSH_MS = 8
const MAX_BUF_BYTES = 256 * 1024
-// Keep-warm window after the renderer detaches (project switch / node unmount): the tmux client
-// process stays attached and is reused if the node remounts within it, so reattach becomes a
-// resize + `refresh-client` (tmux's own pixel-perfect full redraw) instead of a process spawn +
-// attach round-trip per node. After the window the client is truly detached, bounding the idle
-// cost (one ~1 MB tmux client + a drained pty fd per parked node).
-const PARK_KEEP_MS = 5 * 60 * 1000
-
/**
* Manages all live PTY processes and bridges them to the renderer over IPC.
*
@@ -375,19 +363,6 @@ export class PtyManager {
webContentsId: number,
options: PtyCreateOptions
): Promise<PtyCreateResult> {
- // Keep-warm reuse: if this node detached moments ago (project switch), its client process is
- // still attached (parked). Rebind it to the new renderer, match the size, and have tmux
- // repaint the full screen — no spawn, no attach round-trip, no session-exists probe.
- if (options.persistKey) {
- for (const [sid, s] of this.sessions) {
- if (s.persistKey === options.persistKey && s.parked && !s.sshRemote) {
- this.unpark(s, webContentsId, options)
- return s.accountFallback
- ? { sessionId: sid, fresh: false, accountFallback: true }
- : { sessionId: sid, fresh: false }
- }
- }
- }
// A tmux-backed session is "fresh" (cold start) when no live session exists to reattach to
// — i.e. first open, or after a machine reboot killed the tmux server. Plain (non-tmux)
// sessions are always fresh: they have no cross-restart continuity. The renderer uses this
@@ -756,7 +731,6 @@ export class PtyManager {
proc.onData((data) => this.queueData(sessionId, session, data))
proc.onExit(({ exitCode }) => {
- if (session.parkTimer) clearTimeout(session.parkTimer) // died while parked (e.g. destroy)
this.flush(sessionId, session) // deliver any buffered output before the exit signal
if (session.onExit) {
session.onExit(exitCode)
@@ -772,10 +746,6 @@ export class PtyManager {
/** Buffer a chunk; flush immediately past the byte cap, otherwise on a short timer. */
private queueData(sessionId: string, session: Session, data: string): void {
session.outputSinceSnapshot = true
- // Parked (renderer detached, kept warm): nobody is listening — drop the chunk. The pty must
- // keep draining so the tmux client isn't backpressured; the periodic scrollback snapshot
- // still captures the pane from the tmux server directly.
- if (session.parked) return
session.buf.push(data)
session.bufBytes += data.length
if (session.bufBytes >= MAX_BUF_BYTES) {
@@ -843,76 +813,12 @@ export class PtyManager {
// Skipped when nothing arrived since the last periodic capture (pane content is unchanged).
if (session.persistKey && session.outputSinceSnapshot)
void this.snapshotScrollback(session.persistKey, session.sshRemote)
- // Keep-warm: a local tmux-backed renderer session parks (client kept attached, reused by a
- // remount within PARK_KEEP_MS — see create()) instead of detaching immediately. Remote (ssh),
- // detached-sink (relay host), and non-persistent sessions keep the immediate detach.
- if (session.persistKey && !session.sshRemote && !session.onData && this.tmuxPath) {
- this.park(sessionId, session)
- return
- }
// releasePty (not proc.kill()): a paused pty never reads EOF, so kill() alone leaks the
// master fd on every detach until the process runs out of descriptors (see pty-release.ts).
releasePty(session.proc as ReleasablePty)
this.sessions.delete(sessionId)
}
- /** Park a detaching session: keep the tmux client attached for PARK_KEEP_MS, drop output on
- * the floor meanwhile, then truly detach if no remount reclaimed it. */
- private park(sessionId: string, session: Session): void {
- session.parked = true
- session.webContentsId = null
- // Drop anything buffered for the departed renderer, and make sure the pty keeps draining:
- // the renderer may have left flow paused at a high watermark, and a paused parked pty
- // would backpressure the tmux client forever.
- session.buf = []
- session.bufBytes = 0
- try {
- session.proc.resume()
- } catch {
- // already exited; onExit cleans up
- }
- session.parkTimer = setTimeout(() => {
- session.parkTimer = null
- if (!session.parked) return
- releasePty(session.proc as ReleasablePty)
- this.sessions.delete(sessionId)
- }, PARK_KEEP_MS)
- }
-
- /** Rebind a parked session to a (re)mounting renderer: cancel the detach timer, restore IPC
- * forwarding, match the requested size, and have tmux repaint the whole screen. */
- private unpark(session: Session, webContentsId: number, options: PtyCreateOptions): void {
- if (session.parkTimer) clearTimeout(session.parkTimer)
- session.parkTimer = null
- session.parked = false
- session.webContentsId = webContentsId
- if (session.proc.cols !== options.cols || session.proc.rows !== options.rows)
- session.proc.resize(Math.max(1, options.cols), Math.max(1, options.rows))
- // The new xterm is empty: ask tmux for a full client repaint (the same redraw an attach
- // would stream — pixel-perfect, unlike replaying a captured snapshot).
- if (session.persistKey) void this.refreshClient(session.persistKey)
- }
-
- /** Force a full tmux redraw of the (single, `-D`) client attached to a node's session. */
- private async refreshClient(persistKey: string): Promise<void> {
- if (!this.tmuxPath) return
- try {
- const { stdout } = await runAsync(this.tmuxPath, [
- '-L',
- TMUX_SOCKET,
- 'list-clients',
- '-t',
- sessionName(persistKey),
- '-F',
- '#{client_tty}'
- ])
- const tty = stdout.trim().split('\n')[0]
- if (tty) await runAsync(this.tmuxPath, ['-L', TMUX_SOCKET, 'refresh-client', '-t', tty])
- } catch {
- // session/client gone — the next create() spawns a fresh attach instead.
- }
- }
-
/**
* Capture a session's output. `full` grabs the entire scrollback (`-S -`, for the
* markdown view); otherwise the recent ~200 lines (AI naming, palette search).
@@ -1049,7 +955,6 @@ export class PtyManager {
const finals: Promise<unknown>[] = []
for (const session of this.sessions.values()) {
if (session.flushTimer) clearTimeout(session.flushTimer)
- if (session.parkTimer) clearTimeout(session.parkTimer)
// Final scrollback snapshot on quit so a reboot can replay it. Skipped for sessions with
// no output since the last periodic capture (unchanged pane content).
if (session.persistKey && session.outputSinceSnapshot)
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index c714ae934..055b8e77c 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -17,7 +17,11 @@ import {
type Viewport
} from '@xyflow/react'
import type { Edge, Node } from '@xyflow/react'
-import { TerminalNode, setMoveIntoWorktreeHandler } from '../nodes/TerminalNode'
+import {
+ TerminalNode,
+ setMoveIntoWorktreeHandler,
+ disposeTerminalOnUnmount
+} from '../nodes/TerminalNode'
import { StickyNode } from '../nodes/StickyNode'
import { GroupNode, setWorktreeActionHandler } from '../nodes/GroupNode'
import { LazyEditorNode, LazyDiffNode } from '../nodes/lazyMonacoNodes'
@@ -1598,6 +1602,9 @@ export function Canvas() {
const set = new Set(ids)
nodesRef.current.forEach((n) => {
if (!set.has(n.id)) return
+ // Permanent delete: the upcoming unmount must dispose the xterm, not park it (the
+ // session is being destroyed right here). Also drops an already-parked entry.
+ if (n.type === 'terminal') disposeTerminalOnUnmount(n.id)
// Remote terminals have no local persistent session — only destroy local ones.
if (n.type === 'terminal' && !n.data.remote) transport.destroy(n.id)
// Chat nodes: permanently kill the SDK driver + drop the live chat state. The driver
@@ -2879,6 +2886,7 @@ export function Canvas() {
if (projectId === activeProjectId) {
deleteNodes([id])
} else {
+ disposeTerminalOnUnmount(id) // node may be parked from the project switch
transport.destroy(id)
// Chat nodes in an inactive project keep their driver running across the switch;
// dispose is a no-op for non-chat ids, so call it unconditionally like destroy.
@@ -3315,7 +3323,10 @@ export function Canvas() {
// persisted agent status (node unmount no longer removes it).
const project = store.getProject(id)
project?.nodes.forEach((n) => {
- if ((n.kind ?? 'terminal') === 'terminal') transport.destroy(n.id)
+ if ((n.kind ?? 'terminal') === 'terminal') {
+ disposeTerminalOnUnmount(n.id) // may be parked from a recent switch away
+ transport.destroy(n.id)
+ }
if ((n.kind ?? 'terminal') === 'chat') {
window.nodeTerminal.chat.dispose(n.id)
useChatSessions.getState().drop(n.id)
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index 862947ea1..7785314c1 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -104,6 +104,57 @@ export function setMoveIntoWorktreeHandler(fn: ((nodeId: string) => void) | null
moveIntoWorktreeHandler = fn
}
+/**
+ * Parked terminals: when a node unmounts (project switch), its xterm instance and live PTY
+ * session are kept — the `.xterm` element is detached from the DOM and held here — so a remount
+ * within TERM_PARK_MS re-adopts them instead of respawning. This makes switching back to a
+ * project instant AND exact: the tmux client never detaches, so the full terminal state
+ * (alternate screen, mouse-tracking modes, scrollback, cursor) carries over with no redraw and
+ * no mode re-negotiation to get wrong. After the window the entry is disposed for real (the
+ * PTY client detaches; the tmux session itself keeps running, as always).
+ */
+interface ParkedTerminal {
+ term: Terminal
+ fit: FitAddon
+ search: SearchAddon
+ transport: TerminalTransport
+ sessionId: string
+ sentCols: number
+ sentRows: number
+ /** Session-scoped teardown (transport/xterm listeners) — run only at final dispose. */
+ cleanups: Array<() => void>
+ timer: ReturnType<typeof setTimeout>
+}
+const parkedTerminals = new Map<string, ParkedTerminal>()
+const TERM_PARK_MS = 5 * 60 * 1000
+
+function disposeParked(p: ParkedTerminal): void {
+ clearTimeout(p.timer)
+ p.cleanups.forEach((fn) => fn())
+ p.transport.kill(p.sessionId)
+ p.term.dispose()
+}
+
+/** Drop a node's parked terminal (if any), detaching its PTY client. Canvas calls this on
+ * permanent deletion — the node may be unmounted (parked) when it is deleted. */
+export function disposeParkedTerminal(id: string): void {
+ const p = parkedTerminals.get(id)
+ if (!p) return
+ parkedTerminals.delete(id)
+ disposeParked(p)
+}
+
+/** Nodes whose next unmount must dispose (not park) — set on permanent deletion, where the
+ * unmount runs AFTER the session was already destroyed, so parking would keep a dead xterm. */
+const noParkIds = new Set<string>()
+
+/** Canvas calls this when permanently deleting a terminal node: drops an already-parked entry
+ * AND makes the upcoming unmount (if the node is currently mounted) dispose instead of park. */
+export function disposeTerminalOnUnmount(id: string): void {
+ noParkIds.add(id)
+ disposeParkedTerminal(id)
+}
+
/**
* A single terminal node: header (collapse + color + title + close), optional tag chips,
* and a real xterm.js terminal. A hover guard delays entering the terminal so the canvas
@@ -142,6 +193,11 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
const [mdHtml, setMdHtml] = useState('')
const [editingTitle, setEditingTitle] = useState(false)
const hoveredRef = useRef(false)
+ // Render-fresh respawnNonce for the lifecycle cleanup: React updates this ref (render) before
+ // running the old effect's cleanup, so the cleanup can tell a respawn (nonce changed → dispose,
+ // spawn fresh) from a plain unmount (nonce unchanged → park for quick re-adoption).
+ const respawnNonceRef = useRef(data.respawnNonce)
+ respawnNonceRef.current = data.respawnNonce
// Live mirrors for the once-mounted onTitleChange listener (its `[]`-deps closure can't see
// fresh props/state): whether the title still auto-tracks the session, whether the rename box
// is open (don't clobber mid-edit), and the current title (skip no-op updates).
@@ -256,47 +312,79 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
const container = bodyRef.current
if (!container) return
+ // Adopt-or-create: a parked terminal (this node unmounted less than TERM_PARK_MS ago) is
+ // re-adopted with its live PTY session and full xterm state intact; otherwise a fresh
+ // xterm + session are built. `myNonce` vs the render-updated ref tells the cleanup below
+ // whether it runs for a respawn (worktree move — must NOT park) or a plain unmount.
+ const myNonce = data.respawnNonce
+ const parked = parkedTerminals.get(id)
+ if (parked) {
+ parkedTerminals.delete(id)
+ clearTimeout(parked.timer)
+ }
+
const s = useSettings.getState().settings
- const term = new Terminal({
- fontFamily: s.fontFamily,
- fontSize: s.fontSize,
- cursorBlink: s.cursorBlink,
- theme: { background: '#1e1e1e', foreground: '#e6e6e6' },
- allowProposedApi: true
- })
+ const term =
+ parked?.term ??
+ new Terminal({
+ fontFamily: s.fontFamily,
+ fontSize: s.fontSize,
+ cursorBlink: s.cursorBlink,
+ theme: { background: '#1e1e1e', foreground: '#e6e6e6' },
+ allowProposedApi: true
+ })
+ const fit = parked ? parked.fit : new FitAddon()
+ const searchAddon = parked ? parked.search : new SearchAddon()
termRef.current = term
- const fit = new FitAddon()
fitRef.current = fit
- term.loadAddon(fit)
- const searchAddon = new SearchAddon()
searchAddonRef.current = searchAddon
- term.loadAddon(searchAddon)
- term.open(container)
+
// GPU renderer: xterm's default DOM renderer doesn't scale to many terminals streaming
// at once. Must load after open(). Browsers cap live WebGL contexts (~16), so with many
// mounted terminals the oldest context gets evicted — onContextLoss disposes the addon
- // and that terminal falls back to the DOM renderer instead of going blank.
- try {
- const webgl = new WebglAddon()
- webgl.onContextLoss(() => webgl.dispose())
- term.loadAddon(webgl)
- } catch {
- // WebGL2 unavailable — DOM renderer remains active.
+ // and that terminal falls back to the DOM renderer instead of going blank. A parked
+ // terminal gave its context up on park, so both paths (re)acquire one here.
+ let webgl: WebglAddon | null = null
+ const loadWebgl = () => {
+ try {
+ const addon = new WebglAddon()
+ addon.onContextLoss(() => addon.dispose())
+ term.loadAddon(addon)
+ webgl = addon
+ } catch {
+ // WebGL2 unavailable — DOM renderer remains active.
+ }
}
- fit.fit()
- patchTerminalScale(term, getZoom)
- // OSC 52 clipboard write: the remote tmux (set-clipboard on) emits OSC 52 on copy; route the
- // decoded text to the Mac clipboard. WRITE-ONLY — `parseOsc52` returns null for a `?` read
- // query so a remote program can never read the local clipboard. Returning true swallows the
- // sequence (also the read query). This is additive: the local tmux conf also has set-clipboard
- // on, so local tmux DOES emit OSC 52 and this handler fires too — a harmless redundant write of
- // the same selection (pbcopy already wrote it), NOT a no-op.
- term.parser.registerOscHandler(52, (data) => {
- const text = parseOsc52(data)
- if (text !== null) window.nodeTerminal.clipboard.writeText(text)
- return true
- })
+ if (parked) {
+ // Reattach the parked xterm's DOM element: the PTY never detached, so the screen is
+ // already current — no spawn, no tmux redraw, no terminal-mode re-negotiation.
+ if (term.element) container.appendChild(term.element)
+ loadWebgl()
+ try {
+ fit.fit()
+ } catch {
+ // zero-size (collapsed) — the ResizeObserver below refits when it grows
+ }
+ } else {
+ term.loadAddon(fit)
+ term.loadAddon(searchAddon)
+ term.open(container)
+ loadWebgl()
+ fit.fit()
+ patchTerminalScale(term, getZoom)
+ // OSC 52 clipboard write: the remote tmux (set-clipboard on) emits OSC 52 on copy; route the
+ // decoded text to the Mac clipboard. WRITE-ONLY — `parseOsc52` returns null for a `?` read
+ // query so a remote program can never read the local clipboard. Returning true swallows the
+ // sequence (also the read query). This is additive: the local tmux conf also has set-clipboard
+ // on, so local tmux DOES emit OSC 52 and this handler fires too — a harmless redundant write of
+ // the same selection (pbcopy already wrote it), NOT a no-op.
+ term.parser.registerOscHandler(52, (data) => {
+ const text = parseOsc52(data)
+ if (text !== null) window.nodeTerminal.clipboard.writeText(text)
+ return true
+ })
+ }
// Cmd+C copies the terminal selection (xterm renders to canvas, so the DOM-selection
// copy used elsewhere can't see it). Ctrl+C is left alone so it still sends SIGINT.
@@ -310,20 +398,23 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
return true
})
- let sessionId: string | null = null
+ let sessionId: string | null = parked ? parked.sessionId : null
let disposed = false
// Last cols/rows actually sent to the PTY (seeded at create): a resize IPC makes tmux redraw
// the whole pane, so a same-size fit (e.g. the ResizeObserver's initial tick right after
// mount) must not send one — on a bulk project load that redraw doubles per node.
- let sentCols = 0
- let sentRows = 0
- const cleanups: Array<() => void> = []
+ let sentCols = parked ? parked.sentCols : 0
+ let sentRows = parked ? parked.sentRows : 0
+ // Session-scoped teardown. An adopted terminal carries its listeners over (they were wired
+ // to the still-live session on first mount); everything below that pushes here is gated on
+ // `!parked` so nothing is wired twice.
+ const cleanups: Array<() => void> = parked ? parked.cleanups : []
// Remote nodes: surface a dropped relay connection (host gone / socket closed) so the
// terminal isn't silently frozen. The actual relay teardown on node *deletion* happens
// in Canvas.deleteNodes (which can dedupe a connectionId shared by sibling nodes).
const remoteConn = (data.remote as { connectionId: string } | undefined)?.connectionId
- if (remoteConn) {
+ if (!parked && remoteConn) {
cleanups.push(
window.nodeTerminal.remoteClient.onClosed(remoteConn, () => {
term.write('\r\n\x1b[31m[remote disconnected]\x1b[0m\r\n')
@@ -334,7 +425,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
// Agent state (busy/idle/attention) comes from the agent's own hooks via the
// agent:status IPC (handled centrally in Canvas) — not from parsing the output here.
// We only surface the conversation topic from the terminal title, when the agent sets one.
- if (showStatus) {
+ if (!parked && showStatus) {
cleanups.push(
term.onTitleChange((t) => {
const title = t.trim()
@@ -355,8 +446,11 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
// Prefetch the persisted scrollback in parallel with the spawn so it's ready to replay the
// instant the session resolves (a cold restart after a reboot recreates the tmux session
// empty — see the `fresh` handling below). Cheap no-op ('') when there's no snapshot.
- const scrollbackPromise = window.nodeTerminal.pty.readScrollback(id).catch(() => '')
+ const scrollbackPromise = parked
+ ? Promise.resolve('')
+ : window.nodeTerminal.pty.readScrollback(id).catch(() => '')
void (async () => {
+ if (parked) return // adopted a live session — nothing to spawn or replay
// SSH-project terminal: the project's live ControlMaster controlPath is established by
// Canvas's active-project effect. On a cold app load child effects run before that parent
// connect, so wait for it (briefly) before spawning. In Phase 1 a node only exists in the
@@ -491,7 +585,6 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
observer.disconnect()
if (resizeTimer) clearTimeout(resizeTimer)
if (dwellRef.current) clearTimeout(dwellRef.current)
- cleanups.forEach((fn) => fn())
useAgentStatus.getState().setActive(id, false)
// Unmount happens on a project switch (a detach — the tmux session keeps running) as
// well as on real deletion, and we can't tell them apart here. Don't wipe the node's
@@ -500,11 +593,44 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
// deletion drops the entry in Canvas.deleteNodes.
useAgentStatus.getState().setState(id, undefined)
useAgentNodes.getState().clearForParent(id)
- if (sessionId) transport.kill(sessionId)
- term.dispose()
termRef.current = null
fitRef.current = null
searchAddonRef.current = null
+ // Free the GPU context while hidden either way (live WebGL contexts are capped ~16).
+ try {
+ webgl?.dispose()
+ } catch {
+ // already disposed via context loss
+ }
+ // A respawn (worktree move: the ref was bumped before this cleanup ran) needs a FRESH
+ // session in the new cwd — never park it. A plain unmount with a live session parks:
+ // the xterm (element detached) and its PTY stay alive so a remount re-adopts them.
+ const isRespawn = respawnNonceRef.current !== myNonce
+ if (sessionId && !isRespawn && !noParkIds.delete(id)) {
+ term.element?.remove()
+ const entry: ParkedTerminal = {
+ term,
+ fit,
+ search: searchAddon,
+ transport,
+ sessionId,
+ sentCols,
+ sentRows,
+ cleanups,
+ timer: setTimeout(() => {
+ if (parkedTerminals.get(id) === entry) {
+ parkedTerminals.delete(id)
+ disposeParked(entry)
+ }
+ }, TERM_PARK_MS)
+ }
+ disposeParkedTerminal(id) // defensive: never stack two entries for one node
+ parkedTerminals.set(id, entry)
+ return
+ }
+ cleanups.forEach((fn) => fn())
+ if (sessionId) transport.kill(sessionId)
+ term.dispose()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [data.respawnNonce])