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
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ members = [
"core/continuum-core",
"core/continuum-sentinel",
"core/continuum-kernel",
"core/continuum-persona-engine",
"core/continuum-store",
"core/continuum-grid",
"core/continuum-mind",
Expand Down
15 changes: 15 additions & 0 deletions core/continuum-persona-engine/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "continuum-persona-engine"
version = "0.1.0"
edition = "2021"
description = "The persona engine (CONTINUUM-ENGINE 4.14): one mind decodes at a time; the next is staged as data, the previous retires to RAM, a switch binds in ms. Objects, not launch flags."

[dependencies]
uuid.workspace = true
futures.workspace = true
thiserror.workspace = true

[lints.clippy]
unwrap_used = "deny"
expect_used = "deny"
panic = "deny"
1 change: 1 addition & 0 deletions core/continuum-persona-engine/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allow-expect-in-tests = true
14 changes: 14 additions & 0 deletions core/continuum-persona-engine/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! The persona engine (CONTINUUM-ENGINE 4.14): our loop over libllama, replacing
//! `llamacpp_scheduler`. ONE mind decodes at a time. The next is staged as data while she
//! decodes, the previous retires to the RAM tier after the handoff fence, and a switch binds
//! her blocks in milliseconds. Nothing here is a launch flag or a slot count: one-at-a-time is
//! the shape of [`pipeline::Roles`], which cannot hold two active minds.
//!
//! The engine is libllama in-process, called through its API (the fork's `llama_persona_*` and
//! `llama_state_seq_*` over `core/llama`): never a llama-server process configured by launch
//! flags. Each [`residency::Tier`] adapter wraps those calls; the pipeline decides when they run.

pub mod persona;
pub mod pipeline;
pub mod residency;
pub mod schedule;
12 changes: 12 additions & 0 deletions core/continuum-persona-engine/src/persona.rs
Original file line number Diff line number Diff line change
@@ -0,0 +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 lifecycle::{Lifecycle, PersonaParts, Preservation, Suspension};
pub use unit::PersonaUnit;

#[cfg(test)]
mod lifecycle_tests;
10 changes: 10 additions & 0 deletions core/continuum-persona-engine/src/persona/id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Typed identities: a persona and the epoch her state belongs to.

/// One persona, by her citizen id.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PersonaId(pub uuid::Uuid);

/// The generation of her state: a new epoch starts her blocks over (a new model, a revocation),
/// so state from another epoch is never bound as hers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Epoch(pub u64);
60 changes: 60 additions & 0 deletions core/continuum-persona-engine/src/persona/lifecycle.rs
Original file line number Diff line number Diff line change
@@ -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<Output = Self::Suspension> + Send;
}

pub trait Suspension {
fn is_preserved(&self) -> bool;
}

#[derive(Debug, PartialEq, Eq)]
pub enum Preservation<P, E> {
Preserved,
Pending(P),
Failed(E),
}

impl<P, E> Suspension for Preservation<P, E> {
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,
}
102 changes: 102 additions & 0 deletions core/continuum-persona-engine/src/persona/lifecycle_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use super::*;
use futures::executor::block_on;

/// How a test part answers suspension: the three behaviours that exist, named (not two flags).
#[derive(Clone, Copy)]
enum Behaviour {
Ready,
PendingOnce,
Fails,
}

struct Part {
behaviour: Behaviour,
attempts: usize,
}

impl Part {
fn ready() -> Self {
Self::with(Behaviour::Ready)
}
fn with(behaviour: Behaviour) -> Self {
Self { behaviour, attempts: 0 }
}
}

impl Lifecycle for Part {
type Suspension = Preservation<u64, &'static str>;

async fn suspend(&mut self) -> Self::Suspension {
self.attempts += 1;
match self.behaviour {
Behaviour::Fails => Preservation::Failed("storage unavailable"),
Behaviour::PendingOnce if self.attempts == 1 => Preservation::Pending(17),
Behaviour::PendingOnce | Behaviour::Ready => 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::with(Behaviour::PendingOnce),
decode: Part::ready(),
};
parts.tokens.behaviour = Behaviour::Fails;
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<AtomicUsize>,
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());
});
}
47 changes: 47 additions & 0 deletions core/continuum-persona-engine/src/persona/unit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! [`PersonaUnit`]: her five parts as one value. A partial unit cannot be built: the only
//! constructor takes a complete [`PersonaParts`], so a move is whole by type.

use super::{Epoch, Lifecycle, PersonaId, PersonaParts};

/// One persona's execution state. No part of her is held anywhere else: a slot's KV, a cycle's
/// gene set, a learner's pass are views of this unit, never copies.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PersonaUnit<Blocks, Tokens, Genome, Learning, Decode> {
id: PersonaId,
epoch: Epoch,
parts: PersonaParts<Blocks, Tokens, Genome, Learning, Decode>,
}

impl<B, T, G, L, D> PersonaUnit<B, T, G, L, D> {
pub fn new(id: PersonaId, epoch: Epoch, parts: PersonaParts<B, T, G, L, D>) -> Self {
Self { id, epoch, parts }
}

pub fn id(&self) -> PersonaId {
self.id
}

pub fn epoch(&self) -> Epoch {
self.epoch
}

pub fn parts(&self) -> &PersonaParts<B, T, G, L, D> {
&self.parts
}

/// Give up the whole unit as its parts (a tier's copy-out takes all of them at once).
pub fn into_parts(self) -> PersonaParts<B, T, G, L, D> {
self.parts
}
}

impl<B, T, G, L, D> Lifecycle for PersonaUnit<B, T, G, L, D>
where
PersonaParts<B, T, G, L, D>: Lifecycle + Send,
{
type Suspension = <PersonaParts<B, T, G, L, D> as Lifecycle>::Suspension;

async fn suspend(&mut self) -> Self::Suspension {
self.parts.suspend().await
}
}
7 changes: 7 additions & 0 deletions core/continuum-persona-engine/src/pipeline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! The active / next / retiring pipeline.

mod roles;
#[cfg(test)]
mod tests;

pub use roles::{HandoffRefusal, Identified, Refused, RetireRefusal, Roles, StageRefusal};
Loading
Loading