Skip to content

fix(terminal): empty the IME textarea when a key moves the shell cursor - #2900

Merged
devswha merged 6 commits into
mainfrom
lane/issue-2836
Nov 5, 2026
Merged

devswha merged 6 commits into
mainfrom
lane/issue-2836

Conversation

@devswha

@devswha devswha commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

Change

Fixes #2836.

The keyCode 229 fallback in the patched xterm CompositionHelper (patches/@xterm%[email protected]) sends an IME replacement as one DEL per removed character plus the inserted text. It assumes the shell cursor sits at the end of the hidden textarea's text. xterm only emptied the textarea on Enter, Ctrl+C, Backspace, paste and blur, so after a key that moves the shell cursor without touching the textarea, that assumption no longer held. The keys were an arrow, Home, Tab, Esc or a Ctrl/Alt key, from a hardware keyboard or from the key bar through Terminal.input(). A Gboard rewrite of the last word then sent its DELs at the moved cursor and deleted text the user never edited.

  • CoreBrowserTerminal._keyDown now empties the textarea after any control key it sends (first character below 0x20, or DEL), not only Backspace. It keeps the same guards: only outside a composition, and not in screen reader mode, where the uncancelled key moves the textarea's caret along with the shell cursor. Enter and Ctrl+C still clear unconditionally, as before.
  • Terminal.input() (key bar keys, sticky-modifier chords, Cmd+arrows on a Mac) calls the new CompositionHelper.clearTextarea(). It finishes a pending Safari replacement as before, then empties the textarea. A composition still in progress, or one whose deferred send has not run yet, keeps its text. So nothing new is sent ahead of the embedder's own data, which Ovis labels as the key bar key or the Stop.
  • Printable keys are unchanged. The Gboard on-screen keyboard is unchanged too: its keys are 229, and its Enter and Backspace already cleared the textarea.

Reproduction

Real Chromium (playwright-core) with the patched xterm built by scripts/build-xterm.ts. No server, no herdr session. The script typed abcdef through the 229 path, moved the cursor, then had the IME rewrite the word it sees before its caret into X:

