syntext 2.0.0

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
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
//! Phase 5: Incremental/overlay differential oracle.
//!
//! Exercises the overlay delta path (notify_change, notify_delete, commit_batch)
//! by applying random mutation sequences to a live working tree and asserting that
//! `st` results match `rg` (which always reads live files) after each commit.
//!
//! Also includes a golden smoke test for the OverlayFull failure path.

#[path = "oracle_helpers.rs"]
mod oracle_helpers;

use oracle_helpers::{normalize_ndjson, rg_available};
use proptest::prelude::*;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use syntext::index::Index;
use syntext::{Config, IndexError};
use tempfile::TempDir;

// ---------------------------------------------------------------------------
// Mutation Operations (Phase 5.1)
// ---------------------------------------------------------------------------

/// A single mutation to apply to the working tree + index overlay.
#[derive(Debug, Clone)]
enum MutationOp {
    /// Overwrite an existing file with new content.
    ModifyFile { path: String, content: Vec<u8> },
    /// Create a new file (or overwrite if it already exists).
    CreateFile { path: String, content: Vec<u8> },
    /// Delete an existing file.
    DeleteFile { path: String },
    /// Rename a file (delete old, create new).
    RenameFile { from: String, to: String },
    /// Overwrite a file with binary content (contains a NUL byte).
    BinaryifyFile { path: String },
    /// Grow a file past max_file_size so it gets excluded.
    GrowPastLimit { path: String },
    /// notify_change then notify_delete in the same batch (change-then-delete).
    ChangeThenDeleteSameBatch { path: String, content: Vec<u8> },
}

fn text_content() -> impl Strategy<Value = Vec<u8>> {
    prop_oneof![
        Just(b"fn parse_query() {}\n".to_vec()),
        Just(b"fn reparse() { let x = 1; }\n".to_vec()),
        Just(b"def snake_case(camelCase):\n    parse\n".to_vec()),
        Just(b"// TODO: query\nfn helper() {}\n".to_vec()),
        Just(b"let result = process_batch();\n".to_vec()),
        Just(b"fn new_function() { parse_query(42); }\n".to_vec()),
    ]
}

fn file_path_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        Just("src/main.rs".to_string()),
        Just("src/lib.rs".to_string()),
        Just("src/util.rs".to_string()),
        Just("src/helper.rs".to_string()),
        Just("docs/notes.md".to_string()),
    ]
}

fn mutation_op_strategy() -> impl Strategy<Value = MutationOp> {
    prop_oneof![
        3 => (file_path_strategy(), text_content())
                .prop_map(|(path, content)| MutationOp::ModifyFile { path, content }),
        2 => (file_path_strategy(), text_content())
                .prop_map(|(path, content)| MutationOp::CreateFile { path, content }),
        2 => file_path_strategy()
                .prop_map(|path| MutationOp::DeleteFile { path }),
        1 => (file_path_strategy(), file_path_strategy())
                .prop_filter("rename must change path", |(a, b)| a != b)
                .prop_map(|(from, to)| MutationOp::RenameFile { from, to }),
        1 => file_path_strategy()
                .prop_map(|path| MutationOp::BinaryifyFile { path }),
        1 => file_path_strategy()
                .prop_map(|path| MutationOp::GrowPastLimit { path }),
        1 => (file_path_strategy(), text_content())
                .prop_map(|(path, content)| MutationOp::ChangeThenDeleteSameBatch { path, content }),
    ]
}

fn generate_mutation_sequence() -> impl Strategy<Value = Vec<MutationOp>> {
    prop::collection::vec(mutation_op_strategy(), 3..=8)
}

// ---------------------------------------------------------------------------
// Mutation Application (Phase 5.2)
// ---------------------------------------------------------------------------

