From 0c370239e3bc671204f91e64638c54da9a90a7b3 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Tue, 3 Nov 2026 19:16:12 -0600 Subject: [PATCH] A grant moves only when her need outgrows it or the pool needs it back, never on a regime flip On the M5 (d47c5c2a6, 00:49-01:04Z) persona.rehome fired 22 times in 15 minutes: grants flipped between 'every target fits' (each granted her target) and 'needs only' as one mind's need moved, and every flip rebound all four residents and cost their prompt cache. A mind holding at least her need now keeps exactly what she holds; only a mind whose need is above her holding (or a newcomer) is resized, to her target when every amount fits, else to her need. Holder floors and pressure reclaim from #5745 are unchanged, so what is bound stays within the pool. Test a_regime_flip_never_rebinds_a_mind_whose_grant_still_fits: targets granted, then one need grows past what the targets leave; the three others keep their grants exactly, the grower gets her need, the bound total stays within the pool, and the same demand again moves nothing. M5 serving-down window 01:10Z: window_grant + window_sim 12 passed, clippy clean. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- .../src/capacity/window_grant.rs | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/core/continuum-engine/src/capacity/window_grant.rs b/core/continuum-engine/src/capacity/window_grant.rs index 5224d0c5c6..1b6175b6e1 100644 --- a/core/continuum-engine/src/capacity/window_grant.rs +++ b/core/continuum-engine/src/capacity/window_grant.rs @@ -113,9 +113,16 @@ impl WindowPolicy for DemandFollowing { let want = |d: &Demand| need(d).max(holds(d)); let total = |f: &dyn Fn(&Demand) -> u32| demands.iter().map(|d| u64::from(f(d))).sum::(); let pool = u64::from(pool_tokens); - // Room for every target: each mind reads whole, never past one sequence, never below what she - // holds. Otherwise needs first, and what is left stays in the pool. - let aim = |d: &Demand| d.target.get().min(d.ceiling).max(want(d)); + // A mind holding at least her need keeps exactly what she holds: a grant moves only when her + // need outgrows it or the pool needs it back, never because the regime flipped between "every + // target fits" and "needs only" (each flip rebound all four residents and cost their prompt + // cache: 22 rebinds in 15 minutes on the M5, d47c5c2a6). A mind whose need is above what she + // holds (or a newcomer) is sized to her target when every amount fits, else to her need. + let settled = |d: &Demand| holds(d) >= need(d); + let aim = |d: &Demand| match settled(d) { + true => holds(d), + false => d.target.get().min(d.ceiling).max(want(d)), + }; if total(&aim) <= pool { return demands.iter().map(|d| (d.mind, aim(d))).collect(); } @@ -292,4 +299,30 @@ mod tests { assert_eq!(grants, BTreeMap::from([(1, 100_000), (2, 104_015), (3, 88_380), (4, 90_000)]), "every target fits: each reads whole"); } + + // what this catches (the M5, 2026-11-03, d47c5c2a6: 22 rebinds in 15 minutes): a grant that moves + // when the regime flips. Every target fits, so each mind is granted her target; then one mind's + // need grows past what the targets leave. She is granted her need from the room, and every other + // mind, still holding at least her need, keeps exactly her grant: no rebind. What is bound stays + // within the pool. + #[test] + fn a_regime_flip_never_rebinds_a_mind_whose_grant_still_fits() { + let pool = 200_000; + let mind = |m, need, target| Demand { mind: m, need: t(need), target: t(target), in_flight: false, ceiling: pool }; + let first = [mind(1, 14_000, 47_000), mind(2, 20_000, 53_000), mind(3, 20_000, 51_000), mind(4, 14_000, 45_000)]; + let held = DemandFollowing.grant(&first, &BTreeMap::new(), pool); + assert_eq!(held, BTreeMap::from([(1, 47_000), (2, 53_000), (3, 51_000), (4, 45_000)]), "every target fits"); + // Mind 4's need grows to 49,000: the targets no longer all fit (47+53+51+60 > 200), but her need + // fits in what the others hold above theirs (47+53+51+49 = 200). + let flipped = [mind(1, 14_000, 47_000), mind(2, 20_000, 53_000), mind(3, 20_000, 51_000), mind(4, 49_000, 60_000)]; + let next = DemandFollowing.grant(&flipped, &held, pool); + for m in [1, 2, 3] { + assert_eq!(next.get(&m), held.get(&m), "mind {m} still holds at least her need: no rebind ({next:?})"); + } + assert_eq!(next.get(&4), Some(&49_000), "the grower gets her need from what the others leave"); + assert!(next.values().map(|&g| u64::from(g)).sum::() <= u64::from(pool), "bound within the pool: {next:?}"); + // And the same grants again: nothing moves. + assert_eq!(DemandFollowing.grant(&flipped, &next, pool), next, "a steady demand is a steady grant"); + } + }