Skip to content

Commit e479065

Browse files
committed
fix(ios): close false-failure windows in the commit wait
Adversarial review found two deterministic false failures in the wait added by the previous commit, plus a message that asserted a field state never read. The deadline was checked before observing, so a commit landing during the final poll sleep was condemned as never observed — under exactly the loaded-host timing the wait exists for. The check now runs after an observation, so the last thing before condemning is a read. `treatingPlaceholderAsEmpty` maps a value equal to the field's placeholder to "", a prefix of every expected value. `type "0.00"` into a field placeheld "0.00" committed instantly, read as pending for the full 3s, and failed. The observation now settles on an exact raw match; the normalized read still drives the prefix walk. The outcome-to-failure mapping moves to textEntryFailure(forCommitOutcome:) so the branch the command refuses on is pinned by a test rather than living only in a ternary. `.unobservable` staying a success is what keeps `type "...\n"` working, and it now has an assertion. Message and hint no longer claim the field holds a partial value: under both fixed windows it may hold all of it. The docs sentence no longer implies every text-entry route verifies its result — the replacement and keyboard-visible routes have no resolvable element to observe and are unchanged. Refs #1874, #1844
1 parent facc5b2 commit e479065

5 files changed

Lines changed: 77 additions & 11 deletions

File tree

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,32 @@ extension RunnerTests {
208208
observe: () -> String?,
209209
waitForNextObservation: () -> Void
210210
) -> SynthesizedTextCommitOutcome {
211-
while !isExpired() {
211+
// The deadline is checked AFTER an observation, never before one, so the last thing that
212+
// happens before condemning a commit is a read. Checking first would condemn a commit that
213+
// landed during the final poll sleep — the exact loaded-host timing this wait exists for.
214+
while true {
212215
switch synthesizedTextCommitProgress(observedText: observe(), expectedText: expectedText) {
213216
case .committed, .diverged:
214217
return .settled
215218
case .pending:
219+
if isExpired() { return .notObserved }
216220
waitForNextObservation()
217221
}
218222
}
219-
return .notObserved
223+
}
224+
225+
/// The command-level consequence of a commit wait. `.unobservable` is not a failure: there was
226+
/// no baseline to compare against, which is the pre-existing contract for submit-key text and
227+
/// unreadable fields, not evidence that anything went wrong.
228+
static func textEntryFailure(
229+
forCommitOutcome outcome: SynthesizedTextCommitOutcome
230+
) -> TextEntryFailure? {
231+
switch outcome {
232+
case .settled, .unobservable:
233+
return nil
234+
case .notObserved:
235+
return .commitNotObserved
236+
}
220237
}
221238

222239
/// Blocks until the synthesized bare-type text is observable in the target field, so `type`
@@ -238,15 +255,21 @@ extension RunnerTests {
238255
guard let textBefore, !typedText.contains("\n"), !typedText.contains("\r") else {
239256
return .unobservable
240257
}
258+
let expectedText = textBefore + typedText
241259
let deadline = Date().addingTimeInterval(TextEntryTiming.synthesizedCommitTimeout)
242260
return Self.awaitSynthesizedCommitOutcome(
243-
expectedText: textBefore + typedText,
261+
expectedText: expectedText,
244262
isExpired: { Date() >= deadline },
245263
observe: {
246-
editableTextValue(
247-
for: resolveTextEntryElement(app: app, target: target),
248-
treatingPlaceholderAsEmpty: true
249-
)
264+
let element = resolveTextEntryElement(app: app, target: target)
265+
// `treatingPlaceholderAsEmpty` maps a value equal to the field's placeholder to "", which
266+
// is a prefix of every expected value. Typing a string that happens to equal the
267+
// placeholder (`type "0.00"` into a field placeheld "0.00") would then read as pending
268+
// forever and be condemned at the deadline, having in fact committed immediately. The
269+
// exact raw match settles that case; the normalized read still drives the prefix walk,
270+
// where placeholder-as-empty is the reading we want.
271+
if editableTextValue(for: element) == expectedText { return expectedText }
272+
return editableTextValue(for: element, treatingPlaceholderAsEmpty: true)
250273
},
251274
waitForNextObservation: { sleepFor(TextEntryTiming.pollInterval) }
252275
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension RunnerTests {
1616
case .synthesisUnavailable:
1717
return "Reliable text synthesis is unavailable while the software keyboard is hidden."
1818
case .commitNotObserved:
19-
return "The app committed only part of the typed text before the commit deadline."
19+
return "The runner could not confirm the typed text committed before its deadline."
2020
}
2121
}
2222

@@ -27,7 +27,7 @@ extension RunnerTests {
2727
case .synthesisUnavailable:
2828
return "Show the software keyboard, then retry type or fill."
2929
case .commitNotObserved:
30-
return "The field holds a partial value. Read it back before retrying, and prefer fill, which replaces the whole value, over type, which appends to whatever committed."
30+
return "The field may hold none, part, or all of the text. Read it back before retrying, and prefer fill, which replaces the whole value, over type, which appends to whatever committed."
3131
}
3232
}
3333
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,54 @@ extension RunnerTests {
134134
XCTAssertEqual(index, 2)
135135
}
136136

137+
// Adversarial-review finding: the deadline used to be checked BEFORE observing, so a commit
138+
// landing during the final poll sleep was condemned as never observed — a false failure under
139+
// exactly the loaded-host timing this wait exists for. Red against that ordering.
140+
func testCommitLandingDuringTheFinalSleepIsStillObserved() {
141+
var polls = 0
142+
let outcome = Self.awaitSynthesizedCommitOutcome(
143+
expectedText: "hardware-keyboard",
144+
isExpired: { polls >= 1 },
145+
observe: { polls == 0 ? "hardware-" : "hardware-keyboard" },
146+
waitForNextObservation: { polls += 1 }
147+
)
148+
XCTAssertEqual(outcome, .settled)
149+
}
150+
151+
// Adversarial-review finding: `treatingPlaceholderAsEmpty` maps a value equal to the field's
152+
// placeholder to "", a prefix of everything. Typing a string that equals the placeholder
153+
// committed instantly yet read as pending until the deadline. The observation now settles on an
154+
// exact raw match, so this is the wait's view of that field.
155+
func testValueEqualToThePlaceholderSettlesInsteadOfStallingUntilTheDeadline() {
156+
var polls = 0
157+
let outcome = Self.awaitSynthesizedCommitOutcome(
158+
expectedText: "0.00",
159+
isExpired: { polls >= 4 },
160+
// What the fixed observe closure yields once the raw value matches: the exact text, not the
161+
// placeholder-normalized "" that the prefix walk would treat as still pending.
162+
observe: { "0.00" },
163+
waitForNextObservation: { polls += 1 }
164+
)
165+
XCTAssertEqual(outcome, .settled)
166+
XCTAssertEqual(polls, 0, "an exact match must not poll at all")
167+
}
168+
169+
// The mapping the command actually refuses on. `.unobservable` must stay a success: it is the
170+
// pre-existing contract for submit-key text and unreadable fields, so inverting it would fail
171+
// every `type "...\n"`.
172+
func testOnlyAnUnobservedCommitBecomesACommandFailure() {
173+
XCTAssertNil(Self.textEntryFailure(forCommitOutcome: .settled))
174+
XCTAssertNil(Self.textEntryFailure(forCommitOutcome: .unobservable))
175+
XCTAssertEqual(Self.textEntryFailure(forCommitOutcome: .notObserved), .commitNotObserved)
176+
}
177+
137178
func testCommitNotObservedCarriesItsOwnCodeAndRecovery() {
138179
XCTAssertEqual(TextEntryFailure.commitNotObserved.rawValue, "TEXT_INPUT_COMMIT_NOT_OBSERVED")
139180
// The recovery has to name fill: `type` appends, so retrying it over a partial value would
140181
// concatenate onto whatever committed rather than repair it.
141182
XCTAssertTrue(TextEntryFailure.commitNotObserved.hint.contains("fill"))
183+
// And it must not assert a field state the runner never read — the value may be complete.
184+
XCTAssertFalse(TextEntryFailure.commitNotObserved.message.contains("only part"))
142185
}
143186

144187
#if os(iOS)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ extension RunnerTests {
131131
// The characters were posted, so the element stays on the tuple for the caller's
132132
// bookkeeping; the failure is what makes the command refuse. Reporting ok here is the
133133
// defect this route had — the deadline was indistinguishable from a commit.
134-
return (currentTarget, commit == .notObserved ? .commitNotObserved : nil)
134+
return (currentTarget, Self.textEntryFailure(forCommitOutcome: commit))
135135
case .fallback:
136136
return (nil, .synthesisUnavailable)
137137
case .raise(let message):

‎website/docs/docs/commands.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ agent-device gesture transform 200 420 80 -40 2 35 700 # combined pan, zoom, and
427427
`type` accepts text only. Do not pass `@ref` to `type`; use `fill @ref "text"` to target a field directly, or `press @ref` then `type "text"` to append in the focused field.
428428
If `type` reports `TEXT_INPUT_NOT_FOCUSED`, focus a visible text input and retry; when accessibility does not expose the input, use a coordinate focus command before typing.
429429
On iOS, if `type` reports `TEXT_INPUT_SYNTHESIS_UNAVAILABLE` while the software keyboard is hidden, show the software keyboard, then retry `type` or `fill`. The runner reports this error instead of risking partial input through an unreliable text-entry path.
430-
On iOS, if `type` reports `TEXT_INPUT_COMMIT_NOT_OBSERVED`, the app committed only part of the typed text before the runner's commit deadline. The field holds a partial value: read it back, then prefer `fill`, which replaces the whole value, over `type`, which appends to whatever committed. The runner reports this instead of succeeding over a field whose contents it could not confirm.
430+
On iOS, if `type` reports `TEXT_INPUT_COMMIT_NOT_OBSERVED`, the runner could not confirm the typed text reached the field before its deadline. The field may hold none, part, or all of the text: read it back, then prefer `fill`, which replaces the whole value, over `type`, which appends to whatever committed. This covers the bare-type route, which observes the field after synthesizing; it is not a guarantee that every text-entry route verifies its result.
431431
Use plain `fill` or `type` first for ordinary login and form fields. Use `--delay-ms` on `type` or `fill` only when a debounced search field or search-as-you-type input actually misses characters, or when the app must receive incremental updates.
432432
Delayed typing intentionally prefers paced character entry over clipboard-style fallbacks so the target field receives each incremental update.
433433
On Android, `fill` also verifies text and treats IME-owned capture as a terminal failure instead of retrying against the wrong field.

0 commit comments

Comments
 (0)