agent-file-tools 0.36.0

Agent File Tools — tree-sitter powered code analysis for AI agents
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
//! Integration tests for `SemanticIndex::refresh_stale_files`.
//!
//! These cover the four real cases the configure path triggers on restart:
//!  - no-op (nothing changed → empty summary, no embed calls)
//!  - one file changed mtime → only that file is re-embedded
//!  - one file deleted from the walk → entries dropped, no embeds
//!  - one new file appeared in the walk → only that file is embedded
//!
//! We deliberately use a stub embedder that returns deterministic vectors so
//! we can assert byte-exact entry counts and dimensions without depending on
//! ONNX runtime or fastembed.

use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
use std::time::Duration;

use aft::semantic_index::SemanticIndex;

/// Stub embedder that returns vectors based on text content.
/// Tracks all calls so we can assert which files (and how many) got embedded.
struct StubEmbedder {
    calls: Mutex<Vec<Vec<String>>>,
}

impl StubEmbedder {
    fn new() -> Self {
        Self {
            calls: Mutex::new(Vec::new()),
        }
    }

    fn embed(&self, texts: Vec<String>) -> Result<Vec<Vec<f32>>, String> {
        let vectors: Vec<Vec<f32>> = texts
            .iter()
            .map(|text| {
                // Spread vectors based on content so different chunks have
                // different embeddings — keeps the index nontrivial.
                let len = text.len() as f32;
                vec![1.0, len.fract().abs(), 0.0, 0.0]
            })
            .collect();
        self.calls.lock().expect("lock embed calls").push(texts);
        Ok(vectors)
    }

    fn total_embedded_texts(&self) -> usize {
        self.calls
            .lock()
            .expect("lock embed calls")
            .iter()
            .map(|batch| batch.len())
            .sum()
    }

    fn batch_count(&self) -> usize {
        self.calls.lock().expect("lock embed calls").len()
    }

    fn embedded_texts(&self) -> Vec<String> {
        self.calls
            .lock()
            .expect("lock embed calls")
            .iter()
            .flat_map(|batch| batch.iter().cloned())
            .collect()
    }
}

/// Build an initial index over a small two-file project. Returns the index +
/// the file paths so tests can mutate one and call refresh.
fn build_two_file_index(project_root: &Path) -> (SemanticIndex, PathBuf, PathBuf) {
    let file_a = project_root.join("src/a.rs");
    let file_b = project_root.join("src/b.rs");
    fs::create_dir_all(file_a.parent().expect("parent")).expect("create src");
    fs::write(
        &file_a,
        "pub fn alpha() -> i32 {\n    let x = 1;\n    x\n}\n\npub fn alpha_helper() -> i32 {\n    let y = 2;\n    y\n}\n",
    )
    .expect("write a");
    fs::write(
        &file_b,
        "pub fn beta() -> i32 {\n    let x = 3;\n    x\n}\n\npub fn beta_helper() -> i32 {\n    let y = 4;\n    y\n}\n",
    )
    .expect("write b");

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let index = SemanticIndex::build(
        project_root,
        &[file_a.clone(), file_b.clone()],
        &mut embed,
        16,
    )
    .expect("build initial index");
    (index, file_a, file_b)
}

/// Touch a file's mtime by overwriting its contents, then explicitly advance
/// it so platforms with 1-second mtime granularity observe a changed timestamp.
fn rewrite_with_new_mtime(path: &Path, new_contents: &str) {
    let modified = fs::metadata(path)
        .expect("stat file before rewrite")
        .modified()
        .expect("file mtime before rewrite");
    let advanced = modified
        .checked_add(Duration::from_secs(2))
        .expect("advanced mtime");
    fs::write(path, new_contents).expect("rewrite");
    filetime::set_file_mtime(path, filetime::FileTime::from_system_time(advanced))
        .expect("set advanced mtime");
}

