Repository navigation
fix(agent-core-v2): rescan once after a macOS native file watch starts - #5487
Merged
Merged
Conversation
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 detectedLatest commit: 7935c7f The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
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: |
This was referenced Nov 26, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
同时运行 6 份
pnpm exec vitest run packages/agent-core-v2/test/features/skill/project/skillCatalog.test.ts -t "refreshes the configured catalog when",另开 8 个吃满 CPU 的进程。the root link is created72 次失败 7 次,等了 5 秒仍报expected undefined to be 'Replacement target'。脱离产品代码的最小复现:在 macOS 上
fs.watch(dir)返回后立刻把一个链接 rename 进dir,再等 1 秒看有没有收到事件: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 的路径不会变慢关键决策
platform === 'darwin'写死Behavior Changes and Affected Users
只影响 macOS 上的 CLI 和桌面端;Linux 和 Windows 没有变化。没有改动提示词、配置项或契约文件。
AGENTS.md、项目 MCP 配置、local.toml、agent 配置KIMI_CODE_WATCH=0照旧可以关掉所有监听config.toml的监听config.toml本身的路径,整目录通知会被忽略逐项核对多出来的一次重新加载
notifySkillChanges先比较技能列表的哈希,没变就不提醒模型。插件的监听只关心插件来源;loop 的监听只重新计算子 agent 的允许列表,结果和原来一样。AGENTS.md:只有内容或警告变化时,才触发onDidChange。publishIfChanged,没变化就不发布,不会重连 MCP 服务。local.toml:附加目录列表没变化时,不触发fireChange。notifySubagentChanges)先比较子 agent 的名称集合,没变就不提醒。watchedPrimary或监听签名为准);watchCandidates在计划不变时什么也不做。所以多出来的通知只出现一次,不会一轮接一轮地触发。changeset:
@moonshot-ai/agent-core-v2技术条目,CLI 和桌面端各一条(patch)。验证
在 macOS 上验证过;Linux 和 Windows 交给 CI,这两个平台不设启动窗口,行为不变。
src/human/test/utils/watch.test.ts新增 4 条用例,都用可注入的假运行时,结果确定:mkdtemp建目录的事件会被 FSEvents 延迟送达,恰好满足断言。这条用例分辨不出修没修,所以没有加入。真实平台上的效果,靠下面的负载对比验证。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
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.