zc2 0.0.48

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! The runner catalogue: which runner kinds this machine's hardware can host,
//! how each is isolated, and the share of the budget each one reserves
//! (desktop hosted-runners design, epic zc#493 M4).
//!
//! Every supported kind is on by default (`hosting::load_or_default`). A
//! kind that cannot run here stays listed with the reason, so the app can say
//! why it is missing instead of hiding it.

use crate::bench_node::fingerprint::GpuId;
use crate::sandbox::HostResources;
use serde::Serialize;

const GIB: u64 = 1024 * 1024 * 1024;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RunnerKind {
    /// L'Atelier LLM LoRA fine-tuning (peft; MLX export on Apple Silicon).
    AtelierPeft,
    /// L'Atelier image / tabular training (Ludwig).
    AtelierLudwig,
    /// Spaces batches.
    Spaces,
    /// Environment guests (offers).
    Environments,
}

impl RunnerKind {
    pub const ALL: [RunnerKind; 4] = [
        RunnerKind::AtelierPeft,
        RunnerKind::AtelierLudwig,
        RunnerKind::Spaces,
        RunnerKind::Environments,
    ];

    /// Stable id used in the local API, `hosting.toml` and the app.
    pub fn id(&self) -> &'static str {
        match self {
            RunnerKind::AtelierPeft => "atelier-peft",
            RunnerKind::AtelierLudwig => "atelier-ludwig",
            RunnerKind::Spaces => "spaces",
            RunnerKind::Environments => "environments",
        }
    }

    pub fn from_id(id: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|k| k.id() == id)
    }

    /// The Atelier backend name (`[workloads].atelier` entries), if any.
    pub fn atelier_backend(&self) -> Option<&'static str> {
        match self {
            RunnerKind::AtelierPeft => Some("peft"),
            RunnerKind::AtelierLudwig => Some("ludwig"),
            _ => None,
        }
    }
}

/// Where the runner runs. `Host` has no isolation (ZC-0018): Apple GPU work
/// needs Metal, which a Lima VM cannot reach.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Isolation {
    Sandbox,
    Host,
}

/// The share of the budget one runner holds while it is deployed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize)]
pub struct Reservation {
    pub cpu_millis: u32,
    pub memory_bytes: u64,
    pub vram_bytes: u64,
    pub disk_bytes: u64,
}

#[derive(Debug, Clone, PartialEq)]
pub struct CatalogueEntry {
    pub kind: RunnerKind,
    pub supported: bool,
    /// Why it is unsupported: `no-gpu`, `mps-unverified`.
    pub reason: Option<&'static str>,
    pub isolation: Isolation,
    pub reservation: Reservation,
}

/// What the catalogue is derived from.
#[derive(Debug, Clone)]
pub struct Hardware {
    pub apple_silicon: bool,
    /// CUDA cards only; Apple unified memory is not VRAM.
    pub nvidia: Vec<GpuId>,
    pub host: HostResources,
}

impl Hardware {
    pub fn detect() -> Self {
        let nvidia = (crate::agent::offers::real_probe())()
            .unwrap_or_default()
            .into_iter()
            .filter(|g| g.vendor.to_ascii_lowercase().contains("nvidia"))
            .collect();
        Self {
            apple_silicon: cfg!(all(target_os = "macos", target_arch = "aarch64")),
            nvidia,
            host: HostResources::detect(),
        }
    }
}

fn reservation(cpu_millis: u32, memory_gib: u64, vram_gib: u64, disk_gib: u64) -> Reservation {
    Reservation {
        cpu_millis,
        memory_bytes: memory_gib * GIB,
        vram_bytes: vram_gib * GIB,
        disk_bytes: disk_gib * GIB,
    }
}

