gam_solve/loop_guard.rs
1//! Certified termination (#968): ONE exhaustion/stagnation policy for
2//! every damped inner loop.
3//!
4//! # The bug genus this kills
5//!
6//! Every hang in the tracker's history (#874, #789, #683, #744, the
7//! survival-AFT cluster, #826's 42-minute frozen-residual stall) traces to
8//! the same structural flaw: termination safety was a per-branch,
9//! hand-replicated convention. The #874 postmortem is the canonical
10//! specimen — the LM *gain-reject* branch lacked the exhaustion guard its
11//! sibling *screening-reject* branch in the SAME file already had. Guard
12//! drift between sibling branches is the control-flow twin of the
13//! objective↔gradient desync class, and the cure is the same: a single
14//! source of truth that branches consume and cannot locally re-derive.
15//!
16//! # The policy pieces
17//!
18//! [`madsen_can_retry`] / [`madsen_retry_exhausted`] own the damped-retry
19//! exhaustion question for Madsen-style Levenberg–Marquardt loops: a retry
20//! is alive while the damping is finite and below [`MADSEN_DAMPING_CAP`],
21//! and dead once attempts run out or damping leaves that window. Both
22//! engines (reweight.rs Madsen-LM and the custom_family.rs spectral
23//! Newton) must answer this question through these functions — never
24//! through a local predicate.
25//!
26//! [`IterationBound`] and [`RejectEscalator`] are the two *distinct*
27//! safety mechanisms of an unbounded damped-retry loop, kept as two types
28//! on purpose. The bound owns the per-iteration hard count: it ticks once
29//! at the top of EVERY pass — including `continue` paths that neither
30//! accept a step nor reach a reject ritual (Fisher fallback, special
31//! cases) — and is the net that makes an unbounded `loop {}` safe. The
32//! escalator owns the geometric damping discipline applied on REJECTS
33//! only. A single type coupling "count++" to "reject" would either
34//! double-count iterations or silently assume every non-accepting pass
35//! reaches a reject ritual — the exact unbounded-loop hole the guard
36//! exists to close (see the #968 thread's design note).
37//!
38//! [`FlatStreak`] owns the consecutive-window discipline every stagnation
39//! detector shares: a streak that grows on "flat" readings, resets on
40//! recovery, and fires once it spans the window. Loops that own a
41//! scale-aware flatness predicate of their own (the custom_family
42//! joint-Newton objective-flat counter, the blockwise frozen-loglik
43//! divergence detector) consume it directly — they answer the question
44//! attempt caps cannot see: a loop that still "makes progress" every
45//! iteration but whose MERIT is frozen. #744 ran to cycle 1199/1200 at a
46//! flat residual; #826 burned a CI timeout on a frozen joint residual. The
47//! caller feeds its descent quantity (penalized NLL, residual norm, |g|)
48//! through its own flatness predicate once per iteration; the streak
49//! reports a plateau once flat readings span a consecutive window — long
50//! before any iteration cap.
51//!
52//! # Verdicts, not panics
53//!
54//! Exhaustion is an escalation event: the consuming loop converts
55//! [`LoopVerdict::Plateaued`] / [`LoopVerdict::Exhausted`] into its
56//! honest terminal status (`StalledAtValidMinimum`,
57//! `LmStepSearchExhausted`, …) and unwinds. Never a hang, never a panic,
58//! never a silent wrong answer.
59//!
60//! # Migration map (each step deleted a hand-rolled guard)
61//!
62//! 1. (done) reweight.rs `lm_can_retry`/`lm_retry_exhausted` local fns +
63//! the local `LM_MAX_LAMBDA` const deleted; call sites consume this
64//! module's policy.
65//! 2. (done) The 7 copies of the reweight.rs reject ritual
66//! (`loop_lambda *= factor; factor *= 2.0; continue`) collapsed onto
67//! [`RejectEscalator::escalate`], and the per-iteration hard count
68//! moved into [`IterationBound`], so neither discipline can drift
69//! per-branch.
70//! 3. (done) custom_family.rs: the joint-Newton objective-flat counter
71//! and the blockwise frozen-loglik divergence streak both ride
72//! [`FlatStreak`] — the #826-class exit discipline now lives here, not
73//! in per-loop counters. The richer certificate machinery those loops
74//! layer on top (geometric-tail bound, clamped-step side condition)
75//! stays local: it is *policy about what counts as flat*, which the
76//! loops rightly own; the streak/window discipline is what must not
77//! fork.
78//! 4. (dropped) Terminal-verdict reporting into heartbeat scopes: the
79//! `[JN-EXIT]`/`[PIRLS]` per-exit log lines already name why a loop
80//! ended; a parallel verdict channel in the process monitor would be
81//! redundant global state.
82
83/// Damping ceiling for Madsen-style LM retries. Beyond this the proposed
84/// step is numerically a zero step — retrying cannot make progress, so the
85/// retry chain is declared dead. (Moved verbatim from reweight.rs, where it
86/// was a file-local convention; see module docs for why it must be shared.)
87pub const MADSEN_DAMPING_CAP: f64 = 1e12;
88
89/// Default consecutive-window length for a [`FlatStreak`] stagnation
90/// detector: how many successive flat readings must accumulate before the
91/// loop is declared plateaued. Two is the established in-tree streak
92/// convention (reweight.rs soft-acceptance) — one noisy reading can fake a
93/// plateau, two consecutive cannot — plus one for the headroom a merit that
94/// is genuinely creeping (not frozen) needs to escape.
95pub const PLATEAU_DEFAULT_WINDOW: usize = 3;
96
97/// Is a damped retry still alive at this damping level?
98#[inline]
99pub fn madsen_can_retry(damping: f64) -> bool {
100 damping.is_finite() && damping < MADSEN_DAMPING_CAP
101}
102
103/// Has the retry chain exhausted its budget — by attempt count or by the
104/// damping leaving the productive window?
105#[inline]
106pub fn madsen_retry_exhausted(damping: f64, attempts: usize, max_attempts: usize) -> bool {
107 attempts >= max_attempts || !damping.is_finite() || damping > MADSEN_DAMPING_CAP
108}
109
110/// Terminal verdict of a guarded loop. `Continue` is the only
111/// non-terminal answer; the two terminal verdicts are ESCALATION events
112/// the consumer must convert into an honest status, never swallow.
113#[derive(Clone, Copy, Debug, PartialEq, Eq)]
114pub enum LoopVerdict {
115 Continue,
116 /// The merit stream is frozen: stop and report the current iterate as
117 /// the honest answer (StalledAtValidMinimum if KKT-near, else a named
118 /// stall) instead of grinding out the remaining budget (#744, #826).
119 Plateaued,
120 /// Attempts or damping window exhausted (#874's missing branch guard).
121 Exhausted,
122}
123
124/// Consecutive-flatness streak: the window discipline shared by every
125/// stagnation detector in the tree. The caller owns the flatness
126/// predicate (scale-aware objective tolerance, frozen log-likelihood,
127/// sub-tolerance relative improvement, …); this type owns the part that
128/// historically forked per loop — grow on flat, reset on recovery, fire
129/// once the streak spans the window, and keep firing while it persists.
130#[derive(Clone, Debug)]
131pub struct FlatStreak {
132 window: usize,
133 streak: usize,
134}
135
136impl FlatStreak {
137 pub fn new(window: usize) -> Self {
138 Self {
139 window: window.max(1),
140 streak: 0,
141 }
142 }
143
144 /// Record one pre-judged flatness reading; returns the current verdict.
145 pub fn note(&mut self, flat: bool) -> LoopVerdict {
146 if flat {
147 self.streak += 1;
148 if self.streak >= self.window {
149 return LoopVerdict::Plateaued;
150 }
151 } else {
152 self.streak = 0;
153 }
154 LoopVerdict::Continue
155 }
156
157 /// Hard reset, e.g. after a non-finite merit re-baselines the stream.
158 pub fn reset(&mut self) {
159 self.streak = 0;
160 }
161
162 /// Current consecutive-flat count (diagnostic; the verdict is the
163 /// contract).
164 pub fn streak(&self) -> usize {
165 self.streak
166 }
167}
168
169/// Per-iteration hard bound for a damped retry loop: the net that makes
170/// an unbounded `loop {}` safe. Tick it once at the top of EVERY pass —
171/// accepted, rejected, or any `continue` path that reaches neither — and
172/// ask [`IterationBound::exhausted_at`] wherever the loop's exhaustion
173/// question is posed. Created fresh per outer iteration.
174#[derive(Clone, Debug)]
175pub struct IterationBound {
176 used: usize,
177 max: usize,
178}
179
180impl IterationBound {
181 pub fn new(max: usize) -> Self {
182 Self {
183 used: 0,
184 max: max.max(1),
185 }
186 }
187
188 /// Count one loop pass. Top-of-loop, unconditionally.
189 pub fn tick(&mut self) {
190 self.used += 1;
191 }
192
193 /// Passes counted so far (diagnostics: `last_step_halving`, logs).
194 pub fn used(&self) -> usize {
195 self.used
196 }
197
198 /// The configured cap (diagnostics).
199 pub fn max(&self) -> usize {
200 self.max
201 }
202
203 /// Has the pass count alone exhausted the budget?
204 pub fn count_exhausted(&self) -> bool {
205 self.used >= self.max
206 }
207
208 /// The single exhaustion question: count OR damping window
209 /// ([`madsen_retry_exhausted`], answered from owned state).
210 pub fn exhausted_at(&self, damping: f64) -> bool {
211 madsen_retry_exhausted(damping, self.used, self.max)
212 }
213
214 /// [`IterationBound::exhausted_at`] as a verdict, for consumers that
215 /// speak [`LoopVerdict`].
216 pub fn verdict_at(&self, damping: f64) -> LoopVerdict {
217 if self.exhausted_at(damping) {
218 LoopVerdict::Exhausted
219 } else {
220 LoopVerdict::Continue
221 }
222 }
223}
224
225/// Initial damping multiplier on the first rejection of an iteration.
226/// Doubles on every further rejection (geometric escalation), reaching
227/// [`MADSEN_DAMPING_CAP`] from λ = 1 in ~12 rejections — the established
228/// reweight.rs schedule, now owned here.
229pub const MADSEN_INITIAL_REJECT_FACTOR: f64 = 2.0;
230
231/// Geometric damping escalator for one reject chain
232/// (Madsen–Nielsen–Tingleff eq 3.16: the multiplier starts at 2 and
233/// doubles on every rejection, so successive bumps are ×2, ×4, ×8, …).
234/// Owns the factor and the reject count as one indivisible discipline —
235/// no branch can bump the damping without advancing the schedule, the
236/// drift mode behind #874. Deliberately does NOT own the per-iteration
237/// count; that is [`IterationBound`]'s job (see module docs for why the
238/// two must not be one type).
239#[derive(Clone, Debug)]
240pub struct RejectEscalator {
241 factor: f64,
242 rejects: usize,
243}
244
245impl Default for RejectEscalator {
246 fn default() -> Self {
247 Self::new()
248 }
249}
250
251impl RejectEscalator {
252 pub fn new() -> Self {
253 Self {
254 factor: MADSEN_INITIAL_REJECT_FACTOR,
255 rejects: 0,
256 }
257 }
258
259 /// Record a rejection: bumps the damping and advances the geometric
260 /// schedule in one indivisible step.
261 pub fn escalate(&mut self, damping: &mut f64) {
262 *damping *= self.factor;
263 self.factor *= 2.0;
264 self.rejects += 1;
265 }
266
267 /// Restart the schedule — the problem changed under the chain (e.g. a
268 /// Fisher fallback swapped the Hessian curvature), so the trajectory
269 /// begins anew. Pairs with the caller resetting its damping baseline.
270 pub fn restart(&mut self) {
271 self.factor = MADSEN_INITIAL_REJECT_FACTOR;
272 self.rejects = 0;
273 }
274
275 /// Rejections recorded since construction/restart (diagnostics).
276 pub fn rejects(&self) -> usize {
277 self.rejects
278 }
279}
280
281/// Convergence-truthfulness invariant for an inner-solve terminal verdict
282/// (gam#1040).
283///
284/// An inner Newton/PIRLS solve may only report `converged = true` if it
285/// actually certified a stationarity point on a FINITE residual. A
286/// certificate exit that fires on a cycle where the head-of-cycle KKT norm was
287/// non-finite (so the running `min_certified_residual` is left at its `inf`
288/// sentinel) would otherwise emit `converged=true … best_residual_inf=inf` — a
289/// self-contradicting status: a convergence claim with no finite residual
290/// behind it. This predicate is the single source of truth for that gate:
291/// `converged` survives iff a finite certified residual is on record. When it
292/// returns `false` while the solver believed it converged, the caller must
293/// downgrade to non-converged so the outer optimizer rejects the evaluation
294/// rather than consuming a phantom optimum.
295#[inline]
296pub fn inner_convergence_is_truthful(converged: bool, min_certified_residual: f64) -> bool {
297 !converged || min_certified_residual.is_finite()
298}
299
300/// Deterministic slow-geometric-rate stall predicate (gam#979 survival
301/// marginal-slope hang).
302///
303/// The survival marginal-slope oversmoothed-ρ endgame produces a stiff
304/// penalized Hessian (penalty dominates, eigenvalues ~1e6) whose Newton steps
305/// are ~1e-5 far INSIDE a large trust radius, so the inner KKT residual
306/// descends geometrically but very slowly (~0.99×/cycle, halving only every
307/// ~80 cycles). That is neither divergence nor a flat stall: the residual is
308/// genuinely shrinking, just far too slowly to reach `residual_tol` in a
309/// practical cycle count — so the flat-residual no-improve guard never latches
310/// (the residual clears its 10% bar every ~12 cycles) and the loop grinds ~10³
311/// cycles at ~p³ each, the measured #979 "hang".
312///
313/// Given the residual `window_oldest` cycles `window_cycles` ago and the
314/// `current` residual, this projects — from the per-cycle geometric rate
315/// `(current/window_oldest)^(1/window_cycles)` — how many additional cycles
316/// reaching `residual_tol` would take, and returns `true` when that exceeds
317/// `projection_cap` (i.e. the ρ-evaluation cannot finish in a practical
318/// budget). It is FULLY DETERMINISTIC: cycle indices and residual ratios only,
319/// no wall-clock. It also returns `true` when the window shows no net
320/// geometric progress at all (rate ≥ 1, or the window did not shrink), which
321/// likewise cannot reach tol.
322///
323/// A healthy (quadratically / fast-geometrically converging) solve reaches tol
324/// in a handful of cycles and never fills the window, and even when it does the
325/// projected remaining cycles are tiny, so this never fires on it. The caller
326/// uses a `true` verdict to stop with the current finite β as `converged=false`
327/// so the outer optimizer rejects this ρ and moves on; it certifies nothing and
328/// so cannot bias the envelope gradient.
329#[inline]
330pub fn slow_geometric_rate_exceeds_projection_cap(
331 current: f64,
332 window_oldest: f64,
333 window_cycles: usize,
334 residual_tol: f64,
335 projection_cap: usize,
336) -> bool {
337 if window_cycles == 0 {
338 return false;
339 }
340 if !current.is_finite() || current <= residual_tol {
341 // Either non-finite (a different guard owns that) or already at/under
342 // tol (the convergence certificate owns that): not a slow-rate stall.
343 return false;
344 }
345 if !window_oldest.is_finite() || window_oldest <= 0.0 || current >= window_oldest {
346 // No net geometric progress across the whole window: cannot reach tol.
347 return true;
348 }
349 let rate = (current / window_oldest).powf(1.0 / (window_cycles as f64));
350 if !rate.is_finite() || rate >= 1.0 {
351 return true;
352 }
353 let projected_cycles = (residual_tol / current).ln() / rate.ln();
354 projected_cycles.is_finite() && projected_cycles > projection_cap as f64
355}
356
357#[cfg(test)]
358mod tests {
359 use super::*;
360
361 /// #874 regression shape: a reject storm must reach the damping
362 /// ceiling in a bounded number of escalations no matter which branch
363 /// asks — the escalator owns the schedule, the predicates own the
364 /// window.
365 #[test]
366 fn reject_storm_exhausts_in_bounded_steps() {
367 let mut esc = RejectEscalator::new();
368 let mut damping = 1.0;
369 let mut steps = 0usize;
370 while madsen_can_retry(damping) {
371 esc.escalate(&mut damping);
372 steps += 1;
373 assert!(steps <= 64, "escalation must reach the damping cap");
374 }
375 // Geometric doubling of the factor reaches 1e12 in ~9 escalations.
376 assert!(steps <= 16, "escalation took {steps} steps");
377 assert_eq!(esc.rejects(), steps);
378 assert!(madsen_retry_exhausted(damping, 0, usize::MAX));
379 }
380
381 /// The design split the #968 thread demanded: a loop pass that never
382 /// reaches a reject ritual (Fisher fallback / special-case `continue`)
383 /// still burns the iteration budget, because the bound ticks at the
384 /// top of every pass — independent of the escalator.
385 #[test]
386 fn continue_paths_without_rejects_still_exhaust_the_bound() {
387 let mut bound = IterationBound::new(5);
388 let esc = RejectEscalator::new();
389 let damping = 1e-6; // benign forever: only the count can kill it
390 let mut passes = 0usize;
391 while !bound.exhausted_at(damping) {
392 bound.tick();
393 passes += 1;
394 assert!(passes <= 5, "bound must stop a reject-free spin");
395 // No escalate(): this pass `continue`d past every reject site.
396 }
397 assert_eq!(passes, 5);
398 assert_eq!(esc.rejects(), 0, "no reject was ever recorded");
399 assert_eq!(bound.verdict_at(damping), LoopVerdict::Exhausted);
400 }
401
402 /// And the dual: escalations do NOT advance the iteration bound on
403 /// their own — collapsing the rituals onto the escalator must not
404 /// double-count attempts against the per-iteration budget.
405 #[test]
406 fn escalations_do_not_double_count_iterations() {
407 let mut bound = IterationBound::new(10);
408 let mut esc = RejectEscalator::new();
409 let mut damping = 1.0;
410 bound.tick();
411 for _ in 0..3 {
412 esc.escalate(&mut damping);
413 }
414 assert_eq!(bound.used(), 1);
415 assert_eq!(esc.rejects(), 3);
416 assert!(!bound.count_exhausted());
417 }
418
419 #[test]
420 fn restart_rewinds_the_geometric_schedule() {
421 let mut esc = RejectEscalator::new();
422 let mut damping = 1.0;
423 esc.escalate(&mut damping); // ×2
424 esc.escalate(&mut damping); // ×4
425 assert_eq!(damping, 8.0);
426 esc.restart();
427 assert_eq!(esc.rejects(), 0);
428 let mut fresh = 1.0;
429 esc.escalate(&mut fresh);
430 assert_eq!(
431 fresh, MADSEN_INITIAL_REJECT_FACTOR,
432 "schedule restarts at ×2"
433 );
434 }
435
436 /// The streak discipline alone (caller-owned flatness predicate, the
437 /// custom_family consumption shape): grows on flat, resets on
438 /// recovery, fires at the window and keeps firing while flat.
439 #[test]
440 fn flat_streak_pins_the_window_discipline() {
441 let mut streak = FlatStreak::new(3);
442 assert_eq!(streak.note(true), LoopVerdict::Continue); // 1
443 assert_eq!(streak.note(true), LoopVerdict::Continue); // 2
444 assert_eq!(streak.note(false), LoopVerdict::Continue); // reset
445 assert_eq!(streak.streak(), 0);
446 assert_eq!(streak.note(true), LoopVerdict::Continue); // 1
447 assert_eq!(streak.note(true), LoopVerdict::Continue); // 2
448 assert_eq!(streak.note(true), LoopVerdict::Plateaued); // 3 fires
449 assert_eq!(streak.note(true), LoopVerdict::Plateaued); // persists
450 assert_eq!(streak.streak(), 4);
451 }
452
453 /// A certificate exit must never report `converged=true` while the only
454 /// residual on record is the non-finite `inf` sentinel — the gam#1040
455 /// inner-report truthfulness violation. The predicate downgrades exactly
456 /// that case and leaves every genuinely-certified exit untouched.
457 #[test]
458 fn inner_convergence_truthfulness_rejects_converged_with_nonfinite_residual() {
459 // converged with a finite certified residual: honest, survives.
460 assert!(inner_convergence_is_truthful(true, 8.0e-6));
461 assert!(inner_convergence_is_truthful(true, 0.0));
462 // converged with NO finite certified residual (the cycle-1 certificate
463 // exit symptom: best_residual_inf=inf): a truthfulness violation.
464 assert!(!inner_convergence_is_truthful(true, f64::INFINITY));
465 assert!(!inner_convergence_is_truthful(true, f64::NAN));
466 // non-converged exits are always truthful regardless of the residual
467 // sentinel — the report says "not converged", no contradiction.
468 assert!(inner_convergence_is_truthful(false, f64::INFINITY));
469 assert!(inner_convergence_is_truthful(false, 1.0e-3));
470 }
471
472 /// The shared predicates pin the exact reweight.rs semantics they
473 /// replaced (finite + strictly-below cap to retry; count OR window
474 /// exit to exhaust).
475 #[test]
476 fn policy_predicates_pin_the_reweight_semantics() {
477 assert!(madsen_can_retry(1e11));
478 assert!(!madsen_can_retry(MADSEN_DAMPING_CAP));
479 assert!(!madsen_can_retry(f64::INFINITY));
480 assert!(madsen_retry_exhausted(1.0, 5, 5));
481 assert!(madsen_retry_exhausted(f64::NAN, 0, 5));
482 assert!(madsen_retry_exhausted(1e13, 0, 5));
483 assert!(!madsen_retry_exhausted(1.0, 4, 5));
484 }
485
486 /// gam#979 survival marginal-slope: the slow-geometric-rate stall guard must
487 /// TERMINATE the inner joint-Newton in a bounded number of cycles on the
488 /// oversmoothed-ρ endgame (a residual crawling down by a fixed small factor
489 /// ~0.99×/cycle that would otherwise grind ~10³ cycles to the budget — the
490 /// measured hang) WITHOUT firing on a healthy fast-geometric solve. This
491 /// replays the production loop's window bookkeeping (the trailing window of
492 /// the last `LINEAR_RATE_WINDOW` post-step residuals, the guard armed only
493 /// after `MIN_CYCLES`) over a deterministic residual stream and asserts a
494 /// finite, bounded exit cycle — an iteration-count assertion, not a
495 /// wall-clock threshold.
496 #[test]
497 fn slow_geometric_stall_guard_terminates_in_bounded_cycles_979() {
498 // Mirror the production constants in inner_blockwise_fit.rs.
499 const LINEAR_RATE_WINDOW: usize = 16;
500 const LINEAR_RATE_PROJECTION_CAP: usize = 100;
501 const RESIDUAL_STALL_MIN_CYCLES: usize = 40;
502 // A representative inner cycle budget; the guard must exit FAR below it.
503 const INNER_BUDGET: usize = 1000;
504 let residual_tol = 1e-6_f64;
505
506 // Replay the production window: a VecDeque holding at most
507 // LINEAR_RATE_WINDOW+1 residuals (front = residual LINEAR_RATE_WINDOW
508 // cycles back), the guard armed only at/after MIN_CYCLES and once the
509 // window is full.
510 fn run_stream(
511 per_cycle_rate: f64,
512 residual_tol: f64,
513 window: usize,
514 min_cycles: usize,
515 cap: usize,
516 budget: usize,
517 ) -> (Option<usize>, bool) {
518 let mut history: std::collections::VecDeque<f64> =
519 std::collections::VecDeque::with_capacity(window + 1);
520 let mut residual = 1.0_f64; // start well above tol
521 let mut reached_tol = false;
522 for cycle in 0..budget {
523 // A genuine convergence certificate would have exited already.
524 if residual <= residual_tol {
525 reached_tol = true;
526 return (None, reached_tol);
527 }
528 if history.len() > window {
529 history.pop_front();
530 }
531 history.push_back(residual);
532 if cycle + 1 >= min_cycles && history.len() > window {
533 let oldest = *history.front().unwrap();
534 if slow_geometric_rate_exceeds_projection_cap(
535 residual,
536 oldest,
537 window,
538 residual_tol,
539 cap,
540 ) {
541 return (Some(cycle + 1), reached_tol);
542 }
543 }
544 residual *= per_cycle_rate;
545 }
546 (None, reached_tol)
547 }
548
549 // 1) The #979 hang signature: ~0.99×/cycle. Reaching 1e-6 from 1.0 at
550 // 0.99×/cycle would take ~1375 cycles (> the 1000 budget) — a hang.
551 // The guard must fire, and bounded: just past MIN_CYCLES once the
552 // window first fills, NOT at the budget.
553 let (slow_exit, slow_reached) = run_stream(
554 0.99,
555 residual_tol,
556 LINEAR_RATE_WINDOW,
557 RESIDUAL_STALL_MIN_CYCLES,
558 LINEAR_RATE_PROJECTION_CAP,
559 INNER_BUDGET,
560 );
561 assert!(
562 !slow_reached,
563 "the slow-geometric stream must not reach tol within budget (it is the hang)"
564 );
565 let slow_exit =
566 slow_exit.expect("slow-geometric stall guard must fire, not grind to the budget");
567 assert!(
568 slow_exit < INNER_BUDGET / 4,
569 "guard must terminate well below the {INNER_BUDGET}-cycle budget, fired at {slow_exit}"
570 );
571 // It can only arm at MIN_CYCLES, so the exit is bounded from both sides.
572 assert!(
573 slow_exit >= RESIDUAL_STALL_MIN_CYCLES,
574 "guard must not fire before it is armed (MIN_CYCLES={RESIDUAL_STALL_MIN_CYCLES})"
575 );
576
577 // 2) A healthy fast-geometric solve (~0.3×/cycle) reaches tol in a
578 // handful of cycles and NEVER reaches the armed window — the guard
579 // must never fire on it.
580 let (fast_exit, fast_reached) = run_stream(
581 0.3,
582 residual_tol,
583 LINEAR_RATE_WINDOW,
584 RESIDUAL_STALL_MIN_CYCLES,
585 LINEAR_RATE_PROJECTION_CAP,
586 INNER_BUDGET,
587 );
588 assert!(
589 fast_reached,
590 "a fast-geometric solve must reach tol (healthy convergence)"
591 );
592 assert!(
593 fast_exit.is_none(),
594 "the slow-rate guard must NEVER fire on a healthy fast-geometric solve"
595 );
596
597 // 3) Direct predicate properties at the boundary.
598 // No net progress across the window => fire (cannot reach tol).
599 assert!(slow_geometric_rate_exceeds_projection_cap(
600 1.0,
601 1.0,
602 LINEAR_RATE_WINDOW,
603 residual_tol,
604 LINEAR_RATE_PROJECTION_CAP
605 ));
606 // Residual already at/under tol => never fire (certificate owns it).
607 assert!(!slow_geometric_rate_exceeds_projection_cap(
608 1e-7,
609 1.0,
610 LINEAR_RATE_WINDOW,
611 residual_tol,
612 LINEAR_RATE_PROJECTION_CAP
613 ));
614 // A brisk window (0.5×/cycle over 16 cycles, residual 1e-3) projects to
615 // only ~10 more cycles to tol => never fire.
616 let brisk_oldest = 1e-3 / 0.5_f64.powi(LINEAR_RATE_WINDOW as i32);
617 assert!(!slow_geometric_rate_exceeds_projection_cap(
618 1e-3,
619 brisk_oldest,
620 LINEAR_RATE_WINDOW,
621 residual_tol,
622 LINEAR_RATE_PROJECTION_CAP
623 ));
624 }
625}