Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
2ef9f8e
core/llama: a safe PersonaBlocks over the fork's persona seam; the en…
joelteply Nov 18, 2026
998768e
Cargo.lock: core/llama depends on thiserror 2
joelteply Nov 18, 2026
75a7710
PersonaBlocks::bind replaces load(stream): the memory chooses the str…
joelteply Nov 18, 2026
c590d7c
LLAMA_CPP_PIN records the move to fork #147 (persona seam with bind),…
joelteply Nov 18, 2026
df35115
PersonaBlocks: reserve names only her owner, grow adds room ahead of …
joelteply Nov 18, 2026
ea955a8
PersonaBlocks is made with her owner and grows and shrinks; pin fork …
joelteply Nov 18, 2026
078bcac
Merge remote-tracking branch 'origin/canary' into fable/unified-tier
joelteply Nov 18, 2026
3d16663
The persona engine switches minds on the real 35B in ~9 ms: UnifiedTi…
joelteply Nov 18, 2026
370294f
UnifiedTier keeps a mind's custody on stale or failed epochs; streams…
joelteply Nov 18, 2026
d815ce5
Test CI dependency selection for the persona llama adapter
joelteply Nov 18, 2026
de0e34a
UnifiedTier binds a new mind only on a stream the engine reports empt…
joelteply Nov 18, 2026
1865973
PersonaBlocks::bind takes the owner the transfer is for, and refuses …
joelteply Nov 18, 2026
22789ec
Merge remote-tracking branch 'origin/fable/llama-persona-seam' into f…
joelteply Nov 18, 2026
16826fc
UnifiedTier checks every move before it replaces her blocks; binds na…
joelteply Nov 18, 2026
7019c38
Merge remote-tracking branch 'origin/canary' into fable/unified-tier
joelteply Nov 18, 2026
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
10 changes: 10 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 @@ -29,6 +29,7 @@ members = [
"core/continuum-sentinel",
"core/continuum-kernel",
"core/continuum-persona-engine",
"core/continuum-persona-llama",
"core/continuum-store",
"core/continuum-grid",
"core/continuum-mind",
Expand Down
2 changes: 1 addition & 1 deletion core/continuum-persona-engine/src/residency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ mod fence;
mod tier;

pub use fence::{Direction, Fence, FenceTag};
pub use tier::{Tier, TierRefusal};
pub use tier::{Native, Tier, TierRefusal};
22 changes: 21 additions & 1 deletion core/continuum-persona-engine/src/residency/tier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,31 @@ use crate::persona::PersonaId;

/// A tier could not take or give a persona's bytes: named, so the engine keeps her where she is.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{tier}: {reason} for persona {persona:?}")]
#[error("{tier}: {reason} for persona {persona:?}{native}")]
pub struct TierRefusal {
pub tier: &'static str,
pub persona: PersonaId,
pub reason: &'static str,
/// The backend's own refusal, kept rather than erased.
pub native: Native,
}

/// What the backend itself said when the tier refused.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Native {
/// The refusal is the tier's own (a precondition it checked); the backend was not asked.
NotAsked,
/// The backend refused, by its status and the name it gives that status.
Refused { status: i32, name: String },
}

impl std::fmt::Display for Native {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NotAsked => Ok(()),
Self::Refused { status, name } => write!(f, " (backend status {status}: {name})"),
}
}
}

/// A residency tier.
Expand Down
19 changes: 19 additions & 0 deletions core/continuum-persona-llama/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "continuum-persona-llama"
version = "0.1.0"
edition = "2021"
description = "The persona engine's residency tier over the fork's native persona blocks (CONTINUUM-ENGINE 4.14): one context, two execution streams (the active mind decodes in one while the next binds into the other), every other mind's blocks in RAM. A switch is a native bind, never a serialize or a re-prefill."

[features]
metal = ["llama/metal"]

[dependencies]
continuum-persona-engine = { path = "../continuum-persona-engine" }
llama = { path = "../llama" }
thiserror.workspace = true
uuid.workspace = true

