symforge 11.7.2

SymForge — in-memory code intelligence MCP server for Claude Code, Codex, and Gemini CLI
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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
//! Shared symbol-context renderers, trace capture, and reference-section evidence.
use super::file_read::{not_found_file, not_found_symbol_names};
use super::read_context::{
    ContextSourceAuthority, build_with_budget, format_context_envelope, parse_state_label,
};
use super::reference_read::{
    OutputLimits, find_dependents_compact_view, find_dependents_result_view,
    implementations_result_view,
};
use super::source::enforce_token_budget;
#[cfg(feature = "server")]
use crate::live_index::LiveIndex;
use crate::live_index::{
    ContextBundleFoundView, ContextBundleReferenceView, ContextBundleSectionView,
    ContextBundleView, ImplBlockSuggestionView, IndexedFile, TypeDependencyView,
};

/// Get full context bundle for a symbol: definition body + callers + callees + type usages.
///
/// Each section is capped at 20 entries with "...and N more" overflow.
#[cfg(feature = "server")]
pub fn context_bundle_result(
    index: &LiveIndex,
    path: &str,
    name: &str,
    kind_filter: Option<&str>,
) -> String {
    let view = index.capture_context_bundle_view(path, name, kind_filter, None);
    context_bundle_result_view(&view, "full")
}

#[cfg(feature = "server")]
pub fn context_bundle_result_view(view: &ContextBundleView, verbosity: &str) -> String {
    context_bundle_result_view_with_max_tokens(view, verbosity, None)
}

pub fn context_bundle_impl_suggestion_tip(view: &ContextBundleView) -> String {
    match view {
        ContextBundleView::Found(view) => format_impl_block_suggestions(view.as_ref()),
        _ => String::new(),
    }
}

/// Extract a compact callees section from a context bundle view for use in
/// default `get_symbol_context` mode (which otherwise only shows callers).
pub fn context_bundle_callees_text(view: &ContextBundleView) -> String {
    match view {
        ContextBundleView::Found(found) if found.callees.total_count > 0 => {
            format_context_bundle_section("Callees", &found.callees)
        }
        _ => String::new(),
    }
}

pub fn context_bundle_result_view_with_max_tokens(
    view: &ContextBundleView,
    verbosity: &str,
    max_tokens: Option<u64>,
) -> String {
    match view {
        ContextBundleView::FileNotFound { path } => not_found_file(path),
        ContextBundleView::AmbiguousSymbol {
            path,
            name,
            candidate_lines,
        } => format!(
            "Ambiguous symbol selector for {name} in {path}; pass `symbol_line` to disambiguate. Candidates: {}",
            candidate_lines
                .iter()
                .map(u32::to_string)
                .collect::<Vec<_>>()
                .join(", ")
        ),
        ContextBundleView::SymbolNotFound {
            relative_path,
            symbol_names,
            name,
        } => not_found_symbol_names(relative_path, symbol_names, name),
        ContextBundleView::Found(view) => {
            render_context_bundle_found_with_max_tokens(view.as_ref(), verbosity, max_tokens)
        }
    }
}

fn render_context_bundle_found_with_max_tokens(
    view: &ContextBundleFoundView,
    verbosity: &str,
    max_tokens: Option<u64>,
) -> String {
    let (body, actual_level) = resolve_verbosity(
        &view.body,
        Some(verbosity),
        max_tokens,
        0.4, // allocate 40% of budget to body, leave room for deps
    );
    let mut output = format!(
        "{}\n[{}, {}:{}-{}, {} bytes]\n",
        body,
        view.kind_label,
        view.file_path,
        view.line_range.0 + 1,
        view.line_range.1 + 1,
        view.byte_count
    );
    if actual_level != "full" && actual_level != verbosity {
        output.push_str(&format!(
            "[adaptive verbosity: {} — body reduced to fit {} token budget]\n",
            actual_level,
            max_tokens.unwrap_or(0)
        ));
    }
    output.push_str(&format_context_bundle_section("Callers", &view.callers));
    output.push_str(&format_context_bundle_section("Callees", &view.callees));
    output.push_str(&format_unresolved_same_name_member_calls(
        &view.unresolved_same_name_member_calls,
    ));
    output.push_str(&format_context_bundle_section(
        "Type usages",
        &view.type_usages,
    ));
    match max_tokens {
        Some(max_tokens) => {
            let max_bytes = (max_tokens as usize).saturating_mul(4);
            if max_bytes > 0 && output.len() > max_bytes {
                let mut truncated = truncate_text_at_line_boundary(&output, max_bytes);
                truncated.push_str(&format_bundle_truncation_notice(max_tokens, None));
                if !view.implementation_suggestions.is_empty() {
                    truncated.push_str(&format_impl_block_suggestions(view));
                }
                return truncated;
            }

            let (dep_text, omitted) =
                format_type_dependencies_with_budget(&view.dependencies, max_bytes, output.len());
            output.push_str(&dep_text);
            if omitted > 0 {
                output.push_str(&format_bundle_truncation_notice(max_tokens, Some(omitted)));
            }
        }
        None => {
            if !view.dependencies.is_empty() {
                output.push_str(&format_type_dependencies(&view.dependencies));
            }
        }
    }
    if !view.implementation_suggestions.is_empty() {
        output.push_str(&format_impl_block_suggestions(view));
    }
    output
}

