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: <path>' line instead of rejecting the prompt. The cap now only rejects data: URLs, which have no path to fall back to.
This commit is contained in:
LukeParkerDev 2026-09-18 16:26:06 +10:00
parent dca9dbc8b3
commit 30fb0dc3fb
4 changed files with 90 additions and 4 deletions

View file

@ -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}` })))

View file

@ -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") {

View file

@ -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

View file

@ -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(