frigg 0.9.2

Frigg gives AI agents local, source-backed code search and navigation without sending whole repositories through every prompt.
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
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
//! MCP policy resources and routing prompts that describe language support, tool-surface profiles,
//! and when agents should prefer Frigg tools over shell reads.

use rmcp::model::{
    GetPromptResult, Prompt, PromptArgument, PromptMessage, ReadResourceResult, Resource,
    ResourceContents, Role,
};
use serde::Serialize;
use serde_json::{Map, Value, json};

use crate::languages::{LanguageSupportCapability, SymbolLanguage};
use crate::mcp::tool_surface::{ToolSurfaceProfile, manifest_for_tool_surface_profile};
use crate::settings::{
    DEFAULT_GOOGLE_EMBEDDING_MODEL, DEFAULT_LOCAL_EMBEDDING_MODEL,
    DEFAULT_OPENAI_COMPAT_EMBEDDING_MODEL, DEFAULT_OPENAI_EMBEDDING_MODEL, GEMINI_API_KEY_ENV_VAR,
    OPENAI_API_KEY_ENV_VAR, OPENAI_COMPAT_API_KEY_ENV_VAR, OPENAI_COMPAT_ENDPOINT_ENV_VAR,
};
use crate::storage::DEFAULT_VECTOR_DIMENSIONS;

pub(crate) const SUPPORT_MATRIX_RESOURCE_URI: &str = "frigg://policy/support-matrix.json";
pub(crate) const TOOL_SURFACE_RESOURCE_URI: &str = "frigg://policy/tool-surface.json";
pub(crate) const SHELL_REPLACEMENT_MAP_RESOURCE_URI: &str =
    "frigg://policy/shell-replacement-map.json";
/// Machine schema for skill-composed multi-claim review packets (not a callable MCP tool).
pub(crate) const EVIDENCE_PACKET_RESOURCE_URI: &str = "frigg://policy/evidence-packet.json";
/// Curated embedding-model scoreboard (peer to support-matrix; not live quality metrics).
pub(crate) const SEMANTIC_MODELS_RESOURCE_URI: &str = "frigg://policy/semantic-models.json";
pub(crate) const SHELL_GUIDANCE_RESOURCE_URI: &str = "frigg://guidance/shell-vs-frigg.md";
pub(crate) const ROUTING_STATS_RESOURCE_URI: &str =
    crate::mcp::routing_stats::ROUTING_STATS_RESOURCE_URI;
pub(crate) const ROUTING_GUIDE_PROMPT_NAME: &str = "frigg-routing-guide";

/// Native output width of the default local MiniLM alias (parity with
/// `embeddings::local_model::DEFAULT_LOCAL_MODEL_ALIAS.dimensions` when local-embeddings is on).
const LOCAL_DEFAULT_NATIVE_DIMENSIONS: usize = 384;
/// Dimensions Frigg requests/stores for OpenAI default model (matches projection; no pad).
const OPENAI_DEFAULT_NATIVE_DIMENSIONS: usize = DEFAULT_VECTOR_DIMENSIONS;
/// Dimensions Frigg requests for Google default model via `output_dimensionality`
/// (index/query paths pass `Some(DEFAULT_VECTOR_DIMENSIONS)` — not API catalog default 3072 / MRL 768).
const GOOGLE_DEFAULT_NATIVE_DIMENSIONS: usize = DEFAULT_VECTOR_DIMENSIONS;

#[derive(Debug, Clone, Serialize)]
struct LanguageSupportEntry {
    id: &'static str,
    display_name: &'static str,
    capabilities: Value,
    search_outline: &'static str,
    navigation: &'static str,
    semantic_retrieval: &'static str,
    capability_note: &'static str,
}

fn support_matrix_json() -> String {
    let languages = SymbolLanguage::ALL
        .into_iter()
        .map(|language| LanguageSupportEntry {
            id: support_matrix_language_id(language),
            display_name: language.display_name(),
            capabilities: language_capabilities_json(language),
            search_outline: support_matrix_search_outline(language),
            navigation: support_matrix_navigation(language),
            semantic_retrieval: support_matrix_semantic_retrieval(language),
            capability_note: support_matrix_capability_note(language),
        })
        .collect::<Vec<_>>();
    serde_json::to_string_pretty(&json!({
        "schema_id": "frigg.policy.support_matrix.v4",
        "product": "frigg",
        "product_boundary": "local-first deterministic code-evidence engine delivered through MCP",
        "stable_core": [
            "repository discovery and attach",
            "safe file reads",
            "text, symbol, and hybrid search",
            "read-only navigation",
            "evidence-backed auditing"
        ],
        "optional_accelerators": [
            "semantic retrieval",
            "external SCIP ingestion",
            "built-in watch mode"
        ],
        "advanced_consumers": support_matrix_advanced_consumers(),
        "language_support_notes": [
            "Frigg currently supports the listed languages for source-backed search, outline, structural, and hybrid retrieval workflows.",
            "Navigation stays read-only and may combine source heuristics, graph evidence, and optional external artifacts.",
            "Semantic retrieval is optional acceleration only and never the grounding layer."
        ],
        "capability_tiers": {
            "core": "capability is part of FRIGG's stable read-only core contract for that language",
            "optional_accelerator": "capability is an optional accelerator that only contributes when runtime configuration and repository state make it available",
            "unsupported": "capability is not currently provided for that language in the runtime registry"
        },
        "languages": languages
    }))
    .expect("support matrix JSON should serialize")
}

