Skip to main content

zlayer_agent/
error.rs

1//! Agent-specific errors
2
3use std::time::Duration;
4use thiserror::Error;
5
6/// Agent runtime errors
7#[derive(Debug, Error)]
8pub enum AgentError {
9    /// Container not found
10    #[error("Container '{container}' not found: {reason}")]
11    NotFound { container: String, reason: String },
12
13    /// Failed to pull image
14    #[error("Failed to pull image '{image}': {reason}")]
15    PullFailed { image: String, reason: String },
16
17    /// Failed to create container
18    #[error("Failed to create container '{id}': {reason}")]
19    CreateFailed { id: String, reason: String },
20
21    /// Failed to start container
22    #[error("Failed to start container '{id}': {reason}")]
23    StartFailed { id: String, reason: String },
24
25    /// Container exited unexpectedly
26    #[error("Container '{id}' exited unexpectedly with code {code}")]
27    UnexpectedExit { id: String, code: i32 },
28
29    /// Health check failed
30    #[error("Health check failed for '{id}': {reason}")]
31    HealthCheckFailed { id: String, reason: String },
32
33    /// Init action failed
34    #[error("Init action failed for '{id}': {reason}")]
35    InitActionFailed { id: String, reason: String },
36
37    /// Timeout
38    #[error("Timeout after {timeout:?}")]
39    Timeout { timeout: Duration },
40
41    /// Dependency timeout - service waiting for dependency condition
42    #[error("Dependency timeout: '{service}' waiting for '{dependency}' ({condition}) after {timeout:?}")]
43    DependencyTimeout {
44        service: String,
45        dependency: String,
46        condition: String,
47        timeout: Duration,
48    },
49
50    /// Invalid spec
51    #[error("Invalid spec: {0}")]
52    InvalidSpec(String),
53
54    /// Network setup or operation failed
55    #[error("Network error: {0}")]
56    Network(String),
57
58    /// Configuration error (missing or invalid configuration)
59    #[error("Configuration error: {0}")]
60    Configuration(String),
61
62    /// Internal runtime error
63    #[error("Internal error: {0}")]
64    Internal(String),
65
66    /// Operation is not supported by this runtime
67    #[error("Operation not supported by this runtime: {0}")]
68    Unsupported(String),
69
70    /// GPU was requested by the service spec, but the underlying WSL2 host
71    /// cannot deliver GPU access (typically because `/dev/dxg` is not exposed
72    /// by the running WSL2 kernel, or the `WSLg` driver shim mount is missing).
73    ///
74    /// Returned by the WSL2 delegate when wiring `/dev/dxg` and the `WSLg` lib
75    /// mounts into the youki bundle. Silent CPU fallback would be surprising
76    /// for users who explicitly asked for a GPU, so this is a hard error;
77    /// callers must either downgrade the spec to drop `resources.gpu` or
78    /// re-place the workload on a node whose WSL2 distro exposes the
79    /// `DirectX` kernel interface.
80    #[error("GPU requested but WSL2 GPU support not available on this host: {reason}")]
81    WslGpuUnavailable { reason: String },
82
83    /// GPU sharing was requested (MPS or time-slicing) but the host or
84    /// runtime cannot satisfy the requested mode.
85    ///
86    /// Typical causes:
87    /// * `mode = "mps"` but the host MPS pipe / log directory does not exist
88    ///   (the `nvidia-cuda-mps-control` daemon is not running).
89    /// * `mode = "mps"` combined with `isolation: hyperv` on Windows — MPS is
90    ///   not exposed inside the UVM kernel.
91    ///
92    /// Silent fallback to exclusive-mode access would be surprising for users
93    /// who explicitly opted in to sharing (they may be relying on sharing for
94    /// capacity planning), so this is a hard error. Callers must either fix
95    /// the host (start the MPS daemon, switch isolation) or drop the
96    /// `sharing` field from the spec.
97    #[error("GPU sharing mode '{mode}' is unavailable: {reason}")]
98    GpuSharingUnavailable {
99        /// Sharing mode that could not be satisfied (`"mps"`, `"time-slice"`).
100        mode: String,
101        /// Human-readable explanation (e.g. "/tmp/nvidia-mps does not exist; \
102        /// ensure nvidia-cuda-mps-control is running").
103        reason: String,
104    },
105
106    /// The workload cannot run on this node and must be re-placed on a peer
107    /// that can satisfy `required_os`.
108    ///
109    /// Returned by [`crate::runtimes::composite::CompositeRuntime::select_for`]
110    /// when a foreign-OS workload (today: Linux on a Windows node) lands on a
111    /// node that has no suitable local runtime (e.g. no WSL2 delegate
112    /// configured). The scheduler is expected to catch this and re-dispatch
113    /// to a cluster peer whose `NodeState.os` matches `required_os`. When no
114    /// capable peer exists the scheduler marks the service failed with an
115    /// actionable message naming both remediations (enable the local WSL2
116    /// delegate, or add a Linux peer to the cluster).
117    ///
118    /// This variant is *not* a container failure: the service manager must
119    /// surface it to the scheduler and must not roll up `CreateFailed` on top
120    /// of it, otherwise the rescheduling signal is lost.
121    #[error(
122        "route-to-peer: service '{service}' requires OS '{required_os}' on another node: {reason}"
123    )]
124    RouteToPeer {
125        /// Service name that needs to be re-placed.
126        service: String,
127        /// OS the workload requires (OCI-canonical: `linux` / `windows` / `darwin`).
128        required_os: String,
129        /// Human-readable explanation (e.g. "no WSL2 delegate configured on this Windows node").
130        reason: String,
131    },
132
133    /// The local runtime cannot service this image because the image's OS
134    /// does not match the runtime's expected OS.
135    ///
136    /// Returned by the HCS runtime when an image's OCI config reports
137    /// `os != "windows"` (e.g. a Linux alpine image landing on a Windows host
138    /// that also has a WSL2 delegate). Calling `vmcompute.dll!ProcessBaseImage`
139    /// on a non-Windows base layer is guaranteed to fail with
140    /// `ERROR_PATH_NOT_FOUND (0x80070003)` because the HCS API expects the
141    /// Windows-specific `Hives/` / `UtilityVM/` / `Files/Windows/System32/`
142    /// layout. Bailing early with this variant lets the composite runtime
143    /// treat the call as a soft skip (the delegate's parallel pull is the one
144    /// that actually owns the image) instead of failing the whole pull.
145    ///
146    /// This is *not* a container failure: callers in the composite layer
147    /// should distinguish this from a real `PullFailed` and continue with the
148    /// delegate's result.
149    #[error(
150        "wrong-platform: {runtime} runtime cannot handle image '{image}' (expected os={expected}, got os={actual})"
151    )]
152    WrongPlatform {
153        /// Identifier of the runtime that rejected the image (e.g. `"hcs"`,
154        /// `"wsl2"`).
155        runtime: String,
156        /// OCI-canonical OS this runtime expects (e.g. `"windows"`, `"linux"`).
157        expected: String,
158        /// OCI-canonical OS the image manifest reports.
159        actual: String,
160        /// Image reference that triggered the mismatch.
161        image: String,
162    },
163}
164
165pub type Result<T, E = AgentError> = std::result::Result<T, E>;