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.

feat(pty): smallest subscriber wins — authoritative pty:size broadcast

enes8707ab3b88c1
diff --git a/src/core/pty-coattach.test.ts b/src/core/pty-coattach.test.ts
index 5d65b01d6..2c0c379c6 100644
--- a/src/core/pty-coattach.test.ts
+++ b/src/core/pty-coattach.test.ts
@@ -297,6 +297,150 @@ describe('terminal co-attach: one PTY, N subscribers', () => {
})
})
+describe('size negotiation: smallest subscriber wins', () => {
+ let fake: FakePlatform
+
+ beforeEach(() => {
+ spawned.length = 0
+ failNextSpawn = false
+ fake = fakePlatform()
+ initPlatform(fake)
+ vi.useFakeTimers()
+ })
+ afterEach(() => {
+ vi.useRealTimers()
+ resetPlatformForTests()
+ })
+
+ async function manager() {
+ const { PtyManager } = await import('./pty-manager')
+ const m = new PtyManager()
+ m.registerIpc()
+ return m
+ }
+ const create = (clientId: number, cols: number, rows: number, persistKey = 'node-1') =>
+ fake.handlers[IPC.ptyCreate](clientId, { cols, rows, persistKey }) as Promise<{
+ sessionId: string
+ fresh: boolean
+ }>
+ // pty:resize is sender-aware now (a size must be attributed to the client that reported it).
+ const resize = (
+ clientId: number,
+ sessionId: string,
+ cols: number | null,
+ rows: number | null
+ ) => fake.senderListeners[IPC.ptyResize](clientId, sessionId, cols, rows)
+ const kill = (clientId: number, sessionId: string) =>
+ fake.senderListeners[IPC.ptyKill](clientId, sessionId)
+ const sizes = (sessionId: string) =>
+ fake.sent.filter((s) => s.channel === IPC.ptySize(sessionId))
+
+ it('runs the pty at min(cols) x min(rows) and broadcasts it to every subscriber', async () => {
+ await manager()
+ const { sessionId } = await create(ALICE, 120, 40)
+ fake.sent.length = 0
+ await create(BOB, 80, 60) // narrower but taller
+
+ expect(spawned[0].resizes.at(-1)).toEqual({ cols: 80, rows: 40 })
+ const sent = sizes(sessionId)
+ expect(sent.map((s) => s.to).sort()).toEqual([ALICE, BOB])
+ expect(sent.every((s) => s.args[0].cols === 80 && s.args[0].rows === 40)).toBe(true)
+ })
+
+ it('a joiner bigger than the pty is told the authoritative size (nothing is resized)', async () => {
+ await manager()
+ const { sessionId } = await create(ALICE, 80, 24)
+ fake.sent.length = 0
+ await create(BOB, 120, 40)
+
+ // The pty already runs at 80x24 (Alice spawned it there) — do not make tmux redraw for nothing.
+ expect(spawned[0].resizes).toEqual([])
+ // Bob's xterm fitted itself to 120x40, so he MUST be told to render 80x24 and letterbox.
+ const sent = sizes(sessionId)
+ expect(sent.map((s) => s.to)).toEqual([BOB]) // Alice already renders 80x24 — no message for her
+ expect(sent[0].args[0]).toEqual({ cols: 80, rows: 24 })
+ })
+
+ it('a subscriber growing its window does not grow the pty past the smaller peer', async () => {
+ await manager()
+ const { sessionId } = await create(ALICE, 80, 24)
+ await create(BOB, 120, 40)
+ fake.sent.length = 0
+ resize(BOB, sessionId, 200, 80) // Bob maximizes
+
+ expect(spawned[0].resizes.every((r) => r.cols <= 80 && r.rows <= 24)).toBe(true)
+ // Bob's own fit is not authoritative: he is told (again) to render Alice's size.
+ const sent = sizes(sessionId)
+ expect(sent.map((s) => s.to)).toEqual([BOB])
+ expect(sent[0].args[0]).toEqual({ cols: 80, rows: 24 })
+ })
+
+ it('when the smallest subscriber leaves, the pty grows back to the remaining one', async () => {
+ await manager()
+ const { sessionId } = await create(ALICE, 80, 24)
+ await create(BOB, 120, 40)
+
+ fake.sent.length = 0
+ kill(ALICE, sessionId)
+ expect(spawned[0].resizes.at(-1)).toEqual({ cols: 120, rows: 40 })
+ const sent = sizes(sessionId)
+ expect(sent.map((s) => s.to)).toEqual([BOB])
+ expect(sent[0].args[0]).toEqual({ cols: 120, rows: 40 })
+ })
+
+ // ── The single-user path must stay bit-for-bit identical ─────────────────────────────────
+ it('a solo subscriber resizes the pty to its own size, with no pty:size traffic at all', async () => {
+ await manager()
+ const { sessionId } = await create(ALICE, 80, 24)
+ fake.sent.length = 0
+
+ resize(ALICE, sessionId, 100, 30)
+ expect(spawned[0].resizes).toEqual([{ cols: 100, rows: 30 }]) // min(one) is Alice's own size
+ // Alice already renders what she asked for: telling her would be pure round-trip noise.
+ expect(sizes(sessionId)).toEqual([])
+
+ // Same fit reported twice (a ResizeObserver tick that changed nothing) → no second ioctl.
+ resize(ALICE, sessionId, 100, 30)
+ expect(spawned[0].resizes).toHaveLength(1)
+ })
+
+ // ── A PARKED terminal is subscribed but not looking ──────────────────────────────────────
+ // The renderer keeps a node's xterm+PTY alive for 5 minutes after unmount (TERM_PARK_MS), so a
+ // parked client stays a subscriber. Its (possibly tiny) last fit must NOT shrink the terminal
+ // for the people still watching — it reports a null size instead: "listening, not viewing".
+ it('a parked subscriber (null size) stops constraining the shared pty but keeps its output', async () => {
+ await manager()
+ const { sessionId } = await create(ALICE, 80, 24)
+ await create(BOB, 120, 40)
+ fake.sent.length = 0
+
+ resize(ALICE, sessionId, null, null) // Alice's node unmounted → parked
+ expect(spawned[0].resizes.at(-1)).toEqual({ cols: 120, rows: 40 }) // Bob gets his full size back
+ expect(sizes(sessionId).map((s) => s.to).sort()).toEqual([ALICE, BOB])
+
+ // Parked ≠ gone: the parked xterm keeps consuming output (that is what makes re-adoption exact).
+ fake.sent.length = 0
+ spawned[0].onDataCb?.('still streaming')
+ vi.advanceTimersByTime(20)
+ const data = fake.sent.filter((s) => s.channel === IPC.ptyData(sessionId))
+ expect(data.map((s) => s.to).sort()).toEqual([ALICE, BOB])
+
+ // Un-parking (re-adopt → fresh fit) makes her constrain again.
+ resize(ALICE, sessionId, 80, 24)
+ expect(spawned[0].resizes.at(-1)).toEqual({ cols: 80, rows: 24 })
+ })
+
+ it('a parked SOLO subscriber leaves the pty size alone (nobody is left to size it)', async () => {
+ await manager()
+ const { sessionId } = await create(ALICE, 80, 24)
+ fake.sent.length = 0
+
+ resize(ALICE, sessionId, null, null)
+ expect(spawned[0].resizes).toEqual([]) // no viewers → keep the last size, never resize to 1x1
+ expect(sizes(sessionId)).toEqual([])
+ })
+})
+
describe('tmuxAttachFlags', () => {
it("keeps -D for the app's own client: exactly ONE tmux client per session", async () => {
const { tmuxAttachFlags } = await import('./pty-manager')
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.ts
index 5bf12f78c..68d5150a2 100644
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@ -22,7 +22,7 @@ import {
} from './remote-ssh/control-master'
import { TMUX_SOCKET, sessionName } from './tmux-naming'
import { releasePty, type ReleasablePty } from './pty-release'
-import { type PtySize } from './pty-size'
+import { effectiveSize, type PtySize } from './pty-size'
import { machOArch, archMismatch } from './macho-arch'
import { writeScrollback, readScrollback, deleteScrollback } from './scrollback-store'
import { claudeConfigDirFor } from './claude-config-dir'
@@ -228,6 +228,12 @@ export async function findInLoginPath(bin: string): Promise<string | null> {
/** A UI client: an Electron webContents id or a ServerPlatform uiId. */
type ClientId = number
+/** One client's reported fit, run through the same floor/clamp the pty itself gets, so a size we
+ * record for a client is comparable with the effective size we compute from all of them. */
+function normalizeSize(cols: number, rows: number): PtySize {
+ return effectiveSize([{ cols, rows }]) as PtySize // one entry in ⇒ never null out
+}
+
interface Session {
proc: pty.IPty
/** Every UI watching this session. Co-attach: ONE pty and ONE tmux client, N subscribers —
@@ -235,8 +241,19 @@ interface Session {
* client (whose `-D` would then kick the first one off). Empty for a purely detached
* (relay-served) session. */
subscribers: Set<ClientId>
- /** Each subscriber's last reported cols/rows. The pty runs at the min of these (Task 3). */
- sizes: Map<ClientId, PtySize>
+ /** Each VIEWING subscriber's last reported cols/rows — the pty runs at the min of these
+ * (`effectiveSize`). A subscriber that is subscribed but NOT looking is ABSENT from this map:
+ * it still gets output, it just doesn't constrain the size (see `resize`). `null` keys the
+ * relay host's detached sink, which has no ClientId but does report a size. */
+ sizes: Map<ClientId | null, PtySize>
+ /** The size each subscriber's xterm is believed to be rendering: the last authoritative size we
+ * sent it, or — if it has reported a fit since — its own fit (the renderer applies its own fit
+ * locally, exactly as it always has). We only send `pty:size` to a subscriber whose view differs
+ * from the effective size, which is what keeps a SOLO user's resize free of any extra IPC. */
+ shown: Map<ClientId, PtySize>
+ /** The size currently pushed into the pty (seeded from the spawn's cols/rows). Guards against
+ * re-resizing the tmux client to the size it already has — that is a full-pane redraw. */
+ appliedSize?: PtySize
/** The node id this session was created for, whether or not it is tmux-persisted — the key of
* the `byPersistKey` co-attach index (`persistKey` below is only set for PERSISTED sessions,
* because it also gates scrollback snapshots). Undefined for a relay-served (detached) pty,
@@ -384,8 +401,13 @@ export class PtyManager {
platform().on(IPC.ptyWrite, (sessionId: string, data: string) =>
this.write(sessionId, data)
)
- platform().on(IPC.ptyResize, (sessionId: string, cols: number, rows: number) =>
- this.resize(sessionId, cols, rows)
+ // Sender-aware: a size is only meaningful with the client it belongs to — the pty runs at the
+ // smallest one (`resize`). Registered ONLY here: `on` and `onWithSender` compose on the same
+ // channel, so leaving the old plain listener in place would run the resize twice.
+ platform().onWithSender(
+ IPC.ptyResize,
+ (senderId: number, sessionId: string, cols: number | null, rows: number | null) =>
+ this.resize(senderId, sessionId, cols, rows)
)
platform().on(IPC.ptyFlow, (sessionId: string, resume: boolean) =>
this.setFlow(sessionId, resume)
@@ -450,7 +472,12 @@ export class PtyManager {
const existing = existingId ? this.sessions.get(existingId) : undefined
if (!existingId || !existing) return undefined
existing.subscribers.add(clientId)
- existing.sizes.set(clientId, { cols: options.cols, rows: options.rows })
+ // The joiner's xterm has fitted itself to its own window; that is what it renders until we
+ // tell it otherwise. applySize() then either shrinks the pty to it (it is the new smallest) or
+ // sends it the authoritative size to render + letterbox.
+ const size = normalizeSize(options.cols, options.rows)
+ existing.sizes.set(clientId, size)
+ existing.shown.set(clientId, size)
this.applySize(existingId, existing)
// A joiner's backlog is empty by definition, so it will never issue the resume that a
// previous (now gone) client's flow control owed us — and no data would ever arrive to
@@ -824,13 +851,23 @@ export class PtyManager {
const remote = options.sshRemote && options.persistKey && remoteSsh ? options.sshRemote : undefined
const tmuxBacked = !!(this.tmuxPath && settings.tmuxEnabled && options.persistKey)
const persisted = !!options.persistKey && (remote ? true : tmuxBacked)
+ const spawnSize = normalizeSize(options.cols, options.rows)
const session: Session = {
proc,
subscribers: clientId === null ? new Set<ClientId>() : new Set<ClientId>([clientId]),
sizes:
+ clientId === null
+ ? new Map<ClientId | null, PtySize>()
+ : new Map<ClientId | null, PtySize>([[clientId, spawnSize]]),
+ shown:
clientId === null
? new Map<ClientId, PtySize>()
- : new Map<ClientId, PtySize>([[clientId, { cols: options.cols, rows: options.rows }]]),
+ : new Map<ClientId, PtySize>([[clientId, spawnSize]]),
+ // node-pty was just spawned with these cols/rows, so the pty ALREADY has this size — record
+ // it so a co-attach (or a fit that reports the same numbers) doesn't ioctl it needlessly.
+ // A detached (relay-served) pty is left unseeded: its first `resize` must reach the pty,
+ // exactly as before, because its sink never reports a size at create time.
+ appliedSize: clientId === null ? undefined : spawnSize,
indexKey: options.persistKey && !sinks ? options.persistKey : undefined,
onData: sinks?.onData,
onExit: sinks?.onExit,
@@ -871,8 +908,40 @@ export class PtyManager {
this.byPersistKey.delete(session.indexKey)
}
- /** Resize the pty to the smallest subscriber's size and tell everyone (Task 3). */
- private applySize(_sessionId: string, _session: Session): void {}
+ /**
+ * Push the effective (smallest-subscriber) size into the pty, then tell every subscriber whose
+ * xterm is NOT already rendering that grid what it actually is, so each one renders exactly the
+ * pty's grid (and letterboxes the leftover space) instead of its own `fit()` guess.
+ *
+ * Two separate idempotence guards, both load-bearing:
+ * - the pty is only resized when the effective size CHANGED (a same-size ioctl makes the tmux
+ * client redraw the whole pane for nothing);
+ * - a subscriber is only messaged when the size differs from what it is showing. With exactly
+ * one (viewing) subscriber the min of a one-element set is that subscriber's own fit, so a
+ * solo user is never sent a `pty:size` at all — the single-user path is unchanged.
+ */
+ private applySize(sessionId: string, session: Session): void {
+ const size = effectiveSize(session.sizes.values())
+ // Nobody is looking (every subscriber is parked, or none has reported a fit yet): leave the
+ // pty at whatever size it has. Resizing it to a default here would garble the parked xterms'
+ // buffers and the tmux pane behind them for no viewer's benefit.
+ if (!size) return
+ if (session.appliedSize?.cols !== size.cols || session.appliedSize?.rows !== size.rows) {
+ session.appliedSize = size
+ try {
+ session.proc.resize(size.cols, size.rows)
+ } catch {
+ // resize can throw if the proc already exited; ignore.
+ }
+ }
+ const channel = IPC.ptySize(sessionId)
+ for (const sub of session.subscribers) {
+ const shown = session.shown.get(sub)
+ if (shown && shown.cols === size.cols && shown.rows === size.rows) continue
+ session.shown.set(sub, size)
+ this.send(sub, channel, size)
+ }
+ }
/** Buffer a chunk; flush immediately past the byte cap, otherwise on a short timer. */
private queueData(sessionId: string, session: Session, data: string): void {
@@ -935,11 +1004,43 @@ export class PtyManager {
this.sessions.get(sessionId)?.proc.write(data)
}
- resize(sessionId: string, cols: number, rows: number): void {
+ /**
+ * A subscriber reports the size IT can render. The pty runs at the smallest of them, so nobody
+ * is ever sent more columns than their xterm can draw (a subscriber with room to spare
+ * letterboxes the remainder). With exactly one subscriber, min(one) is that subscriber's own
+ * size — the single-user path resizes the pty to exactly what it asked for, as it always did.
+ *
+ * A `null` cols/rows means **"subscribed, but not looking"**: the client stays in the fan-out
+ * (it keeps consuming output) but drops out of the min. This is what a PARKED terminal reports —
+ * the renderer keeps an unmounted node's xterm+PTY alive for 5 minutes so a remount re-adopts
+ * them exactly, and without this a window somebody parked small would keep every other viewer's
+ * terminal shrunk for those 5 minutes even though nobody is looking at it. `null` (not 0) carries
+ * that meaning because 0 already has one: `effectiveSize` clamps a not-yet-measured VIEWING
+ * client's 0 up to 1 rather than letting it zero the pty.
+ *
+ * `clientId` is null for the relay host's detached pty (its sink reports the mirrored client's
+ * size); it constrains the size like any other viewer but is not in `subscribers`, so it gets no
+ * `pty:size` message — the relay has its own size channel.
+ */
+ resize(
+ clientId: ClientId | null,
+ sessionId: string,
+ cols: number | null,
+ rows: number | null
+ ): void {
const session = this.sessions.get(sessionId)
if (!session) return
- // cols/rows must be at least 1, otherwise node-pty throws.
- session.proc.resize(Math.max(1, cols), Math.max(1, rows))
+ if (cols === null || rows === null) {
+ session.sizes.delete(clientId)
+ } else {
+ const size = normalizeSize(cols, rows)
+ session.sizes.set(clientId, size)
+ // The client's own xterm fits itself locally (as it always has), so its fit — not the last
+ // authoritative size we sent it — is what it is rendering right now. If that fit isn't the
+ // effective size, applySize() below corrects it straight back.
+ if (clientId !== null) session.shown.set(clientId, size)
+ }
+ this.applySize(sessionId, session)
}
/**
@@ -957,9 +1058,11 @@ export class PtyManager {
if (clientId === null) {
session.onData = undefined
session.onExit = undefined
+ session.sizes.delete(null)
} else {
session.subscribers.delete(clientId)
session.sizes.delete(clientId)
+ session.shown.delete(clientId)
}
if (session.subscribers.size > 0 || session.onData) {
// Somebody is still watching: the departing client's size no longer constrains the pty.
diff --git a/src/main/remote/host-service.ts b/src/main/remote/host-service.ts
index 783eca519..9092cb2cf 100644
--- a/src/main/remote/host-service.ts
+++ b/src/main/remote/host-service.ts
@@ -61,7 +61,13 @@ export interface HostPtyManager {
/** Current visible screen of a node's tmux session, for the attach snapshot. */
captureSnapshot(persistKey: string): Promise<string>
write(sessionId: string, data: string): void
- resize(sessionId: string, cols: number, rows: number): void
+ /** `clientId` is null for a relay-served (detached) pty — see `kill` below. */
+ resize(
+ clientId: number | null,
+ sessionId: string,
+ cols: number | null,
+ rows: number | null
+ ): void
setFlow(sessionId: string, resume: boolean): void
/** `clientId` is null for a relay-served (detached) pty: the sinks ARE its only subscriber. */
kill(clientId: number | null, sessionId: string): void
@@ -350,7 +356,9 @@ export function createHostHandlers(
frame.payload.byteOffset,
frame.payload.byteLength
)
- pty.resize(stream.sessionId, view.getUint16(0, true), view.getUint16(2, true))
+ // null clientId: this pty is relay-served (its sink is the only "subscriber"), so the
+ // mirrored client's size is recorded against the sink rather than a UI client id.
+ pty.resize(null, stream.sessionId, view.getUint16(0, true), view.getUint16(2, true))
}
}
},
diff --git a/test/remote/host-handlers.test.ts b/test/remote/host-handlers.test.ts
index 69925a14a..98004d66d 100644
--- a/test/remote/host-handlers.test.ts
+++ b/test/remote/host-handlers.test.ts
@@ -34,8 +34,8 @@ function fakePty() {
return snapshot
},
write: (sessionId, data) => calls.push({ method: 'write', args: [sessionId, data] }),
- resize: (sessionId, cols, rows) =>
- calls.push({ method: 'resize', args: [sessionId, cols, rows] }),
+ resize: (clientId, sessionId, cols, rows) =>
+ calls.push({ method: 'resize', args: [clientId, sessionId, cols, rows] }),
setFlow: (sessionId, resume) => calls.push({ method: 'setFlow', args: [sessionId, resume] }),
// Relay-served ptys are killed with clientId null (they have no UI subscribers).
kill: (clientId, sessionId) => calls.push({ method: 'kill', args: [clientId, sessionId] })
@@ -177,7 +177,8 @@ describe('createHostHandlers', () => {
h.onFrame({ op: OP.Resize, streamId: 1, seq: 0, payload: resizePayload(120, 40) })
expect(pty.calls).toContainEqual({ method: 'write', args: ['pty-1', 'ls\n'] })
- expect(pty.calls).toContainEqual({ method: 'resize', args: ['pty-1', 120, 40] })
+ // clientId null: the relay-served pty's size is recorded against its sink, not a UI client.
+ expect(pty.calls).toContainEqual({ method: 'resize', args: [null, 'pty-1', 120, 40] })
})
it('ignores frames for unknown streams', () => {