zc2 0.0.47

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! Environment offers (the provider catalogue): the hub's answer plus, per
//! offer, whether this machine can run the environment (`fits`).

use crate::bench_node::fingerprint::GpuId;
use serde_json::Value;

/// A failed offers call, already in the local API's envelope: `code` is a
/// string because a hub rejection brings its own.
#[derive(Debug, Clone, PartialEq)]
pub struct OfferApiError {
    pub status: u16,
    pub code: String,
    pub message: String,
}

impl OfferApiError {
    pub fn new(status: u16, code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            status,
            code: code.into(),
            message: message.into(),
        }
    }
}

/// How this machine's GPUs are enumerated (bounded: `cuInit` can hang).
/// `None` means the probe gave no answer, i.e. hardware unknown.
pub type GpuProbe = Box<dyn Fn() -> Option<Vec<GpuId>> + Send + Sync>;

pub fn real_probe() -> GpuProbe {
    Box::new(|| {
        crate::bench_node::run::enumerate_with_timeout(&std::sync::atomic::AtomicBool::new(false))
    })
}

fn gb(n: f64) -> String {
    if n.fract() == 0.0 {
        format!("{}", n as u64)
    } else {
        format!("{n}")
    }
}

/// `(fits, fit_reason)` for one template. Only CUDA cards count as GPUs (the
/// environments run under `docker --gpus`; Apple unified memory is not VRAM),
/// and VRAM is per card, like a Space's requirement. Unknown hardware is
/// `(None, None)`: never a guess.
pub fn fit(template: &Value, gpus: Option<&[GpuId]>) -> (Option<bool>, Option<String>) {
    let Some(gpus) = gpus else {
        return (None, None);
    };
    let min_vram = template
        .get("min_vram_gb")
        .and_then(Value::as_f64)
        .filter(|v| *v > 0.0);
    let needs_gpu = template
        .get("gpu_required")
        .and_then(Value::as_bool)
        .unwrap_or(false)
        || min_vram.is_some();
    if !needs_gpu {
        return (Some(true), None);
    }
    let best = gpus
        .iter()
        .filter(|g| g.vendor.eq_ignore_ascii_case("nvidia"))
        .map(|g| g.vram_bytes)
        .max();
    let Some(best) = best else {
        return (Some(false), Some("needs a GPU".into()));
    };
    if let Some(need) = min_vram {
        // Drivers report slightly under the marketing size: a "24 GB" card
        // shows 23.6 GiB. Compare with a 5% tolerance.
        let have = best as f64 / (1u64 << 30) as f64;
        if have < need * 0.95 {
            return (Some(false), Some(format!("needs {} GB VRAM", gb(need))));
        }
    }
    (Some(true), None)
}

/// Sets `fits` / `fit_reason` on one offer object, from its `template`.
pub fn annotate_offer(offer: &mut Value, gpus: Option<&[GpuId]>) {
    let (fits, reason) = fit(offer.get("template").unwrap_or(&Value::Null), gpus);
    if let Some(o) = offer.as_object_mut() {
        o.insert("fits".into(), fits.map_or(Value::Null, Value::Bool));
        o.insert(
            "fit_reason".into(),
            reason.map_or(Value::Null, Value::String),
        );
    }
}

/// The hub's `GET` body with every offer annotated.
pub fn annotate_all(body: &mut Value, gpus: Option<&[GpuId]>) {
    if let Some(list) = body.get_mut("offers").and_then(Value::as_array_mut) {
        for o in list {
            annotate_offer(o, gpus);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn gpu(vendor: &str, gib: u64) -> GpuId {
        GpuId {
            vendor: vendor.into(),
            model: "m".into(),
            uuid: "u".into(),
            vram_bytes: gib << 30,
        }
    }

    #[test]
    fn unknown_hardware_is_null_never_a_guess() {
        let t = json!({"gpu_required": true, "min_vram_gb": 8});
        assert_eq!(fit(&t, None), (None, None));
        assert_eq!(fit(&json!({}), None), (None, None));
    }

    #[test]
    fn cpu_templates_fit_any_known_machine() {
        assert_eq!(
            fit(&json!({"gpu_required": false}), Some(&[])),
            (Some(true), None)
        );
    }

    #[test]
    fn gpu_templates_need_a_cuda_card_with_enough_vram() {
        let t = json!({"gpu_required": true, "min_vram_gb": 8});
        let needs_gpu = (Some(false), Some("needs a GPU".to_string()));
        assert_eq!(fit(&t, Some(&[])), needs_gpu);
        assert_eq!(
            fit(&t, Some(&[gpu("apple", 64)])),
            needs_gpu,
            "Metal is not CUDA"
        );
        assert_eq!(
            fit(&t, Some(&[gpu("nvidia", 6)])),
            (Some(false), Some("needs 8 GB VRAM".to_string()))
        );
        assert_eq!(
            fit(&t, Some(&[gpu("nvidia", 6), gpu("nvidia", 24)])),
            (Some(true), None)
        );
        let no_min = json!({"gpu_required": true, "min_vram_gb": null});
        assert_eq!(fit(&no_min, Some(&[gpu("nvidia", 4)])), (Some(true), None));
    }

    #[test]
    fn annotate_keeps_the_hub_body_and_adds_two_fields() {
        let mut body = json!({"node": "n", "categories": ["a"], "offers": [
            {"template": {"id": "x", "gpu_required": true, "min_vram_gb": 8}, "hosts": 3}]});
        annotate_all(&mut body, Some(&[gpu("nvidia", 4)]));
        assert_eq!(body["node"], "n");
        assert_eq!(body["offers"][0]["hosts"], 3);
        assert_eq!(body["offers"][0]["fits"], false);
        assert_eq!(body["offers"][0]["fit_reason"], "needs 8 GB VRAM");
        annotate_all(&mut body, None);
        assert!(body["offers"][0]["fits"].is_null() && body["offers"][0]["fit_reason"].is_null());
    }
}