symforge 11.20.4

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
//! The accepted VALUES of every tool argument that has a closed set or a range (feature
//! 036, owner rule "no argument may be silently ignored"): one table, judged before any
//! work by every lane an LLM talks to, the MCP server (`protocol::tool_contract`, the
//! daemon route) and the embed host (`HostRoom::dispatch_wire`, the typed query lane), so
//! a mistyped `kind` or a `limit` of 0 is refused with the same bytes everywhere instead
//! of answering a different question.
//!
//! A row refuses a value the handler would otherwise replace with its default, ignore, or
//! clamp without saying so. Not here: values a handler already refuses with its own text
//! (`inspect_match` line past the end, `insert_symbol` position, `secret_remediate`
//! action, typed enums the decoder refuses), and upper bounds a tool's published
//! description promises to cap (`capped at 100`): those stay clamps.
//!
//! A refusal names the argument and lists the valid values or the valid range. It echoes
//! a NUMBER the caller sent (a number cannot carry a secret worth more than its digits,
//! and it is cut at the key cap) but never a string value.

use serde_json::{Map, Value};

use super::request_refusal::{array_items, capped_key, current_value, element_object};

/// What an argument accepts.
#[derive(Clone, Copy, Debug)]
pub(crate) enum Accepts {
    /// One of these strings, exactly.
    OneOf(&'static [&'static str]),
    /// One of these strings, in any letter case (the handler compares caselessly).
    OneOfCaseless(&'static [&'static str]),
    /// An integer of at least `min`, and at most `max` when one is given.
    Range(u64, Option<u64>),
    /// An integer of at least `min`; above `max` the tool uses `max`, as its published
    /// description says, and the answer says so ([`check_values`] returns the note).
    Capped(u64, u64),
}

use Accepts::{Capped, OneOf, OneOfCaseless, Range};

/// The `sections` element the MCP decoders add for `include_tests=true`
/// (`protocol::read_tools::encode_include_tests_marker`). A caller never sends it; the
/// daemon route sees it in the arguments the front end re-serializes, so it is not judged.
pub(crate) const INCLUDE_TESTS_SECTION_MARKER: &str = "__symforge_include_tests";

/// When a row is judged: the handler reads the argument only in some modes, and an
/// argument a mode ignores is not judged here (the mode rules own that).
#[derive(Clone, Copy, Debug)]
pub(crate) enum Applies {
    Always,
    /// Not when `argument` is `value` (as the mode rules compare it).
    Unless(&'static str, &'static str),
    /// Only when `argument` is `value`.
    If(&'static str, &'static str),
}

/// At least one: a count, a 1-based line, or a token budget.
const POSITIVE: Accepts = Range(1, None);

/// Symbol kind filters: every `SymbolKind` display name, the aliases
/// `kind_filter_matches` accepts, and `all` (`live_index::search`).
pub(crate) const SYMBOL_KINDS: &[&str] = &[
    "all",
    "fn",
    "function",
    "method",
    "class",
    "struct",
    "enum",
    "interface",
    "mod",
    "module",
    "const",
    "constant",
    "let",
    "variable",
    "type",
    "trait",
    "impl",
    "other",
    "key",
    "section",
    "macro-generated",
];

/// `get_file_context` `sections` (also the published schema's item enum).
pub(crate) const FILE_CONTEXT_SECTIONS: &[&str] = &[
    "outline",
    "imports",
    "consumers",
    "references",
    "git",
    "knowledge",
];

/// `get_symbol_context` `sections` (also the published schema's item enum).
pub(crate) const SYMBOL_CONTEXT_SECTIONS: &[&str] = &[
    "dependents",
    "siblings",
    "implementations",
    "git",
    "knowledge",
];

/// `get_file_content` `mode` (`read_contract::file_content_options_from_input`).
pub(crate) const FILE_CONTENT_MODES: &[&str] = &["lines", "symbol", "match", "chunk"];

/// `search_text` `group_by` (`search_render`; `purpose` is the alias of `usage`).
pub(crate) const TEXT_GROUPINGS: &[&str] =
    &["file", "symbol", "usage", "purpose", "names", "count"];

