diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.changeTracker.test.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.changeTracker.test.ts index 2e6fc5836f2..119d428bec2 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.changeTracker.test.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.changeTracker.test.ts @@ -1,4 +1,4 @@ -import { applyOps } from '@comfyorg/comfy-multi-player' +import { applyOps, nodesMap } from '@comfyorg/comfy-multi-player' import type { Op, WidgetCatalog } from '@comfyorg/comfy-multi-player' import { fromPartial } from '@total-typescript/shoehorn' import { @@ -11,6 +11,7 @@ import { vi } from 'vitest' import { markRaw, ref } from 'vue' +import * as Y from 'yjs' import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' import type { LGraphCanvas } from '@/lib/litegraph/src/litegraph' @@ -26,7 +27,8 @@ import { useLitegraphService } from '@/services/litegraphService' import { followedDoc } from './__fixtures__/followedDoc' import { LiveGraphApplier } from './liveGraphApplier' -import { attachDocOpMinter } from './docOpMinter' +import { attachDocOpMinter, wireNodeSnapshot } from './docOpMinter' +import { mintWireOps } from './opEnvelope' const appState = vi.hoisted(() => ({ rootGraph: undefined as LGraph | undefined @@ -88,6 +90,31 @@ class DynamicGroupTest extends LGraphNode { } } +class DynamicGroupOverflowTest extends LGraphNode { + constructor() { + super('DynamicGroupOverflowTest') + this.comfyClass = 'DynamicGroupOverflowTest' + this.serialize_widgets = true + this.addWidget('text', 'before', 'head', () => {}) + useLitegraphService().addNodeInput(this, { + name: 'rows', + type: 'COMFY_DYNAMICGROUP_V3', + isOptional: false, + min: 0, + max: 3, + template: { required: { strength: ['FLOAT', { default: 1 }] } } + }) + } +} + +class DynamicGroupOverflowTrailingTest extends DynamicGroupOverflowTest { + constructor() { + super() + this.comfyClass = 'DynamicGroupOverflowTrailingTest' + this.addWidget('text', 'after', 'tail', () => {}) + } +} + const CATALOG: WidgetCatalog = { types: { TestSource: { widget_order: ['steps'] }, @@ -121,6 +148,52 @@ function op(body: OpBody): Op { return { ...envelope, ...body } } +function restoredAboveMaxDoc() { + const local = new LGraph() + const node = LiteGraph.createNode('DynamicGroupTest') + assert.exists(node) + local.add(node) + node.configure({ + ...node.serialize(), + widgets_values: ['head', 4, 0.1, 0.2, 0.3, 0.4, 'tail'] + }) + const snapshot = wireNodeSnapshot(node) + assert.exists(snapshot) + const catalog: WidgetCatalog = { + types: { + DynamicGroupTest: { + widget_order: [ + 'before', + 'rows', + 'rows.0.strength', + 'rows.1.strength', + 'rows.2.strength', + 'rows.3.strength', + 'after' + ] + } + } + } + const { doc, collector } = followedDoc({ nodes: [], links: [] }, catalog) + collector.take() + const add = mintWireOps( + [ + { + op: 'add_node', + node_id: node.id, + class_type: snapshot.type, + pos: [0, 0], + node: snapshot + } + ], + { actor: 'human:user:tab', baseVersion: 1 } + ) + expect( + applyOps(doc, add, catalog).outcomes.map(({ outcome }) => outcome) + ).toEqual(['applied']) + return { doc, collector, node } +} + async function workflowJson(graph: LGraph): Promise { const state = await validateComfyWorkflow(graph.serialize(), (error) => { throw new Error(error) @@ -133,6 +206,26 @@ beforeEach(() => { LiteGraph.registerNodeType('TestSource', TestSource) LiteGraph.registerNodeType('TestSink', TestSink) LiteGraph.registerNodeType('DynamicGroupTest', DynamicGroupTest) + LiteGraph.registerNodeType( + 'DynamicGroupOverflowTest', + DynamicGroupOverflowTest + ) + LiteGraph.registerNodeType( + 'DynamicGroupOverflowTrailingTest', + DynamicGroupOverflowTrailingTest + ) + DynamicGroupOverflowTest.nodeData = fromPartial< + NonNullable + >({ + inputs: { + mode: { + type: 'COMFY_DYNAMICCOMBO_V3', + name: 'mode', + isOptional: false, + options: [] + } + } + }) ChangeTracker.isLoadingGraph = false }) @@ -319,6 +412,181 @@ describe('LiveGraphApplier with the real change tracker', () => { ) }) + it('creates a restored DynamicGroup holding more rows than its max on a follower', () => { + const { doc, collector, node } = restoredAboveMaxDoc() + const follower = new LGraph() + const applier = new LiveGraphApplier({ getGraph: () => follower }) + + const result = applier.applyChanges(doc, collector.take(), CONTEXT) + + expect(result.failureCount).toBe(0) + expect(follower.getNodeById(node.id)?.serialize().widgets_values).toEqual([ + 'head', + 4, + 0.1, + 0.2, + 0.3, + 0.4, + 'tail' + ]) + }) + + it('creates a restored above-max DynamicGroup next to a stray positional key', () => { + const { doc, collector, node } = restoredAboveMaxDoc() + doc.transact(() => { + const widgets = nodesMap(doc).get(String(node.id))?.get('widgets') + assert.instanceOf(widgets, Y.Map) + widgets.set('_extra_9', 1) + }) + const follower = new LGraph() + const applier = new LiveGraphApplier({ getGraph: () => follower }) + + const result = applier.applyChanges(doc, collector.take(), CONTEXT) + + expect(result.failureCount).toBe(1) + expect(follower.getNodeById(node.id)?.serialize().widgets_values).toEqual([ + 'head', + 4, + 0.1, + 0.2, + 0.3, + 0.4, + 'tail' + ]) + }) + + it('creates a DynamicGroup whose row count the document stores under a positional alias', () => { + const { doc, collector } = followedDoc( + { + nodes: [ + { + id: 1, + type: 'DynamicGroupOverflowTest', + pos: [0, 0], + size: [210, 100], + widgets_values: ['head', 2, 0.1, 0.2] + } + ], + links: [] + }, + { types: { DynamicGroupOverflowTest: { widget_order: ['before'] } } } + ) + const follower = new LGraph() + const applier = new LiveGraphApplier({ getGraph: () => follower }) + + const result = applier.applyChanges(doc, collector.take(), CONTEXT) + + expect(result.failureCount).toBe(0) + expect( + follower.getNodeById(toNodeId(1))?.serialize().widgets_values + ).toEqual(['head', 2, 0.1, 0.2]) + }) + + it.for([ + { label: 'only the leading widget', widgetOrder: ['before'] }, + { label: 'the controller', widgetOrder: ['before', 'rows'] }, + { label: 'the trailing widget', widgetOrder: ['before', 'rows', 'after'] } + ])( + 'restores aliased row values and the trailing widget on create when the catalog names $label', + ({ widgetOrder }) => { + const { doc, collector } = followedDoc( + { + nodes: [ + { + id: 1, + type: 'DynamicGroupOverflowTrailingTest', + pos: [0, 0], + size: [210, 100], + widgets_values: ['head', 2, 0.1, 0.2, 'tail'] + } + ], + links: [] + }, + { + types: { + DynamicGroupOverflowTrailingTest: { widget_order: widgetOrder } + } + } + ) + const follower = new LGraph() + const applier = new LiveGraphApplier({ getGraph: () => follower }) + + const result = applier.applyChanges(doc, collector.take(), CONTEXT) + + expect(result.failureCount).toBe(0) + expect( + follower.getNodeById(toNodeId(1))?.serialize().widgets_values + ).toEqual(['head', 2, 0.1, 0.2, 'tail']) + } + ) + + it('still fails to create a restored above-max DynamicGroup on a node with a DynamicCombo (#21297)', () => { + const { doc, collector } = followedDoc( + { + nodes: [ + { + id: 1, + type: 'DynamicGroupOverflowTest', + pos: [0, 0], + size: [210, 100], + widgets_values: ['head', 4, 0.1, 0.2, 0.3, 0.4] + } + ], + links: [] + }, + { + types: { + DynamicGroupOverflowTest: { + widget_order: [ + 'before', + 'rows', + 'rows.0.strength', + 'rows.1.strength', + 'rows.2.strength', + 'rows.3.strength' + ] + } + } + } + ) + const follower = new LGraph() + const applier = new LiveGraphApplier({ getGraph: () => follower }) + + const result = applier.applyChanges(doc, collector.take(), CONTEXT) + + expect(result.failureCount).toBe(1) + expect( + follower.getNodeById(toNodeId(1))?.serialize().widgets_values + ).toEqual(['head', 0]) + }) + + it('refuses a crafted DynamicGroup row count without row values on create', () => { + const { doc, collector } = followedDoc( + { + nodes: [ + { + id: 1, + type: 'DynamicGroupTest', + pos: [0, 0], + size: [210, 100], + widgets_values: ['head', 1e9, 'tail'] + } + ], + links: [] + }, + CATALOG + ) + const follower = new LGraph() + const applier = new LiveGraphApplier({ getGraph: () => follower }) + + const result = applier.applyChanges(doc, collector.take(), CONTEXT) + + expect(result.failureCount).toBe(1) + expect( + follower.getNodeById(toNodeId(1))?.serialize().widgets_values + ).toEqual(['head', 0, 'tail']) + }) + it('builds a DynamicGroup node from the catch-up frame without outbound echo and reloads to the same graph', async () => { const graph = new LGraph() appState.rootGraph = graph diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index 4552b630265..764cad48c2c 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -484,6 +484,28 @@ function documentedRowCount(name: string, widgets: DocNode['widgets']): number { return rows.size } +function deferredRowCounts( + node: LGraphNode, + constructed: readonly IBaseWidget[], + widgets: DocNode['widgets'] +): ReadonlySet { + if ( + widgets === undefined || + Array.isArray(widgets) || + supportsDynamicWidgetOverflow(node) + ) + return new Set() + return new Set( + constructed + .filter( + (widget) => + isDynamicGroupController(widget) && + Object.hasOwn(widgets, widget.name) + ) + .map((widget) => widget.name) + ) +} + /** * Document entries for widgets that only exist after `configure`: aliases past * the constructed list, and names the constructor did not build but the node @@ -494,13 +516,14 @@ function mountedWidgetValues( node: LGraphNode, widgets: DocNode['widgets'], constructed: readonly IBaseWidget[], - constructedNames: ReadonlySet + constructedNames: ReadonlySet, + deferred: ReadonlySet ): Record | undefined { if (widgets === undefined || Array.isArray(widgets)) return undefined const mounted = Object.entries(widgets).filter(([name]) => { const index = overflowWidgetIndex(name) return index === null - ? !constructedNames.has(name) + ? !constructedNames.has(name) || deferred.has(name) : overflowWidget(node, name) === undefined || index >= constructed.length }) return mounted.length === 0 ? undefined : Object.fromEntries(mounted) @@ -514,7 +537,8 @@ function mountedWidgetValues( */ function positionalWidgetValues( node: LGraphNode, - widgets: DocNode['widgets'] + widgets: DocNode['widgets'], + deferred: ReadonlySet ): WidgetValue[] | undefined { if (widgets === undefined) return undefined if (Array.isArray(widgets)) { @@ -523,6 +547,7 @@ function positionalWidgetValues( ) } return serializableWidgets(node).map((widget, index) => { + if (deferred.has(widget.name)) return widget.value const alias = `_extra_${index}` const name = Object.hasOwn(widgets, widget.name) ? widget.name @@ -797,15 +822,21 @@ export class LiveGraphApplier { const constructedNames = new Set( node.widgets?.map((widget) => widget.name) ) + const deferred = deferredRowCounts(node, constructed, docNode.widgets) node.configure({ ...info, - widgets_values: positionalWidgetValues(node, docNode.widgets) + widgets_values: positionalWidgetValues( + node, + docNode.widgets, + deferred + ) }) const mounted = mountedWidgetValues( node, docNode.widgets, constructed, - constructedNames + constructedNames, + deferred ) if (mounted) this.applyWidgets(node, mounted, mode, {