[lints.clippy]
unwrap_used = "deny"
expect_used = "deny"
panic = "deny"
224 changes: 224 additions & 0 deletions core/continuum-persona-llama/examples/persona_switch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
//! The persona engine on a real model: N minds take turns through `Roles` and the `UnifiedTier`, one
//! decoding at a time. Each turn stages the next mind (her copy-in), hands off, retires the previous
//! mind (her copy-out to RAM), then decodes. Reports the switch (handoff + copy-out + copy-in) and the
//! decode rate, separately and together.
//!
//! cargo run --release --features metal --example persona_switch -- <model.gguf> <minds> <prompt_tokens> <decode_tokens> <rounds>

use std::time::{Duration, Instant};

use continuum_persona_engine::persona::{Epoch, PersonaId};
use continuum_persona_engine::pipeline::{Identified, Roles};
use continuum_persona_engine::residency::{Direction, FenceTag, Tier};
use continuum_persona_llama::{Landed, Rebind, UnifiedTier};
use llama::{Batch, ContextParams, FlashAttn, Model, ModelParams};

/// One mind in the run: who she is, where her history ends, and her last sampled token.
struct Mind {
id: PersonaId,
epoch: Epoch,
prompt: Vec<i32>,
pos: i32,
last: i32,
}

impl Identified for Mind {
fn persona(&self) -> PersonaId {
self.id
}
fn epoch(&self) -> Epoch {
self.epoch
}
}

fn argmax(logits: &[f32]) -> i32 {
let mut best = (0usize, f32::MIN);
for (i, &v) in logits.iter().enumerate() {
if v > best.1 {
best = (i, v);
}
}
best.0 as i32
}

fn arg(args: &[String], i: usize, what: &str) -> Result<usize, String> {
args.get(i).ok_or(format!("missing {what}"))?.parse().map_err(|e| format!("{what}: {e}"))
}

/// Codex on #6134: a tier wrapping a context already in use must not report a first bind over the
/// state another sequence holds there. One token is decoded into sequence 0 before the tier exists; a
/// new mind's move in must be refused with that state untouched, and succeed once the stream is empty.
fn adopted_context_is_refused(model: &Model, ctx: &mut llama::Context<'_>) -> Result<(), String> {
let token = *model.tokenize("held", true, false)?.first().ok_or("no token for the probe")?;
let mut batch = Batch::allocated(1, 1);
batch.push(token, 0, &[0], false);
ctx.decode(&batch)?;
let mind = FenceTag { persona: PersonaId(uuid::Uuid::new_v4()), epoch: Epoch(1), direction: Direction::In };
{
let mut tier = UnifiedTier::new(ctx);
if tier.begin(mind).is_ok() {
return Err("adoption: a new mind was bound over sequence 0's state".into());
}
}
if ctx.memory_seq_pos_max(0) != 0 {
return Err("adoption: the refused move changed sequence 0's state".into());
}
ctx.memory_seq_rm(0, -1, -1);
if UnifiedTier::new(ctx).begin(mind).is_err() {
return Err("adoption: a new mind was refused an empty stream".into());
}
ctx.memory_seq_rm(0, -1, -1);
println!("persona_switch: adoption: a new mind is refused over held state and bound on an empty stream");
Ok(())
}

