symforge 11.21.0

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
//! Frozen Gate I `search_knowledge` MCP formatting over the typed retrieval seam.
//!
//! Extraction, ranking, and withheld accounting live in
//! [`crate::live_index::knowledge_retrieve`]. This module never reloads the live
//! index while formatting a result, and never treats rendered MCP text as a
//! retrieval source.

use crate::domain::{CoverageStatus, FreshnessStatus, SourceLocation};
use crate::knowledge::guard_query;
use crate::live_index::knowledge_bridge::DerivedCoverage;
use crate::live_index::knowledge_retrieve::{
    KnowledgeLaneReadiness, KnowledgeRetrieveAuthorityScope, KnowledgeRetrieveHit,
    KnowledgeRetrieveLane, KnowledgeRetrieveRequest, KnowledgeRetrieveResult,
    KnowledgeRetrieveSource, normalize_knowledge_path_prefix, retrieve_knowledge,
    significant_terms,
};
use crate::live_index::{PublishedGeneration, PublishedSourceSet};

use super::review_contract::KnowledgeSourceScope;
use super::search_contract::{KnowledgeAuthorityScope, SearchKnowledgeInput};

const DEFAULT_LIMIT: usize = 10;
const MAX_LIMIT: usize = 100;
const MIN_PROVENANCE_TOKENS: u64 = 64;
/// Shortest digest prefix ever displayed; extended on collision.
const DIGEST_PREFIX_MIN: usize = 12;

/// Per-response identifier abbreviation table (SIFT-WS1, T025).
///
/// Provenance vectors mix opaque content digests with SEMANTIC rule/policy IDs
/// (`authority-history-v1`, `temporal-coverage-incomplete`). Abbreviating
/// indiscriminately would corrupt the semantic ones, and a fixed 48-bit prefix
/// is not collision-safe across thousands of units — so digests abbreviate to
/// the shortest prefix, at least [`DIGEST_PREFIX_MIN`], that is unique across
/// EVERY digest in the response. One length for the whole response, so the same
/// digest never renders two ways in one answer.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
struct DisplayIds {
    digest_len: usize,
}

impl DisplayIds {
    /// A digest is lowercase hex and long enough to be worth shortening.
    /// Anything else — `authority-history-v1`, `role.path.plan-handoff.v1` —
    /// is semantic and renders verbatim.
    fn is_digest(id: &str) -> bool {
        id.len() >= DIGEST_PREFIX_MIN
            && id
                .chars()
                .all(|c| c.is_ascii_digit() || matches!(c, 'a'..='f'))
    }

    fn for_ids<'a>(ids: impl IntoIterator<Item = &'a str>) -> Self {
        let digests: Vec<&str> = ids.into_iter().filter(|id| Self::is_digest(id)).collect();
        let longest = digests.iter().map(|id| id.len()).max().unwrap_or(0);
        let mut digest_len = DIGEST_PREFIX_MIN;
        while digest_len < longest {
            let mut prefixes: Vec<&str> = digests.iter().map(|id| &id[..digest_len]).collect();
            prefixes.sort_unstable();
            let before = prefixes.len();
            prefixes.dedup();
            if prefixes.len() == before {
                break;
            }
            digest_len += 1;
        }
        Self { digest_len }
    }

    fn render(&self, id: &str) -> String {
        if Self::is_digest(id) && id.len() > self.digest_len {
            id[..self.digest_len].to_string()
        } else {
            id.to_string()
        }
    }
}

pub(crate) fn validate_input(
    input: &SearchKnowledgeInput,
) -> Result<KnowledgeRetrieveRequest, String> {
    // Security must run before tokenization, path routing, proxying, analytics,
    // cache lookup, or CCR creation. The rejection never echoes the query.
    guard_query(&input.query)
        .map_err(|_| "Error: sensitive query rejected by repository safety policy.".to_string())?;
    if input
        .path_prefix
        .as_deref()
        .is_some_and(|path| guard_query(path).is_err())
    {
        return Err(
            "Error: sensitive path_prefix rejected by repository safety policy.".to_string(),
        );
    }

    let phrase = input.query.trim();
    if phrase.is_empty() {
        return Err("Error: query must be non-empty.".to_string());
    }
    if input.project.is_some() && input.projects.is_some() {
        return Err("Error: project and projects are mutually exclusive.".to_string());
    }
    if input.projects.as_ref().is_some_and(Vec::is_empty) {
        return Err("Error: projects must not be empty.".to_string());
    }
    if let Some(max_tokens) = input.max_tokens
        && max_tokens < MIN_PROVENANCE_TOKENS
    {
        return Err(format!(
            "Error: max_tokens is too small for provenance; minimum is {MIN_PROVENANCE_TOKENS}."
        ));
    }
    if let Some(projects) = input.projects.as_ref()
        && projects.iter().any(|project| project == "*")
        && projects.len() != 1
    {
        return Err("Error: projects wildcard must be the sole selector.".to_string());
    }
    let path_prefix = normalize_knowledge_path_prefix(input.path_prefix.as_deref()).map_err(|_| {
        "Error: invalid path_prefix; expected a normalized repository-relative path without traversal."
            .to_string()
    })?;
    let terms = significant_terms(phrase);
    let authority_scope = input
        .authority_scope
        .unwrap_or(KnowledgeAuthorityScope::Default);
    let limit = usize::try_from(input.limit.unwrap_or(DEFAULT_LIMIT as u32))
        .unwrap_or(MAX_LIMIT)
        .clamp(1, MAX_LIMIT);

    // Archived records are history: an explicit history/all scope asks for them.
    let include_archived = input.include_archived.unwrap_or(matches!(
        authority_scope,
        KnowledgeAuthorityScope::History | KnowledgeAuthorityScope::All
    ));

    Ok(KnowledgeRetrieveRequest {
        phrase: phrase.to_lowercase(),
        terms,
        path_prefix,
        authority_scope: retrieve_authority_scope(authority_scope),
        limit,
        include_archived,
    })
}

fn retrieve_authority_scope(scope: KnowledgeAuthorityScope) -> KnowledgeRetrieveAuthorityScope {
    match scope {
        KnowledgeAuthorityScope::Default => KnowledgeRetrieveAuthorityScope::Default,
        KnowledgeAuthorityScope::Current => KnowledgeRetrieveAuthorityScope::Current,
        KnowledgeAuthorityScope::Intent => KnowledgeRetrieveAuthorityScope::Intent,
        KnowledgeAuthorityScope::History => KnowledgeRetrieveAuthorityScope::History,
        KnowledgeAuthorityScope::All => KnowledgeRetrieveAuthorityScope::All,
    }
}