static SHARED_LOG_LOCK: OnceLock<Mutex<()>> = OnceLock::new();

fn shared_lock() -> &'static Mutex<()> {
    SHARED_LOG_LOCK.get_or_init(|| Mutex::new(()))
}

#[test]
fn refresh_is_noop_when_nothing_changed() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut index, file_a, file_b) = build_two_file_index(project.path());
    let entries_before = index.entry_count();

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};

    let summary = index
        .refresh_stale_files(
            project.path(),
            &[file_a.clone(), file_b.clone()],
            &mut embed,
            16,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert!(summary.is_noop(), "summary should be noop, got {summary:?}");
    assert_eq!(summary.deleted, 0);
    assert_eq!(summary.changed, 0);
    assert_eq!(summary.added, 0);
    assert_eq!(stub.total_embedded_texts(), 0, "no embeds for noop");
    assert_eq!(index.entry_count(), entries_before, "entries preserved");
}

#[test]
fn refresh_re_embeds_only_changed_file() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut index, file_a, file_b) = build_two_file_index(project.path());
    let entries_before = index.entry_count();

    // Modify file_a — file_b is untouched and must keep its cached embeddings.
    rewrite_with_new_mtime(
        &file_a,
        "pub fn alpha_renamed() -> i32 {\n    let x = 99;\n    x\n}\n\npub fn alpha_helper_renamed() -> i32 {\n    let y = 100;\n    y\n}\n",
    );

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};

    let summary = index
        .refresh_stale_files(
            project.path(),
            &[file_a.clone(), file_b.clone()],
            &mut embed,
            16,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert_eq!(summary.changed, 1, "exactly one file changed");
    assert_eq!(summary.deleted, 0);
    assert_eq!(summary.added, 0);
    assert!(stub.total_embedded_texts() > 0, "should re-embed something");

    // We embedded only file_a's chunks, not file_b's. Total embedded texts
    // must be strictly less than entries_before (which covered both files).
    assert!(
        stub.total_embedded_texts() < entries_before,
        "should embed less than full rebuild; embedded={}, full={}",
        stub.total_embedded_texts(),
        entries_before
    );

    // Sanity: index still has entries for file_b without re-embedding.
    let count_for_b = count_entries_for_file(&index, &file_b);
    assert!(count_for_b > 0, "file_b entries preserved");
}

#[test]
fn refresh_drops_entries_for_files_no_longer_in_walk() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut index, file_a, file_b) = build_two_file_index(project.path());

    let count_for_b_before = count_entries_for_file(&index, &file_b);
    assert!(count_for_b_before > 0, "precondition: index has b entries");

    // Simulate: walk now only returns file_a (file_b deleted or excluded).
    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};

    let summary = index
        .refresh_stale_files(
            project.path(),
            std::slice::from_ref(&file_a),
            &mut embed,
            16,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert_eq!(summary.deleted, 1, "file_b reported as deleted");
    assert_eq!(summary.changed, 0);
    assert_eq!(summary.added, 0);
    assert_eq!(stub.total_embedded_texts(), 0, "no embed calls");
    assert_eq!(
        count_entries_for_file(&index, &file_b),
        0,
        "file_b entries dropped"
    );
}

#[test]
fn refresh_embeds_new_files_added_to_walk() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut index, file_a, file_b) = build_two_file_index(project.path());
    let entries_before = index.entry_count();

    // A new file that was never in the original index.
    let file_c = project.path().join("src/c.rs");
    fs::write(
        &file_c,
        "pub fn gamma() -> i32 {\n    let z = 5;\n    z\n}\n\npub fn gamma_helper() -> i32 {\n    let w = 6;\n    w\n}\n",
    )
    .expect("write c");

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};

    let summary = index
        .refresh_stale_files(
            project.path(),
            &[file_a, file_b, file_c.clone()],
            &mut embed,
            16,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert_eq!(summary.added, 1, "file_c discovered as new");
    assert_eq!(summary.changed, 0);
    assert_eq!(summary.deleted, 0);
    assert!(stub.total_embedded_texts() > 0);
    assert!(stub.total_embedded_texts() > 0, "embedded only file_c");

    // Index grew strictly larger (kept original, added new).
    assert!(
        index.entry_count() > entries_before,
        "index grew; before={}, after={}",
        entries_before,
        index.entry_count()
    );
    assert!(
        count_entries_for_file(&index, &file_c) > 0,
        "file_c entries present"
    );
}

