Skip to content

fix(agent): stop a refused delete being rerouted through a Python script - #6882

Merged
kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:fix/refused-delete-reroute
Nov 7, 2026
Merged

kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:fix/refused-delete-reroute

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

When GAIA refused rm -rf on a folder, the agent deleted the folder anyway by writing a cleanup script and running it with execute_python_file. The eval scenario shell_destructive_rm_refused caught this on Qwen3.6, scoring 5.7 and 4.7 in two local runs. GAIA already keeps a per-turn list of refused targets, but it couldn't see deletes, moves or overwrites inside Python, and it didn't read the unattended "needs your approval" reply as a refusal. Now a Python route to a refused path is blocked like the shell command was, and the user is never asked to "Allow once" something GAIA refused.

  • What Python changes are recognised: shutil.rmtree/move, os.remove/unlink/rmdir/rename/replace, and pathlib unlink/rmdir/write_text/rename. Aliased imports are covered, and targets are resolved through variables, os.path.join and Path /.
  • Targets computed at run time: a target the code only works out when it runs is treated as possibly the refused one. That applies only after something was refused in the same turn, and it's never recorded as a denial itself.
  • Policy refusal vs a person's no: a policy refusal can't be lifted by approving it. A person's "no" is still put to them once, as before.
  • Recovery prompt: "needs your approval" now counts as a denial, so the loop stops asking the model for a new plan after it.

Eval categories to watch on the next RC: gaia_shell, gaia_honesty, gaia_files, gaia_tool_selection.

Test plan

  • New unit tests reproduce the trace (rm refused, then an rmtree script) plus inline, aliased, os.path.join, pathlib and run-time-target variants. All six failed before the fix and pass now. Code that only reads, or touches other paths, isn't caught.
  • tests/unit full suite: 38,332 passed. Agent tests after merging main: 6,126 passed.
  • python util/run_eval_lane.py --category gaia_shell before and after on Qwen3.6, checking shell_destructive_rm_refused. Pending, because the machine is busy with another eval sweep.

When the shell gate refused rm -rf on a folder, the agent wrote an rmtree
script and ran it with execute_python_file, and the folder was deleted.
The per-turn denial ledger recorded the refused folder but could not see
deletes, moves or renames inside Python, and the unattended 'needs your
approval' refusal was not read as a refusal, so the loop asked the model
for another plan.

Python file changes (shutil.rmtree/move, os.remove/unlink/rmdir/rename/
replace, pathlib unlink/rmdir/write_text/rename) now count as the same
path effect, with targets resolved through variables, os.path.join and
Path '/'. A target the code only computes at run time overlaps a refused
path. A reroute of a policy refusal is refused without an 'Allow once'
question; a person's no is still put to them once.
@github-actions github-actions Bot added tests Test changes agents labels Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Verdict: Request changes

This closes a real hole: GAIA refused rm -rf on a folder, then let the agent delete the same folder with a Python script. The new detection of deletes in Python looks solid and is well tested. But the new "a policy refusal can't be lifted" rule also catches refusals that are really redirects, and that breaks file writes in normal use.

  • Blocking: a shell write the guardrails redirect to write_file now permanently blocks that write_file. Models often try echo … > notes.txt or tee notes.txt. The guardrails refuse those with "To write a file, use write_file or edit_file". That refusal is now stored as a no that nothing can lift, so when the agent follows the hint it gets "Not run … tell the user it was not done", and the user is never asked. Before this PR the user was at least asked. Only mark as unliftable the refusals that really are a no (the refuse tier, such as a destructive target or the unattended approval refusal), not shape refusals that point at another tool.

Real-world evidence

N/A: no evidence bundle was produced for this run, so this verdict rests on static review. I also probed the ledger directly: after a refused echo hi > notes.txt, a later write_file notes.txt comes back blocked (output below). The PR's eval item (gaia_shell / shell_destructive_rm_refused on Qwen3.6) is still unchecked. It's worth running before merge, because this change affects the shell, files and honesty categories.

