knot 1.14.0

Codebase Graph + Vector RAG Indexer for Java, TypeScript, JavaScript, Kotlin, Rust, Python, Groovy, C/C++, C#, Build Systems, and HTML/CSS codebases
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
//! Stage 3 — Prepare: UUID assignment and embedding text construction.
//!
//! Each [`ParsedEntity`] already carries a UUID generated at construction time
//! (see [`ParsedEntity::new`]). This stage's responsibility is to build the
//! `embed_text` field — the string that will be fed to the embedding model.
//!
//! # Embedding text format
//! ```text
//! <identifier_phrase>: <first docstring sentence>   ← natural-language role
//!   — or, for a doc-less callable with outgoing calls:
//! <identifier_phrase> — calls <tokenized callee names> (max 6)
//! [<KIND>] <name>
//! Identifier: <tokenized name>        ← omitted for single-token names
//! FQN: <fully qualified name>         ← omitted when it equals the name
//! Decorators: <decorators>            ← omitted when none
//! Signature: <signature>              ← omitted when None
//! <docstring>                         ← omitted when None
//! Implementation context: <comments>  ← omitted when none
//! Calls: <tokenized outgoing names>   ← omitted when none (capped at 20)
//! File: <file_path>:<start_line>
//! ```
//!
//! Keeping the format consistent across runs is important: the same entity
//! should always produce the same embedding so vector updates are idempotent.
//!
//! Recall contract: the embed text must share vocabulary with the
//! natural-language queries that describe the entity's behaviour. The FIRST
//! line is a plain natural-language role sentence (identifier phrase plus
//! the docstring's first sentence, or the outgoing call names for doc-less
//! callables) so the model's strongest signal is behavioural vocabulary —
//! truncation eats structural fields before it eats the role. Identifiers
//! reach it both raw (name, FQN) and tokenized (`useChangePassword` → `use
//! change password`), and the names of the callees in the entity's body are
//! tokenized in a final `Calls:` section — a definition without a doc
//! comment stays retrievable through what it does, as observable from its
//! own body.

use crate::models::{EntityKind, ParsedEntity, ReferenceIntent};
use crate::utils::identifiers::identifier_token_phrase;

/// Maximum characters carried from the docstring's first sentence into the
/// role line. Bounded so the natural-language lead cannot itself push the
/// structural fields past the model's token budget (all current 384-dim
/// models truncate at 512 tokens; the role line and the kind/name header
/// must survive even when everything after them is clipped).
const ROLE_SENTENCE_MAX_CHARS: usize = 200;

/// Outgoing call names used to describe a doc-less entity's behaviour in
/// the role line. Shorter than the `Calls:` section (`MAX_CALL_NAMES`):
/// the role line is prose-shaped and a comma list of 20 identifiers stops
/// reading like a sentence.
const ROLE_CALL_NAMES: usize = 6;

/// Maximum number of outgoing-reference names carried in the `Calls:`
/// section of the embed text. Deliberately no content filter beyond
/// dropping the entity's own name: short callee names (`new`, `use`, `map`)
/// are almost uniformly distributed across entities, so embedding them is
/// noise-neutral, and any length or deny-list heuristic would silently drop
/// legitimate short identifiers (`use` for React hooks, `map`, `post`).
const MAX_CALL_NAMES: usize = 20;

/// Build the `embed_text` field for every entity in-place.
///
/// This function is intentionally synchronous and allocation-cheap; it is
/// called after Rayon parsing and before the async embedding stage.
pub fn prepare_entities(entities: &mut [ParsedEntity]) {
    for entity in entities.iter_mut() {
        if matches!(
            entity.kind,
            EntityKind::MarkdownSection | EntityKind::MarkdownDocument
        ) {
            continue;
        }
        entity.embed_text = build_embed_text(entity);
    }
}

