Skip to content

Commit 5e0d0fd

Browse files
MarcoDotIOclaude
andauthored
fix(server): truncated file previews no longer end in a replacement character (#24255)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 09ac556 commit 5e0d0fd

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
@@ -361,6 +361,26 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
361361
}),
362362
);
363363

364+
it.effect("drops a multibyte character split by the read limit", () =>
365+
Effect.gen(function* () {
366+
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
367+
const fileSystem = yield* FileSystem.FileSystem;
368+
const path = yield* Path.Path;
369+
const cwd = yield* makeTempDir;
370+
const limit = 1024 * 1024;
371+
const euro = new TextEncoder().encode("€");
372+
const bytes = new Uint8Array(limit - 1 + euro.length).fill(0x61);
373+
bytes.set(euro, limit - 1);
374+
yield* fileSystem.writeFile(path.join(cwd, "large.txt"), bytes);
375+
376+
const result = yield* workspaceFileSystem.readFile({ cwd, relativePath: "large.txt" });
377+
378+
expect(result.truncated).toBe(true);
379+
expect(result.byteLength).toBe(bytes.length);
380+
expect(result.contents).toBe("a".repeat(limit - 1));
381+
}),
382+
);
383+
364384
it.effect("rejects binary files without leaking their contents into the error", () =>
365385
Effect.gen(function* () {
366386
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)