From 913ca52ac97d9be1cc061df4297ddbb629dc233e Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 2 Nov 2026 17:21:52 -0600 Subject: [PATCH 1/6] =?UTF-8?q?The=20window=20validator=20(=C2=A76e):=20a?= =?UTF-8?q?=20scenario=20of=20each=20mind's=20demand=20events=20over=20one?= =?UTF-8?q?=20shared=20pool,=20played=20through=20a=20WindowPolicy=20again?= =?UTF-8?q?st=20the=20contract=20(never=20over=20the=20pool,=20every=20dem?= =?UTF-8?q?and=20granted=20when=20all=20fit,=20no=20in-flight=20shrink,=20?= =?UTF-8?q?squeezes=20counted)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Outliers prove the interface, the capacity simulator's shape: today's equal split (one window per slot, residents plus a scratch slot) squeezes a reading mind on the M5's own 2026-11-02 shape (Aiko reads ~70-76k, granted 58,112 while Atlas idles), and a demand-following policy keeps the contract. Needs no engine change; the allocator's uneven-grant work (Codex) is what makes the second outlier real. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- core/continuum-engine/src/capacity/mod.rs | 1 + .../src/capacity/window_sim.rs | 207 ++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 core/continuum-engine/src/capacity/window_sim.rs diff --git a/core/continuum-engine/src/capacity/mod.rs b/core/continuum-engine/src/capacity/mod.rs index bda881af1c..6bfb7b17e3 100644 --- a/core/continuum-engine/src/capacity/mod.rs +++ b/core/continuum-engine/src/capacity/mod.rs @@ -56,6 +56,7 @@ pub mod score; pub mod serving_pager; pub mod settlement; pub mod sim; +pub mod window_sim; pub mod system_profile; #[cfg(any(test, feature = "test-fixtures"))] pub mod profile_fixtures; diff --git a/core/continuum-engine/src/capacity/window_sim.rs b/core/continuum-engine/src/capacity/window_sim.rs new file mode 100644 index 0000000000..42c853770a --- /dev/null +++ b/core/continuum-engine/src/capacity/window_sim.rs @@ -0,0 +1,207 @@ +//! The window validator (FLOW-IMPLEMENTATION-PLAN ยง6e): a mind's window follows what she reads. +//! +//! A scenario is DATA: a timeline of each mind's demand events (her untrimmed required tokens +//! plus her completion reserve, published when it moves) over one shared KV pool. A +//! [`WindowPolicy`] grants every mind a window from the pool; [`run`] plays the timeline through +//! it and checks the contract the policy must keep: +//! +//! - the grants never exceed the pool; +//! - when every demand fits the pool, every mind is granted her demand; +//! - a grant held by an in-flight turn never shrinks under it; +//! - a SQUEEZE (a turn granted less than she demanded while the pool had room) is counted: it +//! is what made the residents re-read what fell out of view (2026-11-02: 54-92% re-reads). +//! +//! The outliers prove the interface, the same shape as [`super::sim`]: the equal split the +//! engine runs today (one window per slot, a scratch slot beside the residents) must squeeze a +//! reading mind while others idle; a demand-following policy must not. Needs no engine change. + +use std::collections::BTreeMap; + +/// A mind on the pool (a resident, or the scratch traffic beside them). +pub type MindId = u32; + +/// One mind's demand changed: what her next turn needs, and whether a turn is in flight now +/// (a grant held by an in-flight turn may not shrink under it). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct DemandEvent { + pub at_ms: u64, + pub mind: MindId, + pub required_tokens: u32, + pub in_flight: bool, +} + +/// A timeline of demand events over one pool. +#[derive(Debug, Clone)] +pub struct WindowScenario { + pub name: &'static str, + pub pool_tokens: u32, + pub events: Vec, +} + +/// The current demand of every mind on the pool, as a policy reads it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Demand { + pub mind: MindId, + pub required_tokens: u32, + pub in_flight: bool, +} + +/// Grants every mind a window from the pool, given every mind's current demand and the grants +/// they hold now. +pub trait WindowPolicy { + fn grant(&mut self, demands: &[Demand], held: &BTreeMap, pool_tokens: u32) -> BTreeMap; +} + +/// What a run measured, and every contract breach it saw. +#[derive(Debug, Default, Clone, PartialEq, Eq)] +pub struct WindowRun { + pub squeezes: u32, + pub over_pool: u32, + pub in_flight_shrinks: u32, + pub unmet_when_fits: u32, +} + +impl WindowRun { + /// The policy kept the contract and squeezed no one the pool had room for. + pub fn kept(&self) -> bool { + self == &Self::default() + } +} + +/// Play `scenario` through `policy`, one regrant per demand event. +pub fn run(scenario: &WindowScenario, policy: &mut dyn WindowPolicy) -> WindowRun { + let mut demands: BTreeMap = BTreeMap::new(); + let mut held: BTreeMap = BTreeMap::new(); + let mut out = WindowRun::default(); + for event in &scenario.events { + demands.insert(event.mind, Demand { mind: event.mind, required_tokens: event.required_tokens, in_flight: event.in_flight }); + let current: Vec = demands.values().copied().collect(); + let grants = policy.grant(¤t, &held, scenario.pool_tokens); + let total: u64 = grants.values().map(|&g| u64::from(g)).sum(); + if total > u64::from(scenario.pool_tokens) { + out.over_pool += 1; + } + let demanded: u64 = current.iter().map(|d| u64::from(d.required_tokens)).sum(); + for demand in ¤t { + let granted = grants.get(&demand.mind).copied().unwrap_or(0); // unwrap_or: a mind the policy granted nothing holds nothing + if demand.in_flight && held.get(&demand.mind).is_some_and(|&before| granted < before) { + out.in_flight_shrinks += 1; + } + if granted < demand.required_tokens { + if demanded <= u64::from(scenario.pool_tokens) { + out.unmet_when_fits += 1; + } + if total < u64::from(scenario.pool_tokens) { + out.squeezes += 1; + } + } + } + held = grants; + } + out +} + +/// TODAY'S ENGINE: one equal window per slot, the slots being the residents plus one scratch +/// slot (`demand.rs` "+1 SCRATCH LANE"): every mind gets `pool / slots`, whatever she reads. +pub struct EqualSplit { + pub slots: u32, +} + +impl WindowPolicy for EqualSplit { + fn grant(&mut self, demands: &[Demand], _held: &BTreeMap, pool_tokens: u32) -> BTreeMap { + let each = pool_tokens / self.slots.max(1); + demands.iter().map(|d| (d.mind, each)).collect() + } +} + +/// Demand-following over one shared pool: every mind is granted her demand when the pool holds +/// them all; otherwise in-flight grants are kept whole and the rest share what remains in +/// proportion to their demand. +pub struct DemandFollowing; + +impl WindowPolicy for DemandFollowing { + fn grant(&mut self, demands: &[Demand], held: &BTreeMap, pool_tokens: u32) -> BTreeMap { + let demanded: u64 = demands.iter().map(|d| u64::from(d.required_tokens)).sum(); + if demanded <= u64::from(pool_tokens) { + return demands.iter().map(|d| (d.mind, d.required_tokens)).collect(); + } + // An in-flight turn keeps what it holds: a grant never shrinks under a turn using it. + let kept: BTreeMap = demands + .iter() + .filter(|d| d.in_flight) + .filter_map(|d| held.get(&d.mind).map(|&h| (d.mind, h))) + .collect(); + let kept_total: u64 = kept.values().map(|&g| u64::from(g)).sum(); + let rest = u64::from(pool_tokens).saturating_sub(kept_total); + let rest_demand: u64 = demands.iter().filter(|d| !kept.contains_key(&d.mind)).map(|d| u64::from(d.required_tokens)).sum(); + let mut grants = kept; + for d in demands.iter().filter(|d| !grants.contains_key(&d.mind)).copied().collect::>() { + let share = if rest_demand == 0 { 0 } else { u64::from(d.required_tokens) * rest / rest_demand }; + grants.insert(d.mind, u32::try_from(share).unwrap_or(u32::MAX).min(d.required_tokens)); // unwrap_or: a share past u32 is capped by her demand anyway + } + grants + } +} + +/// The M5 on 2026-11-02: 290,560 tokens of KV pool; Aiko reading large files (instantiate.rs +/// and skeleton.rs, ~70k of view), Benchy and Aris mid-work, Atlas quiet, and scratch traffic +/// (a 432-token prompt) beside them. +pub fn m5_uneven_reading() -> WindowScenario { + const AIKO: MindId = 1; + const ARIS: MindId = 2; + const ATLAS: MindId = 3; + const BENCHY: MindId = 4; + const SCRATCH: MindId = 5; + let e = |at_ms, mind, required_tokens, in_flight| DemandEvent { at_ms, mind, required_tokens, in_flight }; + WindowScenario { + name: "m5-uneven-reading", + pool_tokens: 290_560, + events: vec![ + e(0, ATLAS, 12_000, false), + e(1, ARIS, 40_000, true), + e(2, BENCHY, 52_000, true), + e(3, SCRATCH, 1_000, true), + e(4, AIKO, 70_000, true), + e(5, AIKO, 76_000, true), + e(6, ATLAS, 9_000, false), + e(7, BENCHY, 58_000, true), + ], + } +} + +#[cfg(test)] +mod tests { + use super::*; + + // what this catches: the equal split the engine runs today passing as adequate. On the M5's + // own shape it grants Aiko 58,112 of the 70-76k she reads while Atlas idles at 12k of his + // 58k: squeezed with the pool half empty, the re-read loop. + #[test] + fn todays_equal_split_squeezes_a_reading_mind_while_others_idle() { + let run = run(&m5_uneven_reading(), &mut EqualSplit { slots: 5 }); + assert!(run.squeezes > 0, "{run:?}"); + assert!(run.unmet_when_fits > 0, "every demand fit the pool and one was cut: {run:?}"); + } + + // what this catches: a demand-following grant breaking the contract: over the pool, an + // in-flight shrink, or a mind cut while the pool holds every demand. + #[test] + fn demand_following_grants_every_mind_what_she_reads_within_the_pool() { + let run = run(&m5_uneven_reading(), &mut DemandFollowing); + assert!(run.kept(), "{run:?}"); + } + + // what this catches: an overloaded pool shrinking a turn in flight to seat another. + #[test] + fn an_overloaded_pool_never_shrinks_a_turn_in_flight() { + let e = |at_ms, mind, required_tokens, in_flight| DemandEvent { at_ms, mind, required_tokens, in_flight }; + let scenario = WindowScenario { + name: "overloaded", + pool_tokens: 100_000, + events: vec![e(0, 1, 60_000, true), e(1, 2, 60_000, false), e(2, 3, 30_000, false)], + }; + let run = run(&scenario, &mut DemandFollowing); + assert_eq!(run.in_flight_shrinks, 0, "{run:?}"); + assert_eq!(run.over_pool, 0, "{run:?}"); + } +} From 4b2c6d8705754b3f3022df5c5934e70ef1f182c9 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 2 Nov 2026 18:25:58 -0600 Subject: [PATCH 2/6] window_sim: a squeeze is a cut while another mind holds grant past her demand (the equal split hands out the whole pool, so 'pool had room' never fired) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- core/continuum-engine/src/capacity/window_sim.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/continuum-engine/src/capacity/window_sim.rs b/core/continuum-engine/src/capacity/window_sim.rs index 42c853770a..74526468dd 100644 --- a/core/continuum-engine/src/capacity/window_sim.rs +++ b/core/continuum-engine/src/capacity/window_sim.rs @@ -8,7 +8,7 @@ //! - the grants never exceed the pool; //! - when every demand fits the pool, every mind is granted her demand; //! - a grant held by an in-flight turn never shrinks under it; -//! - a SQUEEZE (a turn granted less than she demanded while the pool had room) is counted: it +//! - a SQUEEZE (a turn granted less than she demanded while another mind is granted past her own demand) is counted: it //! is what made the residents re-read what fell out of view (2026-11-02: 54-92% re-reads). //! //! The outliers prove the interface, the same shape as [`super::sim`]: the equal split the @@ -82,6 +82,11 @@ pub fn run(scenario: &WindowScenario, policy: &mut dyn WindowPolicy) -> WindowRu out.over_pool += 1; } let demanded: u64 = current.iter().map(|d| u64::from(d.required_tokens)).sum(); + // Granted past demand: the room an idle mind holds that a reading mind could have used. + let slack: u64 = current + .iter() + .map(|d| u64::from(grants.get(&d.mind).copied().unwrap_or(0)).saturating_sub(u64::from(d.required_tokens))) // unwrap_or: a mind granted nothing holds no slack + .sum(); for demand in ¤t { let granted = grants.get(&demand.mind).copied().unwrap_or(0); // unwrap_or: a mind the policy granted nothing holds nothing if demand.in_flight && held.get(&demand.mind).is_some_and(|&before| granted < before) { @@ -91,7 +96,7 @@ pub fn run(scenario: &WindowScenario, policy: &mut dyn WindowPolicy) -> WindowRu if demanded <= u64::from(scenario.pool_tokens) { out.unmet_when_fits += 1; } - if total < u64::from(scenario.pool_tokens) { + if slack > 0 { out.squeezes += 1; } } From b122fc0c0be79baf2e034ccf53772858a9dd8ce5 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 2 Nov 2026 18:26:25 -0600 Subject: [PATCH 3/6] window_sim: checked_div for the proportional share; rustfmt Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- .../src/capacity/window_sim.rs | 95 ++++++++++++++++--- 1 file changed, 80 insertions(+), 15 deletions(-) diff --git a/core/continuum-engine/src/capacity/window_sim.rs b/core/continuum-engine/src/capacity/window_sim.rs index 74526468dd..9e6937f8ef 100644 --- a/core/continuum-engine/src/capacity/window_sim.rs +++ b/core/continuum-engine/src/capacity/window_sim.rs @@ -49,7 +49,12 @@ pub struct Demand { /// Grants every mind a window from the pool, given every mind's current demand and the grants /// they hold now. pub trait WindowPolicy { - fn grant(&mut self, demands: &[Demand], held: &BTreeMap, pool_tokens: u32) -> BTreeMap; + fn grant( + &mut self, + demands: &[Demand], + held: &BTreeMap, + pool_tokens: u32, + ) -> BTreeMap; } /// What a run measured, and every contract breach it saw. @@ -74,7 +79,14 @@ pub fn run(scenario: &WindowScenario, policy: &mut dyn WindowPolicy) -> WindowRu let mut held: BTreeMap = BTreeMap::new(); let mut out = WindowRun::default(); for event in &scenario.events { - demands.insert(event.mind, Demand { mind: event.mind, required_tokens: event.required_tokens, in_flight: event.in_flight }); + demands.insert( + event.mind, + Demand { + mind: event.mind, + required_tokens: event.required_tokens, + in_flight: event.in_flight, + }, + ); let current: Vec = demands.values().copied().collect(); let grants = policy.grant(¤t, &held, scenario.pool_tokens); let total: u64 = grants.values().map(|&g| u64::from(g)).sum(); @@ -85,11 +97,18 @@ pub fn run(scenario: &WindowScenario, policy: &mut dyn WindowPolicy) -> WindowRu // Granted past demand: the room an idle mind holds that a reading mind could have used. let slack: u64 = current .iter() - .map(|d| u64::from(grants.get(&d.mind).copied().unwrap_or(0)).saturating_sub(u64::from(d.required_tokens))) // unwrap_or: a mind granted nothing holds no slack + .map(|d| { + u64::from(grants.get(&d.mind).copied().unwrap_or(0)) + .saturating_sub(u64::from(d.required_tokens)) + }) // unwrap_or: a mind granted nothing holds no slack .sum(); for demand in ¤t { let granted = grants.get(&demand.mind).copied().unwrap_or(0); // unwrap_or: a mind the policy granted nothing holds nothing - if demand.in_flight && held.get(&demand.mind).is_some_and(|&before| granted < before) { + if demand.in_flight + && held + .get(&demand.mind) + .is_some_and(|&before| granted < before) + { out.in_flight_shrinks += 1; } if granted < demand.required_tokens { @@ -113,7 +132,12 @@ pub struct EqualSplit { } impl WindowPolicy for EqualSplit { - fn grant(&mut self, demands: &[Demand], _held: &BTreeMap, pool_tokens: u32) -> BTreeMap { + fn grant( + &mut self, + demands: &[Demand], + _held: &BTreeMap, + pool_tokens: u32, + ) -> BTreeMap { let each = pool_tokens / self.slots.max(1); demands.iter().map(|d| (d.mind, each)).collect() } @@ -125,10 +149,18 @@ impl WindowPolicy for EqualSplit { pub struct DemandFollowing; impl WindowPolicy for DemandFollowing { - fn grant(&mut self, demands: &[Demand], held: &BTreeMap, pool_tokens: u32) -> BTreeMap { + fn grant( + &mut self, + demands: &[Demand], + held: &BTreeMap, + pool_tokens: u32, + ) -> BTreeMap { let demanded: u64 = demands.iter().map(|d| u64::from(d.required_tokens)).sum(); if demanded <= u64::from(pool_tokens) { - return demands.iter().map(|d| (d.mind, d.required_tokens)).collect(); + return demands + .iter() + .map(|d| (d.mind, d.required_tokens)) + .collect(); } // An in-flight turn keeps what it holds: a grant never shrinks under a turn using it. let kept: BTreeMap = demands @@ -138,11 +170,27 @@ impl WindowPolicy for DemandFollowing { .collect(); let kept_total: u64 = kept.values().map(|&g| u64::from(g)).sum(); let rest = u64::from(pool_tokens).saturating_sub(kept_total); - let rest_demand: u64 = demands.iter().filter(|d| !kept.contains_key(&d.mind)).map(|d| u64::from(d.required_tokens)).sum(); + let rest_demand: u64 = demands + .iter() + .filter(|d| !kept.contains_key(&d.mind)) + .map(|d| u64::from(d.required_tokens)) + .sum(); let mut grants = kept; - for d in demands.iter().filter(|d| !grants.contains_key(&d.mind)).copied().collect::>() { - let share = if rest_demand == 0 { 0 } else { u64::from(d.required_tokens) * rest / rest_demand }; - grants.insert(d.mind, u32::try_from(share).unwrap_or(u32::MAX).min(d.required_tokens)); // unwrap_or: a share past u32 is capped by her demand anyway + for d in demands + .iter() + .filter(|d| !grants.contains_key(&d.mind)) + .copied() + .collect::>() + { + let share = (u64::from(d.required_tokens) * rest) + .checked_div(rest_demand) + .unwrap_or(0); // unwrap_or: no demand left to share means no share + grants.insert( + d.mind, + u32::try_from(share) + .unwrap_or(u32::MAX) + .min(d.required_tokens), + ); // unwrap_or: a share past u32 is capped by her demand anyway } grants } @@ -157,7 +205,12 @@ pub fn m5_uneven_reading() -> WindowScenario { const ATLAS: MindId = 3; const BENCHY: MindId = 4; const SCRATCH: MindId = 5; - let e = |at_ms, mind, required_tokens, in_flight| DemandEvent { at_ms, mind, required_tokens, in_flight }; + let e = |at_ms, mind, required_tokens, in_flight| DemandEvent { + at_ms, + mind, + required_tokens, + in_flight, + }; WindowScenario { name: "m5-uneven-reading", pool_tokens: 290_560, @@ -185,7 +238,10 @@ mod tests { fn todays_equal_split_squeezes_a_reading_mind_while_others_idle() { let run = run(&m5_uneven_reading(), &mut EqualSplit { slots: 5 }); assert!(run.squeezes > 0, "{run:?}"); - assert!(run.unmet_when_fits > 0, "every demand fit the pool and one was cut: {run:?}"); + assert!( + run.unmet_when_fits > 0, + "every demand fit the pool and one was cut: {run:?}" + ); } // what this catches: a demand-following grant breaking the contract: over the pool, an @@ -199,11 +255,20 @@ mod tests { // what this catches: an overloaded pool shrinking a turn in flight to seat another. #[test] fn an_overloaded_pool_never_shrinks_a_turn_in_flight() { - let e = |at_ms, mind, required_tokens, in_flight| DemandEvent { at_ms, mind, required_tokens, in_flight }; + let e = |at_ms, mind, required_tokens, in_flight| DemandEvent { + at_ms, + mind, + required_tokens, + in_flight, + }; let scenario = WindowScenario { name: "overloaded", pool_tokens: 100_000, - events: vec![e(0, 1, 60_000, true), e(1, 2, 60_000, false), e(2, 3, 30_000, false)], + events: vec![ + e(0, 1, 60_000, true), + e(1, 2, 60_000, false), + e(2, 3, 30_000, false), + ], }; let run = run(&scenario, &mut DemandFollowing); assert_eq!(run.in_flight_shrinks, 0, "{run:?}"); From 0f7e8934bf374a59db73bd29ac6f683629f9d5a0 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 2 Nov 2026 21:20:42 -0600 Subject: [PATCH 4/6] window_sim: each unwrap_or's justification on its own line (rustfmt had split two) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- core/continuum-engine/src/capacity/window_sim.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/core/continuum-engine/src/capacity/window_sim.rs b/core/continuum-engine/src/capacity/window_sim.rs index 9e6937f8ef..1b7c6ee0a7 100644 --- a/core/continuum-engine/src/capacity/window_sim.rs +++ b/core/continuum-engine/src/capacity/window_sim.rs @@ -98,9 +98,9 @@ pub fn run(scenario: &WindowScenario, policy: &mut dyn WindowPolicy) -> WindowRu let slack: u64 = current .iter() .map(|d| { - u64::from(grants.get(&d.mind).copied().unwrap_or(0)) - .saturating_sub(u64::from(d.required_tokens)) - }) // unwrap_or: a mind granted nothing holds no slack + let granted = grants.get(&d.mind).copied().unwrap_or(0); // unwrap_or: a mind granted nothing holds no slack + u64::from(granted).saturating_sub(u64::from(d.required_tokens)) + }) .sum(); for demand in ¤t { let granted = grants.get(&demand.mind).copied().unwrap_or(0); // unwrap_or: a mind the policy granted nothing holds nothing @@ -185,12 +185,8 @@ impl WindowPolicy for DemandFollowing { let share = (u64::from(d.required_tokens) * rest) .checked_div(rest_demand) .unwrap_or(0); // unwrap_or: no demand left to share means no share - grants.insert( - d.mind, - u32::try_from(share) - .unwrap_or(u32::MAX) - .min(d.required_tokens), - ); // unwrap_or: a share past u32 is capped by her demand anyway + let share = u32::try_from(share).unwrap_or(u32::MAX); // unwrap_or: a share past u32 is capped by her demand below + grants.insert(d.mind, share.min(d.required_tokens)); } grants } From 4aa9284b8735fa9ace41bfe0ab505e7e0a117c83 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 2 Nov 2026 21:48:11 -0600 Subject: [PATCH 5/6] DemandFollowing: a turn in flight keeps at least what it holds, whether or not every demand fits (Cormac on #5718: the fits branch shrank a turn mid-flight) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- .../src/capacity/window_sim.rs | 69 ++++++++++--------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/core/continuum-engine/src/capacity/window_sim.rs b/core/continuum-engine/src/capacity/window_sim.rs index 1b7c6ee0a7..68d9306963 100644 --- a/core/continuum-engine/src/capacity/window_sim.rs +++ b/core/continuum-engine/src/capacity/window_sim.rs @@ -155,40 +155,33 @@ impl WindowPolicy for DemandFollowing { held: &BTreeMap, pool_tokens: u32, ) -> BTreeMap { - let demanded: u64 = demands.iter().map(|d| u64::from(d.required_tokens)).sum(); - if demanded <= u64::from(pool_tokens) { - return demands - .iter() - .map(|d| (d.mind, d.required_tokens)) - .collect(); - } - // An in-flight turn keeps what it holds: a grant never shrinks under a turn using it. - let kept: BTreeMap = demands - .iter() - .filter(|d| d.in_flight) - .filter_map(|d| held.get(&d.mind).map(|&h| (d.mind, h))) - .collect(); - let kept_total: u64 = kept.values().map(|&g| u64::from(g)).sum(); - let rest = u64::from(pool_tokens).saturating_sub(kept_total); - let rest_demand: u64 = demands + // A turn in flight keeps at least what it holds: its grant is a floor whether or not + // every demand fits (Cormac on #5718: the fits branch granted the new, smaller demand + // and shrank a turn mid-flight). The floors fit the pool because they were granted + // inside it; what they leave is shared over the rest of each mind's demand, whole when + // it all fits and in proportion when it does not. + let floor = |d: &Demand| match d.in_flight { + true => held.get(&d.mind).copied().unwrap_or(0), // unwrap_or: a turn that holds nothing yet has no floor + false => 0, + }; + let floors: u64 = demands.iter().map(|d| u64::from(floor(d))).sum(); + let rest = u64::from(pool_tokens).saturating_sub(floors); + let needed: u64 = demands .iter() - .filter(|d| !kept.contains_key(&d.mind)) - .map(|d| u64::from(d.required_tokens)) + .map(|d| u64::from(d.required_tokens.saturating_sub(floor(d)))) .sum(); - let mut grants = kept; - for d in demands + demands .iter() - .filter(|d| !grants.contains_key(&d.mind)) - .copied() - .collect::>() - { - let share = (u64::from(d.required_tokens) * rest) - .checked_div(rest_demand) - .unwrap_or(0); // unwrap_or: no demand left to share means no share - let share = u32::try_from(share).unwrap_or(u32::MAX); // unwrap_or: a share past u32 is capped by her demand below - grants.insert(d.mind, share.min(d.required_tokens)); - } - grants + .map(|d| { + let need = u64::from(d.required_tokens.saturating_sub(floor(d))); + let more = match needed <= rest { + true => need, + false => (need * rest).checked_div(needed).unwrap_or(0), // unwrap_or: nothing needed means nothing more + }; + let more = u32::try_from(more).unwrap_or(u32::MAX); // unwrap_or: more is at most her need, a u32 + (d.mind, floor(d).saturating_add(more)) + }) + .collect() } } @@ -248,6 +241,20 @@ mod tests { assert!(run.kept(), "{run:?}"); } + // what this catches (Cormac on #5718): a turn in flight shrunk when every demand fits + // because her demand fell mid-turn; the grant she is using holds until the turn ends. + #[test] + fn a_turn_in_flight_keeps_its_grant_when_her_demand_falls_and_everything_fits() { + let e = |at_ms, mind, required_tokens, in_flight| DemandEvent { at_ms, mind, required_tokens, in_flight }; + let scenario = WindowScenario { + name: "falling-demand-in-flight", + pool_tokens: 100_000, + events: vec![e(0, 1, 60_000, true), e(1, 1, 30_000, true), e(2, 2, 20_000, false)], + }; + let run = run(&scenario, &mut DemandFollowing); + assert!(run.kept(), "{run:?}"); + } + // what this catches: an overloaded pool shrinking a turn in flight to seat another. #[test] fn an_overloaded_pool_never_shrinks_a_turn_in_flight() { From 89f28e714eabf7f75186b1fd52e9ecd24e25a6d8 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 2 Nov 2026 22:44:15 -0600 Subject: [PATCH 6/6] window_sim: run() judges fits and slack against what each mind is owed (an in-flight turn at least what it holds), so a fallen in-flight demand beside a big neighbour is the pool's limit, not a breach (Cormac on #5718) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- .../src/capacity/window_sim.rs | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/core/continuum-engine/src/capacity/window_sim.rs b/core/continuum-engine/src/capacity/window_sim.rs index 68d9306963..7ae609d632 100644 --- a/core/continuum-engine/src/capacity/window_sim.rs +++ b/core/continuum-engine/src/capacity/window_sim.rs @@ -93,13 +93,24 @@ pub fn run(scenario: &WindowScenario, policy: &mut dyn WindowPolicy) -> WindowRu if total > u64::from(scenario.pool_tokens) { out.over_pool += 1; } - let demanded: u64 = current.iter().map(|d| u64::from(d.required_tokens)).sum(); - // Granted past demand: the room an idle mind holds that a reading mind could have used. + // What each mind is owed: her demand, and while a turn is in flight at least what it + // holds, since that room cannot be taken back mid-turn. "Every demand fits" and "slack" + // are both judged against it (Cormac on #5718: a fallen in-flight demand beside a big + // neighbour read as fitting when the floors did not). + let owed = |d: &Demand| -> u64 { + let floor = match d.in_flight { + true => held.get(&d.mind).copied().unwrap_or(0), // unwrap_or: a turn that holds nothing yet is owed only her demand + false => 0, + }; + u64::from(d.required_tokens.max(floor)) + }; + let demanded: u64 = current.iter().map(owed).sum(); + // Granted past what is owed: room an idle mind holds that a reading mind could have used. let slack: u64 = current .iter() .map(|d| { let granted = grants.get(&d.mind).copied().unwrap_or(0); // unwrap_or: a mind granted nothing holds no slack - u64::from(granted).saturating_sub(u64::from(d.required_tokens)) + u64::from(granted).saturating_sub(owed(d)) }) .sum(); for demand in ¤t { @@ -255,6 +266,21 @@ mod tests { assert!(run.kept(), "{run:?}"); } + // what this catches (Cormac on #5718): the validator judging "every demand fits" on raw + // demands. A turn in flight holding 60k now asks 30k beside a 50k neighbour: the raw sum + // fits 100k, the floors do not, so the neighbour's 40k is the pool's limit, not a breach. + #[test] + fn fits_and_slack_count_a_turn_in_flight_at_what_it_holds() { + let e = |at_ms, mind, required_tokens, in_flight| DemandEvent { at_ms, mind, required_tokens, in_flight }; + let scenario = WindowScenario { + name: "held-floor-beside-a-big-neighbour", + pool_tokens: 100_000, + events: vec![e(0, 1, 60_000, true), e(1, 1, 30_000, true), e(2, 2, 50_000, false)], + }; + let run = run(&scenario, &mut DemandFollowing); + assert!(run.kept(), "{run:?}"); + } + // what this catches: an overloaded pool shrinking a turn in flight to seat another. #[test] fn an_overloaded_pool_never_shrinks_a_turn_in_flight() {