Skip to content

fix(server): correct raw file download responses - #4800

Merged
liruifengv merged 1 commit into
mainfrom
fix/raw-download-error-contract
Nov 10, 2026
Merged

liruifengv merged 1 commit into
mainfrom
fix/raw-download-error-contract

Conversation

@kimi-agent-bot

@kimi-agent-bot kimi-agent-bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

文件预览或下载失败时,/fs:content 与 session :download 会把 HTTP 200 的 JSON 错误 envelope 当成文件内容返回。上传附件与 session media 的另一条响应头路径还会丢弃非 ASCII 文件名。本 PR 修正原始文件响应的错误状态,并让这些路由统一保留 Unicode 文件名。

关联反馈:53825081-cba9-41e9-88a6-2388b086e6fe。该记录包含两个案例;本 PR 处理其中有 HTTP 200 错误 envelope 证据的机制,不把首条无文件名证据的案例强行归为同一根因。

Bug Reproduction Steps

  1. 请求不存在的 /api/v1/fs:content?path=/missing.png 或 /api/v1/sessions/:id/fs/missing.png:download。
  2. 修复前返回 HTTP 200、application/json 和 code=40409,原始文件客户端会把它当成 PNG 内容。
  3. 上传文件名包含中文、全角括号、空格或 emoji 的 PDF/PNG,再请求 /files/:id 或 session media。
  4. 修复前文件字节正确,但 Content-Disposition 只有 inline 或 attachment,原始文件名丢失。

Root Cause

两条原始文件路由复用了普通 JSON RPC 的 reply.send(errEnvelope(...)),没有设置 HTTP 错误状态。普通 JSON RPC 的 HTTP 200 envelope 是既有契约,但原始文件客户端以 HTTP 状态区分文件和错误,因此同一响应在文件路由中会被误读。

buildContentDisposition 只为受限 ASCII 文件名输出 filename,其他名字直接省略。/fs 已在 #4563 中实现 ASCII fallback + RFC 5987 filename*,但上传与 session media 未共享该行为。

Code Changes

 raw file route error
-  HTTP 200 + JSON error envelope
+  HTTP status derived from the protocol error code + the same JSON envelope

 Content-Disposition
-  separate /fs implementation + upload helper that drops non-ASCII names
+  one helper with safe ASCII fallback + RFC 5987 filename*
+  /fs keeps attachment semantics; media keeps inline/attachment selection
  • 新增原始响应错误发送器。它只用于 /fs:content 和 session :download,普通 JSON RPC 仍返回 HTTP 200 envelope。
  • 查询参数预校验、session resume、远程文件系统异常和未映射 I/O 错误也走同一原始错误通道;CONFIG_INVALID 保留 40001/HTTP 400,其他未映射错误返回 HTTP 500,不再落回全局 HTTP 200。
  • 将 40001/40409/40906 等协议码映射为对应的 HTTP 400/404/409;其他协议错误按同一前缀规则处理,无法映射时使用 500。
  • 统一三条文件路由的 Content-Disposition。纯 ASCII 普通文件名保持原行为;引号和反斜杠安全转义;CR/LF 不进入普通 filename;原始名称以 filename* 保留。
  • 当编码后的文件名超过 2048 字符时,省略 filename* 并将 ASCII fallback 限制为 256 字符。先截断再转义,避免长 Unicode 名称使响应头超限或截断引号、反斜杠转义对。
  • OpenAPI 与中英文 server-api.md 同步描述这两条原始下载路由的 400、403、404、409、413、500 JSON 错误响应。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
