From 30fb0dc3fbb6db45b6347b373bb274203fb71cc0 Mon Sep 17 00:00:00 2001 From: LukeParkerDev <10430890+Hona@users.noreply.github.com> Date: Fri, 18 Sep 2026 16:26:06 +1000 Subject: [PATCH] fix(core): admit oversized local attachments as path references A file:// attachment above the inline cap exists on the server, so it is admitted with empty data and rendered to the model as a bare 'Attached file: ' line instead of rejecting the prompt. The cap now only rejects data: URLs, which have no path to fall back to. --- packages/core/src/session/prompt.ts | 14 +++++--- .../core/src/session/runner/to-llm-message.ts | 20 +++++++++++ packages/core/test/session-prompt.test.ts | 26 ++++++++++++++ .../core/test/session-runner-message.test.ts | 34 +++++++++++++++++++ 4 files changed, 90 insertions(+), 4 deletions(-) diff --git a/packages/core/src/session/prompt.ts b/packages/core/src/session/prompt.ts index 80878f354dd..c688fa5301a 100644 --- a/packages/core/src/session/prompt.ts +++ b/packages/core/src/session/prompt.ts @@ -189,11 +189,17 @@ const readFileAttachment = Effect.fn("SessionPrompt.readFileAttachment")(functio } } if (info.type !== "File") return yield* new AttachmentError({ uri, message: `Attachment is not a file: ${uri}` }) + // The file exists on the server, so an oversized one is delivered as a bare reference the + // model reads with tools instead of rejecting the prompt. if (Number(info.size) > MAX_ATTACHMENT_BYTES) - return yield* new AttachmentError({ - uri, - message: `Attachment exceeds the ${MAX_ATTACHMENT_BYTES} byte limit: ${uri}`, - }) + return { + bytes: Buffer.alloc(0), + source: { type: "uri" as const, uri }, + start: undefined, + end: undefined, + name: path.basename(target), + mime: "application/octet-stream", + } const bytes = yield* fs .readFile(target) .pipe(Effect.mapError(() => new AttachmentError({ uri, message: `Unable to read attachment: ${uri}` }))) diff --git a/packages/core/src/session/runner/to-llm-message.ts b/packages/core/src/session/runner/to-llm-message.ts index 313724fa9a4..298940c0044 100644 --- a/packages/core/src/session/runner/to-llm-message.ts +++ b/packages/core/src/session/runner/to-llm-message.ts @@ -72,7 +72,27 @@ const directoryAttachment = (file: FileAttachment): ContentPart => ({ }, }) +const referenceAttachment = (file: FileAttachment, location: string): ContentPart => ({ + type: "text", + text: `\n\n${[ + `Attached file: ${location}`, + file.description === undefined ? undefined : `Description: ${file.description}`, + ] + .filter((line): line is string => line !== undefined) + .join("\n")}`, + metadata: { + attachment: { + source: file.source, + name: file.name, + description: file.description, + }, + }, +}) + const attachmentContent = (file: FileAttachment): ContentPart[] => { + // Oversized files are admitted without their bytes; the model opens them by path. + const location = file.data.length === 0 && file.mime !== "application/x-directory" ? attachmentLocation(file) : undefined + if (location !== undefined) return [referenceAttachment(file, location)] if (file.mime === "text/plain") return [textAttachment(file)] if (file.mime === "application/x-directory") return [directoryAttachment(file)] if (imageMimes.has(file.mime) || file.mime === "application/pdf") { diff --git a/packages/core/test/session-prompt.test.ts b/packages/core/test/session-prompt.test.ts index 96a2cddcb56..96d4222b285 100644 --- a/packages/core/test/session-prompt.test.ts +++ b/packages/core/test/session-prompt.test.ts @@ -431,6 +431,32 @@ describe("Session.prompt", () => { }), ) + it.effect("admits oversized local files as bare references", () => + Effect.gen(function* () { + yield* setup + const session = yield* Session.Service + const directory = yield* tmpdirScoped("opencode-session-prompt-") + const source = path.join(directory.path, "server.log") + yield* Effect.promise(() => Bun.write(source, Buffer.alloc(20 * 1024 * 1024 + 1, 0x61))) + + const message = yield* session.prompt({ + sessionID, + text: "Inspect this log", + files: [{ uri: pathToFileURL(source).href }], + resume: false, + }) + + expect(message.payload.files).toEqual([ + { + data: "", + mime: "application/octet-stream", + source: { type: "uri", uri: pathToFileURL(source).href }, + name: "server.log", + }, + ]) + }), + ) + it.effect("normalizes large image content before validating persisted Base64", () => Effect.gen(function* () { yield* setup diff --git a/packages/core/test/session-runner-message.test.ts b/packages/core/test/session-runner-message.test.ts index 907bbf978c5..4f7ce8ec0d6 100644 --- a/packages/core/test/session-runner-message.test.ts +++ b/packages/core/test/session-runner-message.test.ts @@ -405,6 +405,40 @@ Recent work }) }) + test("renders oversized reference files as a bare path", () => { + const location = path.resolve("/logs/server.log") + const file = FileAttachment.make({ + data: Base64.make(""), + mime: "application/octet-stream", + source: { type: "uri", uri: pathToFileURL(location).href }, + name: "server.log", + }) + const messages = toLLMMessages( + [ + SessionMessage.User.make({ + id: id("user-oversized"), + type: "user", + text: "Review this log", + files: [file], + time: { created }, + }), + ], + model, + ) + + expect(messages[0]).toMatchObject({ + role: "user", + content: [ + { type: "text", text: "Review this log" }, + { + type: "text", + text: `\n\nAttached file: ${location}`, + metadata: { attachment: { source: file.source, name: "server.log" } }, + }, + ], + }) + }) + test("preserves attachment order after the prompt", () => { const directory = path.resolve("/project/src") const messages = toLLMMessages(