mxc-sdk 1.2.0

Importable library for starting MXC sandboxes in-process, with streaming stdio.
Documentation
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Typed failure modes for the transient one-shot Windows Sandbox runner.
//!
//! Each variant carries a developer-facing detail string and maps to a
//! [`ScriptResponse`] with a user-facing `error_message` plus the raw detail
//! in `extended_error`. Keeping the variants distinct (rather than a single
//! opaque string) lets the orchestration code attribute a failure to a
//! specific lifecycle phase and lays the groundwork for richer typed wire
//! errors in the state-aware backend.

use crate::mxc_common::models::{FailurePhase, ScriptResponse};

/// Failure modes of a single transient one-shot execution.
#[derive(Debug)]
pub(crate) enum OneShotError {
    SandboxUnavailable(String),
    Busy(String),
    RuntimeSetup(String),
    Launch(String),
    Policy(String),
    Exec(String),
}

impl OneShotError {
    /// Render this error as a [`ScriptResponse`]. The user-facing summary
    /// lands in both `standard_err` and `error_message`; the raw detail lands
    /// in `extended_error` for diagnostics.
    pub(crate) fn into_response(self) -> ScriptResponse {
        let failure_phase = self.failure_phase();
        let (summary, detail): (&str, String) = match self {
            OneShotError::SandboxUnavailable(d) => (
                "Windows Sandbox is not available on this host. Enable the \
                 'Containers-DisposableClientVM' optional feature and reboot.",
                d,
            ),
            OneShotError::Busy(d) => (
                "Windows Sandbox is already running and was not started by a \
                 disposable MXC run. Refusing to start a one-shot sandbox \
                 (the host allows only one running instance). Close the \
                 existing sandbox and retry.",
                d,
            ),
            OneShotError::RuntimeSetup(d) => (
                "Failed to initialise the async runtime for Windows Sandbox.",
                d,
            ),
            OneShotError::Launch(d) => (
                "Failed to launch the Windows Sandbox VM or connect to the guest agent.",
                d,
            ),
            OneShotError::Policy(d) => (
                "The request's policy cannot be enforced by the Windows Sandbox \
                 backend.",
                d,
            ),
            OneShotError::Exec(d) => ("Execution inside the Windows Sandbox failed.", d),
        };

        ScriptResponse {
            standard_err: summary.to_string(),
            extended_error: detail,
            ..ScriptResponse::failed(failure_phase, summary)
        }
    }

    /// Lifecycle phase this error is attributed to, so a caller can decide
    /// whether a retry could ever succeed.
    fn failure_phase(&self) -> FailurePhase {
        match self {
            // The optional OS feature is absent or disabled, so the host has to
            // be provisioned; no change to the request reaches it.
            OneShotError::SandboxUnavailable(_) => FailurePhase::BackendUnavailable,
            // The request asks for something this backend will not do.
            OneShotError::Policy(_) => FailurePhase::Rejected,
            // Launch attempt failed (incl. transient single-instance contention,
            // async-runtime setup, capture-proof, rendezvous wait, and the
            // initial guest connect) — generally worth retrying.
            OneShotError::Busy(_) | OneShotError::RuntimeSetup(_) | OneShotError::Launch(_) => {
                FailurePhase::LaunchFailed
            }
            // The execution relay failed while running user code.
            OneShotError::Exec(_) => FailurePhase::PostLaunchFailed,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mxc_common::mxc_error::MxcErrorCode;

    #[test]
    fn busy_maps_to_launch_failed_with_detail() {
        let resp = OneShotError::Busy("vm already up".to_string()).into_response();
        assert_eq!(resp.exit_code, -1);
        assert_eq!(resp.failure_phase, FailurePhase::LaunchFailed);
        assert_eq!(resp.extended_error, "vm already up");
        assert!(!resp.error_message.is_empty());
    }

    #[test]
    fn a_refused_policy_is_told_apart_from_a_host_that_cannot_run_it() {
        let refused = OneShotError::Policy("denied path in share".to_string()).into_response();
        assert_eq!(refused.failure_phase, FailurePhase::Rejected);
        assert_eq!(refused.exit_code, 1);
        assert_eq!(
            refused.failure_phase.error_code(),
            MxcErrorCode::PolicyValidation
        );

        let unavailable =
            OneShotError::SandboxUnavailable("feature off".to_string()).into_response();
        assert_eq!(unavailable.failure_phase, FailurePhase::BackendUnavailable);
        assert_eq!(unavailable.exit_code, -1);
        assert_eq!(
            unavailable.failure_phase.error_code(),
            MxcErrorCode::BackendUnavailable
        );
    }

    #[test]
    fn launch_and_exec_errors_map_to_post_launch_phases() {
        // After `Rendezvous` / `Connect` merged into `Launch`, only Launch +
        // Exec remain as the post-pre-flight
        // failure categories. Launch -> retry-the-launch
        // (LaunchFailed); Exec -> the relay broke after a successful
        // launch (PostLaunchFailed).
        assert_eq!(
            OneShotError::Launch("rendezvous timed out after 360s".to_string())
                .into_response()
                .failure_phase,
            FailurePhase::LaunchFailed
        );
        assert_eq!(
            OneShotError::Exec("relay broke".to_string())
                .into_response()
                .failure_phase,
            FailurePhase::PostLaunchFailed
        );
    }

    #[test]
    fn launch_detail_preserved_in_extended_error() {
        let resp =
            OneShotError::Launch("rendezvous timed out after 360s".to_string()).into_response();
        assert_eq!(resp.extended_error, "rendezvous timed out after 360s");
        assert!(resp.error_message.contains("launch"));
    }
}