Skip to main content

qualia_client_core/
local_job_scheduler.rs

1//! Bounded local job queue for cold-path work (ontology import, daemon reload, …).
2//!
3//! Admin/UI layer may allocate; workers run one job at a time by default.
4
5use serde::{Deserialize, Serialize};
6use std::collections::{HashMap, HashSet};
7use std::fs;
8use std::path::{Path, PathBuf};
9use std::sync::atomic::{AtomicBool, Ordering};
10use std::sync::{Arc, Mutex, OnceLock};
11use std::time::{SystemTime, UNIX_EPOCH};
12use tokio::sync::Notify;
13use uuid::Uuid;
14
15pub const MAX_JOBS: usize = 64;
16pub const MAX_COMPLETED_HISTORY: usize = 48;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
19#[serde(rename_all = "snake_case")]
20pub enum JobStatus {
21    Queued,
22    Running,
23    Completed,
24    Failed,
25    Cancelled,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
29#[serde(rename_all = "snake_case", tag = "kind")]
30pub enum LocalJobKind {
31    OntologyCatalogImport {
32        ontology_id: String,
33    },
34    OntologyUriImport {
35        uri: String,
36        #[serde(default)]
37        ontology_id: Option<String>,
38        #[serde(default)]
39        domain: Option<String>,
40        #[serde(default)]
41        title: Option<String>,
42    },
43    BundledOntologySeed {
44        #[serde(default)]
45        ontology_id: Option<String>,
46    },
47    WorkbenchDaemonSync,
48    DaemonGraphReload,
49    /// Download a local inference model into the configured model store.
50    ModelDownload {
51        url: String,
52        filename: String,
53        model_id: String,
54    },
55    /// Validate, index, memory-map, and activate a GGUF/P64 model.
56    ModelActivation {
57        model_name: String,
58    },
59    /// Download public reference GLBs and compile them into the local anatomy `.10d` cache.
60    AnatomyAssetAcquire {
61        model: String,
62    },
63    /// Run one agent turn as a background job — the native agent processes it locally, or (when the
64    /// chosen agent's backend is remote-MCP) routes it out over MCP. Curated from chat by the person
65    /// (or, with confirmation, by their agent). Runs off the chat thread; the reply lands in the session.
66    AgentTurn {
67        session_id: String,
68        #[serde(default)]
69        agent_slug: Option<String>,
70        /// Roster revision captured when the job was curated.  Execution
71        /// refuses a changed/missing definition instead of silently adopting
72        /// new model, sharing, or remote-placement policy.
73        #[serde(default)]
74        agent_updated_at_unix: Option<u64>,
75        prompt: String,
76        /// Opaque serialized context (the agent's blackboard view + semantic skills context).
77        #[serde(default)]
78        context_snapshot: Vec<u8>,
79    },
80}
81
82impl LocalJobKind {
83    /// `true` when ontology-work.
84    pub fn is_ontology_work(&self) -> bool {
85        matches!(
86            self,
87            LocalJobKind::OntologyCatalogImport { .. }
88                | LocalJobKind::OntologyUriImport { .. }
89                | LocalJobKind::BundledOntologySeed { .. }
90                | LocalJobKind::WorkbenchDaemonSync
91                | LocalJobKind::DaemonGraphReload
92        )
93    }
94}
95
96#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct LocalJob {
98    pub id: String,
99    pub kind: LocalJobKind,
100    pub status: JobStatus,
101    pub created_at: u64,
102    pub started_at: Option<u64>,
103    pub finished_at: Option<u64>,
104    pub progress: f64,
105    pub message: String,
106    #[serde(default)]
107    pub result: Option<serde_json::Value>,
108    #[serde(default)]
109    pub error: Option<String>,
110    /// Apparatus that should run the work (`did:q42:device:…`). Empty/None → this install.
111    #[serde(default)]
112    pub target_device_id: Option<String>,
113    /// Apparatus that enqueued the work (local device when known).
114    #[serde(default)]
115    pub originating_device_id: Option<String>,
116    /// Person principal who owns the work (not the OS account).
117    #[serde(default)]
118    pub person_id: Option<String>,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
122pub struct EnqueueJobRequest {
123    #[serde(flatten)]
124    pub kind: LocalJobKind,
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct JobQueueSnapshot {
129    pub jobs: Vec<LocalJob>,
130    pub queued: usize,
131    pub running: usize,
132    pub completed: usize,
133    pub failed: usize,
134}
135
136struct SchedulerInner {
137    jobs: Mutex<Vec<LocalJob>>,
138    cancel: Mutex<HashMap<String, Arc<AtomicBool>>>,
139    notify: Notify,
140    worker_started: AtomicBool,
141    store_path: Mutex<PathBuf>,
142}
143
144#[derive(Clone)]
145pub struct LocalJobScheduler {
146    inner: Arc<SchedulerInner>,
147}
148
149static GLOBAL: OnceLock<Arc<LocalJobScheduler>> = OnceLock::new();
150
151fn now_unix() -> u64 {
152    SystemTime::now()
153        .duration_since(UNIX_EPOCH)
154        .map(|d| d.as_secs())
155        .unwrap_or(0)
156}
157
158fn jobs_store_path() -> PathBuf {
159    crate::state::app_meta_dir().join("local-jobs.json")
160}
161
162impl LocalJobScheduler {
163    /// Creates a new instance.
164    pub fn new() -> Self {
165        let path = jobs_store_path();
166        let mut jobs = load_jobs(&path);
167        // Recover interrupted runs after restart.
168        for job in jobs.iter_mut() {
169            if job.status == JobStatus::Running {
170                job.status = JobStatus::Queued;
171                job.message = "Re-queued after restart".to_string();
172                job.started_at = None;
173            }
174        }
175        Self {
176            inner: Arc::new(SchedulerInner {
177                jobs: Mutex::new(jobs),
178                cancel: Mutex::new(HashMap::new()),
179                notify: Notify::new(),
180                worker_started: AtomicBool::new(false),
181                store_path: Mutex::new(path),
182            }),
183        }
184    }
185
186    /// Global singleton.
187    pub fn global() -> Arc<Self> {
188        GLOBAL.get_or_init(|| Arc::new(Self::new())).clone()
189    }
190
191    /// Spawn the background worker that processes queued jobs. The worker must run on a Tokio runtime;
192    /// pass a `Handle` when calling from outside a runtime context (e.g. from a Tauri `setup` callback,
193    /// which runs synchronously and is NOT inside `tokio::spawn`'s implicit runtime). When called from
194    /// within a runtime context, `None` falls back to `tokio::spawn`.
195    pub fn spawn_global_worker_with_runtime(runtime: tokio::runtime::Handle) {
196        Self::spawn_global_worker(Some(runtime));
197    }
198
199    /// Spawn the background worker. Uses `tokio::spawn` — only call this from within a Tokio runtime
200    /// context. For callers outside a runtime (e.g. Tauri's `setup` hook), use
201    /// `spawn_global_worker_with_runtime()` with an explicit handle.
202    pub fn spawn_global_worker(runtime: Option<tokio::runtime::Handle>) {
203        let scheduler = Self::global();
204        if scheduler
205            .inner
206            .worker_started
207            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
208            .is_err()
209        {
210            return;
211        }
212        let worker = scheduler.clone();
213        let task = async move {
214            worker.worker_loop().await;
215        };
216        match runtime {
217            Some(handle) => {
218                handle.spawn(task);
219            }
220            None => {
221                tokio::spawn(task);
222            }
223        }
224        // Kick the loop so jobs queued before first notify are processed.
225        scheduler.inner.notify.notify_one();
226    }
227
228    /// Enqueues a job.
229    pub fn enqueue(&self, kind: LocalJobKind) -> Result<LocalJob, String> {
230        self.enqueue_for_device(kind, None)
231    }
232
233    /// Patch provenance fields on an existing job (e.g. after fleet accept).
234    pub fn update_job_meta(&self, updated: &LocalJob) -> Result<(), String> {
235        let mut jobs = self.inner.jobs.lock().map_err(|e| e.to_string())?;
236        if let Some(slot) = jobs.iter_mut().find(|j| j.id == updated.id) {
237            slot.originating_device_id = updated.originating_device_id.clone();
238            slot.person_id = updated.person_id.clone();
239            slot.message = updated.message.clone();
240            slot.target_device_id = updated.target_device_id.clone();
241            self.persist(&jobs)?;
242        }
243        Ok(())
244    }
245
246    /// Enqueue work for a specific apparatus. `None` / local device_id runs here.
247    /// Remote registered devices are delivered via the fleet job path (HTTP + outbox).
248    pub fn enqueue_for_device(
249        &self,
250        kind: LocalJobKind,
251        target_device_id: Option<String>,
252    ) -> Result<LocalJob, String> {
253        let plane = crate::identity_plane::ensure_local_apparatus(None).ok();
254        let local_id = plane.as_ref().map(|p| p.local_device_id.clone());
255        let person_id = plane.as_ref().map(|p| p.person.person_id.clone());
256        let placement = crate::identity_plane::resolve_job_placement(target_device_id.as_deref())?;
257        match &placement {
258            crate::identity_plane::JobPlacement::Local { .. } => {}
259            crate::identity_plane::JobPlacement::RemoteRegistered { device_id, .. } => {
260                let entry =
261                    crate::identity_plane::deliver_or_queue_remote_job(kind.clone(), device_id)?;
262                // Represent remote work as a completed/queued audit job on the origin.
263                let job = LocalJob {
264                    id: entry.id.clone(),
265                    kind,
266                    status: if entry.delivered {
267                        JobStatus::Completed
268                    } else {
269                        JobStatus::Queued
270                    },
271                    created_at: entry.created_at_unix,
272                    started_at: Some(entry.last_attempt_unix),
273                    finished_at: if entry.delivered {
274                        Some(entry.last_attempt_unix)
275                    } else {
276                        None
277                    },
278                    progress: if entry.delivered { 1.0 } else { 0.0 },
279                    message: if entry.delivered {
280                        format!("Delivered to remote apparatus {device_id}")
281                    } else {
282                        format!(
283                            "Queued for remote delivery to {device_id}: {}",
284                            entry.last_error.clone().unwrap_or_default()
285                        )
286                    },
287                    result: serde_json::to_value(&entry).ok(),
288                    error: entry.last_error.clone(),
289                    target_device_id: Some(device_id.clone()),
290                    originating_device_id: local_id,
291                    person_id,
292                };
293                let mut jobs = self.inner.jobs.lock().map_err(|e| e.to_string())?;
294                jobs.push(job.clone());
295                self.persist(&jobs)?;
296                return Ok(job);
297            }
298            crate::identity_plane::JobPlacement::Unknown { device_id } => {
299                return Err(format!(
300                    "Unknown target device_id {device_id}. Register it in the device fleet or leave target empty for this install."
301                ));
302            }
303        }
304
305        let job = LocalJob {
306            id: Uuid::new_v4().to_string(),
307            kind,
308            status: JobStatus::Queued,
309            created_at: now_unix(),
310            started_at: None,
311            finished_at: None,
312            progress: 0.0,
313            message: "Queued".to_string(),
314            result: None,
315            error: None,
316            target_device_id: local_id.clone(),
317            originating_device_id: local_id,
318            person_id,
319        };
320        {
321            let mut jobs = self.inner.jobs.lock().map_err(|e| e.to_string())?;
322            if jobs.iter().any(|existing| {
323                matches!(existing.status, JobStatus::Queued | JobStatus::Running)
324                    && existing.kind == job.kind
325            }) {
326                return Err("A matching job is already queued or running".to_string());
327            }
328            if jobs.len() >= MAX_JOBS {
329                self.prune_completed(&mut jobs);
330            }
331            if jobs.len() >= MAX_JOBS {
332                return Err(format!("Job queue full (max {MAX_JOBS})"));
333            }
334            jobs.push(job.clone());
335            self.inner
336                .cancel
337                .lock()
338                .map_err(|e| e.to_string())?
339                .insert(job.id.clone(), Arc::new(AtomicBool::new(false)));
340            self.persist(&jobs)?;
341        }
342        self.inner.notify.notify_one();
343        Ok(job)
344    }
345
346    /// State snapshot.
347    pub fn snapshot(&self) -> Result<JobQueueSnapshot, String> {
348        let jobs = self.inner.jobs.lock().map_err(|e| e.to_string())?;
349        let mut queued = 0usize;
350        let mut running = 0usize;
351        let mut completed = 0usize;
352        let mut failed = 0usize;
353        for job in jobs.iter() {
354            match job.status {
355                JobStatus::Queued => queued += 1,
356                JobStatus::Running => running += 1,
357                JobStatus::Completed => completed += 1,
358                JobStatus::Failed | JobStatus::Cancelled => failed += 1,
359            }
360        }
361        Ok(JobQueueSnapshot {
362            jobs: jobs.clone(),
363            queued,
364            running,
365            completed,
366            failed,
367        })
368    }
369
370    /// Gets.
371    pub fn get(&self, id: &str) -> Result<Option<LocalJob>, String> {
372        let jobs = self.inner.jobs.lock().map_err(|e| e.to_string())?;
373        Ok(jobs.iter().find(|j| j.id == id).cloned())
374    }
375
376    /// Cancels.
377    pub fn cancel(&self, id: &str) -> Result<bool, String> {
378        let mut jobs = self.inner.jobs.lock().map_err(|e| e.to_string())?;
379        let Some(job) = jobs.iter_mut().find(|j| j.id == id) else {
380            return Ok(false);
381        };
382        let download_id = match &job.kind {
383            LocalJobKind::ModelDownload { model_id, .. } => Some(model_id.clone()),
384            _ => None,
385        };
386        if job.status == JobStatus::Queued {
387            job.status = JobStatus::Cancelled;
388            job.finished_at = Some(now_unix());
389            job.message = "Cancelled before start".to_string();
390            self.persist(&jobs)?;
391            return Ok(true);
392        }
393        if job.status == JobStatus::Running {
394            if matches!(job.kind, LocalJobKind::ModelActivation { .. }) {
395                return Err(
396                    "Model activation cannot be interrupted safely after memory mapping starts"
397                        .to_string(),
398                );
399            }
400            if let Some(flag) = self.inner.cancel.lock().map_err(|e| e.to_string())?.get(id) {
401                flag.store(true, Ordering::Relaxed);
402            }
403            job.message = "Cancel requested".to_string();
404            self.persist(&jobs)?;
405            drop(jobs);
406            if let Some(download_id) = download_id {
407                let _ = crate::api::cancel_download(download_id);
408            }
409            return Ok(true);
410        }
411        Ok(false)
412    }
413
414    /// Re-enqueue a completed, failed, or cancelled job with the same bounded inputs.
415    pub fn retry(&self, id: &str) -> Result<LocalJob, String> {
416        let kind = {
417            let jobs = self.inner.jobs.lock().map_err(|e| e.to_string())?;
418            let job = jobs
419                .iter()
420                .find(|job| job.id == id)
421                .ok_or_else(|| "job not found".to_string())?;
422            if matches!(job.status, JobStatus::Queued | JobStatus::Running) {
423                return Err("job is still active".to_string());
424            }
425            job.kind.clone()
426        };
427        self.enqueue(kind)
428    }
429
430    /// Remove finished history while preserving queued and running work.
431    pub fn clear_finished(&self) -> Result<usize, String> {
432        let mut jobs = self.inner.jobs.lock().map_err(|e| e.to_string())?;
433        let before = jobs.len();
434        let removed_ids: Vec<String> = jobs
435            .iter()
436            .filter(|job| {
437                matches!(
438                    job.status,
439                    JobStatus::Completed | JobStatus::Failed | JobStatus::Cancelled
440                )
441            })
442            .map(|job| job.id.clone())
443            .collect();
444        jobs.retain(|job| matches!(job.status, JobStatus::Queued | JobStatus::Running));
445        if let Ok(mut cancel) = self.inner.cancel.lock() {
446            for id in removed_ids {
447                cancel.remove(&id);
448            }
449        }
450        self.persist(&jobs)?;
451        Ok(before.saturating_sub(jobs.len()))
452    }
453
454    fn prune_completed(&self, jobs: &mut Vec<LocalJob>) {
455        let mut done: Vec<(u64, String)> = jobs
456            .iter()
457            .filter_map(|j| {
458                if matches!(
459                    j.status,
460                    JobStatus::Completed | JobStatus::Failed | JobStatus::Cancelled
461                ) {
462                    Some((j.finished_at.unwrap_or(j.created_at), j.id.clone()))
463                } else {
464                    None
465                }
466            })
467            .collect();
468        if done.len() <= MAX_COMPLETED_HISTORY {
469            return;
470        }
471        done.sort_by_key(|(t, _)| *t);
472        let drop_n = done.len() - MAX_COMPLETED_HISTORY;
473        let drop_ids: HashSet<String> = done.into_iter().take(drop_n).map(|(_, id)| id).collect();
474        jobs.retain(|j| !drop_ids.contains(&j.id));
475        if let Ok(mut cancel) = self.inner.cancel.lock() {
476            for id in drop_ids {
477                cancel.remove(&id);
478            }
479        }
480    }
481
482    fn persist(&self, jobs: &[LocalJob]) -> Result<(), String> {
483        let path = self
484            .inner
485            .store_path
486            .lock()
487            .map_err(|e| e.to_string())?
488            .clone();
489        if let Some(parent) = path.parent() {
490            fs::create_dir_all(parent).map_err(|e| e.to_string())?;
491        }
492        let json = serde_json::to_string_pretty(jobs).map_err(|e| e.to_string())?;
493        fs::write(path, json).map_err(|e| e.to_string())
494    }
495
496    fn update_job<F>(&self, id: &str, f: F) -> Result<(), String>
497    where
498        F: FnOnce(&mut LocalJob),
499    {
500        let mut jobs = self.inner.jobs.lock().map_err(|e| e.to_string())?;
501        let Some(job) = jobs.iter_mut().find(|j| j.id == id) else {
502            return Ok(());
503        };
504        f(job);
505        self.persist(&jobs)
506    }
507
508    /// Update visible progress for a running job. Progress is clamped and persisted so recovery and
509    /// agent diagnostics see the same state as the UI.
510    pub fn report_progress(
511        &self,
512        id: &str,
513        progress: f64,
514        message: impl Into<String>,
515    ) -> Result<(), String> {
516        let progress = progress.clamp(0.0, 0.99);
517        let message = message.into();
518        self.update_job(id, |job| {
519            if job.status == JobStatus::Running {
520                job.progress = progress;
521                job.message = message;
522            }
523        })
524    }
525
526    fn cancel_flag(&self, id: &str) -> Option<Arc<AtomicBool>> {
527        self.inner.cancel.lock().ok()?.get(id).cloned()
528    }
529
530    async fn worker_loop(&self) {
531        loop {
532            self.inner.notify.notified().await;
533            loop {
534                let next_id = {
535                    let jobs = match self.inner.jobs.lock() {
536                        Ok(j) => j,
537                        Err(_) => break,
538                    };
539                    jobs.iter()
540                        .find(|j| j.status == JobStatus::Queued)
541                        .map(|j| j.id.clone())
542                };
543                let Some(job_id) = next_id else { break };
544                if let Err(err) = self.run_one(job_id).await {
545                    eprintln!("[local_job_scheduler] worker error: {err}");
546                }
547            }
548        }
549    }
550
551    async fn run_one(&self, job_id: String) -> Result<(), String> {
552        let kind = {
553            let jobs = self.inner.jobs.lock().map_err(|e| e.to_string())?;
554            jobs.iter()
555                .find(|j| j.id == job_id)
556                .map(|j| j.kind.clone())
557                .ok_or_else(|| "job not found".to_string())?
558        };
559
560        self.update_job(&job_id, |job| {
561            job.status = JobStatus::Running;
562            job.started_at = Some(now_unix());
563            job.progress = 0.05;
564            job.message = "Running".to_string();
565        })?;
566
567        if kind.is_ontology_work() {
568            crate::activity_signals::begin_ontology_job();
569        }
570
571        log::info!("JOB|{}|running|{:?}", job_id, kind);
572        let outcome = execute_job(self, &job_id, &kind, self.cancel_flag(&job_id)).await;
573
574        if kind.is_ontology_work() {
575            crate::activity_signals::end_ontology_job();
576        }
577
578        match outcome {
579            Ok(result) => {
580                self.update_job(&job_id, |job| {
581                    job.status = JobStatus::Completed;
582                    job.finished_at = Some(now_unix());
583                    job.progress = 1.0;
584                    job.message = "Completed".to_string();
585                    job.result = Some(result);
586                    job.error = None;
587                })?;
588                log::info!("JOB|{}|completed|Completed", job_id);
589            }
590            Err(err) => {
591                let cancelled = err == "cancelled";
592                if cancelled {
593                    log::warn!("JOB|{}|cancelled|Cancelled", job_id);
594                } else {
595                    log::error!("JOB|{}|failed|{}", job_id, err);
596                }
597                self.update_job(&job_id, |job| {
598                    job.status = if cancelled {
599                        JobStatus::Cancelled
600                    } else {
601                        JobStatus::Failed
602                    };
603                    job.finished_at = Some(now_unix());
604                    job.progress = 1.0;
605                    job.message = if cancelled {
606                        "Cancelled".to_string()
607                    } else {
608                        "Failed".to_string()
609                    };
610                    job.error = Some(err);
611                })?;
612            }
613        }
614        Ok(())
615    }
616}
617
618fn load_jobs(path: &Path) -> Vec<LocalJob> {
619    let Ok(text) = fs::read_to_string(path) else {
620        return Vec::new();
621    };
622    serde_json::from_str(&text).unwrap_or_default()
623}
624
625fn check_cancel(flag: &Option<Arc<AtomicBool>>) -> Result<(), String> {
626    if flag.as_ref().is_some_and(|f| f.load(Ordering::Relaxed)) {
627        Err("cancelled".to_string())
628    } else {
629        Ok(())
630    }
631}
632
633async fn execute_job(
634    scheduler: &LocalJobScheduler,
635    job_id: &str,
636    kind: &LocalJobKind,
637    cancel: Option<Arc<AtomicBool>>,
638) -> Result<serde_json::Value, String> {
639    let state = crate::state::APP_STATE
640        .get()
641        .ok_or("APP_STATE not initialized")?;
642    let storage_path = state
643        .config
644        .lock()
645        .map_err(|e| e.to_string())?
646        .storage_path
647        .clone();
648
649    match kind {
650        LocalJobKind::OntologyCatalogImport { ontology_id } => {
651            check_cancel(&cancel)?;
652            let catalog = crate::api::load_workspace_catalog();
653            let progress = build_import_progress(job_id, state, cancel.clone());
654            let result = crate::resource_import::import_catalog_ontology_with_options(
655                &catalog,
656                ontology_id,
657                Path::new(&storage_path),
658                Some(&progress),
659                true,
660            )
661            .await
662            .map_err(|e| e.to_string())?;
663            check_cancel(&cancel)?;
664            qualia_core_db::daemon_graph::init_daemon_graph(&storage_path);
665            serde_json::to_value(result).map_err(|e| e.to_string())
666        }
667        LocalJobKind::OntologyUriImport {
668            uri,
669            ontology_id,
670            domain,
671            title,
672        } => {
673            check_cancel(&cancel)?;
674            let result = crate::ontology_workbench::import_from_uri(
675                Path::new(&storage_path),
676                uri.clone(),
677                ontology_id.clone(),
678                domain.clone(),
679                title.clone(),
680            )
681            .await?;
682            check_cancel(&cancel)?;
683            serde_json::to_value(result).map_err(|e| e.to_string())
684        }
685        LocalJobKind::BundledOntologySeed { ontology_id } => {
686            check_cancel(&cancel)?;
687            if let Some(id) = ontology_id {
688                let seeded = crate::bundled_ontologies::resolve_bundled_ontology_source(id)
689                    .ok_or_else(|| format!("Bundled source missing for {id}"))?;
690                let catalog = crate::api::load_workspace_catalog();
691                let ont = catalog.find_ontology(id);
692                crate::resource_import::ingest_local_rdf(
693                    &seeded,
694                    id,
695                    Path::new(&storage_path),
696                    ont,
697                )
698                .map_err(|e| e.to_string())?;
699                Ok(serde_json::json!({ "seeded": [id] }))
700            } else {
701                let seeded = crate::bundled_ontologies::seed_bundled_ontologies()?;
702                Ok(serde_json::json!({ "seeded": seeded }))
703            }
704        }
705        LocalJobKind::WorkbenchDaemonSync => {
706            check_cancel(&cancel)?;
707            crate::ontology_workbench::sync_workbench_seeds_to_daemon(Path::new(&storage_path))
708        }
709        LocalJobKind::DaemonGraphReload => {
710            check_cancel(&cancel)?;
711            qualia_core_db::daemon_graph::init_daemon_graph(&storage_path);
712            #[cfg(not(target_arch = "wasm32"))]
713            qualia_core_db::ontology_loader::load_startup_ontologies();
714            Ok(serde_json::json!({
715                "storage_path": storage_path,
716                "daemon_graph": "reloaded"
717            }))
718        }
719        LocalJobKind::ModelDownload {
720            url,
721            filename,
722            model_id,
723        } => {
724            check_cancel(&cancel)?;
725            scheduler.report_progress(
726                job_id,
727                0.02,
728                format!("Connecting to download source for {filename}"),
729            )?;
730            let mut download = Box::pin(crate::api::download_model(
731                url.clone(),
732                filename.clone(),
733                model_id.clone(),
734            ));
735            loop {
736                tokio::select! {
737                    result = &mut download => {
738                        let path = match result {
739                            Ok(path) => path,
740                            Err(_) if cancel
741                                .as_ref()
742                                .is_some_and(|flag| flag.load(Ordering::Relaxed)) =>
743                            {
744                                return Err("cancelled".to_string());
745                            }
746                            Err(error) => return Err(error),
747                        };
748                        check_cancel(&cancel)?;
749                        // Download alone left models as raw files with no install
750                        // manifest, so activation failed with "No install manifest"
751                        // and chat stayed on "no active model". Path-based
752                        // set_active_model finalizes (manifest + index) and activates.
753                        scheduler.report_progress(
754                            job_id,
755                            0.95,
756                            format!("Download complete — indexing and activating {filename}"),
757                        )?;
758                        let path_for_activate = path.clone();
759                        let model_id_for_activate = model_id.clone();
760                        let activate_result = tokio::task::spawn_blocking(move || {
761                            crate::api::set_active_model(path_for_activate.clone()).map_err(|e| {
762                                format!(
763                                    "download ok but activate failed for {model_id_for_activate}: {e}"
764                                )
765                            })?;
766                            Ok::<_, String>(
767                                crate::api::get_active_model().unwrap_or(path_for_activate),
768                            )
769                        })
770                        .await
771                        .map_err(|e| format!("post-download activation task failed: {e}"))?;
772                        let active_path = activate_result?;
773                        check_cancel(&cancel)?;
774                        return Ok(serde_json::json!({
775                            "model_id": model_id,
776                            "filename": filename,
777                            "path": path,
778                            "active": active_path,
779                            "lifecycle": "Active",
780                        }));
781                    }
782                    _ = tokio::time::sleep(std::time::Duration::from_millis(350)) => {
783                        if check_cancel(&cancel).is_err() {
784                            let _ = crate::api::cancel_download(model_id.clone());
785                        }
786                        if let Some(payload) = state
787                            .active_downloads
788                            .lock()
789                            .ok()
790                            .and_then(|downloads| downloads.get(model_id).cloned())
791                        {
792                            let fraction = (payload.progress / 100.0).clamp(0.02, 0.94);
793                            let size = if payload.total_bytes > 0 {
794                                format!(
795                                    "{:.1}/{:.1} MB",
796                                    payload.downloaded_bytes as f64 / 1_000_000.0,
797                                    payload.total_bytes as f64 / 1_000_000.0
798                                )
799                            } else {
800                                format!("{:.1} MB", payload.downloaded_bytes as f64 / 1_000_000.0)
801                            };
802                            scheduler.report_progress(
803                                job_id,
804                                fraction,
805                                format!("Downloading {filename} · {size} · {:.0} KB/s", payload.speed_kbps),
806                            )?;
807                        }
808                    }
809                }
810            }
811        }
812        LocalJobKind::ModelActivation { model_name } => {
813            check_cancel(&cancel)?;
814            scheduler.report_progress(
815                job_id,
816                0.08,
817                format!("Validating and indexing {model_name}"),
818            )?;
819            let selected = model_name.clone();
820            let scheduler_for_task = scheduler.clone();
821            let job_for_task = job_id.to_string();
822            let result = tokio::task::spawn_blocking(move || {
823                scheduler_for_task.report_progress(
824                    &job_for_task,
825                    0.32,
826                    "Mapping model into memory and initialising the inference backend",
827                )?;
828                crate::api::set_active_model(selected)?;
829                scheduler_for_task.report_progress(
830                    &job_for_task,
831                    0.94,
832                    "Model mapped; completing activation record",
833                )?;
834                Ok::<_, String>(())
835            })
836            .await
837            .map_err(|error| format!("model activation task failed: {error}"))?;
838            result?;
839            check_cancel(&cancel)?;
840            Ok(serde_json::json!({
841                "model": model_name,
842                "active": crate::api::get_active_model(),
843            }))
844        }
845        LocalJobKind::AnatomyAssetAcquire { model } => {
846            check_cancel(&cancel)?;
847            let parsed = crate::wellfair::api::parse_anatomy_model(model)?;
848            let scheduler_for_task = scheduler.clone();
849            let job_for_task = job_id.to_string();
850            let storage_for_task = storage_path.clone();
851            let cancel_for_task = cancel.clone();
852            let report = tokio::task::spawn_blocking(move || {
853                qualia_client_core_anatomy_acquire(
854                    &scheduler_for_task,
855                    &job_for_task,
856                    &storage_for_task,
857                    parsed,
858                    cancel_for_task,
859                )
860            })
861            .await
862            .map_err(|error| format!("anatomy acquisition task failed: {error}"))??;
863            check_cancel(&cancel)?;
864            serde_json::to_value(report).map_err(|error| error.to_string())
865        }
866        LocalJobKind::AgentTurn {
867            session_id,
868            agent_slug,
869            agent_updated_at_unix,
870            prompt,
871            context_snapshot: _,
872        } => {
873            check_cancel(&cancel)?;
874            if let Some(slug) = agent_slug.as_deref() {
875                let storage = crate::state::APP_STATE
876                    .get()
877                    .ok_or("Application not initialized")?
878                    .config
879                    .lock()
880                    .map_err(|error| error.to_string())?
881                    .storage_path
882                    .clone();
883                let current =
884                    crate::agent_registry::get_agent(std::path::Path::new(&storage), slug)
885                        .ok_or_else(|| format!("scheduled agent @{slug} no longer exists"))?;
886                if !current.enabled {
887                    return Err(format!("scheduled agent @{slug} is disabled"));
888                }
889                if let Some(expected_revision) = agent_updated_at_unix {
890                    if current.updated_at_unix != *expected_revision {
891                        return Err(format!(
892                            "scheduled agent @{slug} changed after this job was queued; review and schedule it again"
893                        ));
894                    }
895                }
896            }
897            // Route by the chosen agent's backend (local-first). A remote-MCP agent runs its turn out
898            // over MCP (native only); otherwise the native engine runs it and appends the reply.
899            #[cfg(not(target_arch = "wasm32"))]
900            {
901                let backend = crate::api::agent_backend_kind(agent_slug.clone())
902                    .unwrap_or_else(|_| "local".to_string());
903                if backend == "remote" {
904                    let slug = agent_slug.clone().unwrap_or_default();
905                    return crate::api::run_remote_agent_turn(
906                        session_id.clone(),
907                        slug,
908                        prompt.clone(),
909                        false,
910                    );
911                }
912            }
913            let result = crate::chat_inference::run_chat_inference_for_agent(
914                session_id,
915                prompt,
916                agent_slug.as_deref(),
917                None,
918            );
919            if result.committed && !result.text.trim().is_empty() {
920                let _ = crate::api::append_chat_message(
921                    session_id.clone(),
922                    "agent".to_string(),
923                    result.text.clone(),
924                );
925            }
926            serde_json::to_value(&result).map_err(|e| e.to_string())
927        }
928    }
929}
930
931fn qualia_client_core_anatomy_acquire(
932    scheduler: &LocalJobScheduler,
933    job_id: &str,
934    storage_path: &str,
935    model: wellfare_core::anatomy::AnatomyModel,
936    cancel: Option<Arc<AtomicBool>>,
937) -> Result<crate::wellfair::anatomy_assets::AcquireReport, String> {
938    crate::wellfair::anatomy_assets::acquire_body_assets_controlled(
939        storage_path,
940        model,
941        |progress| {
942            let fraction = match progress.stage.as_str() {
943                "discover" => 0.05,
944                "fetch" => 0.10 + 0.58 * (progress.done as f64 / progress.total.max(1) as f64),
945                "compile" => 0.70 + 0.25 * (progress.done as f64 / progress.total.max(1) as f64),
946                "done" => 0.98,
947                _ => 0.05,
948            };
949            let _ = scheduler.report_progress(job_id, fraction, progress.message);
950        },
951        || {
952            cancel
953                .as_ref()
954                .is_some_and(|flag| flag.load(Ordering::Relaxed))
955        },
956    )
957}
958
959fn build_import_progress(
960    job_id: &str,
961    state: &crate::state::AppState,
962    cancel: Option<Arc<AtomicBool>>,
963) -> crate::resource_import::ImportProgressCtx {
964    let flag = cancel.unwrap_or_else(|| Arc::new(AtomicBool::new(false)));
965    if let Ok(mut handles) = state.download_handles.lock() {
966        handles.insert(job_id.to_string(), flag);
967    }
968    crate::resource_import::ImportProgressCtx {
969        id: job_id.to_string(),
970        handles: state.download_handles.clone(),
971        active_downloads: state.active_downloads.clone(),
972        download_events: state.download_events.clone(),
973    }
974}
975
976#[cfg(test)]
977mod tests {
978    use super::*;
979
980    #[test]
981    fn job_kind_deserializes_catalog_import() {
982        let raw = r#"{"kind":"ontology_catalog_import","ontology_id":"shacl"}"#;
983        let req: EnqueueJobRequest = serde_json::from_str(raw).unwrap();
984        assert!(matches!(
985            req.kind,
986            LocalJobKind::OntologyCatalogImport { .. }
987        ));
988    }
989
990    #[test]
991    fn job_kind_deserializes_model_activation() {
992        let raw = r#"{"kind":"model_activation","model_name":"C:\\Models\\local.gguf"}"#;
993        let req: EnqueueJobRequest = serde_json::from_str(raw).unwrap();
994        assert!(matches!(
995            req.kind,
996            LocalJobKind::ModelActivation { model_name }
997                if model_name.ends_with("local.gguf")
998        ));
999    }
1000
1001    #[test]
1002    fn job_kind_deserializes_anatomy_acquisition() {
1003        let raw = r#"{"kind":"anatomy_asset_acquire","model":"female"}"#;
1004        let req: EnqueueJobRequest = serde_json::from_str(raw).unwrap();
1005        assert!(matches!(
1006            req.kind,
1007            LocalJobKind::AnatomyAssetAcquire { model } if model == "female"
1008        ));
1009    }
1010
1011    #[test]
1012    fn agent_turn_snapshot_is_backward_compatible() {
1013        let raw = r#"{"kind":"agent_turn","session_id":"chat-1","agent_slug":"researcher","prompt":"summarise"}"#;
1014        let req: EnqueueJobRequest = serde_json::from_str(raw).unwrap();
1015        assert!(matches!(
1016            req.kind,
1017            LocalJobKind::AgentTurn {
1018                agent_slug: Some(ref slug),
1019                agent_updated_at_unix: None,
1020                ref context_snapshot,
1021                ..
1022            } if slug == "researcher" && context_snapshot.is_empty()
1023        ));
1024    }
1025
1026    #[test]
1027    fn agent_turn_with_context_snapshot_roundtrips() {
1028        let snapshot_data = vec![0xDE, 0xAD, 0xBE, 0xEF, 0x01, 0x02];
1029        let kind = LocalJobKind::AgentTurn {
1030            session_id: "session-42".to_string(),
1031            agent_slug: Some("researcher".to_string()),
1032            agent_updated_at_unix: Some(1724000000),
1033            prompt: "Synthesize the report".to_string(),
1034            context_snapshot: snapshot_data.clone(),
1035        };
1036        let req = EnqueueJobRequest { kind: kind.clone() };
1037        let json = serde_json::to_string(&req).unwrap();
1038        let parsed: EnqueueJobRequest = serde_json::from_str(&json).unwrap();
1039        assert_eq!(parsed.kind, kind);
1040
1041        let scheduler = LocalJobScheduler::new();
1042        let job = scheduler.enqueue(kind).unwrap();
1043        assert_eq!(job.status, JobStatus::Queued);
1044        assert!(matches!(
1045            job.kind,
1046            LocalJobKind::AgentTurn {
1047                ref context_snapshot,
1048                ..
1049            } if context_snapshot == &snapshot_data
1050        ));
1051    }
1052}