pub(crate) use crate::knowledge::source_scope::{select_scoped_sources, source_scope_label};

/// The real label for one source: `current` for the current lane, else
/// `worktree:<id>` / `ref:<name>`. Derived once and reused in the scope line,
/// the per-source list, and every hit, so a hit can never be separated from
/// its provenance (the hit line previously hardcoded `source=current`).
fn source_label(generation: &PublishedGeneration, is_current: bool) -> String {
    if is_current {
        return "current".to_string();
    }
    match generation.source.as_deref().map(|source| &source.location) {
        Some(SourceLocation::WorkingTree { worktree_id }) => format!("worktree:{worktree_id}"),
        Some(SourceLocation::GitRef { name }) => format!("ref:{name}"),
        None => "unbound".to_string(),
    }
}

/// Compose `search_knowledge` across the scope-selected sources of one captured
/// source set (Gate L L-G06). A multi-source scope that selects no sources
/// returns a typed empty readiness result rather than a false complete-absence
/// claim.
///
/// SIFT-WS0: every scope — including `current` — flows through the same
/// extract → rank globally → limit once pipeline. The previous implementation
/// rendered each source through `search_current` (which applies `limit` and
/// computes its own overflow/withheld/filtered counts) and concatenated the
/// resulting strings, so a two-source scope returned `2 x limit` hits and two
/// independent count sets.
pub(crate) fn search_scoped(
    source_set: &PublishedSourceSet,
    input: &SearchKnowledgeInput,
) -> String {
    let rendered = search_scoped_rendered(source_set, input);
    budget_summary(&rendered, input.max_tokens)
}

/// Pack the header plus as many COMPLETE hit blocks as fit.
///
/// Hits are multi-line, and the outer `apply_ccr_budget` cuts at a line
/// boundary. This formatter therefore emits only whole blocks and stops
/// before the next block would exceed the tool's byte budget, so the outer
/// cut never lands inside a record. A fitting payload does not get a CCR
/// footer, so this packs to the full budget rather than reserving footer
/// space.
pub(crate) fn budget_summary(rendered: &str, max_tokens: Option<u64>) -> String {
    let Some(tokens) = crate::index_lifecycle::guidance::compression::resolve_tool_max_tokens(
        "search_knowledge",
        max_tokens,
    )
    .filter(|tokens| *tokens > 0) else {
        return rendered.to_string();
    };
    let max_bytes = (tokens as usize).saturating_mul(4);
    if rendered.len() <= max_bytes {
        return rendered.to_string();
    }
    let budget = max_bytes;

    // Everything before the first block is the envelope: it carries the
    // MUST-include provenance and is never split.
    let is_block_start = |line: &str| {
        line.split_once(". ")
            .is_some_and(|(ordinal, _)| ordinal.parse::<usize>().is_ok())
    };
    let lines: Vec<&str> = rendered.lines().collect();
    let first_block = lines
        .iter()
        .position(|line| is_block_start(line))
        .unwrap_or(lines.len());
    let header = lines[..first_block].join("\n");
    if header.len() >= budget {
        // Provenance alone exceeds the budget. Emit it anyway: there are no
        // hit blocks yet, so this cannot split a record. The outer budget may
        // then cut at an envelope line boundary.
        return header;
    }

    let mut out = header;
    let mut block: Vec<&str> = Vec::new();
    let flush = |block: &mut Vec<&str>, out: &mut String| -> bool {
        if block.is_empty() {
            return true;
        }
        let candidate = format!("\n{}", block.join("\n"));
        block.clear();
        if out.len() + candidate.len() <= budget {
            out.push_str(&candidate);
            true
        } else {
            false
        }
    };
    for line in &lines[first_block..] {
        if is_block_start(line) && !flush(&mut block, &mut out) {
            return out;
        }
        block.push(line);
    }
    flush(&mut block, &mut out);
    out
}

pub(crate) struct KnowledgeSearchProjection {
    pub rendered: String,
    #[cfg_attr(not(feature = "embed"), allow(dead_code))] // read only by embed_* consumers
    pub retrieval: Option<KnowledgeRetrieveResult>,
    #[cfg_attr(not(feature = "embed"), allow(dead_code))] // read only by embed_* consumers
    pub scope: KnowledgeSourceScope,
}

pub(crate) fn search_scoped_projection(
    source_set: &PublishedSourceSet,
    input: &SearchKnowledgeInput,
) -> Result<KnowledgeSearchProjection, String> {
    let request = validate_input(input)?;
    let scope = input.source_scope.unwrap_or(KnowledgeSourceScope::Current);
    let selected = select_scoped_sources(source_set, scope);
    if selected.is_empty() {
        return Ok(KnowledgeSearchProjection {
            rendered: format!(
                "Readiness: no_sources_in_scope; source_scope '{}' selected no sources.",
                source_scope_label(scope)
            ),
            retrieval: None,
            scope,
        });
    }

    let current_id = &source_set.current_source_id;
    let labels: Vec<String> = selected
        .iter()
        .map(|generation| {
            let is_current = generation
                .source
                .as_deref()
                .is_some_and(|source| &source.source_id == current_id);
            source_label(generation, is_current)
        })
        .collect();
    let lanes: Vec<KnowledgeRetrieveLane<'_>> = selected
        .iter()
        .zip(labels.iter())
        .map(|(generation, label)| KnowledgeRetrieveLane { generation, label })
        .collect();
    let retrieved = retrieve_knowledge(&lanes, &request);
    let rendered = render_response(&retrieved, input, scope);
    Ok(KnowledgeSearchProjection {
        rendered,
        retrieval: Some(retrieved),
        scope,
    })
}

fn search_scoped_rendered(source_set: &PublishedSourceSet, input: &SearchKnowledgeInput) -> String {
    match search_scoped_projection(source_set, input) {
        Ok(projection) => projection.rendered,
        Err(error) => error,
    }
}

/// Single-source convenience over the same pipeline (SIFT-WS0: there is no
/// second composition path that could drift). Production callers reach this
/// through `search_scoped`; it is retained for the single-source unit tests
/// that exercise readiness and withheld-provenance behavior directly.
#[cfg(all(test, feature = "server"))]
pub(crate) fn search_current(
    generation: &PublishedGeneration,
    input: &SearchKnowledgeInput,
) -> String {
    let request = match validate_input(input) {
        Ok(request) => request,
        Err(error) => return error,
    };
    let retrieved = retrieve_knowledge(
        &[KnowledgeRetrieveLane {
            generation,
            label: "current",
        }],
        &request,
    );
    render_response(&retrieved, input, KnowledgeSourceScope::Current)
}