/// Codex on #6134 (de0e34a97): a refused move must not cost her the blocks she had. Epoch 1 decodes and
/// parks in RAM; another sequence's state lands on her stream; epoch 2's move in is refused there; once
/// the stream is clear epoch 1 binds back at the position she left (her byte-identical continuation
/// after a bind is the fork's own test, test-persona-blocks).
fn a_refused_newer_epoch_keeps_her_parked_blocks(model: &Model, ctx: &mut llama::Context<'_>) -> Result<(), String> {
let tokens = model.tokenize("She keeps her own notes.", true, false)?;
let her = PersonaId(uuid::Uuid::new_v4());
let tag = |epoch, direction| FenceTag { persona: her, epoch: Epoch(epoch), direction };
let mut tier = UnifiedTier::new(ctx);
tier.begin(tag(1, Direction::In)).map_err(|e| e.to_string())?;
let seq = tier.stream_of(her).ok_or("replay: she is not bound")?.seq();
let mut batch = Batch::allocated(tokens.len() as i32, 1);
for (i, &t) in tokens.iter().enumerate() {
batch.push(t, i as i32, &[seq], false);
}
tier.context().decode(&batch)?;
let held_to = tier.context().memory_seq_pos_max(seq);
tier.begin(tag(1, Direction::Out)).map_err(|e| e.to_string())?;
let first = *tokens.first().ok_or("replay: no tokens")?;
let mut other = Batch::allocated(1, 1);
other.push(first, 0, &[seq], false);
tier.context().decode(&other)?;
if tier.begin(tag(2, Direction::In)).is_ok() {
return Err("replay: epoch 2 was bound over another sequence's state".into());
}
tier.context().memory_seq_rm(seq, -1, -1);
tier.begin(tag(1, Direction::In)).map_err(|e| format!("replay: her parked epoch 1 was lost: {e}"))?;
let back = tier.stream_of(her).ok_or("replay: she is not bound again")?.seq();
if tier.context().memory_seq_pos_max(back) != held_to {
return Err(format!("replay: epoch 1 came back holding {} instead of {held_to}", tier.context().memory_seq_pos_max(back)));
}
tier.begin(tag(1, Direction::Out)).map_err(|e| e.to_string())?;
println!("persona_switch: replay: a refused newer epoch kept her parked epoch 1, which bound back at position {held_to}");
Ok(())
}

