From 11e3ab17371965f72bbf3e91686a678557aba80e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 15 Nov 2026 20:46:26 +0100 Subject: [PATCH] refactor(alert): one helper owns the alert runner budget Follow-up to #4540 review notes: alertRunnerBudgetMs owns the default, floor and ceiling; alertWaitTimeoutMs becomes alertTimeoutMs since it also feeds alert get; drop the hand-built watchdog test that only pinned existing behaviour. Co-Authored-By: Claude Opus 5.5 --- .../src/execution/handlers/snapshot-alert.ts | 10 +++++----- .../src/__tests__/alert.test.ts | 15 -------------- packages/platform-apple/src/alert.ts | 20 +++++++++++++------ 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/packages/daemon/src/execution/handlers/snapshot-alert.ts b/packages/daemon/src/execution/handlers/snapshot-alert.ts index 2e5601c197..78901d1e76 100644 --- a/packages/daemon/src/execution/handlers/snapshot-alert.ts +++ b/packages/daemon/src/execution/handlers/snapshot-alert.ts @@ -99,7 +99,7 @@ export async function handleAlertCommand( params; let session = ref ? sessionStore.requireCurrent(ref) : undefined; const action = readAlertAction(req.positionals?.[0]); - const timeoutMs = alertWaitTimeoutMs(action, req.positionals?.[1]); + const timeoutMs = alertTimeoutMs(action, req.positionals?.[1]); const bound = await resolveBoundAlertRuntime({ device, action, @@ -163,12 +163,12 @@ function readAlertAction(raw: string | undefined): AlertAction { } /** - * The `alert wait` budget before it reaches any leg. Past the contract's window ceiling the - * budget cannot be armed as a timer at all, so the command refuses the argument instead of - * starting a wait that would misfire; the driver's own default remains the last line for + * The `alert` timeout before it reaches any leg: the window `alert wait` polls in, and the budget + * one `alert get` read spends. Past the contract's window ceiling a wait cannot be armed as a timer + * at all, so the command refuses the argument instead of starting a wait that would misfire; the driver's own default remains the last line for * budgets that arrive by other routes. */ -function alertWaitTimeoutMs(action: AlertAction, rawTimeout: string | undefined): number { +function alertTimeoutMs(action: AlertAction, rawTimeout: string | undefined): number { const timeoutMs = parseTimeout(rawTimeout) ?? DEFAULT_TIMEOUT_MS; if (action === 'wait' && timeoutMs > MAX_ALERT_WINDOW_MS) { throw new AppError( diff --git a/packages/platform-apple/src/__tests__/alert.test.ts b/packages/platform-apple/src/__tests__/alert.test.ts index 28e9088410..4432b1a1d9 100644 --- a/packages/platform-apple/src/__tests__/alert.test.ts +++ b/packages/platform-apple/src/__tests__/alert.test.ts @@ -79,21 +79,6 @@ test.each([ }); }); -test('a read cut off by the runner watchdog is reported as itself, never as absence', async () => { - const watchdog = new AppError('COMMAND_FAILED', 'runner main thread timed out', { - runnerErrorCode: 'MAIN_THREAD_TIMEOUT', - reason: 'runner_main_thread_timeout', - dispatched: 'unknown', - }); - mockRunner.mockRejectedValue(watchdog); - - await assert.rejects( - alertIfPresent(readAppleAlert(IOS_SIMULATOR, runnerOptions, { timeoutMs: 3_000 }, process.env)), - (error: unknown) => error === watchdog, - ); - assert.equal(mockRunner.mock.calls.length, 1); -}); - test('a wait window above the ceiling is polled in runner attempts within the ceiling', async () => { mockRunner.mockResolvedValue({ title: 'Camera Access' }); diff --git a/packages/platform-apple/src/alert.ts b/packages/platform-apple/src/alert.ts index e4444f98ec..6bf69b3c8c 100644 --- a/packages/platform-apple/src/alert.ts +++ b/packages/platform-apple/src/alert.ts @@ -65,7 +65,7 @@ function runAppleAlert( command: 'alert', action, appBundleId: options?.appBundleId, - timeoutMs: Math.min(timeoutMs, ALERT_RUNNER_MAX_BUDGET_MS), + timeoutMs: alertRunnerBudgetMs(timeoutMs), }, runnerOptions, )) as Record; @@ -95,11 +95,7 @@ export async function readAppleAlert( options: AlertInteractorOptions | undefined, env: Readonly, ): Promise> { - const requestedMs = options?.timeoutMs; - const budgetMs = - requestedMs === undefined || !Number.isFinite(requestedMs) - ? DEFAULT_ALERT_TIMEOUT_MS - : Math.max(requestedMs, ALERT_MIN_READ_BUDGET_MS); + const budgetMs = alertRunnerBudgetMs(options?.timeoutMs, ALERT_MIN_READ_BUDGET_MS); return await runAppleAlert(device, runnerOptions, options, env)('get', budgetMs); } @@ -186,6 +182,18 @@ export async function actOnAppleAlert( throw withAlertFallbackHint(lastError); } +/** + * The runner budget for one alert attempt: the caller's window, at least `floorMs` and at most + * `ALERT_RUNNER_MAX_BUDGET_MS`, or `DEFAULT_ALERT_TIMEOUT_MS` when the caller gave no window. + */ +function alertRunnerBudgetMs(requestedMs: number | undefined, floorMs = 0): number { + const windowMs = + requestedMs === undefined || !Number.isFinite(requestedMs) + ? DEFAULT_ALERT_TIMEOUT_MS + : Math.max(requestedMs, floorMs); + return Math.min(windowMs, ALERT_RUNNER_MAX_BUDGET_MS); +} + function remainingBudgetMs(start: number, timeoutMs: number): number { return Math.max(1, timeoutMs - (Date.now() - start)); }