Skip to content

fix(computer): clear error when file_read_tool is given a directory (#9087)#9093

Closed
CedricConday wants to merge 1 commit into
AstrBotDevs:masterfrom
CedricConday:fix/file-read-tool-directory-message
Closed

fix(computer): clear error when file_read_tool is given a directory (#9087)#9093
CedricConday wants to merge 1 commit into
AstrBotDevs:masterfrom
CedricConday:fix/file-read-tool-directory-message

Conversation

@CedricConday

@CedricConday CedricConday commented Jun 30, 2026

Copy link
Copy Markdown

问题 / Problem

astrbot_file_read_tool 在传入目录路径时会直接 open(),于是返回了误导性的系统错误(Windows 上为 [Errno 13] Permission denied,Linux 上为 IsADirectoryError),而不是告诉用户传入的是一个目录。详见 #9087

When astrbot_file_read_tool is given a directory path, it opened the path directly, so the LLM/user saw a misleading OS error ([Errno 13] Permission denied on Windows, IsADirectoryError on 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 in read_file_tool_result (local mode) before probing, returning a clear message and pointing to astrbot_execute_shell for directory listing. Cross-platform — it doesn't depend on the OS-specific errno.

返回信息 / Returned message:

Error: <path> is a directory, not a file. Provide a file path instead, or use astrbot_execute_shell to list directory contents.

测试 / Test

新增 test_file_read_tool_reports_directory_clearlytests/test_computer_fs_tools.py):传入目录时返回包含 “is a directory”、引导使用 astrbot_execute_shell 且不再包含 “Permission denied” 的提示。

Adds test_file_read_tool_reports_directory_clearly covering 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:

  • Prevent misleading OS errors by explicitly detecting directory paths in the local file read tool and returning a clear directory-specific error message.

Tests:

  • Add a regression test ensuring the file read tool reports directories clearly, suggests using the shell tool, and no longer surfaces 'Permission denied' errors for this case.

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>
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Jun 30, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
if Path(path).is_dir():
if await to_thread(Path(path).is_dir):

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • 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.
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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Soulter

Soulter commented Jun 30, 2026

Copy link
Copy Markdown
Member

#9088 fixed. Thanks

@Soulter Soulter closed this Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants