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(server): WS backpressure — pause the tmux client when the socket buffer fills
enes33b9a0697cb9
diff --git a/src/server/backpressure.test.ts b/src/server/backpressure.test.tsnew file mode 100644index 000000000..91d753964--- /dev/null+++ b/src/server/backpressure.test.ts@@ -0,0 +1,39 @@+import { describe, it, expect } from 'vitest'+import { ServerPlatform, type UiSink } from './platform-server'++function sink(buffered: () => number): UiSink {+ return { sendText: () => {}, sendBinary: () => {}, bufferedAmount: buffered }+}++describe('ws backpressure', () => {+ it('pauses the session when buffered crosses the high-water mark and resumes below low', () => {+ const p = new ServerPlatform({ userDataDir: '/tmp', appVersion: '0' })+ const flow: Array<{ sid: string; resume: boolean }> = []+ p.setFlowController((sid, resume) => flow.push({ sid, resume }))+ let buffered = 0+ const ui = p.attach(sink(() => buffered))++ buffered = 500_000+ p.sendTo(ui, 'pty:data:s1', 'chunk') // under high-water → no pause+ expect(flow).toEqual([])++ buffered = 1_500_000+ p.sendTo(ui, 'pty:data:s1', 'chunk') // over high-water → pause once+ expect(flow).toEqual([{ sid: 's1', resume: false }])++ buffered = 1_400_000+ p.sendTo(ui, 'pty:data:s1', 'chunk') // still high, already paused → no repeat+ expect(flow).toEqual([{ sid: 's1', resume: false }])++ buffered = 100_000+ p.sendTo(ui, 'pty:data:s1', 'chunk') // below low-water → resume once+ expect(flow).toEqual([{ sid: 's1', resume: false }, { sid: 's1', resume: true }])+ })++ it('does no flow control when no controller is set (and non-pty channels are unaffected)', () => {+ const p = new ServerPlatform({ userDataDir: '/tmp', appVersion: '0' })+ const ui = p.attach(sink(() => 5_000_000))+ expect(() => p.sendTo(ui, 'pty:data:s1', 'x')).not.toThrow()+ expect(() => p.sendTo(ui, 'some:event', 1)).not.toThrow()+ })+})diff --git a/src/server/index.ts b/src/server/index.tsindex c163b376f..0d88b06ca 100644--- a/src/server/index.ts+++ b/src/server/index.ts@@ -70,6 +70,11 @@ export async function startServer(ptyManager.registerIpc()workspaceStore.registerIpc()+ // WS backpressure: when a connection's socket send buffer fills while streaming pty+ // output, pause that tmux client so the OS pipe applies real backpressure (resumes below+ // the low-water mark). See platform-server.ts sendTo.+ platform.setFlowController((sid, resume) => ptyManager.setFlow(sid, resume))+// Desktop's src/main/index.ts registers a few pty handlers outside PtyManager. Of those,// ptyCapture delegates purely to core (ptyManager.captureSession), so it belongs here.// The others (ptyGenerateName / ptyGenerateGroupName → commit-message.ts; ptyReadSessionNamediff --git a/src/server/platform-server.ts b/src/server/platform-server.tsindex 2afcb2226..f1acc4a27 100644--- a/src/server/platform-server.ts+++ b/src/server/platform-server.ts@@ -10,9 +10,15 @@ import {export interface UiSink {sendText(json: string): voidsendBinary(buf: Uint8Array): void+ /** Bytes queued in the socket send buffer (WebSocket.bufferedAmount). Optional so+ * tests/sinks that don't report it opt out of flow control (treated as 0). */+ bufferedAmount?(): number}const PTY_DATA_PREFIX = 'pty:data:'+/** Watermarks match the renderer terminal's own flow control. */+const WS_HIGH_WATER = 1_000_000+const WS_LOW_WATER = 256_000type Handler = { fn: (...args: any[]) => unknown; withSender: boolean }@@ -28,6 +34,15 @@ export class ServerPlatform implements CorePlatform {private sinks = new Map<number, UiSink>()private nextUiId = 1+ // WS backpressure: per-session paused flag + the controller that pauses/resumes the+ // tmux client. Keyed by sessionId alone (a session's pty:data always routes to one ui).+ private paused = new Map<string, boolean>()+ private flowController?: (sessionId: string, resume: boolean) => void++ setFlowController(fn: (sessionId: string, resume: boolean) => void): void {+ this.flowController = fn+ }+constructor(opts: { userDataDir: string; appVersion: string }) {this.userDataDir = opts.userDataDirthis.appVersion = opts.appVersion@@ -49,7 +64,19 @@ export class ServerPlatform implements CorePlatform {const sink = this.sinks.get(uiId)if (!sink) returnif (channel.startsWith(PTY_DATA_PREFIX)) {- sink.sendBinary(encodePtyData(channel.slice(PTY_DATA_PREFIX.length), String(args[0] ?? '')))+ const sessionId = channel.slice(PTY_DATA_PREFIX.length)+ sink.sendBinary(encodePtyData(sessionId, String(args[0] ?? '')))+ if (this.flowController) {+ const buffered = sink.bufferedAmount?.() ?? 0+ const isPaused = this.paused.get(sessionId) ?? false+ if (!isPaused && buffered > WS_HIGH_WATER) {+ this.paused.set(sessionId, true)+ this.flowController(sessionId, false)+ } else if (isPaused && buffered <= WS_LOW_WATER) {+ this.paused.set(sessionId, false)+ this.flowController(sessionId, true)+ }+ }} else {sink.sendText(JSON.stringify({ t: 'ev', channel, args }))}diff --git a/src/server/ws.ts b/src/server/ws.tsindex 22f5d76cf..1156fd06a 100644--- a/src/server/ws.ts+++ b/src/server/ws.ts@@ -74,7 +74,8 @@ export function attachWsServer(server: http.Server, opts: WsServerOpts): void {wss.on('connection', (ws: WebSocket) => {const uiId = platform.attach({sendText: (json) => ws.send(json),- sendBinary: (buf) => ws.send(buf, { binary: true })+ sendBinary: (buf) => ws.send(buf, { binary: true }),+ bufferedAmount: () => ws.bufferedAmount})ws.on('message', (data: unknown, isBinary: boolean) => {