/// Construct the embedding text for a single entity.
fn build_embed_text(entity: &ParsedEntity) -> String {
    let mut parts: Vec<String> = Vec::with_capacity(11);

    // Role line FIRST: the natural-language lead is what the model sees
    // before truncation removes anything. All downstream model families
    // clip at their token budget keeping the front of the text, so the
    // behavioural sentence must precede structural noise.
    parts.push(role_sentence(entity));

    // Header: kind + name
    parts.push(format!("[{}] {}", entity.kind, entity.name));

    // Tokenized identifier: natural-language queries share vocabulary with
    // the identifier's word components, not with its raw spelling.
    let name_phrase = identifier_token_phrase(&entity.name);
    if name_phrase != entity.name {
        parts.push(format!("Identifier: {name_phrase}"));
    }

    // Fully qualified name: path/module components ("api auth login") make
    // a definition retrievable through where it lives, too.
    if !entity.fqn.is_empty() && entity.fqn != entity.name {
        parts.push(format!("FQN: {}", entity.fqn));
        let fqn_phrase = identifier_token_phrase(&entity.fqn);
        if fqn_phrase != name_phrase {
            parts.push(format!("Identifier: {fqn_phrase}"));
        }
    }

    // Optional decorators/annotations (framework metadata)
    if !entity.decorators.is_empty() {
        let decorators_text = entity
            .decorators
            .iter()
            .filter(|d| !d.trim().is_empty())
            .cloned()
            .collect::<Vec<_>>()
            .join(", ");

        if !decorators_text.is_empty() {
            parts.push(format!("Decorators: {}", decorators_text));
        }
    }

    // Optional signature
    if let Some(sig) = &entity.signature {
        parts.push(format!("Signature: {sig}"));
    }

    // Optional docstring (preceding comments)
    if let Some(doc) = &entity.docstring
        && !doc.trim().is_empty()
    {
        parts.push(doc.trim().to_owned());
    }

    // Inline comments found within the entity body
    if !entity.inline_comments.is_empty() {
        let inline_text = entity
            .inline_comments
            .iter()
            .filter(|c| !c.trim().is_empty())
            .cloned()
            .collect::<Vec<_>>()
            .join("\n");

        if !inline_text.trim().is_empty() {
            parts.push(format!("Implementation context:\n{}", inline_text));
        }
    }

    // Outgoing references: the names called/referenced inside this entity's
    // body, tokenized. For a definition without a doc comment this is the
    // only behavioural vocabulary available.
    let calls_section = calls_section_text(entity);
    if !calls_section.is_empty() {
        parts.push(format!("Calls: {calls_section}"));
    }

    // Source location — helps distinguish identically-named entities
    parts.push(format!("File: {}:{}", entity.file_path, entity.start_line));

    parts.join("\n")
}

/// Collect the deduplicated, tokenized names this entity calls or refers to,
/// in first-appearance order, capped at `cap`. The entity's own name is
/// dropped (self-references add no recall).
fn call_names(entity: &ParsedEntity, cap: usize) -> Vec<String> {
    let mut seen: Vec<String> = Vec::new();
    for intent in &entity.reference_intents {
        if seen.len() >= cap {
            break;
        }
        let name = match intent {
            ReferenceIntent::Call { method, .. } => method.as_str(),
            ReferenceIntent::TypeReference { type_name, .. } => type_name.as_str(),
            ReferenceIntent::Extends { parent, .. } => parent.as_str(),
            ReferenceIntent::Implements { interface, .. } => interface.as_str(),
            _ => continue,
        };
        if name == entity.name || name.trim().is_empty() {
            continue;
        }
        if !seen.contains(&name.to_string()) {
            seen.push(name.to_string());
        }
    }
    seen.iter()
        .map(|n| identifier_token_phrase(n))
        .filter(|phrase| !phrase.is_empty())
        .collect()
}

/// The `Calls:` section body: deduplicated tokenized call names, capped at
/// [`MAX_CALL_NAMES`].
fn calls_section_text(entity: &ParsedEntity) -> String {
    call_names(entity, MAX_CALL_NAMES).join(", ")
}

// --- role line (natural-language lead of the embed text) ---------------------

