fix(computer): clear error when file_read_tool is given a directory (#9087)#9093
fix(computer): clear error when file_read_tool is given a directory (#9087)#9093CedricConday wants to merge 1 commit into
Conversation
astrbot_file_read_tool opened the path directly, so passing a directory
surfaced a misleading OS error ('[Errno 13] Permission denied' on Windows,
IsADirectoryError on Linux) instead of saying it's a directory.
Add a proactive is_dir() guard in read_file_tool_result (local mode) that
returns a clear message pointing to astrbot_execute_shell for listing.
Fixes AstrBotDevs#9087
Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request adds a check to prevent reading directories as files in local mode, returning a clear error message instead, and includes a corresponding unit test. The reviewer recommends wrapping the synchronous Path.is_dir() check in to_thread to prevent blocking the asyncio event loop.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| workspace_dir: str | None = None, | ||
| ) -> ToolExecResult: | ||
| if local_mode: | ||
| if Path(path).is_dir(): |
There was a problem hiding this comment.
In an asyncio event loop, performing synchronous I/O operations like Path.is_dir() directly on the main thread can block the event loop and degrade the responsiveness of the entire application. Since to_thread is already imported in this file, it is highly recommended to run this check asynchronously in a separate thread.
| if Path(path).is_dir(): | |
| if await to_thread(Path(path).is_dir): |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
Path(path).is_dir()check ignoresworkspace_dir, so a relative path that’s resolved against the workspace in_probe_local_filemay not exist from the process CWD; consider reusing the same path resolution logic used in_probe_local_fileto ensure the directory check behaves consistently with file probing. - The clearer error message is only applied in
local_mode; if the remote mode is still user-facing, you may want to mirror the same directory detection and messaging there for consistent behavior across modes.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `Path(path).is_dir()` check ignores `workspace_dir`, so a relative path that’s resolved against the workspace in `_probe_local_file` may not exist from the process CWD; consider reusing the same path resolution logic used in `_probe_local_file` to ensure the directory check behaves consistently with file probing.
- The clearer error message is only applied in `local_mode`; if the remote mode is still user-facing, you may want to mirror the same directory detection and messaging there for consistent behavior across modes.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
#9088 fixed. Thanks |
问题 / Problem
astrbot_file_read_tool在传入目录路径时会直接open(),于是返回了误导性的系统错误(Windows 上为[Errno 13] Permission denied,Linux 上为IsADirectoryError),而不是告诉用户传入的是一个目录。详见 #9087。When
astrbot_file_read_toolis given a directory path, it opened the path directly, so the LLM/user saw a misleading OS error ([Errno 13] Permission deniedon Windows,IsADirectoryErroron Linux) instead of being told it's a directory.修复 / Fix
在
read_file_tool_result(local 模式)探测文件前增加Path(path).is_dir()主动判断,返回清晰提示并引导改用astrbot_execute_shell列出目录内容。跨平台一致,不依赖具体的 errno。Add a proactive
Path(path).is_dir()guard inread_file_tool_result(local mode) before probing, returning a clear message and pointing toastrbot_execute_shellfor directory listing. Cross-platform — it doesn't depend on the OS-specific errno.返回信息 / Returned message:
测试 / Test
新增
test_file_read_tool_reports_directory_clearly(tests/test_computer_fs_tools.py):传入目录时返回包含 “is a directory”、引导使用astrbot_execute_shell且不再包含 “Permission denied” 的提示。Adds
test_file_read_tool_reports_directory_clearlycovering the directory case.AI 协助、人工审阅 / AI-assisted, human-reviewed — I'm an AI engineer; I find, fix, and test with AI, then review and verify before opening it under my name.
Summary by Sourcery
Clarify behavior of the file read tool when invoked on directories in local mode to provide a user-friendly, cross-platform error message.
Bug Fixes:
Tests: