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(terminal): render the pty's authoritative size, letterbox the rest, handle redraw + peer delete

enes237cb6d7f804
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index 0582ca52d..f23c3eb07 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -27,7 +27,16 @@ import { ContextMeter } from '../components/ContextMeter'
import { isZoomModifierHeld } from '../lib/zoomModifier'
import { useSettings } from '../state/settings'
import { useAgentStatus, inferInterruptAfterSettle } from '../state/agentStatus'
-import { reportFocus, releaseFocus } from '../state/presence'
+import {
+ closedByLabel,
+ isLetterboxed,
+ reportedSize,
+ shouldApplyResync,
+ toXtermText,
+ type TermSize
+} from '../terminal/terminal-config'
+import { reportFocus, releaseFocus, usePresence } from '../state/presence'
+import type { ClientId } from '@shared/presence'
import { PresenceChips } from '../components/PresenceChips'
import { useAgentNodes } from '../state/agentNodes'
import { useProjects } from '../state/projects'
@@ -121,8 +130,6 @@ interface ParkedTerminal {
search: SearchAddon
transport: TerminalTransport
sessionId: string
- sentCols: number
- sentRows: number
/** Session-scoped teardown (transport/xterm listeners) — run only at final dispose. */
cleanups: Array<() => void>
timer: ReturnType<typeof setTimeout>
@@ -155,6 +162,45 @@ const noParkIds = new Set<string>()
export function disposeTerminalOnUnmount(id: string): void {
noParkIds.add(id)
disposeParkedTerminal(id)
+ coStates.delete(id)
+}
+
+/**
+ * Co-attach UI state per node — kept OUTSIDE React on purpose.
+ *
+ * The transport listeners (onSize / onClosed / onResync) are wired ONCE, in the spawn
+ * continuation, and they SURVIVE a park: an adopted terminal carries its `cleanups` over and never
+ * re-subscribes. A remounted node is a NEW React instance, so a `setState` captured by those
+ * listeners would update a component that no longer exists. They publish here instead, and
+ * whichever instance is currently mounted subscribes.
+ *
+ * `closed` is also the respawn guard: once another client has DESTROYED this node's session
+ * (tmux kill-session — gone for everyone), a remount must NOT call `transport.create` again. Core
+ * can only make that respawn fresh, not impossible — it would resurrect a terminal its owner
+ * deliberately killed. Cleared only on permanent deletion (disposeTerminalOnUnmount).
+ */
+interface CoState {
+ /** The pty runs at a SMALLER subscriber's grid than we could fit → center + letterbox. */
+ letterbox: boolean
+ /** Set once the session was destroyed by someone else. `by` is null for an unattributed destroy. */
+ closed: { by: ClientId | null } | null
+}
+const NO_CO: CoState = { letterbox: false, closed: null }
+const coStates = new Map<string, CoState>()
+const coSubs = new Map<string, (s: CoState) => void>()
+
+function getCo(id: string): CoState {
+ return coStates.get(id) ?? NO_CO
+}
+
+function setCo(id: string, patch: Partial<CoState>): void {
+ const prev = getCo(id)
+ const next = { ...prev, ...patch }
+ // A no-op write must stay a no-op: applyFit clears the letterbox on every fit, and handing the
+ // node a fresh object each time would re-render it for nothing (and, solo, on every resize tick).
+ if (next.letterbox === prev.letterbox && next.closed === prev.closed) return
+ coStates.set(id, next)
+ coSubs.get(id)?.(next)
}
/**
@@ -187,6 +233,9 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
const termRef = useRef<Terminal | null>(null)
const fitRef = useRef<FitAddon | null>(null)
const searchAddonRef = useRef<SearchAddon | null>(null)
+ // The live session's "measure my grid, render it, report it" routine (set by the lifecycle
+ // effect), so effects outside that closure (font/cursor changes) resize through the same path.
+ const applyFitRef = useRef<(() => void) | null>(null)
const dwellRef = useRef<ReturnType<typeof setTimeout> | null>(null)
const [showColors, setShowColors] = useState(false)
const [armed, setArmed] = useState(true)
@@ -255,6 +304,20 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
// Set when the session fell back to the system account because this node's account folder was
// missing at spawn (Task 3 fallback) — flags the account chip with a warning tint + tooltip.
const [accountFallback, setAccountFallback] = useState(false)
+ // Co-attach state published by the (park-surviving) transport listeners — see CoState.
+ const [co, setCo_] = useState<CoState>(() => getCo(id))
+ useEffect(() => {
+ coSubs.set(id, setCo_)
+ setCo_(getCo(id)) // catch up anything published while this instance was mounting
+ return () => {
+ if (coSubs.get(id) === setCo_) coSubs.delete(id)
+ }
+ }, [id])
+ // The name of the peer who closed this node. Read NON-reactively (getState, not a selector): the
+ // presence store is written at cursor rate and its perf contract reserves subscriptions for the
+ // presence components — a per-terminal subscriber would run on every one of those writes. The
+ // overlay is terminal state anyway, so resolving the name when `co.closed` appears is enough.
+ const closedName = co.closed ? closedByLabel(co.closed.by, usePresence.getState().peers) : ''
// Stable fallback reader: serialize the live xterm buffer when tmux capture is unavailable.
const readBuffer = useCallback(() => {
@@ -358,22 +421,63 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
}
}
+ let sessionId: string | null = parked ? parked.sessionId : null
+ let disposed = false
+ // Last cols/rows REPORTED to the pty (seeded at create): a resize IPC makes tmux redraw the
+ // whole pane, so a same-size fit (e.g. the ResizeObserver's initial tick right after mount)
+ // must not send one — on a bulk project load that redraw doubles per node.
+ // NOT carried over from the park entry: parking REPORTS "not viewing" (null, null), which drops
+ // this client out of the pty's size set. Keeping the old values would make the common same-size
+ // adopt send nothing at all — we would never re-enter the set, while still being told to render
+ // the other viewer's (larger) grid. Zeroed here, so an adopting client always re-reports.
+ let sentCols = 0
+ let sentRows = 0
+ /** What we last reported — the reference the letterbox is measured against. */
+ let fitted: TermSize | null = null
+
+ /**
+ * Co-attach sizing: we REPORT what we could render (`proposeDimensions`) and the pty tells us
+ * what it actually runs at — the smallest subscriber's grid (`onSize`).
+ *
+ * Two rules, both load-bearing:
+ * - a REPORTED fit is also applied locally, because that is exactly what the pty assumes we
+ * are showing (PtyManager.resize → `session.shown`). If it is not the effective size, the
+ * broadcast corrects us right back; solo, the min of a one-element set IS our fit, so no
+ * broadcast is ever sent and this is the old `fit.fit()` + report, unchanged.
+ * - an UNCHANGED fit resizes nothing. A sub-cell container change re-runs this with the same
+ * cols/rows, and resizing the emulator back up to our fit here would undo a letterbox with
+ * no report to correct it — the pty's state didn't change, so no `pty:size` would follow.
+ */
+ const applyFit = () => {
+ try {
+ const size = reportedSize(fit.proposeDimensions())
+ if (!size) return
+ if (size.cols === sentCols && size.rows === sentRows) return
+ fitted = size
+ sentCols = size.cols
+ sentRows = size.rows
+ if (term.cols !== size.cols || term.rows !== size.rows) term.resize(size.cols, size.rows)
+ setCo(id, { letterbox: false })
+ // Before the session exists we are the only voice: the size rides the initial `create()`.
+ if (sessionId) transport.resize(sessionId, size.cols, size.rows)
+ } catch {
+ // proposeDimensions can throw when the element is 0-sized (collapsed); ignore.
+ }
+ }
+ applyFitRef.current = applyFit
+
if (parked) {
// Reattach the parked xterm's DOM element: the PTY never detached, so the screen is
// already current — no spawn, no tmux redraw, no terminal-mode re-negotiation.
if (term.element) container.appendChild(term.element)
loadWebgl()
- try {
- fit.fit()
- } catch {
- // zero-size (collapsed) — the ResizeObserver below refits when it grows
- }
+ applyFit()
} else {
term.loadAddon(fit)
term.loadAddon(searchAddon)
term.open(container)
loadWebgl()
- fit.fit()
+ applyFit()
patchTerminalScale(term, getZoom)
// OSC 52 clipboard write: the remote tmux (set-clipboard on) emits OSC 52 on copy; route the
// decoded text to the Mac clipboard. WRITE-ONLY — `parseOsc52` returns null for a `?` read
@@ -400,13 +504,6 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
return true
})
- let sessionId: string | null = parked ? parked.sessionId : null
- let disposed = false
- // Last cols/rows actually sent to the PTY (seeded at create): a resize IPC makes tmux redraw
- // the whole pane, so a same-size fit (e.g. the ResizeObserver's initial tick right after
- // mount) must not send one — on a bulk project load that redraw doubles per node.
- let sentCols = parked ? parked.sentCols : 0
- let sentRows = parked ? parked.sentRows : 0
// Session-scoped teardown. An adopted terminal carries its listeners over (they were wired
// to the still-live session on first mount); everything below that pushes here is gated on
// `!parked` so nothing is wired twice.
@@ -448,11 +545,16 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
// Prefetch the persisted scrollback in parallel with the spawn so it's ready to replay the
// instant the session resolves (a cold restart after a reboot recreates the tmux session
// empty — see the `fresh` handling below). Cheap no-op ('') when there's no snapshot.
- const scrollbackPromise = parked
- ? Promise.resolve('')
- : window.nodeTerminal.pty.readScrollback(id).catch(() => '')
+ const scrollbackPromise =
+ parked || getCo(id).closed
+ ? Promise.resolve('')
+ : window.nodeTerminal.pty.readScrollback(id).catch(() => '')
void (async () => {
if (parked) return // adopted a live session — nothing to spawn or replay
+ // Another client DESTROYED this node's session (tmux kill-session — for everyone). Never
+ // spawn: `create(persistKey)` would happily start a brand-new tmux session and resurrect a
+ // terminal its owner deliberately killed. The overlay explains the state instead.
+ if (getCo(id).closed) return
// SSH-project terminal: the project's live ControlMaster controlPath is established by
// Canvas's active-project effect. On a cold app load child effects run before that parent
// connect, so wait for it (briefly) before spawning. In Phase 1 a node only exists in the
@@ -484,12 +586,45 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
}
sessionId = sid
if (fellBack) setAccountFallback(true)
- // Catch up a size change that landed while the spawn was in flight (resize() skips the
+ // Catch up a size change that landed while the spawn was in flight (applyFit skips the
// IPC until sessionId is set, and the observer won't re-fire without another change).
- if (term.cols !== sentCols || term.rows !== sentRows) {
- sentCols = term.cols
- sentRows = term.rows
- transport.resize(sid, term.cols, term.rows)
+ applyFit()
+ // The pty is the authority on the grid: it runs at the SMALLEST subscriber's size, so
+ // render exactly that and letterbox the leftover space. With one subscriber the min is our
+ // own proposal, so a solo user is never sent this at all — nothing re-fits, nothing repaints.
+ if (transport.onSize) {
+ cleanups.push(
+ transport.onSize(sid, (size) => {
+ if (term.cols !== size.cols || term.rows !== size.rows)
+ term.resize(size.cols, size.rows)
+ setCo(id, { letterbox: isLetterboxed(size, fitted) })
+ })
+ )
+ }
+ // Someone else permanently destroyed this node (tmux kill-session): show who, and make sure
+ // this component never respawns the session — see CoState.
+ if (transport.onClosed) {
+ cleanups.push(
+ transport.onClosed(sid, ({ by }) => {
+ setCo(id, { closed: { by } })
+ term.write('\r\n\x1b[90m[session closed by another user]\x1b[0m\r\n')
+ })
+ )
+ }
+ // We fell so far behind that the server discarded our queued output and redrew us from
+ // tmux. The capture IS the current screen, so reset the emulator and write it — writing it
+ // on top of a stale buffer would splice two different points in time. An EMPTY payload is
+ // ignored outright (shouldApplyResync): a wrongly cleared screen is unrecoverable, a
+ // skipped repaint is not. The separator mirrors the cold-restore one.
+ if (transport.onResync) {
+ cleanups.push(
+ transport.onResync(sid, (screen) => {
+ if (!shouldApplyResync(screen)) return
+ term.reset()
+ term.write(toXtermText(screen))
+ term.write('\r\n\x1b[90m── reconnected — earlier output skipped ──\x1b[0m\r\n')
+ })
+ )
}
// Cold restart: the tmux session (and anything that was running in it) is gone — replay
// the last persisted scrollback so the user sees where they left off. Warm reattach
@@ -559,18 +694,6 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
})
})()
- const resize = () => {
- try {
- fit.fit()
- if (sessionId && (term.cols !== sentCols || term.rows !== sentRows)) {
- sentCols = term.cols
- sentRows = term.rows
- transport.resize(sessionId, term.cols, term.rows)
- }
- } catch {
- // fit can throw when the size is 0 (e.g. collapsed); ignore.
- }
- }
// Coalesce observer bursts: dragging the NodeResizer fires per animation frame, and every
// call is a full cell-geometry measure + a resize IPC → node-pty → tmux (which redraws the
// whole pane). One trailing fit per settle is enough — the canvas node frame itself still
@@ -578,7 +701,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
let resizeTimer: ReturnType<typeof setTimeout> | null = null
const observer = new ResizeObserver(() => {
if (resizeTimer) clearTimeout(resizeTimer)
- resizeTimer = setTimeout(resize, 80)
+ resizeTimer = setTimeout(applyFit, 80)
})
observer.observe(container)
@@ -602,6 +725,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
termRef.current = null
fitRef.current = null
searchAddonRef.current = null
+ if (applyFitRef.current === applyFit) applyFitRef.current = null
// Free the GPU context while hidden either way (live WebGL contexts are capped ~16).
try {
webgl?.dispose()
@@ -610,9 +734,15 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
}
// A respawn (worktree move: the ref was bumped before this cleanup ran) needs a FRESH
// session in the new cwd — never park it. A plain unmount with a live session parks:
- // the xterm (element detached) and its PTY stay alive so a remount re-adopts them.
+ // the xterm (element detached) and its PTY stay alive so a remount re-adopts them. A session
+ // another client destroyed is gone — nothing to park (and nothing left to keep alive).
const isRespawn = respawnNonceRef.current !== myNonce
- if (sessionId && !isRespawn && !noParkIds.delete(id)) {
+ if (sessionId && !isRespawn && !getCo(id).closed && !noParkIds.delete(id)) {
+ // Park = "subscribed, but not viewing": report no size at all, so this window's (possibly
+ // small) grid stops clamping every other subscriber's terminal for the next five minutes.
+ // The subscription itself stays — output keeps streaming into the parked xterm — and the
+ // adopting mount re-reports its size (sentCols/sentRows are NOT carried over; see above).
+ transport.resize(sessionId, null, null)
term.element?.remove()
const entry: ParkedTerminal = {
term,
@@ -620,8 +750,6 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
search: searchAddon,
transport,
sessionId,
- sentCols,
- sentRows,
cleanups,
timer: setTimeout(() => {
if (parkedTerminals.get(id) === entry) {
@@ -641,18 +769,16 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [data.respawnNonce])
- // Live-apply font/cursor settings to the running terminal.
+ // Live-apply font/cursor settings to the running terminal. A new font size means new cell
+ // geometry, i.e. a different grid — route it through applyFit (not a bare fit.fit()) so the pty
+ // is told the new size like any other resize, instead of running at a grid nobody renders.
useEffect(() => {
const term = termRef.current
if (!term) return
term.options.fontSize = fontSize
term.options.fontFamily = fontFamily
term.options.cursorBlink = cursorBlink
- try {
- fitRef.current?.fit()
- } catch {
- // ignore
- }
+ applyFitRef.current?.()
}, [fontSize, fontFamily, cursorBlink])
const toggleCollapse = () =>
@@ -1139,7 +1265,15 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
onDragLeave={onBodyDragLeave}
onDrop={onBodyDrop}
>
- <div className="term-node__xterm nodrag nowheel" ref={bodyRef} />
+ <div
+ className={`term-node__xterm nodrag nowheel${co.letterbox ? ' letterboxed' : ''}`}
+ ref={bodyRef}
+ />
+ {co.closed && (
+ <div className="term-node__closed nodrag">
+ Closed by {closedName} — this session was ended.
+ </div>
+ )}
{armed && !mdMode && (
<div
className="term-hover-guard"
diff --git a/src/renderer/styles.css b/src/renderer/styles.css
index a1581190e..4c2f104c8 100644
--- a/src/renderer/styles.css
+++ b/src/renderer/styles.css
@@ -1034,6 +1034,37 @@ body,
height: 100%;
}
+/* Co-attach: the pty runs at the SMALLEST subscriber's grid, so the terminal can be smaller than
+ this node's body. Center the leftover space instead of stretching — letterboxing, not
+ distortion. Only applied while a smaller peer is actually clamping us: a solo user's grid IS the
+ effective size, so their terminal keeps the plain (stretched, top-left) layout it always had. */
+.term-node__xterm.letterboxed {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ overflow: hidden;
+}
+
+.term-node__xterm.letterboxed .xterm {
+ height: auto;
+}
+
+/* Another client permanently destroyed this node's session (tmux kill-session): the terminal is
+ dead and must never respawn — say so over the last screen. */
+.term-node__closed {
+ position: absolute;
+ inset: 0;
+ z-index: 4;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ padding: 12px;
+ text-align: center;
+ font-size: 12px;
+ color: var(--muted);
+ background: rgba(30, 30, 30, 0.88);
+}
+
/* Markdown view overlay (Cmd/Ctrl+M) */
.term-md {
position: absolute;
diff --git a/src/renderer/terminal/local-transport.ts b/src/renderer/terminal/local-transport.ts
index c83ecfb96..cb0abe9c2 100644
--- a/src/renderer/terminal/local-transport.ts
+++ b/src/renderer/terminal/local-transport.ts
@@ -20,7 +20,7 @@ export class LocalTransport implements TerminalTransport {
this.pty.write(sessionId, data)
}
- resize(sessionId: string, cols: number, rows: number): void {
+ resize(sessionId: string, cols: number | null, rows: number | null): void {
this.pty.resize(sessionId, cols, rows)
}
diff --git a/src/renderer/terminal/remote-transport.ts b/src/renderer/terminal/remote-transport.ts
index cb337aabe..0eb8ec63a 100644
--- a/src/renderer/terminal/remote-transport.ts
+++ b/src/renderer/terminal/remote-transport.ts
@@ -38,7 +38,11 @@ export class RemoteTransport implements TerminalTransport {
this.client.write(this.connectionId, sessionId, data)
}
- resize(sessionId: string, cols: number, rows: number): void {
+ resize(sessionId: string, cols: number | null, rows: number | null): void {
+ // The relay frame protocol has no "not viewing" size: a mirrored client is not a subscriber of
+ // the host's size set, so a park signal (null) has nothing to say to it — drop it rather than
+ // sending a 0×0 the host would try to apply.
+ if (cols === null || rows === null) return
this.client.resize(this.connectionId, sessionId, cols, rows)
}
diff --git a/src/renderer/terminal/terminal-config.test.ts b/src/renderer/terminal/terminal-config.test.ts
new file mode 100644
index 000000000..d24fd7398
--- /dev/null
+++ b/src/renderer/terminal/terminal-config.test.ts
@@ -0,0 +1,71 @@
+import { describe, it, expect } from 'vitest'
+import {
+ closedByLabel,
+ isLetterboxed,
+ reportedSize,
+ shouldApplyResync,
+ toXtermText
+} from './terminal-config'
+import type { ClientId } from '@shared/presence'
+
+describe('reportedSize', () => {
+ it('reports the fit proposal, floored at 1 (a collapsed node can propose 0)', () => {
+ expect(reportedSize({ cols: 132, rows: 43 })).toEqual({ cols: 132, rows: 43 })
+ expect(reportedSize({ cols: 0, rows: 0 })).toEqual({ cols: 1, rows: 1 })
+ })
+
+ it('returns null when the fit cannot be measured (hidden / zero-size node)', () => {
+ expect(reportedSize(undefined)).toBeNull()
+ expect(reportedSize(null)).toBeNull()
+ expect(reportedSize({ cols: NaN, rows: 24 })).toBeNull()
+ expect(reportedSize({ cols: 80, rows: Infinity })).toBeNull()
+ expect(reportedSize({ cols: 80 })).toBeNull()
+ })
+})
+
+describe('isLetterboxed', () => {
+ it('is false for a solo user: the effective size IS their own fit', () => {
+ expect(isLetterboxed({ cols: 100, rows: 30 }, { cols: 100, rows: 30 })).toBe(false)
+ })
+
+ it('is true when the pty runs at a smaller subscriber s grid', () => {
+ expect(isLetterboxed({ cols: 80, rows: 30 }, { cols: 100, rows: 30 })).toBe(true)
+ expect(isLetterboxed({ cols: 100, rows: 24 }, { cols: 100, rows: 30 })).toBe(true)
+ })
+
+ it('is false while our own fit is unknown (nothing to letterbox against)', () => {
+ expect(isLetterboxed({ cols: 80, rows: 24 }, null)).toBe(false)
+ })
+})
+
+describe('shouldApplyResync', () => {
+ it('paints a non-empty capture', () => {
+ expect(shouldApplyResync('$ ls\nfoo\n')).toBe(true)
+ })
+
+ it('IGNORES an empty/absent payload — a wrongly reset screen is unrecoverable', () => {
+ expect(shouldApplyResync('')).toBe(false)
+ expect(shouldApplyResync(null)).toBe(false)
+ expect(shouldApplyResync(undefined)).toBe(false)
+ })
+})
+
+describe('toXtermText', () => {
+ it('turns tmux capture LFs into CRLFs, leaving existing CRLFs alone', () => {
+ expect(toXtermText('a\nb')).toBe('a\r\nb')
+ expect(toXtermText('a\r\nb')).toBe('a\r\nb')
+ })
+})
+
+describe('closedByLabel', () => {
+ const peers = { 7: { name: 'Ada' } } as Record<ClientId, { name: string }>
+
+ it('names the peer who destroyed the node', () => {
+ expect(closedByLabel(7 as ClientId, peers)).toBe('Ada')
+ })
+
+ it('degrades to a neutral label for an unattributed destroy or an unknown/departed peer', () => {
+ expect(closedByLabel(null, peers)).toBe('another user')
+ expect(closedByLabel(99 as ClientId, peers)).toBe('another user')
+ })
+})
diff --git a/src/renderer/terminal/terminal-config.ts b/src/renderer/terminal/terminal-config.ts
new file mode 100644
index 000000000..f072aabae
--- /dev/null
+++ b/src/renderer/terminal/terminal-config.ts
@@ -0,0 +1,73 @@
+import type { ClientId } from '@shared/presence'
+
+/**
+ * Pure decisions behind the xterm instance in `TerminalNode` — extracted so they can be tested
+ * without an xterm/DOM harness (vitest runs in the node environment; there is no jsdom).
+ *
+ * Everything here belongs to co-attach: one pty, N subscribers. The pty runs at the SMALLEST
+ * subscriber's grid, so a terminal no longer owns its own size — it REPORTS what it could render
+ * and RENDERS what the pty broadcasts back.
+ */
+
+/** A terminal grid. `null` cols/rows on the wire means "subscribed, but not viewing" (parked). */
+export interface TermSize {
+ cols: number
+ rows: number
+}
+
+/**
+ * The size a terminal REPORTS to the pty. Under co-attach the renderer proposes what it could
+ * render (`FitAddon.proposeDimensions()`); the pty then broadcasts the min over all subscribers.
+ * `null` means "unmeasurable right now" (a collapsed / zero-size node) — report NOTHING rather
+ * than a bogus 0×0, which would clamp every other viewer's terminal to nothing.
+ */
+export function reportedSize(proposed: Partial<TermSize> | undefined | null): TermSize | null {
+ if (!proposed) return null
+ const { cols, rows } = proposed
+ if (typeof cols !== 'number' || typeof rows !== 'number') return null
+ if (!Number.isFinite(cols) || !Number.isFinite(rows)) return null
+ return { cols: Math.max(1, Math.floor(cols)), rows: Math.max(1, Math.floor(rows)) }
+}
+
+/**
+ * Are we rendering someone else's (smaller) grid? True when the authoritative size the pty
+ * broadcast is smaller than what this node could fit — then the leftover space is letterboxed.
+ * A solo user is always the min of a one-element set, so this is ALWAYS false for them and the
+ * letterbox styling never engages: their terminal looks exactly as it did before co-attach.
+ */
+export function isLetterboxed(effective: TermSize, fitted: TermSize | null): boolean {
+ if (!fitted) return false
+ return effective.cols < fitted.cols || effective.rows < fitted.rows
+}
+
+/**
+ * Should a `pty:resync` payload be painted? The server promises never to send an empty capture,
+ * but the renderer guards anyway: a resync RESETS the emulator, and a screen reset on an empty
+ * payload is unrecoverable (the user loses the screen for nothing), while a skipped repaint is
+ * not (the next byte of output redraws through tmux anyway).
+ */
+export function shouldApplyResync(screen: string | null | undefined): boolean {
+ return typeof screen === 'string' && screen.length > 0
+}
+
+/**
+ * Make text captured from tmux (`capture-pane`, LF-separated lines) safe to `term.write()`.
+ * xterm runs with `convertEol: false` — a bare LF moves down but keeps the column, so raw capture
+ * output would render as a staircase. Lone LFs become CRLF; existing CRLFs are left alone.
+ */
+export function toXtermText(text: string): string {
+ return text.replace(/\r?\n/g, '\r\n')
+}
+
+/**
+ * Who closed this node, for the "closed by …" overlay. `by` is null when the destroy was not
+ * attributed to a client (a local desktop destroy), and an id we have never seen is a peer who
+ * already left — both degrade to a neutral label rather than blocking the overlay on presence.
+ */
+export function closedByLabel(
+ by: ClientId | null,
+ peers: Record<ClientId, { name: string }>
+): string {
+ if (by === null) return 'another user'
+ return peers[by]?.name || 'another user'
+}
diff --git a/src/renderer/terminal/transport.ts b/src/renderer/terminal/transport.ts
index ef55adf7d..1b235346a 100644
--- a/src/renderer/terminal/transport.ts
+++ b/src/renderer/terminal/transport.ts
@@ -12,7 +12,12 @@ import type { ClientId } from '@shared/presence'
export interface TerminalTransport {
create(options: PtyCreateOptions): Promise<PtyCreateResult>
write(sessionId: string, data: string): void
- resize(sessionId: string, cols: number, rows: number): void
+ /**
+ * REPORT the size this client could render (not a command — the pty runs at the smallest
+ * subscriber's grid and answers over `onSize`). `null, null` means "subscribed, but not viewing"
+ * (a parked terminal): it leaves the size set, so a parked small window never clamps the others.
+ */
+ resize(sessionId: string, cols: number | null, rows: number | null): void
/** Flow control: pause (false) / resume (true) the source when the terminal is backed up. */
setFlow(sessionId: string, resume: boolean): void
/** Detaches the client; with tmux the underlying session survives. */
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 6f21def82..05bb99f54 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -203,8 +203,11 @@ export interface PtyApi {
create(options: PtyCreateOptions): Promise<PtyCreateResult>
/** Sends user input to the PTY. */
write(sessionId: string, data: string): void
- /** Updates the PTY when the terminal is resized. */
- resize(sessionId: string, cols: number, rows: number): void
+ /** Updates the PTY when the terminal is resized. The pty runs at the SMALLEST subscriber's grid,
+ * so this is a REPORT, not a command — the effective size comes back over `onSize`.
+ * `cols`/`rows` null means "subscribed, but not viewing" (a parked terminal): the client leaves
+ * the size set entirely, so a parked small window can't shrink everyone else's terminal. */
+ resize(sessionId: string, cols: number | null, rows: number | null): void
/** Flow control: pause (false) or resume (true) reading the PTY when xterm is backed up. */
setFlow(sessionId: string, resume: boolean): void
/** Detaches/terminates the PTY client (the underlying tmux session survives). */