Skip to content

Commit c48bb4a

Browse files
committed
fix(ios): observe fill once without destructive repair
1 parent bbc93e3 commit c48bb4a

13 files changed

Lines changed: 312 additions & 442 deletions

File tree

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

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ @interface AgentDeviceRunnerViewController : UIViewController
7171
// took focus from an input that has stopped answering the accessibility query that found it.
7272
@property(nonatomic, strong) UILabel *textEntryFocusWitness;
7373
@property(nonatomic, assign) NSUInteger textEntryWriteBacks;
74+
@property(nonatomic, assign) NSUInteger textEntryTotalEdits;
7475
@property(nonatomic, copy, nullable) NSString *textEntryRenderedValue;
7576
@property(nonatomic, assign) NSTimeInterval textEntryLastEditTime;
7677
@property(nonatomic, assign) NSTimeInterval textEntryBurstStartTime;
@@ -106,6 +107,16 @@ - (NSString *)accessibilityValue {
106107
@interface AgentDeviceDigitGroupingTextField : UITextField <UITextFieldDelegate>
107108
@end
108109

110+
@interface AgentDeviceCurrencyTextField : UITextField <UITextFieldDelegate>
111+
@property(nonatomic, strong) UILabel *entryStatus;
112+
@property(nonatomic, assign) NSUInteger insertedDigits;
113+
@end
114+
115+
@interface AgentDeviceSubmitClearingTextField : UITextField <UITextFieldDelegate>
116+
@property(nonatomic, strong) UILabel *entryStatus;
117+
@property(nonatomic, assign) NSUInteger submits;
118+
@end
119+
109120
static NSString *AgentDeviceFormatGroupedDigits(NSString *raw) {
110121
NSMutableString *digits = [NSMutableString string];
111122
NSCharacterSet *nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
@@ -176,6 +187,33 @@ - (BOOL)textField:(UITextField *)textField
176187
return NO;
177188
}
178189
@end
190+
191+
@implementation AgentDeviceCurrencyTextField
192+
- (BOOL)textField:(UITextField *)textField
193+
shouldChangeCharactersInRange:(NSRange)range
194+
replacementString:(NSString *)string {
195+
NSString *combined = [textField.text stringByReplacingCharactersInRange:range withString:string];
196+
NSMutableString *digits = [NSMutableString string];
197+
for (NSString *part in [combined componentsSeparatedByCharactersInSet:NSCharacterSet.decimalDigitCharacterSet.invertedSet]) {
198+
[digits appendString:part];
199+
}
200+
unsigned long long cents = digits.longLongValue;
201+
textField.text = [NSString stringWithFormat:@"$%llu.%02llu", cents / 100, cents % 100];
202+
self.insertedDigits += AgentDeviceDigitCount(string, string.length);
203+
self.entryStatus.text = [NSString stringWithFormat:@"%lu", (unsigned long)self.insertedDigits];
204+
textField.selectedTextRange = [textField textRangeFromPosition:textField.endOfDocument toPosition:textField.endOfDocument];
205+
return NO;
206+
}
207+
@end
208+
209+
@implementation AgentDeviceSubmitClearingTextField
210+
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
211+
self.submits += 1;
212+
self.entryStatus.text = [NSString stringWithFormat:@"%lu", (unsigned long)self.submits];
213+
textField.text = @"";
214+
return YES;
215+
}
216+
@end
179217
#endif
180218

181219
@implementation AgentDeviceRunnerViewController
@@ -281,11 +319,12 @@ - (void)updateAlertActionStatus {
281319
- (void)updateTextEntryWriteBackStatus {
282320
NSTimeInterval burstSpan = self.textEntryLastEditTime - self.textEntryBurstStartTime;
283321
self.textEntryWriteBackStatus.text = [NSString
284-
stringWithFormat:@"write-backs=%lu burst-edits=%lu burst-ms=%lu min-gap-ms=%lu",
322+
stringWithFormat:@"write-backs=%lu burst-edits=%lu burst-ms=%lu min-gap-ms=%lu total-edits=%lu",
285323
(unsigned long)self.textEntryWriteBacks,
286324
(unsigned long)self.textEntryBurstEdits,
287325
(unsigned long)llround(burstSpan * 1000),
288-
(unsigned long)llround(self.textEntryBurstMinGap * 1000)];
326+
(unsigned long)llround(self.textEntryBurstMinGap * 1000),
327+
(unsigned long)self.textEntryTotalEdits];
289328
}
290329