/// Format results of `trace_symbol`.
pub fn trace_symbol_result_view(
    view: &crate::live_index::TraceSymbolView,
    name: &str,
    verbosity: &str,
    max_tokens: Option<u64>,
) -> String {
    match view {
        crate::live_index::TraceSymbolView::FileNotFound { path } => not_found_file(path),
        crate::live_index::TraceSymbolView::AmbiguousSymbol {
            path,
            name,
            candidate_lines,
        } => format!(
            "Ambiguous symbol selector for {name} in {path}; pass `symbol_line` to disambiguate. Candidates: {}",
            candidate_lines
                .iter()
                .map(u32::to_string)
                .collect::<Vec<_>>()
                .join(", ")
        ),
        crate::live_index::TraceSymbolView::SymbolNotFound {
            relative_path,
            symbol_names,
            name,
        } => not_found_symbol_names(relative_path, symbol_names, name),
        crate::live_index::TraceSymbolView::Found(found) => {
            let mut output = render_context_bundle_found_with_max_tokens(
                &found.context_bundle,
                verbosity,
                max_tokens,
            );

            if !found.siblings.is_empty() {
                output.push_str(&format_siblings_with_budget(
                    &found.siblings,
                    output.len(),
                    max_tokens,
                ));
            }

            if !found.dependents.files.is_empty() {
                let dependents_fn = if verbosity == "full" {
                    find_dependents_result_view
                } else {
                    find_dependents_compact_view
                };
                let section = format!(
                    "\n\n{}",
                    dependents_fn(
                        &found.dependents,
                        &found.context_bundle.file_path,
                        &OutputLimits::default(),
                    )
                );
                append_budgeted_trace_section(
                    &mut output,
                    &section,
                    max_tokens,
                    "Dependents",
                    found.dependents.files.len(),
                );
            }

            if !found.implementations.entries.is_empty() {
                let section = format!(
                    "\n\n{}",
                    implementations_result_view(
                        &found.implementations,
                        name,
                        &OutputLimits::default(),
                    )
                );
                append_budgeted_trace_section(
                    &mut output,
                    &section,
                    max_tokens,
                    "Implementations",
                    found.implementations.entries.len(),
                );
            }

            if let Some(git) = &found.git_activity {
                let section = format_trace_git_activity(git);
                append_budgeted_trace_section(&mut output, &section, max_tokens, "Git activity", 1);
            }

            enforce_token_budget(output, max_tokens)
        }
    }
}

fn append_budgeted_trace_section(
    output: &mut String,
    section: &str,
    max_tokens: Option<u64>,
    label: &str,
    omitted_count: usize,
) {
    let Some(max_tokens) = max_tokens.filter(|tokens| *tokens > 0) else {
        output.push_str(section);
        return;
    };
    let max_bytes = (max_tokens as usize).saturating_mul(4);

    if output.len().saturating_add(section.len()) <= max_bytes {
        output.push_str(section);
        return;
    }

    let detail = format!("{label} section omitted {omitted_count} item(s).");
    output.push_str(&format!(
        "\n\n{}",
        token_truncation_notice(max_tokens, Some(detail.as_str()))
    ));
}

fn format_siblings_with_budget(
    siblings: &[crate::live_index::SiblingSymbolView],
    current_bytes: usize,
    max_tokens: Option<u64>,
) -> String {
    let Some(max_tokens) = max_tokens.filter(|tokens| *tokens > 0) else {
        return format_siblings(siblings, 0);
    };
    let max_bytes = (max_tokens as usize).saturating_mul(4);

    let mut section = "\nNearby siblings:".to_string();
    let mut shown = 0usize;
    for sib in siblings {
        let line = format!(
            "\n  {:<12} {:<30} {}-{}",
            sib.kind_label, sib.name, sib.line_range.0, sib.line_range.1
        );
        if current_bytes
            .saturating_add(section.len())
            .saturating_add(line.len())
            > max_bytes
        {
            break;
        }
        section.push_str(&line);
        shown += 1;
    }

    let omitted = siblings.len().saturating_sub(shown);
    if omitted > 0 {
        let detail = format!("{omitted} additional sibling(s) not shown.");
        section.push_str(&format!(
            "\n  {}",
            token_truncation_notice(max_tokens, Some(detail.as_str()))
        ));
    }

    section
}

use crate::index_lifecycle::guidance::symbol_read::format_siblings;

fn format_trace_git_activity(git: &crate::live_index::GitActivityView) -> String {
    let mut lines = vec![String::new()];
    lines.push(format!(
        "Git activity:  {} {:.2} ({})    {} commits, last {}",
        git.churn_bar, git.churn_score, git.churn_label, git.commit_count, git.last_relative,
    ));
    lines.push(format!(
        "  Last:  {} \"{}\" ({}, {})",
        git.last_hash, git.last_message, git.last_author, git.last_timestamp,
    ));
    if !git.owners.is_empty() {
        lines.push(format!("  Owners: {}", git.owners.join(", ")));
    }
    if !git.co_changes.is_empty() {
        lines.push("  Co-changes:".to_string());
        for (path, coupling, shared) in &git.co_changes {
            lines.push(format!(
                "    {}  ({:.2} coupling, {} shared commits)",
                path, coupling, shared,
            ));
        }
    }
    lines.join("\n")
}

