syntext 1.1.1

Hybrid code search index for agent workflows
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
//! Unit tests for overlay: incremental updates, batch atomicity, snapshot isolation.
//!
//! T043: single file add, modify, delete, batch atomicity, snapshot isolation.

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

use syntext::index::overlay::{compute_delete_set, OverlayView};
use syntext::IndexError;

/// Helper: build dirty file list with Arc<[u8]> content.
fn dirty(files: &[(&str, &[u8])]) -> Vec<(PathBuf, Arc<[u8]>)> {
    files
        .iter()
        .map(|(p, c)| (PathBuf::from(p), Arc::from(*c)))
        .collect()
}

// ---------------------------------------------------------------------------
// OverlayView::build tests
// ---------------------------------------------------------------------------

#[test]
fn overlay_single_file_add() {
    let overlay =
        OverlayView::build(10, dirty(&[("src/main.rs", b"fn parse_query() { }")])).unwrap();

    assert_eq!(overlay.docs.len(), 1);
    assert_eq!(overlay.docs[0].doc_id, 10); // starts after base range
    assert_eq!(overlay.docs[0].path, Path::new("src/main.rs"));
    assert!(!overlay.gram_index.is_empty(), "overlay should have grams");
}

#[test]
fn overlay_multiple_files() {
    let overlay = OverlayView::build(
        5,
        dirty(&[("a.rs", b"fn alpha() {}"), ("b.rs", b"fn beta() {}")]),
    )
    .unwrap();

    assert_eq!(overlay.docs.len(), 2);
    assert_eq!(overlay.docs[0].doc_id, 5);
    assert_eq!(overlay.docs[1].doc_id, 6);
    assert_eq!(overlay.next_doc_id, 7);
}

#[test]
fn overlay_empty() {
    let overlay = OverlayView::build(100, vec![]).unwrap();
    assert!(overlay.docs.is_empty());
    assert!(overlay.gram_index.is_empty());
    assert_eq!(overlay.next_doc_id, 100);
}

#[test]
fn overlay_doc_lookup_by_id() {
    let overlay = OverlayView::build(0, dirty(&[("test.rs", b"hello world")])).unwrap();

    assert!(overlay.get_doc(0).is_some());
    assert!(overlay.get_doc(1).is_none());
}

#[test]
fn overlay_doc_lookup_by_path() {
    let overlay = OverlayView::build(0, dirty(&[("a.rs", b"aaa"), ("b.rs", b"bbb")])).unwrap();

    assert!(overlay.get_doc_by_path(Path::new("a.rs")).is_some());
    assert!(overlay.get_doc_by_path(Path::new("b.rs")).is_some());
    assert!(overlay.get_doc_by_path(Path::new("c.rs")).is_none());
}

// ---------------------------------------------------------------------------
// Modify semantics: rebuild overlay replaces old content
// ---------------------------------------------------------------------------

#[test]
fn overlay_rebuild_replaces_content() {
    // First version
    let ov1 = OverlayView::build(10, dirty(&[("file.rs", b"fn old_function() {}")])).unwrap();
    let grams_v1: Vec<u64> = ov1.gram_index.keys().copied().collect();

    // Second version (same file, different content)
    let ov2 = OverlayView::build(10, dirty(&[("file.rs", b"fn new_function() {}")])).unwrap();
    let grams_v2: Vec<u64> = ov2.gram_index.keys().copied().collect();

    // Content should be updated
    assert_eq!(
        std::str::from_utf8(&ov2.docs[0].content).unwrap(),
        "fn new_function() {}"
    );

    // Grams should differ (old function name grams gone, new ones present)
    assert_ne!(grams_v1, grams_v2, "gram sets should differ after modify");
}

// ---------------------------------------------------------------------------
// Delete semantics: file removed from overlay entirely
// ---------------------------------------------------------------------------

#[test]
fn overlay_delete_removes_file() {
    // Build with a file, then rebuild without it (simulating delete)
    let ov_with = OverlayView::build(10, dirty(&[("file.rs", b"fn something() {}")])).unwrap();
    assert_eq!(ov_with.docs.len(), 1);

    // After delete, rebuild with empty set
    let ov_without = OverlayView::build(10, vec![]).unwrap();
    assert_eq!(ov_without.docs.len(), 0);
    assert!(ov_without.gram_index.is_empty());
}

// ---------------------------------------------------------------------------
// Snapshot isolation via Arc
// ---------------------------------------------------------------------------

