Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
9333856
Schedule overdue resident turns at landed frame boundaries
joelteply Nov 20, 2026
4341d3c
Represent resident readiness and service timing explicitly
joelteply Nov 20, 2026
082236b
Retain the earliest valid learned checkpoint across deeper captures
joelteply Nov 20, 2026
8dfd786
Keep native checkpoint custody in resident history
joelteply Nov 20, 2026
9403533
Express history transfer and checkpoint basis ownership
joelteply Nov 20, 2026
b9b60cb
Compare token history once when binding a resident
joelteply Nov 20, 2026
e8cadfb
Merge pull request #6184 from CambrianTech/codex/token-history-compar…
joelteply Nov 20, 2026
e74505c
Merge pull request #6182 from CambrianTech/codex/native-checkpoint-owner
joelteply Nov 20, 2026
bb5af61
Merge pull request #6181 from CambrianTech/codex/retain-prefix-checkp…
joelteply Nov 20, 2026
6553432
Correlate empty native turns with their requested tool catalog
joelteply Nov 20, 2026
82a5cad
Group native generation output in the empty-turn probe
joelteply Nov 20, 2026
ad2075c
Pin the deployed Qwen argument-order parser
joelteply Nov 20, 2026
30b78d9
Merge pull request #6185 from CambrianTech/codex/native-empty-turn-re…
joelteply Nov 20, 2026
1af6ffd
Retain one-off native state only while its turn owns it
joelteply Nov 20, 2026
2bbc969
Merge pull request #6186 from CambrianTech/codex/qwen-call-order-pin
joelteply Nov 20, 2026
3a59d95
Resolve retiring native state before recording suspend inventory
joelteply Nov 20, 2026
6404cf3
Report every resident held when suspend cannot preserve the node
joelteply Nov 20, 2026
4bcddd2
Merge pull request #6187 from CambrianTech/codex/one-off-owner-lifetime
joelteply Nov 20, 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
193 changes: 90 additions & 103 deletions core/continuum-engine/src/inference/backends/persona_scheduler/driver.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
use super::{reuse, Basis, Footprint, Held};

/// The tokens her blocks hold, and her checkpoint at her last view's end while what she holds still
/// runs through it.
#[derive(Default)]
pub(super) struct History {
pub(super) held: Vec<i32>,
/// View end, latest learned prefix, and earliest still-valid learned prefix.
kept: [Option<KeptCheckpoint>; 3],
}

/// Her checkpoint and what makes it valid: she held exactly `held[..end]` under `basis` when it was
/// taken, and nothing she holds below `end` has changed since (a cut below it drops it).
struct KeptCheckpoint {
blocks: llama::PersonaCheckpoint,
end: usize,
basis: Basis,
}

/// Her checkpoint holds her recurrent rows in host memory.
impl Footprint for KeptCheckpoint {
fn held(&self) -> Held {
Held::host(self.blocks.bytes() as u64)
}
}

/// Her token history and her checkpoint. Her blocks in the context are reported by the tier that
/// binds them; [`Bound`] is that child's view.
impl Footprint for History {
fn held(&self) -> Held {
self.kept
.iter()
.fold(self.held.held(), |bytes, kept| bytes + kept.held())
}
}

impl History {
/// Keep only her first `len` tokens; a cut below her checkpoint's end drops the checkpoint.
pub(super) fn cut(&mut self, len: usize) {
self.held.truncate(len);
for slot in &mut self.kept {
if slot.as_ref().is_some_and(|kept| kept.end > len) {
*slot = None;
}
}
}

/// Where `prompt` under `basis` would start for her, whose blocks were computed under `under`
/// (`reuse::choose`), deciding nothing and moving nothing: the bind uses it to resume her, the
/// frame to size her turn's prefill.
pub(super) fn reuse(&self, under: &Basis, prompt: &[i32], basis: &Basis) -> reuse::Decision {
let kept = self.kept.iter().flatten().map(|kept| reuse::Kept {
end: kept.end,
basis: &kept.basis,
});
reuse::choose(
reuse::History {
basis: under,
tokens: &self.held,
},
kept,
reuse::History {
basis,
tokens: prompt,
},
)
}
}

#[derive(Clone, Copy)]
pub(super) enum Boundary {
View,
Learned,
}

pub(super) struct Boundaries {
pub(super) view: Option<usize>,
pub(super) learned: Option<usize>,
}
impl Boundaries {
fn entries(&self) -> impl Iterator<Item = (Boundary, usize)> {
[
(Boundary::View, self.view),
(Boundary::Learned, self.learned),
]
.into_iter()
.filter_map(|(kind, end)| Some((kind, end?)))
}
pub(super) fn next_end(&self, pos: usize, prompt_end: usize) -> usize {
self.entries()
.map(|(_, end)| end)
.filter(|end| *end > pos)
.min()
.unwrap_or(prompt_end) // unwrap_or: the prompt ends the final frame
}
pub(super) fn at(&self, end: usize) -> Option<Boundary> {
self.entries()
.find_map(|(kind, pos)| (pos == end).then_some(kind))
}
}
impl From<Vec<i32>> for History {
fn from(held: Vec<i32>) -> Self {
Self {
held,
..Self::default()
}
}
}

