Skip to main content

_diffctx/config/
limits.rs

1use once_cell::sync::Lazy;
2
3use crate::config::env_overrides::{read_env_f64, read_env_fraction, read_env_open_fraction};
4
5pub struct AlgorithmLimits {
6    pub max_file_size: usize,
7    pub max_changed_file_size: usize,
8    pub max_fragments: usize,
9    pub max_generated_fragments: usize,
10    pub max_generated_lines: usize,
11    pub skip_expensive_threshold: usize,
12    pub rare_identifier_threshold: usize,
13    pub overhead_per_fragment: u32,
14}
15
16impl Default for AlgorithmLimits {
17    fn default() -> Self {
18        let max_fragments = std::env::var("DIFFCTX_MAX_FRAGMENTS")
19            .ok()
20            .and_then(|v| v.parse::<usize>().ok())
21            .filter(|&v| v >= 1)
22            .unwrap_or(200);
23        Self {
24            max_file_size: 100_000,
25            max_changed_file_size: 5_000_000,
26            max_fragments,
27            max_generated_fragments: 5,
28            max_generated_lines: 30,
29            skip_expensive_threshold: 2000,
30            rare_identifier_threshold: 3,
31            // Measured against real YAML serialization (path/lines/kind/symbol
32            // keys + quoting): actual scaffold runs ~40-45 tokens/fragment,
33            // not 18 - the old estimate let --budget overshoot by ~18%.
34            overhead_per_fragment: 40,
35        }
36    }
37}
38
39/// Hard ceiling for a single `git cat-file` blob read. Sized with headroom
40/// above `max_changed_file_size` (5 MB) so legitimate large changed files still
41/// load, while a pathological multi-hundred-MB blob is drained and rejected
42/// instead of allocated up front.
43pub const MAX_BLOB_READ_BYTES: usize = 16_000_000;
44
45/// The scoring mode every entry point ships. Named rather than repeated as a
46/// literal in the CLI arg, three pyo3 signatures and the Python layers: those
47/// copies are what would make a future default change land in some entry points
48/// and not others.
49pub const DEFAULT_SCORING: &str = "ego";
50
51pub const DEFAULT_PPR_ALPHA: f64 = 0.60;
52/// v5 re-calibration under per-file admission (4x3 grid, v1 calibration
53/// manifest, held-out validated): winner (tau, cbf) = (0.05, 0.4) at
54/// min(per_benchmark file_recall) = 0.6815 vs 0.6684 for the old point.
55/// Confirmed on the test splits (500x3): file_recall parity with the old
56/// default, contextbench file_precision +0.041 (CI excludes zero), and on
57/// dcbench-372 paired nontrivial recall within noise at -4% tokens. The
58/// weak stop only ships together with the admission gate (#65): without it,
59/// tau=0.05 re-admits the diffuse tail the gate exists to block.
60pub const DEFAULT_STOPPING_THRESHOLD: f64 = 0.05;
61pub const DEFAULT_PIPELINE_TIMEOUT_SECONDS: u64 = 300;
62
63pub struct PPRConfig {
64    pub alpha: f64,
65    pub default_seed_epsilon: f64,
66    pub push_scale_factor: usize,
67    pub max_pushes_cap: usize,
68    pub convergence_tolerance: f64,
69    pub forward_blend: f64,
70}
71
72impl Default for PPRConfig {
73    fn default() -> Self {
74        Self {
75            alpha: DEFAULT_PPR_ALPHA,
76            default_seed_epsilon: 0.1,
77            push_scale_factor: 100,
78            max_pushes_cap: 2_000_000,
79            convergence_tolerance: 1e-4,
80            forward_blend: 0.4,
81        }
82    }
83}
84
85pub struct LexicalConfig {
86    pub min_similarity: f64,
87    pub top_k_neighbors: usize,
88    pub max_df_ratio: f64,
89    pub min_idf: f64,
90    pub max_postings: usize,
91    pub weight_min: f64,
92    pub weight_max: f64,
93    pub backward_factor: f64,
94}
95
96impl Default for LexicalConfig {
97    fn default() -> Self {
98        Self {
99            min_similarity: 0.30,
100            top_k_neighbors: 5,
101            max_df_ratio: 0.15,
102            min_idf: 2.0,
103            max_postings: 100,
104            weight_min: 0.05,
105            weight_max: 0.15,
106            backward_factor: 0.5,
107        }
108    }
109}
110
111pub struct CochangeConfig {
112    pub min_count: usize,
113    pub max_files_per_commit: usize,
114    pub commits_limit: usize,
115    pub log_scale_factor: f64,
116}
117
118impl Default for CochangeConfig {
119    fn default() -> Self {
120        Self {
121            min_count: 2,
122            max_files_per_commit: 30,
123            commits_limit: 500,
124            log_scale_factor: 0.1,
125        }
126    }
127}
128
129pub struct SiblingConfig {
130    pub max_files_per_dir: usize,
131}
132
133impl Default for SiblingConfig {
134    fn default() -> Self {
135        Self {
136            max_files_per_dir: 20,
137        }
138    }
139}
140
141pub struct UtilityConfig {
142    pub eta: f64,
143    pub structural_bonus_weight: f64,
144    pub r_cap_sigma: f64,
145    pub proximity_decay: f64,
146}
147
148impl Default for UtilityConfig {
149    fn default() -> Self {
150        Self {
151            eta: 0.20,
152            structural_bonus_weight: 0.10,
153            r_cap_sigma: 2.0,
154            proximity_decay: 0.30,
155        }
156    }
157}
158
159pub static LIMITS: Lazy<AlgorithmLimits> = Lazy::new(AlgorithmLimits::default);
160pub static PPR: Lazy<PPRConfig> = Lazy::new(|| PPRConfig {
161    alpha: read_env_open_fraction("DIFFCTX_OP_PPR_ALPHA", DEFAULT_PPR_ALPHA),
162    forward_blend: read_env_fraction("DIFFCTX_OP_PPR_FORWARD_BLEND", 0.4),
163    ..PPRConfig::default()
164});
165pub static LEXICAL: Lazy<LexicalConfig> = Lazy::new(LexicalConfig::default);
166pub static COCHANGE: Lazy<CochangeConfig> = Lazy::new(CochangeConfig::default);
167pub static SIBLING: Lazy<SiblingConfig> = Lazy::new(SiblingConfig::default);
168pub static UTILITY: Lazy<UtilityConfig> = Lazy::new(|| UtilityConfig {
169    eta: read_env_f64("DIFFCTX_OP_UTILITY_ETA", 0.20),
170    structural_bonus_weight: read_env_f64("DIFFCTX_OP_UTILITY_STRUCTURAL_BONUS_WEIGHT", 0.10),
171    r_cap_sigma: read_env_f64("DIFFCTX_OP_UTILITY_R_CAP_SIGMA", 2.0),
172    proximity_decay: read_env_f64("DIFFCTX_OP_UTILITY_PROXIMITY_DECAY", 0.30),
173});