A code-refactoring agent loads repository state at the start of a long task. After 45 minutes of operation, the agent continues making changes based on its initial snapshot, but 12 other PRs have been merged in the meantime, creating conflicts. Which technique best prevents this context drift?
Select an answer to reveal the explanation.
Short Explanation and Infographic
It's like a surgeon who paused mid-operation, walked away for an hour, and came back assuming the patient's condition hasn't changed — dangerous. On the exam: long-running agents must periodically validate that their cached state still matches reality, and either self-correct or halt when it doesn't.
Full explanation below image
Full Explanation
Context drift occurs when an agent operates on a stale snapshot of external state while the real-world state continues to change. For a long-running agent, this is especially dangerous because the gap between perceived and actual state grows over time. The correct approach (option D) is to implement periodic state validation — at defined intervals or before committing changes, the agent re-reads critical metadata (e.g., HEAD commit hash, branch state, file modification times) and compares it to its working snapshot. If divergence exceeds a threshold, the agent can either self-correct by rebasing its work or halt and escalate for human review.
Option A is incorrect because speed alone cannot guarantee correctness — in a busy repository, 12 PRs can merge in minutes. Rushing also increases error risk. The root cause is the lack of drift detection, not execution speed.
Option B describes a similar concept but frames it as 'halt only' behavior, missing the correction capability. However, option D is preferred because it explicitly includes both correction and halting as responses, making it the more complete and accurate answer.
Option C is partially correct practice (isolated branches are good hygiene) but does not address drift detection itself. An isolated branch can still drift from its base while the agent works, and rebasing at the end is a single point of failure — if the agent has made 100 changes based on stale state, late detection means much more rework than periodic validation would have required.