impl History {
pub(super) fn deepest(&self) -> Option<usize> {
self.kept.iter().flatten().map(|s| s.end).max()
}
pub(super) fn take_at(&mut self, end: usize) -> Option<Snapshot> {
let (slot, entry) = self
.kept
.iter_mut()
.enumerate()
.find(|(_, entry)| entry.as_ref().is_some_and(|s| s.end == end))?;
Some(Snapshot {
slot,
checkpoint: entry.take()?,
})
}
pub(super) fn put_back(&mut self, snapshot: Snapshot) {
self.kept[snapshot.slot] = Some(snapshot.checkpoint);
}
fn slot(&mut self, kind: Boundary) -> &mut Option<KeptCheckpoint> {
let [view, learned, _] = &mut self.kept;
match kind {
Boundary::View => view,
Boundary::Learned => learned,
}
}
pub(super) fn capture_buffer(
&mut self,
kind: Boundary,
end: usize,
) -> Option<llama::PersonaCheckpoint> {
if let Boundary::Learned = kind {
let [_, latest, earliest] = &mut self.kept;
if latest.as_ref().is_some_and(|prior| {
prior.end < end && earliest.as_ref().is_none_or(|s| prior.end < s.end)
}) {
std::mem::swap(latest, earliest);
}
}
self.slot(kind)
.take()
.map(|s| s.blocks)
.or_else(llama::PersonaCheckpoint::new)
}
pub(super) fn save(
&mut self,
kind: Boundary,
blocks: llama::PersonaCheckpoint,
end: usize,
basis: &Basis,
) {
*self.slot(kind) = Some(KeptCheckpoint {
blocks,
end,
basis: basis.clone(),
});
}
}
pub(super) struct Snapshot {
slot: usize,
checkpoint: KeptCheckpoint,
}
impl Snapshot {
pub(super) fn blocks(&self) -> &llama::PersonaCheckpoint {
&self.checkpoint.blocks
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
//! engine while a mind with a short tail waits behind her (the M5, 2026-11-18, build 88074d9300:
//! turns served whole, back to back, waited 300-360 s behind one another's prefills).
//!
//! The rule, in order:
//! 1. the active mind's turn that is decoding continues: one mind decodes until she stops, and a
//! switch mid-answer would only add a switch;
//! 2. otherwise the turn with the least prefill left goes first, its remaining tokens discounted by
//! how long it has waited, so a short tail never waits behind a long prompt and a long prompt
//! still ages to the front;
//! 3. ties go to the active mind (no switch).
//!
//! The policy is this one pure function, so the owner of goal 9's scheduler replaces it here and
//! nowhere else.
//! Eligible turns share a one-second service budget. The bound mind advances until another
//! turn is due, then the oldest unserved turn gets the stream at the next landed frame.
// derived-or-floor: the conversational service requirement allows one second between eligible frames.
const SERVICE_BUDGET_MS: u64 = 1000;

pub(super) fn share_ms(ready: std::num::NonZeroUsize) -> u64 {
SERVICE_BUDGET_MS.div_ceil(ready.get() as u64)
}

/// A turn as the frame sees it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -23,7 +21,7 @@ pub(super) struct Candidate {
pub decoding: bool,
/// Prompt tokens still to prefill (0 once decoding).
pub remaining: usize,
/// Milliseconds since her turn was submitted.
/// Milliseconds since admission or her most recent completed frame.
pub waited_ms: u64,
}

Expand All @@ -36,8 +34,14 @@ impl Candidate {
}

/// The index of the turn the next frame advances, or `None` for no turns.
pub(super) fn pick(turns: &[Candidate]) -> Option<usize> {
if let Some(decoding) = turns.iter().position(|turn| turn.active && turn.decoding) {
pub(super) fn pick(turns: &[Candidate], bound_ms: u64) -> Option<usize> {
let share_ms = share_ms(std::num::NonZeroUsize::new(turns.len())?);
if let Some((index, other)) = turns.iter().enumerate().filter(|(_, t)| !t.active).max_by_key(|(_, t)| t.waited_ms) {
if other.waited_ms >= SERVICE_BUDGET_MS || (other.waited_ms >= share_ms && bound_ms >= share_ms) {
return Some(index);
}
}
if let Some(decoding) = turns.iter().position(|turn| turn.active && (turn.decoding || (bound_ms < share_ms && turns.iter().any(|other| other.decoding)))) {
return Some(decoding);
}
turns
Expand All @@ -60,18 +64,18 @@ mod tests {
// mind's answer in progress is never cut for a switch; a long prompt still reaches the front by
// waiting; equal costs keep the mind already bound.
#[test]
fn a_short_tail_never_waits_behind_a_long_prompt_and_an_answer_is_never_cut() {
assert_eq!(pick(&[]), None);
fn a_short_tail_never_waits_behind_a_long_prompt_and_an_overdue_turn_can_preempt_decode() {
assert_eq!(pick(&[], 0), None);
let long_active = turn(true, false, 60_000, 0);
let short = turn(false, false, 300, 0);
assert_eq!(pick(&[long_active, short]), Some(1), "the short tail switches in at the chunk boundary");
assert_eq!(pick(&[long_active, short], 0), Some(1), "the short tail switches in at the chunk boundary");

let answering = turn(true, true, 0, 0);
assert_eq!(pick(&[turn(false, false, 1, 90_000), answering]), Some(1), "her answer finishes first");
assert_eq!(pick(&[turn(false, false, 1, 90_000), answering], 500), Some(0), "an overdue turn interrupts an answer");

let aged = turn(false, false, 60_000, 600_000);
assert_eq!(pick(&[turn(false, false, 1_000, 0), aged]), Some(1), "ten minutes of waiting brings a long prompt forward");
assert_eq!(pick(&[turn(false, false, 1_000, 0), aged], 0), Some(1), "ten minutes of waiting brings a long prompt forward");

assert_eq!(pick(&[turn(false, false, 500, 0), turn(true, false, 500, 0)]), Some(1), "a tie keeps the bound mind");
assert_eq!(pick(&[turn(false, false, 500, 0), turn(true, false, 500, 0)], 0), Some(1), "a tie keeps the bound mind");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub(super) struct TurnRow {
pub learned_boundary: Option<u64>,
pub checkpoint_ms: u64,
pub wait_ms: Option<u64>,
pub longest_service_gap_ms: u64,
pub switches: u64,
pub switch_ms: u64,
pub prefill_ms: u64,
Expand All @@ -83,6 +84,7 @@ impl TurnRow {
learned_boundary = ?self.learned_boundary,
checkpoint_ms = self.checkpoint_ms,
wait_ms = ?self.wait_ms,
longest_service_gap_ms = self.longest_service_gap_ms,
switches = self.switches,
switch_ms = self.switch_ms,
prefill_ms = self.prefill_ms,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ impl Miss {
}

/// The tokens two sequences share from their start.
pub(super) fn common_prefix(a: &[i32], b: &[i32]) -> usize {
a.iter().zip(b).take_while(|(x, y)| x == y).count()
}
pub(super) use continuum_persona_engine::turn::common_prefix;

/// One of her checkpoints as reuse sees it: the tokens it holds through (exclusive end) and its basis.
#[derive(Clone, Copy)]
Expand All @@ -69,28 +67,33 @@ pub(super) struct Kept<'a, B> {
/// the furthest one both histories hold unchanged under the same genes. Her head's checkpoint
/// serves a turnover, which changes her view but not her head (the M5, 2026-11-19, 48a1dc80cd: 17
/// of 30 turns re-prefilled from 0 behind an unchanged head, ~180k of 422,847 tokens).
pub(super) fn choose<'a, B: PartialEq + 'a>(held: History<'_, B>, kept: impl IntoIterator<Item = Kept<'a, B>>, next: History<'_, B>) -> Reuse {
let (held_tokens, prompt, basis) = (held.tokens, next.tokens, next.basis);
// The prompt extends her whole held history (her own generated tokens included): append.
if let continuum_persona_engine::turn::Prefill::Append { from } = continuum_persona_engine::turn::plan(held, next) {
return Reuse::Append { from };
pub(super) struct Decision {
pub shared: usize,
pub action: Reuse,
}

pub(super) fn choose<'a, B: PartialEq + 'a>(held: History<'_, B>, kept: impl IntoIterator<Item = Kept<'a, B>>, next: History<'_, B>) -> Decision {
let comparison = held.compare(&next);
let shared = comparison.shared;
let decide = |action| Decision { shared, action };
let (held, prompt, basis) = (held.tokens, next.tokens, next.basis);
if let continuum_persona_engine::turn::Prefill::Append { from } = comparison.prefill {
return decide(Reuse::Append { from });
}
let held = held_tokens;
let mut kept = kept.into_iter().peekable();
if kept.peek().is_none() {
return Reuse::Restart(if held.is_empty() { Miss::Cold } else { Miss::NoCheckpoint });
return decide(Reuse::Restart(if held.is_empty() { Miss::Cold } else { Miss::NoCheckpoint }));
}
let mut same_genes = kept.filter(|kept| kept.basis == basis).peekable();
if same_genes.peek().is_none() {
return Reuse::Restart(Miss::Basis);
return decide(Reuse::Restart(Miss::Basis));
}
// A checkpoint's end must be held unchanged by both, and leave a token to sample from.
let shared = common_prefix(held, prompt);
same_genes
decide(same_genes
.map(|kept| kept.end)
.filter(|end| *end > 0 && *end < prompt.len() && *end <= shared)
.max()
.map_or(Reuse::Restart(Miss::Diverged), |from| Reuse::Rewind { from })
.map_or(Reuse::Restart(Miss::Diverged), |from| Reuse::Rewind { from }))
}

#[cfg(test)]
Expand All @@ -113,20 +116,20 @@ mod tests {
let mut next: Vec<i32> = (0..80).collect();
next.extend([900, 901, 902]); // a new trailer and her call, rendered differently
let kept = [Kept { end: 80, basis: &genes }];
assert_eq!(choose(h(&held, &genes), kept, h(&next, &genes)), Reuse::Rewind { from: 80 });
assert_eq!(choose(h(&held, &genes), kept, h(&next, &genes)).action, Reuse::Rewind { from: 80 });

let mut early: Vec<i32> = (0..40).collect();
early.push(777); // her view itself changed (a new epoch): before her checkpoint
early.extend(41..90);
assert_eq!(choose(h(&held, &genes), kept, h(&early, &genes)), Reuse::Restart(Miss::Diverged));
assert_eq!(choose(h(&held, &genes), kept, h(&early, &genes)).action, Reuse::Restart(Miss::Diverged));

assert_eq!(choose(h(&held, &genes), kept, h(&next, &"tuned")), Reuse::Restart(Miss::Basis));
assert_eq!(choose(h(&held, &genes), [], h(&next, &genes)), Reuse::Restart(Miss::NoCheckpoint));
assert_eq!(choose(h(&[], &genes), [], h(&next, &genes)), Reuse::Restart(Miss::Cold));
assert_eq!(choose(h(&held, &genes), kept, h(&next, &"tuned")).action, Reuse::Restart(Miss::Basis));
assert_eq!(choose(h(&held, &genes), [], h(&next, &genes)).action, Reuse::Restart(Miss::NoCheckpoint));
assert_eq!(choose(h(&[], &genes), [], h(&next, &genes)).action, Reuse::Restart(Miss::Cold));

let mut extends = held.clone();
extends.extend([5, 6]);
assert_eq!(choose(h(&held, &genes), kept, h(&extends, &genes)), Reuse::Append { from: 100 });
assert_eq!(choose(h(&held, &genes), kept, h(&extends, &genes)).action, Reuse::Append { from: 100 });
}

// what this catches (the M5, 2026-11-19, 48a1dc80cd: 17 of 30 turns re-prefilled from 0 behind
Expand All @@ -140,17 +143,17 @@ mod tests {
let kept = [Kept { end: 80, basis: &genes }, Kept { end: 20, basis: &genes }];
let mut turned: Vec<i32> = (0..20).collect();
turned.extend(500..560); // a new epoch: her view re-rendered behind the same head
assert_eq!(choose(h(&held, &genes), kept, h(&turned, &genes)), Reuse::Rewind { from: 20 });
assert_eq!(choose(h(&held, &genes), kept, h(&turned, &genes)).action, Reuse::Rewind { from: 20 });

let mut trailer: Vec<i32> = (0..80).collect();
trailer.push(900);
assert_eq!(choose(h(&held, &genes), kept, h(&trailer, &genes)), Reuse::Rewind { from: 80 });
assert_eq!(choose(h(&held, &genes), kept, h(&trailer, &genes)).action, Reuse::Rewind { from: 80 });

let mut head_changed: Vec<i32> = vec![777];
head_changed.extend(1..90);
assert_eq!(choose(h(&held, &genes), kept, h(&head_changed, &genes)), Reuse::Restart(Miss::Diverged));
assert_eq!(choose(h(&held, &genes), kept, h(&head_changed, &genes)).action, Reuse::Restart(Miss::Diverged));

let mixed = [Kept { end: 80, basis: &"tuned" }, Kept { end: 20, basis: &genes }];
assert_eq!(choose(h(&held, &genes), mixed, h(&trailer, &genes)), Reuse::Rewind { from: 20 }, "only her checkpoints under these genes count");
assert_eq!(choose(h(&held, &genes), mixed, h(&trailer, &genes)).action, Reuse::Rewind { from: 20 }, "only her checkpoints under these genes count");
}
}
Loading
Loading