Skip to main content

kaish_types/
tool.rs

1//! Tool schema and argument types.
2
3use std::collections::{BTreeMap, HashSet};
4
5use crate::value::Value;
6
7fn default_consumes() -> usize {
8    1
9}
10
11/// Schema for a tool parameter.
12#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
13#[non_exhaustive]
14pub struct ParamSchema {
15    /// Parameter name.
16    pub name: String,
17    /// Type hint (string, int, bool, array, object, any).
18    pub param_type: String,
19    /// Whether this parameter is required.
20    pub required: bool,
21    /// Default value if not required.
22    pub default: Option<Value>,
23    /// Description for help text.
24    pub description: String,
25    /// Alternative names/flags for this parameter (e.g., "-r", "-R" for "recursive").
26    pub aliases: Vec<String>,
27    /// Number of positional tokens this non-bool flag consumes per occurrence.
28    ///
29    /// Default 1 (standard `--flag value`). Set to 2 for `--flag NAME VALUE`
30    /// patterns such as jq's `--arg` / `--argjson`. When `consumes > 1`, the
31    /// kernel collects each occurrence as an inner array and accumulates
32    /// repeated occurrences under the same `named` key — the tool sees a
33    /// `Value::Json(Array(Array(...)))` listing every (N-tuple) occurrence.
34    #[serde(default = "default_consumes")]
35    pub consumes: usize,
36    /// True when this flag may appear more than once and each occurrence
37    /// should be kept (clap's `ArgAction::Append`, i.e. a `Vec<_>` value flag
38    /// like sed's `-e`). When set, the kernel accumulates every occurrence
39    /// under the same `named` key as a `Value::Json(Array(...))` instead of
40    /// letting the last write win — the "no silent drop" contract for repeated
41    /// flags. Orthogonal to `consumes`: `consumes` is values-per-occurrence,
42    /// `repeatable` is occurrences-per-invocation.
43    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
44    pub repeatable: bool,
45    /// True for positional arguments (`cat foo.txt`), false for flags
46    /// (`grep --ignore-case`). The validator matches positional params
47    /// against `args.positional` by their order *among positionals only*,
48    /// independent of where they sit in the clap struct. Default false so
49    /// hand-built `ParamSchema::required(...)` constructors keep flag
50    /// semantics; clap-reflected positionals set it via
51    /// `arg.get_index().is_some()`.
52    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
53    pub positional: bool,
54}
55
56impl ParamSchema {
57    /// Create a required parameter.
58    pub fn required(name: impl Into<String>, param_type: impl Into<String>, description: impl Into<String>) -> Self {
59        Self {
60            name: name.into(),
61            param_type: param_type.into(),
62            required: true,
63            default: None,
64            description: description.into(),
65            aliases: Vec::new(),
66            consumes: 1,
67            repeatable: false,
68            positional: false,
69        }
70    }
71
72    /// Create an optional parameter with a default value.
73    pub fn optional(name: impl Into<String>, param_type: impl Into<String>, default: Value, description: impl Into<String>) -> Self {
74        Self {
75            name: name.into(),
76            param_type: param_type.into(),
77            required: false,
78            default: Some(default),
79            description: description.into(),
80            aliases: Vec::new(),
81            consumes: 1,
82            repeatable: false,
83            positional: false,
84        }
85    }
86
87    /// Create a minimal parameter (not required, no default, empty
88    /// description, `consumes` 1, flag — not positional). Chain the `with_*`
89    /// setters to fill in fields. Use this when each field is computed
90    /// independently (e.g. reflected from clap) rather than fitting the
91    /// `required`/`optional` shortcuts. Keeps construction working across the
92    /// `#[non_exhaustive]` boundary.
93    pub fn new(name: impl Into<String>, param_type: impl Into<String>) -> Self {
94        Self {
95            name: name.into(),
96            param_type: param_type.into(),
97            required: false,
98            default: None,
99            description: String::new(),
100            aliases: Vec::new(),
101            consumes: 1,
102            repeatable: false,
103            positional: false,
104        }
105    }
106
107    /// Set the human-readable description.
108    pub fn with_description(mut self, description: impl Into<String>) -> Self {
109        self.description = description.into();
110        self
111    }
112
113    /// Set whether the parameter is required.
114    pub fn with_required(mut self, required: bool) -> Self {
115        self.required = required;
116        self
117    }
118
119    /// Set the default value (used when the parameter is omitted).
120    pub fn with_default(mut self, default: Option<Value>) -> Self {
121        self.default = default;
122        self
123    }
124
125    /// Set the positional flag from a computed boolean (the parameterless
126    /// [`positional`](Self::positional) sets it unconditionally to `true`).
127    pub fn with_positional(mut self, positional: bool) -> Self {
128        self.positional = positional;
129        self
130    }
131
132    /// Mark this parameter as positional (matched by argv order rather than
133    /// by name). Used by `params_from_clap` for clap args with an assigned
134    /// index, and by hand-written schemas for positional parameters like
135    /// jq's `filter`.
136    pub fn positional(mut self) -> Self {
137        self.positional = true;
138        self
139    }
140
141    /// Add alternative names/flags for this parameter.
142    ///
143    /// Aliases are used for short flags like `-r`, `-R` that map to `recursive`.
144    pub fn with_aliases(mut self, aliases: impl IntoIterator<Item = impl Into<String>>) -> Self {
145        self.aliases = aliases.into_iter().map(Into::into).collect();
146        self
147    }
148
149    /// Declare how many positional tokens this non-bool flag consumes per
150    /// occurrence (`--flag v1 v2 ...`). Default is 1. Panics on 0 — a flag
151    /// that consumes nothing is a bool flag, not a schema-typed param.
152    pub fn consumes(mut self, n: usize) -> Self {
153        assert!(n >= 1, "ParamSchema::consumes requires n >= 1 (use a bool param for flags that take no value)");
154        self.consumes = n;
155        self
156    }
157
158    /// Mark this flag as repeatable: each occurrence is accumulated rather than
159    /// overwritten (see [`repeatable`](Self::repeatable)). Set from a computed
160    /// boolean so clap reflection can pass `ArgAction::Append` directly.
161    pub fn with_repeatable(mut self, repeatable: bool) -> Self {
162        self.repeatable = repeatable;
163        self
164    }
165
166    /// Check if a flag name matches this parameter or any of its aliases.
167    pub fn matches_flag(&self, flag: &str) -> bool {
168        if self.name == flag {
169            return true;
170        }
171        self.aliases.iter().any(|a| a == flag)
172    }
173}
174
175/// An example showing how to use a tool.
176#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
177pub struct Example {
178    /// Short description of what the example demonstrates.
179    pub description: String,
180    /// The example command/code.
181    pub code: String,
182}
183
184impl Example {
185    /// Create a new example.
186    pub fn new(description: impl Into<String>, code: impl Into<String>) -> Self {
187        Self {
188            description: description.into(),
189            code: code.into(),
190        }
191    }
192}
193
194/// Schema describing a tool's interface.
195#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
196#[non_exhaustive]
197pub struct ToolSchema {
198    /// Tool name.
199    pub name: String,
200    /// Short description.
201    pub description: String,
202    /// Parameter definitions.
203    pub params: Vec<ParamSchema>,
204    /// Usage examples.
205    pub examples: Vec<Example>,
206    /// Map remaining positional args to named params by schema order.
207    /// Only for MCP/external tools that expect named JSON params.
208    /// Builtins handle their own positionals and should leave this false.
209    pub map_positionals: bool,
210    /// Child schemas for subcommand-aware tools (`kj context list`, …).
211    ///
212    /// Empty for flat tools (`cat`, `grep`, `ls`) — they take the flat binding
213    /// path. When non-empty, the kernel walks leading positionals to pick the
214    /// active leaf and binds flags against *that leaf's* `params` (see
215    /// `select_leaf` in the kernel).
216    ///
217    /// `skip_serializing_if` keeps the wire compact for the many flat tools
218    /// (no `"subcommands":[]` noise); `default` is then required so a flat
219    /// tool's payload (key absent) deserializes back to empty.
220    #[serde(default, skip_serializing_if = "Vec::is_empty")]
221    pub subcommands: Vec<ToolSchema>,
222    /// Command-level aliases (`ls` → `list`, `rm` → `remove`), matched when
223    /// routing a positional to a child. Distinct from [`ParamSchema::aliases`],
224    /// which name *flags*.
225    #[serde(default, skip_serializing_if = "Vec::is_empty")]
226    pub aliases: Vec<String>,
227    /// The tool renders its **own** output, including `--json` — the kernel
228    /// must not re-format its `ExecResult` through `apply_output_format`.
229    ///
230    /// Default false: a tool returns typed [`crate::OutputData`] and the kernel
231    /// renders the requested format uniformly. Set true for tools with bespoke
232    /// JSON envelopes (e.g. an embedder's `kj`): they consume `--json`
233    /// themselves and emit final bytes. See [`ToolSchema::with_owned_output`].
234    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
235    pub owns_output: bool,
236    /// The tool wants its argv **in source order, with types preserved** — the
237    /// binder must NOT split flags into the unordered `flags` set. When true,
238    /// every argument is bound to `positional` in the order written (operators
239    /// like `-f`/`=`/`!` as strings, operands keeping their `Value` type), and
240    /// `named`/`flags` stay empty.
241    ///
242    /// Default false: normal tools get the clap-style order-independent split
243    /// (`-la` == `-al`). Set true for the rare *position-sensitive* command
244    /// whose operands may themselves look like flags — POSIX `test`, where
245    /// `test $x = -n` and `test 0 -gt -5` must see `-n`/`-5` as literal
246    /// operands. See [`ToolSchema::with_raw_argv`].
247    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
248    pub raw_argv: bool,
249    /// The tool consumes glob patterns **as data** — the argv binder must pass
250    /// a bare glob pattern through as literal text instead of expanding it to
251    /// matching paths.
252    ///
253    /// Default false: shell semantics — `cat *.rs` sees matching files and
254    /// zero matches is a bind-time error. Set true for a tool whose input *is*
255    /// the pattern (`glob`), so the natural unquoted spelling
256    /// (`glob **/*.rs`) hands the pattern text to the tool instead of walking
257    /// the tree at bind time and binding the first match as the "pattern".
258    /// See [`ToolSchema::with_glob_passthrough`].
259    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
260    pub glob_passthrough: bool,
261}
262
263impl ToolSchema {
264    /// Create a new tool schema.
265    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
266        Self {
267            name: name.into(),
268            description: description.into(),
269            params: Vec::new(),
270            examples: Vec::new(),
271            map_positionals: false,
272            subcommands: Vec::new(),
273            aliases: Vec::new(),
274            owns_output: false,
275            raw_argv: false,
276            glob_passthrough: false,
277        }
278    }
279
280    /// Declare that this tool wants its argv in source order with types
281    /// preserved (no flag/positional split). See [`ToolSchema::raw_argv`].
282    pub fn with_raw_argv(mut self) -> Self {
283        self.raw_argv = true;
284        self
285    }
286
287    /// Declare that this tool consumes glob patterns as data: the argv binder
288    /// passes bare patterns through as literal text instead of expanding them.
289    /// See [`ToolSchema::glob_passthrough`].
290    pub fn with_glob_passthrough(mut self) -> Self {
291        self.glob_passthrough = true;
292        self
293    }
294
295    /// Enable positional->named parameter mapping for MCP/external tools.
296    pub fn with_positional_mapping(mut self) -> Self {
297        self.map_positionals = true;
298        self
299    }
300
301    /// Add a parameter to the schema.
302    pub fn param(mut self, param: ParamSchema) -> Self {
303        self.params.push(param);
304        self
305    }
306
307    /// Add an example to the schema.
308    pub fn example(mut self, description: impl Into<String>, code: impl Into<String>) -> Self {
309        self.examples.push(Example::new(description, code));
310        self
311    }
312
313    /// Add a child schema, making this a subcommand-aware tool.
314    pub fn subcommand(mut self, child: ToolSchema) -> Self {
315        self.subcommands.push(child);
316        self
317    }
318
319    /// Set command-level aliases (e.g. `ls` for a `list` subcommand). These
320    /// name the *command*, not its flags; flag aliases live on each
321    /// [`ParamSchema`].
322    pub fn with_command_aliases(mut self, aliases: impl IntoIterator<Item = impl Into<String>>) -> Self {
323        self.aliases = aliases.into_iter().map(Into::into).collect();
324        self
325    }
326
327    /// True if `word` names this command — its `name` or any of its
328    /// command-level `aliases`. Used when routing a positional to a child.
329    pub fn matches_command(&self, word: &str) -> bool {
330        self.name == word || self.aliases.iter().any(|a| a == word)
331    }
332
333    /// Declare that this tool renders its own output (including `--json`), so
334    /// the kernel won't re-format its result.
335    ///
336    /// Applies to the whole tree: every subcommand is marked too, and a `json`
337    /// param is advertised on each node that doesn't already declare one.
338    /// Reflection skips `json` as the kernel-global output flag, so this
339    /// re-advertises it for tools that genuinely own it — closing the loop so
340    /// `help <tool> <sub>` lists `--json` where the tool actually handles it.
341    pub fn with_owned_output(mut self) -> Self {
342        self.mark_owned_output();
343        self
344    }
345
346    fn mark_owned_output(&mut self) {
347        self.owns_output = true;
348        if !self.params.iter().any(|p| p.name == "json") {
349            self.params.push(
350                ParamSchema::new("json", "bool").with_description("Render output as JSON"),
351            );
352        }
353        for child in &mut self.subcommands {
354            child.mark_owned_output();
355        }
356    }
357}
358
359/// Parsed arguments ready for tool execution.
360#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
361#[non_exhaustive]
362pub struct ToolArgs {
363    /// Positional arguments in order.
364    pub positional: Vec<Value>,
365    /// Named arguments by key.
366    pub named: BTreeMap<String, Value>,
367    /// Boolean flags (e.g., -l, --force).
368    pub flags: HashSet<String>,
369}
370
371impl ToolArgs {
372    /// Create empty args.
373    pub fn new() -> Self {
374        Self::default()
375    }
376
377    /// Get a positional argument by index.
378    pub fn get_positional(&self, index: usize) -> Option<&Value> {
379        self.positional.get(index)
380    }
381
382    /// Get a named argument by key.
383    pub fn get_named(&self, key: &str) -> Option<&Value> {
384        self.named.get(key)
385    }
386
387    /// Get a named argument or positional fallback.
388    ///
389    /// Useful for tools that accept both `cat file.txt` and `cat path=file.txt`.
390    pub fn get(&self, name: &str, positional_index: usize) -> Option<&Value> {
391        self.named.get(name).or_else(|| self.positional.get(positional_index))
392    }
393
394    /// Get a string value from args.
395    pub fn get_string(&self, name: &str, positional_index: usize) -> Option<String> {
396        self.get(name, positional_index).and_then(|v| match v {
397            Value::String(s) => Some(s.clone()),
398            Value::Int(i) => Some(i.to_string()),
399            Value::Float(f) => Some(f.to_string()),
400            Value::Bool(b) => Some(b.to_string()),
401            _ => None,
402        })
403    }
404
405    /// Get a boolean value from args.
406    pub fn get_bool(&self, name: &str, positional_index: usize) -> Option<bool> {
407        self.get(name, positional_index).and_then(|v| match v {
408            Value::Bool(b) => Some(*b),
409            Value::String(s) => match s.as_str() {
410                "true" | "yes" | "1" => Some(true),
411                "false" | "no" | "0" => Some(false),
412                _ => None,
413            },
414            Value::Int(i) => Some(*i != 0),
415            _ => None,
416        })
417    }
418
419    /// Check if a flag is set (in flags set, or named bool).
420    pub fn has_flag(&self, name: &str) -> bool {
421        // Check the flags set first (from -x or --name syntax)
422        if self.flags.contains(name) {
423            return true;
424        }
425        // Fall back to checking named args (from name=true syntax)
426        self.named.get(name).is_some_and(|v| match v {
427            Value::Bool(b) => *b,
428            Value::String(s) => !s.is_empty() && s != "false" && s != "0",
429            _ => true,
430        })
431    }
432
433    /// Move bool entries from `named` into the appropriate set so a downstream
434    /// clap parser (with `#[arg(...)] field: bool`) accepts them.
435    ///
436    /// Tests routinely seed `args.named.insert(K, Value::Bool(true))` for the
437    /// schema-pre-clap path; `to_argv()` would emit those as `--K=true`, which
438    /// clap rejects for `bool` fields. Promote to:
439    /// - `Bool(true)` → presence in `flags` (clap sees `--K`).
440    /// - `Bool(false)` → dropped (clap treats absent flag and explicit false
441    ///   the same; preserving it would only resurface as `--K=false` and break
442    ///   the same parser).
443    ///
444    /// A `Value::Bool` parked under a key the `schema` declares as a *value-taking*
445    /// flag is the flag's literal value, not a bare bool flag — `spawn --command
446    /// true` binds `command = Bool(true)`. Those keys are left in `named` so
447    /// `to_argv()` renders `--command=true` and clap's `Option<String>` field
448    /// accepts it; collapsing them to a bare `--command` drops the value and
449    /// makes clap error "a value is required". (See docs/issues.md.)
450    ///
451    /// Idempotent. Non-bool named entries are left alone.
452    pub fn flagify_bool_named(&mut self, schema: &ToolSchema) {
453        // Keys (param names + aliases) the schema declares as non-bool, non-positional
454        // flags — i.e. flags that take a value.
455        let value_keys: HashSet<&str> = schema
456            .params
457            .iter()
458            .filter(|p| !p.positional && !is_bool_param_type(&p.param_type))
459            .flat_map(|p| {
460                std::iter::once(p.name.as_str())
461                    .chain(p.aliases.iter().map(|a| a.trim_start_matches('-')))
462            })
463            .collect();
464
465        let bool_keys: Vec<String> = self
466            .named
467            .iter()
468            .filter(|(k, v)| matches!(v, Value::Bool(_)) && !value_keys.contains(k.as_str()))
469            .map(|(k, _)| k.clone())
470            .collect();
471        for k in bool_keys {
472            // Remove unconditionally so Bool(false) doesn't linger and break
473            // a `--K=false` rejection in clap. Only Bool(true) re-enters as a
474            // flag presence.
475            if let Some(Value::Bool(true)) = self.named.remove(&k) {
476                self.flags.insert(k);
477            }
478        }
479    }
480
481    /// Reconstruct a clap-friendly argv vector from already-parsed ToolArgs.
482    ///
483    /// kaish has already done shell parsing (variables expanded, globs expanded,
484    /// `$(...)` substituted, schema-driven flag/value splitting). `to_argv`
485    /// rebuilds a flat token stream suitable for `Parser::parse_from(std::iter::once("<tool>").chain(args.to_argv()))`.
486    ///
487    /// Layout: flags first (as `--<name>`), then named values (as
488    /// `--<name>=<value>`), then positionals — separated from earlier sections
489    /// by `--` so trailing-passthrough builtins still see them as positionals
490    /// even if a value happens to begin with `-`.
491    ///
492    /// See the clap builtin pattern in CLAUDE.md (Contributor conventions).
493    pub fn to_argv(&self) -> Vec<String> {
494        let mut argv = Vec::with_capacity(
495            self.flags.len() + self.named.len() * 2 + self.positional.len() + 1,
496        );
497
498        // Flags are unordered (HashSet); sort for deterministic argv so tests
499        // and snapshots stay stable. Single-char keys emit short form (`-n`)
500        // so clap's natural `#[arg(short = 'n', long = "no_newline")]` derive
501        // accepts them without needing visible_alias gymnastics.
502        let mut flags: Vec<&String> = self.flags.iter().collect();
503        flags.sort();
504        for flag in flags {
505            argv.push(flag_token(flag));
506        }
507
508        // Named values: emit `-k=value` for single-char keys and `--key=value`
509        // for multi-char keys. `=` form keeps parsing unambiguous when the
510        // value begins with `-`. Multi-value (`consumes > 1`) params are
511        // stored as Value::Json(Array(Array(...))) — one entry per occurrence.
512        for (key, value) in &self.named {
513            for rendered in render_named_value(value) {
514                argv.push(format!("{}={}", flag_token(key), rendered));
515            }
516        }
517
518        // `--` terminator so clap treats positionals as positionals even if
519        // they begin with `-` (e.g. `echo -- -n` should print `-n`).
520        if !self.positional.is_empty() {
521            argv.push("--".to_string());
522            for value in &self.positional {
523                argv.push(value_to_argv_token(value));
524            }
525        }
526
527        argv
528    }
529}
530
531fn flag_token(name: &str) -> String {
532    if name.chars().count() == 1 {
533        format!("-{name}")
534    } else {
535        format!("--{name}")
536    }
537}
538
539/// Whether a `ParamSchema::param_type` names a boolean flag.
540fn is_bool_param_type(param_type: &str) -> bool {
541    param_type.eq_ignore_ascii_case("bool") || param_type.eq_ignore_ascii_case("boolean")
542}
543
544fn render_named_value(value: &Value) -> Vec<String> {
545    match value {
546        // `consumes > 1` lands as Json(Array(Array(...))) — one inner array per
547        // occurrence. Flatten each inner array into space-joined tokens; clap
548        // can split on `=` further if needed.
549        Value::Json(serde_json::Value::Array(outer)) if outer.iter().all(|v| v.is_array()) => {
550            outer
551                .iter()
552                .map(|inner| {
553                    inner
554                        .as_array()
555                        .map(|a| a.iter().map(json_value_to_token).collect::<Vec<_>>().join(" "))
556                        .unwrap_or_default()
557                })
558                .collect()
559        }
560        _ => vec![value_to_argv_token(value)],
561    }
562}
563
564fn value_to_argv_token(value: &Value) -> String {
565    match value {
566        Value::Null => String::new(),
567        Value::Bool(b) => b.to_string(),
568        Value::Int(i) => i.to_string(),
569        Value::Float(f) => f.to_string(),
570        Value::String(s) => s.clone(),
571        Value::Json(j) => j.to_string(),
572        // Splatting binary into argv is a text context; a real loud-error guard
573        // lands with the arg-building rework (Phase 2). For now mark it visibly
574        // rather than emitting raw bytes. See docs/binary-data.md.
575        Value::Bytes(data) => format!("[binary: {} bytes]", data.len()),
576    }
577}
578
579fn json_value_to_token(value: &serde_json::Value) -> String {
580    match value {
581        serde_json::Value::Null => String::new(),
582        serde_json::Value::Bool(b) => b.to_string(),
583        serde_json::Value::Number(n) => n.to_string(),
584        serde_json::Value::String(s) => s.clone(),
585        other => other.to_string(),
586    }
587}
588
589#[cfg(test)]
590mod schema_serde_tests {
591    use super::*;
592
593    /// A flat tool (no subcommands/aliases) must serialize byte-identically to
594    /// the pre-subcommand wire format: the two new fields are skipped entirely.
595    #[test]
596    fn flat_schema_omits_new_fields_on_wire() {
597        let schema = ToolSchema::new("cat", "concatenate")
598            .param(ParamSchema::required("path", "string", "file to read").positional());
599        let json = serde_json::to_value(&schema).expect("serialize");
600        let obj = json.as_object().expect("object");
601        assert!(!obj.contains_key("subcommands"), "flat tool leaks subcommands: {json}");
602        assert!(!obj.contains_key("aliases"), "flat tool leaks command aliases: {json}");
603    }
604
605    /// Round-trip the skip: a flat tool serializes *without* the keys, so the
606    /// deserializer must `default` them back to empty. (This is what lets us
607    /// skip-serialize empties without breaking our own flat tools' payloads.)
608    #[test]
609    fn flat_wire_form_deserializes_to_empty() {
610        let flat = serde_json::json!({
611            "name": "cat",
612            "description": "concatenate",
613            "params": [],
614            "examples": [],
615            "map_positionals": false
616        });
617        let schema: ToolSchema = serde_json::from_value(flat).expect("deserialize flat form");
618        assert!(schema.subcommands.is_empty());
619        assert!(schema.aliases.is_empty());
620    }
621
622    /// `with_owned_output` marks the whole tree and advertises `json` on each
623    /// node that didn't already declare it.
624    #[test]
625    fn with_owned_output_marks_tree_and_advertises_json() {
626        let schema = ToolSchema::new("kj", "kaijutsu")
627            .subcommand(
628                ToolSchema::new("context", "ctx")
629                    .subcommand(ToolSchema::new("list", "list contexts")),
630            )
631            .with_owned_output();
632
633        assert!(schema.owns_output, "root marked");
634        assert!(schema.params.iter().any(|p| p.name == "json"), "root advertises json");
635        let context = &schema.subcommands[0];
636        assert!(context.owns_output, "child marked");
637        let list = &context.subcommands[0];
638        assert!(list.owns_output, "grandchild marked");
639        assert!(list.params.iter().any(|p| p.name == "json"), "leaf advertises json");
640    }
641
642    /// `with_owned_output` doesn't duplicate an already-declared `json` param.
643    #[test]
644    fn with_owned_output_does_not_double_add_json() {
645        let schema = ToolSchema::new("kj", "kaijutsu")
646            .param(ParamSchema::new("json", "bool"))
647            .with_owned_output();
648        let json_count = schema.params.iter().filter(|p| p.name == "json").count();
649        assert_eq!(json_count, 1, "json should appear exactly once");
650    }
651
652    /// `owns_output` round-trips and is omitted from the wire when false.
653    #[test]
654    fn owns_output_serde() {
655        let flat = ToolSchema::new("ls", "list");
656        let json = serde_json::to_value(&flat).expect("serialize");
657        let obj = json.as_object().expect("object");
658        assert!(!obj.contains_key("owns_output"), "false omitted: {json}");
659
660        let owned = ToolSchema::new("kj", "kaijutsu").with_owned_output();
661        let wire = serde_json::to_string(&owned).expect("serialize");
662        let back: ToolSchema = serde_json::from_str(&wire).expect("deserialize");
663        assert!(back.owns_output);
664    }
665
666    /// A subcommand tree round-trips through serde with names and aliases intact.
667    #[test]
668    fn subcommand_tree_round_trips() {
669        let schema = ToolSchema::new("kj", "kaijutsu")
670            .subcommand(
671                ToolSchema::new("context", "context ops")
672                    .with_command_aliases(["ctx"])
673                    .subcommand(ToolSchema::new("list", "list contexts").with_command_aliases(["ls"])),
674            );
675        let json = serde_json::to_string(&schema).expect("serialize");
676        let back: ToolSchema = serde_json::from_str(&json).expect("deserialize");
677        assert_eq!(back.subcommands.len(), 1);
678        let context = &back.subcommands[0];
679        assert!(context.matches_command("context"));
680        assert!(context.matches_command("ctx"));
681        assert_eq!(context.subcommands.len(), 1);
682        assert!(context.subcommands[0].matches_command("ls"));
683    }
684}
685
686#[cfg(test)]
687mod to_argv_tests {
688    use super::*;
689
690    #[test]
691    fn empty_args_produce_empty_argv() {
692        assert!(ToolArgs::new().to_argv().is_empty());
693    }
694
695    #[test]
696    fn positionals_emitted_after_double_dash() {
697        let mut args = ToolArgs::new();
698        args.positional.push(Value::String("hello".into()));
699        args.positional.push(Value::String("world".into()));
700        assert_eq!(args.to_argv(), vec!["--", "hello", "world"]);
701    }
702
703    #[test]
704    fn single_char_flags_emit_short_form() {
705        let mut args = ToolArgs::new();
706        args.flags.insert("n".into());
707        args.flags.insert("verbose".into());
708        // Sorted: "n" then "verbose"
709        assert_eq!(args.to_argv(), vec!["-n", "--verbose"]);
710    }
711
712    #[test]
713    fn named_values_use_equals_form() {
714        let mut args = ToolArgs::new();
715        args.named.insert("count".into(), Value::Int(5));
716        args.named.insert("name".into(), Value::String("foo".into()));
717        // BTreeMap iterates in key order, so "count" before "name"
718        assert_eq!(args.to_argv(), vec!["--count=5", "--name=foo"]);
719    }
720
721    #[test]
722    fn single_char_named_emits_short_equals() {
723        let mut args = ToolArgs::new();
724        args.named.insert("n".into(), Value::Int(5));
725        assert_eq!(args.to_argv(), vec!["-n=5"]);
726    }
727
728    #[test]
729    fn positional_with_leading_dash_survives_double_dash() {
730        let mut args = ToolArgs::new();
731        args.positional.push(Value::String("-n".into()));
732        // `echo -- -n` should round-trip as `-- -n`, not be reparsed as a flag.
733        assert_eq!(args.to_argv(), vec!["--", "-n"]);
734    }
735
736    #[test]
737    fn mixed_flags_named_positionals() {
738        let mut args = ToolArgs::new();
739        args.flags.insert("verbose".into());
740        args.named.insert("limit".into(), Value::Int(10));
741        args.positional.push(Value::String("file.txt".into()));
742        assert_eq!(
743            args.to_argv(),
744            vec!["--verbose", "--limit=10", "--", "file.txt"]
745        );
746    }
747
748    #[test]
749    fn flagify_bool_named_promotes_true_to_flag() {
750        let mut args = ToolArgs::new();
751        args.named.insert("recursive".into(), Value::Bool(true));
752        args.named.insert("limit".into(), Value::Int(5));
753
754        args.flagify_bool_named(&ToolSchema::new("t", ""));
755
756        assert!(args.flags.contains("recursive"));
757        assert!(!args.named.contains_key("recursive"));
758        // Non-bool entries are untouched.
759        assert_eq!(args.named.get("limit"), Some(&Value::Int(5)));
760    }
761
762    #[test]
763    fn flagify_bool_named_drops_false() {
764        let mut args = ToolArgs::new();
765        args.named.insert("recursive".into(), Value::Bool(false));
766
767        args.flagify_bool_named(&ToolSchema::new("t", ""));
768
769        assert!(!args.flags.contains("recursive"));
770        assert!(!args.named.contains_key("recursive"));
771    }
772
773    #[test]
774    fn flagify_bool_named_is_idempotent() {
775        let mut args = ToolArgs::new();
776        args.named.insert("recursive".into(), Value::Bool(true));
777        args.flagify_bool_named(&ToolSchema::new("t", ""));
778        args.flagify_bool_named(&ToolSchema::new("t", ""));
779        assert!(args.flags.contains("recursive"));
780    }
781
782    /// Regression guard: argv emitted after flagify must round-trip through
783    /// a clap parser without `--K=true` showing up.
784    #[test]
785    fn flagify_bool_named_round_trips_through_to_argv() {
786        let mut args = ToolArgs::new();
787        args.named.insert("R".into(), Value::Bool(true));
788        args.flagify_bool_named(&ToolSchema::new("t", ""));
789        let argv = args.to_argv();
790        assert!(argv.contains(&"-R".to_string()), "expected -R, got {:?}", argv);
791        assert!(!argv.iter().any(|s| s.contains('=')), "no =value should appear, got {:?}", argv);
792    }
793
794    /// A `Bool(true)` parked under a schema-declared value-taking flag is the
795    /// flag's literal value (`spawn --command true`), not a bare bool flag — it
796    /// stays in `named` and renders as `--K=true`, not a value-less `--K`.
797    #[test]
798    fn flagify_bool_named_keeps_value_flag_value() {
799        let mut schema = ToolSchema::new("spawn", "");
800        schema.params.push(ParamSchema::new("command", "string"));
801
802        let mut args = ToolArgs::new();
803        args.named.insert("command".into(), Value::Bool(true));
804        args.flagify_bool_named(&schema);
805
806        assert!(!args.flags.contains("command"), "value flag must not collapse to a bare flag");
807        assert_eq!(args.named.get("command"), Some(&Value::Bool(true)));
808        let argv = args.to_argv();
809        assert!(
810            argv.iter().any(|s| s == "--command=true"),
811            "expected --command=true, got {:?}",
812            argv
813        );
814    }
815
816    /// One schema carrying both a bool flag and a value-taking flag: the bool
817    /// flag still flagifies, the value flag keeps its value. Proves
818    /// `is_bool_param_type` actually distinguishes the two (an empty-schema test
819    /// can't — it flagifies everything regardless).
820    #[test]
821    fn flagify_bool_named_distinguishes_bool_from_value_param() {
822        let mut schema = ToolSchema::new("t", "");
823        schema.params.push(ParamSchema::new("verbose", "bool"));
824        schema.params.push(ParamSchema::new("command", "string"));
825
826        let mut args = ToolArgs::new();
827        args.named.insert("verbose".into(), Value::Bool(true));
828        args.named.insert("command".into(), Value::Bool(true));
829        args.flagify_bool_named(&schema);
830
831        // Bool flag → promoted to a bare flag.
832        assert!(args.flags.contains("verbose"));
833        assert!(!args.named.contains_key("verbose"));
834        // Value flag → value retained.
835        assert!(!args.flags.contains("command"));
836        assert_eq!(args.named.get("command"), Some(&Value::Bool(true)));
837    }
838}