Skip to content

Commit 4b0d146

Browse files
committed
fix(ios-runner): bound the synthesized replacement pace so a value-owning field keeps it
A replacement burst typed at XCTest's default 60 characters/second outruns an app that owns its field's value and re-applies it after the edit that produced it, which is how a controlled React Native `TextInput` behaves. That write erases whatever the burst typed while it was in flight and the field then sits stable short of the request: first characters and tail survive, a middle run is gone, which is the `fill id="field-email" ada@example` -> `aexample` CI signature. The commit wait can refuse such a value but cannot repair it, because a retype races the same write. 12 characters/second spaces characters ~83 ms apart. The new `--agent-device-text-entry-async-value-write` fixture owns its field's value and the iOS lane test pins the pace against it: raising the pace re-opens the race and turns that test red. A write-back still in flight 150 ms after an edit corrupts a burst at this pace too, so this narrows the window rather than closing it, and the measured wall-clock cost of the pace is recorded next to the commit ceiling it sits upstream of. Closes #2080
1 parent 622435e commit 4b0d146

6 files changed

Lines changed: 146 additions & 8 deletions

File tree

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ @interface AgentDeviceRunnerViewController : UIViewController
6565
@property(nonatomic, strong) UILabel *alertActivationBusyAnswer;
6666
@property(nonatomic, assign) NSUInteger firstAlertActions;
6767
@property(nonatomic, assign) NSUInteger replacementAlertActions;
68+
@property(nonatomic, strong) UILabel *textEntryWriteBackStatus;
69+
@property(nonatomic, assign) NSUInteger textEntryWriteBackAttempts;
70+
@property(nonatomic, assign) NSUInteger textEntryWriteBackApplies;
6871
@property(nonatomic, assign) BOOL alertFixtureStarted;
6972
@property(nonatomic, strong) NSTimer *alertActivationBusyBackstop;
7073
@property(nonatomic, strong) NSTimer *alertBannerRepost;
@@ -175,6 +178,12 @@ - (void)updateAlertActionStatus {
175178
(unsigned long)self.replacementAlertActions];
176179
}
177180

