scientific-workflow 0.17.0

Configuration-driven scientific tasks, typed state, and durable recordings
Documentation
//! Restore task commit evidence and bounded local views from an authoritative run.
use crate::{
    artifact::RunArchive,
    config::{ReuseSource, RuntimeSettings},
    storage::{Store, atomic_json},
};
use std::{
    io::{self, Write},
    path::{Path, PathBuf},
};
pub(crate) fn restore_reuse(
    settings: &RuntimeSettings,
    workspace: &Path,
) -> io::Result<Option<PathBuf>> {
    let Some(ReuseSource::Run {
        run_id,
        execution,
        storage,
    }) = &settings.reuse_from
    else {
        return Ok(None);
    };
    let location = storage.as_ref().unwrap_or(&settings.storage);
    let archive = RunArchive::open(location, run_id)?;
    let store = Store::open(location)?;
    let destination = Store::open(&settings.storage)?;
    let source_handle: serde_json::Value = store.json(&format!("{run_id}/records/handle.json"))?;
    let old = PathBuf::from(
        source_handle["workspace"]
            .as_str()
            .ok_or_else(|| io::Error::other("source run lacks its recorded workspace binding"))?,
    );
    let root = workspace.join("reuse").join(run_id);
    std::fs::create_dir_all(&root)?;
    let mut found = false;
    for (key, reference) in &archive.manifest().artifacts {
        if !(key == execution
            || key.starts_with(&format!("{execution}/"))
            || key.contains("/processed/")
            || key.starts_with("reproduction/")
            || key.starts_with("reuse/"))
        {
            continue;
        }
        if archive.manifest().removed.contains(key) {
            continue;
        }
        found = true;
        let target = root.join(key);
        // Each new run owns retained references to its reused closure, including
        // earlier reuse chains. Source workspace deletion cannot break it.
        let logical = target
            .strip_prefix(workspace)
            .unwrap()
            .to_string_lossy()
            .replace('\\', "/");
        let copied = destination.copy_reference(
            &logical,
            archive.reader(key)?,
            reference,
            reference.role,
        )?;
        crate::workspace::add_reference(target.clone(), copied);
        std::fs::create_dir_all(target.parent().unwrap())?;
        let lazy = key.starts_with("reproduction/")
            || key.ends_with(".jsonl")
            || reference.bytes > 4 * 1024 * 1024;
        if lazy {
            atomic_json(
                &target.with_extension("wf-object"),
                &serde_json::json!({"reference":reference,"locator":crate::artifact::ArtifactLocator{storage:location.clone(),run_id:run_id.clone(),prefix:reference.key.clone()}}),
            )?;
        } else {
            let mut file = std::fs::OpenOptions::new()
                .write(true)
                .create_new(true)
                .open(&target)?;
            io::copy(&mut archive.reader(key)?, &mut file)?;
            file.flush()?;
            file.sync_all()?;
        }
        if crate::workspace::bytes(workspace)? > settings.workspace.max_bytes.get() {
            return Err(io::Error::other(
                "recovery evidence exceeds local workspace budget",
            ));
        }
    }
    if !found {
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            "selected source execution has no available committed artifacts",
        ));
    }
    atomic_json(
        &root.join("handle.json"),
        &serde_json::json!({"run_id":run_id,"storage":location,"workspace":root}),
    )?;
    crate::workspace::add_relocation(
        old.join("execution-input"),
        root.join("reproduction/effective-inputs"),
    );
    crate::workspace::add_relocation(old, root.clone());
    Ok(Some(root.join(execution)))
}