fn format_context_bundle_section(title: &str, section: &ContextBundleSectionView) -> String {
    // Detect if this section has deduplicated entries (any occurrence_count > 1).
    let has_dedup = section.entries.iter().any(|e| e.occurrence_count > 1);

    let header =
        if has_dedup && section.unique_count > 0 && section.unique_count < section.total_count {
            format!(
                "\n{title} ({} total, {} unique):",
                section.total_count, section.unique_count
            )
        } else {
            format!("\n{title} ({}):", section.total_count)
        };

    let mut lines = vec![header];

    let mut external_count = 0usize;

    for entry in &section.entries {
        if is_external_symbol(&entry.display_name, &entry.file_path) {
            external_count += 1;
        }

        // Build the name part, appending ×N for deduplicated entries.
        let name_part = if entry.occurrence_count > 1 {
            format!("{} (×{})", entry.display_name, entry.occurrence_count)
        } else {
            entry.display_name.clone()
        };

        if let Some(enclosing) = &entry.enclosing {
            lines.push(format!(
                "  {:<30} {}:{}  {}",
                name_part, entry.file_path, entry.line_number, enclosing
            ));
        } else {
            lines.push(format!(
                "  {:<30} {}:{}",
                name_part, entry.file_path, entry.line_number
            ));
        }
    }

    if section.overflow_count > 0 {
        // Estimate external ratio from shown entries and extrapolate
        let shown = section.entries.len();
        let est_external = if shown > 0 {
            (external_count as f64 / shown as f64 * section.overflow_count as f64).round() as usize
        } else {
            0
        };
        let est_project = section.overflow_count.saturating_sub(est_external);
        if has_dedup {
            // For deduplicated sections, overflow is in unique callee names
            lines.push(format!(
                "  ...and {} more unique {}",
                section.overflow_count,
                title.to_lowercase()
            ));
        } else if est_external > 0 {
            lines.push(format!(
                "  ...and {} more {} ({} project, ~{} stdlib/framework)",
                section.overflow_count,
                title.to_lowercase(),
                est_project,
                est_external
            ));
        } else {
            lines.push(format!(
                "  ...and {} more {}",
                section.overflow_count,
                title.to_lowercase()
            ));
        }
    }

    lines.join("\n")
}

/// Render the SF-002 "unresolved same-name member calls" section.
///
/// These are `receiver.<target_name>()` calls made from inside the target
/// symbol's body where the receiver type could not be resolved — for example a
/// TypeScript `Controller.foo` whose body calls `this.service.foo()`. They are
/// surfaced under a clearly-labeled line so they are visibly distinct from the
/// exact `Callers`/`Callees` counts, which deliberately exclude them. Renders
/// nothing when empty so the common case keeps its existing output shape.
fn format_unresolved_same_name_member_calls(entries: &[ContextBundleReferenceView]) -> String {
    if entries.is_empty() {
        return String::new();
    }

    let mut lines = vec![format!(
        "\nUnresolved same-name member calls ({}) [receiver type unresolved; not counted as callers/callees]:",
        entries.len()
    )];

    for entry in entries {
        lines.push(format!(
            "  {:<30} {}:{}",
            entry.display_name, entry.file_path, entry.line_number
        ));
    }

    lines.join("\n")
}

/// Heuristic: classify a symbol reference as external (stdlib/framework) vs project-defined.
fn is_external_symbol(name: &str, file_path: &str) -> bool {
    // No file path means it's a builtin/external
    if file_path.is_empty() {
        return true;
    }
    // Common stdlib/framework patterns across languages
    let external_prefixes = [
        "std::",
        "core::",
        "alloc::",
        "System.",
        "Microsoft.",
        "java.",
        "javax.",
        "kotlin.",
        "android.",
        "console.",
        "JSON.",
        "Math.",
        "Object.",
        "Array.",
        "String.",
        "Promise.",
        "Map.",
        "Set.",
        "Error.",
    ];
    for prefix in &external_prefixes {
        if name.starts_with(prefix) {
            return true;
        }
    }
    // Single-word lowercase names that are very common builtins
    let common_builtins = [
        "println",
        "print",
        "eprintln",
        "format",
        "vec",
        "to_string",
        "clone",
        "unwrap",
        "expect",
        "push",
        "pop",
        "len",
        "is_empty",
        "iter",
        "map",
        "filter",
        "collect",
        "into",
        "from",
        "default",
        "new",
        "Add",
        "Sub",
        "Display",
        "Debug",
        "ToString",
        "log",
        "warn",
        "error",
        "info",
        "LogWarning",
        "LogError",
        "LogInformation",
        "Console",
    ];
    common_builtins.contains(&name)
}

/// Extract the full signature from a symbol body.
///
/// Handles common patterns: `fn foo(...)`, `pub struct Foo`, `class Bar`, etc.
/// Skips leading doc comments, then collects lines until the declaration is
/// complete (opening brace `{`, `where` clause, or terminating `;`).
/// Multi-line signatures are joined on one line with spaces, preserving
/// visibility, generic parameters, and return type.
use crate::index_lifecycle::guidance::source::extract_signature;

/// Extract the first doc-comment line from a symbol body.
///
/// Looks for `///`, `//!`, `/** ... */`, `# ...` (Python docstring-adjacent),
/// or `/* ... */` style comments immediately before/after the signature.
fn extract_first_doc_line(body: &str) -> Option<String> {
    for line in body.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        // Rust doc comments
        if let Some(rest) = trimmed.strip_prefix("///") {
            let doc = rest.trim();
            if !doc.is_empty() {
                return Some(doc.to_string());
            }
        }
        // Rust inner doc comments
        if let Some(rest) = trimmed.strip_prefix("//!") {
            let doc = rest.trim();
            if !doc.is_empty() {
                return Some(doc.to_string());
            }
        }
        // C-style block doc comments
        if let Some(rest) = trimmed.strip_prefix("/**") {
            let doc = rest.trim_end_matches("*/").trim();
            if !doc.is_empty() {
                return Some(doc.to_string());
            }
        }
        // XML doc comments (C#)
        if trimmed.starts_with("/// <summary>") || trimmed.starts_with("/// <remarks>") {
            continue; // skip XML tags, look for actual text
        }
        // Python/JS docstrings
        if trimmed.starts_with("\"\"\"") || trimmed.starts_with("'''") {
            let doc = trimmed
                .trim_start_matches("\"\"\"")
                .trim_start_matches("'''")
                .trim_end_matches("\"\"\"")
                .trim_end_matches("'''")
                .trim();
            if !doc.is_empty() {
                return Some(doc.to_string());
            }
        }
        // If we hit a non-comment line, stop looking
        if !trimmed.starts_with("//")
            && !trimmed.starts_with("/*")
            && !trimmed.starts_with('*')
            && !trimmed.starts_with('#')
        {
            break;
        }
    }
    None
}

