Repository navigation
fix(mobile): decide terminal preedit from the marked-text range, not a script table - #15007
Conversation
…a script table The live terminal capture field decided what to withhold from the PTY with a Unicode-block allowlist (Hangul jamo and syllables) and held exactly one trailing code point. Kana and kanji are not in the table, so a Japanese reading streamed to the PTY one fragment at a time and was repaired afterwards with DEL bytes (#7427). A code-point table cannot work, and the counterexample is not exotic: Chinese pinyin preedit is plain ASCII, and a Japanese romaji reading is one code point on the first keystroke and three on the fourth. Preedit is a property of the FIELD, not of the characters in it, so the only signal that identifies it is the text system's marked-text range. That is what a reference terminal implementation uses on every platform it supports - `hasMarkedText` there, the input-method context's composing state elsewhere - and neither one classifies code points anywhere in the input path. So the mirror now takes the marked-text report per change and holds the whole preedit region, whatever its length or script: - Subscribe the capture field to `onChange`, not `onChangeText`; only the raw native event carries the report at all. - A reported preedit is held entire and is never committed by the settle timer, because preedit is not text yet. Explicit boundaries still flush it. - `isTerminalLiveHangulCodePoint` and its four ranges are deleted. iOS reports the range but React Native drops it before JS, so the pinned patch forwards `markedTextRange` into the change payload. It is three hunks and it compiles because the app already sets `buildReactNativeFromSource` for iOS. The same idea was proposed in #11450, which is where the patch comes from. Android has no marked-text report in React Native at all, and a Kotlin patch would not help: Android consumes the prebuilt react-android artifact, so node_modules sources are never compiled. Until the report exists there, the fallback holds the trailing non-ASCII run. It enumerates nothing, it covers kana, kanji and Hangul, and ASCII keeps its zero-latency echo - but it cannot see an ASCII preedit, so Chinese pinyin on Android still leaks its reading. Only a report fixes that. Not-tested: no physical device or emulator was available, so no real IME drove this path. Japanese, Chinese and Korean composition are covered at the model and hook level only, and the iOS patch has not been compiled. Co-authored-by: Brennan Benson <[email protected]>
📝 WalkthroughWalkthroughThe live terminal input now receives native change events with composition state. A React Native patch reports marked-text status through Fabric text input events. Terminal mirroring now handles reported and inferred preedit text, computes deletion and append operations, and delays commits only when composition state is unknown. Tests cover Japanese, Chinese, Hangul, Vietnamese, ASCII, corrections, deletions, commits, and input lifecycle behavior. 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
mobile/src/terminal/terminal-live-preedit-mirror.ts (1)
16-27: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueShorten the preedit fallback comment.
Keep the invariant near
heldPreeditLength. Move the detailed platform and behavior explanation to focused documentation if it must remain.As per coding guidelines, “Comments must be concise, non-obvious, and brief—prefer one line; do not explain obvious behavior or walk through code.”
Proposed comment reduction
-/** - * How much of the field the text system can still rewrite, in code points. - * - * Preedit is a fact about the field, never about the script — Chinese pinyin - * preedit is plain ASCII — so the marked-text range decides and no code point is - * ever classified. That is what makes this hold for input methods nobody tested. - * - * `composing` is undefined only where the platform reports no range at all — - * today React Native Android. The fallback holds the trailing non-ASCII run: a - * conversion IME always leaves one and ASCII keeps its zero-latency echo. It - * enumerates nothing, and it cannot see an ASCII preedit — only a report can. - */ +// Hold reported preedit; without a report, hold the trailing non-ASCII run.Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 41b04128-2cc4-4618-b7b4-4adf46056c8a
⛔ Files ignored due to path filters (1)
mobile/pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (11)
mobile/app/h/[hostId]/session/[worktreeId].tsxmobile/package.jsonmobile/patches/[email protected]mobile/src/terminal/terminal-ios-ime-keyboard.test.tsmobile/src/terminal/terminal-live-hangul-mirror.test.tsmobile/src/terminal/terminal-live-hangul-mirror.tsmobile/src/terminal/terminal-live-preedit-mirror.test.tsmobile/src/terminal/terminal-live-preedit-mirror.tsmobile/src/terminal/use-terminal-live-input-commit.test.tsmobile/src/terminal/use-terminal-live-input-commit.tsmobile/src/terminal/use-terminal-live-pending-input-flush.ts
💤 Files with no reviewable changes (2)
- mobile/src/terminal/terminal-live-hangul-mirror.test.ts
- mobile/src/terminal/terminal-live-hangul-mirror.ts
Included review availability: Your plan includes up to 10 reviews per rolling hour; 5 remain after this review.
The no-report branch walked the trailing non-ASCII run over the whole field and ignored stableLength, unlike the reported branch directly above it. So after a settle-timer commit the next keystroke re-held everything already delivered and the caller erased it with DEL and retyped it — a nine-character Cyrillic word cost a DEL per already-sent character, and for the 300ms before the re-send the held text was the only copy, so a blur or reconnect destroyed characters the pty already had. Bound it the way the reported branch is bounded. Pinned by a test that drives a settle commit between every keystroke and asserts no DEL reaches the wire.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
mobile/src/terminal/terminal-live-preedit-mirror.ts (1)
44-47: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winUse concise comments in the preedit mirror changes.
mobile/src/terminal/terminal-live-preedit-mirror.ts#L44-L47: replace the four-line explanation with one comment that states the sent-text invariant.mobile/src/terminal/terminal-live-preedit-mirror.test.ts#L259-L285: retain one Android-specific comment and remove redundant rationale and BDD labels.As per coding guidelines, “Comments must be concise, non-obvious, and brief—prefer one line.”
Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 9994deb0-9f48-45ce-8059-e6ca3a925c97
📒 Files selected for processing (2)
mobile/src/terminal/terminal-live-preedit-mirror.test.tsmobile/src/terminal/terminal-live-preedit-mirror.ts
Included review availability: Your plan includes up to 10 reviews per rolling hour; 1 remains after this review.
…a script table (stablyai#15007) * fix(mobile): decide terminal preedit from the marked-text range, not a script table The live terminal capture field decided what to withhold from the PTY with a Unicode-block allowlist (Hangul jamo and syllables) and held exactly one trailing code point. Kana and kanji are not in the table, so a Japanese reading streamed to the PTY one fragment at a time and was repaired afterwards with DEL bytes (stablyai#7427). A code-point table cannot work, and the counterexample is not exotic: Chinese pinyin preedit is plain ASCII, and a Japanese romaji reading is one code point on the first keystroke and three on the fourth. Preedit is a property of the FIELD, not of the characters in it, so the only signal that identifies it is the text system's marked-text range. That is what a reference terminal implementation uses on every platform it supports - `hasMarkedText` there, the input-method context's composing state elsewhere - and neither one classifies code points anywhere in the input path. So the mirror now takes the marked-text report per change and holds the whole preedit region, whatever its length or script: - Subscribe the capture field to `onChange`, not `onChangeText`; only the raw native event carries the report at all. - A reported preedit is held entire and is never committed by the settle timer, because preedit is not text yet. Explicit boundaries still flush it. - `isTerminalLiveHangulCodePoint` and its four ranges are deleted. iOS reports the range but React Native drops it before JS, so the pinned patch forwards `markedTextRange` into the change payload. It is three hunks and it compiles because the app already sets `buildReactNativeFromSource` for iOS. The same idea was proposed in stablyai#11450, which is where the patch comes from. Android has no marked-text report in React Native at all, and a Kotlin patch would not help: Android consumes the prebuilt react-android artifact, so node_modules sources are never compiled. Until the report exists there, the fallback holds the trailing non-ASCII run. It enumerates nothing, it covers kana, kanji and Hangul, and ASCII keeps its zero-latency echo - but it cannot see an ASCII preedit, so Chinese pinyin on Android still leaks its reading. Only a report fixes that. Not-tested: no physical device or emulator was available, so no real IME drove this path. Japanese, Chinese and Korean composition are covered at the model and hook level only, and the iOS patch has not been compiled. Co-authored-by: Brennan Benson <[email protected]> * fix(mobile): bound the fallback hold to text the pty has not received The no-report branch walked the trailing non-ASCII run over the whole field and ignored stableLength, unlike the reported branch directly above it. So after a settle-timer commit the next keystroke re-held everything already delivered and the caller erased it with DEL and retyped it — a nine-character Cyrillic word cost a DEL per already-sent character, and for the 300ms before the re-send the held text was the only copy, so a blur or reconnect destroyed characters the pty already had. Bound it the way the reported branch is bounded. Pinned by a test that drives a settle commit between every keystroke and asserts no DEL reaches the wire. --------- Co-authored-by: Brennan Benson <[email protected]>
…a script table (stablyai#15007) * fix(mobile): decide terminal preedit from the marked-text range, not a script table The live terminal capture field decided what to withhold from the PTY with a Unicode-block allowlist (Hangul jamo and syllables) and held exactly one trailing code point. Kana and kanji are not in the table, so a Japanese reading streamed to the PTY one fragment at a time and was repaired afterwards with DEL bytes (stablyai#7427). A code-point table cannot work, and the counterexample is not exotic: Chinese pinyin preedit is plain ASCII, and a Japanese romaji reading is one code point on the first keystroke and three on the fourth. Preedit is a property of the FIELD, not of the characters in it, so the only signal that identifies it is the text system's marked-text range. That is what a reference terminal implementation uses on every platform it supports - `hasMarkedText` there, the input-method context's composing state elsewhere - and neither one classifies code points anywhere in the input path. So the mirror now takes the marked-text report per change and holds the whole preedit region, whatever its length or script: - Subscribe the capture field to `onChange`, not `onChangeText`; only the raw native event carries the report at all. - A reported preedit is held entire and is never committed by the settle timer, because preedit is not text yet. Explicit boundaries still flush it. - `isTerminalLiveHangulCodePoint` and its four ranges are deleted. iOS reports the range but React Native drops it before JS, so the pinned patch forwards `markedTextRange` into the change payload. It is three hunks and it compiles because the app already sets `buildReactNativeFromSource` for iOS. The same idea was proposed in stablyai#11450, which is where the patch comes from. Android has no marked-text report in React Native at all, and a Kotlin patch would not help: Android consumes the prebuilt react-android artifact, so node_modules sources are never compiled. Until the report exists there, the fallback holds the trailing non-ASCII run. It enumerates nothing, it covers kana, kanji and Hangul, and ASCII keeps its zero-latency echo - but it cannot see an ASCII preedit, so Chinese pinyin on Android still leaks its reading. Only a report fixes that. Not-tested: no physical device or emulator was available, so no real IME drove this path. Japanese, Chinese and Korean composition are covered at the model and hook level only, and the iOS patch has not been compiled. Co-authored-by: Brennan Benson <[email protected]> * fix(mobile): bound the fallback hold to text the pty has not received The no-report branch walked the trailing non-ASCII run over the whole field and ignored stableLength, unlike the reported branch directly above it. So after a settle-timer commit the next keystroke re-held everything already delivered and the caller erased it with DEL and retyped it — a nine-character Cyrillic word cost a DEL per already-sent character, and for the 300ms before the re-send the held text was the only copy, so a blur or reconnect destroyed characters the pty already had. Bound it the way the reported branch is bounded. Pinned by a test that drives a settle commit between every keystroke and asserts no DEL reaches the wire. --------- Co-authored-by: Brennan Benson <[email protected]>
Fixes #7427. Supersedes #13070 and #12341, and adopts the necessary half of #11450 with credit.
Root cause: a script allowlist, again
terminal-live-hangul-mirror.tsdecided what to withhold from the pty with a four-range Hangul codepoint allowlist, and held exactly one trailing code point. Both halves are Hangul-specific tricks dressed as general rules:sあinstead ofさ.きょうis a three-code-point reading, so adding kana to the table still leaksきょ.This is the same antipattern the desktop terminal already removed: a 23-term input-source allowlist plus a 30-character punctuation table, replaced by a structural rule that reads
keyfor length only, never identity.The counterexample that settles it
Chinese pinyin preedit is plain ASCII.
n→ni→niha→nihao→你好.No codepoint rule of any kind can tell that from someone typing
nihaoat a shell prompt. That single fact refutes every table-based approach, including the one shipped today and the one #13070 proposes. It is now a test.The structural signal
The text system's marked-text / composing range, delivered per change — never a property of the characters:
composing === true→ hold the entire unsent suffix, any length, any script. The settle timer is not armed: preedit is not text yet, so nothing may promote it on a timer. Explicit boundaries still flush.composing === false→ hold nothing; zero-latency echo, unchanged.composing === undefined(platform reports no range) → hold the trailing non-ASCII run. Enumerates nothing, covers kana/kanji/Hangul/Cyrillic, and ASCII keeps zero lag. Honest limitation: it cannot see an ASCII preedit.isTerminalLiveHangulCodePointand its four ranges are deleted. The file is renamed because the old name enshrined the antipattern.The wiring change that made the signal visible at all:
onChangeText→onChange.onChangeTexthands over a bare string and throws the report away.Why a React Native patch is genuinely required
Upstream RN 0.83.9 does not expose composition state —
_textInputMetricsinRCTTextInputComponentView.mmhas no such field. #11450's premise is correct and its patch is necessary; it is adopted here, regenerated cleanly viapnpm patchagainst pristine 0.83.9, with @brennanb2025 credited as co-author.It takes effect because the app already sets
expo-build-properties.ios.buildReactNativeFromSource: true, so iOS compiles RN fromnode_modules.Android cannot be fixed this way and nobody should try. RN Android has no composing flag anywhere (
ReactTextChangedEventcarriestext+eventCountonly), and Android consumes the prebuiltreact-androidMaven AAR — a Kotlin patch innode_modulesis never compiled. That asymmetry is exactly why theundefinedfallback exists.What reference terminals do
Neither gates on script — grepping both input paths for
0x1100/0xAC00/0x3040/0x4E00returns zero hits. One testsmarkedTextRange.length > 0and consumes the key; the other carries acomposing: boolon the key event and returns early before encoding, with a comment whose worked example is Japanese. In both, preedit is never sent and commit is the only thing that reaches the pty. Neither has any "send it, then repair with DEL" concept — which is why the DEL machinery here survives only as the Android no-report fallback.Verdicts on the other PRs
Proof the test discriminates
Restored main's algorithm behind the new signature, kept the new tests:
The first is #7427 reproduced at the hook level, DEL repair and all. 10 tests fail on the old algorithm; all pass on the new one.
Size
Application production TS: +122 / −79 = net +43. Not net-negative, and I won't pretend otherwise — roughly 14 lines are the rationale comment and ~20 are formatter-expanded signatures for threading one optional parameter. What it does buy is that there is now exactly one hold rule with one input, instead of a table that needs extending per script. Plus the vendored patch (+48),
package.json(+3), lockfile (+5, hand-limited to thepatchedDependenciesblock — pnpm's rewrite had dragged in ~470 lines of unrelated dependency churn).Verified
450 files / 3560 tests pass, 3 skipped.
tsc --noEmit,oxlint,oxfmt --checkclean.pnpm install --frozen-lockfilesucceeds and the patch lands innode_modules— I re-verified that independently.Not verified — needs a device
buildReactNativeFromSourceis set; the app was not built.isComposingisfalseversus absent on Android at runtime. Read from RN source, not observed. If Android ever deliveredfalse, the fallback would be bypassed and Korean would regress there — first thing to check on a device.한글as one unit at the settle timer rather than streaming한then글. Safer, fewer DELs, less live. Unvalidated against a real Gboard IME.A note for anyone hoping #13366 closes the device gap: it does not. It is a build/boot/lifecycle harness; its own docs say a Play Store image and hand-configuration are needed for a CJK keyboard. Useful for manual repro, but it will not produce an automated romaji or flick test.
Readiness review — one P1 found and fixed
The supply-chain check came back cryptographically clean, verified rather than asserted: the patch's pre-image blob hashes match the pristine npm tarball exactly, its post-image hashes match what is installed, a full recursive diff of pristine-vs-installed React Native shows exactly the three files the patch names and nothing else, and the lockfile's integrity hash is the true SHA-256 of the patch file. It adds one optional boolean — no scripts, no hooks, no network, no binaries — and it genuinely reaches the iOS binary via
buildReactNativeFromSource: trueand a local CocoaPods build.P1, now fixed: the
composing === undefinedbranch walked the trailing non-ASCII run over the whole field and ignoredstableLength, unlike the reported branch directly above it. After a settle-timer commit, the next keystroke re-held everything already delivered, and the caller erased it with DEL and retyped it.Measured: a nine-character Cyrillic word produced a DEL per already-sent character. Worse, the DELs go out immediately while the re-send waits 300 ms — and in that window the held text is the only copy, so a connection blip, tab switch, or keyboard toggle left the word erased from the prompt and dropped from the field, destroying characters the pty already had. On
mainthat was structurally impossible, since the hold was one Hangul code point.Bounded the fallback the same way the reported branch is bounded. All nine original vectors still pass. Pinned by a new test that drives a settle commit between every keystroke and asserts no DEL reaches the wire; without the bound it fails with
п\x7fпр\x7f\x7fпри….Correction to this PR body: the earlier claim that Android Korean produces "fewer DELs" was measurably backwards before this fix. With the bound it is now true — zero DELs for the non-composing case.
Follow-up, not fixed here (P2):
use-terminal-live-accessory-input-commit.tstypesapplyLiveInputMirrorwith two parameters and drops the newcomposingargument, so an accessory Backspace during a reported iOS composition takes the fallback branch and arms the settle timer — the one thing this design forbids. It is not a regression frommain, which leaked there too, and the right value for the accessory to pass is a design question rather than a mechanical thread-through, so it is deliberately left for its own change.