arcbox-core 0.6.3

Core orchestration layer for ArcBox
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
//! Automatic VM lifecycle management.
//!
//! This module provides transparent VM management for container operations.
//! Users never need to manually manage VMs - the lifecycle manager automatically
//! creates, starts, stops, and recovers VMs as needed.
//!
//! ## Design Goals
//!
//! - **Transparent**: Users only run `docker run`, VM is invisible
//! - **Eager**: Default VM boots during runtime initialization
//! - **Fast**: Cold start <1.5s, warm <500ms
//! - **Resilient**: Auto-recovery from crashes
//!
//! ## Architecture
//!
//! The lifecycle is an event-driven hierarchical state machine (`machine.rs`,
//! built on `statig`) owned by a single actor task (`actor.rs`). The facade
//! ([`VmLifecycleManager`]) translates public calls into actor commands over an
//! `mpsc` channel and serves reads from a lock-free `watch` channel; slow I/O
//! (create/start/agent-wait/stop) runs in sub-tasks (`boot.rs`) so a force stop
//! can always preempt.
//!
//! ```text
//! VmLifecycleManager ──commands──▶ LifecycleActor ⟳ statig VmLifecycle
//!         ▲                          │        │
//!         └────── watch state ───────┘        └──spawn──▶ boot/stop sub-task
//!//!                                          MachineManager ◀────┘
//! ```

mod actor;
mod balloon;
mod boot;
mod health;
mod machine;
mod recovery;
#[cfg(target_os = "macos")]
mod serial;
#[cfg(test)]
mod tests;
mod types;

use crate::boot_assets::BootAssetProvider;
use crate::error::{CoreError, Result};
use crate::event::EventBus;
use crate::machine::{MachineInfo, MachineManager};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;
use tokio::sync::{mpsc, oneshot, watch};

use actor::{Command, LifecycleActor, LifecycleShared};

/// Default machine name used for container operations.
pub const DEFAULT_MACHINE_NAME: &str = "default";

/// Default startup timeout in seconds.
///
/// Generous enough to cover a cold guest boot (erofs rootfs + large docker.img
/// mount) even when the host daemon is CPU/I/O constrained. A tight 30s budget
/// raced the cold-boot path and produced "timeout waiting for agent" loops.
const DEFAULT_STARTUP_TIMEOUT_SECS: u64 = 90;

/// Default health check interval in seconds.
const DEFAULT_HEALTH_CHECK_INTERVAL_SECS: u64 = 5;

/// Default idle timeout in seconds (5 minutes).
const DEFAULT_IDLE_TIMEOUT_SECS: u64 = 300;

/// Maximum retry attempts for recovery.
const DEFAULT_MAX_RETRIES: u32 = 3;

/// Interval of the actor's idle ticker: the delay before the balloon
/// shrinks once the idle timeout has elapsed, and the granularity of the
/// idle re-target cadence (`balloon::IDLE_RETARGET_INTERVAL_SECS`).
const BALLOON_SHRINK_DELAY_SECS: u64 = 10;

/// Persistent guest dockerd data image name.
const DOCKER_DATA_IMAGE_NAME: &str = "docker.img";
/// Persistent guest dockerd data image size (8 TiB sparse file).
///
/// This is the virtual size of the block device. The host file is sparse and
/// only consumes actual disk space for written blocks. 8 TiB matches OrbStack
/// and prevents users from hitting artificial limits.
const DOCKER_DATA_IMAGE_SIZE_BYTES: u64 = 8 * 1024 * 1024 * 1024 * 1024;
/// Persistent guest metadata image size (2 GiB sparse file).
///
/// The ext4 volume holds only the fsync-hot boltdb metadata directories —
/// bulk data stays on the btrfs data image — so 2 GiB is ~100x headroom.
/// See internal-docs/plans/ext4-metadata-volume.md.
const DOCKER_METADATA_IMAGE_SIZE_BYTES: u64 = 2 * 1024 * 1024 * 1024;

pub(crate) use boot::ensure_sparse_block_image;
pub use health::HealthMonitor;
pub use recovery::{BackoffStrategy, RecoveryAction, RecoveryPolicy};
pub use types::{DefaultVmConfig, VmLifecycleConfig, VmLifecycleState};

