windows_headless/timeouts.rs
1//! Single source of truth for every time budget in the crate. Bumping a
2//! timeout means editing here — not hunting `Duration::from_secs(…)`
3//! literals across modules (they were spread over seven files, and the
4//! user-facing messages duplicated the numbers a second time: "up to 10
5//! min", "after 50 s", "waiting 60 s …").
6//!
7//! Shape rule: one const per concept, typed for its call site — `Duration`
8//! where the API takes one, `u64` seconds where it counts seconds (clap
9//! defaults, retry loops). Messages *format* the const instead of restating
10//! the number, so prose can never drift from the value.
11//!
12//! Every value is the one the code used before this module existed: this is
13//! a re-homing, not a retune. Tiny fixture durations inside unit tests stay
14//! with those tests (they bound error paths, not real waits); the canary and
15//! the example keep their own explicitly generous budgets below rather than
16//! silently inheriting production ones.
17
18use std::time::Duration;
19
20// ---------------------------------------------------------------------------
21// Outer SSH deadlines — the two a user actually waits on
22// ---------------------------------------------------------------------------
23
24/// `run`: boot → first SSH banner. The printed "up to N min" line is
25/// derived from this, so the message can't lie about the real budget.
26/// (`install` uses [`DEFAULT_INSTALL_SSH_DEADLINE_SECS`] instead.)
27pub const BOOT_SSH_DEADLINE: Duration = Duration::from_secs(10 * 60);
28
29/// Default for `--install-timeout-secs` (`install`, `run --win-iso`):
30/// how long a *still-progressing* unattended install may take to report
31/// ready. Generous on purpose — a slow host applies the image for hours,
32/// and a parked guest is caught separately by [`IDLE_BAIL_SECS`].
33pub const DEFAULT_INSTALL_SSH_DEADLINE_SECS: u64 = 6 * 60 * 60;
34
35/// Default for `--screenshots-every-secs`: the install time-lapse and
36/// `run`'s boot time-lapse cadence (also what `run --win-iso` uses for
37/// its install-chain time-lapse, which has no flag of its own).
38/// Disk PNG writes; 0 disables saving (classification still runs on its
39/// own cadence below).
40pub const DEFAULT_SCREENSHOTS_EVERY_SECS: u64 = 60;
41
42/// Default for `--classify-every-secs`: the in-memory screen-classification
43/// cadence (screendump + nearest-exemplar match, no disk write). Faster than the disk
44/// time-lapse so phase changes and error screens surface in the log
45/// within seconds while PNGs stay minute-scale. 0 disables it.
46pub const DEFAULT_CLASSIFY_EVERY_SECS: u64 = 10;
47
48/// Internal snapshot tag for the landed state (`qemu-img snapshot -c`).
49pub const GOLDEN_TAG: &str = "golden";
50
51/// Bound for a live migration-to-file (`snapshot --live`): 4 GB of guest
52/// RAM over loopback lands in seconds; this only ever fires on a wedged
53/// migration, never on a slow-but-moving one.
54pub const MIGRATION_BUDGET_SECS: u64 = 600;
55/// Poll cadence while a live migration runs.
56pub const MIGRATION_POLL_SECS: u64 = 2;
57
58/// Bound for a clean guest shutdown ahead of a golden snapshot: the guest
59/// gets this long to halt after `Stop-Computer` before the snapshot path
60/// gives up and reports instead of capturing a dirty disk.
61pub const GUEST_SHUTDOWN_SECS: u64 = 120;
62
63// ---------------------------------------------------------------------------
64// Per-poll SSH mechanics (the inner loops under those deadlines)
65// ---------------------------------------------------------------------------
66
67/// Budget for reading the `SSH-2.0-…` banner once TCP connects. Bounds the
68/// banner wait so a slirp half-open forward (accepts, then stalls) degrades
69/// to another poll instead of a hang.
70pub const SSH_BANNER_READ_BUDGET: Duration = Duration::from_secs(10);
71
72/// Budget for the TCP connect of a single banner probe (`banner_once`).
73/// Separate from [`SSH_BANNER_READ_BUDGET`]: connect and read stall for
74/// different reasons, and shortening one shouldn't silently shorten the other.
75pub const SSH_BANNER_CONNECT_BUDGET: Duration = Duration::from_secs(10);
76
77/// Sleep between SSH polls. Deliberately short: at 15 s users stared at a
78/// ready desktop while the CLI still said "booting" (observed live), and a
79/// poll is one TCP SYN — latency is what the user feels.
80pub const SSH_POLL_INTERVAL: Duration = Duration::from_secs(5);
81
82/// Hard budget for one guest-readiness probe (`probe_setup_done`). A
83/// stalled handshake mid-reboot must surface as "not yet", never hang the
84/// wait loop.
85pub const READINESS_PROBE_BUDGET: Duration = Duration::from_secs(60);
86
87/// Sleep between readiness probes (each probe is a full login + PowerShell
88/// cold start — no need to hammer).
89pub const READINESS_PROBE_INTERVAL: Duration = Duration::from_secs(10);
90
91/// Bound on one exec-channel drain. PowerShell cold-start on a fresh boot
92/// can stall tens of seconds; beyond this the channel is hung, and the
93/// error must be loud instead of burning the caller's whole budget silently.
94pub const SSH_EXEC_DRAIN: Duration = Duration::from_secs(50);
95
96/// Budget for each half of a guest password rotation: the `net user` exec
97/// and the verifying re-login. A stalled handshake must surface as an error,
98/// never hang the caller.
99pub const SSH_PASSWORD_ROTATION_BUDGET: Duration = Duration::from_secs(60);
100
101// ---------------------------------------------------------------------------
102// QEMU lifecycle
103// ---------------------------------------------------------------------------
104
105/// Grace after spawn before checking whether QEMU rejected its argv — a
106/// healthy boot is still starting here, so a live child means success and
107/// only an *exited* child fails fast (instead of polling SSH for minutes
108/// against a dead process).
109pub const SPAWN_GUARD: Duration = Duration::from_secs(3);
110
111/// Budget for the first successful monitor (HMP) handshake after spawn.
112/// Never fails fast on a live-but-slow QEMU: an early bail left slow hvf
113/// starters to `kill_on_drop`.
114pub const MONITOR_CONNECT_SECS: u64 = 60;
115
116/// Sleep between monitor connect retries — and the amount `connect_retry`
117/// adds to its own accounting, so the reported elapsed time stays honest.
118pub const MONITOR_RETRY_STEP_SECS: u64 = 2;
119
120/// How long to wait for the monitor socket *file* to appear after spawn
121/// (bind = QEMU starting).
122pub const MONITOR_SOCKET_WAIT_SECS: u64 = 30;
123
124/// Poll interval while waiting for the monitor socket file.
125pub const MONITOR_SOCKET_POLL_SECS: u64 = 1;
126
127/// How many times `stop` re-probes QMP after `quit` before declaring the
128/// VM wedged, and the sleep between probes (20 × 500 ms ≈ 10 s total).
129pub const QMP_QUIT_POLL_COUNT: u64 = 20;
130pub const QMP_QUIT_POLL_INTERVAL: Duration = Duration::from_millis(500);
131
132// ---------------------------------------------------------------------------
133// Watchdogs and narration
134// ---------------------------------------------------------------------------
135
136/// Stall watchdog: warn when block I/O has been completely frozen this long
137/// with no SSH banner (a working boot never flatlines — local NVMe only,
138/// no network boot). Warn-once, then [`IDLE_BAIL_SECS`] bails with evidence.
139pub const IDLE_WARN_SECS: u64 = 60;
140
141/// Stall watchdog bail: frozen disk I/O + no banner this long = parked at
142/// an input screen (recovery, EULA). Fails fast with evidence instead of
143/// polling SSH against a guest that has stopped moving — this is what lets
144/// [`DEFAULT_INSTALL_SSH_DEADLINE_SECS`] stay generous.
145pub const IDLE_BAIL_SECS: u64 = 120;
146
147/// Default narration cadence in seconds: the "... still waiting for SSH
148/// (Ns elapsed)" pulse. Owned by `SamplingProfile` like the other
149/// cadences; explicit flags never touch it (it names log rhythm, not a
150/// wait budget).
151pub const DEFAULT_BOOT_NARRATION_SECS: u64 = 30;
152
153/// Cadence of the "still installing: … — Ns elapsed, N reboot(s) so far"
154/// heartbeat during the readiness wait. Probes run every
155/// [`READINESS_PROBE_INTERVAL`]; printing on every one buried the log, so
156/// progress is narrated on change plus this slower pulse (proof of life,
157/// not event spam).
158pub const PROGRESS_HEARTBEAT: Duration = Duration::from_secs(60);
159
160/// Grace the runtime gives leaked blocking threads at process exit before
161/// abandoning them (a pending `stdin.read()` in the shell loop otherwise
162/// parks a pool thread forever and the CLI never exits).
163pub const RUNTIME_EXIT_GRACE: Duration = Duration::from_secs(1);
164
165// ---------------------------------------------------------------------------
166// Tests and examples — intentionally their own budgets
167// ---------------------------------------------------------------------------
168
169/// Weekly canary: latest retail Windows ISO end-to-end, bounded by one deadline used
170/// both as the outer `timeout` and the inner `wait_for_ssh` budget.
171pub const CANARY_SSH_DEADLINE: Duration = Duration::from_secs(25 * 60);
172
173/// `examples/boot.rs`: first-setup wait on a cold, unproven image.
174pub const EXAMPLE_SSH_DEADLINE: Duration = Duration::from_secs(900);
175
176/// `examples/boot.rs`: ACPI-less graceful shutdown budget.
177pub const EXAMPLE_SHUTDOWN_GRACE: Duration = Duration::from_secs(90);