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 upstream_provider: String::new(),
766 };
767 trace.recompute_savings();
768 trace
769 }
770
771 #[tokio::test]
774 async fn warm_start_from_trace_store_replays_counts() {
775 let db = std::env::temp_dir().join(format!("bandit-warmstart-{}.db", uuid::Uuid::now_v7()));
776 let (tx, writer) = crate::store::open(&db).unwrap();
777
778 let mut f = Features::new(TaskKind::CodeEdit);
780 f.prompt_token_bucket = 7; for _ in 0..60 {
784 let attempts = vec![
785 stub_attempt(0, Verdict::Fail),
786 stub_attempt(1, Verdict::Pass),
787 ];
788 tx.try_send(trace_with_features_and_attempts(f.clone(), attempts))
789 .unwrap();
790 }
791 drop(tx);
792 writer.await.unwrap();
793
794 let mut bandit = StartRungBandit::new(50, 0.0); let stored = crate::store::load_all_traces(&db).unwrap();
797 assert_eq!(stored.len(), 60, "all traces must be stored");
798 for trace in &stored {
799 let ctx = ContextBucket::from_features(&trace.request.features);
800 bandit.feed_trace_attempts(&ctx, &trace.attempts);
801 }
802
803 let prices = PriceTable::defaults();
805 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
806 let ctx = ContextBucket::from_features(&f);
807 assert_eq!(
808 bandit.choose_start(&ctx, &ladder, &prices),
809 1,
810 "warm-started bandit must prefer rung 1 after 60 fail/pass pairs"
811 );
812
813 let mut f2 = Features::new(TaskKind::Chat);
815 f2.prompt_token_bucket = 7;
816 let ctx2 = ContextBucket::from_features(&f2);
817 assert_eq!(
818 bandit.choose_start(&ctx2, &ladder, &prices),
819 0,
820 "different context must be independent"
821 );
822
823 let _ = std::fs::remove_file(&db);
824 }
825
826 #[test]
829 fn prior_none_matches_choose_start_with_propensity() {
830 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
833 let prices = PriceTable::defaults();
834 let ctx = ctx_code();
835
836 let mut a = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 123);
837 let mut b = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 123);
838 for _ in 0..30 {
839 a.observe(&ctx, 0, Verdict::Fail);
840 a.observe(&ctx, 1, Verdict::Pass);
841 b.observe(&ctx, 0, Verdict::Fail);
842 b.observe(&ctx, 1, Verdict::Pass);
843 }
844
845 for _ in 0..20 {
846 let want = a.choose_start_with_propensity(&ctx, &ladder, &prices);
847 let got = b.choose_start_with_prior(&ctx, &ladder, &prices, None);
848 assert_eq!(
849 want, got,
850 "prior=None must be byte-identical to the existing method"
851 );
852 }
853 }
854
855 #[test]
856 fn confident_hard_prior_starts_above_zero_with_no_observations() {
857 let ladder = vec![
861 HAIKU.to_owned(),
862 SONNET.to_owned(),
863 "anthropic/claude-opus-4-8".to_owned(),
864 ];
865 let prices = PriceTable::defaults();
866 let ctx = ctx_code();
867 let hard_prior = firstpass_core::cumulative_pass(&[0.05, 0.05, 0.9]).unwrap();
868
869 let mut b = StartRungBandit::with_algorithm(50, 1.0, Algorithm::Ucb1, 1.0, 1)
870 .with_prior_strength(10.0);
871 let (choice, _) = b.choose_start_with_prior(&ctx, &ladder, &prices, Some(&hard_prior));
872 assert!(
873 choice > 0,
874 "a confident hard prior must start above rung 0 with zero observations, got {choice}"
875 );
876 }
877
878 #[test]
879 fn confident_easy_prior_starts_at_zero_with_no_observations() {
880 let ladder = vec![
881 HAIKU.to_owned(),
882 SONNET.to_owned(),
883 "anthropic/claude-opus-4-8".to_owned(),
884 ];
885 let prices = PriceTable::defaults();
886 let ctx = ctx_code();
887 let easy_prior = firstpass_core::cumulative_pass(&[0.95, 0.03, 0.02]).unwrap();
888
889 let mut b = StartRungBandit::with_algorithm(50, 1.0, Algorithm::Ucb1, 1.0, 1)
890 .with_prior_strength(10.0);
891 let (choice, _) = b.choose_start_with_prior(&ctx, &ladder, &prices, Some(&easy_prior));
892 assert_eq!(
893 choice, 0,
894 "a confident easy prior must start at rung 0 with zero observations, got {choice}"
895 );
896 }
897
898 #[test]
899 fn many_observations_override_a_wrong_prior() {
900 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
903 let prices = PriceTable::defaults();
904 let ctx = ctx_code();
905 let wrong_prior = firstpass_core::cumulative_pass(&[0.02, 0.98]).unwrap();
906
907 let mut b = StartRungBandit::with_algorithm(50, 0.0, Algorithm::Ucb1, 1.0, 1)
908 .with_prior_strength(10.0);
909 for _ in 0..200 {
910 b.observe(&ctx, 0, Verdict::Pass);
911 }
912 let (choice, _) = b.choose_start_with_prior(&ctx, &ladder, &prices, Some(&wrong_prior));
913 assert_eq!(
914 choice, 0,
915 "200 real passes at rung 0 must override a wrong low-strength prior, got {choice}"
916 );
917 }
918
919 #[test]
920 fn mismatched_prior_length_falls_back_to_propensity_method() {
921 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
922 let prices = PriceTable::defaults();
923 let ctx = ctx_code();
924 let mut a = StartRungBandit::new(50, 1.0);
925 let mut b = StartRungBandit::new(50, 1.0);
926 let bad_prior = vec![0.5]; assert_eq!(
928 a.choose_start_with_propensity(&ctx, &ladder, &prices),
929 b.choose_start_with_prior(&ctx, &ladder, &prices, Some(&bad_prior)),
930 );
931 }
932
933 #[test]
934 fn beta_sampler_mean_matches_posterior_mean() {
935 let mut b = StartRungBandit::with_algorithm(0, 1.0, Algorithm::Thompson, 1.0, 0xDEADBEEF);
936 let ctx = ctx_code();
938 for _ in 0..7 {
939 b.observe(&ctx, 0, Verdict::Pass);
940 }
941 b.observe(&ctx, 0, Verdict::Fail);
942 let mean: f64 = (0..4000).map(|_| b.thompson_pass(&ctx, 0)).sum::<f64>() / 4000.0;
944 assert!(
945 (mean - 0.8).abs() < 0.03,
946 "Beta(8,2) sample mean should be ~0.8, got {mean}"
947 );
948 let mean_u: f64 = (0..4000).map(|_| b.thompson_pass(&ctx, 5)).sum::<f64>() / 4000.0;
950 assert!(
951 (mean_u - 0.5).abs() < 0.03,
952 "uniform prior mean ~0.5, got {mean_u}"
953 );
954 }
955
956 #[test]
957 fn thompson_converges_to_skipping_a_hopeless_cheap_rung() {
958 let mut b = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 42);
959 let ctx = ctx_code();
960 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
961 let prices = PriceTable::defaults();
962 for _ in 0..80 {
964 b.observe(&ctx, 0, Verdict::Fail);
965 b.observe(&ctx, 1, Verdict::Pass);
966 }
967 let picks_rung1 = (0..100)
968 .filter(|_| b.choose_start_with_propensity(&ctx, &ladder, &prices).0 == 1)
969 .count();
970 assert!(
971 picks_rung1 >= 90,
972 "TS should overwhelmingly skip the hopeless cheap rung, picked rung 1 {picks_rung1}/100"
973 );
974 }
975
976 #[test]
977 fn discounting_adapts_after_a_distribution_flip() {
978 let ctx = ctx_code();
979 let feed = |b: &mut StartRungBandit| {
981 for _ in 0..200 {
982 b.observe(&ctx, 0, Verdict::Fail);
983 }
984 for _ in 0..40 {
985 b.observe(&ctx, 0, Verdict::Pass);
986 }
987 };
988 let mut discounted = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 0.95, 7);
989 let mut undiscounted =
990 StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 7);
991 feed(&mut discounted);
992 feed(&mut undiscounted);
993 let p_disc = discounted.pass_estimate(&ctx, 0).unwrap();
994 let p_flat = undiscounted.pass_estimate(&ctx, 0).unwrap();
995 assert!(
996 p_disc > 0.7 && p_flat < 0.25,
997 "discounted tracks the flip (got {p_disc:.2}), undiscounted lags (got {p_flat:.2})"
998 );
999 }
1000
1001 #[test]
1002 fn thompson_propensity_matches_empirical_selection_frequency() {
1003 let mut b = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 99);
1004 let ctx = ctx_code();
1005 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
1006 let prices = PriceTable::defaults();
1007 for _ in 0..20 {
1009 b.observe(&ctx, 0, Verdict::Pass);
1010 b.observe(&ctx, 0, Verdict::Fail);
1011 b.observe(&ctx, 1, Verdict::Pass);
1012 }
1013 let mut freq = std::collections::HashMap::new();
1015 let mut props: Vec<(u32, f64)> = Vec::new();
1016 for _ in 0..400 {
1017 let (c, p) = b.choose_start_with_propensity(&ctx, &ladder, &prices);
1018 *freq.entry(c).or_insert(0u32) += 1;
1019 props.push((c, p.expect("thompson always logs a propensity")));
1020 }
1021 for (arm, count) in freq {
1022 let empirical = f64::from(count) / 400.0;
1023 let mean_logged: f64 = {
1024 let logged: Vec<f64> = props
1025 .iter()
1026 .filter(|(c, _)| *c == arm)
1027 .map(|(_, p)| *p)
1028 .collect();
1029 logged.iter().sum::<f64>() / logged.len() as f64
1030 };
1031 assert!(
1032 (empirical - mean_logged).abs() < 0.15,
1033 "arm {arm}: empirical {empirical:.2} vs logged propensity {mean_logged:.2}"
1034 );
1035 }
1036 }
1037
1038 #[test]
1039 fn pass_estimate_cold_and_warm() {
1040 let mut b = StartRungBandit::with_algorithm(10, 1.0, Algorithm::Thompson, 1.0, 5);
1041 let ctx = ctx_code();
1042 assert!(
1043 b.pass_estimate(&ctx, 0).is_none(),
1044 "cold context: no estimate"
1045 );
1046 for _ in 0..12 {
1047 b.observe(&ctx, 0, Verdict::Pass);
1048 }
1049 let p = b.pass_estimate(&ctx, 0).expect("warm");
1050 assert!(p > 0.85, "12/12 passes: posterior mean high, got {p}");
1051 assert!(
1052 b.pass_estimate(&ctx, 3).is_none(),
1053 "unobserved arm: no estimate"
1054 );
1055 }
1056
1057 #[test]
1060 fn representative_tokens_grow_with_the_prompt_band() {
1061 let sizes: Vec<u64> = (0..5)
1062 .map(|b| {
1063 ContextBucket {
1064 task_kind: TaskKind::CodeEdit,
1065 prompt_bucket_coarse: b,
1066 }
1067 .representative_prompt_tokens()
1068 })
1069 .collect();
1070 assert!(
1071 sizes.windows(2).all(|w| w[1] > w[0]),
1072 "estimate must be strictly increasing in the band, got {sizes:?}"
1073 );
1074 let huge = ContextBucket {
1076 task_kind: TaskKind::CodeEdit,
1077 prompt_bucket_coarse: u32::MAX,
1078 };
1079 assert!(huge.representative_prompt_tokens() <= 1 << 20);
1080 }
1081
1082 #[test]
1094 fn start_rung_can_depend_on_request_size_not_just_pass_rate() {
1095 let prices = PriceTable::defaults();
1096 let ladder = vec![HAIKU.to_owned(), SONNET.to_owned()];
1097
1098 let mut small = StartRungBandit::new(4, 0.0);
1100 let mut large = StartRungBandit::new(4, 0.0);
1101 let c_small = ContextBucket {
1102 task_kind: TaskKind::CodeEdit,
1103 prompt_bucket_coarse: 1,
1104 };
1105 let c_large = ContextBucket {
1106 task_kind: TaskKind::CodeEdit,
1107 prompt_bucket_coarse: 8,
1108 };
1109 for _ in 0..10 {
1110 small.observe(&c_small, 0, Verdict::Fail);
1111 small.observe(&c_small, 1, Verdict::Pass);
1112 large.observe(&c_large, 0, Verdict::Fail);
1113 large.observe(&c_large, 1, Verdict::Pass);
1114 }
1115
1116 assert_eq!(small.choose_start(&c_small, &ladder, &prices), 1);
1120 assert_eq!(large.choose_start(&c_large, &ladder, &prices), 1);
1121 assert!(
1122 c_large.representative_prompt_tokens() > c_small.representative_prompt_tokens() * 8,
1123 "the large context must price the same ladder materially higher"
1124 );
1125 }
1126}