/// Curated embedding model catalog (EXP-scoreboard B).
///
/// Static rows: defaults and contract facts, not a live CI leaderboard. Product was validated
/// hands-on across multi-repo playbooks; this resource does not retain published rank scores.
/// Semantic retrieval remains optional acceleration (see support-matrix), never the grounding layer.
fn semantic_models_json() -> String {
    serde_json::to_string_pretty(&json!({
        "schema_id": "frigg.policy.semantic_models.v1",
        "product": "frigg",
        "product_role": "optional_accelerator_model_catalog",
        "live": true,
        "quality_scores": "curated",
        "quality_scores_note": "Rows are curated defaults and contract facts (dims, pad, offline, credentials). Frigg does not ship a retained public embedding leaderboard; early multi-repo playbook validation is not re-exported as scores. Prefer exact Frigg tools over hybrid rank-1 even when semantic is on.",
        "semantic_default": {
            "enabled": false,
            "note": "Semantic runtime is off by default. When enabled without a cloud provider, Frigg resolves to local MiniLM."
        },
        "projection_dimensions": DEFAULT_VECTOR_DIMENSIONS,
        "projection_note": "sqlite-vec table width (embedding float[N]). Fixed so one vector index can hold multiple providers. Short model vectors are zero-padded on write/query to fit N; oversize is rejected. Padding is storage interoperability only — it does not add semantic signal or make MiniLM '1536-quality'.",
        "dimensions_contract": {
            "model_field": "native_dimensions",
            "model_field_meaning": "REAL model output width before any storage pad (never the padded store length)",
            "store_field": "projection_dimensions",
            "pad_flag": "pad_to_projection",
            "pad_flag_meaning": "true only when native_dimensions < projection_dimensions; zeros fill the gap at the DB boundary",
            "why_pad": "vec0 requires every row to have exactly projection_dimensions floats; local MiniLM is 384-d so it is padded to fit. Partitions (repository_id, provider, model) keep different models from sharing one cosine space.",
            "reindex": "Changing provider or model requires a semantic reindex (frigg index); partitions do not auto-heal the active head"
        },
        "reindex_on_change": true,
        "source_of_truth": {
            "defaults": "crates/cli/src/settings/semantic_runtime.rs DEFAULT_*_EMBEDDING_MODEL",
            "projection": "crates/cli/src/storage/mod.rs DEFAULT_VECTOR_DIMENSIONS",
            "local_dims": "crates/cli/src/embeddings/local_model.rs DEFAULT_LOCAL_MODEL_ALIAS.dimensions",
            "normalize": "crates/cli/src/storage/semantic_store_support.rs::normalize_embedding_for_vector_projection"
        },
        "models": [
            {
                "id": "local-minilm-l6-v2",
                "provider": "local",
                "model": DEFAULT_LOCAL_EMBEDDING_MODEL,
                "role": "default",
                "quality_tier": "offline_smoke",
                "offline": true,
                "native_dimensions": LOCAL_DEFAULT_NATIVE_DIMENSIONS,
                "pad_to_projection": LOCAL_DEFAULT_NATIVE_DIMENSIONS < DEFAULT_VECTOR_DIMENSIONS,
                "credential_env": null,
                "reindex_on_change": true,
                "quality": "curated",
                "known_limits": [
                    "Offline smoke / zero-key accelerator — general-purpose MiniLM, not a code-specialized embedder",
                    "Product/natural phrases may map weakly to API identifiers (prefer search_symbol / search_text after hybrid)",
                    "Requires local model preparation at startup when provider=local",
                    "native_dimensions is 384 (real); store pads with zeros to projection_dimensions — pad is storage only, not quality",
                    "Embed documents use path+language envelope; after template upgrade run full frigg index (not changed-only)"
                ]
            },
            {
                "id": "openai-text-embedding-3-small",
                "provider": "openai",
                "model": DEFAULT_OPENAI_EMBEDDING_MODEL,
                "role": "recommended",
                "quality_tier": "cloud",
                "offline": false,
                "native_dimensions": OPENAI_DEFAULT_NATIVE_DIMENSIONS,
                "pad_to_projection": OPENAI_DEFAULT_NATIVE_DIMENSIONS < DEFAULT_VECTOR_DIMENSIONS,
                "credential_env": OPENAI_API_KEY_ENV_VAR,
                "reindex_on_change": true,
                "quality": "curated",
                "known_limits": [
                    "Requires OPENAI_API_KEY and network",
                    "Cloud embeddings leave the machine; use local when zero-cloud is required"
                ]
            },
            {
                "id": "google-gemini-embedding-001",
                "provider": "google",
                "model": DEFAULT_GOOGLE_EMBEDDING_MODEL,
                "role": "recommended",
                "quality_tier": "credential_peer",
                "recommended_when": "GEMINI_API_KEY already present (bring-your-key); not Frigg's preferred cloud default over OpenAI",
                "offline": false,
                "native_dimensions": GOOGLE_DEFAULT_NATIVE_DIMENSIONS,
                "pad_to_projection": GOOGLE_DEFAULT_NATIVE_DIMENSIONS < DEFAULT_VECTOR_DIMENSIONS,
                "credential_env": GEMINI_API_KEY_ENV_VAR,
                "reindex_on_change": true,
                "quality": "curated",
                "known_limits": [
                    "Credential-ecosystem peer: use when GEMINI_API_KEY is already present — not Frigg's preferred cloud default over OpenAI",
                    "Requires GEMINI_API_KEY and network",
                    "native_dimensions is the width Frigg requests (output_dimensionality), not a padded value",
                    "API catalog default may be 3072; Frigg requests native_dimensions — no storage pad when equal to projection",
                    "OpenAI-only shops need not configure Google; multi-key is never required"
                ]
            },
            {
                "id": "openai-compat-protocol",
                "provider": "openai_compat",
                "model": DEFAULT_OPENAI_COMPAT_EMBEDDING_MODEL,
                "role": "experimental",
                "quality_tier": "selfhost_protocol",
                "offline": false,
                "native_dimensions": OPENAI_DEFAULT_NATIVE_DIMENSIONS,
                "pad_to_projection": OPENAI_DEFAULT_NATIVE_DIMENSIONS < DEFAULT_VECTOR_DIMENSIONS,
                "credential_env": OPENAI_COMPAT_API_KEY_ENV_VAR,
                "endpoint_env": OPENAI_COMPAT_ENDPOINT_ENV_VAR,
                "reindex_on_change": true,
                "quality": "curated",
                "known_limits": [
                    "Requires FRIGG_SEMANTIC_RUNTIME_OPENAI_COMPAT_ENDPOINT (full embeddings POST URL)",
                    "Requires FRIGG_OPENAI_COMPAT_API_KEY (or OPENAI_API_KEY fallback) as Bearer token",
                    "Same OpenAI HTTP wire format; backend quality/dims are operator-owned",
                    "Storage partition is provider=openai_compat + model string — not openai",
                    "Set FRIGG_SEMANTIC_RUNTIME_MODEL to the backend model id when it is not text-embedding-3-small"
                ]
            }
        ],
        "presets": [
            {
                "id": "offline-small",
                "intent": "Zero-cloud offline_smoke MiniLM when you enable semantic without API keys",
                "provider": "local",
                "model": DEFAULT_LOCAL_EMBEDDING_MODEL,
                "model_id": "local-minilm-l6-v2",
                "quality": "curated",
                "quality_tier": "offline_smoke",
                "cli_alias": false,
                "expands_to": {
                    "FRIGG_SEMANTIC_RUNTIME_ENABLED": "true",
                    "FRIGG_SEMANTIC_RUNTIME_PROVIDER": "local",
                    "FRIGG_SEMANTIC_RUNTIME_MODEL": DEFAULT_LOCAL_EMBEDDING_MODEL
                },
                "required_credential_env": null,
                "storage_keys": {
                    "provider": "local",
                    "model": DEFAULT_LOCAL_EMBEDDING_MODEL,
                    "note": "Partition identity is always provider+model strings — never the preset id alone"
                },
                "failure_modes": [
                    "Semantic still off by product default until FRIGG_SEMANTIC_RUNTIME_ENABLED=true",
                    "Local model prepare failure at startup (cache / HF artifacts)",
                    "Product-phrase → API mapping can be weak; still pivot hybrid to search_text / search_symbol",
                    "semantic_status ok under MiniLM means vectors ran — still exact-pivot for proof",
                    "Changing model later requires frigg index semantic pass (reindex_on_change)"
                ]
            },
            {
                "id": "cloud-openai",
                "intent": "Cloud OpenAI embeddings when OPENAI_API_KEY is available",
                "provider": "openai",
                "model": DEFAULT_OPENAI_EMBEDDING_MODEL,
                "model_id": "openai-text-embedding-3-small",
                "quality": "curated",
                "quality_tier": "cloud",
                "cli_alias": false,
                "expands_to": {
                    "FRIGG_SEMANTIC_RUNTIME_ENABLED": "true",
                    "FRIGG_SEMANTIC_RUNTIME_PROVIDER": "openai",
                    "FRIGG_SEMANTIC_RUNTIME_MODEL": DEFAULT_OPENAI_EMBEDDING_MODEL
                },
                "required_credential_env": OPENAI_API_KEY_ENV_VAR,
                "storage_keys": {
                    "provider": "openai",
                    "model": DEFAULT_OPENAI_EMBEDDING_MODEL,
                    "note": "Partition identity is always provider+model strings — never the preset id alone"
                },
                "failure_modes": [
                    "Missing or empty OPENAI_API_KEY (fail-fast at semantic startup)",
                    "Network / provider outage → semantic degraded; lexical/graph still work",
                    "Cloud embeddings leave the machine (use offline-small for zero-cloud)",
                    "Changing model later requires frigg index semantic pass (reindex_on_change)"
                ]
            },
            {
                "id": "cloud-google",
                "intent": "Cloud Google embeddings when GEMINI_API_KEY is already present (credential peer, not preferred cloud default)",
                "provider": "google",
                "model": DEFAULT_GOOGLE_EMBEDDING_MODEL,
                "model_id": "google-gemini-embedding-001",
                "quality": "curated",
                "quality_tier": "credential_peer",
                "cli_alias": false,
                "expands_to": {
                    "FRIGG_SEMANTIC_RUNTIME_ENABLED": "true",
                    "FRIGG_SEMANTIC_RUNTIME_PROVIDER": "google",
                    "FRIGG_SEMANTIC_RUNTIME_MODEL": DEFAULT_GOOGLE_EMBEDDING_MODEL
                },
                "required_credential_env": GEMINI_API_KEY_ENV_VAR,
                "storage_keys": {
                    "provider": "google",
                    "model": DEFAULT_GOOGLE_EMBEDDING_MODEL,
                    "note": "Partition identity is always provider+model strings — never the preset id alone"
                },
                "failure_modes": [
                    "Missing or empty GEMINI_API_KEY (fail-fast at semantic startup)",
                    "Network / provider outage → semantic degraded; lexical/graph still work",
                    "Frigg requests output_dimensionality = projection_dimensions for this model",
                    "Credential peer only — not Frigg's preferred cloud default over OpenAI",
                    "Changing model later requires frigg index semantic pass (reindex_on_change)"
                ]
            },
            {
                "id": "openai-compat-selfhost",
                "intent": "OpenAI-protocol embeddings at a configured endpoint (vLLM, LM Studio, Azure-compatible, gateways)",
                "provider": "openai_compat",
                "model": DEFAULT_OPENAI_COMPAT_EMBEDDING_MODEL,
                "model_id": "openai-compat-protocol",
                "quality": "curated",
                "cli_alias": false,
                "expands_to": {
                    "FRIGG_SEMANTIC_RUNTIME_ENABLED": "true",
                    "FRIGG_SEMANTIC_RUNTIME_PROVIDER": "openai_compat",
                    "FRIGG_SEMANTIC_RUNTIME_MODEL": DEFAULT_OPENAI_COMPAT_EMBEDDING_MODEL,
                    "FRIGG_SEMANTIC_RUNTIME_OPENAI_COMPAT_ENDPOINT": "<full embeddings POST URL>"
                },
                "required_credential_env": OPENAI_COMPAT_API_KEY_ENV_VAR,
                "required_endpoint_env": OPENAI_COMPAT_ENDPOINT_ENV_VAR,
                "storage_keys": {
                    "provider": "openai_compat",
                    "model": DEFAULT_OPENAI_COMPAT_EMBEDDING_MODEL,
                    "note": "Partition identity is always provider+model strings — never the preset id alone; endpoint is not part of the storage key"
                },
                "failure_modes": [
                    "Missing/blank/invalid openai_compat endpoint URL (fail-fast at semantic startup)",
                    "Missing FRIGG_OPENAI_COMPAT_API_KEY (OPENAI_API_KEY is accepted as fallback Bearer)",
                    "Backend model id mismatch — set FRIGG_SEMANTIC_RUNTIME_MODEL explicitly",
                    "Native vector width may differ from 1536 — pad/reject follows store contract; reindex on model change"
                ]
            }
        ],
        "presets_note": "Soft intent aliases over models[] (EXP-code-presets C + openai_compat self-host). Not CLI flags (B deferred). Not brand embedding vendors (Voyage/Cohere deferred). Not auto local-vs-cloud by key presence (E rejected). Set provider+model (+ endpoint for openai_compat) env/config explicitly; preset id is documentation only.",
        "guidance": [
            "Semantic is optional acceleration — never the sole grounding layer for code claims",
            "Local MiniLM is offline_smoke (zero-key general embedder); prefer exact tools after hybrid",
            "Google Gemini is a credential_peer when GEMINI_API_KEY exists — not Frigg's preferred cloud default over OpenAI",
            "After hybrid, pivot to exact search_text / search_symbol before answering",
            "After changing provider or model (or embed template upgrades), run frigg index for a semantic pass",
            "Do not invent unlisted providers or treat preset id as a storage partition key",
            "Prefer presets for intent (offline smoke vs cloud peer keys vs openai_compat); always apply expands_to provider+model strings",
            "openai_compat requires a full embeddings POST URL; storage partition is openai_compat+model, not openai"
        ]
    }))
    .expect("semantic models JSON should serialize")
}

