Skip to content

A wrapped command can declare a subcommand group - #433

Open
tobert wants to merge 1 commit into
mainfrom
feat/wrapped-nested-verbs
Open

A wrapped command can declare a subcommand group#433
tobert wants to merge 1 commit into
mainfrom
feat/wrapped-nested-verbs

Conversation

@tobert

@tobert tobert commented Sep 1, 2026

Copy link
Copy Markdown
Owner

git worktree list was undeclarable. Verb carried flags and positionals but no child verbs, so a wrapped command could declare worktree and never list under it. A subcommand group is the ordinary shape of a real program — docker, kubectl, gh, and git itself — and wrapped commands are 0.17.0's headline feature.

Verb::verb() declares a child, the same builder the top level uses:

.verb(
    Verb::new("worktree")
        .verb(Verb::new("list").flag(Flag::switch("porcelain")))
        .verb(Verb::new("prune")),
)

A verb with children is a node. A node selects among its children and never runs itself:

git worktree
  git: 'git worktree' needs a verb. Allowed: add, list, lock, prune, remove

git worktree frobnicate
  git: unknown verb 'frobnicate' for 'git worktree'. Allowed: add, list, lock, prune, remove

git worktree list --porcelain
  argv: ["/usr/bin/git", "--no-pager", "worktree", "list", "--porcelain"]

The allowed set in a node-scoped refusal names that node's own children, never the top level's. That is the property nesting exists to give an agent: the next word to try is drawn from the set it actually chose into. A test asserts the top-level verbs are absent from a node-scoped message, which is the assertion that discriminates.

lead and omit_name are meaningful at every level and concatenate down the path. flags, positionals, tail, and stdin belong to the leaf that runs, and a node declaring one is refused at build() rather than ignored. A flag on a node is ambiguous against its leaves' flags; the restriction can relax later, but a wrong binding cannot be un-shipped. The root verb may not take children either — it is the nameless verb, so it would have no word to select with.

Call.verb_index became a path. All 201 existing wrapped-command tests pass unmodified.

Gates: clippy -D warnings, cargo test --all (6533 passed, 0 failed), insta --check, no-default-features, rustdoc -D warnings.

wrapped.rs:381's Verb had name, lead, flags, positionals, tail, and
stdin but no children, so a two-level grammar was undeclarable:
git worktree list could only be one flat verb string, and every
subcommand-group program (docker, kubectl, git itself) was out of
reach for the allowlist that is 0.17.0's headline feature.

Verb gains a recursive verbs: Vec<Verb> and a .verb() builder
mirroring WrappedCommand::verb(). A verb with children is a node: it
selects among them and is never itself callable. Calling one bare
(git worktree) now refuses at plan_call, exit 2, naming its own
children before anything spawns. An unknown or missing leaf under a
node names that node's own children, never the top level's or a
sibling node's — the property nesting exists to give an agent, since
a leaked top-level set would actively misdirect the next attempt.

Call.verb_index: Option<usize> — a flat index into declaration.verbs
— became Call.verb_path: Vec<usize>, a chain of indices walked one
level at a time (empty selects the root). select_verb's return type
grew to match; a new descend() helper keeps consuming words for as
long as the matched verb has children, reusing the exact opaque-word
and dash-prefix handling select_verb already had at the top level,
so the same "the parser describes nothing" rule now applies at every
depth rather than just the first word. scope_of_path() replaces
scope_of() everywhere a Call's resolved verb needs a label, so an
error or a spawn label reads "git worktree list", not "git list".

lead concatenates down the path in path order (verb_chain() in
render.rs walks the whole chain, not just the leaf); flags,
positionals, tail, and stdin stay leaf-only, refused at build() on a
node with the property and the fix named. The root verb may not
declare children either — nesting under root would make a path
index ambiguous between declaration.verbs and root.verbs, so it is
refused rather than half-supported. verb_schema() and
every_verb_is_json() both walk to leaves now, so a node's schema
carries its own children as nested ToolSchemas and typed
substitution still turns on only when every leaf a tool can run
declares json_output().

Every existing wrapped-command test still passes unmodified: 75 in
tools/wrapped/tests.rs, 94 in wrapped_command_parse_tests.rs, 32 in
wrapped_command_exec_tests.rs. New coverage adds a git-worktree
fixture with a sibling top-level verb (so a node-scoped refusal has
a real name to leak, if it still could), a three-level fixture, six
build()-time refusal tests for the node restrictions, two schema-
recursion tests, and two end-to-end exec tests spawning a real child
through a nested path.

docs/wrapped_command.md gains a "Nested verbs" section between the
cargo and TOML examples, with the git-worktree declaration and its
three refusals quoted verbatim from the new tests, plus updates to
the Parsing, Rendering, build()-refusal, and Testing sections.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant