Feature proposal: reject `edit_file` when the file changed since the agent read it

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:

  1. When read_file, write_file, or edit_file succeeds, store a hash of the file content for that path in agent state.
  2. When edit_file targets a path that has a stored hash and the current content differs, return an error asking the model to read the file again.
  3. When edit_file targets 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!

I’d make it opt-in and store hashes in the checkpoint state. Just remember, checking a hash and editing later leaves a small race condition. Someone could change the file in between. Proper protection needs conditional writes or locking. I’d definitely test that scenario too, not just changes right after reading. Otherwise, the safety net has a hole :grinning_face_with_smiling_eyes:

Hi @damat-le, welcome to the community, and thanks for the clear write-up. I reproduced this on deepagents 0.7.41: after a file is changed outside the agent, edit_file still returns success as long as old_string matches. Nothing tracks whether the file changed since it was read.

I’ll flag this to the OSS team. I can’t speak for the maintainers on whether it would be accepted or what the default should be, so the design questions are theirs to answer. You’re also welcome to open a feature request on the deepagents repo and link this thread:Issues · langchain-ai/deepagents · GitHub

@press288’s point about the gap between the check and the write is worth keeping in the proposal.

Point 3 is where the execute and resume cases can slip through. If the middleware clears the stored hash after an execute call or on resume, the next edit_file looks like an edit to an unread path and skips the check. Re-hashing after execute has the same problem, since the formatter’s output becomes the baseline and the edit goes through on content the model never saw.

Keeping the old hash, or overwriting it with a sentinel that never matches a real hash, makes the reread error fire, and unread paths still behave as today.