/// JSON Schema-shaped policy resource for agent-assembled evidence packets (EXP-evidence-packet A+D).
///
/// Composition stays in the skill; this resource is machine documentation only — not an MCP tool.
fn evidence_packet_json() -> String {
    serde_json::to_string_pretty(&json!({
        "schema_id": "frigg.policy.evidence_packet.v1",
        "live": true,
        "product_role": "skill_composition_template",
        "not_a_tool": true,
        "not_a_tool_note": "There is no compose_evidence_packet (or similar) public MCP tool. Agents assemble multi-claim packets from search/nav/read witnesses using the skill Technical review / security cards. Rust types EvidencePacket / EvidencePacketClaim mirror this shape for hosts.",
        "source_of_truth": {
            "skill": "skills/frigg-first-code-search/SKILL.md (Technical review evidence packet)",
            "types": "crates/cli/src/mcp/types/navigation.rs::EvidencePacketClaim / EvidencePacket"
        },
        "claim_fields": {
            "claim": { "type": "string", "required": true, "description": "Human claim text" },
            "tool": { "type": "string", "required": true, "description": "Frigg tool that produced the witness" },
            "path": { "type": "string", "required": true, "description": "Repository-relative path" },
            "start_line": { "type": "integer", "required": true, "minimum": 1 },
            "end_line": { "type": "integer", "required": true, "minimum": 1 },
            "match_id": { "type": "string", "required": false, "description": "Scoped match_id from the same call as result_handle" },
            "result_handle": { "type": "string", "required": false, "description": "Session result_handle from the same call" }
        },
        "envelope": {
            "claims": { "type": "array", "items": "claim", "minItems": 1 }
        },
        "example": {
            "claims": [
                {
                    "claim": "catalog_entries registers callable operations",
                    "tool": "search_symbol",
                    "path": "src/catalog/mod.rs",
                    "start_line": 40,
                    "end_line": 72,
                    "match_id": "symbols:m1",
                    "result_handle": "..."
                }
            ]
        },
        "guidance": [
            "Assemble packets after Frigg search/nav/read; do not invent path/line witnesses.",
            "Prefer citation presentation_mode for user-facing fences; packets are multi-claim internal/report structure.",
            "match_id is valid only with its own result_handle from the same tool call.",
            "Do not treat packet assembly as a server-sealed authority; nav mode (heuristic vs precise) still applies."
        ]
    }))
    .expect("evidence packet policy JSON should serialize")
}

fn shell_replacement_map_json() -> String {
    serde_json::to_string_pretty(&json!({
        "schema_id": "frigg.policy.shell_replacement_map.v1",
        "product": "frigg",
        "default_for": [
            "source-code discovery",
            "exact text search",
            "symbol lookup",
            "repository-relative source reads",
            "code navigation"
        ],
        "fallback_only_for": [
            "git state and diffs",
            "non-code files or workspace metadata",
            "build/test output",
            "generated or unindexed files",
            "explicit live-disk verification",
            "Frigg unavailable"
        ],
        "replacements": [
            {
                "shell": "rg --files",
                "tool": "list_files",
                "params": ["path_regex", "glob", "language", "path_class", "include_hidden", "limit", "resume_from"]
            },
            {
                "shell": "rg -n PATTERN",
                "tool": "search_text",
                "params": ["query", "pattern_type", "case_sensitive", "ignore_case", "word", "context_lines", "limit"]
            },
            {
                "shell": "rg -n 'foo|bar'",
                "tool": "search_text",
                "params": ["query", "pattern_type=regex"]
            },
            {
                "shell": "rg -n PATTERN path/",
                "tool": "search_text",
                "params": ["query", "path_regex"]
            },
            {
                "shell": "rg -n -g GLOB PATTERN",
                "tool": "search_text",
                "params": ["query", "glob", "exclude_glob"]
            },
            {
                "shell": "rg -l PATTERN",
                "tool": "search_text",
                "params": ["query", "files_with_matches=true"]
            },
            {
                "shell": "rg -c PATTERN",
                "tool": "search_text",
                "params": ["query", "count_only=true"]
            },
            {
                "shell": "identifier/API/type/class/function lookup",
                "tool": "search_symbol",
                "params": ["query", "path_regex", "path_class", "limit"]
            },
            {
                "shell": "parallel multi-grep / multi-hypothesis probes",
                "tool": "search_batch",
                "params": ["probes", "merge", "limit", "repository_id", "response_mode"],
                "note": "2..=8 independent concurrent text/symbol/hybrid probes, then merge/dedupe — not one shared multi-query walk"
            },
            {
                "shell": "usages / callers / blast radius for a known symbol",
                "tool": "impact_bundle",
                "params": ["symbol", "path_class", "repository_id", "response_mode"],
                "note": "prefer before sequential find_references + incoming_calls + implementations"
            },
            {
                "shell": "cat path",
                "tool": "read_file",
                "params": ["path"]
            },
            {
                "shell": "sed -n '10,80p' path",
                "tool": "read_file",
                "params": ["path", "start_line", "end_line", "line_count"]
            },
            {
                "shell": "follow definitions/references/calls",
                "tool": "navigation tools",
                "params": ["go_to_definition", "find_references", "find_implementations", "incoming_calls", "outgoing_calls", "impact_bundle"]
            }
        ]
    }))
    .expect("shell replacement map JSON should serialize")
}

fn support_matrix_language_id(language: SymbolLanguage) -> &'static str {
    match language {
        SymbolLanguage::TypeScript => "typescript_tsx",
        other => other.as_str(),
    }
}

fn support_matrix_search_outline(language: SymbolLanguage) -> &'static str {
    match language {
        SymbolLanguage::Blade => "supported_template_surface",
        _ => "supported_source_language",
    }
}

fn support_matrix_navigation(language: SymbolLanguage) -> &'static str {
    match language {
        SymbolLanguage::Blade => "bounded_source_template_navigation",
        _ => "read_only_source_graph_or_artifact_assisted",
    }
}

fn support_matrix_semantic_retrieval(language: SymbolLanguage) -> &'static str {
    if language.supports_semantic_chunking() {
        "optional_when_enabled"
    } else {
        "unsupported"
    }
}

fn support_matrix_capability_note(language: SymbolLanguage) -> &'static str {
    match language {
        SymbolLanguage::Blade => "template_metadata_livewire_flux",
        _ => "general_source_support",
    }
}

fn language_capabilities_json(language: SymbolLanguage) -> Value {
    let mut capabilities = Map::new();
    for capability in LanguageSupportCapability::ALL {
        capabilities.insert(
            capability.as_str().to_owned(),
            Value::String(language.capability_tier(capability).as_str().to_owned()),
        );
    }
    Value::Object(capabilities)
}

fn extended_only_tool_names() -> Vec<String> {
    let core = manifest_for_tool_surface_profile(ToolSurfaceProfile::Core);
    let extended = manifest_for_tool_surface_profile(ToolSurfaceProfile::Extended);
    extended
        .tool_names
        .into_iter()
        .filter(|tool_name| !core.tool_names.contains(tool_name))
        .collect()
}

fn support_matrix_advanced_consumers() -> Vec<String> {
    let mut consumers = extended_only_tool_names();
    consumers.push("self_improvement_loop".to_owned());
    consumers
}