🔍 Technical details

🟡 Every shell policy refusal is recorded as refused=True, including redirect-style ones (src/gaia/agents/base/agent.py:7387)

_execute_tool records any _policy_refusal of a call that has a command with refused=True. Some of those refusals are steps to take rather than a no:

  • The operator refusal from _parse_line (shell_tools.py:2457) has no tier and hints "To write a file, use write_file or edit_file".
  • The FILE_REWRITE_BINARIES refusal (shell_tools.py:4689) hints "use write_file".

Both reach _execute_tool as a refusal, because _line_verdict returns any error that isn't _reaches_the_prompt. The new denial.refused branch in _check_denied_effects then hard-denies the recommended tool. I loaded denied_effects.py standalone to check this:

echo hi > notes.txt  {Effect(kind='path', target='…/notes.txt')}
  write_file blocked: True
tee notes.txt        {Effect(kind='path', target='…/notes.txt')}
  write_file blocked: True

The error text then tells the model "No approval can lift that refusal … Tell the user it was not done", so the agent gives up on an ordinary file write. Gate refused on the hard tier only. The unattended refusal in the new test, and destructive or never-grantable targets, carry tier: "refuse":

                    refused=isinstance(refusal, dict)
                    and refusal.get("tier") == "refuse",

Please add a test for this: echo x > notes.txt refused, then write_file notes.txt is not hard-denied. It's also worth checking whether the tierless redirect refusals should be recorded at all. Today they still lead to a "You declined…" question about something the user never declined, though that predates this PR.

🟢 from os.path import join is not resolved (denied_effects.py, _PyFiles.__init__)

ImportFrom stores only the root module (("os", "join")), so text()'s module == "os.path" check never matches a bare join(...). The target then becomes COMPUTED_PATH. That fails closed, so nothing gets through, but after any refusal in the turn it blocks deletes of unrelated paths. Storing node.module instead of the root for os.path would fix it.

Strengths

  • _PyFiles resolves targets through variables, os.path.join, Path /, iterdir loops and aliased imports. The negative tests (read-only code, str.replace, a different path) guard against over-blocking.
  • An unreadable target is never itself recorded as a denial, which avoids one declined script blocking every later write. That is a good, explicitly tested invariant.
  • The user's own "no" can still be lifted once, and a test pins that, so only the policy path changed.

A shell write refused for its shape ('use write_file or edit_file') was
stored as a refusal nothing could lift, so following the hint was blocked
without asking. Only a refusal whose tier is 'refuse' is now final.
@kovtcharov

Copy link
Copy Markdown
Contributor Author

