From 5724f237b57bd8636eaac009eec88c7a04b6d6fa Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sun, 4 Oct 2026 05:09:43 -0700 Subject: [PATCH 1/5] [backport cloud/1.55] fix(litegraph): recover graph ID allocation safely (#19239) (#20093) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Why `cloud/1.55` is the line production runs, and it still throws `Node ID space exhausted`. [#19239](https://github.com/Comfy-Org/ComfyUI_frontend/pull/19239) fixed this on `main` on 2026-10-01, but **all four backports failed on cherry-pick conflicts** the same minute it merged (bot comments at 16:43:57Z–16:44:02Z), and the manual handoff those comments asked for was never picked up. Three days later the defect is still live: a fresh `cloud-frontend-prod` alert fired at **2026-10-04T10:22:51Z** on release **1.55.16** ([Sentry `CLOUD-FRONTEND-PROD-225`](https://comfy-org.sentry.io/issues/7772166974/), event `2cdb104c0d5649daa34f7d9bb6195edf`), terminating at the same `findNextAvailableId` throw site as the rest of the family. The conflict was partly the stacking conflict the bot guessed at — the five PRs it named have all merged since, which cut the conflict set from ~15 files to 5. The remaining 5 are real and resolved by hand here. ## What this is A `-x` cherry-pick of `ec90e0a` plus one clearly-scoped adaptation commit. **No change to ID allocation semantics, to reserved-mint recovery, or to any assertion carried from #19239.** Four `main`-only divergences, each resolved toward the release line rather than by importing unrelated `main` content: | Divergence | Resolution | |---|---| | `SelectionView` / `SelectionViewCache` in `LGraphCanvas.ts` | Dropped — they belong to `main`'s selection-store refactor, absent here; `SelectedItemsView` is undefined on this line. The PR-authored `InitializedClipboardItems` and `ClipboardPasteContext` are kept, as the rewritten paste path uses them. | | `surface: 'graph'` on two `reportError` calls in `rollbackClipboardPaste` | Dropped — `surface` is a required field of `ReportErrorOptions` on `main` only. Rollback behaviour unchanged; only a telemetry dimension this line does not model is omitted. | | `pastedClipboard`, `mountRichTextEditor`, `readFileSync`, `join` test helpers | Dropped — their consuming cases are not on this line, so they are unused here. `clipboardHtml` is kept (6 call sites). | | `webSessionFixture.ts`, `templateInputService.ts` | Do not exist on `cloud/1.55`; resolved as deleted. Both of #19239's changes to them were incidental to this fix (a `try`/`finally` around `use(page)`; zod `TWidgetValue` typing). | ## Verification on the exact head (`5befe57`) - `pnpm typecheck` — **0 errors**. - Focused suite — **13 files, 425 tests, all passing** (`idAllocation`, `idAllocation.property`, `subgraphDeduplication`, `LGraph`, `LGraph.failedConfigure`, `LGraphCanvas.clipboard`, `usePaste`, `useCopy`, `groupNode`, `widgetInputs`, `entityIdStore`, `linkBadges`, `workflowSchema`). - `eslint` on every changed file — **0 errors** (58 inherited layer warnings, matching #19239). **Red proof on the parent, using the real production IDs.** On pristine `origin/cloud/1.55` a graph whose counters sit at the two IDs that actually appear in `cloud-frontend-prod` telemetry for this family — `4462758126524329` (app `crdt-disjoint` mint, bit 41) and `7729209487955825` (agent `2**40 | random52` mint, bit 40) — throws `Node ID space exhausted`. On this branch the equivalent case asserts `.not.toThrow()` and passes as part of the 425. The black-box regression proof is preserved: `browser_tests/assets/subgraphs/subgraph-nested-duplicate-ids.json` carries those same two IDs as `last_node_id` / `last_link_id`, and is consumed by `subgraphNested.spec.ts` and `subgraphSerialization.spec.ts`. ## Scope `cloud/1.55` only — the line production runs and the line the live alert came from. `cloud/1.54`, `core/1.54` and `core/1.55` still carry the ceiling (`MAX_ID = 100_000_000` at `src/lib/litegraph/src/subgraph/subgraphDeduplication.ts:32`) and need the same treatment; their conflict sets differ per branch. Tracked by [FE-3007](https://linear.app/comfyorg/issue/FE-3007/e15-node-id-space-exhausted-explains-the-intermittent-cloud-copy-paste). Justification: Production release backport of the human-reviewed #19239 safe-ID fix after all automated backports failed — scope is limited to cloud/1.55, the release adaptations preserve semantics and assertions, the production-ID browser regression proof is retained, and all exact-head required checks plus the target-aware gate pass. --------- Co-authored-by: Amp (cherry picked from commit b6c760c1a1cf288541b5aee69679a0ba7f9b3117) --- .../subgraph-nested-duplicate-ids.json | 4 +- browser_tests/tests/copyPaste.spec.ts | 40 ++ eslint-suppressions.json | 5 - src/composables/useCopy.test.ts | 2 +- src/composables/useCopy.ts | 2 +- src/composables/usePaste.test.ts | 144 +++- src/composables/usePaste.ts | 27 +- src/extensions/core/groupNode.test.ts | 21 + src/extensions/core/groupNode.ts | 27 +- src/extensions/core/rerouteNode.ts | 3 +- src/extensions/core/widgetInputs.test.ts | 10 + src/extensions/core/widgetInputs.ts | 5 +- .../src/LGraph.failedConfigure.test.ts | 37 +- src/lib/litegraph/src/LGraph.test.ts | 33 +- src/lib/litegraph/src/LGraph.ts | 176 +++-- .../src/LGraphCanvas.clipboard.test.ts | 464 +++++++++--- src/lib/litegraph/src/LGraphCanvas.ts | 659 +++++++++--------- src/lib/litegraph/src/LGraphNode.ts | 6 +- ...ted.ts => nodeIdsFromReservedMintRange.ts} | 33 +- .../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 | 103 ++- src/lib/litegraph/src/idAllocation.test.ts | 208 +++++- src/lib/litegraph/src/idAllocation.ts | 179 ++++- src/lib/litegraph/src/interfaces.ts | 6 +- .../litegraph/src/subgraph/SubgraphInput.ts | 72 +- .../src/subgraph/SubgraphInputNode.ts | 7 +- .../litegraph/src/subgraph/SubgraphOutput.ts | 7 +- .../subgraph/subgraphDeduplication.test.ts | 135 +++- .../src/subgraph/subgraphDeduplication.ts | 173 +++-- src/lib/litegraph/src/utils/type.ts | 9 +- .../validation/schemas/workflowSchema.test.ts | 85 ++- .../validation/schemas/workflowSchema.ts | 171 ++++- src/stores/entityIdStore.test.ts | 6 +- 35 files changed, 2089 insertions(+), 777 deletions(-) rename src/lib/litegraph/src/__fixtures__/{nodeIdSpaceExhausted.ts => nodeIdsFromReservedMintRange.ts} (86%) 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/copyPaste.spec.ts b/browser_tests/tests/copyPaste.spec.ts index 3bf3e0cf996..d6235524158 100644 --- a/browser_tests/tests/copyPaste.spec.ts +++ b/browser_tests/tests/copyPaste.spec.ts @@ -191,6 +191,46 @@ test.describe('Copy Paste', { tag: ['@screenshot', '@workflow'] }, () => { }) }) + test('Sparse clipboard widget values keep their original indices', async ({ + comfyPage + }) => { + 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', + `
Text` + ) + 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/eslint-suppressions.json b/eslint-suppressions.json index 2900c760e98..855b1c060b7 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 84c3dabef69..e8a48f99304 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 { @@ -68,6 +69,11 @@ function createDataTransfer(files: File[] = []): DataTransfer { return dataTransfer } +function clipboardHtml(data: unknown, attribute = 'data-comfy-metadata') { + const encoded = btoa(JSON.stringify(data)) + return `
Text` +} + const mockCanvas = { current_node: null as LGraphNode | null, graph: { @@ -621,9 +627,14 @@ describe('usePaste', () => { }) it('should handle clipboard items with metadata', async () => { - const data = { test: 'data' } - const encoded = btoa(JSON.stringify(data)) - const html = `
` + const data = { + nodes: [], + groups: [], + reroutes: [], + links: [], + subgraphs: [] + } + const html = clipboardHtml(data) usePaste() @@ -641,6 +652,130 @@ describe('usePaste', () => { }) }) + it('accepts validated legacy data-metadata clipboard items', async () => { + const data = { nodes: [] } + const html = clipboardHtml(data, 'data-metadata') + + 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(mockCanvas._deserializeItems).toHaveBeenCalledWith( + data, + expect.any(Object) + ) + }) + }) + + 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) + document.dispatchEvent( + new ClipboardEvent('paste', { clipboardData: dataTransfer }) + ) + + await vi.waitFor(() => { + expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() + expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + }) + }) + + 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() + + 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('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 html = clipboardHtml(data) + + 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, @@ -653,8 +788,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 f88cf3a35b3..9d7d75d3ba2 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,34 @@ 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( + /^
<\/span><\/div>Text<\/span>$/ + )?.[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), {}) + parsed = JSON.parse(decodedData) + } catch (err) { + useErrorHandling().toastErrorHandler(err) + return true + } + + const clipboardItems = zClipboardItems.safeParse(parsed) + if (!clipboardItems.success) { + useErrorHandling().toastErrorHandler(clipboardItems.error) return true + } + + try { + useCanvasStore().getCanvas()._deserializeItems(clipboardItems.data, {}) } catch (err) { - console.error(err) + useErrorHandling().toastErrorHandler(err) } - return false + return true } function isWorkflow( diff --git a/src/extensions/core/groupNode.test.ts b/src/extensions/core/groupNode.test.ts index 346e7a4dad8..173fc3f75f0 100644 --- a/src/extensions/core/groupNode.test.ts +++ b/src/extensions/core/groupNode.test.ts @@ -134,6 +134,27 @@ 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] satisfies GroupNodeLink + const config = configFrom([linkWithoutType]) + + expect( + config.getNodeDef({ index: 0, type: 'PrimitiveNode' })?.output + ).toEqual([null]) + }) + + test.for([{ unexpected: true }, ['IMAGE', 42]])( + 'normalizes an invalid primitive link type to null', + (invalidType) => { + const link = [0, 0, 1, 0, 1, invalidType] satisfies 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 d0247e942d2..b132c6b1f6b 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 { @@ -53,12 +54,20 @@ export type GroupNodeLink = [ targetNodeIndex: number, targetSlot: number, sourceNodeId: SerializedNodeId, - type: ISlotType + type?: unknown ] 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>> @@ -229,7 +238,14 @@ export class GroupNodeConfig { if (!linksFrom) return const firstLink = linksFrom[0]?.at(0) - let type: string | number | null = firstLink?.[5] ?? null + const linkedType: unknown = firstLink?.at(5) + let type: string | number | null = firstLink + ? linkedType == null + ? null + : isSlotType(linkedType) + ? slotTypeKey(linkedType) + : null + : null if (type === 'COMBO') { // Use the array items const output = node.outputs?.[0] @@ -311,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 } @@ -326,13 +342,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.test.ts b/src/extensions/core/widgetInputs.test.ts index e4a35707183..3ef269b4563 100644 --- a/src/extensions/core/widgetInputs.test.ts +++ b/src/extensions/core/widgetInputs.test.ts @@ -265,6 +265,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 c8251728cff..87a54395496 100644 --- a/src/extensions/core/widgetInputs.ts +++ b/src/extensions/core/widgetInputs.ts @@ -196,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 && !(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.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.test.ts b/src/lib/litegraph/src/LGraph.test.ts index c0ea3e53bfc..d460e8ac8cf 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' @@ -57,7 +57,7 @@ import { } from './__fixtures__/duplicateLinks' import { duplicateSubgraphNodeIds } from './__fixtures__/duplicateSubgraphNodeIds' import { nestedSubgraphProxyWidgets } from './__fixtures__/nestedSubgraphProxyWidgets' -import { nodeIdSpaceExhausted } from './__fixtures__/nodeIdSpaceExhausted' +import { nodeIdsFromReservedMintRange } from './__fixtures__/nodeIdsFromReservedMintRange' import { uniqueSubgraphNodeIds } from './__fixtures__/uniqueSubgraphNodeIds' import { test } from './__fixtures__/testExtensions' @@ -2385,11 +2385,32 @@ describe('deduplicateSubgraphNodeIds (via configure)', () => { }) }) - it('throws when node ID space is exhausted', () => { + 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 = toLinkId(7_729_209_487_955_825) + expect(() => { - const graph = new LGraph() - graph.configure(structuredClone(nodeIdSpaceExhausted)) - }).toThrow('Node ID space exhausted') + graph.configure(structuredClone(nodeIdsFromReservedMintRange)) + }).not.toThrow() + + 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 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/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 3809e3914b6..8b9071e74c6 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -45,7 +45,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, @@ -53,9 +58,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 { collectingSeveredLinks, @@ -69,6 +79,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, @@ -188,9 +199,6 @@ import { runExtensionSerializeHook } from './extensionPersistence' import { - collectReservedGroupIds, - collectReservedLinkIds, - collectReservedRerouteIds, normalizeSubgraphDefinitions, topologicalSortSubgraphs } from './subgraph/subgraphDeduplication' @@ -243,6 +251,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 /** Keep the subgraph definitions the node references; the caller re-creates the node elsewhere in the same root graph. */ preserveSubgraphDefinitions?: boolean } @@ -270,6 +280,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 }) @@ -1379,7 +1397,7 @@ export class LGraph groupId === -1 || layoutStore.getGroupLayout(this.rootGraph.id, groupId) ) { - node.id = mintGroupId(state) + node.id = mintGroupId(state, groupIdReservations(this.rootGraph)) } observeGroupId(state, node.id) @@ -1408,10 +1426,23 @@ export class LGraph throw 'LiteGraph: max number of nodes in a graph reached' } + const reservedNodeIds: ReservedIdIndex = { + has: (candidate) => + [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) + node.id = mintNodeId(state, mintMode, reservedNodeIds) } else { observeNodeId(state, node.id) } @@ -1426,7 +1457,7 @@ export class LGraph node.graph = this attachNodeToStores(this, node, () => - mintNodeId(state, nodeIdMintModeFor(this)) + mintNodeId(state, nodeIdMintModeFor(this), reservedNodeIds) ) this._nodes.push(node) @@ -1504,7 +1535,7 @@ export class LGraph return } // cannot be removed - if (node.ignore_remove) { + if (node.ignore_remove && !options.force) { console.warn('LiteGraph: node cannot be removed', node) return } @@ -1554,38 +1585,40 @@ export class LGraph this.releaseSubgraphs(findReleasableSubgraphs(this.rootGraph, node)) } - // callback - node.onRemoved?.() - clearNodeOwnedStoreState(node) - - const order = node.order - useExecutionOrderStore().remove(graphScopeOf(this), node.id) - detachNodeFromStores(this, node) - detachNodeLayout(node) + try { + node.onRemoved?.() + } finally { + const order = node.order + try { + clearNodeOwnedStoreState(node) + useExecutionOrderStore().remove(graphScopeOf(this), node.id) + detachNodeFromStores(this, node) + detachNodeLayout(node) + + 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) - 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 }) emitGraphIntent({ type: 'remove_node', graph: this, node, removedLinkIds }) @@ -1871,7 +1904,7 @@ export class LGraph addFloatingLink(link: LLink): LLink | undefined { if (link.id === -1) { - link.id = mintLinkId(this.state) + link.id = mintLinkId(this.state, linkIdReservations(this.rootGraph)) } if (!registerLinkTopology(this, link)) return @@ -1980,7 +2013,9 @@ export class LGraph floating }: OptionalProps): Reroute | undefined { const rerouteId = - id === undefined ? mintRerouteId(this.state) : toRerouteId(id) + id === undefined + ? mintRerouteId(this.state, rerouteIdReservations(this.rootGraph)) + : toRerouteId(id) observeRerouteId(this.state, rerouteId) const existingReroute = this.reroutes.get(rerouteId) @@ -2125,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) @@ -2142,27 +2177,27 @@ 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) ) - 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) { + try { + this.releaseSubgraphs(subgraphs) + } catch (cleanupError) { + const combinedError = new AggregateError( + [error, cleanupError], + 'Subgraph configuration and rollback both failed' + ) + combinedError.cause = error + throw combinedError + } + throw error + } } private createNormalizedSubgraph(normalized: ExportedSubgraph): Subgraph { @@ -2711,7 +2746,10 @@ 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, + groupIdReservations(this.rootGraph) + ) groupInfo.id = groupId const group = new LGraphGroup(groupInfo.title, groupId) this.add(group, true) @@ -2801,7 +2839,10 @@ 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, + rerouteIdReservations(this.rootGraph) + ) const migratedReroute = this.setReroute({ id: migratedId, pos: [reroute.pos[0] + offsetX, reroute.pos[1] + offsetY], @@ -3148,20 +3189,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 @@ -3210,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 a9d1095eb59..1d6c5456cde 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, @@ -65,141 +65,393 @@ 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] + const result = canvas._deserializeItems(parsed, {}) + + 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('emits one complete canvas change pair for public paste', () => { + const nodeType = 'test/clipboard-change-hooks' + registerClipboardNodeType(nodeType) + const rootGraph = new LGraph() + 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')) + + canvas.pasteFromClipboard() + + 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' + 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) - remapClipboardSubgraphNodeIds(parsed, 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([]) + }) - const remappedSubgraph = parsed.subgraphs?.[0] - expect(remappedSubgraph).toBeDefined() + 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) - const remappedLink = remappedSubgraph?.links?.[0] - expect(remappedLink).toBeDefined() + expect(() => + canvas._deserializeItems( + { nodes: [createSerialisedNode(1, nodeType)] }, + {} + ) + ).toThrow('onAdded failed') + expect(callbackRan).toBe(true) + expect(rootGraph.nodes).toEqual([]) + }) - const remappedInteriorId = remappedSubgraph?.nodes?.[0]?.id - expect(remappedInteriorId).not.toBe(1) - expect(remappedLink?.origin_id).toBe(remappedInteriorId) - expect(remappedLink?.target_id).toBe(remappedInteriorId) + 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') + } - const remappedNode = parsed.nodes?.[0] - expect(remappedNode).toBeDefined() - expect(remappedNode?.properties?.proxyWidgets).toStrictEqual([ - [String(remappedInteriorId), 'seed'] - ]) + override onRemoved(): void { + removalRan = true + } + } + LiteGraph.registerNodeType(nodeType, PartiallyConfiguredNode) + const rootGraph = new LGraph() + const canvas = createCanvas(rootGraph) + const subgraph = createClipboardSubgraph(createUuidv4()) + subgraph.nodes = [createSerialisedNode(1, nodeType)] + + expect(() => + canvas._deserializeItems({ subgraphs: [subgraph] }, {}) + ).toThrow('subgraph configure failed') + expect(removalRan).toBe(true) + expect(rootGraph.subgraphs.size).toBe(0) }) - it('remaps pasted SubgraphNode previewExposures sourceNodeId references', () => { + 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 existingNode = new LGraphNode('existing') - existingNode.id = toNodeId(1) - rootGraph.add(existingNode) + 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 + } - 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: [] + 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 topologyExisted = false + class ThrowingConfiguredNode extends LGraphNode { + override onGraphConfigured(): void { + 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() + 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 }] - const hostInfo = createSerialisedNode(99, subgraphId) - hostInfo.properties = { - previewExposures: [ + expect(() => + canvas._deserializeItems( { - name: '$$canvas-image-preview', - sourceNodeId: '1', - sourcePreviewName: '$$canvas-image-preview' - } - ] + 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(topologyExisted).toBe(true) + expect(rootGraph.nodes).toEqual([]) + 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) - const parsed: ClipboardItems = { - nodes: [hostInfo], - groups: [], - reroutes: [], - links: [], - subgraphs: [pastedSubgraph] + let thrown: unknown + try { + canvas._deserializeItems( + { nodes: [createSerialisedNode(1, nodeType)] }, + {} + ) + } catch (error) { + thrown = error } - remapClipboardSubgraphNodeIds(parsed, rootGraph) + expect(thrown).toBeInstanceOf(AggregateError) + if (!(thrown instanceof AggregateError)) return + expect(thrown.cause).toBe(operationError) + expect(thrown.errors).toEqual([operationError, finalizationError]) + expect(rootGraph.nodes).toEqual([]) + }) - 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' - } + 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) + const changeEvents: Array<{ nodeCount: number; subType: string }> = [] + const emitEvent = canvas.emitEvent.bind(canvas) + vi.spyOn(canvas, 'emitEvent').mockImplementation((detail) => { + changeEvents.push({ + nodeCount: rootGraph.nodes.length, + subType: detail.subType + }) + emitEvent(detail) + }) + + expect(() => + canvas._deserializeItems( + { nodes: [createSerialisedNode(1, nodeType)] }, + {} + ) + ).toThrow(finalizationError) + expect(rootGraph.nodes).toEqual([]) + expect(changeEvents).toEqual([ + { nodeCount: 0, subType: 'before-change' }, + { 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([]) + }) + + 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 { + 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 09b64573d04..11f7183382e 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' @@ -23,6 +23,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, @@ -49,15 +50,14 @@ 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 type { SerializedNodeId } from '@/types/nodeId' +import { parseNodeId, serializeNodeId } from '@/types/nodeId' +import type { NodeId, SerializedNodeId } from '@/types/nodeId' import { LLink, slotFloatingLinks } from './LLink' import { inputHasLink, @@ -175,6 +175,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' @@ -296,6 +297,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 +} + /** Options for {@link LGraphCanvas.pasteFromClipboard}. */ interface IPasteFromClipboardOptions { /** If `true`, always attempt to connect inputs of pasted nodes - including to nodes that were not pasted. */ @@ -4167,7 +4182,7 @@ export class LGraphCanvas implements CustomEventDispatcher } _deserializeItems( - parsed: ClipboardItems, + clipboardItems: ClipboardItems, options: IPasteFromClipboardOptions ): ClipboardPasteResult | undefined { const { connectInputs = false, position = this.graph_mouse } = options @@ -4184,229 +4199,111 @@ 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) - } + let result: ClipboardPasteResult | undefined + let operationFailed = false + let operationError: unknown + try { + const items = initializeClipboardItems(clipboardItems) + const [offsetX, offsetY] = clipboardOffset(items) + result = { + created: [], + links: new Map(), + nodes: new Map(), + reroutes: new Map(), + subgraphs: new Map() + } + const context: ClipboardPasteContext = { + connectInputs, + dx: position[0] - offsetX, + dy: position[1] - offsetY, + graph, + items, + result, + targetSlotByLink: new Map() + } + + remapClipboardSubgraphIds(items) + createClipboardSubgraphs(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 info of items.nodes) { + const node = LiteGraph.createNode(info.type) + if (!node) continue - if (node instanceof SubgraphNode) { - if ( - node.properties.proxyWidgets !== undefined && - LiteGraph.LGraph.proxyWidgetMigrationFlush - ) { - LiteGraph.LGraph.proxyWidgetMigrationFlush(node, info) + result.nodes.set(serializeNodeId(info.id), node) + info.id = -1 + const linkByInputName = detachSerialisedLinks(info) + node.pos = [info.pos[0] + context.dx, info.pos[1] + context.dy] + try { + graph.add(node) + result.created.push(node) + } catch (error) { + if (graph.nodes.includes(node)) result.created.push(node) + throw error } - 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 + node.configure(info) - 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 + recordClipboardTargetSlots( + info, + linkByInputName, + context.targetSlotByLink ) - 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) - } + configurePastedSubgraphNode(node, info) } - } - - // 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) + createClipboardReroutes(context) + for (const info of items.links) createClipboardLink(context, info) + removeUnusedClipboardReroutes(context) + positionClipboardItems(context) + updateClipboardNodeLayout(context) - const index = created.indexOf(reroute) - if (index !== -1) created.splice(index, 1) - } + this.selectItems(result.created) + forEachNode(graph, (n) => n.onGraphConfigured?.()) + forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) + } catch (error) { + operationFailed = true + operationError = error + if (result) rollbackClipboardPaste(graph, result) } - // 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) + const closingErrors: unknown[] = [] + try { + graph.afterChange() + } catch (error) { + closingErrors.push(error) + if (!operationFailed && result) rollbackClipboardPaste(graph, result) } - // 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()) + try { + this.emitAfterChange() + } catch (error) { + closingErrors.push(error) } - this.selectItems(created) - forEachNode(graph, (n) => n.onGraphConfigured?.()) - forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) - - graph.afterChange() - this.emitAfterChange() - - return results + if (operationFailed && closingErrors.length) { + const combinedError = new AggregateError( + [operationError, ...closingErrors], + 'Clipboard paste and change finalization both failed' + ) + combinedError.cause = operationError + throw combinedError + } + if (operationFailed) 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 { - this.emitBeforeChange() - try { - this._pasteFromClipboard(options) - } finally { - this.emitAfterChange() - } + this._pasteFromClipboard(options) } processNodeDblClicked(n: LGraphNode): void { @@ -6453,7 +6350,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 } @@ -6980,7 +6877,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 @@ -7173,7 +7071,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 @@ -9046,157 +8945,245 @@ 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 = cloneDeep(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 - - const numericId = Number(nodeId) - if (!Number.isSafeInteger(numericId)) return undefined +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, { force: true }) + } else if (item instanceof Reroute) { + graph.removeReroute(item.id) + } + } catch (error) { + reportError(error, { + errorType: 'failure_rolling_back_clipboard_item' + }) + } + } - return remappedIds.get(numericId) + const registeredSubgraphs = [...result.subgraphs.values()].filter( + (subgraph) => graph.rootGraph.subgraphs.get(subgraph.id) === subgraph + ) + try { + graph.releaseSubgraphs(registeredSubgraphs) + } catch (error) { + reportError(error, { + errorType: 'failure_rolling_back_clipboard_subgraphs' + }) + } } -function remapProxyWidgets( - info: ISerialisedNode, - remappedIds: Map | undefined -) { - if (!remappedIds || remappedIds.size === 0) return - - const proxyWidgets = info.properties?.proxyWidgets - if (!Array.isArray(proxyWidgets)) return - - for (const entry of proxyWidgets) { - if (!Array.isArray(entry)) continue - - const [nodeId] = entry - if (typeof nodeId !== 'string' || nodeId === '-1') continue +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 + } - const remappedNodeId = remapNodeId(nodeId, remappedIds) - if (remappedNodeId !== undefined) entry[0] = String(remappedNodeId) + 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 hasStringSourceNodeId( - value: unknown -): value is { sourceNodeId: string } { - return ( - typeof value === 'object' && - value !== null && - 'sourceNodeId' in value && - typeof value.sourceNodeId === 'string' - ) +function createClipboardSubgraphs(context: ClipboardPasteContext): void { + const { graph, items, result } = context + for (const subgraph of graph.createSubgraphs(items.subgraphs, { + nodeIds: items.nodes + .map((node) => parseNodeId(node.id)) + .filter((id): id is NodeId => id !== null) + })) { + result.subgraphs.set(subgraph.id, subgraph) + } } -function remapPreviewExposures( +function recordClipboardTargetSlots( info: ISerialisedNode, - remappedIds: Map | undefined -) { - if (!remappedIds || remappedIds.size === 0) return + 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) + } + } +} - const previewExposures = info.properties?.previewExposures - if (!Array.isArray(previewExposures)) return +function configurePastedSubgraphNode( + node: LGraphNode, + info: ISerialisedNode +): void { + if (!(node instanceof SubgraphNode)) return - for (const entry of previewExposures) { - if (!hasStringSourceNodeId(entry) || entry.sourceNodeId === '-1') continue + const flushProxyWidgets = LiteGraph.LGraph.proxyWidgetMigrationFlush + if (node.properties.proxyWidgets !== undefined && flushProxyWidgets) { + flushProxyWidgets(node, info) + } + LiteGraph.LGraph.autoExposePreviewNodes?.(node) +} - const remappedNodeId = remapNodeId(entry.sourceNodeId, remappedIds) - if (remappedNodeId !== undefined) - entry.sourceNodeId = String(remappedNodeId) +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) + } + + 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 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 - function nextUniqueNodeId() { - let nextId = Number(mintNodeId(rootGraph.state)) - while (usedNodeIds.has(nextId)) { - nextId = Number(mintNodeId(rootGraph.state)) + 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 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 f1471f22f31..3ef1df9437d 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -25,7 +25,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' @@ -3043,7 +3043,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 @@ -3205,7 +3205,7 @@ export class LGraphNode const maybeCommonType = input.type && output.type && commonType(input.type, output.type) - const linkId = mintLinkId(graph.state) + const linkId = mintLinkId(graph.state, linkIdReservations(graph.rootGraph)) const link = new LLink( linkId, diff --git a/src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts b/src/lib/litegraph/src/__fixtures__/nodeIdsFromReservedMintRange.ts similarity index 86% rename from src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts rename to src/lib/litegraph/src/__fixtures__/nodeIdsFromReservedMintRange.ts index b099bbf4abe..72c18d07480 100644 --- a/src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts +++ b/src/lib/litegraph/src/__fixtures__/nodeIdsFromReservedMintRange.ts @@ -4,21 +4,14 @@ 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 = { - id: 'cccccccc-cccc-4ccc-8ccc-cccccccccccc', +// Synthetic reproducer around two observed high-range ID values. +export const nodeIdsFromReservedMintRange = { + id: 'dddddddd-dddd-4ddd-8ddd-dddddddddddd', version: 1, revision: 0, state: { - lastNodeId: 99_999_999, - lastLinkId: 10, + lastNodeId: 4_462_758_126_524_329, + lastLinkId: 0, lastGroupId: 0, lastRerouteId: 0 }, @@ -44,13 +37,23 @@ export const nodeIdSpaceExhausted = { properties: { proxyWidgets: [['8', 'prompt']] } }, { - id: 100_000_000, + id: 4_462_758_126_524_329, type: 'dummy', pos: [600, 0], size: [100, 50], flags: {}, order: 2, mode: 0 + }, + // Reserve the successor so remapping exercises collision recovery. + { + id: 4_462_758_126_524_330, + type: 'dummy', + pos: [900, 0], + size: [100, 50], + flags: {}, + order: 3, + mode: 0 } ], definitions: { @@ -103,7 +106,7 @@ export const nodeIdSpaceExhausted = { ], links: [ { - id: 1, + id: 7_729_209_487_955_825, origin_id: 3, origin_slot: 0, target_id: 8, @@ -161,7 +164,7 @@ export const nodeIdSpaceExhausted = { ], links: [ { - id: 2, + id: 7_729_209_487_955_825, origin_id: 3, origin_slot: 0, target_id: 37, 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 39cb3d13db8..cc767cfc158 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..c268007707d 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, it } from 'vitest' +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' /** @@ -53,10 +59,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 +103,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 +121,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 +148,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( @@ -154,14 +162,83 @@ 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', () => { + 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) + }) +}) + +describe('sequential minting survives a counter raised into the reserved mint range', () => { + 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 the final safe $name id', ({ mint }) => { + expect(mint(Number.MAX_SAFE_INTEGER - 1, new Set())).toBe( + Number.MAX_SAFE_INTEGER + ) + }) + + it('mints a node ID above any reserved-range high-water mark', () => { + const mint = minters[0].mint 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')) - ) + fc.property(reservedRangeHighWaterMark(), (lastId) => { + expect(mint(lastId, new Set())).toBe(lastId + 1) + }) + ) + }) - return new Set(ids).size === ids.length + 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) }) ) }) diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 90418f976c3..c267a3a15e4 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -1,8 +1,9 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } 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,188 @@ 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('keeps searching past the reported set size', () => { + const occupiedIds = new Set([1]) + vi.spyOn(occupiedIds, 'has') + .mockReturnValueOnce(true) + .mockReturnValueOnce(true) + .mockReturnValue(false) + + expect(findNextAvailableId(occupiedIds, 1)).toBe(3) + }) + + it('skips a reserved ID above a stale counter', () => { + const state = createLGraphState() + state.lastGroupId = 1 + + expect(mintGroupId(state, new Set([2]))).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('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', + 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', () => { + 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 ff2376cfdac..c1c4cbe122e 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,6 +21,13 @@ export interface LGraphState { lastRerouteId: RerouteId } +export interface ReservedIdIndex { + has(id: number): boolean + collect(): ReadonlySet +} + +type ReservedIds = ReadonlySet | ReservedIdIndex + export function createLGraphState(): LGraphState { return { lastGroupId: 0, @@ -24,8 +37,104 @@ 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 +): number { + let candidate = Number.isSafeInteger(startAt) && startAt > 0 ? startAt : 1 + while (usedIds.has(candidate)) { + candidate = candidate === Number.MAX_SAFE_INTEGER ? 1 : candidate + 1 + } + return candidate +} + +function mintSequentialId(lastId: number, reservedIds: ReservedIds): number { + if (Number.isSafeInteger(lastId) && lastId >= 0) { + const nextId = lastId + 1 + if (Number.isSafeInteger(nextId) && !reservedIds.has(nextId)) return nextId + } + const usedIds = 'collect' in reservedIds ? reservedIds.collect() : 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}. @@ -52,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)) } @@ -131,42 +245,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(reservedIds) + const id = mintSequentialId(state.lastNodeId, reservedIds) + 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) + 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) + 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) + 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..2c081f03e45 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' @@ -87,7 +87,10 @@ export class SubgraphInput extends SubgraphSlot { subgraph.beforeChange() } - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId( + subgraph.state, + linkIdReservations(subgraph.rootGraph) + ) const link = new LLink( linkId, @@ -105,44 +108,41 @@ 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 + try { + if (existingLink) { + this.parent._disconnectNodeInput(node, slot, existingLink) + if (subgraph.getLink(link.id) !== link) return } - } - - 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 - } - 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..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' @@ -108,7 +108,10 @@ export class SubgraphInputNode if (outputIndex === -1 || inputIndex === -1) throw new Error('Invalid slot indices.') - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId( + subgraph.state, + linkIdReservations(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..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 { @@ -58,7 +58,10 @@ export class SubgraphOutput extends SubgraphSlot { subgraph.beforeChange() } - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId( + subgraph.state, + linkIdReservations(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..92ae84681d9 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts @@ -2,9 +2,10 @@ 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' import { toRerouteId } from '@/types/rerouteId' import { isUuidShapedSubgraphId } from '@/schemas/subgraphIdSchema' @@ -12,6 +13,7 @@ import type { LGraphState } from '../LGraph' import type { ExportedSubgraph, ISerialisedGroup, + ISerialisedNode, SerialisableLLink, SerialisableReroute } from '../types/serialisation' @@ -26,8 +28,12 @@ import { topologicalSortSubgraphs } from './subgraphDeduplication' -type NestedExportedSubgraph = ExportedSubgraph & { +type NestedExportedSubgraph = Omit< + ExportedSubgraph, + 'definitions' | 'nodes' +> & { definitions?: { subgraphs?: NestedExportedSubgraph[] } + nodes: ISerialisedNode[] } function makeSubgraph( @@ -76,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', () => { @@ -104,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. @@ -131,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', () => { @@ -185,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) }) }) @@ -266,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)] @@ -322,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], @@ -340,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', () => { @@ -363,10 +372,98 @@ 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) }) + + 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 + ) + + assert.exists(result.subgraphs[0].nodes) + 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 + ) + + 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( + [{ ...makeSubgraph('next'), links: [chainedLink(1)] }], + { + nodeIds: new Set(), + groupIds: new Set(), + linkIds: new Set([1, 2]), + rerouteIds: new Set() + }, + state + ) + 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] + 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] + ) + assert.exists(result.subgraphs[0].nodes) + assert.exists(result.rootNodes) + 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..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 { @@ -20,17 +19,15 @@ 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, ExposedWidget, - ISerialisedGroup, ISerialisedNode, SerialisableLLink } from '../types/serialisation' -const MAX_ID = 100_000_000 - interface DeduplicationResult< Subgraph extends { id: string; nodes?: { type: string }[] } = ExportedSubgraph, @@ -227,39 +224,64 @@ 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) { - patchProxyWidgets(subgraph.nodes ?? [], subgraphIdSet, remapBySubgraph) + patchSubgraphNodeReferences( + subgraph.nodes ?? [], + subgraphIdSet, + remapBySubgraph + ) } - if (clonedRootNodes) { - patchProxyWidgets(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 } /** @@ -282,9 +304,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) @@ -313,23 +333,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[], @@ -355,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, @@ -392,7 +370,7 @@ export function deduplicateSubgraphLinkIds( const remapped = remapNumericIds( [...(subgraph.links ?? []), ...(subgraph.floatingLinks ?? [])], usedLinkIds, - () => mintLinkId(state), + () => mintLinkId(state, usedLinkIds), (id) => observeLinkId(state, toLinkId(id)), 'link' ) @@ -417,23 +395,13 @@ export function deduplicateSubgraphGroupIds( remapNumericIds( subgraph.groups ?? [], usedGroupIds, - () => mintGroupId(state), + () => mintGroupId(state, usedGroupIds), (id) => observeGroupId(state, toGroupId(id)), 'group' ) } } -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 @@ -470,7 +438,7 @@ function remapRerouteIds( return remapNumericIds( subgraph.reroutes ?? [], usedRerouteIds, - () => mintRerouteId(state), + () => mintRerouteId(state, usedRerouteIds), (id) => observeRerouteId(state, toRerouteId(id)), 'reroute' ) @@ -488,7 +456,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 +549,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 +559,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..6d31c863f4a 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,83 @@ describe('parseComfyWorkflow', () => { }) }) }) + +describe('zClipboardItems', () => { + it('normalizes array-like widget values and preserves array slot types', () => { + 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] }) + + 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('preserves indices when normalizing sparse widget values', () => { + const node = { + ...structuredClone(defaultGraph.nodes[0]), + 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 + 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') + }) + + 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( + accepted + ) + } + ) + + 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 7a59bf5f3f6..8ff57e079e0 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.ts @@ -2,8 +2,16 @@ 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 { 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' +import type { NodeProperty } from '@/types/nodeState' +import { toRerouteId } from '@/types/rerouteId' const zRendererType = z.enum([ 'LG', @@ -210,6 +218,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,9 +235,56 @@ const zProperties = z ver: zVersion.optional(), models: z.array(zModelFile).optional() }) - .passthrough() + .catchall(zNodeProperty.optional()) + +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( + values: z.output | undefined +): boolean { + if (!values || Array.isArray(values)) return true + return ( + typeof values.length !== 'number' || + values.length <= MAX_CLIPBOARD_WIDGET_VALUES + ) +} -const zWidgetValues = z.union([z.array(z.any()), z.record(z.any())]) +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 (!isArrayLikeLength(length)) return [] + 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 +} + +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) } +} export const zComfyNode = z .object({ @@ -511,6 +575,107 @@ const zSubgraphDefinition = zComfyWorkflow1 }) .passthrough() +const zClipboardNode = zComfyNode + .refine( + (node) => hasSupportedClipboardWidgetValuesLength(node.widgets_values), + { + path: ['widgets_values'], + message: 'Clipboard widget values length is too large' + } + ) + .transform(normalizeClipboardNodeWidgets) + +const zClipboardSubgraphInstance = zSubgraphInstance + .refine( + (node) => hasSupportedClipboardWidgetValuesLength(node.widgets_values), + { + path: ['widgets_values'], + message: 'Clipboard widget values length is too large' + } + ) + .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, + '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', () => { From 0212876782d52234c59d2c26f221416f22c2a635 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sun, 4 Oct 2026 15:11:14 -0700 Subject: [PATCH 2/5] fix(backport): restore explicit Vitest test import --- src/extensions/core/groupNode.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/core/groupNode.test.ts b/src/extensions/core/groupNode.test.ts index 173fc3f75f0..f3b46d11afa 100644 --- a/src/extensions/core/groupNode.test.ts +++ b/src/extensions/core/groupNode.test.ts @@ -1,5 +1,5 @@ import { fromAny, fromPartial } from '@total-typescript/shoehorn' -import { describe, expect, it, vi } from 'vitest' +import { describe, expect, it, test, vi } from 'vitest' import { t } from '@/i18n' From d604716c418efd01b3adfcd44e5d4e8d23fb6843 Mon Sep 17 00:00:00 2001 From: bymyself Date: Sun, 4 Oct 2026 22:34:18 +0000 Subject: [PATCH 3/5] fix: validate clipboard widget record lengths Addresses https://github.com/Comfy-Org/ComfyUI_frontend/pull/20132#discussion_r4179516547 --- .../validation/schemas/workflowSchema.test.ts | 19 +++++++++++++++++++ .../validation/schemas/workflowSchema.ts | 15 +++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.test.ts b/src/platform/workflow/validation/schemas/workflowSchema.test.ts index 6d31c863f4a..affbfb98c27 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.test.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.test.ts @@ -429,6 +429,8 @@ describe('zClipboardItems', () => { }) test.for([ + { length: -1, accepted: false }, + { length: 2.5, accepted: false }, { length: 10_000, accepted: true }, { length: 10_001, accepted: false } ])( @@ -462,6 +464,23 @@ describe('zClipboardItems', () => { }) }) + it('preserves a named widget called length as a named value', () => { + const node = { + ...structuredClone(defaultGraph.nodes[0]), + widgets_values: { seed: 42, 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).toBeUndefined() + expect(result.data.nodes?.[0].widgets_values_named).toEqual({ + seed: 42, + length: 3 + }) + }) + it('defaults omitted legacy group and reroute fields', () => { const result = zClipboardItems.safeParse({ groups: [{ title: 'Legacy group', bounding: [0, 0, 100, 100] }], diff --git a/src/platform/workflow/validation/schemas/workflowSchema.ts b/src/platform/workflow/validation/schemas/workflowSchema.ts index 8ff57e079e0..631426cc4d1 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.ts @@ -248,7 +248,9 @@ function hasSupportedClipboardWidgetValuesLength( if (!values || Array.isArray(values)) return true return ( typeof values.length !== 'number' || - values.length <= MAX_CLIPBOARD_WIDGET_VALUES + (Number.isSafeInteger(values.length) && + values.length >= 0 && + values.length <= MAX_CLIPBOARD_WIDGET_VALUES) ) } @@ -272,11 +274,20 @@ function normalizeClipboardWidgetValues( return normalized } +function isArrayLikeWidgetValues( + values: Record +): boolean { + return ( + isArrayLikeLength(values.length) && + Object.keys(values).some((key) => Number.isInteger(Number(key))) + ) +} + function normalizeClipboardNodeWidgets< T extends { widgets_values?: z.output } >(node: T) { const values = node.widgets_values - if (values && !Array.isArray(values) && typeof values.length !== 'number') { + if (values && !Array.isArray(values) && !isArrayLikeWidgetValues(values)) { return { ...node, widgets_values: undefined, From 0b35b373aeb4a1bbe7cf7cb431b57bebbd789d6c Mon Sep 17 00:00:00 2001 From: bymyself Date: Sun, 4 Oct 2026 23:20:18 +0000 Subject: [PATCH 4/5] fix(backport): restore upstream clipboard schema Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/20132#discussion_r4179646216 --- .../workflow/validation/schemas/workflowSchema.ts | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.ts b/src/platform/workflow/validation/schemas/workflowSchema.ts index 631426cc4d1..8ff57e079e0 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.ts @@ -248,9 +248,7 @@ function hasSupportedClipboardWidgetValuesLength( if (!values || Array.isArray(values)) return true return ( typeof values.length !== 'number' || - (Number.isSafeInteger(values.length) && - values.length >= 0 && - values.length <= MAX_CLIPBOARD_WIDGET_VALUES) + values.length <= MAX_CLIPBOARD_WIDGET_VALUES ) } @@ -274,20 +272,11 @@ function normalizeClipboardWidgetValues( return normalized } -function isArrayLikeWidgetValues( - values: Record -): boolean { - return ( - isArrayLikeLength(values.length) && - Object.keys(values).some((key) => Number.isInteger(Number(key))) - ) -} - function normalizeClipboardNodeWidgets< T extends { widgets_values?: z.output } >(node: T) { const values = node.widgets_values - if (values && !Array.isArray(values) && !isArrayLikeWidgetValues(values)) { + if (values && !Array.isArray(values) && typeof values.length !== 'number') { return { ...node, widgets_values: undefined, From edb790b6ca640f6f4e80949caa712f7698465315 Mon Sep 17 00:00:00 2001 From: bymyself Date: Sun, 4 Oct 2026 23:20:18 +0000 Subject: [PATCH 5/5] test(backport): restore upstream clipboard cases Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/20132#discussion_r4179646217 --- .../validation/schemas/workflowSchema.test.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.test.ts b/src/platform/workflow/validation/schemas/workflowSchema.test.ts index affbfb98c27..6d31c863f4a 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.test.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.test.ts @@ -429,8 +429,6 @@ describe('zClipboardItems', () => { }) test.for([ - { length: -1, accepted: false }, - { length: 2.5, accepted: false }, { length: 10_000, accepted: true }, { length: 10_001, accepted: false } ])( @@ -464,23 +462,6 @@ describe('zClipboardItems', () => { }) }) - it('preserves a named widget called length as a named value', () => { - const node = { - ...structuredClone(defaultGraph.nodes[0]), - widgets_values: { seed: 42, 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).toBeUndefined() - expect(result.data.nodes?.[0].widgets_values_named).toEqual({ - seed: 42, - length: 3 - }) - }) - it('defaults omitted legacy group and reroute fields', () => { const result = zClipboardItems.safeParse({ groups: [{ title: 'Legacy group', bounding: [0, 0, 100, 100] }],