/// `get_repo_map` `detail` (`tools.rs` / `embed_read_context` dispatch).
pub(crate) const REPO_MAP_DETAILS: &[&str] = &["compact", "full", "tree"];

/// `find_dependents` `format`.
pub(crate) const DEPENDENT_FORMATS: &[&str] = &["text", "mermaid", "dot"];

/// `find_references` `mode` and `direction`.
pub(crate) const REFERENCE_MODES: &[&str] = &["references", "implementations"];
pub(crate) const REFERENCE_DIRECTIONS: &[&str] = &["trait", "type", "auto"];

/// `get_symbol_context` `verbosity` (`symbol_context::apply_verbosity`).
pub(crate) const VERBOSITIES: &[&str] = &["full", "summary", "signature", "compact"];

/// One argument's accepted values. `argument` is a top-level name, `name[]` for every
/// element of an array argument, or `name[].field` for a field of every element.
#[derive(Clone, Copy, Debug)]
pub(crate) struct ValueRow {
    pub tool: &'static str,
    pub argument: &'static str,
    pub accepts: Accepts,
    pub applies: Applies,
}

const fn row(tool: &'static str, argument: &'static str, accepts: Accepts) -> ValueRow {
    ValueRow {
        tool,
        argument,
        accepts,
        applies: Applies::Always,
    }
}

impl ValueRow {
    const fn unless(mut self, argument: &'static str, value: &'static str) -> Self {
        self.applies = Applies::Unless(argument, value);
        self
    }
    const fn only_if(mut self, argument: &'static str, value: &'static str) -> Self {
        self.applies = Applies::If(argument, value);
        self
    }
}

