Skip to content

perf(agent-core-v2): limit config env reads by domain - #4259

Open
huangzheng2016 wants to merge 1 commit into
MoonshotAI:mainfrom
huangzheng2016:perf/config-domain-env-reads
Open

huangzheng2016 wants to merge 1 commit into
MoonshotAI:mainfrom
huangzheng2016:perf/config-domain-env-reads

Conversation

@huangzheng2016

Copy link
Copy Markdown
Contributor

Requirement or Bug

Agent turn 中频繁读取单个配置域时,重复计算无关域的环境变量覆盖导致 CPU 开销过高。

Bug Reproduction Steps

  1. 用录制响应和工具结果回放 agent turn,不发起模型请求或执行工具。
  2. 对回放进程采样 CPU;getEnv 与 applySectionEnvBindings 分别占活跃采样约 16.6% 和 4.2%。
  3. 在同一批 40 个 session 上分别运行改动前后代码,各交替运行三轮。

Root Cause

ConfigService.get(domain) 为读取一个域调用 freshEffective(),每次都会检查所有配置域的环境变量绑定。仅两个内置覆盖需要跨域数据;其余域的读取不需要完整计算。这是针对该重复计算的直接修复。

Code Changes

get(domain)
  if memory has domain: return memory value
- compute all effective domains and overlays
+ if every overlay declares that it does not affect domain:
+   apply environment bindings for domain only
+ else:
+   compute all effective domains and overlays

两个内置覆盖声明各自影响的域;未声明影响域的覆盖继续走原来的完整计算路径。

Behavior Changes and Affected Users

行为 之前 之后 依赖旧行为的人群 退路
读取不受覆盖影响的单个配置域 每次检查所有域的环境绑定 只检查目标域的环境绑定 TUI、print、desktop/web、VS Code、ACP、SDK 中运行 agent turn 的用户 自动保留:未声明影响域的覆盖仍走完整路径
读取受覆盖影响的域及实时环境变量更新 完整计算,读取当前环境变量 同样完整计算并读取当前环境变量 使用模型、provider、服务凭证环境变量的用户 不需要

受影响模块:agent-core-v2 配置读取;配置解析、覆盖的返回值和配置文件格式不变。配置测试 130/130 通过,typecheck 和定向 oxlint 通过。六轮共同严格匹配的 81 个 agent、82 个 turn 中,三组候选 turn CPU 总量分别下降 22.6%、18.8%、23.8%;p95 内存增量没有稳定下降。全包测试额外有 15 个与本次配置改动无关的失败,其中 11 个因环境缺少 zip,4 个是 MCP 输出断言。

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.

@changeset-bot

changeset-bot Bot commented Oct 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: bd6613d

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 Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T14:14:44.601653Z bd6613d 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 Oct 16, 2026

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

commit: bd6613d

This branch has not been deployed

No deployments
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