fix(terminal): strip opencode's mouse DECSETs so a drag selects text again

opencode's TUI enables mouse tracking. tmux runs with `mouse off`, so it passes
the PANE's DECSETs straight through to the tmux client, and the browser's xterm
obeyed them: `mouseTrackingMode` flipped to 'any' (measured 62 none / 18 any over
16s) and xterm then reported DRAGS to the TUI instead of selecting locally.

In that state marking text produced no selection at all, so copy-on-select
silently did nothing (5/5 dead drags while `any`), and the obvious fallback —
Ctrl+C — is opencode's `app_exit`, which ended the session. Both were hit here.

opencode needs the middle strip: alt-screen toggles AND mouse DECSETs, but NOT
`3J` (a TUI is not a `clear` consumer). That is `altScreen: 'strip-mux-and-mouse'`
+ `isMuxMouseStripMode`, applied to the live stream (session.ts) and the replay
of a stored buffer, now the exported `stripReplayBuffer()` (session-routes.ts).

The browser's mouse-report gate keeps no mode list any more:
`_shouldReportMouseToCli()` reads only `cliMouseTracking`. The server sets that
flag solely in the mouse-strip branch (`_recordStrippedMouseMode`, one caller),
so it can only be true for a mode whose DECSETs are stripped, and whichever modes
the registry strips, the browser follows. Clicks still reach opencode through the
hand-encoded SGR tap it gates.

The `altScreen` JSDoc gets the decision table its three independent choices need
(alt-screen / `3J` / mouse DECSETs), written from the predicates, including that
`preserve` and `strip-mux-only` take the same runtime row. The table is pinned for
every stock CLI, with and without tmux, on both the live strip and the replay
strip, plus the published flag (test/claude-scrollback-strip.test.ts), so the two
halves cannot drift and a mis-ordered replay branch fails.

