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