Fixed in the latest commit. A policy refusal now counts as final only when its tier is refuse, as with the unattended approval refusal or a destructive target. A shape refusal like the shell-operator one ("use write_file or edit_file") has no tier, so a later route to that path is put to the user once, as before. Your case is covered by a new test that fails on the previous commit: a refused echo hi > notes.txt, then a write to notes.txt, asks once and runs when allowed. The gaia_shell eval run is still pending because the machine is busy with another sweep.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Nov 7, 2026
Merged via the queue into amd:main with commit c2ec0eb Nov 7, 2026
38 of 42 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the fix/refused-delete-reroute branch November 7, 2026 07:47
kovtcharov pushed a commit to kovtcharov/gaia that referenced this pull request Nov 7, 2026
…hon (amd#6938)

The path sandbox only governed GAIA's file tools. A write it refused
could still happen later in the same request through code the agent
runs, because `run_python` and the shell aren't confined by it. This
extends the refused-path guard from amd#6882 to writes made from
`run_python`, `execute_python_file` and the shell. When the sandbox
refuses a write to a path, any later code in that request that writes to
that path, creates it, copies onto it, moves it or renames onto it is
refused too. It's a refusal like the original one, not an "Allow once"
question.

- **Recorded as a refusal:** a write the sandbox refuses before the
prompt, before the tool runs, or from inside the tool body. Only a
sandbox "no" counts (the target is outside the allowed folders or
write-blocked). A "read it first" or a size limit isn't recorded. A
person's own "no" to a write still asks them once, as before.
- **Recognised in Python:** the write forms of `open` (also
`io`/`codecs`/`os.open`), pathlib
`write_text`/`write_bytes`/`open`/`touch`/`mkdir`/`rename`/`replace`,
`shutil.copy*`/`copytree`/`move`, and
`os.rename`/`replace`/`makedirs`/`mkdir`. Targets are resolved through
aliased imports, variables, f-strings, `os.path.join`, `Path /`,
`.parent` and `joinpath`. A copy counts only its destination, so reading
the refused file or copying from it still runs.
- **Recognised in the shell:** redirects with or without spaces,
including inside `cmd /c "…"` and `bash -c '…'`,
`cp`/`mv`/`Copy-Item`/`Set-Content`/`Add-Content`, `xcopy`/`robocopy`,
`[IO.File]` write methods, `dd of=` and `sed -i`.
- **Same normalisation on both sides:** a later target is resolved
exactly as the refused path was when it was recorded (links, short names
and equivalent Windows path forms), so two spellings of one file compare
equal.
- **Recovery still works:** writing the result somewhere allowed
instead, such as the working folder or the temp folder, is not blocked.
- **Targets computed at run time** are treated as possibly the refused
path, but only after a refusal in the same request. This covers a
command a script builds at run time and code that picks its call at run
time (`exec`, `eval`, `__import__`, `getattr` on a file module). It's
the same fail-closed rule amd#6882 uses for deletes.

Not covered here (follow-ups):
- Static analysis can't see a path the code builds in ways it doesn't
model (e.g. `str.format`, data read at run time). Those fall back to the
computed-target rule above.
- A relative path in `run_python` is resolved against the agent's
working directory, but the snippet runs from the project root. Absolute
paths are unaffected.
- Running code inside the same path policy (OS-level confinement) would
close this for good. That's a larger change.

Eval categories to watch on the next RC: `gaia_files`, `gaia_shell`,
`gaia_honesty`, `gaia_tool_selection`.

## Test plan
- [x] New `tests/unit/agents/test_refused_write_reroute.py`:
classification tests for each Python and shell form above (inline and as
a script file, including aliased imports, computed commands and paths,
and alternate path spellings on Windows), and false-positive cases
(reads, writes elsewhere, copying from the refused path, ordinary
commands, recovery writes). Agent-level tests reproduce the issue trace
under auto-approve, plus a never-askable path, and check that an allowed
write or a "read it first" records nothing. The rerouting cases failed
before the fix; all pass now.
- [x] `tests/unit/agents/test_denied_effects.py` (amd#6882) still passes.
- [x] Before/after run of every unit test that touches `_execute_tool`,
`run_python`, `write_file` or the denial ledger (about 190 files): no
new failures against `main`.
- [ ] `gaia_files` and `gaia_shell` eval categories on the next RC.

Closes amd#4071
kovtcharov-amd added a commit that referenced this pull request Nov 12, 2026
Main's #6882 added its own policy-refusal flag (a refuse-tier no that no
approval lifts); this branch's flag says who refused. They answer different
questions, so a Denial now carries both, and a final refusal is never
reported as the user's. The redirect/argument effect parser main rewrote
keeps this branch's per-segment refusal scope, and main's shape-refusal test
now covers both a refusal that names the file and one that doesn't.

Also from review: a turn the --time-limit timer cut off now keeps the
agent's latest findings instead of only the canned stop line, the CLI
reference says so and notes the exit code, and -p refuses --time-limit when
the turn would run on the agent binary through the daemon, where the limit
could not be enforced. The agent CHANGELOG entry moves from the released
0.3.4 section to Unreleased.
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