From 99148a34dba5800af6bef5546660d3c80b6dd343 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 06:47:42 -0700 Subject: [PATCH 01/18] fix(litegraph): recover graph ID allocation safely --- eslint-suppressions.json | 5 - src/composables/useCopy.test.ts | 2 +- src/composables/useCopy.ts | 2 +- src/composables/usePaste.test.ts | 115 +++- src/composables/usePaste.ts | 24 +- src/extensions/core/groupNode.ts | 10 +- src/extensions/core/rerouteNode.ts | 3 +- src/extensions/core/widgetInputs.ts | 3 +- src/lib/litegraph/src/LGraph.test.ts | 20 +- src/lib/litegraph/src/LGraph.ts | 89 ++- .../src/LGraphCanvas.clipboard.test.ts | 165 ++--- src/lib/litegraph/src/LGraphCanvas.ts | 612 ++++++++---------- src/lib/litegraph/src/LGraphNode.ts | 7 +- ...ceExhausted.ts => nodeIdsAtFormerLimit.ts} | 10 +- .../litegraph/src/canvas/linkBadgeRenderer.ts | 3 +- .../litegraph/src/canvas/linkBadges.test.ts | 1 + src/lib/litegraph/src/canvas/linkBadges.ts | 3 +- .../src/idAllocation.property.test.ts | 14 +- src/lib/litegraph/src/idAllocation.test.ts | 141 +++- src/lib/litegraph/src/idAllocation.ts | 91 ++- src/lib/litegraph/src/interfaces.ts | 6 +- .../litegraph/src/subgraph/SubgraphInput.ts | 89 ++- .../src/subgraph/SubgraphInputNode.ts | 5 +- .../litegraph/src/subgraph/SubgraphOutput.ts | 5 +- .../subgraph/subgraphDeduplication.test.ts | 69 ++ .../src/subgraph/subgraphDeduplication.ts | 97 +-- src/lib/litegraph/src/utils/type.ts | 9 +- .../validation/schemas/workflowSchema.test.ts | 31 +- .../validation/schemas/workflowSchema.ts | 124 +++- src/stores/entityIdStore.test.ts | 6 +- .../extensions/agent/crdt/linkMintPort.ts | 3 +- 31 files changed, 1118 insertions(+), 646 deletions(-) rename src/lib/litegraph/src/__fixtures__/{nodeIdSpaceExhausted.ts => nodeIdsAtFormerLimit.ts} (90%) diff --git a/eslint-suppressions.json b/eslint-suppressions.json index d4e305a77ff..519df1b3f6c 100644 --- a/eslint-suppressions.json +++ b/eslint-suppressions.json @@ -184,11 +184,6 @@ "count": 1 } }, - "src/lib/litegraph/src/subgraph/subgraphDeduplication.ts": { - "comfy/no-new-error-throw": { - "count": 1 - } - }, "src/platform/assets/services/assetService.ts": { "comfy/no-new-error-throw": { "count": 21 diff --git a/src/composables/useCopy.test.ts b/src/composables/useCopy.test.ts index 5c468bd0cfc..a2da17daf9e 100644 --- a/src/composables/useCopy.test.ts +++ b/src/composables/useCopy.test.ts @@ -64,7 +64,7 @@ function selectDocumentText(selectedCharacters: number): void { function readSerializedClipboardMetadata(dataTransfer: DataTransfer): string { const match = dataTransfer .getData('text/html') - .match(/data-metadata="([A-Za-z0-9+/=]+)"/)?.[1] + .match(/data-comfy-metadata="([A-Za-z0-9+/=]+)"/)?.[1] expect(match).toBeDefined() if (!match) throw new Error('Expected clipboard metadata to be written') diff --git a/src/composables/useCopy.ts b/src/composables/useCopy.ts index 9825791f784..91470065a2b 100644 --- a/src/composables/useCopy.ts +++ b/src/composables/useCopy.ts @@ -10,7 +10,7 @@ const CANVAS_CLIPBOARD_KEY = 'litegrapheditor_clipboard' const CANVAS_CLIPBOARD_ID_KEY = 'litegrapheditor_clipboard_id' const clipboardHTMLWrapper = [ - '
Text' ] const clipboardByteChunkSize = 0x8000 diff --git a/src/composables/usePaste.test.ts b/src/composables/usePaste.test.ts index 347a9d1cdcc..3da44835afb 100644 --- a/src/composables/usePaste.test.ts +++ b/src/composables/usePaste.test.ts @@ -18,6 +18,7 @@ import type { LGraphNode } from '@/lib/litegraph/src/litegraph' import { app } from '@/scripts/app' +import { useToastStore } from '@/platform/updates/common/toastStore' import { createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils' import { createNode } from '@/utils/litegraphUtil' import { shouldIgnoreCopyPaste } from '@/workbench/eventHelpers' @@ -679,7 +680,34 @@ describe('usePaste', () => { }) it('should handle clipboard items with metadata', async () => { - const data = { test: 'data' } + const data = { + nodes: [], + groups: [], + reroutes: [], + links: [], + subgraphs: [] + } + const encoded = btoa(JSON.stringify(data)) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + + const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) + document.dispatchEvent(event) + + await vi.waitFor(() => { + expect(mockCanvas._deserializeItems).toHaveBeenCalledWith( + data, + expect.any(Object) + ) + }) + }) + + it('accepts validated legacy data-metadata clipboard items', async () => { + const data = { nodes: [] } const encoded = btoa(JSON.stringify(data)) const html = `
` @@ -687,6 +715,7 @@ describe('usePaste', () => { const dataTransfer = new DataTransfer() dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'some text') const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) document.dispatchEvent(event) @@ -699,6 +728,88 @@ describe('usePaste', () => { }) }) + it.for([ + { name: 'null payload', data: null }, + { name: 'empty object', data: {} }, + { name: 'malformed node', data: { nodes: [{ type: 'KSampler' }] } }, + { name: 'malformed group', data: { groups: [{ id: 1 }] } }, + { name: 'malformed reroute', data: { reroutes: [{ id: 1 }] } }, + { name: 'malformed link', data: { links: [{ id: 1 }] } }, + { name: 'malformed subgraph', data: { subgraphs: [{ id: 'invalid' }] } } + ])('falls back for malformed Comfy metadata: $name', async ({ data }) => { + const encoded = btoa(JSON.stringify(data)) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'some text') + + document.dispatchEvent( + new ClipboardEvent('paste', { clipboardData: dataTransfer }) + ) + + await vi.waitFor(() => { + expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() + expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + }) + }) + + it('falls back when Comfy metadata is not valid JSON', async () => { + const parseError = vi.spyOn(console, 'error').mockImplementation(() => {}) + onTestFinished(() => parseError.mockRestore()) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'some text') + + document.dispatchEvent( + new ClipboardEvent('paste', { clipboardData: dataTransfer }) + ) + + await vi.waitFor(() => { + expect(parseError).toHaveBeenCalledOnce() + expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() + expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + }) + }) + + it('should toast a deserialization error without falling back', async () => { + const deserializeError = new Error('Paste failed') + vi.mocked(mockCanvas._deserializeItems).mockImplementation(() => { + throw deserializeError + }) + const data = { + nodes: [], + groups: [], + reroutes: [], + links: [], + subgraphs: [] + } + const encoded = btoa(JSON.stringify(data)) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'some text') + + const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) + document.dispatchEvent(event) + + await vi.waitFor(() => { + expect(useToastStore().add).toHaveBeenCalledWith( + expect.objectContaining({ severity: 'error' }) + ) + expect(mockCanvas.pasteFromClipboard).not.toHaveBeenCalled() + }) + }) + it('should skip node metadata paste when a media node is selected', async () => { const mockNode = createMockLGraphNode({ is_selected: true, @@ -712,7 +823,7 @@ describe('usePaste', () => { const nodeData = { nodes: [{ type: 'KSampler' }] } const encoded = btoa(JSON.stringify(nodeData)) - const html = `
` + const html = `
` const dataTransfer = new DataTransfer() dataTransfer.setData('text/html', html) diff --git a/src/composables/usePaste.ts b/src/composables/usePaste.ts index 34538f28774..5fa58bee414 100644 --- a/src/composables/usePaste.ts +++ b/src/composables/usePaste.ts @@ -1,6 +1,8 @@ import { useEventListener } from '@vueuse/core' +import { useErrorHandling } from '@/composables/useErrorHandling' import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' +import { zClipboardItems } from '@/platform/workflow/validation/schemas/workflowSchema' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { app } from '@/scripts/app' import { useWorkspaceStore } from '@/stores/workspaceStore' @@ -42,19 +44,31 @@ export function cloneDataTransfer(original: DataTransfer): DataTransfer { function pasteClipboardItems(data: DataTransfer): boolean { const rawData = data.getData('text/html') - const match = rawData.match(/data-metadata="([A-Za-z0-9+/=]+)"/)?.[1] + const match = rawData.match( + /data-(?:comfy-)?metadata="([A-Za-z0-9+/=]+)"/ + )?.[1] if (!match) return false + + let parsed: unknown try { - // Decode UTF-8 safe base64 const binaryString = atob(match) const bytes = Uint8Array.from(binaryString, (c) => c.charCodeAt(0)) const decodedData = new TextDecoder().decode(bytes) - useCanvasStore().getCanvas()._deserializeItems(JSON.parse(decodedData), {}) - return true + parsed = JSON.parse(decodedData) } catch (err) { console.error(err) + return false + } + + const clipboardItems = zClipboardItems.safeParse(parsed) + if (!clipboardItems.success) return false + + try { + useCanvasStore().getCanvas()._deserializeItems(clipboardItems.data, {}) + } catch (err) { + useErrorHandling().toastErrorHandler(err) } - return false + return true } function isWorkflow( diff --git a/src/extensions/core/groupNode.ts b/src/extensions/core/groupNode.ts index d0247e942d2..f01dcb8fd54 100644 --- a/src/extensions/core/groupNode.ts +++ b/src/extensions/core/groupNode.ts @@ -8,6 +8,7 @@ import type { } from '@/lib/litegraph/src/litegraph' import { LiteGraph } from '@/lib/litegraph/src/litegraph' import { outputLinks } from '@/lib/litegraph/src/node/slotLinks' +import { slotTypeKey } from '@/lib/litegraph/src/utils/type' import type { SerializedNodeId } from '@/types/nodeId' import { parseNodeId } from '@/types/nodeId' import type { @@ -229,7 +230,9 @@ export class GroupNodeConfig { if (!linksFrom) return const firstLink = linksFrom[0]?.at(0) - let type: string | number | null = firstLink?.[5] ?? null + let type: string | number | null = firstLink + ? slotTypeKey(firstLink[5]) + : null if (type === 'COMBO') { // Use the array items const output = node.outputs?.[0] @@ -326,13 +329,14 @@ export class GroupNodeConfig { } config.forceInput = true + const rerouteTypeKey = slotTypeKey(rerouteType) return { input: { required: { - [rerouteType]: [rerouteType, config] + [rerouteTypeKey]: [rerouteTypeKey, config] } }, - output: [rerouteType], + output: [rerouteTypeKey], output_name: [], output_is_list: [] } diff --git a/src/extensions/core/rerouteNode.ts b/src/extensions/core/rerouteNode.ts index 93b24540ef4..fef9732f12f 100644 --- a/src/extensions/core/rerouteNode.ts +++ b/src/extensions/core/rerouteNode.ts @@ -6,6 +6,7 @@ import { } from '@/lib/litegraph/src/litegraph' import type { ISlotType } from '@/lib/litegraph/src/interfaces' import { outputLinks } from '@/lib/litegraph/src/node/slotLinks' +import { slotTypeKey } from '@/lib/litegraph/src/utils/type' import { app } from '../../scripts/app' import { getWidgetConfig, mergeIfValid, setWidgetConfig } from './widgetInputs' @@ -115,7 +116,7 @@ app.registerExtension({ } } - const displayType = inputType || outputType || '*' + const displayType = slotTypeKey(inputType || outputType || '*') const color = LGraphCanvas.link_type_colors[displayType] let widgetConfig diff --git a/src/extensions/core/widgetInputs.ts b/src/extensions/core/widgetInputs.ts index c8251728cff..1b071d9f270 100644 --- a/src/extensions/core/widgetInputs.ts +++ b/src/extensions/core/widgetInputs.ts @@ -13,6 +13,7 @@ import type { import type { IWidgetLocator } from '@/lib/litegraph/src/interfaces' import { NodeSlot } from '@/lib/litegraph/src/node/NodeSlot' import { outputHasLinks, outputLinks } from '@/lib/litegraph/src/node/slotLinks' +import { slotTypeKey } from '@/lib/litegraph/src/utils/type' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { assetService } from '@/platform/assets/services/assetService' import { createAssetWidget } from '@/platform/assets/utils/createAssetWidget' @@ -196,7 +197,7 @@ export class PrimitiveNode extends LGraphNode { ) { // Fires before the link is made allowing us to reject it if it isn't valid // No widget, we can't connect - if (!input.widget && !(input.type in ComfyWidgets)) { + if (!input.widget && !(slotTypeKey(input.type) in ComfyWidgets)) { return false } diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index d5a29e8c355..3620379fb32 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -57,7 +57,7 @@ import { } from './__fixtures__/duplicateLinks' import { duplicateSubgraphNodeIds } from './__fixtures__/duplicateSubgraphNodeIds' import { nestedSubgraphProxyWidgets } from './__fixtures__/nestedSubgraphProxyWidgets' -import { nodeIdSpaceExhausted } from './__fixtures__/nodeIdSpaceExhausted' +import { nodeIdsAtFormerLimit } from './__fixtures__/nodeIdsAtFormerLimit' import { uniqueSubgraphNodeIds } from './__fixtures__/uniqueSubgraphNodeIds' import { test } from './__fixtures__/testExtensions' @@ -2347,11 +2347,19 @@ describe('deduplicateSubgraphNodeIds (via configure)', () => { }) }) - it('throws when node ID space is exhausted', () => { - expect(() => { - const graph = new LGraph() - graph.configure(structuredClone(nodeIdSpaceExhausted)) - }).toThrow('Node ID space exhausted') + it('remaps duplicate node IDs above the former fixed limit', () => { + const graph = new LGraph() + + graph.configure(structuredClone(nodeIdsAtFormerLimit)) + + expect(nodeIdSet(graph, SUBGRAPH_B)).toEqual( + new Set([ + toNodeId(100_000_001), + toNodeId(100_000_002), + toNodeId(100_000_003) + ]) + ) + expect(graph.state.lastNodeId).toBe(100_000_003) }) it('is a no-op when subgraph node IDs are already unique', () => { diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 0263ab1dd5f..fae0e01fea7 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -65,6 +65,7 @@ import { useEntityIdStore } from '@/stores/entityIdStore' import { useExecutionOrderStore } from '@/stores/executionOrderStore' import { useGraphMetadataStore } from '@/stores/graphMetadataStore' import { rekeyGraphId } from '@/stores/rekeyGraphId' +import { toGroupId } from '@/types/groupId' import { UNASSIGNED_NODE_ID, compareNodeIds, @@ -186,6 +187,7 @@ import { import { collectReservedGroupIds, collectReservedLinkIds, + collectReservedNodeIds, collectReservedRerouteIds, normalizeSubgraphDefinitions, topologicalSortSubgraphs @@ -1373,7 +1375,9 @@ export class LGraph groupId === -1 || layoutStore.getGroupLayout(this.rootGraph.id, groupId) ) { - node.id = mintGroupId(state) + node.id = mintGroupId(state, () => + collectReservedGroupIds(this.rootGraph) + ) } observeGroupId(state, node.id) @@ -1405,7 +1409,16 @@ export class LGraph // give him an id if (node.id === UNASSIGNED_NODE_ID) { const mintMode = nodeIdMintModeFor(this) - node.id = mintNodeId(state, mintMode) + node.id = mintNodeId( + state, + mintMode, + () => + new Set( + [...collectReservedNodeIds(this.rootGraph)] + .map(Number) + .filter(Number.isSafeInteger) + ) + ) } else { observeNodeId(state, node.id) } @@ -1420,7 +1433,16 @@ export class LGraph node.graph = this attachNodeToStores(this, node, () => - mintNodeId(state, nodeIdMintModeFor(this)) + mintNodeId( + state, + nodeIdMintModeFor(this), + () => + new Set( + [...collectReservedNodeIds(this.rootGraph)] + .map(Number) + .filter(Number.isSafeInteger) + ) + ) ) this._nodes.push(node) @@ -1877,7 +1899,9 @@ export class LGraph addFloatingLink(link: LLink): LLink | undefined { if (link.id === -1) { - link.id = mintLinkId(this.state) + link.id = mintLinkId(this.state, () => + collectReservedLinkIds(this.rootGraph) + ) } if (!registerLinkTopology(this, link)) return @@ -1984,7 +2008,11 @@ export class LGraph floating }: OptionalProps): Reroute | undefined { const rerouteId = - id === undefined ? mintRerouteId(this.state) : toRerouteId(id) + id === undefined + ? mintRerouteId(this.state, () => + collectReservedRerouteIds(this.rootGraph) + ) + : toRerouteId(id) observeRerouteId(this.state, rerouteId) const existingReroute = this.reroutes.get(rerouteId) @@ -2118,34 +2146,27 @@ export class LGraph return this.createSubgraphs([data])[0] } - createSubgraphs(data: ExportedSubgraph[]): Subgraph[] { + createSubgraphs( + data: ExportedSubgraph[], + rootNodes?: ISerialisedNode[] + ): Subgraph[] { if (!data.length) return [] const normalized = normalizeSubgraphDefinitions( data, { - nodeIds: this.collectReservedNodeIds(), + nodeIds: collectReservedNodeIds(this.rootGraph), groupIds: collectReservedGroupIds(this.rootGraph), linkIds: collectReservedLinkIds(this.rootGraph), rerouteIds: collectReservedRerouteIds(this.rootGraph) }, - this.state - ).subgraphs - return this.createNormalizedSubgraphs(normalized) - } - - private collectReservedNodeIds( - rootNodes: ISerialisedNode[] = [] - ): Set { - const reserved = new Set() - for (const owner of [ - this.rootGraph, - ...this.rootGraph.subgraphs.values() - ]) { - for (const node of owner.nodes) reserved.add(node.id) + this.state, + rootNodes + ) + if (rootNodes && normalized.rootNodes) { + rootNodes.splice(0, rootNodes.length, ...normalized.rootNodes) } - for (const node of rootNodes) reserved.add(toNodeId(node.id)) - return reserved + return this.createNormalizedSubgraphs(normalized.subgraphs) } private createNormalizedSubgraphs(data: ExportedSubgraph[]): Subgraph[] { @@ -2703,7 +2724,9 @@ export class LGraph // Shared definitions may survive, so unpacked groups need fresh layout // ids, like the reroutes below. for (const groupInfo of groups) { - const groupId = mintGroupId(this.rootGraph.state) + const groupId = mintGroupId(this.rootGraph.state, () => + collectReservedGroupIds(this.rootGraph) + ) groupInfo.id = groupId const group = new LGraphGroup(groupInfo.title, groupId) this.add(group, true) @@ -2793,7 +2816,9 @@ export class LGraph const rerouteIdMap = new Map() const oldReroutes = subgraphNode.subgraph.reroutes for (const reroute of oldReroutes.values()) { - const migratedId = mintRerouteId(this.state) + const migratedId = mintRerouteId(this.state, () => + collectReservedRerouteIds(this.rootGraph) + ) const migratedReroute = this.setReroute({ id: migratedId, pos: [reroute.pos[0] + offsetX, reroute.pos[1] + offsetY], @@ -3134,17 +3159,13 @@ export class LGraph const runtimeLastNodeId = runtimeOptional(lastNodeId) const runtimeLastRerouteId = runtimeOptional(lastRerouteId) if (runtimeLastGroupId != null) - state.lastGroupId = Math.max(state.lastGroupId, runtimeLastGroupId) + observeGroupId(state, toGroupId(runtimeLastGroupId)) if (runtimeLastLinkId != null) - state.lastLinkId = toLinkId( - Math.max(state.lastLinkId, runtimeLastLinkId) - ) + observeLinkId(state, toLinkId(runtimeLastLinkId)) if (runtimeLastNodeId != null) - state.lastNodeId = Math.max(state.lastNodeId, runtimeLastNodeId) + observeNodeId(state, toNodeId(runtimeLastNodeId)) if (runtimeLastRerouteId != null) - state.lastRerouteId = toRerouteId( - Math.max(state.lastRerouteId, runtimeLastRerouteId) - ) + observeRerouteId(state, toRerouteId(runtimeLastRerouteId)) } // Links @@ -3190,7 +3211,7 @@ export class LGraph ? normalizeSubgraphDefinitions( subgraphs, { - nodeIds: this.collectReservedNodeIds(nodesData), + nodeIds: collectReservedNodeIds(this.rootGraph, nodesData), groupIds: collectReservedGroupIds(this, data.groups), linkIds: collectReservedLinkIds(this, data.floatingLinks), rerouteIds: collectReservedRerouteIds(this) diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index be08aafe128..a141af972bb 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -27,7 +27,7 @@ import { SubgraphNode, createUuidv4 } from '@/lib/litegraph/src/litegraph' -import { remapClipboardSubgraphNodeIds } from '@/lib/litegraph/src/LGraphCanvas' +import { toLinkId } from '@/types/linkId' import { toNodeId } from '@/types/nodeId' import type { ClipboardItems, @@ -63,138 +63,67 @@ function createSerialisedNode( } } -describe('remapClipboardSubgraphNodeIds', () => { - it('remaps pasted subgraph interior IDs and proxyWidgets references', () => { +describe('clipboard ID allocation', () => { + it('wraps at the safe-integer boundary without mutating clipboard data', () => { + const nodeType = 'test/clipboard-id-wrap' + registerClipboardNodeType(nodeType) const rootGraph = new LGraph() - const existingNode = new LGraphNode('existing') - existingNode.id = toNodeId(1) - rootGraph.add(existingNode) - - const subgraphId = createUuidv4() - const pastedSubgraph: ExportedSubgraph = { - id: subgraphId, - version: 1, - revision: 0, - state: { - lastNodeId: 0, - lastLinkId: 0, - lastGroupId: 0, - lastRerouteId: 0 - }, - config: {}, - name: 'Pasted Subgraph', - inputNode: { - id: SUBGRAPH_INPUT_ID, - bounding: [0, 0, 10, 10] - }, - outputNode: { - id: SUBGRAPH_OUTPUT_ID, - bounding: [0, 0, 10, 10] - }, - inputs: [], - outputs: [], - widgets: [], - nodes: [createSerialisedNode(1, 'test/node')], + rootGraph.state.lastNodeId = Number.MAX_SAFE_INTEGER + rootGraph.state.lastGroupId = Number.MAX_SAFE_INTEGER + rootGraph.state.lastLinkId = toLinkId(Number.MAX_SAFE_INTEGER) + rootGraph.state.lastRerouteId = toRerouteId(Number.MAX_SAFE_INTEGER) + const canvas = createCanvas(rootGraph) + const parsed: ClipboardItems = { + nodes: [ + createSerialisedNode(7, nodeType), + createSerialisedNode(8, nodeType) + ], + groups: [{ id: 7, title: 'Group', bounding: [0, 0, 100, 100] }], links: [ { - id: 1, - type: '*', - origin_id: 1, + id: 7, + origin_id: 7, origin_slot: 0, - target_id: 1, - target_slot: 0 + target_id: 8, + target_slot: 0, + type: '*', + parentId: 7 } ], - groups: [] + reroutes: [{ id: 7, pos: [0, 0], linkIds: [7] }] } + const original = structuredClone(parsed) - const parsed: ClipboardItems = { - nodes: [createSerialisedNode(99, subgraphId, [['1', 'seed']])], - groups: [], - reroutes: [], - links: [], - subgraphs: [pastedSubgraph] - } - - remapClipboardSubgraphNodeIds(parsed, rootGraph) + const result = canvas._deserializeItems(parsed, {}) - const remappedSubgraph = parsed.subgraphs?.[0] - expect(remappedSubgraph).toBeDefined() - - const remappedLink = remappedSubgraph?.links?.[0] - expect(remappedLink).toBeDefined() - - const remappedInteriorId = remappedSubgraph?.nodes?.[0]?.id - expect(remappedInteriorId).not.toBe(1) - expect(remappedLink?.origin_id).toBe(remappedInteriorId) - expect(remappedLink?.target_id).toBe(remappedInteriorId) - - const remappedNode = parsed.nodes?.[0] - expect(remappedNode).toBeDefined() - expect(remappedNode?.properties?.proxyWidgets).toStrictEqual([ - [String(remappedInteriorId), 'seed'] + expect(parsed).toEqual(original) + expect(result?.created).toHaveLength(4) + expect(rootGraph.nodes.map((node) => node.id)).toEqual([ + toNodeId(1), + toNodeId(2) ]) + expect(rootGraph.groups[0].id).toBe(1) + expect([...rootGraph.links.keys()]).toEqual([toLinkId(1)]) + expect([...rootGraph.reroutes.keys()]).toEqual([toRerouteId(1)]) }) - it('remaps pasted SubgraphNode previewExposures sourceNodeId references', () => { + it('emits one complete canvas change pair for public paste', () => { + const nodeType = 'test/clipboard-change-hooks' + registerClipboardNodeType(nodeType) const rootGraph = new LGraph() - const existingNode = new LGraphNode('existing') - existingNode.id = toNodeId(1) - rootGraph.add(existingNode) - - const subgraphId = createUuidv4() - const pastedSubgraph: ExportedSubgraph = { - id: subgraphId, - version: 1, - revision: 0, - state: { - lastNodeId: 0, - lastLinkId: 0, - lastGroupId: 0, - lastRerouteId: 0 - }, - config: {}, - name: 'Pasted Subgraph', - inputNode: { id: SUBGRAPH_INPUT_ID, bounding: [0, 0, 10, 10] }, - outputNode: { id: SUBGRAPH_OUTPUT_ID, bounding: [0, 0, 10, 10] }, - inputs: [], - outputs: [], - widgets: [], - nodes: [createSerialisedNode(1, 'test/node')], - links: [], - groups: [] - } - - const hostInfo = createSerialisedNode(99, subgraphId) - hostInfo.properties = { - previewExposures: [ - { - name: '$$canvas-image-preview', - sourceNodeId: '1', - sourcePreviewName: '$$canvas-image-preview' - } - ] - } - - const parsed: ClipboardItems = { - nodes: [hostInfo], - groups: [], - reroutes: [], - links: [], - subgraphs: [pastedSubgraph] - } + const canvas = createCanvas(rootGraph) + const before = vi.spyOn(canvas, 'emitBeforeChange') + const after = vi.spyOn(canvas, 'emitAfterChange') + localStorage.setItem( + 'litegrapheditor_clipboard', + JSON.stringify({ nodes: [createSerialisedNode(1, nodeType)] }) + ) + onTestFinished(() => localStorage.removeItem('litegrapheditor_clipboard')) - remapClipboardSubgraphNodeIds(parsed, rootGraph) + canvas.pasteFromClipboard() - const remappedInteriorId = parsed.subgraphs?.[0]?.nodes?.[0]?.id - expect(remappedInteriorId).not.toBe(1) - expect(parsed.nodes?.[0]?.properties?.previewExposures).toStrictEqual([ - { - name: '$$canvas-image-preview', - sourceNodeId: String(remappedInteriorId), - sourcePreviewName: '$$canvas-image-preview' - } - ]) + expect(before).toHaveBeenCalledOnce() + expect(after).toHaveBeenCalledOnce() }) }) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 38aa610f504..3691db9e0fb 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -48,14 +48,13 @@ import type { ContextMenu } from './ContextMenu' import { createCursorCache } from './cursorCache' import { DragAndScale } from './DragAndScale' import type { AnimationOptions } from './DragAndScale' -import { mintNodeId, observeNodeId } from './idAllocation' import type { LGraph, SubgraphId } from './LGraph' import { LGraphGroup } from './LGraphGroup' import type { SlotTypeDefaultNodeOpts } from './LiteGraphGlobal' import { LGraphNode } from './LGraphNode' import type { NodeProperty } from './LGraphNode' import { detachSerialisedLinks } from './linkDeduplication' -import { parseNodeId, serializeNodeId, toNodeId } from '@/types/nodeId' +import { parseNodeId, serializeNodeId } from '@/types/nodeId' import type { SerializedNodeId } from '@/types/nodeId' import { LLink, slotFloatingLinks } from './LLink' import { inputHasLink, nodeLinkIds, outputLinks } from './node/slotLinks' @@ -169,6 +168,7 @@ import type { IBaseWidget, TWidgetValue } from './types/widgets' import { alignNodes, distributeNodes, getBoundaryNodes } from './utils/arrange' import { findFirstNode, getDraggedItems } from './utils/collections' import { resolveConnectingLinkColor } from './utils/linkColors' +import { slotTypeKey } from './utils/type' import { createUuidv4 } from '@/utils/uuid' import { BaseWidget } from './widgets/BaseWidget' import { toConcreteWidget } from './widgets/widgetMap' @@ -287,6 +287,20 @@ interface ClipboardPasteResult { subgraphs: Map } +type InitializedClipboardItems = { + [Key in keyof ClipboardItems]-?: NonNullable +} + +interface ClipboardPasteContext { + connectInputs: boolean + dx: number + dy: number + graph: LGraph + items: InitializedClipboardItems + result: ClipboardPasteResult + targetSlotByLink: Map +} + /** Legacy selection views derived from the selection store. */ interface SelectionView { items: SelectedItemsView @@ -4257,7 +4271,7 @@ export class LGraphCanvas implements CustomEventDispatcher } _deserializeItems( - parsed: ClipboardItems, + clipboardItems: ClipboardItems, options: IPasteFromClipboardOptions ): ClipboardPasteResult | undefined { const { connectInputs = false, position = this.graph_mouse } = options @@ -4274,231 +4288,45 @@ export class LGraphCanvas implements CustomEventDispatcher graph.beforeChange() this.emitBeforeChange() - // Parse & initialise - parsed.nodes ??= [] - parsed.groups ??= [] - parsed.reroutes ??= [] - parsed.links ??= [] - parsed.subgraphs ??= [] - - // Find top-left-most boundary - let offsetX = Infinity - let offsetY = Infinity - for (const item of [...parsed.nodes, ...parsed.reroutes]) { - if (item.pos[0] < offsetX) offsetX = item.pos[0] - if (item.pos[1] < offsetY) offsetY = item.pos[1] - } - - // TODO: Remove when implementing `asSerialisable` - for (const group of parsed.groups) { - if (group.bounding[0] < offsetX) offsetX = group.bounding[0] - if (group.bounding[1] < offsetY) offsetY = group.bounding[1] - } - - const results: ClipboardPasteResult = { - created: [], - nodes: new Map(), - links: new Map(), - reroutes: new Map(), - subgraphs: new Map() - } - const { created, nodes, links, reroutes } = results - - // const failedNodes: ISerialisedNode[] = [] - const subgraphIdMap: Record = {} - // SubgraphV2: Remove always-clone behaviour - //Update subgraph ids - for (const subgraphInfo of parsed.subgraphs) - subgraphInfo.id = subgraphIdMap[subgraphInfo.id] = createUuidv4() - const allNodeInfo: ISerialisedNode[] = [ - [parsed.nodes], - parsed.subgraphs.map((s) => s.nodes ?? []) - ].flat(2) - for (const nodeInfo of allNodeInfo) - if (nodeInfo.type in subgraphIdMap) - nodeInfo.type = subgraphIdMap[nodeInfo.type] - remapClipboardSubgraphNodeIds(parsed, graph.rootGraph) - // Subgraphs - const subgraphs = graph.createSubgraphs(parsed.subgraphs) - for (const subgraph of subgraphs) - results.subgraphs.set(subgraph.id, subgraph) - - // Groups - for (const info of parsed.groups) { - info.id = -1 - - const group = new LGraphGroup() - group.configure(info) - graph.add(group) - created.push(group) - } - - // Nodes - const dx = position[0] - offsetX - const dy = position[1] - offsetY - const targetSlotByLink = new Map() - for (const info of parsed.nodes) { - const node = LiteGraph.createNode(info.type) - if (!node) { - // failedNodes.push(info) - continue - } - - nodes.set(serializeNodeId(info.id), node) - info.id = -1 - - const linkByInputName = detachSerialisedLinks(info) - // `add` snapshots the position into the layout store; configure runs after. - node.pos = [info.pos[0] + dx, info.pos[1] + dy] - graph.add(node) - node.configure(info) - - // `configure` overrides may reorder inputs to match the node definition, - // moving the slot each serialized link targets. - for (const [slot, input] of (info.inputs ?? []).entries()) { - const linkId = linkByInputName.get(input.name) - if (linkId != null && !targetSlotByLink.has(linkId)) - targetSlotByLink.set(linkId, slot) - } - - if (node instanceof SubgraphNode) { - if ( - node.properties.proxyWidgets !== undefined && - LiteGraph.LGraph.proxyWidgetMigrationFlush - ) { - LiteGraph.LGraph.proxyWidgetMigrationFlush(node, info) - } - LiteGraph.LGraph.autoExposePreviewNodes?.(node) - } - - created.push(node) - } - - // Reroutes - for (const info of parsed.reroutes) { - const { id, ...rerouteInfo } = info - - const reroute = graph.setReroute(rerouteInfo) - if (!reroute) continue - created.push(reroute) - reroutes.set(toRerouteId(id), reroute) - } - - // Remap reroute parentIds for pasted reroutes - for (const reroute of reroutes.values()) { - if (reroute.parentId == null) continue - - const mapped = reroutes.get(reroute.parentId) - if (mapped) reroute.parentId = mapped.id - } - - // Links - for (const info of parsed.links) { - // Find the copied node / reroute ID - let outNode: LGraphNode | null | undefined = nodes.get( - serializeNodeId(info.origin_id) - ) - let afterRerouteId: RerouteId | undefined - if (info.parentId != null) - afterRerouteId = reroutes.get(toRerouteId(info.parentId))?.id - - // If it wasn't copied, use the original graph value - if ( - connectInputs && - LiteGraph.ctrl_shift_v_paste_connect_unselected_outputs - ) { - const originNodeId = parseNodeId(info.origin_id) - outNode ??= originNodeId ? graph.getNodeById(originNodeId) : null - if (info.parentId !== undefined) { - afterRerouteId ??= toRerouteId(info.parentId) - } - } - - const inNode = nodes.get(serializeNodeId(info.target_id)) - if (inNode) { - const link = outNode?.connect( - info.origin_slot, - inNode, - targetSlotByLink.get(toLinkId(info.id)) ?? info.target_slot, - afterRerouteId - ) - if (link) { - transferLinkPresentation( - graphScopeOf(graph), - { - hidden: info.hidden === true, - label: typeof info.label === 'string' ? info.label : undefined - }, - link.id - ) - links.set(toLinkId(info.id), link) - } - } - } - - // Remove reroutes that no pasted link passes through - for (const [sourceId, reroute] of reroutes) { - if (reroute.totalLinks === 0) { - graph.removeReroute(reroute.id) - reroutes.delete(sourceId) - - const index = created.indexOf(reroute) - if (index !== -1) created.splice(index, 1) - } - } - - // Children of pasted groups are in `created` already, so skip them here. - for (const item of created) { - // Repositioning a paste is not a user drag, so it ignores the pin. - if (item instanceof LGraphNode) - item.setPos(item.pos[0] + dx, item.pos[1] + dy) - else item.move(dx, dy, true) - } - - // TODO: Report failures, i.e. `failedNodes` - - const newPositions = created - .filter((item): item is LGraphNode => item instanceof LGraphNode) - .map((node) => ({ - nodeId: node.id, - bounds: { - x: node.pos[0], - y: node.pos[1], - width: node.size[0], - height: node.size[1] - } - })) - - const rootGraphId = graph.rootGraph.id - layoutStore.batchUpdateNodeBounds(rootGraphId, newPositions, { - source: LayoutSource.Canvas - }) - - // Bring cloned/pasted nodes to front so they render above the originals - const { setNodeZIndex } = useLayoutMutations(LayoutSource.Canvas) - for (const { nodeId } of newPositions) { - setNodeZIndex(rootGraphId, nodeId, layoutStore.allocateZIndex()) - } - - this.selectItems(created) - forEachNode(graph, (n) => n.onGraphConfigured?.()) - forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) - - graph.afterChange() - this.emitAfterChange() - - return results - } - - pasteFromClipboard(options: IPasteFromClipboardOptions = {}): void { - this.emitBeforeChange() try { - this._pasteFromClipboard(options) + const items = initializeClipboardItems(clipboardItems) + const [offsetX, offsetY] = clipboardOffset(items) + const result = createClipboardPasteResult() + const context: ClipboardPasteContext = { + connectInputs, + dx: position[0] - offsetX, + dy: position[1] - offsetY, + graph, + items, + result, + targetSlotByLink: new Map() + } + + remapClipboardSubgraphIds(items) + createClipboardSubgraphs(context) + createClipboardGroups(context) + createClipboardNodes(context) + createClipboardReroutes(context) + createClipboardLinks(context) + removeUnusedClipboardReroutes(context) + positionClipboardItems(context) + updateClipboardNodeLayout(context) + + this.selectItems(result.created) + forEachNode(graph, (n) => n.onGraphConfigured?.()) + forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) + + return result } finally { + graph.afterChange() this.emitAfterChange() } } + pasteFromClipboard(options: IPasteFromClipboardOptions = {}): void { + this._pasteFromClipboard(options) + } + processNodeDblClicked(n: LGraphNode): void { this.onShowNodePanel?.(n) this.onNodeDblClicked?.(n) @@ -6388,7 +6216,7 @@ export class LGraphCanvas implements CustomEventDispatcher visibleReroutes.push(reroute) reroute._colour = link.color || - LGraphCanvas.link_type_colors[link.type] || + LGraphCanvas.link_type_colors[slotTypeKey(link.type)] || this.default_link_color } @@ -6901,7 +6729,8 @@ export class LGraphCanvas implements CustomEventDispatcher } // check for defaults nodes for this slottype - const fromSlotType = slotX.type == LiteGraph.EVENT ? '_event_' : slotX.type + const fromSlotType = + slotX.type == LiteGraph.EVENT ? '_event_' : slotTypeKey(slotX.type) const slotTypesDefault = isFrom ? LiteGraph.slot_types_default_out : LiteGraph.slot_types_default_in @@ -7094,7 +6923,8 @@ export class LGraphCanvas implements CustomEventDispatcher } // get defaults nodes for this slottype - const fromSlotType = slotX.type == LiteGraph.EVENT ? '_event_' : slotX.type + const fromSlotType = + slotX.type == LiteGraph.EVENT ? '_event_' : slotTypeKey(slotX.type) const slotTypesDefault = isFrom ? LiteGraph.slot_types_default_out : LiteGraph.slot_types_default_in @@ -8964,157 +8794,255 @@ export class LGraphCanvas implements CustomEventDispatcher } } -export interface LGraphCanvas { - /** @deprecated Use {@link LGraphCanvas.applyNodePositions} instead. */ - repositionNodesVueMode(positions: NewNodePosition[]): void +function initializeClipboardItems( + clipboardItems: ClipboardItems +): InitializedClipboardItems { + const items = structuredClone(clipboardItems) + return { + groups: items.groups ?? [], + links: items.links ?? [], + nodes: items.nodes ?? [], + reroutes: items.reroutes ?? [], + subgraphs: items.subgraphs ?? [] + } } -defineDeprecatedProperty( - LGraphCanvas.prototype, - 'repositionNodesVueMode', - 'applyNodePositions', - 'LGraphCanvas.repositionNodesVueMode is deprecated. Use applyNodePositions instead.' -) - -function patchLinkNodeIds( - links: - | { origin_id: SerializedNodeId; target_id: SerializedNodeId }[] - | undefined, - remappedIds: Map -) { - if (!links?.length) return - - for (const link of links) { - const newOriginId = remappedIds.get(link.origin_id) - if (newOriginId !== undefined) link.origin_id = newOriginId - - const newTargetId = remappedIds.get(link.target_id) - if (newTargetId !== undefined) link.target_id = newTargetId +function clipboardOffset(items: InitializedClipboardItems): Point { + let x = Infinity + let y = Infinity + for (const item of [...items.nodes, ...items.reroutes]) { + x = Math.min(x, item.pos[0]) + y = Math.min(y, item.pos[1]) + } + for (const group of items.groups) { + x = Math.min(x, group.bounding[0]) + y = Math.min(y, group.bounding[1]) } + return [x, y] } -function remapNodeId( - nodeId: string, - remappedIds: Map -): SerializedNodeId | undefined { - const directMatch = remappedIds.get(nodeId) - if (directMatch !== undefined) return directMatch - if (!/^-?\d+$/.test(nodeId)) return undefined +function createClipboardPasteResult(): ClipboardPasteResult { + return { + created: [], + links: new Map(), + nodes: new Map(), + reroutes: new Map(), + subgraphs: new Map() + } +} - const numericId = Number(nodeId) - if (!Number.isSafeInteger(numericId)) return undefined +function remapClipboardSubgraphIds(items: InitializedClipboardItems): void { + const remappedIds = new Map() + for (const subgraph of items.subgraphs) { + const nextId = createUuidv4() + remappedIds.set(subgraph.id, nextId) + subgraph.id = nextId + } - return remappedIds.get(numericId) + const allNodes = [ + ...items.nodes, + ...items.subgraphs.flatMap((subgraph) => subgraph.nodes ?? []) + ] + for (const node of allNodes) { + const nextType = remappedIds.get(node.type) + if (nextType) node.type = nextType + } } -function remapProxyWidgets( - info: ISerialisedNode, - remappedIds: Map | undefined -) { - if (!remappedIds || remappedIds.size === 0) return +function createClipboardSubgraphs(context: ClipboardPasteContext): void { + const { graph, items, result } = context + for (const subgraph of graph.createSubgraphs(items.subgraphs, items.nodes)) { + result.subgraphs.set(subgraph.id, subgraph) + } +} - const proxyWidgets = info.properties?.proxyWidgets - if (!Array.isArray(proxyWidgets)) return +function createClipboardGroups(context: ClipboardPasteContext): void { + const { graph, items, result } = context + for (const info of items.groups) { + info.id = -1 + const group = new LGraphGroup() + group.configure(info) + graph.add(group) + result.created.push(group) + } +} - for (const entry of proxyWidgets) { - if (!Array.isArray(entry)) continue +function createClipboardNodes(context: ClipboardPasteContext): void { + const { dx, dy, graph, items, result, targetSlotByLink } = context + for (const info of items.nodes) { + const node = LiteGraph.createNode(info.type) + if (!node) continue - const [nodeId] = entry - if (typeof nodeId !== 'string' || nodeId === '-1') continue + result.nodes.set(serializeNodeId(info.id), node) + info.id = -1 + const linkByInputName = detachSerialisedLinks(info) + node.pos = [info.pos[0] + dx, info.pos[1] + dy] + graph.add(node) + node.configure(info) - const remappedNodeId = remapNodeId(nodeId, remappedIds) - if (remappedNodeId !== undefined) entry[0] = String(remappedNodeId) + recordClipboardTargetSlots(info, linkByInputName, targetSlotByLink) + configurePastedSubgraphNode(node, info) + result.created.push(node) } } -function hasStringSourceNodeId( - value: unknown -): value is { sourceNodeId: string } { - return ( - typeof value === 'object' && - value !== null && - 'sourceNodeId' in value && - typeof value.sourceNodeId === 'string' - ) +function recordClipboardTargetSlots( + info: ISerialisedNode, + linkByInputName: Map, + targetSlotByLink: Map +): void { + for (const [slot, input] of (info.inputs ?? []).entries()) { + const linkId = linkByInputName.get(input.name) + if (linkId != null && !targetSlotByLink.has(linkId)) { + targetSlotByLink.set(linkId, slot) + } + } } -function remapPreviewExposures( - info: ISerialisedNode, - remappedIds: Map | undefined -) { - if (!remappedIds || remappedIds.size === 0) return +function configurePastedSubgraphNode( + node: LGraphNode, + info: ISerialisedNode +): void { + if (!(node instanceof SubgraphNode)) return - const previewExposures = info.properties?.previewExposures - if (!Array.isArray(previewExposures)) return + const flushProxyWidgets = LiteGraph.LGraph.proxyWidgetMigrationFlush + if (node.properties.proxyWidgets !== undefined && flushProxyWidgets) { + flushProxyWidgets(node, info) + } + LiteGraph.LGraph.autoExposePreviewNodes?.(node) +} - for (const entry of previewExposures) { - if (!hasStringSourceNodeId(entry) || entry.sourceNodeId === '-1') continue +function createClipboardReroutes(context: ClipboardPasteContext): void { + const { graph, items, result } = context + for (const info of items.reroutes) { + const { id, ...rerouteInfo } = info + const reroute = graph.setReroute(rerouteInfo) + if (!reroute) continue + result.created.push(reroute) + result.reroutes.set(toRerouteId(id), reroute) + } - const remappedNodeId = remapNodeId(entry.sourceNodeId, remappedIds) - if (remappedNodeId !== undefined) - entry.sourceNodeId = String(remappedNodeId) + for (const reroute of result.reroutes.values()) { + if (reroute.parentId == null) continue + const parent = result.reroutes.get(reroute.parentId) + if (parent) reroute.parentId = parent.id } } -export function remapClipboardSubgraphNodeIds( - parsed: ClipboardItems, - rootGraph: LGraph +function createClipboardLinks(context: ClipboardPasteContext): void { + for (const info of context.items.links) createClipboardLink(context, info) +} + +function createClipboardLink( + context: ClipboardPasteContext, + info: InitializedClipboardItems['links'][number] ): void { - const usedNodeIds = new Set() - forEachNode(rootGraph, (node) => { - const numericId = Number(node.id) - if (!Number.isInteger(numericId)) return - usedNodeIds.add(numericId) - observeNodeId(rootGraph.state, toNodeId(numericId)) - }) + const { graph, result, targetSlotByLink } = context + const targetNode = result.nodes.get(serializeNodeId(info.target_id)) + if (!targetNode) return + + const [originNode, afterRerouteId] = clipboardLinkOrigin(context, info) + const link = originNode?.connect( + info.origin_slot, + targetNode, + targetSlotByLink.get(toLinkId(info.id)) ?? info.target_slot, + afterRerouteId + ) + if (!link) return + + transferLinkPresentation( + graphScopeOf(graph), + { + hidden: info.hidden === true, + label: typeof info.label === 'string' ? info.label : undefined + }, + link.id + ) + result.links.set(toLinkId(info.id), link) +} - function nextUniqueNodeId() { - let nextId = Number(mintNodeId(rootGraph.state)) - while (usedNodeIds.has(nextId)) { - nextId = Number(mintNodeId(rootGraph.state)) +function clipboardLinkOrigin( + context: ClipboardPasteContext, + info: InitializedClipboardItems['links'][number] +): [LGraphNode | null | undefined, RerouteId | undefined] { + const { connectInputs, graph, result } = context + let originNode: LGraphNode | null | undefined = result.nodes.get( + serializeNodeId(info.origin_id) + ) + let afterRerouteId = + info.parentId == null + ? undefined + : result.reroutes.get(toRerouteId(info.parentId))?.id + + if ( + connectInputs && + LiteGraph.ctrl_shift_v_paste_connect_unselected_outputs + ) { + const originNodeId = parseNodeId(info.origin_id) + originNode ??= originNodeId ? graph.getNodeById(originNodeId) : null + if (info.parentId !== undefined) { + afterRerouteId ??= toRerouteId(info.parentId) } - usedNodeIds.add(nextId) - return nextId } - const subgraphNodeIdMap = new Map< - SubgraphId, - Map - >() - for (const subgraphInfo of parsed.subgraphs ?? []) { - const remappedIds = new Map() - const interiorNodes = subgraphInfo.nodes ?? [] - - for (const nodeInfo of interiorNodes) { - if (typeof nodeInfo.id !== 'number') continue + return [originNode, afterRerouteId] +} - if (usedNodeIds.has(nodeInfo.id)) { - const oldId = nodeInfo.id - const newId = nextUniqueNodeId() - remappedIds.set(oldId, newId) - nodeInfo.id = newId - continue - } +function removeUnusedClipboardReroutes(context: ClipboardPasteContext): void { + const { graph, result } = context + for (const [sourceId, reroute] of result.reroutes) { + if (reroute.totalLinks !== 0) continue - usedNodeIds.add(nodeInfo.id) - observeNodeId(rootGraph.state, toNodeId(nodeInfo.id)) - } + graph.removeReroute(reroute.id) + result.reroutes.delete(sourceId) + const index = result.created.indexOf(reroute) + if (index !== -1) result.created.splice(index, 1) + } +} - if (remappedIds.size > 0) { - patchLinkNodeIds(subgraphInfo.links, remappedIds) - subgraphNodeIdMap.set(subgraphInfo.id, remappedIds) +function positionClipboardItems(context: ClipboardPasteContext): void { + const { dx, dy, result } = context + for (const item of result.created) { + if (item instanceof LGraphNode) { + item.setPos(item.pos[0] + dx, item.pos[1] + dy) + } else { + item.move(dx, dy, true) } } +} - const allNodeInfo: ISerialisedNode[] = [ - parsed.nodes ? [parsed.nodes] : [], - parsed.subgraphs ? parsed.subgraphs.map((s) => s.nodes ?? []) : [] - ].flat(2) - - for (const nodeInfo of allNodeInfo) { - if (typeof nodeInfo.type !== 'string') continue - const remappedIds = subgraphNodeIdMap.get(nodeInfo.type) - remapProxyWidgets(nodeInfo, remappedIds) - remapPreviewExposures(nodeInfo, remappedIds) +function updateClipboardNodeLayout(context: ClipboardPasteContext): void { + const { graph, result } = context + const positions = result.created + .filter((item): item is LGraphNode => item instanceof LGraphNode) + .map((node) => ({ + nodeId: node.id, + bounds: { + x: node.pos[0], + y: node.pos[1], + width: node.size[0], + height: node.size[1] + } + })) + + const rootGraphId = graph.rootGraph.id + layoutStore.batchUpdateNodeBounds(rootGraphId, positions, { + source: LayoutSource.Canvas + }) + const { setNodeZIndex } = useLayoutMutations(LayoutSource.Canvas) + for (const { nodeId } of positions) { + setNodeZIndex(rootGraphId, nodeId, layoutStore.allocateZIndex()) } } + +export interface LGraphCanvas { + /** @deprecated Use {@link LGraphCanvas.applyNodePositions} instead. */ + repositionNodesVueMode(positions: NewNodePosition[]): void +} + +defineDeprecatedProperty( + LGraphCanvas.prototype, + 'repositionNodesVueMode', + 'applyNodePositions', + 'LGraphCanvas.repositionNodesVueMode is deprecated. Use applyNodePositions instead.' +) diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 3d8b78f87aa..98f24c63527 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -75,6 +75,7 @@ import { resolveInputSlotView } from './node/slotDescriptorView' import { initializeWidgetsView } from './node/widgetsView' +import { collectReservedLinkIds } from './subgraph/subgraphDeduplication' import { extensionConfigureView, hydrateExtensionPayload, @@ -3063,7 +3064,7 @@ export class LGraphNode connect( slot: number | string, target_node: LGraphNode | number | null, - target_slot: ISlotType, + target_slot: number | string, afterRerouteId?: RerouteId ): LLink | null { // Allow legacy API support for searching target_slot by string, without mutating the input variables @@ -3225,7 +3226,9 @@ export class LGraphNode const maybeCommonType = input.type && output.type && commonType(input.type, output.type) - const linkId = mintLinkId(graph.state) + const linkId = mintLinkId(graph.state, () => + collectReservedLinkIds(graph.rootGraph) + ) const link = new LLink( linkId, diff --git a/src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts b/src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts similarity index 90% rename from src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts rename to src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts index b099bbf4abe..b95b110d5a8 100644 --- a/src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts +++ b/src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts @@ -4,15 +4,7 @@ import { } from '@/lib/litegraph/src/constants' import type { SerialisableGraph } from '@/lib/litegraph/src/types/serialisation' -/** - * Workflow where lastNodeId is near the MAX_NODE_ID ceiling (100_000_000) - * and root node 100_000_000 reserves the only remaining candidate ID. - * - * Both subgraph definitions share node IDs [3, 8, 37]. When SubgraphB's - * duplicates need remapping, candidate 100_000_000 is already reserved, - * so the next candidate (100_000_001) exceeds MAX_NODE_ID and must throw. - */ -export const nodeIdSpaceExhausted = { +export const nodeIdsAtFormerLimit = { id: 'cccccccc-cccc-4ccc-8ccc-cccccccccccc', version: 1, revision: 0, diff --git a/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts b/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts index 40fc7720555..1ebe6d6ae0b 100644 --- a/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts +++ b/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts @@ -7,6 +7,7 @@ import { compareNodeIds } from '@/types/nodeId' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { layoutHiddenLinkBadges, queryLinkBadgeAtPoint } from './linkBadges' import { getLinkEndpointPositions } from './linkGeometry' +import { slotTypeKey } from '../utils/type' export function queryHiddenLinkBadgeAtPoint( host: object, @@ -63,7 +64,7 @@ export function layoutGraphLinkBadges( startPos, endPos, (typeof link.color === 'string' && link.color) || - linkTypeColors[link.type] || + linkTypeColors[slotTypeKey(link.type)] || defaultLinkColor ) ) diff --git a/src/lib/litegraph/src/canvas/linkBadges.test.ts b/src/lib/litegraph/src/canvas/linkBadges.test.ts index 99eaaa87213..127b515ed6e 100644 --- a/src/lib/litegraph/src/canvas/linkBadges.test.ts +++ b/src/lib/litegraph/src/canvas/linkBadges.test.ts @@ -57,6 +57,7 @@ describe('linkBadgeText', () => { expected: 'Checkpoint' }, { type: 'MODEL', presentation: {}, expected: 'MODEL' }, + { type: ['IMAGE', 'MASK'], presentation: {}, expected: 'IMAGE,MASK' }, { type: '', presentation: {}, expected: '*' }, { type: -1, presentation: {}, expected: '*' } ] satisfies { diff --git a/src/lib/litegraph/src/canvas/linkBadges.ts b/src/lib/litegraph/src/canvas/linkBadges.ts index 3e72d8c5eff..ec1141cae68 100644 --- a/src/lib/litegraph/src/canvas/linkBadges.ts +++ b/src/lib/litegraph/src/canvas/linkBadges.ts @@ -6,6 +6,7 @@ import type { LLink } from '../LLink' import type { LinkId } from '@/types/linkId' import type { LinkPresentation } from '@/types/linkPresentation' import { overlapBounding } from '../measure' +import { slotTypeKey } from '../utils/type' export const BADGE_GAP = 14 const BADGE_HEIGHT = 18 @@ -57,7 +58,7 @@ export function linkBadgeText( const label = presentation.label?.trim() if (label) return label if (typeof type === 'number') return '*' - return type || '*' + return String(slotTypeKey(type) || '*') } function makeBadge(text: string, color: string): LGraphBadge { diff --git a/src/lib/litegraph/src/idAllocation.property.test.ts b/src/lib/litegraph/src/idAllocation.property.test.ts index 96a115509fa..8492e43bac6 100644 --- a/src/lib/litegraph/src/idAllocation.property.test.ts +++ b/src/lib/litegraph/src/idAllocation.property.test.ts @@ -53,10 +53,10 @@ describe('idAllocation has no collision avoidance against a concurrent external agent.lastNodeId = lastObservedId const frontendIds = Array.from({ length: mintCount }, () => - Number(mintNodeId(frontend)) + Number(mintNodeId(frontend, 'sequential', new Set())) ) const agentIds = Array.from({ length: mintCount }, () => - Number(mintNodeId(agent)) + Number(mintNodeId(agent, 'sequential', new Set())) ) return frontendIds.every((id, index) => id === agentIds[index]) @@ -97,7 +97,9 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen const frontend = createLGraphState() frontend.lastNodeId = lastObservedId - const frontendMintedId = BigInt(mintNodeId(frontend, 'crdt-disjoint')) + const frontendMintedId = BigInt( + mintNodeId(frontend, 'crdt-disjoint', new Set()) + ) return frontendMintedId !== agentId } @@ -113,7 +115,7 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen (mintCount, agentIds) => { const frontend = createLGraphState() const frontendIds = Array.from({ length: mintCount }, () => - BigInt(mintNodeId(frontend, 'crdt-disjoint')) + BigInt(mintNodeId(frontend, 'crdt-disjoint', new Set())) ) const agentIdSet = new Set(agentIds) return frontendIds.every((id) => !agentIdSet.has(id)) @@ -140,7 +142,7 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen fc.property(fc.integer({ min: 1, max: 50 }), (mintCount) => { const frontend = createLGraphState() const ids = Array.from({ length: mintCount }, () => - BigInt(mintNodeId(frontend, 'crdt-disjoint')) + BigInt(mintNodeId(frontend, 'crdt-disjoint', new Set())) ) return ids.every( @@ -158,7 +160,7 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen fc.property(fc.integer({ min: 2, max: 50 }), (mintCount) => { const frontend = createLGraphState() const ids = Array.from({ length: mintCount }, () => - BigInt(mintNodeId(frontend, 'crdt-disjoint')) + BigInt(mintNodeId(frontend, 'crdt-disjoint', new Set())) ) return new Set(ids).size === ids.length diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 90418f976c3..21e1a1416f5 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest' import { AGENT_RESERVED_BIT, createLGraphState, + findNextAvailableId, isReservedBitRangeNodeId, matchesReservedBitConvention, mintGroupId, @@ -22,18 +23,31 @@ import { toRerouteId } from '@/types/rerouteId' describe('idAllocation', () => { it('mints increasing ids for each entity kind', () => { const state = createLGraphState() + const reservedIds = new Set() - expect([mintNodeId(state), mintNodeId(state)]).toEqual(['1', '2']) - expect([mintGroupId(state), mintGroupId(state)]).toEqual([1, 2]) - expect([mintLinkId(state), mintLinkId(state)]).toEqual([1, 2]) - expect([mintRerouteId(state), mintRerouteId(state)]).toEqual([1, 2]) + expect([ + mintNodeId(state, 'sequential', reservedIds), + mintNodeId(state, 'sequential', reservedIds) + ]).toEqual(['1', '2']) + expect([ + mintGroupId(state, reservedIds), + mintGroupId(state, reservedIds) + ]).toEqual([1, 2]) + expect([ + mintLinkId(state, reservedIds), + mintLinkId(state, reservedIds) + ]).toEqual([1, 2]) + expect([ + mintRerouteId(state, reservedIds), + mintRerouteId(state, reservedIds) + ]).toEqual([1, 2]) }) it("mints from a disjoint range in 'crdt-disjoint' mode, ignoring lastNodeId", () => { const state = createLGraphState() state.lastNodeId = 5 - const id = BigInt(mintNodeId(state, 'crdt-disjoint')) + const id = BigInt(mintNodeId(state, 'crdt-disjoint', new Set())) expect(state.lastNodeId).toBe(5) // Bit 40 clear (never the agent's `2**40 | random52` range), bit 41 set. @@ -70,6 +84,123 @@ describe('idAllocation', () => { expect(state.lastNodeId).toBe(12) }) + it.for([ + { + name: 'node', + observe: (state: ReturnType, value: number) => + observeNodeId(state, toNodeId(value)), + mint: (state: ReturnType) => + mintNodeId(state, 'sequential', new Set()) + }, + { + name: 'group', + observe: (state: ReturnType, value: number) => + observeGroupId(state, toGroupId(value)), + mint: (state: ReturnType) => + mintGroupId(state, new Set()) + }, + { + name: 'link', + observe: (state: ReturnType, value: number) => + observeLinkId(state, toLinkId(value)), + mint: (state: ReturnType) => + mintLinkId(state, new Set()) + }, + { + name: 'reroute', + observe: (state: ReturnType, value: number) => + observeRerouteId(state, toRerouteId(value)), + mint: (state: ReturnType) => + mintRerouteId(state, new Set()) + } + ])( + 'continues $name allocation above the former limit', + ({ observe, mint }) => { + const state = createLGraphState() + observe(state, 100_000_001) + + expect(Number(mint(state))).toBe(100_000_002) + } + ) + + it.for([ + { + name: 'node', + setCounter: (state: ReturnType) => { + state.lastNodeId = Number.MAX_SAFE_INTEGER + }, + mint: ( + state: ReturnType, + reservedIds: ReadonlySet + ) => mintNodeId(state, 'sequential', reservedIds) + }, + { + name: 'group', + setCounter: (state: ReturnType) => { + state.lastGroupId = Number.MAX_SAFE_INTEGER + }, + mint: mintGroupId + }, + { + name: 'link', + setCounter: (state: ReturnType) => { + state.lastLinkId = toLinkId(Number.MAX_SAFE_INTEGER) + }, + mint: mintLinkId + }, + { + name: 'reroute', + setCounter: (state: ReturnType) => { + state.lastRerouteId = toRerouteId(Number.MAX_SAFE_INTEGER) + }, + mint: mintRerouteId + } + ])( + 'wraps $name allocation to an available safe ID', + ({ setCounter, mint }) => { + const state = createLGraphState() + setCounter(state) + + expect(Number(mint(state, new Set([1])))).toBe(2) + } + ) + + it('checks the candidate after the last reserved ID', () => { + expect(findNextAvailableId(new Set([1, 2, 3]), 1)).toBe(4) + }) + + it('skips a reserved ID above a stale counter', () => { + const state = createLGraphState() + state.lastGroupId = 1 + + expect(mintGroupId(state, new Set([2]))).toBe(3) + }) + + it('collects reservations only when the counter reaches its boundary', () => { + const state = createLGraphState() + let collections = 0 + const collectReservedIds = () => { + collections++ + return new Set([1]) + } + + expect(mintGroupId(state, collectReservedIds)).toBe(1) + expect(collections).toBe(0) + + state.lastGroupId = Number.MAX_SAFE_INTEGER + expect(mintGroupId(state, collectReservedIds)).toBe(2) + expect(collections).toBe(1) + }) + + it('wraps a reserved maximum candidate to the first available ID', () => { + expect( + findNextAvailableId( + new Set([Number.MAX_SAFE_INTEGER, 1]), + Number.MAX_SAFE_INTEGER + ) + ).toBe(2) + }) + describe('isReservedBitRangeNodeId', () => { it.for([ { id: toNodeId(AGENT_RESERVED_BIT.toString()), name: 'the agent floor' }, diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index 87241ad10a2..f23761b8f7f 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -15,6 +15,8 @@ export interface LGraphState { lastRerouteId: RerouteId } +type ReservedIds = ReadonlySet | (() => ReadonlySet) + export function createLGraphState(): LGraphState { return { lastGroupId: 0, @@ -24,8 +26,36 @@ export function createLGraphState(): LGraphState { } } +export function findNextAvailableId( + usedIds: ReadonlySet, + startAt = 1 +): number { + let candidate = Number.isSafeInteger(startAt) && startAt > 0 ? startAt : 1 + let attempts = 0 + while (usedIds.has(candidate) && attempts < usedIds.size) { + candidate = candidate === Number.MAX_SAFE_INTEGER ? 1 : candidate + 1 + attempts++ + } + return candidate +} + +function mintSequentialId(lastId: number, reservedIds: ReservedIds): number { + if (Number.isSafeInteger(lastId) && lastId >= 0) { + const nextId = lastId + 1 + if ( + Number.isSafeInteger(nextId) && + (typeof reservedIds === 'function' || !reservedIds.has(nextId)) + ) + return nextId + } + const usedIds = + typeof reservedIds === 'function' ? reservedIds() : reservedIds + return findNextAvailableId(usedIds, lastId + 1) +} + /** - * `'sequential'` (default) is the plain-local `++lastNodeId` counter. + * `'sequential'` advances the local counter, wrapping to an unused safe integer + * at the boundary. * `'crdt-disjoint'` is for a graph bound to the in-app agent's collaborative * doc, where a local mint can otherwise land on an id the agent independently * mints for the same doc — see {@link mintCrdtDisjointNodeId}. @@ -132,42 +162,69 @@ export function matchesReservedBitConvention(id: NodeId): boolean { export function mintNodeId( state: LGraphState, - mode: NodeIdMintMode = 'sequential' + mode: NodeIdMintMode, + reservedIds: ReservedIds ): NodeId { - return mode === 'crdt-disjoint' - ? mintCrdtDisjointNodeId() - : toNodeId(++state.lastNodeId) + if (mode === 'crdt-disjoint') return mintCrdtDisjointNodeId() + const id = mintSequentialId(state.lastNodeId, reservedIds) + if (id > state.lastNodeId) state.lastNodeId = id + return toNodeId(id) } -export function mintGroupId(state: LGraphState): GroupId { - return toGroupId(++state.lastGroupId) +export function mintGroupId( + state: LGraphState, + reservedIds: ReservedIds +): GroupId { + const id = mintSequentialId(state.lastGroupId, reservedIds) + if (id > state.lastGroupId) state.lastGroupId = id + return toGroupId(id) } -export function mintLinkId(state: LGraphState): LinkId { - state.lastLinkId = toLinkId(Number(state.lastLinkId) + 1) - return state.lastLinkId +export function mintLinkId( + state: LGraphState, + reservedIds: ReservedIds +): LinkId { + const lastLinkId = Number(state.lastLinkId) + const id = mintSequentialId(lastLinkId, reservedIds) + if (id > lastLinkId) state.lastLinkId = toLinkId(id) + return toLinkId(id) } -export function mintRerouteId(state: LGraphState): RerouteId { - state.lastRerouteId = toRerouteId(Number(state.lastRerouteId) + 1) - return state.lastRerouteId +export function mintRerouteId( + state: LGraphState, + reservedIds: ReservedIds +): RerouteId { + const lastRerouteId = Number(state.lastRerouteId) + const id = mintSequentialId(lastRerouteId, reservedIds) + if (id > lastRerouteId) state.lastRerouteId = toRerouteId(id) + return toRerouteId(id) } export function observeNodeId(state: LGraphState, id: NodeId): void { const numericId = Number(id) - if (Number.isInteger(numericId) && numericId > state.lastNodeId) { + if ( + Number.isSafeInteger(numericId) && + numericId >= 0 && + numericId > state.lastNodeId + ) { state.lastNodeId = numericId } } export function observeGroupId(state: LGraphState, id: GroupId): void { - if (id > state.lastGroupId) state.lastGroupId = id + if (Number.isSafeInteger(id) && id >= 0 && id > state.lastGroupId) { + state.lastGroupId = id + } } export function observeLinkId(state: LGraphState, id: LinkId): void { - if (id > state.lastLinkId) state.lastLinkId = id + if (Number.isSafeInteger(id) && id >= 0 && id > state.lastLinkId) { + state.lastLinkId = id + } } export function observeRerouteId(state: LGraphState, id: RerouteId): void { - if (id > state.lastRerouteId) state.lastRerouteId = id + if (Number.isSafeInteger(id) && id >= 0 && id > state.lastRerouteId) { + state.lastRerouteId = id + } } diff --git a/src/lib/litegraph/src/interfaces.ts b/src/lib/litegraph/src/interfaces.ts index 8edc8a9ae8f..38a88ead163 100644 --- a/src/lib/litegraph/src/interfaces.ts +++ b/src/lib/litegraph/src/interfaces.ts @@ -299,11 +299,11 @@ export type Direction = 'top' | 'bottom' | 'left' | 'right' export type CompassCorners = 'NE' | 'SE' | 'SW' | 'NW' /** - * A string that represents a specific data / slot type, e.g. `STRING`. + * A value that represents a specific data / slot type, e.g. `STRING`. * - * Can be comma-delimited to specify multiple allowed types, e.g. `STRING,INT`. + * Multiple allowed types may be comma-delimited or stored as an array. */ -export type ISlotType = number | string +export type ISlotType = number | string | string[] export interface INodeSlot extends HasBoundingRect { /** diff --git a/src/lib/litegraph/src/subgraph/SubgraphInput.ts b/src/lib/litegraph/src/subgraph/SubgraphInput.ts index 5a167c51bee..860ab5bf677 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInput.ts @@ -19,6 +19,7 @@ import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { SubgraphInputNode } from './SubgraphInputNode' import type { SubgraphOutput } from './SubgraphOutput' import { SubgraphSlot } from './SubgraphSlotBase' +import { collectReservedLinkIds } from './subgraphDeduplication' import { isNodeSlot, isSubgraphOutput } from './subgraphUtils' /** @@ -87,7 +88,9 @@ export class SubgraphInput extends SubgraphSlot { subgraph.beforeChange() } - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId(subgraph.state, () => + collectReservedLinkIds(subgraph.rootGraph) + ) const link = new LLink( linkId, @@ -105,44 +108,60 @@ export class SubgraphInput extends SubgraphSlot { } subgraph._addLink(link) - if (existingLink) { - this.parent._disconnectNodeInput(node, slot, existingLink) - if (subgraph.getLink(link.id) !== link) { - subgraph.afterChange() - return - } - } + return this.finishConnection( + link, + existingLink, + slot, + node, + inputIndex, + inputWidget + ) + } - this.linkIds.push(link.id) - anchorRerouteChain(subgraph, link) - subgraph.incrementVersion() - - if (inputWidget) { - // Keep the widget reference in sync with the active upstream widget. - // Stale references can appear across nested promotion rebinds. - this._widget = inputWidget - this.events.dispatch('input-connected', { - input: slot, - widget: inputWidget, - node - }) - } else { - this.events.dispatch('input-connected', { input: slot }) - } - if (subgraph.getLink(link.id) !== link) { - subgraph.afterChange() - return - } + private finishConnection( + link: LLink, + existingLink: LLink | undefined, + slot: INodeInputSlot, + node: LGraphNode, + inputIndex: number, + inputWidget: IBaseWidget | undefined + ): LLink | undefined { + const { subgraph } = this.parent + try { + if (existingLink) { + this.parent._disconnectNodeInput(node, slot, existingLink) + if (subgraph.getLink(link.id) !== link) return + } - node.onConnectionsChange?.(NodeSlotType.INPUT, inputIndex, true, link, slot) - if (subgraph.getLink(link.id) !== link) { + this.linkIds.push(link.id) + anchorRerouteChain(subgraph, link) + subgraph.incrementVersion() + + if (inputWidget) { + this._widget = inputWidget + this.events.dispatch('input-connected', { + input: slot, + widget: inputWidget, + node + }) + } else { + this.events.dispatch('input-connected', { input: slot }) + } + if (subgraph.getLink(link.id) !== link) return + + node.onConnectionsChange?.( + NodeSlotType.INPUT, + inputIndex, + true, + link, + slot + ) + if (subgraph.getLink(link.id) !== link) return + + return link + } finally { subgraph.afterChange() - return } - - subgraph.afterChange() - - return link } get labelPos(): Point { diff --git a/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts b/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts index 8dff9fca585..b5a2072c18b 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts @@ -23,6 +23,7 @@ import { EmptySubgraphInput } from './EmptySubgraphInput' import { SubgraphIONodeBase } from './SubgraphIONodeBase' import type { SubgraphInput } from './SubgraphInput' import type { SubgraphOutput } from './SubgraphOutput' +import { collectReservedLinkIds } from './subgraphDeduplication' export class SubgraphInputNode extends SubgraphIONodeBase @@ -108,7 +109,9 @@ export class SubgraphInputNode if (outputIndex === -1 || inputIndex === -1) throw new Error('Invalid slot indices.') - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId(subgraph.state, () => + collectReservedLinkIds(subgraph.rootGraph) + ) return new LLink( linkId, diff --git a/src/lib/litegraph/src/subgraph/SubgraphOutput.ts b/src/lib/litegraph/src/subgraph/SubgraphOutput.ts index 6d7c548cf8a..c627757509f 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphOutput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphOutput.ts @@ -15,6 +15,7 @@ import { NodeSlotType } from '@/lib/litegraph/src/types/globalEnums' import type { SubgraphInput } from './SubgraphInput' import type { SubgraphOutputNode } from './SubgraphOutputNode' import { SubgraphSlot } from './SubgraphSlotBase' +import { collectReservedLinkIds } from './subgraphDeduplication' import { isNodeSlot, isSubgraphInput } from './subgraphUtils' /** @@ -58,7 +59,9 @@ export class SubgraphOutput extends SubgraphSlot { subgraph.beforeChange() } - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId(subgraph.state, () => + collectReservedLinkIds(subgraph.rootGraph) + ) const link = new LLink( linkId, diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts index 77c09f7027d..5f24905342d 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts @@ -5,6 +5,7 @@ import { import { describe, expect, it } from 'vitest' import { toLinkId } from '@/types/linkId' +import { toNodeId } from '@/types/nodeId' import { toRerouteId } from '@/types/rerouteId' import { isUuidShapedSubgraphId } from '@/schemas/subgraphIdSchema' @@ -367,6 +368,74 @@ describe('normalizeSubgraphDefinitions', () => { expect(result.inputs![0].linkIds).toEqual([toLinkId(1)]) expect(subgraph.floatingLinks).toHaveLength(1) }) + + it('allocates beyond the former fixed limit', () => { + const subgraph = makeSubgraph('sg', ['dummy']) + const state = freshState() + state.lastNodeId = 100_000_000 + + const result = normalizeSubgraphDefinitions( + [subgraph], + { + nodeIds: new Set([toNodeId(1)]), + groupIds: new Set(), + linkIds: new Set(), + rerouteIds: new Set() + }, + state + ) + + expect(result.subgraphs[0].nodes![0].id).toBe(100_000_001) + expect(state.lastNodeId).toBe(100_000_001) + }) + + it('wraps colliding IDs at the safe-integer boundary', () => { + const subgraph = makeSubgraph('sg', ['dummy']) + subgraph.links = [chainedLink(1)] + const state = freshState() + state.lastLinkId = toLinkId(Number.MAX_SAFE_INTEGER) + + const result = normalizeSubgraphDefinitions( + [subgraph], + { + nodeIds: new Set(), + groupIds: new Set(), + linkIds: new Set([1]), + rerouteIds: new Set() + }, + state + ) + + expect(result.subgraphs[0].links![0].id).toBe(toLinkId(2)) + expect(state.lastLinkId).toBe(toLinkId(Number.MAX_SAFE_INTEGER)) + }) + + it('updates every root-level reference to a remapped interior node', () => { + const subgraph = makeSubgraph('sg', ['dummy']) + const rootNode = makeSubgraph('root', ['sg']).nodes![0] + rootNode.properties = { + proxyWidgets: [['1', 'seed']], + previewExposures: [{ sourceNodeId: '1' }] + } + + const result = normalizeSubgraphDefinitions( + [subgraph], + { + nodeIds: new Set([toNodeId(1)]), + groupIds: new Set(), + linkIds: new Set(), + rerouteIds: new Set() + }, + freshState(), + [rootNode] + ) + const remappedId = result.subgraphs[0].nodes![0].id + + expect(result.rootNodes![0].properties).toEqual({ + proxyWidgets: [[String(remappedId), 'seed']], + previewExposures: [{ sourceNodeId: String(remappedId) }] + }) + }) }) describe('deduplicateSubgraphGroupIds', () => { diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts index 4b52722d378..325f29042d3 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts @@ -20,6 +20,7 @@ import { toNodeId } from '@/types/nodeId' import type { NodeId, SerializedNodeId } from '@/types/nodeId' import { toLinkId } from '@/types/linkId' import { toRerouteId } from '@/types/rerouteId' +import type { NodeProperty } from '@/types/nodeState' import { createUuidv4 } from '@/utils/uuid' import type { ExportedSubgraph, @@ -29,8 +30,6 @@ import type { SerialisableLLink } from '../types/serialisation' -const MAX_ID = 100_000_000 - interface DeduplicationResult< Subgraph extends { id: string; nodes?: { type: string }[] } = ExportedSubgraph, @@ -254,11 +253,15 @@ function deduplicateClonedSubgraphNodeIds( } for (const subgraph of clonedSubgraphs) { - patchProxyWidgets(subgraph.nodes ?? [], subgraphIdSet, remapBySubgraph) + patchSubgraphNodeReferences( + subgraph.nodes ?? [], + subgraphIdSet, + remapBySubgraph + ) } if (clonedRootNodes) { - patchProxyWidgets(clonedRootNodes, subgraphIdSet, remapBySubgraph) + patchSubgraphNodeReferences(clonedRootNodes, subgraphIdSet, remapBySubgraph) } } @@ -282,9 +285,7 @@ function remapNodeIds( const numericId = numericSerializedNodeId(id) if (usedNodeIdKeys.has(key)) { - const newId = findNextAvailableId(usedNodeIds, () => - Number(mintNodeId(state)) - ) + const newId = Number(mintNodeId(state, 'sequential', usedNodeIds)) remappedIds.set(key, newId) node.id = newId usedNodeIds.add(newId) @@ -304,6 +305,18 @@ function remapNodeIds( return remappedIds } +export function collectReservedNodeIds( + rootGraph: Pick, + rootNodes: ISerialisedNode[] = [] +): Set { + return new Set([ + ...rootNodes.map((node) => toNodeId(node.id)), + ...[rootGraph, ...rootGraph.subgraphs.values()].flatMap((owner) => + owner.nodes.map((node) => node.id) + ) + ]) +} + /** Parses a serialized node ID as an integer, or `null` when non-numeric. */ function numericSerializedNodeId(id: SerializedNodeId): number | null { const key = toNodeId(id) @@ -313,23 +326,6 @@ function numericSerializedNodeId(id: SerializedNodeId): number | null { : null } -/** - * Finds the next unused ID by repeatedly calling `advance`. - * Throws if the ID space is exhausted. - */ -function findNextAvailableId( - usedIds: Set, - advance: () => number -): number { - for (;;) { - const nextId = advance() - if (nextId > MAX_ID) { - throw new Error('Node ID space exhausted') - } - if (!usedIds.has(nextId)) return nextId - } -} - /** Patches origin_id / target_id in serialized links. */ function patchSerialisedLinks( links: SerialisableLLink[], @@ -392,7 +388,7 @@ export function deduplicateSubgraphLinkIds( const remapped = remapNumericIds( [...(subgraph.links ?? []), ...(subgraph.floatingLinks ?? [])], usedLinkIds, - () => mintLinkId(state), + () => mintLinkId(state, usedLinkIds), (id) => observeLinkId(state, toLinkId(id)), 'link' ) @@ -417,7 +413,7 @@ export function deduplicateSubgraphGroupIds( remapNumericIds( subgraph.groups ?? [], usedGroupIds, - () => mintGroupId(state), + () => mintGroupId(state, usedGroupIds), (id) => observeGroupId(state, toGroupId(id)), 'group' ) @@ -470,7 +466,7 @@ function remapRerouteIds( return remapNumericIds( subgraph.reroutes ?? [], usedRerouteIds, - () => mintRerouteId(state), + () => mintRerouteId(state, usedRerouteIds), (id) => observeRerouteId(state, toRerouteId(id)), 'reroute' ) @@ -488,7 +484,7 @@ function remapNumericIds( for (const item of items) { const oldId = item.id if (usedIds.has(oldId)) { - const newId = findNextAvailableId(usedIds, nextId) + const newId = nextId() remapped.set(oldId, newId) item.id = newId usedIds.add(newId) @@ -581,8 +577,7 @@ export function topologicalSortSubgraphs( return sorted } -/** Patches legacy proxyWidgets in root-level SubgraphNode instances. */ -function patchProxyWidgets( +function patchSubgraphNodeReferences( rootNodes: ISerialisedNode[], subgraphIdSet: Set, remapBySubgraph: Map> @@ -592,14 +587,40 @@ function patchProxyWidgets( const remappedIds = remapBySubgraph.get(node.type) if (!remappedIds) continue - const proxyWidgets = node.properties?.proxyWidgets - if (!Array.isArray(proxyWidgets)) continue + patchProxyWidgetReferences(node.properties?.proxyWidgets, remappedIds) + patchPreviewExposureReferences( + node.properties?.previewExposures, + remappedIds + ) + } +} - for (const entry of proxyWidgets) { - if (!Array.isArray(entry)) continue - const oldId = toNodeId(entry[0]) - const newId = remappedIds.get(oldId) - if (newId !== undefined) entry[0] = String(newId) - } +function patchProxyWidgetReferences( + property: NodeProperty | undefined, + remappedIds: Map +): void { + if (!Array.isArray(property)) return + for (const entry of property) { + if (!Array.isArray(entry)) continue + const newId = remappedIds.get(toNodeId(entry[0])) + if (newId !== undefined) entry[0] = String(newId) + } +} + +function patchPreviewExposureReferences( + property: NodeProperty | undefined, + remappedIds: Map +): void { + if (!Array.isArray(property)) return + for (const entry of property) { + if ( + typeof entry !== 'object' || + entry === null || + !('sourceNodeId' in entry) || + typeof entry.sourceNodeId !== 'string' + ) + continue + const newId = remappedIds.get(toNodeId(entry.sourceNodeId)) + if (newId !== undefined) entry.sourceNodeId = String(newId) } } diff --git a/src/lib/litegraph/src/utils/type.ts b/src/lib/litegraph/src/utils/type.ts index cc7b99f5932..ccf9801bb63 100644 --- a/src/lib/litegraph/src/utils/type.ts +++ b/src/lib/litegraph/src/utils/type.ts @@ -39,10 +39,15 @@ export function isNodeBindable(widget: unknown): widget is NodeBindable { ) } +export function slotTypeKey(type: ISlotType): string | number { + return Array.isArray(type) ? type.join(',') : type +} + export function commonType(...types: ISlotType[]): ISlotType | undefined { - if (!isStrings(types)) return undefined + const keys = types.map(slotTypeKey) + if (!isStrings(keys)) return undefined - const withoutWildcards = without(types, '*') + const withoutWildcards = without(keys, '*') if (withoutWildcards.length === 0) return '*' const typeLists: string[][] = withoutWildcards.map((type) => type.split(',')) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.test.ts b/src/platform/workflow/validation/schemas/workflowSchema.test.ts index 8297c4d2427..8f9fa0e50c3 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.test.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.test.ts @@ -1,7 +1,10 @@ import fs from 'fs' import { describe, expect, it } from 'vitest' -import { validateComfyWorkflow } from '@/platform/workflow/validation/schemas/workflowSchema' +import { + validateComfyWorkflow, + zClipboardItems +} from '@/platform/workflow/validation/schemas/workflowSchema' import { defaultGraph } from '@/scripts/defaultGraph' const WORKFLOW_DIR = 'src/platform/workflow/validation/schemas/__fixtures__' @@ -391,3 +394,29 @@ describe('parseComfyWorkflow', () => { }) }) }) + +describe('zClipboardItems', () => { + it('normalizes array-like widget values and preserves array slot types', () => { + const node = JSON.parse(JSON.stringify(defaultGraph.nodes[0])) + node.inputs = [{ name: 'input', type: ['IMAGE', 'MASK'] }] + node.widgets_values = { 0: 'first', 1: 2, length: 2 } + + const result = zClipboardItems.safeParse({ nodes: [node] }) + + expect(result.success).toBe(true) + if (!result.success) throw result.error + expect(result.data.nodes?.[0].inputs?.[0].type).toEqual(['IMAGE', 'MASK']) + expect(result.data.nodes?.[0].widgets_values).toEqual(['first', 2]) + }) + + it('normalizes sparse widget values without allocating their declared length', () => { + const node = JSON.parse(JSON.stringify(defaultGraph.nodes[0])) + node.widgets_values = { 0: 'first', length: Number.MAX_SAFE_INTEGER } + + const result = zClipboardItems.safeParse({ nodes: [node] }) + + expect(result.success).toBe(true) + if (!result.success) throw result.error + expect(result.data.nodes?.[0].widgets_values).toEqual(['first']) + }) +}) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.ts b/src/platform/workflow/validation/schemas/workflowSchema.ts index bb3e20187a0..ce80c658d22 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.ts @@ -2,8 +2,15 @@ import { z } from 'zod' import type { SafeParseReturnType } from 'zod' import { fromZodError } from 'zod-validation-error' import type { RendererType } from '@/lib/litegraph/src/LGraph' -import { parseLinkId } from '@/types/linkId' +import type { + ClipboardItems, + ExportedSubgraph +} from '@/lib/litegraph/src/types/serialisation' +import type { TWidgetValue } from '@/lib/litegraph/src/types/widgets' +import { parseLinkId, toLinkId } from '@/types/linkId' import type { LinkPresentation } from '@/types/linkPresentation' +import type { NodeProperty } from '@/types/nodeState' +import { toRerouteId } from '@/types/rerouteId' const zRendererType = z.enum([ 'LG', @@ -210,6 +217,15 @@ const zVersion = z.union([ z.literal('unknown') ]) +const zNodeProperty = z.union([ + z.string(), + z.number(), + z.boolean(), + z.null(), + z.record(z.unknown()), + z.array(z.unknown()) +]) satisfies z.ZodType + const zProperties = z .object({ ['Node name for S&R']: z.string().optional(), @@ -218,10 +234,25 @@ const zProperties = z ver: zVersion.optional(), models: z.array(zModelFile).optional() }) - .passthrough() + .catchall(zNodeProperty.optional()) const zWidgetValues = z.union([z.array(z.any()), z.record(z.any())]) +function normalizeClipboardWidgetValues( + values: z.output | undefined +): TWidgetValue[] | undefined { + if (!values || Array.isArray(values)) return values + const length = values.length + if (!Number.isSafeInteger(length) || length < 0) return [] + return Object.entries(values) + .filter(([key]) => { + const index = Number(key) + return Number.isInteger(index) && index >= 0 && index < length + }) + .sort(([left], [right]) => Number(left) - Number(right)) + .map(([, value]) => value) +} + const zComfyNode = z .object({ id: zNodeId, @@ -511,6 +542,95 @@ const zSubgraphDefinition = zComfyWorkflow1 }) .passthrough() +const zClipboardNode = zComfyNode.transform((node) => { + return { + ...node, + widgets_values: normalizeClipboardWidgetValues(node.widgets_values) + } +}) + +const zClipboardSubgraphInstance = zSubgraphInstance.transform((node) => { + return { + ...node, + widgets_values: normalizeClipboardWidgetValues(node.widgets_values) + } +}) + +const zClipboardGroup = zGroup.extend({ id: z.number() }) +const zClipboardReroute = zReroute.extend({ linkIds: z.array(z.number()) }) + +interface ClipboardSubgraphInput extends Omit< + z.input, + 'groups' | 'nodes' | 'reroutes' | 'subgraphs' | 'definitions' +> { + groups?: z.input[] + nodes: z.input[] + reroutes?: z.input[] + subgraphs?: z.input[] + definitions?: { subgraphs: ClipboardSubgraphInput[] } +} + +const zClipboardSubgraphDefinition: z.ZodType< + ExportedSubgraph, + z.ZodTypeDef, + ClipboardSubgraphInput +> = zSubgraphDefinition + .extend({ + groups: z.array(zClipboardGroup).optional(), + nodes: z.array(zClipboardNode), + reroutes: z.array(zClipboardReroute).optional(), + subgraphs: z.array(zClipboardSubgraphInstance).optional(), + definitions: z + .object({ + subgraphs: z.lazy(() => z.array(zClipboardSubgraphDefinition)) + }) + .optional() + }) + .transform(({ config, extra, ...subgraph }) => { + const normalizedExtra = extra + ? { + ...extra, + reroutes: extra.reroutes?.map((reroute) => ({ + ...reroute, + linkIds: reroute.linkIds ?? [] + })), + linkExtensions: extra.linkExtensions?.map((link) => ({ + ...link, + id: toLinkId(link.id), + parentId: toRerouteId(link.parentId) + })) + } + : undefined + return { + ...subgraph, + config: config ?? undefined, + extra: normalizedExtra + } + }) + +const zClipboardItemsSchema = z + .object({ + nodes: z.array(zClipboardNode).optional(), + groups: z.array(zClipboardGroup).optional(), + reroutes: z.array(zClipboardReroute).optional(), + links: z.array(zComfyLinkObject).optional(), + subgraphs: z.array(zClipboardSubgraphDefinition).optional() + }) + .refine( + (items) => + items.nodes !== undefined || + items.groups !== undefined || + items.reroutes !== undefined || + items.links !== undefined || + items.subgraphs !== undefined + ) + +export const zClipboardItems: z.ZodType< + ClipboardItems, + z.ZodTypeDef, + z.input +> = zClipboardItemsSchema + export type ModelFile = z.infer export type ComfyLinkObject = z.infer export type ComfyNode = z.infer diff --git a/src/stores/entityIdStore.test.ts b/src/stores/entityIdStore.test.ts index 843cbd35649..33019a564c1 100644 --- a/src/stores/entityIdStore.test.ts +++ b/src/stores/entityIdStore.test.ts @@ -13,12 +13,14 @@ describe(useEntityIdStore, () => { it('keeps allocation state when a root graph is rekeyed', () => { const store = useEntityIdStore() const state = store.get(first) - mintNodeId(state) + mintNodeId(state, 'sequential', new Set()) store.rekey(first, second) expect(store.get(second)).toBe(state) - expect(Number(mintNodeId(store.get(second)))).toBe(2) + expect(Number(mintNodeId(store.get(second), 'sequential', new Set()))).toBe( + 2 + ) }) it('replaces compatibility state without sharing the caller object', () => { diff --git a/src/workbench/extensions/agent/crdt/linkMintPort.ts b/src/workbench/extensions/agent/crdt/linkMintPort.ts index 91d1fa2032b..c3fe65f50f7 100644 --- a/src/workbench/extensions/agent/crdt/linkMintPort.ts +++ b/src/workbench/extensions/agent/crdt/linkMintPort.ts @@ -8,6 +8,7 @@ */ import type { NodeId as WireNodeId } from '@comfyorg/comfy-multi-player' +import type { ISlotType } from '@/lib/litegraph/src/interfaces' import { reportError } from '@/platform/telemetry/reportError' import type { GraphOperation } from './graphOperations' @@ -31,7 +32,7 @@ export interface LinkTopologyView { originSlot: number targetNodeId: string | number targetSlot: number - type: string | number + type: ISlotType } interface LinkEventFeed { From ddaf62c8879ac13e8540e6daa15ffd9bb34f1db1 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 07:13:32 -0700 Subject: [PATCH 02/18] fix(litegraph): address safe ID review findings --- browser_tests/tests/copyPaste.spec.ts | 41 ++++++++++++ src/extensions/core/widgetInputs.test.ts | 10 +++ src/extensions/core/widgetInputs.ts | 6 +- src/lib/litegraph/src/LGraph.ts | 29 +++----- src/lib/litegraph/src/idAllocation.test.ts | 11 +++- src/lib/litegraph/src/idAllocation.ts | 1 + .../validation/schemas/workflowSchema.test.ts | 17 ++++- .../validation/schemas/workflowSchema.ts | 66 +++++++++++++------ 8 files changed, 136 insertions(+), 45 deletions(-) diff --git a/browser_tests/tests/copyPaste.spec.ts b/browser_tests/tests/copyPaste.spec.ts index 3bf3e0cf996..c10f370af7b 100644 --- a/browser_tests/tests/copyPaste.spec.ts +++ b/browser_tests/tests/copyPaste.spec.ts @@ -191,6 +191,47 @@ test.describe('Copy Paste', { tag: ['@screenshot', '@workflow'] }, () => { }) }) + test('Sparse clipboard widget values keep their original indices', async ({ + comfyPage + }) => { + // Regression: https://github.com/Comfy-Org/ComfyUI_frontend/pull/19239#discussion_r4123007404 + const originalNodes = await comfyPage.nodeOps.getNodeRefsByType('KSampler') + const originalIds = new Set(originalNodes.map(({ id }) => id)) + + await comfyPage.page.evaluate(() => { + const node = window + .app!.graph.serialize() + .nodes.find(({ type }) => type === 'KSampler') + if (!node) throw new Error('KSampler node not found') + const clipboardNode = { + ...node, + widgets_values: { 0: 123, 2: 47, length: 3 } + } + const encoded = btoa(JSON.stringify({ nodes: [clipboardNode] })) + const dataTransfer = new DataTransfer() + dataTransfer.setData( + 'text/html', + `
` + ) + document.dispatchEvent( + new ClipboardEvent('paste', { + clipboardData: dataTransfer, + bubbles: true, + cancelable: true + }) + ) + }) + + await expect + .poll(async () => { + const nodes = await comfyPage.nodeOps.getNodeRefsByType('KSampler') + const pasted = nodes.find(({ id }) => !originalIds.has(id)) + if (!pasted) return undefined + return await (await pasted.getWidget(2)).getValue() + }) + .toBe(47) + }) + test('Can undo paste multiple nodes as single action', async ({ comfyPage }) => { diff --git a/src/extensions/core/widgetInputs.test.ts b/src/extensions/core/widgetInputs.test.ts index 1ba3974d425..59093c17b09 100644 --- a/src/extensions/core/widgetInputs.test.ts +++ b/src/extensions/core/widgetInputs.test.ts @@ -253,6 +253,16 @@ describe('PrimitiveNode', () => { expect(primitive.widgets?.[0].type).toBe('custom_widget') }) + it('rejects an array slot type when no widget describes its construction', () => { + const primitive = new PrimitiveNode('Primitive') + const target = new LGraphNode('Target') + target.addInput('seed', ['INT']) + + expect(primitive.onConnectOutput(0, '*', target.inputs[0], target, 0)).toBe( + false + ) + }) + it('restores its serialized value through the reroute lifecycle', async () => { await widgetInputsExtension.registerCustomNodes?.(app) localStorage.setItem('Comfy.RerouteNode.DefaultVisibility', 'true') diff --git a/src/extensions/core/widgetInputs.ts b/src/extensions/core/widgetInputs.ts index 1b071d9f270..87a54395496 100644 --- a/src/extensions/core/widgetInputs.ts +++ b/src/extensions/core/widgetInputs.ts @@ -13,7 +13,6 @@ import type { import type { IWidgetLocator } from '@/lib/litegraph/src/interfaces' import { NodeSlot } from '@/lib/litegraph/src/node/NodeSlot' import { outputHasLinks, outputLinks } from '@/lib/litegraph/src/node/slotLinks' -import { slotTypeKey } from '@/lib/litegraph/src/utils/type' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { assetService } from '@/platform/assets/services/assetService' import { createAssetWidget } from '@/platform/assets/utils/createAssetWidget' @@ -197,7 +196,10 @@ export class PrimitiveNode extends LGraphNode { ) { // Fires before the link is made allowing us to reject it if it isn't valid // No widget, we can't connect - if (!input.widget && !(slotTypeKey(input.type) in ComfyWidgets)) { + if ( + !input.widget && + (typeof input.type !== 'string' || !(input.type in ComfyWidgets)) + ) { return false } diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index fae0e01fea7..55965ecf2d0 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -1406,19 +1406,17 @@ export class LGraph throw 'LiteGraph: max number of nodes in a graph reached' } + const collectReservedIds = () => + new Set( + [...collectReservedNodeIds(this.rootGraph)] + .map(Number) + .filter(Number.isSafeInteger) + ) + // give him an id if (node.id === UNASSIGNED_NODE_ID) { const mintMode = nodeIdMintModeFor(this) - node.id = mintNodeId( - state, - mintMode, - () => - new Set( - [...collectReservedNodeIds(this.rootGraph)] - .map(Number) - .filter(Number.isSafeInteger) - ) - ) + node.id = mintNodeId(state, mintMode, collectReservedIds) } else { observeNodeId(state, node.id) } @@ -1433,16 +1431,7 @@ export class LGraph node.graph = this attachNodeToStores(this, node, () => - mintNodeId( - state, - nodeIdMintModeFor(this), - () => - new Set( - [...collectReservedNodeIds(this.rootGraph)] - .map(Number) - .filter(Number.isSafeInteger) - ) - ) + mintNodeId(state, nodeIdMintModeFor(this), collectReservedIds) ) this._nodes.push(node) diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 21e1a1416f5..7732c762360 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import { AGENT_RESERVED_BIT, @@ -169,6 +169,15 @@ describe('idAllocation', () => { expect(findNextAvailableId(new Set([1, 2, 3]), 1)).toBe(4) }) + it('throws instead of returning an occupied ID when the search is exhausted', () => { + const occupiedIds = new Set([1]) + vi.spyOn(occupiedIds, 'has').mockReturnValue(true) + + expect(() => findNextAvailableId(occupiedIds, 1)).toThrow( + 'ID space exhausted' + ) + }) + it('skips a reserved ID above a stale counter', () => { const state = createLGraphState() state.lastGroupId = 1 diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index f23761b8f7f..4d4c8a7266b 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -36,6 +36,7 @@ export function findNextAvailableId( candidate = candidate === Number.MAX_SAFE_INTEGER ? 1 : candidate + 1 attempts++ } + if (usedIds.has(candidate)) throw new RangeError('ID space exhausted') return candidate } diff --git a/src/platform/workflow/validation/schemas/workflowSchema.test.ts b/src/platform/workflow/validation/schemas/workflowSchema.test.ts index 8f9fa0e50c3..218854e031e 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.test.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.test.ts @@ -409,14 +409,25 @@ describe('zClipboardItems', () => { expect(result.data.nodes?.[0].widgets_values).toEqual(['first', 2]) }) - it('normalizes sparse widget values without allocating their declared length', () => { + it('preserves indices when normalizing sparse widget values', () => { const node = JSON.parse(JSON.stringify(defaultGraph.nodes[0])) - node.widgets_values = { 0: 'first', length: Number.MAX_SAFE_INTEGER } + node.widgets_values = { 0: 'first', 2: 'third', length: 3 } const result = zClipboardItems.safeParse({ nodes: [node] }) expect(result.success).toBe(true) if (!result.success) throw result.error - expect(result.data.nodes?.[0].widgets_values).toEqual(['first']) + const values = result.data.nodes?.[0].widgets_values + expect(values).toHaveLength(3) + expect(values?.[0]).toBe('first') + expect(1 in (values ?? [])).toBe(false) + expect(values?.[2]).toBe('third') + }) + + it('rejects an impractically large sparse widget values length', () => { + const node = JSON.parse(JSON.stringify(defaultGraph.nodes[0])) + node.widgets_values = { 0: 'first', length: 10_001 } + + expect(zClipboardItems.safeParse({ nodes: [node] }).success).toBe(false) }) }) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.ts b/src/platform/workflow/validation/schemas/workflowSchema.ts index ce80c658d22..4f8e7c5513c 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.ts @@ -237,6 +237,17 @@ const zProperties = z .catchall(zNodeProperty.optional()) const zWidgetValues = z.union([z.array(z.any()), z.record(z.any())]) +const MAX_CLIPBOARD_WIDGET_VALUES = 10_000 + +function hasSupportedClipboardWidgetValuesLength( + values: z.output | undefined +): boolean { + if (!values || Array.isArray(values)) return true + return ( + typeof values.length !== 'number' || + values.length <= MAX_CLIPBOARD_WIDGET_VALUES + ) +} function normalizeClipboardWidgetValues( values: z.output | undefined @@ -244,13 +255,14 @@ function normalizeClipboardWidgetValues( if (!values || Array.isArray(values)) return values const length = values.length if (!Number.isSafeInteger(length) || length < 0) return [] - return Object.entries(values) - .filter(([key]) => { - const index = Number(key) - return Number.isInteger(index) && index >= 0 && index < length - }) - .sort(([left], [right]) => Number(left) - Number(right)) - .map(([, value]) => value) + const normalized: TWidgetValue[] = [] + normalized.length = length + for (const [key, value] of Object.entries(values)) { + const index = Number(key) + if (Number.isInteger(index) && index >= 0 && index < length) + normalized[index] = value + } + return normalized } const zComfyNode = z @@ -542,19 +554,35 @@ const zSubgraphDefinition = zComfyWorkflow1 }) .passthrough() -const zClipboardNode = zComfyNode.transform((node) => { - return { - ...node, - widgets_values: normalizeClipboardWidgetValues(node.widgets_values) - } -}) +const zClipboardNode = zComfyNode + .refine( + (node) => hasSupportedClipboardWidgetValuesLength(node.widgets_values), + { + path: ['widgets_values'], + message: 'Clipboard widget values length is too large' + } + ) + .transform((node) => { + return { + ...node, + widgets_values: normalizeClipboardWidgetValues(node.widgets_values) + } + }) -const zClipboardSubgraphInstance = zSubgraphInstance.transform((node) => { - return { - ...node, - widgets_values: normalizeClipboardWidgetValues(node.widgets_values) - } -}) +const zClipboardSubgraphInstance = zSubgraphInstance + .refine( + (node) => hasSupportedClipboardWidgetValuesLength(node.widgets_values), + { + path: ['widgets_values'], + message: 'Clipboard widget values length is too large' + } + ) + .transform((node) => { + return { + ...node, + widgets_values: normalizeClipboardWidgetValues(node.widgets_values) + } + }) const zClipboardGroup = zGroup.extend({ id: z.number() }) const zClipboardReroute = zReroute.extend({ linkIds: z.array(z.number()) }) From d45096c0d0f3484279723ad9d316308d6792bc48 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 08:04:22 -0700 Subject: [PATCH 03/18] fix(litegraph): close safe ID current-head review --- browser_tests/tests/copyPaste.spec.ts | 2 +- src/composables/usePaste.test.ts | 79 ++++++++++++------- src/composables/usePaste.ts | 11 ++- src/extensions/core/groupNode.test.ts | 9 +++ src/extensions/core/groupNode.ts | 5 +- .../src/LGraphCanvas.clipboard.test.ts | 47 +++++++++++ src/lib/litegraph/src/LGraphCanvas.ts | 38 ++++++++- src/lib/litegraph/src/idAllocation.test.ts | 48 ++++++++++- src/lib/litegraph/src/idAllocation.ts | 18 ++--- .../validation/schemas/workflowSchema.test.ts | 49 ++++++++++-- .../validation/schemas/workflowSchema.ts | 40 ++++++---- 11 files changed, 271 insertions(+), 75 deletions(-) diff --git a/browser_tests/tests/copyPaste.spec.ts b/browser_tests/tests/copyPaste.spec.ts index c10f370af7b..1fdf2841add 100644 --- a/browser_tests/tests/copyPaste.spec.ts +++ b/browser_tests/tests/copyPaste.spec.ts @@ -211,7 +211,7 @@ test.describe('Copy Paste', { tag: ['@screenshot', '@workflow'] }, () => { const dataTransfer = new DataTransfer() dataTransfer.setData( 'text/html', - `
` + `
Text` ) document.dispatchEvent( new ClipboardEvent('paste', { diff --git a/src/composables/usePaste.test.ts b/src/composables/usePaste.test.ts index 3da44835afb..b4be6010f6f 100644 --- a/src/composables/usePaste.test.ts +++ b/src/composables/usePaste.test.ts @@ -80,6 +80,11 @@ function pastedClipboard(kind: 'workflow JSON' | 'an image'): DataTransfer { return dataTransfer } +function clipboardHtml(data: unknown, attribute = 'data-comfy-metadata') { + const encoded = btoa(JSON.stringify(data)) + return `
Text` +} + function mountRichTextEditor() { const editor = document.createElement('div') editor.contentEditable = 'true' @@ -687,8 +692,7 @@ describe('usePaste', () => { links: [], subgraphs: [] } - const encoded = btoa(JSON.stringify(data)) - const html = `
` + const html = clipboardHtml(data) usePaste() @@ -708,8 +712,7 @@ describe('usePaste', () => { it('accepts validated legacy data-metadata clipboard items', async () => { const data = { nodes: [] } - const encoded = btoa(JSON.stringify(data)) - const html = `
` + const html = clipboardHtml(data, 'data-metadata') usePaste() @@ -728,24 +731,13 @@ describe('usePaste', () => { }) }) - it.for([ - { name: 'null payload', data: null }, - { name: 'empty object', data: {} }, - { name: 'malformed node', data: { nodes: [{ type: 'KSampler' }] } }, - { name: 'malformed group', data: { groups: [{ id: 1 }] } }, - { name: 'malformed reroute', data: { reroutes: [{ id: 1 }] } }, - { name: 'malformed link', data: { links: [{ id: 1 }] } }, - { name: 'malformed subgraph', data: { subgraphs: [{ id: 'invalid' }] } } - ])('falls back for malformed Comfy metadata: $name', async ({ data }) => { - const encoded = btoa(JSON.stringify(data)) - const html = `
` + it('does not treat metadata embedded in arbitrary HTML as a Comfy clipboard', async () => { + const encoded = btoa(JSON.stringify({ nodes: [] })) + const html = `
` usePaste() - const dataTransfer = new DataTransfer() dataTransfer.setData('text/html', html) - dataTransfer.setData('text/plain', 'some text') - document.dispatchEvent( new ClipboardEvent('paste', { clipboardData: dataTransfer }) ) @@ -756,10 +748,41 @@ describe('usePaste', () => { }) }) - it('falls back when Comfy metadata is not valid JSON', async () => { - const parseError = vi.spyOn(console, 'error').mockImplementation(() => {}) - onTestFinished(() => parseError.mockRestore()) - const html = `
` + it.for([ + { name: 'null payload', data: null }, + { name: 'empty object', data: {} }, + { name: 'malformed node', data: { nodes: [{ type: 'KSampler' }] } }, + { name: 'malformed group', data: { groups: [{ id: 1 }] } }, + { name: 'malformed reroute', data: { reroutes: [{ id: 1 }] } }, + { name: 'malformed link', data: { links: [{ id: 1 }] } }, + { name: 'malformed subgraph', data: { subgraphs: [{ id: 'invalid' }] } } + ])( + 'rejects malformed Comfy metadata without stale fallback: $name', + async ({ data }) => { + const html = clipboardHtml(data) + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'some text') + + document.dispatchEvent( + new ClipboardEvent('paste', { clipboardData: dataTransfer }) + ) + + await vi.waitFor(() => { + expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() + expect(mockCanvas.pasteFromClipboard).not.toHaveBeenCalled() + expect(useToastStore().add).toHaveBeenCalledWith( + expect.objectContaining({ severity: 'error' }) + ) + }) + } + ) + + it('rejects invalid Comfy JSON without stale fallback', async () => { + const html = `
Text` usePaste() @@ -772,9 +795,11 @@ describe('usePaste', () => { ) await vi.waitFor(() => { - expect(parseError).toHaveBeenCalledOnce() expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() - expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + expect(mockCanvas.pasteFromClipboard).not.toHaveBeenCalled() + expect(useToastStore().add).toHaveBeenCalledWith( + expect.objectContaining({ severity: 'error' }) + ) }) }) @@ -790,8 +815,7 @@ describe('usePaste', () => { links: [], subgraphs: [] } - const encoded = btoa(JSON.stringify(data)) - const html = `
` + const html = clipboardHtml(data) usePaste() @@ -822,8 +846,7 @@ describe('usePaste', () => { usePaste() const nodeData = { nodes: [{ type: 'KSampler' }] } - const encoded = btoa(JSON.stringify(nodeData)) - const html = `
` + const html = clipboardHtml(nodeData) const dataTransfer = new DataTransfer() dataTransfer.setData('text/html', html) diff --git a/src/composables/usePaste.ts b/src/composables/usePaste.ts index 5fa58bee414..692612fb04a 100644 --- a/src/composables/usePaste.ts +++ b/src/composables/usePaste.ts @@ -45,7 +45,7 @@ export function cloneDataTransfer(original: DataTransfer): DataTransfer { function pasteClipboardItems(data: DataTransfer): boolean { const rawData = data.getData('text/html') const match = rawData.match( - /data-(?:comfy-)?metadata="([A-Za-z0-9+/=]+)"/ + /^
<\/span><\/div>Text<\/span>$/ )?.[1] if (!match) return false @@ -56,12 +56,15 @@ function pasteClipboardItems(data: DataTransfer): boolean { const decodedData = new TextDecoder().decode(bytes) parsed = JSON.parse(decodedData) } catch (err) { - console.error(err) - return false + useErrorHandling().toastErrorHandler(err) + return true } const clipboardItems = zClipboardItems.safeParse(parsed) - if (!clipboardItems.success) return false + if (!clipboardItems.success) { + useErrorHandling().toastErrorHandler(clipboardItems.error) + return true + } try { useCanvasStore().getCanvas()._deserializeItems(clipboardItems.data, {}) diff --git a/src/extensions/core/groupNode.test.ts b/src/extensions/core/groupNode.test.ts index 889befd8c80..52f45df9ec1 100644 --- a/src/extensions/core/groupNode.test.ts +++ b/src/extensions/core/groupNode.test.ts @@ -121,6 +121,15 @@ describe('GroupNodeConfig.getLinks', () => { const config = configFrom([], [[0, 1, 'IMAGE']]) expect(config.externalFrom).toEqual({ 0: { 1: 'IMAGE' } }) }) + + it('normalizes a missing primitive link type to null', () => { + const linkWithoutType = [0, 0, 1, 0, 1] as unknown as GroupNodeLink + const config = configFrom([linkWithoutType]) + + expect( + config.getNodeDef({ index: 0, type: 'PrimitiveNode' })?.output + ).toEqual([null]) + }) }) describe('findUnconsumedWidgetIndex', () => { diff --git a/src/extensions/core/groupNode.ts b/src/extensions/core/groupNode.ts index f01dcb8fd54..1d97a91ae61 100644 --- a/src/extensions/core/groupNode.ts +++ b/src/extensions/core/groupNode.ts @@ -230,8 +230,11 @@ export class GroupNodeConfig { if (!linksFrom) return const firstLink = linksFrom[0]?.at(0) + const linkedType: unknown = firstLink?.at(5) let type: string | number | null = firstLink - ? slotTypeKey(firstLink[5]) + ? linkedType == null + ? null + : slotTypeKey(linkedType as ISlotType) : null if (type === 'COMBO') { // Use the array items diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index a141af972bb..16bf07e7620 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -125,6 +125,53 @@ describe('clipboard ID allocation', () => { expect(before).toHaveBeenCalledOnce() expect(after).toHaveBeenCalledOnce() }) + + it('clones extension values that structuredClone cannot clone', () => { + const nodeType = 'test/clipboard-function-property' + registerClipboardNodeType(nodeType) + const rootGraph = new LGraph() + const canvas = createCanvas(rootGraph) + const extensionCallback = () => 'extension value' + const node = createSerialisedNode(1, nodeType) + node.properties = { extensionCallback } + + const result = canvas._deserializeItems({ nodes: [node] }, {}) + + const pasted = [...(result?.nodes.values() ?? [])][0] + expect(pasted).toBeDefined() + expect(pasted.properties.extensionCallback).toBe(extensionCallback) + expect(node.id).toBe(1) + }) + + it('rolls back every created item when paste throws part-way through', () => { + const workingType = 'test/clipboard-rollback-working' + const throwingType = 'test/clipboard-rollback-throwing' + registerClipboardNodeType(workingType) + class ThrowingClipboardNode extends LGraphNode { + override configure(info: ISerialisedNode): void { + super.configure(info) + throw new Error('configure failed') + } + } + LiteGraph.registerNodeType(throwingType, ThrowingClipboardNode) + const rootGraph = new LGraph() + const canvas = createCanvas(rootGraph) + + expect(() => + canvas._deserializeItems( + { + groups: [{ id: 1, title: 'Group', bounding: [0, 0, 100, 100] }], + nodes: [ + createSerialisedNode(1, workingType), + createSerialisedNode(2, throwingType) + ] + }, + {} + ) + ).toThrow('configure failed') + expect(rootGraph.nodes).toEqual([]) + expect(rootGraph.groups).toEqual([]) + }) }) function createCanvas(graph: LGraph): LGraphCanvas { diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 3691db9e0fb..460359f9f4e 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -1,5 +1,5 @@ import { default as DOMPurify } from 'dompurify' -import { toString } from 'es-toolkit/compat' +import { cloneDeep, toString } from 'es-toolkit/compat' import { toValue } from 'vue' import { isMiddleButtonEvent } from '@/base/pointerUtils' @@ -4288,10 +4288,11 @@ export class LGraphCanvas implements CustomEventDispatcher graph.beforeChange() this.emitBeforeChange() + let result: ClipboardPasteResult | undefined try { const items = initializeClipboardItems(clipboardItems) const [offsetX, offsetY] = clipboardOffset(items) - const result = createClipboardPasteResult() + result = createClipboardPasteResult() const context: ClipboardPasteContext = { connectInputs, dx: position[0] - offsetX, @@ -4317,6 +4318,9 @@ export class LGraphCanvas implements CustomEventDispatcher forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) return result + } catch (error) { + if (result) rollbackClipboardPaste(graph, result) + throw error } finally { graph.afterChange() this.emitAfterChange() @@ -8797,7 +8801,7 @@ export class LGraphCanvas implements CustomEventDispatcher function initializeClipboardItems( clipboardItems: ClipboardItems ): InitializedClipboardItems { - const items = structuredClone(clipboardItems) + const items = cloneDeep(clipboardItems) return { groups: items.groups ?? [], links: items.links ?? [], @@ -8831,6 +8835,32 @@ function createClipboardPasteResult(): ClipboardPasteResult { } } +function rollbackClipboardPaste( + graph: LGraph, + result: ClipboardPasteResult +): void { + for (const item of [...result.created].reverse()) { + try { + if (item instanceof LGraphNode || item instanceof LGraphGroup) { + graph.remove(item) + } else if (item instanceof Reroute) { + graph.removeReroute(item.id) + } + } catch (error) { + console.error('Failed to fully roll back a pasted canvas item', error) + } + } + + const registeredSubgraphs = [...result.subgraphs.values()].filter( + (subgraph) => graph.rootGraph.subgraphs.get(subgraph.id) === subgraph + ) + try { + graph.releaseSubgraphs(registeredSubgraphs) + } catch (error) { + console.error('Failed to fully roll back pasted subgraphs', error) + } +} + function remapClipboardSubgraphIds(items: InitializedClipboardItems): void { const remappedIds = new Map() for (const subgraph of items.subgraphs) { @@ -8878,11 +8908,11 @@ function createClipboardNodes(context: ClipboardPasteContext): void { const linkByInputName = detachSerialisedLinks(info) node.pos = [info.pos[0] + dx, info.pos[1] + dy] graph.add(node) + result.created.push(node) node.configure(info) recordClipboardTargetSlots(info, linkByInputName, targetSlotByLink) configurePastedSubgraphNode(node, info) - result.created.push(node) } } diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 7732c762360..9c44df82657 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -185,7 +185,7 @@ describe('idAllocation', () => { expect(mintGroupId(state, new Set([2]))).toBe(3) }) - it('collects reservations only when the counter reaches its boundary', () => { + it('checks thunk reservations before taking the fast path', () => { const state = createLGraphState() let collections = 0 const collectReservedIds = () => { @@ -193,12 +193,52 @@ describe('idAllocation', () => { return new Set([1]) } - expect(mintGroupId(state, collectReservedIds)).toBe(1) - expect(collections).toBe(0) + expect(mintGroupId(state, collectReservedIds)).toBe(2) + expect(collections).toBe(1) state.lastGroupId = Number.MAX_SAFE_INTEGER expect(mintGroupId(state, collectReservedIds)).toBe(2) - expect(collections).toBe(1) + expect(collections).toBe(2) + }) + + it.for([ + { + name: 'node', + setCounter: (state: ReturnType) => { + state.lastNodeId = Number.MAX_SAFE_INTEGER + }, + mint: (state: ReturnType) => + Number(mintNodeId(state, 'sequential', new Set())) + }, + { + name: 'group', + setCounter: (state: ReturnType) => { + state.lastGroupId = Number.MAX_SAFE_INTEGER + }, + mint: (state: ReturnType) => + Number(mintGroupId(state, new Set())) + }, + { + name: 'link', + setCounter: (state: ReturnType) => { + state.lastLinkId = toLinkId(Number.MAX_SAFE_INTEGER) + }, + mint: (state: ReturnType) => + Number(mintLinkId(state, new Set())) + }, + { + name: 'reroute', + setCounter: (state: ReturnType) => { + state.lastRerouteId = toRerouteId(Number.MAX_SAFE_INTEGER) + }, + mint: (state: ReturnType) => + Number(mintRerouteId(state, new Set())) + } + ])('advances $name allocation after wrapping', ({ setCounter, mint }) => { + const state = createLGraphState() + setCounter(state) + + expect([mint(state), mint(state)]).toEqual([1, 2]) }) it('wraps a reserved maximum candidate to the first available ID', () => { diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index 4d4c8a7266b..197e06f97b4 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -41,16 +41,12 @@ export function findNextAvailableId( } function mintSequentialId(lastId: number, reservedIds: ReservedIds): number { + const usedIds = + typeof reservedIds === 'function' ? reservedIds() : reservedIds if (Number.isSafeInteger(lastId) && lastId >= 0) { const nextId = lastId + 1 - if ( - Number.isSafeInteger(nextId) && - (typeof reservedIds === 'function' || !reservedIds.has(nextId)) - ) - return nextId + if (Number.isSafeInteger(nextId) && !usedIds.has(nextId)) return nextId } - const usedIds = - typeof reservedIds === 'function' ? reservedIds() : reservedIds return findNextAvailableId(usedIds, lastId + 1) } @@ -168,7 +164,7 @@ export function mintNodeId( ): NodeId { if (mode === 'crdt-disjoint') return mintCrdtDisjointNodeId() const id = mintSequentialId(state.lastNodeId, reservedIds) - if (id > state.lastNodeId) state.lastNodeId = id + state.lastNodeId = id return toNodeId(id) } @@ -177,7 +173,7 @@ export function mintGroupId( reservedIds: ReservedIds ): GroupId { const id = mintSequentialId(state.lastGroupId, reservedIds) - if (id > state.lastGroupId) state.lastGroupId = id + state.lastGroupId = id return toGroupId(id) } @@ -187,7 +183,7 @@ export function mintLinkId( ): LinkId { const lastLinkId = Number(state.lastLinkId) const id = mintSequentialId(lastLinkId, reservedIds) - if (id > lastLinkId) state.lastLinkId = toLinkId(id) + state.lastLinkId = toLinkId(id) return toLinkId(id) } @@ -197,7 +193,7 @@ export function mintRerouteId( ): RerouteId { const lastRerouteId = Number(state.lastRerouteId) const id = mintSequentialId(lastRerouteId, reservedIds) - if (id > lastRerouteId) state.lastRerouteId = toRerouteId(id) + state.lastRerouteId = toRerouteId(id) return toRerouteId(id) } diff --git a/src/platform/workflow/validation/schemas/workflowSchema.test.ts b/src/platform/workflow/validation/schemas/workflowSchema.test.ts index 218854e031e..d689c5d4ce9 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.test.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.test.ts @@ -397,9 +397,11 @@ describe('parseComfyWorkflow', () => { describe('zClipboardItems', () => { it('normalizes array-like widget values and preserves array slot types', () => { - const node = JSON.parse(JSON.stringify(defaultGraph.nodes[0])) - node.inputs = [{ name: 'input', type: ['IMAGE', 'MASK'] }] - node.widgets_values = { 0: 'first', 1: 2, length: 2 } + const node = { + ...structuredClone(defaultGraph.nodes[0]), + inputs: [{ name: 'input', type: ['IMAGE', 'MASK'] }], + widgets_values: { 0: 'first', 1: 2, length: 2 } + } const result = zClipboardItems.safeParse({ nodes: [node] }) @@ -410,8 +412,10 @@ describe('zClipboardItems', () => { }) it('preserves indices when normalizing sparse widget values', () => { - const node = JSON.parse(JSON.stringify(defaultGraph.nodes[0])) - node.widgets_values = { 0: 'first', 2: 'third', length: 3 } + const node = { + ...structuredClone(defaultGraph.nodes[0]), + widgets_values: { 0: 'first', 2: 'third', length: 3 } + } const result = zClipboardItems.safeParse({ nodes: [node] }) @@ -425,9 +429,40 @@ describe('zClipboardItems', () => { }) it('rejects an impractically large sparse widget values length', () => { - const node = JSON.parse(JSON.stringify(defaultGraph.nodes[0])) - node.widgets_values = { 0: 'first', length: 10_001 } + const node = { + ...structuredClone(defaultGraph.nodes[0]), + widgets_values: { 0: 'first', length: 10_001 } + } expect(zClipboardItems.safeParse({ nodes: [node] }).success).toBe(false) }) + + it('preserves named-record widget values as named values', () => { + const node = { + ...structuredClone(defaultGraph.nodes[0]), + widgets_values: { seed: 42, steps: 20 } + } + + const result = zClipboardItems.safeParse({ nodes: [node] }) + + expect(result.success).toBe(true) + if (!result.success) throw result.error + expect(result.data.nodes?.[0].widgets_values).toBeUndefined() + expect(result.data.nodes?.[0].widgets_values_named).toEqual({ + seed: 42, + steps: 20 + }) + }) + + it('defaults omitted legacy group and reroute fields', () => { + const result = zClipboardItems.safeParse({ + groups: [{ title: 'Legacy group', bounding: [0, 0, 100, 100] }], + reroutes: [{ id: 4, pos: [10, 20] }] + }) + + expect(result.success).toBe(true) + if (!result.success) throw result.error + expect(result.data.groups?.[0].id).toBe(-1) + expect(result.data.reroutes?.[0].linkIds).toEqual([]) + }) }) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.ts b/src/platform/workflow/validation/schemas/workflowSchema.ts index 4f8e7c5513c..f5963f51ad9 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.ts @@ -265,6 +265,20 @@ function normalizeClipboardWidgetValues( return normalized } +function normalizeClipboardNodeWidgets< + T extends { widgets_values?: z.output } +>(node: T) { + const values = node.widgets_values + if (values && !Array.isArray(values) && typeof values.length !== 'number') { + return { + ...node, + widgets_values: undefined, + widgets_values_named: { ...values } + } + } + return { ...node, widgets_values: normalizeClipboardWidgetValues(values) } +} + const zComfyNode = z .object({ id: zNodeId, @@ -562,12 +576,7 @@ const zClipboardNode = zComfyNode message: 'Clipboard widget values length is too large' } ) - .transform((node) => { - return { - ...node, - widgets_values: normalizeClipboardWidgetValues(node.widgets_values) - } - }) + .transform(normalizeClipboardNodeWidgets) const zClipboardSubgraphInstance = zSubgraphInstance .refine( @@ -577,15 +586,16 @@ const zClipboardSubgraphInstance = zSubgraphInstance message: 'Clipboard widget values length is too large' } ) - .transform((node) => { - return { - ...node, - widgets_values: normalizeClipboardWidgetValues(node.widgets_values) - } - }) - -const zClipboardGroup = zGroup.extend({ id: z.number() }) -const zClipboardReroute = zReroute.extend({ linkIds: z.array(z.number()) }) + .transform(normalizeClipboardNodeWidgets) + +const zClipboardGroup = zGroup.transform((group) => ({ + ...group, + id: group.id ?? -1 +})) +const zClipboardReroute = zReroute.transform((reroute) => ({ + ...reroute, + linkIds: reroute.linkIds ?? [] +})) interface ClipboardSubgraphInput extends Omit< z.input, From d45e6f838349a17367574cafca721473045bed59 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 08:28:35 -0700 Subject: [PATCH 04/18] test(litegraph): expect wrapped link counter advancement --- .../src/subgraph/subgraphDeduplication.test.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts index 5f24905342d..9d0ba1a4e37 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts @@ -407,7 +407,20 @@ describe('normalizeSubgraphDefinitions', () => { ) expect(result.subgraphs[0].links![0].id).toBe(toLinkId(2)) - expect(state.lastLinkId).toBe(toLinkId(Number.MAX_SAFE_INTEGER)) + expect(state.lastLinkId).toBe(toLinkId(2)) + + const next = normalizeSubgraphDefinitions( + [{ ...makeSubgraph('next'), links: [chainedLink(1)] }], + { + nodeIds: new Set(), + groupIds: new Set(), + linkIds: new Set([1, 2]), + rerouteIds: new Set() + }, + state + ) + expect(next.subgraphs[0].links![0].id).toBe(toLinkId(3)) + expect(state.lastLinkId).toBe(toLinkId(3)) }) it('updates every root-level reference to a remapped interior node', () => { From 76e842e079e923f33688d4ba77edb9793b494dae Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 08:28:52 -0700 Subject: [PATCH 05/18] fix(group-node): validate primitive link slot types Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/19239#discussion_r4123855151 --- src/extensions/core/groupNode.test.ts | 12 ++++++++++++ src/extensions/core/groupNode.ts | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/extensions/core/groupNode.test.ts b/src/extensions/core/groupNode.test.ts index 52f45df9ec1..b7bf8146711 100644 --- a/src/extensions/core/groupNode.test.ts +++ b/src/extensions/core/groupNode.test.ts @@ -130,6 +130,18 @@ describe('GroupNodeConfig.getLinks', () => { config.getNodeDef({ index: 0, type: 'PrimitiveNode' })?.output ).toEqual([null]) }) + + it.each([{ unexpected: true }, ['IMAGE', 42]])( + 'normalizes an invalid primitive link type to null', + (invalidType) => { + const link = [0, 0, 1, 0, 1, invalidType] as unknown as GroupNodeLink + const config = configFrom([link]) + + expect( + config.getNodeDef({ index: 0, type: 'PrimitiveNode' })?.output + ).toEqual([null]) + } + ) }) describe('findUnconsumedWidgetIndex', () => { diff --git a/src/extensions/core/groupNode.ts b/src/extensions/core/groupNode.ts index 1d97a91ae61..de127124ec6 100644 --- a/src/extensions/core/groupNode.ts +++ b/src/extensions/core/groupNode.ts @@ -60,6 +60,14 @@ type SlotLinks = Partial> type LinksFromMap = Partial< Record>> > + +function isSlotType(value: unknown): value is ISlotType { + return ( + typeof value === 'number' || + typeof value === 'string' || + (Array.isArray(value) && value.every((item) => typeof item === 'string')) + ) +} type LinksToMap = Partial> type ExternalFromMap = Partial< Record>> @@ -234,7 +242,9 @@ export class GroupNodeConfig { let type: string | number | null = firstLink ? linkedType == null ? null - : slotTypeKey(linkedType as ISlotType) + : isSlotType(linkedType) + ? slotTypeKey(linkedType) + : null : null if (type === 'COMBO') { // Use the array items From 1a5b87f4ed07d4fb36c72409d139ebd7c689cf95 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 08:29:23 -0700 Subject: [PATCH 06/18] perf(litegraph): avoid collecting node IDs on sequential mint Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/19239#discussion_r4123855165 --- src/lib/litegraph/src/LGraph.ts | 22 ++++++++++++-------- src/lib/litegraph/src/idAllocation.test.ts | 24 ++++++++++++++++++++-- src/lib/litegraph/src/idAllocation.ts | 24 ++++++++++++++++++---- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 55965ecf2d0..6a9e1497563 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -1406,17 +1406,23 @@ export class LGraph throw 'LiteGraph: max number of nodes in a graph reached' } - const collectReservedIds = () => - new Set( - [...collectReservedNodeIds(this.rootGraph)] - .map(Number) - .filter(Number.isSafeInteger) - ) + const reservedNodeIds = { + has: (candidate: number) => + [this.rootGraph, ...this.rootGraph.subgraphs.values()].some( + (owner) => owner.getNodeById(toNodeId(candidate)) != null + ), + collect: () => + new Set( + [...collectReservedNodeIds(this.rootGraph)] + .map(Number) + .filter(Number.isSafeInteger) + ) + } // give him an id if (node.id === UNASSIGNED_NODE_ID) { const mintMode = nodeIdMintModeFor(this) - node.id = mintNodeId(state, mintMode, collectReservedIds) + node.id = mintNodeId(state, mintMode, reservedNodeIds) } else { observeNodeId(state, node.id) } @@ -1431,7 +1437,7 @@ export class LGraph node.graph = this attachNodeToStores(this, node, () => - mintNodeId(state, nodeIdMintModeFor(this), collectReservedIds) + mintNodeId(state, nodeIdMintModeFor(this), reservedNodeIds) ) this._nodes.push(node) diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 9c44df82657..59c9ff63425 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -194,11 +194,31 @@ describe('idAllocation', () => { } expect(mintGroupId(state, collectReservedIds)).toBe(2) - expect(collections).toBe(1) + expect(collections).toBe(2) state.lastGroupId = Number.MAX_SAFE_INTEGER expect(mintGroupId(state, collectReservedIds)).toBe(2) - expect(collections).toBe(2) + expect(collections).toBe(3) + }) + + it('checks an indexed reservation without collecting on the fast path', () => { + const state = createLGraphState() + state.lastGroupId = 1 + let collections = 0 + const reservedIds = { + has: (id: number) => id === 99, + collect: () => { + collections++ + return new Set([99]) + } + } + + expect(mintGroupId(state, reservedIds)).toBe(2) + expect(collections).toBe(0) + + state.lastGroupId = Number.MAX_SAFE_INTEGER + expect(mintGroupId(state, reservedIds)).toBe(1) + expect(collections).toBe(1) }) it.for([ diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index 197e06f97b4..3f7c43412c7 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -15,7 +15,15 @@ export interface LGraphState { lastRerouteId: RerouteId } -type ReservedIds = ReadonlySet | (() => ReadonlySet) +interface ReservedIdIndex { + has(id: number): boolean + collect(): ReadonlySet +} + +type ReservedIds = + | ReadonlySet + | (() => ReadonlySet) + | ReservedIdIndex export function createLGraphState(): LGraphState { return { @@ -41,12 +49,20 @@ export function findNextAvailableId( } function mintSequentialId(lastId: number, reservedIds: ReservedIds): number { - const usedIds = - typeof reservedIds === 'function' ? reservedIds() : reservedIds + const hasReservedId = (id: number) => + typeof reservedIds === 'function' + ? reservedIds().has(id) + : reservedIds.has(id) if (Number.isSafeInteger(lastId) && lastId >= 0) { const nextId = lastId + 1 - if (Number.isSafeInteger(nextId) && !usedIds.has(nextId)) return nextId + if (Number.isSafeInteger(nextId) && !hasReservedId(nextId)) return nextId } + const usedIds = + typeof reservedIds === 'function' + ? reservedIds() + : 'collect' in reservedIds + ? reservedIds.collect() + : reservedIds return findNextAvailableId(usedIds, lastId + 1) } From 902477271898913eed0166b5f498efe1e76207d8 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 08:29:55 -0700 Subject: [PATCH 07/18] fix(litegraph): force removal during paste rollback Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/19239#discussion_r4123855169 --- src/lib/litegraph/src/LGraph.ts | 8 +++++++- src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts | 8 +++++++- src/lib/litegraph/src/LGraphCanvas.ts | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 6a9e1497563..2e5371d85c2 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -241,6 +241,8 @@ export interface GraphAddOptions { /** Options for {@link LGraph.remove} method. */ export interface GraphRemoveOptions { + /** Remove an item even when it normally opts out via `ignore_remove`. */ + force?: boolean /** * Detach an adapter after another authority has reconciled canonical stores. * Same-id replacement state is left intact. @@ -1513,7 +1515,11 @@ export class LGraph return } // cannot be removed - if (node.ignore_remove && !options.preserveCanonicalState) { + if ( + node.ignore_remove && + !options.force && + !options.preserveCanonicalState + ) { console.warn('LiteGraph: node cannot be removed', node) return } diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index 16bf07e7620..c51046f543c 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -146,13 +146,19 @@ describe('clipboard ID allocation', () => { it('rolls back every created item when paste throws part-way through', () => { const workingType = 'test/clipboard-rollback-working' const throwingType = 'test/clipboard-rollback-throwing' - registerClipboardNodeType(workingType) + class ProtectedClipboardNode extends LGraphNode { + override configure(info: ISerialisedNode): void { + super.configure(info) + this.ignore_remove = true + } + } class ThrowingClipboardNode extends LGraphNode { override configure(info: ISerialisedNode): void { super.configure(info) throw new Error('configure failed') } } + LiteGraph.registerNodeType(workingType, ProtectedClipboardNode) LiteGraph.registerNodeType(throwingType, ThrowingClipboardNode) const rootGraph = new LGraph() const canvas = createCanvas(rootGraph) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 460359f9f4e..766f45eefea 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -8842,7 +8842,7 @@ function rollbackClipboardPaste( for (const item of [...result.created].reverse()) { try { if (item instanceof LGraphNode || item instanceof LGraphGroup) { - graph.remove(item) + graph.remove(item, { force: true }) } else if (item instanceof Reroute) { graph.removeReroute(item.id) } From 65b8b6178940d028c3913fffa0d4ff0444792aa5 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 08:48:00 -0700 Subject: [PATCH 08/18] test(group-node): use supported parameterized API --- src/extensions/core/groupNode.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/extensions/core/groupNode.test.ts b/src/extensions/core/groupNode.test.ts index b7bf8146711..43563e6c03c 100644 --- a/src/extensions/core/groupNode.test.ts +++ b/src/extensions/core/groupNode.test.ts @@ -1,7 +1,7 @@ import { fromAny, fromPartial } from '@total-typescript/shoehorn' import { readFileSync } from 'node:fs' import { join } from 'node:path' -import { describe, expect, it, vi } from 'vitest' +import { describe, expect, it, test, vi } from 'vitest' import { t } from '@/i18n' @@ -131,7 +131,7 @@ describe('GroupNodeConfig.getLinks', () => { ).toEqual([null]) }) - it.each([{ unexpected: true }, ['IMAGE', 42]])( + test.for([{ unexpected: true }, ['IMAGE', 42]])( 'normalizes an invalid primitive link type to null', (invalidType) => { const link = [0, 0, 1, 0, 1, invalidType] as unknown as GroupNodeLink From 3503b1f73b1f22229582a711bc9d97a2a131dbb4 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 09:07:11 -0700 Subject: [PATCH 09/18] test(litegraph): cover indexed ID collision fallback --- src/lib/litegraph/src/idAllocation.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 59c9ff63425..55f2e10b643 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -221,6 +221,17 @@ describe('idAllocation', () => { expect(collections).toBe(1) }) + it('skips an occupied indexed fast-path candidate', () => { + const state = createLGraphState() + state.lastGroupId = 1 + const reservedIds = { + has: (id: number) => id === 2, + collect: () => new Set([2]) + } + + expect(mintGroupId(state, reservedIds)).toBe(3) + }) + it.for([ { name: 'node', From 7daf736ff002b52f09db52eecea3c3a9fb408d81 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 09:50:48 -0700 Subject: [PATCH 10/18] fix(litegraph): make paste creation rollback-safe --- src/lib/litegraph/src/LGraph.ts | 11 +- .../src/LGraphCanvas.clipboard.test.ts | 111 ++++++++++++++++++ src/lib/litegraph/src/LGraphCanvas.ts | 9 +- 3 files changed, 126 insertions(+), 5 deletions(-) diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 2e5371d85c2..b5becf33eb3 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -2174,9 +2174,14 @@ export class LGraph const subgraphs = data.map((definition) => this.createNormalizedSubgraph(definition) ) - for (const definition of topologicalSortSubgraphs(data)) - this.subgraphs.get(definition.id)?.configure(definition) - return subgraphs + try { + for (const definition of topologicalSortSubgraphs(data)) + this.subgraphs.get(definition.id)?.configure(definition) + return subgraphs + } catch (error) { + this.releaseSubgraphs(subgraphs) + throw error + } } private createNormalizedSubgraph(normalized: ExportedSubgraph): Subgraph { diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index c51046f543c..97e8746c4ff 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -24,6 +24,7 @@ import { LGraphCanvas, LGraphNode, LiteGraph, + Subgraph, SubgraphNode, createUuidv4 } from '@/lib/litegraph/src/litegraph' @@ -178,8 +179,118 @@ describe('clipboard ID allocation', () => { expect(rootGraph.nodes).toEqual([]) expect(rootGraph.groups).toEqual([]) }) + + it('rolls back a node inserted before its onAdded callback throws', () => { + const nodeType = 'test/clipboard-rollback-on-added' + let callbackRan = false + class ThrowingOnAddedNode extends LGraphNode { + override onAdded(): void { + callbackRan = true + throw new Error('onAdded failed') + } + } + LiteGraph.registerNodeType(nodeType, ThrowingOnAddedNode) + const rootGraph = new LGraph() + const canvas = createCanvas(rootGraph) + + expect(() => + canvas._deserializeItems( + { nodes: [createSerialisedNode(1, nodeType)] }, + {} + ) + ).toThrow('onAdded failed') + expect(callbackRan).toBe(true) + expect(rootGraph.nodes).toEqual([]) + }) + + it('releases registered subgraphs when their configuration throws', () => { + const rootGraph = new LGraph() + const canvas = createCanvas(rootGraph) + const subgraph = createClipboardSubgraph(createUuidv4()) + let configureRan = false + const configure = vi + .spyOn(Subgraph.prototype, 'configure') + .mockImplementationOnce(() => { + configureRan = true + throw new Error('subgraph configure failed') + }) + onTestFinished(() => configure.mockRestore()) + + expect(() => + canvas._deserializeItems({ subgraphs: [subgraph] }, {}) + ).toThrow('subgraph configure failed') + expect(configureRan).toBe(true) + expect(rootGraph.subgraphs.size).toBe(0) + }) + + it('rolls back nodes, links, and reroutes after a late lifecycle failure', () => { + const nodeType = 'test/clipboard-rollback-late' + let callbackRan = false + class ThrowingConfiguredNode extends LGraphNode { + override onGraphConfigured(): void { + callbackRan = true + throw new Error('graph configured failed') + } + } + LiteGraph.registerNodeType(nodeType, ThrowingConfiguredNode) + const rootGraph = new LGraph() + const canvas = createCanvas(rootGraph) + const origin = createSerialisedNode(1, nodeType) + const target = createSerialisedNode(2, nodeType) + origin.outputs = [{ name: 'output', type: '*', links: [1] }] + target.inputs = [{ name: 'input', type: '*', link: 1 }] + + expect(() => + canvas._deserializeItems( + { + nodes: [origin, target], + links: [ + { + id: 1, + origin_id: 1, + origin_slot: 0, + target_id: 2, + target_slot: 0, + type: '*', + parentId: 1 + } + ], + reroutes: [{ id: 1, pos: [20, 20], linkIds: [1] }] + }, + {} + ) + ).toThrow('graph configured failed') + expect(callbackRan).toBe(true) + expect(rootGraph.nodes).toEqual([]) + expect(rootGraph.links.size).toBe(0) + expect(rootGraph.reroutes.size).toBe(0) + }) }) +function createClipboardSubgraph(id: string): ExportedSubgraph { + return { + id, + version: 1, + revision: 0, + state: { + lastNodeId: 0, + lastLinkId: 0, + lastGroupId: 0, + lastRerouteId: 0 + }, + config: {}, + name: 'Pasted Subgraph', + inputNode: { id: SUBGRAPH_INPUT_ID, bounding: [0, 0, 10, 10] }, + outputNode: { id: SUBGRAPH_OUTPUT_ID, bounding: [0, 0, 10, 10] }, + inputs: [], + outputs: [], + widgets: [], + nodes: [], + links: [], + groups: [] + } +} + function createCanvas(graph: LGraph): LGraphCanvas { const el = document.createElement('canvas') el.width = 800 diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 766f45eefea..028b246d64f 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -8907,8 +8907,13 @@ function createClipboardNodes(context: ClipboardPasteContext): void { info.id = -1 const linkByInputName = detachSerialisedLinks(info) node.pos = [info.pos[0] + dx, info.pos[1] + dy] - graph.add(node) - result.created.push(node) + try { + graph.add(node) + result.created.push(node) + } catch (error) { + if (graph.nodes.includes(node)) result.created.push(node) + throw error + } node.configure(info) recordClipboardTargetSlots(info, linkByInputName, targetSlotByLink) From 723150bbee0abe22d4b8e91009a9d1cdb86802d6 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 12:56:05 -0700 Subject: [PATCH 11/18] test(litegraph): align failed configure rollback pins --- .../src/LGraph.failedConfigure.test.ts | 37 ++++++++++++------- src/lib/litegraph/src/LGraph.ts | 11 +++++- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/lib/litegraph/src/LGraph.failedConfigure.test.ts b/src/lib/litegraph/src/LGraph.failedConfigure.test.ts index 38aac590d06..9b03d6efb7f 100644 --- a/src/lib/litegraph/src/LGraph.failedConfigure.test.ts +++ b/src/lib/litegraph/src/LGraph.failedConfigure.test.ts @@ -294,7 +294,7 @@ describe('LGraph.configure that throws partway through', () => { expect(configuredEvents).toBe(1) }) - it('LEAK: a nested definition that fails stays registered on an otherwise empty graph', () => { + it('releases a nested definition that fails on an otherwise empty graph', () => { const graph = new LGraph() const created: string[] = [] graph.events.addEventListener('subgraph-created', (event) => { @@ -304,7 +304,7 @@ describe('LGraph.configure that throws partway through', () => { expect(() => graph.configure(failingNestedWorkflow())).toThrow() expect(created).toEqual([NESTED_DEFINITION_ID]) - expect(graph.subgraphs.has(NESTED_DEFINITION_ID)).toBe(true) + expect(graph.subgraphs.has(NESTED_DEFINITION_ID)).toBe(false) expect(graph.empty).toBe(true) }) }) @@ -370,29 +370,38 @@ describe('a workflow loaded after a failed load, on the same graph', () => { }) it('clears nested-owner state before loading the next workflow', () => { - const nested = graphAfterFailedConfigure(failingNestedWorkflow()) - const definition = nested.subgraphs.get(NESTED_DEFINITION_ID) - if (!definition) throw new Error('Expected failed subgraph definition') + const nested = new LGraph() + let failedDefinition: LGraph | undefined + nested.events.addEventListener('subgraph-created', (event) => { + failedDefinition = event.detail.subgraph + }) + expect(() => nested.configure(failingNestedWorkflow())).toThrow( + 'onConfigure exploded' + ) + if (!failedDefinition) + throw new Error('Expected failed subgraph definition') - const scope = graphScopeOf(definition) - const nodeIds = definition.nodes.map((node) => node.id) + const scope = graphScopeOf(failedDefinition) + const nodeIds = failedDefinition.nodes.map((node) => node.id) const linkIds = [...useLinkStore().graphTopologies(scope)].map( (link) => link.id ) - const rerouteIds = [...definition.reroutes.keys()] + const rerouteIds = [...failedDefinition.reroutes.keys()] const widgetIds = nodeIds.flatMap((id) => useWidgetValueStore().getNodeWidgetIds(BAD_ID, id) ) expect(storeOwnership(scope, nodeIds, rerouteIds, [])).toEqual({ - nodes: nodeIds, - links: linkIds, - reroutes: rerouteIds, - nodeLayouts: nodeIds, - rerouteLayouts: rerouteIds, + nodes: [], + links: [], + reroutes: [], + nodeLayouts: [], + rerouteLayouts: [], groupLayouts: [], - widgets: widgetIds + widgets: [] }) + expect(linkIds).toEqual([]) + expect(widgetIds).toEqual([]) nested.configure(unrelatedWorkflow()) diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index b5becf33eb3..9451da84512 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -2179,7 +2179,16 @@ export class LGraph this.subgraphs.get(definition.id)?.configure(definition) return subgraphs } catch (error) { - this.releaseSubgraphs(subgraphs) + try { + this.releaseSubgraphs(subgraphs) + } catch (cleanupError) { + const combinedError = new AggregateError( + [error, cleanupError], + 'Subgraph configuration and rollback both failed', + { cause: error } + ) + throw combinedError + } throw error } } From 36d3dabd9674735f5d3a0b4f6754c6cd4a8c8185 Mon Sep 17 00:00:00 2001 From: Amp Date: Mon, 28 Sep 2026 20:15:11 +0000 Subject: [PATCH 12/18] fix(litegraph): complete safe ID recovery follow-up Amp-Thread-ID: https://ampcode.com/threads/T-01a0e8d6-c302-7581-acfa-2c1a88d59234 --- src/extensions/core/groupNode.test.ts | 4 +- src/extensions/core/groupNode.ts | 4 +- src/lib/litegraph/src/LGraph.ts | 120 ++++++++++-------- .../src/LGraphCanvas.clipboard.test.ts | 72 +++++++++-- src/lib/litegraph/src/LGraphCanvas.ts | 25 ++-- src/lib/litegraph/src/LGraphNode.ts | 7 +- src/lib/litegraph/src/idAllocation.ts | 83 +++++++++++- .../litegraph/src/subgraph/SubgraphInput.ts | 8 +- .../src/subgraph/SubgraphInputNode.ts | 8 +- .../litegraph/src/subgraph/SubgraphOutput.ts | 8 +- .../subgraph/subgraphDeduplication.test.ts | 65 ++++++---- .../src/subgraph/subgraphDeduplication.ts | 102 ++++++--------- .../services/templateInputService.ts | 7 +- .../validation/schemas/workflowSchema.ts | 11 +- 14 files changed, 323 insertions(+), 201 deletions(-) diff --git a/src/extensions/core/groupNode.test.ts b/src/extensions/core/groupNode.test.ts index 43563e6c03c..07e2b2feec7 100644 --- a/src/extensions/core/groupNode.test.ts +++ b/src/extensions/core/groupNode.test.ts @@ -123,7 +123,7 @@ describe('GroupNodeConfig.getLinks', () => { }) it('normalizes a missing primitive link type to null', () => { - const linkWithoutType = [0, 0, 1, 0, 1] as unknown as GroupNodeLink + const linkWithoutType = [0, 0, 1, 0, 1] satisfies GroupNodeLink const config = configFrom([linkWithoutType]) expect( @@ -134,7 +134,7 @@ describe('GroupNodeConfig.getLinks', () => { test.for([{ unexpected: true }, ['IMAGE', 42]])( 'normalizes an invalid primitive link type to null', (invalidType) => { - const link = [0, 0, 1, 0, 1, invalidType] as unknown as GroupNodeLink + const link = [0, 0, 1, 0, 1, invalidType] satisfies GroupNodeLink const config = configFrom([link]) expect( diff --git a/src/extensions/core/groupNode.ts b/src/extensions/core/groupNode.ts index de127124ec6..b132c6b1f6b 100644 --- a/src/extensions/core/groupNode.ts +++ b/src/extensions/core/groupNode.ts @@ -54,7 +54,7 @@ export type GroupNodeLink = [ targetNodeIndex: number, targetSlot: number, sourceNodeId: SerializedNodeId, - type: ISlotType + type?: unknown ] type SlotLinks = Partial> type LinksFromMap = Partial< @@ -327,7 +327,7 @@ export class GroupNodeConfig { } else { // Reroute used as a pipe for (const l of this.nodeData.links) { - if (l[2] === node.index) { + if (l[2] === node.index && isSlotType(l[5])) { rerouteType = l[5] break } diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 9451da84512..0ecf9095842 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -46,7 +46,12 @@ import { isFloatingTopology } from '@/types/linkTopology' import { toRerouteId } from '@/types/rerouteId' import { graphScopeOf, toRootGraphId } from '@/types/graphScopeId' import { + collectReservedGroupIds, + collectReservedLinkIds, + collectReservedNodeIds, + collectReservedRerouteIds, createLGraphState, + linkIdReservations, mintGroupId, mintLinkId, mintNodeId, @@ -54,9 +59,14 @@ import { observeGroupId, observeLinkId, observeNodeId, - observeRerouteId + observeRerouteId, + rerouteIdReservations +} from './idAllocation' +import type { + LGraphState, + NodeIdMintMode, + ReservedIdIndex } from './idAllocation' -import type { LGraphState, NodeIdMintMode } from './idAllocation' import { isRootGraphDocBound } from './docBoundGraphs' import { inputHasLink, outputHasLinks, outputLinks } from './node/slotLinks' import { normalizeWidgetsView } from './node/widgetsView' @@ -185,10 +195,6 @@ import { runExtensionSerializeHook } from './extensionPersistence' import { - collectReservedGroupIds, - collectReservedLinkIds, - collectReservedNodeIds, - collectReservedRerouteIds, normalizeSubgraphDefinitions, topologicalSortSubgraphs } from './subgraph/subgraphDeduplication' @@ -275,6 +281,14 @@ function runtimeOptional(value: T): T | undefined { return value } +function groupIdReservations(rootGraph: LGraph): ReservedIdIndex { + return { + has: (id) => + layoutStore.getGroupLayout(rootGraph.id, toGroupId(id)) != null, + collect: () => collectReservedGroupIds(rootGraph) + } +} + function fireNodeRemovalLifecycle(node: LGraphNode): void { const graph: LGraph | null = node.graph graph?.events.dispatch('node:before-removed', { node }) @@ -1377,9 +1391,7 @@ export class LGraph groupId === -1 || layoutStore.getGroupLayout(this.rootGraph.id, groupId) ) { - node.id = mintGroupId(state, () => - collectReservedGroupIds(this.rootGraph) - ) + node.id = mintGroupId(state, groupIdReservations(this.rootGraph)) } observeGroupId(state, node.id) @@ -1575,46 +1587,48 @@ export class LGraph this.releaseSubgraphs(findReleasableSubgraphs(this.rootGraph, node)) } - // callback - node.onRemoved?.() - if (!successor) clearNodeOwnedStoreState(node) + try { + node.onRemoved?.() + } finally { + const order = node.order + try { + if (!successor) { + clearNodeOwnedStoreState(node) + useExecutionOrderStore().remove(graphScopeOf(this), node.id) + } + if (options.preserveCanonicalState) { + node._graphScope = undefined + releaseNodeLayoutAttachment(node) + } else { + detachNodeFromStores(this, node) + detachNodeLayout(node) + } - const order = node.order - if (!successor) { - useExecutionOrderStore().remove(graphScopeOf(this), node.id) - } - if (options.preserveCanonicalState) { - node._graphScope = undefined - releaseNodeLayoutAttachment(node) - } else { - detachNodeFromStores(this, node) - detachNodeLayout(node) - } + if (!successor) { + const { list_of_graphcanvas } = this + if (list_of_graphcanvas) { + for (const canvas of list_of_graphcanvas) { + delete canvas.selected_nodes[node.id] + canvas.deselect(node) + } + } + useSelectionStore().apply(graphScopeOf(this), { + type: 'selection.remove', + key: toSelectableKey('node', node.id) + }) + } + } finally { + node.graph = null + node.order = order + this.incrementVersion() - node.graph = null - node.order = order - this.incrementVersion() + const pos = this._nodes.indexOf(node) + if (pos != -1) this._nodes.splice(pos, 1) - if (!successor) { - const { list_of_graphcanvas } = this - if (list_of_graphcanvas) { - for (const canvas of list_of_graphcanvas) { - delete canvas.selected_nodes[node.id] - canvas.deselect(node) + if (this._nodes_by_id[node.id] === node) { + delete this._nodes_by_id[node.id] } } - useSelectionStore().apply(graphScopeOf(this), { - type: 'selection.remove', - key: toSelectableKey('node', node.id) - }) - } - - // remove from containers - const pos = this._nodes.indexOf(node) - if (pos != -1) this._nodes.splice(pos, 1) - - if (this._nodes_by_id[node.id] === node) { - delete this._nodes_by_id[node.id] } this.onNodeRemoved?.(node) this.events.dispatch('node:removed', { node }) @@ -1900,9 +1914,7 @@ export class LGraph addFloatingLink(link: LLink): LLink | undefined { if (link.id === -1) { - link.id = mintLinkId(this.state, () => - collectReservedLinkIds(this.rootGraph) - ) + link.id = mintLinkId(this.state, linkIdReservations(this.rootGraph)) } if (!registerLinkTopology(this, link)) return @@ -2010,9 +2022,7 @@ export class LGraph }: OptionalProps): Reroute | undefined { const rerouteId = id === undefined - ? mintRerouteId(this.state, () => - collectReservedRerouteIds(this.rootGraph) - ) + ? mintRerouteId(this.state, rerouteIdReservations(this.rootGraph)) : toRerouteId(id) observeRerouteId(this.state, rerouteId) @@ -2739,8 +2749,9 @@ export class LGraph // Shared definitions may survive, so unpacked groups need fresh layout // ids, like the reroutes below. for (const groupInfo of groups) { - const groupId = mintGroupId(this.rootGraph.state, () => - collectReservedGroupIds(this.rootGraph) + const groupId = mintGroupId( + this.rootGraph.state, + groupIdReservations(this.rootGraph) ) groupInfo.id = groupId const group = new LGraphGroup(groupInfo.title, groupId) @@ -2831,8 +2842,9 @@ export class LGraph const rerouteIdMap = new Map() const oldReroutes = subgraphNode.subgraph.reroutes for (const reroute of oldReroutes.values()) { - const migratedId = mintRerouteId(this.state, () => - collectReservedRerouteIds(this.rootGraph) + const migratedId = mintRerouteId( + this.state, + rerouteIdReservations(this.rootGraph) ) const migratedReroute = this.setReroute({ id: migratedId, diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index 97e8746c4ff..5ad9a8531dc 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -24,7 +24,6 @@ import { LGraphCanvas, LGraphNode, LiteGraph, - Subgraph, SubgraphNode, createUuidv4 } from '@/lib/litegraph/src/litegraph' @@ -203,34 +202,79 @@ describe('clipboard ID allocation', () => { expect(rootGraph.nodes).toEqual([]) }) - it('releases registered subgraphs when their configuration throws', () => { + it('releases registered subgraphs after partial configuration', () => { + const nodeType = 'test/subgraph-partial-configuration' + let removalRan = false + class PartiallyConfiguredNode extends LGraphNode { + override configure(info: ISerialisedNode): void { + super.configure(info) + throw new Error('subgraph configure failed') + } + + override onRemoved(): void { + removalRan = true + } + } + LiteGraph.registerNodeType(nodeType, PartiallyConfiguredNode) const rootGraph = new LGraph() const canvas = createCanvas(rootGraph) const subgraph = createClipboardSubgraph(createUuidv4()) - let configureRan = false - const configure = vi - .spyOn(Subgraph.prototype, 'configure') - .mockImplementationOnce(() => { - configureRan = true - throw new Error('subgraph configure failed') - }) - onTestFinished(() => configure.mockRestore()) + subgraph.nodes = [createSerialisedNode(1, nodeType)] expect(() => canvas._deserializeItems({ subgraphs: [subgraph] }, {}) ).toThrow('subgraph configure failed') - expect(configureRan).toBe(true) + expect(removalRan).toBe(true) + expect(rootGraph.subgraphs.size).toBe(0) + }) + + it('retains configuration and rollback failures', () => { + const nodeType = 'test/subgraph-failed-rollback' + const configurationError = new Error('subgraph configure failed') + const rollbackError = new Error('subgraph rollback failed') + class FailedRollbackNode extends LGraphNode { + override configure(info: ISerialisedNode): void { + super.configure(info) + throw configurationError + } + + override onRemoved(): void { + throw rollbackError + } + } + LiteGraph.registerNodeType(nodeType, FailedRollbackNode) + const rootGraph = new LGraph() + const canvas = createCanvas(rootGraph) + const subgraph = createClipboardSubgraph(createUuidv4()) + subgraph.nodes = [createSerialisedNode(1, nodeType)] + + let thrown: unknown + try { + canvas._deserializeItems({ subgraphs: [subgraph] }, {}) + } catch (error) { + thrown = error + } + + expect(thrown).toBeInstanceOf(AggregateError) + if (!(thrown instanceof AggregateError)) return + expect(thrown.cause).toBe(configurationError) + expect(thrown.errors).toEqual([configurationError, rollbackError]) expect(rootGraph.subgraphs.size).toBe(0) }) it('rolls back nodes, links, and reroutes after a late lifecycle failure', () => { const nodeType = 'test/clipboard-rollback-late' - let callbackRan = false + let topologyExisted = false class ThrowingConfiguredNode extends LGraphNode { override onGraphConfigured(): void { - callbackRan = true + topologyExisted = + this.graph?.links.size === 1 && this.graph.reroutes.size === 1 throw new Error('graph configured failed') } + + override onRemoved(): void { + throw new Error('node removal failed') + } } LiteGraph.registerNodeType(nodeType, ThrowingConfiguredNode) const rootGraph = new LGraph() @@ -260,7 +304,7 @@ describe('clipboard ID allocation', () => { {} ) ).toThrow('graph configured failed') - expect(callbackRan).toBe(true) + expect(topologyExisted).toBe(true) expect(rootGraph.nodes).toEqual([]) expect(rootGraph.links.size).toBe(0) expect(rootGraph.reroutes.size).toBe(0) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 028b246d64f..f72fb17182d 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -4305,10 +4305,16 @@ export class LGraphCanvas implements CustomEventDispatcher remapClipboardSubgraphIds(items) createClipboardSubgraphs(context) - createClipboardGroups(context) + for (const info of items.groups) { + info.id = -1 + const group = new LGraphGroup() + group.configure(info) + graph.add(group) + result.created.push(group) + } createClipboardNodes(context) createClipboardReroutes(context) - createClipboardLinks(context) + for (const info of items.links) createClipboardLink(context, info) removeUnusedClipboardReroutes(context) positionClipboardItems(context) updateClipboardNodeLayout(context) @@ -8886,17 +8892,6 @@ function createClipboardSubgraphs(context: ClipboardPasteContext): void { } } -function createClipboardGroups(context: ClipboardPasteContext): void { - const { graph, items, result } = context - for (const info of items.groups) { - info.id = -1 - const group = new LGraphGroup() - group.configure(info) - graph.add(group) - result.created.push(group) - } -} - function createClipboardNodes(context: ClipboardPasteContext): void { const { dx, dy, graph, items, result, targetSlotByLink } = context for (const info of items.nodes) { @@ -8964,10 +8959,6 @@ function createClipboardReroutes(context: ClipboardPasteContext): void { } } -function createClipboardLinks(context: ClipboardPasteContext): void { - for (const info of context.items.links) createClipboardLink(context, info) -} - function createClipboardLink( context: ClipboardPasteContext, info: InitializedClipboardItems['links'][number] diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 98f24c63527..ac8da3c9950 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -27,7 +27,7 @@ import { useWidgetValueStore } from '@/stores/widgetValueStore' import { graphScopeOf } from '@/types/graphScopeId' import { toLinkId } from '@/types/linkId' import type { GraphScope } from '@/types/graphScopeId' -import { mintLinkId } from './idAllocation' +import { linkIdReservations, mintLinkId } from './idAllocation' import { UNASSIGNED_NODE_ID, toNodeId, serializeNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' import type { NodeProperty, NodeState } from '@/types/nodeState' @@ -75,7 +75,6 @@ import { resolveInputSlotView } from './node/slotDescriptorView' import { initializeWidgetsView } from './node/widgetsView' -import { collectReservedLinkIds } from './subgraph/subgraphDeduplication' import { extensionConfigureView, hydrateExtensionPayload, @@ -3226,9 +3225,7 @@ export class LGraphNode const maybeCommonType = input.type && output.type && commonType(input.type, output.type) - const linkId = mintLinkId(graph.state, () => - collectReservedLinkIds(graph.rootGraph) - ) + const linkId = mintLinkId(graph.state, linkIdReservations(graph.rootGraph)) const link = new LLink( linkId, diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index 3f7c43412c7..4d64ea0c31d 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -1,3 +1,9 @@ +import type { LGraph } from './LGraph' +import type { + ISerialisedGroup, + ISerialisedNode, + SerialisableLLink +} from './types/serialisation' import { toGroupId } from '@/types/groupId' import type { GroupId } from '@/types/groupId' import { toLinkId } from '@/types/linkId' @@ -15,7 +21,7 @@ export interface LGraphState { lastRerouteId: RerouteId } -interface ReservedIdIndex { +export interface ReservedIdIndex { has(id: number): boolean collect(): ReadonlySet } @@ -34,6 +40,81 @@ export function createLGraphState(): LGraphState { } } +export function collectReservedNodeIds( + rootGraph: Pick, + rootNodes: ISerialisedNode[] = [] +): Set { + return new Set([ + ...rootNodes.map((node) => toNodeId(node.id)), + ...[rootGraph, ...rootGraph.subgraphs.values()].flatMap((owner) => + owner.nodes.map((node) => node.id) + ) + ]) +} + +export function collectReservedGroupIds( + graph: Pick, + serializedGroups: ISerialisedGroup[] = [] +): Set { + return new Set([ + ...serializedGroups.map((group) => group.id), + ...[graph, ...graph.subgraphs.values()].flatMap((owner) => + owner.groups.map((group) => group.id) + ) + ]) +} + +export function collectReservedLinkIds( + graph: Pick, + serializedFloatingLinks: SerialisableLLink[] = [] +): Set { + return new Set([ + ...serializedFloatingLinks.map((link) => link.id), + ...[graph, ...graph.subgraphs.values()].flatMap((owner) => [ + ...owner.links.keys(), + ...owner.floatingLinks.keys() + ]) + ]) +} + +export function linkIdReservations( + graph: Pick +): ReservedIdIndex { + return { + has: (id) => { + const linkId = toLinkId(id) + return [graph, ...graph.subgraphs.values()].some( + (owner) => owner.links.has(linkId) || owner.floatingLinks.has(linkId) + ) + }, + collect: () => collectReservedLinkIds(graph) + } +} + +export function collectReservedRerouteIds( + graph: Pick +): Set { + return new Set( + [graph, ...graph.subgraphs.values()].flatMap((owner) => + [...owner.reroutes.values()].map((reroute) => reroute.id) + ) + ) +} + +export function rerouteIdReservations( + graph: Pick +): ReservedIdIndex { + return { + has: (id) => { + const rerouteId = toRerouteId(id) + return [graph, ...graph.subgraphs.values()].some((owner) => + owner.reroutes.has(rerouteId) + ) + }, + collect: () => collectReservedRerouteIds(graph) + } +} + export function findNextAvailableId( usedIds: ReadonlySet, startAt = 1 diff --git a/src/lib/litegraph/src/subgraph/SubgraphInput.ts b/src/lib/litegraph/src/subgraph/SubgraphInput.ts index 860ab5bf677..ea53c5899ad 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInput.ts @@ -1,7 +1,7 @@ import { inputLink } from '@/lib/litegraph/src/node/slotLinks' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import { LLink, replaceLinkTopology } from '@/lib/litegraph/src/LLink' -import { mintLinkId } from '../idAllocation' +import { linkIdReservations, mintLinkId } from '../idAllocation' import { anchorRerouteChain } from '@/lib/litegraph/src/Reroute' import type { RerouteId } from '@/lib/litegraph/src/Reroute' import { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEventTarget' @@ -19,7 +19,6 @@ import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { SubgraphInputNode } from './SubgraphInputNode' import type { SubgraphOutput } from './SubgraphOutput' import { SubgraphSlot } from './SubgraphSlotBase' -import { collectReservedLinkIds } from './subgraphDeduplication' import { isNodeSlot, isSubgraphOutput } from './subgraphUtils' /** @@ -88,8 +87,9 @@ export class SubgraphInput extends SubgraphSlot { subgraph.beforeChange() } - const linkId = mintLinkId(subgraph.state, () => - collectReservedLinkIds(subgraph.rootGraph) + const linkId = mintLinkId( + subgraph.state, + linkIdReservations(subgraph.rootGraph) ) const link = new LLink( diff --git a/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts b/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts index b5a2072c18b..330f11a3589 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts @@ -2,7 +2,7 @@ import type { CanvasPointer } from '@/lib/litegraph/src/CanvasPointer' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { NodeId } from '@/types/nodeId' import { LLink, slotFloatingLinks } from '@/lib/litegraph/src/LLink' -import { mintLinkId } from '../idAllocation' +import { linkIdReservations, mintLinkId } from '../idAllocation' import type { RerouteId } from '@/lib/litegraph/src/Reroute' import type { LinkConnector } from '@/lib/litegraph/src/canvas/LinkConnector' import { SUBGRAPH_INPUT_ID } from '@/lib/litegraph/src/constants' @@ -23,7 +23,6 @@ import { EmptySubgraphInput } from './EmptySubgraphInput' import { SubgraphIONodeBase } from './SubgraphIONodeBase' import type { SubgraphInput } from './SubgraphInput' import type { SubgraphOutput } from './SubgraphOutput' -import { collectReservedLinkIds } from './subgraphDeduplication' export class SubgraphInputNode extends SubgraphIONodeBase @@ -109,8 +108,9 @@ export class SubgraphInputNode if (outputIndex === -1 || inputIndex === -1) throw new Error('Invalid slot indices.') - const linkId = mintLinkId(subgraph.state, () => - collectReservedLinkIds(subgraph.rootGraph) + const linkId = mintLinkId( + subgraph.state, + linkIdReservations(subgraph.rootGraph) ) return new LLink( diff --git a/src/lib/litegraph/src/subgraph/SubgraphOutput.ts b/src/lib/litegraph/src/subgraph/SubgraphOutput.ts index c627757509f..b7f7c81d25c 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphOutput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphOutput.ts @@ -1,6 +1,6 @@ import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import { LLink, replaceLinkTopology } from '@/lib/litegraph/src/LLink' -import { mintLinkId } from '../idAllocation' +import { linkIdReservations, mintLinkId } from '../idAllocation' import { anchorRerouteChain } from '@/lib/litegraph/src/Reroute' import type { RerouteId } from '@/lib/litegraph/src/Reroute' import type { @@ -15,7 +15,6 @@ import { NodeSlotType } from '@/lib/litegraph/src/types/globalEnums' import type { SubgraphInput } from './SubgraphInput' import type { SubgraphOutputNode } from './SubgraphOutputNode' import { SubgraphSlot } from './SubgraphSlotBase' -import { collectReservedLinkIds } from './subgraphDeduplication' import { isNodeSlot, isSubgraphInput } from './subgraphUtils' /** @@ -59,8 +58,9 @@ export class SubgraphOutput extends SubgraphSlot { subgraph.beforeChange() } - const linkId = mintLinkId(subgraph.state, () => - collectReservedLinkIds(subgraph.rootGraph) + const linkId = mintLinkId( + subgraph.state, + linkIdReservations(subgraph.rootGraph) ) const link = new LLink( diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts index 9d0ba1a4e37..92ae84681d9 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts @@ -2,7 +2,7 @@ import { SUBGRAPH_INPUT_ID, SUBGRAPH_OUTPUT_ID } from '@/lib/litegraph/src/constants' -import { describe, expect, it } from 'vitest' +import { assert, describe, expect, it } from 'vitest' import { toLinkId } from '@/types/linkId' import { toNodeId } from '@/types/nodeId' @@ -13,6 +13,7 @@ import type { LGraphState } from '../LGraph' import type { ExportedSubgraph, ISerialisedGroup, + ISerialisedNode, SerialisableLLink, SerialisableReroute } from '../types/serialisation' @@ -27,8 +28,12 @@ import { topologicalSortSubgraphs } from './subgraphDeduplication' -type NestedExportedSubgraph = ExportedSubgraph & { +type NestedExportedSubgraph = Omit< + ExportedSubgraph, + 'definitions' | 'nodes' +> & { definitions?: { subgraphs?: NestedExportedSubgraph[] } + nodes: ISerialisedNode[] } function makeSubgraph( @@ -77,7 +82,7 @@ describe('normalizeSubgraphDefinitionIds', () => { expect(result.subgraphs).toHaveLength(2) expect(isUuidShapedSubgraphId(normalizedId)).toBe(true) - expect(result.subgraphs[1].nodes![0].type).toBe(normalizedId) + expect(result.subgraphs[1].nodes[0].type).toBe(normalizedId) }) it('recursively normalizes a subgraph-within-subgraph definition and hoists it to the flat result', () => { @@ -105,7 +110,7 @@ describe('normalizeSubgraphDefinitionIds', () => { // The parent's node referencing the nested child by its old id now // references the normalized UUID. expect(normalizedParent.nodes).toHaveLength(1) - expect(normalizedParent.nodes?.[0]?.type).toBe(normalizedChild.id) + expect(normalizedParent.nodes[0].type).toBe(normalizedChild.id) // The now-redundant nested list is cleared: callers read the flat // top-level result, not each definition's own nested subgraphs. @@ -132,9 +137,9 @@ describe('normalizeSubgraphDefinitionIds', () => { expect(isUuidShapedSubgraphId(normalizedChild.id)).toBe(true) expect(isUuidShapedSubgraphId(normalizedGrandchild.id)).toBe(true) expect(normalizedParent.nodes).toHaveLength(1) - expect(normalizedParent.nodes?.[0]?.type).toBe(normalizedChild.id) + expect(normalizedParent.nodes[0].type).toBe(normalizedChild.id) expect(normalizedChild.nodes).toHaveLength(1) - expect(normalizedChild.nodes?.[0]?.type).toBe(normalizedGrandchild.id) + expect(normalizedChild.nodes[0].type).toBe(normalizedGrandchild.id) }) it('handles an explicit empty definitions.subgraphs list without error', () => { @@ -186,7 +191,7 @@ describe('normalizeSubgraphDefinitionIds', () => { expect(siblingIds).not.toContain('legacy-sibling-a') expect(siblingIds).not.toContain('legacy-sibling-b') expect(siblingIds).not.toContain('legacy-sibling-c') - expect(normalizedParent.nodes?.map((node) => node.type)).toEqual(siblingIds) + expect(normalizedParent.nodes.map((node) => node.type)).toEqual(siblingIds) }) }) @@ -267,19 +272,20 @@ describe('deduplicateSubgraphNodeIds', () => { const result = deduplicateSubgraphNodeIds([subgraph], new Set([1]), state) - const remappedNodeId = result.subgraphs[0].nodes?.[0].id - expect(result.subgraphs[0].floatingLinks?.[0].origin_id).toBe( - remappedNodeId - ) + const normalizedSubgraph = result.subgraphs[0] + assert.exists(normalizedSubgraph) + assert.exists(normalizedSubgraph.nodes) + const remappedNode = normalizedSubgraph.nodes[0] + assert.exists(remappedNode) + const remappedNodeId = remappedNode.id + expect(normalizedSubgraph.floatingLinks?.[0].origin_id).toBe(remappedNodeId) }) }) describe('deduplicateSubgraphLinkIds', () => { it('patches every reference to a remapped regular link', () => { const subgraph = makeSubgraph('sg', ['dummy']) - const node = subgraph.nodes?.[0] - expect(node).toBeDefined() - if (!node) return + const node = subgraph.nodes[0] node.inputs = [{ name: 'in', type: 'INT', link: 1 }] node.outputs = [{ name: 'out', type: 'INT', links: [1] }] subgraph.links = [chainedLink(1)] @@ -323,7 +329,7 @@ describe('normalizeSubgraphDefinitions', () => { const duplicate = makeSubgraph('legacy-id') duplicate.name = 'duplicate' const parent = makeSubgraph('parent', ['legacy-id']) - const rootNode = makeSubgraph('root', ['legacy-id']).nodes![0] + const rootNode = makeSubgraph('root', ['legacy-id']).nodes[0] const result = normalizeSubgraphDefinitions( [first, duplicate, parent], @@ -341,8 +347,10 @@ describe('normalizeSubgraphDefinitions', () => { expect(result.subgraphs).toHaveLength(2) expect(result.subgraphs[0].name).toBe('first') expect(isUuidShapedSubgraphId(normalizedLegacyId)).toBe(true) - expect(result.subgraphs[1].nodes![0].type).toBe(normalizedLegacyId) - expect(result.rootNodes![0].type).toBe(normalizedLegacyId) + assert.exists(result.subgraphs[1].nodes) + assert.exists(result.rootNodes) + expect(result.subgraphs[1].nodes[0].type).toBe(normalizedLegacyId) + expect(result.rootNodes[0].type).toBe(normalizedLegacyId) }) it('keeps the first same-owner link across regular and floating links', () => { @@ -364,8 +372,10 @@ describe('normalizeSubgraphDefinitions', () => { expect(result.links).toHaveLength(1) expect(result.floatingLinks).toHaveLength(0) - expect(result.links![0].id).toBe(toLinkId(1)) - expect(result.inputs![0].linkIds).toEqual([toLinkId(1)]) + assert.exists(result.links) + assert.exists(result.inputs) + expect(result.links[0].id).toBe(toLinkId(1)) + expect(result.inputs[0].linkIds).toEqual([toLinkId(1)]) expect(subgraph.floatingLinks).toHaveLength(1) }) @@ -385,7 +395,8 @@ describe('normalizeSubgraphDefinitions', () => { state ) - expect(result.subgraphs[0].nodes![0].id).toBe(100_000_001) + assert.exists(result.subgraphs[0].nodes) + expect(result.subgraphs[0].nodes[0].id).toBe(100_000_001) expect(state.lastNodeId).toBe(100_000_001) }) @@ -406,7 +417,8 @@ describe('normalizeSubgraphDefinitions', () => { state ) - expect(result.subgraphs[0].links![0].id).toBe(toLinkId(2)) + assert.exists(result.subgraphs[0].links) + expect(result.subgraphs[0].links[0].id).toBe(toLinkId(2)) expect(state.lastLinkId).toBe(toLinkId(2)) const next = normalizeSubgraphDefinitions( @@ -419,13 +431,14 @@ describe('normalizeSubgraphDefinitions', () => { }, state ) - expect(next.subgraphs[0].links![0].id).toBe(toLinkId(3)) + assert.exists(next.subgraphs[0].links) + expect(next.subgraphs[0].links[0].id).toBe(toLinkId(3)) expect(state.lastLinkId).toBe(toLinkId(3)) }) it('updates every root-level reference to a remapped interior node', () => { const subgraph = makeSubgraph('sg', ['dummy']) - const rootNode = makeSubgraph('root', ['sg']).nodes![0] + const rootNode = makeSubgraph('root', ['sg']).nodes[0] rootNode.properties = { proxyWidgets: [['1', 'seed']], previewExposures: [{ sourceNodeId: '1' }] @@ -442,9 +455,11 @@ describe('normalizeSubgraphDefinitions', () => { freshState(), [rootNode] ) - const remappedId = result.subgraphs[0].nodes![0].id + assert.exists(result.subgraphs[0].nodes) + assert.exists(result.rootNodes) + const remappedId = result.subgraphs[0].nodes[0].id - expect(result.rootNodes![0].properties).toEqual({ + expect(result.rootNodes[0].properties).toEqual({ proxyWidgets: [[String(remappedId), 'seed']], previewExposures: [{ sourceNodeId: String(remappedId) }] }) diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts index 325f29042d3..7479eaadbd2 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts @@ -1,4 +1,3 @@ -import type { LGraph } from '../LGraph' import { isUuidShapedSubgraphId } from '@/schemas/subgraphIdSchema' import { toGroupId } from '@/types/groupId' import { @@ -25,7 +24,6 @@ import { createUuidv4 } from '@/utils/uuid' import type { ExportedSubgraph, ExposedWidget, - ISerialisedGroup, ISerialisedNode, SerialisableLLink } from '../types/serialisation' @@ -226,30 +224,19 @@ function deduplicateClonedSubgraphNodeIds( clonedRootNodes?: ISerialisedNode[] ): void { const usedNodeIdKeys = new Set(reservedNodeIdKeys) - const usedNodeIds = new Set() - for (const id of reservedNodeIdKeys) { - const numericId = numericSerializedNodeId(id) - if (numericId !== null) usedNodeIds.add(numericId) - } + const usedNodeIds = collectNumericNodeIds(reservedNodeIdKeys) const subgraphIdSet = new Set(clonedSubgraphs.map((sg) => sg.id)) const remapBySubgraph = new Map>() for (const subgraph of clonedSubgraphs) { - const remappedIds = remapNodeIds( - subgraph.nodes ?? [], + const remappedIds = remapSubgraphNodeIds( + subgraph, usedNodeIdKeys, usedNodeIds, state ) - if (remappedIds.size === 0) continue remapBySubgraph.set(subgraph.id, remappedIds) - - patchSerialisedLinks( - [...(subgraph.links ?? []), ...(subgraph.floatingLinks ?? [])], - remappedIds - ) - patchPromotedWidgets(subgraph.widgets ?? [], remappedIds) } for (const subgraph of clonedSubgraphs) { @@ -260,9 +247,41 @@ function deduplicateClonedSubgraphNodeIds( ) } - if (clonedRootNodes) { - patchSubgraphNodeReferences(clonedRootNodes, subgraphIdSet, remapBySubgraph) + patchSubgraphNodeReferences( + clonedRootNodes ?? [], + subgraphIdSet, + remapBySubgraph + ) +} + +function collectNumericNodeIds(ids: ReadonlySet): Set { + const numericIds = new Set() + for (const id of ids) { + const numericId = numericSerializedNodeId(id) + if (numericId !== null) numericIds.add(numericId) } + return numericIds +} + +function remapSubgraphNodeIds( + subgraph: ExportedSubgraph, + usedNodeIdKeys: Set, + usedNodeIds: Set, + state: LGraphState +): Map { + const remappedIds = remapNodeIds( + subgraph.nodes ?? [], + usedNodeIdKeys, + usedNodeIds, + state + ) + if (remappedIds.size === 0) return remappedIds + patchSerialisedLinks( + [...(subgraph.links ?? []), ...(subgraph.floatingLinks ?? [])], + remappedIds + ) + patchPromotedWidgets(subgraph.widgets ?? [], remappedIds) + return remappedIds } /** @@ -305,18 +324,6 @@ function remapNodeIds( return remappedIds } -export function collectReservedNodeIds( - rootGraph: Pick, - rootNodes: ISerialisedNode[] = [] -): Set { - return new Set([ - ...rootNodes.map((node) => toNodeId(node.id)), - ...[rootGraph, ...rootGraph.subgraphs.values()].flatMap((owner) => - owner.nodes.map((node) => node.id) - ) - ]) -} - /** Parses a serialized node ID as an integer, or `null` when non-numeric. */ function numericSerializedNodeId(id: SerializedNodeId): number | null { const key = toNodeId(id) @@ -351,31 +358,6 @@ function patchPromotedWidgets( } } -export function collectReservedGroupIds( - graph: Pick, - serializedGroups: ISerialisedGroup[] = [] -): Set { - return new Set([ - ...serializedGroups.map((group) => group.id), - ...[graph, ...graph.subgraphs.values()].flatMap((g) => - g.groups.map((group) => group.id) - ) - ]) -} - -export function collectReservedLinkIds( - graph: Pick, - serializedFloatingLinks: SerialisableLLink[] = [] -): Set { - return new Set([ - ...serializedFloatingLinks.map((link) => link.id), - ...[graph, ...graph.subgraphs.values()].flatMap((owner) => [ - ...owner.links.keys(), - ...owner.floatingLinks.keys() - ]) - ]) -} - export function deduplicateSubgraphLinkIds( subgraphs: ExportedSubgraph[], reservedLinkIds: Set, @@ -420,16 +402,6 @@ export function deduplicateSubgraphGroupIds( } } -export function collectReservedRerouteIds( - graph: Pick -): Set { - return new Set( - [graph, ...graph.subgraphs.values()].flatMap((g) => - [...g.reroutes.values()].map((reroute) => reroute.id) - ) - ) -} - /** * Dedupes reroute IDs across serialized subgraph definitions. Reroute IDs * must be unique within a root graph: the reroute store keys every reroute diff --git a/src/platform/workflow/templates/services/templateInputService.ts b/src/platform/workflow/templates/services/templateInputService.ts index 6490e273727..c8dac60b485 100644 --- a/src/platform/workflow/templates/services/templateInputService.ts +++ b/src/platform/workflow/templates/services/templateInputService.ts @@ -5,6 +5,8 @@ import type { ComfyNode, ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' +import { isWidgetValue } from '@/lib/litegraph/src/types/widgets' +import type { TWidgetValue } from '@/lib/litegraph/src/types/widgets' import { api } from '@/scripts/api' import { zTemplateInput } from '../schemas/templateSchema' @@ -17,8 +19,9 @@ const INPUT_WIDGETS: Readonly> = { LoadVideo: 'file', LoadAudio: 'audio' } -const zValues = z.union([z.array(z.unknown()), z.record(z.unknown())]) -const zNamedValues = z.record(z.unknown()) +const zWidgetValue: z.ZodType = z.custom(isWidgetValue) +const zValues = z.union([z.array(zWidgetValue), z.record(zWidgetValue)]) +const zNamedValues = z.record(zWidgetValue) type PreparationResult = { workflow: ComfyWorkflowJSON diff --git a/src/platform/workflow/validation/schemas/workflowSchema.ts b/src/platform/workflow/validation/schemas/workflowSchema.ts index f5963f51ad9..696ae7c99fc 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.ts @@ -6,6 +6,7 @@ import type { ClipboardItems, ExportedSubgraph } from '@/lib/litegraph/src/types/serialisation' +import { isWidgetValue } from '@/lib/litegraph/src/types/widgets' import type { TWidgetValue } from '@/lib/litegraph/src/types/widgets' import { parseLinkId, toLinkId } from '@/types/linkId' import type { LinkPresentation } from '@/types/linkPresentation' @@ -236,7 +237,9 @@ const zProperties = z }) .catchall(zNodeProperty.optional()) -const zWidgetValues = z.union([z.array(z.any()), z.record(z.any())]) +const zWidgetValue: z.ZodType = z.custom(isWidgetValue) +const zWidgetValues: z.ZodType> = + z.union([z.array(zWidgetValue), z.record(zWidgetValue)]) const MAX_CLIPBOARD_WIDGET_VALUES = 10_000 function hasSupportedClipboardWidgetValuesLength( @@ -249,12 +252,16 @@ function hasSupportedClipboardWidgetValuesLength( ) } +function isArrayLikeLength(value: TWidgetValue): value is number { + return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 +} + function normalizeClipboardWidgetValues( values: z.output | undefined ): TWidgetValue[] | undefined { if (!values || Array.isArray(values)) return values const length = values.length - if (!Number.isSafeInteger(length) || length < 0) return [] + if (!isArrayLikeLength(length)) return [] const normalized: TWidgetValue[] = [] normalized.length = length for (const [key, value] of Object.entries(values)) { From b59e0c299172bd5f6bd8e89a39c5ed6f0895ea4f Mon Sep 17 00:00:00 2001 From: Amp Date: Mon, 28 Sep 2026 20:26:07 +0000 Subject: [PATCH 13/18] fix(litegraph): keep ID recovery non-throwing Amp-Thread-ID: https://ampcode.com/threads/T-01a0e8d6-c302-7581-acfa-2c1a88d59234 --- src/lib/litegraph/src/idAllocation.test.ts | 11 ++++++----- src/lib/litegraph/src/idAllocation.ts | 5 +---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 55f2e10b643..1d8c72f9814 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -169,13 +169,14 @@ describe('idAllocation', () => { expect(findNextAvailableId(new Set([1, 2, 3]), 1)).toBe(4) }) - it('throws instead of returning an occupied ID when the search is exhausted', () => { + it('keeps searching past the reported set size', () => { const occupiedIds = new Set([1]) - vi.spyOn(occupiedIds, 'has').mockReturnValue(true) + vi.spyOn(occupiedIds, 'has') + .mockReturnValueOnce(true) + .mockReturnValueOnce(true) + .mockReturnValue(false) - expect(() => findNextAvailableId(occupiedIds, 1)).toThrow( - 'ID space exhausted' - ) + expect(findNextAvailableId(occupiedIds, 1)).toBe(3) }) it('skips a reserved ID above a stale counter', () => { diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index 4d64ea0c31d..06e95d06206 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -120,12 +120,9 @@ export function findNextAvailableId( startAt = 1 ): number { let candidate = Number.isSafeInteger(startAt) && startAt > 0 ? startAt : 1 - let attempts = 0 - while (usedIds.has(candidate) && attempts < usedIds.size) { + while (usedIds.has(candidate)) { candidate = candidate === Number.MAX_SAFE_INTEGER ? 1 : candidate + 1 - attempts++ } - if (usedIds.has(candidate)) throw new RangeError('ID space exhausted') return candidate } From 54d94af5dbb17eac9739115b0f3183ae08ba3f9f Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 30 Sep 2026 10:45:03 -0700 Subject: [PATCH 14/18] fix: address safe ID review and cloud teardown --- browser_tests/fixtures/webSessionFixture.ts | 6 +- browser_tests/tests/copyPaste.spec.ts | 1 - src/lib/litegraph/src/LGraph.ts | 18 +----- .../src/LGraphCanvas.clipboard.test.ts | 52 +++++++++++++++ src/lib/litegraph/src/LGraphCanvas.ts | 63 ++++++++++++++----- .../src/idAllocation.property.test.ts | 22 ++++--- src/lib/litegraph/src/idAllocation.test.ts | 16 ----- src/lib/litegraph/src/idAllocation.ts | 29 ++++----- .../litegraph/src/subgraph/SubgraphInput.ts | 19 ------ .../validation/schemas/workflowSchema.test.ts | 22 ++++--- 10 files changed, 143 insertions(+), 105 deletions(-) diff --git a/browser_tests/fixtures/webSessionFixture.ts b/browser_tests/fixtures/webSessionFixture.ts index 18313f701d3..5f7e4e0911c 100644 --- a/browser_tests/fixtures/webSessionFixture.ts +++ b/browser_tests/fixtures/webSessionFixture.ts @@ -94,6 +94,10 @@ export const webSessionTest = comfyPageFixture.extend({ await context.addCookies([{ ...WEB_SESSION_COOKIE, url: APP_URL }]) - await use(page) + try { + await use(page) + } finally { + await page.unrouteAll({ behavior: 'ignoreErrors' }) + } } }) diff --git a/browser_tests/tests/copyPaste.spec.ts b/browser_tests/tests/copyPaste.spec.ts index 1fdf2841add..d6235524158 100644 --- a/browser_tests/tests/copyPaste.spec.ts +++ b/browser_tests/tests/copyPaste.spec.ts @@ -194,7 +194,6 @@ test.describe('Copy Paste', { tag: ['@screenshot', '@workflow'] }, () => { test('Sparse clipboard widget values keep their original indices', async ({ comfyPage }) => { - // Regression: https://github.com/Comfy-Org/ComfyUI_frontend/pull/19239#discussion_r4123007404 const originalNodes = await comfyPage.nodeOps.getNodeRefsByType('KSampler') const originalIds = new Set(originalNodes.map(({ id }) => id)) diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 13ea1b1c8ef..2d5ba217919 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -2160,7 +2160,7 @@ export class LGraph ): Subgraph[] { if (!data.length) return [] - const nodeIds = this.collectReservedNodeIds() + const nodeIds = collectReservedNodeIds(this.rootGraph) for (const id of reserved.nodeIds ?? []) nodeIds.add(id) const linkIds = collectReservedLinkIds(this.rootGraph) for (const id of reserved.linkIds ?? []) linkIds.add(id) @@ -2177,20 +2177,6 @@ export class LGraph return this.createNormalizedSubgraphs(normalized) } - private collectReservedNodeIds( - rootNodes: ISerialisedNode[] = [] - ): Set { - const reserved = new Set() - for (const owner of [ - this.rootGraph, - ...this.rootGraph.subgraphs.values() - ]) { - for (const node of owner.nodes) reserved.add(node.id) - } - for (const node of rootNodes) reserved.add(toNodeId(node.id)) - return reserved - } - private createNormalizedSubgraphs(data: ExportedSubgraph[]): Subgraph[] { const subgraphs = data.map((definition) => this.createNormalizedSubgraph(definition) @@ -3258,7 +3244,7 @@ export class LGraph ? normalizeSubgraphDefinitions( subgraphs, { - nodeIds: this.collectReservedNodeIds(nodesData), + nodeIds: collectReservedNodeIds(this.rootGraph, nodesData), groupIds: collectReservedGroupIds(this, data.groups), linkIds: collectReservedLinkIds(this, data.floatingLinks), rerouteIds: collectReservedRerouteIds(this) diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index 5ad9a8531dc..06b93ced565 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -309,6 +309,58 @@ describe('clipboard ID allocation', () => { expect(rootGraph.links.size).toBe(0) expect(rootGraph.reroutes.size).toBe(0) }) + + it('retains the paste failure when change finalization also fails', () => { + const nodeType = 'test/clipboard-operation-and-finalization-fail' + const operationError = new Error('node configure failed') + const finalizationError = new Error('after change failed') + class ThrowingConfigureNode extends LGraphNode { + override configure(): void { + throw operationError + } + } + LiteGraph.registerNodeType(nodeType, ThrowingConfigureNode) + const rootGraph = new LGraph() + rootGraph.onAfterChange = () => { + throw finalizationError + } + const canvas = createCanvas(rootGraph) + + let thrown: unknown + try { + canvas._deserializeItems( + { nodes: [createSerialisedNode(1, nodeType)] }, + {} + ) + } catch (error) { + thrown = error + } + + expect(thrown).toBeInstanceOf(AggregateError) + if (!(thrown instanceof AggregateError)) return + expect(thrown.cause).toBe(operationError) + expect(thrown.errors).toEqual([operationError, finalizationError]) + expect(rootGraph.nodes).toEqual([]) + }) + + it('rolls back a successful paste when change finalization fails', () => { + const nodeType = 'test/clipboard-finalization-fail' + LiteGraph.registerNodeType(nodeType, LGraphNode) + const rootGraph = new LGraph() + const finalizationError = new Error('after change failed') + rootGraph.onAfterChange = () => { + throw finalizationError + } + const canvas = createCanvas(rootGraph) + + expect(() => + canvas._deserializeItems( + { nodes: [createSerialisedNode(1, nodeType)] }, + {} + ) + ).toThrow(finalizationError) + expect(rootGraph.nodes).toEqual([]) + }) }) function createClipboardSubgraph(id: string): ExportedSubgraph { diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index ca80ed6f077..7e99cd4ee15 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -17,6 +17,7 @@ import { import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMutations' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { LayoutSource } from '@/renderer/core/layout/types' +import { reportError } from '@/platform/telemetry/reportError' import { applyCanvasSelection, clearGraphSelection, @@ -4289,10 +4290,17 @@ export class LGraphCanvas implements CustomEventDispatcher this.emitBeforeChange() let result: ClipboardPasteResult | undefined + let operationError: unknown try { const items = initializeClipboardItems(clipboardItems) const [offsetX, offsetY] = clipboardOffset(items) - result = createClipboardPasteResult() + result = { + created: [], + links: new Map(), + nodes: new Map(), + reroutes: new Map(), + subgraphs: new Map() + } const context: ClipboardPasteContext = { connectInputs, dx: position[0] - offsetX, @@ -4345,15 +4353,42 @@ export class LGraphCanvas implements CustomEventDispatcher this.selectItems(result.created) forEachNode(graph, (n) => n.onGraphConfigured?.()) forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) - - return result } catch (error) { + operationError = error if (result) rollbackClipboardPaste(graph, result) - throw error - } finally { + } + + const closingErrors: unknown[] = [] + try { graph.afterChange() + } catch (error) { + closingErrors.push(error) + } + try { this.emitAfterChange() + } catch (error) { + closingErrors.push(error) } + + if (!operationError && closingErrors.length && result) { + rollbackClipboardPaste(graph, result) + } + if (operationError && closingErrors.length) { + const combinedError = new AggregateError( + [operationError, ...closingErrors], + 'Clipboard paste and change finalization both failed' + ) + combinedError.cause = operationError + throw combinedError + } + if (operationError) throw operationError + if (closingErrors.length === 1) throw closingErrors[0] + if (closingErrors.length) + throw new AggregateError( + closingErrors, + 'Clipboard paste change finalization failed' + ) + return result } pasteFromClipboard(options: IPasteFromClipboardOptions = {}): void { @@ -8854,16 +8889,6 @@ function clipboardOffset(items: InitializedClipboardItems): Point { return [x, y] } -function createClipboardPasteResult(): ClipboardPasteResult { - return { - created: [], - links: new Map(), - nodes: new Map(), - reroutes: new Map(), - subgraphs: new Map() - } -} - function rollbackClipboardPaste( graph: LGraph, result: ClipboardPasteResult @@ -8876,7 +8901,9 @@ function rollbackClipboardPaste( graph.removeReroute(item.id) } } catch (error) { - console.error('Failed to fully roll back a pasted canvas item', error) + reportError(error, { + errorType: 'failure_rolling_back_clipboard_item' + }) } } @@ -8886,7 +8913,9 @@ function rollbackClipboardPaste( try { graph.releaseSubgraphs(registeredSubgraphs) } catch (error) { - console.error('Failed to fully roll back pasted subgraphs', error) + reportError(error, { + errorType: 'failure_rolling_back_clipboard_subgraphs' + }) } } diff --git a/src/lib/litegraph/src/idAllocation.property.test.ts b/src/lib/litegraph/src/idAllocation.property.test.ts index 8492e43bac6..14ad52e38b2 100644 --- a/src/lib/litegraph/src/idAllocation.property.test.ts +++ b/src/lib/litegraph/src/idAllocation.property.test.ts @@ -1,5 +1,5 @@ import * as fc from 'fast-check' -import { describe, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import { AGENT_RESERVED_BIT, @@ -156,15 +156,17 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen }) it('never repeats an id across a run of local crdt-disjoint mints', () => { - fc.assert( - fc.property(fc.integer({ min: 2, max: 50 }), (mintCount) => { - const frontend = createLGraphState() - const ids = Array.from({ length: mintCount }, () => - BigInt(mintNodeId(frontend, 'crdt-disjoint', new Set())) - ) - - return new Set(ids).size === ids.length - }) + const frontend = createLGraphState() + vi.spyOn(Math, 'random').mockReturnValue(0.25) + const first = mintNodeId(frontend, 'crdt-disjoint', new Set()) + const second = mintNodeId( + frontend, + 'crdt-disjoint', + new Set([Number(first)]) ) + + expect(second).not.toBe(first) + expect(BigInt(second) & AGENT_RESERVED_BIT).toBe(0n) + expect(BigInt(second) & CRDT_DISJOINT_FLOOR).not.toBe(0n) }) }) diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 1d8c72f9814..c267a3a15e4 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -186,22 +186,6 @@ describe('idAllocation', () => { expect(mintGroupId(state, new Set([2]))).toBe(3) }) - it('checks thunk reservations before taking the fast path', () => { - const state = createLGraphState() - let collections = 0 - const collectReservedIds = () => { - collections++ - return new Set([1]) - } - - expect(mintGroupId(state, collectReservedIds)).toBe(2) - expect(collections).toBe(2) - - state.lastGroupId = Number.MAX_SAFE_INTEGER - expect(mintGroupId(state, collectReservedIds)).toBe(2) - expect(collections).toBe(3) - }) - it('checks an indexed reservation without collecting on the fast path', () => { const state = createLGraphState() state.lastGroupId = 1 diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index aa2450d5aa6..c1c4cbe122e 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -26,10 +26,7 @@ export interface ReservedIdIndex { collect(): ReadonlySet } -type ReservedIds = - | ReadonlySet - | (() => ReadonlySet) - | ReservedIdIndex +type ReservedIds = ReadonlySet | ReservedIdIndex export function createLGraphState(): LGraphState { return { @@ -127,20 +124,11 @@ export function findNextAvailableId( } function mintSequentialId(lastId: number, reservedIds: ReservedIds): number { - const hasReservedId = (id: number) => - typeof reservedIds === 'function' - ? reservedIds().has(id) - : reservedIds.has(id) if (Number.isSafeInteger(lastId) && lastId >= 0) { const nextId = lastId + 1 - if (Number.isSafeInteger(nextId) && !hasReservedId(nextId)) return nextId + if (Number.isSafeInteger(nextId) && !reservedIds.has(nextId)) return nextId } - const usedIds = - typeof reservedIds === 'function' - ? reservedIds() - : 'collect' in reservedIds - ? reservedIds.collect() - : reservedIds + const usedIds = 'collect' in reservedIds ? reservedIds.collect() : reservedIds return findNextAvailableId(usedIds, lastId + 1) } @@ -173,9 +161,14 @@ export const AGENT_RESERVED_BIT = 1n << 40n export const CRDT_DISJOINT_FLOOR = 1n << 41n const CRDT_RANDOM_BIT_COUNT = 52 -function mintCrdtDisjointNodeId(): NodeId { +function mintCrdtDisjointNodeId(reservedIds: ReservedIds): NodeId { const random = BigInt(Math.floor(Math.random() * 2 ** CRDT_RANDOM_BIT_COUNT)) - const id = (random & ~AGENT_RESERVED_BIT) | CRDT_DISJOINT_FLOOR + let id = (random & ~AGENT_RESERVED_BIT) | CRDT_DISJOINT_FLOOR + const partitionStart = id & ~((1n << 40n) - 1n) + while (reservedIds.has(Number(id))) { + const lowBits = (id + 1n) & ((1n << 40n) - 1n) + id = partitionStart | lowBits + } return toNodeId(Number(id)) } @@ -255,7 +248,7 @@ export function mintNodeId( mode: NodeIdMintMode, reservedIds: ReservedIds ): NodeId { - if (mode === 'crdt-disjoint') return mintCrdtDisjointNodeId() + if (mode === 'crdt-disjoint') return mintCrdtDisjointNodeId(reservedIds) const id = mintSequentialId(state.lastNodeId, reservedIds) state.lastNodeId = id return toNodeId(id) diff --git a/src/lib/litegraph/src/subgraph/SubgraphInput.ts b/src/lib/litegraph/src/subgraph/SubgraphInput.ts index ea53c5899ad..2c081f03e45 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInput.ts @@ -108,25 +108,6 @@ export class SubgraphInput extends SubgraphSlot { } subgraph._addLink(link) - return this.finishConnection( - link, - existingLink, - slot, - node, - inputIndex, - inputWidget - ) - } - - private finishConnection( - link: LLink, - existingLink: LLink | undefined, - slot: INodeInputSlot, - node: LGraphNode, - inputIndex: number, - inputWidget: IBaseWidget | undefined - ): LLink | undefined { - const { subgraph } = this.parent try { if (existingLink) { this.parent._disconnectNodeInput(node, slot, existingLink) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.test.ts b/src/platform/workflow/validation/schemas/workflowSchema.test.ts index d689c5d4ce9..6d31c863f4a 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.test.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.test.ts @@ -428,14 +428,22 @@ describe('zClipboardItems', () => { expect(values?.[2]).toBe('third') }) - it('rejects an impractically large sparse widget values length', () => { - const node = { - ...structuredClone(defaultGraph.nodes[0]), - widgets_values: { 0: 'first', length: 10_001 } - } + test.for([ + { length: 10_000, accepted: true }, + { length: 10_001, accepted: false } + ])( + 'accepts sparse widget lengths through the exact limit: $length', + ({ length, accepted }) => { + const node = { + ...structuredClone(defaultGraph.nodes[0]), + widgets_values: { 0: 'first', length } + } - expect(zClipboardItems.safeParse({ nodes: [node] }).success).toBe(false) - }) + expect(zClipboardItems.safeParse({ nodes: [node] }).success).toBe( + accepted + ) + } + ) it('preserves named-record widget values as named values', () => { const node = { From 949645e785b752014ae9dc14133c2c5ed196b57c Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 30 Sep 2026 11:53:42 -0700 Subject: [PATCH 15/18] fix(litegraph): retain falsy paste finalization errors --- .../src/LGraphCanvas.clipboard.test.ts | 24 +++++++++++++++++++ src/lib/litegraph/src/LGraphCanvas.ts | 6 ++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index f3236447e9b..3cdb8cb7b14 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -374,6 +374,30 @@ describe('clipboard ID allocation', () => { { nodeCount: 0, subType: 'after-change' } ]) }) + + it('retains a falsy change finalization failure after rollback', () => { + const nodeType = 'test/clipboard-falsy-finalization-fail' + LiteGraph.registerNodeType(nodeType, LGraphNode) + const rootGraph = new LGraph() + vi.spyOn(rootGraph, 'afterChange').mockImplementationOnce(() => { + throw undefined + }) + const canvas = createCanvas(rootGraph) + + let completed = false + try { + canvas._deserializeItems( + { nodes: [createSerialisedNode(1, nodeType)] }, + {} + ) + completed = true + } catch (error) { + expect(error).toBeUndefined() + } + + expect(completed).toBe(false) + expect(rootGraph.nodes).toEqual([]) + }) }) function createClipboardSubgraph(id: string): ExportedSubgraph { diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 25ea401ac2c..b97ad85c03a 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -4358,16 +4358,14 @@ export class LGraphCanvas implements CustomEventDispatcher if (result) rollbackClipboardPaste(graph, result) } - let graphClosingError: unknown + const closingErrors: unknown[] = [] try { graph.afterChange() } catch (error) { - graphClosingError = error + closingErrors.push(error) if (!operationError && result) rollbackClipboardPaste(graph, result) } - const closingErrors: unknown[] = [] - if (graphClosingError) closingErrors.push(graphClosingError) try { this.emitAfterChange() } catch (error) { From 281b360e0c8060db5e151b262eb772f8f2f5d3fa Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 30 Sep 2026 16:50:40 -0700 Subject: [PATCH 16/18] fix(litegraph): retain falsy paste operation errors --- .../src/LGraphCanvas.clipboard.test.ts | 26 +++++++++++++++++++ src/lib/litegraph/src/LGraphCanvas.ts | 8 +++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index 3cdb8cb7b14..2249fcf158d 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -398,6 +398,32 @@ describe('clipboard ID allocation', () => { expect(completed).toBe(false) expect(rootGraph.nodes).toEqual([]) }) + + it('retains a falsy paste operation failure after rollback', () => { + const nodeType = 'test/clipboard-falsy-operation-fail' + class ThrowingConfigureNode extends LGraphNode { + override configure(): void { + throw undefined + } + } + LiteGraph.registerNodeType(nodeType, ThrowingConfigureNode) + const rootGraph = new LGraph() + const canvas = createCanvas(rootGraph) + + let completed = false + try { + canvas._deserializeItems( + { nodes: [createSerialisedNode(1, nodeType)] }, + {} + ) + completed = true + } catch (error) { + expect(error).toBeUndefined() + } + + expect(completed).toBe(false) + expect(rootGraph.nodes).toEqual([]) + }) }) function createClipboardSubgraph(id: string): ExportedSubgraph { diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index b97ad85c03a..79346ddfc5c 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -4290,6 +4290,7 @@ export class LGraphCanvas implements CustomEventDispatcher this.emitBeforeChange() let result: ClipboardPasteResult | undefined + let operationFailed = false let operationError: unknown try { const items = initializeClipboardItems(clipboardItems) @@ -4354,6 +4355,7 @@ export class LGraphCanvas implements CustomEventDispatcher forEachNode(graph, (n) => n.onGraphConfigured?.()) forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) } catch (error) { + operationFailed = true operationError = error if (result) rollbackClipboardPaste(graph, result) } @@ -4363,7 +4365,7 @@ export class LGraphCanvas implements CustomEventDispatcher graph.afterChange() } catch (error) { closingErrors.push(error) - if (!operationError && result) rollbackClipboardPaste(graph, result) + if (!operationFailed && result) rollbackClipboardPaste(graph, result) } try { @@ -4372,7 +4374,7 @@ export class LGraphCanvas implements CustomEventDispatcher closingErrors.push(error) } - if (operationError && closingErrors.length) { + if (operationFailed && closingErrors.length) { const combinedError = new AggregateError( [operationError, ...closingErrors], 'Clipboard paste and change finalization both failed' @@ -4380,7 +4382,7 @@ export class LGraphCanvas implements CustomEventDispatcher combinedError.cause = operationError throw combinedError } - if (operationError) throw operationError + if (operationFailed) throw operationError if (closingErrors.length === 1) throw closingErrors[0] if (closingErrors.length) throw new AggregateError( From a2a7669200feef7f10970efeeb05d8acbe9c0acc Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 1 Oct 2026 03:25:02 -0700 Subject: [PATCH 17/18] test(litegraph): pin safe ID recovery against production reserved-range IDs Adds proof discriminating the removed 1e8 ceiling from any ceiling merely raised, using the two IDs that actually appear in cloud-frontend-prod telemetry for this family: a node ID this app minted through its crdt-disjoint path (bit 41) and a link ID the agent minted (bit 40). - LGraph.configure regression over both ID classes, red on main with the exact production stack (findNextAvailableId / remapNodeIds / deduplicateClonedSubgraphNodeIds / normalizeSubgraphDefinitions). The fixture reserves the successor of the node high-water mark so the remap reaches collision recovery, and a companion test pins that remapped IDs stay inside the reserved partition they came from. - Property coverage over the whole reserved range for all four ID classes that share findNextAvailableId, including each collision-recovery path. - Browser case loading a workflow whose counters sit in the reserved range: zero nodes load on main, all three load here. --- ...aph-duplicate-ids-reserved-mint-range.json | 599 ++++++++++++++++++ .../tests/subgraph/subgraphNested.spec.ts | 32 + src/lib/litegraph/src/LGraph.test.ts | 79 +++ .../nodeIdsFromReservedMintRange.ts | 217 +++++++ .../src/idAllocation.property.test.ts | 104 ++- 5 files changed, 1030 insertions(+), 1 deletion(-) create mode 100644 browser_tests/assets/subgraphs/subgraph-duplicate-ids-reserved-mint-range.json create mode 100644 src/lib/litegraph/src/__fixtures__/nodeIdsFromReservedMintRange.ts diff --git a/browser_tests/assets/subgraphs/subgraph-duplicate-ids-reserved-mint-range.json b/browser_tests/assets/subgraphs/subgraph-duplicate-ids-reserved-mint-range.json new file mode 100644 index 00000000000..e6eb82d3f0d --- /dev/null +++ b/browser_tests/assets/subgraphs/subgraph-duplicate-ids-reserved-mint-range.json @@ -0,0 +1,599 @@ +{ + "id": "9a37f747-e96b-4304-9212-7abcaad7bdac", + "revision": 0, + "last_node_id": 4462758126524329, + "last_link_id": 7729209487955825, + "nodes": [ + { + "id": 5, + "type": "1e38d8ea-45e1-48a5-aa20-966584201867", + "pos": [788, 433.5], + "size": [210, 108], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [ + { + "name": "string_a", + "type": "STRING", + "widget": { + "name": "string_a" + }, + "link": 4 + } + ], + "outputs": [ + { + "name": "STRING", + "type": "STRING", + "links": [5] + } + ], + "properties": { + "proxyWidgets": [["-1", "string_a"]] + }, + "widgets_values": [""] + }, + { + "id": 2, + "type": "PreviewAny", + "pos": [1135, 429], + "size": [250, 145.5], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [ + { + "name": "source", + "type": "*", + "link": 5 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "PreviewAny" + }, + "widgets_values": [null, null, false] + }, + { + "id": 1, + "type": "PrimitiveStringMultiline", + "pos": [456, 450], + "size": [225, 121.5], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "STRING", + "type": "STRING", + "links": [4] + } + ], + "properties": { + "Node name for S&R": "PrimitiveStringMultiline" + }, + "widgets_values": ["Outer\n"] + } + ], + "links": [ + [4, 1, 0, 5, 0, "STRING"], + [5, 5, 0, 2, 0, "STRING"] + ], + "groups": [], + "definitions": { + "subgraphs": [ + { + "id": "1e38d8ea-45e1-48a5-aa20-966584201867", + "version": 1, + "state": { + "lastGroupId": 0, + "lastNodeId": 6, + "lastLinkId": 9, + "lastRerouteId": 0 + }, + "revision": 0, + "config": {}, + "name": "New Subgraph", + "inputNode": { + "id": -10, + "bounding": [351, 432.5, 120, 60] + }, + "outputNode": { + "id": -20, + "bounding": [1315, 432.5, 120, 60] + }, + "inputs": [ + { + "id": "7bf3e1d4-0521-4b5c-92f5-47ca598b7eb4", + "name": "string_a", + "type": "STRING", + "linkIds": [1], + "localized_name": "string_a", + "pos": [451, 452.5] + } + ], + "outputs": [ + { + "id": "fbe975ba-d7c2-471e-a99a-a1e2c6ab466d", + "name": "STRING", + "type": "STRING", + "linkIds": [9], + "localized_name": "STRING", + "pos": [1335, 452.5] + } + ], + "widgets": [], + "nodes": [ + { + "id": 3, + "type": "StringConcatenate", + "pos": [815, 373], + "size": [347, 231], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [ + { + "localized_name": "string_a", + "name": "string_a", + "type": "STRING", + "widget": { + "name": "string_a" + }, + "link": 1 + }, + { + "localized_name": "string_b", + "name": "string_b", + "type": "STRING", + "widget": { + "name": "string_b" + }, + "link": 2 + } + ], + "outputs": [ + { + "localized_name": "STRING", + "name": "STRING", + "type": "STRING", + "links": [7] + } + ], + "properties": { + "Node name for S&R": "StringConcatenate" + }, + "widgets_values": ["", "", ""] + }, + { + "id": 6, + "type": "9be42452-056b-4c99-9f9f-7381d11c4454", + "pos": [955, 775], + "size": [210, 88], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [ + { + "localized_name": "string_a", + "name": "string_a", + "type": "STRING", + "widget": { + "name": "string_a" + }, + "link": 7 + } + ], + "outputs": [ + { + "localized_name": "STRING", + "name": "STRING", + "type": "STRING", + "links": [9] + } + ], + "properties": { + "proxyWidgets": [["-1", "string_a"]] + }, + "widgets_values": [""] + }, + { + "id": 4, + "type": "PrimitiveStringMultiline", + "pos": [313, 685], + "size": [325, 109], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "localized_name": "STRING", + "name": "STRING", + "type": "STRING", + "links": [2] + } + ], + "properties": { + "Node name for S&R": "PrimitiveStringMultiline" + }, + "widgets_values": ["Inner 1\n"] + } + ], + "groups": [], + "links": [ + { + "id": 2, + "origin_id": 4, + "origin_slot": 0, + "target_id": 3, + "target_slot": 1, + "type": "STRING" + }, + { + "id": 1, + "origin_id": -10, + "origin_slot": 0, + "target_id": 3, + "target_slot": 0, + "type": "STRING" + }, + { + "id": 7, + "origin_id": 3, + "origin_slot": 0, + "target_id": 6, + "target_slot": 0, + "type": "STRING" + }, + { + "id": 6, + "origin_id": 6, + "origin_slot": 0, + "target_id": -20, + "target_slot": 1, + "type": "STRING" + }, + { + "id": 9, + "origin_id": 6, + "origin_slot": 0, + "target_id": -20, + "target_slot": 0, + "type": "STRING" + } + ], + "extra": {} + }, + { + "id": "9be42452-056b-4c99-9f9f-7381d11c4454", + "version": 1, + "state": { + "lastGroupId": 0, + "lastNodeId": 9, + "lastLinkId": 12, + "lastRerouteId": 0 + }, + "revision": 0, + "config": {}, + "name": "New Subgraph", + "inputNode": { + "id": -10, + "bounding": [680, 774, 120, 60] + }, + "outputNode": { + "id": -20, + "bounding": [1320, 774, 120, 60] + }, + "inputs": [ + { + "id": "01c05c51-86b5-4bad-b32f-9c911683a13d", + "name": "string_a", + "type": "STRING", + "linkIds": [4], + "localized_name": "string_a", + "pos": [780, 794] + } + ], + "outputs": [ + { + "id": "a8bcf3bf-a66a-4c71-8d92-17a2a4d03686", + "name": "STRING", + "type": "STRING", + "linkIds": [12], + "localized_name": "STRING", + "pos": [1340, 794] + } + ], + "widgets": [], + "nodes": [ + { + "id": 5, + "type": "StringConcatenate", + "pos": [860, 719], + "size": [400, 200], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [ + { + "localized_name": "string_a", + "name": "string_a", + "type": "STRING", + "widget": { + "name": "string_a" + }, + "link": 4 + }, + { + "localized_name": "string_b", + "name": "string_b", + "type": "STRING", + "widget": { + "name": "string_b" + }, + "link": 7 + } + ], + "outputs": [ + { + "localized_name": "STRING", + "name": "STRING", + "type": "STRING", + "links": [11] + } + ], + "properties": { + "Node name for S&R": "StringConcatenate" + }, + "widgets_values": ["", "", ""] + }, + { + "id": 6, + "type": "PrimitiveStringMultiline", + "pos": [401, 973], + "size": [400, 200], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "localized_name": "STRING", + "name": "STRING", + "type": "STRING", + "links": [7] + } + ], + "properties": { + "Node name for S&R": "PrimitiveStringMultiline" + }, + "widgets_values": ["Inner 2\n"] + }, + { + "id": 9, + "type": "7c2915a5-5eb8-4958-a8fd-4beb30f370ce", + "pos": [1046, 985], + "size": [210, 88], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [ + { + "localized_name": "string_a", + "name": "string_a", + "type": "STRING", + "widget": { + "name": "string_a" + }, + "link": 11 + } + ], + "outputs": [ + { + "localized_name": "STRING", + "name": "STRING", + "type": "STRING", + "links": [12] + } + ], + "properties": { + "proxyWidgets": [["-1", "string_a"]] + }, + "widgets_values": [""] + } + ], + "groups": [], + "links": [ + { + "id": 4, + "origin_id": -10, + "origin_slot": 0, + "target_id": 5, + "target_slot": 0, + "type": "STRING" + }, + { + "id": 7, + "origin_id": 6, + "origin_slot": 0, + "target_id": 5, + "target_slot": 1, + "type": "STRING" + }, + { + "id": 11, + "origin_id": 5, + "origin_slot": 0, + "target_id": 9, + "target_slot": 0, + "type": "STRING" + }, + { + "id": 10, + "origin_id": 9, + "origin_slot": 0, + "target_id": -20, + "target_slot": 0, + "type": "STRING" + }, + { + "id": 12, + "origin_id": 9, + "origin_slot": 0, + "target_id": -20, + "target_slot": 0, + "type": "STRING" + } + ], + "extra": {} + }, + { + "id": "7c2915a5-5eb8-4958-a8fd-4beb30f370ce", + "version": 1, + "state": { + "lastGroupId": 0, + "lastNodeId": 8, + "lastLinkId": 10, + "lastRerouteId": 0 + }, + "revision": 0, + "config": {}, + "name": "New Subgraph", + "inputNode": { + "id": -10, + "bounding": [262, 1222, 120, 60] + }, + "outputNode": { + "id": -20, + "bounding": [1330, 1222, 120, 60] + }, + "inputs": [ + { + "id": "934a8baa-d79c-428c-8ec9-814ad437d7c7", + "name": "string_a", + "type": "STRING", + "linkIds": [9], + "localized_name": "string_a", + "pos": [362, 1242] + } + ], + "outputs": [ + { + "id": "4c3d243b-9ff6-4dcd-9dbf-e4ec8e1fc879", + "name": "STRING", + "type": "STRING", + "linkIds": [10], + "localized_name": "STRING", + "pos": [1350, 1242] + } + ], + "widgets": [], + "nodes": [ + { + "id": 7, + "type": "StringConcatenate", + "pos": [870, 1038], + "size": [400, 200], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [ + { + "localized_name": "string_a", + "name": "string_a", + "type": "STRING", + "widget": { + "name": "string_a" + }, + "link": 9 + }, + { + "localized_name": "string_b", + "name": "string_b", + "type": "STRING", + "widget": { + "name": "string_b" + }, + "link": 8 + } + ], + "outputs": [ + { + "localized_name": "STRING", + "name": "STRING", + "type": "STRING", + "links": [10] + } + ], + "properties": { + "Node name for S&R": "StringConcatenate" + }, + "widgets_values": ["", "", ""] + }, + { + "id": 8, + "type": "PrimitiveStringMultiline", + "pos": [442, 1296], + "size": [400, 200], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "localized_name": "STRING", + "name": "STRING", + "type": "STRING", + "links": [8] + } + ], + "properties": { + "Node name for S&R": "PrimitiveStringMultiline" + }, + "widgets_values": ["Inner 3\n"] + } + ], + "groups": [], + "links": [ + { + "id": 8, + "origin_id": 8, + "origin_slot": 0, + "target_id": 7, + "target_slot": 1, + "type": "STRING" + }, + { + "id": 9, + "origin_id": -10, + "origin_slot": 0, + "target_id": 7, + "target_slot": 0, + "type": "STRING" + }, + { + "id": 10, + "origin_id": 7, + "origin_slot": 0, + "target_id": -20, + "target_slot": 0, + "type": "STRING" + } + ], + "extra": {} + } + ] + }, + "config": {}, + "extra": { + "ds": { + "scale": 1, + "offset": [-7, 144] + }, + "frontendVersion": "1.38.13" + }, + "version": 0.4 +} diff --git a/browser_tests/tests/subgraph/subgraphNested.spec.ts b/browser_tests/tests/subgraph/subgraphNested.spec.ts index b9c029aba1e..612131487f4 100644 --- a/browser_tests/tests/subgraph/subgraphNested.spec.ts +++ b/browser_tests/tests/subgraph/subgraphNested.spec.ts @@ -30,6 +30,38 @@ test.describe('Nested Subgraphs', { tag: ['@subgraph'] }, () => { dispose() } }) + + /** + * The same workflow, with the two id counters raised into the range a + * different mint convention owns: `4462758126524329` is a node id this + * app minted through its CRDT-disjoint path, and `7729209487955825` is + * a link id the server-side agent minted. Both are real values taken + * from `cloud-frontend-prod` telemetry. + * + * Deduplicating this workflow's shared subgraph ids has to mint from + * those counters. While allocation was bounded at a fixed 1e8, that + * throw was unhandled and aborted the load, so the canvas stayed empty + * — the user-visible half of FE-3007, reported from production as + * intermittently broken copy, paste, clone and workflow open. + */ + test('Loads a workflow whose ID counters sit in the reserved mint range', async ({ + comfyPage + }) => { + const SUBGRAPH_HOST_TYPE = '1e38d8ea-45e1-48a5-aa20-966584201867' + + await comfyPage.workflow.loadWorkflow( + 'subgraphs/subgraph-duplicate-ids-reserved-mint-range' + ) + + for (const type of [ + 'PreviewAny', + 'PrimitiveStringMultiline', + SUBGRAPH_HOST_TYPE + ]) { + const nodes = await comfyPage.nodeOps.getNodeRefsByType(type) + expect(nodes, `expected one ${type} node after load`).toHaveLength(1) + } + }) }) test.describe( diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index ba718eb4e85..0939326c8e6 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -27,6 +27,10 @@ import { isRootGraphDocBound, registerDocBoundRootGraphProbe } from '@/lib/litegraph/src/docBoundGraphs' +import { + AGENT_RESERVED_BIT, + CRDT_DISJOINT_FLOOR +} from '@/lib/litegraph/src/idAllocation' import type { UUID } from '@/utils/uuid' import { createUuidv4, zeroUuid } from '@/utils/uuid' import { useEntityIdStore } from '@/stores/entityIdStore' @@ -58,6 +62,7 @@ import { import { duplicateSubgraphNodeIds } from './__fixtures__/duplicateSubgraphNodeIds' import { nestedSubgraphProxyWidgets } from './__fixtures__/nestedSubgraphProxyWidgets' import { nodeIdsAtFormerLimit } from './__fixtures__/nodeIdsAtFormerLimit' +import { nodeIdsFromReservedMintRange } from './__fixtures__/nodeIdsFromReservedMintRange' import { uniqueSubgraphNodeIds } from './__fixtures__/uniqueSubgraphNodeIds' import { test } from './__fixtures__/testExtensions' @@ -2400,6 +2405,80 @@ describe('deduplicateSubgraphNodeIds (via configure)', () => { expect(graph.state.lastNodeId).toBe(100_000_003) }) + /** + * The production defect (FE-3007), reproduced from its own telemetry + * rather than from a value near the former fixed ceiling. See + * `nodeIdsFromReservedMintRange` for the provenance of both ids. + * + * This is the discriminating case: `nodeIdsAtFormerLimit` above still + * passes if the ceiling is merely raised instead of removed, because its + * ids sit just over 1e8. These ids are ~45 million and ~77 million times + * that ceiling, in both the node and link classes `findNextAvailableId` + * serves, so a bound anywhere below them fails here. + * + * The fixture also reserves the successor of the node high-water mark, so + * the node remap reaches collision recovery rather than returning + * `lastNodeId + 1` off the fast path. A ceiling reintroduced inside + * `findNextAvailableId` alone is therefore caught too, not only one on + * the mint. `idAllocation.property.test.ts` states the same property over + * the whole reserved range and all four id classes. + */ + it('remaps duplicate subgraph IDs when a reserved-range mint has raised the counters', () => { + const graph = new LGraph() + const observedNodeId = 4_462_758_126_524_329 + const observedLinkId = 7_729_209_487_955_825 + + expect(() => { + graph.configure(structuredClone(nodeIdsFromReservedMintRange)) + }).not.toThrow() + + // Not +1: that candidate is reserved by a root node, so the first + // remap recovers past it. + expect(nodeIdSet(graph, SUBGRAPH_B)).toEqual( + new Set([ + toNodeId(observedNodeId + 2), + toNodeId(observedNodeId + 3), + toNodeId(observedNodeId + 4) + ]) + ) + expect(graph.state.lastNodeId).toBe(observedNodeId + 4) + + expect(nodeIdSet(graph, SUBGRAPH_A)).toEqual( + new Set([toNodeId(3), toNodeId(8), toNodeId(37)]) + ) + + const linkIdsB = [...graph.subgraphs.get(SUBGRAPH_B)!.links.keys()] + expect(linkIdsB).toEqual([observedLinkId + 1]) + expect(graph.state.lastLinkId).toBe(observedLinkId + 1) + + for (const id of [...nodeIdSet(graph, SUBGRAPH_B)].map(Number)) { + expect(Number.isSafeInteger(id)).toBe(true) + } + }) + + /** + * The remapped ids above sit inside the same 2^40-wide partition as the + * high-water mark that produced them, because a sequential mint only + * advances the low bits. Pinned explicitly so the reserved-mint collision + * contract is visible rather than incidental: a node id remapped off an + * id this app minted (bit 41) keeps bit 41 and keeps bit 40 clear, so it + * cannot land in the agent's `2**40 | random52` range; a link id remapped + * off an agent-minted id (bit 40) stays in that range. + */ + it('keeps remapped IDs inside the reserved partition they were minted from', () => { + const graph = new LGraph() + graph.configure(structuredClone(nodeIdsFromReservedMintRange)) + + for (const id of [...nodeIdSet(graph, SUBGRAPH_B)].map(BigInt)) { + expect(id & CRDT_DISJOINT_FLOOR).not.toBe(0n) + expect(id & AGENT_RESERVED_BIT).toBe(0n) + } + + for (const id of graph.subgraphs.get(SUBGRAPH_B)!.links.keys()) { + expect(BigInt(id) & AGENT_RESERVED_BIT).not.toBe(0n) + } + }) + it('is a no-op when subgraph node IDs are already unique', () => { const graph = new LGraph() graph.configure(structuredClone(uniqueSubgraphNodeIds)) diff --git a/src/lib/litegraph/src/__fixtures__/nodeIdsFromReservedMintRange.ts b/src/lib/litegraph/src/__fixtures__/nodeIdsFromReservedMintRange.ts new file mode 100644 index 00000000000..12fac25721d --- /dev/null +++ b/src/lib/litegraph/src/__fixtures__/nodeIdsFromReservedMintRange.ts @@ -0,0 +1,217 @@ +import { + SUBGRAPH_INPUT_ID, + SUBGRAPH_OUTPUT_ID +} from '@/lib/litegraph/src/constants' +import type { SerialisableGraph } from '@/lib/litegraph/src/types/serialisation' + +/** + * The shape that actually reached production, rather than a synthetic value + * near the former fixed ceiling. + * + * Both ids below are real, taken from `cloud-frontend-prod` telemetry: + * + * - `4462758126524329` is a node id this app minted itself, through + * `mintCrdtDisjointNodeId` — bit 41 set, bit 40 clear. + * - `7729209487955825` is a link id the server-side agent minted, through + * comfy-cli's `mint_id()` (`2**40 | random52`) — bit 40 set. + * + * The two id classes are poisoned by the two different routes production + * took, deliberately: + * + * - `lastNodeId` arrives already raised, as it is on the live graph that + * copy, paste and clone run against: `observeNodeId` raised it when this + * app's own node materialized, long before any deduplication pass. + * - `lastLinkId` starts at zero and is raised *inside* the pass, because + * `deduplicateSubgraphLinkIds` observes every reserved link id — including + * the agent-minted one interior to SubgraphA — before it remaps anything. + * + * Observation has never had a ceiling; minting used to. So in both classes + * the first duplicate subgraph-scoped id that needed remapping minted from a + * counter in the quadrillions and threw `Node ID space exhausted` on its + * first candidate, aborting copy, paste, clone and workflow load. + * + * Both subgraph definitions therefore share node ids `[3, 8, 37]` AND link + * id `7729209487955825`, so configuring this graph has to remap in both id + * classes from a reserved-range high-water mark. + */ +export const nodeIdsFromReservedMintRange = { + id: 'dddddddd-dddd-4ddd-8ddd-dddddddddddd', + version: 1, + revision: 0, + state: { + lastNodeId: 4_462_758_126_524_329, + lastLinkId: 0, + lastGroupId: 0, + lastRerouteId: 0 + }, + nodes: [ + { + id: 102, + type: '11111111-1111-4111-8111-111111111111', + pos: [0, 0], + size: [200, 100], + flags: {}, + order: 0, + mode: 0, + properties: { proxyWidgets: [['3', 'seed']] } + }, + { + id: 103, + type: '22222222-2222-4222-8222-222222222222', + pos: [300, 0], + size: [200, 100], + flags: {}, + order: 1, + mode: 0, + properties: { proxyWidgets: [['8', 'prompt']] } + }, + { + id: 4_462_758_126_524_329, + type: 'dummy', + pos: [600, 0], + size: [100, 50], + flags: {}, + order: 2, + mode: 0 + }, + /** + * The successor of the node high-water mark, reserved so the first + * remap candidate is already taken and minting has to fall through to + * collision recovery — the branch the former ceiling guarded. Node + * deduplication reserves root ids without observing them, so this does + * not itself raise `lastNodeId`; link, group and reroute deduplication + * observe every reserved id, so the same gap cannot be built there from + * a fixture, and `idAllocation.property.test.ts` covers those three + * recovery paths directly instead. + */ + { + id: 4_462_758_126_524_330, + type: 'dummy', + pos: [900, 0], + size: [100, 50], + flags: {}, + order: 3, + mode: 0 + } + ], + definitions: { + subgraphs: [ + { + id: '11111111-1111-4111-8111-111111111111', + version: 1, + revision: 0, + state: { + lastNodeId: 0, + lastLinkId: 0, + lastGroupId: 0, + lastRerouteId: 0 + }, + name: 'SubgraphA', + config: {}, + inputNode: { id: SUBGRAPH_INPUT_ID, bounding: [10, 100, 150, 126] }, + outputNode: { id: SUBGRAPH_OUTPUT_ID, bounding: [400, 100, 140, 126] }, + inputs: [], + outputs: [], + widgets: [{ id: 3, name: 'seed' }], + nodes: [ + { + id: 3, + type: 'dummy', + pos: [0, 0], + size: [100, 50], + flags: {}, + order: 0, + mode: 0 + }, + { + id: 8, + type: 'dummy', + pos: [0, 0], + size: [100, 50], + flags: {}, + order: 1, + mode: 0 + }, + { + id: 37, + type: 'dummy', + pos: [0, 0], + size: [100, 50], + flags: {}, + order: 2, + mode: 0 + } + ], + links: [ + { + id: 7_729_209_487_955_825, + origin_id: 3, + origin_slot: 0, + target_id: 8, + target_slot: 0, + type: 'number' + } + ], + groups: [] + }, + { + id: '22222222-2222-4222-8222-222222222222', + version: 1, + revision: 0, + state: { + lastNodeId: 0, + lastLinkId: 0, + lastGroupId: 0, + lastRerouteId: 0 + }, + name: 'SubgraphB', + config: {}, + inputNode: { id: SUBGRAPH_INPUT_ID, bounding: [10, 100, 150, 126] }, + outputNode: { id: SUBGRAPH_OUTPUT_ID, bounding: [400, 100, 140, 126] }, + inputs: [], + outputs: [], + widgets: [{ id: 8, name: 'prompt' }], + nodes: [ + { + id: 3, + type: 'dummy', + pos: [0, 0], + size: [100, 50], + flags: {}, + order: 0, + mode: 0 + }, + { + id: 8, + type: 'dummy', + pos: [0, 0], + size: [100, 50], + flags: {}, + order: 1, + mode: 0 + }, + { + id: 37, + type: 'dummy', + pos: [0, 0], + size: [100, 50], + flags: {}, + order: 2, + mode: 0 + } + ], + links: [ + { + id: 7_729_209_487_955_825, + origin_id: 3, + origin_slot: 0, + target_id: 37, + target_slot: 0, + type: 'string' + } + ], + groups: [] + } + ] + } +} as const satisfies SerialisableGraph diff --git a/src/lib/litegraph/src/idAllocation.property.test.ts b/src/lib/litegraph/src/idAllocation.property.test.ts index 14ad52e38b2..01497eddbdc 100644 --- a/src/lib/litegraph/src/idAllocation.property.test.ts +++ b/src/lib/litegraph/src/idAllocation.property.test.ts @@ -1,11 +1,17 @@ import * as fc from 'fast-check' import { describe, expect, it, vi } from 'vitest' +import { toLinkId } from '@/types/linkId' +import { toRerouteId } from '@/types/rerouteId' + import { AGENT_RESERVED_BIT, CRDT_DISJOINT_FLOOR, createLGraphState, - mintNodeId + mintGroupId, + mintLinkId, + mintNodeId, + mintRerouteId } from '@/lib/litegraph/src/idAllocation' /** @@ -170,3 +176,99 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen expect(BigInt(second) & CRDT_DISJOINT_FLOOR).not.toBe(0n) }) }) + +/** + * The production failure (FE-3007) generalized past the one trace in + * `LGraph.test.ts`. Observation has no ceiling, so any id a *different* + * mint convention produced — the agent's `2**40 | random52`, or this app's + * own `crdt-disjoint` floor at 2^41 — becomes a sequential counter's + * high-water mark the moment it is seen. Minting then has to keep working + * from there. + * + * Stated over the whole reserved range rather than at one sampled value, + * and over all four id classes, because `findNextAvailableId` is shared by + * every one of them: the four Sentry groups in this family entered through + * three different entry points and two different id classes, and differed + * only in which `remap*` called the allocator. Any reintroduced fixed + * bound below `Number.MAX_SAFE_INTEGER` fails this, wherever it is put. + */ +describe('sequential minting survives a counter raised into the reserved mint range', () => { + /** + * The whole range a reserved-bit id can occupy, from the agent's mint + * floor to just below the safe-integer boundary — deliberately not just + * the production samples, so a ceiling placed anywhere in between is + * caught. The top of the range is held two below `MAX_SAFE_INTEGER` so a + * mint has somewhere to go without exercising the separate wrap path. + */ + const reservedRangeHighWaterMark = (): fc.Arbitrary => + fc.integer({ + min: Number(AGENT_RESERVED_BIT), + max: Number.MAX_SAFE_INTEGER - 2 + }) + + const minters = [ + { + name: 'node', + mint: (lastId: number, reserved: ReadonlySet) => { + const state = createLGraphState() + state.lastNodeId = lastId + return Number(mintNodeId(state, 'sequential', reserved)) + } + }, + { + name: 'link', + mint: (lastId: number, reserved: ReadonlySet) => { + const state = createLGraphState() + state.lastLinkId = toLinkId(lastId) + return Number(mintLinkId(state, reserved)) + } + }, + { + name: 'group', + mint: (lastId: number, reserved: ReadonlySet) => { + const state = createLGraphState() + state.lastGroupId = lastId + return Number(mintGroupId(state, reserved)) + } + }, + { + name: 'reroute', + mint: (lastId: number, reserved: ReadonlySet) => { + const state = createLGraphState() + state.lastRerouteId = toRerouteId(lastId) + return Number(mintRerouteId(state, reserved)) + } + } + ] as const + + it.for(minters)( + 'mints a fresh safe $name id above any reserved-range high-water mark', + ({ mint }) => { + fc.assert( + fc.property(reservedRangeHighWaterMark(), (lastId) => { + const id = mint(lastId, new Set()) + return Number.isSafeInteger(id) && id > lastId + }) + ) + } + ) + + /** + * The property above would also pass an allocator that returned + * `lastId + 1` without consulting the reservation set. Reserving the + * immediate successor forces the collision-recovery path to run from a + * reserved-range start, which is where the former ceiling threw. + */ + it.for(minters)( + 'recovers a free $name id when the successor of a reserved-range mark is taken', + ({ mint }) => { + fc.assert( + fc.property(reservedRangeHighWaterMark(), (lastId) => { + const reserved = new Set([lastId + 1]) + const id = mint(lastId, reserved) + return Number.isSafeInteger(id) && !reserved.has(id) && id !== lastId + }) + ) + } + ) +}) From 39d8e611acc71cbda701564432c41ed0b8f2b769 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 1 Oct 2026 05:14:31 -0700 Subject: [PATCH 18/18] test(litegraph): tighten safe ID recovery proof --- ...aph-duplicate-ids-reserved-mint-range.json | 599 ------------------ .../subgraph-nested-duplicate-ids.json | 4 +- .../tests/subgraph/subgraphNested.spec.ts | 32 - src/lib/litegraph/src/LGraph.test.ts | 78 +-- .../src/__fixtures__/nodeIdsAtFormerLimit.ts | 168 ----- .../nodeIdsFromReservedMintRange.ts | 42 +- .../src/idAllocation.property.test.ts | 73 +-- 7 files changed, 32 insertions(+), 964 deletions(-) delete mode 100644 browser_tests/assets/subgraphs/subgraph-duplicate-ids-reserved-mint-range.json delete mode 100644 src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts diff --git a/browser_tests/assets/subgraphs/subgraph-duplicate-ids-reserved-mint-range.json b/browser_tests/assets/subgraphs/subgraph-duplicate-ids-reserved-mint-range.json deleted file mode 100644 index e6eb82d3f0d..00000000000 --- a/browser_tests/assets/subgraphs/subgraph-duplicate-ids-reserved-mint-range.json +++ /dev/null @@ -1,599 +0,0 @@ -{ - "id": "9a37f747-e96b-4304-9212-7abcaad7bdac", - "revision": 0, - "last_node_id": 4462758126524329, - "last_link_id": 7729209487955825, - "nodes": [ - { - "id": 5, - "type": "1e38d8ea-45e1-48a5-aa20-966584201867", - "pos": [788, 433.5], - "size": [210, 108], - "flags": {}, - "order": 1, - "mode": 0, - "inputs": [ - { - "name": "string_a", - "type": "STRING", - "widget": { - "name": "string_a" - }, - "link": 4 - } - ], - "outputs": [ - { - "name": "STRING", - "type": "STRING", - "links": [5] - } - ], - "properties": { - "proxyWidgets": [["-1", "string_a"]] - }, - "widgets_values": [""] - }, - { - "id": 2, - "type": "PreviewAny", - "pos": [1135, 429], - "size": [250, 145.5], - "flags": {}, - "order": 2, - "mode": 0, - "inputs": [ - { - "name": "source", - "type": "*", - "link": 5 - } - ], - "outputs": [], - "properties": { - "Node name for S&R": "PreviewAny" - }, - "widgets_values": [null, null, false] - }, - { - "id": 1, - "type": "PrimitiveStringMultiline", - "pos": [456, 450], - "size": [225, 121.5], - "flags": {}, - "order": 0, - "mode": 0, - "inputs": [], - "outputs": [ - { - "name": "STRING", - "type": "STRING", - "links": [4] - } - ], - "properties": { - "Node name for S&R": "PrimitiveStringMultiline" - }, - "widgets_values": ["Outer\n"] - } - ], - "links": [ - [4, 1, 0, 5, 0, "STRING"], - [5, 5, 0, 2, 0, "STRING"] - ], - "groups": [], - "definitions": { - "subgraphs": [ - { - "id": "1e38d8ea-45e1-48a5-aa20-966584201867", - "version": 1, - "state": { - "lastGroupId": 0, - "lastNodeId": 6, - "lastLinkId": 9, - "lastRerouteId": 0 - }, - "revision": 0, - "config": {}, - "name": "New Subgraph", - "inputNode": { - "id": -10, - "bounding": [351, 432.5, 120, 60] - }, - "outputNode": { - "id": -20, - "bounding": [1315, 432.5, 120, 60] - }, - "inputs": [ - { - "id": "7bf3e1d4-0521-4b5c-92f5-47ca598b7eb4", - "name": "string_a", - "type": "STRING", - "linkIds": [1], - "localized_name": "string_a", - "pos": [451, 452.5] - } - ], - "outputs": [ - { - "id": "fbe975ba-d7c2-471e-a99a-a1e2c6ab466d", - "name": "STRING", - "type": "STRING", - "linkIds": [9], - "localized_name": "STRING", - "pos": [1335, 452.5] - } - ], - "widgets": [], - "nodes": [ - { - "id": 3, - "type": "StringConcatenate", - "pos": [815, 373], - "size": [347, 231], - "flags": {}, - "order": 2, - "mode": 0, - "inputs": [ - { - "localized_name": "string_a", - "name": "string_a", - "type": "STRING", - "widget": { - "name": "string_a" - }, - "link": 1 - }, - { - "localized_name": "string_b", - "name": "string_b", - "type": "STRING", - "widget": { - "name": "string_b" - }, - "link": 2 - } - ], - "outputs": [ - { - "localized_name": "STRING", - "name": "STRING", - "type": "STRING", - "links": [7] - } - ], - "properties": { - "Node name for S&R": "StringConcatenate" - }, - "widgets_values": ["", "", ""] - }, - { - "id": 6, - "type": "9be42452-056b-4c99-9f9f-7381d11c4454", - "pos": [955, 775], - "size": [210, 88], - "flags": {}, - "order": 1, - "mode": 0, - "inputs": [ - { - "localized_name": "string_a", - "name": "string_a", - "type": "STRING", - "widget": { - "name": "string_a" - }, - "link": 7 - } - ], - "outputs": [ - { - "localized_name": "STRING", - "name": "STRING", - "type": "STRING", - "links": [9] - } - ], - "properties": { - "proxyWidgets": [["-1", "string_a"]] - }, - "widgets_values": [""] - }, - { - "id": 4, - "type": "PrimitiveStringMultiline", - "pos": [313, 685], - "size": [325, 109], - "flags": {}, - "order": 0, - "mode": 0, - "inputs": [], - "outputs": [ - { - "localized_name": "STRING", - "name": "STRING", - "type": "STRING", - "links": [2] - } - ], - "properties": { - "Node name for S&R": "PrimitiveStringMultiline" - }, - "widgets_values": ["Inner 1\n"] - } - ], - "groups": [], - "links": [ - { - "id": 2, - "origin_id": 4, - "origin_slot": 0, - "target_id": 3, - "target_slot": 1, - "type": "STRING" - }, - { - "id": 1, - "origin_id": -10, - "origin_slot": 0, - "target_id": 3, - "target_slot": 0, - "type": "STRING" - }, - { - "id": 7, - "origin_id": 3, - "origin_slot": 0, - "target_id": 6, - "target_slot": 0, - "type": "STRING" - }, - { - "id": 6, - "origin_id": 6, - "origin_slot": 0, - "target_id": -20, - "target_slot": 1, - "type": "STRING" - }, - { - "id": 9, - "origin_id": 6, - "origin_slot": 0, - "target_id": -20, - "target_slot": 0, - "type": "STRING" - } - ], - "extra": {} - }, - { - "id": "9be42452-056b-4c99-9f9f-7381d11c4454", - "version": 1, - "state": { - "lastGroupId": 0, - "lastNodeId": 9, - "lastLinkId": 12, - "lastRerouteId": 0 - }, - "revision": 0, - "config": {}, - "name": "New Subgraph", - "inputNode": { - "id": -10, - "bounding": [680, 774, 120, 60] - }, - "outputNode": { - "id": -20, - "bounding": [1320, 774, 120, 60] - }, - "inputs": [ - { - "id": "01c05c51-86b5-4bad-b32f-9c911683a13d", - "name": "string_a", - "type": "STRING", - "linkIds": [4], - "localized_name": "string_a", - "pos": [780, 794] - } - ], - "outputs": [ - { - "id": "a8bcf3bf-a66a-4c71-8d92-17a2a4d03686", - "name": "STRING", - "type": "STRING", - "linkIds": [12], - "localized_name": "STRING", - "pos": [1340, 794] - } - ], - "widgets": [], - "nodes": [ - { - "id": 5, - "type": "StringConcatenate", - "pos": [860, 719], - "size": [400, 200], - "flags": {}, - "order": 2, - "mode": 0, - "inputs": [ - { - "localized_name": "string_a", - "name": "string_a", - "type": "STRING", - "widget": { - "name": "string_a" - }, - "link": 4 - }, - { - "localized_name": "string_b", - "name": "string_b", - "type": "STRING", - "widget": { - "name": "string_b" - }, - "link": 7 - } - ], - "outputs": [ - { - "localized_name": "STRING", - "name": "STRING", - "type": "STRING", - "links": [11] - } - ], - "properties": { - "Node name for S&R": "StringConcatenate" - }, - "widgets_values": ["", "", ""] - }, - { - "id": 6, - "type": "PrimitiveStringMultiline", - "pos": [401, 973], - "size": [400, 200], - "flags": {}, - "order": 0, - "mode": 0, - "inputs": [], - "outputs": [ - { - "localized_name": "STRING", - "name": "STRING", - "type": "STRING", - "links": [7] - } - ], - "properties": { - "Node name for S&R": "PrimitiveStringMultiline" - }, - "widgets_values": ["Inner 2\n"] - }, - { - "id": 9, - "type": "7c2915a5-5eb8-4958-a8fd-4beb30f370ce", - "pos": [1046, 985], - "size": [210, 88], - "flags": {}, - "order": 1, - "mode": 0, - "inputs": [ - { - "localized_name": "string_a", - "name": "string_a", - "type": "STRING", - "widget": { - "name": "string_a" - }, - "link": 11 - } - ], - "outputs": [ - { - "localized_name": "STRING", - "name": "STRING", - "type": "STRING", - "links": [12] - } - ], - "properties": { - "proxyWidgets": [["-1", "string_a"]] - }, - "widgets_values": [""] - } - ], - "groups": [], - "links": [ - { - "id": 4, - "origin_id": -10, - "origin_slot": 0, - "target_id": 5, - "target_slot": 0, - "type": "STRING" - }, - { - "id": 7, - "origin_id": 6, - "origin_slot": 0, - "target_id": 5, - "target_slot": 1, - "type": "STRING" - }, - { - "id": 11, - "origin_id": 5, - "origin_slot": 0, - "target_id": 9, - "target_slot": 0, - "type": "STRING" - }, - { - "id": 10, - "origin_id": 9, - "origin_slot": 0, - "target_id": -20, - "target_slot": 0, - "type": "STRING" - }, - { - "id": 12, - "origin_id": 9, - "origin_slot": 0, - "target_id": -20, - "target_slot": 0, - "type": "STRING" - } - ], - "extra": {} - }, - { - "id": "7c2915a5-5eb8-4958-a8fd-4beb30f370ce", - "version": 1, - "state": { - "lastGroupId": 0, - "lastNodeId": 8, - "lastLinkId": 10, - "lastRerouteId": 0 - }, - "revision": 0, - "config": {}, - "name": "New Subgraph", - "inputNode": { - "id": -10, - "bounding": [262, 1222, 120, 60] - }, - "outputNode": { - "id": -20, - "bounding": [1330, 1222, 120, 60] - }, - "inputs": [ - { - "id": "934a8baa-d79c-428c-8ec9-814ad437d7c7", - "name": "string_a", - "type": "STRING", - "linkIds": [9], - "localized_name": "string_a", - "pos": [362, 1242] - } - ], - "outputs": [ - { - "id": "4c3d243b-9ff6-4dcd-9dbf-e4ec8e1fc879", - "name": "STRING", - "type": "STRING", - "linkIds": [10], - "localized_name": "STRING", - "pos": [1350, 1242] - } - ], - "widgets": [], - "nodes": [ - { - "id": 7, - "type": "StringConcatenate", - "pos": [870, 1038], - "size": [400, 200], - "flags": {}, - "order": 1, - "mode": 0, - "inputs": [ - { - "localized_name": "string_a", - "name": "string_a", - "type": "STRING", - "widget": { - "name": "string_a" - }, - "link": 9 - }, - { - "localized_name": "string_b", - "name": "string_b", - "type": "STRING", - "widget": { - "name": "string_b" - }, - "link": 8 - } - ], - "outputs": [ - { - "localized_name": "STRING", - "name": "STRING", - "type": "STRING", - "links": [10] - } - ], - "properties": { - "Node name for S&R": "StringConcatenate" - }, - "widgets_values": ["", "", ""] - }, - { - "id": 8, - "type": "PrimitiveStringMultiline", - "pos": [442, 1296], - "size": [400, 200], - "flags": {}, - "order": 0, - "mode": 0, - "inputs": [], - "outputs": [ - { - "localized_name": "STRING", - "name": "STRING", - "type": "STRING", - "links": [8] - } - ], - "properties": { - "Node name for S&R": "PrimitiveStringMultiline" - }, - "widgets_values": ["Inner 3\n"] - } - ], - "groups": [], - "links": [ - { - "id": 8, - "origin_id": 8, - "origin_slot": 0, - "target_id": 7, - "target_slot": 1, - "type": "STRING" - }, - { - "id": 9, - "origin_id": -10, - "origin_slot": 0, - "target_id": 7, - "target_slot": 0, - "type": "STRING" - }, - { - "id": 10, - "origin_id": 7, - "origin_slot": 0, - "target_id": -20, - "target_slot": 0, - "type": "STRING" - } - ], - "extra": {} - } - ] - }, - "config": {}, - "extra": { - "ds": { - "scale": 1, - "offset": [-7, 144] - }, - "frontendVersion": "1.38.13" - }, - "version": 0.4 -} diff --git a/browser_tests/assets/subgraphs/subgraph-nested-duplicate-ids.json b/browser_tests/assets/subgraphs/subgraph-nested-duplicate-ids.json index c5469594a5d..e6eb82d3f0d 100644 --- a/browser_tests/assets/subgraphs/subgraph-nested-duplicate-ids.json +++ b/browser_tests/assets/subgraphs/subgraph-nested-duplicate-ids.json @@ -1,8 +1,8 @@ { "id": "9a37f747-e96b-4304-9212-7abcaad7bdac", "revision": 0, - "last_node_id": 5, - "last_link_id": 5, + "last_node_id": 4462758126524329, + "last_link_id": 7729209487955825, "nodes": [ { "id": 5, diff --git a/browser_tests/tests/subgraph/subgraphNested.spec.ts b/browser_tests/tests/subgraph/subgraphNested.spec.ts index 612131487f4..b9c029aba1e 100644 --- a/browser_tests/tests/subgraph/subgraphNested.spec.ts +++ b/browser_tests/tests/subgraph/subgraphNested.spec.ts @@ -30,38 +30,6 @@ test.describe('Nested Subgraphs', { tag: ['@subgraph'] }, () => { dispose() } }) - - /** - * The same workflow, with the two id counters raised into the range a - * different mint convention owns: `4462758126524329` is a node id this - * app minted through its CRDT-disjoint path, and `7729209487955825` is - * a link id the server-side agent minted. Both are real values taken - * from `cloud-frontend-prod` telemetry. - * - * Deduplicating this workflow's shared subgraph ids has to mint from - * those counters. While allocation was bounded at a fixed 1e8, that - * throw was unhandled and aborted the load, so the canvas stayed empty - * — the user-visible half of FE-3007, reported from production as - * intermittently broken copy, paste, clone and workflow open. - */ - test('Loads a workflow whose ID counters sit in the reserved mint range', async ({ - comfyPage - }) => { - const SUBGRAPH_HOST_TYPE = '1e38d8ea-45e1-48a5-aa20-966584201867' - - await comfyPage.workflow.loadWorkflow( - 'subgraphs/subgraph-duplicate-ids-reserved-mint-range' - ) - - for (const type of [ - 'PreviewAny', - 'PrimitiveStringMultiline', - SUBGRAPH_HOST_TYPE - ]) { - const nodes = await comfyPage.nodeOps.getNodeRefsByType(type) - expect(nodes, `expected one ${type} node after load`).toHaveLength(1) - } - }) }) test.describe( diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index 0939326c8e6..c2120e2dd86 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -1,6 +1,6 @@ import { toGroupId } from '@/types/groupId' import { graphScopeOf } from '@/types/graphScopeId' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { assert, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { NodeLifecycleEvent } from '@/lib/litegraph/src/infrastructure/LGraphEventMap' import type { LGraphCanvas } from '@/lib/litegraph/src/LGraphCanvas' @@ -27,10 +27,6 @@ import { isRootGraphDocBound, registerDocBoundRootGraphProbe } from '@/lib/litegraph/src/docBoundGraphs' -import { - AGENT_RESERVED_BIT, - CRDT_DISJOINT_FLOOR -} from '@/lib/litegraph/src/idAllocation' import type { UUID } from '@/utils/uuid' import { createUuidv4, zeroUuid } from '@/utils/uuid' import { useEntityIdStore } from '@/stores/entityIdStore' @@ -61,7 +57,6 @@ import { } from './__fixtures__/duplicateLinks' import { duplicateSubgraphNodeIds } from './__fixtures__/duplicateSubgraphNodeIds' import { nestedSubgraphProxyWidgets } from './__fixtures__/nestedSubgraphProxyWidgets' -import { nodeIdsAtFormerLimit } from './__fixtures__/nodeIdsAtFormerLimit' import { nodeIdsFromReservedMintRange } from './__fixtures__/nodeIdsFromReservedMintRange' import { uniqueSubgraphNodeIds } from './__fixtures__/uniqueSubgraphNodeIds' import { test } from './__fixtures__/testExtensions' @@ -2390,50 +2385,15 @@ describe('deduplicateSubgraphNodeIds (via configure)', () => { }) }) - it('remaps duplicate node IDs above the former fixed limit', () => { - const graph = new LGraph() - - graph.configure(structuredClone(nodeIdsAtFormerLimit)) - - expect(nodeIdSet(graph, SUBGRAPH_B)).toEqual( - new Set([ - toNodeId(100_000_001), - toNodeId(100_000_002), - toNodeId(100_000_003) - ]) - ) - expect(graph.state.lastNodeId).toBe(100_000_003) - }) - - /** - * The production defect (FE-3007), reproduced from its own telemetry - * rather than from a value near the former fixed ceiling. See - * `nodeIdsFromReservedMintRange` for the provenance of both ids. - * - * This is the discriminating case: `nodeIdsAtFormerLimit` above still - * passes if the ceiling is merely raised instead of removed, because its - * ids sit just over 1e8. These ids are ~45 million and ~77 million times - * that ceiling, in both the node and link classes `findNextAvailableId` - * serves, so a bound anywhere below them fails here. - * - * The fixture also reserves the successor of the node high-water mark, so - * the node remap reaches collision recovery rather than returning - * `lastNodeId + 1` off the fast path. A ceiling reintroduced inside - * `findNextAvailableId` alone is therefore caught too, not only one on - * the mint. `idAllocation.property.test.ts` states the same property over - * the whole reserved range and all four id classes. - */ it('remaps duplicate subgraph IDs when a reserved-range mint has raised the counters', () => { const graph = new LGraph() const observedNodeId = 4_462_758_126_524_329 - const observedLinkId = 7_729_209_487_955_825 + const observedLinkId = toLinkId(7_729_209_487_955_825) expect(() => { graph.configure(structuredClone(nodeIdsFromReservedMintRange)) }).not.toThrow() - // Not +1: that candidate is reserved by a root node, so the first - // remap recovers past it. expect(nodeIdSet(graph, SUBGRAPH_B)).toEqual( new Set([ toNodeId(observedNodeId + 2), @@ -2447,36 +2407,10 @@ describe('deduplicateSubgraphNodeIds (via configure)', () => { new Set([toNodeId(3), toNodeId(8), toNodeId(37)]) ) - const linkIdsB = [...graph.subgraphs.get(SUBGRAPH_B)!.links.keys()] - expect(linkIdsB).toEqual([observedLinkId + 1]) - expect(graph.state.lastLinkId).toBe(observedLinkId + 1) - - for (const id of [...nodeIdSet(graph, SUBGRAPH_B)].map(Number)) { - expect(Number.isSafeInteger(id)).toBe(true) - } - }) - - /** - * The remapped ids above sit inside the same 2^40-wide partition as the - * high-water mark that produced them, because a sequential mint only - * advances the low bits. Pinned explicitly so the reserved-mint collision - * contract is visible rather than incidental: a node id remapped off an - * id this app minted (bit 41) keeps bit 41 and keeps bit 40 clear, so it - * cannot land in the agent's `2**40 | random52` range; a link id remapped - * off an agent-minted id (bit 40) stays in that range. - */ - it('keeps remapped IDs inside the reserved partition they were minted from', () => { - const graph = new LGraph() - graph.configure(structuredClone(nodeIdsFromReservedMintRange)) - - for (const id of [...nodeIdSet(graph, SUBGRAPH_B)].map(BigInt)) { - expect(id & CRDT_DISJOINT_FLOOR).not.toBe(0n) - expect(id & AGENT_RESERVED_BIT).toBe(0n) - } - - for (const id of graph.subgraphs.get(SUBGRAPH_B)!.links.keys()) { - expect(BigInt(id) & AGENT_RESERVED_BIT).not.toBe(0n) - } + const subgraphB = graph.subgraphs.get(SUBGRAPH_B) + assert.exists(subgraphB) + expect([...subgraphB.links.keys()]).toEqual([toLinkId(observedLinkId + 1)]) + expect(graph.state.lastLinkId).toBe(toLinkId(observedLinkId + 1)) }) it('is a no-op when subgraph node IDs are already unique', () => { diff --git a/src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts b/src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts deleted file mode 100644 index b95b110d5a8..00000000000 --- a/src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts +++ /dev/null @@ -1,168 +0,0 @@ -import { - SUBGRAPH_INPUT_ID, - SUBGRAPH_OUTPUT_ID -} from '@/lib/litegraph/src/constants' -import type { SerialisableGraph } from '@/lib/litegraph/src/types/serialisation' - -export const nodeIdsAtFormerLimit = { - id: 'cccccccc-cccc-4ccc-8ccc-cccccccccccc', - version: 1, - revision: 0, - state: { - lastNodeId: 99_999_999, - lastLinkId: 10, - lastGroupId: 0, - lastRerouteId: 0 - }, - nodes: [ - { - id: 102, - type: '11111111-1111-4111-8111-111111111111', - pos: [0, 0], - size: [200, 100], - flags: {}, - order: 0, - mode: 0, - properties: { proxyWidgets: [['3', 'seed']] } - }, - { - id: 103, - type: '22222222-2222-4222-8222-222222222222', - pos: [300, 0], - size: [200, 100], - flags: {}, - order: 1, - mode: 0, - properties: { proxyWidgets: [['8', 'prompt']] } - }, - { - id: 100_000_000, - type: 'dummy', - pos: [600, 0], - size: [100, 50], - flags: {}, - order: 2, - mode: 0 - } - ], - definitions: { - subgraphs: [ - { - id: '11111111-1111-4111-8111-111111111111', - version: 1, - revision: 0, - state: { - lastNodeId: 0, - lastLinkId: 0, - lastGroupId: 0, - lastRerouteId: 0 - }, - name: 'SubgraphA', - config: {}, - inputNode: { id: SUBGRAPH_INPUT_ID, bounding: [10, 100, 150, 126] }, - outputNode: { id: SUBGRAPH_OUTPUT_ID, bounding: [400, 100, 140, 126] }, - inputs: [], - outputs: [], - widgets: [{ id: 3, name: 'seed' }], - nodes: [ - { - id: 3, - type: 'dummy', - pos: [0, 0], - size: [100, 50], - flags: {}, - order: 0, - mode: 0 - }, - { - id: 8, - type: 'dummy', - pos: [0, 0], - size: [100, 50], - flags: {}, - order: 1, - mode: 0 - }, - { - id: 37, - type: 'dummy', - pos: [0, 0], - size: [100, 50], - flags: {}, - order: 2, - mode: 0 - } - ], - links: [ - { - id: 1, - origin_id: 3, - origin_slot: 0, - target_id: 8, - target_slot: 0, - type: 'number' - } - ], - groups: [] - }, - { - id: '22222222-2222-4222-8222-222222222222', - version: 1, - revision: 0, - state: { - lastNodeId: 0, - lastLinkId: 0, - lastGroupId: 0, - lastRerouteId: 0 - }, - name: 'SubgraphB', - config: {}, - inputNode: { id: SUBGRAPH_INPUT_ID, bounding: [10, 100, 150, 126] }, - outputNode: { id: SUBGRAPH_OUTPUT_ID, bounding: [400, 100, 140, 126] }, - inputs: [], - outputs: [], - widgets: [{ id: 8, name: 'prompt' }], - nodes: [ - { - id: 3, - type: 'dummy', - pos: [0, 0], - size: [100, 50], - flags: {}, - order: 0, - mode: 0 - }, - { - id: 8, - type: 'dummy', - pos: [0, 0], - size: [100, 50], - flags: {}, - order: 1, - mode: 0 - }, - { - id: 37, - type: 'dummy', - pos: [0, 0], - size: [100, 50], - flags: {}, - order: 2, - mode: 0 - } - ], - links: [ - { - id: 2, - origin_id: 3, - origin_slot: 0, - target_id: 37, - target_slot: 0, - type: 'string' - } - ], - groups: [] - } - ] - } -} as const satisfies SerialisableGraph diff --git a/src/lib/litegraph/src/__fixtures__/nodeIdsFromReservedMintRange.ts b/src/lib/litegraph/src/__fixtures__/nodeIdsFromReservedMintRange.ts index 12fac25721d..72c18d07480 100644 --- a/src/lib/litegraph/src/__fixtures__/nodeIdsFromReservedMintRange.ts +++ b/src/lib/litegraph/src/__fixtures__/nodeIdsFromReservedMintRange.ts @@ -4,36 +4,7 @@ import { } from '@/lib/litegraph/src/constants' import type { SerialisableGraph } from '@/lib/litegraph/src/types/serialisation' -/** - * The shape that actually reached production, rather than a synthetic value - * near the former fixed ceiling. - * - * Both ids below are real, taken from `cloud-frontend-prod` telemetry: - * - * - `4462758126524329` is a node id this app minted itself, through - * `mintCrdtDisjointNodeId` — bit 41 set, bit 40 clear. - * - `7729209487955825` is a link id the server-side agent minted, through - * comfy-cli's `mint_id()` (`2**40 | random52`) — bit 40 set. - * - * The two id classes are poisoned by the two different routes production - * took, deliberately: - * - * - `lastNodeId` arrives already raised, as it is on the live graph that - * copy, paste and clone run against: `observeNodeId` raised it when this - * app's own node materialized, long before any deduplication pass. - * - `lastLinkId` starts at zero and is raised *inside* the pass, because - * `deduplicateSubgraphLinkIds` observes every reserved link id — including - * the agent-minted one interior to SubgraphA — before it remaps anything. - * - * Observation has never had a ceiling; minting used to. So in both classes - * the first duplicate subgraph-scoped id that needed remapping minted from a - * counter in the quadrillions and threw `Node ID space exhausted` on its - * first candidate, aborting copy, paste, clone and workflow load. - * - * Both subgraph definitions therefore share node ids `[3, 8, 37]` AND link - * id `7729209487955825`, so configuring this graph has to remap in both id - * classes from a reserved-range high-water mark. - */ +// Synthetic reproducer around two observed high-range ID values. export const nodeIdsFromReservedMintRange = { id: 'dddddddd-dddd-4ddd-8ddd-dddddddddddd', version: 1, @@ -74,16 +45,7 @@ export const nodeIdsFromReservedMintRange = { order: 2, mode: 0 }, - /** - * The successor of the node high-water mark, reserved so the first - * remap candidate is already taken and minting has to fall through to - * collision recovery — the branch the former ceiling guarded. Node - * deduplication reserves root ids without observing them, so this does - * not itself raise `lastNodeId`; link, group and reroute deduplication - * observe every reserved id, so the same gap cannot be built there from - * a fixture, and `idAllocation.property.test.ts` covers those three - * recovery paths directly instead. - */ + // Reserve the successor so remapping exercises collision recovery. { id: 4_462_758_126_524_330, type: 'dummy', diff --git a/src/lib/litegraph/src/idAllocation.property.test.ts b/src/lib/litegraph/src/idAllocation.property.test.ts index 01497eddbdc..c268007707d 100644 --- a/src/lib/litegraph/src/idAllocation.property.test.ts +++ b/src/lib/litegraph/src/idAllocation.property.test.ts @@ -177,29 +177,7 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen }) }) -/** - * The production failure (FE-3007) generalized past the one trace in - * `LGraph.test.ts`. Observation has no ceiling, so any id a *different* - * mint convention produced — the agent's `2**40 | random52`, or this app's - * own `crdt-disjoint` floor at 2^41 — becomes a sequential counter's - * high-water mark the moment it is seen. Minting then has to keep working - * from there. - * - * Stated over the whole reserved range rather than at one sampled value, - * and over all four id classes, because `findNextAvailableId` is shared by - * every one of them: the four Sentry groups in this family entered through - * three different entry points and two different id classes, and differed - * only in which `remap*` called the allocator. Any reintroduced fixed - * bound below `Number.MAX_SAFE_INTEGER` fails this, wherever it is put. - */ describe('sequential minting survives a counter raised into the reserved mint range', () => { - /** - * The whole range a reserved-bit id can occupy, from the agent's mint - * floor to just below the safe-integer boundary — deliberately not just - * the production samples, so a ceiling placed anywhere in between is - * caught. The top of the range is held two below `MAX_SAFE_INTEGER` so a - * mint has somewhere to go without exercising the separate wrap path. - */ const reservedRangeHighWaterMark = (): fc.Arbitrary => fc.integer({ min: Number(AGENT_RESERVED_BIT), @@ -241,34 +219,27 @@ describe('sequential minting survives a counter raised into the reserved mint ra } ] as const - it.for(minters)( - 'mints a fresh safe $name id above any reserved-range high-water mark', - ({ mint }) => { - fc.assert( - fc.property(reservedRangeHighWaterMark(), (lastId) => { - const id = mint(lastId, new Set()) - return Number.isSafeInteger(id) && id > lastId - }) - ) - } - ) + it.for(minters)('mints the final safe $name id', ({ mint }) => { + expect(mint(Number.MAX_SAFE_INTEGER - 1, new Set())).toBe( + Number.MAX_SAFE_INTEGER + ) + }) - /** - * The property above would also pass an allocator that returned - * `lastId + 1` without consulting the reservation set. Reserving the - * immediate successor forces the collision-recovery path to run from a - * reserved-range start, which is where the former ceiling threw. - */ - it.for(minters)( - 'recovers a free $name id when the successor of a reserved-range mark is taken', - ({ mint }) => { - fc.assert( - fc.property(reservedRangeHighWaterMark(), (lastId) => { - const reserved = new Set([lastId + 1]) - const id = mint(lastId, reserved) - return Number.isSafeInteger(id) && !reserved.has(id) && id !== lastId - }) - ) - } - ) + it('mints a node ID above any reserved-range high-water mark', () => { + const mint = minters[0].mint + fc.assert( + fc.property(reservedRangeHighWaterMark(), (lastId) => { + expect(mint(lastId, new Set())).toBe(lastId + 1) + }) + ) + }) + + it('recovers the next node ID when the successor is reserved', () => { + const mint = minters[0].mint + fc.assert( + fc.property(reservedRangeHighWaterMark(), (lastId) => { + expect(mint(lastId, new Set([lastId + 1]))).toBe(lastId + 2) + }) + ) + }) })