diff --git a/Cargo.lock b/Cargo.lock index 9343d5df25..91718e71e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3586,6 +3586,15 @@ dependencies = [ "uuid", ] +[[package]] +name = "continuum-persona-engine" +version = "0.1.0" +dependencies = [ + "futures", + "thiserror 2.0.18", + "uuid", +] + [[package]] name = "continuum-positron" version = "0.1.0" diff --git a/core/continuum-persona-engine/Cargo.toml b/core/continuum-persona-engine/Cargo.toml index 234c6cf9e3..4e504a6ff6 100644 --- a/core/continuum-persona-engine/Cargo.toml +++ b/core/continuum-persona-engine/Cargo.toml @@ -6,6 +6,7 @@ description = "The persona engine (CONTINUUM-ENGINE 4.14): one mind decodes at a [dependencies] uuid.workspace = true +futures.workspace = true thiserror.workspace = true [lints.clippy] diff --git a/core/continuum-persona-engine/src/persona.rs b/core/continuum-persona-engine/src/persona.rs index 6b4faff209..d31723cbe0 100644 --- a/core/continuum-persona-engine/src/persona.rs +++ b/core/continuum-persona-engine/src/persona.rs @@ -1,7 +1,12 @@ //! A persona's execution state, owned once and moved only whole. mod id; +mod lifecycle; mod unit; pub use id::{Epoch, PersonaId}; -pub use unit::{PersonaParts, PersonaUnit, PartialMove}; +pub use lifecycle::{Lifecycle, PersonaParts, Preservation, Suspension}; +pub use unit::{PersonaUnit, PartialMove}; + +#[cfg(test)] +mod lifecycle_tests; diff --git a/core/continuum-persona-engine/src/persona/lifecycle.rs b/core/continuum-persona-engine/src/persona/lifecycle.rs new file mode 100644 index 0000000000..83949ed4ae --- /dev/null +++ b/core/continuum-persona-engine/src/persona/lifecycle.rs @@ -0,0 +1,60 @@ +//! Each owner suspends its children and returns their preservation results. +use std::future::Future; + +/// Repeated suspension must preserve the same state. The owner retains custody on +/// cancellation and on refusal. A poll must yield promptly while I/O or GPU work is pending. Implementations can return a nested child report. +pub trait Lifecycle { + type Suspension: Suspension; + fn suspend(&mut self) -> impl Future + Send; +} + +pub trait Suspension { + fn is_preserved(&self) -> bool; +} + +#[derive(Debug, PartialEq, Eq)] +pub enum Preservation { + Preserved, + Pending(P), + Failed(E), +} + +impl Suspension for Preservation { + fn is_preserved(&self) -> bool { + matches!(self, Self::Preserved) + } +} + +// One declaration supplies the owned fields, traversal, and typed report shape. +macro_rules! persona_parts { + ($($field:ident: $part:ident),+ $(,)?) => { + #[derive(Debug, Clone, PartialEq, Eq)] + pub struct PersonaParts<$($part),+> { + $(pub $field: $part),+ + } + + impl<$($part: Lifecycle + Send),+> Lifecycle for PersonaParts<$($part),+> + where $($part::Suspension: Send),+ + { + type Suspension = PersonaParts<$($part::Suspension),+>; + async fn suspend(&mut self) -> Self::Suspension { + let ($($field,)+) = futures::join!($(self.$field.suspend()),+); + PersonaParts { $($field),+ } + } + } + + impl<$($part: Suspension),+> Suspension for PersonaParts<$($part),+> { + fn is_preserved(&self) -> bool { + $(self.$field.is_preserved())&&+ + } + } + }; +} + +persona_parts! { + blocks: Blocks, + tokens: Tokens, + genome: Genome, + learning: Learning, + decode: Decode, +} diff --git a/core/continuum-persona-engine/src/persona/lifecycle_tests.rs b/core/continuum-persona-engine/src/persona/lifecycle_tests.rs new file mode 100644 index 0000000000..c4383294b6 --- /dev/null +++ b/core/continuum-persona-engine/src/persona/lifecycle_tests.rs @@ -0,0 +1,94 @@ +use super::*; +use futures::executor::block_on; + +struct Part { + release_after_retry: bool, + failure: bool, + attempts: usize, +} + +impl Part { + fn ready() -> Self { + Self { release_after_retry: false, failure: false, attempts: 0 } + } +} + +impl Lifecycle for Part { + type Suspension = Preservation; + + async fn suspend(&mut self) -> Self::Suspension { + self.attempts += 1; + if self.failure { + Preservation::Failed("storage unavailable") + } else if self.release_after_retry && self.attempts == 1 { + Preservation::Pending(17) + } else { + Preservation::Preserved + } + } +} + +#[test] +fn root_reports_pending_and_failed_children_and_keeps_custody_for_retry() { + let mut parts = PersonaParts { + blocks: Part::ready(), + tokens: Part::ready(), + genome: Part::ready(), + learning: Part { release_after_retry: true, ..Part::ready() }, + decode: Part::ready(), + }; + parts.tokens.failure = true; + let mut persona = PersonaUnit::new(PersonaId(uuid::Uuid::nil()), Epoch(1), parts); + let report = block_on(persona.suspend()); + assert_eq!(report.tokens, Preservation::Failed("storage unavailable")); + assert_eq!(report.learning, Preservation::Pending(17)); + assert!(report.decode.is_preserved()); + assert!(!report.is_preserved()); + + let retried = block_on(persona.suspend()); + assert!(retried.learning.is_preserved()); + assert!(!retried.is_preserved()); + assert_eq!(persona.parts().learning.attempts, 2); +} + +#[test] +fn one_pending_fence_leaves_all_siblings_free_to_start() { + use futures::channel::oneshot; + use std::sync::{Arc, atomic::{AtomicUsize, Ordering}}; + + struct Gated { + started: Arc, + fence: oneshot::Receiver<()>, + } + impl Lifecycle for Gated { + type Suspension = Preservation<(), oneshot::Canceled>; + + async fn suspend(&mut self) -> Self::Suspension { + self.started.fetch_add(1, Ordering::SeqCst); + match (&mut self.fence).await { + Ok(()) => Preservation::Preserved, + Err(error) => Preservation::Failed(error), + } + } + } + + let started = Arc::new(AtomicUsize::new(0)); + let mut signals = Vec::new(); + let mut part = || { + let (signal, fence) = oneshot::channel(); + signals.push(signal); + Gated { started: started.clone(), fence } + }; + let mut persona = PersonaUnit::new(PersonaId(uuid::Uuid::nil()), Epoch(1), PersonaParts { + blocks: part(), tokens: part(), genome: part(), learning: part(), decode: part(), + }); + block_on(async { + let mut suspend = Box::pin(persona.suspend()); + assert!(futures::poll!(&mut suspend).is_pending()); + assert_eq!(started.load(Ordering::SeqCst), 5); + for signal in signals.into_iter().rev() { + assert!(signal.send(()).is_ok()); + } + assert!(suspend.await.is_preserved()); + }); +} diff --git a/core/continuum-persona-engine/src/persona/unit.rs b/core/continuum-persona-engine/src/persona/unit.rs index 4104f1f079..0aa216378f 100644 --- a/core/continuum-persona-engine/src/persona/unit.rs +++ b/core/continuum-persona-engine/src/persona/unit.rs @@ -2,17 +2,7 @@ use super::{Epoch, PersonaId}; -/// Her five parts (4.14 "The object design"): execution blocks, token history, the genome -/// generation her decode binds, learning custody, decode state (drafts, sampler, rollback). -/// Generic so each part is the native handle in the engine and a plain value in a contract test. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct PersonaParts { - pub blocks: Blocks, - pub tokens: Tokens, - pub genome: Genome, - pub learning: Learning, - pub decode: Decode, -} +use super::{Lifecycle, PersonaParts}; /// A part was missing from a move: the unit is refused whole, never assembled from pieces. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] @@ -78,3 +68,14 @@ impl PersonaUnit { self.parts } } + +impl Lifecycle for PersonaUnit +where + PersonaParts: Lifecycle + Send, +{ + type Suspension = as Lifecycle>::Suspension; + + async fn suspend(&mut self) -> Self::Suspension { + self.parts.suspend().await + } +} diff --git a/tools/scripts/tests/ci-change-scope.test.cjs b/tools/scripts/tests/ci-change-scope.test.cjs index a17a3f6481..12d0cd6a73 100644 --- a/tools/scripts/tests/ci-change-scope.test.cjs +++ b/tools/scripts/tests/ci-change-scope.test.cjs @@ -12,7 +12,7 @@ const select = (...paths) => { // Tested crates a kernel change does not reach: the floor (base, host, git, service, saved-state) and // the leaves that depend on nothing in the workspace (the codec continuum-context, the flow // continuum-flow, the proving ground's types continuum-proving-types). -const BELOW_KERNEL = ['continuum-base', 'continuum-host', 'continuum-git', 'continuum-service', 'continuum-saved-state', 'continuum-context', 'continuum-flow', 'continuum-proving-types']; +const BELOW_KERNEL = ['continuum-base', 'continuum-host', 'continuum-git', 'continuum-service', 'continuum-saved-state', 'continuum-context', 'continuum-flow', 'continuum-proving-types', 'continuum-persona-engine']; test('installer repairs run installer checks without unrelated Rust builds', () => { for (const file of ['install.ps1', 'tools/scripts/lib/install-common.ps1', 'tools/scripts/lib/win-modules.ps1', 'tools/scripts/lib/win-engine.ps1', 'tools/scripts/lib/windows-engine-receipt.ps1', 'tools/scripts/lib/windows-service.ps1', 'tools/scripts/lib/windows-media-reconciliation.ps1', 'tools/scripts/lib/windows-elevation.ps1', 'tools/scripts/tests/windows-process.test.ps1', 'tools/scripts/tests/windows-airc-download.test.ps1', 'tools/scripts/tests/windows-xz.test.ps1', 'tools/scripts/sync-windows-bootstrap.ps1']) @@ -148,10 +148,11 @@ test('a crate-local change runs that crate and every crate that depends on it', const belowMind = crates('continuum-engine', 'continuum-vitals', 'continuum-prompt', 'continuum-engram', 'continuum-capture', 'continuum-embedding', 'continuum-store', 'continuum-models', 'continuum-hands', 'continuum-live', 'continuum-perception', 'continuum-genome', 'continuum-memory', 'continuum-cognition', 'continuum-academy', 'continuum-sentinel', 'continuum-mind', 'continuum-dreaming', 'continuum-grid', 'continuum-serving', 'continuum-core', 'continuum-gateway'); assert.deepEqual(scope(['core/continuum-engine/src/lib.rs']), belowMind); // The floor crates (base, host, git) sit below the kernel: a kernel change runs everything above them. + assert.deepEqual(scope(['core/continuum-persona-engine/src/persona/unit.rs']), crates('continuum-persona-engine')); const aboveFloor = TESTED_CRATES.filter(n => !BELOW_KERNEL.includes(n)); assert.deepEqual(scope(['core/continuum-kernel/src/lib.rs']), { crates: aboveFloor, cli: false }); // A floor change runs the floor crate and every crate built on it. - assert.deepEqual(scope(['core/continuum-host/src/durable/mod.rs']), { crates: TESTED_CRATES.filter(n => !['continuum-base', 'continuum-service', 'continuum-context', 'continuum-flow', 'continuum-proving-types'].includes(n)), cli: true }); + assert.deepEqual(scope(['core/continuum-host/src/durable/mod.rs']), { crates: TESTED_CRATES.filter(n => !['continuum-base', 'continuum-service', 'continuum-context', 'continuum-flow', 'continuum-proving-types', 'continuum-persona-engine'].includes(n)), cli: true }); // continuum-git is used by hands (a citizen's git) and core (the deploy tree): both run, and every crate above hands. assert.deepEqual(scope(['core/continuum-git/src/index_lock.rs']), crates('continuum-git', 'continuum-hands', 'continuum-cognition', 'continuum-academy', 'continuum-mind', 'continuum-dreaming', 'continuum-grid', 'continuum-serving', 'continuum-core')); // Untested crates reach the tested ones through the graph: llama -> engine -> ...