IME: accept the one stale-caret commit after a key-event edit; fix the IME hold

Gboard sends Enter as a key event, not a commit, so the IME's model
lags the buffer by exactly one edit and its next commit is anchored at
the pre-edit caret. The drift guard snapped such commits to the new
caret, duplicating the just-typed word (2026-09-19 phone incident:
'a<enter>a' became 'A' + 'AaAa').

- state.go: track one pending non-IME content edit (imeStaleEditByte,
  armed by noteNonIMEEdit on Enter/backspace/delete/cut/paste, consumed
  by the next IME commit or key edit, cleared by a tap). The guard
  accepts a small (<= 2 rune) TEXT commit ending at or before the edit
  position as-is (IME STALE-OK): everything before the edit is
  byte-identical in both models. Empty-text commits and commits ending
  past the edit still snap to the caret.
- render.go + state.go + main.go: arm the pre-commit flush hold with the
  drained frame's own EditSeq. The hold was armed with the last drawn
  frame's seq while the frame's TextField.EditSeq was never set (always
  0), so every frame matched and the hold released only via its
  8-frame timeout - silently swallowing the resync pushes (and all IME
  sync) that heal the desync. EditorLayout now ships EditSeq with the
  frame's TextField; FlushIME releases on the next higher seq.
- e2e (ime_key_edit_test.go): reproduce the incident (same-batch and
  next-batch variants) and pin the guard edges (deletions and
  past-the-edit commits still snap).
