Skip to main content

borg/select/
policy.rs

1//! The learned policy's core: a Bayesian model of what each route of a graded apply is worth, fitted
2//! from the outcome log, and the decision it drives at one check. Pure: no file, no clock, no
3//! environment (an engineer's budget comes in as an argument).
4//!
5//! **The value model.** One logistic model of the judge's verdict "the lesson changed the work and the
6//! change avoids its consequence" (`asked_then_changed`), per graded apply j and route a: ask (Q),
7//! free (F) or not now (D, measured by the placebo arm, select.rs `PLACEBO_SLOTS`):
8//! - η_j = β₀ + β₂[g=2] + β₃[g=3] + β_rank·ln(1+rank) + β_stop[stop] + β_three[a 3 in the check]
9//!   + β_three_stop[a 3 in a stop's check] + β_pool·ln(tokens/12k) + u_session + w_lesson + γ_judge;
10//! - P(changed | a, m) = σ(η_j + δ_a + κ_a·ln m_a + interactions), δ_F = 0 (the reference), m_a the
11//!   number of applies the check routes to a (dilution: more questions, or more free context, make
12//!   each one less likely to change the work), κ_D = 0, interactions δ_Q×stop, δ_Q×[g=3], δ_D×(g−2).
13//!
14//! The fixed rule asks exactly when a check has a 3, so in its history a route term and the cell of
15//! checks it is asked in are one column. Each route term therefore has its twin among the effects that
16//! do not depend on the route (δ_Q: β_three; δ_Q×stop: β_three_stop; δ_Q×[g=3]: β₃): what the fixed
17//! rule's cells differ by goes to the twin, and a route term learns only from checks whose routes
18//! varied within a cell, never from the fixed rule's history alone.
19//!
20//! Fitted by penalised Newton (MAP) over the fixed effects and the session, lesson and judge effects
21//! together (the largest family eliminated exactly by its Schur complement), with the Laplace
22//! covariance; empirical Bayes (EM, type-II maximum likelihood) for the spread of each family and
23//! automatic relevance (ARD) for the terms without a dated prior (the rank slope, the pool slope and
24//! the interactions), so a weak feature gets weight only when the data support it. A term with a dated
25//! prior (ask and not now against free, `ASK_PRIOR`, `NOT_PRIOR`; the dilution slopes,
26//! `DILUTION_PRIOR`) keeps it: EM would shrink its sd wherever the data cannot tell the term from the
27//! others (under the fixed rule how many are asked or handed over is set by the check's size, never
28//! chosen), which is the data saying nothing, not the effect being small. Dilution and pool slopes are
29//! ≤ 0 (an active set). Priors in `coefs`. No importance weights for the routes: the randomisation
30//! depends only on logged covariates, so the design is ignorable. The judge's missingness is modelled
31//! (`judged`) and enters as truncated inverse-probability weights. Labels fade with age in sessions,
32//! 2^(−age/H), H chosen from `HALF_LIVES` by prequential likelihood over whole sessions (`half_life`).
33//! The fit runs at `borg learn` (select/summary.rs `refit`, main.rs `learn_store`, which a session's
34//! end starts detached), never in a hook. `fit_pruned` is P2's pre-registered pruned model, for
35//! evaluation only.
36//!
37//! **The decision** (`choose`, policy v2: docs/evidence.md, Rules v2). The value of route a for apply
38//! j is τ_j(a, m) = π_a·[P(changed | a, m) − P(changed | D)], with the learned delivery rate π (a stop's
39//! free lesson arrives only if a next prompt comes, a live question only if it reaches the drone; a
40//! stop's question always does, it blocks) (`Rates`). Not now is worth 0, at the stop and during the
41//! turn alike: a lesson left for later earns its value at the check that delivers it, and crediting it
42//! here too would count that delivery twice in the gate's ledger. A check takes one of three routings:
43//! - **On evidence** (`evident`): the routes maximising Σ_j τ_j (`solve`, exact) under the posterior
44//!   mean (`theta_mean`: the coefficients' and every level's mean, no draw), played only when they
45//!   beat the fixed rule at the `DELTA` quantile over the `DRAWS` joint draws (`gate_seed`, the ledger's
46//!   draws), a lesson the session already had never handed over free (its delivery would drop it).
47//!   Without evidence nothing passes the quantile, so the prior alone never leaves the fixed rule: the
48//!   effects the fixed rule's history cannot identify (how much each question dilutes the others, what
49//!   a lesson not delivered still changes) cannot decide from their priors. No Thompson play.
50//! - **The randomised question-count test**, on a live check (never the stop) whose evidence keeps the
51//!   fixed rule: of the fixed rule's k delivered non-3 lessons that may go free (`tested`), the first s
52//!   in its order are asked and the rest handed over, s uniform in 0..=k drawn from the check's seed
53//!   (`draw_count`). Nothing is withheld and every 3 is asked as the fixed rule asks it, so recall is
54//!   the fixed rule's: on a check with a 3 the hand-overs replace questions, and the free delivery
55//!   carries them past its character budget as the questions would have been carried (hook.rs
56//!   `INSTEAD`). The fixed rule is s = 0 without a 3 and s = k with one (`EXPERIMENT_CONTROL`).
57//!   It measures what the fixed rule's history never varies: how many questions to ask.
58//! - **The fixed rule** otherwise.
59//!
60//! The conservative gate (in `choose`) keeps the fixed rule unless the deviation leaves the replayed
61//! value of the policy's checks, at the `DELTA` quantile over `DRAWS` joint draws, at most α × the
62//! fixed rule's own replayed value below the fixed rule (select/summary.rs `Gate`), α the engineer's
63//! tolerance (`ALPHA` unless the collective sets `risk`, select.rs `risk_of`; 0 turns the test off):
64//! cold start is exactly the fixed rule, and the test is rationed by quality, not by an ε. A stop's
65//! grade 3 is always asked (`forced`). Propensities are exact: 0 or 1 on evidence and under the fixed
66//! rule, and under the test each unit's share of the k + 1 values of s that ask it or hand it over (a
67//! value the gate refuses counting as the fixed rule's routes). Spend is not an objective: only the
68//! pool's size has a price, and only under a budget an engineer set for the collective (`price`);
69//! without one the price is 0.
70//!
71//! **The monitor** (`monitor`) estimates, per session, the policy's value against the fixed rule
72//! (doubly robust, cross-fitted by session, truncated self-normalised weights) and keeps a time-uniform
73//! confidence sequence on it (`upper_bound`); when its bound shows harm beyond `ALPHA` of the fixed
74//! rule's value, the store reverts to the fixed rule for good (select/summary.rs `REVERTED`).
75
76use std::collections::{BTreeMap, HashMap};
77
78use serde::{Deserialize, Serialize};
79
80use super::summary::{Apply, Check as Logged, Gate, Label, Summary};
81use super::{ARMS, Route, Unit};
82use crate::conscience::{self, Challenge};
83use crate::memory::Rng;
84
85/// The share of the fixed rule's replayed quality (mistakes avoided) the learned policy may risk below
86/// it while it learns, at the gate (`choose`, unless the collective sets `risk`, select.rs `risk_of`)
87/// and in the monitor (`monitor`), and the share the gate's ledger accrues (`ledger_add_without`).
88/// Method: a bound on quality, not on cost; the engineer's tolerance for learning, stated once. Kept by
89/// dated decision (2026-10-31), with the allowance scaling with the fixed rule's modelled value: about one
90/// deviation in 9 to 10 checks in both main stores.
91pub const ALPHA: f64 = 0.10;
92/// The gate's and the evidence rule's quantile, and the monitor's error probability. Method: the
93/// conventional 95% confidence.
94pub const DELTA: f64 = 0.05;
95/// Joint posterior draws behind the gate's and the evidence rule's quantile: conscience.rs `P_DRAWS`
96/// (method, its standard error ≤ 0.022).
97pub const DRAWS: usize = conscience::P_DRAWS;
98/// Applies a check may have for the learned decision; a larger check takes the fixed rule (source
99/// `size`). Guard: bounds the exact solver's time per check (O(n³ log n), on the posterior mean's
100/// table) and the valuing of the evidence rule's and the test's routes (O(n) per draw, `DRAWS` draws,
101/// k + 2 routings); its hit rate is the share of `size` checks.
102pub const SOLVE_MAX: usize = 16;
103
104/// Prior sd of every identified effect (intercept, grades, channel, a 3 in the check, a 3 at the
105/// stop): N(0, 2.5²). Method: the weakly informative default for logistic coefficients (Gelman et al.
106/// 2008); also the ceiling of every learned spread.
107pub const PRIOR_SD: f64 = 2.5;
108/// Ask against free, N(0, 0.35²). Learned, dated prior: the review→fix evaluation of 2026-10 (asked
109/// +6.3, handed over +7.3 points against the plain agent) puts them within a few points; wide.
110pub const ASK_PRIOR: (f64, f64) = (0.0, 0.35);
111/// Not now against free, N(−0.3, 0.25²). Learned, dated prior: the same evaluation, a lesson not
112/// delivered changes less than one handed over. Fixed by dated decision (2026-10-31, docs/evidence.md):
113/// 8 placebos drawn, 1 judged, the fitted effect is this prior; withholding is not learned until 100
114/// judged placebos exist.
115pub const NOT_PRIOR: (f64, f64) = (-0.3, 0.25);
116/// Dilution slope per ln(m) of asks or free lessons, N(0, 0.1²) truncated ≤ 0 (half-normal, its mode
117/// at 0). Learned, dated prior: the 2026-10-22 estimate (0.04 [0, 0.12] less per extra question on the
118/// log-odds scale) has an interval that includes 0, so no dilution is assumed; its width sets the scale.
119pub const DILUTION_PRIOR: (f64, f64) = (0.0, 0.1);
120/// Starting sd of an ARD term, every term without a dated prior (rank, interactions, pool). Method: EM
121/// moves it.
122pub const ARD_START: f64 = 1.0;
123/// Starting spread of the session, lesson and judge effects. Method: EM moves it.
124pub const LEVEL_START: f64 = 0.5;
125/// Floor of every learned spread. Method: EM's update can never leave 0, and 0.01 on the log-odds
126/// scale is no effect.
127pub const SD_FLOOR: f64 = 0.01;
128/// Half-lives of a label's weight, in sessions, the forgetting is chosen from (None: none). Method: a
129/// doubling grid; a tie goes to the longer.
130pub const HALF_LIVES: [Option<u32>; 5] = [None, Some(64), Some(32), Some(16), Some(8)];
131/// Sessions a fit reads at most, the newest. Guard: bounds the fit's dense block (one effect per
132/// session) and its time; the labels it leaves out are counted (`Fit::dropped`).
133pub const FIT_SESSIONS: usize = 256;
134/// The largest inverse-probability weight of a judged label. Guard: a route the judge rarely reaches
135/// would otherwise let a handful of its labels outweigh the rest.
136pub const MAX_IPW: f64 = 10.0;
137/// The least chance a placebo that happened is weighed at (`monitor`, select/replay.rs P5). Method: a
138/// logged ψ of 0 for a drawn placebo is rounding (outcome.rs `round_p`), so it is read as 1/1000 at
139/// least: such a label weighs at most a thousand sure ones.
140pub const PSI_FLOOR: f64 = 1e-3;
141/// The pool budgets the policy chooses among: whole.rs `TOKENS` × 2^(i/2). Method: a √2 grid around
142/// the baseline.
143pub const POOL_STEPS: [i32; 5] = [-2, -1, 0, 1, 2];
144/// Latency observations needed before the pool may grow past the baseline. Method: with 20, the
145/// empirical 95% quantile has an observation above it.
146pub const LATENCY_MIN: usize = 20;
147/// The share of a budget a call must have read to count as latency observed at that budget
148/// (`PoolFit::latency_reach`, `pool_feasible`). Guard, dated 2026-10-31: a pool grows only to a budget
149/// its latency was measured at, never on a slope extrapolated past the calls (a team store's fit was
150/// flat, b = 0, and would have let a 16,971-token pool in on 12,000-token calls alone). At 0.9 a pool
151/// filled to its budget counts: it stops within a lesson of it (about 90 tokens there), and 193 of
152/// that store's 203 calls at the 12,000 baseline read 10,800 or more.
153pub const LATENCY_NEAR: f64 = 0.9;
154
155/// The labelled applies with leverage (the taken route's difference between the policy and the fixed
156/// rule is not 0) a route needs before the monitor uses its weighted residual (`monitor`); below it the
157/// direct-method term (the cross-fitted outcome model) carries the route alone. Guard, dated
158/// 2026-10-31: weights are normalised per route, so a route's only label carries the whole route (a
159/// team store's not-now route: 1 judged placebo at 42× the mean weight, ESS 9 of 127); at 5 one label
160/// is at most about a fifth of its route's residual. Labels without leverage do not count: they move
161/// no residual, so 5 of them would leave the next label with leverage carrying the route alone. Below it a route's residual is noise one verdict
162/// decides, so dropping it loses no evidence a confidence sequence could use.
163pub const ROUTE_LABELS_MIN: usize = 5;
164
165/// Newton's convergence: the largest step below this. Method.
166const TOL: f64 = 1e-8;
167/// Newton steps per fit at most. Guard: a fit that does not converge stops here (`Model::converged`).
168const MAX_NEWTON: usize = 50;
169/// Step halvings per Newton step at most. Guard: a step that cannot improve the objective (at the
170/// optimum, to float precision) ends the fit instead of halving forever.
171const MAX_HALVINGS: usize = 30;
172/// EM rounds of the spreads per fit at most. Guard: EM's slow crawl toward a spread of 0 stops here.
173const MAX_EB: usize = 50;
174/// EM's convergence: the largest relative change of a spread below this. Method.
175const EB_TOL: f64 = 1e-4;
176/// Sessions the prequential choice of the half-life predicts at most, the newest. Guard: one fit per
177/// session and half-life.
178const PREQ_SESSIONS: usize = 16;
179/// The sessions at which the monitor's confidence sequence is tightest: the pre-registered online
180/// readout's 30 sessions. Method: it sets the normal mixture's ρ, not the error rate.
181const CS_SESSIONS: f64 = 30.0;
182/// Sessions before the monitor may trip. Method: the sequence plugs in the increments' variance,
183/// which needs a few sessions to mean anything.
184const CS_MIN: usize = 10;
185/// Bisection steps of the budget's price. Method: enough halvings for f64 precision.
186const PRICE_STEPS: usize = 60;
187
188// The value model's coefficients (module doc). Protocol: their places in `Model::beta`.
189const B0: usize = 0;
190const G2: usize = 1;
191const G3: usize = 2;
192const RANK: usize = 3;
193const STOP: usize = 4;
194const THREE: usize = 5;
195const ASK: usize = 6;
196const NOT: usize = 7;
197const ASK_M: usize = 8;
198const FREE_M: usize = 9;
199const ASK_STOP: usize = 10;
200const ASK_G3: usize = 11;
201const NOT_G: usize = 12;
202const POOL: usize = 13;
203const THREE_STOP: usize = 14;
204/// The value model's coefficients. Protocol: the layout of `Model::beta` (a cached model of another
205/// layout is not read, `Model::current`).
206pub const P: usize = 15;
207/// Their names, for `borg debug select` and the monitoring, in `Model::beta`'s order.
208pub const NAMES: [&str; P] = [
209    "intercept",
210    "grade2",
211    "grade3",
212    "rank",
213    "stop",
214    "three",
215    "ask",
216    "not",
217    "ask_dilution",
218    "free_dilution",
219    "ask_stop",
220    "ask_grade3",
221    "not_grade",
222    "pool",
223    "three_stop",
224];
225/// The random-effect families, in `Model`'s order. Protocol.
226const SESSION: usize = 0;
227const LESSON: usize = 1;
228const JUDGE: usize = 2;
229const FAMILIES: usize = 3;
230
231/// A coefficient's prior: N(mean, sd²), its sd learned (ARD) or fixed, bounded above by 0 or not.
232#[derive(Debug, Clone, Copy)]
233struct Coef {
234    mean: f64,
235    sd: f64,
236    ard: bool,
237    nonpos: bool,
238}
239
240/// The value model's priors (module doc, constants above): a dated prior keeps its sd, ARD learns the
241/// sd of the terms without one.
242fn coefs() -> [Coef; P] {
243    let fixed = |(mean, sd): (f64, f64), nonpos| Coef { mean, sd, ard: false, nonpos };
244    let ard = |nonpos| Coef { mean: 0.0, sd: ARD_START, ard: true, nonpos };
245    let mut c = [fixed((0.0, PRIOR_SD), false); P];
246    c[RANK] = ard(false);
247    c[ASK] = fixed(ASK_PRIOR, false);
248    c[NOT] = fixed(NOT_PRIOR, false);
249    c[ASK_M] = fixed(DILUTION_PRIOR, true);
250    c[FREE_M] = fixed(DILUTION_PRIOR, true);
251    for i in [ASK_STOP, ASK_G3, NOT_G] {
252        c[i] = ard(false);
253    }
254    c[POOL] = ard(true);
255    c
256}
257
258/// The terms P2's pruned model keeps (`fit_pruned`): grade (the intercept and both grades), route (ask
259/// and not now against free) and dilution. Method: the pre-registered prune (tasks/todo.md, P2).
260const PRUNED_KEEPS: [usize; 7] = [B0, G2, G3, ASK, NOT, ASK_M, FREE_M];
261
262/// The priors of P2's pruned model: `coefs` for the terms it keeps, every other held at N(0,
263/// `SD_FLOOR`²) without ARD, which could bring it back: out of the model.
264fn pruned() -> [Coef; P] {
265    let mut c = coefs();
266    for (k, x) in c.iter_mut().enumerate() {
267        if !PRUNED_KEEPS.contains(&k) {
268            *x = Coef { mean: 0.0, sd: SD_FLOOR, ard: false, nonpos: false };
269        }
270    }
271    c
272}
273
274/// What the value model knows of a graded apply, its route aside.
275#[derive(Debug, Clone, Copy, PartialEq)]
276pub struct Feat {
277    pub grade: u8,
278    pub rank: usize,
279    /// The check is the stop's (else live).
280    pub stop: bool,
281    /// The check has a 3.
282    pub three: bool,
283    /// ln(pool tokens / whole.rs `TOKENS`), 0 when unknown.
284    pub pool: f64,
285}
286
287impl Feat {
288    /// The features of `units` at a check on `stop` with a pool of `tokens` (0 unknown).
289    pub fn of(units: &[Unit], stop: bool, tokens: u32) -> Vec<Feat> {
290        let three = units.iter().any(|u| u.grade == 3);
291        let pool = pool_x(tokens);
292        units.iter().map(|u| Feat { grade: u.grade, rank: u.rank, stop, three, pool }).collect()
293    }
294}
295
296/// ln(tokens / whole.rs `TOKENS`), 0 for an unknown pool (a record from before pool sizes).
297fn pool_x(tokens: u32) -> f64 {
298    if tokens == 0 { 0.0 } else { (f64::from(tokens) / crate::whole::TOKENS as f64).ln() }
299}
300
301/// The value model's row for apply `f` on `route` among `m` of its route.
302fn design(f: &Feat, route: Route, m: usize) -> [f64; P] {
303    let mut x = [0.0; P];
304    let g = f.grade;
305    x[B0] = 1.0;
306    x[G2] = f64::from(u8::from(g == 2));
307    x[G3] = f64::from(u8::from(g == 3));
308    x[RANK] = (1.0 + f.rank as f64).ln();
309    x[STOP] = f64::from(u8::from(f.stop));
310    x[THREE] = f64::from(u8::from(f.three));
311    x[THREE_STOP] = f64::from(u8::from(f.stop && f.three));
312    x[POOL] = f.pool;
313    let ln_m = (m.max(1) as f64).ln();
314    match route {
315        Route::Ask => {
316            x[ASK] = 1.0;
317            x[ASK_M] = ln_m;
318            x[ASK_STOP] = x[STOP];
319            x[ASK_G3] = x[G3];
320        }
321        Route::Free => x[FREE_M] = ln_m,
322        Route::Placebo | Route::Not => {
323            x[NOT] = 1.0;
324            x[NOT_G] = f64::from(g) - 2.0;
325        }
326    }
327    x
328}
329
330fn dot<const K: usize>(a: &[f64; K], b: &[f64; K]) -> f64 {
331    a.iter().zip(b).map(|(x, y)| x * y).sum()
332}
333
334fn sigmoid(x: f64) -> f64 {
335    if x >= 0.0 {
336        1.0 / (1.0 + (-x).exp())
337    } else {
338        let e = x.exp();
339        e / (1.0 + e)
340    }
341}
342
343/// ln(1 + eˣ), without overflow.
344fn log1pexp(x: f64) -> f64 {
345    if x > 0.0 { x + (-x).exp().ln_1p() } else { x.exp().ln_1p() }
346}
347
348/// The lower Cholesky factor of the symmetric positive definite `a` (n×n, row-major), in place; false
349/// when it is not positive definite.
350fn cholesky(a: &mut [f64], n: usize) -> bool {
351    for j in 0..n {
352        let d = a[j * n + j] - (0..j).map(|k| a[j * n + k] * a[j * n + k]).sum::<f64>();
353        if d.is_nan() || d <= 0.0 || !d.is_finite() {
354            return false;
355        }
356        let d = d.sqrt();
357        a[j * n + j] = d;
358        for i in j + 1..n {
359            let s = a[i * n + j] - (0..j).map(|k| a[i * n + k] * a[j * n + k]).sum::<f64>();
360            a[i * n + j] = s / d;
361        }
362        for i in 0..j {
363            a[i * n + j] = 0.0;
364        }
365    }
366    true
367}
368
369/// Solves L Lᵀ x = b in place, `l` from `cholesky`.
370fn chol_solve(l: &[f64], n: usize, b: &mut [f64]) {
371    for i in 0..n {
372        let s = b[i] - (0..i).map(|k| l[i * n + k] * b[k]).sum::<f64>();
373        b[i] = s / l[i * n + i];
374    }
375    for i in (0..n).rev() {
376        let s = b[i] - (i + 1..n).map(|k| l[k * n + i] * b[k]).sum::<f64>();
377        b[i] = s / l[i * n + i];
378    }
379}
380
381/// The inverse of L Lᵀ (n×n, row-major).
382fn chol_inverse(l: &[f64], n: usize) -> Vec<f64> {
383    let mut inv = vec![0.0; n * n];
384    let mut e = vec![0.0; n];
385    for j in 0..n {
386        e.iter_mut().for_each(|x| *x = 0.0);
387        e[j] = 1.0;
388        chol_solve(l, n, &mut e);
389        for (i, x) in e.iter().enumerate() {
390            inv[i * n + j] = *x;
391        }
392    }
393    inv
394}
395
396/// A standard normal draw (Box-Muller, one of the pair): O(1).
397fn normal(rng: &mut Rng) -> f64 {
398    let (u, v) = (rng.unit(), rng.unit());
399    (-2.0 * u.ln()).sqrt() * (std::f64::consts::TAU * v).cos()
400}
401
402/// A Gamma(shape, 1) draw (Marsaglia & Tsang 2000; shape < 1 by the boost U^(1/shape)): O(1) expected.
403fn gamma(shape: f64, rng: &mut Rng) -> f64 {
404    if shape < 1.0 {
405        return gamma(shape + 1.0, rng) * rng.unit().powf(1.0 / shape);
406    }
407    let d = shape - 1.0 / 3.0;
408    let c = 1.0 / (9.0 * d).sqrt();
409    // Guard: each round accepts with probability over 0.95, so 64 rounds never all reject in practice;
410    // the mode-like d stands in if they do.
411    for _ in 0..64 {
412        let z = normal(rng);
413        let v = (1.0 + c * z).powi(3);
414        if v <= 0.0 {
415            continue;
416        }
417        if rng.unit().ln() < 0.5 * z * z + d - d * v + d * v.ln() {
418            return d * v;
419        }
420    }
421    d
422}
423
424/// A Beta(a, b) draw from two Gamma draws.
425fn beta_draw(a: f64, b: f64, rng: &mut Rng) -> f64 {
426    let (x, y) = (gamma(a, rng), gamma(b, rng));
427    if x + y > 0.0 { x / (x + y) } else { a / (a + b) }
428}
429
430/// A stable 64-bit hash of `parts` (blake3, separated), for seeds.
431fn hash(parts: &[&[u8]]) -> u64 {
432    let mut h = blake3::Hasher::new();
433    h.update(b"borg-select-policy-v1\0");
434    for p in parts {
435        h.update(p);
436        h.update(b"\0");
437    }
438    let mut first = [0u8; 8];
439    first.copy_from_slice(&h.finalize().as_bytes()[..8]);
440    u64::from_le_bytes(first)
441}
442
443/// A seed for draw `i` of the stream `seed` (SplitMix64 of both).
444fn mix(seed: u64, i: u64) -> u64 {
445    Rng::new(seed ^ i.wrapping_add(1).wrapping_mul(0x9e37_79b9_7f4a_7c15)).next_u64()
446}
447
448/// One observation of a logistic model: features, label, weight, and its level in each family.
449#[derive(Debug, Clone)]
450struct Obs<const K: usize, const F: usize> {
451    x: [f64; K],
452    y: f64,
453    w: f64,
454    lv: [Option<usize>; F],
455}
456
457/// A fitted logistic model: the MAP and its Laplace covariance.
458#[derive(Debug, Clone)]
459struct Est<const K: usize, const F: usize> {
460    beta: [f64; K],
461    u: [Vec<f64>; F],
462    /// The prior sds the fit used (ARD's learned for theirs) and the families' spreads.
463    sd: [f64; K],
464    spread: [f64; F],
465    /// The marginal covariance of `beta`, and each level's marginal variance.
466    cov: [[f64; K]; K],
467    var_u: [Vec<f64>; F],
468    converged: bool,
469}
470
471impl<const K: usize, const F: usize> Est<K, F> {
472    fn eta(&self, o: &Obs<K, F>) -> f64 {
473        dot(&self.beta, &o.x) + (0..F).filter_map(|f| o.lv[f].map(|l| self.u[f][l])).sum::<f64>()
474    }
475}
476
477/// The MAP of the penalised logistic model over `obs` (`levels[f]` levels in family f), by Newton
478/// steps over every parameter at once: the family with the most levels is eliminated exactly (its
479/// levels are independent given the rest), the rest solved densely. Deterministic. None when the
480/// system is singular (cannot happen with proper priors; a guard).
481fn map<const K: usize, const F: usize>(
482    obs: &[Obs<K, F>],
483    levels: [usize; F],
484    spec: &[Coef; K],
485    sd: &[f64; K],
486    spread: &[f64; F],
487    warm: Option<&Est<K, F>>,
488) -> Option<Est<K, F>> {
489    let elim = (0..F).filter(|&f| levels[f] > 0).max_by_key(|&f| (levels[f], std::cmp::Reverse(f)));
490    let mut off = [0usize; F];
491    let mut dim = K;
492    for f in 0..F {
493        if Some(f) != elim {
494            off[f] = dim;
495            dim += levels[f];
496        }
497    }
498    let members: Vec<Vec<usize>> = match elim {
499        Some(e) => {
500            let mut m = vec![Vec::new(); levels[e]];
501            for (i, o) in obs.iter().enumerate() {
502                if let Some(l) = o.lv[e] {
503                    m[l].push(i);
504                }
505            }
506            m
507        }
508        None => Vec::new(),
509    };
510    let mut beta: [f64; K] = std::array::from_fn(|k| warm.map_or(spec[k].mean, |w| w.beta[k]));
511    for k in 0..K {
512        if spec[k].nonpos {
513            beta[k] = beta[k].min(0.0);
514        }
515    }
516    let mut u: [Vec<f64>; F] = std::array::from_fn(|f| match warm {
517        Some(w) if w.u[f].len() == levels[f] => w.u[f].clone(),
518        _ => vec![0.0; levels[f]],
519    });
520    let prec: [f64; K] = std::array::from_fn(|k| 1.0 / (sd[k] * sd[k]));
521    let uprec: [f64; F] = std::array::from_fn(|f| 1.0 / (spread[f] * spread[f]));
522    let objective = |beta: &[f64; K], u: &[Vec<f64>; F]| -> f64 {
523        let mut ll = 0.0;
524        for o in obs {
525            let eta = dot(beta, &o.x) + (0..F).filter_map(|f| o.lv[f].map(|l| u[f][l])).sum::<f64>();
526            ll += o.w * (o.y * eta - log1pexp(eta));
527        }
528        let pb: f64 = (0..K).map(|k| (beta[k] - spec[k].mean).powi(2) * prec[k]).sum();
529        let pu: f64 = (0..F).map(|f| u[f].iter().map(|x| x * x).sum::<f64>() * uprec[f]).sum();
530        ll - 0.5 * (pb + pu)
531    };
532    let mut converged = false;
533    let mut s = vec![0.0; dim * dim];
534    let mut d_e: Vec<f64> = Vec::new();
535    let mut b_e: Vec<Vec<(usize, f64)>> = Vec::new();
536    let mut current = objective(&beta, &u);
537    for _ in 0..MAX_NEWTON {
538        // The precision (negative Hessian) and gradient at the current point.
539        let mut g = vec![0.0; dim];
540        s.iter_mut().for_each(|x| *x = 0.0);
541        let mut r = vec![0.0; obs.len()];
542        let mut wt = vec![0.0; obs.len()];
543        let mut dense: Vec<(usize, f64)> = Vec::with_capacity(K + F);
544        for (i, o) in obs.iter().enumerate() {
545            let eta = dot(&beta, &o.x) + (0..F).filter_map(|f| o.lv[f].map(|l| u[f][l])).sum::<f64>();
546            let mu = sigmoid(eta);
547            r[i] = o.w * (o.y - mu);
548            wt[i] = o.w * mu * (1.0 - mu);
549            dense.clear();
550            dense.extend((0..K).filter(|&k| o.x[k] != 0.0).map(|k| (k, o.x[k])));
551            dense.extend((0..F).filter(|&f| Some(f) != elim).filter_map(|f| o.lv[f].map(|l| (off[f] + l, 1.0))));
552            for &(a, xa) in &dense {
553                g[a] += r[i] * xa;
554                for &(b, xb) in &dense {
555                    s[a * dim + b] += wt[i] * xa * xb;
556                }
557            }
558        }
559        for k in 0..K {
560            g[k] -= (beta[k] - spec[k].mean) * prec[k];
561            s[k * dim + k] += prec[k];
562        }
563        for f in (0..F).filter(|&f| Some(f) != elim) {
564            for l in 0..levels[f] {
565                g[off[f] + l] -= u[f][l] * uprec[f];
566                s[(off[f] + l) * dim + off[f] + l] += uprec[f];
567            }
568        }
569        // The eliminated family: its own precision, gradient and coupling to the dense block.
570        let mut g_e: Vec<f64> = Vec::new();
571        d_e.clear();
572        b_e.clear();
573        if let Some(e) = elim {
574            for (l, rows) in members.iter().enumerate() {
575                let mut d = uprec[e];
576                let mut gl = -u[e][l] * uprec[e];
577                let mut b: Vec<(usize, f64)> = Vec::new();
578                for &i in rows {
579                    d += wt[i];
580                    gl += r[i];
581                    let o = &obs[i];
582                    for k in 0..K {
583                        add_sparse(&mut b, k, wt[i] * o.x[k]);
584                    }
585                    for f in (0..F).filter(|&f| f != e) {
586                        if let Some(lv) = o.lv[f] {
587                            add_sparse(&mut b, off[f] + lv, wt[i]);
588                        }
589                    }
590                }
591                for &(a, va) in &b {
592                    for &(c, vc) in &b {
593                        s[a * dim + c] -= va * vc / d;
594                    }
595                    g[a] -= va * gl / d;
596                }
597                d_e.push(d);
598                g_e.push(gl);
599                b_e.push(b);
600            }
601        }
602        // The step: a Newton step on the reduced system, with the bounded coefficients at 0 held there
603        // while the gradient pushes them up (an active set).
604        let mut held: Vec<usize> = (0..K).filter(|&k| spec[k].nonpos && beta[k] >= 0.0 && g[k] > 0.0).collect();
605        let step = loop {
606            let mut delta = vec![0.0; dim];
607            for &k in &held {
608                delta[k] = -beta[k];
609            }
610            let free: Vec<usize> = (0..dim).filter(|i| !held.contains(i)).collect();
611            let n = free.len();
612            let mut a = vec![0.0; n * n];
613            let mut rhs = vec![0.0; n];
614            for (fi, &i) in free.iter().enumerate() {
615                rhs[fi] = g[i] - held.iter().map(|&h| s[i * dim + h] * delta[h]).sum::<f64>();
616                for (fj, &j) in free.iter().enumerate() {
617                    a[fi * n + fj] = s[i * dim + j];
618                }
619            }
620            if !cholesky(&mut a, n) {
621                return None;
622            }
623            chol_solve(&a, n, &mut rhs);
624            for (fi, &i) in free.iter().enumerate() {
625                delta[i] = rhs[fi];
626            }
627            let out: Vec<usize> = (0..K).filter(|&k| spec[k].nonpos && !held.contains(&k) && beta[k] + delta[k] > 0.0).collect();
628            if out.is_empty() {
629                break delta;
630            }
631            held.extend(out);
632        };
633        let delta_e: Vec<f64> = (0..d_e.len()).map(|l| (g_e[l] - b_e[l].iter().map(|&(a, v)| v * step[a]).sum::<f64>()) / d_e[l]).collect();
634        let largest = step.iter().chain(&delta_e).fold(0.0f64, |m, x| m.max(x.abs()));
635        // Halve the step until the objective does not fall.
636        let mut t = 1.0;
637        let mut accepted = false;
638        for _ in 0..=MAX_HALVINGS {
639            let nb: [f64; K] = std::array::from_fn(|k| {
640                let x = beta[k] + t * step[k];
641                if spec[k].nonpos { x.min(0.0) } else { x }
642            });
643            let mut nu = u.clone();
644            for f in 0..F {
645                for l in 0..levels[f] {
646                    nu[f][l] += t * if Some(f) == elim { delta_e[l] } else { step[off[f] + l] };
647                }
648            }
649            let next = objective(&nb, &nu);
650            // Not below the current objective beyond float noise (relative 1e-12).
651            if next >= current - 1e-12 * current.abs().max(1.0) {
652                (beta, u, current) = (nb, nu, next);
653                accepted = true;
654                break;
655            }
656            t *= 0.5;
657        }
658        if !accepted || largest * t < TOL {
659            converged = accepted || largest < TOL;
660            break;
661        }
662    }
663    // The Laplace covariance: the inverse of the reduced precision, and each eliminated level's.
664    if !cholesky(&mut s, dim) {
665        return None;
666    }
667    let inv = chol_inverse(&s, dim);
668    let cov: [[f64; K]; K] = std::array::from_fn(|a| std::array::from_fn(|b| inv[a * dim + b]));
669    let var_u: [Vec<f64>; F] = std::array::from_fn(|f| {
670        if Some(f) == elim {
671            (0..levels[f])
672                .map(|l| {
673                    let b = &b_e[l];
674                    let q: f64 = b.iter().map(|&(a, va)| b.iter().map(|&(c, vc)| va * inv[a * dim + c] * vc).sum::<f64>()).sum();
675                    1.0 / d_e[l] + q / (d_e[l] * d_e[l])
676                })
677                .collect()
678        } else {
679            (0..levels[f]).map(|l| inv[(off[f] + l) * dim + off[f] + l]).collect()
680        }
681    });
682    Some(Est { beta, u, sd: *sd, spread: *spread, cov, var_u, converged })
683}
684
685/// Adds `v` at `i` of a short sparse vector.
686fn add_sparse(b: &mut Vec<(usize, f64)>, i: usize, v: f64) {
687    if v == 0.0 {
688        return;
689    }
690    match b.iter_mut().find(|(j, _)| *j == i) {
691        Some(e) => e.1 += v,
692        None => b.push((i, v)),
693    }
694}
695
696/// `map` with its spreads and ARD sds learned by EM (type-II maximum likelihood): each family's spread
697/// is the root mean of its levels' u² + var, each ARD coefficient's sd √((β − mean)² + var), within
698/// [`SD_FLOOR`, `PRIOR_SD`].
699fn eb<const K: usize, const F: usize>(obs: &[Obs<K, F>], levels: [usize; F], spec: &[Coef; K], warm: Option<&Est<K, F>>) -> Option<Est<K, F>> {
700    let mut sd: [f64; K] = std::array::from_fn(|k| warm.filter(|_| spec[k].ard).map_or(spec[k].sd, |w| w.sd[k]));
701    let mut spread: [f64; F] = std::array::from_fn(|f| warm.map_or(LEVEL_START, |w| w.spread[f]));
702    let mut est = map(obs, levels, spec, &sd, &spread, warm)?;
703    for _ in 0..MAX_EB {
704        let nsd: [f64; K] = std::array::from_fn(|k| match spec[k].ard {
705            true => ((est.beta[k] - spec[k].mean).powi(2) + est.cov[k][k]).sqrt().clamp(SD_FLOOR, PRIOR_SD),
706            false => sd[k],
707        });
708        let nspread: [f64; F] = std::array::from_fn(|f| match levels[f] {
709            0 => spread[f],
710            n => (est.u[f].iter().zip(&est.var_u[f]).map(|(x, v)| x * x + v).sum::<f64>() / n as f64).sqrt().clamp(SD_FLOOR, PRIOR_SD),
711        });
712        let moved = sd.iter().zip(&nsd).chain(spread.iter().zip(&nspread)).any(|(a, b)| (a - b).abs() > EB_TOL * a.abs().max(SD_FLOOR));
713        (sd, spread) = (nsd, nspread);
714        est = map(obs, levels, spec, &sd, &spread, Some(&est))?;
715        if !moved {
716            break;
717        }
718    }
719    Some(est)
720}
721
722/// The learned delivery rates a route's value uses (module doc), posterior means of Beta(1, 1) with
723/// the fit's forgetting.
724#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
725pub struct Rates {
726    /// A stop's free lesson was delivered (a next prompt came).
727    pub free_stop: f64,
728    /// A live check's free lesson was delivered.
729    pub free_live: f64,
730    /// A lesson a live check left for later was delivered later in the session: shown, not used (not
731    /// now is worth 0, `Valuer::value`; the later delivery earns its value at its own check).
732    pub later: f64,
733    /// A live question was still waiting at the stop and asked there in a block: shown, not used.
734    pub wait: f64,
735    /// A live question of a check with a 3, of a lesson its session lacked, reached the drone, during
736    /// the turn or waiting for the stop and asked in its block (select/summary.rs `ASK_LIVE`): such an
737    /// ask's rate in its value (`Valuer::ask`; a stop's is 1, it blocks; one of a live check without a
738    /// 3 no stop asks, so it arrives as a hand-over does, at `free_live`). 1 until one is tallied
739    /// (baseline: the fixed rule takes every question as delivered, as a stop's is).
740    pub ask_live: f64,
741}
742
743/// The pool's learned state at the fit (`pool_tokens`).
744#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
745pub struct PoolFit {
746    /// Per log2 bin of fill position, the yield's Beta posterior (shown lessons applied), monotone
747    /// non-increasing in position (pool adjacent violators over the bins).
748    #[serde(default, skip_serializing_if = "Vec::is_empty")]
749    pub yields: Vec<(f64, f64)>,
750    /// Pool tokens per shown lesson.
751    #[serde(default, skip_serializing_if = "is_default")]
752    pub per_lesson: f64,
753    /// The model call's duration in ms against its pool tokens: a + b·tokens, plus the residuals'
754    /// 95% quantile, from `latency_n` calls.
755    #[serde(default, skip_serializing_if = "is_default")]
756    pub latency: (f64, f64, f64),
757    #[serde(default, skip_serializing_if = "is_default")]
758    pub latency_n: u32,
759    /// The largest pool tokens `LATENCY_MIN` of those calls read at least (the `LATENCY_MIN`-th
760    /// largest; 0 with fewer calls): a budget above the baseline is feasible only up to this over
761    /// `LATENCY_NEAR`, where the latency fit is read among calls, not extrapolated.
762    #[serde(default, skip_serializing_if = "is_default")]
763    pub latency_reach: u32,
764    /// The model's mean η of a labelled apply, its pool slope and its "not now" effect (mean, sd):
765    /// what a lesson more in the pool is worth.
766    #[serde(default, skip_serializing_if = "is_default")]
767    pub eta: f64,
768    #[serde(default, skip_serializing_if = "is_default")]
769    pub slope: (f64, f64),
770    #[serde(default, skip_serializing_if = "is_default")]
771    pub not: (f64, f64),
772}
773
774/// The fitted value model (module doc): its MAP and draws' ingredients.
775#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
776pub struct Model {
777    /// The coefficients (`NAMES`) and the lower Cholesky factor of their marginal covariance (P×P,
778    /// row-major).
779    pub beta: Vec<f64>,
780    pub chol: Vec<f64>,
781    /// The prior sd of each coefficient the fit ended with (ARD's learned).
782    pub sd: Vec<f64>,
783    /// The spread of the session, lesson and judge effects.
784    pub spread: [f64; FAMILIES],
785    /// Each level's effect, (mean, marginal sd).
786    pub sessions: BTreeMap<String, (f64, f64)>,
787    pub lessons: BTreeMap<String, (f64, f64)>,
788    pub judges: BTreeMap<String, (f64, f64)>,
789    /// The judge of the newest labelled check: the one that will judge the next.
790    #[serde(default, skip_serializing_if = "String::is_empty")]
791    pub judge: String,
792    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
793    pub converged: bool,
794}
795
796impl Model {
797    /// Whether the model has this build's layout (`P` coefficients). A cache written by a build with
798    /// another layout holds one that is not: its Cholesky rows would be read at the wrong places, so
799    /// decisions and the ledger take it for no model (the fixed rule) until the next fit.
800    #[must_use]
801    pub fn current(&self) -> bool {
802        self.beta.len() == P && self.chol.len() == P * P && self.sd.len() == P
803    }
804}
805
806/// What a decision draws from, fitted at `borg learn` (select/summary.rs `refit`), never in a hook. The
807/// default is the prior: no data, every decision the fixed rule's.
808#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
809pub struct Fit {
810    /// The outcomes it was fitted on (select/summary.rs `Summary::key`: byte offset and hash), logged
811    /// with each check (`Log::fit`) so a replay finds the same fit. Empty for the prior.
812    #[serde(default, skip_serializing_if = "String::is_empty")]
813    pub key: String,
814    /// The value model; None without a label.
815    #[serde(default, skip_serializing_if = "Option::is_none")]
816    pub model: Option<Model>,
817    /// The forgetting's half-life in sessions (None: none).
818    #[serde(default, skip_serializing_if = "Option::is_none")]
819    pub half_life: Option<u32>,
820    #[serde(default, skip_serializing_if = "is_default")]
821    pub rates: Rates,
822    #[serde(default, skip_serializing_if = "is_default")]
823    pub pool: PoolFit,
824    /// The engineer's budget (pool tokens per session) in force at the fit, and its price per token
825    /// (`price`); 0 without a budget.
826    #[serde(default, skip_serializing_if = "Option::is_none")]
827    pub budget: Option<f64>,
828    #[serde(default, skip_serializing_if = "is_default")]
829    pub lambda: f64,
830    /// Whether ask's or not-now's effect changes sign when an unclear verdict counts as "no change"
831    /// instead of missing: the label pipeline's unclear share decides the sign, so read with care.
832    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
833    pub sensitive: bool,
834    /// Labels the model read, and labels `FIT_SESSIONS` left out.
835    #[serde(default, skip_serializing_if = "is_default")]
836    pub labels: u32,
837    #[serde(default, skip_serializing_if = "is_default")]
838    pub dropped: u32,
839}
840
841fn is_default<T: Default + PartialEq>(v: &T) -> bool {
842    *v == T::default()
843}
844
845/// The age weight of a label `age` sessions old under half-life `h`.
846pub(super) fn decay(age: u32, h: Option<u32>) -> f64 {
847    h.map_or(1.0, |h| 0.5f64.powf(f64::from(age) / f64::from(h)))
848}
849
850/// The labelled applies of a summary as the value model's rows, with what the missingness model and
851/// the monitor need beside them.
852struct Data {
853    obs: Vec<Obs<P, FAMILIES>>,
854    /// Per row: its session's tick, its weight before forgetting, and whether its verdict was unclear
855    /// (in the rows only for the unclear-is-no-change variant).
856    tick: Vec<u32>,
857    base_w: Vec<f64>,
858    unclear: Vec<bool>,
859    levels: [usize; FAMILIES],
860    names: [Vec<String>; FAMILIES],
861    /// Per apply of the summary: its levels, its judged probability.
862    lv_of: HashMap<usize, [Option<usize>; FAMILIES]>,
863    judged_p: HashMap<usize, f64>,
864    dropped: u32,
865}
866
867/// The index of a level among the labelled ones, added when new: `names[f]`, `index[f]` by family.
868fn level_of(names: &mut [Vec<String>; FAMILIES], index: &mut [HashMap<String, usize>; FAMILIES], tagged: &(usize, String)) -> usize {
869    let (f, name) = tagged;
870    *index[*f].entry(name.clone()).or_insert_with(|| {
871        names[*f].push(name.clone());
872        names[*f].len() - 1
873    })
874}
875
876/// The route a labelled apply counts as in the model: ask, free, or not now (a placebo); None for an
877/// apply never delivered or never judged.
878fn model_route(a: &Apply) -> Option<Route> {
879    match a.route {
880        Route::Ask | Route::Free if a.delivered != super::summary::Delivered::Dropped => Some(a.route),
881        Route::Placebo => Some(Route::Not),
882        _ => None,
883    }
884}
885
886/// The applies of a logged check of `s` (none for a range the summary does not hold).
887fn applies_of<'a>(s: &'a Summary, c: &Logged) -> &'a [Apply] {
888    s.applies.get(c.first as usize..c.first as usize + usize::from(c.n)).unwrap_or(&[])
889}
890
891/// The lessons a logged check of `s` could not hand over free (select/summary.rs `Apply::nofree`, its
892/// record's `nofree`): the `had` its decision read, so a replay values it against the same fixed rule.
893fn logged_had(s: &Summary, c: &Logged) -> Vec<String> {
894    applies_of(s, c).iter().filter(|a| a.nofree).filter_map(|a| s.lessons.get(a.lesson as usize).cloned()).collect()
895}
896
897/// The units, features and routes of a logged check of `s`.
898fn check_view<'a>(s: &'a Summary, c: &Logged) -> (Vec<Unit<'a>>, Vec<Feat>, Vec<Route>) {
899    let applies = applies_of(s, c);
900    let units: Vec<Unit> =
901        applies.iter().map(|a| Unit { lesson: s.lessons.get(a.lesson as usize).map_or("", String::as_str), grade: a.grade, rank: a.rank as usize }).collect();
902    let feats = Feat::of(&units, c.stop, c.tokens);
903    let routes = applies.iter().map(|a| if a.route == Route::Placebo { Route::Not } else { a.route }).collect();
904    (units, feats, routes)
905}
906
907impl Data {
908    /// The rows of `s` (the newest `FIT_SESSIONS` sessions), unclear verdicts included as no change
909    /// when `zero`, else left out.
910    fn of(s: &Summary, zero: bool) -> Data {
911        let now = s.sessions.len() as u32;
912        let from = now.saturating_sub(FIT_SESSIONS as u32);
913        let mut names: [Vec<String>; FAMILIES] = Default::default();
914        let mut index: [HashMap<String, usize>; FAMILIES] = Default::default();
915        let judged_p = judged(s);
916        let mut d = Data {
917            obs: Vec::new(),
918            tick: Vec::new(),
919            base_w: Vec::new(),
920            unclear: Vec::new(),
921            levels: [0; FAMILIES],
922            names: Default::default(),
923            lv_of: HashMap::new(),
924            judged_p: HashMap::new(),
925            dropped: 0,
926        };
927        let names_of = |c: &Logged, a: &Apply| -> [Option<(usize, String)>; FAMILIES] {
928            [
929                Some((SESSION, s.sessions.get(c.tick as usize).map_or("", |x| x.id.as_str()).to_string())),
930                s.lessons.get(a.lesson as usize).map(|n| (LESSON, n.clone())),
931                a.judge.and_then(|j| s.judges.get(j as usize).cloned()).map(|n| (JUDGE, n)),
932            ]
933        };
934        for c in &s.checks {
935            let (_, feats, _) = check_view(s, c);
936            let (m_q, m_f) = (usize::from(c.m_q), usize::from(c.m_f));
937            for (j, f) in feats.iter().enumerate() {
938                let i = c.first as usize + j;
939                let a = &s.applies[i];
940                let Some(route) = model_route(a) else { continue };
941                let y = match a.label {
942                    Label::Changed => 1.0,
943                    Label::Ignored => 0.0,
944                    Label::Unclear if zero => 0.0,
945                    _ => continue,
946                };
947                if c.tick < from {
948                    d.dropped += 1;
949                    continue;
950                }
951                let m = match route {
952                    Route::Ask => m_q,
953                    Route::Free => m_f,
954                    _ => 1,
955                };
956                let p = judged_p.get(&i).copied().unwrap_or(1.0);
957                let w = 1.0 / p.max(1.0 / MAX_IPW);
958                let lv = names_of(c, a).map(|n| n.map(|n| level_of(&mut names, &mut index, &n)));
959                d.obs.push(Obs { x: design(f, route, m), y, w, lv });
960                d.tick.push(c.tick);
961                d.base_w.push(w);
962                d.unclear.push(a.label == Label::Unclear);
963            }
964        }
965        // Every apply's levels among the labelled ones (the monitor's predictions): an unseen one has none.
966        for c in &s.checks {
967            for i in c.first as usize..(c.first as usize + c.n as usize).min(s.applies.len()) {
968                let n = names_of(c, &s.applies[i]);
969                d.lv_of.insert(i, std::array::from_fn(|f| n[f].as_ref().and_then(|(_, n)| index[f].get(n).copied())));
970            }
971        }
972        d.levels = std::array::from_fn(|f| names[f].len());
973        d.names = names;
974        d.judged_p = judged_p;
975        d
976    }
977
978    /// The rows without their session, lesson and judge levels (P2's pruned model, `fit_pruned`).
979    fn without_levels(mut self) -> Data {
980        for o in &mut self.obs {
981            o.lv = [None; FAMILIES];
982        }
983        for lv in self.lv_of.values_mut() {
984            *lv = [None; FAMILIES];
985        }
986        self.levels = [0; FAMILIES];
987        self.names = Default::default();
988        self
989    }
990
991    /// The rows weighted by forgetting at `now` under `h`.
992    fn weighted(&self, now: u32, h: Option<u32>) -> Vec<Obs<P, FAMILIES>> {
993        self.obs.iter().zip(&self.tick).zip(&self.base_w).map(|((o, t), w)| Obs { w: w * decay(now.saturating_sub(*t), h), ..o.clone() }).collect()
994    }
995}
996
997/// Per delivered apply of `s` with a closed fate (judged, unclear or censored), the probability that
998/// the judge gave it a verdict: a logistic fit on its route, channel, the session's applies waiting
999/// for a verdict at its check, and whether its check was the session's last. Unclear counts as not
1000/// judged.
1001pub(crate) fn judged(s: &Summary) -> HashMap<usize, f64> {
1002    // Derived: the fit's features (intercept, asked, free, stop, log pending, last check).
1003    const K: usize = 6;
1004    let mut last: HashMap<u32, usize> = HashMap::new();
1005    for (i, c) in s.checks.iter().enumerate() {
1006        last.insert(c.tick, i);
1007    }
1008    let mut rows: Vec<(usize, Obs<K, 0>)> = Vec::new();
1009    for (ci, c) in s.checks.iter().enumerate() {
1010        for i in c.first as usize..(c.first as usize + c.n as usize).min(s.applies.len()) {
1011            let a = &s.applies[i];
1012            if model_route(a).is_none() || a.label == Label::Open {
1013                continue;
1014            }
1015            let y = f64::from(u8::from(matches!(a.label, Label::Changed | Label::Ignored)));
1016            let x = [
1017                1.0,
1018                f64::from(u8::from(a.route == Route::Ask)),
1019                f64::from(u8::from(a.route == Route::Free)),
1020                f64::from(u8::from(c.stop)),
1021                (1.0 + f64::from(a.pending)).ln(),
1022                f64::from(u8::from(last.get(&c.tick) == Some(&ci))),
1023            ];
1024            rows.push((i, Obs { x, y, w: 1.0, lv: [] }));
1025        }
1026    }
1027    if rows.is_empty() {
1028        return HashMap::new();
1029    }
1030    let spec = [Coef { mean: 0.0, sd: PRIOR_SD, ard: false, nonpos: false }; K];
1031    let obs: Vec<Obs<K, 0>> = rows.iter().map(|(_, o)| o.clone()).collect();
1032    let Some(est) = map(&obs, [], &spec, &[PRIOR_SD; K], &[], None) else { return HashMap::new() };
1033    rows.iter().map(|(i, o)| (*i, sigmoid(est.eta(o)))).collect()
1034}
1035
1036/// The log-likelihood of `obs` under `est`, each row at weight `w`.
1037fn loglik(est: &Est<P, FAMILIES>, obs: &[Obs<P, FAMILIES>]) -> f64 {
1038    obs.iter()
1039        .map(|o| {
1040            // Clamped so a certain miss costs a large finite loss, not −∞.
1041            let p = sigmoid(est.eta(o)).clamp(1e-12, 1.0 - 1e-12);
1042            o.w * (o.y * p.ln() + (1.0 - o.y) * (1.0 - p).ln())
1043        })
1044        .sum()
1045}
1046
1047/// The forgetting's half-life (`HALF_LIVES`) with the highest prequential log-likelihood: each of the
1048/// newest `PREQ_SESSIONS` labelled sessions predicted by a fit on the sessions before it, aged from it;
1049/// the spreads stay at `base`'s. A tie goes to the longer half-life.
1050fn half_life(d: &Data, base: &Est<P, FAMILIES>, spec: &[Coef; P]) -> Option<u32> {
1051    let mut ticks: Vec<u32> = d.tick.clone();
1052    ticks.sort_unstable();
1053    ticks.dedup();
1054    let eval: Vec<u32> = ticks.iter().skip(1).rev().take(PREQ_SESSIONS).rev().copied().collect();
1055    if eval.is_empty() {
1056        return None;
1057    }
1058    let mut best: (f64, Option<u32>) = (f64::NEG_INFINITY, None);
1059    for h in HALF_LIVES {
1060        let mut score = 0.0;
1061        let mut warm: Option<Est<P, FAMILIES>> = None;
1062        for &t in &eval {
1063            let train: Vec<Obs<P, FAMILIES>> = d
1064                .obs
1065                .iter()
1066                .zip(&d.tick)
1067                .zip(&d.base_w)
1068                .filter(|((_, tk), _)| **tk < t)
1069                .map(|((o, tk), w)| Obs { w: w * decay(t - tk, h), ..o.clone() })
1070                .collect();
1071            let test: Vec<Obs<P, FAMILIES>> = d.obs.iter().zip(&d.tick).filter(|(_, tk)| **tk == t).map(|(o, _)| o.clone()).collect();
1072            let Some(est) = map(&train, d.levels, spec, &base.sd, &base.spread, warm.as_ref()) else { continue };
1073            score += loglik(&est, &test);
1074            warm = Some(est);
1075        }
1076        // A tie (within float noise, relative 1e-9) keeps the longer half-life found first.
1077        if score > best.0 + 1e-9 * best.0.abs().max(1.0) || best.0 == f64::NEG_INFINITY {
1078            best = (score, h);
1079        }
1080    }
1081    best.1
1082}
1083
1084/// The value model of `est` with the level names of `d`; None unless every number is finite (a JSON
1085/// cache cannot hold another, select/summary.rs `save`).
1086fn model_of(est: &Est<P, FAMILIES>, d: &Data, judge: String) -> Option<Model> {
1087    let mut chol: Vec<f64> = est.cov.iter().flatten().copied().collect();
1088    let finite = est.beta.iter().chain(&est.sd).chain(&est.spread).chain(est.u.iter().flatten()).chain(est.var_u.iter().flatten()).all(|x| x.is_finite());
1089    if !finite || !cholesky(&mut chol, P) {
1090        return None;
1091    }
1092    let levels = |f: usize| -> BTreeMap<String, (f64, f64)> {
1093        d.names[f].iter().enumerate().map(|(l, n)| (n.clone(), (est.u[f][l], est.var_u[f][l].max(0.0).sqrt()))).collect()
1094    };
1095    Some(Model {
1096        beta: est.beta.to_vec(),
1097        chol,
1098        sd: est.sd.to_vec(),
1099        spread: est.spread,
1100        sessions: levels(SESSION),
1101        lessons: levels(LESSON),
1102        judges: levels(JUDGE),
1103        judge,
1104        converged: est.converged,
1105    })
1106}
1107
1108/// The fit of `s` (module doc): the value model, the forgetting, the delivery rates, the pool's state
1109/// and, under an engineer's `budget` (pool tokens per session), its price. The prior without labels.
1110#[must_use]
1111pub fn fit(s: &Summary, budget: Option<f64>) -> Fit {
1112    fit_with(s, budget, &coefs(), true)
1113}
1114
1115/// `borg debug select` P2's pre-registered pruned model (tasks/todo.md, P2: "prune the model to
1116/// grade, route and dilution and rerun once"): `fit` of `s` with the value model cut to the terms of
1117/// `PRUNED_KEEPS`, every other coefficient held at N(0, `SD_FLOOR`²) and no session, lesson or judge
1118/// effects (their spreads 0, so a prediction adds none), its half-life chosen as `fit` chooses it.
1119/// Evaluation only: never what a hook decides with.
1120#[must_use]
1121pub fn fit_pruned(s: &Summary, budget: Option<f64>) -> Fit {
1122    fit_with(s, budget, &pruned(), false)
1123}
1124
1125/// `fit` under the priors `spec`, with the session, lesson and judge effects when `levels`.
1126fn fit_with(s: &Summary, budget: Option<f64>, spec: &[Coef; P], levels: bool) -> Fit {
1127    let data = |zero: bool| if levels { Data::of(s, zero) } else { Data::of(s, zero).without_levels() };
1128    let d = data(false);
1129    let now = s.sessions.len() as u32;
1130    let mut fit = Fit { key: s.key(), labels: d.obs.len() as u32, dropped: d.dropped, budget: budget.filter(|b| *b > 0.0), ..Fit::default() };
1131    let base = if d.obs.is_empty() { None } else { eb(&d.obs, d.levels, spec, None) };
1132    if let Some(base) = &base {
1133        fit.half_life = half_life(&d, base, spec);
1134        let est = eb(&d.weighted(now, fit.half_life), d.levels, spec, Some(base));
1135        let judge = s.checks.iter().rev().flat_map(|c| applies_of(s, c)).find_map(|a| a.judge);
1136        let judge = judge.filter(|_| levels).and_then(|j| s.judges.get(j as usize)).cloned().unwrap_or_default();
1137        if let Some(est) = est {
1138            let z = data(true);
1139            if z.unclear.iter().any(|u| *u)
1140                && let Some(alt) = eb(&z.weighted(now, fit.half_life), z.levels, spec, None)
1141            {
1142                fit.sensitive = [ASK, NOT].iter().any(|&k| est.beta[k].signum() != alt.beta[k].signum());
1143            }
1144            let eta = d.obs.iter().map(|o| est.eta(o)).sum::<f64>() / d.obs.len() as f64;
1145            fit.pool.eta = eta;
1146            fit.pool.slope = (est.beta[POOL], est.cov[POOL][POOL].max(0.0).sqrt());
1147            fit.pool.not = (est.beta[NOT], est.cov[NOT][NOT].max(0.0).sqrt());
1148            // Without families no level has an effect: the fit's spreads are EM's untouched start.
1149            fit.model = model_of(&est, &d, judge).map(|m| if levels { m } else { Model { spread: [0.0; FAMILIES], ..m } });
1150        }
1151    }
1152    fit.rates = rates(s, fit.half_life);
1153    pool_state(s, &mut fit.pool);
1154    if let Some(b) = fit.budget {
1155        fit.lambda = price(s, &fit, b);
1156    }
1157    fit
1158}
1159
1160/// The n = 0 prior as a fit, for `borg debug select` P4's "ungated policy under the n = 0 prior": the
1161/// value model's MAP over no labels (every coefficient at its prior mean with its prior covariance,
1162/// every family at its starting spread) and the delivery rates' Beta(1, 1) means. Never what a hook
1163/// decides with: `fit` without a label has no model, so cold start is the fixed rule.
1164#[must_use]
1165pub fn prior() -> Fit {
1166    prior_with(&coefs(), true)
1167}
1168
1169/// The n = 0 prior of P2's pruned model (`fit_pruned`), for its rerun's units with no label before
1170/// their check: `prior` under the pruned model's priors (`pruned`) and, as `fit_pruned`, no session,
1171/// lesson or judge effects (spreads 0). Evaluation only.
1172#[must_use]
1173pub fn prior_pruned() -> Fit {
1174    prior_with(&pruned(), false)
1175}
1176
1177/// The value model's MAP over no labels under the priors `spec`, each family at its starting spread
1178/// when `levels`, else at 0 (`fit_with`), and the delivery rates' Beta(1, 1) means.
1179fn prior_with(spec: &[Coef; P], levels: bool) -> Fit {
1180    let s = Summary::default();
1181    let d = Data::of(&s, false);
1182    let sd: [f64; P] = std::array::from_fn(|k| spec[k].sd);
1183    let model = map(&d.obs, d.levels, spec, &sd, &[LEVEL_START; FAMILIES], None)
1184        .and_then(|est| model_of(&est, &d, String::new()))
1185        .map(|m| if levels { m } else { Model { spread: [0.0; FAMILIES], ..m } });
1186    Fit { model, rates: rates(&s, None), ..Fit::default() }
1187}
1188
1189/// π/8, the probit approximation's scale: E[σ(η)] ≈ σ(μ / √(1 + π σ² / 8)) for η ~ N(μ, σ²) (MacKay
1190/// 1992). Method.
1191const PROBIT: f64 = std::f64::consts::PI / 8.0;
1192
1193/// The model's chance that an apply with features `f`, routed `route` among `m` of its route, changes
1194/// the work (`borg debug select` P2's prediction): σ(η) averaged over η's Laplace posterior by the
1195/// probit approximation (`PROBIT`), η's variance from the coefficients' covariance and the session's,
1196/// lesson's and next judge's effects (an unseen level at 0 with its family's spread). None without a
1197/// model of this build's layout.
1198#[must_use]
1199pub fn predict(fit: &Fit, f: &Feat, route: Route, m: usize, session: &str, lesson: &str) -> Option<f64> {
1200    let model = fit.model.as_ref().filter(|m| m.current())?;
1201    let x = design(f, route, m);
1202    // (Lᵀx)_k over the lower factor's rows i ≥ k: xᵀΣx = |Lᵀx|².
1203    let lx = (0..P).map(|k| (k..P).map(|i| model.chol[i * P + k] * x[i]).sum::<f64>());
1204    let mut var: f64 = lx.map(|v| v * v).sum();
1205    let mut mean: f64 = model.beta.iter().zip(&x).map(|(b, x)| b * x).sum();
1206    for (map, name, spread) in [
1207        (&model.sessions, session, model.spread[SESSION]),
1208        (&model.lessons, lesson, model.spread[LESSON]),
1209        (&model.judges, model.judge.as_str(), model.spread[JUDGE]),
1210    ] {
1211        let (mu, sd) = map.get(name).copied().unwrap_or((0.0, spread));
1212        mean += mu;
1213        var += sd * sd;
1214    }
1215    Some(sigmoid(mean / (1.0 + PROBIT * var).sqrt()))
1216}
1217
1218/// The delivery rates of `s` under half-life `h`: Beta(1, 1) on every session's tallies, the compacted
1219/// ones included (select/summary.rs `Summary::tallies`); a live question's certain until one is tallied.
1220fn rates(s: &Summary, h: Option<u32>) -> Rates {
1221    let mean = |(k, miss): (f64, f64)| (1.0 + k) / (2.0 + k + miss);
1222    let [free_stop, free_live, later, wait, ask] = s.tallies(h);
1223    // Baseline before data (`Rates::ask_live`).
1224    let ask_live = if ask.0 + ask.1 > 0.0 { mean(ask) } else { 1.0 };
1225    Rates { free_stop: mean(free_stop), free_live: mean(free_live), later: mean(later), wait: mean(wait), ask_live }
1226}
1227
1228/// The pool's learned state from `s` into `pool`: the yields (PAVA over the bins), tokens per lesson,
1229/// and the latency model (least squares plus the residuals' 95% quantile).
1230fn pool_state(s: &Summary, pool: &mut PoolFit) {
1231    // Up to the first bin no lesson was shown in: past it (a gap between a tail sample's sparse places,
1232    // select/summary.rs `tail_of`) a bin's yield would be its prior alone, and a pool reaching there
1233    // would count as known (`pool_feasible`). A pool's own bins have no gap.
1234    let bins: Vec<(f64, f64)> = s.pool.bins.iter().take_while(|(n, _)| *n > 0).map(|&(n, k)| (1.0 + k as f64, 1.0 + n.saturating_sub(k) as f64)).collect();
1235    pool.yields = pava(&bins);
1236    pool.per_lesson = if s.pool.shown > 0 { s.pool.tokens as f64 / s.pool.shown as f64 } else { 0.0 };
1237    let obs = &s.pool.latency;
1238    pool.latency_n = obs.len() as u32;
1239    let mut read: Vec<u32> = obs.iter().map(|o| o.0).collect();
1240    read.sort_unstable_by(|a, b| b.cmp(a));
1241    pool.latency_reach = read.get(LATENCY_MIN - 1).copied().unwrap_or(0);
1242    if obs.len() >= 2 {
1243        let n = obs.len() as f64;
1244        let mx = obs.iter().map(|o| f64::from(o.0)).sum::<f64>() / n;
1245        let my = obs.iter().map(|o| f64::from(o.1)).sum::<f64>() / n;
1246        let sxx: f64 = obs.iter().map(|o| (f64::from(o.0) - mx).powi(2)).sum();
1247        let sxy: f64 = obs.iter().map(|o| (f64::from(o.0) - mx) * (f64::from(o.1) - my)).sum();
1248        let b = if sxx > 0.0 { (sxy / sxx).max(0.0) } else { 0.0 };
1249        let a = my - b * mx;
1250        let mut res: Vec<f64> = obs.iter().map(|o| f64::from(o.1) - a - b * f64::from(o.0)).collect();
1251        res.sort_by(f64::total_cmp);
1252        let q = res[((1.0 - DELTA) * (res.len() - 1) as f64).ceil() as usize];
1253        pool.latency = (a, b, q.max(0.0));
1254    }
1255}
1256
1257/// Pool adjacent violators: Beta posteriors per bin, merged so their means never rise with position;
1258/// each bin of a merged block carries the block's summed parameters.
1259fn pava(bins: &[(f64, f64)]) -> Vec<(f64, f64)> {
1260    let mut blocks: Vec<(f64, f64, usize)> = Vec::new();
1261    for &(a, b) in bins {
1262        blocks.push((a, b, 1));
1263        while blocks.len() >= 2 {
1264            let (x, y) = (blocks[blocks.len() - 2], blocks[blocks.len() - 1]);
1265            if x.0 / (x.0 + x.1) >= y.0 / (y.0 + y.1) {
1266                break;
1267            }
1268            blocks.pop();
1269            if let Some(last) = blocks.last_mut() {
1270                *last = (x.0 + y.0, x.1 + y.1, x.2 + y.2);
1271            }
1272        }
1273    }
1274    blocks.iter().flat_map(|&(a, b, n)| std::iter::repeat_n((a, b), n)).collect()
1275}
1276
1277/// A joint draw of the model for one check: the coefficients, and each unit's η without its route
1278/// (its session, lesson and judge effects included).
1279#[derive(Debug, Clone)]
1280struct Theta {
1281    b: [f64; P],
1282    base: Vec<f64>,
1283}
1284
1285/// The seed of each level of a check, by family and name, so draw k of one session or lesson is the
1286/// same at every check (the gate's ledger re-values past checks with the same draws).
1287struct Levels<'a> {
1288    name: &'a str,
1289    session: u64,
1290    judge: u64,
1291    lessons: Vec<u64>,
1292}
1293
1294impl<'a> Levels<'a> {
1295    fn of(session: &'a str, judge: &str, units: &[Unit]) -> Levels<'a> {
1296        Levels {
1297            name: session,
1298            session: hash(&[b"session", session.as_bytes()]),
1299            judge: hash(&[b"judge", judge.as_bytes()]),
1300            lessons: units.iter().map(|u| hash(&[b"lesson", u.lesson.as_bytes()])).collect(),
1301        }
1302    }
1303}
1304
1305/// Draw `seed` of `m`'s coefficients: β̂ + L z, coordinate by coordinate, each bounded slope drawn
1306/// from its normal given the coordinates before it truncated at 0 (`below_zero`: the same z mapped by
1307/// its inverse CDF, never a clamp, which would put half the mass of a slope whose mode is 0 on 0 itself)
1308/// and the later coordinates conditioned on the value it took. Under the n = 0 prior (the slopes
1309/// independent of the rest) each is exactly its prior, `DILUTION_PRIOR`'s half-normal; with data it is
1310/// the sequential (GHK) approximation of the truncated joint posterior.
1311fn draw_beta(m: &Model, seed: u64) -> [f64; P] {
1312    let mut rng = Rng::new(seed);
1313    let mut z: [f64; P] = std::array::from_fn(|_| normal(&mut rng));
1314    let spec = coefs();
1315    let l = |i: usize, k: usize| m.chol.get(i * P + k).copied().unwrap_or(0.0);
1316    let mut x = [0.0; P];
1317    for i in 0..P {
1318        let given = m.beta.get(i).copied().unwrap_or(0.0) + (0..i).map(|k| l(i, k) * z[k]).sum::<f64>();
1319        x[i] = given + l(i, i) * z[i];
1320        if spec[i].nonpos {
1321            x[i] = below_zero(given, l(i, i), z[i]);
1322            // The z that gives the truncated value: what the later coordinates are conditioned on.
1323            if l(i, i) > 0.0 {
1324                z[i] = (x[i] - given) / l(i, i);
1325            }
1326        }
1327    }
1328    x
1329}
1330
1331/// The draw of N(`mean`, `sd`²) truncated to ≤ 0 at the standard normal draw `z`: Φ⁻¹ of Φ(z) scaled
1332/// into the mass below 0, in logs so a bound far below the mean neither underflows nor leaves the
1333/// half-line. Monotone in z; `mean` held ≤ 0 when `sd` is 0.
1334fn below_zero(mean: f64, sd: f64, z: f64) -> f64 {
1335    if sd <= 0.0 || !sd.is_finite() {
1336        return mean.min(0.0);
1337    }
1338    let bound = -mean / sd;
1339    // The draw's lower tail Φ(z)·Φ(bound) and its upper 1 − that, each without cancellation.
1340    let ln_lower = ln_phi(z) + ln_phi(bound);
1341    let upper = phi(-z) + phi(z) * phi(-bound);
1342    (mean + sd * probit(ln_lower, upper)).min(0.0)
1343}
1344
1345/// The standard normal CDF Φ(x): O(1), relative error below 1.2e-7 (`erfc`).
1346fn phi(x: f64) -> f64 {
1347    0.5 * erfc(-x / std::f64::consts::SQRT_2)
1348}
1349
1350/// ln Φ(x), finite far into the lower tail (`erfcx`, the factor exp(−x²/2) taken out).
1351fn ln_phi(x: f64) -> f64 {
1352    if x < 0.0 { 0.5f64.ln() - 0.5 * x * x + erfcx(-x / std::f64::consts::SQRT_2).ln() } else { (-0.5 * erfc(x / std::f64::consts::SQRT_2)).ln_1p() }
1353}
1354
1355/// erfc(x) (`erfcx` for x ≥ 0, reflected below).
1356fn erfc(x: f64) -> f64 {
1357    let a = x.abs();
1358    let tail = (-a * a).exp() * erfcx(a);
1359    if x >= 0.0 { tail } else { 2.0 - tail }
1360}
1361
1362/// exp(x²)·erfc(x) for x ≥ 0: the Chebyshev fit of Numerical Recipes' `erfcc` (Press et al. 2007,
1363/// §6.2), its relative error below 1.2e-7 everywhere. Method: the coefficients are the fit's.
1364fn erfcx(x: f64) -> f64 {
1365    const C: [f64; 10] =
1366        [-1.265_512_23, 1.000_023_68, 0.374_091_96, 0.096_784_18, -0.186_288_06, 0.278_868_07, -1.135_203_98, 1.488_515_87, -0.822_152_23, 0.170_872_77];
1367    let t = 1.0 / (1.0 + 0.5 * x);
1368    t * C.iter().rev().fold(0.0, |acc, c| acc * t + c).exp()
1369}
1370
1371/// Φ⁻¹ of the probability whose lower tail is exp(`ln_lower`) and upper tail `upper` (both given, so
1372/// neither tail loses its digits): Acklam's rational approximation, relative error below 1.15e-9.
1373/// Method: the coefficients and the tails' breakpoint (0.02425) are Acklam's.
1374fn probit(ln_lower: f64, upper: f64) -> f64 {
1375    const A: [f64; 6] =
1376        [-3.969_683_028_665_376e1, 2.209_460_984_245_205e2, -2.759_285_104_469_687e2, 1.383_577_518_672_69e2, -3.066_479_806_614_716e1, 2.506_628_277_459_239];
1377    const B: [f64; 5] = [-5.447_609_879_822_406e1, 1.615_858_368_580_409e2, -1.556_989_798_598_866e2, 6.680_131_188_771_972e1, -1.328_068_155_288_572e1];
1378    const C: [f64; 6] =
1379        [-7.784_894_002_430_293e-3, -3.223_964_580_411_365e-1, -2.400_758_277_161_838, -2.549_732_539_343_734, 4.374_664_141_464_968, 2.938_163_982_698_783];
1380    const D: [f64; 4] = [7.784_695_709_041_462e-3, 3.224_671_290_700_398e-1, 2.445_134_137_142_996, 3.754_408_661_907_416];
1381    const LOW: f64 = 0.02425;
1382    let poly = |c: &[f64], x: f64| c.iter().fold(0.0, |acc, k| acc * x + k);
1383    // The tail from its log: q = √(−2 ln p).
1384    let tail = |ln_p: f64| {
1385        let q = (-2.0 * ln_p).sqrt();
1386        poly(&C, q) / (poly(&D, q) * q + 1.0)
1387    };
1388    let lower = ln_lower.exp();
1389    if lower < LOW {
1390        tail(ln_lower)
1391    } else if upper < LOW {
1392        -tail(upper.ln())
1393    } else {
1394        let q = lower - 0.5;
1395        let r = q * q;
1396        poly(&A, r) * q / (poly(&B, r) * r + 1.0)
1397    }
1398}
1399
1400/// Draw `seed` of `m` for a check, its coefficients `b` (`draw_beta` of the same seed): each level
1401/// from its marginal (an unseen level from its family's spread), seeded by the draw and the level.
1402/// The levels are drawn apart from β: wider than the joint posterior where a level and the intercept
1403/// trade off, so the gate errs cautious.
1404fn theta_with(m: &Model, b: [f64; P], feats: &[Feat], units: &[Unit], lv: &Levels, seed: u64) -> Theta {
1405    theta_by(m, b, feats, units, lv, |(mean, sd), h| mean + sd * normal(&mut Rng::new(mix(seed, h))))
1406}
1407
1408/// `m` for a check at its posterior centre (`choose`'s evidence rule): the coefficients at β̂, the
1409/// Laplace posterior's mean, which for a bounded slope is its mode (held ≤ 0; at the n = 0 prior 0,
1410/// not the half-normal's mean −0.08: dilution is not assumed, docs/evidence.md Rules v2), and each
1411/// level at its mean (an unseen one at 0). Not a draw: the draws (`draw_beta`) carry the truncated
1412/// spread the quantile is taken over.
1413fn theta_mean(m: &Model, feats: &[Feat], units: &[Unit], lv: &Levels) -> Theta {
1414    let spec = coefs();
1415    let b = std::array::from_fn(|i| {
1416        let x = m.beta.get(i).copied().unwrap_or(0.0);
1417        if spec[i].nonpos { x.min(0.0) } else { x }
1418    });
1419    theta_by(m, b, feats, units, lv, |(mean, _), _| mean)
1420}
1421
1422/// `m` for a check under the coefficients `b`, each level's effect from `level` (its posterior mean
1423/// and sd, an unseen level's (0, its family's spread), and its seed in `lv`).
1424fn theta_by(m: &Model, b: [f64; P], feats: &[Feat], units: &[Unit], lv: &Levels, level: impl Fn((f64, f64), u64) -> f64) -> Theta {
1425    let effect = |map: &BTreeMap<String, (f64, f64)>, name: &str, spread: f64, h: u64| level(map.get(name).copied().unwrap_or((0.0, spread)), h);
1426    let shared = effect(&m.sessions, lv.name, m.spread[SESSION], lv.session) + effect(&m.judges, &m.judge, m.spread[JUDGE], lv.judge);
1427    let base = feats
1428        .iter()
1429        .zip(units)
1430        .zip(&lv.lessons)
1431        .map(|((f, u), h)| dot(&b, &design(f, Route::Free, 1)) + shared + effect(&m.lessons, u.lesson, m.spread[LESSON], *h))
1432        .collect();
1433    Theta { b, base }
1434}
1435
1436/// Draw `seed` of `m` for a check (`draw_beta`, `theta_with`).
1437fn theta(m: &Model, feats: &[Feat], units: &[Unit], lv: &Levels, seed: u64) -> Theta {
1438    theta_with(m, draw_beta(m, seed), feats, units, lv, seed)
1439}
1440
1441/// The value of each route of each unit under one draw (module doc: τ).
1442struct Valuer<'a> {
1443    t: &'a Theta,
1444    feats: &'a [Feat],
1445    rates: &'a Rates,
1446}
1447
1448impl Valuer<'_> {
1449    fn n(&self) -> usize {
1450        self.feats.len()
1451    }
1452
1453    fn stop(&self) -> bool {
1454        self.feats.first().is_some_and(|f| f.stop)
1455    }
1456
1457    /// P(changed) of unit j not delivered.
1458    fn p_not(&self, j: usize) -> f64 {
1459        let b = &self.t.b;
1460        sigmoid(self.t.base[j] + b[NOT] + b[NOT_G] * (f64::from(self.feats[j].grade) - 2.0))
1461    }
1462
1463    fn ask(&self, j: usize, m: usize) -> f64 {
1464        let (b, f) = (&self.t.b, &self.feats[j]);
1465        let eta = self.t.base[j]
1466            + b[ASK]
1467            + b[ASK_M] * (m.max(1) as f64).ln()
1468            + b[ASK_STOP] * f64::from(u8::from(f.stop))
1469            + b[ASK_G3] * f64::from(u8::from(f.grade == 3));
1470        // A stop's question blocks, so it arrives; a live one of a check with a 3 only if it reaches the
1471        // drone, during the turn or waiting for the stop, which asks it; one of a check without a 3 no
1472        // stop asks (hook.rs `live_check`): like a hand-over it arrives only at a tool call or the next
1473        // prompt, at the free rate.
1474        let rate = match (self.stop(), f.three) {
1475            (true, _) => 1.0,
1476            (false, true) => self.rates.ask_live,
1477            (false, false) => self.rates.free_live,
1478        };
1479        rate * (sigmoid(eta) - self.p_not(j))
1480    }
1481
1482    fn free(&self, j: usize, m: usize) -> f64 {
1483        let rate = if self.stop() { self.rates.free_stop } else { self.rates.free_live };
1484        rate * (sigmoid(self.t.base[j] + self.t.b[FREE_M] * (m.max(1) as f64).ln()) - self.p_not(j))
1485    }
1486
1487    /// The check's value under `routes` (aligned with the units). Not now (a placebo too) is worth 0,
1488    /// at the stop and during the turn (the comparison is against not delivering): a lesson left for
1489    /// later earns its value at the check that delivers it, whose ledger entry credits it; credited
1490    /// here as well, the later rate × a hand-over's lift, it would count twice, at a rate no draw
1491    /// varies.
1492    fn value(&self, routes: &[Route]) -> f64 {
1493        let m_q = routes.iter().filter(|r| **r == Route::Ask).count();
1494        let m_f = routes.iter().filter(|r| **r == Route::Free).count();
1495        routes
1496            .iter()
1497            .enumerate()
1498            .map(|(j, r)| match r {
1499                Route::Ask => self.ask(j, m_q),
1500                Route::Free => self.free(j, m_f),
1501                Route::Placebo | Route::Not => 0.0,
1502            })
1503            .sum()
1504    }
1505}
1506
1507/// A stop's grade 3 is always asked: never routed away from a question without a pre-registered
1508/// randomised test of its own.
1509fn forced(f: &Feat) -> bool {
1510    f.stop && f.grade == 3
1511}
1512
1513/// One draw's value of every unit on every route and size (`Valuer`), computed once: the solver and
1514/// the gate read sums of it, never a sigmoid again.
1515struct Table {
1516    n: usize,
1517    /// `ask[m·n + j]`, `free[m·n + j]` for m in 0..=n (0 unused); not now is worth 0 (`Valuer::value`).
1518    /// Whether j is forced, and whether it may not go free (a lesson the session already had,
1519    /// `nofree_of`).
1520    ask: Vec<f64>,
1521    free: Vec<f64>,
1522    forced: Vec<bool>,
1523    nofree: Vec<bool>,
1524}
1525
1526impl Table {
1527    /// The table of `v`, the units marked in `nofree` (aligned, or shorter: the rest may go free) kept
1528    /// from the free route.
1529    fn of(v: &Valuer, nofree: &[bool]) -> Table {
1530        let n = v.n();
1531        let mut ask = vec![0.0; (n + 1) * n];
1532        let mut free = vec![0.0; (n + 1) * n];
1533        for m in 1..=n {
1534            for j in 0..n {
1535                ask[m * n + j] = v.ask(j, m);
1536                free[m * n + j] = v.free(j, m);
1537            }
1538        }
1539        Table { n, ask, free, forced: v.feats.iter().map(forced).collect(), nofree: (0..n).map(|j| nofree.get(j).copied().unwrap_or(false)).collect() }
1540    }
1541
1542    /// The check's value under `routes` (`Valuer::value`), read from the table: the tests' check that
1543    /// the solver's entries value as the model does.
1544    #[cfg(test)]
1545    fn value(&self, routes: &[Route]) -> f64 {
1546        let m_q = routes.iter().filter(|r| **r == Route::Ask).count();
1547        let m_f = routes.iter().filter(|r| **r == Route::Free).count();
1548        let n = self.n;
1549        routes
1550            .iter()
1551            .enumerate()
1552            .map(|(j, r)| match r {
1553                Route::Ask => self.ask[m_q * n + j],
1554                Route::Free => self.free[m_f * n + j],
1555                Route::Placebo | Route::Not => 0.0,
1556            })
1557            .sum()
1558    }
1559}
1560
1561/// An f64 ordered by `total_cmp`, for a heap.
1562#[derive(Debug, Clone, Copy, PartialEq)]
1563struct Total(f64);
1564
1565impl Eq for Total {}
1566
1567impl PartialOrd for Total {
1568    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
1569        Some(self.cmp(other))
1570    }
1571}
1572
1573impl Ord for Total {
1574    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
1575        self.0.total_cmp(&other.0)
1576    }
1577}
1578
1579/// Into `out`: the sum of the `k` largest of the first s values, for every s (NEG_INFINITY while
1580/// s < k). O(n log k).
1581fn top_sums(values: impl Iterator<Item = f64>, k: usize, out: &mut Vec<f64>, heap: &mut std::collections::BinaryHeap<std::cmp::Reverse<Total>>) {
1582    out.clear();
1583    heap.clear();
1584    out.push(if k == 0 { 0.0 } else { f64::NEG_INFINITY });
1585    let mut sum = 0.0;
1586    for x in values {
1587        if k > 0 {
1588            if heap.len() < k {
1589                heap.push(std::cmp::Reverse(Total(x)));
1590                sum += x;
1591            } else if let Some(min) = heap.peek().map(|r| r.0.0)
1592                && x > min
1593            {
1594                heap.pop();
1595                heap.push(std::cmp::Reverse(Total(x)));
1596                sum += x - min;
1597            }
1598        }
1599        out.push(if heap.len() < k { f64::NEG_INFINITY } else { sum });
1600    }
1601}
1602
1603/// The routes maximising the check's value (`Table::value`) over every routing with at most `rail`
1604/// asks (more only for the forced ones) and `cap` free lessons, the forced units asked and the units
1605/// that may not go free (`Table::nofree`) never free. Exact: for fixed sizes (m_Q, m_F) an optimum
1606/// asks before it hands over in the order of ask's gain over free's (an exchange argument; a unit that
1607/// may not go free has an infinite key, so it leads, and the split never passes it to the free side),
1608/// so per size pair one sort and one sweep over the split. O(n³ log n). Deterministic: ties keep the
1609/// first found (fewer asks, fewer free, earlier split).
1610fn solve(t: &Table, rail: usize, cap: usize) -> Vec<Route> {
1611    let n = t.n;
1612    let nf_forced = t.forced.iter().filter(|x| **x).count();
1613    let open: Vec<usize> = (0..n).filter(|&j| !t.forced[j]).collect();
1614    // The open units that may not go free: first in every order below, so every split leaves them on
1615    // the ask-or-not side.
1616    let held = open.iter().filter(|&&j| t.nofree[j]).count();
1617    let q_max = rail.max(nf_forced).min(n);
1618    let f_max = cap.min(open.len() - held);
1619    // Gains against not now, which is worth 0 (`Valuer::value`), so a route's value; size 0 gains nothing.
1620    let ask = |m: usize, j: usize| if m == 0 { 0.0 } else { t.ask[m * n + j] };
1621    let free = |m: usize, j: usize| if m == 0 { 0.0 } else { t.free[m * n + j] };
1622    let mut order = open.clone();
1623    let mut keys = vec![0.0; n];
1624    let (mut pre, mut suf) = (Vec::with_capacity(n + 1), Vec::with_capacity(n + 1));
1625    let mut heap = std::collections::BinaryHeap::with_capacity(n + 1);
1626    let sort = |order: &mut Vec<usize>, keys: &mut Vec<f64>, mq: usize, mf: usize| {
1627        for &j in &open {
1628            keys[j] = if t.nofree[j] { f64::INFINITY } else { ask(mq, j) - free(mf, j) };
1629        }
1630        order.clear();
1631        order.extend_from_slice(&open);
1632        order.sort_by(|&a, &b| keys[b].total_cmp(&keys[a]).then(a.cmp(&b)));
1633    };
1634    let mut best: Option<(f64, usize, usize, usize)> = None;
1635    for mq in nf_forced..=q_max {
1636        let k = mq - nf_forced;
1637        if k > open.len() {
1638            break;
1639        }
1640        let fixed: f64 = (0..n).filter(|&j| t.forced[j]).map(|j| ask(mq, j)).sum();
1641        for mf in 0..=f_max.min(open.len() - k) {
1642            sort(&mut order, &mut keys, mq, mf);
1643            top_sums(order.iter().map(|&j| ask(mq, j)), k, &mut pre, &mut heap);
1644            top_sums(order.iter().rev().map(|&j| free(mf, j)), mf, &mut suf, &mut heap);
1645            for split in k.max(held)..=open.len() - mf {
1646                let total = fixed + pre[split] + suf[open.len() - split];
1647                if best.is_none_or(|b| total > b.0) {
1648                    best = Some((total, mq, mf, split));
1649                }
1650            }
1651        }
1652    }
1653    let mut routes = vec![Route::Not; n];
1654    for j in (0..n).filter(|&j| t.forced[j]) {
1655        routes[j] = Route::Ask;
1656    }
1657    let Some((_, mq, mf, split)) = best else { return routes };
1658    sort(&mut order, &mut keys, mq, mf);
1659    let pick = |within: &[usize], gain: &dyn Fn(usize) -> f64, k: usize| -> Vec<usize> {
1660        let mut c = within.to_vec();
1661        c.sort_by(|&a, &b| gain(b).total_cmp(&gain(a)).then(a.cmp(&b)));
1662        c.truncate(k);
1663        c
1664    };
1665    for j in pick(&order[..split], &|j| ask(mq, j), mq - nf_forced) {
1666        routes[j] = Route::Ask;
1667    }
1668    for j in pick(&order[split..], &|j| free(mf, j), mf) {
1669        routes[j] = Route::Free;
1670    }
1671    routes
1672}
1673
1674/// The fixed rule's routes for a session that already had `had` (conscience.rs `graded_without`
1675/// without a cap), by position: a lesson the model named twice keeps each copy's route.
1676#[must_use]
1677pub fn fixed_routes(units: &[Unit], had: &[String]) -> Vec<Route> {
1678    let ls: Vec<Challenge> = units.iter().map(|u| Challenge { id: u.lesson.to_string(), ..Default::default() }).collect();
1679    let applied: Vec<(&Challenge, u8)> = ls.iter().zip(units).map(|(c, u)| (c, u.grade)).collect();
1680    let (ask, free) = conscience::graded_without(&applied, None, had);
1681    let holds = |v: &[&Challenge], c: &Challenge| v.iter().any(|x| std::ptr::eq(*x, c));
1682    ls.iter()
1683        .map(|c| match (holds(&ask, c), holds(&free, c)) {
1684            (true, _) => Route::Ask,
1685            (_, true) => Route::Free,
1686            _ => Route::Not,
1687        })
1688        .collect()
1689}
1690
1691/// Per unit, whether its lesson is one the session already had (`had`): it may not go free.
1692fn nofree_of(units: &[Unit], had: &[String]) -> Vec<bool> {
1693    units.iter().map(|u| had.iter().any(|h| h == u.lesson)).collect()
1694}
1695
1696/// The check a decision is made for, as the model sees it.
1697#[derive(Debug, Clone, Copy)]
1698pub struct Check<'a> {
1699    pub session: &'a str,
1700    pub stop: bool,
1701    /// The check's pool tokens (0 unknown: the baseline's).
1702    pub tokens: u32,
1703}
1704
1705/// `Decision::source` values. Protocol: logged as the graded record's `source` (outcome.rs), which the
1706/// summary fold, `borg debug select`, `borg top` and the web's Selection view read back.
1707/// `FIXED`: the fixed rule because the policy cannot decide yet (cold start, an empty gate, no model).
1708pub const FIXED: &str = "fixed";
1709/// `SIZE`: the fixed rule past `SOLVE_MAX` applies.
1710pub const SIZE: &str = "size";
1711/// `GATE`: the gate kept the fixed rule, its allowance too short for the deviation's risk (the
1712/// evidence rule's, or the randomised test's).
1713pub const GATE: &str = "gate";
1714/// `EVIDENCE`: the evidence rule's routes (`evident`): the routes best under the posterior mean where
1715/// they beat the fixed rule at the `DELTA` quantile over the `DRAWS` joint draws, else, when the
1716/// randomised test does not run (the stop, no lesson to test, α 0), the fixed rule's.
1717pub const EVIDENCE: &str = "evidence";
1718/// `EXPERIMENT`: a check of the randomised question-count test whose draw s routes differently from
1719/// the fixed rule (`Decision::xk`, `xs`).
1720pub const EXPERIMENT: &str = "experiment";
1721/// `EXPERIMENT_CONTROL`: a check of the test whose draw s is the fixed rule's own count (s = 0
1722/// without a 3, s = k with one), so it routes as the fixed rule: the test's control arm.
1723pub const EXPERIMENT_CONTROL: &str = "experiment_control";
1724
1725/// A check's decision: a route per unit, each unit's chance of being asked (`p`) and handed over free
1726/// (`pf`), what set the routes (`source`: `FIXED`, `SIZE`, `GATE`, `EVIDENCE`, `EXPERIMENT`,
1727/// `EXPERIMENT_CONTROL`), the quality the gate may still risk
1728/// after the ledger's past (`Gate`), at its `DELTA` quantile, and how much of it this decision takes
1729/// (`risk`: how far its routes lower that quantile, 0 for the fixed rule's). The gate admits a
1730/// deviation exactly when `risk` ≤ `gate`, which `borg debug select` P3 checks. A check the randomised
1731/// question-count test considered (sources `EXPERIMENT`, `EXPERIMENT_CONTROL`, and `GATE` when the
1732/// gate refused its draw) carries `xk`, the fixed rule's delivered non-3 lessons that may go free, and
1733/// `xs`, the drawn number of them to ask (on `GATE` the draw the gate refused: the routes are the fixed
1734/// rule's); both None on every other check (outcome.rs `xk`, `xs`).
1735#[derive(Debug, Clone, PartialEq)]
1736pub struct Decision {
1737    pub routes: Vec<Route>,
1738    pub p: Vec<f64>,
1739    pub pf: Vec<f64>,
1740    pub source: &'static str,
1741    pub gate: f64,
1742    pub risk: f64,
1743    pub xk: Option<u32>,
1744    pub xs: Option<u32>,
1745}
1746
1747/// The `DELTA` quantile of `v` (the ⌈δ·n⌉-th smallest: the lower side, cautious).
1748fn lower_quantile(v: &mut [f64]) -> f64 {
1749    if v.is_empty() {
1750        return 0.0;
1751    }
1752    v.sort_by(f64::total_cmp);
1753    v[((DELTA * v.len() as f64).ceil() as usize).clamp(1, v.len()) - 1]
1754}
1755
1756/// The quality `gate` may still risk below the fixed rule at `ALPHA`: its allowance plus the ledger's
1757/// `DELTA` quantile, not held at 0 (`remaining_at` is), so a test can see an overspend as a negative.
1758#[cfg(test)]
1759#[must_use]
1760pub(crate) fn remaining(gate: &Gate) -> f64 {
1761    gate.allowance + lower_quantile(&mut gate.gain.clone())
1762}
1763
1764/// `gate`'s allowance at the risk share `alpha` (select.rs `risk_of`): `choose`'s, for the evidence
1765/// rule's deviations and the randomised test's alike.
1766fn allowance_at(gate: &Gate, alpha: f64) -> f64 {
1767    // Derived: the ledger accrues `ALPHA` × the fixed rule's value per check (`add_with`), so its
1768    // allowance scaled by alpha / `ALPHA` is exactly alpha's; 0 stays 0 whatever the ledger holds.
1769    if alpha > 0.0 { gate.allowance * (alpha / ALPHA) } else { 0.0 }
1770}
1771
1772/// What `gate` may still risk below the fixed rule at the risk share `alpha`, never below 0: the gate
1773/// in force (`Decision::gate`), which the monitoring shows.
1774#[must_use]
1775pub fn remaining_at(gate: &Gate, alpha: f64) -> f64 {
1776    let (allowance, ledger) = gate_parts(gate, alpha);
1777    (allowance + ledger).max(0.0)
1778}
1779
1780/// The two parts `remaining_at` adds: `gate`'s allowance at the risk share `alpha`, and its ledger's
1781/// `DELTA` quantile (what the policy's deviations did against the fixed rule, ≤ 0 once they cost).
1782/// The monitoring tells a cold start (no allowance yet) from a spent gate (an allowance the ledger's
1783/// quantile used up) by them.
1784#[must_use]
1785pub fn gate_parts(gate: &Gate, alpha: f64) -> (f64, f64) {
1786    (allowance_at(gate, alpha), lower_quantile(&mut gate.gain.clone()))
1787}
1788
1789/// The seed of the gate's draw `k` under `fit`: shared by every check, so the ledger and a decision
1790/// re-value with the same joint draws.
1791fn gate_seed(fit: &Fit, k: usize) -> u64 {
1792    mix(hash(&[b"gate", fit.key.as_bytes()]), k as u64)
1793}
1794
1795/// A check's joint draws of the model (`gate_seed`: the ledger's, so a decision and its ledger entry
1796/// re-value with the same draws) and the fixed rule's value under each.
1797struct Draws<'a> {
1798    thetas: Vec<Theta>,
1799    feats: Vec<Feat>,
1800    rates: &'a Rates,
1801    base: Vec<f64>,
1802}
1803
1804impl<'a> Draws<'a> {
1805    fn of(model: &Model, fit: &'a Fit, units: &[Unit], check: &Check, lv: &Levels, base: &[Route]) -> Draws<'a> {
1806        let feats = Feat::of(units, check.stop, check.tokens);
1807        let thetas: Vec<Theta> = (0..DRAWS).map(|k| theta(model, &feats, units, lv, gate_seed(fit, k))).collect();
1808        let mut d = Draws { thetas, feats, rates: &fit.rates, base: Vec::new() };
1809        d.base = (0..DRAWS).map(|k| d.value(k, base)).collect();
1810        d
1811    }
1812
1813    /// Draw `k`'s value of `routes` (`Valuer::value`).
1814    fn value(&self, k: usize, routes: &[Route]) -> f64 {
1815        Valuer { t: &self.thetas[k], feats: &self.feats, rates: self.rates }.value(routes)
1816    }
1817
1818    /// Per draw, the value of `routes` minus the fixed rule's.
1819    fn gains(&self, routes: &[Route]) -> Vec<f64> {
1820        self.base.iter().enumerate().map(|(k, b)| self.value(k, routes) - b).collect()
1821    }
1822}
1823
1824/// The evidence rule (module doc): the routes best under the posterior mean (`theta_mean`, `solve`),
1825/// when they are not the fixed rule's `base` and gain on it at the `DELTA` quantile over the joint
1826/// draws `d`; else None (the fixed rule). The units in `nofree` never go free.
1827fn evident(model: &Model, d: &Draws, units: &[Unit], lv: &Levels, base: &[Route], nofree: &[bool]) -> Option<Vec<Route>> {
1828    let t = theta_mean(model, &d.feats, units, lv);
1829    let best = solve(&Table::of(&Valuer { t: &t, feats: &d.feats, rates: d.rates }, nofree), crate::attention::RAIL, conscience::CAP);
1830    (best != base && lower_quantile(&mut d.gains(&best)) > 0.0).then_some(best)
1831}
1832
1833/// The randomised question-count test's lessons (module doc), by position: the applies the fixed rule
1834/// delivers (`base`: asks or hands over) that are not a 3 and whose lesson the session never had
1835/// (`had`: such a lesson may not go free, so it keeps the fixed rule's route), in the fixed rule's order
1836/// (the higher grade first, then the model's: select.rs `ordered`). Their number is the test's k.
1837pub(crate) fn tested(units: &[Unit], base: &[Route], had: &[String]) -> Vec<usize> {
1838    let mut order: Vec<usize> =
1839        (0..units.len()).filter(|&j| units[j].grade != 3 && matches!(base[j], Route::Ask | Route::Free) && !had.iter().any(|h| h == units[j].lesson)).collect();
1840    order.sort_by_key(|&j| std::cmp::Reverse(units[j].grade));
1841    order
1842}
1843
1844/// The test's routes for the count `s`: `base` with the first `s` of `order` (`tested`) asked and the
1845/// rest of it handed over.
1846fn test_routes(base: &[Route], order: &[usize], s: usize) -> Vec<Route> {
1847    let mut routes = base.to_vec();
1848    for (i, &j) in order.iter().enumerate() {
1849        routes[j] = if i < s { Route::Ask } else { Route::Free };
1850    }
1851    routes
1852}
1853
1854/// The test's count at the check whose seed is `seed`: s uniform in 0..=k, on the test's own stream
1855/// of the seed (apart from the placebo's, select.rs `draw_placebo`). The modulo's bias is below
1856/// (k + 1)/2⁶⁴.
1857fn draw_count(seed: u64, k: usize) -> usize {
1858    (hash(&[b"question-count", &seed.to_le_bytes()]) % (k as u64 + 1)) as usize
1859}
1860
1861/// The routes of `units` at `check` for a session that already had the lessons `had` (module doc: the
1862/// decision), none of those handed over free (its `pf` 0) and the fixed rule they are gated against
1863/// the same (`fixed_routes` with `had`). Without a model, with an empty gate or past `SOLVE_MAX`
1864/// applies, exactly the fixed rule, its choice as the propensities. The gate's bound holds for
1865/// decisions made one after another, each against a `gate` with every earlier decision folded in
1866/// (`ledger_add_without`, with the same `had`): two checks deciding against the same `gate` could each
1867/// take all it may still risk, so a caller whose checks run at once decides and records each under one
1868/// lock.
1869///
1870/// `alpha` is the share of the fixed rule's value the engineer tolerates risking for learning (select.rs
1871/// `risk_of`, `ALPHA` unless the collective sets `risk`): the gate's allowance in this decision is the
1872/// ledger's (accrued at `ALPHA`) scaled to it, for the evidence rule's deviations and the test's alike.
1873/// The randomised question-count test runs on a live check (`check.stop` false) whose evidence keeps
1874/// the fixed rule (an evidence deviation the gate refuses is not tested either), only while `alpha` is
1875/// above 0, its count drawn from `seed`.
1876#[must_use]
1877pub fn choose(units: &[Unit], check: &Check, fit: &Fit, gate: &Gate, alpha: f64, seed: u64, had: &[String]) -> Decision {
1878    let base = fixed_routes(units, had);
1879    let fixed = |source: &'static str| Decision {
1880        p: share(&[&base], Route::Ask),
1881        pf: share(&[&base], Route::Free),
1882        routes: base.clone(),
1883        source,
1884        gate: 0.0,
1885        risk: 0.0,
1886        xk: None,
1887        xs: None,
1888    };
1889    let Some(model) = fit.model.as_ref().filter(|m| m.current()) else { return fixed(FIXED) };
1890    if gate.allowance <= 0.0 || gate.gain.len() != DRAWS || units.is_empty() {
1891        return fixed(FIXED);
1892    }
1893    if units.len() > SOLVE_MAX {
1894        return fixed(SIZE);
1895    }
1896    let lv = Levels::of(check.session, &model.judge, units);
1897    let d = Draws::of(model, fit, units, check, &lv, &base);
1898    let allowance = allowance_at(gate, alpha);
1899    let held = lower_quantile(&mut gate.gain.clone());
1900    let remaining = remaining_at(gate, alpha);
1901    // What `routes` take of the remaining budget, None when the gate refuses them: admitted iff
1902    // allowance + q(gain + their gain) ≥ 0, that is iff `risk` = q(gain) − q(gain + their gain) ≤
1903    // allowance + q(gain).
1904    let admit = |routes: &[Route]| -> Option<f64> {
1905        if routes == base {
1906            return Some(0.0);
1907        }
1908        let mut after: Vec<f64> = d.gains(routes).iter().zip(&gate.gain).map(|(x, g)| g + x).collect();
1909        let q = lower_quantile(&mut after);
1910        (q >= -allowance).then_some((held - q).max(0.0))
1911    };
1912    if let Some(best) = evident(model, &d, units, &lv, &base, &nofree_of(units, had)) {
1913        return match admit(&best) {
1914            Some(risk) => Decision {
1915                p: share(&[&best], Route::Ask),
1916                pf: share(&[&best], Route::Free),
1917                routes: best,
1918                source: EVIDENCE,
1919                gate: remaining,
1920                risk,
1921                xk: None,
1922                xs: None,
1923            },
1924            None => Decision { gate: remaining, ..fixed(GATE) },
1925        };
1926    }
1927    let order = tested(units, &base, had);
1928    if check.stop || alpha <= 0.0 || order.is_empty() {
1929        return Decision { gate: remaining, ..fixed(EVIDENCE) };
1930    }
1931    // Every count's routes as played (a count the gate refuses plays the fixed rule's), so each unit's
1932    // propensity is its exact share over the k + 1 equally likely counts.
1933    let k = order.len();
1934    let arms: Vec<(Vec<Route>, Option<f64>)> = (0..=k)
1935        .map(|s| {
1936            let routes = test_routes(&base, &order, s);
1937            let risk = admit(&routes);
1938            (if risk.is_some() { routes } else { base.clone() }, risk)
1939        })
1940        .collect();
1941    let played: Vec<&Vec<Route>> = arms.iter().map(|(r, _)| r).collect();
1942    let s = draw_count(seed, k);
1943    let (routes, risk) = &arms[s];
1944    let source = match risk {
1945        None => GATE,
1946        Some(_) if *routes == base => EXPERIMENT_CONTROL,
1947        Some(_) => EXPERIMENT,
1948    };
1949    let count = |x: usize| Some(u32::try_from(x).unwrap_or(u32::MAX));
1950    Decision {
1951        p: share(&played, Route::Ask),
1952        pf: share(&played, Route::Free),
1953        routes: routes.clone(),
1954        source,
1955        gate: remaining,
1956        risk: risk.unwrap_or(0.0),
1957        xk: count(k),
1958        xs: count(s),
1959    }
1960}
1961
1962/// The decision rule without the randomised question-count test, for a session that already had `had`
1963/// (none of those handed over free, gated against `fixed_routes` with the same `had`): `borg debug
1964/// select` P4a (under the n = 0 prior it must be the fixed rule on every check) and P4b (recall under
1965/// the store's posterior). The evidence rule (`evident`): the routes best under the posterior mean,
1966/// played only when they beat the fixed rule at the `DELTA` quantile over the `DRAWS` joint draws, else
1967/// the fixed rule; `choose`'s routes with α 0 under a gate that admits them. Deterministic: it draws
1968/// nothing of its own (its joint draws are the gate's, `gate_seed`), so it takes no seed. The fixed
1969/// rule whatever the evidence: no model, no applies, past `SOLVE_MAX`.
1970#[must_use]
1971pub fn decide_on_evidence(units: &[Unit], check: &Check, fit: &Fit, had: &[String]) -> Vec<Route> {
1972    let base = fixed_routes(units, had);
1973    let Some(model) = fit.model.as_ref().filter(|m| m.current()) else { return base };
1974    if units.is_empty() || units.len() > SOLVE_MAX {
1975        return base;
1976    }
1977    let lv = Levels::of(check.session, &model.judge, units);
1978    let d = Draws::of(model, fit, units, check, &lv, &base);
1979    evident(model, &d, units, &lv, &base, &nofree_of(units, had)).unwrap_or(base)
1980}
1981
1982/// Per unit, the share of the equally likely routings `draws` that give it `route`, rounded (outcome.rs
1983/// `round_p`): one routing's 0 or 1, the test's k + 1 counts' exact shares.
1984fn share(draws: &[&Vec<Route>], route: Route) -> Vec<f64> {
1985    let n = draws.first().map_or(0, |d| d.len());
1986    (0..n).map(|j| crate::outcome::round_p(draws.iter().filter(|d| d[j] == route).count() as f64 / draws.len() as f64)).collect()
1987}
1988
1989/// `gate` with one more check of the learned policy, decided for a session that already had `had`
1990/// (the graded record's `nofree`): per joint draw k, the value of the routes it took (`routes`) minus
1991/// the fixed rule's with the same `had` (the rule `choose` gated it against), and `ALPHA` × the fixed
1992/// rule's value at its draws' mean (≥ 0; `choose` scales it to the collective's α). The prior fit adds
1993/// nothing: the ledger opens with a model.
1994pub fn ledger_add_without(fit: &Fit, gate: &mut Gate, check: &Check, units: &[Unit], routes: &[Route], had: &[String]) {
1995    let Some(model) = fit.model.as_ref().filter(|m| m.current()) else { return };
1996    let betas: Vec<[f64; P]> = (0..DRAWS).map(|k| draw_beta(model, gate_seed(fit, k))).collect();
1997    add_with(fit, &betas, gate, check, units, routes, had);
1998}
1999
2000/// `ledger_add_without` with the gate's coefficient draws (`draw_beta` of `fit`'s model per
2001/// `gate_seed`) drawn once.
2002fn add_with(fit: &Fit, betas: &[[f64; P]], gate: &mut Gate, check: &Check, units: &[Unit], routes: &[Route], had: &[String]) {
2003    let Some(model) = fit.model.as_ref().filter(|m| m.current()) else { return };
2004    if units.is_empty() || units.len() != routes.len() {
2005        return;
2006    }
2007    if gate.gain.len() != DRAWS {
2008        gate.gain = vec![0.0; DRAWS];
2009    }
2010    let base = fixed_routes(units, had);
2011    // A placebo is not delivered: as the policy decided, not now.
2012    let routes: Vec<Route> = routes.iter().map(|r| if *r == Route::Placebo { Route::Not } else { *r }).collect();
2013    let feats = Feat::of(units, check.stop, check.tokens);
2014    let lv = Levels::of(check.session, &model.judge, units);
2015    let mut mean = 0.0;
2016    for (k, (g, b)) in gate.gain.iter_mut().zip(betas).enumerate() {
2017        let t = theta_with(model, *b, &feats, units, &lv, gate_seed(fit, k));
2018        let v = Valuer { t: &t, feats: &feats, rates: &fit.rates };
2019        let fixed = v.value(&base);
2020        *g += v.value(&routes) - fixed;
2021        mean += fixed / DRAWS as f64;
2022    }
2023    gate.allowance += ALPHA * mean.max(0.0);
2024    gate.checks += 1;
2025    if routes != base {
2026        gate.deviated += 1;
2027    }
2028}
2029
2030/// The gate's ledger over every learned check of `s` under `s.fit` (at `refit`), each as the fold adds
2031/// it (select/summary.rs, `ledger_add_without` with the record's `nofree`, `logged_had`).
2032#[must_use]
2033pub fn ledger(s: &Summary) -> Gate {
2034    let mut gate = Gate::default();
2035    let Some(model) = s.fit.model.as_ref().filter(|m| m.current()) else { return gate };
2036    let betas: Vec<[f64; P]> = (0..DRAWS).map(|k| draw_beta(model, gate_seed(&s.fit, k))).collect();
2037    for c in s.checks.iter().filter(|c| c.learned) {
2038        let (units, _, routes) = check_view(s, c);
2039        let session = s.sessions.get(c.tick as usize).map_or("", |x| x.id.as_str());
2040        add_with(&s.fit, &betas, &mut gate, &Check { session, stop: c.stop, tokens: c.tokens }, &units, &routes, &logged_had(s, c));
2041    }
2042    gate
2043}
2044
2045/// The monitor's reading of the learned policy against the fixed rule (module doc).
2046#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
2047pub struct Monitor {
2048    /// The store went back to the fixed rule (sticky: select/summary.rs `REVERTED`).
2049    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
2050    pub reverted: bool,
2051    /// Sessions and applies of the learned policy it read.
2052    #[serde(default, skip_serializing_if = "is_default")]
2053    pub sessions: u32,
2054    #[serde(default, skip_serializing_if = "is_default")]
2055    pub units: u32,
2056    /// Per apply, the mean of (policy − fixed rule) + α × the fixed rule's value, and the confidence
2057    /// sequence's upper bound on it (None before `CS_MIN` sessions): below 0, the policy costs more
2058    /// than α of the fixed rule's quality.
2059    #[serde(default, skip_serializing_if = "is_default")]
2060    pub mean: f64,
2061    #[serde(default, skip_serializing_if = "Option::is_none")]
2062    pub ucb: Option<f64>,
2063    /// How far its weights can be trusted (borg top `Evaluation`): the labelled applies weighed, the
2064    /// effective number of the weights the estimate used (truncated, normalised per route: (Σw)² /
2065    /// Σw²), the largest of them against their mean, and the applies whose weight the √N cap cut; each
2066    /// over the applies whose weight can move the estimate: a residual in use (`ROUTE_LABELS_MIN`) on a
2067    /// taken route where the policy and the fixed rule differ (a placebo both rules leave undelivered
2068    /// has none, however large its weight).
2069    #[serde(default, skip_serializing_if = "is_default")]
2070    pub labelled: u32,
2071    #[serde(default, skip_serializing_if = "is_default")]
2072    pub ess: f64,
2073    #[serde(default, skip_serializing_if = "is_default")]
2074    pub max_weight: f64,
2075    #[serde(default, skip_serializing_if = "is_default")]
2076    pub truncated: u32,
2077}
2078
2079/// The monitor of `s` (module doc): per session, the doubly robust difference between the policy
2080/// that ran (its logged propensities) and the fixed rule, each apply's outcome model fitted without
2081/// its session's half (cross-fitting by the parity of the session's tick), the inverse-probability
2082/// weights truncated at √N × their mean and normalised per route, a route's weighted residual in use
2083/// from `ROUTE_LABELS_MIN` labelled applies on (the direct-method term alone before); plus α × the
2084/// fixed rule's value;
2085/// per apply, a session's mean an increment, in session order, under a one-sided time-uniform
2086/// confidence sequence at `DELTA` (`upper_bound`). Trips when its upper bound falls below 0.
2087#[must_use]
2088pub fn monitor(s: &Summary) -> Monitor {
2089    let spec = coefs();
2090    let d = Data::of(s, false);
2091    let Some(model) = &s.fit.model else { return Monitor::default() };
2092    let sd: [f64; P] = std::array::from_fn(|k| model.sd.get(k).copied().unwrap_or(spec[k].sd));
2093    let spread = model.spread;
2094    let folds: Vec<Option<Est<P, FAMILIES>>> = (0..2u32)
2095        .map(|half| {
2096            let train: Vec<Obs<P, FAMILIES>> =
2097                d.weighted(s.sessions.len() as u32, s.fit.half_life).into_iter().zip(&d.tick).filter(|(_, t)| *t % 2 != half).map(|(o, _)| o).collect();
2098            map(&train, d.levels, &spec, &sd, &spread, None)
2099        })
2100        .collect();
2101    // Per learned apply: the session, the per-route difference terms before the weights.
2102    struct Term {
2103        tick: u32,
2104        diff: [f64; ARMS],
2105        p_hat: [f64; ARMS],
2106        taken: usize,
2107        y: Option<f64>,
2108        w: f64,
2109        vb: f64,
2110    }
2111    let mut terms: Vec<Term> = Vec::new();
2112    for c in s.checks.iter().filter(|c| c.learned) {
2113        let Some(est) = &folds[(c.tick % 2) as usize] else { continue };
2114        let (units, feats, routes) = check_view(s, c);
2115        // The fixed rule for the lessons the session had, as the decision was gated against it.
2116        let base = fixed_routes(&units, &logged_had(s, c));
2117        let (m_q, m_f) = (usize::from(c.m_q).max(1), usize::from(c.m_f).max(1));
2118        for (j, f) in feats.iter().enumerate() {
2119            let i = c.first as usize + j;
2120            let a = &s.applies[i];
2121            let lv = d.lv_of.get(&i).copied().unwrap_or([None; FAMILIES]);
2122            // The held-out session's own effect is not in its fold's fit.
2123            let eta = |route: Route, m: usize| -> f64 {
2124                dot(&est.beta, &design(f, route, m))
2125                    + lv[1].map_or(0.0, |l| est.u[LESSON].get(l).copied().unwrap_or(0.0))
2126                    + lv[2].map_or(0.0, |l| est.u[JUDGE].get(l).copied().unwrap_or(0.0))
2127            };
2128            let p_hat = [sigmoid(eta(Route::Ask, m_q)), sigmoid(eta(Route::Free, m_f)), sigmoid(eta(Route::Not, 1))];
2129            let mu = [a.p, a.pf, (1.0 - a.p - a.pf).max(0.0)];
2130            let (taken, fixed) = (routes[j].arm(), base[j].arm());
2131            let diff: [f64; ARMS] = std::array::from_fn(|x| mu[x] - f64::from(u8::from(x == fixed)));
2132            let y = match a.label {
2133                Label::Changed => Some(1.0),
2134                Label::Ignored => Some(0.0),
2135                _ => None,
2136            };
2137            // Floors: a logged ψ or μ of 0 for a route that happened is rounding (outcome.rs `round_p`),
2138            // so the least the log can say (ψ at 1/1000, μ at one draw of `DRAWS`).
2139            let psi = if a.route == Route::Placebo { a.psi.max(PSI_FLOOR) } else { 1.0 };
2140            let judged_p = d.judged_p.get(&i).copied().unwrap_or(1.0).max(1.0 / MAX_IPW);
2141            let w = if y.is_some() { 1.0 / (mu[taken].max(1.0 / DRAWS as f64) * judged_p * psi) } else { 0.0 };
2142            let vb = p_hat[fixed] - p_hat[2];
2143            terms.push(Term { tick: c.tick, diff, p_hat, taken, y, w, vb });
2144        }
2145    }
2146    let labelled: Vec<f64> = terms.iter().filter(|t| t.y.is_some()).map(|t| t.w).collect();
2147    if terms.is_empty() || labelled.is_empty() {
2148        return Monitor::default();
2149    }
2150    let cap = (labelled.len() as f64).sqrt() * labelled.iter().sum::<f64>() / labelled.len() as f64;
2151    let mut norm = [0.0f64; ARMS];
2152    for t in &terms {
2153        norm[t.taken] += t.w.min(cap);
2154    }
2155    let count = terms.len() as f64;
2156    let norm = norm.map(|x| if x > 0.0 { x / count } else { 1.0 });
2157    // A route's residual only from `ROUTE_LABELS_MIN` labels with leverage on: below, its direct-method
2158    // term alone. A label whose difference is 0 adds nothing to the residual, only weight to the
2159    // route's normaliser, so it opens no route.
2160    let mut routed = [0usize; ARMS];
2161    for t in terms.iter().filter(|t| t.y.is_some() && t.diff[t.taken] != 0.0) {
2162        routed[t.taken] += 1;
2163    }
2164    let residual = routed.map(|n| n >= ROUTE_LABELS_MIN);
2165    let mut per: BTreeMap<u32, (f64, usize)> = BTreeMap::new();
2166    for t in &terms {
2167        let w = t.w.min(cap) / norm[t.taken];
2168        let r = |a: usize| if a == t.taken && residual[a] { t.y.map_or(0.0, |y| w * (y - t.p_hat[a])) } else { 0.0 };
2169        let dr: f64 = (0..ARMS).map(|a| t.diff[a] * (t.p_hat[a] + r(a))).sum();
2170        let e = per.entry(t.tick).or_insert((0.0, 0));
2171        e.0 += dr + ALPHA * t.vb;
2172        e.1 += 1;
2173    }
2174    let z: Vec<f64> = per.values().map(|(x, n)| x / *n as f64).collect();
2175    let mean = z.iter().sum::<f64>() / z.len() as f64;
2176    let ucb = Some(upper_bound(&z)).filter(|u| u.is_finite());
2177    // The weights' health, over the labelled applies' weights as the estimate used them (each > 0), of
2178    // the applies whose weighted residual enters it: a residual in use, times a difference that is not
2179    // 0 (a placebo neither rule delivers moves nothing, its weight however large).
2180    let moves = |t: &&Term| t.y.is_some() && residual[t.taken] && t.diff[t.taken] != 0.0;
2181    let used: Vec<f64> = terms.iter().filter(moves).map(|t| t.w.min(cap) / norm[t.taken]).collect();
2182    let (sum, sq) = used.iter().fold((0.0, 0.0), |(s, q), w| (s + w, q + w * w));
2183    let ess = if sq > 0.0 { sum * sum / sq } else { 0.0 };
2184    let max_weight = if sum > 0.0 { used.iter().copied().fold(0.0, f64::max) * used.len() as f64 / sum } else { 0.0 };
2185    Monitor {
2186        reverted: ucb.is_some_and(|u| u < 0.0),
2187        sessions: z.len() as u32,
2188        units: terms.len() as u32,
2189        mean,
2190        ucb,
2191        labelled: used.len() as u32,
2192        ess,
2193        max_weight,
2194        truncated: terms.iter().filter(moves).filter(|t| t.w > cap).count() as u32,
2195    }
2196}
2197
2198/// The time-uniform upper bound on the running mean of `x` at `DELTA`, intersected over time from
2199/// `CS_MIN` on: the normal-mixture boundary on the sum, √((v + ρ)(ln(1 + v/ρ) + 2 ln(1/δ))) with
2200/// v = t·σ̂² (Howard et al. 2021; asymptotically valid with the variance plugged in, Waudby-Smith et al.
2201/// 2024), ρ tightest at `CS_SESSIONS`; the two-sided boundary read on one side, so cautious. Valid at
2202/// every time at once, so reading it after every session costs no error. +∞ before `CS_MIN`. An
2203/// empirical-Bernstein sequence would be valid without asymptotics but needs a range bound, and the
2204/// truncated weights' is far wider than the increments' spread: it could never trip on a few dozen
2205/// sessions.
2206fn upper_bound(x: &[f64]) -> f64 {
2207    let log = (1.0 / DELTA).ln();
2208    let shape = 2.0 * log + (1.0 + 2.0 * log).ln();
2209    let (mut sum, mut sum_sq) = (0.0, 0.0);
2210    let mut best = f64::INFINITY;
2211    for (i, &xi) in x.iter().enumerate() {
2212        let t = (i + 1) as f64;
2213        sum += xi;
2214        sum_sq += xi * xi;
2215        if i + 1 < CS_MIN.max(2) {
2216            continue;
2217        }
2218        let mean = sum / t;
2219        let var = ((sum_sq - t * mean * mean) / (t - 1.0)).max(f64::MIN_POSITIVE);
2220        let (v, rho) = (t * var, var * CS_SESSIONS / shape);
2221        best = best.min(mean + ((v + rho) * ((1.0 + v / rho).ln() + 2.0 * log)).sqrt() / t);
2222    }
2223    best
2224}
2225
2226/// The seed of a session's pool draw (`pool_tokens`).
2227#[must_use]
2228pub fn session_seed(session: &str) -> u64 {
2229    hash(&[b"pool", session.as_bytes()])
2230}
2231
2232/// The pool budgets (`POOL_STEPS`) in tokens.
2233#[must_use]
2234pub fn pool_grid() -> Vec<usize> {
2235    POOL_STEPS.iter().map(|&i| (crate::whole::TOKENS as f64 * 2f64.powf(f64::from(i) / 2.0)).round() as usize).collect()
2236}
2237
2238/// The quality a pool of `tokens` buys per check under one draw (`seed`), its price aside: the
2239/// expected applies of its fill positions times what an apply is worth there (the grader's dilution
2240/// included).
2241fn pool_value(pool: &PoolFit, tokens: usize, seed: u64) -> f64 {
2242    let mut rng = Rng::new(seed);
2243    let shown = tokens as f64 / pool.per_lesson.max(1.0);
2244    let mut applies = 0.0;
2245    for (b, &(a, bb)) in pool.yields.iter().enumerate() {
2246        let (lo, hi) = ((1u64 << b) - 1, (1u64 << (b + 1)) - 1);
2247        let inside = (shown.min(hi as f64) - lo as f64).max(0.0);
2248        applies += inside * if inside > 0.0 { beta_draw(a, bb, &mut rng) } else { 0.0 };
2249    }
2250    let slope = (pool.slope.0 + pool.slope.1 * normal(&mut rng)).min(0.0);
2251    let not = pool.not.0 + pool.not.1 * normal(&mut rng);
2252    let eta = pool.eta + slope * (tokens as f64 / crate::whole::TOKENS as f64).ln();
2253    applies * (sigmoid(eta) - sigmoid(eta + not)).max(0.0)
2254}
2255
2256/// Whether a pool of `tokens` may be chosen at all: never above the baseline without a known latency
2257/// limit (`limit_ms`) its 95% latency fits in, latency observed at that budget (`LATENCY_NEAR` of it,
2258/// `PoolFit::latency_reach`: the fit read among its calls, never extrapolated) and a yield for every
2259/// position it adds; below it, a yield for every position it drops, or a price to pay.
2260fn pool_feasible(pool: &PoolFit, tokens: usize, limit_ms: Option<u64>, lambda: f64) -> bool {
2261    let baseline = crate::whole::TOKENS;
2262    let bins_to = |t: usize| -> usize {
2263        let shown = (t as f64 / pool.per_lesson.max(1.0)).ceil() as u64;
2264        (u64::BITS - shown.leading_zeros()) as usize
2265    };
2266    let known = pool.per_lesson > 0.0 && pool.yields.len() >= bins_to(tokens.max(baseline));
2267    if tokens > baseline {
2268        let (a, b, q) = pool.latency;
2269        let fits = limit_ms.is_some_and(|l| a + b * tokens as f64 + q <= l as f64);
2270        let observed = f64::from(pool.latency_reach) >= LATENCY_NEAR * tokens as f64;
2271        return known && fits && observed && pool.latency_n as usize >= LATENCY_MIN;
2272    }
2273    tokens == baseline || known || lambda > 0.0
2274}
2275
2276/// The pool's draws under a fit: per budget of `pool_grid`, its value under each of the `DRAWS`
2277/// shared draws (the gate's evidence) and under a session's own draw (its play).
2278struct PoolDraws {
2279    grid: Vec<usize>,
2280    shared: Vec<Vec<f64>>,
2281}
2282
2283impl PoolDraws {
2284    fn of(pool: &PoolFit) -> PoolDraws {
2285        let grid = pool_grid();
2286        let shared = grid.iter().map(|&t| (0..DRAWS).map(|k| pool_value(pool, t, mix(hash(&[b"pool-gate"]), k as u64))).collect()).collect();
2287        PoolDraws { grid, shared }
2288    }
2289
2290    /// The budget a session plays (`play`: its own draw's value per budget) at price `lambda`: of the
2291    /// feasible budgets, those whose gain over the baseline, price included, is ≥ 0 at the `DELTA`
2292    /// quantile of the shared draws (the baseline always; a larger budget only on a gain > 0 there,
2293    /// never on draws that tie with the baseline), the one its draw values most.
2294    fn choose(&self, play: &[f64], feasible: &[bool], lambda: f64) -> usize {
2295        let baseline = crate::whole::TOKENS;
2296        let Some(b) = self.grid.iter().position(|&t| t == baseline) else { return baseline };
2297        let admits = |i: usize| -> bool {
2298            let cost = lambda * (self.grid[i] as f64 - baseline as f64);
2299            let mut gain: Vec<f64> = self.shared[i].iter().zip(&self.shared[b]).map(|(x, y)| x - y - cost).collect();
2300            let q = lower_quantile(&mut gain);
2301            i == b || if self.grid[i] > baseline { q > 0.0 } else { q >= 0.0 }
2302        };
2303        (0..self.grid.len())
2304            .filter(|&i| feasible[i] && admits(i))
2305            .map(|i| (play[i] - lambda * self.grid[i] as f64, self.grid[i]))
2306            .fold(None, |best: Option<(f64, usize)>, x| if best.is_none_or(|b| x.0 > b.0) { Some(x) } else { best })
2307            .map_or(baseline, |(_, t)| t)
2308    }
2309
2310    /// A session's own draw of each budget's value (`seed`: `session_seed`).
2311    fn play(&self, pool: &PoolFit, seed: u64) -> Vec<f64> {
2312        self.grid.iter().map(|&t| pool_value(pool, t, seed)).collect()
2313    }
2314}
2315
2316/// The pool budget for a session (`seed`) under `fit`: the baseline (whole.rs `TOKENS`) unless the
2317/// learned policy is in force (`active`); else, of the feasible budgets of `pool_grid` whose gain over
2318/// the baseline, the budget's price included, is ≥ 0 at the `DELTA` quantile (> 0 for a larger one,
2319/// `PoolDraws::choose`; the gate's caution: an
2320/// unidentified dilution never moves the pool), the one the session's Thompson draw values most. One
2321/// draw per session keeps a session's pool, and its prompt prefix, stable.
2322#[must_use]
2323pub fn pool_tokens(fit: &Fit, active: bool, limit_ms: Option<u64>, seed: u64) -> usize {
2324    let baseline = crate::whole::TOKENS;
2325    if !active || fit.pool.yields.is_empty() && fit.lambda == 0.0 {
2326        return baseline;
2327    }
2328    let draws = PoolDraws::of(&fit.pool);
2329    let feasible: Vec<bool> = draws.grid.iter().map(|&t| pool_feasible(&fit.pool, t, limit_ms, fit.lambda)).collect();
2330    draws.choose(&draws.play(&fit.pool, seed), &feasible, fit.lambda)
2331}
2332
2333/// The budget's price per pool token: the smallest λ ≥ 0 under which the pools `pool_tokens` chooses
2334/// for the newest `FIT_SESSIONS` sessions of `s` (each with its own draw, every check at its
2335/// session's pool) spend at most `budget` tokens per session on average, by bisection (a higher price
2336/// never grows a pool); deterministic. The pools are replayed under the loosest latency limit, where
2337/// every budget the latency evidence covers may be chosen: a hook's limit (live.rs `call_limit`) only
2338/// takes the largest budgets out of the choice, and a choice among fewer, smaller budgets never takes
2339/// a larger pool, so what the hooks spend under any limit stays within the spend replayed here. A
2340/// budget no pool meets gets the price at which every session takes the smallest pool.
2341#[must_use]
2342pub fn price(s: &Summary, fit: &Fit, budget: f64) -> f64 {
2343    let sessions: Vec<(&str, u32)> = s.sessions.iter().rev().take(FIT_SESSIONS).filter(|x| x.checks > 0).map(|x| (x.id.as_str(), x.checks)).collect();
2344    if sessions.is_empty() || budget <= 0.0 {
2345        return 0.0;
2346    }
2347    let draws = PoolDraws::of(&fit.pool);
2348    let plays: Vec<Vec<f64>> = sessions.iter().map(|(id, _)| draws.play(&fit.pool, session_seed(id))).collect();
2349    let spend = |lambda: f64| -> f64 {
2350        // The loosest limit: none binds (u64::MAX ms).
2351        let feasible: Vec<bool> = draws.grid.iter().map(|&t| pool_feasible(&fit.pool, t, Some(u64::MAX), lambda)).collect();
2352        sessions.iter().zip(&plays).map(|((_, n), play)| f64::from(*n) * draws.choose(play, &feasible, lambda) as f64).sum::<f64>() / sessions.len() as f64
2353    };
2354    if spend(0.0) <= budget {
2355        return 0.0;
2356    }
2357    // The price at which even the most valuable apply is not worth one token more: every pool shrinks.
2358    let mut hi = 1.0 / crate::whole::TOKENS as f64;
2359    for _ in 0..PRICE_STEPS {
2360        if spend(hi) <= budget {
2361            break;
2362        }
2363        hi *= 2.0;
2364    }
2365    let mut lo = 0.0;
2366    for _ in 0..PRICE_STEPS {
2367        let mid = 0.5 * (lo + hi);
2368        if spend(mid) <= budget { hi = mid } else { lo = mid }
2369    }
2370    hi
2371}
2372
2373#[cfg(test)]
2374mod tests {
2375    use super::*;
2376    use crate::outcome::{self, Outcome};
2377    use crate::select::summary::{self, Summary};
2378
2379    fn unit(lesson: &str, grade: u8, rank: usize) -> Unit<'_> {
2380        Unit { lesson, grade, rank }
2381    }
2382
2383    /// Every grade sequence over {1, 2, 3} of up to `n` applies.
2384    fn tables(n: usize) -> Vec<Vec<u8>> {
2385        let mut out = vec![Vec::new()];
2386        for len in 1..=n {
2387            out.extend((0..3usize.pow(len as u32)).map(|mut x| {
2388                (0..len)
2389                    .map(|_| {
2390                        let g = (x % 3) as u8 + 1;
2391                        x /= 3;
2392                        g
2393                    })
2394                    .collect()
2395            }));
2396        }
2397        out
2398    }
2399
2400    /// A planted world: the judge's P(changed) for an apply on a route among m, and a lift of the stop's
2401    /// checks with a 3 on the log-odds scale, planted apart from the model's terms.
2402    #[derive(Clone, Copy)]
2403    struct Truth {
2404        b: [f64; P],
2405        lift: f64,
2406    }
2407
2408    impl Truth {
2409        fn p(&self, f: &Feat, route: Route, m: usize) -> f64 {
2410            sigmoid(dot(&self.b, &design(f, route, m)) + if f.stop && f.three { self.lift } else { 0.0 })
2411        }
2412    }
2413
2414    /// A simulated outcome log: per session, checks of random grades routed by `route` (given the
2415    /// grades, the routes and their logged p, pf), delivered, judged from `truth` (a placebo too), and
2416    /// settled at the session's end.
2417    struct Sim {
2418        rng: Rng,
2419        lines: Vec<Outcome>,
2420        ms: i64,
2421    }
2422
2423    type Router<'a> = dyn FnMut(&[u8], bool, &mut Rng) -> (Vec<Route>, Vec<f64>, Vec<f64>, &'static str) + 'a;
2424
2425    impl Sim {
2426        fn new(seed: u64) -> Sim {
2427            Sim { rng: Rng::new(seed), lines: Vec::new(), ms: 0 }
2428        }
2429
2430        fn session(&mut self, session: &str, checks: usize, lessons: usize, truth: &Truth, route: &mut Router) {
2431            for c in 0..checks {
2432                let n = 1 + (self.rng.next_u64() % 4) as usize;
2433                let stop = c % 4 == 3;
2434                let grades: Vec<u8> = (0..n).map(|_| 1 + (self.rng.next_u64() % 3) as u8).collect();
2435                let ids: Vec<String> = (0..n).map(|_| format!("L{}", self.rng.next_u64() % lessons as u64)).collect();
2436                self.check(session, stop, &grades, &ids, truth, route);
2437            }
2438            self.ms += 1;
2439            let asked: Vec<String> =
2440                self.lines.iter().filter(|o| o.session == session && o.kind == outcome::DELIVERY).flat_map(|o| o.lessons.clone()).collect();
2441            if let Some(l) = asked.first() {
2442                self.lines.push(Outcome {
2443                    kind: outcome::NOT_CORRECTED.into(),
2444                    session: session.into(),
2445                    lessons: vec![l.clone()],
2446                    ms: self.ms,
2447                    reason: outcome::UNJUDGED_NONE.into(),
2448                    ..Default::default()
2449                });
2450            }
2451        }
2452
2453        fn check(&mut self, session: &str, stop: bool, grades: &[u8], ids: &[String], truth: &Truth, route: &mut Router) {
2454            self.ms += 1;
2455            let check = crate::select::check_id(session, if stop { "stop" } else { "live" }, self.ms, 1);
2456            let (routes, p, pf, policy) = route(grades, stop, &mut self.rng);
2457            let pick = |r: Route| ids.iter().zip(&routes).filter(|(_, x)| **x == r).map(|(l, _)| l.clone()).collect::<Vec<_>>();
2458            self.lines.push(Outcome {
2459                kind: outcome::GRADED.into(),
2460                session: session.into(),
2461                lessons: ids.to_vec(),
2462                ms: self.ms,
2463                channel: if stop { "stop" } else { "live" }.into(),
2464                grades: grades.to_vec(),
2465                ranks: (0..ids.len() as u16).collect(),
2466                applied: ids.len() as u16,
2467                chosen: pick(Route::Ask),
2468                free: pick(Route::Free),
2469                placebo: pick(Route::Placebo),
2470                psi: 1.0,
2471                p,
2472                pf,
2473                check: check.clone(),
2474                policy: policy.into(),
2475                ..Default::default()
2476            });
2477            let three = grades.contains(&3);
2478            let m = |r: Route| routes.iter().filter(|x| **x == r).count();
2479            for (j, (l, r)) in ids.iter().zip(&routes).enumerate() {
2480                if *r == Route::Not {
2481                    continue;
2482                }
2483                self.ms += 1;
2484                let delivered = match r {
2485                    Route::Ask => outcome::ROUTE_ASKED,
2486                    Route::Free => outcome::ROUTE_FREE,
2487                    _ => outcome::ROUTE_PLACEBO,
2488                };
2489                self.lines.push(Outcome {
2490                    kind: outcome::DELIVERY.into(),
2491                    session: session.into(),
2492                    lessons: vec![l.clone()],
2493                    ms: self.ms,
2494                    check: check.clone(),
2495                    route: delivered.into(),
2496                    ..Default::default()
2497                });
2498                let f = Feat { grade: grades[j], rank: j, stop, three, pool: 0.0 };
2499                let as_route = if *r == Route::Placebo { Route::Not } else { *r };
2500                let y = self.rng.unit() < truth.p(&f, as_route, m(*r).max(1));
2501                let channel = match r {
2502                    Route::Ask => {
2503                        if stop {
2504                            "stop"
2505                        } else {
2506                            "live"
2507                        }
2508                    }
2509                    Route::Free => "queued",
2510                    _ => outcome::PLACEBO,
2511                };
2512                self.lines.push(Outcome {
2513                    kind: if y { outcome::CHANGED } else { outcome::IGNORED }.into(),
2514                    session: session.into(),
2515                    lessons: vec![l.clone()],
2516                    ms: self.ms,
2517                    channel: channel.into(),
2518                    check: check.clone(),
2519                    judge: 2,
2520                    ..Default::default()
2521                });
2522            }
2523        }
2524
2525        fn text(&self) -> String {
2526            self.lines.iter().map(|o| format!("{}\n", serde_json::to_string(o).unwrap())).collect()
2527        }
2528
2529        fn summary(&self) -> Summary {
2530            summary::fold(&self.text(), Summary::default())
2531        }
2532    }
2533
2534    /// Each apply's route drawn uniformly over ask, free and placebo, the stop's 3s asked.
2535    fn uniform(grades: &[u8], stop: bool, rng: &mut Rng) -> (Vec<Route>, Vec<f64>, Vec<f64>, &'static str) {
2536        let routes: Vec<Route> = grades
2537            .iter()
2538            .map(|g| if stop && *g == 3 { Route::Ask } else { [Route::Ask, Route::Free, Route::Placebo][(rng.next_u64() % 3) as usize] })
2539            .collect();
2540        let p = grades.iter().map(|g| if stop && *g == 3 { 1.0 } else { 0.3333 }).collect();
2541        let pf = grades.iter().map(|g| if stop && *g == 3 { 0.0 } else { 0.3333 }).collect();
2542        (routes, p, pf, "fixed")
2543    }
2544
2545    /// The fixed rule's routes and propensities.
2546    fn fixed_rule(grades: &[u8], _: bool, _: &mut Rng) -> (Vec<Route>, Vec<f64>, Vec<f64>, &'static str) {
2547        let ids: Vec<String> = (0..grades.len()).map(|i| format!("x{i}")).collect();
2548        let units: Vec<Unit> = ids.iter().zip(grades).enumerate().map(|(r, (l, g))| unit(l, *g, r)).collect();
2549        let routes = fixed_routes(&units, &[]);
2550        let p = routes.iter().map(|r| f64::from(u8::from(*r == Route::Ask))).collect();
2551        let pf = routes.iter().map(|r| f64::from(u8::from(*r == Route::Free))).collect();
2552        (routes, p, pf, "fixed")
2553    }
2554
2555    fn truth() -> Truth {
2556        let mut b = [0.0; P];
2557        b[B0] = -1.0;
2558        b[G3] = 1.2;
2559        b[ASK] = 0.6;
2560        b[NOT] = -0.8;
2561        Truth { b, lift: 0.0 }
2562    }
2563
2564    fn planted(sessions: usize, checks: usize, seed: u64) -> Summary {
2565        let mut sim = Sim::new(seed);
2566        for i in 0..sessions {
2567            sim.session(&format!("s{i}"), checks, 30, &truth(), &mut uniform);
2568        }
2569        sim.summary()
2570    }
2571
2572    #[test]
2573    fn the_fit_recovers_a_planted_world() {
2574        let s = planted(24, 40, 1);
2575        let fit = fit(&s, None);
2576        let m = fit.model.as_ref().expect("a model");
2577        assert!(m.converged, "{m:?}");
2578        assert!(fit.labels > 600, "{}", fit.labels);
2579        let cov = |k: usize| (0..=k).map(|j| m.chol[k * P + j].powi(2)).sum::<f64>().sqrt();
2580        for k in [B0, G3, ASK, NOT] {
2581            let (got, want, sd) = (m.beta[k], truth().b[k], cov(k));
2582            assert!((got - want).abs() < 3.0 * sd + 0.15, "{}: {got:.3} vs {want} (sd {sd:.3})", NAMES[k]);
2583        }
2584        assert!(m.beta[ASK] > 0.0 && m.beta[NOT] < 0.0, "{:?}", m.beta);
2585        // Rank has no effect in this world: relevance shrinks its slope's prior.
2586        assert!(m.sd[RANK] < ARD_START, "{}", m.sd[RANK]);
2587        assert!(m.lessons.len() <= 30 && m.sessions.len() == 24);
2588        assert!(m.beta.iter().chain(&m.chol).all(|x| x.is_finite()));
2589    }
2590
2591    /// The fixed rule's history (every 3 asked with its block, every check without one handed over)
2592    /// in a world without dilution: the labels say nothing about it, the dilution slopes keep their
2593    /// dated prior's sd, while EM still moves the terms without one.
2594    #[test]
2595    fn a_dated_prior_keeps_its_sd_where_the_labels_say_nothing() {
2596        let mut sim = Sim::new(12);
2597        for i in 0..16 {
2598            sim.session(&format!("s{i}"), 16, 12, &truth(), &mut fixed_rule);
2599        }
2600        let f = fit(&sim.summary(), None);
2601        let m = f.model.as_ref().expect("a model");
2602        for k in [ASK_M, FREE_M] {
2603            assert_eq!(m.sd[k], DILUTION_PRIOR.1, "{}: {:?}", NAMES[k], m.sd);
2604        }
2605        assert_eq!((m.sd[ASK], m.sd[NOT]), (ASK_PRIOR.1, NOT_PRIOR.1));
2606        assert!([RANK, ASK_STOP, ASK_G3, NOT_G, POOL].iter().any(|&k| m.sd[k] != ARD_START), "EM ran on the rest: {:?}", m.sd);
2607    }
2608
2609    /// The pruned model's n = 0 prior, what P2's rerun predicts a unit with no label before its check by:
2610    /// the kept terms at their prior, every other at 0 with the floor's sd, and no level's spread (an
2611    /// unseen session or lesson adds no variance), unlike the full model's prior.
2612    #[test]
2613    fn the_pruned_prior_is_the_pruned_models_before_any_label() {
2614        let (full, pruned) = (prior(), prior_pruned());
2615        let (fm, pm) = (full.model.as_ref().expect("a prior"), pruned.model.as_ref().expect("a pruned prior"));
2616        let spec = coefs();
2617        for (k, name) in NAMES.iter().enumerate() {
2618            let want = if PRUNED_KEEPS.contains(&k) { (spec[k].mean, spec[k].sd) } else { (0.0, SD_FLOOR) };
2619            assert!((pm.beta[k] - want.0).abs() < 1e-9 && pm.sd[k] == want.1, "{name}: {} sd {}", pm.beta[k], pm.sd[k]);
2620        }
2621        assert_eq!((pm.spread, fm.spread), ([0.0; FAMILIES], [LEVEL_START; FAMILIES]));
2622        let f = Feat { grade: 2, rank: 3, stop: true, three: false, pool: 0.5 };
2623        // Not now, whose prior mean is off 0 (`NOT_PRIOR`): at a mean of 0 every variance predicts ½.
2624        let at = |fit: &Fit| predict(fit, &f, Route::Not, 1, "unseen", "L99").expect("a prediction");
2625        assert_ne!(at(&pruned), at(&full), "the full prior's rank, pool and level variance are not the pruned model's");
2626        assert_eq!(pruned.rates, full.rates);
2627    }
2628
2629    /// P2's pruned model: grade, route and dilution learned, every other term held at 0 with the
2630    /// floor's sd, no session, lesson or judge effect (a prediction is the same for any of them), the
2631    /// half-life chosen as the full fit's is. The full fit of the same world learns the rank slope and
2632    /// the stop's effect the prune leaves out.
2633    #[test]
2634    fn the_pruned_model_keeps_grade_route_and_dilution_alone() {
2635        let mut t = truth();
2636        t.b[RANK] = -0.6;
2637        t.b[STOP] = 0.8;
2638        let mut sim = Sim::new(4);
2639        for i in 0..20 {
2640            sim.session(&format!("s{i}"), 30, 20, &t, &mut uniform);
2641        }
2642        let s = sim.summary();
2643        let (full, pruned) = (fit(&s, None), fit_pruned(&s, None));
2644        let (fm, pm) = (full.model.as_ref().expect("a full model"), pruned.model.as_ref().expect("a pruned model"));
2645        assert!(fm.beta[RANK] < -0.2 && fm.beta[STOP] > 0.2 && !fm.sessions.is_empty(), "the scenario: {:?}", fm.beta);
2646        assert!(pm.current() && pm.converged && pruned.labels == full.labels && pruned.key == full.key);
2647        for (k, name) in NAMES.iter().enumerate() {
2648            if PRUNED_KEEPS.contains(&k) {
2649                assert!(pm.sd[k] > SD_FLOOR, "{name} is kept");
2650            } else {
2651                assert_eq!(pm.sd[k], SD_FLOOR, "{name}");
2652                assert!(pm.beta[k].abs() < 3.0 * SD_FLOOR, "{name}: {}", pm.beta[k]);
2653            }
2654        }
2655        assert!(pm.beta[ASK] > 0.0 && pm.beta[NOT] < 0.0, "{:?}", pm.beta);
2656        assert!(pm.sessions.is_empty() && pm.lessons.is_empty() && pm.judges.is_empty() && pm.judge.is_empty());
2657        assert_eq!(pm.spread, [0.0; FAMILIES]);
2658        assert!(HALF_LIVES.contains(&pruned.half_life));
2659        let f = Feat { grade: 2, rank: 3, stop: true, three: false, pool: 0.0 };
2660        let at = |session: &str, lesson: &str| predict(&pruned, &f, Route::Free, 2, session, lesson);
2661        assert_eq!(at("s0", "L1"), at("unseen", "L99"), "no level effect");
2662        assert_ne!(predict(&full, &f, Route::Free, 2, "s0", "L1"), predict(&full, &f, Route::Free, 2, "unseen", "L99"), "the full model's levels");
2663    }
2664
2665    /// The same model two ways: four session effects as a random-effect family (eliminated by its
2666    /// Schur complement), and as four fixed coefficients with the family's prior. Their MAP and
2667    /// covariance agree.
2668    #[test]
2669    fn eliminating_a_family_equals_solving_it_densely() {
2670        let mut rng = Rng::new(9);
2671        let rows: Vec<([f64; 3], usize, f64)> = (0..400)
2672            .map(|i| {
2673                let x = [1.0, rng.unit() * 2.0 - 1.0, f64::from(u8::from(rng.unit() < 0.4))];
2674                let l = i % 4;
2675                let eta = -0.3 + 0.8 * x[1] - 0.5 * x[2] + [0.6, -0.4, 0.1, 0.0][l];
2676                (x, l, f64::from(u8::from(rng.unit() < sigmoid(eta))))
2677            })
2678            .collect();
2679        let spread = 0.7;
2680        let spec3 = [Coef { mean: 0.0, sd: 2.5, ard: false, nonpos: false }; 3];
2681        let obs: Vec<Obs<3, 1>> = rows.iter().map(|(x, l, y)| Obs { x: *x, y: *y, w: 1.0 + f64::from(*l as u8) * 0.1, lv: [Some(*l)] }).collect();
2682        let a = map(&obs, [4], &spec3, &[2.5; 3], &[spread], None).unwrap();
2683        let mut spec7 = [Coef { mean: 0.0, sd: spread, ard: false, nonpos: false }; 7];
2684        spec7[..3].copy_from_slice(&spec3);
2685        let sd7: [f64; 7] = std::array::from_fn(|k| spec7[k].sd);
2686        let dense: Vec<Obs<7, 0>> = rows
2687            .iter()
2688            .map(|(x, l, y)| {
2689                let mut z = [0.0; 7];
2690                z[..3].copy_from_slice(x);
2691                z[3 + l] = 1.0;
2692                Obs { x: z, y: *y, w: 1.0 + f64::from(*l as u8) * 0.1, lv: [] }
2693            })
2694            .collect();
2695        let b = map(&dense, [], &spec7, &sd7, &[], None).unwrap();
2696        assert!(a.converged && b.converged);
2697        for k in 0..3 {
2698            assert!((a.beta[k] - b.beta[k]).abs() < 1e-6, "{k}: {} {}", a.beta[k], b.beta[k]);
2699            for j in 0..3 {
2700                assert!((a.cov[k][j] - b.cov[k][j]).abs() < 1e-6, "cov {k} {j}");
2701            }
2702        }
2703        for l in 0..4 {
2704            assert!((a.u[0][l] - b.beta[3 + l]).abs() < 1e-6, "level {l}");
2705            assert!((a.var_u[0][l] - b.cov[3 + l][3 + l]).abs() < 1e-6, "var {l}: {} {}", a.var_u[0][l], b.cov[3 + l][3 + l]);
2706        }
2707    }
2708
2709    #[test]
2710    fn a_bounded_slope_stops_at_zero_and_a_negative_one_is_learned() {
2711        let mut rng = Rng::new(3);
2712        let obs: Vec<Obs<3, 0>> = (0..600)
2713            .map(|_| {
2714                let x = [1.0, rng.unit() * 3.0, rng.unit() * 3.0];
2715                let eta = -0.5 + 0.9 * x[1] - 0.7 * x[2];
2716                Obs { x, y: f64::from(u8::from(rng.unit() < sigmoid(eta))), w: 1.0, lv: [] }
2717            })
2718            .collect();
2719        let bounded = Coef { mean: 0.0, sd: 2.5, ard: false, nonpos: true };
2720        let spec = [Coef { nonpos: false, ..bounded }, bounded, bounded];
2721        let e = map(&obs, [], &spec, &[2.5; 3], &[], None).unwrap();
2722        assert_eq!(e.beta[1], 0.0, "a positive effect under a ≤ 0 bound sits at the bound");
2723        assert!(e.beta[2] < -0.3, "{:?}", e.beta);
2724    }
2725
2726    /// Every feasible routing's value, the best; a unit marked in `nofree` never free.
2727    fn brute(v: &Valuer, nofree: &[bool], rail: usize, cap: usize) -> f64 {
2728        let n = v.n();
2729        let mut best = f64::NEG_INFINITY;
2730        for code in 0..3usize.pow(n as u32) {
2731            let mut x = code;
2732            let routes: Vec<Route> = (0..n)
2733                .map(|_| {
2734                    let r = [Route::Ask, Route::Free, Route::Not][x % 3];
2735                    x /= 3;
2736                    r
2737                })
2738                .collect();
2739            let (m_q, m_f) = (routes.iter().filter(|r| **r == Route::Ask).count(), routes.iter().filter(|r| **r == Route::Free).count());
2740            let forced_ok = v.feats.iter().zip(&routes).all(|(f, r)| !forced(f) || *r == Route::Ask);
2741            let free_ok = nofree.iter().zip(&routes).all(|(x, r)| !x || *r != Route::Free);
2742            let f = v.feats.iter().filter(|f| forced(f)).count();
2743            if forced_ok && free_ok && m_q <= rail.max(f) && m_f <= cap {
2744                best = best.max(v.value(&routes));
2745            }
2746        }
2747        best
2748    }
2749
2750    #[test]
2751    fn the_solver_equals_brute_force() {
2752        let mut rng = Rng::new(11);
2753        for case in 0..400 {
2754            let n = case % 8;
2755            let stop = case % 3 == 0;
2756            let feats: Vec<Feat> = (0..n).map(|r| Feat { grade: 1 + (rng.next_u64() % 3) as u8, rank: r, stop, three: false, pool: 0.0 }).collect();
2757            let mut b = [0.0; P];
2758            for x in &mut b {
2759                *x = 2.0 * normal(&mut rng);
2760            }
2761            b[ASK_M] = -rng.unit() * 2.0;
2762            b[FREE_M] = -rng.unit() * 2.0;
2763            let t = Theta { b, base: (0..n).map(|_| normal(&mut rng)).collect() };
2764            let rates = Rates { free_stop: rng.unit(), free_live: rng.unit(), later: rng.unit(), wait: 0.0, ask_live: 1.0 };
2765            let v = Valuer { t: &t, feats: &feats, rates: &rates };
2766            // Every other case: units that may not go free (lessons the session had), forced ones too.
2767            let nofree: Vec<bool> = (0..n).map(|_| case % 2 == 1 && rng.unit() < 0.4).collect();
2768            let table = Table::of(&v, &nofree);
2769            for (rail, cap) in [(20, 10), (2, 1), (1, 3), (0, 0)] {
2770                let got = solve(&table, rail, cap);
2771                let want = brute(&v, &nofree, rail, cap);
2772                assert!((v.value(&got) - want).abs() < 1e-12, "case {case} rail {rail} cap {cap}: {} vs {want} {got:?}", v.value(&got));
2773                assert!((table.value(&got) - v.value(&got)).abs() < 1e-12, "the table values as the model does");
2774                assert!(feats.iter().zip(&got).all(|(f, r)| !forced(f) || *r == Route::Ask));
2775                assert!(nofree.iter().zip(&got).all(|(x, r)| !x || *r != Route::Free), "case {case}: {nofree:?} {got:?}");
2776            }
2777        }
2778    }
2779
2780    #[test]
2781    fn the_value_of_a_route_is_the_models_lift() {
2782        let f = Feat { grade: 3, rank: 2, stop: false, three: true, pool: 0.3 };
2783        let mut b = [0.0; P];
2784        for (i, x) in b.iter_mut().enumerate() {
2785            *x = 0.1 * i as f64 - 0.6;
2786        }
2787        let base = dot(&b, &design(&f, Route::Free, 1)) + 0.25;
2788        let t = Theta { b, base: vec![base] };
2789        let rates = Rates { free_stop: 0.5, free_live: 0.8, later: 0.4, wait: 0.0, ask_live: 0.6 };
2790        let v = Valuer { t: &t, feats: std::slice::from_ref(&f), rates: &rates };
2791        let p = |r: Route, m: usize| sigmoid(dot(&b, &design(&f, r, m)) + 0.25);
2792        // During the turn a question arrives at the learned rate a live one reached the drone.
2793        assert!((v.ask(0, 3) - 0.6 * (p(Route::Ask, 3) - p(Route::Not, 1))).abs() < 1e-12);
2794        assert!((v.free(0, 2) - 0.8 * (p(Route::Free, 2) - p(Route::Not, 1))).abs() < 1e-12);
2795        // Not now counts nothing during the turn, whatever the later rate: the check that delivers it
2796        // later credits its value, and its ledger entry with it.
2797        for r in [Route::Not, Route::Placebo] {
2798            assert_eq!(v.value(&[r]), 0.0, "during the turn a lesson left for later counts nothing here: {r:?}");
2799        }
2800        let stop = Feat { stop: true, ..f };
2801        let t = Theta { b, base: vec![dot(&b, &design(&stop, Route::Free, 1)) + 0.25] };
2802        let v = Valuer { t: &t, feats: std::slice::from_ref(&stop), rates: &rates };
2803        let p = |r: Route, m: usize| sigmoid(dot(&b, &design(&stop, r, m)) + 0.25);
2804        assert!((v.ask(0, 3) - (p(Route::Ask, 3) - p(Route::Not, 1))).abs() < 1e-12, "a stop's question blocks: it arrives");
2805        assert!((v.free(0, 2) - 0.5 * (p(Route::Free, 2) - p(Route::Not, 1))).abs() < 1e-12);
2806        for r in [Route::Not, Route::Placebo] {
2807            assert_eq!(v.value(&[r]), 0.0, "at the stop a lesson not delivered counts nothing: {r:?}");
2808        }
2809    }
2810
2811    #[test]
2812    fn a_live_question_without_a_3_arrives_as_a_hand_over_does() {
2813        let mut b = [0.0; P];
2814        for (i, x) in b.iter_mut().enumerate() {
2815            *x = 0.1 * i as f64 - 0.6;
2816        }
2817        let t = Theta { b, base: vec![0.25] };
2818        let rates = Rates { free_stop: 0.5, free_live: 0.3, later: 0.4, wait: 0.0, ask_live: 0.9 };
2819        let ask = |three: bool| {
2820            let f = Feat { grade: 2, rank: 0, stop: false, three, pool: 0.0 };
2821            Valuer { t: &t, feats: std::slice::from_ref(&f), rates: &rates }.ask(0, 1)
2822        };
2823        // The lift of asking a 2, which the check's 3 does not change.
2824        let lift = sigmoid(0.25 + b[ASK]) - sigmoid(0.25 + b[NOT]);
2825        assert!(lift.abs() > 0.01);
2826        // On a check with a 3 the question waits for the stop, which asks it.
2827        assert!((ask(true) - 0.9 * lift).abs() < 1e-12);
2828        // Without one no stop asks it (hook.rs `live_check`): a tool call or the next prompt brings it,
2829        // as it brings a hand-over.
2830        assert!((ask(false) - 0.3 * lift).abs() < 1e-12);
2831    }
2832
2833    /// A gate that may risk plenty: the learned policy decides.
2834    fn open_gate() -> Gate {
2835        Gate { gain: vec![0.0; DRAWS], allowance: 5.0, ..Gate::default() }
2836    }
2837
2838    #[test]
2839    fn cold_start_is_the_fixed_rule() {
2840        let fit_model = fit(&planted(6, 20, 2), None);
2841        assert!(fit_model.model.is_some());
2842        let wrong_len = Gate { gain: vec![0.0; 3], allowance: 1.0, ..Gate::default() };
2843        let ids: Vec<String> = (0..14).map(|i| format!("L{i}")).collect();
2844        let mut grades = tables(5);
2845        grades.extend([vec![3; 12], [vec![1; 11], vec![2; 3]].concat()]);
2846        for g in &grades {
2847            let units: Vec<Unit> = ids.iter().zip(g).enumerate().map(|(r, (l, g))| unit(l, *g, r)).collect();
2848            let want = fixed_routes(&units, &[]);
2849            for (fit, gate) in [(&Fit::default(), &open_gate()), (&fit_model, &Gate::default()), (&fit_model, &wrong_len)] {
2850                for stop in [true, false] {
2851                    let d = choose(&units, &Check { session: "s", stop, tokens: 0 }, fit, gate, ALPHA, 7, &[]);
2852                    assert_eq!(d.routes, want, "{g:?}");
2853                    assert_eq!(d.p, want.iter().map(|r| f64::from(u8::from(*r == Route::Ask))).collect::<Vec<_>>());
2854                    assert_eq!(d.pf, want.iter().map(|r| f64::from(u8::from(*r == Route::Free))).collect::<Vec<_>>());
2855                    assert_eq!((d.source, d.gate), ("fixed", 0.0));
2856                }
2857            }
2858        }
2859    }
2860
2861    #[test]
2862    fn n_zero_and_n_one() {
2863        let empty = fit(&Summary::default(), None);
2864        assert!(empty.model.is_none() && empty.labels == 0);
2865        let mut sim = Sim::new(5);
2866        let mut one = |_: &[u8], _: bool, _: &mut Rng| (vec![Route::Ask], vec![1.0], vec![0.0], "fixed");
2867        sim.check("s", false, &[2], &["A".to_string()], &truth(), &mut one);
2868        let f = fit(&sim.summary(), None);
2869        let m = f.model.as_ref().expect("one label is a model");
2870        assert_eq!(f.labels, 1);
2871        assert!(m.beta.iter().chain(&m.chol).all(|x| x.is_finite()), "{m:?}");
2872        let gate = open_gate();
2873        assert!(choose(&[], &Check { session: "s", stop: true, tokens: 0 }, &f, &gate, ALPHA, 1, &[]).routes.is_empty());
2874        for g in 1..=3 {
2875            let d = choose(&[unit("A", g, 0)], &Check { session: "s", stop: true, tokens: 0 }, &f, &gate, ALPHA, 1, &[]);
2876            assert_eq!(d.routes.len(), 1);
2877            assert!((d.p[0] + d.pf[0]) <= 1.0 + 1e-9);
2878        }
2879    }
2880
2881    /// A decision is a function of its inputs; the seed moves only the randomised test's count, so at
2882    /// the stop, where the test never runs, no seed changes it.
2883    #[test]
2884    fn decisions_are_deterministic_and_only_the_test_varies() {
2885        let f = fit(&planted(8, 20, 3), None);
2886        let gate = open_gate();
2887        let units = [unit("L1", 2, 0), unit("L2", 1, 1), unit("L3", 3, 2)];
2888        let check = Check { session: "new", stop: false, tokens: 0 };
2889        let a = choose(&units, &check, &f, &gate, ALPHA, 42, &[]);
2890        assert_eq!(a, choose(&units, &check, &f, &gate, ALPHA, 42, &[]));
2891        let plays: std::collections::HashSet<Vec<Route>> = (0..60).map(|s| choose(&units, &check, &f, &gate, ALPHA, s, &[]).routes).collect();
2892        assert!(plays.len() > 1, "the test's count varies with the check: {plays:?}");
2893        let stop = Check { stop: true, ..check };
2894        let at_stop: std::collections::HashSet<Vec<Route>> = (0..60).map(|s| choose(&units, &stop, &f, &gate, ALPHA, s, &[]).routes).collect();
2895        assert_eq!(at_stop.len(), 1, "no Thompson play: {at_stop:?}");
2896    }
2897
2898    #[test]
2899    fn propensities_match_the_frequency_of_the_play() {
2900        let f = fit(&planted(6, 15, 4), None);
2901        let gate = open_gate();
2902        let units = [unit("L1", 2, 0), unit("L2", 1, 1), unit("L3", 1, 2)];
2903        let check = Check { session: "new", stop: false, tokens: 0 };
2904        let n = 300;
2905        let (mut asked, mut free) = ([0.0; 3], [0.0; 3]);
2906        let (mut p, mut pf) = ([0.0; 3], [0.0; 3]);
2907        for seed in 0..n {
2908            let d = choose(&units, &check, &f, &gate, ALPHA, seed, &[]);
2909            for j in 0..3 {
2910                asked[j] += f64::from(u8::from(d.routes[j] == Route::Ask)) / n as f64;
2911                free[j] += f64::from(u8::from(d.routes[j] == Route::Free)) / n as f64;
2912                p[j] += d.p[j] / n as f64;
2913                pf[j] += d.pf[j] / n as f64;
2914            }
2915        }
2916        for j in 0..3 {
2917            for (freq, prop) in [(asked[j], p[j]), (free[j], pf[j])] {
2918                let se = (prop * (1.0 - prop) / n as f64).sqrt();
2919                assert!((freq - prop).abs() <= 4.0 * se + 0.01, "unit {j}: played {freq:.3}, propensity {prop:.3}");
2920            }
2921        }
2922        assert!(asked.iter().chain(&free).any(|x| *x > 0.05 && *x < 0.95), "the play varies: {asked:?} {free:?}");
2923    }
2924
2925    /// A model that thinks handing over beats asking by a wide, uncertain margin.
2926    fn free_beats_ask() -> Fit {
2927        let mut sim = Sim::new(8);
2928        let mut t = truth();
2929        t.b[ASK] = -3.0;
2930        for i in 0..10 {
2931            sim.session(&format!("s{i}"), 12, 10, &t, &mut uniform);
2932        }
2933        fit(&sim.summary(), None)
2934    }
2935
2936    /// Guard: a stop's grade 3 is asked whatever the model says. The same 3 during the turn goes free,
2937    /// so the guard, not the model, keeps it asked.
2938    #[test]
2939    fn a_stop_grade_three_is_never_routed_away_from_asking() {
2940        let f = free_beats_ask();
2941        let gate = open_gate();
2942        let units = [unit("L1", 3, 0), unit("L2", 1, 1)];
2943        let live = choose(&units, &Check { session: "n", stop: false, tokens: 0 }, &f, &gate, ALPHA, 1, &[]);
2944        assert_eq!(live.routes[0], Route::Free, "during the turn the model hands it over: {live:?}");
2945        for seed in 0..20 {
2946            let d = choose(&units, &Check { session: "n", stop: true, tokens: 0 }, &f, &gate, ALPHA, seed, &[]);
2947            assert_eq!((d.routes[0], d.p[0], d.pf[0]), (Route::Ask, 1.0, 0.0), "{d:?}");
2948        }
2949    }
2950
2951    /// A lesson the session already had is never handed over free, at cold start (the fixed rule
2952    /// `graded_without`) or by the learned play, its `pf` 0, though the same model hands it over
2953    /// without `had`. Asking it stays open.
2954    #[test]
2955    fn a_lesson_the_session_had_never_goes_free() {
2956        let f = free_beats_ask();
2957        let gate = open_gate();
2958        let units = [unit("L1", 2, 0), unit("L2", 1, 1), unit("L3", 1, 2)];
2959        let had = ["L2".to_string()];
2960        let check = Check { session: "n", stop: false, tokens: 0 };
2961        assert_eq!(fixed_routes(&units, &had), [Route::Free, Route::Not, Route::Free]);
2962        let cold = choose(&units, &check, &Fit::default(), &gate, ALPHA, 1, &had);
2963        assert_eq!((cold.routes, cold.pf[1], cold.source), (fixed_routes(&units, &had), 0.0, "fixed"));
2964        let mut freed = 0;
2965        for seed in 0..20 {
2966            freed += usize::from(choose(&units, &check, &f, &gate, ALPHA, seed, &[]).routes[1] == Route::Free);
2967            let d = choose(&units, &check, &f, &gate, ALPHA, seed, &had);
2968            assert!(d.routes[1] != Route::Free && d.pf[1] == 0.0, "{d:?}");
2969        }
2970        assert!(freed > 0, "the scenario: without `had` the model hands L2 over");
2971        // A stop's 3 the session had is still asked.
2972        let block = [unit("L2", 3, 0), unit("L1", 1, 1)];
2973        let d = choose(&block, &Check { stop: true, ..check }, &f, &gate, ALPHA, 3, &had);
2974        assert_eq!((d.routes[0], d.p[0]), (Route::Ask, 1.0));
2975    }
2976
2977    /// Guard: a check with more applies than `SOLVE_MAX` takes the fixed rule (source `size`). The same
2978    /// model and gate deviate at `SOLVE_MAX` applies, so the guard, not the model, keeps the fixed rule.
2979    #[test]
2980    fn a_check_past_the_solvers_size_takes_the_fixed_rule() {
2981        let f = free_beats_ask();
2982        let gate = open_gate();
2983        let ids: Vec<String> = (0..=SOLVE_MAX).map(|i| format!("L{i}")).collect();
2984        // A 3 and 2s: the fixed rule asks them all.
2985        let units: Vec<Unit> = ids.iter().enumerate().map(|(r, l)| unit(l, if r == 0 { 3 } else { 2 }, r)).collect();
2986        let check = Check { session: "n", stop: false, tokens: 0 };
2987        let at = choose(&units[..SOLVE_MAX], &check, &f, &gate, ALPHA, 1, &[]);
2988        assert_eq!(at.source, EVIDENCE, "{at:?}");
2989        assert_ne!(at.routes, fixed_routes(&units[..SOLVE_MAX], &[]), "at the size the model hands them over: {at:?}");
2990        let past = choose(&units, &check, &f, &gate, ALPHA, 1, &[]);
2991        let base = fixed_routes(&units, &[]);
2992        assert_eq!((&past.routes, past.source, past.risk), (&base, "size", 0.0));
2993        assert_eq!(past.p, base.iter().map(|r| f64::from(u8::from(*r == Route::Ask))).collect::<Vec<_>>(), "its choice is its propensity");
2994    }
2995
2996    /// A history where every 3 was asked (the fixed rule): the model never saw a 3 handed over, and a
2997    /// stop's 3 stays asked.
2998    #[test]
2999    fn a_confounded_history_keeps_the_stop_three_asked() {
3000        let mut sim = Sim::new(12);
3001        for i in 0..12 {
3002            sim.session(&format!("s{i}"), 16, 12, &truth(), &mut fixed_rule);
3003        }
3004        let s = sim.summary();
3005        assert!(s.applies.iter().all(|a| a.grade != 3 || a.route == Route::Ask || a.route == Route::Not), "never free");
3006        let f = fit(&s, None);
3007        let gate = open_gate();
3008        for seed in 0..10 {
3009            let d = choose(&[unit("L1", 3, 0), unit("L2", 2, 1)], &Check { session: "n", stop: true, tokens: 0 }, &f, &gate, ALPHA, seed, &[]);
3010            assert_eq!((d.routes[0], d.p[0]), (Route::Ask, 1.0));
3011        }
3012    }
3013
3014    /// The fixed rule's history logged as the learned policy's cold start (its gate opens on it), in a
3015    /// world with no route effect and the stop's checks with a 3 lifted by `lift`.
3016    fn three_stop_world(lift: f64) -> Summary {
3017        let mut t = truth();
3018        t.b[ASK] = 0.0;
3019        t.b[NOT] = 0.0;
3020        t.lift = lift;
3021        let mut cold = |grades: &[u8], stop: bool, rng: &mut Rng| {
3022            let (routes, p, pf, _) = fixed_rule(grades, stop, rng);
3023            (routes, p, pf, "learned")
3024        };
3025        let mut sim = Sim::new(1);
3026        for i in 0..40 {
3027            sim.session(&format!("s{i}"), 20, 12, &t, &mut cold);
3028        }
3029        summary::with_fit(sim.summary(), None)
3030    }
3031
3032    /// The fixed rule asks exactly when a check has a 3, so a lift of the stop's checks with a 3 and
3033    /// asking at the stop are one cell of its history. The lift goes to its non-route twin
3034    /// (`three_stop`), not to asking at the stop: a stop without a 3 is asked as often as in the same
3035    /// history without the lift (on evidence alone, so not at all), never by a learned certainty.
3036    #[test]
3037    fn a_lift_of_the_stops_with_a_three_is_not_taken_for_asking() {
3038        let (lifted, plain) = (three_stop_world(1.5), three_stop_world(0.0));
3039        let units = [unit("A", 2, 0), unit("B", 1, 1)];
3040        assert_eq!(fixed_routes(&units, &[]), [Route::Free; 2], "the fixed rule hands both over");
3041        let asked = |s: &Summary| -> f64 {
3042            assert!(remaining(&s.gate) > 0.0, "the gate opened on the history: {:?}", s.gate.allowance);
3043            let seeds = 20u32;
3044            (0..seeds)
3045                .map(|seed| {
3046                    let d = choose(&units, &Check { session: "new", stop: true, tokens: 0 }, &s.fit, &s.gate, ALPHA, u64::from(seed), &[]);
3047                    assert_ne!(d.source, "fixed", "{d:?}");
3048                    d.p[0]
3049                })
3050                .sum::<f64>()
3051                / f64::from(seeds)
3052        };
3053        let (with_lift, without) = (asked(&lifted), asked(&plain));
3054        let m = lifted.fit.model.as_ref().expect("a model");
3055        let sd = |k: usize| (0..=k).map(|j| m.chol[k * P + j].powi(2)).sum::<f64>().sqrt();
3056        let (ask_stop, twin) = ((m.beta[ASK_STOP], sd(ASK_STOP)), (m.beta[THREE_STOP], sd(THREE_STOP)));
3057        let seen = format!("asked {with_lift:.3} with the lift, {without:.3} without; ask_stop {ask_stop:.3?}, three_stop {twin:.3?}");
3058        assert!(with_lift < 0.9, "a stop without a 3 is not blocked by the lift: {seen}");
3059        assert!((with_lift - without).abs() < 0.15, "{seen}");
3060        assert!(ask_stop.0.abs() < 2.0 * ask_stop.1, "asking at the stop is not identified: {seen}");
3061        assert!(twin.0 > 0.75, "the lift is the twin's: {seen}");
3062    }
3063
3064    /// The gate's invariant over a run of checks: the replayed value of what the policy did, at the
3065    /// `DELTA` quantile, never falls more than α × the fixed rule's replayed value below the fixed
3066    /// rule. The same run ungated (every draw of the test admitted) breaks it, so the gate, not the
3067    /// scenario, holds it.
3068    #[test]
3069    fn the_gate_never_risks_more_than_its_alpha_share() {
3070        // A wide posterior whose mean says asking is a little worse than handing over: no evidence for
3071        // a deviation, and most of the test's asks lose under the posterior.
3072        let spec = coefs();
3073        let mut beta: Vec<f64> = spec.iter().map(|c| c.mean).collect();
3074        beta[ASK] = -0.4;
3075        beta[B0] = -0.5;
3076        let mut chol = vec![0.0; P * P];
3077        for k in 0..P {
3078            chol[k * P + k] = if k == ASK { 1.0 } else { 0.3 };
3079        }
3080        let model = Model { beta, chol, sd: spec.iter().map(|c| c.sd).collect(), spread: [0.3; FAMILIES], ..Model::default() };
3081        let rates = Rates { free_stop: 1.0, free_live: 1.0, later: 0.5, wait: 0.0, ask_live: 1.0 };
3082        let f = Fit { key: "test".into(), model: Some(model), rates, ..Fit::default() };
3083        let ids: Vec<String> = (0..10).map(|i| format!("L{i}")).collect();
3084        let mut refused = 0;
3085        let mut run = |gated: bool, rng: &mut Rng| -> (bool, usize) {
3086            let mut gate = Gate::default();
3087            let mut broke = false;
3088            let mut deviated = 0;
3089            for c in 0..200 {
3090                let n = 1 + (rng.next_u64() % 3) as usize;
3091                let units: Vec<Unit> = (0..n).map(|r| unit(&ids[(rng.next_u64() % 10) as usize], 1 + (rng.next_u64() % 3) as u8, r)).collect();
3092                let session = format!("t{}", c / 10);
3093                let check = Check { session: &session, stop: c % 5 == 0, tokens: 0 };
3094                let routes = if c < 10 {
3095                    fixed_routes(&units, &[])
3096                } else if gated {
3097                    let d = choose(&units, &check, &f, &gate, ALPHA, c as u64, &[]);
3098                    // A deviation is admitted exactly when its risk fits what the gate may still risk; a
3099                    // refused one keeps the fixed rule and says so.
3100                    match d.source {
3101                        GATE => {
3102                            assert_eq!((&d.routes, d.risk), (&fixed_routes(&units, &[]), 0.0), "{d:?}");
3103                            refused += 1;
3104                        }
3105                        _ if d.routes != fixed_routes(&units, &[]) => {
3106                            assert_eq!(d.source, EXPERIMENT, "{d:?}");
3107                            assert!(d.risk <= d.gate + 1e-12, "{d:?}");
3108                        }
3109                        _ => {}
3110                    }
3111                    d.routes
3112                } else {
3113                    let open = Gate { allowance: f64::INFINITY, ..gate.clone() };
3114                    choose(&units, &check, &f, &open, ALPHA, c as u64, &[]).routes
3115                };
3116                deviated += usize::from(routes != fixed_routes(&units, &[]));
3117                ledger_add_without(&f, &mut gate, &check, &units, &routes, &[]);
3118                broke |= lower_quantile(&mut gate.gain.clone()) < -gate.allowance - 1e-9;
3119            }
3120            (broke, deviated)
3121        };
3122        let (broke, deviated) = run(true, &mut Rng::new(77));
3123        assert!(!broke && deviated > 0, "gated: broke {broke}, deviated {deviated}");
3124        let (broke, _) = run(false, &mut Rng::new(77));
3125        assert!(broke, "ungated, the same run overspends: the scenario needs the gate");
3126        assert!(refused > 0, "the gate refused deviations, logged as `gate`");
3127    }
3128
3129    #[test]
3130    fn no_price_without_a_budget_and_spend_within_one() {
3131        let mut s = planted(10, 12, 6);
3132        // Pool evidence: every check showed 40 lessons for 12k tokens and applied the first few.
3133        s.pool.bins = (0..6).map(|b| (400 * (1u64 << b), 200 / (b as u64 + 1))).collect();
3134        s.pool.shown = 40 * 120;
3135        s.pool.tokens = 12_000 * 120;
3136        let free = fit(&s, None);
3137        assert_eq!((free.lambda, free.budget), (0.0, None), "no budget, no price");
3138        let checks = s.sessions.iter().map(|x| f64::from(x.checks)).sum::<f64>() / s.sessions.len() as f64;
3139        let spend = |f: &Fit| {
3140            s.sessions.iter().map(|x| f64::from(x.checks) * pool_tokens(f, true, None, session_seed(&x.id)) as f64).sum::<f64>() / s.sessions.len() as f64
3141        };
3142        assert!(spend(&free) >= checks * crate::whole::TOKENS as f64 - 1e-6, "unpriced, the pool keeps the baseline");
3143        let budget = checks * 9_000.0;
3144        let priced = fit(&s, Some(budget));
3145        assert!(priced.lambda > 0.0, "a budget below today's spend prices tokens");
3146        assert!(spend(&priced) <= budget + 1e-6, "{} > {budget}", spend(&priced));
3147        let roomy = fit(&s, Some(checks * 1e6));
3148        assert_eq!(roomy.lambda, 0.0, "a budget the baseline meets costs nothing");
3149    }
3150
3151    /// The hooks choose pools under their harness's limit (live.rs `call_limit`), where a pool with
3152    /// latency evidence may grow past the baseline. The budget's price holds what they spend within the
3153    /// budget under every limit: no limit, one only the middle growth fits under, room for all.
3154    #[test]
3155    fn a_budget_holds_where_pools_may_grow() {
3156        let mut s = Summary::default();
3157        for i in 0..10 {
3158            s.sessions.push(summary::Session { id: format!("s{i}"), checks: 10, ..summary::Session::default() });
3159        }
3160        // Yields known past the largest budget, latency evidence enough, no dilution: unpriced, every
3161        // pool grows as far as the limit lets it. The 95% latency at 16,971 tokens is 3,197 ms, at
3162        // 24,000 3,900 ms.
3163        let pool = PoolFit {
3164            yields: vec![(50.0, 10.0); 8],
3165            per_lesson: 300.0,
3166            latency: (1000.0, 0.1, 500.0),
3167            latency_n: LATENCY_MIN as u32,
3168            latency_reach: 24_000,
3169            not: (-1.0, 0.1),
3170            ..PoolFit::default()
3171        };
3172        let spend = |f: &Fit, limit: Option<u64>| {
3173            s.sessions.iter().map(|x| f64::from(x.checks) * pool_tokens(f, true, limit, session_seed(&x.id)) as f64).sum::<f64>() / s.sessions.len() as f64
3174        };
3175        let baseline = 10.0 * crate::whole::TOKENS as f64;
3176        let unpriced = Fit { pool: pool.clone(), ..Fit::default() };
3177        assert!(spend(&unpriced, Some(60_000)) > 1.5 * baseline, "the scenario: unpriced, the pools grow");
3178        for budget in [baseline, 1.2 * baseline, 0.9 * baseline] {
3179            let mut f = Fit { pool: pool.clone(), budget: Some(budget), ..Fit::default() };
3180            f.lambda = price(&s, &f, budget);
3181            assert!(f.lambda > 0.0, "budget {budget}: growth is priced");
3182            for limit in [None, Some(3_500), Some(60_000)] {
3183                assert!(spend(&f, limit) <= budget + 1e-6, "budget {budget}, limit {limit:?}: spent {} (λ {:e})", spend(&f, limit), f.lambda);
3184            }
3185        }
3186    }
3187
3188    #[test]
3189    fn the_pool_stays_at_the_baseline_without_evidence_or_a_limit() {
3190        let f = Fit::default();
3191        assert_eq!(pool_tokens(&f, true, Some(60_000), 1), crate::whole::TOKENS);
3192        assert_eq!(pool_tokens(&f, false, Some(60_000), 1), crate::whole::TOKENS);
3193        let mut s = planted(3, 6, 7);
3194        s.pool.bins = (0..6).map(|b| (100 * (1u64 << b), 90)).collect();
3195        s.pool.shown = 40 * 18;
3196        s.pool.tokens = 12_000 * 18;
3197        let f = fit(&s, None);
3198        // High yields everywhere, but no latency evidence: never past the baseline.
3199        assert!(pool_tokens(&f, true, Some(60_000), 1) <= crate::whole::TOKENS);
3200        assert_eq!(pool_grid(), [6000, 8485, 12000, 16971, 24000]);
3201    }
3202
3203    /// A larger pool needs a gain > 0 at the `DELTA` quantile: draws that tie with the baseline (no
3204    /// yield past it to tell them apart) never move it, whatever a session's own draw prefers. A
3205    /// smaller one still may on a tie (it costs nothing it was not already pricing).
3206    #[test]
3207    fn a_larger_pool_is_admitted_only_on_a_positive_gain() {
3208        let grid = pool_grid();
3209        let b = grid.iter().position(|&t| t == crate::whole::TOKENS).unwrap();
3210        let tied = PoolDraws { grid: grid.clone(), shared: vec![vec![1.0; DRAWS]; grid.len()] };
3211        let mut play = vec![0.0; grid.len()];
3212        play[grid.len() - 1] = 5.0;
3213        // The baseline and the larger budgets (a smaller one would win the tie of plays at 0).
3214        let feasible: Vec<bool> = (0..grid.len()).map(|i| i >= b).collect();
3215        assert_eq!(tied.choose(&play, &feasible, 0.0), crate::whole::TOKENS, "a tie at 0 keeps the baseline");
3216        let mut ahead = tied.shared.clone();
3217        ahead[grid.len() - 1] = vec![1.5; DRAWS];
3218        let ahead = PoolDraws { grid: grid.clone(), shared: ahead };
3219        assert_eq!(ahead.choose(&play, &feasible, 0.0), grid[grid.len() - 1], "a gain > 0 at every draw admits it");
3220        let mut smaller = vec![0.0; grid.len()];
3221        smaller[0] = 5.0;
3222        assert_eq!(tied.choose(&smaller, &[true; 5], 0.0), grid[0], "a smaller pool on a tie, as before");
3223    }
3224
3225    /// A pool grows only to a budget its latency was measured at: a flat fit over calls at the
3226    /// baseline (a team store on 2026-10-31: b = 0, 203 calls, none above 12,000 tokens) admits no
3227    /// larger budget however roomy the limit, and calls that read `LATENCY_NEAR` of a budget admit it.
3228    #[test]
3229    fn a_pool_grows_only_where_its_latency_was_observed() {
3230        let mut s = Summary::default();
3231        // Pairs of calls at the same size, one 16 s and one 19 s: no slope at all.
3232        s.pool.latency = (0..204u32).map(|i| (if i / 2 < 5 { 9_500 } else { 11_000 + i / 2 }, 16_000 + 3_000 * (i % 2))).collect();
3233        // Yields known past the largest budget, as the bins would give them (`pool_state` reads them).
3234        s.pool.bins = vec![(400, 300); 9];
3235        (s.pool.shown, s.pool.tokens) = (100, 9_100);
3236        let mut pool = PoolFit::default();
3237        pool_state(&s, &mut pool);
3238        assert!(pool.yields.len() == 9 && pool.per_lesson == 91.0, "{pool:?}");
3239        assert_eq!((pool.latency.1, pool.latency_n), (0.0, 204), "the scenario: a flat fit, enough calls");
3240        assert!(pool.latency_reach >= 11_000 && pool.latency_reach < 12_000, "{}", pool.latency_reach);
3241        let roomy = Some(u64::MAX);
3242        assert!(pool_feasible(&pool, crate::whole::TOKENS, roomy, 0.0));
3243        for t in pool_grid().into_iter().filter(|&t| t > crate::whole::TOKENS) {
3244            assert!(!pool_feasible(&pool, t, roomy, 0.0), "{t}: no call read near it");
3245        }
3246        // Twenty calls that read 15,300 tokens or more: 16,971 is within reach, 24,000 is not.
3247        s.pool.latency.extend((0..LATENCY_MIN as u32).map(|i| (15_300 + i, 20_000)));
3248        pool_state(&s, &mut pool);
3249        assert_eq!(pool.latency_reach, 15_300);
3250        assert!(pool_feasible(&pool, 16_971, roomy, 0.0) && !pool_feasible(&pool, 24_000, roomy, 0.0));
3251        // The same evidence without the reach (a fit from before it): no growth.
3252        assert!(!pool_feasible(&PoolFit { latency_reach: 0, ..pool }, 16_971, roomy, 0.0));
3253    }
3254
3255    /// A tail sample's sparse places can leave a bin no lesson was shown in before bins that have some:
3256    /// the yields stop at it, so its prior alone never counts as a yield and no pool past it is known.
3257    #[test]
3258    fn the_yields_stop_at_a_bin_no_lesson_was_shown_in() {
3259        let mut s = Summary::default();
3260        s.pool.bins = vec![(40, 20), (80, 30), (0, 0), (3, 1)];
3261        let mut pool = PoolFit::default();
3262        pool_state(&s, &mut pool);
3263        assert_eq!(pool.yields, pava(&[(21.0, 21.0), (31.0, 51.0)]), "the gap ends them");
3264        s.pool.bins[2] = (5, 1);
3265        pool_state(&s, &mut pool);
3266        assert_eq!(pool.yields.len(), 4, "filled, the tail's bin past it counts");
3267    }
3268
3269    #[test]
3270    fn pava_keeps_the_yields_from_rising() {
3271        let y = pava(&[(5.0, 5.0), (8.0, 2.0), (1.0, 9.0), (2.0, 8.0)]);
3272        let means: Vec<f64> = y.iter().map(|(a, b)| a / (a + b)).collect();
3273        assert!(means.windows(2).all(|w| w[0] >= w[1] - 1e-12), "{means:?}");
3274        assert_eq!(y[0], y[1], "the rising pair merged");
3275        assert_eq!(y[0], (13.0, 7.0));
3276    }
3277
3278    #[test]
3279    fn the_samplers_have_their_moments() {
3280        let mut rng = Rng::new(1);
3281        let n = 20_000;
3282        let z: Vec<f64> = (0..n).map(|_| normal(&mut rng)).collect();
3283        let mean = z.iter().sum::<f64>() / n as f64;
3284        let var = z.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / n as f64;
3285        assert!(mean.abs() < 0.03 && (var - 1.0).abs() < 0.05, "{mean} {var}");
3286        for shape in [0.4, 1.0, 3.5] {
3287            let g = (0..n).map(|_| gamma(shape, &mut rng)).sum::<f64>() / n as f64;
3288            assert!((g - shape).abs() < 0.05 * shape.max(1.0), "{shape}: {g}");
3289        }
3290        let b = (0..n).map(|_| beta_draw(2.0, 6.0, &mut rng)).sum::<f64>() / n as f64;
3291        assert!((b - 0.25).abs() < 0.01, "{b}");
3292    }
3293
3294    /// The confidence sequence: rarely below the true mean at any time, and it closes in on it.
3295    #[test]
3296    fn the_upper_bound_covers_the_mean_at_every_time() {
3297        let mut rng = Rng::new(21);
3298        let mut missed = 0;
3299        for _ in 0..400 {
3300            let x: Vec<f64> = (0..200).map(|_| normal(&mut rng) * 0.4 - 0.1).collect();
3301            missed += usize::from(upper_bound(&x) < -0.1);
3302        }
3303        assert!(missed <= 400 * 7 / 100, "{missed} of 400 below the mean at some time (δ = 5%)");
3304        assert_eq!(upper_bound(&[-5.0; CS_MIN - 1]), f64::INFINITY, "too few sessions to trip");
3305        let x: Vec<f64> = (0..60).map(|_| normal(&mut rng) * 0.4 - 0.3).collect();
3306        assert!(upper_bound(&x) < 0.0, "60 sessions of a clear harm trip it: {}", upper_bound(&x));
3307    }
3308
3309    /// Sixty sessions of a learned policy that hands over half of what the fixed rule asks: under a
3310    /// truth where asking changes far more than handing over (`free_helps` false) the monitor trips;
3311    /// where both routes are as good it does not.
3312    fn monitored(free_helps: bool) -> Summary {
3313        summary::with_fit(monitored_sim(free_helps).0.summary(), None)
3314    }
3315
3316    /// `monitored`'s log before the fit, and its truth.
3317    fn monitored_sim(free_helps: bool) -> (Sim, Truth) {
3318        let mut t = truth();
3319        t.b[ASK] = 1.5;
3320        if free_helps {
3321            t.b[ASK] = 0.0;
3322        }
3323        let mut sim = Sim::new(31);
3324        let mut half = |grades: &[u8], _: bool, rng: &mut Rng| -> (Vec<Route>, Vec<f64>, Vec<f64>, &'static str) {
3325            let routes = grades.iter().map(|_| if rng.unit() < 0.5 { Route::Ask } else { Route::Free }).collect();
3326            (routes, vec![0.5; grades.len()], vec![0.5; grades.len()], "learned")
3327        };
3328        // Every check has a 3 (the fixed rule asks all), live.
3329        let mut sim_session = |sim: &mut Sim, name: &str| {
3330            for _ in 0..6 {
3331                let grades = vec![3, 2];
3332                let ids = vec![format!("L{}", sim.rng.next_u64() % 8), format!("L{}", sim.rng.next_u64() % 8)];
3333                sim.check(name, false, &grades, &ids, &t, &mut half);
3334            }
3335        };
3336        for i in 0..60 {
3337            sim_session(&mut sim, &format!("s{i}"));
3338        }
3339        (sim, t)
3340    }
3341
3342    /// A placebo neither rule delivers (both leave it not now) has no leverage: its weight, however
3343    /// large, multiplies a difference of 0. Nor has a free lesson the policy hands over surely, as the
3344    /// fixed rule does. The monitor's estimate is the 0.7.2 one (pinned within 1e-9), and its weights'
3345    /// health leaves those applies out: before, the placebo, its route's only label, counted at the
3346    /// whole route's weight (a team store's ESS 9 of 127, largest weight 42×).
3347    #[test]
3348    fn a_placebo_neither_rule_delivers_moves_neither_the_estimate_nor_its_health() {
3349        // The monitor of this log under 0.7.2, before its health left zero-leverage applies out.
3350        const MEAN: f64 = -0.155_976_075_091_821;
3351        const UCB: f64 = -0.117_250_989_323_595;
3352        let (mut sim, t) = monitored_sim(false);
3353        let without = summary::with_fit(sim.summary(), None).monitor;
3354        // One check of 12 grade-1 lessons routed as the fixed rule routes them (`CAP` handed over
3355        // surely, the rest not now), the last of the rest drawn as a placebo and judged.
3356        let n = 12;
3357        let mut as_fixed = |grades: &[u8], _: bool, _: &mut Rng| -> (Vec<Route>, Vec<f64>, Vec<f64>, &'static str) {
3358            let routes: Vec<Route> = (0..grades.len())
3359                .map(|j| match j {
3360                    j if j < conscience::CAP => Route::Free,
3361                    j if j + 1 == grades.len() => Route::Placebo,
3362                    _ => Route::Not,
3363                })
3364                .collect();
3365            let pf = routes.iter().map(|r| f64::from(u8::from(*r == Route::Free))).collect();
3366            (routes, vec![0.0; grades.len()], pf, "learned")
3367        };
3368        let ids: Vec<String> = (0..n).map(|i| format!("P{i}")).collect();
3369        sim.check("s59", false, &vec![1; n], &ids, &t, &mut as_fixed);
3370        let s = summary::with_fit(sim.summary(), None);
3371        let judged = |r: Route| s.applies.iter().filter(|a| a.route == r && matches!(a.label, summary::Label::Changed | summary::Label::Ignored)).count();
3372        assert_eq!(judged(Route::Placebo), 1, "the scenario: the placebo is judged");
3373        assert_eq!(fixed_routes(&ids.iter().map(|l| unit(l, 1, 0)).collect::<Vec<_>>(), &[]).iter().filter(|r| **r == Route::Free).count(), conscience::CAP);
3374        let m = &s.monitor;
3375        assert!((m.mean - MEAN).abs() < 1e-9, "mean {} vs 0.7.2's {MEAN}", m.mean);
3376        assert!(m.ucb.is_some_and(|u| (u - UCB).abs() < 1e-9), "ucb {:?} vs 0.7.2's {UCB}", m.ucb);
3377        // The health of the applies that move it: the check added none.
3378        assert_eq!((m.labelled, m.truncated), (without.labelled, without.truncated), "{m:?} vs {without:?}");
3379        assert!(m.max_weight < 10.0 && m.ess > 0.5 * f64::from(m.labelled), "{m:?}");
3380    }
3381
3382    /// A placebo on a check where the policy deviated (the fixed rule asks it, the policy left it not
3383    /// now with chance 1/2) has leverage, and as its route's only label its normalised weight is the
3384    /// whole route's: under `ROUTE_LABELS_MIN` its residual stays out, so one verdict moves the
3385    /// estimate only as far as it moves the fit (0.7.2: by 0.37 of a unit).
3386    #[test]
3387    fn one_label_does_not_carry_its_route() {
3388        for changed in [true, false] {
3389            let (mut sim, t) = monitored_sim(true);
3390            let without = summary::with_fit(sim.summary(), None).monitor;
3391            let mut deviated = |grades: &[u8], _: bool, _: &mut Rng| -> (Vec<Route>, Vec<f64>, Vec<f64>, &'static str) {
3392                (vec![Route::Ask, Route::Placebo], vec![1.0, 0.5], vec![0.0; grades.len()], "learned")
3393            };
3394            let truth = Truth { b: if changed { [9.0; P] } else { [-9.0; P] }, ..t };
3395            sim.check("s59", false, &[3, 2], &["Q0".to_string(), "Q1".to_string()], &truth, &mut deviated);
3396            let s = summary::with_fit(sim.summary(), None);
3397            let want = if changed { summary::Label::Changed } else { summary::Label::Ignored };
3398            assert_eq!(s.applies.iter().filter(|a| a.route == Route::Placebo).map(|a| a.label).collect::<Vec<_>>(), [want], "the scenario");
3399            let shift = (s.monitor.mean - without.mean).abs();
3400            assert!(shift < 0.05, "changed {changed}: one placebo moved the mean by {shift} ({} vs {})", s.monitor.mean, without.mean);
3401        }
3402    }
3403
3404    /// `ROUTE_LABELS_MIN` counts labels with leverage: judged placebos neither rule delivers (the
3405    /// expected kind) do not open the not-now route, so the first placebo on a deviated check after
3406    /// them still moves the estimate only as far as it moves the fit.
3407    #[test]
3408    fn labels_without_leverage_do_not_open_a_route() {
3409        let n = 12;
3410        let mut as_fixed = |grades: &[u8], _: bool, _: &mut Rng| -> (Vec<Route>, Vec<f64>, Vec<f64>, &'static str) {
3411            let routes: Vec<Route> = (0..grades.len())
3412                .map(|j| match j {
3413                    j if j < conscience::CAP => Route::Free,
3414                    j if j + 1 == grades.len() => Route::Placebo,
3415                    _ => Route::Not,
3416                })
3417                .collect();
3418            let pf = routes.iter().map(|r| f64::from(u8::from(*r == Route::Free))).collect();
3419            (routes, vec![0.0; grades.len()], pf, "learned")
3420        };
3421        let mut deviated = |grades: &[u8], _: bool, _: &mut Rng| -> (Vec<Route>, Vec<f64>, Vec<f64>, &'static str) {
3422            (vec![Route::Ask, Route::Placebo], vec![1.0, 0.5], vec![0.0; grades.len()], "learned")
3423        };
3424        for changed in [true, false] {
3425            let (mut sim, t) = monitored_sim(true);
3426            for k in 0..ROUTE_LABELS_MIN {
3427                let ids: Vec<String> = (0..n).map(|i| format!("Z{k}-{i}")).collect();
3428                sim.check(&format!("z{k}"), false, &vec![1; n], &ids, &t, &mut as_fixed);
3429            }
3430            let before = summary::with_fit(sim.summary(), None);
3431            let placebos = |s: &Summary| {
3432                s.applies.iter().filter(|a| a.route == Route::Placebo && matches!(a.label, summary::Label::Changed | summary::Label::Ignored)).count()
3433            };
3434            assert_eq!(placebos(&before), ROUTE_LABELS_MIN, "the scenario: every zero-leverage placebo judged");
3435            let truth = Truth { b: if changed { [9.0; P] } else { [-9.0; P] }, ..t };
3436            sim.check("s59", false, &[3, 2], &["Q0".to_string(), "Q1".to_string()], &truth, &mut deviated);
3437            let s = summary::with_fit(sim.summary(), None);
3438            assert_eq!(placebos(&s), ROUTE_LABELS_MIN + 1);
3439            let shift = (s.monitor.mean - before.monitor.mean).abs();
3440            assert!(shift < 0.05, "changed {changed}: one placebo moved the mean by {shift} ({} vs {})", s.monitor.mean, before.monitor.mean);
3441        }
3442    }
3443
3444    #[test]
3445    fn the_monitor_reverts_a_harmful_history_and_keeps_a_benign_one() {
3446        let harmful = monitored(false);
3447        assert!(harmful.monitor.reverted && harmful.monitor.ucb.is_some_and(|u| u < 0.0), "{:?}", harmful.monitor);
3448        assert_eq!(harmful.monitor.sessions, 60);
3449        // Its weights' health (borg top `Evaluation`): labelled applies weighed, an effective number no
3450        // larger, the largest weight at least their mean, no more cut than weighed.
3451        let m = &harmful.monitor;
3452        assert!(m.labelled > 0 && m.labelled <= m.units, "{m:?}");
3453        assert!(m.ess > 0.0 && m.ess <= f64::from(m.labelled) + 1e-9 && m.max_weight >= 1.0 && m.truncated <= m.labelled, "{m:?}");
3454        let benign = monitored(true);
3455        assert!(!benign.monitor.reverted, "{:?}", benign.monitor);
3456        assert!(benign.monitor.mean > harmful.monitor.mean);
3457    }
3458
3459    /// `choose` at cold start is `fixed_routes` (`cold_start_is_the_fixed_rule`); this pins
3460    /// `fixed_routes` itself, by position.
3461    #[test]
3462    fn the_fixed_routes_are_the_fixed_rule_by_position() {
3463        assert!(fixed_routes(&[], &[]).is_empty());
3464        let block = [unit("A", 1, 0), unit("B", 3, 1), unit("C", 2, 2)];
3465        assert_eq!(fixed_routes(&block, &[]), [Route::Ask; 3], "a 3 blocks: every 3, 2 and 1 up to the cap");
3466        let soft = [unit("A", 1, 0), unit("B", 2, 1)];
3467        assert_eq!(fixed_routes(&soft, &[]), [Route::Free; 2], "no 3: handed over free");
3468        let many: Vec<String> = (0..12).map(|i| format!("L{i}")).collect();
3469        let over: Vec<Unit> = many.iter().enumerate().map(|(r, l)| unit(l, 1, r)).collect();
3470        let routes = fixed_routes(&over, &[]);
3471        assert_eq!(routes.iter().filter(|r| **r == Route::Free).count(), conscience::CAP);
3472        assert_eq!(&routes[conscience::CAP..], &[Route::Not; 2], "past the cap: not now");
3473        // The same lesson named twice past the cap: each copy keeps its own route.
3474        let twice: Vec<Unit> = (0..11).map(|r| unit(if r < 9 { many[r].as_str() } else { "Z" }, 1, r)).collect();
3475        assert_eq!(&fixed_routes(&twice, &[])[9..], &[Route::Free, Route::Not]);
3476    }
3477
3478    #[test]
3479    fn a_prior_fit_writes_no_key() {
3480        assert_eq!(serde_json::to_string(&Fit::default()).unwrap(), "{}");
3481        assert_eq!(serde_json::from_str::<Fit>("{}").unwrap(), Fit::default());
3482    }
3483
3484    /// `borg debug select` P4's policy (`decide_on_evidence`) is `choose`'s routes without the test (α
3485    /// 0) under a gate that admits every deviation, whatever `choose`'s seed; without a model it is the
3486    /// fixed rule.
3487    #[test]
3488    fn the_evidence_rule_is_chooses_without_the_test() {
3489        let f = free_beats_ask();
3490        let open = Gate { gain: vec![0.0; DRAWS], allowance: f64::INFINITY, ..Gate::default() };
3491        let ids: Vec<String> = (0..6).map(|i| format!("L{i}")).collect();
3492        let mut left = 0;
3493        for (n, g) in [(3, [2, 1, 1]), (3, [3, 2, 1]), (2, [1, 1, 0])] {
3494            let units: Vec<Unit> = (0..n).map(|r| unit(&ids[r], g[r], r)).collect();
3495            for stop in [false, true] {
3496                let check = Check { session: "new", stop, tokens: 0 };
3497                let routes = decide_on_evidence(&units, &check, &f, &[]);
3498                for seed in 0..5 {
3499                    let d = choose(&units, &check, &f, &open, 0.0, seed, &[]);
3500                    assert_eq!((&routes, d.xk), (&d.routes, None), "{g:?} stop {stop} seed {seed}");
3501                }
3502                left += usize::from(routes != fixed_routes(&units, &[]));
3503                assert_eq!(decide_on_evidence(&units, &check, &Fit::default(), &[]), fixed_routes(&units, &[]), "no model: the fixed rule");
3504            }
3505        }
3506        assert!(left > 0, "the scenario leaves the fixed rule");
3507    }
3508
3509    /// P4a's rule, here on every grade table: under the n = 0 prior the evidence rule is the fixed rule,
3510    /// at the stop and during the turn, for a session that had a lesson or none, and so is `choose`
3511    /// without the test (its source `evidence`, the prior's deviations refused by no gate but by the
3512    /// evidence). A Thompson play of the prior leaves the fixed rule on such tables.
3513    #[test]
3514    fn under_the_prior_the_evidence_rule_is_the_fixed_rule_on_every_grade_table() {
3515        let p = prior();
3516        let open = Gate { gain: vec![0.0; DRAWS], allowance: f64::INFINITY, ..Gate::default() };
3517        let ids: Vec<String> = (0..8).map(|i| format!("L{i}")).collect();
3518        let mut grades = tables(5);
3519        grades.extend([vec![2; 8], vec![1; 8], [vec![3], vec![2; 7]].concat()]);
3520        for g in &grades {
3521            let units: Vec<Unit> = ids.iter().zip(g).enumerate().map(|(r, (l, g))| unit(l, *g, r)).collect();
3522            for had in [Vec::new(), vec!["L1".to_string()]] {
3523                let want = fixed_routes(&units, &had);
3524                for stop in [true, false] {
3525                    let check = Check { session: "s", stop, tokens: 0 };
3526                    assert_eq!(decide_on_evidence(&units, &check, &p, &had), want, "{g:?} stop {stop} had {had:?}");
3527                    let d = choose(&units, &check, &p, &open, 0.0, 7, &had);
3528                    assert_eq!((&d.routes, d.source), (&want, if units.is_empty() { FIXED } else { EVIDENCE }), "{g:?} stop {stop} had {had:?}");
3529                }
3530            }
3531        }
3532    }
3533
3534    /// A fit certain that every question dilutes the others (`ask_dilution` −1, tight): asking the nine
3535    /// a check with a 3 asks loses to asking few and handing over the rest, and the evidence rule
3536    /// deviates, exactly (0/1 propensities, no test). A fit whose mode dilutes but whose posterior,
3537    /// truncated at 0 as every draw is (`draw_beta`), still reaches no dilution at the `DELTA` quantile
3538    /// (`ask_dilution` −0.2, sd 0.5), or a fit without dilution, keeps the fixed rule.
3539    #[test]
3540    fn a_fit_with_evidence_of_dilution_deviates() {
3541        let world = |dilution: f64, sd: f64| {
3542            let mut beta = vec![0.0; P];
3543            beta[ASK] = 0.2;
3544            beta[NOT] = -1.0;
3545            beta[ASK_M] = dilution;
3546            let mut chol = vec![0.0; P * P];
3547            for k in 0..P {
3548                chol[k * P + k] = if k == ASK_M { sd } else { 0.01 };
3549            }
3550            let model = Model { beta, chol, sd: vec![1.0; P], spread: [0.01; FAMILIES], ..Model::default() };
3551            let rates = Rates { free_stop: 1.0, free_live: 1.0, later: 0.5, wait: 0.0, ask_live: 1.0 };
3552            Fit { key: "dilution".into(), model: Some(model), rates, ..Fit::default() }
3553        };
3554        let ids: Vec<String> = (0..9).map(|i| format!("L{i}")).collect();
3555        let units: Vec<Unit> = ids.iter().enumerate().map(|(r, l)| unit(l, if r == 0 { 3 } else { 2 }, r)).collect();
3556        let base = fixed_routes(&units, &[]);
3557        assert_eq!(base, [Route::Ask; 9], "the scenario: the fixed rule asks all nine");
3558        let gate = open_gate();
3559        for stop in [false, true] {
3560            let check = Check { session: "n", stop, tokens: 0 };
3561            let d = choose(&units, &check, &world(-1.0, 0.01), &gate, ALPHA, 1, &[]);
3562            assert_eq!((d.source, d.xk, d.xs), (EVIDENCE, None, None), "{d:?}");
3563            assert_ne!(d.routes, base, "{d:?}");
3564            assert!(d.routes.contains(&Route::Free) && d.routes.contains(&Route::Ask), "asks few, hands over the rest: {d:?}");
3565            assert_eq!(d.p, d.routes.iter().map(|r| f64::from(u8::from(*r == Route::Ask))).collect::<Vec<_>>(), "exact");
3566            assert_eq!(d.pf, d.routes.iter().map(|r| f64::from(u8::from(*r == Route::Free))).collect::<Vec<_>>(), "exact");
3567            // The wide fit's mode would deviate: only the quantile keeps it on the fixed rule.
3568            let wide = world(-0.2, 0.5);
3569            let m = wide.model.as_ref().expect("a model");
3570            let feats = Feat::of(&units, stop, 0);
3571            let t = theta_mean(m, &feats, &units, &Levels::of(check.session, &m.judge, &units));
3572            let at_mode = solve(&Table::of(&Valuer { t: &t, feats: &feats, rates: &wide.rates }, &[false; 9]), crate::attention::RAIL, conscience::CAP);
3573            assert_ne!(at_mode, base, "stop {stop}");
3574            for f in [wide, world(0.0, 0.01)] {
3575                assert_eq!(decide_on_evidence(&units, &check, &f, &[]), base, "stop {stop}");
3576            }
3577        }
3578    }
3579
3580    /// The test's lessons and order for `units` at a live check under the n = 0 prior (no evidence) and
3581    /// a gate that admits every count: `choose` at many seeds.
3582    fn tested_at(units: &[Unit], had: &[String], seeds: u64) -> Vec<Decision> {
3583        let p = prior();
3584        let open = Gate { gain: vec![0.0; DRAWS], allowance: 1e9, ..Gate::default() };
3585        (0..seeds).map(|seed| choose(units, &Check { session: "s", stop: false, tokens: 0 }, &p, &open, ALPHA, seed, had)).collect()
3586    }
3587
3588    /// The randomised test draws s uniformly in 0..=k (each count's frequency over seeds within four
3589    /// standard errors of 1/(k + 1)), asks the first s in the fixed rule's order (2s before 1s, then
3590    /// the model's) and hands over the rest, its propensities each unit's exact share of the k + 1
3591    /// counts, s = 0 (the fixed rule without a 3) the control.
3592    #[test]
3593    fn the_tests_count_is_uniform_and_its_propensities_exact() {
3594        let ids: Vec<String> = (0..5).map(|i| format!("L{i}")).collect();
3595        let units: Vec<Unit> = ids.iter().zip([1, 2, 1, 2, 1]).enumerate().map(|(r, (l, g))| unit(l, g, r)).collect();
3596        let order = [1, 3, 0, 2, 4];
3597        let n = 1200;
3598        let mut counts = [0usize; 6];
3599        for d in tested_at(&units, &[], n) {
3600            let (Some(5), Some(s)) = (d.xk, d.xs) else { panic!("{d:?}") };
3601            let s = s as usize;
3602            counts[s] += 1;
3603            for (i, &j) in order.iter().enumerate() {
3604                assert_eq!(d.routes[j], if i < s { Route::Ask } else { Route::Free }, "{d:?}");
3605                assert_eq!((d.p[j], d.pf[j]), (outcome::round_p((5 - i) as f64 / 6.0), outcome::round_p((i + 1) as f64 / 6.0)), "{d:?}");
3606            }
3607            assert_eq!(d.source, if s == 0 { EXPERIMENT_CONTROL } else { EXPERIMENT }, "{d:?}");
3608        }
3609        let q = 1.0 / 6.0;
3610        let se = (q * (1.0 - q) / n as f64).sqrt();
3611        for (s, c) in counts.iter().enumerate() {
3612            let freq = *c as f64 / n as f64;
3613            assert!((freq - q).abs() <= 4.0 * se, "s = {s}: {freq:.4} against {q:.4} ({counts:?})");
3614        }
3615    }
3616
3617    /// The test never runs at the stop, and never moves a 3: with a 3 during the turn the 3 is asked on
3618    /// every draw (p 1), and the control is s = k, the fixed rule asking everything.
3619    #[test]
3620    fn the_test_never_touches_the_stop_or_a_three() {
3621        let ids: Vec<String> = (0..3).map(|i| format!("L{i}")).collect();
3622        let units: Vec<Unit> = ids.iter().zip([2, 3, 1]).enumerate().map(|(r, (l, g))| unit(l, g, r)).collect();
3623        let base = fixed_routes(&units, &[]);
3624        let p = prior();
3625        let open = Gate { gain: vec![0.0; DRAWS], allowance: 1e9, ..Gate::default() };
3626        for seed in 0..40 {
3627            let d = choose(&units, &Check { session: "s", stop: true, tokens: 0 }, &p, &open, ALPHA, seed, &[]);
3628            assert_eq!((&d.routes, d.source, d.xk, d.xs), (&base, EVIDENCE, None, None), "{d:?}");
3629        }
3630        let mut seen = std::collections::BTreeSet::new();
3631        for d in tested_at(&units, &[], 60) {
3632            assert_eq!((d.routes[1], d.p[1], d.pf[1], d.xk), (Route::Ask, 1.0, 0.0, Some(2)), "{d:?}");
3633            let s = d.xs.unwrap_or(9);
3634            assert_eq!(d.source == EXPERIMENT_CONTROL, s == 2, "{d:?}");
3635            seen.insert(s);
3636        }
3637        assert_eq!(seen.len(), 3, "every count drawn: {seen:?}");
3638    }
3639
3640    /// A lesson the session already had is not among the test's lessons: it keeps the fixed rule's
3641    /// route on every draw (not now without a 3, asked with one), never free.
3642    #[test]
3643    fn the_test_never_hands_over_a_lesson_the_session_had() {
3644        let ids: Vec<String> = (0..4).map(|i| format!("L{i}")).collect();
3645        let had = ["L2".to_string()];
3646        let soft: Vec<Unit> = ids.iter().zip([2, 1, 2, 1]).enumerate().map(|(r, (l, g))| unit(l, g, r)).collect();
3647        let block: Vec<Unit> = ids.iter().zip([2, 3, 2, 1]).enumerate().map(|(r, (l, g))| unit(l, g, r)).collect();
3648        for (units, kept) in [(&soft, Route::Not), (&block, Route::Ask)] {
3649            let mut moved = 0;
3650            for d in tested_at(units, &had, 60) {
3651                assert_eq!((d.routes[2], d.pf[2], d.xk), (kept, 0.0, Some(if kept == Route::Not { 3 } else { 2 })), "{d:?}");
3652                moved += usize::from(d.source == EXPERIMENT);
3653            }
3654            assert!(moved > 0, "the scenario: the test ran");
3655        }
3656    }
3657
3658    /// The gate rations the test: with nothing left to risk, every count that leaves the fixed rule is
3659    /// refused (source `gate`, the fixed rule's routes, k and the refused s logged, nothing taken), so
3660    /// the propensities are the fixed rule's; and α 0 (`risk` 0 in collectives.json) turns it off.
3661    #[test]
3662    fn the_gate_refuses_the_test_when_short_and_risk_zero_turns_it_off() {
3663        let ids: Vec<String> = (0..3).map(|i| format!("L{i}")).collect();
3664        let units: Vec<Unit> = ids.iter().zip([2, 1, 1]).enumerate().map(|(r, (l, g))| unit(l, g, r)).collect();
3665        let base = fixed_routes(&units, &[]);
3666        let exact = |r: Route| base.iter().map(|x| f64::from(u8::from(*x == r))).collect::<Vec<_>>();
3667        let p = prior();
3668        let short = Gate { gain: vec![0.0; DRAWS], allowance: 1e-12, ..Gate::default() };
3669        let check = Check { session: "s", stop: false, tokens: 0 };
3670        let (mut refused, mut control) = (0, 0);
3671        for seed in 0..40 {
3672            let d = choose(&units, &check, &p, &short, ALPHA, seed, &[]);
3673            assert_eq!((&d.routes, &d.p, &d.pf, d.risk, d.xk), (&base, &exact(Route::Ask), &exact(Route::Free), 0.0, Some(3)), "{d:?}");
3674            match d.xs {
3675                Some(0) => control += usize::from(d.source == EXPERIMENT_CONTROL),
3676                _ => refused += usize::from(d.source == GATE),
3677            }
3678            let off = choose(&units, &check, &p, &open_gate(), 0.0, seed, &[]);
3679            assert_eq!((&off.routes, off.source, off.xk, off.xs), (&base, EVIDENCE, None, None), "{off:?}");
3680        }
3681        assert!(refused > 0 && control > 0 && refused + control == 40, "refused {refused}, control {control}");
3682        // The same gate under a larger α (the collective's `risk`) has room: the share scales the
3683        // allowance the ledger accrued at `ALPHA`.
3684        let room = Gate { allowance: 1e9 * ALPHA, ..short };
3685        assert!((0..40).any(|seed| choose(&units, &check, &p, &room, ALPHA, seed, &[]).source == EXPERIMENT));
3686        assert!((0..40).all(|seed| choose(&units, &check, &p, &room, 1e-12, seed, &[]).source != EXPERIMENT), "α scales it down");
3687    }
3688
3689    /// α scales the evidence rule's room too: a ledger that holds no gains, only a loss it already
3690    /// took, admits an evident deviation at `ALPHA` from the allowance alone, and α 0 (`risk` 0 in
3691    /// collectives.json) refuses the same deviation (source `gate`, the fixed rule's routes, nothing
3692    /// taken), though the ledger's allowance is unchanged.
3693    #[test]
3694    fn risk_zero_refuses_an_evidence_deviation_the_allowance_alone_would_cover() {
3695        let f = free_beats_ask();
3696        // A 3 and 2s during the turn: the fixed rule asks them all, the model hands them over.
3697        let ids: Vec<String> = (0..4).map(|i| format!("L{i}")).collect();
3698        let units: Vec<Unit> = ids.iter().enumerate().map(|(r, l)| unit(l, if r == 0 { 3 } else { 2 }, r)).collect();
3699        let base = fixed_routes(&units, &[]);
3700        let check = Check { session: "n", stop: false, tokens: 0 };
3701        // Guard: the loss outweighs any gain the deviation's own value can make (a value is a sum of
3702        // per-apply chances, so it gains less than one per apply), and the allowance covers the loss.
3703        let loss = 2.0 * units.len() as f64;
3704        let gate = Gate { gain: vec![-loss; DRAWS], allowance: 2.0 * loss, ..Gate::default() };
3705        let at = choose(&units, &check, &f, &gate, ALPHA, 1, &[]);
3706        assert_eq!(at.source, EVIDENCE, "the scenario: the allowance admits the deviation at ALPHA: {at:?}");
3707        assert_ne!(at.routes, base, "{at:?}");
3708        let off = choose(&units, &check, &f, &gate, 0.0, 1, &[]);
3709        assert_eq!((&off.routes, off.source, off.risk, off.gate, off.xk), (&base, GATE, 0.0, 0.0, None), "{off:?}");
3710    }
3711
3712    /// Dilution is not assumed: its prior's mode is 0, so under the n = 0 prior's mean the value of a
3713    /// question does not depend on how many are asked with it.
3714    #[test]
3715    fn the_dilution_prior_has_its_mode_at_zero() {
3716        let c = coefs();
3717        for k in [ASK_M, FREE_M] {
3718            assert_eq!((c[k].mean, c[k].sd, c[k].nonpos, c[k].ard), (0.0, 0.1, true, false), "{}", NAMES[k]);
3719        }
3720        let p = prior();
3721        let m = p.model.as_ref().expect("the prior is a model");
3722        let units = [unit("A", 2, 0), unit("B", 2, 1), unit("C", 2, 2)];
3723        let feats = Feat::of(&units, false, 0);
3724        let t = theta_mean(m, &feats, &units, &Levels::of("s", &m.judge, &units));
3725        let v = Valuer { t: &t, feats: &feats, rates: &p.rates };
3726        assert!(v.ask(0, 1) > 0.0 && (v.ask(0, 1) - v.ask(0, 3)).abs() < 1e-12, "{} vs {}", v.ask(0, 1), v.ask(0, 3));
3727        assert!((v.free(0, 1) - v.free(0, 3)).abs() < 1e-12);
3728    }
3729
3730    /// The gate's and the evidence rule's draws of a bounded slope are its prior truncated at 0, not
3731    /// clamped: under the n = 0 prior `DILUTION_PRIOR`'s half-normal, every draw below 0 (no point mass
3732    /// at 0) with the mean −0.1·√(2/π); the unbounded coefficients keep their plain draw β̂ + L z.
3733    #[test]
3734    fn a_bounded_slope_is_drawn_from_its_truncated_prior() {
3735        let p = prior();
3736        let m = p.model.as_ref().expect("the prior is a model");
3737        const N: u64 = 4000;
3738        let draws: Vec<[f64; P]> = (0..N).map(|k| draw_beta(m, gate_seed(&p, k as usize))).collect();
3739        let half = -DILUTION_PRIOR.1 * (2.0 / std::f64::consts::PI).sqrt();
3740        // Its sd 0.1·√(1 − 2/π) ≈ 0.060 over 4000 draws: a standard error of 0.001.
3741        for k in [ASK_M, FREE_M] {
3742            assert!(draws.iter().all(|b| b[k] < 0.0), "{}: a draw at 0", NAMES[k]);
3743            let mean = draws.iter().map(|b| b[k]).sum::<f64>() / N as f64;
3744            assert!((mean - half).abs() < 0.005, "{}: mean {mean} against {half}", NAMES[k]);
3745        }
3746        let ask = draws.iter().map(|b| b[ASK]).sum::<f64>() / N as f64;
3747        assert!((ask - ASK_PRIOR.0).abs() < 0.03, "ask {ask}");
3748        // A bound far above the mean leaves the draw as it was; one far below it, never past 0.
3749        for z in [-2.0, -0.3, 0.0, 1.0, 2.5] {
3750            assert!((below_zero(-1.0, 0.1, z) - (-1.0 + 0.1 * z)).abs() < 1e-6, "z {z}");
3751            let x = below_zero(50.0, 0.1, z);
3752            assert!(x <= 0.0 && x > -0.01, "z {z}: {x}");
3753        }
3754        assert!(below_zero(0.0, 0.1, -1.0) < below_zero(0.0, 0.1, 1.0), "monotone in z");
3755        assert!((phi(1.959_964) - 0.975).abs() < 1e-6 && (probit(0.975f64.ln(), 0.025) - 1.959_964).abs() < 1e-5);
3756    }
3757
3758    /// The n = 0 prior is a model at the priors (means, sds, the families' starting spreads) with
3759    /// Beta(1, 1) delivery rates; `fit` without a label still has none, so cold start stays the fixed rule.
3760    #[test]
3761    fn the_prior_is_a_model_of_the_priors_alone() {
3762        let p = prior();
3763        let m = p.model.as_ref().expect("the prior is a model");
3764        assert!(m.current() && m.converged && p.key.is_empty() && p.labels == 0);
3765        for (k, c) in coefs().iter().enumerate() {
3766            assert_eq!(m.beta[k], if c.nonpos { c.mean.min(0.0) } else { c.mean }, "{}", NAMES[k]);
3767            assert!((m.chol[k * P + k] - c.sd).abs() < 1e-12, "{}", NAMES[k]);
3768        }
3769        assert_eq!(m.spread, [LEVEL_START; FAMILIES]);
3770        assert_eq!(p.rates, Rates { free_stop: 0.5, free_live: 0.5, later: 0.5, wait: 0.5, ask_live: 1.0 });
3771        assert!(fit(&Summary::default(), None).model.is_none());
3772    }
3773
3774    /// A prediction is σ(η) where the model is certain, drawn toward ½ by its uncertainty (the level
3775    /// effects' included), and nothing for a model of another layout.
3776    #[test]
3777    fn a_prediction_is_the_posterior_chance_of_a_change() {
3778        let f = Feat { grade: 3, rank: 1, stop: true, three: true, pool: 0.0 };
3779        let mut beta = vec![0.0; P];
3780        beta[B0] = 0.8;
3781        beta[G3] = 0.4;
3782        beta[ASK] = 0.3;
3783        let lessons = BTreeMap::from([("A".to_string(), (0.5, 0.0))]);
3784        let certain = Model { beta: beta.clone(), chol: vec![0.0; P * P], sd: vec![1.0; P], spread: [0.0; FAMILIES], lessons, ..Model::default() };
3785        let mut fit = Fit { model: Some(certain.clone()), ..Fit::default() };
3786        let eta = dot(&std::array::from_fn(|k| beta[k]), &design(&f, Route::Ask, 2)) + 0.5;
3787        assert!((predict(&fit, &f, Route::Ask, 2, "s", "A").unwrap() - sigmoid(eta)).abs() < 1e-12);
3788        let mut wide = certain.clone();
3789        for k in 0..P {
3790            wide.chol[k * P + k] = 1.0;
3791        }
3792        wide.spread = [0.5; FAMILIES];
3793        fit.model = Some(wide);
3794        let p = predict(&fit, &f, Route::Ask, 2, "s", "unseen").unwrap();
3795        assert!(p > 0.5 && p < sigmoid(eta - 0.5), "{p} against {}", sigmoid(eta - 0.5));
3796        fit.model = Some(Model { beta: vec![0.0; P - 1], ..certain });
3797        assert_eq!(predict(&fit, &f, Route::Ask, 2, "s", "A"), None);
3798    }
3799}