Introducing a new way to review your PRs.

Read the announcement

arcboxlabs / arcbox

Run AI agents on real and isolated machines — own kernel, filesystem, and network — with <100ms boot. Local first, OCI compatible, pure Rust.

View on GitHub

32 documents · Updated Nov 6, 2026

docs / daemon-lifecycle

Daemon lifecycle and host orchestration

The ArcBox daemon is the host process that turns a configured ArcBox installation into a usable control and workload environment. It owns daemon-wide state for the System VM, host networking, service endpoints, and long-running background work. The daemon exposes progress to clients while it prepares the host, starts the runtime, and brings workloads back into service.

System role

The daemon sits between host clients and the core runtime. It owns the process lifetime and host resources, while the core and engine layers turn workload requests into VM, guest, container, and network actions. See the Application control plane for the wider host-facing model.

Daemon boundary

The daemon accepts configuration and lifecycle requests from local clients, then coordinates the API layer, the core runtime, and host services. The API surface is described in API and Docker compatibility, while privileged host changes cross the boundary described in Privileged helper boundary.

The daemon uses one selected data directory as the ownership scope for its sockets, lock, logs, and persistent runtime state. A lifetime lock prevents two daemon processes from driving the same state at once. A separate lease protects a non-default container network range from use by another ArcBox daemon.

References: app/…/main.rs• L1-13 app/…/context.rs• L106-139 app/…/lock.rs• L10-25 app/…/container_network_lease.rs• L11-29

Runtime ownership

The core runtime is the daemon's long-running coordination object. It brings together machine management, System VM lifecycle, the guest container backend, host networking, persistent workload state, port exposure, DNS records, and migration state. API handlers use this shared runtime rather than creating a new runtime for each request.

The daemon keeps the runtime available to background tasks and to the shutdown path. This lets a VM restart, a host route change, a guest export remount, or a workload operation update the same state that API clients observe.

References: app/…/runtime.rs• L90-159 app/…/runtime.rs• L242-299 app/…/mod.rs• L122-136

Startup lifecycle

Startup is an ordered, typed path from host preparation to a ready daemon. Each phase consumes the state from the previous phase, so the daemon does not expose a later lifecycle capability before the state it needs exists. Startup handles are created before the pipeline runs, which lets cancellation reach setup status, shared cancellation, and runtime state during an interrupted start.

Startup phases

The daemon follows eight load-bearing phases. Each phase passes a more complete lifecycle context to the next phase, so skipping a phase is prevented by the phase types and normal runtime operations do not start before the required state exists.

  1. Host preparation creates directories, resolves profiles and endpoints, and builds the pre-lock context.
  2. Daemon lease acquisition takes exclusive ownership of the data directory and records the process identity for diagnostics.
  3. Control-plane startup binds the gRPC and DNS endpoints before slow preparation begins.
  4. Stale-resource release waits for resources left by a displaced or interrupted daemon.
  5. Asset preparation makes the kernel, guest root file system, agent, and runtime assets available.
  6. Runtime boot constructs the shared runtime and prepares the Machine host.
  7. Runtime-service startup binds services that need the initialized runtime and prepares host recovery state.
  8. Readiness publication starts retained background work, admits the host, publishes readiness, and starts the retained System task.

The following diagram shows the ordered phases and the point at which the optional System VM task starts.

flowchart LR
    A[Host preparation] --> B[Daemon lease]
    B --> C[Control plane and DNS]
    C --> D[Stale-resource release]
    D --> E[Asset preparation]
    E --> F[Shared runtime and Machine host]
    F --> G[Runtime services]
    G --> H[Network ready]
    H --> I[Host admission and READY]
    I --> J{Linux VM enabled?}
    J -- Yes --> K[Retained System task]
    J -- No --> L[VM-host-only state]

References: app/…/main.rs• L172-220 app/…/pipeline.rs• L21-240 app/…/context.rs• L1-12

Readiness contract

Clients learn startup state through WatchSetupStatus, not by checking logs or guessing from socket timing. The normal path reports initialization, optional cleanup and asset download, asset readiness, optional VM milestones, network readiness, and final readiness. A fatal startup error publishes FAILED with its cause before the daemon exits.

The daemon publishes NETWORK_READY after runtime-dependent host listeners and required host preparation complete. It then admits the host and publishes READY before it starts the retained System task. This separates host availability from System VM startup and keeps the Machine host usable when the System VM is disabled or fails later.

The VM milestones have distinct meanings: VM_STARTING begins after guest binaries are staged, and VM_READY means the guest agent answers. The guest container runtime can still be starting after VM_READY; NETWORK_READY follows only after the host services promised by this daemon are bound. In VM-host-only mode, the Linux VM milestones and VM-dependent services are absent.

References: rpc/…/api.proto• L1021-1089 app/…/system.rs• L275-305 app/…/system.rs• L321-359 app/…/mod.rs• L235-303 app/…/pipeline.rs• L150-239

Host service orchestration

The daemon binds the control plane and DNS early so clients can observe progress and endpoint conflicts can fail before slow runtime work. After the runtime exists, it starts the host services that make guest workloads reachable and runs them under shared lifecycle control.

Control plane and service endpoints

The control plane is the daemon's local request path for system, machine, sandbox, Kubernetes, migration, and related services. One Unix socket serves Connect, gRPC, and gRPC-Web requests, so local tools and native clients use the same handlers and readiness state.

With the Linux VM enabled, the daemon also starts the Docker API socket and the Kubernetes API proxy. DNS runs on the host and resolves ArcBox-managed names through the runtime's network state. The Kubernetes listener is optional when its default port is busy, but an explicitly requested port is a startup requirement.

The daemon can also apply host integrations such as the canonical Docker socket link, resolver setup, Docker CLI integration, and the host alias used by the guest data export. These changes are best effort unless the launch explicitly requires a resolver setup.

References: app/…/control_plane.rs• L32-82 app/…/services.rs• L28-59 app/…/services.rs• L62-161 app/…/recovery.rs• L18-27 app/…/dns_service.rs• L1-13 app/…/pipeline.rs• L68-81

Recovery and reconciliation

Recovery makes a daemon restart less disruptive to workloads that remain in persistent guest state. It restores container DNS and port forwarding, checks the host route for the container network, and starts background tasks that reconcile host state with later VM events.

The daemon treats a VM restart as a new guest incarnation. Route state and host exports are rebuilt when the VM becomes ready again, rather than when a stop begins. This keeps host state aligned with the guest that can actually receive requests.

The same model applies to the optional ~/ArcBox export. A background supervisor waits for VM readiness, establishes the guest export, and retries after transient failure or a later VM restart. Shutdown removes only a mount that this daemon created.

References: app/…/recovery.rs• L18-63 app/…/services.rs• L163-195 app/…/services.rs• L222-295 app/…/nfs_mount.rs• L48-67 app/…/nfs_mount.rs• L92-151

Workload coordination

The daemon does not implement each workload action itself. It gives API handlers a shared runtime, and that runtime coordinates the core managers and engine lifecycle services that operate the System VM and other machines.

Core and engine coordination

The core runtime composes the machine manager, the System VM lifecycle manager, the guest container backend, and host networking. The engine lifecycle manager owns the state of the System VM and translates requests such as ensure-ready, stop, and force-stop into serialized lifecycle work. See Workload engine for the engine's broader role.

The lifecycle manager reports state separately from request completion. Callers can observe whether the VM is ready while another operation is in progress, and background daemon tasks can react to both VM arrival and VM departure. This separation lets API requests, recovery tasks, and shutdown share one VM owner without competing direct control paths.

References: app/…/runtime.rs• L90-108 app/…/runtime.rs• L242-280 engine/…/mod.rs• L1-29 engine/…/mod.rs• L125-149 engine/…/types.rs• L11-50

Workload start and stop

When a client starts a workload, the API layer checks that the shared runtime is ready and delegates the request to the target guest or machine. The runtime ensures that the needed VM path is available, performs the guest operation, and updates host-side state such as DNS and exposed ports. Docker-compatible requests follow the separate Docker service path described in API and Docker compatibility.

When a client stops or removes a workload, the guest operation is followed by host cleanup. VM-level stop uses a graceful guest stop when possible and falls back to force stop when the deadline expires. A force stop can preempt an in-flight boot or stop, which prevents a stuck guest operation from blocking daemon control.

