Skip to content

fix(agent-core-v2): release the environment lease when a git, gh or rg command fails to start - #5532

Merged
RealKai42 merged 1 commit into
mainfrom
fix/git-spawn-lease-leak
Nov 26, 2026
Merged

RealKai42 merged 1 commit into
mainfrom
fix/git-spawn-lease-leak

Conversation

@RealKai42

@RealKai42 RealKai42 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

远端主机上没装 gh(或 git)时,每次查询 PR 或 git 状态都会泄漏一个环境租约。之后移除这个环境(远端环境的声明变化、退出程序)时,要等满 5 秒的租约排空超时,用户感觉到的是退出或切换远端环境时卡了大约 5 秒。

这个 PR 在命令启动失败时释放租约。没装 gh、没装 git、rg 起不来时原有的降级和报错都不变。

Bug Reproduction Steps

  1. 连接一台装了 git、没装 gh 的远端主机,在上面开一个会话。
  2. 打开会话列表。列表会查这个会话工作目录的 git 信息,其中 PR 那一步用 gh 查询,gh 在远端起不来。
  3. 退出程序,或者改掉这个远端环境的声明。
  4. 移除这个环境时要多等大约 5 秒。

kap-server 的 sessionList.test.ts 里有 3 条用例走的就是这条路径,修复前每条都在最后多等 5 秒。

Root Cause

GitService.runCommand 先拿环境租约,再启动命令。启动失败时直接返回 exitCode: -1,没有释放租约。

  • 本地环境不受影响:本地会先在 PATH 里找程序,找不到就在拿租约之前返回。
  • 远端环境会把 git、gh 这个名字直接交给远端去启动,没装时启动失败,于是每次都泄漏一个租约。

EnvironmentRegistry.remove() 移除环境时,会等租约数降到 0,最多等 ENVIRONMENT_DRAIN_TIMEOUT_MS(5 秒)。泄漏的租约永远不会释放,所以每次都要等满 5 秒。

文件服务的 runRgLines(MoonshotAI/kimi-code#5527 合并出来的 rg 进程运行器)也是同样的写法:在释放租约的 try 之外 await spawn(...),启动抛错时租约就漏掉了。

这是根本修复:启动失败的分支补上释放租约,不改任何超时。

Code Changes

  • packages/agent-core-v2/src/app/git/gitService.ts:runCommand 在启动失败时先 lease.dispose(),再返回 exitCode: -1。
  • packages/agent-core-v2/src/project/projectFs/fsService.ts:runRgLines 的启动抛错时先释放租约,再把错误原样抛出。文件建议和内容搜索两个调用方都走这里,所以只改这一处。

Behavior Changes and Affected Users

行为 改动前 改动后 谁依赖旧行为 逃生口
git、gh、rg 启动失败之后,移除该环境(远端环境声明变化、退出程序) 等满 5 秒的租约排空超时 立即完成 没有人:这是泄漏 不需要

其余行为不变:

  • 没装 gh:PR 照样显示为没有(null),照样缓存 60 秒。
  • 没装 git:status、pullRequest 照样抛 FS_GIT_UNAVAILABLE。
  • rg 起不来:文件建议照样退回 Node 遍历;内容搜索照样报错。

受影响的人群:用远端环境的用户(桌面端和 Web 的会话列表会查远端会话的 git 信息),前提是远端主机上缺 gh 或 git,或者 rg 在那里起不来。本地环境下缺 git、gh 不受影响。

测试覆盖:

  • packages/agent-core-v2/test/app/git/gitService.test.ts:远端环境没装 gh(status 照常返回,其中的 pullRequest 为 null),以及没装 git 时的 status、pullRequest 两个入口,都断言租约数回到 0。
  • packages/agent-core-v2/test/project/projectFs/fsService.test.ts:rg 启动失败时,suggest 退回 Node 遍历、grep 报错,都断言租约数回到 0。
验证

只在 macOS 上验证过,Linux 和 Windows 交给 CI。改动只是在启动失败的分支里释放租约,没有平台分支,也不涉及路径处理。

  • 5 条回归用例在修复前都失败(租约数为 1),修复后通过。
  • kap-server sessionList.test.ts 里那 3 条移除远端环境的用例,修复前后各跑一次:
    • 「routes session git by environment even when local and remote cwds collide」:5.1 秒 → 0.4 秒;
    • 「does not cache remote git while its environment is not ready」:5.1 秒 → 0.3 秒;
    • 「times out a stalled remote git lookup without starting a second one」:11.1 秒 → 6.1 秒。剩下的 6 秒是这条用例要证明的两次 3 秒远端 git 查询超时。
  • packages/agent-core-v2/test/app/git、packages/agent-core-v2/test/project/projectFs、kap-server 的 sessionList.test.ts 和 fs.test.ts:共 373 条通过。
  • agent-core-v2 类型检查通过;pnpm lint 通过;pnpm run check:duplication 没有新增重复。
  • scripts/checks/baselines/oxlint-ratchet.json 少了一条 no-unused-vars:gitService.test.ts 里原来没用到的 EnvironmentResolver 导入,现在被新用例用上了。

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.

…g command fails to start

GitService.runCommand acquires an environment lease before it spawns the
command, but returned early without releasing it when the spawn failed.
A remote environment hands the program name to the remote host, so a
host without gh failed that spawn on every pull request lookup, and each
lookup leaked a lease. Removing that environment, which happens when its
declaration changes and on shutdown, then waited out the five second
lease drain timeout. The fs service's ripgrep runner had the same shape:
it awaited the spawn outside the try that releases the lease.

Release the lease when the spawn fails in both places. The fallbacks are
unchanged: a missing gh still yields no pull request, a missing git still
reports FS_GIT_UNAVAILABLE, file suggestions still fall back to the node
walk and content search still rejects. Regression cases cover a remote
environment without gh, without git (status and pullRequest), and rg
failing to start for suggest and grep. The kap-server session list cases
that removed such an environment drop from 5.1 s to 0.1 s.

The ratchet baseline drops one no-unused-vars finding in the git service
test, whose EnvironmentResolver import is now used.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2e7eeba

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-26T11:47:02.803982Z 2e7eeba 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@2e7eeba
npx https://pkg.pr.new/@moonshot-ai/kimi-code@2e7eeba

commit: 2e7eeba

@RealKai42
RealKai42 merged commit 0235ca0 into main Nov 26, 2026
23 checks passed
@RealKai42
RealKai42 deleted the fix/git-spawn-lease-leak branch November 26, 2026 11:57
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