/// Apply a mutation to the on-disk working tree and notify the index.
/// Returns `Ok(true)` if a `commit_batch` should be called, `Ok(false)` if the
/// op was a no-op (e.g. delete of non-existent file).
///
/// `git_cmd` is called with the git sub-command args to keep the git index in
/// sync so that `st`'s git-walk sees the same file set as `rg`'s filesystem walk.
fn apply_mutation(
    repo: &Path,
    index: &Index,
    op: &MutationOp,
    max_file_size: u64,
    git_cmd: &dyn Fn(&[&str]),
) -> Result<bool, String> {
    match op {
        MutationOp::ModifyFile { path, content } => {
            let abs = repo.join(path);
            if !abs.exists() {
                return Ok(false);
            }
            fs::write(&abs, content).map_err(|e| format!("ModifyFile write failed: {e}"))?;
            index
                .notify_change(&abs)
                .map_err(|e| format!("notify_change failed: {e}"))?;
            // No git add needed — existing tracked file, rg already sees it.
            Ok(true)
        }
        MutationOp::CreateFile { path, content } => {
            let abs = repo.join(path);
            if let Some(p) = abs.parent() {
                fs::create_dir_all(p).map_err(|e| format!("create_dir_all failed: {e}"))?;
            }
            fs::write(&abs, content).map_err(|e| format!("CreateFile write failed: {e}"))?;
            index
                .notify_change(&abs)
                .map_err(|e| format!("notify_change failed: {e}"))?;
            // Track in git so st's git-walk includes it.
            git_cmd(&["add", path]);
            Ok(true)
        }
        MutationOp::DeleteFile { path } => {
            let abs = repo.join(path);
            if !abs.exists() {
                return Ok(false);
            }
            fs::remove_file(&abs).map_err(|e| format!("DeleteFile remove_file failed: {e}"))?;
            index
                .notify_delete(&abs)
                .map_err(|e| format!("notify_delete failed: {e}"))?;
            // Remove from git index so rg and st agree the file is gone.
            git_cmd(&["rm", "--cached", "--ignore-unmatch", path]);
            Ok(true)
        }
        MutationOp::RenameFile { from, to } => {
            let abs_from = repo.join(from);
            let abs_to = repo.join(to);
            if !abs_from.exists() {
                return Ok(false);
            }
            if let Some(p) = abs_to.parent() {
                fs::create_dir_all(p)
                    .map_err(|e| format!("RenameFile create_dir_all failed: {e}"))?;
            }
            fs::rename(&abs_from, &abs_to).map_err(|e| format!("RenameFile rename failed: {e}"))?;
            index
                .notify_delete(&abs_from)
                .map_err(|e| format!("notify_delete(from) failed: {e}"))?;
            index
                .notify_change(&abs_to)
                .map_err(|e| format!("notify_change(to) failed: {e}"))?;
            // Update git index: remove old path, add new path.
            git_cmd(&["rm", "--cached", "--ignore-unmatch", from]);
            git_cmd(&["add", to]);
            Ok(true)
        }
        MutationOp::BinaryifyFile { path } => {
            let abs = repo.join(path);
            // Binary content: text prefix + NUL byte
            let mut content = b"fn binary_content() { ".to_vec();
            content.push(0);
            content.extend_from_slice(b" }\n");
            if let Some(p) = abs.parent() {
                fs::create_dir_all(p)
                    .map_err(|e| format!("BinaryifyFile create_dir_all failed: {e}"))?;
            }
            fs::write(&abs, &content).map_err(|e| format!("BinaryifyFile write failed: {e}"))?;
            index
                .notify_change(&abs)
                .map_err(|e| format!("notify_change failed: {e}"))?;
            Ok(true)
        }
        MutationOp::GrowPastLimit { path } => {
            let abs = repo.join(path);
            if let Some(p) = abs.parent() {
                fs::create_dir_all(p)
                    .map_err(|e| format!("GrowPastLimit create_dir_all failed: {e}"))?;
            }
            // Write max_file_size + 1 bytes so the file is excluded from indexing
            let oversized = vec![b'x'; (max_file_size + 1) as usize];
            fs::write(&abs, &oversized).map_err(|e| format!("GrowPastLimit write failed: {e}"))?;
            index
                .notify_change(&abs)
                .map_err(|e| format!("notify_change failed: {e}"))?;
            Ok(true)
        }
        MutationOp::ChangeThenDeleteSameBatch { path, content } => {
            let abs = repo.join(path);
            if let Some(p) = abs.parent() {
                fs::create_dir_all(p)
                    .map_err(|e| format!("ChangeThenDelete create_dir_all failed: {e}"))?;
            }
            // Write the file, notify_change, then delete, notify_delete — all in one batch
            fs::write(&abs, content).map_err(|e| format!("ChangeThenDelete write failed: {e}"))?;
            index
                .notify_change(&abs)
                .map_err(|e| format!("notify_change failed: {e}"))?;
            fs::remove_file(&abs).map_err(|e| format!("ChangeThenDelete remove failed: {e}"))?;
            index
                .notify_delete(&abs)
                .map_err(|e| format!("notify_delete failed: {e}"))?;
            // Remove from git index — file is gone from disk.
            git_cmd(&["rm", "--cached", "--ignore-unmatch", path]);
            Ok(true)
        }
    }
}

