scientific-workflow 0.6.0

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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! Phase-based scheduling and centralized runtime display.
//!
//! Applications declare the complete `config -> tasks -> phases -> runtime`
//! structure before execution. [`WorkflowRuntime`] schedules only those tasks,
//! displays their lifecycle, and provides cooperative cancellation. Each task
//! workload owns all scientific I/O, artifacts, recordings, and subprocesses.
//!
//! ```no_run
//! use scientific_workflow::prelude::basics::*;
//! use scientific_workflow::prelude::runtime::*;
//!
//! # fn main() -> Result<(), RuntimeError> {
//! let project = ScientificProject::load("my-project")
//!     .map_err(|error| RuntimeError::TaskWorkload {
//!         task: "load-project".to_owned(),
//!         source: Box::new(error),
//!     })?;
//! let phase = Phase::builder(2, "simulation")
//!     .activity_tasks_from_project(&project, "prepare", |context| {
//!         context.set_detail("ready");
//!         Ok(())
//!     })
//!     .max_concurrent_workloads(1)
//!     .queue_capacity(1)
//!     .build()?;
//! let summary = WorkflowRuntime::builder()
//!     .phase(phase)
//!     .hidden()
//!     .build()?
//!     .run_phases([2])?;
//! assert!(summary.is_success());
//! # Ok(())
//! # }
//! ```

use std::collections::{HashMap, HashSet};
use std::fmt;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

mod error;
mod phase;
mod renderer;
mod reporting;
mod scheduler;
mod task;

pub use error::RuntimeError;
pub use phase::{
    Phase, PhaseBuilder, PhaseFailurePolicy, PhaseId, Task, TaskDisplayKind, TaskId, TaskKey,
    TaskSelector,
};
pub use reporting::{
    ActivityTask, CancellationToken, ProgressSummary, TaskIdentity, TaskProgress, TaskStatus,
};
pub use task::{TaskContext, TaskResult};

use renderer::RuntimeOutput;
use reporting::RuntimeReporter;

static RUNTIME_OWNED: AtomicBool = AtomicBool::new(false);

type SatisfiedPhaseVerifier = Arc<dyn Fn(PhaseId) -> bool + Send + Sync + 'static>;

/// Builder for one immutable runtime plan.
pub struct WorkflowRuntimeBuilder {
    phases: Vec<Phase>,
    output: RuntimeOutput,
    satisfied_phase: Option<SatisfiedPhaseVerifier>,
}

impl WorkflowRuntimeBuilder {
    /// Adds one already validated nonempty phase.
    pub fn phase(mut self, phase: Phase) -> Self {
        self.phases.push(phase);
        self
    }

    /// Adds phases in deterministic declaration order.
    pub fn phases<I>(mut self, phases: I) -> Self
    where
        I: IntoIterator<Item = Phase>,
    {
        self.phases.extend(phases);
        self
    }

    /// Supplies application verification for an omitted completed dependency.
    pub fn satisfied_phase_verifier<F>(mut self, verifier: F) -> Self
    where
        F: Fn(PhaseId) -> bool + Send + Sync + 'static,
    {
        self.satisfied_phase = Some(Arc::new(verifier));
        self
    }

    /// Selects automatic terminal/plain output detection.
    pub fn automatic(mut self) -> Self {
        self.output = RuntimeOutput::Auto;
        self
    }

    /// Forces cursor-controlled interactive display.
    pub fn terminal(mut self) -> Self {
        self.output = RuntimeOutput::Terminal;
        self
    }

    /// Forces append-only uncolored line output.
    pub fn plain(mut self) -> Self {
        self.output = RuntimeOutput::Plain;
        self
    }

    /// Suppresses display while preserving scheduling and lifecycle checks.
    pub fn hidden(mut self) -> Self {
        self.output = RuntimeOutput::Hidden;
        self
    }

    /// Validates the complete phase/task/dependency plan.
    pub fn build(self) -> Result<WorkflowRuntime, RuntimeError> {
        validate_plan(&self.phases)?;
        Ok(WorkflowRuntime {
            phases: self.phases,
            output: self.output,
            satisfied_phase: self.satisfied_phase,
            cancellation: CancellationToken::new(),
        })
    }
}

