Skip to content

fix(agent-core-v2): rescan once after a macOS native file watch starts - #5487

Merged
RealKai42 merged 1 commit into
mainfrom
fix/macos-watch-settle
Nov 26, 2026
Merged

RealKai42 merged 1 commit into
mainfrom
fix/macos-watch-settle

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

在 macOS 上,文件监听刚启动的那一瞬间建立的技能目录或链接、AGENTS.md、项目 MCP 配置、local.toml 和 agent 配置文件,有时不会被发现,要等下一次有别的变化才会生效。features/skill/project/skillCatalog.test.ts 的 refreshes the configured catalog when 'the root link is created' 也因此在负载下偶发超时。这个 PR 之后,这些变化会在监听启动约 100ms 内被补扫到。

Bug Reproduction Steps

  1. 同时运行 6 份 pnpm exec vitest run packages/agent-core-v2/test/features/skill/project/skillCatalog.test.ts -t "refreshes the configured catalog when",另开 8 个吃满 CPU 的进程。

  2. the root link is created 72 次失败 7 次,等了 5 秒仍报 expected undefined to be 'Replacement target'。

  3. 脱离产品代码的最小复现:在 macOS 上 fs.watch(dir) 返回后立刻把一个链接 rename 进 dir,再等 1 秒看有没有收到事件:

    负载 立刻 rename 隔 50ms 再 rename
    空闲 150 次丢 1 次 150 次丢 0 次
    8 个吃满 CPU 的进程 150 次丢 7 次 150 次丢 0 次

Root Cause

macOS 上,Node 对目录的 fs.watch 由 FSEvents 实现,而 FSEvents 的事件流是在另一个线程上异步启动的。fs.watch() 返回之后、事件流真正开始之前的变化,不会被送达。

共享的监听工具 src/human/utils/watch.ts 在 fs.watch() 返回时就把原生监听标成 ready(leg.nativeStarted)。这违背了「ready 之后的变化都会上报」这个承诺。

在失败的那条用例里临时加日志看到的顺序是:监听配置目录的入口监听报告 ready,2ms 后用例创建链接,之后再也没有收到事件。通过的那次,同样的顺序下,rename 之后 58ms 收到了事件。

Linux 的 inotify 和 Windows 的 ReadDirectoryChangesW 都是同步注册的,没有这个窗口。

Code Changes

改动 说明
运行时可以声明原生监听的启动窗口 WatchRuntime 新增可选字段 nativeStartSettleMs。只有 Node 的真实运行时在 macOS 上把它设为 100ms,其他平台不设
窗口结束后补发一次整目录失效通知 原生监听启动成功后,用运行时的 scheduleRetry 排一个一次性计时器,到点时发出一次整目录失效通知(leg.nativeSettled)。这和已有的恢复路径发出的是同一种通知,使用方收到后会重新扫描,把启动窗口里漏掉的变化补回来。原生监听出错、监听句柄被释放时,计时器随这一轮监听一起取消;监听恢复后,会重新计时一次
ready 的时机不变 原生监听仍然在 fs.watch() 返回时就 ready,等待 ready 的路径不会变慢

关键决策

决策 选择 没选 理由
怎么补上启动窗口 ready 照旧,窗口结束后补发一次失效通知 在 macOS 上把 ready 推迟 100ms 项目技能加载等路径会等 ready,推迟 ready 会让它们在 macOS 上都慢 100ms。补发通知只多一次重新扫描,各使用方本来就有 200ms 防抖
窗口长度放在哪 由运行时声明,默认不设 状态机里按 platform === 'darwin' 写死 现有测试的假运行时默认就是 darwin,而且按下标断言重试计时器。由运行时声明的话,现有断言一个不用改,新行为也能用假运行时单独测
窗口取多长 100ms 50ms 最小复现里隔 50ms 已经 0 丢失,取两倍余量。这是经验值:负载极端时,启动窗口仍可能超过 100ms

Behavior Changes and Affected Users

只影响 macOS 上的 CLI 和桌面端;Linux 和 Windows 没有变化。没有改动提示词、配置项或契约文件。

Behavior Before After Who relies on the old behavior Escape hatch
监听启动瞬间创建的技能、AGENTS.md、项目 MCP 配置、local.toml、agent 配置 可能不被发现,直到下一次变化 约 100ms 后被补扫到 无 KIMI_CODE_WATCH=0 照旧可以关掉所有监听
上述模块在监听启动后的重新加载 只在有事件时重新加载 每次监听启动约 100ms 后多重新加载一次 无:见下面的逐项核对 —
用户 config.toml 的监听 — 不变:它只认 config.toml 本身的路径,整目录通知会被忽略 — —

逐项核对多出来的一次重新加载

  • 技能:目录重新加载后,notifySkillChanges 先比较技能列表的哈希,没变就不提醒模型。插件的监听只关心插件来源;loop 的监听只重新计算子 agent 的允许列表,结果和原来一样。
  • AGENTS.md:只有内容或警告变化时,才触发 onDidChange。
  • 项目 MCP 配置:走 publishIfChanged,没变化就不发布,不会重连 MCP 服务。
  • local.toml:附加目录列表没变化时,不触发 fireChange。
  • agent 配置:重新加载后,给模型的子 agent 变更提醒(notifySubagentChanges)先比较子 agent 的名称集合,没变就不提醒。
  • 不会循环:这些模块重新加载时都不会重建监听(以 watchedPrimary 或监听签名为准);watchCandidates 在计划不变时什么也不做。所以多出来的通知只出现一次,不会一轮接一轮地触发。

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

