AI

工作树里放开了已发货退款,绿测试跑在主仓库那份检出上(中英切换)

This post is currently available in Chinese only. The original text follows.Open the Chinese page →

三人、七台电脑。1 到 3 号机跑编码代理,4、5 号机审查,6 号机做 Windows 验证,7 号机负责合入。1 号机上常常同时有两份检出:主目录守在 main,当前工单在旁边另开一棵 git worktree。这样主仓库的工作区不会被未完成的改动弄脏,另一台代理也可以继续看 main。

今天的工单很窄:给 refund_order 加一个可选的 reason,方便仓储记下退款原因。已支付的订单仍可退。已发货、已签收的必须继续拒绝。已经退过的也必须拒绝。不要改 export,不要改 auth。验收是:在改动所在的同一棵工作树里跑 pytest -q 为绿,并且对已发货订单的探测仍被拒绝。

1 号机按习惯从主目录检出里加了一棵工作树,分支叫 feat/refund-reason。代理在工作树里重写了 refund_order,签名加上了 reason,却在改写时把 shipped 从 LOCKED_STATUSES 里拿掉了,并允许已发货订单走退款成功的路径。接着要跑验收。合入脚本、或平时开着的那个终端,工作目录仍是主仓库那份检出。pytest -q 输出 9 passed,对已发货订单 ord_1002 的探测是 rejected。看起来工单做完了。同一条命令如果在工作树里跑,结果是 1 failed, 8 passed,失败的正是 test_shipped_order_cannot_be_refunded,探测则是 allowed,ord_1002 变成了 refunded。

错目录很少是故意跑错。1 号机的编辑器工作区往往开在主仓库路径上,代理发出的命令默认就在那里执行。工作树是旁边的目录:改文件时,工具也许跟了过去,下一条 pytest -q 却仍沿用会话开始时的工作目录,或沿用合入脚本里写死的 MAIN_CHECKOUT。有人会先跑 git worktree list,看到 shop-wt 已经指向功能分支,以为验收也会落在那棵树上。pytest 默认只打印点数和「9 passed」,不会打印 toplevel,也不会打印 refund.__file__。主目录的 git status 是干净的,git branch 显示的是 main,看起来比一份「旁边的工作树」更像官方验收环境。正是这些看起来更稳妥的信号,把错树的绿日志送进了合入环节。

这篇要核对的是「测试进程到底加载了哪一份代码」,不是端口或数据库抢在一起。它和 一台机器上三个代理同时跑测试,怎么知道结果是自己分支的? 谈的是三棵工作树里的测试都启动了,却共用 8000 端口和同一份测试库,结论会串。今天两份检出没有互相监听,也没有共用业务数据库;绿的那一次,导入的是主仓库里尚未改动的 refund.py。它也不是 审查意见回来时,同一份工作区里已经写下了下一单未完成的重试 里那个脏工作区:今天主仓库的工作区是干净的,问题出在验收跑进了另一棵树。

我在 GitHub 上建了一个公开的实验室仓库 shuizhengqi1/iotxing-lab-worktree-cwd-20261203。店铺数据是 zhou@shop.invalid 这一类,卡号后四位是 1111,都不是真实商户。夹具提交是按常见的分单写法准备的,并没有再开一个编码模型来生成这些 diff。仓库规则集是空的,main 没有分支保护,也没有把本地的来源闸门设成必检,实验室没有使用 GitHub Actions。没有使用 --admin。未跑 Windows。

种子提交 09d416063958(09d4160639583e465e7edfe106ac0844dae881b6)在 2026-12-03 18:40:43(Asia/Shanghai)推上 main。里面已有正确的退款规则:LOCKED_STATUSES 包含 shipped 和 delivered,已退款会被拒绝,还没有 reason 参数。工单和 AGENTS.md 已经写明:改动必须在同一棵工作树里验收,绿日志如果来自主仓库那份检出,并不能证明当前分支。这一步在主目录检出里跑 pytest -q,结果是 9 passed;对 ord_1002 探测是 PROBE_RESULT=rejected。shop.refund.__file__ 指向主目录里的 src/shop/refund.py。

