Repository navigation
fix(tools): report an unreadable file as a file editor error - #6596
Conversation
validate_file sniffs the file with binaryornot before anything catches errors, so viewing or editing a file without read permission raised a bare PermissionError out of the tool instead of returning an error observation. Check readability first and raise FileValidationError.
Coverage Report •
|
||||||||||||||||||||
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Scope
The change belongs here: FileEditor behavior is owned by this repository, and the fix targets the item "unreadable file raises PermissionError instead of a tool error" in #6348 (currently labeled ready-for-dev). The PR body, head (81a79bd), and diff (+24, two files) all match, and every GitHub Actions check on this head is green.
Verification
I reproduced the reported bug on this head as a non-root user: view on a chmod 000 file now returns File validation failed for .../secret.txt: File is not readable (permission denied). as an error observation instead of an uncaught PermissionError. The ported test test_view_unreadable_file_returns_error_observation passes here (skipped on Windows and for root, which is correct), and uv run pytest tests/tools/file_editor passes (171 tests). I also checked the untested corners: create on an existing unreadable file already fails cleanly (validate_path rejects overwrite), and str_replace is fixed because it runs validate_file before its first file read.
Finding: insert still escapes with a raw PermissionError
The fix sits in validate_file, but not every command reaches validate_file first. insert (and write_file/read_file) carry an encoding parameter, so the @with_encoding wrapper (utils/encoding.py) calls get_encoding(path) → detect_encoding(path) → an unguarded open(path, "rb") before the method body runs. On this head, as a non-root user:
editor(command="insert", path=<chmod 000 file>, insert_line=0, new_str="x")
→ PermissionError: [Errno 13] Permission denied
at utils/encoding.py:72 detect_encoding → :176 get_encoding → :219 wrapper
FileEditorExecutor.__call__ (and the file_editor helper) catch only ToolError, so this exception still escapes the tool exactly as the linked issue describes — the agent gets an uncaught PermissionError instead of an error observation, just via insert instead of view. str_replace dodges it only because its signature dropped the encoding parameter.
Suggested fix: either catch PermissionError (or OSError) around encoding detection in the wrapper and translate it to FileValidationError, or hoist the readability check into validate_path/__call__ so it runs before any decorated method.
This is the one material gap; the rest of the change is correct, minimal, and well tested.
🔄 CHANGES REQUESTED
| if not path.exists() or not path.is_file(): | ||
| return | ||
|
|
||
| if not os.access(path, os.R_OK): |
There was a problem hiding this comment.
This check only protects commands that reach validate_file before their first file read. insert (whose signature keeps the encoding parameter) is wrapped by @with_encoding, whose wrapper calls get_encoding(path) → detect_encoding(path) → an unguarded open(path, "rb") (utils/encoding.py:72) before the method body runs. On this head, as a non-root user, insert on a chmod 000 file still escapes as a raw PermissionError (verified: PermissionError: [Errno 13] raised at encoding.py:72 via the wrapper at :219), which FileEditorExecutor/file_editor do not catch — so the agent still gets an uncaught exception instead of an error observation, the exact symptom this PR fixes for view. Consider translating PermissionError/OSError from encoding detection into FileValidationError (or hoisting the readability check so it runs before any decorated method).
insert and undo_edit read the file (encoding detection) before they reach validate_file, so an unreadable file still raised PermissionError there. Move the check into validate_path, which every command runs first.
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Scope
The change belongs here (FileEditor tool behavior is owned by this repository) and implements the "unreadable file raises PermissionError instead of a tool error" item from #6348 (ready-for-dev). Head (4f3a396), PR body, and diff (+34 across two files) all match.
Verification
I reproduced the original symptom on this environment as a non-root user and verified the fix on the current head through the real tool dispatch (file_editor helper, no mocks):
- On the previous approach,
insertandundo_editstill escaped with a rawPermissionErrorbecause the@with_encodingwrapper detects the file's encoding beforevalidate_fileruns. - On this head the readability check lives in
validate_path(editor.py:673), which__call__runs first for every command, soview,str_replace,insert, andundo_editon achmod 000file all returnFile validation failed for ...: File is not readable (permission denied).as an error observation, andcreateon a new readable file is unaffected.
uv run pytest tests/tools/file_editor passes here (174, matching the PR description). The parametrized test covers all four reading commands with the correct Windows/root skip conditions. No other code path calls FileEditor methods while bypassing __call__, so the earlier-placed check has no remaining gaps. Readable-file behavior is unchanged (os.access(R_OK) passes), root-run containers are unaffected, and on Windows the check is effectively a no-op as before.
This resolves the inline finding from the earlier review of 81a79bd. No material correctness, security, or compatibility findings remain on this head.
✅ APPROVED
HUMAN:
just fixing a bug
AGENT:
Why
Every file editor command that reads a file starts with
validate_file, which callsbinaryornot.is_binary(path)to open and sniff the file. For a file the agent cannot read (chmod 000, or owned by another user), thatopenraisesPermissionError. It is not aToolError, so instead of an error observation the agent gets an uncaught exception out of the tool.Summary
validate_path, which every command runs first, raiseFileValidationError("File is not readable (permission denied).") for an existing file the process cannot read. It has to run that early becauseinsertandundo_editread the file (encoding detection in@with_encoding) before they reachvalidate_file.test_file_validation.py, parametrized overview,str_replace,insertandundo_edit, with itsskipiffor Windows and root.Issue Number
Part of #6348 (item: "unreadable file raises PermissionError instead of a tool error").
How to Test
On
mainall four commands raisePermissionErroron achmod 000file. Here each returnsFile validation failed for .../secret.txt: File is not readable (permission denied).tests/tools/file_editorpasses (174).Type
🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:4f3a396-pythonRun
All tags pushed for this build
About Multi-Architecture Support
4f3a396-python) is a multi-arch manifest supporting both amd64 and arm644f3a396-python-amd64) are also available if needed