181+
- (void)updateTextEntryWriteBackStatus {
182+
self.textEntryWriteBackStatus.text = [NSString stringWithFormat:@"Write-backs: %lu attempted, %lu applied",
183+
(unsigned long)self.textEntryWriteBackAttempts,
184+
(unsigned long)self.textEntryWriteBackApplies];
185+
}
186+
178187
- (void)presentAlertFixtureReplacement:(BOOL)replacement {
179188
NSArray<NSString *> *arguments = NSProcessInfo.processInfo.arguments;
180189
BOOL sameTitle = [arguments containsObject:@"--agent-device-alert-same-title"];
@@ -232,7 +241,35 @@ - (void)viewDidAppear:(BOOL)animated {
232241
}
233242
#endif
234243

244+
// How long after an edit this fixture writes the value it observed back into the field. The write
245+
// has to land after the next character of a fast burst has arrived for it to erase anything, and
246+
// the lane test needs it to land before the next character of a paced burst does, so the window is
247+
// one character interval at the old 60 characters/second (16.7ms) to one at the pace synthesized
248+
// text entry now types at (~83ms). 25ms sits near the fast end, which keeps the slow side
249+
// comfortable on a loaded host at the cost of a thin margin on the fast side.
250+
static const NSTimeInterval AgentDeviceTextEntryAsyncWriteDelaySeconds = 0.025;
251+
235252
- (void)agentDeviceTextEntryDidChange:(UITextField *)textField {
253+
// A field whose app owns its value: like a controlled React Native `TextInput`, this fixture
254+
// re-applies the value it observed a moment after the edit that produced it. A replacement burst
255+
// typed faster than that write lands loses whatever it typed while the write was in flight, and
256+
// the field settles stable short of the requested text.
257+
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-async-value-write"]) {
258+
NSString *observedText = [textField.text copy];
259+
__weak UITextField *weakTextField = textField;
260+
dispatch_after(
261+
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(AgentDeviceTextEntryAsyncWriteDelaySeconds * NSEC_PER_SEC)),
262+
dispatch_get_main_queue(),
263+
^{
264+
self.textEntryWriteBackAttempts += 1;
265+
UITextField *field = weakTextField;
266+
if (field != nil && field.window != nil && ![field.text isEqualToString:observedText]) {
267+
field.text = observedText;
268+
self.textEntryWriteBackApplies += 1;
269+
}
270+
[self updateTextEntryWriteBackStatus];
271+
});
272+
}
236273
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-disappear-after-input"] &&
237274
textField.text.length > 0) {
238275
[textField removeFromSuperview];
@@ -293,6 +330,20 @@ - (void)viewDidLoad {
293330
[textField.widthAnchor constraintEqualToConstant:240],
294331
[textField.heightAnchor constraintEqualToConstant:44],
295332
]];
333+
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-async-value-write"]) {
334+
// Reports how many write-backs this fixture ran and how many of them changed the field, so a
335+
// lane test can tell a burst that survived the race from an inert fixture. Counts only: no
336+
// field content crosses into the test.
337+
self.textEntryWriteBackStatus = [[UILabel alloc] init];
338+
self.textEntryWriteBackStatus.accessibilityIdentifier = @"agent-device-text-entry-write-backs";
339+
self.textEntryWriteBackStatus.translatesAutoresizingMaskIntoConstraints = NO;
340+
[self.view addSubview:self.textEntryWriteBackStatus];
341+
[NSLayoutConstraint activateConstraints:@[
342+
[self.textEntryWriteBackStatus.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
343+
[self.textEntryWriteBackStatus.topAnchor constraintEqualToAnchor:textField.bottomAnchor constant:12],
344+
]];
345+
[self updateTextEntryWriteBackStatus];
346+
}
296347
}
297348

298349
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-crowded-screen"]) {

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerSynthesizedTextEntry.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ typedef NS_ENUM(NSInteger, RunnerSynthesizedTextEntryStatus) {
2222
+ (RunnerSynthesizedTextEntryResult *)synthesizeTextWithApplication:(id)application
2323
text:(NSString *)text;
2424

25-
// Replaces the current first responder's contents using one synthesized
26-
// Command-A, Delete, and text-input event sequence.
25+
// Replaces the current first responder's contents with one synthesized Command-A record
26+
// followed by a text-input record, typed at the bounded pace declared in the implementation.
2727
+ (RunnerSynthesizedTextEntryResult *)replaceTextWithApplication:(id)application
2828
text:(NSString *)text;
2929

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerSynthesizedTextEntry.m‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55

66
static NSString *const RunnerTextSynthesisSurface = @"text";
77

8+
// XCTest's `typingSpeed:` argument is characters per second. At 60 the 11 characters of a `fill`
9+
// arrived at a fixture field across 131 ms (~13 ms per gap), which is faster than an app that owns
10+
// its field's value and re-applies it after the edit (a controlled React Native `TextInput`, an
11+
// async validator) can acknowledge: such a write lands between two characters of the burst and
12+
// erases what was typed while it was in flight, leaving a value that is stable short of the
13+
// request. 12 characters/second spaces them ~83 ms apart, so an app whose write-back lands inside
14+
// one character interval no longer has anything to erase. It is not immunity: a write-back still in
15+
// flight 150 ms after an edit corrupted an 11-character burst at this pace too. The
16+
// `--agent-device-text-entry-async-value-write` lane test pins the relationship.
17+
static const NSUInteger RunnerTextEntryTypingSpeedCharactersPerSecond = 12;
18+
819
typedef id (*RunnerTextMsgSendInit)(id, SEL, NSString *);
920
typedef id (*RunnerTextMsgSendInitPath)(id, SEL);
1021
typedef void (*RunnerTextMsgSendType)(id, SEL, NSString *, NSTimeInterval, NSUInteger, BOOL);
@@ -128,7 +139,14 @@ + (RunnerSynthesizedTextEntryResult *)replaceTextWithApplication:(id)application
128139
);
129140
}
130141
((RunnerMsgSendSetInteger)objc_msgSend)(record, bridge.core.setTargetProcessIDSelector, targetProcessID);
131-
((RunnerTextMsgSendType)objc_msgSend)(path, bridge.typeTextSelector, text, 0.0, 60, YES);
142+
((RunnerTextMsgSendType)objc_msgSend)(
143+
path,
144+
bridge.typeTextSelector,
145+
text,
146+
0.0,
147+
RunnerTextEntryTypingSpeedCharactersPerSecond,
148+
YES
149+
);
132150
((RunnerMsgSendAddPath)objc_msgSend)(record, bridge.core.addPathSelector, path);
133151

