Repository navigation
fix: preserve UTF-8 BOM when editing files - #4644
Conversation
🦋 Changeset detectedLatest commit: 75a01ac The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this comment.
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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
commit: |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
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.
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
EF BB BF 61 0A 62 0A的文件,即 UTF-8 BOM +a\nb\n。b替换为c。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 启用该选项;其他读取调用的默认行为不变。TextDecoder的ignoreBOM: true会保留前导U+FEFF,现有 UTF-8 写回会将其重新编码为EF BB BF。替换仍使用原来的子串匹配和TextModel,因此可见内容的匹配及 CRLF/混合换行行为不变。测试覆盖:
FileEditService编辑后 BOM 保持不变。Behavior Changes and Affected Users
iconv主动转换readText调用preserveBOM默认关闭受影响模块为
agent-core-v2的 Edit 服务、本地文件系统和远程文件系统。上述三行分别由本地有/无 BOM 字节测试、现有 Edit 回归测试、默认参数未变的实现及现有调用方测试覆盖。Write 的整体覆盖写行为、UTF-16/32、
.editorconfig和换行处理均不在本 PR 范围内。ACP 客户端提供的已解码文本路径未改;客户端返回的前导U+FEFF会原样保留,本 PR 不新增 ACP 字节级编码保证。验证
@moonshot-ai/agent-core-v2与@moonshot-ai/acp-server类型检查通过。remoteFileSystem.ts:285有一条未改行的既有类型参数警告。check-no-comments通过,git diff --check通过。gen-changesets和gen-docs;这是恢复既有文件内容的 bug 修复,无新增用法或配置,因此无需文档更新。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.