Skip to content
Closed
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
48 changes: 41 additions & 7 deletions core/continuum-cognition/src/cognition/working_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ pub struct PersonaDemand {
/// Legacy files without it read 0.
#[serde(default)]
pub sent_peak: u32,
/// Recent starvation-aware growth samples; the lifetime peak is diagnostic only.
#[serde(default)]
pub growth_recent: [u32; RECENT_TURNS],
#[serde(default)]
pub growth_next: u8,
/// The last [`RECENT_TURNS`] untrimmed demands, newest overwriting oldest — what
/// her turns TYPICALLY assemble, as opposed to `peak_tokens` (once, ever). The
/// lane REQUIREMENT is the median of these with headroom (`typical_tokens`):
Expand Down Expand Up @@ -169,6 +174,31 @@ fn push_need(d: &mut PersonaDemand, need_tokens: u32) {
d.need_next = ((i + 1) % RECENT_TURNS) as u8;
}

fn push_growth(d: &mut PersonaDemand, tokens: u32) {
if tokens == 0 {
return;
}
d.sent_peak = d.sent_peak.max(tokens);
let i = (d.growth_next as usize) % RECENT_TURNS;
d.growth_recent[i] = tokens;
d.growth_next = ((i + 1) % RECENT_TURNS) as u8;
}

/// Migration uses recent untrimmed demand until growth samples arrive. Old files
/// with no ring use their last demand; sent-only legacy records have only sent_peak.
/// No historical growth sample means no growth vote; assembly alone is not one.
/// Never substitute post-fit need: a starved window must still request growth.
fn growth_tokens(d: &PersonaDemand) -> u32 {
if let Some(tokens) = median_of(&d.growth_recent) {
return tokens;
}
if d.sent_peak == 0 {
return 0;
}
let demand = requirement_tokens(d);
if demand > 0 { demand } else { 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 +520,12 @@ 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| push_growth(d, sent_tokens))
.or_insert_with(|| {
let mut d = PersonaDemand { last_seen_ms: now_ms, ..PersonaDemand::default() };
push_growth(&mut d, sent_tokens);
d
})
}

/// Record the post-fit size of a turn the fit HELD — her need (see
Expand Down Expand Up @@ -527,14 +561,14 @@ impl WorkingSetRegistry {
}

/// 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
/// recent starvation-aware samples. 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.
pub fn sent_median_of(&self, personas: &[Uuid]) -> Option<u32> {
self.sent_median_with(personas, &[])
}
/// Growth demand over the residents' peaks PLUS `extra` requested samples —
/// Growth demand over the residents' recent medians PLUS `extra` requested samples —
/// including refused prompts from minds hosted on other nodes
/// ([`crate::inference::resource_admission::leased_in_sent_samples`]). ONE median for
/// the seat's whole demand pool: a seat that plans lanes for leased-in minds must size
Expand All @@ -543,7 +577,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).map(|e| growth_tokens(e.value())))
.chain(extra.iter().copied())
.filter(|&t| t > 0)
.collect();
Expand All @@ -553,11 +587,11 @@ impl WorkingSetRegistry {
peaks.sort_unstable();
Some(peaks[peaks.len() / 2])
}
/// Largest retained growth sample among `personas`, including unmet demand.
/// Largest current growth requirement among `personas`, including unmet demand.
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).map(|e| growth_tokens(e.value())))
.max()
.filter(|&t| t > 0)
}
Expand Down
63 changes: 62 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, growth_recent: [0; RECENT_TURNS], growth_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,64 @@
assert!(a2 < start2, "the control: verbatim samples contract ({start2} → {a2})");
}



#[test]
fn persisted_m5_peaks_do_not_pin_the_publication_floor() -> Result<(), Box<dyn std::error::Error>> {
let reg = WorkingSetRegistry::new();
let residents = [p(201), p(202), p(203), p(204)];
for (id, (peak, recent)) in residents.iter().zip([
(370_493, 24_064), (339_156, 43_129), (294_317, 31_355), (209_252, 35_500),
]) {
let mut legacy = serde_json::json!({
"peak_tokens": peak, "last_tokens": recent, "last_seen_ms": 1,
"turns": 100, "sent_peak": peak,
});
legacy["recent"] = serde_json::to_value(vec![recent; RECENT_TURNS])?;
let demand: PersonaDemand = serde_json::from_value(legacy)?;
let restored = serde_json::from_str(&serde_json::to_string(&demand)?)?;
reg.observed.insert(*id, restored);
}
assert_eq!(reg.sent_median_of(&residents), Some(35_500));
assert_eq!(reg.sent_ceiling_of(&residents), Some(43_129));
assert_eq!(reg.ceiling_of(&residents), Some(370_493), "retain historical diagnostics");
Ok(())
}

#[test]
fn assembly_without_a_growth_observation_does_not_create_a_publication_floor() {
let reg = WorkingSetRegistry::new();
let mind = p(206);
reg.record_in_memory(mind, 200_000, 1);
assert_eq!(reg.sent_median_of(&[mind]), None);
assert_eq!(reg.sent_ceiling_of(&[mind]), None);
assert_eq!(reg.ceiling_of(&[mind]), Some(200_000));
reg.record_sent_in_memory(mind, 30_000, 2);
assert_eq!(reg.sent_median_of(&[mind]), Some(30_000));
}

#[test]
fn growth_history_ages_out_peaks_but_never_learns_a_starved_windows_echo()
-> Result<(), Box<dyn std::error::Error>>
{
let reg = WorkingSetRegistry::new();
let mind = p(205);
reg.record_sent_in_memory(mind, 339_156, 1);
for turn in 0..RECENT_TURNS {
let sample = WorkingSetRegistry::sent_sample_for_plan(56_057, 828, 2_048);
reg.record_sent_in_memory(mind, sample, turn as u64 + 2);
}
assert_eq!(reg.sent_median_of(&[mind]), Some(56_057));
let Some(demand) = reg.demand_of(mind) else { return Err("missing demand".into()); };
assert_eq!(demand.sent_peak, 339_156);
let restarted = WorkingSetRegistry::new();
restarted.observed.insert(mind, serde_json::from_str(&serde_json::to_string(&demand)?)?);
assert_eq!(restarted.sent_median_of(&[mind]), Some(56_057));
for turn in 0..RECENT_TURNS {
let held = WorkingSetRegistry::sent_sample_for_plan(30_000, 25_000, 64_000);
restarted.record_sent_in_memory(mind, held, turn as u64 + 20);
}
assert_eq!(restarted.sent_median_of(&[mind]), Some(25_000));
assert_eq!(restarted.sent_median_with(&[mind], &[60_000, 62_000]), Some(60_000));
Ok(())
}
Loading