fn tool_surface_json(active_profile: ToolSurfaceProfile) -> String {
    let core = manifest_for_tool_surface_profile(ToolSurfaceProfile::Core);
    let active = manifest_for_tool_surface_profile(active_profile);
    let core_guidance = if cfg!(feature = "playbook") {
        "Product tools including explore are on core. Playbook tools are compile-time opt-in (`--features playbook`) and extended-profile only — not default cargo features. Set FRIGG_MCP_TOOL_SURFACE_PROFILE=core to hide playbook tools even when the binary was built with the playbook feature."
    } else {
        "Product tools including explore are on core. Playbook tools require building with `--features playbook` and the extended profile; they are not on default builds."
    };
    serde_json::to_string_pretty(&json!({
        "schema_id": "frigg.policy.tool_surface.v1",
        "live": true,
        "source_of_truth": {
            "public_tool_names": "crates/cli/src/mcp/types.rs::PUBLIC_TOOL_NAMES",
            "profile_manifest": "crates/cli/src/mcp/tool_surface.rs::manifest_for_tool_surface_profile",
            "process_registered": "workspace.runtime.tools_exposed or MCP tools/list"
        },
        "not_authoritative": [
            "Historical inventory freezes (for example docs/futura-phase0-inventory.md) are forensic only",
            "Host schema caches and non-public #[tool] handlers are not the public surface"
        ],
        "default_profile": ToolSurfaceProfile::Extended.as_str(),
        "active_profile": active_profile.as_str(),
        "core_tools": core.tool_names,
        "extended_only_tools": extended_only_tool_names(),
        "active_tools": active.tool_names,
        "guidance": [
            "This resource is the machine-readable live tool surface. Prefer it (or tools/list / workspace.runtime.tools_exposed) over Phase 0 / systems inventory freezes.",
            "Use Frigg as the default for code discovery, file listing, navigation, exact code search, and bounded source reads.",
            "Use workspace for compact workspace status or to adopt a target path/repository; repo-aware tools auto-adopt sensible defaults when possible.",
            "Workspace freshness is authoritative: clean snapshot=ready is usable on HTTP and stdio; wait_for_refresh occurs only for leased debouncing/refreshing dirty work. mode_off, no_lease, retry_backoff, blocked, and notify_degraded cannot converge by waiting, so use live disk for touched paths. Missing/uninitialized/error snapshots require CLI/operator frigg index; there is no public reindex/write tool. Use exact next_actions when present; legacy gate fields remain compatibility projections for two minor releases.",
            "Before shell rg/grep/find/fd/cat/sed for code exploration, use list_files, search_text, search_symbol, search_batch, search_hybrid, read_file, read_match, impact_bundle, or navigation tools.",
            "Use search_hybrid only for broad discovery-style repository questions; use search_text for rg-shaped literal or safe-regex code scans, including grouped alternation and path_regex narrowing; pass the search term as query, not pattern; use search_symbol for known identifiers; use search_batch for multi-hypothesis guesses (2..=8 independent concurrent probes, then fixed consensus-first reciprocal_rank_fusion); inspect evidence/completeness and replay its opaque continuation only unchanged; use impact_bundle with a copied target_ref for usages/callers/blast radius; use list_files for rg --files-shaped listing.",
            "Use shell tools as the exception for git state and diffs, non-code files, build/test output, generated or unindexed files, explicit live-disk verification, and unavailable Frigg results.",
            "Use Frigg when repository-aware evidence, symbols, navigation, provenance, or multi-repo context matter.",
            "Read surfaces are text-first by default: read_file, read_match, and explore(operation=zoom). Request presentation_mode=json when a downstream consumer needs the structured compatibility payload.",
            "next_actions[].tool plus exact next_actions[].arguments is authoritative for follow-ups; execute the named existing MCP tool yourself, respecting role/order/dependencies and host authorization. Compact and full carry identical action data. suggested_next is deprecated and lossy; stale or mixed proof handles require rerunning the typed origin producer and choosing a fresh match_id. No generic executor or automatic chaining endpoint exists.",
            "For navigation and impact, prefer search -> copy a row's target_ref unchanged -> pass it as target to the navigation tool or impact_bundle. result_match {result_handle, match_id, target_scope} is session/source scoped; target_scope is opaque correlation, not authentication. stable_symbol {repository_id, stable_symbol_id, snapshot_token} is repository/corpus scoped and never crosses repositories. On TARGET_SCOPE_MISMATCH, STALE_TARGET_SNAPSHOT, stale handle/proof, or TARGET_NOT_FOUND, rerun the producer and use a fresh target; Frigg does not navigate historical source. Direct symbol/location fields remain compatibility input, but cannot be mixed with target; a matching repository_id is only an assertion. Ambiguous legacy impact does not run children; read sections for execution/trust/completeness, use section proof_targets with next_actions, opt into test evidence with include_test_mentions, and keep outgoing calls separate.",
            "Use include_follow_up_structural=true when you want replayable search_structural follow-ups from inspect_syntax_tree, search_structural, or anchored navigation and outline results.",
            core_guidance
        ]
    }))
    .expect("tool surface JSON should serialize")
}

fn shell_vs_frigg_markdown(active_profile: ToolSurfaceProfile) -> String {
    let explore_guidance = "`explore` is on the core product surface for bounded single-artifact follow-up after discovery. `explore(operation=zoom)` defaults to the same text-first read rendering as `read_file` and `read_match`, while `probe` and `refine` stay structured by default.";
    let playbook_guidance = if cfg!(feature = "playbook") {
        if active_profile == ToolSurfaceProfile::Extended {
            "Playbook tools (`playbook_run`, `playbook_replay`, `playbook_compose_citations`) are present on this extended build — they are trace/dev tooling, not first-line discovery."
        } else {
            "Playbook tools are compiled in but hidden on the `core` profile; set FRIGG_MCP_TOOL_SURFACE_PROFILE=extended only for explicit playbook workflows."
        }
    } else {
        "Playbook tools are not compiled into this binary (build with `--features playbook` only for explicit trace/dev workflows)."
    };
    format!(
        "# Shell vs Frigg\n\n\
    Use Frigg as the default for code discovery, file listing, navigation, exact code search, and bounded source reads.\n\n\
    - repository-aware file listing through `list_files` instead of `rg --files`, `find`, or `fd`\n\
    - symbol, definition, reference, implementation, or call navigation\n\
    - exact literal, safe-regex, grouped-alternation, or `rg`-shaped code searches with repository scoping and result handles\n\
    - bounded source reads through `read_file`, `read_match`, or `explore(operation=zoom)`\n\
    - mixed doc/runtime questions where lexical, graph, witness, and semantic channels may all matter\n\
    - evidence-backed answers or replayable source references\n\
    - attached multi-repo context instead of one current shell directory\n\n\
    Use shell tools only as the exception for git state and diffs, non-code files, build/test output, generated or unindexed files, explicit live-disk verification, and unavailable Frigg results.\n\n\
    Use shell `rg` for explicit live-disk verification, ripgrep-specific flags outside `search_text`, or generated/unindexed files.\n\n\
    ## Workspace freshness\n\n\
    `workspace.freshness` is authoritative. A clean `snapshot=ready` works on HTTP and stdio; do not wait merely because watch is off. For dirty known paths, use `wait_for_refresh` only when a leased watch is `debouncing` or `refreshing`. `mode_off`, `no_lease`, `retry_backoff`, `blocked`, and `notify_degraded` cannot converge by waiting: direct-read touched paths, or use HTTP/operator recovery. Missing, uninitialized, or error snapshots require CLI `frigg index`; this is not a public MCP write/reindex tool. Follow exact `next_actions` when present; legacy gate fields are compatibility projections retained for two minor releases. After a touched edit, rerun a `read_match` producer before proof.\n\n\
    Shell replacement map:\n\
    - `rg --files` -> `list_files`\n\
    - `rg -n \"text\"` -> `search_text`\n\
    - `rg -n \"foo|bar\"` -> `search_text` with regex mode\n\
    - `rg -n \"text\" path/` -> `search_text` with `path_regex`\n\
    - identifier/API/type/class/function lookup -> `search_symbol`\n\
    - parallel multi-grep / multi-hypothesis probes -> `search_batch` (2..=8 independent concurrent probes, then consensus-first fixed-RRF merge/dedupe; not one shared multi-query walk)\n\
    - usages / callers / blast radius for a known symbol -> prefer `impact_bundle(target)` with a copied target_ref before sequential navigation tools\n\
    - `cat path` -> `read_file`\n\
    - `sed -n '10,80p' path` -> `read_file` with `start_line`, `end_line`, or `line_count`\n\
    - follow definitions/references/calls -> navigation tools (or `impact_bundle` when the symbol is already known)\n\n\
    Use `search_hybrid` only for broad discovery-style repository questions when there is no stable string, symbol, or path anchor yet. Use `search_text` for `rg`-shaped literal or safe-regex scans, including grouped alternation, `path_regex` narrowing, context windows, per-file limits (`max_count_per_file`), and file-containment probes (`files_with_matches`). For `search_text`, pass the search term as `query`, not `pattern`. Frigg may execute those scans with its native scanner, its ripgrep accelerator, or a mixed path while preserving repository-scoped results and result handles. Use `search_symbol` for known identifiers. Use `search_batch` when you would fire several Frigg probes in one turn (text/symbol/hybrid); each probe is a full independent search, then results merge by consensus, fixed equal-weight reciprocal-rank fusion, and derived strength. Read per-row `evidence`, `consensus_count`, `rrf_score`, and `match_strength`, plus per-probe and aggregate `completeness`; `merge_strategy` is fixed to `reciprocal_rank_fusion`, and legacy `merge=rank_by_probe_hit_strength` is a two-minor compatibility input only. Replay its opaque `continuation` only with the same normalized probes, scopes, and snapshots. Prefer `impact_bundle` for impact/refactor questions with a copied target_ref before chaining `find_references` / `incoming_calls` / `find_implementations` by hand.\n\n\
    `read_file` and `read_match` default to text-first output. Ask for `presentation_mode=json` when a caller needs the structured compatibility payload with explicit `content`, and apply the same rule to `explore(operation=zoom)`.\n\n\
    Structural follow-up suggestions are opt-in. Use `include_follow_up_structural=true` on `inspect_syntax_tree`, `search_structural`, or anchored navigation and outline tools when you want replayable `search_structural` follow-ups derived from the resolved AST focus.\n\n\
    For exact navigation, use search -> copy a row's `target_ref` unchanged -> pass it as `target` to navigation or `impact_bundle`. A result-match target has `kind=result_match`, `result_handle`, `match_id`, and `target_scope`, and is session/source scoped; `target_scope` is opaque correlation, not authentication. A stable-symbol target has `kind=stable_symbol`, `repository_id`, `stable_symbol_id`, and `snapshot_token`, and is repository/corpus scoped. Do not combine `target` with direct symbol/location fields (`CONFLICTING_TARGET_INPUT`); a matching top-level `repository_id` is an assertion, and a mismatch returns `TARGET_REPOSITORY_MISMATCH`. On `TARGET_SCOPE_MISMATCH`, `STALE_TARGET_SNAPSHOT`, stale handle/proof, or `TARGET_NOT_FOUND`, rerun the producer and copy a fresh target; Frigg does not navigate historical source. `impact_bundle` accepts exactly one of `target` or legacy non-empty `symbol`; target mode resolves once for all child work, while legacy same-rank symbol results surface disambiguation and run no child section. Its authoritative `sections[]` keeps execution, trust, and completeness separate; use section-qualified `proof_targets` with canonical `next_actions`, opt into exact test evidence with `include_test_mentions=true`, and keep outgoing calls as a separate provisional tool.\n\n\
    Semantic retrieval remains an optional accelerator, not the grounding layer.\n\n\
    {explore_guidance}\n\
    {playbook_guidance}\n"
    )
}

