Skip to content

fix(terminal): drop IME preedits abandoned by Backspace - #12274

Closed
kcfang wants to merge 1 commit into
stablyai:mainfrom
kcfang:fix/ime-abandoned-preedit
Closed

kcfang wants to merge 1 commit into
stablyai:mainfrom
kcfang:fix/ime-abandoned-preedit

Conversation

@kcfang

@kcfang kcfang commented Aug 3, 2026

Copy link
Copy Markdown

Summary

Backspace that empties an IME preedit no longer replays the abandoned preedit into the PTY.

On macOS Zhuyin, pressing Backspace on the last symbol of a preedit deleted nothing and instead typed that symbol into the terminal, so the user had to press Backspace a second time to actually remove it. Reproduced on a real macOS Zhuyin session with stty -icanon -echo; cat -v, which returned the raw bytes E3 84 91 / E3 84 98 (ㄑ U+3111, ㄘ U+3118) — no CR and no DEL.

Root cause is in our own xterm patch, not upstream. CompositionHelper._sendPendingComposition resolves the session payload as:

textareaInput || pending.endData || pending.compositionData

and _lastCompositionData is deliberately not cleared by an empty compositionupdate. When an IME empties its preedit instead of committing, all observable channels are empty, so the chain falls back to compositionData — the preedit the user just deleted — and sends it. Pristine @xterm/[email protected] emits nothing for the same event sequence, verified by running the identical sequence against an unpatched build.

The fix adds installTerminalImeAbandonedPreeditGuard, which treats a composition as abandoned when compositionend carries no data and no following input event carries text. A real commit always surfaces on one of those two channels, so the discriminator does not depend on the compositionupdate sequence, which differs per IME dialect (Sogou/fcitx blank the preedit first; macOS can go straight to compositionend).

It is wired into both xterm surfaces:

  • terminal-ime-composition-route.ts (terminal pane) skips the payload instead of calling terminal.input.
  • preview-terminal-ime-bridge.ts (dashboard popout preview terminal) has no composition route and would otherwise emit through xterm's own data event, so it cancels the session-end instead.

Screenshots

No visual change.

Testing

  • pnpm lint — exit 0; oxlint, native and type-aware code-quality audits all report 0 warnings / 0 errors; reliability gate manifest check passed for 62 gates
  • pnpm typecheck — exit 0
  • pnpm test — 4111 files passed, 16 skipped; 44115 tests passed, 135 skipped, 0 failed
  • pnpm build — exit 0 (desktop, CLI, web projection, native)
  • Added or updated high-quality tests that would catch regressions

New file terminal-ime-abandoned-preedit.test.ts drives real Terminal instances with the real composition route, not mocks:

Case Expectation
Backspace empties preedit, IME blanks it with its own compositionupdate first nothing reaches the PTY
Backspace empties preedit, IME goes straight to compositionend nothing reaches the PTY
Backspace only shrinks a multi-character preedit still composing, nothing reaches the PTY
Sogou-style commit announced by an empty compositionupdate 你 routes exactly once
fcitx-style commit whose text only lands on the input event after an empty compositionend 你 routes exactly once
Routeless dashboard-popout preview terminal, abandoned preedit nothing reaches the PTY

The last three are the important ones: they fail if the guard over-suppresses. Forcing the guard off turns the four suppression assertions red while the three commit assertions stay green, so the suite discriminates rather than passing vacuously.

Registered under the existing terminal-input.ime-and-synthetic-forwarding reliability gate (commands, testFiles, assertionRefs, evidenceRuns), and narrowed its knownGaps entry accordingly.

AI Review Report

Review focused on the risk that a payload guard drops text the user actually committed.

Cross-platform. The guard contains no platform branch — it reads only CompositionEvent.data and InputEvent.data. No paths, no shell, no keyboard-accelerator or shortcut-label changes, so the metaKey/CmdOrCtrl and ⌘/Ctrl+ rules are not touched. One behavioral note: installPreviewImeBridge previously returned null on non-darwin and now always returns a bridge, because the abandoned-preedit path is not macOS-specific. Both call sites use imeBridge?.claimKeyEvent(event) ?? false and imeBridge?.dispose(); the non-darwin bridge returns claimKeyEvent: () => false, so observable behavior on Linux and Windows is unchanged and cleanup is now actually performed. Windows MS-IME and Linux fcitx/IBus commits always carry text on compositionend or the following input event, which the two "must route" tests pin — but that reasoning is covered by unit tests only, not by real-OS IME automation on those platforms.

SSH / remote / local. No change to transport, PTY ownership, or process assumptions. The composition route's existing captured-transport and pty-id checks are untouched; the guard only adds one more reason to skip terminal.input, evaluated before those checks change any state.

Agents / integrations / git providers. Nothing provider- or agent-specific. The change sits below every CLI agent in the renderer input path.

Performance. Three passive listeners per terminal element and two booleans. No allocation in the keystroke path, no polling, no IPC. Listeners are removed in dispose(), which both surfaces call on teardown.

UI quality. No UI surface touched.

What was flagged and changed. The first implementation keyed on compositionupdate('') preceding compositionend(''). That sequence was assumed, not observed, and would have silently failed on IMEs that skip the blanking update — the fix would have looked correct while doing nothing. It was replaced with the dialect-independent discriminator described above, and a second test case was added for the skipped-update dialect. The first implementation also only covered the terminal pane; the dashboard popout preview terminal was found to be independently affected and was fixed in the same change.

Security Audit

No input validation, command execution, path handling, authentication, secrets, IPC, or dependency surface is touched. The change is a renderer-local DOM event state machine that can only ever suppress one already-computed string; it cannot construct, widen, or redirect a payload, and it does not alter which PTY a payload reaches. The residual risk is functional rather than security-related: an over-broad guard would drop legitimate committed text, which the three "must route" test cases exist to catch. No follow-up needed.

Notes

  • The bug is triggered by IMEs that empty a preedit instead of committing (macOS Zhuyin Backspace is the reported case), but the fix is platform-neutral and applies wherever xterm runs.
  • This is a regression from our own config/patches/@[email protected], not an upstream xterm bug, so no upstream PR is needed. The || pending.compositionData fallback is intentionally kept for IMEs that clear the textarea before committing; only the abandoned case is filtered out.
  • Real-OS IME automation remains a registered gap for this gate on every platform. Verification here is renderer-unit plus a manual macOS Zhuyin reproduction.

Orca's xterm patch resolves a composition session payload as
`textareaInput || endData || compositionData`, and `_lastCompositionData`
is never cleared by an empty compositionupdate. When an IME empties its
preedit instead of committing - macOS Zhuyin Backspace on the last
symbol - every observable channel is empty, so the chain falls back to
the abandoned preedit and replays it into the PTY. The keystroke deletes
nothing and injects the symbol the user just removed.

Confirmed against a real macOS Zhuyin session: `cat -v` on a raw tty
returned bytes E3 84 91 / E3 84 98 (U+3111 U+3118) with no CR and no
DEL. Pristine upstream @xterm/xterm 6.1.0-beta.287 sends nothing for the
same event sequence, so this is a regression from the patch, not an
upstream bug.

Add a composition guard that treats a session as abandoned when
compositionend carries no data and no following input event carries
text. A commit always surfaces on one of those two channels, so the
discriminator holds across IME dialects that differ in whether they
blank the preedit with their own compositionupdate first.

Wire it into both xterm surfaces: the terminal pane's composition route
skips the payload, and the dashboard-popout preview terminal - which has
no route and would otherwise emit through xterm's own data event -
cancels the session end.
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 69d5a30b-80ab-45f5-9063-138b607ee3f2

📥 Commits

Reviewing files that changed from the base of the PR and between 8e9640c and 0fb2154.

📒 Files selected for processing (5)
  • config/reliability-gates.jsonc
  • src/renderer/src/components/dashboard-popout/preview-terminal-ime-bridge.ts
  • src/renderer/src/components/terminal-pane/terminal-ime-abandoned-preedit.test.ts
  • src/renderer/src/components/terminal-pane/terminal-ime-abandoned-preedit.ts
  • src/renderer/src/components/terminal-pane/terminal-ime-composition-route.ts

📝 Walkthrough

Walkthrough

Adds an abandoned-preedit guard that tracks composition and input events, suppresses empty preedit forwarding, and supports disposal. Integrates the guard with terminal composition routing and the popout preview IME bridge. Non-macOS platforms now receive a functional bridge. Adds tests for emptying and shrinking preedit text, genuine Sogou and fcitx/IBus commits, and preview-terminal behavior. Updates reliability-gate commands, evidence, assertions, and known-gap wording.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary fix: preventing abandoned IME preedits from being sent after Backspace.
Description check ✅ Passed The description covers the required summary, screenshots, testing, AI review, security audit, and platform-specific notes with specific verification details.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

@OrcaWin

OrcaWin commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Superseded by the cancelled-preedit reconciliation now on main plus #12281's explicit Sogou
ihao → … → n → empty no-trailing-input regression. Because the fix lives inside the patched xterm CompositionHelper, it also covers routeless terminal surfaces without another listener state machine. Thank you @kcfang for the real macOS Zhuyin byte trace and the cross-dialect analysis.

@OrcaWin OrcaWin closed this Aug 3, 2026
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.

2 participants