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
54 changes: 46 additions & 8 deletions core/continuum-cognition/src/cognition/working_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ pub struct PersonaDemand {
/// Next slot to overwrite in `need_recent`.
#[serde(default)]
pub need_next: u8,
/// The last [`RECENT_TURNS`] growth samples (what `sent_peak` takes the max of, over a
/// lifetime): the lane floor reads their max, so one historic giant turn stops sizing
/// every plan forever. On the M5, 2026-10-28, all-time `sent_peak`s of 188-370k
/// (recent turns 20-43k) set a 423,945-token requirement no plan could meet, every
/// runnable plan was refused as below the floor, and four residents shared one
/// 27,904-token slot. Legacy files read empty.
#[serde(default)]
pub sent_recent: [u32; RECENT_TURNS],
/// Next slot to overwrite in `sent_recent`.
#[serde(default)]
pub sent_next: u8,
}

/// How many recent turns define "typical". Sixteen: a working session, not a
Expand All @@ -169,6 +180,27 @@ fn push_need(d: &mut PersonaDemand, need_tokens: u32) {
d.need_next = ((i + 1) % RECENT_TURNS) as u8;
}

/// Push one growth sample into her sent ring (newest overwrites oldest).
fn push_sent(d: &mut PersonaDemand, sent_tokens: u32) {
let i = (d.sent_next as usize) % RECENT_TURNS;
d.sent_recent[i] = sent_tokens;
d.sent_next = ((i + 1) % RECENT_TURNS) as u8;
}

/// Her growth sample for the lane floor: the largest of her RECENT growth samples, never a
/// lifetime peak. A mind that never sent a growth sample has none, so she sets no floor. A
/// record written before the ring existed holds only its lifetime peak: it stands on her
/// typical demand, the same rule [`requirement_tokens`] states, else on that peak.
pub fn growth_sample(d: &PersonaDemand) -> Option<u32> {
if let Some(recent) = d.sent_recent.iter().copied().max().filter(|&t| t > 0) {
return Some(recent);
}
if d.sent_peak == 0 {
return None;
}
typical_tokens(d).or(Some(d.sent_peak))
}