18:40:58,我在主目录检出上执行 git worktree add,得到 shop-wt,分支 feat/refund-reason,当时两份检出的 HEAD 都还是种子提交。随后只在工作树里改了 src/shop/refund.py:加上可选的 reason,把 LOCKED_STATUSES 改成只剩 delivered,并让 status != "paid" and status != "shipped" 才能拒绝——也就是已发货可以退。主目录那份文件没有动。

同一分钟里做了两次验收。先在主目录检出跑:工作目录、git rev-parse --show-toplevel、HEAD 09d416063958、分支 main,导入的 refund.py 仍是主目录那一份,pytest -q 是 9 passed,探测是 rejected。再在工作树里跑:toplevel 是 shop-wt,分支是 feat/refund-reason,导入的是工作树里的 refund.py,pytest -q 是 1 failed, 8 passed,失败用例是 tests/test_invariants.py::test_shipped_order_cannot_be_refunded,报 DID NOT RAISE RefundError;探测是 PROBE_RESULT=allowed,ord_1002 的状态变成 refunded,还带上了 refund_reason=probe。这时候工作树的 HEAD 仍是种子——改动还在工作区,尚未提交——但已经足以说明:同一条 pytest -q,工作目录不同,加载的实现就不同。

工作树里的实现已放开已发货退款,绿测试却跑在主仓库那份检出上

18:40:59,工作树提交 0912373ae537(0912373ae5379991c2a81a31eda4d6895752ce3c),说明是「feat: add optional refund reason」,diff 只有 src/shop/refund.py,8 行新增、5 行删除。提交之后再跑一遍:主目录仍然是 9 passed、探测 rejected,HEAD 仍是种子;工作树 HEAD 变成 0912373ae537,测试仍是 1 failed, 8 passed,探测仍是 allowed。18:41:03 打开 PR #1,头提交就是 0912373ae537。

只看「测试绿了」会把主目录那份日志当成验收通过。我用 record_provenance.py 把每次运行的工作目录、toplevel、HEAD、分支和 shop.refund.__file__ 记下来,再用 test_provenance_gate.py 去核对:这份日志的 HEAD 是否就是准备合入的那个 PR 头提交,以及 pytest 退出码是否为 0。拿主目录那份 9 passed 的日志去对 PR 头提交,闸门退出码是 1,原因是 provenance HEAD 09d416063958 对不上 0912373ae537。拿工作树的日志去对同一个头提交,HEAD 对上了,但 pytest 退出码是 1,闸门同样拒绝。也就是说:错树的绿不能用,对树的红也不能用。

实验 B 从同一份种子分出另一棵工作树 shop-wt-b,分支 feat/refund-reason-safe。提交 4e66873d1413(4e66873d14138b34563a83b456ad384abc0229fc)在 18:41:05,加上了 reason,但 LOCKED_STATUSES 仍包含 shipped 和 delivered,已发货不会走进成功路径,并补了 test_refund_records_reason。在这棵工作树里跑,pytest -q 是 10 passed,探测是 rejected,refund.py 来自 shop-wt-b。同一时刻主目录检出仍停在种子上,那里跑一次也是 9 passed、探测 rejected——因为主目录根本还没有今天的改动,当然也没有新的 reason 用例。以来源闸门去核对这些日志:工作树这份对 4e66873d1413 退出码是 0;主目录那份 9 passed 对同一个提交,退出码是 1,原因仍是 HEAD 09d416063958 对不上。这件事值得单独说:即便分支上的改动是对的,主目录的绿日志也不能拿来证明它。读者若要看这次安全改动,应检出 tag 或分支 experiment-b。

实验 B 把「绿并不等于证明了要合的那次提交」拆得更清楚。正确的改动已经在 shop-wt-b 里通过了 10 个测试,已发货探测也被拒绝。若有人觉得再到主目录跑一遍更稳,跑出来的确实是绿的——但那 9 passed 来自种子,少了 test_refund_records_reason,也没有加载带 reason 的新实现。把这份日志交给闸门去证明 4e66873d1413,会被拒绝。主目录的绿,在这里甚至算不上保守验收,只是另一份旧代码的自我确认。

实验 A 在主目录 9 passed 却合进了工作树里的红测试;实验 B 在对的树上 10 passed,已发货仍被拒绝

