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/// Confirmed by the v4 re-calibration on the EGO pipeline (5x3 grid,
53/// v1 calibration manifest): validated winner (tau, cbf) = (0.12, 0.5)
54/// at min(per_benchmark file_recall) = 0.6532, validation 0.6604.
55/// Surface flat and monotone (top-3 within 0.004) — choice is robust.
56pub const DEFAULT_STOPPING_THRESHOLD: f64 = 0.12;
57pub const DEFAULT_PIPELINE_TIMEOUT_SECONDS: u64 = 300;
58
59pub struct PPRConfig {
60    pub alpha: f64,
61    pub default_seed_epsilon: f64,
62    pub push_scale_factor: usize,
63    pub max_pushes_cap: usize,
64    pub convergence_tolerance: f64,
65    pub forward_blend: f64,
66}
67
68impl Default for PPRConfig {
69    fn default() -> Self {
70        Self {
71            alpha: DEFAULT_PPR_ALPHA,
72            default_seed_epsilon: 0.1,
73            push_scale_factor: 100,
74            max_pushes_cap: 2_000_000,
75            convergence_tolerance: 1e-4,
76            forward_blend: 0.4,
77        }
78    }
79}
80
81pub struct LexicalConfig {
82    pub min_similarity: f64,
83    pub top_k_neighbors: usize,
84    pub max_df_ratio: f64,
85    pub min_idf: f64,
86    pub max_postings: usize,
87    pub weight_min: f64,
88    pub weight_max: f64,
89    pub backward_factor: f64,
90}
91
92impl Default for LexicalConfig {
93    fn default() -> Self {
94        Self {
95            min_similarity: 0.30,
96            top_k_neighbors: 5,
97            max_df_ratio: 0.15,
98            min_idf: 2.0,
99            max_postings: 100,
100            weight_min: 0.05,
101            weight_max: 0.15,
102            backward_factor: 0.5,
103        }
104    }
105}
106
107pub struct CochangeConfig {
108    pub min_count: usize,
109    pub max_files_per_commit: usize,
110    pub commits_limit: usize,
111    pub log_scale_factor: f64,
112}
113
114impl Default for CochangeConfig {
115    fn default() -> Self {
116        Self {
117            min_count: 2,
118            max_files_per_commit: 30,
119            commits_limit: 500,
120            log_scale_factor: 0.1,
121        }
122    }
123}
124
125pub struct SiblingConfig {
126    pub max_files_per_dir: usize,
127}
128
129impl Default for SiblingConfig {
130    fn default() -> Self {
131        Self {
132            max_files_per_dir: 20,
133        }
134    }
135}
136
137pub struct UtilityConfig {
138    pub eta: f64,
139    pub structural_bonus_weight: f64,
140    pub r_cap_sigma: f64,
141    pub proximity_decay: f64,
142}
143
144impl Default for UtilityConfig {
145    fn default() -> Self {
146        Self {
147            eta: 0.20,
148            structural_bonus_weight: 0.10,
149            r_cap_sigma: 2.0,
150            proximity_decay: 0.30,
151        }
152    }
153}
154
155pub static LIMITS: Lazy<AlgorithmLimits> = Lazy::new(AlgorithmLimits::default);
156pub static PPR: Lazy<PPRConfig> = Lazy::new(|| PPRConfig {
157    alpha: read_env_open_fraction("DIFFCTX_OP_PPR_ALPHA", DEFAULT_PPR_ALPHA),
158    forward_blend: read_env_fraction("DIFFCTX_OP_PPR_FORWARD_BLEND", 0.4),
159    ..PPRConfig::default()
160});
161pub static LEXICAL: Lazy<LexicalConfig> = Lazy::new(LexicalConfig::default);
162pub static COCHANGE: Lazy<CochangeConfig> = Lazy::new(CochangeConfig::default);
163pub static SIBLING: Lazy<SiblingConfig> = Lazy::new(SiblingConfig::default);
164pub static UTILITY: Lazy<UtilityConfig> = Lazy::new(|| UtilityConfig {
165    eta: read_env_f64("DIFFCTX_OP_UTILITY_ETA", 0.20),
166    structural_bonus_weight: read_env_f64("DIFFCTX_OP_UTILITY_STRUCTURAL_BONUS_WEIGHT", 0.10),
167    r_cap_sigma: read_env_f64("DIFFCTX_OP_UTILITY_R_CAP_SIGMA", 2.0),
168    proximity_decay: read_env_f64("DIFFCTX_OP_UTILITY_PROXIMITY_DECAY", 0.30),
169});