The best way to control Claude Code, Codex and more from any computer or phone
fix(mobile): preserve keyboard suggestions while typing (#6323)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Julius Marminge committed
Aug 14, 2026 at 10:45 UTC
b3b4b57794476c2e130d38f9243f3c5da1a0cbe7
6 files changed
+242
-53
apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt
+7
@@ -252,6 +252,9 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
252
val textLength = editor.text?.length ?: 0
253
val safeStart = start.coerceIn(0, textLength)
254
val safeEnd = end.coerceIn(0, textLength)
255
+ // Re-applying an unchanged selection resets the keyboard's suggestion
256
+ // state, so a no-op assignment must be skipped.
257
+ if (editor.selectionStart == safeStart && editor.selectionEnd == safeEnd) return
258
editor.setSelection(safeStart, safeEnd)
259
}
260
@@ -281,6 +284,10 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
284
)
285
286
private fun emitSelectionChange(start: Int, end: Int) {
287
+ // Caret moves advance the revision counter like text edits do: a
288
+ // controlled payload computed before this move is stale and must fail the
289
+ // revision guard instead of yanking the caret back mid-typing.
290
+ nativeEventCount += 1
291
onComposerSelectionChange(
292
mapOf(
293
"value" to editor.text.toString(),
apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift
+17
-1
@@ -489,6 +489,12 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
489
return
490
}
491
restoreBaseTypingAttributes()
492
+ // UIKit moves the selection before textViewDidChange runs. Emitting here
493
+ // would pair the post-edit text with a pre-edit revision counter, so let
494
+ // the change event that follows carry both; only pure caret moves emit.
495
+ guard self.textView.serializedText() == value else {
496
+ return
497
+ }
498
emitSelection()
499
}
500
@@ -774,8 +780,12 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
780
}
781
782
private func emitSelection() {
783
+ // Caret moves advance the revision counter like text edits do: a
784
+ // controlled payload computed before this move is stale and must fail the
785
+ // revision guard instead of yanking the caret back mid-typing.
786
let currentValue = textView.serializedText()
787
let selection = sourceSelection()
788
+ nativeEventCount += 1
789
onComposerSelectionChange([
790
"value": currentValue,
791
"selection": ["start": selection.start, "end": selection.end],
@@ -817,10 +827,16 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
827
NSMaxRange(nextRange) <= textView.attributedText.length else {
828
return
829
}
830
+ self.requestedSelection = nil
831
+ // Programmatically assigning selectedRange resets the keyboard's
832
+ // autocorrect and predictive-text context even when the range is
833
+ // unchanged, so a no-op assignment must be skipped.
834
+ guard !NSEqualRanges(nextRange, textView.selectedRange) else {
835
+ return
836
+ }
837
isApplyingControlledValue = true
838
textView.selectedRange = nextRange
839
isApplyingControlledValue = false
823
- self.requestedSelection = nil
840
}
841
842
private func updatePlaceholderVisibility() {
apps/mobile/src/native/T3ComposerEditor.ios.tsx
+38
-22
@@ -19,6 +19,7 @@ import { useFontFamily } from "../lib/useFontFamily";
19
import { useScaledTextRole } from "../features/settings/appearance/useScaledTextRole";
20
import {
21
acknowledgeComposerNativeEvent,
22
+ assumeComposerControlledState,
23
isComposerNativeEcho,
24
pruneAcknowledgedComposerNativeEvents,
25
resolveComposerControlledEventCount,
@@ -102,11 +103,11 @@ export function ComposerEditor({
103
const nativeRef = useRef<NativeComposerEditorRef>(null);
104
const mostRecentEventCountRef = useRef(0);
105
const [mostRecentEventCount, setMostRecentEventCount] = useState(0);
105
- const [nativeEventSequence, setNativeEventSequence] = useState(0);
106
- const previousRenderedEventSequenceRef = useRef(0);
107
- const nativeEventSnapshotsRef = useRef<ComposerNativeEventSnapshot[]>([
108
- { eventCount: 0, value: props.value, selection: selection ?? null },
109
- ]);
106
+ const [, forceNativeEventRender] = useState(0);
107
+ // The native editor mounts empty, so the snapshot history starts empty: the
108
+ // first controlled payload must be a non-echo so a restored draft (or a
109
+ // recycled native view) is applied rather than skipped.
110
+ const nativeEventSnapshotsRef = useRef<ComposerNativeEventSnapshot[]>([]);
111
const confirmedTokensRef = useRef(collectComposerInlineTokens(props.value));
112
const bodyText = useScaledTextRole("body");
113
const textColor = useThemeColor("--color-foreground");
@@ -154,15 +155,16 @@ export function ComposerEditor({
155
})),
156
);
157
}, [props.value, skillLabels]);
157
- const includesNativeEvent = nativeEventSequence !== previousRenderedEventSequenceRef.current;
158
- const controlledEventCount = includesNativeEvent
159
- ? resolveComposerControlledEventCount(
160
- props.value,
161
- selection ?? null,
162
- mostRecentEventCount,
163
- nativeEventSnapshotsRef.current,
164
- )
165
- : mostRecentEventCount;
158
+ // Every render resolves against the snapshot history, so a render whose
159
+ // (value, selection) lags the acknowledged native state is stamped behind
160
+ // the native revision and rejected by the editor instead of re-applying a
161
+ // stale caret or stale text mid-typing.
162
+ const controlledEventCount = resolveComposerControlledEventCount(
163
+ props.value,
164
+ selection ?? null,
165
+ mostRecentEventCount,
166
+ nativeEventSnapshotsRef.current,
167
+ );
168
const acknowledgesLatestNativeEvent = isComposerNativeEcho(
169
props.value,
170
selection ?? null,
@@ -170,9 +172,7 @@ export function ComposerEditor({
172
nativeEventSnapshotsRef.current,
173
);
174
const isNativeEcho =
173
- includesNativeEvent &&
174
- controlledEventCount === mostRecentEventCount &&
175
- acknowledgesLatestNativeEvent;
175
+ controlledEventCount === mostRecentEventCount && acknowledgesLatestNativeEvent;
176
const controlledDocumentJson = JSON.stringify({
177
value: props.value,
178
selection: isNativeEcho ? null : (selection ?? null),
@@ -180,9 +180,6 @@ export function ComposerEditor({
180
mostRecentEventCount: controlledEventCount,
181
isNativeEcho,
182
});
183
- useEffect(() => {
184
- previousRenderedEventSequenceRef.current = nativeEventSequence;
185
- }, [nativeEventSequence]);
183
useEffect(() => {
184
if (!acknowledgesLatestNativeEvent) return;
185
nativeEventSnapshotsRef.current = pruneAcknowledgedComposerNativeEvents(
@@ -190,6 +187,18 @@ export function ComposerEditor({
187
mostRecentEventCount,
188
);
189
}, [acknowledgesLatestNativeEvent, mostRecentEventCount]);
190
+ const assumedValue = props.value;
191
+ useEffect(() => {
192
+ // A native event that arrived after this render was committed moves the
193
+ // acknowledged revision forward; the editor rejects this payload, so the
194
+ // snapshot history must not assume it applied.
195
+ if (isNativeEcho || controlledEventCount !== mostRecentEventCountRef.current) return;
196
+ nativeEventSnapshotsRef.current = assumeComposerControlledState(
197
+ nativeEventSnapshotsRef.current,
198
+ controlledEventCount,
199
+ assumedValue,
200
+ );
201
+ }, [assumedValue, controlledEventCount, isNativeEcho, controlledDocumentJson]);
202
const acceptNativeEvent = useCallback(
203
(eventCount: number, value: string, nextSelection: ComposerEditorSelection) => {
204
const acknowledgedEventCount = acknowledgeComposerNativeEvent(
@@ -257,7 +266,7 @@ export function ComposerEditor({
266
onChangeText(event.nativeEvent.value);
267
onSelectionChange?.(event.nativeEvent.selection);
268
setMostRecentEventCount(acknowledgedEventCount);
260
- setNativeEventSequence((sequence) => sequence + 1);
269
+ forceNativeEventRender((sequence) => sequence + 1);
270
}}
271
onComposerSelectionChange={(event) => {
272
const acknowledgedEventCount = acceptNativeEvent(
@@ -266,9 +275,16 @@ export function ComposerEditor({
275
event.nativeEvent.selection,
276
);
277
if (acknowledgedEventCount === false) return;
278
+ // A selection change that raced a text mutation can carry post-edit
279
+ // text. It must reach the parent alongside the acknowledged revision,
280
+ // or the next render stamps the stale draft at that revision and can
281
+ // re-apply it over the newer native text.
282
+ if (event.nativeEvent.value !== props.value) {
283
+ onChangeText(event.nativeEvent.value);
284
+ }
285
onSelectionChange?.(event.nativeEvent.selection);
286
setMostRecentEventCount(acknowledgedEventCount);
271
- setNativeEventSequence((sequence) => sequence + 1);
287
+ forceNativeEventRender((sequence) => sequence + 1);
288
}}
289
onComposerPasteImages={(event) => onPasteImages?.(event.nativeEvent.uris)}
290
onComposerFocus={onFocus}
apps/mobile/src/native/T3ComposerEditor.native.tsx
+39
-22
@@ -21,6 +21,7 @@ import { useFontFamily } from "../lib/useFontFamily";
21
import { useThemeColor } from "../lib/useThemeColor";
22
import {
23
acknowledgeComposerNativeEvent,
24
+ assumeComposerControlledState,
25
isComposerNativeEcho,
26
pruneAcknowledgedComposerNativeEvents,
27
resolveComposerControlledEventCount,
@@ -103,11 +104,11 @@ export function ComposerEditor({
104
const nativeRef = useRef<NativeComposerEditorRef>(null);
105
const mostRecentEventCountRef = useRef(0);
106
const [mostRecentEventCount, setMostRecentEventCount] = useState(0);
106
- const [nativeEventSequence, setNativeEventSequence] = useState(0);
107
- const previousRenderedEventSequenceRef = useRef(0);
108
- const nativeEventSnapshotsRef = useRef<ComposerNativeEventSnapshot[]>([
109
- { eventCount: 0, value: props.value, selection: selection ?? null },
110
- ]);
107
+ const [, forceNativeEventRender] = useState(0);
108
+ // The native editor mounts empty, so the snapshot history starts empty: the
109
+ // first controlled payload must be a non-echo so a restored draft (or a
110
+ // recycled native view) is applied rather than skipped.
111
+ const nativeEventSnapshotsRef = useRef<ComposerNativeEventSnapshot[]>([]);
112
const [initialConfirmedTokens] = useState(() => collectComposerInlineTokens(props.value));
113
const confirmedTokensRef = useRef(initialConfirmedTokens);
114
const textColor = useThemeColor("--color-foreground");
@@ -155,15 +156,16 @@ export function ComposerEditor({
156
})),
157
);
158
}, [props.value, skillLabels]);
158
- const includesNativeEvent = nativeEventSequence !== previousRenderedEventSequenceRef.current;
159
- const controlledEventCount = includesNativeEvent
160
- ? resolveComposerControlledEventCount(
161
- props.value,
162
- selection ?? null,
163
- mostRecentEventCount,
164
- nativeEventSnapshotsRef.current,
165
- )
166
- : mostRecentEventCount;
159
+ // Every render resolves against the snapshot history, so a render whose
160
+ // (value, selection) lags the acknowledged native state is stamped behind
161
+ // the native revision and rejected by the editor instead of re-applying a
162
+ // stale caret or stale text mid-typing.
163
+ const controlledEventCount = resolveComposerControlledEventCount(
164
+ props.value,
165
+ selection ?? null,
166
+ mostRecentEventCount,
167
+ nativeEventSnapshotsRef.current,
168
+ );
169
const acknowledgesLatestNativeEvent = isComposerNativeEcho(
170
props.value,
171
selection ?? null,
@@ -171,9 +173,7 @@ export function ComposerEditor({
173
nativeEventSnapshotsRef.current,
174
);
175
const isNativeEcho =
174
- includesNativeEvent &&
175
- controlledEventCount === mostRecentEventCount &&
176
- acknowledgesLatestNativeEvent;
176
+ controlledEventCount === mostRecentEventCount && acknowledgesLatestNativeEvent;
177
const controlledDocumentJson = JSON.stringify({
178
value: props.value,
179
selection: isNativeEcho ? null : (selection ?? null),
@@ -181,9 +181,6 @@ export function ComposerEditor({
181
mostRecentEventCount: controlledEventCount,
182
isNativeEcho,
183
});
184
- useEffect(() => {
185
- previousRenderedEventSequenceRef.current = nativeEventSequence;
186
- }, [nativeEventSequence]);
184
useEffect(() => {
185
if (!acknowledgesLatestNativeEvent) return;
186
nativeEventSnapshotsRef.current = pruneAcknowledgedComposerNativeEvents(
@@ -191,6 +188,18 @@ export function ComposerEditor({
188
mostRecentEventCount,
189
);
190
}, [acknowledgesLatestNativeEvent, mostRecentEventCount]);
191
+ const assumedValue = props.value;
192
+ useEffect(() => {
193
+ // A native event that arrived after this render was committed moves the
194
+ // acknowledged revision forward; the editor rejects this payload, so the
195
+ // snapshot history must not assume it applied.
196
+ if (isNativeEcho || controlledEventCount !== mostRecentEventCountRef.current) return;
197
+ nativeEventSnapshotsRef.current = assumeComposerControlledState(
198
+ nativeEventSnapshotsRef.current,
199
+ controlledEventCount,
200
+ assumedValue,
201
+ );
202
+ }, [assumedValue, controlledEventCount, isNativeEcho, controlledDocumentJson]);
203
const acceptNativeEvent = useCallback(
204
(eventCount: number, value: string, nextSelection: ComposerEditorSelection) => {
205
const acknowledgedEventCount = acknowledgeComposerNativeEvent(
@@ -263,7 +272,7 @@ export function ComposerEditor({
272
onChangeText(event.nativeEvent.value);
273
onSelectionChange?.(event.nativeEvent.selection);
274
setMostRecentEventCount(acknowledgedEventCount);
266
- setNativeEventSequence((sequence) => sequence + 1);
275
+ forceNativeEventRender((sequence) => sequence + 1);
276
}}
277
onComposerSelectionChange={(event) => {
278
const acknowledgedEventCount = acceptNativeEvent(
@@ -272,9 +281,17 @@ export function ComposerEditor({
281
event.nativeEvent.selection,
282
);
283
if (acknowledgedEventCount === false) return;
284
+ // Android emits the selection change mid-mutation, before the change
285
+ // event, so the payload can carry post-edit text. It must reach the
286
+ // parent alongside the acknowledged revision, or the next render
287
+ // stamps the stale draft at that revision and can re-apply it over
288
+ // the newer native text.
289
+ if (event.nativeEvent.value !== props.value) {
290
+ onChangeText(event.nativeEvent.value);
291
+ }
292
onSelectionChange?.(event.nativeEvent.selection);
293
setMostRecentEventCount(acknowledgedEventCount);
277
- setNativeEventSequence((sequence) => sequence + 1);
294
+ forceNativeEventRender((sequence) => sequence + 1);
295
}}
296
onComposerPasteImages={(event) => onPasteImages?.(event.nativeEvent.uris)}
297
onComposerFocus={onFocus}
apps/mobile/src/native/composerEditorRevision.test.ts
+87
-2
@@ -2,6 +2,7 @@ import { describe, expect, it } from "@effect/vitest";
2
3
import {
4
acknowledgeComposerNativeEvent,
5
+ assumeComposerControlledState,
6
isComposerNativeEcho,
7
pruneAcknowledgedComposerNativeEvents,
8
resolveComposerControlledEventCount,
@@ -42,6 +43,19 @@ describe("isComposerNativeEcho", () => {
43
it("matches value and revision when selection is uncontrolled", () => {
44
expect(isComposerNativeEcho("native", null, 3, snapshots)).toBe(true);
45
});
46
+
47
+ it("does not claim a controlled selection against an assumed state without one", () => {
48
+ // An echo payload serializes `selection: null`; classifying a controlled
49
+ // selection as an echo of an assumed state would drop a parent caret move.
50
+ const assumed = [{ eventCount: 3, value: "native", selection: null }];
51
+ expect(isComposerNativeEcho("native", { start: 0, end: 0 }, 3, assumed)).toBe(false);
52
+ expect(isComposerNativeEcho("other", { start: 0, end: 0 }, 3, assumed)).toBe(false);
53
+ });
54
+
55
+ it("matches an assumed state when selection is uncontrolled", () => {
56
+ const assumed = [{ eventCount: 3, value: "native", selection: null }];
57
+ expect(isComposerNativeEcho("native", null, 3, assumed)).toBe(true);
58
+ });
59
});
60
61
describe("resolveComposerControlledEventCount", () => {
@@ -95,7 +109,7 @@ describe("pruneAcknowledgedComposerNativeEvents", () => {
109
selection: { start: eventCount, end: eventCount },
110
}));
111
98
- expect(pruneAcknowledgedComposerNativeEvents(snapshots, 999)).toEqual([]);
112
+ expect(pruneAcknowledgedComposerNativeEvents(snapshots, 999)).toEqual([snapshots[999]]);
113
});
114
115
it("retains native events that arrive after the acknowledged render", () => {
@@ -104,6 +118,77 @@ describe("pruneAcknowledgedComposerNativeEvents", () => {
118
{ eventCount: 41, value: "ab", selection: { start: 2, end: 2 } },
119
];
120
107
- expect(pruneAcknowledgedComposerNativeEvents(snapshots, 40)).toEqual([snapshots[1]]);
121
+ expect(pruneAcknowledgedComposerNativeEvents(snapshots, 40)).toEqual(snapshots);
122
+ });
123
+
124
+ it("retains the newest acknowledged snapshot so settled re-renders stay echoes", () => {
125
+ const snapshots = [
126
+ { eventCount: 40, value: "a", selection: { start: 1, end: 1 } },
127
+ { eventCount: 41, value: "ab", selection: { start: 2, end: 2 } },
128
+ { eventCount: 42, value: "abc", selection: { start: 3, end: 3 } },
129
+ ];
130
+
131
+ const pruned = pruneAcknowledgedComposerNativeEvents(snapshots, 42);
132
+ expect(pruned).toEqual([snapshots[2]]);
133
+ expect(isComposerNativeEcho("abc", { start: 3, end: 3 }, 42, pruned)).toBe(true);
134
+ });
135
+
136
+ it("keeps the newest of several snapshots sharing the acknowledged revision", () => {
137
+ const snapshots = [
138
+ { eventCount: 41, value: "ab", selection: { start: 2, end: 2 } },
139
+ { eventCount: 41, value: "ab", selection: { start: 1, end: 1 } },
140
+ ];
141
+
142
+ expect(pruneAcknowledgedComposerNativeEvents(snapshots, 41)).toEqual([snapshots[1]]);
143
+ });
144
+});
145
+
146
+describe("assumeComposerControlledState", () => {
147
+ it("replaces the acknowledged history with the applied controlled state", () => {
148
+ const snapshots = [{ eventCount: 3, value: "typed", selection: { start: 5, end: 5 } }];
149
+
150
+ expect(assumeComposerControlledState(snapshots, 3, "")).toEqual([
151
+ { eventCount: 3, value: "", selection: null },
152
+ ]);
153
+ });
154
+
155
+ it("keeps native events that raced past the controlled revision", () => {
156
+ const snapshots = [
157
+ { eventCount: 3, value: "typed", selection: { start: 5, end: 5 } },
158
+ { eventCount: 4, value: "typed!", selection: { start: 6, end: 6 } },
159
+ ];
160
+
161
+ expect(assumeComposerControlledState(snapshots, 3, "")).toEqual([
162
+ { eventCount: 3, value: "", selection: null },
163
+ snapshots[1],
164
+ ]);
165
+ });
166
+
167
+ it("applies a parent caret move on the assumed value at the assumed revision", () => {
168
+ // Same value, new caret: not an echo (so the selection is serialized) but
169
+ // still stamped at the assumed revision so the editor accepts it.
170
+ const snapshots = assumeComposerControlledState([], 3, "typed");
171
+
172
+ expect(isComposerNativeEcho("typed", { start: 2, end: 2 }, 3, snapshots)).toBe(false);
173
+ expect(resolveComposerControlledEventCount("typed", { start: 2, end: 2 }, 3, snapshots)).toBe(
174
+ 3,
175
+ );
176
+ });
177
+
178
+ it("re-applies a parent value that round-trips back to an acknowledged state", () => {
179
+ // Native acknowledged "typed", the parent then controlled the editor to ""
180
+ // (a send clearing the draft) and back to "typed" (the send failed and the
181
+ // draft was restored). The restore must be a fresh non-echo edit stamped at
182
+ // the current revision, not an echo the editor would drop.
183
+ const snapshots = assumeComposerControlledState(
184
+ [{ eventCount: 3, value: "typed", selection: { start: 5, end: 5 } }],
185
+ 3,
186
+ "",
187
+ );
188
+
189
+ expect(isComposerNativeEcho("typed", { start: 5, end: 5 }, 3, snapshots)).toBe(false);
190
+ expect(resolveComposerControlledEventCount("typed", { start: 5, end: 5 }, 3, snapshots)).toBe(
191
+ 3,
192
+ );
193
});
194
});
apps/mobile/src/native/composerEditorRevision.ts
+54
-6
@@ -31,10 +31,7 @@ export function resolveComposerControlledEventCount(
31
if (snapshot?.value !== value) continue;
32
33
newestValueEventCount ??= snapshot.eventCount;
34
- if (
35
- selection === null ||
36
- (snapshot.selection?.start === selection.start && snapshot.selection.end === selection.end)
37
- ) {
34
+ if (selection === null || snapshotSelectionMatches(snapshot, selection)) {
35
return snapshot.eventCount;
36
}
37
}
@@ -49,6 +46,21 @@ export function resolveComposerControlledEventCount(
46
return mostRecentEventCount;
47
}
48
49
+// A snapshot without a selection describes a state the editor applied itself
50
+// (an assumed controlled document, where the native side may have bounded the
51
+// caret). Revision stamping treats it as matching any controlled selection so
52
+// a parent caret move on the assumed value stays at the assumed revision and
53
+// passes the editor's staleness guard. Echo detection must not reuse this
54
+// wildcard: an echo payload serializes `selection: null`, which would drop
55
+// that caret move instead of applying it.
56
+function snapshotSelectionMatches(
57
+ snapshot: ComposerNativeEventSnapshot,
58
+ selection: ComposerEditorSelection,
59
+): boolean {
60
+ if (snapshot.selection === null) return true;
61
+ return snapshot.selection.start === selection.start && snapshot.selection.end === selection.end;
62
+}
63
+
64
export function isComposerNativeEcho(
65
value: string,
66
selection: ComposerEditorSelection | null,
@@ -62,7 +74,9 @@ export function isComposerNativeEcho(
74
snapshot.eventCount === eventCount &&
75
snapshot.value === value &&
76
(selection === null ||
65
- (snapshot.selection?.start === selection.start && snapshot.selection.end === selection.end))
77
+ (snapshot.selection !== null &&
78
+ snapshot.selection.start === selection.start &&
79
+ snapshot.selection.end === selection.end))
80
) {
81
return true;
82
}
@@ -70,9 +84,43 @@ export function isComposerNativeEcho(
84
return false;
85
}
86
87
+/**
88
+ * Records that a parent-driven controlled document was handed to the native
89
+ * editor. From that point the acknowledged snapshot history describes a
90
+ * superseded native state, so it is replaced with the assumed applied state;
91
+ * a later parent update back to a previously acknowledged value must classify
92
+ * as a fresh edit, not as a native echo the editor would drop. Native events
93
+ * that raced past the controlled revision stay authoritative and are kept.
94
+ */
95
+export function assumeComposerControlledState(
96
+ snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
97
+ eventCount: number,
98
+ value: string,
99
+): ComposerNativeEventSnapshot[] {
100
+ return [
101
+ { eventCount, value, selection: null },
102
+ ...snapshots.filter((snapshot) => snapshot.eventCount > eventCount),
103
+ ];
104
+}
105
+
106
export function pruneAcknowledgedComposerNativeEvents(
107
snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
108
acknowledgedEventCount: number,
109
): ComposerNativeEventSnapshot[] {
77
- return snapshots.filter((snapshot) => snapshot.eventCount > acknowledgedEventCount);
110
+ // The newest acknowledged snapshot must survive pruning: it is what lets a
111
+ // later, unrelated re-render classify the settled composer state as a native
112
+ // echo instead of a parent-driven edit that would re-control the caret (and
113
+ // reset the keyboard's autocorrect context on iOS).
114
+ let latestAcknowledgedIndex = -1;
115
+ for (let index = snapshots.length - 1; index >= 0; index -= 1) {
116
+ const snapshot = snapshots[index];
117
+ if (snapshot !== undefined && snapshot.eventCount <= acknowledgedEventCount) {
118
+ latestAcknowledgedIndex = index;
119
+ break;
120
+ }
121
+ }
122
+ return snapshots.filter(
123
+ (snapshot, index) =>
124
+ index === latestAcknowledgedIndex || snapshot.eventCount > acknowledgedEventCount,
125
+ );
126
}