Skip to content

feat(agent-core-v2): replace hidden media tool results with omission notices - #4601

Merged
sailist merged 3 commits into
mainfrom
feat-321-11-02-media-policy-omit-placeholder
Nov 2, 2026
Merged

sailist merged 3 commits into
mainfrom
feat-321-11-02-media-policy-omit-placeholder

Conversation

@sailist

@sailist sailist commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

纯文本模型(image_in/video_in 均不成立)的会话中,历史里 ReadMediaFile 的 tool result 在每次请求的投影阶段被整条删除,模型完全感知不到"这里曾经读过某个媒体文件",连文件路径等纯文本上下文也一并丢失。本 PR 把含媒体内容的 result 从删除改为在原位置替换为一条 user 占位提醒,让模型知道此处省略了一段媒体内容、来源文件路径、以及当前模型看不到它的原因。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

核心思路:agent-media-tools resolver(mediaPolicy.ts)在纯文本模型请求中本来就要过滤 ReadMediaFile 的广告面和历史调用;本 PR 只改变含媒体标签的 result 的处理方式——从"整条丢弃"改为"原位替换为 user 消息",其余逻辑不变。assistant 侧所有 ReadMediaFile tool call 仍被过滤(tools 列表中该工具已被剔除,其调用痕迹保持一致过滤;且 result 变为非 tool 角色后保留 call 会产生悬挂调用)。

 hideReadMediaFile(messages)
   按名字收集所有 ReadMediaFile call id,扫描其 result 中的 image/video 媒体标签
   tool result 处理:
-    命中的一律整条丢弃
+    result 含媒体标签 → 替换为 UserMessage:
+      "[image omitted: a ReadMediaFile tool result was hidden because
+        the current model does not support image input; use a model with
+        image input capability to view it]" + 文件路径 tag
+    result 无媒体标签 → 整条丢弃(不变)
   assistant 消息:过滤所有 ReadMediaFile toolCalls(不变,含无 result 的悬挂 call)
   profile.tools:过滤 ReadMediaFile(不变)

文案风格对齐仓库既有写法(mediaResolverService.ts 的 unsupportedMedia:omit 说明 + capability 提示 + buildMediaPathTag 路径标签)。占位消息只影响发给模型的请求投影,持久化历史不动,切回视觉模型后历史调用与结果原样恢复。

关键决策:占位采用 user 而非 system 角色——文件路径来自工具结果,属于不可信内容(文件名可包含换行与指令性文本),放入 system 消息会使注入内容获得更高指令优先级(部分 adapter 还会提升为 developer);user 角色保持提醒语义的同时避免这一提权。

阅读指南:只需看 packages/agent-core-v2/src/agent/loop/mediaPolicy.ts 的 mediaTagOf / omittedReadMediaResult / hideReadMediaFile;测试改动在 llmRequesterService.test.ts 的既有 media 用例内,可略读。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
纯文本模型请求中,含媒体标签的 ReadMediaFile 历史 tool result 整条从请求消息中删除 原位替换为 user 占位消息(媒体类型 + capability 提示 + 文件路径) 使用纯文本模型的会话(所有客户端,经 kap-server 请求投影);旧行为使模型对媒体读取历史完全无感知 无
纯文本模型请求中,无媒体标签的 ReadMediaFile 历史 tool result 整条删除 不变 — —
assistant 历史消息中的 ReadMediaFile tool call(含悬挂 call) 全部过滤 不变 — —
请求 tools 列表中的 ReadMediaFile 纯文本模型时过滤 不变 — —
视觉模型(image_in 或 video_in 成立)请求 消息原样放行 不变 — —

受影响模块与测试覆盖:

  • packages/agent-core-v2/src/agent/loop/mediaPolicy.ts:test/agent/llmRequester/llmRequesterService.test.ts 的 media 用例覆盖带路径标签(变 user 占位)与无标签(仍删除)两种 result,并双向钉住纯文本与视觉模型(消息原样)行为;原有 tools/toolCalls 过滤断言保留。

验证结论:改动测试文件 61/61 通过;tsc --noEmit 通过;oxlint 与注释检查无新增告警。m0-dev 全量套件 18248/18276 通过,唯一失败为 sessionExport 时间戳 zip 用例(与本改动无交集,单文件重跑 22/22 通过,环境性失败)。

本地验证命令
  • corepack pnpm vitest run test/agent/llmRequester/llmRequesterService.test.ts:61/61 通过
  • corepack pnpm exec tsc --noEmit:通过
  • node scripts/check-no-comments.mjs:OK
  • oxlint(两个改动文件):0 warnings / 0 errors

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 2, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: e8089aa

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 2, 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-11-02T05:27:52.877928Z fe4f7d3 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 2, 2026 •

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

commit: e8089aa

@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: 4409496d75

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

`use a model with ${tag.kind} input capability to view it]`;
return {
role: 'system',
content: [{ type: 'text', text: `${hint}\n${buildMediaPathTag(tag.kind, tag.path)}` }],

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 Keep untrusted paths out of system-role notices

When a workspace or attachment filename contains instruction-like text (POSIX filenames can include newlines), tag.path is derived from the tool result and inserted into a real system message. buildMediaPathTag escapes XML delimiters but not semantic prompt content, and the OpenAI Responses adapter preserves this as system or promotes it to developer, so a repository-controlled filename gains higher authority than its original tool output. Keep the dynamic path at user/tool authority rather than embedding it in a system-role notice.

Useful? React with 👍 / 👎.

Comment on lines +22 to +27
if (tag === undefined) {
return {
role: 'system',
content: [{
type: 'text',
text: '[media omitted: a ReadMediaFile tool result was hidden because the current model does not support image or video input]',

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 Distinguish failed reads from omitted media

When an older session contains a failed ReadMediaFile call—such as an empty, missing, oversized, text, or unsupported file—the result has no media tag and now becomes this notice claiming media was omitted because the model lacks image/video support. That replaces a failed operation with a false successful-media implication, so a text-only model can reason as though a file was read; inspect the result's error state and emit a failure-specific notice instead.

AGENTS.md reference: AGENTS.md:L111-L113

Useful? React with 👍 / 👎.

if (message.toolCallId === undefined || !hiddenCallIds.has(message.toolCallId)) {
out.push(message);
} else {
out.push(omittedReadMediaResult(message));

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 Add a changeset for the visible behavior

This commit intentionally changes user-visible model behavior for every text-only session with ReadMediaFile history, but its file list contains no .changeset entry. Add a patch changeset for @moonshot-ai/kimi-code describing the new omission notices so the behavior change appears in the CLI release notes.

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

Useful? React with 👍 / 👎.

@sailist
sailist merged commit b4da921 into main Nov 2, 2026
16 of 17 checks passed
@sailist
sailist deleted the feat-321-11-02-media-policy-omit-placeholder branch November 2, 2026 05:36
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 3, 2026
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