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}
250
251impl ToolSchema {
252    /// Create a new tool schema.
253    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
254        Self {
255            name: name.into(),
256            description: description.into(),
257            params: Vec::new(),
258            examples: Vec::new(),
259            map_positionals: false,
260            subcommands: Vec::new(),
261            aliases: Vec::new(),
262            owns_output: false,
263            raw_argv: false,
264        }
265    }
266
267    /// Declare that this tool wants its argv in source order with types
268    /// preserved (no flag/positional split). See [`ToolSchema::raw_argv`].
269    pub fn with_raw_argv(mut self) -> Self {
270        self.raw_argv = true;
271        self
272    }
273
274    /// Enable positional->named parameter mapping for MCP/external tools.
275    pub fn with_positional_mapping(mut self) -> Self {
276        self.map_positionals = true;
277        self
278    }
279
280    /// Add a parameter to the schema.
281    pub fn param(mut self, param: ParamSchema) -> Self {
282        self.params.push(param);
283        self
284    }
285
286    /// Add an example to the schema.
287    pub fn example(mut self, description: impl Into<String>, code: impl Into<String>) -> Self {
288        self.examples.push(Example::new(description, code));
289        self
290    }
291
292    /// Add a child schema, making this a subcommand-aware tool.
293    pub fn subcommand(mut self, child: ToolSchema) -> Self {
294        self.subcommands.push(child);
295        self
296    }
297
298    /// Set command-level aliases (e.g. `ls` for a `list` subcommand). These
299    /// name the *command*, not its flags; flag aliases live on each
300    /// [`ParamSchema`].
301    pub fn with_command_aliases(mut self, aliases: impl IntoIterator<Item = impl Into<String>>) -> Self {
302        self.aliases = aliases.into_iter().map(Into::into).collect();
303        self
304    }
305
306    /// True if `word` names this command — its `name` or any of its
307    /// command-level `aliases`. Used when routing a positional to a child.
308    pub fn matches_command(&self, word: &str) -> bool {
309        self.name == word || self.aliases.iter().any(|a| a == word)
310    }
311
312    /// Declare that this tool renders its own output (including `--json`), so
313    /// the kernel won't re-format its result.
314    ///
315    /// Applies to the whole tree: every subcommand is marked too, and a `json`
316    /// param is advertised on each node that doesn't already declare one.
317    /// Reflection skips `json` as the kernel-global output flag, so this
318    /// re-advertises it for tools that genuinely own it — closing the loop so
319    /// `help <tool> <sub>` lists `--json` where the tool actually handles it.
320    pub fn with_owned_output(mut self) -> Self {
321        self.mark_owned_output();
322        self
323    }
324
325    fn mark_owned_output(&mut self) {
326        self.owns_output = true;
327        if !self.params.iter().any(|p| p.name == "json") {
328            self.params.push(
329                ParamSchema::new("json", "bool").with_description("Render output as JSON"),
330            );
331        }
332        for child in &mut self.subcommands {
333            child.mark_owned_output();
334        }
335    }
336}
337
338/// Parsed arguments ready for tool execution.
339#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
340#[non_exhaustive]
341pub struct ToolArgs {
342    /// Positional arguments in order.
343    pub positional: Vec<Value>,
344    /// Named arguments by key.
345    pub named: BTreeMap<String, Value>,
346    /// Boolean flags (e.g., -l, --force).
347    pub flags: HashSet<String>,
348}
349
350impl ToolArgs {
351    /// Create empty args.
352    pub fn new() -> Self {
353        Self::default()
354    }
355
356    /// Get a positional argument by index.
357    pub fn get_positional(&self, index: usize) -> Option<&Value> {
358        self.positional.get(index)
359    }
360
361    /// Get a named argument by key.
362    pub fn get_named(&self, key: &str) -> Option<&Value> {
363        self.named.get(key)
364    }
365
366    /// Get a named argument or positional fallback.
367    ///
368    /// Useful for tools that accept both `cat file.txt` and `cat path=file.txt`.
369    pub fn get(&self, name: &str, positional_index: usize) -> Option<&Value> {
370        self.named.get(name).or_else(|| self.positional.get(positional_index))
371    }
372
373    /// Get a string value from args.
374    pub fn get_string(&self, name: &str, positional_index: usize) -> Option<String> {
375        self.get(name, positional_index).and_then(|v| match v {
376            Value::String(s) => Some(s.clone()),
377            Value::Int(i) => Some(i.to_string()),
378            Value::Float(f) => Some(f.to_string()),
379            Value::Bool(b) => Some(b.to_string()),
380            _ => None,
381        })
382    }
383
384    /// Get a boolean value from args.
385    pub fn get_bool(&self, name: &str, positional_index: usize) -> Option<bool> {
386        self.get(name, positional_index).and_then(|v| match v {
387            Value::Bool(b) => Some(*b),
388            Value::String(s) => match s.as_str() {
389                "true" | "yes" | "1" => Some(true),
390                "false" | "no" | "0" => Some(false),
391                _ => None,
392            },
393            Value::Int(i) => Some(*i != 0),
394            _ => None,
395        })
396    }
397
398    /// Check if a flag is set (in flags set, or named bool).
399    pub fn has_flag(&self, name: &str) -> bool {
400        // Check the flags set first (from -x or --name syntax)
401        if self.flags.contains(name) {
402            return true;
403        }
404        // Fall back to checking named args (from name=true syntax)
405        self.named.get(name).is_some_and(|v| match v {
406            Value::Bool(b) => *b,
407            Value::String(s) => !s.is_empty() && s != "false" && s != "0",
408            _ => true,
409        })
410    }
411
412    /// Move bool entries from `named` into the appropriate set so a downstream
413    /// clap parser (with `#[arg(...)] field: bool`) accepts them.
414    ///
415    /// Tests routinely seed `args.named.insert(K, Value::Bool(true))` for the
416    /// schema-pre-clap path; `to_argv()` would emit those as `--K=true`, which
417    /// clap rejects for `bool` fields. Promote to:
418    /// - `Bool(true)` → presence in `flags` (clap sees `--K`).
419    /// - `Bool(false)` → dropped (clap treats absent flag and explicit false
420    ///   the same; preserving it would only resurface as `--K=false` and break
421    ///   the same parser).
422    ///
423    /// A `Value::Bool` parked under a key the `schema` declares as a *value-taking*
424    /// flag is the flag's literal value, not a bare bool flag — `spawn --command
425    /// true` binds `command = Bool(true)`. Those keys are left in `named` so
426    /// `to_argv()` renders `--command=true` and clap's `Option<String>` field
427    /// accepts it; collapsing them to a bare `--command` drops the value and
428    /// makes clap error "a value is required". (See docs/issues.md.)
429    ///
430    /// Idempotent. Non-bool named entries are left alone.
431    pub fn flagify_bool_named(&mut self, schema: &ToolSchema) {
432        // Keys (param names + aliases) the schema declares as non-bool, non-positional
433        // flags — i.e. flags that take a value.
434        let value_keys: HashSet<&str> = schema
435            .params
436            .iter()
437            .filter(|p| !p.positional && !is_bool_param_type(&p.param_type))
438            .flat_map(|p| {
439                std::iter::once(p.name.as_str())
440                    .chain(p.aliases.iter().map(|a| a.trim_start_matches('-')))
441            })
442            .collect();
443
444        let bool_keys: Vec<String> = self
445            .named
446            .iter()
447            .filter(|(k, v)| matches!(v, Value::Bool(_)) && !value_keys.contains(k.as_str()))
448            .map(|(k, _)| k.clone())
449            .collect();
450        for k in bool_keys {
451            // Remove unconditionally so Bool(false) doesn't linger and break
452            // a `--K=false` rejection in clap. Only Bool(true) re-enters as a
453            // flag presence.
454            if let Some(Value::Bool(true)) = self.named.remove(&k) {
455                self.flags.insert(k);
456            }
457        }
458    }
459
460    /// Reconstruct a clap-friendly argv vector from already-parsed ToolArgs.
461    ///
462    /// kaish has already done shell parsing (variables expanded, globs expanded,
463    /// `$(...)` substituted, schema-driven flag/value splitting). `to_argv`
464    /// rebuilds a flat token stream suitable for `Parser::parse_from(std::iter::once("<tool>").chain(args.to_argv()))`.
465    ///
466    /// Layout: flags first (as `--<name>`), then named values (as
467    /// `--<name>=<value>`), then positionals — separated from earlier sections
468    /// by `--` so trailing-passthrough builtins still see them as positionals
469    /// even if a value happens to begin with `-`.
470    ///
471    /// See the clap builtin pattern in CLAUDE.md (Contributor conventions).
472    pub fn to_argv(&self) -> Vec<String> {
473        let mut argv = Vec::with_capacity(
474            self.flags.len() + self.named.len() * 2 + self.positional.len() + 1,
475        );
476
477        // Flags are unordered (HashSet); sort for deterministic argv so tests
478        // and snapshots stay stable. Single-char keys emit short form (`-n`)
479        // so clap's natural `#[arg(short = 'n', long = "no_newline")]` derive
480        // accepts them without needing visible_alias gymnastics.
481        let mut flags: Vec<&String> = self.flags.iter().collect();
482        flags.sort();
483        for flag in flags {
484            argv.push(flag_token(flag));
485        }
486
487        // Named values: emit `-k=value` for single-char keys and `--key=value`
488        // for multi-char keys. `=` form keeps parsing unambiguous when the
489        // value begins with `-`. Multi-value (`consumes > 1`) params are
490        // stored as Value::Json(Array(Array(...))) — one entry per occurrence.
491        for (key, value) in &self.named {
492            for rendered in render_named_value(value) {
493                argv.push(format!("{}={}", flag_token(key), rendered));
494            }
495        }
496
497        // `--` terminator so clap treats positionals as positionals even if
498        // they begin with `-` (e.g. `echo -- -n` should print `-n`).
499        if !self.positional.is_empty() {
500            argv.push("--".to_string());
501            for value in &self.positional {
502                argv.push(value_to_argv_token(value));
503            }
504        }
505
506        argv
507    }
508}
509
510fn flag_token(name: &str) -> String {
511    if name.chars().count() == 1 {
512        format!("-{name}")
513    } else {
514        format!("--{name}")
515    }
516}
517
518/// Whether a `ParamSchema::param_type` names a boolean flag.
519fn is_bool_param_type(param_type: &str) -> bool {
520    param_type.eq_ignore_ascii_case("bool") || param_type.eq_ignore_ascii_case("boolean")
521}
522
523fn render_named_value(value: &Value) -> Vec<String> {
524    match value {
525        // `consumes > 1` lands as Json(Array(Array(...))) — one inner array per
526        // occurrence. Flatten each inner array into space-joined tokens; clap
527        // can split on `=` further if needed.
528        Value::Json(serde_json::Value::Array(outer)) if outer.iter().all(|v| v.is_array()) => {
529            outer
530                .iter()
531                .map(|inner| {
532                    inner
533                        .as_array()
534                        .map(|a| a.iter().map(json_value_to_token).collect::<Vec<_>>().join(" "))
535                        .unwrap_or_default()
536                })
537                .collect()
538        }
539        _ => vec![value_to_argv_token(value)],
540    }
541}
542
543fn value_to_argv_token(value: &Value) -> String {
544    match value {
545        Value::Null => String::new(),
546        Value::Bool(b) => b.to_string(),
547        Value::Int(i) => i.to_string(),
548        Value::Float(f) => f.to_string(),
549        Value::String(s) => s.clone(),
550        Value::Json(j) => j.to_string(),
551        // Splatting binary into argv is a text context; a real loud-error guard
552        // lands with the arg-building rework (Phase 2). For now mark it visibly
553        // rather than emitting raw bytes. See docs/binary-data.md.
554        Value::Bytes(data) => format!("[binary: {} bytes]", data.len()),
555    }
556}
557
558fn json_value_to_token(value: &serde_json::Value) -> String {
559    match value {
560        serde_json::Value::Null => String::new(),
561        serde_json::Value::Bool(b) => b.to_string(),
562        serde_json::Value::Number(n) => n.to_string(),
563        serde_json::Value::String(s) => s.clone(),
564        other => other.to_string(),
565    }
566}
567
568#[cfg(test)]
569mod schema_serde_tests {
570    use super::*;
571
572    /// A flat tool (no subcommands/aliases) must serialize byte-identically to
573    /// the pre-subcommand wire format: the two new fields are skipped entirely.
574    #[test]
575    fn flat_schema_omits_new_fields_on_wire() {
576        let schema = ToolSchema::new("cat", "concatenate")
577            .param(ParamSchema::required("path", "string", "file to read").positional());
578        let json = serde_json::to_value(&schema).expect("serialize");
579        let obj = json.as_object().expect("object");
580        assert!(!obj.contains_key("subcommands"), "flat tool leaks subcommands: {json}");
581        assert!(!obj.contains_key("aliases"), "flat tool leaks command aliases: {json}");
582    }
583
584    /// Round-trip the skip: a flat tool serializes *without* the keys, so the
585    /// deserializer must `default` them back to empty. (This is what lets us
586    /// skip-serialize empties without breaking our own flat tools' payloads.)
587    #[test]
588    fn flat_wire_form_deserializes_to_empty() {
589        let flat = serde_json::json!({
590            "name": "cat",
591            "description": "concatenate",
592            "params": [],
593            "examples": [],
594            "map_positionals": false
595        });
596        let schema: ToolSchema = serde_json::from_value(flat).expect("deserialize flat form");
597        assert!(schema.subcommands.is_empty());
598        assert!(schema.aliases.is_empty());
599    }
600
601    /// `with_owned_output` marks the whole tree and advertises `json` on each
602    /// node that didn't already declare it.
603    #[test]
604    fn with_owned_output_marks_tree_and_advertises_json() {
605        let schema = ToolSchema::new("kj", "kaijutsu")
606            .subcommand(
607                ToolSchema::new("context", "ctx")
608                    .subcommand(ToolSchema::new("list", "list contexts")),
609            )
610            .with_owned_output();
611
612        assert!(schema.owns_output, "root marked");
613        assert!(schema.params.iter().any(|p| p.name == "json"), "root advertises json");
614        let context = &schema.subcommands[0];
615        assert!(context.owns_output, "child marked");
616        let list = &context.subcommands[0];
617        assert!(list.owns_output, "grandchild marked");
618        assert!(list.params.iter().any(|p| p.name == "json"), "leaf advertises json");
619    }
620
621    /// `with_owned_output` doesn't duplicate an already-declared `json` param.
622    #[test]
623    fn with_owned_output_does_not_double_add_json() {
624        let schema = ToolSchema::new("kj", "kaijutsu")
625            .param(ParamSchema::new("json", "bool"))
626            .with_owned_output();
627        let json_count = schema.params.iter().filter(|p| p.name == "json").count();
628        assert_eq!(json_count, 1, "json should appear exactly once");
629    }
630
631    /// `owns_output` round-trips and is omitted from the wire when false.
632    #[test]
633    fn owns_output_serde() {
634        let flat = ToolSchema::new("ls", "list");
635        let json = serde_json::to_value(&flat).expect("serialize");
636        let obj = json.as_object().expect("object");
637        assert!(!obj.contains_key("owns_output"), "false omitted: {json}");
638
639        let owned = ToolSchema::new("kj", "kaijutsu").with_owned_output();
640        let wire = serde_json::to_string(&owned).expect("serialize");
641        let back: ToolSchema = serde_json::from_str(&wire).expect("deserialize");
642        assert!(back.owns_output);
643    }
644
645    /// A subcommand tree round-trips through serde with names and aliases intact.
646    #[test]
647    fn subcommand_tree_round_trips() {
648        let schema = ToolSchema::new("kj", "kaijutsu")
649            .subcommand(
650                ToolSchema::new("context", "context ops")
651                    .with_command_aliases(["ctx"])
652                    .subcommand(ToolSchema::new("list", "list contexts").with_command_aliases(["ls"])),
653            );
654        let json = serde_json::to_string(&schema).expect("serialize");
655        let back: ToolSchema = serde_json::from_str(&json).expect("deserialize");
656        assert_eq!(back.subcommands.len(), 1);
657        let context = &back.subcommands[0];
658        assert!(context.matches_command("context"));
659        assert!(context.matches_command("ctx"));
660        assert_eq!(context.subcommands.len(), 1);
661        assert!(context.subcommands[0].matches_command("ls"));
662    }
663}
664
665#[cfg(test)]
666mod to_argv_tests {
667    use super::*;
668
669    #[test]
670    fn empty_args_produce_empty_argv() {
671        assert!(ToolArgs::new().to_argv().is_empty());
672    }
673
674    #[test]
675    fn positionals_emitted_after_double_dash() {
676        let mut args = ToolArgs::new();
677        args.positional.push(Value::String("hello".into()));
678        args.positional.push(Value::String("world".into()));
679        assert_eq!(args.to_argv(), vec!["--", "hello", "world"]);
680    }
681
682    #[test]
683    fn single_char_flags_emit_short_form() {
684        let mut args = ToolArgs::new();
685        args.flags.insert("n".into());
686        args.flags.insert("verbose".into());
687        // Sorted: "n" then "verbose"
688        assert_eq!(args.to_argv(), vec!["-n", "--verbose"]);
689    }
690
691    #[test]
692    fn named_values_use_equals_form() {
693        let mut args = ToolArgs::new();
694        args.named.insert("count".into(), Value::Int(5));
695        args.named.insert("name".into(), Value::String("foo".into()));
696        // BTreeMap iterates in key order, so "count" before "name"
697        assert_eq!(args.to_argv(), vec!["--count=5", "--name=foo"]);
698    }
699
700    #[test]
701    fn single_char_named_emits_short_equals() {
702        let mut args = ToolArgs::new();
703        args.named.insert("n".into(), Value::Int(5));
704        assert_eq!(args.to_argv(), vec!["-n=5"]);
705    }
706
707    #[test]
708    fn positional_with_leading_dash_survives_double_dash() {
709        let mut args = ToolArgs::new();
710        args.positional.push(Value::String("-n".into()));
711        // `echo -- -n` should round-trip as `-- -n`, not be reparsed as a flag.
712        assert_eq!(args.to_argv(), vec!["--", "-n"]);
713    }
714
715    #[test]
716    fn mixed_flags_named_positionals() {
717        let mut args = ToolArgs::new();
718        args.flags.insert("verbose".into());
719        args.named.insert("limit".into(), Value::Int(10));
720        args.positional.push(Value::String("file.txt".into()));
721        assert_eq!(
722            args.to_argv(),
723            vec!["--verbose", "--limit=10", "--", "file.txt"]
724        );
725    }
726
727    #[test]
728    fn flagify_bool_named_promotes_true_to_flag() {
729        let mut args = ToolArgs::new();
730        args.named.insert("recursive".into(), Value::Bool(true));
731        args.named.insert("limit".into(), Value::Int(5));
732
733        args.flagify_bool_named(&ToolSchema::new("t", ""));
734
735        assert!(args.flags.contains("recursive"));
736        assert!(!args.named.contains_key("recursive"));
737        // Non-bool entries are untouched.
738        assert_eq!(args.named.get("limit"), Some(&Value::Int(5)));
739    }
740
741    #[test]
742    fn flagify_bool_named_drops_false() {
743        let mut args = ToolArgs::new();
744        args.named.insert("recursive".into(), Value::Bool(false));
745
746        args.flagify_bool_named(&ToolSchema::new("t", ""));
747
748        assert!(!args.flags.contains("recursive"));
749        assert!(!args.named.contains_key("recursive"));
750    }
751
752    #[test]
753    fn flagify_bool_named_is_idempotent() {
754        let mut args = ToolArgs::new();
755        args.named.insert("recursive".into(), Value::Bool(true));
756        args.flagify_bool_named(&ToolSchema::new("t", ""));
757        args.flagify_bool_named(&ToolSchema::new("t", ""));
758        assert!(args.flags.contains("recursive"));
759    }
760
761    /// Regression guard: argv emitted after flagify must round-trip through
762    /// a clap parser without `--K=true` showing up.
763    #[test]
764    fn flagify_bool_named_round_trips_through_to_argv() {
765        let mut args = ToolArgs::new();
766        args.named.insert("R".into(), Value::Bool(true));
767        args.flagify_bool_named(&ToolSchema::new("t", ""));
768        let argv = args.to_argv();
769        assert!(argv.contains(&"-R".to_string()), "expected -R, got {:?}", argv);
770        assert!(!argv.iter().any(|s| s.contains('=')), "no =value should appear, got {:?}", argv);
771    }
772
773    /// A `Bool(true)` parked under a schema-declared value-taking flag is the
774    /// flag's literal value (`spawn --command true`), not a bare bool flag — it
775    /// stays in `named` and renders as `--K=true`, not a value-less `--K`.
776    #[test]
777    fn flagify_bool_named_keeps_value_flag_value() {
778        let mut schema = ToolSchema::new("spawn", "");
779        schema.params.push(ParamSchema::new("command", "string"));
780
781        let mut args = ToolArgs::new();
782        args.named.insert("command".into(), Value::Bool(true));
783        args.flagify_bool_named(&schema);
784
785        assert!(!args.flags.contains("command"), "value flag must not collapse to a bare flag");
786        assert_eq!(args.named.get("command"), Some(&Value::Bool(true)));
787        let argv = args.to_argv();
788        assert!(
789            argv.iter().any(|s| s == "--command=true"),
790            "expected --command=true, got {:?}",
791            argv
792        );
793    }
794
795    /// One schema carrying both a bool flag and a value-taking flag: the bool
796    /// flag still flagifies, the value flag keeps its value. Proves
797    /// `is_bool_param_type` actually distinguishes the two (an empty-schema test
798    /// can't — it flagifies everything regardless).
799    #[test]
800    fn flagify_bool_named_distinguishes_bool_from_value_param() {
801        let mut schema = ToolSchema::new("t", "");
802        schema.params.push(ParamSchema::new("verbose", "bool"));
803        schema.params.push(ParamSchema::new("command", "string"));
804
805        let mut args = ToolArgs::new();
806        args.named.insert("verbose".into(), Value::Bool(true));
807        args.named.insert("command".into(), Value::Bool(true));
808        args.flagify_bool_named(&schema);
809
810        // Bool flag → promoted to a bare flag.
811        assert!(args.flags.contains("verbose"));
812        assert!(!args.named.contains_key("verbose"));
813        // Value flag → value retained.
814        assert!(!args.flags.contains("command"));
815        assert_eq!(args.named.get("command"), Some(&Value::Bool(true)));
816    }
817}