Skip to content

feat(agent-core-v2): attach Kimi request metadata - #4830

Merged
sailist merged 1 commit into
mainfrom
feat-350-11-11-kimi-request-metadata
Nov 11, 2026
Merged

sailist merged 1 commit into
mainfrom
feat-350-11-11-kimi-request-metadata

Conversation

@sailist

@sailist sailist commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

分析 Kimi Code 的模型请求时,服务端日志只有 HTTP 请求本身,无法区分请求来自哪种 Agent、这一轮为什么开始、这一步为什么发出,以及它属于哪一次用户输入。本 PR 将 #4753 的语义应用到最新 main:声明了 requestMetadata 的 provider 在模型请求上附带 X-Msh-Client-Metadata,供请求日志关联。目前只有 Kimi 声明该能力。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

归因由 RequestMetadataTracker 与 Session 级 ISessionRequestAttributionService 完成。AgentLoopRequestFactory.resolveRequest() 在统一装配点通过现有 endpoint.headers 附加 header,AgentLoopRequestObserver.setRequestId() 统一上报当前 Turn 的 request ID;是否附带由 provider definition 上的 requestMetadata: true 决定。Core Loop、SessionMetadata、SubagentService 和各工具不需要透传归因参数。

头里是一份 JSON:

  • agent_role 来自 IAgentScopeContext.meta,映射为 main、btw、tower_worker、swarm_member、subagent、other。
  • turn_trigger 来自 TurnPrompt.origin。压缩请求发生在 Turn 内时沿用当前 trigger,idle /compact 归为 user。
  • step_reason 由 Turn 事件决定:收到 TurnPrompt 为 turn_start,首步 retry/recovery 保持该值;收到 TurnStepCompleted 后为 tool_results;full compaction 为 compact。
  • user_input_id 记录真实用户发出的 prompt ID,后续 cron、background task、goal continuation 和 idle 压缩沿用。子 Agent 在实时派发或 fork 时继承父值,主 Agent 可从 Wire 历史恢复。
  • parent_request_id 只在符合条件的子 Agent 上有值,实时 subagent spawn/resume 时采样父 Agent 当时的 active request ID;swarm retry 和 tower inbox 不重新采样,纯 replay、/init 和 /btw 不带。
  • 非 ASCII 转为 \uXXXX,保证 header 是合法 HTTP ByteString;无值的可选字段省略。

阅读顺序:agent/loop/requestMetadata.ts(映射与事件折叠)、session/requestAttribution/(跨 Agent 归因)、agent/loop/requester.ts(统一 header 装配)、agent/loop/requestObservation.ts(request ID 上报)。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
providerType === 'kimi' 的模型请求 HTTP headers 不带 X-Msh-Client-Metadata 带 agent_role、turn_trigger、step_reason,以及有值时的 user_input_id、parent_request_id。仅声明 requestMetadata 的 Kimi 协议发送 无客户端或网关依赖该请求头缺失 无
Kimi 压缩请求 与普通模型请求一样不带该头 step_reason 为 compact;idle 压缩的 turn_trigger 为 user,并沿用上一轮 user_input_id 无 无
其他 provider 的模型请求 不带该头 不变 未声明 requestMetadata 的 provider 不受影响 无

验证:

  • @moonshot-ai/agent-core-v2 typecheck 与 build 通过;根 pnpm run lint 为 0 errors。
  • requester、projector、loop 三个测试文件共 186 个测试通过,覆盖角色和 trigger 映射、步骤重试、父子归因、replay 恢复、压缩、provider 门控和非 ASCII header;测试声明总数未增加。
  • m0-dev 远程复验仍受 SSH hostkey 与半成品同步环境阻塞,未将远程测试记为通过。

本变更不改变 CLI 用户可见行为,按 gen-changesets 规则不需要 changeset;不需要文档更新。

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 Nov 11, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5a2c66b

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 11, 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-11T09:43:10.029282Z 5a2c66b 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 11, 2026 •

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

commit: 5a2c66b

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 13f713003e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

request: this.protocolRegistry.request(model, params),
request: provider?.requestMetadata === true
? request.with({
endpoint: { headers: requestMetadataHeaders(this.requestMetadata.resolve(overrides.source)) },

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve the configured metadata header

When a Kimi provider already sets X-Msh-Client-Metadata through the documented providers.*.custom_headers option, this later .with() deep-merge replaces that value with the generated JSON because the new value wins on a same-named header. Before this commit, the protocol request retained the configured header assembled in ProtocolAdapterRegistry; after upgrading, gateway routing, signing, or correlation data using this header is silently discarded with no escape hatch. Preserve an explicit configured value, or reject/reserve the key with a migration path instead of silently overriding it.

AGENTS.md reference: AGENTS.md:L110-L112

Useful? React with 👍 / 👎.

this.activeTurn = {
turnId: input.turnId,
turnTrigger: input.continuation === 'retry' ? 'swarm_retry' : turnTriggerOf(origin),
stepReason: input.continuation === 'resume' && input.step() > 1 ? 'tool_results' : 'turn_start',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Mark the first resumed follow-up as tool results

When a process resumes an interrupted turn after its first LLM step, the restored turn carries step === 1, and the loop creates this execution before incrementing it to step 2. Consequently, this > 1 check labels the resumed request turn_start even though it consumes the completed or synthesized-aborted tool results; this misclassifies Kimi requests for sessions resumed after a crash during the first tool execution. A resumed turn with any prior step (input.step() > 0) should use tool_results.

Useful? React with 👍 / 👎.

const next: AgentRequestAttribution = {
...current,
userInputId: parent.userInputId ?? current.userInputId,
parentRequestId: parent.activeRequestId ?? current.parentRequestId,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clear stale parent IDs on a fresh dispatch

When a previously used subagent is dispatched again and the new parent request has no request ID, this nullish fallback retains the subagent's parent ID from its earlier run. Absent request IDs are supported, so a resumed subagent using Kimi can then be logged as a descendant of an unrelated previous parent request. A fresh dispatch should store the current parent's activeRequestId directly, including clearing the field when it is undefined.

Useful? React with 👍 / 👎.

@sailist
sailist force-pushed the feat-350-11-11-kimi-request-metadata branch from 13f7130 to 5a2c66b Compare November 11, 2026 09:40
@sailist
sailist merged commit 82a0388 into main Nov 11, 2026
17 checks passed
@sailist
sailist deleted the feat-350-11-11-kimi-request-metadata branch November 11, 2026 09:54
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 15, 2026
Port MoonshotAI#4901, MoonshotAI#4909 and MoonshotAI#4911: complete the task before asserting a
zero-timeout wait sees the completed snapshot, restore the instructions
reload spy after the failure assertion, poll the search index until it is
ready before asserting the page, and run remote-session git probes
through a host process that refuses gh.

Record MoonshotAI#4840 as equivalent (the refresh path already rebuilds from the
effective binding here) and pin it with a regression case, record MoonshotAI#4883
as ported with the compaction profile already present, and record MoonshotAI#4830
as blocked on the absent loop requester/request-attribution layering.
Clear the generated-bundle, release-sync and working-notes rows with
their applicable verdicts.
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