impl fmt::Debug for WorkflowRuntimeBuilder {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("WorkflowRuntimeBuilder")
            .field("phases", &self.phases.len())
            .field("output", &self.output)
            .field(
                "has_satisfied_phase_verifier",
                &self.satisfied_phase.is_some(),
            )
            .finish_non_exhaustive()
    }
}

/// Non-clone scheduler and display owner for one declared workflow plan.
pub struct WorkflowRuntime {
    phases: Vec<Phase>,
    output: RuntimeOutput,
    satisfied_phase: Option<SatisfiedPhaseVerifier>,
    cancellation: CancellationToken,
}

impl WorkflowRuntime {
    /// Starts an empty builder. At least one phase is mandatory.
    pub fn builder() -> WorkflowRuntimeBuilder {
        WorkflowRuntimeBuilder {
            phases: Vec::new(),
            output: RuntimeOutput::Auto,
            satisfied_phase: None,
        }
    }

    /// Borrows all registered phases in deterministic declaration order.
    pub fn phases(&self) -> &[Phase] {
        &self.phases
    }

    /// Borrows one registered phase by stable ID.
    pub fn phase(&self, id: impl Into<PhaseId>) -> Option<&Phase> {
        let id = id.into();
        self.phases.iter().find(|phase| phase.id() == id)
    }

    /// Returns a cheap token that can request or observe runtime cancellation.
    pub fn cancellation_token(&self) -> CancellationToken {
        self.cancellation.clone()
    }

    /// Returns the unique task matching an exact partial selector.
    pub fn unique_task_matching(&self, selector: &TaskSelector) -> Result<&Task, RuntimeError> {
        let mut matches = self
            .phases
            .iter()
            .flat_map(|phase| phase.tasks())
            .filter(|task| selector.matches(task));
        let first = matches
            .next()
            .ok_or_else(|| RuntimeError::ManagedTaskNotFound {
                selector: selector.to_string(),
            })?;
        if let Some(second) = matches.next() {
            return Err(RuntimeError::ManagedTaskSelectorAmbiguous {
                selector: selector.to_string(),
                first: first.key().to_string(),
                second: second.key().to_string(),
            });
        }
        Ok(first)
    }

    /// Runs exactly the selected phases; omitted unsatisfied dependencies fail.
    pub fn run_phases<I, P>(self, phases: I) -> Result<RuntimeSummary, RuntimeError>
    where
        I: IntoIterator<Item = P>,
        P: Into<PhaseId>,
    {
        self.run_phases_exact(phases)
    }

    /// Runs exactly the selected phases; omitted unsatisfied dependencies fail.
    pub fn run_phases_exact<I, P>(self, phases: I) -> Result<RuntimeSummary, RuntimeError>
    where
        I: IntoIterator<Item = P>,
        P: Into<PhaseId>,
    {
        let selected = self.select_exact(phases)?;
        self.execute(selected)
    }

    /// Adds unsatisfied dependencies and runs the deterministic closure.
    pub fn run_phases_with_dependencies<I, P>(
        self,
        phases: I,
    ) -> Result<RuntimeSummary, RuntimeError>
    where
        I: IntoIterator<Item = P>,
        P: Into<PhaseId>,
    {
        let selected = self.select_with_dependencies(phases)?;
        self.execute(selected)
    }

    fn select_exact<I, P>(&self, phases: I) -> Result<Vec<usize>, RuntimeError>
    where
        I: IntoIterator<Item = P>,
        P: Into<PhaseId>,
    {
        let requested = selected_ids(&self.phases, phases)?;
        for phase in self
            .phases
            .iter()
            .filter(|phase| requested.contains(&phase.id()))
        {
            for dependency in phase.dependencies() {
                if !requested.contains(dependency) && !self.is_satisfied(*dependency) {
                    return Err(RuntimeError::UnsatisfiedPhaseDependency {
                        phase: phase.id().get(),
                        dependency: dependency.get(),
                    });
                }
            }
        }
        Ok(topological_positions(&self.phases)?
            .into_iter()
            .filter(|position| requested.contains(&self.phases[*position].id()))
            .collect())
    }

    fn select_with_dependencies<I, P>(&self, phases: I) -> Result<Vec<usize>, RuntimeError>
    where
        I: IntoIterator<Item = P>,
        P: Into<PhaseId>,
    {
        let mut selected = selected_ids(&self.phases, phases)?;
        let positions: HashMap<_, _> = self
            .phases
            .iter()
            .enumerate()
            .map(|(position, phase)| (phase.id(), position))
            .collect();
        let mut pending: Vec<_> = selected.iter().copied().collect();
        while let Some(id) = pending.pop() {
            let phase = &self.phases[positions[&id]];
            for dependency in phase.dependencies() {
                if !self.is_satisfied(*dependency) && selected.insert(*dependency) {
                    pending.push(*dependency);
                }
            }
        }
        Ok(topological_positions(&self.phases)?
            .into_iter()
            .filter(|position| selected.contains(&self.phases[*position].id()))
            .collect())
    }

    fn is_satisfied(&self, phase: PhaseId) -> bool {
        self.satisfied_phase
            .as_ref()
            .is_some_and(|verify| verify(phase))
    }

    fn execute(self, selected: Vec<usize>) -> Result<RuntimeSummary, RuntimeError> {
        let _lease = RuntimeLease::acquire()?;
        let total_phases = selected.len();
        let total_tasks = selected
            .iter()
            .map(|position| self.phases[*position].tasks().len())
            .sum();
        let mut summaries = Vec::with_capacity(total_phases);
        let mut phases = self.phases.into_iter().map(Some).collect::<Vec<_>>();

        for (selection_position, phase_position) in selected.iter().copied().enumerate() {
            let phase = phases[phase_position]
                .take()
                .expect("selected phase positions are unique");
            renderer::phase_start(self.output, &phase, selection_position + 1, total_phases);
            let heading = renderer::phase_heading(&phase, selection_position + 1, total_phases);
            let builder = RuntimeReporter::for_phase(&phase, &heading)?
                .cancellation_token(self.cancellation.clone());
            let reporter = match self.output {
                RuntimeOutput::Auto => builder,
                RuntimeOutput::Terminal => builder.terminal(),
                RuntimeOutput::Plain => builder.plain(),
                RuntimeOutput::Hidden => builder.hidden(),
            }
            .start()?;
            let phase_id = phase.id();
            let phase_label: Arc<str> = phase.label().into();
            let require_confirm = phase.requires_confirmation();
            let result = scheduler::execute_phase(phase, &reporter);
            let progress = if result.is_ok() {
                reporter.complete(format!("phase {phase_id} completed"))?
            } else {
                reporter.fail(format!("phase {phase_id} failed"))?
            };
            let success = result.is_ok() && progress.is_success();
            renderer::phase_complete(self.output, phase_id, &phase_label, success);
            summaries.push(PhaseSummary {
                id: phase_id,
                label: phase_label,
                progress,
            });
            if let Err(error) = result {
                renderer::runtime_complete(self.output, summaries.len(), total_tasks, false);
                return Err(RuntimeError::PhaseExecutionFailed {
                    summary: RuntimeSummary {
                        phases: summaries.into(),
                    },
                    source: Box::new(error),
                });
            }
            if require_confirm && selection_position + 1 < total_phases {
                let next = phases[selected[selection_position + 1]]
                    .as_ref()
                    .expect("the next selected phase has not executed");
                let confirmed = match renderer::confirm_transition(phase_id, next) {
                    Ok(confirmed) => confirmed,
                    Err(source) => {
                        renderer::runtime_complete(
                            self.output,
                            summaries.len(),
                            total_tasks,
                            false,
                        );
                        return Err(RuntimeError::PhaseExecutionFailed {
                            summary: RuntimeSummary {
                                phases: summaries.into(),
                            },
                            source: Box::new(RuntimeError::PhaseConfirmationInput {
                                phase: phase_id.get(),
                                source,
                            }),
                        });
                    }
                };
                if !confirmed {
                    renderer::runtime_complete(self.output, summaries.len(), total_tasks, false);
                    return Err(RuntimeError::PhaseExecutionFailed {
                        summary: RuntimeSummary {
                            phases: summaries.into(),
                        },
                        source: Box::new(RuntimeError::PhaseConfirmationEof {
                            phase: phase_id.get(),
                        }),
                    });
                }
            }
        }

        renderer::runtime_complete(self.output, summaries.len(), total_tasks, true);
        Ok(RuntimeSummary {
            phases: summaries.into(),
        })
    }
}

impl fmt::Debug for WorkflowRuntime {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("WorkflowRuntime")
            .field("phases", &self.phases.len())
            .field(
                "tasks",
                &self.phases.iter().map(|p| p.tasks().len()).sum::<usize>(),
            )
            .field("output", &self.output)
            .finish_non_exhaustive()
    }
}