pub(crate) fn policy_resources() -> Vec<Resource> {
    vec![
        Resource::new(SUPPORT_MATRIX_RESOURCE_URI, "FRIGG Support Matrix")
            .with_description("Machine-readable supported languages and capability notes.")
            .with_mime_type("application/json"),
        Resource::new(TOOL_SURFACE_RESOURCE_URI, "FRIGG Tool Surface Policy")
            .with_description(
                "Live machine catalog of core vs extended MCP tools (active_tools, profile manifests). Prefer over inventory freezes.",
            )
            .with_mime_type("application/json"),
        Resource::new(
            SHELL_REPLACEMENT_MAP_RESOURCE_URI,
            "FRIGG Shell Replacement Map",
        )
        .with_description("Machine-readable shell-to-Frigg replacement table.")
        .with_mime_type("application/json"),
        Resource::new(
            EVIDENCE_PACKET_RESOURCE_URI,
            "FRIGG Evidence Packet Schema",
        )
        .with_description(
            "Skill-composed multi-claim evidence packet shape (schema only; not a callable MCP tool).",
        )
        .with_mime_type("application/json"),
        Resource::new(
            SEMANTIC_MODELS_RESOURCE_URI,
            "FRIGG Semantic Models Catalog",
        )
        .with_description(
            "Curated embedding-model defaults, soft intent presets (offline-small / cloud-*), and contract facts (dims, pad, offline, credentials). No retained public leaderboard; peer to support-matrix. Presets are not CLI flags.",
        )
        .with_mime_type("application/json"),
        Resource::new(SHELL_GUIDANCE_RESOURCE_URI, "Shell vs Frigg Guidance")
            .with_description(
                "Guidance for when to use shell tools versus repo-aware Frigg surfaces.",
            )
            .with_mime_type("text/markdown"),
        Resource::new(ROUTING_STATS_RESOURCE_URI, "Frigg Routing Stats")
            .with_description(
                "Local opt-in session routing stats (tool mix, zero-hits, recovery, handle failures). Enable with FRIGG_ROUTING_STATS=1. No cloud telemetry.",
            )
            .with_mime_type("application/json"),
    ]
}

pub(crate) fn read_policy_resource(
    uri: &str,
    active_profile: ToolSurfaceProfile,
) -> Option<ReadResourceResult> {
    let (content, mime_type) = match uri {
        SUPPORT_MATRIX_RESOURCE_URI => (support_matrix_json(), "application/json"),
        TOOL_SURFACE_RESOURCE_URI => (tool_surface_json(active_profile), "application/json"),
        SHELL_REPLACEMENT_MAP_RESOURCE_URI => (shell_replacement_map_json(), "application/json"),
        EVIDENCE_PACKET_RESOURCE_URI => (evidence_packet_json(), "application/json"),
        SEMANTIC_MODELS_RESOURCE_URI => (semantic_models_json(), "application/json"),
        SHELL_GUIDANCE_RESOURCE_URI => (shell_vs_frigg_markdown(active_profile), "text/markdown"),
        ROUTING_STATS_RESOURCE_URI => (
            crate::mcp::routing_stats::snapshot_json(),
            "application/json",
        ),
        _ => return None,
    };

    Some(ReadResourceResult::new(vec![
        ResourceContents::text(content, uri).with_mime_type(mime_type),
    ]))
}

pub(crate) fn guidance_prompts() -> Vec<Prompt> {
    vec![Prompt::new(
        ROUTING_GUIDE_PROMPT_NAME,
        Some("Route a code question toward Frigg tools, shell exceptions, or extended follow-up."),
        Some(vec![PromptArgument::new("task")
            .with_description("Optional task or question to route.")
            .with_required(false)]),
    )
    .with_title("FRIGG Routing Guide")]
}

pub(crate) fn read_guidance_prompt(
    name: &str,
    arguments: Option<&Map<String, Value>>,
    active_profile: ToolSurfaceProfile,
) -> Option<GetPromptResult> {
    if name != ROUTING_GUIDE_PROMPT_NAME {
        return None;
    }

    let task = arguments
        .and_then(|map| map.get("task"))
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|value| !value.is_empty());
    let profile_note = if active_profile == ToolSurfaceProfile::Extended {
        "Active profile: `extended`."
    } else {
        "Active profile: `core`."
    };
    let mut text = String::new();
    if let Some(task) = task {
        text.push_str("Task:\n");
        text.push_str(task);
        text.push_str("\n\n");
    }
    text.push_str(
        "Routing policy:\n\
1. Prefer Frigg for code discovery, navigation, exact code search, and bounded source reads.\n\
2. Use `search_hybrid` only for broad discovery-style repository questions when there is no stable string, symbol, or path anchor yet; use `search_text` for `rg`-shaped literal or safe-regex scans, including grouped alternation and `path_regex`; use `search_symbol` for known identifiers; use `search_batch` for multi-hypothesis guesses (2..=8 independent concurrent probes, then consensus-first fixed reciprocal-rank fusion); inspect evidence and per-probe/aggregate completeness, and replay its opaque `continuation` only unchanged; prefer `impact_bundle(target)` with a copied `target_ref` for usages/callers/blast radius before sequential navigation.\n\
3. Use shell tools as the exception for non-code files, git/filesystem inspection, explicit live-disk verification, trivial one-offs, or ripgrep-specific flags outside `search_text`.\n\
4. Prefer Frigg core tools when repository-aware evidence, symbols, navigation, provenance, or multi-repo context matter.\n\
5. Treat semantic retrieval as optional acceleration only. When broad discovery is weak, pivot to lexical, graph, and source-witness evidence instead of diagnosing runtime state by default.\n\
6. Treat the current supported-language set as one public list: Rust, PHP, Blade, TypeScript / TSX, Python, Go, Kotlin / KTS, Java, Lua, Roc, and Nim. Describe differences in concrete capability terms, not first-class or baseline badges.\n\
7. `read_file` and `read_match` default to text-first output; request `presentation_mode=json` only when the caller truly needs the structured compatibility payload. In the extended profile, `explore(operation=zoom)` follows the same text-first default, while `probe` and `refine` stay structured.\n\
8. Use `include_follow_up_structural=true` when you want replayable `search_structural` follow-ups from `inspect_syntax_tree`, `search_structural`, or anchored navigation and outline results.\n\
9. Use `explore` only after discovery and only when the active profile includes it.\n\
10. For navigation/impact, prefer search -> copied `target_ref` -> `target`; result-match targets are session/source scoped and stable-symbol targets are repository/corpus scoped. Target scope is not authentication. Recover `TARGET_SCOPE_MISMATCH`, `STALE_TARGET_SNAPSHOT`, stale proof/handles, or `TARGET_NOT_FOUND` by rerunning the producer for a fresh target. Direct inputs remain compatible but cannot be mixed with `target`; `impact_bundle` accepts target or legacy symbol and resolves supplied targets once. Ambiguous legacy symbols run no child section. Read `sections[]` for independent execution, trust, and completeness; use section-qualified `proof_targets` with canonical `next_actions`; opt into test evidence with `include_test_mentions=true`; keep outgoing calls separate/provisional.\n\n",
    );
    text.push_str(profile_note);

    Some(
        GetPromptResult::new(vec![
            PromptMessage::new_text(Role::Assistant, text),
            PromptMessage::new_resource_link(
                Role::Assistant,
                Resource::new(SUPPORT_MATRIX_RESOURCE_URI, "FRIGG Support Matrix"),
            ),
            PromptMessage::new_resource_link(
                Role::Assistant,
                Resource::new(
                    SEMANTIC_MODELS_RESOURCE_URI,
                    "FRIGG Semantic Models Catalog",
                ),
            ),
            PromptMessage::new_resource_link(
                Role::Assistant,
                Resource::new(TOOL_SURFACE_RESOURCE_URI, "FRIGG Tool Surface Policy"),
            ),
            PromptMessage::new_resource_link(
                Role::Assistant,
                Resource::new(
                    SHELL_REPLACEMENT_MAP_RESOURCE_URI,
                    "FRIGG Shell Replacement Map",
                ),
            ),
            PromptMessage::new_resource_link(
                Role::Assistant,
                Resource::new(SHELL_GUIDANCE_RESOURCE_URI, "Shell vs Frigg Guidance"),
            ),
        ])
        .with_description("Guide shell-vs-FRIGG routing and link the relevant policy resources."),
    )
}

