The best way to control Claude Code, Codex and more from any computer or phone
fix(server): finish short reads before returning workspace text (#14989)
Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Alex Southwell committed
Nov 1, 2026 at 20:52 UTC
88483a5cf05669c4fec976c3b87e0d80e12d27a3
2 files changed
+86
-14
apps/server/src/workspace/WorkspaceFileSystem.test.ts
+68
-1
@@ -23,7 +23,23 @@ import { vi } from "vite-plus/test";
23
24
vi.mock("node:fs/promises", async (importOriginal) => {
25
const actual = await importOriginal<typeof NodeFSP>();
26
- return { ...actual, realpath: vi.fn(actual.realpath) };
26
+ return { ...actual, realpath: vi.fn(actual.realpath), open: vi.fn(actual.open) };
27
+});
28
+
29
+const injectShortReads = Effect.fn("injectShortReads")(function* (target: string) {
30
+ const native = yield* Effect.promise(() => vi.importActual<typeof NodeFSP>("node:fs/promises"));
31
+ const open = vi.mocked(NodeFSP.open).mockImplementation(async (path, flags, mode) => {
32
+ const handle = await native.open(path, flags, mode);
33
+ if (path === target) {
34
+ const read = handle.read.bind(handle);
35
+ Object.defineProperty(handle, "read", {
36
+ value: (buffer: Uint8Array, offset: number, length: number, position: number) =>
37
+ read(buffer, offset, Math.min(length, 2), position),
38
+ });
39
+ }
40
+ return handle;
41
+ });
42
+ yield* Effect.addFinalizer(() => Effect.sync(() => open.mockImplementation(native.open)));
43
});
44
45
const UNKNOWN_WORKSPACE_PREFIX = "t3code-unknown-workspace-";
@@ -162,6 +178,57 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
178
});
179
180
describe("readFile", () => {
181
+ it.effect("reads UTF-8 files relative to the workspace root", () =>
182
+ Effect.gen(function* () {
183
+ const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
184
+ const cwd = yield* makeTempDir;
185
+ yield* writeTextFile(cwd, "src/index.ts", "export const answer = 42;\n");
186
+
187
+ const result = yield* workspaceFileSystem.readFile({
188
+ cwd,
189
+ relativePath: "src/index.ts",
190
+ });
191
+
192
+ expect(result).toEqual({
193
+ relativePath: "src/index.ts",
194
+ contents: "export const answer = 42;\n",
195
+ byteLength: 26,
196
+ truncated: false,
197
+ });
198
+ }),
199
+ );
200
+
201
+ it.effect("finishes short reads before decoding UTF-8 text", () =>
202
+ Effect.gen(function* () {
203
+ const files = yield* WorkspaceFileSystem.WorkspaceFileSystem;
204
+ const fs = yield* FileSystem.FileSystem;
205
+ const cwd = yield* makeTempDir;
206
+ const contents = "A café 🐈\n";
207
+ yield* writeTextFile(cwd, "short.txt", contents);
208
+ const target = yield* fs.realPath(`${cwd}/short.txt`);
209
+ yield* injectShortReads(target);
210
+ expect(yield* files.readFile({ cwd, relativePath: "short.txt" })).toEqual({
211
+ relativePath: "short.txt",
212
+ contents,
213
+ byteLength: Buffer.byteLength(contents),
214
+ truncated: false,
215
+ });
216
+ }),
217
+ );
218
+
219
+ it.effect("rejects binary bytes delivered after an initial short read", () =>
220
+ Effect.gen(function* () {
221
+ const files = yield* WorkspaceFileSystem.WorkspaceFileSystem;
222
+ const fs = yield* FileSystem.FileSystem;
223
+ const cwd = yield* makeTempDir;
224
+ yield* writeTextFile(cwd, "short.txt", "ab\0cd");
225
+ const target = yield* fs.realPath(`${cwd}/short.txt`);
226
+ yield* injectShortReads(target);
227
+ const error = yield* files.readFile({ cwd, relativePath: "short.txt" }).pipe(Effect.flip);
228
+ expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceBinaryFileError);
229
+ }),
230
+ );
231
+
232
it.effect("reads host files outside the workspace root by absolute path", () =>
233
Effect.gen(function* () {
234
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
apps/server/src/workspace/WorkspaceFileSystem.ts
+18
-13
@@ -405,19 +405,24 @@ export const make = Effect.gen(function* () {
405
406
const bytesToRead = Math.min(stat.size, PROJECT_READ_FILE_MAX_BYTES);
407
const buffer = Buffer.alloc(bytesToRead);
408
- const { bytesRead } = yield* Effect.tryPromise({
409
- try: () => handle.read(buffer, 0, bytesToRead, 0),
410
- catch: (cause) =>
411
- new WorkspaceFileSystemOperationError({
412
- workspaceRoot: input.cwd,
413
- relativePath: input.relativePath,
414
- resolvedPath: realTargetPath,
415
- operationPath: realTargetPath,
416
- operation: "read",
417
- cause,
418
- }),
419
- });
420
- const fileBytes = buffer.subarray(0, bytesRead);
408
+ let offset = 0;
409
+ while (offset < bytesToRead) {
410
+ const { bytesRead } = yield* Effect.tryPromise({
411
+ try: () => handle.read(buffer, offset, bytesToRead - offset, offset),
412
+ catch: (cause) =>
413
+ new WorkspaceFileSystemOperationError({
414
+ workspaceRoot: input.cwd,
415
+ relativePath: input.relativePath,
416
+ resolvedPath: realTargetPath,
417
+ operationPath: realTargetPath,
418
+ operation: "read",
419
+ cause,
420
+ }),
421
+ });
422
+ if (bytesRead <= 0) break;
423
+ offset += bytesRead;
424
+ }
425
+ const fileBytes = buffer.subarray(0, offset);
426
if (fileBytes.includes(0)) {
427
return yield* new WorkspaceBinaryFileError({
428
workspaceRoot: input.cwd,