Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(terminal): stop a new terminal from closing the client's other ones

0xdiego2544a775477c3d
diff --git a/cli/src/lib/terminalStreamManager.spec.ts b/cli/src/lib/terminalStreamManager.spec.ts
index 0d7d33c52..b942bc1c0 100644
--- a/cli/src/lib/terminalStreamManager.spec.ts
+++ b/cli/src/lib/terminalStreamManager.spec.ts
@@ -232,6 +232,50 @@ describe('TerminalStreamManager', () => {
expect(sent.findLast((frame) => frame.type === 'terminal_ready')?.connId).toBe('web-2')
})
+ it('keeps a client\'s other terminals alive when it opens another one', async () => {
+ // The desktop app's pane grid: one connection, several agents, each its own tmux window.
+ agents.set('agent-2', {
+ ...session('claude', 'agent-2'),
+ runtimes: [{ backend: 'tmux', paneId: '%2' }],
+ primaryRuntimeKey: 'tmux:default:%2',
+ } as unknown as RegisteredSession)
+
+ await manager.handleFrame('app-1', 'terminal_open', {
+ requestId: 'open-1', protocolVersion: 3, agentId: 'agent-1', cols: 100, rows: 30,
+ })
+ const first = sent.find((frame) => frame.type === 'terminal_ready')?.payload.streamId as string
+
+ await manager.handleFrame('app-1', 'terminal_open', {
+ requestId: 'open-2', protocolVersion: 3, agentId: 'agent-2', cols: 100, rows: 30,
+ })
+
+ // Observed through a resize rather than a close frame: replacing a stream is deliberately
+ // silent, so the only way to tell a live stream from a dead one is whether it still acts.
+ const before = stream.sizes.length
+ await manager.handleFrame('app-1', 'terminal_resize', {
+ streamId: first, resizeSeq: 1, cols: 90, rows: 25,
+ })
+ expect(stream.sizes.length).toBe(before + 1)
+ })
+
+ it('still replaces its own stream when the same terminal is reopened', async () => {
+ await manager.handleFrame('app-1', 'terminal_open', {
+ requestId: 'open-1', protocolVersion: 3, agentId: 'agent-1', cols: 100, rows: 30,
+ })
+ const first = sent.find((frame) => frame.type === 'terminal_ready')?.payload.streamId as string
+
+ await manager.handleFrame('app-1', 'terminal_open', {
+ requestId: 'open-2', protocolVersion: 3, agentId: 'agent-1', cols: 100, rows: 30,
+ })
+
+ // Two tmux clients on one window is the thing this must never allow.
+ const before = stream.sizes.length
+ await manager.handleFrame('app-1', 'terminal_resize', {
+ streamId: first, resizeSeq: 1, cols: 90, rows: 25,
+ })
+ expect(stream.sizes.length).toBe(before)
+ })
+
it('uses frequent ACKs rather than the five-second heartbeat for output backpressure', async () => {
await manager.handleFrame('web-1', 'terminal_open', {
requestId: 'open-1', protocolVersion: 3, agentId: 'agent-1', cols: 100, rows: 30,
diff --git a/cli/src/lib/terminalStreamManager.ts b/cli/src/lib/terminalStreamManager.ts
index 9e10ce48c..6b6e703bd 100644
--- a/cli/src/lib/terminalStreamManager.ts
+++ b/cli/src/lib/terminalStreamManager.ts
@@ -229,7 +229,16 @@ export class TerminalStreamManager {
// A terminal is single-controller. A later client explicitly wins the lease and the incumbent
// receives a targeted close notification; it must not be broadcast to other clients.
await this.closeStreamsForTakeover(session.agentId, reservedPlacement, connId)
- await this.closeConnection(connId, 'replaced', false)
+ // Only THIS connection's stream for THIS terminal, not every stream it holds.
+ //
+ // It used to be closeConnection(connId), i.e. "opening a terminal ends every other terminal
+ // this client had". That was invisible while a client could only ever show one terminal at a
+ // time. A client showing several — the desktop app's pane grid — opens a second agent on the
+ // same connection, and the blanket close killed the FIRST agent's stream a few milliseconds
+ // after handing it its opening keyframe. The pane kept rendering that stale keyframe and
+ // looked alive, but its session never reached `controlling`, so every keystroke into it was
+ // dropped in silence.
+ await this.closeOwnStreamsFor(connId, session.agentId, reservedPlacement)
this.controllerByAgent.set(session.agentId, connId)
this.controllerByPlacement.set(reservedPlacement, connId)
const streamId = randomUUID()
@@ -627,6 +636,19 @@ export class TerminalStreamManager {
})
}
+ /// Replace this connection's own stream for the same terminal.
+ ///
+ /// Reopening is routine — a resync, a relay that came back — and the old stream has to go or the
+ /// agent would have two tmux clients on one window. Matched on agent AND placement so a client
+ /// holding several different terminals keeps the ones it did not ask about. No notification: the
+ /// client that asked for this is the one being replaced, and it already knows.
+ private async closeOwnStreamsFor(connId: string, agentId: string, placementKey: string): Promise<void> {
+ const own = [...this.streams.values()].filter((state) =>
+ !state.closing && state.connId === connId
+ && (state.agentId === agentId || state.placementKey === placementKey))
+ await Promise.all(own.map((state) => this.closeStream(state, 'replaced', false)))
+ }
+
private async closeStreamsForTakeover(agentId: string, placementKey: string, nextConnId: string): Promise<void> {
const incumbents = [...this.streams.values()].filter((state) =>
!state.closing && state.connId !== nextConnId