Docs and comments that still said opencode keeps its mouse reporting or gets the
narrow strip are updated (CLAUDE.md, architecture-invariants, scrollback and
copy-shortcut plans, session.ts, terminal-ui.js).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Randalix
2026-10-08 14:55:02 +02:00
co-authored by Claude Opus 5.5
parent 5a0018fc86
commit 5ba729fcbb
12 changed files with 318 additions and 85 deletions
+54 -16
View File
@@ -265,10 +265,11 @@ function cliExportsTruecolor(mode: SessionMode): boolean {
* Codex, Claude Code, and Gemini are known, controlled (Ink/React) TUIs that
* repaint via cursor positioning, so dropping the alt-screen switch is safe —
* content stays in the normal buffer. Excluded: `shell` (arbitrary programs like
* vim/less/htop legitimately need the alt screen), `opencode` (renders its own
* TUI that may rely on it), `pi` (below) and `grok` (a fullscreen alt-screen TUI
* with mouse support, i.e. the opencode case, not the Ink case). Keep parity
* with the replay-side strip in session-routes.ts.
* vim/less/htop legitimately need the alt screen), `opencode` (its own MIDDLE strip,
* isMuxMouseStripMode), `pi` (below) and `grok` (a fullscreen alt-screen TUI with
* mouse support). Keep parity with the replay-side strip (`stripReplayBuffer` in
* session-routes.ts); the table in `CliCapabilities.altScreen` is pinned for every
* stock CLI in test/claude-scrollback-strip.test.ts.
*
* ⚠️ Being excluded here does NOT preserve the alt screen. Every excluded mode
* falls through to isMuxAltScreenOnlyStripMode(), which strips the alt-screen
@@ -288,8 +289,10 @@ export function isAltScreenStripMode(mode: SessionMode): boolean {
/**
* Modes that need the NARROW strip: alt-screen toggles only, leaving `\x1b[3J`
* and the mouse-tracking DECSETs alone. Applies to every mode `isAltScreenStripMode`
* excludes, but ONLY when the session is tmux-backed (`useMux`).
* and the mouse-tracking DECSETs alone. Applies to every mode that is neither
* `strip-full` (isAltScreenStripMode) nor `strip-mux-and-mouse` (isMuxMouseStripMode),
* so `strip-mux-only` and `preserve` alike, but ONLY when the session is tmux-backed
* (`useMux`).
*
* The bug (issue #205): the tmux CLIENT emits `smcup` (`\x1b[?1049h`) as its first
* bytes on attach, before any program has run. Unstripped, xterm.js parks in the
@@ -314,7 +317,31 @@ export function isAltScreenStripMode(mode: SessionMode): boolean {
* `\x1b[3J` from a user's own `clear` is a deliberate "wipe my scrollback".
*/
export function isMuxAltScreenOnlyStripMode(mode: SessionMode, useMux: boolean): boolean {
return useMux && !isAltScreenStripMode(mode);
if (!useMux) return false;
const altScreen = getCli(mode)?.capabilities.altScreen;
return altScreen !== 'strip-full' && altScreen !== 'strip-mux-and-mouse';
}
/**
* Modes whose mouse-tracking DECSETs must be stripped, leaving `3J` alone:
* `altScreen: 'strip-mux-and-mouse'`, i.e. a mouse-capable full-screen TUI.
*
* Why this exists (opencode, measured 2026-09-16): the TUI enables tracking
* DECSETs, tmux runs with `mouse off` and therefore passes the PANE's DECSETs
* straight through to the tmux client, and the browser's xterm obeyed them —
* `mouseTrackingMode` flipped to `'any'` and xterm then reported DRAGS to the TUI
* instead of selecting locally. "Mark text, copy on select" silently did nothing
* (measured 62 `none` / 18 `any` over 16s, and 5/5 dead drags while `any`), and
* the obvious fallback — Ctrl+C — is opencode's `app_exit`, so the failure also
* ended sessions. Stripping at the source keeps xterm in selection mode; clicks
* still reach the CLI through the browser's hand-encoded tap, which this strip
* publishes as `cliMouseTracking` (`_recordStrippedMouseMode`).
*
* Gated on `useMux` for the same reason as the narrow strip: on the direct-PTY
* fallback the program's own DECSETs really do reach xterm and must be honoured.
*/
export function isMuxMouseStripMode(mode: SessionMode, useMux: boolean): boolean {
return useMux && getCli(mode)?.capabilities.altScreen === 'strip-mux-and-mouse';
}
// Note: Claude CLI PATH resolution moved to session-cli-builder.ts (buildClaudeEnv)
@@ -2489,14 +2516,21 @@ export class Session extends EventEmitter {
// redraws overwrite only the cells they target, so non-erased rows keep
// their content. Gated to Codex/Claude/Gemini (isAltScreenStripMode).
//
// Every OTHER mode (shell/opencode/antigravity) gets the NARROW strip when it
// is tmux-backed: alt-screen toggles only, because the sequence that breaks
// Every OTHER mode (shell/antigravity/pi/grok/deepseek/omp) gets the NARROW strip
// when it is tmux-backed: alt-screen toggles only, because the sequence that breaks
// scrollback there is tmux's own client-side smcup at attach, not anything the
// program in the pane emitted (issue #205, see isMuxAltScreenOnlyStripMode).
// 3J and the mouse DECSETs stay, so `clear` and mouse-aware TUIs keep working.
//
// The MIDDLE case (isMuxMouseStripMode) is a mouse-capable full-screen TUI:
// it needs smcup AND the mouse DECSETs gone — otherwise the pane's tracking
// reaches xterm and every drag becomes a mouse report instead of a text
// selection, which is what killed mark-and-copy in opencode — while 3J stays,
// because a TUI is not a `clear` consumer.
const fullStrip = isAltScreenStripMode(this.mode);
const altOnlyStrip = !fullStrip && isMuxAltScreenOnlyStripMode(this.mode, this._useMux);
if (fullStrip || altOnlyStrip) {
const mouseStrip = isMuxMouseStripMode(this.mode, this._useMux);
const altOnlyStrip = !fullStrip && !mouseStrip && isMuxAltScreenOnlyStripMode(this.mode, this._useMux);
if (fullStrip || mouseStrip || altOnlyStrip) {
// Reassemble sequences split across PTY chunk boundaries first: a chunk
// ending mid-sequence ('\x1b[?104' now, '9h' next) would slip past the
// strip below and leave xterm stuck in the scrollback-less alt buffer
@@ -2515,14 +2549,18 @@ export class Session extends EventEmitter {
// eslint-disable-next-line no-control-regex
data = data.replace(/\x1b\[\?(?:47|1047|1049)[hl]/g, '');
if (fullStrip) {
data = data
// eslint-disable-next-line no-control-regex
data = data.replace(/\x1b\[3J/g, '');
}
if (fullStrip || mouseStrip) {
data = data.replace(
// eslint-disable-next-line no-control-regex
.replace(/\x1b\[3J/g, '')
// eslint-disable-next-line no-control-regex
.replace(/\x1b\[\?(?:1000|1001|1002|1003|1005|1006|1007)[hl]/g, (seq) => {
/\x1b\[\?(?:1000|1001|1002|1003|1005|1006|1007)[hl]/g,
(seq) => {
this._recordStrippedMouseMode(seq);
return '';
});
}
);
}
}