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.

feat: update tmux snapshot capture to use escaped cell sequences for preserved SGR styling and fix mouse mode restoration

0xken153447c3e64345
diff --git a/cli/src/lib/tmuxStream.real.spec.ts b/cli/src/lib/tmuxStream.real.spec.ts
index 37b097828..c156a760d 100644
--- a/cli/src/lib/tmuxStream.real.spec.ts
+++ b/cli/src/lib/tmuxStream.real.spec.ts
@@ -45,9 +45,10 @@ run('TmuxControlStream real tmux', () => {
if (opened.state !== 'succeeded') return
const historyMarker = 'HARNESS_OLD_TUI_FRAME_MUST_NOT_REPLAY'
+ const styledHistoryMarker = 'HARNESS_STYLED_HISTORY'
const snapshotMarker = 'HARNESS_SNAPSHOT_OK'
await opened.value.writeRaw(Buffer.from(
- `printf '${historyMarker}\\n'; for i in {1..40}; do printf 'filler-%s\\n' "$i"; done; printf '${snapshotMarker}\\n'\r`,
+ `printf '\\033[31m${styledHistoryMarker}\\033[0m\\n${historyMarker}\\n'; for i in {1..40}; do printf 'filler-%s\\n' "$i"; done; printf '${snapshotMarker}\\n'\r`,
))
await eventually(async () => (await tmux(['capture-pane', '-p', '-t', paneId])).includes(snapshotMarker))
opened.value.beginSnapshot()
@@ -59,6 +60,7 @@ run('TmuxControlStream real tmux', () => {
expect(Buffer.from(snapshot.value.bytes).includes(Buffer.from('\u001bc'))).toBe(true)
expect(Buffer.from(snapshot.value.bytes).includes(Buffer.from(snapshotMarker))).toBe(true)
expect(Buffer.from(snapshot.value.bytes).includes(Buffer.from(historyMarker))).toBe(true)
+ expect(Buffer.from(snapshot.value.bytes).includes(Buffer.from(`\u001b[31m${styledHistoryMarker}`))).toBe(true)
}
const postCutMarker = 'HARNESS_POST_CUT_OK'
diff --git a/cli/src/lib/tmuxStream.spec.ts b/cli/src/lib/tmuxStream.spec.ts
index 4192a285c..6ae79d88f 100644
--- a/cli/src/lib/tmuxStream.spec.ts
+++ b/cli/src/lib/tmuxStream.spec.ts
@@ -69,10 +69,10 @@ describe('tmux snapshot row normalization', () => {
expect(snapshot).toContain('\u001b[2;5H\u001b[?7h\u001b[?25h')
})
- it('seeds inert plain history before rebuilding the visible grid', () => {
- const history = Buffer.from('old-frame-1\nold-frame-2\n')
+ it('seeds styled cell history before rebuilding the visible grid', () => {
+ const history = Buffer.from('\u001b[31mold-frame-1\u001b[39m\nold-frame-2\n')
expect(Buffer.from(normalizeTmuxHistoryLines(history)).toString()).toBe(
- 'old-frame-1\u001b[0m\r\nold-frame-2\u001b[0m\r\n',
+ '\u001b[31mold-frame-1\u001b[39m\u001b[0m\r\nold-frame-2\u001b[0m\r\n',
)
const snapshot = Buffer.from(synthesizeTmuxSnapshot(
Buffer.from('current-screen\n'),
@@ -89,4 +89,19 @@ describe('tmux snapshot row normalization', () => {
expect(snapshot.indexOf('old-frame-1')).toBeLessThan(snapshot.indexOf('current-screen'))
expect(snapshot).toContain('old-frame-2\u001b[0m\r\n\r\n\r\n\u001b[H\u001b[2J')
})
+
+ it('restores tmux mouse modes even when no scrollback exists', () => {
+ const snapshot = Buffer.from(synthesizeTmuxSnapshot(
+ Buffer.from('alternate-screen\n'),
+ {
+ sessionId: '$1', windowId: '@1', windowPanes: 1,
+ windowWidth: 80, windowHeight: 2, paneWidth: 80, paneHeight: 2,
+ alternateOn: true, cursorX: 0, cursorY: 0,
+ mouseStandard: false, mouseButton: false, mouseAll: true,
+ mouseUtf8: false, mouseSgr: true,
+ },
+ )).toString()
+
+ expect(snapshot).toContain('\u001b[H\u001b[2J\u001b[?1003h\u001b[?1006h')
+ })
})
diff --git a/cli/src/lib/tmuxStream.ts b/cli/src/lib/tmuxStream.ts
index 0db685309..c97709fb9 100644
--- a/cli/src/lib/tmuxStream.ts
+++ b/cli/src/lib/tmuxStream.ts
@@ -223,13 +223,14 @@ export function synthesizeTmuxSnapshot(
)
const normalizedHistory = normalizeTmuxHistoryLines(history)
// Push every captured history row above the viewport before clearing the
- // visible grid. History is intentionally captured without `-e`, so old TUI
- // cursor/repaint sequences become inert text instead of corrupting the new
- // emulator. Extra blank rows move even the newest history lines into local
- // scrollback; ESC[2J clears only the viewport, not that scrollback.
- const historySuffix = normalizedHistory.length === 0
- ? Buffer.alloc(0)
- : Buffer.from(`${'\r\n'.repeat(meta.paneHeight)}\u001b[H\u001b[2J${mouseModes(meta)}`, 'utf8')
+ // visible grid. `capture-pane -e` serializes tmux's already-rendered cells:
+ // it preserves SGR colours but contains none of the original TUI's cursor
+ // movement/repaint stream. Extra blank rows move even the newest history
+ // lines into local scrollback; ESC[2J clears only the viewport, not it.
+ const historySuffix = Buffer.from(
+ `${normalizedHistory.length === 0 ? '' : '\r\n'.repeat(meta.paneHeight)}\u001b[H\u001b[2J${mouseModes(meta)}`,
+ 'utf8',
+ )
const raw = Buffer.from(capture)
const rows: Buffer[] = []
let start = 0
@@ -512,6 +513,15 @@ export class TmuxControlStream implements TerminalStreamHandle<TmuxRuntimeRef> {
if (!metadata.ok) return { state: 'failed', reason: 'tmux snapshot metadata is unavailable' }
const meta = parsePaneMeta(metadata.stdout)
if (!meta || meta.windowPanes !== 1) return { state: 'failed', reason: 'tmux snapshot metadata is invalid' }
+ // History is a styled cell capture used only to seed the receiver's
+ // local scrollback. It preserves SGR attributes without replaying old
+ // TUI cursor movement. Read it before the authoritative visible-grid cut
+ // so concurrent output is included there or in a post-cut delta.
+ const history = meta.alternateOn
+ ? Buffer.alloc(0)
+ : (await this.runControlCommand(
+ `capture-pane -p -e -N -t ${this.runtime.paneId} -S -${SNAPSHOT_HISTORY_LINES} -E -1`,
+ )).stdout
const capture = await this.runControlCommand(baseCapture, () => {
// `%end` is the ordered cut. Notifications observed before it are
// represented by capture-pane; only later output may follow the keyframe.
@@ -519,11 +529,6 @@ export class TmuxControlStream implements TerminalStreamHandle<TmuxRuntimeRef> {
this.snapshotPostCutBytes = 0
})
if (!capture.ok) return { state: 'failed', reason: 'tmux snapshot failed' }
- const history = meta.alternateOn
- ? Buffer.alloc(0)
- : (await this.runControlCommand(
- `capture-pane -p -N -t ${this.runtime.paneId} -S -${SNAPSHOT_HISTORY_LINES} -E -1`,
- )).stdout
return {
state: 'succeeded',
value: {