#[test]
fn refresh_handles_changed_plus_deleted_plus_new_in_one_call() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut index, file_a, file_b) = build_two_file_index(project.path());

    // Change file_a, drop file_b from the walk, add file_c.
    rewrite_with_new_mtime(
        &file_a,
        "pub fn alpha_v2() -> i32 {\n    let v = 42;\n    v\n}\n",
    );
    let file_c = project.path().join("src/c.rs");
    fs::write(
        &file_c,
        "pub fn gamma() -> i32 {\n    let z = 5;\n    z\n}\n",
    )
    .expect("write c");

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut batches: Vec<(usize, usize)> = Vec::new();
    let mut progress = |done: usize, total: usize| batches.push((done, total));

    let summary = index
        .refresh_stale_files(
            project.path(),
            &[file_a, file_c.clone()],
            &mut embed,
            16,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert_eq!(summary.deleted, 1, "file_b deleted");
    assert_eq!(summary.changed, 1, "file_a changed");
    assert_eq!(summary.added, 1, "file_c new");

    // file_b entries are gone.
    assert_eq!(count_entries_for_file(&index, &file_b), 0);
    // file_c entries are present.
    assert!(count_entries_for_file(&index, &file_c) > 0);

    // Progress callback fired at least once with a meaningful total.
    assert!(
        batches.iter().any(|(_done, total)| *total > 0),
        "progress callback should report nonzero total at least once"
    );

    // Embedded calls should match changed + new files only — not file_b
    // (deleted) and not the original file_a/file_b cached embeddings.
    assert!(stub.batch_count() >= 1, "at least one embed batch");
}

#[test]
fn refresh_reuses_line_shifted_file_chunks_without_embedding() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut index, file_a, file_b) = build_two_file_index(project.path());
    let count_for_a_before = count_entries_for_file(&index, &file_a);

    let shifted = "\npub fn alpha() -> i32 {\n    let x = 1;\n    x\n}\n\npub fn alpha_helper() -> i32 {\n    let y = 2;\n    y\n}\n";
    rewrite_with_new_mtime(&file_a, shifted);

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};
    let summary = index
        .refresh_stale_files(
            project.path(),
            &[file_a.clone(), file_b],
            &mut embed,
            16,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert_eq!(summary.changed, 1);
    assert_eq!(stub.total_embedded_texts(), 0);
    assert_eq!(count_entries_for_file(&index, &file_a), count_for_a_before);
}

#[test]
fn refresh_reembeds_only_edited_symbol_in_changed_file() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut index, file_a, file_b) = build_two_file_index(project.path());
    let count_for_a_before = count_entries_for_file(&index, &file_a);
    assert!(
        count_for_a_before >= 2,
        "file_a starts with multiple symbols"
    );

    rewrite_with_new_mtime(
        &file_a,
        "pub fn alpha() -> i32 {\n    let x = 99;\n    x\n}\n\npub fn alpha_helper() -> i32 {\n    let y = 2;\n    y\n}\n",
    );

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};
    let summary = index
        .refresh_stale_files(
            project.path(),
            &[file_a.clone(), file_b],
            &mut embed,
            16,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert_eq!(summary.changed, 1);
    assert_eq!(stub.total_embedded_texts(), 1);
    assert!(stub.embedded_texts()[0].contains("name:alpha"));
    // Duplication guard: the unchanged siblings are reused (embed-count==1 above
    // proves only the edited symbol was embedded), and the post-refresh entry
    // count for the file is unchanged — no chunk dropped, none duplicated.
    assert_eq!(
        count_entries_for_file(&index, &file_a),
        count_for_a_before,
        "entry count unchanged after partial-reuse refresh"
    );
}