/// Every runner kind, with whether `hw` can host it. Default reservations
/// were measured 2026-11-10 (resnet18 @64 px peaks at 1.4 GiB).
pub fn catalogue(hw: &Hardware) -> Vec<CatalogueEntry> {
    let cuda = !hw.nvidia.is_empty();
    let unsupported = |kind, reason| CatalogueEntry {
        kind,
        supported: false,
        reason: Some(reason),
        isolation: Isolation::Sandbox,
        reservation: Reservation::default(),
    };
    let supported = |kind, isolation, reservation| CatalogueEntry {
        kind,
        supported: true,
        reason: None,
        isolation,
        reservation,
    };
    let peft = if hw.apple_silicon {
        supported(
            RunnerKind::AtelierPeft,
            Isolation::Host,
            reservation(2000, 8, 0, 30),
        )
    } else if cuda {
        supported(
            RunnerKind::AtelierPeft,
            Isolation::Sandbox,
            reservation(2000, 8, 8, 30),
        )
    } else {
        unsupported(RunnerKind::AtelierPeft, "no-gpu")
    };
    let ludwig = if cuda {
        supported(
            RunnerKind::AtelierLudwig,
            Isolation::Sandbox,
            reservation(2000, 4, 4, 20),
        )
    } else if hw.apple_silicon {
        unsupported(RunnerKind::AtelierLudwig, "mps-unverified")
    } else {
        unsupported(RunnerKind::AtelierLudwig, "no-gpu")
    };
    vec![
        peft,
        ludwig,
        supported(
            RunnerKind::Spaces,
            Isolation::Sandbox,
            reservation(1000, 2, 0, 10),
        ),
        supported(
            RunnerKind::Environments,
            Isolation::Sandbox,
            reservation(1000, 2, 0, 10),
        ),
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bench_node::fingerprint::GpuId;
    use crate::sandbox::HostResources;

    const GIB: u64 = 1024 * 1024 * 1024;

    fn host() -> HostResources {
        HostResources {
            logical_cpus: 10,
            memory_bytes: Some(16 * GIB),
        }
    }

    fn mac() -> Hardware {
        Hardware {
            apple_silicon: true,
            nvidia: vec![],
            host: host(),
        }
    }

    fn nvidia() -> Hardware {
        Hardware {
            apple_silicon: false,
            nvidia: vec![GpuId {
                vendor: "nvidia".into(),
                model: "NVIDIA GeForce GTX 1070".into(),
                uuid: "GPU-1".into(),
                vram_bytes: 8 * GIB,
            }],
            host: host(),
        }
    }

    fn cpu_only() -> Hardware {
        Hardware {
            apple_silicon: false,
            nvidia: vec![],
            host: host(),
        }
    }

    fn entry(hw: &Hardware, kind: RunnerKind) -> CatalogueEntry {
        catalogue(hw).into_iter().find(|e| e.kind == kind).unwrap()
    }

    #[test]
    fn apple_silicon_offers_peft_on_host() {
        let e = entry(&mac(), RunnerKind::AtelierPeft);
        assert!(e.supported);
        assert_eq!(e.isolation, Isolation::Host);
        assert_eq!(e.reservation.memory_bytes, 8 * GIB);
        assert_eq!(e.reservation.vram_bytes, 0);
        assert_eq!(e.reservation.disk_bytes, 30 * GIB);
    }

    #[test]
    fn apple_silicon_ludwig_is_unverified() {
        let e = entry(&mac(), RunnerKind::AtelierLudwig);
        assert!(!e.supported);
        assert_eq!(e.reason, Some("mps-unverified"));
    }

    #[test]
    fn nvidia_offers_both_atelier_in_sandbox() {
        let peft = entry(&nvidia(), RunnerKind::AtelierPeft);
        assert!(peft.supported);
        assert_eq!(peft.isolation, Isolation::Sandbox);
        assert_eq!(peft.reservation.memory_bytes, 8 * GIB);
        assert_eq!(peft.reservation.vram_bytes, 8 * GIB);
        let ludwig = entry(&nvidia(), RunnerKind::AtelierLudwig);
        assert!(ludwig.supported);
        assert_eq!(ludwig.isolation, Isolation::Sandbox);
        assert_eq!(ludwig.reservation.memory_bytes, 4 * GIB);
        assert_eq!(ludwig.reservation.vram_bytes, 4 * GIB);
        assert_eq!(ludwig.reservation.disk_bytes, 20 * GIB);
    }

    #[test]
    fn cpu_only_has_no_atelier() {
        for kind in [RunnerKind::AtelierPeft, RunnerKind::AtelierLudwig] {
            let e = entry(&cpu_only(), kind);
            assert!(!e.supported);
            assert_eq!(e.reason, Some("no-gpu"));
        }
        let spaces = entry(&cpu_only(), RunnerKind::Spaces);
        assert!(spaces.supported);
        assert_eq!(spaces.reservation.memory_bytes, 2 * GIB);
        assert_eq!(spaces.reservation.cpu_millis, 1000);
        assert_eq!(spaces.reservation.disk_bytes, 10 * GIB);
    }

    #[test]
    fn ids_are_stable() {
        assert_eq!(RunnerKind::AtelierPeft.id(), "atelier-peft");
        assert_eq!(RunnerKind::AtelierLudwig.id(), "atelier-ludwig");
        assert_eq!(RunnerKind::Spaces.id(), "spaces");
        assert_eq!(RunnerKind::Environments.id(), "environments");
    }
}