From 010a7ddbeedc822f853841fd7edb72571ae80a15 Mon Sep 17 00:00:00 2001 From: joelteply Date: Sat, 21 Nov 2026 23:21:23 -0600 Subject: [PATCH 1/2] A mind waiting for a window grant is never dropped by the act timer; her own queue place is its progress IntelMac, 8f924e561, 2026-11-22: 20 of 47 settle.tick.deadline drops spent the whole 25-min act floor in deliberation.window_grant (9,547 delib.window.waiting re-decisions), never dispatched. That wait was named (stage) but not registered with the admission clock, so it was charged to her act. - PoolGrants carries `owed` (each owed mind's publication-owed-since, from DispatchState at publish); `owed_rank` orders cohorts then ids; `waiting_place` reads Granted / Owed{ahead} / Unqueued. - admission_clock::window_grant is the ONE way deliberation waits for her window: the stage and the registration together (contribute.rs calls only it). Progress is HER place: becoming granted, or fewer minds ahead than she has had. Grants churn (~1.5 s on the IntelMac), reordering, a newcomer ahead, or staying granted while her hold is refused move nothing, so a livelock still ends at one checkpoint, now as settle.queue.stalled rather than a bare tick deadline. Tests (settle/tests.rs, existing mod; window_grant tests): a grant at 25 s past a 10 s bound runs and 9 s of later work still fits (the wait is not charged); churn stalls at one bound; staying granted earns one checkpoint; cancellation releases the wait; cohort rank and absent != rank 0. Event-driven (oneshots + paused clock), no new test sleeps. Mutation-proven: staying-granted-counts-always, any-rank-change, no registration in window_grant_with, ties-ignore-id each fail their test. Ratchets pass at the merge base. Not here: P3 arming (Fable #6116), in-flight drops via GET /slots on the engine loop (needs fork evidence first, per Codex). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE --- .../src/cognition/act_observe/settle/tests.rs | 103 ++++++++++++++++++ .../admission_clock.rs | 56 ++++++++++ .../verdict/contribute.rs | 2 +- .../tests/a_stale_lease_posts_nothing.rs | 1 + .../src/capacity/dyno/service/world.rs | 2 +- .../src/capacity/window_grant.rs | 46 ++++++++ .../src/capacity/window_grant/lease/fair.rs | 2 +- .../window_grant/lease/fair/diagnosis.rs | 2 +- .../src/modules/serving_daemon/live_pool.rs | 2 +- 9 files changed, 211 insertions(+), 5 deletions(-) diff --git a/core/continuum-cognition/src/cognition/act_observe/settle/tests.rs b/core/continuum-cognition/src/cognition/act_observe/settle/tests.rs index 3045436712..53340adef2 100644 --- a/core/continuum-cognition/src/cognition/act_observe/settle/tests.rs +++ b/core/continuum-cognition/src/cognition/act_observe/settle/tests.rs @@ -115,6 +115,109 @@ assert_eq!(start.elapsed(), std::time::Duration::ZERO); } + /// Her place in the grants queue as each checkpoint reads it, from a script of what she sees. + fn scripted_place(script: Vec) -> impl FnMut() -> Option { + let mut reads = script.into_iter(); + let mut last = None; + move || { + last = reads.next().or(last); + last + } + } + + // what this catches (IntelMac 2026-11-22: 20 acts dropped at the 25-min floor inside + // deliberation.window_grant, never dispatched): a grant that comes after the old act deadline, + // while her place moves, still lets her act run, and the wait is not charged to her act: the + // 9 s of work after a 25 s wait fits a 10 s bound. + #[tokio::test(start_paused = true)] + async fn a_grant_after_the_old_deadline_runs_and_her_act_time_excludes_the_wait() { + use crate::capacity::window_grant::WaitingPlace::{Granted, Owed}; + let bound = std::time::Duration::from_secs(10); + let start = tokio::time::Instant::now(); + let place = scripted_place(vec![Owed { ahead: 3 }, Owed { ahead: 2 }, Owed { ahead: 1 }, Granted]); + let (grant, granted) = tokio::sync::oneshot::channel::<()>(); + let (finish, finished) = tokio::sync::oneshot::channel::<()>(); + let step = async move { + admission_clock::window_grant_with(granted, place).await.expect("test: the grant is sent"); // test fixture + finished.await.expect("test: the work is finished"); // test fixture + }; + let turn = tokio::spawn(super::until_act_stalls(start + bound, bound, None, step)); + run_for(std::time::Duration::ZERO).await; // her turn starts and registers its wait at 0 s + // her place moves at the 10 s and 20 s checkpoints; the grant arrives at 25 s, past the old deadline + for seconds in [10, 10, 5] { + run_for(std::time::Duration::from_secs(seconds)).await; + } + grant.send(()).expect("test: her act still waits for the grant, never ended by its old deadline"); // test fixture + // 9 s of work after the grant fits her 10 s act only if the 25 s wait was not charged to it + run_for(std::time::Duration::from_secs(9)).await; + finish.send(()).expect("test: her act is still running: the wait was not charged to it"); // test fixture + assert!(turn.await.expect("test: the turn task").is_ok(), "her place moved at every checkpoint, so the wait never ended her act"); // test fixture + assert_eq!(start.elapsed(), std::time::Duration::from_secs(34)); + } + + /// Move the paused clock by `by`, letting every woken task run: events, never a sleep, move the work. + async fn run_for(by: std::time::Duration) { + tokio::time::advance(by).await; + for _ in 0..16 { + tokio::task::yield_now().await; + } + } + + // what this catches: the grants churn (published every ~1.5 s on the IntelMac, #6070) read as + // her progress, which would hold a livelocked act forever. Other minds reordering, a newcomer + // ahead of her and going unqueued move nothing, so her act ends at its first checkpoint. + #[tokio::test(start_paused = true)] + async fn grants_churn_with_her_place_unmoved_ends_her_act_at_one_bound() { + use crate::capacity::window_grant::WaitingPlace::{Owed, Unqueued}; + let bound = std::time::Duration::from_secs(10); + let start = tokio::time::Instant::now(); + let place = scripted_place(vec![Owed { ahead: 2 }, Owed { ahead: 3 }, Unqueued, Owed { ahead: 2 }]); + let waiting = admission_clock::window_grant_with(std::future::pending::<()>(), place); + assert!(tokio::time::timeout(bound * 10, super::until_act_stalls(start + bound, bound, None, waiting)).await.expect("a regression fails here, never hangs").is_err()); + assert_eq!(start.elapsed(), bound, "churn is not her place moving"); + } + + // what this catches: granted at every checkpoint while her hold is refused (the 9,547 + // 'her grant moved between her decision and her act' re-decisions) read as progress. Becoming + // granted is one move; staying granted is none. + #[tokio::test(start_paused = true)] + async fn staying_granted_while_her_hold_is_refused_moves_her_once() { + use crate::capacity::window_grant::WaitingPlace::{Granted, Owed}; + let bound = std::time::Duration::from_secs(10); + let start = tokio::time::Instant::now(); + let place = scripted_place(vec![Owed { ahead: 1 }, Granted]); + let waiting = admission_clock::window_grant_with(std::future::pending::<()>(), place); + assert!(tokio::time::timeout(bound * 10, super::until_act_stalls(start + bound, bound, None, waiting)).await.expect("a regression fails here, never hangs").is_err()); + assert_eq!(start.elapsed(), bound * 2, "the grant earned one checkpoint, never a standing exemption"); + } + + // what this catches: a cancelled caller leaving her grant wait alive (its acquisition and its + // place observer held past the act that owned them). + #[tokio::test(start_paused = true)] + async fn a_cancelled_caller_releases_her_grant_wait() { + use crate::capacity::window_grant::WaitingPlace::Owed; + let held = std::sync::Arc::new(()); + let (acquired, observed) = (std::sync::Arc::clone(&held), std::sync::Arc::clone(&held)); + let bound = std::time::Duration::from_secs(10); + let start = tokio::time::Instant::now(); + let acquire = async move { + let _acquired = acquired; + std::future::pending::<()>().await + }; + let mut ahead = 9; + let place = move || { + let _observed = &observed; + ahead -= 1; + Some(Owed { ahead }) + }; + let turn = tokio::spawn(super::until_act_stalls(start + bound, bound, None, admission_clock::window_grant_with(acquire, place))); + tokio::task::yield_now().await; + assert_eq!(std::sync::Arc::strong_count(&held), 3, "the wait holds its acquisition and its observer"); + turn.abort(); + assert!(turn.await.is_err_and(|e| e.is_cancelled())); + assert_eq!(std::sync::Arc::strong_count(&held), 1, "cancelling her act released both"); + } + async fn queued(gate: &LaneAdmission, count: usize) { for _ in 0..100 { if gate.work_waiting() == count { return; } diff --git a/core/continuum-cognition/src/cognition/llm_deliberation_faculty/admission_clock.rs b/core/continuum-cognition/src/cognition/llm_deliberation_faculty/admission_clock.rs index d8c8d9c7a1..1c7ee27c6f 100644 --- a/core/continuum-cognition/src/cognition/llm_deliberation_faculty/admission_clock.rs +++ b/core/continuum-cognition/src/cognition/llm_deliberation_faculty/admission_clock.rs @@ -171,6 +171,62 @@ pub(crate) async fn waiting(wait: LaneWait, acquire: impl Future) waiting_with_probe(acquire, || sample(&wait)).await } +/// Her wait for a window grant ([`window_grant`]), registered like a lane wait: its time is the scheduler's, never her +/// act's. It ends at a checkpoint only if HER place did not move, not if grants merely changed. The +/// IntelMac, 2026-11-22: 20 acts waited the whole 25-minute floor in `deliberation.window_grant` +/// (9,547 `delib.window.waiting` re-decisions) and were dropped by the act timer, never dispatched. +/// The ONE way deliberation waits for her window: the named stage and its registration together, so +/// the call site cannot take one without the other. +pub(crate) async fn window_grant(persona: uuid::Uuid, acquire: impl Future) -> T { + window_grant_with(acquire, move || crate::capacity::window_grant::waiting_place(persona)).await +} + +/// [`window_grant`] with her place read from `place`: the production stage and registration, with +/// only the place source a test scripts. +pub(crate) async fn window_grant_with( + acquire: impl Future, + mut place: impl FnMut() -> Option, +) -> T { + let mut moved = GrantPlace::default(); + stage("deliberation.window_grant", waiting_with_probe(acquire, move || std::future::ready(moved.observe(place())))).await +} + +/// What counts as her moving toward a grant, across checkpoints: being granted where she was not, +/// or fewer owed minds ahead of her than she has ever had. Churn, minds reordering, a newcomer ahead +/// of her, or staying granted while her hold is refused, move nothing. +#[derive(Default)] +struct GrantPlace { + granted: bool, + fewest_ahead: Option, + moves: u64, +} + +impl GrantPlace { + fn observe(&mut self, place: Option) -> QueueProgress { + use crate::capacity::window_grant::WaitingPlace; + match place { + Some(WaitingPlace::Granted) => { + if !self.granted { + self.moves += 1; + } + self.granted = true; + } + Some(WaitingPlace::Owed { ahead }) => { + self.granted = false; + if self.fewest_ahead.is_none_or(|fewest| ahead < fewest) { + // the first sighting sets her baseline; only a later, smaller count is a move + if self.fewest_ahead.is_some() { + self.moves += 1; + } + self.fewest_ahead = Some(ahead); + } + } + Some(WaitingPlace::Unqueued) | None => self.granted = false, + } + QueueProgress { grants: (self.moves, 0), holders: Vec::new() } + } +} + pub(crate) async fn waiting_with_probe(acquire: impl Future, mut probe: P) -> T where P: FnMut() -> S, 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 2a2c0627f8..0c91dba694 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 @@ -47,7 +47,7 @@ impl LlmDeliberationFaculty { // first grants evaluation after it: granted, she decides again; not, her turn defers with its reason. // Her window, decided and (a grant) HELD for this act (`decide_and_hold`): a seat being reclaimed // or moved before the hold is waited for within her turn, never a failed turn (card d50e8e66). - let (decided, mut act_hold) = match super::admission_clock::stage("deliberation.window_grant", crate::capacity::mind_window::decide_and_hold( + let (decided, mut act_hold) = match super::admission_clock::window_grant(self.persona_id, crate::capacity::mind_window::decide_and_hold( self.persona_id, || crate::capacity::mind_window::mind_window(self.persona_id, loaded.adapter.as_ref(), loaded.context_window), || { diff --git a/core/continuum-engine/src/ai/openai_adapter/tests/a_stale_lease_posts_nothing.rs b/core/continuum-engine/src/ai/openai_adapter/tests/a_stale_lease_posts_nothing.rs index 8cb3a7b3d2..474112f883 100644 --- a/core/continuum-engine/src/ai/openai_adapter/tests/a_stale_lease_posts_nothing.rs +++ b/core/continuum-engine/src/ai/openai_adapter/tests/a_stale_lease_posts_nothing.rs @@ -103,6 +103,7 @@ async fn a_stamped_admission_on_her_current_lease_dispatches() { by_mind: std::collections::BTreeMap::from([(persona, 148_838)]), engine: Some(engine), revisions: Default::default(), + owed: Default::default(), }; crate::capacity::window_grant::stamp_revisions(None, &mut grants); let (_publisher, receiver) = tokio::sync::watch::channel(Some(grants)); diff --git a/core/continuum-engine/src/capacity/dyno/service/world.rs b/core/continuum-engine/src/capacity/dyno/service/world.rs index d515bf5ff7..f769403a83 100644 --- a/core/continuum-engine/src/capacity/dyno/service/world.rs +++ b/core/continuum-engine/src/capacity/dyno/service/world.rs @@ -193,7 +193,7 @@ impl World { pub(crate) fn start_published(&mut self, grants: &[(usize, u32)]) { let mut next = PoolGrants { pool_tokens: self.pool, per_sequence_cap: self.ceiling, engine: Some(self.engine), - by_mind: grants.iter().map(|(i, tokens)| (self.mind(*i), *tokens)).collect(), revisions: BTreeMap::new(), + by_mind: grants.iter().map(|(i, tokens)| (self.mind(*i), *tokens)).collect(), revisions: BTreeMap::new(), owed: BTreeMap::new(), }; crate::capacity::window_grant::stamp_revisions(None, &mut next); for (i, tokens) in grants { diff --git a/core/continuum-engine/src/capacity/window_grant.rs b/core/continuum-engine/src/capacity/window_grant.rs index 2748b76cdc..dca11334d1 100644 --- a/core/continuum-engine/src/capacity/window_grant.rs +++ b/core/continuum-engine/src/capacity/window_grant.rs @@ -115,9 +115,19 @@ pub struct PoolGrants { pub engine: Option, /// Each mind's grant revision ([`stamp_revisions`]). pub revisions: BTreeMap, + /// Each mind owed a seat, at the publication she became owed (the dispatch owner's queue, oldest + /// first by value). A waiting mind reads her place in it ([`Self::owed_rank`]), never the churn. + pub owed: BTreeMap, } impl PoolGrants { + /// How many owed minds wait ahead of her (owed at an earlier publication, ties by id), when she + /// is owed. `None` when she is not owed: granted, or not waiting on this pool. + pub fn owed_rank(&self, persona: uuid::Uuid) -> Option { + let since = *self.owed.get(&persona)?; + Some(self.owed.iter().filter(|(mind, at)| (**at, **mind) < (since, persona)).count()) + } + /// Her grant, when she is one of the minds on this pool. pub fn grant_for(&self, persona: uuid::Uuid) -> Option { self.by_mind.get(&persona).copied() @@ -159,6 +169,28 @@ pub fn current_lease(persona: uuid::Uuid) -> Option { GRANTS.get()?.borrow().as_ref()?.lease_for(persona) } +/// Where a mind waiting for her window stands in the latest publication. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum WaitingPlace { + /// She holds a grant: her wait is answered. + Granted, + /// She is owed a seat behind this many owed minds. + Owed { ahead: usize }, + /// Neither granted nor owed: the dispatch owner has not queued her. + Unqueued, +} + +/// Her place in the latest publication, or `None` when no grants are published in this process. +pub fn waiting_place(persona: uuid::Uuid) -> Option { + let grants = GRANTS.get()?.borrow(); + let grants = grants.as_ref()?; + Some(match (grants.grant_for(persona), grants.owed_rank(persona)) { + (Some(_), _) => WaitingPlace::Granted, + (None, Some(ahead)) => WaitingPlace::Owed { ahead }, + (None, None) => WaitingPlace::Unqueued, + }) +} + /// How wide one seat on this node's engine is: the most ONE mind may be granted, as the grant owner /// reads it off the engine (one slot on fixed slots, the pool capped at the trained window on a shared /// one). Never the snapshot's equal share of the pool (`sequence_share`, a KV charge): an unreported @@ -621,6 +653,20 @@ mod tests { // what this catches: a window divided by a count (Joel, 2026-11-03: "What a mind needs not what // you dictate"). Minds that fit get exactly what they need, never an equal split, never past one // sequence's ceiling; what is left stays in the pool, granted to no one. + // what this catches (Codex on card 06254dcc): a waiting mind's place read wrong, which her grant + // wait counts as progress. Minds owed at the same publication are one cohort, ordered by id as the + // dispatch owner iterates them; a mind not owed has no rank, which is not rank zero. + #[test] + fn her_rank_counts_older_cohorts_then_ids_and_absent_is_not_rank_zero() { + let mind = uuid::Uuid::from_u128; + let grants = PoolGrants { owed: BTreeMap::from([(mind(1), 7), (mind(2), 9), (mind(3), 9), (mind(4), 12)]), ..PoolGrants::default() }; + assert_eq!(grants.owed_rank(mind(1)), Some(0), "the oldest waits behind no one"); + assert_eq!(grants.owed_rank(mind(2)), Some(1), "same publication as mind 3: the lower id goes first"); + assert_eq!(grants.owed_rank(mind(3)), Some(2), "behind the older one and her cohort's lower id"); + assert_eq!(grants.owed_rank(mind(4)), Some(3)); + assert_eq!(grants.owed_rank(mind(5)), None, "not owed: no rank, never rank zero"); + } + #[test] fn every_mind_gets_what_she_needs_and_the_rest_stays_in_the_pool() { let pool = 257_536; diff --git a/core/continuum-engine/src/capacity/window_grant/lease/fair.rs b/core/continuum-engine/src/capacity/window_grant/lease/fair.rs index a1c5589f73..a61b3afea3 100644 --- a/core/continuum-engine/src/capacity/window_grant/lease/fair.rs +++ b/core/continuum-engine/src/capacity/window_grant/lease/fair.rs @@ -271,7 +271,7 @@ impl Reservations { dispatch.changed.insert(d.mind, served); } } - let mut next = PoolGrants { pool_tokens: pool, per_sequence_cap: ceiling, by_mind: grants, engine, revisions: BTreeMap::new() }; + let mut next = PoolGrants { pool_tokens: pool, per_sequence_cap: ceiling, by_mind: grants, engine, revisions: BTreeMap::new(), owed: dispatch.owed.clone() }; stamp_revisions(current.as_ref(), &mut next); // An owed mind is owed each seat she may take when its holder's act ends: every such holder // is armed, so her next reserve after a completed act is refused at the receipt boundary and diff --git a/core/continuum-engine/src/capacity/window_grant/lease/fair/diagnosis.rs b/core/continuum-engine/src/capacity/window_grant/lease/fair/diagnosis.rs index c3bc919c02..6bda183f41 100644 --- a/core/continuum-engine/src/capacity/window_grant/lease/fair/diagnosis.rs +++ b/core/continuum-engine/src/capacity/window_grant/lease/fair/diagnosis.rs @@ -105,7 +105,7 @@ mod tests { ], ); state.receipts.insert(holder, (None, 8)); - let next = PoolGrants { pool_tokens: 100, per_sequence_cap: 100, by_mind: BTreeMap::from([(holder, 60)]), engine: None, revisions: BTreeMap::new() }; + let next = PoolGrants { pool_tokens: 100, per_sequence_cap: 100, by_mind: BTreeMap::from([(holder, 60)]), engine: None, revisions: BTreeMap::new(), owed: BTreeMap::new() }; let snapshot = DispatchDiagnosis::capture(&dispatch, &state, &next); assert_eq!(snapshot.owed, vec![(waiter, 3)]); let held = &snapshot.holders[&holder]; diff --git a/core/continuum-serving/src/modules/serving_daemon/live_pool.rs b/core/continuum-serving/src/modules/serving_daemon/live_pool.rs index 37b84485cb..1e166a4944 100644 --- a/core/continuum-serving/src/modules/serving_daemon/live_pool.rs +++ b/core/continuum-serving/src/modules/serving_daemon/live_pool.rs @@ -342,7 +342,7 @@ mod tests { let kv = NonZeroU64::new(M5_KV_PER_TOKEN).expect("test: a nonzero cost"); // expect: a positive literal let served = NonZeroU32::new(480_430).expect("test: the M5's pool after #5989"); // expect: a positive literal let (her, other) = (1, 2); - let granted = PoolGrants { pool_tokens: served.get(), per_sequence_cap: 262_144, by_mind: [(uuid::Uuid::from_u128(her), 140_000)].into(), engine: None, revisions: Default::default() }; + let granted = PoolGrants { pool_tokens: served.get(), per_sequence_cap: 262_144, by_mind: [(uuid::Uuid::from_u128(her), 140_000)].into(), engine: None, revisions: Default::default(), owed: Default::default() }; let demands = [demand(her, 100_000, true), demand(other, 60_000, false), demand(3, 40_000, false)]; let floor = need_floor(&demands, Some(&granted)); assert_eq!(floor, 140_000 + 60_000, "her act in flight keeps the grant it began on; the largest waiting need is held for the next seat, the smaller waiter holds no standing share"); From 1480c325bfd6e7d05da27a0332e1073fa3ce99f0 Mon Sep 17 00:00:00 2001 From: joelteply Date: Sat, 21 Nov 2026 23:29:53 -0600 Subject: [PATCH 2/2] window_grant tests: the cohort-rank test sits above the existing test's 'what this catches', not between it and its fn (Fable on #6218) Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE --- core/continuum-engine/src/capacity/window_grant.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/continuum-engine/src/capacity/window_grant.rs b/core/continuum-engine/src/capacity/window_grant.rs index dca11334d1..1e0d7b7285 100644 --- a/core/continuum-engine/src/capacity/window_grant.rs +++ b/core/continuum-engine/src/capacity/window_grant.rs @@ -650,9 +650,6 @@ mod tests { Demand { mind, need: t(need), target: t(need), wish: t(need), in_flight, settled: true, ceiling } } - // what this catches: a window divided by a count (Joel, 2026-11-03: "What a mind needs not what - // you dictate"). Minds that fit get exactly what they need, never an equal split, never past one - // sequence's ceiling; what is left stays in the pool, granted to no one. // what this catches (Codex on card 06254dcc): a waiting mind's place read wrong, which her grant // wait counts as progress. Minds owed at the same publication are one cohort, ordered by id as the // dispatch owner iterates them; a mind not owed has no rank, which is not rank zero. @@ -667,6 +664,9 @@ mod tests { assert_eq!(grants.owed_rank(mind(5)), None, "not owed: no rank, never rank zero"); } + // what this catches: a window divided by a count (Joel, 2026-11-03: "What a mind needs not what + // you dictate"). Minds that fit get exactly what they need, never an equal split, never past one + // sequence's ceiling; what is left stays in the pool, granted to no one. #[test] fn every_mind_gets_what_she_needs_and_the_rest_stays_in_the_pool() { let pool = 257_536;