/// Every judged argument, in the order a call is judged (the first failing row refuses).
pub(crate) static ARGUMENT_VALUES: &[ValueRow] = &[
    // -- reads --
    row("get_file_content", "mode", OneOf(FILE_CONTENT_MODES)),
    row("get_file_content", "start_line", POSITIVE),
    row("get_file_content", "end_line", POSITIVE),
    row("get_file_content", "around_line", POSITIVE),
    row("get_file_content", "chunk_index", POSITIVE),
    row("get_file_content", "max_lines", POSITIVE),
    row("get_file_content", "match_occurrence", POSITIVE),
    row("get_file_content", "symbol_line", POSITIVE),
    row("get_file_content", "limit", POSITIVE),
    row("get_file_content", "max_tokens", POSITIVE),
    row("get_symbol", "kind", OneOfCaseless(SYMBOL_KINDS)),
    row("get_symbol", "symbol_line", POSITIVE),
    row("get_symbol", "targets[].kind", OneOfCaseless(SYMBOL_KINDS)),
    row("get_symbol", "targets[].symbol_line", POSITIVE),
    row("get_symbol", "max_tokens", POSITIVE),
    row("get_repo_map", "detail", OneOf(REPO_MAP_DETAILS)),
    // "default 2, max 5"; 0 groups every file under the scope root (`map_dir_key`).
    row("get_repo_map", "depth", Capped(0, 5)),
    row("get_repo_map", "max_files", POSITIVE),
    row("get_repo_map", "max_tokens", POSITIVE),
    row(
        "get_file_context",
        "sections[]",
        OneOfCaseless(FILE_CONTEXT_SECTIONS),
    ),
    row("get_file_context", "max_tokens", POSITIVE),
    row(
        "get_symbol_context",
        "symbol_kind",
        OneOfCaseless(SYMBOL_KINDS),
    ),
    row("get_symbol_context", "symbol_line", POSITIVE),
    row("get_symbol_context", "verbosity", OneOf(VERBOSITIES)),
    row(
        "get_symbol_context",
        "sections[]",
        OneOfCaseless(SYMBOL_CONTEXT_SECTIONS),
    ),
    row("get_symbol_context", "max_tokens", POSITIVE),
    row("inspect_match", "max_tokens", POSITIVE),
    // The daemon's `trace_symbol` alias (judged as `get_symbol_context` plus this row).
    row("trace_symbol", "kind", OneOfCaseless(SYMBOL_KINDS)),
    // -- search --
    row("search_symbols", "kind", OneOfCaseless(SYMBOL_KINDS)),
    row("search_symbols", "limit", Capped(1, 100)),
    row("search_symbols", "max_tokens", POSITIVE),
    row("search_text", "limit", Range(1, Some(10_000))).unless("group_by", "count"),
    row("search_text", "max_per_file", Range(1, Some(10_000))).unless("group_by", "count"),
    row("search_text", "follow_refs_limit", POSITIVE).only_if("follow_refs", "true"),
    row("search_text", "group_by", OneOf(TEXT_GROUPINGS)),
    row("search_text", "max_tokens", POSITIVE),
    row("search_files", "limit", Capped(1, 50)),
    row("search_files", "max_tokens", POSITIVE),
    row("search_knowledge", "limit", Capped(1, 100)),
    // "Server-bounded" by the published description (`knowledge::review` MAX_LIMIT).
    row("review_knowledge", "limit", Capped(1, 100)),
    row("explore", "depth", Range(1, Some(3))),
    row("explore", "limit", Range(1, Some(10_000))),
    row("explore", "max_tokens", POSITIVE),
    // -- references --
    row(
        "find_references",
        "kind",
        OneOf(super::reference_read::REFERENCE_KIND_NAMES),
    ),
    row(
        "find_references",
        "symbol_kind",
        OneOfCaseless(SYMBOL_KINDS),
    ),
    row("find_references", "symbol_line", POSITIVE).unless("mode", "implementations"),
    // "capped at 100/500" and "capped at 50" by the published description.
    row("find_references", "limit", Capped(1, 100)).unless("mode", "implementations"),
    row("find_references", "limit", Capped(1, 500)).only_if("mode", "implementations"),
    row("find_references", "max_per_file", Capped(1, 50)).unless("mode", "implementations"),
    row("find_references", "mode", OneOf(REFERENCE_MODES)),
    row("find_references", "direction", OneOf(REFERENCE_DIRECTIONS)),
    row("find_references", "max_tokens", POSITIVE),
    row("find_dependents", "limit", Capped(1, 100)),
    row("find_dependents", "max_per_file", Capped(1, 50)),
    row("find_dependents", "format", OneOf(DEPENDENT_FORMATS)),
    row("find_dependents", "max_tokens", POSITIVE),
    // -- changes, impact, guidance --
    row("analyze_file_impact", "co_changes_limit", POSITIVE),
    row("what_changed", "max_tokens", POSITIVE),
    row("diff_symbols", "max_tokens", POSITIVE),
    row("ask", "max_tokens", POSITIVE),
    // The published schema's minimum.
    row("symforge", "max_tokens", Range(64, None)),
    row("health", "quarantine_limit", Capped(1, 1000)),
    // -- edits (MCP request shapes; the embed edit requests have their own) --
    row("replace_symbol_body", "kind", OneOfCaseless(SYMBOL_KINDS)),
    row("replace_symbol_body", "symbol_line", POSITIVE),
    row("insert_symbol", "kind", OneOfCaseless(SYMBOL_KINDS)),
    row("insert_symbol", "symbol_line", POSITIVE),
    row("delete_symbol", "kind", OneOfCaseless(SYMBOL_KINDS)),
    row("delete_symbol", "symbol_line", POSITIVE),
    row("edit_within_symbol", "kind", OneOfCaseless(SYMBOL_KINDS)),
    row("edit_within_symbol", "symbol_line", POSITIVE),
    row("edit_within_symbol", "near_line", POSITIVE),
    row("batch_edit", "edits[].kind", OneOfCaseless(SYMBOL_KINDS)),
    row("batch_edit", "edits[].symbol_line", POSITIVE),
    row(
        "batch_insert",
        "targets[].kind",
        OneOfCaseless(SYMBOL_KINDS),
    ),
    row("batch_insert", "targets[].symbol_line", POSITIVE),
    row("batch_rename", "kind", OneOfCaseless(SYMBOL_KINDS)),
    row("batch_rename", "symbol_line", POSITIVE),
];

/// A required string argument whose empty (or blank) value names nothing: refused, unless
/// `unless` is given (the call then selects by that argument instead). `defaulted`: the
/// decoder fills an absent value with `""`, so absence is judged here too; otherwise an
/// absent value is the decoder's missing-field refusal.
#[derive(Clone, Copy, Debug)]
pub(crate) struct NonEmptyRow {
    pub tool: &'static str,
    pub argument: &'static str,
    pub unless: Option<&'static str>,
    pub defaulted: bool,
}

pub(crate) static NON_EMPTY: &[NonEmptyRow] = &[
    NonEmptyRow {
        tool: "get_symbol",
        argument: "name",
        unless: Some("targets"),
        defaulted: true,
    },
    NonEmptyRow {
        tool: "get_symbol_context",
        argument: "name",
        unless: None,
        defaulted: false,
    },
    NonEmptyRow {
        tool: "find_references",
        argument: "name",
        unless: None,
        defaulted: false,
    },
];

/// A value outside an argument's set or range.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ValueRefusal {
    pub text: String,
    /// The value is outside a closed set of names (an unsupported option), rather than
    /// a number out of range or an empty name.
    pub closed_set: bool,
}

/// Judge `args` of MCP tool `tool`: the first argument outside its accepted values is
/// refused; otherwise every capped value that was above its cap gets one note the answer
/// must carry (`Note: "limit" = 500 is above its maximum of 100; 100 was used.`). Null,
/// `""` and `[]` are absent (FR-003); a value of the wrong JSON type is the decoder's.
pub(crate) fn check_values(
    tool: &str,
    args: &Map<String, Value>,
) -> Result<Vec<CapNote>, ValueRefusal> {
    if let Some(text) = blank_name_refusal(tool, args) {
        return Err(ValueRefusal {
            text,
            closed_set: false,
        });
    }
    let mut notes = Vec::new();
    for row in ARGUMENT_VALUES.iter().filter(|row| row.tool == tool) {
        if !applies(row.applies, args) {
            continue;
        }
        for (path, value) in values_of(row.argument, args) {
            match judge(tool, &path, row.accepts, &value) {
                Judged::Valid => {}
                Judged::Capped(note) => notes.push(note),
                Judged::Refused(text) => {
                    return Err(ValueRefusal {
                        text,
                        closed_set: matches!(row.accepts, OneOf(_) | OneOfCaseless(_)),
                    });
                }
            }
        }
    }
    Ok(notes)
}

/// [`check_values`]' refusal text only.
pub(crate) fn value_refusal(tool: &str, args: &Map<String, Value>) -> Option<String> {
    check_values(tool, args).err().map(|refusal| refusal.text)
}

/// A capped value: the caller sent `requested` for `path`, the tool used `max`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct CapNote {
    pub path: String,
    pub requested: u64,
    pub max: u64,
}

impl CapNote {
    fn sentence(&self) -> String {
        format!(
            "Note: \"{}\" = {} is above its maximum of {}; {} was used.",
            self.path,
            capped_key(&self.requested.to_string()),
            self.max,
            self.max
        )
    }
}

/// The text to append to `tool`'s answer for `notes`, in that answer's grammar: a
/// `key=value` line for `review_knowledge`, a `//` comment after a dot graph, a `%%`
/// comment after a mermaid diagram, otherwise a prose line after a blank line. Every
/// other tool with a capped argument answers in prose. Empty when there are no notes.
pub(crate) fn notes_suffix(tool: &str, args: &Map<String, Value>, notes: &[CapNote]) -> String {
    let format = current_value(args, "format");
    notes
        .iter()
        .map(|note| match (tool, format.as_str()) {
            ("review_knowledge", _) => format!(
                "\nnote=capped argument={} requested={} used={}",
                note.path,
                capped_key(&note.requested.to_string()),
                note.max
            ),
            ("find_dependents", "dot") => format!("\n// {}", note.sentence()),
            ("find_dependents", "mermaid") => format!("\n%% {}", note.sentence()),
            _ => format!("\n\n{}", note.sentence()),
        })
        .collect()
}

/// Bytes [`notes_suffix`] adds, as tokens (~4 bytes each), so a caller's `max_tokens`
/// can be reduced by them before the answer is built and the note fits the budget.
pub(crate) fn notes_tokens(suffix: &str) -> u64 {
    (suffix.len() as u64).div_ceil(4)
}

