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.

refactor: abstract tmux mouse configuration into a reusable helper and retroactively apply it to existing panes during discovery

0xken1591458dc3b618
diff --git a/cli/src/lib/tmux.ts b/cli/src/lib/tmux.ts
index 4062e2ea6..d251243e3 100644
--- a/cli/src/lib/tmux.ts
+++ b/cli/src/lib/tmux.ts
@@ -595,6 +595,20 @@ function tmuxLoadBuffer(name: string, content: string): Promise<boolean> {
})
}
+/**
+ * Session-scoped (no `-g`) so this never touches tmux mouse behavior outside sessions this app
+ * created — `-t` resolves a pane id up to its session's option table. Without this, scrolling over a
+ * program in the alternate screen buffer (Claude Code's own TUI, most chat CLIs) is forwarded to
+ * that program as raw wheel/arrow-key bytes instead of being caught by tmux, which mostly has no
+ * binding for it — scrolling silently does nothing. Idempotent and best-effort: safe to call on
+ * every discovery scan, including for a pane whose mouse option is already on.
+ */
+export function setPaneMouseOn(pane: string): Promise<void> {
+ return new Promise((resolve) => {
+ execFile('tmux', ['set-option', '-t', pane, 'mouse', 'on'], { timeout: 2_000 }, () => resolve())
+ })
+}
+
function tmuxDeleteBuffer(name: string): Promise<void> {
return new Promise((resolve) => {
execFile('tmux', ['delete-buffer', '-b', name], { timeout: 2_000 }, () => resolve())
diff --git a/cli/src/lib/tmuxAgentDiscovery.ts b/cli/src/lib/tmuxAgentDiscovery.ts
index 9b4133eb8..0a9aff830 100644
--- a/cli/src/lib/tmuxAgentDiscovery.ts
+++ b/cli/src/lib/tmuxAgentDiscovery.ts
@@ -22,6 +22,7 @@ import {
enrichProcessRows,
parseProcessRow,
resumeSessionId,
+ setPaneMouseOn,
type ProcessRow,
} from './tmux.js'
@@ -256,6 +257,10 @@ export class TmuxAgentReconciler {
private timer: NodeJS.Timeout | null = null
private readonly hints = new Map<string, AgentEngine>()
private readonly warnedAmbiguousPanes = new Set<string>()
+ /** Panes already confirmed `mouse on` this run — set once per pane, not re-checked every scan. A
+ * pane surviving a daemon restart (or created by an older build, before this existed) starts
+ * outside this set, so the very next scan that sees it fixes it — not just brand-new panes. */
+ private readonly mouseEnabledPanes = new Set<string>()
constructor(private readonly deps: TmuxAgentDiscoveryDeps) {}
@@ -301,6 +306,17 @@ export class TmuxAgentReconciler {
console.warn(`[discovery] ${probe.error}; keeping ${this.deps.current().length} agent(s)`)
return
}
+ // Retroactive: a pane from an older build (before tmuxBackend.create() started doing this itself)
+ // or one that survived a daemon restart never got `mouse on` set. Fire-and-forget — a scan that
+ // misses one because tmux was briefly slow just catches it on the next pass.
+ for (const pane of probe.panes) {
+ if (this.mouseEnabledPanes.has(pane)) continue
+ this.mouseEnabledPanes.add(pane)
+ void setPaneMouseOn(pane)
+ }
+ for (const pane of [...this.mouseEnabledPanes]) {
+ if (!probe.panes.has(pane)) this.mouseEnabledPanes.delete(pane)
+ }
for (const pane of hints.keys()) this.hints.delete(pane)
for (const pane of probe.ambiguousPanes) {
if (this.warnedAmbiguousPanes.has(pane)) continue
diff --git a/cli/src/lib/tmuxBackend.ts b/cli/src/lib/tmuxBackend.ts
index f79bcdeaf..e2588af41 100644
--- a/cli/src/lib/tmuxBackend.ts
+++ b/cli/src/lib/tmuxBackend.ts
@@ -26,6 +26,7 @@ import {
sendKeyToTmux,
sendLiteralToTmux,
sendToTmux,
+ setPaneMouseOn,
} from './tmux.js'
import { listTmuxPanes } from './tmuxAgentDiscovery.js'
import { terminalRouteKey } from './terminalRuntime.js'
@@ -93,15 +94,7 @@ export class TmuxBackend implements TerminalBackend<TmuxRuntimeRef> {
if (!/^%\d+$/.test(paneId)) {
return terminalActionPossiblyExecuted('tmux created a session without returning its root pane')
}
- // Without this, a scroll gesture over a program in the alternate screen buffer (Claude Code's
- // own TUI, most chat CLIs) is forwarded to that program as raw wheel/arrow-key bytes instead of
- // being caught by tmux — the program mostly has no binding for it, so scrolling silently does
- // nothing. `mouse on` makes tmux itself catch the wheel event and enter copy-mode, which DOES
- // keep scrollback for the alternate screen. Session-scoped (target is the pane tmux just
- // returned, no -g) so this never touches tmux mouse behavior outside sessions this app creates.
- await new Promise<void>((resolve) => {
- execFile('tmux', ['set-option', '-t', paneId, 'mouse', 'on'], { timeout: 2_000 }, () => resolve())
- })
+ await setPaneMouseOn(paneId)
return { state: 'succeeded', dispatch: 'executed', runtime: { backend: 'tmux', paneId } }
}