Skip to content

refactor(agent-core-v2): run both ripgrep paths of the fs service through one process runner - #5527

Merged
RealKai42 merged 1 commit into
mainfrom
refactor/fs-rg-process-lifecycle
Nov 26, 2026
Merged

RealKai42 merged 1 commit into
mainfrom
refactor/fs-rg-process-lifecycle

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

packages/agent-core-v2/src/project/projectFs/fsService.ts 里,文件建议(suggestWithRg)和内容搜索(grepWithRg)各自带着一份几乎相同的 ripgrep 进程生命周期代码。两份已经开始走样,生命周期上的修复也要改两遍:接下来要修的「启动失败时泄漏环境租约」,就是两份里各有一处。

这个 PR 只做合并,不改行为。它是那个修复的前置:合并之后,修复只需要改一处,CI 的新增重复检查也不会再因为改到这段重复代码而报错。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

新增私有方法 runRgLines(binary, args, signal, onLine),统一负责:

  • 在环境租约下启动 rg;
  • 中止信号触发时强杀 rg;
  • 把 stdout 按行切分,交给调用方的 onLine;被自己强杀时,容忍 stdout 的提前关闭;
  • 等待 stderr 读完和进程退出;
  • 释放进程和租约,返回退出码,以及是否是自己强杀的。

两个调用方只保留各自的逐行处理和收尾逻辑。

旧路径的功能清单,以及每项在新代码里的位置
行为 原来在哪 现在在哪
在环境租约下启动 rg 两处各写一遍 runRgLines
中止信号触发时强杀 rg(只杀一次) 两处各写一遍 runRgLines
stdout 按 \n 切分,结尾不完整的一行最后再处理 两处各写一遍 runRgLines
被自己强杀后,stdout 提前关闭的错误不抛出 两处各写一遍 runRgLines
等 stderr 读完、等进程退出(wait 出错按 -1 处理) 两处各写一遍 runRgLines
移除中止监听、释放进程、释放租约 两处各写一遍 runRgLines
跳过空行 只有内容搜索 runRgLines。文件建议的 handleLine 本来就会在去掉 \r 和开头的 ./ 之后丢掉空行,所以对它没有影响
结果达到上限时强杀 rg 内容搜索 内容搜索的 onLine 调用 stop()
结尾不完整的那一行,即使达到上限也不强杀 内容搜索 runRgLines 在 stdout 读完后让 stop() 失效
退出码不是 0 或 1、且不是自己强杀时抛错(调用方随后退回 Node 遍历) 文件建议 文件建议根据 runRgLines 返回的值判断
不看退出码 内容搜索 内容搜索忽略返回值
启动失败时不释放租约 两处 runRgLines,原样保留,由后续的修复 PR 处理

内容搜索的累加器 RgJsonAccumulator 现在在启动 rg 之前创建。它的构造函数只保存参数,没有副作用。

Behavior Changes and Affected Users

None。上面的清单逐项说明了原有行为的去处,没有改动配置、契约文件或提示词。

没有 changeset:没有可感知的行为变化。

验证

只在 macOS 上验证过,Linux 和 Windows 交给 CI。改动不涉及路径处理或平台分支。

  • 变异检验:
    • 让「达到上限就停」不生效,「stops rg as soon as max_total_matches is reached」失败;
    • 去掉文件建议的退出码检查,「falls back to the node walk when rg exits with an error status」失败;
    • 让中止不再强杀 rg,原有的 234 条用例全部照样通过:这一项原来没有测试盯着。
  • 新增两条用例,固定「内容搜索 / 文件建议到了截止时间,就会强杀 rg」。它们用假计时器推进到截止时间,配一个不被杀掉就永远不结束的假进程。在本 PR 之前的代码上也通过;在「中止不强杀」的变异上,两条都在约 1 秒内失败。
  • packages/agent-core-v2/test/project/projectFs、kap-server 的 fs.test.ts 和 workspaceFs.test.ts、node-sdk 的 sdk-rpc-client-v2.test.ts、app-core 的 daemon-client.test.ts:共 536 条通过。
  • pnpm run check:duplication:没有新增重复。fsService.ts 内部的重复从 4 处降到 3 处,两条 ripgrep 路径之间那段 19 行的重复没有了。
  • pnpm lint 通过,agent-core-v2 类型检查通过。

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.

…ough one process runner

File suggestions and content search each carried their own copy of the
ripgrep process lifecycle: spawn under an environment lease, kill on
abort, split stdout into lines while tolerating the premature close a
kill causes, wait for stderr and the exit code, then release the
process and the lease. The copies had drifted apart, and a fix to that
lifecycle had to be made twice.

Move the lifecycle into runRgLines. Each caller now passes only its line
handler; the runner skips empty lines, which the suggest handler already
ignored, and hands back the exit code and whether it killed rg. The
differences between the callers stay where they were: search still
stops rg once its accumulator is capped (the stop callback no longer
fires for the trailing partial line, as before) and still ignores the
exit code, and suggest still fails on an exit code other than 0 or 1
unless it killed rg. Behaviour is otherwise unchanged, including the
lease not being released when the spawn itself fails.

Two cases pin that an expired grep or suggest deadline kills rg, which
no existing case covered: a runner that ignored the abort signal passed
all 234 fs tests. They pass on the code before this change too.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 01d609a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a 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-26T11:24:30.721057Z 01d609a 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@01d609a
npx https://pkg.pr.new/@moonshot-ai/kimi-code@01d609a

commit: 01d609a

@RealKai42
RealKai42 merged commit 7f71717 into main Nov 26, 2026
22 checks passed
@RealKai42
RealKai42 deleted the refactor/fs-rg-process-lifecycle branch November 26, 2026 11:28
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