From dd86d71e9bdc751ec67cd4a6ab518f3305c1cea7 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Tue, 3 Nov 2026 21:14:27 -0600 Subject: [PATCH 1/3] A grants waiter marks the current grants seen before it waits: an ungranted mind parks until a NEWER publish, never spins d39469463 (#5752) pegged the M5 core at 339% CPU: delib.window.ungranted fired 751 times for Atlas in about a minute. grants_changed() cloned the installed watch receiver, which never marks a value seen, so every clone's changed() resolved at once, and an ungranted mind looped measure -> wake -> ungranted with nothing changed. The same latent spin was in CapacityReadiness::wait's grants arm on any node with grants. Rolled back to 0b2854327. - GrantsWaiter::new marks the current grants seen (borrow_and_update); changed() resolves only on a newer publish. grants_waiter() is taken BEFORE each decision (decide_window) and before the readiness check, so a publish between the two is never lost. - decide_window refuses with GrantsUnavailable only when she is ungranted; a decided window needs no watch. - Tests on a LIVE tokio watch: a waiter over unchanged grants is Pending and one send wakes it; an ungranted mind is measured once, parks, and runs at her grant; no watch refuses only an ungranted mind. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- .../verdict/contribute.rs | 2 +- .../src/cognition/workspace/capacity.rs | 12 +- .../src/capacity/mind_window.rs | 103 +++++++++++++----- .../src/capacity/window_grant.rs | 2 +- 4 files changed, 88 insertions(+), 31 deletions(-) diff --git a/core/continuum-cognition/src/cognition/llm_deliberation_faculty/verdict/contribute.rs b/core/continuum-cognition/src/cognition/llm_deliberation_faculty/verdict/contribute.rs index 8954bff3bf..b36f8af4fc 100644 --- a/core/continuum-cognition/src/cognition/llm_deliberation_faculty/verdict/contribute.rs +++ b/core/continuum-cognition/src/cognition/llm_deliberation_faculty/verdict/contribute.rs @@ -55,7 +55,7 @@ impl LlmDeliberationFaculty { ); let _measured = self.prompt_view_with_visual_feedback(ws, loaded.context_window, PromptCalibration::default(), None); }, - crate::capacity::mind_window::grants_changed, + crate::capacity::mind_window::grants_waiter, ) .await { diff --git a/core/continuum-cognition/src/cognition/workspace/capacity.rs b/core/continuum-cognition/src/cognition/workspace/capacity.rs index 0ca8ea0a79..4d1b29d648 100644 --- a/core/continuum-cognition/src/cognition/workspace/capacity.rs +++ b/core/continuum-cognition/src/cognition/workspace/capacity.rs @@ -165,9 +165,15 @@ impl CapacityReadiness { // grants watch in this process there is no such change to wait for, so that arm never // fires (a refused watch resolving at once would spin this loop; the turn's own // decide_window is where a missing watch is refused). - let grants_change = async { - if crate::capacity::mind_window::grants_changed().await.is_err() { - std::future::pending::<()>().await; + // A mind on the shared pool's window is her grant: a NEWER grant is a capacity change. + // The waiter is taken before the check below and marks the current grants seen, so it + // parks on unchanged grants (a clone that did not spun this loop: 339% CPU on the M5). + // With no grants watch there is no such change, and the arm never fires. + let waiter = crate::capacity::mind_window::grants_waiter(); + let grants_change = async move { + match waiter { + Ok(waiter) if waiter.changed().await.is_ok() => {} + _ => std::future::pending::<()>().await, } }; if self.changed() { diff --git a/core/continuum-engine/src/capacity/mind_window.rs b/core/continuum-engine/src/capacity/mind_window.rs index 5485d5a219..9c8db60d56 100644 --- a/core/continuum-engine/src/capacity/mind_window.rs +++ b/core/continuum-engine/src/capacity/mind_window.rs @@ -11,7 +11,7 @@ use crate::inference::planning::window_allocator::Tokens; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum MindWindow { /// On the shared pool with no grant yet: she has no window, and her turn waits for one on - /// the grants watch ([`grants_changed`]); never a provisional number. + /// the grants watch ([`grants_waiter`]); never a provisional number. Ungranted, Decided(Decided), } @@ -64,27 +64,49 @@ impl std::fmt::Display for GrantsUnavailable { /// Resolves when the serving daemon publishes new grants: what an ungranted mind waits on, and what a /// mind on the shared pool watches for a changed window. -pub async fn grants_changed() -> Result<(), GrantsUnavailable> { - let mut rx = super::window_grant::grants_receiver().ok_or(GrantsUnavailable)?; - rx.changed().await.map_err(|_| GrantsUnavailable) +/// +/// Take it BEFORE deciding (`grants_waiter`), so a grant published between the decision and the wait +/// still wakes her. +pub fn grants_waiter() -> Result { + super::window_grant::grants_receiver().map(GrantsWaiter::new).ok_or(GrantsUnavailable) +} + +/// A wait for grants NEWER than those current when it was made. The receiver is a clone of the +/// installed one, which never marks a value seen, so the clone marks the current grants seen first: +/// without that, `changed()` resolves at once on every call, and an ungranted mind looped measure, +/// wake, ungranted with nothing changed (the M5 core at 339% CPU on d39469463, Atlas 751 times in a +/// minute). +pub struct GrantsWaiter(tokio::sync::watch::Receiver>); + +impl GrantsWaiter { + pub fn new(mut rx: tokio::sync::watch::Receiver>) -> Self { + rx.borrow_and_update(); + Self(rx) + } + + /// Resolves when newer grants are published; `GrantsUnavailable` when the sender is gone. + pub async fn changed(mut self) -> Result<(), GrantsUnavailable> { + self.0.changed().await.map_err(|_| GrantsUnavailable) + } } /// Decide her window. An ungranted mind is MEASURED first (`measure` builds her view, which records -/// her wish, so the next grants tick can grant her), then waits for new grants and decides again: -/// measure, park, wake, decide (Cormac on #5752: a wait before the measurement waits forever). -pub async fn decide_window(decide: D, mut measure: M, wait: W) -> Result +/// her wish, so the next grants tick can grant her), then waits for NEWER grants and decides again +/// (Cormac on #5752). The waiter is taken before each decision, so no publish between the two is lost. +/// No grants watch is a refusal only when she is ungranted: a decided window needs none. +pub async fn decide_window(decide: D, mut measure: M, subscribe: S) -> Result where D: Fn() -> MindWindow, M: FnMut(), - W: Fn() -> F, - F: std::future::Future>, + S: Fn() -> Result, { loop { + let waiter = subscribe(); match decide() { MindWindow::Decided(decided) => return Ok(decided), MindWindow::Ungranted => { measure(); - wait().await?; + waiter?.changed().await?; } } } @@ -96,33 +118,62 @@ mod tests { use crate::inference::planning::window_allocator::test_tokens as t; use std::cell::Cell; + fn grants(by_mind: &[(uuid::Uuid, u32)]) -> Option { + Some(super::super::window_grant::PoolGrants { + pool_tokens: 200_000, + per_sequence_cap: 200_000, + by_mind: by_mind.iter().copied().collect(), + }) + } + + // what this catches (the M5 core at 339% CPU on d39469463): a waiter over grants that never + // change resolving at once. A clone of a receiver that never marked a value seen did, so an + // ungranted mind spun. Unchanged grants park the waiter; only a newer publish wakes it. + #[tokio::test] + async fn a_waiter_parks_on_unchanged_grants_and_wakes_on_a_newer_publish() { + let (sender, installed) = tokio::sync::watch::channel(grants(&[])); + let waiter = GrantsWaiter::new(installed.clone()); + let mut waiting = Box::pin(waiter.changed()); + let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); + assert!(std::future::Future::poll(waiting.as_mut(), &mut cx).is_pending(), "unchanged grants: it parks"); + let _ = sender.send(grants(&[(uuid::Uuid::from_u128(4), 14_860)])); // the receiver is held above + assert_eq!(waiting.await, Ok(()), "a newer publish wakes it"); + } + // what this catches (Cormac on #5752): a new mind that waits for a grant before anything measures - // her, so she is never granted. She is measured, then parks, then is granted and runs. + // her. She is measured, then parks, then is granted and runs; and a grant published between her + // decision and her wait is not lost (the waiter is taken first). #[tokio::test] - async fn an_ungranted_mind_is_measured_before_she_parks_then_runs_at_her_grant() { + async fn an_ungranted_mind_is_measured_parks_then_runs_at_her_grant() { + let her = uuid::Uuid::from_u128(4); + let (sender, installed) = tokio::sync::watch::channel(grants(&[])); let measured = Cell::new(0); - let woke = Cell::new(0); + let granted = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); let decided = decide_window( - || match measured.get() { - 0 => MindWindow::Ungranted, - _ => MindWindow::Decided(Decided::Granted(t(20_817))), + || match granted.load(std::sync::atomic::Ordering::SeqCst) { + false => MindWindow::Ungranted, + true => MindWindow::Decided(Decided::Granted(t(14_860))), }, - || measured.set(measured.get() + 1), || { - assert_eq!(measured.get(), 1, "measured before she parks"); - woke.set(woke.get() + 1); - async { Ok(()) } + measured.set(measured.get() + 1); + // the serving tick that grants her runs after she is measured + granted.store(true, std::sync::atomic::Ordering::SeqCst); + let _ = sender.send(grants(&[(her, 14_860)])); // the receiver is held by the waiter }, + || Ok(GrantsWaiter::new(installed.clone())), ) .await; - assert_eq!(decided, Ok(Decided::Granted(t(20_817)))); - assert_eq!((measured.get(), woke.get()), (1, 1), "one measurement, one wait, then her grant"); + assert_eq!(decided, Ok(Decided::Granted(t(14_860)))); + assert_eq!(measured.get(), 1, "measured once, then granted"); } - // what this catches: a turn hanging silently on a watch that can never fire. + // what this catches: a turn hanging on a watch that can never fire, and a decided window refused + // only because no watch exists. #[tokio::test] - async fn no_grants_watch_is_a_typed_refusal_never_a_hang() { - let decided = decide_window(|| MindWindow::Ungranted, || {}, || async { Err(GrantsUnavailable) }).await; - assert_eq!(decided, Err(GrantsUnavailable)); + async fn no_grants_watch_refuses_only_an_ungranted_mind() { + let refused = decide_window(|| MindWindow::Ungranted, || {}, || Err(GrantsUnavailable)).await; + assert_eq!(refused, Err(GrantsUnavailable)); + let bound = decide_window(|| MindWindow::Decided(Decided::Bound(8_192)), || {}, || Err(GrantsUnavailable)).await; + assert_eq!(bound, Ok(Decided::Bound(8_192)), "a bound window needs no grants watch"); } } diff --git a/core/continuum-engine/src/capacity/window_grant.rs b/core/continuum-engine/src/capacity/window_grant.rs index aa15c00b78..7fa3289470 100644 --- a/core/continuum-engine/src/capacity/window_grant.rs +++ b/core/continuum-engine/src/capacity/window_grant.rs @@ -46,7 +46,7 @@ pub fn install_grants(rx: tokio::sync::watch::Receiver>) -> b } /// A receiver of the published grants, for a waiter that must wake when they change -/// ([`super::mind_window::grants_changed`]). `None` before the serving daemon installs them. +/// ([`super::mind_window::grants_waiter`]). `None` before the serving daemon installs them. pub fn grants_receiver() -> Option>> { GRANTS.get().cloned() } From 3d3672e9068d6011c7b4f0bb27d0dea618e85b72 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Tue, 3 Nov 2026 21:32:46 -0600 Subject: [PATCH 2/3] Readiness grants arm: an if-let, not a moving pattern guard Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- .../src/cognition/workspace/capacity.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/core/continuum-cognition/src/cognition/workspace/capacity.rs b/core/continuum-cognition/src/cognition/workspace/capacity.rs index 4d1b29d648..d017238fff 100644 --- a/core/continuum-cognition/src/cognition/workspace/capacity.rs +++ b/core/continuum-cognition/src/cognition/workspace/capacity.rs @@ -171,10 +171,12 @@ impl CapacityReadiness { // With no grants watch there is no such change, and the arm never fires. let waiter = crate::capacity::mind_window::grants_waiter(); let grants_change = async move { - match waiter { - Ok(waiter) if waiter.changed().await.is_ok() => {} - _ => std::future::pending::<()>().await, + if let Ok(waiter) = waiter { + if waiter.changed().await.is_ok() { + return; + } } + std::future::pending::<()>().await }; if self.changed() { return; From d1ecefa6a8c8f1e8b08d2e5125c7730fc33f9860 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Tue, 3 Nov 2026 21:37:42 -0600 Subject: [PATCH 3/3] The grants waiter type is private: grants_waiter() returns an opaque future (unwired-machinery ratchet) GrantsWaiter was a pub type constructed only in its own file, which the production_reachability ratchet counts as unwired public machinery. Callers only await a newer publish, so grants_waiter() returns that future and the type stays private; it still marks the current grants seen when called (eager), so no publish between the waiter and the decision is lost. decide_window takes any such future. M5 03:36Z: ratchets against canary pass; engine capacity::mind_window 3; cognition workspace::capacity 4; clippy clean on changed lines. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- .../src/cognition/workspace/capacity.rs | 2 +- .../src/capacity/mind_window.rs | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/core/continuum-cognition/src/cognition/workspace/capacity.rs b/core/continuum-cognition/src/cognition/workspace/capacity.rs index d017238fff..dedcaf6c3b 100644 --- a/core/continuum-cognition/src/cognition/workspace/capacity.rs +++ b/core/continuum-cognition/src/cognition/workspace/capacity.rs @@ -172,7 +172,7 @@ impl CapacityReadiness { let waiter = crate::capacity::mind_window::grants_waiter(); let grants_change = async move { if let Ok(waiter) = waiter { - if waiter.changed().await.is_ok() { + if waiter.await.is_ok() { return; } } diff --git a/core/continuum-engine/src/capacity/mind_window.rs b/core/continuum-engine/src/capacity/mind_window.rs index 9c8db60d56..49884cd3e4 100644 --- a/core/continuum-engine/src/capacity/mind_window.rs +++ b/core/continuum-engine/src/capacity/mind_window.rs @@ -67,8 +67,8 @@ impl std::fmt::Display for GrantsUnavailable { /// /// Take it BEFORE deciding (`grants_waiter`), so a grant published between the decision and the wait /// still wakes her. -pub fn grants_waiter() -> Result { - super::window_grant::grants_receiver().map(GrantsWaiter::new).ok_or(GrantsUnavailable) +pub fn grants_waiter() -> Result>, GrantsUnavailable> { + super::window_grant::grants_receiver().map(|rx| GrantsWaiter::new(rx).changed()).ok_or(GrantsUnavailable) } /// A wait for grants NEWER than those current when it was made. The receiver is a clone of the @@ -76,16 +76,16 @@ pub fn grants_waiter() -> Result { /// without that, `changed()` resolves at once on every call, and an ungranted mind looped measure, /// wake, ungranted with nothing changed (the M5 core at 339% CPU on d39469463, Atlas 751 times in a /// minute). -pub struct GrantsWaiter(tokio::sync::watch::Receiver>); +struct GrantsWaiter(tokio::sync::watch::Receiver>); impl GrantsWaiter { - pub fn new(mut rx: tokio::sync::watch::Receiver>) -> Self { + fn new(mut rx: tokio::sync::watch::Receiver>) -> Self { rx.borrow_and_update(); Self(rx) } /// Resolves when newer grants are published; `GrantsUnavailable` when the sender is gone. - pub async fn changed(mut self) -> Result<(), GrantsUnavailable> { + async fn changed(mut self) -> Result<(), GrantsUnavailable> { self.0.changed().await.map_err(|_| GrantsUnavailable) } } @@ -94,11 +94,12 @@ impl GrantsWaiter { /// her wish, so the next grants tick can grant her), then waits for NEWER grants and decides again /// (Cormac on #5752). The waiter is taken before each decision, so no publish between the two is lost. /// No grants watch is a refusal only when she is ungranted: a decided window needs none. -pub async fn decide_window(decide: D, mut measure: M, subscribe: S) -> Result +pub async fn decide_window(decide: D, mut measure: M, subscribe: S) -> Result where D: Fn() -> MindWindow, M: FnMut(), - S: Fn() -> Result, + S: Fn() -> Result, + W: std::future::Future>, { loop { let waiter = subscribe(); @@ -106,7 +107,7 @@ where MindWindow::Decided(decided) => return Ok(decided), MindWindow::Ungranted => { measure(); - waiter?.changed().await?; + waiter?.await?; } } } @@ -160,7 +161,7 @@ mod tests { granted.store(true, std::sync::atomic::Ordering::SeqCst); let _ = sender.send(grants(&[(her, 14_860)])); // the receiver is held by the waiter }, - || Ok(GrantsWaiter::new(installed.clone())), + || Ok(GrantsWaiter::new(installed.clone()).changed()), ) .await; assert_eq!(decided, Ok(Decided::Granted(t(14_860)))); @@ -171,9 +172,9 @@ mod tests { // only because no watch exists. #[tokio::test] async fn no_grants_watch_refuses_only_an_ungranted_mind() { - let refused = decide_window(|| MindWindow::Ungranted, || {}, || Err(GrantsUnavailable)).await; + let refused = decide_window(|| MindWindow::Ungranted, || {}, || Err::>, _>(GrantsUnavailable)).await; assert_eq!(refused, Err(GrantsUnavailable)); - let bound = decide_window(|| MindWindow::Decided(Decided::Bound(8_192)), || {}, || Err(GrantsUnavailable)).await; + let bound = decide_window(|| MindWindow::Decided(Decided::Bound(8_192)), || {}, || Err::>, _>(GrantsUnavailable)).await; assert_eq!(bound, Ok(Decided::Bound(8_192)), "a bound window needs no grants watch"); } }