/// Run `st` and `rg` on the current working tree with `--json` and compare.
/// Applies Tier A (no false negatives) and Tier B (exact set match).
fn assert_st_matches_rg(
    repo: &Path,
    index_dir: &Path,
    query: &str,
    step: usize,
) -> Result<(), String> {
    if !rg_available() {
        return Ok(());
    }

    let st_bin = env!("CARGO_BIN_EXE_st");

    let st_args = [
        "--repo-root",
        repo.to_str().unwrap(),
        "--index-dir",
        index_dir.to_str().unwrap(),
        "--json",
        query,
    ];

    let st_output = Command::new(st_bin)
        .args(&st_args)
        .current_dir(repo)
        .env("SYNTEXT_DETERMINISTIC", "1")
        .output()
        .map_err(|e| format!("step {step}: failed to run st: {e}"))?;

    let rg_output = Command::new("rg")
        .args([
            "--json",
            "--hidden",
            "--crlf",
            "--glob",
            "!.gitignore",
            "--glob",
            "!.syntext",
            query,
            ".",
        ])
        .current_dir(repo)
        .output()
        .map_err(|e| format!("step {step}: failed to run rg: {e}"))?;

    let st_matches = normalize_ndjson(&st_output.stdout)
        .map_err(|e| format!("step {step}: st NDJSON parse error: {e}"))?;
    let rg_matches = normalize_ndjson(&rg_output.stdout)
        .map_err(|e| format!("step {step}: rg NDJSON parse error: {e}"))?;

    // Tier A: no false negatives at the line level.
    let st_line_keys: std::collections::HashSet<(&str, usize)> = st_matches
        .iter()
        .map(|m| (m.path.as_str(), m.line_number))
        .collect();
    for m in &rg_matches {
        if !st_line_keys.contains(&(m.path.as_str(), m.line_number)) {
            return Err(format!(
                "step {step}: Tier A Violation: rg found {:?} but st did not.\n\
                 Query: {:?}\n\
                 st stdout:\n{}\n\
                 rg stdout:\n{}",
                m,
                query,
                String::from_utf8_lossy(&st_output.stdout),
                String::from_utf8_lossy(&rg_output.stdout),
            ));
        }
    }

    if st_matches.len() != rg_matches.len() {
        return Err(format!(
            "step {step}: Tier B Violation: st={} matches, rg={} matches.\n\
             Query: {:?}\n\
             st stdout:\n{}\n\
             rg stdout:\n{}",
            st_matches.len(),
            rg_matches.len(),
            query,
            String::from_utf8_lossy(&st_output.stdout),
            String::from_utf8_lossy(&rg_output.stdout),
        ));
    }

    Ok(())
}

/// commit_batch with retry on LockConflict (matches incremental.rs pattern).
fn commit_batch_with_retry(index: &Index) -> Result<(), IndexError> {
    use std::thread;
    use std::time::Duration;
    const MAX: usize = 5;
    for attempt in 1..=MAX {
        match index.commit_batch() {
            Ok(()) => return Ok(()),
            Err(IndexError::LockConflict(_)) if attempt < MAX => {
                thread::sleep(Duration::from_millis(10));
            }
            Err(e) => return Err(e),
        }
    }
    unreachable!()
}

// ---------------------------------------------------------------------------
// Phase 5.2: Incremental Differential Property Test
// ---------------------------------------------------------------------------

