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
fix(pty): only close a control-mode block on its own ts+num
enes55620de6e2da
diff --git a/src/core/tmux-control.test.ts b/src/core/tmux-control.test.tsindex 3a55e310b..60642e827 100644--- a/src/core/tmux-control.test.ts+++ b/src/core/tmux-control.test.ts@@ -34,6 +34,23 @@ describe('createControlDecoder', () => {const ev = d.push('%begin 100 8 0\nno such session\n%error 100 8 0\n')expect(ev).toEqual([{ kind: 'reply', num: 8, ok: false, body: ['no such session'] }])})+ it('keeps a body line that only looks like a terminator inside the reply body', () => {+ // tmux does NOT escape command output inside a block, so a reply carrying pane text (e.g.+ // capture-pane) can contain a line spelled exactly like `%end`. Only the OPEN block's ts+num+ // may close it — otherwise the reply truncates and every later line is parsed one block off.+ const d = createControlDecoder()+ const ev = d.push('%begin 100 7 0\n%end 99 6 0\ntail\n%end 100 7 0\n')+ expect(ev).toEqual([{ kind: 'reply', num: 7, ok: true, body: ['%end 99 6 0', 'tail'] }])+ })+ it('does not let a foreign %error close the open block', () => {+ const d = createControlDecoder()+ const ev = d.push('%begin 100 7 0\n%error 100 6 0\n%end 100 7 0\n')+ expect(ev).toEqual([{ kind: 'reply', num: 7, ok: true, body: ['%error 100 6 0'] }])+ })+ it('parses CRLF-terminated lines like LF ones, keeping an escaped CR in the payload', () => {+ const d = createControlDecoder()+ expect(d.push('%output %3 ab\\015\r\n')).toEqual([{ kind: 'output', paneId: '%3', data: 'ab\r' }])+ })it('reports %exit as exited', () => {expect(createControlDecoder().push('%exit\n')).toEqual([{ kind: 'exited' }])})diff --git a/src/core/tmux-control.ts b/src/core/tmux-control.tsindex f8edd1772..b2118b72a 100644--- a/src/core/tmux-control.ts+++ b/src/core/tmux-control.ts@@ -4,6 +4,13 @@// Protocol: notifications are %-prefixed lines; command replies arrive as a// `%begin <ts> <num> <flags>` … body … `%end|%error <ts> <num> <flags>` block; `%output %<pane> <data>`// carries raw pane bytes with non-printables as \ooo octal escapes.+//+// ENCODING (hard requirement on the client): feed this decoder LATIN1-decoded chunks — read the+// tmux process stream as 'latin1'/'binary' (or decode Buffers with latin1), never `setEncoding('utf8')`.+// tmux escapes only bytes < 0x20 and the backslash; every byte >= 0x80 rides the channel RAW, so a+// UTF-8 decode of the transport silently mangles them (a `ç` split across two chunks becomes U+FFFD+// and is unrecoverable). Latin1 is lossless byte↔char, which is what makes `decodeOctal`'s+// byte-per-char output re-assemblable into correct UTF-8 downstream.export type ControlEvent =| { kind: 'output'; paneId: string; data: string }@@ -16,6 +23,9 @@ export type ControlEvent =* byte-per-char string (latin1-style), not decoded UTF-8: a `ç` arrives as `\303\247` and comes back* as two chars. The caller re-assembles bytes before handing them to a UTF-8 decoder, which is also* what makes split multi-byte sequences across chunks survive.+ *+ * That contract holds ONLY for latin1-decoded input (see the ENCODING note at the top of the file):+ * unescaped high bytes must have survived the transport one-char-per-byte to be re-assembled here.*/export function decodeOctal(s: string): string {return s.replace(/\\(\d{3}|\\)/g, (_, esc: string) =>@@ -35,11 +45,13 @@ export function encodeSendKeysHex(target: string, data: string): string {/*** A stateful line splitter over the control-mode stream. `push` takes an arbitrary chunk (partial- * lines are held until their newline arrives) and returns the events it completed.+ * lines are held until their newline arrives) and returns the events it completed. Chunks MUST be+ * latin1-decoded — see the ENCODING note at the top of the file.*/export function createControlDecoder(): { push(chunk: string): ControlEvent[] } {let buf = ''- let block: { num: number; body: string[] } | null = null+ // ts+num are kept as the RAW strings from `%begin` so the terminator can be matched literally.+ let block: { ts: string; num: string; body: string[] } | null = nullreturn {push(chunk: string): ControlEvent[] {buf += chunk@@ -49,20 +61,27 @@ export function createControlDecoder(): { push(chunk: string): ControlEvent[] }const line = buf.slice(0, nl).replace(/\r$/, '')buf = buf.slice(nl + 1)if (block) {- // The terminator repeats the command number; tmux cannot interleave blocks, so the open- // block's own num stays authoritative and only end-vs-error decides success.- const done = line.match(/^%(end|error) \d+ \d+ \d+$/)- if (done) {- out.push({ kind: 'reply', num: block.num, ok: done[1] === 'end', body: block.body })+ // Command output inside a block is NOT escaped by tmux, so a body line can be spelled+ // exactly like a terminator (capture-pane of a screen showing one, say). Only a terminator+ // repeating this block's own ts AND num closes it; anything else is body. Without that+ // guard the reply truncates and every following line is parsed one block out of phase.+ const done = line.match(/^%(end|error) (\d+) (\d+) \d+$/)+ if (done && done[2] === block.ts && done[3] === block.num) {+ out.push({+ kind: 'reply',+ num: Number(block.num),+ ok: done[1] === 'end',+ body: block.body+ })block = null} else {block.body.push(line)}continue}- const begin = line.match(/^%begin \d+ (\d+) \d+$/)+ const begin = line.match(/^%begin (\d+) (\d+) \d+$/)if (begin) {- block = { num: Number(begin[1]), body: [] }+ block = { ts: begin[1], num: begin[2], body: [] }} else if (line.startsWith('%output ')) {const m = line.match(/^%output (%\d+) (.*)$/s)if (m) out.push({ kind: 'output', paneId: m[1], data: decodeOctal(m[2]) })