a3s-box-runtime 3.2.3

MicroVM runtime engine — VM lifecycle, OCI images, attestation, networking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
//! VM Manager - Lifecycle management for MicroVM instances.

mod boot;
mod execution;
mod layout;
mod lifecycle;
mod maintenance;
mod network;
mod oci_microvm;
mod ready;
pub mod reap;
mod sandbox;
mod spec;
#[cfg(windows)]
mod windows_stop;

pub(crate) use layout::{
    legacy_sandbox_runtime_root, persistent_rootfs_generation_exists, runtime_socket_dir,
    sandbox_runtime_root,
};
pub use maintenance::archive_stopped_guest_native_rootfs;

use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Callback type for image pull progress: `(current, total, digest, size_bytes)`.
pub type PullProgressFn = Arc<dyn Fn(usize, usize, &str, i64) + Send + Sync>;

use a3s_box_core::config::BoxConfig;
#[cfg(unix)]
use a3s_box_core::config::TeeConfig;
use a3s_box_core::error::{BoxError, Result};
use a3s_box_core::event::{BoxEvent, EventEmitter};
use a3s_box_core::execution::ResolvedExecutionPlan;
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
use tracing::Instrument;

#[cfg(unix)]
use libc;

#[cfg(unix)]
use crate::grpc::ExecClient;
#[cfg(unix)]
use crate::tee::TeeExtension;
use crate::vmm::{VmController, VmHandler, VmmProvider, DEFAULT_SHUTDOWN_TIMEOUT_MS};

/// Box state machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BoxState {
    /// Config captured, no VM started
    Created,

    /// VM booted, container initialized, gRPC healthy
    Ready,

    /// A session is actively processing a prompt
    Busy,

    /// A session is compressing its context
    Compacting,

    /// VM terminated, resources freed
    Stopped,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
enum VmBootMode {
    #[default]
    Workload,
    RootfsMaintenance,
}

/// Layout of directories for a box instance.
pub(crate) struct BoxLayout {
    /// Host staging path for a fresh directory-derived generation.
    ///
    /// This path is intentionally non-authoritative when `resumed_rootfs` is
    /// present; directly assembled and resumed guest-owned disks have no host
    /// directory view.
    pub(crate) rootfs_path: PathBuf,
    /// Guest-owned generation finalized before the directory staging boundary.
    /// This covers direct OCI assembly and persistent restart.
    pub(crate) resumed_rootfs: Option<crate::rootfs::ResumedRootfs>,
    /// Path to the exec Unix socket
    pub(crate) exec_socket_path: PathBuf,
    /// Path to the PTY Unix socket
    pub(crate) pty_socket_path: PathBuf,
    /// Path to the attestation Unix socket
    pub(crate) attest_socket_path: PathBuf,
    /// Path to the CRI port-forward Unix socket
    pub(crate) port_forward_socket_path: PathBuf,
    /// Path to the workspace directory
    pub(crate) workspace_path: PathBuf,
    /// Path to console output file (optional)
    pub(crate) console_output: Option<PathBuf>,
    /// OCI image config (entrypoint, env, working dir, volumes)
    pub(crate) oci_config: Option<crate::oci::OciImageConfig>,
    /// Exact resolved OCI manifest behind a fresh image-derived generation.
    /// Snapshot and externally prebuilt roots intentionally have no reusable
    /// base identity until their own generation protocol is implemented.
    #[cfg(target_os = "macos")]
    pub(crate) oci_manifest_digest: Option<String>,
    /// Fresh image/cache rootfs generations must ignore any terminal manifest
    /// baked into an older malicious image. Persistent and Snapshot generations
    /// instead prefer the terminal manifest captured after guest writes.
    pub(crate) prefer_image_rootfs_metadata: bool,
    /// TEE instance configuration (if TEE is enabled)
    pub(crate) tee_instance_config: Option<crate::vmm::TeeInstanceConfig>,
}

