1use crate::{
2 acquire::{Acquisition, Artifact, DownloadTransport, SizePolicy, hash_file},
3 adapters::{BackendAdapter, BackendError, ServeRequest, model_id},
4 application::events::{LifecycleObserver, LifecycleScope, LifecycleStage, observe},
5 command::{CommandRunner, OllamaCommandContext},
6 ollama_installed::verify_catalog_manifest,
7};
8use rigspark_core::catalog::{GgufSource, MlxSource};
9use serde::{Deserialize, Serialize};
10use std::{
11 path::{Path, PathBuf},
12 time::Duration,
13};
14use tokio_util::sync::CancellationToken;
15
16pub async fn with_ollama_daemon<T>(
17 adapter: &dyn BackendAdapter,
18 endpoint: &str,
19 cancel: &CancellationToken,
20 operation: impl std::future::Future<Output = Result<T, BackendError>>,
21) -> Result<T, BackendError> {
22 if adapter.name() != "ollama" || cancel.is_cancelled() {
23 return Err(BackendError(
24 "invalid or cancelled acquisition daemon request".into(),
25 ));
26 }
27 let request = ServeRequest {
28 draft_model_path: None,
29 model_id: "llmup-acquisition".into(),
30 endpoint: endpoint.into(),
31 model_path: None,
32 context: None,
33 cache: None,
34 slot_save_path: None,
35 };
36 let handle = adapter.serve(&request, cancel).await?;
37 let result = tokio::select! { biased; _ = cancel.cancelled() => Err(BackendError("pull cancelled".into())), result = operation => result };
38 if handle.owned_by_us {
39 let cleanup = tokio::time::timeout(
40 Duration::from_secs(15),
41 adapter.stop(&handle, &CancellationToken::new()),
42 )
43 .await;
44 let failure = match cleanup {
45 Ok(Ok(())) => None,
46 Ok(Err(error)) => Some(error.0),
47 Err(_) => Some("cleanup timed out".into()),
48 };
49 if let Some(failure) = failure {
50 return Err(BackendError(format!(
51 "{}; acquisition daemon cleanup failed (pid {:?}): {failure}",
52 result
53 .as_ref()
54 .err()
55 .map(|error| error.0.as_str())
56 .unwrap_or("pull completed"),
57 handle.pid
58 )));
59 }
60 }
61 result
62}
63
64#[derive(Clone)]
65pub struct PullRequest {
66 pub backend: String,
67 pub model_id: String,
68 pub expected_bytes: u64,
69 pub expected_sha256: Option<String>,
70 pub projectors: Vec<rigspark_core::sizing::ProjectorArtifact>,
71 pub gguf: Option<GgufSource>,
72 pub mlx: Option<MlxSource>,
73}
74#[derive(Debug, Clone, Serialize)]
75#[serde(rename_all = "camelCase")]
76pub struct PreparedModel {
77 pub model_id: String,
78 pub model_path: Option<PathBuf>,
79 pub digest_verified: bool,
80 pub local_manifest_digest: Option<String>,
81}
82pub struct PullService<'runtime> {
83 pub acquisition: &'runtime Acquisition,
84 pub download: &'runtime dyn DownloadTransport,
85 pub commands: &'runtime dyn CommandRunner,
86 pub ollama_models: &'runtime Path,
87 pub studio_models: &'runtime Path,
88}
89impl PullService<'_> {
90 pub async fn pull(
91 &self,
92 request: &PullRequest,
93 binary: &Path,
94 cancel: &CancellationToken,
95 ) -> Result<PreparedModel, BackendError> {
96 self.pull_at(request, binary, "http://127.0.0.1:11434", cancel)
97 .await
98 }
99 pub async fn pull_at(
100 &self,
101 request: &PullRequest,
102 binary: &Path,
103 endpoint: &str,
104 cancel: &CancellationToken,
105 ) -> Result<PreparedModel, BackendError> {
106 self.pull_at_observed(request, binary, endpoint, cancel, None)
107 .await
108 }
109 pub async fn pull_at_observed(
110 &self,
111 request: &PullRequest,
112 binary: &Path,
113 endpoint: &str,
114 cancel: &CancellationToken,
115 observer: Option<&LifecycleObserver>,
116 ) -> Result<PreparedModel, BackendError> {
117 model_id(&request.model_id)?;
118 let mut projector_digests = std::collections::HashSet::new();
119 let projector_bytes = request
120 .projectors
121 .iter()
122 .try_fold(0_u64, |total, projector| {
123 if projector.bytes == 0
124 || projector.sha256.len() != 64
125 || !projector
126 .sha256
127 .bytes()
128 .all(|byte| byte.is_ascii_hexdigit())
129 || !projector_digests.insert(&projector.sha256)
130 {
131 return None;
132 }
133 total.checked_add(projector.bytes)
134 });
135 if request.projectors.len() > 16
136 || projector_bytes.is_none_or(|total| total >= request.expected_bytes)
137 || (!request.projectors.is_empty() && request.expected_sha256.is_none())
138 {
139 return Err(BackendError("invalid expected projector integrity".into()));
140 }
141 if request.expected_bytes == 0
142 || request.expected_bytes > 9007199254740991
143 || request.expected_sha256.as_ref().is_some_and(|value| {
144 value.len() != 64 || !value.bytes().all(|byte| byte.is_ascii_hexdigit())
145 })
146 {
147 return Err(BackendError("invalid expected model integrity".into()));
148 }
149 if cancel.is_cancelled() {
150 return Err(BackendError("cancelled".into()));
151 }
152 let mut result = PreparedModel {
153 model_id: request.model_id.clone(),
154 model_path: None,
155 digest_verified: false,
156 local_manifest_digest: None,
157 };
158 match request.backend.as_str() {
159 "ollama" => {
160 let context = OllamaCommandContext::new(endpoint, self.ollama_models)?;
161 observe(
162 observer,
163 LifecycleScope::Runtime,
164 LifecycleStage::Acquisition,
165 async {
166 self.commands
167 .run_ollama(
168 binary,
169 &["pull".into(), "--".into(), request.model_id.clone()],
170 &context,
171 cancel,
172 Duration::from_secs(1800),
173 )
174 .await
175 .map_err(BackendError)
176 },
177 )
178 .await?;
179 let digest = observe(
180 observer,
181 LifecycleScope::Runtime,
182 LifecycleStage::Verification,
183 async {
184 let digest = crate::ollama_installed::manifest_digests(
185 self.ollama_models,
186 &request.model_id,
187 )
188 .map_err(|error| BackendError(error.to_string()))?
189 .into_iter()
190 .next()
191 .ok_or_else(|| BackendError("manifest digest unavailable".into()))?;
192 verify_catalog_manifest(
193 self.ollama_models,
194 &request.model_id,
195 &digest,
196 request.expected_sha256.as_deref(),
197 Some(request.expected_bytes),
198 &request.projectors,
199 cancel,
200 )
201 .await
202 .map_err(|error| BackendError(error.to_string()))?;
203 Ok(digest)
204 },
205 )
206 .await?;
207 result.digest_verified = request.expected_sha256.is_some();
208 result.local_manifest_digest = Some(digest);
209 }
210 "llamacpp" | "bitnet" => {
211 let source = request.gguf.as_ref().ok_or_else(|| {
212 BackendError(format!("{} requires a pinned GGUF source", request.backend))
213 })?;
214 let ceiling = request
215 .expected_bytes
216 .checked_add(request.expected_bytes.div_ceil(2))
217 .ok_or_else(|| BackendError("model size overflow".into()))?
218 .max(
219 request
220 .expected_bytes
221 .checked_add(64 * 1024 * 1024)
222 .ok_or_else(|| BackendError("model size overflow".into()))?,
223 );
224 let artifact = Artifact {
225 backend: request.backend.clone(),
226 repo: source.repo.clone(),
227 revision: source.revision.clone(),
228 file: source.file.clone(),
229 sha256: source.sha256.clone(),
230 bytes: ceiling,
231 };
232 let acquired = observe(
233 observer,
234 LifecycleScope::Runtime,
235 LifecycleStage::AcquisitionVerification,
236 async {
237 self.acquisition
238 .acquire_sized(&artifact, SizePolicy::Ceiling, self.download, cancel)
239 .await
240 .map_err(BackendError)
241 },
242 )
243 .await?;
244 result.model_path = Some(acquired.path);
245 result.digest_verified = true;
246 }
247 "mlx" => {
248 let source = request.mlx.as_ref().ok_or_else(|| {
249 BackendError("MLX requires pinned repository manifest".into())
250 })?;
251 let files = repository_artifacts(source, &request.backend, request.expected_bytes)?;
252 result.model_path = Some(
253 observe(
254 observer,
255 LifecycleScope::Runtime,
256 LifecycleStage::AcquisitionVerification,
257 async {
258 self.acquisition
259 .repository(&files, self.download, cancel)
260 .await
261 .map_err(BackendError)
262 },
263 )
264 .await?,
265 );
266 result.digest_verified = true;
267 }
268 "lmstudio" => {
269 let raw = self
270 .commands
271 .run(
272 binary,
273 &[
274 "ls".into(),
275 "--json".into(),
276 "--llm".into(),
277 "--quiet".into(),
278 ],
279 cancel,
280 Duration::from_secs(5),
281 )
282 .await?;
283 let models = studio_models(&raw)?;
284 let mut matches = models.into_iter().filter(|model| {
285 let path = model.path.replace('\\', "/");
286 if let Some(source) = &request.gguf {
287 let suffix = format!("{}/{}", source.repo, source.file);
288 path == suffix || path.ends_with(&format!("/{suffix}"))
289 } else if let Some(source) = &request.mlx {
290 path == source.repo || path.ends_with(&format!("/{}", source.repo))
291 } else {
292 model.model_key.as_deref() == Some(request.model_id.as_str())
293 || path == request.model_id
294 }
295 });
296 let selected = matches
297 .next()
298 .ok_or_else(|| BackendError("model is not downloaded in LM Studio".into()))?;
299 if matches.next().is_some() {
300 return Err(BackendError("ambiguous LM Studio model selection".into()));
301 }
302 let local = studio_path(self.studio_models, &selected.path)?;
303 observe(
304 observer,
305 LifecycleScope::Runtime,
306 LifecycleStage::Verification,
307 async {
308 if let Some(source) = &request.gguf {
309 let (digest, bytes) = hash_file(&local, cancel).await?;
310 if !digest.eq_ignore_ascii_case(&source.sha256)
311 || bytes < request.expected_bytes.div_ceil(2)
312 {
313 return Err(BackendError(
314 "delegated GGUF integrity mismatch".into(),
315 ));
316 }
317 result.digest_verified = true;
318 } else if let Some(source) = &request.mlx {
319 let files =
320 repository_artifacts(source, "mlx", request.expected_bytes)?;
321 for artifact in files {
322 let path = studio_path(&local, &artifact.file)?;
323 let (digest, bytes) = hash_file(&path, cancel).await?;
324 if !digest.eq_ignore_ascii_case(&artifact.sha256)
325 || bytes != artifact.bytes
326 {
327 return Err(BackendError(
328 "delegated MLX integrity mismatch".into(),
329 ));
330 }
331 }
332 crate::special_adapters::validate_mlx_directory(&local)?;
333 result.digest_verified = true;
334 } else {
335 return Err(BackendError(
336 "delegated model needs catalog integrity evidence".into(),
337 ));
338 }
339 Ok(())
340 },
341 )
342 .await?;
343 result.model_path = Some(PathBuf::from(selected.path));
344 }
345 _ => return Err(BackendError("unknown backend".into())),
346 }
347 Ok(result)
348 }
349}
350fn repository_artifacts(
351 source: &MlxSource,
352 backend: &str,
353 expected: u64,
354) -> Result<Vec<Artifact>, BackendError> {
355 let mut total = 0u64;
356 let mut artifacts = Vec::new();
357 for file in &source.files {
358 if !file.bytes.is_finite()
359 || file.bytes.fract() != 0.0
360 || file.bytes <= 0.0
361 || file.bytes > 9007199254740991.0
362 {
363 return Err(BackendError("invalid repository file size".into()));
364 }
365 total = total
366 .checked_add(file.bytes as u64)
367 .ok_or_else(|| BackendError("repository size overflow".into()))?;
368 let artifact = Artifact {
369 backend: backend.into(),
370 repo: source.repo.clone(),
371 revision: source.revision.clone(),
372 file: file.file.clone(),
373 sha256: file.sha256.clone(),
374 bytes: file.bytes as u64,
375 };
376 artifact.validate()?;
377 artifacts.push(artifact);
378 }
379 if total != expected {
380 return Err(BackendError("repository/catalog size mismatch".into()));
381 }
382 Ok(artifacts)
383}
384#[derive(Deserialize)]
385#[serde(rename_all = "camelCase")]
386struct StudioModel {
387 path: String,
388 model_key: Option<String>,
389}
390fn studio_models(raw: &str) -> Result<Vec<StudioModel>, BackendError> {
391 #[derive(Deserialize)]
392 struct Group {
393 model: serde_json::Value,
394 variants: Vec<StudioModel>,
395 }
396 #[derive(Deserialize)]
397 #[serde(untagged)]
398 enum Entry {
399 Model(StudioModel),
400 Group(Group),
401 }
402 let entries: Vec<Entry> = serde_json::from_str(raw)
403 .map_err(|_| BackendError("invalid LM Studio inventory".into()))?;
404 if entries.len() > 10000 {
405 return Err(BackendError("LM Studio inventory exceeds limit".into()));
406 }
407 let mut models = Vec::new();
408 for entry in entries {
409 match entry {
410 Entry::Model(model) => models.push(model),
411 Entry::Group(group) => {
412 let key = group
413 .model
414 .get("modelKey")
415 .and_then(serde_json::Value::as_str);
416 for mut model in group.variants {
417 if model.model_key.is_none() {
418 model.model_key = key.map(str::to_owned);
419 }
420 models.push(model);
421 }
422 }
423 }
424 }
425 if models.len() > 10000
426 || models.iter().any(|model| {
427 model.path.is_empty()
428 || model.path.len() > 4096
429 || model.path.chars().any(char::is_control)
430 || model.model_key.as_ref().is_some_and(|key| {
431 key.is_empty() || key.len() > 1024 || key.chars().any(char::is_control)
432 })
433 })
434 {
435 return Err(BackendError("invalid delegated model path".into()));
436 }
437 Ok(models)
438}
439fn studio_path(root: &Path, value: &str) -> Result<PathBuf, BackendError> {
440 let normalized = value.replace('\\', "/");
441 if normalized.split('/').any(|part| part == "..") {
442 return Err(BackendError("delegated path traversal".into()));
443 }
444 let root = root
445 .canonicalize()
446 .map_err(|_| BackendError("delegated root unavailable".into()))?;
447 let path = Path::new(&normalized);
448 let path = if path.is_absolute() {
449 path.to_owned()
450 } else {
451 root.join(path)
452 };
453 let path = path
454 .canonicalize()
455 .map_err(|_| BackendError("delegated artifact unavailable".into()))?;
456 if !path.starts_with(&root) || path == root {
457 return Err(BackendError("delegated artifact escaped root".into()));
458 }
459 Ok(path)
460}