291330
- (void)presentAlertFixtureReplacement:(BOOL)replacement {
@@ -371,6 +410,7 @@ - (void)agentDeviceTextEntryDidChange:(UITextField *)textField {
371410
// typed faster than the app renders loses the characters that arrived while a render was in
372411
// flight, and the field settles stable short of the request.
373412
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-app-owned-value"]) {
413+
self.textEntryTotalEdits += 1;
374414
NSTimeInterval now = NSProcessInfo.processInfo.systemUptime;
375415
NSTimeInterval gap = now - self.textEntryLastEditTime;
376416
BOOL overtookARender = self.textEntryRenderedValue != nil && gap < self.textEntryAcknowledgeWindowSeconds;
@@ -489,16 +529,24 @@ - (void)viewDidLoad {
489529
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-digit-count-value"];
490530
BOOL digitGroupingValue =
491531
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-digit-grouping-value"];
532+
BOOL currencyValue =
533+
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-currency-value"];
534+
BOOL submitClears =
535+
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-submit-clears"];
492536
UITextField *textField;
493537
if (digitCountValue) {
494538
textField = [[AgentDeviceDigitCountTextField alloc] init];
495539
} else if (digitGroupingValue) {
496540
textField = [[AgentDeviceDigitGroupingTextField alloc] init];
541+
} else if (currencyValue) {
542+
textField = [[AgentDeviceCurrencyTextField alloc] init];
543+
textField.text = @"$0.00";
544+
} else if (submitClears) {
545+
textField = [[AgentDeviceSubmitClearingTextField alloc] init];
497546
} else {
498547
textField = [[UITextField alloc] init];
499548
}
500-
if (digitGroupingValue) {
501-
// The formatter field is its own delegate: every edit is reformatted before it lands.
549+
if (digitGroupingValue || currencyValue || submitClears) {
502550
textField.delegate = (id<UITextFieldDelegate>)textField;
503551
}
504552
if (![NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-unnamed-input"]) {
@@ -525,6 +573,22 @@ - (void)viewDidLoad {
525573
[textField.widthAnchor constraintEqualToConstant:240],
526574
[textField.heightAnchor constraintEqualToConstant:AgentDeviceTextEntryFieldHeight],
527575
]];
576+
if (currencyValue || submitClears) {
577+
UILabel *entryStatus = [[UILabel alloc] init];
578+
entryStatus.accessibilityIdentifier = @"agent-device-text-entry-events";
579+
entryStatus.text = @"0";
580+
entryStatus.translatesAutoresizingMaskIntoConstraints = NO;
581+
[self.view addSubview:entryStatus];
582+
[NSLayoutConstraint activateConstraints:@[
583+
[entryStatus.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
584+
[entryStatus.topAnchor constraintEqualToAnchor:textField.bottomAnchor constant:12],
585+
]];
586+
if (currencyValue) {
587+
((AgentDeviceCurrencyTextField *)textField).entryStatus = entryStatus;
588+
} else {
589+
((AgentDeviceSubmitClearingTextField *)textField).entryStatus = entryStatus;
590+
}
591+
}
528592
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-unqueryable-on-focus"]) {
529593
self.textEntryFocusWitness = [[UILabel alloc] init];
530594
self.textEntryFocusWitness.accessibilityIdentifier = @"agent-device-text-entry-focus";

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,8 @@ extension RunnerTests {
522522
/// `awaitSynthesizedReplacementCommit` binds the real XCUI reads). The budget defaults to the
523523
/// shipped one, so only a test that is asking about time has to name it.
524524
///
525-
/// Only an exact match settles. A value with a hole in the middle ("ada@example" -> "aexample")
526-
/// is the corruption this wait exists to catch, and `.replacement` mode has no formatter or
527-
/// autocomplete carve-out: `isRepairableTextEntryMismatch` (RunnerTests+TextTyping.swift) treats
528-
/// every `.replacement` mismatch as repairable, because `fill` owns the whole field via
529-
/// select-all. A settled non-match is therefore always the wait's failure case.
525+
/// This route has no element-bound baseline for unconfirmed evidence. Only exact read-back
526+
/// confirms a commit; prefix progress extends the bounded wait without confirming the value.
530527
static func awaitSynthesizedReplacementCommitOutcome(
531528
expectedText: String,
532529
placeholder: String?,

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,8 @@ extension RunnerTests {
6363
static let pollInterval: TimeInterval = 0.02
6464
static let warmupValueTimeout: TimeInterval = 0.4
6565
static let verificationStabilityWindow: TimeInterval = 0.2
66-
/// How long a replacement value may take to move off its baseline, and then how long an unechoed
67-
/// value may keep changing before it must hold for `verificationStabilityWindow`; the app renders
68-
/// the last characters' summary late.
69-
static let unconfirmedSettleCeiling: TimeInterval = 1.0
66+
/// Total observation budget after replacement; late-rendered values must settle within it.
67+
static let replacementSettleCeiling: TimeInterval = 1.0
7068
/// How long the commit wait tolerates seeing NO further progress toward the expected value.
7169
/// Numerically the flat deadline this replaced, so a pipeline that delivers nothing is
7270
/// condemned at exactly the same instant it always was (see `SynthesizedCommitDeadline`).

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

Lines changed: 28 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import XCTest
22

3-
// What a replacement's read-back can prove when the field's value does not echo the typed text.
3+
// Target-bound observations and the replacement read-back policy.
44
// The wire shape mirrors the cross-platform `FillUnconfirmedVerification` in
55
// packages/contracts/src/fill-evidence.ts.
66
extension RunnerTests {
@@ -39,96 +39,37 @@ extension RunnerTests {
3939
let target: TextEntryElementIdentity
4040
}
4141

42-
/// Whether `observed` can be a degraded copy of the residual `baseline` plus the request: it is in
43-
/// order inside baseline + request (dropped characters), or it contains the request in order and
44-
/// the baseline did not already (residual text), or it contains baseline + request in order. A
45-
/// value related to the entry in none of these ways, such as an OTP field announcing
46-
/// "6 of 6 digits", is the app's own representation, so retyping cannot make it match.
47-
///
48-
/// The two containment clauses keep one exception (#2634): a value the request plus inserted
49-
/// formatting alone EXPLAINS — see `textValueCompletesRequest`, which owns the whole test
50-
/// including the baseline — is not a degraded copy of anything, and the field that normalizes
51-
/// its content shows exactly that after receiving the request. Completion is not correctness
52-
/// (a cents-shifting mask passes it); it only stops the value from being refused the
53-
/// unconfirmed outcome, and nothing but exact equality verifies.
54-
static func textEntryValueEchoes(observed: String, expected: String, baseline: String) -> Bool {
55-
let request = textEntryRequestWithoutSubmitKeys(expected)
56-
let residualAndRequest = baseline + request
57-
if isOrderedSubsequence(observed, of: residualAndRequest) {
58-
return true
59-
}
60-
if textValueCompletesRequest(observed: observed, request: request, baseline: baseline) {
61-
return false
62-
}
63-
return (isOrderedSubsequence(request, of: observed) && !isOrderedSubsequence(request, of: baseline))
64-
|| isOrderedSubsequence(residualAndRequest, of: observed)
65-
}
66-
67-
/// Whether the request plus inserted formatting explains the WHOLE of `observed`: every request
68-
/// character in order under the leftmost embedding, every unconsumed character explainable as
69-
/// neither a character of the request (an ambiguous embedding may be a dropped-and-shifted
70-
/// copy) nor of the post-clear `baseline` (any baseline character in the value is failed-clear
71-
/// residual, wherever a mask relocated it), and at least one insertion strictly BETWEEN the
72-
/// request's first and last characters — entry cannot insert between two characters the same
73-
/// burst typed, so end-only insertions (`"old123456"` for `"123456"`) stay echoes. A doubled
74-
/// entry (`"66"` for `"6"`) leaves its surplus at the ends; a mask inserting a request
75-
/// character (`.` for a decimal value) falls back to the echo reading; a one-character request
76-
/// has no between. Known limit: a mask that keeps its template through the clear shares
77-
/// characters with the value it formats (`"$0.00"` for `"$10.00"`), so its fill keeps the echo
78-
/// reading — text cannot tell surviving template from failed-clear residual.
79-
static func textValueCompletesRequest(observed: String, request: String, baseline: String) -> Bool {
80-
guard !request.isEmpty, request != observed, request.count > 1 else {
81-
return false
82-
}
83-
// The leftmost embedding of the request into the observed value.
84-
var consumedOffsets = IndexSet()
85-
var cursor = observed.startIndex
86-
for character in request {
87-
guard let match = observed[cursor...].firstIndex(of: character) else {
88-
return false
89-
}
90-
consumedOffsets.insert(observed.distance(from: observed.startIndex, to: match))
91-
cursor = observed.index(after: match)
92-
}
93-
let first = consumedOffsets.first!
94-
let last = consumedOffsets.last!
95-
var sawInteriorInsertion = false
96-
for (offset, character) in observed.enumerated() where !consumedOffsets.contains(offset) {
97-
// A request character makes the embedding ambiguous; a baseline character may be residual
98-
// a mask relocated into the span. Either way the request plus formatting does not explain
99-
// the value.
100-
if request.contains(character) || baseline.contains(character) {
101-
return false
102-
}
103-
if offset > first && offset < last {
104-
sawInteriorInsertion = true
105-
}
106-
}
107-
return sawInteriorInsertion
108-
}
109-
110-
/// Classifies a replacement whose read-back never matched. The entry is unconfirmed, not failed,
111-
/// only when the same element's value moved off its pre-entry baseline to one that does not echo
112-
/// the request; every other mismatch stays a failure.
113-
static func unconfirmedTextEntryEvidence(
42+
/// Literal read-back policy; a mismatch does not establish why the app changed the text.
43+
static func replacementTextEntryResult(
11444
requested: String,
11545
baseline: TextEntryObservation?,
11646
observed: TextEntryObservation?
117-
) -> TextEntryUnconfirmedEvidence? {
118-
guard !textEntryRequestWithoutSubmitKeys(requested).isEmpty,
119-
let baseline,
120-
let observed,
121-
baseline.identity.isSameElement(as: observed.identity),
122-
observed.value != baseline.value,
123-
!textEntryValueEchoes(observed: observed.value, expected: requested, baseline: baseline.value)
124-
else {
125-
return nil
47+
) -> TextEntryResult {
48+
func result(_ verified: Bool?) -> TextEntryResult {
49+
TextEntryResult(verified: verified, repaired: false, expectedText: requested, observedText: observed?.value)
12650
}
127-
return TextEntryUnconfirmedEvidence(
128-
requested: requested,
129-
before: baseline.value,
130-
after: observed.value,
131-
target: observed.identity
51+
guard let observed else { return result(nil) }
52+
if let baseline, !baseline.identity.isSameElement(as: observed.identity) {
53+
return result(false)
54+
}
55+
let request = observed.identity.elementType == elementTypeNamesByRawValue[XCUIElement.ElementType.textView.rawValue]
56+
? requested : textEntryRequestWithoutSubmitKeys(requested)
57+
if observed.value == requested || observed.value == request { return result(true) }
58+
// A submit may consume or clear a single-line input; repeating it can send the action twice.
59+
if request != requested { return result(nil) }
60+
guard !request.isEmpty, let baseline else { return result(false) }
61+
if isOrderedSubsequence(observed.value, of: baseline.value + request) { return result(false) }
62+
return TextEntryResult(
63+
verified: nil,
64+
repaired: false,
65+
expectedText: requested,
66+
observedText: observed.value,
67+
unconfirmed: TextEntryUnconfirmedEvidence(
68+
requested: requested,
69+
before: baseline.value,
70+
after: observed.value,
71+
target: observed.identity
72+
)
13273
)
13374
}
13475

0 commit comments

Comments
 (0)