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:
@@ -210,3 +210,94 @@ 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.
|
||||
func TestRealFile_IMEForceResync(t *testing.T) {
|
||||
model := largeFileContent(4000)
|
||||
h, _ := realFileHarness(t, "resync.txt", model)
|
||||
defer h.Cleanup()
|
||||
|
||||
h.SendConfig(780, 400)
|
||||
if _, err := h.WaitForFrame(5 * time.Second); err != nil {
|
||||
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) {
|
||||
st.Editor.CursorPosition = caretByte
|
||||
}); err != nil {
|
||||
t.Fatalf("WithState: %v", err)
|
||||
}
|
||||
|
||||
commit := func(start, end int, text string) {
|
||||
h.SendInput([]ui.InputEvent{{
|
||||
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.
|
||||
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 {
|
||||
t.Fatal("no frames")
|
||||
}
|
||||
for _, el := range frames[len(frames)-1] {
|
||||
if tf, ok := el.(ui.TextField); ok && tf.ID() == "editor_text" {
|
||||
return tf.IMEForceResync
|
||||
}
|
||||
}
|
||||
t.Fatal("no editor_text element in latest frame")
|
||||
return false
|
||||
}
|
||||
|
||||
// Two anomalous commits (an empty fix-up and a drifted insertion):
|
||||
// below the threshold, no resync.
|
||||
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")
|
||||
}
|
||||
|
||||
// A later frame (no new anomalies) carries no flag: the resync fires
|
||||
// exactly once.
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user