11// @effect-diagnostics nodeBuiltinImport:off -- workflow fixtures use files and the fake Claude CLI uses Node streams
2- import { PDFDocument } from "pdf-lib";
32import * as NodeEvents from "node:events";
43import * as NodeFS from "node:fs";
54import * as NodeOS from "node:os";
@@ -1829,13 +1828,25 @@ describe("ClaudeAdapterV2 resume compaction", () => {
18291828
18301829describe("ClaudeAdapterV2 attachments", () => {
18311830 it.effect.each([
1831+ ...["retained-images", "retained-text", "same-message-images", "retained-documents"].map(
1832+ (budget) => ({
1833+ name: budget,
1834+ prompt: "Read this PDF.",
1835+ expectedText: "Read this PDF.",
1836+ command: undefined,
1837+ ultrathink: false,
1838+ resumed: false,
1839+ budget,
1840+ }),
1841+ ),
18321842 {
18331843 name: "ordinary text",
18341844 prompt: "What’s in this image?",
18351845 expectedText: "Ultrathink:\nWhat’s in this image?",
18361846 command: undefined,
18371847 ultrathink: true,
18381848 resumed: false,
1849+ budget: "none",
18391850 },
18401851 {
18411852 name: "mid-sentence skill",
@@ -1844,6 +1855,7 @@ describe("ClaudeAdapterV2 attachments", () => {
18441855 command: "/implement fixes\n\nThen report the result",
18451856 ultrathink: true,
18461857 resumed: false,
1858+ budget: "none",
18471859 },
18481860 {
18491861 name: "prompt-leading skill",
@@ -1852,6 +1864,7 @@ describe("ClaudeAdapterV2 attachments", () => {
18521864 command: undefined,
18531865 ultrathink: false,
18541866 resumed: false,
1867+ budget: "none",
18551868 },
18561869 {
18571870 name: "resumed history with an unknown PDF budget",
@@ -1860,10 +1873,11 @@ describe("ClaudeAdapterV2 attachments", () => {
18601873 command: undefined,
18611874 ultrathink: true,
18621875 resumed: true,
1876+ budget: "none",
18631877 },
18641878 ])(
18651879 "preserves $name with native PDFs, images, and paths on sends and steering",
1866- ({ prompt, expectedText, command, ultrathink, resumed }) =>
1880+ ({ prompt, expectedText, command, ultrathink, resumed, budget }) =>
18671881 Effect.scoped(
18681882 Effect.gen(function* () {
18691883 const fileSystem = yield* FileSystem.FileSystem;
@@ -1977,9 +1991,9 @@ describe("ClaudeAdapterV2 attachments", () => {
19771991 path.join(attachmentsDir, attachmentRelativePath(attachment)!),
19781992 Uint8Array.from([1, 2, 3, 4]),
19791993 );
1980- const pdf = yield* Effect.promise(() => PDFDocument.create());
1981- pdf.addPage();
1982- const pdfBytes = yield* Effect.promise(() => pdf.save() );
1994+ const pdfBytes = Buffer.from(
1995+ "%PDF-1.7\n1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n2 0 obj << /Type /Pages /Count 1 /Kids [3 0 R] >> endobj\n3 0 obj << /Type /Page /Parent 2 0 R >> endobj\n%%EOF",
1996+ );
19831997 yield* fileSystem.writeFile(
19841998 path.join(attachmentsDir, attachmentRelativePath(document)!),
19851999 pdfBytes,
@@ -1999,12 +2013,77 @@ describe("ClaudeAdapterV2 attachments", () => {
19992013 }),
20002014 nativeThreadHasTurns: resumed,
20012015 };
2016+ if (budget !== "none") {
2017+ const providerTurnId = idAllocator.derive.providerTurn({
2018+ driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
2019+ nativeTurnId: `turn:${attemptId}`,
2020+ });
2021+ const imagePath = path.join(attachmentsDir, attachmentRelativePath(attachment)!);
2022+ // The on-disk length deliberately disagrees with sizeBytes.
2023+ yield* fileSystem.writeFile(imagePath, Buffer.alloc(4 * 1024 * 1024));
2024+ if (budget === "retained-documents") {
2025+ yield* fileSystem.writeFile(
2026+ path.join(attachmentsDir, attachmentRelativePath(document)!),
2027+ Buffer.concat([
2028+ pdfBytes.subarray(0, pdfBytes.length - 5),
2029+ Buffer.alloc(10 * 1024 * 1024, 32),
2030+ Buffer.from("%%EOF"),
2031+ ]),
2032+ );
2033+ }
2034+ yield* runtime.startTurn({
2035+ ...turnInput,
2036+ message: {
2037+ ...turnInput.message,
2038+ text: budget === "retained-text" ? "x".repeat(10 * 1024 * 1024) : prompt,
2039+ attachments:
2040+ budget === "retained-text"
2041+ ? []
2042+ : budget === "retained-documents"
2043+ ? [document]
2044+ : [attachment, attachment],
2045+ },
2046+ });
2047+ const largePdf = Buffer.concat([
2048+ pdfBytes.subarray(0, pdfBytes.length - 5),
2049+ Buffer.alloc(
2050+ (budget === "retained-documents" ? 20 : 19) * 1024 * 1024 - pdfBytes.length,
2051+ 32,
2052+ ),
2053+ Buffer.from("%%EOF"),
2054+ ]);
2055+ const documentPath = path.join(attachmentsDir, attachmentRelativePath(document)!);
2056+ yield* fileSystem.writeFile(documentPath, largePdf);
2057+ yield* runtime.steerTurn({
2058+ threadId,
2059+ runId: RunId.make("run-claude-attachments"),
2060+ providerThread,
2061+ providerTurnId,
2062+ message: {
2063+ ...turnInput.message,
2064+ attachments:
2065+ budget === "same-message-images"
2066+ ? [document, attachment, attachment]
2067+ : [document],
2068+ },
2069+ });
2070+ const sent = offeredMessages.at(-1)!.message.content;
2071+ assert.isArray(sent);
2072+ assert.lengthOf(
2073+ typeof sent === "string" ? [] : sent.filter((block) => block.type === "document"),
2074+ 0,
2075+ budget,
2076+ );
2077+ assert.include(JSON.stringify(sent), documentPath);
2078+ return;
2079+ }
20022080 if (!resumed) {
2003- const undelivered = yield* Effect.promise(() => PDFDocument.create());
2004- for (let i = 0; i < 60; i++) undelivered.addPage();
2081+ const undelivered = Buffer.from(
2082+ `%PDF-1.7\n1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n2 0 obj << /Type /Pages /Count 60 >> endobj\n${Array.from({ length: 60 }, (_, i) => `${i + 3} 0 obj << /Type /Page /Parent 2 0 R >> endobj`).join("\n")}\n%%EOF`,
2083+ );
20052084 yield* fileSystem.writeFile(
20062085 path.join(attachmentsDir, attachmentRelativePath(document)!),
2007- yield* Effect.promise(() => undelivered.save()) ,
2086+ undelivered,
20082087 );
20092088 failOpen = true;
20102089 assert.isTrue(Exit.isFailure(yield* runtime.startTurn(turnInput).pipe(Effect.exit)));
@@ -2075,6 +2154,14 @@ describe("ClaudeAdapterV2 attachments", () => {
20752154 "bytes",
20762155 "invalid",
20772156 "invalid-page-tree",
2157+ "object-stream",
2158+ "encrypted",
2159+ "cyclic-tree",
2160+ "unterminated-string",
2161+ "missing-eof",
2162+ "escaped-name",
2163+ "incremental",
2164+ "compressed-bomb",
20782165 "aggregate-pages",
20792166 "retained-pages",
20802167 "delivery-failure",
@@ -2085,14 +2172,11 @@ describe("ClaudeAdapterV2 attachments", () => {
20852172 if (
20862173 ["pages", "aggregate-pages", "retained-pages", "delivery-failure"].includes(scenario)
20872174 ) {
2088- const large = yield* Effect.promise(() => PDFDocument.create());
2089- for (
2090- let i = 0;
2091- i < (scenario === "pages" ? 101 : scenario === "delivery-failure" ? 30 : 60);
2092- i++
2093- )
2094- large.addPage();
2095- yield* fileSystem.writeFile(documentPath, yield* Effect.promise(() => large.save()));
2175+ const pages = scenario === "pages" ? 101 : scenario === "delivery-failure" ? 30 : 60;
2176+ const large = Buffer.from(
2177+ `%PDF-1.7\n1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n2 0 obj << /Type /Pages /Count ${pages} >> endobj\n${Array.from({ length: pages }, (_, i) => `${i + 3} 0 obj << /Type /Page /Parent 2 0 R >> endobj`).join("\n")}\n%%EOF`,
2178+ );
2179+ yield* fileSystem.writeFile(documentPath, large);
20962180 if (scenario === "aggregate-pages") {
20972181 const second = {
20982182 ...document,
@@ -2113,9 +2197,40 @@ describe("ClaudeAdapterV2 attachments", () => {
21132197 const malformedBytes = Buffer.from(
21142198 "%PDF-1.7\n1 0 obj\n<< /Type /Catalog /Pages 42 >>\nendobj\ntrailer\n<< /Root 1 0 R >>\n%%EOF",
21152199 );
2116- const malformed = yield* Effect.promise(() => PDFDocument.load(malformedBytes));
2117- assert.throws(() => malformed.getPageCount());
21182200 yield* fileSystem.writeFile(documentPath, malformedBytes);
2201+ } else if (
2202+ ["object-stream", "encrypted", "cyclic-tree", "compressed-bomb"].includes(scenario)
2203+ ) {
2204+ const suffix =
2205+ scenario === "encrypted"
2206+ ? "/Encrypt 9 0 R"
2207+ : scenario === "cyclic-tree"
2208+ ? "/Count 0 /Kids [2 0 R]"
2209+ : "/Type /ObjStm /Filter /FlateDecode /Length 1000000000";
2210+ yield* fileSystem.writeFile(
2211+ documentPath,
2212+ Buffer.concat([
2213+ pdfBytes.subarray(0, pdfBytes.length - 5),
2214+ Buffer.from(`\n4 0 obj << ${suffix} >> stream\nx\nendstream endobj\n%%EOF`),
2215+ ]),
2216+ );
2217+ } else if (
2218+ ["unterminated-string", "missing-eof", "escaped-name", "incremental"].includes(
2219+ scenario,
2220+ )
2221+ ) {
2222+ const raw = pdfBytes.toString();
2223+ yield* fileSystem.writeFileString(
2224+ documentPath,
2225+ scenario === "missing-eof"
2226+ ? raw.slice(0, -5)
2227+ : scenario === "escaped-name"
2228+ ? raw.replace("/Type /Page ", "/Type /Pa#67e ")
2229+ : raw.replace(
2230+ "%%EOF",
2231+ scenario === "incremental" ? "/Prev 42\n%%EOF" : "(unfinished\n%%EOF",
2232+ ),
2233+ );
21192234 } else if (scenario === "pasted-text") {
21202235 yield* fileSystem.writeFile(documentPath, pdfBytes);
21212236 attachments = [{ ...document, source: { _tag: "pasted-text" } }];
0 commit comments