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
//! Unit tests for query decomposition and routing (T035).
//!
//! CRITICAL: The `(foo)?bar` test verifies that optional prefixes do NOT
//! contribute grams. This is not a bug -- it is required for correctness.
//! Requiring "foo" grams would produce false negatives for inputs like "bazbar".

use tempfile::TempDir;

use syntext::__internal::regex_decompose::decompose;
use syntext::__internal::{is_literal, literal_grams, route_query, GramQuery, QueryRoute};
use syntext::index::Index;
use syntext::Config;

// ---------------------------------------------------------------------------
// GramQuery::simplify tests
// ---------------------------------------------------------------------------

#[test]
fn simplify_removes_all_from_and() {
    let q = GramQuery::And(vec![
        GramQuery::All,
        GramQuery::Grams(vec![1, 2]),
        GramQuery::All,
    ]);
    match q.simplify() {
        GramQuery::Grams(hashes) => assert_eq!(hashes, vec![1, 2]),
        other => panic!("expected Grams, got {:?}", other),
    }
}

#[test]
fn simplify_all_dominates_or() {
    let q = GramQuery::Or(vec![
        GramQuery::Grams(vec![1]),
        GramQuery::All,
        GramQuery::Grams(vec![2]),
    ]);
    assert!(matches!(q.simplify(), GramQuery::All));
}

#[test]
fn simplify_empty_and_becomes_all() {
    let q = GramQuery::And(vec![]);
    assert!(matches!(q.simplify(), GramQuery::All));
}

#[test]
fn simplify_empty_or_becomes_none() {
    let q = GramQuery::Or(vec![]);
    assert!(matches!(q.simplify(), GramQuery::None));
}

#[test]
fn simplify_single_child_and_unwraps() {
    let q = GramQuery::And(vec![GramQuery::Grams(vec![42])]);
    match q.simplify() {
        GramQuery::Grams(hashes) => assert_eq!(hashes, vec![42]),
        other => panic!("expected Grams, got {:?}", other),
    }
}

#[test]
fn simplify_single_child_or_unwraps() {
    let q = GramQuery::Or(vec![GramQuery::Grams(vec![99])]);
    match q.simplify() {
        GramQuery::Grams(hashes) => assert_eq!(hashes, vec![99]),
        other => panic!("expected Grams, got {:?}", other),
    }
}

#[test]
fn simplify_and_of_all_becomes_all() {
    let q = GramQuery::And(vec![GramQuery::All, GramQuery::All]);
    assert!(matches!(q.simplify(), GramQuery::All));
}

// ---------------------------------------------------------------------------
// HIR decomposition tests
// ---------------------------------------------------------------------------

/// Regex literal "parse_query" -> All (full scan) because regex literals
/// use build_covering_inner which only emits grams with both boundaries at
/// forced boundary characters. "parse" and "query" touch synthetic 0/len
/// boundaries, so no interior grams survive.
///
/// Note: LITERAL queries (QueryRoute::Literal) still use build_covering
/// which treats 0/len as real boundaries. The distinction is intentional:
/// literal searches are complete tokens; regex literals are fragments.
#[test]
fn decompose_literal_falls_back_to_scan() {
    let q = decompose("parse_query", false).unwrap().simplify();
    // Regex literals with no interior forced-boundary grams fall to All.
    assert!(matches!(q, GramQuery::All), "expected All, got {:?}", q);
}

/// `foo.*bar` -> All because "foo" and "bar" are regex literals without
/// interior forced-boundary grams. Both fall to All, and And([All, All, All])
/// simplifies to All (full scan). This is correct: regex literals at
/// arbitrary document positions cannot safely use edge grams.
#[test]
fn decompose_dot_star_between_literals() {
    let q = decompose("foo.*bar", false).unwrap().simplify();
    assert!(
        matches!(q, GramQuery::All),
        "foo.*bar should fall to full scan with forced boundaries, got {:?}",
        q
    );
}

/// `foo|bar` -> All because "foo" and "bar" have no interior forced-boundary
/// grams. Or([All, All]) -> All (full scan).
#[test]
fn decompose_alternation() {
    let q = decompose("foo|bar", false).unwrap().simplify();
    assert!(matches!(q, GramQuery::All), "expected All, got {:?}", q);
}

#[test]
fn decompose_exact_literal_alternation_with_shared_prefix() {
    let q = decompose("LanguageServer(Id|InstallationStatus)", false)
        .unwrap()
        .simplify();
    assert!(
        matches!(q, GramQuery::Or(_) | GramQuery::Grams(_)),
        "expected indexed grams for exact literal alternation, got {:?}",
        q
    );
}

