Skip to content

fix(agent-core-v2): recover failed uploaded image references - #5000

Open
pvzheroes125 wants to merge 1 commit into
MoonshotAI:release/2026-11-16from
pvzheroes125:fix/image-inline-recovery-release-20261117
Open

pvzheroes125 wants to merge 1 commit into
MoonshotAI:release/2026-11-16from
pvzheroes125:fix/image-inline-recovery-release-20261117

Conversation

@pvzheroes125

Copy link
Copy Markdown
Contributor

Requirement or Bug

已上传图片可能留下不可读的 ms:// 引用,导致包含这张历史图片的后续模型请求持续返回 400。即使会话仍保留本地原图,现有上传缓存也会一直复用失败引用。

本 PR 提供 #4948 图片恢复部分的独立替代方案,目标同为 release/2026-11-16;不替代 #4948 的其他修复。该分支已包含 #4905 的上传状态校验,本 PR 在其基础上补充错误发生后的恢复和持久标记。

Bug Reproduction Steps

  1. 会话保存两张本地图片,并缓存上传得到的两个文件引用。
  2. 其中一张上传文件的状态为 error,模型读取它时返回图片引用错误,或 invalid_request_error 类型的 Invalid request Error。
  3. 继续该会话:旧逻辑再次发送同一个失败引用,请求仍失败。

Root Cause

上传缓存记录远端文件 ID,但没有记录“这个本地图片在当前上传目标已明确失败”。上传响应校验可以防止新失败引用进入缓存,无法处理旧版本已经保存的失败引用。

这是客户端恢复措施:复用本地原图发送内联图片,不修复服务端文件。旧缓存引用只有在文件查询明确返回 status: error 或 extract_status: error 时才作永久标记;新上传响应明确失败时也写同一标记。

Code Changes

复用现有附件、上传缓存和请求恢复策略。正常请求不增加文件 GET;在限定的 400 后查询本次实际发送且能映射回本地图片的文件,保留正常引用,只让确认失败的图片回退原图。

正常请求 → 复用原有上传缓存,无文件状态查询
特定 400 → 对本次图片 ID 去重查询(最多 4 并发,总计 5 秒,可取消)
         → 确认 error:持久标记该图不再上传 → 原图内联 → 重试一次
         → 未确认失败:保持原错误与原缓存
后续请求 / 进程重启 → 读取持久标记,直接内联这张原图
  • mediaPolicy.ts:仅匹配 HTTP 400 的两种形态:Invalid request Error 且类型为 invalid_request_error,或明确的 failed to validate image_url content: blob ID … does not exist。保留现有格式、解码等图片错误处理。
  • mediaResolverService.ts:记录已解析图片与既有缓存键的对应关系;标记复用 image-upload-cache,作用域仍是本地文件、provider、协议、base URL 和账号。保留旧缓存文件和本地原图,不改会话历史或数据库格式。
  • Kimi files adapter:复用上传使用的 endpoint 与凭证查询状态;新上传明确失败也写同一标记。恢复策略增加异步能力及实际请求、原始错误、取消信号的传递。

Behavior Changes and Affected Users

行为 之前 之后 受影响用户 / 边界
正常图片请求 复用上传缓存 保持;零文件状态 GET Kimi CLI、SDK 与嵌入 Core 的客户端
旧缓存引用触发指定 400 继续失败 查询确认失败后,仅该图内联并重试模型一次 仍有本地原图、且 files GET 可用的 Kimi 会话
图片上传明确返回 error 当前请求内联,后续可能重新上传 持久记录该图在当前目标不再上传 同一缓存作用域内不再自动恢复上传;继续用本地原图,其他账号或 provider 不受该标记影响
状态为 pending、查询超时、鉴权失败或其他不确定结果 保留现有失败行为 不永久标坏;未恢复任何图片时保留原模型错误 不把网络故障或中间态认作坏图
其他 400、404、既有图片降级与视频 走原有路径 保持;新增失败恢复仅覆盖图片 不增加 404 恢复或视频回退;原图丢失沿用既有降级,只有裸 ms 引用且无本地映射时跳过查询

必要回归共 143 项通过,覆盖原有媒体能力、上传状态校验与请求生命周期,以及多图去重、只替换坏图、单次重试、后续请求与新 resolver 实例不再上传、账号隔离、不确定查询结果和其他 400 不误伤。无限重试下同一次请求也只检查一批文件,下一次独立请求仍可重新检查尚未确认失败的文件。Core typecheck 通过;变更文件的 type-aware lint 无 error,6 条 warning 均位于未改动的基线代码。

同一恢复实现此前还用合成图片做过真实服务端验证:失败引用触发 400 后查询状态,坏图回退原图并成功请求,下一轮与新进程不再查询或上传。该实测使用旧 Vendor 基线;本 release 移植的验证为上述定向回归和类型检查,未重新跑整套 UI 或 Windows fullstack。

验证命令与文件
pnpm --filter @moonshot-ai/agent-core-v2 test test/agent/media/mediaResolver.test.ts test/llm-adapter/model/modelRequester.test.ts test/agent/llmRequester/llmRequesterService.test.ts
pnpm --filter @moonshot-ai/agent-core-v2 typecheck
pnpm exec oxlint --type-aware <changed TypeScript files>
git diff --check

新增回归放入现有 mediaResolver.test.ts;llmRequesterService.test.ts 仅补接口 stub。release 已有的正常 MCP 截图上传测试与上传状态测试完整保留。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve). No new issue; this is an alternative implementation of the media recovery in fix: resolve behavior regressions from #4630 #4948 for maintainer review.
  • 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. No new command or configuration.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 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-16T17:51:55.944021Z c15b4a6 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.

@changeset-bot

changeset-bot Bot commented Nov 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c15b4a6

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 16, 2026

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

commit: c15b4a6

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