Skip to content

fix(cli)!: preserve argv boundaries and rewrite quoted shell wrappers - #3057

Closed
pashifika wants to merge 5 commits into
rtk-ai:developfrom
pashifika:fix/shell-compatibility
Closed

pashifika wants to merge 5 commits into
rtk-ai:developfrom
pashifika:fix/shell-compatibility

Conversation

@pashifika

@pashifika pashifika commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This pull request makes RTK's shell boundary explicit across CLI execution and hook rewriting. Generic runners now preserve argv through direct execution, intentional scripts require an explicit command-string mode, ambiguous shell syntax defers unchanged, and exact quoted sh, bash, zsh, and fish wrappers can safely rewrite portable inner commands.

This addresses four related limitations in the current execution model:

  • rtk run, rtk err, rtk test, and rtk summary rebuilt positional arguments with spaces and executed the result through sh -c on Unix or cmd /C on Windows, which lost argument boundaries and enabled unintended shell expansion.
  • Agent hosts can expose a fish login shell through $SHELL while evaluating commands with zsh or a POSIX-compatible shell, so guessing the parser from the environment is unreliable.
  • The hook safety gate recognized POSIX substitutions but previously missed bare fish command substitution and command-position fish control keywords, allowing uncertain input to be rewritten or auto-allowed.
  • Quoted command strings such as bash -c "head foo && grep -R bar ." were treated as opaque wrapper arguments, so the portable commands inside them could not use RTK rewrites as reported in rtk bash -c and rtk fish -c wrapping #2767.

The host still owns the shell that evaluates hook output. This change makes RTK's own execution and rewrite boundaries explicit; it does not translate syntax between fish, zsh, and POSIX shells or change the host executor.

Execution model and migration

The updated CLI separates direct argv execution from intentional command-string execution:

  • Positional forms such as rtk run <cmd> [args...], rtk err <cmd> [args...], rtk test <cmd> [args...], and rtk summary <cmd> [args...] execute the supplied program directly and preserve every argument boundary.
  • rtk run -c '<script>' retains the existing platform default of sh -c on Unix and cmd /C on Windows.
  • rtk run --shell fish -c '<script>' selects a shell explicitly. PowerShell and pwsh use -Command; other named shells use -c.
  • rtk err, rtk test, and rtk summary accept --shell <name> '<script>' when filtering an intentional shell command.
  • Explicit shell mode requires the complete script as one quoted argument, preventing RTK from reconstructing quoting after Clap has parsed argv.
  • Installed Codex guidance now recommends explicit rtk run --shell <name> -c '<script>' for shell-specific scripts and portable syntax otherwise.

This is a breaking behavior change for callers that relied on implicit shell expansion in positional arguments. Those callers must move shell syntax to rtk run -c '<script>' or use --shell <name> with one quoted script.

Conservative hook handling

The shared lexer now treats the following forms as unattestable before rewrite and permission decisions:

  • command and process substitution, including bare fish (...) syntax;
  • parenthesized shell constructs and incomplete quoting or trailing escapes;
  • file-target redirects;
  • fish control keywords such as and, or, if, switch, and end when they occur at a command boundary.

Keyword detection is command-position aware, so ordinary arguments such as rg and src remain attestable. Ambiguous constructs defer to the host unchanged and resolve to Ask rather than Allow, even when a wildcard allow rule is configured.

Quoted shell wrapper rewriting

The rewrite pipeline now recognizes a deliberately narrow command-string wrapper subset:

  • Exact sh -c, bash -c, zsh -c, and fish -c wrappers are eligible when the script is one balanced single- or double-quoted argument.
  • RTK rewrites the existing portable compound-command subset inside the script and replaces only the original script byte span, preserving the shell path, whitespace, quote delimiters, suffix arguments, and file-descriptor redirects.
  • Double-quoted scripts are accepted only when the outer shell has no active $ expansion, backticks, or backslash escapes to evaluate.
  • Unquoted or malformed scripts, additional shell options, file redirects, fish-specific control syntax, and nested wrappers pass through unchanged.
  • Wrapper rewriting is limited to one level and never grants automatic permission. Ask-capable hosts request confirmation, hosts without an explicit ask response preserve their native permission flow, and an inner deny rule takes precedence over an outer allow rule.

The wrapper parser is shared by rewriting and permission evaluation so the two paths cannot disagree about the supported boundary. Agent protocol tests cover Claude, Cursor, Gemini, Droid, and Copilot behavior.

Key files changed

The following files are the main review entry points:

