IME: guard distant commits; arm the resync on the first anomaly

The boundary trace (PADIME at EditorStateChanged) showed why a
desynchronized IME never heals itself: gioui's EditorReplace advances
its stored selection state when the commit arrives, so the
post-commit selection push is always deduplicated away and
updateSelection is never sent after a commit. A desynced IME is
therefore healed only by a snippet restart — and the resync was
armed only after THREE consecutive anomalies, while the phone
incident's first anomaly (a 5-rune autocorrect 19,000 runes from
the caret, width > 2) sailed through the old <=2-rune guard and
clobbered distant text before any resync could fire.

Now:
- the guard also snaps commits that are far from the caret AND
  outside the live selection (a legitimate commit is always local:
  at the caret, inside the selection, or a nearby correction);
  a distant commit lands at the caret instead of clobbering text;
- the resync is armed on the FIRST anomalous commit (snapped or
  distant), not after a streak; the streak counter is gone;
- imeSelectionRuneRange() lets the guard allow selection
  replacements anywhere.

TestRealFile_IMEForceResync rewritten for the new semantics and
extended with the distant-replacement case (must land at the
caret and arm the resync).
This commit is contained in:
gmp
2026-09-13 22:31:28 -04:00
parent 51344b9a5b
commit a83cc1a72f
4 changed files with 98 additions and 66 deletions
+40 -36
View File
@@ -211,12 +211,12 @@ func firstDiff(a, b string) int {
return n
}
// TestRealFile_IMEForceResync pins the desync self-heal: after several
// consecutive anomalous IME commits (drift-snapped or empty — the pattern
// Gboard produces when its local text has desynchronized from the app and it
// keeps re-sending the same fix), the next frame arms a forced snippet
// re-push (a restartInput that makes the IME re-fetch the real text). A
// normal commit resets the streak, so isolated anomalies never resync.
// TestRealFile_IMEForceResync pins the desync recovery: the first
// anomalous IME commit (a drift-snapped small commit, a distant commit —
// the phone autocorrect that landed 19,000 runes from the caret — or an
// empty fix-up) arms a forced re-syncing snippet re-push on the very next
// frame (a desynced IME is healed only by a restart; a selection push is
// deduplicated away after commits). A normal commit never arms it.
func TestRealFile_IMEForceResync(t *testing.T) {
model := largeFileContent(4000)
h, _ := realFileHarness(t, "resync.txt", model)
@@ -227,7 +227,6 @@ func TestRealFile_IMEForceResync(t *testing.T) {
t.Fatalf("wait for frame: %v", err)
}
// A caret deep in the file, in absolute file runes.
const caretByte = 10000
caretRune := utf8.RuneCountInString(model[:caretByte])
if err := h.WithState(func(st *editor.State) {
@@ -241,17 +240,13 @@ func TestRealFile_IMEForceResync(t *testing.T) {
Handler: editor.HandleIMECommit,
Data: editor.IMECommit{StartRune: start, EndRune: end, Text: text},
}})
// The logic goroutine applies commits asynchronously; wait for the
// resulting frame before asserting on the derived state.
// The logic applies commits asynchronously; wait for the resulting
// frame before asserting on the derived state.
prev := h.FrameCount()
if _, err := h.WaitForFrameCount(prev+1, 5*time.Second); err != nil {
t.Fatalf("wait for commit frame: %v", err)
}
}
// forceInLatestFrame reports whether the latest delivered frame's editor
// element carries the forced-resync flag (the state flag is consumed by
// the very frame that follows the arming commit, so the frame is the
// observable artifact).
forceInLatestFrame := func() bool {
frames := h.GetFrames()
if len(frames) == 0 {
@@ -266,38 +261,47 @@ func TestRealFile_IMEForceResync(t *testing.T) {
return false
}
// Two anomalous commits (an empty fix-up and a drifted insertion):
// below the threshold, no resync.
// A normal commit at the caret arms nothing.
commit(caretRune, caretRune, "q")
if forceInLatestFrame() {
t.Fatal("resync armed by a normal commit")
}
// A small drifted commit (Gboard fix-up at a stale position): the frame
// emitted by that commit carries the resync flag.
commit(caretRune+5, caretRune+5, "")
commit(caretRune+6, caretRune+6, "z")
if forceInLatestFrame() {
t.Fatalf("resync armed after only 2 anomalous commits")
}
// A normal commit resets the streak: two more anomalies must not arm.
commit(caretRune+10, caretRune+10, "q")
commit(caretRune+11, caretRune+11, "")
commit(caretRune+12, caretRune+12, "")
if forceInLatestFrame() {
t.Fatalf("resync armed after a reset streak")
}
// One more anomaly makes three in a row: the frame emitted by that
// commit carries the flag on its editor element (the state flag is
// consumed by that same frame).
commit(caretRune+13, caretRune+13, "")
if !forceInLatestFrame() {
t.Fatalf("resync not armed after 3 consecutive anomalous commits")
t.Fatal("resync not armed after a drift-snapped commit")
}
// A later frame (no new anomalies) carries no flag: the resync fires
// exactly once.
// One-shot: a later frame carries no flag.
prev := h.FrameCount()
h.SendConfig(780, 400)
if _, err := h.WaitForFrameCount(prev+1, 5*time.Second); err != nil {
t.Fatalf("wait for frame: %v", err)
}
if forceInLatestFrame() {
t.Fatalf("resync flag not one-shot")
t.Fatal("resync flag not one-shot")
}
// A distant replacement commit (width > 2: the autocorrect class that
// the old guard let through) is snapped to the caret and arms the
// resync.
commit(caretRune+20000, caretRune+20005, "silly")
if !forceInLatestFrame() {
t.Fatal("resync not armed after a distant commit")
}
// The distant commit must have landed at the caret, not at 20000:
// the caret advances by exactly the inserted text length.
var cur int
if err := h.WithState(func(st *editor.State) {
cur = st.Editor.CursorPosition
}); err != nil {
t.Fatalf("WithState: %v", err)
}
// "q" (1) + "silly" (5) inserted at the original caret.
if want := caretByte + 6; cur != want {
t.Fatalf("cursor = %d, want %d (distant commit must land at the caret)", cur, want)
}
}