proc.Look keeps "not found" for a tool installed in the same clock tick as its folder's last change
我这台 Linux(ext4,5.15 内核)上跑 internal/agent,TestHowInstalled 经常红(cliupdate_test.go:192: pnpm: <nil>),在 c922225f 上单独跑 5 次红 3 次。TestInstalledVersion、TestOmpDetectedWithPiDir、TestCursorFolderFromTheEditorIsNotTheCLI 也一样,分别是 5/5、5/5、4/5。我之前在 #2932、#2942 的记录里把它们算成了我这台机器 PATH 的问题,其实不是。CI 上是绿的,我不清楚 CI 机器的时钟粒度,可能刚好没撞上。
原因在 proc.Look、FindTool 和 listed() 的缓存:它们靠 PATH 文件夹的修改时间判断有没有变,但这个时间是粗粒度时钟打的。我这台 ext4 上紧跟着建文件,200 次里有 197 次文件夹的时间不动。所以先查一次 pnpm(没有),马上往同一个文件夹写一个 pnpm 再查,第二次拿到的还是缓存里的"没有"。真实使用里就是安装器在同一个 tick 里往 PATH 文件夹写东西,这个工具会一直找不到,直到这个文件夹再变一次,或者点 Look again。
TestLookPathSeesAnInstallAndARemoval 里那两个 time.Sleep(20ms)("a folder time a coarse clock tells apart")其实就是在绕开这个。
补丁在 fix/look-racy-stamp,一个 commit f9d7eac9,基于 c922225f,现在的 main ba319a75 上也能直接打:
git format-patch --stdout c922225f..f9d7eac9 | sha256sum
0c83cfc1d7f057f9b75f08b99e3f0599a2fdabc16847a56d9024707a060c29f4
改法:一个文件夹在 100ms 内刚改过,就不信它的时间戳,查到的结果也不留,下次再查。和 git 的 racy entry 是一个思路,magpie 里 filememo 的 settled、Codex 列表的 codexListSettled 也是这么做的。时间比现在还晚的文件夹算稳定,不然缓存会一直关着。
- 新加的
TestLookPathSeesAnInstallInTheSameTick在旧代码上红,只改 stamp 或只改 listing 也红,两边都改才绿。 - 上面四个 agent 测试不用改,
-count=5全绿;TestHowInstalled 改前 10 次红 9 次,改后 0 次。 TestLookPathSeesAnInstallAndARemoval去掉那两个 sleep 以后,在旧代码上 10 次红 10 次,改后绿。go test -race ./internal/proc/绿;完整套件除了我这台机器在干净 main 上本来就红的 8 个,没有新的红。详细记录在 commit body 里。
100ms 而不是 2s:页面一次请求要查一百来个命令,#1624 那次 Windows 加杀毒软件已经慢到 5–8s,窗口越长,刚改过的文件夹被重复读的时间越长。取舍写在 commit body 里了,你觉得 2s 更统一的话改个常量就行。
没试过的:Windows 和 macOS 只跑了 vet,没真机跑;也没拿真实安装器去跟 magpie 抢。
另外两个看到但没动的:TestCodexDriftReached 也经常红(c922225f 上 40 次红 20 次),但它读的是 Codex 的 logs_2.sqlite,不是 PATH,跟这个不是一回事。filememo 的 settled 会把时间比现在还晚的文件一直当成没稳定、每次都重读,只影响性能。
Implemented on my fork, adapting your f9d7eac9 patch with Co-authored-by retained. Both cache layers now avoid keeping entries for recently changed directories: lookup answers and directory name listings. Stable and future-dated directories still reuse their caches; the normal two-second stat window and explicit Look again refresh remain.
I also froze the clock in TestLookPathSeesAnInstallInTheSameTick, so a loaded runner cannot age its directory out of the 100ms window while the test is running. It uses real executable files in an isolated temporary PATH. The regression fails with the original implementation, with only the stamp fixed, and with only the listing fixed; all three failures are the installed executable still being reported missing. Restored, the lookup/listing regressions and the four affected agent tests pass five race runs.
The 100ms tradeoff is kept and documented in code and the subsystem reference: filesystems with timestamp resolution greater than 100ms need a longer settling window. No real installer/account or production configuration was used; Windows/macOS runtime behavior was not tested.
Fork commit: 89bceb39, branch fix/path-same-tick-install. Go 1.26.8 native/Linux nogui/Windows vet and builds, plus go test -tags nogui ./... -count=1 -timeout=45m, all pass at this exact commit (seven checks; 69 packages pass, 19 have no tests). No upstream PR opened.
@skygunner thanks, freezing the clock is right. On a busy runner the 100ms can pass between dating the folder and the second lookup, and then the test goes red on the fixed code too. I missed that. Your branch looks good to me.
One more test this covers: TestZedAppPath. zedAppPath finds the zed CLI through proc.Look, and the test symlinks zed into PATH right after a lookup there. On b437e4ad it's red 7 of 10 alone (zed_credential_other_test.go:81: CLI's bundle: ".../Zed Preview.app", <nil>), and with f9d7eac9 on top it passed 40 of 40. TestHowInstalled is still red on b437e4ad too, 3 of 10.
Two more this covers: TestCopilotFolderFromAnIDEPluginIsNotTheCLI and TestQoderSettingsFromAnIDEPluginIsNotTheCLI. Each looks its CLI up on an empty PATH, writes it there, and looks again. On 9fc382e6, alone at -count=10, Copilot is red 7 of 10 (ide_plugin_dir_test.go:36: the copilot command on PATH no longer finds the Copilot CLI) and Qoder 9 of 10 (:86: qodercli on PATH no longer finds Qoder, and the same for qoderclicn). With the internal/proc change from f9d7eac9, or from skygunner's 89bceb39, both pass 10 of 10.
skygunner's 89bceb39, rebased: it no longer applied on main because of one line in docs/subsystems/agent-wiring.md. Main has since changed the line his was added after, so I kept main's wording there and put his line after it, word for word. Nothing else changed: author, message and code are his, and the four proc files he touched are byte for byte the same as at 89bceb39, since main hasn't touched them. Main has changed other files in internal/proc since then, and I haven't run the rebased commit; the runs in his comment above were at 89bceb39.
Branch: https://github.com/JoeyWu-tech/magpie/tree/fix/3141-sky-rebased, commit 5dea2647 on top of main 3142a555, git format-patch --no-signature --full-index --stdout 3142a555..5dea2647 | sha256sum is e1a60f9719001b8bdcd7589a5b7b1082e4374a90991c4f631a0b80a32cfb6a58. It's a single commit on top of main now, so it can be taken as is.
skygunner's 89bceb39 rebased again: 5dea2647 no longer applies on main, again only because of one spot in docs/subsystems/agent-wiring.md. Since 3142a555 main changed the lines around his: 92f02d677 extended the "An agent is detected by its config file…" line his goes after, 0cabaaf24 (#4151) added the VS Code line above it, and 606743831 added one below. I kept main's lines and put his right after "An agent is detected…", word for word, as before. Nothing else changed: author, message and code are his, and the four proc files he touched are byte for byte the same as at 89bceb39, since main still hasn't touched them. Same as 5dea2647, I haven't run this commit; the runs in his comment above were at 89bceb39.
Branch: https://github.com/yinwu01/magpie/tree/fix/3141-sky-rebased-2, commit 5a171bc3 on top of main 889767b7, git format-patch --no-signature --full-index --stdout 889767b7..5a171bc3 | sha256sum is 945c63d4b284e215e6d29e0596e6e467af3c85e533d8f73a44f85b4e0a544788. (My GitHub name changed from JoeyWu-tech to yinwu01; the fork moved with it.)
Taken onto main as @skygunner's 89bceb39 (your rebase 5a171bc3 applied, authors kept): 62f612fd4, in v0.1.2855. TestLookPathSeesAnInstallInTheSameTick is red on the old internal/proc and green with it; TestZedAppPath, TestCopilotFolderFromAnIDEPluginIsNotTheCLI and TestQoderSettingsFromAnIDEPluginIsNotTheCLI pass with it (they were the #4033 reds on Linux, below). Leaving the issue open for a run on your side.
Ran it on my side at main 43849d51 (Linux, go1.26.8, outside any proxy): internal/proc's Look tests 10 times under -race, and the agent tests that were red here before (TestInstalledVersion, TestHowInstalled, the Cursor, Copilot, Qoder and Omp folder tests, TestZedAppPath) 5 times each. All green. Thanks @skygunner for the fix, closing.