scientific-workflow 0.13.5

Configuration-driven scientific tasks, typed state, and durable recordings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! Typed, immutable results supplied to a task by its declared dependencies.
//!
//! Selectors preserve snapshot order and never read scientific files. Runtime
//! owns dependency scope; this API cannot discover unrelated output directories.

use serde::Deserialize;
use serde_json::Value;
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Invalid input or a dependency selection with unexpected cardinality.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum DependencyError {
    /// An environment variable required by the standard launch contract is absent.
    #[error("missing {variable}; run this program through Workflow's standard study layout")]
    MissingEnvironment {
        /// Required variable name.
        variable: &'static str,
    },
    /// A snapshot could not be read.
    #[error("cannot read dependency snapshot `{path}`")]
    Io {
        /// Expected snapshot path.
        path: PathBuf,
        /// Underlying filesystem failure.
        #[source]
        source: std::io::Error,
    },
    /// A known result or the snapshot structure is malformed.
    #[error("invalid dependency snapshot: {0}")]
    Invalid(String),
    /// No result satisfies the selection.
    #[error("no dependency matches {selection}")]
    Missing {
        /// Applied filters.
        selection: String,
    },
    /// Several results satisfy a selection requiring at most one.
    #[error("ambiguous dependency selection {selection}: {matches:?}; select a phase or task")]
    Ambiguous {
        /// Applied filters.
        selection: String,
        /// Candidate source identifiers in snapshot order.
        matches: Vec<String>,
    },
}

/// One validated dependency snapshot. Unknown workload kinds remain in raw JSON.
#[derive(Clone, Debug)]
pub struct Dependencies {
    raw: Value,
    recordings: Vec<RecordingDependency>,
    programs: Vec<ProgramDependency>,
    npy_batches: Vec<NpyDependency>,
}

#[derive(Deserialize)]
struct Phase {
    phase: String,
    tasks: Vec<Task>,
}
#[derive(Deserialize)]
struct Task {
    identity: String,
    output_directory: PathBuf,
    workload: Value,
}
#[derive(Deserialize)]
struct Member {
    identity: String,
    final_iteration: u64,
    output_directory: PathBuf,
}
#[derive(Deserialize)]
struct Unit {
    execution_unit: String,
    members: Vec<Member>,
}
#[derive(Deserialize)]
struct Program {
    executable: PathBuf,
    python_script: Option<PathBuf>,
}
#[derive(Deserialize)]
struct Npy {
    processed_directory: PathBuf,
}

impl Dependencies {
    /// Validates an owned JSON snapshot without opening its referenced files.
    pub fn from_json(raw: Value) -> Result<Self, DependencyError> {
        let phases: Vec<Phase> = decode(raw.clone())?;
        let mut result = Self {
            raw,
            recordings: Vec::new(),
            programs: Vec::new(),
            npy_batches: Vec::new(),
        };
        let mut phases_seen = std::collections::HashSet::new();
        for phase in phases {
            name(&phase.phase)?;
            if !phases_seen.insert(phase.phase.clone()) {
                return Err(DependencyError::Invalid("duplicate phase identity".into()));
            }
            let mut tasks_seen = std::collections::HashSet::new();
            for task in phase.tasks {
                name(&task.identity)?;
                path(&task.output_directory)?;
                if !tasks_seen.insert(task.identity.clone()) {
                    return Err(DependencyError::Invalid(
                        "duplicate task identity in phase".into(),
                    ));
                }
                let source = Source {
                    phase: phase.phase.clone(),
                    task: task.identity,
                };
                let kind = task
                    .workload
                    .get("kind")
                    .and_then(Value::as_str)
                    .ok_or_else(|| {
                        DependencyError::Invalid("workload.kind must be a nonempty string".into())
                    })?;
                name(kind)?;
                match kind {
                    "execution_unit" => {
                        let unit: Unit = decode(task.workload)?;
                        name(&unit.execution_unit)?;
                        let mut members_seen = std::collections::HashSet::new();
                        if unit.members.is_empty() {
                            return Err(DependencyError::Invalid(
                                "execution unit has no members".into(),
                            ));
                        }
                        for member in unit.members {
                            name(&member.identity)?;
                            path(&member.output_directory)?;
                            if !members_seen.insert(member.identity.clone()) {
                                return Err(DependencyError::Invalid(
                                    "duplicate member identity".into(),
                                ));
                            }
                            result.recordings.push(RecordingDependency {
                                source: source.clone(),
                                execution_unit: unit.execution_unit.clone(),
                                member: member.identity,
                                final_iteration: member.final_iteration,
                                directory: member.output_directory,
                            });
                        }
                    }
                    "program" | "python" => {
                        let program: Program = decode(task.workload.clone())?;
                        path(&program.executable)?;
                        if kind == "python" && program.python_script.is_none() {
                            return Err(DependencyError::Invalid(
                                "python workload requires python_script".into(),
                            ));
                        }
                        if let Some(script) = &program.python_script {
                            path(script)?;
                        }
                        result.programs.push(ProgramDependency {
                            source,
                            directory: task.output_directory.join("artifacts"),
                            executable: program.executable,
                            python_script: program.python_script,
                        });
                    }
                    "npy" => {
                        let npy: Npy = decode(task.workload)?;
                        path(&npy.processed_directory)?;
                        result.npy_batches.push(NpyDependency {
                            source,
                            directory: npy.processed_directory,
                        });
                    }
                    _ => {} // Forward-compatible raw access; known kinds are always validated.
                }
            }
        }
        Ok(result)
    }

    /// Loads and validates a standard dependency snapshot from an explicit file.
    pub fn load(path: &Path) -> Result<Self, DependencyError> {
        let bytes = std::fs::read(path).map_err(|source| DependencyError::Io {
            path: path.to_owned(),
            source,
        })?;
        Self::from_json(
            serde_json::from_slice(&bytes)
                .map_err(|error| DependencyError::Invalid(error.to_string()))?,
        )
    }

    /// Loads `WORKFLOW_DEPENDENCIES_PATH`, supplied by a Workflow program launch.
    pub fn from_env() -> Result<Self, DependencyError> {
        let path = std::env::var_os("WORKFLOW_DEPENDENCIES_PATH").ok_or(
            DependencyError::MissingEnvironment {
                variable: "WORKFLOW_DEPENDENCIES_PATH",
            },
        )?;
        Self::load(Path::new(&path))
    }

    /// Borrows the original snapshot, including unknown extension fields/kinds.
    pub const fn raw_json(&self) -> &Value {
        &self.raw
    }
    /// Selects completed member recordings in deterministic snapshot order.
    pub fn recordings(&self) -> Selection<'_, RecordingDependency> {
        Selection::new(&self.recordings)
    }
    /// Selects program and Python task artifact directories.
    pub fn programs(&self) -> Selection<'_, ProgramDependency> {
        Selection::new(&self.programs)
    }
    /// Selects aggregate NPY batches; this does not filter members within a batch.
    pub fn npy_batches(&self) -> Selection<'_, NpyDependency> {
        Selection::new(&self.npy_batches)
    }
}

fn decode<T: serde::de::DeserializeOwned>(value: Value) -> Result<T, DependencyError> {
    serde_json::from_value(value).map_err(|error| DependencyError::Invalid(error.to_string()))
}
fn name(value: &str) -> Result<(), DependencyError> {
    if value.is_empty() || value.trim() != value {
        return Err(DependencyError::Invalid(
            "identifiers must be nonempty without surrounding whitespace".into(),
        ));
    }
    Ok(())
}
fn path(value: &Path) -> Result<(), DependencyError> {
    if !value.is_absolute() {
        return Err(DependencyError::Invalid(format!(
            "expected absolute path, got {}",
            value.display()
        )));
    }
    Ok(())
}

#[derive(Clone, Debug)]
struct Source {
    phase: String,
    task: String,
}

mod sealed {
    pub trait Sealed {}
}
/// Shared identity access for Workflow-owned dependency result types.
///
/// This trait is sealed; downstream implementations are not supported.
pub trait Dependency: sealed::Sealed {
    /// Declared phase key.
    fn phase(&self) -> &str;
    /// Runtime-generated task identity.
    fn task(&self) -> &str;
    /// Human-readable source used in ambiguity diagnostics.
    fn description(&self) -> String;
}