/// One per-source identity line. A source whose envelope was withheld or that
/// could not be read still gets a line — its absence must be visible, never
/// silent — but never echoes guarded identity.
fn render_source_line(
    source: &KnowledgeRetrieveSource,
    prefix: &str,
    overall_coverage: &str,
    ids: &DisplayIds,
) -> String {
    let Some(envelope) = source.envelope.as_ref() else {
        let state = lane_readiness_text(source.readiness);
        return format!(
            "{prefix}: source=withheld source_id=withheld source_version=withheld \
             publication={} content={} freshness=withheld coverage={overall_coverage} \
             manifest_digest=withheld readiness={state}",
            source.publication_generation, source.content_generation,
        );
    };
    let source_version = format!(
        "branch={};commit={};working_tree={}",
        envelope
            .source_version
            .branch
            .as_deref()
            .unwrap_or("not_applicable"),
        envelope
            .source_version
            .commit
            .as_deref()
            .unwrap_or("not_applicable"),
        snake_debug(envelope.source_version.working_tree)
    );
    // SIFT-WS1: bounded IDs here too. This line carried two FULL 64-hex digests
    // (~300 chars) and was the single largest item in the envelope, crowding
    // hits out of small budgets entirely. The frozen contract's own example
    // renders `hash=<bounded-id>`; full values stay resolvable through
    // `review_knowledge`.
    format!(
        "{prefix}: source={} source_id={} source_version={source_version} publication={} \
         content={} freshness={} coverage={} manifest_digest={}",
        source.label,
        ids.render(envelope.source.source_id.as_str()),
        envelope.publication_generation,
        envelope.content_generation,
        freshness_label(&envelope.freshness),
        coverage_label(envelope.coverage),
        ids.render(&envelope.manifest_digest),
    )
}

fn lane_readiness_text(readiness: Option<KnowledgeLaneReadiness>) -> &'static str {
    match readiness {
        Some(KnowledgeLaneReadiness::IndexScoutingOrVerifying) => {
            "index_scouting_or_verifying; retry after the current publication completes"
        }
        Some(KnowledgeLaneReadiness::KnowledgeBridgeLoading) => {
            "knowledge_bridge_loading; retry after the knowledge bridge publication completes"
        }
        Some(KnowledgeLaneReadiness::NoValidSource) => {
            "no_valid_source; run index_folder to rebuild from repository source"
        }
        Some(KnowledgeLaneReadiness::EnvelopeUnavailable) => {
            "no_valid_source; source envelope is unavailable and no evidence was served"
        }
        Some(KnowledgeLaneReadiness::EvidenceWithheld) => "evidence_withheld",
        None => "unavailable",
    }
}

fn render_response(
    retrieved: &KnowledgeRetrieveResult,
    input: &SearchKnowledgeInput,
    scope: KnowledgeSourceScope,
) -> String {
    let sources = &retrieved.sources;
    let hits = &retrieved.hits;
    let mut all_ids: Vec<&str> = Vec::new();
    for source in sources {
        if let Some(envelope) = source.envelope.as_ref() {
            all_ids.push(envelope.source.source_id.as_str());
            all_ids.push(envelope.manifest_digest.as_str());
        }
    }
    for hit in hits {
        all_ids.push(hit.content_hash.as_str());
        all_ids.extend(hit.authority.finding_ids.iter().map(String::as_str));
        all_ids.extend(hit.authority.provenance_ids.iter().map(String::as_str));
        all_ids.extend(
            hit.relationship_evidence
                .iter()
                .map(|evidence| evidence.link_id.as_str()),
        );
    }
    let ids = &DisplayIds::for_ids(all_ids);

    let overall_coverage = if retrieved.degraded {
        "degraded"
    } else {
        "complete"
    };
    let path_scope = input.path_prefix.as_deref().unwrap_or("repository");
    let scope_label = source_scope_label(scope);
    let multi = sources.len() > 1;
    let primary = sources.first();

    let (publication, content) = primary
        .map(|source| (source.publication_generation, source.content_generation))
        .unwrap_or((0, 0));
    let derived = primary
        .map(|source| source.derived.clone())
        .unwrap_or_default();
    let trust_source = if multi {
        scope_label.to_string()
    } else {
        primary
            .map(|source| {
                if source.envelope.is_some() {
                    source.label.clone()
                } else {
                    "withheld".to_string()
                }
            })
            .unwrap_or_else(|| "unbound".to_string())
    };

    let mut output = String::new();
    if multi || !matches!(scope, KnowledgeSourceScope::Current) {
        output.push_str(&format!("Source scope searched: {scope_label}\n"));
    }
    output.push_str(&format!(
        "Trust: exact repository knowledge evidence | publication={publication} | \
         content={content} | source={trust_source} | coverage={overall_coverage}\n\
         Secret policy: version {}\n\
         Scope: {} + {path_scope}\n",
        derived.secret_policy_version,
        if multi { scope_label } else { "current" },
    ));
    for (index, source) in sources.iter().enumerate() {
        let prefix = if multi {
            format!("Source[{}]", index + 1)
        } else {
            "Source".to_string()
        };
        output.push_str(&render_source_line(source, &prefix, overall_coverage, ids));
        output.push('\n');
    }
    output.push_str(&format!(
        "Derived: authority_rule_version={} policy_version={} secret_policy_version={} \
         bridge_coverage={} authority_coverage={} overall_coverage={overall_coverage}\n\
         Counts: overflow={} withheld_sensitive={} \
         filtered_current={} filtered_intent={} filtered_history_only={} \
         filtered_suppressed={} filtered_review_required={} filtered_unknown={}",
        derived.authority_rule_version,
        derived.policy_version,
        derived.secret_policy_version,
        derived_coverage_label(&derived.bridge_coverage),
        derived_coverage_label(&derived.authority_coverage),
        retrieved.overflow,
        retrieved.withheld_count,
        retrieved.filtered.current,
        retrieved.filtered.intent,
        retrieved.filtered.history_only,
        retrieved.filtered.suppressed,
        retrieved.filtered.review_required,
        retrieved.filtered.unknown,
    ));
    // SF-ARC-07: a measured count of matching units the default scope left out.
    if retrieved.filtered.archived > 0 {
        output.push_str(&format!(
            "\nExcluded by default scope: {} matching unit(s) in archive or dated research/review folders; retry with include_archived=true to search them.",
            retrieved.filtered.archived
        ));
    }

    if let Some(no_match) = retrieved.absence {
        output.push_str(&format!("\nNo match: {}", no_match.as_str()));
        return output;
    }

    for (index, hit) in hits.iter().enumerate() {
        output.push_str(&render_hit_block(index + 1, hit, ids));
    }
    output
}

/// One indivisible, answer-first hit block (SIFT-WS1, T027).
///
/// Ordered so the answer arrives before the provenance chrome: location,
/// heading breadcrumb, excerpt, then evidence. The previous single
/// pipe-delimited mega-line cost ~250 tokens per hit and buried the excerpt
/// mid-line, so reading hit 3 of 10 meant parsing thousands of tokens of
/// provenance first.
///
/// Field NAMES are unchanged from the mega-line (`content_hash=`, `authority:`,
/// `finding_ids=`, `bridge_previews=`, ...). Only the layout changed: agents
/// and tests already parse these tokens, and renaming them would be churn on a
/// frozen surface for no readability gain.
///
/// Budgeting treats this whole block as atomic -- see `budget_summary`.
fn render_hit_block(ordinal: usize, hit: &KnowledgeRetrieveHit, ids: &DisplayIds) -> String {
    let heading = if hit.heading_path.is_empty() {
        "(no heading)".to_string()
    } else {
        hit.heading_path.join(" > ")
    };
    let render_list = |values: &[String]| -> String {
        values
            .iter()
            .map(|id| ids.render(id))
            .collect::<Vec<_>>()
            .join(",")
    };
    format!(
        "\n{ordinal}. {} · {}:{}\n   {heading}\n   \"{}\"\n   source={} content_hash={} publication={} content={} line_range={}..{}\n   authority: lifecycle={} domain={} code={} voice={} coverage={}\n   finding_ids=[{}] omitted={} provenance_ids=[{}] omitted={}\n   bridge_previews=[{}] omitted={}",
        hit.source_label,
        hit.path,
        hit.line,
        hit.preview,
        hit.source_label,
        ids.render(&hit.content_hash),
        hit.publication_generation,
        hit.content_generation,
        hit.line_range.start,
        hit.line_range.end,
        snake_debug(hit.authority.lifecycle),
        snake_debug(hit.authority.authority_domain),
        snake_debug(hit.authority.code_evidence),
        snake_debug(hit.authority.voice),
        derived_coverage_label(&hit.authority.coverage),
        render_list(&hit.authority.finding_ids),
        hit.authority.finding_ids_omitted,
        render_list(&hit.authority.provenance_ids),
        hit.authority.provenance_ids_omitted,
        hit.relationship_evidence
            .iter()
            .map(|evidence| abbreviate_preview_ids(&evidence.preview_token(), ids))
            .collect::<Vec<_>>()
            .join(" | "),
        hit.relationship_evidence_omitted,
    )
}

/// Bridge previews are `<link-id>:<kind>:<resolution>` triples whose leading
/// link ID is a digest. Abbreviate only that ID; the kind and resolution are
/// semantic and stay verbatim.
fn abbreviate_preview_ids(preview: &str, ids: &DisplayIds) -> String {
    match preview.split_once(':') {
        Some((id, rest)) => format!("{}:{rest}", ids.render(id)),
        None => preview.to_string(),
    }
}

fn coverage_label(coverage: CoverageStatus) -> &'static str {
    match coverage {
        CoverageStatus::Complete => "complete",
        CoverageStatus::Degraded => "degraded",
    }
}

fn freshness_label(freshness: &FreshnessStatus) -> &'static str {
    match freshness {
        FreshnessStatus::Current => "current",
        FreshnessStatus::Verifying => "verifying",
        FreshnessStatus::Degraded { .. } => "degraded_last_valid",
    }
}

fn derived_coverage_label(coverage: &DerivedCoverage) -> &'static str {
    match coverage {
        DerivedCoverage::Complete => "complete",
        DerivedCoverage::Truncated { .. } => "truncated",
        DerivedCoverage::Loading => "loading",
    }
}

fn snake_debug(value: impl std::fmt::Debug) -> String {
    let debug = format!("{value:?}");
    let mut output = String::with_capacity(debug.len() + 4);
    let mut previous_lowercase = false;
    for character in debug.chars() {
        if character.is_ascii_uppercase() {
            if previous_lowercase {
                output.push('_');
            }
            output.push(character.to_ascii_lowercase());
            previous_lowercase = false;
        } else {
            previous_lowercase = character.is_ascii_lowercase() || character.is_ascii_digit();
            output.push(character);
        }
    }
    output
}

#[cfg(all(test, feature = "server"))]
mod tests {
    use std::sync::Arc;

    use super::*;
    use crate::live_index::knowledge_retrieve::{EXCERPT_MAX_CHARS, window_excerpt};
    use crate::live_index::{LiveIndex, PublishedIndexStatus};

    fn input(query: &str) -> SearchKnowledgeInput {
        SearchKnowledgeInput {
            query: query.to_string(),
            path_prefix: None,
            source_scope: Some(KnowledgeSourceScope::Current),
            authority_scope: Some(KnowledgeAuthorityScope::Default),
            project: None,
            projects: None,
            limit: Some(10),
            max_tokens: None,
            include_archived: None,
        }
    }

    fn bracketed_field(output: &str, field: &str) -> String {
        let marker = format!("{field}=[");
        let start = output.find(&marker).expect("field marker") + marker.len();
        let end = output[start..].find(']').expect("field terminator") + start;
        output[start..end].to_string()
    }

    // ── SIFT-WS0 multi-source composition fixtures ──────────────────────────

    /// Count rendered hit blocks. A hit begins at the start of a line with
    /// `<n>. `, which holds for both the pre-slice mega-line format and the
    /// post-slice block format, so the same helper measures before and after.
    fn hit_count(output: &str) -> usize {
        output
            .lines()
            .filter(|line| {
                line.split_once(". ")
                    .is_some_and(|(ordinal, _)| ordinal.parse::<usize>().is_ok())
            })
            .count()
    }

    fn occurrences(output: &str, needle: &str) -> usize {
        output.matches(needle).count()
    }

    /// Build a project whose docs all match `term`, one unit per file.
    fn corpus(files: &[(&str, &str)]) -> (tempfile::TempDir, Arc<PublishedGeneration>) {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::create_dir_all(dir.path().join("docs")).expect("docs dir");
        for (name, body) in files {
            std::fs::write(dir.path().join("docs").join(name), body).expect("doc fixture");
        }
        let index = LiveIndex::load(dir.path()).expect("load corpus");
        let generation = index.published_source_set().current_generation();
        (dir, generation)
    }