/// Apply verbosity filter to a symbol body.
///
/// - `"summary"`: one-line natural language summary (doc comment or heuristic from name/signature).
/// - `"signature"`: full declaration line — visibility, name, generics, params, return type (~80% smaller).
/// - `"compact"`: signature + first doc-comment line.
/// - `"full"` or anything else: complete body (default).
pub(crate) fn apply_verbosity(body: &str, verbosity: &str) -> String {
    match verbosity {
        "summary" => auto_summarize(body),
        "signature" => extract_signature(body),
        "compact" => {
            let sig = extract_signature(body);
            if let Some(doc) = extract_first_doc_line(body) {
                format!("{sig}\n  // {doc}")
            } else {
                sig
            }
        }
        _ => body.to_string(),
    }
}

/// Auto-select the richest verbosity level whose output fits within a token budget.
///
/// Cascades through `full → compact → signature → summary`, returning the first
/// level whose rendered output is ≤ `body_budget_tokens * 4` bytes.  Always returns
/// at least the summary level even if it exceeds the budget.
///
/// Returns `(rendered_body, level_name)`.
pub(crate) fn adaptive_verbosity(body: &str, body_budget_tokens: u64) -> (String, &'static str) {
    let max_bytes = (body_budget_tokens as usize).saturating_mul(4);

    // Try full first
    if body.len() <= max_bytes {
        return (body.to_string(), "full");
    }

    // Try compact
    let compact = apply_verbosity(body, "compact");
    if compact.len() <= max_bytes {
        return (compact, "compact");
    }

    // Try signature
    let signature = apply_verbosity(body, "signature");
    if signature.len() <= max_bytes {
        return (signature, "signature");
    }

    // Summary as last resort
    (apply_verbosity(body, "summary"), "summary")
}

/// Resolve verbosity for a symbol body, with adaptive fallback.
///
/// - If `explicit_verbosity` is `Some` (user chose a level), applies it directly.
/// - If `max_tokens` is `Some` and no explicit verbosity, auto-selects the richest
///   level fitting within `max_tokens * body_fraction`.
/// - Otherwise returns the full body.
///
/// `body_fraction` controls how much of the total token budget is allocated to the
/// body (e.g. 0.4 for bundle mode where dependencies need space, 0.7 for standalone).
///
/// Returns `(rendered_body, level_name)`.
pub(crate) fn resolve_verbosity(
    body: &str,
    explicit_verbosity: Option<&str>,
    max_tokens: Option<u64>,
    body_fraction: f64,
) -> (String, &'static str) {
    // Explicit user choice always wins
    if let Some(v) = explicit_verbosity
        && v != "full"
    {
        let level: &'static str = match v {
            "summary" => "summary",
            "signature" => "signature",
            "compact" => "compact",
            _ => "full",
        };
        return (apply_verbosity(body, v), level);
    }

    // Adaptive: auto-select when budget is set and no explicit verbosity (or explicit "full")
    if let Some(tokens) = max_tokens
        && tokens > 0
    {
        let body_budget = (tokens as f64 * body_fraction) as u64;
        let (rendered, level) = adaptive_verbosity(body, body_budget);
        if level != "full" {
            return (rendered, level);
        }
    }

    // Default: full
    (body.to_string(), "full")
}

/// Generate a one-line natural language summary for a symbol body.
///
/// Priority:
/// 1. First doc-comment line (if present and meaningful)
/// 2. Heuristic from function name patterns (get_, set_, is_, new, from_, etc.)
/// 3. Signature-based fallback with parameter/return type info
pub(crate) fn auto_summarize(body: &str) -> String {
    // Try doc comment first
    if let Some(doc) = extract_first_doc_line(body) {
        // Ensure it's meaningful (not just a tag or very short)
        if doc.len() > 5 && !doc.starts_with('@') && !doc.starts_with('<') {
            return doc;
        }
    }

    let sig = extract_signature(body);

    // Extract the function/type name from the signature
    let name = extract_declaration_name(&sig).unwrap_or_default();
    if name.is_empty() {
        return sig;
    }

    // Try heuristic summary based on name patterns
    if let Some(heuristic) = heuristic_from_name(&name, &sig) {
        return heuristic;
    }

    // Fallback: signature-based summary
    sig
}

