zc2 0.0.51

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! Capability tags a node advertises for its own workers (zc#403).
//!
//! A request names the tags it needs in `X-Zakuro-Requirements` (`"tags":
//! ["apple-silicon"]`) and the router only considers workers carrying all of
//! them. This module decides which tags a broker stamps onto the workers it
//! owns: `apple-silicon` is derived from the host (macOS on aarch64, the only
//! place `mlx` has wheels) and the operator can add more with
//! `ZAKURO_NODE_TAGS=a,b`. Tags a worker reports itself are kept.

/// Tag derived on macOS/aarch64 hosts.
pub const APPLE_SILICON: &str = "apple-silicon";

/// Env var: comma-separated extra tags for every worker this broker owns.
pub const NODE_TAGS_ENV: &str = "ZAKURO_NODE_TAGS";

/// Tags for a host with this `os`/`arch` (`std::env::consts` spelling) plus
/// the operator's comma-separated `extra`. Sorted, de-duplicated, trimmed.
pub fn node_tags_for(os: &str, arch: &str, extra: Option<&str>) -> Vec<String> {
    let mut tags: Vec<String> = Vec::new();
    if os == "macos" && arch == "aarch64" {
        tags.push(APPLE_SILICON.to_string());
    }
    if let Some(extra) = extra {
        tags.extend(
            extra
                .split(',')
                .map(|t| t.trim().to_ascii_lowercase())
                .filter(|t| !t.is_empty()),
        );
    }
    tags.sort();
    tags.dedup();
    tags
}

/// This host's tags: [`node_tags_for`] on the running OS/arch and the env.
pub fn local_node_tags() -> Vec<String> {
    node_tags_for(
        std::env::consts::OS,
        std::env::consts::ARCH,
        std::env::var(NODE_TAGS_ENV).ok().as_deref(),
    )
}

/// `reported` (what the worker said about itself) with `node` tags added.
pub fn merge_tags(reported: Vec<String>, node: &[String]) -> Vec<String> {
    let mut out = reported;
    for t in node {
        if !out.contains(t) {
            out.push(t.clone());
        }
    }
    out
}

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

    #[test]
    fn apple_silicon_is_macos_aarch64_only() {
        assert_eq!(node_tags_for("macos", "aarch64", None), ["apple-silicon"]);
        assert!(node_tags_for("macos", "x86_64", None).is_empty());
        assert!(node_tags_for("linux", "aarch64", None).is_empty());
        assert!(node_tags_for("linux", "x86_64", None).is_empty());
    }

    #[test]
    fn operator_tags_are_trimmed_lowercased_and_deduped() {
        assert_eq!(
            node_tags_for("macos", "aarch64", Some(" Apple-Silicon, m4 ,,m4")),
            ["apple-silicon", "m4"]
        );
        assert_eq!(node_tags_for("linux", "x86_64", Some("a100")), ["a100"]);
    }

    #[test]
    fn merge_keeps_reported_tags_and_adds_node_tags_once() {
        let node = vec!["apple-silicon".to_string()];
        let merged = merge_tags(vec!["gpu".into(), "apple-silicon".into()], &node);
        assert_eq!(merged, ["gpu", "apple-silicon"]);
        assert_eq!(merge_tags(vec![], &node), ["apple-silicon"]);
    }
}