Skip to main content

_diffctx/config/
selection.rs

1use crate::config::env_overrides::{read_env_fraction, read_env_u32};
2
3// v5 re-calibration under per-file admission: validated winner
4// (tau, cbf) = (0.05, 0.4); the tau half lives in limits.rs. Named (and
5// exported through pybridge) so the Python layers read it instead of
6// restating it — the same drift class as #175's tau copies.
7pub const DEFAULT_CORE_BUDGET_FRACTION: f64 = 0.4;
8
9#[derive(Clone)]
10pub struct SelectionConfig {
11    pub core_budget_fraction: f64,
12    pub r_cap_min: f64,
13    /// Share of the run budget one file may consume while other files still
14    /// have unplaced candidates (#194: a single +9k-line data blob monopolized
15    /// 364 of 370 output sections). The ceiling binds only in the first
16    /// placement phase — leftovers no other file claims flow back to whoever
17    /// wants them, so a single-file change is unaffected.
18    pub per_file_budget_fraction: f64,
19}
20
21impl Default for SelectionConfig {
22    fn default() -> Self {
23        Self {
24            // cbf margin over 0.5 is small but consistent in direction on
25            // both the calibration and held-out manifests. Surface flat and
26            // monotone, as in v4.
27            core_budget_fraction: DEFAULT_CORE_BUDGET_FRACTION,
28            r_cap_min: 0.01,
29            per_file_budget_fraction: 0.25,
30        }
31    }
32}
33
34#[derive(Clone)]
35pub struct RescueConfig {
36    pub budget_fraction: f64,
37    pub min_score_percentile: f64,
38}
39
40impl Default for RescueConfig {
41    fn default() -> Self {
42        Self {
43            budget_fraction: 0.05,
44            min_score_percentile: 0.80,
45        }
46    }
47}
48
49#[derive(Clone)]
50pub struct BoltzmannConfig {
51    pub beta_lo: f64,
52    pub beta_hi: f64,
53    pub bisect_iters: u32,
54    pub calibration_tolerance: f64,
55}
56
57impl Default for BoltzmannConfig {
58    fn default() -> Self {
59        Self {
60            beta_lo: 1e-6,
61            beta_hi: 1.0,
62            bisect_iters: 24,
63            calibration_tolerance: 0.05,
64        }
65    }
66}
67
68pub fn selection() -> SelectionConfig {
69    SelectionConfig {
70        core_budget_fraction: read_env_fraction(
71            "DIFFCTX_OP_SELECTION_CORE_BUDGET_FRACTION",
72            DEFAULT_CORE_BUDGET_FRACTION,
73        ),
74        r_cap_min: read_env_fraction("DIFFCTX_OP_SELECTION_R_CAP_MIN", 0.01),
75        per_file_budget_fraction: read_env_fraction(
76            "DIFFCTX_OP_SELECTION_PER_FILE_BUDGET_FRACTION",
77            0.25,
78        ),
79    }
80}
81
82pub fn rescue() -> RescueConfig {
83    RescueConfig {
84        budget_fraction: read_env_fraction("DIFFCTX_OP_RESCUE_BUDGET_FRACTION", 0.05),
85        min_score_percentile: read_env_fraction("DIFFCTX_OP_RESCUE_MIN_SCORE_PERCENTILE", 0.80),
86    }
87}
88
89pub fn boltzmann() -> BoltzmannConfig {
90    BoltzmannConfig {
91        calibration_tolerance: read_env_fraction(
92            "DIFFCTX_OP_BOLTZMANN_CALIBRATION_TOLERANCE",
93            0.05,
94        ),
95        bisect_iters: read_env_u32("DIFFCTX_OP_BOLTZMANN_BISECT_ITERS", 24).max(1),
96        ..BoltzmannConfig::default()
97    }
98}