Repository navigation
fix(terminal): scope the IME composition route to captured sessions - #15056
Conversation
…essions Two ways a terminal composition went wrong, both from state that was not scoped to the session it belonged to. The route called preventDefault() before checking whether it owned the session. The patched xterm treats that cancellation as "someone else will deliver this" and skips its own triggerDataEvent, so a route installed mid-composition — the connection effect re-runs, a reconnect swaps the transport, StrictMode remounts — suppressed the insertion and then returned without delivering anything. The commit vanished. Ownership is now decided first; preventDefault stays for sessions the route does own, including the ones it deliberately drops after a transport swap, since that drop is its decision to make. Pending-composition state was a bare per-element count, so a waiter could only ask "is anything composing", not "is what I was waiting for still composing". A composition the user starts after pressing Enter is behind that Enter, not in front of it, but it kept the count non-zero and held the newline anyway — `한` Enter `글` reaching the terminal as `한글\n`. The count is now reference-counted per session id, and the deferred send snapshots the sessions open when it starts and waits only for those. Reference counts rather than a set, so two overlapping routes owning the same session cannot clear each other's pending state. Co-authored-by: yeongjunyoo <[email protected]>
📝 WalkthroughWalkthroughIME composition tracking now records session IDs with reference counts and supports snapshot-scoped pending checks. Route event handling validates session ownership before suppressing commits and releases sessions individually during disposal. Deferred newline handling captures pending sessions when waiting starts, so later compositions do not delay the newline. Tests cover route replacement, dropped commits, overlapping routes, and deferred newline ordering. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 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: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 88cf5082-2961-4588-b8ce-ea974aef7cf8
📒 Files selected for processing (4)
src/renderer/src/components/terminal-pane/terminal-ime-composition-route.test.tssrc/renderer/src/components/terminal-pane/terminal-ime-composition-route.tssrc/renderer/src/components/terminal-pane/terminal-ime-deferred-newline.test.tssrc/renderer/src/components/terminal-pane/terminal-ime-deferred-newline.ts
Included review availability: Your plan includes up to 10 reviews per rolling hour; 7 remain after this review.
| } | ||
| if (!sessions.has(detail.id)) { | ||
| adjustPendingCompositionCount(terminalElement, 1) | ||
| addPendingCompositionSession(terminalElement, detail.id) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Elect one route to complete each shared session.
If two routes overlap with the same capturedTransport, both handlers own the ID and pass the transport checks. preventDefault() does not stop the other listener. Both handlers then call terminal.input(detail.data) on Line 149 and duplicate the committed text.
Record one current session owner. Transfer ownership when that route disposes. Make non-owner routes return without suppressing or forwarding the event. Add a regression with two active routes, one input mock, and a session-end event.
Also applies to: 135-138
…essions (stablyai#15056) Two ways a terminal composition went wrong, both from state that was not scoped to the session it belonged to. The route called preventDefault() before checking whether it owned the session. The patched xterm treats that cancellation as "someone else will deliver this" and skips its own triggerDataEvent, so a route installed mid-composition — the connection effect re-runs, a reconnect swaps the transport, StrictMode remounts — suppressed the insertion and then returned without delivering anything. The commit vanished. Ownership is now decided first; preventDefault stays for sessions the route does own, including the ones it deliberately drops after a transport swap, since that drop is its decision to make. Pending-composition state was a bare per-element count, so a waiter could only ask "is anything composing", not "is what I was waiting for still composing". A composition the user starts after pressing Enter is behind that Enter, not in front of it, but it kept the count non-zero and held the newline anyway — `한` Enter `글` reaching the terminal as `한글\n`. The count is now reference-counted per session id, and the deferred send snapshots the sessions open when it starts and waits only for those. Reference counts rather than a set, so two overlapping routes owning the same session cannot clear each other's pending state. Co-authored-by: yeongjunyoo <[email protected]>
…essions (stablyai#15056) Two ways a terminal composition went wrong, both from state that was not scoped to the session it belonged to. The route called preventDefault() before checking whether it owned the session. The patched xterm treats that cancellation as "someone else will deliver this" and skips its own triggerDataEvent, so a route installed mid-composition — the connection effect re-runs, a reconnect swaps the transport, StrictMode remounts — suppressed the insertion and then returned without delivering anything. The commit vanished. Ownership is now decided first; preventDefault stays for sessions the route does own, including the ones it deliberately drops after a transport swap, since that drop is its decision to make. Pending-composition state was a bare per-element count, so a waiter could only ask "is anything composing", not "is what I was waiting for still composing". A composition the user starts after pressing Enter is behind that Enter, not in front of it, but it kept the count non-zero and held the newline anyway — `한` Enter `글` reaching the terminal as `한글\n`. The count is now reference-counted per session id, and the deferred send snapshots the sessions open when it starts and waits only for those. Reference counts rather than a set, so two overlapping routes owning the same session cannot clear each other's pending state. Co-authored-by: yeongjunyoo <[email protected]>
Ports @yeongjunyoo's #11983 to current main, with
Co-authored-bycredit. Their design — a refcounted per-session map, snapshot capture, ownership decided beforepreventDefault()— is carried over essentially verbatim.Two real defects, verified on current main
1. Committed text is silently lost when the route is reinstalled mid-composition.
terminal-ime-composition-route.ts:85callsevent.preventDefault()before thesessions.get(detail.id)lookup on line 86 and theif (!captured) returnon 87. The loss is explicit in the vendored xterm patch:Cancel without delivering and the committed CJK text is simply gone, with no user-visible signal.
Reachability:
installTerminalImeCompositionRouteruns inside the connection effect (pty-connection.ts:4230), disposed by its cleanup (:9471). Any effect re-run mid-composition — reconnect, transport swap, StrictMode remount — disposes the route holding the session and installs a fresh one with an emptysessionsmap on the same terminal element.2. A deferred newline can be released by an unrelated composition. Pending state is a bare count (
pendingCompositionCountByElement, line 17), so an overlapping composition that starts after the defer satisfies the wait.한Enter글arrives as한글\n.Severity split honestly: (1) is data loss and clearly worth fixing; (2) is ordering corruption, bounded by the 200 ms fallback and requiring overlapping compositions — milder, but it is what motivates the data-structure change.
Proof it discriminates
Against unmodified
origin/main:Restored, all 62 pass.
What differs from the original PR
Their branch is stacked on #11896, which is now closed, and
terminal-ime-deferred-newline.tshas since diverged on main (fallbackMs: nullplus a disposer, from #12871 and STA-4476). A wholesale port would have regressed that. So this ports the two verified claims and leaves #11896's separatemaxIdleMsre-arm work out.One correction to the original rationale, recorded because it matters for anyone reading the diff: refcounts and a bare count are numerically equivalent for the plain "is anything pending" query. The refcount map is required for the snapshot query, and refcounts-over-a-Set matter only for overlapping-route ownership. The overlapping-route test passes on main too — it is kept as a design guard and is explicitly not one of the three discriminating cases.
Reference check
A reference terminal implementation scopes preedit state per surface/session, never globally per view. Neither reference has runtime ownership arbitration, because both keep the composition-owning object structurally stable for the session's lifetime — one deliberately preserves its view across split rebuilds to avoid exactly this. That is a stronger guarantee than an ownership check, and it confirms the direction: per-session scoping is right, and Orca needs the explicit check precisely because its route is reinstallable. Both do conditionally suppress the default insert path, and one's own comments flag that suppression as a known soft spot — which is what bit us here.
Verification
npm run typecheckclean; oxlint, oxfmt clean; max-lines ratchet OK with no new bypassesNote on the original PR's CI
An earlier triage note of mine said CI had never run on that fork branch. That was wrong — PR Checks ran green on 2026-08-04. But it ran against a base so stale that Greptile bailed at 1422 changed files, so it carries no signal for current main. This run does.
Unverified
No runtime or manual IME validation; correctness rests on the unit tests plus the xterm patch source.