fn blank_name_refusal(tool: &str, args: &Map<String, Value>) -> Option<String> {
    for rule in NON_EMPTY.iter().filter(|rule| rule.tool == tool) {
        let blank = match args.get(rule.argument) {
            None | Some(Value::Null) => rule.defaulted,
            Some(Value::String(text)) => text.trim().is_empty(),
            Some(_) => false,
        };
        let excused = rule
            .unless
            .and_then(|other| args.get(other))
            .and_then(array_items)
            .is_some_and(|items| !items.is_empty());
        if blank && !excused {
            let unless = rule
                .unless
                .map(|other| format!(" unless \"{other}\" is given"))
                .unwrap_or_default();
            return Some(format!(
                "invalid_request: {tool} requires a non-empty \"{}\"{unless}.",
                rule.argument
            ));
        }
    }
    None
}

fn applies(applies: Applies, args: &Map<String, Value>) -> bool {
    match applies {
        Applies::Always => true,
        Applies::Unless(argument, value) => current_value(args, argument) != value,
        Applies::If(argument, value) => current_value(args, argument) == value,
    }
}

/// Every value `argument` names in `args`, with the path the refusal prints for it.
fn values_of(argument: &str, args: &Map<String, Value>) -> Vec<(String, Value)> {
    let Some((array, field)) = argument.split_once("[]") else {
        return args
            .get(argument)
            .map(|value| vec![(argument.to_string(), value.clone())])
            .unwrap_or_default();
    };
    let Some(items) = args.get(array).and_then(array_items) else {
        return Vec::new();
    };
    let field = field.strip_prefix('.');
    items
        .into_iter()
        .enumerate()
        .filter(|(_, item)| item.as_str() != Some(INCLUDE_TESTS_SECTION_MARKER))
        .filter_map(|(index, item)| match field {
            None => Some((format!("{array}.{index}"), item)),
            Some(field) => element_object(&item)?
                .get(field)
                .map(|value| (format!("{array}.{index}.{field}"), value.clone())),
        })
        .collect()
}

fn listed(values: &[&str]) -> String {
    values
        .iter()
        .map(|value| format!("\"{value}\""))
        .collect::<Vec<_>>()
        .join(", ")
}

/// An integer exactly as the field's lenient decoder accepts one
/// (`serde_input::lenient_u32` / `lenient_u64`): a non-negative JSON integer or a string
/// of digits, untrimmed, that fits the field's type. Anything else is the decoder's to
/// refuse, in its own words.
fn integer(tool: &str, path: &str, value: &Value) -> Option<u64> {
    let number = match value {
        Value::Number(number) => number.as_u64(),
        Value::String(text) => text.parse::<u64>().ok(),
        _ => None,
    }?;
    // Every judged number is a `u32` field except the `max_tokens` budgets, which are
    // `u64` everywhere but the `symforge` facade.
    let wide = path == "max_tokens" && tool != "symforge";
    (wide || number <= u64::from(u32::MAX)).then_some(number)
}

enum Judged {
    Valid,
    Capped(CapNote),
    Refused(String),
}

fn range_text(min: u64, max: Option<u64>) -> String {
    match max {
        Some(max) => format!("{min} to {max}"),
        None => format!("{min} or greater"),
    }
}

