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
fix(server): release a WS pause on the drain sweep, not only on the next chunk
enesd873935b47b4
diff --git a/src/server/backpressure.test.ts b/src/server/backpressure.test.tsindex 1b012b734..3eddea291 100644--- a/src/server/backpressure.test.ts+++ b/src/server/backpressure.test.ts@@ -105,6 +105,80 @@ describe('ws backpressure', () => {])})+ // ── A pause with NO further output must not wedge the shared pty ─────────────────────────────+ // The resume check lives in the pty:data branch, so it only ever runs when the NEXT chunk for+ // that session is sent. A socket that crosses the high-water mark on the LAST chunk of a flood+ // — but never reaches WS_DROP_WATER, so nothing desyncs — had nothing left to re-evaluate it:+ // the pty stayed paused, the producing process blocked on a full pipe, and with co-attach that+ // freezes the terminal for EVERY viewer of that session. The renderer's own flow control cannot+ // save it (edge-latched: its backlog never crosses its own high-water mark on a slow link, so it+ // never pauses and never resumes). The drain sweep is the mechanism that re-evaluates it.+ it('releases a pause on the drain sweep when no further output ever arrives', async () => {+ vi.useFakeTimers()+ try {+ const p = new ServerPlatform({ userDataDir: '/tmp', appVersion: '0' })+ const flow: Array<{ uiId: number; sid: string; resume: boolean }> = []+ p.setFlowController((uiId, sid, resume) => flow.push({ uiId, sid, resume }))+ let buffered = 0+ const ui = p.attach(sink(() => buffered))++ buffered = 1_500_000+ p.sendTo(ui, 'pty:data:s1', 'the last chunk of the flood')+ expect(flow).toEqual([{ uiId: ui, sid: 's1', resume: false }])++ // Output has ENDED. The socket drains on its own; nothing will ever call sendTo again.+ buffered = 100_000+ await vi.advanceTimersByTimeAsync(300)+ expect(flow).toEqual([+ { uiId: ui, sid: 's1', resume: false },+ { uiId: ui, sid: 's1', resume: true }+ ])+ // Nothing paused, nothing desynced → the sweep must not keep spinning.+ expect(vi.getTimerCount()).toBe(0)+ } finally {+ vi.useRealTimers()+ }+ })++ it('the sweep leaves a pause in place while the socket is still backed up', async () => {+ vi.useFakeTimers()+ try {+ const p = new ServerPlatform({ userDataDir: '/tmp', appVersion: '0' })+ const flow: Array<{ sid: string; resume: boolean }> = []+ p.setFlowController((_uiId, sid, resume) => flow.push({ sid, resume }))+ const ui = p.attach(sink(() => 900_000)) // above LOW_WATER, below HIGH — still behind++ p.sendTo(ui, 'pty:data:s1', 'x') // 900k < HIGH → no pause yet+ expect(flow).toEqual([])+ // Force the pause the honest way: cross the high-water mark.+ const p2 = new ServerPlatform({ userDataDir: '/tmp', appVersion: '0' })+ const flow2: Array<{ sid: string; resume: boolean }> = []+ p2.setFlowController((_uiId, sid, resume) => flow2.push({ sid, resume }))+ const ui2 = p2.attach(sink(() => 1_500_000)) // stays jammed+ p2.sendTo(ui2, 'pty:data:s1', 'x')+ expect(flow2).toEqual([{ sid: 's1', resume: false }])++ await vi.advanceTimersByTimeAsync(1_000)+ expect(flow2).toEqual([{ sid: 's1', resume: false }]) // still jammed → no resume+ } finally {+ vi.useRealTimers()+ }+ })++ it('a disconnected client leaves no sweep behind', async () => {+ vi.useFakeTimers()+ try {+ const p = new ServerPlatform({ userDataDir: '/tmp', appVersion: '0' })+ p.setFlowController(() => {})+ const ui = p.attach(sink(() => 1_500_000))+ p.sendTo(ui, 'pty:data:s1', 'x') // paused → the sweep is armed+ p.detach(ui)+ expect(vi.getTimerCount()).toBe(0)+ } finally {+ vi.useRealTimers()+ }+ })+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))diff --git a/src/server/platform-server.ts b/src/server/platform-server.tsindex 8e0495cac..e4517f07a 100644--- a/src/server/platform-server.ts+++ b/src/server/platform-server.ts@@ -176,6 +176,12 @@ export class ServerPlatform implements CorePlatform {// proc.pause() is idempotent, and this branch only runs when data actually arrived (the// pty was running), so it self-limits — no spamming.this.paused.add(key)+ // A flood that ENDS is the normal case, and the resume check below only runs when the+ // NEXT chunk for this session is sent. If this is the last chunk, nothing else would ever+ // re-evaluate the pause: the pty would stay paused, the producing process blocked on a+ // full pipe — and with co-attach that freezes the terminal for EVERY viewer. So the same+ // drain sweep the desync path uses watches the socket for us (see sweepDesynced).+ this.armSweep()this.flowController(uiId, sessionId, false)} else if (isPaused && buffered <= WS_LOW_WATER) {this.paused.delete(key)@@ -295,22 +301,60 @@ export class ServerPlatform implements CorePlatform {}}- /** The drain trigger. A flood that ends leaves no further pty output to hang a redraw off, so a- * low-frequency sweep watches the socket instead. It exists ONLY while something is desynced. */+ /** The drain trigger, for BOTH backpressure states. A flood that ends leaves no further pty output+ * to hang a redraw — or a resume — off, so a low-frequency sweep watches the socket instead. It+ * exists ONLY while something is paused or desynced, so the healthy path (every solo user, and+ * every client that keeps up) never spins a timer. */private armSweep(): void {- if (this.sweepTimer || this.desynced.size === 0) return- this.sweepTimer = setInterval(() => this.sweepDesynced(), RESYNC_SWEEP_MS)+ if (this.sweepTimer || (this.desynced.size === 0 && this.paused.size === 0)) return+ this.sweepTimer = setInterval(() => this.sweep(), RESYNC_SWEEP_MS)// Must never hold the process (or a vitest run) open.this.sweepTimer.unref?.()}private stopSweepIfIdle(): void {- if (this.sweepTimer && this.desynced.size === 0) {+ if (this.sweepTimer && this.desynced.size === 0 && this.paused.size === 0) {clearInterval(this.sweepTimer)this.sweepTimer = undefined}}+ private sweep(): void {+ this.sweepPaused()+ this.sweepDesynced()+ this.stopSweepIfIdle()+ }++ /**+ * Hand back a pause whose socket has drained, when no further output will do it for us.+ *+ * This is the ONLY thing that unsticks the pause a last-chunk-of-a-flood send left behind: the+ * pty is paused, so no data arrives, so the `pty:data` branch never runs again — and the browser's+ * own flow control cannot rescue it either (it is edge-latched, and on a slow link its xterm+ * backlog never crossed its own high-water mark, so it has no pause to return). With ONE pty and+ * N subscribers, that stuck pause is not one dead UI: it is a dead terminal for everybody.+ *+ * Released at the LOW-water mark, exactly like the in-band check, so a socket that is merely+ * "less full" does not flap the pty.+ */+ private sweepPaused(): void {+ if (!this.flowController) return+ for (const key of [...this.paused]) {+ const sep = key.indexOf(' ')+ const uiId = Number(key.slice(0, sep))+ const sink = this.sinks.get(uiId)+ // Client gone (belt and braces: detach prunes, and PtyManager.dropClient returns the pause+ // it owed on the pty side).+ if (!sink) {+ this.paused.delete(key)+ continue+ }+ if ((sink.bufferedAmount?.() ?? 0) > WS_LOW_WATER) continue+ this.paused.delete(key)+ this.flowController(uiId, key.slice(sep + 1), true)+ }+ }+private sweepDesynced(): void {for (const key of [...this.desynced.keys()]) {const sep = key.indexOf(' ')@@ -324,7 +368,6 @@ export class ServerPlatform implements CorePlatform {// client that never drains costs one bufferedAmount read per tick — it cannot spin.this.maybeResync(uiId, key.slice(sep + 1), sink.bufferedAmount?.() ?? 0)}- this.stopSweepIfIdle()}broadcast(channel: string, ...args: any[]): void {