scientific-workflow 0.13.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
//! Application-owned scientific execution contract.

use std::collections::{BTreeMap, BTreeSet};
use std::sync::{Mutex, MutexGuard};

use serde::de::DeserializeOwned;
use serde_json::{Map, Value};
use sha2::{Digest, Sha256};
use thiserror::Error;

use crate::observation::ObservationPlan;
use crate::state::{StateSchemaProvider, SystemState, SystemStateSchema};

use super::result::UnitResult;

pub(crate) const SEED_DERIVATION_ALGORITHM: &str = "scientific-workflow.seed.v1";

/// Immutable dependency results, execution facts, and optional seed derivation.
///
/// Workflow creates one context for each execution-unit initialization. A
/// unit may inspect completed correlated dependencies. A stochastic unit
/// requests only the named seeds it actually needs; Workflow derives them
/// without counters or request-order dependence and records successful
/// requests with the affected member's output metadata.
pub struct InitializationContext {
    master_seed: Option<u64>,
    replicate_ordinal: u64,
    task_identity: Box<str>,
    execution_unit_key: Box<str>,
    dependencies: Value,
    requests: Mutex<BTreeMap<SeedRequest, u64>>,
}

impl InitializationContext {
    #[cfg(test)]
    pub(crate) fn new(
        master_seed: Option<u64>,
        replicate_ordinal: u64,
        task_identity: &str,
        execution_unit_key: &str,
    ) -> Self {
        Self::with_dependencies(
            master_seed,
            replicate_ordinal,
            task_identity,
            execution_unit_key,
            Value::Array(Vec::new()),
        )
    }

    pub(crate) fn with_dependencies(
        master_seed: Option<u64>,
        replicate_ordinal: u64,
        task_identity: &str,
        execution_unit_key: &str,
        dependencies: Value,
    ) -> Self {
        Self {
            master_seed,
            replicate_ordinal,
            task_identity: task_identity.into(),
            execution_unit_key: execution_unit_key.into(),
            dependencies,
            requests: Mutex::new(BTreeMap::new()),
        }
    }

    /// Returns completed summaries from the task's declared dependency phases.
    pub const fn dependencies(&self) -> &Value {
        &self.dependencies
    }

    /// Returns whether `wf_configs/study.json` supplied a master seed.
    pub const fn has_master_seed(&self) -> bool {
        self.master_seed.is_some()
    }

    /// Derives a seed shared by all members in this execution unit.
    ///
    /// `purpose` is a stable, nonempty semantic name such as `"pairing"`.
    /// Repeating the same request returns the same seed and metadata entry.
    pub fn shared_seed(&self, purpose: &str) -> Result<u64, SeedError> {
        self.seed(SeedScope::Shared, purpose)
    }

    /// Derives a seed belonging to one member exposed by this execution unit.
    ///
    /// `member_identity` must exactly match that member's [`MemberView`] identity;
    /// Workflow rejects an initialization that requested a seed for an unknown
    /// member. `purpose` is a stable, nonempty semantic name such as
    /// `"initialization"` or `"replacement"`.
    pub fn member_seed(&self, member_identity: &str, purpose: &str) -> Result<u64, SeedError> {
        validate_name(member_identity, "member identity")?;
        self.seed(SeedScope::Member(member_identity.into()), purpose)
    }

    fn seed(&self, scope: SeedScope, purpose: &str) -> Result<u64, SeedError> {
        validate_name(purpose, "purpose")?;
        let master_seed = self.master_seed.ok_or(SeedError::MissingMasterSeed)?;
        let request = SeedRequest {
            scope,
            purpose: purpose.into(),
        };
        if let Some(seed) = self.request_ledger().get(&request) {
            return Ok(*seed);
        }

        let seed = derive_seed(
            master_seed,
            self.replicate_ordinal,
            &self.task_identity,
            &self.execution_unit_key,
            &request.scope,
            &request.purpose,
        );
        self.request_ledger().insert(request, seed);
        Ok(seed)
    }

