Skip to content

fix: preserve UTF-8 BOM when editing files - #4644

Merged
7Sageer merged 1 commit into
MoonshotAI:mainfrom
7Sageer:fix/edit-utf8-bom
Nov 3, 2026
Merged

7Sageer merged 1 commit into
MoonshotAI:mainfrom
7Sageer:fix/edit-utf8-bom

Conversation

@7Sageer

@7Sageer 7Sageer commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Resolve #3825。

使用 Edit 修改带 UTF-8 BOM 的文件时,本地和远程环境都会在工具返回成功后丢失 BOM。这个 PR 让 Edit 在保留原有匹配和换行行为的同时,逐字节保留 UTF-8 BOM。

本 PR 是范围明确的精简方案:只处理 UTF-8 BOM,不改变 Write 覆盖写、UTF-16/32、换行或 .editorconfig 行为。相比 #4371 的多编码支持和 #3946 的 Write/文件风格推断,本 PR 只覆盖当前已复现的 UTF-8 数据丢失。

Bug Reproduction Steps

  1. 创建内容为 EF BB BF 61 0A 62 0A 的文件,即 UTF-8 BOM + a\nb\n。
  2. 使用 Edit 将 b 替换为 c。
  3. 工具返回 ok: true,但文件变成 61 0A 63 0A,BOM 已消失;预期文件为 EF BB BF 61 0A 63 0A。

本地 HostFileSystem 与远程 RemoteFileSystem 均可复现。无 BOM 文件应保持无 BOM。

Root Cause

Edit 通过 readText({ errors: 'strict' }) 读取文件,TextDecoder 在默认 ignoreBOM: false 下会移除开头的 UTF-8 BOM。后续文本替换和 UTF-8 写回没有恢复 BOM,因此一次普通子串编辑也会静默改变文件头。

本 PR 在现有读改写链路中保留 BOM,属于根本修复,不需要额外探测读取或第二次 RPC。

Code Changes

给内部 readText 增加可选的 preserveBOM,本地和远程文件系统将它传给现有的 decodeTextWithErrors。仅 Edit 启用该选项;其他读取调用的默认行为不变。

 Edit
-  readText({ errors: 'strict' })
+  readText({ errors: 'strict', preserveBOM: true })
   在解码后的文本中执行原有子串替换
   writeText(edited text)

TextDecoder 的 ignoreBOM: true 会保留前导 U+FEFF,现有 UTF-8 写回会将其重新编码为 EF BB BF。替换仍使用原来的子串匹配和 TextModel,因此可见内容的匹配及 CRLF/混合换行行为不变。

测试覆盖:

  • 本地真实文件系统:有 BOM 保持有、无 BOM 保持无,并断言完整字节序列。
  • 远程 subprocess RPC 回环:真实 FileEditService 编辑后 BOM 保持不变。
  • 既有“远程小文件一次读、一次写 RPC”测试继续通过,证明没有增加 RPC。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
本地或远程环境中,Edit 修改带 UTF-8 BOM 的文件 成功返回但 BOM 被删除 成功返回且 BOM 保留 依赖 Edit 删除 BOM 作为编码转换副作用的用户 通过 Bash/iconv 主动转换
Edit 修改无 BOM 的 UTF-8 文件 保持无 BOM 不变 现有本地和远程用户 N/A
非 Edit 的 readText 调用 默认 BOM 解码行为 不变;preserveBOM 默认关闭 配置、skill、plan 等现有读取方 N/A

受影响模块为 agent-core-v2 的 Edit 服务、本地文件系统和远程文件系统。上述三行分别由本地有/无 BOM 字节测试、现有 Edit 回归测试、默认参数未变的实现及现有调用方测试覆盖。

Write 的整体覆盖写行为、UTF-16/32、.editorconfig 和换行处理均不在本 PR 范围内。ACP 客户端提供的已解码文本路径未改;客户端返回的前导 U+FEFF 会原样保留,本 PR 不新增 ACP 字节级编码保证。

验证
  • Edit + 远程文件系统定向测试:45/45 通过。
  • ACP 全量测试:156/156 通过。
  • @moonshot-ai/agent-core-v2 与 @moonshot-ai/acp-server 类型检查通过。
  • 变更文件 oxlint 无错误;remoteFileSystem.ts:285 有一条未改行的既有类型参数警告。
  • check-no-comments 通过,git diff --check 通过。
  • agent-core-v2 全量测试首轮 7895 通过、4 个非相关用例超时;其中 3 个在低并发重跑中通过。
  • 剩余 subagent 超时用例在修改分支单独重跑和未修改基线 worktree 中均以 30 秒超时,属于基线已有问题,不涉及本次文件读写路径。
  • 未执行真实 Windows/SSH 端到端测试;远程行为通过实际 subprocess RPC 回环保测。
  • 已执行 gen-changesets 和 gen-docs;这是恢复既有文件内容的 bug 修复,无新增用法或配置,因此无需文档更新。

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.

Copilot AI balanced review requested due to automatic review settings November 3, 2026 09:28
@changeset-bot

changeset-bot Bot commented Nov 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 75a01ac

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

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 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-03T09:32:11.534771Z 75a01ac 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.

@7Hanrui

7Hanrui commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

@codex review

@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026

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

commit: 75a01ac

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 75a01ac00c

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

@7Sageer
7Sageer merged commit 8985b71 into MoonshotAI:main Nov 3, 2026
17 checks passed
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 5, 2026
Completes the remote half of upstream 8985b71 (MoonshotAI#4644) now that the
remote backend exists.

`RemoteFileSystem.readText` dropped the option's fourth argument, so a
BOM-prefixed file read over an executor decoded with the BOM stripped and an
edit wrote it back without one. The option is forwarded to
`decodeTextWithErrors`, whose fourth parameter is WHATWG `ignoreBOM` — true
keeps the BOM, which is what `preserveBOM` means at the call sites.

The host backend, the interface and `FileEditService` already carried it from
the core port, so this is the last piece and the changeset is unchanged.

Verified: a cross-platform in-process-loopback case edits a BOM-prefixed file
and asserts the BOM survives; dropping the forwarded argument reddens it and
restoring it turns it green. agent-core-v2 typecheck clean apart from the
unrelated associativeMemory files an adjacent workstream has untracked in this
tree; check:no-comments, check:deep-imports and check:boundaries green.

Ledger: MoonshotAI#4644 ported; closed 70 -> 71, remaining 29 -> 28.
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.

Kimi code内置Edit工具剥掉BOM问题

4 participants