/// Generate a heuristic summary from common naming patterns.
pub(crate) fn heuristic_from_name(name: &str, sig: &str) -> Option<String> {
    let lower = name.to_ascii_lowercase();

    // Common prefixes with semantic meaning
    let patterns: &[(&str, &str)] = &[
        ("test_", "Test: "),
        ("get_", "Returns the "),
        ("set_", "Sets the "),
        ("is_", "Checks whether "),
        ("has_", "Checks whether it has "),
        ("should_", "Checks whether it should "),
        ("can_", "Checks whether it can "),
        ("with_", "Creates a copy with "),
        ("from_", "Constructs from "),
        ("into_", "Converts into "),
        ("try_", "Attempts to "),
        ("parse_", "Parses "),
        ("render_", "Renders "),
        ("format_", "Formats "),
        ("validate_", "Validates "),
        ("build_", "Builds "),
        ("create_", "Creates "),
        ("make_", "Creates "),
        ("load_", "Loads "),
        ("save_", "Saves "),
        ("read_", "Reads "),
        ("write_", "Writes "),
        ("find_", "Finds "),
        ("search_", "Searches for "),
        ("collect_", "Collects "),
        ("compute_", "Computes "),
        ("calculate_", "Calculates "),
        ("update_", "Updates "),
        ("delete_", "Deletes "),
        ("remove_", "Removes "),
        ("add_", "Adds "),
        ("insert_", "Inserts "),
        ("handle_", "Handles "),
        ("process_", "Processes "),
        ("run_", "Runs "),
        ("execute_", "Executes "),
        ("start_", "Starts "),
        ("stop_", "Stops "),
        ("init_", "Initializes "),
        ("setup_", "Sets up "),
        ("cleanup_", "Cleans up "),
        ("resolve_", "Resolves "),
        ("normalize_", "Normalizes "),
        ("convert_", "Converts "),
        ("transform_", "Transforms "),
        ("apply_", "Applies "),
        ("check_", "Checks "),
        ("ensure_", "Ensures "),
        ("spawn_", "Spawns "),
        ("emit_", "Emits "),
        ("dispatch_", "Dispatches "),
        ("register_", "Registers "),
        ("detect_", "Detects "),
        ("extract_", "Extracts "),
        ("capture_", "Captures "),
        ("record_", "Records "),
    ];

    for (prefix, verb) in patterns {
        if lower.starts_with(prefix) {
            let rest = &name[prefix.len()..];
            let readable = rest.replace('_', " ");
            let mut chars = readable.chars();
            let capitalized = match chars.next() {
                Some(first) => {
                    let mut text = first.to_uppercase().collect::<String>();
                    text.push_str(chars.as_str());
                    text
                }
                None => readable,
            };
            return Some(format!("{verb}{capitalized}"));
        }
    }

    // Special cases
    if lower == "new" || lower == "default" {
        // Check if it's inside an impl block
        if sig.contains("impl") || sig.contains("Self") || sig.contains("->") {
            return Some("Constructor".to_string());
        }
    }

    if lower == "drop" {
        return Some("Destructor / cleanup on drop".to_string());
    }

    if lower == "fmt" && sig.contains("Formatter") {
        return Some("Display/Debug formatting implementation".to_string());
    }

    // Struct/enum/type with field count
    if sig.contains("struct ") || sig.contains("class ") {
        return Some(format!("Data type: {name}"));
    }
    if sig.contains("enum ") {
        return Some(format!("Enumeration: {name}"));
    }
    if sig.contains("trait ") || sig.contains("interface ") {
        return Some(format!("Interface/trait: {name}"));
    }
    if sig.contains("impl ") {
        return Some(format!("Implementation block for {name}"));
    }

    None
}

pub(crate) fn format_type_dependencies(deps: &[TypeDependencyView]) -> String {
    let mut output = format!("\nDependencies ({}):", deps.len());
    for dep in deps {
        output.push_str(&format_type_dependency(dep));
    }
    output
}

fn format_type_dependencies_with_budget(
    deps: &[TypeDependencyView],
    max_bytes: usize,
    base_len: usize,
) -> (String, usize) {
    if deps.is_empty() || max_bytes == 0 {
        return (String::new(), 0);
    }

    let mut rendered = String::new();
    let header = format!("\nDependencies ({}):", deps.len());
    let mut header_added = false;
    let mut included = 0usize;

    for dep in deps {
        let dep_block = format_type_dependency(dep);
        let header_cost = if header_added { 0 } else { header.len() };
        if base_len + rendered.len() + header_cost + dep_block.len() > max_bytes {
            break;
        }
        if !header_added {
            rendered.push_str(&header);
            header_added = true;
        }
        rendered.push_str(&dep_block);
        included += 1;
    }

    if included == deps.len() {
        return (rendered, 0);
    }
    if included == 0 {
        return (String::new(), deps.len());
    }
    (rendered, deps.len().saturating_sub(included))
}

fn format_type_dependency(dep: &TypeDependencyView) -> String {
    let depth_marker = if dep.depth > 0 {
        format!(" (depth {})", dep.depth)
    } else {
        String::new()
    };
    format!(
        "\n── {} [{}, {}:{}-{}{}] ──\n{}",
        dep.name,
        dep.kind_label,
        dep.file_path,
        dep.line_range.0 + 1,
        dep.line_range.1 + 1,
        depth_marker,
        dep.body
    )
}

fn format_impl_block_suggestions(view: &ContextBundleFoundView) -> String {
    let is_type_definition = matches!(view.kind_label.as_str(), "struct" | "enum");
    if !is_type_definition
        || view.callers.total_count != 0
        || view.implementation_suggestions.is_empty()
    {
        return String::new();
    }

    let mut output = format!(
        "\nTip: This {} has 0 direct callers. Try `get_symbol_context` on one of its impl blocks:",
        view.kind_label
    );
    for suggestion in &view.implementation_suggestions {
        output.push_str(&format_impl_block_suggestion(suggestion));
    }
    output.push('\n');
    output
}