/fs:content 失败 HTTP 200 JSON 错误体 对应 4xx/500 + 相同错误体 Desktop、Web、读取 host 文件的 SDK 客户端 无;旧行为会把错误当文件
session :download 失败 HTTP 200 JSON 错误体 对应 4xx/500 + 相同错误体 Desktop、Web、会话文件下载客户端 无;旧行为会把错误当文件
普通 JSON RPC HTTP 200 + code 不变 现有控制接口与脚本 不需要
上传与 session media 的 Unicode 文件名 省略 filename ASCII fallback + filename*,原始文件字节不变 上传附件、prompt 媒体预览与下载 无;恢复原始文件名
/fs 成功下载 #4563 的响应头行为 不变,只共用实现 Desktop、Web 不需要
极长文件名 可能产生超限响应头 超过 2048 编码字符时省略 filename*,ASCII fallback 截为 256 字符 上传极长 Unicode 名称的客户端 无;以可下载和可读性优先
OpenAPI 原始下载错误响应 未按 HTTP 状态描述 描述对应 JSON 错误响应 API 使用方 不需要

验证:

  • 最终 HEAD:f8b7c67a7;CI 全绿(test-windows 按条件跳过),Codex completed,未决线程 0。
  • 最终 base:11d9fda8e(已包含 test(server): cover download filename edge cases #4790 的完整文件名测试矩阵,并已 rebase 到当前 main)。
  • 修复前 main + 新测试:7 项失败;修复后文件路由与 OpenAPI 共 130/130 通过。
  • 覆盖 400/404/409、Unicode PDF/PNG 的完整响应头与字节、2000 字 CJK 名称的响应头上限、普通 ASCII 兼容、51 MiB 上传、Range 和远程 environment。
  • kap-server typecheck 通过。
  • kap-server 全量另有 searchService 的 2 项既有失败,与本次文件路由改动无关;不据此宣称全量测试通过。
  • 真实 Desktop / Web AFTER:正常中文 PDF/PNG 4/4、缺失错误 4/4、真实上传与媒体路由 8/8 全部通过。缺失文件均返回 404 + code40409,UI 显示真实错误,不再显示 JSON 或破图;上传 PNG 在两端真实渲染,PDF/PNG 响应字节与源文件一致。
  • 最终 6e1736f76 的快速复核 6/6 通过:正常中文 PNG 两端真实渲染,工作区与外部缺失均返回 404 + code40409,UI 分别显示项目内未找到与文件不存在。随后仅 rebase 到 11d9fda8e,运行时代码未再变化。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • 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 10, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f8b7c67

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 Nov 10, 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-10T09:51:21.914822Z f8b7c67 Manual request
ℹ️ 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

Copy link
Copy Markdown
Collaborator Author

@codex review

@pkg-pr-new

pkg-pr-new Bot commented Nov 10, 2026 •

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

commit: f8b7c67

@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: 06e41566ec

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

@kimi-agent-bot
kimi-agent-bot force-pushed the fix/raw-download-error-contract branch from 06e4156 to 6ff3259 Compare November 10, 2026 09:06
@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

@kimi-agent-bot
kimi-agent-bot force-pushed the fix/raw-download-error-contract branch from 6ff3259 to f496ec7 Compare November 10, 2026 09:08
@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

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

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

Comment thread packages/kap-server/src/routes/fs.ts Resolved
@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

@kimi-agent-bot
kimi-agent-bot force-pushed the fix/raw-download-error-contract branch from f496ec7 to 77d9791 Compare November 10, 2026 09:26
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 77d9791664

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

@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: 77d9791664

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

Comment thread packages/kap-server/src/routes/fs.ts Resolved
@kimi-agent-bot
kimi-agent-bot force-pushed the fix/raw-download-error-contract branch from 77d9791 to 6e1736f Compare November 10, 2026 09:36
@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 6e1736f76a

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

@kimi-agent-bot
kimi-agent-bot force-pushed the fix/raw-download-error-contract branch from 6e1736f to f8b7c67 Compare November 10, 2026 09:49
@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: f8b7c67a7a

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

@liruifengv
liruifengv merged commit 4b4cf3a into main Nov 10, 2026
18 checks passed
@liruifengv
liruifengv deleted the fix/raw-download-error-contract branch November 10, 2026 10:02
This was referenced Nov 11, 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.

3 participants