- ime.md: document the key-event desync invariant and the hold.
This commit is contained in:
gmp
2026-09-19 21:52:35 -04:00
parent 9d5c02db03
commit a9183a0f73
5 changed files with 328 additions and 21 deletions
+46 -1
View File
@@ -49,6 +49,24 @@ is only correct if the two still agree.
the caret and arms the resync. A 5-rune autocorrect 19,000 runes from
the caret is never legitimate; applying it verbatim clobbers distant
text (commit `a83cc1a`).
- **Key-event edits desync the IME exactly like a dropped commit — and the
IME's next commit is anchored at the pre-edit caret.** Gboard sends
Enter as a KEY EVENT, not a commit (the logic's `handleKey(NameReturn)`
inserts the `\n`); the IME's model never sees that edit unless a
re-syncing snippet push reaches it first, and the push can lose the race
— the next commit can arrive in the very same input batch as the key
event, so no frame has drawn. While one such edit is pending
(`EditorState.imeStaleEditByte`), the guard accepts a small (≤ 2 rune)
TEXT commit that ends at or before the edit position (`IME STALE-OK`):
everything before the edit is byte-identical in both models, so applying
it verbatim is exact (the usual shape is the IME appending to, or
correcting, the word it last committed). The pending edit is consumed and
a resync is armed. Never relaxed for: empty-text commits (a stale range
deletion is how documents get clobbered), commits ending past the edit
(the range crosses the shift), or anything without a pending edit
(commit `a83cc1a`'s guard stays intact otherwise). The pending edit is
set by `noteNonIMEEdit` (Enter, hardware backspace/delete, cut, paste),
consumed by the next IME commit or key edit, cleared by a tap.
- **A desynchronized IME is healed only by a restart.** See the structural
fact below: selection pushes after a commit are deduplicated away, so
the *only* mechanism that makes Gboard re-read the truth is a snippet
@@ -57,6 +75,26 @@ is only correct if the two still agree.
rune (one restart), the following frame ships the full window (one more),
then quiet. One-shot, so normal typing never pays for it
(commits `cb8ebc0`, `a83cc1a`).
- **Pre-commit frames must not flush the IME — and the hold is armed with
the drained frame's own seq.** `ApplyIMECommitToModel` updates the pushed
model on the drain pass and arms `imeHold` with the **EditSeq of the
frame being drained** (it is the pre-commit frame — built before the
logic processed the commit — and its snippet would clobber the just-
updated model). `FlushIME` skips frames whose EditSeq is at most the
armed one and resumes on the next higher; the logic's `HandleIMECommit`
always markDirties, so the post-commit frame exists and always advances
the seq. The hold is bounded (8 frames) so a stalled logic can never
stick the renderer. Two details that are load-bearing: the frame's
`ui.TextField.EditSeq` is copied from the state in `EditorLayout` —
without it every frame reads 0, `EditSeq <= holdSeq` matches *every*
frame, the hold releases only via the 8-frame timeout, and resync pushes
(and all IME sync) are silently swallowed (this is why the resync armed
by the 2026-09-19 drift-snap never reached Gboard, letting it keep
committing against a stale model); and the seq must be the drained
frame's, not "the last drawn frame's" — the drained frame is pre-commit
even when its seq already differs from earlier ones (a key event landed
in between), and arming with the older value releases the hold on the
very frame whose snippet is stale.
## What stock gioui does structurally (worked around, not patched)
@@ -90,6 +128,13 @@ behavioral change.
## Gboard behaviors observed
- Gboard sends Enter as a **key event** (InputConnection.sendKeyEvent),
not a text commit: the app inserts the `\n` via `handleKey(NameReturn)`
and the IME's model never learns about it until a re-syncing snippet
push (see the key-event-edit invariant above). The emulator's Gboard
does the same; its word-appending is milder than the phone's AICore
Gboard, so the desync shows as a caret-off-by-one there and as
duplicated text on the phone.
- Gboard keeps a word selection on its own (a tap near a word selects it)
and commits autocorrect as a **replacement over its local selection** —
the commit range is then as wide as the word, not a point insertion.
@@ -113,7 +158,7 @@ Permanent, low-volume lines (see "Diagnostics"):
|---|---|
| `IME TAP … cursor=N winStart=M` | A tap moved the logic cursor (bytes). Compare with the next `IME COMMIT` ranges (runes) — they should be in the same neighborhood (rune ≈ byte − multibyte count, **compute it, don't assume a constant**). |
| `IME COMMIT range=[a,b) text=… -> [x,y)` | The logic applied the commit at bytes [x,y). The pre-`->` range is Gboard's coordinate space; the post-`->` range is where it actually landed. |
| `IME DRIFT-SNAP … snap to caret + resync` | Anomaly detected: a commit far from the caret/selection (or a stale small commit) was applied at the caret instead. One = a tap/scroll desync just happened; a run = Gboard was looping. |
| `IME DRIFT-SNAP … snap to caret + resync` | Anomaly detected: a commit far from the caret/selection (or a stale small commit) was applied at the caret instead. One = a tap/scroll desync just happened; a run = Gboard was looping. | | `IME STALE-OK … staleEdit=N -> apply as-is + resync` | A key-event edit (usually Enter) is pending and the IME's commit was anchored at the pre-edit caret; it was applied at its reported range (byte-identical there) and a resync armed. Expected once per key-edit → keystroke sequence; a run without intervening commits = something is replaying. |
| `IME PUSH snippet=[a,b) … (restart)` | We re-anchored the IME window → Gboard restarts its input session. Should be rare: app open, file switch, caret crossing a window margin. Frequent restarts = the hysteresis gate is being defeated (check render-window coupling). |
| `IME PUSH sel=[s,e)` | We pushed a caret/selection to Gboard. If a tap does not produce one (or it repeats forever), the selection pipeline is broken upstream (focus gate, dedup, float-noise re-emit). |
| `PADIME EditorStateChanged sel A -> B` / `PADIME updateSelection sel=[…]` / `PADIME restartInput` | The gio→Gboard boundary: what actually crossed into Android. If `IME PUSH` shows a value the `PADIME` lines never show, the drop is inside gioui (focus gate / dedup); if `PADIME` shows it and Gboard still misbehaves, the IME ignored it. |