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(watch-link): strip string-type escape sequences from a viewer's stream; byte budget

reviewf06be31e9e24
diff --git a/src/core/watch-link/stream-filter.test.ts b/src/core/watch-link/stream-filter.test.ts
new file mode 100644
index 000000000..5f2d1eecc
--- /dev/null
+++ b/src/core/watch-link/stream-filter.test.ts
@@ -0,0 +1,224 @@
+import { describe, it, expect } from 'vitest'
+import { createStreamFilter } from './stream-filter'
+
+const ESC = '\x1b'
+const BEL = '\x07'
+const run = (chunks: string[], max?: number): string => {
+ const f = createStreamFilter(max)
+ return chunks.map((c) => f.push(c)).join('')
+}
+
+// The filter's rules, one UTF-16 unit at a time and without a fast path: the oracle the fuzz test
+// holds the production parser to.
+function oneCharAtATime(input: string, max: number): string {
+ let mode: 'text' | 'esc' | 'string' | 'stringEsc' = 'text'
+ let osc = false
+ let len = 0
+ let out = ''
+ const start = (ch: string): 'string' => {
+ osc = ch === ']' || ch === '\x9d'
+ len = 0
+ return 'string'
+ }
+ for (let i = 0; i < input.length; i++) {
+ const ch = input[i]
+ const n = ch.charCodeAt(0)
+ const intro8 = '\x90\x98\x9d\x9e\x9f'.includes(ch)
+ if (mode === 'text') {
+ if (ch === ESC) mode = 'esc'
+ else if (intro8) mode = start(ch)
+ else out += ch
+ } else if (mode === 'string') {
+ if (ch === '\x9c' || (ch === BEL && osc)) mode = 'text'
+ else if (ch === ESC) mode = 'stringEsc'
+ else if (intro8) mode = start(ch)
+ else if (++len > max) {
+ mode = 'text'
+ len = 0
+ }
+ } else if (']PX^_'.includes(ch) || intro8) mode = start(ch)
+ else if (ch === ESC) mode = 'esc'
+ else if (n <= 0x17 || n === 0x19 || (n >= 0x1c && n <= 0x1f)) out += ch
+ else if (n === 0x7f) continue
+ else if (ch === '\\' && mode === 'stringEsc') mode = 'text'
+ else {
+ out += ESC + ch
+ mode = 'text'
+ }
+ }
+ return out
+}
+
+describe('createStreamFilter', () => {
+ it('passes text, CSI and SGR untouched', () => {
+ const s = `plain ${ESC}[31mred${ESC}[0m ${ESC}[2J${ESC}[H\r\n${ESC}7${ESC}8 é🙂`
+ expect(run([s])).toBe(s)
+ })
+
+ it('removes OSC 52 terminated by BEL and by ST, and every string-type sequence', () => {
+ expect(run([`a${ESC}]52;c;c2VjcmV0\x07b`])).toBe('ab')
+ expect(run([`a${ESC}]0;title${ESC}\\b`])).toBe('ab')
+ expect(run([`a${ESC}Pq#0;2;0;0;0${ESC}\\b`])).toBe('ab')
+ expect(run([`a${ESC}_Gf=100;AAAA${ESC}\\b`])).toBe('ab')
+ expect(run([`a${ESC}^pm${ESC}\\b${ESC}Xsos${ESC}\\c`])).toBe('abc')
+ expect(run([`a\x9d52;c;x\x9cb\x90dcs\x9cc`])).toBe('abc')
+ })
+
+ it('handles a split at every position of an OSC 52', () => {
+ const s = `before${ESC}]52;c;c2VjcmV0${ESC}\\after${ESC}[1m!`
+ for (let i = 0; i <= s.length; i++) {
+ expect(run([s.slice(0, i), s.slice(i)])).toBe(`beforeafter${ESC}[1m!`)
+ }
+ })
+
+ it('an ESC inside a string aborts it and starts a new sequence', () => {
+ expect(run([`a${ESC}]52;c;xx${ESC}[31mred`])).toBe(`a${ESC}[31mred`)
+ })
+
+ it('keeps swallowing an unterminated string until the cap, then resumes text', () => {
+ const f = createStreamFilter(10)
+ expect(f.push(`a${ESC}]52;c;`)).toBe('a')
+ expect(f.push('12345')).toBe('')
+ // The 11th swallowed char ('6') trips the cap and is dropped with the rest; text resumes after it.
+ expect(f.push('67890XYZ')).toBe('7890XYZ')
+ })
+
+ it('reset forgets a half-read sequence', () => {
+ const f = createStreamFilter()
+ expect(f.push(`a${ESC}]52;c;`)).toBe('a')
+ f.reset()
+ expect(f.push('visible')).toBe('visible')
+ })
+
+ it('gives up only past 1 MiB by default: a string of exactly 1 MiB is still swallowed whole', () => {
+ const cap = 1_048_576
+ // `52;c;` counts too: the string's payload starts right after the introducer.
+ const payload = '52;c;' + 'A'.repeat(cap - 5)
+ expect(run([`x${ESC}]${payload}${BEL}y`])).toBe('xy')
+ // One more char trips the cap; it is dropped and text resumes after it.
+ expect(run([`x${ESC}]${payload}Ztail`])).toBe('xtail')
+ })
+
+ it('a keyframe screen (capture-pane -e) loses its OSC 8 links and keeps the link text', () => {
+ // tmux 3.4 `capture-pane -p -e` emits hyperlinks verbatim (measured), ST-terminated; programs
+ // may end them with BEL instead. A keyframe is one push of a whole screen through a FRESH filter.
+ const screen = [
+ `${ESC}[1m$ ${ESC}[0mls -l`,
+ `see ${ESC}]8;id=1;https://example.com/a${ESC}\\the docs${ESC}]8;;${ESC}\\ for more`,
+ `${ESC}]8;;file:///etc/passwd${ESC}\\passwd${ESC}]8;;${ESC}\\ and ${ESC}[4m${ESC}]8;;https://x.test/${BEL}x${ESC}]8;;${BEL}${ESC}[24m`,
+ ''
+ ].join('\n')
+ const out = createStreamFilter().push(screen)
+ expect(out).toBe(
+ [`${ESC}[1m$ ${ESC}[0mls -l`, 'see the docs for more', `passwd and ${ESC}[4mx${ESC}[24m`, ''].join('\n')
+ )
+ expect(out).not.toContain(']8;')
+ })
+
+ // Each case below is a way the viewer could otherwise RECEIVE bytes the owner's terminal treats as
+ // string payload. The reference is xterm.js 5.5's VT500 transition table
+ // (node_modules/@xterm/xterm/src/common/parser/EscapeSequenceParser.ts): it renders the owner's
+ // node, the session host's emulator and the viewer page alike.
+
+ it('BEL ends only an OSC: a DCS, SOS, PM or APC keeps swallowing until ST', () => {
+ // xterm.js treats BEL inside DCS as data and inside SOS/PM/APC as ignored, so the owner never
+ // sees what follows it; ending the string there would show the viewer hidden bytes.
+ expect(run([`a${ESC}Ptmux;x${BEL}hidden${ESC}\\b`])).toBe('ab')
+ expect(run([`a${ESC}_Gq${BEL}hidden${ESC}\\b`])).toBe('ab')
+ expect(run([`a${ESC}^pm${BEL}hidden${ESC}\\b${ESC}Xsos${BEL}hidden\x9cc`])).toBe('abc')
+ expect(run([`a\x90dcs${BEL}hidden\x9cb`])).toBe('ab')
+ expect(run([`a\x9eq${BEL}hidden\x9cb\x98s${BEL}h\x9cc\x9fa${BEL}h\x9cd`])).toBe('abcd')
+ })
+
+ it('an 8-bit introducer after ESC starts a string, as it does in xterm.js', () => {
+ expect(run([`a${ESC}\x9d52;c;c2VjcmV0\x9cb`])).toBe('ab')
+ expect(run([`a${ESC}]0;t${ESC}\x9d52;c;c2VjcmV0\x9cb`])).toBe('ab')
+ expect(run([`a${ESC}`, `\x90q#0\x9cb`])).toBe('ab')
+ })
+
+ it('a C0 control or DEL between ESC and the introducer does not end the escape', () => {
+ // xterm.js executes C0 and ignores DEL while in ESCAPE, so `ESC \n ]52;…` is still an OSC 52.
+ expect(run([`a${ESC}\n]52;c;c2VjcmV0${BEL}b`])).toBe('a\nb')
+ expect(run([`a${ESC}\x7f]52;c;c2VjcmV0${BEL}b`])).toBe('ab')
+ expect(run([`a${ESC}]0;t${ESC}\r\x7f]52;c;c2VjcmV0${BEL}b`])).toBe('a\rb')
+ // The C0 is still executed, and the ESC still applies to what follows it.
+ expect(run([`a${ESC}\r[1mB`])).toBe(`a\r${ESC}[1mB`)
+ })
+
+ it('never emits a lone ESC that the text after a removed string would complete', () => {
+ // `ESC ESC ]…` is ONE OSC in xterm.js (the second ESC restarts the escape); emitting the first
+ // ESC would glue it to the text after the removed string, here into an OSC 0 of its own.
+ expect(run([`${ESC}${ESC}]52;c;c2VjcmV0${BEL}]0;x${BEL}`])).toBe(`]0;x${BEL}`)
+ expect(run([`a${ESC}`, `${ESC}`, `]52;c;x${BEL}]b`])).toBe('a]b')
+ expect(run([`a${ESC}${ESC}[1mb`])).toBe(`a${ESC}[1mb`)
+ })
+
+ it('an 8-bit introducer inside a string starts a new string of its own kind', () => {
+ // OSC → DCS: the BEL is now DCS data, not a terminator.
+ expect(run([`a${ESC}]0;t\x90q${BEL}hidden\x9cb`])).toBe('ab')
+ // ...and it starts a fresh count toward the cap.
+ const f = createStreamFilter(10)
+ expect(f.push(`a${ESC}]` + '12345678')).toBe('a')
+ expect(f.push('\x9d' + '12345678')).toBe('')
+ expect(f.push(`${BEL}b`)).toBe('b')
+ })
+
+ it('reset also forgets a pending ESC', () => {
+ const f = createStreamFilter()
+ expect(f.push(`a${ESC}`)).toBe('a')
+ f.reset()
+ expect(f.push(']visible')).toBe(']visible')
+ })
+
+ it('output never carries a string introducer, whatever the input and however it is chunked', () => {
+ // Why this is enough: in xterm.js a string state (OSC/DCS/SOS/PM/APC) is entered only from
+ // ESCAPE on ] P X ^ _, or anywhere on an 8-bit introducer; ESCAPE is entered only by ESC and is
+ // left by any char except C0 executables, DEL and ESC. So if no push emits an 8-bit introducer
+ // or ends on ESC, and every ESC it emits is followed by a char that leaves ESCAPE without
+ // entering a string, the viewer's parser can never enter one — even when the caller drops some
+ // pushes (drop-and-redraw) or starts on a keyframe.
+ const ALPHABET = [
+ 'a', 'b', ' ', '\n', '\r', '\t', '\x00', ESC, ']', 'P', 'X', '^', '_', '[', '\\', 'm', '8', ';',
+ BEL, '\x18', '\x1a', '\x7f', '\x9c', '\x9d', '\x90', '\x98', '\x9e', '\x9f', '\x9b', '\x85', 'é', '🙂'
+ ]
+ const INTRO_8 = /[\x90\x98\x9d\x9e\x9f]/
+ const staysOrStringAfterEsc = (c: string): boolean => {
+ const n = c.charCodeAt(0)
+ const c0Executable = n <= 0x17 || n === 0x19 || (n >= 0x1c && n <= 0x1f)
+ return c0Executable || n === 0x7f || c === ESC || ']PX^_'.includes(c) || INTRO_8.test(c)
+ }
+ // mulberry32, seeded: reproducible, and without an LCG's short low-bit cycles.
+ let seed = 0x5eed
+ const rand = (n: number): number => {
+ seed = (seed + 0x6d2b79f5) >>> 0
+ let z = seed
+ z = Math.imul(z ^ (z >>> 15), z | 1)
+ z ^= z + Math.imul(z ^ (z >>> 7), z | 61)
+ return ((z ^ (z >>> 14)) >>> 0) % n
+ }
+ for (let iter = 0; iter < 4000; iter++) {
+ let input = ''
+ for (let i = rand(60); i > 0; i--) input += ALPHABET[rand(ALPHABET.length)]
+ const cap = [2, 5, 1_048_576][rand(3)]
+ // Cut anywhere, a surrogate pair included: the cap counts UTF-16 units, so nothing depends on it.
+ const cuts = [0, input.length]
+ for (let k = rand(5); k > 0; k--) cuts.push(rand(input.length + 1))
+ cuts.sort((x, y) => x - y)
+ const chunks = cuts.slice(1).map((end, i) => input.slice(cuts[i], end))
+
+ const f = createStreamFilter(cap)
+ const outs = chunks.map((c) => f.push(c))
+ for (const out of outs) {
+ const ctx = JSON.stringify({ input, chunks, cap, out })
+ expect(INTRO_8.test(out), ctx).toBe(false)
+ expect(out.endsWith(ESC), ctx).toBe(false)
+ for (let i = out.indexOf(ESC); i !== -1; i = out.indexOf(ESC, i + 1)) {
+ expect(staysOrStringAfterEsc(out[i + 1]), ctx).toBe(false)
+ }
+ }
+ // However it is chunked, the result is the char-by-char statement of the same rules: the
+ // parser's slicing and fast path neither drop, reorder nor add a char.
+ expect(outs.join(''), JSON.stringify({ chunks, cap })).toBe(oneCharAtATime(input, cap))
+ }
+ })
+})
diff --git a/src/core/watch-link/stream-filter.ts b/src/core/watch-link/stream-filter.ts
new file mode 100644
index 000000000..76d7da280
--- /dev/null
+++ b/src/core/watch-link/stream-filter.ts
@@ -0,0 +1,128 @@
+// Removes every STRING-type escape sequence (OSC, DCS, SOS, PM, APC; 7- and 8-bit introducers)
+// from a live link's pty stream. They carry what is not text on the screen: the clipboard (OSC 52,
+// which tmux emits on every copy with `set-clipboard on`), window titles, hyperlink targets, file
+// transfers, palette changes. Everything else (CSI, other ESC sequences, text) passes unchanged.
+//
+// Stateful across chunks, and it must see EVERY byte of the stream even while nothing is being
+// forwarded: a frame that skipped the parser would leave it mid-sequence and print the tail of an
+// OSC 52 (the clipboard) as text. That is also why it is reset only for a new pty session.
+// An unterminated string is swallowed until its terminator; only past `maxStringChars` (UTF-16
+// units, i.e. string length) does the parser give up and return to text.
+//
+// Where a string starts and ends follows xterm.js 5.5's VT500 table (EscapeSequenceParser.ts), which
+// renders the owner's node, the session host's emulator and the viewer page. Every rule below that
+// goes past "ESC + ] P X ^ _" closes a way the viewer would receive bytes the owner's terminal
+// treats as string payload:
+// - BEL ends an OSC only. Inside DCS it is data, inside SOS/PM/APC it is ignored, so the owner never
+// sees what follows it.
+// - In ESCAPE, xterm executes C0 controls and ignores DEL without leaving it, restarts it on a second
+// ESC, and takes an 8-bit introducer as a string start: `ESC \n ]52;…` is still an OSC 52.
+// - An 8-bit introducer inside a string starts a new string of ITS kind (an OSC turned DCS no longer
+// ends at BEL), with a fresh count toward the cap.
+// Where xterm ENDS a string on something this parser does not (CAN, SUB, other C1 controls), the
+// viewer misses a little text until the next terminator; it never sees more than the owner.
+//
+// The output guarantee the caller relies on: no push emits an 8-bit introducer or ends on ESC, and
+// every ESC it emits is followed by a char that leaves ESCAPE without starting a string. So the
+// viewer's parser can never be put into a string state, even by output that is dropped in part
+// (drop-and-redraw) or starts on a keyframe.
+//
+// It runs on the owner's process for every byte, once per viewer, so text is copied by the slice
+// between escapes rather than char by char: about 3x faster, measured on a captured tmux stream of
+// colored output (an ESC every ~8 bytes) on a loaded host: 22-39 MB/s char by char, 64-130 MB/s by
+// slice. Such a flood reaches a tmux 3.4 client at ~2.7 MB/s.
+
+const ESC = '\x1b'
+// Fast path: a chunk with none of these, read in text mode, is returned as it is.
+const TEXT_SPECIAL = /[\x1b\x90\x98\x9d-\x9f]/
+const INTRO_8_ANY = /[\x90\x98\x9d-\x9f]/
+
+// OSC, DCS, SOS, PM, APC: `ESC ] P X ^ _` and their 8-bit forms.
+function isIntro7(c: number): boolean {
+ return c === 0x5d || c === 0x50 || c === 0x58 || c === 0x5e || c === 0x5f
+}
+function isIntro8(c: number): boolean {
+ return c === 0x9d || c === 0x90 || c === 0x98 || c === 0x9e || c === 0x9f
+}
+// The C0 controls xterm executes while staying in ESCAPE: all but CAN, SUB and ESC itself.
+function isC0Executable(c: number): boolean {
+ return c <= 0x17 || c === 0x19 || (c >= 0x1c && c <= 0x1f)
+}
+
+export interface StreamFilter {
+ push(chunk: string): string
+ reset(): void
+}
+
+export function createStreamFilter(maxStringChars = 1_048_576): StreamFilter {
+ // 'esc' is ESCAPE after a plain ESC; 'stringEsc' is ESCAPE after the ESC that ended a string,
+ // where `ESC \` is that string's ST and is dropped with it.
+ let mode: 'text' | 'esc' | 'string' | 'stringEsc' = 'text'
+ let osc = false
+ let len = 0
+ const enterString = (introducer: number): void => {
+ mode = 'string'
+ osc = introducer === 0x5d || introducer === 0x9d
+ len = 0
+ }
+ return {
+ reset() {
+ mode = 'text'
+ osc = false
+ len = 0
+ },
+ push(chunk) {
+ if (mode === 'text' && !TEXT_SPECIAL.test(chunk)) return chunk
+ // An 8-bit introducer is rare (a decoded stream carries one only if a program wrote the
+ // two-byte UTF-8 form), so in most chunks ESC is the only char text mode stops at.
+ const has8 = INTRO_8_ANY.test(chunk)
+ const n = chunk.length
+ let out = ''
+ let i = 0
+ while (i < n) {
+ if (mode === 'text') {
+ let j = chunk.indexOf(ESC, i)
+ if (j === -1) j = n
+ if (has8) {
+ for (let k = i; k < j; k++) {
+ if (isIntro8(chunk.charCodeAt(k))) {
+ j = k
+ break
+ }
+ }
+ }
+ out += chunk.slice(i, j)
+ if (j === n) break
+ const c = chunk.charCodeAt(j)
+ if (c === 0x1b) mode = 'esc'
+ else enterString(c)
+ i = j + 1
+ } else if (mode === 'string') {
+ const c = chunk.charCodeAt(i++)
+ if (c === 0x9c || (c === 0x07 && osc)) mode = 'text'
+ else if (c === 0x1b) mode = 'stringEsc'
+ else if (isIntro8(c)) enterString(c)
+ else if (++len > maxStringChars) {
+ mode = 'text'
+ len = 0
+ }
+ } else {
+ // ESCAPE. The ESC is held until the char that decides what it starts arrives.
+ const c = chunk.charCodeAt(i)
+ if (isIntro7(c) || isIntro8(c)) enterString(c)
+ else if (c === 0x1b) mode = 'esc'
+ else if (isC0Executable(c)) out += chunk[i]
+ else if (c === 0x7f) {
+ // DEL: ignored in ESCAPE.
+ } else if (c === 0x5c && mode === 'stringEsc') mode = 'text'
+ else {
+ out += ESC + chunk[i]
+ mode = 'text'
+ }
+ i++
+ }
+ }
+ return out
+ }
+ }
+}
diff --git a/src/core/watch-link/token-bucket.test.ts b/src/core/watch-link/token-bucket.test.ts
new file mode 100644
index 000000000..1a50f3bb9
--- /dev/null
+++ b/src/core/watch-link/token-bucket.test.ts
@@ -0,0 +1,42 @@
+import { describe, it, expect } from 'vitest'
+import { createTokenBucket } from './token-bucket'
+
+describe('createTokenBucket', () => {
+ it('spends a burst, then refills at the rate, never above the burst', () => {
+ let t = 0
+ const b = createTokenBucket({ ratePerSec: 100, burst: 300, now: () => t })
+ expect(b.take(300)).toBe(true)
+ expect(b.take(1)).toBe(false)
+ t = 500
+ expect(b.take(50)).toBe(true)
+ expect(b.take(1)).toBe(false)
+ t = 100_000
+ expect(b.take(301)).toBe(false)
+ expect(b.take(300)).toBe(true)
+ })
+
+ it('a refused take spends nothing', () => {
+ let t = 0
+ const b = createTokenBucket({ ratePerSec: 100, burst: 300, now: () => t })
+ expect(b.take(250)).toBe(true)
+ expect(b.take(60)).toBe(false)
+ expect(b.take(50)).toBe(true)
+ t = 10
+ expect(b.take(1)).toBe(true)
+ expect(b.take(1)).toBe(false)
+ })
+
+ it('a wall clock stepping backwards neither drains nor refills the bucket', () => {
+ // The caller passes Date.now, which NTP can step back; an hour back must not leave the viewer
+ // on keyframes for an hour, and the refill resumes from the new reading.
+ let t = 10_000_000
+ const b = createTokenBucket({ ratePerSec: 100, burst: 300, now: () => t })
+ expect(b.take(200)).toBe(true)
+ t -= 3_600_000
+ expect(b.take(100)).toBe(true)
+ expect(b.take(1)).toBe(false)
+ t += 1000
+ expect(b.take(100)).toBe(true)
+ expect(b.take(1)).toBe(false)
+ })
+})
diff --git a/src/core/watch-link/token-bucket.ts b/src/core/watch-link/token-bucket.ts
new file mode 100644
index 000000000..9822f59cf
--- /dev/null
+++ b/src/core/watch-link/token-bucket.ts
@@ -0,0 +1,22 @@
+// A per-viewer byte budget: a live link must not turn a flooding terminal into unbounded relay
+// traffic. Past it, the viewer gets at most one keyframe per second instead of the stream.
+export interface TokenBucket {
+ take(n: number): boolean
+}
+
+export function createTokenBucket(o: { ratePerSec: number; burst: number; now: () => number }): TokenBucket {
+ let tokens = o.burst
+ let last = o.now()
+ return {
+ take(n) {
+ const t = o.now()
+ // `now` may be a wall clock (Date.now), which can step back: a negative interval would drain
+ // the bucket for as long as the step, so it counts as none and the refill resumes from `t`.
+ tokens = Math.min(o.burst, tokens + (Math.max(0, t - last) / 1000) * o.ratePerSec)
+ last = t
+ if (n > tokens) return false
+ tokens -= n
+ return true
+ }
+ }
+}