The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
feat: implement immediate output flushing and command pipelining for improved terminal responsiveness and reliability
0xken15d5f914c88515
diff --git a/cli/src/lib/terminalStreamManager.spec.ts b/cli/src/lib/terminalStreamManager.spec.tsindex b942bc1c0..820f442f5 100644--- a/cli/src/lib/terminalStreamManager.spec.ts+++ b/cli/src/lib/terminalStreamManager.spec.ts@@ -391,4 +391,34 @@ describe('TerminalStreamManager', () => {expect(sent.at(-1)?.type).toBe('terminal_ready')expect(binarySent.at(-1)?.frame.kind).toBe(TerminalBinaryKind.keyframe)})+ it('sends the first output after a quiet gap without waiting for the coalescing window', async () => {+ await manager.handleFrame('web-1', 'terminal_open', {+ requestId: 'open-echo', protocolVersion: 3, agentId: 'agent-1', cols: 100, rows: 30,+ })+ const before = binarySent.length++ // A keystroke echo: no timers advanced at all.+ sink!.onData(Buffer.from('a'))+ expect(binarySent.length).toBe(before + 1)+ expect(binarySent.at(-1)?.frame.kind).toBe(TerminalBinaryKind.output)+ expect(Buffer.from(binarySent.at(-1)!.frame.bytes).toString()).toBe('a')+ })++ it('still coalesces a burst onto the trailing window', async () => {+ await manager.handleFrame('web-1', 'terminal_open', {+ requestId: 'open-burst', protocolVersion: 3, agentId: 'agent-1', cols: 100, rows: 30,+ })+ sink!.onData(Buffer.from('lead'))+ const afterLeadingEdge = binarySent.length++ // Everything arriving inside the window rides one frame, not one frame each.+ sink!.onData(Buffer.from('one'))+ sink!.onData(Buffer.from('two'))+ sink!.onData(Buffer.from('three'))+ expect(binarySent.length).toBe(afterLeadingEdge)++ await vi.advanceTimersByTimeAsync(8)+ expect(binarySent.length).toBe(afterLeadingEdge + 1)+ expect(Buffer.from(binarySent.at(-1)!.frame.bytes).toString()).toBe('onetwothree')+ })})diff --git a/cli/src/lib/terminalStreamManager.ts b/cli/src/lib/terminalStreamManager.tsindex 6b6e703bd..e90380bdd 100644--- a/cli/src/lib/terminalStreamManager.ts+++ b/cli/src/lib/terminalStreamManager.ts@@ -45,6 +45,7 @@ interface ActiveStream {output: Buffer[]outputBytes: numberflushTimer: ReturnType<typeof setTimeout> | null+ lastFlushAt: numbersnapshotting: booleanresyncing: booleanclosing: boolean@@ -301,6 +302,7 @@ export class TerminalStreamManager {output: buffered,outputBytes: buffered.reduce((sum, chunk) => sum + chunk.length, 0),flushTimer: null,+ lastFlushAt: 0,snapshotting: false,resyncing: false,closing: false,@@ -428,12 +430,21 @@ export class TerminalStreamManager {this.flushOutput(state)return}+ // Leading edge: the first output after a quiet gap goes out immediately, which is what a+ // keystroke echo is. Waiting the full window for it cost every echo 8ms for no benefit —+ // there was nothing else to coalesce it with. A burst still lands on the trailing timer+ // below, so the frame rate ceiling is unchanged.+ if (!state.flushTimer && this.now() - state.lastFlushAt >= OUTPUT_FLUSH_MS) {+ this.flushOutput(state)+ return+ }state.flushTimer ??= setTimeout(() => this.flushOutput(state), OUTPUT_FLUSH_MS)}private flushOutput(state: ActiveStream): void {if (state.flushTimer) { clearTimeout(state.flushTimer); state.flushTimer = null }if (state.closing || state.snapshotting || state.outputBytes === 0) return+ state.lastFlushAt = this.now()const all = Buffer.concat(state.output, state.outputBytes)state.output = []state.outputBytes = 0diff --git a/cli/src/lib/tmuxStream.real.spec.ts b/cli/src/lib/tmuxStream.real.spec.tsindex c156a760d..5304da407 100644--- a/cli/src/lib/tmuxStream.real.spec.ts+++ b/cli/src/lib/tmuxStream.real.spec.ts@@ -1,4 +1,7 @@import { execFile } from 'node:child_process'+import { mkdtemp, readFile } from 'node:fs/promises'+import { tmpdir } from 'node:os'+import { join } from 'node:path'import { randomUUID } from 'node:crypto'import { afterAll, beforeAll, describe, expect, it } from 'vitest'import { ENGINES } from '../engines/types.js'@@ -84,6 +87,43 @@ run('TmuxControlStream real tmux', () => {expect(closedReason === '' || closedReason === 'closed').toBe(true)})+ it('delivers a multi-chunk paste to the pane whole and in order', async () => {+ // 8 KiB in one writeRaw spans several `send-keys -H` commands (INPUT_CHUNK_BYTES is 2 KiB).+ // They are pipelined into the control client, so this is the case that would expose either a+ // lost chunk or a reordered one. Lines stay under the tty's canonical-mode limit on purpose.+ const directory = await mkdtemp(join(tmpdir(), 'harness-paste-'))+ const sink = join(directory, 'paste.txt')+ const pasteSession = `harness-paste-${randomUUID().slice(0, 8)}`+ // tmux runs the command with execvp, not a shell, so the redirect needs an explicit one.+ const pastePane = await tmux([+ 'new-session', '-d', '-P', '-F', '#{pane_id}', '-s', pasteSession,+ 'bash', '--noprofile', '--norc', '-c', `cat > ${sink}`,+ ])++ try {+ const opened = await TmuxControlStream.open(pastePane, { cols: 120, rows: 30 }, {+ onData: () => { /* echo is irrelevant; the file is the assertion */ },+ onClose: () => { /* torn down below */ },+ })+ expect(opened.state).toBe('succeeded')+ if (opened.state !== 'succeeded') return++ const lines = Array.from({ length: 100 }, (_, index) => `${String(index).padStart(4, '0')}${'y'.repeat(96)}`)+ const payload = Buffer.from(`${lines.join('\r')}\r`)+ // Spans five `send-keys -H` commands at INPUT_CHUNK_BYTES.+ expect(Math.ceil(payload.length / 2048)).toBeGreaterThanOrEqual(5)++ expect((await opened.value.writeRaw(payload)).state).toBe('succeeded')+ await eventually(async () => (await readFile(sink, 'utf8')).split('\n').length > lines.length)++ const written = (await readFile(sink, 'utf8')).split('\n').filter((line) => line.length > 0)+ expect(written).toEqual(lines)+ await opened.value.close()+ } finally {+ await tmux(['kill-session', '-t', pasteSession]).catch(() => { /* best effort */ })+ }+ })+it('runs every catalog engine through the same manager and real tmux stream', async () => {for (const [index, engine] of ENGINES.entries()) {const agentId = `real-tmux-${engine}`diff --git a/cli/src/lib/tmuxStream.spec.ts b/cli/src/lib/tmuxStream.spec.tsindex 067262777..599a1425c 100644--- a/cli/src/lib/tmuxStream.spec.ts+++ b/cli/src/lib/tmuxStream.spec.ts@@ -1,5 +1,6 @@-import { describe, expect, it } from 'vitest'+import { describe, expect, it, vi } from 'vitest'import {+ ControlCommandQueue,decodeTmuxControlData,normalizeTmuxHistoryLines,normalizeTmuxCaptureLines,@@ -124,3 +125,121 @@ describe('tmux snapshot row normalization', () => {expect(snapshot).toContain('\u001b[H\u001b[2J\u001b[?1003h\u001b[?1006h')})})++describe('ControlCommandQueue', () => {+ function harness(timeoutMs = 3_000) {+ const written: string[] = []+ const fatal: string[] = []+ let accept = true+ const queue = new ControlCommandQueue({+ write: (line) => { if (!accept) return false; written.push(line); return true },+ onFatal: (reason) => fatal.push(reason),+ timeoutMs,+ })+ queue.markReady()+ return { queue, written, fatal, reject: () => { accept = false } }+ }++ it('pipelines commands and completes them in order', async () => {+ const { queue, written } = harness()+ const first = queue.run('display-message -p one')+ const second = queue.run('display-message -p two')++ // Both reached tmux before either replied — that is what stops input queueing behind a snapshot.+ expect(written).toEqual(['display-message -p one\n', 'display-message -p two\n'])+ expect(queue.idle).toBe(false)++ queue.handleBegin('10')+ queue.appendResponseLine(Buffer.from('ONE'))+ queue.handleCompleted('end', '10')+ queue.handleBegin('11')+ queue.appendResponseLine(Buffer.from('TWO'))+ queue.handleCompleted('end', '11')++ expect((await first).stdout.toString()).toBe('ONE\n')+ expect((await second).stdout.toString()).toBe('TWO\n')+ expect(queue.idle).toBe(true)+ })++ it('keeps an untracked send-keys from stealing a real command number', async () => {+ const { queue } = harness()+ const keys = queue.run('send-keys -t %1 -H 61')+ const query = queue.run('display-message -p meta')++ queue.handleBegin('20')+ queue.handleCompleted('end', '20')+ queue.handleBegin('21')+ queue.appendResponseLine(Buffer.from('META'))+ queue.handleCompleted('end', '21')++ expect((await keys).ok).toBe(true)+ // The reply landed on the query, not on the keystroke that preceded it.+ expect((await query).stdout.toString()).toBe('META\n')+ })++ it('fails one command on %error without disturbing the next', async () => {+ const { queue } = harness()+ const bad = queue.run('send-keys -t %1 -H zz')+ const good = queue.run('display-message -p ok')++ queue.handleBegin('30')+ queue.handleCompleted('error', '30')+ queue.handleBegin('31')+ queue.appendResponseLine(Buffer.from('OK'))+ queue.handleCompleted('end', '31')++ expect((await bad).ok).toBe(false)+ expect((await good).ok).toBe(true)+ expect((await good).stdout.toString()).toBe('OK\n')+ })++ it('ignores a completion that does not match the head', async () => {+ const { queue } = harness()+ const pending = queue.run('display-message -p one')+ queue.handleBegin('40')+ expect(queue.handleCompleted('end', '41')).toBe(false)+ expect(queue.idle).toBe(false)+ expect(queue.handleCompleted('end', '40')).toBe(true)+ expect((await pending).ok).toBe(true)+ })++ it('times out from the moment a command reaches the head, not when it was written', async () => {+ vi.useFakeTimers()+ try {+ const { queue, fatal } = harness(100)+ const slow = queue.run('capture-pane -p')+ const queued = queue.run('send-keys -t %1 -H 61')++ queue.handleBegin('50')+ // The second command has been on the wire the whole time but is not being timed yet.+ await vi.advanceTimersByTimeAsync(90)+ queue.handleCompleted('end', '50')+ await vi.advanceTimersByTimeAsync(90)+ expect(fatal).toEqual([])++ await vi.advanceTimersByTimeAsync(20)+ expect(fatal).toEqual(['tmux control command timed out'])+ await slow+ queue.failAll()+ await queued+ } finally {+ vi.useRealTimers()+ }+ })++ it('resolves everything as failed when the channel dies', async () => {+ const { queue } = harness()+ const inFlight = queue.run('display-message -p one')+ queue.handleBegin('60')+ queue.failAll()+ expect((await inFlight).ok).toBe(false)+ expect(queue.idle).toBe(true)+ })++ it('reports a write that the pipe refused', async () => {+ const { queue, reject } = harness()+ reject()+ expect((await queue.run('display-message -p one')).ok).toBe(false)+ expect(queue.idle).toBe(true)+ })+})diff --git a/cli/src/lib/tmuxStream.ts b/cli/src/lib/tmuxStream.tsindex 06ceff3d9..ca2212671 100644--- a/cli/src/lib/tmuxStream.ts+++ b/cli/src/lib/tmuxStream.ts@@ -1,5 +1,4 @@import { execFile, spawn, type ChildProcessWithoutNullStreams } from 'node:child_process'-import { randomUUID } from 'node:crypto'import {TERMINAL_ACTION_SUCCEEDED,terminalActionNotStarted,@@ -17,7 +16,9 @@ const MIN_COLS = 40const MAX_COLS = 300const MIN_ROWS = 12const MAX_ROWS = 120-const SMALL_INPUT_BYTES = 4 * 1024+// One `send-keys -H` line carries two hex characters plus a space per byte. tmux accepts a command+// line built from 8192 such bytes and rejects 16384 with `%error`, so this leaves a 4x margin.+const INPUT_CHUNK_BYTES = 2 * 1024const CONTROL_COMMAND_TIMEOUT_MS = 3_000const SNAPSHOT_BUFFER_MAX_BYTES = 2 * 1024 * 1024const SNAPSHOT_QUIET_MS = 8@@ -49,15 +50,6 @@ interface PaneMeta {mouseSgr: boolean}-interface PendingControlCommand {- command: string- lines: Buffer[]- commandNumber: string | null- timer: ReturnType<typeof setTimeout> | null- onEnd?: () => void- resolve: (result: { ok: boolean; stdout: Buffer }) => void-}-function boundedSize(size: TerminalStreamSize): TerminalStreamSize {return {cols: Math.max(MIN_COLS, Math.min(MAX_COLS, Math.floor(size.cols))),@@ -65,6 +57,8 @@ function boundedSize(size: TerminalStreamSize): TerminalStreamSize {}}+/** Spawns a one-shot tmux client. Only the pre-open probe below may use it: once the control+ * client is attached, every command goes down its stdin instead (see `ControlCommandQueue`). */function execTmux(args: string[], timeout = 2_000): Promise<{ ok: boolean; stdout: Buffer }> {return new Promise((resolve) => {execFile('tmux', args, { timeout, encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 }, (error, stdout) => {@@ -263,32 +257,146 @@ export function synthesizeTmuxSnapshot(return Buffer.concat([prefix, normalizedHistory, historySuffix, ...rows, suffix])}-async function loadAndPaste(paneId: string, bytes: Uint8Array): Promise<TerminalActionResult> {- const bufferName = `harness-terminal-${randomUUID()}`- const loaded = await new Promise<boolean>((resolve) => {- const child = spawn('tmux', ['load-buffer', '-b', bufferName, '-'], { stdio: ['pipe', 'ignore', 'ignore'] })- child.once('error', () => resolve(false))- child.once('close', (code) => resolve(code === 0))- child.stdin.end(Buffer.from(bytes))- })- if (!loaded) return terminalActionNotStarted('tmux input buffer could not be loaded')- const pasted = await execTmux(['paste-buffer', '-d', '-b', bufferName, '-t', paneId])- if (!pasted.ok) {- void execTmux(['delete-buffer', '-b', bufferName])- return terminalActionPossiblyExecuted('tmux raw paste did not complete')+export interface ControlCommandResult {+ ok: boolean+ stdout: Buffer+}++interface PendingControlCommand {+ command: string+ lines: Buffer[]+ commandNumber: string | null+ timer: ReturnType<typeof setTimeout> | null+ onEnd?: () => void+ resolve: (result: ControlCommandResult) => void+}++const CONTROL_COMMAND_FAILED: ControlCommandResult = { ok: false, stdout: Buffer.alloc(0) }++export interface ControlCommandQueueDeps {+ /** Write one command line to the control client's stdin. False means the pipe is gone. */+ write: (line: string) => boolean+ /** The channel is unusable and the owning stream must be torn down. */+ onFatal: (reason: string) => void+ timeoutMs?: number+}++/**+ * FIFO bookkeeping for `tmux -C` commands, kept free of the child process so it can be tested+ * directly — the same reason the octal/capture helpers above are exported as pure functions.+ *+ * tmux answers EVERY command with its own `%begin`/`%end` block, strictly in the order the commands+ * were written, with monotonically increasing command numbers. That holds even when several+ * commands are written back-to-back while an earlier one is still outstanding, which is what lets+ * keystrokes pipeline instead of waiting for whatever else is in flight.+ */+export class ControlCommandQueue {+ private readonly waiting: PendingControlCommand[] = []+ private readonly outstanding: PendingControlCommand[] = []+ private ready = false+ private readonly timeoutMs: number++ constructor(private readonly deps: ControlCommandQueueDeps) {+ this.timeoutMs = deps.timeoutMs ?? CONTROL_COMMAND_TIMEOUT_MS+ }++ /** No command of ours is in flight. Distinguishes tmux's own opening transaction from a reply. */+ get idle(): boolean {+ return this.outstanding.length === 0+ }++ get isReady(): boolean {+ return this.ready+ }++ /** `tmux -C attach-session` finishes its own initial transaction before ours may be written. */+ markReady(): void {+ if (this.ready) return+ this.ready = true+ this.pump()+ }++ run(command: string, onEnd?: () => void): Promise<ControlCommandResult> {+ return new Promise((resolve) => {+ this.waiting.push({ command, lines: [], commandNumber: null, timer: null, onEnd, resolve })+ this.pump()+ })+ }++ handleBegin(commandNumber: string): void {+ const pending = this.outstanding.find((command) => command.commandNumber == null)+ if (pending) pending.commandNumber = commandNumber+ }++ /** Response lines belong to the head: tmux never interleaves two commands' output. */+ appendResponseLine(line: Buffer): void {+ const head = this.outstanding[0]+ if (head?.commandNumber != null) head.lines.push(Buffer.from(line))+ }++ handleCompleted(kind: 'end' | 'error', commandNumber: string): boolean {+ const head = this.outstanding[0]+ if (!head || head.commandNumber !== commandNumber) return false+ this.outstanding.shift()+ if (head.timer) clearTimeout(head.timer)+ head.timer = null+ if (kind === 'end') head.onEnd?.()+ head.resolve({ ok: kind === 'end', stdout: decodeControlResponse(head.lines) })+ this.armHead()+ return true+ }++ failAll(): void {+ const abandoned = [...this.outstanding.splice(0), ...this.waiting.splice(0)]+ for (const command of abandoned) {+ if (command.timer) clearTimeout(command.timer)+ command.timer = null+ command.resolve(CONTROL_COMMAND_FAILED)+ }+ }++ private pump(): void {+ if (!this.ready) return+ while (this.waiting.length) {+ const next = this.waiting.shift()!+ let written = false+ try {+ written = this.deps.write(`${next.command}\n`)+ } catch {+ written = false+ }+ if (!written) {+ next.resolve(CONTROL_COMMAND_FAILED)+ continue+ }+ this.outstanding.push(next)+ }+ this.armHead()+ }++ /**+ * Only the head is timed. The timeout has to start when a command reaches the head rather than+ * when it was written: commands now pipeline, so anything queued behind a slow `capture-pane`+ * would otherwise expire while tmux was still legitimately busy and take the stream down with it.+ */+ private armHead(): void {+ const head = this.outstanding[0]+ if (!head || head.timer) return+ head.timer = setTimeout(() => {+ if (this.outstanding[0] !== head) return+ this.deps.onFatal('tmux control command timed out')+ }, this.timeoutMs)}- return TERMINAL_ACTION_SUCCEEDED}-async function sendHexBytes(paneId: string, bytes: Uint8Array): Promise<TerminalActionResult> {- for (let offset = 0; offset < bytes.length; offset += 256) {- const hex = [...bytes.slice(offset, offset + 256)].map((byte) => byte.toString(16).padStart(2, '0'))- const result = await execTmux(['send-keys', '-t', paneId, '-H', ...hex])- if (!result.ok) return offset === 0- ? terminalActionNotStarted('tmux raw input could not be sent')- : terminalActionPossiblyExecuted('tmux raw input stopped after a partial write')+/** tmux escapes a response line that starts with `%` as `%%`; the rest is octal-escaped as usual. */+function decodeControlResponse(lines: readonly Buffer[]): Buffer {+ const chunks: Buffer[] = []+ for (const line of lines) {+ const escaped = line.subarray(0, 2).equals(Buffer.from('%%')) ? line.subarray(1) : line+ chunks.push(Buffer.from(decodeTmuxControlBytes(escaped)), Buffer.from('\n'))}- return TERMINAL_ACTION_SUCCEEDED+ return Buffer.concat(chunks)}export class TmuxControlStream implements TerminalStreamHandle<TmuxRuntimeRef> {@@ -299,9 +407,7 @@ export class TmuxControlStream implements TerminalStreamHandle<TmuxRuntimeRef> {private closeNotified = false// `tmux -C attach-session` emits its own initial %begin/%end transaction.// Do not assign that command number to our first queued command.- private controlReady = false- private commandActive: PendingControlCommand | null = null- private readonly commandQueue: PendingControlCommand[] = []+ private readonly commands: ControlCommandQueueprivate operationTail: Promise<void> = Promise.resolve()private snapshotGated = falseprivate snapshotPostCut: Buffer[] = []@@ -315,6 +421,19 @@ export class TmuxControlStream implements TerminalStreamHandle<TmuxRuntimeRef> {) {this.runtime = { backend: 'tmux', paneId }this.child = child+ this.commands = new ControlCommandQueue({+ write: (line) => {+ if (this.closed || !child.stdin.writable) return false+ // A large paste can outrun the pipe. Node buffers what does not fit and drains it in+ // order, so a false return here is backpressure, not loss — never a reason to drop input.+ child.stdin.write(line)+ return true+ },+ onFatal: (reason) => {+ this.notifyClose(reason)+ void this.close()+ },+ })child.stdout.on('data', (chunk: Buffer) => this.onStdout(chunk))child.once('error', (error) => this.notifyClose(`tmux control client error: ${error.message}`))child.once('close', (code) => this.notifyClose(this.closed ? 'closed' : `tmux control client exited (${code ?? 'signal'})`))@@ -363,23 +482,16 @@ export class TmuxControlStream implements TerminalStreamHandle<TmuxRuntimeRef> {const text = line.toString('ascii')const begin = /^%begin\s+\d+\s+(\d+)\s+\d+$/.exec(text)if (begin) {- if (this.commandActive && this.commandActive.commandNumber == null) {- this.commandActive.commandNumber = begin[1]- }+ this.commands.handleBegin(begin[1])return}const completed = /^%(end|error)\s+\d+\s+(\d+)\s+\d+$/.exec(text)if (completed) {- const active = this.commandActive- if (!this.controlReady && active == null) {- this.controlReady = true- this.pumpControlCommand()+ if (!this.commands.isReady && this.commands.idle) {+ this.commands.markReady()return}- if (active?.commandNumber === completed[2]) {- if (completed[1] === 'end') active.onEnd?.()- this.finishControlCommand(active, completed[1] === 'end')- }+ this.commands.handleCompleted(completed[1] === 'end' ? 'end' : 'error', completed[2])return}const paused = /^%pause\s+(%\d+)$/.exec(text)@@ -398,7 +510,7 @@ export class TmuxControlStream implements TerminalStreamHandle<TmuxRuntimeRef> {// leading-percent line is an asynchronous control-mode notification and// must not be mixed into a command response.if (text.startsWith('%') && !text.startsWith('%%')) return- if (this.commandActive?.commandNumber != null) this.commandActive.lines.push(Buffer.from(line))+ this.commands.appendResponseLine(line)}private routeOutput(bytes: Uint8Array): void {@@ -417,12 +529,9 @@ export class TmuxControlStream implements TerminalStreamHandle<TmuxRuntimeRef> {}}- private runControlCommand(command: string, onEnd?: () => void): Promise<{ ok: boolean; stdout: Buffer }> {- if (this.closed || !this.child.stdin.writable) return Promise.resolve({ ok: false, stdout: Buffer.alloc(0) })- return new Promise((resolve) => {- this.commandQueue.push({ command, lines: [], commandNumber: null, timer: null, onEnd, resolve })- this.pumpControlCommand()- })+ private runControlCommand(command: string, onEnd?: () => void): Promise<ControlCommandResult> {+ if (this.closed || !this.child.stdin.writable) return Promise.resolve(CONTROL_COMMAND_FAILED)+ return this.commands.run(command, onEnd)}private serializeOperation<T>(operation: () => Promise<T>): Promise<T> {@@ -431,49 +540,8 @@ export class TmuxControlStream implements TerminalStreamHandle<TmuxRuntimeRef> {return result}- private pumpControlCommand(): void {- if (!this.controlReady || this.commandActive || this.closed) return- const next = this.commandQueue.shift()- if (!next) return- this.commandActive = next- next.timer = setTimeout(() => {- if (this.commandActive !== next) return- this.notifyClose('tmux control command timed out')- void this.close()- }, CONTROL_COMMAND_TIMEOUT_MS)- try {- this.child.stdin.write(`${next.command}\n`)- } catch {- this.finishControlCommand(next, false)- }- }-- private finishControlCommand(command: PendingControlCommand, ok: boolean): void {- if (this.commandActive !== command) return- if (command.timer) clearTimeout(command.timer)- command.timer = null- this.commandActive = null- const chunks: Buffer[] = []- for (const line of command.lines) {- const escaped = line.subarray(0, 2).equals(Buffer.from('%%')) ? line.subarray(1) : line- chunks.push(Buffer.from(decodeTmuxControlBytes(escaped)), Buffer.from('\n'))- }- command.resolve({ ok, stdout: Buffer.concat(chunks) })- this.pumpControlCommand()- }-private failControlCommands(): void {- const active = this.commandActive- this.commandActive = null- const queuedCommands = this.commandQueue.splice(0)- if (active) {- if (active.timer) clearTimeout(active.timer)- active.timer = null- active.resolve({ ok: false, stdout: Buffer.alloc(0) })- }- for (const queued of queuedCommands) {- queued.resolve({ ok: false, stdout: Buffer.alloc(0) })- }+ this.commands.failAll()}private notifyClose(reason: string): void {@@ -557,18 +625,42 @@ export class TmuxControlStream implements TerminalStreamHandle<TmuxRuntimeRef> {for (const chunk of postCut) this.sink.onData(chunk)}+ /**+ * Keystrokes go down the control client that is already attached, never a fresh `tmux` process.+ * Spawning one cost 4.4ms at the median and 13ms at the tail — per keystroke — against 0.2ms+ * here, and an 8 KiB paste went from 184ms across 32 spawns to under a millisecond.+ *+ * Deliberately NOT wrapped in `serializeOperation`: input must not queue behind a `snapshot()`+ * or a `resize()`. Ordering against those still holds because tmux runs control commands in the+ * order they were written, which is also what keeps the snapshot's `%end` cut correct.+ */async writeRaw(bytes: Uint8Array): Promise<TerminalActionResult> {if (this.closed) return terminalActionNotStarted('terminal stream is closed')if (bytes.length === 0) return TERMINAL_ACTION_SUCCEEDED- return bytes.length <= SMALL_INPUT_BYTES- ? sendHexBytes(this.runtime.paneId, bytes)- : loadAndPaste(this.runtime.paneId, bytes)+ // Every chunk is handed to the queue before any reply is awaited, so a paste costs one+ // round-trip rather than one per chunk. tmux applies them in the order they were written.+ const sends: Array<Promise<ControlCommandResult>> = []+ for (let offset = 0; offset < bytes.length; offset += INPUT_CHUNK_BYTES) {+ const chunk = bytes.subarray(offset, offset + INPUT_CHUNK_BYTES)+ const hex = [...chunk].map((byte) => byte.toString(16).padStart(2, '0')).join(' ')+ sends.push(this.runControlCommand(`send-keys -t ${this.runtime.paneId} -H ${hex}`))+ }+ const results = await Promise.all(sends)+ const failedAt = results.findIndex((result) => !result.ok)+ if (failedAt < 0) return TERMINAL_ACTION_SUCCEEDED+ return failedAt === 0+ ? terminalActionNotStarted('tmux raw input could not be sent')+ : terminalActionPossiblyExecuted('tmux raw input stopped after a partial write')}async resize(requested: TerminalStreamSize): Promise<TerminalActionResult> {return this.serializeOperation(async () => {if (this.closed) return terminalActionNotStarted('terminal stream is closed')- const meta = await paneMeta(this.runtime.paneId)+ const metadata = await this.runControlCommand(+ `display-message -p -t ${this.runtime.paneId} '${PANE_META_FORMAT}'`,+ )+ if (!metadata.ok) return terminalActionNotStarted('tmux pane disappeared')+ const meta = parsePaneMeta(metadata.stdout)if (!meta) return terminalActionNotStarted('tmux pane disappeared')if (meta.windowPanes !== 1) return terminalActionNotStarted('TERMINAL_MULTI_PANE_UNSUPPORTED')const size = boundedSize(requested)@@ -578,7 +670,9 @@ export class TmuxControlStream implements TerminalStreamHandle<TmuxRuntimeRef> {if (meta.windowWidth === size.cols && meta.windowHeight === size.rows) {return TERMINAL_ACTION_SUCCEEDED}- const result = await execTmux(['resize-window', '-t', meta.windowId, '-x', String(size.cols), '-y', String(size.rows)])+ const result = await this.runControlCommand(+ `resize-window -t ${meta.windowId} -x ${size.cols} -y ${size.rows}`,+ )if (!result.ok) return terminalActionPossiblyExecuted('tmux resize did not complete')return TERMINAL_ACTION_SUCCEEDED})