fn format_impl_block_suggestion(suggestion: &ImplBlockSuggestionView) -> String {
    format!(
        "\n- {} ({}:{})",
        suggestion.display_name, suggestion.file_path, suggestion.line_number
    )
}

use crate::index_lifecycle::guidance::source::{
    token_truncation_footer, token_truncation_notice, truncate_text_at_line_boundary,
};

fn format_bundle_truncation_notice(max_tokens: u64, omitted_dependencies: Option<usize>) -> String {
    let detail = omitted_dependencies
        .map(|count| format!("{count} additional type dependencies not shown."));
    token_truncation_footer(max_tokens, detail.as_deref())
}

fn is_likely_type_keyword(word: &str) -> bool {
    matches!(
        word,
        "string"
            | "String"
            | "int"
            | "Int32"
            | "Int64"
            | "bool"
            | "Boolean"
            | "float"
            | "double"
            | "decimal"
            | "char"
            | "byte"
            | "long"
            | "short"
            | "uint"
            | "object"
            | "var"
            | "number"
            | "bigint"
            | "any"
    )
}

pub(crate) fn extract_declaration_name(line: &str) -> Option<String> {
    // Strip leading visibility modifier generically: pub, pub(crate), pub(super), pub(in path).
    let stripped = if let Some(rest) = line.strip_prefix("pub") {
        if let Some(after_paren) = rest.strip_prefix('(') {
            // Skip balanced parens: pub(crate), pub(super), pub(in crate::foo)
            if let Some(close) = after_paren.find(')') {
                after_paren[close + 1..].trim_start()
            } else {
                rest.trim_start()
            }
        } else {
            rest.trim_start()
        }
    } else if let Some(rest) = line.strip_prefix("export default ") {
        rest
    } else if let Some(rest) = line.strip_prefix("export ") {
        rest
    } else {
        line
    };

    let keywords = [
        "async fn ",
        "fn ",
        "struct ",
        "enum ",
        "trait ",
        "type ",
        "const ",
        "static ",
        "class ",
        "interface ",
        "function ",
        "async function ",
        "async def ",
        "def ",
    ];

    for kw in &keywords {
        if let Some(rest) = stripped.strip_prefix(kw) {
            let name: String = rest
                .chars()
                .take_while(|c| c.is_alphanumeric() || *c == '_')
                .collect();
            if name.is_empty() {
                continue;
            }
            // For `const`, the first word might be a type name (C#: `const string Foo`).
            // If it looks like a well-known type, skip it and take the next identifier.
            if *kw == "const " && is_likely_type_keyword(&name) {
                let after_type = &rest[name.len()..].trim_start();
                let real_name: String = after_type
                    .chars()
                    .take_while(|c| c.is_alphanumeric() || *c == '_')
                    .collect();
                if !real_name.is_empty() {
                    return Some(real_name);
                }
            }
            return Some(name);
        }
    }
    None
}

pub(crate) fn context_bundle_completeness_label(
    view: &crate::live_index::ContextBundleFoundView,
    rendered: &str,
) -> String {
    let section_overflow =
        view.callers.overflow_count + view.callees.overflow_count + view.type_usages.overflow_count;
    let mut parts = Vec::new();
    if rendered.contains("Truncated at ~") {
        parts.push("budget-limited".to_string());
    }
    if section_overflow > 0 {
        parts.push(format!(
            "section-capped ({} additional reference entries not shown)",
            section_overflow
        ));
    }
    if parts.is_empty() {
        "full".to_string()
    } else {
        parts.join("; ")
    }
}

pub(crate) fn format_ambiguous_symbol_context(name: &str, candidates: &[String]) -> String {
    let mut output = format!(
        "Ambiguous symbol selector: {} symbols named \"{}\" found.\nPass `path` or `file` to disambiguate.\nCandidate paths:",
        candidates.len(),
        name
    );
    for path in candidates {
        output.push_str(&format!("\n- {path}"));
    }
    output
}

pub(crate) fn render_symbol_context_header(
    file: &IndexedFile,
    name: &str,
    symbol_kind: Option<&str>,
    symbol_line: Option<u32>,
    verbosity: &str,
    max_tokens: Option<u64>,
) -> Option<String> {
    use crate::live_index::query::{SymbolSelectorMatch, resolve_symbol_selector};

    match resolve_symbol_selector(file, name, symbol_kind, symbol_line) {
        SymbolSelectorMatch::Selected(_, sym) => {
            let body = std::str::from_utf8(
                &file.content[sym.byte_range.0 as usize..sym.byte_range.1 as usize],
            )
            .ok()?;
            let (rendered, actual_level) = resolve_verbosity(
                body,
                Some(verbosity),
                max_tokens,
                0.7, // standalone symbol — allocate 70% of budget to body
            );
            let mut output = format!(
                "{}\n[{}, {}:{}-{}]",
                rendered,
                sym.kind,
                file.relative_path,
                sym.line_range.0 + 1,
                sym.line_range.1 + 1
            );
            if actual_level != "full" && actual_level != verbosity {
                output.push_str(&format!(
                    "\n[adaptive verbosity: {} — fits within {} token budget]",
                    actual_level,
                    max_tokens.unwrap_or(0)
                ));
            }
            Some(output)
        }
        SymbolSelectorMatch::NotFound | SymbolSelectorMatch::Ambiguous(_) => None,
    }
}