fn judge(tool: &str, path: &str, accepts: Accepts, value: &Value) -> Judged {
    let refused_number = |number: u64, max: Option<u64>, min: u64| {
        Judged::Refused(format!(
            "invalid_request: {tool} does not accept \"{path}\" = {}. Valid range: {}.",
            capped_key(&number.to_string()),
            range_text(min, max)
        ))
    };
    match accepts {
        OneOf(values) | OneOfCaseless(values) => {
            let Value::String(text) = value else {
                return Judged::Valid;
            };
            if text.is_empty() {
                return Judged::Valid;
            }
            let caseless = matches!(accepts, OneOfCaseless(_));
            let known = values.iter().any(|valid| {
                *valid == text.as_str() || (caseless && valid.eq_ignore_ascii_case(text))
            });
            if known {
                Judged::Valid
            } else {
                Judged::Refused(format!(
                    "invalid_request: {tool} does not accept that value of \"{path}\". Valid \
                     values: {}.",
                    listed(values)
                ))
            }
        }
        Range(min, max) => match integer(tool, path, value) {
            Some(number) if number < min || max.is_some_and(|max| number > max) => {
                refused_number(number, max, min)
            }
            _ => Judged::Valid,
        },
        Capped(min, max) => match integer(tool, path, value) {
            Some(number) if number < min => refused_number(number, None, min),
            Some(number) if number > max => Judged::Capped(CapNote {
                path: path.to_string(),
                requested: number,
                max,
            }),
            _ => Judged::Valid,
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::SymbolKind;
    use serde_json::json;

    fn refusal(tool: &str, args: Value) -> Option<String> {
        value_refusal(tool, args.as_object().expect("object"))
    }

    #[test]
    fn a_value_outside_the_set_is_refused_with_the_set() {
        assert_eq!(
            refusal(
                "find_dependents",
                json!({ "path": "a.rs", "format": "bogus" })
            )
            .as_deref(),
            Some(
                "invalid_request: find_dependents does not accept that value of \"format\". \
                 Valid values: \"text\", \"mermaid\", \"dot\"."
            )
        );
        assert_eq!(refusal("find_dependents", json!({ "format": "dot" })), None);
    }

    #[test]
    fn a_number_outside_the_range_is_refused_with_the_range() {
        assert_eq!(
            refusal("explore", json!({ "query": "x", "depth": 4 })).as_deref(),
            Some("invalid_request: explore does not accept \"depth\" = 4. Valid range: 1 to 3.")
        );
        assert_eq!(
            refusal("find_references", json!({ "name": "x", "limit": "0" })).as_deref(),
            Some(
                "invalid_request: find_references does not accept \"limit\" = 0. Valid range: 1 \
                 or greater."
            )
        );
        assert_eq!(
            refusal("explore", json!({ "query": "x", "depth": 3 })),
            None
        );
    }

    #[test]
    fn absent_values_and_wrong_types_are_not_judged_here() {
        for args in [
            json!({ "name": "x", "kind": null }),
            json!({ "name": "x", "kind": "" }),
            json!({ "name": "x", "limit": "" }),
            json!({ "name": "x", "limit": "many" }),
            json!({ "name": "x", "kind": 5 }),
        ] {
            assert_eq!(refusal("find_references", args.clone()), None, "{args}");
        }
    }

    #[test]
    fn elements_and_element_fields_are_named_by_their_path() {
        assert_eq!(
            refusal(
                "get_symbol_context",
                json!({ "name": "x", "sections": ["git", "bogus"] })
            )
            .as_deref(),
            Some(
                "invalid_request: get_symbol_context does not accept that value of \
                 \"sections.1\". Valid values: \"dependents\", \"siblings\", \
                 \"implementations\", \"git\", \"knowledge\"."
            )
        );
        let stringified =
            json!({ "targets": "[{\"path\":\"a.rs\",\"name\":\"x\",\"symbol_line\":0}]" });
        assert_eq!(
            refusal("get_symbol", stringified).as_deref(),
            Some(
                "invalid_request: get_symbol does not accept \"targets.0.symbol_line\" = 0. \
                 Valid range: 1 or greater."
            )
        );
        assert_eq!(
            refusal(
                "get_symbol_context",
                json!({ "name": "x", "sections": ["GIT"] })
            ),
            None,
            "sections compare caselessly, as the handlers do"
        );
    }

    #[test]
    fn a_blank_required_name_is_refused_unless_targets_select() {
        assert_eq!(
            refusal("find_references", json!({ "name": " " })).as_deref(),
            Some("invalid_request: find_references requires a non-empty \"name\".")
        );
        assert_eq!(
            refusal("get_symbol", json!({ "path": "a.rs", "name": "" })).as_deref(),
            Some(
                "invalid_request: get_symbol requires a non-empty \"name\" unless \"targets\" \
                 is given."
            )
        );
        assert_eq!(
            refusal(
                "get_symbol",
                json!({ "targets": [{ "path": "a.rs", "name": "x" }] })
            ),
            None
        );
    }

    #[test]
    fn a_refusal_never_echoes_a_string_value() {
        let text = refusal(
            "search_text",
            json!({ "query": "x", "group_by": "secret-value" }),
        )
        .expect("refused");
        assert!(!text.contains("secret-value"), "{text}");
    }

    #[test]
    fn every_row_names_a_published_argument() {
        for row in ARGUMENT_VALUES
            .iter()
            .filter(|row| row.tool != "trace_symbol")
        {
            let top = row.argument.split("[]").next().unwrap_or(row.argument);
            let published = super::super::request_refusal::tool_arguments(row.tool)
                .unwrap_or_else(|| panic!("{} has no published arguments", row.tool));
            assert!(
                published.contains(&top),
                "{}.{} is not a published argument",
                row.tool,
                row.argument
            );
        }
    }

    /// Every symbol kind, by an exhaustive match: a new kind fails to compile here.
    fn every_symbol_kind() -> Vec<SymbolKind> {
        let all = [
            SymbolKind::Function,
            SymbolKind::Method,
            SymbolKind::Class,
            SymbolKind::Struct,
            SymbolKind::Enum,
            SymbolKind::Interface,
            SymbolKind::Module,
            SymbolKind::Constant,
            SymbolKind::Variable,
            SymbolKind::Type,
            SymbolKind::Trait,
            SymbolKind::Impl,
            SymbolKind::Other,
            SymbolKind::Key,
            SymbolKind::Section,
            SymbolKind::MacroGenerated,
        ];
        for kind in &all {
            match kind {
                SymbolKind::Function
                | SymbolKind::Method
                | SymbolKind::Class
                | SymbolKind::Struct
                | SymbolKind::Enum
                | SymbolKind::Interface
                | SymbolKind::Module
                | SymbolKind::Constant
                | SymbolKind::Variable
                | SymbolKind::Type
                | SymbolKind::Trait
                | SymbolKind::Impl
                | SymbolKind::Other
                | SymbolKind::Key
                | SymbolKind::Section
                | SymbolKind::MacroGenerated => {}
            }
        }
        all.to_vec()
    }

    #[test]
    fn the_symbol_kind_set_is_the_filter_the_index_applies() {
        let kinds = every_symbol_kind();
        for kind in &kinds {
            assert!(
                SYMBOL_KINDS.contains(&kind.to_string().as_str()),
                "{kind} is missing from SYMBOL_KINDS"
            );
        }
        for name in SYMBOL_KINDS {
            assert!(
                *name == "all"
                    || kinds
                        .iter()
                        .any(|kind| crate::live_index::search::kind_filter_matches(name, kind)),
                "{name} matches no symbol kind"
            );
        }
    }

    #[test]
    fn the_reference_kind_set_is_the_filter_the_handlers_apply() {
        use super::super::reference_read::{REFERENCE_KIND_FILTERS, REFERENCE_KIND_NAMES};
        let names: Vec<&str> = REFERENCE_KIND_FILTERS
            .iter()
            .map(|(name, _)| *name)
            .collect();
        assert_eq!(names, REFERENCE_KIND_NAMES);
    }

    #[test]
    fn the_file_content_modes_are_the_modes_the_reader_dispatches() {
        for mode in FILE_CONTENT_MODES {
            let input: super::super::read_contract::GetFileContentInput =
                serde_json::from_value(json!({ "path": "a.rs", "mode": mode })).expect("input");
            let outcome = super::super::read_contract::file_content_options_from_input(&input);
            assert!(
                !outcome
                    .as_ref()
                    .err()
                    .is_some_and(|error| error.starts_with("Unknown mode")),
                "{mode}: {outcome:?}"
            );
        }
    }

    #[cfg(feature = "embed")]
    #[test]
    fn the_embed_enums_accept_exactly_the_published_names() {
        use crate::embed::parity::search::TextGrouping;
        for name in TEXT_GROUPINGS {
            serde_json::from_value::<TextGrouping>(json!(name))
                .unwrap_or_else(|error| panic!("{name}: {error}"));
        }
        for grouping in [
            TextGrouping::File,
            TextGrouping::Symbol,
            TextGrouping::Usage,
            TextGrouping::Names,
            TextGrouping::Count,
        ] {
            let name = serde_json::to_value(grouping).expect("name");
            assert!(
                TEXT_GROUPINGS.contains(&name.as_str().expect("string")),
                "{name}"
            );
        }
    }
}