Runtime execution orchestration
Runtime execution orchestration turns a sandbox request into a running, controlled virtual machine (VM). It prepares the guest and its host resources, keeps guest work behind bounded control paths, and preserves enough state for safe pause, checkpoint, restore, and shutdown. This page complements Sandbox lifecycle, Guest agent session handling, and Managed jobs.
Runtime role
The runtime owns the active part of a sandbox run. It joins the launch contract, protected storage, VM state, guest agent, host controls, resource monitors, and exit cleanup into one run owned by a single runtime process.
The host and guest have different roles:
- The host admits resources, applies policy, owns host-only controls, and decides when the run must stop.
- The guest boots from one typed description and reports readiness, activity, resource state, and workload results through the agent path.
- The relay joins client traffic to the guest while keeping lifecycle traffic private and bounded.
The main run is a staged handoff:
flowchart LR
A[Launch request] --> B[Host admission]
B --> C[VM and guest setup]
C --> D[Guest readiness or restore activation]
D --> E[Relay and host control]
E --> F[Workloads and sessions]
F --> G[Lifetime monitoring]
G --> H[Guest-first stop]
H --> I[Host cleanup and state release]
References: crates/…/vm.rs• L596-635 crates/…/vm.rs• L1280-1395 crates/…/vm.rs• L1560-1580 crates/…/lib.rs• L1-5
Launch contract
The launch contract is the boundary between the launcher and the runtime. It carries the guest boot description and host-only run policy through an inherited configuration channel, so sensitive network and environment data do not appear in normal process arguments.
The contract describes the decisions that shape one run:
| Area | Purpose |
|---|---|
| Execution intent | Selects a fresh boot or a recognized restore. |
| VM capacity | Sets boot CPU and memory plus any live-growth ceiling. |
| Guest bootstrap | Defines the root, mounts, identity, environment, network, and security profile. |
| Host policy | Carries CPU placement, writeback, clock, filesystem-state, and isolation choices. |
| Workload and lifetime | Defines startup work and idle or maximum-duration limits. |
| Runtime handoff | Connects the child to its agent, logs, metrics, parent watch, and lifecycle owner. |
Boot and restore are explicit. A restore needs a complete recognized source and cannot also request a fresh startup command. Older launch formats can be translated only when they can represent the request; unsupported restore state or policy is rejected instead of being silently dropped. Earlier formats retain their synchronized guest-clock behavior.
References: crates/…/launch.rs• L162-313 crates/…/launch.rs• L315-380 crates/…/launch.rs• L481-568 crates/…/launch.rs• L26-51 crates/…/launch.rs• L136-218 crates/…/launch.rs• L226-251
VM preparation
VM preparation combines the machine shape, guest filesystems, disks, network, console, and workload settings before guest code can run. The runtime checks host paths and resource bindings first, then gives the VM only the resources described by the admitted launch.
The root may be a directory, a disk image, or a runtime-owned layer chain. Additional mounts and isolated file mounts stay distinct from the root. Runtime-owned disk state keeps the guest-visible device identity stable across checkpoints, compaction, growth, and restore.
The runtime also prepares trust material and private staging areas when a run needs them:
- Protected layout admission makes host storage authoritative before activation.
- Private launch staging holds archive or restore inputs until the operation owns them. Abandoned preparation is removed on a later reservation.
- Host-owned interception identities stay outside the guest runtime share. A durable checkpoint can carry an authenticated sealed receipt so a restore keeps the expected signer.
- RAM cache files support local branches and restore speed. Cache inspection and pruning remove only recognized, unpinned files.
The VM exposes separate paths for guest traffic, optional bulk traffic, host control, and metrics. The runtime enables a dedicated bulk path only after the guest and relay agree on it, so high-volume data cannot overtake control or terminal records.
References: crates/…/layout.rs• L97-210 crates/…/launch_staging.rs• L15-76 crates/…/cache_storage.rs• L68-184 crates/…/tls_identity.rs• L26-67 crates/…/tls_receipt.rs• L122-170 crates/…/vm.rs• L1930-2001 crates/…/vm.rs• L2160-2180 crates/…/relay.rs• L1321-1477
Policy and resource allocation
Runtime policy limits guest behavior and reserves host capacity before the VM enters service. The runtime keeps the reservations and the effective settings linked to the VM until exit.
| Policy | Run-time effect |
|---|---|
| Guest security and mounts | Limits guest privilege and the host resources visible through mounts. |
| Guest clock | Keeps host clock sync by default or disables host clock updates when requested. |
| CPU and memory placement | Selects and records the host placement used by the VM. |
| Writable-disk pressure | Shares a bounded writeback budget across active writable disks. |
| Network profile | Applies the selected guest egress and host-service boundary. |
| Live resize | Reserves boot-time headroom for later CPU or memory changes. |
| Metrics | Publishes VM, network, storage, and valid live-memory samples to the reserved observer slot. |
Placement may be best effort unless the selected profile requires strict placement. Writeback coordination fails closed, so a lost coordination store does not restore an unsafe write window. Metrics and resize reports distinguish accepted host capacity from guest convergence; unknown or stale observations are not presented as current state.
References: crates/…/launch.rs• L201-246 crates/…/vm.rs• L758-802 crates/…/vm.rs• L844-863 crates/…/vm.rs• L908-1050 crates/…/metrics.rs• L137-241 crates/…/metrics_memory.rs• L12-75 crates/…/admission.rs• L66-283
Guest and host control paths
Guest work uses the agent relay. VM capacity, pause state, checkpoint work, labels, secrets, and other host-owned changes use a host control endpoint because the guest cannot be trusted to change host state.
The control endpoint supports a legacy one-line JSON exchange and a negotiated persistent framed exchange. Both enter the same runtime authority, which checks the runtime boot identity, request identity, and expected revision before applying a command.
The dispatcher protects the host while preserving useful concurrency:
- Ordinary requests stay ordered per connection and receive fair service across connections.
- Execution signals use a separate bounded lane, so a signal waiting on guest delivery does not block lifecycle work.
- Reply capacity is reserved before a host mutation starts.
- A disconnected session cancels its queued work.
The runtime advertises only the live operations supported by the VM boot. These can include CPU and memory targets, root growth, disk compaction, pause and resume, checkpoint or branch capture, secret changes, label changes, and capability inspection. A failed quiesce, resume, or storage rebind can move the runtime into a recovery-owned paused state; an ordinary resume must not release that state.
References: crates/…/control.rs• L34-181 crates/…/dispatch.rs• L95-185 crates/…/dispatch.rs• L230-467 crates/…/executor.rs• L213-281 crates/…/executor.rs• L502-603 crates/…/labels.rs• L34-130 crates/…/handler.rs• L183-257 crates/…/resize.rs• L54-168
Workload transport
The workload transport protects lifecycle operations from guest application traffic. A trusted private channel freezes and thaws workload input for pause or capture, while the ordinary relay path admits client frames only when the current guest policy and transport credit allow them.
When a capture or resident pause starts, the runtime gates new guest input and waits for both transport lanes to reach a complete record boundary. It keeps the gate until the guest confirms thaw. If the gate is dropped before that confirmation, the runtime fences the transport instead of releasing queued input into an unknown guest state.
The relay can use a separate bulk lane for large records. It keeps records ordered across lanes, tracks each bulk operation by client incarnation, and sends a bounded typed cancellation when transport failure prevents a normal terminal result. A slow input path, failed peer, late terminal record, or completed operation cannot consume unbounded relay state.
References: crates/…/workload_control.rs• L59-75 crates/…/workload_control.rs• L183-205 crates/…/workload_control.rs• L244-375 crates/…/workload_control.rs• L378-520 crates/…/relay.rs• L882-1143 crates/…/relay.rs• L2604-2837 crates/…/exec_admission.rs• L106-205 crates/…/exec_admission.rs• L262-382
Checkpoint and restore state
Checkpoint and restore preserve a runnable guest state without granting captured data new host authority. Capture joins guest workload state, VM device state, disks, memory, external bindings, and host-only evidence at one quiesced boundary.
The flow is:
flowchart LR
A[Admit source and bindings] --> B[Freeze workload]
B --> C[Pause VM]
C --> D[Capture memory, devices, disks, and evidence]
D --> E[Publish complete artifact]
E --> F[Resume source or retain recovery pause]
F --> G[Validate and install restore state]
G --> H[Activate guest after host checks]
Restore validates the checkpoint closure, execution epoch, device state, VM geometry, network identity, and guest-agent transport state before activation. External mounts may be restored, reported as unavailable, or rejected according to the launch policy. The restored agent state includes the transport position and credit already consumed before capture, so restored input cannot be replayed or overrun.
Runtime-owned root disks use a forward journal. The journal is durable before a live backend switch, which lets a later start complete an interrupted growth, rollover, or compaction without guessing which disk head is current. Full capture can also retain private configuration evidence, but missing evidence remains unknown rather than becoming a default.
Restored sandboxes can carry deferred command bindings and execution limits. The destination admits these requirements in the same transaction as its reservation, then closes the guard only after a compatible runtime has accepted them. This prevents an older runtime from starting a process with silently missing command inputs or weaker limits.
References: crates/…/coordinator.rs• L184-289 crates/…/coordinator.rs• L524-710 crates/…/coordinator.rs• L812-1174 crates/…/restore.rs• L208-360 crates/…/restore.rs• L480-513 crates/…/disk.rs• L476-607 crates/…/command_bindings.rs• L13-72 crates/…/command_bindings.sql• L1-10 crates/…/restored_exec_limits.rs• L10-63 crates/…/snapshot_editor.rs• L19-97
Configuration evidence
Configuration evidence records what the active runtime confirmed, not what a caller later desired. The retention journal fences a new start or live change before its effect, confirms it only after activation or mutation succeeds, and marks an uncertain result as unknown.
The same distinction applies to checkpoint evidence:
- Live capture reads the confirmed owner while the runtime mutation authority remains held.
- Stopped-source selection checks the latest run, catalog identity, terminal receipt, and lifecycle ownership.
- Optional storage, execution-limit, capacity, script, and init observations describe only what the runtime actually observed.
- Private receipts are bound to the checkpoint identity and content digest. They are not guest-visible archive members.
This prevents a failed start, a partial secret batch, a changed catalog row, or a replaced host backend from being presented as current configuration. It also lets older clients continue ordinary runs without inventing evidence they could not record.
References: crates/…/retention.rs• L46-140 crates/…/retention.rs• L248-345 crates/…/retention.rs• L361-461 crates/…/live.rs• L35-190 crates/…/selection.rs• L16-127 crates/…/capture.rs• L127-235 crates/…/capture.rs• L282-480 crates/…/init.rs• L36-85 crates/…/storage_observation.rs• L17-114
Managed job execution
Managed jobs provide a runtime-owned path for detached commands. The host keeps job identity, status, output history, and attachment leases while the guest agent carries the process stream.
Each job is fenced by the current runtime boot identity. A repeated job identity with the same command is an observation, not a second launch. A changed command is rejected. Active jobs block full capture and branching because a restored process must retain a host owner that can attach to it.
Attachments separate output reading from input ownership. Output and input queues are bounded, completed output is kept in durable history, and a runtime restart marks an active job as lost rather than starting it again. Detached jobs do not by themselves satisfy caller-attached idle rules, while a live attachment keeps the sandbox active.
References: crates/…/jobs.rs• L177-249 crates/…/jobs.rs• L251-350 crates/…/jobs.rs• L420-569 crates/…/jobs.rs• L572-750 crates/…/jobs.rs• L797-1100 crates/…/executor.rs• L425-480 crates/…/executor.rs• L524-536
Lifetime monitoring
Lifetime monitoring keeps a running sandbox bounded without treating one missing signal as proof of failure. Readiness, activity, attached ownership, maximum duration, startup completion, and parent ownership are separate inputs to the stop decision.
The runtime uses these rules:
- Readiness must complete before the run becomes public. Restore activation must also finish its host checks.
- Idle shutdown uses guest activity and caller-owned session state. A stale heartbeat alone does not kill a busy run.
- A detached managed job does not count as an attached caller session. A live job attachment does.
- Maximum duration and attached-parent policy can request shutdown independently.
- A startup workload can make its completion the terminal condition for a detached run.
Shutdown asks the guest to flush and power off first. The host exit handle remains the backstop when the guest does not finish within the allowed grace period.
References: crates/…/vm.rs• L1284-1395 crates/…/vm.rs• L1414-1628 crates/…/relay.rs• L570-617 crates/…/jobs.rs• L234-249
Termination and recovery
Termination releases run-owned resources and records the terminal result before the runtime process exits. Recovery handles the case where the process dies before its exit observer can finish.
The exit path keeps ownership order clear:
- Release CPU and writable-disk reservations.
- Remove runtime-owned endpoints without following links.
- Record the run result and termination reason.
- Mark the sandbox terminal and clear active runtime state.
- Remove terminal ephemeral state when the sandbox is ephemeral.
- Release metrics and finish host-side logs.
Startup maintenance is bounded and opportunistic. It reconciles dead active rows, preserves terminal configuration evidence when possible, and removes socket and sandbox artifacts before deleting the database row. A failed filesystem removal leaves a row for a later retry. Lifecycle, transition, and snapshot-lineage guards stop cleanup from crossing a live owner or an active publication.
Pre-ready failures also leave a bounded diagnostic for the launcher. A later successful relay-ready event can remove that diagnostic, so a failed boot is visible without making the guest runtime share host authority.
References: crates/…/maintenance.rs• L175-300 crates/…/maintenance.rs• L343-474 crates/…/maintenance.rs• L771-907 crates/…/ipc.rs• L78-170 crates/…/ipc.rs• L383-435 crates/…/vm.rs• L865-1092 crates/…/vm.rs• L1546-1578
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to launch intent, restore fields, or compatibility decoding must keep boot and restore explicit and must reject features that an older format cannot carry.
- Changes to protected layout, root-disk journals, or migration must keep host bindings, writer ownership, publication order, and restart recovery aligned.
- Changes to RAM cache or launch staging must keep pins, owner locks, private namespaces, bounded scans, and abandoned-operation cleanup aligned.
- Changes to captured network or TLS state must keep the guest gateway identity and signer evidence tied to the trusted host binding.
- Changes to retention or configuration capture must keep owner identity, generation fences, catalog identity, private receipts, and unknown-state handling aligned.
- Changes to restored command bindings or execution limits must keep the destination transaction guard and runtime admission step paired.
- Changes to relay transport or workload control must keep private lifecycle traffic separate from client traffic, preserve cross-lane order, and fence input after uncertain thaw.
- Changes to control dispatch must keep parser selection, per-connection order, reply reservation, request fencing, deduplication, and signal isolation aligned.
- Changes to resize or metrics must keep host allocation, guest convergence, observation epochs, pause invalidation, and published limits aligned.
- Changes to managed jobs must keep boot fencing, launch deduplication, active-job capture gates, attachment ownership, bounded queues, durable history, and restart recovery aligned.
- Changes to endpoint publication or cleanup must keep lifecycle ownership, legacy compatibility paths, liveness preservation, and no-follow removal aligned.
- Changes to lifetime decisions must keep readiness, guest activity, caller-owned sessions, managed-job attachments, parent ownership, and shutdown reasons distinct.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Restore intent has no complete recognized source | The runtime must not cold-boot with missing state. | Launch validation requires matching intent and restore data before VM construction. |
| Protected storage is incomplete or changed during admission | The sandbox stays in recovery instead of exposing uncertain host state. | Layout admission uses host bindings, bounded inspection, and a ready marker. |
| A root-disk journal is written but backend rebind is uncertain | The VM may remain paused until restart recovery completes the forward state. | Durable journal state is written before the live backend switch. |
| A capture includes an active managed job | The restored process could have no host owner. | Full capture and branch operations reject active jobs. |
| Guest input is gated and thaw is not confirmed | Queued input could reach an unknown guest state. | Workload gates fence the transport when dropped without a confirmed thaw. |
| A bulk transport fails before its finish record | The client could wait forever. | The relay sends bounded typed cancellation and tracks completed correlations. |
| A control reply has no reserved capacity | A host mutation could finish without a result path. | Dispatch reserves reply bytes before mutation. |
| A request targets another runtime boot or revision | A stale client could change a newer runtime. | The executor checks boot identity, request identity, and revision. |
| Guest or host resize evidence is stale or mismatched | The runtime could report accepted capacity as applied. | Resize epochs and the observer require matching host and guest state. |
| A memory shrink exceeds guest-available memory | Unplugging could pressure or stop the guest. | The runtime checks guest pressure before applying a tighter target. |
| A managed job has no terminal result when the runtime dies | A later runtime could run the command twice. | Startup recovery marks the job lost instead of relaunching it. |
| A heartbeat is stale while only detached work remains | The VM could stay alive without caller ownership. | Idle checks distinguish detached jobs from live attachments. |
| Runtime endpoints cannot be classified as dead | Startup cleanup could remove a live owner's path. | Liveness probes retain uncertain endpoints and cleanup uses lifecycle guards. |
Key files
| File | Why to read it |
|---|---|
| crates/…/vm.rs | Main path from launch admission through VM setup, restore activation, monitors, and exit handling. |
| crates/…/launch.rs | Current boot-versus-restore contract and host policy handoff. |
| crates/…/launch.rs | Compatibility boundary for older launch formats. |
| crates/…/layout.rs | Protected sandbox storage admission and ready-state publication. |
| crates/…/coordinator.rs | Pause, checkpoint, branch, and root-last capture authority. |
| crates/…/restore.rs | Validation and installation of checkpoint state before guest activation. |
| crates/…/disk.rs | Runtime-owned root journal, growth, rollover, and compaction model. |
| crates/…/launch_staging.rs | Private staging ownership for archive preparation. |
| crates/…/tls_identity.rs and tls_receipt.rs | Host-owned signer identity and authenticated checkpoint transfer. |
crates/…/retention.rs and retention/ | Runtime configuration evidence and capture receipts. |
| crates/…/control.rs | Host control request and capability contract. |
| crates/…/dispatch.rs | Fair dispatch, bounded queues, and reply reservation. |
| crates/…/executor.rs | Single authority for fenced host mutations and lifecycle state. |
| crates/…/workload_control.rs | Private freeze/thaw channel and transport gate. |
| crates/…/relay.rs and relay/exec_admission.rs | Guest relay, bulk ordering, client ownership, and execution limits. |
| crates/…/jobs.rs | Durable managed-job state and attachment flow. |
| crates/…/metrics.rs and metrics_memory.rs | Shared metrics publication and confirmed memory limits. |
| crates/…/maintenance.rs and client/ipc.rs | Startup recovery, endpoint ownership, and safe cleanup. |