Skip to content

Commit d4eb753

Browse files
pcbeingused333claudethymikee
authored
fix(platform-apple): simplify alert action retry budgets (#4123)
* fix(platform-apple): do not start an alert action retry the remaining budget cannot hold The alert act loop slept the poll interval and then started another runner read with whatever budget was left, which could be a few milliseconds. The runner answers such a read with a main-thread timeout, which is not a typed absence, so a quiet screen was reported as a failed read. Stop once the remaining budget cannot hold a read, so the window ends with the last typed absence. The wait loop already guards this on main. * refactor(platform-apple): reuse the poll-interval floor for alert action retries Drops MIN_ALERT_READ_BUDGET_MS and canHoldRead: the act loop now skips a retry when no more than ALERT_POLL_INTERVAL_MS remains, like awaitAppleAlert. The test also asserts the loop retried at least once. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * fix(platform-apple): start an alert read only with enough budget to find and press a late prompt Both alert loops skipped a read with no more than one poll interval left. A live run showed a permission prompt appearing about 1 s into the command still ended MAIN_THREAD_TIMEOUT: the read that saw it started with 300-760 ms left, and a read needs up to about 1.4 s to find and press the button. Replace the shared poll-interval floor with one named ALERT_MIN_READ_BUDGET_MS (1.5 s) in the alert contract, used by awaitAppleAlert and actOnAppleAlert. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * test(daemon): dismiss retries a typed absence once under the alert read floor Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * fix(platform-apple): keep the poll-interval read floor for the macOS helper The 1.5 s floor exists for the XCTest runner's main-thread watchdog. The macOS helper takes no budget, so it keeps reading until one poll interval remains. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * docs(contracts): state that the alert read floor gates retry reads only Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * fix(platform-apple): keep the alert read floor out of the wait path The 1500 ms floor blinded short waits. It now gates only the act loop's runner retries; the wait path stays on the poll-interval rule from #4187. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(platform-apple): give runner alert actions a 3 s retry window With the read floor, a 2 s window could not reach a prompt that appears after about 0.4 s. The runner leg now retries in a 3 s window, and a quiet screen still gives up sooner than the old 2 s loop. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(platform-apple): simplify alert action retry budgets * docs(contracts): correct live alert read timings --------- Co-authored-by: Claude Sonnet 5.5 <[email protected]> Co-authored-by: Michał Pierzchała <[email protected]>
1 parent 0db6ed4 commit d4eb753

3 files changed

Lines changed: 112 additions & 1 deletion

File tree

‎packages/contracts/src/alert-contract.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
export const ALERT_POLL_INTERVAL_MS = 300;
2+
/**
3+
* The minimum budget for a retry of `alert accept` or `alert dismiss` through the XCTest runner.
4+
* A read cut off by the runner's main-thread watchdog is reported as `MAIN_THREAD_TIMEOUT`, not
5+
* absence. Live late-prompt reads were accepted up to 1492 ms; three were cut off at 1502–1610 ms.
6+
* `alert wait` and the macOS helper do not use it.
7+
*/
8+
export const ALERT_MIN_READ_BUDGET_MS = 1_500;
29
export const DEFAULT_ALERT_TIMEOUT_MS = 10_000;
310
export const ALERT_ACTION_RETRY_MS = 2_000;
411

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ vi.mock('../core/runner-client.ts', () => ({ runAppleRunnerCommand: vi.fn() }));
55
vi.mock('../os/macos/helper.ts', () => ({ runMacOsAlertAction: vi.fn() }));
66

77
import {
8+
ALERT_MIN_READ_BUDGET_MS,
89
ALERT_NOT_FOUND_REASON,
910
ALERT_NOT_FOUND_RUNNER_CODE,
11+
ALERT_POLL_INTERVAL_MS,
1012
ALERT_UNRESOLVED_RUNNER_CODE,
1113
} from '@agent-device/contracts/alert-contract';
1214
import { AppError } from '@agent-device/kernel/errors';
@@ -150,6 +152,85 @@ test('an exhausted accept carries the scoped-snapshot fallback the agent needs n
150152
assert.match(String(error.details?.hint), /scoped snapshot/i);
151153
});
152154