#[cfg(target_os = "windows")]
const WINDOWS_GUEST_EXIT_CODE: &str = ".a3s_exit_code";
#[cfg(target_os = "windows")]
const WINDOWS_GUEST_STDOUT: &str = "guest-init.stdout.log";
#[cfg(target_os = "windows")]
const WINDOWS_GUEST_STDERR: &str = "guest-init.stderr.log";
#[cfg(target_os = "windows")]
const WINDOWS_STOP_DELIVERY_TIMEOUT_MS: u64 = 1_000;
#[cfg(target_os = "windows")]
const WINDOWS_GUEST_FINALIZATION_TIMEOUT_MS: u64 = 30_000;
#[cfg(target_os = "windows")]
const WINDOWS_GUEST_RESULT_MARKER: &str = ".a3s_host_result_collected";
#[cfg(target_os = "windows")]
const WINDOWS_LIVE_LOGS_DRAINED_MARKER: &str = ".a3s_host_live_logs_drained";

pub(crate) const TERMINAL_EXIT_POLL_INTERVAL: std::time::Duration =
    std::time::Duration::from_millis(25);
pub(crate) const TERMINAL_EXIT_POLL_TIMEOUT: std::time::Duration =
    std::time::Duration::from_secs(5);

/// Append a completed Windows guest stream to its raw host console, filtering
/// libkrun's pre-guest C-init diagnostics while preserving arbitrary bytes.
#[cfg(target_os = "windows")]
fn append_windows_guest_stream(
    source: &Path,
    destination: &Path,
    runtime_filter: &a3s_box_core::log::RuntimeConsoleFilter,
) -> std::io::Result<()> {
    use std::io::{BufRead, Write};

    let input = match a3s_box_core::windows_file::open_regular_file(source, None) {
        Ok((input, _)) => input,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
        Err(error) => return Err(error),
    };
    let mut reader = std::io::BufReader::new(input);
    let mut output = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(destination)?;
    let mut line = Vec::new();

    loop {
        line.clear();
        if reader.read_until(b'\n', &mut line)? == 0 {
            break;
        }
        let keep = !line.ends_with(b"\n")
            || std::str::from_utf8(&line).map_or(true, |line| runtime_filter.keep_line(line));
        if keep {
            output.write_all(&line)?;
        }
    }

    output.flush()
}

#[cfg(target_os = "windows")]
fn windows_marker_matches(path: &Path, expected: &[u8]) -> bool {
    use std::io::Read;

    let Ok((file, _)) = a3s_box_core::windows_file::open_regular_file(path, None) else {
        return false;
    };
    let mut contents = Vec::with_capacity(expected.len().saturating_add(1));
    if file
        .take(expected.len().saturating_add(1) as u64)
        .read_to_end(&mut contents)
        .is_err()
    {
        return false;
    }
    contents == expected
}

/// Read the durable workload status without treating it as provider completion.
///
/// The WHPX guest writes this file before libkrun necessarily returns to the
/// shim. Readiness and boot cleanup use its presence only to distinguish a
/// completed one-shot from a live guest that never became ready; normal wait
/// paths still wait for the shim to finish relaying logs before collecting it.
#[cfg(target_os = "windows")]
fn windows_guest_persisted_exit_code(box_dir: &Path) -> Option<i32> {
    use std::io::Read;

    let exit_path = box_dir.join("rootfs").join(WINDOWS_GUEST_EXIT_CODE);
    let (file, _) = a3s_box_core::windows_file::open_regular_file(&exit_path, None).ok()?;
    let mut contents = String::new();
    file.take(64).read_to_string(&mut contents).ok()?;
    contents.trim().parse::<i32>().ok()
}

