Skip to content

fix(agents): name a directory instead of leaking a raw errno from file tools - #3891

Merged
kovtcharov-amd merged 3 commits into
amd:mainfrom
AmirF194:fix/3890-directory-path-errno21-guard
Sep 17, 2026
Merged

kovtcharov-amd merged 3 commits into
amd:mainfrom
AmirF194:fix/3890-directory-path-errno21-guard

Conversation

@AmirF194

Copy link
Copy Markdown
Contributor

Summary

read_file, edit_python_file and replace_function all raised a raw IsADirectoryError when pointed at a directory instead of telling the model what went wrong.

Why

os.path.exists() is true for a directory, so the existing-path guard at all three call sites let it through to open(). The IsADirectoryError lands in the generic except Exception handler and comes back as "[Errno 21] Is a directory: '...'", which isn't something a model can act on, it just retries the same call. Your own comment on the issue confirmed the same shape hits edit_python_file and replace_function too.

Linked issue

Fixes #3890

Changes

  • Added os.path.isdir() right after the existing os.path.exists() check in all three tools, returning a message that names the directory instead of the errno.
  • Kept the message tool-agnostic rather than naming browse_directory, per your comment, since it isn't registered on every agent that composes this mixin.
  • New test file covering all three call sites: a directory returns the new message, a real file and a missing path still behave as before.

Test plan

  • pytest tests/unit/test_file_io_directory_path_errors.py -v: 5 passed. Reverting just the isdir check makes the three directory-case tests fail with the raw errno back in the message.
  • pytest tests/unit/test_file_write_guardrails.py tests/unit/test_replace_function_spans.py tests/unit/test_file_editing_prompt.py tests/unit/test_file_access_scope.py tests/unit/agents/test_file_edit_semantics.py -v: all still pass, in the same python:3.12-slim container both times.
  • black, isort (pinned 8.0.1), flake8 and pylint (errors-only) clean on both changed files.

Evidence

  • Agent exposed in the Agent UI: N/A. This is a return-value fix in a tool function, no UI of its own to screenshot.
  • MCP tools / servers: N/A, same reason.
  • CLI: N/A, these three tools aren't exposed as gaia subcommands.
  • HTTP API / REST: N/A, same reason.

Exercising it live needs an agent session against a model server, which isn't available in this environment; the tests above call the registered tool functions directly, which is the same code path a live session would hit.

Checklist

  • Linked a GitHub issue (Fixes #3890).
  • Described why, not just what.
  • Ran linting and tests locally.
  • Evidence surfaces marked N/A with reason.
  • No user-visible docs to update, this only changes an error string an agent reads.

…e tools

read_file, edit_python_file and replace_function all guard on
os.path.exists(), which is true for a directory. Pointing any of them at
one reached open() and surfaced IsADirectoryError as an unstructured
"[Errno 21] Is a directory: ..." string, which an agent has no way to act
on and just retries.

Add an os.path.isdir() check right after the existence check at all three
call sites, returning a message that names the problem instead. Fixes amd#3890.
@kovtcharov

Copy link
Copy Markdown
Contributor

Thanks for this — it fixes exactly what we saw in benchmark runs, where read_file on a folder came back as a raw Errno 21 and the agent retried the same call until the loop guard stopped it.

One more place has the same gap: the read_file in src/gaia/agents/tools/file_tools.py (registered by the chat profiles) still returns the raw [Errno 21] Is a directory for a folder. Would you be up for applying the same check there so both variants behave the same? (filesystem_tools.py already handles directories.)

…lsMixin.read_file

The FileIOToolsMixin variant already guards against os.path.exists()
being true for a directory (amd#3890/amd#3891); this mixin registers its own
read_file and had the same gap.
@AmirF194

Copy link
Copy Markdown
Contributor Author

Good catch, confirmed the same gap in file_tools.py's read_file (open() on a directory raises IsADirectoryError into the generic except Exception, same raw errno). Pushed a matching isdir guard there plus a test, 2a3effe.

@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

The fix is correct and complete. All four call sites that could pass a directory to open() are guarded, the helper is DRY in file_io_tools.py, and the slightly more specific message in file_tools.py (naming search_directory) is the right call. Tests cover the new error path plus the real-file and missing-file happy/regression paths where they matter most.

One 🟢 nit:

The inline comment in file_tools.py cites (amd/gaia#3891) — the PR number — where the bug lives at issue #3890. The wrong number sticks in the git history and confuses anyone tracing it later.

🔍 Technical details

src/gaia/agents/tools/file_tools.py:654 — (amd/gaia#3891) should be (amd/gaia#3890). #3891 is this PR; #3890 is the issue the PR fixes.

                # os.path.exists() is true for a directory too, so without this
                # check open() below raises IsADirectoryError into the generic
                # except Exception handler as a raw errno string (amd/gaia#3890).

@AmirF194

Copy link
Copy Markdown
Contributor Author

Good catch, fixed in 7f005f2. The comment cited the PR number where it meant the issue.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Sep 17, 2026
Merged via the queue into amd:main with commit 9051c02 Sep 17, 2026
34 checks passed
@AmirF194
AmirF194 deleted the fix/3890-directory-path-errno21-guard branch September 17, 2026 19:27
pull Bot pushed a commit to bhardwajRahul/gaia that referenced this pull request Sep 25, 2026
… project (amd#3902)

Asking the agent to run a project's test file used to fail before any
test ran. `execute_python_file` ran every script from the script's own
folder, so `tests/test_dates.py` doing `from toybox import dates` died
with `ModuleNotFoundError: No module named 'toybox'`: the project root
was neither the working directory nor on the import path. Now a script
inside the agent's project directory runs from that directory, with it
on `PYTHONPATH`, so project tests import the project the way they do
under pytest. A script outside the project still runs from its own
folder.

Found in the same 168-run Fireworks benchmark as amd#3890. Models made the
identical failing call over and over, and in two runs the loop guard
ended a task that was already finished.

The directory half of amd#3890 (`read_file` on a folder) is left to amd#3891.

<details>
<summary>🔍 Technical details</summary>

- The project directory comes from `resolve_project_root`: the config's
`project_root`, then `GAIA_PROJECT_ROOT`, then the nearest code
repository at or above the working directory, never the home directory.
The flagship defers to its project map's cached root, so the map and a
script's working directory always name the same tree. With no project, a
script runs from its own folder with the environment untouched. An
existing `PYTHONPATH` is kept, after the project root.
- Only flat-layout projects become importable; a `src/` layout would
need `src/` on the path.
- The sandbox check (`is_path_allowed`) and existence check are
unchanged and still run first.
- One behavior change: a project script that opens files by a path
relative to its own folder now resolves them against the project root.
The tool docstring says where scripts run, so the model can see this.
- The docstring is part of the LLM-visible tool schema. No agent eval
was run for this PR.
- amd#3891 fixes `FileIOToolsMixin.read_file` but not
`FileSearchToolsMixin.read_file` in `file_tools.py`, which the chat
profiles also register and which still hits the raw errno.
</details>

## Test plan

- [x] `pytest tests/unit/test_execute_python_file_cwd.py` (9 passed): a
temp project's `tests/test_x.py` doing `from pkg import mod` runs
through the registered tool with return code 0 and cwd = project root,
also when launched from `tests/`. Also covers the no-project case, a bad
`GAIA_PROJECT_ROOT`, `PYTHONPATH` preservation, and a real `GaiaAgent`
whose script cwd follows its project map after a `chdir`.
- [x] Related suites pass: `tests/unit/test_chat_agent_integration.py`,
`tests/unit/agents/test_tool_arg_coercion.py`,
`tests/unit/agents/test_confirmation_required_tools.py`,
`tests/unit/test_starter_skills.py`, the prompt-budget tests,
`hub/agents/chat/python/tests` (265 passed, 15 skipped)
- [ ] `gaia chat` in a project with a `tests/` folder: ask it to run one
test file and confirm the imports resolve

---------

Co-authored-by: Kalin Ovtcharov <[email protected]>
Co-authored-by: Ovtcharov <[email protected]>
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.

fix(tools): read_file on a directory returns a raw Errno 21 the agent can't act on

3 participants