Skip to content

fix(ime): stop dropping and splitting composition commits in the terminal - #11896

Closed
nwparker wants to merge 1 commit into
nwparker/ime-test-fixturesfrom
nwparker/ime-commit-delivery
Closed

nwparker wants to merge 1 commit into
nwparker/ime-test-fixturesfrom
nwparker/ime-commit-delivery

Conversation

@nwparker

@nwparker nwparker commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Three terminal IME bugs, all of which corrupt committed CJK text.

1. Dropped commits after a transport swap. The composition route delivered a finished commit to whatever transport was current at delivery time. If the pty was swapped mid-composition, the text landed on the wrong pty. The route now checks transport identity and ownership before delivering, and deliberately drops when the captured transport is gone — that drop is the route's decision to make, rather than silently writing to a stranger.

2. Newlines split the preedit. The deferred-newline fallback called finish directly, bypassing the pending-composition re-check the event path uses. A composition still open at 200 ms got a newline sent into the middle of its preedit — you press Enter, and your half-converted Japanese gets cut in two with a line break through it.

The deadline now re-arms while a composition is pending. Its 2 s ceiling is measured from the last compositionupdate, not from the start: multi-segment bunsetsu conversion legitimately keeps a Japanese composition pending far longer than any fixed total-elapsed budget, so a wall-clock ceiling would reintroduce exactly the split it is meant to prevent. A composition that stops progressing still cannot strand the keystroke.

3. Compositions already in flight when the route installs. The re-arm asked the route whether a composition was pending, but a route installed mid-composition never saw that session start, so it had nothing recorded to report — and the deadline split that composition too. A live compositionupdate now counts as evidence in its own right.

Screenshots

No visual change in the UI chrome. The user-visible effect is in terminal text: committed CJK no longer lands on the wrong pty or gets bisected by a newline.

Testing

  • pnpm lint
  • pnpm typecheck
  • pnpm test — 2905 passing across the terminal-pane and IME suites
  • pnpm build — not run
  • Added or updated high-quality tests that would catch regressions

On the idle ceiling specifically: the new test (holds the newline through a long conversion that keeps updating its preedit) was verified to fail without the fix and pass with it, so it is a real regression test rather than a tautology.

AI Review Report

A review subagent found the wall-clock ceiling bug in my own first version of this PR — the original 2 s budget was absolute, which would split any conversion running longer than 2 s. A total-elapsed ceiling is the wrong shape for this: composition length is a property of what the user is typing, not of anything going wrong. The bound that is actually meaningful is idle time — a composition that has stopped progressing. Fixed here, with the regression test above.

Also verified while fixing it: xterm's own CompositionHelper.blur():245-247 already finalizes on blur, so the original "compositionend is not guaranteed on focus loss" rationale was not the real justification for the ceiling. The comment now states the actual one — a composition that stops progressing.

Cross-platform: the deferral is driven by DOM composition events and the xterm session events, both platform-neutral. SSH and folder-workspace cases are unaffected — the route keys on transport identity, which is the same abstraction for local, SSH, and relay ptys, and the drop-on-missing-transport path is what makes an SSH reconnect safe.

Security Audit

  • IPC / transport: this is the security-relevant part, and it improves. Previously a commit could be written to a transport other than the one that owned the composition; the ownership + identity check closes that. Writing input to the wrong pty is a real confidentiality concern when the swapped-in pty is a different host or a different user's shell over SSH.
  • Input handling: composed text is passed through unmodified. Per the IME rules it is deliberately not normalized at commit — normalization happens only at path handling and equality comparison, so no normalization-based bypass is introduced here.
  • No command execution, auth, secrets, or dependency changes.

Notes

Base: #11895. Depends on #11894 for the readable CompositionHelper source.

@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch from c27126c to 787ba98 Compare August 1, 2026 08:10
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch from 787ba98 to 0964124 Compare August 1, 2026 08:12
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch 2 times, most recently from a8a6894 to b92ab08 Compare August 1, 2026 08:25
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch 2 times, most recently from 57d6dcf to 877b571 Compare August 1, 2026 08:37
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch from 877b571 to 818123d Compare August 1, 2026 08:43
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch from 818123d to b513a74 Compare August 1, 2026 08:50
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch from b513a74 to 666a2a0 Compare August 1, 2026 09:17
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch from 666a2a0 to 5e5ddd7 Compare August 1, 2026 10:04
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch from 5e5ddd7 to ab2bf67 Compare August 1, 2026 10:14
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch from ab2bf67 to 4dcdb23 Compare August 1, 2026 23:44
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch from 4dcdb23 to b7d2c99 Compare August 2, 2026 02:34
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch from b7d2c99 to 2edcbf7 Compare August 2, 2026 03:16
@nwparker nwparker closed this Aug 2, 2026
@nwparker nwparker reopened this Aug 2, 2026
@nwparker
nwparker force-pushed the nwparker/ime-commit-delivery branch 2 times, most recently from e9baec0 to 659de87 Compare August 2, 2026 04:09
…inal

Two ways a committed composition was lost or corrupted.

The composition route called preventDefault() before checking whether it owned
the session. A route installed mid-composition never saw the matching session
start, so it suppressed xterm's own 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.

The deferred-newline fallback fired `finish` directly, bypassing the
pending-composition re-check that the event path uses. A composition still open
at 200 ms got a newline sent into the middle of its preedit. The deadline now
re-arms while a composition is pending, and its 2 s ceiling is measured from the
last compositionupdate rather than from the start. A slow candidate window no
longer splits the preedit, and neither does multi-segment bunsetsu conversion,
which stays legitimately pending well past any fixed total-elapsed ceiling. A
composition that stops progressing still cannot strand the keystroke.

