Skip to content

Commit f584e8a

Browse files
committed
fix(alerts): let only the latest answered capture claim a visible dialog
Two review findings on the confirm split, one batch: - The spent-budget exits of confirmTreeAlertDismissed omitted details.reason, while commands.md promises every post-press failure branches on alert-window-expired. Both unconfirmed and still-visible failures now carry the typed reason, pinned in the Android and Limrun suites. - The same-alert flag latched on the first matching capture, so a later undecided re-read still reported the dialog as currently visible. Each probe now replaces the verdict with its own observation: the still-visible failure requires the latest capture that answered to name the same alert, and a fresher undecided read exits as an unconfirmed press. A probe whose capture never answered is not a completed observation, so the thrown cutoff keeps saying only that the press went unconfirmed, at any earlier match. Pinned fail-before at the pre-fix alert.ts: same-alert then undecided to budget-end, and same-alert then a hung capture, both expect the unconfirmed message and the typed reason. The helper-scope comment is trimmed to the non-obvious constraint per AGENTS.
1 parent 29a3b13 commit f584e8a

5 files changed

Lines changed: 79 additions & 25 deletions

File tree

‎packages/platform-android/src/__tests__/alert.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ test('dismiss fails when the dialog is still visible after the action budget', a
182182
(error: unknown) =>
183183
error instanceof Error &&
184184
error.message === 'alert dismiss did not dismiss the visible alert' &&
185-
(error as { code?: string }).code === 'COMMAND_FAILED',
185+
(error as { code?: string }).code === 'COMMAND_FAILED' &&
186+
(error as { details?: { reason?: unknown } }).details?.reason === 'alert-window-expired',
186187
);
187188
assert.equal(runAndroidAdb.mock.calls.length, 1);
188189
});

‎packages/platform-android/src/alert.ts‎

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -493,14 +493,15 @@ async function confirmTreeAlertDismissed<
493493
action: 'accept' | 'dismiss',
494494
signal?: AbortSignal,
495495
): Promise<void> {
496-
// Only an observation that named this same alert on screen proves the "still visible" claim.
497-
// Captures that were cut off or stayed unresolvable prove nothing, and the failure wording
498-
// must not accuse a dialog that may already be gone of still standing.
499-
let sameAlertObserved = false;
496+
// The "still visible" claim belongs to the latest capture that answered, never to a match a
497+
// later undecided re-read overruled. The window-closed sentinel is the budget ending, not an
498+
// observation, so it leaves the latest completed capture as the freshest fact.
499+
let lastCaptureNamesAlert = false;
500500
const probe: TreeAlertObserver<TCandidate> = async () => {
501501
const observation = await observe();
502-
if (observation.kind === 'alert' && sameTreeAlert(observation.candidate.alert, dismissed)) {
503-
sameAlertObserved = true;
502+
if (observation.kind !== 'window-closed') {
503+
lastCaptureNamesAlert =
504+
observation.kind === 'alert' && sameTreeAlert(observation.candidate.alert, dismissed);
504505
}
505506
return observation;
506507
};
@@ -516,33 +517,26 @@ async function confirmTreeAlertDismissed<
516517
signal,
517518
}));
518519
} catch (error) {
519-
// A confirmation capture this phase cuts off says what the press cannot say alone: whether
520-
// the dialog is still there, or whether the check itself never answered. Only the first is
521-
// the stuck-alert failure; the second must not claim a visible dialog it never observed.
520+
// The newest probe never answered, so no capture of this phase can claim where the dialog
521+
// stands at the budget's end — even one that saw the alert a poll earlier.
522522
if (isAlertWindowExpired(error)) {
523-
if (sameAlertObserved) {
524-
throw new AppError('COMMAND_FAILED', `alert ${action} did not dismiss the visible alert`, {
525-
alert: dismissed,
526-
reason: ALERT_WINDOW_EXPIRED_REASON,
527-
hint: 'The alert button was pressed but the dialog is still visible. Inspect alert get --json, then press the visible button by label/ref or retry.',
528-
});
529-
}
530523
throw new AppError(
531524
'COMMAND_FAILED',
532525
`alert ${action} pressed the button but the dismissal could not be confirmed`,
533526
{
534527
alert: dismissed,
535528
reason: ALERT_WINDOW_EXPIRED_REASON,
536-
hint: 'The alert button was pressed; the post-press read never answered within the budget, so the dialog may already be gone. Run alert get --json before retrying.',
529+
hint: 'The alert button was pressed; the latest post-press check never answered within the budget, so the dialog may already be gone. Run alert get --json before retrying.',
537530
},
538531
);
539532
}
540533
throw error;
541534
}
542535
if (settled) return;
543-
if (sameAlertObserved) {
536+
if (lastCaptureNamesAlert) {
544537
throw new AppError('COMMAND_FAILED', `alert ${action} did not dismiss the visible alert`, {
545538
alert: dismissed,
539+
reason: ALERT_WINDOW_EXPIRED_REASON,
546540
hint: 'The alert button was pressed but the dialog is still visible. Inspect alert get --json, then press the visible button by label/ref or retry.',
547541
});
548542
}
@@ -551,6 +545,7 @@ async function confirmTreeAlertDismissed<
551545
`alert ${action} pressed the button but the dismissal could not be confirmed`,
552546
{
553547
alert: dismissed,
548+
reason: ALERT_WINDOW_EXPIRED_REASON,
554549
hint: 'The alert button was pressed; no post-press read resolved to an answer within the budget, so the dialog may already be gone. Run alert get --json before retrying.',
555550
},
556551
);

