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.

fix(server): backpressure re-asserts pause so renderer resume can't latch it off

enesf3e1d9b5820b
diff --git a/docs/SERVER.md b/docs/SERVER.md
index caf4f7945..fa29ff1b5 100644
--- a/docs/SERVER.md
+++ b/docs/SERVER.md
@@ -171,6 +171,14 @@ The **backpressure / flow-control** gap noted in the Phase 2 limitations is now
closed: a flooding PTY is automatically paused based on the WebSocket
`bufferedAmount` and resumed when it drains, so the WS is protected.
+> **Loose coordination (follow-up).** The server-side WS backpressure and the
+> renderer's terminal (xterm) flow control coordinate only **loosely** over a shared
+> pause actuator (`ptyManager.setFlow`) — full two-master coordination is a follow-up.
+> Because either side can resume the pty independently, the server **re-asserts** its
+> pause on every send while the socket buffer stays above the high-water mark (rather
+> than latching on a single rising edge), so a renderer-side resume cannot silently
+> latch the server's protection off.
+
**Still deferred to Phase 3b:** the SDK **chat node** and the **agent-status
badges/hooks** are not yet wired into the server bridge.
diff --git a/src/server/backpressure.test.ts b/src/server/backpressure.test.ts
index 91d753964..519f83399 100644
--- a/src/server/backpressure.test.ts
+++ b/src/server/backpressure.test.ts
@@ -22,12 +22,39 @@ describe('ws backpressure', () => {
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 }])
+ p.sendTo(ui, 'pty:data:s1', 'chunk') // still above high-water → re-asserts pause (idempotent)
+ expect(flow).toEqual([{ sid: 's1', resume: false }, { 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 }])
+ p.sendTo(ui, 'pty:data:s1', 'chunk') // below low-water → resume once (resume side stays edge-guarded)
+ expect(flow).toEqual([
+ { sid: 's1', resume: false },
+ { sid: 's1', resume: false },
+ { sid: 's1', resume: true }
+ ])
+ })
+
+ it('re-asserts pause on every high send so a renderer-side resume cannot latch it off', () => {
+ // The renderer's own xterm flow control drives the SAME ptyManager.setFlow actuator.
+ // If it resumes the pty underneath us while the socket buffer is still full, the
+ // server must be able to re-pause — an edge-latched guard would silently latch OFF.
+ const p = new ServerPlatform({ userDataDir: '/tmp', appVersion: '0' })
+ const flow: Array<{ sid: string; resume: boolean }> = []
+ p.setFlowController((sid, resume) => flow.push({ sid, resume }))
+ const ui = p.attach(sink(() => 1_500_000)) // stays above high-water
+
+ p.sendTo(ui, 'pty:data:s1', 'chunk') // pause (rising edge)
+ expect(flow).toEqual([{ sid: 's1', resume: false }])
+
+ // Simulate the renderer resuming the pty underneath us (bypassing the server's Map):
+ // data floods again → a subsequent high send must RE-FIRE the pause, not skip it.
+ p.sendTo(ui, 'pty:data:s1', 'chunk')
+ p.sendTo(ui, 'pty:data:s1', 'chunk')
+ expect(flow).toEqual([
+ { sid: 's1', resume: false },
+ { sid: 's1', resume: false },
+ { sid: 's1', resume: false }
+ ])
})
it('does no flow control when no controller is set (and non-pty channels are unaffected)', () => {
diff --git a/src/server/handlers/fs.test.ts b/src/server/handlers/fs.test.ts
index 36f2bf33f..92d9cd11d 100644
--- a/src/server/handlers/fs.test.ts
+++ b/src/server/handlers/fs.test.ts
@@ -30,7 +30,7 @@ describe('server fs handlers', () => {
it('list returns directory entries', async () => {
fs.writeFileSync(path.join(dir, 'a.txt'), 'x')
fs.mkdirSync(path.join(dir, 'sub'))
- const entries = (await call(IPC.fsList, dir)) as Array<{ name: string; isDirectory: boolean }>
+ const entries = (await call(IPC.fsList, dir)) as Array<{ name: string; dir: boolean }>
expect(entries.map((e) => e.name).sort()).toEqual(['a.txt', 'sub'])
})
it('readBinary returns base64', async () => {
diff --git a/src/server/platform-server.ts b/src/server/platform-server.ts
index f1acc4a27..0c253f531 100644
--- a/src/server/platform-server.ts
+++ b/src/server/platform-server.ts
@@ -69,7 +69,13 @@ export class ServerPlatform implements CorePlatform {
if (this.flowController) {
const buffered = sink.bufferedAmount?.() ?? 0
const isPaused = this.paused.get(sessionId) ?? false
- if (!isPaused && buffered > WS_HIGH_WATER) {
+ if (buffered > WS_HIGH_WATER) {
+ // Re-assert the pause on EVERY high send (not just the rising edge). The
+ // renderer's own xterm flow control drives the same setFlow actuator and may
+ // have resumed the pty underneath us; an edge-latched `!isPaused` guard would
+ // then never re-pause until the buffer drained to LOW. 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.set(sessionId, true)
this.flowController(sessionId, false)
} else if (isPaused && buffered <= WS_LOW_WATER) {