Skip to content

Commit da72842

Browse files
committed
fix(daemon): validate boot's --timeout deadline and cover it with a test
Compute the absolute boot deadline once at the daemon boundary, the same way open/prepare already do, instead of converting a raw timeoutMs inside the Apple runtime. This fixes a NaN/non-positive value silently disabling the boot_timeout budget, and removes the second, looser conversion path. Also adds a handler test asserting flags.timeoutMs reaches bootTarget's deadline, tightens the runtime-layer boot-deadline test assertions to also cover the bootstatus call, and corrects the commands.md line that claimed the 120s boot-wait cap applies without --timeout (the 90s request envelope ends the boot first).
1 parent d33d6e6 commit da72842

8 files changed

Lines changed: 99 additions & 14 deletions

File tree

‎packages/contracts/src/device-readiness-runtime.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import type { DeviceInventoryRequest } from './device-inventory.ts';
44
export type EnsureReadyInput = Readonly<{
55
serial?: string;
66
androidSerialAllowlist?: readonly string[];
7-
/** Startup budget in milliseconds, from `boot --timeout`. Bounds a cold Simulator boot wait. */
8-
timeoutMs?: number;
7+
/**
8+
* Absolute deadline (epoch ms), from `boot --timeout`, already validated finite and positive.
9+
* Bounds a cold Simulator boot wait; the Apple runtime is the only current consumer.
10+
*/
11+
deadlineAtMs?: number;
912
}>;
1013

1114
export type DeviceReadinessRuntimeOperations = Readonly<{

‎packages/platform-apple/src/runtime.test.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,19 @@ test('bootTarget forwards a --timeout budget as the Simulator boot deadline (#30
555555
},
556556
});
557557

558-
await binding.operations.bootTarget?.({ timeoutMs: 45_000 });
558+
const deadlineAtMs = Date.now() + 45_000;
559+
await binding.operations.bootTarget?.({ deadlineAtMs });
559560

560-
// The startup budget reaches the boot wait, same as open/prepare (#2325): the
561-
// envelope's --timeout is the deadline the simctl calls run under, not a fixed default.
561+
// The startup budget reaches the boot wait, same as open/prepare (#2325): every simctl call the
562+
// wait issues runs under the caller's --timeout budget until the absolute deadline, not a fixed
563+
// default. Both `boot` and `bootstatus` derive their timeout from the same deadline, so both
564+
// must sit within a tight window of the 45s budget - a fixed default like 10s or 30s would fail.
562565
const bootCall = calls.find((call) => call.args.includes('boot'));
566+
const bootstatusCall = calls.find((call) => call.args.includes('bootstatus'));
567+
expect(bootCall?.timeoutMs).toBeGreaterThan(44_900);
563568
expect(bootCall?.timeoutMs).toBeLessThanOrEqual(45_000);
564-
expect(bootCall?.timeoutMs).toBeGreaterThan(0);
569+
expect(bootstatusCall?.timeoutMs).toBeGreaterThan(44_900);
570+
expect(bootstatusCall?.timeoutMs).toBeLessThanOrEqual(45_000);
565571
});
566572

