1use std::collections::HashMap;
54
55use firstpass_core::{Features, PriceTable, TaskKind, Verdict};
56
57#[derive(Debug, Clone, PartialEq, Eq, Hash)]
67pub struct ContextBucket {
68 pub task_kind: TaskKind,
70 pub prompt_bucket_coarse: u32,
72}
73
74impl ContextBucket {
75 #[must_use]
77 pub fn from_features(f: &Features) -> Self {
78 Self {
79 task_kind: f.task_kind,
80 prompt_bucket_coarse: f.prompt_token_bucket / 2,
81 }
82 }
83
84 #[must_use]
95 pub fn representative_prompt_tokens(&self) -> u64 {
96 1u64 << (self.prompt_bucket_coarse.min(9) * 2 + 1)
100 }
101}
102
103#[derive(Debug, Default, Clone)]
105struct ArmCounts {
106 pass: f64,
107 fail: f64,
108}
109
110impl ArmCounts {
111 fn n(&self) -> f64 {
112 self.pass + self.fail
113 }
114}
115
116#[derive(Debug)]
122pub struct StartRungBandit {
123 exploration: f64,
125 min_observations: usize,
127 algorithm: Algorithm,
130 discount: f64,
134 rng: u64,
136 data: HashMap<ContextBucket, HashMap<u32, ArmCounts>>,
138 prior_strength: f64,
144}
145
146const PROPENSITY_SAMPLES: usize = 64;
149
150fn argmin_expected_cost(
154 ladder: &[String],
155 prices: &PriceTable,
156 in_tokens: u64,
157 mut p_pass: impl FnMut(u32) -> f64,
158) -> u32 {
159 let out_tokens = (in_tokens / 2).max(1);
162
163 let mut best_s = 0u32;
164 let mut best_cost = f64::MAX;
165 for s in 0..ladder.len() {
166 let mut expected_cost = 0.0_f64;
167 let mut p_reach = 1.0_f64;
168 for (r, model) in ladder.iter().enumerate().skip(s) {
169 let price = prices
170 .get(model)
171 .map(|p| p.cost(in_tokens, out_tokens))
172 .unwrap_or(0.0);
173 expected_cost += p_reach * price;
174 p_reach *= 1.0 - p_pass(r as u32);
175 if p_reach < 1e-10 {
176 break;
177 }
178 }
179 if expected_cost < best_cost {
180 best_cost = expected_cost;
181 best_s = s as u32;
182 }
183 }
184 best_s
185}
186
187#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
188pub enum Algorithm {
189 #[default]
192 Ucb1,
193 Thompson,
197}
198
199impl StartRungBandit {
200 #[must_use]
202 pub fn new(min_observations: usize, exploration: f64) -> Self {
203 Self::with_algorithm(min_observations, exploration, Algorithm::Ucb1, 1.0, 0x9E37)
204 }
205
206 #[must_use]
209 pub fn with_algorithm(
210 min_observations: usize,
211 exploration: f64,
212 algorithm: Algorithm,
213 discount: f64,
214 seed: u64,
215 ) -> Self {
216 Self {
217 exploration,
218 min_observations,
219 algorithm,
220 discount: discount.clamp(f64::MIN_POSITIVE, 1.0),
221 rng: seed.max(1),
222 data: HashMap::new(),
223 prior_strength: 10.0,
224 }
225 }
226
227 #[must_use]
229 pub fn with_prior_strength(mut self, strength: f64) -> Self {
230 self.prior_strength = strength;
231 self
232 }
233
234 fn discount_context(&mut self, ctx: &ContextBucket) {
236 if self.discount >= 1.0 {
237 return;
238 }
239 if let Some(arms) = self.data.get_mut(ctx) {
240 for c in arms.values_mut() {
241 c.pass *= self.discount;
242 c.fail *= self.discount;
243 }
244 }
245 }
246
247 fn next_u01(&mut self) -> f64 {
249 let mut x = self.rng;
250 x ^= x >> 12;
251 x ^= x << 25;
252 x ^= x >> 27;
253 self.rng = x;
254 let v = x.wrapping_mul(0x2545_F491_4F6C_DD1D);
255 (v >> 11) as f64 / (1u64 << 53) as f64
256 }
257
258 fn next_normal(&mut self) -> f64 {
260 let u1 = self.next_u01().max(f64::MIN_POSITIVE);
261 let u2 = self.next_u01();
262 (-2.0 * u1.ln()).sqrt() * (std::f64::consts::TAU * u2).cos()
263 }
264
265 fn next_gamma(&mut self, shape: f64) -> f64 {
267 debug_assert!(shape >= 1.0, "Beta(+1 prior) keeps shapes >= 1");
268 let d = shape - 1.0 / 3.0;
269 let c = 1.0 / (9.0 * d).sqrt();
270 loop {
271 let x = self.next_normal();
272 let v = (1.0 + c * x).powi(3);
273 if v <= 0.0 {
274 continue;
275 }
276 let u = self.next_u01();
277 if u < 1.0 - 0.0331 * x.powi(4) || u.ln() < 0.5 * x * x + d * (1.0 - v + v.ln()) {
278 return d * v;
279 }
280 }
281 }
282
283 fn thompson_pass(&mut self, ctx: &ContextBucket, rung: u32) -> f64 {
286 let (a, b) = self
287 .data
288 .get(ctx)
289 .and_then(|arms| arms.get(&rung))
290 .map_or((1.0, 1.0), |c| (c.pass + 1.0, c.fail + 1.0));
291 let x = self.next_gamma(a);
292 let y = self.next_gamma(b);
293 x / (x + y)
294 }
295
296 #[must_use]
300 pub fn pass_estimate(&self, ctx: &ContextBucket, rung: u32) -> Option<f64> {
301 let arms = self.data.get(ctx)?;
302 let total: f64 = arms.values().map(ArmCounts::n).sum();
303 if total < self.min_observations as f64 {
304 return None;
305 }
306 let c = arms.get(&rung)?;
307 if c.n() == 0.0 {
308 return None;
309 }
310 Some((c.pass + 1.0) / (c.n() + 2.0))
311 }
312
313 pub fn observe(&mut self, ctx: &ContextBucket, rung: u32, verdict: Verdict) {
317 match verdict {
318 Verdict::Abstain => {} Verdict::Pass => {
320 self.discount_context(ctx);
321 self.data
322 .entry(ctx.clone())
323 .or_default()
324 .entry(rung)
325 .or_default()
326 .pass += 1.0;
327 }
328 Verdict::Fail => {
329 self.discount_context(ctx);
330 self.data
331 .entry(ctx.clone())
332 .or_default()
333 .entry(rung)
334 .or_default()
335 .fail += 1.0;
336 }
337 }
338 }
339
340 fn ucb_pass(&self, ctx: &ContextBucket, rung: u32, ln_n: f64) -> f64 {
344 let Some(arms) = self.data.get(ctx) else {
345 return 1.0; };
347 let Some(counts) = arms.get(&rung) else {
348 return 1.0; };
350 let n = counts.n();
351 if n == 0.0 {
352 return 1.0;
353 }
354 let p_hat = counts.pass / n;
355 (p_hat + self.exploration * (ln_n / n).sqrt()).clamp(0.0, 1.0)
356 }
357
358 #[must_use]
366 pub fn choose_start(&self, ctx: &ContextBucket, ladder: &[String], prices: &PriceTable) -> u32 {
367 let top = ladder.len();
368 if top == 0 {
369 return 0;
370 }
371
372 let n_total: f64 = self
374 .data
375 .get(ctx)
376 .map(|arms| arms.values().map(ArmCounts::n).sum())
377 .unwrap_or(0.0);
378 if n_total < self.min_observations as f64 {
379 return 0;
380 }
381
382 let ln_n = n_total.ln();
383 argmin_expected_cost(ladder, prices, ctx.representative_prompt_tokens(), |r| {
384 self.ucb_pass(ctx, r, ln_n)
385 })
386 }
387
388 #[must_use]
398 pub fn choose_start_with_propensity(
399 &mut self,
400 ctx: &ContextBucket,
401 ladder: &[String],
402 prices: &PriceTable,
403 ) -> (u32, Option<f64>) {
404 if self.algorithm == Algorithm::Ucb1 {
405 return (self.choose_start(ctx, ladder, prices), None);
406 }
407 let top = ladder.len();
408 if top == 0 {
409 return (0, None);
410 }
411 let n_total: f64 = self
412 .data
413 .get(ctx)
414 .map(|arms| arms.values().map(ArmCounts::n).sum())
415 .unwrap_or(0.0);
416 if n_total < self.min_observations as f64 {
417 return (0, None);
418 }
419
420 let draw = |this: &mut Self| {
421 let samples: Vec<f64> = (0..top)
423 .map(|r| this.thompson_pass(ctx, r as u32))
424 .collect();
425 argmin_expected_cost(ladder, prices, ctx.representative_prompt_tokens(), |r| {
426 samples[r as usize]
427 })
428 };
429
430 let choice = draw(self);
431 let matches = (0..PROPENSITY_SAMPLES)
432 .filter(|_| draw(self) == choice)
433 .count();
434 let p = (matches as f64 / PROPENSITY_SAMPLES as f64)
437 .max(1.0 / (2.0 * PROPENSITY_SAMPLES as f64));
438 (choice, Some(p))
439 }
440
441 fn counts(&self, ctx: &ContextBucket, rung: u32) -> (f64, f64) {
443 self.data
444 .get(ctx)
445 .and_then(|arms| arms.get(&rung))
446 .map_or((0.0, 0.0), |c| (c.pass, c.fail))
447 }
448
449 #[must_use]
464 pub fn choose_start_with_prior(
465 &mut self,
466 ctx: &ContextBucket,
467 ladder: &[String],
468 prices: &PriceTable,
469 prior: Option<&[f64]>,
470 ) -> (u32, Option<f64>) {
471 let Some(prior) = prior else {
472 return self.choose_start_with_propensity(ctx, ladder, prices);
473 };
474 if prior.is_empty() || prior.len() != ladder.len() {
475 return self.choose_start_with_propensity(ctx, ladder, prices);
476 }
477 let strength = self.prior_strength;
478
479 if self.algorithm == Algorithm::Ucb1 {
480 let choice =
481 argmin_expected_cost(ladder, prices, ctx.representative_prompt_tokens(), |r| {
482 let (obs_pass, obs_fail) = self.counts(ctx, r);
483 let a = strength * prior[r as usize] + obs_pass;
484 let b = strength * (1.0 - prior[r as usize]) + obs_fail;
485 (a / (a + b)).clamp(0.0, 1.0)
486 });
487 return (choice, None);
488 }
489
490 let draw = |this: &mut Self| {
491 let samples: Vec<f64> = (0..ladder.len())
492 .map(|r| {
493 let (obs_pass, obs_fail) = this.counts(ctx, r as u32);
494 let a = (strength * prior[r] + obs_pass + 1.0).max(1.0);
497 let b = (strength * (1.0 - prior[r]) + obs_fail + 1.0).max(1.0);
498 let x = this.next_gamma(a);
499 let y = this.next_gamma(b);
500 x / (x + y)
501 })
502 .collect();
503 argmin_expected_cost(ladder, prices, ctx.representative_prompt_tokens(), |r| {
504 samples[r as usize]
505 })
506 };
507
508 let choice = draw(self);
509 let matches = (0..PROPENSITY_SAMPLES)
510 .filter(|_| draw(self) == choice)
511 .count();
512 let p = (matches as f64 / PROPENSITY_SAMPLES as f64)
513 .max(1.0 / (2.0 * PROPENSITY_SAMPLES as f64));
514 (choice, Some(p))
515 }
516
517 pub fn feed_trace_attempts(
519 &mut self,
520 ctx: &ContextBucket,
521 attempts: &[firstpass_core::Attempt],
522 ) {
523 for attempt in attempts {
524 self.observe(ctx, attempt.rung, attempt.verdict);
525 }
526 }
527}
528
529#[cfg(test)]
530mod tests {
531 use super::*;
532 use firstpass_core::{
533 Attempt, Features, FinalOutcome, GENESIS_HASH, Mode, PolicyRef, RequestInfo, ServedFrom,
534 TaskKind, Trace,
535 };
536
537 #[test]
550 fn the_trajectory_hint_does_not_affect_routing() {
551 let mut easy = Features::new(TaskKind::CodeEdit);
552 easy.prompt_token_bucket = 6;
553 let mut hard = easy.clone();
554 hard.difficulty_hint = 3;
555
556 assert_eq!(
557 ContextBucket::from_features(&easy),
558 ContextBucket::from_features(&hard),
559 "the hint is refuted as a routing signal — requests differing only in it must share an \
560 arm, or the bandit is re-fragmented by a dimension that measured negative"
561 );
562 assert_eq!(hard.difficulty_hint, 3);
564 }
565
566 fn ctx_code() -> ContextBucket {
567 ContextBucket {
568 task_kind: TaskKind::CodeEdit,
569 prompt_bucket_coarse: 3,
570 }
571 }
572
573 fn ctx_chat() -> ContextBucket {
574 ContextBucket {
575 task_kind: TaskKind::Chat,
576 prompt_bucket_coarse: 3,
577 }
578 }
579
580 const HAIKU: &str = "anthropic/claude-haiku-4-5";
581 const SONNET: &str = "anthropic/claude-sonnet-5";
582
583 #[test]
584 fn cold_start_returns_rung_0() {
585 let b = StartRungBandit::new(50, 1.0);
586 let prices = PriceTable::defaults();
587 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
588 assert_eq!(b.choose_start(&ctx_code(), &ladder, &prices), 0);
590 }
591
592 #[test]
593 fn empty_ladder_returns_rung_0() {
594 let b = StartRungBandit::new(50, 1.0);
595 let prices = PriceTable::defaults();
596 assert_eq!(b.choose_start(&ctx_code(), &[], &prices), 0);
597 }
598
599 #[test]
600 fn after_enough_rung0_fails_rung1_passes_picks_rung1_for_that_context() {
601 let mut b = StartRungBandit::new(50, 1.0);
602 let prices = PriceTable::defaults();
603 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
604
605 let code = ctx_code();
607 for _ in 0..60 {
608 b.observe(&code, 0, Verdict::Fail);
609 b.observe(&code, 1, Verdict::Pass);
610 }
611 assert_eq!(
613 b.choose_start(&code, &ladder, &prices),
614 1,
615 "should skip rung 0 after observing it always fails"
616 );
617
618 let chat = ctx_chat();
620 assert_eq!(
621 b.choose_start(&chat, &ladder, &prices),
622 0,
623 "different context must be independent (cold start)"
624 );
625 }
626
627 #[test]
628 fn abstain_verdicts_are_not_counted() {
629 let mut b = StartRungBandit::new(2, 1.0); let code = ctx_code();
631 for _ in 0..100 {
633 b.observe(&code, 0, Verdict::Abstain);
634 }
635 let prices = PriceTable::defaults();
636 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
637 assert_eq!(b.choose_start(&code, &ladder, &prices), 0);
639 }
640
641 #[test]
642 fn single_rung_ladder_always_returns_0() {
643 let mut b = StartRungBandit::new(1, 1.0);
644 let code = ctx_code();
645 b.observe(&code, 0, Verdict::Fail);
646 b.observe(&code, 0, Verdict::Pass);
647 let prices = PriceTable::defaults();
648 let ladder = vec![HAIKU.to_owned()];
649 assert_eq!(b.choose_start(&code, &ladder, &prices), 0);
650 }
651
652 fn stub_attempt(rung: u32, verdict: Verdict) -> Attempt {
654 Attempt {
655 rung,
656 model: HAIKU.to_owned(),
657 provider: "anthropic".to_owned(),
658 in_tokens: 1000,
659 cache_write_tokens: 0,
660 cache_read_tokens: 0,
661 out_tokens: 500,
662 cost_usd: 0.001,
663 latency_ms: 10,
664 gates: vec![],
665 verdict,
666 truncated: false,
667 }
668 }
669
670 #[test]
671 fn from_features_derives_bucket_correctly() {
672 let mut f = Features::new(TaskKind::CodeEdit);
673 f.prompt_token_bucket = 7; let b = ContextBucket::from_features(&f);
675 assert_eq!(b.task_kind, TaskKind::CodeEdit);
676 assert_eq!(b.prompt_bucket_coarse, 3);
677 }
678
679 #[test]
680 fn feed_trace_attempts_populates_counts() {
681 let mut bandit = StartRungBandit::new(2, 1.0);
682 let ctx = ctx_code();
683 let attempts = vec![
684 stub_attempt(0, Verdict::Fail),
685 stub_attempt(1, Verdict::Pass),
686 ];
687 bandit.feed_trace_attempts(&ctx, &attempts);
688
689 let prices = PriceTable::defaults();
692 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
693 let _ = bandit.choose_start(&ctx, &ladder, &prices);
697 }
698
699 #[test]
703 fn zero_pass_rate_rung_is_not_optimistic() {
704 let mut b = StartRungBandit::new(10, 0.0); let ctx = ctx_code();
708 for _ in 0..20 {
709 b.observe(&ctx, 0, Verdict::Fail);
710 b.observe(&ctx, 1, Verdict::Pass);
711 }
712 let prices = PriceTable::defaults();
713 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
714 assert_eq!(b.choose_start(&ctx, &ladder, &prices), 1);
718 }
719
720 fn trace_with_features_and_attempts(features: Features, attempts: Vec<Attempt>) -> Trace {
725 let mut trace = Trace {
726 trace_id: uuid::Uuid::now_v7(),
727 prev_hash: GENESIS_HASH.to_owned(),
728 tenant_id: "test-tenant".to_owned(),
729 session_id: "sess-warm".to_owned(),
730 ts: jiff::Timestamp::now(),
731 mode: Mode::Enforce,
732 policy: PolicyRef {
733 id: "test@v0".to_owned(),
734 explore: false,
735 propensity: None,
736 mode_profile: None,
737 },
738 request: RequestInfo {
739 api: "anthropic.messages".to_owned(),
740 prompt_hash: "deadbeef".to_owned(),
741 features,
742 },
743 attempts,
744 deferred: vec![],
745 final_: FinalOutcome {
746 served_rung: Some(0),
747 served_from: ServedFrom::Attempt,
748 total_cost_usd: 0.001,
749 gate_cost_usd: 0.0,
750 total_latency_ms: 10,
751 escalations: 0,
752 counterfactual_baseline_usd: 0.001,
753 savings_usd: 0.0,
754 cache_source: None,
755 },
756 probe: None,
757 rollout: None,
758 shadow: None,
759 route_ix: None,
760 predicted_pass: None,
761 decision_prior: None,
762 elastic: None,
763 risk: None,
764 experiment: None,
765 };
766 trace.recompute_savings();
767 trace
768 }
769
770 #[tokio::test]
773 async fn warm_start_from_trace_store_replays_counts() {
774 let db = std::env::temp_dir().join(format!("bandit-warmstart-{}.db", uuid::Uuid::now_v7()));
775 let (tx, writer) = crate::store::open(&db).unwrap();
776
777 let mut f = Features::new(TaskKind::CodeEdit);
779 f.prompt_token_bucket = 7; for _ in 0..60 {
783 let attempts = vec![
784 stub_attempt(0, Verdict::Fail),
785 stub_attempt(1, Verdict::Pass),
786 ];
787 tx.try_send(trace_with_features_and_attempts(f.clone(), attempts))
788 .unwrap();
789 }
790 drop(tx);
791 writer.await.unwrap();
792
793 let mut bandit = StartRungBandit::new(50, 0.0); let stored = crate::store::load_all_traces(&db).unwrap();
796 assert_eq!(stored.len(), 60, "all traces must be stored");
797 for trace in &stored {
798 let ctx = ContextBucket::from_features(&trace.request.features);
799 bandit.feed_trace_attempts(&ctx, &trace.attempts);
800 }
801
802 let prices = PriceTable::defaults();
804 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
805 let ctx = ContextBucket::from_features(&f);
806 assert_eq!(
807 bandit.choose_start(&ctx, &ladder, &prices),
808 1,
809 "warm-started bandit must prefer rung 1 after 60 fail/pass pairs"
810 );
811
812 let mut f2 = Features::new(TaskKind::Chat);
814 f2.prompt_token_bucket = 7;
815 let ctx2 = ContextBucket::from_features(&f2);
816 assert_eq!(
817 bandit.choose_start(&ctx2, &ladder, &prices),
818 0,
819 "different context must be independent"
820 );
821
822 let _ = std::fs::remove_file(&db);
823 }
824
825 #[test]
828 fn prior_none_matches_choose_start_with_propensity() {
829 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
832 let prices = PriceTable::defaults();
833 let ctx = ctx_code();
834
835 let mut a = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 123);
836 let mut b = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 123);
837 for _ in 0..30 {
838 a.observe(&ctx, 0, Verdict::Fail);
839 a.observe(&ctx, 1, Verdict::Pass);
840 b.observe(&ctx, 0, Verdict::Fail);
841 b.observe(&ctx, 1, Verdict::Pass);
842 }
843
844 for _ in 0..20 {
845 let want = a.choose_start_with_propensity(&ctx, &ladder, &prices);
846 let got = b.choose_start_with_prior(&ctx, &ladder, &prices, None);
847 assert_eq!(
848 want, got,
849 "prior=None must be byte-identical to the existing method"
850 );
851 }
852 }
853
854 #[test]
855 fn confident_hard_prior_starts_above_zero_with_no_observations() {
856 let ladder = vec![
860 HAIKU.to_owned(),
861 SONNET.to_owned(),
862 "anthropic/claude-opus-4-8".to_owned(),
863 ];
864 let prices = PriceTable::defaults();
865 let ctx = ctx_code();
866 let hard_prior = firstpass_core::cumulative_pass(&[0.05, 0.05, 0.9]).unwrap();
867
868 let mut b = StartRungBandit::with_algorithm(50, 1.0, Algorithm::Ucb1, 1.0, 1)
869 .with_prior_strength(10.0);
870 let (choice, _) = b.choose_start_with_prior(&ctx, &ladder, &prices, Some(&hard_prior));
871 assert!(
872 choice > 0,
873 "a confident hard prior must start above rung 0 with zero observations, got {choice}"
874 );
875 }
876
877 #[test]
878 fn confident_easy_prior_starts_at_zero_with_no_observations() {
879 let ladder = vec![
880 HAIKU.to_owned(),
881 SONNET.to_owned(),
882 "anthropic/claude-opus-4-8".to_owned(),
883 ];
884 let prices = PriceTable::defaults();
885 let ctx = ctx_code();
886 let easy_prior = firstpass_core::cumulative_pass(&[0.95, 0.03, 0.02]).unwrap();
887
888 let mut b = StartRungBandit::with_algorithm(50, 1.0, Algorithm::Ucb1, 1.0, 1)
889 .with_prior_strength(10.0);
890 let (choice, _) = b.choose_start_with_prior(&ctx, &ladder, &prices, Some(&easy_prior));
891 assert_eq!(
892 choice, 0,
893 "a confident easy prior must start at rung 0 with zero observations, got {choice}"
894 );
895 }
896
897 #[test]
898 fn many_observations_override_a_wrong_prior() {
899 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
902 let prices = PriceTable::defaults();
903 let ctx = ctx_code();
904 let wrong_prior = firstpass_core::cumulative_pass(&[0.02, 0.98]).unwrap();
905
906 let mut b = StartRungBandit::with_algorithm(50, 0.0, Algorithm::Ucb1, 1.0, 1)
907 .with_prior_strength(10.0);
908 for _ in 0..200 {
909 b.observe(&ctx, 0, Verdict::Pass);
910 }
911 let (choice, _) = b.choose_start_with_prior(&ctx, &ladder, &prices, Some(&wrong_prior));
912 assert_eq!(
913 choice, 0,
914 "200 real passes at rung 0 must override a wrong low-strength prior, got {choice}"
915 );
916 }
917
918 #[test]
919 fn mismatched_prior_length_falls_back_to_propensity_method() {
920 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
921 let prices = PriceTable::defaults();
922 let ctx = ctx_code();
923 let mut a = StartRungBandit::new(50, 1.0);
924 let mut b = StartRungBandit::new(50, 1.0);
925 let bad_prior = vec![0.5]; assert_eq!(
927 a.choose_start_with_propensity(&ctx, &ladder, &prices),
928 b.choose_start_with_prior(&ctx, &ladder, &prices, Some(&bad_prior)),
929 );
930 }
931
932 #[test]
933 fn beta_sampler_mean_matches_posterior_mean() {
934 let mut b = StartRungBandit::with_algorithm(0, 1.0, Algorithm::Thompson, 1.0, 0xDEADBEEF);
935 let ctx = ctx_code();
937 for _ in 0..7 {
938 b.observe(&ctx, 0, Verdict::Pass);
939 }
940 b.observe(&ctx, 0, Verdict::Fail);
941 let mean: f64 = (0..4000).map(|_| b.thompson_pass(&ctx, 0)).sum::<f64>() / 4000.0;
943 assert!(
944 (mean - 0.8).abs() < 0.03,
945 "Beta(8,2) sample mean should be ~0.8, got {mean}"
946 );
947 let mean_u: f64 = (0..4000).map(|_| b.thompson_pass(&ctx, 5)).sum::<f64>() / 4000.0;
949 assert!(
950 (mean_u - 0.5).abs() < 0.03,
951 "uniform prior mean ~0.5, got {mean_u}"
952 );
953 }
954
955 #[test]
956 fn thompson_converges_to_skipping_a_hopeless_cheap_rung() {
957 let mut b = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 42);
958 let ctx = ctx_code();
959 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
960 let prices = PriceTable::defaults();
961 for _ in 0..80 {
963 b.observe(&ctx, 0, Verdict::Fail);
964 b.observe(&ctx, 1, Verdict::Pass);
965 }
966 let picks_rung1 = (0..100)
967 .filter(|_| b.choose_start_with_propensity(&ctx, &ladder, &prices).0 == 1)
968 .count();
969 assert!(
970 picks_rung1 >= 90,
971 "TS should overwhelmingly skip the hopeless cheap rung, picked rung 1 {picks_rung1}/100"
972 );
973 }
974
975 #[test]
976 fn discounting_adapts_after_a_distribution_flip() {
977 let ctx = ctx_code();
978 let feed = |b: &mut StartRungBandit| {
980 for _ in 0..200 {
981 b.observe(&ctx, 0, Verdict::Fail);
982 }
983 for _ in 0..40 {
984 b.observe(&ctx, 0, Verdict::Pass);
985 }
986 };
987 let mut discounted = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 0.95, 7);
988 let mut undiscounted =
989 StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 7);
990 feed(&mut discounted);
991 feed(&mut undiscounted);
992 let p_disc = discounted.pass_estimate(&ctx, 0).unwrap();
993 let p_flat = undiscounted.pass_estimate(&ctx, 0).unwrap();
994 assert!(
995 p_disc > 0.7 && p_flat < 0.25,
996 "discounted tracks the flip (got {p_disc:.2}), undiscounted lags (got {p_flat:.2})"
997 );
998 }
999
1000 #[test]
1001 fn thompson_propensity_matches_empirical_selection_frequency() {
1002 let mut b = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 99);
1003 let ctx = ctx_code();
1004 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
1005 let prices = PriceTable::defaults();
1006 for _ in 0..20 {
1008 b.observe(&ctx, 0, Verdict::Pass);
1009 b.observe(&ctx, 0, Verdict::Fail);
1010 b.observe(&ctx, 1, Verdict::Pass);
1011 }
1012 let mut freq = std::collections::HashMap::new();
1014 let mut props: Vec<(u32, f64)> = Vec::new();
1015 for _ in 0..400 {
1016 let (c, p) = b.choose_start_with_propensity(&ctx, &ladder, &prices);
1017 *freq.entry(c).or_insert(0u32) += 1;
1018 props.push((c, p.expect("thompson always logs a propensity")));
1019 }
1020 for (arm, count) in freq {
1021 let empirical = f64::from(count) / 400.0;
1022 let mean_logged: f64 = {
1023 let logged: Vec<f64> = props
1024 .iter()
1025 .filter(|(c, _)| *c == arm)
1026 .map(|(_, p)| *p)
1027 .collect();
1028 logged.iter().sum::<f64>() / logged.len() as f64
1029 };
1030 assert!(
1031 (empirical - mean_logged).abs() < 0.15,
1032 "arm {arm}: empirical {empirical:.2} vs logged propensity {mean_logged:.2}"
1033 );
1034 }
1035 }
1036
1037 #[test]
1038 fn pass_estimate_cold_and_warm() {
1039 let mut b = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 5);
1040 let ctx = ctx_code();
1041 assert!(
1042 b.pass_estimate(&ctx, 0).is_none(),
1043 "cold context: no estimate"
1044 );
1045 for _ in 0..12 {
1046 b.observe(&ctx, 0, Verdict::Pass);
1047 }
1048 let p = b.pass_estimate(&ctx, 0).expect("warm");
1049 assert!(p > 0.85, "12/12 passes: posterior mean high, got {p}");
1050 assert!(
1051 b.pass_estimate(&ctx, 3).is_none(),
1052 "unobserved arm: no estimate"
1053 );
1054 }
1055
1056 #[test]
1059 fn representative_tokens_grow_with_the_prompt_band() {
1060 let sizes: Vec<u64> = (0..5)
1061 .map(|b| {
1062 ContextBucket {
1063 task_kind: TaskKind::CodeEdit,
1064 prompt_bucket_coarse: b,
1065 }
1066 .representative_prompt_tokens()
1067 })
1068 .collect();
1069 assert!(
1070 sizes.windows(2).all(|w| w[1] > w[0]),
1071 "estimate must be strictly increasing in the band, got {sizes:?}"
1072 );
1073 let huge = ContextBucket {
1075 task_kind: TaskKind::CodeEdit,
1076 prompt_bucket_coarse: u32::MAX,
1077 };
1078 assert!(huge.representative_prompt_tokens() <= 1 << 20);
1079 }
1080
1081 #[test]
1093 fn start_rung_can_depend_on_request_size_not_just_pass_rate() {
1094 let prices = PriceTable::defaults();
1095 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
1096
1097 let mut small = StartRungBandit::new(4, 0.0);
1099 let mut large = StartRungBandit::new(4, 0.0);
1100 let c_small = ContextBucket {
1101 task_kind: TaskKind::CodeEdit,
1102 prompt_bucket_coarse: 1,
1103 };
1104 let c_large = ContextBucket {
1105 task_kind: TaskKind::CodeEdit,
1106 prompt_bucket_coarse: 8,
1107 };
1108 for _ in 0..10 {
1109 small.observe(&c_small, 0, Verdict::Fail);
1110 small.observe(&c_small, 1, Verdict::Pass);
1111 large.observe(&c_large, 0, Verdict::Fail);
1112 large.observe(&c_large, 1, Verdict::Pass);
1113 }
1114
1115 assert_eq!(small.choose_start(&c_small, &ladder, &prices), 1);
1119 assert_eq!(large.choose_start(&c_large, &ladder, &prices), 1);
1120 assert!(
1121 c_large.representative_prompt_tokens() > c_small.representative_prompt_tokens() * 8,
1122 "the large context must price the same ladder materially higher"
1123 );
1124 }
1125}