/// `(parse_query)+` -> All because the regex literal "parse_query" has no
/// interior forced-boundary grams (both "parse" and "query" touch synthetic
/// edges). Required repetition (min=1) is valid for gram constraints, but
/// there are no grams to constrain with.
#[test]
fn decompose_required_repetition() {
    let q = decompose("(parse_query)+", false).unwrap().simplify();
    assert!(
        matches!(q, GramQuery::All),
        "(parse_query)+ should fall to full scan with forced boundaries, got {:?}",
        q
    );
}

/// `.*` -> All -> FullScan route.
/// No extractable grams from a pure wildcard.
#[test]
fn decompose_dot_star_alone_is_all() {
    let q = decompose(".*", false).unwrap().simplify();
    assert!(matches!(q, GramQuery::All), "expected All, got {:?}", q);
}

/// CRITICAL: `(foo)?bar` -> Grams("bar") only.
///
/// The optional prefix `(foo)?` must NOT contribute gram constraints.
/// Requiring "foo" grams would cause false negatives for inputs like "bazbar".
/// After simplification: And([All, Grams("bar")]) -> Grams("bar").
#[test]
fn optional_prefix_does_not_contribute_grams() {
    let q = decompose("(foo)?bar", false).unwrap().simplify();

    // Must produce grams for "bar" only (not an And requiring both foo and bar).
    match &q {
        GramQuery::Grams(_) => {
            // Exactly what we want: only grams for the required part.
        }
        GramQuery::And(children) => {
            // If And, it must NOT contain any node requiring "foo" grams that
            // would miss inputs containing "bar" without "foo".
            // For this pattern, And should have been simplified away.
            panic!(
                "optional prefix produced And node (may cause false negatives): {:?}",
                children
            );
        }
        GramQuery::All => {
            // Acceptable: no grams extracted, falls back to scan (bar is short).
        }
        other => panic!("unexpected query type for (foo)?bar: {:?}", other),
    }

    // The grams produced must NOT require "foo". Verify by checking that
    // the query is NOT And(Grams(foo_hashes), Grams(bar_hashes)).
    // Since we verified it's Grams or All above, foo grams cannot be required.
}

// ---------------------------------------------------------------------------
// Query routing tests
// ---------------------------------------------------------------------------

#[test]
fn is_literal_no_metacharacters() {
    assert!(is_literal("parse_query"));
    assert!(is_literal("hello world"));
    assert!(is_literal("foo_bar_baz"));
    assert!(is_literal("some::path::to::function"));
}

#[test]
fn is_literal_with_metacharacters() {
    assert!(!is_literal("foo.*bar"));
    assert!(!is_literal("foo?bar"));
    assert!(!is_literal("[abc]"));
    assert!(!is_literal("(foo)+"));
    assert!(!is_literal("^start"));
    assert!(!is_literal("end$"));
    assert!(!is_literal("foo\\d"));
}

#[test]
fn route_literal_pattern() {
    let route = route_query("parse_query", false).unwrap();
    assert!(
        matches!(route, QueryRoute::Literal),
        "expected Literal route, got {:?}",
        route
    );
}

#[test]
fn route_regex_without_interior_grams_is_fullscan() {
    // With forced boundaries, regex literals have no interior grams, so
    // alternation of short literals falls to FullScan.
    let route = route_query("parse_query|process_batch", false).unwrap();
    assert!(
        matches!(route, QueryRoute::FullScan),
        "expected FullScan for regex without interior grams, got {:?}",
        route
    );
}

#[test]
fn route_regex_with_extractable_grams_is_indexed() {
    let route = route_query("(fn_parse_filter_query)+", false).unwrap();
    assert!(
        matches!(route, QueryRoute::IndexedRegex(_)),
        "expected IndexedRegex for extractable regex grams, got {:?}",
        route
    );
}

#[test]
fn route_exact_literal_alternation_with_shared_prefix_is_indexed() {
    let route = route_query("LanguageServer(Id|InstallationStatus)", false).unwrap();
    assert!(
        matches!(route, QueryRoute::IndexedRegex(_)),
        "expected IndexedRegex for exact literal alternation, got {:?}",
        route
    );
}

#[test]
fn route_full_scan_for_dot_star() {
    let route = route_query(".*", false).unwrap();
    assert!(
        matches!(route, QueryRoute::FullScan),
        "expected FullScan for .*, got {:?}",
        route
    );
}

