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.

feat(relay): serve pty.scroll so a phone can scroll tmux's history

enesdff07fb80b13
diff --git a/src/main/remote/host-service.ts b/src/main/remote/host-service.ts
index 1880a5fd9..e585917c9 100644
--- a/src/main/remote/host-service.ts
+++ b/src/main/remote/host-service.ts
@@ -311,6 +311,30 @@ export function createHostHandlers(
}
}
+ /**
+ * Scroll a remote client's view of the session's tmux history.
+ *
+ * Scrolling belongs to tmux (its mouse is on and the pane lives on the alternate screen, so the
+ * client's emulator has no scrollback of its own). This stream's pty IS a tmux client, so the
+ * wheel is simply written into it as an SGR mouse event and tmux does the rest — no tmux command
+ * channel, no copy-mode bookkeeping on our side. The phone drives this because it cannot deliver
+ * the wheel itself: its own emulator would swallow the gesture.
+ *
+ * `lines` is untrusted (it arrives from a remote client) — clamp it, and address the wheel to
+ * cell 1,1 so a hostile value cannot be interpolated anywhere interesting.
+ */
+ function handleScroll(req: RpcRequest): void {
+ const p = asRecord(req.params)
+ const stream = streams.get(num(p.streamId, -1))
+ if (stream) {
+ const up = str(p.dir) !== 'down'
+ const notches = Math.min(20, Math.max(1, Math.floor(num(p.lines, 1))))
+ const seq = `\x1b[<${up ? 64 : 65};1;1M`
+ for (let i = 0; i < notches; i++) pty.write(getClientId(), stream.sessionId, seq)
+ }
+ socket.respond(req.id, true, {})
+ }
+
function handleKill(req: RpcRequest): void {
const streamId = num(asRecord(req.params).streamId, -1)
const stream = streams.get(streamId)
@@ -336,6 +360,9 @@ export function createHostHandlers(
case 'pty.kill':
handleKill(req)
break
+ case 'pty.scroll':
+ handleScroll(req)
+ break
case 'fs.list':
case 'fs.read':
case 'fs.readBinary':
diff --git a/src/main/remote/remote-security.test.ts b/src/main/remote/remote-security.test.ts
index 2630f6b72..f669052a2 100644
--- a/src/main/remote/remote-security.test.ts
+++ b/src/main/remote/remote-security.test.ts
@@ -213,3 +213,40 @@ describe('R3: replayed encrypted frames are dropped', () => {
expect(clientFrames).toHaveLength(1) // dropped — not re-delivered
})
})
+
+// Scrolling belongs to tmux (mouse on, alternate screen), and the phone cannot deliver the wheel
+// itself — its emulator swallows the gesture — so it asks the host to write it into the stream's
+// pty, which IS a tmux client. `lines` and the stream target both come off the wire.
+describe('pty.scroll drives tmux through the session pty', () => {
+ const attach = (handlers: ReturnType<typeof createHostHandlers>): void => {
+ handlers.onRpc({ id: 'a', method: 'pty.attach', params: { nodeId: 'node-a', cols: 80, rows: 24 } })
+ }
+ const writes = (pty: HostPtyManager): string[] =>
+ (pty.write as ReturnType<typeof vi.fn>).mock.calls.map((c) => c[2] as string)
+
+ it('writes one SGR wheel event per notch, up or down', () => {
+ const { socket, fs, pty } = makeHostFakes()
+ const handlers = createHostHandlers(pty, socket, fs, () => ['/work'])
+ attach(handlers)
+ handlers.onRpc({ id: '1', method: 'pty.scroll', params: { streamId: 1, dir: 'up', lines: 3 } })
+ expect(writes(pty)).toEqual(['\x1b[<64;1;1M', '\x1b[<64;1;1M', '\x1b[<64;1;1M'])
+ handlers.onRpc({ id: '2', method: 'pty.scroll', params: { streamId: 1, dir: 'down', lines: 1 } })
+ expect(writes(pty).at(-1)).toBe('\x1b[<65;1;1M')
+ })
+
+ it('clamps a hostile `lines` (it arrives from a remote client) and ignores an unknown stream', () => {
+ const { socket, responses, fs, pty } = makeHostFakes()
+ const handlers = createHostHandlers(pty, socket, fs, () => ['/work'])
+ attach(handlers)
+ handlers.onRpc({ id: '1', method: 'pty.scroll', params: { streamId: 1, dir: 'up', lines: 1e9 } })
+ expect(writes(pty)).toHaveLength(20) // capped, not a million writes
+ ;(pty.write as ReturnType<typeof vi.fn>).mockClear()
+ handlers.onRpc({ id: '2', method: 'pty.scroll', params: { streamId: 1, lines: -5 } })
+ expect(writes(pty)).toHaveLength(1) // floored at one notch
+ ;(pty.write as ReturnType<typeof vi.fn>).mockClear()
+ // An unknown streamId is a no-op, and still answers — never a hang.
+ handlers.onRpc({ id: '3', method: 'pty.scroll', params: { streamId: 99, dir: 'up', lines: 2 } })
+ expect(pty.write).not.toHaveBeenCalled()
+ expect(responses.at(-1)).toMatchObject({ id: '3', ok: true })
+ })
+})