File Purpose
src/core/shell.rs Centralizes direct command construction, explicit shell invocation, platform flags, shell resolution, and script-boundary validation.
src/main.rs Adds the --shell CLI surface and routes positional commands through direct argv execution.
src/discover/lexer.rs Detects parenthesized syntax, command-position control keywords, and incomplete quoting as unattestable.
src/discover/shell_wrapper.rs Parses the conservative quoted -c wrapper subset and exposes byte spans without decoding or re-quoting scripts.
src/discover/registry.rs Applies one-level inner compound rewrites while preserving the original wrapper and passthrough contract.
src/hooks/permissions.rs Prevents wrapper auto-allow and evaluates inner deny rules before returning Ask.
src/hooks/hook_cmd.rs Preserves each supported agent's rewrite and permission protocol for shell wrappers.
hooks/codex/rtk-awareness.md Documents explicit shell selection for generated Codex RTK guidance.
tests/run_shell_test.rs Covers literal argv preservation, exit codes, default and explicit shells, missing shells, and split-script rejection.
tests/rewrite_shell_wrapper_test.rs Covers the issue example, portable fish scripts, conservative passthrough, output, and rewrite exit codes.

Test plan

  • Run cargo fmt --all --check.
  • Run cargo clippy --all-targets -- -D warnings.
  • Run focused shell-wrapper tests, including 27 parser, registry, permission, and agent protocol tests plus the 3 CLI integration tests in tests/rewrite_shell_wrapper_test.rs.
  • Run env RIPGREP_CONFIG_PATH=/dev/null cargo test --all (2,478 unit tests passed, 8 ignored; all integration suites passed).
  • Run env RIPGREP_CONFIG_PATH=/dev/null cargo test --all -- --ignored --nocapture (8 ignored integration tests passed).
  • Run cargo build --release.
  • Verify direct argv execution, explicit sh, zsh, and fish execution, and quoted bash -c and fish -c rewriting on macOS.
  • Verify ambiguous shell constructs pass through, wildcard allow rules cannot auto-allow wrappers, and inner deny rules take precedence.
  • Compare release size, resident memory, and repeated startup/rewrite timings with pre-change release binaries; no material regression was observed. The shell-wrapper change added 16 bytes, with peak memory remaining approximately 1.3 MB.
  • Let CI validate Linux and Windows execution, including cmd /C, PowerShell flag selection, and platform-independent wrapper parsing.

The local ripgrep configuration injects --max-columns=150, which shortens the raw comparison output used by the existing bulky_grep_yields_token_savings test. The unsanitized cargo test --all run therefore fails that unrelated assertion; the complete suite passes when RIPGREP_CONFIG_PATH is cleared as shown above. This pull request does not change search behavior.

The broad scripts/test-all.sh suite was not run because it uses an installed binary and includes git fetch, external HTTP calls, and unrelated ecosystem commands.

🤖 Generated with Codex

Execute positional run, err, test, and summary commands directly instead of reconstructing them for sh -c. Add explicit shell selection for command strings while retaining the platform defaults for run -c.

BREAKING CHANGE: positional arguments for run, err, test, and summary no longer receive implicit shell expansion; use -c or --shell with one quoted script when shell syntax is required.
Treat bare parentheses, command-position control keywords, and incomplete quoting as unattestable so hooks neither rewrite nor auto-allow commands whose shell semantics are uncertain.
@CLAassistant

CLAassistant commented Jul 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Rewrite portable commands inside exact sh, bash, zsh, and fish -c scripts while preserving their original quoting and suffix arguments.

Require confirmation for wrapper rewrites and evaluate deny rules against parsed inner command segments.

Refs: rtk-ai#2767
@pashifika pashifika changed the title fix(cli)!: preserve argv boundaries and defer ambiguous shell syntax fix(cli)!: preserve argv boundaries and rewrite quoted shell wrappers Jul 19, 2026
@ccoVeille

Copy link
Copy Markdown

I cannot test for now, but thanks.

I'm curious about feedbacks that you will get

@SindriFr

SindriFr commented Sep 7, 2026

Copy link
Copy Markdown

@pashifika — heads up that I rebased this work onto current develop and opened it as #3828, because I needed wrapper rewriting locally and this PR no longer applied. You are the commit author there and the code is overwhelmingly yours: shell_wrapper.rs is your file essentially unchanged, and most of the registry, permissions and hook_cmd hunks and tests are yours too.

What changed in the rebase:

I would rather not step on your PR. If you would prefer to refresh this one yourself, say so and I will close #3828 — happy either way, and no attribution concerns from my side. If you have no plans to pick it back up, #3828 is ready and should close this when it lands.

@pashifika

Copy link
Copy Markdown
Contributor Author

@SindriFr thanks for the rebase — no attribution concerns here either, and dropping the lexer.rs / main.rs / core/shell.rs hunks was the right call: #3828 reviews much better as the rewrite path alone. Your sudo decision is correct too — the inner rtk off secure_path is exactly why it must pass through.

Closing this in favour of #3828 plus #3911, which is the rest of this PR rebased onto your branch: the argv-boundary work, the fish-aware unattestable gate, fish -c back in the wrapper subset (that gate is the guarantee #3828 was missing), and unambiguously-fish scripts wrapped as rtk run --shell fish -c '<script>'. #3828 stays first in the stack, so merging it does not need to wait on #3911.

@pashifika pashifika closed this Sep 7, 2026
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.

4 participants