155+
test('an action gives every retry at least the minimum read budget', async () => {
156+
vi.useFakeTimers();
157+
mockRunner.mockRejectedValue(runnerAbsence());
158+
159+
const outcome = actOnAppleAlert(
160+
IOS_SIMULATOR,
161+
runnerOptions,
162+
'accept',
163+
undefined,
164+
process.env,
165+
).then(
166+
() => undefined,
167+
(error: unknown) => error,
168+
);
169+
await vi.advanceTimersByTimeAsync(5_000);
170+
171+
await outcome;
172+
const budgets = mockRunner.mock.calls.map((call) => (call[1] as { timeoutMs: number }).timeoutMs);
173+
assert.ok(budgets.length >= 2, 'the act loop never retried');
174+
for (const budget of budgets.slice(1)) {
175+
assert.ok(budget >= ALERT_MIN_READ_BUDGET_MS, `retry started with only ${budget} ms`);
176+
}
177+
});
178+
179+
test('a runner action still reaches a prompt that appears 0.8 s into its window', async () => {
180+
vi.useFakeTimers();
181+
const start = Date.now();
182+
mockRunner.mockImplementation(async () => {
183+
if (Date.now() - start < 800) throw runnerAbsence();
184+
return { accepted: true };
185+
});
186+
187+
const outcome = actOnAppleAlert(
188+
IOS_SIMULATOR,
189+
runnerOptions,
190+
'accept',
191+
undefined,
192+
process.env,
193+
).then(
194+
(value) => value,
195+
(error: unknown) => error,
196+
);
197+
await vi.advanceTimersByTimeAsync(5_000);
198+
199+
assert.deepEqual(await outcome, { accepted: true });
200+
const lastRead = mockRunner.mock.calls.at(-1)?.[1] as { timeoutMs: number };
201+
assert.ok(lastRead.timeoutMs >= ALERT_MIN_READ_BUDGET_MS);
202+
});
203+
204+
test('a slow first action read still retries within the two-second window', async () => {
205+
vi.useFakeTimers();
206+
mockRunner.mockImplementation(async () => {
207+
if (mockRunner.mock.calls.length === 1) {
208+
await new Promise((resolve) => setTimeout(resolve, 1_300));
209+
throw runnerAbsence();
210+
}
211+
return { accepted: true };
212+
});
213+
214+
const outcome = actOnAppleAlert(
215+
IOS_SIMULATOR,
216+
runnerOptions,
217+
'accept',
218+
undefined,
219+
process.env,
220+
).then(
221+
(value) => value,
222+
(error: unknown) => error,
223+
);
224+
await vi.advanceTimersByTimeAsync(5_000);
225+
226+
assert.deepEqual(await outcome, { accepted: true });
227+
assert.equal(mockRunner.mock.calls.length, 2);
228+
const retryCall = mockRunner.mock.calls[1];
229+
assert.ok(retryCall);
230+
const retry = retryCall[1] as { timeoutMs: number };
231+
assert.equal(retry.timeoutMs, ALERT_MIN_READ_BUDGET_MS);
232+
});
233+
153234
// The whole point of typing absence: a backend that failed for any other reason must not be
154235
// retried until the window expires and then reported as a timeout. These three pin that the
155236
// evidence — not the message text — is what decides.
@@ -203,6 +284,24 @@ test('an action does not retry a failure that only reads like an absence', async
203284
assert.equal(error.details?.hint, undefined);
204285
});
205286

287+
test('a macOS action retries while more than a poll interval remains: its helper has no runner watchdog', async () => {
288+
vi.useFakeTimers();
289+
const firstReadMs = 2_000 - ALERT_POLL_INTERVAL_MS - ALERT_POLL_INTERVAL_MS - 1;
290+
mockHelper.mockImplementation(async () => {
291+
if (mockHelper.mock.calls.length === 1) {
292+
await new Promise((resolve) => setTimeout(resolve, firstReadMs));
293+
throw helperAbsence();
294+
}
295+
return { action: 'accept' };
296+
});
297+
298+
const outcome = actOnAppleAlert(MACOS_DEVICE, runnerOptions, 'accept', undefined, process.env);
299+
await vi.advanceTimersByTimeAsync(firstReadMs + ALERT_POLL_INTERVAL_MS);
300+
301+
assert.deepEqual(await outcome, { action: 'accept' });
302+
assert.equal(mockHelper.mock.calls.length, 2);
303+
});
304+
206305
test('the macOS helper states absence as a typed reason, and the family retries it', async () => {
207306
vi.useFakeTimers();
208307
let calls = 0;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
ALERT_ACTION_RETRY_MS,
3+
ALERT_MIN_READ_BUDGET_MS,
34
ALERT_NOT_FOUND_REASON,
45
ALERT_NOT_FOUND_RUNNER_CODE,
56
ALERT_POLL_INTERVAL_MS,
@@ -128,6 +129,10 @@ export async function awaitAppleAlert(
128129
* is still animating in — or an unreadable prompt. Any other failure is reported on its first
129130
* occurrence. A window exhausted on absence carries the scoped-snapshot fallback the agent needs
130131
* next; one exhausted on an unreadable prompt keeps the runner's own hint.
132+
*
133+
* Retries start within `ALERT_ACTION_RETRY_MS`. Each XCTest runner retry gets at least
134+
* `ALERT_MIN_READ_BUDGET_MS` to avoid cutting alert acquisition short, so an in-flight retry
135+
* can finish after the retry window. The macOS helper ignores the read budget.
131136
*/
132137
export async function actOnAppleAlert(
133138
device: DeviceInfo,
@@ -145,7 +150,7 @@ export async function actOnAppleAlert(
145150
try {
146151
const budgetMs = firstAttempt
147152
? runnerTimeoutMs
148-
: remainingBudgetMs(start, ALERT_ACTION_RETRY_MS);
153+
: Math.max(remainingBudgetMs(start, ALERT_ACTION_RETRY_MS), ALERT_MIN_READ_BUDGET_MS);
149154
firstAttempt = false;
150155
return await runAlert(action, budgetMs);
151156
} catch (error) {

0 commit comments

Comments
 (0)