diff --git a/browser_tests/fixtures/helpers/SubgraphHelper.ts b/browser_tests/fixtures/helpers/SubgraphHelper.ts index dce472a3634..24500f57863 100644 --- a/browser_tests/fixtures/helpers/SubgraphHelper.ts +++ b/browser_tests/fixtures/helpers/SubgraphHelper.ts @@ -18,6 +18,14 @@ import { SubgraphSlotReference } from '@e2e/fixtures/utils/litegraphUtils' import { getAllHostPromotedWidgets } from '@e2e/fixtures/utils/promotedWidgets' import type { PromotedWidgetEntry } from '@e2e/fixtures/utils/promotedWidgets' +interface SubgraphDefinitionRow { + name: string + nodes: number + links: number +} + +type SubgraphDefinitionInventory = SubgraphDefinitionRow[] + export class SubgraphHelper { public readonly editor: SubgraphEditor @@ -586,6 +594,66 @@ export class SubgraphHelper { return id } + /** Descends `levels` times, requiring exactly one subgraph node at each level. */ + async descendSubgraphs(levels: number): Promise { + for (let level = 0; level < levels; level++) { + await expect.poll(() => this.countSubgraphNodes()).toBe(1) + const parentGraphId = await this.getActiveGraphId() + await this.enterSubgraphWithFallback(await this.findSubgraphNodeId()) + await expect.poll(() => this.getActiveGraphId()).not.toBe(parentGraphId) + } + } + + /** Number of subgraph host nodes in the graph currently shown on the canvas. */ + async countSubgraphNodes(): Promise { + return this.page.evaluate( + () => + window.app!.canvas.graph!.nodes.filter((node) => node.isSubgraphNode()) + .length + ) + } + + /** + * Interior node and link counts of every subgraph definition, one row per + * definition sorted by name, then node count, then link count. Definitions + * sharing a name stay as separate rows, because `Convert to Subgraph` names + * every new definition 'New Subgraph'. + */ + async getDefinitionInventory(): Promise { + return this.page.evaluate(() => + [...window.app!.rootGraph.subgraphs.values()] + .map((definition) => ({ + name: definition.name, + nodes: definition.nodes.length, + links: definition.links.size + })) + .sort( + (a, b) => + a.name.localeCompare(b.name) || + a.nodes - b.nodes || + a.links - b.links + ) + ) + } + + /** Same inventory, read back from a serialized workflow rather than live state. */ + async getSerializedDefinitionInventory(): Promise { + return this.page.evaluate(() => + (window.app!.rootGraph.asSerialisable().definitions?.subgraphs ?? []) + .map((definition) => ({ + name: definition.name, + nodes: definition.nodes?.length ?? 0, + links: definition.links?.length ?? 0 + })) + .sort( + (a, b) => + a.name.localeCompare(b.name) || + a.nodes - b.nodes || + a.links - b.links + ) + ) + } + async getBoundaryLinkSnapshot() { return this.page.evaluate(() => { const graph = window.app!.graph diff --git a/browser_tests/tests/subgraph/subgraphWrapNestedDefinition.spec.ts b/browser_tests/tests/subgraph/subgraphWrapNestedDefinition.spec.ts new file mode 100644 index 00000000000..d7751370185 --- /dev/null +++ b/browser_tests/tests/subgraph/subgraphWrapNestedDefinition.spec.ts @@ -0,0 +1,101 @@ +import { expect } from '@playwright/test' + +import { comfyPageFixture as test } from '@e2e/fixtures/ComfyPage' + +test.use({ initialSettings: { 'Comfy.UseNewMenu': 'Disabled' } }) + +test.describe( + 'Wrapping a subgraph host that owns nested definitions', + { tag: ['@subgraph', '@slow'] }, + () => { + const WRAPPED_SELECTION = ['subgraph 2', 'VAE Decode', 'Save Image'] + + const NESTED_DEFINITIONS_INTACT = [ + { name: 'subgraph 2', nodes: 3, links: 7 }, + { name: 'subgraph 3', nodes: 3, links: 6 } + ] + + // The wrapper's 4th link is the boundary link feeding Save Image's + // filename_prefix widget, which conversion promotes to a wrapper input. + const NESTED_DEFINITIONS_AFTER_WRAP = [ + { name: 'New Subgraph', nodes: 3, links: 4 }, + ...NESTED_DEFINITIONS_INTACT + ] + + test.beforeEach(async ({ comfyPage }) => { + await comfyPage.workflow.loadWorkflow('subgraphs/nested-subgraph') + }) + + test('keeps the wrapped and nested definitions populated', async ({ + comfyPage + }) => { + await expect + .poll(() => comfyPage.subgraph.getDefinitionInventory()) + .toEqual(NESTED_DEFINITIONS_INTACT) + + await test.step('Wrap the subgraph host together with its siblings', async () => { + await comfyPage.nodeOps.selectNodes(WRAPPED_SELECTION) + expect( + [...(await comfyPage.nodeOps.getSelectedNodeIds())].sort(), + 'all three nodes must be selected, or the conversion under test is not the one being asserted' + ).toEqual(['10', '8', '9']) + + const host = await comfyPage.nodeOps.getNodeRefById('10') + await host.convertToSubgraph() + }) + + await expect + .poll(() => comfyPage.subgraph.getDefinitionInventory()) + .toEqual(NESTED_DEFINITIONS_AFTER_WRAP) + }) + + test('keeps the nested definitions populated in the saved workflow', async ({ + comfyPage + }) => { + await test.step('Wrap the subgraph host together with its siblings', async () => { + await comfyPage.nodeOps.selectNodes(WRAPPED_SELECTION) + const host = await comfyPage.nodeOps.getNodeRefById('10') + await host.convertToSubgraph() + }) + + await comfyPage.subgraph.serializeAndReload() + + await expect + .poll(() => comfyPage.subgraph.getSerializedDefinitionInventory()) + .toEqual(NESTED_DEFINITIONS_AFTER_WRAP) + }) + + test( + 'still renders the nested interior after wrapping', + { tag: ['@vue-nodes'] }, + async ({ comfyPage }) => { + await test.step('Wrap the subgraph host together with its siblings', async () => { + await comfyPage.nodeOps.selectNodes(WRAPPED_SELECTION) + const host = await comfyPage.nodeOps.getNodeRefById('10') + await host.convertToSubgraph() + }) + + await test.step('Descend through the wrapper into the deepest nested subgraph', async () => { + await comfyPage.subgraph.descendSubgraphs(3) + }) + + await test.step('Assert the deepest interior still renders its three nodes', async () => { + await expect(comfyPage.vueNodes.nodes).toHaveCount(3) + await expect( + comfyPage.vueNodes.getNodeByTitle('Load Checkpoint') + ).toBeVisible() + await expect( + comfyPage.vueNodes.getNodeByTitle('Empty Latent Image') + ).toBeVisible() + await expect( + comfyPage.vueNodes.getNodeByTitle('CLIP Text Encode (Prompt)') + ).toBeVisible() + await comfyPage.attachScreenshot( + 'nested-subgraph-interior-after-wrap', + { runInCI: true } + ) + }) + } + ) + } +) diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index a633f0bc68b..0258d9e2bb5 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -140,6 +140,7 @@ import { import type { UnpackedTargetInput } from './subgraph/unpackSubgraph' import { findUnresolvableSubgraphLink, + findOrphanedSubgraphs, findReleasableSubgraphs, findUsedSubgraphIds, getBoundaryLinks, @@ -234,6 +235,8 @@ export interface GraphRemoveOptions { * Same-id replacement state is left intact. */ preserveCanonicalState?: boolean + /** Keep the subgraph definitions the node references; the caller re-creates the node elsewhere in the same root graph. */ + preserveSubgraphDefinitions?: boolean } export interface LGraphExtra extends Dictionary { @@ -1495,7 +1498,7 @@ export class LGraph } } - if (node.isSubgraphNode()) { + if (node.isSubgraphNode() && !options.preserveSubgraphDefinitions) { this.releaseSubgraphs(findReleasableSubgraphs(this.rootGraph, node)) } @@ -2216,11 +2219,18 @@ export class LGraph resolved.inputNode ) - for (const node of nodes) this.remove(node) + for (const node of nodes) + this.remove(node, { preserveSubgraphDefinitions: true }) for (const reroute of reroutes) this.removeReroute(reroute.id) for (const group of groups) this.remove(group) - const subgraph = this.createSubgraph(data) + let subgraph: Subgraph + try { + subgraph = this.createSubgraph(data) + } catch (error) { + this.releaseSubgraphs(findOrphanedSubgraphs(this.rootGraph, nodes)) + throw error + } for (const node of subgraph.nodes) node.onGraphConfigured?.() for (const node of subgraph.nodes) node.onAfterGraphConfigured?.() diff --git a/src/lib/litegraph/src/subgraph/SubgraphConversion.nestedDefinitions.test.ts b/src/lib/litegraph/src/subgraph/SubgraphConversion.nestedDefinitions.test.ts new file mode 100644 index 00000000000..3692c7f7790 --- /dev/null +++ b/src/lib/litegraph/src/subgraph/SubgraphConversion.nestedDefinitions.test.ts @@ -0,0 +1,222 @@ +import { + assert, + beforeEach, + describe, + expect, + it, + onTestFinished, + vi +} from 'vitest' + +import type { + LGraph, + Positionable, + Subgraph, + SubgraphNode +} from '@/lib/litegraph/src/litegraph' +import { LiteGraph } from '@/lib/litegraph/src/litegraph' +import { createTestNode } from '@/lib/litegraph/src/__fixtures__/nodeHelpers' +import { reportError } from '@/platform/telemetry/reportError' + +import { + createTestRootGraph, + enableSubgraphNodeCreation, + resetSubgraphFixtureState +} from './__fixtures__/subgraphHelpers' + +vi.mock(import('@/platform/telemetry/reportError'), () => ({ + reportError: vi.fn() +})) + +beforeEach(() => { + resetSubgraphFixtureState() +}) + +interface DefinitionHealth { + nodes: number + links: number + serialisedNodes: number + serialisedLinks: number + reportedErrors: number +} + +function captureHealth( + rootGraph: LGraph, + definition: Subgraph +): DefinitionHealth { + const serialised = rootGraph + .asSerialisable() + .definitions?.subgraphs?.find(({ id }) => id === definition.id) + + return { + nodes: definition.nodes.length, + links: definition.links.size, + serialisedNodes: serialised?.nodes?.length ?? 0, + serialisedLinks: serialised?.links?.length ?? 0, + reportedErrors: vi.mocked(reportError).mock.calls.length + } +} + +function buildSubgraphWithTwoLinkedInteriorNodes(parent: LGraph): { + subgraph: Subgraph + node: SubgraphNode +} { + const origin = createTestNode(parent, [], ['number']) + const target = createTestNode(parent, ['number']) + origin.connect(0, target, 0) + return parent.convertToSubgraph(new Set([origin, target])) +} + +describe('Convert to Subgraph with a nested subgraph host in the selection', () => { + it('keeps the nested definition serialisable after its only host moves into the new subgraph', () => { + const rootGraph = createTestRootGraph() + onTestFinished(enableSubgraphNodeCreation(rootGraph)) + + const origin = createTestNode(rootGraph, [], ['number']) + const target = createTestNode(rootGraph, ['number']) + origin.connect(0, target, 0) + const { subgraph: inner, node: innerHost } = rootGraph.convertToSubgraph( + new Set([origin, target]) + ) + const sibling = createTestNode(rootGraph) + + const { subgraph: outer } = rootGraph.convertToSubgraph( + new Set([innerHost, sibling]) + ) + + const serialized = rootGraph.serialize() + + expect(reportError).not.toHaveBeenCalled() + const definitions = new Map( + (serialized.definitions?.subgraphs ?? []).map((s) => [s.id, s]) + ) + const innerDefinition = definitions.get(inner.id) + const outerDefinition = definitions.get(outer.id) + assert(innerDefinition?.nodes) + assert(outerDefinition?.nodes) + expect(innerDefinition.nodes.map((node) => node.type).sort()).toEqual( + [origin.type, target.type].sort() + ) + expect(outerDefinition.nodes.map((node) => node.type)).toContain(inner.id) + }) + + it.for([ + { + shape: 'host selected on its own', + wrap: (rootGraph: LGraph, host: SubgraphNode) => { + rootGraph.convertToSubgraph(new Set([host])) + } + }, + { + shape: 'host selected alongside a plain sibling', + wrap: (rootGraph: LGraph, host: SubgraphNode) => { + const sibling = createTestNode(rootGraph) + rootGraph.convertToSubgraph(new Set([host, sibling])) + } + }, + { + shape: 'host wrapped twice in a row', + wrap: (rootGraph: LGraph, host: SubgraphNode) => { + const { node: wrapper } = rootGraph.convertToSubgraph( + new Set([host]) + ) + rootGraph.convertToSubgraph(new Set([wrapper])) + } + }, + { + shape: 'host wrapped from inside the subgraph that owns it', + wrap: (rootGraph: LGraph, host: SubgraphNode) => { + const { subgraph: middle } = rootGraph.convertToSubgraph( + new Set([host]) + ) + const relocatedHost = middle.nodes.find((node) => node.isSubgraphNode()) + assert(relocatedHost) + middle.convertToSubgraph(new Set([relocatedHost])) + } + } + ])( + 'keeps the wrapped definition intact when the $shape', + ({ wrap }, { expect }) => { + const rootGraph = createTestRootGraph() + onTestFinished(enableSubgraphNodeCreation(rootGraph)) + const { subgraph: inner, node: innerHost } = + buildSubgraphWithTwoLinkedInteriorNodes(rootGraph) + + wrap(rootGraph, innerHost) + + expect(captureHealth(rootGraph, inner)).toEqual({ + nodes: 2, + links: 1, + serialisedNodes: 2, + serialisedLinks: 1, + reportedErrors: 0 + }) + } + ) + + it('keeps every level of a three-deep nest intact when the outermost host is wrapped', () => { + const rootGraph = createTestRootGraph() + onTestFinished(enableSubgraphNodeCreation(rootGraph)) + + const { subgraph: leaf, node: leafHost } = + buildSubgraphWithTwoLinkedInteriorNodes(rootGraph) + const middleOrigin = createTestNode(rootGraph, [], ['number']) + const middleTarget = createTestNode(rootGraph, ['number']) + middleOrigin.connect(0, middleTarget, 0) + const { subgraph: middle, node: middleHost } = rootGraph.convertToSubgraph( + new Set([leafHost, middleOrigin, middleTarget]) + ) + + rootGraph.convertToSubgraph( + new Set([middleHost, createTestNode(rootGraph)]) + ) + + expect({ + leaf: captureHealth(rootGraph, leaf), + middle: captureHealth(rootGraph, middle) + }).toEqual({ + leaf: { + nodes: 2, + links: 1, + serialisedNodes: 2, + serialisedLinks: 1, + reportedErrors: 0 + }, + middle: { + nodes: 3, + links: 1, + serialisedNodes: 3, + serialisedLinks: 1, + reportedErrors: 0 + } + }) + }) +}) + +describe('Convert to Subgraph failing after the originals are removed', () => { + it('releases only the definitions no live node still references', () => { + const rootGraph = createTestRootGraph() + onTestFinished(enableSubgraphNodeCreation(rootGraph)) + const { subgraph: orphaned, node: orphanedHost } = + buildSubgraphWithTwoLinkedInteriorNodes(rootGraph) + const { subgraph: shared, node: sharedHost } = + buildSubgraphWithTwoLinkedInteriorNodes(rootGraph) + const survivingSharedHost = LiteGraph.createNode(shared.id) + assert(survivingSharedHost) + rootGraph.add(survivingSharedHost) + vi.spyOn(rootGraph, 'createSubgraph').mockImplementationOnce(() => { + throw new Error('boom') + }) + + expect(() => + rootGraph.convertToSubgraph( + new Set([orphanedHost, sharedHost]) + ) + ).toThrow('boom') + + expect({ + orphaned: rootGraph.subgraphs.has(orphaned.id), + shared: rootGraph.subgraphs.has(shared.id) + }).toEqual({ orphaned: false, shared: true }) + }) +}) diff --git a/src/lib/litegraph/src/subgraph/subgraphUtils.ts b/src/lib/litegraph/src/subgraph/subgraphUtils.ts index 7a199e75bad..4de9b1bad6f 100644 --- a/src/lib/litegraph/src/subgraph/subgraphUtils.ts +++ b/src/lib/litegraph/src/subgraph/subgraphUtils.ts @@ -564,6 +564,19 @@ export function findReleasableSubgraphs( return removedSubtree.filter((subgraph) => !liveIds.has(subgraph.id)) } +export function findOrphanedSubgraphs( + rootGraph: LGraph, + removedNodes: Iterable +): Subgraph[] { + const orphaned = new Map() + for (const node of removedNodes) { + if (!node.isSubgraphNode()) continue + for (const subgraph of findReleasableSubgraphs(rootGraph, node)) + orphaned.set(subgraph.id, subgraph) + } + return [...orphaned.values()] +} + function reorderInPlace(arr: unknown[], indices: readonly number[]): void { arr.splice(0, arr.length, ...indices.flatMap((i) => arr[i] ?? [])) } diff --git a/src/renderer/core/layout/operations/subgraphConversionLayout.test.ts b/src/renderer/core/layout/operations/subgraphConversionLayout.test.ts new file mode 100644 index 00000000000..aaa8a86dff3 --- /dev/null +++ b/src/renderer/core/layout/operations/subgraphConversionLayout.test.ts @@ -0,0 +1,66 @@ +import { beforeEach, describe, expect, it, onTestFinished } from 'vitest' + +import type { + LGraph, + Positionable, + Subgraph +} from '@/lib/litegraph/src/litegraph' +import { createTestNode } from '@/lib/litegraph/src/__fixtures__/nodeHelpers' +import { + createTestRootGraph, + enableSubgraphNodeCreation, + resetSubgraphFixtureState +} from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' +import { layoutStore } from '@/renderer/core/layout/store/layoutStore' + +import { setNodePosition } from './graphLayoutAttachment' + +beforeEach(() => { + resetSubgraphFixtureState() + layoutStore.resetForTests() +}) + +function interiorGeometry( + rootGraph: LGraph, + definition: Subgraph +): Record { + return Object.fromEntries( + definition.nodes.map((node) => [ + String(node.id), + layoutStore.getNodeLayout(rootGraph.id, node.id)?.position ?? null + ]) + ) +} + +describe('Convert to Subgraph and the layout records of a nested definition', () => { + it('leaves each interior node at its own distinct position', () => { + const rootGraph = createTestRootGraph() + onTestFinished(enableSubgraphNodeCreation(rootGraph)) + + const origin = createTestNode(rootGraph, [], ['number']) + const target = createTestNode(rootGraph, ['number']) + origin.connect(0, target, 0) + setNodePosition(origin, [110, 220]) + setNodePosition(target, [330, 440]) + + const { subgraph: inner, node: innerHost } = rootGraph.convertToSubgraph( + new Set([origin, target]) + ) + const before = interiorGeometry(rootGraph, inner) + + rootGraph.convertToSubgraph( + new Set([innerHost, createTestNode(rootGraph)]) + ) + + expect({ before, after: interiorGeometry(rootGraph, inner) }).toEqual({ + before: { + [origin.id]: { x: 110, y: 220 }, + [target.id]: { x: 330, y: 440 } + }, + after: { + [origin.id]: { x: 110, y: 220 }, + [target.id]: { x: 330, y: 440 } + } + }) + }) +})