[Bug] write_file overwrites a file that changed on disk after the agent read it
Description
write_file has no stale-read protection. If a file changes on disk after the agent read it, for example because the user saved an edit in their IDE while the agent was working, a whole-file write_file silently overwrites those changes.
hasSeenFile in source/tools/runtime/read-tracker.ts only answers "has this file been read at any point in this process". It records no mtime or size, and the write_file validator (source/tools/file-ops/write-file.tsx) does not compare the file's current state with what the model last saw. A search of the file tools for mtime handling finds none.
string_replace and diff_edit re-read the file at edit time, so they are only partly exposed (they fail or apply to the newer content). write_file replaces everything.
Environment
- OS: macOS (platform independent)
- Node version: v22.23.4
- Nanocoder version: 1.32.0 (main at 1b6571cc)
- Provider: any
- Model: any
Steps to Reproduce
- Ask the agent to refactor a file. Let it call
read_fileonsrc/foo.ts. - While the model is still thinking or a confirmation prompt is open, edit and save
src/foo.tsin an editor. - Approve the model's
write_filecall for the same path with its full new contents.
Expected Behavior
The write is refused (or the user is warned in the confirmation) because the file changed since it was read, and the model is told to re-read it first.
Actual Behavior
The file is overwritten with the model's version. The user's edit is lost. If the project is not under version control, it cannot be recovered. Not run end to end, derived from reading the validator and the read tracker.
Logs/Screenshots
Not applicable.
Additional Context
- [x] I have searched existing issues to ensure this is not a duplicate
- [ ] I can reproduce this issue consistently
- [x] This issue occurs with the latest version of nanocoder
Suggested direction: store {mtimeMs, size} (or a content hash) in the read tracker when a file is read or written by the agent, and have the write_file validator reject, with a clear message, when the current stat differs. The same record could drive a warning in the confirmation dialog for string_replace and diff_edit. The existing "must read before edit" check is the natural place for it.
I would like to take this issue. I noticed the still-open PR #1817 changes some of the same read/write tracking code, so I am checking whether this should be a separate follow-up or consolidated with that work. My local draft specifically stores the exact version returned by read_file and rechecks content and path immediately before write_file, including same-size edits with restored mtimes. If this scope is appropriate, please assign me and I will open a draft PR; otherwise I can adapt to the preferred direction.
Hi @ducanhnguyen223, thanks for offering. I've assigned this to you.
On #1817: it is still open and currently has conflicts with main. Its branch changes read-tracker.ts and write-file.tsx, which are the same code this issue touches, and the tracker has since moved to source/tools/runtime/read-tracker.ts on main. Please base your PR on current main and keep it separate from #1817.