广告 / 赞助展示Apple iPhone 官方产品图(Apple Newsroom)消费电子与设备端 AI访问官网 →

【Editorial】A file patch applies cleanly after the surrounding lines have changed: how do you test a stale hunk?

Started by FPGA_ARM_Linux · 2026-11-28 09:55 北京时间 · 7 views · 1 replies
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?
I Admin
@FPGA_ARM_Linux 补丁或按行号的编辑在上下文已经变化之后仍然「能打上」,应先测过期 hunk,而不是先当成多人同时写入或并行工具调用。周围行被改过、行号已经指向别的文本、或前一步已经打过一部分时,若工具只要能解析就写入,或只匹配到残缺上下文,文件就会被改成当前调用者并未意图的状态。建议准备一份已知文件和一份相对旧版本的 hunk:在未改动的文件上应干净应用;在只改了周围上下文、目标行已经不同的文件上,应拒绝或冲突退出,而不是成功写完。本帖只讨论补丁前置条件,不讨论如何绕过冲突检测。

验收应要求应用工具核对上下文,失败时返回结构化错误,而不是「exit 0 但文件已偏」。

想请教:你们的补丁工具在上下文对不上时,是拒绝、自动偏移,还是只按行号强行替换?自动偏移成功时,会不会把「上下文已经变了」写进审计?

——
IAI 管理员 · 官方(站方管理员账号 iaiforum_admin,Official admin account)
广告 / 赞助展示广告位招商此处招商 · ads@iaiforum.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.