#[cfg(test)]
mod tests {
    use super::{
        EVIDENCE_PACKET_RESOURCE_URI, LOCAL_DEFAULT_NATIVE_DIMENSIONS, ROUTING_GUIDE_PROMPT_NAME,
        SEMANTIC_MODELS_RESOURCE_URI, SHELL_GUIDANCE_RESOURCE_URI,
        SHELL_REPLACEMENT_MAP_RESOURCE_URI, SUPPORT_MATRIX_RESOURCE_URI, TOOL_SURFACE_RESOURCE_URI,
        policy_resources, read_guidance_prompt, read_policy_resource,
    };
    use crate::languages::{LanguageSupportCapability, SymbolLanguage};
    use crate::mcp::tool_surface::ToolSurfaceProfile;
    use crate::settings::{
        DEFAULT_GOOGLE_EMBEDDING_MODEL, DEFAULT_LOCAL_EMBEDDING_MODEL,
        DEFAULT_OPENAI_COMPAT_EMBEDDING_MODEL, DEFAULT_OPENAI_EMBEDDING_MODEL,
        GEMINI_API_KEY_ENV_VAR, OPENAI_API_KEY_ENV_VAR, OPENAI_COMPAT_API_KEY_ENV_VAR,
        OPENAI_COMPAT_ENDPOINT_ENV_VAR,
    };
    use crate::storage::DEFAULT_VECTOR_DIMENSIONS;

    const _: () = assert!(LOCAL_DEFAULT_NATIVE_DIMENSIONS < DEFAULT_VECTOR_DIMENSIONS);
    use rmcp::model::ResourceContents;
    use serde_json::{Value, json};

    fn resource_text(uri: &str, profile: ToolSurfaceProfile) -> String {
        let result = read_policy_resource(uri, profile).expect("resource should exist");
        let ResourceContents::TextResourceContents { text, .. } = &result.contents[0] else {
            unreachable!("expected text resource contents");
        };
        text.clone()
    }

    #[test]
    fn semantic_models_catalog_matches_runtime_defaults_and_stays_curated() {
        assert!(
            policy_resources()
                .iter()
                .any(|resource| resource.uri == SEMANTIC_MODELS_RESOURCE_URI),
            "policy_resources should list semantic-models"
        );

        let json = resource_text(SEMANTIC_MODELS_RESOURCE_URI, ToolSurfaceProfile::Core);
        let parsed =
            serde_json::from_str::<Value>(&json).expect("semantic models JSON should parse");
        assert_eq!(
            parsed["schema_id"],
            json!("frigg.policy.semantic_models.v1")
        );
        assert_eq!(
            parsed["projection_dimensions"],
            json!(DEFAULT_VECTOR_DIMENSIONS)
        );
        assert_eq!(parsed["quality_scores"], json!("curated"));
        assert_eq!(parsed["semantic_default"]["enabled"], json!(false));
        assert_eq!(parsed["reindex_on_change"], json!(true));

        let models = parsed["models"].as_array().expect("models array").to_vec();
        assert_eq!(
            models.len(),
            4,
            "curated defaults + openai_compat protocol — not a brand model zoo"
        );
        let models_by_id: std::collections::BTreeMap<&str, &Value> = models
            .iter()
            .filter_map(|row| row["id"].as_str().map(|id| (id, row)))
            .collect();

        let local = models
            .iter()
            .find(|row| row["provider"] == "local")
            .expect("local row");
        assert_eq!(local["model"], json!(DEFAULT_LOCAL_EMBEDDING_MODEL));
        assert_eq!(local["role"], json!("default"));
        assert_eq!(local["offline"], json!(true));
        assert_eq!(
            local["native_dimensions"],
            json!(LOCAL_DEFAULT_NATIVE_DIMENSIONS),
            "local native_dimensions must be REAL MiniLM width (384), not padded 1536"
        );
        assert_eq!(local["pad_to_projection"], json!(true));
        assert!(local["credential_env"].is_null());
        assert_eq!(local["quality"], json!("curated"));
        assert_eq!(
            local["quality_tier"],
            json!("offline_smoke"),
            "MiniLM is offline smoke / zero-key general embedder"
        );
        let local_limits = local["known_limits"]
            .as_array()
            .expect("local known_limits");
        assert!(
            local_limits.iter().any(|limit| {
                limit.as_str().is_some_and(|text| {
                    text.contains("Offline smoke") || text.contains("not a code-specialized")
                })
            }),
            "local known_limits must state MiniLM offline-smoke / general-embedder role"
        );
        assert!(
            local.get("dimensions").is_none() || local["dimensions"] == local["native_dimensions"]
        );
        assert!(local.get("stored_dimensions").is_none());

        let openai = models
            .iter()
            .find(|row| row["provider"] == "openai")
            .expect("openai row");
        assert_eq!(openai["model"], json!(DEFAULT_OPENAI_EMBEDDING_MODEL));
        assert_eq!(openai["credential_env"], json!(OPENAI_API_KEY_ENV_VAR));
        assert_eq!(
            openai["native_dimensions"],
            json!(DEFAULT_VECTOR_DIMENSIONS)
        );
        assert_eq!(
            openai["pad_to_projection"],
            json!(false),
            "OpenAI default matches projection; no pad"
        );
        assert_eq!(openai["offline"], json!(false));
        assert_eq!(openai["quality"], json!("curated"));

        let google = models
            .iter()
            .find(|row| row["provider"] == "google")
            .expect("google row");
        assert_eq!(google["model"], json!(DEFAULT_GOOGLE_EMBEDDING_MODEL));
        assert_eq!(google["credential_env"], json!(GEMINI_API_KEY_ENV_VAR));
        assert_eq!(
            google["native_dimensions"],
            json!(DEFAULT_VECTOR_DIMENSIONS),
            "Google native_dimensions is Frigg-requested REAL width, not a padded value"
        );
        assert_eq!(
            google["pad_to_projection"],
            json!(false),
            "Google path requests full projection width; storage pad unused on happy path"
        );
        assert_eq!(google["quality"], json!("curated"));
        assert_eq!(
            google["quality_tier"],
            json!("credential_peer"),
            "Gemini is credential ecosystem peer, not Frigg preferred cloud default"
        );
        assert!(
            google["recommended_when"]
                .as_str()
                .is_some_and(|text| text.contains("GEMINI_API_KEY")),
            "google recommended_when must mention GEMINI_API_KEY bring-your-key"
        );
        let google_limits = google["known_limits"]
            .as_array()
            .expect("google known_limits");
        assert!(
            google_limits.iter().any(|limit| {
                limit.as_str().is_some_and(|text| {
                    text.contains("Credential-ecosystem peer") || text.contains("credential peer")
                })
            }),
            "google known_limits must state credential-peer positioning"
        );
        assert_eq!(openai["quality_tier"], json!("cloud"));

        let openai_compat = models
            .iter()
            .find(|row| row["provider"] == "openai_compat")
            .expect("openai_compat row");
        assert_eq!(
            openai_compat["model"],
            json!(DEFAULT_OPENAI_COMPAT_EMBEDDING_MODEL)
        );
        assert_eq!(
            openai_compat["credential_env"],
            json!(OPENAI_COMPAT_API_KEY_ENV_VAR)
        );
        assert_eq!(
            openai_compat["endpoint_env"],
            json!(OPENAI_COMPAT_ENDPOINT_ENV_VAR)
        );
        assert_eq!(openai_compat["role"], json!("experimental"));
        assert_eq!(openai_compat["quality"], json!("curated"));

        for row in &models {
            assert!(row.get("score").is_none());
            assert!(row.get("benchmark_score").is_none());
            assert!(row.get("leaderboard_rank").is_none());
            let native = row["native_dimensions"]
                .as_u64()
                .expect("native_dimensions");
            let pad = row["pad_to_projection"]
                .as_bool()
                .expect("pad_to_projection");
            if pad {
                assert!(
                    (native as usize) < DEFAULT_VECTOR_DIMENSIONS,
                    "when pad_to_projection, native_dimensions must be REAL pre-pad width < projection"
                );
            }
            assert!(
                row.get("stored_dimensions").is_none(),
                "do not report padded store length on model rows"
            );
        }
        assert_eq!(
            parsed["dimensions_contract"]["model_field"],
            json!("native_dimensions")
        );

        let presets = parsed["presets"]
            .as_array()
            .expect("presets array (EXP-code-presets C)");
        assert_eq!(
            presets.len(),
            4,
            "soft presets over existing models + openai_compat self-host — not a brand zoo"
        );
        assert!(
            parsed["presets_note"]
                .as_str()
                .is_some_and(|note| note.contains("CLI") && note.contains("deferred")),
            "presets_note should state CLI aliases deferred"
        );

        let expected_presets = [
            (
                "offline-small",
                "local",
                DEFAULT_LOCAL_EMBEDDING_MODEL,
                "local-minilm-l6-v2",
            ),
            (
                "cloud-openai",
                "openai",
                DEFAULT_OPENAI_EMBEDDING_MODEL,
                "openai-text-embedding-3-small",
            ),
            (
                "cloud-google",
                "google",
                DEFAULT_GOOGLE_EMBEDDING_MODEL,
                "google-gemini-embedding-001",
            ),
            (
                "openai-compat-selfhost",
                "openai_compat",
                DEFAULT_OPENAI_COMPAT_EMBEDDING_MODEL,
                "openai-compat-protocol",
            ),
        ];
        for (id, provider, model, model_id) in expected_presets {
            let preset = presets
                .iter()
                .find(|row| row["id"] == id)
                .expect("expected semantic preset must exist");
            assert_eq!(preset["provider"], json!(provider));
            assert_eq!(preset["model"], json!(model));
            assert_eq!(preset["model_id"], json!(model_id));
            assert_eq!(preset["quality"], json!("curated"));
            assert_eq!(
                preset["cli_alias"],
                json!(false),
                "preset {id} is documentation only — not a CLI flag (B deferred)"
            );
            assert_eq!(preset["storage_keys"]["provider"], json!(provider));
            assert_eq!(preset["storage_keys"]["model"], json!(model));
            assert_eq!(
                preset["expands_to"]["FRIGG_SEMANTIC_RUNTIME_PROVIDER"],
                json!(provider)
            );
            assert_eq!(
                preset["expands_to"]["FRIGG_SEMANTIC_RUNTIME_MODEL"],
                json!(model)
            );
            assert_eq!(
                preset["expands_to"]["FRIGG_SEMANTIC_RUNTIME_ENABLED"],
                json!("true")
            );
            assert!(
                preset["expands_to"].get(OPENAI_API_KEY_ENV_VAR).is_none()
                    && preset["expands_to"].get(GEMINI_API_KEY_ENV_VAR).is_none(),
                "preset {id} must not put credential values in expands_to"
            );
            let resolved = models_by_id
                .get(model_id)
                .expect("semantic preset model_id must resolve to models[]");
            assert_eq!(
                resolved["provider"],
                json!(provider),
                "preset {id} provider must match models[]"
            );
            assert_eq!(
                resolved["model"],
                json!(model),
                "preset {id} model must match models[]"
            );
            assert_eq!(
                preset["required_credential_env"], resolved["credential_env"],
                "preset {id} credential env must match models[]"
            );
            assert!(
                preset["failure_modes"]
                    .as_array()
                    .is_some_and(|modes| !modes.is_empty()),
                "preset {id} needs failure_modes"
            );
            assert!(preset.get("score").is_none());
            assert!(preset.get("benchmark_score").is_none());
        }
        let offline = presets
            .iter()
            .find(|row| row["id"] == "offline-small")
            .expect("offline-small");
        assert!(offline["required_credential_env"].is_null());
        assert_eq!(
            offline["quality_tier"],
            json!("offline_smoke"),
            "offline-small preset must carry MiniLM offline_smoke tier"
        );
        let cloud_google = presets
            .iter()
            .find(|row| row["id"] == "cloud-google")
            .expect("cloud-google");
        assert_eq!(
            cloud_google["quality_tier"],
            json!("credential_peer"),
            "cloud-google preset must carry Gemini credential_peer tier"
        );
        assert_eq!(
            cloud_google["required_credential_env"],
            json!(GEMINI_API_KEY_ENV_VAR)
        );

        #[cfg(feature = "local-embeddings")]
        {
            use crate::embeddings::local_model::DEFAULT_LOCAL_MODEL_ALIAS;
            assert_eq!(
                LOCAL_DEFAULT_NATIVE_DIMENSIONS, DEFAULT_LOCAL_MODEL_ALIAS.dimensions,
                "scoreboard local dims must match DEFAULT_LOCAL_MODEL_ALIAS"
            );
            assert_eq!(
                DEFAULT_LOCAL_EMBEDDING_MODEL,
                DEFAULT_LOCAL_MODEL_ALIAS.semantic_model
            );
        }
    }

