semantex-core 1.0.3

Core library for semantex semantic code search (indexing, embeddings, search)
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
use std::collections::HashMap;
use std::sync::LazyLock;

/// Stem a word using the Snowball English stemmer.
fn stem(word: &str) -> String {
    use rust_stemmers::{Algorithm, Stemmer};
    let stemmer = Stemmer::create(Algorithm::English);
    stemmer.stem(word).into_owned()
}

/// Split a programming identifier into NL-ish lowercased subwords.
///
/// Handles camelCase, PascalCase, snake_case, kebab-case, dot.path, and
/// `::`-separated paths. Returns `None` if the token isn't an identifier
/// (no alpha-numeric content) or doesn't decompose into multiple subwords.
///
/// Examples:
/// - `getUserById`     → `["get", "user", "by", "id"]`
/// - `RetryPolicy`     → `["retry", "policy"]`
/// - `max_retry_count` → `["max", "retry", "count"]`
/// - `HTTPSConnection` → `["https", "connection"]`
/// - `user`            → `None` (single word — nothing to split)
/// - `the`             → `None` (no internal boundary signal)
///
/// E4: surfacing subwords lets the BM25 expanded-query channel match
/// NL phrasing that would otherwise miss compound identifiers.
pub fn split_identifier(token: &str) -> Option<Vec<String>> {
    // Need some alphanumeric content.
    if !token.chars().any(char::is_alphanumeric) {
        return None;
    }

    // Pre-split on common separators (snake_case, kebab-case, dots, ::, slashes).
    // We treat any sequence of non-alphanumerics as a boundary.
    let mut pieces: Vec<&str> = token
        .split(|c: char| !c.is_alphanumeric())
        .filter(|s| !s.is_empty())
        .collect();

    // If no separator-driven splits happened, the single token still needs
    // camelCase/PascalCase decomposition.
    let mut subwords: Vec<String> = Vec::new();
    if pieces.len() == 1 {
        subwords.extend(split_camel_case(pieces.remove(0)));
    } else {
        for piece in pieces {
            subwords.extend(split_camel_case(piece));
        }
    }

    if subwords.len() < 2 {
        return None;
    }
    Some(subwords)
}

/// Split a single mixed-case run into lowercased subwords.
/// Handles camelCase, PascalCase, and ALL-CAPS-followed-by-camelCase runs
/// (e.g. `HTTPSConnection` → `https connection`).
fn split_camel_case(run: &str) -> Vec<String> {
    if run.is_empty() {
        return Vec::new();
    }

    let chars: Vec<char> = run.chars().collect();
    let mut boundaries: Vec<usize> = vec![0];

    for i in 1..chars.len() {
        let prev = chars[i - 1];
        let curr = chars[i];

        // Standard camelCase: lowercase/digit followed by uppercase.
        let lower_to_upper = (prev.is_lowercase() || prev.is_ascii_digit()) && curr.is_uppercase();

        // Acronym boundary: uppercase followed by uppercase-then-lowercase
        // (e.g. `HTTPSConnection` → split between `HTTPS` and `Connection`).
        let acronym_boundary = prev.is_uppercase()
            && curr.is_uppercase()
            && i + 1 < chars.len()
            && chars[i + 1].is_lowercase();

        // Alpha-numeric transition: letter to digit or digit to letter.
        let alpha_digit = (prev.is_alphabetic() && curr.is_ascii_digit())
            || (prev.is_ascii_digit() && curr.is_alphabetic());

        if lower_to_upper || acronym_boundary || alpha_digit {
            boundaries.push(i);
        }
    }
    boundaries.push(chars.len());

    let mut out = Vec::with_capacity(boundaries.len().saturating_sub(1));
    for window in boundaries.windows(2) {
        let piece: String = chars[window[0]..window[1]]
            .iter()
            .collect::<String>()
            .to_lowercase();
        if !piece.is_empty() {
            out.push(piece);
        }
    }
    out
}

