Skip to content

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.

Merge remote-tracking branch 'origin/main' into pr-55

bubu-dudufa06db582356
diff --cc cli/src/lib/terminalStreamManager.spec.ts
index b38eb2032,be0df60fc..69793adc2
--- a/cli/src/lib/terminalStreamManager.spec.ts
+++ b/cli/src/lib/terminalStreamManager.spec.ts
@@@ -137,19 -175,34 +177,47 @@@ describe('TerminalStreamManager', () =
expect(sent.at(-1)?.payload.code).toBe('TERMINAL_AGENT_NOT_FOUND')
})
+ it('tells a Grok snapshot not to seed tmux history, and leaves other engines alone', async () => {
+ await manager.handleFrame('web-1', 'terminal_open', {
+ requestId: 'open-codex', protocolVersion: 3, agentId: 'agent-1', cols: 120, rows: 40,
+ })
+ expect(stream.snapshotOptions.at(-1)).toBeUndefined()
+
+ agents.set('grok-1', session('grok', 'grok-1'))
+ await manager.handleFrame('web-1', 'terminal_open', {
+ requestId: 'open-grok', protocolVersion: 3, agentId: 'grok-1', cols: 120, rows: 40,
+ })
+ expect(stream.snapshotOptions.at(-1)).toEqual({ tuiOwnsScrollback: true })
+ })
+
+ it('tells the daemon an agent took input from its controller — never from a watcher, never for input it refused', async () => {
+ // The keystroke prewarm (grid-reads-without-waking issue 03) hangs off this: typing into a pane whose
+ // agent runs on a sleeping grid starts that grid. A read-only watcher's input reaches no pty, so it
+ // must start nothing either.
+ const typed: string[] = []
+ await manager.stop()
+ manager = newManager({ onInput: (agentId) => { typed.push(agentId) } })
+ await manager.handleFrame('web-1', 'terminal_open', { requestId: 'open-1', protocolVersion: 3, agentId: 'agent-1', cols: 120, rows: 40 })
+ const controller = sent.find((frame) => frame.type === 'terminal_ready' && frame.connId === 'web-1')!.payload.streamId as string
+ await manager.handleFrame('web-2', 'terminal_open', {
+ requestId: 'open-2', protocolVersion: 3, agentId: 'agent-1', cols: 120, rows: 40, takeover: false,
+ })
+ const watcher = sent.find((frame) => frame.type === 'terminal_ready' && frame.connId === 'web-2')!
+ expect(watcher.payload.readOnly).toBe(true)
+
+ await manager.handleBinary('web-2', {
+ kind: TerminalBinaryKind.input, streamId: watcher.payload.streamId as string, seq: 0, compressed: false, bytes: Buffer.from('x'),
+ })
+ await manager.handleBinary('web-1', {
+ kind: TerminalBinaryKind.input, streamId: controller, seq: 5, compressed: false, bytes: Buffer.from('out of order'),
+ })
+ expect(typed).toEqual([])
+
+ await manager.handleBinary('web-1', { kind: TerminalBinaryKind.input, streamId: controller, seq: 0, compressed: false, bytes: Buffer.from('x') })
+ await manager.handleBinary('web-1', { kind: TerminalBinaryKind.paste, streamId: controller, seq: 0, compressed: false, bytes: Buffer.from('hello') })
+ expect(typed).toEqual(['agent-1', 'agent-1'])
+ })
+
it('opens with a keyframe, streams coalesced output, and writes ordered raw input', async () => {
await manager.handleFrame('web-1', 'terminal_open', {
requestId: 'open-1', protocolVersion: 3, agentId: 'agent-1', cols: 120, rows: 40, compression: ['zlib'],
diff --cc cli/src/lib/tmuxStream.ts
index 17b82fe73,9abb037c4..9e6d5b7cd
--- a/cli/src/lib/tmuxStream.ts
+++ b/cli/src/lib/tmuxStream.ts
@@@ -667,11 -657,26 +674,17 @@@ export class TmuxControlStream implemen
* 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.
+ *
+ * ⚠️ Every `runControlCommand` below MUST stay before the first `await`. The caller
+ * (TerminalStreamManager.input) no longer waits for this promise before handing over the next
+ * keystroke, so the order keystrokes reach tmux is the order these calls run synchronously — an
+ * `await` slipped in ahead of them would let a later keystroke overtake an earlier one.
*/
async writeRaw(bytes: Uint8Array): Promise<TerminalActionResult> {
+ if (this.readOnly) return terminalActionNotStarted('VIEW_ONLY')
if (this.closed) return terminalActionNotStarted('terminal stream is closed')
if (bytes.length === 0) return TERMINAL_ACTION_SUCCEEDED
- // scroll() below leaves the pane in copy-mode, which captures all keyboard input for its own
- // navigation. Cancel it before the very next real keystroke, queued ahead of the input chunks
- // (the ControlCommandQueue is strict FIFO) so the pane is back to normal by the time tmux
- // applies them — otherwise the first keystroke after a scroll silently vanishes into copy-mode
- // instead of reaching the program, trading "can't scroll" for "can't type".
const sends: Array<Promise<ControlCommandResult>> = []
- if (this.inCopyMode) {
- this.inCopyMode = false
- sends.push(this.runControlCommand(`send-keys -X -t ${this.runtime.paneId} cancel`))
- }
// 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.
for (let offset = 0; offset < bytes.length; offset += INPUT_CHUNK_BYTES) {
@@@ -702,32 -708,24 +716,33 @@@
: terminalActionNotStarted('tmux paste-buffer could not be sent')
}
- /** Scroll via tmux's own copy-mode rather than writing bytes into the pty. Some remote CLIs (e.g.
- * Claude Code) declare terminal mouse-tracking and correctly handle SGR wheel reports themselves;
- * others declare it but don't (confirmed live for Grok: it echoes the raw escape bytes into its
- * own prompt as literal characters instead of scrolling) — copy-mode works regardless, since it
- * never depends on the program in the pane understanding anything about the bytes at all. */
+ /** Scroll a full-screen TUI by sending PageUp/PageDown into the pty.
+ *
+ * Grok declares mouse-tracking but echoes SGR wheel reports as literal
+ * characters, so the desktop does not send wheel bytes. tmux copy-mode is
+ * the wrong substitute: for an alt-screen TUI, tmux history is prior
+ * full-screen repaint frames, which stacked as empty Grok chrome above the
+ * live screen. Grok's own PageUp/PageDown scroll the conversation even
+ * while the prompt is focused. */
async scroll(direction: 'up' | 'down', lines: number): Promise<TerminalActionResult> {
+ if (this.readOnly) return terminalActionNotStarted('VIEW_ONLY')
if (this.closed) return terminalActionNotStarted('terminal stream is closed')
- if (lines <= 0) return TERMINAL_ACTION_SUCCEEDED
- // Idempotent — safe even if already in copy-mode from a previous scroll.
- const entered = await this.runControlCommand(`copy-mode -t ${this.runtime.paneId}`)
- if (!entered.ok) return terminalActionNotStarted('tmux copy-mode could not be entered')
- this.inCopyMode = true
- const command = direction === 'up' ? 'scroll-up' : 'scroll-down'
- const scrolled = await this.runControlCommand(
- `send-keys -N ${lines} -X -t ${this.runtime.paneId} ${command}`,
+ const pages = tuiScrollPageCount(lines)
+ if (pages <= 0) return TERMINAL_ACTION_SUCCEEDED
+ const now = Date.now()
+ if (now - this.lastTuiScrollAt < TUI_SCROLL_MIN_INTERVAL_MS) return TERMINAL_ACTION_SUCCEEDED
+ this.lastTuiScrollAt = now
+ // Leave copy-mode if an older daemon (or a leftover gesture) parked there.
+ // `send-keys -X` fails when the pane is not in a mode; that is the common
+ // case and is ignored. Both commands are queued before either reply so
+ // cancel still runs first (the queue is FIFO) without an extra round-trip.
+ const key = direction === 'up' ? 'PageUp' : 'PageDown'
+ const cancel = this.runControlCommand(`send-keys -X -t ${this.runtime.paneId} cancel`)
+ const scrolled = this.runControlCommand(
+ `send-keys -N ${pages} -t ${this.runtime.paneId} ${key}`,
)
- if (!scrolled.ok) return terminalActionPossiblyExecuted('tmux scroll did not complete')
+ await cancel
+ if (!(await scrolled).ok) return terminalActionPossiblyExecuted('tmux TUI scroll did not complete')
return TERMINAL_ACTION_SUCCEEDED
}