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(server): cast isolates throwing listeners; one listener registry (Electron order)

enes1d45c18189c2
diff --git a/src/server/platform-server.test.ts b/src/server/platform-server.test.ts
index fce261c57..44937ecb0 100644
--- a/src/server/platform-server.test.ts
+++ b/src/server/platform-server.test.ts
@@ -1,4 +1,4 @@
-import { describe, it, expect } from 'vitest'
+import { describe, it, expect, vi } from 'vitest'
import { ServerPlatform, type UiSink } from './platform-server'
import { E_NO_HANDLER } from '../shared/rpc'
import { decodePtyData } from '../shared/rpc'
@@ -73,6 +73,32 @@ describe('ServerPlatform', () => {
expect(hits).toEqual(['A:x', 'B:x'])
})
+ it('cast isolates a throwing listener: the others on the channel still run', () => {
+ const p = new ServerPlatform({ userDataDir: '/tmp', appVersion: '0' })
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ const hits: string[] = []
+ p.onWithSender('pty:write', () => { throw new Error('attribution blew up') })
+ p.onWithSender('pty:write', (_sender: number, a: string) => hits.push(`S:${a}`))
+ p.on('pty:write', () => { throw new Error('plain blew up') })
+ p.on('pty:write', (a: string) => hits.push(`P:${a}`))
+ const ui = p.attach({ sendText: () => {}, sendBinary: () => {} })
+ expect(() => p.cast(ui, 'pty:write', ['keystroke'])).not.toThrow()
+ expect(hits).toEqual(['S:keystroke', 'P:keystroke'])
+ expect(warn).toHaveBeenCalledTimes(2)
+ warn.mockRestore()
+ })
+
+ it('cast fires listeners in registration order across on() and onWithSender()', () => {
+ const p = new ServerPlatform({ userDataDir: '/tmp', appVersion: '0' })
+ const order: string[] = []
+ p.on('c', () => order.push('plain-1'))
+ p.onWithSender('c', () => order.push('sender-1'))
+ p.on('c', () => order.push('plain-2'))
+ const ui = p.attach({ sendText: () => {}, sendBinary: () => {} })
+ p.cast(ui, 'c', [])
+ expect(order).toEqual(['plain-1', 'sender-1', 'plain-2'])
+ })
+
it('sendTo routes pty:data as binary, other channels as JSON events, drops when detached', () => {
const p = new ServerPlatform({ userDataDir: '/tmp/x', appVersion: '1.0.0' })
const a = fakeSink()
diff --git a/src/server/platform-server.ts b/src/server/platform-server.ts
index da6f44203..9b8a7b3a6 100644
--- a/src/server/platform-server.ts
+++ b/src/server/platform-server.ts
@@ -21,6 +21,7 @@ const WS_HIGH_WATER = 1_000_000
const WS_LOW_WATER = 256_000
type Handler = { fn: (...args: any[]) => unknown; withSender: boolean }
+type Listener = { fn: (...args: any[]) => void; withSender: boolean }
/** The Linux-server CorePlatform: RPC registries + a WS-connection (UiSink) registry.
* One instance per server process; each authenticated WebSocket attaches as one UI. */
@@ -30,8 +31,9 @@ export class ServerPlatform implements CorePlatform {
readonly isPackaged = true
private handlers = new Map<string, Handler>()
- private listeners = new Map<string, Set<(...args: any[]) => void>>()
- private senderListeners = new Map<string, Set<(senderId: number, ...args: any[]) => void>>()
+ // One registry for both on() and onWithSender(), so cast fires listeners in REGISTRATION
+ // order across the two — same as Electron's ipcMain.on. A Set preserves insertion order.
+ private listeners = new Map<string, Set<Listener>>()
private sinks = new Map<number, UiSink>()
private nextUiId = 1
@@ -58,21 +60,20 @@ export class ServerPlatform implements CorePlatform {
}
on(channel: string, fn: (...args: any[]) => void): void {
- let set = this.listeners.get(channel)
- if (!set) {
- set = new Set()
- this.listeners.set(channel, set)
- }
- set.add(fn)
+ this.addListener(channel, { fn, withSender: false })
}
onWithSender(channel: string, fn: (senderId: number, ...args: any[]) => void): void {
- let set = this.senderListeners.get(channel)
+ this.addListener(channel, { fn, withSender: true })
+ }
+
+ private addListener(channel: string, listener: Listener): void {
+ let set = this.listeners.get(channel)
if (!set) {
set = new Set()
- this.senderListeners.set(channel, set)
+ this.listeners.set(channel, set)
}
- set.add(fn)
+ set.add(listener)
}
sendTo(uiId: number, channel: string, ...args: any[]): void {
@@ -144,10 +145,21 @@ export class ServerPlatform implements CorePlatform {
}
cast(uiId: number, method: string, args: unknown[]): void {
- const withSender = this.senderListeners.get(method)
- if (withSender) for (const fn of withSender) fn(uiId, ...args)
const set = this.listeners.get(method)
if (!set) return
- for (const fn of set) fn(...args)
+ for (const l of set) {
+ // A cast has no reply channel (unlike dispatch, which returns E_HANDLER), so isolate each
+ // listener: one throw must not skip the rest — e.g. a broken pty:write attribution listener
+ // would otherwise swallow the user's keystrokes. Log it, keep going.
+ try {
+ if (l.withSender) l.fn(uiId, ...args)
+ else l.fn(...args)
+ } catch (err) {
+ console.warn(
+ `[nodeterm-server] cast listener for ${method} threw`,
+ err instanceof Error ? err.message : String(err)
+ )
+ }
+ }
}
}