fn generate_incremental_run() -> impl Strategy<Value = (Vec<MutationOp>, String)> {
    let query_strat = prop_oneof![
        Just("parse".to_string()),
        Just("parse_query".to_string()),
        Just("fn".to_string()),
        Just("let".to_string()),
        Just("helper".to_string()),
        Just("result".to_string()),
    ];
    (generate_mutation_sequence(), query_strat)
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(15))]
    #[test]
    fn test_incremental_differential((mutations, query) in generate_incremental_run()) {
        let repo = TempDir::new().unwrap();
        let index_dir_tmp = TempDir::new().unwrap();
        let index_dir: PathBuf = index_dir_tmp.path().to_path_buf();

        // Write initial corpus
        let initial_files = [
            ("src/main.rs", b"fn parse_query() {}\n".as_ref()),
            ("src/lib.rs", b"fn reparse() { let x = 1; }\n".as_ref()),
            ("src/util.rs", b"fn helper() {}\n".as_ref()),
        ];
        for (path, content) in &initial_files {
            let abs = repo.path().join(path);
            fs::create_dir_all(abs.parent().unwrap()).unwrap();
            fs::write(&abs, content).unwrap();
        }

        // Git init so st can discover files
        let git = |args: &[&str]| {
            Command::new("git")
                .arg("-C").arg(repo.path())
                .args(args)
                .output()
                .ok();
        };
        git(&["init"]);
        git(&["config", "user.name", "oracle"]);
        git(&["config", "user.email", "oracle@example.com"]);
        fs::write(repo.path().join(".gitignore"), b".syntext/\n.git/\n").unwrap();
        git(&["add", "."]);
        git(&["commit", "-m", "init", "--no-gpg-sign"]);

        let max_file_size: u64 = 512 * 1024; // 512KB for tests
        let config = Config {
            index_dir: index_dir.clone(),
            repo_root: repo.path().to_path_buf(),
            max_file_size,
            auto_update: false, // disable auto-update; we control commits manually
            ..Config::default()
        };

        let mut index = Index::build(config.clone()).expect("build index");

        // Apply mutations one at a time; commit + assert after each step
        for (step, op) in mutations.iter().enumerate() {
            let needs_commit = apply_mutation(repo.path(), &index, op, max_file_size, &git)
                .unwrap_or(false);

            if needs_commit {
                match commit_batch_with_retry(&index) {
                    Ok(()) => {}
                    Err(IndexError::OverlayFull { .. }) => {
                        // Overlay too large — this is a valid outcome; skip Tier A/B for this step
                        // (the overlay is in a known degraded state, not corrupted).
                        break;
                    }
                    Err(e) => panic!("step {step}: commit_batch failed: {e}"),
                }

                // Release the in-process index's shared dir lock while the
                // subprocess `st` runs. The subprocess auto-updates from git
                // independently (it never reads this process's in-memory
                // overlay), and its bounded update may need an exclusive lock to
                // rebuild the base on a large delta. Holding the shared lock
                // here would block that rebuild (LockConflict -> silent stale),
                // producing a false Tier-A failure that no real one-shot `st`
                // invocation would hit. Reopen afterward to keep exercising the
                // in-process overlay delta path on the next mutation.
                drop(index);
                assert_st_matches_rg(
                    repo.path(),
                    &index_dir,
                    &query,
                    step,
                ).expect("incremental differential mismatch");
                index = Index::open(config.clone()).expect("reopen index");
            }
        }

        drop(index);
    }
}

// ---------------------------------------------------------------------------
// Phase 5.3: OverlayFull Failure Path Golden Smoke Test
// ---------------------------------------------------------------------------

/// Verify that after an OverlayFull error:
/// 1. `commit_batch` returns `IndexError::OverlayFull`.
/// 2. Subsequent `st` searches still satisfy Tier A against the pre-error tree
///    (the verifier re-reads real files, so no fabricated matches possible).
#[test]
fn overlay_full_correctness() {
    if !rg_available() {
        return;
    }

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

    // Write exactly 1 file so the base has 1 document.
    fs::create_dir_all(repo.path().join("src")).unwrap();
    fs::write(repo.path().join("src/main.rs"), b"fn parse_query() {}\n").unwrap();

    let git = |args: &[&str]| {
        Command::new("git")
            .arg("-C")
            .arg(repo.path())
            .args(args)
            .output()
            .ok();
    };
    git(&["init"]);
    git(&["config", "user.name", "oracle"]);
    git(&["config", "user.email", "oracle@example.com"]);
    fs::write(repo.path().join(".gitignore"), b".syntext/\n.git/\n").unwrap();
    git(&["add", "."]);
    git(&["commit", "-m", "init", "--no-gpg-sign"]);

    let config = Config {
        index_dir: index_dir.clone(),
        repo_root: repo.path().to_path_buf(),
        auto_update: false,
        ..Config::default()
    };

    let index = Index::build(config).expect("build");

    // Verify baseline: st and rg agree on the initial corpus
    assert_st_matches_rg(repo.path(), &index_dir, "parse_query", 0)
        .expect("baseline differential mismatch");

    // Now: create enough overlay entries to exceed 50% of base docs.
    // Base has 1 doc. OverlayFull triggers when overlay_docs > 50% * base_docs.
    // 1 change gets us to 100% overlay, crossing the threshold.
    // Add 2 more distinct files to ensure we exceed the enforce threshold.
    let extra_files = ["src/lib.rs", "src/util.rs"];
    for path in &extra_files {
        let abs = repo.path().join(path);
        fs::write(&abs, b"fn helper() { let x = parse_all(); }\n").unwrap();
        index.notify_change(&abs).ok();
    }

    let result = index.commit_batch();
    match result {
        Err(IndexError::OverlayFull { .. }) => {
            // Expected — now verify Tier A still holds on the pre-error tree state.
            // st should not return fabricated matches (verifier re-reads live files).
            assert_st_matches_rg(repo.path(), &index_dir, "parse_query", 99).expect(
                "post-OverlayFull differential mismatch: verifier must not fabricate matches",
            );
        }
        Ok(()) => {
            // The overlay didn't fill up with this corpus size.
            // This means the index has more base docs than expected (proptest shrinking,
            // or the threshold wasn't crossed). Not a failure — just skip the assertion.
            // We still verify correctness.
            assert_st_matches_rg(repo.path(), &index_dir, "parse_query", 99)
                .expect("post-commit differential mismatch");
        }
        Err(e) => panic!("unexpected commit_batch error: {e}"),
    }

    drop(index);
}