/// Strip the comment markers a docstring collector may have carried verbatim
/// (`///`, `/**`, `*/`, `*`, `//`, `#`, `\"\"\"` …), collapse whitespace and
/// return the prose. Deterministic and language-agnostic: the same line
/// rule set applies to every language's comment syntax.
fn strip_comment_markers(doc: &str) -> String {
    doc.lines()
        .filter(|line| !line.trim().is_empty())
        .map(|line| {
            // `trim_start_matches` peels repeated leading markers per line,
            // so `///`, `* *`, `*/` and `/*` all become empty or prose.
            let peeled = line
                .trim()
                .trim_start_matches(['/', '*', '-', '#', '"', '\'', '>', '<']);
            peeled.trim()
        })
        .filter(|line| !line.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}

/// First sentence of a comment-stripped docstring: text up to (and
/// including) the first `. ` / `.\n`, or the trailing period, or the whole
/// text when there is no sentence boundary. Truncated to
/// [`ROLE_SENTENCE_MAX_CHARS`] on a char boundary (never byte-sliced) with
/// an ellipsis, so multi-byte docstrings cannot panic.
fn first_sentence(doc: &str) -> String {
    let doc = doc.trim();
    let end = doc
        .find(". ")
        .or_else(|| doc.find(".\n"))
        .or_else(|| {
            if doc.ends_with('.') {
                Some(doc.len() - 1)
            } else {
                None
            }
        })
        .map(|i| i + 1)
        .unwrap_or(doc.len());
    let sentence = doc[..end].trim();

    if sentence.chars().count() <= ROLE_SENTENCE_MAX_CHARS {
        return sentence.to_owned();
    }
    let cut: String = sentence
        .char_indices()
        .take(ROLE_SENTENCE_MAX_CHARS)
        .map(|(_, c)| c)
        .collect();
    format!("{cut}…")
}

/// Deterministic natural-language role sentence for an entity.
///
/// Precedence:
/// 1. a non-empty docstring → `identifier phrase: first docstring sentence`;
/// 2. otherwise, for an entity whose body shows behaviour →
///    `identifier phrase — calls <tokenized callee names>`;
/// 3. otherwise the bare identifier phrase alone.
fn role_sentence(entity: &ParsedEntity) -> String {
    let phrase = {
        let p = identifier_token_phrase(&entity.name);
        if p.is_empty() { entity.name.clone() } else { p }
    };

    let docstring = entity
        .docstring
        .as_ref()
        .map(|d| strip_comment_markers(d))
        .filter(|d| !d.is_empty());

    if let Some(doc) = docstring {
        let sentence = first_sentence(&doc);
        if !sentence.is_empty() {
            return format!("{phrase}: {sentence}");
        }
    }

    let calls = call_names(entity, ROLE_CALL_NAMES);
    if !calls.is_empty() {
        return format!("{phrase} — calls {}", calls.join(", "));
    }

    phrase
}

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

    #[test]
    fn test_build_embed_text_minimal() {
        let entity = ParsedEntity::new(
            "MyClass",
            EntityKind::Class,
            "com.example.MyClass",
            None,
            None,
            "java",
            "com/example/MyClass.java",
            10,
            20,
            None,
            "test-repo",
        );

        let embed_text = build_embed_text(&entity);

        assert!(embed_text.contains("[class] MyClass"));
        assert!(embed_text.contains("File: com/example/MyClass.java:10"));
        assert!(!embed_text.contains("Signature:"));
        assert!(!embed_text.contains("Decorators:"));
    }

    #[test]
    fn test_build_embed_text_full() {
        let mut entity = ParsedEntity::new(
            "saveUser",
            EntityKind::Method,
            "UserService.saveUser",
            Some("public void saveUser(User user)".to_string()),
            Some("Saves a new user to the database.".to_string()),
            "java",
            "UserService.java",
            42,
            50,
            Some("UserService".to_string()),
            "test-repo",
        );

        entity.decorators = vec!["@Transactional".to_string(), "@Override".to_string()];
        entity.inline_comments = vec![
            "// Check for duplicates".to_string(),
            "/* Commit transaction */".to_string(),
        ];

        let embed_text = build_embed_text(&entity);

        assert!(embed_text.contains("[method] saveUser"));
        assert!(embed_text.contains("Signature: public void saveUser(User user)"));
        assert!(embed_text.contains("Saves a new user to the database."));
        assert!(embed_text.contains("Decorators: @Transactional, @Override"));
        assert!(embed_text.contains(
            "Implementation context:\n// Check for duplicates\n/* Commit transaction */"
        ));
        assert!(embed_text.contains("File: UserService.java:42"));
    }

    #[test]
    fn test_prepare_entities_batch() {
        let entity1 = ParsedEntity::new(
            "Class1",
            EntityKind::Class,
            "Class1",
            None,
            None,
            "java",
            "file1.java",
            1,
            10,
            None,
            "test-repo",
        );
        let entity2 = ParsedEntity::new(
            "Class2",
            EntityKind::Class,
            "Class2",
            None,
            None,
            "java",
            "file2.java",
            1,
            10,
            None,
            "test-repo",
        );

        let mut entities = vec![entity1, entity2];

        // Before prepare, embed_text is empty
        assert!(entities[0].embed_text.is_empty());
        assert!(entities[1].embed_text.is_empty());

        prepare_entities(&mut entities);

        // After prepare, embed_text is populated
        assert!(!entities[0].embed_text.is_empty());
        assert!(!entities[1].embed_text.is_empty());
        assert!(entities[0].embed_text.contains("Class1"));
        assert!(entities[1].embed_text.contains("Class2"));
    }

    #[test]
    fn test_build_embed_text_build_dependency() {
        let entity = ParsedEntity::new(
            "org.springframework:spring-core:5.3.29",
            EntityKind::BuildDependency,
            "org.springframework:spring-core:5.3.29",
            Some("scope: compile".to_string()),
            Some("Maven dependency: org.springframework:spring-core:5.3.29".to_string()),
            "xml",
            "pom.xml",
            15,
            19,
            None,
            "test-repo",
        );

        let embed_text = build_embed_text(&entity);

        assert!(embed_text.contains("[build_dependency] org.springframework:spring-core:5.3.29"));
        assert!(embed_text.contains("Signature: scope: compile"));
        assert!(embed_text.contains("Maven dependency:"));
        assert!(embed_text.contains("File: pom.xml:15"));
    }

    #[test]
    fn test_build_embed_text_build_plugin() {
        let entity = ParsedEntity::new(
            "org.apache.maven.plugins:maven-compiler-plugin:3.11.0",
            EntityKind::BuildPlugin,
            "org.apache.maven.plugins:maven-compiler-plugin:3.11.0",
            None,
            Some("Maven plugin: org.apache.maven.plugins:maven-compiler-plugin:3.11.0".to_string()),
            "xml",
            "pom.xml",
            30,
            35,
            None,
            "test-repo",
        );

        let embed_text = build_embed_text(&entity);

        assert!(embed_text.contains("[build_plugin]"));
        assert!(embed_text.contains("maven-compiler-plugin"));
        assert!(embed_text.contains("Maven plugin:"));
        assert!(embed_text.contains("File: pom.xml:30"));
    }

    #[test]
    fn test_build_embed_text_build_task() {
        let entity = ParsedEntity::new(
            "buildDocs",
            EntityKind::BuildTask,
            "buildDocs",
            None,
            Some("task buildDocs(type: Copy) {".to_string()),
            "groovy",
            "build.gradle",
            25,
            25,
            None,
            "test-repo",
        );

        let embed_text = build_embed_text(&entity);

        assert!(embed_text.contains("[build_task] buildDocs"));
        assert!(embed_text.contains("task buildDocs(type: Copy) {"));
        assert!(embed_text.contains("File: build.gradle:25"));
    }

    #[test]
    fn test_build_embed_text_pipeline_stage() {
        let entity = ParsedEntity::new(
            "stage: Build",
            EntityKind::PipelineStage,
            "stage: Build",
            None,
            Some("stage('Build') {".to_string()),
            "groovy",
            "Jenkinsfile",
            10,
            10,
            None,
            "test-repo",
        );

        let embed_text = build_embed_text(&entity);

        assert!(embed_text.contains("[pipeline_stage] stage: Build"));
        assert!(embed_text.contains("File: Jenkinsfile:10"));
    }

    #[test]
    fn test_build_embed_text_pipeline_step() {
        let entity = ParsedEntity::new(
            "sh: mvn compile",
            EntityKind::PipelineStep,
            "sh: mvn compile",
            None,
            Some("sh 'mvn compile'".to_string()),
            "groovy",
            "Jenkinsfile",
            12,
            12,
            None,
            "test-repo",
        );

        let embed_text = build_embed_text(&entity);

        assert!(embed_text.contains("[pipeline_step] sh: mvn compile"));
        assert!(embed_text.contains("File: Jenkinsfile:12"));
    }

    #[test]
    fn test_build_embed_text_groovy_class() {
        let entity = ParsedEntity::new(
            "MyGroovyClass",
            EntityKind::GroovyClass,
            "com.example.MyGroovyClass",
            None,
            Some("A Groovy class".to_string()),
            "groovy",
            "src/MyGroovyClass.groovy",
            1,
            10,
            None,
            "test-repo",
        );
        let embed_text = build_embed_text(&entity);
        assert!(embed_text.contains("[groovy_class] MyGroovyClass"));
        assert!(embed_text.contains("A Groovy class"));
    }

    #[test]
    fn test_build_embed_text_groovy_interface() {
        let entity = ParsedEntity::new(
            "MyGroovyInterface",
            EntityKind::GroovyInterface,
            "com.example.MyGroovyInterface",
            None,
            None,
            "groovy",
            "src/MyGroovyInterface.groovy",
            1,
            5,
            None,
            "test-repo",
        );
        let embed_text = build_embed_text(&entity);
        assert!(embed_text.contains("[groovy_interface] MyGroovyInterface"));
    }

    #[test]
    fn test_build_embed_text_groovy_trait() {
        let entity = ParsedEntity::new(
            "MyGroovyTrait",
            EntityKind::GroovyTrait,
            "com.example.MyGroovyTrait",
            None,
            None,
            "groovy",
            "src/MyGroovyTrait.groovy",
            1,
            8,
            None,
            "test-repo",
        );
        let embed_text = build_embed_text(&entity);
        assert!(embed_text.contains("[groovy_trait] MyGroovyTrait"));
    }

    #[test]
    fn test_build_embed_text_groovy_method() {
        let mut entity = ParsedEntity::new(
            "save",
            EntityKind::GroovyMethod,
            "UserService.save",
            Some("def save(User user)".to_string()),
            Some("Saves the user".to_string()),
            "groovy",
            "UserService.groovy",
            42,
            46,
            Some("UserService".to_string()),
            "test-repo",
        );
        entity.decorators = vec!["@Transactional".to_string()];
        let embed_text = build_embed_text(&entity);
        assert!(embed_text.contains("[groovy_method] save"));
        assert!(embed_text.contains("Signature: def save(User user)"));
        assert!(embed_text.contains("Saves the user"));
        assert!(embed_text.contains("Decorators: @Transactional"));
    }

    #[test]
    fn test_embed_text_groovy_method_includes_docstring() {
        // BDD contract: a Groovy entity's docstring must reach embed_text so
        // semantic search can match its concepts (nextflow `init` regression).
        let entity = ParsedEntity::new(
            "init",
            EntityKind::GroovyMethod,
            "nextflow.plugin.extension.PluginExtensionPoint.init",
            Some("abstract protected void init(Session session)".to_string()),
            Some(
                "Channel factory initialization. This method is invoked one and only once"
                    .to_string(),
            ),
            "groovy",
            "PluginExtensionPoint.groovy",
            12,
            12,
            Some("PluginExtensionPoint".to_string()),
            "test-repo",
        );
        let embed_text = build_embed_text(&entity);
        assert!(embed_text.contains("[groovy_method] init"));
        assert!(embed_text.contains("Channel factory initialization"));
    }

    #[test]
    fn test_build_embed_text_groovy_property() {
        let entity = ParsedEntity::new(
            "config",
            EntityKind::GroovyProperty,
            "com.example.App.config",
            None,
            None,
            "groovy",
            "App.groovy",
            3,
            3,
            None,
            "test-repo",
        );
        let embed_text = build_embed_text(&entity);
        assert!(embed_text.contains("[groovy_property] config"));
    }

    /// BDD contract (recall bug): the embed text must carry the entity's
    /// FQN so a definition is retrievable through the path components of
    /// its fully qualified name, not only its short name.
    #[test]
    fn embed_text_includes_fqn() {
        let entity = ParsedEntity::new(
            "login",
            EntityKind::RustFunction,
            "jobwatch::api::auth::login",
            None,
            None,
            "rust",
            "src/api/auth.rs",
            136,
            160,
            None,
            "job-watch",
        );
        let embed_text = build_embed_text(&entity);
        assert!(embed_text.contains("jobwatch::api::auth::login"));
    }

    /// BDD contract (recall bug): multi-case identifiers must reach the
    /// embed text in token form (`useChangePassword` → `use change
    /// password`) so natural-language queries share vocabulary with the
    /// vector without knowing the identifier's spelling.
    #[test]
    fn embed_text_includes_tokenized_identifier() {
        let entity = ParsedEntity::new(
            "useChangePassword",
            EntityKind::Function,
            "useChangePassword",
            None,
            None,
            "typescript",
            "src/api/authQueries.ts",
            5,
            12,
            None,
            "ui",
        );
        let embed_text = build_embed_text(&entity);
        assert!(embed_text.contains("Identifier: use change password"));
        // snake_case stays transparent too.
        let snake = ParsedEntity::new(
            "verify_credentials_or_fail",
            EntityKind::RustFunction,
            "verify_credentials_or_fail",
            None,
            None,
            "rust",
            "src/api/auth.rs",
            190,
            200,
            None,
            "job-watch",
        );
        let embed_text = build_embed_text(&snake);
        assert!(embed_text.contains("Identifier: verify credentials or fail"));
    }

    /// BDD contract (recall bug): the names of the callees in an entity's
    /// body must reach the embed text tokenized, so a paraphrase describing
    /// the behaviour ("authenticate user with email and password") shares
    /// vocabulary with a definition whose behaviour is only observable
    /// through its body.
    #[test]
    fn embed_text_includes_outgoing_call_names() {
        let mut entity = ParsedEntity::new(
            "login",
            EntityKind::RustFunction,
            "login",
            None,
            None,
            "rust",
            "src/api/auth.rs",
            136,
            160,
            None,
            "job-watch",
        );
        entity.reference_intents.push(ReferenceIntent::Call {
            method: "normalize_email".to_string(),
            receiver: None,
            line: 1,
            arg_count: None,
        });
        entity.reference_intents.push(ReferenceIntent::Call {
            method: "new".to_string(),
            receiver: None,
            line: 2,
            arg_count: None,
        });
        entity.reference_intents.push(ReferenceIntent::Call {
            method: "useChangePassword".to_string(),
            receiver: None,
            line: 3,
            arg_count: None,
        });

        let embed_text = build_embed_text(&entity);
        assert!(embed_text.contains("Calls: normalize email, new, use change password"));
    }

    /// BDD contract: the embed text is a deterministic pure function of the
    /// entity state, and the `Calls:` section keeps a stable size for
    /// entities with many outgoing references.
    #[test]
    fn embed_text_is_deterministic_and_bounded() {
        let build = |calls: usize| {
            let mut entity = ParsedEntity::new(
                "hub",
                EntityKind::RustFunction,
                "hub",
                None,
                None,
                "rust",
                "src/hub.rs",
                1,
                2,
                None,
                "r",
            );
            for i in 0..calls {
                entity.reference_intents.push(ReferenceIntent::Call {
                    method: if i == 0 {
                        "hub".to_string() // self reference must be dropped
                    } else {
                        format!("callee_{i:03}")
                    },
                    receiver: None,
                    line: i + 1,
                    arg_count: None,
                });
            }
            prepare_entities(std::slice::from_mut(&mut entity));
            (entity.embed_text.clone(), entity)
        };

        let (first, _) = build(0);
        let (second, _) = build(0);
        assert_eq!(first, second, "same entity must embed identically");

        let (many, _) = build(60);
        let counted = many
            .lines()
            .find(|l| l.starts_with("Calls:"))
            .expect("Calls section present");
        let names = counted.trim_start_matches("Calls: ").split(", ").count();
        assert_eq!(names, 20, "Calls section capped at 20 names");

        // Self-name never enters the Calls section.
        let (with_self, _) = build(3);
        assert!(!with_self.contains("Calls: hub"));
    }

    #[test]
    fn prepare_entities_leaves_markdown_embed_text_untouched() {
        let mut md_doc = ParsedEntity::new(
            "README.md",
            EntityKind::MarkdownDocument,
            "README.md",
            None,
            None,
            "markdown",
            "README.md",
            1,
            50,
            None,
            "test-repo",
        );
        md_doc.embed_text = "Original Markdown Document Body".to_string();

        let mut md_section = ParsedEntity::new(
            "Overview",
            EntityKind::MarkdownSection,
            "README.md::Overview",
            None,
            None,
            "markdown",
            "README.md",
            5,
            20,
            None,
            "test-repo",
        );
        md_section.embed_text = "Original Markdown Section Body".to_string();

        let code_entity = ParsedEntity::new(
            "run",
            EntityKind::Function,
            "run",
            None,
            None,
            "rust",
            "src/main.rs",
            1,
            10,
            None,
            "test-repo",
        );

        let mut batch = vec![md_doc, md_section, code_entity];
        prepare_entities(&mut batch);

        assert_eq!(batch[0].embed_text, "Original Markdown Document Body");
        assert_eq!(batch[1].embed_text, "Original Markdown Section Body");
        assert!(
            batch[2].embed_text.starts_with("run\n"),
            "code entity must be prepared with role line: {:?}",
            batch[2].embed_text
        );
    }

    // --- role line (natural-language lead, Workstream A) --------------------

    #[test]
    fn role_sentence_leads_with_docstring() {
        let entity = ParsedEntity::new(
            "saveUser",
            EntityKind::Method,
            "UserService.saveUser",
            None,
            Some("Saves a new user to the database. Throws on conflict.".to_string()),
            "java",
            "UserService.java",
            42,
            50,
            Some("UserService".to_string()),
            "test-repo",
        );
        let embed_text = build_embed_text(&entity);
        // First line is the role sentence; the kind header follows it.
        assert!(
            embed_text.starts_with("save user: Saves a new user to the database.\n"),
            "role sentence must lead plainly: {embed_text:?}"
        );
        assert!(
            embed_text
                .lines()
                .nth(1)
                .unwrap()
                .starts_with("[method] saveUser")
        );
    }

    #[test]
    fn role_sentence_falls_back_to_call_names() {
        let mut entity = ParsedEntity::new(
            "login",
            EntityKind::RustFunction,
            "jobwatch::api::auth::login",
            None,
            None,
            "rust",
            "src/api/auth.rs",
            136,
            160,
            None,
            "job-watch",
        );
        for callee in [
            "normalize_email",
            "verify_credentials_or_fail",
            "issue_token",
        ] {
            entity.reference_intents.push(ReferenceIntent::Call {
                method: callee.to_string(),
                receiver: None,
                line: 1,
                arg_count: None,
            });
        }
        let embed_text = build_embed_text(&entity);
        let expected = "login — calls normalize email, verify credentials or fail, issue token";
        assert!(
            embed_text.starts_with(expected),
            "doc-less callable must lead with its behavioural vocabulary: {embed_text:?}"
        );
        // The full Calls: section is still there (doc-less recall contract).
        assert!(embed_text.contains("\nCalls: normalize email"));
    }

    #[test]
    fn role_sentence_bare_identifier_when_nothing_known() {
        let entity = ParsedEntity::new(
            "useChangePassword",
            EntityKind::Function,
            "useChangePassword",
            None,
            None,
            "typescript",
            "src/api/authQueries.ts",
            5,
            12,
            None,
            "ui",
        );
        let embed_text = build_embed_text(&entity);
        assert!(
            embed_text.starts_with("use change password\n"),
            "{embed_text:?}"
        );
        assert!(
            !embed_text.contains("—"),
            "no call list may appear: {embed_text:?}"
        );
    }

    #[test]
    fn role_sentence_truncates_long_docstring_on_char_boundary() {
        // 1000 chars of multibyte prose with no sentence boundary: must not
        // panic (byte-slicing UTF-8 would) and must emit the ellipsis.
        let mut doc = String::new();
        while doc.chars().count() < 1000 {
            doc.push_str("sin() y así ");
        }
        let entity = ParsedEntity::new(
            "weave",
            EntityKind::RustFunction,
            "weave",
            None,
            Some(doc),
            "rust",
            "src/weave.rs",
            1,
            10,
            None,
            "test-repo",
        );
        let embed_text = build_embed_text(&entity);
        let role = embed_text
            .lines()
            .next()
            .expect("role line exists")
            .to_string();
        let char_count = role.chars().count();
        // phrase ("weave") + ": " + sentence(max 200) + …
        assert!(
            char_count <= ROLE_SENTENCE_MAX_CHARS + "weave: ".chars().count() + 1,
            "role line must be bounded: {char_count} chars"
        );
        assert!(
            role.ends_with('…'),
            "cut mid-sentence must show ellipsis: {role:?}"
        );
    }

    #[test]
    fn role_sentence_strips_comment_markers() {
        let entity = ParsedEntity::new(
            "init",
            EntityKind::GroovyMethod,
            "nextflow.plugin.extension.PluginExtensionPoint.init",
            None,
            Some("/**\n * Channel factory initialization.\n */".to_string()),
            "groovy",
            "PluginExtensionPoint.groovy",
            12,
            12,
            Some("PluginExtensionPoint".to_string()),
            "test-repo",
        );
        let embed_text = build_embed_text(&entity);
        assert!(
            embed_text.starts_with("init: Channel factory initialization.\n"),
            "comment markers must not leak into the role line: {embed_text:?}"
        );
    }

    #[test]
    fn embed_text_role_line_precedes_structural_fields() {
        let mut entity = ParsedEntity::new(
            "saveUser",
            EntityKind::Method,
            "UserService.saveUser",
            Some("public void saveUser(User user)".to_string()),
            Some("Saves a new user to the database.".to_string()),
            "java",
            "UserService.java",
            42,
            50,
            Some("UserService".to_string()),
            "test-repo",
        );
        entity.reference_intents.push(ReferenceIntent::Call {
            method: "to_database".to_string(),
            receiver: None,
            line: 1,
            arg_count: None,
        });
        let embed_text = build_embed_text(&entity);
        let role_idx = embed_text.lines().take(1).count();
        let sig_idx = embed_text
            .lines()
            .position(|l| l.starts_with("Signature:"))
            .expect("signature present")
            + 1;
        let calls_idx = embed_text
            .lines()
            .position(|l| l.starts_with("Calls:"))
            .expect("calls present")
            + 1;
        assert!(role_idx < sig_idx, "role must precede Signature");
        assert!(role_idx < calls_idx, "role must precede Calls");
        // The docstring body itself still follows.
        assert!(embed_text.contains("\nSaves a new user to the database.\n"));
    }

    #[test]
    fn role_sentence_handles_non_code_kinds() {
        // Config/build entities have no behaviour: the role line collapses
        // to the identifier phrase and must not panic.
        let entity = ParsedEntity::new(
            "org.springframework:spring-core:5.3.29",
            EntityKind::BuildDependency,
            "org.springframework:spring-core:5.3.29",
            Some("scope: compile".to_string()),
            Some("Maven dependency: org.springframework:spring-core:5.3.29".to_string()),
            "xml",
            "pom.xml",
            15,
            19,
            None,
            "test-repo",
        );
        let embed_text = build_embed_text(&entity);
        let first = embed_text.lines().next().unwrap();
        // Docstring branch fires for any kind; the phrase still leads and
        // nothing about config/build kinds panics or misformats.
        assert!(
            first.starts_with("org springframework spring core 5 3 29: "),
            "{first:?}"
        );
    }

    #[test]
    fn role_sentence_uses_shorter_call_cap_than_calls_section() {
        let mut entity = ParsedEntity::new(
            "hub",
            EntityKind::RustFunction,
            "hub",
            None,
            None,
            "rust",
            "src/hub.rs",
            1,
            2,
            None,
            "r",
        );
        for i in 0..MAX_CALL_NAMES + 1 {
            entity.reference_intents.push(ReferenceIntent::Call {
                method: format!("callee_{i:02}"),
                receiver: None,
                line: i + 1,
                arg_count: None,
            });
        }
        let embed_text = build_embed_text(&entity);
        let role = embed_text.lines().next().unwrap();
        assert_eq!(
            role.matches("callee").count(),
            ROLE_CALL_NAMES,
            "role line capped at {ROLE_CALL_NAMES} names: {role:?}"
        );
        let calls = embed_text
            .lines()
            .find(|l| l.starts_with("Calls:"))
            .expect("Calls section");
        assert_eq!(calls.matches("callee").count(), MAX_CALL_NAMES);
    }
}