From e6846559f5ca1fbf9c8a4046b481a2c2334b3bc4 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 23 Sep 2026 21:45:22 +0000 Subject: [PATCH 01/11] fix(litegraph): recycle freed node/link/group/reroute IDs `findNextAvailableId` only ever advanced each counter and threw once it passed the 100M ceiling, even though most of that ceiling was IDs whose owning node/link/group/reroute had long since been deleted. Track freed IDs in a small per-counter pool, released on removal, and prefer reusing one before minting past the counter. The ceiling itself stays a true safety cap. Also: - Clamp incoming counter values on hydration and observation so a corrupted/stale persisted counter, or an ID far outside the normal range, can't push a graph's allocator toward the ceiling on its own. - Add the missing exhaustion guard to the clipboard subgraph node-ID remap, which previously had no ceiling check and would spin instead of failing clearly. - Stage ID-state changes on a disposable copy in both the subgraph dedup path and the clipboard remap path, committing only once the whole operation succeeds, so a throw partway through never leaves a graph's real allocator partially advanced. --- .../LGraph.linkPresentationTransfer.test.ts | 8 +- src/lib/litegraph/src/LGraph.test.ts | 79 +++++++++ src/lib/litegraph/src/LGraph.ts | 28 ++-- .../src/LGraphCanvas.clipboard.test.ts | 58 +++++++ .../LGraphCanvas.subgraphPasteDelete.test.ts | 16 +- src/lib/litegraph/src/LGraphCanvas.ts | 30 ++-- .../canvas/LinkConnector.integration.test.ts | 6 +- src/lib/litegraph/src/idAllocation.test.ts | 155 +++++++++++++++++- src/lib/litegraph/src/idAllocation.ts | 128 +++++++++++++-- .../subgraph/subgraphDeduplication.test.ts | 58 ++++++- .../src/subgraph/subgraphDeduplication.ts | 31 +++- 11 files changed, 547 insertions(+), 50 deletions(-) diff --git a/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts b/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts index 445deb07005..c3311075bdc 100644 --- a/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts +++ b/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts @@ -507,6 +507,7 @@ describe('link presentation transfer across recreation flows', () => { connector.moveInputLink(graph, oldTarget, oldTarget.inputs[0]) connector.dropOnReroute(reroute, createMockCanvasPointerEvent(100, 100)) + const retargetedIds = new Set() for (const target of [first, second]) { const link = target.getInputLink(0) if (!link) throw new Error('Expected retargeted link') @@ -515,8 +516,13 @@ describe('link presentation transfer across recreation flows', () => { hidden: true, label: 'Fan out' }) + retargetedIds.add(Number(link.id)) + } + // `original.id` may now be a recycled ID reused by one of the + // retargeted links above, which legitimately carries the presentation. + if (!retargetedIds.has(Number(original.id))) { + expect(store.getPresentation(scope, original.id)).toBeUndefined() } - expect(store.getPresentation(scope, original.id)).toBeUndefined() }) it('preserves each ordinary reroute presentation when moving its source', () => { diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index e4e31f5e846..702005b278d 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -1824,6 +1824,85 @@ describe('Shared LGraphState', () => { }) }) +describe('ID recycling on removal', () => { + it('reuses a removed node id for the next node minted', () => { + const graph = new LGraph() + const first = new DummyNode() + graph.add(first) + const removedId = first.id + + graph.remove(first) + + const second = new DummyNode() + graph.add(second) + + expect(second.id).toBe(removedId) + }) + + it('reuses a removed group id for the next group minted', () => { + const graph = new LGraph() + const first = new LGraphGroup('first') + graph.add(first) + const removedId = first.id + + graph.remove(first) + + const second = new LGraphGroup('second') + graph.add(second) + + expect(second.id).toBe(removedId) + }) + + it('reuses a removed link id for the next link minted', () => { + const graph = new LGraph() + const source = new LGraphNode('source') + const target = new LGraphNode('target') + source.addOutput('out', '*') + target.addInput('in', '*') + target.addInput('in2', '*') + graph.add(source) + graph.add(target) + + const firstLink = source.connect(0, target, 0)! + const removedId = firstLink.id + graph.removeLink(firstLink.id) + + const secondLink = source.connect(0, target, 1)! + + expect(secondLink.id).toBe(removedId) + }) + + it('reuses a removed reroute id for the next reroute minted', () => { + const graph = new LGraph() + const first = graph.setReroute({ pos: [0, 0], linkIds: [] })! + const removedId = first.id + + graph.removeReroute(first.id) + + const second = graph.setReroute({ pos: [0, 0], linkIds: [] })! + + expect(second.id).toBe(removedId) + }) + + it('does not recycle a node id kept alive by a successor', () => { + const graph = new LGraph() + const node = new LGraphNode('test') + graph.add(node) + const originalId = node.id + const successor = new LGraphNode('test') + successor.id = node.id + graph._nodes.push(successor) + graph._nodes_by_id[node.id] = successor + + graph.remove(node, { preserveCanonicalState: true }) + + const nextNode = new DummyNode() + graph.add(nextNode) + + expect(nextNode.id).not.toBe(originalId) + }) +}) + describe('persisted duplicate links', () => { const onConnectionsChange = vi.fn>() diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 4a3fac5292d..2273650f7d5 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -53,7 +53,11 @@ import { observeGroupId, observeLinkId, observeNodeId, - observeRerouteId + observeRerouteId, + releaseGroupId, + releaseLinkId, + releaseNodeId, + releaseRerouteId } from './idAllocation' import type { LGraphState, NodeIdMintMode } from './idAllocation' import { isRootGraphDocBound } from './docBoundGraphs' @@ -64,6 +68,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, @@ -1470,6 +1475,7 @@ export class LGraph if (index != -1) { this._groups.splice(index, 1) } + releaseGroupId(this.state, node.id) detachGroupLayout(node) node.graph = undefined this.incrementVersion() @@ -1544,7 +1550,10 @@ export class LGraph // callback node.onRemoved?.() - if (!successor) clearNodeOwnedStoreState(node) + if (!successor) { + clearNodeOwnedStoreState(node) + releaseNodeId(this.state, node.id) + } const order = node.order if (!successor) { @@ -1873,6 +1882,7 @@ export class LGraph removeFloatingLink(link: LLink): void { if (this.floatingLinks.get(link.id) !== link) return unregisterLinkTopology(link) + releaseLinkId(this.state, link.id) const reroutes = LLink.getReroutes(this, link) for (const reroute of reroutes) { @@ -1904,6 +1914,7 @@ export class LGraph unregisterLinkTopology(link) layoutStore.deleteLinkLayout(linkId) this.getNodeById(link.target_id)?.updateComputedDisabled() + releaseLinkId(this.state, linkId) return true } @@ -1955,6 +1966,7 @@ export class LGraph this.reroutesInternal.delete(id) unregisterRerouteChain(reroute) detachRerouteLayout(reroute) + releaseRerouteId(this.state, id) } /** @@ -3115,17 +3127,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 diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index 5b978e7ffc1..64a513989e2 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -28,6 +28,7 @@ import { createUuidv4 } from '@/lib/litegraph/src/litegraph' import { remapClipboardSubgraphNodeIds } from '@/lib/litegraph/src/LGraphCanvas' +import { MAX_ID } from '@/lib/litegraph/src/idAllocation' import { toNodeId } from '@/types/nodeId' import type { ClipboardItems, @@ -198,6 +199,63 @@ describe('remapClipboardSubgraphNodeIds', () => { } ]) }) + + function createExhaustedRootGraph(): LGraph { + const rootGraph = new LGraph() + rootGraph.state.lastNodeId = MAX_ID + + const existingNode = new LGraphNode('existing') + existingNode.id = toNodeId(1) + rootGraph.add(existingNode) + return rootGraph + } + + function createCollidingClipboardItems(): ClipboardItems { + const pastedSubgraph: ExportedSubgraph = { + id: createUuidv4(), + 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: [] + } + return { + nodes: [], + groups: [], + reroutes: [], + links: [], + subgraphs: [pastedSubgraph] + } + } + + it('throws instead of hanging when the node ID space is exhausted', () => { + const rootGraph = createExhaustedRootGraph() + const parsed = createCollidingClipboardItems() + + expect(() => remapClipboardSubgraphNodeIds(parsed, rootGraph)).toThrow( + 'Node ID space exhausted' + ) + }) + + it('leaves the graph ID state untouched when remapping throws', () => { + const rootGraph = createExhaustedRootGraph() + const snapshotLastNodeId = rootGraph.state.lastNodeId + const snapshotFreeNodeIds = new Set(rootGraph.state.freeNodeIds) + const parsed = createCollidingClipboardItems() + + expect(() => remapClipboardSubgraphNodeIds(parsed, rootGraph)).toThrow() + + expect(rootGraph.state.lastNodeId).toBe(snapshotLastNodeId) + expect(rootGraph.state.freeNodeIds).toEqual(snapshotFreeNodeIds) + }) }) function createCanvas(graph: LGraph): LGraphCanvas { diff --git a/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts b/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts index b3eedc1f92b..7ccfc086cb6 100644 --- a/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts @@ -206,6 +206,18 @@ function terminalState(f: Fixture) { } } +/** Recycled floating-link IDs are history-dependent; only their count is order-independent. */ +function withFloatingLinkCounts(state: ReturnType) { + return { + ...state, + remainingFloatingLinks: state.remainingFloatingLinks.length, + remainingReroutes: state.remainingReroutes.map((reroute) => ({ + linkIds: reroute.linkIds, + floatingLinkIds: reroute.floatingLinkIds.length + })) + } +} + describe('subgraph copy/paste then delete in both orders', () => { beforeEach(() => { LiteGraph.registerNodeType(INTERIOR_TYPE, InteriorNode) @@ -359,7 +371,9 @@ describe('subgraph copy/paste then delete in both orders', () => { copyFirst.rootGraph.remove(copyFirst.copy) copyFirst.rootGraph.remove(copyFirst.original) - expect(terminalState(copyFirst)).toStrictEqual(terminalState(originalFirst)) + expect(withFloatingLinkCounts(terminalState(copyFirst))).toStrictEqual( + withFloatingLinkCounts(terminalState(originalFirst)) + ) // That shared state is: both definitions released, both instances gone, // every real link gone — and the reroute survives on a *real* floating diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 7c9feb93d1a..db23548e8ce 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -43,13 +43,19 @@ import type { ContextMenu } from './ContextMenu' import { createCursorCache } from './cursorCache' import { DragAndScale } from './DragAndScale' import type { AnimationOptions } from './DragAndScale' -import { mintNodeId, observeNodeId } from './idAllocation' +import { + cloneLGraphState, + commitLGraphState, + 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 { findNextAvailableId } from './subgraph/subgraphDeduplication' import { parseNodeId, serializeNodeId, toNodeId } from '@/types/nodeId' import type { SerializedNodeId } from '@/types/nodeId' import { LLink, slotFloatingLinks } from './LLink' @@ -9122,21 +9128,22 @@ export function remapClipboardSubgraphNodeIds( parsed: ClipboardItems, rootGraph: LGraph ): void { + // Mint/observe against a disposable copy of the graph's ID state so a + // throw (e.g. ID space exhaustion) leaves the real allocator untouched. + const workingState = cloneLGraphState(rootGraph.state) + 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)) + observeNodeId(workingState, toNodeId(numericId)) }) - function nextUniqueNodeId() { - let nextId = Number(mintNodeId(rootGraph.state)) - while (usedNodeIds.has(nextId)) { - nextId = Number(mintNodeId(rootGraph.state)) - } - usedNodeIds.add(nextId) - return nextId + function nextUniqueNodeId(): number { + return findNextAvailableId(usedNodeIds, () => + Number(mintNodeId(workingState)) + ) } const subgraphNodeIdMap = new Map< @@ -9153,13 +9160,14 @@ export function remapClipboardSubgraphNodeIds( if (usedNodeIds.has(nodeInfo.id)) { const oldId = nodeInfo.id const newId = nextUniqueNodeId() + usedNodeIds.add(newId) remappedIds.set(oldId, newId) nodeInfo.id = newId continue } usedNodeIds.add(nodeInfo.id) - observeNodeId(rootGraph.state, toNodeId(nodeInfo.id)) + observeNodeId(workingState, toNodeId(nodeInfo.id)) } if (remappedIds.size > 0) { @@ -9179,4 +9187,6 @@ export function remapClipboardSubgraphNodeIds( remapProxyWidgets(nodeInfo, remappedIds) remapPreviewExposures(nodeInfo, remappedIds) } + + commitLGraphState(rootGraph.state, workingState) } diff --git a/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts b/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts index 04ad0e22e42..422bd999222 100644 --- a/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts +++ b/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts @@ -221,8 +221,6 @@ describe('LinkConnector Integration', () => { describe('Moving input links', () => { test('Should move input links', ({ graph, connector }) => { - const nextLinkId = graph.last_link_id + 1 - const hasInputNode = graph.getNodeById(toNodeId(2))! const disconnectedNode = graph.getNodeById(toNodeId(9))! @@ -250,7 +248,9 @@ describe('LinkConnector Integration', () => { expect(connector.renderLinks.length).toBe(0) expect(connector.inputLinks.length).toBe(0) - expect(disconnectedNode.inputs[0].link).toBe(nextLinkId) + const newLinkId = disconnectedNode.inputs[0].link + expect(newLinkId).not.toBeNull() + expect(graph.links.get(newLinkId!)).toBeInstanceOf(LLink) expect(hasInputNode.inputs[0].link).toBeNull() const reroutesAfter = LLink.getReroutes( diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 90418f976c3..f2f50fd9364 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -2,6 +2,9 @@ import { describe, expect, it } from 'vitest' import { AGENT_RESERVED_BIT, + MAX_ID, + cloneLGraphState, + commitLGraphState, createLGraphState, isReservedBitRangeNodeId, matchesReservedBitConvention, @@ -12,7 +15,11 @@ import { observeGroupId, observeLinkId, observeNodeId, - observeRerouteId + observeRerouteId, + releaseGroupId, + releaseLinkId, + releaseNodeId, + releaseRerouteId } from '@/lib/litegraph/src/idAllocation' import { toGroupId } from '@/types/groupId' import { toLinkId } from '@/types/linkId' @@ -57,7 +64,11 @@ describe('idAllocation', () => { lastGroupId: 5, lastNodeId: 4, lastLinkId: 6, - lastRerouteId: 7 + lastRerouteId: 7, + freeNodeIds: new Set(), + freeGroupIds: new Set(), + freeLinkIds: new Set(), + freeRerouteIds: new Set() }) }) @@ -130,4 +141,144 @@ describe('idAllocation', () => { expect(matchesReservedBitConvention(id)).toBe(false) }) }) + + describe('id recycling', () => { + it.for([ + { + release: (state: ReturnType) => + releaseNodeId(state, toNodeId(2)), + mint: mintNodeId, + expected: ['1', '2', '3', '2', '4'], + name: 'node' + }, + { + release: (state: ReturnType) => + releaseGroupId(state, toGroupId(2)), + mint: mintGroupId, + expected: [1, 2, 3, 2, 4], + name: 'group' + }, + { + release: (state: ReturnType) => + releaseLinkId(state, toLinkId(2)), + mint: mintLinkId, + expected: [1, 2, 3, 2, 4], + name: 'link' + }, + { + release: (state: ReturnType) => + releaseRerouteId(state, toRerouteId(2)), + mint: mintRerouteId, + expected: [1, 2, 3, 2, 4], + name: 'reroute' + } + ])( + 'reuses a freed $name ID before minting past the counter', + ({ release, mint, expected }) => { + const state = createLGraphState() + expect([mint(state), mint(state), mint(state)]).toEqual( + expected.slice(0, 3) + ) + + release(state) + expect(mint(state)).toEqual(expected[3]) + expect(mint(state)).toEqual(expected[4]) + } + ) + + it('ignores releasing a nonnumeric legacy node ID', () => { + const state = createLGraphState() + releaseNodeId(state, toNodeId('named')) + expect(state.freeNodeIds.size).toBe(0) + }) + + it('never recycles an ID in the agent/CRDT reserved-bit range', () => { + const state = createLGraphState() + releaseNodeId(state, toNodeId(AGENT_RESERVED_BIT.toString())) + expect(state.freeNodeIds.size).toBe(0) + }) + + it('drops a freed ID from the pool once it is observed as in use again', () => { + const state = createLGraphState() + mintNodeId(state) + releaseNodeId(state, toNodeId(1)) + expect(state.freeNodeIds.has(1)).toBe(true) + + observeNodeId(state, toNodeId(1)) + expect(state.freeNodeIds.has(1)).toBe(false) + }) + }) + + describe('counter clamping', () => { + it.for([ + { + observe: (state: ReturnType) => + observeNodeId(state, toNodeId(MAX_ID + 1)), + read: (state: ReturnType) => state.lastNodeId, + name: 'node' + }, + { + observe: (state: ReturnType) => + observeGroupId(state, toGroupId(MAX_ID + 1)), + read: (state: ReturnType) => + state.lastGroupId, + name: 'group' + }, + { + observe: (state: ReturnType) => + observeLinkId(state, toLinkId(MAX_ID + 1)), + read: (state: ReturnType) => + Number(state.lastLinkId), + name: 'link' + }, + { + observe: (state: ReturnType) => + observeRerouteId(state, toRerouteId(MAX_ID + 1)), + read: (state: ReturnType) => + Number(state.lastRerouteId), + name: 'reroute' + } + ])( + 'clamps an absurd incoming $name counter to MAX_ID', + ({ observe, read }) => { + const state = createLGraphState() + observe(state) + expect(read(state)).toBe(MAX_ID) + } + ) + + it('rejects a negative counter candidate outright', () => { + const state = createLGraphState() + observeNodeId(state, toNodeId(-5)) + expect(state.lastNodeId).toBe(0) + }) + }) + + describe('cloneLGraphState / commitLGraphState', () => { + it('leaves the original untouched until explicitly committed', () => { + const state = createLGraphState() + const working = cloneLGraphState(state) + + mintNodeId(working) + mintNodeId(working) + + expect(state.lastNodeId).toBe(0) + expect(working.lastNodeId).toBe(2) + + commitLGraphState(state, working) + expect(state.lastNodeId).toBe(2) + }) + + it('clones free-id sets so mutating the copy cannot affect the original', () => { + const state = createLGraphState() + mintNodeId(state) + releaseNodeId(state, toNodeId(1)) + + const working = cloneLGraphState(state) + mintNodeId(working) + + expect(state.freeNodeIds.has(1)).toBe(true) + expect(working.freeNodeIds.has(1)).toBe(false) + }) + }) }) diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index 87241ad10a2..fdc2a377919 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -7,12 +7,20 @@ import type { NodeId } from '@/types/nodeId' import { toRerouteId } from '@/types/rerouteId' import type { RerouteId } from '@/types/rerouteId' +/** Hard ceiling shared by every counter — a true safety cap, not a working limit. */ +export const MAX_ID = 100_000_000 + export interface LGraphState { /** Counter, not an id — brand at the point a group is constructed. */ lastGroupId: number lastNodeId: number lastLinkId: LinkId lastRerouteId: RerouteId + /** IDs freed by a removed node/group/link/reroute, reused before minting past the counter. */ + freeNodeIds: Set + freeGroupIds: Set + freeLinkIds: Set + freeRerouteIds: Set } export function createLGraphState(): LGraphState { @@ -20,10 +28,61 @@ export function createLGraphState(): LGraphState { lastGroupId: 0, lastNodeId: 0, lastLinkId: toLinkId(0), - lastRerouteId: toRerouteId(0) + lastRerouteId: toRerouteId(0), + freeNodeIds: new Set(), + freeGroupIds: new Set(), + freeLinkIds: new Set(), + freeRerouteIds: new Set() } } +/** + * A disposable copy of `state`, safe for an operation to mutate speculatively + * (minting, observing, recycling) and discard on failure via + * {@link commitLGraphState} only once it fully succeeds. + */ +export function cloneLGraphState(state: LGraphState): LGraphState { + return { + lastGroupId: state.lastGroupId, + lastNodeId: state.lastNodeId, + lastLinkId: state.lastLinkId, + lastRerouteId: state.lastRerouteId, + freeNodeIds: new Set(state.freeNodeIds), + freeGroupIds: new Set(state.freeGroupIds), + freeLinkIds: new Set(state.freeLinkIds), + freeRerouteIds: new Set(state.freeRerouteIds) + } +} + +/** Copies every counter and free-id pool from `source` onto `target`, in place. */ +export function commitLGraphState( + target: LGraphState, + source: LGraphState +): void { + target.lastGroupId = source.lastGroupId + target.lastNodeId = source.lastNodeId + target.lastLinkId = source.lastLinkId + target.lastRerouteId = source.lastRerouteId + target.freeNodeIds = source.freeNodeIds + target.freeGroupIds = source.freeGroupIds + target.freeLinkIds = source.freeLinkIds + target.freeRerouteIds = source.freeRerouteIds +} + +/** The next value from `freeIds`, removed from the set, or `undefined` when empty. */ +function takeFreeId(freeIds: Set): number | undefined { + const { done, value } = freeIds.values().next() + if (done) return undefined + freeIds.delete(value) + return value +} + +/** Clamps a candidate counter value to the valid range, or `undefined` to reject it outright. */ +function sanitizeCounterCandidate(value: number): number | undefined { + if (!Number.isInteger(value) || value < 0) return undefined + return Math.min(value, MAX_ID) +} + /** * `'sequential'` (default) is the plain-local `++lastNodeId` counter. * `'crdt-disjoint'` is for a graph bound to the in-app agent's collaborative @@ -134,40 +193,89 @@ export function mintNodeId( state: LGraphState, mode: NodeIdMintMode = 'sequential' ): NodeId { - return mode === 'crdt-disjoint' - ? mintCrdtDisjointNodeId() - : toNodeId(++state.lastNodeId) + if (mode === 'crdt-disjoint') return mintCrdtDisjointNodeId() + const recycled = takeFreeId(state.freeNodeIds) + return toNodeId(recycled ?? ++state.lastNodeId) } export function mintGroupId(state: LGraphState): GroupId { - return toGroupId(++state.lastGroupId) + const recycled = takeFreeId(state.freeGroupIds) + return toGroupId(recycled ?? ++state.lastGroupId) } export function mintLinkId(state: LGraphState): LinkId { + const recycled = takeFreeId(state.freeLinkIds) + if (recycled !== undefined) return toLinkId(recycled) state.lastLinkId = toLinkId(Number(state.lastLinkId) + 1) return state.lastLinkId } export function mintRerouteId(state: LGraphState): RerouteId { + const recycled = takeFreeId(state.freeRerouteIds) + if (recycled !== undefined) return toRerouteId(recycled) state.lastRerouteId = toRerouteId(Number(state.lastRerouteId) + 1) return state.lastRerouteId } +/** + * Returns `id` to `state`'s free-node pool so a later mint reuses it before + * advancing `lastNodeId`. Ignored for a nonnumeric legacy id and for one in + * the agent/CRDT reserved-bit range (see {@link AGENT_RESERVED_BIT}), which + * this counter never mints into. + */ +export function releaseNodeId(state: LGraphState, id: NodeId): void { + const numeric = safeIntegerValueOf(id) + if (numeric === undefined || numeric <= 0) return + if (BigInt(numeric) >= AGENT_RESERVED_BIT) return + state.freeNodeIds.add(numeric) +} + +export function releaseGroupId(state: LGraphState, id: GroupId): void { + if (!Number.isInteger(id) || id <= 0) return + state.freeGroupIds.add(id) +} + +export function releaseLinkId(state: LGraphState, id: LinkId): void { + const numeric = Number(id) + if (!Number.isInteger(numeric) || numeric <= 0) return + state.freeLinkIds.add(numeric) +} + +export function releaseRerouteId(state: LGraphState, id: RerouteId): void { + const numeric = Number(id) + if (!Number.isInteger(numeric) || numeric <= 0) return + state.freeRerouteIds.add(numeric) +} + export function observeNodeId(state: LGraphState, id: NodeId): void { const numericId = Number(id) - if (Number.isInteger(numericId) && numericId > state.lastNodeId) { - state.lastNodeId = numericId + state.freeNodeIds.delete(numericId) + const sanitized = sanitizeCounterCandidate(numericId) + if (sanitized !== undefined && sanitized > state.lastNodeId) { + state.lastNodeId = sanitized } } export function observeGroupId(state: LGraphState, id: GroupId): void { - if (id > state.lastGroupId) state.lastGroupId = id + state.freeGroupIds.delete(id) + const sanitized = sanitizeCounterCandidate(id) + if (sanitized !== undefined && sanitized > state.lastGroupId) { + state.lastGroupId = sanitized + } } export function observeLinkId(state: LGraphState, id: LinkId): void { - if (id > state.lastLinkId) state.lastLinkId = id + state.freeLinkIds.delete(Number(id)) + const sanitized = sanitizeCounterCandidate(Number(id)) + if (sanitized !== undefined && sanitized > Number(state.lastLinkId)) { + state.lastLinkId = toLinkId(sanitized) + } } export function observeRerouteId(state: LGraphState, id: RerouteId): void { - if (id > state.lastRerouteId) state.lastRerouteId = id + state.freeRerouteIds.delete(Number(id)) + const sanitized = sanitizeCounterCandidate(Number(id)) + if (sanitized !== undefined && sanitized > Number(state.lastRerouteId)) { + state.lastRerouteId = toRerouteId(sanitized) + } } diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts index 77c09f7027d..6aebf5f8b4f 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts @@ -5,10 +5,12 @@ import { import { describe, expect, it } from 'vitest' import { toLinkId } from '@/types/linkId' +import { toNodeId } from '@/types/nodeId' import { toRerouteId } from '@/types/rerouteId' import { isUuidShapedSubgraphId } from '@/schemas/subgraphIdSchema' import type { LGraphState } from '../LGraph' +import { MAX_ID, createLGraphState } from '../idAllocation' import type { ExportedSubgraph, ISerialisedGroup, @@ -246,12 +248,9 @@ function chainedLink(id: number, parentId?: number): SerialisableLLink { } function freshState(lastRerouteId = 0): LGraphState { - return { - lastGroupId: 0, - lastNodeId: 0, - lastLinkId: toLinkId(0), - lastRerouteId: toRerouteId(lastRerouteId) - } + const state = createLGraphState() + state.lastRerouteId = toRerouteId(lastRerouteId) + return state } function group(id: number): ISerialisedGroup { @@ -367,6 +366,53 @@ describe('normalizeSubgraphDefinitions', () => { expect(result.inputs![0].linkIds).toEqual([toLinkId(1)]) expect(subgraph.floatingLinks).toHaveLength(1) }) + + it('reuses a freed node ID before minting past the counter', () => { + const second = makeSubgraph('second', ['dummy']) + const state = freshState() + state.lastNodeId = 2 + state.freeNodeIds.add(5) + + const result = normalizeSubgraphDefinitions( + [second], + { + nodeIds: new Set([toNodeId(1)]), + groupIds: new Set(), + linkIds: new Set(), + rerouteIds: new Set() + }, + state + ) + + expect(result.subgraphs[0].nodes![0].id).toBe(5) + expect(state.lastNodeId).toBe(2) + expect(state.freeNodeIds.has(5)).toBe(false) + }) + + it('leaves the passed-in state untouched when a dedup step throws', () => { + const first = makeSubgraph('first', ['dummy', 'dummy', 'dummy']) + const second = makeSubgraph('second', ['dummy', 'dummy', 'dummy']) + const state = freshState() + state.lastNodeId = MAX_ID - 1 + const snapshotLastNodeId = state.lastNodeId + const snapshotFreeNodeIds = new Set(state.freeNodeIds) + + expect(() => + normalizeSubgraphDefinitions( + [first, second], + { + nodeIds: new Set(), + groupIds: new Set(), + linkIds: new Set(), + rerouteIds: new Set() + }, + state + ) + ).toThrow('Node ID space exhausted') + + expect(state.lastNodeId).toBe(snapshotLastNodeId) + expect(state.freeNodeIds).toEqual(snapshotFreeNodeIds) + }) }) describe('deduplicateSubgraphGroupIds', () => { diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts index 4b52722d378..9d85c95cb20 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts @@ -2,6 +2,9 @@ import type { LGraph } from '../LGraph' import { isUuidShapedSubgraphId } from '@/schemas/subgraphIdSchema' import { toGroupId } from '@/types/groupId' import { + MAX_ID, + cloneLGraphState, + commitLGraphState, mintGroupId, mintLinkId, mintNodeId, @@ -29,8 +32,6 @@ import type { SerialisableLLink } from '../types/serialisation' -const MAX_ID = 100_000_000 - interface DeduplicationResult< Subgraph extends { id: string; nodes?: { type: string }[] } = ExportedSubgraph, @@ -64,15 +65,31 @@ export function normalizeSubgraphDefinitions( clonedSubgraphs[index] = normalizeConfiguredTopology(subgraph) } + // Mint/observe against a disposable copy of `state` so a throw partway + // through (e.g. ID space exhaustion) leaves the real allocator untouched. + const workingState = cloneLGraphState(state) deduplicateClonedSubgraphNodeIds( clonedSubgraphs, reservations.nodeIds, - state, + workingState, clonedRootNodes ) - deduplicateSubgraphGroupIds(clonedSubgraphs, reservations.groupIds, state) - deduplicateSubgraphLinkIds(clonedSubgraphs, reservations.linkIds, state) - deduplicateSubgraphRerouteIds(clonedSubgraphs, reservations.rerouteIds, state) + deduplicateSubgraphGroupIds( + clonedSubgraphs, + reservations.groupIds, + workingState + ) + deduplicateSubgraphLinkIds( + clonedSubgraphs, + reservations.linkIds, + workingState + ) + deduplicateSubgraphRerouteIds( + clonedSubgraphs, + reservations.rerouteIds, + workingState + ) + commitLGraphState(state, workingState) return { subgraphs: clonedSubgraphs, rootNodes: clonedRootNodes } } @@ -317,7 +334,7 @@ function numericSerializedNodeId(id: SerializedNodeId): number | null { * Finds the next unused ID by repeatedly calling `advance`. * Throws if the ID space is exhausted. */ -function findNextAvailableId( +export function findNextAvailableId( usedIds: Set, advance: () => number ): number { From 4bef2243c86d814019a7d839052cff108fc88ff6 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 23 Sep 2026 21:46:25 +0000 Subject: [PATCH 02/11] fix(paste): don't swallow real deserialization errors as invalid clipboard data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The clipboard-metadata paste path wrapped one try/catch around both the base64/JSON decode (expected to throw on garbage clipboard data) and the actual graph deserialization. A genuine deserialization/graph-mutation error — for example ID space exhaustion — was caught by the same handler, looked identical to "no valid metadata payload", and fell through to the legacy localStorage-clipboard paste, silently repeating the same failure. Split the two: an invalid payload still falls through to the other paste strategies, but a real deserialization error now surfaces to the user as a toast instead of triggering another paste attempt. --- src/composables/usePaste.test.ts | 29 +++++++++++++++++++++++++++++ src/composables/usePaste.ts | 20 +++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/composables/usePaste.test.ts b/src/composables/usePaste.test.ts index 84c3dabef69..15ed774a1b4 100644 --- a/src/composables/usePaste.test.ts +++ b/src/composables/usePaste.test.ts @@ -1,4 +1,5 @@ import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' +import { useToastStore } from '@/platform/updates/common/toastStore' import { useWorkspaceStore } from '@/stores/workspaceStore' import { afterEach, @@ -641,6 +642,34 @@ describe('usePaste', () => { }) }) + it('should surface a deserialization error as a toast instead of falling back to legacy paste', async () => { + const deserializeError = new Error('Node ID space exhausted') + vi.mocked(mockCanvas._deserializeItems).mockImplementation(() => { + throw deserializeError + }) + + usePaste() + + const data = { test: 'data' } + const encoded = btoa(JSON.stringify(data)) + const html = `
` + + 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).toHaveBeenCalled() + 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, diff --git a/src/composables/usePaste.ts b/src/composables/usePaste.ts index f88cf3a35b3..76b9150d119 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 type { ClipboardItems } from '@/lib/litegraph/src/types/serialisation' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { app } from '@/scripts/app' import { useWorkspaceStore } from '@/stores/workspaceStore' @@ -44,17 +46,29 @@ function pasteClipboardItems(data: DataTransfer): boolean { const rawData = data.getData('text/html') const match = rawData.match(/data-metadata="([A-Za-z0-9+/=]+)"/)?.[1] if (!match) return false + + let parsed: ClipboardItems try { // Decode UTF-8 safe base64 const binaryString = atob(match) const bytes = Uint8Array.from(binaryString, (c) => c.charCodeAt(0)) const decodedData = new TextDecoder().decode(bytes) - useCanvasStore().getCanvas()._deserializeItems(JSON.parse(decodedData), {}) - return true + parsed = JSON.parse(decodedData) as ClipboardItems } catch (err) { + // Not a valid metadata payload — other paste strategies may still apply. console.error(err) + return false + } + + // A real deserialization/graph-mutation failure (e.g. node ID space + // exhaustion) is a genuine paste error, not "no valid metadata payload" — + // it must not fall through to the other paste strategies below. + try { + useCanvasStore().getCanvas()._deserializeItems(parsed, {}) + } catch (err) { + useErrorHandling().toastErrorHandler(err) } - return false + return true } function isWorkflow( From 17006a820b971cd8dbc147c8d2015b5ad63dbfa7 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 24 Sep 2026 00:30:36 +0000 Subject: [PATCH 03/11] fix(litegraph): address ID-recycling review findings - Guard group ID release behind the found-in-list check, so a redundant remove() can't return a live group's ID to the free pool - Move MAX_ID enforcement from observe* onto mint*: observe* now records the true unclamped high-water mark (rejecting only negative/non-integer input and agent/CRDT-range node ids), while mint* refuses to allocate past the ceiling - release* now ignore ids above MAX_ID instead of pooling them - Clear executionStore progress state and cached node outputs when a node's id is released, via the existing error-clearing hook - Wrap _deserializeItems's mutation body in try/finally so afterChange()/emitAfterChange() always run, even on a mid-paste throw - Validate decoded clipboard payloads against a minimal shape before committing to the ComfyUI-clipboard paste path, falling through to legacy paste for anything that doesn't match - Project only the four persisted counters in asSerialisable(), keeping the free-id pools out of saved workflows and clipboard data - Strengthen the recycled-ID assertions in two tests that had been weakened to tolerate ID recycling --- .../graph/useErrorClearingHooks.test.ts | 33 ++++++ .../graph/useErrorClearingHooks.ts | 4 + src/composables/usePaste.test.ts | 54 +++++++++- src/composables/usePaste.ts | 68 +++++++++++- .../LGraph.linkPresentationTransfer.test.ts | 25 +++++ src/lib/litegraph/src/LGraph.test.ts | 55 ++++++++++ src/lib/litegraph/src/LGraph.ts | 7 +- .../src/LGraphCanvas.clipboard.test.ts | 58 ++++++++++ .../LGraphCanvas.subgraphPasteDelete.test.ts | 28 +++-- src/lib/litegraph/src/LGraphCanvas.ts | 17 ++- src/lib/litegraph/src/idAllocation.test.ts | 99 ++++++++++++++++- src/lib/litegraph/src/idAllocation.ts | 100 +++++++++++++----- .../NodeIdSpaceExhaustedError.ts | 6 ++ .../litegraph/src/subgraph/Subgraph.test.ts | 3 +- src/stores/executionStore.ts | 34 ++++++ 15 files changed, 541 insertions(+), 50 deletions(-) create mode 100644 src/lib/litegraph/src/infrastructure/NodeIdSpaceExhaustedError.ts diff --git a/src/composables/graph/useErrorClearingHooks.test.ts b/src/composables/graph/useErrorClearingHooks.test.ts index d7936c1fb51..d1366892509 100644 --- a/src/composables/graph/useErrorClearingHooks.test.ts +++ b/src/composables/graph/useErrorClearingHooks.test.ts @@ -27,6 +27,8 @@ import { useMissingNodesErrorStore } from '@/platform/nodeReplacement/missingNod import { app } from '@/scripts/app' import { ChangeTracker } from '@/scripts/changeTracker' import { useExecutionErrorStore } from '@/stores/executionErrorStore' +import { useExecutionStore } from '@/stores/executionStore' +import { useNodeOutputStore } from '@/stores/nodeOutputStore' import { createNodeExecutionId } from '@/types/nodeIdentification' import { toNodeId } from '@/types/nodeId' import { seedRequiredInputMissingNodeError } from '@/utils/__tests__/executionErrorTestUtils' @@ -443,6 +445,37 @@ describe('installErrorClearingHooks lifecycle', () => { expect(node.onWidgetChanged).toBe(originalOnWidgetChanged) }) + it('clears execution progress and cached outputs for a removed node, so a later mint of the recycled ID does not inherit them', () => { + const graph = new LGraph() + const node = new LGraphNode('test') + graph.add(node) + installErrorClearingHooks(graph) + + const executionStore = useExecutionStore() + const nodeOutputStore = useNodeOutputStore() + executionStore.nodeProgressStates = { + [String(node.id)]: { + display_node_id: node.id, + node_id: node.id, + prompt_id: 'test-prompt', + value: 1, + max: 2, + state: 'running' + } + } + nodeOutputStore.setNodeOutputs(node, 'output.png') + expect(nodeOutputStore.getNodeOutputs(node)).toBeDefined() + + graph.remove(node) + + expect(executionStore.nodeProgressStates[String(node.id)]).toBeUndefined() + + const replacement = new LGraphNode('test') + replacement.id = node.id + graph.add(replacement) + expect(nodeOutputStore.getNodeOutputs(replacement)).toBeUndefined() + }) + it('does not double-wrap callbacks when installErrorClearingHooks is called twice', () => { const graph = new LGraph() const node = new LGraphNode('test') diff --git a/src/composables/graph/useErrorClearingHooks.ts b/src/composables/graph/useErrorClearingHooks.ts index a6df91ef8f0..5c26f649e72 100644 --- a/src/composables/graph/useErrorClearingHooks.ts +++ b/src/composables/graph/useErrorClearingHooks.ts @@ -41,6 +41,8 @@ import { useNodeReplacementStore } from '@/platform/nodeReplacement/nodeReplacem import { getCnrIdFromNode } from '@/platform/nodeReplacement/cnrIdUtil' import { app } from '@/scripts/app' import { useExecutionErrorStore } from '@/stores/executionErrorStore' +import { useExecutionStore } from '@/stores/executionStore' +import { useNodeOutputStore } from '@/stores/nodeOutputStore' import { toNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' import { useModelToNodeStore } from '@/stores/modelToNodeStore' @@ -623,6 +625,8 @@ export function installErrorClearingHooks(graph: LGraph): () => void { if (!successor) { const execId = getRemovedNodeExecutionId(graph, node.id) removeNodeErrors(node, execId) + useExecutionStore().clearNodeProgressState(String(node.id)) + useNodeOutputStore().removeNodeOutputsForNode(node) } scheduleDropOutOfScopeMissingMedia() restoreNodeHooksRecursive(node) diff --git a/src/composables/usePaste.test.ts b/src/composables/usePaste.test.ts index 15ed774a1b4..9ff092b7fe0 100644 --- a/src/composables/usePaste.test.ts +++ b/src/composables/usePaste.test.ts @@ -621,9 +621,12 @@ describe('usePaste', () => { expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() }) + const validClipboardData = { + nodes: [{ id: 1, type: 'KSampler', pos: [0, 0] }] + } + it('should handle clipboard items with metadata', async () => { - const data = { test: 'data' } - const encoded = btoa(JSON.stringify(data)) + const encoded = btoa(JSON.stringify(validClipboardData)) const html = `
` usePaste() @@ -636,7 +639,7 @@ describe('usePaste', () => { await vi.waitFor(() => { expect(mockCanvas._deserializeItems).toHaveBeenCalledWith( - data, + validClipboardData, expect.any(Object) ) }) @@ -650,8 +653,7 @@ describe('usePaste', () => { usePaste() - const data = { test: 'data' } - const encoded = btoa(JSON.stringify(data)) + const encoded = btoa(JSON.stringify(validClipboardData)) const html = `
` const dataTransfer = new DataTransfer() @@ -670,6 +672,48 @@ describe('usePaste', () => { }) }) + it('falls through to legacy paste when the data-metadata payload is not clipboard-shaped', async () => { + // A third-party page whose HTML coincidentally contains a + // base64-ish `data-metadata` attribute that happens to decode to + // unrelated, valid JSON — must not be treated as a graph payload. + const unrelatedPayload = { foo: 'bar' } + const encoded = btoa(JSON.stringify(unrelatedPayload)) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'the real copied text') + + const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) + document.dispatchEvent(event) + + await vi.waitFor(() => { + expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + }) + expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() + }) + + it('falls through to legacy paste when the decoded metadata is malformed (e.g. base64-encoded null)', async () => { + const encoded = btoa(JSON.stringify(null)) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'the real copied text') + + const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) + document.dispatchEvent(event) + + await vi.waitFor(() => { + expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + }) + expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() + }) + it('should skip node metadata paste when a media node is selected', async () => { const mockNode = createMockLGraphNode({ is_selected: true, diff --git a/src/composables/usePaste.ts b/src/composables/usePaste.ts index 76b9150d119..791182e5b03 100644 --- a/src/composables/usePaste.ts +++ b/src/composables/usePaste.ts @@ -1,4 +1,5 @@ import { useEventListener } from '@vueuse/core' +import { z } from 'zod' import { useErrorHandling } from '@/composables/useErrorHandling' import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' @@ -15,6 +16,66 @@ import { } from '@/utils/litegraphUtil' import { shouldIgnoreCopyPaste } from '@/workbench/eventHelpers' +const zClipboardNodeId = z.union([z.number(), z.string()]) +const zClipboardPos = z.array(z.number()).min(2) + +const zClipboardNode = z + .object({ + id: zClipboardNodeId, + type: z.string(), + pos: zClipboardPos + }) + .passthrough() + +const zClipboardGroup = z + .object({ + bounding: z.array(z.number()).min(4) + }) + .passthrough() + +const zClipboardReroute = z + .object({ + id: z.number(), + pos: zClipboardPos + }) + .passthrough() + +const zClipboardLink = z + .object({ + id: z.number(), + origin_id: zClipboardNodeId, + target_id: zClipboardNodeId + }) + .passthrough() + +const zClipboardSubgraph = z.object({ id: z.string() }).passthrough() + +/** + * The subset of `ClipboardItems`' shape `_deserializeItems` actually + * dereferences (`item.pos[0]`, `group.bounding[0]`, node/link ids). Rejects a + * decoded `data-metadata` payload that merely happens to be valid JSON — + * copied from a third-party page whose HTML coincidentally matches the + * base64-ish attribute pattern — as well as a well-formed but empty object, + * which carries none of the five keys below. + */ +const zClipboardItems = z + .object({ + nodes: z.array(zClipboardNode).optional(), + groups: z.array(zClipboardGroup).optional(), + reroutes: z.array(zClipboardReroute).optional(), + links: z.array(zClipboardLink).optional(), + subgraphs: z.array(zClipboardSubgraph).optional() + }) + .passthrough() + .refine( + (value) => + Array.isArray(value.nodes) || + Array.isArray(value.groups) || + Array.isArray(value.reroutes) || + Array.isArray(value.links) || + Array.isArray(value.subgraphs) + ) + export function cloneDataTransfer(original: DataTransfer): DataTransfer { const persistent = new DataTransfer() @@ -47,19 +108,22 @@ function pasteClipboardItems(data: DataTransfer): boolean { const match = rawData.match(/data-metadata="([A-Za-z0-9+/=]+)"/)?.[1] if (!match) return false - let parsed: ClipboardItems + let decoded: 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) - parsed = JSON.parse(decodedData) as ClipboardItems + decoded = JSON.parse(decodedData) } catch (err) { // Not a valid metadata payload — other paste strategies may still apply. console.error(err) return false } + if (!zClipboardItems.safeParse(decoded).success) return false + const parsed = decoded as ClipboardItems + // A real deserialization/graph-mutation failure (e.g. node ID space // exhaustion) is a genuine paste error, not "no valid metadata payload" — // it must not fall through to the other paste strategies below. diff --git a/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts b/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts index c3311075bdc..c1f25408a87 100644 --- a/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts +++ b/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts @@ -525,6 +525,31 @@ describe('link presentation transfer across recreation flows', () => { } }) + it('clears stale presentation when a released link ID is recycled onto an unrelated link', () => { + const graph = createTestRootGraph() + const origin = createTestNode(graph, [], ['number']) + const target = createTestNode(graph, ['number']) + const link = origin.connect(0, target, 0) + if (!link) throw new Error('Failed to connect removed test link') + const scope = graphScopeOf(graph) + useLinkPresentationStore().patch(scope, link.id, { + hidden: true, + label: 'Stale' + }) + + graph.removeLink(link.id) + + const otherOrigin = createTestNode(graph, [], ['number']) + const otherTarget = createTestNode(graph, ['number']) + const recycled = otherOrigin.connect(0, otherTarget, 0) + if (!recycled) throw new Error('Failed to connect recycling test link') + + expect(recycled.id).toBe(link.id) + expect( + useLinkPresentationStore().getPresentation(scope, recycled.id) + ).toBeUndefined() + }) + it('preserves each ordinary reroute presentation when moving its source', () => { const graph = createTestRootGraph() const source = createTestNode(graph, [], ['number']) diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index 702005b278d..3d99d6057eb 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -27,6 +27,7 @@ import { isRootGraphDocBound, registerDocBoundRootGraphProbe } from '@/lib/litegraph/src/docBoundGraphs' +import { MAX_ID } from '@/lib/litegraph/src/idAllocation' import type { UUID } from '@/utils/uuid' import { createUuidv4, zeroUuid } from '@/utils/uuid' import { useEntityIdStore } from '@/stores/entityIdStore' @@ -1171,6 +1172,26 @@ describe('node:before-removed event', () => { expect(fired).not.toHaveBeenCalled() }) + it('does not release a group ID on a redundant remove() call, even after a new group has reused it', () => { + const graph = new LGraph() + const first = new LGraphGroup('first') + graph.add(first) + const firstId = first.id + + graph.remove(first) + + const second = new LGraphGroup('second') + graph.add(second) + expect(second.id).toBe(firstId) + + // Redundant: `first` is already detached, not in `graph.groups`. + graph.remove(first) + + const third = new LGraphGroup('third') + graph.add(third) + expect(third.id).not.toBe(second.id) + }) + it('does not fire node:before-removed when ignore_remove is set', () => { const graph = new LGraph() const node = new LGraphNode('test') @@ -2426,6 +2447,40 @@ describe('deduplicateSubgraphNodeIds (via configure)', () => { }).toThrow('Node ID space exhausted') }) + it('does not let observing an above-MAX_ID node ID cause a later mint to collide with it', () => { + const graph = new LGraph() + const existing = new LGraphNode('test') + existing.id = toNodeId(MAX_ID + 1) + graph.add(existing) + + expect(() => graph.add(new LGraphNode('test'))).toThrow( + 'Node ID space exhausted' + ) + expect(graph.getNodeById(toNodeId(MAX_ID + 1))).toBe(existing) + expect(graph.nodes).toHaveLength(1) + }) + + it('asSerialisable() projects only the four counters, not the live free-ID pools', () => { + const graph = new LGraph() + const node = new LGraphNode('test') + graph.add(node) + graph.remove(node) + + const serialised = graph.asSerialisable() + + expect(serialised.state).toEqual({ + lastGroupId: 0, + lastNodeId: 1, + lastLinkId: 0, + lastRerouteId: 0 + }) + expect(JSON.stringify(serialised.state)).not.toContain('freeNodeIds') + + // The returned snapshot must not alias the live allocator state. + graph.state.freeNodeIds.add(999) + expect(serialised.state).not.toHaveProperty('freeNodeIds') + }) + it('is a no-op when subgraph node IDs are already unique', () => { const graph = new LGraph() graph.configure(structuredClone(uniqueSubgraphNodeIds)) diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 2273650f7d5..01e66eb07e2 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -45,6 +45,7 @@ import { isFloatingTopology } from '@/types/linkTopology' import { toRerouteId } from '@/types/rerouteId' import { graphScopeOf, toRootGraphId } from '@/types/graphScopeId' import { + counterSnapshot, createLGraphState, mintGroupId, mintLinkId, @@ -1474,8 +1475,8 @@ export class LGraph const index = this._groups.indexOf(node) if (index != -1) { this._groups.splice(index, 1) + releaseGroupId(this.state, node.id) } - releaseGroupId(this.state, node.id) detachGroupLayout(node) node.graph = undefined this.incrementVersion() @@ -2970,7 +2971,7 @@ export class LGraph revision, version: LGraph.serialisedSchemaVersion, config, - state, + state: counterSnapshot(state), groups, nodes, ...topology, @@ -3719,7 +3720,7 @@ export class Subgraph return { id: this.id, version: LGraph.serialisedSchemaVersion, - state: this.state, + state: counterSnapshot(this.state), revision: this.revision, config: this.config, name: this.name, diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index 64a513989e2..01a4836384e 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -269,6 +269,64 @@ function createCanvas(graph: LGraph): LGraphCanvas { return new LGraphCanvas(el, graph, { skip_render: true, skip_events: true }) } +describe('_deserializeItems change-tracking invariant', () => { + function createExhaustedRootGraph(): LGraph { + const rootGraph = new LGraph() + rootGraph.state.lastNodeId = MAX_ID + + const existingNode = new LGraphNode('existing') + existingNode.id = toNodeId(1) + rootGraph.add(existingNode) + return rootGraph + } + + function createCollidingClipboardItems(): ClipboardItems { + const pastedSubgraph: ExportedSubgraph = { + id: createUuidv4(), + 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: [] + } + return { + nodes: [], + groups: [], + reroutes: [], + links: [], + subgraphs: [pastedSubgraph] + } + } + + it('calls afterChange/emitAfterChange even when the paste throws partway through', () => { + const rootGraph = createExhaustedRootGraph() + const canvas = createCanvas(rootGraph) + const parsed = createCollidingClipboardItems() + + const beforeChangeSpy = vi.spyOn(rootGraph, 'beforeChange') + const afterChangeSpy = vi.spyOn(rootGraph, 'afterChange') + const emitBeforeChangeSpy = vi.spyOn(canvas, 'emitBeforeChange') + const emitAfterChangeSpy = vi.spyOn(canvas, 'emitAfterChange') + + expect(() => canvas._deserializeItems(parsed, {})).toThrow( + 'Node ID space exhausted' + ) + + expect(beforeChangeSpy).toHaveBeenCalledTimes(1) + expect(emitBeforeChangeSpy).toHaveBeenCalledTimes(1) + expect(afterChangeSpy).toHaveBeenCalledTimes(1) + expect(emitAfterChangeSpy).toHaveBeenCalledTimes(1) + }) +}) + describe('link presentation transfer across recreation flows', () => { it.for([ { diff --git a/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts b/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts index 7ccfc086cb6..763c0a6f008 100644 --- a/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts @@ -206,14 +206,28 @@ function terminalState(f: Fixture) { } } -/** Recycled floating-link IDs are history-dependent; only their count is order-independent. */ -function withFloatingLinkCounts(state: ReturnType) { +/** + * Recycled floating-link IDs are history-dependent, so raw values can differ + * between the two delete orders even when the underlying structure agrees. + * Replacing each ID with its rank among this run's own sorted floating-link + * IDs still proves the real invariant — that a reroute's `floatingLinkIds` + * entry resolves to one of the graph's *actual* floating links, and to the + * same relative one in both orders — without asserting the exact recycled + * value. Reducing to bare counts, by contrast, would equally accept a + * reroute that references a link the graph doesn't have, or the wrong one + * among several, as long as the totals matched. + */ +function withStableFloatingLinkIdentities( + state: ReturnType +) { + const sortedIds = [...state.remainingFloatingLinks].sort((a, b) => a - b) + const rankOf = (id: LinkId) => sortedIds.indexOf(id) return { ...state, - remainingFloatingLinks: state.remainingFloatingLinks.length, + remainingFloatingLinks: sortedIds.map((_, index) => index), remainingReroutes: state.remainingReroutes.map((reroute) => ({ linkIds: reroute.linkIds, - floatingLinkIds: reroute.floatingLinkIds.length + floatingLinkIds: reroute.floatingLinkIds.map(rankOf) })) } } @@ -371,8 +385,10 @@ describe('subgraph copy/paste then delete in both orders', () => { copyFirst.rootGraph.remove(copyFirst.copy) copyFirst.rootGraph.remove(copyFirst.original) - expect(withFloatingLinkCounts(terminalState(copyFirst))).toStrictEqual( - withFloatingLinkCounts(terminalState(originalFirst)) + expect( + withStableFloatingLinkIdentities(terminalState(copyFirst)) + ).toStrictEqual( + withStableFloatingLinkIdentities(terminalState(originalFirst)) ) // That shared state is: both definitions released, both instances gone, diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index db23548e8ce..81561251919 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -4180,6 +4180,20 @@ export class LGraphCanvas implements CustomEventDispatcher graph.beforeChange() this.emitBeforeChange() + try { + return this._deserializeItemsBody(parsed, graph, connectInputs, position) + } finally { + graph.afterChange() + this.emitAfterChange() + } + } + + private _deserializeItemsBody( + parsed: ClipboardItems, + graph: LGraph, + connectInputs: boolean, + position: Point + ): ClipboardPasteResult | undefined { // Parse & initialise parsed.nodes ??= [] parsed.groups ??= [] @@ -4390,9 +4404,6 @@ export class LGraphCanvas implements CustomEventDispatcher forEachNode(graph, (n) => n.onGraphConfigured?.()) forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) - graph.afterChange() - this.emitAfterChange() - return results } diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index f2f50fd9364..4ecc776d46e 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -214,12 +214,16 @@ describe('idAllocation', () => { { observe: (state: ReturnType) => observeNodeId(state, toNodeId(MAX_ID + 1)), + mint: (state: ReturnType) => + mintNodeId(state), read: (state: ReturnType) => state.lastNodeId, name: 'node' }, { observe: (state: ReturnType) => observeGroupId(state, toGroupId(MAX_ID + 1)), + mint: (state: ReturnType) => + mintGroupId(state), read: (state: ReturnType) => state.lastGroupId, name: 'group' @@ -227,6 +231,8 @@ describe('idAllocation', () => { { observe: (state: ReturnType) => observeLinkId(state, toLinkId(MAX_ID + 1)), + mint: (state: ReturnType) => + mintLinkId(state), read: (state: ReturnType) => Number(state.lastLinkId), name: 'link' @@ -234,16 +240,19 @@ describe('idAllocation', () => { { observe: (state: ReturnType) => observeRerouteId(state, toRerouteId(MAX_ID + 1)), + mint: (state: ReturnType) => + mintRerouteId(state), read: (state: ReturnType) => Number(state.lastRerouteId), name: 'reroute' } ])( - 'clamps an absurd incoming $name counter to MAX_ID', - ({ observe, read }) => { + 'records an incoming $name counter above MAX_ID unclamped, but refuses to mint past it', + ({ observe, mint, read }) => { const state = createLGraphState() observe(state) - expect(read(state)).toBe(MAX_ID) + expect(read(state)).toBe(MAX_ID + 1) + expect(() => mint(state)).toThrow('Node ID space exhausted') } ) @@ -252,6 +261,90 @@ describe('idAllocation', () => { observeNodeId(state, toNodeId(-5)) expect(state.lastNodeId).toBe(0) }) + + it('does not let an observed node ID in the agent/CRDT reserved-bit range drag lastNodeId up', () => { + const state = createLGraphState() + observeNodeId(state, toNodeId(AGENT_RESERVED_BIT.toString())) + expect(state.lastNodeId).toBe(0) + expect(mintNodeId(state)).toBe('1') + }) + }) + + describe('mint-side MAX_ID enforcement', () => { + it.for([ + { + mint: mintNodeId, + seed: (state: ReturnType) => { + state.lastNodeId = MAX_ID + }, + name: 'node' + }, + { + mint: mintGroupId, + seed: (state: ReturnType) => { + state.lastGroupId = MAX_ID + }, + name: 'group' + }, + { + mint: mintLinkId, + seed: (state: ReturnType) => { + state.lastLinkId = toLinkId(MAX_ID) + }, + name: 'link' + }, + { + mint: mintRerouteId, + seed: (state: ReturnType) => { + state.lastRerouteId = toRerouteId(MAX_ID) + }, + name: 'reroute' + } + ])('refuses to mint a $name ID past MAX_ID', ({ mint, seed }) => { + const state = createLGraphState() + seed(state) + expect(() => mint(state)).toThrow('Node ID space exhausted') + }) + }) + + describe('release-side range enforcement', () => { + it.for([ + { + release: (state: ReturnType) => + releaseNodeId(state, toNodeId(MAX_ID + 1)), + read: (state: ReturnType) => + state.freeNodeIds, + name: 'node' + }, + { + release: (state: ReturnType) => + releaseGroupId(state, toGroupId(MAX_ID + 1)), + read: (state: ReturnType) => + state.freeGroupIds, + name: 'group' + }, + { + release: (state: ReturnType) => + releaseLinkId(state, toLinkId(MAX_ID + 1)), + read: (state: ReturnType) => + state.freeLinkIds, + name: 'link' + }, + { + release: (state: ReturnType) => + releaseRerouteId(state, toRerouteId(MAX_ID + 1)), + read: (state: ReturnType) => + state.freeRerouteIds, + name: 'reroute' + } + ])( + 'never pools an above-MAX_ID $name ID for reuse', + ({ release, read }) => { + const state = createLGraphState() + release(state) + expect(read(state).size).toBe(0) + } + ) }) describe('cloneLGraphState / commitLGraphState', () => { diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index fdc2a377919..9abac8168d7 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -1,3 +1,4 @@ +import { NodeIdSpaceExhaustedError } from './infrastructure/NodeIdSpaceExhaustedError' import { toGroupId } from '@/types/groupId' import type { GroupId } from '@/types/groupId' import { toLinkId } from '@/types/linkId' @@ -54,6 +55,27 @@ export function cloneLGraphState(state: LGraphState): LGraphState { } } +/** + * Projects only the four persisted counters off `state`, leaving its free-id + * pools behind. `asSerialisable()` uses this instead of returning `state` + * itself, so a saved workflow or clipboard payload never carries the live, + * mutable free-id `Set`s by reference — those are transient allocator + * bookkeeping, not part of the persisted schema. + */ +export function counterSnapshot(state: LGraphState): { + lastGroupId: number + lastNodeId: number + lastLinkId: number + lastRerouteId: number +} { + return { + lastGroupId: state.lastGroupId, + lastNodeId: state.lastNodeId, + lastLinkId: state.lastLinkId, + lastRerouteId: state.lastRerouteId + } +} + /** Copies every counter and free-id pool from `source` onto `target`, in place. */ export function commitLGraphState( target: LGraphState, @@ -77,10 +99,9 @@ function takeFreeId(freeIds: Set): number | undefined { return value } -/** Clamps a candidate counter value to the valid range, or `undefined` to reject it outright. */ -function sanitizeCounterCandidate(value: number): number | undefined { - if (!Number.isInteger(value) || value < 0) return undefined - return Math.min(value, MAX_ID) +/** Whether `value` is a well-formed counter candidate — a non-negative integer. */ +function isValidCounterCandidate(value: number): boolean { + return Number.isInteger(value) && value >= 0 } /** @@ -189,31 +210,42 @@ export function matchesReservedBitConvention(id: NodeId): boolean { return (big & AGENT_RESERVED_BIT) !== 0n || (big & CRDT_DISJOINT_FLOOR) !== 0n } +/** Advances a counter by one, throwing rather than minting past `MAX_ID`. */ +function advanceCounter(lastValue: number): number { + const next = lastValue + 1 + if (next > MAX_ID) throw new NodeIdSpaceExhaustedError() + return next +} + export function mintNodeId( state: LGraphState, mode: NodeIdMintMode = 'sequential' ): NodeId { if (mode === 'crdt-disjoint') return mintCrdtDisjointNodeId() const recycled = takeFreeId(state.freeNodeIds) - return toNodeId(recycled ?? ++state.lastNodeId) + if (recycled !== undefined) return toNodeId(recycled) + state.lastNodeId = advanceCounter(state.lastNodeId) + return toNodeId(state.lastNodeId) } export function mintGroupId(state: LGraphState): GroupId { const recycled = takeFreeId(state.freeGroupIds) - return toGroupId(recycled ?? ++state.lastGroupId) + if (recycled !== undefined) return toGroupId(recycled) + state.lastGroupId = advanceCounter(state.lastGroupId) + return toGroupId(state.lastGroupId) } export function mintLinkId(state: LGraphState): LinkId { const recycled = takeFreeId(state.freeLinkIds) if (recycled !== undefined) return toLinkId(recycled) - state.lastLinkId = toLinkId(Number(state.lastLinkId) + 1) + state.lastLinkId = toLinkId(advanceCounter(Number(state.lastLinkId))) return state.lastLinkId } export function mintRerouteId(state: LGraphState): RerouteId { const recycled = takeFreeId(state.freeRerouteIds) if (recycled !== undefined) return toRerouteId(recycled) - state.lastRerouteId = toRerouteId(Number(state.lastRerouteId) + 1) + state.lastRerouteId = toRerouteId(advanceCounter(Number(state.lastRerouteId))) return state.lastRerouteId } @@ -225,57 +257,71 @@ export function mintRerouteId(state: LGraphState): RerouteId { */ export function releaseNodeId(state: LGraphState, id: NodeId): void { const numeric = safeIntegerValueOf(id) - if (numeric === undefined || numeric <= 0) return + if (numeric === undefined || numeric <= 0 || numeric > MAX_ID) return if (BigInt(numeric) >= AGENT_RESERVED_BIT) return state.freeNodeIds.add(numeric) } export function releaseGroupId(state: LGraphState, id: GroupId): void { - if (!Number.isInteger(id) || id <= 0) return + if (!Number.isInteger(id) || id <= 0 || id > MAX_ID) return state.freeGroupIds.add(id) } export function releaseLinkId(state: LGraphState, id: LinkId): void { const numeric = Number(id) - if (!Number.isInteger(numeric) || numeric <= 0) return + if (!Number.isInteger(numeric) || numeric <= 0 || numeric > MAX_ID) return state.freeLinkIds.add(numeric) } export function releaseRerouteId(state: LGraphState, id: RerouteId): void { const numeric = Number(id) - if (!Number.isInteger(numeric) || numeric <= 0) return + if (!Number.isInteger(numeric) || numeric <= 0 || numeric > MAX_ID) return state.freeRerouteIds.add(numeric) } +/** + * Records that `id` is in use, raising `lastNodeId` to match when it is + * higher. Never clamped to `MAX_ID`: an observed id is proof an entity with + * that id already exists, so lying about the high-water mark would let a + * later mint hand out an id the observed entity already owns. An id in the + * agent/CRDT reserved-bit range (see {@link AGENT_RESERVED_BIT}) is ignored + * for this purpose the same way {@link releaseNodeId} ignores it for + * recycling — that range is a disjoint id space the sequential counter never + * mints into, so observing it must not drag `lastNodeId` up to match. + */ export function observeNodeId(state: LGraphState, id: NodeId): void { const numericId = Number(id) state.freeNodeIds.delete(numericId) - const sanitized = sanitizeCounterCandidate(numericId) - if (sanitized !== undefined && sanitized > state.lastNodeId) { - state.lastNodeId = sanitized - } + if (!isValidCounterCandidate(numericId)) return + if (BigInt(numericId) >= AGENT_RESERVED_BIT) return + if (numericId > state.lastNodeId) state.lastNodeId = numericId } export function observeGroupId(state: LGraphState, id: GroupId): void { state.freeGroupIds.delete(id) - const sanitized = sanitizeCounterCandidate(id) - if (sanitized !== undefined && sanitized > state.lastGroupId) { - state.lastGroupId = sanitized + if (isValidCounterCandidate(id) && id > state.lastGroupId) { + state.lastGroupId = id } } export function observeLinkId(state: LGraphState, id: LinkId): void { - state.freeLinkIds.delete(Number(id)) - const sanitized = sanitizeCounterCandidate(Number(id)) - if (sanitized !== undefined && sanitized > Number(state.lastLinkId)) { - state.lastLinkId = toLinkId(sanitized) + const numericId = Number(id) + state.freeLinkIds.delete(numericId) + if ( + isValidCounterCandidate(numericId) && + numericId > Number(state.lastLinkId) + ) { + state.lastLinkId = toLinkId(numericId) } } export function observeRerouteId(state: LGraphState, id: RerouteId): void { - state.freeRerouteIds.delete(Number(id)) - const sanitized = sanitizeCounterCandidate(Number(id)) - if (sanitized !== undefined && sanitized > Number(state.lastRerouteId)) { - state.lastRerouteId = toRerouteId(sanitized) + const numericId = Number(id) + state.freeRerouteIds.delete(numericId) + if ( + isValidCounterCandidate(numericId) && + numericId > Number(state.lastRerouteId) + ) { + state.lastRerouteId = toRerouteId(numericId) } } diff --git a/src/lib/litegraph/src/infrastructure/NodeIdSpaceExhaustedError.ts b/src/lib/litegraph/src/infrastructure/NodeIdSpaceExhaustedError.ts new file mode 100644 index 00000000000..a70777e677a --- /dev/null +++ b/src/lib/litegraph/src/infrastructure/NodeIdSpaceExhaustedError.ts @@ -0,0 +1,6 @@ +export class NodeIdSpaceExhaustedError extends Error { + constructor(message: string = 'Node ID space exhausted', cause?: Error) { + super(message, { cause }) + this.name = 'NodeIdSpaceExhaustedError' + } +} diff --git a/src/lib/litegraph/src/subgraph/Subgraph.test.ts b/src/lib/litegraph/src/subgraph/Subgraph.test.ts index 8449ed31ff4..36101412369 100644 --- a/src/lib/litegraph/src/subgraph/Subgraph.test.ts +++ b/src/lib/litegraph/src/subgraph/Subgraph.test.ts @@ -14,6 +14,7 @@ import { } from '@/lib/litegraph/src/constants' import type { LGraph } from '@/lib/litegraph/src/litegraph' import { createUuidv4, Subgraph } from '@/lib/litegraph/src/litegraph' +import { counterSnapshot } from '@/lib/litegraph/src/idAllocation' import { subgraphTest } from './__fixtures__/subgraphFixtures' import { assertSubgraphStructure, @@ -103,7 +104,7 @@ describe('Subgraph Construction', () => { expect(subgraph.rootGraph.subgraphs.has(clone.id)).toBe(false) expect(subgraph.asSerialisable()).toEqual({ ...sourceBeforeClone, - state: subgraph.state + state: counterSnapshot(subgraph.state) }) }) }) diff --git a/src/stores/executionStore.ts b/src/stores/executionStore.ts index 8554849756f..41d7620d773 100644 --- a/src/stores/executionStore.ts +++ b/src/stores/executionStore.ts @@ -945,6 +945,39 @@ export const useExecutionStore = defineStore('execution', () => { executionErrorStore.clearPromptError(runErrorKey) } + /** + * Removes every progress-state entry belonging to `nodeId`, from the live + * snapshot and from every retained per-job snapshot. Entries for a node + * nested in a subgraph are keyed by a colon-joined execution id + * (`parentId:nodeId`), so a key matches when its final segment is `nodeId`. + * Called when a node id is released back to the free pool, so a later + * mint of that id cannot inherit a deleted node's cached progress. + */ + function clearNodeProgressState(nodeId: string) { + const matches = (key: string) => + key === nodeId || key.endsWith(`:${nodeId}`) + + if (Object.keys(nodeProgressStates.value).some(matches)) { + const next = { ...nodeProgressStates.value } + for (const key of Object.keys(next)) { + if (matches(key)) delete next[key] + } + nodeProgressStates.value = next + } + + let changedByJob = false + const nextByJob = { ...nodeProgressStatesByJob.value } + for (const [jobId, states] of Object.entries(nextByJob)) { + const staleKeys = Object.keys(states).filter(matches) + if (!staleKeys.length) continue + const nextStates = { ...states } + for (const key of staleKeys) delete nextStates[key] + nextByJob[jobId] = nextStates + changedByJob = true + } + if (changedByJob) nodeProgressStatesByJob.value = nextByJob + } + function getNodeIdIfExecuting(nodeId: string | number) { const nodeIdStr = String(nodeId) return nodeIdStr.includes(':') @@ -1149,6 +1182,7 @@ export const useExecutionStore = defineStore('execution', () => { nodeProgressStates, nodeLocationProgressStates, nodeProgressStatesByJob, + clearNodeProgressState, runningJobIds, runningWorkflowCount, initializingJobIds, From c65a9c6227a4daf60a44a703dc928c9666068c5c Mon Sep 17 00:00:00 2001 From: Amp Date: Thu, 24 Sep 2026 00:39:39 +0000 Subject: [PATCH 04/11] fix: preserve monotonic graph IDs Amp-Thread-ID: https://ampcode.com/threads/T-01a0d0b9-b759-73a9-833a-e61f0faadb1b --- eslint-suppressions.json | 5 - .../graph/useErrorClearingHooks.test.ts | 33 -- .../graph/useErrorClearingHooks.ts | 4 - src/composables/useCopy.test.ts | 2 +- src/composables/useCopy.ts | 2 +- src/composables/usePaste.test.ts | 93 ++--- src/composables/usePaste.ts | 75 +--- .../LGraph.linkPresentationTransfer.test.ts | 33 +- src/lib/litegraph/src/LGraph.test.ts | 148 +------- src/lib/litegraph/src/LGraph.ts | 20 +- .../src/LGraphCanvas.clipboard.test.ts | 153 +++----- .../LGraphCanvas.subgraphPasteDelete.test.ts | 32 +- src/lib/litegraph/src/LGraphCanvas.ts | 2 +- ...ceExhausted.ts => nodeIdsAtFormerLimit.ts} | 10 +- .../canvas/LinkConnector.integration.test.ts | 6 +- src/lib/litegraph/src/idAllocation.test.ts | 340 +++++------------- src/lib/litegraph/src/idAllocation.ts | 199 +++------- .../NodeIdSpaceExhaustedError.ts | 6 - .../litegraph/src/subgraph/Subgraph.test.ts | 3 +- .../subgraph/subgraphDeduplication.test.ts | 45 ++- .../src/subgraph/subgraphDeduplication.ts | 21 +- src/stores/executionStore.ts | 34 -- 22 files changed, 279 insertions(+), 987 deletions(-) rename src/lib/litegraph/src/__fixtures__/{nodeIdSpaceExhausted.ts => nodeIdsAtFormerLimit.ts} (90%) delete mode 100644 src/lib/litegraph/src/infrastructure/NodeIdSpaceExhaustedError.ts diff --git a/eslint-suppressions.json b/eslint-suppressions.json index d4e305a77ff..519df1b3f6c 100644 --- a/eslint-suppressions.json +++ b/eslint-suppressions.json @@ -184,11 +184,6 @@ "count": 1 } }, - "src/lib/litegraph/src/subgraph/subgraphDeduplication.ts": { - "comfy/no-new-error-throw": { - "count": 1 - } - }, "src/platform/assets/services/assetService.ts": { "comfy/no-new-error-throw": { "count": 21 diff --git a/src/composables/graph/useErrorClearingHooks.test.ts b/src/composables/graph/useErrorClearingHooks.test.ts index d1366892509..d7936c1fb51 100644 --- a/src/composables/graph/useErrorClearingHooks.test.ts +++ b/src/composables/graph/useErrorClearingHooks.test.ts @@ -27,8 +27,6 @@ import { useMissingNodesErrorStore } from '@/platform/nodeReplacement/missingNod import { app } from '@/scripts/app' import { ChangeTracker } from '@/scripts/changeTracker' import { useExecutionErrorStore } from '@/stores/executionErrorStore' -import { useExecutionStore } from '@/stores/executionStore' -import { useNodeOutputStore } from '@/stores/nodeOutputStore' import { createNodeExecutionId } from '@/types/nodeIdentification' import { toNodeId } from '@/types/nodeId' import { seedRequiredInputMissingNodeError } from '@/utils/__tests__/executionErrorTestUtils' @@ -445,37 +443,6 @@ describe('installErrorClearingHooks lifecycle', () => { expect(node.onWidgetChanged).toBe(originalOnWidgetChanged) }) - it('clears execution progress and cached outputs for a removed node, so a later mint of the recycled ID does not inherit them', () => { - const graph = new LGraph() - const node = new LGraphNode('test') - graph.add(node) - installErrorClearingHooks(graph) - - const executionStore = useExecutionStore() - const nodeOutputStore = useNodeOutputStore() - executionStore.nodeProgressStates = { - [String(node.id)]: { - display_node_id: node.id, - node_id: node.id, - prompt_id: 'test-prompt', - value: 1, - max: 2, - state: 'running' - } - } - nodeOutputStore.setNodeOutputs(node, 'output.png') - expect(nodeOutputStore.getNodeOutputs(node)).toBeDefined() - - graph.remove(node) - - expect(executionStore.nodeProgressStates[String(node.id)]).toBeUndefined() - - const replacement = new LGraphNode('test') - replacement.id = node.id - graph.add(replacement) - expect(nodeOutputStore.getNodeOutputs(replacement)).toBeUndefined() - }) - it('does not double-wrap callbacks when installErrorClearingHooks is called twice', () => { const graph = new LGraph() const node = new LGraphNode('test') diff --git a/src/composables/graph/useErrorClearingHooks.ts b/src/composables/graph/useErrorClearingHooks.ts index 5c26f649e72..a6df91ef8f0 100644 --- a/src/composables/graph/useErrorClearingHooks.ts +++ b/src/composables/graph/useErrorClearingHooks.ts @@ -41,8 +41,6 @@ import { useNodeReplacementStore } from '@/platform/nodeReplacement/nodeReplacem import { getCnrIdFromNode } from '@/platform/nodeReplacement/cnrIdUtil' import { app } from '@/scripts/app' import { useExecutionErrorStore } from '@/stores/executionErrorStore' -import { useExecutionStore } from '@/stores/executionStore' -import { useNodeOutputStore } from '@/stores/nodeOutputStore' import { toNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' import { useModelToNodeStore } from '@/stores/modelToNodeStore' @@ -625,8 +623,6 @@ export function installErrorClearingHooks(graph: LGraph): () => void { if (!successor) { const execId = getRemovedNodeExecutionId(graph, node.id) removeNodeErrors(node, execId) - useExecutionStore().clearNodeProgressState(String(node.id)) - useNodeOutputStore().removeNodeOutputsForNode(node) } scheduleDropOutOfScopeMissingMedia() restoreNodeHooksRecursive(node) 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 9ff092b7fe0..ba77ca37172 100644 --- a/src/composables/usePaste.test.ts +++ b/src/composables/usePaste.test.ts @@ -1,5 +1,4 @@ import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' -import { useToastStore } from '@/platform/updates/common/toastStore' import { useWorkspaceStore } from '@/stores/workspaceStore' import { afterEach, @@ -19,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 { @@ -98,13 +98,7 @@ beforeEach(() => { mockCanvasStore.canvas = mockCanvas }) -vi.mock(import('@/scripts/app'), () => ({ - app: { - nodeOutputs: {}, - nodePreviewImages: {}, - loadGraphData: vi.fn() - } -})) +vi.mock(import('@/scripts/app')) vi.mock(import('@/utils/litegraphUtil'), { spy: true }) @@ -621,13 +615,16 @@ describe('usePaste', () => { expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() }) - const validClipboardData = { - nodes: [{ id: 1, type: 'KSampler', pos: [0, 0] }] - } - it('should handle clipboard items with metadata', async () => { - const encoded = btoa(JSON.stringify(validClipboardData)) - const html = `
` + const data = { + nodes: [], + groups: [], + reroutes: [], + links: [], + subgraphs: [] + } + const encoded = btoa(JSON.stringify(data)) + const html = `
` usePaste() @@ -639,79 +636,61 @@ describe('usePaste', () => { await vi.waitFor(() => { expect(mockCanvas._deserializeItems).toHaveBeenCalledWith( - validClipboardData, + data, expect.any(Object) ) }) }) - it('should surface a deserialization error as a toast instead of falling back to legacy paste', async () => { - const deserializeError = new Error('Node ID space exhausted') - vi.mocked(mockCanvas._deserializeItems).mockImplementation(() => { - throw deserializeError - }) - - usePaste() - - const encoded = btoa(JSON.stringify(validClipboardData)) - const html = `
` - - 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).toHaveBeenCalled() - expect(useToastStore().add).toHaveBeenCalledWith( - expect.objectContaining({ severity: 'error' }) - ) - expect(mockCanvas.pasteFromClipboard).not.toHaveBeenCalled() - }) - }) - - it('falls through to legacy paste when the data-metadata payload is not clipboard-shaped', async () => { - // A third-party page whose HTML coincidentally contains a - // base64-ish `data-metadata` attribute that happens to decode to - // unrelated, valid JSON — must not be treated as a graph payload. - const unrelatedPayload = { foo: 'bar' } - const encoded = btoa(JSON.stringify(unrelatedPayload)) + it('should ignore metadata from other applications', async () => { + const encoded = btoa(JSON.stringify({ test: 'data' })) const html = `
` usePaste() const dataTransfer = new DataTransfer() dataTransfer.setData('text/html', html) - dataTransfer.setData('text/plain', 'the real copied text') + dataTransfer.setData('text/plain', 'some text') const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) document.dispatchEvent(event) await vi.waitFor(() => { + expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() }) - expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() }) - it('falls through to legacy paste when the decoded metadata is malformed (e.g. base64-encoded null)', async () => { - const encoded = btoa(JSON.stringify(null)) - const html = `
` + it('should toast a deserialization error without falling back', async () => { + const deserializeError = new Error('ID space exhausted') + vi.mocked(mockCanvas._deserializeItems).mockImplementation(() => { + throw deserializeError + }) + const data = { + nodes: [], + groups: [], + reroutes: [], + links: [], + subgraphs: [] + } + const encoded = btoa(JSON.stringify(data)) + const html = `
` usePaste() const dataTransfer = new DataTransfer() dataTransfer.setData('text/html', html) - dataTransfer.setData('text/plain', 'the real copied text') + dataTransfer.setData('text/plain', 'some text') const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) document.dispatchEvent(event) await vi.waitFor(() => { - expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + expect(useToastStore().add).toHaveBeenCalledWith( + expect.objectContaining({ severity: 'error' }) + ) + expect(mockCanvas.pasteFromClipboard).not.toHaveBeenCalled() }) - expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() }) it('should skip node metadata paste when a media node is selected', async () => { @@ -727,7 +706,7 @@ describe('usePaste', () => { const nodeData = { nodes: [{ type: 'KSampler' }] } const encoded = btoa(JSON.stringify(nodeData)) - const html = `
` + const html = `
` const dataTransfer = new DataTransfer() dataTransfer.setData('text/html', html) diff --git a/src/composables/usePaste.ts b/src/composables/usePaste.ts index 791182e5b03..159dd617ccb 100644 --- a/src/composables/usePaste.ts +++ b/src/composables/usePaste.ts @@ -1,5 +1,4 @@ import { useEventListener } from '@vueuse/core' -import { z } from 'zod' import { useErrorHandling } from '@/composables/useErrorHandling' import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' @@ -16,66 +15,6 @@ import { } from '@/utils/litegraphUtil' import { shouldIgnoreCopyPaste } from '@/workbench/eventHelpers' -const zClipboardNodeId = z.union([z.number(), z.string()]) -const zClipboardPos = z.array(z.number()).min(2) - -const zClipboardNode = z - .object({ - id: zClipboardNodeId, - type: z.string(), - pos: zClipboardPos - }) - .passthrough() - -const zClipboardGroup = z - .object({ - bounding: z.array(z.number()).min(4) - }) - .passthrough() - -const zClipboardReroute = z - .object({ - id: z.number(), - pos: zClipboardPos - }) - .passthrough() - -const zClipboardLink = z - .object({ - id: z.number(), - origin_id: zClipboardNodeId, - target_id: zClipboardNodeId - }) - .passthrough() - -const zClipboardSubgraph = z.object({ id: z.string() }).passthrough() - -/** - * The subset of `ClipboardItems`' shape `_deserializeItems` actually - * dereferences (`item.pos[0]`, `group.bounding[0]`, node/link ids). Rejects a - * decoded `data-metadata` payload that merely happens to be valid JSON — - * copied from a third-party page whose HTML coincidentally matches the - * base64-ish attribute pattern — as well as a well-formed but empty object, - * which carries none of the five keys below. - */ -const zClipboardItems = z - .object({ - nodes: z.array(zClipboardNode).optional(), - groups: z.array(zClipboardGroup).optional(), - reroutes: z.array(zClipboardReroute).optional(), - links: z.array(zClipboardLink).optional(), - subgraphs: z.array(zClipboardSubgraph).optional() - }) - .passthrough() - .refine( - (value) => - Array.isArray(value.nodes) || - Array.isArray(value.groups) || - Array.isArray(value.reroutes) || - Array.isArray(value.links) || - Array.isArray(value.subgraphs) - ) - export function cloneDataTransfer(original: DataTransfer): DataTransfer { const persistent = new DataTransfer() @@ -105,28 +44,20 @@ export function cloneDataTransfer(original: DataTransfer): DataTransfer { function pasteClipboardItems(data: DataTransfer): boolean { const rawData = data.getData('text/html') - const match = rawData.match(/data-metadata="([A-Za-z0-9+/=]+)"/)?.[1] + const match = rawData.match(/data-comfy-metadata="([A-Za-z0-9+/=]+)"/)?.[1] if (!match) return false - let decoded: unknown + let parsed: ClipboardItems 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) - decoded = JSON.parse(decodedData) + parsed = JSON.parse(decodedData) } catch (err) { - // Not a valid metadata payload — other paste strategies may still apply. console.error(err) return false } - if (!zClipboardItems.safeParse(decoded).success) return false - const parsed = decoded as ClipboardItems - - // A real deserialization/graph-mutation failure (e.g. node ID space - // exhaustion) is a genuine paste error, not "no valid metadata payload" — - // it must not fall through to the other paste strategies below. try { useCanvasStore().getCanvas()._deserializeItems(parsed, {}) } catch (err) { diff --git a/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts b/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts index c1f25408a87..445deb07005 100644 --- a/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts +++ b/src/lib/litegraph/src/LGraph.linkPresentationTransfer.test.ts @@ -507,7 +507,6 @@ describe('link presentation transfer across recreation flows', () => { connector.moveInputLink(graph, oldTarget, oldTarget.inputs[0]) connector.dropOnReroute(reroute, createMockCanvasPointerEvent(100, 100)) - const retargetedIds = new Set() for (const target of [first, second]) { const link = target.getInputLink(0) if (!link) throw new Error('Expected retargeted link') @@ -516,38 +515,8 @@ describe('link presentation transfer across recreation flows', () => { hidden: true, label: 'Fan out' }) - retargetedIds.add(Number(link.id)) } - // `original.id` may now be a recycled ID reused by one of the - // retargeted links above, which legitimately carries the presentation. - if (!retargetedIds.has(Number(original.id))) { - expect(store.getPresentation(scope, original.id)).toBeUndefined() - } - }) - - it('clears stale presentation when a released link ID is recycled onto an unrelated link', () => { - const graph = createTestRootGraph() - const origin = createTestNode(graph, [], ['number']) - const target = createTestNode(graph, ['number']) - const link = origin.connect(0, target, 0) - if (!link) throw new Error('Failed to connect removed test link') - const scope = graphScopeOf(graph) - useLinkPresentationStore().patch(scope, link.id, { - hidden: true, - label: 'Stale' - }) - - graph.removeLink(link.id) - - const otherOrigin = createTestNode(graph, [], ['number']) - const otherTarget = createTestNode(graph, ['number']) - const recycled = otherOrigin.connect(0, otherTarget, 0) - if (!recycled) throw new Error('Failed to connect recycling test link') - - expect(recycled.id).toBe(link.id) - expect( - useLinkPresentationStore().getPresentation(scope, recycled.id) - ).toBeUndefined() + expect(store.getPresentation(scope, original.id)).toBeUndefined() }) it('preserves each ordinary reroute presentation when moving its source', () => { diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index 3d99d6057eb..233e23778dc 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -27,7 +27,6 @@ import { isRootGraphDocBound, registerDocBoundRootGraphProbe } from '@/lib/litegraph/src/docBoundGraphs' -import { MAX_ID } from '@/lib/litegraph/src/idAllocation' import type { UUID } from '@/utils/uuid' import { createUuidv4, zeroUuid } from '@/utils/uuid' import { useEntityIdStore } from '@/stores/entityIdStore' @@ -58,7 +57,7 @@ import { } from './__fixtures__/duplicateLinks' import { duplicateSubgraphNodeIds } from './__fixtures__/duplicateSubgraphNodeIds' import { nestedSubgraphProxyWidgets } from './__fixtures__/nestedSubgraphProxyWidgets' -import { nodeIdSpaceExhausted } from './__fixtures__/nodeIdSpaceExhausted' +import { nodeIdsAtFormerLimit } from './__fixtures__/nodeIdsAtFormerLimit' import { uniqueSubgraphNodeIds } from './__fixtures__/uniqueSubgraphNodeIds' import { test } from './__fixtures__/testExtensions' @@ -1172,26 +1171,6 @@ describe('node:before-removed event', () => { expect(fired).not.toHaveBeenCalled() }) - it('does not release a group ID on a redundant remove() call, even after a new group has reused it', () => { - const graph = new LGraph() - const first = new LGraphGroup('first') - graph.add(first) - const firstId = first.id - - graph.remove(first) - - const second = new LGraphGroup('second') - graph.add(second) - expect(second.id).toBe(firstId) - - // Redundant: `first` is already detached, not in `graph.groups`. - graph.remove(first) - - const third = new LGraphGroup('third') - graph.add(third) - expect(third.id).not.toBe(second.id) - }) - it('does not fire node:before-removed when ignore_remove is set', () => { const graph = new LGraph() const node = new LGraphNode('test') @@ -1845,85 +1824,6 @@ describe('Shared LGraphState', () => { }) }) -describe('ID recycling on removal', () => { - it('reuses a removed node id for the next node minted', () => { - const graph = new LGraph() - const first = new DummyNode() - graph.add(first) - const removedId = first.id - - graph.remove(first) - - const second = new DummyNode() - graph.add(second) - - expect(second.id).toBe(removedId) - }) - - it('reuses a removed group id for the next group minted', () => { - const graph = new LGraph() - const first = new LGraphGroup('first') - graph.add(first) - const removedId = first.id - - graph.remove(first) - - const second = new LGraphGroup('second') - graph.add(second) - - expect(second.id).toBe(removedId) - }) - - it('reuses a removed link id for the next link minted', () => { - const graph = new LGraph() - const source = new LGraphNode('source') - const target = new LGraphNode('target') - source.addOutput('out', '*') - target.addInput('in', '*') - target.addInput('in2', '*') - graph.add(source) - graph.add(target) - - const firstLink = source.connect(0, target, 0)! - const removedId = firstLink.id - graph.removeLink(firstLink.id) - - const secondLink = source.connect(0, target, 1)! - - expect(secondLink.id).toBe(removedId) - }) - - it('reuses a removed reroute id for the next reroute minted', () => { - const graph = new LGraph() - const first = graph.setReroute({ pos: [0, 0], linkIds: [] })! - const removedId = first.id - - graph.removeReroute(first.id) - - const second = graph.setReroute({ pos: [0, 0], linkIds: [] })! - - expect(second.id).toBe(removedId) - }) - - it('does not recycle a node id kept alive by a successor', () => { - const graph = new LGraph() - const node = new LGraphNode('test') - graph.add(node) - const originalId = node.id - const successor = new LGraphNode('test') - successor.id = node.id - graph._nodes.push(successor) - graph._nodes_by_id[node.id] = successor - - graph.remove(node, { preserveCanonicalState: true }) - - const nextNode = new DummyNode() - graph.add(nextNode) - - expect(nextNode.id).not.toBe(originalId) - }) -}) - describe('persisted duplicate links', () => { const onConnectionsChange = vi.fn>() @@ -2440,45 +2340,19 @@ describe('deduplicateSubgraphNodeIds (via configure)', () => { }) }) - it('throws when node ID space is exhausted', () => { - expect(() => { - const graph = new LGraph() - graph.configure(structuredClone(nodeIdSpaceExhausted)) - }).toThrow('Node ID space exhausted') - }) - - it('does not let observing an above-MAX_ID node ID cause a later mint to collide with it', () => { - const graph = new LGraph() - const existing = new LGraphNode('test') - existing.id = toNodeId(MAX_ID + 1) - graph.add(existing) - - expect(() => graph.add(new LGraphNode('test'))).toThrow( - 'Node ID space exhausted' - ) - expect(graph.getNodeById(toNodeId(MAX_ID + 1))).toBe(existing) - expect(graph.nodes).toHaveLength(1) - }) - - it('asSerialisable() projects only the four counters, not the live free-ID pools', () => { + it('remaps duplicate node IDs above the former fixed limit', () => { const graph = new LGraph() - const node = new LGraphNode('test') - graph.add(node) - graph.remove(node) - - const serialised = graph.asSerialisable() - expect(serialised.state).toEqual({ - lastGroupId: 0, - lastNodeId: 1, - lastLinkId: 0, - lastRerouteId: 0 - }) - expect(JSON.stringify(serialised.state)).not.toContain('freeNodeIds') + graph.configure(structuredClone(nodeIdsAtFormerLimit)) - // The returned snapshot must not alias the live allocator state. - graph.state.freeNodeIds.add(999) - expect(serialised.state).not.toHaveProperty('freeNodeIds') + expect(nodeIdSet(graph, SUBGRAPH_B)).toEqual( + new Set([ + toNodeId(100_000_001), + toNodeId(100_000_002), + toNodeId(100_000_003) + ]) + ) + expect(graph.state.lastNodeId).toBe(100_000_003) }) it('is a no-op when subgraph node IDs are already unique', () => { diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 01e66eb07e2..6a82ea2a01e 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -45,7 +45,6 @@ import { isFloatingTopology } from '@/types/linkTopology' import { toRerouteId } from '@/types/rerouteId' import { graphScopeOf, toRootGraphId } from '@/types/graphScopeId' import { - counterSnapshot, createLGraphState, mintGroupId, mintLinkId, @@ -54,11 +53,7 @@ import { observeGroupId, observeLinkId, observeNodeId, - observeRerouteId, - releaseGroupId, - releaseLinkId, - releaseNodeId, - releaseRerouteId + observeRerouteId } from './idAllocation' import type { LGraphState, NodeIdMintMode } from './idAllocation' import { isRootGraphDocBound } from './docBoundGraphs' @@ -1475,7 +1470,6 @@ export class LGraph const index = this._groups.indexOf(node) if (index != -1) { this._groups.splice(index, 1) - releaseGroupId(this.state, node.id) } detachGroupLayout(node) node.graph = undefined @@ -1551,10 +1545,7 @@ export class LGraph // callback node.onRemoved?.() - if (!successor) { - clearNodeOwnedStoreState(node) - releaseNodeId(this.state, node.id) - } + if (!successor) clearNodeOwnedStoreState(node) const order = node.order if (!successor) { @@ -1883,7 +1874,6 @@ export class LGraph removeFloatingLink(link: LLink): void { if (this.floatingLinks.get(link.id) !== link) return unregisterLinkTopology(link) - releaseLinkId(this.state, link.id) const reroutes = LLink.getReroutes(this, link) for (const reroute of reroutes) { @@ -1915,7 +1905,6 @@ export class LGraph unregisterLinkTopology(link) layoutStore.deleteLinkLayout(linkId) this.getNodeById(link.target_id)?.updateComputedDisabled() - releaseLinkId(this.state, linkId) return true } @@ -1967,7 +1956,6 @@ export class LGraph this.reroutesInternal.delete(id) unregisterRerouteChain(reroute) detachRerouteLayout(reroute) - releaseRerouteId(this.state, id) } /** @@ -2971,7 +2959,7 @@ export class LGraph revision, version: LGraph.serialisedSchemaVersion, config, - state: counterSnapshot(state), + state, groups, nodes, ...topology, @@ -3720,7 +3708,7 @@ export class Subgraph return { id: this.id, version: LGraph.serialisedSchemaVersion, - state: counterSnapshot(this.state), + state: this.state, revision: this.revision, config: this.config, name: this.name, diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index 01a4836384e..d5c95657b81 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -28,7 +28,6 @@ import { createUuidv4 } from '@/lib/litegraph/src/litegraph' import { remapClipboardSubgraphNodeIds } from '@/lib/litegraph/src/LGraphCanvas' -import { MAX_ID } from '@/lib/litegraph/src/idAllocation' import { toNodeId } from '@/types/nodeId' import type { ClipboardItems, @@ -66,6 +65,32 @@ function createSerialisedNode( } } +function createSubgraphClipboardItems(interiorNodeId: number): ClipboardItems { + return { + nodes: [], + groups: [], + reroutes: [], + links: [], + subgraphs: [ + { + id: createUuidv4(), + version: 1, + revision: 0, + state: { + lastNodeId: 0, + lastLinkId: 0, + lastGroupId: 0, + lastRerouteId: 0 + }, + name: 'Pasted Subgraph', + inputNode: { id: SUBGRAPH_INPUT_ID, bounding: [0, 0, 10, 10] }, + outputNode: { id: SUBGRAPH_OUTPUT_ID, bounding: [0, 0, 10, 10] }, + nodes: [createSerialisedNode(interiorNodeId, 'test/node')] + } + ] + } +} + describe('remapClipboardSubgraphNodeIds', () => { it('remaps pasted subgraph interior IDs and proxyWidgets references', () => { const rootGraph = new LGraph() @@ -200,61 +225,37 @@ describe('remapClipboardSubgraphNodeIds', () => { ]) }) - function createExhaustedRootGraph(): LGraph { + it('remaps collisions above the former fixed limit', () => { const rootGraph = new LGraph() - rootGraph.state.lastNodeId = MAX_ID - const existingNode = new LGraphNode('existing') existingNode.id = toNodeId(1) rootGraph.add(existingNode) - return rootGraph - } + rootGraph.state.lastNodeId = 100_000_000 + const parsed = createSubgraphClipboardItems(1) - function createCollidingClipboardItems(): ClipboardItems { - const pastedSubgraph: ExportedSubgraph = { - id: createUuidv4(), - 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: [] - } - return { - nodes: [], - groups: [], - reroutes: [], - links: [], - subgraphs: [pastedSubgraph] - } - } - - it('throws instead of hanging when the node ID space is exhausted', () => { - const rootGraph = createExhaustedRootGraph() - const parsed = createCollidingClipboardItems() + remapClipboardSubgraphNodeIds(parsed, rootGraph) - expect(() => remapClipboardSubgraphNodeIds(parsed, rootGraph)).toThrow( - 'Node ID space exhausted' - ) + expect(parsed.subgraphs?.[0].nodes?.[0].id).toBe(100_000_001) + expect(rootGraph.state.lastNodeId).toBe(100_000_001) }) - it('leaves the graph ID state untouched when remapping throws', () => { - const rootGraph = createExhaustedRootGraph() - const snapshotLastNodeId = rootGraph.state.lastNodeId - const snapshotFreeNodeIds = new Set(rootGraph.state.freeNodeIds) - const parsed = createCollidingClipboardItems() - - expect(() => remapClipboardSubgraphNodeIds(parsed, rootGraph)).toThrow() + it('closes change tracking and preserves counters when remapping fails', () => { + const rootGraph = new LGraph() + const existingNode = new LGraphNode('existing') + existingNode.id = toNodeId(1) + rootGraph.add(existingNode) + rootGraph.state.lastNodeId = Number.MAX_SAFE_INTEGER + const canvas = createCanvas(rootGraph) + const afterGraphChange = vi.spyOn(rootGraph, 'afterChange') + const afterCanvasChange = vi.spyOn(canvas, 'emitAfterChange') + const parsed = createSubgraphClipboardItems(1) - expect(rootGraph.state.lastNodeId).toBe(snapshotLastNodeId) - expect(rootGraph.state.freeNodeIds).toEqual(snapshotFreeNodeIds) + expect(() => canvas._deserializeItems(parsed, {})).toThrow( + 'ID space exhausted' + ) + expect(rootGraph.state.lastNodeId).toBe(Number.MAX_SAFE_INTEGER) + expect(afterGraphChange).toHaveBeenCalledOnce() + expect(afterCanvasChange).toHaveBeenCalledOnce() }) }) @@ -269,64 +270,6 @@ function createCanvas(graph: LGraph): LGraphCanvas { return new LGraphCanvas(el, graph, { skip_render: true, skip_events: true }) } -describe('_deserializeItems change-tracking invariant', () => { - function createExhaustedRootGraph(): LGraph { - const rootGraph = new LGraph() - rootGraph.state.lastNodeId = MAX_ID - - const existingNode = new LGraphNode('existing') - existingNode.id = toNodeId(1) - rootGraph.add(existingNode) - return rootGraph - } - - function createCollidingClipboardItems(): ClipboardItems { - const pastedSubgraph: ExportedSubgraph = { - id: createUuidv4(), - 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: [] - } - return { - nodes: [], - groups: [], - reroutes: [], - links: [], - subgraphs: [pastedSubgraph] - } - } - - it('calls afterChange/emitAfterChange even when the paste throws partway through', () => { - const rootGraph = createExhaustedRootGraph() - const canvas = createCanvas(rootGraph) - const parsed = createCollidingClipboardItems() - - const beforeChangeSpy = vi.spyOn(rootGraph, 'beforeChange') - const afterChangeSpy = vi.spyOn(rootGraph, 'afterChange') - const emitBeforeChangeSpy = vi.spyOn(canvas, 'emitBeforeChange') - const emitAfterChangeSpy = vi.spyOn(canvas, 'emitAfterChange') - - expect(() => canvas._deserializeItems(parsed, {})).toThrow( - 'Node ID space exhausted' - ) - - expect(beforeChangeSpy).toHaveBeenCalledTimes(1) - expect(emitBeforeChangeSpy).toHaveBeenCalledTimes(1) - expect(afterChangeSpy).toHaveBeenCalledTimes(1) - expect(emitAfterChangeSpy).toHaveBeenCalledTimes(1) - }) -}) - describe('link presentation transfer across recreation flows', () => { it.for([ { diff --git a/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts b/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts index 763c0a6f008..b3eedc1f92b 100644 --- a/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts @@ -206,32 +206,6 @@ function terminalState(f: Fixture) { } } -/** - * Recycled floating-link IDs are history-dependent, so raw values can differ - * between the two delete orders even when the underlying structure agrees. - * Replacing each ID with its rank among this run's own sorted floating-link - * IDs still proves the real invariant — that a reroute's `floatingLinkIds` - * entry resolves to one of the graph's *actual* floating links, and to the - * same relative one in both orders — without asserting the exact recycled - * value. Reducing to bare counts, by contrast, would equally accept a - * reroute that references a link the graph doesn't have, or the wrong one - * among several, as long as the totals matched. - */ -function withStableFloatingLinkIdentities( - state: ReturnType -) { - const sortedIds = [...state.remainingFloatingLinks].sort((a, b) => a - b) - const rankOf = (id: LinkId) => sortedIds.indexOf(id) - return { - ...state, - remainingFloatingLinks: sortedIds.map((_, index) => index), - remainingReroutes: state.remainingReroutes.map((reroute) => ({ - linkIds: reroute.linkIds, - floatingLinkIds: reroute.floatingLinkIds.map(rankOf) - })) - } -} - describe('subgraph copy/paste then delete in both orders', () => { beforeEach(() => { LiteGraph.registerNodeType(INTERIOR_TYPE, InteriorNode) @@ -385,11 +359,7 @@ describe('subgraph copy/paste then delete in both orders', () => { copyFirst.rootGraph.remove(copyFirst.copy) copyFirst.rootGraph.remove(copyFirst.original) - expect( - withStableFloatingLinkIdentities(terminalState(copyFirst)) - ).toStrictEqual( - withStableFloatingLinkIdentities(terminalState(originalFirst)) - ) + expect(terminalState(copyFirst)).toStrictEqual(terminalState(originalFirst)) // That shared state is: both definitions released, both instances gone, // every real link gone — and the reroute survives on a *real* floating diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 81561251919..435e9e142ae 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -46,6 +46,7 @@ import type { AnimationOptions } from './DragAndScale' import { cloneLGraphState, commitLGraphState, + findNextAvailableId, mintNodeId, observeNodeId } from './idAllocation' @@ -55,7 +56,6 @@ import type { SlotTypeDefaultNodeOpts } from './LiteGraphGlobal' import { LGraphNode } from './LGraphNode' import type { NodeProperty } from './LGraphNode' import { detachSerialisedLinks } from './linkDeduplication' -import { findNextAvailableId } from './subgraph/subgraphDeduplication' import { parseNodeId, serializeNodeId, toNodeId } from '@/types/nodeId' import type { SerializedNodeId } from '@/types/nodeId' import { LLink, slotFloatingLinks } from './LLink' diff --git a/src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts b/src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts similarity index 90% rename from src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts rename to src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts index b099bbf4abe..b95b110d5a8 100644 --- a/src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts +++ b/src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts @@ -4,15 +4,7 @@ import { } from '@/lib/litegraph/src/constants' import type { SerialisableGraph } from '@/lib/litegraph/src/types/serialisation' -/** - * Workflow where lastNodeId is near the MAX_NODE_ID ceiling (100_000_000) - * and root node 100_000_000 reserves the only remaining candidate ID. - * - * Both subgraph definitions share node IDs [3, 8, 37]. When SubgraphB's - * duplicates need remapping, candidate 100_000_000 is already reserved, - * so the next candidate (100_000_001) exceeds MAX_NODE_ID and must throw. - */ -export const nodeIdSpaceExhausted = { +export const nodeIdsAtFormerLimit = { id: 'cccccccc-cccc-4ccc-8ccc-cccccccccccc', version: 1, revision: 0, diff --git a/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts b/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts index 422bd999222..04ad0e22e42 100644 --- a/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts +++ b/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts @@ -221,6 +221,8 @@ describe('LinkConnector Integration', () => { describe('Moving input links', () => { test('Should move input links', ({ graph, connector }) => { + const nextLinkId = graph.last_link_id + 1 + const hasInputNode = graph.getNodeById(toNodeId(2))! const disconnectedNode = graph.getNodeById(toNodeId(9))! @@ -248,9 +250,7 @@ describe('LinkConnector Integration', () => { expect(connector.renderLinks.length).toBe(0) expect(connector.inputLinks.length).toBe(0) - const newLinkId = disconnectedNode.inputs[0].link - expect(newLinkId).not.toBeNull() - expect(graph.links.get(newLinkId!)).toBeInstanceOf(LLink) + expect(disconnectedNode.inputs[0].link).toBe(nextLinkId) expect(hasInputNode.inputs[0].link).toBeNull() const reroutesAfter = LLink.getReroutes( diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 4ecc776d46e..83fc862bd09 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -2,10 +2,10 @@ import { describe, expect, it } from 'vitest' import { AGENT_RESERVED_BIT, - MAX_ID, cloneLGraphState, commitLGraphState, createLGraphState, + findNextAvailableId, isReservedBitRangeNodeId, matchesReservedBitConvention, mintGroupId, @@ -15,11 +15,7 @@ import { observeGroupId, observeLinkId, observeNodeId, - observeRerouteId, - releaseGroupId, - releaseLinkId, - releaseNodeId, - releaseRerouteId + observeRerouteId } from '@/lib/litegraph/src/idAllocation' import { toGroupId } from '@/types/groupId' import { toLinkId } from '@/types/linkId' @@ -64,11 +60,7 @@ describe('idAllocation', () => { lastGroupId: 5, lastNodeId: 4, lastLinkId: 6, - lastRerouteId: 7, - freeNodeIds: new Set(), - freeGroupIds: new Set(), - freeLinkIds: new Set(), - freeRerouteIds: new Set() + lastRerouteId: 7 }) }) @@ -81,6 +73,99 @@ describe('idAllocation', () => { expect(state.lastNodeId).toBe(12) }) + it.for([ + { + name: 'node', + observe: (state: ReturnType, value: number) => + observeNodeId(state, toNodeId(value)), + mint: mintNodeId + }, + { + name: 'group', + observe: (state: ReturnType, value: number) => + observeGroupId(state, toGroupId(value)), + mint: mintGroupId + }, + { + name: 'link', + observe: (state: ReturnType, value: number) => + observeLinkId(state, toLinkId(value)), + mint: mintLinkId + }, + { + name: 'reroute', + observe: (state: ReturnType, value: number) => + observeRerouteId(state, toRerouteId(value)), + mint: mintRerouteId + } + ])( + '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: mintNodeId + }, + { + 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 + } + ])('rejects unsafe $name allocation', ({ setCounter, mint }) => { + const state = createLGraphState() + setCounter(state) + + expect(() => mint(state)).toThrow('ID space exhausted') + }) + + it('bounds collision searches by the number of reservations', () => { + let calls = 0 + + expect(() => + findNextAvailableId(new Set([1, 2]), () => { + calls++ + return 1 + }) + ).toThrow('ID space exhausted') + expect(calls).toBe(3) + }) + + it('commits speculative counter updates only when requested', () => { + const state = createLGraphState() + const workingState = cloneLGraphState(state) + mintNodeId(workingState) + + expect(state.lastNodeId).toBe(0) + commitLGraphState(state, workingState) + expect(state.lastNodeId).toBe(1) + }) + describe('isReservedBitRangeNodeId', () => { it.for([ { id: toNodeId(AGENT_RESERVED_BIT.toString()), name: 'the agent floor' }, @@ -141,237 +226,4 @@ describe('idAllocation', () => { expect(matchesReservedBitConvention(id)).toBe(false) }) }) - - describe('id recycling', () => { - it.for([ - { - release: (state: ReturnType) => - releaseNodeId(state, toNodeId(2)), - mint: mintNodeId, - expected: ['1', '2', '3', '2', '4'], - name: 'node' - }, - { - release: (state: ReturnType) => - releaseGroupId(state, toGroupId(2)), - mint: mintGroupId, - expected: [1, 2, 3, 2, 4], - name: 'group' - }, - { - release: (state: ReturnType) => - releaseLinkId(state, toLinkId(2)), - mint: mintLinkId, - expected: [1, 2, 3, 2, 4], - name: 'link' - }, - { - release: (state: ReturnType) => - releaseRerouteId(state, toRerouteId(2)), - mint: mintRerouteId, - expected: [1, 2, 3, 2, 4], - name: 'reroute' - } - ])( - 'reuses a freed $name ID before minting past the counter', - ({ release, mint, expected }) => { - const state = createLGraphState() - expect([mint(state), mint(state), mint(state)]).toEqual( - expected.slice(0, 3) - ) - - release(state) - expect(mint(state)).toEqual(expected[3]) - expect(mint(state)).toEqual(expected[4]) - } - ) - - it('ignores releasing a nonnumeric legacy node ID', () => { - const state = createLGraphState() - releaseNodeId(state, toNodeId('named')) - expect(state.freeNodeIds.size).toBe(0) - }) - - it('never recycles an ID in the agent/CRDT reserved-bit range', () => { - const state = createLGraphState() - releaseNodeId(state, toNodeId(AGENT_RESERVED_BIT.toString())) - expect(state.freeNodeIds.size).toBe(0) - }) - - it('drops a freed ID from the pool once it is observed as in use again', () => { - const state = createLGraphState() - mintNodeId(state) - releaseNodeId(state, toNodeId(1)) - expect(state.freeNodeIds.has(1)).toBe(true) - - observeNodeId(state, toNodeId(1)) - expect(state.freeNodeIds.has(1)).toBe(false) - }) - }) - - describe('counter clamping', () => { - it.for([ - { - observe: (state: ReturnType) => - observeNodeId(state, toNodeId(MAX_ID + 1)), - mint: (state: ReturnType) => - mintNodeId(state), - read: (state: ReturnType) => state.lastNodeId, - name: 'node' - }, - { - observe: (state: ReturnType) => - observeGroupId(state, toGroupId(MAX_ID + 1)), - mint: (state: ReturnType) => - mintGroupId(state), - read: (state: ReturnType) => - state.lastGroupId, - name: 'group' - }, - { - observe: (state: ReturnType) => - observeLinkId(state, toLinkId(MAX_ID + 1)), - mint: (state: ReturnType) => - mintLinkId(state), - read: (state: ReturnType) => - Number(state.lastLinkId), - name: 'link' - }, - { - observe: (state: ReturnType) => - observeRerouteId(state, toRerouteId(MAX_ID + 1)), - mint: (state: ReturnType) => - mintRerouteId(state), - read: (state: ReturnType) => - Number(state.lastRerouteId), - name: 'reroute' - } - ])( - 'records an incoming $name counter above MAX_ID unclamped, but refuses to mint past it', - ({ observe, mint, read }) => { - const state = createLGraphState() - observe(state) - expect(read(state)).toBe(MAX_ID + 1) - expect(() => mint(state)).toThrow('Node ID space exhausted') - } - ) - - it('rejects a negative counter candidate outright', () => { - const state = createLGraphState() - observeNodeId(state, toNodeId(-5)) - expect(state.lastNodeId).toBe(0) - }) - - it('does not let an observed node ID in the agent/CRDT reserved-bit range drag lastNodeId up', () => { - const state = createLGraphState() - observeNodeId(state, toNodeId(AGENT_RESERVED_BIT.toString())) - expect(state.lastNodeId).toBe(0) - expect(mintNodeId(state)).toBe('1') - }) - }) - - describe('mint-side MAX_ID enforcement', () => { - it.for([ - { - mint: mintNodeId, - seed: (state: ReturnType) => { - state.lastNodeId = MAX_ID - }, - name: 'node' - }, - { - mint: mintGroupId, - seed: (state: ReturnType) => { - state.lastGroupId = MAX_ID - }, - name: 'group' - }, - { - mint: mintLinkId, - seed: (state: ReturnType) => { - state.lastLinkId = toLinkId(MAX_ID) - }, - name: 'link' - }, - { - mint: mintRerouteId, - seed: (state: ReturnType) => { - state.lastRerouteId = toRerouteId(MAX_ID) - }, - name: 'reroute' - } - ])('refuses to mint a $name ID past MAX_ID', ({ mint, seed }) => { - const state = createLGraphState() - seed(state) - expect(() => mint(state)).toThrow('Node ID space exhausted') - }) - }) - - describe('release-side range enforcement', () => { - it.for([ - { - release: (state: ReturnType) => - releaseNodeId(state, toNodeId(MAX_ID + 1)), - read: (state: ReturnType) => - state.freeNodeIds, - name: 'node' - }, - { - release: (state: ReturnType) => - releaseGroupId(state, toGroupId(MAX_ID + 1)), - read: (state: ReturnType) => - state.freeGroupIds, - name: 'group' - }, - { - release: (state: ReturnType) => - releaseLinkId(state, toLinkId(MAX_ID + 1)), - read: (state: ReturnType) => - state.freeLinkIds, - name: 'link' - }, - { - release: (state: ReturnType) => - releaseRerouteId(state, toRerouteId(MAX_ID + 1)), - read: (state: ReturnType) => - state.freeRerouteIds, - name: 'reroute' - } - ])( - 'never pools an above-MAX_ID $name ID for reuse', - ({ release, read }) => { - const state = createLGraphState() - release(state) - expect(read(state).size).toBe(0) - } - ) - }) - - describe('cloneLGraphState / commitLGraphState', () => { - it('leaves the original untouched until explicitly committed', () => { - const state = createLGraphState() - const working = cloneLGraphState(state) - - mintNodeId(working) - mintNodeId(working) - - expect(state.lastNodeId).toBe(0) - expect(working.lastNodeId).toBe(2) - - commitLGraphState(state, working) - expect(state.lastNodeId).toBe(2) - }) - - it('clones free-id sets so mutating the copy cannot affect the original', () => { - const state = createLGraphState() - mintNodeId(state) - releaseNodeId(state, toNodeId(1)) - - const working = cloneLGraphState(state) - mintNodeId(working) - - expect(state.freeNodeIds.has(1)).toBe(true) - expect(working.freeNodeIds.has(1)).toBe(false) - }) - }) }) diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index 9abac8168d7..abc33181234 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -1,4 +1,3 @@ -import { NodeIdSpaceExhaustedError } from './infrastructure/NodeIdSpaceExhaustedError' import { toGroupId } from '@/types/groupId' import type { GroupId } from '@/types/groupId' import { toLinkId } from '@/types/linkId' @@ -8,20 +7,19 @@ import type { NodeId } from '@/types/nodeId' import { toRerouteId } from '@/types/rerouteId' import type { RerouteId } from '@/types/rerouteId' -/** Hard ceiling shared by every counter — a true safety cap, not a working limit. */ -export const MAX_ID = 100_000_000 - export interface LGraphState { /** Counter, not an id — brand at the point a group is constructed. */ lastGroupId: number lastNodeId: number lastLinkId: LinkId lastRerouteId: RerouteId - /** IDs freed by a removed node/group/link/reroute, reused before minting past the counter. */ - freeNodeIds: Set - freeGroupIds: Set - freeLinkIds: Set - freeRerouteIds: Set +} + +class IdSpaceExhaustedError extends Error { + constructor() { + super('ID space exhausted') + this.name = 'IdSpaceExhaustedError' + } } export function createLGraphState(): LGraphState { @@ -29,79 +27,44 @@ export function createLGraphState(): LGraphState { lastGroupId: 0, lastNodeId: 0, lastLinkId: toLinkId(0), - lastRerouteId: toRerouteId(0), - freeNodeIds: new Set(), - freeGroupIds: new Set(), - freeLinkIds: new Set(), - freeRerouteIds: new Set() + lastRerouteId: toRerouteId(0) } } -/** - * A disposable copy of `state`, safe for an operation to mutate speculatively - * (minting, observing, recycling) and discard on failure via - * {@link commitLGraphState} only once it fully succeeds. - */ export function cloneLGraphState(state: LGraphState): LGraphState { - return { - lastGroupId: state.lastGroupId, - lastNodeId: state.lastNodeId, - lastLinkId: state.lastLinkId, - lastRerouteId: state.lastRerouteId, - freeNodeIds: new Set(state.freeNodeIds), - freeGroupIds: new Set(state.freeGroupIds), - freeLinkIds: new Set(state.freeLinkIds), - freeRerouteIds: new Set(state.freeRerouteIds) - } -} - -/** - * Projects only the four persisted counters off `state`, leaving its free-id - * pools behind. `asSerialisable()` uses this instead of returning `state` - * itself, so a saved workflow or clipboard payload never carries the live, - * mutable free-id `Set`s by reference — those are transient allocator - * bookkeeping, not part of the persisted schema. - */ -export function counterSnapshot(state: LGraphState): { - lastGroupId: number - lastNodeId: number - lastLinkId: number - lastRerouteId: number -} { - return { - lastGroupId: state.lastGroupId, - lastNodeId: state.lastNodeId, - lastLinkId: state.lastLinkId, - lastRerouteId: state.lastRerouteId - } + return { ...state } } -/** Copies every counter and free-id pool from `source` onto `target`, in place. */ export function commitLGraphState( target: LGraphState, source: LGraphState ): void { - target.lastGroupId = source.lastGroupId - target.lastNodeId = source.lastNodeId - target.lastLinkId = source.lastLinkId - target.lastRerouteId = source.lastRerouteId - target.freeNodeIds = source.freeNodeIds - target.freeGroupIds = source.freeGroupIds - target.freeLinkIds = source.freeLinkIds - target.freeRerouteIds = source.freeRerouteIds -} - -/** The next value from `freeIds`, removed from the set, or `undefined` when empty. */ -function takeFreeId(freeIds: Set): number | undefined { - const { done, value } = freeIds.values().next() - if (done) return undefined - freeIds.delete(value) - return value + Object.assign(target, source) } -/** Whether `value` is a well-formed counter candidate — a non-negative integer. */ -function isValidCounterCandidate(value: number): boolean { - return Number.isInteger(value) && value >= 0 +function advanceCounter(counter: number): number { + if ( + !Number.isSafeInteger(counter) || + counter < 0 || + counter === Number.MAX_SAFE_INTEGER + ) { + throw new IdSpaceExhaustedError() + } + return counter + 1 +} + +export function findNextAvailableId( + usedIds: Set, + advance: () => number +): number { + for (let attempts = 0; attempts <= usedIds.size; attempts++) { + const nextId = advance() + if (!Number.isSafeInteger(nextId) || nextId < 0) { + throw new IdSpaceExhaustedError() + } + if (!usedIds.has(nextId)) return nextId + } + throw new IdSpaceExhaustedError() } /** @@ -210,118 +173,54 @@ export function matchesReservedBitConvention(id: NodeId): boolean { return (big & AGENT_RESERVED_BIT) !== 0n || (big & CRDT_DISJOINT_FLOOR) !== 0n } -/** Advances a counter by one, throwing rather than minting past `MAX_ID`. */ -function advanceCounter(lastValue: number): number { - const next = lastValue + 1 - if (next > MAX_ID) throw new NodeIdSpaceExhaustedError() - return next -} - export function mintNodeId( state: LGraphState, mode: NodeIdMintMode = 'sequential' ): NodeId { - if (mode === 'crdt-disjoint') return mintCrdtDisjointNodeId() - const recycled = takeFreeId(state.freeNodeIds) - if (recycled !== undefined) return toNodeId(recycled) - state.lastNodeId = advanceCounter(state.lastNodeId) - return toNodeId(state.lastNodeId) + return mode === 'crdt-disjoint' + ? mintCrdtDisjointNodeId() + : toNodeId((state.lastNodeId = advanceCounter(state.lastNodeId))) } export function mintGroupId(state: LGraphState): GroupId { - const recycled = takeFreeId(state.freeGroupIds) - if (recycled !== undefined) return toGroupId(recycled) - state.lastGroupId = advanceCounter(state.lastGroupId) - return toGroupId(state.lastGroupId) + return toGroupId((state.lastGroupId = advanceCounter(state.lastGroupId))) } export function mintLinkId(state: LGraphState): LinkId { - const recycled = takeFreeId(state.freeLinkIds) - if (recycled !== undefined) return toLinkId(recycled) state.lastLinkId = toLinkId(advanceCounter(Number(state.lastLinkId))) return state.lastLinkId } export function mintRerouteId(state: LGraphState): RerouteId { - const recycled = takeFreeId(state.freeRerouteIds) - if (recycled !== undefined) return toRerouteId(recycled) state.lastRerouteId = toRerouteId(advanceCounter(Number(state.lastRerouteId))) return state.lastRerouteId } -/** - * Returns `id` to `state`'s free-node pool so a later mint reuses it before - * advancing `lastNodeId`. Ignored for a nonnumeric legacy id and for one in - * the agent/CRDT reserved-bit range (see {@link AGENT_RESERVED_BIT}), which - * this counter never mints into. - */ -export function releaseNodeId(state: LGraphState, id: NodeId): void { - const numeric = safeIntegerValueOf(id) - if (numeric === undefined || numeric <= 0 || numeric > MAX_ID) return - if (BigInt(numeric) >= AGENT_RESERVED_BIT) return - state.freeNodeIds.add(numeric) -} - -export function releaseGroupId(state: LGraphState, id: GroupId): void { - if (!Number.isInteger(id) || id <= 0 || id > MAX_ID) return - state.freeGroupIds.add(id) -} - -export function releaseLinkId(state: LGraphState, id: LinkId): void { - const numeric = Number(id) - if (!Number.isInteger(numeric) || numeric <= 0 || numeric > MAX_ID) return - state.freeLinkIds.add(numeric) -} - -export function releaseRerouteId(state: LGraphState, id: RerouteId): void { - const numeric = Number(id) - if (!Number.isInteger(numeric) || numeric <= 0 || numeric > MAX_ID) return - state.freeRerouteIds.add(numeric) -} - -/** - * Records that `id` is in use, raising `lastNodeId` to match when it is - * higher. Never clamped to `MAX_ID`: an observed id is proof an entity with - * that id already exists, so lying about the high-water mark would let a - * later mint hand out an id the observed entity already owns. An id in the - * agent/CRDT reserved-bit range (see {@link AGENT_RESERVED_BIT}) is ignored - * for this purpose the same way {@link releaseNodeId} ignores it for - * recycling — that range is a disjoint id space the sequential counter never - * mints into, so observing it must not drag `lastNodeId` up to match. - */ export function observeNodeId(state: LGraphState, id: NodeId): void { const numericId = Number(id) - state.freeNodeIds.delete(numericId) - if (!isValidCounterCandidate(numericId)) return - if (BigInt(numericId) >= AGENT_RESERVED_BIT) return - if (numericId > state.lastNodeId) state.lastNodeId = numericId + if ( + Number.isSafeInteger(numericId) && + numericId >= 0 && + numericId > state.lastNodeId + ) { + state.lastNodeId = numericId + } } export function observeGroupId(state: LGraphState, id: GroupId): void { - state.freeGroupIds.delete(id) - if (isValidCounterCandidate(id) && id > state.lastGroupId) { + if (Number.isSafeInteger(id) && id >= 0 && id > state.lastGroupId) { state.lastGroupId = id } } export function observeLinkId(state: LGraphState, id: LinkId): void { - const numericId = Number(id) - state.freeLinkIds.delete(numericId) - if ( - isValidCounterCandidate(numericId) && - numericId > Number(state.lastLinkId) - ) { - state.lastLinkId = toLinkId(numericId) + if (Number.isSafeInteger(id) && id >= 0 && id > state.lastLinkId) { + state.lastLinkId = id } } export function observeRerouteId(state: LGraphState, id: RerouteId): void { - const numericId = Number(id) - state.freeRerouteIds.delete(numericId) - if ( - isValidCounterCandidate(numericId) && - numericId > Number(state.lastRerouteId) - ) { - state.lastRerouteId = toRerouteId(numericId) + if (Number.isSafeInteger(id) && id >= 0 && id > state.lastRerouteId) { + state.lastRerouteId = id } } diff --git a/src/lib/litegraph/src/infrastructure/NodeIdSpaceExhaustedError.ts b/src/lib/litegraph/src/infrastructure/NodeIdSpaceExhaustedError.ts deleted file mode 100644 index a70777e677a..00000000000 --- a/src/lib/litegraph/src/infrastructure/NodeIdSpaceExhaustedError.ts +++ /dev/null @@ -1,6 +0,0 @@ -export class NodeIdSpaceExhaustedError extends Error { - constructor(message: string = 'Node ID space exhausted', cause?: Error) { - super(message, { cause }) - this.name = 'NodeIdSpaceExhaustedError' - } -} diff --git a/src/lib/litegraph/src/subgraph/Subgraph.test.ts b/src/lib/litegraph/src/subgraph/Subgraph.test.ts index 36101412369..8449ed31ff4 100644 --- a/src/lib/litegraph/src/subgraph/Subgraph.test.ts +++ b/src/lib/litegraph/src/subgraph/Subgraph.test.ts @@ -14,7 +14,6 @@ import { } from '@/lib/litegraph/src/constants' import type { LGraph } from '@/lib/litegraph/src/litegraph' import { createUuidv4, Subgraph } from '@/lib/litegraph/src/litegraph' -import { counterSnapshot } from '@/lib/litegraph/src/idAllocation' import { subgraphTest } from './__fixtures__/subgraphFixtures' import { assertSubgraphStructure, @@ -104,7 +103,7 @@ describe('Subgraph Construction', () => { expect(subgraph.rootGraph.subgraphs.has(clone.id)).toBe(false) expect(subgraph.asSerialisable()).toEqual({ ...sourceBeforeClone, - state: counterSnapshot(subgraph.state) + state: subgraph.state }) }) }) diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts index 6aebf5f8b4f..7102039afcd 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts @@ -10,7 +10,6 @@ import { toRerouteId } from '@/types/rerouteId' import { isUuidShapedSubgraphId } from '@/schemas/subgraphIdSchema' import type { LGraphState } from '../LGraph' -import { MAX_ID, createLGraphState } from '../idAllocation' import type { ExportedSubgraph, ISerialisedGroup, @@ -248,9 +247,12 @@ function chainedLink(id: number, parentId?: number): SerialisableLLink { } function freshState(lastRerouteId = 0): LGraphState { - const state = createLGraphState() - state.lastRerouteId = toRerouteId(lastRerouteId) - return state + return { + lastGroupId: 0, + lastNodeId: 0, + lastLinkId: toLinkId(0), + lastRerouteId: toRerouteId(lastRerouteId) + } } function group(id: number): ISerialisedGroup { @@ -367,14 +369,13 @@ describe('normalizeSubgraphDefinitions', () => { expect(subgraph.floatingLinks).toHaveLength(1) }) - it('reuses a freed node ID before minting past the counter', () => { - const second = makeSubgraph('second', ['dummy']) + it('allocates beyond the former fixed limit', () => { + const subgraph = makeSubgraph('sg', ['dummy']) const state = freshState() - state.lastNodeId = 2 - state.freeNodeIds.add(5) + state.lastNodeId = 100_000_000 const result = normalizeSubgraphDefinitions( - [second], + [subgraph], { nodeIds: new Set([toNodeId(1)]), groupIds: new Set(), @@ -384,34 +385,30 @@ describe('normalizeSubgraphDefinitions', () => { state ) - expect(result.subgraphs[0].nodes![0].id).toBe(5) - expect(state.lastNodeId).toBe(2) - expect(state.freeNodeIds.has(5)).toBe(false) + expect(result.subgraphs[0].nodes![0].id).toBe(100_000_001) + expect(state.lastNodeId).toBe(100_000_001) }) - it('leaves the passed-in state untouched when a dedup step throws', () => { - const first = makeSubgraph('first', ['dummy', 'dummy', 'dummy']) - const second = makeSubgraph('second', ['dummy', 'dummy', 'dummy']) + it('does not commit earlier counter updates when a later phase fails', () => { + const subgraph = makeSubgraph('sg', ['dummy']) + subgraph.links = [chainedLink(1)] const state = freshState() - state.lastNodeId = MAX_ID - 1 - const snapshotLastNodeId = state.lastNodeId - const snapshotFreeNodeIds = new Set(state.freeNodeIds) + state.lastLinkId = toLinkId(Number.MAX_SAFE_INTEGER) + const initialState = { ...state } expect(() => normalizeSubgraphDefinitions( - [first, second], + [subgraph], { nodeIds: new Set(), groupIds: new Set(), - linkIds: new Set(), + linkIds: new Set([1]), rerouteIds: new Set() }, state ) - ).toThrow('Node ID space exhausted') - - expect(state.lastNodeId).toBe(snapshotLastNodeId) - expect(state.freeNodeIds).toEqual(snapshotFreeNodeIds) + ).toThrow('ID space exhausted') + expect(state).toEqual(initialState) }) }) diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts index 9d85c95cb20..ce1b7cb1ccb 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts @@ -2,9 +2,9 @@ import type { LGraph } from '../LGraph' import { isUuidShapedSubgraphId } from '@/schemas/subgraphIdSchema' import { toGroupId } from '@/types/groupId' import { - MAX_ID, cloneLGraphState, commitLGraphState, + findNextAvailableId, mintGroupId, mintLinkId, mintNodeId, @@ -65,8 +65,6 @@ export function normalizeSubgraphDefinitions( clonedSubgraphs[index] = normalizeConfiguredTopology(subgraph) } - // Mint/observe against a disposable copy of `state` so a throw partway - // through (e.g. ID space exhaustion) leaves the real allocator untouched. const workingState = cloneLGraphState(state) deduplicateClonedSubgraphNodeIds( clonedSubgraphs, @@ -330,23 +328,6 @@ function numericSerializedNodeId(id: SerializedNodeId): number | null { : null } -/** - * Finds the next unused ID by repeatedly calling `advance`. - * Throws if the ID space is exhausted. - */ -export 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[], diff --git a/src/stores/executionStore.ts b/src/stores/executionStore.ts index 41d7620d773..8554849756f 100644 --- a/src/stores/executionStore.ts +++ b/src/stores/executionStore.ts @@ -945,39 +945,6 @@ export const useExecutionStore = defineStore('execution', () => { executionErrorStore.clearPromptError(runErrorKey) } - /** - * Removes every progress-state entry belonging to `nodeId`, from the live - * snapshot and from every retained per-job snapshot. Entries for a node - * nested in a subgraph are keyed by a colon-joined execution id - * (`parentId:nodeId`), so a key matches when its final segment is `nodeId`. - * Called when a node id is released back to the free pool, so a later - * mint of that id cannot inherit a deleted node's cached progress. - */ - function clearNodeProgressState(nodeId: string) { - const matches = (key: string) => - key === nodeId || key.endsWith(`:${nodeId}`) - - if (Object.keys(nodeProgressStates.value).some(matches)) { - const next = { ...nodeProgressStates.value } - for (const key of Object.keys(next)) { - if (matches(key)) delete next[key] - } - nodeProgressStates.value = next - } - - let changedByJob = false - const nextByJob = { ...nodeProgressStatesByJob.value } - for (const [jobId, states] of Object.entries(nextByJob)) { - const staleKeys = Object.keys(states).filter(matches) - if (!staleKeys.length) continue - const nextStates = { ...states } - for (const key of staleKeys) delete nextStates[key] - nextByJob[jobId] = nextStates - changedByJob = true - } - if (changedByJob) nodeProgressStatesByJob.value = nextByJob - } - function getNodeIdIfExecuting(nodeId: string | number) { const nodeIdStr = String(nodeId) return nodeIdStr.includes(':') @@ -1182,7 +1149,6 @@ export const useExecutionStore = defineStore('execution', () => { nodeProgressStates, nodeLocationProgressStates, nodeProgressStatesByJob, - clearNodeProgressState, runningJobIds, runningWorkflowCount, initializingJobIds, From e57d160bff1bfa9ef288336d0cfbb27d2ab49d40 Mon Sep 17 00:00:00 2001 From: Amp Date: Thu, 24 Sep 2026 00:57:36 +0000 Subject: [PATCH 05/11] fix: validate clipboard metadata payloads Amp-Thread-ID: https://ampcode.com/threads/T-01a0d0b9-b759-73a9-833a-e61f0faadb1b --- src/composables/usePaste.test.ts | 23 +++++++++++++++++++ src/composables/usePaste.ts | 10 ++++++-- .../validation/schemas/workflowSchema.ts | 19 +++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/composables/usePaste.test.ts b/src/composables/usePaste.test.ts index ba77ca37172..b31956e99cd 100644 --- a/src/composables/usePaste.test.ts +++ b/src/composables/usePaste.test.ts @@ -661,6 +661,29 @@ describe('usePaste', () => { }) }) + it.for([ + { name: 'null payload', data: null }, + { name: 'malformed node', data: { nodes: [{ type: 'KSampler' }] } } + ])('falls back for malformed Comfy metadata: $name', async ({ data }) => { + const encoded = btoa(JSON.stringify(data)) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'some text') + + document.dispatchEvent( + new ClipboardEvent('paste', { clipboardData: dataTransfer }) + ) + + await vi.waitFor(() => { + expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() + expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + }) + }) + it('should toast a deserialization error without falling back', async () => { const deserializeError = new Error('ID space exhausted') vi.mocked(mockCanvas._deserializeItems).mockImplementation(() => { diff --git a/src/composables/usePaste.ts b/src/composables/usePaste.ts index 159dd617ccb..241d1ae34f3 100644 --- a/src/composables/usePaste.ts +++ b/src/composables/usePaste.ts @@ -3,6 +3,7 @@ import { useEventListener } from '@vueuse/core' import { useErrorHandling } from '@/composables/useErrorHandling' import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' import type { ClipboardItems } from '@/lib/litegraph/src/types/serialisation' +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' @@ -47,7 +48,7 @@ function pasteClipboardItems(data: DataTransfer): boolean { const match = rawData.match(/data-comfy-metadata="([A-Za-z0-9+/=]+)"/)?.[1] if (!match) return false - let parsed: ClipboardItems + let parsed: unknown try { const binaryString = atob(match) const bytes = Uint8Array.from(binaryString, (c) => c.charCodeAt(0)) @@ -58,8 +59,13 @@ function pasteClipboardItems(data: DataTransfer): boolean { return false } + const clipboardItems = zClipboardItems.safeParse(parsed) + if (!clipboardItems.success) return false + try { - useCanvasStore().getCanvas()._deserializeItems(parsed, {}) + useCanvasStore() + .getCanvas() + ._deserializeItems(clipboardItems.data as ClipboardItems, {}) } catch (err) { useErrorHandling().toastErrorHandler(err) } diff --git a/src/platform/workflow/validation/schemas/workflowSchema.ts b/src/platform/workflow/validation/schemas/workflowSchema.ts index d28d1695d1f..18198552502 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.ts @@ -511,6 +511,25 @@ const zSubgraphDefinition = zComfyWorkflow1 }) .passthrough() +export const zClipboardItems = z + .object({ + nodes: z.array(zComfyNode).optional(), + groups: z.array(zGroup.extend({ id: z.number() })).optional(), + reroutes: z + .array(zReroute.extend({ linkIds: z.array(z.number()) })) + .optional(), + links: z.array(zComfyLinkObject).optional(), + subgraphs: z.array(zSubgraphDefinition).optional() + }) + .refine( + (items) => + items.nodes !== undefined || + items.groups !== undefined || + items.reroutes !== undefined || + items.links !== undefined || + items.subgraphs !== undefined + ) + export type ModelFile = z.infer export type ComfyLinkObject = z.infer export type ComfyNode = z.infer From e5af7473706c3d73879c266be93ad3180e693e24 Mon Sep 17 00:00:00 2001 From: Amp Date: Thu, 24 Sep 2026 03:11:03 +0000 Subject: [PATCH 06/11] test: cover malformed clipboard metadata Amp-Thread-ID: https://ampcode.com/threads/T-01a0d0b9-b759-73a9-833a-e61f0faadb1b --- src/composables/usePaste.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/composables/usePaste.test.ts b/src/composables/usePaste.test.ts index b31956e99cd..02340c9a813 100644 --- a/src/composables/usePaste.test.ts +++ b/src/composables/usePaste.test.ts @@ -663,6 +663,7 @@ describe('usePaste', () => { it.for([ { name: 'null payload', data: null }, + { name: 'empty object', data: {} }, { name: 'malformed node', data: { nodes: [{ type: 'KSampler' }] } } ])('falls back for malformed Comfy metadata: $name', async ({ data }) => { const encoded = btoa(JSON.stringify(data)) @@ -684,6 +685,28 @@ describe('usePaste', () => { }) }) + it('falls back when Comfy metadata is not valid JSON', async () => { + const parseError = vi.spyOn(console, 'error').mockImplementation(() => {}) + onTestFinished(() => parseError.mockRestore()) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'some text') + + document.dispatchEvent( + new ClipboardEvent('paste', { clipboardData: dataTransfer }) + ) + + await vi.waitFor(() => { + expect(parseError).toHaveBeenCalledOnce() + expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() + expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + }) + }) + it('should toast a deserialization error without falling back', async () => { const deserializeError = new Error('ID space exhausted') vi.mocked(mockCanvas._deserializeItems).mockImplementation(() => { From 8d22ce52d233cd18df6741e827235640063d32a1 Mon Sep 17 00:00:00 2001 From: Amp Date: Sun, 27 Sep 2026 01:07:40 +0000 Subject: [PATCH 07/11] test: prevent partial paste on ID exhaustion Amp-Thread-ID: https://ampcode.com/threads/T-01a0d0b9-b759-73a9-833a-e61f0faadb1b --- .../src/LGraphCanvas.clipboard.test.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index d5c95657b81..e6b09738f9f 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -257,6 +257,32 @@ describe('remapClipboardSubgraphNodeIds', () => { expect(afterGraphChange).toHaveBeenCalledOnce() expect(afterCanvasChange).toHaveBeenCalledOnce() }) + + it('does not partially paste when a later ID allocation is exhausted', () => { + const nodeType = 'test/clipboard-id-preflight' + registerClipboardNodeType(nodeType) + const rootGraph = new LGraph() + rootGraph.state.lastGroupId = Number.MAX_SAFE_INTEGER - 1 + rootGraph.state.lastNodeId = Number.MAX_SAFE_INTEGER + const canvas = createCanvas(rootGraph) + const parsed: ClipboardItems = { + groups: [ + { + id: 1, + title: 'Group', + bounding: [0, 0, 100, 100] + } + ], + nodes: [createSerialisedNode(1, nodeType)] + } + + expect(() => canvas._deserializeItems(parsed, {})).toThrow( + 'ID space exhausted' + ) + expect(rootGraph.groups).toHaveLength(0) + expect(rootGraph.nodes).toHaveLength(0) + expect(rootGraph.state.lastGroupId).toBe(Number.MAX_SAFE_INTEGER - 1) + }) }) function createCanvas(graph: LGraph): LGraphCanvas { From d203bb0888710dc109fe7b158bd43513b4bb4858 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 27 Sep 2026 01:28:02 +0000 Subject: [PATCH 08/11] fix(litegraph): preflight clipboard paste id space before mutating graph Pasting groups/nodes minted ids directly against the live graph state one item at a time. If a later mint (e.g. node ids) hit id-space exhaustion, earlier groups already added via graph.add() stayed committed instead of the whole paste being rolled back, and the group id counter was left advanced. Preflight both the groups and nodes counts against a disposable clone of the graph's id state before any mutation, mirroring the pattern already used by remapClipboardSubgraphNodeIds, so exhaustion throws before anything is added to the graph. --- src/lib/litegraph/src/LGraphCanvas.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 435e9e142ae..b567b6d05f9 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -43,13 +43,16 @@ import type { ContextMenu } from './ContextMenu' import { createCursorCache } from './cursorCache' import { DragAndScale } from './DragAndScale' import type { AnimationOptions } from './DragAndScale' +import { isRootGraphDocBound } from './docBoundGraphs' import { cloneLGraphState, commitLGraphState, findNextAvailableId, + mintGroupId, mintNodeId, observeNodeId } from './idAllocation' +import type { NodeIdMintMode } from './idAllocation' import type { LGraph, SubgraphId } from './LGraph' import { LGraphGroup } from './LGraphGroup' import type { SlotTypeDefaultNodeOpts } from './LiteGraphGlobal' @@ -4238,6 +4241,18 @@ export class LGraphCanvas implements CustomEventDispatcher if (nodeInfo.type in subgraphIdMap) nodeInfo.type = subgraphIdMap[nodeInfo.type] remapClipboardSubgraphNodeIds(parsed, graph.rootGraph) + // Preflight the target graph's id space against a disposable clone + // before mutating anything: minting group/node ids is not + // transactional, so an exhaustion partway through the Groups/Nodes + // loops below would otherwise leave earlier groups already committed. + const idPreflightState = cloneLGraphState(graph.state) + const nodeMintMode: NodeIdMintMode = + graph.isRootGraph && isRootGraphDocBound(graph.id) + ? 'crdt-disjoint' + : 'sequential' + for (let i = parsed.groups.length; i > 0; i--) mintGroupId(idPreflightState) + for (let i = parsed.nodes.length; i > 0; i--) + mintNodeId(idPreflightState, nodeMintMode) // Subgraphs const subgraphs = graph.createSubgraphs(parsed.subgraphs) for (const subgraph of subgraphs) From cd095a35ddff34d385c1efe958964d716b88195e Mon Sep 17 00:00:00 2001 From: Amp Date: Sun, 27 Sep 2026 07:45:23 +0000 Subject: [PATCH 09/11] fix: preflight all clipboard IDs atomically Amp-Thread-ID: https://ampcode.com/threads/T-01a0d0b9-b759-73a9-833a-e61f0faadb1b --- .../src/LGraphCanvas.clipboard.test.ts | 40 +++++++++++++ src/lib/litegraph/src/LGraphCanvas.ts | 60 ++++++++++++++----- 2 files changed, 85 insertions(+), 15 deletions(-) diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index e6b09738f9f..00f851af7f0 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -283,6 +283,46 @@ describe('remapClipboardSubgraphNodeIds', () => { expect(rootGraph.nodes).toHaveLength(0) expect(rootGraph.state.lastGroupId).toBe(Number.MAX_SAFE_INTEGER - 1) }) + + it('does not partially paste when reroute ID allocation is exhausted', () => { + const rootGraph = new LGraph() + rootGraph.state.lastGroupId = Number.MAX_SAFE_INTEGER - 1 + rootGraph.state.lastRerouteId = toRerouteId(Number.MAX_SAFE_INTEGER) + const canvas = createCanvas(rootGraph) + const parsed: ClipboardItems = { + groups: [ + { + id: 1, + title: 'Group', + bounding: [0, 0, 100, 100] + } + ], + reroutes: [{ id: 1, pos: [0, 0], linkIds: [] }] + } + + expect(() => canvas._deserializeItems(parsed, {})).toThrow( + 'ID space exhausted' + ) + expect(rootGraph.groups).toHaveLength(0) + expect(rootGraph.state.lastGroupId).toBe(Number.MAX_SAFE_INTEGER - 1) + }) + + it('preserves counters when preflight fails after subgraph remapping', () => { + const rootGraph = new LGraph() + const existingNode = new LGraphNode('existing') + existingNode.id = toNodeId(1) + rootGraph.add(existingNode) + rootGraph.state.lastNodeId = Number.MAX_SAFE_INTEGER - 1 + rootGraph.state.lastGroupId = Number.MAX_SAFE_INTEGER + const canvas = createCanvas(rootGraph) + const parsed = createSubgraphClipboardItems(1) + parsed.groups = [{ id: 1, title: 'Group', bounding: [0, 0, 100, 100] }] + + expect(() => canvas._deserializeItems(parsed, {})).toThrow( + 'ID space exhausted' + ) + expect(rootGraph.state.lastNodeId).toBe(Number.MAX_SAFE_INTEGER - 1) + }) }) function createCanvas(graph: LGraph): LGraphCanvas { diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index b567b6d05f9..92802a1a564 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -41,18 +41,19 @@ import { forEachNode } from '@/utils/graphTraversalUtil' import { CanvasPointer } from './CanvasPointer' import type { ContextMenu } from './ContextMenu' import { createCursorCache } from './cursorCache' +import { isRootGraphDocBound } from './docBoundGraphs' import { DragAndScale } from './DragAndScale' import type { AnimationOptions } from './DragAndScale' -import { isRootGraphDocBound } from './docBoundGraphs' import { cloneLGraphState, commitLGraphState, findNextAvailableId, mintGroupId, + mintLinkId, mintNodeId, + mintRerouteId, observeNodeId } from './idAllocation' -import type { NodeIdMintMode } from './idAllocation' import type { LGraph, SubgraphId } from './LGraph' import { LGraphGroup } from './LGraphGroup' import type { SlotTypeDefaultNodeOpts } from './LiteGraphGlobal' @@ -60,7 +61,7 @@ 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 type { NodeId, SerializedNodeId } from '@/types/nodeId' import { LLink, slotFloatingLinks } from './LLink' import { inputHasLink, @@ -71,6 +72,12 @@ import { import type { LinkId } from './LLink' import { Reroute } from './Reroute' import type { RerouteId } from './Reroute' +import { + collectReservedGroupIds, + collectReservedLinkIds, + collectReservedRerouteIds, + normalizeSubgraphDefinitions +} from './subgraph/subgraphDeduplication' import type { CanvasInteractionModeReader } from './canvas/CanvasInteractionMode' import { LinkConnector } from './canvas/LinkConnector' import { @@ -4197,6 +4204,8 @@ export class LGraphCanvas implements CustomEventDispatcher connectInputs: boolean, position: Point ): ClipboardPasteResult | undefined { + preflightClipboardItemIds(parsed, graph) + // Parse & initialise parsed.nodes ??= [] parsed.groups ??= [] @@ -4241,18 +4250,6 @@ export class LGraphCanvas implements CustomEventDispatcher if (nodeInfo.type in subgraphIdMap) nodeInfo.type = subgraphIdMap[nodeInfo.type] remapClipboardSubgraphNodeIds(parsed, graph.rootGraph) - // Preflight the target graph's id space against a disposable clone - // before mutating anything: minting group/node ids is not - // transactional, so an exhaustion partway through the Groups/Nodes - // loops below would otherwise leave earlier groups already committed. - const idPreflightState = cloneLGraphState(graph.state) - const nodeMintMode: NodeIdMintMode = - graph.isRootGraph && isRootGraphDocBound(graph.id) - ? 'crdt-disjoint' - : 'sequential' - for (let i = parsed.groups.length; i > 0; i--) mintGroupId(idPreflightState) - for (let i = parsed.nodes.length; i > 0; i--) - mintNodeId(idPreflightState, nodeMintMode) // Subgraphs const subgraphs = graph.createSubgraphs(parsed.subgraphs) for (const subgraph of subgraphs) @@ -9216,3 +9213,36 @@ export function remapClipboardSubgraphNodeIds( commitLGraphState(rootGraph.state, workingState) } + +function preflightClipboardItemIds( + parsed: ClipboardItems, + graph: LGraph +): void { + const rootGraph = graph.rootGraph + const workingState = cloneLGraphState(rootGraph.state) + if (parsed.subgraphs?.length) { + const nodeIds = new Set() + forEachNode(rootGraph, (node) => nodeIds.add(node.id)) + normalizeSubgraphDefinitions( + parsed.subgraphs, + { + nodeIds, + groupIds: collectReservedGroupIds(rootGraph), + linkIds: collectReservedLinkIds(rootGraph), + rerouteIds: collectReservedRerouteIds(rootGraph) + }, + workingState + ) + } + + for (let index = 0; index < (parsed.groups?.length ?? 0); index++) + mintGroupId(workingState) + if (!graph.isRootGraph || !isRootGraphDocBound(graph.id)) { + for (let index = 0; index < (parsed.nodes?.length ?? 0); index++) + mintNodeId(workingState) + } + for (let index = 0; index < (parsed.reroutes?.length ?? 0); index++) + mintRerouteId(workingState) + for (let index = 0; index < (parsed.links?.length ?? 0); index++) + mintLinkId(workingState) +} From ac1636ef689a695f6b86f69fa611262256d9d67f Mon Sep 17 00:00:00 2001 From: Amp Date: Sun, 27 Sep 2026 09:14:04 +0000 Subject: [PATCH 10/11] fix: recover exhausted graph ID allocation Amp-Thread-ID: https://ampcode.com/threads/T-01a0d0b9-b759-73a9-833a-e61f0faadb1b --- src/composables/usePaste.test.ts | 19 +- src/composables/usePaste.ts | 9 +- src/extensions/core/groupNode.ts | 10 +- src/extensions/core/rerouteNode.ts | 3 +- src/extensions/core/widgetInputs.ts | 3 +- src/lib/litegraph/src/LGraph.ts | 76 +++-- .../src/LGraphCanvas.clipboard.test.ts | 284 +++--------------- src/lib/litegraph/src/LGraphCanvas.ts | 224 +------------- src/lib/litegraph/src/LGraphNode.ts | 7 +- .../litegraph/src/canvas/linkBadgeRenderer.ts | 3 +- .../litegraph/src/canvas/linkBadges.test.ts | 1 + src/lib/litegraph/src/canvas/linkBadges.ts | 3 +- .../src/idAllocation.property.test.ts | 14 +- src/lib/litegraph/src/idAllocation.test.ts | 97 ++++-- src/lib/litegraph/src/idAllocation.ts | 102 ++++--- src/lib/litegraph/src/interfaces.ts | 6 +- .../litegraph/src/subgraph/SubgraphInput.ts | 5 +- .../src/subgraph/SubgraphInputNode.ts | 5 +- .../litegraph/src/subgraph/SubgraphOutput.ts | 5 +- .../subgraph/subgraphDeduplication.test.ts | 58 +++- .../src/subgraph/subgraphDeduplication.ts | 77 ++--- src/lib/litegraph/src/utils/type.ts | 9 +- .../validation/schemas/workflowSchema.test.ts | 31 +- .../validation/schemas/workflowSchema.ts | 119 +++++++- src/stores/entityIdStore.test.ts | 6 +- .../extensions/agent/crdt/linkMintPort.ts | 4 +- 26 files changed, 524 insertions(+), 656 deletions(-) diff --git a/src/composables/usePaste.test.ts b/src/composables/usePaste.test.ts index 02340c9a813..ac4dad62601 100644 --- a/src/composables/usePaste.test.ts +++ b/src/composables/usePaste.test.ts @@ -642,8 +642,9 @@ describe('usePaste', () => { }) }) - it('should ignore metadata from other applications', async () => { - const encoded = btoa(JSON.stringify({ test: 'data' })) + it('accepts validated legacy data-metadata clipboard items', async () => { + const data = { nodes: [] } + const encoded = btoa(JSON.stringify(data)) const html = `
` usePaste() @@ -656,15 +657,21 @@ describe('usePaste', () => { document.dispatchEvent(event) await vi.waitFor(() => { - expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() - expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + expect(mockCanvas._deserializeItems).toHaveBeenCalledWith( + data, + expect.any(Object) + ) }) }) it.for([ { name: 'null payload', data: null }, { name: 'empty object', data: {} }, - { name: 'malformed node', data: { nodes: [{ type: 'KSampler' }] } } + { name: 'malformed node', data: { nodes: [{ type: 'KSampler' }] } }, + { name: 'malformed group', data: { groups: [{ id: 1 }] } }, + { name: 'malformed reroute', data: { reroutes: [{ id: 1 }] } }, + { name: 'malformed link', data: { links: [{ id: 1 }] } }, + { name: 'malformed subgraph', data: { subgraphs: [{ id: 'invalid' }] } } ])('falls back for malformed Comfy metadata: $name', async ({ data }) => { const encoded = btoa(JSON.stringify(data)) const html = `
` @@ -708,7 +715,7 @@ describe('usePaste', () => { }) it('should toast a deserialization error without falling back', async () => { - const deserializeError = new Error('ID space exhausted') + const deserializeError = new Error('Paste failed') vi.mocked(mockCanvas._deserializeItems).mockImplementation(() => { throw deserializeError }) diff --git a/src/composables/usePaste.ts b/src/composables/usePaste.ts index 241d1ae34f3..1f1adbe70f9 100644 --- a/src/composables/usePaste.ts +++ b/src/composables/usePaste.ts @@ -2,7 +2,6 @@ import { useEventListener } from '@vueuse/core' import { useErrorHandling } from '@/composables/useErrorHandling' import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' -import type { ClipboardItems } from '@/lib/litegraph/src/types/serialisation' import { zClipboardItems } from '@/platform/workflow/validation/schemas/workflowSchema' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { app } from '@/scripts/app' @@ -45,7 +44,9 @@ export function cloneDataTransfer(original: DataTransfer): DataTransfer { function pasteClipboardItems(data: DataTransfer): boolean { const rawData = data.getData('text/html') - const match = rawData.match(/data-comfy-metadata="([A-Za-z0-9+/=]+)"/)?.[1] + const match = rawData.match( + /data-(?:comfy-)?metadata="([A-Za-z0-9+/=]+)"/ + )?.[1] if (!match) return false let parsed: unknown @@ -63,9 +64,7 @@ function pasteClipboardItems(data: DataTransfer): boolean { if (!clipboardItems.success) return false try { - useCanvasStore() - .getCanvas() - ._deserializeItems(clipboardItems.data as ClipboardItems, {}) + useCanvasStore().getCanvas()._deserializeItems(clipboardItems.data, {}) } catch (err) { useErrorHandling().toastErrorHandler(err) } diff --git a/src/extensions/core/groupNode.ts b/src/extensions/core/groupNode.ts index 89fd86ac513..55e95e2e90e 100644 --- a/src/extensions/core/groupNode.ts +++ b/src/extensions/core/groupNode.ts @@ -8,6 +8,7 @@ import type { } from '@/lib/litegraph/src/litegraph' import { LiteGraph } from '@/lib/litegraph/src/litegraph' import { outputLinks } from '@/lib/litegraph/src/node/slotLinks' +import { slotTypeKey } from '@/lib/litegraph/src/utils/type' import type { SerializedNodeId } from '@/types/nodeId' import { parseNodeId } from '@/types/nodeId' import type { @@ -229,7 +230,9 @@ export class GroupNodeConfig { if (!linksFrom) return const firstLink = linksFrom[0]?.at(0) - let type: string | number | null = firstLink?.[5] ?? null + let type: string | number | null = firstLink + ? slotTypeKey(firstLink[5]) + : null if (type === 'COMBO') { // Use the array items const output = node.outputs?.[0] @@ -326,13 +329,14 @@ export class GroupNodeConfig { } config.forceInput = true + const rerouteTypeKey = slotTypeKey(rerouteType) return { input: { required: { - [rerouteType]: [rerouteType, config] + [rerouteTypeKey]: [rerouteTypeKey, config] } }, - output: [rerouteType], + output: [rerouteTypeKey], output_name: [], output_is_list: [] } diff --git a/src/extensions/core/rerouteNode.ts b/src/extensions/core/rerouteNode.ts index 93b24540ef4..fef9732f12f 100644 --- a/src/extensions/core/rerouteNode.ts +++ b/src/extensions/core/rerouteNode.ts @@ -6,6 +6,7 @@ import { } from '@/lib/litegraph/src/litegraph' import type { ISlotType } from '@/lib/litegraph/src/interfaces' import { outputLinks } from '@/lib/litegraph/src/node/slotLinks' +import { slotTypeKey } from '@/lib/litegraph/src/utils/type' import { app } from '../../scripts/app' import { getWidgetConfig, mergeIfValid, setWidgetConfig } from './widgetInputs' @@ -115,7 +116,7 @@ app.registerExtension({ } } - const displayType = inputType || outputType || '*' + const displayType = slotTypeKey(inputType || outputType || '*') const color = LGraphCanvas.link_type_colors[displayType] let widgetConfig diff --git a/src/extensions/core/widgetInputs.ts b/src/extensions/core/widgetInputs.ts index c8251728cff..1b071d9f270 100644 --- a/src/extensions/core/widgetInputs.ts +++ b/src/extensions/core/widgetInputs.ts @@ -13,6 +13,7 @@ import type { import type { IWidgetLocator } from '@/lib/litegraph/src/interfaces' import { NodeSlot } from '@/lib/litegraph/src/node/NodeSlot' import { outputHasLinks, outputLinks } from '@/lib/litegraph/src/node/slotLinks' +import { slotTypeKey } from '@/lib/litegraph/src/utils/type' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { assetService } from '@/platform/assets/services/assetService' import { createAssetWidget } from '@/platform/assets/utils/createAssetWidget' @@ -196,7 +197,7 @@ export class PrimitiveNode extends LGraphNode { ) { // Fires before the link is made allowing us to reject it if it isn't valid // No widget, we can't connect - if (!input.widget && !(input.type in ComfyWidgets)) { + if (!input.widget && !(slotTypeKey(input.type) in ComfyWidgets)) { return false } diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 6a82ea2a01e..b22e55c11f3 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -186,6 +186,7 @@ import { import { collectReservedGroupIds, collectReservedLinkIds, + collectReservedNodeIds, collectReservedRerouteIds, normalizeSubgraphDefinitions, topologicalSortSubgraphs @@ -1372,7 +1373,9 @@ export class LGraph groupId === -1 || layoutStore.getGroupLayout(this.rootGraph.id, groupId) ) { - node.id = mintGroupId(state) + node.id = mintGroupId(state, () => + collectReservedGroupIds(this.rootGraph) + ) } observeGroupId(state, node.id) @@ -1403,7 +1406,16 @@ export class LGraph // give him an id if (node.id === UNASSIGNED_NODE_ID) { const mintMode = nodeIdMintModeFor(this) - node.id = mintNodeId(state, mintMode) + node.id = mintNodeId( + state, + mintMode, + () => + new Set( + [...collectReservedNodeIds(this.rootGraph)] + .map(Number) + .filter(Number.isSafeInteger) + ) + ) } else { observeNodeId(state, node.id) } @@ -1417,7 +1429,16 @@ export class LGraph node.graph = this attachNodeToStores(this, node, () => - mintNodeId(state, nodeIdMintModeFor(this)) + mintNodeId( + state, + nodeIdMintModeFor(this), + () => + new Set( + [...collectReservedNodeIds(this.rootGraph)] + .map(Number) + .filter(Number.isSafeInteger) + ) + ) ) this._nodes.push(node) @@ -1863,7 +1884,9 @@ export class LGraph addFloatingLink(link: LLink): LLink | undefined { if (link.id === -1) { - link.id = mintLinkId(this.state) + link.id = mintLinkId(this.state, () => + collectReservedLinkIds(this.rootGraph) + ) } if (!registerLinkTopology(this, link)) return @@ -1970,7 +1993,11 @@ export class LGraph floating }: OptionalProps): Reroute | undefined { const rerouteId = - id === undefined ? mintRerouteId(this.state) : toRerouteId(id) + id === undefined + ? mintRerouteId(this.state, () => + collectReservedRerouteIds(this.rootGraph) + ) + : toRerouteId(id) observeRerouteId(this.state, rerouteId) const existingReroute = this.reroutes.get(rerouteId) @@ -2100,34 +2127,27 @@ export class LGraph return this.createSubgraphs([data])[0] } - createSubgraphs(data: ExportedSubgraph[]): Subgraph[] { + createSubgraphs( + data: ExportedSubgraph[], + rootNodes?: ISerialisedNode[] + ): Subgraph[] { if (!data.length) return [] const normalized = normalizeSubgraphDefinitions( data, { - nodeIds: this.collectReservedNodeIds(), + nodeIds: collectReservedNodeIds(this.rootGraph), groupIds: collectReservedGroupIds(this.rootGraph), linkIds: collectReservedLinkIds(this.rootGraph), rerouteIds: collectReservedRerouteIds(this.rootGraph) }, - this.state - ).subgraphs - return this.createNormalizedSubgraphs(normalized) - } - - private collectReservedNodeIds( - rootNodes: ISerialisedNode[] = [] - ): Set { - const reserved = new Set() - for (const owner of [ - this.rootGraph, - ...this.rootGraph.subgraphs.values() - ]) { - for (const node of owner.nodes) reserved.add(node.id) + this.state, + rootNodes + ) + if (rootNodes && normalized.rootNodes) { + rootNodes.splice(0, rootNodes.length, ...normalized.rootNodes) } - for (const node of rootNodes) reserved.add(toNodeId(node.id)) - return reserved + return this.createNormalizedSubgraphs(normalized.subgraphs) } private createNormalizedSubgraphs(data: ExportedSubgraph[]): Subgraph[] { @@ -2685,7 +2705,9 @@ export class LGraph // Shared definitions may survive, so unpacked groups need fresh layout // ids, like the reroutes below. for (const groupInfo of groups) { - const groupId = mintGroupId(this.rootGraph.state) + const groupId = mintGroupId(this.rootGraph.state, () => + collectReservedGroupIds(this.rootGraph) + ) groupInfo.id = groupId const group = new LGraphGroup(groupInfo.title, groupId) this.add(group, true) @@ -2775,7 +2797,9 @@ export class LGraph const rerouteIdMap = new Map() const oldReroutes = subgraphNode.subgraph.reroutes for (const reroute of oldReroutes.values()) { - const migratedId = mintRerouteId(this.state) + const migratedId = mintRerouteId(this.state, () => + collectReservedRerouteIds(this.rootGraph) + ) const migratedReroute = this.setReroute({ id: migratedId, pos: [reroute.pos[0] + offsetX, reroute.pos[1] + offsetY], @@ -3168,7 +3192,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 00f851af7f0..43b0fa9dada 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,263 +65,67 @@ function createSerialisedNode( } } -function createSubgraphClipboardItems(interiorNodeId: number): ClipboardItems { - return { - nodes: [], - groups: [], - reroutes: [], - links: [], - subgraphs: [ - { - id: createUuidv4(), - version: 1, - revision: 0, - state: { - lastNodeId: 0, - lastLinkId: 0, - lastGroupId: 0, - lastRerouteId: 0 - }, - name: 'Pasted Subgraph', - inputNode: { id: SUBGRAPH_INPUT_ID, bounding: [0, 0, 10, 10] }, - outputNode: { id: SUBGRAPH_OUTPUT_ID, bounding: [0, 0, 10, 10] }, - nodes: [createSerialisedNode(interiorNodeId, 'test/node')] - } - ] - } -} - -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: [] - } - - const parsed: ClipboardItems = { - nodes: [createSerialisedNode(99, subgraphId, [['1', 'seed']])], - groups: [], - reroutes: [], - links: [], - subgraphs: [pastedSubgraph] + reroutes: [{ id: 7, pos: [0, 0], linkIds: [7] }] } + const original = structuredClone(parsed) - remapClipboardSubgraphNodeIds(parsed, rootGraph) - - const remappedSubgraph = parsed.subgraphs?.[0] - expect(remappedSubgraph).toBeDefined() + const result = canvas._deserializeItems(parsed, {}) - const remappedLink = remappedSubgraph?.links?.[0] - expect(remappedLink).toBeDefined() - - const remappedInteriorId = remappedSubgraph?.nodes?.[0]?.id - expect(remappedInteriorId).not.toBe(1) - expect(remappedLink?.origin_id).toBe(remappedInteriorId) - expect(remappedLink?.target_id).toBe(remappedInteriorId) - - const remappedNode = parsed.nodes?.[0] - expect(remappedNode).toBeDefined() - expect(remappedNode?.properties?.proxyWidgets).toStrictEqual([ - [String(remappedInteriorId), 'seed'] + expect(parsed).toEqual(original) + expect(result?.created).toHaveLength(4) + expect(rootGraph.nodes.map((node) => node.id)).toEqual([ + toNodeId(1), + toNodeId(2) ]) + expect(rootGraph.groups[0].id).toBe(1) + expect([...rootGraph.links.keys()]).toEqual([toLinkId(1)]) + expect([...rootGraph.reroutes.keys()]).toEqual([toRerouteId(1)]) }) - it('remaps pasted SubgraphNode previewExposures sourceNodeId references', () => { - const rootGraph = new LGraph() - const existingNode = new LGraphNode('existing') - existingNode.id = toNodeId(1) - rootGraph.add(existingNode) - - const subgraphId = createUuidv4() - const pastedSubgraph: ExportedSubgraph = { - id: subgraphId, - version: 1, - revision: 0, - state: { - lastNodeId: 0, - lastLinkId: 0, - lastGroupId: 0, - lastRerouteId: 0 - }, - config: {}, - name: 'Pasted Subgraph', - inputNode: { id: SUBGRAPH_INPUT_ID, bounding: [0, 0, 10, 10] }, - outputNode: { id: SUBGRAPH_OUTPUT_ID, bounding: [0, 0, 10, 10] }, - inputs: [], - outputs: [], - widgets: [], - nodes: [createSerialisedNode(1, 'test/node')], - links: [], - groups: [] - } - - const hostInfo = createSerialisedNode(99, subgraphId) - hostInfo.properties = { - previewExposures: [ - { - name: '$$canvas-image-preview', - sourceNodeId: '1', - sourcePreviewName: '$$canvas-image-preview' - } - ] - } - - const parsed: ClipboardItems = { - nodes: [hostInfo], - groups: [], - reroutes: [], - links: [], - subgraphs: [pastedSubgraph] - } - - remapClipboardSubgraphNodeIds(parsed, rootGraph) - - 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('remaps collisions above the former fixed limit', () => { - const rootGraph = new LGraph() - const existingNode = new LGraphNode('existing') - existingNode.id = toNodeId(1) - rootGraph.add(existingNode) - rootGraph.state.lastNodeId = 100_000_000 - const parsed = createSubgraphClipboardItems(1) - - remapClipboardSubgraphNodeIds(parsed, rootGraph) - - expect(parsed.subgraphs?.[0].nodes?.[0].id).toBe(100_000_001) - expect(rootGraph.state.lastNodeId).toBe(100_000_001) - }) - - it('closes change tracking and preserves counters when remapping fails', () => { - const rootGraph = new LGraph() - const existingNode = new LGraphNode('existing') - existingNode.id = toNodeId(1) - rootGraph.add(existingNode) - rootGraph.state.lastNodeId = Number.MAX_SAFE_INTEGER - const canvas = createCanvas(rootGraph) - const afterGraphChange = vi.spyOn(rootGraph, 'afterChange') - const afterCanvasChange = vi.spyOn(canvas, 'emitAfterChange') - const parsed = createSubgraphClipboardItems(1) - - expect(() => canvas._deserializeItems(parsed, {})).toThrow( - 'ID space exhausted' - ) - expect(rootGraph.state.lastNodeId).toBe(Number.MAX_SAFE_INTEGER) - expect(afterGraphChange).toHaveBeenCalledOnce() - expect(afterCanvasChange).toHaveBeenCalledOnce() - }) - - it('does not partially paste when a later ID allocation is exhausted', () => { - const nodeType = 'test/clipboard-id-preflight' + it('emits one complete canvas change pair for public paste', () => { + const nodeType = 'test/clipboard-change-hooks' registerClipboardNodeType(nodeType) const rootGraph = new LGraph() - rootGraph.state.lastGroupId = Number.MAX_SAFE_INTEGER - 1 - rootGraph.state.lastNodeId = Number.MAX_SAFE_INTEGER const canvas = createCanvas(rootGraph) - const parsed: ClipboardItems = { - groups: [ - { - id: 1, - title: 'Group', - bounding: [0, 0, 100, 100] - } - ], - nodes: [createSerialisedNode(1, nodeType)] - } - - expect(() => canvas._deserializeItems(parsed, {})).toThrow( - 'ID space exhausted' + const before = vi.spyOn(canvas, 'emitBeforeChange') + const after = vi.spyOn(canvas, 'emitAfterChange') + localStorage.setItem( + 'litegrapheditor_clipboard', + JSON.stringify({ nodes: [createSerialisedNode(1, nodeType)] }) ) - expect(rootGraph.groups).toHaveLength(0) - expect(rootGraph.nodes).toHaveLength(0) - expect(rootGraph.state.lastGroupId).toBe(Number.MAX_SAFE_INTEGER - 1) - }) + onTestFinished(() => localStorage.removeItem('litegrapheditor_clipboard')) - it('does not partially paste when reroute ID allocation is exhausted', () => { - const rootGraph = new LGraph() - rootGraph.state.lastGroupId = Number.MAX_SAFE_INTEGER - 1 - rootGraph.state.lastRerouteId = toRerouteId(Number.MAX_SAFE_INTEGER) - const canvas = createCanvas(rootGraph) - const parsed: ClipboardItems = { - groups: [ - { - id: 1, - title: 'Group', - bounding: [0, 0, 100, 100] - } - ], - reroutes: [{ id: 1, pos: [0, 0], linkIds: [] }] - } + canvas.pasteFromClipboard() - expect(() => canvas._deserializeItems(parsed, {})).toThrow( - 'ID space exhausted' - ) - expect(rootGraph.groups).toHaveLength(0) - expect(rootGraph.state.lastGroupId).toBe(Number.MAX_SAFE_INTEGER - 1) - }) - - it('preserves counters when preflight fails after subgraph remapping', () => { - const rootGraph = new LGraph() - const existingNode = new LGraphNode('existing') - existingNode.id = toNodeId(1) - rootGraph.add(existingNode) - rootGraph.state.lastNodeId = Number.MAX_SAFE_INTEGER - 1 - rootGraph.state.lastGroupId = Number.MAX_SAFE_INTEGER - const canvas = createCanvas(rootGraph) - const parsed = createSubgraphClipboardItems(1) - parsed.groups = [{ id: 1, title: 'Group', bounding: [0, 0, 100, 100] }] - - expect(() => canvas._deserializeItems(parsed, {})).toThrow( - 'ID space exhausted' - ) - expect(rootGraph.state.lastNodeId).toBe(Number.MAX_SAFE_INTEGER - 1) + expect(before).toHaveBeenCalledOnce() + expect(after).toHaveBeenCalledOnce() }) }) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 92802a1a564..352a43f05dd 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -41,27 +41,16 @@ import { forEachNode } from '@/utils/graphTraversalUtil' import { CanvasPointer } from './CanvasPointer' import type { ContextMenu } from './ContextMenu' import { createCursorCache } from './cursorCache' -import { isRootGraphDocBound } from './docBoundGraphs' import { DragAndScale } from './DragAndScale' import type { AnimationOptions } from './DragAndScale' -import { - cloneLGraphState, - commitLGraphState, - findNextAvailableId, - mintGroupId, - mintLinkId, - mintNodeId, - mintRerouteId, - 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 { NodeId, SerializedNodeId } from '@/types/nodeId' +import { parseNodeId, serializeNodeId } from '@/types/nodeId' +import type { SerializedNodeId } from '@/types/nodeId' import { LLink, slotFloatingLinks } from './LLink' import { inputHasLink, @@ -72,12 +61,6 @@ import { import type { LinkId } from './LLink' import { Reroute } from './Reroute' import type { RerouteId } from './Reroute' -import { - collectReservedGroupIds, - collectReservedLinkIds, - collectReservedRerouteIds, - normalizeSubgraphDefinitions -} from './subgraph/subgraphDeduplication' import type { CanvasInteractionModeReader } from './canvas/CanvasInteractionMode' import { LinkConnector } from './canvas/LinkConnector' import { @@ -185,6 +168,7 @@ import type { IBaseWidget, TWidgetValue } from './types/widgets' import { alignNodes, distributeNodes, getBoundaryNodes } from './utils/arrange' import { findFirstNode, getDraggedItems } from './utils/collections' import { resolveConnectingLinkColor } from './utils/linkColors' +import { slotTypeKey } from './utils/type' import { createUuidv4 } from '@/utils/uuid' import { BaseWidget } from './widgets/BaseWidget' import { toConcreteWidget } from './widgets/widgetMap' @@ -4199,12 +4183,12 @@ export class LGraphCanvas implements CustomEventDispatcher } private _deserializeItemsBody( - parsed: ClipboardItems, + clipboardItems: ClipboardItems, graph: LGraph, connectInputs: boolean, position: Point ): ClipboardPasteResult | undefined { - preflightClipboardItemIds(parsed, graph) + const parsed = structuredClone(clipboardItems) // Parse & initialise parsed.nodes ??= [] @@ -4249,9 +4233,8 @@ export class LGraphCanvas implements CustomEventDispatcher 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) + const subgraphs = graph.createSubgraphs(parsed.subgraphs, parsed.nodes) for (const subgraph of subgraphs) results.subgraphs.set(subgraph.id, subgraph) @@ -4420,12 +4403,7 @@ export class LGraphCanvas implements CustomEventDispatcher } pasteFromClipboard(options: IPasteFromClipboardOptions = {}): void { - this.emitBeforeChange() - try { - this._pasteFromClipboard(options) - } finally { - this.emitAfterChange() - } + this._pasteFromClipboard(options) } processNodeDblClicked(n: LGraphNode): void { @@ -6477,7 +6455,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 } @@ -6989,7 +6967,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 @@ -7182,7 +7161,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 @@ -9066,183 +9046,3 @@ defineDeprecatedProperty( '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 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 - - return remappedIds.get(numericId) -} - -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 - - const remappedNodeId = remapNodeId(nodeId, remappedIds) - if (remappedNodeId !== undefined) entry[0] = String(remappedNodeId) - } -} - -function hasStringSourceNodeId( - value: unknown -): value is { sourceNodeId: string } { - return ( - typeof value === 'object' && - value !== null && - 'sourceNodeId' in value && - typeof value.sourceNodeId === 'string' - ) -} - -function remapPreviewExposures( - info: ISerialisedNode, - remappedIds: Map | undefined -) { - if (!remappedIds || remappedIds.size === 0) return - - const previewExposures = info.properties?.previewExposures - if (!Array.isArray(previewExposures)) return - - for (const entry of previewExposures) { - if (!hasStringSourceNodeId(entry) || entry.sourceNodeId === '-1') continue - - const remappedNodeId = remapNodeId(entry.sourceNodeId, remappedIds) - if (remappedNodeId !== undefined) - entry.sourceNodeId = String(remappedNodeId) - } -} - -export function remapClipboardSubgraphNodeIds( - parsed: ClipboardItems, - rootGraph: LGraph -): void { - // Mint/observe against a disposable copy of the graph's ID state so a - // throw (e.g. ID space exhaustion) leaves the real allocator untouched. - const workingState = cloneLGraphState(rootGraph.state) - - const usedNodeIds = new Set() - forEachNode(rootGraph, (node) => { - const numericId = Number(node.id) - if (!Number.isInteger(numericId)) return - usedNodeIds.add(numericId) - observeNodeId(workingState, toNodeId(numericId)) - }) - - function nextUniqueNodeId(): number { - return findNextAvailableId(usedNodeIds, () => - Number(mintNodeId(workingState)) - ) - } - - 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 - - if (usedNodeIds.has(nodeInfo.id)) { - const oldId = nodeInfo.id - const newId = nextUniqueNodeId() - usedNodeIds.add(newId) - remappedIds.set(oldId, newId) - nodeInfo.id = newId - continue - } - - usedNodeIds.add(nodeInfo.id) - observeNodeId(workingState, toNodeId(nodeInfo.id)) - } - - if (remappedIds.size > 0) { - patchLinkNodeIds(subgraphInfo.links, remappedIds) - subgraphNodeIdMap.set(subgraphInfo.id, remappedIds) - } - } - - 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) - } - - commitLGraphState(rootGraph.state, workingState) -} - -function preflightClipboardItemIds( - parsed: ClipboardItems, - graph: LGraph -): void { - const rootGraph = graph.rootGraph - const workingState = cloneLGraphState(rootGraph.state) - if (parsed.subgraphs?.length) { - const nodeIds = new Set() - forEachNode(rootGraph, (node) => nodeIds.add(node.id)) - normalizeSubgraphDefinitions( - parsed.subgraphs, - { - nodeIds, - groupIds: collectReservedGroupIds(rootGraph), - linkIds: collectReservedLinkIds(rootGraph), - rerouteIds: collectReservedRerouteIds(rootGraph) - }, - workingState - ) - } - - for (let index = 0; index < (parsed.groups?.length ?? 0); index++) - mintGroupId(workingState) - if (!graph.isRootGraph || !isRootGraphDocBound(graph.id)) { - for (let index = 0; index < (parsed.nodes?.length ?? 0); index++) - mintNodeId(workingState) - } - for (let index = 0; index < (parsed.reroutes?.length ?? 0); index++) - mintRerouteId(workingState) - for (let index = 0; index < (parsed.links?.length ?? 0); index++) - mintLinkId(workingState) -} diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 1a1eacc4d7f..e8fefd0e9a2 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -72,6 +72,7 @@ import { resolveInputSlotView } from './node/slotDescriptorView' import { initializeWidgetsView } from './node/widgetsView' +import { collectReservedLinkIds } from './subgraph/subgraphDeduplication' import { extensionConfigureView, hydrateExtensionPayload, @@ -3036,7 +3037,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 @@ -3198,7 +3199,9 @@ export class LGraphNode const maybeCommonType = input.type && output.type && commonType(input.type, output.type) - const linkId = mintLinkId(graph.state) + const linkId = mintLinkId(graph.state, () => + collectReservedLinkIds(graph.rootGraph) + ) const link = new LLink( linkId, diff --git a/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts b/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts index 40fc7720555..1ebe6d6ae0b 100644 --- a/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts +++ b/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts @@ -7,6 +7,7 @@ import { compareNodeIds } from '@/types/nodeId' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { layoutHiddenLinkBadges, queryLinkBadgeAtPoint } from './linkBadges' import { getLinkEndpointPositions } from './linkGeometry' +import { slotTypeKey } from '../utils/type' export function queryHiddenLinkBadgeAtPoint( host: object, @@ -63,7 +64,7 @@ export function layoutGraphLinkBadges( startPos, endPos, (typeof link.color === 'string' && link.color) || - linkTypeColors[link.type] || + linkTypeColors[slotTypeKey(link.type)] || defaultLinkColor ) ) diff --git a/src/lib/litegraph/src/canvas/linkBadges.test.ts b/src/lib/litegraph/src/canvas/linkBadges.test.ts index 99eaaa87213..127b515ed6e 100644 --- a/src/lib/litegraph/src/canvas/linkBadges.test.ts +++ b/src/lib/litegraph/src/canvas/linkBadges.test.ts @@ -57,6 +57,7 @@ describe('linkBadgeText', () => { expected: 'Checkpoint' }, { type: 'MODEL', presentation: {}, expected: 'MODEL' }, + { type: ['IMAGE', 'MASK'], presentation: {}, expected: 'IMAGE,MASK' }, { type: '', presentation: {}, expected: '*' }, { type: -1, presentation: {}, expected: '*' } ] satisfies { diff --git a/src/lib/litegraph/src/canvas/linkBadges.ts b/src/lib/litegraph/src/canvas/linkBadges.ts index 3e72d8c5eff..ec1141cae68 100644 --- a/src/lib/litegraph/src/canvas/linkBadges.ts +++ b/src/lib/litegraph/src/canvas/linkBadges.ts @@ -6,6 +6,7 @@ import type { LLink } from '../LLink' import type { LinkId } from '@/types/linkId' import type { LinkPresentation } from '@/types/linkPresentation' import { overlapBounding } from '../measure' +import { slotTypeKey } from '../utils/type' export const BADGE_GAP = 14 const BADGE_HEIGHT = 18 @@ -57,7 +58,7 @@ export function linkBadgeText( const label = presentation.label?.trim() if (label) return label if (typeof type === 'number') return '*' - return type || '*' + return String(slotTypeKey(type) || '*') } function makeBadge(text: string, color: string): LGraphBadge { diff --git a/src/lib/litegraph/src/idAllocation.property.test.ts b/src/lib/litegraph/src/idAllocation.property.test.ts index 96a115509fa..8492e43bac6 100644 --- a/src/lib/litegraph/src/idAllocation.property.test.ts +++ b/src/lib/litegraph/src/idAllocation.property.test.ts @@ -53,10 +53,10 @@ describe('idAllocation has no collision avoidance against a concurrent external agent.lastNodeId = lastObservedId const frontendIds = Array.from({ length: mintCount }, () => - Number(mintNodeId(frontend)) + Number(mintNodeId(frontend, 'sequential', new Set())) ) const agentIds = Array.from({ length: mintCount }, () => - Number(mintNodeId(agent)) + Number(mintNodeId(agent, 'sequential', new Set())) ) return frontendIds.every((id, index) => id === agentIds[index]) @@ -97,7 +97,9 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen const frontend = createLGraphState() frontend.lastNodeId = lastObservedId - const frontendMintedId = BigInt(mintNodeId(frontend, 'crdt-disjoint')) + const frontendMintedId = BigInt( + mintNodeId(frontend, 'crdt-disjoint', new Set()) + ) return frontendMintedId !== agentId } @@ -113,7 +115,7 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen (mintCount, agentIds) => { const frontend = createLGraphState() const frontendIds = Array.from({ length: mintCount }, () => - BigInt(mintNodeId(frontend, 'crdt-disjoint')) + BigInt(mintNodeId(frontend, 'crdt-disjoint', new Set())) ) const agentIdSet = new Set(agentIds) return frontendIds.every((id) => !agentIdSet.has(id)) @@ -140,7 +142,7 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen fc.property(fc.integer({ min: 1, max: 50 }), (mintCount) => { const frontend = createLGraphState() const ids = Array.from({ length: mintCount }, () => - BigInt(mintNodeId(frontend, 'crdt-disjoint')) + BigInt(mintNodeId(frontend, 'crdt-disjoint', new Set())) ) return ids.every( @@ -158,7 +160,7 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen fc.property(fc.integer({ min: 2, max: 50 }), (mintCount) => { const frontend = createLGraphState() const ids = Array.from({ length: mintCount }, () => - BigInt(mintNodeId(frontend, 'crdt-disjoint')) + BigInt(mintNodeId(frontend, 'crdt-disjoint', new Set())) ) return new Set(ids).size === ids.length diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 83fc862bd09..21e1a1416f5 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -2,8 +2,6 @@ import { describe, expect, it } from 'vitest' import { AGENT_RESERVED_BIT, - cloneLGraphState, - commitLGraphState, createLGraphState, findNextAvailableId, isReservedBitRangeNodeId, @@ -25,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. @@ -78,25 +89,29 @@ describe('idAllocation', () => { name: 'node', observe: (state: ReturnType, value: number) => observeNodeId(state, toNodeId(value)), - mint: mintNodeId + mint: (state: ReturnType) => + mintNodeId(state, 'sequential', new Set()) }, { name: 'group', observe: (state: ReturnType, value: number) => observeGroupId(state, toGroupId(value)), - mint: mintGroupId + mint: (state: ReturnType) => + mintGroupId(state, new Set()) }, { name: 'link', observe: (state: ReturnType, value: number) => observeLinkId(state, toLinkId(value)), - mint: mintLinkId + mint: (state: ReturnType) => + mintLinkId(state, new Set()) }, { name: 'reroute', observe: (state: ReturnType, value: number) => observeRerouteId(state, toRerouteId(value)), - mint: mintRerouteId + mint: (state: ReturnType) => + mintRerouteId(state, new Set()) } ])( 'continues $name allocation above the former limit', @@ -114,7 +129,10 @@ describe('idAllocation', () => { setCounter: (state: ReturnType) => { state.lastNodeId = Number.MAX_SAFE_INTEGER }, - mint: mintNodeId + mint: ( + state: ReturnType, + reservedIds: ReadonlySet + ) => mintNodeId(state, 'sequential', reservedIds) }, { name: 'group', @@ -137,33 +155,50 @@ describe('idAllocation', () => { }, mint: mintRerouteId } - ])('rejects unsafe $name allocation', ({ setCounter, mint }) => { - const state = createLGraphState() - setCounter(state) + ])( + 'wraps $name allocation to an available safe ID', + ({ setCounter, mint }) => { + const state = createLGraphState() + setCounter(state) + + expect(Number(mint(state, new Set([1])))).toBe(2) + } + ) - expect(() => mint(state)).toThrow('ID space exhausted') + it('checks the candidate after the last reserved ID', () => { + expect(findNextAvailableId(new Set([1, 2, 3]), 1)).toBe(4) }) - it('bounds collision searches by the number of reservations', () => { - let calls = 0 + it('skips a reserved ID above a stale counter', () => { + const state = createLGraphState() + state.lastGroupId = 1 - expect(() => - findNextAvailableId(new Set([1, 2]), () => { - calls++ - return 1 - }) - ).toThrow('ID space exhausted') - expect(calls).toBe(3) + expect(mintGroupId(state, new Set([2]))).toBe(3) }) - it('commits speculative counter updates only when requested', () => { + it('collects reservations only when the counter reaches its boundary', () => { const state = createLGraphState() - const workingState = cloneLGraphState(state) - mintNodeId(workingState) + let collections = 0 + const collectReservedIds = () => { + collections++ + return new Set([1]) + } + + expect(mintGroupId(state, collectReservedIds)).toBe(1) + expect(collections).toBe(0) + + state.lastGroupId = Number.MAX_SAFE_INTEGER + expect(mintGroupId(state, collectReservedIds)).toBe(2) + expect(collections).toBe(1) + }) - expect(state.lastNodeId).toBe(0) - commitLGraphState(state, workingState) - expect(state.lastNodeId).toBe(1) + it('wraps a reserved maximum candidate to the first available ID', () => { + expect( + findNextAvailableId( + new Set([Number.MAX_SAFE_INTEGER, 1]), + Number.MAX_SAFE_INTEGER + ) + ).toBe(2) }) describe('isReservedBitRangeNodeId', () => { diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index abc33181234..f23761b8f7f 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -15,12 +15,7 @@ export interface LGraphState { lastRerouteId: RerouteId } -class IdSpaceExhaustedError extends Error { - constructor() { - super('ID space exhausted') - this.name = 'IdSpaceExhaustedError' - } -} +type ReservedIds = ReadonlySet | (() => ReadonlySet) export function createLGraphState(): LGraphState { return { @@ -31,44 +26,36 @@ export function createLGraphState(): LGraphState { } } -export function cloneLGraphState(state: LGraphState): LGraphState { - return { ...state } -} - -export function commitLGraphState( - target: LGraphState, - source: LGraphState -): void { - Object.assign(target, source) -} - -function advanceCounter(counter: number): number { - if ( - !Number.isSafeInteger(counter) || - counter < 0 || - counter === Number.MAX_SAFE_INTEGER - ) { - throw new IdSpaceExhaustedError() +export function findNextAvailableId( + usedIds: ReadonlySet, + startAt = 1 +): number { + let candidate = Number.isSafeInteger(startAt) && startAt > 0 ? startAt : 1 + let attempts = 0 + while (usedIds.has(candidate) && attempts < usedIds.size) { + candidate = candidate === Number.MAX_SAFE_INTEGER ? 1 : candidate + 1 + attempts++ } - return counter + 1 + return candidate } -export function findNextAvailableId( - usedIds: Set, - advance: () => number -): number { - for (let attempts = 0; attempts <= usedIds.size; attempts++) { - const nextId = advance() - if (!Number.isSafeInteger(nextId) || nextId < 0) { - throw new IdSpaceExhaustedError() - } - if (!usedIds.has(nextId)) return nextId +function mintSequentialId(lastId: number, reservedIds: ReservedIds): number { + if (Number.isSafeInteger(lastId) && lastId >= 0) { + const nextId = lastId + 1 + if ( + Number.isSafeInteger(nextId) && + (typeof reservedIds === 'function' || !reservedIds.has(nextId)) + ) + return nextId } - throw new IdSpaceExhaustedError() + const usedIds = + typeof reservedIds === 'function' ? reservedIds() : reservedIds + return findNextAvailableId(usedIds, lastId + 1) } /** - * `'sequential'` (default) is the plain-local `++lastNodeId` counter. + * `'sequential'` advances the local counter, wrapping to an unused safe integer + * at the boundary. * `'crdt-disjoint'` is for a graph bound to the in-app agent's collaborative * doc, where a local mint can otherwise land on an id the agent independently * mints for the same doc — see {@link mintCrdtDisjointNodeId}. @@ -175,25 +162,42 @@ 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 = advanceCounter(state.lastNodeId))) + if (mode === 'crdt-disjoint') return mintCrdtDisjointNodeId() + const id = mintSequentialId(state.lastNodeId, reservedIds) + if (id > state.lastNodeId) state.lastNodeId = id + return toNodeId(id) } -export function mintGroupId(state: LGraphState): GroupId { - return toGroupId((state.lastGroupId = advanceCounter(state.lastGroupId))) +export function mintGroupId( + state: LGraphState, + reservedIds: ReservedIds +): GroupId { + const id = mintSequentialId(state.lastGroupId, reservedIds) + if (id > state.lastGroupId) state.lastGroupId = id + return toGroupId(id) } -export function mintLinkId(state: LGraphState): LinkId { - state.lastLinkId = toLinkId(advanceCounter(Number(state.lastLinkId))) - return state.lastLinkId +export function mintLinkId( + state: LGraphState, + reservedIds: ReservedIds +): LinkId { + const lastLinkId = Number(state.lastLinkId) + const id = mintSequentialId(lastLinkId, reservedIds) + if (id > lastLinkId) state.lastLinkId = toLinkId(id) + return toLinkId(id) } -export function mintRerouteId(state: LGraphState): RerouteId { - state.lastRerouteId = toRerouteId(advanceCounter(Number(state.lastRerouteId))) - return state.lastRerouteId +export function mintRerouteId( + state: LGraphState, + reservedIds: ReservedIds +): RerouteId { + const lastRerouteId = Number(state.lastRerouteId) + const id = mintSequentialId(lastRerouteId, reservedIds) + if (id > lastRerouteId) state.lastRerouteId = toRerouteId(id) + return toRerouteId(id) } export function observeNodeId(state: LGraphState, id: NodeId): void { 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..3cc380db5e4 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInput.ts @@ -19,6 +19,7 @@ import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { SubgraphInputNode } from './SubgraphInputNode' import type { SubgraphOutput } from './SubgraphOutput' import { SubgraphSlot } from './SubgraphSlotBase' +import { collectReservedLinkIds } from './subgraphDeduplication' import { isNodeSlot, isSubgraphOutput } from './subgraphUtils' /** @@ -87,7 +88,9 @@ export class SubgraphInput extends SubgraphSlot { subgraph.beforeChange() } - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId(subgraph.state, () => + collectReservedLinkIds(subgraph.rootGraph) + ) const link = new LLink( linkId, diff --git a/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts b/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts index 8dff9fca585..b5a2072c18b 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts @@ -23,6 +23,7 @@ import { EmptySubgraphInput } from './EmptySubgraphInput' import { SubgraphIONodeBase } from './SubgraphIONodeBase' import type { SubgraphInput } from './SubgraphInput' import type { SubgraphOutput } from './SubgraphOutput' +import { collectReservedLinkIds } from './subgraphDeduplication' export class SubgraphInputNode extends SubgraphIONodeBase @@ -108,7 +109,9 @@ export class SubgraphInputNode if (outputIndex === -1 || inputIndex === -1) throw new Error('Invalid slot indices.') - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId(subgraph.state, () => + collectReservedLinkIds(subgraph.rootGraph) + ) return new LLink( linkId, diff --git a/src/lib/litegraph/src/subgraph/SubgraphOutput.ts b/src/lib/litegraph/src/subgraph/SubgraphOutput.ts index 6d7c548cf8a..c627757509f 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphOutput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphOutput.ts @@ -15,6 +15,7 @@ import { NodeSlotType } from '@/lib/litegraph/src/types/globalEnums' import type { SubgraphInput } from './SubgraphInput' import type { SubgraphOutputNode } from './SubgraphOutputNode' import { SubgraphSlot } from './SubgraphSlotBase' +import { collectReservedLinkIds } from './subgraphDeduplication' import { isNodeSlot, isSubgraphInput } from './subgraphUtils' /** @@ -58,7 +59,9 @@ export class SubgraphOutput extends SubgraphSlot { subgraph.beforeChange() } - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId(subgraph.state, () => + collectReservedLinkIds(subgraph.rootGraph) + ) const link = new LLink( linkId, diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts index 7102039afcd..5f24905342d 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts @@ -389,26 +389,52 @@ describe('normalizeSubgraphDefinitions', () => { expect(state.lastNodeId).toBe(100_000_001) }) - it('does not commit earlier counter updates when a later phase fails', () => { + 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 initialState = { ...state } - - expect(() => - normalizeSubgraphDefinitions( - [subgraph], - { - nodeIds: new Set(), - groupIds: new Set(), - linkIds: new Set([1]), - rerouteIds: new Set() - }, - state - ) - ).toThrow('ID space exhausted') - expect(state).toEqual(initialState) + + const result = normalizeSubgraphDefinitions( + [subgraph], + { + nodeIds: new Set(), + groupIds: new Set(), + linkIds: new Set([1]), + rerouteIds: new Set() + }, + state + ) + + expect(result.subgraphs[0].links![0].id).toBe(toLinkId(2)) + expect(state.lastLinkId).toBe(toLinkId(Number.MAX_SAFE_INTEGER)) + }) + + it('updates every root-level reference to a remapped interior node', () => { + const subgraph = makeSubgraph('sg', ['dummy']) + const rootNode = makeSubgraph('root', ['sg']).nodes![0] + rootNode.properties = { + proxyWidgets: [['1', 'seed']], + previewExposures: [{ sourceNodeId: '1' }] + } + + const result = normalizeSubgraphDefinitions( + [subgraph], + { + nodeIds: new Set([toNodeId(1)]), + groupIds: new Set(), + linkIds: new Set(), + rerouteIds: new Set() + }, + freshState(), + [rootNode] + ) + const remappedId = result.subgraphs[0].nodes![0].id + + expect(result.rootNodes![0].properties).toEqual({ + proxyWidgets: [[String(remappedId), 'seed']], + previewExposures: [{ sourceNodeId: String(remappedId) }] + }) }) }) diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts index ce1b7cb1ccb..ef9044e0c71 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts @@ -2,9 +2,6 @@ import type { LGraph } from '../LGraph' import { isUuidShapedSubgraphId } from '@/schemas/subgraphIdSchema' import { toGroupId } from '@/types/groupId' import { - cloneLGraphState, - commitLGraphState, - findNextAvailableId, mintGroupId, mintLinkId, mintNodeId, @@ -65,29 +62,15 @@ export function normalizeSubgraphDefinitions( clonedSubgraphs[index] = normalizeConfiguredTopology(subgraph) } - const workingState = cloneLGraphState(state) deduplicateClonedSubgraphNodeIds( clonedSubgraphs, reservations.nodeIds, - workingState, + state, clonedRootNodes ) - deduplicateSubgraphGroupIds( - clonedSubgraphs, - reservations.groupIds, - workingState - ) - deduplicateSubgraphLinkIds( - clonedSubgraphs, - reservations.linkIds, - workingState - ) - deduplicateSubgraphRerouteIds( - clonedSubgraphs, - reservations.rerouteIds, - workingState - ) - commitLGraphState(state, workingState) + deduplicateSubgraphGroupIds(clonedSubgraphs, reservations.groupIds, state) + deduplicateSubgraphLinkIds(clonedSubgraphs, reservations.linkIds, state) + deduplicateSubgraphRerouteIds(clonedSubgraphs, reservations.rerouteIds, state) return { subgraphs: clonedSubgraphs, rootNodes: clonedRootNodes } } @@ -297,9 +280,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) @@ -319,6 +300,18 @@ function remapNodeIds( return remappedIds } +export function collectReservedNodeIds( + rootGraph: Pick, + rootNodes: ISerialisedNode[] = [] +): Set { + return new Set([ + ...rootNodes.map((node) => toNodeId(node.id)), + ...[rootGraph, ...rootGraph.subgraphs.values()].flatMap((owner) => + owner.nodes.map((node) => node.id) + ) + ]) +} + /** Parses a serialized node ID as an integer, or `null` when non-numeric. */ function numericSerializedNodeId(id: SerializedNodeId): number | null { const key = toNodeId(id) @@ -390,7 +383,7 @@ export function deduplicateSubgraphLinkIds( const remapped = remapNumericIds( [...(subgraph.links ?? []), ...(subgraph.floatingLinks ?? [])], usedLinkIds, - () => mintLinkId(state), + () => mintLinkId(state, usedLinkIds), (id) => observeLinkId(state, toLinkId(id)), 'link' ) @@ -415,7 +408,7 @@ export function deduplicateSubgraphGroupIds( remapNumericIds( subgraph.groups ?? [], usedGroupIds, - () => mintGroupId(state), + () => mintGroupId(state, usedGroupIds), (id) => observeGroupId(state, toGroupId(id)), 'group' ) @@ -468,7 +461,7 @@ function remapRerouteIds( return remapNumericIds( subgraph.reroutes ?? [], usedRerouteIds, - () => mintRerouteId(state), + () => mintRerouteId(state, usedRerouteIds), (id) => observeRerouteId(state, toRerouteId(id)), 'reroute' ) @@ -486,7 +479,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) @@ -579,7 +572,7 @@ export function topologicalSortSubgraphs( return sorted } -/** Patches legacy proxyWidgets in root-level SubgraphNode instances. */ +/** Patches node references in root-level SubgraphNode instances. */ function patchProxyWidgets( rootNodes: ISerialisedNode[], subgraphIdSet: Set, @@ -591,13 +584,27 @@ function patchProxyWidgets( if (!remappedIds) continue const proxyWidgets = node.properties?.proxyWidgets - if (!Array.isArray(proxyWidgets)) continue + if (Array.isArray(proxyWidgets)) { + 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) + } + } - 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) + const previewExposures = node.properties?.previewExposures + if (!Array.isArray(previewExposures)) continue + for (const entry of previewExposures) { + if ( + typeof entry !== 'object' || + entry === null || + !('sourceNodeId' in entry) || + typeof entry.sourceNodeId !== 'string' + ) + continue + const newId = remappedIds.get(toNodeId(entry.sourceNodeId)) + if (newId !== undefined) entry.sourceNodeId = String(newId) } } } diff --git a/src/lib/litegraph/src/utils/type.ts b/src/lib/litegraph/src/utils/type.ts index cc7b99f5932..ccf9801bb63 100644 --- a/src/lib/litegraph/src/utils/type.ts +++ b/src/lib/litegraph/src/utils/type.ts @@ -39,10 +39,15 @@ export function isNodeBindable(widget: unknown): widget is NodeBindable { ) } +export function slotTypeKey(type: ISlotType): string | number { + return Array.isArray(type) ? type.join(',') : type +} + export function commonType(...types: ISlotType[]): ISlotType | undefined { - if (!isStrings(types)) return undefined + const keys = types.map(slotTypeKey) + if (!isStrings(keys)) return undefined - const withoutWildcards = without(types, '*') + const withoutWildcards = without(keys, '*') if (withoutWildcards.length === 0) return '*' const typeLists: string[][] = withoutWildcards.map((type) => type.split(',')) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.test.ts b/src/platform/workflow/validation/schemas/workflowSchema.test.ts index 8297c4d2427..8f9fa0e50c3 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.test.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.test.ts @@ -1,7 +1,10 @@ import fs from 'fs' import { describe, expect, it } from 'vitest' -import { validateComfyWorkflow } from '@/platform/workflow/validation/schemas/workflowSchema' +import { + validateComfyWorkflow, + zClipboardItems +} from '@/platform/workflow/validation/schemas/workflowSchema' import { defaultGraph } from '@/scripts/defaultGraph' const WORKFLOW_DIR = 'src/platform/workflow/validation/schemas/__fixtures__' @@ -391,3 +394,29 @@ describe('parseComfyWorkflow', () => { }) }) }) + +describe('zClipboardItems', () => { + it('normalizes array-like widget values and preserves array slot types', () => { + const node = JSON.parse(JSON.stringify(defaultGraph.nodes[0])) + node.inputs = [{ name: 'input', type: ['IMAGE', 'MASK'] }] + node.widgets_values = { 0: 'first', 1: 2, length: 2 } + + const result = zClipboardItems.safeParse({ nodes: [node] }) + + expect(result.success).toBe(true) + if (!result.success) throw result.error + expect(result.data.nodes?.[0].inputs?.[0].type).toEqual(['IMAGE', 'MASK']) + expect(result.data.nodes?.[0].widgets_values).toEqual(['first', 2]) + }) + + it('normalizes sparse widget values without allocating their declared length', () => { + const node = JSON.parse(JSON.stringify(defaultGraph.nodes[0])) + node.widgets_values = { 0: 'first', length: Number.MAX_SAFE_INTEGER } + + const result = zClipboardItems.safeParse({ nodes: [node] }) + + expect(result.success).toBe(true) + if (!result.success) throw result.error + expect(result.data.nodes?.[0].widgets_values).toEqual(['first']) + }) +}) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.ts b/src/platform/workflow/validation/schemas/workflowSchema.ts index 18198552502..265dc223e9e 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.ts @@ -2,8 +2,15 @@ import { z } from 'zod' import type { SafeParseReturnType } from 'zod' import { fromZodError } from 'zod-validation-error' import type { RendererType } from '@/lib/litegraph/src/LGraph' -import { parseLinkId } from '@/types/linkId' +import type { + ClipboardItems, + ExportedSubgraph +} from '@/lib/litegraph/src/types/serialisation' +import type { TWidgetValue } from '@/lib/litegraph/src/types/widgets' +import { parseLinkId, toLinkId } from '@/types/linkId' import type { LinkPresentation } from '@/types/linkPresentation' +import type { NodeProperty } from '@/types/nodeState' +import { toRerouteId } from '@/types/rerouteId' const zRendererType = z.enum([ 'LG', @@ -210,6 +217,15 @@ const zVersion = z.union([ z.literal('unknown') ]) +const zNodeProperty = z.union([ + z.string(), + z.number(), + z.boolean(), + z.null(), + z.record(z.unknown()), + z.array(z.unknown()) +]) satisfies z.ZodType + const zProperties = z .object({ ['Node name for S&R']: z.string().optional(), @@ -218,10 +234,25 @@ const zProperties = z ver: zVersion.optional(), models: z.array(zModelFile).optional() }) - .passthrough() + .catchall(zNodeProperty.optional()) const zWidgetValues = z.union([z.array(z.any()), z.record(z.any())]) +function normalizeClipboardWidgetValues( + values: z.output | undefined +): TWidgetValue[] | undefined { + if (!values || Array.isArray(values)) return values + const length = values.length + if (!Number.isSafeInteger(length) || length < 0) return [] + return Object.entries(values) + .filter(([key]) => { + const index = Number(key) + return Number.isInteger(index) && index >= 0 && index < length + }) + .sort(([left], [right]) => Number(left) - Number(right)) + .map(([, value]) => value) +} + const zComfyNode = z .object({ id: zNodeId, @@ -511,15 +542,79 @@ const zSubgraphDefinition = zComfyWorkflow1 }) .passthrough() -export const zClipboardItems = z +const zClipboardNode = zComfyNode.transform((node) => { + return { + ...node, + widgets_values: normalizeClipboardWidgetValues(node.widgets_values) + } +}) + +const zClipboardSubgraphInstance = zSubgraphInstance.transform((node) => { + return { + ...node, + widgets_values: normalizeClipboardWidgetValues(node.widgets_values) + } +}) + +const zClipboardGroup = zGroup.extend({ id: z.number() }) +const zClipboardReroute = zReroute.extend({ linkIds: z.array(z.number()) }) + +interface ClipboardSubgraphInput extends Omit< + z.input, + 'groups' | 'nodes' | 'reroutes' | 'subgraphs' | 'definitions' +> { + groups?: z.input[] + nodes: z.input[] + reroutes?: z.input[] + subgraphs?: z.input[] + definitions?: { subgraphs: ClipboardSubgraphInput[] } +} + +const zClipboardSubgraphDefinition: z.ZodType< + ExportedSubgraph, + z.ZodTypeDef, + ClipboardSubgraphInput +> = zSubgraphDefinition + .extend({ + groups: z.array(zClipboardGroup).optional(), + nodes: z.array(zClipboardNode), + reroutes: z.array(zClipboardReroute).optional(), + subgraphs: z.array(zClipboardSubgraphInstance).optional(), + definitions: z + .object({ + subgraphs: z.lazy(() => z.array(zClipboardSubgraphDefinition)) + }) + .optional() + }) + .transform(({ config, extra, ...subgraph }) => { + const normalizedExtra = extra + ? { + ...extra, + reroutes: extra.reroutes?.map((reroute) => ({ + ...reroute, + linkIds: reroute.linkIds ?? [] + })), + linkExtensions: extra.linkExtensions?.map((link) => ({ + ...link, + id: toLinkId(link.id), + parentId: toRerouteId(link.parentId) + })) + } + : undefined + return { + ...subgraph, + config: config ?? undefined, + extra: normalizedExtra + } + }) + +const zClipboardItemsSchema = z .object({ - nodes: z.array(zComfyNode).optional(), - groups: z.array(zGroup.extend({ id: z.number() })).optional(), - reroutes: z - .array(zReroute.extend({ linkIds: z.array(z.number()) })) - .optional(), + nodes: z.array(zClipboardNode).optional(), + groups: z.array(zClipboardGroup).optional(), + reroutes: z.array(zClipboardReroute).optional(), links: z.array(zComfyLinkObject).optional(), - subgraphs: z.array(zSubgraphDefinition).optional() + subgraphs: z.array(zClipboardSubgraphDefinition).optional() }) .refine( (items) => @@ -530,6 +625,12 @@ export const zClipboardItems = z items.subgraphs !== undefined ) +export const zClipboardItems: z.ZodType< + ClipboardItems, + z.ZodTypeDef, + z.input +> = zClipboardItemsSchema + export type ModelFile = z.infer export type ComfyLinkObject = z.infer export type ComfyNode = z.infer diff --git a/src/stores/entityIdStore.test.ts b/src/stores/entityIdStore.test.ts index 843cbd35649..33019a564c1 100644 --- a/src/stores/entityIdStore.test.ts +++ b/src/stores/entityIdStore.test.ts @@ -13,12 +13,14 @@ describe(useEntityIdStore, () => { it('keeps allocation state when a root graph is rekeyed', () => { const store = useEntityIdStore() const state = store.get(first) - mintNodeId(state) + mintNodeId(state, 'sequential', new Set()) store.rekey(first, second) expect(store.get(second)).toBe(state) - expect(Number(mintNodeId(store.get(second)))).toBe(2) + expect(Number(mintNodeId(store.get(second), 'sequential', new Set()))).toBe( + 2 + ) }) it('replaces compatibility state without sharing the caller object', () => { diff --git a/src/workbench/extensions/agent/crdt/linkMintPort.ts b/src/workbench/extensions/agent/crdt/linkMintPort.ts index 613fd5ce269..17014076898 100644 --- a/src/workbench/extensions/agent/crdt/linkMintPort.ts +++ b/src/workbench/extensions/agent/crdt/linkMintPort.ts @@ -9,6 +9,8 @@ */ import type { NodeId as WireNodeId } from '@comfyorg/comfy-multi-player' +import type { ISlotType } from '@/lib/litegraph/src/interfaces' + import type { GraphOperation } from './graphOperations' import { shouldMint } from './mintGate' import type { MintSession } from './mintSession' @@ -30,7 +32,7 @@ export interface LinkTopologyView { originSlot: number targetNodeId: string | number targetSlot: number - type: string | number + type: ISlotType } interface LinkEventFeed { From f7cbddc540f2de4e80a8d76d9090359cf85425a8 Mon Sep 17 00:00:00 2001 From: Amp Date: Mon, 28 Sep 2026 07:51:39 +0000 Subject: [PATCH 11/11] refactor: resolve fallow complexity findings Amp-Thread-ID: https://ampcode.com/threads/T-01a0d0b9-b759-73a9-833a-e61f0faadb1b --- src/lib/litegraph/src/LGraphCanvas.ts | 506 ++++++++++-------- .../litegraph/src/subgraph/SubgraphInput.ts | 84 +-- .../src/subgraph/subgraphDeduplication.ts | 68 ++- 3 files changed, 376 insertions(+), 282 deletions(-) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index f14d1bbd68e..3691db9e0fb 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -287,6 +287,20 @@ interface ClipboardPasteResult { subgraphs: Map } +type InitializedClipboardItems = { + [Key in keyof ClipboardItems]-?: NonNullable +} + +interface ClipboardPasteContext { + connectInputs: boolean + dx: number + dy: number + graph: LGraph + items: InitializedClipboardItems + result: ClipboardPasteResult + targetSlotByLink: Map +} + /** Legacy selection views derived from the selection store. */ interface SelectionView { items: SelectedItemsView @@ -4257,7 +4271,7 @@ export class LGraphCanvas implements CustomEventDispatcher } _deserializeItems( - parsed: ClipboardItems, + clipboardItems: ClipboardItems, options: IPasteFromClipboardOptions ): ClipboardPasteResult | undefined { const { connectInputs = false, position = this.graph_mouse } = options @@ -4275,233 +4289,40 @@ export class LGraphCanvas implements CustomEventDispatcher this.emitBeforeChange() try { - return this._deserializeItemsBody(parsed, graph, connectInputs, position) + const items = initializeClipboardItems(clipboardItems) + const [offsetX, offsetY] = clipboardOffset(items) + const result = createClipboardPasteResult() + const context: ClipboardPasteContext = { + connectInputs, + dx: position[0] - offsetX, + dy: position[1] - offsetY, + graph, + items, + result, + targetSlotByLink: new Map() + } + + remapClipboardSubgraphIds(items) + createClipboardSubgraphs(context) + createClipboardGroups(context) + createClipboardNodes(context) + createClipboardReroutes(context) + createClipboardLinks(context) + removeUnusedClipboardReroutes(context) + positionClipboardItems(context) + updateClipboardNodeLayout(context) + + this.selectItems(result.created) + forEachNode(graph, (n) => n.onGraphConfigured?.()) + forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) + + return result } finally { graph.afterChange() this.emitAfterChange() } } - private _deserializeItemsBody( - clipboardItems: ClipboardItems, - graph: LGraph, - connectInputs: boolean, - position: Point - ): ClipboardPasteResult | undefined { - const parsed = structuredClone(clipboardItems) - - // 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] - // Subgraphs - const subgraphs = graph.createSubgraphs(parsed.subgraphs, parsed.nodes) - for (const subgraph of subgraphs) - results.subgraphs.set(subgraph.id, subgraph) - - // Groups - for (const info of parsed.groups) { - info.id = -1 - - const group = new LGraphGroup() - group.configure(info) - graph.add(group) - created.push(group) - } - - // Nodes - const dx = position[0] - offsetX - const dy = position[1] - offsetY - const targetSlotByLink = new Map() - for (const info of parsed.nodes) { - const node = LiteGraph.createNode(info.type) - if (!node) { - // failedNodes.push(info) - continue - } - - nodes.set(serializeNodeId(info.id), node) - info.id = -1 - - const linkByInputName = detachSerialisedLinks(info) - // `add` snapshots the position into the layout store; configure runs after. - node.pos = [info.pos[0] + dx, info.pos[1] + dy] - graph.add(node) - node.configure(info) - - // `configure` overrides may reorder inputs to match the node definition, - // moving the slot each serialized link targets. - for (const [slot, input] of (info.inputs ?? []).entries()) { - const linkId = linkByInputName.get(input.name) - if (linkId != null && !targetSlotByLink.has(linkId)) - targetSlotByLink.set(linkId, slot) - } - - if (node instanceof SubgraphNode) { - if ( - node.properties.proxyWidgets !== undefined && - LiteGraph.LGraph.proxyWidgetMigrationFlush - ) { - LiteGraph.LGraph.proxyWidgetMigrationFlush(node, info) - } - LiteGraph.LGraph.autoExposePreviewNodes?.(node) - } - - created.push(node) - } - - // Reroutes - for (const info of parsed.reroutes) { - const { id, ...rerouteInfo } = info - - const reroute = graph.setReroute(rerouteInfo) - if (!reroute) continue - created.push(reroute) - reroutes.set(toRerouteId(id), reroute) - } - - // Remap reroute parentIds for pasted reroutes - for (const reroute of reroutes.values()) { - if (reroute.parentId == null) continue - - const mapped = reroutes.get(reroute.parentId) - if (mapped) reroute.parentId = mapped.id - } - - // Links - for (const info of parsed.links) { - // Find the copied node / reroute ID - let outNode: LGraphNode | null | undefined = nodes.get( - serializeNodeId(info.origin_id) - ) - let afterRerouteId: RerouteId | undefined - if (info.parentId != null) - afterRerouteId = reroutes.get(toRerouteId(info.parentId))?.id - - // If it wasn't copied, use the original graph value - if ( - connectInputs && - LiteGraph.ctrl_shift_v_paste_connect_unselected_outputs - ) { - const originNodeId = parseNodeId(info.origin_id) - outNode ??= originNodeId ? graph.getNodeById(originNodeId) : null - if (info.parentId !== undefined) { - afterRerouteId ??= toRerouteId(info.parentId) - } - } - - const inNode = nodes.get(serializeNodeId(info.target_id)) - if (inNode) { - const link = outNode?.connect( - info.origin_slot, - inNode, - targetSlotByLink.get(toLinkId(info.id)) ?? info.target_slot, - afterRerouteId - ) - if (link) { - transferLinkPresentation( - graphScopeOf(graph), - { - hidden: info.hidden === true, - label: typeof info.label === 'string' ? info.label : undefined - }, - link.id - ) - links.set(toLinkId(info.id), link) - } - } - } - - // Remove reroutes that no pasted link passes through - for (const [sourceId, reroute] of reroutes) { - if (reroute.totalLinks === 0) { - graph.removeReroute(reroute.id) - reroutes.delete(sourceId) - - const index = created.indexOf(reroute) - if (index !== -1) created.splice(index, 1) - } - } - - // Children of pasted groups are in `created` already, so skip them here. - for (const item of created) { - // Repositioning a paste is not a user drag, so it ignores the pin. - if (item instanceof LGraphNode) - item.setPos(item.pos[0] + dx, item.pos[1] + dy) - else item.move(dx, dy, true) - } - - // TODO: Report failures, i.e. `failedNodes` - - const newPositions = created - .filter((item): item is LGraphNode => item instanceof LGraphNode) - .map((node) => ({ - nodeId: node.id, - bounds: { - x: node.pos[0], - y: node.pos[1], - width: node.size[0], - height: node.size[1] - } - })) - - const rootGraphId = graph.rootGraph.id - layoutStore.batchUpdateNodeBounds(rootGraphId, newPositions, { - source: LayoutSource.Canvas - }) - - // Bring cloned/pasted nodes to front so they render above the originals - const { setNodeZIndex } = useLayoutMutations(LayoutSource.Canvas) - for (const { nodeId } of newPositions) { - setNodeZIndex(rootGraphId, nodeId, layoutStore.allocateZIndex()) - } - - this.selectItems(created) - forEachNode(graph, (n) => n.onGraphConfigured?.()) - forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) - - return results - } - pasteFromClipboard(options: IPasteFromClipboardOptions = {}): void { this._pasteFromClipboard(options) } @@ -8973,6 +8794,247 @@ export class LGraphCanvas implements CustomEventDispatcher } } +function initializeClipboardItems( + clipboardItems: ClipboardItems +): InitializedClipboardItems { + const items = structuredClone(clipboardItems) + return { + groups: items.groups ?? [], + links: items.links ?? [], + nodes: items.nodes ?? [], + reroutes: items.reroutes ?? [], + subgraphs: items.subgraphs ?? [] + } +} + +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 createClipboardPasteResult(): ClipboardPasteResult { + return { + created: [], + links: new Map(), + nodes: new Map(), + reroutes: new Map(), + subgraphs: new Map() + } +} + +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 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 createClipboardSubgraphs(context: ClipboardPasteContext): void { + const { graph, items, result } = context + for (const subgraph of graph.createSubgraphs(items.subgraphs, items.nodes)) { + result.subgraphs.set(subgraph.id, subgraph) + } +} + +function createClipboardGroups(context: ClipboardPasteContext): void { + const { graph, items, result } = context + for (const info of items.groups) { + info.id = -1 + const group = new LGraphGroup() + group.configure(info) + graph.add(group) + result.created.push(group) + } +} + +function createClipboardNodes(context: ClipboardPasteContext): void { + const { dx, dy, graph, items, result, targetSlotByLink } = context + for (const info of items.nodes) { + const node = LiteGraph.createNode(info.type) + if (!node) continue + + result.nodes.set(serializeNodeId(info.id), node) + info.id = -1 + const linkByInputName = detachSerialisedLinks(info) + node.pos = [info.pos[0] + dx, info.pos[1] + dy] + graph.add(node) + node.configure(info) + + recordClipboardTargetSlots(info, linkByInputName, targetSlotByLink) + configurePastedSubgraphNode(node, info) + result.created.push(node) + } +} + +function recordClipboardTargetSlots( + info: ISerialisedNode, + linkByInputName: Map, + targetSlotByLink: Map +): void { + for (const [slot, input] of (info.inputs ?? []).entries()) { + const linkId = linkByInputName.get(input.name) + if (linkId != null && !targetSlotByLink.has(linkId)) { + targetSlotByLink.set(linkId, slot) + } + } +} + +function configurePastedSubgraphNode( + node: LGraphNode, + info: ISerialisedNode +): void { + if (!(node instanceof SubgraphNode)) return + + const flushProxyWidgets = LiteGraph.LGraph.proxyWidgetMigrationFlush + if (node.properties.proxyWidgets !== undefined && flushProxyWidgets) { + flushProxyWidgets(node, info) + } + LiteGraph.LGraph.autoExposePreviewNodes?.(node) +} + +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 + } +} + +function createClipboardLinks(context: ClipboardPasteContext): void { + for (const info of context.items.links) createClipboardLink(context, info) +} + +function createClipboardLink( + context: ClipboardPasteContext, + info: InitializedClipboardItems['links'][number] +): void { + const { graph, result, targetSlotByLink } = context + const targetNode = result.nodes.get(serializeNodeId(info.target_id)) + if (!targetNode) return + + const [originNode, afterRerouteId] = clipboardLinkOrigin(context, info) + const link = originNode?.connect( + info.origin_slot, + targetNode, + targetSlotByLink.get(toLinkId(info.id)) ?? info.target_slot, + afterRerouteId + ) + if (!link) return + + transferLinkPresentation( + graphScopeOf(graph), + { + hidden: info.hidden === true, + label: typeof info.label === 'string' ? info.label : undefined + }, + link.id + ) + result.links.set(toLinkId(info.id), link) +} + +function 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) + } + } + + return [originNode, afterRerouteId] +} + +function removeUnusedClipboardReroutes(context: ClipboardPasteContext): void { + const { graph, result } = context + for (const [sourceId, reroute] of result.reroutes) { + if (reroute.totalLinks !== 0) continue + + graph.removeReroute(reroute.id) + result.reroutes.delete(sourceId) + const index = result.created.indexOf(reroute) + if (index !== -1) result.created.splice(index, 1) + } +} + +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) + } + } +} + +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 diff --git a/src/lib/litegraph/src/subgraph/SubgraphInput.ts b/src/lib/litegraph/src/subgraph/SubgraphInput.ts index 3cc380db5e4..860ab5bf677 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInput.ts @@ -108,44 +108,60 @@ export class SubgraphInput extends SubgraphSlot { } subgraph._addLink(link) - if (existingLink) { - this.parent._disconnectNodeInput(node, slot, existingLink) - if (subgraph.getLink(link.id) !== link) { - subgraph.afterChange() - return - } - } + return this.finishConnection( + link, + existingLink, + slot, + node, + inputIndex, + inputWidget + ) + } - this.linkIds.push(link.id) - anchorRerouteChain(subgraph, link) - subgraph.incrementVersion() - - if (inputWidget) { - // Keep the widget reference in sync with the active upstream widget. - // Stale references can appear across nested promotion rebinds. - this._widget = inputWidget - this.events.dispatch('input-connected', { - input: slot, - widget: inputWidget, - node - }) - } else { - this.events.dispatch('input-connected', { input: slot }) - } - if (subgraph.getLink(link.id) !== link) { - subgraph.afterChange() - return - } + private finishConnection( + link: LLink, + existingLink: LLink | undefined, + slot: INodeInputSlot, + node: LGraphNode, + inputIndex: number, + inputWidget: IBaseWidget | undefined + ): LLink | undefined { + const { subgraph } = this.parent + try { + if (existingLink) { + this.parent._disconnectNodeInput(node, slot, existingLink) + if (subgraph.getLink(link.id) !== link) return + } - node.onConnectionsChange?.(NodeSlotType.INPUT, inputIndex, true, link, slot) - if (subgraph.getLink(link.id) !== link) { + this.linkIds.push(link.id) + anchorRerouteChain(subgraph, link) + subgraph.incrementVersion() + + if (inputWidget) { + this._widget = inputWidget + this.events.dispatch('input-connected', { + input: slot, + widget: inputWidget, + node + }) + } else { + this.events.dispatch('input-connected', { input: slot }) + } + if (subgraph.getLink(link.id) !== link) return + + node.onConnectionsChange?.( + NodeSlotType.INPUT, + inputIndex, + true, + link, + slot + ) + if (subgraph.getLink(link.id) !== link) return + + return link + } finally { subgraph.afterChange() - return } - - subgraph.afterChange() - - return link } get labelPos(): Point { diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts index ef9044e0c71..325f29042d3 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts @@ -20,6 +20,7 @@ import { toNodeId } from '@/types/nodeId' import type { NodeId, SerializedNodeId } from '@/types/nodeId' import { toLinkId } from '@/types/linkId' import { toRerouteId } from '@/types/rerouteId' +import type { NodeProperty } from '@/types/nodeState' import { createUuidv4 } from '@/utils/uuid' import type { ExportedSubgraph, @@ -252,11 +253,15 @@ function deduplicateClonedSubgraphNodeIds( } for (const subgraph of clonedSubgraphs) { - patchProxyWidgets(subgraph.nodes ?? [], subgraphIdSet, remapBySubgraph) + patchSubgraphNodeReferences( + subgraph.nodes ?? [], + subgraphIdSet, + remapBySubgraph + ) } if (clonedRootNodes) { - patchProxyWidgets(clonedRootNodes, subgraphIdSet, remapBySubgraph) + patchSubgraphNodeReferences(clonedRootNodes, subgraphIdSet, remapBySubgraph) } } @@ -572,8 +577,7 @@ export function topologicalSortSubgraphs( return sorted } -/** Patches node references in root-level SubgraphNode instances. */ -function patchProxyWidgets( +function patchSubgraphNodeReferences( rootNodes: ISerialisedNode[], subgraphIdSet: Set, remapBySubgraph: Map> @@ -583,28 +587,40 @@ function patchProxyWidgets( const remappedIds = remapBySubgraph.get(node.type) if (!remappedIds) continue - const proxyWidgets = node.properties?.proxyWidgets - if (Array.isArray(proxyWidgets)) { - 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) - } - } + patchProxyWidgetReferences(node.properties?.proxyWidgets, remappedIds) + patchPreviewExposureReferences( + node.properties?.previewExposures, + remappedIds + ) + } +} - const previewExposures = node.properties?.previewExposures - if (!Array.isArray(previewExposures)) continue - for (const entry of previewExposures) { - 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) - } +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) } }