    /// Relabel a captured generation as a distinct non-current source so one
    /// `PublishedSourceSet` can carry several. Only the source identity is
    /// swapped; all captured evidence is shared by `Arc`, so this cannot
    /// fabricate content.
    fn relabel(generation: &PublishedGeneration, location: SourceLocation) -> PublishedGeneration {
        let source_id = crate::domain::SourceId::new(match &location {
            SourceLocation::WorkingTree { worktree_id } => format!("src-{worktree_id}"),
            SourceLocation::GitRef { name } => format!("src-{name}"),
        });
        let identity = crate::domain::SourceIdentity {
            repository_id: generation
                .source
                .as_deref()
                .map(|source| source.repository_id.clone())
                .expect("fixture generation must be bound"),
            source_id,
            location,
        };
        PublishedGeneration {
            source: Some(Arc::new(identity)),
            publication_generation: generation.publication_generation,
            content_generation: generation.content_generation,
            project_generation: generation.project_generation,
            source_version: generation.source_version.clone(),
            freshness: Arc::clone(&generation.freshness),
            manifest: generation.manifest.clone(),
            code_signals: Arc::clone(&generation.code_signals),
            bridge: Arc::clone(&generation.bridge),
            authority: Arc::clone(&generation.authority),
            live: Arc::clone(&generation.live),
            health: Arc::clone(&generation.health),
            outline: Arc::clone(&generation.outline),
            memory_counts: std::sync::OnceLock::new(),
        }
    }

    fn source_set(
        current: &Arc<PublishedGeneration>,
        others: Vec<PublishedGeneration>,
    ) -> PublishedSourceSet {
        let current_id = current
            .source
            .as_deref()
            .expect("current must be bound")
            .source_id
            .clone();
        let mut sources: std::collections::BTreeMap<_, _> = std::collections::BTreeMap::new();
        sources.insert(current_id.clone(), Arc::clone(current));
        for other in others {
            let id = other
                .source
                .as_deref()
                .expect("other must be bound")
                .source_id
                .clone();
            sources.insert(id, Arc::new(other));
        }
        PublishedSourceSet {
            registry_generation: 1,
            current_source_id: current_id,
            sources,
        }
    }

    fn scoped_input(query: &str, limit: u32) -> SearchKnowledgeInput {
        SearchKnowledgeInput {
            source_scope: Some(KnowledgeSourceScope::All),
            limit: Some(limit),
            ..input(query)
        }
    }

    fn rendered_hit_paths(output: &str) -> Vec<String> {
        output
            .lines()
            .filter_map(|line| {
                let rest = line.split_once(". ")?;
                rest.0.parse::<usize>().ok()?;
                let path_and_line = rest.1.split_once(" · ")?.1;
                Some(path_and_line.split_once(':')?.0.to_string())
            })
            .collect()
    }

    /// Same fixture and query: typed seam hit order equals the server tool.
    #[test]
    fn typed_knowledge_seam_matches_server_search_knowledge_hits() {
        let (_dir, generation) = corpus(&[
            (
                "alpha.md",
                "# Alpha\nalpha appears here once without the rest of the phrase.\n",
            ),
            (
                "beta.md",
                "# alpha heading match\nalpha beta terms but not the full phrase.\n",
            ),
            (
                "gamma.md",
                "# Gamma\nalpha beta gamma exact phrase lives here.\n",
            ),
        ]);
        let query = "alpha beta gamma";
        let request = KnowledgeRetrieveRequest::parse(
            query,
            None,
            KnowledgeRetrieveAuthorityScope::Default,
            10,
        )
        .expect("parse request");
        let retrieved = retrieve_knowledge(
            &[KnowledgeRetrieveLane {
                generation: generation.as_ref(),
                label: "current",
            }],
            &request,
        );
        let seam_paths: Vec<&str> = retrieved.hits.iter().map(|hit| hit.path.as_str()).collect();
        let rendered = search_current(generation.as_ref(), &input(query));
        let server_paths = rendered_hit_paths(&rendered);
        assert_eq!(
            seam_paths,
            server_paths.iter().map(String::as_str).collect::<Vec<_>>(),
            "typed seam must preserve server hit order:\n{rendered}"
        );
        assert_eq!(
            seam_paths,
            ["docs/gamma.md", "docs/beta.md", "docs/alpha.md"],
            "frozen rank tuple: exact phrase, then heading-plus-terms, then path"
        );
    }

    // ── SIFT-WS1 excerpt windowing ──────────────────────────────────────────

    /// SIFT-WS1 (T020). Excerpts were the raw matched line with no cap:
    /// dogfood captured a 1.5 KB Markdown table row as one hit's excerpt.
    #[test]
    fn excerpt_is_bounded_and_keeps_the_match_in_window() {
        let row = format!(
            "| {} | persistence boundary | {} |",
            "filler ".repeat(120),
            "tail ".repeat(120)
        );
        let windowed = window_excerpt(&row, "persistence boundary", &[]);
        let chars = windowed.chars().count();
        assert!(
            chars <= EXCERPT_MAX_CHARS + 8,
            "excerpt must be bounded, got {chars} chars"
        );
        assert!(
            windowed.contains("persistence boundary"),
            "the match must survive the window: {windowed}"
        );
        assert!(
            windowed.len() < row.len(),
            "a long row must actually shrink"
        );
    }

    /// SIFT-WS1 (T020). Cuts land on CHARACTER boundaries. Byte offsets taken
    /// from a lowercased copy are the landmine here: `to_lowercase()` is not
    /// length-preserving and a byte index can split a multi-byte character.
    #[test]
    fn excerpt_cuts_on_character_boundaries_for_cjk_emoji_and_combining_marks() {
        for filler in ["日本語テキスト", "🙂🚀🎉", "éécombining", "İIıi"] {
            let line = format!(
                "{} persistence boundary {}",
                filler.repeat(60),
                filler.repeat(60)
            );
            // Must not panic, and must remain valid UTF-8 by construction.
            let windowed = window_excerpt(&line, "persistence boundary", &[]);
            assert!(
                windowed.chars().count() <= EXCERPT_MAX_CHARS + 8,
                "bounded for {filler}: {windowed}"
            );
            assert!(
                windowed.contains("persistence boundary"),
                "match kept for {filler}: {windowed}"
            );
        }
    }

