Repository navigation
fix(shell): allow the two stderr redirections that write nothing - #4041
kovtcharov wants to merge 6 commits into
Conversation
Request changesThis lets the shell tool run chained lines ( A pipe inside single quotes confuses which command a A Two gates from the test plan are still open. The tool's description string changed, which the project rules put squarely in the "run the agent eval first" bucket — Note this PR is stacked, so the diff under review carries three commits, not one. The review covers all three. Real-world evidenceN/A — no evidence bundle was produced for this run, and this change has no CLI/API/MCP/Agent-UI surface of its own; it changes a tool the agent calls. The PR's own evidence is the pytest suite ( 🔍 Technical details🟡 Important1. Stderr redirection is attributed to the wrong pipeline segment when a single-quoted argument contains a spaced pipe (
The first reaches The fix belongs in the pipe scan only; leave (with 2. The for step in steps:
step_cwds.append(walk_cwd)
if step.is_cd:
walk_cwd, error = self._resolve_cd_target(step.segments[0][1], walk_cwd)This runs before execution and ignores connectors, while the execution loop below gates each step on Both are defensible as a conservative simplification — resolving 3. Required eval gate not run for a tool-docstring change
🟢 Minor4. A pipeline that times out loses its partial stdout ( 5. Chained lines weaken the rate limiter's per-command accounting ( 6. Strengths
|
On macOS and Linux every piped shell command returned wrong output with status "success". The validator drops the `|` tokens and the executor then ran the segments as one argv, so `cat notes.txt | sort` ran as `cat notes.txt sort` and `grep a x.txt | wc -l` returned "x.txt" instead of a count. Windows was unaffected because it goes through cmd.exe. Validated segments are now chained as separate processes, still without a shell. The exit code is the rightmost failing stage, so `pytest | tail` cannot turn a failing suite into a verified pass; an upstream stage killed by SIGPIPE (`| head`) is not a failure. Every stage's stderr is kept, and a timeout kills the whole pipeline. test_a_pipeline_is_not_run_as_argv only pinned the shell flag, so it passed while the bug was live; it now checks that the stages are chained.
Agents reach for `python -m pytest` first, and the shell refused it with "only read-only commands are allowed" - no hint that pytest exists behind the coding skill. In a 14-task benchmark run on main that dead end was hit 5 times; the agent gave up on the tests and twice reported them as passing. `python -m pytest` is now judged as `pytest`: same grant, same flag and path policy, no extra prompt. Without the grant it gets pytest's "load that skill first" refusal. The typed spelling is what runs, because `-m` puts the working directory on the import path, so a checkout that was never installed imports without a pytest.ini workaround. Only the exact `python|python3|py -m pytest` shape maps; `-c`, a script, another module or an interpreter flag stays refused. An inline `PYTHONPATH=. pytest` was refused as an unknown command named "pythonpath=."; it now says inline environment assignments are unsupported.
A shell command over the rate limit (3 per 10 s, 10 per minute) was refused with "wait N seconds", so the model spent a whole step sending the same command again after the same wait. In a benchmark run the refused command was the recovery itself - `python -m pytest` right after bare `pytest` failed to import the project - in 2 of 14 tasks. The tool now waits for the window and then runs the command, reporting waited_seconds. The rate stays exactly as capped; a wait longer than max_rate_limit_wait_seconds (60 s) is still refused.
The shell tool refused `&&`, `||` and `;` outright, so work that is one call elsewhere — `cd src && ls`, `pytest -q | tail -5; grep -rn parse_` — cost GAIA three or four. Two benchmark runs of 14 tasks hit 4 operator refusals and about 7 recovery steps, and used 100 steps where Claude Code used 82; every extra step re-sends a ~12K-token prompt, and fresh input is 53% of the cost. A line is now split into its pipelines, and every segment of every pipeline goes through the same tiered validation as before, before any of it runs: one refused command refuses the whole line, one confirm-tier command raises a single prompt showing the whole line, and a skill grant covers the line only when it covers every segment. The steps then run sequentially in-process with sh's semantics — `&&` on success, `||` on failure, `;` regardless — through the existing pipeline runner, so no line reaches a shell on POSIX and Windows gets one pipeline per cmd.exe call instead of the line. `cd <dir>` is allowed as a bare one-argument command, path-validated like `working_directory`, and moves only the rest of its own line. Redirection, command substitution, a background `&` and inline `VAR=value` are still refused; newlines now are too, which they were not before, and cmd.exe reads one as a command separator. The line costs the rate limiter one command because it is one model step, and a failure no `||` handled still sets `has_errors`, so `pytest -q; ls` cannot report a failing suite as a passing check.
Across seven benchmark runs of 14 tasks on four models, 19 of the 21 shell operator refusals were `2>/dev/null` or `2>&1` — neither of which creates a file. They are how every model asks for a test run (`pytest -q 2>&1 | tail -20`, `ls tests/ 2>/dev/null`), each refusal cost the call plus a median of one more to recover (43 wasted tool calls, ~5% of all of them), and the refusal's own hint said to use `write_file`, which is not advice for a stderr redirect. One measured case became a false claim: the agent could not test a patch in a scratch copy, and reported a later run against unpatched code as if it had covered the patch. Both forms are now lifted out of the line before the operator scan and recorded as a flag on the segment that wrote them — exact standalone tokens only, and never inside double quotes. `2>&1` points that segment's stderr at its own stdout pipe, so a later stage's `grep` sees it; `2>/dev/null` points it at the null device. Neither names a file, and on POSIX neither reaches a shell: they are destinations for a stream this process already captures. On Windows the step is a cmd.exe string, so the redirection travels in it, spelled `2>nul`. Everything else stays refused: `2>` to any other path, `1>`, `>`, `>>`, `<`, `&>`, `>&`, backticks, `$()`, `&`, and newlines. Return codes are untouched, so discarding stderr cannot turn a failing command into a passing one.
13162ea to
9f53884
Compare
|
Everything functional in this PR is already on It landed as part of #4049, which squash-merged the tip of the stack this branch sits in and carried every commit below it along. GitHub does not auto-close the lower PRs in a stack when the tip is squashed, which is why this stayed open — and why it has been showing a conflict against a Merging it now would actively hurt: the only thing it still adds is an outdated paragraph of the Closing as superseded. No work is lost; reopen if you disagree. 🔍 Technical detailsVerified rather than assumed — merged Residual: a stale docstring block in |
The shell tool refused any
>, which meant it also refused the two redirections that write nothing. Across seven benchmark runs of 14 tasks on four models, 19 of the 21 operator refusals were one of them — and they are how every model asks for a test run (pytest -q 2>&1 | tail -20,ls tests/ 2>/dev/null). Each refusal cost the call plus a median of one more to recover, ~5% of all tool calls; the refusal even told the model to usewrite_file, which is no help for a stderr redirect. In one measured run it caused a false claim: the agent's attempt to test a patch in a scratch copy was blocked, and it reported a later test run against unpatched code as if it had covered the patch.2>/dev/null2>&1>>append>writeBoth now work; every other redirection stays refused,
2>to any other path included.Stacked on #4037 (
claude/compound-shell-commands) — it rewrites the same parsing path, so this builds on it rather than conflicting with it. Review #4037 first; this PR's own diff is the last commit.Test plan
pytest tests/unit/test_shell_stderr_redirection.py— new file: both forms run,2>&1feeds the merged stream to the next pipeline stage, a discarded stderr still reports a non-zero exit, and 16 other redirection spellings are refused with no file createdpytest tests/unit/test_shell_*.py tests/unit/test_skill_binary_grants.py tests/unit/test_powershell_command_injection.py— 503 passedpython tests/verify_shell_security.py— chaining, pipes and path traversal unchangedpytest tests/unit— only the 24 failures already failing onmainfor this machinegaia eval agent --category tool_selection— the tool docstring changedpytest tests/unit/test_shell_stderr_redirection.pyis POSIX-only; the cmd.exe path needs a manual check (ls nosuchdir 2>/dev/null,git status 2>&1 | findstr branch)🔍 Technical details
Why the security surface does not widen. Both forms are recognised as exact standalone tokens (
2>&1,2>/dev/null), never as a general2>PATH, and never inside double quotes. They are lifted out of the line before the operator scan and become a flag on the pipeline segment that wrote them, so what runs is the same allowlist-validated argv as before. The flags are destinations for a stream the tool already captures —subprocess.STDOUTandsubprocess.DEVNULL— so neither opens a path, creates or truncates a file, or runs anything.2>to any other path,1>,>,>>,<,&>,>&, backticks,$(),&and newlines are refused exactly as before, and a token glued to anything (2>&1x,2>&1>out.txt) matches nothing and is refused too.A merged segment writes stderr down its own stdout pipe, which is what puts it in front of the next stage's
grep. Return codes are untouched by either flag, so the pipefail rule still fails a line whose stderr was discarded. On Windows a step is already a cmd.exe string, so the redirection travels in it (spelled2>nul) rather than as a capture flag — cmd.exe owns that pipeline, so only it can route a middle segment's stderr.The refusal message and hint now name the two allowed forms instead of pointing a stderr redirect at
write_file, anddocs/spec/shell-tools-mixin.mdxlists them in the composition table.