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(pty): refuse multi-line control-mode commands

enesa82f70f8c75b
diff --git a/src/core/tmux-control-client.test.ts b/src/core/tmux-control-client.test.ts
index a6823a041..2443b88ca 100644
--- a/src/core/tmux-control-client.test.ts
+++ b/src/core/tmux-control-client.test.ts
@@ -168,6 +168,23 @@ describe('ControlModeClient.command', () => {
expect(await first).toEqual({ ok: true, body: ['80'] })
expect(await second).toEqual({ ok: true, body: ['24'] })
})
+ it('refuses a line with an embedded newline instead of desyncing the FIFO', async () => {
+ // One `command()` call queues ONE resolver, so a line carrying its own newline would send TWO
+ // commands, draw TWO replies, and pair every later reply with the wrong caller — forever, since
+ // this layer has no timers to notice. Reject before anything is written or queued.
+ const { client, child } = makeClient()
+ const bad = client.command('display -p a\nkill-server')
+ await expect(bad).rejects.toThrow(/newline/)
+ expect(child.writes).toEqual([])
+ const good = client.command('list-panes')
+ child.feed(reply(1, ['still in phase']))
+ await expect(good).resolves.toEqual({ ok: true, body: ['still in phase'] })
+ })
+ it('refuses a line with an embedded carriage return', async () => {
+ const { client, child } = makeClient()
+ await expect(client.command('display -p a\rkill-server')).rejects.toThrow(/newline/)
+ expect(child.writes).toEqual([])
+ })
it('rejects instead of hanging when the client is not running', async () => {
const spawner = new FakeControlSpawn()
const client = new ControlModeClient({
@@ -210,7 +227,12 @@ describe('ControlModeClient exit', () => {
const { client, child } = makeClient({ onExit })
const pending = client.command('list-panes')
child.feed('%begin 1700 1 0\n')
- child.feed('x'.repeat(MAX_PENDING_BYTES + 1))
+ // Fed in realistic ~64KiB pipe chunks, each one complete lines and each far under the cap: what
+ // must trip is the total accumulated SINCE THE LAST COMPLETED EVENT (no event can complete while
+ // the block stays open), not any single chunk's length.
+ const chunk = `${'x'.repeat(64 * 1024 - 1)}\n`
+ const enough = Math.ceil(MAX_PENDING_BYTES / chunk.length) + 1
+ for (let i = 0; i < enough && client.alive; i++) child.feed(chunk)
expect(child.killed).toBe(1)
expect(onExit).toHaveBeenCalledTimes(1)
expect(client.alive).toBe(false)
diff --git a/src/core/tmux-control-client.ts b/src/core/tmux-control-client.ts
index 820ca02fe..c517d714e 100644
--- a/src/core/tmux-control-client.ts
+++ b/src/core/tmux-control-client.ts
@@ -113,8 +113,19 @@ export class ControlModeClient {
return ok
}
- /** Run one control-mode command line. Rejects if the client is not running or dies while waiting. */
+ /**
+ * Run ONE control-mode command line. Every failure arrives as a rejected promise (this class never
+ * throws at the caller): a `line` that is not a single line, a client that is not running, or a
+ * client that dies while the reply is outstanding.
+ */
command(line: string): Promise<{ ok: boolean; body: string[] }> {
+ if (/[\r\n]/.test(line)) {
+ // The protocol is one command per line and this call queues exactly ONE resolver, so a line
+ // carrying its own newline would run two commands, draw two replies, and pair every later
+ // reply with the wrong caller — permanently, because positional correlation has no timer to
+ // notice. Refusing the input is what keeps that state unreachable.
+ return Promise.reject(new Error('tmux control-mode command must not contain a newline'))
+ }
if (!this.alive || !this.proc) {
return Promise.reject(new Error('tmux control-mode client is not running'))
}