Skip to content

fix(tools): report an unreadable file as a file editor error - #6596

Merged
VascoSch92 merged 2 commits into
mainfrom
vasco/file-editor-unreadable
Nov 7, 2026
Merged

VascoSch92 merged 2 commits into
mainfrom
vasco/file-editor-unreadable

Conversation

@VascoSch92

@VascoSch92 VascoSch92 commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

just fixing a bug


AGENT:

Why

Every file editor command that reads a file starts with validate_file, which calls binaryornot.is_binary(path) to open and sniff the file. For a file the agent cannot read (chmod 000, or owned by another user), that open raises PermissionError. It is not a ToolError, so instead of an error observation the agent gets an uncaught exception out of the tool.

Summary

  • In validate_path, which every command runs first, raise FileValidationError ("File is not readable (permission denied).") for an existing file the process cannot read. It has to run that early because insert and undo_edit read the file (encoding detection in @with_encoding) before they reach validate_file.
  • Ports the pinned test from [Bug]: Edge-case tests expose ~70 bugs across the SDK, agent server, tools and workspaces #6348 into test_file_validation.py, parametrized over view, str_replace, insert and undo_edit, with its skipif for Windows and root.

Issue Number

Part of #6348 (item: "unreadable file raises PermissionError instead of a tool error").

How to Test

uv run pytest tests/tools/file_editor

On main all four commands raise PermissionError on a chmod 000 file. Here each returns File validation failed for .../secret.txt: File is not readable (permission denied). tests/tools/file_editor passes (174).

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

🐳 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

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:4f3a396-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-4f3a396-python \
  ghcr.io/openhands/agent-server:4f3a396-python

All tags pushed for this build

ghcr.io/openhands/agent-server:4f3a396-golang-amd64
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-golang-amd64
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-golang-amd64
ghcr.io/openhands/agent-server:4f3a396-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:4f3a396-golang-arm64
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-golang-arm64
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-golang-arm64
ghcr.io/openhands/agent-server:4f3a396-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:4f3a396-java-amd64
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-java-amd64
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-java-amd64
ghcr.io/openhands/agent-server:4f3a396-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:4f3a396-java-arm64
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-java-arm64
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-java-arm64
ghcr.io/openhands/agent-server:4f3a396-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:4f3a396-python-amd64
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-python-amd64
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-python-amd64
ghcr.io/openhands/agent-server:4f3a396-python-node-runtime-amd64
ghcr.io/openhands/agent-server:4f3a396-python-arm64
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-python-arm64
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-python-arm64
ghcr.io/openhands/agent-server:4f3a396-python-node-runtime-arm64
ghcr.io/openhands/agent-server:4f3a396-python-minimal-amd64
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-python-minimal-amd64
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-python-minimal-amd64
ghcr.io/openhands/agent-server:4f3a396-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:4f3a396-python-minimal-arm64
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-python-minimal-arm64
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-python-minimal-arm64
ghcr.io/openhands/agent-server:4f3a396-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:4f3a396-python-slim-amd64
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-python-slim-amd64
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-python-slim-amd64
ghcr.io/openhands/agent-server:4f3a396-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:4f3a396-python-slim-arm64
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-python-slim-arm64
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-python-slim-arm64
ghcr.io/openhands/agent-server:4f3a396-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:4f3a396-golang
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-golang
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-golang
ghcr.io/openhands/agent-server:4f3a396-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:4f3a396-java
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-java
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-java
ghcr.io/openhands/agent-server:4f3a396-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:4f3a396-python-minimal
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-python-minimal
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-python-minimal
ghcr.io/openhands/agent-server:4f3a396-python-node-runtime-minimal
ghcr.io/openhands/agent-server:4f3a396-python-slim
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-python-slim
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-python-slim
ghcr.io/openhands/agent-server:4f3a396-python-node-runtime-slim
ghcr.io/openhands/agent-server:4f3a396-python
ghcr.io/openhands/agent-server:4f3a39620abfa1cdac970fac678e6ffd72288b6c-python
ghcr.io/openhands/agent-server:vasco-file-editor-unreadable-python
ghcr.io/openhands/agent-server:4f3a396-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 4f3a396-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 4f3a396-python-amd64) are also available if needed

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.
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-tools/openhands/tools/file_editor
   editor.py32019340%49–53, 101, 104, 124, 134–140, 146–154, 156, 195–196, 200, 204–205, 214, 221–223, 231–232, 236–238, 244, 247, 252, 255, 258–259, 262, 265–266, 270, 274, 290–291, 298–302, 310, 314–315, 319, 324, 335–338, 340–343, 346–347, 356–357, 363–364, 381–382, 388–390, 398–402, 406, 408–409, 416–417, 419, 422–423, 425, 433–435, 437–440, 443, 446–453, 458–460, 477–478, 493, 497, 501, 506, 531–533, 555–556, 558–559, 565, 570–577, 581–582, 585–586, 589–594, 596, 599–600, 604, 607–608, 611, 613–614, 620, 624, 643, 646–649, 651, 659, 666, 674, 679, 690–693, 695, 697, 727, 742–744, 757, 784–791, 793, 800–801, 812, 826, 831, 848
TOTAL543592066762% 

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@VascoSch92
VascoSch92 marked this pull request as ready for review November 7, 2026 16:24

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, insert and undo_edit still escaped with a raw PermissionError because the @with_encoding wrapper detects the file's encoding before validate_file runs.
  • On this head the readability check lives in validate_path (editor.py:673), which __call__ runs first for every command, so view, str_replace, insert, and undo_edit on a chmod 000 file all return File validation failed for ...: File is not readable (permission denied). as an error observation, and create on 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

@VascoSch92
VascoSch92 enabled auto-merge (squash) November 7, 2026 21:58
@VascoSch92
VascoSch92 merged commit 1bc8d25 into main Nov 7, 2026
43 of 45 checks passed
@VascoSch92
VascoSch92 deleted the vasco/file-editor-unreadable branch November 7, 2026 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants