Hi all. I would like feedback on a feature idea for Deep Agents.
The problem
When an agent reads a file and the file changes afterwards, edit_file still succeeds as long as old_string matches. The agent keeps working from an outdated view of the file and gets no signal.
Example with FilesystemBackend: the agent reads config.py, I rename a function at the top of the file in my editor, and the agent then edits another line using the old function name. The edit returns success, and the file now calls a function that no longer exists. I have a short script that reproduces this on deepagents 0.7.38 and can share it.
The same can happen when the agent runs a formatter through execute, when a checkpointed thread is resumed later, or when two subagents share a file. I have only reproduced the first case.
The idea
Track what the agent has read, in FilesystemMiddleware:
- When
read_file,write_file, oredit_filesucceeds, store a hash of the file content for that path in agent state. - When
edit_filetargets a path that has a stored hash and the current content differs, return an error asking the model to read the file again. - When
edit_filetargets a path with no stored hash, behave exactly as today.
Point 3 keeps edits on unread files working, which the evals rely on. The check needs no change to the backend protocol, and it could be opt-in.
A possible second step is to tell the model about changed files before a model call, using the same hashes. That would also help an agent that answers from an outdated read without editing. I see open questions there about prompt caching and cost, so I would treat it separately.
Related discussion
The same problem came up in the LangGraph category, in Are people hitting race conditions in multi-agent LangChain setups?. There, two agents edited the same repo and one silently overwrote part of the other’s work. @wfh you described the pattern I am proposing in that thread: track the last read, and only commit the write if the agent has read the file since the last edit.
- Is this something the maintainers would want in the SDK?
- Should it be opt-in or on by default?
- Is agent state the right place for the stored hashes?
Related: https://github.com/langchain-ai/deepagents/issues/7733 is a small bug report I opened about the edit_file description, which I found while looking into this.
Thanks!