Skip to content

Commit f6d1ad5

Browse files
MarcoDotIOclaude
andcommitted
fix(server): truncated file previews no longer end in a replacement character
When a file larger than the 1 MiB read limit was cut inside a multibyte UTF-8 character, readFile decoded the partial bytes as U+FFFD. Decode the truncated read in streaming mode so the split character is dropped, as the shared client preview decoder already does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 96ffd7a commit f6d1ad5

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎apps/server/src/workspace/WorkspaceFileSystem.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,26 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
343343
}),
344344
);
345345

346+
it.effect("drops a multibyte character split by the read limit", () =>
347+
Effect.gen(function* () {
348+
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
349+
const fileSystem = yield* FileSystem.FileSystem;
350+
const path = yield* Path.Path;
351+
const cwd = yield* makeTempDir;
352+
const limit = 1024 * 1024;
353+
const euro = new TextEncoder().encode("€");
354+
const bytes = new Uint8Array(limit - 1 + euro.length).fill(0x61);
355+
bytes.set(euro, limit - 1);
356+
yield* fileSystem.writeFile(path.join(cwd, "large.txt"), bytes);
357+
358+
const result = yield* workspaceFileSystem.readFile({ cwd, relativePath: "large.txt" });
359+
360+
expect(result.truncated).toBe(true);
361+
expect(result.byteLength).toBe(bytes.length);
362+
expect(result.contents).toBe("a".repeat(limit - 1));
363+
}),
364+
);
365+
346366
it.effect("rejects binary files without leaking their contents into the error", () =>
347367
Effect.gen(function* () {
348368
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;

‎apps/server/src/workspace/WorkspaceFileSystem.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,13 @@ export const make = Effect.gen(function* () {
426426
});
427427
}
428428

429+
const truncated = stat.size > PROJECT_READ_FILE_MAX_BYTES;
429430
return {
430431
relativePath: target.relativePath,
431-
contents: new TextDecoder("utf-8").decode(fileBytes),
432+
// Streaming decode drops a character the byte limit split, instead of ending in U+FFFD.
433+
contents: new TextDecoder("utf-8").decode(fileBytes, { stream: truncated }),
432434
byteLength: stat.size,
433-
truncated: stat.size > PROJECT_READ_FILE_MAX_BYTES,
435+
truncated,
434436
};
435437
}),
436438
(handle) =>

0 commit comments

Comments
 (0)