From eb80a7e67076566db74e1f94248842cae29f4a81 Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 4 Sep 2026 15:26:12 +0000 Subject: [PATCH 01/16] fix(agent): bound attachment upload contract --- .../extensions/agent/AgentPanelRoot.test.ts | 20 ++---- .../extensions/agent/AgentPanelRoot.vue | 20 ++---- .../composables/agent/useAttachment.test.ts | 66 ++++++++++++++++++- .../agent/composables/agent/useAttachment.ts | 27 ++++++-- .../services/agent/agentRestClient.test.ts | 4 +- .../agent/services/agent/agentRestClient.ts | 5 +- .../agent/utils/attachableFiles.test.ts | 25 ++++--- .../extensions/agent/utils/attachableFiles.ts | 27 ++++++-- 8 files changed, 136 insertions(+), 58 deletions(-) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 090f81832ad..29d5684a460 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -763,7 +763,7 @@ describe('AgentPanelRoot attach flow', () => { expect(screen.queryByText('movie.mp4')).not.toBeInTheDocument() }) - it('keeps the image limit at 20MB when the server permits more', async () => { + it('uses a larger server limit for non-video attachments', async () => { getServerFeature.mockReturnValue(100 * 1024 * 1024) const uploaded = stubUploadFetch() render(AgentPanelRoot, { global: { plugins: [i18n] } }) @@ -771,15 +771,9 @@ describe('AgentPanelRoot attach flow', () => { const image = fileOfSize('huge.png', MAX_ATTACHMENT_BYTES + 1, 'image/png') dispatchDrag(screen.getByRole('textbox'), 'drop', { files: [image] }) - await nextTick() - expect(uploaded).toEqual([]) - expect(useToastStore().messagesToAdd).toContainEqual( - expect.objectContaining({ - severity: 'warn', - detail: 'huge.png is larger than 20MB' - }) - ) + expect(await screen.findByText('huge.png')).toBeInTheDocument() + await vi.waitFor(() => expect(uploaded).toEqual(['huge.png'])) }) it('uploads a dropped video above 20MB when the server permits it', async () => { @@ -863,13 +857,13 @@ describe('AgentPanelRoot attach flow', () => { await vi.waitFor(() => expect(refresh).toHaveBeenCalled()) }) - it('keeps the 20MB limit for an oversize audio file', async () => { - getServerFeature.mockReturnValue(100 * 1024 * 1024) + it('uses the server limit for audio rejection copy', async () => { + getServerFeature.mockReturnValue(24 * 1024 * 1024) const uploaded = stubUploadFetch() render(AgentPanelRoot, { global: { plugins: [i18n] } }) await nextTick() - const song = fileOfSize('big.mp3', MAX_ATTACHMENT_BYTES + 1, 'audio/mpeg') + const song = fileOfSize('big.mp3', 25 * 1024 * 1024, 'audio/mpeg') dispatchDrag(screen.getByRole('textbox'), 'drop', { files: [song] }) await nextTick() @@ -877,7 +871,7 @@ describe('AgentPanelRoot attach flow', () => { expect(useToastStore().messagesToAdd).toContainEqual( expect.objectContaining({ severity: 'warn', - detail: 'big.mp3 is larger than 20MB' + detail: 'big.mp3 is larger than 24MB' }) ) }) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 5982a439765..72413a9cdee 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -27,11 +27,7 @@ import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' import { useAppMode } from '@/composables/useAppMode' import { MIME_ASSET_INFO } from '@/platform/assets/schemas/mediaAssetSchema' import { assetService } from '@/platform/assets/services/assetService' -import { - fetchDroppedAsset, - getDroppedAsset, - hasVideoType -} from '@/utils/eventUtils' +import { fetchDroppedAsset, getDroppedAsset } from '@/utils/eventUtils' import { useAssetsStore } from '@/stores/assetsStore' import { AGENT_ATTACH_ACCEPT, isAgentAttachable } from './utils/attachableFiles' import { getNodeByLocatorId } from '@/utils/graphTraversalUtil' @@ -820,22 +816,14 @@ function onSelectNodes(): void { const assetsStore = useAssetsStore() const attachment = useAttachment({ - upload: async (file) => { - const uploaded = await rest.uploadImage(file, file.name) + upload: async (file, signal) => { + const uploaded = await rest.uploadImage(file, file.name, signal) // The library caches input assets; without this refresh a just-uploaded // file is neither listed in the Assets tab nor mentionable this session. void assetsStore.inputAssets.loadNew() return { ref: uploaded.name } }, - maxBytes: (file) => { - const serverLimit = api.getServerFeature( - 'max_upload_size', - MAX_ATTACHMENT_BYTES - ) - return hasVideoType(file) - ? serverLimit - : Math.min(MAX_ATTACHMENT_BYTES, serverLimit) - }, + maxBytes: () => api.getServerFeature('max_upload_size', MAX_ATTACHMENT_BYTES), // A rejected file is the user's problem to fix, not an agent failure, so it // must not raise the server-error overlay. onError: (message) => diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts index 69bc2e2e2d0..a5e93ad93b9 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts @@ -71,7 +71,71 @@ describe('useAttachment', () => { await addFiles([movie]) expect(maxBytes).toHaveBeenCalledWith(movie) - expect(upload).toHaveBeenCalledWith(movie) + expect(upload).toHaveBeenCalledWith(movie, expect.any(AbortSignal)) + }) + + it('stages a whole batch before uploading it concurrently', async () => { + const resolvers: Array<(result: { ref: string }) => void> = [] + const upload = vi.fn( + () => + new Promise<{ ref: string }>((resolve) => { + resolvers.push(resolve) + }) + ) + const registry = chipRegistry() + const { addFiles } = useAttachment({ upload, ...registry }) + + const pending = addFiles([ + fileOfSize('a.png', 1), + fileOfSize('b.png', 1), + fileOfSize('c.png', 1) + ]) + + expect(registry.chips.map(({ name }) => name)).toEqual([ + 'a.png', + 'b.png', + 'c.png' + ]) + expect(upload).toHaveBeenCalledTimes(3) + + resolvers[2]({ ref: 'c.png' }) + resolvers[0]({ ref: 'a.png' }) + resolvers[1]({ ref: 'b.png' }) + await pending + expect(registry.chips.map(({ ref }) => ref)).toEqual([ + 'a.png', + 'b.png', + 'c.png' + ]) + }) + + it('aborts and removes an upload that exceeds the configured timeout', async () => { + vi.useFakeTimers() + try { + let signal: AbortSignal | undefined + const upload = vi.fn((_file: File, uploadSignal?: AbortSignal) => { + signal = uploadSignal + return new Promise<{ ref: string }>(() => {}) + }) + const onError = vi.fn() + const registry = chipRegistry() + const { addFiles } = useAttachment({ + upload, + uploadTimeoutMs: 1000, + onError, + ...registry + }) + + const pending = addFiles([fileOfSize('stuck.png', 1)]) + await vi.advanceTimersByTimeAsync(1000) + await pending + + expect(signal?.aborted).toBe(true) + expect(registry.chips).toEqual([]) + expect(onError).toHaveBeenCalledWith('stuck.png could not be uploaded') + } finally { + vi.useRealTimers() + } }) it('rejects against the resolved limit and warns with that limit', async () => { diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.ts index c312408466a..04b57c1d8e2 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.ts @@ -3,6 +3,7 @@ import { hasImageType } from '@/utils/eventUtils' import type { ComposerAttachment } from './useComposer' export const MAX_ATTACHMENT_BYTES = 20 * 1024 * 1024 +const ATTACHMENT_UPLOAD_TIMEOUT_MS = 2 * 60 * 1000 interface UploadResult { ref: string @@ -10,7 +11,8 @@ interface UploadResult { } export interface UseAttachmentOptions { - upload: (file: File) => Promise + upload: (file: File, signal: AbortSignal) => Promise + uploadTimeoutMs?: number maxBytes?: (file: File) => number onError?: (message: string) => void stage: (attachment: ComposerAttachment) => void @@ -45,8 +47,19 @@ export function useAttachment(options: UseAttachmentOptions) { name: file.name, previewUrl: hasImageType(file) ? URL.createObjectURL(file) : undefined }) + const controller = new AbortController() + let timeout: ReturnType | undefined try { - const result = await options.upload(file) + const timedOut = new Promise((_resolve, reject) => { + timeout = setTimeout(() => { + controller.abort() + reject(new Error('Attachment upload timed out')) + }, options.uploadTimeoutMs ?? ATTACHMENT_UPLOAD_TIMEOUT_MS) + }) + const result = await Promise.race([ + options.upload(file, controller.signal), + timedOut + ]) options.update(id, { ref: result.ref, uploading: false }) return true } catch { @@ -55,6 +68,8 @@ export function useAttachment(options: UseAttachmentOptions) { ) options.remove(id) return false + } finally { + clearTimeout(timeout) } } @@ -77,10 +92,10 @@ export function useAttachment(options: UseAttachmentOptions) { } async function addFiles(files: Iterable): Promise { - for (const file of files) { - if (isTooLarge(file)) continue - await uploadStagedFile(stage(file.name), file) - } + const staged = [...files] + .filter((file) => !isTooLarge(file)) + .map((file) => ({ file, id: stage(file.name) })) + await Promise.all(staged.map(({ id, file }) => uploadStagedFile(id, file))) } return { addDeferredFile, addFiles } diff --git a/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts b/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts index 80d8e6a85f5..616b0324a13 100644 --- a/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts @@ -221,12 +221,14 @@ describe('uploadImage multipart', () => { respond(jsonResponse(200, { name: 'x.png', subfolder: '', type: 'input' })) const appendSpy = vi.spyOn(FormData.prototype, 'append') const blob = new Blob(['bytes'], { type: 'image/png' }) - await makeClient().uploadImage(blob, 'x.png') + const controller = new AbortController() + await makeClient().uploadImage(blob, 'x.png', controller.signal) const { route, init } = lastCall() expect(route).toBe('/upload/image') expect(init.method).toBe('POST') expect(init.body).toBeInstanceOf(FormData) + expect(init.signal).toBe(controller.signal) expect(appendSpy).toHaveBeenCalledWith('image', blob, 'x.png') expect(contentType(init)).toBeUndefined() appendSpy.mockRestore() diff --git a/src/workbench/extensions/agent/services/agent/agentRestClient.ts b/src/workbench/extensions/agent/services/agent/agentRestClient.ts index f9ffa5b572e..89de29afdf0 100644 --- a/src/workbench/extensions/agent/services/agent/agentRestClient.ts +++ b/src/workbench/extensions/agent/services/agent/agentRestClient.ts @@ -216,13 +216,14 @@ export function createAgentRestClient() { async function uploadImage( image: Blob, - filename: string + filename: string, + signal?: AbortSignal ): Promise { const form = new FormData() form.append('image', image, filename) return request( '/upload/image', - { method: 'POST', body: form }, + { method: 'POST', body: form, signal }, zUploadImageResult ) } diff --git a/src/workbench/extensions/agent/utils/attachableFiles.test.ts b/src/workbench/extensions/agent/utils/attachableFiles.test.ts index 318e81a5813..a52c75e5344 100644 --- a/src/workbench/extensions/agent/utils/attachableFiles.test.ts +++ b/src/workbench/extensions/agent/utils/attachableFiles.test.ts @@ -1,6 +1,10 @@ import { describe, expect, it } from 'vitest' -import { AGENT_ATTACH_ACCEPT, isAgentAttachable } from './attachableFiles' +import { + AGENT_ATTACH_ACCEPT, + AGENT_ATTACH_EXTENSIONS, + isAgentAttachable +} from './attachableFiles' /* Dragged files often carry no MIME (glb, md) or a generic one, so the predicate must hold with an empty type. */ @@ -33,18 +37,11 @@ describe('isAgentAttachable', () => { expect(isAgentAttachable(fileNamed('noextension'))).toBe(false) }) - it('names every approved extension in the picker accept list', () => { - for (const extension of [ - '.mp4', - '.m4a', - '.mov', - '.mp3', - '.wav', - '.glb', - '.md', - '.txt' - ]) { - expect(AGENT_ATTACH_ACCEPT).toContain(extension) - } + it('derives picker extension tokens from the runtime allowlist', () => { + const pickerExtensions = AGENT_ATTACH_ACCEPT.split(',') + .filter((token) => token.startsWith('.')) + .map((token) => token.slice(1)) + + expect(pickerExtensions).toEqual(AGENT_ATTACH_EXTENSIONS) }) }) diff --git a/src/workbench/extensions/agent/utils/attachableFiles.ts b/src/workbench/extensions/agent/utils/attachableFiles.ts index 6ef973b9e7c..410a8490be9 100644 --- a/src/workbench/extensions/agent/utils/attachableFiles.ts +++ b/src/workbench/extensions/agent/utils/attachableFiles.ts @@ -1,17 +1,34 @@ import type { MediaType } from '@/utils/formatUtil' import { getMediaTypeFromFilename } from '@/utils/formatUtil' -const MEDIA_ATTACHABLE_KINDS = new Set(['image', 'video', 'audio']) +const AGENT_ATTACH_MEDIA_KINDS = [ + 'image', + 'video', + 'audio' +] as const satisfies readonly MediaType[] +const MEDIA_ATTACHABLE_KINDS = new Set(AGENT_ATTACH_MEDIA_KINDS) /* Non-media formats approved for agent attach (Jo, FE-1323); extended as the backend grows support. */ -const EXTRA_ATTACHABLE_EXTENSIONS = new Set(['glb', 'md', 'txt']) +export const AGENT_ATTACH_EXTENSIONS = [ + 'mp4', + 'm4a', + 'mov', + 'mp3', + 'wav', + 'glb', + 'md', + 'txt' +] as const +const ATTACHABLE_EXTENSIONS = new Set(AGENT_ATTACH_EXTENSIONS) /* The OS picker cannot express "any audio plus these extensions" through MIME alone (glb and md have no reliable browser MIME), so the accept list names the extensions explicitly alongside the media wildcards. */ -export const AGENT_ATTACH_ACCEPT = - 'image/*,video/*,audio/*,.mp4,.m4a,.mov,.mp3,.wav,.glb,.md,.txt' +export const AGENT_ATTACH_ACCEPT = [ + ...AGENT_ATTACH_MEDIA_KINDS.map((kind) => `${kind}/*`), + ...AGENT_ATTACH_EXTENSIONS.map((extension) => `.${extension}`) +].join(',') /** * Judged by file NAME, not MIME type: dragged glb/md/txt files carry an empty @@ -21,5 +38,5 @@ export function isAgentAttachable(file: File): boolean { if (MEDIA_ATTACHABLE_KINDS.has(getMediaTypeFromFilename(file.name))) return true const extension = file.name.split('.').pop()?.toLowerCase() ?? '' - return EXTRA_ATTACHABLE_EXTENSIONS.has(extension) + return ATTACHABLE_EXTENSIONS.has(extension) } From 90a5456421b610dc16d6ea96ba739556d220fb7e Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Fri, 11 Sep 2026 09:09:00 -0700 Subject: [PATCH 02/16] test(agent): pin the approved picker extensions literally Deriving the expectation from the same array that builds the accept list made the assertion unfalsifiable: deleting glb left it green. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3936961911 --- .../agent/utils/attachableFiles.test.ts | 26 +++++++++++-------- .../extensions/agent/utils/attachableFiles.ts | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/workbench/extensions/agent/utils/attachableFiles.test.ts b/src/workbench/extensions/agent/utils/attachableFiles.test.ts index a52c75e5344..c8afec6f760 100644 --- a/src/workbench/extensions/agent/utils/attachableFiles.test.ts +++ b/src/workbench/extensions/agent/utils/attachableFiles.test.ts @@ -1,10 +1,6 @@ import { describe, expect, it } from 'vitest' -import { - AGENT_ATTACH_ACCEPT, - AGENT_ATTACH_EXTENSIONS, - isAgentAttachable -} from './attachableFiles' +import { AGENT_ATTACH_ACCEPT, isAgentAttachable } from './attachableFiles' /* Dragged files often carry no MIME (glb, md) or a generic one, so the predicate must hold with an empty type. */ @@ -37,11 +33,19 @@ describe('isAgentAttachable', () => { expect(isAgentAttachable(fileNamed('noextension'))).toBe(false) }) - it('derives picker extension tokens from the runtime allowlist', () => { - const pickerExtensions = AGENT_ATTACH_ACCEPT.split(',') - .filter((token) => token.startsWith('.')) - .map((token) => token.slice(1)) - - expect(pickerExtensions).toEqual(AGENT_ATTACH_EXTENSIONS) + it('names every approved extension in the picker accept list', () => { + expect(AGENT_ATTACH_ACCEPT.split(',')).toEqual([ + 'image/*', + 'video/*', + 'audio/*', + '.mp4', + '.m4a', + '.mov', + '.mp3', + '.wav', + '.glb', + '.md', + '.txt' + ]) }) }) diff --git a/src/workbench/extensions/agent/utils/attachableFiles.ts b/src/workbench/extensions/agent/utils/attachableFiles.ts index 410a8490be9..498e8e7e2e1 100644 --- a/src/workbench/extensions/agent/utils/attachableFiles.ts +++ b/src/workbench/extensions/agent/utils/attachableFiles.ts @@ -10,7 +10,7 @@ const MEDIA_ATTACHABLE_KINDS = new Set(AGENT_ATTACH_MEDIA_KINDS) /* Non-media formats approved for agent attach (Jo, FE-1323); extended as the backend grows support. */ -export const AGENT_ATTACH_EXTENSIONS = [ +const AGENT_ATTACH_EXTENSIONS = [ 'mp4', 'm4a', 'mov', From cce78d7f10efad47d5440c2452c8aadc11e12553 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Fri, 11 Sep 2026 09:09:28 -0700 Subject: [PATCH 03/16] fix(agent): bound the attachment upload contract by size, not wall clock Sizes the upload deadline from the file and a floor throughput instead of a flat two minutes, caps concurrent uploads, validates the advertised server limit behind a client ceiling with multipart headroom, gives the deferred asset fetch its own deadline, and reports the preserved cause. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3936961867 https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3936961857 https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3936961872 https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3936961907 https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3936961892 https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3936961899 https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3935519104 --- .../composables/agent/useAttachment.test.ts | 202 +++++++++++++++++- .../agent/composables/agent/useAttachment.ts | 136 ++++++++++-- 2 files changed, 311 insertions(+), 27 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts index a5e93ad93b9..f2ff996326a 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts @@ -1,7 +1,17 @@ import { describe, expect, it, vi } from 'vitest' +import { reportError } from '@/platform/telemetry/reportError' + import type { ComposerAttachment } from './useComposer' -import { MAX_ATTACHMENT_BYTES, useAttachment } from './useAttachment' +import { + MAX_ATTACHMENT_BYTES, + resolveAttachmentLimit, + useAttachment +} from './useAttachment' + +vi.mock(import('@/platform/telemetry/reportError'), () => ({ + reportError: vi.fn() +})) function fileOfSize(name: string, size: number, type = 'image/png'): File { const file = new File(['x'], name, { type }) @@ -56,7 +66,7 @@ describe('useAttachment', () => { expect(registry.chips).toEqual([]) expect(upload).not.toHaveBeenCalled() expect(onError).toHaveBeenCalledOnce() - expect(onError).toHaveBeenCalledWith('huge.png is larger than 20MB') + expect(onError).toHaveBeenCalledWith('huge.png is larger than 20 MB') }) it('uses the resolved limit for each file', async () => { @@ -153,7 +163,7 @@ describe('useAttachment', () => { expect(registry.chips).toEqual([]) expect(upload).not.toHaveBeenCalled() - expect(onError).toHaveBeenCalledWith('huge.mp4 is larger than 30MB') + expect(onError).toHaveBeenCalledWith('huge.mp4 is larger than 30 MB') }) it('stages an uploading chip immediately, then settles it with the server ref', async () => { @@ -244,11 +254,12 @@ describe('useAttachment', () => { expect(registry.chips).toEqual([]) expect(upload).not.toHaveBeenCalled() - expect(onError).toHaveBeenCalledWith('large.mp4 is larger than 30MB') + expect(onError).toHaveBeenCalledWith('large.mp4 is larger than 30 MB') }) it('removes the chip and surfaces the error when the upload fails', async () => { - const upload = vi.fn().mockRejectedValue(new Error('network down')) + const cause = new Error('network down') + const upload = vi.fn().mockRejectedValue(cause) const onError = vi.fn() const registry = chipRegistry() const { addFiles } = useAttachment({ upload, onError, ...registry }) @@ -257,6 +268,9 @@ describe('useAttachment', () => { expect(registry.chips).toEqual([]) expect(onError).toHaveBeenCalledOnce() + expect(reportError).toHaveBeenCalledWith(cause, { + errorType: 'agent_attachment_upload_failed' + }) }) it('keeps earlier settled chips and continues the batch when one upload fails', async () => { @@ -279,4 +293,182 @@ describe('useAttachment', () => { expect(registry.chips.every((chip) => chip.uploading === false)).toBe(true) expect(onError).toHaveBeenCalledWith('b.png could not be uploaded') }) + + it('keeps a dropped folder from saturating the connection pool', async () => { + const resolvers: Array<(result: { ref: string }) => void> = [] + const upload = vi.fn(() => { + return new Promise<{ ref: string }>((resolve) => { + resolvers.push(resolve) + }) + }) + const registry = chipRegistry() + const { addFiles } = useAttachment({ upload, ...registry }) + + const pending = addFiles( + Array.from({ length: 8 }, (_unused, index) => + fileOfSize(`${index}.png`, 1) + ) + ) + + expect(registry.chips).toHaveLength(8) + expect(upload).toHaveBeenCalledTimes(3) + + resolvers[0]({ ref: '0.png' }) + await vi.waitFor(() => expect(upload).toHaveBeenCalledTimes(4)) + + await vi.waitFor(() => { + for (const resolve of resolvers) resolve({ ref: 'done' }) + expect(upload).toHaveBeenCalledTimes(8) + }) + await pending + }) + + it('scales the upload deadline to the file size', async () => { + vi.useFakeTimers() + try { + let signal: AbortSignal | undefined + const upload = vi.fn((_file: File, uploadSignal: AbortSignal) => { + signal = uploadSignal + return new Promise<{ ref: string }>(() => {}) + }) + const registry = chipRegistry() + const { addFiles } = useAttachment({ + upload, + maxBytes: () => 200 * 1024 * 1024, + ...registry + }) + + const pending = addFiles([fileOfSize('big.png', 100 * 1024 * 1024)]) + await vi.advanceTimersByTimeAsync(5 * 60 * 1000) + + expect(signal?.aborted).toBe(false) + + await vi.advanceTimersByTimeAsync(60 * 60 * 1000) + await pending + expect(signal?.aborted).toBe(true) + } finally { + vi.useRealTimers() + } + }) + + it('aborts a cancelled upload without reporting it as a failure', async () => { + let signal: AbortSignal | undefined + const upload = vi.fn((_file: File, uploadSignal: AbortSignal) => { + signal = uploadSignal + return new Promise<{ ref: string }>((_resolve, reject) => { + uploadSignal.addEventListener('abort', () => + reject(uploadSignal.reason) + ) + }) + }) + const onError = vi.fn() + const registry = chipRegistry() + const { addFiles, cancelUpload } = useAttachment({ + upload, + onError, + ...registry + }) + + const pending = addFiles([fileOfSize('cat.png', 1024)]) + cancelUpload(registry.chips[0].id) + await pending + + expect(signal?.aborted).toBe(true) + expect(onError).not.toHaveBeenCalled() + }) + + it('aborts every in-flight upload when the panel goes away', async () => { + const signals: AbortSignal[] = [] + const upload = vi.fn((_file: File, uploadSignal: AbortSignal) => { + signals.push(uploadSignal) + return new Promise<{ ref: string }>((_resolve, reject) => { + uploadSignal.addEventListener('abort', () => + reject(uploadSignal.reason) + ) + }) + }) + const registry = chipRegistry() + const { addFiles, cancelAllUploads } = useAttachment({ + upload, + ...registry + }) + + const pending = addFiles([fileOfSize('a.png', 1), fileOfSize('b.png', 1)]) + cancelAllUploads() + await pending + + expect(signals.map(({ aborted }) => aborted)).toEqual([true, true]) + }) + + it('removes a deferred chip whose source never resolves', async () => { + vi.useFakeTimers() + try { + const upload = vi.fn() + const onError = vi.fn() + const registry = chipRegistry() + const { addDeferredFile } = useAttachment({ + upload, + onError, + ...registry + }) + + const pending = addDeferredFile( + 'stuck.mp4', + () => new Promise(() => {}) + ) + await vi.advanceTimersByTimeAsync(60 * 1000) + + await expect(pending).resolves.toBeUndefined() + expect(registry.chips).toEqual([]) + expect(upload).not.toHaveBeenCalled() + expect(onError).toHaveBeenCalledWith('stuck.mp4 could not be uploaded') + expect(reportError).toHaveBeenCalledWith(expect.any(Error), { + errorType: 'agent_attachment_fetch_failed' + }) + } finally { + vi.useRealTimers() + } + }) + + it('signals a settled batch once, not once per file', async () => { + const upload = vi.fn(async (file: File) => ({ ref: file.name })) + const onUploaded = vi.fn() + const registry = chipRegistry() + const { addFiles } = useAttachment({ upload, onUploaded, ...registry }) + + await addFiles([ + fileOfSize('a.png', 1), + fileOfSize('b.png', 1), + fileOfSize('c.png', 1) + ]) + + expect(onUploaded).toHaveBeenCalledOnce() + }) +}) + +describe('resolveAttachmentLimit', () => { + const MULTIPART_ENVELOPE_BYTES = 1024 + + it('falls back to the built-in limit for an unusable advertised value', () => { + for (const advertised of [undefined, null, 0, -1, Number.NaN, '100MB']) + expect(resolveAttachmentLimit(advertised)).toBeLessThanOrEqual( + MAX_ATTACHMENT_BYTES + ) + expect(resolveAttachmentLimit(null)).toBe( + MAX_ATTACHMENT_BYTES - MULTIPART_ENVELOPE_BYTES + ) + }) + + it('keeps a client ceiling below an implausible advertised value', () => { + expect(resolveAttachmentLimit(Number.MAX_SAFE_INTEGER)).toBe( + 512 * 1024 * 1024 - MULTIPART_ENVELOPE_BYTES + ) + }) + + it('leaves room for the multipart envelope the server also counts', () => { + const advertised = 100 * 1024 * 1024 + expect(resolveAttachmentLimit(advertised)).toBe( + advertised - MULTIPART_ENVELOPE_BYTES + ) + }) }) diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.ts index 04b57c1d8e2..e5929c584ad 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.ts @@ -1,9 +1,16 @@ import { i18n } from '@/i18n' +import { reportError } from '@/platform/telemetry/reportError' import { hasImageType } from '@/utils/eventUtils' +import { formatSize } from '@/utils/formatUtil' import type { ComposerAttachment } from './useComposer' export const MAX_ATTACHMENT_BYTES = 20 * 1024 * 1024 -const ATTACHMENT_UPLOAD_TIMEOUT_MS = 2 * 60 * 1000 +const MAX_ADVERTISED_ATTACHMENT_BYTES = 512 * 1024 * 1024 +const MULTIPART_ENVELOPE_BYTES = 1024 +const UPLOAD_HANDSHAKE_TIMEOUT_MS = 30 * 1000 +const UPLOAD_FLOOR_BYTES_PER_SECOND = 64 * 1024 +const DEFERRED_FETCH_TIMEOUT_MS = 60 * 1000 +const MAX_CONCURRENT_UPLOADS = 3 interface UploadResult { ref: string @@ -15,14 +22,75 @@ export interface UseAttachmentOptions { uploadTimeoutMs?: number maxBytes?: (file: File) => number onError?: (message: string) => void + onUploaded?: () => void stage: (attachment: ComposerAttachment) => void update: (id: string, patch: Partial) => void remove: (id: string) => void } +/** + * The server advertises `max_upload_size` as an unvalidated flag value, and the + * cap it enforces covers the whole multipart body rather than the file alone. + */ +export function resolveAttachmentLimit(advertised: unknown): number { + const advertisedBytes = + typeof advertised === 'number' && + Number.isFinite(advertised) && + advertised > 0 + ? Math.min(advertised, MAX_ADVERTISED_ATTACHMENT_BYTES) + : MAX_ATTACHMENT_BYTES + return Math.max(0, advertisedBytes - MULTIPART_ENVELOPE_BYTES) +} + +// A fetch upload reports no transfer progress, so the deadline is sized from a +// floor throughput instead of being keyed off a stall. +function uploadDeadlineMs(file: File): number { + return ( + UPLOAD_HANDSHAKE_TIMEOUT_MS + + (file.size / UPLOAD_FLOOR_BYTES_PER_SECOND) * 1000 + ) +} + +async function withDeadline( + work: Promise, + timeoutMs: number, + onExpire?: () => void +): Promise { + let timer: ReturnType | undefined + const expiry = new Promise((_resolve, reject) => { + timer = setTimeout(() => { + onExpire?.() + reject(new Error(`Timed out after ${timeoutMs}ms`)) + }, timeoutMs) + }) + try { + return await Promise.race([work, expiry]) + } finally { + clearTimeout(timer) + } +} + +async function forEachWithLimit( + items: T[], + limit: number, + run: (item: T) => Promise +): Promise { + let next = 0 + const workers = Array.from( + { length: Math.min(limit, items.length) }, + async () => { + while (next < items.length) await run(items[next++]) + } + ) + await Promise.all(workers) +} + let stagedCount = 0 export function useAttachment(options: UseAttachmentOptions) { + const inFlight = new Map() + const cancelled = new Set() + function stage(name: string): string { const id = `upload-${++stagedCount}:${name}` options.stage({ id, name, ref: '', uploading: true }) @@ -36,49 +104,65 @@ export function useAttachment(options: UseAttachmentOptions) { options.onError?.( i18n.global.t('agent.attachmentTooLarge', { name: file.name, - limit: `${maxBytes / 1024 / 1024}MB` + limit: formatSize(maxBytes) }) ) return true } + function failAttachment(id: string, name: string, errorType: string) { + return (cause: unknown): undefined => { + reportError(cause, { errorType }) + options.onError?.(i18n.global.t('agent.attachmentUploadFailed', { name })) + options.remove(id) + return undefined + } + } + async function uploadStagedFile(id: string, file: File): Promise { options.update(id, { name: file.name, previewUrl: hasImageType(file) ? URL.createObjectURL(file) : undefined }) const controller = new AbortController() - let timeout: ReturnType | undefined + inFlight.set(id, controller) try { - const timedOut = new Promise((_resolve, reject) => { - timeout = setTimeout(() => { - controller.abort() - reject(new Error('Attachment upload timed out')) - }, options.uploadTimeoutMs ?? ATTACHMENT_UPLOAD_TIMEOUT_MS) - }) - const result = await Promise.race([ + const result = await withDeadline( options.upload(file, controller.signal), - timedOut - ]) + options.uploadTimeoutMs ?? uploadDeadlineMs(file), + () => controller.abort() + ) options.update(id, { ref: result.ref, uploading: false }) return true - } catch { - options.onError?.( - i18n.global.t('agent.attachmentUploadFailed', { name: file.name }) - ) - options.remove(id) + } catch (cause) { + if (!cancelled.has(id)) + failAttachment(id, file.name, 'agent_attachment_upload_failed')(cause) return false } finally { - clearTimeout(timeout) + inFlight.delete(id) + cancelled.delete(id) } } + function cancelUpload(id: string): void { + const controller = inFlight.get(id) + if (!controller) return + cancelled.add(id) + controller.abort() + } + + function cancelAllUploads(): void { + for (const id of [...inFlight.keys()]) cancelUpload(id) + } + async function addDeferredFile( name: string, resolve: () => Promise ): Promise { const id = stage(name) - const file = await resolve() + const file = await withDeadline(resolve(), DEFERRED_FETCH_TIMEOUT_MS).catch( + failAttachment(id, name, 'agent_attachment_fetch_failed') + ) if (!file) { options.remove(id) return undefined @@ -87,7 +171,7 @@ export function useAttachment(options: UseAttachmentOptions) { options.remove(id) return file } - await uploadStagedFile(id, file) + if (await uploadStagedFile(id, file)) options.onUploaded?.() return file } @@ -95,8 +179,16 @@ export function useAttachment(options: UseAttachmentOptions) { const staged = [...files] .filter((file) => !isTooLarge(file)) .map((file) => ({ file, id: stage(file.name) })) - await Promise.all(staged.map(({ id, file }) => uploadStagedFile(id, file))) + let uploaded = 0 + await forEachWithLimit( + staged, + MAX_CONCURRENT_UPLOADS, + async ({ id, file }) => { + if (await uploadStagedFile(id, file)) uploaded += 1 + } + ) + if (uploaded > 0) options.onUploaded?.() } - return { addDeferredFile, addFiles } + return { addDeferredFile, addFiles, cancelUpload, cancelAllUploads } } From 9a1014e0a3f24b100616325055e5f3ab65969fdf Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Fri, 11 Sep 2026 09:09:53 -0700 Subject: [PATCH 04/16] fix(agent): cancel attachment uploads and refresh assets once per batch Removing a chip or closing the panel now aborts the request instead of leaving an orphan in the input directory, and the library refresh runs once after a batch settles so an overlapping loadNew cannot coalesce a just-committed asset away. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3936961879 https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3936961886 https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3935519098 https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3936961872 --- .../extensions/agent/AgentPanelRoot.test.ts | 53 ++++++++++++++++++- .../extensions/agent/AgentPanelRoot.vue | 16 ++++-- .../agent/components/agent/AgentPanel.vue | 2 + .../agent/components/agent/Composer.vue | 8 ++- 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 51a36cd17e1..4c95597cc68 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -1008,7 +1008,7 @@ describe('AgentPanelRoot attach flow', () => { expect(useToastStore().messagesToAdd).toContainEqual( expect.objectContaining({ severity: 'warn', - detail: 'movie.mp4 is larger than 24MB' + detail: 'movie.mp4 is larger than 24 MB' }) ) expect(screen.queryByText('movie.mp4')).not.toBeInTheDocument() @@ -1108,6 +1108,55 @@ describe('AgentPanelRoot attach flow', () => { await vi.waitFor(() => expect(refresh).toHaveBeenCalled()) }) + it('refreshes the input asset library once for a dropped batch', async () => { + // Overlapping refreshes coalesce into the in-flight query without a + // trailing run, so an asset committing mid-refresh would be dropped. + const uploaded = stubUploadFetch() + renderWithSelectedTarget() + await nextTick() + const refresh = vi + .spyOn(useAssetsStore().inputAssets, 'loadNew') + .mockResolvedValue(undefined) + + dispatchDrag(screen.getByRole('textbox'), 'drop', { + files: [ + new File(['x'], 'a.png', { type: 'image/png' }), + new File(['x'], 'b.png', { type: 'image/png' }), + new File(['x'], 'c.png', { type: 'image/png' }) + ] + }) + + await vi.waitFor(() => expect(uploaded).toHaveLength(3)) + await vi.waitFor(() => expect(refresh).toHaveBeenCalled()) + expect(refresh).toHaveBeenCalledOnce() + }) + + it('aborts an upload when its chip is removed', async () => { + const signals: AbortSignal[] = [] + vi.stubGlobal( + 'fetch', + vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { + if (!String(input).includes('/upload/')) + return json(200, agentThreadList()) + if (init?.signal) signals.push(init.signal) + return new Promise(() => {}) + }) + ) + renderWithSelectedTarget() + await nextTick() + + dispatchDrag(screen.getByRole('textbox'), 'drop', { + files: [new File(['x'], 'cat.png', { type: 'image/png' })] + }) + await vi.waitFor(() => expect(signals).toHaveLength(1)) + + await userEvent.click( + await screen.findByRole('button', { name: i18n.global.t('agent.remove') }) + ) + + expect(signals[0].aborted).toBe(true) + }) + it('uses the server limit for audio rejection copy', async () => { getServerFeature.mockReturnValue(24 * 1024 * 1024) const uploaded = stubUploadFetch() @@ -1122,7 +1171,7 @@ describe('AgentPanelRoot attach flow', () => { expect(useToastStore().messagesToAdd).toContainEqual( expect.objectContaining({ severity: 'warn', - detail: 'big.mp3 is larger than 24MB' + detail: 'big.mp3 is larger than 24 MB' }) ) }) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 7b331379b2b..f5d3f4f07b4 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -57,7 +57,7 @@ import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI' import AgentPanel from './components/agent/AgentPanel.vue' import OnboardingCoach from './components/agent/OnboardingCoach.vue' import { - MAX_ATTACHMENT_BYTES, + resolveAttachmentLimit, useAttachment } from './composables/agent/useAttachment' import type { ActiveTab } from './types/activeTab' @@ -916,12 +916,15 @@ const assetsStore = useAssetsStore() const attachment = useAttachment({ upload: async (file, signal) => { const uploaded = await rest.uploadImage(file, file.name, signal) - // The library caches input assets; without this refresh a just-uploaded - // file is neither listed in the Assets tab nor mentionable this session. - void assetsStore.inputAssets.loadNew() return { ref: uploaded.name } }, - maxBytes: () => api.getServerFeature('max_upload_size', MAX_ATTACHMENT_BYTES), + // The library caches input assets; without this refresh a just-uploaded file + // is neither listed in the Assets tab nor mentionable this session. One run + // per settled batch, because the query queue coalesces an overlapping refresh + // into the in-flight one instead of scheduling a trailing pass. + onUploaded: () => void assetsStore.inputAssets.loadNew(), + maxBytes: () => + resolveAttachmentLimit(api.getServerFeature('max_upload_size')), // A rejected file is the user's problem to fix, not an agent failure, so it // must not raise the server-error overlay. onError: (message) => @@ -931,6 +934,8 @@ const attachment = useAttachment({ remove: (id) => panelRef.value?.removeAttachment(id) }) +onBeforeUnmount(() => attachment.cancelAllUploads()) + function onAttach(): void { exitNodeSelectionMode() useTelemetry()?.trackAgentAttachButtonClicked() @@ -1105,6 +1110,7 @@ function onPanelDrop(event: DragEvent): void { @open-assets="onOpenAssets" @select-nodes="onSelectNodes" @remove-tag="onRemoveSelectionTag" + @remove-attachment="attachment.cancelUpload" @mention-pick="onMentionPick" @request-workflow-references="onRequestWorkflowReferences" @remove-workflow-reference="composerStore.removeWorkflowReference" diff --git a/src/workbench/extensions/agent/components/agent/AgentPanel.vue b/src/workbench/extensions/agent/components/agent/AgentPanel.vue index b980c612fc4..208b22d88dd 100644 --- a/src/workbench/extensions/agent/components/agent/AgentPanel.vue +++ b/src/workbench/extensions/agent/components/agent/AgentPanel.vue @@ -105,6 +105,7 @@ const emit = defineEmits<{ openAssets: [] selectNodes: [] removeTag: [id: string] + removeAttachment: [id: string] mentionPick: [node: SelectedNode] requestWorkflowReferences: [] removeWorkflowReference: [id: string] @@ -384,6 +385,7 @@ defineExpose({ addAttachment, updateAttachment, removeAttachment }) @open-assets="emit('openAssets')" @select-nodes="emit('selectNodes')" @remove-tag="emit('removeTag', $event)" + @remove-attachment="emit('removeAttachment', $event)" @mention-pick="emit('mentionPick', $event)" @request-workflow-references="emit('requestWorkflowReferences')" @remove-workflow-reference="emit('removeWorkflowReference', $event)" diff --git a/src/workbench/extensions/agent/components/agent/Composer.vue b/src/workbench/extensions/agent/components/agent/Composer.vue index 42e9221b0ce..14ed5bf3cd3 100644 --- a/src/workbench/extensions/agent/components/agent/Composer.vue +++ b/src/workbench/extensions/agent/components/agent/Composer.vue @@ -75,6 +75,7 @@ const emit = defineEmits<{ openAssets: [] selectNodes: [] removeTag: [id: string] + removeAttachment: [id: string] mentionPick: [node: SelectedNode] requestWorkflowReferences: [] removeWorkflowReference: [id: string] @@ -232,6 +233,11 @@ function replaceDraft(prompt: PromptSnapshot): void { editorRef.value?.focus() } +function onRemoveAttachment(id: string): void { + composer.removeAttachment(id) + emit('removeAttachment', id) +} + defineExpose({ insert, replaceDraft, @@ -401,7 +407,7 @@ defineExpose({ :name="item.name" :preview-url="item.previewUrl" :uploading="item.uploading" - @remove="composer.removeAttachment(item.id)" + @remove="onRemoveAttachment(item.id)" /> From 7f0743cd03b5023467d446de53727c58ebdde691 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Fri, 11 Sep 2026 09:41:41 -0700 Subject: [PATCH 05/16] fix(agent): cancel queued uploads and chain library refreshes A pooled batch stages every chip before the workers start, so removing a queued file left the worker free to upload it into an orphan. Refreshes from separate batches now chain instead of coalescing into an in-flight run that already snapshotted. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3991180128 https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3991180117 https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3991180140 --- .../extensions/agent/AgentPanelRoot.test.ts | 36 +++++++++++++++++++ .../extensions/agent/AgentPanelRoot.vue | 13 +++++-- .../composables/agent/useAttachment.test.ts | 33 +++++++++++++++++ .../agent/composables/agent/useAttachment.ts | 26 ++++++++++---- .../agent/utils/attachableFiles.test.ts | 31 +++++++++------- 5 files changed, 117 insertions(+), 22 deletions(-) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 4c95597cc68..e93c88de3fc 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -1131,6 +1131,42 @@ describe('AgentPanelRoot attach flow', () => { expect(refresh).toHaveBeenCalledOnce() }) + it('chains input asset refreshes across overlapping batches', async () => { + const uploaded = stubUploadFetch() + renderWithSelectedTarget() + await nextTick() + let releaseFirstRefresh: () => void = () => {} + const refresh = vi + .spyOn(useAssetsStore().inputAssets, 'loadNew') + .mockImplementationOnce( + () => + new Promise((resolve) => { + releaseFirstRefresh = () => resolve(undefined) + }) + ) + .mockResolvedValue(undefined) + + const textbox = screen.getByRole('textbox') + dispatchDrag(textbox, 'drop', { + files: [new File(['x'], 'a.png', { type: 'image/png' })] + }) + await vi.waitFor(() => expect(refresh).toHaveBeenCalledOnce()) + + dispatchDrag(textbox, 'drop', { + files: [new File(['x'], 'b.png', { type: 'image/png' })] + }) + await vi.waitFor(() => expect(uploaded).toEqual(['a.png', 'b.png'])) + await expect( + vi.waitFor(() => expect(refresh).toHaveBeenCalledTimes(2), { + timeout: 250, + interval: 10 + }) + ).rejects.toThrow() + + releaseFirstRefresh() + await vi.waitFor(() => expect(refresh).toHaveBeenCalledTimes(2)) + }) + it('aborts an upload when its chip is removed', async () => { const signals: AbortSignal[] = [] vi.stubGlobal( diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index f5d3f4f07b4..2020087ea0e 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -912,6 +912,7 @@ function onSelectNodes(): void { } const assetsStore = useAssetsStore() +let inputAssetRefresh: Promise = Promise.resolve() const attachment = useAttachment({ upload: async (file, signal) => { @@ -920,9 +921,15 @@ const attachment = useAttachment({ }, // The library caches input assets; without this refresh a just-uploaded file // is neither listed in the Assets tab nor mentionable this session. One run - // per settled batch, because the query queue coalesces an overlapping refresh - // into the in-flight one instead of scheduling a trailing pass. - onUploaded: () => void assetsStore.inputAssets.loadNew(), + // per settled batch, chained, because the query queue coalesces an + // overlapping refresh into the in-flight one instead of scheduling a + // trailing pass. + onUploaded: () => { + inputAssetRefresh = inputAssetRefresh.then( + () => assetsStore.inputAssets.loadNew(), + () => assetsStore.inputAssets.loadNew() + ) + }, maxBytes: () => resolveAttachmentLimit(api.getServerFeature('max_upload_size')), // A rejected file is the user's problem to fix, not an agent failure, so it diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts index f2ff996326a..710c3c2582e 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts @@ -377,6 +377,39 @@ describe('useAttachment', () => { expect(onError).not.toHaveBeenCalled() }) + it('never starts an upload cancelled while it waited in the queue', async () => { + const resolvers: Array<() => void> = [] + const upload = vi.fn((file: File) => { + return new Promise<{ ref: string }>((resolve) => { + resolvers.push(() => resolve({ ref: file.name })) + }) + }) + const onError = vi.fn() + const registry = chipRegistry() + const { addFiles, cancelUpload } = useAttachment({ + upload, + onError, + ...registry + }) + + const pending = addFiles( + Array.from({ length: 4 }, (_unused, index) => + fileOfSize(`${index}.png`, 1) + ) + ) + const queued = registry.chips[3] + cancelUpload(queued.id) + + await vi.waitFor(() => { + for (const resolve of resolvers) resolve() + expect(upload).toHaveBeenCalledTimes(3) + }) + await pending + + expect(upload).toHaveBeenCalledTimes(3) + expect(onError).not.toHaveBeenCalled() + }) + it('aborts every in-flight upload when the panel goes away', async () => { const signals: AbortSignal[] = [] const upload = vi.fn((_file: File, uploadSignal: AbortSignal) => { diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.ts index e5929c584ad..4f10107462f 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.ts @@ -88,15 +88,23 @@ async function forEachWithLimit( let stagedCount = 0 export function useAttachment(options: UseAttachmentOptions) { + const pending = new Set() const inFlight = new Map() const cancelled = new Set() function stage(name: string): string { const id = `upload-${++stagedCount}:${name}` + pending.add(id) options.stage({ id, name, ref: '', uploading: true }) return id } + function settle(id: string): void { + pending.delete(id) + inFlight.delete(id) + cancelled.delete(id) + } + function isTooLarge(file: File): boolean { const maxBytes = options.maxBytes?.(file) ?? MAX_ATTACHMENT_BYTES if (file.size <= maxBytes) return false @@ -120,6 +128,12 @@ export function useAttachment(options: UseAttachmentOptions) { } async function uploadStagedFile(id: string, file: File): Promise { + // A pooled batch stages every chip up front, so a queued upload can be + // cancelled before its worker reaches it. + if (cancelled.has(id)) { + settle(id) + return false + } options.update(id, { name: file.name, previewUrl: hasImageType(file) ? URL.createObjectURL(file) : undefined @@ -139,20 +153,18 @@ export function useAttachment(options: UseAttachmentOptions) { failAttachment(id, file.name, 'agent_attachment_upload_failed')(cause) return false } finally { - inFlight.delete(id) - cancelled.delete(id) + settle(id) } } function cancelUpload(id: string): void { - const controller = inFlight.get(id) - if (!controller) return + if (!pending.has(id)) return cancelled.add(id) - controller.abort() + inFlight.get(id)?.abort() } function cancelAllUploads(): void { - for (const id of [...inFlight.keys()]) cancelUpload(id) + for (const id of [...pending]) cancelUpload(id) } async function addDeferredFile( @@ -164,10 +176,12 @@ export function useAttachment(options: UseAttachmentOptions) { failAttachment(id, name, 'agent_attachment_fetch_failed') ) if (!file) { + settle(id) options.remove(id) return undefined } if (isTooLarge(file)) { + settle(id) options.remove(id) return file } diff --git a/src/workbench/extensions/agent/utils/attachableFiles.test.ts b/src/workbench/extensions/agent/utils/attachableFiles.test.ts index c8afec6f760..171be3479ab 100644 --- a/src/workbench/extensions/agent/utils/attachableFiles.test.ts +++ b/src/workbench/extensions/agent/utils/attachableFiles.test.ts @@ -34,18 +34,23 @@ describe('isAgentAttachable', () => { }) it('names every approved extension in the picker accept list', () => { - expect(AGENT_ATTACH_ACCEPT.split(',')).toEqual([ - 'image/*', - 'video/*', - 'audio/*', - '.mp4', - '.m4a', - '.mov', - '.mp3', - '.wav', - '.glb', - '.md', - '.txt' - ]) + const accepted = AGENT_ATTACH_ACCEPT.split(',') + + expect(new Set(accepted)).toEqual( + new Set([ + 'image/*', + 'video/*', + 'audio/*', + '.mp4', + '.m4a', + '.mov', + '.mp3', + '.wav', + '.glb', + '.md', + '.txt' + ]) + ) + expect(accepted).toHaveLength(11) }) }) From ca007f43bfc52000f8ccbd166fa092f37f410ec7 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Fri, 11 Sep 2026 10:04:44 -0700 Subject: [PATCH 06/16] fix(agent): terminate the asset refresh chain on failure A rejecting loadNew was only observed by the next batch's handler, so a failed refresh with no successor surfaced as an unhandled rejection. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r3991423799 --- .../extensions/agent/AgentPanelRoot.test.ts | 22 +++++++++++++++++++ .../extensions/agent/AgentPanelRoot.vue | 7 +++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index e93c88de3fc..afd1c214e9d 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -1167,6 +1167,28 @@ describe('AgentPanelRoot attach flow', () => { await vi.waitFor(() => expect(refresh).toHaveBeenCalledTimes(2)) }) + it('keeps refreshing later batches after a refresh fails', async () => { + const uploaded = stubUploadFetch() + renderWithSelectedTarget() + await nextTick() + const refresh = vi + .spyOn(useAssetsStore().inputAssets, 'loadNew') + .mockRejectedValueOnce(new Error('asset fetch failed')) + .mockResolvedValue(undefined) + + const textbox = screen.getByRole('textbox') + dispatchDrag(textbox, 'drop', { + files: [new File(['x'], 'a.png', { type: 'image/png' })] + }) + await vi.waitFor(() => expect(refresh).toHaveBeenCalledOnce()) + + dispatchDrag(textbox, 'drop', { + files: [new File(['x'], 'b.png', { type: 'image/png' })] + }) + await vi.waitFor(() => expect(uploaded).toEqual(['a.png', 'b.png'])) + await vi.waitFor(() => expect(refresh).toHaveBeenCalledTimes(2)) + }) + it('aborts an upload when its chip is removed', async () => { const signals: AbortSignal[] = [] vi.stubGlobal( diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 2020087ea0e..4d05361c828 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -925,10 +925,9 @@ const attachment = useAttachment({ // overlapping refresh into the in-flight one instead of scheduling a // trailing pass. onUploaded: () => { - inputAssetRefresh = inputAssetRefresh.then( - () => assetsStore.inputAssets.loadNew(), - () => assetsStore.inputAssets.loadNew() - ) + inputAssetRefresh = inputAssetRefresh + .then(() => assetsStore.inputAssets.loadNew()) + .catch(() => undefined) }, maxBytes: () => resolveAttachmentLimit(api.getServerFeature('max_upload_size')), From 2f3f8c0916981c5e207d2d81cf87c7c980aeabef Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Tue, 15 Sep 2026 21:15:09 -0700 Subject: [PATCH 07/16] test(agent): cover server attachment upload limit --- browser_tests/tests/agent/agentPanel.spec.ts | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/browser_tests/tests/agent/agentPanel.spec.ts b/browser_tests/tests/agent/agentPanel.spec.ts index e68412194d3..88a2fd1c2e2 100644 --- a/browser_tests/tests/agent/agentPanel.spec.ts +++ b/browser_tests/tests/agent/agentPanel.spec.ts @@ -301,6 +301,54 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { .toBeLessThanOrEqual(1) }) + test('uses the server upload limit for Agent file attachments', async ({ + comfyPage + }) => { + const page = comfyPage.page + let uploadCount = 0 + await page.route('**/api/upload/image', async (route) => { + uploadCount += 1 + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ + name: 'uploaded_movie.mp4', + subfolder: '', + type: 'input' + }) + }) + }) + await page.evaluate(() => { + window.app!.api.serverFeatureFlags.value = { + ...window.app!.api.serverFeatureFlags.value, + max_upload_size: 24 * 1024 * 1024 + } + }) + + await page.getByRole('button', { name: OPEN_AGENT_LABEL }).click() + const panel = page.locator('#agent-panel-root') + const fileInput = panel.getByTestId('agent-file-input') + + await fileInput.setInputFiles({ + name: 'movie.mp4', + mimeType: 'video/mp4', + buffer: Buffer.alloc(21 * 1024 * 1024) + }) + await expect(panel.getByText('movie.mp4')).toBeVisible() + await expect.poll(() => uploadCount).toBe(1) + + await fileInput.setInputFiles({ + name: 'too-large.mp4', + mimeType: 'video/mp4', + buffer: Buffer.alloc(25 * 1024 * 1024) + }) + await expect( + page.getByText('too-large.mp4 is larger than 24 MB') + ).toBeVisible() + await expect(panel.getByText('too-large.mp4')).toHaveCount(0) + await expect.poll(() => uploadCount).toBe(1) + }) + test('exits node selection when the active workflow changes', async ({ comfyPage }) => { From 6bd05f55650ad799ff248895eed376104b11421c Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 17 Sep 2026 21:08:36 -0700 Subject: [PATCH 08/16] fix(agent): remove cancelled attachments before persisting drafts Assert exact fallback limits and cover closing the panel during an upload. Addresses https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r4019548129 Addresses https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r4019548137 --- .../tests/agent/agentAttachmentUpload.spec.ts | 62 +++++++++++++++++++ .../composables/agent/useAttachment.test.ts | 34 ++++++---- .../agent/composables/agent/useAttachment.ts | 1 + 3 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 browser_tests/tests/agent/agentAttachmentUpload.spec.ts diff --git a/browser_tests/tests/agent/agentAttachmentUpload.spec.ts b/browser_tests/tests/agent/agentAttachmentUpload.spec.ts new file mode 100644 index 00000000000..bc0c63e48fb --- /dev/null +++ b/browser_tests/tests/agent/agentAttachmentUpload.spec.ts @@ -0,0 +1,62 @@ +import { expect } from '@playwright/test' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } + +import { agentTest as test } from '@e2e/tests/agent/agentPanelMocks' + +test.describe('Agent attachment cancellation', { tag: '@cloud' }, () => { + test('closing the panel removes a pending upload and preserves a sendable draft', async ({ + comfyPage, + agentPanel + }) => { + const page = comfyPage.page + let releaseUpload = () => {} + const uploadHeld = new Promise((resolve) => { + releaseUpload = resolve + }) + await page.route('**/api/upload/image', async (route) => { + await uploadHeld + await route.abort() + }) + + try { + const openButton = page.getByRole('button', { + name: enMessages.agent.askComfyAgent + }) + await openButton.click() + await agentPanel.selectWorkflow() + const panel = page.locator('#agent-panel-root') + const composer = panel.getByRole('textbox', { name: /^Describe ideas/ }) + const send = panel.getByRole('button', { name: 'Send', exact: true }) + await composer.fill('Keep this draft') + await expect(send).toBeEnabled() + + const uploadReceived = page.waitForRequest('**/api/upload/image') + await panel.getByTestId('agent-file-input').setInputFiles({ + name: 'pending.mp4', + mimeType: 'video/mp4', + buffer: Buffer.from('pending upload') + }) + await uploadReceived + await expect( + panel.getByTestId('composer-asset-section').getByText('pending.mp4') + ).toBeVisible() + await expect(send).toBeDisabled() + + await panel + .getByRole('button', { name: enMessages.g.close, exact: true }) + .click() + await expect(panel).toHaveCount(0) + await openButton.click() + + await expect(composer).toHaveText('Keep this draft') + await expect(panel.getByText('pending.mp4', { exact: true })).toHaveCount( + 0 + ) + await expect(send).toBeEnabled() + } finally { + releaseUpload() + await page.unrouteAll({ behavior: 'wait' }) + } + }) +}) diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts index 710c3c2582e..ecb091651c7 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts @@ -371,9 +371,11 @@ describe('useAttachment', () => { const pending = addFiles([fileOfSize('cat.png', 1024)]) cancelUpload(registry.chips[0].id) + expect(registry.chips).toEqual([]) await pending expect(signal?.aborted).toBe(true) + expect(registry.chips).toEqual([]) expect(onError).not.toHaveBeenCalled() }) @@ -399,6 +401,11 @@ describe('useAttachment', () => { ) const queued = registry.chips[3] cancelUpload(queued.id) + expect(registry.chips.map(({ name }) => name)).toEqual([ + '0.png', + '1.png', + '2.png' + ]) await vi.waitFor(() => { for (const resolve of resolvers) resolve() @@ -410,7 +417,7 @@ describe('useAttachment', () => { expect(onError).not.toHaveBeenCalled() }) - it('aborts every in-flight upload when the panel goes away', async () => { + it('removes active and queued uploads synchronously when the panel goes away', async () => { const signals: AbortSignal[] = [] const upload = vi.fn((_file: File, uploadSignal: AbortSignal) => { signals.push(uploadSignal) @@ -426,11 +433,17 @@ describe('useAttachment', () => { ...registry }) - const pending = addFiles([fileOfSize('a.png', 1), fileOfSize('b.png', 1)]) + const pending = addFiles( + ['a.png', 'b.png', 'c.png', 'queued.png'].map((name) => + fileOfSize(name, 1) + ) + ) cancelAllUploads() + expect(registry.chips).toEqual([]) await pending - expect(signals.map(({ aborted }) => aborted)).toEqual([true, true]) + expect(signals.map(({ aborted }) => aborted)).toEqual([true, true, true]) + expect(registry.chips).toEqual([]) }) it('removes a deferred chip whose source never resolves', async () => { @@ -482,15 +495,12 @@ describe('useAttachment', () => { describe('resolveAttachmentLimit', () => { const MULTIPART_ENVELOPE_BYTES = 1024 - it('falls back to the built-in limit for an unusable advertised value', () => { - for (const advertised of [undefined, null, 0, -1, Number.NaN, '100MB']) - expect(resolveAttachmentLimit(advertised)).toBeLessThanOrEqual( - MAX_ATTACHMENT_BYTES - ) - expect(resolveAttachmentLimit(null)).toBe( - MAX_ATTACHMENT_BYTES - MULTIPART_ENVELOPE_BYTES - ) - }) + it.for([undefined, null, 0, -1, Number.NaN, '100MB'])( + 'falls back to 20 MiB minus request overhead for %s', + (advertised) => { + expect(resolveAttachmentLimit(advertised)).toBe(20 * 1024 * 1024 - 1024) + } + ) it('keeps a client ceiling below an implausible advertised value', () => { expect(resolveAttachmentLimit(Number.MAX_SAFE_INTEGER)).toBe( diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.ts index 4f10107462f..39661927301 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.ts @@ -160,6 +160,7 @@ export function useAttachment(options: UseAttachmentOptions) { function cancelUpload(id: string): void { if (!pending.has(id)) return cancelled.add(id) + options.remove(id) inFlight.get(id)?.abort() } From c2aadacf4250a5d7cff7263d7863d7348d791cb1 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 17 Sep 2026 21:42:38 -0700 Subject: [PATCH 09/16] fix(agent): preserve Undo for removed in-flight attachments Keep reference removal reversible while uploads finish. Panel disposal still cancels and retires pending uploads. The existing inline-reference browser regression failed before removing the cancellation listener and passes afterward. --- .../extensions/agent/AgentPanelRoot.test.ts | 28 +++++++++++++++++-- .../extensions/agent/AgentPanelRoot.vue | 1 - 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 40c40b53114..7af35c5fb80 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -1293,30 +1293,52 @@ describe('AgentPanelRoot attach flow', () => { await vi.waitFor(() => expect(refresh).toHaveBeenCalledTimes(2)) }) - it('aborts an upload when its chip is removed', async () => { + it('lets a removed upload finish without reattaching until Undo', async () => { const signals: AbortSignal[] = [] + let finishUpload: (response: Response) => void = () => {} + const upload = new Promise((resolve) => { + finishUpload = resolve + }) vi.stubGlobal( 'fetch', vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { if (!String(input).includes('/upload/')) return json(200, agentThreadList()) if (init?.signal) signals.push(init.signal) - return new Promise(() => {}) + return upload }) ) renderWithSelectedTarget() await nextTick() + const refresh = vi + .spyOn(useAssetsStore().inputAssets, 'loadNew') + .mockResolvedValue(undefined) dispatchDrag(screen.getByRole('textbox'), 'drop', { files: [new File(['x'], 'cat.png', { type: 'image/png' })] }) await vi.waitFor(() => expect(signals).toHaveLength(1)) + const composer = useAgentComposerStore() + const prompt = composer.prompt await userEvent.click( await screen.findByRole('button', { name: i18n.global.t('agent.remove') }) ) - expect(signals[0].aborted).toBe(true) + expect(signals[0].aborted).toBe(false) + finishUpload( + json(200, { name: 'uploaded-cat.png', subfolder: '', type: 'input' }) + ) + await vi.waitFor(() => expect(refresh).toHaveBeenCalledOnce()) + expect(composer.attachments).toEqual([]) + composer.applyEditorPrompt(prompt) + expect(composer.attachments).toEqual([ + expect.objectContaining({ + name: 'cat.png', + ref: 'uploaded-cat.png', + uploading: false + }) + ]) }) it('uses the server limit for audio rejection copy', async () => { diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 39b657154e8..dff88df9f56 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -1150,7 +1150,6 @@ function onPanelDrop(event: DragEvent): void { @open-assets="onOpenAssets" @select-nodes="onSelectNodes" @remove-tag="onRemoveSelectionTag" - @remove-attachment="attachment.cancelUpload" @mention-pick="onMentionPick" @request-workflow-references="onRequestWorkflowReferences" @remove-workflow-reference="composerStore.removeWorkflowReference" From 64071fdf1ff97b6b2d937ccfbc670ebba8574163 Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 18 Sep 2026 00:23:58 -0700 Subject: [PATCH 10/16] fix(agent): share upload slots across attachment sources Addresses https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r4044294518 --- .../composables/agent/useAttachment.test.ts | 38 ++++++++++++++ .../agent/composables/agent/useAttachment.ts | 50 +++++++------------ 2 files changed, 56 insertions(+), 32 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts index ecb091651c7..639eb47de85 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts @@ -323,6 +323,44 @@ describe('useAttachment', () => { await pending }) + it('shares three upload slots across overlapping selections and deferred assets', async () => { + let release = () => {} + const gate = new Promise((resolve) => { + release = resolve + }) + let active = 0 + let peak = 0 + const upload = vi.fn(async (file: File) => { + active += 1 + peak = Math.max(peak, active) + await gate + active -= 1 + return { ref: file.name } + }) + const registry = chipRegistry() + const { addFiles, addDeferredFile } = useAttachment({ upload, ...registry }) + + const first = addFiles(['a', 'b', 'c'].map((name) => fileOfSize(name, 1))) + const second = addFiles(['d', 'e', 'f'].map((name) => fileOfSize(name, 1))) + const deferred = addDeferredFile('g', async () => fileOfSize('g', 1)) + await Promise.resolve() + const startedBeforeRelease = upload.mock.calls.length + release() + await Promise.all([first, second, deferred]) + + expect(startedBeforeRelease).toBe(3) + expect(peak).toBe(3) + expect(registry.chips.map(({ ref }) => ref)).toEqual([ + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g' + ]) + }) + it('scales the upload deadline to the file size', async () => { vi.useFakeTimers() try { diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.ts index 39661927301..3dc01daa8bc 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.ts @@ -70,27 +70,14 @@ async function withDeadline( } } -async function forEachWithLimit( - items: T[], - limit: number, - run: (item: T) => Promise -): Promise { - let next = 0 - const workers = Array.from( - { length: Math.min(limit, items.length) }, - async () => { - while (next < items.length) await run(items[next++]) - } - ) - await Promise.all(workers) -} - let stagedCount = 0 export function useAttachment(options: UseAttachmentOptions) { const pending = new Set() const inFlight = new Map() const cancelled = new Set() + const waiting: Array<() => void> = [] + let activeUploads = 0 function stage(name: string): string { const id = `upload-${++stagedCount}:${name}` @@ -128,19 +115,17 @@ export function useAttachment(options: UseAttachmentOptions) { } async function uploadStagedFile(id: string, file: File): Promise { - // A pooled batch stages every chip up front, so a queued upload can be - // cancelled before its worker reaches it. - if (cancelled.has(id)) { - settle(id) - return false - } - options.update(id, { - name: file.name, - previewUrl: hasImageType(file) ? URL.createObjectURL(file) : undefined - }) - const controller = new AbortController() - inFlight.set(id, controller) + if (activeUploads === MAX_CONCURRENT_UPLOADS) + await new Promise((resolve) => waiting.push(resolve)) + else activeUploads += 1 try { + if (cancelled.has(id)) return false + options.update(id, { + name: file.name, + previewUrl: hasImageType(file) ? URL.createObjectURL(file) : undefined + }) + const controller = new AbortController() + inFlight.set(id, controller) const result = await withDeadline( options.upload(file, controller.signal), options.uploadTimeoutMs ?? uploadDeadlineMs(file), @@ -154,6 +139,9 @@ export function useAttachment(options: UseAttachmentOptions) { return false } finally { settle(id) + const next = waiting.shift() + if (next) next() + else activeUploads -= 1 } } @@ -195,12 +183,10 @@ export function useAttachment(options: UseAttachmentOptions) { .filter((file) => !isTooLarge(file)) .map((file) => ({ file, id: stage(file.name) })) let uploaded = 0 - await forEachWithLimit( - staged, - MAX_CONCURRENT_UPLOADS, - async ({ id, file }) => { + await Promise.all( + staged.map(async ({ id, file }) => { if (await uploadStagedFile(id, file)) uploaded += 1 - } + }) ) if (uploaded > 0) options.onUploaded?.() } From f3e00d3064029692a1b405a8c8e78c3e72e7bd9c Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 18 Sep 2026 00:25:41 -0700 Subject: [PATCH 11/16] fix(agent): let attachment deadlines control upload transport Addresses https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r4044294528 --- .../agent/useAttachment.transport.test.ts | 58 +++++++++++++++++++ .../agent/services/agent/agentRestClient.ts | 9 ++- 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/workbench/extensions/agent/composables/agent/useAttachment.transport.test.ts diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.transport.test.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.transport.test.ts new file mode 100644 index 00000000000..1afb0098670 --- /dev/null +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.transport.test.ts @@ -0,0 +1,58 @@ +import { assert, expect, it, onTestFinished, vi } from 'vitest' + +import { createAgentRestClient } from '../../services/agent/agentRestClient' +import { useAttachment } from './useAttachment' +import type { ComposerAttachment } from './useComposer' + +it('keeps a 4 MiB upload alive past 60 seconds and aborts it at 94 seconds', async () => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + let signal: AbortSignal | null | undefined + vi.stubGlobal( + 'fetch', + vi.fn((_input, init) => { + signal = init?.signal + return new Promise((_resolve, reject) => { + signal?.addEventListener('abort', () => reject(signal?.reason), { + once: true + }) + }) + }) + ) + const client = createAgentRestClient() + const chips = new Map() + const onError = vi.fn() + const { addFiles } = useAttachment({ + upload: async (file, uploadSignal) => { + const result = await client.uploadImage(file, file.name, uploadSignal) + return { ref: result.name } + }, + stage: (chip) => chips.set(chip.id, chip), + update: (id, patch) => { + const chip = chips.get(id) + assert.exists(chip) + Object.assign(chip, patch) + }, + remove: (id) => chips.delete(id), + onError + }) + const file = new File(['image'], 'slow.png', { type: 'image/png' }) + Object.defineProperty(file, 'size', { value: 4 * 1024 * 1024 }) + const pending = addFiles([file]) + + await vi.advanceTimersByTimeAsync(93_999) + assert.exists(signal) + expect(signal.aborted).toBe(false) + expect([...chips.values()]).toMatchObject([ + { name: 'slow.png', uploading: true } + ]) + expect(onError).not.toHaveBeenCalled() + + await vi.advanceTimersByTimeAsync(1) + await pending + expect(signal.aborted).toBe(true) + expect(chips.size).toBe(0) + expect(onError).toHaveBeenCalledWith('slow.png could not be uploaded') +}) diff --git a/src/workbench/extensions/agent/services/agent/agentRestClient.ts b/src/workbench/extensions/agent/services/agent/agentRestClient.ts index 3457324b3b1..dfc9cd46636 100644 --- a/src/workbench/extensions/agent/services/agent/agentRestClient.ts +++ b/src/workbench/extensions/agent/services/agent/agentRestClient.ts @@ -113,7 +113,7 @@ export function createAgentRestClient() { async function request( route: string, - init: RequestInit, + init: Parameters[1], schema: z.ZodType ): Promise { const response = await api.fetchApi(route, init) @@ -243,7 +243,12 @@ export function createAgentRestClient() { form.append('image', image, filename) return request( '/upload/image', - { method: 'POST', body: form, signal }, + { + method: 'POST', + body: form, + signal, + timeoutMs: signal ? null : undefined + }, zUploadImageResult ) } From 24a5b0960177ffaa5d8f940f34b7125554a30f4d Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 18 Sep 2026 00:28:57 -0700 Subject: [PATCH 12/16] fix(agent): silence deferred attachment warnings after cancellation Addresses https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r4044294537 --- .../extensions/agent/AgentPanelRoot.test.ts | 43 +++++++++++++++++++ .../extensions/agent/AgentPanelRoot.vue | 4 +- .../composables/agent/useAttachment.test.ts | 43 +++++++++++++++++-- .../agent/composables/agent/useAttachment.ts | 34 +++++++++------ 4 files changed, 104 insertions(+), 20 deletions(-) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 7af35c5fb80..325a3d30f40 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -1613,6 +1613,49 @@ describe('AgentPanelRoot attach flow', () => { ) }) + it('does not warn after closing the panel during a deferred asset fetch', async () => { + vi.stubGlobal( + 'fetch', + vi.fn((input) => { + const url = String(input) + if (url.includes('/api/view')) return new Promise(() => {}) + if (url.includes('/assets')) + return Promise.resolve( + json(200, { assets: [], total: 0, has_more: false }) + ) + if (url.includes('/workflows')) + return Promise.resolve( + json(200, { data: [], total: 0, has_more: false }) + ) + return Promise.resolve(json(200, agentThreadList())) + }) + ) + const { unmount } = renderWithSelectedTarget() + await nextTick() + const toast = useToastStore() + vi.useFakeTimers() + try { + dispatchDrag(screen.getByRole('textbox'), 'drop', { + types: ['application/x-comfy-asset-info', 'text/uri-list'], + getData: (type: string) => + type === 'application/x-comfy-asset-info' + ? JSON.stringify({ filename: 'gen.png', type: 'input' }) + : 'http://localhost/api/view?filename=gen.png' + }) + await nextTick() + expect( + screen.getByLabelText(i18n.global.t('agent.uploading')) + ).toBeInTheDocument() + + unmount() + await vi.advanceTimersByTimeAsync(60_000) + + expect(toast.messagesToAdd).toEqual([]) + } finally { + vi.useRealTimers() + } + }) + it('attaches dropped assets and leaves other files to the graph loader', async () => { // The graph loader only opens a dropped workflow while the drop is // unclaimed, so the panel must not claim files it cannot attach. diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index dff88df9f56..9056379aca0 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -1062,11 +1062,11 @@ async function attachDroppedAsset(event: DragEvent): Promise { return } - const file = await attachment.addDeferredFile(asset.name, async () => { + const result = await attachment.addDeferredFile(asset.name, async () => { const file = await fetchDroppedAsset(asset) return file && isAgentAttachable(file) ? file : undefined }) - if (!file) + if (result === 'unsupported') toast.add({ severity: 'warn', detail: t('agent.assetNotAttachable'), diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts index 639eb47de85..e97f0e5f90c 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.test.ts @@ -215,7 +215,7 @@ describe('useAttachment', () => { expect(upload).not.toHaveBeenCalled() resolveFile(fileOfSize('dropped.mp4', 1024, 'video/mp4')) - await expect(pending).resolves.toMatchObject({ name: 'dropped.mp4' }) + await expect(pending).resolves.toBe('uploaded') expect(upload).toHaveBeenCalledOnce() expect(registry.chips[0]).toMatchObject({ ref: 'dropped.mp4', @@ -230,7 +230,7 @@ describe('useAttachment', () => { await expect( addDeferredFile('missing.mp4', async () => undefined) - ).resolves.toBeUndefined() + ).resolves.toBe('unsupported') expect(registry.chips).toEqual([]) expect(upload).not.toHaveBeenCalled() @@ -250,7 +250,7 @@ describe('useAttachment', () => { await expect( addDeferredFile('large.mp4', async () => oversized) - ).resolves.toBe(oversized) + ).resolves.toBe('failed') expect(registry.chips).toEqual([]) expect(upload).not.toHaveBeenCalled() @@ -502,7 +502,7 @@ describe('useAttachment', () => { ) await vi.advanceTimersByTimeAsync(60 * 1000) - await expect(pending).resolves.toBeUndefined() + await expect(pending).resolves.toBe('failed') expect(registry.chips).toEqual([]) expect(upload).not.toHaveBeenCalled() expect(onError).toHaveBeenCalledWith('stuck.mp4 could not be uploaded') @@ -514,6 +514,41 @@ describe('useAttachment', () => { } }) + it.for(['rejected', 'oversized', 'unsupported'] as const)( + 'ignores a %s deferred source after cancellation', + async (outcome) => { + let resolveSource: (file: File | undefined) => void = () => {} + let rejectSource: (cause: Error) => void = () => {} + const source = new Promise((resolve, reject) => { + resolveSource = resolve + rejectSource = reject + }) + const upload = vi.fn() + const onError = vi.fn() + const registry = chipRegistry() + const { addDeferredFile, cancelAllUploads } = useAttachment({ + upload, + onError, + ...registry + }) + const pending = addDeferredFile('cancelled.png', () => source) + cancelAllUploads() + const finish = { + rejected: () => rejectSource(new Error('source failed')), + oversized: () => + resolveSource(fileOfSize('cancelled.png', MAX_ATTACHMENT_BYTES + 1)), + unsupported: () => resolveSource(undefined) + } + finish[outcome]() + + await expect(pending).resolves.toBe('cancelled') + expect(registry.chips).toEqual([]) + expect(upload).not.toHaveBeenCalled() + expect(onError).not.toHaveBeenCalled() + expect(reportError).not.toHaveBeenCalled() + } + ) + it('signals a settled batch once, not once per file', async () => { const upload = vi.fn(async (file: File) => ({ ref: file.name })) const onUploaded = vi.fn() diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.ts index 3dc01daa8bc..3ca3a226ece 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.ts @@ -159,23 +159,29 @@ export function useAttachment(options: UseAttachmentOptions) { async function addDeferredFile( name: string, resolve: () => Promise - ): Promise { + ): Promise<'uploaded' | 'unsupported' | 'cancelled' | 'failed'> { const id = stage(name) - const file = await withDeadline(resolve(), DEFERRED_FETCH_TIMEOUT_MS).catch( - failAttachment(id, name, 'agent_attachment_fetch_failed') - ) - if (!file) { - settle(id) - options.remove(id) - return undefined - } - if (isTooLarge(file)) { + try { + const file = await withDeadline(resolve(), DEFERRED_FETCH_TIMEOUT_MS) + if (cancelled.has(id)) return 'cancelled' + if (!file) { + options.remove(id) + return 'unsupported' + } + if (isTooLarge(file)) { + options.remove(id) + return 'failed' + } + if (!(await uploadStagedFile(id, file))) return 'failed' + options.onUploaded?.() + return 'uploaded' + } catch (cause) { + if (cancelled.has(id)) return 'cancelled' + failAttachment(id, name, 'agent_attachment_fetch_failed')(cause) + return 'failed' + } finally { settle(id) - options.remove(id) - return file } - if (await uploadStagedFile(id, file)) options.onUploaded?.() - return file } async function addFiles(files: Iterable): Promise { From 71f4a4af50523a0c33554a886b700a0c3bc73562 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Sun, 20 Sep 2026 10:09:25 -0700 Subject: [PATCH 13/16] test: disambiguate the Agent panel opener Fixes exact-head CI failure:\nhttps://github.com/Comfy-Org/ComfyUI_frontend/actions/runs/35521973703/job/106107993807 --- browser_tests/tests/agent/agentAttachmentUpload.spec.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/browser_tests/tests/agent/agentAttachmentUpload.spec.ts b/browser_tests/tests/agent/agentAttachmentUpload.spec.ts index 1a4524004de..e7e3160f701 100644 --- a/browser_tests/tests/agent/agentAttachmentUpload.spec.ts +++ b/browser_tests/tests/agent/agentAttachmentUpload.spec.ts @@ -21,7 +21,8 @@ test.describe('Agent attachment cancellation', { tag: '@cloud' }, () => { try { const openButton = page.getByRole('button', { - name: enMessages.agent.entryButton + name: enMessages.agent.entryButton, + exact: true }) await openButton.click() await agentPanel.selectWorkflow() From c391a4ded03fc4cb56dceec434bf403a5765eab2 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 21 Sep 2026 15:12:18 -0700 Subject: [PATCH 14/16] fix: honor advertised attachment limit Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16985#discussion_r4059034064 --- .../extensions/agent/AgentPanelRoot.vue | 7 +++-- .../composables/agent/useAttachment.test.ts | 30 +------------------ .../agent/composables/agent/useAttachment.ts | 16 ---------- 3 files changed, 6 insertions(+), 47 deletions(-) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 35ebc452dbe..9f32841d7e8 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -1,6 +1,7 @@