Move origin/main 483fdb8: textarea after move, then sent This branch
none (control) abcdef, then 6×DEL X same (correct: the cursor is at the end)
hardware ArrowLeft abcdef, then ESC[D, 6×DEL X empty, then ESC[D, X
hardware Home abcdef, then ESC[H, 6×DEL X empty, then ESC[H, X
hardware Tab abcdef, then \t, 6×DEL X empty, then \t, X
key bar term.input("\x1b[D") abcdef, then ESC[D, 6×DEL X empty, then ESC[D, X

Tests

scripts/xterm-composition.test.ts now also builds the patched CoreBrowserTerminal. It drives a real, unopened terminal (fake textarea, real key evaluation, real onData), alongside the existing helper-only cases:

  • ArrowLeft, Home, Tab, Escape, Ctrl+A and Alt+B, followed by a Gboard word rewrite, send no DELs.
  • The embedder's input, followed by the same rewrite, sends no DELs.
  • A composition that just ended is still sent once, by its own deferred send, after the embedder's input. The embedder's input stays the first data event.

With origin/main's patch the 7 rewrite cases fail: the rewrite sends 6×DEL X. The last case guards the review fix: it fails with the first commit's patch, which sent the commit first. With this branch, 18/18 pass.

Not changed

The issue's lower-impact case, an edit before the end of the textarea, still forwards only a plain insertion. That happens when the textarea's caret was moved inside it, for example by Gboard's space-bar swipe, which moves the caret without a key. Fixing it changes what the pane receives for caret moves inside the field and needs a device check, so it is left out of this PR.

Validation

  • bun run check fast at aa7cc3e (main merged in, including fix(i18n): keep settings part files under the line cap #2886): workflow syntax, generated types and themes, typecheck, build and unit tests pass (11665 pass, 0 fail, 46 skip). Earlier runs on this branch before that merge failed only src/lib/i18n.test.ts's 400-line cap, which was red on main until fix(i18n): keep settings part files under the line cap #2886.
  • bun test scripts/xterm-composition.test.ts: 18 pass. Before the patch change: 10 pass, 8 fail.
  • bun test scripts/changelog-fold.test.ts scripts/conventions.test.ts scripts/build-xterm.test.ts scripts/xterm-parser-state.test.ts: 45 pass.
  • A clean bun install --frozen-lockfile applies the regenerated patch. bun.lock is unchanged.
  • The Chromium reproduction above, before and after.

Not run: bun run check full or the browser lane, and a check on a physical Android device with Gboard or on iOS. One thing for an iPhone check: the key bar now leaves the helper textarea empty until the next key, focus or input event puts back its one-space placeholder (#862). The first ⌫ keydown puts it back, as it does after Enter.

🤖 Generated with Claude Code

The keyCode 229 fallback in the patched xterm CompositionHelper sends an
IME replacement as one DEL per removed character plus the inserted text,
assuming the shell cursor sits at the end of the hidden textarea's text.
A key that moves that cursor without touching the textarea broke that:
after an arrow, Home, Tab, Esc or Ctrl key (hardware, or the key bar
through Terminal.input), a Gboard rewrite of the last word sent its DELs
at the moved cursor and deleted text the user never edited.

_keyDown now empties the textarea after any control key, not only
Backspace, with the same guards (outside composition, not in screen
reader mode). Terminal.input() calls the new
CompositionHelper.clearTextarea(), which finishes a pending replacement,
sends a just finished composition first, then empties the textarea; a
composition still in progress keeps its text.

Fixes #2836

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 19 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between d2a2c6a and fae565e.

Files selected for processing: 4

  • changelog.d/terminal-ime-moved-cursor.md
  • docs/terminal-input.md
  • patches/@xterm%[email protected]
  • scripts/xterm-composition.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

devswha and others added 2 commits November 5, 2026 15:58
… commit

Terminal.input() drained a just finished composition's deferred send
before the embedder's own data. Ovis labels the next data event as the
key bar key or the Stop (useTerminalMount's onData), so that commit took
the label and the Escape went out as plain input; the early send could
also read the textarea before the IME's final edit landed.

clearTextarea() now sends nothing itself: a composition in progress, or
one whose deferred send has not yet read the textarea, keeps its text,
as before this branch. Found in review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Deploying ovis with  Cloudflare Pages  Cloudflare Pages

Latest commit: 04ba1dc
Status: ✅  Deploy successful!
Preview URL: https://022fee66.ovis.pages.dev
Branch Preview URL: https://lane-issue-2836.ovis.pages.dev

View logs

@devswha

devswha commented Nov 5, 2026

Copy link
Copy Markdown
Owner Author

Review: Codex (gpt-6.1-terra, xhigh, adversarial) on 96ad617, and how each finding was resolved

One fix round, a5fa0f5. CodeRabbit skipped this PR, so this review stands in for it.

1. Embedder input's label taken by a pending commit (reported HIGH): fixed. In 96ad617, Terminal.input() drained a composition's deferred send when the composition had just ended, before sending the embedder's own data. Ovis labels the next data event as the key bar key or the Stop (useTerminalMount's onData reads barKeyRef and stopKeyRef once). The Composer's Stop (abortTurn) sets that label without waiting for a composition commit, so the commit took the Stop label and the Escape went out as plain input. In a5fa0f5, clearTextarea() sends nothing itself. A composition in progress, or one whose deferred send has not run yet, keeps the textarea, as on main. The test leaves a just finished composition to its own send after the embedder's input checks that the embedder's input is the first data event. It fails on 96ad617 and passes on a5fa0f5.

2. The early send could read the textarea before the IME's final edit (reported MEDIUM): fixed by the same change. Nothing is sent ahead of its deferred send any more.

3. A cursor key during an active composition does not clear (reported HIGH): no change, kept on purpose. The new rule keeps the guards Backspace already had: only outside a composition, and not in screen reader mode. An open IME composition is the browser's to finish, and clearing under it is what the Backspace guard avoids. I found no input path that reaches this. Desktop IMEs report a key pressed during a composition as keyCode 229, which never gets here. Gboard commits the composition before it forwards a hardware arrow, so the composition has already ended at the keydown. The key bar and sticky chords wait for a direct-typing composition (composingRef / compositionCommitPendingRef). A Safari replacement syllable is committed by the key itself and is not re-edited after a cursor key.

4. iOS: the key bar leaves the helper textarea without its one-space placeholder (#862) until the next focus, key or input (reported MEDIUM): no change, not verified on a device. Ovis's simulator notes (docs/terminal-input.md, "Native iOS keyboard check") say the keyboard repeats ⌫ "only while the focused field has text, cancelled keydowns included". The first ⌫ keydown puts the space back (keepText on keydown), as it does after Enter, which also empties the field. scripts/key-bar-hold-repeat-regression.ts does not tap the key bar before its iPhone field checks, so it is unaffected. An iPhone simulator check (tap a key bar arrow, then hold ⌫) would settle it.

5. A mouse-driven cursor move (a click in a mouse-reporting app, or xterm's Alt+click) still keeps the textarea (reported MEDIUM): not in this PR. Mouse reports reach the pane through xterm's mouse service, not _keyDown or input(). The issue names keys and embedder input. Covering mouse reports means emptying the textarea on mouse traffic as well (any-event tracking sends a report per move), which is a separate change. This is a follow-up candidate, not reproduced.

Validation after the fix round: bun test scripts/xterm-composition.test.ts 18 pass. bun run check fast at aa7cc3e (after merging main): all steps pass, unit tests 11665 pass, 0 fail.

@devswha

devswha commented Nov 5, 2026

Copy link
Copy Markdown
Owner Author

Second Codex pass after the fix round (aa7cc3e), and the main merge

origin/main (15e4344) is merged on top of aa7cc3e, without conflicts: ef1aafa.

Codex (gpt-6.1-terra, xhigh, adversarial) reviewed the PR again after the fix round. It found no HIGH issues and two MEDIUM ones:

  • A hardware arrow that ends an open composition does not clear the textarea (patches/@xterm%[email protected]:185). This is finding 3 above, already answered: the guard Backspace has is kept on purpose, and no input path reaching it was found.
  • New: the embedder path clears the textarea in screen reader mode too (patches/@xterm%[email protected]:529). Terminal.input() calls clearTextarea() without the screenReaderMode guard that the _keyDown path keeps. A key bar arrow tapped with a screen reader on empties the field it reads, which can change its next character and deletion announcements. This was not reproduced with a screen reader. The PR already had its one fix round, so this is left as a follow-up: add the same screenReaderMode check in clearTextarea() or at its input() call site.

Validation at ef1aafa: bun run check fast passed (workflow syntax, generated types and themes, typecheck, build, unit 11669 pass / 0 fail, including scripts/xterm-composition.test.ts).

@devswha

devswha commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner Author

Merge of origin/main and a review at the new head (04ba1dc)

origin/main (b2aadc4) is merged into this branch as one merge commit, 04ba1dc, without conflicts. main's changes since the last merge touch none of this PR's files.

Codex (gpt-6.1-terra, xhigh, adversarial) reviewed the PR again at the merge of 0c41431 (6ca8265a; the PR's own diff is the same at 04ba1dc). It found no HIGH issues and one MEDIUM:

  • Terminal.input() in the same task as a composition's end leaves the textarea as it was (patches/@xterm%[email protected]:528). When compositionend has fired but its deferred send (setTimeout 0) has not run yet, clearTextarea() keeps the text on purpose, so the commit is not sent under the embedder's label (finding 1 of the first review). Nothing clears it after that send, so a later Gboard rewrite could send DELs for text before the cursor move. Codex reproduced this with synthetic events. No change: no Ovis caller of Terminal.input() can run inside that window. The key bar and the sticky-modifier chords return while composingRef or compositionCommitPendingRef is set (src/lib/useTypedKeys.ts, src/lib/useTerminalMount.ts), and that ref stays set until a timeout queued after xterm's deferred send. Cmd+arrows run in the bubble phase, after xterm's own keydown listener has drained a pending send (_finalizeComposition(false)). The Composer's Stop is a click, which arrives in a later task than the compositionend that the press caused. Clearing from inside the deferred send would race the 229 fallback's own timers, so it stays as it is unless a real input path turns up.

The screen reader follow-up from the earlier comment still stands.

Earlier unresolved review threads: none.

Validation at 04ba1dc: bun run check fast passed (workflow syntax, generated types and themes, typecheck, build, unit tests 11922 pass / 0 fail, including scripts/xterm-composition.test.ts).

@devswha

devswha commented Nov 5, 2026

Copy link
Copy Markdown
Owner Author

A second merge of origin/main (fae565e)

#2962 changed the same CompositionHelper.updateCompositionElements hunk of patches/@xterm%[email protected] (Firefox's IME candidate window), so origin/main (d2a2c6a) is merged once more. Only the hunk header and the file's index line conflicted. The header is now @@ -218,27 +431,57 @@: main's longer old side, and this PR's 14-line offset with main's added lines. The index blob is recomputed from the patched file. The patch applies with bun install --frozen-lockfile, and scripts/xterm-composition.test.ts passes 19 tests (this PR's 18 and #2962's Firefox test). The PR's diff against main is unchanged (4 files).

Validation at fae565e: bun run check fast passed (typecheck, build, unit tests 11997 pass / 0 fail).

@devswha

devswha commented Nov 5, 2026

Copy link
Copy Markdown
Owner Author

Local merge gate (owner-approved): adopted and re-driven on old-desktop: origin/main merged, bun run check fast passed at this head, review at this head (omo/Codex) with no reproduced HIGH; findings and resolutions are in the PR comments; threads resolved; merges cleanly. CodeRabbit was rate-limited where noted, so the omo + Codex reviews stand in. Merged without waiting for GitHub CI.

@devswha
devswha merged commit 2da018f into main Nov 5, 2026
13 checks passed
@devswha
devswha deleted the lane/issue-2836 branch November 5, 2026 12:37
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.

terminal: 229 fallback can delete text after the shell cursor moves

1 participant