aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / spark-harness / src / execution.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Task execution states for the provisional scheduler (Headstart M2, |
| 17 | //! issue #535). |
| 18 | //! |
| 19 | //! ```text |
| 20 | //! Blocked --dispatch--> Provisional --work done--> AwaitingFinalProof --finalize--> Committable |
| 21 | //! ^ | | | |
| 22 | //! +------ revoke --------+---------------------------+-------------------------------+ |
| 23 | //! any of the first four --cancel / failure / lease expiry--> Aborted --retry--> Blocked |
| 24 | //! |
| 25 | //! Committable --begin commit--> Committing --outcome--> Committed | CommitUncertain | Aborted |
| 26 | //! ``` |
| 27 | //! |
| 28 | //! `Committable` means "every dependency is final-verified for its current |
| 29 | //! generation". Committing itself is done by the existing effect/World |
| 30 | //! machinery in aien-runtime, which this crate does not touch. The optional |
| 31 | //! `Committing` state (Headstart M3, issue #537) lets a caller record that |
| 32 | //! it is about to run such a commit: a journaled `BeginCommit` freezes the |
| 33 | //! task's generation and the producer completions it was checked against, |
| 34 | //! and a journaled `CommitOutcome` ends it. While a task is `Committing`, |
| 35 | //! every event that would revoke, cancel, fail or re-generate it (directly |
| 36 | //! or through an upstream producer) is refused. A `Committing` task found |
| 37 | //! after a restart has no recorded outcome and becomes `CommitUncertain`; |
| 38 | //! it is never run again. `Committed` and `CommitUncertain` are terminal. |
| 39 | //! |
| 40 | //! Revocation returns a task to `Blocked` rather than `Aborted`. The task did |
| 41 | //! nothing wrong: an upstream producer failed, changed its interface or |
| 42 | //! started a new generation. Its provisional result is discarded and it waits |
| 43 | //! to be dispatched again against the new upstream state, without a manual |
| 44 | //! retry. A task fails into `Aborted` only for its own reasons (its own |
| 45 | //! failure, cancellation, lost lease). |
| 46 | //! |
| 47 | //! Only `Provisional` holds resource permits. Leaving `Provisional` releases |
| 48 | //! them, so every other state holds none. |
| 49 | |
| 50 | use serde::{Deserialize, Serialize}; |
| 51 | use std::fmt; |
| 52 | |
| 53 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] |
| 54 | pub enum TaskState { |
| 55 | Blocked, |
| 56 | Provisional, |
| 57 | AwaitingFinalProof, |
| 58 | Committable, |
| 59 | Aborted, |
| 60 | /// A commit of this task's result is running (see the module docs). |
| 61 | Committing, |
| 62 | /// The commit reported success. Terminal. |
| 63 | Committed, |
| 64 | /// The commit's outcome is unknown (it may have reached the World). |
| 65 | /// Terminal: never run again; an operator resolves it. |
| 66 | CommitUncertain, |
| 67 | } |
| 68 | |
| 69 | impl TaskState { |
| 70 | pub fn as_str(self) -> &'static str { |
| 71 | match self { |
| 72 | TaskState::Blocked => "blocked", |
| 73 | TaskState::Provisional => "provisional", |
| 74 | TaskState::AwaitingFinalProof => "awaiting_final_proof", |
| 75 | TaskState::Committable => "committable", |
| 76 | TaskState::Aborted => "aborted", |
| 77 | TaskState::Committing => "committing", |
| 78 | TaskState::Committed => "committed", |
| 79 | TaskState::CommitUncertain => "commit_uncertain", |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// `Committed` or `CommitUncertain`: the task is done and no upstream |
| 84 | /// change reaches it or, through it, its consumers. |
| 85 | pub fn is_commit_terminal(self) -> bool { |
| 86 | matches!(self, TaskState::Committed | TaskState::CommitUncertain) |
| 87 | } |
| 88 | |
| 89 | /// A completion that consumers may treat as final. |
| 90 | pub fn is_final(self) -> bool { |
| 91 | matches!( |
| 92 | self, |
| 93 | TaskState::Committable | TaskState::Committing | TaskState::Committed |
| 94 | ) |
| 95 | } |
| 96 | |
| 97 | /// States whose work or proof is built on upstream artifacts and must be |
| 98 | /// revoked when upstream changes. |
| 99 | pub fn is_active(self) -> bool { |
| 100 | matches!( |
| 101 | self, |
| 102 | TaskState::Provisional | TaskState::AwaitingFinalProof | TaskState::Committable |
| 103 | ) |
| 104 | } |
| 105 | |
| 106 | /// Only a running task holds resource permits. |
| 107 | pub fn holds_permits(self) -> bool { |
| 108 | self == TaskState::Provisional |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 113 | pub struct IllegalTransition { |
| 114 | pub from: TaskState, |
| 115 | pub to: TaskState, |
| 116 | } |
| 117 | |
| 118 | impl fmt::Display for IllegalTransition { |
| 119 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 120 | write!( |
| 121 | f, |
| 122 | "illegal task transition {} -> {}", |
| 123 | self.from.as_str(), |
| 124 | self.to.as_str() |
| 125 | ) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | impl std::error::Error for IllegalTransition {} |
| 130 | |
| 131 | /// The complete transition table. Anything not listed is refused. |
| 132 | pub fn check_transition(from: TaskState, to: TaskState) -> Result<(), IllegalTransition> { |
| 133 | use TaskState::*; |
| 134 | let ok = matches!( |
| 135 | (from, to), |
| 136 | (Blocked, Provisional) |
| 137 | | (Provisional, AwaitingFinalProof) |
| 138 | | (AwaitingFinalProof, Committable) |
| 139 | | (Provisional | AwaitingFinalProof | Committable, Blocked) |
| 140 | | ( |
| 141 | Blocked | Provisional | AwaitingFinalProof | Committable, |
| 142 | Aborted |
| 143 | ) |
| 144 | | (Aborted, Blocked) |
| 145 | | (Committable, Committing) |
| 146 | | (Committing, Committed | CommitUncertain | Aborted) |
| 147 | ); |
| 148 | if ok { |
| 149 | Ok(()) |
| 150 | } else { |
| 151 | Err(IllegalTransition { from, to }) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #[cfg(test)] |
| 156 | mod tests { |
| 157 | use super::*; |
| 158 | use TaskState::*; |
| 159 | |
| 160 | #[test] |
| 161 | fn table_refuses_shortcuts() { |
| 162 | for (from, to) in [ |
| 163 | (Blocked, AwaitingFinalProof), |
| 164 | (Blocked, Committable), |
| 165 | (Provisional, Committable), |
| 166 | (Aborted, Provisional), |
| 167 | (Aborted, Committable), |
| 168 | (Committable, Provisional), |
| 169 | (Blocked, Blocked), |
| 170 | (AwaitingFinalProof, Committing), |
| 171 | (Committing, Blocked), |
| 172 | (Committing, Committable), |
| 173 | (Committed, Blocked), |
| 174 | (Committed, Aborted), |
| 175 | (Committed, Committing), |
| 176 | (CommitUncertain, Blocked), |
| 177 | (CommitUncertain, Aborted), |
| 178 | (CommitUncertain, Committing), |
| 179 | ] { |
| 180 | assert!(check_transition(from, to).is_err(), "{from:?} -> {to:?}"); |
| 181 | } |
| 182 | for (from, to) in [ |
| 183 | (Blocked, Provisional), |
| 184 | (Provisional, AwaitingFinalProof), |
| 185 | (AwaitingFinalProof, Committable), |
| 186 | (Committable, Blocked), |
| 187 | (Aborted, Blocked), |
| 188 | (Committable, Committing), |
| 189 | (Committing, Committed), |
| 190 | (Committing, CommitUncertain), |
| 191 | (Committing, Aborted), |
| 192 | ] { |
| 193 | assert!(check_transition(from, to).is_ok(), "{from:?} -> {to:?}"); |
| 194 | } |
| 195 | } |
| 196 | } |