Skip to content

fix(terminal): stop cancelled IME compositions replaying stale preedit - #12273

Merged
OrcaWin merged 1 commit into
mainfrom
OrcaWin/ime-cancelled-preedit
Aug 3, 2026
Merged

OrcaWin merged 1 commit into
mainfrom
OrcaWin/ime-cancelled-preedit

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Aug 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Treat an IME composition with no textarea, compositionend, input, or keypress commit evidence as cancelled instead of replaying stale preedit text.
  • Preserve valid empty-compositionend commits that deliver text through a following insertText event.
  • Add regression coverage against the real patched xterm build for macOS Pinyin Backspace cancellation and the Linux IBus commit shape.

Fixes #11936.

Regression source

Introduced by #11293, merge commit fe6f929c6e (fix(terminal): reconcile cross-platform IME composition lifecycle). That commit added the _sendPendingComposition fallback to the last non-empty compositionupdate value. It is present in v1.4.163 and absent from the v1.4.162-rc.0 baseline.

A full revert of #11293 would restore the duplicate/drop and composition-ordering bugs it fixed. This PR changes only the ambiguous empty-evidence fallback.

Adapted from #11938 by @Him188; the contributor remains the commit author.

Comparison with #11550

#11550 is complementary rather than an alternative to this fix:

The deletion-only portion of #11550 remains a good candidate for a separate follow-up adapted to #12265's current window-capture ownership. Folding it here would mix a Windows keyboard-routing change into the macOS cancellation fix and make both harder to validate.

Before reuse, that extraction also needs an explicit Windows runtime gate and coverage for delayed native progress and complex preedit deletion. #11550 currently lets any platform's composing keyCode=229 Backspace/Delete bypass suppression and reconciles unchanged preedit on the next zero-delay task.

Screenshots

No visual change.

Testing

  • 253 terminal IME and keyboard ownership tests
  • pnpm typecheck:web
  • Frozen install/patch integrity
  • Formatting, default Oxlint, native/type-aware quality scans, reliability gates, max-lines ratchet, and diff checks
  • Added cancellation and valid empty-end commit regression tests against the installed xterm build

pnpm lint passes all changed-code and code-quality stages, then stops at verify:skill-bundle-manifest because unrelated generated skill artifacts on current main are stale. This PR does not touch those artifacts.

Security audit

This only narrows when renderer composition text may be emitted to the PTY. It adds no command execution, IPC, transport, dependency, path, auth, or secret handling changes.

Backspacing away an entire Pinyin preedit ended the composition with
empty data, no textarea residue, and no input/keypress events — yet
_sendPendingComposition fell back to the last non-empty
compositionupdate data and typed its first character into the PTY.
Only trust that fallback when observed input evidence corroborates it;
a composition with no evidence in any channel was cancelled.

Fixes the macOS Pinyin regression from #11293 (stray letter left after
deleting a preedit); same fix covers IBus/fcitx Backspace cancellation.

Co-Authored-By: Claude Fable 5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Added a happy-dom Vitest suite for xterm IME composition behavior. The suite provides terminal setup, IME event helpers, canvas mocking, cleanup, and event-loop synchronization. It tests full Pinyin preedit deletion, cancellation without input events, and empty composition completion followed by committed insertText input.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Linked Issues check ❓ Inconclusive The linked issue requires an implementation fix, but the review summary lists only regression tests and does not verify the production code change. Provide the implementation diff or reviewable summary showing that cancelled empty-evidence compositions no longer replay stale preedit text.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the terminal IME cancellation fix and matches the pull request's primary change.
Description check ✅ Passed The description covers the summary, screenshots, testing, and security audit, but omits the AI Review Report and Notes sections.
Out of Scope Changes check ✅ Passed The summarized change adds only IME cancellation regression tests, which directly support issue #11936 and the stated terminal objective.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e05932ae-6adc-4b6f-a19e-304bed5c29d3

📥 Commits

Reviewing files that changed from the base of the PR and between 173abf8 and aaef8e1.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (2)
  • config/patches/@[email protected]
  • src/renderer/src/components/terminal-pane/terminal-ime-xterm-composition-cancel.test.ts

Comment on lines +69 to +72
afterEach(() => {
vi.restoreAllMocks()
document.body.replaceChildren()
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Dispose every opened terminal in afterEach.

If a helper or assertion throws before Line 96, Line 114, or Line 133, terminal.dispose() does not run. document.body.replaceChildren() removes the container but does not dispose the xterm instance. Track opened terminals and dispose them before restoring mocks and clearing the DOM.

Proposed fix
+const openedTerminals = new Set<Terminal>()
+
 function openTerminal(): {
   emitted: string[]
   terminal: Terminal
@@
   document.body.appendChild(container)
   const terminal = new Terminal()
+  openedTerminals.add(terminal)
   terminal.open(container)
@@
   afterEach(() => {
+    for (const terminal of openedTerminals) {
+      terminal.dispose()
+    }
+    openedTerminals.clear()
     vi.restoreAllMocks()
     document.body.replaceChildren()
   })
@@
-    terminal.dispose()
@@
-    terminal.dispose()
@@
-    terminal.dispose()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
afterEach(() => {
vi.restoreAllMocks()
document.body.replaceChildren()
})
const openedTerminals = new Set<Terminal>()
function openTerminal(): {
emitted: string[]
terminal: Terminal
// existing return type members
} {
// existing setup
document.body.appendChild(container)
const terminal = new Terminal()
openedTerminals.add(terminal)
terminal.open(container)
// existing implementation
}
afterEach(() => {
for (const terminal of openedTerminals) {
terminal.dispose()
}
openedTerminals.clear()
vi.restoreAllMocks()
document.body.replaceChildren()
})

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.

[Bug]: macOS Pinyin IME — first keystroke leaks into the terminal; Backspace over preedit leaves a stray letter (regression since #11293)

2 participants