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(transport): expose pty:size, pty:closed and pty:resync to the renderer (desktop + browser)

enesb7cc8af823c2
diff --git a/src/preload/index.ts b/src/preload/index.ts
index 0f9706d0d..3066c25b1 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -10,7 +10,7 @@ import type {
UpdateProgress,
Workspace
} from '../shared/types'
-import type { PeerDiff, PeerIdentity, PeerState } from '../shared/presence'
+import type { ClientId, PeerDiff, PeerIdentity, PeerState } from '../shared/presence'
// Fan a single ipcRenderer listener per channel out to many renderer subscribers. Without
// this, every node that subscribes (e.g. Cmd+M markdown toggle on each terminal/editor) adds
@@ -68,6 +68,24 @@ const api: NodeTerminalApi = {
const handler = (_e: unknown, code: number) => listener(code)
ipcRenderer.on(channel, handler)
return () => ipcRenderer.removeListener(channel, handler)
+ },
+ onSize: (sessionId, listener) => {
+ const channel = IPC.ptySize(sessionId)
+ const handler = (_e: unknown, size: { cols: number; rows: number }) => listener(size)
+ ipcRenderer.on(channel, handler)
+ return () => ipcRenderer.removeListener(channel, handler)
+ },
+ onClosed: (sessionId, listener) => {
+ const channel = IPC.ptyClosed(sessionId)
+ const handler = (_e: unknown, info: { by: ClientId | null }) => listener(info)
+ ipcRenderer.on(channel, handler)
+ return () => ipcRenderer.removeListener(channel, handler)
+ },
+ onResync: (sessionId, listener) => {
+ const channel = IPC.ptyResync(sessionId)
+ const handler = (_e: unknown, screen: string) => listener(screen)
+ ipcRenderer.on(channel, handler)
+ return () => ipcRenderer.removeListener(channel, handler)
}
},
workspace: {
diff --git a/src/renderer/bridge/ws-bridge.ts b/src/renderer/bridge/ws-bridge.ts
index 4b949baca..e367427ed 100644
--- a/src/renderer/bridge/ws-bridge.ts
+++ b/src/renderer/bridge/ws-bridge.ts
@@ -178,7 +178,14 @@ function buildRealApi(client: RpcClient): Pick<NodeTerminalApi, 'pty' | 'workspa
onData: (sessionId, listener) =>
client.subscribe(IPC.ptyData(sessionId), listener as Listener),
onExit: (sessionId, listener) =>
- client.subscribe(IPC.ptyExit(sessionId), listener as Listener)
+ client.subscribe(IPC.ptyExit(sessionId), listener as Listener),
+ // Co-attach channels: ordinary JSON `ev` frames (only pty:data is binary), so the frame
+ // decoder is unchanged — they just fan out through the generic channel subscription.
+ onSize: (sessionId, listener) => client.subscribe(IPC.ptySize(sessionId), listener as Listener),
+ onClosed: (sessionId, listener) =>
+ client.subscribe(IPC.ptyClosed(sessionId), listener as Listener),
+ onResync: (sessionId, listener) =>
+ client.subscribe(IPC.ptyResync(sessionId), listener as Listener)
}
const workspace: WorkspaceApi = {
diff --git a/src/renderer/terminal/local-transport.test.ts b/src/renderer/terminal/local-transport.test.ts
new file mode 100644
index 000000000..e9ea1ecb2
--- /dev/null
+++ b/src/renderer/terminal/local-transport.test.ts
@@ -0,0 +1,34 @@
+import { describe, it, expect, vi } from 'vitest'
+import { LocalTransport } from './local-transport'
+
+describe('LocalTransport co-attach members', () => {
+ it('subscribes to the authoritative size, the closed-by-peer event and the redraw', () => {
+ const unsubSize = vi.fn()
+ const unsubClosed = vi.fn()
+ const unsubResync = vi.fn()
+ const onSize = vi.fn(() => unsubSize)
+ const onClosed = vi.fn(() => unsubClosed)
+ const onResync = vi.fn(() => unsubResync)
+ // @ts-expect-error — minimal window shim for the three members under test
+ globalThis.window = { nodeTerminal: { pty: { onSize, onClosed, onResync } } }
+ const t = new LocalTransport()
+ const sizeCb = (): void => {}
+ const closedCb = (): void => {}
+ const resyncCb = (): void => {}
+ const offSize = t.onSize?.('pty-1', sizeCb)
+ const offClosed = t.onClosed?.('pty-1', closedCb)
+ const offResync = t.onResync?.('pty-1', resyncCb)
+ expect(onSize).toHaveBeenCalledWith('pty-1', sizeCb)
+ expect(onClosed).toHaveBeenCalledWith('pty-1', closedCb)
+ expect(onResync).toHaveBeenCalledWith('pty-1', resyncCb)
+
+ // Every subscription must hand back a working unsubscribe: a terminal node mounts and
+ // unmounts constantly (project switches), so a dropped unsubscribe is a leak per mount.
+ offSize?.()
+ offClosed?.()
+ offResync?.()
+ expect(unsubSize).toHaveBeenCalledTimes(1)
+ expect(unsubClosed).toHaveBeenCalledTimes(1)
+ expect(unsubResync).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/src/renderer/terminal/local-transport.ts b/src/renderer/terminal/local-transport.ts
index 8f1dfe105..c83ecfb96 100644
--- a/src/renderer/terminal/local-transport.ts
+++ b/src/renderer/terminal/local-transport.ts
@@ -1,4 +1,5 @@
import type { PtyCreateOptions, PtyCreateResult } from '@shared/types'
+import type { ClientId } from '@shared/presence'
import type { TerminalTransport } from './transport'
/**
@@ -42,6 +43,18 @@ export class LocalTransport implements TerminalTransport {
onExit(sessionId: string, listener: (exitCode: number) => void): () => void {
return this.pty.onExit(sessionId, listener)
}
+
+ onSize(sessionId: string, listener: (size: { cols: number; rows: number }) => void): () => void {
+ return this.pty.onSize(sessionId, listener)
+ }
+
+ onClosed(sessionId: string, listener: (info: { by: ClientId | null }) => void): () => void {
+ return this.pty.onClosed(sessionId, listener)
+ }
+
+ onResync(sessionId: string, listener: (screen: string) => void): () => void {
+ return this.pty.onResync(sessionId, listener)
+ }
}
/** The single transport instance used by the app. Becomes selectable later. */
diff --git a/src/renderer/terminal/remote-transport.ts b/src/renderer/terminal/remote-transport.ts
index 4087a783d..cb337aabe 100644
--- a/src/renderer/terminal/remote-transport.ts
+++ b/src/renderer/terminal/remote-transport.ts
@@ -12,6 +12,13 @@ import type { TerminalTransport } from './transport'
* detaches the client stream via `kill` — the host-side tmux session survives, exactly like
* LocalTransport's kill/destroy split, but the client never owns the lifecycle of the host's
* persistent session.
+ *
+ * The co-attach members (`onSize` / `onClosed` / `onResync`) are deliberately NOT implemented:
+ * the relay frame protocol carries no size, closed or resync frame, and a relay client is a
+ * mirror of the host's own terminal rather than an equal subscriber of the pty. Omitting the
+ * optional members makes TerminalNode fall back to today's behavior for relay-backed nodes
+ * (local FitAddon sizing, no closed overlay, no server-driven repaint) instead of silently
+ * doing nothing behind an implemented-looking method. See CLAUDE.md, "Three surfaces".
*/
export class RemoteTransport implements TerminalTransport {
constructor(private readonly connectionId: string) {}
diff --git a/src/renderer/terminal/transport.ts b/src/renderer/terminal/transport.ts
index e3409f0a5..ef55adf7d 100644
--- a/src/renderer/terminal/transport.ts
+++ b/src/renderer/terminal/transport.ts
@@ -1,4 +1,5 @@
import type { PtyCreateOptions, PtyCreateResult } from '@shared/types'
+import type { ClientId } from '@shared/presence'
/**
* Abstraction over the terminal session layer.
@@ -22,4 +23,31 @@ export interface TerminalTransport {
onData(sessionId: string, listener: (data: string) => void): () => void
/** Fires when the session closes; returns an unsubscribe function. */
onExit(sessionId: string, listener: (exitCode: number) => void): () => void
+ /**
+ * The authoritative size of a co-attached session: min(cols) × min(rows) over all subscribers
+ * ("smallest subscriber wins"). The terminal must render at this size (letterboxing the slack)
+ * instead of its own FitAddon result, or the two viewers' screens diverge.
+ * OPTIONAL: a transport that cannot negotiate a shared size (RemoteTransport over the relay,
+ * whose frame protocol has no size frame) simply omits it, and the terminal falls back to
+ * driving its own FitAddon — today's behavior. Returns an unsubscribe function.
+ */
+ onSize?(sessionId: string, listener: (size: { cols: number; rows: number }) => void): () => void
+ /**
+ * Another client permanently DESTROYED this node while we were co-viewing it: the session is
+ * gone for good, so the terminal shows a "closed by <peer>" state instead of respawning it.
+ * `by` is the destroying client's ClientId (null when no client was attributed — e.g. a local
+ * destroy on the desktop); map it to a peer name via the presence store.
+ * OPTIONAL for the same reason as onSize. Returns an unsubscribe function.
+ */
+ onClosed?(sessionId: string, listener: (info: { by: ClientId | null }) => void): () => void
+ /**
+ * This client fell so far behind that the server discarded its queued output (WS_DROP_WATER)
+ * and is redrawing it from tmux: clear the emulator and write this capture — it is the CURRENT
+ * screen.
+ * CONTRACT: the server only ever emits a NON-EMPTY capture (a failed/empty capture is retried,
+ * never sent). The listener must STILL ignore an empty/falsy payload — no reset, no separator:
+ * a wrongly reset screen is unrecoverable, a skipped repaint is not.
+ * OPTIONAL for the same reason as onSize. Returns an unsubscribe function.
+ */
+ onResync?(sessionId: string, listener: (screen: string) => void): () => void
}
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 8a9211d7b..6f21def82 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -230,6 +230,21 @@ export interface PtyApi {
onData(sessionId: string, listener: (data: string) => void): () => void
/** Fires when the PTY process exits. Returns an unsubscribe function. */
onExit(sessionId: string, listener: (exitCode: number) => void): () => void
+ /** The authoritative size of a co-attached session: min(cols) × min(rows) over all subscribers
+ * ("smallest subscriber wins"). Broadcast whenever the subscriber set or any reported size
+ * changes; the terminal renders at this size instead of its own fit. Returns an unsubscribe. */
+ onSize(sessionId: string, listener: (size: { cols: number; rows: number }) => void): () => void
+ /** Another client permanently destroyed this node while we were co-viewing it: the session is
+ * gone for good (do not respawn — show a "closed by <peer>" state). `by` is the destroying
+ * client's ClientId, or null when the destroy was not attributed to a client (a local desktop
+ * destroy); resolve it to a name via the presence store. Returns an unsubscribe. */
+ onClosed(sessionId: string, listener: (info: { by: ClientId | null }) => void): () => void
+ /** We fell too far behind and the server dropped our queued output; this is the session's
+ * CURRENT screen captured from tmux. Reset the emulator and repaint from it.
+ * CONTRACT: the payload is guaranteed NON-EMPTY (a failed capture is retried, never sent). The
+ * listener must STILL ignore an empty/falsy payload — never reset on one: a wrongly cleared
+ * screen is unrecoverable, a skipped repaint is not. Returns an unsubscribe. */
+ onResync(sessionId: string, listener: (screen: string) => void): () => void
}
export interface WorkspaceApi {