/// Holds the VM out of idle while a host-side operation is in flight.
///
/// Returned by [`VmLifecycleManager::begin_activity`]; dropping it releases
/// the hold and stamps fresh activity (the idle clock starts from the
/// operation's *end*).
pub struct ActivityScope {
    shared: Arc<LifecycleShared>,
}

impl Drop for ActivityScope {
    fn drop(&mut self) {
        self.shared.active_ops.fetch_sub(1, Ordering::AcqRel);
        self.shared.record_activity();
    }
}

/// Deferred actor state, consumed when the actor is first started.
///
/// The constructors are synchronous and may run outside a tokio runtime (e.g.
/// `Runtime::new` in tests), where `tokio::spawn` would panic — so the actor is
/// spawned lazily from the first async facade call instead.
struct ActorSeed {
    /// Receiving half of the facade's command channel.
    commands: mpsc::UnboundedReceiver<Command>,
    /// Publishing half of the state channel.
    state_tx: watch::Sender<VmLifecycleState>,
}

/// VM lifecycle manager.
///
/// Provides transparent VM management for container operations.
/// Users never need to manually manage VMs.
///
/// ## Usage
///
/// ```ignore
/// let manager = VmLifecycleManager::new(machine_manager, event_bus, data_dir, config)?;
///
/// // Ensure VM is ready before any container operation
/// let cid = manager.ensure_ready().await?;
/// ```
pub struct VmLifecycleManager {
    /// State shared with the lifecycle actor and its boot/stop sub-tasks.
    shared: Arc<LifecycleShared>,
    /// Commands to the lifecycle actor.
    cmd_tx: mpsc::UnboundedSender<Command>,
    /// Public lifecycle state, published by the actor after every transition.
    state_rx: watch::Receiver<VmLifecycleState>,
    /// One-shot actor startup latch; see [`ActorSeed`].
    actor: OnceLock<()>,
    /// Actor state handed to `tokio::spawn` on first use.
    seed: Mutex<Option<ActorSeed>>,
}

impl VmLifecycleManager {
    /// Creates a new VM lifecycle manager for the default native machine.
    pub fn new(
        machine_manager: Arc<MachineManager>,
        event_bus: EventBus,
        data_dir: PathBuf,
        config: VmLifecycleConfig,
    ) -> Result<Self> {
        Self::for_machine(
            String::from(DEFAULT_MACHINE_NAME),
            String::from(DOCKER_DATA_IMAGE_NAME),
            machine_manager,
            event_bus,
            data_dir,
            config,
        )
    }

    /// Creates a new VM lifecycle manager bound to a specific machine name
    /// and persistent data image. Used to build per-role lifecycles (e.g.
    /// the secondary VZ Rosetta VM) that must not share state with the
    /// default native machine.
    pub fn for_machine(
        machine_name: String,
        data_image_filename: String,
        machine_manager: Arc<MachineManager>,
        event_bus: EventBus,
        data_dir: PathBuf,
        config: VmLifecycleConfig,
    ) -> Result<Self> {
        let boot_assets = Arc::new(
            BootAssetProvider::with_config(
                crate::boot_assets::BootAssetConfig::with_cache_dir(data_dir.join("boot"))
                    .with_unpinned_manifest_allowed(config.allow_unpinned_boot_manifest),
            )?
            .with_kernel(config.default_vm.kernel.clone().unwrap_or_default())?,
        );

        let health_monitor = Arc::new(HealthMonitor::new(
            config.health_check_interval,
            config.max_retries,
        ));

        let recovery = RecoveryPolicy::new(config.max_retries, BackoffStrategy::default());

        // Seed the backend from the persisted machine so a prior switch survives
        // daemon restarts; fall back to the config default on first boot.
        let seeded_backend = machine_manager
            .get(&machine_name)
            .map_or(config.backend, |info| info.backend);

        let now_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64;

        let shared = Arc::new(LifecycleShared {
            machine_name,
            data_image_filename,
            data_dir,
            machine_manager,
            event_bus,
            boot_assets,
            recovery,
            health_monitor,
            config,
            backend: AtomicU8::new(seeded_backend as u8),
            restart_generation: AtomicU64::new(0),
            last_activity_ms: AtomicU64::new(now_ms),
            active_ops: AtomicUsize::new(0),
            kubernetes_hold: AtomicBool::new(false),
        });

        // The machine always boots its state graph from `NotExist`; whether a
        // boot must (re)create the machine is derived from the machine
        // registry at boot time, so no persisted-state seeding is needed (all
        // persisted states map to non-ready states after crash recovery).
        let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
        let (state_tx, state_rx) = watch::channel(VmLifecycleState::NotExist);

        Ok(Self {
            shared,
            cmd_tx,
            state_rx,
            actor: OnceLock::new(),
            seed: Mutex::new(Some(ActorSeed {
                commands: cmd_rx,
                state_tx,
            })),
        })
    }