#[test]
fn snapshot_isolation_via_arc() {
    // Simulate: reader holds old snapshot, writer creates new one.
    let ov1 = Arc::new(OverlayView::build(0, dirty(&[("file.rs", b"version one")])).unwrap());

    // Reader holds a reference to v1
    let reader_snap = Arc::clone(&ov1);

    // Writer creates v2 (in real code this would be ArcSwap::store)
    let _ov2 = Arc::new(OverlayView::build(0, dirty(&[("file.rs", b"version two")])).unwrap());

    // Reader still sees v1
    assert_eq!(
        std::str::from_utf8(&reader_snap.docs[0].content).unwrap(),
        "version one"
    );
}

// ---------------------------------------------------------------------------
// build_incremental tests
// ---------------------------------------------------------------------------

#[test]
fn incremental_reuses_unchanged_content() {
    let old = OverlayView::build(
        10,
        dirty(&[("a.rs", b"aaa content"), ("b.rs", b"bbb content")]),
    )
    .unwrap();
    let old_a_ptr = Arc::as_ptr(
        &old.docs
            .iter()
            .find(|d| d.path == Path::new("a.rs"))
            .unwrap()
            .content,
    );

    // Only b.rs changed; a.rs should be reused via Arc::clone.
    let newly_changed: HashSet<PathBuf> = [PathBuf::from("b.rs")].into();
    let removed: HashSet<PathBuf> = HashSet::new();
    let new_files = dirty(&[("b.rs", b"bbb updated")]);

    let inc =
        OverlayView::build_incremental(10, &old, new_files, &newly_changed, &removed).unwrap();

    assert_eq!(inc.docs.len(), 2);

    // a.rs content should be the same Arc (pointer equality).
    let inc_a = inc
        .docs
        .iter()
        .find(|d| d.path == Path::new("a.rs"))
        .unwrap();
    assert!(
        std::ptr::eq(Arc::as_ptr(&inc_a.content), old_a_ptr),
        "unchanged doc should share Arc, not clone"
    );

    // b.rs content should be updated.
    let inc_b = inc
        .docs
        .iter()
        .find(|d| d.path == Path::new("b.rs"))
        .unwrap();
    assert_eq!(std::str::from_utf8(&inc_b.content).unwrap(), "bbb updated");
}

#[test]
fn incremental_removes_deleted() {
    let old = OverlayView::build(10, dirty(&[("a.rs", b"aaa"), ("b.rs", b"bbb")])).unwrap();

    let newly_changed: HashSet<PathBuf> = HashSet::new();
    let removed: HashSet<PathBuf> = [PathBuf::from("b.rs")].into();

    let inc = OverlayView::build_incremental(10, &old, vec![], &newly_changed, &removed).unwrap();

    assert_eq!(inc.docs.len(), 1);
    assert_eq!(inc.docs[0].path, Path::new("a.rs"));
    assert!(inc.get_doc_by_path(Path::new("b.rs")).is_none());
}

#[test]
fn incremental_from_empty_old_overlay() {
    let old = OverlayView::empty();
    let newly_changed: HashSet<PathBuf> = [PathBuf::from("new.rs")].into();
    let removed: HashSet<PathBuf> = HashSet::new();
    let new_files = dirty(&[("new.rs", b"fn new() {}")]);

    let inc = OverlayView::build_incremental(5, &old, new_files, &newly_changed, &removed).unwrap();

    assert_eq!(inc.docs.len(), 1);
    assert_eq!(inc.docs[0].path, Path::new("new.rs"));
    assert_eq!(inc.docs[0].doc_id, 5);
}

#[test]
fn compute_delete_set_marks_all_base_docs_for_invalidated_paths() {
    let mut base_path_doc_ids = HashMap::new();
    base_path_doc_ids.insert(PathBuf::from("src/main.rs"), vec![1, 7]);
    base_path_doc_ids.insert(PathBuf::from("src/lib.rs"), vec![3]);

    let modified: std::collections::HashSet<PathBuf> = [PathBuf::from("src/main.rs")].into();
    let deleted: std::collections::HashSet<PathBuf> = [PathBuf::from("src/missing.rs")].into();
    let delete_set = compute_delete_set(
        &base_path_doc_ids,
        &modified,
        &deleted,
        &roaring::RoaringBitmap::new(),
    );

    assert!(delete_set.contains(1));
    assert!(delete_set.contains(7));
    assert!(!delete_set.contains(3));
}

#[test]
fn overlay_build_stores_base_doc_count() {
    let ov = OverlayView::build(42, dirty(&[("a.rs", b"fn a() {}")])).unwrap();
    assert_eq!(ov.base_doc_count, 42);
}

#[test]
fn overlay_empty_base_doc_count_is_zero() {
    let ov = OverlayView::empty();
    assert_eq!(ov.base_doc_count, 0);
}