验证

在 macOS 上验证过;Linux 和 Windows 交给 CI,这两个平台不设启动窗口,行为不变。

  • src/human/test/utils/watch.test.ts 新增 4 条用例,都用可注入的假运行时,结果确定:
    • 入口监听和信号监听,在窗口结束后各补发一次通知(两条)。
    • 原生监听失败后,旧计时器被取消;恢复后重新计时,事件序列正确。
    • 监听句柄释放后,计时器被取消。
  • 还原产品代码后,这 4 条全部失败;原有用例的断言没有改动。
  • 我也试过一条 macOS 真实文件系统的用例,但它在旧代码上也会通过:mkdtemp 建目录的事件会被 FSEvents 延迟送达,恰好满足断言。这条用例分辨不出修没修,所以没有加入。真实平台上的效果,靠下面的负载对比验证。
  • 负载对比(6 份副本同时跑,另有 8 个吃满 CPU 的进程,各 72 次):refreshes the configured catalog when 这组用例,修复前失败 7 次,修复后 0 次;the root link is created 最慢一次 0.74 秒。
  • packages/agent-core-v2/test/features/skill 和 src/human/test 共 240 条通过。
  • 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.

On macOS, fs.watch on a directory is backed by FSEvents, whose stream
starts asynchronously on another thread. The watch utility marked a
native watch ready as soon as fs.watch returned, so a change made right
after that could be lost: a bare fs.watch followed immediately by a
rename missed the event 1 of 150 times idle and 7 of 150 times with
eight CPU burners, and none of 150 with a 50 ms gap either way. The skill
catalog case that creates a configured root link right after loading
failed 7 of 72 runs under the same load.

The watch runtime can now declare a native start settle window. The Node
runtime sets it to 100 ms on macOS, and a native watch then emits one
root invalidation once that window has passed, so consumers rescan and
pick up anything created while the stream was starting. The timer is
cancelled when the native watch fails or the handle is disposed, and a
rearmed watch settles again. Other platforms register their native
watches synchronously and are unchanged.

Every watchCandidates consumer (skills, AGENTS.md, project MCP config,
local.toml, agent profiles) reloads once more shortly after its watch
starts on macOS. Their change notifications compare content first, so
the extra reload is not visible when nothing changed, and none of them
recreates its watches on reload.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7935c7f

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-26T04:42:27.735001Z 7935c7f PR opened
🔒 Security Review ✅ Completed 2026-11-26T04:43:50.162588Z 7935c7f 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@7935c7f
npx https://pkg.pr.new/@moonshot-ai/kimi-code@7935c7f

commit: 7935c7f

@RealKai42
RealKai42 merged commit b8f4b35 into main Nov 26, 2026
32 of 33 checks passed
@RealKai42
RealKai42 deleted the fix/macos-watch-settle branch November 26, 2026 05:05
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Dec 2, 2026
`bun scripts/upstream-ledger.mjs --refresh` at upstream/main 4eb2919
added 126 rows, so every open upstream commit and every PR it references
now has a ledger row: 0/596, down from 125 at 8bf8ba3 and 126 at the
fetched tip. The refresh only reads `upstream/main ^ main`, so the three
release-2.6.2 commits named by the mission (MoonshotAI#4875/MoonshotAI#4905/MoonshotAI#4922) gained
rows with the refresh's row shape.

Settled with live probes in this tree:

- ported: MoonshotAI#5533 (docs 2.8.1 changelog), MoonshotAI#4875 (registry search_url and
  feedback_url plus the feedback service), MoonshotAI#4905 (Kimi upload status
  validation in packages/core), MoonshotAI#4922 (swarm task events in
  sessionTaskFeed), MoonshotAI#5438 (unload only declared dynamic tools), MoonshotAI#5592
  (dynamic tool replacement order), MoonshotAI#5595 (append-log known length),
  MoonshotAI#5573 (skill roots for every work dir), MoonshotAI#5576 (date reminder cwd
  comparison), MoonshotAI#5618 (session titles for skill openers), MoonshotAI#5638 (frozen
  loop tool baseline), MoonshotAI#5626 (legacy turn-input origins)
- partial: MoonshotAI#5505 (processLiveness source and consumers present; the
  upstream unit test file was never carried), MoonshotAI#5571 (program-resume and
  OSC 7577 present; the upstream test helper was never carried), MoonshotAI#5532
  (gitService half present; fsService still leaks the environment lease
  at fsService.ts:1133 and :1267), MoonshotAI#5557 (app-client icon registry kept;
  the move to app-ui was never carried)
- not-applicable: MoonshotAI#4655 (upstream reverted auto review in MoonshotAI#5649), MoonshotAI#5333
  (PLAYBOOK:111 deliberate flat experimental flags)

M39's "likely present" list was checked, not trusted: MoonshotAI#5438 is present
(upstream test passes), MoonshotAI#5487 is not (nativeStartSettleMs/leg.nativeSettled
absent), MoonshotAI#5635 is not (ToolsSettled absent), MoonshotAI#5429 needs a visual probe.
The remaining 194 pending rows stay open with their reasons; no feature
was ported and no upstream-drift allowlist entry was added.

Verification: verify -> 332 closed rows, 332 carry every file of their
PR; 317 probes pass, 2 foreign-runner skips, 0 fail; a second verify is
byte-identical (ledger sha256 cbf53638...). Probe tests 29 pass; bun run
lint and node scripts/checks/ratchet.mjs green.
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