#[test]
fn route_case_insensitive_literal_not_literal_route() {
    // Case-insensitive search can't use memmem, but long literals should still
    // extract covering grams from the lowercased index.
    let route = route_query("_parse_query_", true).unwrap();
    assert!(
        matches!(route, QueryRoute::IndexedRegex(_)),
        "case-insensitive anchored long literal should use IndexedRegex, got {:?}",
        route
    );
}

#[test]
fn literal_grams_returns_none_for_short_pattern() {
    assert!(literal_grams("ab").is_none());
    assert!(literal_grams("x").is_none());
}

#[test]
fn literal_grams_returns_some_for_long_pattern() {
    assert!(literal_grams("parse_query").is_some());
}

#[test]
fn route_case_insensitive_short_literal_falls_back_to_full_scan() {
    let route = route_query("ab", true).unwrap();
    assert!(
        matches!(route, QueryRoute::FullScan),
        "short case-insensitive literal should fall back to FullScan, got {:?}",
        route
    );
}

/// Case-insensitive literal whose only grams are optional (synthetic-edge,
/// unanchored) must route to FullScan, not IndexedRegex(Grams(...)). ANDing the
/// optional gram silently drops documents where the match is sub-token
/// (e.g. `-i parse` missing `reparse` when another file emits `parse` as a
/// token-aligned gram). FullScan lets the case-insensitive regex verifier see
/// every candidate. Regression for the case-insensitive AND-intersection bug.
#[test]
fn route_case_insensitive_optional_only_literal_is_full_scan() {
    let route = route_query("parse", true).unwrap();
    assert!(
        matches!(route, QueryRoute::FullScan),
        "case-insensitive 'parse' (optional-only grams) must route to FullScan, got {:?}",
        route
    );
}

/// A regex escaped-backslash-then-`n` (`\\n`, i.e. a literal backslash followed
/// by the letter n) is NOT a newline and must route, not error. This is what a
/// fixed string like `C:\new` becomes after `regex::escape`, and what the review
/// case `foo\\nbar` is. The old `contains("\\n")` string guard wrongly rejected
/// both because their source text contains the two bytes `\` `n`. Only a raw
/// newline byte, or a genuine `\n` regex newline escape, may be rejected.
#[test]
fn route_backslash_n_is_not_a_newline() {
    // `foo\\nbar` as a regex: literal `\`, then `nbar`. No newline -> Ok.
    assert!(
        route_query(r"foo\\nbar", false).is_ok(),
        "escaped-backslash-then-n must not be treated as a literal newline"
    );
    // The escaped form of the fixed string `C:\new` (`C:\\new`) -> Ok.
    assert!(
        route_query(r"C:\\new", false).is_ok(),
        "regex-escaped `C:\\new` must route, not error"
    );
    // A raw newline byte is still rejected.
    assert!(
        route_query("a\nb", false).is_err(),
        "a raw newline byte must still be rejected"
    );
    // A genuine `\n` regex newline escape is still rejected (HIR check).
    assert!(
        route_query(r"a\nb", false).is_err(),
        "a `\\n` regex newline escape must still be rejected"
    );
}

// ---------------------------------------------------------------------------
// EH-0001: literal-substring false negatives from synthetic token-edge grams
// ---------------------------------------------------------------------------

