Skip to content

fix(agent-core-v2): check the Tower lock holder before taking the git reaper guard - #5479

Merged
RealKai42 merged 1 commit into
mainfrom
fix/tower-lock-reaper
Nov 26, 2026
Merged

RealKai42 merged 1 commit into
mainfrom
fix/tower-lock-reaper

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Tower 的多个 agent 同时发消息、注册或更新任务时,在负载较高的机器上,工具调用会报 timed out after 10000ms waiting for the tower state lock 或 ... inbox publication lock。agent-core-v2 全量测试里 features/tower/store.test.ts 的三条并发用例也因此偶发失败。这个 PR 之后,持锁者还活着时,等锁的一方不再起 git 进程,同样的负载下不再超时。

Bug Reproduction Steps

  1. 同时运行 6 份 pnpm exec vitest run packages/agent-core-v2/test/features/tower/store.test.ts -t "sends without waiting for the state lock and preserves",另开 6 个吃满 CPU 的进程。
  2. 12 次全部失败,报 TowerProtocolError: timed out after 10000ms waiting for the tower state lock 或 inbox publication lock。同样的用例空闲时 0.9 秒通过。

Root Cause

等锁的一方每 250ms 检查一次锁是否已被死掉的进程遗留(reapStaleStateLock)。这个检查先拿一个由 git ref 实现的回收守卫,过程中要起 rev-parse、hash-object -w、update-ref 几个 git 进程,释放时还要起一次 update-ref -d。拿到守卫之后,它才去读锁文件,用 process.kill(pid, 0) 判断持锁者是否已死。

持锁者活着时,这些 git 调用全部白费。等锁的人一多,起出来的 git 进程反过来把持锁者也拖慢了,形成自我放大。在上面那条用例里临时加计时:

空闲 负载下
回收检查次数 / 累计耗时 93 次 / 23.2 秒 180 次 / 174.7 秒
单次回收检查最长 381ms 1,558ms
持锁期间写一条消息最长 241ms 1,232ms
用例总耗时 1.7 秒 超过 10 秒的锁等待,失败

用例里的持锁者一直是活着的本进程,所以这些回收检查没有一次真正需要回收。这是根本修复:去掉了持锁者活着时的无用工作,没有调整超时。

Code Changes

改动 说明
拿守卫之前先查持锁者 reapStaleStateLock 先读锁文件、判断持锁者是否已死,活着就跳过这一轮,不碰 git。只有看起来已死,才走原来的守卫路径
守卫内的判定不变 原来守卫里「读锁文件、解析 pid、判断已死」写了两遍,提成 readDeadStateLockHolder,守卫外的预检和守卫内的两次确认共用。守卫内仍然要两次确认已死、两次读到的内容一致,才删锁

关键决策

决策 选择 没选 理由
怎么消除等锁时的开销 守卫外先做一次存活预检 调大锁超时或拉长检查间隔 调大超时只是把失败往后推,等锁时的 git 进程照样会拖慢持锁者。预检只读一个文件、做一次 kill(pid, 0),判定和守卫内完全相同
为什么预检不会误删锁

预检只会让回收检查提前返回 false,永远不会让它删掉什么:删锁仍然只发生在守卫内,条件和原来一样。

预检的判定和守卫内第一次确认相同,所以凡是预检跳过的轮次,在原来的代码里拿到守卫后也会返回 false。唯一的差别在时序:持锁者在预检之后、下一轮检查之前死掉的话,回收会晚一轮(250ms)。

反过来的情况也有覆盖:一个回收者通过了预检(锁看起来是死进程留下的),但在它拿到守卫之前,锁已被别人清掉、又被新的持锁者拿到。这时守卫内的复查读到的是新持锁者,所以返回 false。新增的一条用例固定了这个顺序:删掉守卫内的复查,它就会失败。

测试

用例 说明
waits for a live holder in %s without taking the reaper guard(新增,两种情况) 持锁者是本进程或另一个活着的进程时,等锁直到超时都不拿回收守卫。还原产品代码后,两种情况都失败
rechecks the holder under the guard when the lock is replaced before the guard is taken(新增) 固定「通过预检后、拿到守卫前锁被换掉」这个顺序,新旧代码都通过。删掉守卫内的复查后失败,因为新锁被误删
never deletes a replacement created after stale removal、uses one reaper guard across real and symlink checkout aliases(调整) 这两条要构造「第一个回收者持有守卫时被暂停」,原来靠的是在第一次 stateLockHolderIsDead 调用处暂停,因为旧代码里第一次存活检查发生在守卫内。加了预检后,第一次存活检查提前到守卫外,暂停时还没拿到守卫,两个回收者的角色就会对调:真正回收死锁的变成了第二个,没有误删。现在改成在第一个回收者拿到守卫之后显式暂停,场景和断言不变,在新旧代码上都通过。第一条原来的 staleChecks 只会是 1,toBe(1) 其实什么也没检查。现在改成统计守卫被拿到的次数,能真正证明第二个回收者被守卫挡住了

Behavior Changes and Affected Users

受影响的人群:打开了实验特性 Tower 的用户,开关是 KIMI_CODE_EXPERIMENTAL_TOWER 或 config.toml 的 [experimental] tower,CLI 和桌面端都有。没有改动提示词、配置项或契约文件。

Behavior Before After Who relies on the old behavior Escape hatch
持锁者活着时的过期锁检查 每次都去拿 git 回收守卫,起 2 到 4 个 git 进程,再判断持锁者活着、放弃 读锁文件判断持锁者活着,直接跳过,不起 git 进程 无:原来这些 git 调用不产生任何结果 —
持锁者已死时的过期锁回收 拿守卫,两次确认后删锁 先预检一次,再走同样的守卫路径删锁 无:判定条件不变,只多读一次锁文件 —
崩溃的回收者留下的守卫 ref(refs/tower/state-lock-reaper-*) 任何一次过期锁检查都会顺带检查它,持有者已死就接管 只在持锁者看起来已死、需要回收时才会接管 无:这个 ref 只在回收时起作用,需要回收时照常接管。recovers from incomplete legacy claims and a crashed reaper guard 继续覆盖这条路径 —
多个 agent 同时写 Tower 状态或发消息 负载下可能等锁超时,工具调用报错 同样的负载下不超时 — —

changeset:@moonshot-ai/agent-core-v2 技术条目,CLI 和桌面端各一条(patch)。

验证

只在 macOS 上验证过,Linux 和 Windows 交给 CI。改动只涉及读文件和 process.kill(pid, 0),原来的守卫路径就已经在各平台上这样调用。新增用例用到的 process.ppid,在 Windows 上同样可以用 kill(pid, 0) 探测。

  • 负载对比(6 份副本同时跑,另有 6 个吃满 CPU 的进程,三条并发用例):修复前 12 次全部失败,最慢 12.2 秒;修复后 18 次 0 失败,最慢 1.07 秒。
  • 新增用例的失败条件都核对过:回归用例在还原产品代码后失败(acquireStateLockReaper 被调用了 1 次);复查用例在删掉守卫内的复查后失败。两条调整过的竞态用例,在新旧产品代码上都通过。
  • packages/agent-core-v2/test/features/tower 下 598 条通过。packages/node-sdk/test/sdk-rpc-client-v2.test.ts 用到了 TowerStore,173 条通过。
  • pnpm lint 通过,agent-core-v2 类型检查通过,pnpm run check:duplication 没有新增重复。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

… reaper guard

Every Tower lock waiter runs a stale-lock check every 250 ms. The check
took the git-backed reaper guard first (rev-parse, hash-object,
update-ref, then update-ref -d on release) and only then read the lock
file to see whether its holder had died. With a live holder all of that
git work is wasted, and with many waiters the spawned git processes
starve the holder itself: under load one concurrent send/register case
spent 175 s of cumulative time in reaper checks within 11 s and timed
out on the 10 s lock wait in 12 of 12 runs.

Read the lock file and check the holder's liveness before taking the
guard, and skip the round while the holder is alive. The guarded path
is unchanged: it still re-reads the lock, confirms the holder is dead
twice and compares the content before removing it.

The two reaper race cases paused on the first liveness check, which
used to run under the guard; they now pause explicitly once the first
reaper holds the guard, and the first one counts guard acquisitions so
it proves the second reaper was locked out. New cases pin that a live
holder never makes a waiter take the guard, and that the guarded
recheck still protects a lock replaced between the unguarded check and
the guard.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6b50a20

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@moonshot-ai/kimi-code Patch
kimi-code-app Patch
@moonshot-ai/agent-core-v2 Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T03:28:26.319445Z 6b50a20 PR opened
🔒 Security Review ✅ Completed 2026-11-26T03:30:19.837032Z 6b50a20 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@6b50a20
npx https://pkg.pr.new/@moonshot-ai/kimi-code@6b50a20

commit: 6b50a20

@RealKai42
RealKai42 merged commit 59dda4a into main Nov 26, 2026
22 checks passed
@RealKai42
RealKai42 deleted the fix/tower-lock-reaper branch November 26, 2026 03:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant