Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ impl LlmDeliberationFaculty {
// excess and the grant she then waits for is too small (Fable's audit, 2026-11-09).
let (_, calibration) = self.prompt_fit(&loaded);
let _measured = self.prompt_view_with_visual_feedback(ws, loaded.context_window, calibration, None);
// Her wait's named bound, derived from the queue she just joined (card 250d7603): measured
// lanes size it, unmeasured take the ceiling - never a constant where measurement exists.
crate::inference::resource_admission::grants_wait_bound()
},
crate::capacity::mind_window::evaluation_after,
))
Expand Down
140 changes: 121 additions & 19 deletions core/continuum-engine/src/capacity/mind_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ pub fn mind_window(persona: uuid::Uuid, adapter: &dyn AIProviderAdapter, bound:

/// Decide her window and, when it is a grant, hold it for the act about to render against it
/// ([`super::window_grant::ActHold`]). A grant taken between the decision and the hold is waited for (the next grants
/// evaluation, then she decides again), never a failed turn (card d50e8e66).
/// evaluation, then she decides again), never a failed turn (card d50e8e66). Her measure's return names the bound
/// her ungranted wait is held under ([`decide_window`]).
pub async fn decide_and_hold<D, M, E, F>(persona: uuid::Uuid, decide: D, mut measure: M, evaluation_after: E) -> Result<(Decided, Option<super::window_grant::ActHold>), WindowRefused>
where
D: Fn() -> MindWindow,
M: FnMut(),
M: FnMut() -> std::time::Duration,
E: Fn(std::time::Instant) -> Result<F, GrantsUnavailable>,
F: std::future::Future<Output = Result<super::window_grant::GrantEvaluation, WindowRefused>>,
{
Expand Down Expand Up @@ -193,22 +194,30 @@ fn next_evaluation(
/// evaluation with her demand standing: on the M5 (26960c09d) each queued turn was refused and
/// re-measured instead, 186 failed turns an hour. Any other reason refuses her turn with the
/// evaluation's own words, so a defect is named within one publisher cadence, never parked (card
/// 5e79e1c1). No evaluations are a refusal only when she is ungranted: a decided window needs none.
/// 5e79e1c1). Waiting for ROOM across evaluations carries its own total bound - her measure names it
/// when the wait begins, and a wait that outlives it defers with this evaluation's own words instead of
/// parking to the act's tick floor (card 250d7603).
/// No evaluations are a refusal only when she is ungranted: a decided window needs none.
pub async fn decide_window<D, M, E, F>(persona: uuid::Uuid, decide: D, mut measure: M, evaluation_after: E) -> Result<Decided, WindowRefused>
where
D: Fn() -> MindWindow,
M: FnMut(),
M: FnMut() -> std::time::Duration,
E: Fn(std::time::Instant) -> Result<F, GrantsUnavailable>,
F: std::future::Future<Output = Result<super::window_grant::GrantEvaluation, WindowRefused>>,
{
let mut measured = false;
// When her ungranted wait began, and under the bound her measure named for it: a wait that outlives
// that bound is starvation, not queuing - she defers with the publisher's words at that moment instead
// of parking to the act's tick floor (card 250d7603). Measured on the runtime clock: story time under
// start_paused, real monotonic in production.
let mut waiting: Option<(tokio::time::Instant, std::time::Duration)> = None;
loop {
match decide() {
MindWindow::Decided(decided) => return Ok(decided),
MindWindow::Ungranted => {
let first = !measured;
if first {
measure();
waiting = Some((tokio::time::Instant::now(), measure()));
measured = true;
}
// After her measure: only an evaluation that began later has read her demand.
Expand All @@ -221,12 +230,19 @@ where
let evaluation = evaluation_after(asked)?.await?;
match evaluation.ungranted(persona) {
Some(why) if !why.awaits_room() => return Err(WindowRefused::Ungranted(why)),
Some(why) => continuum_base::probe!(
class = "delib.window.waiting",
persona = %persona,
reason = %why,
"her turn waits for a later grants evaluation"
),
Some(why) => {
// Her named bound across evaluations: outlived, the wait is starvation - defer now with
// this evaluation's own words (card 250d7603).
if waiting.as_ref().is_some_and(|(since, bound)| since.elapsed() >= *bound) {
return Err(WindowRefused::Ungranted(why));
}
continuum_base::probe!(
class = "delib.window.waiting",
persona = %persona,
reason = %why,
"her turn waits for a later grants evaluation"
);
},
// Her grant is listed but she is ungranted: it is being reclaimed for a ready mind.
// She holds nothing the reclaim takes (`Ungranted::awaits_room`), so she waits for
// the evaluation after it, within her turn.
Expand All @@ -246,6 +262,7 @@ where
mod tests {
use super::*;
use crate::inference::planning::window_allocator::test_tokens as t;
use crate::inference::resource_admission::LANE_WAIT_CEILING;
use std::cell::Cell;

fn grants(by_mind: &[(uuid::Uuid, u32)]) -> Option<super::super::window_grant::PoolGrants> {
Expand Down Expand Up @@ -308,7 +325,7 @@ mod tests {
false => MindWindow::Ungranted,
true => MindWindow::Decided(Decided::Granted(t(14_860))),
},
|| measured.set(measured.get() + 1),
|| { measured.set(measured.get() + 1); LANE_WAIT_CEILING },
|at| Ok(next_evaluation(installed.clone(), at)),
)
.await;
Expand All @@ -332,7 +349,7 @@ mod tests {
});
let refused = tokio::time::timeout(
std::time::Duration::from_secs(5),
decide_window(her, || MindWindow::Ungranted, || {}, |at| Ok(next_evaluation(installed.clone(), at))),
decide_window(her, || MindWindow::Ungranted, || LANE_WAIT_CEILING, |at| Ok(next_evaluation(installed.clone(), at))),
)
.await
.expect("one evaluation ends the wait");
Expand All @@ -358,7 +375,7 @@ mod tests {
false => MindWindow::Ungranted,
true => MindWindow::Decided(Decided::Granted(t(30_000))),
},
|| measured.set(measured.get() + 1),
|| { measured.set(measured.get() + 1); LANE_WAIT_CEILING },
|at| {
evaluations.set(evaluations.get() + 1);
reseated.set(evaluations.get() >= 2);
Expand Down Expand Up @@ -410,7 +427,7 @@ mod tests {
false => MindWindow::Ungranted,
true => MindWindow::Decided(Decided::Granted(t(14_860))),
},
|| measured.set(measured.get() + 1),
|| { measured.set(measured.get() + 1); LANE_WAIT_CEILING },
|at| Ok(next_evaluation(installed.clone(), at)),
),
)
Expand Down Expand Up @@ -439,14 +456,14 @@ mod tests {
let (_sender, installed) = tokio::sync::watch::channel(Some(stale));
let refused = tokio::time::timeout(
std::time::Duration::from_secs(5),
decide_window(her, || MindWindow::Ungranted, || {}, |at| Ok(next_evaluation(installed.clone(), at))),
decide_window(her, || MindWindow::Ungranted, || LANE_WAIT_CEILING, |at| Ok(next_evaluation(installed.clone(), at))),
)
.await
.expect("bounded by the publisher's cadence");
assert!(matches!(refused, Err(WindowRefused::PublisherSilent { silent_for: Some(_) })), "{refused:?}");

let (_sender, never) = tokio::sync::watch::channel(None);
let refused = decide_window(her, || MindWindow::Ungranted, || {}, |at| Ok(next_evaluation(never.clone(), at))).await;
let refused = decide_window(her, || MindWindow::Ungranted, || LANE_WAIT_CEILING, |at| Ok(next_evaluation(never.clone(), at))).await;
assert_eq!(refused, Err(WindowRefused::PublisherSilent { silent_for: None }));
}

Expand All @@ -456,9 +473,94 @@ mod tests {
async fn no_evaluations_refuse_only_an_ungranted_mind() {
type Never = std::future::Ready<Result<super::super::window_grant::GrantEvaluation, WindowRefused>>;
let her = uuid::Uuid::from_u128(4);
let refused = decide_window(her, || MindWindow::Ungranted, || {}, |_| Err::<Never, _>(GrantsUnavailable)).await;
let refused = decide_window(her, || MindWindow::Ungranted, || LANE_WAIT_CEILING, |_| Err::<Never, _>(GrantsUnavailable)).await;
assert_eq!(refused, Err(WindowRefused::Unavailable(GrantsUnavailable)));
let bound = decide_window(her, || MindWindow::Decided(Decided::Bound(8_192)), || {}, |_| Err::<Never, _>(GrantsUnavailable)).await;
let bound = decide_window(her, || MindWindow::Decided(Decided::Bound(8_192)), || LANE_WAIT_CEILING, |_| Err::<Never, _>(GrantsUnavailable)).await;
assert_eq!(bound, Ok(Decided::Bound(8_192)), "a bound window needs no evaluations");
}

// what this catches (card 250d7603; observed live on engine pid 33236, build 4f96d32b): a publisher
// that keeps evaluating on its own cadence and keeps reporting her demand as Waiting::BehindActs
// grants her nothing. Each single wait is bounded by the publisher's cadence (PublisherSilent), but
// without a TOTAL bound of its own the loop across evaluations parks her to the act's TICK_DEADLINE
// floor (1500s observed; lanes physically free: 2 of 3) instead of deferring at her named bound with
// her act budget intact. The orphan-acting defect that keeps BehindActs true forever is pinned
// separately as its own evidence in fair/tests.rs; this pair does not depend on it - ANY publisher
// behavior that keeps her ungranted across evaluations must end in a typed deferral at the bound her
// measure named, never at the tick floor. The bound itself is derived like lane_wait_bound from the
// queue she joins (card question 2): measured lanes size it, unmeasured take the ceiling - so both
// cases below drive the real derivation with known inputs and assert the deferral EXACT in story time
// (the beats are event-driven under start_paused; a bound that is a multiple of the beat lands on an
// exact evaluation).
/// Her starved scenario, run to its end: a healthy publisher beating every half second of story time
/// while she stays BehindActs. Returns her refusal, how many times she was measured, and the story
/// time it took - so each case can assert where exactly she deferred.
async fn starved_until_refused(bound: std::time::Duration) -> (Result<Decided, WindowRefused>, u32, std::time::Duration) {
use super::super::window_grant::Waiting;
let her = uuid::Uuid::from_u128(4);
// The publisher's healthy beat, in story time: evaluations keep flowing (the live seat
// re-reported BehindActs hundreds of times an hour), so PublisherSilent never fires on her.
let beat = std::time::Duration::from_millis(500);
let behind_acts = move |at: std::time::Instant| {
let mut e = evaluation(at, grants(&[]), None).expect("evaluation");
// Generous cadence: a single wait's PublisherSilent bound (2x cadence) must never fire
// inside her outer bound - this pin isolates the total bound across evaluations, not
// publisher silence (covered by a_publisher_that_misses_its_cadence_is_named_not_waited_out).
e.cadence = std::time::Duration::from_secs(3600);
e.waiting.insert(her, Waiting::BehindActs { acting: 2 });
e
};
let (sender, installed) = tokio::sync::watch::channel(Some(behind_acts(std::time::Instant::now())));
let publisher = tokio::spawn(async move {
loop {
// Her turn first - she processes the previous evaluation or waits for this one - then exactly
// one healthy beat of story time, advanced by name (the house idiom under start_paused; rule 10
// never sleeps): each evaluation arrives as its own event and no burst can overshoot her bound.
tokio::task::yield_now().await;
tokio::time::advance(beat).await;
if sender.send(Some(behind_acts(std::time::Instant::now()))).is_err() {
return; // the waiter is gone: her bound fired, or this test gave up on her
}
}
});
let measured = Cell::new(0);
let started_at = tokio::time::Instant::now(); // story time: the controlled clock
let refused = tokio::time::timeout(
bound + beat * 2,
decide_window(her, || MindWindow::Ungranted, || { measured.set(measured.get() + 1); bound }, |at| {
// The clone here is right and not a borrow: next_evaluation keeps its receiver across the wait for the evaluation after `asked`, so each of her passes needs an owned handle while installed stays behind for the pass after that.
let rx = installed.clone();
Ok(next_evaluation(rx, at))
}),
)
.await
.expect("she defers within her named bound instead of parking to the tick floor (card 250d7603)");
publisher.abort();
(refused, measured.get(), started_at.elapsed())
}

#[tokio::test(start_paused = true)]
async fn a_starved_mind_defers_at_her_named_bound_not_the_tick_floor() {
// The queue she joins, measured: three held-work callers ahead of her on two lanes whose turns the
// lanes have timed (p50 120 s over twelve samples) - the derivation's own test inputs.
let bound = crate::inference::resource_admission::lane_wait_bound(3, 2, 120_000, 12)
.unwrap_or(LANE_WAIT_CEILING); // unwrap_or: unmeasured lanes use the ceiling (admission.rs's own policy)
let (refused, measured, elapsed) = starved_until_refused(bound).await;
assert!(matches!(refused, Err(WindowRefused::Ungranted(_))), "a typed deferral - her next tick measures and asks again; nothing was dispatched: {refused:?}");
assert_eq!(measured, 1, "her demand stood while she waited: measured once");
assert_eq!(elapsed, bound, "exact in story time: deferred at the derived bound ({bound:?}), not the act's tick floor (25 min)");
}

#[tokio::test(start_paused = true)]
async fn a_starved_mind_defers_at_the_ceiling_when_the_queue_is_unmeasured() {
// Unmeasured lanes: no holds timed yet, so the derivation names no bound and she waits under the
// ceiling - unknown is not a quantity, but it is still HER OWN named bound under the tick floor.
let bound = crate::inference::resource_admission::lane_wait_bound(0, 0, 0, 0)
.unwrap_or(LANE_WAIT_CEILING); // unwrap_or: unmeasured lanes use the existing diagnostic ceiling
assert_eq!(bound, LANE_WAIT_CEILING, "unmeasured: the derivation names no bound and the ceiling stands");
let (refused, measured, elapsed) = starved_until_refused(bound).await;
assert!(matches!(refused, Err(WindowRefused::Ungranted(_))), "a typed deferral - her next tick measures and asks again: {refused:?}");
assert_eq!(measured, 1, "her demand stood while she waited: measured once");
assert_eq!(elapsed, bound, "exact at the ceiling ({bound:?}), not the act's tick floor (25 min)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,35 @@ fn resizing_a_continuously_held_seat_preserves_its_earned_yield() {
assert!(bench.owner.hold_act(big, || Some(next)).is_none());
}
}

// what this catches (card 250d7603; observed live on engine pid 33236, build 4f96d32b and still at
// canary tip 978e3f16): a request that ends without its terminal receipt leaves one Unresolved
// custody entry behind (settle with no receipt -> Drop -> abandon), and such entries clear only at an
// engine fence. The reclaim candidate filter (`!state.entries.contains_key(mind)`) treats a holder
// with even one orphan as acting forever: she is armed every evaluation yet never reclaimed, so the
// waiter behind her waits BehindActs until the 1500s act floor, over and over - no named bound. In
// the wild this sat for 8h+ (256 evaluations, owed fixed at one publication seq while the holder's
// unresolved entries grew 7 -> 10 and her terminal receipts flowed). Unresolved means nothing is at
// the engine and no one holds the reservation handle; a reclaim's physical safety rides on the
// engine's save-and-erase receipt (this path issues its tickets directly), not on this bookkeeping.
#[test]
#[ignore = "pins card 250d7603: an orphan-only holder blocks seat turnover for the engine's life; un-ignore with the fix"]
fn an_orphan_only_holder_does_not_hold_the_seat_forever() {
let mut bench = Bench::new();
let (a, b) = (mind(1), mind(2));
// a holds the whole pool: b's only seat to take is a's.
bench.publish(&[demand(a, 100, 100)], 100);
bench.serve(a);
let lease = bench.lease(a);
// One of a's requests ends without its terminal receipt (a cut stream): the reservation is
// dropped, leaving exactly one Unresolved entry - nothing at the engine, no handle held.
drop(bench.owner.reserve_against(a, lease, 10, || Some(lease)).expect("test: reserved")); // test: her own grant
let demands = [demand(a, 100, 100), demand(b, 90, 100)];
for _ in 0..3 {
let tickets = bench.publish(&demands, 100);
for ticket in tickets {
bench.owner.lock().reclaimed.insert(ticket.persona(), ticket.lease()); // test: the engine reclaimed it
}
}
assert_eq!(bench.grants().get(&b), Some(&90), "the waiter seats once the holder's acts are all orphans");
}
11 changes: 11 additions & 0 deletions core/continuum-engine/src/inference/resource_admission/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,17 @@ pub fn lane_wait_bound(
.clamp(LANE_WAIT_FLOOR, LANE_WAIT_CEILING),
)
}
/// The bound an ungranted mind waits under ACROSS grants evaluations while she is queued here,
/// derived from the queue she is joining exactly like [`lane_wait_bound`]: her place in line over
/// the lanes' measured turn time. Unmeasured lanes take the ceiling, because a node that has never
/// timed a lane may not guess a short one ([[unknown-is-not-a-quantity]]). A wait that outlives it
/// is starvation, not queuing: she defers at her own named bound with her act budget intact instead
/// of parking to the act's tick floor (card 250d7603).
pub fn grants_wait_bound() -> std::time::Duration {
let (hold_p50_ms, hold_samples) = lane_hold_p50_ms();
lane_wait_bound(work_waiting_now(), served_lane_count(), hold_p50_ms, hold_samples)
.unwrap_or(LANE_WAIT_CEILING) // unwrap_or: unmeasured lanes use the existing diagnostic ceiling
}
/// PURE: may a WORK call be lent the lane reserved for directed calls? Whenever no directed
/// line is pending here. A directed arrival then takes the next lane any call releases.
///
Expand Down
Loading