/// Terminal summary for one selected phase.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PhaseSummary {
    id: PhaseId,
    label: Arc<str>,
    progress: ProgressSummary,
}

impl PhaseSummary {
    pub const fn id(&self) -> PhaseId {
        self.id
    }

    pub fn label(&self) -> &str {
        &self.label
    }

    pub fn progress(&self) -> &ProgressSummary {
        &self.progress
    }

    pub fn is_success(&self) -> bool {
        self.progress.is_success()
    }
}

/// Immutable aggregate for a completed selected runtime plan.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RuntimeSummary {
    phases: Arc<[PhaseSummary]>,
}

impl RuntimeSummary {
    pub fn phases(&self) -> &[PhaseSummary] {
        &self.phases
    }

    pub fn total_tasks(&self) -> u64 {
        self.phases.iter().map(|phase| phase.progress.total()).sum()
    }

    pub fn is_success(&self) -> bool {
        !self.phases.is_empty() && self.phases.iter().all(PhaseSummary::is_success)
    }
}

struct RuntimeLease;

impl RuntimeLease {
    fn acquire() -> Result<Self, RuntimeError> {
        RUNTIME_OWNED
            .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
            .map(|_| Self)
            .map_err(|_| RuntimeError::TerminalAlreadyOwned)
    }
}

impl Drop for RuntimeLease {
    fn drop(&mut self) {
        RUNTIME_OWNED.store(false, Ordering::Release);
    }
}

fn selected_ids<I, P>(phases: &[Phase], requested: I) -> Result<HashSet<PhaseId>, RuntimeError>
where
    I: IntoIterator<Item = P>,
    P: Into<PhaseId>,
{
    let known: HashSet<_> = phases.iter().map(Phase::id).collect();
    let selected: HashSet<_> = requested.into_iter().map(Into::into).collect();
    if selected.is_empty() {
        return Err(RuntimeError::EmptyPhaseSet);
    }
    if let Some(unknown) = selected.iter().find(|id| !known.contains(id)) {
        return Err(RuntimeError::UnknownSelectedPhase {
            phase: unknown.get(),
        });
    }
    Ok(selected)
}

fn validate_plan(phases: &[Phase]) -> Result<(), RuntimeError> {
    if phases.is_empty() {
        return Err(RuntimeError::EmptyPhaseSet);
    }
    let mut ids = HashSet::with_capacity(phases.len());
    for phase in phases {
        if !ids.insert(phase.id()) {
            return Err(RuntimeError::DuplicatePhaseId {
                phase: phase.id().get(),
            });
        }
    }
    for phase in phases {
        for dependency in phase.dependencies() {
            if !ids.contains(dependency) {
                return Err(RuntimeError::UnknownPhaseDependency {
                    phase: phase.id().get(),
                    dependency: dependency.get(),
                });
            }
        }
        for task in phase.tasks() {
            if !task.has_workload() && !task.is_reused() {
                return Err(RuntimeError::MissingTaskWorkload {
                    task: task.key().to_string(),
                });
            }
        }
    }
    topological_positions(phases).map(|_| ())
}

fn topological_positions(phases: &[Phase]) -> Result<Vec<usize>, RuntimeError> {
    let positions: HashMap<_, _> = phases
        .iter()
        .enumerate()
        .map(|(position, phase)| (phase.id(), position))
        .collect();
    let mut states = vec![0_u8; phases.len()];
    let mut ordered = Vec::with_capacity(phases.len());
    fn visit(
        position: usize,
        phases: &[Phase],
        positions: &HashMap<PhaseId, usize>,
        states: &mut [u8],
        ordered: &mut Vec<usize>,
    ) -> Result<(), RuntimeError> {
        match states[position] {
            2 => return Ok(()),
            1 => {
                return Err(RuntimeError::PhaseDependencyCycle {
                    phase: phases[position].id().get(),
                });
            }
            _ => {}
        }
        states[position] = 1;
        for dependency in phases[position].dependencies() {
            visit(positions[dependency], phases, positions, states, ordered)?;
        }
        states[position] = 2;
        ordered.push(position);
        Ok(())
    }
    for position in 0..phases.len() {
        visit(position, phases, &positions, &mut states, &mut ordered)?;
    }
    Ok(ordered)
}