semantex-core 1.1.0

Core library for semantex semantic code search (indexing, embeddings, search)
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
//! Rule-based semantic role classification for code chunks.
//!
//! Classifies functions/methods by their behavioral purpose (constructor, validator,
//! transformer, etc.) using name patterns and structural signals.
//!
//! Also hosts `synthesize_nl_annotation` — the E3 ExCS-style chunk annotation
//! synthesizer (see v0.3 SOTA spec). It produces a rule-derived natural-language
//! block from existing signals (no LLM call) that is indexed in Tantivy's
//! `nl_annotation` field to close the NL → code vocabulary gap.

use super::structured_meta::{
    SemanticRole, StructuredChunkMeta, expand_identifier, is_trivial_call,
};

/// Classify a chunk's semantic role based on name patterns and structural signals.
///
/// Returns `None` if no role can be confidently determined.
#[allow(clippy::too_many_lines)]
pub fn classify_semantic_role(meta: &StructuredChunkMeta, _content: &str) -> Option<SemanticRole> {
    let raw_name = meta.name.as_deref()?;
    let name = raw_name.to_lowercase();
    let expanded = expand_identifier(raw_name).to_lowercase();

    // Priority ordering: more specific patterns first

    // Sanitizer: sanitize, redact, mask, strip_pii, clean, scrub
    if matches_any(
        &expanded,
        &[
            "sanitiz",
            "redact",
            "mask pii",
            "strip pii",
            "clean pii",
            "scrub",
        ],
    ) {
        return Some(SemanticRole::Sanitizer);
    }
    // Be careful with "mask", "clean", "strip" alone -- too generic.
    // Only match if combined with sanitization context.
    if matches_any(&name, &["sanitize", "redact", "scrub"]) {
        return Some(SemanticRole::Sanitizer);
    }

    // ErrorHandler: catch, recover, fallback, retry, on_error
    if matches_any(
        &expanded,
        &[
            "error handler",
            "catch error",
            "recover",
            "fallback",
            "retry",
        ],
    ) {
        return Some(SemanticRole::ErrorHandler);
    }
    if name.starts_with("on_error") || name.starts_with("handle_error") {
        return Some(SemanticRole::ErrorHandler);
    }

    // Middleware: intercept, filter, guard, middleware
    if matches_any(
        &expanded,
        &[
            "intercept",
            "middlewar",
            "guard request",
            "before request",
            "after request",
        ],
    ) {
        return Some(SemanticRole::Middleware);
    }
    if matches_any(&name, &["interceptor", "middleware"]) {
        return Some(SemanticRole::Middleware);
    }

    // Constructor: new, create, init, build, make, setup, from
    if matches_any(&name, &["new", "create", "init", "build", "make", "setup"])
        || name.starts_with("create_")
        || name.starts_with("build_")
        || name.starts_with("new_")
        || name.starts_with("from_")
        || expanded.starts_with("create ")
        || expanded.starts_with("build ")
    {
        return Some(SemanticRole::Constructor);
    }

    // Destructor: drop, close, dispose, shutdown, destroy, cleanup, teardown
    if matches_any(
        &expanded,
        &[
            "close",
            "dispos",
            "shutdown",
            "shut down",
            "destroy",
            "cleanup",
            "clean up",
            "teardown",
            "tear down",
        ],
    ) {
        return Some(SemanticRole::Destructor);
    }
    if name == "drop" || name.starts_with("drop_") {
        return Some(SemanticRole::Destructor);
    }

    // Validator: validate, check, verify, assert, ensure, is_valid
    if matches_any(&expanded, &["validat", "verif", "is valid"]) {
        return Some(SemanticRole::Validator);
    }
    if name.starts_with("check_") || name.starts_with("ensure_") || name.starts_with("assert_") {
        return Some(SemanticRole::Validator);
    }

    // Transformer: convert, transform, serialize, deserialize, parse, format, encode, decode
    if matches_any(
        &expanded,
        &[
            "convert",
            "transform",
            "serializ",
            "deserializ",
            "encod",
            "decod",
        ],
    ) {
        return Some(SemanticRole::Transformer);
    }
    if name.starts_with("to_")
        || name.starts_with("into_")
        || name.starts_with("parse_")
        || name.starts_with("format_")
    {
        return Some(SemanticRole::Transformer);
    }

    // Fetcher: get, fetch, load, read, find, search, retrieve, lookup
    if matches_any(&expanded, &["fetch", "retriev", "lookup", "look up"]) {
        return Some(SemanticRole::Fetcher);
    }
    if name.starts_with("get_")
        || name.starts_with("load_")
        || name.starts_with("read_")
        || name.starts_with("find_")
    {
        return Some(SemanticRole::Fetcher);
    }

    // Persister: save, store, write, insert, update, delete, put, persist, upsert
    if matches_any(&expanded, &["persist", "upsert"]) {
        return Some(SemanticRole::Persister);
    }
    if name.starts_with("save_")
        || name.starts_with("store_")
        || name.starts_with("write_")
        || name.starts_with("insert_")
        || name.starts_with("delete_")
        || name.starts_with("put_")
    {
        return Some(SemanticRole::Persister);
    }
    if matches_any(
        &name,
        &[
            "save", "store", "write", "insert", "delete", "persist", "upsert",
        ],
    ) {
        return Some(SemanticRole::Persister);
    }

    // Handler: handle, process, dispatch (generic, check late)
    if matches_any(&expanded, &["handl", "dispatch"]) || name.starts_with("process_") {
        return Some(SemanticRole::Handler);
    }
    if name.starts_with("on_") {
        return Some(SemanticRole::Handler);
    }

    // Orchestrator: orchestrate, coordinate, pipeline, workflow
    if matches_any(
        &expanded,
        &["orchestrat", "coordinat", "pipeline", "workflow"],
    ) {
        return Some(SemanticRole::Orchestrator);
    }
    // "run"/"execute" only if chunk calls 3+ other functions (orchestration signal)
    if (name.starts_with("run_")
        || name.starts_with("execute_")
        || name == "run"
        || name == "execute")
        && meta.calls.len() >= 3
    {
        return Some(SemanticRole::Orchestrator);
    }

    None
}

