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(remote,pty): pin kill→dropStream adjacency; correct the %output-drop comments

enes2efbbd62506a
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.ts
index 1dd8787d6..cc4f35dfe 100644
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@ -786,8 +786,13 @@ export class PtyManager {
socket: TMUX_SOCKET,
sessionName: sessionName(persistKey),
// Nothing consumes a shadowed session's output yet (the first consumer, Task 4's background
- // write path, only WRITES). Dropping it keeps `%output`'s ~4x octal inflation off the main
- // thread until something actually asks for the bytes.
+ // write path, only WRITES). This drops the bytes; it does NOT avoid their cost — an attached
+ // `-C` client is sent every `%output` line regardless, and the client octal-decodes then
+ // UTF-8-decodes each one on the main thread before this callback throws it away. All the drop
+ // buys is not forwarding it any further. STANDING OBLIGATION: before any first production
+ // caller, either run the wire-cost probe (measure that traffic on a busy session) or issue
+ // `refresh-client -fno-output` for write-only clients so tmux stops sending it at all
+ // (tmux >= 3.2 — the floor this feature would then require).
onOutput: () => {},
// An UNEXPECTED death only — `dispose()` is silent by design, so this can never fire for a
// swap-out we asked for. Forget the entry and stop there: the tmux session, its processes and
@@ -1026,8 +1031,14 @@ export class PtyManager {
tmuxBin: this.tmuxPath,
socket: TMUX_SOCKET,
sessionName: sessionName(persistKey),
- // This client writes; it never reads. Dropping `%output` keeps its ~4x octal inflation off
- // the main thread for every session on the server it happens to be attached to.
+ // This client writes; it never reads — but dropping `%output` here saves none of its cost.
+ // While attached, tmux sends this client the pane output of the session it landed on, and it
+ // octal-decodes then UTF-8-decodes all of it on the main thread before this callback discards
+ // it; the drop
+ // only stops it going further. The linger bounds how long that is paid for. STANDING
+ // OBLIGATION (same as the per-session shadow above): before any first production caller,
+ // either run the wire-cost probe or issue `refresh-client -fno-output` for these write-only
+ // clients (tmux >= 3.2) so the bytes are never sent.
onOutput: () => {},
onExit: () => {
if (this.shared?.client === client) this.shared = null
diff --git a/src/main/remote/remote-security.test.ts b/src/main/remote/remote-security.test.ts
index f669052a2..8cb13c3ca 100644
--- a/src/main/remote/remote-security.test.ts
+++ b/src/main/remote/remote-security.test.ts
@@ -2,6 +2,8 @@
// R1 — host serves no `pty.create`; `fs.*` is confined to the shared roots.
// R2 — the channel SAS is deterministic + identical on both peers.
// R3 — replayed/reordered encrypted boxes are dropped (per-direction monotonic counter).
+// R4 — killing a stream forgets it in the SAME synchronous turn, so a late Input frame for that
+// streamId can never be written into a session that is already released.
import { describe, expect, it, vi } from 'vitest'
import { createHostHandlers, type HostFsOps, type HostPtyManager, type HostRelaySocket } from './host-service'
import { genKeyPair, deriveSharedKey, sasFromSharedKey, publicKeyToB64 } from './e2ee'
@@ -250,3 +252,72 @@ describe('pty.scroll drives tmux through the session pty', () => {
expect(responses.at(-1)).toMatchObject({ id: '3', ok: true })
})
})
+
+// --- R4: kill → dropStream is ONE synchronous step ---------------------------
+
+// The invariant: a client's Input frame must never be written into a session the host has already
+// released. Nothing enforces that but the ADJACENCY of two statements — `pty.kill(null, sessionId)`
+// and the `dropStream` that forgets the streamId (handleKill; closeAll's `streams.clear()`). Put any
+// yield between them and every frame the relay delivers in that window is written into a session on
+// its way out (and, once a session is released, a background write can re-reach its tmux pane).
+//
+// So these tests pin the adjacency rather than the outcome: the late frame is queued as a microtask
+// BEFORE the kill turn runs, which is exactly the window an `await` between the two statements would
+// open. They pass only while the drop is synchronous with the kill.
+function inputFrame(streamId: number, data: string): Frame {
+ return { op: OP.Input, streamId, seq: 0, payload: new TextEncoder().encode(data) }
+}
+
+/** Attach a stream (id = n-th attach) and settle the async capture → attachDetached handoff. */
+async function attachStream(
+ handlers: ReturnType<typeof createHostHandlers>,
+ nodeId: string
+): Promise<void> {
+ handlers.onRpc({ id: `a-${nodeId}`, method: 'pty.attach', params: { nodeId, cols: 80, rows: 24 } })
+ await new Promise((resolve) => setTimeout(resolve, 0))
+}
+
+describe('R4: a killed stream stops accepting input in the same turn', () => {
+ it('drops an Input frame that lands one microtask after pty.kill', async () => {
+ const { socket, fs, pty } = makeHostFakes()
+ const writeMock = pty.write as ReturnType<typeof vi.fn>
+ const handlers = createHostHandlers(pty, socket, fs, () => ['/work'])
+ await attachStream(handlers, 'node-a')
+
+ // Control: while the stream lives, this exact frame IS written — so a later "not written"
+ // cannot be an artifact of a wrong streamId or an attach that never completed.
+ handlers.onFrame(inputFrame(1, 'echo live\n'))
+ expect(writeMock).toHaveBeenCalledWith(null, 'sess', 'echo live\n')
+ writeMock.mockClear()
+
+ // Queue the frame FIRST: its microtask runs immediately after the kill's synchronous turn,
+ // i.e. inside the gap any `await` between kill() and dropStream() would create.
+ const late = Promise.resolve().then(() => handlers.onFrame(inputFrame(1, 'echo late\n')))
+ handlers.onRpc({ id: 'k', method: 'pty.kill', params: { streamId: 1 } })
+ expect(pty.kill).toHaveBeenCalledWith(null, 'sess')
+ await late
+
+ expect(writeMock).not.toHaveBeenCalled()
+ })
+
+ it('drops Input frames for every stream that closeAll killed', async () => {
+ const { socket, fs, pty } = makeHostFakes()
+ const writeMock = pty.write as ReturnType<typeof vi.fn>
+ const handlers = createHostHandlers(pty, socket, fs, () => ['/work'])
+ await attachStream(handlers, 'node-a')
+ await attachStream(handlers, 'node-b')
+ handlers.onFrame(inputFrame(2, 'echo live\n')) // control: stream 2 is real and routable
+ expect(writeMock).toHaveBeenCalledTimes(1)
+ writeMock.mockClear()
+
+ const late = Promise.resolve().then(() => {
+ handlers.onFrame(inputFrame(1, 'echo late\n'))
+ handlers.onFrame(inputFrame(2, 'echo late\n'))
+ })
+ handlers.closeAll()
+ expect(pty.kill).toHaveBeenCalledTimes(2)
+ await late
+
+ expect(writeMock).not.toHaveBeenCalled()
+ })
+})