From f4639d859771846eb19e4498b843cfdd46c7bf84 Mon Sep 17 00:00:00 2001 From: Amp Date: Sat, 12 Sep 2026 06:23:07 +0000 Subject: [PATCH 01/47] fix(load3d): reload model and refresh capture cache on agent-driven model_file changes Load3D preview capture stopped working after the in-app agent interacted with the graph (PM-1011). Root cause: Load3DConfiguration.setupModelHandling only reloads the Three.js scene and marks the capture cache dirty when something assigns modelWidget.value directly (an Object.defineProperty override on the widget instance). The agent/CRDT follower instead writes widget values straight into widgetValueStore (graphMutations.ts's setWidget/ reconcileNode commit path), bypassing that property entirely. The scene kept rendering the pre-edit model, and serializeValue (load3d.ts) kept returning the stale cached image/mask/normal/recording on the next queue or manual capture. Fix: subscribe to widgetValueStore.onValueChange from within setupModelHandling and replay the widget's own value setter whenever a remote-origin (agent) change lands for that widget id, so the existing load/dirty-marking logic runs regardless of which code path wrote the value. Self-detaches once the widget is deregistered (node removed) to avoid an unbounded listener leak. Added regression coverage in Load3DConfiguration.test.ts; verified the new tests fail without the fix and pass with it. --- .../core/load3d/Load3DConfiguration.test.ts | 215 ++++++++++++++++++ .../core/load3d/Load3DConfiguration.ts | 44 ++++ 2 files changed, 259 insertions(+) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index d238efe99ac..4c294b97b04 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -48,6 +48,45 @@ vi.mock('@/extensions/core/load3d/Load3dUtils', () => ({ } })) +type ValueChangeListener = (change: { + widgetId: string + value: unknown + oldValue?: unknown + context?: unknown +}) => void + +const { widgetValueStoreMock } = vi.hoisted(() => { + const listeners = new Set() + const widgets = new Map() + return { + widgetValueStoreMock: { + listeners, + widgets, + onValueChange: vi.fn((listener: ValueChangeListener) => { + listeners.add(listener) + return () => listeners.delete(listener) + }), + getWidget: vi.fn((id: string) => widgets.get(id)), + emit: (change: { + widgetId: string + value: unknown + oldValue?: unknown + context?: unknown + }) => { + for (const listener of [...listeners]) listener(change) + }, + reset: () => { + listeners.clear() + widgets.clear() + } + } + } +}) + +vi.mock('@/stores/widgetValueStore', () => ({ + useWidgetValueStore: () => widgetValueStoreMock +})) + type WithPrivate = { loadModelConfig(): ModelConfig loadSceneConfig(): SceneConfig @@ -776,3 +815,179 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }).not.toThrow() }) }) + +// PM-1011: after the agent interacts with the graph, Load3D preview capture +// stopped working. Root cause: the agent/CRDT follower writes widget values +// straight into widgetValueStore (graphMutations.ts `setWidget`), bypassing +// this file's `Object.defineProperty` override on `modelWidget.value`. That +// override is the only thing that reloads the Three.js scene and marks the +// capture cache dirty, so a remote-origin model_file change left the scene +// (and the next capture) stale. These tests pin the bridge that fixes it. +describe('Load3DConfiguration remote (agent) model updates', () => { + function makeLoad3dMock(): Load3d { + return { + loadModel: vi.fn().mockResolvedValue(undefined), + clearModel: vi.fn(), + setUpDirection: vi.fn(), + setMaterialMode: vi.fn(), + setTargetSize: vi.fn(), + setCameraState: vi.fn(), + toggleGrid: vi.fn(), + setBackgroundColor: vi.fn(), + setBackgroundImage: vi.fn().mockResolvedValue(undefined), + setBackgroundRenderMode: vi.fn(), + toggleCamera: vi.fn(), + setFOV: vi.fn(), + setLightIntensity: vi.fn(), + setHDRIIntensity: vi.fn(), + setHDRIAsBackground: vi.fn(), + setHDRIEnabled: vi.fn(), + emitModelReady: vi.fn() + } as unknown as Load3d + } + + async function flush() { + await new Promise((resolve) => setTimeout(resolve, 0)) + } + + const REMOTE_CONTEXT = { + source: 'agent-remote', + actor: 'agent', + opId: 'op-1' + } + + beforeEach(() => { + widgetValueStoreMock.reset() + vi.mocked(Load3dUtils.splitFilePath).mockReturnValue(['', 'model.glb']) + vi.mocked(Load3dUtils.getResourceURL).mockReturnValue('/view') + }) + + it('reloads the model when the agent changes model_file via widgetValueStore', async () => { + const load3d = makeLoad3dMock() + const onSceneInvalidated = vi.fn() + const modelWidget = { + value: 'none', + widgetId: 'widget-1' + } as unknown as IBaseWidget + widgetValueStoreMock.widgets.set('widget-1', modelWidget) + + const config = new Load3DConfiguration(load3d) + config.configure({ + modelWidget, + loadFolder: 'input', + onSceneInvalidated + }) + await flush() + vi.mocked(load3d.loadModel).mockClear() + onSceneInvalidated.mockClear() + + // Simulate the agent/CRDT follower's setWidget write: the store value + // changes but nothing ever assigns `modelWidget.value` directly. + widgetValueStoreMock.emit({ + widgetId: 'widget-1', + value: 'agent-model.glb', + oldValue: 'none', + context: REMOTE_CONTEXT + }) + await flush() + + expect(load3d.loadModel).toHaveBeenCalledWith( + expect.any(String), + 'agent-model.glb', + { silentOnNotFound: false } + ) + expect(onSceneInvalidated).toHaveBeenCalled() + expect(modelWidget.value).toBe('agent-model.glb') + }) + + it('ignores local (non-remote) store changes to avoid double-loading', async () => { + const load3d = makeLoad3dMock() + const modelWidget = { + value: 'none', + widgetId: 'widget-2' + } as unknown as IBaseWidget + widgetValueStoreMock.widgets.set('widget-2', modelWidget) + + const config = new Load3DConfiguration(load3d) + config.configure({ modelWidget, loadFolder: 'input' }) + await flush() + vi.mocked(load3d.loadModel).mockClear() + + widgetValueStoreMock.emit({ + widgetId: 'widget-2', + value: 'local-change.glb', + oldValue: 'none', + context: undefined + }) + await flush() + + expect(load3d.loadModel).not.toHaveBeenCalled() + }) + + it('ignores value changes for a different widget id', async () => { + const load3d = makeLoad3dMock() + const modelWidget = { + value: 'none', + widgetId: 'widget-3' + } as unknown as IBaseWidget + widgetValueStoreMock.widgets.set('widget-3', modelWidget) + + const config = new Load3DConfiguration(load3d) + config.configure({ modelWidget, loadFolder: 'input' }) + await flush() + vi.mocked(load3d.loadModel).mockClear() + + widgetValueStoreMock.emit({ + widgetId: 'some-other-widget', + value: 'agent-model.glb', + oldValue: 'none', + context: REMOTE_CONTEXT + }) + await flush() + + expect(load3d.loadModel).not.toHaveBeenCalled() + }) + + it('does not register a second listener when configure runs again for the same widget', async () => { + const load3d = makeLoad3dMock() + const modelWidget = { + value: 'none', + widgetId: 'widget-4' + } as unknown as IBaseWidget + widgetValueStoreMock.widgets.set('widget-4', modelWidget) + + const config = new Load3DConfiguration(load3d) + config.configure({ modelWidget, loadFolder: 'input' }) + config.configure({ modelWidget, loadFolder: 'input' }) + await flush() + + expect(widgetValueStoreMock.onValueChange).toHaveBeenCalledTimes(1) + }) + + it('detaches once the widget is no longer registered (node removed)', async () => { + const load3d = makeLoad3dMock() + const modelWidget = { + value: 'none', + widgetId: 'widget-5' + } as unknown as IBaseWidget + widgetValueStoreMock.widgets.set('widget-5', modelWidget) + + const config = new Load3DConfiguration(load3d) + config.configure({ modelWidget, loadFolder: 'input' }) + await flush() + + // Node removed: the widget is deregistered from the store. + widgetValueStoreMock.widgets.delete('widget-5') + + expect(widgetValueStoreMock.listeners.size).toBe(1) + + widgetValueStoreMock.emit({ + widgetId: 'widget-5', + value: 'ignored.glb', + oldValue: 'none', + context: REMOTE_CONTEXT + }) + + expect(widgetValueStoreMock.listeners.size).toBe(0) + }) +}) diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index c3822b95d7e..939f9f5fce1 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -14,6 +14,14 @@ import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { useSettingStore } from '@/platform/settings/settingStore' import { api } from '@/scripts/api' +import { useWidgetValueStore } from '@/stores/widgetValueStore' +import { isRemoteMutationContext } from '@/types/graphMutationContext' + +// One store subscription per widget instance, regardless of how many times +// `configure()` re-runs across this node's lifecycle (queue results, preview +// re-applies, etc). Keyed by the widget object itself so a widget torn down +// with its node is naturally released. +const remoteModelUpdateSubscribed = new WeakSet() type Load3DConfigurationSettings = { loadFolder: string @@ -160,6 +168,42 @@ class Load3DConfiguration { onSceneInvalidated?.() } + + this.subscribeToRemoteModelUpdates(modelWidget) + } + + /** + * The agent/CRDT follower writes widget values straight into + * `widgetValueStore` (`graphMutations.ts`'s `setWidget`/`reconcileNode` + * commit path), never through this widget's own JS property. The + * `Object.defineProperty` override above only fires when something sets + * `modelWidget.value` directly, so a remote-origin `model_file` change + * would otherwise never reload the Three.js scene or invalidate the + * capture cache, leaving `serializeValue` (load3d.ts) returning a stale + * cached image/mask/normal/recording on the next queue or manual capture. + * Bridge the two by replaying the widget's own setter for value changes + * that originated remotely. + */ + private subscribeToRemoteModelUpdates(modelWidget: IBaseWidget): void { + if (remoteModelUpdateSubscribed.has(modelWidget)) return + remoteModelUpdateSubscribed.add(modelWidget) + + const widgetValueStore = useWidgetValueStore() + const unsubscribe = widgetValueStore.onValueChange( + ({ widgetId, value, context }) => { + // The widget (and its node) may have been removed since this + // listener was registered; `onValueChange` has no per-widget scope, + // so detect staleness here and detach rather than leaking forever. + if (!widgetValueStore.getWidget(modelWidget.widgetId ?? widgetId)) { + unsubscribe() + return + } + if (!isRemoteMutationContext(context)) return + if (widgetId !== modelWidget.widgetId) return + if (value === modelWidget.value) return + modelWidget.value = value as IBaseWidget['value'] + } + ) } private setupDefaultProperties(bgImagePath?: string) { From d8a843eb0396e265dd027b23f75f38d86db1cbc4 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 16 Sep 2026 16:24:27 -0700 Subject: [PATCH 02/47] fix(load3d): keep model configuration idempotent Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4001574778 --- .../core/load3d/Load3DConfiguration.test.ts | 18 +++++++++-- .../core/load3d/Load3DConfiguration.ts | 31 ++++++++++++++++--- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 4c294b97b04..33556906148 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -950,6 +950,7 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('does not register a second listener when configure runs again for the same widget', async () => { const load3d = makeLoad3dMock() + const onSceneInvalidated = vi.fn() const modelWidget = { value: 'none', widgetId: 'widget-4' @@ -957,11 +958,24 @@ describe('Load3DConfiguration remote (agent) model updates', () => { widgetValueStoreMock.widgets.set('widget-4', modelWidget) const config = new Load3DConfiguration(load3d) - config.configure({ modelWidget, loadFolder: 'input' }) - config.configure({ modelWidget, loadFolder: 'input' }) + config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) + config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) await flush() expect(widgetValueStoreMock.onValueChange).toHaveBeenCalledTimes(1) + + vi.mocked(load3d.loadModel).mockClear() + onSceneInvalidated.mockClear() + widgetValueStoreMock.emit({ + widgetId: 'widget-4', + value: 'agent-model.glb', + oldValue: 'none', + context: REMOTE_CONTEXT + }) + await flush() + + expect(load3d.loadModel).toHaveBeenCalledTimes(1) + expect(onSceneInvalidated).toHaveBeenCalledTimes(1) }) it('detaches once the widget is no longer registered (node removed)', async () => { diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index 939f9f5fce1..7dda53fcc85 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -23,6 +23,14 @@ import { isRemoteMutationContext } from '@/types/graphMutationContext' // with its node is naturally released. const remoteModelUpdateSubscribed = new WeakSet() +type ModelWidgetBinding = { + onModelWidgetUpdate: (value: IBaseWidget['value']) => Promise + onSceneInvalidated?: () => void + originalCallback?: IBaseWidget['callback'] +} + +const modelWidgetBindings = new WeakMap() + type Load3DConfigurationSettings = { loadFolder: string modelWidget: IBaseWidget @@ -142,7 +150,20 @@ class Load3DConfiguration { void onModelWidgetUpdate(modelWidget.value) } - const originalCallback = modelWidget.callback + const existingBinding = modelWidgetBindings.get(modelWidget) + if (existingBinding) { + existingBinding.onModelWidgetUpdate = onModelWidgetUpdate + existingBinding.onSceneInvalidated = onSceneInvalidated + this.subscribeToRemoteModelUpdates(modelWidget) + return + } + + const binding: ModelWidgetBinding = { + onModelWidgetUpdate, + onSceneInvalidated, + originalCallback: modelWidget.callback + } + modelWidgetBindings.set(modelWidget, binding) let currentValue = modelWidget.value Object.defineProperty(modelWidget, 'value', { @@ -160,13 +181,13 @@ class Load3DConfiguration { }) modelWidget.callback = (value: string | number | boolean | object) => { - void onModelWidgetUpdate(value) + void binding.onModelWidgetUpdate(value) - if (originalCallback) { - originalCallback(value) + if (binding.originalCallback) { + binding.originalCallback(value) } - onSceneInvalidated?.() + binding.onSceneInvalidated?.() } this.subscribeToRemoteModelUpdates(modelWidget) From 16cd65516d2276815d0764ded1d3507f41f87bc4 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 16 Sep 2026 16:24:54 -0700 Subject: [PATCH 03/47] fix(load3d): clear remotely emptied models Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4001574783 --- .../core/load3d/Load3DConfiguration.test.ts | 28 +++++++++++++++++++ .../core/load3d/Load3DConfiguration.ts | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 33556906148..294ca5b08fd 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -900,6 +900,34 @@ describe('Load3DConfiguration remote (agent) model updates', () => { expect(modelWidget.value).toBe('agent-model.glb') }) + it('clears the model when the agent clears model_file via widgetValueStore', async () => { + const load3d = makeLoad3dMock() + const onSceneInvalidated = vi.fn() + const modelWidget = { + value: 'model.glb', + widgetId: 'widget-clear' + } as unknown as IBaseWidget + widgetValueStoreMock.widgets.set('widget-clear', modelWidget) + + const config = new Load3DConfiguration(load3d) + config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) + await flush() + vi.mocked(load3d.clearModel).mockClear() + onSceneInvalidated.mockClear() + + widgetValueStoreMock.emit({ + widgetId: 'widget-clear', + value: '', + oldValue: 'model.glb', + context: REMOTE_CONTEXT + }) + await flush() + + expect(load3d.clearModel).toHaveBeenCalledTimes(1) + expect(onSceneInvalidated).toHaveBeenCalledTimes(1) + expect(modelWidget.value).toBe('') + }) + it('ignores local (non-remote) store changes to avoid double-loading', async () => { const load3d = makeLoad3dMock() const modelWidget = { diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index 7dda53fcc85..58271ffcd4e 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -172,7 +172,7 @@ class Load3DConfiguration { }, set(newValue) { currentValue = newValue - if (modelWidget.callback && newValue !== undefined && newValue !== '') { + if (modelWidget.callback && newValue !== undefined) { modelWidget.callback(newValue) } }, From 821df488e7515448caebd53a6388a9008ef3fd7f Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 16 Sep 2026 16:47:32 -0700 Subject: [PATCH 04/47] test(load3d): cover agent-driven capture refresh Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4001574772 --- .../tests/load3d/load3dAgentUpdate.spec.ts | 177 ++++++++++++++++++ .../core/load3d/Load3DConfiguration.ts | 4 +- 2 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 browser_tests/tests/load3d/load3dAgentUpdate.spec.ts diff --git a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts new file mode 100644 index 00000000000..cbf4a5d7d8c --- /dev/null +++ b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts @@ -0,0 +1,177 @@ +import type { Page } from '@playwright/test' +import { expect } from '@playwright/test' + +import workflow from '@e2e/assets/3d/load3d_node.json' with { type: 'json' } +import { cloudAppFixture, waitForCloudApp } from '@e2e/fixtures/cloudAppFixture' +import { mockBilling } from '@e2e/fixtures/utils/cloudBillingMocks' +import { bootCloud, mockCloudBoot } from '@e2e/fixtures/utils/cloudBootMocks' +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' +import { assetPath } from '@e2e/fixtures/utils/paths' +import { Load3DHelper } from '@e2e/tests/load3d/Load3DHelper' +import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' + +const test = cloudAppFixture +const WORKFLOW_ID = 'a81718a4-02ae-41e6-ae85-c33b7bb880f6' +const THREAD_ID = 'd4c016c4-3b8c-44cf-97de-1ae27e43e718' + +function promptImage(body: unknown): string { + const prompt = ( + body as { prompt?: Record }> } + ).prompt + const image = Object.values(prompt ?? {}).find( + (node) => node.inputs?.image !== undefined + )?.inputs?.image + expect(image).toBeTruthy() + return (image as { image: string }).image +} + +async function bootAgentLoad3d(page: Page): Promise { + await page.addInitScript(() => { + localStorage.setItem('Comfy.AgentPanel.onboarded', 'true') + localStorage.setItem('Comfy.Agent.CrdtFollower', 'true') + }) + await mockCloudBoot(page, { + features: { + posthog_project_token: 'phc_e2e_agent_load3d', + posthog_config: { + advanced_disable_flags: true, + bootstrap: { featureFlags: { 'agent-in-app-experience': true } } + } + }, + settings: { + 'Comfy.TutorialCompleted': true, + 'Comfy.VueNodes.Enabled': true, + 'Comfy.RightSidePanel.ShowErrorsTab': false + } + }) + // Keep the cloud shell deterministic, but use the real black-box backend's + // Load3D definition so this exercises the production node/viewer. + await page.unroute('**/api/object_info') + await mockBilling(page) + await page.route('**/api/assets**', (route) => + route.fulfill(jsonRoute({ assets: [], total: 0, has_more: false })) + ) + let capture = 0 + await page.route('**/api/upload/image**', (route) => + route.fulfill( + jsonRoute({ + name: `agent-load3d-capture-${++capture}.png`, + subfolder: 'temp', + type: 'temp' + }) + ) + ) + await page.route('**/api/view?*filename=cube.obj*', (route) => + route.fulfill({ path: assetPath('cube.obj') }) + ) + await page.route('**/api/view?*filename=workflow.glb*', (route) => + route.fulfill({ path: assetPath('workflowInMedia/workflow.glb') }) + ) + await page.route('**://t.comfy.org/**', (route) => + route.fulfill(jsonRoute({ status: 1 })) + ) + await page.route('**/api/agent/threads/*/messages', (route) => + route.fulfill( + jsonRoute({ + message_id: '3818ba00-d772-4a3f-98c1-9312725b577d', + thread_id: THREAD_ID, + workflow_id: WORKFLOW_ID + }) + ) + ) + await page.route(/\/api\/prompt$/, (route) => { + if (route.request().method() !== 'POST') return route.fallback() + return route.fulfill( + jsonRoute({ prompt_id: crypto.randomUUID(), node_errors: {} }) + ) + }) + await bootCloud(page) + await page.goto('http://localhost:5173?agentCrdtFollower=1') + await waitForCloudApp(page) + await page.evaluate( + async (data) => { + await window.app!.loadGraphData(data, true, true) + }, + workflow as unknown as ComfyWorkflowJSON + ) + await expect(page.locator('[data-node-id="1"]')).toBeVisible() +} + +async function captureNextPrompt(page: Page): Promise { + const request = page.waitForRequest( + (value) => + value.method() === 'POST' && + new URL(value.url()).pathname === '/api/prompt' + ) + await page.getByTestId('queue-button').click() + return promptImage((await request).postDataJSON()) +} + +async function setRemoteModel(page: Page, value: string, opId: string) { + await page.evaluate( + async ({ value, opId }) => { + const [ + { createGraphMutations }, + { toNodeId }, + { toRootGraphId, toOwningGraphId }, + { useWorkflowStore } + ] = await Promise.all([ + // @ts-expect-error Vite resolves this browser-only module URL. + import('/src/core/graph/graphMutations.ts'), + // @ts-expect-error Vite resolves this browser-only module URL. + import('/src/types/nodeId.ts'), + // @ts-expect-error Vite resolves this browser-only module URL. + import('/src/types/graphScopeId.ts'), + // @ts-expect-error Vite resolves this browser-only module URL. + import('/src/platform/workflow/management/stores/workflowStore.ts') + ]) + const graphId = useWorkflowStore().activeWorkflow?.activeState?.id + if (!graphId) throw new Error('active workflow has no graph id') + createGraphMutations({ + getScope: () => ({ + rootGraphId: toRootGraphId(graphId), + owningGraphId: toOwningGraphId(graphId) + }) + }).setWidget(toNodeId(1), 'model_file', value, { + source: 'agent-remote', + actor: 'agent:e2e', + opId + }) + }, + { value, opId } + ) +} + +test.describe('Load3D agent updates', { tag: '@cloud' }, () => { + test('an agent model_file update refreshes the viewer and capture cache', async ({ + page + }) => { + test.setTimeout(60_000) + await bootAgentLoad3d(page) + const load3d = new Load3DHelper(page.locator('[data-node-id="1"]')) + await expect(load3d.canvas).toBeVisible() + await setRemoteModel(page, 'cube.obj', 'load3d-model-a') + await load3d.waitForModelLoaded() + const beforeImage = await captureNextPrompt(page) + await setRemoteModel(page, 'workflow.glb', 'load3d-model-update') + + await expect + .poll(() => + page.evaluate( + () => + window + .app!.graph.getNodeById('1' as never) + ?.widgets?.find((widget) => widget.name === 'model_file')?.value + ) + ) + .toBe('workflow.glb') + await load3d.waitForModelLoaded() + const afterImage = await captureNextPrompt(page) + expect(afterImage).not.toBe(beforeImage) + await expect(load3d.canvas).toBeVisible() + await test.info().attach('agent-updated-load3d.png', { + body: await load3d.node.screenshot(), + contentType: 'image/png' + }) + }) +}) diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index 58271ffcd4e..128d183c717 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -24,7 +24,9 @@ import { isRemoteMutationContext } from '@/types/graphMutationContext' const remoteModelUpdateSubscribed = new WeakSet() type ModelWidgetBinding = { - onModelWidgetUpdate: (value: IBaseWidget['value']) => Promise + onModelWidgetUpdate: ( + value: string | number | boolean | object + ) => Promise onSceneInvalidated?: () => void originalCallback?: IBaseWidget['callback'] } From a4746df5dbdfc35a8f7e82308c89bef9b782444b Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 16 Sep 2026 19:26:59 -0700 Subject: [PATCH 05/47] test(load3d): make agent regression CI-safe --- .../tests/load3d/load3dAgentUpdate.spec.ts | 65 +++++++++++-------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts index cbf4a5d7d8c..2c15594610a 100644 --- a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts +++ b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts @@ -11,6 +11,7 @@ import { Load3DHelper } from '@e2e/tests/load3d/Load3DHelper' import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' const test = cloudAppFixture +const APP_URL = process.env.PLAYWRIGHT_TEST_URL || 'http://localhost:8188' const WORKFLOW_ID = 'a81718a4-02ae-41e6-ae85-c33b7bb880f6' const THREAD_ID = 'd4c016c4-3b8c-44cf-97de-1ae27e43e718' @@ -86,7 +87,7 @@ async function bootAgentLoad3d(page: Page): Promise { ) }) await bootCloud(page) - await page.goto('http://localhost:5173?agentCrdtFollower=1') + await page.goto(new URL('/?agentCrdtFollower=1', APP_URL).toString()) await waitForCloudApp(page) await page.evaluate( async (data) => { @@ -110,33 +111,43 @@ async function captureNextPrompt(page: Page): Promise { async function setRemoteModel(page: Page, value: string, opId: string) { await page.evaluate( async ({ value, opId }) => { - const [ - { createGraphMutations }, - { toNodeId }, - { toRootGraphId, toOwningGraphId }, - { useWorkflowStore } - ] = await Promise.all([ - // @ts-expect-error Vite resolves this browser-only module URL. - import('/src/core/graph/graphMutations.ts'), - // @ts-expect-error Vite resolves this browser-only module URL. - import('/src/types/nodeId.ts'), - // @ts-expect-error Vite resolves this browser-only module URL. - import('/src/types/graphScopeId.ts'), - // @ts-expect-error Vite resolves this browser-only module URL. - import('/src/platform/workflow/management/stores/workflowStore.ts') - ]) - const graphId = useWorkflowStore().activeWorkflow?.activeState?.id - if (!graphId) throw new Error('active workflow has no graph id') - createGraphMutations({ - getScope: () => ({ - rootGraphId: toRootGraphId(graphId), - owningGraphId: toOwningGraphId(graphId) + const root = document.querySelector('#vue-app') as Element & { + __vue_app__?: { + _context: { provides: Record } + } + } + const pinia = Reflect.ownKeys(root.__vue_app__?._context.provides ?? {}) + .map( + (key) => + root.__vue_app__?._context.provides[key] as + | { _s?: Map } + | undefined + ) + .find((candidate) => candidate?._s instanceof Map) + const widgetStore = pinia?._s?.get('widgetValue') as + | { + setValue: ( + id: string, + nextValue: string, + context: Record + ) => boolean + } + | undefined + const modelWidget = window + .app!.graph.getNodeById('1' as never) + ?.widgets?.find((widget) => widget.name === 'model_file') + if (!widgetStore || !modelWidget?.widgetId) { + throw new Error('model_file widget store binding unavailable') + } + if ( + !widgetStore.setValue(modelWidget.widgetId, value, { + source: 'agent-remote', + actor: 'agent:e2e', + opId }) - }).setWidget(toNodeId(1), 'model_file', value, { - source: 'agent-remote', - actor: 'agent:e2e', - opId - }) + ) { + throw new Error('remote model_file update was not applied') + } }, { value, opId } ) From a39263047131bdffa884abaa4388eef8f39f5948 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 16 Sep 2026 20:21:31 -0700 Subject: [PATCH 06/47] test(load3d): preserve NodeId contract in browser assertion Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4032564752 --- browser_tests/tests/load3d/load3dAgentUpdate.spec.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts index 2c15594610a..7437b878231 100644 --- a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts +++ b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts @@ -9,6 +9,7 @@ import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' import { assetPath } from '@e2e/fixtures/utils/paths' import { Load3DHelper } from '@e2e/tests/load3d/Load3DHelper' import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' +import { toNodeId } from '@/types/nodeId' const test = cloudAppFixture const APP_URL = process.env.PLAYWRIGHT_TEST_URL || 'http://localhost:8188' @@ -169,10 +170,11 @@ test.describe('Load3D agent updates', { tag: '@cloud' }, () => { await expect .poll(() => page.evaluate( - () => + (nodeId) => window - .app!.graph.getNodeById('1' as never) - ?.widgets?.find((widget) => widget.name === 'model_file')?.value + .app!.graph.getNodeById(nodeId) + ?.widgets?.find((widget) => widget.name === 'model_file')?.value, + toNodeId('1') ) ) .toBe('workflow.glb') From a22262943d34afa48332d6b4b46123da405f0d65 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 16 Sep 2026 20:25:53 -0700 Subject: [PATCH 07/47] test(load3d): use valid model fixtures in agent regression Adversarial follow-up: the prior workflow.glb fixture has a length mismatch and let the loading-overlay assertion pass after a parser failure. Route the valid animated GLB and preserve NodeId typing at the remote-update seam. --- browser_tests/tests/load3d/load3dAgentUpdate.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts index 7437b878231..befe9638add 100644 --- a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts +++ b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts @@ -67,7 +67,7 @@ async function bootAgentLoad3d(page: Page): Promise { route.fulfill({ path: assetPath('cube.obj') }) ) await page.route('**/api/view?*filename=workflow.glb*', (route) => - route.fulfill({ path: assetPath('workflowInMedia/workflow.glb') }) + route.fulfill({ path: assetPath('animated_triangle.glb') }) ) await page.route('**://t.comfy.org/**', (route) => route.fulfill(jsonRoute({ status: 1 })) @@ -111,7 +111,7 @@ async function captureNextPrompt(page: Page): Promise { async function setRemoteModel(page: Page, value: string, opId: string) { await page.evaluate( - async ({ value, opId }) => { + async ({ nodeId, value, opId }) => { const root = document.querySelector('#vue-app') as Element & { __vue_app__?: { _context: { provides: Record } @@ -135,7 +135,7 @@ async function setRemoteModel(page: Page, value: string, opId: string) { } | undefined const modelWidget = window - .app!.graph.getNodeById('1' as never) + .app!.graph.getNodeById(nodeId) ?.widgets?.find((widget) => widget.name === 'model_file') if (!widgetStore || !modelWidget?.widgetId) { throw new Error('model_file widget store binding unavailable') @@ -150,7 +150,7 @@ async function setRemoteModel(page: Page, value: string, opId: string) { throw new Error('remote model_file update was not applied') } }, - { value, opId } + { nodeId: toNodeId('1'), value, opId } ) } From 71cd26d48906c6a569f8818e3f64881efde3e778 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 16 Sep 2026 20:37:44 -0700 Subject: [PATCH 08/47] test(load3d): use the shared testing Pinia Fixes the exact lint-and-format failure from https://github.com/Comfy-Org/ComfyUI_frontend/actions/runs/35178461448/job/105065270490 by stubbing widget store actions through the global testing Pinia instead of mocking the store module. --- .../core/load3d/Load3DConfiguration.test.ts | 57 +++++++++---------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 52376f786cc..002bd29d7f1 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -19,6 +19,7 @@ import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { Dictionary } from '@/lib/litegraph/src/interfaces' import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' import { useSettingStore } from '@/platform/settings/settingStore' +import { useWidgetValueStore } from '@/stores/widgetValueStore' vi.mock(import('@/scripts/api'), () => ({ api: fromPartial({ @@ -53,37 +54,22 @@ type ValueChangeListener = (change: { context?: unknown }) => void -const { widgetValueStoreMock } = vi.hoisted(() => { - const listeners = new Set() - const widgets = new Map() - return { - widgetValueStoreMock: { - listeners, - widgets, - onValueChange: vi.fn((listener: ValueChangeListener) => { - listeners.add(listener) - return () => listeners.delete(listener) - }), - getWidget: vi.fn((id: string) => widgets.get(id)), - emit: (change: { - widgetId: string - value: unknown - oldValue?: unknown - context?: unknown - }) => { - for (const listener of [...listeners]) listener(change) - }, - reset: () => { - listeners.clear() - widgets.clear() - } - } +const widgetValueStoreMock = { + listeners: new Set(), + widgets: new Map(), + emit(change: { + widgetId: string + value: unknown + oldValue?: unknown + context?: unknown + }) { + for (const listener of [...this.listeners]) listener(change) + }, + reset() { + this.listeners.clear() + this.widgets.clear() } -}) - -vi.mock('@/stores/widgetValueStore', () => ({ - useWidgetValueStore: () => widgetValueStoreMock -})) +} type WithPrivate = { loadModelConfig(): ModelConfig @@ -856,6 +842,15 @@ describe('Load3DConfiguration remote (agent) model updates', () => { beforeEach(() => { widgetValueStoreMock.reset() + const widgetValueStore = useWidgetValueStore() + vi.mocked(widgetValueStore.onValueChange).mockImplementation((listener) => { + widgetValueStoreMock.listeners.add(listener as ValueChangeListener) + return () => + widgetValueStoreMock.listeners.delete(listener as ValueChangeListener) + }) + vi.mocked(widgetValueStore.getWidget).mockImplementation( + (id) => widgetValueStoreMock.widgets.get(id) as never + ) vi.mocked(Load3dUtils.splitFilePath).mockReturnValue(['', 'model.glb']) vi.mocked(Load3dUtils.getResourceURL).mockReturnValue('/view') }) @@ -988,7 +983,7 @@ describe('Load3DConfiguration remote (agent) model updates', () => { config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) await flush() - expect(widgetValueStoreMock.onValueChange).toHaveBeenCalledTimes(1) + expect(useWidgetValueStore().onValueChange).toHaveBeenCalledTimes(1) vi.mocked(load3d.loadModel).mockClear() onSceneInvalidated.mockClear() From 6672293ff21148b0f79907e132553a5789434e25 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 16 Sep 2026 20:42:09 -0700 Subject: [PATCH 09/47] refactor(load3d): remove redundant widget value cast --- src/extensions/core/load3d/Load3DConfiguration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index 1757e894577..d479d7360c4 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -224,7 +224,7 @@ class Load3DConfiguration { if (!isRemoteMutationContext(context)) return if (widgetId !== modelWidget.widgetId) return if (value === modelWidget.value) return - modelWidget.value = value as IBaseWidget['value'] + modelWidget.value = value } ) } From 976429f5fceb403944295cb0923faa68f7fad60d Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 16 Sep 2026 22:55:14 -0700 Subject: [PATCH 10/47] fix(load3d): renew remote subscription after re-registration Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#pullrequestreview-5230386874 --- .../core/load3d/Load3DConfiguration.test.ts | 36 +++++++++++++++++++ .../core/load3d/Load3DConfiguration.ts | 1 + 2 files changed, 37 insertions(+) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 002bd29d7f1..682bd334cda 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -1025,4 +1025,40 @@ describe('Load3DConfiguration remote (agent) model updates', () => { expect(widgetValueStoreMock.listeners.size).toBe(0) }) + + it('subscribes again when the same widget is re-registered', async () => { + const load3d = makeLoad3dMock() + const modelWidget = { + value: 'none', + widgetId: 'widget-6' + } as unknown as IBaseWidget + widgetValueStoreMock.widgets.set('widget-6', modelWidget) + + const config = new Load3DConfiguration(load3d) + config.configure({ modelWidget, loadFolder: 'input' }) + await flush() + widgetValueStoreMock.widgets.delete('widget-6') + widgetValueStoreMock.emit({ + widgetId: 'widget-6', + value: 'ignored.glb', + oldValue: 'none', + context: REMOTE_CONTEXT + }) + widgetValueStoreMock.widgets.set('widget-6', modelWidget) + config.configure({ modelWidget, loadFolder: 'input' }) + await flush() + vi.mocked(load3d.loadModel).mockClear() + + widgetValueStoreMock.emit({ + widgetId: 'widget-6', + value: 'restored.glb', + oldValue: 'none', + context: REMOTE_CONTEXT + }) + await flush() + + expect(widgetValueStoreMock.listeners.size).toBe(1) + expect(load3d.loadModel).toHaveBeenCalledTimes(1) + expect(modelWidget.value).toBe('restored.glb') + }) }) diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index d479d7360c4..0295734cced 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -218,6 +218,7 @@ class Load3DConfiguration { // listener was registered; `onValueChange` has no per-widget scope, // so detect staleness here and detach rather than leaking forever. if (!widgetValueStore.getWidget(modelWidget.widgetId ?? widgetId)) { + remoteModelUpdateSubscribed.delete(modelWidget) unsubscribe() return } From 145fa7954574b9762e7a1b579e797c7149a95061 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 16 Sep 2026 23:03:24 -0700 Subject: [PATCH 11/47] test(load3d): compare captured image bytes Adversarial oracle follow-up: mocked upload filenames always differed even when the captured image did not. --- .../tests/load3d/load3dAgentUpdate.spec.ts | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts index befe9638add..5f5f7660035 100644 --- a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts +++ b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts @@ -1,4 +1,4 @@ -import type { Page } from '@playwright/test' +import type { Page, Request } from '@playwright/test' import { expect } from '@playwright/test' import workflow from '@e2e/assets/3d/load3d_node.json' with { type: 'json' } @@ -27,7 +27,7 @@ function promptImage(body: unknown): string { return (image as { image: string }).image } -async function bootAgentLoad3d(page: Page): Promise { +async function bootAgentLoad3d(page: Page): Promise { await page.addInitScript(() => { localStorage.setItem('Comfy.AgentPanel.onboarded', 'true') localStorage.setItem('Comfy.Agent.CrdtFollower', 'true') @@ -54,15 +54,17 @@ async function bootAgentLoad3d(page: Page): Promise { route.fulfill(jsonRoute({ assets: [], total: 0, has_more: false })) ) let capture = 0 - await page.route('**/api/upload/image**', (route) => - route.fulfill( + const uploadRequests: Request[] = [] + await page.route('**/api/upload/image**', (route) => { + uploadRequests.push(route.request()) + return route.fulfill( jsonRoute({ name: `agent-load3d-capture-${++capture}.png`, subfolder: 'temp', type: 'temp' }) ) - ) + }) await page.route('**/api/view?*filename=cube.obj*', (route) => route.fulfill({ path: assetPath('cube.obj') }) ) @@ -97,6 +99,20 @@ async function bootAgentLoad3d(page: Page): Promise { workflow as unknown as ComfyWorkflowJSON ) await expect(page.locator('[data-node-id="1"]')).toBeVisible() + return uploadRequests +} + +async function uploadedImageBytes(request: Request): Promise { + const body = request.postDataBuffer() + expect(body).not.toBeNull() + if (!body) throw new Error('upload request has no body') + const form = await new Response(new Uint8Array(body), { + headers: { 'content-type': request.headers()['content-type'] ?? '' } + }).formData() + const image = form.get('image') + expect(image).toBeInstanceOf(File) + if (!(image instanceof File)) throw new Error('upload has no image file') + return new Uint8Array(await image.arrayBuffer()) } async function captureNextPrompt(page: Page): Promise { @@ -159,7 +175,7 @@ test.describe('Load3D agent updates', { tag: '@cloud' }, () => { page }) => { test.setTimeout(60_000) - await bootAgentLoad3d(page) + const uploadRequests = await bootAgentLoad3d(page) const load3d = new Load3DHelper(page.locator('[data-node-id="1"]')) await expect(load3d.canvas).toBeVisible() await setRemoteModel(page, 'cube.obj', 'load3d-model-a') @@ -181,6 +197,11 @@ test.describe('Load3D agent updates', { tag: '@cloud' }, () => { await load3d.waitForModelLoaded() const afterImage = await captureNextPrompt(page) expect(afterImage).not.toBe(beforeImage) + expect(uploadRequests).toHaveLength(6) + const beforeImageBytes = await uploadedImageBytes(uploadRequests[0]) + const afterImageBytes = await uploadedImageBytes(uploadRequests[3]) + expect(beforeImageBytes.byteLength).toBeGreaterThan(0) + expect(afterImageBytes).not.toEqual(beforeImageBytes) await expect(load3d.canvas).toBeVisible() await test.info().attach('agent-updated-load3d.png', { body: await load3d.node.screenshot(), From 54e0eb214e429405b0b10135c7c765ce13d27c59 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Thu, 17 Sep 2026 12:34:19 -0700 Subject: [PATCH 12/47] fix(load3d): detach remote listener after node removal Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#issuecomment-5715768185 --- .../core/load3d/Load3DConfiguration.test.ts | 25 +++++++++++++------ .../core/load3d/Load3DConfiguration.ts | 6 +++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 902b15071e7..7243afb3786 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -18,6 +18,7 @@ import type { import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { Dictionary } from '@/lib/litegraph/src/interfaces' import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' +import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import { useSettingStore } from '@/platform/settings/settingStore' import type { Settings } from '@/platform/settings/types' import { useWidgetValueStore } from '@/stores/widgetValueStore' @@ -1002,23 +1003,31 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('detaches once the widget is no longer registered (node removed)', async () => { const load3d = makeLoad3dMock() - const modelWidget = { - value: 'none', - widgetId: 'widget-5' - } as unknown as IBaseWidget - widgetValueStoreMock.widgets.set('widget-5', modelWidget) + const graph = new LGraph() + const node = new LGraphNode('Load3D') + graph.add(node) + const modelWidget = node.addWidget( + 'string', + 'model_file', + 'none', + () => undefined + ) + const registeredWidgetId = modelWidget.widgetId + if (!registeredWidgetId) throw new Error('Expected a registered widget') + widgetValueStoreMock.widgets.set(registeredWidgetId, modelWidget) const config = new Load3DConfiguration(load3d) config.configure({ modelWidget, loadFolder: 'input' }) await flush() - // Node removed: the widget is deregistered from the store. - widgetValueStoreMock.widgets.delete('widget-5') + graph.remove(node) + expect(modelWidget.widgetId).toBeUndefined() + widgetValueStoreMock.widgets.delete(registeredWidgetId) expect(widgetValueStoreMock.listeners.size).toBe(1) widgetValueStoreMock.emit({ - widgetId: 'widget-5', + widgetId: 'some-other-widget', value: 'ignored.glb', oldValue: 'none', context: REMOTE_CONTEXT diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index 0295734cced..8ea77355831 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -208,6 +208,8 @@ class Load3DConfiguration { * that originated remotely. */ private subscribeToRemoteModelUpdates(modelWidget: IBaseWidget): void { + const targetWidgetId = modelWidget.widgetId + if (!targetWidgetId) return if (remoteModelUpdateSubscribed.has(modelWidget)) return remoteModelUpdateSubscribed.add(modelWidget) @@ -217,13 +219,13 @@ class Load3DConfiguration { // The widget (and its node) may have been removed since this // listener was registered; `onValueChange` has no per-widget scope, // so detect staleness here and detach rather than leaking forever. - if (!widgetValueStore.getWidget(modelWidget.widgetId ?? widgetId)) { + if (!widgetValueStore.getWidget(targetWidgetId)) { remoteModelUpdateSubscribed.delete(modelWidget) unsubscribe() return } if (!isRemoteMutationContext(context)) return - if (widgetId !== modelWidget.widgetId) return + if (widgetId !== targetWidgetId) return if (value === modelWidget.value) return modelWidget.value = value } From cb479153a02651223004ed49f474c609989e4754 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Thu, 17 Sep 2026 14:10:41 -0700 Subject: [PATCH 13/47] test(load3d): assert ignored updates preserve state Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4041388961 --- .../core/load3d/Load3DConfiguration.test.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 7243afb3786..6ce85abf377 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -925,6 +925,7 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('ignores local (non-remote) store changes to avoid double-loading', async () => { const load3d = makeLoad3dMock() + const onSceneInvalidated = vi.fn() const modelWidget = { value: 'none', widgetId: 'widget-2' @@ -932,9 +933,11 @@ describe('Load3DConfiguration remote (agent) model updates', () => { widgetValueStoreMock.widgets.set('widget-2', modelWidget) const config = new Load3DConfiguration(load3d) - config.configure({ modelWidget, loadFolder: 'input' }) + config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) await flush() vi.mocked(load3d.loadModel).mockClear() + vi.mocked(load3d.clearModel).mockClear() + onSceneInvalidated.mockClear() widgetValueStoreMock.emit({ widgetId: 'widget-2', @@ -945,10 +948,14 @@ describe('Load3DConfiguration remote (agent) model updates', () => { await flush() expect(load3d.loadModel).not.toHaveBeenCalled() + expect(load3d.clearModel).not.toHaveBeenCalled() + expect(onSceneInvalidated).not.toHaveBeenCalled() + expect(modelWidget.value).toBe('none') }) it('ignores value changes for a different widget id', async () => { const load3d = makeLoad3dMock() + const onSceneInvalidated = vi.fn() const modelWidget = { value: 'none', widgetId: 'widget-3' @@ -956,9 +963,11 @@ describe('Load3DConfiguration remote (agent) model updates', () => { widgetValueStoreMock.widgets.set('widget-3', modelWidget) const config = new Load3DConfiguration(load3d) - config.configure({ modelWidget, loadFolder: 'input' }) + config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) await flush() vi.mocked(load3d.loadModel).mockClear() + vi.mocked(load3d.clearModel).mockClear() + onSceneInvalidated.mockClear() widgetValueStoreMock.emit({ widgetId: 'some-other-widget', @@ -969,6 +978,9 @@ describe('Load3DConfiguration remote (agent) model updates', () => { await flush() expect(load3d.loadModel).not.toHaveBeenCalled() + expect(load3d.clearModel).not.toHaveBeenCalled() + expect(onSceneInvalidated).not.toHaveBeenCalled() + expect(modelWidget.value).toBe('none') }) it('does not register a second listener when configure runs again for the same widget', async () => { From 9838a748d9425aa6b567dd1b9d9dd044445c59b8 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Thu, 17 Sep 2026 14:11:29 -0700 Subject: [PATCH 14/47] test(load3d): use typed partial test doubles Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4041135452 https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4041171825 https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4041174219 --- .../core/load3d/Load3DConfiguration.test.ts | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 6ce85abf377..d618e100ba5 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -499,7 +499,7 @@ describe('Load3DConfiguration.configure forwards persisted + settings to load3d' let load3d: Load3d function makeLoad3dMock(): Load3d { - return { + return fromPartial({ loadModel: vi.fn().mockResolvedValue(undefined), setUpDirection: vi.fn(), setMaterialMode: vi.fn(), @@ -516,7 +516,7 @@ describe('Load3DConfiguration.configure forwards persisted + settings to load3d' setHDRIAsBackground: vi.fn(), setHDRIEnabled: vi.fn(), emitModelReady: vi.fn() - } as unknown as Load3d + }) } async function flush() { @@ -860,10 +860,10 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('reloads the model when the agent changes model_file via widgetValueStore', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = { + const modelWidget = fromPartial({ value: 'none', widgetId: 'widget-1' - } as unknown as IBaseWidget + }) widgetValueStoreMock.widgets.set('widget-1', modelWidget) const config = new Load3DConfiguration(load3d) @@ -898,10 +898,10 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('clears the model when the agent clears model_file via widgetValueStore', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = { + const modelWidget = fromPartial({ value: 'model.glb', widgetId: 'widget-clear' - } as unknown as IBaseWidget + }) widgetValueStoreMock.widgets.set('widget-clear', modelWidget) const config = new Load3DConfiguration(load3d) @@ -926,10 +926,10 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('ignores local (non-remote) store changes to avoid double-loading', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = { + const modelWidget = fromPartial({ value: 'none', widgetId: 'widget-2' - } as unknown as IBaseWidget + }) widgetValueStoreMock.widgets.set('widget-2', modelWidget) const config = new Load3DConfiguration(load3d) @@ -956,10 +956,10 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('ignores value changes for a different widget id', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = { + const modelWidget = fromPartial({ value: 'none', widgetId: 'widget-3' - } as unknown as IBaseWidget + }) widgetValueStoreMock.widgets.set('widget-3', modelWidget) const config = new Load3DConfiguration(load3d) @@ -986,10 +986,10 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('does not register a second listener when configure runs again for the same widget', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = { + const modelWidget = fromPartial({ value: 'none', widgetId: 'widget-4' - } as unknown as IBaseWidget + }) widgetValueStoreMock.widgets.set('widget-4', modelWidget) const config = new Load3DConfiguration(load3d) @@ -1050,10 +1050,10 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('subscribes again when the same widget is re-registered', async () => { const load3d = makeLoad3dMock() - const modelWidget = { + const modelWidget = fromPartial({ value: 'none', widgetId: 'widget-6' - } as unknown as IBaseWidget + }) widgetValueStoreMock.widgets.set('widget-6', modelWidget) const config = new Load3DConfiguration(load3d) From fce3a446a50f751c335d1e70a2582f38dffee76a Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Thu, 17 Sep 2026 16:24:57 -0700 Subject: [PATCH 15/47] refactor(load3d): bind widget reactions to viewer lifecycle Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4041388704 --- .../core/load3d/Load3DConfiguration.test.ts | 251 ++++++------------ .../core/load3d/Load3DConfiguration.ts | 160 +++-------- src/extensions/core/load3d/Load3d.ts | 8 + 3 files changed, 131 insertions(+), 288 deletions(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index d618e100ba5..b5f41ff5e03 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -1,5 +1,6 @@ import { fromAny, fromPartial } from '@total-typescript/shoehorn' import { beforeEach, describe, expect, it, vi } from 'vitest' +import { reactive } from 'vue' import type Load3d from '@/extensions/core/load3d/Load3d' import Load3DConfiguration, { @@ -21,7 +22,6 @@ import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import { useSettingStore } from '@/platform/settings/settingStore' import type { Settings } from '@/platform/settings/types' -import { useWidgetValueStore } from '@/stores/widgetValueStore' vi.mock(import('@/scripts/api'), () => ({ api: fromPartial({ @@ -49,30 +49,6 @@ vi.mock(import('@/extensions/core/load3d/Load3dUtils'), () => ({ }) })) -type ValueChangeListener = (change: { - widgetId: string - value: unknown - oldValue?: unknown - context?: unknown -}) => void - -const widgetValueStoreMock = { - listeners: new Set(), - widgets: new Map(), - emit(change: { - widgetId: string - value: unknown - oldValue?: unknown - context?: unknown - }) { - for (const listener of [...this.listeners]) listener(change) - }, - reset() { - this.listeners.clear() - this.widgets.clear() - } -} - type WithPrivate = { loadModelConfig(): ModelConfig loadSceneConfig(): SceneConfig @@ -89,6 +65,13 @@ function stubSettings(values: Partial) { vi.mocked(useSettingStore().get).mockImplementation((key) => values[key]) } +function reactiveWidget( + value: IBaseWidget['value'], + widgetId?: string +): IBaseWidget { + return reactive(fromPartial({ value, widgetId })) +} + const defaultGizmo: GizmoConfig = { enabled: false, mode: 'translate', @@ -230,7 +213,8 @@ describe('Load3DConfiguration.silentOnNotFound propagation', () => { setHDRIIntensity: vi.fn(), setHDRIAsBackground: vi.fn(), setHDRIEnabled: vi.fn(), - emitModelReady: vi.fn() + emitModelReady: vi.fn(), + setConfigurationCleanup: vi.fn() } as unknown as Load3d } @@ -515,7 +499,8 @@ describe('Load3DConfiguration.configure forwards persisted + settings to load3d' setHDRIIntensity: vi.fn(), setHDRIAsBackground: vi.fn(), setHDRIEnabled: vi.fn(), - emitModelReady: vi.fn() + emitModelReady: vi.fn(), + setConfigurationCleanup: vi.fn() }) } @@ -608,7 +593,8 @@ describe('Load3DConfiguration "none" model handling', () => { setHDRIIntensity: vi.fn(), setHDRIAsBackground: vi.fn(), setHDRIEnabled: vi.fn(), - emitModelReady: vi.fn() + emitModelReady: vi.fn(), + setConfigurationCleanup: vi.fn() } as unknown as Load3d } @@ -636,7 +622,7 @@ describe('Load3DConfiguration "none" model handling', () => { it('clears the model (and skips loadModel) when the widget value changes to "none"', async () => { const config = new Load3DConfiguration(load3d) - const widget = { value: 'model.glb' } as unknown as IBaseWidget + const widget = reactiveWidget('model.glb') config.configure({ modelWidget: widget, loadFolder: 'input' }) await flush() @@ -652,7 +638,7 @@ describe('Load3DConfiguration "none" model handling', () => { it('loads a model when the widget value transitions from "none" to a real path', async () => { const config = new Load3DConfiguration(load3d) - const widget = { value: 'none' } as unknown as IBaseWidget + const widget = reactiveWidget('none') config.configure({ modelWidget: widget, loadFolder: 'input' }) await flush() @@ -686,7 +672,8 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { setHDRIIntensity: vi.fn(), setHDRIAsBackground: vi.fn(), setHDRIEnabled: vi.fn(), - emitModelReady: vi.fn() + emitModelReady: vi.fn(), + setConfigurationCleanup: vi.fn() } as unknown as Load3d } @@ -701,8 +688,8 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { it('width.callback invokes onSceneInvalidated', async () => { const onSceneInvalidated = vi.fn() - const width = { value: 1024 } as unknown as IBaseWidget - const height = { value: 1024 } as unknown as IBaseWidget + const width = reactiveWidget(1024) + const height = reactiveWidget(1024) const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -714,15 +701,15 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }) await flush() - width.callback!(2048) + width.value = 2048 expect(onSceneInvalidated).toHaveBeenCalledTimes(1) }) it('height.callback invokes onSceneInvalidated', async () => { const onSceneInvalidated = vi.fn() - const width = { value: 1024 } as unknown as IBaseWidget - const height = { value: 1024 } as unknown as IBaseWidget + const width = reactiveWidget(1024) + const height = reactiveWidget(1024) const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -734,14 +721,14 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }) await flush() - height.callback!(2048) + height.value = 2048 expect(onSceneInvalidated).toHaveBeenCalledTimes(1) }) it('model_file widget callback invokes onSceneInvalidated after the model loads', async () => { const onSceneInvalidated = vi.fn() - const modelWidget = { value: 'none' } as unknown as IBaseWidget + const modelWidget = reactiveWidget('none') const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -760,10 +747,11 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { it('preserves any pre-existing model widget callback alongside the invalidation hook', async () => { const onSceneInvalidated = vi.fn() const original = vi.fn() - const modelWidget = { + const modelWidget = reactiveWidget('none') + Object.assign(modelWidget, { value: 'none', callback: original - } as unknown as IBaseWidget + }) const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -774,6 +762,7 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { await flush() modelWidget.value = 'model.glb' + modelWidget.callback?.('model.glb') await flush() expect(original).toHaveBeenCalledWith('model.glb') @@ -781,9 +770,9 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }) it('callbacks remain safe when onSceneInvalidated is omitted', async () => { - const width = { value: 1024 } as unknown as IBaseWidget - const height = { value: 1024 } as unknown as IBaseWidget - const modelWidget = { value: 'none' } as unknown as IBaseWidget + const width = reactiveWidget(1024) + const height = reactiveWidget(1024) + const modelWidget = reactiveWidget('none') const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -794,8 +783,12 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }) await flush() - expect(() => width.callback!(2048)).not.toThrow() - expect(() => height.callback!(2048)).not.toThrow() + expect(() => { + width.value = 2048 + }).not.toThrow() + expect(() => { + height.value = 2048 + }).not.toThrow() expect(() => { modelWidget.value = 'model.glb' }).not.toThrow() @@ -811,7 +804,8 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { // (and the next capture) stale. These tests pin the bridge that fixes it. describe('Load3DConfiguration remote (agent) model updates', () => { function makeLoad3dMock(): Load3d { - return { + let cleanup: (() => void) | undefined + return fromPartial({ loadModel: vi.fn().mockResolvedValue(undefined), clearModel: vi.fn(), setUpDirection: vi.fn(), @@ -828,31 +822,19 @@ describe('Load3DConfiguration remote (agent) model updates', () => { setHDRIIntensity: vi.fn(), setHDRIAsBackground: vi.fn(), setHDRIEnabled: vi.fn(), - emitModelReady: vi.fn() - } as unknown as Load3d + emitModelReady: vi.fn(), + setConfigurationCleanup: vi.fn((nextCleanup: () => void) => { + cleanup?.() + cleanup = nextCleanup + }) + }) } async function flush() { await new Promise((resolve) => setTimeout(resolve, 0)) } - const REMOTE_CONTEXT = { - source: 'agent-remote', - actor: 'agent', - opId: 'op-1' - } - beforeEach(() => { - widgetValueStoreMock.reset() - const widgetValueStore = useWidgetValueStore() - vi.mocked(widgetValueStore.onValueChange).mockImplementation((listener) => { - widgetValueStoreMock.listeners.add(listener as ValueChangeListener) - return () => - widgetValueStoreMock.listeners.delete(listener as ValueChangeListener) - }) - vi.mocked(widgetValueStore.getWidget).mockImplementation( - (id) => widgetValueStoreMock.widgets.get(id) as never - ) vi.mocked(Load3dUtils.splitFilePath).mockReturnValue(['', 'model.glb']) vi.mocked(Load3dUtils.getResourceURL).mockReturnValue('/view') }) @@ -860,11 +842,7 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('reloads the model when the agent changes model_file via widgetValueStore', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = fromPartial({ - value: 'none', - widgetId: 'widget-1' - }) - widgetValueStoreMock.widgets.set('widget-1', modelWidget) + const modelWidget = reactiveWidget('none', 'widget-1') const config = new Load3DConfiguration(load3d) config.configure({ @@ -878,12 +856,7 @@ describe('Load3DConfiguration remote (agent) model updates', () => { // Simulate the agent/CRDT follower's setWidget write: the store value // changes but nothing ever assigns `modelWidget.value` directly. - widgetValueStoreMock.emit({ - widgetId: 'widget-1', - value: 'agent-model.glb', - oldValue: 'none', - context: REMOTE_CONTEXT - }) + modelWidget.value = 'agent-model.glb' await flush() expect(load3d.loadModel).toHaveBeenCalledWith( @@ -898,11 +871,7 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('clears the model when the agent clears model_file via widgetValueStore', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = fromPartial({ - value: 'model.glb', - widgetId: 'widget-clear' - }) - widgetValueStoreMock.widgets.set('widget-clear', modelWidget) + const modelWidget = reactiveWidget('model.glb', 'widget-clear') const config = new Load3DConfiguration(load3d) config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) @@ -910,12 +879,7 @@ describe('Load3DConfiguration remote (agent) model updates', () => { vi.mocked(load3d.clearModel).mockClear() onSceneInvalidated.mockClear() - widgetValueStoreMock.emit({ - widgetId: 'widget-clear', - value: '', - oldValue: 'model.glb', - context: REMOTE_CONTEXT - }) + modelWidget.value = '' await flush() expect(load3d.clearModel).toHaveBeenCalledTimes(1) @@ -923,14 +887,10 @@ describe('Load3DConfiguration remote (agent) model updates', () => { expect(modelWidget.value).toBe('') }) - it('ignores local (non-remote) store changes to avoid double-loading', async () => { + it('reacts once to local model changes through the shared state path', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = fromPartial({ - value: 'none', - widgetId: 'widget-2' - }) - widgetValueStoreMock.widgets.set('widget-2', modelWidget) + const modelWidget = reactiveWidget('none', 'widget-2') const config = new Load3DConfiguration(load3d) config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) @@ -939,28 +899,20 @@ describe('Load3DConfiguration remote (agent) model updates', () => { vi.mocked(load3d.clearModel).mockClear() onSceneInvalidated.mockClear() - widgetValueStoreMock.emit({ - widgetId: 'widget-2', - value: 'local-change.glb', - oldValue: 'none', - context: undefined - }) + modelWidget.value = 'local-change.glb' await flush() - expect(load3d.loadModel).not.toHaveBeenCalled() + expect(load3d.loadModel).toHaveBeenCalledTimes(1) expect(load3d.clearModel).not.toHaveBeenCalled() - expect(onSceneInvalidated).not.toHaveBeenCalled() - expect(modelWidget.value).toBe('none') + expect(onSceneInvalidated).toHaveBeenCalledTimes(1) + expect(modelWidget.value).toBe('local-change.glb') }) it('ignores value changes for a different widget id', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = fromPartial({ - value: 'none', - widgetId: 'widget-3' - }) - widgetValueStoreMock.widgets.set('widget-3', modelWidget) + const modelWidget = reactiveWidget('none', 'widget-3') + const otherWidget = reactiveWidget('none', 'some-other-widget') const config = new Load3DConfiguration(load3d) config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) @@ -969,12 +921,7 @@ describe('Load3DConfiguration remote (agent) model updates', () => { vi.mocked(load3d.clearModel).mockClear() onSceneInvalidated.mockClear() - widgetValueStoreMock.emit({ - widgetId: 'some-other-widget', - value: 'agent-model.glb', - oldValue: 'none', - context: REMOTE_CONTEXT - }) + otherWidget.value = 'agent-model.glb' await flush() expect(load3d.loadModel).not.toHaveBeenCalled() @@ -986,34 +933,25 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('does not register a second listener when configure runs again for the same widget', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = fromPartial({ - value: 'none', - widgetId: 'widget-4' - }) - widgetValueStoreMock.widgets.set('widget-4', modelWidget) + const modelWidget = reactiveWidget('none', 'widget-4') const config = new Load3DConfiguration(load3d) config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) await flush() - expect(useWidgetValueStore().onValueChange).toHaveBeenCalledTimes(1) + expect(load3d.setConfigurationCleanup).toHaveBeenCalledTimes(2) vi.mocked(load3d.loadModel).mockClear() onSceneInvalidated.mockClear() - widgetValueStoreMock.emit({ - widgetId: 'widget-4', - value: 'agent-model.glb', - oldValue: 'none', - context: REMOTE_CONTEXT - }) + modelWidget.value = 'agent-model.glb' await flush() expect(load3d.loadModel).toHaveBeenCalledTimes(1) expect(onSceneInvalidated).toHaveBeenCalledTimes(1) }) - it('detaches once the widget is no longer registered (node removed)', async () => { + it('stops reacting as soon as the viewer lifecycle is removed', async () => { const load3d = makeLoad3dMock() const graph = new LGraph() const node = new LGraphNode('Load3D') @@ -1024,63 +962,42 @@ describe('Load3DConfiguration remote (agent) model updates', () => { 'none', () => undefined ) - const registeredWidgetId = modelWidget.widgetId - if (!registeredWidgetId) throw new Error('Expected a registered widget') - widgetValueStoreMock.widgets.set(registeredWidgetId, modelWidget) - const config = new Load3DConfiguration(load3d) config.configure({ modelWidget, loadFolder: 'input' }) await flush() - + const cleanup = vi + .mocked(load3d.setConfigurationCleanup) + .mock.calls.at(-1)?.[0] + if (!cleanup) throw new Error('Expected reactive configuration cleanup') graph.remove(node) expect(modelWidget.widgetId).toBeUndefined() - widgetValueStoreMock.widgets.delete(registeredWidgetId) - - expect(widgetValueStoreMock.listeners.size).toBe(1) - - widgetValueStoreMock.emit({ - widgetId: 'some-other-widget', - value: 'ignored.glb', - oldValue: 'none', - context: REMOTE_CONTEXT - }) - - expect(widgetValueStoreMock.listeners.size).toBe(0) + cleanup() + vi.mocked(load3d.loadModel).mockClear() + modelWidget.value = 'ignored.glb' + await flush() + expect(load3d.loadModel).not.toHaveBeenCalled() }) - it('subscribes again when the same widget is re-registered', async () => { + it('isolates a replacement widget that reuses the same id', async () => { const load3d = makeLoad3dMock() - const modelWidget = fromPartial({ - value: 'none', - widgetId: 'widget-6' - }) - widgetValueStoreMock.widgets.set('widget-6', modelWidget) + const oldWidget = reactiveWidget('none', 'widget-6') + const replacementWidget = reactiveWidget('none', 'widget-6') const config = new Load3DConfiguration(load3d) - config.configure({ modelWidget, loadFolder: 'input' }) - await flush() - widgetValueStoreMock.widgets.delete('widget-6') - widgetValueStoreMock.emit({ - widgetId: 'widget-6', - value: 'ignored.glb', - oldValue: 'none', - context: REMOTE_CONTEXT - }) - widgetValueStoreMock.widgets.set('widget-6', modelWidget) - config.configure({ modelWidget, loadFolder: 'input' }) + config.configure({ modelWidget: oldWidget, loadFolder: 'input' }) + config.configure({ modelWidget: replacementWidget, loadFolder: 'input' }) await flush() vi.mocked(load3d.loadModel).mockClear() - widgetValueStoreMock.emit({ - widgetId: 'widget-6', - value: 'restored.glb', - oldValue: 'none', - context: REMOTE_CONTEXT - }) + oldWidget.value = 'stale.glb' + replacementWidget.value = 'restored.glb' await flush() - expect(widgetValueStoreMock.listeners.size).toBe(1) expect(load3d.loadModel).toHaveBeenCalledTimes(1) - expect(modelWidget.value).toBe('restored.glb') + expect(load3d.loadModel).toHaveBeenCalledWith( + expect.any(String), + 'restored.glb', + { silentOnNotFound: false } + ) }) }) diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index 8ea77355831..c8c08a35137 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -1,3 +1,5 @@ +import { effectScope, watch } from 'vue' + import { LOAD3D_NONE_MODEL } from '@/extensions/core/load3d/constants' import type Load3d from '@/extensions/core/load3d/Load3d' import Load3dUtils from '@/extensions/core/load3d/Load3dUtils' @@ -14,24 +16,6 @@ import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { useSettingStore } from '@/platform/settings/settingStore' import { api } from '@/scripts/api' -import { useWidgetValueStore } from '@/stores/widgetValueStore' -import { isRemoteMutationContext } from '@/types/graphMutationContext' - -// One store subscription per widget instance, regardless of how many times -// `configure()` re-runs across this node's lifecycle (queue results, preview -// re-applies, etc). Keyed by the widget object itself so a widget torn down -// with its node is naturally released. -const remoteModelUpdateSubscribed = new WeakSet() - -type ModelWidgetBinding = { - onModelWidgetUpdate: ( - value: string | number | boolean | object - ) => Promise - onSceneInvalidated?: () => void - originalCallback?: IBaseWidget['callback'] -} - -const modelWidgetBindings = new WeakMap() type Load3DConfigurationSettings = { loadFolder: string @@ -85,38 +69,50 @@ class Load3DConfiguration { } configure(setting: Load3DConfigurationSettings) { - this.setupModelHandling( + const onModelWidgetUpdate = this.setupModelHandling( setting.modelWidget, setting.loadFolder, setting.cameraState, - setting.silentOnNotFound ?? false, - setting.onSceneInvalidated - ) - this.setupTargetSize( - setting.width, - setting.height, - setting.onSceneInvalidated + setting.silentOnNotFound ?? false ) + this.setupTargetSize(setting.width, setting.height) + this.setupReactiveHandling(setting, onModelWidgetUpdate) this.setupDefaultProperties(setting.bgImagePath) } - private setupTargetSize( - width?: IBaseWidget, - height?: IBaseWidget, - onSceneInvalidated?: () => void - ) { - if (width && height) { - this.load3d.setTargetSize(width.value as number, height.value as number) + private setupReactiveHandling( + setting: Load3DConfigurationSettings, + onModelWidgetUpdate: (value: IBaseWidget['value']) => Promise + ): void { + const scope = effectScope() + scope.run(() => { + watch( + () => setting.modelWidget.value, + (value) => { + void onModelWidgetUpdate(value) + setting.onSceneInvalidated?.() + }, + { flush: 'sync' } + ) - width.callback = (value: number) => { - this.load3d.setTargetSize(value, height.value as number) - onSceneInvalidated?.() + const { width, height } = setting + if (width && height) { + watch( + [() => width.value, () => height.value], + ([nextWidth, nextHeight]) => { + this.load3d.setTargetSize(nextWidth as number, nextHeight as number) + setting.onSceneInvalidated?.() + }, + { flush: 'sync' } + ) } + }) + this.load3d.setConfigurationCleanup(() => scope.stop()) + } - height.callback = (value: number) => { - this.load3d.setTargetSize(width.value as number, value) - onSceneInvalidated?.() - } + private setupTargetSize(width?: IBaseWidget, height?: IBaseWidget) { + if (width && height) { + this.load3d.setTargetSize(width.value as number, height.value as number) } } @@ -140,8 +136,7 @@ class Load3DConfiguration { modelWidget: IBaseWidget, loadFolder: string, cameraState?: CameraState, - silentOnNotFound: boolean = false, - onSceneInvalidated?: () => void + silentOnNotFound: boolean = false ) { const onModelWidgetUpdate = this.createModelUpdateHandler( loadFolder, @@ -152,84 +147,7 @@ class Load3DConfiguration { void onModelWidgetUpdate(modelWidget.value) } - const existingBinding = modelWidgetBindings.get(modelWidget) - if (existingBinding) { - existingBinding.onModelWidgetUpdate = onModelWidgetUpdate - existingBinding.onSceneInvalidated = onSceneInvalidated - this.subscribeToRemoteModelUpdates(modelWidget) - return - } - - const binding: ModelWidgetBinding = { - onModelWidgetUpdate, - onSceneInvalidated, - originalCallback: modelWidget.callback - } - modelWidgetBindings.set(modelWidget, binding) - - let currentValue = modelWidget.value - Object.defineProperty(modelWidget, 'value', { - get() { - return currentValue - }, - set(newValue) { - currentValue = newValue - if (modelWidget.callback && newValue !== undefined) { - modelWidget.callback(newValue) - } - }, - enumerable: true, - configurable: true - }) - - modelWidget.callback = (value: string | number | boolean | object) => { - void binding.onModelWidgetUpdate(value) - - if (binding.originalCallback) { - binding.originalCallback(value) - } - - binding.onSceneInvalidated?.() - } - - this.subscribeToRemoteModelUpdates(modelWidget) - } - - /** - * The agent/CRDT follower writes widget values straight into - * `widgetValueStore` (`graphMutations.ts`'s `setWidget`/`reconcileNode` - * commit path), never through this widget's own JS property. The - * `Object.defineProperty` override above only fires when something sets - * `modelWidget.value` directly, so a remote-origin `model_file` change - * would otherwise never reload the Three.js scene or invalidate the - * capture cache, leaving `serializeValue` (load3d.ts) returning a stale - * cached image/mask/normal/recording on the next queue or manual capture. - * Bridge the two by replaying the widget's own setter for value changes - * that originated remotely. - */ - private subscribeToRemoteModelUpdates(modelWidget: IBaseWidget): void { - const targetWidgetId = modelWidget.widgetId - if (!targetWidgetId) return - if (remoteModelUpdateSubscribed.has(modelWidget)) return - remoteModelUpdateSubscribed.add(modelWidget) - - const widgetValueStore = useWidgetValueStore() - const unsubscribe = widgetValueStore.onValueChange( - ({ widgetId, value, context }) => { - // The widget (and its node) may have been removed since this - // listener was registered; `onValueChange` has no per-widget scope, - // so detect staleness here and detach rather than leaking forever. - if (!widgetValueStore.getWidget(targetWidgetId)) { - remoteModelUpdateSubscribed.delete(modelWidget) - unsubscribe() - return - } - if (!isRemoteMutationContext(context)) return - if (widgetId !== targetWidgetId) return - if (value === modelWidget.value) return - modelWidget.value = value - } - ) + return onModelWidgetUpdate } private setupDefaultProperties(bgImagePath?: string) { @@ -372,7 +290,7 @@ class Load3DConfiguration { silentOnNotFound: boolean = false ) { let isFirstLoad = true - return async (value: string | number | boolean | object) => { + return async (value: IBaseWidget['value']) => { if (!value || value === LOAD3D_NONE_MODEL) { this.load3d.clearModel() return diff --git a/src/extensions/core/load3d/Load3d.ts b/src/extensions/core/load3d/Load3d.ts index 135f363d091..78073482ce9 100644 --- a/src/extensions/core/load3d/Load3d.ts +++ b/src/extensions/core/load3d/Load3d.ts @@ -62,6 +62,7 @@ class Load3d extends Viewport3d { animationManager: AnimationManager gizmoManager: GizmoManager adapterRef: AdapterRef + private configurationCleanup?: () => void private loadingPromise: Promise | null = null private _loadGeneration: number = 0 @@ -666,7 +667,14 @@ class Load3d extends Viewport3d { this.forceRender() } + setConfigurationCleanup(cleanup: () => void): void { + this.configurationCleanup?.() + this.configurationCleanup = cleanup + } + protected override disposeManagers(): void { + this.configurationCleanup?.() + this.configurationCleanup = undefined super.disposeManagers() this.hdriManager.dispose() this.loaderManager.dispose() From 847d9a21372bf8c56d25ab3fdd3366fcfbae99d1 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Thu, 17 Sep 2026 16:28:23 -0700 Subject: [PATCH 16/47] test(load3d): fuzz reactive model sequences --- .../core/load3d/Load3DConfiguration.test.ts | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index b5f41ff5e03..721b7875833 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -1,4 +1,5 @@ import { fromAny, fromPartial } from '@total-typescript/shoehorn' +import fc from 'fast-check' import { beforeEach, describe, expect, it, vi } from 'vitest' import { reactive } from 'vue' @@ -908,6 +909,44 @@ describe('Load3DConfiguration remote (agent) model updates', () => { expect(modelWidget.value).toBe('local-change.glb') }) + it('preserves exactly-once model effects across generated value sequences', async () => { + await fc.assert( + fc.asyncProperty( + fc.array(fc.constantFrom('', 'none', 'a.glb', 'b.obj'), { + minLength: 1, + maxLength: 20 + }), + async (values) => { + const load3d = makeLoad3dMock() + const modelWidget = reactiveWidget('none', 'generated-widget') + new Load3DConfiguration(load3d).configure({ + modelWidget, + loadFolder: 'input' + }) + vi.mocked(load3d.loadModel).mockClear() + vi.mocked(load3d.clearModel).mockClear() + + let previous = 'none' + let loads = 0 + let clears = 0 + for (const value of values) { + if (value !== previous) { + if (!value || value === 'none') clears++ + else loads++ + previous = value + } + modelWidget.value = value + } + await flush() + + expect(load3d.loadModel).toHaveBeenCalledTimes(loads) + expect(load3d.clearModel).toHaveBeenCalledTimes(clears) + } + ), + { numRuns: 100 } + ) + }) + it('ignores value changes for a different widget id', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() From 49049d664f1d81f792722ffbfa02daa830d203df Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Thu, 17 Sep 2026 16:30:18 -0700 Subject: [PATCH 17/47] test(load3d): kill deferred invalidation mutation --- .../core/load3d/Load3DConfiguration.test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 721b7875833..cbbc6793ee5 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -909,6 +909,22 @@ describe('Load3DConfiguration remote (agent) model updates', () => { expect(modelWidget.value).toBe('local-change.glb') }) + it('invalidates synchronously before queueing can observe stale capture state', () => { + const load3d = makeLoad3dMock() + const onSceneInvalidated = vi.fn() + const modelWidget = reactiveWidget('none', 'sync-widget') + new Load3DConfiguration(load3d).configure({ + modelWidget, + loadFolder: 'input', + onSceneInvalidated + }) + + modelWidget.value = 'replacement.glb' + + expect(onSceneInvalidated).toHaveBeenCalledTimes(1) + expect(load3d.loadModel).toHaveBeenCalledTimes(1) + }) + it('preserves exactly-once model effects across generated value sequences', async () => { await fc.assert( fc.asyncProperty( From aae454ba7555c74099ca7baaf4c864a3c467adfa Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Thu, 17 Sep 2026 16:32:10 -0700 Subject: [PATCH 18/47] test(load3d): cover reactive cleanup replacement --- src/extensions/core/load3d/Load3d.test.ts | 14 ++++++++++++++ src/extensions/core/load3d/Load3d.ts | 8 ++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/extensions/core/load3d/Load3d.test.ts b/src/extensions/core/load3d/Load3d.test.ts index 3fd825cede4..94e4a8cace0 100644 --- a/src/extensions/core/load3d/Load3d.test.ts +++ b/src/extensions/core/load3d/Load3d.test.ts @@ -159,6 +159,20 @@ function makeInstance() { describe('Load3d', () => { let ctx: ReturnType + it('replaces and clears reactive configuration cleanup exactly once', () => { + const load3d = Object.create(Load3d.prototype) as Load3d + const first = vi.fn() + const second = vi.fn() + + load3d.setConfigurationCleanup(first) + load3d.setConfigurationCleanup(second) + load3d.clearConfigurationCleanup() + load3d.clearConfigurationCleanup() + + expect(first).toHaveBeenCalledTimes(1) + expect(second).toHaveBeenCalledTimes(1) + }) + beforeEach(() => { ctx = makeInstance() }) diff --git a/src/extensions/core/load3d/Load3d.ts b/src/extensions/core/load3d/Load3d.ts index 78073482ce9..39a3c548e9c 100644 --- a/src/extensions/core/load3d/Load3d.ts +++ b/src/extensions/core/load3d/Load3d.ts @@ -668,13 +668,17 @@ class Load3d extends Viewport3d { } setConfigurationCleanup(cleanup: () => void): void { - this.configurationCleanup?.() + this.clearConfigurationCleanup() this.configurationCleanup = cleanup } - protected override disposeManagers(): void { + clearConfigurationCleanup(): void { this.configurationCleanup?.() this.configurationCleanup = undefined + } + + protected override disposeManagers(): void { + this.clearConfigurationCleanup() super.disposeManagers() this.hdriManager.dispose() this.loaderManager.dispose() From 082409b2557c59eae6118f377228cc9ae75f4b91 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Thu, 17 Sep 2026 16:34:37 -0700 Subject: [PATCH 19/47] test(load3d): flush reactive work without timers Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4041203987 --- .../core/load3d/Load3DConfiguration.test.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index cbbc6793ee5..83b94954041 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -1,7 +1,7 @@ import { fromAny, fromPartial } from '@total-typescript/shoehorn' import fc from 'fast-check' import { beforeEach, describe, expect, it, vi } from 'vitest' -import { reactive } from 'vue' +import { nextTick, reactive } from 'vue' import type Load3d from '@/extensions/core/load3d/Load3d' import Load3DConfiguration, { @@ -220,7 +220,8 @@ describe('Load3DConfiguration.silentOnNotFound propagation', () => { } async function flush() { - await new Promise((resolve) => setTimeout(resolve, 0)) + await nextTick() + await Promise.resolve() } beforeEach(() => { @@ -506,7 +507,8 @@ describe('Load3DConfiguration.configure forwards persisted + settings to load3d' } async function flush() { - await new Promise((resolve) => setTimeout(resolve, 0)) + await nextTick() + await Promise.resolve() } beforeEach(() => { @@ -600,7 +602,8 @@ describe('Load3DConfiguration "none" model handling', () => { } async function flush() { - await new Promise((resolve) => setTimeout(resolve, 0)) + await nextTick() + await Promise.resolve() } beforeEach(() => { @@ -679,7 +682,8 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { } async function flush() { - await new Promise((resolve) => setTimeout(resolve, 0)) + await nextTick() + await Promise.resolve() } beforeEach(() => { @@ -832,7 +836,8 @@ describe('Load3DConfiguration remote (agent) model updates', () => { } async function flush() { - await new Promise((resolve) => setTimeout(resolve, 0)) + await nextTick() + await Promise.resolve() } beforeEach(() => { From ee167d98dd0230c708c8e78459babfa5b0411e0a Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Thu, 17 Sep 2026 16:37:31 -0700 Subject: [PATCH 20/47] test(load3d): prove viewer teardown stops reactions Covers the production remove path for the lifecycle-owned reactive scope. --- src/extensions/core/load3d/Load3d.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/extensions/core/load3d/Load3d.test.ts b/src/extensions/core/load3d/Load3d.test.ts index 94e4a8cace0..a83eb7c5904 100644 --- a/src/extensions/core/load3d/Load3d.test.ts +++ b/src/extensions/core/load3d/Load3d.test.ts @@ -1483,7 +1483,10 @@ describe('Load3d', () => { expect(source(12, 34)).toBe(ndc) expect(clientPointToNdc).toHaveBeenCalledWith(12, 34) + const configurationCleanup = vi.fn() + load3d.setConfigurationCleanup(configurationCleanup) load3d.remove() + expect(configurationCleanup).toHaveBeenCalledOnce() }) }) }) From 6a015cf37bf745471afc43e32bb74938212719f7 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 17 Sep 2026 18:42:42 -0700 Subject: [PATCH 21/47] fix(load3d): await pending model load before scene capture Load3D serializeValue snapshotted and captured whatever mesh was on screen, even while a model swap was still loading. Wait for whenLoadIdle so the queued prompt captures the model that was actually set. --- src/extensions/core/load3d.test.ts | 28 ++++++++++++++++++++++++++-- src/extensions/core/load3d.ts | 4 ++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/extensions/core/load3d.test.ts b/src/extensions/core/load3d.test.ts index abadc873b7e..41e6a6dd8c8 100644 --- a/src/extensions/core/load3d.test.ts +++ b/src/extensions/core/load3d.test.ts @@ -1109,6 +1109,7 @@ describe('Comfy.Load3D scene widget serializeValue caching', () => { function makeFullFakeLoad3d() { return { + whenLoadIdle: vi.fn(async () => {}), getCurrentCameraType: vi.fn(() => 'perspective'), cameraManager: { perspectiveCamera: { fov: 35 } }, getCameraState: vi.fn(() => ({ position: { x: 0, y: 0, z: 0 } })), @@ -1144,16 +1145,39 @@ describe('Comfy.Load3D scene widget serializeValue caching', () => { { name: 'image', value: '' } ] const node = makeLoad3DNode({ widgets, properties: {} }) - useLoad3dModule.nodeToLoad3dMap.set(node, makeFullFakeLoad3d() as never) + const load3d = makeFullFakeLoad3d() + useLoad3dModule.nodeToLoad3dMap.set(node, load3d as never) await load3DExt.nodeCreated!(node, app) const serialize = widgets[3].serializeValue! as () => Promise<{ image: string } | null> - return { node, serialize, uploadTempImage, useLoad3dModule } + return { node, load3d, serialize, uploadTempImage, useLoad3dModule } } + it('waits for a pending model load before capturing the scene', async () => { + const { load3d, serialize } = await setup() + let releaseLoad!: () => void + load3d.whenLoadIdle.mockImplementation( + () => + new Promise((resolve) => { + releaseLoad = resolve + }) + ) + + const pending = serialize() + await flush() + expect(load3d.whenLoadIdle).toHaveBeenCalledTimes(1) + expect(load3d.captureScene).not.toHaveBeenCalled() + + releaseLoad() + const result = await pending + + expect(load3d.captureScene).toHaveBeenCalledTimes(1) + expect(result?.image).toBe('threed/scene-1.png [temp]') + }) + it('reuses the cached output when the scene has not been dirtied', async () => { const { node, serialize, uploadTempImage, useLoad3dModule } = await setup() diff --git a/src/extensions/core/load3d.ts b/src/extensions/core/load3d.ts index dbe6fb3b2e8..6d92b2894eb 100644 --- a/src/extensions/core/load3d.ts +++ b/src/extensions/core/load3d.ts @@ -425,6 +425,10 @@ useExtensionService().registerExtension({ if (cached) return cached } + // A model swap (user or agent) may still be loading. Capture the + // scene the queue will actually run, not the one being replaced. + await currentLoad3d.whenLoadIdle() + const { camera_info, model_3d_info } = snapshotLoad3dState( node, currentLoad3d From 5dcc7623faf2aaf0be496e94ae1aa819626939b6 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 17 Sep 2026 18:45:03 -0700 Subject: [PATCH 22/47] refactor(load3d): make clearConfigurationCleanup private Callers only need setConfigurationCleanup; teardown runs through remove(). Cover replacement and disposal through the public surface. --- src/extensions/core/load3d/Load3d.test.ts | 34 +++++++++++------------ src/extensions/core/load3d/Load3d.ts | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/extensions/core/load3d/Load3d.test.ts b/src/extensions/core/load3d/Load3d.test.ts index a83eb7c5904..e5ac1ce0d95 100644 --- a/src/extensions/core/load3d/Load3d.test.ts +++ b/src/extensions/core/load3d/Load3d.test.ts @@ -159,20 +159,6 @@ function makeInstance() { describe('Load3d', () => { let ctx: ReturnType - it('replaces and clears reactive configuration cleanup exactly once', () => { - const load3d = Object.create(Load3d.prototype) as Load3d - const first = vi.fn() - const second = vi.fn() - - load3d.setConfigurationCleanup(first) - load3d.setConfigurationCleanup(second) - load3d.clearConfigurationCleanup() - load3d.clearConfigurationCleanup() - - expect(first).toHaveBeenCalledTimes(1) - expect(second).toHaveBeenCalledTimes(1) - }) - beforeEach(() => { ctx = makeInstance() }) @@ -1482,11 +1468,25 @@ describe('Load3d', () => { expect(source(12, 34)).toBe(ndc) expect(clientPointToNdc).toHaveBeenCalledWith(12, 34) + }) + + it('runs the replaced configuration cleanup immediately and the current one once on remove()', () => { + const { container, deps } = makeConstructorDeps() + const load3d = new Load3d(container, deps) + const first = vi.fn() + const second = vi.fn() + + load3d.setConfigurationCleanup(first) + expect(first).not.toHaveBeenCalled() - const configurationCleanup = vi.fn() - load3d.setConfigurationCleanup(configurationCleanup) + load3d.setConfigurationCleanup(second) + expect(first).toHaveBeenCalledOnce() + expect(second).not.toHaveBeenCalled() + + load3d.remove() load3d.remove() - expect(configurationCleanup).toHaveBeenCalledOnce() + expect(first).toHaveBeenCalledOnce() + expect(second).toHaveBeenCalledOnce() }) }) }) diff --git a/src/extensions/core/load3d/Load3d.ts b/src/extensions/core/load3d/Load3d.ts index 39a3c548e9c..e217ba28bdc 100644 --- a/src/extensions/core/load3d/Load3d.ts +++ b/src/extensions/core/load3d/Load3d.ts @@ -672,7 +672,7 @@ class Load3d extends Viewport3d { this.configurationCleanup = cleanup } - clearConfigurationCleanup(): void { + private clearConfigurationCleanup(): void { this.configurationCleanup?.() this.configurationCleanup = undefined } From e351a1dffec353416ec808269798e38aeff1baf3 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 17 Sep 2026 18:51:31 -0700 Subject: [PATCH 23/47] test(load3d): pin Load3DConfiguration reactive widget contract Frame the suite as the reactive widget contract Load3DConfiguration relies on, drop the widget-id filter test that no longer describes production behaviour, and add a store-backed case that drives model_file through widgetValueStore the way the CRDT follower does. --- .../core/load3d/Load3DConfiguration.test.ts | 110 ++++++++++-------- 1 file changed, 61 insertions(+), 49 deletions(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 83b94954041..9c625c54cf7 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -22,6 +22,7 @@ import type { Dictionary } from '@/lib/litegraph/src/interfaces' import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import { useSettingStore } from '@/platform/settings/settingStore' +import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { Settings } from '@/platform/settings/types' vi.mock(import('@/scripts/api'), () => ({ @@ -66,11 +67,8 @@ function stubSettings(values: Partial) { vi.mocked(useSettingStore().get).mockImplementation((key) => values[key]) } -function reactiveWidget( - value: IBaseWidget['value'], - widgetId?: string -): IBaseWidget { - return reactive(fromPartial({ value, widgetId })) +function reactiveWidget(value: IBaseWidget['value']): IBaseWidget { + return reactive(fromPartial({ value })) } const defaultGizmo: GizmoConfig = { @@ -800,14 +798,12 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }) }) -// PM-1011: after the agent interacts with the graph, Load3D preview capture -// stopped working. Root cause: the agent/CRDT follower writes widget values -// straight into widgetValueStore (graphMutations.ts `setWidget`), bypassing -// this file's `Object.defineProperty` override on `modelWidget.value`. That -// override is the only thing that reloads the Three.js scene and marks the -// capture cache dirty, so a remote-origin model_file change left the scene -// (and the next capture) stale. These tests pin the bridge that fixes it. -describe('Load3DConfiguration remote (agent) model updates', () => { +// Load3DConfiguration owns no widget listeners of its own. It observes +// `modelWidget.value`, `width.value`, and `height.value` reactively, so any +// writer that goes through widget state (user edit, widgetValueStore.setValue +// from the CRDT follower, undo) reloads the scene and invalidates the capture +// cache. These tests pin that reactive contract. +describe('Load3DConfiguration reactive widget contract', () => { function makeLoad3dMock(): Load3d { let cleanup: (() => void) | undefined return fromPartial({ @@ -845,10 +841,10 @@ describe('Load3DConfiguration remote (agent) model updates', () => { vi.mocked(Load3dUtils.getResourceURL).mockReturnValue('/view') }) - it('reloads the model when the agent changes model_file via widgetValueStore', async () => { + it('reloads the model when model_file changes', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = reactiveWidget('none', 'widget-1') + const modelWidget = reactiveWidget('none') const config = new Load3DConfiguration(load3d) config.configure({ @@ -860,8 +856,6 @@ describe('Load3DConfiguration remote (agent) model updates', () => { vi.mocked(load3d.loadModel).mockClear() onSceneInvalidated.mockClear() - // Simulate the agent/CRDT follower's setWidget write: the store value - // changes but nothing ever assigns `modelWidget.value` directly. modelWidget.value = 'agent-model.glb' await flush() @@ -874,10 +868,10 @@ describe('Load3DConfiguration remote (agent) model updates', () => { expect(modelWidget.value).toBe('agent-model.glb') }) - it('clears the model when the agent clears model_file via widgetValueStore', async () => { + it('clears the model when model_file is emptied', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = reactiveWidget('model.glb', 'widget-clear') + const modelWidget = reactiveWidget('model.glb') const config = new Load3DConfiguration(load3d) config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) @@ -893,10 +887,10 @@ describe('Load3DConfiguration remote (agent) model updates', () => { expect(modelWidget.value).toBe('') }) - it('reacts once to local model changes through the shared state path', async () => { + it('reacts exactly once per model_file change', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = reactiveWidget('none', 'widget-2') + const modelWidget = reactiveWidget('none') const config = new Load3DConfiguration(load3d) config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) @@ -917,7 +911,7 @@ describe('Load3DConfiguration remote (agent) model updates', () => { it('invalidates synchronously before queueing can observe stale capture state', () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = reactiveWidget('none', 'sync-widget') + const modelWidget = reactiveWidget('none') new Load3DConfiguration(load3d).configure({ modelWidget, loadFolder: 'input', @@ -939,7 +933,7 @@ describe('Load3DConfiguration remote (agent) model updates', () => { }), async (values) => { const load3d = makeLoad3dMock() - const modelWidget = reactiveWidget('none', 'generated-widget') + const modelWidget = reactiveWidget('none') new Load3DConfiguration(load3d).configure({ modelWidget, loadFolder: 'input' @@ -968,32 +962,10 @@ describe('Load3DConfiguration remote (agent) model updates', () => { ) }) - it('ignores value changes for a different widget id', async () => { - const load3d = makeLoad3dMock() - const onSceneInvalidated = vi.fn() - const modelWidget = reactiveWidget('none', 'widget-3') - const otherWidget = reactiveWidget('none', 'some-other-widget') - - const config = new Load3DConfiguration(load3d) - config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) - await flush() - vi.mocked(load3d.loadModel).mockClear() - vi.mocked(load3d.clearModel).mockClear() - onSceneInvalidated.mockClear() - - otherWidget.value = 'agent-model.glb' - await flush() - - expect(load3d.loadModel).not.toHaveBeenCalled() - expect(load3d.clearModel).not.toHaveBeenCalled() - expect(onSceneInvalidated).not.toHaveBeenCalled() - expect(modelWidget.value).toBe('none') - }) - it('does not register a second listener when configure runs again for the same widget', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() - const modelWidget = reactiveWidget('none', 'widget-4') + const modelWidget = reactiveWidget('none') const config = new Load3DConfiguration(load3d) config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) @@ -1038,10 +1010,50 @@ describe('Load3DConfiguration remote (agent) model updates', () => { expect(load3d.loadModel).not.toHaveBeenCalled() }) - it('isolates a replacement widget that reuses the same id', async () => { + it('reloads the model when the CRDT follower writes model_file through widgetValueStore', async () => { + const load3d = makeLoad3dMock() + const onSceneInvalidated = vi.fn() + const graph = new LGraph() + const node = new LGraphNode('Load3D') + graph.add(node) + const modelWidget = node.addWidget( + 'string', + 'model_file', + 'none', + () => undefined + ) + const widgetId = modelWidget.widgetId + if (!widgetId) throw new Error('Expected a store-backed widget id') + + new Load3DConfiguration(load3d).configure({ + modelWidget, + loadFolder: 'input', + onSceneInvalidated + }) + await flush() + vi.mocked(load3d.loadModel).mockClear() + onSceneInvalidated.mockClear() + + const applied = useWidgetValueStore().setValue(widgetId, 'agent.glb', { + source: 'agent-remote', + actor: 'agent:e2e', + opId: 'op-1' + }) + + expect(applied).toBe(true) + expect(onSceneInvalidated).toHaveBeenCalledTimes(1) + expect(load3d.loadModel).toHaveBeenCalledWith( + expect.any(String), + 'agent.glb', + { silentOnNotFound: false } + ) + expect(modelWidget.value).toBe('agent.glb') + }) + + it('stops reacting to the previous widget once configure runs with a replacement', async () => { const load3d = makeLoad3dMock() - const oldWidget = reactiveWidget('none', 'widget-6') - const replacementWidget = reactiveWidget('none', 'widget-6') + const oldWidget = reactiveWidget('none') + const replacementWidget = reactiveWidget('none') const config = new Load3DConfiguration(load3d) config.configure({ modelWidget: oldWidget, loadFolder: 'input' }) From c986009ea45ba58cebf3f401ad8ba8c373a00715 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 17 Sep 2026 19:50:58 -0700 Subject: [PATCH 24/47] test(load3d): drive agent model swaps through the follower boundary Replace the spec that assigned modelWidget.value directly with a fixture that runs production's path: the panel opens the agent tab, the follower subscribes over /ws, and a fake host applies recorded set_widget ops that reach the Load3D node through the doc frames the real client parses. The spec holds the replacement model's fetch while queueing, asserting the queue waits for the model that will actually run. Mutation evidence: removing `await currentLoad3d.whenLoadIdle()` in load3d.ts serializeValue fails the spec with "a prompt was posted before the model finished loading"; restoring it passes. The onSceneInvalidated call in the model watch is not discriminated here because model load already marks the scene dirty via viewer events; Load3DConfiguration.test.ts pins it. --- browser_tests/fixtures/load3dAgentFixture.ts | 389 ++++++++++++++++++ .../tests/load3d/load3dAgentUpdate.spec.ts | 236 ++--------- 2 files changed, 426 insertions(+), 199 deletions(-) create mode 100644 browser_tests/fixtures/load3dAgentFixture.ts diff --git a/browser_tests/fixtures/load3dAgentFixture.ts b/browser_tests/fixtures/load3dAgentFixture.ts new file mode 100644 index 00000000000..40eedcc44cb --- /dev/null +++ b/browser_tests/fixtures/load3dAgentFixture.ts @@ -0,0 +1,389 @@ +import type { Page, Request, WebSocketRoute } from '@playwright/test' +import { expect } from '@playwright/test' + +import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } +import { zComfyWorkflow } from '@/platform/workflow/validation/schemas/workflowSchema' +import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' +import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' +import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' +import { toNodeId } from '@/types/nodeId' + +import load3dWorkflow from '@e2e/assets/3d/load3d_node.json' with { type: 'json' } +import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' +import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' +import { agentTest, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import type { RecordedGraphOperation } from '@e2e/fixtures/data/agent/agentConversation' +import { TestIds } from '@e2e/fixtures/selectors' +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' +import { assetPath } from '@e2e/fixtures/utils/paths' +import { Load3DHelper } from '@e2e/tests/load3d/Load3DHelper' + +const WORKFLOW_ID = 'a81718a4-02ae-41e6-ae85-c33b7bb880f6' +const SOCKET_SID = '5b0e2c9a-6f1d-4a83-9c27-3e4f5a6b7c8d' +const NODE_ID = '1' +const PANEL_MOUNT_TIMEOUT = 30_000 +const SUBSCRIBE_TIMEOUT = 15_000 + +// The backend's Load3D definition, in `widgets_values` order; the host applier +// rejects a `set_widget` naming anything outside this list. +const LOAD3D_CATALOG: WidgetCatalog = { + types: { + Load3D: { widget_order: ['model_file', 'image', 'width', 'height'] } + } +} + +// The prompt names a capture as `threed/ [temp]`. +const PROMPT_IMAGE_REF = /^threed\/(.+) \[temp\]$/ + +/** Model files the fixture serves from `browser_tests/assets/3d`. */ +type AgentModelFile = 'cube.obj' | 'workflow.glb' + +const MODEL_ASSETS: Record = { + 'cube.obj': 'cube.obj', + 'workflow.glb': 'animated_triangle.glb' +} + +export interface Load3dCapture { + /** The `image` reference the queued prompt carried for the Load3D node. */ + promptImage: string + /** The PNG the viewer uploaded under that reference. */ + imageBytes: Uint8Array +} + +// The seed the host holds: the checked-in Load3D workflow with its widgets +// stored by name, so nothing here depends on positional order. +function seedWorkflow(): WorkflowJSON { + const { extra, ...parsed } = zComfyWorkflow.parse(load3dWorkflow) + const nodes = parsed.nodes.map((node) => ({ + ...node, + widgets_values: { model_file: '', width: 1024, height: 1024 } + })) + // The workflow schema allows `extra: null`; the multi-player seed does not. + return { ...parsed, extra: extra ?? undefined, nodes, links: [] } +} + +function promptImageRef(body: unknown): string { + const prompt = ( + body as { prompt?: Record }> } + ).prompt + const image = Object.values(prompt ?? {}).find( + (node) => node.inputs?.image !== undefined + )?.inputs?.image + const ref = (image as { image?: unknown } | undefined)?.image + if (typeof ref !== 'string') + throw new Error('the queued prompt carries no Load3D image reference') + return ref +} + +async function uploadedImageBytes(request: Request): Promise { + const body = request.postDataBuffer() + if (!body) throw new Error('upload request has no body') + const form = await new Response(new Uint8Array(body), { + headers: { 'content-type': request.headers()['content-type'] ?? '' } + }).formData() + const image = form.get('image') + if (!(image instanceof File)) throw new Error('upload has no image file') + return new Uint8Array(await image.arrayBuffer()) +} + +async function withTimeout( + promise: Promise, + ms: number, + message: string +): Promise { + let timer: ReturnType | undefined + const timeout = new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error(message)), ms) + }) + try { + return await Promise.race([promise, timeout]) + } finally { + clearTimeout(timer) + } +} + +/** + * One Load3D node bound to an agent workflow, driven the way production is: + * the panel opens the agent's tab, the follower subscribes over `/ws`, and the + * fake host applies recorded `set_widget` ops that reach the node through the + * doc frames the real client parses. + */ +class Load3dAgentHarness { + readonly viewer: Load3DHelper + + private readonly host = new HostDoc( + WORKFLOW_ID, + seedWorkflow(), + LOAD3D_CATALOG + ) + private socket: WebSocketRoute | null = null + private resolveSubscribed: (() => void) | null = null + private readonly subscribed = new Promise((resolve) => { + this.resolveSubscribed = resolve + }) + private uploads = 0 + private readonly uploadsByName = new Map() + private readonly heldModels = new Map>() + + constructor(private readonly page: Page) { + this.viewer = new Load3DHelper(page.locator(`[data-node-id="${NODE_ID}"]`)) + } + + async boot(agentFlag: boolean): Promise { + await this.mockAgentApi() + await this.mockLoad3dIo() + // The follower re-drives a pending subscribe only on a status frame, which every real connect sends. + await this.page.routeWebSocket(/\/ws/, (socket) => { + this.socket = socket + socket.onMessage((raw) => this.onClientFrame(raw)) + socket.send( + JSON.stringify({ + type: 'status', + data: { + status: { exec_info: { queue_remaining: 0 } }, + sid: SOCKET_SID + } + }) + ) + }) + await bootAgentApp(this.page, agentFlag, { + // Only the Vue node renderer projects follower edits onto the canvas. + settings: { + 'Comfy.VueNodes.Enabled': true, + 'Comfy.Graph.CanvasInfo': false + }, + // The node materializes from the black-box backend's Load3D definition. + objectInfo: 'server' + }) + await this.page + .getByRole('button', { name: enMessages.agent.askComfyAgent }) + .click() + await expect(this.page.locator('#agent-panel-root')).toBeVisible({ + timeout: PANEL_MOUNT_TIMEOUT + }) + // The agent announces its tab; the panel opens it and binds the follower. + this.send({ + type: 'agent_active_tab', + data: { workflow_id: WORKFLOW_ID, name: 'Load3D preview' } + }) + await withTimeout( + this.subscribed, + SUBSCRIBE_TIMEOUT, + 'the follower never subscribed to the agent workflow after agent_active_tab' + ) + await expect(this.viewer.node).toBeVisible() + await expect(this.viewer.canvas).toBeVisible() + } + + /** + * The agent sets `model_file`; the value travels as a host doc frame. The + * envelope (`op_id`, actor, version) is minted by the host, as on the wire. + */ + setModelFromAgent(model: AgentModelFile): void { + const widgets = this.host.graph().nodes[NODE_ID]?.widgets + const old = + typeof widgets === 'object' && widgets !== null + ? (widgets as Record)['model_file'] + : undefined + const op: RecordedGraphOperation = { + op: 'set_widget', + node_id: NODE_ID, + widget: 'model_file', + old, + value: model + } + this.send(this.host.apply([op])) + } + + /** Waits until the node's `model_file` widget shows `model`. */ + async expectModel(model: AgentModelFile): Promise { + await expect + .poll(() => + this.page.evaluate( + (nodeId) => + window + .app!.graph.getNodeById(nodeId) + ?.widgets?.find((widget) => widget.name === 'model_file')?.value, + toNodeId(NODE_ID) + ) + ) + .toBe(model) + } + + /** The viewer's own loading overlay, shown while a model file is in flight. */ + get loadingOverlay() { + return this.viewer.node.getByTestId(TestIds.loading.overlay) + } + + /** + * Stops serving `model` until the returned release runs. A capture queued + * while the file is held must wait: the prompt is not allowed to carry the + * scene the swap is replacing. + */ + holdModel(model: AgentModelFile): () => void { + let release: () => void = () => {} + this.heldModels.set( + model, + new Promise((resolve) => { + release = resolve + }) + ) + return () => { + this.heldModels.delete(model) + release() + } + } + + /** Clicks Queue and resolves once the prompt for it has been posted. */ + async queuePrompt(): Promise { + const posted = this.nextPrompt() + await this.page.getByTestId(TestIds.topbar.queueButton).click() + return posted + } + + /** + * Lets a held model finish loading and returns the prompt the queue click + * made while it was held; that click is still waiting on the viewer. + */ + async releaseAndAwaitPrompt(release: () => void): Promise { + const posted = this.nextPrompt() + release() + return posted + } + + private async nextPrompt(): Promise { + const request = await this.page.waitForRequest( + (candidate) => + candidate.method() === 'POST' && + new URL(candidate.url()).pathname === '/api/prompt' + ) + return request.postDataJSON() + } + + /** Asserts that no prompt reaches the server within `ms` of clicking Queue. */ + async expectQueueHeld(ms: number): Promise { + let posted = false + const onRequest = (request: Request) => { + if ( + request.method() === 'POST' && + new URL(request.url()).pathname === '/api/prompt' + ) + posted = true + } + this.page.on('request', onRequest) + try { + await this.page.getByTestId(TestIds.topbar.queueButton).click() + // Negative assertion: the fixed window is the check itself. There is no + // page condition to wait for because the expected outcome is that + // nothing happens while the model route is held. + await new Promise((resolve) => setTimeout(resolve, ms)) + expect( + posted, + 'a prompt was posted before the model finished loading' + ).toBe(false) + } finally { + this.page.off('request', onRequest) + } + } + + /** Reads the capture the last queued prompt carried and its uploaded bytes. */ + async captureFor(promptBody: unknown): Promise { + const promptImage = promptImageRef(promptBody) + const name = PROMPT_IMAGE_REF.exec(promptImage)?.[1] + const upload = name === undefined ? undefined : this.uploadsByName.get(name) + if (!upload) + throw new Error( + `the prompt references ${promptImage}, which was never uploaded` + ) + return { promptImage, imageBytes: await uploadedImageBytes(upload) } + } + + /** Queues a prompt and returns the capture it carried. */ + async capture(): Promise { + return this.captureFor(await this.queuePrompt()) + } + + private async mockAgentApi(): Promise { + const { page } = this + await page.route('**/api/agent/threads', (route) => + route.fulfill(jsonRoute({ threads: [] })) + ) + await page.route('**/api/agent/threads/*/messages', (route) => + route.fulfill(jsonRoute([])) + ) + // No cloud workflow carries the agent's id, so the panel opens a new tab for it. + await page.route('**/api/workflows**', (route) => + route.fulfill( + jsonRoute({ + data: [], + pagination: { has_more: false, limit: 100, offset: 0, total: 0 } + }) + ) + ) + } + + private async mockLoad3dIo(): Promise { + const { page } = this + await page.route('**/api/upload/image**', (route) => { + const name = `agent-load3d-capture-${++this.uploads}.png` + this.uploadsByName.set(name, route.request()) + return route.fulfill(jsonRoute({ name, subfolder: 'temp', type: 'temp' })) + }) + for (const model of Object.keys(MODEL_ASSETS) as AgentModelFile[]) { + await page.route(`**/api/view?*filename=${model}*`, async (route) => { + await this.heldModels.get(model) + return route.fulfill({ path: assetPath(MODEL_ASSETS[model]) }) + }) + } + await page.route(/\/api\/prompt$/, (route) => { + if (route.request().method() !== 'POST') return route.fallback() + return route.fulfill( + jsonRoute({ prompt_id: crypto.randomUUID(), node_errors: {} }) + ) + }) + } + + private send(frame: AgentWsEvent | HostFrame): void { + // Every frame must satisfy production's own parser, so a host that stopped + // emitting a required field fails here, not silently on the client. + if (frame.type.startsWith('doc_') || frame.type === 'awareness') { + if (parseServerDocFrame(frame) === null) + throw new Error(`host frame ${frame.type} is not a valid doc frame`) + } else if (!parseAgentWsEvent(frame).success) { + throw new Error(`agent event ${frame.type} is not a valid agent event`) + } + if (!this.socket) throw new Error('the app has not opened /ws yet') + this.socket.send(JSON.stringify(frame)) + } + + private onClientFrame(raw: string | Buffer): void { + const frame: unknown = JSON.parse(raw.toString()) + if (typeof frame !== 'object' || frame === null) return + const { type, data } = frame as { type?: unknown; data?: unknown } + if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) + return + const { workflow_id, state_vector_b64 } = data as { + workflow_id?: unknown + state_vector_b64?: unknown + } + if (workflow_id !== WORKFLOW_ID || typeof state_vector_b64 !== 'string') + return + this.send(this.host.subscribed()) + this.send(this.host.catchUp(state_vector_b64)) + this.resolveSubscribed?.() + } +} + +interface Load3dAgentFixtures { + load3dAgent: Load3dAgentHarness +} + +export const load3dAgentTest = agentTest.extend({ + load3dAgent: async ({ page, agentFlagEnabled }, use) => { + const harness = new Load3dAgentHarness(page) + await harness.boot(agentFlagEnabled) + await use(harness) + } +}) diff --git a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts index 5f5f7660035..4ef1ddd7e77 100644 --- a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts +++ b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts @@ -1,211 +1,49 @@ -import type { Page, Request } from '@playwright/test' import { expect } from '@playwright/test' -import workflow from '@e2e/assets/3d/load3d_node.json' with { type: 'json' } -import { cloudAppFixture, waitForCloudApp } from '@e2e/fixtures/cloudAppFixture' -import { mockBilling } from '@e2e/fixtures/utils/cloudBillingMocks' -import { bootCloud, mockCloudBoot } from '@e2e/fixtures/utils/cloudBootMocks' -import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' -import { assetPath } from '@e2e/fixtures/utils/paths' -import { Load3DHelper } from '@e2e/tests/load3d/Load3DHelper' -import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' -import { toNodeId } from '@/types/nodeId' +import type { Load3dCapture } from '@e2e/fixtures/load3dAgentFixture' +import { load3dAgentTest as test } from '@e2e/fixtures/load3dAgentFixture' -const test = cloudAppFixture -const APP_URL = process.env.PLAYWRIGHT_TEST_URL || 'http://localhost:8188' -const WORKFLOW_ID = 'a81718a4-02ae-41e6-ae85-c33b7bb880f6' -const THREAD_ID = 'd4c016c4-3b8c-44cf-97de-1ae27e43e718' - -function promptImage(body: unknown): string { - const prompt = ( - body as { prompt?: Record }> } - ).prompt - const image = Object.values(prompt ?? {}).find( - (node) => node.inputs?.image !== undefined - )?.inputs?.image - expect(image).toBeTruthy() - return (image as { image: string }).image -} - -async function bootAgentLoad3d(page: Page): Promise { - await page.addInitScript(() => { - localStorage.setItem('Comfy.AgentPanel.onboarded', 'true') - localStorage.setItem('Comfy.Agent.CrdtFollower', 'true') - }) - await mockCloudBoot(page, { - features: { - posthog_project_token: 'phc_e2e_agent_load3d', - posthog_config: { - advanced_disable_flags: true, - bootstrap: { featureFlags: { 'agent-in-app-experience': true } } - } - }, - settings: { - 'Comfy.TutorialCompleted': true, - 'Comfy.VueNodes.Enabled': true, - 'Comfy.RightSidePanel.ShowErrorsTab': false - } - }) - // Keep the cloud shell deterministic, but use the real black-box backend's - // Load3D definition so this exercises the production node/viewer. - await page.unroute('**/api/object_info') - await mockBilling(page) - await page.route('**/api/assets**', (route) => - route.fulfill(jsonRoute({ assets: [], total: 0, has_more: false })) - ) - let capture = 0 - const uploadRequests: Request[] = [] - await page.route('**/api/upload/image**', (route) => { - uploadRequests.push(route.request()) - return route.fulfill( - jsonRoute({ - name: `agent-load3d-capture-${++capture}.png`, - subfolder: 'temp', - type: 'temp' - }) - ) - }) - await page.route('**/api/view?*filename=cube.obj*', (route) => - route.fulfill({ path: assetPath('cube.obj') }) - ) - await page.route('**/api/view?*filename=workflow.glb*', (route) => - route.fulfill({ path: assetPath('animated_triangle.glb') }) - ) - await page.route('**://t.comfy.org/**', (route) => - route.fulfill(jsonRoute({ status: 1 })) - ) - await page.route('**/api/agent/threads/*/messages', (route) => - route.fulfill( - jsonRoute({ - message_id: '3818ba00-d772-4a3f-98c1-9312725b577d', - thread_id: THREAD_ID, - workflow_id: WORKFLOW_ID - }) - ) - ) - await page.route(/\/api\/prompt$/, (route) => { - if (route.request().method() !== 'POST') return route.fallback() - return route.fulfill( - jsonRoute({ prompt_id: crypto.randomUUID(), node_errors: {} }) - ) - }) - await bootCloud(page) - await page.goto(new URL('/?agentCrdtFollower=1', APP_URL).toString()) - await waitForCloudApp(page) - await page.evaluate( - async (data) => { - await window.app!.loadGraphData(data, true, true) - }, - workflow as unknown as ComfyWorkflowJSON - ) - await expect(page.locator('[data-node-id="1"]')).toBeVisible() - return uploadRequests -} - -async function uploadedImageBytes(request: Request): Promise { - const body = request.postDataBuffer() - expect(body).not.toBeNull() - if (!body) throw new Error('upload request has no body') - const form = await new Response(new Uint8Array(body), { - headers: { 'content-type': request.headers()['content-type'] ?? '' } - }).formData() - const image = form.get('image') - expect(image).toBeInstanceOf(File) - if (!(image instanceof File)) throw new Error('upload has no image file') - return new Uint8Array(await image.arrayBuffer()) -} - -async function captureNextPrompt(page: Page): Promise { - const request = page.waitForRequest( - (value) => - value.method() === 'POST' && - new URL(value.url()).pathname === '/api/prompt' - ) - await page.getByTestId('queue-button').click() - return promptImage((await request).postDataJSON()) -} - -async function setRemoteModel(page: Page, value: string, opId: string) { - await page.evaluate( - async ({ nodeId, value, opId }) => { - const root = document.querySelector('#vue-app') as Element & { - __vue_app__?: { - _context: { provides: Record } - } - } - const pinia = Reflect.ownKeys(root.__vue_app__?._context.provides ?? {}) - .map( - (key) => - root.__vue_app__?._context.provides[key] as - | { _s?: Map } - | undefined - ) - .find((candidate) => candidate?._s instanceof Map) - const widgetStore = pinia?._s?.get('widgetValue') as - | { - setValue: ( - id: string, - nextValue: string, - context: Record - ) => boolean - } - | undefined - const modelWidget = window - .app!.graph.getNodeById(nodeId) - ?.widgets?.find((widget) => widget.name === 'model_file') - if (!widgetStore || !modelWidget?.widgetId) { - throw new Error('model_file widget store binding unavailable') - } - if ( - !widgetStore.setValue(modelWidget.widgetId, value, { - source: 'agent-remote', - actor: 'agent:e2e', - opId - }) - ) { - throw new Error('remote model_file update was not applied') - } - }, - { nodeId: toNodeId('1'), value, opId } - ) -} +// A queued capture may not carry a scene the viewer is still replacing; this +// is how long the held click must stay silent before the hold is trusted. +const HELD_QUEUE_WINDOW_MS = 1_500 test.describe('Load3D agent updates', { tag: '@cloud' }, () => { test('an agent model_file update refreshes the viewer and capture cache', async ({ - page + load3dAgent }) => { test.setTimeout(60_000) - const uploadRequests = await bootAgentLoad3d(page) - const load3d = new Load3DHelper(page.locator('[data-node-id="1"]')) - await expect(load3d.canvas).toBeVisible() - await setRemoteModel(page, 'cube.obj', 'load3d-model-a') - await load3d.waitForModelLoaded() - const beforeImage = await captureNextPrompt(page) - await setRemoteModel(page, 'workflow.glb', 'load3d-model-update') + const { viewer } = load3dAgent + + const before = + await test.step('capture the initial agent model', async () => { + load3dAgent.setModelFromAgent('cube.obj') + await load3dAgent.expectModel('cube.obj') + await viewer.waitForModelLoaded() + return load3dAgent.capture() + }) - await expect - .poll(() => - page.evaluate( - (nodeId) => - window - .app!.graph.getNodeById(nodeId) - ?.widgets?.find((widget) => widget.name === 'model_file')?.value, - toNodeId('1') - ) - ) - .toBe('workflow.glb') - await load3d.waitForModelLoaded() - const afterImage = await captureNextPrompt(page) - expect(afterImage).not.toBe(beforeImage) - expect(uploadRequests).toHaveLength(6) - const beforeImageBytes = await uploadedImageBytes(uploadRequests[0]) - const afterImageBytes = await uploadedImageBytes(uploadRequests[3]) - expect(beforeImageBytes.byteLength).toBeGreaterThan(0) - expect(afterImageBytes).not.toEqual(beforeImageBytes) - await expect(load3d.canvas).toBeVisible() - await test.info().attach('agent-updated-load3d.png', { - body: await load3d.node.screenshot(), - contentType: 'image/png' + const heldPrompt = + await test.step('hold the replacement model while the agent swaps it in', async () => { + const release = load3dAgent.holdModel('workflow.glb') + load3dAgent.setModelFromAgent('workflow.glb') + await load3dAgent.expectModel('workflow.glb') + await expect(load3dAgent.loadingOverlay).toBeVisible() + // The queued prompt must wait for the swap instead of shipping the cube. + await load3dAgent.expectQueueHeld(HELD_QUEUE_WINDOW_MS) + return load3dAgent.releaseAndAwaitPrompt(release) + }) + + await test.step('the held prompt carries the replacement model', async () => { + await viewer.waitForModelLoaded() + const after: Load3dCapture = await load3dAgent.captureFor(heldPrompt) + expect(after.promptImage).not.toBe(before.promptImage) + expect(after.imageBytes.byteLength).toBeGreaterThan(0) + expect(after.imageBytes).not.toEqual(before.imageBytes) + await expect(viewer.canvas).toBeVisible() + await test.info().attach('agent-updated-load3d.png', { + body: await viewer.node.screenshot(), + contentType: 'image/png' + }) }) }) }) From 8b1d4d7e6acdbd7201a35fb2d37c23508066acd8 Mon Sep 17 00:00:00 2001 From: Terry Jia Date: Thu, 17 Sep 2026 23:35:25 -0400 Subject: [PATCH 25/47] test(load3d): drop the timing window and generated oracle from the agent capture regression --- browser_tests/fixtures/load3dAgentFixture.ts | 62 ++++++------------- .../tests/load3d/load3dAgentUpdate.spec.ts | 13 ++-- .../core/load3d/Load3DConfiguration.test.ts | 39 ------------ .../core/load3d/Load3DConfiguration.ts | 4 +- 4 files changed, 25 insertions(+), 93 deletions(-) diff --git a/browser_tests/fixtures/load3dAgentFixture.ts b/browser_tests/fixtures/load3dAgentFixture.ts index 40eedcc44cb..b369e906c46 100644 --- a/browser_tests/fixtures/load3dAgentFixture.ts +++ b/browser_tests/fixtures/load3dAgentFixture.ts @@ -4,7 +4,11 @@ import { expect } from '@playwright/test' import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' import enMessages from '@/locales/en/main.json' with { type: 'json' } -import { zComfyWorkflow } from '@/platform/workflow/validation/schemas/workflowSchema' +import type { ComfyApiWorkflow } from '@/platform/workflow/validation/schemas/workflowSchema' +import { + zComfyApiWorkflow, + zComfyWorkflow +} from '@/platform/workflow/validation/schemas/workflowSchema' import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' @@ -64,14 +68,20 @@ function seedWorkflow(): WorkflowJSON { return { ...parsed, extra: extra ?? undefined, nodes, links: [] } } +function getQueuedPrompt(body: unknown): ComfyApiWorkflow { + if (typeof body !== 'object' || body === null || !('prompt' in body)) + throw new Error('the queued prompt body carries no prompt object') + return zComfyApiWorkflow.parse(body.prompt) +} + function promptImageRef(body: unknown): string { - const prompt = ( - body as { prompt?: Record }> } - ).prompt - const image = Object.values(prompt ?? {}).find( - (node) => node.inputs?.image !== undefined - )?.inputs?.image - const ref = (image as { image?: unknown } | undefined)?.image + const image: unknown = Object.values(getQueuedPrompt(body)).find( + (node) => node.inputs.image !== undefined + )?.inputs.image + const ref = + typeof image === 'object' && image !== null && 'image' in image + ? image.image + : undefined if (typeof ref !== 'string') throw new Error('the queued prompt carries no Load3D image reference') return ref @@ -243,16 +253,6 @@ class Load3dAgentHarness { return posted } - /** - * Lets a held model finish loading and returns the prompt the queue click - * made while it was held; that click is still waiting on the viewer. - */ - async releaseAndAwaitPrompt(release: () => void): Promise { - const posted = this.nextPrompt() - release() - return posted - } - private async nextPrompt(): Promise { const request = await this.page.waitForRequest( (candidate) => @@ -262,32 +262,6 @@ class Load3dAgentHarness { return request.postDataJSON() } - /** Asserts that no prompt reaches the server within `ms` of clicking Queue. */ - async expectQueueHeld(ms: number): Promise { - let posted = false - const onRequest = (request: Request) => { - if ( - request.method() === 'POST' && - new URL(request.url()).pathname === '/api/prompt' - ) - posted = true - } - this.page.on('request', onRequest) - try { - await this.page.getByTestId(TestIds.topbar.queueButton).click() - // Negative assertion: the fixed window is the check itself. There is no - // page condition to wait for because the expected outcome is that - // nothing happens while the model route is held. - await new Promise((resolve) => setTimeout(resolve, ms)) - expect( - posted, - 'a prompt was posted before the model finished loading' - ).toBe(false) - } finally { - this.page.off('request', onRequest) - } - } - /** Reads the capture the last queued prompt carried and its uploaded bytes. */ async captureFor(promptBody: unknown): Promise { const promptImage = promptImageRef(promptBody) diff --git a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts index 4ef1ddd7e77..5f027d2085d 100644 --- a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts +++ b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts @@ -3,15 +3,12 @@ import { expect } from '@playwright/test' import type { Load3dCapture } from '@e2e/fixtures/load3dAgentFixture' import { load3dAgentTest as test } from '@e2e/fixtures/load3dAgentFixture' -// A queued capture may not carry a scene the viewer is still replacing; this -// is how long the held click must stay silent before the hold is trusted. -const HELD_QUEUE_WINDOW_MS = 1_500 - test.describe('Load3D agent updates', { tag: '@cloud' }, () => { + test.describe.configure({ timeout: 60_000 }) + test('an agent model_file update refreshes the viewer and capture cache', async ({ load3dAgent }) => { - test.setTimeout(60_000) const { viewer } = load3dAgent const before = @@ -28,9 +25,9 @@ test.describe('Load3D agent updates', { tag: '@cloud' }, () => { load3dAgent.setModelFromAgent('workflow.glb') await load3dAgent.expectModel('workflow.glb') await expect(load3dAgent.loadingOverlay).toBeVisible() - // The queued prompt must wait for the swap instead of shipping the cube. - await load3dAgent.expectQueueHeld(HELD_QUEUE_WINDOW_MS) - return load3dAgent.releaseAndAwaitPrompt(release) + const heldPrompt = load3dAgent.queuePrompt() + release() + return heldPrompt }) await test.step('the held prompt carries the replacement model', async () => { diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 9c625c54cf7..bdb42082be6 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -1,5 +1,4 @@ import { fromAny, fromPartial } from '@total-typescript/shoehorn' -import fc from 'fast-check' import { beforeEach, describe, expect, it, vi } from 'vitest' import { nextTick, reactive } from 'vue' @@ -924,44 +923,6 @@ describe('Load3DConfiguration reactive widget contract', () => { expect(load3d.loadModel).toHaveBeenCalledTimes(1) }) - it('preserves exactly-once model effects across generated value sequences', async () => { - await fc.assert( - fc.asyncProperty( - fc.array(fc.constantFrom('', 'none', 'a.glb', 'b.obj'), { - minLength: 1, - maxLength: 20 - }), - async (values) => { - const load3d = makeLoad3dMock() - const modelWidget = reactiveWidget('none') - new Load3DConfiguration(load3d).configure({ - modelWidget, - loadFolder: 'input' - }) - vi.mocked(load3d.loadModel).mockClear() - vi.mocked(load3d.clearModel).mockClear() - - let previous = 'none' - let loads = 0 - let clears = 0 - for (const value of values) { - if (value !== previous) { - if (!value || value === 'none') clears++ - else loads++ - previous = value - } - modelWidget.value = value - } - await flush() - - expect(load3d.loadModel).toHaveBeenCalledTimes(loads) - expect(load3d.clearModel).toHaveBeenCalledTimes(clears) - } - ), - { numRuns: 100 } - ) - }) - it('does not register a second listener when configure runs again for the same widget', async () => { const load3d = makeLoad3dMock() const onSceneInvalidated = vi.fn() diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index c8c08a35137..9b9284ea751 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -26,8 +26,8 @@ type Load3DConfigurationSettings = { bgImagePath?: string silentOnNotFound?: boolean /** - * Called when a user-driven change to one of the wired widgets - * (model_file, width, height) makes the previously captured scene stale. + * Called when any change to one of the wired widgets (model_file, width, + * height), local or remote, makes the previously captured scene stale. * Backend caching covers these inputs by themselves; this hook lets the * caller invalidate any frontend-side capture cache so the next serialize * re-renders at the new state. From b2128eefb90ce240768f2c35c64a414af23502e2 Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 18 Sep 2026 04:12:20 -0700 Subject: [PATCH 26/47] fix(load3d): publish accepted load tail synchronously Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4043526795 --- src/extensions/core/load3d/Load3d.test.ts | 37 +++++++++++++++++++++++ src/extensions/core/load3d/Load3d.ts | 25 ++++++++------- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/extensions/core/load3d/Load3d.test.ts b/src/extensions/core/load3d/Load3d.test.ts index e5ac1ce0d95..637449f0df0 100644 --- a/src/extensions/core/load3d/Load3d.test.ts +++ b/src/extensions/core/load3d/Load3d.test.ts @@ -892,6 +892,43 @@ describe('Load3d', () => { await expect(idle).resolves.toBeUndefined() }) + + it('waits for a load accepted while the current load is still pending', async () => { + let resolveFirst!: () => void + let resolveSecond!: () => void + const first = new Promise((resolve) => { + resolveFirst = resolve + }) + const second = new Promise((resolve) => { + resolveSecond = resolve + }) + const internal = vi + .fn() + .mockImplementationOnce(() => first) + .mockImplementationOnce(() => second) + Object.assign(ctx.load3d, { + loadingPromise: null, + _loadModelInternal: internal + }) + + const loadA = ctx.load3d.loadModel('api/view?filename=a.glb') + const idle = ctx.load3d.whenLoadIdle() + const loadB = ctx.load3d.loadModel('api/view?filename=b.glb') + let settled = false + void idle.then(() => { + settled = true + }) + + resolveFirst() + await loadA + await Promise.resolve() + expect(internal).toHaveBeenCalledTimes(2) + expect(settled).toBe(false) + + resolveSecond() + await Promise.all([idle, loadB]) + expect(settled).toBe(true) + }) }) describe('currentLoadGeneration', () => { diff --git a/src/extensions/core/load3d/Load3d.ts b/src/extensions/core/load3d/Load3d.ts index e217ba28bdc..cf63d454801 100644 --- a/src/extensions/core/load3d/Load3d.ts +++ b/src/extensions/core/load3d/Load3d.ts @@ -336,20 +336,25 @@ class Load3d extends Viewport3d { ): Promise { this._loadGeneration += 1 - if (this.loadingPromise) { + const previousLoad = this.loadingPromise + const acceptedLoad = (async () => { try { - await this.loadingPromise + await previousLoad } catch { // Serialization only: the rejection already reached the loadModel caller. } - } - this.loadingPromise = this._loadModelInternal( - url, - originalFileName, - options - ) - return this.loadingPromise + await this._loadModelInternal(url, originalFileName, options) + })() + + // Publish the tail before waiting so every accepted load is visible to + // whenLoadIdle(), including loads queued behind the current one. + this.loadingPromise = acceptedLoad + try { + await acceptedLoad + } finally { + if (this.loadingPromise === acceptedLoad) this.loadingPromise = null + } } async whenLoadIdle(): Promise { @@ -403,8 +408,6 @@ class Load3d extends Viewport3d { } this.handleResize() - - this.loadingPromise = null } isSplatModel(): boolean { From 46b42185657f9a8d9160e969a17613a860128de8 Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 18 Sep 2026 04:14:56 -0700 Subject: [PATCH 27/47] test(agent): share follower host socket fixture Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4043526801 --- .../fixtures/agentConversationFixture.ts | 68 +++----------- .../fixtures/agentFollowerHostSocket.ts | 89 +++++++++++++++++++ browser_tests/fixtures/load3dAgentFixture.ts | 83 +++-------------- 3 files changed, 113 insertions(+), 127 deletions(-) create mode 100644 browser_tests/fixtures/agentFollowerHostSocket.ts diff --git a/browser_tests/fixtures/agentConversationFixture.ts b/browser_tests/fixtures/agentConversationFixture.ts index 1115d2accd8..b3a51e78e3c 100644 --- a/browser_tests/fixtures/agentConversationFixture.ts +++ b/browser_tests/fixtures/agentConversationFixture.ts @@ -1,4 +1,4 @@ -import type { Locator, Page, WebSocketRoute } from '@playwright/test' +import type { Locator, Page } from '@playwright/test' import { expect } from '@playwright/test' import { z } from 'zod' @@ -14,12 +14,12 @@ import type { AgentMessages, AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' -import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' import { agentTest, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers' import type { AgentConversation, @@ -38,7 +38,6 @@ const turnId = (turn: number): string => `0c5b1e77-2d4a-4f9e-8b63-1a2c3d4e5${turn.toString(16).padStart(3, '0')}` const SOCKET_SID = '7d1f2e3a-4b5c-4d6e-8f90-1a2b3c4d5e6f' const PANEL_MOUNT_TIMEOUT = 30_000 -const SUBSCRIBE_TIMEOUT = 15_000 const CANCEL_TIMEOUT = 10_000 const OPEN_AGENT_LABEL = enMessages.agent.askComfyAgent @@ -132,21 +131,17 @@ class AgentConversationHarness { readonly vueNodes: VueNodeHelpers private readonly host: HostDoc + private readonly hostSocket: AgentFollowerHostSocket private readonly streams: Locator private readonly summaries: Locator private readonly seedIds: Set // Every node id the host has held so far, seed included. private readonly seenIds: Set private readonly expectations: ExpectedTurn[] - private socket: WebSocketRoute | null = null private postedTurns = 0 private readonly displayNames = new Map() // Resolved when the panel cancels the turn the recording stopped. private readonly cancelWaiters = new Map void>() - private resolveSubscribed: (() => void) | null = null - private readonly subscribed = new Promise((resolve) => { - this.resolveSubscribed = resolve - }) constructor( private readonly page: Page, @@ -156,6 +151,12 @@ class AgentConversationHarness { ) { const { workflow } = conversation this.host = new HostDoc(workflow.id, workflow.seed, workflow.catalog) + this.hostSocket = new AgentFollowerHostSocket( + page, + workflow.id, + this.host, + SOCKET_SID + ) this.seedIds = new Set(workflow.seed.nodes.map((node) => String(node.id))) this.seenIds = new Set(this.seedIds) const expectations = RECORDED_EXPECTATIONS[caseId] @@ -173,20 +174,7 @@ class AgentConversationHarness { async boot(agentFlag: boolean): Promise { await this.mockAgentApi() - // The follower re-drives a pending subscribe only on a status frame, which every real connect sends. - await this.page.routeWebSocket(/\/ws/, (socket) => { - this.socket = socket - socket.onMessage((raw) => this.onClientFrame(raw)) - socket.send( - JSON.stringify({ - type: 'status', - data: { - status: { exec_info: { queue_remaining: 0 } }, - sid: SOCKET_SID - } - }) - ) - }) + await this.hostSocket.install() const objectInfo = this.page.waitForResponse((response) => new URL(response.url()).pathname.endsWith('/api/object_info') ) @@ -575,43 +563,11 @@ class AgentConversationHarness { } private send(frame: AgentWsEvent | HostFrame): void { - // Every host frame must satisfy production's own parser, so a host that - // stopped emitting a required field fails here, not silently on the client. - if ( - (frame.type.startsWith('doc_') || frame.type === 'awareness') && - parseServerDocFrame(frame) === null - ) - throw new Error(`host frame ${frame.type} is not a valid doc frame`) - if (!this.socket) throw new Error('the app has not opened /ws yet') - this.socket.send(JSON.stringify(frame)) - } - - private onClientFrame(raw: string | Buffer): void { - const frame: unknown = JSON.parse(raw.toString()) - if (typeof frame !== 'object' || frame === null) return - const { type, data } = frame as { type?: unknown; data?: unknown } - if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) - return - const { workflow_id, state_vector_b64 } = data as { - workflow_id?: unknown - state_vector_b64?: unknown - } - if ( - workflow_id !== this.conversation.workflow.id || - typeof state_vector_b64 !== 'string' - ) - return - this.send(this.host.subscribed()) - this.send(this.host.catchUp(state_vector_b64)) - this.resolveSubscribed?.() + this.hostSocket.send(frame) } private waitForSubscribe(): Promise { - return withTimeout( - this.subscribed, - SUBSCRIBE_TIMEOUT, - 'the follower never subscribed to the conversation workflow; graph_ops need an agent_active_tab (or a bound tab) first' - ) + return this.hostSocket.waitForSubscribe() } } diff --git a/browser_tests/fixtures/agentFollowerHostSocket.ts b/browser_tests/fixtures/agentFollowerHostSocket.ts new file mode 100644 index 00000000000..e39e6b83ee2 --- /dev/null +++ b/browser_tests/fixtures/agentFollowerHostSocket.ts @@ -0,0 +1,89 @@ +import type { Page, WebSocketRoute } from '@playwright/test' + +import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' +import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' +import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' + +import type { HostDoc, HostFrame } from '@e2e/fixtures/agentConversationHostDoc' + +const SUBSCRIBE_TIMEOUT = 15_000 + +/** Routed `/ws` host shared by black-box Agent follower fixtures. */ +export class AgentFollowerHostSocket { + private socket: WebSocketRoute | null = null + private resolveSubscribed: (() => void) | null = null + private readonly subscribed = new Promise((resolve) => { + this.resolveSubscribed = resolve + }) + + constructor( + private readonly page: Page, + private readonly workflowId: string, + private readonly host: HostDoc, + private readonly socketSid: string + ) {} + + async install(): Promise { + await this.page.routeWebSocket(/\/ws/, (socket) => { + this.socket = socket + socket.onMessage((raw) => this.onClientFrame(raw)) + socket.send( + JSON.stringify({ + type: 'status', + data: { + status: { exec_info: { queue_remaining: 0 } }, + sid: this.socketSid + } + }) + ) + }) + } + + send(frame: AgentWsEvent | HostFrame): void { + if (frame.type.startsWith('doc_') || frame.type === 'awareness') { + if (parseServerDocFrame(frame) === null) + throw new Error(`host frame ${frame.type} is not a valid doc frame`) + } else if (!parseAgentWsEvent(frame).success) { + throw new Error(`agent event ${frame.type} is not a valid agent event`) + } + if (!this.socket) throw new Error('the app has not opened /ws yet') + this.socket.send(JSON.stringify(frame)) + } + + async waitForSubscribe(): Promise { + let timer: ReturnType | undefined + const timeout = new Promise((_, reject) => { + timer = setTimeout( + () => + reject( + new Error( + `the follower never subscribed to workflow ${this.workflowId}` + ) + ), + SUBSCRIBE_TIMEOUT + ) + }) + try { + await Promise.race([this.subscribed, timeout]) + } finally { + clearTimeout(timer) + } + } + + private onClientFrame(raw: string | Buffer): void { + const frame: unknown = JSON.parse(raw.toString()) + if (typeof frame !== 'object' || frame === null) return + const { type, data } = frame as { type?: unknown; data?: unknown } + if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) + return + const { workflow_id, state_vector_b64 } = data as { + workflow_id?: unknown + state_vector_b64?: unknown + } + if (workflow_id !== this.workflowId || typeof state_vector_b64 !== 'string') + return + this.send(this.host.subscribed()) + this.send(this.host.catchUp(state_vector_b64)) + this.resolveSubscribed?.() + } +} diff --git a/browser_tests/fixtures/load3dAgentFixture.ts b/browser_tests/fixtures/load3dAgentFixture.ts index b369e906c46..6aa8e75b3f1 100644 --- a/browser_tests/fixtures/load3dAgentFixture.ts +++ b/browser_tests/fixtures/load3dAgentFixture.ts @@ -1,4 +1,4 @@ -import type { Page, Request, WebSocketRoute } from '@playwright/test' +import type { Page, Request } from '@playwright/test' import { expect } from '@playwright/test' import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' @@ -9,14 +9,13 @@ import { zComfyApiWorkflow, zComfyWorkflow } from '@/platform/workflow/validation/schemas/workflowSchema' -import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' -import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' import { toNodeId } from '@/types/nodeId' import load3dWorkflow from '@e2e/assets/3d/load3d_node.json' with { type: 'json' } import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { agentTest, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' import type { RecordedGraphOperation } from '@e2e/fixtures/data/agent/agentConversation' import { TestIds } from '@e2e/fixtures/selectors' @@ -28,7 +27,6 @@ const WORKFLOW_ID = 'a81718a4-02ae-41e6-ae85-c33b7bb880f6' const SOCKET_SID = '5b0e2c9a-6f1d-4a83-9c27-3e4f5a6b7c8d' const NODE_ID = '1' const PANEL_MOUNT_TIMEOUT = 30_000 -const SUBSCRIBE_TIMEOUT = 15_000 // The backend's Load3D definition, in `widgets_values` order; the host applier // rejects a `set_widget` naming anything outside this list. @@ -98,22 +96,6 @@ async function uploadedImageBytes(request: Request): Promise { return new Uint8Array(await image.arrayBuffer()) } -async function withTimeout( - promise: Promise, - ms: number, - message: string -): Promise { - let timer: ReturnType | undefined - const timeout = new Promise((_, reject) => { - timer = setTimeout(() => reject(new Error(message)), ms) - }) - try { - return await Promise.race([promise, timeout]) - } finally { - clearTimeout(timer) - } -} - /** * One Load3D node bound to an agent workflow, driven the way production is: * the panel opens the agent's tab, the follower subscribes over `/ws`, and the @@ -128,36 +110,25 @@ class Load3dAgentHarness { seedWorkflow(), LOAD3D_CATALOG ) - private socket: WebSocketRoute | null = null - private resolveSubscribed: (() => void) | null = null - private readonly subscribed = new Promise((resolve) => { - this.resolveSubscribed = resolve - }) + private readonly hostSocket: AgentFollowerHostSocket private uploads = 0 private readonly uploadsByName = new Map() private readonly heldModels = new Map>() constructor(private readonly page: Page) { + this.hostSocket = new AgentFollowerHostSocket( + page, + WORKFLOW_ID, + this.host, + SOCKET_SID + ) this.viewer = new Load3DHelper(page.locator(`[data-node-id="${NODE_ID}"]`)) } async boot(agentFlag: boolean): Promise { await this.mockAgentApi() await this.mockLoad3dIo() - // The follower re-drives a pending subscribe only on a status frame, which every real connect sends. - await this.page.routeWebSocket(/\/ws/, (socket) => { - this.socket = socket - socket.onMessage((raw) => this.onClientFrame(raw)) - socket.send( - JSON.stringify({ - type: 'status', - data: { - status: { exec_info: { queue_remaining: 0 } }, - sid: SOCKET_SID - } - }) - ) - }) + await this.hostSocket.install() await bootAgentApp(this.page, agentFlag, { // Only the Vue node renderer projects follower edits onto the canvas. settings: { @@ -178,11 +149,7 @@ class Load3dAgentHarness { type: 'agent_active_tab', data: { workflow_id: WORKFLOW_ID, name: 'Load3D preview' } }) - await withTimeout( - this.subscribed, - SUBSCRIBE_TIMEOUT, - 'the follower never subscribed to the agent workflow after agent_active_tab' - ) + await this.hostSocket.waitForSubscribe() await expect(this.viewer.node).toBeVisible() await expect(this.viewer.canvas).toBeVisible() } @@ -320,33 +287,7 @@ class Load3dAgentHarness { } private send(frame: AgentWsEvent | HostFrame): void { - // Every frame must satisfy production's own parser, so a host that stopped - // emitting a required field fails here, not silently on the client. - if (frame.type.startsWith('doc_') || frame.type === 'awareness') { - if (parseServerDocFrame(frame) === null) - throw new Error(`host frame ${frame.type} is not a valid doc frame`) - } else if (!parseAgentWsEvent(frame).success) { - throw new Error(`agent event ${frame.type} is not a valid agent event`) - } - if (!this.socket) throw new Error('the app has not opened /ws yet') - this.socket.send(JSON.stringify(frame)) - } - - private onClientFrame(raw: string | Buffer): void { - const frame: unknown = JSON.parse(raw.toString()) - if (typeof frame !== 'object' || frame === null) return - const { type, data } = frame as { type?: unknown; data?: unknown } - if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) - return - const { workflow_id, state_vector_b64 } = data as { - workflow_id?: unknown - state_vector_b64?: unknown - } - if (workflow_id !== WORKFLOW_ID || typeof state_vector_b64 !== 'string') - return - this.send(this.host.subscribed()) - this.send(this.host.catchUp(state_vector_b64)) - this.resolveSubscribed?.() + this.hostSocket.send(frame) } } From 8030003d43571016dbdc0c5a08e8e266f82059b0 Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 18 Sep 2026 04:16:01 -0700 Subject: [PATCH 28/47] test(load3d): keep browser oracle deterministic Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4043933981 --- browser_tests/fixtures/load3dAgentFixture.ts | 32 ++----------------- .../tests/load3d/load3dAgentUpdate.spec.ts | 16 ++++------ 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/browser_tests/fixtures/load3dAgentFixture.ts b/browser_tests/fixtures/load3dAgentFixture.ts index 6aa8e75b3f1..4e2786ce7bb 100644 --- a/browser_tests/fixtures/load3dAgentFixture.ts +++ b/browser_tests/fixtures/load3dAgentFixture.ts @@ -113,7 +113,6 @@ class Load3dAgentHarness { private readonly hostSocket: AgentFollowerHostSocket private uploads = 0 private readonly uploadsByName = new Map() - private readonly heldModels = new Map>() constructor(private readonly page: Page) { this.hostSocket = new AgentFollowerHostSocket( @@ -189,30 +188,6 @@ class Load3dAgentHarness { .toBe(model) } - /** The viewer's own loading overlay, shown while a model file is in flight. */ - get loadingOverlay() { - return this.viewer.node.getByTestId(TestIds.loading.overlay) - } - - /** - * Stops serving `model` until the returned release runs. A capture queued - * while the file is held must wait: the prompt is not allowed to carry the - * scene the swap is replacing. - */ - holdModel(model: AgentModelFile): () => void { - let release: () => void = () => {} - this.heldModels.set( - model, - new Promise((resolve) => { - release = resolve - }) - ) - return () => { - this.heldModels.delete(model) - release() - } - } - /** Clicks Queue and resolves once the prompt for it has been posted. */ async queuePrompt(): Promise { const posted = this.nextPrompt() @@ -273,10 +248,9 @@ class Load3dAgentHarness { return route.fulfill(jsonRoute({ name, subfolder: 'temp', type: 'temp' })) }) for (const model of Object.keys(MODEL_ASSETS) as AgentModelFile[]) { - await page.route(`**/api/view?*filename=${model}*`, async (route) => { - await this.heldModels.get(model) - return route.fulfill({ path: assetPath(MODEL_ASSETS[model]) }) - }) + await page.route(`**/api/view?*filename=${model}*`, (route) => + route.fulfill({ path: assetPath(MODEL_ASSETS[model]) }) + ) } await page.route(/\/api\/prompt$/, (route) => { if (route.request().method() !== 'POST') return route.fallback() diff --git a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts index 5f027d2085d..9e57f670941 100644 --- a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts +++ b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts @@ -19,20 +19,16 @@ test.describe('Load3D agent updates', { tag: '@cloud' }, () => { return load3dAgent.capture() }) - const heldPrompt = - await test.step('hold the replacement model while the agent swaps it in', async () => { - const release = load3dAgent.holdModel('workflow.glb') + const updatedPrompt = + await test.step('queue after the agent replacement finishes loading', async () => { load3dAgent.setModelFromAgent('workflow.glb') await load3dAgent.expectModel('workflow.glb') - await expect(load3dAgent.loadingOverlay).toBeVisible() - const heldPrompt = load3dAgent.queuePrompt() - release() - return heldPrompt + await load3dAgent.viewer.waitForModelLoaded() + return load3dAgent.queuePrompt() }) - await test.step('the held prompt carries the replacement model', async () => { - await viewer.waitForModelLoaded() - const after: Load3dCapture = await load3dAgent.captureFor(heldPrompt) + await test.step('the prompt carries the replacement model', async () => { + const after: Load3dCapture = await load3dAgent.captureFor(updatedPrompt) expect(after.promptImage).not.toBe(before.promptImage) expect(after.imageBytes.byteLength).toBeGreaterThan(0) expect(after.imageBytes).not.toEqual(before.imageBytes) From 3ed056b6d7ea2fbed8ec464b01bfd769e3ea857a Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 05:13:43 -0700 Subject: [PATCH 29/47] fix: keep clear ordered with pending Load3D loads Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4046571944 --- src/extensions/core/load3d/Load3d.test.ts | 50 +++++++++++++++++++++++ src/extensions/core/load3d/Load3d.ts | 7 ++++ 2 files changed, 57 insertions(+) diff --git a/src/extensions/core/load3d/Load3d.test.ts b/src/extensions/core/load3d/Load3d.test.ts index 637449f0df0..5d7aaf1689d 100644 --- a/src/extensions/core/load3d/Load3d.test.ts +++ b/src/extensions/core/load3d/Load3d.test.ts @@ -136,6 +136,8 @@ function makeInstance() { animationManager, eventManager, adapterRef: { current: null }, + _loadGeneration: 0, + loadingPromise: null, forceRender: vi.fn(), handleResize: vi.fn(), preRenderCallbacks: [], @@ -929,6 +931,54 @@ describe('Load3d', () => { await Promise.all([idle, loadB]) expect(settled).toBe(true) }) + + it('keeps the viewer empty when clear is accepted during a pending load', async () => { + let resolveLoad!: () => void + const pendingLoad = new Promise((resolve) => { + resolveLoad = resolve + }) + const loadedModel = new THREE.Group() + const modelManager = { + ...ctx.modelManager, + currentModel: null as THREE.Object3D | null, + originalModel: null, + clearModel: vi.fn(() => { + modelManager.currentModel = null + }) + } + Object.assign(ctx.load3d, { + _loadGeneration: 0, + loadingPromise: null, + cameraManager: { + ...ctx.cameraManager, + getCameraState: vi.fn(), + getCurrentCameraType: vi.fn(() => 'perspective'), + setCameraState: vi.fn() + }, + controlsManager: { ...ctx.controlsManager, reset: vi.fn() }, + loaderManager: { + loadModel: vi.fn(async () => { + await pendingLoad + modelManager.currentModel = loadedModel + }) + }, + modelManager, + animationManager: { + ...ctx.animationManager, + setupModelAnimations: vi.fn() + }, + hasLoadedModel: false + }) + + const load = ctx.load3d.loadModel('api/view?filename=a.glb') + ctx.load3d.clearModel() + const idle = ctx.load3d.whenLoadIdle() + resolveLoad() + await Promise.all([load, idle]) + + expect(ctx.load3d.getCurrentModel()).toBeNull() + expect(modelManager.clearModel).toHaveBeenCalledTimes(3) + }) }) describe('currentLoadGeneration', () => { diff --git a/src/extensions/core/load3d/Load3d.ts b/src/extensions/core/load3d/Load3d.ts index cf63d454801..b10ca4eb100 100644 --- a/src/extensions/core/load3d/Load3d.ts +++ b/src/extensions/core/load3d/Load3d.ts @@ -335,6 +335,7 @@ class Load3d extends Viewport3d { options?: LoadModelOptions ): Promise { this._loadGeneration += 1 + const loadGeneration = this._loadGeneration const previousLoad = this.loadingPromise const acceptedLoad = (async () => { @@ -345,6 +346,7 @@ class Load3d extends Viewport3d { } await this._loadModelInternal(url, originalFileName, options) + if (loadGeneration !== this._loadGeneration) this.clearModelState() })() // Publish the tail before waiting so every accepted load is visible to @@ -423,6 +425,11 @@ class Load3d extends Viewport3d { } clearModel(): void { + this._loadGeneration += 1 + this.clearModelState() + } + + private clearModelState(): void { this.animationManager.dispose() this.gizmoManager.detach() this.modelManager.clearModel() From bf07ea93d32a050dffab2fd6c501a0e98129a6ec Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 05:15:27 -0700 Subject: [PATCH 30/47] test: consolidate reactive Load3D configuration cases Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4046571954 --- .../core/load3d/Load3DConfiguration.test.ts | 334 +++++------------- 1 file changed, 93 insertions(+), 241 deletions(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index bdb42082be6..7613ed16bf7 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -574,9 +574,10 @@ describe('Load3DConfiguration "none" model handling', () => { let clearModelSpy: ReturnType function makeLoad3dMock(): Load3d { + let cleanup: (() => void) | undefined loadModelSpy = vi.fn().mockResolvedValue(undefined) clearModelSpy = vi.fn() - return { + return fromPartial({ loadModel: loadModelSpy, clearModel: clearModelSpy, setUpDirection: vi.fn(), @@ -594,8 +595,11 @@ describe('Load3DConfiguration "none" model handling', () => { setHDRIAsBackground: vi.fn(), setHDRIEnabled: vi.fn(), emitModelReady: vi.fn(), - setConfigurationCleanup: vi.fn() - } as unknown as Load3d + setConfigurationCleanup: vi.fn((nextCleanup: () => void) => { + cleanup?.() + cleanup = nextCleanup + }) + }) } async function flush() { @@ -652,6 +656,92 @@ describe('Load3DConfiguration "none" model handling', () => { silentOnNotFound: false }) }) + + it('stops reacting as soon as the viewer lifecycle is removed', async () => { + const graph = new LGraph() + const node = new LGraphNode('Load3D') + graph.add(node) + const modelWidget = node.addWidget( + 'string', + 'model_file', + 'none', + () => undefined + ) + const config = new Load3DConfiguration(load3d) + config.configure({ modelWidget, loadFolder: 'input' }) + await flush() + const cleanup = vi + .mocked(load3d.setConfigurationCleanup) + .mock.calls.at(-1)?.[0] + if (!cleanup) throw new Error('Expected reactive configuration cleanup') + graph.remove(node) + expect(modelWidget.widgetId).toBeUndefined() + cleanup() + loadModelSpy.mockClear() + + modelWidget.value = 'ignored.glb' + await flush() + + expect(loadModelSpy).not.toHaveBeenCalled() + }) + + it('reloads the model when the CRDT follower writes model_file through widgetValueStore', async () => { + const onSceneInvalidated = vi.fn() + const graph = new LGraph() + const node = new LGraphNode('Load3D') + graph.add(node) + const modelWidget = node.addWidget( + 'string', + 'model_file', + 'none', + () => undefined + ) + const widgetId = modelWidget.widgetId + if (!widgetId) throw new Error('Expected a store-backed widget id') + + new Load3DConfiguration(load3d).configure({ + modelWidget, + loadFolder: 'input', + onSceneInvalidated + }) + await flush() + loadModelSpy.mockClear() + onSceneInvalidated.mockClear() + + const applied = useWidgetValueStore().setValue(widgetId, 'agent.glb', { + source: 'agent-remote', + actor: 'agent:e2e', + opId: 'op-1' + }) + + expect(applied).toBe(true) + expect(onSceneInvalidated).toHaveBeenCalledTimes(1) + expect(loadModelSpy).toHaveBeenCalledWith(expect.any(String), 'agent.glb', { + silentOnNotFound: false + }) + expect(modelWidget.value).toBe('agent.glb') + }) + + it('stops reacting to the previous widget once configure runs with a replacement', async () => { + const oldWidget = reactiveWidget('none') + const replacementWidget = reactiveWidget('none') + const config = new Load3DConfiguration(load3d) + config.configure({ modelWidget: oldWidget, loadFolder: 'input' }) + config.configure({ modelWidget: replacementWidget, loadFolder: 'input' }) + await flush() + loadModelSpy.mockClear() + + oldWidget.value = 'stale.glb' + replacementWidget.value = 'restored.glb' + await flush() + + expect(loadModelSpy).toHaveBeenCalledTimes(1) + expect(loadModelSpy).toHaveBeenCalledWith( + expect.any(String), + 'restored.glb', + { silentOnNotFound: false } + ) + }) }) describe('Load3DConfiguration.onSceneInvalidated', () => { @@ -796,241 +886,3 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }).not.toThrow() }) }) - -// Load3DConfiguration owns no widget listeners of its own. It observes -// `modelWidget.value`, `width.value`, and `height.value` reactively, so any -// writer that goes through widget state (user edit, widgetValueStore.setValue -// from the CRDT follower, undo) reloads the scene and invalidates the capture -// cache. These tests pin that reactive contract. -describe('Load3DConfiguration reactive widget contract', () => { - function makeLoad3dMock(): Load3d { - let cleanup: (() => void) | undefined - return fromPartial({ - loadModel: vi.fn().mockResolvedValue(undefined), - clearModel: vi.fn(), - setUpDirection: vi.fn(), - setMaterialMode: vi.fn(), - setTargetSize: vi.fn(), - setCameraState: vi.fn(), - toggleGrid: vi.fn(), - setBackgroundColor: vi.fn(), - setBackgroundImage: vi.fn().mockResolvedValue(undefined), - setBackgroundRenderMode: vi.fn(), - toggleCamera: vi.fn(), - setFOV: vi.fn(), - setLightIntensity: vi.fn(), - setHDRIIntensity: vi.fn(), - setHDRIAsBackground: vi.fn(), - setHDRIEnabled: vi.fn(), - emitModelReady: vi.fn(), - setConfigurationCleanup: vi.fn((nextCleanup: () => void) => { - cleanup?.() - cleanup = nextCleanup - }) - }) - } - - async function flush() { - await nextTick() - await Promise.resolve() - } - - beforeEach(() => { - vi.mocked(Load3dUtils.splitFilePath).mockReturnValue(['', 'model.glb']) - vi.mocked(Load3dUtils.getResourceURL).mockReturnValue('/view') - }) - - it('reloads the model when model_file changes', async () => { - const load3d = makeLoad3dMock() - const onSceneInvalidated = vi.fn() - const modelWidget = reactiveWidget('none') - - const config = new Load3DConfiguration(load3d) - config.configure({ - modelWidget, - loadFolder: 'input', - onSceneInvalidated - }) - await flush() - vi.mocked(load3d.loadModel).mockClear() - onSceneInvalidated.mockClear() - - modelWidget.value = 'agent-model.glb' - await flush() - - expect(load3d.loadModel).toHaveBeenCalledWith( - expect.any(String), - 'agent-model.glb', - { silentOnNotFound: false } - ) - expect(onSceneInvalidated).toHaveBeenCalled() - expect(modelWidget.value).toBe('agent-model.glb') - }) - - it('clears the model when model_file is emptied', async () => { - const load3d = makeLoad3dMock() - const onSceneInvalidated = vi.fn() - const modelWidget = reactiveWidget('model.glb') - - const config = new Load3DConfiguration(load3d) - config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) - await flush() - vi.mocked(load3d.clearModel).mockClear() - onSceneInvalidated.mockClear() - - modelWidget.value = '' - await flush() - - expect(load3d.clearModel).toHaveBeenCalledTimes(1) - expect(onSceneInvalidated).toHaveBeenCalledTimes(1) - expect(modelWidget.value).toBe('') - }) - - it('reacts exactly once per model_file change', async () => { - const load3d = makeLoad3dMock() - const onSceneInvalidated = vi.fn() - const modelWidget = reactiveWidget('none') - - const config = new Load3DConfiguration(load3d) - config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) - await flush() - vi.mocked(load3d.loadModel).mockClear() - vi.mocked(load3d.clearModel).mockClear() - onSceneInvalidated.mockClear() - - modelWidget.value = 'local-change.glb' - await flush() - - expect(load3d.loadModel).toHaveBeenCalledTimes(1) - expect(load3d.clearModel).not.toHaveBeenCalled() - expect(onSceneInvalidated).toHaveBeenCalledTimes(1) - expect(modelWidget.value).toBe('local-change.glb') - }) - - it('invalidates synchronously before queueing can observe stale capture state', () => { - const load3d = makeLoad3dMock() - const onSceneInvalidated = vi.fn() - const modelWidget = reactiveWidget('none') - new Load3DConfiguration(load3d).configure({ - modelWidget, - loadFolder: 'input', - onSceneInvalidated - }) - - modelWidget.value = 'replacement.glb' - - expect(onSceneInvalidated).toHaveBeenCalledTimes(1) - expect(load3d.loadModel).toHaveBeenCalledTimes(1) - }) - - it('does not register a second listener when configure runs again for the same widget', async () => { - const load3d = makeLoad3dMock() - const onSceneInvalidated = vi.fn() - const modelWidget = reactiveWidget('none') - - const config = new Load3DConfiguration(load3d) - config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) - config.configure({ modelWidget, loadFolder: 'input', onSceneInvalidated }) - await flush() - - expect(load3d.setConfigurationCleanup).toHaveBeenCalledTimes(2) - - vi.mocked(load3d.loadModel).mockClear() - onSceneInvalidated.mockClear() - modelWidget.value = 'agent-model.glb' - await flush() - - expect(load3d.loadModel).toHaveBeenCalledTimes(1) - expect(onSceneInvalidated).toHaveBeenCalledTimes(1) - }) - - it('stops reacting as soon as the viewer lifecycle is removed', async () => { - const load3d = makeLoad3dMock() - const graph = new LGraph() - const node = new LGraphNode('Load3D') - graph.add(node) - const modelWidget = node.addWidget( - 'string', - 'model_file', - 'none', - () => undefined - ) - const config = new Load3DConfiguration(load3d) - config.configure({ modelWidget, loadFolder: 'input' }) - await flush() - const cleanup = vi - .mocked(load3d.setConfigurationCleanup) - .mock.calls.at(-1)?.[0] - if (!cleanup) throw new Error('Expected reactive configuration cleanup') - graph.remove(node) - expect(modelWidget.widgetId).toBeUndefined() - cleanup() - vi.mocked(load3d.loadModel).mockClear() - modelWidget.value = 'ignored.glb' - await flush() - expect(load3d.loadModel).not.toHaveBeenCalled() - }) - - it('reloads the model when the CRDT follower writes model_file through widgetValueStore', async () => { - const load3d = makeLoad3dMock() - const onSceneInvalidated = vi.fn() - const graph = new LGraph() - const node = new LGraphNode('Load3D') - graph.add(node) - const modelWidget = node.addWidget( - 'string', - 'model_file', - 'none', - () => undefined - ) - const widgetId = modelWidget.widgetId - if (!widgetId) throw new Error('Expected a store-backed widget id') - - new Load3DConfiguration(load3d).configure({ - modelWidget, - loadFolder: 'input', - onSceneInvalidated - }) - await flush() - vi.mocked(load3d.loadModel).mockClear() - onSceneInvalidated.mockClear() - - const applied = useWidgetValueStore().setValue(widgetId, 'agent.glb', { - source: 'agent-remote', - actor: 'agent:e2e', - opId: 'op-1' - }) - - expect(applied).toBe(true) - expect(onSceneInvalidated).toHaveBeenCalledTimes(1) - expect(load3d.loadModel).toHaveBeenCalledWith( - expect.any(String), - 'agent.glb', - { silentOnNotFound: false } - ) - expect(modelWidget.value).toBe('agent.glb') - }) - - it('stops reacting to the previous widget once configure runs with a replacement', async () => { - const load3d = makeLoad3dMock() - const oldWidget = reactiveWidget('none') - const replacementWidget = reactiveWidget('none') - - const config = new Load3DConfiguration(load3d) - config.configure({ modelWidget: oldWidget, loadFolder: 'input' }) - config.configure({ modelWidget: replacementWidget, loadFolder: 'input' }) - await flush() - vi.mocked(load3d.loadModel).mockClear() - - oldWidget.value = 'stale.glb' - replacementWidget.value = 'restored.glb' - await flush() - - expect(load3d.loadModel).toHaveBeenCalledTimes(1) - expect(load3d.loadModel).toHaveBeenCalledWith( - expect.any(String), - 'restored.glb', - { silentOnNotFound: false } - ) - }) -}) From 7bbccea2233143e4915080c0f8ad618e407d9e51 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 05:16:15 -0700 Subject: [PATCH 31/47] test: call follower host socket directly Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4046571962 --- .../fixtures/agentConversationFixture.ts | 16 ++++------------ browser_tests/fixtures/load3dAgentFixture.ts | 10 ++-------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/browser_tests/fixtures/agentConversationFixture.ts b/browser_tests/fixtures/agentConversationFixture.ts index b3a51e78e3c..0bdeae44191 100644 --- a/browser_tests/fixtures/agentConversationFixture.ts +++ b/browser_tests/fixtures/agentConversationFixture.ts @@ -18,7 +18,6 @@ import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApi import { agentTest, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' -import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers' import type { @@ -271,10 +270,11 @@ class AgentConversationHarness { await new Promise((resolve) => setTimeout(resolve, entry.at_ms! - (Date.now() - startedAt)) ) - if (entry.kind === 'event') this.send(this.stampTurn(entry.event, turn)) + if (entry.kind === 'event') + this.hostSocket.send(this.stampTurn(entry.event, turn)) else { - await this.waitForSubscribe() - this.send(this.host.apply(entry.ops)) + await this.hostSocket.waitForSubscribe() + this.hostSocket.send(this.host.apply(entry.ops)) for (const id of Object.keys(this.host.graph().nodes)) this.seenIds.add(id) } @@ -561,14 +561,6 @@ class AgentConversationHarness { ) return parsed.data } - - private send(frame: AgentWsEvent | HostFrame): void { - this.hostSocket.send(frame) - } - - private waitForSubscribe(): Promise { - return this.hostSocket.waitForSubscribe() - } } export type ReplayTiming = 'immediate' | 'recorded' diff --git a/browser_tests/fixtures/load3dAgentFixture.ts b/browser_tests/fixtures/load3dAgentFixture.ts index 4e2786ce7bb..a6533474332 100644 --- a/browser_tests/fixtures/load3dAgentFixture.ts +++ b/browser_tests/fixtures/load3dAgentFixture.ts @@ -9,12 +9,10 @@ import { zComfyApiWorkflow, zComfyWorkflow } from '@/platform/workflow/validation/schemas/workflowSchema' -import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' import { toNodeId } from '@/types/nodeId' import load3dWorkflow from '@e2e/assets/3d/load3d_node.json' with { type: 'json' } import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' -import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { agentTest, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' import type { RecordedGraphOperation } from '@e2e/fixtures/data/agent/agentConversation' @@ -144,7 +142,7 @@ class Load3dAgentHarness { timeout: PANEL_MOUNT_TIMEOUT }) // The agent announces its tab; the panel opens it and binds the follower. - this.send({ + this.hostSocket.send({ type: 'agent_active_tab', data: { workflow_id: WORKFLOW_ID, name: 'Load3D preview' } }) @@ -170,7 +168,7 @@ class Load3dAgentHarness { old, value: model } - this.send(this.host.apply([op])) + this.hostSocket.send(this.host.apply([op])) } /** Waits until the node's `model_file` widget shows `model`. */ @@ -259,10 +257,6 @@ class Load3dAgentHarness { ) }) } - - private send(frame: AgentWsEvent | HostFrame): void { - this.hostSocket.send(frame) - } } interface Load3dAgentFixtures { From b6d37d7b3c3b29a3be6e467ff0ed10f4a919277c Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 05:17:04 -0700 Subject: [PATCH 32/47] test: collapse Load3D capture fixture operation Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4046571973 --- browser_tests/fixtures/load3dAgentFixture.ts | 6 +++--- browser_tests/tests/load3d/load3dAgentUpdate.spec.ts | 6 ++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/browser_tests/fixtures/load3dAgentFixture.ts b/browser_tests/fixtures/load3dAgentFixture.ts index a6533474332..df0dbb66717 100644 --- a/browser_tests/fixtures/load3dAgentFixture.ts +++ b/browser_tests/fixtures/load3dAgentFixture.ts @@ -45,7 +45,7 @@ const MODEL_ASSETS: Record = { 'workflow.glb': 'animated_triangle.glb' } -export interface Load3dCapture { +interface Load3dCapture { /** The `image` reference the queued prompt carried for the Load3D node. */ promptImage: string /** The PNG the viewer uploaded under that reference. */ @@ -187,7 +187,7 @@ class Load3dAgentHarness { } /** Clicks Queue and resolves once the prompt for it has been posted. */ - async queuePrompt(): Promise { + private async queuePrompt(): Promise { const posted = this.nextPrompt() await this.page.getByTestId(TestIds.topbar.queueButton).click() return posted @@ -203,7 +203,7 @@ class Load3dAgentHarness { } /** Reads the capture the last queued prompt carried and its uploaded bytes. */ - async captureFor(promptBody: unknown): Promise { + private async captureFor(promptBody: unknown): Promise { const promptImage = promptImageRef(promptBody) const name = PROMPT_IMAGE_REF.exec(promptImage)?.[1] const upload = name === undefined ? undefined : this.uploadsByName.get(name) diff --git a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts index 9e57f670941..263f67a57e3 100644 --- a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts +++ b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts @@ -1,6 +1,5 @@ import { expect } from '@playwright/test' -import type { Load3dCapture } from '@e2e/fixtures/load3dAgentFixture' import { load3dAgentTest as test } from '@e2e/fixtures/load3dAgentFixture' test.describe('Load3D agent updates', { tag: '@cloud' }, () => { @@ -19,16 +18,15 @@ test.describe('Load3D agent updates', { tag: '@cloud' }, () => { return load3dAgent.capture() }) - const updatedPrompt = + const after = await test.step('queue after the agent replacement finishes loading', async () => { load3dAgent.setModelFromAgent('workflow.glb') await load3dAgent.expectModel('workflow.glb') await load3dAgent.viewer.waitForModelLoaded() - return load3dAgent.queuePrompt() + return load3dAgent.capture() }) await test.step('the prompt carries the replacement model', async () => { - const after: Load3dCapture = await load3dAgent.captureFor(updatedPrompt) expect(after.promptImage).not.toBe(before.promptImage) expect(after.imageBytes.byteLength).toBeGreaterThan(0) expect(after.imageBytes).not.toEqual(before.imageBytes) From 7742ba8934b2c0ad5dd7b35522477063e8db7bf9 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 05:17:39 -0700 Subject: [PATCH 33/47] test: iterate Load3D model assets without assertion Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4046571978 --- browser_tests/fixtures/load3dAgentFixture.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/browser_tests/fixtures/load3dAgentFixture.ts b/browser_tests/fixtures/load3dAgentFixture.ts index df0dbb66717..63072962197 100644 --- a/browser_tests/fixtures/load3dAgentFixture.ts +++ b/browser_tests/fixtures/load3dAgentFixture.ts @@ -245,9 +245,9 @@ class Load3dAgentHarness { this.uploadsByName.set(name, route.request()) return route.fulfill(jsonRoute({ name, subfolder: 'temp', type: 'temp' })) }) - for (const model of Object.keys(MODEL_ASSETS) as AgentModelFile[]) { + for (const [model, asset] of Object.entries(MODEL_ASSETS)) { await page.route(`**/api/view?*filename=${model}*`, (route) => - route.fulfill({ path: assetPath(MODEL_ASSETS[model]) }) + route.fulfill({ path: assetPath(asset) }) ) } await page.route(/\/api\/prompt$/, (route) => { From 8cb6ef2e6fbfae61e801224279a8ed5789551222 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 07:12:00 -0700 Subject: [PATCH 34/47] test: type the mutable Load3D model fixture Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4046962259 --- src/extensions/core/load3d/Load3d.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/extensions/core/load3d/Load3d.test.ts b/src/extensions/core/load3d/Load3d.test.ts index 5d7aaf1689d..1d1e0aec326 100644 --- a/src/extensions/core/load3d/Load3d.test.ts +++ b/src/extensions/core/load3d/Load3d.test.ts @@ -938,9 +938,11 @@ describe('Load3d', () => { resolveLoad = resolve }) const loadedModel = new THREE.Group() - const modelManager = { + const modelManager: typeof ctx.modelManager & { + currentModel: THREE.Object3D | null + } = { ...ctx.modelManager, - currentModel: null as THREE.Object3D | null, + currentModel: null, originalModel: null, clearModel: vi.fn(() => { modelManager.currentModel = null From 84e98ef7261725988b95d335c5c0ba4f209560c6 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 07:12:10 -0700 Subject: [PATCH 35/47] test: keep stale Load3D clear assertion behavioral Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4046962253 --- src/extensions/core/load3d/Load3d.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/extensions/core/load3d/Load3d.test.ts b/src/extensions/core/load3d/Load3d.test.ts index 1d1e0aec326..eeeb8f55591 100644 --- a/src/extensions/core/load3d/Load3d.test.ts +++ b/src/extensions/core/load3d/Load3d.test.ts @@ -979,7 +979,6 @@ describe('Load3d', () => { await Promise.all([load, idle]) expect(ctx.load3d.getCurrentModel()).toBeNull() - expect(modelManager.clearModel).toHaveBeenCalledTimes(3) }) }) From 73a068fb961b2ceba8ebde5675c798ecd322c2d2 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 07:14:08 -0700 Subject: [PATCH 36/47] fix: retry stale Load3D scene captures Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4046962245 --- src/extensions/core/load3d.test.ts | 25 +++++++ src/extensions/core/load3d.ts | 107 ++++++++++++++++------------- 2 files changed, 86 insertions(+), 46 deletions(-) diff --git a/src/extensions/core/load3d.test.ts b/src/extensions/core/load3d.test.ts index 41e6a6dd8c8..076ab5bbb27 100644 --- a/src/extensions/core/load3d.test.ts +++ b/src/extensions/core/load3d.test.ts @@ -1205,6 +1205,31 @@ describe('Comfy.Load3D scene widget serializeValue caching', () => { expect(refreshed?.image).toBe('threed/scene-4.png [temp]') }) + it('re-captures when the scene changes during an upload', async () => { + const { load3d, serialize, uploadTempImage } = await setup() + let releaseUploads!: () => void + const uploadsBlocked = new Promise((resolve) => { + releaseUploads = resolve + }) + uploadTempImage.mockImplementationOnce(async () => { + await uploadsBlocked + return { name: 'stale-scene.png' } + }) + + const pending = serialize() + await flush() + + const configuration = configureMock.mock.calls.at(-1)?.[0] + configuration.onSceneInvalidated() + releaseUploads() + + const refreshed = await pending + + expect(load3d.captureScene).toHaveBeenCalledTimes(2) + expect(uploadTempImage).toHaveBeenCalledTimes(6) + expect(refreshed?.image).toBe('threed/scene-3.png [temp]') + }) + it('returns null when no load3d instance is registered for the node', async () => { const widgets: FakeWidget[] = [ { name: 'model_file', value: 'm.glb' }, diff --git a/src/extensions/core/load3d.ts b/src/extensions/core/load3d.ts index 6d92b2894eb..896b6684ef5 100644 --- a/src/extensions/core/load3d.ts +++ b/src/extensions/core/load3d.ts @@ -68,6 +68,13 @@ const inputSpecPreview3D: CustomInputSpec = { isPreview: true } +const load3dSceneRevisions = new WeakMap() + +function invalidateLoad3dScene(node: LGraphNode): void { + load3dSceneRevisions.set(node, (load3dSceneRevisions.get(node) ?? 0) + 1) + markLoad3dSceneDirty(node) +} + async function handleModelUpload(files: FileList, node: LGraphNode) { if (!files?.length) return @@ -112,7 +119,7 @@ async function handleModelUpload(files: FileList, node: LGraphNode) { modelWidget.value = uploadPath } - markLoad3dSceneDirty(node) + invalidateLoad3dScene(node) } catch (error) { console.error('Model upload failed:', error) useToastStore().addAlert(t('toastMessages.fileUploadFailed')) @@ -130,7 +137,7 @@ async function handleResourcesUpload(files: FileList, node: LGraphNode) { : '3d' await Load3dUtils.uploadMultipleFiles(files, subfolder) - markLoad3dSceneDirty(node) + invalidateLoad3dScene(node) } catch (error) { console.error('Extra resources upload failed:', error) useToastStore().addAlert(t('toastMessages.extraResourcesUploadFailed')) @@ -342,7 +349,7 @@ useExtensionService().registerExtension({ if (modelWidget) { modelWidget.value = LOAD3D_NONE_MODEL } - markLoad3dSceneDirty(node) + invalidateLoad3dScene(node) }) } @@ -402,7 +409,7 @@ useExtensionService().registerExtension({ cameraState, width, height, - onSceneInvalidated: () => markLoad3dSceneDirty(node) + onSceneInvalidated: () => invalidateLoad3dScene(node) }) }) @@ -420,58 +427,66 @@ useExtensionService().registerExtension({ return null } - if (!isLoad3dSceneDirty(node)) { - const cached = getLoad3dOutputCache(node) - if (cached) return cached - } + while (true) { + if (!isLoad3dSceneDirty(node)) { + const cached = getLoad3dOutputCache(node) + if (cached) return cached + } - // A model swap (user or agent) may still be loading. Capture the - // scene the queue will actually run, not the one being replaced. - await currentLoad3d.whenLoadIdle() + const sceneRevision = load3dSceneRevisions.get(node) ?? 0 - const { camera_info, model_3d_info } = snapshotLoad3dState( - node, - currentLoad3d - ) + // A model swap (user or agent) may still be loading. Capture the + // scene the queue will actually run, not the one being replaced. + await currentLoad3d.whenLoadIdle() - const { - scene: imageData, - mask: maskData, - normal: normalData - } = await currentLoad3d.captureScene( - width.value as number, - height.value as number - ) + const { camera_info, model_3d_info } = snapshotLoad3dState( + node, + currentLoad3d + ) + + const { + scene: imageData, + mask: maskData, + normal: normalData + } = await currentLoad3d.captureScene( + width.value as number, + height.value as number + ) - const [data, dataMask, dataNormal] = await Promise.all([ - Load3dUtils.uploadTempImage(imageData, 'scene'), - Load3dUtils.uploadTempImage(maskData, 'scene_mask'), - Load3dUtils.uploadTempImage(normalData, 'scene_normal') - ]) + const [data, dataMask, dataNormal] = await Promise.all([ + Load3dUtils.uploadTempImage(imageData, 'scene'), + Load3dUtils.uploadTempImage(maskData, 'scene_mask'), + Load3dUtils.uploadTempImage(normalData, 'scene_normal') + ]) - currentLoad3d.handleResize() + currentLoad3d.handleResize() - const returnVal: Load3dCachedOutput = { - image: `threed/${data.name} [temp]`, - mask: `threed/${dataMask.name} [temp]`, - normal: `threed/${dataNormal.name} [temp]`, - camera_info, - recording: '', - model_3d_info - } + const returnVal: Load3dCachedOutput = { + image: `threed/${data.name} [temp]`, + mask: `threed/${dataMask.name} [temp]`, + normal: `threed/${dataNormal.name} [temp]`, + camera_info, + recording: '', + model_3d_info + } - const recordingData = currentLoad3d.getRecordingData() + const recordingData = currentLoad3d.getRecordingData() - if (recordingData) { - const [recording] = await Promise.all([ - Load3dUtils.uploadTempImage(recordingData, 'recording', 'mp4') - ]) - returnVal.recording = `threed/${recording.name} [temp]` - } + if (recordingData) { + const [recording] = await Promise.all([ + Load3dUtils.uploadTempImage(recordingData, 'recording', 'mp4') + ]) + returnVal.recording = `threed/${recording.name} [temp]` + } - setLoad3dOutputCache(node, returnVal) + if ((load3dSceneRevisions.get(node) ?? 0) !== sceneRevision) { + continue + } - return returnVal + setLoad3dOutputCache(node, returnVal) + + return returnVal + } } } }) From 6083383ed24ef2bf8ade980978d3cc115632695d Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 07:15:01 -0700 Subject: [PATCH 37/47] fix: suppress effects from stale Load3D loads Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4046962235 --- .../core/load3d/Load3DConfiguration.test.ts | 57 ++++++++++++++++++- .../core/load3d/Load3DConfiguration.ts | 5 +- src/extensions/core/load3d/Load3d.test.ts | 4 +- src/extensions/core/load3d/Load3d.ts | 17 ++++-- 4 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 7613ed16bf7..336b44937c6 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -194,9 +194,10 @@ describe('Load3DConfiguration.silentOnNotFound propagation', () => { let loadModelSpy: ReturnType function makeLoad3dMock(): Load3d { - loadModelSpy = vi.fn().mockResolvedValue(undefined) + loadModelSpy = vi.fn().mockResolvedValue(true) return { loadModel: loadModelSpy, + clearModel: vi.fn(), setUpDirection: vi.fn(), setMaterialMode: vi.fn(), setTargetSize: vi.fn(), @@ -318,6 +319,60 @@ describe('Load3DConfiguration.silentOnNotFound propagation', () => { await flush() expect(vi.mocked(load3d.emitModelReady)).toHaveBeenCalledTimes(1) }) + + it('does not publish effects for a load superseded by clear', async () => { + let resolveLoad!: (accepted: boolean) => void + const load3d = makeLoad3dMock() + vi.mocked(load3d.loadModel).mockImplementation( + () => + new Promise((resolve) => { + resolveLoad = resolve + }) + ) + const modelWidget = reactiveWidget('a.glb') + const config = new Load3DConfiguration(load3d) + + config.configure({ modelWidget, loadFolder: 'output' }) + modelWidget.value = '' + resolveLoad(false) + await flush() + + expect(load3d.setUpDirection).not.toHaveBeenCalled() + expect(load3d.setMaterialMode).not.toHaveBeenCalled() + expect(load3d.emitModelReady).not.toHaveBeenCalled() + }) + + it('publishes effects only for the replacement after clear', async () => { + let resolveFirst!: (accepted: boolean) => void + let resolveSecond!: (accepted: boolean) => void + const load3d = makeLoad3dMock() + vi.mocked(load3d.loadModel) + .mockImplementationOnce( + () => + new Promise((resolve) => { + resolveFirst = resolve + }) + ) + .mockImplementationOnce( + () => + new Promise((resolve) => { + resolveSecond = resolve + }) + ) + const modelWidget = reactiveWidget('a.glb') + const config = new Load3DConfiguration(load3d) + + config.configure({ modelWidget, loadFolder: 'output' }) + modelWidget.value = '' + modelWidget.value = 'b.glb' + resolveFirst(false) + resolveSecond(true) + await flush() + + expect(load3d.setUpDirection).toHaveBeenCalledTimes(1) + expect(load3d.setMaterialMode).toHaveBeenCalledTimes(1) + expect(load3d.emitModelReady).toHaveBeenCalledTimes(1) + }) }) describe('parseAnnotatedFilename', () => { diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index 9b9284ea751..e39ead4756b 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -310,7 +310,10 @@ class Load3DConfiguration { ) ) - await this.load3d.loadModel(modelUrl, filename, { silentOnNotFound }) + const accepted = await this.load3d.loadModel(modelUrl, filename, { + silentOnNotFound + }) + if (!accepted) return const modelConfig = this.loadModelConfig() this.applyModelConfig(modelConfig) diff --git a/src/extensions/core/load3d/Load3d.test.ts b/src/extensions/core/load3d/Load3d.test.ts index eeeb8f55591..8ab92c3cf8d 100644 --- a/src/extensions/core/load3d/Load3d.test.ts +++ b/src/extensions/core/load3d/Load3d.test.ts @@ -940,6 +940,7 @@ describe('Load3d', () => { const loadedModel = new THREE.Group() const modelManager: typeof ctx.modelManager & { currentModel: THREE.Object3D | null + originalModel: THREE.Object3D | null } = { ...ctx.modelManager, currentModel: null, @@ -976,8 +977,9 @@ describe('Load3d', () => { ctx.load3d.clearModel() const idle = ctx.load3d.whenLoadIdle() resolveLoad() - await Promise.all([load, idle]) + const [accepted] = await Promise.all([load, idle]) + expect(accepted).toBe(false) expect(ctx.load3d.getCurrentModel()).toBeNull() }) }) diff --git a/src/extensions/core/load3d/Load3d.ts b/src/extensions/core/load3d/Load3d.ts index b10ca4eb100..cb248c5ece1 100644 --- a/src/extensions/core/load3d/Load3d.ts +++ b/src/extensions/core/load3d/Load3d.ts @@ -64,7 +64,7 @@ class Load3d extends Viewport3d { adapterRef: AdapterRef private configurationCleanup?: () => void - private loadingPromise: Promise | null = null + private loadingPromise: Promise | null = null private _loadGeneration: number = 0 private hasLoadedModel: boolean = false @@ -333,7 +333,7 @@ class Load3d extends Viewport3d { url: string, originalFileName?: string, options?: LoadModelOptions - ): Promise { + ): Promise { this._loadGeneration += 1 const loadGeneration = this._loadGeneration @@ -345,22 +345,27 @@ class Load3d extends Viewport3d { // Serialization only: the rejection already reached the loadModel caller. } - await this._loadModelInternal(url, originalFileName, options) - if (loadGeneration !== this._loadGeneration) this.clearModelState() + try { + await this._loadModelInternal(url, originalFileName, options) + } finally { + if (loadGeneration !== this._loadGeneration) this.clearModelState() + } + + return loadGeneration === this._loadGeneration })() // Publish the tail before waiting so every accepted load is visible to // whenLoadIdle(), including loads queued behind the current one. this.loadingPromise = acceptedLoad try { - await acceptedLoad + return await acceptedLoad } finally { if (this.loadingPromise === acceptedLoad) this.loadingPromise = null } } async whenLoadIdle(): Promise { - let last: Promise | null = null + let last: Promise | null = null while (this.loadingPromise && this.loadingPromise !== last) { last = this.loadingPromise try { From 7224a8bfe775b9e8b141c472acaf80f95ec450db Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 09:40:47 -0700 Subject: [PATCH 38/47] fix(load3d): centralize scene revision invalidation Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4047869924 --- src/composables/useLoad3d.ts | 12 ++++++++++-- src/extensions/core/load3d.test.ts | 17 +++++++++++++---- src/extensions/core/load3d.ts | 12 +++--------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/composables/useLoad3d.ts b/src/composables/useLoad3d.ts index 605e8baf2e5..49af82eca8f 100644 --- a/src/composables/useLoad3d.ts +++ b/src/composables/useLoad3d.ts @@ -51,13 +51,18 @@ export type Load3dCachedOutput = { } const load3dSceneDirty = new WeakMap() +const load3dSceneRevisions = new WeakMap() const load3dOutputCache = new WeakMap() export const markLoad3dSceneDirty = (node: LGraphNode | null): void => { if (!node) return + load3dSceneRevisions.set(node, (load3dSceneRevisions.get(node) ?? 0) + 1) load3dSceneDirty.set(node, true) } +export const getLoad3dSceneRevision = (node: LGraphNode): number => + load3dSceneRevisions.get(node) ?? 0 + export const isLoad3dSceneDirty = (node: LGraphNode): boolean => load3dSceneDirty.get(node) !== false @@ -67,10 +72,13 @@ export const getLoad3dOutputCache = ( export const setLoad3dOutputCache = ( node: LGraphNode, - output: Load3dCachedOutput -): void => { + output: Load3dCachedOutput, + sceneRevision: number = getLoad3dSceneRevision(node) +): boolean => { + if (getLoad3dSceneRevision(node) !== sceneRevision) return false load3dOutputCache.set(node, output) load3dSceneDirty.set(node, false) + return true } const pendingCallbacks = new Map() const persistentReadyCallbacks = new Map() diff --git a/src/extensions/core/load3d.test.ts b/src/extensions/core/load3d.test.ts index 076ab5bbb27..f01834858a0 100644 --- a/src/extensions/core/load3d.test.ts +++ b/src/extensions/core/load3d.test.ts @@ -57,6 +57,7 @@ vi.mock(import('@/services/load3dService'), () => ({ vi.mock(import('@/composables/useLoad3d'), () => { const sceneDirty = new WeakMap() + const sceneRevisions = new WeakMap() const outputCache = new WeakMap() return { useLoad3d: () => @@ -67,13 +68,21 @@ vi.mock(import('@/composables/useLoad3d'), () => { nodeToLoad3dMap: fromAny(nodeToLoad3dMap), markLoad3dSceneDirty: (node: LGraphNode | null) => { if (!node) return + sceneRevisions.set(node, (sceneRevisions.get(node) ?? 0) + 1) sceneDirty.set(node, true) }, + getLoad3dSceneRevision: (node: LGraphNode) => sceneRevisions.get(node) ?? 0, isLoad3dSceneDirty: (node: LGraphNode) => sceneDirty.get(node) !== false, getLoad3dOutputCache: fromAny((node: LGraphNode) => outputCache.get(node)), - setLoad3dOutputCache: (node: LGraphNode, value: unknown) => { + setLoad3dOutputCache: ( + node: LGraphNode, + value: unknown, + revision: number = sceneRevisions.get(node) ?? 0 + ) => { + if ((sceneRevisions.get(node) ?? 0) !== revision) return false outputCache.set(node, value) sceneDirty.set(node, false) + return true } } }) @@ -1206,7 +1215,8 @@ describe('Comfy.Load3D scene widget serializeValue caching', () => { }) it('re-captures when the scene changes during an upload', async () => { - const { load3d, serialize, uploadTempImage } = await setup() + const { node, load3d, serialize, uploadTempImage, useLoad3dModule } = + await setup() let releaseUploads!: () => void const uploadsBlocked = new Promise((resolve) => { releaseUploads = resolve @@ -1219,8 +1229,7 @@ describe('Comfy.Load3D scene widget serializeValue caching', () => { const pending = serialize() await flush() - const configuration = configureMock.mock.calls.at(-1)?.[0] - configuration.onSceneInvalidated() + useLoad3dModule.markLoad3dSceneDirty(node) releaseUploads() const refreshed = await pending diff --git a/src/extensions/core/load3d.ts b/src/extensions/core/load3d.ts index 896b6684ef5..dcf3830cf41 100644 --- a/src/extensions/core/load3d.ts +++ b/src/extensions/core/load3d.ts @@ -4,6 +4,7 @@ import Load3D from '@/components/load3d/Load3D.vue' import Load3DViewerContent from '@/components/load3d/Load3dViewerContent.vue' import { getLoad3dOutputCache, + getLoad3dSceneRevision, isLoad3dSceneDirty, markLoad3dSceneDirty, nodeToLoad3dMap, @@ -68,10 +69,7 @@ const inputSpecPreview3D: CustomInputSpec = { isPreview: true } -const load3dSceneRevisions = new WeakMap() - function invalidateLoad3dScene(node: LGraphNode): void { - load3dSceneRevisions.set(node, (load3dSceneRevisions.get(node) ?? 0) + 1) markLoad3dSceneDirty(node) } @@ -433,7 +431,7 @@ useExtensionService().registerExtension({ if (cached) return cached } - const sceneRevision = load3dSceneRevisions.get(node) ?? 0 + const sceneRevision = getLoad3dSceneRevision(node) // A model swap (user or agent) may still be loading. Capture the // scene the queue will actually run, not the one being replaced. @@ -479,11 +477,7 @@ useExtensionService().registerExtension({ returnVal.recording = `threed/${recording.name} [temp]` } - if ((load3dSceneRevisions.get(node) ?? 0) !== sceneRevision) { - continue - } - - setLoad3dOutputCache(node, returnVal) + if (!setLoad3dOutputCache(node, returnVal, sceneRevision)) continue return returnVal } From deee56d272f59cde4eebd56cb14cf9256eb5b28a Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 09:42:17 -0700 Subject: [PATCH 39/47] fix(load3d): gate stale load side effects Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4047869936 --- src/composables/useLoad3d.ts | 3 ++- src/composables/useLoad3dViewer.test.ts | 18 +++++++++++++++++- src/composables/useLoad3dViewer.ts | 9 ++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/composables/useLoad3d.ts b/src/composables/useLoad3d.ts index 49af82eca8f..6e007c4a691 100644 --- a/src/composables/useLoad3d.ts +++ b/src/composables/useLoad3d.ts @@ -852,7 +852,8 @@ export const useLoad3d = (nodeOrRef: MaybeRef) => { ) loadingMessage.value = t('load3d.loadingModel') - await load3d.loadModel(modelUrl) + const accepted = await load3d.loadModel(modelUrl) + if (!accepted) 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 83c025c5b31..abff7e84ae5 100644 --- a/src/composables/useLoad3dViewer.test.ts +++ b/src/composables/useLoad3dViewer.test.ts @@ -125,7 +125,7 @@ describe('useLoad3dViewer', () => { forceRender: vi.fn(), remove: vi.fn(), setTargetSize: vi.fn(), - loadModel: vi.fn().mockResolvedValue(undefined), + loadModel: vi.fn().mockResolvedValue(true), captureThumbnail: vi.fn().mockResolvedValue('data:image/png;base64,x'), setCameraState: vi.fn(), addEventListener: vi.fn(), @@ -626,6 +626,22 @@ describe('useLoad3dViewer', () => { }) describe('handleModelDrop', () => { + it('does not publish dropped-model state when the load was superseded', async () => { + vi.mocked(Load3dUtils.uploadFile).mockResolvedValueOnce( + '3d/superseded.glb' + ) + vi.mocked(mockLoad3d.loadModel!).mockResolvedValueOnce(false) + const viewer = useLoad3dViewer(mockNode) + const containerRef = document.createElement('div') + await viewer.initializeViewer(containerRef, mockSourceLoad3d as Load3d) + vi.mocked(mockLoad3d.getCurrentModelCapabilities!).mockClear() + + await viewer.handleModelDrop(new File([''], 'superseded.glb')) + + expect(mockNode.widgets).toEqual([]) + expect(mockLoad3d.getCurrentModelCapabilities).not.toHaveBeenCalled() + }) + it('refreshes the capability refs after the dropped model loads, so the sidebar reflects the new model', async () => { vi.mocked(Load3dUtils.uploadFile).mockResolvedValueOnce( '3d/dropped.splat' diff --git a/src/composables/useLoad3dViewer.ts b/src/composables/useLoad3dViewer.ts index 73bb2000b66..39c26b855ce 100644 --- a/src/composables/useLoad3dViewer.ts +++ b/src/composables/useLoad3dViewer.ts @@ -495,7 +495,8 @@ export const useLoad3dViewer = (node?: LGraphNode) => { load3d.updateStatusMouseOnViewer(true) } - await load3d.loadModel(modelUrl) + const accepted = await load3d.loadModel(modelUrl) + if (!accepted) return currentModelUrl = modelUrl restoreStandaloneConfig(modelUrl) captureAdapterFlags(load3d) @@ -531,7 +532,8 @@ export const useLoad3dViewer = (node?: LGraphNode) => { try { saveStandaloneConfig() - await load3d.loadModel(modelUrl) + const accepted = await load3d.loadModel(modelUrl) + if (!accepted) return currentModelUrl = modelUrl restoreStandaloneConfig(modelUrl) captureAdapterFlags(load3d) @@ -821,7 +823,8 @@ export const useLoad3dViewer = (node?: LGraphNode) => { ) ) - await load3d.loadModel(modelUrl) + const accepted = await load3d.loadModel(modelUrl) + if (!accepted) return captureAdapterFlags(load3d) From e0eb66dbdbe0676a415d28f657494282a6727fd0 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 09:43:35 -0700 Subject: [PATCH 40/47] refactor(load3d): narrow pending load type Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/17537#discussion_r4047869939 --- src/extensions/core/load3d/Load3d.test.ts | 18 +++++++++--------- src/extensions/core/load3d/Load3d.ts | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/extensions/core/load3d/Load3d.test.ts b/src/extensions/core/load3d/Load3d.test.ts index 8ab92c3cf8d..1f6a279bf57 100644 --- a/src/extensions/core/load3d/Load3d.test.ts +++ b/src/extensions/core/load3d/Load3d.test.ts @@ -831,8 +831,8 @@ describe('Load3d', () => { }) it('waits for the current loadingPromise to settle', async () => { - let resolveLoad!: () => void - const p = new Promise((resolve) => { + let resolveLoad!: (accepted: boolean) => void + const p = new Promise((resolve) => { resolveLoad = resolve }) Object.assign(ctx.load3d, { loadingPromise: p }) @@ -846,7 +846,7 @@ describe('Load3d', () => { await Promise.resolve() expect(settled).toBe(false) - resolveLoad() + resolveLoad(true) Object.assign(ctx.load3d, { loadingPromise: null }) await idle @@ -854,12 +854,12 @@ describe('Load3d', () => { }) it('drains a chained sequence of loads before resolving', async () => { - let resolveFirst!: () => void - const first = new Promise((resolve) => { + let resolveFirst!: (accepted: boolean) => void + const first = new Promise((resolve) => { resolveFirst = resolve }) - let resolveSecond!: () => void - const second = new Promise((resolve) => { + let resolveSecond!: (accepted: boolean) => void + const second = new Promise((resolve) => { resolveSecond = resolve }) @@ -874,11 +874,11 @@ describe('Load3d', () => { settled = true }) - resolveFirst() + resolveFirst(true) await new Promise((r) => setTimeout(r, 0)) expect(settled).toBe(false) - resolveSecond() + resolveSecond(true) Object.assign(ctx.load3d, { loadingPromise: null }) await idle expect(settled).toBe(true) diff --git a/src/extensions/core/load3d/Load3d.ts b/src/extensions/core/load3d/Load3d.ts index cb248c5ece1..dbddfd4076f 100644 --- a/src/extensions/core/load3d/Load3d.ts +++ b/src/extensions/core/load3d/Load3d.ts @@ -64,7 +64,7 @@ class Load3d extends Viewport3d { adapterRef: AdapterRef private configurationCleanup?: () => void - private loadingPromise: Promise | null = null + private loadingPromise: Promise | null = null private _loadGeneration: number = 0 private hasLoadedModel: boolean = false @@ -365,7 +365,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 { From 9d0f15dcbf8a7434f40067f9ebff2c9dbfc6e79a Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 13:21:23 -0700 Subject: [PATCH 41/47] fix(load3d): complete viewer setup before the first standalone load initializeStandaloneViewer() set isPreview and registered animation events only after awaiting loadModel(). When a second call superseded the first, the first returned early and the viewer was left without preview mode or animation listeners; loadStandaloneModel() never sets them either. Move both steps to right after createLoad3d() so they run exactly once per viewer regardless of load supersession. Also address review nits: loadModel doubles now resolve true for nominal success and are typed from Load3d['loadModel'], and add a narrow test that exercises the real useLoad3d revision gate (setLoad3dOutputCache rejects a stale revision and keeps dirty). --- src/composables/useLoad3d.test.ts | 23 ++++++++++++- src/composables/useLoad3dViewer.test.ts | 33 +++++++++++++++++++ src/composables/useLoad3dViewer.ts | 10 +++--- .../core/load3d/Load3DConfiguration.test.ts | 12 +++---- 4 files changed, 67 insertions(+), 11 deletions(-) diff --git a/src/composables/useLoad3d.test.ts b/src/composables/useLoad3d.test.ts index 26be0aa54bd..566e03fd7db 100644 --- a/src/composables/useLoad3d.test.ts +++ b/src/composables/useLoad3d.test.ts @@ -5,6 +5,7 @@ import { useSettingStore } from '@/platform/settings/settingStore' import { getLoad3dOutputCache, + getLoad3dSceneRevision, isLoad3dSceneDirty, markLoad3dSceneDirty, nodeToLoad3dMap, @@ -142,7 +143,7 @@ describe('useLoad3d', () => { setFOV: vi.fn(), setLightIntensity: vi.fn(), setCameraState: vi.fn(), - loadModel: vi.fn().mockResolvedValue(undefined), + loadModel: vi.fn().mockResolvedValue(true), refreshViewport: vi.fn(), updateStatusMouseOnNode: vi.fn(), updateStatusMouseOnScene: vi.fn(), @@ -1879,6 +1880,26 @@ describe('useLoad3d', () => { expect(isLoad3dSceneDirty(b)).toBe(true) }) + it('setLoad3dOutputCache rejects a stale scene revision and keeps the node dirty', () => { + const fresh = createMockLGraphNode({ properties: {} }) + const capturedRevision = getLoad3dSceneRevision(fresh) + + markLoad3dSceneDirty(fresh) + expect(getLoad3dSceneRevision(fresh)).toBe(capturedRevision + 1) + + expect(setLoad3dOutputCache(fresh, fakeCache, capturedRevision)).toBe( + false + ) + expect(getLoad3dOutputCache(fresh)).toBeUndefined() + expect(isLoad3dSceneDirty(fresh)).toBe(true) + + expect( + setLoad3dOutputCache(fresh, fakeCache, getLoad3dSceneRevision(fresh)) + ).toBe(true) + expect(getLoad3dOutputCache(fresh)).toBe(fakeCache) + expect(isLoad3dSceneDirty(fresh)).toBe(false) + }) + it('markLoad3dSceneDirty on null is a no-op', () => { expect(() => markLoad3dSceneDirty(null)).not.toThrow() }) diff --git a/src/composables/useLoad3dViewer.test.ts b/src/composables/useLoad3dViewer.test.ts index abff7e84ae5..e0fcf80b953 100644 --- a/src/composables/useLoad3dViewer.test.ts +++ b/src/composables/useLoad3dViewer.test.ts @@ -868,6 +868,39 @@ describe('useLoad3dViewer', () => { await newViewer.initializeStandaloneViewer(containerRef, modelUrl) expect(newViewer.backgroundColor.value).toBe('#0000ff') }) + + it('completes viewer setup when a concurrent call supersedes the first load', async () => { + let settleFirstLoad!: (accepted: boolean) => void + vi.mocked(mockLoad3d.loadModel!) + .mockImplementationOnce( + () => + new Promise((resolve) => { + settleFirstLoad = resolve + }) + ) + .mockResolvedValueOnce(true) + const viewer = useLoad3dViewer() + const containerRef = document.createElement('div') + + const first = viewer.initializeStandaloneViewer(containerRef, 'a.glb') + const replacement = viewer.initializeStandaloneViewer( + containerRef, + 'b.glb' + ) + settleFirstLoad(false) + await Promise.all([first, replacement]) + + expect(createLoad3d).toHaveBeenCalledTimes(1) + expect(viewer.isPreview.value).toBe(true) + expect(mockLoad3d.addEventListener).toHaveBeenCalledWith( + 'animationListChange', + expect.any(Function) + ) + expect(mockLoad3d.addEventListener).toHaveBeenCalledWith( + 'animationProgressChange', + expect.any(Function) + ) + }) }) describe('gizmo controls', () => { diff --git a/src/composables/useLoad3dViewer.ts b/src/composables/useLoad3dViewer.ts index 39c26b855ce..335d722c774 100644 --- a/src/composables/useLoad3dViewer.ts +++ b/src/composables/useLoad3dViewer.ts @@ -495,15 +495,17 @@ export const useLoad3dViewer = (node?: LGraphNode) => { load3d.updateStatusMouseOnViewer(true) } + // Viewer-lifetime setup belongs to instance creation, not to this + // request: a concurrent initializeStandaloneViewer() call reuses the + // instance through loadStandaloneModel() and can supersede this load. + isPreview.value = true + setupAnimationEvents() + const accepted = await load3d.loadModel(modelUrl) if (!accepted) return currentModelUrl = modelUrl restoreStandaloneConfig(modelUrl) captureAdapterFlags(load3d) - - isPreview.value = true - - setupAnimationEvents() persistStandaloneThumbnail(modelUrl) } catch (error) { console.error('Error initializing standalone 3D viewer:', error) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 336b44937c6..5cb0cb6f2bd 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -191,10 +191,10 @@ describe('Load3DConfiguration.loadModelConfig', () => { }) describe('Load3DConfiguration.silentOnNotFound propagation', () => { - let loadModelSpy: ReturnType + let loadModelSpy: ReturnType> function makeLoad3dMock(): Load3d { - loadModelSpy = vi.fn().mockResolvedValue(true) + loadModelSpy = vi.fn().mockResolvedValue(true) return { loadModel: loadModelSpy, clearModel: vi.fn(), @@ -538,7 +538,7 @@ describe('Load3DConfiguration.configure forwards persisted + settings to load3d' function makeLoad3dMock(): Load3d { return fromPartial({ - loadModel: vi.fn().mockResolvedValue(undefined), + loadModel: vi.fn().mockResolvedValue(true), setUpDirection: vi.fn(), setMaterialMode: vi.fn(), setTargetSize: vi.fn(), @@ -625,12 +625,12 @@ describe('Load3DConfiguration.configure forwards persisted + settings to load3d' describe('Load3DConfiguration "none" model handling', () => { let load3d: Load3d - let loadModelSpy: ReturnType + let loadModelSpy: ReturnType> let clearModelSpy: ReturnType function makeLoad3dMock(): Load3d { let cleanup: (() => void) | undefined - loadModelSpy = vi.fn().mockResolvedValue(undefined) + loadModelSpy = vi.fn().mockResolvedValue(true) clearModelSpy = vi.fn() return fromPartial({ loadModel: loadModelSpy, @@ -802,7 +802,7 @@ describe('Load3DConfiguration "none" model handling', () => { describe('Load3DConfiguration.onSceneInvalidated', () => { function makeLoad3dMock(): Load3d { return { - loadModel: vi.fn().mockResolvedValue(undefined), + loadModel: vi.fn().mockResolvedValue(true), clearModel: vi.fn(), setUpDirection: vi.fn(), setMaterialMode: vi.fn(), From 480c2d698721bee325c9713b196b76a631fd01a3 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 18 Sep 2026 14:46:02 -0700 Subject: [PATCH 42/47] fix(load3d): keep camera snapshot from dirtying the scene and bound stale re-capture The cloud Playwright spec load3dAgentUpdate hung on Queue because sceneWidget.serializeValue never settled. node.properties is the node data store's reactive proxy, and useLoad3d deep-watches Camera Config to mark the scene dirty. snapshotLoad3dState wrote the live camera state through that proxy, so every capture bumped the scene revision, the capture-until-stable loop saw a stale capture, and re-captured forever. Write the snapshot through toRaw(node.properties): recording the camera is a read of the scene, not a change. Bound the re-capture loop to MAX_STALE_CAPTURE_RETRIES as a safety net so queueing can never stall on a revision that keeps moving; the node stays dirty so the next queue re-captures. --- src/extensions/core/load3d.test.ts | 15 ++++++ src/extensions/core/load3d.ts | 17 +++++-- .../core/load3d/load3dSerialize.test.ts | 50 +++++++++++++++++++ src/extensions/core/load3d/load3dSerialize.ts | 12 ++++- 4 files changed, 89 insertions(+), 5 deletions(-) diff --git a/src/extensions/core/load3d.test.ts b/src/extensions/core/load3d.test.ts index f01834858a0..a033fd01d93 100644 --- a/src/extensions/core/load3d.test.ts +++ b/src/extensions/core/load3d.test.ts @@ -1239,6 +1239,21 @@ describe('Comfy.Load3D scene widget serializeValue caching', () => { expect(refreshed?.image).toBe('threed/scene-3.png [temp]') }) + it('still resolves when every capture dirties the scene again', async () => { + const { node, load3d, serialize, useLoad3dModule } = await setup() + load3d.captureScene.mockImplementation(async () => { + useLoad3dModule.markLoad3dSceneDirty(node) + return { scene: 'scene-data', mask: 'mask-data', normal: 'normal-data' } + }) + + const result = await serialize() + + expect(result?.image).toBe('threed/scene-7.png [temp]') + expect(load3d.captureScene).toHaveBeenCalledTimes(3) + expect(useLoad3dModule.isLoad3dSceneDirty(node)).toBe(true) + expect(useLoad3dModule.getLoad3dOutputCache(node)).toBeUndefined() + }) + it('returns null when no load3d instance is registered for the node', async () => { const widgets: FakeWidget[] = [ { name: 'model_file', value: 'm.glb' }, diff --git a/src/extensions/core/load3d.ts b/src/extensions/core/load3d.ts index 8844b5d143e..c49fb66bbb3 100644 --- a/src/extensions/core/load3d.ts +++ b/src/extensions/core/load3d.ts @@ -41,6 +41,10 @@ import type { NodeLocatorId } from '@/types/nodeIdentification' import { getNodeByLocatorId } from '@/utils/graphTraversalUtil' type Matrix = number[][] + +/** Extra captures allowed when the scene changes mid-capture. */ +const MAX_STALE_CAPTURE_RETRIES = 2 + type Load3dPreviewOutput = NodeOutputWith<{ result?: [string?, CameraState?, string?, Matrix?, Matrix?] }> @@ -425,7 +429,7 @@ useExtensionService().registerExtension({ return null } - for (;;) { + for (let attempt = 0; ; attempt++) { if (!isLoad3dSceneDirty(node)) { const cached = getLoad3dOutputCache(node) if (cached) return cached @@ -477,9 +481,16 @@ useExtensionService().registerExtension({ returnVal.recording = `threed/${recording.name} [temp]` } - if (!setLoad3dOutputCache(node, returnVal, sceneRevision)) continue + if (setLoad3dOutputCache(node, returnVal, sceneRevision)) { + return returnVal + } - return returnVal + // The scene moved while we captured. Retry a bounded number of + // times; a capture that raced a change is still a real capture + // of a recent scene, and queueing must never stall on a + // revision that keeps moving. The node stays dirty so the next + // queue re-captures. + if (attempt >= MAX_STALE_CAPTURE_RETRIES) return returnVal } } } diff --git a/src/extensions/core/load3d/load3dSerialize.test.ts b/src/extensions/core/load3d/load3dSerialize.test.ts index ae99489da39..6a4745cc00a 100644 --- a/src/extensions/core/load3d/load3dSerialize.test.ts +++ b/src/extensions/core/load3d/load3dSerialize.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it, vi } from 'vitest' +import { nextTick, reactive, toRaw, watch } from 'vue' import type Load3d from '@/extensions/core/load3d/Load3d' import { snapshotLoad3dState } from '@/extensions/core/load3d/load3dSerialize' @@ -84,4 +85,53 @@ describe('snapshotLoad3dState', () => { ) expect(result.model_3d_info).toEqual([]) }) + + describe('with reactive node.properties (node data store proxy)', () => { + it('records the camera state without notifying Camera Config watchers', async () => { + const props = reactive>({ + 'Camera Config': { cameraType: 'perspective', fov: 75, state: null } + }) + const node = { properties: props } as unknown as LGraphNode + let triggers = 0 + watch( + () => props['Camera Config'], + () => { + triggers++ + }, + { deep: true } + ) + + const result = snapshotLoad3dState(node, makeLoad3d()) + await nextTick() + + expect(triggers).toBe(0) + expect(result.camera_info).toBe(baseCameraState) + expect( + (toRaw(props)['Camera Config'] as Record).state + ).toBe(baseCameraState) + }) + + it('creates Camera Config without notifying watchers when it is absent', async () => { + const props = reactive>({}) + const node = { properties: props } as unknown as LGraphNode + let triggers = 0 + watch( + () => props['Camera Config'], + () => { + triggers++ + }, + { deep: true } + ) + + snapshotLoad3dState(node, makeLoad3d({ fov: 42 })) + await nextTick() + + expect(triggers).toBe(0) + expect(toRaw(props)['Camera Config']).toMatchObject({ + cameraType: 'perspective', + fov: 42, + state: baseCameraState + }) + }) + }) }) diff --git a/src/extensions/core/load3d/load3dSerialize.ts b/src/extensions/core/load3d/load3dSerialize.ts index ee0541e47fc..20e5bbd58b1 100644 --- a/src/extensions/core/load3d/load3dSerialize.ts +++ b/src/extensions/core/load3d/load3dSerialize.ts @@ -1,3 +1,5 @@ +import { toRaw } from 'vue' + import type Load3d from '@/extensions/core/load3d/Load3d' import type { CameraConfig, @@ -15,14 +17,20 @@ export function snapshotLoad3dState( node: LGraphNode, load3d: Load3d ): Load3dSerializedBase { - const cameraConfig: CameraConfig = (node.properties['Camera Config'] as + // `node.properties` is the node data store's reactive proxy, and useLoad3d + // deep-watches `Camera Config` to mark the scene dirty. Recording the live + // camera into the snapshot is a read of the scene, not a change to it, so + // write through the raw object; otherwise every capture re-dirties the + // scene and the capture-until-stable loop in load3d.ts never settles. + const properties = toRaw(node.properties) + const cameraConfig: CameraConfig = (properties['Camera Config'] as | CameraConfig | undefined) || { cameraType: load3d.getCurrentCameraType(), fov: load3d.cameraManager.perspectiveCamera.fov } cameraConfig.state = load3d.getCameraState() - node.properties['Camera Config'] = cameraConfig + properties['Camera Config'] = cameraConfig load3d.stopRecording() From 1c800940d940498b4af030518f710d4ec660de92 Mon Sep 17 00:00:00 2001 From: Amp Date: Sat, 19 Sep 2026 00:30:45 +0000 Subject: [PATCH 43/47] test: cover remaining Load3D stale state paths Amp-Thread-ID: https://ampcode.com/threads/T-01a0b107-c593-7599-a961-97b4991643d5 --- src/extensions/core/load3d.test.ts | 7 ++++--- src/services/load3dService.test.ts | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/extensions/core/load3d.test.ts b/src/extensions/core/load3d.test.ts index a033fd01d93..f6aef45782a 100644 --- a/src/extensions/core/load3d.test.ts +++ b/src/extensions/core/load3d.test.ts @@ -1239,16 +1239,17 @@ describe('Comfy.Load3D scene widget serializeValue caching', () => { expect(refreshed?.image).toBe('threed/scene-3.png [temp]') }) - it('still resolves when every capture dirties the scene again', async () => { + it('rejects when the scene never stabilizes during capture', async () => { const { node, load3d, serialize, useLoad3dModule } = await setup() load3d.captureScene.mockImplementation(async () => { useLoad3dModule.markLoad3dSceneDirty(node) return { scene: 'scene-data', mask: 'mask-data', normal: 'normal-data' } }) - const result = await serialize() + await expect(serialize()).rejects.toThrow( + 'Load3D scene did not stabilize during capture' + ) - expect(result?.image).toBe('threed/scene-7.png [temp]') expect(load3d.captureScene).toHaveBeenCalledTimes(3) expect(useLoad3dModule.isLoad3dSceneDirty(node)).toBe(true) expect(useLoad3dModule.getLoad3dOutputCache(node)).toBeUndefined() diff --git a/src/services/load3dService.test.ts b/src/services/load3dService.test.ts index 26858d8658d..ea2691a2c1d 100644 --- a/src/services/load3dService.test.ts +++ b/src/services/load3dService.test.ts @@ -442,7 +442,7 @@ describe('load3dService', () => { remove: sceneRemove } as unknown as THREE.Scene }), - loadModel: vi.fn().mockResolvedValue(undefined), + loadModel: vi.fn().mockResolvedValue(true), setMaterialMode: vi.fn(), setUpDirection: vi.fn(), applyGizmoTransform: vi.fn(), @@ -503,6 +503,26 @@ describe('load3dService', () => { expect(skeletonCloneMock).not.toHaveBeenCalled() }) + it('does not apply stale scene state after a rejected splat load', async () => { + const source = makeSource({ + currentModel: makeModel(), + isSplat: true, + originalURL: 'http://example.com/scan.splat' + }) + const { target } = makeTarget() + vi.mocked(target.loadModel).mockResolvedValue(false) + + await useLoad3dService().copyLoad3dState(source, target) + + expect(target.toggleCamera).not.toHaveBeenCalled() + expect(target.setCameraState).not.toHaveBeenCalled() + expect(target.setBackgroundColor).not.toHaveBeenCalled() + expect(target.toggleGrid).not.toHaveBeenCalled() + expect(target.setBackgroundImage).not.toHaveBeenCalled() + expect(target.setLightIntensity).not.toHaveBeenCalled() + expect(target.setFOV).not.toHaveBeenCalled() + }) + it('skips loadModel for splat models when originalURL is null', async () => { const source = makeSource({ currentModel: makeModel(), From 8061733260baa955c68a86c2fff22422a7f7881c Mon Sep 17 00:00:00 2001 From: Amp Date: Sat, 19 Sep 2026 00:46:55 +0000 Subject: [PATCH 44/47] test: expect unstable Load3D capture to fail closed Amp-Thread-ID: https://ampcode.com/threads/T-01a0b107-c593-7599-a961-97b4991643d5 --- src/extensions/core/load3d.test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/extensions/core/load3d.test.ts b/src/extensions/core/load3d.test.ts index f6aef45782a..b22d255b045 100644 --- a/src/extensions/core/load3d.test.ts +++ b/src/extensions/core/load3d.test.ts @@ -1239,16 +1239,14 @@ describe('Comfy.Load3D scene widget serializeValue caching', () => { expect(refreshed?.image).toBe('threed/scene-3.png [temp]') }) - it('rejects when the scene never stabilizes during capture', async () => { + it('returns no scene when capture never stabilizes', async () => { const { node, load3d, serialize, useLoad3dModule } = await setup() load3d.captureScene.mockImplementation(async () => { useLoad3dModule.markLoad3dSceneDirty(node) return { scene: 'scene-data', mask: 'mask-data', normal: 'normal-data' } }) - await expect(serialize()).rejects.toThrow( - 'Load3D scene did not stabilize during capture' - ) + expect(await serialize()).toBeNull() expect(load3d.captureScene).toHaveBeenCalledTimes(3) expect(useLoad3dModule.isLoad3dSceneDirty(node)).toBe(true) From 29835d5aa1e7e8bef8737be6aa4d01275fe07207 Mon Sep 17 00:00:00 2001 From: Amp Date: Sat, 19 Sep 2026 00:48:39 +0000 Subject: [PATCH 45/47] fix: fail closed on stale Load3D state Amp-Thread-ID: https://ampcode.com/threads/T-01a0b107-c593-7599-a961-97b4991643d5 --- src/extensions/core/load3d.ts | 14 ++++++++------ src/services/load3dService.ts | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/extensions/core/load3d.ts b/src/extensions/core/load3d.ts index c49fb66bbb3..d2699b1de84 100644 --- a/src/extensions/core/load3d.ts +++ b/src/extensions/core/load3d.ts @@ -36,6 +36,7 @@ import type { NodeExecutionOutput, NodeOutputWith } from '@/platform/remote/comfyui/execution/types' +import { reportError } from '@/platform/telemetry/reportError' import type { ComfyNodeDef } from '@/schemas/nodeDefSchema' import type { NodeLocatorId } from '@/types/nodeIdentification' import { getNodeByLocatorId } from '@/utils/graphTraversalUtil' @@ -485,12 +486,13 @@ useExtensionService().registerExtension({ return returnVal } - // The scene moved while we captured. Retry a bounded number of - // times; a capture that raced a change is still a real capture - // of a recent scene, and queueing must never stall on a - // revision that keeps moving. The node stays dirty so the next - // queue re-captures. - if (attempt >= MAX_STALE_CAPTURE_RETRIES) return returnVal + if (attempt >= MAX_STALE_CAPTURE_RETRIES) { + reportError( + new Error('Load3D scene did not stabilize during capture'), + { errorType: 'load3d_scene_capture_unstable' } + ) + return null + } } } } diff --git a/src/services/load3dService.ts b/src/services/load3dService.ts index af4e7d7f942..cc1550c16ec 100644 --- a/src/services/load3dService.ts +++ b/src/services/load3dService.ts @@ -232,8 +232,8 @@ class Load3dService { if (source.isSplatModel()) { const originalURL = source.modelManager.originalURL - if (originalURL) { - await target.loadModel(originalURL) + if (originalURL && !(await target.loadModel(originalURL))) { + return } } else { // Use SkeletonUtils.clone for proper skeletal animation support From 7cd66055d77cd67315a6c002b2099a023596e0ce Mon Sep 17 00:00:00 2001 From: Amp Date: Sat, 19 Sep 2026 00:53:18 +0000 Subject: [PATCH 46/47] fix: name Load3D capture diagnostic Amp-Thread-ID: https://ampcode.com/threads/T-01a0b107-c593-7599-a961-97b4991643d5 --- src/extensions/core/load3d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/core/load3d.ts b/src/extensions/core/load3d.ts index d2699b1de84..0919b9868e8 100644 --- a/src/extensions/core/load3d.ts +++ b/src/extensions/core/load3d.ts @@ -489,7 +489,7 @@ useExtensionService().registerExtension({ if (attempt >= MAX_STALE_CAPTURE_RETRIES) { reportError( new Error('Load3D scene did not stabilize during capture'), - { errorType: 'load3d_scene_capture_unstable' } + { errorType: 'error_capturing_load3d_scene_unstable' } ) return null } From 3ae8880afe30b424013999f56279776c4c6bf9cb Mon Sep 17 00:00:00 2001 From: Amp Date: Sat, 19 Sep 2026 01:08:53 +0000 Subject: [PATCH 47/47] refactor(load3d): tighten widget configuration types Amp-Thread-ID: https://ampcode.com/threads/T-01a0b107-c593-7599-a961-97b4991643d5 --- src/extensions/core/load3d.test.ts | 5 ++-- src/extensions/core/load3d.ts | 25 +++++++++++-------- .../core/load3d/Load3DConfiguration.test.ts | 21 ++++++++++------ .../core/load3d/Load3DConfiguration.ts | 15 ++++++----- src/extensions/core/load3dAdvanced.ts | 9 +++++-- 5 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/extensions/core/load3d.test.ts b/src/extensions/core/load3d.test.ts index b22d255b045..71a93fbb069 100644 --- a/src/extensions/core/load3d.test.ts +++ b/src/extensions/core/load3d.test.ts @@ -160,6 +160,7 @@ const registeredExtensionCount = registerExtensionMock.mock.calls.length interface FakeWidget { name: string + type?: string value: unknown serializeValue?: () => Promise } @@ -484,8 +485,8 @@ describe('Comfy.Load3D.nodeCreated', () => { it('configures with the input folder and width/height widgets', async () => { const widgets: FakeWidget[] = [ { name: 'model_file', value: 'model.glb' }, - { name: 'width', value: 1024 }, - { name: 'height', value: 768 }, + { name: 'width', type: 'number', value: 1024 }, + { name: 'height', type: 'number', value: 768 }, { name: 'image', value: '' } ] const node = makeLoad3DNode({ widgets }) diff --git a/src/extensions/core/load3d.ts b/src/extensions/core/load3d.ts index 0919b9868e8..e3474b27c43 100644 --- a/src/extensions/core/load3d.ts +++ b/src/extensions/core/load3d.ts @@ -30,7 +30,10 @@ import Load3dUtils from '@/extensions/core/load3d/Load3dUtils' import { t } from '@/i18n' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { IContextMenuValue } from '@/lib/litegraph/src/interfaces' -import type { IStringWidget } from '@/lib/litegraph/src/types/widgets' +import type { + INumericWidget, + IStringWidget +} from '@/lib/litegraph/src/types/widgets' import { useToastStore } from '@/platform/updates/common/toastStore' import type { NodeExecutionOutput, @@ -74,10 +77,6 @@ const inputSpecPreview3D: CustomInputSpec = { isPreview: true } -function invalidateLoad3dScene(node: LGraphNode): void { - markLoad3dSceneDirty(node) -} - async function handleModelUpload(files: FileList, node: LGraphNode) { if (!files.length) return @@ -122,7 +121,7 @@ async function handleModelUpload(files: FileList, node: LGraphNode) { modelWidget.value = uploadPath } - invalidateLoad3dScene(node) + markLoad3dSceneDirty(node) } catch (error) { console.error('Model upload failed:', error) useToastStore().addAlert(t('toastMessages.fileUploadFailed')) @@ -140,7 +139,7 @@ async function handleResourcesUpload(files: FileList, node: LGraphNode) { : '3d' await Load3dUtils.uploadMultipleFiles(files, subfolder) - invalidateLoad3dScene(node) + markLoad3dSceneDirty(node) } catch (error) { console.error('Extra resources upload failed:', error) useToastStore().addAlert(t('toastMessages.extraResourcesUploadFailed')) @@ -352,7 +351,7 @@ useExtensionService().registerExtension({ if (modelWidget) { modelWidget.value = LOAD3D_NONE_MODEL } - invalidateLoad3dScene(node) + markLoad3dSceneDirty(node) }) } @@ -396,8 +395,12 @@ useExtensionService().registerExtension({ useLoad3d(node).onLoad3dReady((load3d) => { const modelWidget = node.widgets?.find((w) => w.name === 'model_file') - const width = node.widgets?.find((w) => w.name === 'width') - const height = node.widgets?.find((w) => w.name === 'height') + const width = node.widgets?.find( + (w): w is INumericWidget => w.name === 'width' && w.type === 'number' + ) + const height = node.widgets?.find( + (w): w is INumericWidget => w.name === 'height' && w.type === 'number' + ) if (!modelWidget || !width || !height) return const cameraConfig = node.properties['Camera Config'] as @@ -412,7 +415,7 @@ useExtensionService().registerExtension({ cameraState, width, height, - onSceneInvalidated: () => invalidateLoad3dScene(node) + onSceneInvalidated: () => markLoad3dSceneDirty(node) }) }) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index afafe918950..b34258dafbf 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -17,7 +17,10 @@ import type { SceneConfig, StoredModelConfig } from '@/extensions/core/load3d/interfaces' -import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' +import type { + IBaseWidget, + INumericWidget +} from '@/lib/litegraph/src/types/widgets' import type { Dictionary } from '@/lib/litegraph/src/interfaces' import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' @@ -71,6 +74,10 @@ function reactiveWidget(value: IBaseWidget['value']): IBaseWidget { return reactive(fromPartial({ value })) } +function reactiveNumericWidget(value: number): INumericWidget { + return reactive(fromPartial({ type: 'number', value })) +} + const defaultGizmo: GizmoConfig = { enabled: false, mode: 'translate', @@ -836,8 +843,8 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { it('width.callback invokes onSceneInvalidated', async () => { const onSceneInvalidated = vi.fn() - const width = reactiveWidget(1024) - const height = reactiveWidget(1024) + const width = reactiveNumericWidget(1024) + const height = reactiveNumericWidget(1024) const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -856,8 +863,8 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { it('height.callback invokes onSceneInvalidated', async () => { const onSceneInvalidated = vi.fn() - const width = reactiveWidget(1024) - const height = reactiveWidget(1024) + const width = reactiveNumericWidget(1024) + const height = reactiveNumericWidget(1024) const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -918,8 +925,8 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }) it('callbacks remain safe when onSceneInvalidated is omitted', async () => { - const width = reactiveWidget(1024) - const height = reactiveWidget(1024) + const width = reactiveNumericWidget(1024) + const height = reactiveNumericWidget(1024) const modelWidget = reactiveWidget('none') const config = new Load3DConfiguration(makeLoad3dMock()) diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index 5457a590fab..fa8cd51719c 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -15,7 +15,10 @@ import type { } from '@/extensions/core/load3d/interfaces' import type { Dictionary } from '@/lib/litegraph/src/interfaces' import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' -import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' +import type { + IBaseWidget, + INumericWidget +} from '@/lib/litegraph/src/types/widgets' import { useSettingStore } from '@/platform/settings/settingStore' import { api } from '@/scripts/api' @@ -23,8 +26,8 @@ type Load3DConfigurationSettings = { loadFolder: string modelWidget: IBaseWidget cameraState?: CameraState - width?: IBaseWidget - height?: IBaseWidget + width?: INumericWidget + height?: INumericWidget bgImagePath?: string silentOnNotFound?: boolean /** @@ -110,7 +113,7 @@ class Load3DConfiguration { watch( [() => width.value, () => height.value], ([nextWidth, nextHeight]) => { - this.load3d.setTargetSize(nextWidth as number, nextHeight as number) + this.load3d.setTargetSize(nextWidth, nextHeight) setting.onSceneInvalidated?.() }, { flush: 'sync' } @@ -120,9 +123,9 @@ class Load3DConfiguration { this.load3d.setConfigurationCleanup(() => scope.stop()) } - private setupTargetSize(width?: IBaseWidget, height?: IBaseWidget) { + private setupTargetSize(width?: INumericWidget, height?: INumericWidget) { if (width && height) { - this.load3d.setTargetSize(width.value as number, height.value as number) + this.load3d.setTargetSize(width.value, height.value) } } diff --git a/src/extensions/core/load3dAdvanced.ts b/src/extensions/core/load3dAdvanced.ts index 9da4912408f..963887ca334 100644 --- a/src/extensions/core/load3dAdvanced.ts +++ b/src/extensions/core/load3dAdvanced.ts @@ -8,6 +8,7 @@ import Load3DConfiguration from '@/extensions/core/load3d/Load3DConfiguration' import { snapshotLoad3dState } from '@/extensions/core/load3d/load3dSerialize' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { IContextMenuValue } from '@/lib/litegraph/src/interfaces' +import type { INumericWidget } from '@/lib/litegraph/src/types/widgets' import type { CustomInputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2' import { ComponentWidgetImpl, addWidget } from '@/scripts/domWidget' import { useExtensionService } from '@/services/extensionService' @@ -67,8 +68,12 @@ useExtensionService().registerExtension({ useLoad3d(node).onLoad3dReady((load3d) => { const modelWidget = node.widgets?.find((w) => w.name === 'model_file') - const width = node.widgets?.find((w) => w.name === 'width') - const height = node.widgets?.find((w) => w.name === 'height') + const width = node.widgets?.find( + (w): w is INumericWidget => w.name === 'width' && w.type === 'number' + ) + const height = node.widgets?.find( + (w): w is INumericWidget => w.name === 'height' && w.type === 'number' + ) if (!modelWidget || !width || !height) return const cameraConfig = node.properties['Camera Config'] as