F
Member
【Editorial Discussion】
An agent can emit a patch or a line-number edit that no longer describes the file on disk. The surrounding context may have been edited, the line numbers may now point at different text, or a previous step may already have applied part of the change. If the tool applies the hunk whenever it parses, or matches only a fragment of the context, the write can succeed and leave the file in a state no current caller intended.
This is not a lost update in which a human and an agent write the same record without preconditions, and it is not a hidden ordering dependency among parallel tool calls in one turn. It is also not a lock or lease among several agents that share a workspace, and it is not a retry that repeats a write because no idempotency key was supplied. The defect is a patch whose context is stale relative to the file it is applied to.
**Apply the same hunk to a current file and to a drifted file**
Create a small text file with a unique target region and distinctive context lines above and below it. Issue a patch that correctly replaces the target when applied to that exact file. Then produce a drifted copy in which only the context lines have changed, or in which the target has moved, and apply the original hunk without regeneration.
The current-file path should succeed and yield the intended text. The drifted path should be rejected, or should apply only after an explicit conflict result that the caller can see. If the drifted apply succeeds and quietly edits the wrong region, or splices the new text into the wrong neighborhood, the tool has accepted a stale hunk.
**Require context, not only line numbers**
1) Record the hash of the file, the context lines the patch claimed, and the text actually replaced. A success bit is not enough. A line-number-only edit should be treated as a weaker contract than a context hunk, and should be tested as such.
2) Include a case in which the intended target text still appears twice after a drift, and a case in which the context matches but the line numbers do not. The tool should have a documented rule for each case, and the test should show that rule, not a silent “first match wins.”
3) Add a control in which the file is unchanged and the patch is applied twice. The second apply should be a documented no-op or a conflict, not a duplicated insertion. That is a useful related check, but it is not a substitute for the stale-context fixture.
There are limits. Some editors intentionally apply fuzzy matches, and some formats cannot carry long context. Fuzzy application can be acceptable if it is named, logged, and covered by tests that show when it must still refuse. The fixtures here do not determine a workspace-wide locking policy. They ask whether a single patch tool can be shown to notice that the file is no longer the one the hunk was written against.
What context would you require before a file-edit tool may apply a hunk? Which drift—moved target, edited neighbors, or a duplicated match—has caused a “successful” apply to change the wrong text?
An agent can emit a patch or a line-number edit that no longer describes the file on disk. The surrounding context may have been edited, the line numbers may now point at different text, or a previous step may already have applied part of the change. If the tool applies the hunk whenever it parses, or matches only a fragment of the context, the write can succeed and leave the file in a state no current caller intended.
This is not a lost update in which a human and an agent write the same record without preconditions, and it is not a hidden ordering dependency among parallel tool calls in one turn. It is also not a lock or lease among several agents that share a workspace, and it is not a retry that repeats a write because no idempotency key was supplied. The defect is a patch whose context is stale relative to the file it is applied to.
**Apply the same hunk to a current file and to a drifted file**
Create a small text file with a unique target region and distinctive context lines above and below it. Issue a patch that correctly replaces the target when applied to that exact file. Then produce a drifted copy in which only the context lines have changed, or in which the target has moved, and apply the original hunk without regeneration.
The current-file path should succeed and yield the intended text. The drifted path should be rejected, or should apply only after an explicit conflict result that the caller can see. If the drifted apply succeeds and quietly edits the wrong region, or splices the new text into the wrong neighborhood, the tool has accepted a stale hunk.
**Require context, not only line numbers**
1) Record the hash of the file, the context lines the patch claimed, and the text actually replaced. A success bit is not enough. A line-number-only edit should be treated as a weaker contract than a context hunk, and should be tested as such.
2) Include a case in which the intended target text still appears twice after a drift, and a case in which the context matches but the line numbers do not. The tool should have a documented rule for each case, and the test should show that rule, not a silent “first match wins.”
3) Add a control in which the file is unchanged and the patch is applied twice. The second apply should be a documented no-op or a conflict, not a duplicated insertion. That is a useful related check, but it is not a substitute for the stale-context fixture.
There are limits. Some editors intentionally apply fuzzy matches, and some formats cannot carry long context. Fuzzy application can be acceptable if it is named, logged, and covered by tests that show when it must still refuse. The fixtures here do not determine a workspace-wide locking policy. They ask whether a single patch tool can be shown to notice that the file is no longer the one the hunk was written against.
What context would you require before a file-edit tool may apply a hunk? Which drift—moved target, edited neighbors, or a duplicated match—has caused a “successful” apply to change the wrong text?

