Repository navigation
fix(terminal): make primary-selection paste suppression single-shot - #10526
Conversation
Middle-clicking in the terminal armed a 750ms window that swallowed every native paste event, not just Chromium's one follow-up — so a real Ctrl+V inside that window was silently dropped on Linux. Consume the deadline on first use (`shouldSuppress*` -> `consume*`) so the arm owes exactly one event; 750ms stays as that event's expiry bound.
|
Warning Review limit reached
Next review available in: 19 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughNative primary-selection paste suppression now uses a consuming helper that clears the suppression deadline after the first eligible terminal-native paste. The paste hook calls this helper for terminal paste events. Existing unit tests are updated to the new API, and new integration tests cover Linux follow-up suppression, later paste allowance, unarmed terminals, and macOS behavior. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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 |
Review follow-ups on the single-shot suppression change; no source change. The new real-module file only synthesized `beforeinput`. An Electron probe (Chromium 150) showed `paste` fires first, is cancelable, and cancelling it at document capture suppresses `beforeinput` entirely — and xterm registers `handlePasteEvent` for `paste` only, never `beforeinput`. So `paste` is the sole event that can double-write the PTY, and it was the one event the end-to-end file did not exercise. Add it; it fails with the fix reverted. Consuming mutates, so `isTerminalNativePasteTarget(...) && consume()` is now load-bearing: swapped operands would burn the arm on an unrelated paste and let the real follow-up double-paste. The guarding test asserted only `defaultPrevented`, which survives the swap — assert `consume` is never reached instead. Rename the mocked-file case that claimed to prove single-shot. With the module mocked its sequence is dictated by the mock; it pins that the hook re-asks per event rather than caching, so name it that.
Review round — CodeRabbit disposition + subagent findingsCodeRabbitCodeRabbit generated no actionable inline comments ("No actionable comments were generated in the recent review"). Its pre-merge checks came back 4 passed / 1 warning. Disposition of the one warning:
Subagent review (2 agents, both Opus 5, same effort)Both returned. They disagreed on the main functional question; adjudication below. Actioned — pushed in 6df6e41 (tests only, no source change):
Declined, with reasoning:
Verification
|
…atches Round-2 review nit on my own round-1 change: once `dispatchClipboardPaste` existed alongside it, a helper named `dispatchPaste` that dispatches `beforeinput` inverted the reader's expectation. Match the sibling file's `dispatchNativePasteBeforeInput` convention.
Round 2 — adversarial re-verifyA second independent agent re-ran the revert-check from scratch and tried to break the change. It reproduced
Actioned — pushed in f7ea787 (tests only): a naming inversion I introduced in round 1 — a helper named Declined, with reasoning:
Scope re-checked and unchanged: one concern, one production behavior change ( Still not verified: real Linux/X11 middle-click. All review and testing was done on macOS; the Chromium event-ordering claims come from an Electron probe and Blink source, not from a live X11 session. |
…tablyai#10526) * fix(terminal): make primary-selection paste suppression single-shot Middle-clicking in the terminal armed a 750ms window that swallowed every native paste event, not just Chromium's one follow-up — so a real Ctrl+V inside that window was silently dropped on Linux. Consume the deadline on first use (`shouldSuppress*` -> `consume*`) so the arm owes exactly one event; 750ms stays as that event's expiry bound. * test(terminal): cover the paste event xterm actually forwards to the PTY Review follow-ups on the single-shot suppression change; no source change. The new real-module file only synthesized `beforeinput`. An Electron probe (Chromium 150) showed `paste` fires first, is cancelable, and cancelling it at document capture suppresses `beforeinput` entirely — and xterm registers `handlePasteEvent` for `paste` only, never `beforeinput`. So `paste` is the sole event that can double-write the PTY, and it was the one event the end-to-end file did not exercise. Add it; it fails with the fix reverted. Consuming mutates, so `isTerminalNativePasteTarget(...) && consume()` is now load-bearing: swapped operands would burn the arm on an unrelated paste and let the real follow-up double-paste. The guarding test asserted only `defaultPrevented`, which survives the swap — assert `consume` is never reached instead. Rename the mocked-file case that claimed to prove single-shot. With the module mocked its sequence is dictated by the mock; it pins that the hook re-asks per event rather than caching, so name it that. * test(terminal): name the beforeinput dispatcher for the event it dispatches Round-2 review nit on my own round-1 change: once `dispatchClipboardPaste` existed alongside it, a helper named `dispatchPaste` that dispatches `beforeinput` inverted the reader's expectation. Match the sibling file's `dispatchNativePasteBeforeInput` convention.
Summary
On Linux, middle-clicking in the integrated terminal armed a 750ms window (
PRIMARY_SELECTION_NATIVE_PASTE_SUPPRESSION_MS) that swallowed every paste event on the terminal surface for its full duration. Press Ctrl+V within 750ms of a middle-click paste and the keyboard paste was silently dropped — no error, no text, no feedback.The armed window exists for exactly one reason: the terminal injects the primary selection into the PTY itself on middle-click, so Chromium's single follow-up native paste event has to be swallowed to avoid a double paste. #8993, which introduced the window, says so in its own commit message: "This swallows the single native paste event so the selection reaches the PTY exactly once." The 750ms is an upper bound on when that one event arrives — not a period during which all pastes should die.
shouldSuppressPrimarySelectionNativePastenever cleared the deadline once consumed, so it kept answeringtrueuntil it timed out.Fix: make the suppression single-shot.
consumePrimarySelectionNativePasteSuppressionclears the deadline on first use, so the arm owes at most one event. 750ms stays as the expiry bound for that one event.The rename makes the mutation obvious at the call site — the old name promised a pure predicate.
usePrimarySelectionPasteis the only non-test consumer (TerminalPaneonly arms), and the short-circuit order is preserved so a paste outside the terminal surface never eats the arm.Platform gating is untouched:
armPrimarySelectionNativePasteSuppressionstill no-ops off Linux/X11, andsetPrimarySelectionEnabled(false)still resets the deadline to0.17 net source lines across 2 files; the rest is test coverage.
Screenshots
No visual change— this is terminal input-event behavior with no UI surface.Testing
pnpm lint— cleanpnpm typecheck— cleanpnpm test— the three touched files: 28 passed. Wider sweep:src/renderer/src/lib/+src/renderer/src/hooks/= 373 files / 3414 passed, andsrc/renderer/src/components/terminal-pane/= 183 files / 2381 passed.pnpm build— via CIverifyNon-vacuity was proven by reverting only the source logic (restoring the non-consuming predicate body while keeping the new name, so failures are about logic and not imports) and re-running:
With the fix restored:
Tests 28 passed (28).The last two failures are from a new file,
usePrimarySelectionPaste.terminal-native-paste.test.tsx. The existing hook test mocks@/lib/primary-selectionwholesale, so it structurally cannot observe single-shot behavior — a same-file test there would pass with or without the fix. The new file wires the hook to the real module and dispatches two paste events at an.xterm-helper-textarea, asserting the first is prevented and the second is not — once asbeforeinput, and once as a realpasteClipboardEvent, which is the event xterm actually forwards to the PTY. Those are the end-to-end assertions for the bug.The short-circuit order in
usePrimarySelectionPaste.tsalso became load-bearing onceconsumegained a side effect: swapping the operands would burn the arm on an unrelated paste and let the real follow-up double-paste, while leavingdefaultPreventedunchanged. That swap is now caught by assertingconsumeis never reached for a non-terminal target (verified by simulating the swap: the guard fails, and without it the swap was invisible).Also kept from the existing suite: time-based expiry, the disable-clears-armed-state case, off-Linux no-arm, and "unrelated document paste is not swallowed while armed."
AI Review Report
Reviewed with parallel AI subagents (correctness/regression, and scope/standards) plus a throwaway runtime probe, looping until clean.
Risks checked and what came back:
TerminalPanearms on bothmousedown(TerminalPane.tsx:2663) andauxclick(TerminalPane.tsx:2740). A runtime probe confirmed arm→arm→consume leaves the window disarmed, so the follow-up is eaten and a subsequent real paste survives.pasteandbeforeinputwere suppressed for the whole window; single-shot eats only the first. This is safe because only one of the two can reach the handler per native paste: Blink dispatchespastefirst, and the capture-phasepreventDefaultaborts the paste pipeline, sobeforeinputnever fires. It is also the event that matters — xterm forwards to the PTY from apastelistener on its textarea/element (@xterm/xtermCoreBrowserTerminal.ts), and does not listen tobeforeinput. A probe confirmed a realpasteClipboardEventis suppressed exactly once per arm.armPrimarySelectionNativePasteSuppressionearly-returns unlessisLinuxUserAgent(getUserAgent()), so macOS and Windows never arm and are byte-for-byte unaffected. No shortcuts, labels, paths, shell behavior, or Electron main-process code are touched — this is renderer-only DOM event handling. A macOS no-arm case is covered in the new test file.consumeis the same O(1) numeric comparison the predicate was, on the same capture-phase handler, with no new allocation, timer, or listener. It now reduces work in the common case, since a disarmed window short-circuits onnativePasteSuppressionUntil === 0.max-linesdisables (AGENTS.md), no stale references to the old symbol name anywhere in the repo.Security Audit
No new attack surface. No input parsing, no command execution, no path handling, no auth, no secrets, no dependency changes, and no IPC surface is touched. The change narrows when a DOM event is suppressed inside the renderer; it does not alter what text is read, where it comes from, or what is written to the PTY. Clipboard content still flows through the existing
PRIMARY_SELECTION_MAX_LENGTH/maxBytesbounds, which are unchanged. If anything the change is defensively better: it shortens the window during which the app silently discards user input. No follow-up needed.Notes
Platform-specific behavior and residual risks, stated honestly:
pastefires first, is cancelable, and that cancelling it at document capture suppressesbeforeinputandinputentirely. But that probe droveEditor::Paste, not an actual X11 middle-click; the real primary-selection follow-up on a Linux host is still unexercised. This is the one thing worth a Linux reviewer's eyes.TerminalPanearms onmousedownand re-arms onauxclick. If Chromium's native paste ever landed between those two, the consume would disarm andauxclickwould immediately re-arm an unowed window. Checked against Blink:event_handler.ccdispatches pointerup/mouseup and then auxclick viaDispatchMouseClickIfNeeded, and only then runs the paste —selection_controller.ccconfirms the paste happens on mouse-up "after the event handlers have been fired." So the paste lands after both arms and the spare-arm path is not reachable. Left alone deliberately: touching it would mean changingTerminalPane, which is out of scope here.