    pub(crate) fn validate_member_identities<'a>(
        &self,
        identities: impl IntoIterator<Item = &'a str>,
    ) -> Result<(), SeedError> {
        let identities = identities.into_iter().collect::<BTreeSet<_>>();
        for request in self.request_ledger().keys() {
            if let SeedScope::Member(identity) = &request.scope
                && !identities.contains(identity.as_ref())
            {
                return Err(SeedError::UnknownMemberIdentity {
                    identity: identity.to_string(),
                });
            }
        }
        Ok(())
    }

    pub(crate) fn metadata_for_member(&self, member_identity: &str) -> Option<Value> {
        let requests = self
            .request_ledger()
            .iter()
            .filter_map(|(request, seed)| match &request.scope {
                SeedScope::Shared => Some(Value::Object(Map::from_iter([
                    ("scope".to_owned(), "shared".into()),
                    ("purpose".to_owned(), request.purpose.to_string().into()),
                    ("seed".to_owned(), (*seed).into()),
                ]))),
                SeedScope::Member(identity) if identity.as_ref() == member_identity => {
                    Some(Value::Object(Map::from_iter([
                        ("scope".to_owned(), "member".into()),
                        ("member_identity".to_owned(), identity.to_string().into()),
                        ("purpose".to_owned(), request.purpose.to_string().into()),
                        ("seed".to_owned(), (*seed).into()),
                    ])))
                }
                SeedScope::Member(_) => None,
            })
            .collect::<Vec<_>>();
        (!requests.is_empty()).then(|| {
            Value::Object(Map::from_iter([
                ("algorithm".to_owned(), SEED_DERIVATION_ALGORITHM.into()),
                (
                    "master_seed".to_owned(),
                    self.master_seed
                        .expect("a recorded request has a seed")
                        .into(),
                ),
                ("requests".to_owned(), requests.into()),
            ]))
        })
    }

    fn request_ledger(&self) -> MutexGuard<'_, BTreeMap<SeedRequest, u64>> {
        self.requests
            .lock()
            .unwrap_or_else(|error| error.into_inner())
    }
}

pub(crate) fn derive_program_seed(
    master_seed: u64,
    replicate_ordinal: u64,
    task_identity: &str,
    program_kind: &str,
    purpose: &str,
) -> u64 {
    derive_seed(
        master_seed,
        replicate_ordinal,
        task_identity,
        program_kind,
        &SeedScope::Shared,
        purpose,
    )
}

fn derive_seed(
    master_seed: u64,
    replicate_ordinal: u64,
    task_identity: &str,
    workload_key: &str,
    scope: &SeedScope,
    purpose: &str,
) -> u64 {
    let mut digest = Sha256::new();
    hash_part(&mut digest, SEED_DERIVATION_ALGORITHM.as_bytes());
    digest.update(master_seed.to_le_bytes());
    digest.update(replicate_ordinal.to_le_bytes());
    hash_part(&mut digest, task_identity.as_bytes());
    hash_part(&mut digest, workload_key.as_bytes());
    match scope {
        SeedScope::Shared => digest.update([0]),
        SeedScope::Member(identity) => {
            digest.update([1]);
            hash_part(&mut digest, identity.as_bytes());
        }
    }
    hash_part(&mut digest, purpose.as_bytes());
    u64::from_le_bytes(digest.finalize()[..8].try_into().expect("SHA-256 prefix"))
}

fn hash_part(digest: &mut Sha256, bytes: &[u8]) {
    digest.update((bytes.len() as u64).to_le_bytes());
    digest.update(bytes);
}

fn validate_name(value: &str, field: &'static str) -> Result<(), SeedError> {
    if value.is_empty() || value.trim() != value {
        return Err(SeedError::InvalidName {
            field,
            value: value.to_owned(),
        });
    }
    Ok(())
}

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
struct SeedRequest {
    scope: SeedScope,
    purpose: Box<str>,
}

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
enum SeedScope {
    Shared,
    Member(Box<str>),
}

