Skip to content

fix(terminal): the mouse reaches a child that reads it - #312

Merged
bastien-gallay merged 3 commits into
mainfrom
fix+155-mouse-forward
Sep 16, 2026
Merged

bastien-gallay merged 3 commits into
mainfrom
fix+155-mouse-forward

Conversation

@bastien-gallay

Copy link
Copy Markdown
Collaborator

Closes #155

What

A program with mouse reporting on — Claude Code's /diff and /resume, vim, lazygit, fzf, less, tmux — now receives presses, releases, drags and (under 1003) bare moves, in the SGR or X10 encoding it negotiated. Only wheel notches were ever forwarded; a press was consumed as a local text selection and the child never heard of it.

How — one predicate, three readers

                 core::app::pointer
    PointerEvent::route(Option<MouseReporting>) -> PointerRoute
          Forward | Select(LocalGesture) | Nothing
                 ▲              ▲              ▲
     pty thread  │   shell (MCP)│      canvas  │
   live TermMode │ Screen.mouse_│  Screen.mouse_reporting
 (pointer_input) │  reporting   │  (+ Shift / link modifier = local)
                 │              │
   Forward ──► mouse_bytes ──► write PTY      forwarded
   Select  ──► pointer_select (offset live)   selection
   Nothing ──► dropped                        ignored
  • MouseReporting { Click, Drag, Motion } is the DECSET 1000/1002/1003 ladder; route never answers Select under any reporting (proptest) — the mouse is the child's, a motion it did not ask for is dropped, not selected.
  • The mode bits are read once, in a new pty::mode leaf: input (encoder gate) and grid (Screen.mouse_reporting) both need it and the module-boundary script forbids either importing the other.
  • wheel_bytes' SGR/X10 body became the shared mouse_report; mouse_bytes extends it to buttons and motion. It only encodes — the gate is route's, asked by the caller.
  • Canvas: bare press/drag/release/move → Message::TermPointer; Shift (xterm override) and the link modifier keep the pointer for the terminal's own gestures. Double-click word, shift-extend and link click never left the canvas.
  • MCP: PointerOutcome::Forwarded; the shell answers off the last Screen, the same lag class as its bounds check; the terminal decides again on its live mode.

