ripmap 0.1.0

Ultra-fast codebase cartography for LLMs
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//! Contextual boost calculation for ripmap.
//!
//! This module applies multiplicative boosts to base PageRank scores based on
//! various contextual signals:
//!
//! - **Mentioned identifiers**: Symbols explicitly referenced in queries/chat (10x)
//! - **Mentioned files**: Files explicitly named in context (5x)
//! - **Chat files**: Files being actively edited (20x)
//! - **Temporal coupling**: Files that co-change with chat files (3x)
//! - **Focus expansion**: Graph neighbors of focus symbols (5x)
//!
//! The final rank combines all signals multiplicatively:
//! ```text
//! final_rank = base_rank × boost × git_weight × caller_weight
//! ```
//!
//! This creates a powerful ranking signal that elevates contextually relevant
//! symbols to the top while still respecting structural importance (PageRank).

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use crate::types::{RankedTag, RankingConfig, Tag, TagKind};

/// Calculator for applying contextual boosts to symbol ranks.
///
/// Takes base PageRank scores and amplifies them based on contextual signals
/// like chat files, mentions, temporal coupling, and graph expansion.
///
/// The boost system is multiplicative: multiple signals stack to create
/// very high boosts for highly relevant symbols.
pub struct BoostCalculator {
    config: RankingConfig,
}

impl BoostCalculator {
    /// Create a new boost calculator with the given configuration.
    pub fn new(config: RankingConfig) -> Self {
        Self { config }
    }

    /// Apply boosts to tags based on contextual signals.
    ///
    /// Produces RankedTag instances with final scores computed as:
    /// ```text
    /// final_rank = base_rank × boost × git_weight × caller_weight
    /// ```
    ///
    /// # Arguments
    ///
    /// * `tags_by_file` - Map from absolute file path to its tags
    /// * `file_ranks` - PageRank scores for files (keyed by relative path)
    /// * `symbol_ranks` - Optional PageRank scores for symbols (keyed by (file, name))
    /// * `chat_fnames` - Files being actively edited (absolute paths)
    /// * `mentioned_fnames` - Files explicitly mentioned in query (relative paths)
    /// * `mentioned_idents` - Symbols explicitly mentioned in query
    /// * `temporal_boost_files` - Files that co-change with chat files (relative paths)
    /// * `git_weights` - Recency/churn-based git weights (keyed by relative path)
    /// * `caller_weights` - Reverse edge bias weights for debugging intent
    /// * `focus_expansion_weights` - Graph neighbor expansion weights for (file, symbol)
    ///
    /// # Returns
    ///
    /// Vector of RankedTag sorted by final rank (descending - highest first).
    /// Only includes definition tags (TagKind::Def).
    pub fn apply_boosts(
        &self,
        tags_by_file: &HashMap<String, Vec<Tag>>,
        file_ranks: &HashMap<String, f64>,
        symbol_ranks: Option<&HashMap<(Arc<str>, Arc<str>), f64>>,
        chat_fnames: &HashSet<String>,
        mentioned_fnames: &HashSet<String>,
        mentioned_idents: &HashSet<String>,
        temporal_boost_files: &HashSet<String>,
        git_weights: Option<&HashMap<String, f64>>,
        caller_weights: Option<&HashMap<String, f64>>,
        focus_expansion_weights: Option<&HashMap<(Arc<str>, Arc<str>), f64>>,
    ) -> Vec<RankedTag> {
        let mut result = Vec::new();

        // Convert chat_fnames (absolute paths) to relative for comparison
        let chat_rel_fnames: HashSet<String> =
            chat_fnames.iter().map(|f| extract_rel_fname(f)).collect();

        for (fname, tags) in tags_by_file {
            let rel_fname = extract_rel_fname(fname);

            // Get file-level rank and weights
            let file_rank = file_ranks.get(&rel_fname).copied().unwrap_or(0.0);
            let git_weight = git_weights
                .and_then(|w| w.get(&rel_fname))
                .copied()
                .unwrap_or(1.0);
            // Caller weight with hub damping: balance between "called = important"
            // and "utility function = noise".
            //
            // The interplay between boost_caller_weight and hub_damping:
            //   - boost_caller_weight amplifies the caller signal
            //   - hub_damping counteracts it to penalize "hub" nodes
            //
            // Effective formula:
            //   effective_boost = boost_caller_weight * (1.0 - hub_damping)
            //
            // With hub_damping = 0.0: full boost (called functions are important)
            // With hub_damping = 1.0: boost neutralized (caller count ignored)
            // With hub_damping > 1.0: penalty (hubs are downranked)
            let raw_caller_weight = caller_weights
                .and_then(|w| w.get(&rel_fname))
                .copied()
                .unwrap_or(1.0);

            // Apply hub damping: reduce or invert the caller weight effect
            let effective_boost = self.config.boost_caller_weight * (1.0 - self.config.hub_damping);
            let caller_weight = (1.0 + (raw_caller_weight - 1.0) * effective_boost).max(0.01);

            // Process only definition tags
            for tag in tags.iter().filter(|t| t.kind == TagKind::Def) {
                // Determine base rank: use symbol rank if available, else file rank
                let base_rank = symbol_ranks
                    .and_then(|sr| {
                        let key = (Arc::clone(&tag.rel_fname), Arc::clone(&tag.name));
                        sr.get(&key).copied()
                    })
                    .unwrap_or(file_rank);

                // Calculate contextual boost by multiplying all applicable signals
                let mut boost = 1.0;

                // Boost 1: Mentioned identifier (10x) - symbol explicitly in query
                if mentioned_idents.contains(tag.name.as_ref()) {
                    boost *= self.config.boost_mentioned_ident;
                }

                // Boost 2: Mentioned file (5x) - file explicitly in query
                if mentioned_fnames.contains(&rel_fname) {
                    boost *= self.config.boost_mentioned_file;
                }

                // Boost 3: Chat file (20x) - file being actively edited
                if chat_rel_fnames.contains(&rel_fname) {
                    boost *= self.config.boost_chat_file;
                }

                // Boost 4: Temporal coupling (3x) - co-changes with chat files
                if temporal_boost_files.contains(&rel_fname) {
                    boost *= self.config.boost_temporal_coupling;
                }

                // Boost 5: Focus expansion (5x × expansion_weight) - graph neighbor
                if let Some(expansion_weights) = focus_expansion_weights {
                    let key = (Arc::clone(&tag.rel_fname), Arc::clone(&tag.name));
                    if let Some(&expansion_weight) = expansion_weights.get(&key) {
                        boost *= self.config.boost_focus_expansion * expansion_weight;
                    }
                }

                // Compute final rank: base × boost × git × caller
                let final_rank = base_rank * boost * git_weight * caller_weight;

                result.push(RankedTag::new(final_rank, tag.clone()));
            }
        }

        // Sort by rank descending (highest first)
        result.sort();

        result
    }
}

