Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
413 changes: 210 additions & 203 deletions core/continuum-cognition/src/cognition/deferred_faculty.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions core/continuum-kernel/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub mod runtime;
pub mod service_module;
pub mod share_controller;
pub mod shared_compute;
pub mod stage;
pub mod storage_provider;
pub mod params;

Expand Down
41 changes: 41 additions & 0 deletions core/continuum-kernel/src/runtime/stage/control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! Control requests and the receipts that prove a stop. A request is not a stop: only a receipt
//! from the run's owner proves which stop happened.

use super::identity::RunId;

/// What a running stage may be asked to do at its next yield point.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ControlRequest {
Continue,
Pause,
Yield,
Cancel,
}

/// Proof of a stop, owned by the task and runtime owner across a caller returning, a forgotten
/// handle and restart.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StopReceipt {
/// Quiescent; its continuation is retained; execution and residency both stay charged.
Paused { run: RunId },
/// Its continuation is retained; execution capacity was released; residency stays as named.
Yielded {
run: RunId,
residency_kept: super::cost::ResidencyCost,
},
/// Terminal after quiescence; the effects it had already committed are recorded.
Cancelled { run: RunId, committed_effects: u64 },
/// The request could not be honoured as asked; never a silent drop presented as a stop.
Refused { run: RunId, refusal: StopRefusal },
}

/// Why a stop request was refused.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StopRefusal {
/// This stage, or the engine under it, cannot pause the same execution.
PauseUnsupported,
/// The run had already ended.
AlreadyTerminal,
/// The request came from a superseded incarnation.
StaleIncarnation,
}
34 changes: 34 additions & 0 deletions core/continuum-kernel/src/runtime/stage/cost.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! What a run costs. Execution and residency are separate claims on the one resource authority:
//! paused or yielded state still holds bytes after execution capacity is released.

use std::time::Duration;

/// The model tier a call needs; the resource authority maps it to a lane and a model.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ModelTier {
Small,
Mid,
Large,
}

/// Executing: CPU time (its measured cost, `None` until measured: never claimed free, never a
/// guessed number) or a model call at a tier.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExecutionCost {
Cpu { measured: Option<Duration> },
Model { tier: ModelTier },
}

/// Keeping state resident across a pause or yield: bytes that stay charged, by kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ResidencyCost {
pub host_bytes: u64,
pub device_bytes: u64,
}

/// One run's claim: what executing costs, and what staying resident costs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Cost {
pub execution: ExecutionCost,
pub residency: ResidencyCost,
}
51 changes: 51 additions & 0 deletions core/continuum-kernel/src/runtime/stage/identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! Who a stage is, which instance of it is running, and which run produced a result.

use uuid::Uuid;

/// A brain function's identity, stable across restarts and nodes: derived from its name, which
/// is kept beside it as an attribute, never parsed from it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct StageId(Uuid);

impl StageId {
/// The one derivation: the same name yields the same id everywhere.
pub fn named(name: &str) -> Self {
Self(Uuid::new_v5(
&Uuid::NAMESPACE_OID,
format!("continuum.stage.{name}").as_bytes(),
))
}

pub fn as_uuid(self) -> Uuid {
self.0
}
}

/// The mind a stage serves.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MindId(pub Uuid);

/// One running instance of a stage for one mind. A new incarnation (restart, migration,
/// re-host) adopts the previous one's open obligations with a durable receipt and fences its
/// mutations; `epoch` orders incarnations of the same (stage, mind).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StageIncarnation {
pub stage: StageId,
pub mind: MindId,
pub epoch: u64,
}

impl StageIncarnation {
/// Whether `other` is a later incarnation of the same stage for the same mind: the one rule
/// that fences a stale incarnation's mutations.
pub fn is_superseded_by(&self, other: &StageIncarnation) -> bool {
self.stage == other.stage && self.mind == other.mind && other.epoch > self.epoch
}
}

/// One run within an incarnation: what a dispatch, a stop receipt and an output are tied to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RunId {
pub incarnation: StageIncarnation,
pub seq: u64,
}
31 changes: 31 additions & 0 deletions core/continuum-kernel/src/runtime/stage/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! The stage contract (M0): every brain function is a stage on one substrate.
//!
//! A stage declares its inputs (typed topics), its triggers, its cost (execution and residency,
//! claimed from the one resource authority) and its yield points; the substrate gives it
//! delivery, scheduling, fusion, interruption and capture once, for all stages
//! (docs/architecture/STAGE-CONTRACT.md, THE-WHOLE-MIND §1b).
//!
//! This module owns the contract's types and the pure rules every stage shares (identity,
//! provenance, the publication compare-and-set). The task lifetime and delivery owner and the
//! resource authority implement their halves behind these types.

mod control;
mod cost;
mod identity;
mod publication;
mod runner;
mod stage;
mod topic;
mod triggers;

pub use control::{ControlRequest, StopReceipt, StopRefusal};
pub use cost::{Cost, ExecutionCost, ModelTier, ResidencyCost};
pub use identity::{MindId, RunId, StageId, StageIncarnation};
pub use publication::{Publication, Published};
pub use runner::{PublishedOutput, PublishedState, Ran, StageRunner};
pub use stage::{RunOutcome, Stage, StageContext};
pub use topic::{InputRevision, Provenance, TopicKind, TopicName};
pub use triggers::Triggers;

#[cfg(test)]
mod tests;
40 changes: 40 additions & 0 deletions core/continuum-kernel/src/runtime/stage/publication.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! The publication rule: within one incarnation, an output derived from older input never
//! overwrites state derived from newer input (overlapping runs, a late result).

use std::collections::BTreeMap;

use super::topic::{InputRevision, TopicName};

/// The input revisions the currently published state was derived from, per topic.
#[derive(Debug, Clone, Default)]
pub struct Publication {
derived_from: BTreeMap<TopicName, u64>,
}

/// What an attempt to publish did.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Published {
/// The output's inputs are at least as new as the published state's on every topic: it is
/// now the published state.
Accepted,
/// Some topic it read is older than what the published state already reflects: refused.
Stale,
}

impl Publication {
/// Compare and set: publish only if no topic the output read is behind the published state.
pub fn try_publish(&mut self, from: &[InputRevision]) -> Published {
let behind = from.iter().any(|input| {
self.derived_from
.get(&input.topic)
.is_some_and(|&published| input.through < published)
});
if behind {
return Published::Stale;
}
for input in from {
self.derived_from.insert(input.topic, input.through);
}
Published::Accepted
}
}
Loading
Loading