scientific_workflow/task/model.rs
1//! Application-owned scientific model contract.
2
3use serde::de::DeserializeOwned;
4
5use crate::observation::advanced::ObservationPlan;
6use crate::state::advanced::{SystemState, SystemStateSchema};
7
8use super::result::TaskResult;
9
10/// A stateful scientific workload initialized from config-supplied constants.
11///
12/// # Direct state ownership
13///
14/// The implementing model **must directly own** the [`SystemState`] returned
15/// by [`ScientificModel::state`] for its entire execution. Rust traits cannot
16/// express field-level structural ownership, so this requirement is semantic;
17/// the task runtime enforces the observable parts by requiring a stable state
18/// address and the exact schema allocation supplied to [`Self::initialize`].
19/// Returning a temporary, swapping the state owner, or changing schemas is a
20/// contract violation.
21///
22/// A successful [`Self::step`] represents exactly one scientifically
23/// observable transition and must strictly advance the state's iteration.
24/// Workflow observes the initial state, every successful step, and the final
25/// state automatically. Implementations do not report progress or invoke the
26/// observation plan themselves.
27pub trait ScientificModel: Send + Sized + 'static {
28 /// One complete set of model constants supplied by config.
29 type Constants: DeserializeOwned + Send + Sync + 'static;
30
31 /// Defines the scientific observations recorded for this model.
32 ///
33 /// The default records every declared state field at every iteration.
34 /// Implementations may derive stream selection and sampling cadence from
35 /// model constants. This function is called once during effect-free Study
36 /// preflight; Study binds and retains that exact plan for execution. It
37 /// must not perform external side effects.
38 fn observation_plan(_constants: &Self::Constants) -> TaskResult<ObservationPlan> {
39 Ok(ObservationPlan::all_fields())
40 }
41
42 /// Builds a fully initialized model from resolved constants and the
43 /// runtime-loaded state schema.
44 ///
45 /// The returned model must already contain a usable, fully populated state
46 /// created from this exact `schema` allocation. No state observation occurs
47 /// until this method succeeds.
48 fn initialize(constants: Self::Constants, schema: &SystemStateSchema) -> TaskResult<Self>;
49
50 /// Borrows the model's directly owned canonical state.
51 ///
52 /// This method must be side-effect free and must return the same state
53 /// owner and schema allocation throughout execution.
54 fn state(&self) -> &SystemState;
55
56 /// Reports whether no further scientific transition is required.
57 ///
58 /// This method must be side-effect free. Once it returns `true` during one
59 /// execution, Workflow performs the final observation without calling
60 /// [`Self::step`] again.
61 fn is_complete(&self) -> bool;
62
63 /// Performs exactly one scientifically observable transition.
64 ///
65 /// Success must strictly increase [`SystemState::time`]'s iteration while
66 /// preserving the state owner and schema. Failure must not claim a
67 /// completed transition: Workflow emits neither a successful-step snapshot
68 /// nor a successful-step observation for an error result.
69 fn step(&mut self) -> TaskResult;
70
71 /// Optionally reports the expected final iteration for inferred progress.
72 ///
73 /// The default is unknown. A reported target must be at least the current
74 /// iteration. Once present during an execution, it must never decrease or
75 /// disappear. This method must be side-effect free.
76 fn target_iteration(&self) -> Option<u64> {
77 None
78 }
79}