Skip to content

fix(terminal): scope the IME composition route to captured sessions - #15056

Merged
nwparker merged 1 commit into
mainfrom
nwparker/composition-route-session-scope
Aug 17, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/composition-route-session-scope

Conversation

@nwparker

@nwparker nwparker commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​101 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​101
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​68 $\color{#cf222e}{\Huge{\mathbf{−}}}$​13 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​55

Ports @yeongjunyoo's #11983 to current main, with Co-authored-by credit. Their design — a refcounted per-session map, snapshot capture, ownership decided before preventDefault() — 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:85 calls event.preventDefault() before the sessions.get(detail.id) lookup on line 86 and the if (!captured) return on 87. The loss is explicit in the vendored xterm patch:

this._dispatchCompositionSessionEvent(event);
prevented = event.defaultPrevented;
if (input.length > 0 && !prevented) {
  this._coreService.triggerDataEvent(input, true);   // skipped
}

Cancel without delivering and the committed CJK text is simply gone, with no user-visible signal.

Reachability: installTerminalImeCompositionRoute runs 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 empty sessions map 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:

× leaves an uncaptured session to xterm when installed mid-composition
× does not swallow a commit when the connection re-installs the route mid-composition
    AssertionError: expected false to be true
× does not let a composition started after the defer hold the newline
    AssertionError: expected "vi.fn()" to be called 1 times, but got 0 times

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.ts has since diverged on main (fallbackMs: null plus 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 separate maxIdleMs re-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 typecheck clean; oxlint, oxfmt clean; max-lines ratchet OK with no new bypasses
  • 370 files / 3625 tests pass in the terminal pane
  • The three new cases proven to fail against unmodified main, quoted above

Note 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.

…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]>
@coderabbitai

coderabbitai Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

IME 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)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the defects, design, tests, and limitations, but it omits required template sections including Linked Issue and Visual Proof. Add the required issue reference, Visual Proof section with N/A and justification if applicable, checklist status, and other missing template sections.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Title check ✅ Passed The title clearly states that IME composition routing is scoped to captured sessions, matching the main implementation and deferred-newline fix.
✨ 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: 88cf5082-2961-4588-b8ce-ea974aef7cf8

📥 Commits

Reviewing files that changed from the base of the PR and between 453237c and d0ff8e7.

📒 Files selected for processing (4)
  • src/renderer/src/components/terminal-pane/terminal-ime-composition-route.test.ts
  • src/renderer/src/components/terminal-pane/terminal-ime-composition-route.ts
  • src/renderer/src/components/terminal-pane/terminal-ime-deferred-newline.test.ts
  • src/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)

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.

🎯 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

@nwparker
nwparker merged commit 876e5b8 into main Aug 17, 2026
46 checks passed
@nwparker
nwparker deleted the nwparker/composition-route-session-scope branch August 17, 2026 08:05
paidaxingyo666 pushed a commit to paidaxingyo666/Manta that referenced this pull request Aug 21, 2026
…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]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…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]>
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.

1 participant