Repository navigation
fix(terminal): deliver an IME commit the deferred textarea diff missed - #15198
Conversation
📝 WalkthroughWalkthroughUpdated xterm composition handling to track IME transactions, reconcile composition and input events, suppress duplicate commits, and clean up pending state during blur or disposal. Composition rendering now supports preedit row tails, themed backgrounds, and cancellable repositioning. Added a happy-dom Vitest regression suite for direct, composition-based, deferred, duplicate, and stale IME commit scenarios. 🚥 Pre-merge checks | ✅ 2 | ❌ 3❌ Failed checks (3 warnings)
✅ Passed checks (2 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/terminal-ime-xterm-consumed-key-commit.test.ts (1)
2-18: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winReduce the test commentary.
The test names and dispatched events already describe the scenarios. Keep only short comments that state a non-obvious precondition or ordering constraint. Move defect history and xterm control-flow detail to the issue or commit description.
Suggested reduction
-// The reporter's failing case. ibus answers over DBus, so the commit lands after the deferred -// textarea diff has already run against an unchanged textarea — and the number key is still down. +// The sessionless commit arrives after the deferred textarea diff.As per coding guidelines, “Comments must be concise, non-obvious, and brief—prefer one line; do not explain obvious behavior or walk through code.”
Also applies to: 135-136, 150-151, 164-166, 179-181
Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 82deca9a-1dac-4544-b6c6-f00a1f99c379
📥 Commits
Reviewing files that changed from the base of the PR and between 6ee265e and 501d9ab20a8cbca0cbbe412c310bae34a894db7e.
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (2)
config/patches/@[email protected]src/renderer/src/components/terminal-pane/terminal-ime-xterm-consumed-key-commit.test.ts
Included review availability: Your plan includes up to 10 reviews per rolling hour; 4 remain after this review.
Disclosure: this patch hand-edits the minified bundles, against a documented ruleFlagging this myself rather than letting a reviewer find it.
This patch does exactly that. The five semantic edits live in the Why the rule could not be followedThe document it points at does not exist on HEAD, and neither does the tooling:
All of it landed in This is not specific to this PREvery xterm patch merged since that date is in the same position: Cost, stated plainlyThe bundle hunks cannot be reproduced by a future regeneration, and RecommendationRestore the harness from Happy to hold this PR if a maintainer would rather see the harness restored first — say so and I will. |
Picking a single Chinese character from the candidate window with a number key loses it. The character flashes and disappears. Picking the same candidate with the mouse works, and picking multi-character words with number keys works. Two paths can deliver an IME commit, and this falls between them. A keydown the input method consumed routes into a setTimeout(0) diff of the helper textarea, and that diff is what normally delivers the commit; xterm's _keyDownSeen guard exists to defer to it. When the commit arrives after that timer has already run, neither path delivers. Mouse selection works because no key is down, and a real composition session works because it takes a different path entirely. That narrows it to an input method whose commit round-trips asynchronously and which shows no in-application preedit. Track that a consumed keydown still owes its commit, and deliver only when the diff did not. The upstream guard and its single read site are untouched, which is what keeps the duplicate-commit behaviour it was added for sealed. Not doing the obvious repairs deliberately: clearing the flag, skipping it for keyCode 229, or setting it after the composition short-circuit each unblock the input path without retiring the diff, and all three were measured emitting the character twice. The patch and the lockfile hash here are generated. Review config/patches/xterm-src/@[email protected], which is the hand-written source of the change; the shipped patch and both minified bundles are the regenerator's output from the pinned upstream build, so nothing in this change was hand-transcribed into a bundle. Refs xtermjs/xterm.js#6036 Closes #12099
501d9ab to
dfaa6fb
Compare
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
config/patches/xterm-src/@[email protected] (2)
819-823: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winGuard the suffix subtraction, as the two sibling helpers do.
_composedRegionLengthsubtracts_compositionSuffix.lengthwithout checking that the value still ends with the suffix._getPendingTextareaInputand_getCompositionInputboth checkvalue.endsWith(...)first. If the textarea tail changed, the computed end is too small, the method returns 0, and_deferPreeditResynccancels an active composition.🐛 Proposed fix
private _composedRegionLength(): number { - const end = this._textarea.value.length - this._compositionSuffix.length; + const value = this._textarea.value; + const end = this._compositionSuffix.length > 0 && value.endsWith(this._compositionSuffix) + ? value.length - this._compositionSuffix.length + : value.length; return Math.max(0, end - this._compositionPosition.start); }
757-778: 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick winWire
_endPendingCompositionSessioninto pending-session finalization.The method is never called, so
sessionEndedremainsfalseanddataPendingReconciliation: trueis never dispatched. This preventsterminal-ime-composition-route.tsfrom handling pending session data correctly.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 8d261653-33e8-43da-addd-c7f6b8dcaad3
📥 Commits
Reviewing files that changed from the base of the PR and between 501d9ab20a8cbca0cbbe412c310bae34a894db7e and dfaa6fb.
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (2)
config/patches/@[email protected]config/patches/xterm-src/@[email protected]
Included review availability: Your plan includes up to 10 reviews per rolling hour; 4 remain after this review.
Merging this, with a debt recorded against itAn audit of the recent terminal input work landed while this was in review, and it is worth writing down what it found before this merges, because this change is the third patch to the same guard. The guard is asking the wrong question. Printable bytes are emitted from the key event, and
Same question, opposite failures. Other terminals resolve it structurally rather than by inference: they hand the event to the platform's input context first, let the text system call back with the committed or marked text, and only then ask whether the raw key should still produce bytes. The equivalent flag there is the return value of having asked, not a guess derived from "a key went down." So the honest framing of this change: it is a fourth arbiter between two delivery paths that arguably should not both exist. The right fix is to stop emitting printable bytes from the key path and make the text system the sole source, letting the existing textarea diff own it — which would delete considerably more code than it adds. I am still merging it, deliberately: #12099 is a real bug that users are hitting today, this fix is correct and tested for it, and the rework above is a much larger piece of work that should not gate a live bug fix. But it does make that guard load-bearing in one more place, and if a fourth regression appears in this area, this comment is the reason to stop patching and do the rework instead. Two things did improve in the meantime:
|
stablyai#15198) Picking a single Chinese character from the candidate window with a number key loses it. The character flashes and disappears. Picking the same candidate with the mouse works, and picking multi-character words with number keys works. Two paths can deliver an IME commit, and this falls between them. A keydown the input method consumed routes into a setTimeout(0) diff of the helper textarea, and that diff is what normally delivers the commit; xterm's _keyDownSeen guard exists to defer to it. When the commit arrives after that timer has already run, neither path delivers. Mouse selection works because no key is down, and a real composition session works because it takes a different path entirely. That narrows it to an input method whose commit round-trips asynchronously and which shows no in-application preedit. Track that a consumed keydown still owes its commit, and deliver only when the diff did not. The upstream guard and its single read site are untouched, which is what keeps the duplicate-commit behaviour it was added for sealed. Not doing the obvious repairs deliberately: clearing the flag, skipping it for keyCode 229, or setting it after the composition short-circuit each unblock the input path without retiring the diff, and all three were measured emitting the character twice. The patch and the lockfile hash here are generated. Review config/patches/xterm-src/@[email protected], which is the hand-written source of the change; the shipped patch and both minified bundles are the regenerator's output from the pinned upstream build, so nothing in this change was hand-transcribed into a bundle. Refs xtermjs/xterm.js#6036 Closes stablyai#12099
stablyai#15198) Picking a single Chinese character from the candidate window with a number key loses it. The character flashes and disappears. Picking the same candidate with the mouse works, and picking multi-character words with number keys works. Two paths can deliver an IME commit, and this falls between them. A keydown the input method consumed routes into a setTimeout(0) diff of the helper textarea, and that diff is what normally delivers the commit; xterm's _keyDownSeen guard exists to defer to it. When the commit arrives after that timer has already run, neither path delivers. Mouse selection works because no key is down, and a real composition session works because it takes a different path entirely. That narrows it to an input method whose commit round-trips asynchronously and which shows no in-application preedit. Track that a consumed keydown still owes its commit, and deliver only when the diff did not. The upstream guard and its single read site are untouched, which is what keeps the duplicate-commit behaviour it was added for sealed. Not doing the obvious repairs deliberately: clearing the flag, skipping it for keyCode 229, or setting it after the composition short-circuit each unblock the input path without retiring the diff, and all three were measured emitting the character twice. The patch and the lockfile hash here are generated. Review config/patches/xterm-src/@[email protected], which is the hand-written source of the change; the shipped patch and both minified bundles are the regenerator's output from the pinned upstream build, so nothing in this change was hand-transcribed into a bundle. Refs xtermjs/xterm.js#6036 Closes stablyai#12099
stablyai#15198) Picking a single Chinese character from the candidate window with a number key loses it. The character flashes and disappears. Picking the same candidate with the mouse works, and picking multi-character words with number keys works. Two paths can deliver an IME commit, and this falls between them. A keydown the input method consumed routes into a setTimeout(0) diff of the helper textarea, and that diff is what normally delivers the commit; xterm's _keyDownSeen guard exists to defer to it. When the commit arrives after that timer has already run, neither path delivers. Mouse selection works because no key is down, and a real composition session works because it takes a different path entirely. That narrows it to an input method whose commit round-trips asynchronously and which shows no in-application preedit. Track that a consumed keydown still owes its commit, and deliver only when the diff did not. The upstream guard and its single read site are untouched, which is what keeps the duplicate-commit behaviour it was added for sealed. Not doing the obvious repairs deliberately: clearing the flag, skipping it for keyCode 229, or setting it after the composition short-circuit each unblock the input path without retiring the diff, and all three were measured emitting the character twice. The patch and the lockfile hash here are generated. Review config/patches/xterm-src/@[email protected], which is the hand-written source of the change; the shipped patch and both minified bundles are the regenerator's output from the pinned upstream build, so nothing in this change was hand-transcribed into a bundle. Refs xtermjs/xterm.js#6036 Closes stablyai#12099
Fixes #12099, reported by @xdewx: in the terminal, picking a single Chinese character from the candidate window with a number key loses it. The character flashes on screen and disappears. Picking the same candidate with the mouse works. Picking multi-character words with number keys works.
Those three outcomes are the whole diagnosis, and no capture from the reporter was needed to get there.
The mechanism
There are two paths that can deliver an IME commit, and this bug falls between them.
A keydown the input method consumed (
keyCode 229) is routed into_handleAnyTextareaChanges(), which schedules asetTimeout(0)diff of the helper textarea. That diff is the path that normally delivers the commit. Upstream's guard in_inputEvent:is therefore not "block IME text" — trusted events are always
composed: true, so it collapses to "deliver only if no key is down", which in practice means defer to the diff.When the commit arrives after that timer has already run, neither path delivers and the character is gone. That is specifically an input method whose commit round-trips asynchronously and which shows no in-application preedit — ibus with Rime, or fcitx5 with application preedit disabled. One that commits synchronously beats the timer and is unaffected.
This accounts for all three reported outcomes exactly:
inputevent directlyThe character "appears briefly" because the preedit overlay is drawn locally. It never reached the pty, so when the overlay clears there is nothing underneath.
This is not only ours. The same report exists upstream against a plain library demo — xtermjs/xterm.js#6036 — reproducing in other terminals built on it, with the trigger stated as "application preedit is disabled."
Why the three obvious fixes are wrong — measured, not argued
The tempting repairs all target
_keyDownSeenitself: don't set it, clear it on a consumed keydown, or set it after the composition short-circuit. Emulated against a realTerminal, all three produce:They unblock the
inputpath without retiring the diff, so any synchronous commit is delivered twice.xterm-bypass-policy.ts:127deliberately lets standalone 229 keydowns through on macOS and Linux precisely so that diff can arm, so both platforms are exposed. Windows suppresses them and is unaffected either way.So this instead tracks that a consumed keydown is still owing its commit, and delivers only when the diff did not. The upstream guard and its single read site are untouched, which is what keeps the duplicate-commit behaviour it was originally added for sealed.
Why new state is justified
The standing preference here is minimal code, so the added field has to earn its place. It is one boolean plus a short private method, because this is an arbitration problem between two delivery paths and no existing field expresses "the consumed keydown still owes its commit". The zero-new-field variants were checked and rejected:
_textareaChangeTimeras the marker overloads one field with two meanings and breaks the re-arm guard, so later consumed keydowns stop re-baselining_dataAlreadySentis written by the diff and never read by itTests
terminal-ime-xterm-consumed-key-commit.test.ts, six cases against a realTerminal:Case 3 is the only one that fails against unmodified main (
expected '' to be '好'); the before-run was a genuine revert — patch stashed andpnpm installre-run, confirmed withgrep -c _claimImeKeydownCommit node_modules/@xterm/xterm/lib/xterm.mjsreturning 0.Worth being precise about what each case does: the duplicate-commit case passes in both directions — it is a guard, not a discriminator. The cases that actually discriminate against the wrong fixes are case 4 (emits
好好under any of the three_keyDownSeenvariants) and case 6 (emitsAAif the disarm is dropped).One trap worth recording
@xterm/xtermships bothsrc/and a minifiedlib/, and everything at runtime and under vitest resolves tolib/xterm.mjsvia the packagemodulefield. A TS-only edit to the vendored patch is inert — it would leave tests passing while changing nothing. The change is applied tosrc/,lib/xterm.jsandlib/xterm.mjs.Verification
lib/pane-manager/,dashboard-popout/, terminal search decoration): 108 files / 1082 pass, 1 skippednpm run typecheckreports two errors insrc/renderer/src/components/tab-bar/open-tab-search-retention.test.ts, proven pre-existing by stashing and re-running to byte-identical output — unrelated and not fixed hereNot verified
No real hardware. ibus+Rime panel-only preedit could not be reproduced on this machine, so the async commit ordering is modelled rather than observed live. The reporter has been given an inline-preedit experiment that doubles as a workaround; if it does not help them, the model of their setup is wrong and that should block this.
lib/*.mapare now slightly stale, since sourcemaps were not regenerated. Devtools-only.