Skip to content

fix(agent-core-v2): reduce workspace registry read amplification - #4747

Merged
sailist merged 1 commit into
mainfrom
fix/workspace-catalog-read-amplification
Nov 9, 2026
Merged

sailist merged 1 commit into
mainfrom
fix/workspace-catalog-read-amplification

Conversation

@hunshcn

@hunshcn hunshcn commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Requirement or Bug

长时间运行的服务积累大量 workspace 后,项目注册或更新会让多个会话重复读取并解析同一份 projects.json,拖慢其他 workspace 操作。本 PR 复用未变化的项目索引,并合并等待执行的目录刷新,降低这部分重复工作。

Bug Reproduction Steps

  1. 准备包含大量项目的 projects.json,启动多个使用该项目注册表的会话。
  2. 连续注册或更新 workspace,使各会话收到多次注册表变更通知。
  3. 同时执行 workspace 查询。原实现会为每次通知排入刷新,并在查询过程中重复读取、解析全量索引;通知和会话越多,前台查询等待的工作越多。

Root Cause

FileWorkspacePersistence.load() 每次都重新读取并解析全量文档。每个 WorkspaceDirsService 又会为每次全局变更单独排入一次刷新,同一文件的解析工作被多个会话及连续通知放大。

本 PR 直接减少重复读取和重复排队,不改变超时时间,也不清理已有项目。

Code Changes

项目索引按文件版本复用解析结果;目录服务只保留一个尚未开始的刷新,刷新运行期间收到的变更仍可触发一次后续刷新。刷新继续调用 reloadWorkspace(),保留主目录变化时同步本地配置和文件监听的行为。

 load catalog
-  read and parse the entire file
+  reuse the parsed catalog when file metadata is unchanged
+  otherwise read the file and cache only a stable snapshot

 on registry change
-  enqueue one reload per notification
+  enqueue a reload only when none is pending
+  allow a follow-up reload once the current reload starts

缓存版本包含 dev、ino、size、mtimeNs 和 ctimeNs;读取前后版本不同则不缓存该次结果。文件监听通知和写入均使缓存失效。写操作继续在原有锁内读取最新文档;没有本地路径或 stat 失败时仍走原读取路径。

建议从 fileWorkspacePersistence.ts 的 load/update 看起,再看 workspaceDirsService.ts 的通知排队逻辑。

Behavior Changes and Affected Users

行为 修改前 修改后 依赖原行为的人群 退路
未变化项目索引的读取 每次重新读取并解析 文件版本一致时复用解析结果 使用本地项目注册表的 TUI、print、web/desktop、ACP 和 SDK 用户 无配置开关;无法取得文件版本时自动走原读取路径
连续注册表通知 每条通知分别排队刷新 合并尚未执行的刷新;运行期间的变化仍会再刷新一次 同时运行多个 workspace 会话的上述客户端 无配置开关;最终目录状态保持更新
文件替换、损坏及写入后的读取 重新读取文件 版本变化或主动失效后重新读取,损坏内容仍报错 外部脚本修改项目索引、多进程使用同一数据目录的用户 保留原读写及错误路径
外部工具原地覆写同尺寸内容,且文件时间戳未变化 下次查询重新读取内容 可能继续复用旧快照,直到监听通知使缓存失效或文件版本变化;不保证立即可见 直接编辑 projects.json 的外部脚本 使用临时文件加 rename 的原子替换,与内置写入方式一致

相关回归覆盖重复读取、写后可见、同大小且保留 mtime 的原子替换、损坏后恢复、读取期间原子替换,以及刷新进行中收到突发通知后最终目录仍正确。未改协议、配置默认值、项目数据格式或旧数据迁移;没有删除项目或改变 trust 判定。

本地验证
  • 基于 main 1c259c8d35ff648ce8aa3f77169d9887e2a16384 验证 workspace、aliases、dirs、trust 共 4 个测试文件:128 passed。
  • 读取期间原子替换的回归用例重复 30 次通过,使用与内置持久化相同的原子写入接口。
  • agent-core-v2 typecheck 通过;rebase 后的完整构建由新提交的 CI 验证。
  • lint 0 errors / 2697 warnings;no-comments 和提交时的 staged lint 检查通过。
  • 验证环境为 Linux ARM64;macOS 和 Windows 尚未本地验证。生产尚未部署。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve). No separate issue; reproduction and cause are described above.
  • 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. No user-facing command, configuration, or API documentation changed.

@changeset-bot

changeset-bot Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7ce5b31

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

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code 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 8, 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-09T11:31:14.528391Z 7ce5b31 New commits
ℹ️ 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 8, 2026 •

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

commit: 7ce5b31

@hunshcn
hunshcn force-pushed the fix/workspace-catalog-read-amplification branch from 387c4a8 to 7ce5b31 Compare November 9, 2026 11:28
@sailist
sailist merged commit 79ff88b into main Nov 9, 2026
17 checks passed
@sailist
sailist deleted the fix/workspace-catalog-read-amplification branch November 9, 2026 11: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.

2 participants