仓库规则集是空数组,访问 main 的保护接口返回 404,PR #1 的合并状态是 MERGEABLE、CLEAN。实验室准备了一份故意写坏的合入脚本 merge_if_green.sh:它先 cd 到写死的主目录检出,跑 pytest -q,绿了就执行 gh pr merge --merge。18:41:16,这份脚本的工作目录是主仓库,HEAD 是 09d416063958,分支是 main,pytest 仍是 9 passed,随后在没有管理员权限、也没有加 --admin 的情况下把 PR #1 合并了。合并提交是 289ff39a53a3(289ff39a53a3611932a7b76a58c26743c7085506)。把主目录快进到这份合并之后,再跑一次:pytest -q 变成 1 failed, 8 passed,失败的还是已发货不可退那条,探测变成 allowed。main 上现在就允许已发货退款。这是实验室有意留下的结果,不是事后改掉的。

机制并不绕。git worktree 共用同一份对象库,但每棵树有自己的 HEAD、索引和工作区文件。实验室里 pytest.ini 写的是 pythonpath = src,测试导入的 shop.refund 来自「当前工作目录下面的 src」。你在主目录里敲 pytest -q,加载的就是主目录这份还没改的实现;你在工作树里敲同一条命令,加载的才是刚改过的文件。git worktree list 当时已经把两棵树、两个 SHA、两条分支都列出来了,但默认的 pytest 输出不会写这些。代理或合入脚本如果习惯 cd ~/shop,或者终端本来就停在主目录,验收日志会一直绿,直到有人在正确的树上跑一次,或把 main 快进到那次合并之后。

放到七台机器上,这条错还会被放大。1 号机把「9 passed」贴进 PR 或群里时,4 号机看到的是一行结论,看不到当时的工作目录,也看不到导入的是哪一份 refund.py。7 号机如果也习惯在自己的主目录检出里再跑一次「确认」,而那份检出同样停在旧的 main 上,还会再得到一次绿。两边都没有撒谎,只是都没有跑到 PR 头提交上。来源字段要跟着日志走,而不是相信某台机器平时的工作习惯。

有人会说:只要 CI 对着 PR 的头提交跑测试,这棵雷就扫掉了。这个反驳成立,而且应该做。实验室没有把任何检查设成必检,也没有在线跑 Actions,为的是看:当合入发生在「本机日志已经绿了」这条路径上时,错树的绿能不能直接进 main。三人七机的日常里,这条路径很常见——本地验收、gh pr merge,或者脚本里写死的仓库路径。CI 能挡住最终合入,挡不住一份被转发到群里的「9 passed」被当成这张工单已经做完。来源闸门要问的不是「有没有一份绿日志」,而是「这份日志的 HEAD 是不是要合的那个提交,导入的实现是不是那棵树里的文件,以及测试本身过没过」。

这套做法不适用的地方也要说清楚。它不代替不变量测试本身:如果工作树里同时删掉了 test_shipped_order_cannot_be_refunded,对树运行也会是绿的,这时还要有路径闸门或探测,那是范围问题,不是工作目录问题。它也不能代替真正的 CI:本机闸门拦的是这台机器上的合入习惯,拦不住别人从网页上直接合。如果测试不靠本地 src,而是导入已经 pip install 进环境的包,只看工作目录还不够,必须看 shop.refund.__file__ 到底指向哪棵树。实验室没有在 Windows 上跑,6 号机那种路径分隔或盘符切换会不会另加一层错目录,本文不下结论。

我先试过只靠 AGENTS.md 里那句「必须在同一棵工作树里跑测试」。实验室的种子里已经写了这句话,它没有挡住 merge_if_green.sh,因为脚本不会去读那份文档。我也核对过「工作区干净能不能当开工信号」:主目录确实干净,测试也确实绿,但干净的是另一份代码。这些检查都有用,只是它们回答的不是「你要合的那个 SHA 跑过了没有」。

