Skip to content
TypeScript

fix(server): truncated file previews no longer end in a replacement character (#24255)

MarcoDotIO5e0d0fd74c4a
diff --git a/apps/server/src/workspace/WorkspaceFileSystem.test.ts b/apps/server/src/workspace/WorkspaceFileSystem.test.ts
index 63461314b50..0f252d600a5 100644
--- a/apps/server/src/workspace/WorkspaceFileSystem.test.ts
+++ b/apps/server/src/workspace/WorkspaceFileSystem.test.ts
@@ -361,6 +361,26 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
}),
);
+ it.effect("drops a multibyte character split by the read limit", () =>
+ Effect.gen(function* () {
+ const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const cwd = yield* makeTempDir;
+ const limit = 1024 * 1024;
+ const euro = new TextEncoder().encode("€");
+ const bytes = new Uint8Array(limit - 1 + euro.length).fill(0x61);
+ bytes.set(euro, limit - 1);
+ yield* fileSystem.writeFile(path.join(cwd, "large.txt"), bytes);
+
+ const result = yield* workspaceFileSystem.readFile({ cwd, relativePath: "large.txt" });
+
+ expect(result.truncated).toBe(true);
+ expect(result.byteLength).toBe(bytes.length);
+ expect(result.contents).toBe("a".repeat(limit - 1));
+ }),
+ );
+
it.effect("rejects binary files without leaking their contents into the error", () =>
Effect.gen(function* () {
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
diff --git a/apps/server/src/workspace/WorkspaceFileSystem.ts b/apps/server/src/workspace/WorkspaceFileSystem.ts
index ca617caac0e..e611d2bd71a 100644
--- a/apps/server/src/workspace/WorkspaceFileSystem.ts
+++ b/apps/server/src/workspace/WorkspaceFileSystem.ts
@@ -426,11 +426,13 @@ export const make = Effect.gen(function* () {
});
}
+ const truncated = stat.size > PROJECT_READ_FILE_MAX_BYTES;
return {
relativePath: target.relativePath,
- contents: new TextDecoder("utf-8").decode(fileBytes),
+ // Streaming decode drops a character the byte limit split, instead of ending in U+FFFD.
+ contents: new TextDecoder("utf-8").decode(fileBytes, { stream: truncated }),
byteLength: stat.size,
- truncated: stat.size > PROJECT_READ_FILE_MAX_BYTES,
+ truncated,
};
}),
(handle) =>