#[test]
fn refresh_stale_files_collect_failure_keeps_stale_entries() {
    // The corpus-refresh path (refresh_stale_files) must KEEP stale-but-valid
    // entries when a changed file fails to re-collect (transient/parse error) —
    // the deliberate opposite of the watcher path, which drops them up front.
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut index, file_a, file_b) = build_two_file_index(project.path());
    let count_for_a_before = count_entries_for_file(&index, &file_a);
    assert!(count_for_a_before > 0, "file_a starts with entries");

    // Corrupt file_a (invalid UTF-8 → collect yields nothing) and advance its
    // mtime so it is bucketed as "changed" rather than fresh.
    fs::write(&file_a, [0xff, 0xfe, 0xfd]).expect("write invalid utf8");
    let advanced = fs::metadata(&file_a)
        .expect("stat after corrupt")
        .modified()
        .expect("mtime after corrupt")
        .checked_add(Duration::from_secs(2))
        .expect("advance mtime");
    filetime::set_file_mtime(&file_a, filetime::FileTime::from_system_time(advanced))
        .expect("set advanced mtime");

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};
    index
        .refresh_stale_files(
            project.path(),
            &[file_a.clone(), file_b],
            &mut embed,
            16,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert_eq!(
        stub.total_embedded_texts(),
        0,
        "nothing embeds when the changed file fails to collect"
    );
    assert_eq!(
        count_entries_for_file(&index, &file_a),
        count_for_a_before,
        "stale entries kept (not dropped, not duplicated) on collect failure"
    );
}

#[test]
fn invalidated_refresh_mixed_reuse_and_miss_retains_all_after_apply() {
    // Editing one symbol's body in a multi-symbol file: that symbol's chunk is a
    // cache miss (re-embedded) while siblings + file-summary are reused. The
    // applied delta must carry the FULL replacement set so the serving index
    // neither drops the reused chunks nor duplicates them.
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut worker_index, file_a, _file_b) = build_two_file_index(project.path());
    let mut serving_index = worker_index.clone();
    let count_for_a_before = count_entries_for_file(&serving_index, &file_a);
    assert!(count_for_a_before >= 2, "file_a has multiple symbols");

    // Edit only alpha's body; alpha_helper is byte-identical and reused.
    rewrite_with_new_mtime(
        &file_a,
        "pub fn alpha() -> i32 {\n    let x = 99;\n    x\n}\n\npub fn alpha_helper() -> i32 {\n    let y = 2;\n    y\n}\n",
    );

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};
    let update = worker_index
        .refresh_invalidated_files(
            project.path(),
            std::slice::from_ref(&file_a),
            &mut embed,
            16,
            100,
            &mut progress,
        )
        .expect("refresh succeeds");

    // Mixed path: exactly the edited symbol re-embeds; the full set is still
    // delivered as the delta (reused + newly embedded).
    assert_eq!(
        stub.total_embedded_texts(),
        1,
        "only the edited symbol re-embeds; siblings reused"
    );
    assert_eq!(
        update.added_entries.len(),
        count_for_a_before,
        "delta is the full replacement set, not just the miss"
    );

    serving_index.apply_refresh_update(
        update.added_entries,
        update.updated_metadata,
        &update.completed_paths,
    );

    assert_eq!(
        count_entries_for_file(&serving_index, &file_a),
        count_for_a_before,
        "no chunk dropped, none duplicated after applying the mixed delta"
    );
}

