cxpak 1.0.1

Spends CPU cycles so you don't spend tokens. The LLM gets a briefing packet instead of a flashlight in a dark room.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
pub mod seed;
pub mod signals;

use crate::index::CodebaseIndex;
use std::collections::HashSet;

/// Result of scoring a single file against a query.
#[derive(Debug, Clone)]
pub struct ScoredFile {
    pub path: String,
    pub score: f64,
    pub signals: Vec<SignalResult>,
    pub token_count: usize,
}

/// Breakdown of a single signal's contribution.
#[derive(Debug, Clone)]
pub struct SignalResult {
    pub name: &'static str,
    pub score: f64,
    pub detail: String,
}

/// Trait for scoring file relevance against a query.
pub trait RelevanceScorer: Send + Sync {
    fn score(&self, query: &str, file_path: &str, index: &CodebaseIndex) -> ScoredFile;
}

/// Combines multiple weighted signals into a single score.
pub struct MultiSignalScorer {
    pub weights: SignalWeights,
    pub expanded_tokens: Option<HashSet<String>>,
}

#[derive(Debug, Clone)]
pub struct SignalWeights {
    pub path_similarity: f64,
    pub symbol_match: f64,
    pub import_proximity: f64,
    pub term_frequency: f64,
    pub recency_boost: f64,
    pub pagerank: f64,
    /// Always present; value is 0.0 when embeddings are inactive.
    pub embedding_similarity: f64,
}

impl Default for SignalWeights {
    fn default() -> Self {
        Self::without_embeddings()
    }
}

impl SignalWeights {
    /// Weights used when an embedding index is available (7 active signals, sum = 1.0).
    pub fn with_embeddings() -> Self {
        Self {
            path_similarity: 0.15,
            symbol_match: 0.27,
            import_proximity: 0.12,
            term_frequency: 0.16,
            recency_boost: 0.00,
            pagerank: 0.15,
            embedding_similarity: 0.15,
        }
    }

    /// Weights used when no embedding index is present (matches v0.13.0, sum = 1.0).
    pub fn without_embeddings() -> Self {
        Self {
            path_similarity: 0.18,
            symbol_match: 0.32,
            import_proximity: 0.14,
            term_frequency: 0.19,
            recency_boost: 0.00, // no git history in index yet; weight redistributed
            pagerank: 0.17,
            embedding_similarity: 0.00,
        }
    }
}

impl Default for MultiSignalScorer {
    fn default() -> Self {
        Self::new()
    }
}

impl MultiSignalScorer {
    pub fn new() -> Self {
        Self {
            weights: SignalWeights::default(),
            expanded_tokens: None,
        }
    }

    /// Select weights based on whether the index has an embedding index.
    pub fn new_for_index(index: &CodebaseIndex) -> Self {
        let weights = if index.has_embedding_index() {
            SignalWeights::with_embeddings()
        } else {
            SignalWeights::without_embeddings()
        };
        Self {
            weights,
            expanded_tokens: None,
        }
    }

    pub fn with_weights(weights: SignalWeights) -> Self {
        Self {
            weights,
            expanded_tokens: None,
        }
    }

    pub fn with_expansion(mut self, tokens: HashSet<String>) -> Self {
        self.expanded_tokens = Some(tokens);
        self
    }

    /// Score all files in the index against the query.
    pub fn score_all(&self, query: &str, index: &CodebaseIndex) -> Vec<ScoredFile> {
        index
            .files
            .iter()
            .map(|f| self.score(query, &f.relative_path, index))
            .collect()
    }
}

