Skip to content

fix(shell): let the pytest grant cover python -m pytest - #4020

Closed
kovtcharov wants to merge 3 commits into
amd:mainfrom
kovtcharov:claude/python-m-pytest-grant
Closed

kovtcharov wants to merge 3 commits into
amd:mainfrom
kovtcharov:claude/python-m-pytest-grant

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Agents type python -m pytest first, and the shell refused it with "only read-only commands are allowed". The refusal never mentioned that pytest is available once the coding skill is loaded. In a 14-task benchmark run on main this dead end came up 5 times, and the agent fell back to hand-written asserts through run_python instead of the project's test suite. python -m pytest now counts as pytest: same grant, same flag and path rules, no extra prompt. It also imports a project that was never installed, so the agent no longer writes a pytest.ini into the repo to work around import errors.

Test plan

  • python -m pytest tests/unit/test_skill_binary_grants.py passes. On main, the 6 new reproducing tests fail.
  • With the coding skill loaded, python -m pytest -q runs in a flat checkout with no config and no install (covered end to end by test_python_m_pytest_imports_a_checkout_that_was_never_installed).
  • python -c …, python -m pip … and python -X dev -m pytest are still refused.
🔍 Technical details

@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

python -m pytest now counts as the pytest grant — same flag and path rules, same lack of an extra prompt — and the mapping is deliberately narrow: only the exact python|python3|py -m pytest shape, everything else still falls through to the whitelist. I checked the mapping and the policy decisions by hand and they behave exactly as the docstring claims. No security or correctness blockers.

One thing worth fixing before merge: the skill the agent actually reads still says to run bare pytest. The coding skill's instructions never mention the -m spelling, so the agent will keep typing the one that fails on a checkout that was never installed — which is the workaround (writing a config file into the repo) this PR set out to stop. Teaching the gate without teaching the prompt only fixes half of it.

Smaller: when an agent tries PYTHONPATH=. pytest, the new refusal correctly names it as an environment assignment but then tells the agent to drop it — which walks straight back into the import error. Point it at python -m pytest instead, the thing that now works.

Real-world evidence

No evidence-bundle.md was produced for this PR, and this change touches an agent tool gate rather than the CLI / REST / Agent UI / MCP surfaces the evidence lane exercises — so there were no pixels or HTTP transcripts to defer.

I did exercise the new logic directly on this checkout rather than reading it. Loading binaries.py standalone and calling the new mapping plus the pytest policy:

'python -m pytest -q tests'       -> ['pytest', '-q', 'tests']
'python3 -m pytest'               -> ['pytest']
'py -m pytest -q'                 -> ['pytest', '-q']
'python -X dev -m pytest'         -> ['python', '-X', 'dev', '-m', 'pytest']
'./python -m pytest'              -> ['./python', '-m', 'pytest']
'python -m pytest_evil'           -> ['python', '-m', 'pytest_evil']
'python -m pip install requests'  -> ['python', '-m', 'pip', 'install', 'requests']
'python -mpytest'                 -> ['python', '-mpytest']
'python -m'                       -> ['python', '-m']

pytest -q tests              allow
pytest -p no:cacheprovider   allow
pytest ../../etc/passwd      refuse
pytest --pdb                 refuse
pytest -c /etc/pytest.ini    refuse
pytest -p evil               refuse

That confirms both halves of the claim: the granted spelling maps, and every near-miss shape stays an ordinary python call the whitelist refuses.

I could not run tests/unit/test_skill_binary_grants.py on this runner — the review container has no openai/dotenv, so the module fails at import before collection. The six new tests are unverified here and rest on CI. The PR's own test-plan boxes are all still unchecked; please tick them (or say which lane ran them) before merge.

🔍 Technical details

Issues found

🟡 The coding skill still teaches only bare pytest (hub/skills/coding/SKILL.md:67-72)

The PR's stated motivation is that agents type python -m pytest first and that -m is what puts the working directory on sys.path for an uninstalled checkout. The skill prompt is the surface that tells the model what to type, and it says:

This skill grants pytest, so run it directly:

pytest -q tests/
pytest -x -k discount tests/test_cart.py

With that unchanged, the model still reaches for bare pytest, hits the import error on a flat checkout, and works around it — the pytest.ini-into-the-repo behaviour the description says this removes. CLAUDE.md's doc-sync rule applies: a behaviour change updates every doc that describes it. Suggested addition after the examples (not a ```suggestion block, since the file isn't in this diff):

`python -m pytest` is the same grant with the same rules. Prefer it on a checkout
that was never installed — `-m` puts the working directory on the import path, so
`from mypkg import ...` resolves without a config file or a `pip install -e .`.

🟢 The env-assignment hint points away from the fix (src/gaia/agents/tools/shell_tools.py:991)

PYTHONPATH=. pytest -q is exactly an agent reaching for the import path, and "Run the command without the assignment." returns it to the failure this PR removes.

                    "hint": (
                        "Run the command without the assignment — "
                        "'python -m pytest' already puts the working "
                        "directory on the import path."
                    ),

🟢 Common Windows/Linux launcher spellings are still refused (src/gaia/skills/binaries.py:727)

PYTHON_LAUNCHERS covers python, python3, py, but py -3 -m pytest (the standard Windows launcher form) and python3.12 -m pytest both fall through to the whitelist and get the generic refusal. The strict-shape rule is the right default — flagging only so it's a decision rather than an oversight. A versioned-launcher pattern (python3(\.\d+)?) would cover the second at no loss of strictness; py -3 legitimately stays out, since the interpreter-flag exclusion is what keeps -X dev from sneaking through.

Strengths

  • The narrow-shape rule is the right call and is pinned by tests from both directions: test_python_m_pytest_is_held_to_the_pytest_policy proves the rewritten argv still meets the full pytest policy, and test_only_the_exact_python_m_pytest_shape_is_covered proves -X dev, pytest_evil, ./python and -m pip don't inherit the grant. The interpreter-flag exclusion in particular is the non-obvious part, and it's both documented and tested.
  • policy_argv is applied at every decision point that previously keyed off the raw argv — the grant check, the confirmation skip (skill_grant_covers_call), the path-scan exemption, and the Windows argv rule — while the executed argv stays as typed. That's the invariant that matters: one tier can't disagree with another about what the command is, and -m still does its sys.path job at runtime.
  • test_python_m_pytest_imports_a_checkout_that_was_never_installed tests from the user's real cold state (flat project, no config, no install) instead of the primed dev box, which is the failure mode CLAUDE.md's testing section calls out by name.
  • The BinaryPolicy.python_module field generalises cleanly — a future policed CLI declares its module and inherits the whole mapping without touching shell_tools.py.

Kalin Ovtcharov and others added 3 commits September 21, 2026 04:14
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.
@kovtcharov

Copy link
Copy Markdown
Contributor Author

Restacked on #4019 (fix-posix-shell-pipes). Merge after #4019.

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Everything in this PR is already on main, so there is nothing left to merge.

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 main that already contains its work.

Closing as superseded. No work is lost; reopen if you disagree.

🔍 Technical details

Verified rather than assumed — merged origin/main into this branch taking main's side, then diffed the result against main:

git checkout -B probe <branch> && git merge origin/main --no-edit -X theirs
git diff --stat origin/main HEAD

Residual: empty. The merge produces a tree identical to main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants