Skip to content

test(ime): add a recorded-trace harness and one composition predicate - #11895

Closed
nwparker wants to merge 4 commits into
nwparker/ime-xterm-patch-buildfrom
nwparker/ime-test-fixtures
Closed

nwparker wants to merge 4 commits into
nwparker/ime-xterm-patch-buildfrom
nwparker/ime-test-fixtures

Conversation

@nwparker

@nwparker nwparker commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Collapses the app's scattered IME guards onto one predicate, and adds the trace harness that lets IME behavior be tested without an IME.

Before this, each surface open-coded its own composition check. They disagreed: some tested only isComposing, some only keyCode === 229, some nothing at all. isImeCompositionKeyDown is now the single place that decides a keystroke belongs to an IME, and 14 call sites migrate to it.

The predicate checks an app-wide composition flag as well as the per-event bits, because the two cover opposite gaps. keyCode === 229 (Windows) and key === 'Process' (Android) mark a key the IME consumed, and 229 is also what catches Safari's extra keydown after compositionend — the flag is already cleared by then, so it cannot help there. The flag covers the reverse: keys arriving during a live composition that carry neither marker.

The app-wide flag tracks the composing target, not a bare boolean, and revalidates on read that the target is still connected. Clearing on blur is the usual answer, because browsers do not reliably deliver compositionend when the composing element loses focus — but blur alone is not enough here: Chromium fires no focusout when a focused node is removed, so a composer that unmounts mid-composition would latch the flag on for the rest of the session and silently kill every Enter-to-commit surface in the app. Editors that scope composition state per editor instance escape this, since the state dies with the editor; a document-scoped flag has to revalidate instead.

shouldSuppressEnterSubmit now delegates to the same predicate, which brings the three remaining TaskPage.tsx sites in with it. An earlier revision of this PR described those three as telemetry-only reads; that was wrong — they are Enter-submit gates, so bare isComposing was missing an IME-owned Enter on Windows and Android.

Escape is exempt from the derived flag

The flag is derived state, and compositionend is not guaranteed to arrive. When it drifts, every key it guards goes dead — and eleven call sites guard Escape above their key dispatch, so the casualties are the rename fields, search boxes, pickers and panels whose only way out is Escape.

An Escape the IME owns should arrive marked (isComposing, 229, or 'Process') and is still suppressed by the clauses above. An unmarked Escape therefore means the flag has drifted, and swallowing it strands the user behind a flag nothing is going to clear. So the flag no longer applies to Escape; the per-event markers still do.

How well-evidenced that premise is, stated plainly: no trace in this stack contains a mid-composition Escape, recorded or derived, so the marking is argued rather than shown. The nearest evidence is the candidate-navigation trace, where mid-composition ArrowDown/ArrowUp carry isComposing: true with their own keyCodes (40/38) rather than 229 — exactly the shape the argument needs — but that trace is derived. The single recorded trace contains no mid-composition non-Process key at all.

The cost of being wrong is also not symmetric with the terminal's, which is why it is worth naming rather than burying. xterm-bypass-policy.ts accepts "worst case one extra keypress" for the same rule — benign. Here the Escape handlers run onClear() and onClose(), so an IME-owned-but-unmarked Escape would discard a search query or close a panel while the user was only dismissing a candidate window. Before this change that case was a silent no-op. The trade is still the right one — the drift outage is concrete and reproducible, this case is hypothetical — but it trades a no-op for a possible wrong action, not a free win. #11927's recorder settles it: one macOS or Windows recording of Escape during a candidate window turns this from derived reasoning into evidence.

This is the rule xterm-bypass-policy.ts already applies in the terminal, where Escape is deliberately absent from TERMINAL_IME_OWNED_KEYS for exactly this reason. Before this change the two disagreed about the same keystroke depending on which pane had focus. Fixing it in the predicate rather than at the call sites is what keeps them in agreement — patching eleven handlers would have re-created the scatter this PR exists to remove.

Recovery was already bounded rather than permanent, since the target revalidation above clears the flag on the next focus change. The failure was "Escape and Enter silently do nothing until you click away and back," with nothing telling the user that is the fix.

Trace harness and provenance

ime-composition-trace.test-fixtures.ts replays an event sequence against a real input element, stamping each event's observed buffer state in before dispatching it, so the code under test sees byte-for-byte what a browser produced.

Each trace declares its provenance:

  • recorded — captured from a real engine, authoritative.
  • derived — reconstructed from a documented event ordering. Pins current behavior, but cannot prove the ordering is what the engine really emits.

Six traces: five derived, one recorded. Read the provenance field, not this paragraph — but the shape of the set is worth stating up front, because the five derived ones are not all derived the same way.