    #[test]
    fn support_matrix_lists_supported_languages_without_rollout_tiers() {
        let json = resource_text(SUPPORT_MATRIX_RESOURCE_URI, ToolSurfaceProfile::Core);
        let parsed =
            serde_json::from_str::<Value>(&json).expect("support matrix JSON should parse");
        assert!(parsed.get("next_language_priority").is_none());
        assert!(parsed.get("language_rollout_policy").is_none());
        assert_eq!(parsed["schema_id"], json!("frigg.policy.support_matrix.v4"));
        assert_eq!(
            parsed["capability_tiers"]["core"].as_str(),
            Some("capability is part of FRIGG's stable read-only core contract for that language")
        );
        assert_eq!(
            parsed["capability_tiers"]["optional_accelerator"].as_str(),
            Some(
                "capability is an optional accelerator that only contributes when runtime configuration and repository state make it available"
            )
        );
        for language_id in [
            "rust",
            "php",
            "blade",
            "typescript_tsx",
            "python",
            "go",
            "kotlin",
            "java",
            "lua",
            "roc",
            "nim",
        ] {
            assert!(
                parsed["languages"]
                    .as_array()
                    .expect("languages should be an array")
                    .iter()
                    .any(|entry| entry["id"] == json!(language_id)),
                "expected {language_id} to be listed as supported"
            );
        }
        assert!(
            parsed["languages"]
                .as_array()
                .expect("languages should be an array")
                .iter()
                .any(|entry| {
                    entry["id"] == json!("blade")
                        && entry["capability_note"] == json!("template_metadata_livewire_flux")
                })
        );
        assert_eq!(
            parsed["languages"]
                .as_array()
                .expect("languages should be an array")
                .iter()
                .find(|entry| entry["id"] == json!("typescript_tsx"))
                .and_then(|entry| entry.get("capabilities"))
                .and_then(|value| value.get("semantic_chunking"))
                .and_then(|value| value.as_str()),
            Some("unsupported")
        );
        assert_eq!(
            parsed["languages"]
                .as_array()
                .expect("languages should be an array")
                .iter()
                .find(|entry| entry["id"] == json!("rust"))
                .and_then(|entry| entry.get("capabilities"))
                .and_then(|value| value.get("precise_artifact_assist"))
                .and_then(|value| value.as_str()),
            Some("optional_accelerator")
        );
    }

    #[test]
    fn support_matrix_capabilities_match_language_registry() {
        let json = resource_text(SUPPORT_MATRIX_RESOURCE_URI, ToolSurfaceProfile::Core);
        let parsed =
            serde_json::from_str::<Value>(&json).expect("support matrix JSON should parse");
        let languages = parsed["languages"]
            .as_array()
            .expect("languages should be an array");

        for language in SymbolLanguage::ALL {
            let expected_id = if matches!(language, SymbolLanguage::TypeScript) {
                "typescript_tsx"
            } else {
                language.as_str()
            };
            let entry = languages
                .iter()
                .find(|entry| entry["id"] == json!(expected_id))
                .unwrap_or_else(|| unreachable!("expected {expected_id} to be listed"));
            for capability in LanguageSupportCapability::ALL {
                let expected = language.capability_tier(capability).as_str();
                assert_eq!(
                    entry["capabilities"][capability.as_str()].as_str(),
                    Some(expected),
                    "expected {expected_id} capability {} to match the registry",
                    capability.as_str()
                );
            }
        }
    }

    #[test]
    fn support_matrix_advanced_consumers_follow_extended_tool_surface_manifest() {
        let json = resource_text(SUPPORT_MATRIX_RESOURCE_URI, ToolSurfaceProfile::Core);
        let parsed =
            serde_json::from_str::<Value>(&json).expect("support matrix JSON should parse");
        let advanced_consumers = parsed["advanced_consumers"]
            .as_array()
            .expect("advanced_consumers should be an array");
        let core =
            crate::mcp::tool_surface::manifest_for_tool_surface_profile(ToolSurfaceProfile::Core);
        let extended = crate::mcp::tool_surface::manifest_for_tool_surface_profile(
            ToolSurfaceProfile::Extended,
        );

        for tool_name in extended
            .tool_names
            .iter()
            .filter(|tool_name| !core.tool_names.contains(tool_name))
        {
            assert!(
                advanced_consumers
                    .iter()
                    .any(|entry| entry.as_str() == Some(tool_name.as_str())),
                "expected advanced_consumers to include extended-only tool {tool_name}"
            );
        }
        assert!(
            !advanced_consumers
                .iter()
                .any(|entry| entry.as_str() == Some("search_text")),
            "stable-core tools must not leak into advanced_consumers"
        );
        assert!(
            advanced_consumers
                .iter()
                .any(|entry| entry.as_str() == Some("self_improvement_loop")),
            "support matrix should keep non-tool advanced consumers explicit"
        );
    }

    #[test]
    fn tool_surface_policy_lists_explore_on_core_not_extended_only() {
        let json = resource_text(TOOL_SURFACE_RESOURCE_URI, ToolSurfaceProfile::Extended);
        let parsed =
            serde_json::from_str::<Value>(&json).expect("tool surface policy JSON should parse");
        assert!(
            parsed["core_tools"]
                .as_array()
                .expect("core_tools should be an array")
                .iter()
                .any(|entry| entry == "explore"),
            "explore is product tooling and belongs on core"
        );
        assert!(
            !parsed["extended_only_tools"]
                .as_array()
                .expect("extended_only_tools should be an array")
                .iter()
                .any(|entry| entry == "explore"),
            "explore must not be extended-only"
        );
    }

