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(watch-link): a viewer receives watch:* and its pty:size, nothing else

review0083fefcfd8d
diff --git a/src/core/watch-link/watcher-policy.test.ts b/src/core/watch-link/watcher-policy.test.ts
index 732184c36..a8b737573 100644
--- a/src/core/watch-link/watcher-policy.test.ts
+++ b/src/core/watch-link/watcher-policy.test.ts
@@ -9,6 +9,9 @@ import type { UiSink } from '../ui-sink-registry'
const channelValues = (): string[] =>
(Object.values(IPC) as unknown[]).filter((v): v is string => typeof v === 'string')
+/** Every per-id channel factory in IPC (`pty:data:<sid>`, `pty:size:<sid>`, …). */
+const channelFactories = (): Array<(id: string) => string> =>
+ (Object.values(IPC) as unknown[]).filter((v): v is (id: string) => string => typeof v === 'function')
describe('watcherAccess', () => {
it('refuses every IPC channel as a request and as a cast, for both roles', () => {
@@ -27,16 +30,29 @@ describe('watcherAccess', () => {
})
describe('watcherEventAllowed', () => {
- it('passes watch:* and this session\'s pty lifecycle events only', () => {
+ it('passes watch:* and this session\'s pty:size only', () => {
expect(watcherEventAllowed(WATCH_EVENT.meta, null)).toBe(true)
expect(watcherEventAllowed(IPC.ptySize('s1'), 's1')).toBe(true)
- expect(watcherEventAllowed(IPC.ptyResync('s1'), 's1')).toBe(true)
expect(watcherEventAllowed(IPC.ptySize('s2'), 's1')).toBe(false)
+ expect(watcherEventAllowed(IPC.ptySize('s11'), 's1')).toBe(false)
expect(watcherEventAllowed(IPC.ptySize('s1'), null)).toBe(false)
})
+ it('refuses this session\'s lifecycle and resync events (consumed, or history)', () => {
+ for (const ch of [IPC.ptyExit('s1'), IPC.ptyClosed('s1'), IPC.ptyRecycled('s1'), IPC.ptyResync('s1')]) {
+ expect(watcherEventAllowed(ch, 's1')).toBe(false)
+ }
+ })
it('refuses every broadcast channel in IPC', () => {
for (const ch of channelValues()) expect(watcherEventAllowed(ch, 's1')).toBe(false)
})
+ it('passes exactly one per-session channel in IPC: pty:size of this session', () => {
+ const factories = channelFactories()
+ expect(factories.length).toBeGreaterThan(5)
+ for (const f of factories) {
+ expect(watcherEventAllowed(f('s1'), 's1')).toBe(f === IPC.ptySize)
+ expect(watcherEventAllowed(f('s2'), 's1')).toBe(false)
+ }
+ })
})
function harness(
@@ -64,6 +80,14 @@ function harness(
const ev = (channel: string, ...args: unknown[]) => JSON.stringify({ t: 'ev', channel, args })
describe('wrapWatcherSink', () => {
+ it('forwards watch:* events and this session\'s pty:size', () => {
+ const h = harness()
+ const meta = ev(WATCH_EVENT.meta, { v: 1 })
+ const size = ev(IPC.ptySize('s1'), 80, 24)
+ h.sink.sendText(meta)
+ h.sink.sendText(size)
+ expect(h.text).toEqual([meta, size])
+ })
it('drops broadcast events and another session\'s data', () => {
const h = harness()
h.sink.sendText(ev('canvas:mut', 'p1', {}))
@@ -79,11 +103,29 @@ describe('wrapWatcherSink', () => {
h.sink.sendBinary(encodePtyData('s1', 'cmV0\x07visible'))
expect(h.bin).toEqual(['visible'])
})
- it('reports lifecycle events for its session', () => {
+ it('never lets another session\'s bytes move this viewer\'s parser', () => {
+ const h = harness()
+ h.sink.sendBinary(encodePtyData('s2', '\x1b]52;c;'))
+ h.sink.sendBinary(encodePtyData('s1', 'visible'))
+ expect(h.bin).toEqual(['visible'])
+ })
+ it('reports its session\'s lifecycle events and forwards none of them', () => {
const h = harness()
h.sink.sendText(ev(IPC.ptyExit('s1'), 0))
+ // `{by}` names another client: never for a viewer's eyes.
+ h.sink.sendText(ev(IPC.ptyClosed('s1'), { by: 3 }))
h.sink.sendText(ev(IPC.ptyRecycled('s1'), { ready: true }))
- expect(h.calls.life).toEqual(['exit', 'recycled'])
+ expect(h.calls.life).toEqual(['exit', 'closed', 'recycled'])
+ expect(h.text).toEqual([])
+ })
+ it('ignores another session\'s lifecycle events and refuses a resync', () => {
+ const h = harness()
+ h.sink.sendText(ev(IPC.ptyExit('s2'), 0))
+ h.sink.sendText(ev(IPC.ptyClosed('s2'), { by: 3 }))
+ h.sink.sendText(ev(IPC.ptyRecycled('s2'), { ready: true }))
+ h.sink.sendText(ev(IPC.ptyResync('s1'), 'HISTORY \x1b]8;;https://x\x07link\x1b]8;;\x07'))
+ expect(h.calls.life).toEqual([])
+ expect(h.text).toEqual([])
})
it('goes over budget when the socket backs up or the bucket is empty', () => {
const backed = harness({ buffered: WATCHER_BUFFER_LIMIT + 1 })
diff --git a/src/core/watch-link/watcher-policy.ts b/src/core/watch-link/watcher-policy.ts
index 0db7f170c..0707cd9e9 100644
--- a/src/core/watch-link/watcher-policy.ts
+++ b/src/core/watch-link/watcher-policy.ts
@@ -2,10 +2,19 @@
// access-policy.ts's `decideAccess`: that function evaluates any non-editor role against the whole
// VIEW table (files, git, presence, board log), and a live link may reach none of it.
// - Inbound: every request and cast is refused, except a Commenter link's chat cast.
-// - Outbound: only `watch:*` events and this viewer's own pty session; every broadcast is dropped
-// (a second layer: the watcher is also a QUIET client, absent from broadcast() and clientIds()).
+// - Outbound, deny by default too: `watch:*` events, this viewer's own pty data frames (filtered,
+// paced) and its session's `pty:size`, nothing else. Every broadcast is dropped (a second layer:
+// the watcher is also a QUIET client, absent from broadcast() and clientIds()).
+// - `pty:exit` / `pty:closed` / `pty:recycled` are CONSUMED (`onLifecycle`, which answers with
+// `watch:waiting`) and never forwarded: `pty:closed` carries `{by: ClientId}`, which identifies
+// another client of this core.
+// - `pty:resync` is refused. Its payload is the registry's default capture, which is history on
+// the SSH and session-host paths and carries OSC 8 verbatim; a watcher repaints only through
+// `watch:keyframe` (a visible-only capture passed through a fresh filter). The registry never
+// resyncs a self-paced client anyway; this is the second layer.
+// The viewer page reads `pty:size` and nothing else among pty events.
import { IPC } from '../../shared/ipc'
-import { decodePtyData, encodePtyData } from '../../shared/rpc'
+import { decodePtyData, decodePtyDataSessionId, encodePtyData } from '../../shared/rpc'
import { WATCH_CHAT_CAST, WATCH_EVENT_PREFIX, type WatchLinkRole } from '../../shared/watch-link/protocol'
import type { AccessDecision } from '../relay/relay-host'
import type { UiSink } from '../ui-sink-registry'
@@ -21,20 +30,26 @@ export function watcherAccess(kind: 'req' | 'cast', method: string, role: WatchL
return { allow: false, message: WATCHER_REFUSAL }
}
+/** The events a viewer may receive: `watch:*`, and its own session's `pty:size`. Nothing else. */
export function watcherEventAllowed(channel: string, sessionId: string | null): boolean {
if (channel.startsWith(WATCH_EVENT_PREFIX)) return true
- if (!sessionId) return false
- return (
- channel === IPC.ptySize(sessionId) ||
- channel === IPC.ptyExit(sessionId) ||
- channel === IPC.ptyClosed(sessionId) ||
- channel === IPC.ptyRecycled(sessionId) ||
- channel === IPC.ptyResync(sessionId)
- )
+ return !!sessionId && channel === IPC.ptySize(sessionId)
}
export type PtyLifecycle = 'exit' | 'closed' | 'recycled'
+function lifecycleOf(channel: string, sessionId: string): PtyLifecycle | null {
+ if (channel === IPC.ptyExit(sessionId)) return 'exit'
+ if (channel === IPC.ptyClosed(sessionId)) return 'closed'
+ if (channel === IPC.ptyRecycled(sessionId)) return 'recycled'
+ return null
+}
+
+/**
+ * What the link host supplies. A throw from `onLifecycle`, `onOverBudget` or `filter.push` is not
+ * caught here: it propagates into `UiSinkRegistry.deliver` as a sink strike, and two in a row evict
+ * the watcher (the session is torn down). That fails closed, which is the intent.
+ */
export interface WatcherSinkDeps {
sessionId(): string | null
/** False until the keyframe for the current session was sent, and while throttled. */
@@ -43,7 +58,14 @@ export interface WatcherSinkDeps {
filter: StreamFilter
/** Counts encoded BYTES (the frame as it goes on the wire), not characters. */
bucket: TokenBucket
+ /**
+ * A frame was dropped (socket backed up, or the bucket refused it). MUST make `streaming()` false
+ * before it returns, until a keyframe repaints: otherwise the next frame that fits the bucket is
+ * forwarded after the dropped one with no keyframe between. The filter stays in step either way
+ * (it saw the dropped bytes), but the viewer's screen is wrong until the next repaint.
+ */
onOverBudget(): void
+ /** This session exited, was closed or recycled. The event itself is never forwarded. */
onLifecycle(kind: PtyLifecycle): void
}
@@ -60,17 +82,23 @@ export function wrapWatcherSink(base: UiSink, d: WatcherSinkDeps): UiSink {
return {
sendText: (json) => {
const channel = channelOf(json)
+ if (channel === null) return
const sid = d.sessionId()
- if (channel === null || !watcherEventAllowed(channel, sid)) return
- if (sid && channel === IPC.ptyExit(sid)) d.onLifecycle('exit')
- else if (sid && channel === IPC.ptyClosed(sid)) d.onLifecycle('closed')
- else if (sid && channel === IPC.ptyRecycled(sid)) d.onLifecycle('recycled')
+ const life = sid ? lifecycleOf(channel, sid) : null
+ if (life) {
+ d.onLifecycle(life)
+ return
+ }
+ if (!watcherEventAllowed(channel, sid)) return
base.sendText(json)
},
sendBinary: (buf) => {
- const frame = decodePtyData(buf)
const sid = d.sessionId()
- if (!frame || !sid || frame.sessionId !== sid) return
+ // The header alone decides: another session's payload is never decoded, and its bytes never
+ // reach this viewer's parser.
+ if (!sid || decodePtyDataSessionId(buf) !== sid) return
+ const frame = decodePtyData(buf)
+ if (!frame) return
// Always parse, even when nothing is forwarded (see stream-filter.ts).
const text = d.filter.push(frame.data)
if (!d.streaming() || !text) return