Because those changes can alter the meaning of a previously read value without the harness announcing it. The agent keeps reasoning from the old description, so a command that looked safe when read can become destructive by the time it runs.
Why branch switches and file edits change the meaning of an agent’s next action
Branch switches and file edits are dangerous because they invalidate the assumptions behind the model’s earlier reading. A coding agent may be reasoning over one version of a file, then execute a command against a different version without noticing the context shift. That mismatch turns a previously safe step into something that can delete, overwrite, or misconfigure the wrong target.
In practice, the hazard is not just that files changed, but that the agent’s internal plan often lags behind the repository state. If the harness does not force a fresh read or revalidation, the agent can keep acting on stale intent. That is why apparently harmless edits, renames, branch checkouts, or dependency changes can have outsized impact in agentic workflows.
The problem gets worse when the agent treats file content as a stable instruction surface. A modified prompt, config, script, or instruction file can redirect tool use, change command arguments, or alter which paths are considered safe. At the same time, a branch switch can swap in a different build, different permissions assumptions, or different deployment targets, which means the agent may still “understand” the old environment while operating in the new one.
Where the risk becomes material in coding workflows
The risk becomes material whenever the agent can both observe repository content and take side effects based on that content. In those settings, the repository is not just source code, it is also an input channel that can reshape the agent’s behaviour. That is the same trust problem discussed in AI Coding Agents Security Guide, where agent instructions, secrets in context, and sandboxing all affect how much damage a stale or manipulated view can cause.
File edits are especially risky when they affect files the agent uses as control signals, such as instructions, build manifests, dependency locks, or scripts. A small change there can shift the agent from a read-only or low-risk path into a destructive one, even if the original task was benign. Branch switches create a similar effect by changing the codebase state under the same task, so the agent’s prior judgment no longer matches what is now on disk.
This is also why branch-sensitive behaviour should be treated as an authorization and execution problem, not only as a code-quality problem. If a command was approved against one tree state, it may not be acceptable against another. Guidance in AI Agent Authorisation Guide is useful here because it frames access as task-scoped and per-action, which is exactly the kind of boundary needed when the target environment can change underneath the agent.
How practitioners should defend against stale context and silent repository drift
Defence starts with forcing the agent to prove that its next action still matches the current state of the workspace. That means re-reading affected files after branch switches, re-resolving paths after edits, and treating any repository mutation as a possible invalidation event. If the agent cannot show the current content it is acting on, the harness should assume its reasoning is stale.
Human reviewers should pay special attention to commands that touch generated files, build outputs, lockfiles, scripts, and automation metadata, because those are the places where a small edit can change the meaning of a large action. The safest pattern is to narrow each tool call to a single, well-bounded intent and require confirmation when the observed file state no longer matches the precondition that was originally approved. That approach aligns with the control logic in AI Agent Observability, Audit and Incident Response Guide, which emphasizes attribution, logging, and kill-switch readiness when agent behaviour drifts.
For teams building agent harnesses, the key design choice is whether the agent is allowed to continue from cached understanding after the workspace changes. If the answer is yes, you are relying on memory across a mutable state boundary, which is usually the wrong trade-off for anything that can delete or deploy. If the answer is no, make the refresh explicit and cheap enough that the agent can safely revalidate before every consequential write.
Risk and Threat Considerations
Repository state drift creates a real attack surface because an adversary only needs to change what the agent sees, not directly compromise the agent’s reasoning. A branch switch, edited instruction file, poisoned script, or altered dependency can convert an ordinary follow-up command into destructive behaviour before the harness notices the mismatch.
Failure mechanism: The agent continues to act on an earlier mental model while the actual files, branch, or execution context have changed. That stale context can cause the next tool call to target the wrong path, execute the wrong command, or trust content that was never revalidated.
Impact: The result can be accidental deletion, misdeployment, credential exposure, or attacker-guided code execution, especially when the changed file is itself used to steer automation or approvals.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP Agentic AI Top 10 addresses the attack and risk surface, while NIST SP 800-53 Rev 5 sets the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP Agentic AI Top 10 | ASI03 — Identity & Privilege Abuse | Branch and file drift can redirect agent authority and action scope. |
| ASI02 — Tool Misuse | Stale repository context can make a safe tool call harmful after edits or checkout. | |
| ASI08 — Cascading Failures | A small repository change can trigger a chain of incorrect agent actions. | |
| Recommendation — Require state revalidation before any action that could use changed authority or context. Constrain tool use to the current file state and recheck inputs before execution. Add change-triggered checkpoints before chained agent operations can continue. | ||
| NIST SP 800-53 Rev 5 | CM-3 — Configuration Change Control | Branch switches and edits are configuration changes that should be controlled. |
| AU-6 — Audit Record Review, Analysis, and Reporting | Agent actions need traceability when repository state changes alter execution meaning. | |
| Recommendation — Gate meaningful workspace changes through change control and revalidation. Log state changes and review agent actions against the file version they used. | ||
Practitioner Guidance
What to verify: Require a fresh read of any file, branch, or configuration that could change the command’s meaning before allowing a write, delete, or deploy action. If the agent cannot name the exact current branch and file version it relied on, treat the step as untrusted.
Common mistake: Teams often assume the agent’s earlier analysis still applies after a checkout or edit because the task text has not changed. In practice, the environment changed even if the prompt did not, so the approval should expire with the state it described.
Decision rule: If the planned action depends on repository content that can be altered mid-task, force revalidation and narrow the action scope before execution. If you cannot make the action state-bound and observable, keep it read-only.
Practitioner takeaway: The control objective is not to prevent every file change, but to prevent an agent from executing against a state it has not just revalidated.
Related resources from NHI Mgmt Group
- Why does a long instruction file create more risk for coding agents?
- Why do coding agents create more risk than ordinary automation when credentials are involved?
- Why do AI coding agents create new IAM risk even when prompt injection is addressed?
- Why do AI coding agents create governance risk even when they improve productivity?
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on October 12, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org