Skip to content

Commit cb24ef6

Browse files
committed
fix(ios): stop fill from failing and repairing fields that normalize their input (#2634)
The bug was textEntryValueEchoes' containment clauses, not the repair gate. A field that reformats what it holds (a '## ### ## ##' digit grouping, a phone mask, a currency format) shows the request with the formatter's separators inserted; clauses 2/3 classify that as an echo, which withheld the unconfirmed outcome, so the fill fell to mismatch -> destructive clear-and-retype of a correct field -> TEXT_ENTRY_MISMATCH. Measured on iPhone 18 Pro / iOS 27.0 against a digit-grouping fixture: REPAIR_TEXT_ENTRY expectedLength=9 observedLength=12, then the mismatch. A value that COMPLETES the request now falls through to the existing unconfirmed-evidence path: every request character in order, plus inserted characters strictly between the request's own characters that appear nowhere in the request. Degradation removes; entry cannot insert a separator between two characters the same burst typed, and a failed clear leaves residual text at the ENDS of the typed run — so end-only insertions ('old123456') stay echoes and keep their repair, while an interior foreign insertion does not. Leftmost embedding and a no-request-character-extras guard keep doubled entries and ambiguous embeddings on the failure side. This deliberately flips the pin that '(555) 123-4567' IS an echo: 'echo' means 'could be a degraded copy', and a complete mask value is not one. Completion is never a correctness claim — a cents-shifting mask passes it — so the outcome is unconfirmed with before/after evidence, never verified: true; only exact equality verifies. fill-evidence.ts already names app-owned formatting as this shape's purpose; Android already reports it unconfirmed; iOS was the outlier. isRepairableTextEntryMismatch and the synthesized-replacement commit wait are UNCHANGED; the coordinate route's exact-match-only settle is pinned as an explicit non-goal. The write-back-truncation shape (ada@example -> adxe) stays a typed failure under #2903's ownership, and so do drops, stale residuals, and doubled entries. Fixture: AgentDeviceDigitGroupingTextField reproduces the reported formatter and restores the caret across reformats the way the real formatter it models does; both normalizing device tests share one fill-and-assert helper so their plumbing cannot drift. The device test proves the repair no longer runs (message 'typed', not 'typed after repair') and the field keeps 00 062 91 77.
1 parent 24b2ce6 commit cb24ef6

6 files changed

Lines changed: 367 additions & 17 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunner/AgentDeviceRunnerApp.m‎

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,85 @@ - (NSString *)accessibilityValue {
9797
return [NSString stringWithFormat:@"%lu of 6 digits", (unsigned long)self.text.length];
9898
}
9999
@end
100+
101+
// A field that reformats its own text on every edit into `## ### ## ##` digit groups, the way a
102+
// digit-grouping formatter in a Flutter or React Native input does. The accessibility value is
103+
// what the user sees — the formatted string — so it never equals the digits a `fill` sent (#2634).
104+
// The field owns the delegate it installs, keeping the formatter out of the view controller's
105+
// own delegate callbacks.
106+
@interface AgentDeviceDigitGroupingTextField : UITextField <UITextFieldDelegate>
107+
@end
108+
109+
static NSString *AgentDeviceFormatGroupedDigits(NSString *raw) {
110+
NSMutableString *digits = [NSMutableString string];
111+
NSCharacterSet *nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
112+
for (NSString *chunk in [raw componentsSeparatedByCharactersInSet:nonDigits]) {
113+
[digits appendString:chunk];
114+
}
115+
NSMutableArray<NSNumber *> *groupWidths = [NSMutableArray arrayWithArray:@[@2, @3, @2, @2]];
116+
NSMutableString *formatted = [NSMutableString string];
117+
NSUInteger cursor = 0;
118+
for (NSNumber *width in groupWidths) {
119+
if (cursor >= digits.length) {
120+
break;
121+
}
122+
if (formatted.length > 0) {
123+
[formatted appendString:@" "];
124+
}
125+
NSUInteger end = MIN(cursor + width.unsignedIntegerValue, digits.length);
126+
[formatted appendString:[digits substringWithRange:NSMakeRange(cursor, end - cursor)]];
127+
cursor = end;
128+
}
129+
// Digits past the last group keep grouping by threes rather than vanishing from the field.
130+
while (cursor < digits.length) {
131+
NSUInteger end = MIN(cursor + 3, digits.length);
132+
[formatted appendString:[NSString stringWithFormat:@" %@", [digits substringWithRange:NSMakeRange(cursor, end - cursor)]]];
133+
cursor = end;
134+
}
135+
return formatted;
136+
}
137+
138+
static NSUInteger AgentDeviceDigitCount(NSString *text, NSUInteger limit) {
139+
NSUInteger count = 0;
140+
for (NSUInteger index = 0; index < limit && index < text.length; index++) {
141+
if ([NSCharacterSet.decimalDigitCharacterSet characterIsMember:[text characterAtIndex:index]]) {
142+
count += 1;
143+
}
144+
}
145+
return count;
146+
}
147+
148+
@implementation AgentDeviceDigitGroupingTextField
149+
- (BOOL)textField:(UITextField *)textField
150+
shouldChangeCharactersInRange:(NSRange)range
151+
replacementString:(NSString *)string {
152+
NSString *combined = [textField.text stringByReplacingCharactersInRange:range withString:string];
153+
NSString *formatted = AgentDeviceFormatGroupedDigits(combined);
154+
textField.text = formatted;
155+
// Assigning `text` drops the caret at the end, which a real grouping formatter does not do:
156+
// a mid-string edit must keep editing where the user put the caret. The digits before the
157+
// caret survive the reformat unchanged, so the caret belongs before the digit that follows
158+
// them — past the separators the grouping inserted behind them — and at the end when it
159+
// followed all of them. Typed digits count as before the caret; a delete does not move it.
160+
NSUInteger digitsBeforeCaret =
161+
AgentDeviceDigitCount(textField.text, range.location) + AgentDeviceDigitCount(string, string.length);
162+
NSUInteger caretOffset = formatted.length;
163+
NSUInteger seen = 0;
164+
for (NSUInteger index = 0; index < formatted.length; index++) {
165+
if (![NSCharacterSet.decimalDigitCharacterSet characterIsMember:[formatted characterAtIndex:index]]) {
166+
continue;
167+
}
168+
seen += 1;
169+
if (seen == digitsBeforeCaret + 1) {
170+
caretOffset = index;
171+
break;
172+
}
173+
}
174+
textField.selectedTextRange = [textField textRangeFromPosition:[textField positionFromPosition:textField.beginningOfDocument offset:caretOffset]
175+
toPosition:[textField positionFromPosition:textField.beginningOfDocument offset:caretOffset]];
176+
return NO;
177+
}
178+
@end
100179
#endif
101180

102181
@implementation AgentDeviceRunnerViewController
@@ -408,8 +487,20 @@ - (void)viewDidLoad {
408487
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-regression"]) {
409488
BOOL digitCountValue =
410489
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-digit-count-value"];
411-
UITextField *textField =
412-
digitCountValue ? [[AgentDeviceDigitCountTextField alloc] init] : [[UITextField alloc] init];
490+
BOOL digitGroupingValue =
491+
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-digit-grouping-value"];
492+
UITextField *textField;
493+
if (digitCountValue) {
494+
textField = [[AgentDeviceDigitCountTextField alloc] init];
495+
} else if (digitGroupingValue) {
496+
textField = [[AgentDeviceDigitGroupingTextField alloc] init];
497+
} else {
498+
textField = [[UITextField alloc] init];
499+
}
500+
if (digitGroupingValue) {
501+
// The formatter field is its own delegate: every edit is reformatted before it lands.
502+
textField.delegate = (id<UITextFieldDelegate>)textField;
503+
}
413504
if (![NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-unnamed-input"]) {
414505
textField.accessibilityIdentifier = @"agent-device-hardware-keyboard-input";
415506
}

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+TextEntryConfirmation.swift‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,77 @@ extension RunnerTests {
4444
/// the baseline did not already (residual text), or it contains baseline + request in order. A
4545
/// value related to the entry in none of these ways, such as an OTP field announcing
4646
/// "6 of 6 digits", is the app's own representation, so retyping cannot make it match.
47+
///
48+
/// The two containment clauses keep an exception (#2634): a value that COMPLETES the request —
49+
/// every request character in order, plus inserted characters between the request's own
50+
/// characters that appear nowhere in the request — is not a degraded copy of anything. It is
51+
/// what a field that normalizes its content shows after receiving the request: the typed digits
52+
/// with a `## ### ## ##` grouping's separators between them (#2634), a phone mask's punctuation,
53+
/// a currency symbol. Degradation removes characters; entry cannot insert one between two
54+
/// characters the same burst typed, and a failed clear leaves residual text at the ENDS of the
55+
/// typed run, never between its characters — which is why an end-only insertion stays an echo
56+
/// (`"old123456"` for `"123456"`) and gets its repair, while an interior insertion does not.
57+
/// This is not a correctness claim: a cents-shifting mask (`"1000"` shown as `"$10.00"`) passes
58+
/// completion and is still worth 100x too little or too much. Completion only stops the value
59+
/// from being refused the unconfirmed outcome; nothing but exact equality verifies.
4760
static func textEntryValueEchoes(observed: String, expected: String, baseline: String) -> Bool {
4861
let request = textEntryRequestWithoutSubmitKeys(expected)
4962
let residualAndRequest = baseline + request
50-
return isOrderedSubsequence(observed, of: residualAndRequest)
51-
|| (isOrderedSubsequence(request, of: observed) && !isOrderedSubsequence(request, of: baseline))
63+
if isOrderedSubsequence(observed, of: residualAndRequest) {
64+
return true
65+
}
66+
if textValueCompletesRequest(observed: observed, request: request) {
67+
return false
68+
}
69+
return (isOrderedSubsequence(request, of: observed) && !isOrderedSubsequence(request, of: baseline))
5270
|| isOrderedSubsequence(residualAndRequest, of: observed)
5371
}
5472

73+
/// Whether `observed` shows every character of `request` in order with at least one inserted
74+
/// character strictly BETWEEN the request's first and last matched characters, none of the
75+
/// inserted characters appearing anywhere in the request. The embedding is the leftmost one, so
76+
/// a doubled entry (`"66"` for `"6"`, `"121"` for `"12"`) leaves its surplus at the ends and
77+
/// stays an echo, and a mask that inserts a character the request itself contains (a `.`
78+
/// separator for a value with a decimal point) falls back to the echo reading rather than
79+
/// pretending the embedding is ambiguous. A one-character request has no between, so nothing
80+
/// completes it.
81+
static func textValueCompletesRequest(observed: String, request: String) -> Bool {
82+
guard !request.isEmpty, request != observed else {
83+
return false
84+
}
85+
// The leftmost embedding of the request into the observed value.
86+
var matchedOffsets = IndexSet()
87+
var cursor = observed.startIndex
88+
for character in request {
89+
guard let match = observed[cursor...].firstIndex(of: character) else {
90+
return false
91+
}
92+
matchedOffsets.insert(observed.distance(from: observed.startIndex, to: match))
93+
cursor = observed.index(after: match)
94+
}
95+
// A one-character request has no between for an insertion to sit in.
96+
guard request.count > 1 else {
97+
return false
98+
}
99+
let first = matchedOffsets.first!
100+
let last = matchedOffsets.last!
101+
// Every character the embedding did not consume is inserted. None may be a request
102+
// character — with an ambiguous embedding the value may still be a dropped-and-shifted
103+
// copy, and the echo reading (failure, repair) is the honest one. At least one insertion
104+
// must sit strictly inside the matched run: end-only insertions are where a failed clear's
105+
// residual text lands.
106+
var sawInteriorInsertion = false
107+
for (offset, character) in observed.enumerated() where !matchedOffsets.contains(offset) {
108+
if request.contains(character) {
109+
return false
110+
}
111+
if offset > first && offset < last {
112+
sawInteriorInsertion = true
113+
}
114+
}
115+
return sawInteriorInsertion
116+
}
117+
55118
/// Classifies a replacement whose read-back never matched. The entry is unconfirmed, not failed,
56119
/// only when the same element's value moved off its pre-entry baseline to one that does not echo
57120
/// the request; every other mismatch stays a failure.

0 commit comments

Comments
 (0)