fn main() -> Result<(), String> {
let args: Vec<String> = std::env::args().collect();
let model_path = args.get(1).ok_or("usage: persona_switch <model.gguf> <minds> <prompt_tokens> <decode_tokens> <rounds>")?;
let (minds, prompt_tokens, decode_tokens, rounds) =
(arg(&args, 2, "minds")?, arg(&args, 3, "prompt_tokens")?, arg(&args, 4, "decode_tokens")?, arg(&args, 5, "rounds")?);

llama::backend_init();
let model = Model::load(model_path, ModelParams::default())?;
// two execution streams (active + next); each holds one mind's whole window
let window = (prompt_tokens + decode_tokens * rounds + 64) as u32;
let params = ContextParams { n_ctx: window * 2, n_batch: 2048, n_ubatch: 2048, n_seq_max: 2, flash_attn: FlashAttn::Enabled, ..ContextParams::default() };
let mut ctx = model.new_context(params)?;
adopted_context_is_refused(&model, &mut ctx)?;
a_refused_newer_epoch_keeps_her_parked_blocks(&model, &mut ctx)?;
let mut tier = UnifiedTier::new(&mut ctx);

let mut parked: Vec<Mind> = (0..minds)
.map(|i| {
let text = format!("Mind {i} keeps her own notes. ").repeat(prompt_tokens / 6 + 1);
let mut prompt = model.tokenize(&text, true, false)?;
prompt.truncate(prompt_tokens);
Ok(Mind { id: PersonaId(uuid::Uuid::new_v4()), epoch: Epoch(1), prompt, pos: 0, last: 0 })
})
.collect::<Result<_, String>>()?;

let mut roles: Roles<Mind, Landed> = Roles::default();
let (mut same_seq, mut other_seq) = (Vec::new(), Vec::new());
let (mut switches, mut decode_time, mut decoded, mut prefill_time) = (Vec::new(), Duration::ZERO, 0usize, Duration::ZERO);
let run = Instant::now();
for turn in 0..minds * rounds {
// the next mind in turn order (the scheduler's choice in the engine; round-robin here)
let next = parked.remove(0);
let t = Instant::now();
let fence = tier.begin(FenceTag { persona: next.id, epoch: next.epoch, direction: Direction::In }).map_err(|e| e.to_string())?;
let rebind = tier.last_rebind();
roles.stage(next, fence).map_err(|r| format!("{:?}", r.reason))?;
roles.handoff().map_err(|e| format!("{e:?}"))?;
if let Some(out) = roles.awaiting_copy_out() {
let fence = tier.begin(out).map_err(|e| e.to_string())?;
roles.begin_retire(fence).map_err(|r| format!("{:?}", r.reason))?;
parked.push(roles.release_retired().map_err(|e| format!("{e:?}"))?);
}
let switched = t.elapsed();
if turn > 0 {
switches.push(switched);
match rebind {
Some(Rebind::SameSequence) => same_seq.push(switched),
Some(Rebind::OtherSequence) => other_seq.push(switched),
None => {}
}
}

let mind = roles.active_mut().ok_or("no active mind after handoff")?;
let seq = tier.stream_of(mind.id).ok_or("the active mind is not bound")?.seq();
let ctx = tier.context();
if mind.pos == 0 {
let t = Instant::now();
let mut last_index = 0;
for chunk in mind.prompt.chunks(2048) {
let mut batch = Batch::allocated(chunk.len() as i32, 1);
for (j, &tok) in chunk.iter().enumerate() {
batch.push(tok, mind.pos + j as i32, &[seq], j + 1 == chunk.len());
}
ctx.decode(&batch)?;
mind.pos += chunk.len() as i32;
last_index = chunk.len() as i32 - 1;
}
mind.last = argmax(ctx.logits_ith(last_index));
prefill_time += t.elapsed();
}
let t = Instant::now();
for _ in 0..decode_tokens {
let mut batch = Batch::allocated(1, 1);
batch.push(mind.last, mind.pos, &[seq], true);
ctx.decode(&batch)?;
mind.pos += 1;
mind.last = argmax(ctx.logits_ith(0));
}
decode_time += t.elapsed();
decoded += decode_tokens;
}

// a stale request for an older epoch of a held mind is refused and leaves her blocks: she still binds
let probe = parked.first().ok_or("no parked mind to probe")?;
let stale = tier.begin(FenceTag { persona: probe.id, epoch: Epoch(0), direction: Direction::In });
let kept = tier.begin(FenceTag { persona: probe.id, epoch: probe.epoch, direction: Direction::In }).is_ok();
println!("persona_switch: stale epoch refused={} her blocks still bind={kept}", stale.is_err());
switches.sort();
same_seq.sort();
other_seq.sort();
let ms = |d: Duration| d.as_secs_f64() * 1e3;
let at = |v: &[Duration], q: f64| v.get(((v.len().max(1) as f64 - 1.0) * q) as usize).copied().map(ms).unwrap_or(0.0); // unwrap_or: an empty class prints 0
let p = |q: f64| at(&switches, q);
let decode_tps = decoded as f64 / decode_time.as_secs_f64();
let switch_total: Duration = switches.iter().sum();
let sustained = decoded as f64 / (decode_time + switch_total).as_secs_f64();
println!(
"persona_switch: minds={minds} prompt={prompt_tokens} decode/turn={decode_tokens} rounds={rounds} turns={} | switch p50 {:.2} ms p90 {:.2} ms max {:.2} ms | decode {decode_tps:.1} tok/s | sustained incl. switches {sustained:.1} tok/s | prefill {:.1} s total | wall {:.1} s",
minds * rounds,
p(0.5),
p(0.9),
p(1.0),
prefill_time.as_secs_f64(),
run.elapsed().as_secs_f64()
);
println!(
"persona_switch: rebinds (binds of a mind saved before) same sequence n={} p50 {:.2} ms | other sequence (cells relabeled over her span) n={} p50 {:.2} ms; stage, handoff and copy-out run serially before her decode, not overlapped",
same_seq.len(),
at(&same_seq, 0.5),
other_seq.len(),
at(&other_seq, 0.5)
);
Ok(())
}
9 changes: 9 additions & 0 deletions core/continuum-persona-llama/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! The persona engine's [`Tier`](continuum_persona_engine::residency::Tier) over the fork's native
//! persona blocks: one libllama context, two execution streams, and every other mind's blocks held in
//! RAM as she left them. The pipeline ([`continuum_persona_engine::pipeline::Roles`]) decides when a
//! mind moves; this adapter only performs the move (a native save out, a native bind in) and reports
//! it landed.

mod unified_tier;

pub use unified_tier::{Landed, Rebind, Stream, UnifiedTier};
Loading
Loading