IME: force a re-syncing snippet re-push when the IME desynchronizes

After the selection-replacement autocorrect on the phone, Gboard's
local text was out of sync with ours and it re-sent the same empty
fix-up commit in an endless loop (~one per 150 ms, each drift-snapped
to the caret and applied as a no-op). The file was never damaged, but
the IME never converged because it kept 'fixing' text that did not
exist in its own model.

The app cannot see the IME's model; the only recovery the IME
contract offers is a restartInput, which makes it re-fetch the
real text and selection around the caret. Arm that recovery
automatically: three consecutive anomalous commits (drift-snapped,
or empty text) set IMEForceResync, and the next frame ships the
snippet trimmed by one rune, which changes the pushed text and
forces the restart. The streak resets on any normal commit, so
isolated anomalies never trigger it, and the resync is one-shot.

TestRealFile_IMEForceResync pins the arm/reset/consume cycle.
This commit is contained in:
gmp
2026-09-13 18:23:06 -04:00
parent dd9493bdbd
commit cb8ebc003f
4 changed files with 141 additions and 0 deletions
+4
View File
@@ -224,6 +224,10 @@ type TextField struct {
IMECaretRune int
IMESelStartRune int
IMESelEndRune int
// IMEForceResync: re-push the snippet trimmed by one rune this frame
// (a forced restartInput that heals a desynchronized IME; see
// EditorState.IMEForceResync).
IMEForceResync bool
// EditSeq is the buffer edit sequence at frame-build time (0 for
// non-editor fields); the renderer uses it to detect the frame that
// carries an IME commit's result (see Renderer.imeHold).
+18
View File
@@ -379,6 +379,24 @@ func (r *Renderer) FlushIME(gtx layout.Context, tf TextField) {
},
Text: tf.IMESnippetText,
}
if tf.IMEForceResync && utf8.RuneCountInString(tf.IMESnippetText) > 2 {
// The IME's local text has desynchronized (several consecutive
// drift-snapped or empty commits — see EditorState.IMEForceResync)
// and it keeps re-sending the same fix in a loop. Pushing the
// window trimmed by one rune forces a snippet change (and thus a
// restartInput): the IME re-fetches the real text and selection
// around the caret and its model converges. The next frame pushes
// the full window again (one more restart), then everything is
// quiet again.
rs := []rune(tf.IMESnippetText)
snippet = key.Snippet{
Range: key.Range{
Start: tf.IMESnippetStartRune + 1,
End: tf.IMESnippetEndRune,
},
Text: string(rs[1:]),
}
}
if snippet != r.lastSnippet {
r.lastSnippet = snippet
// A snippet change resets the IME's selection: force the selection