symforge 8.16.5

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
//! Read-oriented MCP tool input types and pure request parsing helpers.

use schemars::{JsonSchema, Schema};
use serde::{Deserialize, Deserializer, Serialize};

use crate::live_index::search;

/// Deserialize a `u32` from either a JSON number or a stringified number like `"5"`.
pub(crate) fn lenient_u32<'de, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<Option<u32>, D::Error> {
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum NumOrStr {
        Num(u32),
        Str(String),
        Null,
    }
    match NumOrStr::deserialize(deserializer)? {
        NumOrStr::Num(n) => Ok(Some(n)),
        NumOrStr::Str(s) if s.is_empty() => Ok(None),
        NumOrStr::Str(s) => s.parse::<u32>().map(Some).map_err(serde::de::Error::custom),
        NumOrStr::Null => Ok(None),
    }
}

/// Deserialize a `bool` from either a JSON boolean or a stringified boolean like `"true"`.
pub(crate) fn lenient_bool<'de, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<Option<bool>, D::Error> {
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum BoolOrStr {
        Bool(bool),
        Str(String),
        Null,
    }
    match BoolOrStr::deserialize(deserializer)? {
        BoolOrStr::Bool(b) => Ok(Some(b)),
        BoolOrStr::Str(s) => match s.as_str() {
            "true" | "1" => Ok(Some(true)),
            "false" | "0" => Ok(Some(false)),
            "" => Ok(None),
            _ => Err(serde::de::Error::custom(format!(
                "expected boolean or \"true\"/\"false\", got \"{s}\""
            ))),
        },
        BoolOrStr::Null => Ok(None),
    }
}

pub(crate) fn lenient_bool_required<'de, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<bool, D::Error> {
    lenient_bool(deserializer).map(|value| value.unwrap_or(false))
}

const INCLUDE_TESTS_SECTION_MARKER: &str = "__symforge_include_tests";

pub(crate) fn encode_include_tests_marker(
    mut sections: Option<Vec<String>>,
    include_tests: bool,
) -> Option<Vec<String>> {
    if include_tests {
        sections
            .get_or_insert_with(Vec::new)
            .push(INCLUDE_TESTS_SECTION_MARKER.to_string());
    }
    sections
}

pub(crate) fn include_tests_from_sections(sections: Option<&Vec<String>>) -> bool {
    sections
        .map(|items| {
            items
                .iter()
                .any(|section| section == INCLUDE_TESTS_SECTION_MARKER)
        })
        .unwrap_or(false)
}

pub(crate) fn visible_sections(sections: &Option<Vec<String>>) -> Option<Vec<String>> {
    sections.as_ref().and_then(|items| {
        let had_marker = items
            .iter()
            .any(|section| section.as_str() == INCLUDE_TESTS_SECTION_MARKER);
        let visible = items
            .iter()
            .filter(|section| section.as_str() != INCLUDE_TESTS_SECTION_MARKER)
            .cloned()
            .collect::<Vec<_>>();
        if visible.is_empty() && had_marker {
            None
        } else {
            Some(visible)
        }
    })
}

fn add_include_tests_schema(schema: &mut Schema) {
    if let Some(serde_json::Value::Object(properties)) = schema.get_mut("properties") {
        properties.insert(
            "include_tests".to_string(),
            serde_json::json!({
                "type": "boolean",
                "default": false,
                "description": "Include expanded test modules in context output."
            }),
        );
    }
}

pub(crate) fn lenient_u32_required<'de, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<u32, D::Error> {
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum NumOrStr {
        Num(u32),
        Str(String),
    }
    match NumOrStr::deserialize(deserializer)? {
        NumOrStr::Num(n) => Ok(n),
        NumOrStr::Str(s) => s.parse::<u32>().map_err(serde::de::Error::custom),
    }
}

/// Deserialize a `u64` from either a JSON number or a stringified number.
pub(crate) fn lenient_u64<'de, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<Option<u64>, D::Error> {
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum NumOrStr {
        Num(u64),
        Str(String),
        Null,
    }
    match NumOrStr::deserialize(deserializer)? {
        NumOrStr::Num(n) => Ok(Some(n)),
        NumOrStr::Str(s) if s.is_empty() => Ok(None),
        NumOrStr::Str(s) => s.parse::<u64>().map(Some).map_err(serde::de::Error::custom),
        NumOrStr::Null => Ok(None),
    }
}

/// Deserialize an `i64` from either a JSON number or a stringified number.
pub(crate) fn lenient_i64<'de, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<Option<i64>, D::Error> {
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum NumOrStr {
        Num(i64),
        Str(String),
        Null,
    }
    match NumOrStr::deserialize(deserializer)? {
        NumOrStr::Num(n) => Ok(Some(n)),
        NumOrStr::Str(s) if s.is_empty() => Ok(None),
        NumOrStr::Str(s) => s.parse::<i64>().map(Some).map_err(serde::de::Error::custom),
        NumOrStr::Null => Ok(None),
    }
}

