Repository navigation
test: wait for the tower lock holder instead of polling the lock file - #5245
Conversation
The stale-release case polled the state lock file and read it as soon as
it existed. tryAcquireStateLock creates the file with open('wx') and only
then writes the holder line, so under load the read could land between
the two steps and see an empty file.
Each holder now signals when it enters its critical section, and the test
reads the lock only after that signal. The test also holds the new
holder's create-to-write window open with a writeFile spy and runs one
stale release inside it, so the empty-content case is covered on every
run. A second stale holder releases after the content is written, which
keeps the case the test covered before.
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
Rewrite the stale-release case to signal holder entry and hold the create-to-write window with a writeFile spy, so a stale release during lock creation observes empty content deterministically and the post-write case keeps the covered content (MoonshotAI#5245). jest.spyOn replaces vitest's vi and FileHandle is imported. Ledger: 5038 not-applicable (its case is absent from the fork's tower suite) and 5080 equivalent.
Requirement or Bug
跑 agent-core-v2 全量测试时,
packages/agent-core-v2/test/features/tower/store.test.ts里的concurrent state writes > a stale release cannot delete another holder's state lock偶发失败,报expected '' to match /pid=\d+ since=\S+ token=[0-9a-f-]+/;单独跑这个文件时稳定通过。这个 PR 修掉用例的同步方式,让它每次都确定性地通过,并且每次运行都覆盖锁文件「已创建、内容还没写」的那一刻。没有对应的 issue。Bug Reproduction Steps
不施加 CPU 压力时很难碰到,下面是确定性复现的办法(不依赖 sleep、重试或超时):
FileHandle.prototype.writeFile的 spy 把新持有者写入锁内容(pid=… since=… token=…)的那一步挂起。这时open(lockPath, 'wx')已经建好了锁文件,但文件是空的。''。Root Cause
TowerStore.tryAcquireStateLock分两步拿锁:先open(lockPath, 'wx')建出空文件,再handle.writeFile写入持有者信息,两步之间至少隔一次 libuv 线程池往返。原用例每 10ms 轮询一次,readFile一成功就退出,把「锁文件存在」当成了「second 已经持有锁」。全量运行时 CPU 争用激烈,readFile的 open、fstat、read 可能正好落在这两步之间,读到空字符串。sequenceDiagram participant T as 用例 participant S as second(新持有者) participant F as 锁文件 S->>F: open(lockPath, 'wx'),文件存在但内容为空 T->>F: readFile 成功,读到 '' Note over T: 退出轮询,断言失败 S->>F: writeFile("pid=… since=… token=…") S->>S: 进入临界区这是用例同步方式的问题,这次是根本修复:锁真正拿到的信号是持有者进入临界区,而不是锁文件出现。用例改成等这个信号。
生产代码在空内容窗口里的行为(已实测,本 PR 不改)
releaseStateLock(过期的 release)reapStaleStateLockwithProtocolLock超时报错held by后面为空,并说成另一个 tower 进程另外,如果进程恰好死在
open和writeFile之间,会留下一个空锁文件,且不会被自动回收。之后的调用会明确报超时错误,需要手动删锁,不会悄悄失败。这种情况概率极低,和这次的偶发失败无关,所以没有放进本 PR。Code Changes
只改了一个用例。改动后的流程:
每个持锁方都在临界区里发
entered信号,用例等到信号才读锁文件,原来的setTimeout轮询删掉了。spy 让每次运行都把「锁文件已建、内容未写」的窗口撑开,并在窗口里做一次过期 release,所以旧的同步方式在这个用例下每次都会失败,这个用例本身就是回归测试。staleAfterWrite在内容写完后再做一次过期 release,保住了原用例覆盖的情形。用FileHandle.prototype.writeFilespy 的写法沿用了同文件里 inbox 原子发布那个用例。link让锁内容原子写入wx时窗口依然存在,而且改的是并发锁本身Behavior Changes and Affected Users
None。只改了
packages/agent-core-v2/test/features/tower/store.test.ts里的这一个用例,没有改生产代码、默认值或契约文件。受影响模块:tower 状态锁的测试。覆盖情况:过期 release 在两种时机下都不能删除别人的锁,一次在新持有者建锁和写内容之间,一次在内容写完之后。
验证结论:本机 macOS 上,这个用例单独运行 20 次全部通过。临时把
releaseStateLock改坏成三种写法(读到空内容就删、不比对 token、只在内容非空时删),这个用例都会失败。整个测试文件通过,lint 和类型检查都是 0 退出。Linux 由 CI 验证,Windows 没有验证。实际运行的命令和结果
pnpm exec vitest run --reporter=agent packages/agent-core-v2/test/features/tower/store.test.ts -t "a stale release cannot delete":连续 20 次,20 次通过。releaseStateLock改坏写法,各跑一次,都失败,跑完已还原源码。pnpm exec vitest run --reporter=agent packages/agent-core-v2/test/features/tower/store.test.ts:229/229 通过。pnpm lint:退出码 0。pnpm exec tsc -p packages/agent-core-v2/tsconfig.json --noEmit:退出码 0,这个 tsconfig 包含test目录。pnpm run check:duplication <base-sha>:没有新增重复。pnpm exec vitest run --reporter=agent packages/agent-core-v2:store.test.ts全部通过。test/tool/tool.test.ts里有 3 个用例 30s 超时(treats timeoutMs=0 as no deadline for background subagents and preserves the metadata、runs foreground subagents without a deadline under print mode defaults、runs background subagents without a deadline under print mode defaults),单独跑这个文件也每次失败。本 PR 相对 main 只改了store.test.ts,所以这 3 个失败与本 PR 无关。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.