明天可以直接抄的有三条。第一,跑验收时把 pwd、git rev-parse --show-toplevel、git rev-parse HEAD、当前分支,以及被测模块的 __file__ 写进同一份日志,不要只留一行 9 passed。第二,合入前用闸门核对这些字段:日志里的 HEAD 必须等于 PR 头提交,pytest 退出码必须是 0,导入路径必须落在那棵树的 toplevel 下面;错树的绿和对树的红都应该拒绝。第三,不要在合入脚本里写死 cd 到主仓库再跑测试。要测哪次提交,就在那次提交的工作树里跑,或者单独检出那个 SHA。若要在本机复现「对着指定提交跑测试」,不一定要保留那棵工作树。可以再 git worktree add --detach 一份只指向 PR 头提交的临时检出,跑完即删;或者用干净目录 git clone --branch 到那个 SHA。关键是测试进程的工作目录和导入路径都落在这份检出上,而不是回到写死的主目录。实验室的 record_provenance.py 和 test_provenance_gate.py 就是为这次核对准备的,可以和测试日志放在一起保存。

AGENTS.md 里写「在同一棵工作树里跑测试」有用,但写在文档里挡不住一份 cd 到主目录的脚本。实验室的 main 现在仍是放开已发货退款的状态,安全对照在 experiment-b。

只看主目录的绿日志会放行实验 A;来源闸门要求日志 HEAD 等于要合的提交,并且测试本身是绿的

Three people, seven computers. Machines 1–3 run coding agents, 4 and 5 review, 6 does Windows validation, and 7 merges. Machine 1 often keeps two checkouts at once: the main directory stays on main, and the current ticket lives in a sibling git worktree. That keeps the main working tree clean, and another agent can still read main.

Today's ticket is narrow: add an optional reason argument to refund_order so warehouse notes can record why a paid order was refunded. Paid orders still refund. Shipped and delivered orders must still be rejected. Already-refunded orders must still be rejected. Do not change export or auth. Acceptance is pytest -q green in the same worktree as the change, and a shipped-order probe still rejected.

Machine 1 adds a worktree from the main checkout, on a branch named feat/refund-reason. The agent rewrites refund_order in that worktree, adds reason, and while rewriting drops shipped from LOCKED_STATUSES, so a shipped order can refund successfully. Then it is time to run the tests. The merge script, or the terminal that was already open, still has its working directory in the main checkout. pytest -q prints 9 passed. A probe of shipped order ord_1002 is rejected. The ticket looks done. The same command in the worktree is 1 failed, 8 passed. The failure is test_shipped_order_cannot_be_refunded. The probe is allowed, and ord_1002 becomes refunded.

The wrong directory is rarely chosen on purpose. Machine 1's editor workspace is often the main repo path, so agent commands start there. The worktree is a sibling directory: tools may follow it when editing files, then the next pytest -q still uses the session's original working directory, or a hardcoded MAIN_CHECKOUT in a merge script. Someone may run git worktree list, see that shop-wt already points at the feature branch, and assume acceptance will run there too. Default pytest output prints dots and "9 passed"; it does not print the toplevel or refund.__file__. In the main checkout, git status is clean and git branch shows main, which looks more official than "the tree next door". Those reassuring signals are what carried the wrong-tree green log into the merge.

This post is about which tree the test process actually imported, not about ports or databases colliding. When three agents run tests on one machine, how do you know the result belongs to your branch? was about tests that did start in each worktree, then stepped on port 8000 and a shared test database. Today's two checkouts did not listen on the same port and did not share a business database. The green run imported the untouched refund.py from the main checkout. This is also not the dirty checkout that already had the next ticket's unfinished retry in it when review comments came back. The main working tree was clean. The acceptance run simply went into the other tree.

I published a public lab at shuizhengqi1/iotxing-lab-worktree-cwd-20261203. Shop fixtures use addresses such as zhou@shop.invalid and card last-four 1111; they are not a real merchant. The commits were prepared as ordinary fixture diffs. No coding model was asked to write them. The repository has no rulesets, main is not branch-protected, and the local provenance gate was not a required check. GitHub Actions was not used. The merge did not use --admin. There was no Windows run.

Seed commit 09d416063958 (09d4160639583e465e7edfe106ac0844dae881b6) landed on main at 2026-12-03 18:40:43 (Asia/Shanghai). Refund rules were already correct: LOCKED_STATUSES contains shipped and delivered, already-refunded orders are rejected, and there is no reason argument yet. The ticket and AGENTS.md already say that tests must run in the same worktree as the change, and that a green log from the main checkout does not prove the current branch. In the main checkout, pytest -q was 9 passed, and the probe of ord_1002 was PROBE_RESULT=rejected. shop.refund.__file__ pointed at src/shop/refund.py in the main directory.