/// Leniently deserialize an `Option<Vec<T>>` — accepts a native JSON array, a
/// stringified JSON array (as sent by some MCP clients like Kilo Code), or a
/// native array of stringified JSON objects (as sent by Codex).
pub(crate) fn lenient_option_vec<'de, D, T>(deserializer: D) -> Result<Option<Vec<T>>, D::Error>
where
    D: Deserializer<'de>,
    T: serde::de::DeserializeOwned,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum VecOrStr {
        Vec(Vec<serde_json::Value>),
        Str(String),
        Null,
    }
    match VecOrStr::deserialize(deserializer)? {
        VecOrStr::Vec(values) => {
            let result: Result<Vec<T>, _> = values
                .into_iter()
                .enumerate()
                .map(|(i, v)| match serde_json::from_value::<T>(v.clone()) {
                    Ok(item) => Ok(item),
                    Err(direct_err) => {
                        if let serde_json::Value::String(ref s) = v {
                            serde_json::from_str::<T>(s).map_err(|_| {
                                serde::de::Error::custom(format!("element {i}: {direct_err}"))
                            })
                        } else {
                            Err(serde::de::Error::custom(format!(
                                "element {i}: {direct_err}"
                            )))
                        }
                    }
                })
                .collect();
            result.map(Some)
        }
        VecOrStr::Str(s) if s.is_empty() || s == "null" => Ok(None),
        VecOrStr::Str(s) => serde_json::from_str::<Vec<T>>(&s)
            .map(Some)
            .map_err(serde::de::Error::custom),
        VecOrStr::Null => Ok(None),
    }
}

/// Leniently deserialize a `Vec<T>` — accepts a native JSON array, a stringified
/// JSON array (as sent by some MCP clients like Kilo Code), or a native array of
/// stringified JSON objects (as sent by Codex, e.g. `["{...}", "{...}"]`).
pub(crate) fn lenient_vec_required<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
    D: Deserializer<'de>,
    T: serde::de::DeserializeOwned,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum VecOrStr {
        /// Native JSON array — try direct deserialization first via raw Value.
        Vec(Vec<serde_json::Value>),
        /// Entire array serialized as a single JSON string.
        Str(String),
    }
    match VecOrStr::deserialize(deserializer)? {
        VecOrStr::Vec(values) => {
            // Try deserializing each element. If an element is a JSON string
            // that looks like a JSON object, parse the inner string first.
            values
                .into_iter()
                .enumerate()
                .map(|(i, v)| {
                    // First try direct deserialization (normal case: native objects)
                    match serde_json::from_value::<T>(v.clone()) {
                        Ok(item) => Ok(item),
                        Err(direct_err) => {
                            // If the value is a string, try parsing it as JSON
                            if let serde_json::Value::String(ref s) = v {
                                serde_json::from_str::<T>(s).map_err(|_| {
                                    serde::de::Error::custom(format!("element {i}: {direct_err}"))
                                })
                            } else {
                                Err(serde::de::Error::custom(format!(
                                    "element {i}: {direct_err}"
                                )))
                            }
                        }
                    }
                })
                .collect()
        }
        VecOrStr::Str(s) => serde_json::from_str::<Vec<T>>(&s).map_err(serde::de::Error::custom),
    }
}

/// Input for `get_symbol`.
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct GetSymbolInput {
    /// Optional explicit project selector (daemon sessions with multiple open
    /// projects): an open project ID or unique project name. Omit for the
    /// session's home project. Local/embedded servers are bound to one project
    /// and refuse a non-matching selector.
    #[serde(default)]
    pub project: Option<String>,
    /// Relative path to the file. OPTIONAL: when omitted, the symbol is resolved
    /// by an exact `name` search across the index (pass `symbol_line`/`kind` to
    /// disambiguate a common name). Provide `path` to skip the search and go
    /// straight to that file. Ignored when `targets` is provided.
    #[serde(default)]
    pub path: String,
    /// Symbol name to look up (required for single lookup; ignored when `targets` is provided).
    #[serde(default)]
    pub name: String,
    /// Optional kind filter: "fn", "struct", "enum", "impl", etc.
    pub kind: Option<String>,
    /// Disambiguate when multiple symbols share the same name. Pass the 1-based start line of the
    /// desired symbol (shown in ambiguity errors and search_symbols output).
    #[serde(default, deserialize_with = "lenient_u32")]
    pub symbol_line: Option<u32>,
    /// Optional batch mode: provide multiple targets to retrieve 2+ symbols or code slices in one call.
    /// Each target is a file path + symbol name or byte range. When provided, path/name/kind above are ignored.
    #[serde(default, deserialize_with = "lenient_option_vec")]
    #[schemars(with = "Vec<SymbolTarget>")]
    pub targets: Option<Vec<SymbolTarget>>,
    /// When true, return an approximate token cost estimate instead of actual content.
    /// Useful for budget planning before fetching large symbols.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub estimate: Option<bool>,
    /// Optional maximum token budget for the response (~1000 default when unset).
    #[serde(default)]
    pub max_tokens: Option<u64>,
    /// When true, bypass session cache-hit and return a fresh payload.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub force_refresh: Option<bool>,
}