pub(crate) fn capture_trace_symbol_view(
    published: &crate::live_index::PublishedGeneration,
    path: &str,
    name: &str,
    kind: Option<&str>,
    symbol_line: Option<u32>,
    sections: Option<&[String]>,
    include_tests: bool,
) -> crate::live_index::TraceSymbolView {
    let mut trace_view = published.live.capture_trace_symbol_view(
        path,
        name,
        kind,
        symbol_line,
        sections,
        include_tests,
    );

    if let crate::live_index::TraceSymbolView::Found(ref mut found) = trace_view {
        let wants_git = sections
            .map(|values| values.iter().any(|value| value.eq_ignore_ascii_case("git")))
            .unwrap_or(true);

        if wants_git {
            let temporal = &published.code_signals.temporal;
            if temporal.state == crate::live_index::git_temporal::GitTemporalState::Ready
                && let Some(history) = temporal.files.get(path)
            {
                use crate::live_index::git_temporal::{churn_bar, churn_label, relative_time};

                found.git_activity = Some(crate::live_index::GitActivityView {
                    churn_score: history.churn_score,
                    churn_bar: churn_bar(history.churn_score),
                    churn_label: churn_label(history.churn_score).to_string(),
                    commit_count: history.commit_count,
                    last_relative: relative_time(history.last_commit.days_ago),
                    last_hash: history.last_commit.hash.clone(),
                    last_message: history.last_commit.message_head.clone(),
                    last_author: history.last_commit.author.clone(),
                    last_timestamp: history.last_commit.timestamp.clone(),
                    owners: history
                        .contributors
                        .iter()
                        .map(|contributor| {
                            format!("{} {:.0}%", contributor.author, contributor.percentage)
                        })
                        .collect(),
                    co_changes: history
                        .co_changes
                        .iter()
                        .map(|entry| {
                            (
                                entry.path.clone(),
                                entry.coupling_score,
                                entry.shared_commits,
                            )
                        })
                        .collect(),
                });
            }
        }
    }

    trace_view
}

/// Whether any indexed file defines the selected symbol (name, plus kind and
/// line when given), by the same selector every exact lane resolves with.
// ponytail: linear scan over files, only on the empty-answer path; a name index if it grows hot.
pub(crate) fn symbol_defined_anywhere(
    index: &crate::live_index::LiveIndex,
    name: &str,
    symbol_kind: Option<&str>,
    symbol_line: Option<u32>,
) -> bool {
    use crate::live_index::query::{SymbolSelectorMatch, resolve_symbol_selector};
    index.all_files().any(|(_, file)| {
        !matches!(
            resolve_symbol_selector(file, name, symbol_kind, symbol_line),
            SymbolSelectorMatch::NotFound
        )
    })
}

pub(crate) struct SymbolContextSelector<'a> {
    pub name: &'a str,
    pub file: Option<&'a str>,
    pub path: Option<&'a str>,
    pub symbol_kind: Option<&'a str>,
    pub symbol_line: Option<u32>,
}