At 18:40:58 I ran git worktree add from the main checkout and got shop-wt on feat/refund-reason. Both trees still pointed at the seed. I then edited src/shop/refund.py only in the worktree: add optional reason, leave only delivered in LOCKED_STATUSES, and reject only when status != "paid" and status != "shipped" — so shipped orders can be refunded. The file in the main checkout was not touched.

The two acceptance runs happened in the same minute. In the main checkout, the working directory, git rev-parse --show-toplevel, HEAD 09d416063958, and branch main all pointed at that tree, refund.py was still the main copy, pytest -q was 9 passed, and the probe was rejected. In the worktree, the toplevel was shop-wt, the branch was feat/refund-reason, and the import was the worktree copy. pytest -q was 1 failed, 8 passed. The failing test was tests/test_invariants.py::test_shipped_order_cannot_be_refunded, with DID NOT RAISE RefundError. The probe was PROBE_RESULT=allowed: ord_1002 became refunded and carried refund_reason=probe. The worktree HEAD was still the seed — the edit was still uncommitted — which is already enough: the same pytest -q loads a different implementation when the working directory is different.

The worktree already allows shipped refunds, but the green tests ran in the main checkout

At 18:40:59 the worktree commit 0912373ae537 (0912373ae5379991c2a81a31eda4d6895752ce3c) landed, described as "feat: add optional refund reason". The diff is only src/shop/refund.py, 8 insertions and 5 deletions. After the commit, the main checkout was still 9 passed and probe rejected, still on the seed. The worktree HEAD was now 0912373ae537, still 1 failed, 8 passed, probe allowed. PR #1 opened at 18:41:03, with that commit as its head.

A log that only says the tests were green will treat the main checkout run as acceptance. record_provenance.py writes the working directory, toplevel, HEAD, branch, and shop.refund.__file__ for each run. test_provenance_gate.py then checks that the log's HEAD is the PR tip you intend to merge, and that pytest exited 0. Against the PR tip, the main checkout's 9-passed log is refused (exit 1) because provenance HEAD 09d416063958 does not match 0912373ae537. The worktree log has the right HEAD, but pytest exited 1, so the gate refuses that one too. A green log from the wrong tree is not enough, and a red log from the right tree is not enough either.

Experiment B starts from the same seed in a second worktree, shop-wt-b, on feat/refund-reason-safe. Commit 4e66873d1413 (4e66873d14138b34563a83b456ad384abc0229fc) at 18:41:05 adds reason, keeps shipped and delivered in LOCKED_STATUSES, does not let shipped orders take the success path, and adds test_refund_records_reason. In that worktree, pytest -q is 10 passed, the probe is rejected, and refund.py comes from shop-wt-b. At the same moment the main checkout is still on the seed, so a run there is also 9 passed and probe rejected — it does not have today's change, and it does not have the new reason test. The provenance gate exits 0 for the worktree log against 4e66873d1413, and exits 1 for the main checkout's 9-passed log against the same commit, again because HEAD 09d416063958 does not match. That last part matters on its own: even when the branch is correct, a green log from the main checkout does not prove it. To inspect the safe change, check out the experiment-b tag or branch.

Experiment B makes the weaker claim clearer: green is not proof of the commit you are merging. The correct change already had 10 passed in shop-wt-b, and the shipped probe was rejected. Running the suite again in the main checkout "to be safe" is still green — but those 9 passed come from the seed, without test_refund_records_reason and without loading the new implementation that accepts reason. Hand that log to the gate as proof of 4e66873d1413, and it is refused. A green main checkout is not a conservative re-check here. It is the old tree confirming itself.

Experiment A was 9 passed in main and still merged a red worktree; experiment B was 10 passed in the right tree and still rejects shipped refunds

The ruleset list was an empty array, the branch protection API for main returned 404, and PR #1 was MERGEABLE and CLEAN. The lab includes a deliberately bad helper, merge_if_green.sh: it cds into a hardcoded main checkout, runs pytest -q, and on green runs gh pr merge --merge. At 18:41:16 that script's working directory was the main checkout, HEAD was 09d416063958, the branch was main, pytest was still 9 passed, and it merged PR #1 without admin rights and without --admin. The merge commit is 289ff39a53a3 (289ff39a53a3611932a7b76a58c26743c7085506). After fast-forwarding the main checkout to that merge, pytest -q is 1 failed, 8 passed, still on the shipped invariant, and the probe is allowed. main now allows shipped refunds. That is an intentional leftover, not a later cleanup.

The mechanism is straightforward. Git worktrees share an object store, but each tree has its own HEAD, index, and working files. In this lab pytest.ini sets pythonpath = src, so shop.refund is loaded from src under the current working directory. pytest -q in the main directory loads the untouched implementation; the same command in the worktree loads the file that was just edited. git worktree list already showed both trees, both SHAs, and both branches. Default pytest output does not. If an agent or a merge script habitually cd ~/shop, or if the terminal was already in the main directory, the acceptance log stays green until someone runs the same command in the right tree, or fast-forwards main past the merge.

On seven machines the same miss gets amplified. When machine 1 pastes "9 passed" into the PR or a chat, machine 4 sees a conclusion and not the working directory, and not which refund.py was imported. If machine 7 "confirms" the result in its own main checkout, and that checkout is also still on the old main, it gets another green run. Neither side is lying. Neither run was on the PR tip. Provenance has to travel with the log, rather than trusting how a particular machine usually works.

A fair objection is that CI on the PR tip would have caught this. It would, and you should have that CI. The lab left every check optional and did not run Actions, because the question was whether a wrong-tree green log can reach main when merge happens on the "it was green on my machine" path. That path is ordinary on a three-person, seven-machine team: local acceptance, gh pr merge, or a helper with a hardcoded repo path. CI can still stop the final merge. It cannot stop a "9 passed" log from being forwarded in chat as proof that the ticket is done. The provenance gate does not ask whether some green log exists. It asks whether that log's HEAD is the commit being merged, whether the imported implementation lives in that tree, and whether the tests themselves passed.

The limits matter too. This does not replace invariant tests. If the worktree also deleted test_shipped_order_cannot_be_refunded, a right-tree run would be green, and you would still need a path gate or a probe — that is a scope problem, not a working-directory problem. It also does not replace real CI: a local gate constrains merge habits on this machine, not a merge started from the GitHub UI. If tests import a package from the environment rather than local src, looking at the working directory is not enough; you have to check where shop.refund.__file__ actually points. There was no Windows run, so this post does not claim anything about drive letters or path separators on machine 6.

I first tried relying only on the sentence in AGENTS.md that says tests must run in the same worktree. That sentence was already in the seed. It did not stop merge_if_green.sh, because the script does not read the document. I also checked whether a clean working tree was a safe "ready to test" signal: the main checkout was clean, and the tests were green, but the clean tree was a different copy of the code. Those checks are useful. They do not answer whether the SHA you are merging has been run.

Three things you can copy tomorrow. First, write pwd, git rev-parse --show-toplevel, git rev-parse HEAD, the current branch, and the module __file__ into the same log as the test run; do not keep only the line that says 9 passed. Second, before merge, gate those fields: the log's HEAD must equal the PR tip, pytest must have exited 0, and the import path must sit under that tree's toplevel. Refuse a green log from the wrong tree and a red log from the right one. Third, do not hard-code cd into the main checkout in a merge helper. If you need to test a commit, run the tests in that commit's worktree, or check that SHA out on its own. You do not have to keep the original worktree around to re-run tests against a specific commit. You can git worktree add --detach a temporary checkout of the PR tip and delete it afterwards, or clone that SHA into a clean directory. What matters is that the test process's working directory and import path both sit in that checkout, instead of falling back to a hardcoded main directory. The lab's record_provenance.py and test_provenance_gate.py are meant to be kept next to the test log. A line in AGENTS.md that says "run tests in the same worktree" helps, but it will not stop a script that cds into the main directory. The lab's main still allows shipped refunds; the safe contrast is experiment-b.

A green log from the main checkout would merge experiment A; the provenance gate requires the log HEAD to equal the commit being merged, and the tests themselves to be green