/// A single target in a `get_symbols` batch request.
///
/// Either provide `name` (symbol lookup) or `start_byte`/`end_byte` (code slice).
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct SymbolTarget {
    /// Relative file path.
    pub path: String,
    /// Symbol name for symbol lookup (mutually exclusive with byte range).
    pub name: Option<String>,
    /// Kind filter for symbol lookup (e.g., "fn", "struct").
    pub kind: Option<String>,
    /// Disambiguate when multiple symbols share the same name. Pass the 1-based start line.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub symbol_line: Option<u32>,
    /// Start byte offset for code slice (mutually exclusive with name).
    #[serde(default, deserialize_with = "lenient_u32")]
    pub start_byte: Option<u32>,
    /// End byte offset for code slice (inclusive).
    #[serde(default, deserialize_with = "lenient_u32")]
    pub end_byte: Option<u32>,
}

/// Input for `get_file_content`.
#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetFileContentInput {
    /// Optional explicit project selector (daemon sessions with multiple open
    /// projects): an open project ID or unique project name. Omit for the
    /// session's home project. Local/embedded servers are bound to one project
    /// and refuse a non-matching selector.
    #[serde(default)]
    pub project: Option<String>,
    /// Relative path to the file.
    pub path: String,
    /// Selection mode: `lines`, `symbol`, `match`, `chunk`.
    /// When set, only flags valid for that mode are accepted; cross-mode flags error.
    /// When omitted, mode is inferred from flags (backward compatible).
    #[serde(default)]
    pub mode: Option<String>,
    /// First line to include (1-indexed).
    #[serde(default, deserialize_with = "lenient_u32")]
    pub start_line: Option<u32>,
    /// Last line to include (1-indexed, inclusive).
    #[serde(default, deserialize_with = "lenient_u32")]
    pub end_line: Option<u32>,
    /// Select a 1-based chunk from the file using `max_lines` as the chunk size.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub chunk_index: Option<u32>,
    /// Maximum number of lines to include in a chunked read.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub max_lines: Option<u32>,
    /// Center the read around this 1-indexed line.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub around_line: Option<u32>,
    /// Center the read around the first case-insensitive literal match in the file.
    pub around_match: Option<String>,
    /// Select a specific 1-based occurrence of `around_match` instead of the first match.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub match_occurrence: Option<u32>,
    /// Center the read around a symbol in the target file.
    pub around_symbol: Option<String>,
    /// Optional exact-selector line for `around_symbol`.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub symbol_line: Option<u32>,
    /// Number of lines of symmetric context to include around `around_line` or `around_match`.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub context_lines: Option<u32>,
    /// Show 1-indexed line numbers for ordinary full-file or explicit-range reads.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub show_line_numbers: Option<bool>,
    /// Prepend a stable path or path-plus-range header for ordinary full-file or explicit-range reads.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub header: Option<bool>,
    /// When true, return an approximate token count for the file instead of content.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub estimate: Option<bool>,
    /// Alias for `start_line` using the Read-tool idiom: 0-based line count to skip.
    /// Translated to `start_line = offset + 1` before processing.
    /// Cannot be combined with `start_line`, `end_line`, `around_line`, `around_match`,
    /// `around_symbol`, `chunk_index`, or `mode`.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub offset: Option<u32>,
    /// Alias for `end_line` using the Read-tool idiom: number of lines to include.
    /// Translated to `end_line = offset + limit` before processing.
    /// Cannot be combined with the fields listed under `offset`.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub limit: Option<u32>,
    /// Optional maximum token budget for the response.
    #[serde(default)]
    pub max_tokens: Option<u64>,
    /// When true, bypass session cache-hit and return a fresh payload.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub force_refresh: Option<bool>,
}

/// Input for `symforge_retrieve` (CCR full output recovery, 011).
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct SymforgeRetrieveInput {
    /// 12-character hex handle from a CCR footer.
    pub hash: String,
}

#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub(crate) struct ValidateFileSyntaxInput {
    /// Optional explicit project selector (daemon sessions with multiple open
    /// projects): an open project ID or unique project name. Omit for the
    /// session's home project. Local/embedded servers are bound to one project
    /// and refuse a non-matching selector.
    #[serde(default)]
    pub project: Option<String>,
    pub path: String,
}

/// Input for `find_dependents`.
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct FindDependentsInput {
    /// Optional explicit project selector (daemon sessions with multiple open
    /// projects): an open project ID or unique project name. Omit for the
    /// session's home project. Local/embedded servers are bound to one project
    /// and refuse a non-matching selector.
    #[serde(default)]
    pub project: Option<String>,
    /// Relative file path to find dependents for.
    pub path: String,
    /// Symbol name. NOT a valid parameter for file-level dependents — present
    /// only to detect a symbol-shaped misuse: if you pass `name` (symbol-level)
    /// alongside `path`, the handler returns an explicit redirect to
    /// `find_references`, which answers "who calls this symbol?". Leave unset for
    /// the file-level dependency graph ("what imports this file?").
    #[serde(default)]
    pub name: Option<String>,
    /// Maximum number of dependent files to show (default 20, capped at 100).
    #[serde(default, deserialize_with = "lenient_u32")]
    pub limit: Option<u32>,
    /// Maximum number of reference lines per file (default 5, capped at 50).
    #[serde(default, deserialize_with = "lenient_u32")]
    pub max_per_file: Option<u32>,
    /// Output format: "text" (default), "mermaid", or "dot".
    pub format: Option<String>,
    /// When true, show compact output: one line per dependent file as
    /// `path (N refs: M call, K type_usage, J import)` with no source text
    /// (60-75% smaller). Best for hub files with many dependents.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub compact: Option<bool>,
    /// When true, return an approximate token cost estimate instead of actual content.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub estimate: Option<bool>,
    /// Optional maximum token budget for the response.
    #[serde(default, deserialize_with = "lenient_u64")]
    pub max_tokens: Option<u64>,
}

/// Input for `get_repo_map`.
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct GetRepoMapInput {
    /// Optional explicit project selector (daemon sessions with multiple open
    /// projects): an open project ID or unique project name. Omit for the
    /// session's home project. Local/embedded servers are bound to one project
    /// and refuse a non-matching selector.
    #[serde(default)]
    pub project: Option<String>,
    /// Detail level: "compact" (default — ~500 token project overview), "full" (complete symbol outline of every file), "tree" (browsable file tree with per-file stats).
    pub detail: Option<String>,
    /// Subtree path to browse (only used when detail="tree", default: project root).
    pub path: Option<String>,
    /// Max depth levels to expand (only used when detail="tree", default: 2, max: 5).
    #[serde(default, deserialize_with = "lenient_u32")]
    pub depth: Option<u32>,
    /// Maximum number of files to include in the output (only used when detail="full", default: 200).
    #[serde(default, deserialize_with = "lenient_u32")]
    pub max_files: Option<u32>,
    /// When true, return an approximate token cost estimate instead of actual content.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub estimate: Option<bool>,
    /// Optional maximum token budget for the response.
    #[serde(default, deserialize_with = "lenient_u64")]
    pub max_tokens: Option<u64>,
}

/// Input for `get_file_context`.
#[derive(Serialize, JsonSchema)]
#[schemars(transform = add_file_context_schema)]
pub struct GetFileContextInput {
    /// Optional explicit project selector (daemon sessions with multiple open
    /// projects): an open project ID or unique project name. Omit for the
    /// session's home project. Local/embedded servers are bound to one project
    /// and refuse a non-matching selector.
    #[serde(default)]
    pub project: Option<String>,
    /// Relative path to the file.
    pub path: String,
    /// Optional max token budget, matching hook behavior.
    #[serde(default, deserialize_with = "lenient_u64")]
    pub max_tokens: Option<u64>,
    /// Optional list of sections to include. Allowed values: "outline", "imports", "consumers", "references", "git", "knowledge". Omit or pass an empty list to include all sections.
    #[serde(default, deserialize_with = "lenient_option_vec")]
    #[schemars(with = "Vec<String>")]
    pub sections: Option<Vec<String>>,
    /// When true, return an approximate token cost estimate instead of actual content.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub estimate: Option<bool>,
    /// When true, bypass session cache-hit and return a fresh payload.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub force_refresh: Option<bool>,
}

impl<'de> Deserialize<'de> for GetFileContextInput {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        struct Raw {
            #[serde(default)]
            project: Option<String>,
            path: String,
            #[serde(default, deserialize_with = "lenient_u64")]
            max_tokens: Option<u64>,
            #[serde(default, deserialize_with = "lenient_option_vec")]
            sections: Option<Vec<String>>,
            #[serde(default, deserialize_with = "lenient_bool_required")]
            include_tests: bool,
            #[serde(default, deserialize_with = "lenient_bool")]
            estimate: Option<bool>,
            #[serde(default, deserialize_with = "lenient_bool")]
            force_refresh: Option<bool>,
        }

        let raw = Raw::deserialize(deserializer)?;
        Ok(Self {
            project: raw.project,
            path: raw.path,
            max_tokens: raw.max_tokens,
            sections: encode_include_tests_marker(raw.sections, raw.include_tests),
            estimate: raw.estimate,
            force_refresh: raw.force_refresh,
        })
    }
}

/// Input for `get_symbol_context`.
#[derive(Serialize, JsonSchema)]
#[schemars(transform = add_symbol_context_schema)]
pub struct GetSymbolContextInput {
    /// Optional explicit project selector (daemon sessions with multiple open
    /// projects): an open project ID or unique project name. Omit for the
    /// session's home project. Local/embedded servers are bound to one project
    /// and refuse a non-matching selector.
    #[serde(default)]
    pub project: Option<String>,
    /// Symbol name to inspect.
    pub name: String,
    /// Optional file filter (ignored when bundle=true; use path instead).
    pub file: Option<String>,
    /// File path from `search_symbols`. Required when bundle=true or sections is provided.
    pub path: Option<String>,
    /// Optional selected symbol kind such as `fn`, `class`, or `struct`.
    pub symbol_kind: Option<String>,
    /// Optional selected symbol line from `search_symbols`.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub symbol_line: Option<u32>,
    /// Output verbosity: "summary" (one-line natural language summary ~90% smaller), "signature" (name+params+return only, ~80% smaller), "compact" (signature + first doc line), "full" (default — complete body). Applies to all three modes: default (controls the definition body), bundle (controls the main symbol body; dependency types always show full definitions), and sections/trace (controls the definition shown in the trace header).
    pub verbosity: Option<String>,
    /// When true, switch to bundle mode: returns symbol body + full definitions of all referenced custom types, resolved recursively. Best for edit preparation. Requires path.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub bundle: Option<bool>,
    /// Optional trace-analysis sections. When provided, switches to trace mode: definition,
    /// callers, callees, implementations, type dependencies, git activity.
    /// Valid values: "dependents", "siblings", "implementations", "git", "knowledge".
    /// Omit for default symbol-context mode. Pass empty array for all trace sections.
    #[serde(default, deserialize_with = "lenient_option_vec")]
    #[schemars(with = "Vec<String>")]
    pub sections: Option<Vec<String>>,
    /// Optional max token budget for bundle mode. When set, preserves the main
    /// symbol body and sections, then includes type dependencies in priority
    /// order (direct first, then transitive) until the approximate budget
    /// (~4 chars per token) is exhausted.
    #[serde(default, deserialize_with = "lenient_u64")]
    pub max_tokens: Option<u64>,
    /// When true, return an approximate token cost estimate instead of actual content.
    /// Shows estimated tokens for body, callers, bundle, and raw file.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub estimate: Option<bool>,
}