// ---------------------------------------------------------------------------
// Additional golden smoke: rename correctness
// ---------------------------------------------------------------------------

#[test]
fn golden_incremental_rename() {
    if !rg_available() {
        return;
    }

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

    fs::create_dir_all(repo.path().join("src")).unwrap();
    fs::write(repo.path().join("src/old.rs"), b"fn parse_query() {}\n").unwrap();

    let git = |args: &[&str]| {
        Command::new("git")
            .arg("-C")
            .arg(repo.path())
            .args(args)
            .output()
            .ok();
    };
    git(&["init"]);
    git(&["config", "user.name", "oracle"]);
    git(&["config", "user.email", "oracle@example.com"]);
    fs::write(repo.path().join(".gitignore"), b".syntext/\n.git/\n").unwrap();
    git(&["add", "."]);
    git(&["commit", "-m", "init", "--no-gpg-sign"]);

    let config = Config {
        index_dir: index_dir.clone(),
        repo_root: repo.path().to_path_buf(),
        auto_update: false,
        ..Config::default()
    };
    let index = Index::build(config).expect("build");

    // Verify initial state
    assert_st_matches_rg(repo.path(), &index_dir, "parse_query", 0).unwrap();

    // Rename src/old.rs -> src/new.rs
    let abs_old = repo.path().join("src/old.rs");
    let abs_new = repo.path().join("src/new.rs");
    fs::rename(&abs_old, &abs_new).unwrap();
    index.notify_delete(&abs_old).unwrap();
    index.notify_change(&abs_new).unwrap();
    index.commit_batch().unwrap();

    // Update git index: remove old path, add new path so st's git-walk agrees with rg.
    git(&["rm", "--cached", "--ignore-unmatch", "src/old.rs"]);
    git(&["add", "src/new.rs"]);

    // After rename: st must find the match in the new path, not the old
    assert_st_matches_rg(repo.path(), &index_dir, "parse_query", 1).unwrap();

    drop(index);
}

/// Verify that a file grown past max_file_size is excluded from results.
#[test]
fn golden_incremental_grow_past_limit() {
    if !rg_available() {
        return;
    }

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

    fs::create_dir_all(repo.path().join("src")).unwrap();
    fs::write(repo.path().join("src/main.rs"), b"fn parse_query() {}\n").unwrap();

    let git = |args: &[&str]| {
        Command::new("git")
            .arg("-C")
            .arg(repo.path())
            .args(args)
            .output()
            .ok();
    };
    git(&["init"]);
    git(&["config", "user.name", "oracle"]);
    git(&["config", "user.email", "oracle@example.com"]);
    fs::write(repo.path().join(".gitignore"), b".syntext/\n.git/\n").unwrap();
    git(&["add", "."]);
    git(&["commit", "-m", "init", "--no-gpg-sign"]);

    let max_file_size: u64 = 1024; // 1KB limit for this test
    let config = Config {
        index_dir: index_dir.clone(),
        repo_root: repo.path().to_path_buf(),
        max_file_size,
        auto_update: false,
        ..Config::default()
    };
    let index = Index::build(config).expect("build");

    // Grow past limit
    let abs_main = repo.path().join("src/main.rs");
    let oversized = vec![b'x'; (max_file_size + 1) as usize];
    fs::write(&abs_main, &oversized).unwrap();
    index.notify_change(&abs_main).unwrap();
    index.commit_batch().unwrap();

    // After growing past the limit: rg won't search oversized binary-looking file by default,
    // and st should have removed it from the index. Both should report 0 matches.
    // We use a literal that was only in the old content.
    assert_st_matches_rg(repo.path(), &index_dir, "parse_query", 1).unwrap();

    drop(index);
}