    /// Spawns the lifecycle actor on first use.
    ///
    /// Must be called from a tokio runtime context; every caller is an async
    /// facade method, which guarantees that.
    fn ensure_actor(&self) {
        self.actor.get_or_init(|| {
            let seed = self
                .seed
                .lock()
                .expect("lifecycle actor seed lock poisoned")
                .take();
            if let Some(seed) = seed {
                let actor = LifecycleActor::new(
                    Arc::clone(&self.shared),
                    seed.commands,
                    self.cmd_tx.clone(),
                    seed.state_tx,
                );
                drop(tokio::spawn(actor.run()));
            }
        });
    }

    /// Sends `command` to the actor and awaits its reply.
    async fn request<T>(
        &self,
        command: Command,
        reply_rx: oneshot::Receiver<Result<T>>,
    ) -> Result<T> {
        self.ensure_actor();
        self.cmd_tx
            .send(command)
            .map_err(|_| CoreError::Vm("VM lifecycle actor terminated".to_string()))?;
        reply_rx
            .await
            .map_err(|_| CoreError::Vm("VM lifecycle actor terminated".to_string()))?
    }

    /// Returns the machine name this lifecycle manager owns.
    #[must_use]
    pub fn machine_name(&self) -> &str {
        &self.shared.machine_name
    }

    /// Returns the current VM incarnation counter, bumped on every stop.
    ///
    /// The Docker proxy compares this against the value it last verified
    /// against to detect a System VM restart (e.g. a backend switch) and reset
    /// its cached readiness + pooled connections before the next request.
    #[must_use]
    pub fn restart_generation(&self) -> u64 {
        self.shared
            .restart_generation
            .load(std::sync::atomic::Ordering::Acquire)
    }

    /// Returns the absolute path of this machine's persistent dockerd
    /// data image.
    #[must_use]
    pub fn data_image_path(&self) -> PathBuf {
        self.shared
            .data_dir
            .join(arcbox_constants::paths::host::DATA)
            .join(&self.shared.data_image_filename)
    }

    /// Returns the current lifecycle state.
    #[allow(clippy::unused_async, reason = "public API compatibility")]
    pub async fn state(&self) -> VmLifecycleState {
        *self.state_rx.borrow()
    }

    /// Subscribes to lifecycle state transitions.
    ///
    /// Prefer this over polling [`Self::restart_generation`] when a task must
    /// react to the VM coming *up*: the generation counter is bumped on stop,
    /// so it marks the start of the gap where no guest exists, not the arrival
    /// of the next one.
    #[must_use]
    pub fn subscribe_state(&self) -> watch::Receiver<VmLifecycleState> {
        self.state_rx.clone()
    }

    /// Returns true if the VM is running and ready.
    #[allow(clippy::unused_async, reason = "public API compatibility")]
    pub async fn is_running(&self) -> bool {
        self.state_rx.borrow().is_ready()
    }

    /// Ensures a VM is ready for container operations.
    ///
    /// This is the main entry point for all container commands.
    /// It handles:
    /// - Creating VM if not exists
    /// - Starting VM if stopped
    /// - Waiting for agent ready
    /// - Health verification
    ///
    /// # Returns
    /// CID for agent communication.
    ///
    /// # Errors
    /// Returns an error if VM cannot be started or agent is not ready.
    pub async fn ensure_ready(&self) -> Result<u32> {
        self.ensure_ready_with_timeout(self.shared.config.startup_timeout)
            .await
    }