567573
test('macOS readiness is a no-op while boot remains unavailable', async () => {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,7 @@ export function createApplePlatformRuntime(host: PlatformRuntimeHost): PlatformR
478478
...whenAdmitted(facts.operations.bootTarget, () => ({
479479
bootTarget: async (input: EnsureReadyInput) =>
480480
await ensureAppleReady(host, request.device, request.scope.signal, {
481-
deadlineAtMs:
482-
typeof input.timeoutMs === 'number' ? Date.now() + input.timeoutMs : undefined,
481+
deadlineAtMs: input.deadlineAtMs,
483482
}),
484483
})),
485484
...whenAdmitted(facts.operations.listApps, () => ({

‎src/daemon/handlers/__tests__/session-boot-shutdown.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,70 @@ test('boot prefers explicit device selector over active session device', async (
9595
}
9696
});
9797

98+
test('boot --timeout forwards a startup deadline to bootTarget (#3004)', async () => {
99+
const sessionStore = makeSessionStore();
100+
const selectedDevice: SessionState['device'] = {
101+
platform: 'apple',
102+
id: 'sim-timeout',
103+
name: 'iPhone 17 Pro',
104+
kind: 'simulator',
105+
booted: false,
106+
};
107+
mockResolveTargetDevice.mockResolvedValue(selectedDevice);
108+
109+
const beforeMs = Date.now();
110+
const response = await handleSessionCommands({
111+
req: {
112+
token: 't',
113+
session: 'default',
114+
command: 'boot',
115+
positionals: [],
116+
flags: { platform: 'ios', device: 'iPhone 17 Pro', timeoutMs: 300_000 },
117+
},
118+
sessionName: 'default',
119+
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
120+
sessionStore,
121+
invoke: noopInvoke,
122+
});
123+
const afterMs = Date.now();
124+
125+
expect(response?.ok, JSON.stringify(response)).toBe(true);
126+
expect(mockEnsureReadyRuntime).toHaveBeenCalledOnce();
127+
const deadlineAtMs = mockEnsureReadyRuntime.mock.calls[0]?.[0]?.deadlineAtMs;
128+
expect(deadlineAtMs).toBeGreaterThanOrEqual(beforeMs + 300_000);
129+
expect(deadlineAtMs).toBeLessThanOrEqual(afterMs + 300_000);
130+
});
131+
132+
test('boot without --timeout leaves the startup deadline unset', async () => {
133+
const sessionStore = makeSessionStore();
134+
const selectedDevice: SessionState['device'] = {
135+
platform: 'apple',
136+
id: 'sim-no-timeout',
137+
name: 'iPhone 17 Pro',
138+
kind: 'simulator',
139+
booted: false,
140+
};
141+
mockResolveTargetDevice.mockResolvedValue(selectedDevice);
142+
143+
const response = await handleSessionCommands({
144+
req: {
145+
token: 't',
146+
session: 'default',
147+
command: 'boot',
148+
positionals: [],
149+
flags: { platform: 'ios', device: 'iPhone 17 Pro' },
150+
},
151+
sessionName: 'default',
152+
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
153+
sessionStore,
154+
invoke: noopInvoke,
155+
});
156+
157+
expect(response?.ok, JSON.stringify(response)).toBe(true);
158+
expect(mockEnsureReadyRuntime).toHaveBeenCalledOnce();
159+
expect(mockEnsureReadyRuntime.mock.calls[0]?.[0]?.deadlineAtMs).toBeUndefined();
160+
});
161+
98162
test('boot --headless admits a stopped Android emulator through facts and binds once', async () => {
99163
const sessionStore = makeSessionStore();
100164
const placeholder: SessionState['device'] = {

‎src/daemon/handlers/session-state.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '@agent-device/kernel/device';
1616
import type { DaemonRequest, DaemonResponse } from '../daemon-request.ts';
1717
import { SessionStore } from '../session-store.ts';
18+
import { startupDeadlineAtMs } from '../startup-deadline.ts';
1819
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
1920
import type { AppleApplicationState } from '@agent-device/kernel/snapshot';
2021
import { resolveAndroidSerialAllowlist } from '@agent-device/kernel/device-isolation';
@@ -302,7 +303,11 @@ export async function handleSessionStateCommands(params: {
302303
});
303304
if (admitted.type === 'response') return admitted.response;
304305

305-
const input = { serial: flags.serial, androidSerialAllowlist, timeoutMs: flags.timeoutMs };
306+
const input = {
307+
serial: flags.serial,
308+
androidSerialAllowlist,
309+
deadlineAtMs: startupDeadlineAtMs(flags.timeoutMs),
310+
};
306311
if (plan.kind === 'boot-target-headless') {
307312
device = await (await admitted.bind(device, plan.use)).operations.bootTargetHeadless(input);
308313
} else {

‎src/daemon/session-lifecycle/internal/session-open-prepare.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { openApplicationRuntimeUse } from '@agent-device/contracts/application-l
44
import type { BoundDeviceRuntime } from '@agent-device/contracts/platform-runtime';
55
import type { DeviceInfo } from '@agent-device/kernel/device';
66
import type { DaemonRequest, DaemonResponse } from '../../daemon-request.ts';
7+
import { startupDeadlineAtMs } from '../../startup-deadline.ts';
78
import type { SessionRuntimeHints, SessionState } from '../../session-state.ts';
89
import { SessionStore } from '../../session-store.ts';
910
import {
@@ -229,8 +230,5 @@ async function resolvePreparedOpenIdentity(params: {
229230

230231
/** `open --timeout` is a startup budget; it becomes the absolute deadline the boot wait honors. */
231232
function openStartupDeadlineAtMs(req: DaemonRequest): number | undefined {
232-
const timeoutMs = req.flags?.timeoutMs;
233-
return typeof timeoutMs === 'number' && Number.isFinite(timeoutMs) && timeoutMs > 0
234-
? Date.now() + timeoutMs
235-
: undefined;
233+
return startupDeadlineAtMs(req.flags?.timeoutMs);
236234
}

‎src/daemon/startup-deadline.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Converts a `--timeout <ms>` startup budget into the absolute deadline a readiness wait honors.
3+
* A missing, non-finite, or non-positive budget yields `undefined`, so callers fall back to their
4+
* own default wait instead of silently disabling it.
5+
*/
6+
export function startupDeadlineAtMs(timeoutMs: unknown): number | undefined {
7+
return typeof timeoutMs === 'number' && Number.isFinite(timeoutMs) && timeoutMs > 0
8+
? Date.now() + timeoutMs
9+
: undefined;
10+
}

‎website/docs/docs/commands.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ agent-device fold open
6969
- `--platform apple` is an alias for the Apple automation backend (`ios`, `tvOS`, `macOS` selection).
7070
- Use `--target mobile|tv|desktop` with `--platform` (required) to select phone/tablet vs TV-class vs desktop-class targets.
7171
- `boot` is mainly needed when starting a new session and `open` fails because no booted simulator/emulator is available.
72-
- `boot --timeout <ms>` is a startup budget for the Simulator boot, same as `open`'s. A never-booted iOS Simulator runs Apple's first-boot migration, which can take several minutes; without the flag the boot wait is capped at 120 seconds. When the budget runs out the command fails with `error.details.reason: boot_timeout` and the Simulator keeps booting, so a retry finds it further along.
72+
- `boot --timeout <ms>` is a startup budget for the Simulator boot, same as `open`'s. A never-booted iOS Simulator runs Apple's first-boot migration, which can take several minutes; without the flag the command's 90-second request envelope ends the boot first, before the wait's own 120-second cap ever applies. When the budget runs out the command fails with `error.details.reason: boot_timeout` and the Simulator keeps booting, so a retry finds it further along.
7373
- Android: `boot --platform android --device <avd-name>` launches that emulator in GUI mode when needed.
7474
- Android: add `--headless` to launch without opening a GUI window.
7575
- Android: `shutdown --platform android --device <avd-name>` stops a running emulator.

0 commit comments

Comments
 (0)