‎packages/provider-limrun/src/alert.test.ts‎

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,68 @@ describe('acting budgets', () => {
191191
it('spends the confirm budget and reports a stuck alert', async () => {
192192
const client = clientWithTrees([limrunAttAlertTree(), limrunAttAlertTree()]);
193193

194-
await expect(underFakeTimers(() => handleLimrunIosAlert(client, 'dismiss'))).rejects.toThrow(
195-
'alert dismiss did not dismiss the visible alert',
194+
const thrown = await underFakeTimers(() => handleLimrunIosAlert(client, 'dismiss')).then(
195+
() => undefined,
196+
(error: unknown) => error,
197+
);
198+
expect(thrown).toBeInstanceOf(AppError);
199+
const failure = thrown as AppError;
200+
expect(failure.message).toBe('alert dismiss did not dismiss the visible alert');
201+
// Every spent-budget exit of an acting leg branches on this reason, per commands.md.
202+
expect(failure.details?.reason).toBe('alert-window-expired');
203+
expect(client.tap).toHaveBeenCalledTimes(1);
204+
});
205+
206+
it('reports an unconfirmed dismissal when an undecided re-read overrules the last match', async () => {
207+
// Confirm probe 1 re-names the pressed alert; probe 2 is alert-shaped yet unresolvable and
208+
// stays that way to the end of the budget. The undecided read is the fresher fact, so the
209+
// leg may not report the dialog as standing on the older match — it reports the press as
210+
// unconfirmed, which is all a spent budget with no settling read may say.
211+
const client = clientWithTrees([
212+
limrunAttAlertTree(),
213+
limrunAttAlertTree(),
214+
[limrunAttAlertTree(), limrunAttAlertTree()],
215+
]);
216+
217+
const thrown = await underFakeTimers(() => handleLimrunIosAlert(client, 'dismiss')).then(
218+
() => undefined,
219+
(error: unknown) => error,
220+
);
221+
expect(thrown).toBeInstanceOf(AppError);
222+
const failure = thrown as AppError;
223+
expect(failure.message).toBe(
224+
'alert dismiss pressed the button but the dismissal could not be confirmed',
225+
);
226+
expect(failure.details?.reason).toBe('alert-window-expired');
227+
});
228+
229+
it('reports an unconfirmed dismissal when a later capture hangs after one re-named the alert', async () => {
230+
// Same alert at confirm probe 1, then a stalled transport the budget cuts off. With the
231+
// newest read unanswered, the dialog's state at the budget's end is unknown: the leg must
232+
// not claim it is still visible on the older match.
233+
const client = {
234+
elementTree: vi
235+
.fn()
236+
.mockResolvedValueOnce(limrunAttAlertTree())
237+
.mockResolvedValueOnce(limrunAttAlertTree())
238+
.mockImplementationOnce(
239+
async () =>
240+
new Promise((resolve) => {
241+
setTimeout(() => resolve([limrunAppScreenTree()]), 3_000);
242+
}),
243+
),
244+
tap: vi.fn(async () => {}),
245+
} satisfies LimrunIosAlertClient;
246+
247+
const thrown = await underFakeTimers(() => handleLimrunIosAlert(client, 'accept')).then(
248+
() => undefined,
249+
(error: unknown) => error,
250+
);
251+
expect(thrown).toBeInstanceOf(AppError);
252+
expect((thrown as AppError).message).toBe(
253+
'alert accept pressed the button but the dismissal could not be confirmed',
196254
);
255+
expect((thrown as AppError).details?.reason).toBe('alert-window-expired');
197256
expect(client.tap).toHaveBeenCalledTimes(1);
198257
});
199258

‎src/core/interactors/android.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ export function createAndroidInteractor(
6060
const capture = await snapshotAndroid(device, {
6161
includeHiddenContentHints: false,
6262
signal: seam.signal,
63-
// Alert probes must share the session's warm helper like every other app-backed read:
64-
// a command-scope alert probe cold-starts the helper past the window that bounds it,
65-
// so every re-poll and the post-press confirm would abort before the device answers.
63+
// App-backed sessions keep the snapshot helper warm; a command-scoped probe would
64+
// force-stop and cold-restart it inside the window that bounds the probe.
6665
helperSessionScope,
6766
});
6867
return buildSnapshotState(androidSnapshotPublicationInput(capture), undefined).nodes;

‎website/docs/docs/commands.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ agent-device alert dismiss
386386
- Polling wait timeouts, and a replayed wait refused with `wait_landmark_identity_mismatch`, also carry `captures`, `readableCaptures`, and `polls` so you can see where the budget went. Each poll has `startedMs`, `durationMs`, and an `outcome`: `readable`, `unreadable`, `retriable`, `deadline`, `runner-restart`, or `readiness`. Long waits keep the first 5 and last 25 polls. A wait that never saw a readable capture reports the error its polls hit, with that error's code and retry details, instead of a generic timeout; `wait stable` and a never-readable strict absence keep their own diagnostics. `logPath` links the full request log.
387387
- `alert` (same as `alert get`) inspects or handles system alerts on iOS simulator, macOS desktop, and Android permission dialogs. On iOS it also covers app-owned modal popups with native blocking markers. `alert wait <ms>` takes a budget up to 2147483647 ms (~24.8 days); larger is refused as `INVALID_ARGS` before anything runs.
388388
- On the snapshot-derived backends (Android, Limrun iOS) `alert get --json` answers with a typed `status`: `visible`, `none`, or `undecided` (an alert-shaped capture that resolves to no single alert). Branch on `status`, not on the message.
389-
- On those backends, when `alert accept`/`dismiss` fails, `error.details.reason` names why. Before any press, `dispatched: no` and the reason is `alert-not-found` (the action budget passed with no alert seen), `alert-window-expired` (the budget closed before a capture or the press could resolve), `alert_capture_undecided`, or `alert_button_undecided` (the sheet is too ambiguous to act on). After a press, “did not dismiss the visible alert” means a re-read named the same dialog again; “could not be confirmed” means the press went out but no post-press read resolved in time, so the dialog may already be gone; `dispatched: unknown` means the press itself was cut off and may have landed. Run `alert get` before acting again.
389+
- On those backends, when `alert accept`/`dismiss` fails, `error.details.reason` names why. Before any press, `dispatched: no` and the reason is `alert-not-found` (the action budget passed with no alert seen), `alert-window-expired` (the budget closed before a capture or the press could resolve), `alert_capture_undecided`, or `alert_button_undecided` (the sheet is too ambiguous to act on). After a press, `dispatched` is absent and the reason is `alert-window-expired`; “did not dismiss the visible alert” means the latest post-press capture that answered named the same dialog again, “could not be confirmed” means the press went out but the latest read was unresolvable or never answered, so the dialog may already be gone; `dispatched: unknown` means the press itself was cut off and may have landed. Run `alert get` before acting again.
390390
- On Android, an alert capture that outlives its budget is stopped in flight. On Limrun the transport cannot cancel a request: the command refuses a late answer and presses nothing, but a hung capture can keep running after the command has already failed. If Limrun alert commands stall, restart the instance session.
391391
- Use `alert get` for a quick check, and `alert wait <short-ms>` only when a prompt may appear after async work. Don't use `settings permission` to answer a dialog already on screen; it is for setup before a flow.
392392
- `accept` and `dismiss` are sent once on every platform and never replayed, even if the response is lost. On iOS the runner activates the button once, then only observes until the alert disappears, changes, or the deadline expires. A changed alert does not prove a permission was granted; verify the app outcome.

0 commit comments

Comments
 (0)