Repository navigation
Description
Title
FileNotFoundError from entry.stat() in helpers/file_tree.py kills chats when files are deleted during the directory scan (TOCTOU race)
Summary
Chats intermittently die with FileNotFoundError: [Errno 2] No such file or directory: '.../state.json' raised from helpers/file_tree.py while the framework prepares a prompt. The project file-structure snapshot (built by the message_loop_prompts_after workdir-extras extension on every prompt) scans directories with os.scandir() and only calls entry.stat() on the collected DirEntry objects afterwards. Any file deleted inside that window — e.g. transient job state like experiments/agent-hub-v2/state.json churned by agents or background jobs — makes the stat raise and the exception propagates through prepare_prompt, aborting the whole chat turn. The error is intermittent because the deletion must land inside the scan-to-stat window, which is why it recurs in busy workdirs but is hard to reproduce on demand.
Reproduction
- Have a project workdir containing a subfolder whose files are created and deleted frequently (e.g. job state files).
- Run
file_tree()(directly or via prompt preparation) while another thread/process deletes files in that subfolder. - Observe
FileNotFoundErrorfromentry.stat(follow_symlinks=False)in_create_folder_unprocessed_comment(ormake_entry), crashing the caller.
A deterministic in-process reproduction: list a directory with os.scandir, delete all listed files, then call the internal tree-building path on the stale entries — it raises before any fix.
Root cause
helpers/file_tree.py collects os.DirEntry objects in _list_directory_children() and stats them later:
make_entry()(called from the main scan loop):def make_entry(entry: os.DirEntry, ...) -> _TreeEntry: stat = entry.stat(follow_symlinks=False)
_create_folder_unprocessed_comment()stats the leftover hidden-folder/file entries after the fact:for entry in folders: stat = entry.stat(follow_symlinks=False) ... for entry in files: stat = entry.stat(follow_symlinks=False)
os.scandir entries cache DirEntry metadata lazily; once the underlying path is gone, entry.stat() raises FileNotFoundError. The module already handles the directory-vanishing case (except FileNotFoundError around os.scandir calls), but not the entry-vanishing case, so a single deleted file escapes as an unhandled exception through projects.get_file_structure → the workdir-extras extension → Agent.prepare_prompt, failing the loop.
Proposed fix
Add a small helper and use it at the three entry.stat call sites:
def _safe_entry_stat(entry: os.DirEntry) -> os.stat_result | None:
try:
return entry.stat(follow_symlinks=False)
except (FileNotFoundError, OSError):
return Nonemake_entry()returnsNonefor vanished entries; the scan loop filters theNones out._create_folder_unprocessed_comment()continues past vanished entries (it already returnsNonegracefully when the whole folder is gone).
A more conservative alternative that changes no signatures: wrap each entry.stat() in try/except FileNotFoundError and skip the entry locally at each site. The helper version keeps the tolerance policy in one place.
Before/after
| Scenario | Before | After |
|---|---|---|
| File deleted between scandir and stat | FileNotFoundError kills prompt preparation / chat turn |
Entry silently skipped, tree renders |
| Whole scanned directory deleted mid-walk | Already tolerated (except FileNotFoundError) |
Unchanged |
| Entry replaced by a broken symlink | Potential OSError crash |
Tolerated, skipped |
| Normal tree building | Unchanged | Unchanged |
Verified locally with a regression test suite including a 300-iteration file_tree() run against a thread continuously creating/deleting state*.json files — zero failures after the fix; the same churn crashes within the first iterations before the fix.
Environment
- Agent Zero, Docker deployment, current
main(November 2026). - Crash surfaced via
extensions/python/message_loop_prompts_after/_75_include_workdir_extras.py→helpers/projects.py::get_file_structure→helpers/file_tree.py.
Activity