    #[test]
    fn tool_surface_policy_matches_profile_manifests() {
        let json = resource_text(TOOL_SURFACE_RESOURCE_URI, ToolSurfaceProfile::Extended);
        let parsed =
            serde_json::from_str::<Value>(&json).expect("tool surface policy JSON should parse");
        let core =
            crate::mcp::tool_surface::manifest_for_tool_surface_profile(ToolSurfaceProfile::Core);
        let extended = crate::mcp::tool_surface::manifest_for_tool_surface_profile(
            ToolSurfaceProfile::Extended,
        );
        let expected_extended_only = extended
            .tool_names
            .iter()
            .filter(|tool_name| !core.tool_names.contains(tool_name))
            .cloned()
            .map(Value::String)
            .collect::<Vec<_>>();

        assert_eq!(
            parsed["default_profile"].as_str(),
            Some(ToolSurfaceProfile::Extended.as_str())
        );
        assert_eq!(parsed["live"], json!(true));
        assert_eq!(
            parsed["source_of_truth"]["public_tool_names"].as_str(),
            Some("crates/cli/src/mcp/types.rs::PUBLIC_TOOL_NAMES")
        );
        assert!(
            parsed["not_authoritative"]
                .as_array()
                .is_some_and(|rows| !rows.is_empty()),
            "tool-surface.json should mark inventory freezes as non-authoritative"
        );
        assert_eq!(
            parsed["core_tools"].as_array(),
            Some(
                &core
                    .tool_names
                    .iter()
                    .cloned()
                    .map(Value::String)
                    .collect::<Vec<_>>()
            )
        );
        assert_eq!(
            parsed["extended_only_tools"].as_array(),
            Some(&expected_extended_only)
        );
        assert_eq!(
            parsed["active_tools"].as_array(),
            Some(
                &extended
                    .tool_names
                    .iter()
                    .cloned()
                    .map(Value::String)
                    .collect::<Vec<_>>()
            ),
            "active_tools must match the active profile manifest (live SSOT)"
        );
    }

    #[test]
    fn tool_surface_active_tools_follow_core_profile() {
        let json = resource_text(TOOL_SURFACE_RESOURCE_URI, ToolSurfaceProfile::Core);
        let parsed =
            serde_json::from_str::<Value>(&json).expect("tool surface policy JSON should parse");
        let core =
            crate::mcp::tool_surface::manifest_for_tool_surface_profile(ToolSurfaceProfile::Core);
        assert_eq!(parsed["active_profile"].as_str(), Some("core"));
        assert_eq!(
            parsed["active_tools"].as_array(),
            Some(
                &core
                    .tool_names
                    .iter()
                    .cloned()
                    .map(Value::String)
                    .collect::<Vec<_>>()
            )
        );
        assert!(
            parsed["active_tools"]
                .as_array()
                .expect("active_tools")
                .iter()
                .any(|entry| entry == "explore"),
            "core active_tools must include product explore tool"
        );
        assert!(
            !parsed["active_tools"]
                .as_array()
                .expect("active_tools")
                .iter()
                .any(|entry| entry.as_str().is_some_and(|s| s.starts_with("playbook_"))),
            "core active_tools must omit playbook tools"
        );
    }

    #[test]
    fn evidence_packet_policy_is_schema_only_not_a_tool() {
        let json = resource_text(EVIDENCE_PACKET_RESOURCE_URI, ToolSurfaceProfile::Core);
        let parsed =
            serde_json::from_str::<Value>(&json).expect("evidence packet policy should parse");
        assert_eq!(
            parsed["schema_id"],
            json!("frigg.policy.evidence_packet.v1")
        );
        assert_eq!(parsed["not_a_tool"], json!(true));
        assert_eq!(parsed["product_role"], json!("skill_composition_template"));
        for field in [
            "claim",
            "tool",
            "path",
            "start_line",
            "end_line",
            "match_id",
            "result_handle",
        ] {
            assert!(
                parsed["claim_fields"][field].is_object(),
                "claim_fields.{field} must be documented"
            );
        }
        assert!(
            parsed["envelope"]["claims"].is_object(),
            "envelope.claims must be documented"
        );
        let example = parsed
            .get("example")
            .cloned()
            .expect("policy must include example packet");
        let packet: crate::mcp::types::EvidencePacket = serde_json::from_value(example)
            .expect("policy example must deserialize as EvidencePacket");
        assert!(!packet.claims.is_empty());
    }

    #[test]
    fn shell_replacement_map_is_machine_readable() {
        let json = resource_text(SHELL_REPLACEMENT_MAP_RESOURCE_URI, ToolSurfaceProfile::Core);
        let parsed =
            serde_json::from_str::<Value>(&json).expect("shell replacement map JSON should parse");
        assert_eq!(
            parsed["schema_id"],
            json!("frigg.policy.shell_replacement_map.v1")
        );
        assert!(
            parsed["replacements"]
                .as_array()
                .expect("replacements should be an array")
                .iter()
                .any(|entry| {
                    entry["shell"] == json!("sed -n '10,80p' path")
                        && entry["tool"] == json!("read_file")
                        && entry["params"]
                            .as_array()
                            .expect("params should be an array")
                            .iter()
                            .any(|param| param == "start_line")
                        && entry["params"]
                            .as_array()
                            .expect("params should be an array")
                            .iter()
                            .any(|param| param == "line_count")
                })
        );
        assert!(
            parsed["replacements"]
                .as_array()
                .expect("replacements should be an array")
                .iter()
                .any(|entry| {
                    entry["shell"] == json!("rg -l PATTERN")
                        && entry["tool"] == json!("search_text")
                        && entry["params"]
                            .as_array()
                            .expect("params should be an array")
                            .iter()
                            .any(|param| param == "files_with_matches=true")
                })
        );
        assert!(
            parsed["replacements"]
                .as_array()
                .expect("replacements should be an array")
                .iter()
                .any(|entry| entry["tool"] == json!("search_batch")),
            "shell map should list search_batch for multi-hypothesis work"
        );
        assert!(
            parsed["replacements"]
                .as_array()
                .expect("replacements should be an array")
                .iter()
                .any(|entry| entry["tool"] == json!("impact_bundle")),
            "shell map should list impact_bundle for usages/callers"
        );
    }

    #[test]
    fn guidance_surfaces_mention_search_batch_and_impact_bundle() {
        let shell_guidance = resource_text(SHELL_GUIDANCE_RESOURCE_URI, ToolSurfaceProfile::Core);
        let tool_surface = resource_text(TOOL_SURFACE_RESOURCE_URI, ToolSurfaceProfile::Core);
        let shell_map = resource_text(SHELL_REPLACEMENT_MAP_RESOURCE_URI, ToolSurfaceProfile::Core);
        let prompt = read_guidance_prompt(
            ROUTING_GUIDE_PROMPT_NAME,
            None,
            ToolSurfaceProfile::Extended,
        )
        .expect("routing prompt should exist");
        let prompt_text = format!("{prompt:?}");

        for surface in [&shell_guidance, &tool_surface, &shell_map, &prompt_text] {
            assert!(
                surface.contains("search_batch"),
                "guidance surface missing search_batch: {surface:.200}"
            );
            assert!(
                surface.contains("impact_bundle"),
                "guidance surface missing impact_bundle: {surface:.200}"
            );
        }
        assert!(
            shell_guidance.contains("independent concurrent"),
            "shell guidance should describe batch as independent concurrent probes"
        );
    }

    #[test]
    fn routing_prompt_links_policy_resources() {
        let prompt = read_guidance_prompt(
            ROUTING_GUIDE_PROMPT_NAME,
            Some(&serde_json::Map::from_iter([(
                "task".to_owned(),
                json!("where is runtime state wired"),
            )])),
            ToolSurfaceProfile::Extended,
        )
        .expect("routing prompt should exist");
        assert_eq!(prompt.messages.len(), 6);
    }

    #[test]
    fn agent_directive_core_sentences_are_in_guidance_surfaces() {
        let shell_guidance = resource_text(SHELL_GUIDANCE_RESOURCE_URI, ToolSurfaceProfile::Core);
        let tool_surface = resource_text(TOOL_SURFACE_RESOURCE_URI, ToolSurfaceProfile::Core);
        let prompt = read_guidance_prompt(
            ROUTING_GUIDE_PROMPT_NAME,
            Some(&serde_json::Map::from_iter([(
                "task".to_owned(),
                json!("find every caller of load_config"),
            )])),
            ToolSurfaceProfile::Extended,
        )
        .expect("routing prompt should exist");
        let prompt_debug = format!("{prompt:?}");

        assert!(shell_guidance.contains(
            "Use Frigg as the default for code discovery, file listing, navigation, exact code search, and bounded source reads."
        ));
        assert!(tool_surface.contains(
            "Use Frigg as the default for code discovery, file listing, navigation, exact code search, and bounded source reads."
        ));
        assert!(
            shell_guidance.contains(
                "Use `search_hybrid` only for broad discovery-style repository questions"
            )
        );
        assert!(
            tool_surface
                .contains("Use search_hybrid only for broad discovery-style repository questions")
        );
        assert!(
            prompt_debug.contains(
                "Use `search_hybrid` only for broad discovery-style repository questions"
            )
        );
        assert!(prompt_debug.contains(
            "Prefer Frigg for code discovery, navigation, exact code search, and bounded source reads."
        ));
        assert!(shell_guidance.contains(
            "Use shell tools only as the exception for git state and diffs, non-code files, build/test output, generated or unindexed files, explicit live-disk verification, and unavailable Frigg results."
        ));
        assert!(tool_surface.contains(
            "Use shell tools as the exception for git state and diffs, non-code files, build/test output, generated or unindexed files, explicit live-disk verification, and unavailable Frigg results."
        ));
        assert!(prompt_debug.contains(
            "Use shell tools as the exception for non-code files, git/filesystem inspection, explicit live-disk verification, trivial one-offs, or ripgrep-specific flags outside `search_text`."
        ));
    }
}