From e14cb962ab7fc7858bcc557e6cbb728f0a3f84b0 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 9 Oct 2026 16:33:06 +0000 Subject: [PATCH 1/2] fix(execution): land run outputs on agent-inserted nodes (PM-2037) (#20601) --- .../vueNodes/agent-inserted-save-image.json | 41 +++++++ .../vueNodes/agentInsertedNodeOutputs.spec.ts | 39 +++++++ .../management/stores/workflowStore.test.ts | 98 +++++++++++++++- .../management/stores/workflowStore.ts | 19 ++-- .../vueNodes/components/LGraphNode.vue | 11 +- src/stores/executionStore.test.ts | 37 +++--- ...eOutputStore.insertWorkflowOutputs.test.ts | 73 ++++++++++++ src/stores/previewExposureStore.test.ts | 8 +- src/types/nodeIdentification.test.ts | 94 ++++++++++------ src/types/nodeIdentification.ts | 89 +++++++++------ src/utils/graphTraversalUtil.test.ts | 106 +++++++++++++++++- src/utils/graphTraversalUtil.ts | 73 +++++------- 12 files changed, 525 insertions(+), 163 deletions(-) create mode 100644 browser_tests/assets/vueNodes/agent-inserted-save-image.json create mode 100644 browser_tests/tests/vueNodes/agentInsertedNodeOutputs.spec.ts create mode 100644 src/stores/nodeOutputStore.insertWorkflowOutputs.test.ts diff --git a/browser_tests/assets/vueNodes/agent-inserted-save-image.json b/browser_tests/assets/vueNodes/agent-inserted-save-image.json new file mode 100644 index 00000000000..463418dd849 --- /dev/null +++ b/browser_tests/assets/vueNodes/agent-inserted-save-image.json @@ -0,0 +1,41 @@ +{ + "last_node_id": 9, + "last_link_id": 9, + "nodes": [ + { + "id": "insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:9", + "type": "SaveImage", + "pos": { + "0": 64, + "1": 104 + }, + "size": { + "0": 210, + "1": 58 + }, + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": null + } + ], + "outputs": [], + "properties": {}, + "widgets_values": ["ComfyUI"] + } + ], + "links": [], + "groups": [], + "config": {}, + "extra": { + "ds": { + "scale": 1, + "offset": [0, 0] + } + }, + "version": 0.4 +} diff --git a/browser_tests/tests/vueNodes/agentInsertedNodeOutputs.spec.ts b/browser_tests/tests/vueNodes/agentInsertedNodeOutputs.spec.ts new file mode 100644 index 00000000000..8eb0219679d --- /dev/null +++ b/browser_tests/tests/vueNodes/agentInsertedNodeOutputs.spec.ts @@ -0,0 +1,39 @@ +import { mergeTests } from '@playwright/test' + +import { + comfyExpect as expect, + comfyPageFixture +} from '@e2e/fixtures/ComfyPage' +import { ExecutionHelper } from '@e2e/fixtures/helpers/ExecutionHelper' +import { webSocketFixture } from '@e2e/fixtures/ws' + +const test = mergeTests(comfyPageFixture, webSocketFixture) + +const INSERTED_NODE_ID = 'insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:9' + +test.describe('Agent-inserted node outputs', { tag: '@vue-nodes' }, () => { + test('shows the executed image in an agent-inserted Save Image node', async ({ + comfyPage, + getWebSocket + }) => { + const ws = await getWebSocket() + await comfyPage.workflow.loadWorkflow('vueNodes/agent-inserted-save-image') + + const exec = new ExecutionHelper(comfyPage, ws) + const jobId = await exec.run() + exec.executionStart(jobId) + exec.executing(jobId, INSERTED_NODE_ID) + exec.executed(jobId, INSERTED_NODE_ID, { + images: [{ filename: 'example.png', subfolder: '', type: 'input' }] + }) + exec.executing(jobId, null) + exec.executionSuccess(jobId) + exec.status(0) + + await expect( + comfyPage.vueNodes + .getNodeByTitle('Save Image') + .getByRole('img', { name: 'View image 1 of 1' }) + ).toBeVisible() + }) +}) diff --git a/src/platform/workflow/management/stores/workflowStore.test.ts b/src/platform/workflow/management/stores/workflowStore.test.ts index 852ec62c230..b2badf9bf9c 100644 --- a/src/platform/workflow/management/stores/workflowStore.test.ts +++ b/src/platform/workflow/management/stores/workflowStore.test.ts @@ -19,6 +19,7 @@ import { defaultGraph, defaultGraphJSON } from '@/scripts/defaultGraph' import { useExecutionStore } from '@/stores/executionStore' import { toNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' +import type { NodeLocatorId } from '@/types/nodeIdentification' import { createNodeLocatorId } from '@/types/nodeIdentification' import { isValidUuid } from '@/utils/formatUtil' import { syncEntities } from '@/utils/syncUtil' @@ -1075,11 +1076,23 @@ describe('useWorkflowStore', () => { expect(result).toBe('a1b2c3d4-e5f6-7890-abcd-ef1234567890:456') }) - it('should return simple node ID for root graph nodes', () => { - store.activeSubgraph = undefined - const result = store.nodeIdToNodeLocatorId(toNodeId(123)) - expect(result).toBe('123') - }) + it.for([ + { rawId: '', locatorId: '~root:' }, + { + rawId: 'insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:9', + locatorId: + '~root:insert%3A0fbd38ecb13037d0b3b0ca78b8a20a5a%3Aroot%3Anode%3A9' + } + ])( + 'should key root node id $rawId under $locatorId', + ({ rawId, locatorId }) => { + store.activeSubgraph = undefined + const result = store.nodeIdToNodeLocatorId(toNodeId(rawId)) + + expect(result).toBe(locatorId) + expect(store.nodeLocatorIdToNodeId(result)).toBe(rawId) + } + ) it('should use provided subgraph instead of active one', () => { const customSubgraphId = '11111111-2222-4333-8444-555555555555' @@ -1146,6 +1159,25 @@ describe('useWorkflowStore', () => { ) expect(stringResult).toBe('node_1') }) + + it('should decode an encoded root-leaf locator to its node id', () => { + const locatorId = createNodeLocatorId( + null, + 'insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:9' + ) + const result = store.nodeLocatorIdToNodeId(locatorId) + expect(result).toBe( + 'insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:9' + ) + }) + + it('should return null for an unparseable locator', () => { + const locatorId = fromAny( + '~subgraph:not-a-uuid:5' + ) + + expect(store.nodeLocatorIdToNodeId(locatorId)).toBeNull() + }) }) describe('nodeLocatorIdToNodeExecutionId', () => { @@ -1179,6 +1211,62 @@ describe('useWorkflowStore', () => { ) expect(result).toBeNull() }) + + it('should return null for an unparseable locator', () => { + const locatorId = fromAny( + '~subgraph:not-a-uuid:5' + ) + + expect(store.nodeLocatorIdToNodeExecutionId(locatorId)).toBeNull() + }) + + it('should mint a leaf execution id from an encoded root-leaf locator', () => { + const locatorId = createNodeLocatorId( + null, + 'insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:9' + ) + const result = store.nodeLocatorIdToNodeExecutionId(locatorId) + expect(result).toBe( + 'insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:9' + ) + }) + + it('should append an encoded subgraph leaf to its execution path', () => { + vi.mocked(isSubgraph).mockImplementation((obj): obj is Subgraph => { + return obj === store.activeSubgraph + }) + const locatorId = createNodeLocatorId( + 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', + 'insert:abc123:root:node:5' + ) + + const result = store.nodeLocatorIdToNodeExecutionId(locatorId) + + expect(result).toBe('123:insert:abc123:root:node:5') + }) + + it('should preserve a colon-bearing subgraph host in the execution path', () => { + const subgraphUuid = '11111111-2222-4333-8444-555555555555' + const subgraph = fromPartial({ id: subgraphUuid }) + vi.mocked(comfyApp).rootGraph = fromPartial({ + _nodes: [ + createMockLGraphNode({ + id: toNodeId('insert:op:root:node:3'), + isSubgraphNode: () => true, + subgraph + }) + ] + }) + vi.mocked(isSubgraph).mockImplementation( + (graph): graph is Subgraph => graph === subgraph + ) + + expect( + store.nodeLocatorIdToNodeExecutionId( + createNodeLocatorId(subgraphUuid, toNodeId(7)) + ) + ).toBe('insert:op:root:node:3:7') + }) }) }) diff --git a/src/platform/workflow/management/stores/workflowStore.ts b/src/platform/workflow/management/stores/workflowStore.ts index 3bd2fb1e2f0..45524d7e0af 100644 --- a/src/platform/workflow/management/stores/workflowStore.ts +++ b/src/platform/workflow/management/stores/workflowStore.ts @@ -24,10 +24,11 @@ import { defaultGraph } from '@/scripts/defaultGraph' import { useExecutionStore } from '@/stores/executionStore' import type { NodeExecutionId, NodeLocatorId } from '@/types/nodeIdentification' import { - createNodeExecutionId, + createLeafNodeExecutionId, createNodeLocatorId, parseNodeExecutionId, - parseNodeLocatorId + parseNodeLocatorId, + tryNormalizeNodeExecutionId } from '@/types/nodeIdentification' import { parseNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' @@ -88,7 +89,7 @@ interface WorkflowStore { executionIdToCurrentId: (id: string) => string | undefined nodeIdToNodeLocatorId: (nodeId: NodeId, subgraph?: Subgraph) => NodeLocatorId nodeToNodeLocatorId: (node: LGraphNode) => NodeLocatorId - nodeLocatorIdToNodeId: (locatorId: NodeLocatorId) => NodeId + nodeLocatorIdToNodeId: (locatorId: NodeLocatorId) => NodeId | null nodeLocatorIdToNodeExecutionId: ( locatorId: NodeLocatorId, targetSubgraph?: Subgraph @@ -635,7 +636,7 @@ export const useWorkflowStore = defineStore('workflow', () => { * Convert a node ID to a NodeLocatorId * @param nodeId The local node ID * @param subgraph The subgraph containing the node (defaults to active subgraph) - * @returns The NodeLocatorId (for root graph nodes, returns the node ID as-is) + * @returns The NodeLocatorId */ const nodeIdToNodeLocatorId = ( nodeId: NodeId, @@ -643,7 +644,6 @@ export const useWorkflowStore = defineStore('workflow', () => { ): NodeLocatorId => { const targetSubgraph = subgraph ?? activeSubgraph.value if (!targetSubgraph) { - // Node is in the root graph, return the node ID as-is return createNodeLocatorId(null, nodeId) } @@ -666,9 +666,8 @@ export const useWorkflowStore = defineStore('workflow', () => { * @param locatorId The NodeLocatorId * @returns The local node ID */ - const nodeLocatorIdToNodeId = (locatorId: NodeLocatorId): NodeId => { - return parseNodeLocatorId(locatorId)!.localNodeId - } + const nodeLocatorIdToNodeId = (locatorId: NodeLocatorId): NodeId | null => + parseNodeLocatorId(locatorId)?.localNodeId ?? null /** * Convert a NodeLocatorId to an execution ID for a specific context @@ -687,7 +686,7 @@ export const useWorkflowStore = defineStore('workflow', () => { // If no subgraph UUID, this is a root graph node if (!subgraphUuid) { - return createNodeExecutionId([localNodeId]) + return createLeafNodeExecutionId(localNodeId) } // Find the path from root to the subgraph with this UUID @@ -730,7 +729,7 @@ export const useWorkflowStore = defineStore('workflow', () => { return null } - return createNodeExecutionId([...path, localNodeId]) + return tryNormalizeNodeExecutionId([...path, localNodeId].join(':')) } return { diff --git a/src/renderer/extensions/vueNodes/components/LGraphNode.vue b/src/renderer/extensions/vueNodes/components/LGraphNode.vue index 6786b9f6314..50b0e176bc5 100644 --- a/src/renderer/extensions/vueNodes/components/LGraphNode.vue +++ b/src/renderer/extensions/vueNodes/components/LGraphNode.vue @@ -313,8 +313,7 @@ import { useRightSidePanelStore } from '@/stores/workspace/rightSidePanelStore' import { isVideoOutput } from '@/utils/litegraphUtil' import { getNodeByLocatorId, - locatorIdFromState, - subgraphIdFromState + locatorIdFromState } from '@/utils/graphTraversalUtil' import { cn } from '@comfyorg/tailwind-utils' import { toNodeId } from '@/types/nodeId' @@ -637,11 +636,6 @@ const handleEnterSubgraph = () => { const nodeOutputs = useNodeOutputStore() -const nodeOutputLocatorId = computed(() => { - const subgraphId = subgraphIdFromState(nodeData, canvasStore.rootGraphId) - return subgraphId ? `${subgraphId}:${nodeData.id}` : nodeData.id -}) - function resolveLGraphNode() { const locatorId = nodeLocatorId.value if (!locatorId) return null @@ -723,7 +717,8 @@ const hasVideoEditWidget = computed(() => ) const nodeMedia = computed(() => { - const newOutputs = nodeOutputs.nodeOutputs[nodeOutputLocatorId.value] + const locatorId = nodeLocatorId.value + const newOutputs = locatorId ? nodeOutputs.nodeOutputs[locatorId] : undefined const node = lgraphNode.value if ( diff --git a/src/stores/executionStore.test.ts b/src/stores/executionStore.test.ts index 98b6e0458fd..20099fdce0b 100644 --- a/src/stores/executionStore.test.ts +++ b/src/stores/executionStore.test.ts @@ -122,6 +122,17 @@ function createPromptNode(title: string, classType: string) { } } +const TEST_SUBGRAPH_UUID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' + +function createTestSubgraph(...nodeIds: number[]) { + const nodes = nodeIds.map((id) => createMockLGraphNode({ id: toNodeId(id) })) + return { + id: TEST_SUBGRAPH_UUID, + nodes, + getNodeById: (id: string) => nodes.find((node) => node.id === id) ?? null + } +} + describe('useExecutionStore - NodeLocatorId conversions', () => { let store: ReturnType @@ -134,10 +145,7 @@ describe('useExecutionStore - NodeLocatorId conversions', () => { describe('executionIdToNodeLocatorId', () => { it('should convert execution ID to NodeLocatorId', () => { // Mock subgraph structure - const mockSubgraph = { - id: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', - nodes: [] - } + const mockSubgraph = createTestSubgraph(456) const mockNode = createMockLGraphNode({ id: 123, @@ -227,10 +235,7 @@ describe('useExecutionStore - nodeLocationProgressStates caching', () => { }) it('should resolve execution IDs to locator IDs for subgraph nodes', () => { - const mockSubgraph = { - id: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', - nodes: [] - } + const mockSubgraph = createTestSubgraph(456) const mockNode = createMockLGraphNode({ id: 123, isSubgraphNode: () => true, @@ -263,10 +268,7 @@ describe('useExecutionStore - nodeLocationProgressStates caching', () => { }) it('should not re-traverse graph for same execution IDs across progress updates', () => { - const mockSubgraph = { - id: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', - nodes: [] - } + const mockSubgraph = createTestSubgraph(456) const mockNode = createMockLGraphNode({ id: 123, isSubgraphNode: () => true, @@ -315,10 +317,7 @@ describe('useExecutionStore - nodeLocationProgressStates caching', () => { }) it('should correctly resolve multiple sibling nodes in the same subgraph', () => { - const mockSubgraph = { - id: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', - nodes: [] - } + const mockSubgraph = createTestSubgraph(456, 789) const mockNode = createMockLGraphNode({ id: 123, isSubgraphNode: () => true, @@ -1523,11 +1522,7 @@ describe('useExecutionErrorStore - Node Error Lookups', () => { it('should return node error by locator ID for subgraph node', () => { const subgraphUuid = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' - const mockSubgraph = { - id: subgraphUuid, - getNodeById: vi.fn(), - nodes: [] - } + const mockSubgraph = createTestSubgraph(456) const mockNode = createMockLGraphNode({ id: 123, diff --git a/src/stores/nodeOutputStore.insertWorkflowOutputs.test.ts b/src/stores/nodeOutputStore.insertWorkflowOutputs.test.ts new file mode 100644 index 00000000000..7971535f987 --- /dev/null +++ b/src/stores/nodeOutputStore.insertWorkflowOutputs.test.ts @@ -0,0 +1,73 @@ +import { assert, describe, expect, it, vi } from 'vitest' + +import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' +import type { ExecutedWsMessage } from '@/platform/remote/comfyui/execution/types' +import { app } from '@/scripts/app' +import { useNodeOutputStore } from '@/stores/nodeOutputStore' +import { toNodeId } from '@/types/nodeId' +import { tryNormalizeNodeExecutionId } from '@/types/nodeIdentification' + +vi.mock(import('@/scripts/app')) + +const REMAPPED_ID = 'insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:9' + +const OUTPUT: ExecutedWsMessage['output'] = { + images: [{ filename: 'astronaut.png', subfolder: '', type: 'output' }] +} + +class SaveImage extends LGraphNode { + constructor() { + super('SaveImage') + this.addInput('images', 'IMAGE') + } +} + +function addRootSaveImageNode(rawNodeId: string) { + const graph = new LGraph() + const node = new SaveImage() + node.id = toNodeId(rawNodeId) + graph.add(node) + Object.assign(app, { + rootGraph: graph, + nodeOutputs: {}, + nodePreviewImages: {} + }) + return node +} + +function setOutputsByRawExecutionId(rawNodeId: string) { + const executionId = tryNormalizeNodeExecutionId(rawNodeId) + assert.exists(executionId) + useNodeOutputStore().setNodeOutputsByExecutionId(executionId, OUTPUT) +} + +describe('nodeOutputStore: outputs for insert_workflow-remapped node ids', () => { + it('regression: lands an executed output on an agent-inserted node (PM-2037)', () => { + const node = addRootSaveImageNode(REMAPPED_ID) + + setOutputsByRawExecutionId(REMAPPED_ID) + + expect(useNodeOutputStore().getNodeOutputs(node)).toEqual(OUTPUT) + }) + + it('keys two inserted nodes separately rather than collapsing them', () => { + const graph = new LGraph() + const first = new SaveImage() + first.id = toNodeId(REMAPPED_ID) + const second = new SaveImage() + second.id = toNodeId('insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:10') + graph.add(first) + graph.add(second) + Object.assign(app, { + rootGraph: graph, + nodeOutputs: {}, + nodePreviewImages: {} + }) + + setOutputsByRawExecutionId(String(first.id)) + + const store = useNodeOutputStore() + expect(store.getNodeOutputs(first)).toEqual(OUTPUT) + expect(store.getNodeOutputs(second)).toBeUndefined() + }) +}) diff --git a/src/stores/previewExposureStore.test.ts b/src/stores/previewExposureStore.test.ts index 202a71b9cb4..0ffefade2fb 100644 --- a/src/stores/previewExposureStore.test.ts +++ b/src/stores/previewExposureStore.test.ts @@ -11,17 +11,15 @@ import { } from './previewExposureStore' describe(getPreviewExposureHostLocator, () => { - it('reports a host ID that cannot form a locator', () => { + it('encodes a delimiter-bearing host ID', () => { const host = fromAny({ graph: null, id: toNodeId('invalid:id') }) const error = vi.spyOn(console, 'error').mockImplementation(() => {}) - expect(getPreviewExposureHostLocator(host)).toBeNull() - expect(error).toHaveBeenCalledWith( - 'Cannot create preview exposure host locator for node invalid:id' - ) + expect(getPreviewExposureHostLocator(host)).toBe('~root:invalid%3Aid') + expect(console.error).not.toHaveBeenCalled() }) }) diff --git a/src/types/nodeIdentification.test.ts b/src/types/nodeIdentification.test.ts index 7352eb7b7a8..c0f24a1ab32 100644 --- a/src/types/nodeIdentification.test.ts +++ b/src/types/nodeIdentification.test.ts @@ -5,7 +5,6 @@ import type { NodeId } from '@/types/nodeId' import { compareExecutionId, createLeafNodeExecutionId, - createLeafNodeLocatorId, createNodeExecutionId, createNodeLocatorId, getAncestorExecutionIds, @@ -115,6 +114,14 @@ describe('nodeIdentification', () => { expect(parseNodeLocatorId(`${validUuid}:node:1`)).toBeNull() expect(parseNodeLocatorId('')).toBeNull() }) + + it.for(['~root:insert:op', '~root:%69nsert%3Aop'])( + 'rejects non-canonical encoded locator %s', + (locatorId) => { + expect(parseNodeLocatorId(locatorId)).toBeNull() + expect(isNodeLocatorId(locatorId)).toBe(false) + } + ) }) describe('createNodeLocatorId', () => { @@ -129,43 +136,66 @@ describe('nodeIdentification', () => { expect(result).toBe(`${validUuid}:node_1`) expect(isNodeLocatorId(result)).toBe(true) }) - - it('should return null for node ID segments with separators', () => { - expect(createNodeLocatorId(validUuid, toNodeId('node:1'))).toBeNull() - expect(createNodeLocatorId(null, toNodeId('node:1'))).toBeNull() - }) }) - describe('createLeafNodeLocatorId', () => { - it('behaves like createNodeLocatorId for an ordinary, colon-free root id', () => { - expect(createLeafNodeLocatorId(null, toNodeId(123))).toBe('123') - }) - - it('behaves like createNodeLocatorId for an ordinary subgraph-nested id', () => { - expect(createLeafNodeLocatorId(validUuid, toNodeId(123))).toBe( - validNodeLocatorId - ) - }) - - it('keeps a colon-bearing root-level id whole instead of rejecting it (PM-1580)', () => { - // comfy-multi-player's insert_workflow remaps every inserted node's - // id to a derived string with colons unrelated to subgraph scoping. - const rawId = 'insert:abc123:root:node:5' - expect(createNodeLocatorId(null, toNodeId(rawId))).toBeNull() - expect(createLeafNodeLocatorId(null, rawId)).toBe(rawId) + describe('createNodeLocatorId encoded forms', () => { + it('keeps an ordinary root id as-is', () => { + expect(createNodeLocatorId(null, toNodeId(123))).toBe('123') }) - it('still rejects a colon-bearing id when it really is subgraph-nested', () => { - // There is no subgraph UUID to disambiguate a colon-bearing id from - // in the root-level case, but a node that IS scoped to a subgraph - // still goes through the strict, delimiter-aware path. - const rawId = 'insert:abc123:root:node:5' - expect(createLeafNodeLocatorId(validUuid, rawId)).toBeNull() + it.for([ + { + name: 'colon-bearing root-level id', + subgraphUuid: null, + rawId: 'insert:abc123:root:node:5', + expectedLocator: '~root:insert%3Aabc123%3Aroot%3Anode%3A5' + }, + { + name: 'colon-bearing subgraph-local id', + subgraphUuid: validUuid, + rawId: 'insert:abc123:root:node:5', + expectedLocator: `~subgraph:${validUuid}:insert%3Aabc123%3Aroot%3Anode%3A5` + }, + { + name: 'root id shaped like a subgraph locator', + subgraphUuid: null, + rawId: `${validUuid}:123`, + expectedLocator: `~root:${validUuid}%3A123` + }, + { + name: 'empty root id', + subgraphUuid: null, + rawId: '', + expectedLocator: '~root:' + }, + { + name: 'non-integer numeric root id', + subgraphUuid: null, + rawId: 1.5, + expectedLocator: '~root:1.5' + }, + { + name: 'root id containing a literal escape sequence', + subgraphUuid: null, + rawId: 'a%3Ab:c', + expectedLocator: '~root:a%253Ab%3Ac' + } + ])('encodes $name', ({ subgraphUuid, rawId, expectedLocator }) => { + const locatorId = createNodeLocatorId(subgraphUuid, rawId) + + expect(locatorId).toBe(expectedLocator) + expect(parseNodeLocatorId(locatorId)).toEqual({ + subgraphUuid, + localNodeId: String(rawId) + }) }) - it('returns null for an empty id', () => { - expect(createLeafNodeLocatorId(null, '')).toBeNull() - }) + it.for(['~subgraph:not-a-uuid:x', `~subgraph:${validUuid}`, '~root:%ZZ'])( + 'rejects malformed encoded locator %s', + (locatorId) => { + expect(parseNodeLocatorId(locatorId)).toBeNull() + } + ) }) }) diff --git a/src/types/nodeIdentification.ts b/src/types/nodeIdentification.ts index 607a7b2c889..c2b2fda1092 100644 --- a/src/types/nodeIdentification.ts +++ b/src/types/nodeIdentification.ts @@ -1,8 +1,36 @@ -import { parseNodeId } from '@/types/nodeId' +import { parseNodeId, toNodeId } from '@/types/nodeId' import type { NodeId, SerializedNodeId } from '@/types/nodeId' const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i +const ENCODED_ROOT_LOCATOR_PREFIX = '~root:' +const ENCODED_SUBGRAPH_LOCATOR_PREFIX = '~subgraph:' + +interface ParsedNodeLocatorId { + subgraphUuid: string | null + localNodeId: NodeId +} + +function decodeNodeId(value: string): NodeId | null { + try { + const decoded = decodeURIComponent(value) + return encodeURIComponent(decoded) === value ? toNodeId(decoded) : null + } catch { + return null + } +} + +function parseEncodedSubgraphLocator(id: string): ParsedNodeLocatorId | null { + const encodedLocator = id.slice(ENCODED_SUBGRAPH_LOCATOR_PREFIX.length) + const separatorIndex = encodedLocator.indexOf(':') + if (separatorIndex === -1) return null + + const subgraphUuid = encodedLocator.slice(0, separatorIndex) + if (!UUID_PATTERN.test(subgraphUuid)) return null + + const localNodeId = decodeNodeId(encodedLocator.slice(separatorIndex + 1)) + return localNodeId !== null ? { subgraphUuid, localNodeId } : null +} /** * A globally unique identifier for nodes that maintains consistency across @@ -68,9 +96,18 @@ export function isNodeExecutionId(value: unknown): value is NodeExecutionId { * @param id The NodeLocatorId to parse * @returns The subgraph UUID and local node ID, or null if invalid */ -export function parseNodeLocatorId( - id: string -): { subgraphUuid: string | null; localNodeId: NodeId } | null { +export function parseNodeLocatorId(id: string): ParsedNodeLocatorId | null { + if (id.startsWith(ENCODED_ROOT_LOCATOR_PREFIX)) { + const localNodeId = decodeNodeId( + id.slice(ENCODED_ROOT_LOCATOR_PREFIX.length) + ) + return localNodeId !== null ? { subgraphUuid: null, localNodeId } : null + } + + if (id.startsWith(ENCODED_SUBGRAPH_LOCATOR_PREFIX)) { + return parseEncodedSubgraphLocator(id) + } + const parts = id.split(':') if (parts.length === 1) { @@ -95,50 +132,32 @@ export function parseNodeLocatorId( * Create a NodeLocatorId from components * @param subgraphUuid The UUID of the immediate containing subgraph * @param localNodeId The local node ID within that subgraph - * @returns A properly formatted NodeLocatorId + * @returns A locator that encodes local IDs that are not plain segments, or + * `null` when the containing subgraph ID is not a UUID */ export function createNodeLocatorId( subgraphUuid: string | null, - localNodeId: NodeId + localNodeId: SerializedNodeId ): NodeLocatorId export function createNodeLocatorId( subgraphUuid: string | null, - localNodeId: NodeId + localNodeId: SerializedNodeId ): NodeLocatorId | null { const nodeId = requireNodeIdSegment(localNodeId) - if (!nodeId) return null if (subgraphUuid && !UUID_PATTERN.test(subgraphUuid)) return null - if (!subgraphUuid) return String(nodeId) as NodeLocatorId + if (nodeId) { + if (!subgraphUuid) return String(nodeId) as NodeLocatorId - return `${subgraphUuid}:${nodeId}` as NodeLocatorId -} - -/** - * Create a `NodeLocatorId` from components, tolerating a colon inside a - * root-graph local id. - * - * `createNodeLocatorId` rejects a colon in `localNodeId` because colon is - * the delimiter between the subgraph UUID and the local id. That is the - * right contract for a node that really lives in a subgraph, but it is too - * strict for a root-graph node (no `subgraphUuid`) whose raw id itself - * contains colons for reasons that have nothing to do with locator-id - * encoding (comfy-multi-player's `insert_workflow` remapped ids, e.g. - * `insert::root:node:`, PM-1580). There is no subgraph - * UUID to disambiguate such an id from, so nothing is lost by keeping it - * whole rather than rejecting it outright. - */ -export function createLeafNodeLocatorId( - subgraphUuid: string | null, - localNodeId: SerializedNodeId -): NodeLocatorId | null { - const strictNodeId = requireNodeIdSegment(localNodeId) - if (strictNodeId) return createNodeLocatorId(subgraphUuid, strictNodeId) - if (subgraphUuid) return null + return `${subgraphUuid}:${nodeId}` as NodeLocatorId + } - const bareNodeId = parseNodeId(localNodeId) - return bareNodeId ? (String(bareNodeId) as NodeLocatorId) : null + const encodedNodeId = encodeURIComponent(String(localNodeId)) + return subgraphUuid + ? (`${ENCODED_SUBGRAPH_LOCATOR_PREFIX}${subgraphUuid}:${encodedNodeId}` as NodeLocatorId) + : (`${ENCODED_ROOT_LOCATOR_PREFIX}${encodedNodeId}` as NodeLocatorId) } + /** * Parse a NodeExecutionId into its component node IDs * @param id The NodeExecutionId to parse diff --git a/src/utils/graphTraversalUtil.test.ts b/src/utils/graphTraversalUtil.test.ts index cc64619d01e..e7b18acde17 100644 --- a/src/utils/graphTraversalUtil.test.ts +++ b/src/utils/graphTraversalUtil.test.ts @@ -37,7 +37,8 @@ import { isCandidateScopeActive, isExecutionPathActive, isMissingCandidateActive, - findSubgraphNodePathById + findSubgraphNodePathById, + executionIdToNodeLocatorId } from '@/utils/graphTraversalUtil' import { LGraphEventMode } from '@/lib/litegraph/src/types/globalEnums' import { toNodeId } from '@/types/nodeId' @@ -617,6 +618,9 @@ describe('graphTraversalUtil', () => { }) describe('getNodeByExecutionId', () => { + const remappedNodeId = + 'insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:9' + it('should find node in root graph', () => { const nodes = [createMockNode('123'), createMockNode('456')] @@ -683,6 +687,27 @@ describe('graphTraversalUtil', () => { const found = getNodeByExecutionId(graph, '') expect(found).toBeNull() }) + + it('finds a root node whose id contains execution path delimiters', () => { + const targetNode = createMockNode(remappedNodeId) + const graph = createMockGraph([targetNode]) + + expect(getNodeByExecutionId(graph, remappedNodeId)).toBe(targetNode) + }) + + it('finds an interior node below a colon-bearing subgraph host id', () => { + const targetNode = createMockNode('7') + const subgraph = createMockSubgraph('sub-uuid', [targetNode]) + const host = createMockNode(remappedNodeId, { + isSubgraph: true, + subgraph + }) + const graph = createMockGraph([host]) + + expect(getNodeByExecutionId(graph, `${remappedNodeId}:7`)).toBe( + targetNode + ) + }) }) describe('getExecutionIdByNode', () => { @@ -1087,17 +1112,19 @@ describe('graphTraversalUtil', () => { { id: toNodeId(rawId), graphId: ROOT_GRAPH_ID }, ROOT_GRAPH_ID ) - expect(locatorId).toBe(rawId) + expect(locatorId).toBe('~root:insert%3Aabc123%3Aroot%3Anode%3A5') }) - it('still rejects a colon-bearing id when the node really is subgraph-owned (no regression)', () => { + it('encodes a colon-bearing id when the node is subgraph-owned', () => { const subgraphUuid = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' const rawId = 'insert:abc123:root:node:5' const locatorId = locatorIdFromState( { id: toNodeId(rawId), graphId: subgraphUuid }, ROOT_GRAPH_ID ) - expect(locatorId).toBeNull() + expect(locatorId).toBe( + `~subgraph:${subgraphUuid}:insert%3Aabc123%3Aroot%3Anode%3A5` + ) }) }) @@ -1748,4 +1775,75 @@ describe('graphTraversalUtil', () => { }) }) }) + + describe('executionIdToNodeLocatorId', () => { + const REMAPPED_ID = 'insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:9' + + it('regression: resolves an insert_workflow-remapped root id to its root locator (PM-2037)', () => { + const graph = createMockGraph([createMockNode(REMAPPED_ID)]) + + expect(executionIdToNodeLocatorId(graph, REMAPPED_ID)).toBe( + '~root:insert%3A0fbd38ecb13037d0b3b0ca78b8a20a5a%3Aroot%3Anode%3A9' + ) + }) + + it('still returns undefined for a colon-bearing id no node on the graph carries', () => { + const graph = createMockGraph([createMockNode('9')]) + + expect(executionIdToNodeLocatorId(graph, REMAPPED_ID)).toBeUndefined() + }) + + it('keeps the subgraph-path reading when both readings are available', () => { + const subgraphUuid = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' + const interior = createMockNode('999') + const subgraph = createMockSubgraph(subgraphUuid, [interior]) + const host = createMockNode('123', { isSubgraph: true, subgraph }) + const literal = createMockNode('123:999') + const graph = createMockGraph([host, literal]) + + expect(executionIdToNodeLocatorId(graph, '123:999')).toBe( + `${subgraphUuid}:999` + ) + }) + + it('falls back to a literal root id when the parsed subgraph leaf does not exist', () => { + const subgraph = createMockSubgraph( + 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', + [] + ) + const host = createMockNode('123', { isSubgraph: true, subgraph }) + const literal = createMockNode('123:999') + const graph = createMockGraph([host, literal]) + + expect(executionIdToNodeLocatorId(graph, '123:999')).toBe( + '~root:123%3A999' + ) + }) + + it('leaves a missing subgraph leaf unresolved without a literal root id', () => { + const subgraph = createMockSubgraph( + 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', + [] + ) + const host = createMockNode('123', { isSubgraph: true, subgraph }) + const graph = createMockGraph([host]) + + expect(executionIdToNodeLocatorId(graph, '123:999')).toBeUndefined() + }) + + it('resolves an interior node below a colon-bearing subgraph host id', () => { + const subgraphUuid = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' + const interior = createMockNode('7') + const subgraph = createMockSubgraph(subgraphUuid, [interior]) + const host = createMockNode(REMAPPED_ID, { + isSubgraph: true, + subgraph + }) + const graph = createMockGraph([host]) + + expect(executionIdToNodeLocatorId(graph, `${REMAPPED_ID}:7`)).toBe( + `${subgraphUuid}:7` + ) + }) + }) }) diff --git a/src/utils/graphTraversalUtil.ts b/src/utils/graphTraversalUtil.ts index bb12db8b37d..b69a4d25d9a 100644 --- a/src/utils/graphTraversalUtil.ts +++ b/src/utils/graphTraversalUtil.ts @@ -8,13 +8,12 @@ import { LGraphEventMode } from '@/lib/litegraph/src/types/globalEnums' import type { NodeExecutionId, NodeLocatorId } from '@/types/nodeIdentification' import { createLeafNodeExecutionId, - createLeafNodeLocatorId, createNodeExecutionId, createNodeLocatorId, getParentExecutionIds, parseNodeLocatorId } from '@/types/nodeIdentification' -import { parseNodeId } from '@/types/nodeId' +import { parseNodeId, toNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' import type { NodeState } from '@/types/nodeState' import type { UUID } from '@/utils/uuid' @@ -43,21 +42,12 @@ export function subgraphIdFromState( /** * The locator id for a node described by its shell state. - * - * A root-owned node has no ancestor path to encode, so its raw id can be - * kept whole even when it contains a colon that isn't a subgraph-scope - * prefix (comfy-multi-player's insert_workflow remapped ids, PM-1580) — see - * `createLeafNodeLocatorId`. A node that IS meant to live inside a subgraph - * still goes through the strict, delimiter-aware path. */ export function locatorIdFromState( state: Pick, rootGraphId: UUID | undefined ): NodeLocatorId | null { - return createLeafNodeLocatorId( - subgraphIdFromState(state, rootGraphId), - state.id - ) + return createNodeLocatorId(subgraphIdFromState(state, rootGraphId), state.id) } function parseNodeIdPath(path: string[]): NodeId[] | null { @@ -442,25 +432,35 @@ export function getNodeByExecutionId( rootGraph: LGraph, executionId: string ): LGraphNode | null { - const localNodeId = getLocalNodeIdFromExecutionId(executionId) - if (!localNodeId) return null + return resolveExecutionNode(rootGraph, executionId)?.node ?? null +} - const subgraphPath = getSubgraphPathFromExecutionId(executionId) +interface ResolvedExecutionNode { + graph: LGraph | Subgraph + node: LGraphNode +} - // If no subgraph path, it's in the root graph - const parsedLocalNodeId = parseNodeId(localNodeId) - if (!parsedLocalNodeId) return null +function resolveExecutionNode( + graph: LGraph | Subgraph, + executionId: string +): ResolvedExecutionNode | null { + for ( + let end = executionId.indexOf(':'); + end !== -1; + end = executionId.indexOf(':', end + 1) + ) { + const host = graph.getNodeById(toNodeId(executionId.slice(0, end))) + if (!host || !isSubgraphNode(host)) continue - if (subgraphPath.length === 0) { - return rootGraph.getNodeById(parsedLocalNodeId) || null + const nested = resolveExecutionNode( + host.subgraph, + executionId.slice(end + 1) + ) + if (nested) return nested } - // Traverse to the target subgraph - const targetGraph = traverseSubgraphPath(rootGraph, subgraphPath) - if (!targetGraph) return null - - // Get the node from the target graph - return targetGraph.getNodeById(parsedLocalNodeId) || null + const node = executionId ? graph.getNodeById(toNodeId(executionId)) : null + return node ? { graph, node } : null } /** @@ -668,12 +668,6 @@ export function getNodeByLocatorId( ): LGraphNode | null { const parsedIds = parseNodeLocatorId(locatorId) if (!parsedIds) { - // parseNodeLocatorId's delimiter-aware format rejects a locator id - // whose local id itself contains a colon that isn't a subgraph-scope - // prefix (comfy-multi-player's insert_workflow remapped ids, PM-1580). - // createLeafNodeLocatorId keeps such an id whole instead of splitting - // it into `:`, so there is no subgraph prefix to - // strip here either: resolve it directly against the root graph. const leafNodeId = parseNodeId(locatorId) return leafNodeId ? rootGraph.getNodeById(leafNodeId) || null : null } @@ -714,19 +708,12 @@ export function executionIdToNodeLocatorId( return createNodeLocatorId(null, localNodeId) } - // It's an execution node ID — resolve subgraph path if (!rootGraph) return undefined - const parts = nodeIdStr.split(':') - const localNodeId = parts.at(-1)! - const subgraphPath = parts.slice(0, -1) - - const targetGraph = traverseSubgraphPath(rootGraph, subgraphPath) - if (!targetGraph) return undefined - - const parsedLocalNodeId = parseNodeId(localNodeId) - if (!parsedLocalNodeId) return undefined + const resolved = resolveExecutionNode(rootGraph, nodeIdStr) + if (!resolved) return undefined - return createNodeLocatorId(targetGraph.id, parsedLocalNodeId) + const subgraphUuid = resolved.graph === rootGraph ? null : resolved.graph.id + return createNodeLocatorId(subgraphUuid, resolved.node.id) } /** From 85678792ee2b7b8b5ae3fe1cf585dccc7417248b Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Fri, 9 Oct 2026 11:36:41 -0700 Subject: [PATCH 2/2] fix(execution): reconcile core 1.55 locator API --- .../composables/processedWidgetRenderModel.ts | 6 +++--- ...nodeOutputStore.insertWorkflowOutputs.test.ts | 16 ++++++---------- src/stores/previewExposureStore.test.ts | 2 +- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts b/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts index 25c9c7d857e..11c461677fb 100644 --- a/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts +++ b/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts @@ -27,8 +27,8 @@ import { useWidgetValueStore } from '@/stores/widgetValueStore' import { - createLeafNodeLocatorId, - createNodeExecutionId + createNodeExecutionId, + createNodeLocatorId } from '@/types/nodeIdentification' import type { NodeExecutionId, NodeLocatorId } from '@/types/nodeIdentification' import type { NodeId } from '@/types/nodeId' @@ -363,7 +363,7 @@ function widgetNodeLocatorId( // prefix to collide with, so it can be kept whole (comfy-multi-player's // insert_workflow remapped ids, PM-1580) via the leaf-tolerant path. if (subgraphId && bareWidgetId.includes(':')) return undefined - return createLeafNodeLocatorId(subgraphId, bareWidgetId) ?? undefined + return createNodeLocatorId(subgraphId, bareWidgetId) } interface WidgetProcessingContext { diff --git a/src/stores/nodeOutputStore.insertWorkflowOutputs.test.ts b/src/stores/nodeOutputStore.insertWorkflowOutputs.test.ts index 7971535f987..1c85f234939 100644 --- a/src/stores/nodeOutputStore.insertWorkflowOutputs.test.ts +++ b/src/stores/nodeOutputStore.insertWorkflowOutputs.test.ts @@ -27,11 +27,9 @@ function addRootSaveImageNode(rawNodeId: string) { const node = new SaveImage() node.id = toNodeId(rawNodeId) graph.add(node) - Object.assign(app, { - rootGraph: graph, - nodeOutputs: {}, - nodePreviewImages: {} - }) + vi.spyOn(app, 'rootGraph', 'get').mockReturnValue(graph) + vi.spyOn(app, 'nodeOutputs', 'get').mockReturnValue({}) + app.nodePreviewImages = {} return node } @@ -58,11 +56,9 @@ describe('nodeOutputStore: outputs for insert_workflow-remapped node ids', () => second.id = toNodeId('insert:0fbd38ecb13037d0b3b0ca78b8a20a5a:root:node:10') graph.add(first) graph.add(second) - Object.assign(app, { - rootGraph: graph, - nodeOutputs: {}, - nodePreviewImages: {} - }) + vi.spyOn(app, 'rootGraph', 'get').mockReturnValue(graph) + vi.spyOn(app, 'nodeOutputs', 'get').mockReturnValue({}) + app.nodePreviewImages = {} setOutputsByRawExecutionId(String(first.id)) diff --git a/src/stores/previewExposureStore.test.ts b/src/stores/previewExposureStore.test.ts index 0ffefade2fb..c9d0000d3e6 100644 --- a/src/stores/previewExposureStore.test.ts +++ b/src/stores/previewExposureStore.test.ts @@ -16,7 +16,7 @@ describe(getPreviewExposureHostLocator, () => { graph: null, id: toNodeId('invalid:id') }) - const error = vi.spyOn(console, 'error').mockImplementation(() => {}) + vi.spyOn(console, 'error').mockImplementation(() => {}) expect(getPreviewExposureHostLocator(host)).toBe('~root:invalid%3Aid') expect(console.error).not.toHaveBeenCalled()