Skip to content

Mobile terminal: autocomplete replacement re-inserts the partial word and the preceding clause (IME replace events reach the PTY as keystrokes) #1883

Closed

Description

@waleedkadous

Problem

Typing to an architect from a phone through the dashboard terminal produces duplicated fragments. It never happens on desktop, is intermittent, and is tied to the mobile keyboard's autocomplete: accepting a suggestion adds characters. Reported 2026-11-08 by the owner.

Verbatim samples received that day:

  1. check again. i fwel really fatigued but at the same time cant rest check afain. gain. i fwel really fatigued but at the same time cant rest. .
  2. makes no sense. under normal ciecumakes nonsense. sense. under normal circumstances rcumstances thr thing i love most is thr fredom to chose what i do
  3. what have you done since we last spoke what have you done since we last spoke. .

Pattern: at the point where autocomplete replaced a word, the composer ends up with the partial word, the completed word, and a repeat of the preceding clause.

Root cause (as far as read)

apps/web/src/components/Terminal.tsx, the term.onData handler, carries the mobile dedup from #253 and #517. That guard only suppresses an exact duplicate onData payload within 150 ms while composing or on a coarse-pointer device.

An autocomplete acceptance is not an exact duplicate. The mobile IME performs a replace (delete the surrounding partial word, commit the full word), and xterm.js, which has no way to retract characters already sent to the PTY, surfaces the committed text as new onData. On some keyboards the textarea's whole current value is re-emitted on compositionend, which is the repeated clause. None of that matches data === lastSentData, so every byte reaches the PTY as fresh keystrokes.

#253 and #517 fixed the simple double-fire case. This is the replace case, which the same dedup cannot catch by design.

Fix (prescribed)

Handle composition as a replace, not as a stream of appends, in the same handler:

  • Listen to beforeinput on term.textarea and, during composition on a touch device, track inputType values insertCompositionText, insertReplacementText, deleteContentBackward and insertFromComposition with their data/getTargetRanges().
  • At compositionend, compute the diff between what was already sent for this composition and the committed text. Send the minimal edit: N backspaces (\x7f) for the characters being replaced, then the new characters. Suppress the intermediate onData payloads xterm emits during the composition rather than forwarding them.
  • Guard against the whole-value re-emit: when a compositionend payload has the already-sent text as a prefix, send only the suffix.
  • Keep the existing exact-duplicate guard; it still covers the Mobile: duplicate letters and extra periods when typing #517 case.

Prefer a small pure function reconcileComposition(sent: string, committed: string): string that returns the byte sequence to send, unit-tested in isolation, with the DOM wiring thin around it.

Testing

  • Unit tests for reconcileComposition with the three samples above decomposed into sent/committed pairs, plus: plain append, replace of the last word, replace mid-sentence, whole-value re-emit, empty commit.
  • A Playwright test that drives the textarea with synthetic compositionstart, beforeinput (insertReplacementText) and compositionend events on a coarse-pointer emulated device and asserts the bytes written to the PTY mock.
  • Manual: on a real phone (iOS Safari and Android Chrome), type a sentence accepting at least two autocomplete suggestions, and confirm the architect receives it exactly once with no fragments. The dashboard is a UI surface, so the browser check is required before the PR is called done.

Protocol: BUGFIX.

Activity

  1. added
    area/terminal
    on Nov 8, 2026
  2. waleedkadous commented on Nov 8, 2026

    @waleedkadous
    ContributorAuthor

    Addendum from the owner (2026-11-08): it also happens without pressing an autocomplete suggestion. Treat the suggestion tap as one trigger, not the cause. The broader suspect is the mobile IME's composition behaviour in general, including predictive text correcting a word in place after a space or punctuation. The prescribed fix above already targets composition replace events rather than the suggestion UI, so it still applies; the test matrix should include the no-tap case: type a misspelled word, then a space, and let the keyboard auto-correct it in place.

  3. waleedkadous commented on Nov 9, 2026

    @waleedkadous
    ContributorAuthor

    On it! Working on a fix now.

  4. waleedkadous commented on Nov 9, 2026

    @waleedkadous
    ContributorAuthor

    Root cause correction (from the bugfix-1883 investigation)

    The root cause in the issue body is partly wrong. Reproduced against the real xterm.js 5.5.0 bundle in Chromium, with the keyboard edits driven through CDP's real IME pipeline (Input.imeSetComposition with replacement ranges, Input.insertText, each preceded by keydown 229 the way Chrome on Android sends them).

    xterm.js keeps everything typed since the last Enter in its hidden textarea. The mobile keyboard edits that text in place, because it is the keyboard's autocomplete context. All three of xterm's textarea-derived emitters assume the textarea only ever grows by appends:

    1. CompositionHelper._handleAnyTextareaChanges runs on keydown 229 outside a composition and sends newValue.replace(oldValue, ''). Once the IME has edited text it already committed, oldValue is no longer a substring of newValue, so the whole textarea is resent. An equal-length change also resends the whole value. This produces the repeated clause in samples 1 and 3. It is not the compositionend path the issue guessed.
    2. CompositionHelper._finalizeComposition sends value.substring(start), where start was recorded at the end of the value on compositionstart. When the IME recomposes a word that begins earlier, the result is a misaligned tail, e.g. ciecu followed by a fragment of circumstances.
    3. Terminal._inputEvent forwards insertText data as an append.
    Keyboard operation Textarea afterwards Stock xterm sends to the PTY
    smart punctuation: spoke → spoke. since we last spoke. since we last spoke since we last spoke.
    IME recomposes committed ciecu → circumstances under normal circumstances under normal ciecumstances
    suggestion tapped while the partial word is still composing under normal circumstances correct

    Consequence: a fix scoped to compositionend would leave the clause repeats in place. The fix (approved by the architect) treats every IME-driven textarea edit on a touch device as a replace. It sends the minimal edit (DELs for the diverging tail, then the new text), computed against a mirror of what the PTY already has, and keeps xterm's append-only IME paths out of it. The desktop path is unchanged.

    Upstream context: xterm.js PR #5704 — handle insertReplacementText for Korean IME on WKWebView/Safari (open, changes requested) hits the same failure family on WKWebView. It buffers insertReplacementText that _inputEvent used to drop, and suppresses _handleAnyTextareaChanges on keyCode 229 during that replacement composition. It patches xterm internals and is not merged, so it does not fix this case for us.

  5. added 6 commits that reference this issue on Nov 9, 2026
    3fbe679
    d26a58f
    ae8f93c
    200fc88
    6a7d588
    e97f500
  6. added a commit that references this issue on Nov 9, 2026
    9c51d4a
  7. added a commit that references this issue on Nov 9, 2026
  8. waleedkadous commented on Nov 10, 2026

    @waleedkadous
    ContributorAuthor

    Real-device confirmation from the owner, 2026-11-10: typing to an architect from a phone through the cloud tunnel, on the build from main at 4291dab (PR #1884 plus PR #1888 installed). Message received intact, with ordinary typos but no duplicated fragments or repeated clauses. Device, browser and keyboard not yet recorded; iOS Safari versus Android Chrome coverage remains as noted in #1887.

  9. waleedkadous commented on Nov 10, 2026

    @waleedkadous
    ContributorAuthor

    Device for the confirmation above: Android Chrome. That path is now proven on a real device. iOS Safari remains emulated only (hand-built insertReplacementText events), tracked in #1887.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/terminal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions