Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/app/formatters/summary-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ function formatDiff(diff: string): string {
return formattedLines.join("\n");
}

// Unlabelled .txt attachments are guessed at by the reader; without this mark Notepad
// and Android browsers fall back to the system code page and render UTF-8 as mojibake
const UTF8_BOM = "\uFEFF";

export function prepareCodeFile(
content: string,
filePath: string,
Expand Down Expand Up @@ -386,7 +390,7 @@ export function prepareCodeFile(
: t("tool.file_header.edit", { path: displayPath });
const fullContent = header + processedContent;

const buffer = Buffer.from(fullContent, "utf8");
const buffer = Buffer.from(UTF8_BOM + fullContent, "utf8");
const basename = path.basename(filePath);
const filename = `${operation}_${basename}.txt`;

Expand Down
16 changes: 16 additions & 0 deletions tests/bot/messages/summary-message-formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,22 @@ describe("bot/messages/summary-message-formatter", () => {
expect(oversized).toBeNull();
});

it("starts file payloads with a UTF-8 BOM so readers do not guess the system code page", () => {
const writeFile = prepareCodeFile("const x = 1;", "src/app.ts", "write");
expect(writeFile?.buffer.subarray(0, 3).toString("hex")).toBe("efbbbf");
expect(writeFile?.buffer.subarray(3).toString("utf8")).toBe(
"Write File/Path: src/app.ts\n============================================================\n\nconst x = 1;",
);

const editFile = prepareCodeFile("@@ -1,1 +1,1 @@\n-line1\n+line2", "src/app.ts", "edit");
expect(editFile?.buffer.subarray(0, 3).toString("hex")).toBe("efbbbf");
expect(editFile?.buffer.subarray(3).toString("utf8")).toContain("Edit File/Path: src/app.ts");

const cyrillic = prepareCodeFile('const привет = "мир";', "src/app.ts", "write");
expect(cyrillic?.buffer.subarray(0, 3).toString("hex")).toBe("efbbbf");
expect(cyrillic?.buffer.subarray(3).toString("utf8")).toContain('const привет = "мир";');
});

it("normalizes absolute paths to project-relative form", () => {
const writeText = formatToolInfo({
sessionId: "s1",
Expand Down
Loading