#[test]
fn overlay_build_returns_doc_id_overflow() {
    // base_doc_count near u32::MAX means the first += 1 would overflow.
    let result = OverlayView::build(u32::MAX, dirty(&[("a.rs", b"fn a() {}")]));
    assert!(
        matches!(
            result,
            Err(IndexError::DocIdOverflow {
                base_doc_count: u32::MAX,
                overlay_docs: 1,
            })
        ),
        "overflow should return a structured error"
    );
}

#[test]
fn incremental_reuses_cached_grams() {
    let old = OverlayView::build(10, dirty(&[("a.rs", b"fn alpha_function() {}")])).unwrap();

    // Capture gram set from old overlay.
    let old_a_grams: Vec<u64> = {
        let doc = old
            .docs
            .iter()
            .find(|d| d.path == Path::new("a.rs"))
            .unwrap();
        doc.grams.clone()
    };
    assert!(!old_a_grams.is_empty(), "should have grams");

    // Incremental: only b.rs is new; a.rs should reuse cached grams.
    let newly_changed: HashSet<PathBuf> = [PathBuf::from("b.rs")].into();
    let removed: HashSet<PathBuf> = HashSet::new();
    let new_files = dirty(&[("b.rs", b"fn beta() {}")]);

    let inc =
        OverlayView::build_incremental(10, &old, new_files, &newly_changed, &removed).unwrap();

    let inc_a = inc
        .docs
        .iter()
        .find(|d| d.path == Path::new("a.rs"))
        .unwrap();
    assert_eq!(
        inc_a.grams, old_a_grams,
        "reused doc should have same cached grams"
    );
}

// ---------------------------------------------------------------------------
// build_incremental_delta tests (Task 2)
// ---------------------------------------------------------------------------

/// Delta path: unchanged files keep their exact old doc_ids when base_doc_count is stable.
#[test]
fn delta_unchanged_files_keep_doc_ids() {
    let old = OverlayView::build(
        10,
        dirty(&[("a.rs", b"fn alpha() {}"), ("b.rs", b"fn beta() {}")]),
    )
    .unwrap();
    let a_old_id = old
        .docs
        .iter()
        .find(|d| d.path == Path::new("a.rs"))
        .unwrap()
        .doc_id;

    let newly_changed: HashSet<PathBuf> = [PathBuf::from("b.rs")].into();
    let removed: HashSet<PathBuf> = HashSet::new();
    let new_files = dirty(&[("b.rs", b"fn beta_v2() {}")]);

    // Same base_doc_count => routes to delta path via build_incremental.
    let inc =
        OverlayView::build_incremental(10, &old, new_files, &newly_changed, &removed).unwrap();

    let a_new_id = inc
        .docs
        .iter()
        .find(|d| d.path == Path::new("a.rs"))
        .unwrap()
        .doc_id;
    assert_eq!(
        a_new_id, a_old_id,
        "unchanged doc keeps its doc_id on delta path"
    );
}

/// Delta path: gram_index for unchanged files equals what full rebuild produces.
#[test]
fn delta_gram_index_matches_full_rebuild() {
    let old = OverlayView::build(
        10,
        dirty(&[("a.rs", b"fn alpha() {}"), ("b.rs", b"fn beta() {}")]),
    )
    .unwrap();

    let newly_changed: HashSet<PathBuf> = [PathBuf::from("b.rs")].into();
    let removed: HashSet<PathBuf> = HashSet::new();
    let new_b = dirty(&[("b.rs", b"fn beta_v2() {}")]);

    // Same base_doc_count => routes to delta path via build_incremental.
    let delta =
        OverlayView::build_incremental(10, &old, new_b.clone(), &newly_changed, &removed).unwrap();

    // Full rebuild for comparison.
    let all_files = dirty(&[("a.rs", b"fn alpha() {}"), ("b.rs", b"fn beta_v2() {}")]);
    let full = OverlayView::build(10, all_files).unwrap();

    // Same gram keys (order-independent comparison).
    let mut delta_keys: Vec<u64> = delta.gram_index.keys().copied().collect();
    let mut full_keys: Vec<u64> = full.gram_index.keys().copied().collect();
    delta_keys.sort_unstable();
    full_keys.sort_unstable();
    assert_eq!(
        delta_keys, full_keys,
        "gram_index keys must match full rebuild"
    );

    // Also verify posting list contents (not just keys).
    for (&hash, delta_ids) in &delta.gram_index {
        let mut d = delta_ids.clone();
        d.sort_unstable();
        // Map delta doc_ids back to paths, compare against full's doc_ids for same gram.
        // Since doc_ids differ between delta and full (stable vs. reassigned), compare
        // via the set of paths that contain each gram.
        let delta_paths: std::collections::BTreeSet<PathBuf> = d
            .iter()
            .filter_map(|&id| delta.get_doc(id).map(|doc| doc.path.clone()))
            .collect();
        let full_ids = full.gram_index.get(&hash).cloned().unwrap_or_default();
        let full_paths: std::collections::BTreeSet<PathBuf> = full_ids
            .iter()
            .filter_map(|&id| full.get_doc(id).map(|doc| doc.path.clone()))
            .collect();
        assert_eq!(
            delta_paths, full_paths,
            "posting list paths must match for gram {hash:#x}"
        );
    }
}