/// Build the stemmed synonym table: keys are stemmed (single words or
/// space-joined stemmed multi-word phrases) so that inflected query words
/// match after stemming.
fn build_stemmed_synonyms(
    raw: &'static [(&'static str, &'static [&'static str])],
) -> HashMap<String, Vec<&'static str>> {
    let mut m: HashMap<String, Vec<&'static str>> = HashMap::new();
    for &(key, values) in raw {
        let stemmed_key: String = key
            .split_whitespace()
            .map(stem)
            .collect::<Vec<_>>()
            .join(" ");
        m.insert(stemmed_key, values.to_vec());
    }
    m
}

/// Static synonym/expansion table mapping NL concepts to code tokens.
/// Used to bridge the vocabulary gap between natural-language queries and
/// code identifiers for BM25 retrieval.
/// Keys are stemmed at init time so inflected query words (e.g. "encrypting",
/// "classification") match after stemming.
static SYNONYMS: LazyLock<HashMap<String, Vec<&'static str>>> =
    LazyLock::new(|| build_stemmed_synonyms(RAW_SYNONYMS));

/// Raw synonym definitions. Keys are canonical NL forms; values are code tokens
/// to inject into the BM25 query.
const RAW_SYNONYMS: &[(&str, &[&str])] = &[
    // Concurrency patterns
    (
        "parallel",
        &[
            "concurrent",
            "async",
            "Promise.all",
            "allSettled",
            "tokio::spawn",
            "rayon",
            "par_iter",
            "ThreadPool",
            "ExecutorService",
        ],
    ),
    (
        "concurrent",
        &["parallel", "async", "thread", "mutex", "lock", "atomic"],
    ),
    (
        "async",
        &["await", "Future", "Promise", "Task", "coroutine"],
    ),
    // Error handling
    (
        "failure",
        &["error", "Error", "Result::Err", "reject", "anyhow"],
    ),
    (
        "parallel failure",
        &["Promise.allSettled", "allSettled", "Promise.all", "settled"],
    ),
    (
        "error handling",
        &[
            "try",
            "catch",
            "Result",
            "Option",
            "unwrap_or",
            "map_err",
            "context",
            "bail",
        ],
    ),
    (
        "recovery",
        &[
            "retry",
            "restart",
            "resume",
            "restore",
            "failover",
            "resilience",
            "fallback",
        ],
    ),
    (
        "fallback",
        &["default", "backup", "alternative", "rescue", "degraded"],
    ),
    (
        "retry",
        &[
            "backoff",
            "exponential",
            "retryWithBackoff",
            "max_retries",
            "attempt",
            "retry_policy",
        ],
    ),
    // Networking / connections
    (
        "connection",
        &["connect", "disconnect", "reconnect", "pool", "WebSocket"],
    ),
    (
        "lifecycle",
        &[
            "shutdown",
            "dispose",
            "cleanup",
            "close",
            "destroy",
            "initiate",
            "establish",
            "teardown",
            "open",
            "refresh",
            "sync",
        ],
    ),
    (
        "connection lifecycle",
        &[
            "connect",
            "disconnect",
            "reconnect",
            "connection_pool",
            "keep_alive",
            "heartbeat",
            "idle_timeout",
        ],
    ),
    // PII / sanitization
    (
        "pii",
        &[
            "sanitize",
            "redact",
            "mask",
            "scrub",
            "anonymize",
            "sensitive",
        ],
    ),
    (
        "redact",
        &[
            "sanitize",
            "mask",
            "scrub",
            "anonymize",
            "censor",
            "obfuscate",
            "strip",
        ],
    ),
    // Auth patterns
    (
        "authentication",
        &[
            "auth",
            "login",
            "logout",
            "session",
            "token",
            "JWT",
            "OAuth",
            "passport",
            "credential",
        ],
    ),
    (
        "authorization",
        &[
            "permission",
            "role",
            "access_control",
            "ACL",
            "RBAC",
            "guard",
            "policy",
        ],
    ),
    // Data patterns
    (
        "validation",
        &[
            "validate",
            "schema",
            "check",
            "verify",
            "sanitize",
            "constraint",
            "Zod",
            "Joi",
            "pydantic",
        ],
    ),
    (
        "serialization",
        &[
            "serialize",
            "deserialize",
            "marshal",
            "unmarshal",
            "encode",
            "decode",
            "serde",
            "JSON.parse",
            "JSON.stringify",
        ],
    ),
    (
        "caching",
        &[
            "cache",
            "memoize",
            "memo",
            "Redis",
            "LRU",
            "TTL",
            "invalidate",
            "cache_control",
        ],
    ),
    // Common programming patterns
    (
        "sql",
        &[
            "SELECT",
            "WHERE",
            "INSERT",
            "QueryBuilder",
            "ORM",
            "knex",
            "prisma",
        ],
    ),
    (
        "query builder",
        &["QueryBuilder", "buildQuery", "createQuery", "parameterized"],
    ),
    (
        "encryption",
        &["encrypt", "decrypt", "cipher", "KMS", "aes", "crypto"],
    ),
    (
        "token",
        &[
            "JWT",
            "accessToken",
            "refreshToken",
            "bearer",
            "OAuth",
            "credential",
        ],
    ),
    (
        "factory",
        &["Factory", "Provider", "builder", "AbstractFactory"],
    ),
    (
        "interceptor",
        &["middleware", "hook", "filter", "guard", "Interceptor"],
    ),
    ("sync", &["synchronize", "replicate", "mirror", "reconcile"]),
    // Architecture
    (
        "middleware",
        &[
            "interceptor",
            "filter",
            "guard",
            "pipe",
            "beforeEach",
            "use",
            "handler",
        ],
    ),
    (
        "dependency injection",
        &[
            "inject",
            "provider",
            "container",
            "IoC",
            "DI",
            "service_locator",
            "@Injectable",
        ],
    ),
    (
        "state management",
        &[
            "store", "reducer", "dispatch", "action", "selector", "Riverpod", "BLoC", "Redux",
            "Vuex",
        ],
    ),
    // Testing
    (
        "mock",
        &[
            "stub",
            "fake",
            "spy",
            "double",
            "mockito",
            "jest.mock",
            "patch",
            "monkeypatch",
        ],
    ),
    (
        "test",
        &[
            "spec",
            "assert",
            "expect",
            "should",
            "describe",
            "it",
            "#[test]",
            "def test_",
        ],
    ),
    // E4 — additional universal code↔NL pairs. Each entry passes:
    //   (1) Used across multiple languages
    //   (2) Helps a random project (not test-repo metadata)
    //   (3) Specific enough to avoid false positives
    //   (4) Not in the over-generic banned list (per CLAUDE.md)
    (
        "thread safety",
        &[
            "Mutex",
            "RwLock",
            "atomic",
            "Arc",
            "synchronized",
            "lock",
            "guarded",
            "thread_local",
        ],
    ),
    (
        "race condition",
        &[
            "atomic",
            "compare_and_swap",
            "CAS",
            "Mutex",
            "ordering",
            "fence",
            "happens_before",
        ],
    ),
    (
        "memory leak",
        &[
            "leak", "drop", "Drop", "dispose", "Weak", "finalize", "refcount", "free",
        ],
    ),
    (
        "timeout",
        &[
            "deadline",
            "expires",
            "TTL",
            "Duration",
            "cancel",
            "abort",
            "WithTimeout",
            "set_timeout",
        ],
    ),
    (
        "logging",
        &[
            "log",
            "logger",
            "info!",
            "debug!",
            "warn!",
            "tracing",
            "println",
            "console.log",
            "Logger",
            "slf4j",
        ],
    ),
    (
        "rate limit",
        &[
            "throttle",
            "debounce",
            "quota",
            "limiter",
            "RateLimiter",
            "max_per_second",
            "bucket",
        ],
    ),
    (
        "iterator",
        &[
            "iter",
            "Iterator",
            "next",
            "for_each",
            "foreach",
            "enumerate",
            "Iter",
            "yield",
        ],
    ),
    (
        "pagination",
        &[
            "paginate",
            "page",
            "offset",
            "limit",
            "cursor",
            "next_page",
            "page_size",
            "has_more",
        ],
    ),
    (
        "subscription",
        &[
            "subscribe",
            "unsubscribe",
            "publisher",
            "subscriber",
            "observable",
            "listen",
            "emit",
        ],
    ),
    (
        "event handler",
        &[
            "on_event",
            "handle",
            "listener",
            "callback",
            "addEventListener",
            "EventHandler",
            "subscribe",
        ],
    ),
    (
        "graceful shutdown",
        &[
            "shutdown",
            "SIGTERM",
            "SIGINT",
            "cleanup",
            "drain",
            "close",
            "abort_handle",
            "cancellation",
        ],
    ),
    (
        "feature flag",
        &[
            "feature_flag",
            "flag",
            "toggle",
            "rollout",
            "experiment",
            "if_enabled",
            "is_enabled",
        ],
    ),
    (
        "circuit breaker",
        &[
            "circuit_breaker",
            "CircuitBreaker",
            "breaker",
            "half_open",
            "trip",
            "failover",
        ],
    ),
    (
        "background job",
        &[
            "worker",
            "job",
            "queue",
            "scheduler",
            "cron",
            "tokio::spawn",
            "BackgroundService",
            "celery",
        ],
    ),
    (
        "telemetry",
        &[
            "trace",
            "span",
            "metric",
            "OpenTelemetry",
            "OTEL",
            "Histogram",
            "Counter",
            "instrument",
        ],
    ),
];