impl<'de> Deserialize<'de> for GetSymbolContextInput {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        struct Raw {
            #[serde(default)]
            project: Option<String>,
            name: String,
            file: Option<String>,
            path: Option<String>,
            symbol_kind: Option<String>,
            #[serde(default, deserialize_with = "lenient_u32")]
            symbol_line: Option<u32>,
            verbosity: Option<String>,
            #[serde(default, deserialize_with = "lenient_bool")]
            bundle: Option<bool>,
            #[serde(default, deserialize_with = "lenient_option_vec")]
            sections: Option<Vec<String>>,
            #[serde(default, deserialize_with = "lenient_bool_required")]
            include_tests: bool,
            #[serde(default, deserialize_with = "lenient_u64")]
            max_tokens: Option<u64>,
            #[serde(default, deserialize_with = "lenient_bool")]
            estimate: Option<bool>,
        }

        let raw = Raw::deserialize(deserializer)?;
        let sections = match raw.sections {
            Some(sections) => encode_include_tests_marker(Some(sections), raw.include_tests),
            None => None,
        };
        Ok(Self {
            project: raw.project,
            name: raw.name,
            file: raw.file,
            path: raw.path,
            symbol_kind: raw.symbol_kind,
            symbol_line: raw.symbol_line,
            verbosity: raw.verbosity,
            bundle: raw.bundle,
            sections,
            max_tokens: raw.max_tokens,
            estimate: raw.estimate,
        })
    }
}

/// Input for `trace_symbol`.
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct TraceSymbolInput {
    /// File path containing the symbol.
    pub path: String,
    /// Symbol name to trace.
    pub name: String,
    /// Optional kind filter (e.g., "fn", "struct").
    pub kind: Option<String>,
    /// Optional line number to disambiguate overloaded symbols.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub symbol_line: Option<u32>,
    /// Optional list of output sections to include. When omitted, all sections are included.
    /// Valid values: "dependents", "siblings", "implementations", "git".
    #[serde(default, deserialize_with = "lenient_option_vec")]
    pub sections: Option<Vec<String>>,
    /// Output verbosity: "summary" (one-line natural language summary ~90% smaller), "signature" (name+params+return only, ~80% smaller), "compact" (signature + first doc line), "full" (default — complete body).
    pub verbosity: Option<String>,
    /// Optional maximum token budget for the response.
    #[serde(default, deserialize_with = "lenient_u64")]
    pub max_tokens: Option<u64>,
    /// When true, return an approximate token cost estimate instead of actual content.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub estimate: Option<bool>,
}

/// Input for `inspect_match`.
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct InspectMatchInput {
    /// Relative path to the file.
    pub path: String,
    /// 1-based line number to inspect.
    #[serde(deserialize_with = "lenient_u32_required")]
    pub line: u32,
    /// Number of context lines to show around the match (default 3).
    #[serde(default, deserialize_with = "lenient_u32")]
    pub context: Option<u32>,
    /// Maximum number of siblings to show (default 10). Use 0 to hide siblings entirely.
    #[serde(default, deserialize_with = "lenient_u32")]
    pub sibling_limit: Option<u32>,
    /// When true, return an approximate token cost estimate instead of actual content.
    #[serde(default, deserialize_with = "lenient_bool")]
    pub estimate: Option<bool>,
    /// Optional maximum token budget for the response.
    #[serde(default, deserialize_with = "lenient_u64")]
    pub max_tokens: Option<u64>,
}

pub(crate) fn normalize_file_content_aliases(
    input: &mut GetFileContentInput,
) -> Result<(), String> {
    if input.offset.is_none() && input.limit.is_none() {
        return Ok(());
    }

    // Reject combination with any explicit native selector.
    let mut conflicts: Vec<&str> = Vec::new();
    if input.start_line.is_some() {
        conflicts.push("`start_line`");
    }
    if input.end_line.is_some() {
        conflicts.push("`end_line`");
    }
    if input.around_line.is_some() {
        conflicts.push("`around_line`");
    }
    if input.around_match.is_some() {
        conflicts.push("`around_match`");
    }
    if input.around_symbol.is_some() {
        conflicts.push("`around_symbol`");
    }
    if input.chunk_index.is_some() {
        conflicts.push("`chunk_index`");
    }
    if input.mode.is_some() {
        conflicts.push("`mode`");
    }
    if !conflicts.is_empty() {
        return Err(format!(
            "Invalid get_file_content request: `offset`/`limit` aliases cannot be combined with {}. Use native params instead.",
            conflicts.join(", ")
        ));
    }

    if input.limit == Some(0) {
        return Err("Invalid get_file_content request: `limit` must be 1 or greater.".to_string());
    }

    let offset = input.offset.unwrap_or(0);
    input.start_line = Some(offset.saturating_add(1));
    if let Some(limit) = input.limit {
        input.end_line = Some(offset.saturating_add(limit));
    }
    input.offset = None;
    input.limit = None;
    Ok(())
}

pub(crate) fn file_content_options_from_input(
    input: &GetFileContentInput,
) -> Result<search::FileContentOptions, String> {
    let show_line_numbers = input.show_line_numbers.unwrap_or(false);
    let header = input.header.unwrap_or(false);

    // ── Mode dispatch ───────────────────────────────────────────────────
    if let Some(mode) = &input.mode {
        let mode_str = mode.as_str();
        let result = match mode_str {
            "lines" => validate_lines_mode(input),
            "symbol" => validate_symbol_mode(input),
            "match" => validate_match_mode(input),
            "chunk" => validate_chunk_mode(input),
            "search" => Err("mode 'search' is not yet implemented".to_string()),
            other => Err(format!(
                "Unknown mode '{other}'. Valid modes: lines, symbol, match, chunk."
            )),
        };
        return result.map(|mut opts| {
            opts.content_context.mode_name = Some(mode_str.to_string());
            opts.content_context.mode_explicit = true;
            opts
        });
    }
    // No mode — infer from flags (existing backward-compatible behavior below)

    if input.symbol_line.is_some() && input.around_symbol.is_none() {
        return Err(
            "Invalid get_file_content request: `symbol_line` requires `around_symbol`.".to_string(),
        );
    }

    if input.match_occurrence.is_some() && input.around_match.is_none() {
        return Err(
            "Invalid get_file_content request: `match_occurrence` requires `around_match`."
                .to_string(),
        );
    }

    if matches!(input.match_occurrence, Some(0)) {
        return Err(
            "Invalid get_file_content request: `match_occurrence` must be 1 or greater."
                .to_string(),
        );
    }

    if let Some(raw_around_symbol) = input.around_symbol.as_deref() {
        let around_symbol = raw_around_symbol.trim();
        if around_symbol.is_empty() {
            return Err(
                "Invalid get_file_content request: `around_symbol` must not be empty.".to_string(),
            );
        }

        if input.start_line.is_some()
            || input.end_line.is_some()
            || input.around_line.is_some()
            || input.around_match.is_some()
            || input.chunk_index.is_some()
        {
            return Err(
                "Invalid get_file_content request: `around_symbol` cannot be combined with `start_line`, `end_line`, `around_line`, `around_match`, or `chunk_index`. Valid with `around_symbol`: `symbol_line`, `context_lines`, `max_lines`."
                    .to_string(),
            );
        }

        let mut opts = search::FileContentOptions::for_explicit_path_read_around_symbol(
            input.path.clone(),
            around_symbol,
            input.symbol_line,
            input.context_lines,
            input.max_lines,
            show_line_numbers,
            header,
        );
        opts.content_context.mode_name = Some("symbol".to_string());
        opts.content_context.mode_explicit = false;
        return Ok(opts);
    }

    if input.max_lines.is_some() && input.chunk_index.is_none() {
        return Err(
            "Invalid get_file_content request: `max_lines` requires `chunk_index`.".to_string(),
        );
    }

    if let Some(chunk_index) = input.chunk_index {
        let Some(max_lines) = input.max_lines else {
            return Err(
                "Invalid get_file_content request: `chunk_index` requires `max_lines`.".to_string(),
            );
        };

        if chunk_index == 0 {
            return Err(
                "Invalid get_file_content request: `chunk_index` must be 1 or greater.".to_string(),
            );
        }

        if max_lines == 0 {
            return Err(
                "Invalid get_file_content request: `max_lines` must be 1 or greater.".to_string(),
            );
        }

        if input.start_line.is_some()
            || input.end_line.is_some()
            || input.around_line.is_some()
            || input.around_match.is_some()
        {
            return Err(
                "Invalid get_file_content request: chunked reads (`chunk_index` + `max_lines`) cannot be combined with `start_line`, `end_line`, `around_line`, or `around_match`."
                    .to_string(),
            );
        }

        let mut opts = search::FileContentOptions::for_explicit_path_read_chunk(
            input.path.clone(),
            chunk_index,
            max_lines,
        );
        opts.content_context.mode_name = Some("chunk".to_string());
        opts.content_context.mode_explicit = false;
        return Ok(opts);
    }

    // show_line_numbers and header are now allowed with all read modes
    // including around_line and around_match for better usability.

    if let Some(raw_around_match) = input.around_match.as_deref() {
        let around_match = raw_around_match.trim();
        if around_match.is_empty() {
            return Err(
                "Invalid get_file_content request: `around_match` must not be empty.".to_string(),
            );
        }

        if input.start_line.is_some() || input.end_line.is_some() || input.around_line.is_some() {
            return Err(
                "Invalid get_file_content request: `around_match` cannot be combined with `start_line`, `end_line`, or `around_line`. Valid with `around_match`: `context_lines`."
                    .to_string(),
            );
        }

        let mut opts = search::FileContentOptions::for_explicit_path_read_around_match(
            input.path.clone(),
            around_match,
            input.match_occurrence,
            input.context_lines,
            show_line_numbers,
            header,
        );
        opts.content_context.mode_name = Some("match".to_string());
        opts.content_context.mode_explicit = false;
        return Ok(opts);
    }

    if input.around_line.is_some() && (input.start_line.is_some() || input.end_line.is_some()) {
        return Err(
            "Invalid get_file_content request: `around_line` cannot be combined with `start_line` or `end_line`. Valid with `around_line`: `context_lines`."
                .to_string(),
        );
    }

    let mut opts = match input.around_line {
        Some(around_line) => search::FileContentOptions::for_explicit_path_read_around_line(
            input.path.clone(),
            around_line,
            input.context_lines,
            show_line_numbers,
            header,
        ),
        None => search::FileContentOptions::for_explicit_path_read_with_format(
            input.path.clone(),
            input.start_line,
            input.end_line,
            show_line_numbers,
            header,
        ),
    };
    opts.content_context.mode_name = Some("lines".to_string());
    opts.content_context.mode_explicit = false;
    Ok(opts)
}

// ── Mode validators for get_file_content ────────────────────────────────

/// Collect names of flags that are `Some` / `true` for error messages.
fn describe_received_flags(input: &GetFileContentInput) -> String {
    let mut flags = Vec::new();
    if input.start_line.is_some() {
        flags.push("start_line");
    }
    if input.end_line.is_some() {
        flags.push("end_line");
    }
    if input.around_line.is_some() {
        flags.push("around_line");
    }
    if input.around_match.is_some() {
        flags.push("around_match");
    }
    if input.match_occurrence.is_some() {
        flags.push("match_occurrence");
    }
    if input.around_symbol.is_some() {
        flags.push("around_symbol");
    }
    if input.symbol_line.is_some() {
        flags.push("symbol_line");
    }
    if input.chunk_index.is_some() {
        flags.push("chunk_index");
    }
    if input.max_lines.is_some() {
        flags.push("max_lines");
    }
    if input.context_lines.is_some() {
        flags.push("context_lines");
    }
    if input.show_line_numbers.is_some() {
        flags.push("show_line_numbers");
    }
    if input.header.is_some() {
        flags.push("header");
    }
    flags.join(", ")
}

fn validate_lines_mode(input: &GetFileContentInput) -> Result<search::FileContentOptions, String> {
    let cross: Vec<&str> = [
        input.around_symbol.as_ref().map(|_| "around_symbol"),
        input.around_match.as_ref().map(|_| "around_match"),
        input.match_occurrence.map(|_| "match_occurrence"),
        input.chunk_index.map(|_| "chunk_index"),
    ]
    .into_iter()
    .flatten()
    .collect();

    if !cross.is_empty() {
        return Err(format!(
            "mode=lines conflicts with {}. Use mode={}. Received: {}",
            cross.join(", "),
            if input.around_symbol.is_some() {
                "symbol"
            } else if input.around_match.is_some() || input.match_occurrence.is_some() {
                "match"
            } else {
                "chunk"
            },
            describe_received_flags(input),
        ));
    }

    let show_line_numbers = input.show_line_numbers.unwrap_or(false);
    let header = input.header.unwrap_or(false);

    if input.around_line.is_some() && (input.start_line.is_some() || input.end_line.is_some()) {
        return Err(
            "Invalid get_file_content request: `around_line` cannot be combined with `start_line` or `end_line`. Valid with `around_line`: `context_lines`."
                .to_string(),
        );
    }

    Ok(match input.around_line {
        Some(around_line) => search::FileContentOptions::for_explicit_path_read_around_line(
            input.path.clone(),
            around_line,
            input.context_lines,
            show_line_numbers,
            header,
        ),
        None => search::FileContentOptions::for_explicit_path_read_with_format(
            input.path.clone(),
            input.start_line,
            input.end_line,
            show_line_numbers,
            header,
        ),
    })
}

fn validate_symbol_mode(input: &GetFileContentInput) -> Result<search::FileContentOptions, String> {
    let Some(raw_around_symbol) = input.around_symbol.as_deref() else {
        return Err("mode=symbol requires around_symbol".to_string());
    };
    let around_symbol = raw_around_symbol.trim();
    if around_symbol.is_empty() {
        return Err("mode=symbol requires around_symbol".to_string());
    }

    let cross: Vec<&str> = [
        input.start_line.map(|_| "start_line"),
        input.end_line.map(|_| "end_line"),
        input.around_line.map(|_| "around_line"),
        input.around_match.as_ref().map(|_| "around_match"),
        input.match_occurrence.map(|_| "match_occurrence"),
        input.chunk_index.map(|_| "chunk_index"),
    ]
    .into_iter()
    .flatten()
    .collect();

    if !cross.is_empty() {
        return Err(format!(
            "mode=symbol conflicts with {}. Received: {}",
            cross.join(", "),
            describe_received_flags(input),
        ));
    }

    let show_line_numbers = input.show_line_numbers.unwrap_or(false);
    let header = input.header.unwrap_or(false);

    Ok(
        search::FileContentOptions::for_explicit_path_read_around_symbol(
            input.path.clone(),
            around_symbol,
            input.symbol_line,
            input.context_lines,
            input.max_lines,
            show_line_numbers,
            header,
        ),
    )
}

