Skip to main content

scientific_workflow/
registry.rs

1//! Rebuildable run index. Authoritative manifests remain the source of truth.
2use crate::{
3    artifact::{RunManifest, StorageLocation},
4    storage::Store,
5};
6use serde::{Deserialize, Serialize};
7use std::{io, path::Path};
8/// A searchable run summary, with outcome separate from retention and liveness.
9#[derive(Clone, Debug, Serialize, Deserialize)]
10pub struct RunRecord {
11    /// UTC operational identity.
12    pub run_id: String,
13    /// StudyPlan or campaign.
14    pub kind: String,
15    /// Last recorded lifecycle outcome; does not establish current liveness.
16    pub outcome: crate::protocol::Outcome,
17    /// Scientific input fingerprint.
18    pub scientific_identity: String,
19    /// Whether raw artifacts have intentionally been removed.
20    pub sealed: bool,
21    /// Capture closure was committed before execution.
22    pub capture_complete: bool,
23}
24impl From<&RunManifest> for RunRecord {
25    fn from(m: &RunManifest) -> Self {
26        Self {
27            run_id: m.run_id.clone(),
28            kind: m.kind.clone(),
29            outcome: m.outcome,
30            scientific_identity: m.scientific_identity.clone(),
31            sealed: !m.removed.is_empty(),
32            capture_complete: m.capture_complete,
33        }
34    }
35}
36pub(crate) fn index_manifest(
37    store: &Store,
38    manifest: &RunManifest,
39    workspace: &Path,
40) -> io::Result<()> {
41    let mut record = RunRecord::from(manifest);
42    record.sealed = store
43        .json::<crate::seal::SealReport>(&format!("{}/seal.json", manifest.run_id))
44        .is_ok();
45    store.put_json(
46        &format!("registry/{}.json", manifest.run_id),
47        &record,
48        workspace,
49        false,
50    )
51}
52/// Queries the derived index. Use `open_run` to verify selected authoritative data.
53pub fn query(location: &StorageLocation) -> io::Result<Vec<RunRecord>> {
54    let store = Store::open(location)?;
55    let mut out = Vec::new();
56    for key in store.backend.list("registry/")? {
57        out.push(store.json(&key)?);
58    }
59    out.sort_by(|a: &RunRecord, b| a.run_id.cmp(&b.run_id));
60    Ok(out)
61}
62/// Rebuilds the index by reading authoritative run manifests, without executing science.
63pub fn rebuild(location: &StorageLocation, workspace: &Path) -> io::Result<Vec<RunRecord>> {
64    let store = Store::open(location)?;
65    let mut out = Vec::new();
66    for key in store.backend.list("")? {
67        if key.ends_with("/manifest.json") && key.split('/').count() == 2 {
68            let manifest: RunManifest = store.json(&key)?;
69            if manifest.format != "scientific-workflow-run.v1" {
70                continue;
71            }
72            index_manifest(&store, &manifest, workspace)?;
73            out.push(store.json(&format!("registry/{}.json", manifest.run_id))?);
74        }
75    }
76    Ok(out)
77}