/// Expand a semantic query with relevant code synonyms.
///
/// Returns `None` if no expansion is applicable (query is a single word
/// with no internal identifier structure and no synonym matches).
///
/// Query words are stemmed before lookup so that inflected forms
/// (e.g. "encrypting", "classification", "tokens") match canonical keys
/// (e.g. "encryption", "classify", "token").
///
/// E4 — additionally splits identifier-like tokens (camelCase, snake_case)
/// into NL subwords (`getUserById` → `get user by id`) so that queries
/// containing compound identifiers also surface natural-language phrasing.
pub fn expand_query(query: &str) -> Option<String> {
    // Use the original (case-preserving) tokens for identifier splitting,
    // but lowercase for synonym lookup.
    let raw_words: Vec<&str> = query.split_whitespace().collect();
    let query_lower = query.to_lowercase();
    let words: Vec<&str> = query_lower.split_whitespace().collect();

    // E4 identifier-split additions — collected before the early-return so
    // single-token identifier-like queries (e.g. "getUserById") still expand.
    let mut id_expansions: Vec<String> = Vec::new();
    for raw in &raw_words {
        if let Some(subwords) = split_identifier(raw) {
            id_expansions.extend(subwords);
        }
    }

    // For single-word queries: only expand if identifier-splitting produced
    // something. Synonym lookup needs at least two tokens to consider
    // multi-word matches; single-word synonym lookup would be too broad.
    if words.len() < 2 && id_expansions.is_empty() {
        return None;
    }

    // Stem each word once for reuse in lookups
    let stemmed_words: Vec<String> = words.iter().map(|w| stem(w)).collect();

    let mut expansions: Vec<&str> = Vec::new();

    // Try multi-word matches first (higher priority), using stemmed words.
    // Also try the raw (unstemmed) form as a fallback — some multi-word keys
    // may not round-trip perfectly through per-word stemming.
    let max_window = stemmed_words.len().min(3);
    for window_size in (2..=max_window).rev() {
        for (i, window) in stemmed_words.windows(window_size).enumerate() {
            let phrase = window.join(" ");
            if let Some(synonyms) = SYNONYMS.get(&phrase) {
                expansions.extend(synonyms.iter());
            } else {
                // Fallback: try raw (unstemmed, lowercase) window
                let raw_phrase = words[i..i + window_size].join(" ");
                if let Some(synonyms) = SYNONYMS.get(&raw_phrase) {
                    expansions.extend(synonyms.iter());
                }
            }
        }
    }

    // Then single-word matches using stemmed words
    for stemmed in &stemmed_words {
        if let Some(synonyms) = SYNONYMS.get(stemmed.as_str()) {
            expansions.extend(synonyms.iter());
        }
    }

    if expansions.is_empty() && id_expansions.is_empty() {
        return None;
    }

    // Deduplicate string-slice expansions
    expansions.sort_unstable();
    expansions.dedup();

    // Deduplicate identifier subword expansions
    id_expansions.sort();
    id_expansions.dedup();

    // Combine: original query + synonym tokens + identifier subwords.
    // The order doesn't affect BM25 outcomes; we put synonyms before
    // subwords for readability.
    let mut tail: Vec<String> = expansions.iter().map(|s| (*s).to_string()).collect();
    tail.extend(id_expansions);

    Some(format!("{} {}", query, tail.join(" ")))
}

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

    #[test]
    fn expand_parallel_failure_handling() {
        let result = expand_query("parallel failure handling").expect("should expand");
        // Multi-word "parallel failure" match (stemmed: "parallel failur")
        assert!(
            result.contains("Promise.allSettled"),
            "should contain Promise.allSettled"
        );
        assert!(result.contains("allSettled"), "should contain allSettled");
        assert!(result.contains("settled"), "should contain settled");
        // Single-word "parallel" match
        assert!(result.contains("concurrent"), "should contain concurrent");
        // Single-word "failure" match (stemmed "failur" matches key "failure" → "failur")
        assert!(result.contains("error"), "should contain error");
        assert!(result.contains("reject"), "should contain reject");
    }

    #[test]
    fn expand_connection_lifecycle() {
        let result = expand_query("connection lifecycle").expect("should expand");
        assert!(result.contains("connect"), "should contain connect");
        assert!(result.contains("disconnect"), "should contain disconnect");
        assert!(
            result.contains("heartbeat"),
            "should contain heartbeat from multi-word match"
        );
    }

    #[test]
    fn single_word_returns_none() {
        assert!(expand_query("x").is_none());
    }

    #[test]
    fn no_matching_synonyms_returns_none() {
        assert!(expand_query("find the bug").is_none());
    }

    #[test]
    fn expansions_are_deduplicated() {
        let result = expand_query("parallel concurrent tasks").expect("should expand");
        // "async" appears in both "parallel" and "concurrent" synonym lists
        let async_count = result.matches("async").count();
        // The original query doesn't contain "async", so it should appear exactly once
        assert_eq!(
            async_count, 1,
            "async should appear exactly once (deduplicated)"
        );
    }

    #[test]
    fn stemmed_lookup_encrypting_matches_encryption() {
        // "encrypting" stems to "encrypt", same as "encryption" → key matches
        let result = expand_query("encrypting sensitive data").expect("should expand");
        assert!(
            result.contains("KMS"),
            "should contain KMS from encryption synonyms"
        );
        assert!(result.contains("cipher"), "should contain cipher");
        assert!(result.contains("decrypt"), "should contain decrypt");
    }

    #[test]
    fn stemmed_lookup_tokens_matches_token() {
        // "tokens" stems to "token", matching the "token" key
        let result = expand_query("OAuth tokens refresh").expect("should expand");
        assert!(
            result.contains("JWT"),
            "should contain JWT from token synonyms"
        );
        assert!(
            result.contains("refreshToken"),
            "should contain refreshToken"
        );
    }

    #[test]
    fn stemmed_lookup_classifying_matches_synonyms() {
        // "classifying" stems to "classifi" — no direct key, but validates no crash
        // "validation" stems to "valid" which matches "validation" key → "valid"
        let result = expand_query("validating input data");
        assert!(
            result.is_some(),
            "validating should match validation key via stemming"
        );
        let r = result.expect("should expand");
        assert!(
            r.contains("sanitize"),
            "should contain sanitize from validation synonyms"
        );
    }

    // =================================================================
    // E4 — split_identifier (camelCase / snake_case decomposition)
    // =================================================================

    #[test]
    fn split_camel_case_basic() {
        let parts = split_identifier("getUserById").expect("should split");
        assert_eq!(parts, vec!["get", "user", "by", "id"]);
    }

    #[test]
    fn split_pascal_case() {
        let parts = split_identifier("RetryPolicy").expect("should split");
        assert_eq!(parts, vec!["retry", "policy"]);
    }

    #[test]
    fn split_snake_case() {
        let parts = split_identifier("max_retry_count").expect("should split");
        assert_eq!(parts, vec!["max", "retry", "count"]);
    }

    #[test]
    fn split_acronym_then_word() {
        // HTTPS|Connection — the boundary is between the last upper of the
        // acronym run and the first upper of the next word.
        let parts = split_identifier("HTTPSConnection").expect("should split");
        assert_eq!(parts, vec!["https", "connection"]);
    }

    #[test]
    fn split_alpha_digit() {
        // Words containing digits should split at the alpha-digit boundary.
        let parts = split_identifier("OAuth2Provider").expect("should split");
        // O|Auth|2|Provider — alpha-digit and lower-upper boundaries
        assert!(parts.contains(&"o".to_string()));
        assert!(parts.contains(&"auth".to_string()));
        assert!(parts.contains(&"2".to_string()));
        assert!(parts.contains(&"provider".to_string()));
    }

    #[test]
    fn split_kebab_case() {
        let parts = split_identifier("retry-with-backoff").expect("should split");
        assert_eq!(parts, vec!["retry", "with", "backoff"]);
    }

    #[test]
    fn split_dot_path() {
        let parts = split_identifier("com.example.UserService").expect("should split");
        assert_eq!(parts, vec!["com", "example", "user", "service"]);
    }

    #[test]
    fn split_double_colon_path() {
        let parts = split_identifier("std::io::Read").expect("should split");
        assert_eq!(parts, vec!["std", "io", "read"]);
    }

    #[test]
    fn split_single_lowercase_word_returns_none() {
        // A plain English word doesn't decompose — must return None
        assert!(split_identifier("user").is_none());
        assert!(split_identifier("the").is_none());
    }

    #[test]
    fn split_non_alphanumeric_returns_none() {
        assert!(split_identifier("---").is_none());
        assert!(split_identifier("").is_none());
    }

    // =================================================================
    // E4 — expand_query: identifier splitting integration
    // =================================================================

    #[test]
    fn expand_query_splits_identifier_in_multiword() {
        // A query mixing prose and a compound identifier should include the
        // subwords as expansions.
        let result = expand_query("how does getUserById work").expect("should expand");
        // Identifier subwords appear
        assert!(result.contains(" get "), "subword 'get' should appear");
        assert!(result.contains(" user"), "subword 'user' should appear");
        assert!(result.contains(" by "), "subword 'by' should appear");
        assert!(result.contains(" id"), "subword 'id' should appear");
    }

    #[test]
    fn expand_query_splits_single_identifier_only() {
        // Single-word non-identifier still returns None.
        assert!(expand_query("hello").is_none());
        // Single-word identifier IS now expanded (camelCase subwords).
        let result = expand_query("RetryPolicy");
        assert!(
            result.is_some(),
            "single-word identifier should expand via subwords"
        );
        let r = result.expect("should expand");
        assert!(r.contains("retry"));
        assert!(r.contains("policy"));
    }

    // =================================================================
    // E4 — new universal NL pairs (sanity)
    // =================================================================

    #[test]
    fn expand_thread_safety_pair() {
        let r = expand_query("thread safety concerns").expect("should expand");
        assert!(r.contains("Mutex"));
        assert!(r.contains("atomic"));
    }

    #[test]
    fn expand_rate_limit_pair() {
        let r = expand_query("rate limit api calls").expect("should expand");
        assert!(r.contains("throttle"));
        assert!(r.contains("RateLimiter"));
    }

    #[test]
    fn expand_graceful_shutdown_pair() {
        let r = expand_query("graceful shutdown sequence").expect("should expand");
        assert!(r.contains("SIGTERM"));
        assert!(r.contains("drain"));
    }

    #[test]
    fn expand_memory_leak_pair() {
        let r = expand_query("debug memory leak").expect("should expand");
        assert!(r.contains("Weak"));
        assert!(r.contains("dispose"));
    }

    #[test]
    fn expand_telemetry_pair() {
        let r = expand_query("add telemetry to handler").expect("should expand");
        assert!(r.contains("trace"));
        assert!(r.contains("OpenTelemetry"));
    }

    #[test]
    fn expand_dual_route_differs_from_single_route() {
        // E4 design intent: expansion meaningfully changes the BM25 query
        // text. Verify that the expanded form is a strict superset of the
        // original (original is preserved as prefix) and adds tokens not
        // present in the input.
        let original = "thread safety check";
        let expanded = expand_query(original).expect("should expand");
        assert!(
            expanded.starts_with(original),
            "expanded should preserve original prefix"
        );
        // Expanded has more than just the original
        assert!(
            expanded.len() > original.len(),
            "expanded should be strictly longer than original"
        );
        // Specific token from synonyms
        assert!(expanded.contains("Mutex"));
    }
}