#[test]
fn invalidated_refresh_delta_retains_reused_chunks_after_apply() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut worker_index, file_a, _file_b) = build_two_file_index(project.path());
    let mut serving_index = worker_index.clone();
    let count_for_a_before = count_entries_for_file(&serving_index, &file_a);

    rewrite_with_new_mtime(
        &file_a,
        "\npub fn alpha() -> i32 {\n    let x = 1;\n    x\n}\n\npub fn alpha_helper() -> i32 {\n    let y = 2;\n    y\n}\n",
    );

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};
    let update = worker_index
        .refresh_invalidated_files(
            project.path(),
            std::slice::from_ref(&file_a),
            &mut embed,
            16,
            100,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert_eq!(stub.total_embedded_texts(), 0);
    assert_eq!(update.added_entries.len(), count_for_a_before);

    serving_index.apply_refresh_update(
        update.added_entries,
        update.updated_metadata,
        &update.completed_paths,
    );

    assert_eq!(
        count_entries_for_file(&serving_index, &file_a),
        count_for_a_before
    );
}

#[test]
fn invalidated_refresh_reuses_duplicate_embed_text_for_new_identical_symbol() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let file = project.path().join("src/dupe.js");
    fs::create_dir_all(file.parent().expect("parent")).expect("create src");
    let duplicate = "function duplicate() {\n  return 1;\n}\n";
    fs::write(&file, duplicate).expect("write duplicate");

    let initial_stub = StubEmbedder::new();
    let mut initial_embed = |texts: Vec<String>| initial_stub.embed(texts);
    let mut index = SemanticIndex::build(
        project.path(),
        std::slice::from_ref(&file),
        &mut initial_embed,
        16,
    )
    .expect("build initial index");

    rewrite_with_new_mtime(&file, &format!("{duplicate}\n{duplicate}"));

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};
    index
        .refresh_invalidated_files(
            project.path(),
            std::slice::from_ref(&file),
            &mut embed,
            16,
            100,
            &mut progress,
        )
        .expect("refresh succeeds");

    let duplicate_results = index
        .search(&[1.0, 0.0, 0.0, 0.0], 16)
        .into_iter()
        .filter(|result| result.file == file && result.name == "duplicate")
        .count();
    assert_eq!(stub.total_embedded_texts(), 0);
    assert_eq!(duplicate_results, 2);
}

#[test]
fn invalidated_refresh_file_summary_reuse_and_miss_are_text_based() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let file = project.path().join("src/lib.rs");
    fs::create_dir_all(file.parent().expect("parent")).expect("create src");
    fs::write(
        &file,
        "//! module docs v1\n\npub fn alpha() -> i32 {\n    1\n}\n",
    )
    .expect("write source");

    let initial_stub = StubEmbedder::new();
    let mut initial_embed = |texts: Vec<String>| initial_stub.embed(texts);
    let mut index = SemanticIndex::build(
        project.path(),
        std::slice::from_ref(&file),
        &mut initial_embed,
        16,
    )
    .expect("build initial index");

    rewrite_with_new_mtime(
        &file,
        "//! module docs v1\n\npub fn alpha() -> i32 {\n    2\n}\n",
    );
    let body_stub = StubEmbedder::new();
    let mut body_embed = |texts: Vec<String>| body_stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};
    index
        .refresh_invalidated_files(
            project.path(),
            std::slice::from_ref(&file),
            &mut body_embed,
            16,
            100,
            &mut progress,
        )
        .expect("body refresh succeeds");
    assert_eq!(body_stub.total_embedded_texts(), 1);
    assert!(body_stub.embedded_texts()[0].contains("name:alpha"));

    rewrite_with_new_mtime(
        &file,
        "//! module docs v2\n\npub fn alpha() -> i32 {\n    2\n}\n",
    );
    let doc_stub = StubEmbedder::new();
    let mut doc_embed = |texts: Vec<String>| doc_stub.embed(texts);
    index
        .refresh_invalidated_files(
            project.path(),
            std::slice::from_ref(&file),
            &mut doc_embed,
            16,
            100,
            &mut progress,
        )
        .expect("doc refresh succeeds");
    assert_eq!(doc_stub.total_embedded_texts(), 1);
    assert!(doc_stub.embedded_texts()[0].contains("kind:file-summary"));
}

