Repository navigation
fix(terminal): drop the agent gutter from copied selections (#19770) - #20545
Conversation
xterm selections are screen cells, not logical text. Agent CLIs paint their messages behind a fixed left gutter, so every copied line carried that gutter into the clipboard and pasted replies came out indented. Terminal clipboard writes now drop the run of spaces that *every* selected line shares, so relative indentation (nested bullets, fenced code, YAML) survives and only the gutter is lost. A selection that starts mid-line, or that includes any column-0 line, has a shared run of zero and is copied verbatim. Applied at every terminal clipboard seam: the Cmd/Ctrl+C shortcut, the pane context menu's Copy, right-click-to-copy, the app menu's Copy, copy-on-select, the X11 primary selection, the dashboard popout's preview terminal, and mobile's selection Copy button. New "Trim Gutter on Copy" terminal setting (default on) restores the old verbatim-cell behaviour.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (7)
Included review availability: Your plan provides up to 10 included reviews per hour; 5 remain after this review. 📝 WalkthroughWalkthroughThe change adds an enabled-by-default Priority: ⬇️ Low Severity of issue fixed: Low Merge Risk: 🟡 Moderate · up to Some copied selections may still lose intended indentation, and a mobile reconnection can temporarily preserve the previous host’s disabled copy setting. These behavior risks should be resolved before merge. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: b38a7766-41b6-41f6-b64c-678333cc9978
📒 Files selected for processing (15)
mobile/src/session/use-mobile-session-accessory-selection.tssrc/renderer/src/components/dashboard-popout/preview-terminal-key-handler.tssrc/renderer/src/components/settings/TerminalInteractionSection.tsxsrc/renderer/src/components/settings/TerminalScrollSpeedSlider.tsxsrc/renderer/src/components/settings/terminal-clipboard-search.tssrc/renderer/src/components/terminal-pane/terminal-clipboard-selection-text.test.tssrc/renderer/src/components/terminal-pane/terminal-clipboard-selection-text.tssrc/renderer/src/components/terminal-pane/terminal-pane-menu-copy-actions.tssrc/renderer/src/components/terminal-pane/terminal-pane-pane-links.tssrc/renderer/src/components/terminal-pane/terminal-selection-copy.tssrc/renderer/src/i18n/locales/en.jsonsrc/shared/default-global-settings.tssrc/shared/global-settings-types.tssrc/shared/terminal-selection-gutter.test.tssrc/shared/terminal-selection-gutter.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
There was a problem hiding this comment.
ℹ️ No critical issues — minor suggestions inline.
Reviewed changes
- Shared gutter rule — new pure
stripTerminalSelectionGutter/measureTerminalSelectionGutterdrops the space run shared by every non-blank line; unit-tested across CRLF joins, blank/whitespace-only lines, uneven indents, empty input, and tabs. - Clipboard seam wiring — every desktop/web terminal copy path now reads through
readTerminalClipboardSelection, so the rule applies to the shortcut, context menu, right-click, app menu, copy-on-select, X11 primary selection, the dashboard popout preview, and mobile. - New setting —
terminalCopyTrimsGutter(default on) added to the settings type and defaults, the Terminal Interaction pane, settings search, anden.json;ScrollSpeedSliderwas extracted soTerminalInteractionSection.tsxstays under the line cap.
ℹ️ Default-on trimming also drops real indentation on single-line selections
For a selection whose only non-blank line is one line, the "shared run" equals that line's entire leading whitespace, so all of it is removed — even when it was genuine indentation rather than the agent gutter. A user copying one indented line from a diff, cat output, or a code block in any terminal loses the indent entirely, and terminalCopyTrimsGutter is the only escape hatch. This is pinned intentionally by the "single wrapped line" test, so it reads as a scope question rather than a defect, but it is not covered by the PR's stated guarantee that relative indentation survives.
Technical details
# Single-line selections have no relative indentation to preserve
## Affected sites
- `src/shared/terminal-selection-gutter.ts:19` — `measureTerminalSelectionGutter` returns the lone line's full leading-space count.
- `src/shared/terminal-selection-gutter.ts:51` — `stripTerminalSelectionGutter` then slices it off.
## Required outcome
- Decide whether a single full-line selection should be trimmed at all, or document the behavior so users understand the setting is the escape hatch.
## Open questions for the human
- Is the single-line case intentionally in scope (one visible wrapped agent row), accepting that a lone line of genuine indented output is trimmed too?ℹ️ Nitpicks
- The test
still reports no selection when the gutter is all there was(terminal-clipboard-selection-text.test.ts:50) actually exercises the empty-selection guard (getSelection: () => ''). A gutter-only selection is whitespace-only, which the shared rule deliberately leaves untouched, so it is still copied — the name suggests the opposite. Rename to match what it asserts.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
Mobile stripped the gutter unconditionally, so turning "Trim Gutter on Copy" off left one surface still rewriting the clipboard. Mobile now mirrors the desktop preference through the existing settings.get RPC — a host predating the setting sends no key, which reads as on, matching the desktop default. Also folds the single-use gutter helpers into their callers so the shared module exposes one function.
Also locks the Windows subtlety with a test: a blank CRLF row is '\r', which reads as a zero-indent content row and would cancel the gutter unless the CR is split off first.
|
On the single-line case: intentional, and it's the main case the fix exists for. xterm rejoins soft-wrapped rows into one logical line ( The test name is fixed ( |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 63a28579-bc19-45dd-a08f-d294fb2bb7b6
📒 Files selected for processing (9)
mobile/src/session/mobile-session-route-parity.test.tsmobile/src/session/use-mobile-session-accessory-selection.tsmobile/src/terminal/terminal-copy-gutter-preference.tsmobile/src/transport/settings-read-operations.test.tsmobile/src/transport/settings-read-operations.tssrc/renderer/src/components/terminal-pane/terminal-clipboard-selection-text.test.tssrc/renderer/src/components/terminal-pane/terminal-clipboard-selection-text.tssrc/shared/terminal-selection-gutter.test.tssrc/shared/terminal-selection-gutter.ts
🚧 Files skipped from review as they are similar to previous changes (2)
- src/renderer/src/components/terminal-pane/terminal-clipboard-selection-text.test.ts
- mobile/src/session/use-mobile-session-accessory-selection.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
| ): RefObject<boolean> { | ||
| const trimsGutterRef = useRef(true) | ||
|
|
||
| useEffect(() => { |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Reset the default before each new preference read.
If a previous connection returned false, trimsGutterRef.current stays false for the next connection. A copy before the new request resolves, or after it fails, then preserves the gutter instead of using the documented enabled default.
Set trimsGutterRef.current = true after the connected-client guard and before the request starts.
Proposed fix
if (!client || connState !== 'connected') {
return
}
+ trimsGutterRef.current = true
let stale = falseThere was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewing the delta since the prior pullfrog review (868a2030ef) — the new commit a3fe210fb8:
- Mobile honors the setting — copy now routes through the new
useTerminalCopyTrimsGutterhook, which readsterminalCopyTrimsGutterover the existingsettings.getRPC once per connection and defaults to trim, so turning the desktop toggle off yields verbatim screen cells on mobile too. - New mobile RPC reader —
terminalCopyTrimsGutterRead(settings.terminal-copy-trims-gutter-or-skip) treats an absent key as on to match the desktop default, with new cases insettings-read-operations.test.tscovering on/off/absent/null/refused. - Shared rule refactor —
stripTerminalSelectionGutterinlines measurement (measureGutter/measureIndent/splitTerminator) and drops the now-unusedmeasureTerminalSelectionGutterexport; behavior is unchanged. - Desktop wrapper inlined —
applyTerminalClipboardGutterSettingis folded intoreadTerminalClipboardSelection, keeping the=== falseopt-out. - Parity + test upkeep — route-parity hook count/hashes regenerated for the new hook, and the misleading "gutter is all there was" test renamed to match its empty-selection assertion (the prior review's nitpick).
Verified locally: src/shared/terminal-selection-gutter.test.ts (12), mobile/src/transport/settings-read-operations.test.ts (18), and mobile/src/session/mobile-session-route-parity.test.ts (3) all pass. Mobile tsc reports only the pre-existing missing *-engine.generated modules.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewing the delta since the prior pullfrog review (a3fe210fb8) — the new commit 9cc4be9881:
- Parsed each selection line once —
parseLinereturns aSelectionLine(indent/text/terminator) so the indent is measured once and reused bymeasureGutterand the strip, replacing the map-then-remeasure pass; behavior is unchanged. - Added a Windows CRLF regression test — a blank CRLF row (
'\r') reads as a zero-indent content row and would cancel the gutter if the CR weren't split off first; now pinned. - Added a wide/emoji content test — confirms the space-only rule leaves multibyte content untouched.
Verified locally: src/shared/terminal-selection-gutter.test.ts (14 tests) passes.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.
⚠️ Outside diff range comments (1)
src/renderer/src/components/terminal-pane/terminal-clipboard-selection-text.ts (1)
9-17: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winreadTerminalClipboardSelection trims every selection whose returned lines share leading spaces, but it has no selection-start column. A mid-line selection that starts on meaningful spaces can therefore be normalized as gutter text and lose those selected characters in the desktop copy and primary-selection paths. Use the terminal selection position to bypass trimming unless the selection starts at column zero.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: e5217c45-643b-4e5e-ab67-622756475889
📒 Files selected for processing (2)
src/shared/terminal-selection-gutter.test.tssrc/shared/terminal-selection-gutter.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review.
settings.get is an explicit allowlist projection, not the whole settings object, so terminalCopyTrimsGutter never reached mobile: the client read the key as absent, which means "older host", which means on. Mobile therefore always trimmed and the desktop opt-out was inert. Adds the field to the projection and a test that fails if it is ever dropped again — absence is indistinguishable on the client from an old host, so a silent regression here has no other signal.
A repo-wide `pnpm format` swept a quote-style change in pnpm-workspace.yaml and a blank line in source-tree-walk.test.ts into this branch; neither is related to the gutter fix.
xterm binds its own DOM `copy` listener that writes raw screen cells (CoreBrowserTerminal `_initGlobal`). Orca's own chords never reach it — they preventDefault in keydown — but Ctrl+Insert is a Chromium copy accelerator on Windows/Linux and is not in `terminal.copySelection`'s bindings, so it still copied the gutter. Orca binds Shift+Insert for paste on those platforms, which makes the asymmetry worse. A capture-phase listener on the xterm element now writes the trimmed text, closing the class rather than the one chord: any native copy event — assistive tech, execCommand — lands on the same path. Installed for both terminal panes and the dashboard popout's preview terminal.
44e379d to
19e3168
Compare
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewing the delta since the prior pullfrog review (9cc4be9881) — the new commit 0dbe84f6bd:
- Publishes the gutter-trim setting to paired clients —
terminalCopyTrimsGutteris added to theRuntimeClientSettingsallowlist projection returned bysettings.get, with!== falsesemantics. Before this, the key was absent, which mobile'sterminalCopyTrimsGutterReadreads as an older host and therefore as on, so the desktop opt-out was inert on mobile. - Adds a projection regression test —
runtime-client-settings-terminal-copy-projection.test.tspins false / true / absent; it would fail if the field were dropped from the projection again (the exact silent-regression shape the commit describes). - Types the field on the store contract —
RuntimeStore.getSettings()now carriesterminalCopyTrimsGutter. - Tightens the gutter rule's doc comment — states that xterm's
selectionTextgetter folds every NBSP cell to a plain space. Verified againstnode_modules/@xterm/xterm/src/browser/services/SelectionService.ts:255-259; the claim is accurate. - Incidental, non-behavioral formatting also landed in this commit:
pnpm-workspace.yamlquote normalization and one stray blank line removed fromsource-tree-walk.test.ts.
Verified locally: runtime-client-settings-terminal-copy-projection.test.ts (3) and runtime-client-settings-minimax-projection.test.ts (3) pass; pnpm tc:node passes.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

Fixes #19770.
The bug
An xterm selection is a rectangle of screen cells, not logical text. Agent CLIs paint their messages behind a fixed left gutter, so
terminal.getSelection()hands back every gutter space along with the words — and every terminal clipboard path in Orca wrote that string straight to the clipboard.Reporter's symptom: "I paste replies to customers straight from the terminal, and each one comes out indented."
The fix
Terminal clipboard writes now drop the run of spaces that every selected line shares.
That rule is deliberately conservative, and it is the whole design:
Applied at every terminal clipboard seam, not just the shortcut:
Cmd/Ctrl+Cshortcutterminal-keyboard-action-dispatch.ts→terminal-selection-copy.tsterminal-pane-menu-copy-actions.tsuse-terminal-context-menu-trigger.tsterminal-pane-paste-listeners.tsterminal-pane-pane-links.tsterminal-pane-pane-links.tspreview-terminal-key-handler.ts,preview-terminal-app-menu-clipboard.ts,preview-terminal-right-click-paste.tsuse-mobile-session-accessory-selection.tscopyevent (Ctrl+Insert on Windows/Linux, assistive tech,execCommand)terminal-native-copy-gutter.tsThat last row is the one that is easy to miss: xterm binds its own DOM
copylistener that writes raw screen cells (CoreBrowserTerminal_initGlobal). Orca's own chords never reach it — theypreventDefaultin keydown — butCtrl+Insertis a Chromium copy accelerator on Windows/Linux and is not interminal.copySelection's bindings (definitions-core-3.ts:206-209), while Orca does bindShift+Insertfor paste there. A capture-phase listener on the xterm element now writes the trimmed text, which closes the whole class rather than that one chord.The rule itself lives in one pure module,
src/shared/terminal-selection-gutter.ts, shared by desktop/web and mobile. A new terminal setting, Trim Gutter on Copy (default on), restores the old verbatim-cell behaviour; mobile mirrors that preference over the existingsettings.getRPC, and a host predating the setting sends no key, which reads as on to match the desktop default.Proof
Everything below was captured against this branch running as a real Electron build (background launch, CDP-driven), using a real mouse drag over the pane and a real
Cmd+C, then reading the system clipboard withpbpaste | cat -vet($marks end-of-line so leading spaces are unambiguous).The selection, as the user makes it
The highlight starts at the pane's left edge and swallows the 2-space gutter:
Before → after, same build, same drag
Toggling the new setting exercises both code paths in one running app.
Trim Gutter on CopyOFF — today's shipping behaviour, bug reproduced. Every copied line carries the gutter:Trim Gutter on CopyON — the fix. Same pane, same drag, sameCmd+C:Relative indentation survives
The 2-space gutter is gone; the nested 2/4-space structure is untouched.
The context-menu path too
Same selection, copied via the pane context menu's Copy instead of the shortcut — identical clipboard:
A column-0 line in the selection is copied verbatim
Extending the same drag up to include the marker line, whose glyph sits at column 0:
Nothing is stripped — the shared run is 0. This is the conservative boundary, shown on purpose.
The setting
Discoverable from settings search, default on:
Automated
src/shared/terminal-selection-gutter.test.ts— 12 cases: agent-body gutter, single wrapped line, nested indentation, mid-line start, unindented shell output, blank/whitespace-only lines, all-whitespace selection, CRLF joins, uneven indents, empty selection, tabs.src/renderer/src/components/terminal-pane/terminal-clipboard-selection-text.test.ts— 5 cases: default on, explicit on, explicit off (verbatim), end-to-end throughcopyTerminalSelection, empty-selection guard.(One unrelated flake,
AgentMapWorkspaceContextMenu.test.tsx, timed out under full-suite load and passes on its own.)TerminalInteractionSection.tsxwould have crossed the 400-line cap with the new toggle, soScrollSpeedSlidermoved to its own file rather than bumping the limit.Not in this PR
The issue also floats a "copy message" action that copies a message's raw text. That needs message-boundary detection on a raw PTY grid, which is a different and much larger problem than the gutter — worth its own issue rather than smuggling it in here.