diff --git a/browser_tests/fixtures/utils/viewFileMocks.ts b/browser_tests/fixtures/utils/viewFileMocks.ts index 29b57c931bd..b9a8a87c74e 100644 --- a/browser_tests/fixtures/utils/viewFileMocks.ts +++ b/browser_tests/fixtures/utils/viewFileMocks.ts @@ -4,7 +4,7 @@ type RouteFulfillOptions = NonNullable[0]> type ViewFile = Pick -const transparentPng = Buffer.from( +export const transparentPng = Buffer.from( 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAFgwJ/lwPIRwAAAABJRU5ErkJggg==', 'base64' ) diff --git a/browser_tests/tests/agent/agentPanelMocks.ts b/browser_tests/tests/agent/agentPanelMocks.ts index b4a431926e3..741f3541453 100644 --- a/browser_tests/tests/agent/agentPanelMocks.ts +++ b/browser_tests/tests/agent/agentPanelMocks.ts @@ -94,15 +94,20 @@ export const RESIZE_IMAGE_TOOL_EVENT: AgentWsEvent = { const MESSAGE_DELTA_TEXT = 'The graph is **fully ready** to go — prompt set to the red fox in the snow.' -export const MESSAGE_DELTA_EVENT: AgentWsEvent = { - type: 'agent_message_delta', - data: { - delta: MESSAGE_DELTA_TEXT, - message_id: TURN_ID, - thread_id: THREAD_ID +export function messageDeltaEvent(delta: string): AgentWsEvent { + return { + type: 'agent_message_delta', + data: { + delta, + message_id: TURN_ID, + thread_id: THREAD_ID + } } } +export const MESSAGE_DELTA_EVENT: AgentWsEvent = + messageDeltaEvent(MESSAGE_DELTA_TEXT) + export const MESSAGE_DONE_EVENT: AgentWsEvent = { type: 'agent_message_done', data: { diff --git a/browser_tests/tests/agent/agentReplyAssets.spec.ts b/browser_tests/tests/agent/agentReplyAssets.spec.ts new file mode 100644 index 00000000000..e8c0eb3d88b --- /dev/null +++ b/browser_tests/tests/agent/agentReplyAssets.spec.ts @@ -0,0 +1,127 @@ +import type { Route, WebSocketRoute } from '@playwright/test' +import { expect, mergeTests } from '@playwright/test' + +import { webSocketFixture } from '@e2e/fixtures/ws' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } +import type { FeatureFlagsWsMessage } from '@/schemas/apiSchema' +import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' + +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' +import { transparentPng } from '@e2e/fixtures/utils/viewFileMocks' +import { + MESSAGE_DONE_EVENT, + agentTest, + messageDeltaEvent +} from '@e2e/tests/agent/agentPanelMocks' + +const COLLAPSED_COUNT = 12 +const MODEL_COUNT = 13 +const HIDDEN_MODEL = `mesh-${MODEL_COUNT - 1}.glb` +const VISIBLE_MODELS = Array.from( + { length: COLLAPSED_COUNT }, + (_, index) => `mesh-${index}.glb` +) + +const MODEL_REPLY = Array.from( + { length: MODEL_COUNT }, + (_, index) => `- ![mesh-${index}](/api/view?filename=mesh-${index}.glb)` +).join('\n') + +type WsFrame = + | AgentWsEvent + | { type: 'feature_flags'; data: FeatureFlagsWsMessage } + +function pushEvent(ws: WebSocketRoute, event: WsFrame): void { + ws.send(JSON.stringify(event)) +} + +/** + * Names the component asked the asset API about, in request order. Every + * looked-up model answers with a ready preview, so a tile renders without + * needing a WebGL thumbnail render. + */ +const test = mergeTests(agentTest, webSocketFixture).extend<{ + lookedUpModels: string[] +}>({ + lookedUpModels: async ({ page }, use) => { + const lookedUp: string[] = [] + + await page.route('**/api/assets**', (route: Route) => { + const url = new URL(route.request().url()) + if (url.pathname.endsWith('/content')) + return route.fulfill({ + contentType: 'image/png', + body: transparentPng + }) + + const name = + url.searchParams.get('hash') ?? url.searchParams.get('name_contains') + if (!name) return route.fulfill(jsonRoute({ assets: [] })) + + if (/^mesh-\d+\.glb$/.test(name)) lookedUp.push(name) + return route.fulfill( + jsonRoute({ + assets: [ + { + id: `asset-${name}`, + name, + hash: name, + preview_id: `preview-${name}` + } + ] + }) + ) + }) + + await use(lookedUp) + } +}) + +test.describe('Agent reply assets', { tag: '@cloud' }, () => { + test.use({ connectWebSocketToServer: false }) + + test('looks up 3D previews only for the reply assets on screen', async ({ + comfyPage, + postedMessages, + getWebSocket, + lookedUpModels + }) => { + test.setTimeout(30_000) + + const page = comfyPage.page + await page + .getByRole('button', { name: enMessages.agent.askComfyAgent }) + .click() + + const panel = page.locator('#agent-panel-root') + await panel + .getByRole('textbox', { name: /^Describe ideas/ }) + .fill('show me every mesh') + + const ws = await getWebSocket() + await panel.getByRole('button', { name: enMessages.agent.send }).click() + await expect.poll(() => postedMessages.length).toBeGreaterThanOrEqual(1) + + pushEvent(ws, { type: 'feature_flags', data: { assets: true } }) + pushEvent(ws, messageDeltaEvent(MODEL_REPLY)) + pushEvent(ws, MESSAGE_DONE_EVENT) + + const tiles = panel.getByRole('button', { name: /^mesh-\d+$/ }) + const thumbnails = panel.locator('img[alt^="mesh-"]') + + await expect(tiles).toHaveCount(COLLAPSED_COUNT) + await expect(thumbnails).toHaveCount(COLLAPSED_COUNT) + expect( + [...new Set(lookedUpModels)].sort(), + 'a collapsed reply must look up the models it shows, and only those' + ).toEqual([...VISIBLE_MODELS].sort()) + + await panel.getByRole('button', { name: enMessages.agent.showMore }).click() + + await expect(tiles).toHaveCount(MODEL_COUNT) + await expect(thumbnails).toHaveCount(MODEL_COUNT) + await expect.poll(() => new Set(lookedUpModels).size).toBe(MODEL_COUNT) + expect(lookedUpModels).toContain(HIDDEN_MODEL) + }) +}) diff --git a/src/components/load3d/modelThumbnail.test.ts b/src/components/load3d/modelThumbnail.test.ts index a77c8b025d0..ab5bfd7afce 100644 --- a/src/components/load3d/modelThumbnail.test.ts +++ b/src/components/load3d/modelThumbnail.test.ts @@ -2,15 +2,20 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { generateModelThumbnail } from './modelThumbnail' +const createLoad3d = vi.hoisted(() => vi.fn()) +vi.mock('@/extensions/core/load3d/createLoad3d', () => ({ createLoad3d })) + const isAssetPreviewSupported = vi.hoisted(() => vi.fn(() => false)) -const persistThumbnail = vi.hoisted(() => vi.fn(async () => {})) +const persistThumbnail = vi.hoisted(() => + vi.fn(async (_assetName: string, _blob: Blob) => {}) +) vi.mock('@/platform/assets/utils/assetPreviewUtil', () => ({ isAssetPreviewSupported, persistThumbnail })) -const createLoad3d = vi.hoisted(() => vi.fn()) -vi.mock('@/extensions/core/load3d/createLoad3d', () => ({ createLoad3d })) +const reportError = vi.hoisted(() => vi.fn()) +vi.mock('@/platform/telemetry/reportError', () => ({ reportError })) function mockInstance(overrides: Record = {}) { return { @@ -26,6 +31,7 @@ describe('generateModelThumbnail', () => { createLoad3d.mockReset() isAssetPreviewSupported.mockReset().mockReturnValue(false) persistThumbnail.mockReset() + reportError.mockReset() }) it('renders offscreen, returns the data url, and disposes the instance', async () => { @@ -37,28 +43,86 @@ describe('generateModelThumbnail', () => { 'a.glb' ) - expect(result).toBe('data:image/png;base64,thumb') - expect(instance.loadModel).toHaveBeenCalledWith('/api/view?filename=a.glb') + expect(result).toEqual({ + status: 'rendered', + dataUrl: 'data:image/png;base64,thumb' + }) + expect(instance.loadModel).toHaveBeenCalledWith( + '/api/view?filename=a.glb', + undefined, + { silent: true } + ) expect(instance.remove).toHaveBeenCalledTimes(1) expect(persistThumbnail).not.toHaveBeenCalled() }) - it('persists the thumbnail when the asset API is available', async () => { - isAssetPreviewSupported.mockReturnValue(true) - vi.stubGlobal( - 'fetch', - vi.fn().mockResolvedValue({ blob: () => Promise.resolve(new Blob()) }) - ) - createLoad3d.mockReturnValue(mockInstance()) + it('releases the queue the moment a running render is aborted', async () => { + vi.useFakeTimers() + try { + const stalled = mockInstance({ + loadModel: vi.fn(() => new Promise(() => {})) + }) + const next = mockInstance() + createLoad3d.mockReturnValueOnce(stalled).mockReturnValueOnce(next) + const controller = new AbortController() + + const abortedRun = generateModelThumbnail( + '/slow.glb', + 'slow.glb', + controller.signal + ) + const nextRun = generateModelThumbnail('/next.glb', 'next.glb') + await vi.advanceTimersByTimeAsync(0) + expect(createLoad3d).toHaveBeenCalledTimes(1) + + controller.abort() + await vi.advanceTimersByTimeAsync(0) + + await expect(abortedRun).resolves.toEqual({ status: 'cancelled' }) + await expect(nextRun).resolves.toEqual({ + status: 'rendered', + dataUrl: 'data:image/png;base64,thumb' + }) + expect(stalled.remove).toHaveBeenCalledOnce() + // The abandoned `stalled` render's underlying withTimeout deadline is + // not cancelled by the caller abort (see module doc: the transfer and + // parse are not abortable and run to completion in the background), + // so a live timer for it — and for `next`'s own in-flight deadline — + // is expected here, not zero. + expect(reportError).not.toHaveBeenCalled() + } finally { + vi.useRealTimers() + } + }) - await generateModelThumbnail('/a.glb', 'a.glb') + it('skips a queued render whose caller aborted before its turn', async () => { + const blocked = mockInstance({ + loadModel: vi.fn(() => new Promise(() => {})) + }) + const skipped = mockInstance() + createLoad3d.mockReturnValueOnce(blocked).mockReturnValueOnce(skipped) + const controller = new AbortController() - await vi.waitFor(() => - expect(persistThumbnail).toHaveBeenCalledWith('a.glb', expect.any(Blob)) - ) + vi.useFakeTimers() + try { + const blockedRun = generateModelThumbnail('/stuck.glb', 'stuck.glb') + const skippedRun = generateModelThumbnail( + '/next.glb', + 'next.glb', + controller.signal + ) + controller.abort() + await vi.advanceTimersByTimeAsync(15_000) + + await expect(blockedRun).resolves.toEqual({ status: 'failed' }) + await expect(skippedRun).resolves.toEqual({ status: 'cancelled' }) + expect(createLoad3d).toHaveBeenCalledTimes(1) + } finally { + vi.useRealTimers() + } }) - it('returns null and still disposes when the model fails to load', async () => { + it('reports a failed render and still disposes the instance', async () => { const instance = mockInstance({ loadModel: vi.fn().mockRejectedValue(new Error('bad model')) }) @@ -66,10 +130,62 @@ describe('generateModelThumbnail', () => { const result = await generateModelThumbnail('/broken.glb', 'broken.glb') - expect(result).toBeNull() + expect(result).toEqual({ status: 'failed' }) expect(instance.remove).toHaveBeenCalledTimes(1) }) + it('redacts the request URL from a loader error before reporting', async () => { + const loaderError = new Error( + 'fetch for "https://tok:sec@host/api/view?filename=a.glb&sig=abc" responded with 404' + ) + // A stack the redacted copy cannot reproduce on its own: the frame proves + // the original stack was carried over, the token proves it was scrubbed. + loaderError.stack = + 'Error: fetch failed\n at GLTFLoader.parse (https://host/api/view?filename=a.glb&stacksecret=zzz:1:1)' + const instance = mockInstance({ + loadModel: vi.fn().mockRejectedValue(loaderError) + }) + createLoad3d.mockReturnValue(instance) + + await expect(generateModelThumbnail('/a.glb', 'a.glb')).resolves.toEqual({ + status: 'failed' + }) + + const [reported, options] = reportError.mock.calls[0] + const { message, stack } = reported as Error + expect(message).not.toContain('sig=abc') + expect(message).not.toContain('tok:sec') + expect(message).toContain('https://host/api/view') + expect(stack).toContain('GLTFLoader.parse') + expect(stack).not.toContain('stacksecret=zzz') + expect((reported as Error).cause).toBeUndefined() + expect(options).toEqual({ + errorType: 'agent_model_thumbnail_generation_failure' + }) + }) + + it('redacts a root-relative agent asset URL from a loader error', async () => { + const instance = mockInstance({ + loadModel: vi + .fn() + .mockRejectedValue( + new Error( + 'fetch for "/api/view?filename=mesh-0.glb&token=xyz" responded with 403' + ) + ) + }) + createLoad3d.mockReturnValue(instance) + + await expect( + generateModelThumbnail('/mesh-0.glb', 'mesh-0.glb') + ).resolves.toEqual({ status: 'failed' }) + + const { message } = reportError.mock.calls[0][0] as Error + expect(message).not.toContain('token=xyz') + expect(message).not.toContain('filename=mesh-0.glb') + expect(message).toContain('/api/view') + }) + it('runs generations one at a time', async () => { let releaseFirst!: () => void const first = mockInstance({ @@ -93,21 +209,47 @@ describe('generateModelThumbnail', () => { it('times out a stuck load, disposes it, and advances the queue', async () => { vi.useFakeTimers() - const stuck = mockInstance({ - loadModel: vi.fn(() => new Promise(() => {})) - }) - const next = mockInstance() - createLoad3d.mockReturnValueOnce(stuck).mockReturnValueOnce(next) + try { + const stuck = mockInstance({ + loadModel: vi.fn(() => new Promise(() => {})) + }) + const next = mockInstance() + createLoad3d.mockReturnValueOnce(stuck).mockReturnValueOnce(next) + + const stuckRun = generateModelThumbnail('/stuck.glb', 'stuck.glb') + const nextRun = generateModelThumbnail('/next.glb', 'next.glb') + await vi.waitFor(() => expect(createLoad3d).toHaveBeenCalledTimes(1)) + + await vi.advanceTimersByTimeAsync(15_000) + + await expect(stuckRun).resolves.toEqual({ status: 'failed' }) + await expect(nextRun).resolves.toEqual({ + status: 'rendered', + dataUrl: 'data:image/png;base64,thumb' + }) + expect(stuck.remove).toHaveBeenCalledTimes(1) + expect(next.loadModel).toHaveBeenCalledWith('/next.glb', undefined, { + silent: true + }) + } finally { + vi.useRealTimers() + } + }) - const stuckRun = generateModelThumbnail('/stuck.glb', 'stuck.glb') - const nextRun = generateModelThumbnail('/next.glb', 'next.glb') - await vi.waitFor(() => expect(createLoad3d).toHaveBeenCalledTimes(1)) + it('persists a supported asset thumbnail after rendering', async () => { + const instance = mockInstance() + createLoad3d.mockReturnValue(instance) + isAssetPreviewSupported.mockReturnValue(true) + const blob = new Blob(['thumbnail'], { type: 'image/png' }) + vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response(blob)) - await vi.advanceTimersByTimeAsync(15_000) + await generateModelThumbnail('/model.glb', 'model.glb') + await vi.waitFor(() => expect(persistThumbnail).toHaveBeenCalledOnce()) - await expect(stuckRun).resolves.toBeNull() - await expect(nextRun).resolves.toBe('data:image/png;base64,thumb') - expect(stuck.remove).toHaveBeenCalledTimes(1) - expect(next.loadModel).toHaveBeenCalledWith('/next.glb') + const [assetName, persistedBlob] = persistThumbnail.mock.calls[0] + expect(assetName).toBe('model.glb') + expect(persistedBlob).toBeInstanceOf(Blob) + expect(persistedBlob.type).toBe('image/png') + await expect(persistedBlob.text()).resolves.toBe('thumbnail') }) }) diff --git a/src/components/load3d/modelThumbnail.ts b/src/components/load3d/modelThumbnail.ts index fc874dd40fc..56a6ac35162 100644 --- a/src/components/load3d/modelThumbnail.ts +++ b/src/components/load3d/modelThumbnail.ts @@ -1,54 +1,219 @@ -import { withTimeout } from 'es-toolkit' +import { TimeoutError } from 'es-toolkit' import { isAssetPreviewSupported, persistThumbnail } from '@/platform/assets/utils/assetPreviewUtil' +import { reportError } from '@/platform/telemetry/reportError' let queue: Promise = Promise.resolve() const MODEL_LOAD_TIMEOUT_MS = 15_000 +/** + * `modelUrl` on the agent path is untrusted — `classifyAssetUrl` keeps the + * raw `href` from the model's markdown reply, which may carry credentials + * or a signed query string. three.js's `FileLoader` embeds the URL verbatim + * in its thrown error (`fetch for "" responded with `), and + * that error's message and stack both reach `reportError` (Sentry/Datadog), + * so both must be scrubbed before they leave this module: query strings are + * dropped and `user:pass@` credentials are stripped from any URL-shaped + * substring. Agent reply assets are also referenced by root-relative URLs + * (e.g. `/api/view?filename=mesh-0.glb`), which three.js embeds verbatim + * too, so path-shaped tokens carrying a query string are scrubbed as well. + */ +function redactUrls(text: string): string { + return text + .replace( + /https?:\/\/(?:[^\s"']*@)?[^\s"']+/g, + (match) => match.replace(/^(https?:\/\/)[^@\s"']*@/, '$1').split('?')[0] + ) + .replace(/(\/[^\s"']*)\?[^\s"']*/g, '$1') +} + +function redactedCopy(error: unknown): Error { + const source = error instanceof Error ? error : new Error(String(error)) + // A fresh Error, not `new Error(source.message)` alone, so the redacted + // copy still carries a stack that distinguishes GLTFLoader from + // FBXLoader from fetchModelData — triage needs that frame. `cause` is + // deliberately not propagated: Sentry's linkedErrorsIntegration walks it + // by default and would re-leak the unscrubbed original. + const redacted = new Error(redactUrls(source.message)) + redacted.name = source.name + if (source.stack) redacted.stack = redactUrls(source.stack) + return redacted +} + +/** + * Outcome of an offscreen thumbnail render. `cancelled` is separated from + * `failed` so a caller that walked away is not reported as a fault. + */ +export type ModelThumbnailResult = + | { status: 'rendered'; dataUrl: string } + | { status: 'cancelled' } + | { status: 'failed' } + /** * Render a model to a thumbnail data URL offscreen, without opening the - * viewer. Runs one generation at a time to bound live WebGL contexts and - * persists the result through the asset API so other surfaces pick it up. - * Resolves null when the model cannot be rendered. + * viewer. Starts one render at a time so at most one offscreen scene is + * live, and persists the result through the asset API so other surfaces + * pick it up. + * + * The whole render (see #16485, extended to cover the full body not just + * the model load) is bounded by a deadline so a stuck render cannot block + * the queue forever; a render that outlives its deadline is given up on — + * its viewer is torn down and the queue moves on — but the underlying + * transfer and parse are not abortable and run to completion in the + * background. Aborting `callerSignal` gives up the same way, and skips the + * render entirely if it has not started yet. */ export function generateModelThumbnail( modelUrl: string, - assetName: string -): Promise { - const run = queue.then(() => renderThumbnail(modelUrl, assetName)) + assetName: string, + callerSignal?: AbortSignal +): Promise { + const run = queue.then( + (): ModelThumbnailResult | Promise => + callerSignal?.aborted + ? { status: 'cancelled' } + : renderThumbnailWithTimeout(modelUrl, assetName, callerSignal) + ) queue = run.catch(() => null) return run } -async function renderThumbnail( +async function renderThumbnailWithTimeout( modelUrl: string, - assetName: string -): Promise { + assetName: string, + callerSignal?: AbortSignal +): Promise { + const abortController = new AbortController() + const cancelError = new Error('Model thumbnail generation cancelled') + let onCallerAbort: (() => void) | undefined + + const renderPromise = renderThumbnail( + modelUrl, + assetName, + abortController.signal + ) + try { - const { createLoad3d } = - await import('@/extensions/core/load3d/createLoad3d') - const load3d = createLoad3d(document.createElement('div'), { - width: 256, - height: 256, - isViewerMode: true + const dataUrl = await (callerSignal + ? Promise.race([ + renderPromise, + new Promise((_, reject) => { + onCallerAbort = () => { + abortController.abort(cancelError) + reject(cancelError) + } + callerSignal.addEventListener('abort', onCallerAbort, { + once: true + }) + }) + ]) + : renderPromise) + return { status: 'rendered', dataUrl } + } catch (error) { + // Classify by the caught error's identity, not `callerSignal.aborted` at + // catch time. One shared AbortController/signal used to cover every + // model in a group (it no longer does — see the per-url controllers in + // ReplyAssetGroup.vue), and a mutable flag read after the fact can't + // tell a genuine render fault (parse error, WebGL fault, the render + // deadline) from an unrelated abort that lands in the same tick. + // `cancelError` is the one thing only a real caller-signal abort + // produces, so compare against it directly. + if (error === cancelError) return { status: 'cancelled' } + // modelUrl is untrusted and may be embedded verbatim in three.js's own + // thrown error (FileLoader's "fetch for responded with "); + // report a redacted copy rather than the original. + reportError(redactedCopy(error), { + errorType: 'agent_model_thumbnail_generation_failure' }) - try { - await withTimeout(() => load3d.loadModel(modelUrl), MODEL_LOAD_TIMEOUT_MS) - const dataUrl = await load3d.captureThumbnail(256, 256) - if (isAssetPreviewSupported()) { - void fetch(dataUrl) - .then((response) => response.blob()) - .then((blob) => persistThumbnail(assetName, blob)) - .catch(() => {}) - } - return dataUrl - } finally { + return { status: 'failed' } + } finally { + if (onCallerAbort) callerSignal?.removeEventListener('abort', onCallerAbort) + // Swallow late rejection from the abandoned background render so an + // abort/timeout never surfaces as an unhandled rejection. + renderPromise.catch(() => {}) + } +} + +async function renderThumbnail( + modelUrl: string, + assetName: string, + signal: AbortSignal +): Promise { + // The deadline covers the whole render body, not just `loadModel`: the + // dynamic `import()` below and `captureThumbnail` run in the same queued + // task with no deadline of their own, and `queue` is module-global and + // serial, so a stalled chunk fetch or a capture that never settles would + // otherwise block every later `generateModelThumbnail` caller. + // + // `deadline` is threaded into `renderThumbnailInner` as its abort signal + // so `remove()` there fires immediately once the deadline (or the outer + // `signal`) fires, tearing down the viewer the same way a caller cancel + // does. Racing a plain timer here (rather than wrapping the whole body in + // `withTimeout`) is what lets this function's own promise settle without + // waiting on `renderThumbnailInner` — the underlying transfer/parse is + // not itself abortable and keeps running in the background once given up + // on; awaiting it here would defeat the deadline entirely. + const deadline = new AbortController() + const onSignalAbort = () => deadline.abort() + signal.addEventListener('abort', onSignalAbort, { once: true }) + let timer: ReturnType + const timedOut = new Promise((_resolve, reject) => { + timer = setTimeout(() => { + deadline.abort() + reject(new TimeoutError()) + }, MODEL_LOAD_TIMEOUT_MS) + }) + try { + return await Promise.race([ + renderThumbnailInner(modelUrl, assetName, deadline.signal), + timedOut + ]) + } finally { + clearTimeout(timer!) + deadline.abort() + signal.removeEventListener('abort', onSignalAbort) + } +} + +async function renderThumbnailInner( + modelUrl: string, + assetName: string, + signal: AbortSignal +): Promise { + const { createLoad3d } = await import('@/extensions/core/load3d/createLoad3d') + signal.throwIfAborted() + + const load3d = createLoad3d(document.createElement('div'), { + width: 256, + height: 256, + isViewerMode: true + }) + let removed = false + const remove = () => { + if (!removed) { + removed = true load3d.remove() } - } catch { - return null + } + signal.addEventListener('abort', remove, { once: true }) + + try { + await load3d.loadModel(modelUrl, undefined, { silent: true }) + signal.throwIfAborted() + const dataUrl = await load3d.captureThumbnail(256, 256) + signal.throwIfAborted() + if (isAssetPreviewSupported()) { + void fetch(dataUrl) + .then((response) => response.blob()) + .then((blob) => persistThumbnail(assetName, blob)) + .catch(() => {}) + } + return dataUrl + } finally { + signal.removeEventListener('abort', remove) + remove() } } diff --git a/src/composables/useLoad3d.test.ts b/src/composables/useLoad3d.test.ts index f36bd7d98ca..df7d39702cf 100644 --- a/src/composables/useLoad3d.test.ts +++ b/src/composables/useLoad3d.test.ts @@ -163,7 +163,7 @@ describe('useLoad3d', () => { setFOV: vi.fn(), setLightIntensity: vi.fn(), setCameraState: vi.fn(), - loadModel: vi.fn().mockResolvedValue(undefined), + loadModel: vi.fn().mockResolvedValue('loaded'), refreshViewport: vi.fn(), updateStatusMouseOnNode: vi.fn(), updateStatusMouseOnScene: vi.fn(), diff --git a/src/composables/useLoad3d.ts b/src/composables/useLoad3d.ts index c2e56334745..6cd29be48d7 100644 --- a/src/composables/useLoad3d.ts +++ b/src/composables/useLoad3d.ts @@ -834,7 +834,10 @@ export const useLoad3d = (nodeOrRef: MaybeRef) => { ) loadingMessage.value = t('load3d.loadingModel') - await load3d.loadModel(modelUrl) + const outcome = await load3d.loadModel(modelUrl) + // A cancelled/failed/empty load never reached setupModel, so committing + // the widget here would point the node at a model that isn't showing. + if (outcome !== 'loaded') return const modelWidget = node.widgets?.find((w) => w.name === 'model_file') diff --git a/src/composables/useLoad3dViewer.test.ts b/src/composables/useLoad3dViewer.test.ts index ed8aee9aedb..ac957081d0a 100644 --- a/src/composables/useLoad3dViewer.test.ts +++ b/src/composables/useLoad3dViewer.test.ts @@ -7,6 +7,7 @@ import Load3dUtils from '@/extensions/core/load3d/Load3dUtils' import { createLoad3d } from '@/extensions/core/load3d/createLoad3d' import type { LGraph } from '@/lib/litegraph/src/LGraph' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { useToastStore } from '@/platform/updates/common/toastStore' import { useLoad3dService } from '@/services/load3dService' import { createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils' @@ -129,7 +130,7 @@ describe('useLoad3dViewer', () => { forceRender: vi.fn(), remove: vi.fn(), setTargetSize: vi.fn(), - loadModel: vi.fn().mockResolvedValue(undefined), + loadModel: vi.fn().mockResolvedValue('loaded'), captureThumbnail: vi.fn().mockResolvedValue('data:image/png;base64,x'), setCameraState: vi.fn(), addEventListener: vi.fn(), @@ -177,7 +178,21 @@ describe('useLoad3dViewer', () => { } as Load3d['modelManager'], setBackgroundImage: vi.fn().mockResolvedValue(undefined), setBackgroundRenderMode: vi.fn(), - forceRender: vi.fn() + forceRender: vi.fn(), + // captureAdapterFlags(source) reads these during initializeViewer. + // Without them it throws and the rest of initialization never runs. + isSplatModel: vi.fn().mockReturnValue(false), + isPlyModel: vi.fn().mockReturnValue(false), + getSourceFormat: vi.fn().mockReturnValue('glb'), + getCurrentModelCapabilities: vi.fn().mockReturnValue({ + fitToViewer: true, + requiresMaterialRebuild: false, + gizmoTransform: true, + lighting: true, + exportable: true, + materialModes: ['original', 'normal', 'wireframe'], + fitTargetSize: 5 + }) } vi.mocked(Load3d).mockImplementation(function () { @@ -652,6 +667,9 @@ describe('useLoad3dViewer', () => { 'normal', 'wireframe' ]) + // Captured from the source: copyLoad3dState clones the object graph + // without carrying originalURL, so the new instance reports none. + expect(viewer.sourceFormat.value).toBe('glb') vi.mocked(mockLoad3d.isSplatModel!).mockReturnValueOnce(true) vi.mocked(mockLoad3d.getCurrentModelCapabilities!).mockReturnValueOnce({ @@ -676,6 +694,53 @@ describe('useLoad3dViewer', () => { expect([...viewer.materialModes.value]).toEqual([]) }) + it('commits the widget value when the dropped model loads', async () => { + vi.mocked(Load3dUtils.uploadFile).mockResolvedValueOnce('3d/dropped.glb') + const modelWidget = { name: 'model_file', value: 'old.glb', options: {} } + mockNode.widgets = [modelWidget as unknown as IBaseWidget] + + const viewer = useLoad3dViewer(mockNode) + const containerRef = document.createElement('div') + await viewer.initializeViewer(containerRef, mockSourceLoad3d as Load3d) + + await viewer.handleModelDrop(new File([''], 'dropped.glb')) + + expect(modelWidget.value).toBe('3d/dropped.glb') + }) + + it('does not commit the widget value when the dropped model load is cancelled', async () => { + vi.mocked(Load3dUtils.uploadFile).mockResolvedValueOnce('3d/dropped.glb') + const modelWidget = { name: 'model_file', value: 'old.glb', options: {} } + mockNode.widgets = [modelWidget as unknown as IBaseWidget] + + const viewer = useLoad3dViewer(mockNode) + const containerRef = document.createElement('div') + await viewer.initializeViewer(containerRef, mockSourceLoad3d as Load3d) + vi.mocked(mockLoad3d.loadModel!).mockResolvedValueOnce('cancelled') + + await viewer.handleModelDrop(new File([''], 'dropped.glb')) + + expect(modelWidget.value).toBe('old.glb') + expect(mockLoad3d.getCurrentModelCapabilities).not.toHaveBeenCalled() + expect(mockToastStore.addAlert).not.toHaveBeenCalled() + }) + + it('refreshes the capability refs but keeps the widget value when the dropped model fails to load', async () => { + vi.mocked(Load3dUtils.uploadFile).mockResolvedValueOnce('3d/dropped.glb') + const modelWidget = { name: 'model_file', value: 'old.glb', options: {} } + mockNode.widgets = [modelWidget as unknown as IBaseWidget] + + const viewer = useLoad3dViewer(mockNode) + const containerRef = document.createElement('div') + await viewer.initializeViewer(containerRef, mockSourceLoad3d as Load3d) + vi.mocked(mockLoad3d.loadModel!).mockResolvedValueOnce('failed') + + await viewer.handleModelDrop(new File([''], 'dropped.glb')) + + expect(modelWidget.value).toBe('old.glb') + expect(viewer.sourceFormat.value).toBeNull() + }) + it('alerts and does not call loadModel when there is no active load3d instance', async () => { const viewer = useLoad3dViewer(mockNode) @@ -793,6 +858,42 @@ describe('useLoad3dViewer', () => { expect(mockLoad3d.captureThumbnail).toHaveBeenCalledWith(256, 256) }) + it('abandons the standalone viewer when a newer load supersedes it', async () => { + isAssetPreviewSupported.mockReturnValue(true) + vi.mocked(mockLoad3d.loadModel!).mockResolvedValueOnce('cancelled') + const viewer = useLoad3dViewer() + const containerRef = document.createElement('div') + + await viewer.initializeStandaloneViewer( + containerRef, + '/api/view?filename=mesh.glb&type=output' + ) + await nextTick() + + expect(mockLoad3d.getCurrentModelCapabilities).not.toHaveBeenCalled() + expect(viewer.isPreview.value).toBe(false) + }) + + it('still restores the viewer config when the standalone model fails to load', async () => { + isAssetPreviewSupported.mockReturnValue(true) + vi.mocked(mockLoad3d.loadModel!).mockResolvedValueOnce('failed') + const viewer = useLoad3dViewer() + const containerRef = document.createElement('div') + + await viewer.initializeStandaloneViewer( + containerRef, + '/api/view?filename=mesh.glb&type=output' + ) + await nextTick() + + // backgroundColor starts as '' and restoreStandaloneConfig is its only + // initialiser, so skipping it would bind '' into the colour input. + expect(viewer.backgroundColor.value).not.toBe('') + expect(mockLoad3d.getCurrentModelCapabilities).toHaveBeenCalled() + expect(mockLoad3d.captureThumbnail).not.toHaveBeenCalled() + expect(persistThumbnail).not.toHaveBeenCalled() + }) + it('skips thumbnail persistence when the asset API is unavailable', async () => { const viewer = useLoad3dViewer() const containerRef = document.createElement('div') @@ -805,6 +906,38 @@ describe('useLoad3dViewer', () => { }) describe('standalone mode persistence', () => { + it('does not write a failed model\u2019s config into the previous model\u2019s slot', async () => { + const viewer = useLoad3dViewer() + const containerRef = document.createElement('div') + + await viewer.initializeStandaloneViewer(containerRef, 'kept.glb') + viewer.backgroundColor.value = '#ff0000' + viewer.showGrid.value = false + await nextTick() + + vi.mocked(mockLoad3d.loadModel!).mockResolvedValueOnce('failed') + await viewer.initializeStandaloneViewer(containerRef, 'broken.glb') + expect(viewer.backgroundColor.value).toBe('#282828') + + await viewer.initializeStandaloneViewer(containerRef, 'other.glb') + await viewer.initializeStandaloneViewer(containerRef, 'kept.glb') + + expect(viewer.backgroundColor.value).toBe('#ff0000') + expect(viewer.showGrid.value).toBe(false) + }) + + it('abandons a superseded standalone reload without touching the viewer', async () => { + const viewer = useLoad3dViewer() + const containerRef = document.createElement('div') + await viewer.initializeStandaloneViewer(containerRef, 'kept.glb') + vi.mocked(mockLoad3d.getCurrentModelCapabilities!).mockClear() + + vi.mocked(mockLoad3d.loadModel!).mockResolvedValueOnce('cancelled') + await viewer.initializeStandaloneViewer(containerRef, 'superseded.glb') + + expect(mockLoad3d.getCurrentModelCapabilities).not.toHaveBeenCalled() + }) + it('should save and restore configuration in standalone mode', async () => { const viewer = useLoad3dViewer() const containerRef = document.createElement('div') diff --git a/src/composables/useLoad3dViewer.ts b/src/composables/useLoad3dViewer.ts index fb0dc14ccdb..500ed29e65b 100644 --- a/src/composables/useLoad3dViewer.ts +++ b/src/composables/useLoad3dViewer.ts @@ -491,15 +491,18 @@ export const useLoad3dViewer = (node?: LGraphNode) => { load3d.updateStatusMouseOnViewer(true) } - await load3d.loadModel(modelUrl) + const outcome = await load3d.loadModel(modelUrl) + if (outcome === 'cancelled') return currentModelUrl = modelUrl restoreStandaloneConfig(modelUrl) captureAdapterFlags(load3d) isPreview.value = true + // One-time wiring for this composable instance: later loads go through + // loadStandaloneModel, which never registers these. setupAnimationEvents() - persistStandaloneThumbnail(modelUrl) + if (outcome === 'loaded') persistStandaloneThumbnail(modelUrl) } catch (error) { console.error('Error initializing standalone 3D viewer:', error) useToastStore().addAlert(t('toastMessages.failedToLoadModel')) @@ -527,11 +530,12 @@ export const useLoad3dViewer = (node?: LGraphNode) => { try { saveStandaloneConfig() - await load3d.loadModel(modelUrl) + const outcome = await load3d.loadModel(modelUrl) + if (outcome === 'cancelled') return currentModelUrl = modelUrl restoreStandaloneConfig(modelUrl) captureAdapterFlags(load3d) - persistStandaloneThumbnail(modelUrl) + if (outcome === 'loaded') persistStandaloneThumbnail(modelUrl) } catch (error) { console.error('Error loading model in standalone viewer:', error) useToastStore().addAlert('Failed to load 3D model') @@ -821,9 +825,11 @@ export const useLoad3dViewer = (node?: LGraphNode) => { ) ) - await load3d.loadModel(modelUrl) + const outcome = await load3d.loadModel(modelUrl) + if (outcome === 'cancelled') return captureAdapterFlags(load3d) + if (outcome !== 'loaded') return const modelWidget = node?.widgets?.find((w) => w.name === 'model_file') if (modelWidget) { diff --git a/src/extensions/core/load3d/Load3d.test.ts b/src/extensions/core/load3d/Load3d.test.ts index bc482261bce..431a751aa8f 100644 --- a/src/extensions/core/load3d/Load3d.test.ts +++ b/src/extensions/core/load3d/Load3d.test.ts @@ -5,7 +5,8 @@ import type { Load3dDeps } from '@/extensions/core/load3d/Load3d' import Load3d from '@/extensions/core/load3d/Load3d' import type { CameraState, - GizmoMode + GizmoMode, + LoadModelOutcome } from '@/extensions/core/load3d/interfaces' import type { PointerNdcSource } from '@/extensions/core/load3d/load3dViewport' @@ -954,7 +955,9 @@ describe('Load3d', () => { })) const setCameraState = vi.fn() const getCurrentCameraType = vi.fn(() => 'perspective' as const) - const loaderLoadModel = vi.fn().mockResolvedValue(undefined) + const loaderLoadModel = vi.fn<() => Promise>( + async () => 'loaded' + ) Object.assign(ctx.load3d, { cameraManager: { ...ctx.cameraManager, diff --git a/src/extensions/core/load3d/Load3d.ts b/src/extensions/core/load3d/Load3d.ts index b484eb6702e..d7b149f6166 100644 --- a/src/extensions/core/load3d/Load3d.ts +++ b/src/extensions/core/load3d/Load3d.ts @@ -18,6 +18,7 @@ import type { GizmoMode, Load3DOptions, LoadModelOptions, + LoadModelOutcome, MaterialMode, Model3DTransform, UpDirection @@ -62,7 +63,7 @@ class Load3d extends Viewport3d { gizmoManager: GizmoManager adapterRef: AdapterRef - private loadingPromise: Promise | null = null + private loadingPromise: Promise | null = null private _loadGeneration: number = 0 private hasLoadedModel: boolean = false @@ -331,7 +332,7 @@ class Load3d extends Viewport3d { url: string, originalFileName?: string, options?: LoadModelOptions - ): Promise { + ): Promise { this._loadGeneration += 1 if (this.loadingPromise) { @@ -349,7 +350,7 @@ class Load3d extends Viewport3d { } async whenLoadIdle(): Promise { - let last: Promise | null = null + let last: Promise | null = null while (this.loadingPromise && this.loadingPromise !== last) { last = this.loadingPromise try { @@ -362,7 +363,7 @@ class Load3d extends Viewport3d { url: string, originalFileName?: string, options?: LoadModelOptions - ): Promise { + ): Promise { const shouldRetainView = this.hasLoadedModel const savedCameraState = shouldRetainView ? this.cameraManager.getCameraState() @@ -376,29 +377,50 @@ class Load3d extends Viewport3d { this.modelManager.clearModel() this.animationManager.dispose() - await this.loaderManager.loadModel(url, originalFileName, options) - - if (this.modelManager.currentModel) { - this.animationManager.setupModelAnimations( - this.modelManager.currentModel, - this.modelManager.originalModel + try { + const outcome = await this.loaderManager.loadModel( + url, + originalFileName, + options ) - this.hasLoadedModel = true - } - if (savedCameraState) { - if ( - savedCameraState.cameraType !== - this.cameraManager.getCurrentCameraType() - ) { - this.toggleCamera(savedCameraState.cameraType) + // A cancelled/failed/empty load clears adapterRef up-front in + // LoaderManager but never calls setupModel, so the viewer's + // capability flags (format, gizmo, export) would otherwise keep + // advertising the *previous* model over an emptied scene. Only a + // successful load runs the post-load camera/animation restore below — + // those touch state (`currentModel`, camera) that a cancelled load may + // have left mid-teardown. + if (outcome !== 'loaded') return outcome + + if (this.modelManager.currentModel) { + this.animationManager.setupModelAnimations( + this.modelManager.currentModel, + this.modelManager.originalModel + ) + this.hasLoadedModel = true } - this.cameraManager.setCameraState(savedCameraState) - } - this.handleResize() + if (savedCameraState) { + if ( + savedCameraState.cameraType !== + this.cameraManager.getCurrentCameraType() + ) { + this.toggleCamera(savedCameraState.cameraType) + } + this.cameraManager.setCameraState(savedCameraState) + } + + this.handleResize() - this.loadingPromise = null + return outcome + } finally { + // Must run even when loaderManager.loadModel throws (the `{ silent: + // true }` path) — otherwise loadingPromise stays permanently non-null + // and every subsequent loadModel call waits on this settled promise + // forever without progressing. + this.loadingPromise = null + } } isSplatModel(): boolean { diff --git a/src/extensions/core/load3d/LoaderManager.test.ts b/src/extensions/core/load3d/LoaderManager.test.ts index c2d7eb800a0..82f618f5552 100644 --- a/src/extensions/core/load3d/LoaderManager.test.ts +++ b/src/extensions/core/load3d/LoaderManager.test.ts @@ -590,6 +590,182 @@ describe('LoaderManager', () => { expect(addAlert).toHaveBeenCalledWith('toastMessages.errorLoadingModel') }) + it('rejects with the load failure instead of alerting when silent is set', async () => { + const { lm } = makeLoaderManager() + meshLoad.mockRejectedValueOnce(new Error('parse failure: bad header')) + vi.spyOn(console, 'error').mockImplementation(() => {}) + + await expect( + lm.loadModel('api/view?filename=cube.glb', undefined, { silent: true }) + ).rejects.toThrow('parse failure: bad header') + expect(addAlert).not.toHaveBeenCalled() + }) + + it('rejects on an undeterminable file type instead of alerting when silent is set', async () => { + const { lm } = makeLoaderManager() + vi.spyOn(console, 'error').mockImplementation(() => {}) + + await expect( + lm.loadModel('api/view?type=output', undefined, { silent: true }) + ).rejects.toThrow(/Unknown model file type/) + expect(addAlert).not.toHaveBeenCalled() + }) + + it('rejects when no adapter claims the extension and silent is set', async () => { + const { lm, modelManager } = makeLoaderManager() + + await expect( + lm.loadModel('api/view?filename=scene.usdz', undefined, { + silent: true + }) + ).rejects.toThrow(/No model was produced/) + expect(modelManager.setupModel).not.toHaveBeenCalled() + expect(addAlert).not.toHaveBeenCalled() + }) + + it('rejects when the URL carries no filename and silent is set', async () => { + const { lm } = makeLoaderManager() + const consoleError = vi + .spyOn(console, 'error') + .mockImplementation(() => {}) + + await expect( + lm.loadModel('api/view?type=output', 'scene.glb', { silent: true }) + ).rejects.toThrow(/No model was produced/) + expect(addAlert).not.toHaveBeenCalled() + // The URL may carry credentials (e.g. a signed query string) — a + // silent load must never log it to the console on the caller's + // behalf, matching the "never embeds the requested URL" guarantee + // for the thrown-error path below. + expect(consoleError).not.toHaveBeenCalled() + }) + + it('never embeds the requested URL in a silent load error, only the file type', async () => { + const { lm } = makeLoaderManager() + + await expect( + lm.loadModel( + 'api/view?filename=secret.usdz&token=abc123&subfolder=priv', + undefined, + { silent: true } + ) + ).rejects.toSatisfy((error: Error) => { + expect(error.message).not.toMatch(/token=abc123|secret|priv/) + return true + }) + }) + + it('disposes a stale result on a live (non-disposed) manager and never publishes its setOriginalModel write', async () => { + const { lm, modelManager } = makeLoaderManager() + + let resolveFirst!: (value: THREE.Object3D) => void + const firstLoad = new Promise((r) => { + resolveFirst = r + }) + const geometry = new THREE.BufferGeometry() + const material = new THREE.MeshBasicMaterial() + const firstModel = new THREE.Mesh(geometry, material) + const disposeGeometry = vi.spyOn(geometry, 'dispose') + const disposeMaterial = vi.spyOn(material, 'dispose') + const secondModel = new THREE.Object3D() + + meshLoad.mockImplementationOnce((ctx) => + firstLoad.then((object) => { + // Adapters call setOriginalModel synchronously during load(), + // before loadModel can know this result will be superseded. + ctx.setOriginalModel(object) + return loadResult(object) + }) + ) + meshLoad.mockResolvedValueOnce(loadResult(secondModel)) + + const firstPromise = lm.loadModel('api/view?filename=first.glb') + const secondPromise = lm.loadModel('api/view?filename=second.glb') + + resolveFirst(firstModel) + await Promise.all([firstPromise, secondPromise]) + + // The manager was never disposed (still live) — the old narrowed + // guard (`result && this.disposed`) would have skipped disposal here + // and leaked the stale geometry/material. + expect(disposeGeometry).toHaveBeenCalledOnce() + expect(disposeMaterial).toHaveBeenCalledOnce() + // The stale load's setOriginalModel write must never have landed — + // createLoadContext gates it on loadId. + expect(modelManager.setOriginalModel).not.toHaveBeenCalledWith(firstModel) + }) + + it('disposes textures held by a stale result material, not just the material itself', async () => { + // Regression coverage: Material.dispose() releases GPU program/shader + // state but not the textures it references — a stale result's + // material was disposed but its map/normalMap/etc. textures were + // leaked, retaining full-resolution texture memory per superseded + // load. + const { lm, modelManager } = makeLoaderManager() + + let resolveFirst!: (value: THREE.Object3D) => void + const firstLoad = new Promise((r) => { + resolveFirst = r + }) + const geometry = new THREE.BufferGeometry() + const map = new THREE.Texture() + const normalMap = new THREE.Texture() + const material = new THREE.MeshStandardMaterial({ map, normalMap }) + const firstModel = new THREE.Mesh(geometry, material) + const disposeMap = vi.spyOn(map, 'dispose') + const disposeNormalMap = vi.spyOn(normalMap, 'dispose') + const secondModel = new THREE.Object3D() + + meshLoad.mockImplementationOnce((ctx) => + firstLoad.then((object) => { + ctx.setOriginalModel(object) + return loadResult(object) + }) + ) + meshLoad.mockResolvedValueOnce(loadResult(secondModel)) + + const firstPromise = lm.loadModel('api/view?filename=first.glb') + const secondPromise = lm.loadModel('api/view?filename=second.glb') + + resolveFirst(firstModel) + await Promise.all([firstPromise, secondPromise]) + + expect(disposeMap).toHaveBeenCalledOnce() + expect(disposeNormalMap).toHaveBeenCalledOnce() + expect(modelManager.setOriginalModel).not.toHaveBeenCalledWith(firstModel) + }) + + it('does not dispose textures on the shared standardMaterial', async () => { + const { lm, modelManager } = makeLoaderManager() + + let resolveFirst!: (value: THREE.Object3D) => void + const firstLoad = new Promise((r) => { + resolveFirst = r + }) + const geometry = new THREE.BufferGeometry() + const sharedMap = new THREE.Texture() + modelManager.standardMaterial.map = sharedMap + const firstModel = new THREE.Mesh(geometry, modelManager.standardMaterial) + const disposeSharedMap = vi.spyOn(sharedMap, 'dispose') + const secondModel = new THREE.Object3D() + + meshLoad.mockImplementationOnce((ctx) => + firstLoad.then((object) => { + ctx.setOriginalModel(object) + return loadResult(object) + }) + ) + meshLoad.mockResolvedValueOnce(loadResult(secondModel)) + + const firstPromise = lm.loadModel('api/view?filename=first.glb') + const secondPromise = lm.loadModel('api/view?filename=second.glb') + + resolveFirst(firstModel) + await Promise.all([firstPromise, secondPromise]) + + expect(disposeSharedMap).not.toHaveBeenCalled() + }) + it('discards the result of a stale load when a newer one has started', async () => { const { lm, modelManager, eventManager } = makeLoaderManager() @@ -622,6 +798,62 @@ describe('LoaderManager', () => { expect(endEmits).toHaveLength(1) }) + it('drops and disposes an in-flight load when disposed', async () => { + const { lm, modelManager } = makeLoaderManager() + let resolveLoad!: (value: ReturnType) => void + const pendingLoad = new Promise>( + (resolve) => { + resolveLoad = resolve + } + ) + const geometry = new THREE.BufferGeometry() + const material = new THREE.MeshBasicMaterial() + const model = new THREE.Mesh(geometry, material) + const disposeGeometry = vi.spyOn(geometry, 'dispose') + const disposeMaterial = vi.spyOn(material, 'dispose') + meshLoad.mockReturnValueOnce(pendingLoad) + + const load = lm.loadModel('api/view?filename=cube.glb') + lm.dispose() + resolveLoad(loadResult(model)) + + await expect(load).resolves.toBe('cancelled') + expect(modelManager.setupModel).not.toHaveBeenCalled() + expect(disposeGeometry).toHaveBeenCalledOnce() + expect(disposeMaterial).toHaveBeenCalledOnce() + expect(addAlert).not.toHaveBeenCalled() + }) + + it('drops a load that fails after disposal without alerting', async () => { + const { lm, modelManager } = makeLoaderManager() + let rejectLoad!: (reason: Error) => void + meshLoad.mockReturnValueOnce( + new Promise((_, reject) => { + rejectLoad = reject + }) + ) + + const load = lm.loadModel('api/view?filename=cube.glb') + lm.dispose() + rejectLoad(new Error('connection reset')) + + await expect(load).resolves.toBe('cancelled') + expect(modelManager.setupModel).not.toHaveBeenCalled() + expect(addAlert).not.toHaveBeenCalled() + }) + + it('drops a load started after disposal without alerting', async () => { + const { lm, modelManager } = makeLoaderManager() + lm.dispose() + + await expect(lm.loadModel('api/view?filename=cube.glb')).resolves.toBe( + 'cancelled' + ) + expect(modelManager.setupModel).not.toHaveBeenCalled() + expect(meshLoad).not.toHaveBeenCalled() + expect(addAlert).not.toHaveBeenCalled() + }) + it('logs and drops the load when the URL is missing a filename param', async () => { const { lm, modelManager } = makeLoaderManager() const consoleError = vi diff --git a/src/extensions/core/load3d/LoaderManager.ts b/src/extensions/core/load3d/LoaderManager.ts index a160297ae68..f525836f49e 100644 --- a/src/extensions/core/load3d/LoaderManager.ts +++ b/src/extensions/core/load3d/LoaderManager.ts @@ -1,4 +1,4 @@ -import type * as THREE from 'three' +import * as THREE from 'three' import { t } from '@/i18n' import { useToastStore } from '@/platform/updates/common/toastStore' @@ -9,13 +9,15 @@ import type { AdapterRef, ModelAdapter, ModelAdapterCapabilities, - ModelLoadContext + ModelLoadContext, + ModelLoadResult } from './ModelAdapter' import { PointCloudModelAdapter } from './PointCloudModelAdapter' import { SplatModelAdapter } from './SplatModelAdapter' import type { EventManagerInterface, LoadModelOptions, + LoadModelOutcome, LoaderManagerInterface, ModelManagerInterface } from './interfaces' @@ -40,6 +42,23 @@ function isNotFoundError(error: unknown): boolean { return /\b404\b/.test(error.message) } +/** + * Materials own their maps (`map`, `normalMap`, `roughnessMap`, + * `metalnessMap`, `aoMap`, `emissiveMap`, `alphaMap`, `bumpMap`, + * `displacementMap`, `envMap`, `clearcoatMap`, ...) but `Material.dispose()` + * only releases GPU program/shader state, not the textures it references. + * `disposeLoadResult` disposes non-shared materials on every load-generation + * change, so leaving their textures alive would retain full-resolution + * texture memory for every superseded model. Walk own enumerable properties + * rather than a hardcoded map-name list so newly added map types (e.g. a + * future `sheenColorMap`) are covered without touching this function. + */ +function disposeMaterialTextures(material: THREE.Material): void { + for (const value of Object.values(material)) { + if (value instanceof THREE.Texture) value.dispose() + } +} + /** * Default adapter set: mesh + splat + pointCloud. Each adapter declares the * file extensions it owns. For shared extensions (.ply), the adapter with an @@ -60,6 +79,7 @@ export class LoaderManager implements LoaderManagerInterface { private readonly adapters: ModelAdapter[] private readonly adapterRef: AdapterRef private currentLoadId: number = 0 + private disposed = false constructor( modelManager: ModelManagerInterface, @@ -77,15 +97,21 @@ export class LoaderManager implements LoaderManagerInterface { return this.adapterRef.current } - init(): void {} + init(): void { + this.disposed = false + } - dispose(): void {} + dispose(): void { + this.disposed = true + this.currentLoadId += 1 + } async loadModel( url: string, originalFileName?: string, options?: LoadModelOptions - ): Promise { + ): Promise { + if (this.disposed) return 'cancelled' const loadId = ++this.currentLoadId try { @@ -112,17 +138,34 @@ export class LoaderManager implements LoaderManagerInterface { } if (!fileExtension) { + // The agent path may pass an untrusted, credential-bearing URL — + // never embed it in a thrown/reported error (see the redaction in + // the catch block and in modelThumbnail.ts's reportError call). + if (options?.silent) throw new Error('Unknown model file type') useToastStore().addAlert(t('toastMessages.couldNotDetermineFileType')) - return + return 'empty' } - const result = await this.loadModelInternal(url, fileExtension) + const result = await this.loadModelInternal( + url, + fileExtension, + loadId, + options?.silent + ) if (loadId !== this.currentLoadId) { - // A newer loadModel has superseded us — do not publish our adapter - // and do not setup the model. Whichever load is current owns the - // shared state. - return + // A newer loadModel has superseded us. createLoadContext gates on + // loadId, so a superseded adapter's setOriginalModel / + // registerOriginalMaterial writes never landed — the result never + // entered the scene (setupModel is skipped below) and nothing else + // can reach it, so it is always safe to dispose here regardless of + // whether the manager itself has been torn down. + if (result) this.disposeLoadResult(result) + return 'cancelled' + } + + if (!result && options?.silent) { + throw new Error(`No model was produced for type: ${fileExtension}`) } if (result) { @@ -135,17 +178,44 @@ export class LoaderManager implements LoaderManagerInterface { } this.eventManager.emitEvent('modelLoadingEnd', null) + return result ? 'loaded' : 'empty' } catch (error) { - if (loadId === this.currentLoadId) { - this.eventManager.emitEvent('modelLoadingEnd', null) - console.error('Error loading model:', error) - if (!(options?.silentOnNotFound && isNotFoundError(error))) { - useToastStore().addAlert(t('toastMessages.errorLoadingModel')) - } + if (loadId !== this.currentLoadId) return 'cancelled' + this.eventManager.emitEvent('modelLoadingEnd', null) + // A silent load's error (and the untrusted URL it may embed, e.g. + // from three.js's FileLoader "fetch for responded with ...") + // is the caller's to report — logging it here on their behalf would + // write it to the console unredacted regardless of what the caller + // does with the rethrown error. + if (options?.silent) throw error + console.error('Error loading model:', error) + if (!(options?.silentOnNotFound && isNotFoundError(error))) { + useToastStore().addAlert(t('toastMessages.errorLoadingModel')) } + return 'failed' } } + private disposeLoadResult( + result: ModelLoadResult & { adapter: ModelAdapter } + ): void { + result.adapter.disposeModel?.(result.object) + result.object.traverse((child) => { + if (!(child instanceof THREE.Mesh || child instanceof THREE.Points)) + return + child.geometry?.dispose() + const materials = Array.isArray(child.material) + ? child.material + : [child.material] + for (const material of materials) { + if (!material || material === this.modelManager.standardMaterial) + continue + disposeMaterialTextures(material) + material.dispose() + } + }) + } + private async pickAdapter( extension: string, fetchBytes: () => Promise @@ -160,12 +230,22 @@ export class LoaderManager implements LoaderManagerInterface { return null } - private createLoadContext(): ModelLoadContext { + private createLoadContext(loadId: number): ModelLoadContext { const mm = this.modelManager + // Adapters call setOriginalModel / registerOriginalMaterial synchronously + // during adapter.load(), before loadModel can check whether this load is + // still current. Gate those writes on identity here so a superseded + // load's result can never land in modelManager — that is what makes it + // safe to unconditionally dispose a stale result afterward (see the + // loadId !== this.currentLoadId branch in loadModel). + const isCurrent = () => loadId === this.currentLoadId return { - setOriginalModel: (model) => mm.setOriginalModel(model), - registerOriginalMaterial: (mesh, material) => - mm.originalMaterials.set(mesh, material), + setOriginalModel: (model) => { + if (isCurrent()) mm.setOriginalModel(model) + }, + registerOriginalMaterial: (mesh, material) => { + if (isCurrent()) mm.originalMaterials.set(mesh, material) + }, get standardMaterial() { return mm.standardMaterial }, @@ -177,7 +257,9 @@ export class LoaderManager implements LoaderManagerInterface { private async loadModelInternal( url: string, - fileExtension: string + fileExtension: string, + loadId: number, + silent?: boolean ): Promise<{ object: THREE.Object3D adapter: ModelAdapter @@ -187,7 +269,10 @@ export class LoaderManager implements LoaderManagerInterface { const filename = params.get('filename') if (!filename) { - console.error('Missing filename in URL:', url) + // Silent loads may carry an untrusted, credential-bearing URL (see the + // redaction note in loadModel's catch block) — never log it here on + // the caller's behalf. + if (!silent) console.error('Missing filename in URL:', url) return null } @@ -211,7 +296,7 @@ export class LoaderManager implements LoaderManagerInterface { if (!adapter) return null const loadResult = await adapter.load( - this.createLoadContext(), + this.createLoadContext(loadId), path, filename, fetchBytes diff --git a/src/extensions/core/load3d/interfaces.ts b/src/extensions/core/load3d/interfaces.ts index 8fedb5395e8..61590f2d424 100644 --- a/src/extensions/core/load3d/interfaces.ts +++ b/src/extensions/core/load3d/interfaces.ts @@ -249,6 +249,13 @@ export interface LoadModelOptions { * (e.g. shared workflows on a fresh machine). */ silentOnNotFound?: boolean + /** + * When true, raise no toast and reject with the underlying error + * instead. Use for offscreen renders the viewer never asked for (e.g. + * thumbnail generation), which have no surface to show a toast on and + * need the real cause for their own reporting. + */ + silent?: boolean } export interface SceneOverlay { @@ -259,6 +266,26 @@ export interface SceneOverlay { dispose(): void } +/** + * Outcome of a `loadModel` call. A caller that ignores this and assumes + * success can run its post-load steps (camera restore, widget commit, + * capability capture) against a torn-down manager or a scene that never + * received a model: + * - `'loaded'` — a model was fetched, parsed, and handed to `setupModel`. + * - `'cancelled'` — a newer `loadModel` or `dispose()` superseded this load; + * the manager may already be torn down. + * - `'empty'` — no adapter claimed the file, or the URL had no filename; + * `{ silent: true }` throws instead of returning this. + * - `'failed'` — the load threw (network, parse, or an unknown file type). + * + * `Load3d.loadModel` awaits any prior `loadingPromise` before starting, but + * that only serialises callers pairwise: three overlapping loads all observe + * the same in-flight promise, so the middle one is superseded and returns + * `'cancelled'`. Disposal is therefore not the only source of it — any + * caller recording per-load state must check the outcome before writing. + */ +export type LoadModelOutcome = 'loaded' | 'cancelled' | 'empty' | 'failed' + export interface LoaderManagerInterface { init(): void dispose(): void @@ -266,5 +293,5 @@ export interface LoaderManagerInterface { url: string, originalFileName?: string, options?: LoadModelOptions - ): Promise + ): Promise } diff --git a/src/workbench/extensions/agent/components/agent/message/ReplyAssetGroup.test.ts b/src/workbench/extensions/agent/components/agent/message/ReplyAssetGroup.test.ts index df3509e7085..42dff3c8d1e 100644 --- a/src/workbench/extensions/agent/components/agent/message/ReplyAssetGroup.test.ts +++ b/src/workbench/extensions/agent/components/agent/message/ReplyAssetGroup.test.ts @@ -2,6 +2,7 @@ import { render, screen, waitFor } from '@testing-library/vue' import userEvent from '@testing-library/user-event' import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { ModelThumbnailResult } from '@/components/load3d/modelThumbnail' import { i18n } from '@/i18n' import type { ReplyAsset } from '../../../utils/replyAssets' @@ -26,12 +27,21 @@ vi.mock('@/platform/assets/utils/assetPreviewUtil', () => ({ })) const generateModelThumbnail = vi.hoisted(() => - vi.fn(async (): Promise => null) + vi.fn( + async ( + _modelUrl: string, + _assetName: string, + _callerSignal?: AbortSignal + ): Promise => ({ status: 'failed' }) + ) ) vi.mock('@/components/load3d/modelThumbnail', () => ({ generateModelThumbnail })) +const reportError = vi.hoisted(() => vi.fn()) +vi.mock('@/platform/telemetry/reportError', () => ({ reportError })) + const image = (n: number): ReplyAsset => ({ url: `https://x/i${n}.png`, filename: `i${n}.png`, @@ -85,7 +95,8 @@ describe('ReplyAssetGroup', () => { isAssetPreviewSupported.mockReset().mockReturnValue(false) findServerPreviewUrl.mockReset().mockResolvedValue(null) findOutputAsset.mockReset().mockResolvedValue(undefined) - generateModelThumbnail.mockReset().mockResolvedValue(null) + generateModelThumbnail.mockReset().mockResolvedValue({ status: 'failed' }) + reportError.mockReset() }) it('T-09 / PM-652 / FE-1326 renders image and video previews inline', () => { @@ -207,15 +218,185 @@ describe('ReplyAssetGroup', () => { it('generates a thumbnail offscreen when the server has none', async () => { isAssetPreviewSupported.mockReturnValue(true) - generateModelThumbnail.mockResolvedValue('data:image/png;base64,gen') + generateModelThumbnail.mockResolvedValue({ + status: 'rendered', + dataUrl: 'data:image/png;base64,gen' + }) renderGroup([model]) const thumb = await screen.findByRole('img', { name: 'mesh.glb' }) expect(thumb).toHaveAttribute('src', 'data:image/png;base64,gen') expect(generateModelThumbnail).toHaveBeenCalledWith( 'https://x/mesh.glb', - 'mesh.glb' + 'mesh.glb', + expect.any(AbortSignal) + ) + }) + + it('does not generate a thumbnail after unmounting during preview lookup', async () => { + isAssetPreviewSupported.mockReturnValue(true) + let resolvePreview!: (value: string | null) => void + findServerPreviewUrl.mockReturnValueOnce( + new Promise((resolve) => { + resolvePreview = resolve + }) + ) + const { unmount } = renderGroup([model]) + + unmount() + resolvePreview(null) + await Promise.resolve() + + expect(generateModelThumbnail).not.toHaveBeenCalled() + }) + + it('reports preview lookup failures and retries generation on a timed pass', async () => { + isAssetPreviewSupported.mockReturnValue(true) + findServerPreviewUrl.mockRejectedValueOnce(new Error('preview failed')) + renderGroup([model]) + + await waitFor(() => + expect(reportError).toHaveBeenCalledWith(expect.any(Error), { + errorType: 'agent_reply_asset_preview_failure' + }) ) + expect(generateModelThumbnail).not.toHaveBeenCalled() + + // A rejected lookup schedules a bounded retry rather than requiring a + // re-render to pick it back up (see modelThumbnailSrc / scheduleThumbnailRetry: + // deleting the key on failure was tried and found inert, since nothing + // re-triggers the watcher once the message stops changing). + await vi.advanceTimersByTimeAsync(2000) + await waitFor(() => expect(generateModelThumbnail).toHaveBeenCalledOnce()) + }) + + it('leaves a model that failed to render as a placeholder', async () => { + isAssetPreviewSupported.mockReturnValue(true) + renderGroup([model]) + await vi.waitFor(() => + expect(generateModelThumbnail).toHaveBeenCalledOnce() + ) + + vi.useFakeTimers() + try { + await vi.advanceTimersByTimeAsync(30_000) + } finally { + vi.useRealTimers() + } + + expect(generateModelThumbnail).toHaveBeenCalledOnce() + expect(screen.queryByRole('img', { name: 'mesh.glb' })).toBeNull() + }) + + it('bounds retries to exactly the initial attempt plus MAX_THUMBNAIL_RETRY_ATTEMPTS', async () => { + // Regression coverage for a bug where every retry rescheduled with + // attempts=0 instead of the incremented count, so a persistently + // failing render never reached MAX_THUMBNAIL_RETRY_ATTEMPTS and kept + // rescheduling every THUMBNAIL_RETRY_DELAY_MS indefinitely. + isAssetPreviewSupported.mockReturnValue(true) + vi.useFakeTimers() + try { + renderGroup([model]) + await vi.waitFor(() => + expect(generateModelThumbnail).toHaveBeenCalledOnce() + ) + + // MAX_THUMBNAIL_RETRY_ATTEMPTS = 2, THUMBNAIL_RETRY_DELAY_MS = 2000: + // the initial call plus 2 retries is 3 total calls, then the retry + // budget is spent and no further calls are scheduled. + await vi.advanceTimersByTimeAsync(2_000) + expect(generateModelThumbnail).toHaveBeenCalledTimes(2) + await vi.advanceTimersByTimeAsync(2_000) + expect(generateModelThumbnail).toHaveBeenCalledTimes(3) + + // No timer left pending once the budget is spent. + expect(vi.getTimerCount()).toBe(0) + await vi.advanceTimersByTimeAsync(10_000) + expect(generateModelThumbnail).toHaveBeenCalledTimes(3) + } finally { + vi.useRealTimers() + } + }) + + it('cancels a pending retry timer when the asset is hidden', async () => { + // Regression coverage: a retry scheduled while the tile was visible + // used to keep a bare setTimeout with no per-url handle, so + // `hideThumbnail` (fired by "Show less" / the asset leaving + // visibleVisual) had nothing to cancel. The retry fired anyway and + // called loadModelThumbnail for an asset the watcher had already + // dropped from thumbnailState. + isAssetPreviewSupported.mockReturnValue(true) + vi.useFakeTimers() + try { + const { rerender } = renderGroup([model]) + await vi.waitFor(() => + expect(generateModelThumbnail).toHaveBeenCalledOnce() + ) + + // The failed render schedules a retry in THUMBNAIL_RETRY_DELAY_MS. + // Hide the asset before that retry fires. + await rerender({ assets: [audio] }) + + await vi.advanceTimersByTimeAsync(10_000) + expect(generateModelThumbnail).toHaveBeenCalledOnce() + expect(vi.getTimerCount()).toBe(0) + } finally { + vi.useRealTimers() + } + }) + + it('aborts queued generation when unmounted', async () => { + isAssetPreviewSupported.mockReturnValue(true) + // Keep the render pending so the strand is still 'loading' (not yet + // settled to 'ready'/'gaveUp') when unmount fires — a mock that + // resolves immediately would let the retry-on-failure transition win + // the race and leave nothing in flight to abort. + let resolveGenerate!: (result: { + status: 'rendered' + dataUrl: string + }) => void + generateModelThumbnail.mockReturnValueOnce( + new Promise((resolve) => { + resolveGenerate = resolve + }) + ) + const { unmount } = renderGroup([model]) + await waitFor(() => expect(generateModelThumbnail).toHaveBeenCalledOnce()) + + const [, , signal] = generateModelThumbnail.mock.calls[0] + expect(signal?.aborted).toBe(false) + + unmount() + + expect(signal?.aborted).toBe(true) + resolveGenerate({ status: 'rendered', dataUrl: 'data:image/png;base64,x' }) + }) + + it('clears a pending thumbnail refresh when unmounted', async () => { + vi.useFakeTimers() + try { + isAssetPreviewSupported.mockReturnValue(true) + const { unmount } = renderGroup([model]) + await vi.waitFor(() => expect(findServerPreviewUrl).toHaveBeenCalled()) + // The initial lookup's fallback render (default mock: 'failed') has + // already scheduled its own bounded retry alongside the dialog-close + // refresh below, so two independent timers are pending at this point. + await vi.waitFor(() => expect(vi.getTimerCount()).toBe(1)) + await userEvent.click(screen.getByRole('button', { name: 'mesh.glb' })) + const dialog = showDialog.mock.calls.at(-1)?.[0] + dialog.dialogComponentProps.onClose() + await vi.advanceTimersByTimeAsync(0) + const callsBeforeUnmount = findServerPreviewUrl.mock.calls.length + expect(vi.getTimerCount()).toBe(2) + + unmount() + expect(vi.getTimerCount()).toBe(0) + + await vi.advanceTimersByTimeAsync(2_000) + expect(findServerPreviewUrl).toHaveBeenCalledTimes(callsBeforeUnmount) + } finally { + vi.useRealTimers() + } }) it('refreshes the tile thumbnail after the viewer closes', async () => { @@ -261,4 +442,78 @@ describe('ReplyAssetGroup', () => { expect(thumbs()).toHaveLength(12) expect(toggle()).toHaveTextContent('Show more') }) + + it('generates thumbnails only for currently visible 3D entries', async () => { + isAssetPreviewSupported.mockReturnValue(true) + const models = Array.from({ length: 13 }, (_, n) => ({ + ...model, + url: `https://x/mesh-${n}.glb`, + filename: `mesh-${n}.glb` + })) + renderGroup(models) + await waitFor(() => expect(findServerPreviewUrl).toHaveBeenCalledTimes(12)) + await waitFor(() => + expect(generateModelThumbnail).toHaveBeenCalledTimes(12) + ) + expect(generateModelThumbnail).not.toHaveBeenCalledWith( + 'https://x/mesh-12.glb', + 'mesh-12.glb', + expect.any(AbortSignal) + ) + + await userEvent.click(toggle()!) + await waitFor(() => + expect(generateModelThumbnail).toHaveBeenCalledWith( + 'https://x/mesh-12.glb', + 'mesh-12.glb', + expect.any(AbortSignal) + ) + ) + expect(generateModelThumbnail).toHaveBeenCalledTimes(13) + }) + + it('aborts a hidden render and restarts it on re-render without leaving the tile permanently blank', async () => { + isAssetPreviewSupported.mockReturnValue(true) + let resolveGenerate!: (result: { + status: 'rendered' + dataUrl: string + }) => void + generateModelThumbnail.mockReturnValueOnce( + new Promise((resolve) => { + resolveGenerate = resolve + }) + ) + const { rerender } = renderGroup([model]) + await waitFor(() => expect(generateModelThumbnail).toHaveBeenCalledOnce()) + const [, , firstSignal] = generateModelThumbnail.mock.calls[0] + + // The asset drops out of visibleVisual (e.g. the message shrinking to + // just the audio track) before its render settles. + await rerender({ assets: [audio] }) + expect(firstSignal?.aborted).toBe(true) + + // A late resolution of the aborted strand must not resurrect it as the + // tile's src (it is no longer the owning strand for that url). + resolveGenerate({ status: 'rendered', dataUrl: 'data:image/png;base64,x' }) + await Promise.resolve() + + // The asset becomes visible again: it must restart, not stay + // permanently blank (the bug this replaced state model fixes). + generateModelThumbnail.mockResolvedValueOnce({ + status: 'rendered', + dataUrl: 'data:image/png;base64,restarted' + }) + await rerender({ assets: [model] }) + await waitFor(() => + expect(generateModelThumbnail).toHaveBeenCalledWith( + model.url, + model.filename, + expect.any(AbortSignal) + ) + ) + const restartedCall = generateModelThumbnail.mock.calls.find( + (call) => call[0] === model.url && call[2] !== firstSignal + ) + expect(restartedCall).toBeDefined() + }) }) diff --git a/src/workbench/extensions/agent/components/agent/message/ReplyAssetGroup.vue b/src/workbench/extensions/agent/components/agent/message/ReplyAssetGroup.vue index f6e710d95c6..98c005e7c1b 100644 --- a/src/workbench/extensions/agent/components/agent/message/ReplyAssetGroup.vue +++ b/src/workbench/extensions/agent/components/agent/message/ReplyAssetGroup.vue @@ -1,12 +1,21 @@