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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
feat(watch-link): a mid-stream filter says when it has settled
review436a78807fb6
diff --git a/src/core/watch-link/stream-filter.test.ts b/src/core/watch-link/stream-filter.test.tsindex 28cf1dbfe..6a0ea6a01 100644--- a/src/core/watch-link/stream-filter.test.ts+++ b/src/core/watch-link/stream-filter.test.ts@@ -1,4 +1,4 @@-import { describe, it, expect } from 'vitest'+import { describe, it, expect, vi } from 'vitest'import { readFileSync } from 'node:fs'import { join } from 'node:path'import { createStreamFilter } from './stream-filter'@@ -315,6 +315,86 @@ describe('createStreamFilter, joined mid-stream', () => {expect(g.push(`x${BEL}y${ESC}[mz`)).toBe(`${ESC}[mz`)})+ // Controller ruling R23: the link host takes a follow-up keyframe once a joined filter has left its+ // unknown start state (the text it swallowed until then is on the owner's screen, not the viewer's).+ describe('onSettled', () => {+ const settledBy = (input: string): number => {+ let n = 0+ createStreamFilter({ midStream: true, onSettled: () => n++ }).push(input)+ return n+ }++ it('fires once, when the unknown start state is first left: ESC, ST or an 8-bit introducer', () => {+ expect(settledBy(`x${ESC}[mtext`)).toBe(1)+ expect(settledBy(`x${ESC}\\text`)).toBe(1)+ expect(settledBy('x\x9ctext')).toBe(1)+ expect(settledBy(`x\x9d52;c;x${BEL}text`)).toBe(1)+ // BEL does not end the unknown string (it may be a DCS or APC), so it settles nothing.+ expect(settledBy(`x${BEL}more`)).toBe(0)+ expect(settledBy('plain text, no escape')).toBe(0)+ })++ it('fires once only, however many escapes follow, across pushes', () => {+ let n = 0+ const f = createStreamFilter({ midStream: true, onSettled: () => n++ })+ expect(f.push('swallowed ')).toBe('')+ expect(n).toBe(0)+ expect(f.push(`${ESC}[1mA${ESC}[m ${ESC}]0;t${BEL}B`)).toBe(`${ESC}[1mA${ESC}[m B`)+ expect(n).toBe(1)+ // Later strings end at later stop chars; none of them is the start state any more.+ expect(f.push(`${ESC}[2J${ESC}]0;x${BEL}${ESC}P1$r${ESC}\\${ESC}[H`)).toBe(`${ESC}[2J${ESC}[H`)+ expect(n).toBe(1)+ })++ it('fires after the chunk is parsed: the push still returns everything after the escape', () => {+ const seen: string[] = []+ const f = createStreamFilter({ midStream: true, onSettled: () => seen.push('settled') })+ expect(f.push(`lost${ESC}[Hkept`)).toBe(`${ESC}[Hkept`)+ expect(seen).toEqual(['settled'])+ })++ it('never fires for a filter that starts in text', () => {+ let n = 0+ const f = createStreamFilter({ onSettled: () => n++ })+ f.push(`a${ESC}]52;c;x${BEL}b${ESC}[m`)+ expect(n).toBe(0)+ })++ it('reset({ midStream, onSettled }) re-arms it; reset() and a midStream reset without it disarm it', () => {+ let a = 0+ let b = 0+ const f = createStreamFilter({ midStream: true, onSettled: () => a++ })+ f.push(`${ESC}[m`)+ f.reset({ midStream: true, onSettled: () => b++ })+ f.push(`${ESC}[m`)+ expect([a, b]).toEqual([1, 1])+ // Each disarmed filter then reads a whole OSC, whose BEL is a stop char a stale callback would fire on.+ f.reset({ midStream: true, onSettled: () => b++ })+ f.reset({ midStream: true })+ f.push(`${ESC}[m${ESC}]0;t${BEL}${ESC}[m`)+ f.reset({ midStream: true, onSettled: () => b++ })+ f.reset()+ f.push(`${ESC}]0;t${BEL}${ESC}[m`)+ expect([a, b]).toEqual([1, 1])+ })++ it('a throwing callback costs nothing: the chunk is returned whole and the state is right', () => {+ const f = createStreamFilter({+ midStream: true,+ onSettled: () => {+ throw new Error('boom')+ }+ })+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})+ try {+ expect(f.push(`lost${ESC}[Hkept`)).toBe(`${ESC}[Hkept`)+ expect(f.push(`${ESC}]52;c;c2VjcmV0${BEL}after`)).toBe('after')+ } finally {+ warn.mockRestore()+ }+ })+ })+it('reset({ midStream }) drops what was being read and starts as a join', () => {const f = createStreamFilter()expect(f.push('a')).toBe('a')diff --git a/src/core/watch-link/stream-filter.ts b/src/core/watch-link/stream-filter.tsindex 781158cc6..45d9040dc 100644--- a/src/core/watch-link/stream-filter.ts+++ b/src/core/watch-link/stream-filter.ts@@ -75,6 +75,14 @@ export interface StreamFilterOptions {* string may be a DCS or APC, where BEL is data). Like every string, it has no length limit.*/midStream?: boolean+ /**+ * With `midStream`: called ONCE, when that unknown start state is first left (the first ESC, ST or+ * 8-bit introducer), after the chunk that left it has been parsed. From then on text flows again, so+ * the link host takes one more keyframe to repaint the text it swallowed before (controller ruling+ * R23). Ignored without `midStream`; replaced or dropped by every `reset`. A throw is logged, never+ * propagated: the parse it follows is complete, and the chunk's output must not be lost with it.+ */+ onSettled?: () => void}export interface StreamFilter {@@ -93,17 +101,30 @@ export function createStreamFilter(opts?: StreamFilterOptions): StreamFilter {// where `ESC \` is that string's ST and is dropped with it.let mode: 'text' | 'esc' | 'string' | 'stringEsc' = 'text'let osc = false+ // Armed while a midStream start has not been left: the start state is the only string state it is+ // set in, so the first stop char read while it is set is the one that leaves that state.+ let settle: (() => void) | null = nullconst enterString = (introducer: number): void => {mode = 'string'osc = introducer === 0x5d || introducer === 0x9d}const start = (o: StreamFilterOptions | undefined): void => {- if (o?.midStream) enterString(0x90)- else {+ settle = null+ if (o?.midStream) {+ enterString(0x90)+ settle = o.onSettled ?? null+ } else {mode = 'text'osc = false}}+ const fire = (cb: () => void): void => {+ try {+ cb()+ } catch (err) {+ console.warn(`[watch-link] onSettled threw: ${err instanceof Error ? err.message : String(err)}`)+ }+ }start(opts)return {reset(o) {@@ -117,6 +138,7 @@ export function createStreamFilter(opts?: StreamFilterOptions): StreamFilter {const n = chunk.lengthlet out = ''let i = 0+ let settled: (() => void) | null = nullwhile (i < n) {if (mode === 'text') {let j = chunk.indexOf(ESC, i)@@ -143,6 +165,10 @@ export function createStreamFilter(opts?: StreamFilterOptions): StreamFilter {if (m === null) breakconst c = chunk.charCodeAt(m.index)i = m.index + 1+ if (settle) {+ settled = settle+ settle = null+ }if (c === 0x1b) mode = 'stringEsc'else if (c === 0x9c || c === 0x07) mode = 'text'else enterString(c)@@ -162,6 +188,7 @@ export function createStreamFilter(opts?: StreamFilterOptions): StreamFilter {i++}}+ if (settled) fire(settled)return out}}