Skip to main content

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/// The `#[scientific_workflow::model("key")]` attribute only registers the
23/// implementation under a manifest key. It does not generate, wrap, proxy, or
24/// provide access to model state. The model defines an ordinary `SystemState`
25/// field, initializes its concrete payloads, and returns a direct borrow of
26/// that field from [`Self::state`]. Coupled fields remain ordinary typed tuple
27/// borrows through [`SystemState::borrow_payloads`] and
28/// [`SystemState::borrow_payloads_mut`]; no field-access macro is involved.
29///
30/// A successful [`Self::step`] represents exactly one scientifically
31/// observable transition and must strictly advance the state's iteration.
32/// Workflow observes the initial state, every successful step, and the final
33/// state automatically. Implementations do not report progress or invoke the
34/// observation plan themselves.
35pub trait ScientificModel: Send + Sized + 'static {
36    /// One complete set of model constants supplied by config.
37    type Constants: DeserializeOwned + Send + Sync + 'static;
38
39    /// Defines the scientific observations recorded for this model.
40    ///
41    /// The default records every declared state field at every iteration.
42    /// Implementations may derive stream selection and sampling cadence from
43    /// model constants. This function is called once during effect-free Study
44    /// preflight; Study binds and retains that exact plan for execution. It
45    /// must not perform external side effects.
46    fn observation_plan(_constants: &Self::Constants) -> TaskResult<ObservationPlan> {
47        Ok(ObservationPlan::all_fields())
48    }
49
50    /// Builds a fully initialized model from resolved constants and the
51    /// runtime-loaded state schema.
52    ///
53    /// The returned model must already contain a usable, fully populated state
54    /// created from this exact `schema` allocation. No state observation occurs
55    /// until this method succeeds.
56    fn initialize(constants: Self::Constants, schema: &SystemStateSchema) -> TaskResult<Self>;
57
58    /// Borrows the model's directly owned canonical state.
59    ///
60    /// This method must be side-effect free and must return the same state
61    /// owner and schema allocation throughout execution.
62    fn state(&self) -> &SystemState;
63
64    /// Reports whether no further scientific transition is required.
65    ///
66    /// This method must be side-effect free. Once it returns `true` during one
67    /// execution, Workflow performs the final observation without calling
68    /// [`Self::step`] again.
69    fn is_complete(&self) -> bool;
70
71    /// Performs exactly one scientifically observable transition.
72    ///
73    /// Success must strictly increase [`SystemState::time`]'s iteration while
74    /// preserving the state owner and schema. Failure must not claim a
75    /// completed transition: Workflow emits neither a successful-step snapshot
76    /// nor a successful-step observation for an error result.
77    fn step(&mut self) -> TaskResult;
78
79    /// Optionally reports the expected final iteration for inferred progress.
80    ///
81    /// The default is unknown. A reported target must be at least the current
82    /// iteration. Once present during an execution, it must never decrease or
83    /// disappear. This method must be side-effect free.
84    fn target_iteration(&self) -> Option<u64> {
85        None
86    }
87}