/// Extract relative filename from absolute path.
///
/// Simplified heuristic: strips leading slash. In production, this would use
/// proper path resolution relative to repo root.
fn extract_rel_fname(abs_fname: &str) -> String {
    abs_fname.strip_prefix('/').unwrap_or(abs_fname).to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::TagKind;

    /// Helper to create a test tag
    fn make_tag(rel_fname: &str, name: &str, kind: TagKind) -> Tag {
        Tag {
            rel_fname: Arc::from(rel_fname),
            fname: Arc::from(format!("/{}", rel_fname)),
            line: 1,
            name: Arc::from(name),
            kind,
            node_type: Arc::from("function"),
            parent_name: None,
            parent_line: None,
            signature: None,
            fields: None,
            metadata: None,
        }
    }

    #[test]
    fn test_no_boosts() {
        // Without any contextual signals, should just use base ranks
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config);

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![make_tag("a.rs", "foo", TagKind::Def)],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 0.5);

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            None,
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            None,
            None,
            None,
        );

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].rank, 0.5); // base_rank × 1.0 (no boosts)
        assert_eq!(result[0].tag.name.as_ref(), "foo");
    }

    #[test]
    fn test_mentioned_ident_boost() {
        // Mentioned identifier should get 10x boost
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config.clone());

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![make_tag("a.rs", "foo", TagKind::Def)],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 0.1);

        let mut mentioned_idents = HashSet::new();
        mentioned_idents.insert("foo".to_string());

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            None,
            &HashSet::new(),
            &HashSet::new(),
            &mentioned_idents,
            &HashSet::new(),
            None,
            None,
            None,
        );

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].rank, 0.1 * config.boost_mentioned_ident); // 0.1 × 10 = 1.0
    }

    #[test]
    fn test_mentioned_file_boost() {
        // Mentioned file should get 5x boost
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config.clone());

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![make_tag("a.rs", "foo", TagKind::Def)],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 0.2);

        let mut mentioned_fnames = HashSet::new();
        mentioned_fnames.insert("a.rs".to_string());

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            None,
            &HashSet::new(),
            &mentioned_fnames,
            &HashSet::new(),
            &HashSet::new(),
            None,
            None,
            None,
        );

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].rank, 0.2 * config.boost_mentioned_file); // 0.2 × 5 = 1.0
    }

    #[test]
    fn test_chat_file_boost() {
        // Chat file should get 20x boost
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config.clone());

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![make_tag("a.rs", "foo", TagKind::Def)],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 0.05);

        let mut chat_fnames = HashSet::new();
        chat_fnames.insert("/a.rs".to_string()); // Absolute path

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            None,
            &chat_fnames,
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            None,
            None,
            None,
        );

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].rank, 0.05 * config.boost_chat_file); // 0.05 × 20 = 1.0
    }

    #[test]
    fn test_temporal_coupling_boost() {
        // Temporal coupling should get 3x boost
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config.clone());

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![make_tag("a.rs", "foo", TagKind::Def)],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 0.5);

        let mut temporal_boost_files = HashSet::new();
        temporal_boost_files.insert("a.rs".to_string());

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            None,
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &temporal_boost_files,
            None,
            None,
            None,
        );

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].rank, 0.5 * config.boost_temporal_coupling); // 0.5 × 3 = 1.5
    }

    #[test]
    fn test_multiple_boosts_multiply() {
        // Multiple boosts should stack multiplicatively
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config.clone());

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![make_tag("a.rs", "foo", TagKind::Def)],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 0.01);

        let mut mentioned_idents = HashSet::new();
        mentioned_idents.insert("foo".to_string());

        let mut chat_fnames = HashSet::new();
        chat_fnames.insert("/a.rs".to_string());

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            None,
            &chat_fnames,
            &HashSet::new(),
            &mentioned_idents,
            &HashSet::new(),
            None,
            None,
            None,
        );

        assert_eq!(result.len(), 1);
        // 0.01 × 10 (mentioned_ident) × 20 (chat_file) = 2.0
        assert_eq!(
            result[0].rank,
            0.01 * config.boost_mentioned_ident * config.boost_chat_file
        );
    }

    #[test]
    fn test_git_weight_multiplier() {
        // Git weight should multiply into final rank
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config);

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![make_tag("a.rs", "foo", TagKind::Def)],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 1.0);

        let mut git_weights = HashMap::new();
        git_weights.insert("a.rs".to_string(), 2.0);

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            None,
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            Some(&git_weights),
            None,
            None,
        );

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].rank, 1.0 * 2.0); // base × git_weight
    }

    #[test]
    fn test_caller_weight_multiplier() {
        // Caller weight should multiply into final rank
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config);

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![make_tag("a.rs", "foo", TagKind::Def)],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 1.0);

        let mut caller_weights = HashMap::new();
        caller_weights.insert("a.rs".to_string(), 1.5);

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            None,
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            None,
            Some(&caller_weights),
            None,
        );

        assert_eq!(result.len(), 1);
        // With boost_caller_weight=2.0 (default) and raw=1.5:
        // caller_weight = 1.0 + (1.5 - 1.0) * 2.0 = 2.0
        assert_eq!(result[0].rank, 1.0 * 2.0); // base × scaled_caller_weight
    }

    #[test]
    fn test_focus_expansion_weight() {
        // Focus expansion weight should boost graph neighbors
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config.clone());

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![make_tag("a.rs", "foo", TagKind::Def)],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 0.1);

        let mut focus_expansion_weights = HashMap::new();
        focus_expansion_weights.insert((Arc::from("a.rs"), Arc::from("foo")), 0.8);

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            None,
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            None,
            None,
            Some(&focus_expansion_weights),
        );

        assert_eq!(result.len(), 1);
        // 0.1 × (5.0 × 0.8) = 0.4
        assert_eq!(result[0].rank, 0.1 * config.boost_focus_expansion * 0.8);
    }

    #[test]
    fn test_symbol_rank_overrides_file_rank() {
        // When symbol rank is available, use it instead of file rank
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config);

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![make_tag("a.rs", "foo", TagKind::Def)],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 0.1);

        let mut symbol_ranks = HashMap::new();
        symbol_ranks.insert((Arc::from("a.rs"), Arc::from("foo")), 0.9);

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            Some(&symbol_ranks),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            None,
            None,
            None,
        );

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].rank, 0.9); // Uses symbol rank, not file rank
    }

    #[test]
    fn test_only_definitions_included() {
        // Only TagKind::Def should be included, not TagKind::Ref
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config);

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![
                make_tag("a.rs", "foo", TagKind::Def),
                make_tag("a.rs", "bar", TagKind::Ref),
            ],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 1.0);

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            None,
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            None,
            None,
            None,
        );

        assert_eq!(result.len(), 1); // Only "foo" (Def), not "bar" (Ref)
        assert_eq!(result[0].tag.name.as_ref(), "foo");
    }

    #[test]
    fn test_sorting_descending() {
        // Results should be sorted by rank descending (highest first)
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config);

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![
                make_tag("a.rs", "low", TagKind::Def),
                make_tag("a.rs", "medium", TagKind::Def),
                make_tag("a.rs", "high", TagKind::Def),
            ],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 1.0);

        let mut symbol_ranks = HashMap::new();
        symbol_ranks.insert((Arc::from("a.rs"), Arc::from("low")), 0.1);
        symbol_ranks.insert((Arc::from("a.rs"), Arc::from("medium")), 0.5);
        symbol_ranks.insert((Arc::from("a.rs"), Arc::from("high")), 0.9);

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            Some(&symbol_ranks),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            None,
            None,
            None,
        );

        assert_eq!(result.len(), 3);
        assert_eq!(result[0].tag.name.as_ref(), "high");
        assert_eq!(result[1].tag.name.as_ref(), "medium");
        assert_eq!(result[2].tag.name.as_ref(), "low");
    }

    #[test]
    fn test_combined_all_weights() {
        // Test all weight/boost combinations together
        let config = RankingConfig::default();
        let calculator = BoostCalculator::new(config.clone());

        let mut tags_by_file = HashMap::new();
        tags_by_file.insert(
            "/a.rs".to_string(),
            vec![make_tag("a.rs", "foo", TagKind::Def)],
        );

        let mut file_ranks = HashMap::new();
        file_ranks.insert("a.rs".to_string(), 0.1);

        let mut mentioned_idents = HashSet::new();
        mentioned_idents.insert("foo".to_string());

        let mut mentioned_fnames = HashSet::new();
        mentioned_fnames.insert("a.rs".to_string());

        let mut chat_fnames = HashSet::new();
        chat_fnames.insert("/a.rs".to_string());

        let mut temporal_boost_files = HashSet::new();
        temporal_boost_files.insert("a.rs".to_string());

        let mut git_weights = HashMap::new();
        git_weights.insert("a.rs".to_string(), 2.0);

        let mut caller_weights = HashMap::new();
        caller_weights.insert("a.rs".to_string(), 1.5);

        let mut focus_expansion_weights = HashMap::new();
        focus_expansion_weights.insert((Arc::from("a.rs"), Arc::from("foo")), 0.5);

        let result = calculator.apply_boosts(
            &tags_by_file,
            &file_ranks,
            None,
            &chat_fnames,
            &mentioned_fnames,
            &mentioned_idents,
            &temporal_boost_files,
            Some(&git_weights),
            Some(&caller_weights),
            Some(&focus_expansion_weights),
        );

        assert_eq!(result.len(), 1);

        // Expected: 0.1 × 10 (ident) × 5 (file) × 20 (chat) × 3 (temporal)
        //           × (5 × 0.5) (focus) × 2.0 (git) × scaled_caller
        // With raw_caller=1.5, boost_caller_weight=2.0:
        //   scaled_caller = 1.0 + (1.5 - 1.0) * 2.0 = 2.0
        let raw_caller = 1.5;
        let scaled_caller = 1.0 + (raw_caller - 1.0) * config.boost_caller_weight;
        let expected = 0.1
            * config.boost_mentioned_ident
            * config.boost_mentioned_file
            * config.boost_chat_file
            * config.boost_temporal_coupling
            * (config.boost_focus_expansion * 0.5)
            * 2.0
            * scaled_caller;

        assert!((result[0].rank - expected).abs() < 1e-6);
    }

    #[test]
    fn test_extract_rel_fname() {
        assert_eq!(extract_rel_fname("/a.rs"), "a.rs");
        assert_eq!(extract_rel_fname("/src/lib.rs"), "src/lib.rs");
        assert_eq!(extract_rel_fname("no_slash.rs"), "no_slash.rs");
    }
}