diff --git a/browser_tests/tests/dialogs/modelImportProgressDialog.spec.ts b/browser_tests/tests/dialogs/modelImportProgressDialog.spec.ts index 2c574bf6365..68b61391916 100644 --- a/browser_tests/tests/dialogs/modelImportProgressDialog.spec.ts +++ b/browser_tests/tests/dialogs/modelImportProgressDialog.spec.ts @@ -258,6 +258,73 @@ test.describe('Model import progress toast', { tag: ['@screenshot'] }, () => { }) }) + test('a dismissed unconfirmed cancellation stays hidden from late progress', async ({ + comfyPage + }) => { + const { page } = comfyPage + const taskId = '1396cc07-bab2-4f12-9b54-741f83f9224e' + const assetName = 'unconfirmed-cancel-model.safetensors' + await page.clock.install() + await page.route(`**/tasks/${taskId}`, async (route) => { + if (route.request().method() === 'DELETE') { + await route.fulfill({ status: 204 }) + return + } + await route.fulfill({ + json: { + id: taskId, + idempotency_key: taskId, + task_name: 'task:download_file', + payload: {}, + status: 'running', + create_time: new Date().toISOString(), + update_time: new Date().toISOString() + } satisfies TaskResponse + }) + }) + + await comfyPage.assets.dispatchDownload({ + task_id: taskId, + asset_name: assetName, + bytes_total: 1000, + bytes_downloaded: 200, + progress: 20, + status: 'running' + }) + + const toast = page.getByRole('status').filter({ hasText: assetName }) + await expect(toast).toBeVisible() + await toast.getByRole('button', { name: 'Expand' }).click() + await toast.getByRole('button', { name: 'Cancel Download' }).click() + + const advanceReconciliation = async () => { + const response = page.waitForResponse( + (candidate) => + candidate.url().endsWith(`/tasks/${taskId}`) && + candidate.request().method() === 'GET' + ) + await page.clock.runFor(10_001) + await (await response).finished() + await page.clock.runFor(1) + } + await advanceReconciliation() + await expect( + toast.getByText('Cancelled', { exact: true }).first() + ).toBeVisible() + await toast.getByRole('button', { name: 'Close' }).click() + await expect(toast).toBeHidden() + + await comfyPage.assets.dispatchDownload({ + task_id: taskId, + asset_name: assetName, + bytes_total: 1000, + bytes_downloaded: 750, + progress: 75, + status: 'running' + }) + await expect(toast).toBeHidden() + }) + test('closing a failed download while polling does not reopen its toast', async ({ comfyPage }) => { diff --git a/src/locales/en/main.json b/src/locales/en/main.json index 44f24aceff7..048638979d2 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -5049,6 +5049,7 @@ "failed": "Failed", "finished": "Finished", "pending": "Pending", + "taskUnavailable": "The download task is no longer available.", "progressCount": "{completed} of {total}", "filter": { "all": "All", @@ -5068,6 +5069,7 @@ "exportFailedSingle": "Failed to create ZIP export", "downloadExport": "Download export", "downloadFailed": "Failed to download \"{name}\"", + "unsupportedDownloadUrl": "Unsupported export download URL", "retryDownload": "Retry download" }, "workspace": { diff --git a/src/platform/assets/composables/useUploadModelWizard.test.ts b/src/platform/assets/composables/useUploadModelWizard.test.ts index 43f5b1a4925..40bff22c7a9 100644 --- a/src/platform/assets/composables/useUploadModelWizard.test.ts +++ b/src/platform/assets/composables/useUploadModelWizard.test.ts @@ -19,7 +19,7 @@ vi.mock(import('@/platform/assets/services/assetService'), () => ({ assetService: { getAssetMetadata: vi.fn(), uploadAssetAsync: vi.fn(), - uploadAssetPreviewImage: vi.fn() + uploadAssetFromBase64: vi.fn() } })) @@ -102,6 +102,114 @@ describe('useUploadModelWizard', () => { } }) + it('does not start an upload after reset while preview creation is pending', async () => { + const { assetService } = + await import('@/platform/assets/services/assetService') + let finishPreview!: () => void + vi.mocked(assetService.uploadAssetFromBase64).mockReturnValue( + new Promise((resolve) => { + finishPreview = () => resolve(fromPartial({ id: 'preview-id' })) + }) + ) + + const wizard = setupUploadModelWizard(modelTypes) + wizard.wizardData.value.url = 'https://civitai.com/models/stale-preview' + wizard.wizardData.value.previewImage = 'data:image/png;base64,cHJldmlldw==' + wizard.selectedModelType.value = 'checkpoints' + + const upload = wizard.uploadModel() + await vi.waitFor(() => { + expect(assetService.uploadAssetFromBase64).toHaveBeenCalledOnce() + }) + wizard.resetWizard() + finishPreview() + + await expect(upload).resolves.toBeNull() + expect(assetService.uploadAssetAsync).not.toHaveBeenCalled() + expect(wizard.currentStep.value).toBe(1) + expect(wizard.isUploading.value).toBe(false) + }) + + it('tracks a backend task that resolves after the wizard is reset', async () => { + const { assetService } = + await import('@/platform/assets/services/assetService') + let finishUpload!: (value: AsyncUploadResponse) => void + vi.mocked(assetService.uploadAssetAsync).mockReturnValue( + new Promise((resolve) => { + finishUpload = resolve + }) + ) + + const wizard = setupUploadModelWizard(modelTypes) + wizard.wizardData.value.url = 'https://civitai.com/models/stale-response' + wizard.selectedModelType.value = 'checkpoints' + + const upload = wizard.uploadModel() + await vi.waitFor(() => { + expect(assetService.uploadAssetAsync).toHaveBeenCalledOnce() + }) + wizard.resetWizard() + finishUpload({ + type: 'async', + task: { + task_id: 'task-after-reset', + status: 'created', + message: 'Download queued' + } + }) + + await expect(upload).resolves.toBeNull() + expect(useAssetDownloadStore().downloadList).toEqual([ + expect.objectContaining({ + taskId: 'task-after-reset', + modelType: 'checkpoints' + }) + ]) + expect(wizard.currentStep.value).toBe(1) + }) + + it('does not let a stale failure overwrite a replacement upload', async () => { + const { assetService } = + await import('@/platform/assets/services/assetService') + let failFirstUpload!: (error: Error) => void + vi.mocked(assetService.uploadAssetAsync) + .mockReturnValueOnce( + new Promise((_, reject) => { + failFirstUpload = reject + }) + ) + .mockResolvedValueOnce({ + type: 'async', + task: { + task_id: 'replacement-task', + status: 'created', + message: 'Download queued' + } + }) + + const wizard = setupUploadModelWizard(modelTypes) + wizard.wizardData.value.url = 'https://civitai.com/models/first' + wizard.selectedModelType.value = 'checkpoints' + const staleUpload = wizard.uploadModel() + await vi.waitFor(() => { + expect(assetService.uploadAssetAsync).toHaveBeenCalledOnce() + }) + + wizard.resetWizard() + wizard.wizardData.value.url = 'https://civitai.com/models/replacement' + wizard.selectedModelType.value = 'checkpoints' + await expect(wizard.uploadModel()).resolves.toMatchObject({ + taskId: 'replacement-task', + status: 'processing' + }) + + failFirstUpload(new Error('stale failure')) + await expect(staleUpload).resolves.toBeNull() + expect(wizard.uploadStatus.value).toBe('processing') + expect(wizard.uploadError.value).toBe('') + expect(wizard.currentStep.value).toBe(3) + }) + it('updates uploadStatus to success when async download completes', async () => { const { assetService } = await import('@/platform/assets/services/assetService') diff --git a/src/platform/assets/composables/useUploadModelWizard.ts b/src/platform/assets/composables/useUploadModelWizard.ts index 0e730279247..2de235520a0 100644 --- a/src/platform/assets/composables/useUploadModelWizard.ts +++ b/src/platform/assets/composables/useUploadModelWizard.ts @@ -86,7 +86,15 @@ export function useUploadModelWizard( const uploadTypeMismatch = ref(null) let stopAsyncWatch: (() => void) | undefined let activeAsyncTaskId: string | undefined - onScopeDispose(() => stopAsyncWatch?.()) + let uploadGeneration = 0 + let disposed = false + const isStaleUpload = (generation: number) => + disposed || generation !== uploadGeneration + onScopeDispose(() => { + disposed = true + uploadGeneration++ + stopAsyncWatch?.() + }) const wizardData = ref({ url: '', @@ -310,6 +318,74 @@ export function useUploadModelWizard( return true } + function watchAsyncUpload( + taskId: string, + filename: string, + modelType: string | undefined + ): UploadModelSuccess { + uploadStatus.value = 'processing' + const uploadSuccess: UploadModelSuccess = { + filename, + modelType, + taskId, + status: 'processing' + } + + activeAsyncTaskId = taskId + const watchState: { resolved: boolean; stop?: () => void } = { + resolved: false + } + const stop = watch( + () => + assetDownloadStore.downloadList.find((d) => d.taskId === taskId) + ?.status, + async (status) => { + if (activeAsyncTaskId !== taskId) { + watchState.stop?.() + return + } + if (status === 'completed') { + watchState.resolved = true + uploadStatus.value = 'success' + uploadError.value = '' + await refreshModelCaches() + watchState.stop?.() + if (stopAsyncWatch === watchState.stop) stopAsyncWatch = undefined + if (activeAsyncTaskId === taskId) activeAsyncTaskId = undefined + return + } + if ( + status === 'failed' || + status === 'cancellation_pending' || + status === 'cancelled' + ) { + // Keep watching because an authoritative completion can replace + // provisional failure and cancellation states. + const download = assetDownloadStore.downloadList.find( + (d) => d.taskId === taskId + ) + uploadStatus.value = 'error' + uploadError.value = + status === 'cancelled' || status === 'cancellation_pending' + ? t('electronFileDownload.cancelled') + : download?.error || + t('assetBrowser.downloadFailed', { + name: download?.assetName || '' + }) + } + }, + { immediate: true } + ) + watchState.stop = stop + if (watchState.resolved) { + stop() + stopAsyncWatch = undefined + } else { + stopAsyncWatch = stop + } + return uploadSuccess + } + async function uploadModel(): Promise { if (isUploading.value) return null if (!canUploadModel.value) { @@ -323,6 +399,7 @@ export function useUploadModelWizard( } isUploading.value = true + const generation = ++uploadGeneration stopAsyncWatch?.() stopAsyncWatch = undefined activeAsyncTaskId = undefined @@ -342,6 +419,8 @@ export function useUploadModelWizard( 'model' const previewId = await uploadPreviewImage(filename) + if (isStaleUpload(generation)) return null + const userMetadata = { source: source.type, source_url: wizardData.value.url, @@ -363,73 +442,16 @@ export function useUploadModelWizard( filename ) } - uploadStatus.value = 'processing' - uploadSuccess = { - filename, - modelType, - taskId: result.task.task_id, - status: 'processing' - } + } - const taskId = result.task.task_id - activeAsyncTaskId = taskId - const watchState: { - resolved: boolean - stop?: () => void - } = { resolved: false } - const stop = watch( - () => - assetDownloadStore.downloadList.find((d) => d.taskId === taskId) - ?.status, - async (status) => { - if (activeAsyncTaskId !== taskId) { - watchState.stop?.() - return - } - if (status === 'completed') { - // `completed` is the only status the download store treats as - // immutable, so it is the only one worth stopping on. - watchState.resolved = true - uploadStatus.value = 'success' - uploadError.value = '' - await refreshModelCaches() - watchState.stop?.() - if (stopAsyncWatch === watchState.stop) { - stopAsyncWatch = undefined - } - if (activeAsyncTaskId === taskId) { - activeAsyncTaskId = undefined - } - } else if ( - status === 'failed' || - status === 'cancellation_pending' || - status === 'cancelled' - ) { - // Keep watching: the download store leaves `failed` recheckable - // and lets an authoritative `completed` replace a confirmed - // `cancelled`, so this error screen can still be recovered from. - const download = assetDownloadStore.downloadList.find( - (d) => d.taskId === taskId - ) - uploadStatus.value = 'error' - uploadError.value = - status === 'cancelled' || status === 'cancellation_pending' - ? t('electronFileDownload.cancelled') - : download?.error || - t('assetBrowser.downloadFailed', { - name: download?.assetName || '' - }) - } - }, - { immediate: true } + if (isStaleUpload(generation)) return null + + if (result.type === 'async' && result.task.status !== 'completed') { + uploadSuccess = watchAsyncUpload( + result.task.task_id, + filename, + modelType ) - watchState.stop = stop - if (watchState.resolved) { - stop() - stopAsyncWatch = undefined - } else { - stopAsyncWatch = stop - } } else { if ( requiredModelType && @@ -452,13 +474,16 @@ export function useUploadModelWizard( } currentStep.value = 3 } catch (error) { + if (isStaleUpload(generation)) return null console.error('Failed to upload asset:', error) uploadStatus.value = 'error' uploadError.value = error instanceof Error ? error.message : 'Failed to upload model' currentStep.value = 3 } finally { - isUploading.value = false + if (!isStaleUpload(generation)) { + isUploading.value = false + } } return uploadSuccess } @@ -470,6 +495,7 @@ export function useUploadModelWizard( } function resetWizard() { + uploadGeneration++ stopAsyncWatch?.() stopAsyncWatch = undefined activeAsyncTaskId = undefined diff --git a/src/stores/assetDownloadStore.test.ts b/src/stores/assetDownloadStore.test.ts index de7eef770ae..7b9282d3e63 100644 --- a/src/stores/assetDownloadStore.test.ts +++ b/src/stores/assetDownloadStore.test.ts @@ -10,7 +10,10 @@ import { taskService } from '@/platform/tasks/services/taskService' import type { AssetDownloadWsMessage } from '@/platform/remote/comfyui/execution/types' -import { useAssetDownloadStore } from '@/stores/assetDownloadStore' +import { + isDownloadCancelled, + useAssetDownloadStore +} from '@/stores/assetDownloadStore' type DownloadEventHandler = (e: CustomEvent) => void @@ -150,6 +153,95 @@ describe('useAssetDownloadStore', () => { expect(store.isDownloadedThisSession('asset-456')).toBe(true) }) + it('does not resurrect a dismissed pending cancellation on late events', async () => { + const store = useAssetDownloadStore() + vi.mocked(taskService.cancelTask).mockResolvedValue({ + ok: true, + value: 'cancelling' + }) + store.trackDownload('task-123', 'checkpoints', 'model.safetensors') + dispatch(createDownloadMessage({ status: 'running' })) + + await store.cancelDownload('task-123') + store.clearDismissibleDownloads() + dispatch(createDownloadMessage({ status: 'running', progress: 0.75 })) + + expect(store.downloadList).toHaveLength(0) + + dispatch(createDownloadMessage({ status: 'failed' })) + dispatch(createDownloadMessage({ status: 'running', progress: 0.5 })) + expect(store.downloadList).toHaveLength(0) + + dispatch(createDownloadMessage({ status: 'completed', progress: 1 })) + expect(store.downloadList).toHaveLength(0) + expect(store.lastCompletedDownload).toMatchObject({ + taskId: 'task-123', + modelType: 'checkpoints' + }) + + // A terminal frame consumes the tombstone, so a deliberately reused + // task id is no longer muted for the remainder of the session. + dispatch(createDownloadMessage({ status: 'running', progress: 0.25 })) + expect(store.activeDownloads).toHaveLength(1) + }) + + it('does not resurrect a dismissed unconfirmed cancellation on late events', async () => { + const store = useAssetDownloadStore() + vi.mocked(taskService.cancelTask).mockResolvedValue({ + ok: true, + value: 'cancelling' + }) + vi.mocked(taskService.getTask).mockResolvedValue({ + ok: true, + value: createTaskResponse({ status: 'running', result: undefined }) + }) + store.trackDownload('task-123', 'checkpoints', 'model.safetensors') + dispatch(createDownloadMessage({ status: 'running' })) + + await store.cancelDownload('task-123') + await vi.advanceTimersByTimeAsync(60_000) + expect(store.downloadList[0].status).toBe('cancellation_unconfirmed') + + store.clearDismissibleDownloads() + dispatch(createDownloadMessage({ status: 'running', progress: 0.75 })) + expect(store.downloadList).toHaveLength(0) + + dispatch(createDownloadMessage({ status: 'completed', progress: 1 })) + expect(store.downloadList).toHaveLength(0) + expect(store.lastCompletedDownload).toMatchObject({ + taskId: 'task-123', + modelType: 'checkpoints' + }) + }) + + it('allows explicit tracking to reuse a dismissed task id', async () => { + const store = useAssetDownloadStore() + vi.mocked(taskService.cancelTask).mockResolvedValue({ + ok: true, + value: 'cancelling' + }) + store.trackDownload('task-123', 'checkpoints', 'first.safetensors') + dispatch(createDownloadMessage({ status: 'running' })) + await store.cancelDownload('task-123') + store.clearDismissibleDownloads() + + store.trackDownload('task-123', 'loras', 'second.safetensors') + dispatch( + createDownloadMessage({ + status: 'running', + asset_name: 'second.safetensors' + }) + ) + + expect(store.downloadList).toEqual([ + expect.objectContaining({ + taskId: 'task-123', + modelType: 'loras', + assetName: 'second.safetensors' + }) + ]) + }) + // REGRESSION COVERAGE PM-1302 / PM-1309: cloud's HandleDownloadFile // (download_file.go) can broadcast a terminal `failed` WS message for a // retryable error before asynq decides whether to retry, then @@ -411,6 +503,24 @@ describe('useAssetDownloadStore', () => { expect(taskService.getTask).toHaveBeenCalledWith('task-123') }) + it('shows pending cancellation when a DELETE 404 reread is still running', async () => { + const store = useAssetDownloadStore() + vi.mocked(taskService.cancelTask).mockResolvedValue({ + ok: true, + value: 'missing' + }) + vi.mocked(taskService.getTask).mockResolvedValue({ + ok: true, + value: createTaskResponse({ status: 'running', result: undefined }) + }) + dispatch(createDownloadMessage({ status: 'running' })) + + await store.cancelDownload('task-123') + + expect(store.downloadList[0].status).toBe('cancellation_pending') + expect(store.hasPendingCancellation).toBe(true) + }) + it('allows an authoritative completion to replace confirmed cancellation', async () => { const store = useAssetDownloadStore() vi.mocked(taskService.cancelTask).mockResolvedValue({ @@ -528,7 +638,7 @@ describe('useAssetDownloadStore', () => { expect(store.lastCompletedDownload?.modelType).toBe('checkpoints') }) - it('reconciles an authoritative failure after local cancellation and keeps polling', async () => { + it('stops polling after an authoritative failure during cancellation', async () => { const store = useAssetDownloadStore() store.trackDownload('task-123', 'checkpoints', 'model.safetensors') vi.mocked(taskService.cancelTask).mockResolvedValue({ @@ -557,12 +667,12 @@ describe('useAssetDownloadStore', () => { error: 'Cancellation failed' }) - await vi.advanceTimersByTimeAsync(10_000) + await vi.advanceTimersByTimeAsync(60_000) - expect(taskService.getTask).toHaveBeenCalledTimes(2) + expect(taskService.getTask).toHaveBeenCalledTimes(1) expect(store.finishedDownloads[0]).toMatchObject({ - status: 'completed', - assetId: 'asset-456' + status: 'failed', + error: 'Cancellation failed' }) }) @@ -638,7 +748,7 @@ describe('useAssetDownloadStore', () => { expect(taskService.getTask).toHaveBeenCalledTimes(1) }) - it('keeps re-polling a running download whose task lookup 404s', async () => { + it('settles a running download after repeated task lookup 404s', async () => { const store = useAssetDownloadStore() vi.mocked(taskService.getTask).mockResolvedValue({ ok: false, @@ -648,9 +758,62 @@ describe('useAssetDownloadStore', () => { await vi.advanceTimersByTimeAsync(10_000) - // Only a *pending cancellation* is settled by a 404; an ordinary - // download keeps its reported status. expect(store.activeDownloads).toHaveLength(1) + await vi.advanceTimersByTimeAsync(10_000) + + expect(store.activeDownloads).toHaveLength(0) + expect(store.finishedDownloads[0]).toMatchObject({ + status: 'failed', + error: 'The download task is no longer available.' + }) + + await vi.advanceTimersByTimeAsync(60_000) + expect(taskService.getTask).toHaveBeenCalledTimes(2) + }) + + it('stops polling after reconciliation confirms a failed task', async () => { + const store = useAssetDownloadStore() + vi.mocked(taskService.getTask).mockResolvedValue({ + ok: true, + value: createTaskResponse({ + status: 'failed', + result: undefined, + error_message: 'Download failed' + }) + }) + dispatch(createDownloadMessage({ status: 'failed' })) + + await vi.advanceTimersByTimeAsync(10_000) + expect(store.finishedDownloads[0].status).toBe('failed') + + await vi.advanceTimersByTimeAsync(60_000) + expect(taskService.getTask).toHaveBeenCalledTimes(1) + }) + + it('keeps a terminally reconciled failure terminal after late progress', async () => { + const store = useAssetDownloadStore() + vi.mocked(taskService.getTask).mockResolvedValue({ + ok: true, + value: createTaskResponse({ + status: 'failed', + result: undefined, + error_message: 'Download failed' + }) + }) + dispatch(createDownloadMessage({ status: 'failed' })) + await vi.advanceTimersByTimeAsync(10_000) + + dispatch(createDownloadMessage({ status: 'running', progress: 75 })) + + expect(store.finishedDownloads[0]).toMatchObject({ + status: 'failed', + error: 'Download failed' + }) + expect(store.activeDownloads).toHaveLength(0) + + store.clearFinishedDownloads() + dispatch(createDownloadMessage({ status: 'running', progress: 80 })) + expect(store.downloadList).toHaveLength(0) }) it('settles a pending cancellation the backend never confirms', async () => { @@ -680,10 +843,10 @@ describe('useAssetDownloadStore', () => { expect(store.finishedDownloads[0].status).toBe('cancellation_unconfirmed') expect(store.hasPendingCancellation).toBe(false) - // The unconfirmed state remains recheckable because the backend still - // reports an active task. + // The unconfirmed state is terminal for polling; a late WebSocket + // terminal event can still replace it. await vi.advanceTimersByTimeAsync(10_000) - expect(taskService.getTask).toHaveBeenCalledTimes(7) + expect(taskService.getTask).toHaveBeenCalledTimes(6) }) it('does not consume the cancellation bound on transient lookup failures', async () => { @@ -721,7 +884,7 @@ describe('useAssetDownloadStore', () => { await vi.advanceTimersByTimeAsync(0) }) - it('keeps polling when a completed task has a malformed result', async () => { + it('settles a completed task with a malformed result', async () => { const store = useAssetDownloadStore() store.trackDownload('task-123', 'checkpoints', 'model.safetensors') vi.mocked(taskService.getTask).mockResolvedValue({ @@ -733,11 +896,11 @@ describe('useAssetDownloadStore', () => { await vi.advanceTimersByTimeAsync(10_000) - expect(store.downloadList[0].status).toBe('created') - expect(store.finishedDownloads).toHaveLength(0) + expect(store.downloadList[0].status).toBe('completed') + expect(store.finishedDownloads).toHaveLength(1) }) - it('bounds pending cancellation reconciliation for malformed completed results', async () => { + it('accepts completed cancellation reconciliation with a malformed result', async () => { const store = useAssetDownloadStore() vi.mocked(taskService.cancelTask).mockResolvedValue({ ok: true, @@ -756,8 +919,8 @@ describe('useAssetDownloadStore', () => { await store.cancelDownload('task-123') await vi.advanceTimersByTimeAsync(60_000) - expect(taskService.getTask).toHaveBeenCalledTimes(6) - expect(store.finishedDownloads[0].status).toBe('cancellation_unconfirmed') + expect(taskService.getTask).toHaveBeenCalledTimes(1) + expect(store.finishedDownloads[0].status).toBe('completed') expect(store.hasPendingCancellation).toBe(false) }) @@ -803,7 +966,7 @@ describe('useAssetDownloadStore', () => { ) await store.cancelDownload('task-123') - expect(store.downloadList[0].error).toBe('Source server error') + expect(store.downloadList[0].error).toBeUndefined() await vi.advanceTimersByTimeAsync(10_000) expect(store.finishedDownloads[0]).toMatchObject({ @@ -896,6 +1059,31 @@ describe('useAssetDownloadStore', () => { expect(store.finishedDownloads).toHaveLength(0) }) + + it('keeps a cleared unconfirmed cancellation hidden from late progress', async () => { + const store = useAssetDownloadStore() + vi.mocked(taskService.cancelTask).mockResolvedValue({ + ok: true, + value: 'cancelling' + }) + vi.mocked(taskService.getTask).mockResolvedValue({ + ok: true, + value: createTaskResponse({ status: 'running', result: undefined }) + }) + store.trackDownload('task-123', 'checkpoints', 'model.safetensors') + dispatch(createDownloadMessage({ status: 'running' })) + await store.cancelDownload('task-123') + await vi.advanceTimersByTimeAsync(60_000) + + store.clearFinishedDownloads() + dispatch(createDownloadMessage({ status: 'running', progress: 75 })) + + expect(store.downloadList).toHaveLength(0) + }) + }) + + it('classifies an unconfirmed cancellation as cancelled for the UI', () => { + expect(isDownloadCancelled('cancellation_unconfirmed')).toBe(true) }) describe('session download tracking', () => { diff --git a/src/stores/assetDownloadStore.ts b/src/stores/assetDownloadStore.ts index 27ed92f965c..d2e6deb9a6f 100644 --- a/src/stores/assetDownloadStore.ts +++ b/src/stores/assetDownloadStore.ts @@ -16,6 +16,7 @@ import { } from '@/platform/tasks/services/taskService' import type { AssetDownloadWsMessage } from '@/platform/remote/comfyui/execution/types' import { api } from '@/scripts/api' +import { t } from '@/i18n' export type AssetDownloadStatus = | TaskStatus @@ -33,8 +34,7 @@ const recheckableStatuses = new Set([ 'created', 'running', 'failed', - 'cancellation_pending', - 'cancellation_unconfirmed' + 'cancellation_pending' ]) const reconcilableTaskStatuses = new Set([ 'completed', @@ -177,9 +177,9 @@ function createReconciledDownloadMessage( task: TaskResponse ): AssetDownloadWsMessage | undefined { const result = parseDownloadFileResult(task.result) - // A completed download needs a valid result (or an asset id already learned - // from the socket) before it can be recorded as a completed asset. - if (task.status === 'completed' && !result && !download.assetId) return + // The task status is authoritative even when an older backend returns a + // result shape we cannot parse. Preserve the fields learned from the socket + // so the row can still settle instead of polling forever. const reconciledResult: Partial = result ?? {} const assetId = resultValue(reconciledResult.asset_id, download.assetId) const assetName = requiredResultValue( @@ -221,6 +221,7 @@ function isSettledDownload(download: AssetDownload) { function beginPendingCancellation(download: AssetDownload) { download.status = 'cancellation_pending' + download.error = undefined download.cancellationReconcileAttempts = 0 download.lastUpdate = Date.now() } @@ -229,7 +230,13 @@ function settleUnknownDownloadStatus( existing: AssetDownload | undefined, data: AssetDownloadWsMessage ) { - if (!existing || isSettledDownload(existing)) return + if ( + !existing || + isSettledDownload(existing) || + existing.status === 'cancellation_pending' || + existing.status === 'failed' + ) + return existing.status = 'failed' existing.error = data.error || `Unknown task status: ${data.status}` existing.lastUpdate = Date.now() @@ -239,6 +246,9 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { const downloads = ref>(new Map()) const cancellingTaskIds = ref(new Set()) const reconcilingTasks = new Map>() + const dismissedPendingDownloads = new Map() + const taskNotFoundAttempts = new Map() + const terminalReconciledFailures = new Set() const lastCompletedDownload = ref(null) const downloadList = computed(() => Array.from(downloads.value.values())) @@ -264,8 +274,10 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { ) ) const recheckableDownloads = computed(() => - downloadList.value.filter((download) => - isDownloadRecheckable(download.status) + downloadList.value.filter( + (download) => + isDownloadRecheckable(download.status) && + !terminalReconciledFailures.has(download.taskId) ) ) const hasRecheckableDownloads = computed( @@ -287,14 +299,40 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { function trackDownload(taskId: TaskId, modelType: string, assetName: string) { if (downloads.value.has(taskId)) return + dismissedPendingDownloads.delete(taskId) + taskNotFoundAttempts.delete(taskId) + terminalReconciledFailures.delete(taskId) downloads.value.set( taskId, generateDownloadTrackingPlaceholder(taskId, modelType, assetName) ) } + function consumeDismissedDownload(data: AssetDownloadWsMessage): boolean { + if (!dismissedPendingDownloads.has(data.task_id)) return false + + const modelType = dismissedPendingDownloads.get(data.task_id) + if (data.status === 'completed' && modelType) { + lastCompletedDownload.value = { + taskId: data.task_id, + modelType, + timestamp: Date.now() + } + } + if (data.status === 'completed' || data.status === 'cancelled') { + dismissedPendingDownloads.delete(data.task_id) + } + return true + } + function handleAssetDownload(e: CustomEvent) { const data = e.detail + if (consumeDismissedDownload(data)) return + if ( + terminalReconciledFailures.has(data.task_id) && + activeStatuses.has(data.status) + ) + return const existing = downloads.value.get(data.task_id) // WebSocket payloads are not runtime-validated at the event boundary. @@ -330,6 +368,29 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { } } + function handleTaskNotFound(download: AssetDownload) { + if ( + download.status === 'cancellation_pending' || + download.status === 'cancellation_unconfirmed' + ) { + taskNotFoundAttempts.delete(download.taskId) + finalizeCancellation(download) + return + } + + const attempts = (taskNotFoundAttempts.get(download.taskId) ?? 0) + 1 + taskNotFoundAttempts.set(download.taskId, attempts) + if (attempts < 2) { + download.lastUpdate = Date.now() + return + } + taskNotFoundAttempts.delete(download.taskId) + terminalReconciledFailures.add(download.taskId) + download.status = 'failed' + download.error = t('progressToast.taskUnavailable') + download.lastUpdate = Date.now() + } + async function runReconciliation(download: AssetDownload) { const result = await taskService.getTask(download.taskId) if (downloads.value.get(download.taskId) !== download) return @@ -339,14 +400,12 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { // cancellation has nothing left to wait for. Other lookup failures are // transient and must not consume the authoritative reconciliation bound. if (result.error instanceof TaskNotFoundError) { - if (download.status === 'cancellation_pending') { - finalizeCancellation(download) - } - return + handleTaskNotFound(download) } return } + taskNotFoundAttempts.delete(download.taskId) const task = result.value if ( download.status === 'cancellation_pending' && @@ -362,6 +421,9 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { noteAuthoritativePendingCancellation(download) return } + if (task.status === 'failed') { + terminalReconciledFailures.add(download.taskId) + } handleAssetDownload( new CustomEvent('asset_download', { detail: message @@ -425,6 +487,10 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { function clearFinishedDownloads() { for (const download of finishedDownloads.value) { + if (download.status === 'cancellation_unconfirmed') { + dismissedPendingDownloads.set(download.taskId, download.modelType) + } + taskNotFoundAttempts.delete(download.taskId) downloads.value.delete(download.taskId) } } @@ -435,6 +501,13 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { isDownloadFinished(download.status) || download.status === 'cancellation_pending' ) { + if ( + download.status === 'cancellation_pending' || + download.status === 'cancellation_unconfirmed' + ) { + dismissedPendingDownloads.set(download.taskId, download.modelType) + } + taskNotFoundAttempts.delete(download.taskId) downloads.value.delete(download.taskId) } } @@ -461,6 +534,9 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { if (result.value === 'missing') { beginPendingCancellation(current) await reconcileDownload(current, true) + const latest = downloads.value.get(taskId) + if (!latest) return { ok: true, value: false } + if (latest.status === 'cancelled') return { ok: true, value: true } return { ok: true, value: false } } diff --git a/src/stores/assetExportStore.test.ts b/src/stores/assetExportStore.test.ts index b949c5a2ed9..18a48fea990 100644 --- a/src/stores/assetExportStore.test.ts +++ b/src/stores/assetExportStore.test.ts @@ -196,12 +196,31 @@ describe('useAssetExportStore polling', () => { await vi.advanceTimersByTimeAsync(10_000) + expect(store.activeExports).toHaveLength(1) + await vi.advanceTimersByTimeAsync(10_000) + expect(store.finishedExports[0]).toMatchObject({ status: 'failed', - error: expect.stringContaining('Task not found') + error: 'progressToast.failed' }) }) + it('requires consecutive task-not-found responses before settling', async () => { + const store = useAssetExportStore() + vi.mocked(api.fetchApi) + .mockResolvedValueOnce(new Response(null, { status: 404 })) + .mockResolvedValueOnce(new Response(null, { status: 503 })) + .mockResolvedValue(new Response(null, { status: 404 })) + dispatch(createExportMessage()) + + await vi.advanceTimersByTimeAsync(30_000) + + expect(store.activeExports).toHaveLength(1) + await vi.advanceTimersByTimeAsync(10_000) + expect(store.finishedExports[0].status).toBe('failed') + expect(api.fetchApi).toHaveBeenCalledTimes(4) + }) + it('keeps completion terminal when fetching the download URL fails', async () => { const store = useAssetExportStore() vi.mocked(assetService.getExportDownloadUrl).mockRejectedValueOnce( @@ -288,6 +307,11 @@ describe('assetExportStore triggerDownload', () => { name: 'an absolute signed URL unchanged', url: 'https://storage.example.com/exports/e.zip?signature=abc', expected: 'https://storage.example.com/exports/e.zip?signature=abc' + }, + { + name: 'a document-relative URL under the document base', + url: 'exports/e.zip', + expected: new URL('exports/e.zip', document.baseURI).href } ])('downloads $name', async ({ url, expected }) => { const originalBase = api.api_base @@ -311,4 +335,33 @@ describe('assetExportStore triggerDownload', () => { expect(clickedHrefs).toEqual([expected]) }) + + it.for([ + { name: 'a non-HTTP URL', url: 'javascript:alert(1)' }, + { + name: 'a protocol-relative URL', + url: '//storage.example.com/exports/e.zip?signature=abc' + }, + { + name: 'a backslash-prefixed authority URL', + url: '\\\\storage.example.com/exports/e.zip?signature=abc' + }, + { name: 'an empty URL', url: '' }, + { name: 'a whitespace-only URL', url: ' ' } + ])('rejects $name', async ({ url }) => { + vi.mocked(assetService.getExportDownloadUrl).mockResolvedValue({ + url + }) + const click = vi.spyOn(HTMLAnchorElement.prototype, 'click') + const store = useAssetExportStore() + store.trackExport('task-1') + store.exportList[0].exportName = 'e.zip' + + await store.triggerDownload(store.exportList[0]) + + expect(click).not.toHaveBeenCalled() + expect(store.exportList[0].downloadError).toBe( + 'exportToast.unsupportedDownloadUrl' + ) + }) }) diff --git a/src/stores/assetExportStore.ts b/src/stores/assetExportStore.ts index 7ce29baa6e5..5b0d6974332 100644 --- a/src/stores/assetExportStore.ts +++ b/src/stores/assetExportStore.ts @@ -4,7 +4,12 @@ import { computed, ref, watch } from 'vue' import { assetService } from '@/platform/assets/services/assetService' import { useToastStore } from '@/platform/updates/common/toastStore' -import type { TaskId, TaskStatus } from '@/platform/tasks/services/taskService' +import type { + TaskId, + TaskResponse, + TaskResult, + TaskStatus +} from '@/platform/tasks/services/taskService' import { TaskNotFoundError, taskService @@ -31,6 +36,7 @@ export interface AssetExport { const STALE_THRESHOLD_MS = 10_000 const POLL_INTERVAL_MS = 10_000 +const MAX_TASK_NOT_FOUND_ATTEMPTS = 2 /** * `DELETE /tasks/{id}` is task-type agnostic, so an export can be cancelled @@ -57,9 +63,62 @@ function numberValue(value: unknown, fallback: number): number { return typeof value === 'number' ? value : fallback } +function shouldIgnoreExportUpdate( + existing: AssetExport | undefined, + data: AssetExportWsMessage +): boolean { + const ignoresCompleted = + existing?.status === 'completed' && + !( + data.status === 'completed' && + !existing.exportName && + data.export_name !== undefined + ) + const ignoresFailed = + existing?.status === 'failed' && existing.downloadTriggered + const ignoresCancelled = + existing?.status === 'cancelled' && data.status !== 'completed' + return ignoresCompleted || ignoresFailed || ignoresCancelled +} + +function settleUnknownExportStatus( + existing: AssetExport | undefined, + data: AssetExportWsMessage +): boolean { + if (wireExportStatuses.has(data.status)) return false + if (existing) { + existing.status = 'failed' + existing.error = data.error || `Unknown task status: ${data.status}` + existing.lastUpdate = Date.now() + } + return true +} + +function resolveExportDownloadUrl( + url: string +): { ok: true; value: URL } | { ok: false; error: string } { + const trimmedUrl = url.trim() + if ( + !trimmedUrl || + trimmedUrl.startsWith('//') || + trimmedUrl.startsWith('\\\\') + ) { + return { ok: false, error: t('exportToast.unsupportedDownloadUrl') } + } + const resolvedUrl = trimmedUrl.startsWith('/') + ? api.apiURL(trimmedUrl) + : trimmedUrl + const parsedUrl = new URL(resolvedUrl, document.baseURI) + if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') { + return { ok: false, error: t('exportToast.unsupportedDownloadUrl') } + } + return { ok: true, value: parsedUrl } +} + export const useAssetExportStore = defineStore('assetExport', () => { const exports = ref>(new Map()) const pollingTaskIds = new Set() + const taskNotFoundAttempts = new Map() const exportList = computed(() => Array.from(exports.value.values())) const activeExports = computed(() => @@ -98,8 +157,19 @@ export const useAssetExportStore = defineStore('assetExport', () => { try { exp.downloadError = undefined const { url } = await assetService.getExportDownloadUrl(exp.exportName) + const resolvedUrl = resolveExportDownloadUrl(url) + if (!resolvedUrl.ok) { + exp.downloadError = resolvedUrl.error + exp.downloadTriggered = false + useToastStore().add({ + severity: 'error', + summary: t('exportToast.downloadFailed', { name: exp.exportName }), + detail: resolvedUrl.error + }) + return + } const link = document.createElement('a') - link.href = url.startsWith('/') ? api.apiURL(url) : url + link.href = resolvedUrl.value.href link.download = exp.exportName link.style.display = 'none' link.target = '_blank' @@ -128,21 +198,10 @@ export const useAssetExportStore = defineStore('assetExport', () => { // Completion is authoritative even when fetching the signed URL failed; // late progress/cancellation frames must not revive polling or erase the // retryable download error. - if (existing?.status === 'completed') return - if (existing?.status === 'failed' && existing.downloadTriggered) return - // A cancelled export is only superseded by an authoritative completion; // a stale progress message must not revive it and resume polling. - if (existing?.status === 'cancelled' && data.status !== 'completed') return - - if (!wireExportStatuses.has(data.status)) { - if (existing) { - existing.status = 'failed' - existing.error = data.error || `Unknown task status: ${data.status}` - existing.lastUpdate = Date.now() - } - return - } + if (shouldIgnoreExportUpdate(existing, data)) return + if (settleUnknownExportStatus(existing, data)) return const exp: AssetExport = { taskId: data.task_id, @@ -174,6 +233,52 @@ export const useAssetExportStore = defineStore('assetExport', () => { if (staleExports.length === 0) return + function handleMissingExportTask(exp: AssetExport) { + const attempts = (taskNotFoundAttempts.get(exp.taskId) ?? 0) + 1 + taskNotFoundAttempts.set(exp.taskId, attempts) + if (attempts < MAX_TASK_NOT_FOUND_ATTEMPTS) { + exp.lastUpdate = Date.now() + return + } + taskNotFoundAttempts.delete(exp.taskId) + handleAssetExport({ + task_id: exp.taskId, + export_name: exp.exportName, + assets_total: exp.assetsTotal, + assets_attempted: exp.assetsAttempted, + assets_failed: exp.assetsFailed, + bytes_total: exp.bytesTotal, + bytes_processed: exp.bytesProcessed, + progress: exp.progress, + status: 'failed', + error: t('progressToast.failed') + }) + } + + function applyFinishedExport( + exp: AssetExport, + result: Extract, { ok: true }> + ) { + const task = result.value + if (!finishedExportStatuses.has(task.status)) return + const taskResult = task.result ?? {} + handleAssetExport({ + task_id: exp.taskId, + export_name: stringValue(taskResult.export_name, exp.exportName), + assets_total: numberValue(taskResult.assets_total, exp.assetsTotal), + assets_attempted: numberValue( + taskResult.assets_attempted, + exp.assetsAttempted + ), + assets_failed: numberValue(taskResult.assets_failed, exp.assetsFailed), + bytes_total: exp.bytesTotal, + bytes_processed: exp.bytesTotal, + progress: task.status === 'completed' ? 1 : exp.progress, + status: task.status, + error: task.error_message ?? stringValue(taskResult.error, '') + }) + } + async function pollSingleExport(exp: AssetExport) { if (pollingTaskIds.has(exp.taskId)) return pollingTaskIds.add(exp.taskId) @@ -185,44 +290,14 @@ export const useAssetExportStore = defineStore('assetExport', () => { if (exports.value.get(exp.taskId) !== exp) return if (!result.ok) { if (result.error instanceof TaskNotFoundError) { - handleAssetExport({ - task_id: exp.taskId, - export_name: exp.exportName, - assets_total: exp.assetsTotal, - assets_attempted: exp.assetsAttempted, - assets_failed: exp.assetsFailed, - bytes_total: exp.bytesTotal, - bytes_processed: exp.bytesProcessed, - progress: exp.progress, - status: 'failed', - error: result.error.message - }) + handleMissingExportTask(exp) + } else { + taskNotFoundAttempts.delete(exp.taskId) } return } - - const task = result.value - if (finishedExportStatuses.has(task.status)) { - const taskResult = task.result ?? {} - handleAssetExport({ - task_id: exp.taskId, - export_name: stringValue(taskResult.export_name, exp.exportName), - assets_total: numberValue(taskResult.assets_total, exp.assetsTotal), - assets_attempted: numberValue( - taskResult.assets_attempted, - exp.assetsAttempted - ), - assets_failed: numberValue( - taskResult.assets_failed, - exp.assetsFailed - ), - bytes_total: exp.bytesTotal, - bytes_processed: exp.bytesTotal, - progress: task.status === 'completed' ? 1 : exp.progress, - status: task.status, - error: task.error_message ?? stringValue(taskResult.error, '') - }) - } + taskNotFoundAttempts.delete(exp.taskId) + applyFinishedExport(exp, result) } finally { pollingTaskIds.delete(exp.taskId) } @@ -250,6 +325,7 @@ export const useAssetExportStore = defineStore('assetExport', () => { function clearFinishedExports() { for (const exp of finishedExports.value) { + taskNotFoundAttempts.delete(exp.taskId) exports.value.delete(exp.taskId) } }