134152
NSError *error = nil;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ extension RunnerTests {
5151
static let synthesizedCommitStallTimeout: TimeInterval = 3.0
5252
/// The commit wait's absolute bound, however long characters keep arriving. Sits well inside
5353
/// the daemon's per-command budget (`RUNNER_COMMAND_TIMEOUT_MS`, 45s), which also has to cover
54-
/// focus, clear and verification around this wait.
54+
/// focus, clear and verification around this wait. Synthesized delivery happens before this
55+
/// wait starts, so long text spends its character intervals upstream of it: a 240-character
56+
/// `fill` measures ~21s end to end at the bounded pace, and the budget runs out near 500.
5557
static let synthesizedCommitCeiling: TimeInterval = 10.0
5658
static let synthesizedCommitPollInterval: TimeInterval = 0.2
5759
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import XCTest
2+
3+
extension RunnerTests {
4+
#if AGENT_DEVICE_RUNNER_UNIT_TESTS && os(iOS)
5+
/// A field whose app owns its value re-applies that value a moment after the edit which produced
6+
/// it, the way a controlled React Native `TextInput` does. A replacement burst typed faster than
7+
/// the write lands has its in-flight characters erased by the app itself, so the field settles
8+
/// stable short of the request — the shape CI reported for `fill id="field-email" ada@example`
9+
/// as `aexample`. The pace bound on synthesized replacement is what this pins: raising it back
10+
/// re-opens the race and this test goes red. The commit wait cannot stand in for it, because a
11+
/// retype races the same write instead of repairing it.
12+
func testSynthesizedReplacementSurvivesFieldValueWrittenBackByTheApp() throws {
13+
app.launchArguments = [
14+
"--agent-device-text-entry-regression", "--agent-device-text-entry-async-value-write"
15+
]
16+
app.launch()
17+
defer {
18+
clearSnapshotXCTestChannelPenalty(reason: "test-cleanup")
19+
invalidateCachedTarget(reason: "unit_test_cleanup")
20+
app.terminate()
21+
}
22+
XCTAssertTrue(app.waitForExistence(timeout: appExistenceTimeout))
23+
24+
let textField = app.textFields["agent-device-hardware-keyboard-input"]
25+
XCTAssertTrue(textField.waitForExistence(timeout: appExistenceTimeout))
26+
currentApp = app
27+
currentBundleId = "com.callstack.agentdevice.runner"
28+
currentAppProcessIdentifier = try XCTUnwrap(Self.processIdentifier(of: app))
29+
30+
let focusCommand = try runnerCommandFixture(
31+
#"{"command":"tap","commandId":"tap-async-write-field","selectorKey":"id","selectorValue":"agent-device-hardware-keyboard-input"}"#
32+
)
33+
let focusResponse = try executeOnMainPrepared(command: focusCommand, activeApp: app)
34+
XCTAssertTrue(focusResponse.ok, String(describing: focusResponse.error))
35+
36+
penalizeSnapshotXCTestChannel(bundleId: nil, reason: "test")
37+
38+
let frame = textField.frame
39+
XCTAssertFalse(frame.isEmpty)
40+
41+
// Twice: the second replacement selects the first one's value away, which is the shape the
42+
// reported CI trace had — a `fill` onto a field that already held text.
43+
for commandId in ["fill-async-write-first", "fill-async-write-second"] {
44+
let command = try runnerCommandFixture(
45+
#"{"command":"type","commandId":"\#(commandId)","text":"ada@example","textEntryMode":"replace","x":\#(frame.midX),"y":\#(frame.midY)}"#
46+
)
47+
let failuresBeforeType = currentXCTestFailureCount()
48+
let response = executeTypeCommand(activeApp: app, command: command)
49+
XCTAssertTrue(response.ok, String(describing: response.error))
50+
XCTAssertEqual(response.data?.textEntryRoute, "synthesized-first-responder-replacement")
51+
XCTAssertFalse(didRecordXCTestFailure(since: failuresBeforeType))
52+
XCTAssertEqual(String(describing: textField.value ?? ""), "ada@example")
53+
}
54+
55+
// Without a write-back that actually ran, the value above would only prove the fixture is
56+
// inert. A block scheduled during the burst can also fire after it, which is why only the
57+
// per-edit attempts are asserted: whether a write landed in flight is what the value checks
58+
// above answer, and the label's applied count is there to read when they do not.
59+
Thread.sleep(forTimeInterval: 0.5)
60+
let writeBackCounts = app.staticTexts["agent-device-text-entry-write-backs"].label
61+
.split(whereSeparator: { !$0.isNumber }).compactMap { Int($0) }
62+
XCTAssertEqual(writeBackCounts.count, 2, "unexpected write-back status: \(writeBackCounts)")
63+
XCTAssertGreaterThan(try XCTUnwrap(writeBackCounts.first), 0)
64+
}
65+
#endif
66+
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ extension RunnerTests {
9191
// A value with a hole in the middle is neither a matching prefix nor an exact match, and must
9292
// never settle. These are the two corruption strings actually observed in CI on `fill`
9393
// (id="field-name" "Ada Lovelace" -> "Avelace", id="field-email" "ada@example" -> "aexample";
94-
// first character and tail survive, a middle run is missing).
95-
func testSynthesizedReplacementCommitCatchesDroppedMiddleCharacters() {
94+
// first character and tail survive, a middle run is missing). The wait can refuse a value like
95+
// this but not repair it: no later read distinguishes it from a field that has settled.
96+
func testSynthesizedReplacementCommitCatchesMiddleRunMissingFromTheField() {
9697
let corruptions: [(expected: String, observedAfterDrop: String)] = [
9798
(expected: "Ada Lovelace", observedAfterDrop: "Avelace"),
9899
(expected: "ada@example", observedAfterDrop: "aexample"),
@@ -284,8 +285,8 @@ extension RunnerTests {
284285
// landing as "aexample" CI signature). The fake synthesizer never actually writes into
285286
// Springboard, so the wait's `observe()` reads nil (no matching field at that point) on every
286287
// poll and the value never becomes "abc" — under the replacement-mode outcome function that is
287-
// correctly a failure (see `testSynthesizedReplacementCommitCatchesDroppedMiddleCharacters` for
288-
// why it must NOT be waved through as success), so this call runs the real 3-second deadline
288+
// correctly a failure (see `testSynthesizedReplacementCommitCatchesMiddleRunMissingFromTheField`
289+
// for why it must NOT be waved through as success), so this call runs the real 3-second deadline
289290
// (`TextEntryTiming.synthesizedCommitStallTimeout`; a nil read never advances the expected
290291
// prefix, so `SynthesizedCommitDeadline` grants it no extra time) before returning. That is
291292
// deliberate here, not a flake: this test only runs in the nightly XCUITest lane (see

0 commit comments

Comments
 (0)