/// Borrowed, filtered dependency results. Filters combine by intersection.
#[derive(Clone, Debug)]
pub struct Selection<'a, T> {
    entries: Vec<&'a T>,
    filters: Vec<String>,
}
impl<'a, T: Dependency> Selection<'a, T> {
    fn new(entries: &'a [T]) -> Self {
        Self {
            entries: entries.iter().collect(),
            filters: Vec::new(),
        }
    }
    fn filter(mut self, label: String, predicate: impl Fn(&T) -> bool) -> Self {
        self.entries.retain(|entry| predicate(entry));
        self.filters.push(label);
        self
    }
    /// Restricts matches to a declared phase key.
    pub fn in_phase(self, phase: &str) -> Self {
        self.filter(format!("phase={phase:?}"), |entry| entry.phase() == phase)
    }
    /// Restricts matches to an exact runtime task identity.
    pub fn task(self, task: &str) -> Self {
        self.filter(format!("task={task:?}"), |entry| entry.task() == task)
    }
    /// Returns exactly one result, reporting missing or ambiguous matches.
    pub fn one(&self) -> Result<&'a T, DependencyError> {
        self.optional()?.ok_or_else(|| DependencyError::Missing {
            selection: self.label(),
        })
    }
    /// Returns zero or one result; several matches produce an ambiguity error.
    pub fn optional(&self) -> Result<Option<&'a T>, DependencyError> {
        match self.entries.as_slice() {
            [] => Ok(None),
            [entry] => Ok(Some(*entry)),
            _ => Err(DependencyError::Ambiguous {
                selection: self.label(),
                matches: self
                    .entries
                    .iter()
                    .map(|entry| entry.description())
                    .collect(),
            }),
        }
    }
    /// Iterates all matching results in snapshot order, including zero matches.
    pub fn iter(&self) -> impl ExactSizeIterator<Item = &'a T> + '_ {
        self.entries.iter().copied()
    }
    fn label(&self) -> String {
        format!(
            "{} [{}]",
            std::any::type_name::<T>(),
            self.filters.join(", ")
        )
    }
}

impl Selection<'_, RecordingDependency> {
    /// Restricts recordings to an exact execution-unit registration key.
    pub fn execution_unit(self, key: &str) -> Self {
        self.filter(format!("execution_unit={key:?}"), |entry| {
            entry.execution_unit == key
        })
    }
    /// Restricts recordings to an exact member identity.
    pub fn member(self, member: &str) -> Self {
        self.filter(format!("member={member:?}"), |entry| entry.member == member)
    }
}

/// A completed member recording.
#[derive(Clone, Debug)]
pub struct RecordingDependency {
    source: Source,
    execution_unit: String,
    member: String,
    final_iteration: u64,
    directory: PathBuf,
}
impl RecordingDependency {
    /// Declared phase key.
    pub fn phase(&self) -> &str {
        &self.source.phase
    }
    /// Runtime-generated task identity.
    pub fn task(&self) -> &str {
        &self.source.task
    }
    /// Execution-unit registration key.
    pub fn execution_unit(&self) -> &str {
        &self.execution_unit
    }
    /// Member identity within its execution unit.
    pub fn member(&self) -> &str {
        &self.member
    }
    /// Final scientific iteration.
    pub fn final_iteration(&self) -> u64 {
        self.final_iteration
    }
    /// Completed recording directory.
    pub fn directory(&self) -> &Path {
        &self.directory
    }
}
impl sealed::Sealed for RecordingDependency {}
impl Dependency for RecordingDependency {
    fn phase(&self) -> &str {
        self.phase()
    }
    fn task(&self) -> &str {
        self.task()
    }
    fn description(&self) -> String {
        format!("{}/{}", self.phase(), self.task()) + &format!("/{}", self.member)
    }
}