trace provenance derived from
IBUS_HANGUL_TERMINAL_JAMO_COMMIT_TRACE recorded a live ibus-hangul engine, captured by tests/e2e/terminal-ime-boundary-probe.ts on ubuntu-22.04 (run 30691057123)
IBUS_HANGUL_RETAINED_COMMIT_TRACE derived the synthetic sequence tests/e2e/terminal-ime-observed-event-sequences.ts replays in CI
IBUS_HANGUL_MIXED_LATIN_TRACE derived same
TELEX_BACKSPACE_MID_COMPOSITION_TRACE derived the reported symptom in orca#6494 / #6698 / #6905
CANDIDATE_ARROW_NAVIGATION_TRACE derived the reported symptom in orca#9803
CANDIDATE_ESCAPE_DISMISSAL_TRACE derived the Escape exemption in ime-composition-keyboard-event.ts

Two of the derived traces are transcriptions of sequences CI hand-dispatches rather than captures, with selection offsets reconstructed as a collapsed caret at end-of-buffer. The other three are reconstructed from a documented event ordering around a real reported bug — they pin the behavior that was fixed, but nothing in them proves the engine emits that exact ordering.

Promotion is not blocked on someone's physical hardware, and an earlier revision of this paragraph wrongly said it was. The hangul trace above was recorded by CI, on a runner, from a real engine — that is what the IBus engine matrix higher in this stack exists to make routine, and CANDIDATE_ESCAPE_DISMISSAL_TRACE names config/scripts/record-ime-trace.mjs as its promotion path. What is genuinely still missing is coverage for engines the Linux matrix cannot host: macOS-native and Windows IMEs. Those need a human at the keyboard.

The trace assertions compare an interpretation derived from event data against each trace's declared final state, per the AGENTS.md rule that committed text comes from ranges and event data, never from diffing two observations of a mutable buffer. An earlier revision asserted the textarea's final contents instead, which was vacuous: the harness stamps that state in itself, so the assertion held no matter what any listener did.

Three fields the harness could not previously carry:

field why it has to survive replay
isComposing: undefined The field is typed boolean | undefined precisely so a Safari trace can be told from a Chromium one. The replayer coalesced it to false on construction and the repair function declined to touch it, so the one distinction it exists to express could not be reproduced.
repeat The accent panel a macOS press-and-hold opens is recognisable by nothing else. Without it, the two held keydowns replay identically to the first press.
selectionDirection Absent it, a preedit replacement range can only ever be represented as a collapsed caret, never a backward selection.

The first is the one worth flagging: undefined is a recorded value, not a missing one, and constructing an InputEvent coerces it. The replayer now defines the property explicitly, the same way it already patches the legacy keyCode.

Screenshots

No visual change — this preserves existing behavior at every migrated call site and fixes the sites that had no guard.

Testing

  • pnpm lint — oxlint 0 warnings / 0 errors
  • pnpm typecheck
  • pnpm test — predicate + trace-replay suites pass
  • pnpm audit:code-quality:type-aware
  • pnpm build — not run
  • Added or updated high-quality tests that would catch regressions
  • The unmount-latch regression test was confirmed to fail without its fix (expected true to be false) and pass with it
  • The trace interpreter was confirmed non-vacuous by mutating a trace's declared compositionend data: the new assertions failed, the old one still passed
  • All four new tests were run against the unfixed sources and each failed with the predicted error — expected true to be false (Escape), expected false to be undefined (isComposing), expected false to be true (repeat), expected [ 'none' ] to deeply equal [ 'backward' ] (selection direction)
  • Full renderer component suite (1,405 files / 11,733 tests) green, which is what covers the eleven Escape call sites the predicate change reaches

AI Review Report

A subagent audited the predicate against the behavior real engines produce rather than against what the spec implies. Four properties a correct composition guard needs, all reproduced here:

  • The same three-signal check (isComposing, 229, 'Process') rather than isComposing alone.
  • The guard placed at the top of keydown, before any per-key branching.
  • A derived app-wide composition flag alongside the per-event bits, for the keys that carry no marker.
  • Composition state cleared when the composing element goes away — which is where the unmount gap above came from.

One deliberate design choice, now documented in the code: the 229/'Process' markers are unconditional, with no opt-out argument. xterm-bypass-policy.ts lets a bare 229 keydown through on macOS/Linux, so the two look inconsistent — a review pass raised exactly that. The divergence is correct, but the original justification ("only the terminal needs it") named the wrong reason, and the comment now states the right one: the terminal needs the keystroke because xterm's CompositionHelper reads the committed glyph off the helper textarea, whereas callers of this predicate receive committed text through input. A marked keydown carries nothing they could act on — its key is 'Process' or 'Unidentified', never the glyph — so suppressing it costs no input.

The one call site that looked like a counterexample, the emulator screen keyboard, was checked and is not one: it forwards through an ASCII/named-key USB HID table, which returns null for 'Process' and drops the keystroke identically whether or not the guard fires. At one site suppressing is better than merely harmless — the address picker concatenates event.key into a typeahead prefix, which 'Process' would have poisoned.