    /// SIFT-WS1 (T020). A short line is returned untouched — no ellipsis, no
    /// churn on the common case.
    #[test]
    fn excerpt_leaves_short_lines_untouched() {
        let line = "Shutdown is not a safe persistence boundary.";
        assert_eq!(window_excerpt(line, "persistence boundary", &[]), line);
    }

    /// SIFT-WS1 (T020). A window that had to cut snaps to whitespace rather
    /// than slicing a word in half.
    #[test]
    fn excerpt_snaps_to_whitespace_when_it_cuts() {
        let line = format!(
            "{}persistence boundary{}",
            "alpha ".repeat(80),
            " omega".repeat(80)
        );
        let windowed = window_excerpt(&line, "persistence boundary", &[]);
        let trimmed = windowed.trim_start_matches('…').trim_end_matches('…');
        assert!(
            !trimmed.starts_with("lpha") && !trimmed.starts_with("pha"),
            "leading cut must snap to a word boundary: {windowed}"
        );
    }

    // ── SIFT-WS1 type-aware ID abbreviation ─────────────────────────────────

    /// SIFT-WS1 (T021). Provenance vectors mix 64-hex digests with SEMANTIC
    /// rule IDs (`authority-history-v1`, `temporal-coverage-incomplete`).
    /// Abbreviating indiscriminately would corrupt the semantic ones.
    #[test]
    fn semantic_ids_render_verbatim_and_only_digests_abbreviate() {
        let digest_a = "a".repeat(64);
        let digest_b = "b".repeat(64);
        let ids = [
            digest_a.clone(),
            digest_b.clone(),
            "authority-history-v1".to_string(),
            "temporal-coverage-incomplete".to_string(),
            "role.path.plan-handoff.v1".to_string(),
        ];
        let display = DisplayIds::for_ids(ids.iter().map(String::as_str));

        assert_eq!(
            display.render("authority-history-v1"),
            "authority-history-v1"
        );
        assert_eq!(
            display.render("temporal-coverage-incomplete"),
            "temporal-coverage-incomplete"
        );
        assert_eq!(
            display.render("role.path.plan-handoff.v1"),
            "role.path.plan-handoff.v1"
        );
        assert_eq!(display.render(&digest_a), "a".repeat(12));
        assert_eq!(display.render(&digest_b), "b".repeat(12));
    }

    /// SIFT-WS1 (T021). A forced 12-hex collision must extend until unique --
    /// a fixed 48-bit prefix is not collision-safe across thousands of units.
    #[test]
    fn forced_digest_prefix_collision_extends_until_unique() {
        let shared = "c".repeat(12);
        let left = format!("{shared}0{}", "d".repeat(51));
        let right = format!("{shared}1{}", "e".repeat(51));
        let display = DisplayIds::for_ids([left.as_str(), right.as_str()]);

        let rendered_left = display.render(&left);
        let rendered_right = display.render(&right);
        assert_ne!(
            rendered_left, rendered_right,
            "colliding digests must not render identically"
        );
        assert!(
            rendered_left.len() > 12,
            "prefix must EXTEND past 12 on collision, got {rendered_left}"
        );
        assert_eq!(
            rendered_left.len(),
            rendered_right.len(),
            "one length for the whole response, so a digest never renders two ways"
        );
        assert!(left.starts_with(&rendered_left));
        assert!(right.starts_with(&rendered_right));
    }

    /// SIFT-WS0 (T007). The frozen contract ranks and limits across the
    /// selected sources; `search_scoped` instead rendered each source through
    /// `search_current` -- which applies `limit` and computes its own
    /// overflow/withheld/filtered counts -- and concatenated the strings. With
    /// two sources and `limit=10` that returns up to 20 hits and two
    /// independent count sets.
    #[test]
    fn global_limit_and_counts_apply_once_across_sources() {
        let body = |n: usize| format!("# Doc {n}\nalpha beta persistence boundary evidence {n}.\n");
        let files: Vec<(String, String)> = (0..8).map(|n| (format!("d{n}.md"), body(n))).collect();
        let refs: Vec<(&str, &str)> = files
            .iter()
            .map(|(name, body)| (name.as_str(), body.as_str()))
            .collect();

        let (_a, current) = corpus(&refs);
        let (_b, other) = corpus(&refs);
        let other = relabel(
            &other,
            SourceLocation::WorkingTree {
                worktree_id: "wt1".to_string(),
            },
        );
        let set = source_set(&current, vec![other]);

        let output = search_scoped(&set, &scoped_input("alpha beta persistence boundary", 10));

        assert_eq!(
            hit_count(&output),
            10,
            "limit must apply ONCE globally, not per source:\n{output}"
        );
        assert_eq!(
            occurrences(&output, "overflow="),
            1,
            "counts must be a single aggregate, not one set per source:\n{output}"
        );
        assert_eq!(
            occurrences(&output, "withheld_sensitive="),
            1,
            "withheld count must be a single aggregate:\n{output}"
        );
    }

    /// SIFT-WS0 (T008). Ranking is global: an exact-phrase hit in a
    /// lower-precedence source must outrank a term-only hit in the current
    /// source. Per-source concatenation puts every current-source hit first
    /// regardless of match quality.
    #[test]
    fn global_ranking_interleaves_sources_by_frozen_tuple() {
        let (_a, current) = corpus(&[("weak.md", "# Weak\nalpha appears here alone.\n")]);
        let (_b, other) = corpus(&[(
            "strong.md",
            "# Strong\nalpha beta gamma exact phrase lives here.\n",
        )]);
        let other = relabel(
            &other,
            SourceLocation::WorkingTree {
                worktree_id: "wt1".to_string(),
            },
        );
        let set = source_set(&current, vec![other]);

        let output = search_scoped(&set, &scoped_input("alpha beta gamma", 10));
        let strong = output.find("strong.md").expect("strong hit present");
        let weak = output.find("weak.md").expect("weak hit present");
        assert!(
            strong < weak,
            "exact-phrase hit in a non-current source must outrank a term-only current hit:\n{output}"
        );
    }