/// A completed external executable or Python task.
#[derive(Clone, Debug)]
pub struct ProgramDependency {
    source: Source,
    directory: PathBuf,
    executable: PathBuf,
    python_script: Option<PathBuf>,
}
impl ProgramDependency {
    /// Declared phase key.
    pub fn phase(&self) -> &str {
        &self.source.phase
    }
    /// Runtime-generated task identity.
    pub fn task(&self) -> &str {
        &self.source.task
    }
    /// Standard artifacts directory, excluding logs and snapshots.
    pub fn directory(&self) -> &Path {
        &self.directory
    }
    /// Resolved launcher executable.
    pub fn executable(&self) -> &Path {
        &self.executable
    }
    /// Canonical Python script when this was a Python task.
    pub fn python_script(&self) -> Option<&Path> {
        self.python_script.as_deref()
    }
}
impl sealed::Sealed for ProgramDependency {}
impl Dependency for ProgramDependency {
    fn phase(&self) -> &str {
        self.phase()
    }
    fn task(&self) -> &str {
        self.task()
    }
    fn description(&self) -> String {
        format!("{}/{}", self.phase(), self.task())
    }
}

/// An aggregate converted NPY batch.
#[derive(Clone, Debug)]
pub struct NpyDependency {
    source: Source,
    directory: PathBuf,
}
impl NpyDependency {
    /// Declared phase key.
    pub fn phase(&self) -> &str {
        &self.source.phase
    }
    /// Runtime-generated task identity.
    pub fn task(&self) -> &str {
        &self.source.task
    }
    /// Verified-reader entry directory containing the batch manifest.
    pub fn directory(&self) -> &Path {
        &self.directory
    }
}
impl sealed::Sealed for NpyDependency {}
impl Dependency for NpyDependency {
    fn phase(&self) -> &str {
        self.phase()
    }
    fn task(&self) -> &str {
        self.task()
    }
    fn description(&self) -> String {
        format!("{}/{}", self.phase(), self.task())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    fn snapshot() -> Value {
        serde_json::json!([
            {"phase":"initialize", "tasks":[{"identity":"a", "output_directory":"/run/a", "workload":{"kind":"execution_unit","execution_unit":"init","members":[{"identity":"one","final_iteration":4,"output_directory":"/run/a"}]}}]},
            {"phase":"other", "tasks":[{"identity":"b", "output_directory":"/run/b", "workload":{"kind":"execution_unit","execution_unit":"init","members":[{"identity":"one","final_iteration":5,"output_directory":"/run/b"}]}}]}
        ])
    }
    #[test]
    fn selection_is_explicit_and_preserves_order_and_source() {
        let deps = Dependencies::from_json(snapshot()).unwrap();
        assert!(
            matches!(deps.recordings().execution_unit("init").one(), Err(DependencyError::Ambiguous { matches, .. }) if matches == ["initialize/a/one", "other/b/one"])
        );
        let selected = deps
            .recordings()
            .in_phase("initialize")
            .member("one")
            .one()
            .unwrap();
        assert_eq!(selected.directory(), Path::new("/run/a"));
        assert_eq!(
            deps.recordings()
                .iter()
                .map(|r| r.final_iteration())
                .collect::<Vec<_>>(),
            [4, 5]
        );
        assert!(
            deps.recordings()
                .in_phase("missing")
                .optional()
                .unwrap()
                .is_none()
        );
        assert!(matches!(
            deps.recordings().in_phase("missing").one(),
            Err(DependencyError::Missing { .. })
        ));
        assert_eq!(deps.raw_json(), &snapshot());
    }
    #[test]
    fn malformed_known_results_fail_and_extensions_remain_available() {
        let mut value = snapshot();
        value[0]["tasks"][0]["workload"]["members"][0]["final_iteration"] = "bad".into();
        assert!(Dependencies::from_json(value).is_err());
        let value = serde_json::json!([{"phase":"extension","tasks":[{"identity":"task","output_directory":"/output","workload":{"kind":"future","extra":1}}]}]);
        let deps = Dependencies::from_json(value.clone()).unwrap();
        assert_eq!(deps.raw_json(), &value);
        assert_eq!(deps.recordings().iter().len(), 0);
    }
}