/// Collect the completed WHPX guest result after the shim process has exited.
///
/// Current shims drain structured logs before exiting. The runtime still owns
/// raw-console collection and provides a completed-stream fallback for older
/// libkrun bundles that terminate the shim with `_exit`.
#[cfg(target_os = "windows")]
pub fn collect_windows_guest_result(
    box_dir: &Path,
    log_config: &a3s_box_core::log::LogConfig,
    shim_exit_code: i32,
) -> Result<i32> {
    let rootfs = box_dir.join("rootfs");
    let logs = box_dir.join("logs");
    let marker = rootfs.join(WINDOWS_GUEST_RESULT_MARKER);
    let live_logs_drained = rootfs.join(WINDOWS_LIVE_LOGS_DRAINED_MARKER);
    let stdout_source = rootfs.join(WINDOWS_GUEST_STDOUT);
    let stderr_source = rootfs.join(WINDOWS_GUEST_STDERR);

    if !windows_marker_matches(&marker, b"collected\n") {
        std::fs::create_dir_all(&logs)?;
        let runtime_filter = a3s_box_core::log::RuntimeConsoleFilter::new();

        for (source, destination) in [
            (&stdout_source, logs.join("console.log")),
            (&stderr_source, logs.join("console.err.log")),
        ] {
            append_windows_guest_stream(source, &destination, &runtime_filter).map_err(
                |error| BoxError::BoxBootError {
                    message: format!(
                        "Failed to collect Windows guest output {} into {}: {error}",
                        source.display(),
                        destination.display()
                    ),
                    hint: None,
                },
            )?;
        }

        // New Windows shims tail these sources live and drain them before exit.
        // Older libkrun bundles still terminate the shim with `_exit`, so keep
        // the completed-stream fallback when no drained marker exists. Process
        // the sources rather than the retained raw console to avoid replaying a
        // previous restart.
        if !windows_marker_matches(&live_logs_drained, b"drained\n") {
            let stopped = std::sync::atomic::AtomicBool::new(true);
            a3s_box_core::log::run_log_processor_streams(
                &stdout_source,
                &stderr_source,
                &logs,
                log_config,
                &stopped,
            );
        }

        a3s_box_core::windows_file::replace_regular_file(&marker, b"collected\n").map_err(
            |error| BoxError::BoxBootError {
                message: format!(
                    "Failed to mark the Windows guest result collected at {}: {error}",
                    marker.display()
                ),
                hint: None,
            },
        )?;
    }

    let exit_path = rootfs.join(WINDOWS_GUEST_EXIT_CODE);
    let contents = match a3s_box_core::windows_file::open_regular_file(&exit_path, None) {
        Ok((file, _)) => {
            use std::io::Read;
            let mut contents = String::new();
            file.take(64)
                .read_to_string(&mut contents)
                .map_err(|error| BoxError::BoxBootError {
                    message: format!(
                        "Failed to read the Windows guest exit code {}: {error}",
                        exit_path.display()
                    ),
                    hint: None,
                })?;
            contents
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound && shim_exit_code != 0 => {
            return Ok(shim_exit_code);
        }
        Err(error) => {
            return Err(BoxError::BoxBootError {
                message: if error.kind() == std::io::ErrorKind::NotFound {
                    format!(
                        "WHPX stopped before the guest persisted its exit code ({})",
                        exit_path.display()
                    )
                } else {
                    format!(
                        "Failed to read the Windows guest exit code {}: {error}",
                        exit_path.display()
                    )
                },
                hint: Some(
                    "Inspect logs/init-rust.log and the shim log for the guest boot failure"
                        .to_string(),
                ),
            });
        }
    };

    contents
        .trim()
        .parse::<i32>()
        .map_err(|error| BoxError::BoxBootError {
            message: format!(
                "Invalid Windows guest exit code in {}: {error}",
                exit_path.display()
            ),
            hint: None,
        })
}

/// VM manager - orchestrates VM lifecycle.
pub struct VmManager {
    /// Box configuration
    pub(crate) config: BoxConfig,

    /// Unique box identifier
    pub(crate) box_id: String,

    /// Internal boot contract. Maintenance never becomes persisted box state.
    boot_mode: VmBootMode,

    /// Current state
    pub(crate) state: Arc<RwLock<BoxState>>,

    /// Event emitter
    pub(crate) event_emitter: EventEmitter,

    /// VMM provider (spawns VMs via pluggable backend)
    pub(crate) provider: Option<Box<dyn VmmProvider>>,

    /// VM handler (runtime operations on running VM)
    pub(crate) handler: Arc<RwLock<Option<Box<dyn VmHandler>>>>,

    /// Exec client for executing commands in the guest
    #[cfg(unix)]
    pub(crate) exec_client: Option<ExecClient>,

    /// Network backend manager for bridge networking (None if TSI mode).
    /// Platform-specific: passt on Linux, gvproxy on macOS.
    pub(crate) net_manager: Option<Box<dyn crate::network::NetworkBackend>>,

    /// A3S home directory (~/.a3s)
    pub(crate) home_dir: PathBuf,

    /// Anonymous volume names created during boot (from OCI VOLUME directives)
    pub(crate) anonymous_volumes: Vec<String>,

    /// Anonymous volumes newly created by the current boot attempt.
    ///
    /// Reused anonymous volumes must survive failed restarts because they may
    /// contain data from an existing stopped box.
    pub(crate) created_anonymous_volumes: Vec<String>,

    /// OCI image config resolved during the last successful boot.
    pub(crate) image_config: Option<crate::oci::OciImageConfig>,

    /// Exact rootfs cache entry paired with a snapshot-fork template.
    ///
    /// The template's guest memory and filesystem must come from the same
    /// resolved image even when its configured tag moves later.
    pub(crate) restore_rootfs_cache_key: Option<String>,

    /// Suppress an image-defined health check for callers that explicitly
    /// requested Docker-compatible `--no-healthcheck` semantics.
    pub(crate) healthcheck_disabled: bool,

    /// Whether this boot attempt started with an existing persistent rootfs
    /// generation. Failed first boots may discard a partial extraction, while
    /// failed restarts must retain the pre-existing guest data.
    pub(crate) preserve_rootfs_on_boot_failure: bool,

    /// TEE extension (attestation, sealing, secret injection)
    #[cfg(unix)]
    pub(crate) tee: Option<Box<dyn TeeExtension>>,

    /// Rootfs preparation and transport provider.
    pub(crate) rootfs_provider: Box<dyn crate::rootfs::RootfsProvider>,

    /// Path to the exec Unix socket (set after boot)
    pub(crate) exec_socket_path: Option<PathBuf>,

    /// Path to the PTY Unix socket (set after boot)
    pub(crate) pty_socket_path: Option<PathBuf>,

    /// Path to the CRI port-forward Unix socket (set after boot)
    pub(crate) port_forward_socket_path: Option<PathBuf>,

    /// Prometheus metrics (optional, for instrumented deployments).
    pub(crate) prom: Option<crate::prom::RuntimeMetrics>,

    /// Exit code captured from the shim process after it exits.
    pub(crate) shim_exit_code: Option<i32>,

    /// Optional progress callback for image pulls: `(current, total, digest, size_bytes)`.
    pub(crate) pull_progress_fn: Option<PullProgressFn>,

    /// Logging driver config, threaded into the InstanceSpec so the shim runs
    /// the log processor for the box's lifetime (set by the CLI via
    /// [`VmManager::set_log_config`]).
    pub(crate) log_config: a3s_box_core::log::LogConfig,

    /// Backend-neutral resolution captured before any boot side effects.
    pub(crate) resolved_execution_plan: Option<ResolvedExecutionPlan>,

    /// Runtime-owned tmpfs root whose regular files may be prepared for the
    /// Sandbox user namespace. Arbitrary external bind mounts remain immutable.
    pub(crate) managed_secret_root: Option<PathBuf>,

    /// One in-memory registry authorization selected for this boot attempt.
    /// It is consumed and zeroized while preparing the image layout.
    pub(crate) transient_registry_auth: Option<crate::oci::RegistryAuth>,
}

impl VmManager {
    /// Create a new VM manager.
    pub fn new(config: BoxConfig, event_emitter: EventEmitter) -> Self {
        let box_id = uuid::Uuid::new_v4().to_string();
        let home_dir = a3s_box_core::dirs_home();
        let rootfs_provider =
            crate::rootfs::default_provider_for_boot(rootfs_snapshot_requested(&config));

        Self {
            config,
            box_id,
            boot_mode: VmBootMode::Workload,
            state: Arc::new(RwLock::new(BoxState::Created)),
            event_emitter,
            provider: None,
            handler: Arc::new(RwLock::new(None)),
            #[cfg(unix)]
            exec_client: None,
            net_manager: None,
            home_dir,
            anonymous_volumes: Vec::new(),
            created_anonymous_volumes: Vec::new(),
            image_config: None,
            restore_rootfs_cache_key: None,
            healthcheck_disabled: false,
            preserve_rootfs_on_boot_failure: false,
            #[cfg(unix)]
            tee: None,
            rootfs_provider,
            exec_socket_path: None,
            pty_socket_path: None,
            port_forward_socket_path: None,
            prom: None,
            shim_exit_code: None,
            pull_progress_fn: None,
            log_config: a3s_box_core::log::LogConfig::default(),
            resolved_execution_plan: None,
            managed_secret_root: None,
            transient_registry_auth: None,
        }
    }

    /// Create a new VM manager with a specific box ID.
    pub fn with_box_id(config: BoxConfig, event_emitter: EventEmitter, box_id: String) -> Self {
        let home_dir = a3s_box_core::dirs_home();
        let rootfs_provider = crate::rootfs::default_provider_for_box_boot(
            &home_dir.join("boxes").join(&box_id),
            rootfs_snapshot_requested(&config),
        );

        Self {
            config,
            box_id,
            boot_mode: VmBootMode::Workload,
            state: Arc::new(RwLock::new(BoxState::Created)),
            event_emitter,
            provider: None,
            handler: Arc::new(RwLock::new(None)),
            #[cfg(unix)]
            exec_client: None,
            net_manager: None,
            home_dir,
            anonymous_volumes: Vec::new(),
            created_anonymous_volumes: Vec::new(),
            image_config: None,
            restore_rootfs_cache_key: None,
            healthcheck_disabled: false,
            preserve_rootfs_on_boot_failure: false,
            #[cfg(unix)]
            tee: None,
            rootfs_provider,
            exec_socket_path: None,
            pty_socket_path: None,
            port_forward_socket_path: None,
            prom: None,
            shim_exit_code: None,
            pull_progress_fn: None,
            log_config: a3s_box_core::log::LogConfig::default(),
            resolved_execution_plan: None,
            managed_secret_root: None,
            transient_registry_auth: None,
        }
    }

    /// Remove host-side boot artifacts after a failed boot attempt.
    async fn cleanup_boot_failure(&mut self) {
        let box_dir = self.home_dir.join("boxes").join(&self.box_id);

        #[cfg(target_os = "windows")]
        let guest_exit_before_cleanup = windows_guest_persisted_exit_code(&box_dir);

        if let Some(mut handler) = self.handler.write().await.take() {
            // A short-lived workload can finish before the runtime publishes
            // its readiness endpoint. That is a normal terminal completion,
            // not a failed rootfs build. Preserve its writable generation so a
            // managed restart observes the same persistent filesystem while
            // ephemeral mounts are recreated by the next runtime generation.
            // Process termination and publication of the exact wait result are
            // separate events. Try once before cleanup, let `stop` collect its
            // owned child, then wait within the common terminal bound only when
            // the workload was already known to have exited naturally. A live
            // boot failure stopped by Box must not be reclassified as success.
            let exited_before_cleanup = handler.has_exited();
            let collected_before_cleanup = match handler.try_wait_exit() {
                Ok(Some(exit_code)) => {
                    self.shim_exit_code = Some(exit_code);
                    true
                }
                Ok(None) => false,
                Err(error) => {
                    tracing::debug!(
                        box_id = %self.box_id,
                        error = %error,
                        "Failed to collect a terminal status before boot cleanup"
                    );
                    false
                }
            };
            if let Err(error) = handler.stop(default_stop_signal(), DEFAULT_SHUTDOWN_TIMEOUT_MS) {
                tracing::warn!(
                    box_id = %self.box_id,
                    error = %error,
                    "Failed to stop VM handler after boot failure"
                );
            }
            let provider_exit_code = handler.exit_code().or(self.shim_exit_code);
            #[cfg(not(target_os = "windows"))]
            {
                self.shim_exit_code =
                    crate::rootfs::resolve_workload_exit_code(&box_dir, provider_exit_code);
            }
            #[cfg(target_os = "windows")]
            {
                let completed_before_cleanup = collected_before_cleanup
                    || exited_before_cleanup
                    || guest_exit_before_cleanup.is_some();
                if completed_before_cleanup {
                    let fallback_exit_code = guest_exit_before_cleanup.or(provider_exit_code);
                    if let Some(fallback_exit_code) = fallback_exit_code {
                        match collect_windows_guest_result(
                            &box_dir,
                            &self.log_config,
                            fallback_exit_code,
                        ) {
                            Ok(exit_code) => self.shim_exit_code = Some(exit_code),
                            Err(error) => {
                                tracing::warn!(
                                    box_id = %self.box_id,
                                    error = %error,
                                    "Failed to collect the completed Windows guest during boot cleanup"
                                );
                                self.shim_exit_code =
                                    guest_exit_before_cleanup.or(provider_exit_code);
                            }
                        }
                    } else {
                        // A provider can report process exit before its owned
                        // child status becomes collectable. Keep the status
                        // pending so the delayed terminal poll below can reap it.
                        self.shim_exit_code = None;
                    }
                } else {
                    self.shim_exit_code = provider_exit_code;
                }
            }
            if exited_before_cleanup && self.shim_exit_code.is_none() {
                self.shim_exit_code =
                    wait_for_delayed_terminal_exit(handler.as_mut(), &box_dir, &self.box_id).await;
            }
            let completed_before_cleanup = collected_before_cleanup || exited_before_cleanup;
            #[cfg(target_os = "windows")]
            let completed_before_cleanup =
                completed_before_cleanup || guest_exit_before_cleanup.is_some();
            if self.config.persistent && self.shim_exit_code.is_some() && completed_before_cleanup {
                self.preserve_rootfs_on_boot_failure = true;
            }
        }

        if let Some(mut net_manager) = self.net_manager.take() {
            net_manager.stop();
        }

        self.cleanup_created_anonymous_volumes();
        self.cleanup_box_dir();
    }

    fn cleanup_created_anonymous_volumes(&mut self) {
        if self.created_anonymous_volumes.is_empty() {
            return;
        }

        let created = std::mem::take(&mut self.created_anonymous_volumes);
        let created_set: std::collections::HashSet<_> = created.iter().cloned().collect();
        let store = crate::volume::VolumeStore::new(
            self.home_dir.join("volumes.json"),
            self.home_dir.join("volumes"),
        );

        for volume_name in &created {
            if let Err(error) = store.remove_anonymous(volume_name, &self.box_id) {
                tracing::debug!(
                    box_id = %self.box_id,
                    volume = volume_name,
                    error = %error,
                    "Failed to remove anonymous volume after boot failure"
                );
            }
        }

        self.anonymous_volumes
            .retain(|name| !created_set.contains(name));
    }

    /// Remove transient host boot artifacts, retaining persistent guest data.
    fn cleanup_box_dir(&self) {
        let box_dir = self.home_dir.join("boxes").join(&self.box_id);
        let socket_dir = self.socket_dir();
        let mount_aliases_clean = match self.cleanup_sandbox_mount_aliases() {
            Ok(()) => true,
            Err(error) => {
                tracing::warn!(
                    box_id = %self.box_id,
                    %error,
                    "Failed to cleanup Sandbox attachment aliases after boot failure"
                );
                false
            }
        };

        // Reap the box's passt daemon (Linux bridge mode) BEFORE removing its
        // socket dir. A boot that fails after passt spawned but before
        // `self.net_manager` was assigned leaves `net_manager.stop()` a no-op, so
        // passt would otherwise survive holding the published port — the
        // "Address already in use" on the next start. terminate_passt reads
        // `socket_dir/passt.pid` and is a no-op when there is no passt.
        #[cfg(target_os = "linux")]
        crate::network::terminate_passt(&self.socket_dir());

        let preserve_rootfs_on_boot_failure = self.preserve_rootfs_on_boot_failure
            || self.rootfs_provider.preserve_on_boot_failure(&box_dir);
        if let Err(error) = self
            .rootfs_provider
            .cleanup(&box_dir, preserve_rootfs_on_boot_failure)
        {
            tracing::warn!(
                box_id = %self.box_id,
                path = %box_dir.display(),
                error = %error,
                "Failed to cleanup rootfs provider after boot failure"
            );
        }

        match std::fs::remove_dir_all(&socket_dir) {
            Ok(()) => {}
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => {
                tracing::debug!(
                    box_id = %self.box_id,
                    path = %socket_dir.display(),
                    error = %error,
                    "Failed to cleanup socket directory after boot failure"
                );
            }
        }

        // A failed restart must never erase a persistent writable rootfs. The
        // provider cleanup above detaches transient mounts while retaining the
        // persistent generation; only ephemeral boxes are removed wholesale.
        if !self.config.persistent && mount_aliases_clean {
            match std::fs::remove_dir_all(&box_dir) {
                Ok(()) => {}
                Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
                Err(error) => {
                    tracing::warn!(
                        box_id = %self.box_id,
                        path = %box_dir.display(),
                        error = %error,
                        "Failed to cleanup box directory after boot failure"
                    );
                }
            }
        }
    }

    fn cleanup_sandbox_mount_aliases(&self) -> Result<()> {
        if self.config.isolation.is_sandbox() {
            crate::sandbox::cleanup_sandbox_mount_aliases(&self.home_dir, &self.box_id)
        } else {
            Ok(())
        }
    }

    /// Create a new VM manager with a custom VMM provider.
    pub fn with_provider(
        config: BoxConfig,
        event_emitter: EventEmitter,
        provider: Box<dyn VmmProvider>,
    ) -> Self {
        let box_id = uuid::Uuid::new_v4().to_string();
        let home_dir = a3s_box_core::dirs_home();
        let rootfs_provider =
            crate::rootfs::default_provider_for_boot(rootfs_snapshot_requested(&config));
        Self {
            config,
            box_id,
            boot_mode: VmBootMode::Workload,
            state: Arc::new(RwLock::new(BoxState::Created)),
            event_emitter,
            provider: Some(provider),
            handler: Arc::new(RwLock::new(None)),
            #[cfg(unix)]
            exec_client: None,
            net_manager: None,
            home_dir,
            anonymous_volumes: Vec::new(),
            created_anonymous_volumes: Vec::new(),
            image_config: None,
            restore_rootfs_cache_key: None,
            healthcheck_disabled: false,
            preserve_rootfs_on_boot_failure: false,
            #[cfg(unix)]
            tee: None,
            rootfs_provider,
            exec_socket_path: None,
            pty_socket_path: None,
            port_forward_socket_path: None,
            prom: None,
            shim_exit_code: None,
            pull_progress_fn: None,
            log_config: a3s_box_core::log::LogConfig::default(),
            resolved_execution_plan: None,
            managed_secret_root: None,
            transient_registry_auth: None,
        }
    }

    /// Get the box ID.
    pub fn box_id(&self) -> &str {
        &self.box_id
    }

    /// Get current state.
    pub async fn state(&self) -> BoxState {
        *self.state.read().await
    }
}

/// Whether this launch couples the rootfs to a VMM memory snapshot.
///
/// The provider must be selected before layout preparation, so keep every
/// snapshot entry point in one predicate. `KRUN_RESTORE_FROM` remains the
/// compatibility input for the single-VM restore path.
fn rootfs_snapshot_requested(config: &BoxConfig) -> bool {
    if config.snapshot_mem_file.is_some()
        || config.snapshot_sock.is_some()
        || config.restore_from.is_some()
    {
        return true;
    }

    #[cfg(unix)]
    {
        std::env::var_os("KRUN_RESTORE_FROM").is_some_and(|value| !value.as_os_str().is_empty())
    }

    #[cfg(not(unix))]
    {
        false
    }
}

/// Whether the vendored libkrun snapshot state contract exists for this build.
///
/// Its serialized VM/vCPU/device state is intentionally compiled only for
/// Linux KVM on x86_64. Detect this before layout preparation so unsupported
/// hosts do not pull an image, allocate file-backed RAM, or create a temporary
/// rootfs transport for an operation that cannot produce a restorable state.
pub(crate) const fn native_snapshot_fork_supported() -> bool {
    cfg!(all(target_os = "linux", target_arch = "x86_64"))
}

fn validate_snapshot_launch(config: &BoxConfig) -> Result<()> {
    let memory = config
        .snapshot_mem_file
        .as_deref()
        .filter(|value| !value.is_empty())
        .map(str::to_owned)
        .or_else(|| snapshot_env_nonempty("KRUN_SNAPSHOT_MEM_FILE"));
    let trigger = config
        .snapshot_sock
        .as_deref()
        .filter(|value| !value.is_empty())
        .map(str::to_owned)
        .or_else(|| snapshot_env_nonempty("KRUN_SNAPSHOT_SOCK"));
    let restore = config
        .restore_from
        .as_deref()
        .filter(|value| !value.is_empty())
        .map(str::to_owned)
        .or_else(|| snapshot_env_nonempty("KRUN_RESTORE_FROM"));

    validate_snapshot_launch_shape(memory.is_some(), trigger.is_some(), restore.is_some())
}

fn validate_snapshot_launch_shape(memory: bool, trigger: bool, restore: bool) -> Result<()> {
    let requested = memory || trigger || restore;
    if !requested {
        return Ok(());
    }
    if !native_snapshot_fork_supported() {
        return Err(BoxError::ConfigError(
            "native VM snapshot-fork is supported only by the Linux x86_64 KVM build".to_string(),
        ));
    }
    match (memory, trigger, restore) {
        (true, true, false) | (true, false, true) => Ok(()),
        _ => Err(BoxError::ConfigError(
            "invalid native VM snapshot configuration: template mode requires memory + trigger socket, while restore mode requires memory + state file"
                .to_string(),
        )),
    }
}

fn snapshot_env_nonempty(name: &str) -> Option<String> {
    std::env::var(name).ok().filter(|value| !value.is_empty())
}

/// Whether this boot is a snapshot-fork restore (the guest is resumed already-booted
/// rather than cold-booted). PER-VM: a pool / fork daemon sets `config.restore_from`
/// so one process can restore different VMs; the single-VM `run` path uses the
/// `KRUN_RESTORE_FROM` env. Either source means restore mode.
#[cfg(unix)]
fn is_restore_mode(config: &BoxConfig) -> bool {
    config
        .restore_from
        .as_deref()
        .is_some_and(|s| !s.is_empty())
        || std::env::var("KRUN_RESTORE_FROM")
            .map(|v| !v.is_empty())
            .unwrap_or(false)
}

/// Simple FNV-1a hash for generating short deterministic hashes from strings.
pub(crate) fn fnv1a_hash(input: &str) -> u64 {
    let mut hash: u64 = 0xcbf29ce484222325;
    for byte in input.bytes() {
        hash ^= byte as u64;
        hash = hash.wrapping_mul(0x100000001b3);
    }
    hash
}

#[cfg(unix)]
fn default_stop_signal() -> i32 {
    libc::SIGTERM
}

#[cfg(windows)]
fn default_stop_signal() -> i32 {
    15
}

async fn wait_for_delayed_terminal_exit(
    handler: &mut dyn VmHandler,
    box_dir: &Path,
    box_id: &str,
) -> Option<i32> {
    let deadline = tokio::time::Instant::now() + TERMINAL_EXIT_POLL_TIMEOUT;
    let mut reported_wait_error = false;
    loop {
        if let Some(exit_code) = handler.exit_code() {
            return Some(exit_code);
        }
        if let Some(exit_code) = boot_failure_persisted_exit_code(box_dir) {
            return Some(exit_code);
        }
        match handler.try_wait_exit() {
            Ok(Some(exit_code)) => return Some(exit_code),
            Ok(None) => {}
            Err(error) => {
                if !reported_wait_error {
                    tracing::debug!(
                        %box_id,
                        %error,
                        "Terminal status remained unavailable after boot cleanup"
                    );
                    reported_wait_error = true;
                }
            }
        }
        if tokio::time::Instant::now() >= deadline {
            return None;
        }
        tokio::time::sleep(TERMINAL_EXIT_POLL_INTERVAL).await;
    }
}

#[cfg(not(target_os = "windows"))]
fn boot_failure_persisted_exit_code(box_dir: &Path) -> Option<i32> {
    crate::rootfs::read_persisted_exit_code(box_dir)
}

#[cfg(target_os = "windows")]
fn boot_failure_persisted_exit_code(box_dir: &Path) -> Option<i32> {
    windows_guest_persisted_exit_code(box_dir)
}

#[cfg(test)]
#[path = "tests.rs"]
mod tests;