Skip to content

fix(agent-core-v2): ensure tool_result content is never empty - #4300

Closed
kimi-agent-bot wants to merge 2 commits into
MoonshotAI:release/2026-10-21from
kimi-agent-bot:fix/tool-result-empty-placeholder
Closed

kimi-agent-bot wants to merge 2 commits into
MoonshotAI:release/2026-10-21from
kimi-agent-bot:fix/tool-result-empty-placeholder

Conversation

@kimi-agent-bot

@kimi-agent-bot kimi-agent-bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A persisted tool.result wire record whose output folds to empty content (not a string, not a part array — e.g. {}, [], null) is re-lowered into every subsequent request as an empty tool message. Anthropic-protocol endpoints reject it with HTTP 400 tool_result.content: Field required, and strict OpenAI-compatible validators (e.g. LiteLLM) reject tool messages missing content with 422. Since the bad record stays in the session history, every retry, /compact, and resume fails identically — the session is permanently bricked.

Fix

Guard the serialization branches so an empty tool result lowers to the non-empty placeholder Tool output is empty. instead, at five lowering sites:

  • agent-core-v2 Anthropic lower: pad an empty tool_result block list
  • kosong Anthropic toolResultToBlock: same guard
  • agent-core-v2 OpenAI lower: placeholder for empty string / empty part list
  • agent-core-v2 OpenAI Responses lower: placeholder for empty function_call_output (both extract-text and keep-parts modes)
  • kosong OpenAI legacy: cover the three empty shapes it could produce — undefined (content key dropped at serialization), '', and all-empty-text part arrays

Media-only tool results keep their media blocks unchanged; non-empty results are byte-identical. Because every request re-lowers from the wire history, already-corrupted sessions self-heal with no migration.

Tests

  • Each lowering site gets unit tests for the empty shapes; anthropic-lower.test.ts replays a persisted bad tool.result through foldV2WireRecords to prove the session self-heals.
  • kosong openai-legacy: 5 new cases (empty content / single empty text / double empty text / think-only / empty under extract_text).
  • agent-core-v2 + kosong targeted suites green; typecheck clean.

拆分自 #4269 的 layer 1(layer 2「媒体拒收恢复」拆到另一个 PR;原 PR 保持不动)。

@changeset-bot

changeset-bot Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 15e0558

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 Minor

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 20, 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-20T17:30:18.827636Z e064d06 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.

kimi-agent-bot and others added 2 commits October 20, 2026 17:26
A tool result that serialized to empty content (e.g. a persisted
tool.result wire record whose output was neither a string nor a part
array, folded back as an empty tool message) was re-lowered into every
subsequent request, and Anthropic-protocol endpoints reject it with
400 "tool_result.content: Field required" — permanently bricking the
session, with /compact and resume failing the same way.

Guard the serialization branches so an empty tool result lowers to a
non-empty placeholder ("Tool output is empty.") instead:

- agent-core-v2 anthropic lower: pad an empty tool_result block list
- kosong anthropic toolResultToBlock: same guard
- agent-core-v2 openai lower: placeholder for empty string/part list
- agent-core-v2 openai-responses lower: placeholder for empty output

Since every request re-lowers from the wire history, fixing the
serialization self-heals already-corrupted sessions with no migration.
Media-only tool results keep their media blocks unchanged.
The OpenAI legacy (chat-completions) lowering left an empty tool result
as a dropped content key (no parts / think-only), an empty string
(extract_text of nothing, or a single empty text part), or an empty /
all-empty-text part array. Strict OpenAI-compatible validators (e.g.
LiteLLM) reject tool messages without content, the same permanent-brick
failure class as an empty tool_result on Anthropic endpoints.

Normalize every empty shape to the same 'Tool output is empty.'
placeholder used by the other lowering paths; non-empty tool results
pass through byte-identical. Also rewrites the changeset to describe
this PR's single purpose (empty tool results never serialize as empty
content) instead of the media-recovery scenario, which belongs to a
different PR of the split.
@pkg-pr-new

pkg-pr-new Bot commented Oct 20, 2026

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

commit: e064d06

@kimi-agent-bot
kimi-agent-bot force-pushed the fix/tool-result-empty-placeholder branch from e064d06 to 15e0558 Compare October 20, 2026 17:28
@kimi-agent-bot
kimi-agent-bot changed the base branch from main to release/2026-10-21 October 20, 2026 17:28

@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: e064d062c9

ℹ️ 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".

"@moonshot-ai/kimi-code": patch
---

Fix sessions permanently failing with HTTP 400 (`tool_result.content: Field required`, or missing tool message content on strict OpenAI-compatible validators such as LiteLLM) when an empty persisted tool result is replayed. A tool result that serialized to empty content was re-lowered into every subsequent request and rejected, so `/compact` and resume kept failing the same way. Empty tool results now lower to a non-empty placeholder text (`Tool output is empty.`) across the Anthropic, OpenAI, OpenAI legacy, and OpenAI Responses request paths, so already-affected sessions self-heal without any migration.

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 Condense the changeset to one user-facing sentence

This entry is three implementation-heavy sentences naming status codes, validator implementations, protocol paths, and migration details. Changesets feeds this text to CLI release notes, but the repository requires one short user-facing sentence stating only the visible change; condense it to the session-recovery outcome.

AGENTS.md reference: AGENTS.md:L90-L90

Useful? React with 👍 / 👎.

role: 'tool',
tool_call_id: message.toolCallId,
content:
content === undefined || content.length === 0 ? EMPTY_TOOL_CONTENT_PLACEHOLDER : content,

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 Pad all-empty output in every OpenAI lowerer

When toolMessageConversion: 'keep_parts' replays a tool result containing two or more empty text parts, content is a non-empty array, so this length-only guard forwards only empty strings instead of the placeholder (the new kosong legacy guard already treats that shape as empty). A repo-wide search also shows packages/kosong/src/providers/openai-responses.ts:528-548 still returns [] or '' for empty/think-only results. These inputs can therefore continue sending the validator-rejected payload and leave the session unrecoverable; detect usable content and apply the fallback in both lowerers.

AGENTS.md reference: AGENTS.md:L104-L104

Useful? React with 👍 / 👎.

@sailist

sailist commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

关闭说明:同一问题在 main 上已通过重构后的架构修复,本 PR 不再需要合入。

  • 本 PR 修改的 5 个文件在 origin/main 上均已不存在(llm 层经 refactor(agent-core-v2): port the agent-core llm core into human/llm #4352、refactor(core): extract loop, llm, and tool into a DI-free package #4820 两轮重构后迁至 packages/core)。
  • main 上由协议管线的单一收口保证空 tool result 不会上链:packages/core/src/llm/message.ts 的 renderToolResultForModel 把空输出(空串 / 纯空白 / 空数组 / 全空文本 part)替换为 <system>Tool output is empty.</system> 占位文本;packages/core/src/llm/protocol/pipeline.ts 的 lowerableMessage 对每条 role === 'tool' 消息在进入各 provider 编码前强制执行该渲染。anthropic / openai / openai-responses / google-genai 四个协议全部走 runPipeline 这一收口,无绕行路径;媒体 part 原样保留,且每次请求从历史重新渲染,已损坏会话同样自愈。
  • 因此 main 不需要此修复。若 release/2026-10-21 分支后续仍需发版修复,可 reopen。

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