Skip to content

Commit 96796cc

Browse files
committed
computer: Reject empty model attachments
1 parent 28f018d commit 96796cc

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

packages/computer/src/tools/ai.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,41 @@ describe("createAITools filesystem tools", () => {
10901090
expect(reads).toBe(1);
10911091
});
10921092

1093+
it("rejects empty image and PDF attachments", async () => {
1094+
for (const { path, size } of [
1095+
{ path: "/workspace/empty.png", size: 0 },
1096+
{ path: "/workspace/empty.pdf", size: 0 },
1097+
{ path: "/workspace/incomplete.png", size: 10 },
1098+
]) {
1099+
const store = memoryStore({ size });
1100+
store.readChunks = async function* () {};
1101+
const tool = createReadTool({ store });
1102+
1103+
await expect(executeTool(tool, { path })).resolves.toEqual({
1104+
error: `Cannot attach empty file: ${path}`,
1105+
});
1106+
}
1107+
1108+
const tool = createReadTool({ store: memoryStore({ size: 0 }) });
1109+
await expect(
1110+
modelOutput(
1111+
tool,
1112+
{ path: "/workspace/empty.png" },
1113+
{
1114+
kind: "image",
1115+
path: "/workspace/empty.png",
1116+
name: "empty.png",
1117+
mediaType: "image/png",
1118+
sizeBytes: 0,
1119+
data: "",
1120+
},
1121+
),
1122+
).resolves.toEqual({
1123+
type: "error-text",
1124+
value: "Cannot attach empty file: /workspace/empty.png",
1125+
});
1126+
});
1127+
10931128
it("sniffs only a bounded prefix for files without a known extension", async () => {
10941129
const content = new Uint8Array([0x25, 0x50, 0x44, 0x46, 0x2d, ...bytes("body")]);
10951130
const ranges: Array<{ offset: number; length: number | undefined }> = [];

packages/computer/src/tools/fs/read.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export function createReadTool(options: ReadToolOptions): Tool<z.infer<typeof in
140140
}
141141
throw error;
142142
}
143+
if (bytes.byteLength === 0) return { error: `Cannot attach empty file: ${path}` };
143144
result.sizeBytes = bytes.byteLength;
144145
if (bytes.byteLength <= maxModelBytes) result.data = uint8ArrayToBase64(bytes);
145146
return result;
@@ -310,6 +311,9 @@ export function createReadTool(options: ReadToolOptions): Tool<z.infer<typeof in
310311
if (output.data === undefined) {
311312
return { type: "error-text", value: `Could not read captured file bytes: ${output.path}` };
312313
}
314+
if (output.data.length === 0) {
315+
return { type: "error-text", value: `Cannot attach empty file: ${output.path}` };
316+
}
313317
return {
314318
type: "content",
315319
value: [

0 commit comments

Comments
 (0)