/// Failure to make or validate a deterministic initialization seed request.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum SeedError {
    /// The execution unit requested a seed but the study has none.
    #[error("this execution unit requires `seed` in `wf_configs/study.json`")]
    MissingMasterSeed,
    /// A stable seed name was empty or had surrounding whitespace.
    #[error("invalid seed {field} `{value}`: it must be nonempty with no surrounding whitespace")]
    InvalidName {
        /// The invalid semantic field.
        field: &'static str,
        /// The rejected value.
        value: String,
    },
    /// A member seed was requested for an identity the unit did not expose.
    #[error("seed requested for unknown member identity `{identity}`")]
    UnknownMemberIdentity {
        /// The requested identity absent from the execution unit.
        identity: String,
    },
}

/// The optional completion declaration for one execution-unit member.
///
/// This borrowed value distinguishes an incomplete member from a completed
/// member with or without a structured reason. The execution unit retains the
/// reason object; Workflow copies it only when the member first completes.
#[derive(Clone, Copy, Debug)]
pub struct MemberCompletion<'a> {
    reason: Option<&'a Map<String, Value>>,
}

impl<'a> MemberCompletion<'a> {
    /// Declares completion without a structured reason.
    pub const fn without_reason() -> Self {
        Self { reason: None }
    }

    /// Declares completion with a borrowed structured JSON reason.
    pub const fn with_reason(reason: &'a Map<String, Value>) -> Self {
        Self {
            reason: Some(reason),
        }
    }

    /// Returns the structured reason, when the execution unit supplied one.
    pub const fn reason(self) -> Option<&'a Map<String, Value>> {
        self.reason
    }
}

/// A borrowed view of one independently stateful member inside an execution unit.
///
/// The identity, state owner, and schema allocation must remain stable at the
/// same member index for the complete execution. `completion` and
/// `target_iteration` are declarations inspected by Workflow; constructing a
/// view has no side effects.
#[derive(Clone, Copy, Debug)]
pub struct MemberView<'a> {
    identity: &'a str,
    state: &'a SystemState,
    completion: Option<MemberCompletion<'a>>,
    target_iteration: Option<u64>,
}

impl<'a> MemberView<'a> {
    /// Describes one member owned by an [`ExecutionUnit`].
    ///
    /// `identity` must be nonempty, contain no surrounding whitespace, and be
    /// unique within the unit. `state` must remain at the same address and use
    /// the schema supplied to [`ExecutionUnit::initialize`]. A target, when
    /// present, must not precede the state's current iteration.
    pub const fn new(
        identity: &'a str,
        state: &'a SystemState,
        completion: Option<MemberCompletion<'a>>,
        target_iteration: Option<u64>,
    ) -> Self {
        Self {
            identity,
            state,
            completion,
            target_iteration,
        }
    }

    /// Returns the stable identity of this member within its execution unit.
    pub const fn identity(self) -> &'a str {
        self.identity
    }

    /// Borrows this member's directly owned canonical state.
    pub const fn state(self) -> &'a SystemState {
        self.state
    }

    /// Returns completion details, or `None` while the member is incomplete.
    pub const fn completion(self) -> Option<MemberCompletion<'a>> {
        self.completion
    }

    /// Returns this member's optional expected final iteration.
    pub const fn target_iteration(self) -> Option<u64> {
        self.target_iteration
    }
}