    /// SIFT-WS0 (T009). Each hit carries its own source label. The hit line
    /// previously hardcoded `source=current` for every source in scope.
    #[test]
    fn hits_carry_real_source_labels_not_hardcoded_current() {
        let (_a, current) = corpus(&[("c.md", "# C\nalpha persistence boundary in current.\n")]);
        let (_b, other) = corpus(&[("w.md", "# W\nalpha persistence boundary in worktree.\n")]);
        let other = relabel(
            &other,
            SourceLocation::WorkingTree {
                worktree_id: "wt1".to_string(),
            },
        );
        let (_c, refsrc) = corpus(&[("r.md", "# R\nalpha persistence boundary in ref.\n")]);
        let refsrc = relabel(
            &refsrc,
            SourceLocation::GitRef {
                name: "refs/heads/other".to_string(),
            },
        );
        let set = source_set(&current, vec![other, refsrc]);

        let output = search_scoped(&set, &scoped_input("alpha persistence boundary", 10));

        // Assert on the HIT LINE, not on any section banner: the banner already
        // carried the real label, while the hit itself hardcoded
        // `source=current` for every source in scope.
        let hit_line = |needle: &str| -> String {
            output
                .lines()
                .find(|line| {
                    line.contains(needle)
                        && line
                            .split_once(". ")
                            .is_some_and(|(ordinal, _)| ordinal.parse::<usize>().is_ok())
                })
                .unwrap_or_else(|| panic!("no hit line for {needle}:\n{output}"))
                .to_string()
        };

        let worktree_hit = hit_line("w.md");
        assert!(
            worktree_hit.contains("worktree:wt1"),
            "worktree hit must carry its real label, not `source=current`:\n{worktree_hit}"
        );
        let ref_hit = hit_line("r.md");
        assert!(
            ref_hit.contains("ref:refs/heads/other"),
            "ref hit must carry its real label, not `source=current`:\n{ref_hit}"
        );
    }

    fn with_readiness(
        generation: &PublishedGeneration,
        status: PublishedIndexStatus,
        freshness: FreshnessStatus,
        keep_manifest: bool,
    ) -> PublishedGeneration {
        let mut health = generation.health.as_ref().clone();
        health.status = status;
        PublishedGeneration {
            publication_generation: generation.publication_generation,
            content_generation: generation.content_generation,
            project_generation: generation.project_generation,
            source: generation.source.clone(),
            source_version: generation.source_version.clone(),
            freshness: Arc::new(freshness),
            manifest: keep_manifest.then(|| generation.manifest.clone()).flatten(),
            code_signals: Arc::clone(&generation.code_signals),
            bridge: Arc::clone(&generation.bridge),
            authority: Arc::clone(&generation.authority),
            live: Arc::clone(&generation.live),
            health: Arc::new(health),
            outline: Arc::clone(&generation.outline),
            memory_counts: std::sync::OnceLock::new(),
        }
    }

    #[test]
    fn captured_generation_stays_coherent_across_later_publication() {
        let project = tempfile::tempdir().expect("project");
        std::fs::create_dir_all(project.path().join("docs")).expect("docs");
        let document = project.path().join("docs").join("recovery.md");
        std::fs::write(
            &document,
            "# Recovery\nOld persistence boundary remains visible.\n",
        )
        .expect("old document");

        let shared = LiveIndex::load(project.path()).expect("load old generation");
        let captured = shared.published_source_set().current_generation();

        std::fs::write(
            &document,
            "# Recovery\nNew persistence boundary is now visible.\n",
        )
        .expect("new document");
        shared
            .reload(project.path())
            .expect("publish new generation");
        let next = shared.published_source_set().current_generation();
        assert_ne!(
            captured.publication_generation, next.publication_generation,
            "fixture must publish a distinct generation"
        );

        let old_output = search_current(&captured, &input("persistence boundary"));
        assert!(
            old_output.contains("Old persistence boundary"),
            "old: {old_output}"
        );
        assert!(
            !old_output.contains("New persistence boundary"),
            "captured response mixed a later publication: {old_output}"
        );

        let new_output = search_current(&next, &input("persistence boundary"));
        assert!(
            new_output.contains("New persistence boundary"),
            "new: {new_output}"
        );
        assert!(
            !new_output.contains("Old persistence boundary"),
            "next call served the prior publication: {new_output}"
        );
    }

    #[test]
    fn stable_finding_and_provenance_ids_survive_derived_only_republication() {
        let project = tempfile::tempdir().expect("project");
        std::fs::create_dir_all(project.path().join("docs")).expect("docs");
        std::fs::write(
            project.path().join("docs").join("decision.md"),
            "# Decision\nCurrent boundary evidence.\n",
        )
        .expect("document");
        std::fs::write(
            project.path().join(".symforge-knowledge.toml"),
            "version = 1\n[[entries]]\nentry_id = \"stale-entry\"\nlifecycle = \"archived\"\njustification_code = \"stale\"\n[entries.target]\npath = \"docs/decision.md\"\ncontent_hash = \"stale-hash\"\n",
        )
        .expect("policy");

        let shared = LiveIndex::load(project.path()).expect("load generation");
        let before = shared.published_source_set().current_generation();
        let before_output = search_current(&before, &input("boundary evidence"));
        let finding_ids = bracketed_field(&before_output, "finding_ids");
        let provenance_ids = bracketed_field(&before_output, "provenance_ids");
        assert!(
            !finding_ids.is_empty(),
            "fixture must emit a finding: {before_output}"
        );
        assert!(
            !provenance_ids.is_empty(),
            "fixture must emit provenance: {before_output}"
        );

        let prepared = shared.prepare_authority_rebuild();
        assert!(
            shared.publish_prepared_authority(prepared),
            "derived-only publication must be accepted"
        );
        let after = shared.published_source_set().current_generation();
        assert_eq!(before.content_generation, after.content_generation);
        assert_ne!(before.publication_generation, after.publication_generation);

        let after_output = search_current(&after, &input("boundary evidence"));
        assert_eq!(finding_ids, bracketed_field(&after_output, "finding_ids"));
        assert_eq!(
            provenance_ids,
            bracketed_field(&after_output, "provenance_ids")
        );
    }