    /// Ensures VM is ready with custom timeout.
    pub async fn ensure_ready_with_timeout(&self, timeout: Duration) -> Result<u32> {
        // Skip VM check for testing.
        if self.shared.config.skip_vm_check {
            tracing::debug!("ensure_ready: skipping VM check (test mode)");
            // Register a mock machine so that container operations work.
            let mock_cid = 3;
            self.shared
                .machine_manager
                .register_mock_machine(&self.shared.machine_name, mock_cid)?;
            // Return a mock CID for testing.
            return Ok(mock_cid);
        }

        let (reply, reply_rx) = oneshot::channel();
        self.request(Command::EnsureReady { timeout, reply }, reply_rx)
            .await
    }

    /// Records external activity (e.g. a proxied Docker API request),
    /// resetting the idle clock and exiting idle if the VM is there.
    ///
    /// Cheap and non-blocking — safe to call on every request. Unlike
    /// [`Self::ensure_ready`] it never boots a stopped VM.
    pub fn note_activity(&self) {
        self.shared.record_activity();
        if *self.state_rx.borrow() == VmLifecycleState::Idle {
            // Exit idle so the balloon is restored to full memory.
            let _ = self.cmd_tx.send(Command::Activity);
        }
    }

    /// Like [`Self::note_activity`], but additionally holds the VM out of
    /// idle until the returned scope is dropped.
    ///
    /// For long-lived proxied operations (image pulls, builds, log
    /// streams): a request in flight is activity for its whole duration,
    /// not just at its first byte — the 2026-07-15 follow-up incident had
    /// an idle shrink squeeze a guest mid-pull.
    pub fn begin_activity(&self) -> ActivityScope {
        self.note_activity();
        self.shared.active_ops.fetch_add(1, Ordering::AcqRel);
        ActivityScope {
            shared: Arc::clone(&self.shared),
        }
    }

    /// Enables or disables the Kubernetes lifecycle hold.
    #[allow(clippy::unused_async, reason = "public API compatibility")]
    pub async fn set_kubernetes_hold(&self, active: bool) {
        self.shared
            .kubernetes_hold
            .store(active, std::sync::atomic::Ordering::Relaxed);
        self.shared.record_activity();

        if active {
            // Exit idle (restoring the balloon) so the hold takes effect
            // immediately.
            self.ensure_actor();
            let _ = self.cmd_tx.send(Command::Activity);
        }
    }

    /// Gracefully stops the VM.
    ///
    /// # Errors
    /// Returns an error if the VM cannot be stopped.
    pub async fn shutdown(&self) -> Result<()> {
        let (reply, reply_rx) = oneshot::channel();
        self.request(Command::Shutdown { reply }, reply_rx).await
    }

    /// Forces VM termination, preempting any in-flight boot or graceful stop.
    ///
    /// # Errors
    /// Returns an error if the VM cannot be terminated.
    pub async fn force_stop(&self) -> Result<()> {
        let (reply, reply_rx) = oneshot::channel();
        self.request(Command::ForceStop { reply }, reply_rx).await
    }

    /// Returns the System VM's current hypervisor backend.
    #[must_use]
    pub fn backend(&self) -> arcbox_vmm::VmBackend {
        self.shared.backend()
    }

    /// Sets the hypervisor backend used on the next (re)boot of the System VM.
    ///
    /// Does not stop or restart a running VM; to apply immediately the caller
    /// must force a recreate (see `Runtime::switch_system_vm_backend`).
    pub fn set_backend(&self, backend: arcbox_vmm::VmBackend) {
        self.shared
            .backend
            .store(backend as u8, std::sync::atomic::Ordering::Release);
    }

    /// Returns the configuration.
    #[must_use]
    pub fn config(&self) -> &VmLifecycleConfig {
        &self.shared.config
    }

    /// Returns the boot asset provider.
    #[must_use]
    pub fn boot_assets(&self) -> &Arc<BootAssetProvider> {
        &self.shared.boot_assets
    }

    /// Returns the resolved default VM configuration used by lifecycle.
    #[must_use]
    pub fn default_vm_config(&self) -> DefaultVmConfig {
        self.shared.config.default_vm.clone()
    }

    /// Returns the health monitor.
    #[must_use]
    pub fn health_monitor(&self) -> &Arc<HealthMonitor> {
        &self.shared.health_monitor
    }

    /// Returns the machine info for the default machine.
    pub fn default_machine_info(&self) -> Option<MachineInfo> {
        self.shared.machine_manager.get(&self.shared.machine_name)
    }
}