Out of scope, on purpose: modifier bits in the report (the event lost modifiers in #311; it comes back as one shared type when a caller needs it), and folding the wheel into PointerKind (its scrollback/alt-scroll fallback has no pointer equivalent).

Tests

30 new: route table + PBT (core), SGR/X10 encoder incl. X10 saturation (pty::input), mode ladder (pty::mode), pointer_input pure + a terminal-thread test asserting the bytes a click writes to the PTY (pty::session), snapshot ladder (pty::grid), forwarded outcome (shell), the three words (handler), and the canvas hand-off: buttons, drag ≥ Drag, move = Motion, release after a forwarded press, Shift override, right press outranking paste.

Mutants on the changed files: 103 caught · 0 missed in the diff (the 50 survivors are pre-existing draw/live_hover arithmetic in canvas.rs, swept because -f is file-level). One survivor in the diff was a kind != Move guard the link-modifier early return made unreachable; removed.

Docs

Book: mcp/live-bridge.md (third answer, who decides, gaps table), workspace/terminal.md ("When the program reads the mouse"). AGENTS, CHANGELOG, roadmap entries that named #155 as blocked.

Verification beyond CI

The rung this builds on (#300) exists so an agent can verify this: mouse_in_session over a /resume list in a Claude session should answer forwarded and move the highlight. Not yet run against a live TUI in this PR — a manual pass from the release app is the remaining check.

Only wheel notches were ever encoded to the PTY. A press, release, drag or
motion over a TUI with mouse reporting on was consumed as a local text
selection and the child never heard of it — so Claude Code's /diff and
/resume, vim, lazygit, fzf and every other mouse-mode program got a wheel
and nothing else.

One predicate, three readers. `core::app::pointer` gains `MouseReporting`
(the DECSET 1000 / 1002 / 1003 ladder) and `PointerEvent::route`, which
answers Forward / Select / Nothing: under any mouse reporting the mouse is
the child's, so what its mode covers is forwarded and the rest is dropped
rather than selected. The terminal thread routes on its live mode
(`session::pointer_input`), the MCP shell answers `forwarded` off the last
`Screen`, and the canvas hands the bare gestures to the child through
`Message::TermPointer` while a held modifier — Shift, the xterm override,
or the link modifier — keeps the pointer for the terminal's own gestures.

The mode bits are read in one place, a new `pty::mode` leaf both `input`
and `grid` may import, since neither may import the other; `Screen` carries
the reading as `mouse_reporting`. `wheel_bytes`' SGR/X10 body becomes the
shared `mouse_report`, which `mouse_bytes` extends to press, release, drag
and motion — no second encoder. The write path is one `forward_mouse`.

Closes #155
…rded mouse

`mouse_in_session` documents its third answer and which of the two the
program decides; the terminal page gains "When the program reads the
mouse" with the Shift override; the live-bridge gaps table loses #155.
AGENTS records the one-predicate-three-readers rule, the `pty::mode` leaf
and why it exists, and where modifiers went. The roadmap entries that
named #155 as blocked read it as fixed.

Mutation testing on the changed files left one survivor in the diff, a
`kind != Move` guard in the canvas hand-off that the link-modifier early
return made unreachable; the guard is gone rather than tested.
The end-to-end claim behind the rung, made against a spawned shell running
cat -v with mouse reporting on: the Screen reports the mode, the pointer
is routed and encoded, and the child prints the SGR click it was sent.
@bastien-gallay
bastien-gallay merged commit f1f6e65 into main Sep 16, 2026
20 checks passed
@bastien-gallay
bastien-gallay deleted the fix+155-mouse-forward branch September 16, 2026 15:56
bastien-gallay added a commit that referenced this pull request Sep 16, 2026
…reaching the child (#314)

* docs: the README, the book's tool count and four other surfaces catch up with the pointer reaching the child

The two pointer PRs (#311, #312) and the review pass (#313) updated the
book, AGENTS, the roadmap and the changelog and none of them opened
README.md or docs/src/mcp/index.md. The README tool table listed fifteen
tools with no mouse_in_session, its follow-up sentence still said the
surface could press keys but not click and counted #300 among what
remained, and its gesture paragraph promised a drag selects with no word
for a program that owns the mouse; the book's index counted fifteen twice.
live-bridge.md's follow-up header undercounted its own five-row table,
PtyHost::pointer's doc-comment spoke of the mode gate in the future tense,
f-mcp-pointer-terminal said Blocks for a bug that is closed, and
f-mcp-attach named the tool count as fifteen in two places — a number that
rots on every rung, now phrased as the set the spawned session gets.

* docs(agents): an MCP rung owes the README table and the book's tool count — four in a row shipped without them

Every rung merged since the README tool table was written (#215, #229,
#281, #300) landed without touching README.md, each repaired by a later
pass; verified with git show --stat on the four squashes. The book table
in AGENTS now names README's table and follow-up sentence and the count
in docs/src/mcp/index.md on the MCP-tool row, and .wrap.md records the
fourth miss on the sections it already listed.
bastien-gallay added a commit that referenced this pull request Sep 22, 2026
… keeps no cache (#318)

* docs: the copy chord's cache fallback is a defect where a program owns the mouse (#316)

A drag in a Claude Code tab is the program's since #312, so the terminal
never has a visible selection there — and the copy chord then writes its
last-copied cache over the copy the program just made, reporting `ran`.
Filed as #316; this caveats every surface that promised otherwise, adds the
defect to each "what remains" list, and records the lesson beside the cache
bullet in AGENTS.md. Nothing here describes the fix, which is still open.

* fix(terminal): the copy chord asks the terminal for its selection and keeps no cache

In a pane whose program reads the mouse an unshifted drag is the program's,
so the terminal never selects there — and the chord, finding no visible
spans, fell back to `Shell::selection`, a cache of the last text termherd
had copied, and wrote it over the clipboard the program had just filled.
Once anything had been copied anywhere, every chord in every such pane did
this and reported `ran`.

The terminal owns the selection, so it now answers whether one exists:
`Screen::has_selection`, true for a non-empty selection whose rows the
emulator still holds, on screen or scrolled into history — and false once a
scroll past the scrollback rotated the selection off the rows it covered,
where `selection_to_string` has nothing to read. The chord gates on that and
sends `CopyTerminalSelection`, or refuses `inert / no-context`. The cache and
both its writers are gone.

The three-state copy test becomes two states on the screen's flag; the
"prefers the live selection to the cache" test retires with the cache; two
tests pin the scrolled-out copy and the inert chord after an earlier copy.
Mutation testing on the diff: 12 mutants, 2 caught, 0 missed.

Closes #316

* docs: the copy chord's caveats become the fixed behaviour (#316)

The caveats the previous commit's sibling PR (#317) put on eight surfaces
described an open defect; with the cache gone they describe the fix
instead, and #316 leaves every "what remains" list. A changelog entry
under Unreleased names the user-visible change, and the quality-bar bullet
in AGENTS.md carries the lesson: a fallback that fires whenever a
precondition is absent becomes a bug the day a feature makes that
precondition permanently absent.

* docs(wrap): caveats for a defect about to be fixed stay on the branch, not in their own PR

PR #317 shipped eight caveats for #316 and was superseded by the fix PR an hour later; the note records the rule so the next wrap holds them instead.

* docs(agents): the copy-cache bullet speaks of the cache in the past tense

The bullet described the reordering that kept the cache as the current design; the cache is gone since the previous commit on this branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mouse clicks are never forwarded to the child process (Claude Code /diff, vim, lazygit…)

1 participant