References: app/…/control.rs• L24-42 app/…/control.rs• L53-138 app/…/machine.rs• L177-247 engine/…/actor.rs• L1-13 engine/…/boot.rs• L46-95

Shutdown lifecycle

Shutdown is a coordinated drain of host services, workloads, VM state, and owned host resources. The daemon uses one cancellation token to stop background service loops and keeps enough pre-start state alive to clean up a VM even when shutdown interrupts boot.

Graceful shutdown

The first shutdown signal stops new service work, drains active connections, and asks the runtime to stop the System VM gracefully. The runtime then stops port forwarding, the default System VM, other managed machines, macOS guests when present, and the network manager. The daemon removes its API sockets but keeps the lock file so the kernel-managed lock remains the source of truth.

The daemon also cleans up the NFS export, an isolated container route, and production Docker integration. Cleanup is ownership-aware: the daemon removes only host state that it owns.

References: app/…/shutdown.rs• L31-64 app/…/shutdown.rs• L128-163 app/…/runtime.rs• L959-1030

Forced shutdown

A second signal, a shutdown deadline, or a failed graceful-stop task switches to forced shutdown. The daemon force-stops the System VM and remaining machines after it stops host port forwarding, then completes route and socket cleanup. If startup is still in progress, the signal path uses the early runtime handle to stop a VM that may have continued booting after the startup task was cancelled.

Startup failures use the same cleanup principle without pretending that the daemon reached readiness. The failure is first published to setup-status subscribers, then the runtime and any acquired network lease are force-cleaned before the original error is returned.

References: app/…/shutdown.rs• L67-126 app/…/shutdown.rs• L165-223 app/…/runtime.rs• L1032-1064 app/…/main.rs• L179-199

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the startup phase order, setup-status phase meanings, and client-visible readiness stream aligned when a phase or service boundary changes.
  • Keep early_runtime and shared_runtime semantics aligned with the diagnostics and normal API calls that use them.
  • Keep daemon lock ownership, socket paths, and data-directory resolution aligned with the CLI handoff and any isolated-daemon launch path.
  • Keep the VM-ready event path aligned with route installation, vm_running, guest export mounting, and restart recovery.
  • Keep service binding and readiness publication together so a promised endpoint cannot fail after the daemon reports NETWORK_READY.
  • Keep graceful and forced shutdown paths aligned for port forwarding, VM state, network routes, sockets, and privileged host resources.

Important failure modes

TriggerConsequenceGuard
A client waits for readiness by log search or sleepIt can miss a phase or hide a published startup failureConsume WatchSetupStatus and handle FAILED and daemon exit
A new service publishes readiness before its listener bindsClients see a ready daemon but cannot reach the promised serviceBind required listeners before exposing the shared runtime
A daemon starts without holding the data-directory lockTwo processes can drive the same sockets, VM state, or disk imagesAcquire the lifetime daemon lease before serving the control plane
Route or guest-export recovery acts when a VM stops instead of when it is readyRequests enter a gap with no guest, and later VM incarnations can lack host stateReconcile from the VM ready state and retry within the supervisor
A diagnostic RPC waits on the normal runtime handle during a stuck bootThe client cannot inspect the VM that is blocking startupServe boot diagnostics from the early runtime handle
A second shutdown signal arrives during graceful stopA blocking stop can delay exit and leave host state behindBound graceful stop, force-stop the VM, then run ownership-aware cleanup

Key files

FileWhy to read it
app/…/main.rsShows the process entry point, startup order, signal coverage, and handoff to shutdown.
app/…/pipeline.rsShows the typed lifecycle from host preparation through host admission, readiness, and the retained System task.
app/…/context.rsExplains which daemon state survives startup cancellation and which services exist at runtime.
app/…/services.rsShows how the control plane, DNS, Docker, Kubernetes, route, and VM-state services are coordinated.
app/…/shutdown.rsShows graceful, forced, interrupted-startup, and cleanup behavior.
app/…/runtime.rsShows the shared runtime boundary between API calls, VM lifecycle, guest services, and host state.
engine/…/mod.rsExplains the engine's VM ownership model and the public lifecycle contract.
engine/…/actor.rsShows how lifecycle requests and state changes share one VM owner.
app/…/system.rsDefines the setup-status and diagnostic contract used during startup.