Skip to content

Commit c6755d6

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; clause 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. Fixture: AgentDeviceDigitGroupingTextField reproduces the reported formatter; 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 c6755d6

6 files changed

Lines changed: 318 additions & 7 deletions

File tree

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

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,53 @@ - (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+
@implementation AgentDeviceDigitGroupingTextField
139+
- (BOOL)textField:(UITextField *)textField
140+
shouldChangeCharactersInRange:(NSRange)range
141+
replacementString:(NSString *)string {
142+
NSString *combined = [textField.text stringByReplacingCharactersInRange:range withString:string];
143+
textField.text = AgentDeviceFormatGroupedDigits(combined);
144+
return NO;
145+
}
146+
@end
100147
#endif
101148

102149
@implementation AgentDeviceRunnerViewController
@@ -408,8 +455,20 @@ - (void)viewDidLoad {
408455
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-regression"]) {
409456
BOOL digitCountValue =
410457
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-digit-count-value"];
411-
UITextField *textField =
412-
digitCountValue ? [[AgentDeviceDigitCountTextField alloc] init] : [[UITextField alloc] init];
458+
BOOL digitGroupingValue =
459+
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-digit-grouping-value"];
460+
UITextField *textField;
461+
if (digitCountValue) {
462+
textField = [[AgentDeviceDigitCountTextField alloc] init];
463+
} else if (digitGroupingValue) {
464+
textField = [[AgentDeviceDigitGroupingTextField alloc] init];
465+
} else {
466+
textField = [[UITextField alloc] init];
467+
}
468+
if (digitGroupingValue) {
469+
// The formatter field is its own delegate: every edit is reformatted before it lands.
470+
textField.delegate = (id<UITextFieldDelegate>)textField;
471+
}
413472
if (![NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-unnamed-input"]) {
414473
textField.accessibilityIdentifier = @"agent-device-hardware-keyboard-input";
415474
}

‎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.

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+TextEntryConfirmationTests.swift‎

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,46 @@ extension RunnerTests {
2020
XCTAssertTrue(Self.textEntryValueEchoes(observed: "", expected: "123456", baseline: ""))
2121
XCTAssertTrue(Self.textEntryValueEchoes(observed: "12456", expected: "123456", baseline: ""))
2222
XCTAssertTrue(Self.textEntryValueEchoes(observed: "old123456", expected: "123456", baseline: ""))
23-
XCTAssertTrue(Self.textEntryValueEchoes(observed: "(555) 123-4567", expected: "5551234567", baseline: ""))
23+
// #2634 flips this pin, deliberately. It asserted a phone mask's formatted value IS an echo,
24+
// but "echo" means "could be a degraded copy of baseline + request", and a mask value with
25+
// every requested digit present is not degraded — entry cannot insert a separator between
26+
// two characters the same burst typed. Classifying it as an echo withheld the unconfirmed
27+
// outcome from the exact field that shape was built for (fill-evidence.ts names app-owned
28+
// formatting as its purpose; Android already reports this case unconfirmed), and pushed the
29+
// runner into a destructive retype of a correct field. The mask is now an echo only when
30+
// something was actually lost from it.
31+
XCTAssertFalse(Self.textEntryValueEchoes(observed: "(555) 123-4567", expected: "5551234567", baseline: ""))
2432
XCTAssertTrue(Self.textEntryValueEchoes(observed: "hello", expected: "hello\n", baseline: ""))
2533
XCTAssertFalse(Self.textEntryValueEchoes(observed: "6 of 6 digits", expected: "123456", baseline: ""))
2634
XCTAssertFalse(Self.textEntryValueEchoes(observed: "6 digits", expected: "123456", baseline: ""))
2735
}
2836

37+
// The completion predicate's own table: positives are real formatter outputs for their
38+
// request; the negatives are the degradations and residual shapes the echo must keep.
39+
// Pairing them is the whole duty of this predicate — completion never claims correctness
40+
// (only exact equality verifies), it only decides who may reach the unconfirmed outcome.
41+
func testOnlyInteriorForeignInsertionsCompleteARequest() {
42+
XCTAssertTrue(Self.textValueCompletesRequest(observed: "00 062 91 77", request: "000629177"))
43+
XCTAssertTrue(Self.textValueCompletesRequest(observed: "(555) 123-4567", request: "5551234567"))
44+
XCTAssertTrue(Self.textValueCompletesRequest(observed: "$10.00", request: "1000"))
45+
// Dropped digit inside the grouping: the value no longer contains the request at all.
46+
XCTAssertFalse(Self.textValueCompletesRequest(observed: "00 62 91 7", request: "000629177"))
47+
XCTAssertFalse(Self.textValueCompletesRequest(observed: "00 062 91 7", request: "000629177"))
48+
// Reordering is a drop plus a re-insertion: the request is not in order inside it.
49+
XCTAssertFalse(Self.textValueCompletesRequest(observed: "00 026 91 77", request: "000629177"))
50+
// End-only insertions are where residual text from a failed clear lands.
51+
XCTAssertFalse(Self.textValueCompletesRequest(observed: "old123456", request: "123456"))
52+
XCTAssertFalse(Self.textValueCompletesRequest(observed: "123456old", request: "123456"))
53+
// A doubled entry leaves its surplus at the ends under the leftmost embedding.
54+
XCTAssertFalse(Self.textValueCompletesRequest(observed: "66", request: "6"))
55+
XCTAssertFalse(Self.textValueCompletesRequest(observed: "121", request: "12"))
56+
// A one-character request has no between; a mask separator that appears IN the request makes
57+
// the embedding ambiguous, so the echo reading stands.
58+
XCTAssertFalse(Self.textValueCompletesRequest(observed: "$0.05", request: "5"))
59+
XCTAssertFalse(Self.textValueCompletesRequest(observed: "1.234.5", request: "12.5"))
60+
XCTAssertFalse(Self.textValueCompletesRequest(observed: "anything", request: ""))
61+
}
62+
2963
func testDigitCountSummaryThatMovedOffItsBaselineIsUnconfirmed() {
3064
let evidence = Self.unconfirmedTextEntryEvidence(
3165
requested: "123456",
@@ -90,6 +124,84 @@ extension RunnerTests {
90124
}
91125
}
92126

127+
// #2634's outcome, named: a normalizing field reaches the EXISTING unconfirmed evidence path
128+
// once its value completes the request. Unconfirmed is a soft success with target-bound
129+
// before/after evidence — never `verified: true`. A cents-shifting mask ("$10.00" typed as
130+
// "1000" is 100x off, "$0.05" as "5" is 100x off the other way) passes completion because no
131+
// text rule can see the number behind the string, so the runner discloses and the caller
132+
// reads. The refusals are the shapes whose entry may have failed: the drop, the stale
133+
// residual, and the write-back truncation of #2903, whose owner needs "adxe" to stay a
134+
// typed failure until that issue replaces the assumption with a proven or typed outcome.
135+
func testACompletingFieldReachesUnconfirmedAndDegradationsStayFailures() {
136+
let grouping = Self.unconfirmedTextEntryEvidence(
137+
requested: "000629177",
138+
baseline: Self.otpObservation(""),
139+
observed: Self.otpObservation("00 062 91 77")
140+
)
141+
XCTAssertEqual(grouping?.requested, "000629177")
142+
XCTAssertEqual(grouping?.before, "")
143+
XCTAssertEqual(grouping?.after, "00 062 91 77")
144+
145+
let phone = Self.unconfirmedTextEntryEvidence(
146+
requested: "5551234567",
147+
baseline: Self.otpObservation(""),
148+
observed: Self.otpObservation("(555) 123-4567")
149+
)
150+
XCTAssertEqual(phone?.after, "(555) 123-4567")
151+
152+
// The cents-shift pair: disclosed, never verified.
153+
let currency = Self.unconfirmedTextEntryEvidence(
154+
requested: "1000",
155+
baseline: Self.otpObservation(""),
156+
observed: Self.otpObservation("$10.00")
157+
)
158+
XCTAssertEqual(currency?.after, "$10.00")
159+
// A value that lost its decimal under a thousands-separator mask matches no echo clause and
160+
// was unconfirmed before this change; pin that it stays so.
161+
let lostSeparator = Self.unconfirmedTextEntryEvidence(
162+
requested: "10.50",
163+
baseline: Self.otpObservation(""),
164+
observed: Self.otpObservation("1,050")
165+
)
166+
XCTAssertEqual(lostSeparator?.after, "1,050")
167+
// A digit lost under the grouping itself: unconfirmed today, and still unconfirmed.
168+
let lostDigit = Self.unconfirmedTextEntryEvidence(
169+
requested: "000629177",
170+
baseline: Self.otpObservation(""),
171+
observed: Self.otpObservation("00 062 91 7")
172+
)
173+
XCTAssertEqual(lostDigit?.after, "00 062 91 7")
174+
175+
// The refusals, each the completion positives' closest negative. Note the shape they share:
176+
// every one of them is still an echo — a value that could be a degraded copy — which is the
177+
// only refusal this path now makes. Values that match no echo clause at all (a digit lost
178+
// under the grouping, "00 062 91 7") were unconfirmed before this change and still are:
179+
// completion only ever ADDS to the echo set's exceptions, never removes one.
180+
for (name, requested, observed) in [
181+
("dropped characters", "123456", "12456"),
182+
("stale residual text", "123456", "old123456"),
183+
("write-back truncation (#2903)", "ada@example", "adxe"),
184+
("doubled entry", "6", "66"),
185+
] {
186+
XCTAssertNil(
187+
Self.unconfirmedTextEntryEvidence(
188+
requested: requested,
189+
baseline: Self.otpObservation(""),
190+
observed: Self.otpObservation(observed)
191+
),
192+
name
193+
)
194+
}
195+
// A value that never moved off its baseline is still no evidence, however well it completes.
196+
XCTAssertNil(
197+
Self.unconfirmedTextEntryEvidence(
198+
requested: "5551234567",
199+
baseline: Self.otpObservation("(555) 123-4567"),
200+
observed: Self.otpObservation("(555) 123-4567")
201+
)
202+
)
203+
}
204+
93205
func testElementWithoutIdentifierIsTheSameOnlyAtTheSameTypeAndFrame() {
94206
let frame = CGRect(x: 0, y: 0, width: 100, height: 40)
95207
let field = TextEntryElementIdentity(identifier: nil, elementType: "TextField", frame: frame)
@@ -133,6 +245,61 @@ extension RunnerTests {
133245
try assertDigitCountFillIsUnconfirmed(code: "6", summary: "1 of 6 digits")
134246
}
135247

248+
/// #2634: a field that reformats what it holds, the way a `## ### ## ##` digit-grouping formatter
249+
/// does. Every requested digit arrives, so the fill succeeds with target-bound evidence instead of
250+
/// failing on a value raw equality can never reach — and, the reported half, the runner does not
251+
/// clear and retype the field it was asked to fill: the unconfirmed check runs before any repair,
252+
/// and the response message proves it ("typed after repair" would name the retype).
253+
@MainActor
254+
func testFillIntoDigitGroupingFieldReportsUnconfirmedEvidenceAndDoesNotRepair() throws {
255+
app.launchArguments = [
256+
"--agent-device-text-entry-regression",
257+
"--agent-device-text-entry-digit-grouping-value",
258+
]
259+
app.launch()
260+
addTeardownBlock { [self] in
261+
invalidateCachedTarget(reason: "unit_test_cleanup")
262+
app.terminate()
263+
}
264+
let textField = app.textFields["agent-device-hardware-keyboard-input"]
265+
XCTAssertTrue(textField.waitForExistence(timeout: appExistenceTimeout))
266+
let frame = textField.frame
267+
let command = try JSONDecoder().decode(
268+
Command.self,
269+
from: JSONSerialization.data(withJSONObject: [
270+
"command": "type",
271+
"commandId": "fill-digit-grouping",
272+
"text": "000629177",
273+
"textEntryMode": "replace",
274+
"x": frame.midX,
275+
"y": frame.midY,
276+
"appBundleId": "com.callstack.agentdevice.runner",
277+
])
278+
)
279+
280+
let failuresBeforeType = currentXCTestFailureCount()
281+
let response = executeTypeCommand(activeApp: app, command: command)
282+
283+
XCTAssertFalse(didRecordXCTestFailure(since: failuresBeforeType))
284+
XCTAssertTrue(response.ok, String(describing: response.error))
285+
XCTAssertEqual(response.data?.message, "typed")
286+
XCTAssertEqual(response.data?.verification, "unconfirmed")
287+
XCTAssertEqual(response.data?.requested, "000629177")
288+
XCTAssertEqual(response.data?.before, "")
289+
XCTAssertEqual(response.data?.after, "00 062 91 77")
290+
XCTAssertEqual(response.data?.target?.resourceId, "agent-device-hardware-keyboard-input")
291+
// The field is left holding the digits the request carried, grouped: neither cleared out nor
292+
// doubled by a retype.
293+
let deadline = Date().addingTimeInterval(appExistenceTimeout)
294+
var observed: String?
295+
while Date() < deadline {
296+
observed = textField.value as? String
297+
if observed == "00 062 91 77" { break }
298+
Thread.sleep(forTimeInterval: 0.25)
299+
}
300+
XCTAssertEqual(observed, "00 062 91 77")
301+
}
302+
136303
/// Fills the digit-count fixture field with `code` and asserts unconfirmed evidence and one entry.
137304
@MainActor
138305
private func assertDigitCountFillIsUnconfirmed(code: String, summary: String) throws {

0 commit comments

Comments
 (0)