/// Delta path: deleting a file removes all its grams from gram_index.
#[test]
fn delta_deletion_removes_grams() {
    // Build with a file whose grams we know won't appear in the other file.
    let old = OverlayView::build(
        5,
        dirty(&[
            ("a.rs", b"fn unique_zzzq() {}"),
            ("b.rs", b"fn common() {}"),
        ]),
    )
    .unwrap();
    let zzzq_hash = old
        .gram_index
        .keys()
        .find(|&&h| {
            // Find a gram hash only in a.rs (doc_id=5), not b.rs (doc_id=6).
            let ids = &old.gram_index[&h];
            ids == &[5]
        })
        .copied();

    if let Some(h) = zzzq_hash {
        let removed: HashSet<PathBuf> = [PathBuf::from("a.rs")].into();
        let newly_changed: HashSet<PathBuf> = HashSet::new();
        // Same base_doc_count => routes to delta path via build_incremental.
        let inc =
            OverlayView::build_incremental(5, &old, vec![], &newly_changed, &removed).unwrap();

        assert!(
            !inc.gram_index.contains_key(&h),
            "gram unique to deleted file should be removed from index"
        );
    }
    // If all grams happen to be shared, the test is vacuously valid.
}

/// Posting lists for new docs are in sorted order after delta append.
#[test]
fn delta_posting_lists_sorted_after_new_doc() {
    let old = OverlayView::build(0, dirty(&[("a.rs", b"fn shared_token() {}")])).unwrap();

    let newly_changed: HashSet<PathBuf> = [PathBuf::from("b.rs")].into();
    let removed: HashSet<PathBuf> = HashSet::new();
    // b.rs shares some grams with a.rs (e.g., "fn").
    let new_files = dirty(&[("b.rs", b"fn shared_token() {}")]);

    // Same base_doc_count => routes to delta path via build_incremental.
    let inc = OverlayView::build_incremental(0, &old, new_files, &newly_changed, &removed).unwrap();

    for ids in inc.gram_index.values() {
        let mut sorted = ids.clone();
        sorted.sort_unstable();
        assert_eq!(ids, &sorted, "posting list must be sorted");
    }
}

/// When base_doc_count changes (segment flush), incremental falls back to full
/// rebuild: all doc_ids are reassigned starting from the new base.
#[test]
fn incremental_base_changed_reassigns_doc_ids() {
    let old = OverlayView::build(10, dirty(&[("a.rs", b"fn alpha() {}")])).unwrap();
    let a_old_id = old.docs[0].doc_id; // == 10

    // Simulate a segment flush: base_doc_count grew from 10 to 20.
    let newly_changed: HashSet<PathBuf> = HashSet::new();
    let removed: HashSet<PathBuf> = HashSet::new();
    let inc = OverlayView::build_incremental(20, &old, vec![], &newly_changed, &removed).unwrap();

    let a_new_id = inc.docs[0].doc_id;
    assert_ne!(
        a_new_id, a_old_id,
        "doc_id must be reassigned when base_doc_count grows"
    );
    assert_eq!(a_new_id, 20, "first doc starts at new base_doc_count");
}

#[test]
fn commit_does_not_clone_base_doc_to_file_id() {
    use syntext::index::Index;
    use syntext::Config;
    use tempfile::TempDir;

    let repo = TempDir::new().unwrap();
    let index_dir = TempDir::new().unwrap();

    for i in 0..10u8 {
        let name = format!("base_{i}.rs");
        std::fs::write(repo.path().join(&name), format!("fn f{i}() {{}}\n")).unwrap();
    }

    let config = Config {
        index_dir: index_dir.path().to_path_buf(),
        repo_root: repo.path().to_path_buf(),
        ..Config::default()
    };
    let index = Index::build(config).unwrap();

    let snap_before = index.snapshot();
    let ptr_before = Arc::as_ptr(&snap_before.base_doc_to_file_id);

    let new_file = repo.path().join("new.rs");
    std::fs::write(&new_file, b"fn beta() {}\n").unwrap();
    index.notify_change(&new_file).unwrap();
    index.commit_batch().unwrap();

    let snap_after = index.snapshot();
    let ptr_after = Arc::as_ptr(&snap_after.base_doc_to_file_id);
    assert_eq!(
        ptr_before, ptr_after,
        "base_doc_to_file_id Arc must be shared across commits"
    );
    drop(index);
}