fn matches_any(text: &str, patterns: &[&str]) -> bool {
    patterns.iter().any(|p| text.contains(p))
}

// ─────────────────────────────────────────────────────────────────────────────
// E3 — ExCS-style chunk annotation synthesis (no LLM)
// ─────────────────────────────────────────────────────────────────────────────

/// Synthesize an ExCS-style natural-language annotation block for a chunk.
///
/// This is the E3 synthesizer from the semantex v0.3 SOTA spec. It bridges the
/// NL → code vocabulary gap (e.g. a query like *"parallel failure handling"*
/// matching a chunk that uses `Promise.allSettled`) by emitting human-shaped
/// prose that BM25 can score against, without any LLM call.
///
/// The annotation is composed from existing chunk signals:
///
/// 1. **Docstring** (first sentence, if present) — leading authorial intent.
/// 2. **Kind + name** — `function foo`, `struct Bar`, etc., with camelCase /
///    snake_case expansion so identifier-style words land in BM25 as English.
/// 3. **Purpose line** — a one-sentence summary derived from the signature
///    (kind + expanded name + return type if any).
/// 4. **Semantic role label** — e.g. `parallel failure handling`,
///    `validator checker`, drawn from `semantic_role.rs` mappings.
/// 5. **Calls** — what this chunk *does* (filtered to non-trivial calls, then
///    expanded camelCase → English).
/// 6. **Called by** — what *invokes* this chunk (also expanded). Surfacing
///    callers lets NL queries about consumers find providers.
/// 7. **Implements** / **inherits** — type relationships for class-y kinds.
///
/// The output is a multi-line block joined by `\n`. Empty fields are dropped.
///
/// # Parameters
///
/// - `meta`: the chunk's structured metadata (carries docstring, role, etc.).
/// - `calls`: outbound calls — typically `meta.calls`, but accepted separately
///   so callers can post-process (e.g. fold trivial calls).
/// - `called_by`: incoming callers, normally filled by the post-graph-resolve
///   pass — pass an empty slice if not yet resolved at synthesis time.
///
/// # Example
///
/// ```text
/// Parse the input string and return the decoded value.
/// purpose: function parse json — parses, returns Value
/// role: transformer converter serializer parser
/// calls: tokenize, decode value
/// called by: load config, read input
/// ```
#[must_use]
pub fn synthesize_nl_annotation(
    meta: &StructuredChunkMeta,
    calls: &[String],
    called_by: &[String],
) -> String {
    let mut lines: Vec<String> = Vec::with_capacity(8);

    // 1. Docstring (first sentence) — surfaces author-provided intent first.
    if let Some(ref doc) = meta.docstring {
        let trimmed = doc.trim();
        if !trimmed.is_empty() {
            let first = first_sentence(trimmed);
            if !first.is_empty() && first.len() <= 200 {
                lines.push(first.to_string());
            }
        }
    }

    // 2 + 3. Purpose line (kind + expanded name + return type when present).
    if let Some(ref name) = meta.name {
        let kind = meta.kind_label();
        let expanded = expand_identifier(name);
        let mut purpose = format!("purpose: {kind} {expanded}");
        if let Some(ref ret) = meta.return_type {
            let ret_trim = ret.trim();
            if !ret_trim.is_empty() && ret_trim.len() <= 80 {
                purpose.push_str(" — returns ");
                purpose.push_str(ret_trim);
            }
        }
        lines.push(purpose);
    }

    // 4. Semantic role label (e.g. "transformer converter serializer parser").
    if let Some(ref role) = meta.semantic_role {
        lines.push(format!("role: {}", role.as_label()));
    }

    // 5. Outbound calls — filtered to architecturally-meaningful targets,
    // then expanded so camelCase identifiers land as English.
    let interesting_calls: Vec<String> = calls
        .iter()
        .filter(|c| !is_trivial_call(c))
        .take(8)
        .map(|c| expand_identifier(c))
        .collect();
    if !interesting_calls.is_empty() {
        lines.push(format!("calls: {}", interesting_calls.join(", ")));
    }

    // 6. Incoming callers — likewise expanded. Useful for "who consumes X" queries.
    let expanded_callers: Vec<String> = called_by
        .iter()
        .take(8)
        .map(|c| expand_identifier(c))
        .collect();
    if !expanded_callers.is_empty() {
        lines.push(format!("called by: {}", expanded_callers.join(", ")));
    }

    // 7. Type relationships — surfacing impls and inheritance lifts the
    // vocabulary of trait-/interface-level queries.
    if !meta.implements.is_empty() {
        let impls: Vec<String> = meta
            .implements
            .iter()
            .map(|r| {
                format!(
                    "{} implements {}",
                    expand_identifier(&r.implementor),
                    expand_identifier(&r.trait_name)
                )
            })
            .collect();
        lines.push(impls.join("; "));
    }
    if !meta.inherits.is_empty() {
        let parents: Vec<String> = meta.inherits.iter().map(|p| expand_identifier(p)).collect();
        lines.push(format!("inherits: {}", parents.join(", ")));
    }

    lines.join("\n")
}