pub(crate) fn symbol_context_references(
    published: &crate::live_index::PublishedGeneration,
    params: &SymbolContextSelector<'_>,
    references_budget_bytes: u64,
    max_reference_entries: usize,
    source_authority: ContextSourceAuthority,
) -> (String, u64, usize) {
    let guard = published.live.as_ref();

    let references = if let Some(path) = params.path {
        match guard.find_exact_references_for_symbol(
            path,
            params.name,
            params.symbol_kind,
            params.symbol_line,
            None,
        ) {
            Ok(refs) => refs,
            Err(error) => return (error, 0, 0),
        }
    } else {
        guard.find_references_for_name(params.name, None, false)
    };

    // Group by file, applying optional file filter, capping at 10 total matches.
    let mut map: std::collections::HashMap<String, Vec<(u32, String, Option<String>)>> =
        std::collections::HashMap::new();

    let mut total = 0usize;
    let mut grand_total = 0usize;

    for (file_path, reference) in &references {
        grand_total += 1;
        if let Some(filter_file) = params.file
            && *file_path != filter_file
        {
            continue;
        }
        if total >= max_reference_entries {
            continue; // count beyond 10 but don't include
        }

        // Capture the enclosing symbol as a kind-aware display label
        // (e.g. "impl BucketManager", "struct BucketManager", "fn delta")
        // instead of bare name, so the reference list does not mislabel every
        // enclosing symbol as a function.
        let enclosing = reference.enclosing_symbol_index.and_then(|idx| {
            guard
                .get_file(file_path)
                .and_then(|f| f.symbols.get(idx as usize))
                .map(|s| super::symbol_read::symbol_kind_name_label(&s.kind.to_string(), &s.name))
        });

        map.entry(file_path.to_string()).or_default().push((
            reference.line_range.0,
            format!("{}", reference.kind),
            enclosing,
        ));
        total += 1;
    }

    // Without an exact `path`, an empty answer is a miss only when the selected
    // symbol exists nowhere in the index. A symbol defined somewhere but not
    // referenced (in the `file` filter, when given) is "No references found",
    // and references to an undefined name are still shown.
    if map.is_empty()
        && params.path.is_none()
        && !symbol_defined_anywhere(guard, params.name, params.symbol_kind, params.symbol_line)
    {
        return (
            format!("Symbol \"{}\" not found in index.", params.name),
            0,
            0,
        );
    }

    // Compute total bytes for savings (sum of content of all matched files).
    let total_bytes: u64 = map
        .keys()
        .filter_map(|fp| guard.get_file(fp))
        .map(|f| f.byte_len)
        .sum();

    let parse_state = if let Some(path) = params.path {
        guard
            .get_file(path)
            .map(parse_state_label)
            .unwrap_or_else(|| {
                aggregate_parse_state_label(std::iter::empty(), published.health.as_ref())
            })
    } else if let Some(file) = params.file {
        guard
            .get_file(file)
            .map(parse_state_label)
            .unwrap_or_else(|| {
                aggregate_parse_state_label(std::iter::empty(), published.health.as_ref())
            })
    } else {
        aggregate_parse_state_label(
            map.keys()
                .filter_map(|file_path| guard.get_file(file_path))
                .map(|file| &file.parse_status),
            published.health.as_ref(),
        )
    };

    // Sort files for deterministic output.
    let mut files: Vec<String> = map.keys().cloned().collect();
    files.sort();

    let mut evidence_anchors: Vec<String> = Vec::new();
    // Files that contributed at least one anchor. The 3-anchor cap can exhaust
    // on the first file(s); the evidence line must then say how many reference
    // files it left unnamed instead of silently undercounting usage sites.
    let mut anchored_files = 0usize;
    for file in &files {
        // safe: `files` is built from `map.keys()` immediately above; lookup cannot miss.
        let refs = map.get(file).unwrap();
        let mut sorted_refs = refs.clone();
        sorted_refs.sort_by_key(|(line, _, _)| *line);
        let before = evidence_anchors.len();
        for (line, _, _) in &sorted_refs {
            if evidence_anchors.len() >= 3 {
                break;
            }
            evidence_anchors.push(format!("{file}:{line}"));
        }
        if evidence_anchors.len() > before {
            anchored_files += 1;
        }
        if evidence_anchors.len() >= 3 {
            break;
        }
    }

    let mut body_lines: Vec<String> = Vec::new();

    for file in &files {
        body_lines.push(format!("── {} ──", file));
        // safe: `files` is built from `map.keys()` above; lookup cannot miss.
        let refs = map.get(file).unwrap();
        let mut sorted_refs = refs.clone();
        sorted_refs.sort_by_key(|(line, _, _)| *line);
        for (line, _kind, enclosing) in &sorted_refs {
            if let Some(sym_label) = enclosing {
                body_lines.push(format!("  line {}  in {}", line, sym_label));
            } else {
                body_lines.push(format!("  line {}  (module level)", line));
            }
        }
    }

    if body_lines.is_empty() {
        // Dogfood #8 (2026-07-06): hooks feed this into prompt context on
        // every Grep, so a zero-hit report must cost one line.
        body_lines.push(
            "No references found in the index (not a symbol, or only dynamic/external usage)."
                .to_string(),
        );
    }

    if total < grand_total {
        if params.file.is_some() {
            body_lines.push(format!(
                "... (showing {} of {} matches — use `path` to narrow further)",
                total, grand_total
            ));
        } else {
            body_lines.push(format!(
                "... (showing {} of {} matches — use `path` or `file` to narrow)",
                total, grand_total
            ));
        }
    }

    // Apply the references-section budget. Tool calls get ~1000 tokens
    // (4000 bytes); the prompt-context hook stays at ~100 tokens (400 bytes).
    let (body_text, remaining) = build_with_budget(&body_lines, references_budget_bytes);
    let completeness = if total < grand_total {
        "truncated"
    } else if remaining > 0 {
        "budget-limited"
    } else {
        "full"
    };
    let match_type = if params.path.is_some() && params.symbol_line.is_some() {
        "exact"
    } else if params.path.is_some() || params.file.is_some() {
        "constrained"
    } else {
        "heuristic"
    };
    let evidence = if let Some(path) = params.path {
        match params.symbol_line {
            Some(line) => format!(
                "exact selector `{path}:{line}` for symbol `{}`",
                params.name
            ),
            None => format!("path-constrained symbol `{}` in `{path}`", params.name),
        }
    } else if let Some(file) = params.file {
        format!("file filter `{file}` for symbol `{}`", params.name)
    } else if evidence_anchors.is_empty() {
        format!(
            "symbol token `{}` with no indexed reference anchors",
            params.name
        )
    } else {
        let more_files = files.len().saturating_sub(anchored_files);
        if more_files > 0 {
            format!(
                "symbol token `{}` anchored at {} (+{} more files)",
                params.name,
                evidence_anchors.join(", "),
                more_files
            )
        } else {
            format!(
                "symbol token `{}` anchored at {}",
                params.name,
                evidence_anchors.join(", ")
            )
        }
    };
    let scope = if let Some(path) = params.path {
        match params.symbol_line {
            Some(line) => format!("path `{path}`; exact selector line {line}"),
            None => format!("path `{path}`; symbol-scoped references"),
        }
    } else if let Some(file) = params.file {
        format!("file filter `{file}`; symbol token `{}`", params.name)
    } else {
        format!("repo-wide symbol token `{}`", params.name)
    };
    let envelope = format_context_envelope(
        match_type,
        source_authority,
        parse_state,
        completeness,
        scope,
        evidence,
    );
    let text = format!("{envelope}\n\n{body_text}");

    (text, total_bytes, total)
}

fn aggregate_parse_state_label<'a>(
    statuses: impl IntoIterator<Item = &'a crate::live_index::store::ParseStatus>,
    published: &crate::live_index::store::PublishedIndexState,
) -> &'static str {
    let mut saw_partial = false;
    for status in statuses {
        match status {
            crate::live_index::store::ParseStatus::Parsed => {}
            crate::live_index::store::ParseStatus::PartialParse { .. } => saw_partial = true,
            crate::live_index::store::ParseStatus::Failed { .. } => return "degraded",
        }
    }
    if saw_partial {
        "partial"
    } else if matches!(
        published.status,
        crate::live_index::store::PublishedIndexStatus::Degraded
    ) {
        "degraded"
    } else {
        "parsed"
    }
}