fn median_of(ring: &[u32; RECENT_TURNS]) -> Option<u32> {
let mut v: Vec<u32> = ring.iter().copied().filter(|t| *t > 0).collect();
if v.is_empty() {
Expand Down Expand Up @@ -490,8 +522,15 @@ impl WorkingSetRegistry {
*self
.observed
.entry(persona)
.and_modify(|d| d.sent_peak = d.sent_peak.max(sent_tokens))
.or_insert(PersonaDemand { last_seen_ms: now_ms, sent_peak: sent_tokens, ..PersonaDemand::default() })
.and_modify(|d| {
d.sent_peak = d.sent_peak.max(sent_tokens);
push_sent(d, sent_tokens);
})
.or_insert_with(|| {
let mut d = PersonaDemand { last_seen_ms: now_ms, sent_peak: sent_tokens, ..PersonaDemand::default() };
push_sent(&mut d, sent_tokens);
d
})
}

/// Record the post-fit size of a turn the fit HELD — her need (see
Expand Down Expand Up @@ -526,8 +565,8 @@ impl WorkingSetRegistry {
.filter(|&t| t > 0)
}

/// Typical growth sample among `personas` — the median of their retained
/// starvation-aware peaks. The planner's per-lane floor follows this, not the ceiling: one mind
/// Typical growth sample among `personas` — the median of their RECENT starvation-aware
/// peaks ([`growth_sample`], never a lifetime max). The planner's per-lane floor follows this, not the ceiling: one mind
/// with a 96k prompt must not size every lane to 120k and starve the roster
/// (2026-09-14: 16 residents on 3 lanes). The outlier is reconciled down to the
/// served window instead.
Expand All @@ -543,7 +582,7 @@ impl WorkingSetRegistry {
pub fn sent_median_with(&self, personas: &[Uuid], extra: &[u32]) -> Option<u32> {
let mut peaks: Vec<u32> = personas
.iter()
.filter_map(|p| self.observed.get(p).map(|e| e.value().sent_peak))
.filter_map(|p| self.observed.get(p).and_then(|e| growth_sample(e.value())))
.chain(extra.iter().copied())
.filter(|&t| t > 0)
.collect();
Expand All @@ -553,13 +592,12 @@ impl WorkingSetRegistry {
peaks.sort_unstable();
Some(peaks[peaks.len() / 2])
}
/// Largest retained growth sample among `personas`, including unmet demand.
/// Largest recent growth sample among `personas`, including unmet demand ([`growth_sample`]).
pub fn sent_ceiling_of(&self, personas: &[Uuid]) -> Option<u32> {
personas
.iter()
.filter_map(|p| self.observed.get(p).map(|e| e.value().sent_peak))
.filter_map(|p| self.observed.get(p).and_then(|e| growth_sample(e.value())))
.max()
.filter(|&t| t > 0)
}

/// Record one completed generation's measured output for `persona`.
Expand Down
27 changes: 26 additions & 1 deletion core/continuum-cognition/src/cognition/working_set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
assert_eq!(d.peak_tokens, 176_154);
assert_eq!(typical_tokens(&d), Some(45_000), "the median of recent turns");
assert_eq!(requirement_tokens(&d), 45_000);
let legacy = PersonaDemand { peak_tokens: 176_154, last_tokens: 29_911, last_seen_ms: 0, prev_seen_ms: 0, turns: 5, sent_peak: 0, recent: [0; RECENT_TURNS], recent_next: 0, need_recent: [0; RECENT_TURNS], need_next: 0 };
let legacy = PersonaDemand { peak_tokens: 176_154, last_tokens: 29_911, last_seen_ms: 0, prev_seen_ms: 0, turns: 5, sent_peak: 0, recent: [0; RECENT_TURNS], recent_next: 0, need_recent: [0; RECENT_TURNS], need_next: 0, sent_recent: [0; RECENT_TURNS], sent_next: 0 };
assert_eq!(requirement_tokens(&legacy), 29_911, "no ring: the last turn, never the peak");
let ser: PersonaDemand = serde_json::from_str(r#"{"peak_tokens":1,"last_tokens":2,"last_seen_ms":3,"turns":4}"#).expect("legacy json");
assert_eq!(typical_tokens(&ser), None);
Expand Down Expand Up @@ -468,3 +468,28 @@
assert!(a2 < start2, "the control: verbatim samples contract ({start2} → {a2})");
}

// what this catches: the lane floor sized by a lifetime peak (the M5, 2026-10-28: sent_peaks
// of 188-370k against recent turns of 20-43k set a 423,945-token requirement, every runnable
// plan was refused, and four residents shared one 27,904-token slot). The floor follows her
// recent growth samples; a legacy record with only a peak stands on her typical demand.
#[test]
fn a_historic_giant_turn_stops_setting_the_lane_floor() {
let reg = WorkingSetRegistry::new();
let (aris, benchy) = (Uuid::new_v4(), Uuid::new_v4());
reg.record_sent_in_memory(aris, 370_493, 1);
reg.record_sent_in_memory(benchy, 339_156, 1);
for turn in 0..RECENT_TURNS as u32 {
reg.record_sent_in_memory(aris, 21_000 + turn, 2);
reg.record_sent_in_memory(benchy, 24_000 + turn, 2);
}
let floor = reg.sent_median_of(&[aris, benchy]).expect("two residents");
assert!(floor < 30_000, "the floor follows recent turns, not the 339k-370k peaks: {floor}");
assert_eq!(reg.sent_ceiling_of(&[aris, benchy]), Some(24_000 + RECENT_TURNS as u32 - 1));
let legacy = PersonaDemand { sent_peak: 339_156, recent: [35_000; RECENT_TURNS], ..PersonaDemand::default() };
assert_eq!(growth_sample(&legacy), Some(35_000), "a record from before the ring stands on her typical turn");
assert_eq!(growth_sample(&PersonaDemand { sent_peak: 9_000, ..PersonaDemand::default() }), Some(9_000));
// A mind that never sent a growth sample sets no floor, however large her typical turn:
// the re-home tests' plans moved when such a record contributed one (#5579 CI).
let never_sent = PersonaDemand { recent: [35_000; RECENT_TURNS], ..PersonaDemand::default() };
assert_eq!(growth_sample(&never_sent), None);
}
Loading