Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/daemon/src/execution/handlers/snapshot-alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 0 additions & 15 deletions packages/platform-apple/src/__tests__/alert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });

Expand Down
20 changes: 14 additions & 6 deletions packages/platform-apple/src/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>;
Expand Down Expand Up @@ -95,11 +95,7 @@ export async function readAppleAlert(
options: AlertInteractorOptions | undefined,
env: Readonly<EnvMap>,
): Promise<Record<string, unknown>> {
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);
}

Expand Down Expand Up @@ -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));
}
Expand Down
Loading