    #[test]
    fn readiness_and_degraded_last_valid_never_claim_complete_absence() {
        let project = tempfile::tempdir().expect("project");
        std::fs::create_dir_all(project.path().join("docs")).expect("docs");
        std::fs::write(
            project.path().join("docs").join("recovery.md"),
            "# Recovery\nRetained evidence remains available.\n",
        )
        .expect("document");
        let shared = LiveIndex::load(project.path()).expect("load generation");
        let current = shared.published_source_set().current_generation();

        let loading = with_readiness(
            &current,
            PublishedIndexStatus::Loading,
            FreshnessStatus::Verifying,
            true,
        );
        let loading_output = search_current(&loading, &input("missing evidence"));
        assert!(
            loading_output.contains("index_scouting_or_verifying")
                && !loading_output.contains("no_evidence_complete"),
            "loading readiness must not masquerade as complete absence: {loading_output}"
        );

        let no_valid = with_readiness(
            &current,
            PublishedIndexStatus::Empty,
            FreshnessStatus::Verifying,
            false,
        );
        let no_valid_output = search_current(&no_valid, &input("missing evidence"));
        assert!(
            no_valid_output.contains("no_valid_source")
                && !no_valid_output.contains("no_evidence_complete"),
            "an absent valid snapshot must return recovery guidance: {no_valid_output}"
        );

        let verifying = with_readiness(
            &current,
            PublishedIndexStatus::Ready,
            FreshnessStatus::Verifying,
            true,
        );
        let degraded_miss = search_current(&verifying, &input("orbital zebra lattice"));
        assert!(
            degraded_miss.contains("freshness=verifying")
                && degraded_miss.contains("overall_coverage=degraded")
                && degraded_miss.contains("no_evidence_degraded")
                && !degraded_miss.contains("no_evidence_complete"),
            "verifying last-valid no-match must remain explicitly degraded: {degraded_miss}"
        );
        let degraded_hit = search_current(&verifying, &input("retained evidence"));
        assert!(
            degraded_hit.contains("docs/recovery.md:2")
                && degraded_hit.contains("freshness=verifying")
                && degraded_hit.contains("overall_coverage=degraded"),
            "last-valid evidence may be served only with degraded provenance: {degraded_hit}"
        );
    }

    #[test]
    fn loading_knowledge_bridge_does_not_claim_a_complete_miss() {
        let project = tempfile::tempdir().expect("project");
        std::fs::create_dir_all(project.path().join("docs")).expect("docs");
        std::fs::write(
            project.path().join("docs").join("recovery.md"),
            "# Recovery\nRetained evidence remains available.\n",
        )
        .expect("document");
        let shared = LiveIndex::load(project.path()).expect("load generation");
        shared.install_loading_knowledge_for_test();
        let loading = shared.published_source_set().current_generation();
        assert!(matches!(
            loading.bridge.coverage,
            crate::live_index::knowledge_bridge::DerivedCoverage::Loading
        ));
        let output = search_current(&loading, &input("retained evidence"));
        assert!(
            output.contains("knowledge_bridge_loading")
                && output.contains("bridge_coverage=loading")
                && !output.contains("no_evidence_complete")
                && !output.contains("docs/recovery.md"),
            "a loading bridge must not serve hits or claim complete absence: {output}"
        );
        let review = crate::protocol::knowledge_review::review_current(
            &loading,
            &crate::protocol::search_tools::ReviewKnowledgeInput {
                mode: crate::protocol::search_tools::ReviewKnowledgeMode::Summary,
                path: None,
                path_prefix: None,
                source_scope: Some(KnowledgeSourceScope::Current),
                project: None,
                projects: None,
                limit: Some(1),
                max_tokens: None,
            },
        );
        let review = review.expect_err("review must refuse a loading bridge");
        assert!(
            review.contains("knowledge_bridge_loading"),
            "review refusal must name the loading bridge: {review}"
        );
        let map = crate::protocol::knowledge_model::render_repository_knowledge_map(&loading);
        assert!(
            map.contains("bridge=loading") && map.contains("still publishing"),
            "knowledge map must not look like an empty complete corpus: {map}"
        );
    }

    #[test]
    fn sensitive_source_version_is_withheld_before_any_hit_is_formatted() {
        let project = tempfile::tempdir().expect("project");
        std::fs::create_dir_all(project.path().join("docs")).expect("docs");
        std::fs::write(
            project.path().join("docs").join("recovery.md"),
            "# Recovery\nSafe checkpoint evidence.\n",
        )
        .expect("document");
        let shared = LiveIndex::load(project.path()).expect("load generation");
        let current = shared.published_source_set().current_generation();
        let canary = ["runtime", "-", "canary", "-", "segment"].concat();
        let mut source_version = current
            .source_version
            .as_ref()
            .expect("source version")
            .as_ref()
            .clone();
        source_version.branch = Some(format!("token={canary}"));
        let unsafe_generation = PublishedGeneration {
            publication_generation: current.publication_generation,
            content_generation: current.content_generation,
            project_generation: current.project_generation,
            source: current.source.clone(),
            source_version: Some(Arc::new(source_version)),
            freshness: Arc::clone(&current.freshness),
            manifest: current.manifest.clone(),
            code_signals: Arc::clone(&current.code_signals),
            bridge: Arc::clone(&current.bridge),
            authority: Arc::clone(&current.authority),
            live: Arc::clone(&current.live),
            health: Arc::clone(&current.health),
            outline: Arc::clone(&current.outline),
            memory_counts: std::sync::OnceLock::new(),
        };

        let output = search_current(&unsafe_generation, &input("checkpoint evidence"));
        assert!(
            output.contains("evidence_withheld"),
            "unsafe source provenance must fail closed"
        );
        assert!(
            !output.contains(&canary),
            "unsafe source provenance must never be echoed"
        );
        assert!(
            !output.contains("docs/recovery.md"),
            "no hit may be formatted under unsafe source provenance"
        );
    }

    #[test]
    fn unknown_lifecycle_units_remain_visible_at_default_authority_scope() {
        let project = tempfile::tempdir().expect("project");
        std::fs::create_dir_all(project.path().join("docs")).expect("docs");
        std::fs::write(
            project.path().join("docs").join("notes.md"),
            "# Notes\nXylophone lifecycle canary with no declared status.\n",
        )
        .expect("undeclared document");
        let shared = LiveIndex::load(project.path()).expect("load generation");
        let current = shared.published_source_set().current_generation();

        let default_output = search_current(&current, &input("xylophone lifecycle canary"));
        assert!(
            default_output.contains("docs/notes.md"),
            "default authority_scope must still surface an unevidenced unit: {default_output}"
        );
        assert!(
            default_output.contains("lifecycle=unknown"),
            "the hit must report the lifecycle that was actually observed: {default_output}"
        );
        assert!(
            default_output.contains("voice=unknown"),
            "an unevidenced lifecycle must not be consumed as voice=current: {default_output}"
        );

        let mut history = input("xylophone lifecycle canary");
        history.authority_scope = Some(KnowledgeAuthorityScope::History);
        let history_output = search_current(&current, &history);
        assert!(
            !history_output.contains("docs/notes.md"),
            "history scope must not surface an Unknown-voice unit, or the default-scope hit is unfiltered: {history_output}"
        );
    }
}