Repository navigation
Fix #1883: send mobile keyboard replaces to the PTY as DELs plus new text - #1884
Conversation
…text A soft keyboard's autocomplete, autocorrect and smart punctuation edit text already committed in xterm's hidden textarea. xterm's textarea handlers assume append-only edits: on keydown 229 _handleAnyTextareaChanges resends the whole textarea once the old value is no longer a substring, and compositionend sends a tail misaligned with a recomposed word. The PTY got the partial word and the preceding clause again. On coarse-pointer devices the Terminal now owns IME text: an IME bridge keeps composition/input events and keydown 229 away from xterm and sends each textarea edit as reconcileComposition(mirror, value). A mirror floor stops any DEL from reaching text that reached the PTY another way (keys, paste, sticky modifiers, virtual Esc/Tab). A desync logs [Terminal IME #1883] and resyncs from textarea.value. Desktop is unchanged. Tests: unit (reconcileComposition with the issue's samples, mirror, bridge); jsdom component tests for Enter, Backspace, arrows, sticky Ctrl-C, paste, virtual keys, an iPad hardware keyboard and desktop; a Playwright spec driving Chromium's IME pipeline against the real xterm.js and Terminal, which fails on the pre-fix code with the samples' signatures. A new CI job runs it.
… on unretractable replaces Addresses the PR review: - Follow xterm's textarea clear on blur, so dismissing the keyboard no longer trips the desync alarm. - Split the mirror floor in two. A replace never reaches back past other input, and text that never reached the PTY is never deleted. A soft backspace after a virtual key now deletes, where before it sent nothing. - Accept the browser's paste insert as already on the PTY (last line only), so autocorrecting pasted text replaces it. - Log a replace that cannot be retracted. Log lengths only, never the command line. - Add soft-backspace and blur tests at the unit, component and browser levels.
|
Excellent work. The reproduction through Chromium's real IME path, the two-floor mirror, and the fail-loud resync are exactly the shape I hoped for, and every condition I set has a named test. Thank you. Integration review (3-way): gemini APPROVE, codex REQUEST_CHANGES, claude REQUEST_CHANGES. Both change requests land on the same spot, and I agree with them. I would like three changes before merge: 1. Rebase the mirror only for input that actually reached the PTY. In 2. Edits that change the textarea without reaching the PTY must not detach silently. 3. Type-check the Playwright harness. Smaller, your call: an expected closed-socket during reconnect should not share Please also file the #517 iPad key-repeat follow-up you identified. The real-phone pass stays the acceptance gate; the owner has been asked which device produced the samples. |
…PTY; sync every textarea edit Integration review changes: - onData: call noteForeignInput() after every drop point (closed socket, dedup, initial-phase filter, sticky Cmd-C/Cmd-V), and skip payloads that are only terminal replies (DA, CPR, DECRPM). The three regexes are now shared as stripTerminalReplies with the initial-phase filter. - Sync cut, drag, drop, yank and undo/redo like any other edit instead of detaching silently. Only paste stays special. - Type-check the Playwright harness: tsconfig include, check-types script, and a web-browser CI step. - Log a closed-socket edit with console.warn, keeping console.error for real desyncs. - Make onKeySent a useCallback, and note the noteForeignInput() invariant in arch.md. - Tests: reply skip (jsdom and real xterm via ESC[6n), cut/undo/drop. The phone afterEach now unmounts before asserting. The desktop control types spaces as real keys.
…ped virtual keys and bulk deletes Review changes (codex round 3): - Sticky modifiers: a soft-keyboard character Cmd swallows, or Ctrl turns into a control character, is marked as never sent (noteSwallowedEdit). Backspacing it no longer sends a DEL into real text. - VirtualKeyboard: sendRawKey reports whether it sent; onKeySent fires only then. - TextareaMirror: only a backspace-type delete may delete past other input. A cut, drag or undo that would fails loud. - Playwright: a real native paste (CDP paste editing command), confirming in Chromium that the mirror's paste bookkeeping runs before the browser's insert, with autocorrect of the pasted word.
|
All three changes verified at head: the mirror rebase now sits after the socket, dedup, reply-filter and modifier drop points and skips pure terminal replies; cut/drag/undo edits sync or fail loud; the Playwright harness is type-checked in CI. The three extra bugs codex found in round 3 are fixed with tests that fail on the prior head, and the paste-ordering claim was disproved with a real-paste test rather than argued. Follow-ups #1885, #1886 and #1887 cover the rest. Thank you for a thorough round. My integration review is satisfied. Codex's remaining request is the real-device pass, which is the owner's acceptance gate; merge waits on that word. |
Summary
On a phone, accepting a keyboard suggestion in the dashboard terminal sent the partial word and the preceding clause to the PTY again. On touch devices the Terminal now sends every soft-keyboard edit as a replace: DELs for the text being replaced, then the new text. The desktop path is unchanged.
Fixes #1883
Root Cause
The root cause is partly different from what the issue describes (correction posted on #1883). xterm.js keeps everything typed since the last Enter in its hidden textarea. A mobile keyboard's autocomplete, autocorrect and smart punctuation edit that text in place. xterm's textarea handlers assume the textarea only grows by appends:
_handleAnyTextareaChanges(keydown 229, outside a composition) sendsnewValue.replace(oldValue, ''). Once the keyboard has edited text it already committed,oldValueis no longer a substring, so the whole textarea is resent. This is the repeated clause in samples 1 and 3._finalizeCompositionsendsvalue.substring(start)withstartrecorded at the end of the value. When the keyboard recomposes a word that starts earlier, the result is a misaligned tail (ciecuplus a fragment ofcircumstances).The #253/#517 guard only drops exact repeats, so none of this was caught.
Fix
apps/web/src/lib/imeInput.ts:reconcileComposition(sent, committed)is a pure function that returns DELs for the diverging tail, then the new text. It counts code points, so an emoji takes one DEL.TextareaMirrortracks what the PTY holds of the textarea. Two floors bound the DELs. A replace never reaches back past other input (a key, a virtual key, a paste), because its DELs would hit that input. Text that never reached the PTY (the context-menu selection xterm puts in the textarea) is never deleted at all. Only the keyboard's backspace still deletes past other input, at the cursor as stock xterm does. A cut, drag or undo of hidden textarea text that would reach back past it fails loud instead.attachImeInputkeeps composition and input events away from xterm's textarea listeners (container capture phase) and sends each edit throughterm.input.Terminal.tsx:(pointer: coarse). It owns keydown 229 through the custom key handler.onDatapayload that is actually sent (keys, paste, sticky modifiers) rebases the mirror. Data dropped by the socket check, the dedup guard or the initial-phase filter does not. Neither do terminal replies (DA, CPR, DECRPM): xterm answers a TUI's queries throughonData, and those three are now skipped using the same regexes as the initial-phase filter, shared asstripTerminalReplies. Moving the full server reply table intocodev-typesis follow-up Share the terminal-reply table between the server gate and the web client (codev-types) #1885.VirtualKeyboard.tsx: Esc and Tab go straight to the WebSocket, so they report back (onKeySent) and rebase the mirror, but only when the socket actually took them.[Terminal IME #1883] …withconsole.errorand resyncs fromtextarea.valuein two cases: the textarea changed outside an input event, or an edit replaced text sent before other terminal input (that text cannot be retracted). An edit the closed socket could not take (a reconnect) logsconsole.warninstead, soconsole.errorstays meaningful. It never deletes text the PTY didn't get from the textarea, and never resends text already sent. The log carries lengths only, never the command line.preventDefault, so the browser also inserts the clipboard into the textarea. ThatinsertFromPasteis not resent, and it is taken as already on the PTY, so autocorrecting pasted text replaces it (teh→the, nottehthe). Only the last line of a multi-line paste is editable.Behaviour change for CJK input on touch devices: xterm's composition overlay never activates. Intermediate composing text (kana) streams to the PTY as it is typed, and is retracted with DELs when the IME converts it (kanji). This converges to the right bytes in a line editor, but it has not been tried with a real CJK IME on a device. The same applies to the alternate screen (vim normal mode,
less,fzf). There, streamed composing text and the DELs that retract it act as commands, not text. Stock xterm also mangled autocorrect there (whole-textarea resends), but whether to bypass the bridge on the alternate screen, and how to treat CJK composition, is follow-up #1887.Deviation from the issue's prescription (approved by the architect): sync is live per
inputevent rather than deferred tocompositionend, and it covers non-composition edits, because that is where the clause repeat actually comes from.Test Plan
Regression test, real browser:
apps/web/playwright/ime-replace.spec.ts(newpnpm test:browser, new CI jobweb-browser). The real xterm.js and the realTerminalrun in Chromium. Keyboard edits are driven through Chromium's own IME pipeline via CDP (Input.imeSetCompositionwith replacement ranges,Input.insertText), against a fake PTY socket that applies DEL like a line editor.…spoke what have you done since we last spoke.check afaincheck again.ciecumstances--repeat-each=3. They include a desktop control (frames byte-identical to stock) and two guards added after review: soft backspace (keydown 229 +deleteContentBackward), blur then refocus with no desync alarm, and (after the integration review) a real TUI query: the fake PTY sendsESC[6nmid-typing, real xterm answers throughonData, and a later autocorrect still replaces; and a real native paste (a CDPpasteediting command, with clipboard permission) followed by autocorrecting the pasted word. That test fails if the mirror's paste bookkeeping ran after the browser's insert, which settles a review question about event order in Chromium. Without the blur handling, the latter fails on real xterm withexpectedLength: 6, actualLength: 0.Component tests (jsdom):
Terminal.ime-replace.test.tsxcovers:Of the original 11, 9 fail on pre-fix
Terminal.tsx; the 2 that pass are the must-not-change controls (iPad, desktop).Unit tests:
imeInput.test.tscoversreconcileCompositionwith the issue's three samples decomposed, plus append, last-word replace, mid-sentence replace, whole-value re-emit, empty commit and emoji. It also covers the mirror (floors, paste then autocorrect, multi-line paste, divergence) and the bridge, including cut, undo/redo, drop, long-press backspace and swallowed edits (41 tests).Build and
tsc -bpass.apps/webnow type-checks its Playwright harness (tsconfig.jsoninclude,pnpm check-types, a step in theweb-browserCI job).packages/codev7229 tests pass.Manual on a real phone (owner): type a sentence on Android Chrome and on iOS Safari, accepting at least two suggestions, and confirm it arrives once with no fragments. I could only emulate.
Notes:
localStorageshadows jsdom's and breaks every Terminal component test, including ones that predate this PR. Run them withNODE_OPTIONS=--no-experimental-webstorage. CI uses Node 20.insertReplacementTextcase dispatches a hand-builtInputEvent. The owner's iOS Safari check is the real acceptance test for iOS.codev-types), Touch-device duplicate guard drops repeated hardware keys (iPad key repeat, fast double letters) #1886 (the touch-device duplicate guard drops repeated hardware keys, e.g. iPad key repeat), and Touch IME bridge: decide behaviour for alternate-screen apps and CJK composition #1887 (decide the bridge's behaviour for alternate-screen apps and CJK composition).