Agent should stop and confirm when an expected file is missing, not silently write to a different location
Type
Bug / agent behavior (safety)
Summary
When a file the user explicitly asked the agent to edit turns out not to exist where expected, the agent should stop and surface the anomaly rather than silently locating a same-named file elsewhere and writing there. In a real session this silent fallback caused all edits to land in an unexpected working tree.
What happened
While working in a git worktree session (-w), the agent attempted to edit a file using the correct worktree-absolute path and got:
<tool_use_error>File does not exist. Note: your current working directory is
<repo>/.claude/worktrees/<name>/packages/rrweb.</tool_use_error>
Instead of treating "a file I was just asked to edit is missing from the worktree I'm supposed to be working in" as a red flag, the agent resolved the same relative path against the parent repository (where an identically-named file did exist) and proceeded to make ~70 edits there. The user only discovered much later that all the work had landed on the main checkout's branch (master) instead of the worktree branch.
The underlying reason the file was missing: the worktree had been created from a stale base, so it genuinely didn't contain the feature files (details in #70069). But regardless of why the file was missing, the agent's response is the issue here.
Expected behavior
A missing expected file is a strong signal that an assumption is wrong (wrong branch, stale/incorrectly-based worktree, wrong directory, file moved/renamed, etc.). The agent should:
- Stop and report the anomaly, with the likely causes ("this file doesn't exist in the worktree — you may be on the wrong branch, or the worktree may be based on a stale ref").
- Not silently switch to writing to a different location, especially one outside the current working tree / worktree boundary.
- Ask the user how to proceed rather than guessing.
Why it matters
Silently redirecting writes to an unexpected location can:
- Defeat worktree/branch isolation (work lands on the wrong branch).
- Produce changes the user can't find or didn't intend.
- Hide a setup mistake (here, a wrongly-based worktree) that the user would have wanted to fix immediately.
This is a general safety principle beyond worktrees: an unexpected "file not found" for something the user explicitly referenced should pause for confirmation, not trigger an automatic search-and-substitute.
Related
- #70069 (the worktree-creation defect that caused the file to be missing in the first place)
Environment
- Claude Code v2.1.81
- Linux
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