Repository navigation
feat(shell): run compound read-only commands under the same allowlist - #4037
kovtcharov wants to merge 5 commits into
Conversation
Request changesThis lets the shell tool run
The eval wasn't run. The tool's model-facing docstring changed, which is on the list of changes that require Real-world evidenceNo I exercised the mixin directly in this checkout instead (real subprocesses, temp dir). Connectors, pipes-inside-chains, quoted operators and
🔍 Technical details🟡
|
|
Heads-up for whoever merges last: this PR touches Interacts with: #4019 (pipes — this is stacked on it), #4020, #4023, #3808, #3791, #3765. 🔍 Technical detailsThe one that actually matters — #3808's tiers. Once a command outside Per-PR rules used:
Three r4 tests changed, because the behaviour they pinned is the behaviour this PR changes: |
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.
a76660d to
e6b4065
Compare
|
🟡 The
The new tests only put 🔍 Technical details
for step in steps:
step_cwds.append(walk_cwd)
if step.is_cd:
walk_cwd, error = self._resolve_cd_target(...)
if error:
return error # ← fires even when && would have skipped the cdSuggested fix — check position in for index, step in enumerate(steps):
step_cwds.append(walk_cwd)
if step.is_cd:
if index != 0:
return [], {
**NOT_EXECUTED,
"status": "error",
"error": "cd must be the first command on the line.",
"has_errors": True,
"hint": "Write 'cd <dir> && <command>', or pass working_directory.",
}
walk_cwd, error = self._resolve_cd_target(...)
if error:
return errorTwo tests to add to def test_cd_behind_failing_and_is_not_resolved(notes):
# 'cd nope' is gated by &&; the failing cat means it never runs
(notes / "sub").mkdir()
result = _run("cat missing.txt && cd nope ; ls", notes)
assert result["status"] == "success", result
assert "notes.txt" in result["stdout"]
def test_cd_behind_failing_and_does_not_shift_later_steps(notes):
(notes / "sub").mkdir()
(notes / "sub" / "only-here.txt").write_text("x\n")
result = _run("cat missing.txt && cd sub || ls", notes, host=_Confined(notes))
assert result["status"] == "success", result
assert "notes.txt" in result["stdout"] # ran in root, not sub
assert "only-here.txt" not in result["stdout"]The tool docstring also changed (line 1466–1470), which per |
|
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
&&,||and;, so looking something up and checking it cost three or four agent calls where one would do — and each extra call re-sends a ~12K-token prompt, which is where 53% of GAIA's spend goes. Two benchmark runs of 14 tasks hit 4 outright operator refusals, spent about 7 steps recovering from them, and used 100 steps for work Claude Code did in 82. Chained lines now run, under exactly the security model a single command already gets: every command on the line is validated before any of it runs, one refused command refuses the whole line, and one approval covers the line.Stacked on #4019 — the first commit here is that PR's pipe fix, which this builds on. Merge it first, or review the second commit on its own.
Test plan
python -m pytest tests/unit/test_shell_compound_commands.py -q— 46 new tests against real subprocesses intmp_path: each connector's semantics, a pipeline inside a chained line,cd, refusals, timeouts, and the confirm/refuse/grant tierspython -m pytest tests/unit/test_shell_guardrails.py tests/unit/test_shell_pipelines.py tests/unit/test_skill_binary_grants.py tests/unit/test_shell_rewrite_redirect.py tests/unit/test_powershell_command_injection.py -qpython -m pytest tests/unit -qcd src && dirfromgaia chatand confirm it works — each pipeline is handed tocmd.exeseparately, which is the platform path no CI machine here coversgaia eval agentagainst the committed baseline before merging: the tool's model-facing docstring changed, and that is not covered by unit tests🔍 Technical details
Parse.
_split_connectorssplits the raw line on&&,||,;outside double quotes — the same quote model_outside_double_quotesalready uses, odd-count caveat included, so the splitter and the operator blocklist cannot disagree about where a segment ends. Single quotes deliberately do not protect an operator, becausecmd.exedoes not honour them:grep 'a||b' fsplits here and dies on the unbalanced quote rather than reachingcmd.exeas two commands nothing validated. Each part is then operator-scanned,shlex-split and pipeline-split by the existing_split_pipeline, giving a_Step(text, segments, connector)._parse_lineis shared by the refusal path andskill_grant_covers_call, so the tiers answer the same question about what a segment is.Tiers.
_shell_command_refusalruns_validate_commandover every segment of every step, unchanged; a REFUSE anywhere returns before the executor starts, stampedexecuted: False. A CONFIRM-tier segment leaves the line un-refused so it reaches one prompt, which renders the wholecommandargument.skill_grant_covers_callnow loops every segment of every step:gh issue list && gh issue view 1is covered,gh issue list && rm -rf xandgh issue list | headare not.Execution. Sequential and in-process:
_connector_runsimplements sh's rule, and a skipped step leaves the exit code standing sofalse && a || brunsb. Each step goes through the existing_run_pipeline/argv path, so no line reaches a shell on POSIX. On Windows each pipeline is handed tocmd.exeas its own string; the compound line never is. The whole line shares one timeout deadline, and a timeout kills the current pipeline and starts no later step.cd. Allowed only as a barecd <dir>: never a pipeline stage, never a flag, exactly one operand. Targets are resolved and validated in a pre-pass before anything runs, held to the same bar asworking_directory, and each step's arguments are path-checked against its own resulting directory — socd sub && cat ../../secretis still refused.Still refused:
>,>>,<, backticks,$(), a background&, inlineVAR=value(via the allowlist), and — new — newlines, whichcmd.exereads as a command separator and nothing refused before.Reporting.
return_codeis the last command that ran and the result gainssteps: [{command, return_code}].has_errorsadditionally covers a failure no||handled, sopytest -q; lscannot report a failing suite as a passing check via the verification footer. A missing executable mid-line is reported as exit 127 rather than claiming nothing ran; when nothing did run,executed: Falseis unchanged.Rate limiting: one line costs one slot, because it is one model step — that is the whole point of the change, and it is stated in the tool's docstring.