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
+28
View File
@@ -126,6 +126,16 @@ type EditorState struct {
IMECaretRune int
IMESelStartRune int
IMESelEndRune int
// IMEForceResync asks the next frame to re-push the snippet trimmed by
// one rune (a forced restartInput): after several consecutive
// anomalous commits (drift-snapped or empty) the IME's local text is
// desynchronized from ours and it keeps re-sending the same fix (an
// endless empty-commit loop). A forced restart makes it re-fetch the
// real text and selection around the caret, which heals the model.
// Set by HandleIMECommit, consumed by the next frame (EditorLayout).
IMEForceResync bool
// imeAnomStreak counts consecutive anomalous IME commits (see above).
imeAnomStreak int
// EditSeq counts content edits (incremented by markDirty). The shaped
// glyph layout arriving via layoutChan is only applied to the WrapIndex
// when its EditSeq matches, so a layout shaped before an edit can never
@@ -2352,13 +2362,29 @@ func HandleIMECommit(data any) {
if startRune > endRune {
startRune, endRune = endRune, startRune
}
snapped := false
if endRune-startRune <= 2 {
caretRune := TheState.imeRuneOffsetAt(TheState.Editor.CursorPosition)
if endRune != caretRune {
log.Printf("IME DRIFT-SNAP range=[%d,%d) caret=%d -> snap to caret", startRune, endRune, caretRune)
startRune, endRune = caretRune, caretRune
snapped = true
}
}
// Anomalous commits (snapped, or empty: an IME fix-up that changes
// nothing) mean the IME's local text has desynchronized from ours.
// After a few in a row, force a re-syncing snippet re-push on the next
// frame (see IMEForceResync) so the IME re-fetches the real text and
// the loop ends; a normal commit resets the streak.
if snapped || c.Text == "" {
TheState.Editor.imeAnomStreak++
if TheState.Editor.imeAnomStreak >= 3 {
TheState.Editor.imeAnomStreak = 0
TheState.Editor.IMEForceResync = true
}
} else {
TheState.Editor.imeAnomStreak = 0
}
absStart, absEnd := runeToByteWhole(startRune), runeToByteWhole(endRune)
log.Printf("IME COMMIT range=[%d,%d) text=%q -> [%d,%d)", startRune, endRune, c.Text, absStart, absEnd)
applyIMECommitBytes(absStart, absEnd, c.Text)
@@ -2837,6 +2863,8 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
editorElem.IMECaretRune = TheState.Editor.IMECaretRune
editorElem.IMESelStartRune = TheState.Editor.IMESelStartRune
editorElem.IMESelEndRune = TheState.Editor.IMESelEndRune
editorElem.IMEForceResync = TheState.Editor.IMEForceResync
TheState.Editor.IMEForceResync = false
editorElem.WindowStartByte = start
// While the find bar is open, key focus belongs to the main-owned
// "find_bar" widget.Editor: the editor stops its per-frame IME sync, and