Skip to main content

hyprstream_core/storage/
release_store.rs

1//! Release registry manifest types for variant-aware distribution.
2//!
3//! The release registry is a git2db bare repo (`hyprstream-releases.git`) containing
4//! per-variant backends with XET-stored binaries. Manifests describe available variants
5//! and their requirements.
6
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9use std::path::Path;
10
11/// Root manifest for the release registry (`manifest.toml`).
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct ReleaseManifest {
14    pub release: ReleaseInfo,
15    pub variants: HashMap<String, VariantRequirements>,
16}
17
18/// Release-level metadata.
19#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct ReleaseInfo {
21    pub version: String,
22    pub libtorch_version: String,
23    /// ABI compatibility (e.g., "pre-cxx11", "cxx11")
24    pub abi: String,
25}
26
27/// Host requirements for a variant.
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct VariantRequirements {
30    /// What the host must have (e.g., "glibc", "nvidia-driver", "rocm")
31    pub host_requires: String,
32}
33
34/// Per-variant manifest (`backends/<variant>/manifest.toml`).
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct VariantManifest {
37    pub variant: VariantInfo,
38    #[serde(default)]
39    pub host: Option<HostRequirements>,
40    #[serde(default)]
41    pub files: Option<FileCategories>,
42}
43
44/// Variant identification and sizing.
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct VariantInfo {
47    pub id: String,
48    #[serde(default)]
49    pub binary_sha256: Option<String>,
50    #[serde(default)]
51    pub libtorch_sha256: Option<String>,
52    #[serde(default)]
53    pub total_size_bytes: Option<u64>,
54    #[serde(default)]
55    pub runtime_size_bytes: Option<u64>,
56}
57
58/// Host requirements for GPU variants.
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct HostRequirements {
61    #[serde(default)]
62    pub min_driver_version: Option<String>,
63    #[serde(default)]
64    pub cuda_compat: Option<String>,
65    #[serde(default)]
66    pub rocm_version: Option<String>,
67}
68
69/// File categories for selective download.
70///
71/// Only `runtime` files are downloaded by default. `dev` and `test` are skipped
72/// to save ~500MB per variant.
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct FileCategories {
75    /// Files needed at runtime (binary + .so libs). Downloaded by default.
76    #[serde(default)]
77    pub runtime: Vec<String>,
78    /// Development files (headers, cmake configs). Skipped by default.
79    #[serde(default)]
80    pub dev: Vec<String>,
81    /// Test files (test binaries, python bindings). Skipped by default.
82    #[serde(default)]
83    pub test: Vec<String>,
84}
85
86impl ReleaseManifest {
87    /// Parse from a TOML string.
88    pub fn from_toml(s: &str) -> Result<Self, toml::de::Error> {
89        toml::from_str(s)
90    }
91
92    /// Read from a file path.
93    pub fn from_file(path: &Path) -> anyhow::Result<Self> {
94        let content = std::fs::read_to_string(path)?;
95        Ok(Self::from_toml(&content)?)
96    }
97
98    /// Get variant IDs that are available.
99    pub fn variant_ids(&self) -> Vec<&str> {
100        self.variants
101            .keys()
102            .map(std::string::String::as_str)
103            .collect()
104    }
105}
106
107impl VariantManifest {
108    /// Parse from a TOML string.
109    pub fn from_toml(s: &str) -> Result<Self, toml::de::Error> {
110        toml::from_str(s)
111    }
112
113    /// Read from a file path.
114    pub fn from_file(path: &Path) -> anyhow::Result<Self> {
115        let content = std::fs::read_to_string(path)?;
116        Ok(Self::from_toml(&content)?)
117    }
118
119    /// Get pathspec prefixes for selective checkout (runtime files only).
120    ///
121    /// Returns paths suitable for `DriverOpts::checkout_paths`.
122    /// Uses the variant ID from the manifest itself to prevent caller mismatch.
123    pub fn runtime_pathspecs(&self) -> Vec<String> {
124        let variant_id = &self.variant.id;
125        let mut paths = vec![format!("backends/{variant_id}/")];
126
127        // If file categories are specified, use the explicit runtime list
128        if let Some(ref files) = self.files {
129            if !files.runtime.is_empty() {
130                paths.clear();
131                for f in &files.runtime {
132                    paths.push(format!("backends/{variant_id}/{f}"));
133                }
134            }
135        }
136
137        // Always include root manifest
138        paths.push("manifest.toml".to_owned());
139        paths
140    }
141}
142
143#[cfg(test)]
144mod tests {
145    use super::*;
146
147    #[test]
148    fn test_parse_release_manifest() -> Result<(), Box<dyn std::error::Error>> {
149        let toml = r#"
150[release]
151version = "0.1.0"
152libtorch_version = "2.11.0"
153abi = "pre-cxx11"
154
155[variants]
156cpu = { host_requires = "glibc" }
157cuda130 = { host_requires = "nvidia-driver" }
158rocm71 = { host_requires = "rocm" }
159"#;
160        let manifest = ReleaseManifest::from_toml(toml)?;
161        assert_eq!(manifest.release.version, "0.1.0");
162        assert_eq!(manifest.variants.len(), 3);
163        assert!(manifest.variants.contains_key("cuda130"));
164        Ok(())
165    }
166
167    #[test]
168    fn test_parse_variant_manifest() -> Result<(), Box<dyn std::error::Error>> {
169        let toml = r#"
170[variant]
171id = "cuda130"
172total_size_bytes = 2500000000
173runtime_size_bytes = 1800000000
174
175[host]
176min_driver_version = "535.0"
177cuda_compat = "13.0"
178
179[files]
180runtime = [
181    "hyprstream",
182    "libtorch/lib/libtorch.so",
183    "libtorch/lib/libtorch_cpu.so",
184    "libtorch/lib/libtorch_cuda.so",
185    "libtorch/lib/libc10.so",
186]
187dev = ["libtorch/include/**"]
188test = ["libtorch/lib/*_test*"]
189"#;
190        let manifest = VariantManifest::from_toml(toml)?;
191        assert_eq!(manifest.variant.id, "cuda130");
192        let host = manifest.host.as_ref().ok_or("host missing")?;
193        assert_eq!(host.min_driver_version, Some("535.0".to_owned()));
194        let paths = manifest.runtime_pathspecs();
195        assert!(paths.contains(&"manifest.toml".to_owned()));
196        assert!(paths.iter().any(|p| p.contains("libtorch")));
197        Ok(())
198    }
199
200    #[test]
201    fn test_runtime_pathspecs_default() -> Result<(), Box<dyn std::error::Error>> {
202        let toml = r#"
203[variant]
204id = "cpu"
205"#;
206        let manifest = VariantManifest::from_toml(toml)?;
207        let paths = manifest.runtime_pathspecs();
208        assert_eq!(paths, vec!["backends/cpu/", "manifest.toml"]);
209        Ok(())
210    }
211}