/// Extract the first sentence from a docstring or comment.
///
/// "Sentence" here is loosely defined: ends at the first `.`, `!`, `?`, or
/// double newline. Common doc-comment leaders (`///`, `//`, `*`) are stripped
/// from the start of each line.
fn first_sentence(s: &str) -> &str {
    let trimmed = strip_doc_leaders(s);
    for (i, ch) in trimmed.char_indices() {
        if matches!(ch, '.' | '!' | '?') {
            return &trimmed[..i];
        }
    }
    // Fall back: stop at the first blank line if no terminator was found.
    if let Some(idx) = trimmed.find("\n\n") {
        return &trimmed[..idx];
    }
    trimmed
}

/// Strip leading doc-comment markers (`///`, `//`, `*`) from a doc string.
/// Only touches the first line — subsequent lines keep their original shape
/// because we never read past the first sentence anyway.
fn strip_doc_leaders(s: &str) -> &str {
    let t = s.trim_start();
    if let Some(rest) = t.strip_prefix("///") {
        return rest.trim_start();
    }
    if let Some(rest) = t.strip_prefix("//") {
        return rest.trim_start();
    }
    if let Some(rest) = t.strip_prefix("/**") {
        return rest.trim_start();
    }
    if let Some(rest) = t.strip_prefix("*") {
        return rest.trim_start();
    }
    t
}

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

    fn meta_with_name(name: &str) -> StructuredChunkMeta {
        StructuredChunkMeta {
            name: Some(name.to_string()),
            ..StructuredChunkMeta::default()
        }
    }

    fn meta_with_name_and_calls(name: &str, call_count: usize) -> StructuredChunkMeta {
        let mut meta = meta_with_name(name);
        meta.calls = (0..call_count).map(|i| format!("fn_{i}")).collect();
        meta
    }

    #[test]
    fn test_constructor_patterns() {
        assert_eq!(
            classify_semantic_role(&meta_with_name("new"), ""),
            Some(SemanticRole::Constructor)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("create_user"), ""),
            Some(SemanticRole::Constructor)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("buildConfig"), ""),
            Some(SemanticRole::Constructor)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("from_str"), ""),
            Some(SemanticRole::Constructor)
        );
    }

    #[test]
    fn test_destructor_patterns() {
        assert_eq!(
            classify_semantic_role(&meta_with_name("close"), ""),
            Some(SemanticRole::Destructor)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("shutdown"), ""),
            Some(SemanticRole::Destructor)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("cleanup"), ""),
            Some(SemanticRole::Destructor)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("dispose"), ""),
            Some(SemanticRole::Destructor)
        );
    }

    #[test]
    fn test_sanitizer_patterns() {
        assert_eq!(
            classify_semantic_role(&meta_with_name("sanitize"), ""),
            Some(SemanticRole::Sanitizer)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("redact"), ""),
            Some(SemanticRole::Sanitizer)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("stripPII"), ""),
            Some(SemanticRole::Sanitizer)
        );
    }

    #[test]
    fn test_orchestrator_needs_calls() {
        assert_eq!(
            classify_semantic_role(&meta_with_name_and_calls("run", 1), ""),
            None
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name_and_calls("run", 3), ""),
            Some(SemanticRole::Orchestrator)
        );
        // pipeline/workflow don't need call count
        assert_eq!(
            classify_semantic_role(&meta_with_name("runPipeline"), ""),
            Some(SemanticRole::Orchestrator)
        );
    }

    #[test]
    fn test_validator_patterns() {
        assert_eq!(
            classify_semantic_role(&meta_with_name("validate_input"), ""),
            Some(SemanticRole::Validator)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("check_bounds"), ""),
            Some(SemanticRole::Validator)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("ensure_valid"), ""),
            Some(SemanticRole::Validator)
        );
    }

    #[test]
    fn test_transformer_patterns() {
        assert_eq!(
            classify_semantic_role(&meta_with_name("to_string"), ""),
            Some(SemanticRole::Transformer)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("parse_json"), ""),
            Some(SemanticRole::Transformer)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("serialize"), ""),
            Some(SemanticRole::Transformer)
        );
    }

    #[test]
    fn test_fetcher_patterns() {
        assert_eq!(
            classify_semantic_role(&meta_with_name("get_user"), ""),
            Some(SemanticRole::Fetcher)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("load_config"), ""),
            Some(SemanticRole::Fetcher)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("fetchData"), ""),
            Some(SemanticRole::Fetcher)
        );
    }

    #[test]
    fn test_persister_patterns() {
        assert_eq!(
            classify_semantic_role(&meta_with_name("save_record"), ""),
            Some(SemanticRole::Persister)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("write_to_disk"), ""),
            Some(SemanticRole::Persister)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("upsert"), ""),
            Some(SemanticRole::Persister)
        );
    }

    #[test]
    fn test_handler_patterns() {
        assert_eq!(
            classify_semantic_role(&meta_with_name("handleRequest"), ""),
            Some(SemanticRole::Handler)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("on_click"), ""),
            Some(SemanticRole::Handler)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("process_event"), ""),
            Some(SemanticRole::Handler)
        );
    }

    #[test]
    fn test_error_handler_patterns() {
        assert_eq!(
            classify_semantic_role(&meta_with_name("on_error"), ""),
            Some(SemanticRole::ErrorHandler)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("handle_error"), ""),
            Some(SemanticRole::ErrorHandler)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("retryRequest"), ""),
            Some(SemanticRole::ErrorHandler)
        );
    }

    #[test]
    fn test_middleware_patterns() {
        assert_eq!(
            classify_semantic_role(&meta_with_name("interceptor"), ""),
            Some(SemanticRole::Middleware)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("middleware"), ""),
            Some(SemanticRole::Middleware)
        );
        assert_eq!(
            classify_semantic_role(&meta_with_name("authInterceptor"), ""),
            Some(SemanticRole::Middleware)
        );
    }

    #[test]
    fn test_no_match() {
        assert_eq!(classify_semantic_role(&meta_with_name("foo"), ""), None);
        assert_eq!(classify_semantic_role(&meta_with_name("bar_baz"), ""), None);
    }

    #[test]
    fn test_no_name_returns_none() {
        let meta = StructuredChunkMeta::default();
        assert_eq!(classify_semantic_role(&meta, ""), None);
    }

    // ─────────────────────────────────────────────────────────────────────
    // synthesize_nl_annotation tests
    // ─────────────────────────────────────────────────────────────────────

    use super::super::structured_meta::{ImplRelation, SemanticRole as SR};

    #[test]
    fn test_synthesize_basic_function() {
        let meta = StructuredChunkMeta {
            name: Some("parseJson".to_string()),
            kind: Some("function".to_string()),
            docstring: Some("Parse the input string. Returns the decoded value.".to_string()),
            return_type: Some("Value".to_string()),
            semantic_role: Some(SR::Transformer),
            ..Default::default()
        };
        let calls = vec!["tokenize".to_string(), "decodeValue".to_string()];
        let callers = vec!["loadConfig".to_string()];
        let out = synthesize_nl_annotation(&meta, &calls, &callers);

        assert!(out.contains("Parse the input string"), "{out}");
        assert!(out.contains("purpose: function parse json"), "{out}");
        assert!(out.contains("returns Value"), "{out}");
        assert!(out.contains("role: transformer"), "{out}");
        assert!(out.contains("calls: tokenize, decode value"), "{out}");
        assert!(out.contains("called by: load config"), "{out}");
    }

    #[test]
    fn test_synthesize_class_with_impls() {
        let meta = StructuredChunkMeta {
            name: Some("ConnectionPool".to_string()),
            kind: Some("struct".to_string()),
            implements: vec![ImplRelation {
                implementor: "ConnectionPool".to_string(),
                trait_name: "Drop".to_string(),
            }],
            inherits: vec!["BasePool".to_string()],
            ..Default::default()
        };
        let out = synthesize_nl_annotation(&meta, &[], &[]);
        assert!(out.contains("purpose: struct connection pool"), "{out}");
        assert!(out.contains("connection pool implements drop"), "{out}");
        assert!(out.contains("inherits: base pool"), "{out}");
    }

    #[test]
    fn test_synthesize_module_with_only_docstring() {
        let meta = StructuredChunkMeta {
            name: Some("server".to_string()),
            kind: Some("module".to_string()),
            docstring: Some("TCP daemon server for handling search requests.".to_string()),
            ..Default::default()
        };
        let out = synthesize_nl_annotation(&meta, &[], &[]);
        assert!(
            out.contains("TCP daemon server for handling search requests"),
            "{out}"
        );
        assert!(out.contains("purpose: module server"), "{out}");
    }

    #[test]
    fn test_synthesize_filters_trivial_calls() {
        let meta = StructuredChunkMeta {
            name: Some("collect".to_string()),
            kind: Some("function".to_string()),
            ..Default::default()
        };
        let calls = vec![
            "push".to_string(),   // trivial
            "unwrap".to_string(), // trivial
            "validateInput".to_string(),
        ];
        let out = synthesize_nl_annotation(&meta, &calls, &[]);
        assert!(out.contains("validate input"), "{out}");
        assert!(
            !out.contains("calls: push") && !out.contains(", push"),
            "trivial 'push' should be filtered: {out}"
        );
    }

    #[test]
    fn test_synthesize_empty_meta_returns_empty() {
        let meta = StructuredChunkMeta::default();
        let out = synthesize_nl_annotation(&meta, &[], &[]);
        assert!(
            out.is_empty(),
            "empty meta should yield empty annotation: {out}"
        );
    }

    #[test]
    fn test_synthesize_no_llm_no_async_no_io() {
        // The function is synchronous, allocation-only — proven by the fact
        // that it takes only borrowed references and returns a String built
        // from string concatenation. We verify there are no surprising
        // allocations like network calls by ensuring it runs in <1 ms on
        // typical inputs.
        use std::time::Instant;
        let meta = StructuredChunkMeta {
            name: Some("complexExample".to_string()),
            kind: Some("function".to_string()),
            docstring: Some("Does many things.".to_string()),
            return_type: Some("Result<()>".to_string()),
            semantic_role: Some(SR::Orchestrator),
            ..Default::default()
        };
        let calls: Vec<String> = (0..20).map(|i| format!("doStep{i}")).collect();
        let callers: Vec<String> = (0..20).map(|i| format!("invoker{i}")).collect();

        let start = Instant::now();
        for _ in 0..100 {
            let _ = synthesize_nl_annotation(&meta, &calls, &callers);
        }
        let elapsed = start.elapsed();
        assert!(
            elapsed.as_millis() < 50,
            "100 syntheses should be sub-50ms (no LLM, no IO); got {}ms",
            elapsed.as_millis()
        );
    }

    #[test]
    fn test_first_sentence_strips_doc_leader() {
        assert_eq!(first_sentence("/// Hello there. More text."), "Hello there");
        assert_eq!(first_sentence("// short comment"), "short comment");
        assert_eq!(first_sentence("Normal text. tail"), "Normal text");
    }

    #[test]
    fn test_first_sentence_long_docstring_truncates() {
        let s = "First sentence ends here. Second sentence.";
        assert_eq!(first_sentence(s), "First sentence ends here");
    }
}