Repository navigation
fix(agents): stop run_python from swallowing a turn on an unusable import - #4087
Conversation
…port A task whose first step was a non-CORE tool could end with no result: the model reached for run_python to invoke that tool, hit a bare ImportError, read it as a typo, and reissued the same call until the step budget was gone. Two of four runs on the reporter's repro produced nothing at all. run_python puts the project root on PYTHONPATH, so `from gaia import <tool>` resolves far enough to fail on the symbol rather than the package — an error shaped like a fixable mistake. The snippet is a separate process and can never reach the agent's tools, so the call is refused before it spawns, with an error that names the tool-call path instead. The docstring says the same thing, since the model reasons from it.
main added a check-result contract to run_python's successful return (attach_check/CheckResult); this branch added an import guard that refuses a gaia-importing snippet before it spawns. Both landed at the tail of tests/unit/test_run_python_tool.py, so the only conflict was two test blocks appended to the same spot — both kept. The guard's early return carries no check_result key, matching main's own error returns in run_python: the contract covers a call that actually ran.
|
The red eval check on this PR isn't yours — nothing in your branch causes it and there's nothing to fix here. The judge account hit its weekly Claude limit, so the eval workflow's preflight step fails before it runs anything. It's hitting every branch in the repo right now (37 runs, 28 branches, zero successes since 00:13 UTC), tracked in #4080. It clears on its own at Sep 23, 8am PT; a re-run after that should go green with no push from you. |
itomek
left a comment
There was a problem hiding this comment.
@amd-pworfolk — verified locally at 4f2a1d7d: 30/30 in test_run_python_tool.py, the refusal returns without spawning anything (checked with my own spies on run/Popen/posix_spawn, not your test), and I reproduced the old behaviour on main — it was worse than the description says, since main returned status: "success" next to that ImportError. The narrow claim holds, and the scope note is the right call. Approving.
Two things worth a look; neither blocks.
The same dead end is still reachable through execute_python_file. It sits in the same file as run_python, spawns a subprocess the same way, and a script on disk that imports a GAIA tool fails exactly the way #4084 describes — so an agent that writes a file and runs it lands back in the loop this PR closes. The codebase already treats the two as one class. Extending the guard is a couple of lines plus a test, and it makes the fix match the failure rather than the tool.
One sentence in the description isn't true of the code. It says a mention inside a string or a comment is not a match. Comments are fine; a string isn't — an import on its own line inside a triple-quoted string does match, so a snippet carrying a docstring that quotes the bad pattern gets refused. Either tighten detection or soften the sentence; I'd soften it, since the cost is one wasted retry.
🔍 Technical details
Sibling path. execute_python_file is ~77 lines above run_python in hub/agents/chat/python/gaia_agent_chat/agent.py and runs [sys.executable, <path>] through subprocess.run with no guard. src/gaia/agents/base/verification.py:132,159,469 already pairs the two tools, so this isn't a new abstraction — just applying _imports_gaia_tools to the file's contents. run_shell_command (src/gaia/agents/tools/shell_tools.py:1821) admits python -c "from gaia import ..." for the same reason, though that one is a longer stretch.
The string case, running the pattern exactly as shipped:
docstring, own line '"""\nfrom gaia import write_file\n"""' -> 'from gaia import write_file' # matches
comment, own line '# from gaia import write_file' -> None # correct
inline mention "print('from gaia import x')" -> None # correct
The parametrized negative case covers the inline mention, which passes; the own-line-inside-a-string case is the one that slips. Also not matched: x = 1; from gaia import y, __import__('gaia'), importlib.import_module('gaia') — fine for a nudge aimed at what a model actually writes, just not airtight.
Smaller notes.
- The test plan says 26 pass; it's 30 now (15 new is right) — the merge from
mainpulled in 4. python util/lint.py --allis green but never reads the changed file:util/lint.py:30-31,47scanssrc/gaiaandtestsonly. I ranblack --checkon it directly — clean, so nothing to fix, but the checkbox isn't evidence for that file.- The guard also refuses a plain
import gaia, which succeeds onmain(print(gaia.__version__)works there). Defensible, just broader than the docstring's "from gaia import <tool>does not work".
On #4084's third bullet (no loop-breaker on repeated failing calls): one does exist — src/gaia/agents/base/agent.py:7009-7041, max_consecutive_repeats default 4 — but it keys on byte-identical (tool_name, str(tool_args)). A model that reads the error as a typo varies the name each retry, so it never fires and the step budget runs out instead. That looks worth its own issue, and it'd cover the generic case (import pandas fails the same way today).
A task whose first step was a tool outside the always-loaded set could end with nothing to show for itself — no summary, no file, no error worth acting on. The model reached for
run_pythonto invoke that tool, got back a bareImportError, read it as a typo worth retrying, and reissued the same call until the step budget ran out. On the reporter's repro that was 2 of 4 runs producing no output at all. Now the unusable call is refused before it spawns, with an error that points at calling the tool directly, and the model moves on instead of looping.Closes #4084
Test plan
python -m pytest tests/unit/test_run_python_tool.py— 26 pass (15 new: detector positives/negatives, the refusal, no subprocess spawned, docstring content)python util/lint.py --allpassesrun_pythonstill work — ask an agent to compute something from a CSV in the project and confirm onerun_pythoncall succeeds with no extra round triprun_python(code="from gaia import write_file")returnsstatus: errornaming the offending line🔍 Technical details
Why the error was misleading.
run_pythonputs the project root onPYTHONPATH, soimport gaiaresolves the package and fails on the symbol —cannot import name 'transcribe_media' from 'gaia'. That reads like a wrong name, not a wrong approach, which is what made retrying look reasonable.Scope. Detection is a single anchored regex over
from gaia[.x] import .../import gaia[.x], matched per line withre.MULTILINE:(?![\w.])after the module keepsgaiatools,gaia_helper, andgaia_agent_chat— a real, importable package — out.Refusing before
subprocess.runmeans the guard costs nothing on the normal path: one regex against the snippet, and no process spawned when it fires.Verified live. On merged
mainthe repro loopedrun_pythonand died at the step cap. On this branch the guard fires once, the model reads the correction, and does not reissuerun_python— 0 retries after the refusal.Scope note. This fixes the loop, not every way the turn can fail. In the same live run the model's next guess was
read_fileon the.mp4, so that task still did not complete — a separate wrong-tool choice, not this one. The measured claim here is narrow: therun_pythonretry loop no longer eats the turn.