Skip to main content

rigspark_runtime/catalog_update/
store.rs

1use super::{
2    CatalogUpdateError, MAX_ARTIFACT_BYTES, SignedCatalog, VerifiedCatalog, decode_hex, verify,
3};
4use crate::secure_fs::Directory;
5use cap_fs_ext::{FollowSymlinks, OpenOptionsFollowExt};
6use rigspark_core::catalog::Catalog;
7use serde::{Deserialize, Serialize};
8use sha2::{Digest, Sha256};
9use std::{
10    io::ErrorKind,
11    path::{Path, PathBuf},
12};
13
14const STATE: &str = "catalog/snapshots.json";
15const MAX_STATE_BYTES: u64 = (MAX_ARTIFACT_BYTES * 3) as u64;
16
17#[derive(Serialize)]
18#[serde(rename_all = "camelCase")]
19pub struct CatalogStatus {
20    pub source: &'static str,
21    pub revision: Option<u64>,
22    pub digest: String,
23    pub generated_at: String,
24    pub published_at: Option<String>,
25    pub model_count: usize,
26    /// Models the user added from Hugging Face, included in `model_count`.
27    pub local_count: usize,
28    pub updates_configured: bool,
29    pub warnings: Vec<String>,
30}
31
32pub struct LoadedCatalog {
33    pub catalog: Catalog,
34    pub raw: String,
35    pub status: CatalogStatus,
36}
37
38#[derive(Serialize, Deserialize)]
39#[serde(rename_all = "camelCase", deny_unknown_fields)]
40struct Snapshots {
41    version: u8,
42    highest_revision: u64,
43    highest_digest: String,
44    current: SignedCatalog,
45    previous: Option<SignedCatalog>,
46}
47
48pub struct CatalogStore {
49    home: PathBuf,
50    pub(super) key: Option<[u8; 32]>,
51}
52
53impl CatalogStore {
54    pub fn new(home: impl Into<PathBuf>, key: Option<[u8; 32]>) -> Self {
55        Self {
56            home: home.into(),
57            key,
58        }
59    }
60
61    pub fn official(home: impl Into<PathBuf>) -> Self {
62        let key = decode_hex(include_str!("../catalog_public_key.hex").trim()).ok();
63        Self::new(home, key)
64    }
65
66    fn read_state(&self) -> Result<Option<Snapshots>, CatalogUpdateError> {
67        let result = Directory::open(&self.home)
68            .and_then(|directory| directory.read(Path::new(STATE), MAX_STATE_BYTES, false));
69        let bytes = match result {
70            Ok(bytes) => bytes,
71            Err(error) if error.kind() == ErrorKind::NotFound => return Ok(None),
72            Err(error) => return Err(error.into()),
73        };
74        let state: Snapshots = serde_json::from_slice(&bytes)
75            .map_err(|_| CatalogUpdateError::Invalid("cache state"))?;
76        if state.version != 1
77            || state.highest_revision == 0
78            || decode_hex::<32>(&state.highest_digest).is_err()
79        {
80            return Err(CatalogUpdateError::Invalid(
81                "cache version or high-water mark",
82            ));
83        }
84        Ok(Some(state))
85    }
86
87    fn verify_cached(
88        &self,
89        envelope: &SignedCatalog,
90    ) -> Result<VerifiedCatalog, CatalogUpdateError> {
91        let bytes = serde_json::to_vec(envelope)
92            .map_err(|_| CatalogUpdateError::Invalid("cache envelope"))?;
93        verify(&bytes, &self.key.ok_or(CatalogUpdateError::NotConfigured)?)
94    }
95
96    fn selected(
97        &self,
98        snapshot: VerifiedCatalog,
99        source: &'static str,
100        warnings: Vec<String>,
101    ) -> LoadedCatalog {
102        let status = CatalogStatus {
103            source,
104            revision: Some(snapshot.revision),
105            digest: snapshot.digest,
106            generated_at: snapshot.catalog.generated_at.clone(),
107            published_at: Some(snapshot.published_at),
108            model_count: snapshot.catalog.models.len(),
109            local_count: 0,
110            updates_configured: self.key.is_some(),
111            warnings,
112        };
113        LoadedCatalog {
114            catalog: snapshot.catalog,
115            raw: snapshot.raw,
116            status,
117        }
118    }
119
120    /// The signed or bundled catalog plus the models the user added from Hugging Face.
121    pub fn load(&self) -> Result<LoadedCatalog, CatalogUpdateError> {
122        let mut loaded = self.load_snapshot()?;
123        match crate::local_catalog::LocalCatalog::new(&self.home).models() {
124            Ok(added) => {
125                let added: Vec<_> = added
126                    .into_iter()
127                    .filter(|entry| {
128                        !loaded
129                            .catalog
130                            .models
131                            .iter()
132                            .any(|model| model.id == entry.id)
133                    })
134                    .collect();
135                if !added.is_empty() {
136                    let mut catalog = loaded.catalog.clone();
137                    catalog.models.extend(added.iter().cloned());
138                    catalog.schema_version =
139                        rigspark_core::catalog::required_schema_version(&catalog.models)
140                            .max(catalog.schema_version)
141                            .max(3);
142                    let raw = serde_json::to_string(&catalog)
143                        .map_err(|_| CatalogUpdateError::Invalid("merged catalog"))?;
144                    let catalog = Catalog::parse(&raw)
145                        .map_err(|_| CatalogUpdateError::Invalid("merged catalog"))?;
146                    loaded.status.local_count = added.len();
147                    loaded.status.model_count = catalog.models.len();
148                    loaded.catalog = catalog;
149                    loaded.raw = raw;
150                }
151            }
152            Err(error) => loaded
153                .status
154                .warnings
155                .push(format!("Models you added are hidden: {error}")),
156        }
157        Ok(loaded)
158    }
159
160    /// Only the signed or bundled catalog, without models the user added.
161    pub fn load_published(&self) -> Result<Catalog, CatalogUpdateError> {
162        Ok(self.load_snapshot()?.catalog)
163    }
164
165    fn load_snapshot(&self) -> Result<LoadedCatalog, CatalogUpdateError> {
166        let mut warnings = Vec::new();
167        let catalog = Catalog::parse(rigspark_core::MODELS_JSON)
168            .map_err(|_| CatalogUpdateError::Invalid("bundled catalog"))?;
169        // A downloaded snapshot only wins while it is at least as new as the catalog
170        // compiled into this binary; otherwise upgrading rigspark would stay pinned to it.
171        let current = |snapshot: &VerifiedCatalog| {
172            generated(&snapshot.catalog.generated_at) >= generated(&catalog.generated_at)
173        };
174        match self.read_state() {
175            Ok(Some(state)) => {
176                match self.verify_cached(&state.current) {
177                    Ok(snapshot) if current(&snapshot) => {
178                        return Ok(self.selected(snapshot, "updated", warnings));
179                    }
180                    Ok(_) => {}
181                    Err(_) => {
182                        warnings.push("Active catalog failed verification; using fallback.".into())
183                    }
184                }
185                if let Some(previous) = state.previous {
186                    match self.verify_cached(&previous) {
187                        Ok(snapshot) if current(&snapshot) => {
188                            return Ok(self.selected(snapshot, "previous", warnings));
189                        }
190                        Ok(_) => {}
191                        Err(_) => warnings.push("Previous catalog failed verification.".into()),
192                    }
193                }
194            }
195            Ok(None) => (),
196            Err(_) => warnings
197                .push("Catalog cache is unreadable or invalid; using bundled catalog.".into()),
198        }
199        let status = CatalogStatus {
200            source: "bundled",
201            revision: None,
202            digest: format!(
203                "{:x}",
204                Sha256::digest(rigspark_core::MODELS_JSON.as_bytes())
205            ),
206            generated_at: catalog.generated_at.clone(),
207            published_at: None,
208            model_count: catalog.models.len(),
209            local_count: 0,
210            updates_configured: self.key.is_some(),
211            warnings,
212        };
213        Ok(LoadedCatalog {
214            catalog,
215            raw: rigspark_core::MODELS_JSON.into(),
216            status,
217        })
218    }
219
220    pub fn install(&self, bytes: &[u8]) -> Result<CatalogStatus, CatalogUpdateError> {
221        let snapshot = verify(bytes, &self.key.ok_or(CatalogUpdateError::NotConfigured)?)?;
222        std::fs::create_dir_all(&self.home)?;
223        let directory = Directory::open(&self.home)?;
224        directory.ensure_dir(Path::new("catalog"))?;
225        let cache = directory.subdir(Path::new("catalog"))?;
226        let mut options = cap_std::fs::OpenOptions::new();
227        options
228            .read(true)
229            .write(true)
230            .create(true)
231            .follow(FollowSymlinks::No);
232        #[cfg(unix)]
233        {
234            use cap_std::fs::OpenOptionsExt;
235            options
236                .mode(0o600)
237                .custom_flags(rustix::fs::OFlags::NONBLOCK.bits() as i32);
238        }
239        let lock = cache.open_with("update.lock", &options)?.into_std();
240        if !lock.metadata()?.is_file() {
241            return Err(CatalogUpdateError::Invalid("update lock"));
242        }
243        lock.try_lock().map_err(|_| CatalogUpdateError::Busy)?;
244        let state = self.read_state()?;
245        if let Some(state) = &state
246            && (snapshot.revision < state.highest_revision
247                || (snapshot.revision == state.highest_revision
248                    && snapshot.digest != state.highest_digest))
249        {
250            return Err(CatalogUpdateError::Rollback);
251        }
252        let previous = state.and_then(|state| {
253            if self
254                .verify_cached(&state.current)
255                .is_ok_and(|current| current.revision < snapshot.revision)
256            {
257                Some(state.current)
258            } else {
259                state
260                    .previous
261                    .filter(|previous| self.verify_cached(previous).is_ok())
262            }
263        });
264        let current =
265            serde_json::from_slice(bytes).map_err(|_| CatalogUpdateError::Invalid("envelope"))?;
266        let state = Snapshots {
267            version: 1,
268            highest_revision: snapshot.revision,
269            highest_digest: snapshot.digest.clone(),
270            current,
271            previous,
272        };
273        let encoded = serde_json::to_vec(&state)
274            .map_err(|_| CatalogUpdateError::Invalid("cache encoding"))?;
275        if encoded.len() as u64 > MAX_STATE_BYTES {
276            return Err(CatalogUpdateError::TooLarge);
277        }
278        directory.write(Path::new(STATE), &encoded, false, false)?;
279        Ok(self.selected(snapshot, "updated", Vec::new()).status)
280    }
281}
282
283/// Generation time of a validated catalog; unparsable stamps sort first.
284fn generated(stamp: &str) -> Option<time::OffsetDateTime> {
285    time::OffsetDateTime::parse(stamp, &time::format_description::well_known::Rfc3339).ok()
286}