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(relay): serve pty.captureHistory so a re-attaching client can hydrate scrollback
enes2a7e65b35921
diff --git a/src/main/remote/host-service.ts b/src/main/remote/host-service.tsindex 0bc20c6cf..7fcd4d421 100644--- a/src/main/remote/host-service.ts+++ b/src/main/remote/host-service.ts@@ -11,6 +11,8 @@// into an `OP.Error` frame (then the stream is dropped).// - `OP.Input` frame -> `write(sessionId, <utf-8 payload>)`// - `OP.Resize` frame -> `resize(sessionId, cols, rows)` (payload = 2x uint16 LE)+// - RPC `pty.captureHistory {streamId}` -> `captureHistory(persistKey)`, returning `{ output }`+// (tmux scrollback, so a re-entering client can hydrate its empty emulator scrollback)// - RPC `pty.kill {streamId}` -> `kill(sessionId)`// Output backpressure: when `sendFrame` returns false the host pauses the PTY via `setFlow`// and resumes it on the next successful send.@@ -58,6 +60,9 @@ export interface HostPtyManager {): string/** Current visible screen of a node's tmux session, for the attach snapshot. */captureSnapshot(persistKey: string): Promise<string>+ /** Scrollback (history + visible screen) of a node's tmux session, for hydrating a fresh+ * emulator on re-entry — the client's own scrollback is empty on a warm reattach. */+ captureHistory(persistKey: string): Promise<string>write(sessionId: string, data: string): voidresize(sessionId: string, cols: number, rows: number): voidsetFlow(sessionId: string, resume: boolean): void@@ -89,6 +94,9 @@ export interface HostFsOps {interface Stream {sessionId: string+ /** The node id (tmux persistKey) this stream attached to. The ONLY tmux target a client can+ * reach: every session-scoped RPC resolves it from the streamId, never from client params. */+ persistKey: string/** Outbound OP.Output sequence counter. */seq: number/** True while the PTY is paused due to relay backpressure. */@@ -216,7 +224,7 @@ export function createHostHandlers(const rows = Math.max(1, num(p.rows, 24))const streamId = ++streamCounter- const stream: Stream = { sessionId: '', seq: 0, paused: false }+ const stream: Stream = { sessionId: '', persistKey: nodeId, seq: 0, paused: false }const sinks = makeSinks(streamId, stream)// Reserve the stream and respond up front so the client can route Input/Resize frames; the@@ -289,6 +297,25 @@ export function createHostHandlers(}}+ /**+ * Scrollback of the session this stream is ALREADY attached to, so the client can hydrate its+ * (empty) emulator scrollback on re-entry. Deliberately keyed by `streamId`, never by a+ * client-supplied tmux session name / persistKey: a client may only read the history of a+ * session `pty.attach` already granted it. An unknown stream degrades to an empty result.+ */+ function handleCaptureHistory(req: RpcRequest): void {+ const streamId = num(asRecord(req.params).streamId, -1)+ const stream = streams.get(streamId)+ if (!stream) {+ socket.respond(req.id, true, { output: '' })+ return+ }+ void pty+ .captureHistory(stream.persistKey)+ .then((output) => socket.respond(req.id, true, { output }))+ .catch(() => socket.respond(req.id, true, { output: '' }))+ }+function handleKill(req: RpcRequest): void {const streamId = num(asRecord(req.params).streamId, -1)const stream = streams.get(streamId)@@ -311,6 +338,9 @@ export function createHostHandlers(case 'pty.attach':handleAttach(req)break+ case 'pty.captureHistory':+ handleCaptureHistory(req)+ breakcase 'pty.kill':handleKill(req)breakdiff --git a/src/main/remote/remote-security.test.ts b/src/main/remote/remote-security.test.tsindex 2630f6b72..a3e1d3b35 100644--- a/src/main/remote/remote-security.test.ts+++ b/src/main/remote/remote-security.test.ts@@ -33,6 +33,7 @@ function makeHostFakes() {createDetached: vi.fn(() => 'sess'),attachDetached: vi.fn(() => 'sess'),captureSnapshot: vi.fn(async () => ''),+ captureHistory: vi.fn(async () => 'HISTORY'),write: vi.fn(),resize: vi.fn(),setFlow: vi.fn(),@@ -92,6 +93,47 @@ describe('R1: fs.* is confined to the shared roots', () => {})})+// --- pty.captureHistory: scoped to an already-attached stream ----------------++describe('pty.captureHistory is scoped to the client\'s own streams', () => {+ it('captures the history of the session the streamId is attached to', async () => {+ const { socket, responses, fs, pty } = makeHostFakes()+ const handlers = createHostHandlers(pty, socket, fs, () => [])+ handlers.onRpc({ id: '1', method: 'pty.attach', params: { nodeId: 'node-a', cols: 80, rows: 24 } })+ await Promise.resolve()+ await Promise.resolve()+ const streamId = (responses[0].body as { streamId: number }).streamId++ handlers.onRpc({ id: '2', method: 'pty.captureHistory', params: { streamId } })+ await Promise.resolve()+ await Promise.resolve()+ expect(pty.captureHistory as ReturnType<typeof vi.fn>).toHaveBeenCalledWith('node-a')+ expect(responses[1]).toEqual({ id: '2', ok: true, body: { output: 'HISTORY' } })+ })++ it('degrades to an empty result for an unknown streamId (never reads another session)', async () => {+ const { socket, responses, fs, pty } = makeHostFakes()+ const handlers = createHostHandlers(pty, socket, fs, () => [])+ handlers.onRpc({ id: '1', method: 'pty.captureHistory', params: { streamId: 42 } })+ await Promise.resolve()+ expect(pty.captureHistory as ReturnType<typeof vi.fn>).not.toHaveBeenCalled()+ expect(responses).toEqual([{ id: '1', ok: true, body: { output: '' } }])+ })++ it('ignores a client-supplied session name / persistKey', async () => {+ const { socket, responses, fs, pty } = makeHostFakes()+ const handlers = createHostHandlers(pty, socket, fs, () => [])+ handlers.onRpc({+ id: '1',+ method: 'pty.captureHistory',+ params: { session: 'nt-victim', persistKey: 'victim', nodeId: 'victim' }+ })+ await Promise.resolve()+ expect(pty.captureHistory as ReturnType<typeof vi.fn>).not.toHaveBeenCalled()+ expect(responses).toEqual([{ id: '1', ok: true, body: { output: '' } }])+ })+})+// --- projects.list: read-only browse RPC ------------------------------------describe('projects.list serves the host projects blob', () => {