Cross-platform: the predicate is explicitly the union of the macOS, Windows (229), and Android ('Process') markers; no platform branch, because all three markers are safe to check everywhere.

Security Audit

  • Input handling: this is keyboard-event classification only. It never reads or forwards event content, so there is no injection surface.
  • No command execution, path handling, auth, secrets, dependency, or IPC changes.
  • Note the guard widens suppression at previously-unguarded sites, which can only prevent actions, never trigger new ones.

Notes

Base: #11894. The trace harness added here is the test substrate for #11896 and #11897.

@nwparker
nwparker force-pushed the nwparker/ime-test-fixtures branch from ae0784f to 2a55af0 Compare August 1, 2026 08:10
@nwparker
nwparker force-pushed the nwparker/ime-test-fixtures branch from 2a55af0 to 0df9009 Compare August 1, 2026 08:12
@nwparker
nwparker force-pushed the nwparker/ime-test-fixtures branch from 0df9009 to 02239a4 Compare August 1, 2026 08:20
@nwparker
nwparker force-pushed the nwparker/ime-test-fixtures branch 2 times, most recently from ca8eda2 to d64da8c Compare August 1, 2026 08:43
@nwparker
nwparker force-pushed the nwparker/ime-test-fixtures branch 2 times, most recently from 7d3746e to f0497dd Compare August 1, 2026 09:17
@nwparker
nwparker force-pushed the nwparker/ime-test-fixtures branch 2 times, most recently from f65dfe0 to b2b6ebc Compare August 1, 2026 10:14
@nwparker
nwparker force-pushed the nwparker/ime-test-fixtures branch 3 times, most recently from 0f1db5a to cf70f6b Compare August 2, 2026 03:16
@nwparker nwparker closed this Aug 2, 2026
@nwparker nwparker reopened this Aug 2, 2026
nwparker and others added 4 commits August 1, 2026 21:47
Synthetic keystrokes cannot produce a composition — KeyboardEvents dispatched
through CDP bypass the OS input-method layer — so the IME suites each grew their
own ad-hoc dispatch helpers that assert against invented event orderings.

Adds a trace harness that replays an event sequence captured from a real engine,
stamping the observed buffer before each event so the code under test sees what
the browser actually produced. Traces carry a provenance field: `recorded`
traces come from a real engine and are authoritative, `derived` traces pin
current behavior but prove nothing about the engine, so a fix justified only by
a derived trace is visibly unverified. Two of the four seeded traces are
recorded from ibus-hangul in CI; the Telex and Pinyin ones are derived and say
so.

Also replaces 17 inline `nativeEvent.isComposing` guards with one predicate.
`isComposing` alone misses the extra keydown Safari emits after compositionend
and the engines that report only keyCode 229 or key 'Process', so each call site
was subtly different. The shared predicate adds a document-wide composition flag
cleared on focusout, because compositionend is not delivered when the composing
element loses focus. It also tracks the composing target and revalidates that the
target is still connected on read: Chromium fires no focusout when a focused node
is removed, so a composer that unmounts mid-composition would otherwise latch the
flag on for the session and kill every Enter-to-commit surface in the app.

The three remaining TaskPage sites go through shouldSuppressEnterSubmit, which now
delegates to the same predicate. An earlier version of this message called them
telemetry-only; they are Enter-submit gates, so bare isComposing was missing an
IME-owned Enter on Windows and Android.

Trace assertions compare an interpretation derived from event data against each
trace's declared final state. The previous assertion read back the buffer the
harness itself had stamped, so it could not fail.

Co-authored-by: Orca <[email protected]>
An Escape the IME owns always arrives marked, so an unmarked one means the
document flag has drifted. compositionend is not guaranteed, and eleven
call sites guard Escape above their key dispatch, so the drift left rename
fields, search boxes and pickers with no way to cancel.

Matches the rule xterm-bypass-policy.ts already applies to the terminal.

Co-authored-by: Orca <[email protected]>
The schema could not express repeat or selectionDirection, and the replayer
coerced an absent isComposing to false — the one distinction the field is
typed `boolean | undefined` to capture.

repeat is what a press-and-hold accent panel is recognised by, so a trace
recorded for that quirk could not reproduce it.

Co-authored-by: Orca <[email protected]>
The Escape exemption assumes an IME-owned Escape arrives marked. Nothing
recorded showed that, so the assumption lived only in prose. Encode it as a
labelled derived trace with the command that promotes it.

Co-authored-by: Orca <[email protected]>
@nwparker
nwparker force-pushed the nwparker/ime-test-fixtures branch from 21b091b to 22991a5 Compare August 2, 2026 04:49
@nwparker

nwparker commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #13128 (17b3dff) — the composition predicate it pairs with is gone. The recorded-trace discipline itself was the right instinct and is what the surviving tests use; see #11927 for the recorder, which I have left open.

@nwparker nwparker closed this Aug 8, 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.

1 participant