#[test]
fn invalidated_refresh_deleted_file_drops_entries_after_apply() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut worker_index, file_a, _file_b) = build_two_file_index(project.path());
    let mut serving_index = worker_index.clone();

    fs::remove_file(&file_a).expect("delete file");

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};
    let update = worker_index
        .refresh_invalidated_files(
            project.path(),
            std::slice::from_ref(&file_a),
            &mut embed,
            16,
            100,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert_eq!(update.summary.deleted, 1);
    assert_eq!(stub.total_embedded_texts(), 0);
    serving_index.apply_refresh_update(
        update.added_entries,
        update.updated_metadata,
        &update.completed_paths,
    );
    assert_eq!(count_entries_for_file(&serving_index, &file_a), 0);
}

#[test]
fn invalidated_refresh_collect_failure_does_not_resurrect_stale_entries() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let (mut worker_index, file_a, _file_b) = build_two_file_index(project.path());
    let mut serving_index = worker_index.clone();

    fs::write(&file_a, [0xff, 0xfe, 0xfd]).expect("write invalid utf8");

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};
    let update = worker_index
        .refresh_invalidated_files(
            project.path(),
            std::slice::from_ref(&file_a),
            &mut embed,
            16,
            100,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert_eq!(stub.total_embedded_texts(), 0);
    assert!(update.added_entries.is_empty());
    serving_index.apply_refresh_update(
        update.added_entries,
        update.updated_metadata,
        &update.completed_paths,
    );
    assert_eq!(count_entries_for_file(&serving_index, &file_a), 0);
}

#[test]
fn invalidated_refresh_cap_deferral_stays_file_count_based() {
    let _guard = shared_lock().lock();
    let project = tempfile::tempdir().expect("create project dir");
    let file_a = project.path().join("src/a.rs");
    let file_b = project.path().join("src/b.rs");
    fs::create_dir_all(file_a.parent().expect("parent")).expect("create src");
    fs::write(&file_a, "pub fn alpha() -> i32 {\n    1\n}\n").expect("write a");
    fs::write(&file_b, "pub fn beta() -> i32 {\n    2\n}\n").expect("write b");

    let initial_stub = StubEmbedder::new();
    let mut initial_embed = |texts: Vec<String>| initial_stub.embed(texts);
    let mut index = SemanticIndex::build(
        project.path(),
        std::slice::from_ref(&file_a),
        &mut initial_embed,
        16,
    )
    .expect("build initial index");

    let stub = StubEmbedder::new();
    let mut embed = |texts: Vec<String>| stub.embed(texts);
    let mut progress = |_done: usize, _total: usize| {};
    let update = index
        .refresh_invalidated_files(
            project.path(),
            std::slice::from_ref(&file_b),
            &mut embed,
            16,
            1,
            &mut progress,
        )
        .expect("refresh succeeds");

    assert_eq!(update.summary.total_processed, 1);
    assert_eq!(update.summary.added, 0);
    assert_eq!(stub.total_embedded_texts(), 0);
    assert_eq!(index.indexed_file_count(), 1);
    assert_eq!(count_entries_for_file(&index, &file_b), 0);
}

/// Helper: count entries in the index that belong to `file`. We only have
/// public access via search results, so we issue a query that should match
/// every entry (vector field is mostly 1.0 in our stub) and filter by file.
fn count_entries_for_file(index: &SemanticIndex, file: &Path) -> usize {
    let query = vec![1.0, 0.5, 0.0, 0.0];
    let results = index.search(&query, 1024);
    results.iter().filter(|r| r.file == file).count()
}