Repository navigation
fix(agent-core-v2): release the environment lease when a git, gh or rg command fails to start - #5532
Merged
Merged
Conversation
…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 detectedLatest commit: 2e7eeba 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 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
远端主机上没装 gh(或 git)时,每次查询 PR 或 git 状态都会泄漏一个环境租约。之后移除这个环境(远端环境的声明变化、退出程序)时,要等满 5 秒的租约排空超时,用户感觉到的是退出或切换远端环境时卡了大约 5 秒。
这个 PR 在命令启动失败时释放租约。没装 gh、没装 git、rg 起不来时原有的降级和报错都不变。
Bug Reproduction Steps
kap-server 的
sessionList.test.ts里有 3 条用例走的就是这条路径,修复前每条都在最后多等 5 秒。Root Cause
GitService.runCommand先拿环境租约,再启动命令。启动失败时直接返回exitCode: -1,没有释放租约。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
其余行为不变:
null),照样缓存 60 秒。status、pullRequest照样抛FS_GIT_UNAVAILABLE。受影响的人群:用远端环境的用户(桌面端和 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。改动只是在启动失败的分支里释放租约,没有平台分支,也不涉及路径处理。
sessionList.test.ts里那 3 条移除远端环境的用例,修复前后各跑一次:packages/agent-core-v2/test/app/git、packages/agent-core-v2/test/project/projectFs、kap-server 的sessionList.test.ts和fs.test.ts:共 373 条通过。pnpm lint通过;pnpm run check:duplication没有新增重复。scripts/checks/baselines/oxlint-ratchet.json少了一条no-unused-vars:gitService.test.ts里原来没用到的EnvironmentResolver导入,现在被新用例用上了。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.