impl RelevanceScorer for MultiSignalScorer {
    fn score(&self, query: &str, file_path: &str, index: &CodebaseIndex) -> ScoredFile {
        let w = &self.weights;
        let expanded = self.expanded_tokens.as_ref();

        let path_sig = signals::path_similarity(query, file_path);
        let symbol_sig = signals::symbol_match(query, file_path, index, expanded);
        let import_sig = signals::import_proximity(file_path, index);
        let tf_sig = signals::term_frequency(query, file_path, index, expanded);
        let recency_sig = SignalResult {
            name: "recency_boost",
            score: 0.5, // neutral — no git history in index
            detail: "no git history available".to_string(),
        };
        let pr_sig = signals::pagerank_signal(file_path, &index.pagerank);

        // Signal 7: embedding similarity (feature-gated, value 0.0 when inactive).
        #[cfg(feature = "embeddings")]
        let emb_sig = signals::embedding_similarity_signal(query, file_path, index);
        #[cfg(not(feature = "embeddings"))]
        let emb_sig = SignalResult {
            name: "embedding_similarity",
            score: 0.0,
            detail: "embeddings feature not enabled".to_string(),
        };

        let combined = w.path_similarity * path_sig.score
            + w.symbol_match * symbol_sig.score
            + w.import_proximity * import_sig.score
            + w.term_frequency * tf_sig.score
            + w.recency_boost * recency_sig.score
            + w.pagerank * pr_sig.score
            + w.embedding_similarity * emb_sig.score;

        // Clamp to 0.0–1.0
        let score = combined.clamp(0.0, 1.0);

        let token_count = index
            .files
            .iter()
            .find(|f| f.relative_path == file_path)
            .map(|f| f.token_count)
            .unwrap_or(0);

        ScoredFile {
            path: file_path.to_string(),
            score,
            signals: vec![
                path_sig,
                symbol_sig,
                import_sig,
                tf_sig,
                recency_sig,
                pr_sig,
                emb_sig,
            ],
            token_count,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::budget::counter::TokenCounter;
    use crate::parser::language::{ParseResult, Symbol, SymbolKind, Visibility};
    use crate::scanner::ScannedFile;
    use std::collections::HashMap;

    fn make_test_index() -> CodebaseIndex {
        let counter = TokenCounter::new();
        let dir = tempfile::TempDir::new().unwrap();
        let fp1 = dir.path().join("src/api/mod.rs");
        let fp2 = dir.path().join("src/api/middleware.rs");
        let fp3 = dir.path().join("src/config.rs");
        std::fs::create_dir_all(dir.path().join("src/api")).unwrap();
        std::fs::write(&fp1, "pub fn handle_request() { rate_limit(); }").unwrap();
        std::fs::write(&fp2, "pub fn rate_limit() {}").unwrap();
        std::fs::write(&fp3, "pub struct Config {}").unwrap();

        let files = vec![
            ScannedFile {
                relative_path: "src/api/mod.rs".into(),
                absolute_path: fp1,
                language: Some("rust".into()),
                size_bytes: 42,
            },
            ScannedFile {
                relative_path: "src/api/middleware.rs".into(),
                absolute_path: fp2,
                language: Some("rust".into()),
                size_bytes: 22,
            },
            ScannedFile {
                relative_path: "src/config.rs".into(),
                absolute_path: fp3,
                language: Some("rust".into()),
                size_bytes: 22,
            },
        ];

        let mut parse_results = HashMap::new();
        parse_results.insert(
            "src/api/mod.rs".to_string(),
            ParseResult {
                symbols: vec![Symbol {
                    name: "handle_request".into(),
                    kind: SymbolKind::Function,
                    visibility: Visibility::Public,
                    signature: "pub fn handle_request()".into(),
                    body: "{ rate_limit(); }".into(),
                    start_line: 1,
                    end_line: 1,
                }],
                imports: vec![],
                exports: vec![],
            },
        );
        parse_results.insert(
            "src/api/middleware.rs".to_string(),
            ParseResult {
                symbols: vec![Symbol {
                    name: "rate_limit".into(),
                    kind: SymbolKind::Function,
                    visibility: Visibility::Public,
                    signature: "pub fn rate_limit()".into(),
                    body: "{}".into(),
                    start_line: 1,
                    end_line: 1,
                }],
                imports: vec![],
                exports: vec![],
            },
        );

        CodebaseIndex::build(files, parse_results, &counter)
    }

    #[test]
    fn test_multi_signal_scorer_returns_scores() {
        let index = make_test_index();
        let scorer = MultiSignalScorer::new();
        let result = scorer.score("api request handler", "src/api/mod.rs", &index);
        assert!(result.score >= 0.0 && result.score <= 1.0);
        assert_eq!(result.signals.len(), 7);
        assert_eq!(result.path, "src/api/mod.rs");
    }

    #[test]
    fn test_score_all_returns_all_files() {
        let index = make_test_index();
        let scorer = MultiSignalScorer::new();
        let results = scorer.score_all("rate limit", &index);
        assert_eq!(results.len(), 3);
    }

    #[test]
    fn test_relevant_file_scores_higher() {
        let index = make_test_index();
        let scorer = MultiSignalScorer::new();
        let api_score = scorer.score("api request", "src/api/mod.rs", &index);
        let config_score = scorer.score("api request", "src/config.rs", &index);
        assert!(
            api_score.score > config_score.score,
            "api/mod.rs ({}) should score higher than config.rs ({}) for 'api request'",
            api_score.score,
            config_score.score
        );
    }

    #[test]
    fn test_weights_sum_to_one() {
        let w = SignalWeights::default();
        let sum = w.path_similarity
            + w.symbol_match
            + w.import_proximity
            + w.term_frequency
            + w.recency_boost
            + w.pagerank
            + w.embedding_similarity;
        assert!(
            (sum - 1.0).abs() < 0.001,
            "Weights should sum to 1.0, got {sum}"
        );
    }

    #[test]
    fn weights_without_embeddings_sum_to_one() {
        let w = SignalWeights::without_embeddings();
        let sum = w.path_similarity
            + w.symbol_match
            + w.import_proximity
            + w.term_frequency
            + w.recency_boost
            + w.pagerank
            + w.embedding_similarity;
        assert!(
            (sum - 1.0).abs() < 0.001,
            "without_embeddings weights should sum to 1.0, got {sum}"
        );
        assert_eq!(
            w.embedding_similarity, 0.0,
            "embedding_similarity must be 0.0 when inactive"
        );
    }

    #[test]
    fn weights_with_embeddings_sum_to_one() {
        let w = SignalWeights::with_embeddings();
        let sum = w.path_similarity
            + w.symbol_match
            + w.import_proximity
            + w.term_frequency
            + w.recency_boost
            + w.pagerank
            + w.embedding_similarity;
        assert!(
            (sum - 1.0).abs() < 0.001,
            "with_embeddings weights should sum to 1.0, got {sum}"
        );
        assert!(
            w.embedding_similarity > 0.0,
            "embedding_similarity must be positive when active"
        );
    }

    #[test]
    fn scorer_selects_correct_weights() {
        let index = make_test_index();
        let scorer = MultiSignalScorer::new_for_index(&index);
        // Index was built without embeddings (local provider would fail in tests),
        // so we expect without_embeddings weights.
        assert_eq!(
            scorer.weights.embedding_similarity, 0.0,
            "no embedding index => embedding_similarity weight must be 0.0"
        );
        // Ensure the total still sums to 1.0.
        let w = &scorer.weights;
        let sum = w.path_similarity
            + w.symbol_match
            + w.import_proximity
            + w.term_frequency
            + w.recency_boost
            + w.pagerank
            + w.embedding_similarity;
        assert!(
            (sum - 1.0).abs() < 0.001,
            "scorer weights should sum to 1.0, got {sum}"
        );
    }

    #[test]
    fn test_custom_weights() {
        let index = make_test_index();
        let scorer = MultiSignalScorer::with_weights(SignalWeights {
            path_similarity: 1.0,
            symbol_match: 0.0,
            import_proximity: 0.0,
            term_frequency: 0.0,
            recency_boost: 0.0,
            pagerank: 0.0,
            embedding_similarity: 0.0,
        });
        let result = scorer.score("api", "src/api/mod.rs", &index);
        // Only path_similarity contributes
        assert!(result.score > 0.0);
    }

    #[test]
    fn test_score_nonexistent_file() {
        let index = make_test_index();
        let scorer = MultiSignalScorer::new();
        let result = scorer.score("test", "nonexistent.rs", &index);
        assert_eq!(result.token_count, 0);
        // Should still return a valid score (likely low)
        assert!(result.score >= 0.0 && result.score <= 1.0);
    }

    #[test]
    fn test_all_zero_query() {
        let index = make_test_index();
        let scorer = MultiSignalScorer::new();
        let result = scorer.score("xyznonexistent", "src/config.rs", &index);
        // Should be low but valid
        assert!(result.score >= 0.0 && result.score <= 1.0);
    }
}