diff --git a/browser_tests/tests/dialogs/modelImportProgressDialog.spec.ts b/browser_tests/tests/dialogs/modelImportProgressDialog.spec.ts index 2c574bf6365..ce564ff44d2 100644 --- a/browser_tests/tests/dialogs/modelImportProgressDialog.spec.ts +++ b/browser_tests/tests/dialogs/modelImportProgressDialog.spec.ts @@ -258,6 +258,80 @@ test.describe('Model import progress toast', { tag: ['@screenshot'] }, () => { }) }) + test('a dismissed unconfirmed cancellation stays hidden from late progress', async ({ + comfyPage + }) => { + test.setTimeout(30_000) + const { page } = comfyPage + const taskId = '1396cc07-bab2-4f12-9b54-741f83f9224e' + const assetName = 'unconfirmed-cancel-model.safetensors' + 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() + const cancellation = page.waitForResponse( + (candidate) => + candidate.url().endsWith(`/tasks/${taskId}`) && + candidate.request().method() === 'DELETE' + ) + await toast.getByRole('button', { name: 'Cancel Download' }).click() + await cancellation + await expect( + toast.getByText('Cancelled', { exact: true }).first() + ).toBeVisible() + + const advanceReconciliation = async () => { + const response = page.waitForResponse( + (candidate) => + candidate.url().endsWith(`/tasks/${taskId}`) && + candidate.request().method() === 'GET' + ) + await (await response).finished() + } + 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 47512d6e534..1c35cd22d3a 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -4889,6 +4889,7 @@ "failed": "Failed", "finished": "Finished", "pending": "Pending", + "taskUnavailable": "The download task is no longer available.", "progressCount": "{completed} of {total}", "filter": { "all": "All", @@ -4908,6 +4909,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 7ea9c6312ca..d97455e9976 100644 --- a/src/platform/assets/composables/useUploadModelWizard.test.ts +++ b/src/platform/assets/composables/useUploadModelWizard.test.ts @@ -19,7 +19,8 @@ vi.mock(import('@/platform/assets/services/assetService'), () => ({ assetService: { getAssetMetadata: vi.fn(), uploadAssetAsync: vi.fn(), - uploadAssetPreviewImage: vi.fn() + uploadAssetFromBase64: vi.fn(), + deleteAsset: vi.fn() } })) @@ -102,6 +103,218 @@ 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', created_new: true })) + }) + ) + + 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(assetService.deleteAsset).toHaveBeenCalledWith('preview-id') + expect(wizard.currentStep.value).toBe(1) + expect(wizard.isUploading.value).toBe(false) + }) + + it('does not delete a deduplicated preview after reset', async () => { + const { assetService } = + await import('@/platform/assets/services/assetService') + let finishPreview!: () => void + vi.mocked(assetService.uploadAssetFromBase64).mockReturnValue( + new Promise((resolve) => { + finishPreview = () => + resolve(fromPartial({ id: 'shared-preview', created_new: false })) + }) + ) + const wizard = setupUploadModelWizard(modelTypes) + wizard.wizardData.value.url = 'https://civitai.com/models/shared-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.deleteAsset).not.toHaveBeenCalled() + }) + + it('does not reopen a reset wizard after a synchronous refresh finishes', async () => { + const { assetService } = + await import('@/platform/assets/services/assetService') + vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({ + type: 'sync', + asset: fromPartial({ + id: 'asset-1', + name: 'model.safetensors', + tags: ['models', 'checkpoints'] + }) + }) + let finishRefresh!: () => void + const refreshPending = new Promise((resolve) => { + finishRefresh = resolve + }) + vi.spyOn(useModelToNodeStore(), 'getAllNodeProviders').mockReturnValue([ + fromPartial({ nodeDef: { name: 'CheckpointLoaderSimple' } }) + ]) + vi.spyOn(useAssetsStore(), 'updateModelsForNodeType').mockReturnValue( + refreshPending + ) + + const wizard = setupUploadModelWizard(modelTypes) + wizard.wizardData.value.url = 'https://civitai.com/models/sync' + wizard.selectedModelType.value = 'checkpoints' + const upload = wizard.uploadModel() + await vi.waitFor(() => { + expect(useAssetsStore().updateModelsForNodeType).toHaveBeenCalledOnce() + }) + + wizard.resetWizard() + finishRefresh() + + await expect(upload).resolves.toBeNull() + expect(wizard.currentStep.value).toBe(1) + expect(wizard.uploadStatus.value).toBeUndefined() + }) + + 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('refreshes model caches when a synchronous import finishes after 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 + }) + ) + vi.spyOn(useModelToNodeStore(), 'getAllNodeProviders').mockReturnValue([ + fromPartial({ nodeDef: { name: 'CheckpointLoaderSimple' } }) + ]) + const refresh = vi + .spyOn(useAssetsStore(), 'updateModelsForNodeType') + .mockResolvedValue(undefined) + + const wizard = setupUploadModelWizard(modelTypes) + wizard.wizardData.value.url = 'https://civitai.com/models/stale-sync' + wizard.selectedModelType.value = 'checkpoints' + const upload = wizard.uploadModel() + await vi.waitFor(() => { + expect(assetService.uploadAssetAsync).toHaveBeenCalledOnce() + }) + wizard.resetWizard() + finishUpload({ + type: 'sync', + asset: fromPartial({ + id: 'asset-after-reset', + name: 'model.safetensors', + tags: ['models', 'checkpoints'] + }) + }) + + await expect(upload).resolves.toBeNull() + expect(refresh).toHaveBeenCalledWith('CheckpointLoaderSimple') + 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..832bd0b8131 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: '', @@ -225,7 +233,7 @@ export function useUploadModelWizard( async function uploadPreviewImage( filename: string - ): Promise { + ): Promise<{ id: string; createdNew: boolean } | undefined> { if (!wizardData.value.previewImage) return undefined try { @@ -243,19 +251,20 @@ export function useUploadModelWizard( name: `${baseFilename}_preview.${extension}`, tags: ['preview'] }) - return previewAsset.id + return { + id: previewAsset.id, + createdNew: previewAsset.created_new + } } catch (error) { console.error('Failed to upload preview image:', error) return undefined } } - async function refreshModelCaches() { - if (!resolvedModelType.value) return + async function refreshModelCaches(modelType = resolvedModelType.value) { + if (!modelType) return - const providers = modelToNodeStore.getAllNodeProviders( - resolvedModelType.value - ) + const providers = modelToNodeStore.getAllNodeProviders(modelType) const results = await Promise.allSettled( providers.map((provider) => assetsStore.updateModelsForNodeType(provider.nodeDef.name) @@ -310,6 +319,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 +400,7 @@ export function useUploadModelWizard( } isUploading.value = true + const generation = ++uploadGeneration stopAsyncWatch?.() stopAsyncWatch = undefined activeAsyncTaskId = undefined @@ -341,7 +419,18 @@ export function useUploadModelWizard( wizardData.value.metadata?.name || 'model' - const previewId = await uploadPreviewImage(filename) + const preview = await uploadPreviewImage(filename) + if (isStaleUpload(generation)) { + if (preview?.createdNew) { + try { + await assetService.deleteAsset(preview.id) + } catch (error) { + console.error('Failed to clean up stale preview image:', error) + } + } + return null + } + const userMetadata = { source: source.type, source_url: wizardData.value.url, @@ -352,7 +441,7 @@ export function useUploadModelWizard( source_url: wizardData.value.url, tags, user_metadata: userMetadata, - preview_id: previewId + preview_id: preview?.id }) if (result.type === 'async' && result.task.status !== 'completed') { @@ -363,73 +452,21 @@ export function useUploadModelWizard( filename ) } - uploadStatus.value = 'processing' - uploadSuccess = { - filename, - modelType, - taskId: result.task.task_id, - status: 'processing' + } + + if (isStaleUpload(generation)) { + if (result.type === 'sync' || result.task.status === 'completed') { + await refreshModelCaches(modelType) } + return null + } - 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 (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 && @@ -443,6 +480,7 @@ export function useUploadModelWizard( uploadStatus.value = 'success' await refreshModelCaches() + if (isStaleUpload(generation)) return null uploadSuccess = { filename: result.type === 'sync' ? getAssetFilename(result.asset) : filename, @@ -452,13 +490,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 +511,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 4b25d704b84..c82f39aa16a 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 '@/schemas/apiSchema' -import { useAssetDownloadStore } from '@/stores/assetDownloadStore' +import { + isDownloadCancelled, + useAssetDownloadStore +} from '@/stores/assetDownloadStore' type DownloadEventHandler = (e: CustomEvent) => void @@ -150,6 +153,99 @@ 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' + }) + const completionTimestamp = store.lastCompletedDownload?.timestamp + await vi.advanceTimersByTimeAsync(1) + dispatch(createDownloadMessage({ status: 'completed', progress: 1 })) + expect(store.lastCompletedDownload?.timestamp).toBe(completionTimestamp) + + // Late progress after a terminal frame is still part of the dismissed + // task and must not reopen the toast. + dispatch(createDownloadMessage({ status: 'running', progress: 0.25 })) + expect(store.downloadList).toHaveLength(0) + }) + + 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 +507,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 +642,7 @@ describe('useAssetDownloadStore', () => { expect(store.lastCompletedDownload?.modelType).toBe('checkpoints') }) - it('reconciles an authoritative failure after local cancellation and keeps polling', async () => { + it('recovers from an authoritative failure during cancellation', async () => { const store = useAssetDownloadStore() store.trackDownload('task-123', 'checkpoints', 'model.safetensors') vi.mocked(taskService.cancelTask).mockResolvedValue({ @@ -557,12 +671,12 @@ describe('useAssetDownloadStore', () => { error: 'Cancellation failed' }) - await vi.advanceTimersByTimeAsync(10_000) + await vi.advanceTimersByTimeAsync(60_000) expect(taskService.getTask).toHaveBeenCalledTimes(2) expect(store.finishedDownloads[0]).toMatchObject({ status: 'completed', - assetId: 'asset-456' + error: undefined }) }) @@ -638,7 +752,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 +762,161 @@ 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('restarts reconciliation when cancelling a task-unavailable row', async () => { + const store = useAssetDownloadStore() + vi.mocked(taskService.getTask) + .mockResolvedValueOnce({ + ok: false, + error: new TaskNotFoundError('task-123') + }) + .mockResolvedValueOnce({ + ok: false, + error: new TaskNotFoundError('task-123') + }) + .mockResolvedValue({ + ok: true, + value: createTaskResponse({ status: 'running', result: undefined }) + }) + vi.mocked(taskService.cancelTask).mockResolvedValue({ + ok: true, + value: 'cancelling' + }) + dispatch(createDownloadMessage({ status: 'running' })) + await vi.advanceTimersByTimeAsync(20_000) + expect(store.finishedDownloads[0].status).toBe('failed') + + await store.cancelDownload('task-123') + await vi.advanceTimersByTimeAsync(10_000) + + expect(taskService.getTask).toHaveBeenCalledTimes(3) + expect(store.downloadList[0].status).toBe('cancellation_pending') + }) + + it('recovers a task-unavailable row from new active progress', async () => { + const store = useAssetDownloadStore() + vi.mocked(taskService.getTask).mockResolvedValue({ + ok: false, + error: new TaskNotFoundError('task-123') + }) + dispatch(createDownloadMessage({ status: 'running' })) + await vi.advanceTimersByTimeAsync(20_000) + expect(store.finishedDownloads[0].status).toBe('failed') + + dispatch(createDownloadMessage({ status: 'running', progress: 75 })) + + expect(store.activeDownloads[0]).toMatchObject({ + status: 'running', + progress: 75 + }) + }) + + it('keeps reconciling a failed task so a backend retry can recover', async () => { + const store = useAssetDownloadStore() + vi.mocked(taskService.getTask) + .mockResolvedValueOnce({ + ok: true, + value: createTaskResponse({ + status: 'failed', + result: undefined, + error_message: 'Retrying download' + }) + }) + .mockResolvedValueOnce({ + ok: true, + value: createTaskResponse({ status: 'running', result: undefined }) + }) + .mockResolvedValueOnce({ + ok: true, + value: createTaskResponse({ status: 'completed' }) + }) + dispatch(createDownloadMessage({ status: 'failed' })) + + await vi.advanceTimersByTimeAsync(30_000) + + expect(taskService.getTask).toHaveBeenCalledTimes(3) + expect(store.finishedDownloads[0].status).toBe('completed') + }) + + it('bounds polling for a permanently 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(300_000) + expect(taskService.getTask).toHaveBeenCalledTimes(30) + await vi.advanceTimersByTimeAsync(60_000) + expect(taskService.getTask).toHaveBeenCalledTimes(30) + + dispatch(createDownloadMessage({ status: 'running', progress: 75 })) + expect(store.activeDownloads).toHaveLength(1) + }) + + it('accepts retry progress after an authoritative failed snapshot', 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.activeDownloads[0]).toMatchObject({ + status: 'running', + progress: 75 + }) + }) + + it('requires consecutive task lookup 404s before settling', async () => { + const store = useAssetDownloadStore() + vi.mocked(taskService.getTask) + .mockResolvedValueOnce({ + ok: false, + error: new TaskNotFoundError('task-123') + }) + .mockResolvedValueOnce({ + ok: false, + error: new Error('gateway unavailable') + }) + .mockResolvedValueOnce({ + ok: false, + error: new TaskNotFoundError('task-123') + }) + .mockResolvedValueOnce({ + ok: true, + value: createTaskResponse({ status: 'running', result: undefined }) + }) + dispatch(createDownloadMessage({ status: 'running' })) + + await vi.advanceTimersByTimeAsync(40_000) + + expect(store.activeDownloads).toHaveLength(1) + expect(store.finishedDownloads).toHaveLength(0) }) it('settles a pending cancellation the backend never confirms', async () => { @@ -680,10 +946,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 +987,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 +999,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 +1022,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 +1069,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({ @@ -835,6 +1101,16 @@ describe('useAssetDownloadStore', () => { expect(store.hasDownloads).toBe(false) }) + it('does not restore a dismissed confirmed cancellation from late progress', () => { + const store = useAssetDownloadStore() + dispatch(createDownloadMessage({ status: 'cancelled' })) + + store.clearFinishedDownloads() + dispatch(createDownloadMessage({ status: 'running', progress: 80 })) + + expect(store.downloadList).toHaveLength(0) + }) + it('polls and marks failed downloads', async () => { const store = useAssetDownloadStore() @@ -896,6 +1172,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 ee665b0d59b..db82efa4378 100644 --- a/src/stores/assetDownloadStore.ts +++ b/src/stores/assetDownloadStore.ts @@ -1,6 +1,6 @@ import { useIntervalFn } from '@vueuse/core' import { defineStore } from 'pinia' -import { computed, ref, watch } from 'vue' +import { computed, reactive, ref, watch } from 'vue' import type { DownloadFileResult, @@ -16,6 +16,7 @@ import { } from '@/platform/tasks/services/taskService' import type { AssetDownloadWsMessage } from '@/schemas/apiSchema' 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', @@ -93,6 +93,10 @@ interface CompletedDownload { modelType: string timestamp: number } +interface DismissedDownload { + modelType?: string + completionNotified: boolean +} const STALE_THRESHOLD_MS = 10_000 const POLL_INTERVAL_MS = 10_000 /** @@ -102,6 +106,9 @@ const POLL_INTERVAL_MS = 10_000 * reports a terminal status from pinning the toast open until a page reload. */ const MAX_CANCELLATION_RECONCILE_ATTEMPTS = 6 +const MAX_TASK_NOT_FOUND_ATTEMPTS = 2 +const MAX_FAILED_RECONCILE_ATTEMPTS = 30 +const MAX_DISMISSED_DOWNLOADS = 256 function generateDownloadTrackingPlaceholder( taskId: TaskId, @@ -150,8 +157,8 @@ function finalizeCancellation(download: AssetDownload) { /** * Record a reconciliation attempt that left a pending cancellation unresolved, * exposing an unconfirmed but dismissible state once the attempts are - * exhausted. It remains recheckable so a late authoritative outcome can - * replace it. + * exhausted. A later WebSocket completion can still replace it, but polling + * stops once the bounded reconciliation window is exhausted. */ function noteAuthoritativePendingCancellation(download: AssetDownload) { if (download.status !== 'cancellation_pending') return @@ -177,9 +184,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 +228,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 +237,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 +253,11 @@ 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 unavailableTaskIds = reactive(new Set()) + const failedReconcileAttempts = reactive(new Map()) + const settledFailureTaskIds = reactive(new Set()) const lastCompletedDownload = ref(null) const downloadList = computed(() => Array.from(downloads.value.values())) @@ -264,8 +283,11 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { ) ) const recheckableDownloads = computed(() => - downloadList.value.filter((download) => - isDownloadRecheckable(download.status) + downloadList.value.filter( + (download) => + isDownloadRecheckable(download.status) && + !unavailableTaskIds.has(download.taskId) && + !settledFailureTaskIds.has(download.taskId) ) ) const hasRecheckableDownloads = computed( @@ -287,14 +309,52 @@ 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) + unavailableTaskIds.delete(taskId) + failedReconcileAttempts.delete(taskId) + settledFailureTaskIds.delete(taskId) downloads.value.set( taskId, generateDownloadTrackingPlaceholder(taskId, modelType, assetName) ) } + function rememberDismissedDownload(download: AssetDownload) { + dismissedPendingDownloads.delete(download.taskId) + dismissedPendingDownloads.set(download.taskId, { + modelType: download.modelType, + completionNotified: download.status === 'completed' + }) + while (dismissedPendingDownloads.size > MAX_DISMISSED_DOWNLOADS) { + const oldestTaskId = dismissedPendingDownloads.keys().next().value + if (oldestTaskId === undefined) break + dismissedPendingDownloads.delete(oldestTaskId) + } + } + + function consumeDismissedDownload(data: AssetDownloadWsMessage): boolean { + if (!dismissedPendingDownloads.has(data.task_id)) return false + + const dismissed = dismissedPendingDownloads.get(data.task_id) + if ( + data.status === 'completed' && + dismissed?.modelType && + !dismissed.completionNotified + ) { + lastCompletedDownload.value = { + taskId: data.task_id, + modelType: dismissed.modelType, + timestamp: Date.now() + } + dismissed.completionNotified = true + } + return true + } + function handleAssetDownload(e: CustomEvent) { const data = e.detail + if (consumeDismissedDownload(data)) return const existing = downloads.value.get(data.task_id) // WebSocket payloads are not runtime-validated at the event boundary. @@ -306,6 +366,13 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { if (shouldIgnoreDownloadUpdate(existing?.status, data.status)) return + if (activeStatuses.has(data.status)) { + taskNotFoundAttempts.delete(data.task_id) + unavailableTaskIds.delete(data.task_id) + failedReconcileAttempts.delete(data.task_id) + settledFailureTaskIds.delete(data.task_id) + } + const download: AssetDownload = { taskId: data.task_id, assetId: data.asset_id, @@ -330,6 +397,26 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { } } + function handleTaskNotFound(download: AssetDownload) { + if (download.status === 'cancellation_pending') { + taskNotFoundAttempts.delete(download.taskId) + finalizeCancellation(download) + return + } + + const attempts = (taskNotFoundAttempts.get(download.taskId) ?? 0) + 1 + taskNotFoundAttempts.set(download.taskId, attempts) + if (attempts < MAX_TASK_NOT_FOUND_ATTEMPTS) { + download.lastUpdate = Date.now() + return + } + taskNotFoundAttempts.delete(download.taskId) + unavailableTaskIds.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 +426,15 @@ 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) + } else { + taskNotFoundAttempts.delete(download.taskId) } return } + taskNotFoundAttempts.delete(download.taskId) + unavailableTaskIds.delete(download.taskId) const task = result.value if ( download.status === 'cancellation_pending' && @@ -362,6 +450,16 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { noteAuthoritativePendingCancellation(download) return } + if (task.status === 'failed') { + const attempts = (failedReconcileAttempts.get(download.taskId) ?? 0) + 1 + failedReconcileAttempts.set(download.taskId, attempts) + if (attempts >= MAX_FAILED_RECONCILE_ATTEMPTS) { + settledFailureTaskIds.add(download.taskId) + } + } else { + failedReconcileAttempts.delete(download.taskId) + settledFailureTaskIds.delete(download.taskId) + } handleAssetDownload( new CustomEvent('asset_download', { detail: message @@ -425,6 +523,11 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { function clearFinishedDownloads() { for (const download of finishedDownloads.value) { + rememberDismissedDownload(download) + taskNotFoundAttempts.delete(download.taskId) + unavailableTaskIds.delete(download.taskId) + failedReconcileAttempts.delete(download.taskId) + settledFailureTaskIds.delete(download.taskId) downloads.value.delete(download.taskId) } } @@ -435,6 +538,11 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { isDownloadFinished(download.status) || download.status === 'cancellation_pending' ) { + rememberDismissedDownload(download) + taskNotFoundAttempts.delete(download.taskId) + unavailableTaskIds.delete(download.taskId) + failedReconcileAttempts.delete(download.taskId) + settledFailureTaskIds.delete(download.taskId) downloads.value.delete(download.taskId) } } @@ -455,12 +563,18 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { if (isSettledDownload(current)) { return { ok: true, value: result.value === 'cancelling' } } + unavailableTaskIds.delete(taskId) + failedReconcileAttempts.delete(taskId) + settledFailureTaskIds.delete(taskId) // A DELETE 404 can mean the cancellation route is unavailable, not that // the task is gone. Re-read the task before deciding how to settle it. 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 d8a6e6dcd60..9c34664c8a3 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.taskUnavailable' }) }) + 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,40 @@ 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: 'a mixed backslash authority URL', + url: '\\/storage.example.com/exports/e.zip?signature=abc' + }, + { name: 'a malformed HTTP URL', url: 'http://' }, + { name: 'a malformed IPv6 URL', url: 'https://[' }, + { name: 'a slash-backslash authority URL', url: '/\\evil.com/x' }, + { 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 891dcc40292..471635bb5f9 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,68 @@ 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('\\') || + trimmedUrl.startsWith('/\\') + ) { + return { ok: false, error: t('exportToast.unsupportedDownloadUrl') } + } + const resolvedUrl = trimmedUrl.startsWith('/') + ? api.apiURL(trimmedUrl) + : trimmedUrl + let parsedUrl: URL + try { + parsedUrl = new URL(resolvedUrl, document.baseURI) + } catch { + return { ok: false, error: t('exportToast.unsupportedDownloadUrl') } + } + 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 +163,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 +204,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 +239,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.taskUnavailable') + }) + } + + 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 +296,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 +331,7 @@ export const useAssetExportStore = defineStore('assetExport', () => { function clearFinishedExports() { for (const exp of finishedExports.value) { + taskNotFoundAttempts.delete(exp.taskId) exports.value.delete(exp.taskId) } }