Skip to main content

microvms_app/control/
microvm.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Launch, the launch wait, the lifecycle calls, and the proxy token (TRAP-5, TRAP-8).
3//!
4//! # TRAP-5: the payload ceiling is 4096 bytes, inclusive
5//!
6//! [`RunHookPayload`] cannot hold an over-ceiling value, so the check happens when the
7//! payload is *made* rather than when the request is sent — which means every path into
8//! `RunMicrovm` is closed by one guard instead of each needing its own.
9//!
10//! The number is worth stating precisely because the project got it wrong in the dangerous
11//! direction. `docs/STRATEGY.md` and `docs/TRUST.md` claimed 16 KB — and so does the
12//! service model's own **documentation** string for `runHookPayload`, which reads "Maximum:
13//! 16,384 bytes" while its shape `RunMicrovmRequestRunHookPayloadString` says `max: 4096`.
14//! So a reader who checks the docs rather than the shape reproduces the bug. 4x wrong in
15//! the direction that tells a caller four times as much secret material fits as actually
16//! does. Bracketed 2026-08-07 by calling `RunMicrovm` with a deliberately bogus
17//! `imageIdentifier` so nothing could be created or billed: **4096 passes, 4097 fails.**
18//!
19//! Measured in UTF-8 **bytes**, not characters. A payload counted by character length
20//! passes while the same value with one multi-byte character in it does not.
21//!
22//! # TRAP-5's other half: why the payload at all
23//!
24//! It is the only per-VM secret channel the platform offers, and it is what keeps the agent
25//! token out of the shared image snapshot. That is safe because the platform forwards no
26//! external traffic until the run hook returns 200, so a per-VM secret delivered at launch
27//! wins the first-writer race through the endpoint.
28//!
29//! The daemon reads it one JSON parse deeper than expected: the platform **wraps** the
30//! string, so the hook body is `{"runHookPayload": "{\"agent_token\": \"...\"}"}`.
31//!
32//! # TRAP-8: a terminal state before RUNNING
33//!
34//! A VM that reaches a terminal state before RUNNING died during startup, and for a
35//! hook-serving daemon that almost always means a lifecycle hook failed. Polling through it
36//! wastes minutes and then reports a connection error that hides the cause — and by then
37//! the VM is gone, so `stateReason` is the only evidence left. [`ControlPlane::wait_for_running`]
38//! fails fast and attaches both the state and the reason.
39//!
40//! # TRAP-11, again: the shell mint is a separate surface, not a sibling of exec's
41//!
42//! [`ControlPlane::mint_auth_token`] calls `CreateMicrovmAuthToken` and is what the exec
43//! path uses. [`ControlPlane::mint_shell_auth_token`] calls `CreateMicrovmShellAuthToken`
44//! and exists for exactly one caller: `microvm shell`, the interactive PTY (issue #69).
45//! Nothing on the exec or session path reaches it — the lifecycle test below drives
46//! everything exec can do and asserts zero shell calls were emitted. See [`super`].
47
48use super::transport::{Call, paths, send_with_retry};
49use super::{ControlPlane, RunMicrovmRequest, WaitOpts, ops, timed_out, token};
50use crate::error::{Error, ErrorKind};
51
52/// The proxy token's auth header, which is also the key in the `authToken` map.
53pub const PROXY_AUTH_HEADER: &str = "X-aws-proxy-auth";
54
55/// The proxy port header.
56///
57/// Sent on **every** endpoint request alongside the auth header: `X-aws-proxy-auth` without
58/// `X-aws-proxy-port` is rejected in a way that reads like a bad token, which is a long
59/// detour for a missing header.
60pub const PROXY_PORT_HEADER: &str = "X-aws-proxy-port";
61
62/// The service ceiling on a proxy token's life, in minutes.
63pub const MAX_TOKEN_MINUTES: u32 = 60;
64
65/// A `runHookPayload` that is known to fit the service ceiling.
66///
67/// The type is the guard: there is no way to build one over 4096 bytes, so no call site has
68/// to remember to check. See the module docs for the measurement and for why the service
69/// model's own documentation string disagrees with its shape.
70#[derive(Clone, Eq, PartialEq)]
71pub struct RunHookPayload(String);
72
73/// Prints the size and nothing else.
74///
75/// The payload is the platform's only per-VM secret channel — its whole purpose is to carry
76/// the agent token — so a derived `Debug` would print that token into every log line that
77/// formats one, and into every line that formats the [`RunMicrovmRequest`] holding it.
78/// [`RunMicrovmRequest`] keeps its derive: with this impl in place, the derived one prints
79/// `RunHookPayload(<N bytes>)` for that field, so the safety is inherited rather than
80/// restated. The length is kept because it is the number every TRAP-5 diagnosis needs.
81impl std::fmt::Debug for RunHookPayload {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        write!(f, "RunHookPayload(<{} bytes>)", self.0.len())
84    }
85}
86
87impl RunHookPayload {
88    /// The payload carrying an agent token, as the daemon expects to find it.
89    ///
90    /// `{"agent_token": "<token>"}` — and the platform wraps *this whole string* as the
91    /// value of `runHookPayload`, which is the extra parse the daemon does.
92    ///
93    /// Checked even though this function builds the JSON itself, because the token is
94    /// caller-supplied: someone passing a JWT or a signed blob rather than a bearer token is
95    /// exactly who this catches.
96    pub fn for_agent_token(agent_token: &str) -> Result<Self, Error> {
97        Self::for_launch(agent_token, &std::collections::HashMap::new())
98    }
99
100    /// The payload carrying an agent token and a launch environment.
101    ///
102    /// `{"agent_token": "<token>", "env": {...}}`, and the platform wraps that whole
103    /// string as the value of `runHookPayload` — the extra parse the daemon does.
104    /// The daemon applies `env` as the base environment of every subsequent exec,
105    /// under the per-request `env`.
106    ///
107    /// **This is where the 4096-byte refusal fires for a launch env.** The check is
108    /// on the serialized payload rather than on the map, because the ceiling is on
109    /// the string: JSON escaping, the key names, and the punctuation all count, so a
110    /// caller who measured their own values would measure the wrong thing. And it is
111    /// local rather than left to the service for the same reason
112    /// [`super::super::control::image`]'s `require_workdir` and
113    /// `require_matching_from` are local: the service's answer arrives as a
114    /// `ValidationException` on a member the caller did not know they were filling,
115    /// after the call, and botocore does not enforce it client-side at all (measured;
116    /// `docs/PLATFORM.md`). `env` is what makes this reachable in practice — one
117    /// bearer token has always fit with room to spare, and a map of credentials does
118    /// not.
119    ///
120    /// The env is omitted from the JSON entirely when it is empty, so a caller who
121    /// passes no launch env produces byte-for-byte the payload
122    /// [`RunHookPayload::for_agent_token`] always produced. A pinned daemon that has
123    /// never heard of `env` therefore sees no change, and the two constructors do not
124    /// have two different budgets.
125    pub fn for_launch(
126        agent_token: &str,
127        env: &std::collections::HashMap<String, String>,
128    ) -> Result<Self, Error> {
129        Self::for_launch_with_identity(agent_token, env, None)
130    }
131
132    /// [`RunHookPayload::for_launch`], optionally carrying the tunnel identity material.
133    ///
134    /// The two identity fields are the VM's own seed and the launching host's public key,
135    /// under the keys `protocol::identity` declares — the daemon pins one and derives from
136    /// the other, which is what makes `microvm tunnel --verify-identity` able to prove the
137    /// far end without trusting the endpoint proxy. `None` produces byte-for-byte the
138    /// payload the other constructors always built, so a caller who never asks for identity
139    /// cannot be affected by the field existing (the same compatibility rule `env` follows).
140    ///
141    /// The material costs [`protocol::identity::IDENTITY_PAYLOAD_BYTES`] of the shared
142    /// 4096-byte budget — two 44-character base64 values plus JSON framing — and the ceiling
143    /// check below covers it like everything else in the payload.
144    pub fn for_launch_with_identity(
145        agent_token: &str,
146        env: &std::collections::HashMap<String, String>,
147        identity: Option<&crate::identity::LaunchIdentity>,
148    ) -> Result<Self, Error> {
149        let mut payload = if env.is_empty() {
150            serde_json::json!({ "agent_token": agent_token })
151        } else {
152            serde_json::json!({ "agent_token": agent_token, "env": env })
153        };
154        if let Some(identity) = identity {
155            let object = payload
156                .as_object_mut()
157                .expect("the payload is built as an object two lines up");
158            object.insert(
159                protocol::identity::SEED_KEY.to_string(),
160                serde_json::Value::String(identity.seed_field()),
161            );
162            object.insert(
163                protocol::identity::HOST_PUBLIC_KEY_KEY.to_string(),
164                serde_json::Value::String(identity.host_public_field()),
165            );
166        }
167        let text = serde_json::to_string(&payload).map_err(|error| {
168            Error::new(
169                ErrorKind::Unexpected,
170                format!("could not serialize the run-hook payload: {error}"),
171            )
172        })?;
173        // The length is read before the value moves into `new`, so the refusal below can
174        // account for it without rebuilding the payload.
175        let total = text.len();
176        Self::new(text).map_err(|error| {
177            if env.is_empty() {
178                return error;
179            }
180            // The generic message names the byte count and the ceiling, which is the
181            // whole diagnosis for a token. For a launch env it is half of one: the
182            // caller wants to know how much of the budget their env is, because the
183            // fix is to move a value out of it rather than to shorten the token.
184            Error::invalid_arg(format!(
185                "{error} The launch env contributed {} of those bytes across {} variable(s); \
186                 the payload's other {} are the token and the JSON framing. A launch env is \
187                 for small values a workload needs at startup — move credential-scale \
188                 material to PUT /v1/fs/file after bootstrap, or to a role the workload \
189                 assumes.",
190                env_contribution(agent_token, env),
191                env.len(),
192                total.saturating_sub(env_contribution(agent_token, env)),
193            ))
194        })
195    }
196
197    /// An arbitrary payload, if it fits.
198    ///
199    /// Public because the run-hook body is the caller's channel and this crate does not own
200    /// what goes in it; the ceiling is the only thing it enforces.
201    pub fn new(payload: impl Into<String>) -> Result<Self, Error> {
202        let payload = payload.into();
203        // Bytes, not characters: the ceiling is on the serialized string, so a payload
204        // measured by character count passes while the same value with one multi-byte
205        // character in it does not.
206        let size = payload.len();
207        if size <= crate::constants::MAX_RUN_HOOK_PAYLOAD_BYTES {
208            return Ok(Self(payload));
209        }
210        Err(Error::invalid_arg(format!(
211            "runHookPayload is {size} bytes, over the ceiling of {} (service model {}, measured \
212             inclusive 2026-08-07: 4096 passes and 4097 fails). This is the only per-VM secret \
213             channel the platform offers — one bearer token fits, a cloud credential set does \
214             not. Note that docs/STRATEGY.md, docs/TRUST.md, and the service model's own \
215             documentation string for this member all claim 16 KB, which is wrong by 4x in the \
216             dangerous direction; the shape RunMicrovmRequestRunHookPayloadString is the \
217             authority and it states max 4096.",
218            crate::constants::MAX_RUN_HOOK_PAYLOAD_BYTES,
219            crate::constants::MODEL_API_VERSION,
220        )))
221    }
222
223    /// The payload's bytes as the wire will carry them.
224    pub fn as_str(&self) -> &str {
225        &self.0
226    }
227
228    /// The size in bytes, which is what the ceiling measures.
229    pub fn len(&self) -> usize {
230        self.0.len()
231    }
232
233    /// Whether the payload is empty. The model's minimum is 0, so an empty payload is legal.
234    pub fn is_empty(&self) -> bool {
235        self.0.is_empty()
236    }
237}
238
239/// How many of a launch payload's bytes the env accounts for.
240///
241/// Measured by difference against the token-only payload rather than by summing key and
242/// value lengths, because the ceiling is on the *serialized* string: the `"env":{}`
243/// wrapper, the quotes, the commas, and every backslash JSON escaping adds all count
244/// against the budget, and a figure that summed the raw values would understate exactly
245/// the payloads that are near the limit.
246///
247/// Only ever called on the refusal path, where the payload has already been built once.
248fn env_contribution(agent_token: &str, env: &std::collections::HashMap<String, String>) -> usize {
249    let with = serde_json::json!({ "agent_token": agent_token, "env": env }).to_string();
250    let without = serde_json::json!({ "agent_token": agent_token }).to_string();
251    with.len().saturating_sub(without.len())
252}
253
254/// A launched MicroVM, as the service last described it.
255#[derive(Clone, Debug, Eq, PartialEq)]
256pub struct Microvm {
257    pub id: String,
258    pub state: String,
259    /// The proxy endpoint. Present from the launch response onward.
260    pub endpoint: String,
261    pub image_arn: String,
262    pub image_version: String,
263    /// Why the VM is in this state, when the service said. The absence is information —
264    /// TRAP-8's message distinguishes "no stateReason" from an empty one.
265    pub state_reason: Option<String>,
266    /// The idle policy the service reports the VM is running under.
267    ///
268    /// Carried rather than dropped because it is the platform's own account of the window
269    /// that will suspend and then terminate this VM, and it was measured coming back on a
270    /// real `GetMicrovm` — see [`ops::IdlePolicy`], which used to claim it does not. The
271    /// client's request is what *asked* for a window; this is what the service says it got,
272    /// and a caller diagnosing a VM that vanished earlier than expected needs the second.
273    ///
274    /// `Option` because the model does not mark the member required on either response
275    /// shape, so an absent one must parse rather than fail.
276    pub idle_policy: Option<ops::IdlePolicy>,
277    /// `maximumDurationInSeconds` as the service reports it.
278    pub maximum_duration_seconds: Option<u32>,
279    /// When the VM first started (`startedAt`). Absent on a launch reply.
280    pub started_at: Option<std::time::SystemTime>,
281    /// When the VM terminated (`terminatedAt`), once it has.
282    pub terminated_at: Option<std::time::SystemTime>,
283}
284
285/// Epoch seconds from a `Timestamp` member, or `None` for an absent or unrepresentable one.
286///
287/// The checked conversion refuses a negative, NaN, infinite or too-large figure. The unchecked
288/// one, behind a filter for the first three, panicked on a figure past what a `Duration` holds
289/// before `checked_add` could refuse it (#335).
290fn epoch(seconds: Option<f64>) -> Option<std::time::SystemTime> {
291    let since = std::time::Duration::try_from_secs_f64(seconds?).ok()?;
292    std::time::UNIX_EPOCH.checked_add(since)
293}
294
295/// The optional filters `ListMicrovms` accepts.
296#[derive(Clone, Debug, Default, Eq, PartialEq)]
297pub struct MicrovmFilter {
298    /// Only VMs running this image (ARN or ID).
299    pub image_identifier: Option<String>,
300    /// Only VMs running this image version.
301    pub image_version: Option<String>,
302}
303
304impl Microvm {
305    /// The reason the service gave, or a phrase saying it gave none.
306    ///
307    /// A phrase rather than an empty string, because "the service said nothing" and "the
308    /// service said nothing useful" are different diagnoses and a caller reading a message
309    /// has to be able to tell them apart.
310    fn reason(&self) -> &str {
311        self.state_reason.as_deref().unwrap_or("no stateReason")
312    }
313}
314
315impl From<ops::MicrovmResponseWire> for Microvm {
316    fn from(wire: ops::MicrovmResponseWire) -> Self {
317        Self {
318            id: wire.microvm_id,
319            state: wire.state,
320            endpoint: wire.endpoint,
321            image_arn: wire.image_arn,
322            image_version: wire.image_version,
323            state_reason: wire.state_reason,
324            idle_policy: wire.idle_policy,
325            maximum_duration_seconds: wire.maximum_duration_in_seconds,
326            started_at: epoch(wire.started_at),
327            terminated_at: epoch(wire.terminated_at),
328        }
329    }
330}
331
332/// The minted proxy token, as a header map (TRAP-7).
333///
334/// # Why a map rather than a string
335///
336/// Because that is what the service answers: `authToken` is a `TokenParts` map, and the
337/// value at `X-aws-proxy-auth` is the header value. A client that treated the response as a
338/// string would send a stringified map and get a rejection that reads like a bad token.
339///
340/// [`ProxyToken::headers`] always emits **both** headers, which is the other half of the
341/// same trap: `X-aws-proxy-auth` without `X-aws-proxy-port` is rejected the same
342/// indistinguishable way.
343#[derive(Clone, Eq, PartialEq)]
344pub struct ProxyToken {
345    value: String,
346    port: u16,
347}
348
349/// Names the headers and the port, never the token value.
350///
351/// A minted proxy token is a credential, and a derived `Debug` would print it into every
352/// log line that formats a [`ProxyToken`], a [`RunMicrovmRequest`] holding one, or an error
353/// chain containing either. The sibling type in [`crate::session::proxy`] made this choice
354/// first; this is the same rule on the control-plane side of the conversion.
355impl std::fmt::Debug for ProxyToken {
356    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
357        f.debug_struct("ProxyToken")
358            .field("headers", &[PROXY_AUTH_HEADER, PROXY_PORT_HEADER])
359            .field("port", &self.port)
360            .finish()
361    }
362}
363
364impl ProxyToken {
365    /// Reads the token out of the service's `authToken` map.
366    ///
367    /// A missing `X-aws-proxy-auth` key is [`ErrorKind::Retryable`] via
368    /// [`crate::error::WireKind::AuthTokenMint`]: minting is inside the request retry path
369    /// (TRAP-9), and a throttle at minute 30 of a long run must not kill a trial that is
370    /// otherwise healthy.
371    pub fn from_map(
372        map: &std::collections::BTreeMap<String, String>,
373        port: u16,
374    ) -> Result<Self, Error> {
375        Self::from_operation_map("CreateMicrovmAuthToken", map, port)
376    }
377
378    /// [`ProxyToken::from_map`], naming the operation whose response the map came from.
379    ///
380    /// The shell mint reads the same `TokenParts` shape out of
381    /// `CreateMicrovmShellAuthToken`, and a missing-key failure that blamed the ordinary
382    /// mint would send the reader to the wrong operation's response.
383    pub fn from_operation_map(
384        operation: &str,
385        map: &std::collections::BTreeMap<String, String>,
386        port: u16,
387    ) -> Result<Self, Error> {
388        let value = map.get(PROXY_AUTH_HEADER).ok_or_else(|| {
389            Error::wire(
390                crate::error::WireKind::AuthTokenMint,
391                format!(
392                    "the {operation} response carried no {PROXY_AUTH_HEADER} key. The \
393                     authToken member is a map, not a string, and that key is the header value \
394                     (keys present: {:?}).",
395                    map.keys().collect::<Vec<_>>()
396                ),
397            )
398        })?;
399        Ok(Self {
400            value: value.clone(),
401            port,
402        })
403    }
404
405    /// Both headers every endpoint request needs.
406    ///
407    /// Both, always. See the type docs: sending only the auth header is rejected in a way
408    /// that reads like a bad token.
409    pub fn headers(&self) -> [(&'static str, String); 2] {
410        [
411            (PROXY_AUTH_HEADER, self.value.clone()),
412            (PROXY_PORT_HEADER, self.port.to_string()),
413        ]
414    }
415
416    /// The raw token value.
417    pub fn value(&self) -> &str {
418        &self.value
419    }
420
421    /// The port the token was minted for.
422    pub fn port(&self) -> u16 {
423        self.port
424    }
425}
426
427impl ControlPlane {
428    /// Launches a MicroVM.
429    ///
430    /// TRAP-5 is already closed by [`RunHookPayload`]'s type; the duration range is checked
431    /// here, and the connectors are derived from intents (TRAP-4). The `clientToken` is
432    /// minted from a label (TRAP-1).
433    pub async fn run_microvm(&self, request: RunMicrovmRequest) -> Result<Microvm, Error> {
434        let (ingress, egress) = self.launch_connectors(&request)?;
435
436        let wire = ops::RunMicrovmWire {
437            image_identifier: request.image_identifier.clone(),
438            image_version: request.image_version.clone(),
439            execution_role_arn: request.execution_role_arn.clone(),
440            ingress_network_connectors: ingress,
441            // Absent rather than empty: omitting egress omits the connector from the request.
442            // Measured 2026-09-11 and 2026-09-12 the platform still gave such a VM outbound
443            // network (`docs/PLATFORM.md`, "A VM launched without the egress connector still
444            // has outbound network").
445            egress_network_connectors: (!egress.is_empty()).then_some(egress),
446            idle_policy: ops::IdlePolicy {
447                max_idle_duration_seconds: request.max_idle_sec,
448                suspended_duration_seconds: request.suspended_sec,
449                auto_resume_enabled: request.auto_resume,
450            },
451            maximum_duration_in_seconds: request.max_duration_sec,
452            run_hook_payload: request.run_hook_payload.as_str().to_string(),
453            logging: request.logging.clone(),
454            client_token: match request.client_token {
455                Some(token) => token,
456                None => token::run_token_with(
457                    request
458                        .token_scope
459                        .as_deref()
460                        .unwrap_or(&request.image_identifier),
461                    self.entropy(),
462                )?,
463            },
464        };
465
466        let call = Call::post_json("RunMicrovm", paths::microvms(), &wire)?;
467        let reply = send_with_retry(self.transport(), call).await?;
468        let launched: ops::MicrovmResponseWire = reply.json("RunMicrovm")?;
469        Ok(launched.into())
470    }
471
472    /// Every local refusal [`Self::run_microvm`] makes, then the ingress and egress connector
473    /// ARNs the launch carries. Local; zero calls.
474    ///
475    /// Separate so [`crate::sandbox::Sandbox::run`] can ask it before it resolves an image
476    /// name: that listing is a call, and a launch this client refuses anyway should cost none.
477    pub(crate) fn launch_connectors(
478        &self,
479        request: &RunMicrovmRequest,
480    ) -> Result<(Vec<String>, Vec<String>), Error> {
481        if let Some(token) = request.client_token.as_deref() {
482            super::token::require_run_token(token)?;
483        }
484        super::require_valid_identifier("imageIdentifier", &request.image_identifier)?;
485        super::require_duration_in_range(request.max_duration_sec)?;
486        // `min: 60` on `IdlePolicyMaxIdleDurationSecondsInteger`. There used to be no guard for
487        // this on the grounds that botocore enforces `min` locally — true of the deleted Python
488        // client and never of this one. See `super::require_idle_duration`.
489        super::require_idle_duration(request.max_idle_sec)?;
490        // Checked before the wire because a pinned launch is what a rollback does, and a
491        // `ValidationException` on the member rather than on the version is the least useful
492        // failure available at that moment. See `super::require_valid_version`.
493        if let Some(version) = request.image_version.as_deref() {
494            super::require_valid_version("imageVersion", version)?;
495        }
496        // `RoleArn` — optional in the model, and every real launch has one. A malformed value
497        // here is a `ValidationException` on a member the caller may not know is being filled
498        // from their infra config, so the refusal names it.
499        if let Some(role) = request.execution_role_arn.as_deref() {
500            super::require_valid_role_arn("executionRoleArn", role)?;
501        }
502        if let Some(ops::Logging::CloudWatch { cloud_watch }) = &request.logging {
503            let Some(group) = cloud_watch.log_group.as_deref() else {
504                return Err(Error::invalid_arg(
505                    "per-VM CloudWatch logging needs a logGroup; pass a group name or disable logging",
506                ));
507            };
508            super::require_valid_log_group(group)?;
509            if let Some(stream) = cloud_watch.log_stream.as_deref() {
510                super::require_valid_log_stream(stream)?;
511            }
512        }
513
514        // `ALL_INGRESS` cannot be combined with any other ingress connector, and the
515        // platform says so only at token-mint time: `RunMicrovm` accepts the invalid
516        // set, the VM reaches RUNNING, and it bills until something asks for a shell
517        // token (`docs/PLATFORM.md`, measured 2026-08-15). Refused here instead, before
518        // anything launches or bills. The pair that works is `[HTTP_INGRESS,
519        // SHELL_INGRESS]`.
520        let has_all_ingress = request
521            .connectors
522            .contains(&super::ConnectorIntent::AllIngress);
523        let has_finer_ingress = request.connectors.iter().any(|intent| {
524            matches!(
525                intent,
526                super::ConnectorIntent::HttpIngress | super::ConnectorIntent::ShellIngress
527            )
528        });
529        if has_all_ingress && has_finer_ingress {
530            return Err(Error::invalid_arg(
531                "ALL_INGRESS cannot be combined with other ingress connectors; use \
532                 HTTP_INGRESS and/or SHELL_INGRESS instead. The platform accepts the \
533                 combination at launch and rejects it only when a shell token is minted, \
534                 after the VM has launched and billed — so it is refused here.",
535            ));
536        }
537
538        // The other half of the same measured constraint: the pair that launches and
539        // mints a shell token is `[HTTP_INGRESS, SHELL_INGRESS]`. A shell-only ingress
540        // set has no measured success path, and the platform accepts an invalid set at
541        // launch — so it too is refused here, before anything launches or bills.
542        let has_shell_ingress = request
543            .connectors
544            .contains(&super::ConnectorIntent::ShellIngress);
545        let has_http_ingress = request
546            .connectors
547            .contains(&super::ConnectorIntent::HttpIngress);
548        if has_shell_ingress && !has_http_ingress {
549            return Err(Error::invalid_arg(
550                "SHELL_INGRESS was requested without HTTP_INGRESS. The measured pair that \
551                 launches and mints a shell token is [HTTP_INGRESS, SHELL_INGRESS] \
552                 (docs/PLATFORM.md); the platform accepts an invalid ingress set at launch \
553                 and the VM runs and bills before any failure appears — so it is refused \
554                 here.",
555            ));
556        }
557
558        // Ingress and egress go in separate members, so they are split by intent rather
559        // than concatenated into one list.
560        let ingress: Vec<String> = request
561            .connectors
562            .iter()
563            .filter(|intent| {
564                matches!(
565                    intent,
566                    super::ConnectorIntent::AllIngress
567                        | super::ConnectorIntent::HttpIngress
568                        | super::ConnectorIntent::ShellIngress
569                )
570            })
571            .map(|intent| intent.arn(&self.region))
572            .collect();
573        let mut egress: Vec<String> = request
574            .connectors
575            .iter()
576            .filter(|intent| matches!(intent, super::ConnectorIntent::Egress))
577            .map(|intent| intent.arn(&self.region))
578            .collect();
579
580        // The same refusals, in the same order and words, as `egress_posture_for`, which
581        // `Sandbox::run` asks first; these stand for a caller of `run_microvm` directly.
582        if !egress.is_empty() && !request.egress_network_connectors.is_empty() {
583            return Err(super::connector::egress_with_connectors());
584        }
585        for arn in &request.egress_network_connectors {
586            super::connector::require_egress_connector_arn(arn, &self.region)?;
587            egress.push(arn.clone());
588        }
589
590        // Each request member is a NetworkConnectorList with its own limit.
591        for (member, count) in [
592            ("ingressNetworkConnectors", ingress.len()),
593            ("egressNetworkConnectors", egress.len()),
594        ] {
595            if let Some(refusal) = super::connector::over_connector_ceiling(member, count) {
596                return Err(refusal);
597            }
598        }
599
600        Ok((ingress, egress))
601    }
602
603    /// Polls to RUNNING, failing fast on a terminal state (TRAP-8).
604    ///
605    /// The fast failure is the whole value. A VM that reaches a terminal state before
606    /// RUNNING died during startup; polling through it wastes minutes and then reports a
607    /// connection error that hides the cause, and by then the VM is gone so `stateReason` is
608    /// the only evidence left.
609    pub async fn wait_for_running(&self, id: &str, opts: WaitOpts) -> Result<Microvm, Error> {
610        self.wait_for_state(id, &["RUNNING"], &crate::constants::TERMINAL_STATES, opts)
611            .await
612    }
613
614    /// Polls a launch that may have **adopted** an existing VM through its client token.
615    ///
616    /// A repeated `clientToken` makes `RunMicrovm` answer with the VM the first attempt
617    /// launched, and that VM may have idle-suspended since (#195). [`Self::wait_for_running`]
618    /// would read that SUSPENDED as a death during startup (TRAP-8) and report LaunchDied
619    /// for a healthy VM. Here SUSPENDING is waited through, SUSPENDED is resumed once, and
620    /// only the dead states fail fast. A genuine startup death still ends in a dead state,
621    /// so it still fails with `stateReason`.
622    pub async fn wait_for_launch(&self, id: &str, opts: WaitOpts) -> Result<Microvm, Error> {
623        let started = self.clock().elapsed();
624        let mut resumed = false;
625        loop {
626            let got = self.get_microvm(id).await?;
627            match got.state.as_str() {
628                "RUNNING" => return Ok(got),
629                state if crate::constants::DEAD_STATES.contains(&state) => {
630                    return Err(self.reached_terminal_state(&got, &["RUNNING"]));
631                }
632                // `GetMicrovm` is eventually consistent, so SUSPENDED can still be read
633                // just after the resume was accepted; one resume is enough.
634                "SUSPENDED" if !resumed => {
635                    self.resume(id).await?;
636                    resumed = true;
637                    continue;
638                }
639                _ => {}
640            }
641
642            let elapsed = self.clock().elapsed().saturating_sub(started);
643            if elapsed >= opts.timeout {
644                return Err(timed_out(
645                    &format!(
646                        "microvm {id} never reached [\"RUNNING\"] (last state {})",
647                        got.state
648                    ),
649                    elapsed,
650                ));
651            }
652            self.clock().sleep(opts.poll_interval).await;
653        }
654    }
655
656    /// Polls until the VM reaches one of `wanted`, failing fast on any of `fail_on`.
657    ///
658    /// # Why `fail_on` is a parameter rather than always the terminal set
659    ///
660    /// Because `suspend` **wants** SUSPENDED and tolerates TERMINATED: a VM that dies while
661    /// suspending is a state to report, not an exception to raise out of the middle of a
662    /// teardown. The resume path passes the *dead* states instead, because a VM the idle
663    /// policy terminated during suspension never reaches RUNNING — and waiting only for
664    /// RUNNING there burns the full timeout and then reports "never reached RUNNING", a
665    /// timeout message hiding a cause the service had already stated.
666    pub async fn wait_for_state(
667        &self,
668        id: &str,
669        wanted: &[&str],
670        fail_on: &[&str],
671        opts: WaitOpts,
672    ) -> Result<Microvm, Error> {
673        let started = self.clock().elapsed();
674        loop {
675            let got = self.get_microvm(id).await?;
676
677            if wanted.contains(&got.state.as_str()) {
678                return Ok(got);
679            }
680            if fail_on.contains(&got.state.as_str()) {
681                return Err(self.reached_terminal_state(&got, wanted));
682            }
683
684            let elapsed = self.clock().elapsed().saturating_sub(started);
685            if elapsed >= opts.timeout {
686                return Err(timed_out(
687                    &format!(
688                        "microvm {id} never reached {wanted:?} (last state {})",
689                        got.state
690                    ),
691                    elapsed,
692                ));
693            }
694            self.clock().sleep(opts.poll_interval).await;
695        }
696    }
697
698    /// TRAP-8's error: the state **and** the reason, both in the message.
699    ///
700    /// Both because either alone is unactionable. The state says the VM is gone; the reason
701    /// says why, and it is the only evidence that survives the VM.
702    fn reached_terminal_state(&self, got: &Microvm, wanted: &[&str]) -> Error {
703        Error::new(
704            ErrorKind::LaunchDied,
705            format!(
706                "microvm {} reached {} before {wanted:?}: {}. A VM that reaches a terminal state \
707                 before RUNNING died during startup, and for a hook-serving daemon that almost \
708                 always means a lifecycle hook failed — the stateReason above is the only evidence \
709                 left, because the VM is gone (docs/PLATFORM.md, 'A MicroVM that dies during \
710                 startup reports a connection error that hides the cause').",
711                got.id,
712                got.state,
713                got.reason(),
714            ),
715        )
716    }
717
718    /// `GetMicrovm`.
719    ///
720    /// `microvmIdentifier` is a URI parameter, so an empty one collapses the path onto the
721    /// listing and asks a different question — see [`super::require_valid_identifier`]. This is
722    /// the read every wait loop makes, so the check also covers
723    /// [`Self::wait_for_state`] and everything built on it.
724    pub async fn get_microvm(&self, id: &str) -> Result<Microvm, Error> {
725        super::require_valid_identifier("microvmIdentifier", id)?;
726        let call = Call::get("GetMicrovm", paths::microvm(id));
727        let reply = send_with_retry(self.transport(), call).await?;
728        let got: ops::MicrovmResponseWire = reply.json("GetMicrovm")?;
729        Ok(got.into())
730    }
731
732    /// `ListMicrovms`, read to its last page.
733    ///
734    /// # Why every page
735    ///
736    /// `maxResults` caps at 50, so a fleet above 50 VMs read from one page is a
737    /// **confidently wrong** answer rather than a missing one: the caller gets a list that
738    /// looks complete, and a VM absent from it is a VM nothing here will terminate. That is
739    /// the same argument [`ops::ListImagesResponseWire`] makes for the image listing, and a
740    /// fleet listing is the one a teardown reads.
741    pub async fn list_microvms(&self) -> Result<Vec<ops::MicrovmItemWire>, Error> {
742        self.list_microvms_matching(&MicrovmFilter::default()).await
743    }
744
745    /// [`Self::list_microvms`], narrowed by the service's image and version filters.
746    pub async fn list_microvms_matching(
747        &self,
748        filter: &MicrovmFilter,
749    ) -> Result<Vec<ops::MicrovmItemWire>, Error> {
750        if let Some(image) = filter.image_identifier.as_deref() {
751            super::require_valid_identifier("imageIdentifier", image)?;
752        }
753        if let Some(version) = filter.image_version.as_deref() {
754            super::require_valid_version("imageVersion", version)?;
755        }
756        let mut items = Vec::new();
757        let mut next_token: Option<String> = None;
758        loop {
759            let call = Call::get(
760                "ListMicrovms",
761                paths::microvms_list_matching(
762                    filter.image_identifier.as_deref(),
763                    filter.image_version.as_deref(),
764                    next_token.as_deref(),
765                ),
766            );
767            let reply = send_with_retry(self.transport(), call).await?;
768            let page: ops::ListMicrovmsResponseWire = reply.json("ListMicrovms")?;
769            items.extend(page.items);
770            match page.next_token {
771                Some(token) => next_token = Some(token),
772                None => return Ok(items),
773            }
774        }
775    }
776
777    /// `SuspendMicrovm`. Freezes the VM; the caller waits for SUSPENDED.
778    pub async fn suspend(&self, id: &str) -> Result<(), Error> {
779        super::require_valid_identifier("microvmIdentifier", id)?;
780        let call = Call::post_empty("SuspendMicrovm", paths::suspend(id));
781        send_with_retry(self.transport(), call).await?;
782        Ok(())
783    }
784
785    /// `ResumeMicrovm`. Thaws the VM; no token re-delivery and no re-bootstrap.
786    pub async fn resume(&self, id: &str) -> Result<(), Error> {
787        super::require_valid_identifier("microvmIdentifier", id)?;
788        let call = Call::post_empty("ResumeMicrovm", paths::resume(id));
789        send_with_retry(self.transport(), call).await?;
790        Ok(())
791    }
792
793    /// `TerminateMicrovm`.
794    ///
795    /// The identifier check matters most on this one, and for a reason the others do not share:
796    /// the path is `DELETE /microvms/{microvmIdentifier}`, so an empty identifier is a `DELETE`
797    /// addressed at the **collection**. There is no operation on that path and the service
798    /// refuses it, but "send a delete at the fleet and rely on the service to refuse" is not a
799    /// thing to leave to the service.
800    pub async fn terminate(&self, id: &str) -> Result<(), Error> {
801        super::require_valid_identifier("microvmIdentifier", id)?;
802        let call = Call::delete("TerminateMicrovm", paths::microvm(id));
803        send_with_retry(self.transport(), call).await?;
804        Ok(())
805    }
806
807    /// Mints a proxy token for the agent port (TRAP-7, TRAP-9).
808    ///
809    /// `expirationInMinutes` is capped at [`MAX_TOKEN_MINUTES`] here rather than passed
810    /// through, because over-asking is rejected and the rejection reads like a bad request
811    /// rather than like a ceiling. The *refresh* interval — half the ceiling — belongs to
812    /// the endpoint client that holds the token (T-W2-5), not here.
813    ///
814    /// Scoped to the agent port alone, which is right for every request this client makes on
815    /// its own behalf and **wrong for a caller reaching another port on the same VM**. That
816    /// caller wants [`Self::mint_auth_token_for`]; see [`ops::PortSpecification`] for the
817    /// measurement that made the distinction necessary.
818    pub async fn mint_auth_token(&self, id: &str) -> Result<ProxyToken, Error> {
819        self.mint_auth_token_for(id, &[ops::PortSpecification::port(self.port())])
820            .await
821    }
822
823    /// Mints a proxy token scoped to `ports`, whatever they are.
824    ///
825    /// The agent port is **not** added for you. A caller naming ports is answering the
826    /// question the token asks, and silently widening the scope would hand back a
827    /// credential broader than the one requested — the opposite of the mistake this exists
828    /// to fix, and a worse one, since it is invisible. A caller that needs both the agent
829    /// port and a workload port names both, which is what [`ProxyAuth`] does.
830    ///
831    /// The returned token still records the agent port as its *default* header port, because
832    /// that is the port a request carries when nobody names one.
833    ///
834    /// [`ProxyAuth`]: crate::session::ProxyAuth
835    pub async fn mint_auth_token_for(
836        &self,
837        id: &str,
838        ports: &[ops::PortSpecification],
839    ) -> Result<ProxyToken, Error> {
840        super::require_valid_identifier("microvmIdentifier", id)?;
841        if ports.is_empty() {
842            // The model declares `min: 1` on `allowedPorts`, so an empty list is a
843            // round-trip that comes back as a validation error naming a member the caller
844            // did not know it was sending. Refused here, with the reason.
845            return Err(Error::new(
846                ErrorKind::Precondition,
847                "a proxy token must allow at least one port ('allowedPorts' declares min: 1). \
848                 Name the ports this token is for — the agent port plus any workload port \
849                 reached through the endpoint.",
850            ));
851        }
852        // `PortNumber` is `min: 1`, and the value that reaches here as a 0 is not a typo — it is
853        // a caller who took `ControlPlane::with_port(0)`'s default port, or who passed a
854        // `SocketAddr`'s port before binding. A token minted for port 0 authorizes nothing, and
855        // the proxy's refusal is `403 Access to port denied`, which reads like the token being
856        // wrong rather than its scope being empty (measured 2026-08-15, see
857        // `ops::PortSpecification`). Both ends of a range are checked, because `PortRange`'s two
858        // members name the same shape and a range starting at 0 is the same defect.
859        for spec in ports {
860            match spec {
861                ops::PortSpecification::One { port } => {
862                    super::require_valid_port("allowedPorts[].port", *port)?;
863                }
864                ops::PortSpecification::Range { range } => {
865                    super::require_valid_port("allowedPorts[].range.startPort", range.start_port)?;
866                    super::require_valid_port("allowedPorts[].range.endPort", range.end_port)?;
867                }
868                // `allPorts` names no number, so there is nothing to bound.
869                ops::PortSpecification::All { .. } => {}
870            }
871        }
872        let wire = ops::CreateAuthTokenWire {
873            expiration_in_minutes: MAX_TOKEN_MINUTES,
874            allowed_ports: ports.to_vec(),
875        };
876        let call = Call::post_json("CreateMicrovmAuthToken", paths::auth_token(id), &wire)?;
877        let reply = send_with_retry(self.transport(), call).await?;
878        let minted: ops::CreateAuthTokenResponseWire = reply.json("CreateMicrovmAuthToken")?;
879        ProxyToken::from_map(&minted.auth_token, self.port())
880    }
881
882    /// Mints a shell token: the credential `microvm shell` opens the PTY WebSocket with.
883    ///
884    /// `CreateMicrovmShellAuthToken`, which the VM must have been launched with
885    /// `SHELL_INGRESS` to answer — a VM launched without it gets a `ValidationException`
886    /// naming the connector, and `RunMicrovmRequest::with_shell` is how a launch requests
887    /// the measured pair. The request takes only the expiration; there is no
888    /// `allowedPorts`, because the shell is not a port (`docs/PLATFORM.md`, 2026-08-15).
889    ///
890    /// # This is `microvm shell`'s method, not exec's (TRAP-11)
891    ///
892    /// One interactive session is not programmatic exec — no exec ids, no idempotency,
893    /// no separated stdout/stderr, no exit codes — so nothing on the exec or session
894    /// path calls this, and the lifecycle test asserts that absence. A caller holding
895    /// the returned token has a real root PTY on the VM; treat it like the credential
896    /// it is.
897    ///
898    /// The expiration is pinned at [`MAX_TOKEN_MINUTES`] for the reason
899    /// [`Self::mint_auth_token`] gives: over-asking is rejected in a way that reads like
900    /// a bad request rather than like a ceiling. A shell session held past the ceiling
901    /// keeps its already-open WebSocket — the expiry gates the *handshake*, not an
902    /// established session.
903    pub async fn mint_shell_auth_token(&self, id: &str) -> Result<ProxyToken, Error> {
904        super::require_valid_identifier("microvmIdentifier", id)?;
905        let wire = ops::CreateShellAuthTokenWire {
906            expiration_in_minutes: MAX_TOKEN_MINUTES,
907        };
908        let call = Call::post_json(
909            "CreateMicrovmShellAuthToken",
910            paths::shell_auth_token(id),
911            &wire,
912        )?;
913        let reply = send_with_retry(self.transport(), call).await?;
914        let minted: ops::CreateShellAuthTokenResponseWire =
915            reply.json("CreateMicrovmShellAuthToken")?;
916        ProxyToken::from_operation_map(
917            "CreateMicrovmShellAuthToken",
918            &minted.auth_token,
919            self.port(),
920        )
921    }
922}
923
924/// The states a suspend may end in.
925///
926/// TERMINATED is *wanted* rather than a failure: a VM that dies while suspending is a state
927/// to report, not an exception to raise out of the middle of a teardown.
928pub const SUSPEND_WANTED: [&str; 2] = ["SUSPENDED", "TERMINATED"];
929
930#[cfg(test)]
931mod tests {
932    use std::sync::Arc;
933
934    use super::super::fake::{self as fake, Answer, FakeControlPlane, TestClock};
935    use super::*;
936    use crate::region::Region;
937
938    /// A suspend waits for these, so each has to be a state the service can report. The rest
939    /// of the polling loops' sets are checked beside the enum in `constants`; this one lives
940    /// here, so its check does too.
941    #[test]
942    fn every_state_a_suspend_waits_for_is_in_the_models_enum() {
943        for state in SUSPEND_WANTED {
944            assert!(
945                crate::constants::MICROVM_STATES.contains(&state),
946                "SUSPEND_WANTED has {state}, which is not a MicrovmState: a suspend would wait \
947                 for a state the service can't report and time out"
948            );
949        }
950    }
951
952    fn planted() -> (ControlPlane, Arc<FakeControlPlane>, Arc<TestClock>) {
953        let fake = Arc::new(FakeControlPlane::new());
954        let clock = Arc::new(TestClock::new());
955        let plane = crate::testing::control_plane(fake.clone(), Region::UsEast1, clock.clone());
956        (plane, fake, clock)
957    }
958
959    /// **TRAP-5, the boundary.** 4096 bytes passes, 4097 is rejected — inclusive, as
960    /// measured 2026-08-07.
961    ///
962    /// **Falsification** — change the comparison to `<` and the 4096 case fails; change the
963    /// ceiling to 16384 (the number the docs and the model's own documentation string claim)
964    /// and the 4097 case fails.
965    #[test]
966    fn the_payload_ceiling_is_four_thousand_ninety_six_bytes_inclusive() {
967        let at_ceiling = "a".repeat(4096);
968        let payload = RunHookPayload::new(at_ceiling.clone()).expect("4096 bytes fits");
969        assert_eq!(payload.len(), 4096);
970        assert_eq!(payload.as_str(), at_ceiling);
971
972        let over = "a".repeat(4097);
973        let error = RunHookPayload::new(over).expect_err("4097 bytes does not fit");
974        assert_eq!(error.kind(), ErrorKind::InvalidArg);
975        let message = error.to_string();
976        assert!(message.contains("4097 bytes"), "{message}");
977        assert!(
978            message.contains("ceiling of 4096"),
979            "the message must name the service-model ceiling: {message}"
980        );
981        assert!(message.contains("2025-09-09"), "{message}");
982        assert!(
983            message.contains("wrong by 4x"),
984            "the 16 KB claim has to be named as wrong: {message}"
985        );
986    }
987
988    /// **TRAP-5, the local-refusal half.** An over-ceiling payload means the fake records
989    /// **zero** calls: the launch is refused before any control-plane call.
990    #[tokio::test]
991    async fn an_over_ceiling_payload_reaches_no_control_plane_call() {
992        let (plane, fake, _) = planted();
993
994        // The payload type refuses first, so there is no way to even build the request.
995        let refused = RunHookPayload::new("a".repeat(4097));
996        assert!(refused.is_err());
997        assert_eq!(fake.calls().len(), 0);
998
999        // And the at-ceiling one does launch, so the guard is not simply refusing
1000        // everything.
1001        fake.answer(
1002            "RunMicrovm",
1003            Answer::ok(fake::microvm_response("PENDING", None)),
1004        );
1005        let payload = RunHookPayload::new("a".repeat(4096)).expect("fits");
1006        plane
1007            .run_microvm(RunMicrovmRequest::new("arn:image", payload))
1008            .await
1009            .expect("launches");
1010        assert_eq!(fake.call_count("RunMicrovm"), 1);
1011    }
1012
1013    /// The ceiling is measured in **bytes**, not characters. A payload of 2049 two-byte
1014    /// characters is 4098 bytes and must be refused even though it is 2049 characters.
1015    #[test]
1016    fn the_ceiling_counts_utf8_bytes_rather_than_characters() {
1017        let two_byte_chars = "é".repeat(2049);
1018        assert_eq!(
1019            two_byte_chars.chars().count(),
1020            2049,
1021            "well under 4096 chars"
1022        );
1023        assert_eq!(two_byte_chars.len(), 4098, "but over 4096 bytes");
1024
1025        let error = RunHookPayload::new(two_byte_chars).expect_err("bytes are what count");
1026        assert!(error.to_string().contains("4098 bytes"), "{error}");
1027
1028        // 2048 of them is exactly 4096 bytes, which fits.
1029        let exact = "é".repeat(2048);
1030        assert_eq!(exact.len(), 4096);
1031        RunHookPayload::new(exact).expect("exactly at the ceiling");
1032    }
1033
1034    /// The identity material costs exactly its declared budget, and a launch without it is
1035    /// byte-for-byte what the older constructors built.
1036    ///
1037    /// Measured by difference rather than trusted from the constant's comment, the same
1038    /// discipline `env_contribution` applies: the ceiling is on the serialized string, so
1039    /// only the serialized string can say what the material costs. The compatibility half
1040    /// matters just as much — a pinned daemon that has never heard of identity must see no
1041    /// change from a client that did not ask for it.
1042    #[test]
1043    fn the_identity_material_costs_its_declared_budget_and_absence_costs_nothing() {
1044        let env = std::collections::HashMap::new();
1045        let without = RunHookPayload::for_launch("tok", &env).expect("fits");
1046        let none_asked = RunHookPayload::for_launch_with_identity("tok", &env, None).expect("fits");
1047        assert_eq!(
1048            without.as_str(),
1049            none_asked.as_str(),
1050            "no identity must produce byte-for-byte the payload this client always sent"
1051        );
1052
1053        let identity = crate::identity::LaunchIdentity::from_seeds([7_u8; 32], [9_u8; 32])
1054            .expect("valid seeds");
1055        let with =
1056            RunHookPayload::for_launch_with_identity("tok", &env, Some(&identity)).expect("fits");
1057        assert_eq!(
1058            with.len() - without.len(),
1059            protocol::identity::IDENTITY_PAYLOAD_BYTES,
1060            "the budget constant must equal what the material really costs on the wire"
1061        );
1062
1063        // And the payload parses as the daemon parses it, with both halves present.
1064        let parsed = protocol::hook::RunHook::parse(with.as_str()).expect("the daemon can read it");
1065        assert_eq!(
1066            parsed.identity_seed.as_deref(),
1067            Some(identity.seed_field().as_str())
1068        );
1069        assert_eq!(
1070            parsed.identity_host_public_key.as_deref(),
1071            Some(identity.host_public_field().as_str())
1072        );
1073    }
1074
1075    /// The agent-token payload is the shape the daemon parses, and an oversized token is
1076    /// caught even though the client builds the JSON — because the token is caller-supplied.
1077    #[test]
1078    fn the_agent_token_payload_is_checked_even_though_the_client_builds_it() {
1079        let payload = RunHookPayload::for_agent_token("bearer-token-value").expect("fits");
1080        assert_eq!(payload.as_str(), r#"{"agent_token":"bearer-token-value"}"#);
1081
1082        // A caller passing a credential bundle rather than a bearer token is who this
1083        // catches.
1084        let huge = "x".repeat(4096);
1085        let error = RunHookPayload::for_agent_token(&huge)
1086            .expect_err("the JSON wrapper pushes it over the ceiling");
1087        assert_eq!(error.kind(), ErrorKind::InvalidArg);
1088        assert!(
1089            error.to_string().contains("one bearer token fits"),
1090            "{error}"
1091        );
1092    }
1093
1094    /// An empty launch env produces byte-for-byte the payload the token-only
1095    /// constructor always produced, so a caller who never passes one is unaffected by
1096    /// the field existing — and a daemon baked into an older image sees no new key.
1097    ///
1098    /// **Falsification** — always emit `"env"` and this fails on both assertions.
1099    #[test]
1100    fn an_empty_launch_env_emits_the_payload_the_token_only_constructor_did() {
1101        let with_empty = RunHookPayload::for_launch("tok", &std::collections::HashMap::new())
1102            .expect("an empty env fits");
1103        let token_only = RunHookPayload::for_agent_token("tok").expect("fits");
1104        assert_eq!(with_empty.as_str(), token_only.as_str());
1105        assert_eq!(with_empty.as_str(), r#"{"agent_token":"tok"}"#);
1106        assert!(
1107            !with_empty.as_str().contains("env"),
1108            "an empty env must not appear on the wire at all: {}",
1109            with_empty.as_str()
1110        );
1111    }
1112
1113    /// A launch env is carried in the payload the daemon parses.
1114    #[test]
1115    fn a_launch_env_rides_in_the_payload_the_daemon_parses() {
1116        let mut env = std::collections::HashMap::new();
1117        env.insert("ANTHROPIC_BASE_URL".to_string(), "https://x".to_string());
1118        let payload = RunHookPayload::for_launch("tok", &env).expect("fits");
1119
1120        // Parsed rather than string-compared, since a HashMap has no key order.
1121        let value: serde_json::Value =
1122            serde_json::from_str(payload.as_str()).expect("the payload is JSON");
1123        assert_eq!(value["agent_token"], "tok");
1124        assert_eq!(value["env"]["ANTHROPIC_BASE_URL"], "https://x");
1125    }
1126
1127    /// **The local 4096-byte refusal for a launch env.**
1128    ///
1129    /// The reason this is the case worth its own test rather than a repeat of the
1130    /// ceiling test above: one bearer token has always fit with room to spare, so
1131    /// before `env` existed the ceiling was effectively unreachable through the typed
1132    /// constructor. A launch env is what makes it reachable, and botocore does not
1133    /// enforce it client-side — the oversized request goes to the wire and comes back
1134    /// as a `ValidationException` on a member the caller did not know they were
1135    /// filling (`docs/PLATFORM.md`). So this refusal is the only local signal there is.
1136    ///
1137    /// The message must carry the byte count, the ceiling, and the env's share of it.
1138    /// Without the share, a caller reading "4128 bytes, ceiling 4096" cannot tell
1139    /// whether to shorten the token or drop a variable.
1140    ///
1141    /// **Falsification** — return `Ok` from `for_launch` without calling `new`, or drop
1142    /// the byte-count clause from the message, and this goes red on the specific
1143    /// assertion.
1144    #[test]
1145    fn an_over_ceiling_launch_env_is_refused_locally_with_the_byte_count() {
1146        let mut env = std::collections::HashMap::new();
1147        // Comfortably over on its own, the way a set of session credentials is.
1148        env.insert("CREDENTIALS".to_string(), "c".repeat(4096));
1149
1150        // The expected figure is derived from the same serialization the constructor
1151        // does rather than written as a literal, because a literal here asserts my
1152        // arithmetic about JSON framing and the point is the *count*, not the framing.
1153        let expected = serde_json::json!({ "agent_token": "tok", "env": &env })
1154            .to_string()
1155            .len();
1156        assert!(
1157            expected > 4096,
1158            "the fixture must actually be over: {expected}"
1159        );
1160
1161        let error = RunHookPayload::for_launch("tok", &env)
1162            .expect_err("a credential-scale launch env does not fit");
1163        assert_eq!(error.kind(), ErrorKind::InvalidArg);
1164        let message = error.to_string();
1165
1166        assert!(
1167            message.contains("ceiling of 4096"),
1168            "the ceiling has to be named: {message}"
1169        );
1170        assert!(
1171            message.contains(&format!("{expected} bytes")),
1172            "the actual byte count has to be named: {message}"
1173        );
1174        assert!(
1175            message.contains("launch env contributed"),
1176            "the env's share of the budget is the actionable half: {message}"
1177        );
1178        assert!(
1179            message.contains("1 variable(s)"),
1180            "the variable count has to be named: {message}"
1181        );
1182    }
1183
1184    /// The refusal fires on the *serialized* payload, so a caller who measured their own
1185    /// values would be measuring the wrong thing.
1186    ///
1187    /// Two variables whose raw bytes sum to 4060 — under the ceiling — go over it once
1188    /// the JSON framing, the key names, and the quotes are counted. A check on the map
1189    /// rather than on the string would accept this and the launch would fail at AWS.
1190    #[test]
1191    fn the_launch_env_ceiling_counts_the_serialized_payload_and_not_the_raw_values() {
1192        let mut env = std::collections::HashMap::new();
1193        env.insert("A".to_string(), "a".repeat(2030));
1194        env.insert("B".to_string(), "b".repeat(2030));
1195        let raw: usize = env.values().map(String::len).sum();
1196        assert_eq!(raw, 4060, "the raw values are under the ceiling");
1197        assert!(
1198            raw <= crate::constants::MAX_RUN_HOOK_PAYLOAD_BYTES,
1199            "the fixture only means something if the raw sum fits: {raw}"
1200        );
1201
1202        let error = RunHookPayload::for_launch("tok", &env)
1203            .expect_err("the serialized payload is over even though the raw values are not");
1204        assert!(error.to_string().contains("2 variable(s)"), "{error}");
1205
1206        // And the boundary is still inclusive with an env in play: trimmed to fit, the
1207        // same shape succeeds — so the refusal is a ceiling and not a blanket ban.
1208        let mut fits = std::collections::HashMap::new();
1209        fits.insert("A".to_string(), "a".repeat(2000));
1210        fits.insert("B".to_string(), "b".repeat(2000));
1211        let payload = RunHookPayload::for_launch("tok", &fits).expect("4000-odd bytes fits");
1212        assert!(payload.len() <= 4096, "{}", payload.len());
1213    }
1214
1215    /// An empty payload is legal: the model's minimum is 0.
1216    #[test]
1217    fn an_empty_payload_is_legal() {
1218        let payload = RunHookPayload::new("").expect("min is 0");
1219        assert!(payload.is_empty());
1220        assert_eq!(payload.len(), 0);
1221    }
1222
1223    /// The launch request lands on the model's path with the model's member names, and the
1224    /// connectors are derived ARNs split across the two members (TRAP-4).
1225    #[tokio::test]
1226    async fn the_launch_emits_derived_connector_arns_in_the_right_members() {
1227        let (plane, fake, _) = planted();
1228        fake.answer(
1229            "RunMicrovm",
1230            Answer::ok(fake::microvm_response("PENDING", None)),
1231        );
1232
1233        let payload = RunHookPayload::for_agent_token("token").expect("fits");
1234        let request = RunMicrovmRequest::new("arn:image", payload).with_egress();
1235        plane.run_microvm(request).await.expect("launches");
1236
1237        let calls = fake.calls();
1238        assert_eq!(calls[0].path, "/2025-09-09/microvms");
1239        assert_eq!(calls[0].method, super::super::transport::Method::Post);
1240
1241        let body = fake.first_body("RunMicrovm");
1242        assert_eq!(
1243            body["ingressNetworkConnectors"],
1244            serde_json::json!([
1245                "arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:ALL_INGRESS"
1246            ])
1247        );
1248        assert_eq!(
1249            body["egressNetworkConnectors"],
1250            serde_json::json!([
1251                "arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:INTERNET_EGRESS"
1252            ])
1253        );
1254        assert_eq!(body["runHookPayload"], r#"{"agent_token":"token"}"#);
1255        assert_eq!(body["maximumDurationInSeconds"], 3600);
1256    }
1257
1258    /// Without egress, the member is **absent** — the request names no connector. What
1259    /// the platform does with that omission is a measurement, not this test's claim:
1260    /// `docs/PLATFORM.md`, "A VM launched without the egress connector still has
1261    /// outbound network" (2026-09-11 and 2026-09-12).
1262    #[tokio::test]
1263    async fn a_launch_without_egress_omits_the_member_entirely() {
1264        let (plane, fake, _) = planted();
1265        fake.answer(
1266            "RunMicrovm",
1267            Answer::ok(fake::microvm_response("PENDING", None)),
1268        );
1269        let payload = RunHookPayload::for_agent_token("token").expect("fits");
1270        plane
1271            .run_microvm(RunMicrovmRequest::new("arn:image", payload))
1272            .await
1273            .expect("launches");
1274
1275        let body = fake.first_body("RunMicrovm");
1276        assert!(
1277            body.get("egressNetworkConnectors").is_none(),
1278            "omitting egress is the whole mechanism: {body}"
1279        );
1280        assert!(body.get("ingressNetworkConnectors").is_some());
1281    }
1282
1283    #[tokio::test]
1284    async fn customer_vpc_connector_arns_reach_the_egress_member_unchanged() {
1285        let (plane, fake, _) = planted();
1286        fake.answer(
1287            "RunMicrovm",
1288            Answer::ok(fake::microvm_response("PENDING", None)),
1289        );
1290        let payload = RunHookPayload::for_agent_token("token").expect("fits");
1291        let mut request = RunMicrovmRequest::new("arn:image", payload);
1292        let arns: Vec<String> = (0..10)
1293            .map(|id| format!("arn:aws:lambda:us-east-1:123456789012:network-connector:vpc-{id}:1"))
1294            .collect();
1295        request.egress_network_connectors = arns.clone();
1296        plane
1297            .run_microvm(request)
1298            .await
1299            .expect("ten egress connectors plus ingress fits");
1300        let body = fake.first_body("RunMicrovm");
1301        assert_eq!(body["egressNetworkConnectors"], serde_json::json!(arns));
1302        assert_eq!(
1303            body["ingressNetworkConnectors"].as_array().unwrap().len(),
1304            1
1305        );
1306    }
1307
1308    #[tokio::test]
1309    async fn invalid_customer_egress_connectors_are_refused_before_launch() {
1310        let valid = "arn:aws:lambda:us-east-1:123456789012:network-connector:vpc-test";
1311        let invalid = [
1312            "VPC_EGRESS",
1313            "arn:aws:lambda:us-west-2:123456789012:network-connector:vpc-test",
1314            "arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:INTERNET_EGRESS",
1315            "arn:aws:lambda:us-east-1:123456789012:network-connector:",
1316            "arn:aws:lambda:us-east-1:123456789012:network-connector:vpc-test:0",
1317            "arn:aws:lambda:us-east-1:123456789012:network-connector:vpc-test/other",
1318        ];
1319        for arn in invalid {
1320            let (plane, fake, _) = planted();
1321            let payload = RunHookPayload::for_agent_token("token").expect("fits");
1322            let error = plane
1323                .run_microvm(
1324                    RunMicrovmRequest::new("arn:image", payload).with_egress_network_connector(arn),
1325                )
1326                .await
1327                .expect_err(arn);
1328            assert_eq!(error.kind(), ErrorKind::InvalidArg);
1329            assert!(
1330                error.to_string().contains("egressNetworkConnectors"),
1331                "{error}"
1332            );
1333            assert!(fake.calls().is_empty());
1334        }
1335        for (internet, count) in [(true, 1), (false, 11)] {
1336            let (plane, fake, _) = planted();
1337            let payload = RunHookPayload::for_agent_token("token").expect("fits");
1338            let mut request = RunMicrovmRequest::new("arn:image", payload);
1339            request.egress_network_connectors = vec![valid.to_string(); count];
1340            if internet {
1341                request = request.with_egress();
1342            }
1343            let error = plane
1344                .run_microvm(request)
1345                .await
1346                .expect_err("invalid routing request");
1347            assert_eq!(error.kind(), ErrorKind::InvalidArg);
1348            assert!(fake.calls().is_empty());
1349        }
1350    }
1351
1352    /// **A pinned `imageVersion` reaches the wire, and an unpinned launch omits the member.**
1353    ///
1354    /// The absence half is the one that matters for compatibility: an unpinned launch has to
1355    /// emit byte-for-byte what this client always sent, so a `"imageVersion": null` on every
1356    /// launch would be a new member on a request that has worked for months. The presence half
1357    /// is what makes a canary a canary — the launch goes against the version named rather than
1358    /// against whatever became latest while it was starting.
1359    ///
1360    /// **Falsification** — run 2026-08-16. Drop the `image_version` assignment in
1361    /// `run_microvm` and the pinned assertion goes red with the member absent; remove
1362    /// `skip_serializing_if` from the wire field and the absence assertion goes red with a
1363    /// `null`.
1364    #[tokio::test]
1365    async fn a_pinned_launch_version_reaches_the_wire_and_an_unpinned_one_omits_the_member() {
1366        let (plane, fake, _) = planted();
1367        fake.answer(
1368            "RunMicrovm",
1369            Answer::ok(fake::microvm_response("PENDING", None)),
1370        );
1371        let payload = RunHookPayload::for_agent_token("token").expect("fits");
1372        plane
1373            .run_microvm(RunMicrovmRequest::new("arn:image", payload).with_image_version("2.0"))
1374            .await
1375            .expect("launches");
1376
1377        let body = fake.first_body("RunMicrovm");
1378        assert_eq!(body["imageVersion"], "2.0");
1379        assert_eq!(
1380            body["imageIdentifier"], "arn:image",
1381            "pinning a version does not replace the identifier; both are sent"
1382        );
1383
1384        let (plane, fake, _) = planted();
1385        fake.answer(
1386            "RunMicrovm",
1387            Answer::ok(fake::microvm_response("PENDING", None)),
1388        );
1389        let payload = RunHookPayload::for_agent_token("token").expect("fits");
1390        plane
1391            .run_microvm(RunMicrovmRequest::new("arn:image", payload))
1392            .await
1393            .expect("launches");
1394        assert!(
1395            fake.first_body("RunMicrovm").get("imageVersion").is_none(),
1396            "an unpinned launch must send what this client always sent: {}",
1397            fake.first_body("RunMicrovm")
1398        );
1399    }
1400
1401    /// An invalid `imageVersion` is refused before any call — including on the launch path,
1402    /// which is where a rollback happens.
1403    ///
1404    /// A `ValidationException` about the request rather than about the version, arriving at the
1405    /// moment someone is re-pinning away from a bad build, is the least useful failure
1406    /// available. So the `Version` shape's three constraints are checked here.
1407    ///
1408    /// **Falsification** — run 2026-08-16. Delete the `require_valid_version` call from
1409    /// `run_microvm` and the zero-call assertion goes red.
1410    #[tokio::test]
1411    async fn an_invalid_launch_version_reaches_no_control_plane_call() {
1412        for bad in ["", "2.0\n", "a b"] {
1413            let (plane, fake, _) = planted();
1414            let payload = RunHookPayload::for_agent_token("token").expect("fits");
1415            let error = plane
1416                .run_microvm(RunMicrovmRequest::new("arn:image", payload).with_image_version(bad))
1417                .await
1418                .expect_err(&format!("{bad:?} is not a legal Version"));
1419            assert_eq!(error.kind(), ErrorKind::InvalidArg, "{bad:?}");
1420            assert!(
1421                error.to_string().contains("imageVersion"),
1422                "{bad:?}: {error}"
1423            );
1424            assert_eq!(fake.calls().len(), 0, "{bad:?} must not reach the wire");
1425        }
1426    }
1427
1428    /// The connectors reaching the wire are ARNs, never bare names — a bare name is rejected
1429    /// with "Malformed network connector ARN", and it is the value that reads most natural
1430    /// to write.
1431    #[tokio::test]
1432    async fn no_bare_connector_name_reaches_the_wire() {
1433        let (plane, fake, _) = planted();
1434        fake.answer(
1435            "RunMicrovm",
1436            Answer::ok(fake::microvm_response("PENDING", None)),
1437        );
1438        let payload = RunHookPayload::for_agent_token("token").expect("fits");
1439        plane
1440            .run_microvm(RunMicrovmRequest::new("arn:image", payload).with_egress())
1441            .await
1442            .expect("launches");
1443
1444        let body = fake.first_body("RunMicrovm");
1445        for member in ["ingressNetworkConnectors", "egressNetworkConnectors"] {
1446            for value in body[member].as_array().expect("a list") {
1447                let text = value.as_str().expect("a string");
1448                assert!(text.starts_with("arn:aws:lambda:"), "{member}: {text}");
1449                assert_ne!(text, "ALL_INGRESS");
1450                assert_ne!(text, "INTERNET_EGRESS");
1451            }
1452        }
1453    }
1454
1455    /// An out-of-range duration is refused before any call.
1456    #[tokio::test]
1457    async fn an_out_of_range_duration_reaches_no_control_plane_call() {
1458        let (plane, fake, _) = planted();
1459        let payload = RunHookPayload::for_agent_token("token").expect("fits");
1460        let mut request = RunMicrovmRequest::new("arn:image", payload);
1461        request.max_duration_sec = 28_801;
1462
1463        let error = plane
1464            .run_microvm(request)
1465            .await
1466            .expect_err("over the ceiling");
1467        assert_eq!(error.kind(), ErrorKind::InvalidArg);
1468        assert_eq!(fake.calls().len(), 0);
1469    }
1470
1471    /// **TRAP-8.** A VM that reaches a terminal state before RUNNING is rejected with the
1472    /// state **and** the `stateReason` in the message.
1473    ///
1474    /// The message is asserted rather than merely the error, because a timeout also produces
1475    /// an error — so "an error occurred" would pass against the exact defect this closes.
1476    ///
1477    /// **Falsification** — remove the `fail_on` branch from `wait_for_state` and this test
1478    /// fails with an `ErrorKind::Timeout` whose message names neither the state nor the
1479    /// reason. Verified; see the packet's guard proofs.
1480    #[tokio::test]
1481    async fn a_terminal_state_before_running_carries_both_the_state_and_the_reason() {
1482        let (plane, fake, _) = planted();
1483        fake.answer(
1484            "GetMicrovm",
1485            Answer::ok(fake::microvm_response(
1486                "TERMINATED",
1487                Some("run hook returned 500"),
1488            )),
1489        );
1490
1491        let error = plane
1492            .wait_for_running("mvm-abc123", WaitOpts::for_launch())
1493            .await
1494            .expect_err("a dead VM must not be waited out");
1495
1496        assert_eq!(error.kind(), ErrorKind::LaunchDied);
1497        assert_eq!(error.code(), "ERR_LAUNCH_DIED");
1498        let message = error.to_string();
1499        assert!(
1500            message.contains("TERMINATED"),
1501            "the state must be attached: {message}"
1502        );
1503        assert!(
1504            message.contains("run hook returned 500"),
1505            "the stateReason must be attached: {message}"
1506        );
1507        assert!(
1508            message.contains("lifecycle hook failed"),
1509            "the likely cause is worth naming: {message}"
1510        );
1511        assert!(
1512            fake.call_count("GetMicrovm") == 1,
1513            "it fails fast rather than polling to the deadline"
1514        );
1515    }
1516
1517    /// Every terminal state fails the launch fast, not just TERMINATED. SUSPENDED reached
1518    /// before RUNNING is also a death — the VM never came up.
1519    ///
1520    /// The states are read from `constants::TERMINAL_STATES` rather than listed, so a state
1521    /// added there is covered here without a second edit — and the count is asserted so the
1522    /// loop cannot silently become empty.
1523    #[tokio::test]
1524    async fn every_terminal_state_fails_a_launch_fast() {
1525        assert_eq!(crate::constants::TERMINAL_STATES.len(), 4);
1526        for state in crate::constants::TERMINAL_STATES {
1527            let (plane, fake, _) = planted();
1528            fake.answer(
1529                "GetMicrovm",
1530                Answer::ok(fake::microvm_response(state, Some("hook timeout"))),
1531            );
1532            let error = plane
1533                .wait_for_running("mvm-abc123", WaitOpts::for_launch())
1534                .await
1535                .expect_err(&format!("{state} before RUNNING is a death"));
1536            assert_eq!(error.kind(), ErrorKind::LaunchDied, "{state}");
1537            assert!(error.to_string().contains(state), "{state}: {error}");
1538            assert!(
1539                error.to_string().contains("hook timeout"),
1540                "{state}: {error}"
1541            );
1542            assert_eq!(
1543                fake.call_count("GetMicrovm"),
1544                1,
1545                "{state} must fail fast rather than polling to the deadline"
1546            );
1547        }
1548    }
1549
1550    /// A terminal state with **no** `stateReason` says so rather than printing an empty
1551    /// string: "the service said nothing" is a different diagnosis from "the service said
1552    /// nothing useful".
1553    #[tokio::test]
1554    async fn a_missing_state_reason_is_named_rather_than_printed_empty() {
1555        let (plane, fake, _) = planted();
1556        fake.answer(
1557            "GetMicrovm",
1558            Answer::ok(fake::microvm_response("TERMINATED", None)),
1559        );
1560
1561        let error = plane
1562            .wait_for_running("mvm-abc123", WaitOpts::for_launch())
1563            .await
1564            .expect_err("still a death");
1565        let message = error.to_string();
1566        assert!(message.contains("no stateReason"), "{message}");
1567        // The failure this guards against is an absent reason rendering as an empty string,
1568        // which reads as the service having answered with nothing rather than not having
1569        // answered. `: .` is what that looks like in this message's shape.
1570        assert!(
1571            !message.contains(r#"["RUNNING"]: ."#),
1572            "an absent reason must not render as an empty string: {message}"
1573        );
1574        assert_eq!(error.kind(), ErrorKind::LaunchDied);
1575    }
1576
1577    /// The wait returns on RUNNING, having polled through PENDING.
1578    #[tokio::test]
1579    async fn the_launch_wait_polls_through_pending_and_returns_on_running() {
1580        let (plane, fake, _) = planted();
1581        fake.answer(
1582            "GetMicrovm",
1583            Answer::ok(fake::microvm_response("PENDING", None)),
1584        )
1585        .answer(
1586            "GetMicrovm",
1587            Answer::ok(fake::microvm_response("RUNNING", None)),
1588        );
1589
1590        let vm = plane
1591            .wait_for_running("mvm-abc123", WaitOpts::for_launch())
1592            .await
1593            .expect("reaches RUNNING");
1594        assert_eq!(vm.state, "RUNNING");
1595        assert_eq!(
1596            vm.endpoint,
1597            "https://mvm-abc123.microvm.us-east-1.amazonaws.com"
1598        );
1599        assert_eq!(fake.call_count("GetMicrovm"), 2);
1600    }
1601
1602    /// A launch that never comes up ends at its deadline with a `Timeout` — which is a
1603    /// *different* error from TRAP-8's, and the distinction is the whole reason the TRAP-8
1604    /// test asserts on the message.
1605    #[tokio::test]
1606    async fn a_launch_that_never_comes_up_times_out_rather_than_reporting_a_death() {
1607        let (plane, fake, _) = planted();
1608        fake.answer(
1609            "GetMicrovm",
1610            Answer::ok(fake::microvm_response("PENDING", None)),
1611        );
1612
1613        let error = plane
1614            .wait_for_running("mvm-abc123", WaitOpts::for_launch())
1615            .await
1616            .expect_err("the deadline elapses");
1617        assert_eq!(error.kind(), ErrorKind::Timeout);
1618        assert_ne!(
1619            error.kind(),
1620            ErrorKind::LaunchDied,
1621            "a timeout is not a death, and conflating them is what hid the cause"
1622        );
1623        assert!(error.to_string().contains("PENDING"), "{error}");
1624    }
1625
1626    /// The resume path fails fast on the **dead** states and waits through SUSPENDED —
1627    /// which is the state a resume is called from, so failing on it would fail every resume.
1628    #[tokio::test]
1629    async fn the_resume_wait_tolerates_suspended_and_fails_on_terminated() {
1630        let (plane, fake, _) = planted();
1631        fake.answer(
1632            "GetMicrovm",
1633            Answer::ok(fake::microvm_response("SUSPENDED", None)),
1634        )
1635        .answer(
1636            "GetMicrovm",
1637            Answer::ok(fake::microvm_response("RUNNING", None)),
1638        );
1639
1640        let vm = plane
1641            .wait_for_state(
1642                "mvm-abc123",
1643                &["RUNNING"],
1644                &crate::constants::DEAD_STATES,
1645                WaitOpts::for_launch(),
1646            )
1647            .await
1648            .expect("SUSPENDED is a waypoint on the resume path");
1649        assert_eq!(vm.state, "RUNNING");
1650
1651        let (plane, fake, _) = planted();
1652        fake.answer(
1653            "GetMicrovm",
1654            Answer::ok(fake::microvm_response(
1655                "TERMINATED",
1656                Some("suspended window elapsed"),
1657            )),
1658        );
1659        let error = plane
1660            .wait_for_state(
1661                "mvm-abc123",
1662                &["RUNNING"],
1663                &crate::constants::DEAD_STATES,
1664                WaitOpts::for_launch(),
1665            )
1666            .await
1667            .expect_err("a terminated VM never reaches RUNNING");
1668        assert_eq!(error.kind(), ErrorKind::LaunchDied);
1669        assert!(
1670            error.to_string().contains("suspended window elapsed"),
1671            "{error}"
1672        );
1673    }
1674
1675    /// A suspend **wants** TERMINATED as well as SUSPENDED: a VM that dies while suspending
1676    /// is a state to report, not an exception out of a teardown.
1677    #[tokio::test]
1678    async fn a_suspend_treats_terminated_as_a_state_to_report() {
1679        let (plane, fake, _) = planted();
1680        fake.answer(
1681            "GetMicrovm",
1682            Answer::ok(fake::microvm_response("TERMINATED", None)),
1683        );
1684
1685        let vm = plane
1686            .wait_for_state("mvm-abc123", &SUSPEND_WANTED, &[], WaitOpts::for_launch())
1687            .await
1688            .expect("a suspend reports the state it reached");
1689        assert_eq!(vm.state, "TERMINATED");
1690    }
1691
1692    /// The lifecycle calls land on the model's paths and methods. `POST` for suspend and
1693    /// resume, `DELETE` for terminate — transcribed from each operation's `http` trait.
1694    #[tokio::test]
1695    async fn the_lifecycle_calls_use_the_models_paths_and_methods() {
1696        let (plane, fake, _) = planted();
1697        fake.answer("SuspendMicrovm", Answer::ok(fake::empty_response()))
1698            .answer("ResumeMicrovm", Answer::ok(fake::empty_response()))
1699            .answer("TerminateMicrovm", Answer::ok(fake::empty_response()));
1700
1701        plane.suspend("mvm-1").await.expect("suspends");
1702        plane.resume("mvm-1").await.expect("resumes");
1703        plane.terminate("mvm-1").await.expect("terminates");
1704
1705        let calls = fake.calls();
1706        use super::super::transport::Method;
1707        assert_eq!(
1708            calls
1709                .iter()
1710                .map(|call| (call.operation, call.method, call.path.as_str()))
1711                .collect::<Vec<_>>(),
1712            [
1713                (
1714                    "SuspendMicrovm",
1715                    Method::Post,
1716                    "/2025-09-09/microvms/mvm-1/suspend"
1717                ),
1718                (
1719                    "ResumeMicrovm",
1720                    Method::Post,
1721                    "/2025-09-09/microvms/mvm-1/resume"
1722                ),
1723                (
1724                    "TerminateMicrovm",
1725                    Method::Delete,
1726                    "/2025-09-09/microvms/mvm-1"
1727                ),
1728            ]
1729        );
1730    }
1731
1732    /// **TRAP-7.** The minted token is read out of the `authToken` **map**, and
1733    /// [`ProxyToken::headers`] emits both headers.
1734    #[tokio::test]
1735    async fn the_minted_token_comes_from_the_map_and_carries_both_headers() {
1736        let (plane, fake, _) = planted();
1737        fake.answer(
1738            "CreateMicrovmAuthToken",
1739            Answer::ok(fake::auth_token_response("opaque-proxy-token")),
1740        );
1741
1742        let token = plane.mint_auth_token("mvm-1").await.expect("mints");
1743        assert_eq!(token.value(), "opaque-proxy-token");
1744        assert_eq!(token.port(), 9000);
1745
1746        let headers = token.headers();
1747        assert_eq!(
1748            headers[0],
1749            ("X-aws-proxy-auth", "opaque-proxy-token".to_string())
1750        );
1751        assert_eq!(
1752            headers[1],
1753            ("X-aws-proxy-port", "9000".to_string()),
1754            "the auth header without the port header is rejected in a way that reads like a \
1755             bad token"
1756        );
1757
1758        let body = fake.first_body("CreateMicrovmAuthToken");
1759        assert_eq!(body["expirationInMinutes"], 60);
1760        assert_eq!(body["allowedPorts"][0]["port"], 9000);
1761    }
1762
1763    /// The shell mint posts to the model's `shell-auth-token` route with **only** the
1764    /// expiration in its body, and reads the credential out of the `authToken` map —
1765    /// the same `TokenParts` discipline as TRAP-7.
1766    ///
1767    /// The `allowedPorts` absence is asserted rather than assumed, because it is the
1768    /// measured heart of the operation: `CreateMicrovmShellAuthToken` takes no ports at
1769    /// all — the shell is not a port — and a client that sent them would be sending a
1770    /// member the model does not declare (`docs/PLATFORM.md`, 2026-08-15).
1771    ///
1772    /// **Falsification** — watched fail 2026-08-31: pointing the call at
1773    /// `paths::auth_token` fails the path assertion at the `assert_eq!` on `calls[0].path`.
1774    #[tokio::test]
1775    async fn the_shell_mint_names_its_own_route_and_sends_no_ports() {
1776        let (plane, fake, _) = planted();
1777        fake.answer(
1778            "CreateMicrovmShellAuthToken",
1779            Answer::ok(fake::auth_token_response("opaque-shell-jwe")),
1780        );
1781
1782        let token = plane
1783            .mint_shell_auth_token("mvm-1")
1784            .await
1785            .expect("mints a shell token");
1786        assert_eq!(token.value(), "opaque-shell-jwe");
1787
1788        let calls = fake.calls();
1789        assert_eq!(calls.len(), 1);
1790        assert_eq!(calls[0].operation, "CreateMicrovmShellAuthToken");
1791        assert_eq!(calls[0].path, "/2025-09-09/microvms/mvm-1/shell-auth-token");
1792
1793        let body = fake.first_body("CreateMicrovmShellAuthToken");
1794        assert_eq!(body["expirationInMinutes"], 60);
1795        assert_eq!(
1796            body.get("allowedPorts"),
1797            None,
1798            "the shell is not a port: the request declares no allowedPorts member"
1799        );
1800        assert_eq!(
1801            body.as_object().map(serde_json::Map::len),
1802            Some(1),
1803            "expirationInMinutes is the body's only member: {body}"
1804        );
1805    }
1806
1807    /// The shell mint refuses an empty identifier locally, like every other
1808    /// identifier-in-the-URI operation: an empty segment would address a route the model
1809    /// does not declare.
1810    #[tokio::test]
1811    async fn the_shell_mint_refuses_an_empty_identifier_before_the_wire() {
1812        let (plane, fake, _) = planted();
1813        let error = plane
1814            .mint_shell_auth_token("")
1815            .await
1816            .expect_err("an empty identifier is refused");
1817        assert_eq!(error.kind(), ErrorKind::InvalidArg);
1818        assert_eq!(fake.calls().len(), 0, "nothing may reach the wire");
1819    }
1820
1821    /// A launch request's `Debug` does not print the agent token.
1822    ///
1823    /// Asserted on the whole [`RunMicrovmRequest`] rather than on the payload alone,
1824    /// because that is the type a caller actually logs — and it keeps its derived `Debug`,
1825    /// so what is pinned here is that the derive inherits [`RunHookPayload`]'s hand-written
1826    /// one. **Falsification** — restore `#[derive(Debug)]` on [`RunHookPayload`] and the
1827    /// `SECRETTOKEN` assertion fails through the derived request `Debug`.
1828    #[test]
1829    fn a_launch_requests_debug_does_not_print_the_agent_token() {
1830        let payload = RunHookPayload::for_agent_token("SECRETTOKEN").expect("fits");
1831
1832        let rendered = format!("{payload:?}");
1833        assert!(
1834            !rendered.contains("SECRETTOKEN"),
1835            "the agent token reached a Debug string: {rendered}"
1836        );
1837        assert!(
1838            rendered.contains(&format!("{} bytes", payload.len())),
1839            "the size is what a TRAP-5 diagnosis needs: {rendered}"
1840        );
1841
1842        let request = RunMicrovmRequest::new("arn:image", payload);
1843        let rendered = format!("{request:?}");
1844        assert!(rendered.contains("arn:image"), "{rendered}");
1845        assert!(
1846            !rendered.contains("SECRETTOKEN"),
1847            "the agent token reached a Debug string through the request: {rendered}"
1848        );
1849    }
1850
1851    /// A minted token's `Debug` names its headers and never its value, so a logged
1852    /// launch does not log a credential.
1853    ///
1854    /// The mirror of `session::proxy`'s `a_proxy_token_debug_does_not_print_the_credential`,
1855    /// on the control-plane side of the conversion. **Falsification** — restore
1856    /// `#[derive(Debug)]` on [`ProxyToken`] and this fails on the `secret` assertion.
1857    #[test]
1858    fn a_proxy_token_debug_does_not_print_the_credential() {
1859        let mut map = std::collections::BTreeMap::new();
1860        map.insert(
1861            PROXY_AUTH_HEADER.to_string(),
1862            "eyJhbGciOi-secret".to_string(),
1863        );
1864        let token = ProxyToken::from_map(&map, 9000).expect("the map has the auth key");
1865
1866        let rendered = format!("{token:?}");
1867        assert!(rendered.contains(PROXY_AUTH_HEADER), "{rendered}");
1868        assert!(rendered.contains("9000"), "{rendered}");
1869        assert!(
1870            !rendered.contains("secret"),
1871            "the token value reached a Debug string: {rendered}"
1872        );
1873    }
1874
1875    /// A response whose map lacks the auth key is **retryable**, because minting happens
1876    /// inside the request retry path and a throttle mid-run must not kill a healthy trial.
1877    #[test]
1878    fn a_map_missing_the_auth_key_is_a_retryable_mint_failure() {
1879        let mut map = std::collections::BTreeMap::new();
1880        map.insert("SomeOtherKey".to_string(), "value".to_string());
1881
1882        let error = ProxyToken::from_map(&map, 9000).expect_err("no auth key");
1883        assert_eq!(
1884            error.wire_kind(),
1885            Some(crate::error::WireKind::AuthTokenMint)
1886        );
1887        assert!(error.retryable(), "minting is inside the retry path");
1888        assert!(
1889            error.to_string().contains("is a map, not a string"),
1890            "{error}"
1891        );
1892    }
1893
1894    /// The token is minted for the configured port, not always 9000.
1895    #[tokio::test]
1896    async fn the_token_is_minted_for_the_configured_port() {
1897        let fake = Arc::new(FakeControlPlane::new());
1898        let plane = crate::testing::control_plane(
1899            fake.clone(),
1900            Region::UsEast1,
1901            Arc::new(TestClock::new()),
1902        )
1903        .with_port(8080)
1904        .expect("8080 is a legal port");
1905        fake.answer(
1906            "CreateMicrovmAuthToken",
1907            Answer::ok(fake::auth_token_response("t")),
1908        );
1909
1910        let token = plane.mint_auth_token("mvm-1").await.expect("mints");
1911        assert_eq!(token.port(), 8080);
1912        assert_eq!(
1913            fake.first_body("CreateMicrovmAuthToken")["allowedPorts"][0]["port"],
1914            8080
1915        );
1916    }
1917
1918    /// **TRAP-11, revised, across a full lifecycle.** Drive create, wait, launch, wait,
1919    /// mint, suspend, resume, terminate, delete — and then assert the recorder saw
1920    /// **zero** shell-auth calls, and that the launch request carried **exactly the
1921    /// connectors the caller asked for**, nothing added and nothing dropped.
1922    ///
1923    /// The body scan used to assert zero `SHELL_INGRESS` anywhere, standing on the claim
1924    /// that no intent could name it. `ConnectorIntent::ShellIngress` exists now — the
1925    /// measured ground is that one interactive session is not programmatic exec, not
1926    /// that the connector is unspeakable (`docs/PLATFORM.md`, 2026-08-15) — so the
1927    /// honest assertion is set-equality against the request: a caller that asked for
1928    /// `[ALL_INGRESS]` plus egress gets that set on the wire and no other.
1929    ///
1930    /// The shell-auth scans stay as counts, and their ground moved once more:
1931    /// `mint_shell_auth_token` exists now (issue #69, `microvm shell`'s method), so the
1932    /// absence being pinned is no longer the method's but the **exec path's** — a
1933    /// lifecycle that asked for no shell drives everything exec can do and emits zero
1934    /// shell operations. Nothing here calls the shell mint, and that is the assertion.
1935    ///
1936    /// **Falsification** — make any lifecycle step (`run_microvm`, `mint_auth_token`,
1937    /// suspend/resume/terminate) also touch the shell route, or call
1938    /// `mint_shell_auth_token` from this lifecycle: the operation scan and the path scan
1939    /// both fail. Make `run_microvm` inject a connector the request did not carry: the
1940    /// set-equality assertions fail.
1941    #[tokio::test]
1942    async fn a_full_lifecycle_never_calls_shell_auth_and_requests_only_what_was_asked() {
1943        let (plane, fake, _) = planted();
1944        fake.answer(
1945            "CreateMicrovmImage",
1946            Answer::created(fake::create_image_response("img")),
1947        )
1948        .answer(
1949            "GetMicrovmImage",
1950            Answer::ok(fake::get_image_response("img", "CREATED")),
1951        )
1952        .answer(
1953            "RunMicrovm",
1954            Answer::ok(fake::microvm_response("PENDING", None)),
1955        )
1956        .answer(
1957            "GetMicrovm",
1958            Answer::ok(fake::microvm_response("RUNNING", None)),
1959        )
1960        .answer(
1961            "CreateMicrovmAuthToken",
1962            Answer::ok(fake::auth_token_response("proxy-token")),
1963        )
1964        .answer("SuspendMicrovm", Answer::ok(fake::empty_response()))
1965        .answer("ResumeMicrovm", Answer::ok(fake::empty_response()))
1966        .answer("TerminateMicrovm", Answer::ok(fake::empty_response()))
1967        .answer(
1968            "ListMicrovmImageVersions",
1969            Answer::ok(fake::list_versions_response("1")),
1970        )
1971        .answer(
1972            "DeleteMicrovmImage",
1973            Answer::ok(fake::delete_image_response()),
1974        );
1975
1976        // Everything this client can do, in the order a real run does it.
1977        let request = super::super::CreateImageRequest::new(
1978            "img",
1979            crate::testing::aarch64_daemon(b"binary"),
1980            "s3://bucket/img.zip",
1981            "arn:aws:iam::123456789012:role/build",
1982        );
1983        let image = plane.create_image(request).await.expect("creates");
1984        plane
1985            .wait_for_image(&image.identifier, image.size, WaitOpts::default())
1986            .await
1987            .expect("becomes usable");
1988
1989        let payload = RunHookPayload::for_agent_token("agent-token").expect("fits");
1990        let vm = plane
1991            .run_microvm(
1992                super::super::RunMicrovmRequest::new(&image.identifier, payload).with_egress(),
1993            )
1994            .await
1995            .expect("launches");
1996        plane
1997            .wait_for_running(&vm.id, WaitOpts::for_launch())
1998            .await
1999            .expect("reaches RUNNING");
2000        plane.mint_auth_token(&vm.id).await.expect("mints");
2001        plane.suspend(&vm.id).await.expect("suspends");
2002        plane.resume(&vm.id).await.expect("resumes");
2003        plane.terminate(&vm.id).await.expect("terminates");
2004        plane
2005            .delete_image(&image.identifier, 1, std::time::Duration::from_secs(1))
2006            .await;
2007
2008        // The lifecycle really ran, so the zero counts below are about absence rather than
2009        // about nothing having happened.
2010        let operations = fake.operations();
2011        assert!(
2012            operations.len() >= 10,
2013            "the lifecycle must actually have run: {operations:?}"
2014        );
2015        assert!(operations.contains(&"CreateMicrovmAuthToken"));
2016
2017        assert_eq!(
2018            fake.call_count("CreateMicrovmShellAuthToken"),
2019            0,
2020            "the shell mint exists for `microvm shell` alone, and a lifecycle that asked \
2021             for no shell must mint no shell token: {operations:?}"
2022        );
2023        for operation in &operations {
2024            assert!(
2025                !operation.contains("Shell"),
2026                "no operation may be a shell one: {operation}"
2027            );
2028        }
2029        for path in fake.paths() {
2030            assert!(
2031                !path.contains("shell"),
2032                "no request may reach a shell route: {path}"
2033            );
2034        }
2035
2036        // The launch carried exactly the connector set the caller asked for —
2037        // `ALL_INGRESS` from the default plus the egress `with_egress` added — and this
2038        // client injected nothing of its own. Set-equality cuts both ways: a connector
2039        // the caller never named (the shell, or anything else) fails it, and so does a
2040        // dropped one.
2041        let launch = fake.first_body("RunMicrovm");
2042        assert_eq!(
2043            launch["ingressNetworkConnectors"],
2044            serde_json::json!([
2045                "arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:ALL_INGRESS"
2046            ]),
2047            "ingress is exactly what the caller asked for"
2048        );
2049        assert_eq!(
2050            launch["egressNetworkConnectors"],
2051            serde_json::json!([
2052                "arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:INTERNET_EGRESS"
2053            ]),
2054            "egress is exactly what the caller asked for"
2055        );
2056    }
2057
2058    /// The measured pair `[HTTP_INGRESS, SHELL_INGRESS]` reaches the wire intact — the
2059    /// combination `docs/PLATFORM.md` (2026-08-15) records as launching and minting a
2060    /// shell token successfully. Both are ingress connectors, so both must land in
2061    /// `ingressNetworkConnectors`; a split that only recognized `ALL_INGRESS` as ingress
2062    /// would silently drop them, and this is the test that catches that.
2063    #[tokio::test]
2064    async fn http_plus_shell_ingress_is_the_pair_that_reaches_the_wire() {
2065        let (plane, fake, _) = planted();
2066        fake.answer(
2067            "RunMicrovm",
2068            Answer::ok(fake::microvm_response("PENDING", None)),
2069        );
2070
2071        let payload = RunHookPayload::for_agent_token("agent-token").expect("fits");
2072        let mut request = RunMicrovmRequest::new("arn:image", payload);
2073        request.connectors = vec![
2074            super::super::ConnectorIntent::HttpIngress,
2075            super::super::ConnectorIntent::ShellIngress,
2076        ];
2077        plane.run_microvm(request).await.expect("launches");
2078
2079        let launch = fake.first_body("RunMicrovm");
2080        assert_eq!(
2081            launch["ingressNetworkConnectors"],
2082            serde_json::json!([
2083                "arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:HTTP_INGRESS",
2084                "arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:SHELL_INGRESS"
2085            ]),
2086            "both fine-grained ingress connectors reach the wire, in the order asked"
2087        );
2088        assert_eq!(
2089            launch.get("egressNetworkConnectors"),
2090            None,
2091            "no egress was asked for, so the member is absent rather than empty"
2092        );
2093    }
2094
2095    /// `ALL_INGRESS` combined with the shell connector is refused **before the wire**.
2096    /// The platform accepts the invalid set at launch — the VM reaches RUNNING and bills
2097    /// — and rejects it only when a shell token is minted (`docs/PLATFORM.md`,
2098    /// 2026-08-15). A local refusal is the only one that costs nothing.
2099    ///
2100    /// **Falsification** — run 2026-08-31: with the combination guard in
2101    /// [`ControlPlane::run_microvm`] disabled, this failed on the error assertion.
2102    /// Restored.
2103    #[tokio::test]
2104    async fn all_ingress_combined_with_shell_ingress_is_refused_before_the_wire() {
2105        let (plane, fake, _) = planted();
2106
2107        let payload = RunHookPayload::for_agent_token("agent-token").expect("fits");
2108        let mut request = RunMicrovmRequest::new("arn:image", payload);
2109        request
2110            .connectors
2111            .push(super::super::ConnectorIntent::ShellIngress);
2112        let error = plane
2113            .run_microvm(request)
2114            .await
2115            .expect_err("the invalid combination is refused locally");
2116
2117        assert!(
2118            error
2119                .to_string()
2120                .contains("HTTP_INGRESS and/or SHELL_INGRESS"),
2121            "the refusal names the pair that works: {error}"
2122        );
2123        assert_eq!(
2124            fake.call_count("RunMicrovm"),
2125            0,
2126            "nothing launched, so nothing billed"
2127        );
2128    }
2129
2130    /// **W1, measured constraint 1, second half.** `SHELL_INGRESS` without `HTTP_INGRESS`
2131    /// is the same launches-bills-fails-late shape as `ALL_INGRESS` plus a finer connector:
2132    /// the measured pair that launches and mints a shell token is `[HTTP_INGRESS,
2133    /// SHELL_INGRESS]` (`docs/PLATFORM.md`), no measurement says a shell-only ingress set
2134    /// even mints, and the platform accepts an invalid set at launch — the VM reaches
2135    /// RUNNING and bills before any refusal appears. Refused here instead, before the wire.
2136    ///
2137    /// **Falsification** — run 2026-08-31. This test was written before the guard clause
2138    /// existed and failed on both assertions (`run_microvm` reached the wire and launched);
2139    /// adding the shell-without-HTTP refusal in `run_microvm` turned it green.
2140    #[tokio::test]
2141    async fn shell_ingress_without_http_ingress_is_refused_before_the_wire() {
2142        let (plane, fake, _) = planted();
2143
2144        let payload = RunHookPayload::for_agent_token("agent-token").expect("fits");
2145        let mut request = RunMicrovmRequest::new("arn:image", payload);
2146        request.connectors = vec![
2147            super::super::ConnectorIntent::ShellIngress,
2148            super::super::ConnectorIntent::Egress,
2149        ];
2150        let error = plane
2151            .run_microvm(request)
2152            .await
2153            .expect_err("shell ingress without HTTP ingress is refused locally");
2154
2155        assert!(
2156            error.to_string().contains("HTTP_INGRESS"),
2157            "the refusal names the missing connector: {error}"
2158        );
2159        assert_eq!(
2160            fake.call_count("RunMicrovm"),
2161            0,
2162            "nothing launched, so nothing billed"
2163        );
2164    }
2165
2166    /// A page the service throttles is retried, not the end of the listing. The service
2167    /// sent that throttle as HTTP 400 named only by the `x-amzn-ErrorType` header
2168    /// (docs/PLATFORM.md, 2026-10-02), and a fleet of 140 VMs, fourteen pages read back to
2169    /// back, met it on most listings, each of which then failed as a validation refusal.
2170    ///
2171    /// **Falsification** — run 2026-10-02. Guard the classifier's header arm with `false` and
2172    /// the listing stops at the throttled page with the 400 as its error, so `expect` panics
2173    /// (`guards:fire -- --only control-listing-stops-at-a-throttled-page`). Restored.
2174    #[tokio::test]
2175    async fn the_fleet_listing_retries_a_page_the_service_throttled_as_400() {
2176        let (plane, fake, _) = planted();
2177        fake.answer(
2178            "ListMicrovms",
2179            Answer::ok(fake::list_microvms_page(
2180                &["mvm-page1-a"],
2181                Some("fleet-page-2"),
2182            )),
2183        )
2184        .answer("ListMicrovms", Answer::throttled_as_400())
2185        .answer(
2186            "ListMicrovms",
2187            Answer::ok(fake::list_microvms_page(&["mvm-page2-a"], None)),
2188        );
2189
2190        let fleet = plane
2191            .list_microvms()
2192            .await
2193            .expect("lists past the throttle");
2194        let ids: Vec<&str> = fleet.iter().map(|vm| vm.microvm_id.as_str()).collect();
2195        assert_eq!(ids, ["mvm-page1-a", "mvm-page2-a"]);
2196        assert_eq!(
2197            fake.call_count("ListMicrovms"),
2198            3,
2199            "page two was asked for again after the throttle"
2200        );
2201        let paths = fake.paths();
2202        assert_eq!(paths[1], paths[2], "the retry is the identical request");
2203    }
2204
2205    /// **Issue #23.** The fleet listing follows `nextToken`, so a fleet larger than one page
2206    /// is not silently truncated.
2207    ///
2208    /// One page is a **confidently wrong** answer rather than a missing one: the caller gets a
2209    /// list that looks complete, and a VM absent from it is a VM nothing here will terminate.
2210    ///
2211    /// The assertion is the fleet **length** and the presence of the page-two ids, which is
2212    /// what a test against the single-page code cannot satisfy.
2213    ///
2214    /// **Falsification** — run 2026-08-15. Replace the `Some(token)` arm with
2215    /// `Some(_) => return Ok(items)` and this fails with 2 VMs instead of 3, and both
2216    /// page-two `contains` assertions go red. Restored.
2217    #[tokio::test]
2218    async fn the_fleet_listing_follows_next_token_to_the_vms_on_page_two() {
2219        let (plane, fake, _) = planted();
2220        fake.answer(
2221            "ListMicrovms",
2222            Answer::ok(fake::list_microvms_page(
2223                &["mvm-page1-a", "mvm-page1-b"],
2224                Some("fleet-page-2"),
2225            )),
2226        )
2227        .answer(
2228            "ListMicrovms",
2229            Answer::ok(fake::list_microvms_page(&["mvm-page2-a"], None)),
2230        );
2231
2232        let fleet = plane.list_microvms().await.expect("lists");
2233        assert_eq!(
2234            fleet.len(),
2235            3,
2236            "a fleet spanning two pages is three VMs, not two"
2237        );
2238
2239        let ids: Vec<&str> = fleet.iter().map(|vm| vm.microvm_id.as_str()).collect();
2240        assert!(ids.contains(&"mvm-page2-a"), "page two is missing: {ids:?}");
2241        assert!(ids.contains(&"mvm-page1-a"), "{ids:?}");
2242        assert_eq!(fake.call_count("ListMicrovms"), 2, "both pages were read");
2243
2244        let paths = fake.paths();
2245        assert!(
2246            paths[1].contains("nextToken=fleet-page-2"),
2247            "the second request must carry the first page's token: {}",
2248            paths[1]
2249        );
2250        assert!(
2251            !paths[0].contains('?'),
2252            "the first request carries no cursor: {}",
2253            paths[0]
2254        );
2255    }
2256
2257    /// **Issue #25.** The `idlePolicy` the service reports is carried rather than dropped.
2258    ///
2259    /// The comment on [`ops::IdlePolicy`] used to claim `suspendedDurationSeconds` "exists
2260    /// only in the request". A live `GetMicrovm` on a RUNNING VM disagrees, which is what the
2261    /// fake's body now reflects — so this test is over a response shape that was measured
2262    /// rather than assumed.
2263    ///
2264    /// **Falsification** — drop `idle_policy` from the `From<MicrovmResponseWire>` impl and
2265    /// this does not compile; set it to `None` there and every field assertion goes red.
2266    #[tokio::test]
2267    async fn the_reported_idle_policy_is_carried_rather_than_dropped() {
2268        let (plane, fake, _) = planted();
2269        fake.answer(
2270            "GetMicrovm",
2271            Answer::ok(fake::microvm_response("RUNNING", None)),
2272        );
2273
2274        let vm = plane.get_microvm("mvm-1").await.expect("reads");
2275        let policy = vm
2276            .idle_policy
2277            .expect("GetMicrovm returns an idlePolicy, measured 2026-08-15");
2278        assert_eq!(policy.max_idle_duration_seconds, 1800);
2279        assert_eq!(
2280            policy.suspended_duration_seconds, 600,
2281            "the member the comment claimed was request-only"
2282        );
2283        assert!(!policy.auto_resume_enabled);
2284    }
2285
2286    /// A response with no `idlePolicy` still parses, and the absence is `None` rather than a
2287    /// default. The model does not mark the member required on either response shape, so an
2288    /// invented `0` would be a window nobody configured.
2289    #[tokio::test]
2290    async fn an_absent_idle_policy_is_none_rather_than_a_zero_window() {
2291        let (plane, fake, _) = planted();
2292        fake.answer(
2293            "GetMicrovm",
2294            Answer::ok(
2295                r#"{
2296                    "microvmId": "mvm-1",
2297                    "state": "RUNNING",
2298                    "endpoint": "https://e",
2299                    "imageArn": "arn:aws:lambda:us-east-1:123456789012:microvm-image:img",
2300                    "imageVersion": "1"
2301                }"#,
2302            ),
2303        );
2304
2305        let vm = plane.get_microvm("mvm-1").await.expect("parses");
2306        assert_eq!(vm.idle_policy, None);
2307    }
2308
2309    /// A transport failure is retried and then succeeds, so a connection reset on the way to
2310    /// a launch is not a failed launch.
2311    #[tokio::test]
2312    async fn a_transport_failure_is_retried_rather_than_reported() {
2313        let (plane, fake, _) = planted();
2314        fake.fail_transport("GetMicrovm", 2).answer(
2315            "GetMicrovm",
2316            Answer::ok(fake::microvm_response("RUNNING", None)),
2317        );
2318
2319        let vm = plane
2320            .get_microvm("mvm-1")
2321            .await
2322            .expect("the retry succeeds");
2323        assert_eq!(vm.state, "RUNNING");
2324        assert_eq!(
2325            fake.call_count("GetMicrovm"),
2326            3,
2327            "two failures then a success"
2328        );
2329    }
2330
2331    // ── issue #24's guards on the launch and lifecycle paths ─────────────────
2332
2333    /// **`max_idle_sec: 59` no longer reaches the wire.**
2334    ///
2335    /// The measurement issue #24 made, turned into a test. The exemption it replaces was not a
2336    /// wrong number — it was a correct fact about botocore's `VALIDATED_METADATA_ATTRS` applied to
2337    /// a client that does not use botocore, so `IdlePolicy.maxIdleDurationSeconds` was the one
2338    /// constraint in the model with a *reason* for having no guard rather than an oversight.
2339    ///
2340    /// The fake has `RunMicrovm` answered, so a missing guard shows as a launch that **succeeded**
2341    /// with 59 on the wire rather than as some other failure. That is what makes the zero-call
2342    /// assertion the load-bearing one.
2343    ///
2344    /// **Guard proof** — run 2026-08-16. Delete `require_idle_duration` from `run_microvm` and
2345    /// this fails on `expect_err`, with the fake recording `RunMicrovm` once and
2346    /// `idlePolicy.maxIdleDurationSeconds: 59` in its body.
2347    #[tokio::test]
2348    async fn an_under_minimum_idle_duration_reaches_no_control_plane_call() {
2349        for under in [0, 1, 59] {
2350            let (plane, fake, _) = planted();
2351            fake.answer(
2352                "RunMicrovm",
2353                Answer::ok(fake::microvm_response("PENDING", None)),
2354            );
2355
2356            let payload = RunHookPayload::for_agent_token("token").expect("fits");
2357            let mut request = RunMicrovmRequest::new("arn:image", payload);
2358            request.max_idle_sec = under;
2359
2360            let error = plane
2361                .run_microvm(request)
2362                .await
2363                .expect_err("the model's min is 60");
2364            assert_eq!(error.kind(), ErrorKind::InvalidArg);
2365            let message = error.to_string();
2366            assert!(message.contains("maxIdleDurationSeconds"), "{message}");
2367            assert!(message.contains(&under.to_string()), "{message}");
2368            assert_eq!(
2369                fake.calls().len(),
2370                0,
2371                "{under} was refused locally; before this guard it was serialized and sent"
2372            );
2373        }
2374
2375        // 60 exactly does launch, so the boundary is inclusive and the guard is not refusing
2376        // every idle window.
2377        let (plane, fake, _) = planted();
2378        fake.answer(
2379            "RunMicrovm",
2380            Answer::ok(fake::microvm_response("PENDING", None)),
2381        );
2382        let payload = RunHookPayload::for_agent_token("token").expect("fits");
2383        let mut request = RunMicrovmRequest::new("arn:image", payload);
2384        request.max_idle_sec = 60;
2385        plane.run_microvm(request).await.expect("60 is the minimum");
2386        assert_eq!(
2387            fake.first_body("RunMicrovm")["idlePolicy"]["maxIdleDurationSeconds"],
2388            60
2389        );
2390    }
2391
2392    /// **The launch's `executionRoleArn` and `imageIdentifier`, refused before the call.**
2393    ///
2394    /// `executionRoleArn` is filled from an infra config on every real launch — the CLI reads it
2395    /// out of `MICROVM_EXECUTION_ROLE_ARN` — so a malformed one is a `ValidationException` about a
2396    /// member the caller never typed. Naming it locally is the whole difference.
2397    ///
2398    /// **Guard proof** — run 2026-08-16. Delete `require_valid_role_arn("executionRoleArn", …)`
2399    /// and the role rows launch successfully with the bad ARN on the wire; delete
2400    /// `require_valid_identifier` and the identifier rows do.
2401    #[tokio::test]
2402    async fn a_malformed_launch_role_or_identifier_reaches_no_control_plane_call() {
2403        /// What to break, aliased for the reason clippy's `type_complexity` gives — and the same
2404        /// alias `image.rs`'s create-path table uses, so the two read alike.
2405        type Mutate = fn(&mut RunMicrovmRequest);
2406        let rows: [(&str, Mutate, &str); 6] = [
2407            (
2408                "an execution role that is a bare name",
2409                |request| {
2410                    request.execution_role_arn = Some("execution-role".to_string());
2411                },
2412                "role *name*",
2413            ),
2414            (
2415                "an execution role with eleven account digits",
2416                |request| {
2417                    request.execution_role_arn =
2418                        Some("arn:aws:iam::12345678901:role/exec".to_string());
2419                },
2420                "exactly twelve digits",
2421            ),
2422            (
2423                "an execution role that is a function ARN",
2424                |request| {
2425                    request.execution_role_arn =
2426                        Some("arn:aws:lambda:us-east-1:123456789012:function:h".to_string());
2427                },
2428                "RoleArn pattern",
2429            ),
2430            (
2431                "an empty image identifier",
2432                |request| {
2433                    request.image_identifier = String::new();
2434                },
2435                "collapses the path",
2436            ),
2437            (
2438                "a 257-character image identifier",
2439                |request| {
2440                    request.image_identifier = "a".repeat(257);
2441                },
2442                "MicrovmImageArn permits 2048",
2443            ),
2444            (
2445                "an image version with a newline",
2446                |request| {
2447                    request.image_version = Some("2.0\n".to_string());
2448                },
2449                "contains whitespace",
2450            ),
2451        ];
2452
2453        for (label, mutate, expected) in rows {
2454            let (plane, fake, _) = planted();
2455            fake.answer(
2456                "RunMicrovm",
2457                Answer::ok(fake::microvm_response("PENDING", None)),
2458            );
2459
2460            let payload = RunHookPayload::for_agent_token("token").expect("fits");
2461            let mut request = RunMicrovmRequest::new("arn:image", payload);
2462            mutate(&mut request);
2463
2464            let error = plane
2465                .run_microvm(request)
2466                .await
2467                .expect_err(&format!("{label} must be refused"));
2468            assert_eq!(error.kind(), ErrorKind::InvalidArg, "{label}");
2469            assert!(
2470                error.to_string().contains(expected),
2471                "{label}: wanted {expected:?}, got {error}"
2472            );
2473            assert_eq!(fake.calls().len(), 0, "{label}");
2474        }
2475
2476        // The control case: a launch carrying a real execution role, a pinned version, and the
2477        // account's actual ARN shape goes through.
2478        let (plane, fake, _) = planted();
2479        fake.answer(
2480            "RunMicrovm",
2481            Answer::ok(fake::microvm_response("PENDING", None)),
2482        );
2483        let payload = RunHookPayload::for_agent_token("token").expect("fits");
2484        let mut request = RunMicrovmRequest::new(
2485            "arn:aws:lambda:us-east-1:392583147479:microvm-image:agentd-conformance",
2486            payload,
2487        );
2488        request.execution_role_arn =
2489            Some("arn:aws:iam::392583147479:role/bonk-sandbox-microvm-execution".to_string());
2490        request.image_version = Some("1.0".to_string());
2491        plane
2492            .run_microvm(request)
2493            .await
2494            .expect("a realistic pinned launch with a real execution role");
2495        assert_eq!(fake.call_count("RunMicrovm"), 1);
2496    }
2497
2498    /// **The `microvmIdentifier` guard on every lifecycle operation, proved by the call count.**
2499    ///
2500    /// `TerminateMicrovm` is the one worth reading twice. Its path is
2501    /// `DELETE /microvms/{microvmIdentifier}`, so an empty identifier is a `DELETE` addressed at
2502    /// the fleet collection. The service has no operation there and refuses it, but "send a delete
2503    /// at the collection and trust the service" is not a thing to leave to the service.
2504    ///
2505    /// **Guard proof** — run 2026-08-16. Delete `require_valid_identifier` from any one of the
2506    /// five and its assertion goes red with `calls: 1`.
2507    #[tokio::test]
2508    async fn no_lifecycle_operation_sends_an_empty_or_over_long_microvm_identifier() {
2509        for bad in ["", &"a".repeat(257)] {
2510            let (plane, fake, _) = planted();
2511            // Every operation answered, so a missing guard is a *successful* call rather than a
2512            // different failure.
2513            fake.answer(
2514                "GetMicrovm",
2515                Answer::ok(fake::microvm_response("RUNNING", None)),
2516            );
2517            fake.answer("SuspendMicrovm", Answer::ok("{}"));
2518            fake.answer("ResumeMicrovm", Answer::ok("{}"));
2519            fake.answer("TerminateMicrovm", Answer::ok("{}"));
2520            fake.answer(
2521                "CreateMicrovmAuthToken",
2522                Answer::ok(fake::auth_token_response("t")),
2523            );
2524
2525            plane.get_microvm(bad).await.expect_err("GetMicrovm");
2526            plane.suspend(bad).await.expect_err("SuspendMicrovm");
2527            plane.resume(bad).await.expect_err("ResumeMicrovm");
2528            plane.terminate(bad).await.expect_err("TerminateMicrovm");
2529            plane
2530                .mint_auth_token(bad)
2531                .await
2532                .expect_err("CreateMicrovmAuthToken");
2533            plane
2534                .wait_for_running(bad, WaitOpts::for_launch())
2535                .await
2536                .expect_err("GetMicrovm, in a loop");
2537
2538            assert_eq!(
2539                fake.calls().len(),
2540                0,
2541                "six operations refused {bad:?} locally. An empty identifier on the terminate \
2542                 path is a DELETE addressed at the collection."
2543            );
2544        }
2545    }
2546
2547    /// **Issue #24's `PortNumber` half: a token is never minted for port 0.**
2548    ///
2549    /// The measurement that makes this worth a guard rather than a comment is in
2550    /// [`ops::PortSpecification`]: a token whose `allowedPorts` does not cover the port a request
2551    /// names is refused with **403 `Access to port denied`**, and on the WebSocket path it is close
2552    /// code 1006 with no reason. So a token minted for port 0 authorizes nothing and fails in a
2553    /// way that reads like a bad credential rather than like an empty scope.
2554    ///
2555    /// Both ends of a range are checked as well as the single-port form, because `PortRange`'s two
2556    /// members name the same shape and a range starting at 0 is the same defect.
2557    ///
2558    /// **Guard proof** — run 2026-08-16. Delete the `for spec in ports` loop from
2559    /// `mint_auth_token_for` and all three rows mint successfully with `{"port": 0}` or
2560    /// `{"range": {"startPort": 0, …}}` on the wire.
2561    #[tokio::test]
2562    async fn no_proxy_token_is_minted_for_port_zero() {
2563        let rows: [(&str, ops::PortSpecification, &str); 3] = [
2564            (
2565                "a single port of 0",
2566                ops::PortSpecification::port(0),
2567                "allowedPorts[].port",
2568            ),
2569            (
2570                "a range starting at 0",
2571                ops::PortSpecification::range(0, 9000),
2572                "allowedPorts[].range.startPort",
2573            ),
2574            (
2575                "a range ending at 0",
2576                ops::PortSpecification::range(0, 0),
2577                "allowedPorts[].range.startPort",
2578            ),
2579        ];
2580
2581        for (label, spec, member) in rows {
2582            let (plane, fake, _) = planted();
2583            fake.answer(
2584                "CreateMicrovmAuthToken",
2585                Answer::ok(fake::auth_token_response("t")),
2586            );
2587
2588            let error = plane
2589                .mint_auth_token_for("mvm-1", &[spec])
2590                .await
2591                .expect_err(&format!("{label} must be refused"));
2592            assert_eq!(error.kind(), ErrorKind::InvalidArg, "{label}");
2593            let message = error.to_string();
2594            assert!(message.contains(member), "{label}: {message}");
2595            assert!(
2596                message.contains("authorizes nothing"),
2597                "{label}: the 403 consequence has to be in the message: {message}"
2598            );
2599            assert_eq!(fake.calls().len(), 0, "{label}");
2600        }
2601
2602        // A zero port among legal ones is still refused: the check is per-entry, not on the
2603        // first. A token minted with `[{port: 9000}, {port: 0}]` would look scoped correctly and
2604        // carry a member the service refuses the whole request over.
2605        let (plane, fake, _) = planted();
2606        fake.answer(
2607            "CreateMicrovmAuthToken",
2608            Answer::ok(fake::auth_token_response("t")),
2609        );
2610        plane
2611            .mint_auth_token_for(
2612                "mvm-1",
2613                &[
2614                    ops::PortSpecification::port(9000),
2615                    ops::PortSpecification::port(0),
2616                ],
2617            )
2618            .await
2619            .expect_err("every entry is checked, not the first");
2620        assert_eq!(fake.calls().len(), 0);
2621
2622        // And the three legal forms mint, including `allPorts` — which names no number, so there
2623        // is nothing to bound and the guard must not refuse it.
2624        for legal in [
2625            ops::PortSpecification::port(1),
2626            ops::PortSpecification::port(65_535),
2627            ops::PortSpecification::range(1, 65_535),
2628            ops::PortSpecification::all(),
2629        ] {
2630            let (plane, fake, _) = planted();
2631            fake.answer(
2632                "CreateMicrovmAuthToken",
2633                Answer::ok(fake::auth_token_response("t")),
2634            );
2635            plane
2636                .mint_auth_token_for("mvm-1", &[legal])
2637                .await
2638                .expect("a legal port specification mints");
2639            assert_eq!(fake.call_count("CreateMicrovmAuthToken"), 1);
2640        }
2641    }
2642
2643    /// **#195.** A launch whose client token adopted a VM that idle-suspended since the
2644    /// first attempt resumes it and returns RUNNING, rather than reporting LaunchDied.
2645    ///
2646    /// **Falsification** — route `wait_for_launch` through `TERMINAL_STATES` (the fresh
2647    /// launch's fail set) and this reports LaunchDied on the first SUSPENDED poll.
2648    #[tokio::test]
2649    async fn an_adopted_launch_that_finds_its_vm_suspended_resumes_it() {
2650        for first in ["SUSPENDING", "SUSPENDED"] {
2651            let (plane, fake, _) = planted();
2652            fake.answer(
2653                "GetMicrovm",
2654                Answer::ok(fake::microvm_response(first, None)),
2655            )
2656            .answer(
2657                "GetMicrovm",
2658                Answer::ok(fake::microvm_response("SUSPENDED", None)),
2659            )
2660            .answer(
2661                "GetMicrovm",
2662                Answer::ok(fake::microvm_response("SUSPENDED", None)),
2663            )
2664            .answer(
2665                "GetMicrovm",
2666                Answer::ok(fake::microvm_response("RUNNING", None)),
2667            )
2668            .answer("ResumeMicrovm", Answer::ok(fake::empty_response()));
2669            let running = plane
2670                .wait_for_launch("mvm-abc123", WaitOpts::for_launch())
2671                .await
2672                .unwrap_or_else(|error| panic!("{first}: {error}"));
2673            assert_eq!(running.state, "RUNNING", "{first}");
2674            assert_eq!(
2675                fake.call_count("ResumeMicrovm"),
2676                1,
2677                "{first}: one resume, even though GetMicrovm still read SUSPENDED after it"
2678            );
2679        }
2680    }
2681
2682    /// The adopted-launch wait still fails fast, with `stateReason`, on a dead state.
2683    #[tokio::test]
2684    async fn an_adopted_launch_still_fails_fast_on_a_dead_state() {
2685        for state in crate::constants::DEAD_STATES {
2686            let (plane, fake, _) = planted();
2687            fake.answer(
2688                "GetMicrovm",
2689                Answer::ok(fake::microvm_response(state, Some("hook timeout"))),
2690            );
2691            let error = plane
2692                .wait_for_launch("mvm-abc123", WaitOpts::for_launch())
2693                .await
2694                .expect_err(&format!("{state} is a death"));
2695            assert_eq!(error.kind(), ErrorKind::LaunchDied, "{state}");
2696            assert!(
2697                error.to_string().contains("hook timeout"),
2698                "{state}: {error}"
2699            );
2700            assert_eq!(fake.call_count("GetMicrovm"), 1, "{state}");
2701            assert_eq!(fake.call_count("ResumeMicrovm"), 0, "{state}");
2702        }
2703    }
2704
2705    /// `startedAt`, `terminatedAt`, and `maximumDurationInSeconds` reach [`Microvm`].
2706    #[tokio::test]
2707    async fn get_microvm_carries_the_lifetime_members() {
2708        let (plane, fake, _) = planted();
2709        fake.answer(
2710            "GetMicrovm",
2711            Answer::ok(fake::microvm_response("RUNNING", None)),
2712        );
2713        let vm = plane.get_microvm("mvm-abc123").await.expect("parses");
2714        assert_eq!(vm.maximum_duration_seconds, Some(3600));
2715        assert_eq!(
2716            vm.started_at,
2717            Some(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_754_524_800))
2718        );
2719        assert_eq!(vm.terminated_at, None);
2720    }
2721
2722    /// A timestamp past what a `Duration` holds reads as absent, as a negative one does, and
2723    /// the rest of the reply still parses. The conversion ran before the checked add, so such
2724    /// a reply panicked the parser instead (#335). The negative figure is last, so the fault
2725    /// below panics on an out-of-range one first.
2726    ///
2727    /// **Falsification**: `verify/guards/faults/seconds-flags.toml` entry
2728    /// `app-epoch-past-a-duration-panics` restores `Duration::from_secs_f64` in `epoch`, and
2729    /// the `1e300` reply panics.
2730    #[tokio::test]
2731    async fn an_out_of_range_timestamp_reads_as_absent() {
2732        for seconds in ["2e19", "1e300", "1.7976931348623157e308", "-1"] {
2733            let (plane, fake, _) = planted();
2734            let body = fake::microvm_response("TERMINATED", None).replace(
2735                r#""startedAt": 1754524800"#,
2736                &format!(r#""startedAt": {seconds}, "terminatedAt": {seconds}"#),
2737            );
2738            fake.answer("GetMicrovm", Answer::ok(body));
2739            let vm = plane
2740                .get_microvm("mvm-abc123")
2741                .await
2742                .unwrap_or_else(|error| panic!("{seconds}: {error}"));
2743            assert_eq!(vm.state, "TERMINATED", "{seconds}");
2744            assert_eq!(vm.maximum_duration_seconds, Some(3600), "{seconds}");
2745            assert_eq!(vm.started_at, None, "{seconds}");
2746            assert_eq!(vm.terminated_at, None, "{seconds}");
2747        }
2748    }
2749
2750    /// The list filters reach the query string in SigV4's sorted order, ARN colons escaped.
2751    #[tokio::test]
2752    async fn list_filters_reach_the_query_string() {
2753        let (plane, fake, _) = planted();
2754        fake.answer(
2755            "ListMicrovms",
2756            Answer::ok(fake::list_microvms_page(&["mvm-1"], None)),
2757        );
2758        let filter = MicrovmFilter {
2759            image_identifier: Some(
2760                "arn:aws:lambda:us-east-1:123456789012:microvm-image:img".into(),
2761            ),
2762            image_version: Some("1.0".into()),
2763        };
2764        let items = plane.list_microvms_matching(&filter).await.expect("lists");
2765        assert_eq!(items.len(), 1);
2766        let path = fake.paths().pop().expect("one call");
2767        assert!(
2768            path.ends_with(
2769                "/microvms?imageIdentifier=arn%3Aaws%3Alambda%3Aus-east-1%3A123456789012%3Amicrovm-image%3Aimg&imageVersion=1.0"
2770            ),
2771            "{path}"
2772        );
2773        plane.list_microvms().await.expect("unfiltered");
2774        assert!(fake.paths().pop().expect("second").ends_with("/microvms"));
2775    }
2776
2777    /// Per-VM logging reaches the wire, and a malformed group is refused before any call.
2778    #[tokio::test]
2779    async fn per_vm_logging_is_sent_and_checked_locally() {
2780        let (plane, fake, _) = planted();
2781        fake.answer(
2782            "RunMicrovm",
2783            Answer::ok(fake::microvm_response("PENDING", None)),
2784        );
2785        let payload = || RunHookPayload::for_agent_token("t").expect("fits");
2786        let mut request = RunMicrovmRequest::new("arn:image", payload());
2787        request.logging = Some(ops::Logging::cloud_watch("/team/agents", None));
2788        plane.run_microvm(request).await.expect("launches");
2789        assert_eq!(
2790            fake.first_body("RunMicrovm")["logging"],
2791            serde_json::json!({"cloudWatch": {"logGroup": "/team/agents"}})
2792        );
2793
2794        let mut disabled = RunMicrovmRequest::new("arn:image", payload());
2795        disabled.logging = Some(ops::Logging::disabled());
2796        plane.run_microvm(disabled).await.expect("launches");
2797        assert_eq!(
2798            fake.body_of("RunMicrovm", 1)["logging"],
2799            serde_json::json!({"disabled": {}})
2800        );
2801
2802        let plain = RunMicrovmRequest::new("arn:image", payload());
2803        plane.run_microvm(plain).await.expect("launches");
2804        assert!(fake.body_of("RunMicrovm", 2).get("logging").is_none());
2805
2806        for bad in [
2807            ops::Logging::cloud_watch("bad group!", None),
2808            ops::Logging::CloudWatch {
2809                cloud_watch: ops::CloudWatchLogging {
2810                    log_group: None,
2811                    log_stream: Some("s".into()),
2812                },
2813            },
2814        ] {
2815            let mut request = RunMicrovmRequest::new("arn:image", payload());
2816            request.logging = Some(bad);
2817            let error = plane.run_microvm(request).await.expect_err("refused");
2818            assert_eq!(error.kind(), ErrorKind::InvalidArg, "{error}");
2819        }
2820        assert_eq!(
2821            fake.call_count("RunMicrovm"),
2822            3,
2823            "the refusals cost no call"
2824        );
2825    }
2826}