/// One schedulable scientific execution containing one or more members.
///
/// Workflow manages every implementation through the same lifecycle and does
/// not distinguish a standalone unit from a coordinated ensemble. A normal
/// unit returns one [`MemberView`]; an ensemble returns one view per member and
/// keeps all internal parallelism, shared inputs, and synchronization private.
/// Each exposed member owns a distinct [`SystemState`].
///
/// Member count, index order, identities, state owners, and schema allocations
/// must remain stable after initialization. One successful [`Self::step`] must
/// strictly advance at least one incomplete member and must never advance a
/// member that was already complete. Other incomplete members may wait during a
/// coordinated step, which permits synchronized ensembles and restored members
/// at different iterations.
pub trait ExecutionUnit: Send + Sized + 'static {
    /// One complete set of constants supplied by Config.
    type Constants: DeserializeOwned + 'static;

    /// Returns the standard static schema supplied for this unit, if any.
    ///
    /// Study uses this provider only when the task omits an explicit project
    /// `state`. The default requires callers to select a project schema. An
    /// upstream provider owns the JSON document; this unit merely declares
    /// that it can receive states using that standard layout.
    fn standard_state_schema() -> Option<StateSchemaProvider> {
        None
    }

    /// Validates unit-owned configuration and defines per-member observations.
    ///
    /// All members of one execution unit use the state schema selected by the
    /// task and this common observation plan. The default records every field
    /// at every iteration without additional validation. An override owns all
    /// domain validation that can be performed before initialization; Study
    /// trusts a successful result. This preflight operation must not create
    /// output, initialize the unit, or mutate external state.
    fn preflight(
        _constants: &Self::Constants,
        _schema: &SystemStateSchema,
    ) -> UnitResult<ObservationPlan> {
        Ok(ObservationPlan::all_fields())
    }

    /// Builds a fully initialized standalone unit or ensemble.
    ///
    /// Every state subsequently exposed through [`Self::member`] must have been
    /// created from this exact schema allocation.
    fn initialize(
        constants: Self::Constants,
        schema: &SystemStateSchema,
        context: &InitializationContext,
    ) -> UnitResult<Self>;

    /// Returns the stable positive number of independently stateful members.
    fn member_count(&self) -> usize;

    /// Borrows one member by stable zero-based index.
    ///
    /// The method must be side-effect free. Workflow calls it repeatedly before
    /// and after coordinated steps. An index below [`Self::member_count`] must
    /// always return `Some`; all other indices must return `None`.
    fn member(&self, index: usize) -> Option<MemberView<'_>>;

    /// Performs one coordinated scientific transition.
    ///
    /// A standalone unit advances itself. An ensemble may advance members in
    /// parallel or share generated inputs, but it must return only after the
    /// complete logical step is visible through [`Self::member`].
    fn step(&mut self) -> UnitResult;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn named_seed_derivation_is_stable_and_request_order_independent() {
        let first = InitializationContext::new(Some(99), 3, "phase/task", "ensemble");
        let first_member = first.member_seed("alpha", "initialization").unwrap();
        let first_shared = first.shared_seed("pairing").unwrap();
        assert_eq!(first_member, 16_741_472_295_366_384_357);
        assert_eq!(first_shared, 1_632_703_961_247_452_931);

        let second = InitializationContext::new(Some(99), 3, "phase/task", "ensemble");
        assert_eq!(second.shared_seed("pairing").unwrap(), first_shared);
        assert_eq!(
            second.member_seed("alpha", "initialization").unwrap(),
            first_member
        );
        assert_ne!(
            second.member_seed("beta", "initialization").unwrap(),
            first_member
        );

        let next_replicate = InitializationContext::new(Some(99), 4, "phase/task", "ensemble");
        assert_ne!(next_replicate.shared_seed("pairing").unwrap(), first_shared);
    }

    #[test]
    fn deterministic_units_need_no_master_seed_but_requests_do() {
        let context = InitializationContext::new(None, 0, "task", "unit");
        assert!(!context.has_master_seed());
        assert!(matches!(
            context.shared_seed("pairing"),
            Err(SeedError::MissingMasterSeed)
        ));
        assert!(context.metadata_for_member("member").is_none());
    }

    #[test]
    fn program_seed_derivation_uses_task_kind_purpose_and_replicate() {
        let seed = derive_program_seed(99, 3, "phase/task", "program", "preparation");
        assert_eq!(
            derive_program_seed(99, 3, "phase/task", "program", "preparation"),
            seed
        );
        assert_ne!(
            derive_program_seed(99, 4, "phase/task", "program", "preparation"),
            seed
        );
        assert_ne!(
            derive_program_seed(99, 3, "phase/task", "python", "preparation"),
            seed
        );
        assert_ne!(
            derive_program_seed(99, 3, "phase/task", "program", "analysis"),
            seed
        );
    }
}