Co-authored-by: Orca <[email protected]>

The re-arm also treats a live compositionupdate as evidence in its own right, not
just a session the route recorded. A route installed while a composition was
already in flight never saw that session start, so it has nothing to report as
pending and the deadline would otherwise split that composition too.
@holdn2

holdn2 commented Aug 2, 2026

Copy link
Copy Markdown

Heads up — I've opened #12120 as a draft that ports part of ab2bf67 from this PR,
and I want to be explicit about which part.

Taken: only the deferred-newline fallback re-arm — the compositionupdate evidence, the
idle ceiling measured from the last update rather than from the start, and its four tests.
Attributed with a Co-authored-by: trailer since it is a partial port rather than a
cherry-pick.

Not taken: the composition-route ownership/preventDefault reordering and the
transport-identity work. Those are a larger change than the combined branch needed, and
they're better reviewed here on their own merits.

I kept your bunsetsu rationale in the comments verbatim, since the idle-vs-total-elapsed
distinction is the whole point and I didn't want it lost.

Not meant to supersede this PR. Happy to close mine, and glad to be corrected if I've
drawn the line in the wrong place.

holdn2 pushed a commit to holdn2/orca that referenced this pull request Aug 3, 2026
Ports only the fallback re-arm portion of stablyai#11896 commit ab2bf67; transport-routing changes are intentionally excluded.

Co-authored-by: Neil <[email protected]>
@holdn2

holdn2 commented Aug 3, 2026

Copy link
Copy Markdown

Heads up now that #12265 has landed: the fallback re-arm from this PR is the one piece of
the Windows Korean IME work still not on main.

terminal-ime-deferred-newline.ts at main is unchanged on that path:

const fallbackTimer = window.setTimeout(finish, fallbackMs)   // line 45

finish still fires directly at 200 ms without the pending-composition re-check the event
path uses, so a composition still open at the deadline gets a newline dropped into the
middle of its preedit. None of maxIdleMs, onFallbackDeadline, sawRecentUpdate or
TERMINAL_IME_DEFERRED_NEWLINE_MAX_IDLE_MS exist there. #12265 closed the keydown and
keyup synthesis paths, and the xterm commit-ordering work landed separately, but neither
touches this one.

I had ported just that hunk into #12120 — the compositionupdate evidence and the idle
ceiling measured from the last update rather than from the start, with your four tests and
a Co-authored-by: to you. I am closing that PR now that the overlap has landed, so
nothing of mine is competing with this one. If it is useful, the four tests apply cleanly
on top of main as they are:

  • pending composition is not split at fallbackMs
  • a long conversion that keeps updating its preedit is held past the ceiling
  • a composition already in flight with no recorded session start is still held
  • a stalled composition sends exactly once at the ceiling

Happy to leave it entirely to you — this is your hunk and your rationale, and the
idle-vs-total-elapsed distinction in your comments is the part that matters. Just flagging
that it is now the remaining gap rather than one of several.

@dogfooter8750

Copy link
Copy Markdown
Contributor

Validation data for this draft, since it is the last piece of #11878 still open on main and @holdn2 flagged it when closing #12120.

I measured when the deferred newline actually fires, on main at 13f033f0 and with this branch applied, driving sendTerminalInputAfterComposition directly with fake timers.

Two scenarios. In both, a newline is deferred at t=0 with a composition already open.

  • A — stalled composition: nothing further happens. This is the case the ceiling exists for.
  • B — the user keeps typing: six syllables at ~120 ms apart, each dispatching compositionupdate and rolling the session over (session-start for the next syllable, session-end for the previous). Typing finishes at t=720 ms.
                            main (13f033f0)      this branch
A  stalled composition          300 ms             2100 ms
B  typing through 720 ms        240 ms             2620 ms

B is the one that matters. On main the newline lands at 240 ms — the user is four syllables from finishing the word, and the bytes go out mid-composition. That is exactly the "newline appears in the middle of a word" shape reported on #11878, and it is still live on main today. With this branch the deferral rides out the whole run and releases at 2620 ms, after typing has stopped.

A is not a regression. 2100 ms is the idle ceiling doing its job. Measuring idle from the last compositionupdate rather than from the defer is the right call — it means a genuinely stuck IME still releases the user's newline instead of swallowing it, while an active composition is never interrupted. The Japanese bunsetsu note in your comment is the case that would break under a total-elapsed ceiling, and this avoids it.

terminal-ime-deferred-newline.test.ts and terminal-ime-composition-route.test.ts pass with the branch applied (58 tests) on Windows.

Caveat on what this is and is not: this is unit-level with fake timers, so it establishes the ordering and the deadline behaviour, not real-device timing. I have a Windows 11 + Microsoft Korean IME (2-beolsik) machine and can run the real-device pass against a build of this branch whenever one exists — that was the gap that held up the predicate fix too, and I would rather not have it hold this one up as well.

Related, in case it saves someone a search: #11983 stacks the defer-time session capture on top of this, which covers the other half I raised on #11878 — a session that starts after the defer holding the newline hostage. Between the two the fallback path looks fully addressed; nothing further from me.

@nwparker

nwparker commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Duplicate of #12942 and obsolete for the same reason: all four files it touches were deleted by #13128 (17b3dff). The deferral mechanism was the bug — on 1.4.171 it wrote a Shift+Enter payload to the PTY with no Enter pressed, 10/10, at 203-218 ms after keydown. If commit-splitting still reproduces on current main, please reopen with a trace.

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.

3 participants