/// A literal query for "parse" must find files containing sub-token matches
/// like "reparse", not just files where "parse" is a token-aligned gram.
#[test]
fn eh0001_literal_parse_finds_subtoken_reparse() {
    let repo = TempDir::new().unwrap();
    let index_dir = TempDir::new().unwrap();

    // File A: "reparse" — parse appears as a substring (not token-aligned).
    std::fs::write(repo.path().join("a.rs"), "fn reparse() {}\n").unwrap();
    // File B: "parse" — parse IS token-aligned.
    std::fs::write(repo.path().join("b.rs"), "fn parse() {}\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 results = index.search("parse", &Default::default()).unwrap();

    let mut paths: Vec<String> = results
        .iter()
        .map(|m| m.path.file_name().unwrap().to_string_lossy().to_string())
        .collect();
    paths.sort();
    paths.dedup();

    assert_eq!(
        paths,
        vec!["a.rs", "b.rs"],
        "literal 'parse' must find both 'reparse' (a.rs) and 'parse' (b.rs)"
    );
    drop(index);
}

/// Insertion order must not affect results: whether reparse or parse is
/// indexed first, both files must appear.
#[test]
fn eh0001_literal_parse_insertion_order_independent() {
    for first in &["reparse", "parse"] {
        let repo = TempDir::new().unwrap();
        let index_dir = TempDir::new().unwrap();

        // Index the files in a known order within a single build.
        let content_a = if *first == "reparse" {
            "fn reparse() {}\nfn parse_it() {}\n"
        } else {
            "fn parse() {}\nfn reparsing() {}\n"
        };
        std::fs::write(repo.path().join("lib.rs"), content_a).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 results = index.search("parse", &Default::default()).unwrap();

        assert!(
            !results.is_empty(),
            "parse must produce results regardless of insertion order (tried {first} first)"
        );
        drop(index);
    }
}

/// build_covering("parse") must classify its single gram as optional
/// (unanchored) and produce zero required grams.
#[test]
fn eh0001_build_covering_classifies_parse_as_optional_only() {
    let covering = syntext::__internal::build_covering(b"parse").unwrap();
    assert!(
        covering.required.is_empty(),
        "parse has no interior boundaries: all grams must be optional"
    );
    assert_eq!(
        covering.optional.len(),
        1,
        "parse must produce exactly one optional gram"
    );
}

/// build_covering("parse_query") must classify both grams as optional
/// because each has a synthetic boundary (start or end of query).
#[test]
fn eh0001_build_covering_classifies_parse_query_as_optional() {
    let covering = syntext::__internal::build_covering(b"parse_query").unwrap();
    assert!(
        covering.required.is_empty(),
        "parse_query has synthetic edges; grams must be optional"
    );
    assert_eq!(
        covering.optional.len(),
        2,
        "parse_query must produce exactly two optional grams"
    );
}

/// build_covering("_parse_query_") must classify both grams as required
/// because both are anchored by forced boundaries (start, end, and middle are all '_').
#[test]
fn eh0001_build_covering_classifies_anchored_parse_query_as_required() {
    let covering = syntext::__internal::build_covering(b"_parse_query_").unwrap();
    assert_eq!(
        covering.required.len(),
        2,
        "anchored parse_query must produce required grams"
    );
    assert!(
        covering.optional.is_empty(),
        "anchored parse_query should not have optional grams"
    );
}

/// Case-insensitive counterpart of `eh0001_literal_parse_finds_subtoken_reparse`.
/// `-i parse` must find `reparse` (sub-token) even when another file emits the
/// `parse` gram token-aligned. Previously the case-insensitive route AND-intersected
/// the optional `parse` gram, returned a non-empty single-doc candidate set, and
/// silently dropped the sub-token file.
#[test]
fn eh0001_case_insensitive_literal_finds_subtoken_reparse() {
    let repo = TempDir::new().unwrap();
    let index_dir = TempDir::new().unwrap();

    // a.rs: `parse` appears only as a sub-token of `reparse`.
    std::fs::write(repo.path().join("a.rs"), "fn reparse() {}\n").unwrap();
    // b.rs: `PARSE` lowercases to the token-aligned `parse` gram.
    std::fs::write(repo.path().join("b.rs"), "fn PARSE() {}\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 mut opts = syntext::SearchOptions::default();
    opts.case_insensitive = true;
    let results = index.search("parse", &opts).unwrap();

    let mut paths: Vec<String> = results
        .iter()
        .map(|m| m.path.file_name().unwrap().to_string_lossy().to_string())
        .collect();
    paths.sort();
    paths.dedup();

    assert_eq!(
        paths,
        vec!["a.rs", "b.rs"],
        "case-insensitive 'parse' must find both reparse (a.rs) and PARSE (b.rs)"
    );
    drop(index);
}

#[test]
fn route_query_rejects_newlines() {
    assert_eq!(
        route_query("foo\nbar", false).unwrap_err(),
        "literal \\n not allowed".to_string()
    );
    assert_eq!(
        route_query("foo\\nbar", false).unwrap_err(),
        "literal \\n not allowed".to_string()
    );
    assert_eq!(
        route_query("foo\\x0abar", false).unwrap_err(),
        "literal \\n not allowed".to_string()
    );
    assert_eq!(
        route_query("foo\\u000abar", false).unwrap_err(),
        "literal \\n not allowed".to_string()
    );
    assert_eq!(
        route_query("foo\\u{a}bar", false).unwrap_err(),
        "literal \\n not allowed".to_string()
    );
}