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
+21 -17
View File
@@ -147,17 +147,18 @@ type Renderer struct {
// imeHold: a commit was applied to the pushed model (see
// ApplyIMECommitToModel) but the logic's post-commit frame has not been
// drawn yet. The frame(s) drawn in the gap are pre-commit; their
// snippets would clobber the model with the stale text, regressing
// gioui's editor state and causing a restart per keystroke. While the
// hold is active, FlushIME skips a frame whose EditSeq equals the
// pre-commit sequence and resumes on the next one.
// drawn yet. The frame being drained is pre-commit (it was built before
// the logic processed the commit); its snippet would clobber the model
// with the stale text, regressing gioui's editor state and causing a
// restart per keystroke. The hold is armed with the drained frame's
// EditSeq: while the hold is active, FlushIME skips a frame whose
// EditSeq is at most the armed one (the drained frame, and any no-op
// frame repeating the seq) and resumes on the next higher one. The
// logic's HandleIMECommit markDirties the buffer, so the post-commit
// frame always advances it.
imeHoldActive bool
imeHoldEditSeq uint64
imeHoldFrames int
// lastEditSeq: the EditSeq of the last editor frame drawn (the
// pre-commit sequence when an IME commit is in flight).
lastEditSeq int64
// FocusCmd dedup (main-owned, persistent across frames). key.FocusCmd is
// issued ONLY on a focus transition, never per frame: even a no-op FocusCmd
@@ -296,7 +297,9 @@ func (r *Renderer) pointInHandleBox(p image.Point) bool {
// pushed) and does nothing. If the logic SNAPS the commit to a different
// position (drift guard), the post-commit frame's snippet differs from the
// model and one resyncing restart is pushed then — correct.
func (r *Renderer) ApplyIMECommitToModel(gtx layout.Context, ev key.EditEvent) {
// frameEditSeq is the EditSeq of the frame the commit was drained from
// (the pre-commit frame); it arms the hold (see the imeHold field).
func (r *Renderer) ApplyIMECommitToModel(gtx layout.Context, ev key.EditEvent, frameEditSeq uint64) {
sn := r.lastSnippet
local := ev.Range.Start - sn.Range.Start
total := utf8.RuneCountInString(sn.Text)
@@ -327,9 +330,12 @@ func (r *Renderer) ApplyIMECommitToModel(gtx layout.Context, ev key.EditEvent) {
// moved it there). The px caret position is the current one (the next
// frame corrects it); gioui's updateCaret is cosmetic for the IME.
// Hold the pre-commit frame(s) out of FlushIME until the post-commit
// frame arrives (they would clobber the model just pushed).
// frame arrives (they would clobber the model just pushed). Arm with
// the drained frame's own EditSeq: it is the seq of the last
// pre-commit frame, and the post-commit frame's seq is strictly higher
// (the logic's HandleIMECommit markDirties the buffer).
r.imeHoldActive = true
r.imeHoldEditSeq = uint64(r.lastEditSeq)
r.imeHoldEditSeq = frameEditSeq
r.imeHoldFrames = 0
// The post-commit caret is at the END of the inserted text, which
// starts at Range.Start: for an insertion (Start == End) that is
@@ -357,9 +363,10 @@ func (r *Renderer) FlushIME(gtx layout.Context, tf TextField) {
// An IME commit is in flight (see ApplyIMECommitToModel): a pre-commit
// frame must not push its stale snippet and regress gioui's state.
if r.imeHoldActive {
if tf.EditSeq == r.imeHoldEditSeq {
// Bounded so a no-op commit (no EditSeq advance) cannot stick
// the hold: after a few frames resume normal flushing.
if tf.EditSeq <= r.imeHoldEditSeq {
// Bounded so a commit whose post-commit frame never arrives
// (logic stall) cannot stick the hold: after a few frames
// resume normal flushing.
if r.imeHoldFrames++; r.imeHoldFrames > 8 {
r.imeHoldActive = false
} else {
@@ -368,9 +375,6 @@ func (r *Renderer) FlushIME(gtx layout.Context, tf TextField) {
}
r.imeHoldActive = false
}
if tf.EditSeq > 0 {
r.lastEditSeq = int64(tf.EditSeq)
}
// The snippet is the hysteresis window around the caret (see
// State.computeIMESnippetWindow), shipped precomputed by the logic:
// stable across render-window moves (keyboard show/hide, tap