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
feat(pty): captureHistory — tmux scrollback above the visible screen, capped
enes26ce89c234f5
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.tsindex 6773b879d..2ec3ed823 100644--- a/src/core/pty-manager.ts+++ b/src/core/pty-manager.ts@@ -18,7 +18,8 @@ import {remoteTmuxHasSessionArgs,remoteTmuxKillArgs,remoteTmuxPtyArgs,- remoteCapturePaneArgs+ remoteCapturePaneArgs,+ remoteCaptureHistoryArgs} from './remote-ssh/control-master'import { TMUX_SOCKET, sessionName } from './tmux-naming'import { releasePty, type ReleasablePty } from './pty-release'@@ -32,6 +33,11 @@ import { AUTH_ENV_STRIP, accountTmuxEnvArgs, remoteAccountConfigDirAbs } from '.// runs on detach; the interval covers an ungraceful power loss between detaches.const SCROLLBACK_SNAPSHOT_MS = 15_000+// Reattach hydration caps: tmux history is the source, but an attach must not stall on a huge+// transfer (an ssh/mobile link pays for every byte, and `-e` colour codes inflate it).+const HISTORY_LINES = 5000+const HISTORY_MAX_BYTES = 1024 * 1024+// Async exec for tmux side-calls (capture / send-keys / kill-session) so they never block// the main event loop — a synchronous capture-pane of a large scrollback would stall every// other session's PTY streaming and all IPC for its duration.@@ -61,6 +67,18 @@ set -g set-clipboard on`}+/** Trim `text` to at most `maxBytes`, dropping whole lines from the HEAD (oldest first) and+ * never splitting a multi-byte character. */+export function trimToBytes(text: string, maxBytes: number): string {+ const buf = Buffer.from(text, 'utf-8')+ if (buf.byteLength <= maxBytes) return text+ const tail = buf.subarray(buf.byteLength - maxBytes).toString('utf-8')+ // toString on a mid-character boundary yields a leading replacement char; drop the partial+ // first line either way — it is the oldest content and the least missed.+ const nl = tail.indexOf('\n')+ return nl === -1 ? tail : tail.slice(nl + 1)+}+/** Resolve an absolute tmux path (GUI apps don't inherit the shell PATH). */function findTmux(): string | null {for (const c of ['/opt/homebrew/bin/tmux', '/usr/local/bin/tmux', '/usr/bin/tmux', '/bin/tmux']) {@@ -852,6 +870,41 @@ export class PtyManager {}}+ /**+ * Capture a session's scrollback ABOVE the visible screen, for hydrating a fresh emulator on a+ * warm reattach (the app restarted; tmux kept running and only redraws the visible screen).+ * `-E -1` excludes that visible screen, so the hydration cannot duplicate the redraw.+ * Best-effort: returns '' when tmux/ssh is unavailable.+ */+ async captureHistory(persistKey: string, lines = HISTORY_LINES): Promise<string> {+ const sshRemote = this.sessionByPersistKey(persistKey)?.sshRemote+ if (sshRemote) {+ const ssh = findSsh()+ if (!ssh) return ''+ try {+ const { stdout } = await runAsync(+ ssh,+ remoteCaptureHistoryArgs(sshRemote.conn, sshRemote.controlPath, sessionName(persistKey), lines),+ { encoding: 'utf-8', maxBuffer: 50 * 1024 * 1024 }+ )+ return trimToBytes(stdout, HISTORY_MAX_BYTES)+ } catch {+ return ''+ }+ }+ if (!this.tmuxPath) return ''+ try {+ const { stdout } = await runAsync(+ this.tmuxPath,+ ['-L', TMUX_SOCKET, 'capture-pane', '-p', '-e', '-t', sessionName(persistKey), '-S', `-${lines}`, '-E', '-1'],+ { encoding: 'utf-8', maxBuffer: 50 * 1024 * 1024 }+ )+ return trimToBytes(stdout, HISTORY_MAX_BYTES)+ } catch {+ return ''+ }+ }+/*** Snapshot a node's recent scrollback (with colors, `-e`) to disk for cold-restart replay.* Best-effort: a missing session / unavailable tmux just leaves the prior snapshot in place.diff --git a/src/core/remote-ssh/control-master.test.ts b/src/core/remote-ssh/control-master.test.tsindex 66c6fa0c0..54167dee5 100644--- a/src/core/remote-ssh/control-master.test.ts+++ b/src/core/remote-ssh/control-master.test.ts@@ -6,6 +6,7 @@ import {childArgs,remoteTmuxHasSessionArgs,remoteCapturePaneArgs,+ remoteCaptureHistoryArgs,remoteTmuxPtyArgs,listDirArgs,mkDirArgs,@@ -183,3 +184,11 @@ describe('listDirArgs', () => {])})})++describe('remoteCaptureHistoryArgs', () => {+ it('captures history ABOVE the visible screen (-E -1) so the attach redraw is not duplicated', () => {+ const args = remoteCaptureHistoryArgs(conn, '/tmp/cm', 'nt-x', 5000)+ const cmd = args[args.length - 1]+ expect(cmd).toContain('capture-pane -p -e -t nt-x -S -5000 -E -1')+ })+})diff --git a/src/core/remote-ssh/control-master.ts b/src/core/remote-ssh/control-master.tsindex a1a64bef4..8bd379dac 100644--- a/src/core/remote-ssh/control-master.ts+++ b/src/core/remote-ssh/control-master.ts@@ -76,6 +76,20 @@ export function remoteCapturePaneArgs(conn: SshConnection, controlPath: string,`tmux -L ${RMT_TMUX_SOCKET} capture-pane -p -e -t ${sessionId} -S ${full ? '-' : '-200'}`)}+/** Capture ONLY the history above the visible screen (`-E -1`). tmux redraws the visible screen+ * itself on attach, so including it here would print it twice. */+export function remoteCaptureHistoryArgs(+ conn: SshConnection,+ controlPath: string,+ sessionId: string,+ lines: number+): string[] {+ return childArgs(+ conn,+ controlPath,+ `tmux -L ${RMT_TMUX_SOCKET} capture-pane -p -e -t ${sessionId} -S -${lines} -E -1`+ )+}export function tmuxProbeArgs(conn: SshConnection, controlPath: string): string[] {return childArgs(conn, controlPath, 'command -v tmux')}diff --git a/src/core/tmux-conf.test.ts b/src/core/tmux-conf.test.tsindex 006b2945b..477d40b68 100644--- a/src/core/tmux-conf.test.ts+++ b/src/core/tmux-conf.test.ts@@ -1,5 +1,5 @@import { describe, it, expect } from 'vitest'-import { tmuxConf } from './pty-manager'+import { tmuxConf, trimToBytes } from './pty-manager'describe('tmuxConf', () => {const c = tmuxConf(50000)@@ -19,3 +19,19 @@ describe('tmuxConf', () => {expect(tmuxConf(10)).toContain('set -g history-limit 1000')})})++describe('trimToBytes', () => {+ it('returns the text untouched when it is under the cap', () => {+ expect(trimToBytes('hello', 1024)).toBe('hello')+ })+ it('trims from the HEAD (the oldest lines go first)', () => {+ const out = trimToBytes('aaaa\nbbbb\ncccc\n', 10)+ expect(out.endsWith('cccc\n')).toBe(true)+ expect(out).not.toContain('aaaa')+ expect(Buffer.byteLength(out, 'utf-8')).toBeLessThanOrEqual(10)+ })+ it('never splits a multi-byte character', () => {+ const out = trimToBytes('ü'.repeat(50), 10)+ expect(out).toBe(Buffer.from(out, 'utf-8').toString('utf-8'))+ })+})