From 4b60235a518a0157f282e6fc671b1561e3e04175 Mon Sep 17 00:00:00 2001 From: "opencode-agent[bot]" <219766164+opencode-agent[bot]@users.noreply.github.com> Date: Thu, 29 Oct 2026 08:39:03 +0000 Subject: [PATCH] fix(app): report composer attachment persistence failures through onError (#59546) Co-authored-by: Brendonovich <14191578+Brendonovich@users.noreply.github.com> --- .../src/composer/attachments/attachments.ts | 28 ++-- .../composer-attachment-errors.test.tsx | 140 ++++++++++++++++++ 2 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 packages/app/test-browser/composer-attachment-errors.test.tsx diff --git a/packages/app/src/composer/attachments/attachments.ts b/packages/app/src/composer/attachments/attachments.ts index 5cc33dcc351..f8953eb10a1 100644 --- a/packages/app/src/composer/attachments/attachments.ts +++ b/packages/app/src/composer/attachments/attachments.ts @@ -17,13 +17,15 @@ type PromptTarget = { export type ComposerAttachmentConfig = { picker?: ( options: { defaultPath?: string; multiple?: boolean; accept?: string[] }, - onFile: (file: File) => Promise, + onFile: (file: File) => Promise, ) => Promise directory: () => string destination: () => AttachmentDestination isDialogActive: () => boolean duplicate: () => void + // oxlint-disable-next-line anti-slop/no-unknown-parameters -- a failed attachment is opaque; the host formats it onError: (error: unknown) => void + // oxlint-disable-next-line anti-slop/no-unknown-parameters -- a failed upload is opaque; the host formats it onUploadError: (error: unknown) => void readClipboardImage?: () => Promise getPathForFile?: (file: File) => string @@ -130,7 +132,7 @@ export function createComposerAttachments( .track({ id, filename: file.name, mime, size: file.size }, (report, signal) => destination.upload(file, report, signal), ) - .catch((error: unknown) => { + .catch((error) => { input.onUploadError(error) return undefined @@ -140,13 +142,20 @@ export function createComposerAttachments( if (path) addPath(target, { filename: file.name, mime, path }) } - const addAttachments = async (files: File[], target = capture()) => { - return files.reduce(async (result, file) => { - const previous = await result + // Callers start this without awaiting it, so a failure (for example a blob write on a full disk) + // is reported through onError instead of escaping as an unhandled rejection. + const addAttachments = (files: File[], target = capture(), clipboard = false) => + files + .reduce(async (result, file) => { + const previous = await result - return (await add(file, target)) || previous - }, Promise.resolve(false)) - } + return (await add(file, target, clipboard)) || previous + }, Promise.resolve(false)) + .catch((error) => { + input.onError(error) + + return false + }) const handlePaste = async (event: ClipboardEvent) => { const clipboardData = event.clipboardData @@ -176,7 +185,7 @@ export function createComposerAttachments( if (input.readClipboardImage && !plainText) { const file = await input.readClipboardImage() - if (file && (await add(file, target, true))) return + if (file && (await addAttachments([file], target, true))) return } if (!plainText) return @@ -195,6 +204,7 @@ export function createComposerAttachments( return } + // oxlint-disable-next-line anti-slop/no-runtime-typeof -- probing for a deprecated browser API that some environments lack if (typeof document.execCommand === "function" && document.execCommand("insertText", false, text)) return put() } diff --git a/packages/app/test-browser/composer-attachment-errors.test.tsx b/packages/app/test-browser/composer-attachment-errors.test.tsx new file mode 100644 index 00000000000..e3c84e17da9 --- /dev/null +++ b/packages/app/test-browser/composer-attachment-errors.test.tsx @@ -0,0 +1,140 @@ +import { afterEach, beforeEach, describe, expect, test } from "bun:test" +import { render } from "solid-js/web" +import { createStore } from "solid-js/store" +import { createComposerAttachments } from "@/composer/attachments/attachments" +import type { ComposerPrompt } from "@/composer/types" + +const failure = new Error("ENOSPC: no space left on device") + +const unhandled: unknown[] = [] + +const recordUnhandled: NodeJS.UnhandledRejectionListener = (reason) => { + unhandled.push(reason) +} + +beforeEach(() => { + unhandled.length = 0 + process.on("unhandledRejection", recordUnhandled) +}) + +afterEach(() => { + process.off("unhandledRejection", recordUnhandled) +}) + +function setup(options: { readClipboardImage?: () => Promise } = {}) { + const [store, setStore] = createStore<{ prompt: ComposerPrompt; cursor: number }>({ + prompt: [{ type: "text", content: "", start: 0, end: 0 }], + cursor: 0, + }) + + const errors: unknown[] = [] + + let attachments!: ReturnType + + const dispose = render(() => { + attachments = createComposerAttachments({ + capture: () => ({ + current: () => store.prompt, + cursor: () => store.cursor, + set: (prompt, cursor = store.cursor) => setStore({ prompt, cursor }), + }), + editor: () => document.createElement("div"), + focusEditor() {}, + addPart: () => false, + setDraggingType() {}, + directory: () => "/repo", + destination: () => ({ + input: { image: true, pdf: true }, + local: false, + upload: () => Promise.reject(new Error("inline images must not upload")), + }), + isDialogActive: () => false, + duplicate() {}, + onError: (error) => errors.push(error), + onUploadError: (error) => errors.push(error), + store: () => Promise.reject(failure), + readClipboardImage: options.readClipboardImage, + }) + + return null + }, document.createElement("div")) + + return { attachments, errors, store, dispose } +} + +const image = (name = "image.png") => new File([name], name, { type: "image/png" }) + +function transfer(file?: File) { + const data = new DataTransfer() + + if (file) data.items.add(file) + + return data +} + +// Lets fire-and-forget work settle and any unhandled rejection surface. +const settle = () => new Promise((resolve) => setTimeout(resolve, 10)) + +describe("Composer attachment persistence failures", () => { + test("adding files reports a failed blob write through onError", async () => { + const { attachments, errors, store, dispose } = setup() + + void attachments.addAttachments([image()]) + await settle() + + expect(errors).toEqual([failure]) + expect(unhandled).toEqual([]) + expect(store.prompt.some((part) => part.type === "image")).toBe(false) + dispose() + }) + + test("pasting an image reports a failed blob write through onError", async () => { + const { attachments, errors, dispose } = setup() + const event = new ClipboardEvent("paste", { cancelable: true, clipboardData: transfer(image()) }) + + void attachments.handlePaste(event) + await settle() + + expect(errors).toEqual([failure]) + expect(unhandled).toEqual([]) + dispose() + }) + + test("dropping an image on the document reports a failed blob write through onError", async () => { + const { errors, dispose } = setup() + await Promise.resolve() + const event = new DragEvent("drop", { cancelable: true }) + // happy-dom ignores the dataTransfer init option. + Object.defineProperty(event, "dataTransfer", { value: transfer(image()) }) + + document.dispatchEvent(event) + await settle() + + expect(errors).toEqual([failure]) + expect(unhandled).toEqual([]) + dispose() + }) + + test("pasting a native clipboard image reports a failed blob write through onError", async () => { + const { attachments, errors, dispose } = setup({ readClipboardImage: async () => image() }) + const event = new ClipboardEvent("paste", { cancelable: true, clipboardData: transfer() }) + + void attachments.handlePaste(event) + await settle() + + expect(errors).toEqual([failure]) + expect(unhandled).toEqual([]) + dispose() + }) + + test("a failed batch reports through onError once", async () => { + const { attachments, errors, dispose } = setup() + + const added = await attachments.addAttachments([image("first.png"), image("second.png")]) + + expect(added).toBe(false) + expect(errors).toEqual([failure]) + expect(unhandled).toEqual([]) + dispose() + }) +})