fn validate_match_mode(input: &GetFileContentInput) -> Result<search::FileContentOptions, String> {
    let Some(raw_around_match) = input.around_match.as_deref() else {
        return Err("mode=match requires around_match".to_string());
    };
    let around_match = raw_around_match.trim();
    if around_match.is_empty() {
        return Err("mode=match requires around_match".to_string());
    }
    if matches!(input.match_occurrence, Some(0)) {
        return Err(
            "Invalid get_file_content request: `match_occurrence` must be 1 or greater."
                .to_string(),
        );
    }

    let cross: Vec<&str> = [
        input.start_line.map(|_| "start_line"),
        input.end_line.map(|_| "end_line"),
        input.around_line.map(|_| "around_line"),
        input.around_symbol.as_ref().map(|_| "around_symbol"),
        input.chunk_index.map(|_| "chunk_index"),
    ]
    .into_iter()
    .flatten()
    .collect();

    if !cross.is_empty() {
        return Err(format!(
            "mode=match conflicts with {}. Use mode={}. Received: {}",
            cross.join(", "),
            if input.around_symbol.is_some() {
                "symbol"
            } else if input.start_line.is_some()
                || input.end_line.is_some()
                || input.around_line.is_some()
            {
                "lines"
            } else {
                "chunk"
            },
            describe_received_flags(input),
        ));
    }

    let show_line_numbers = input.show_line_numbers.unwrap_or(false);
    let header = input.header.unwrap_or(false);

    Ok(
        search::FileContentOptions::for_explicit_path_read_around_match(
            input.path.clone(),
            around_match,
            input.match_occurrence,
            input.context_lines,
            show_line_numbers,
            header,
        ),
    )
}

fn validate_chunk_mode(input: &GetFileContentInput) -> Result<search::FileContentOptions, String> {
    let chunk_index = input
        .chunk_index
        .ok_or_else(|| "mode=chunk requires chunk_index".to_string())?;
    let max_lines = input
        .max_lines
        .ok_or_else(|| "mode=chunk requires max_lines".to_string())?;

    if chunk_index == 0 {
        return Err(
            "Invalid get_file_content request: `chunk_index` must be 1 or greater.".to_string(),
        );
    }
    if max_lines == 0 {
        return Err(
            "Invalid get_file_content request: `max_lines` must be 1 or greater.".to_string(),
        );
    }

    let cross: Vec<&str> = [
        input.around_symbol.as_ref().map(|_| "around_symbol"),
        input.around_match.as_ref().map(|_| "around_match"),
        input.match_occurrence.map(|_| "match_occurrence"),
        input.around_line.map(|_| "around_line"),
        input.start_line.map(|_| "start_line"),
        input.end_line.map(|_| "end_line"),
    ]
    .into_iter()
    .flatten()
    .collect();

    if !cross.is_empty() {
        return Err(format!(
            "mode=chunk conflicts with {}. Use mode={}. Received: {}",
            cross.join(", "),
            if input.around_symbol.is_some() {
                "symbol"
            } else if input.around_match.is_some() || input.match_occurrence.is_some() {
                "match"
            } else {
                "lines"
            },
            describe_received_flags(input),
        ));
    }

    Ok(search::FileContentOptions::for_explicit_path_read_chunk(
        input.path.clone(),
        chunk_index,
        max_lines,
    ))
}

fn add_sections_allowlist(schema: &mut Schema, values: &[&str]) {
    if let Some(serde_json::Value::Object(properties)) = schema.get_mut("properties")
        && let Some(serde_json::Value::Object(sections)) = properties.get_mut("sections")
    {
        sections.insert(
            "items".to_string(),
            serde_json::json!({ "type": "string", "enum": values }),
        );
    }
}

fn add_file_context_schema(schema: &mut Schema) {
    add_include_tests_schema(schema);
    add_sections_allowlist(
        schema,
        &[
            "outline",
            "imports",
            "consumers",
            "references",
            "git",
            "knowledge",
        ],
    );
}

fn add_symbol_context_schema(schema: &mut Schema) {
    add_include_tests_schema(schema);
    add_sections_allowlist(
        schema,
        &[
            "dependents",
            "siblings",
            "implementations",
            "git",
            "knowledge",
        ],
    );
}

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

    fn section_values<T: JsonSchema>() -> Vec<String> {
        let schema = serde_json::to_value(schemars::schema_for!(T)).expect("schema json");
        schema["properties"]["sections"]["items"]["enum"]
            .as_array()
            .expect("sections enum")
            .iter()
            .map(|value| value.as_str().expect("string enum").to_string())
            .collect()
    }

    #[test]
    fn context_schemas_advertise_the_exact_knowledge_section_allowlists() {
        assert_eq!(
            section_values::<GetFileContextInput>(),
            [
                "outline",
                "imports",
                "consumers",
                "references",
                "git",
                "knowledge",
            ]
        );
        assert_eq!(
            section_values::<GetSymbolContextInput>(),
            [
                "dependents",
                "siblings",
                "implementations",
                "git",
                "knowledge",
            ]
        );
    }
}