From 5a5fa651734a27923dc7821958d9bcae199a585d Mon Sep 17 00:00:00 2001 From: jaeone lee <89377375+jaeone94@users.noreply.github.com> Date: Tue, 20 Oct 2026 22:48:37 +0000 Subject: [PATCH] fix: create restored above-max DynamicGroups on followers (#21280) > Stacked on #21264. Keep as draft until that merges; the base will be retargeted to `main` then. ## Summary A DynamicGroup restored from a saved workflow can hold more rows than its `max`. When that node reaches another tab through the shared doc, the follower's create path configures it with only the widgets a fresh node has. The row values are dropped, `validateSavedRowCount` throws, and the follower records an apply failure and ends up with 0 rows. - Fixes #21279 - Part of #21094 (item 1: remote create now goes through the remote row-count path) On create, `createNode` now holds a DynamicGroup row count at its constructed value during `configure`, then applies it with the row values through the existing widget rounds. There the remote row-count guard from #21264 lets a real restored group through, because its limit counts the rows the document carries values for: `max(max, current rows, documented rows)`. A crafted doc with a huge count and no row values is still refused before any row is allocated. **Scope:** the count is only deferred when the document stores it by name on a node **without** a DynamicCombo input. On a DynamicCombo node, positional `_extra_N` aliases can address slots that the rows would shift, so those nodes keep the existing `configure` path exactly as before. That keeps this PR free of the alias edge cases Cursor kept finding. The DynamicCombo-node case is tracked in #21297, and a test asserts today's behavior there (0 rows, one failure). ## Red-Green Verification | Commit | CI Status | Purpose | |--------|-----------|---------| | `test: cover creating a restored above-max DynamicGroup on a follower` (612c215) | :red_circle: [Red](https://github.com/Comfy-Org/ComfyUI_frontend/actions/runs/39108094938) | Proves the test catches the bug | | `fix: create restored above-max DynamicGroups on followers` (4733ced) | :green_circle: [Green](https://github.com/Comfy-Org/ComfyUI_frontend/actions/runs/39108847535) | Proves the fix resolves the bug | | `test: create a DynamicGroup whose row count is stored under an overflow alias` (c12b1cd) | :red_circle: [Red](https://github.com/Comfy-Org/ComfyUI_frontend/actions/runs/39134067553) | Cursor: an alias-stored count was dropped on create | | `test: keep aliased DynamicGroup row values off a trailing widget on create` (04427b5) | :red_circle: [Red](https://github.com/Comfy-Org/ComfyUI_frontend/actions/runs/39137999361) | Cursor: aliased row values landed on a trailing widget | | `test: cover a named DynamicGroup row count followed by aliased values on create` (4d85d35) | :red_circle: [Red](https://github.com/Comfy-Org/ComfyUI_frontend/actions/runs/39141788205) | Cursor: a named count followed by aliases had the same shift | | `test: create a restored above-max DynamicGroup next to a stray overflow key` (c5c277b) | :red_circle: [Red](https://github.com/Comfy-Org/ComfyUI_frontend/actions/runs/39145478948) | Cursor: a stray `_extra_9` brought back #21279 | | `test: cover a fully named catalog order on an overflow DynamicGroup node` (c48b631) | :red_circle: [Red](https://github.com/Comfy-Org/ComfyUI_frontend/actions/runs/39149443794) | Cursor: a named trailing slot holding a row value | | `fix: defer DynamicGroup row counts only on nodes without overflow aliases` (6e9f43c) | :green_circle: [Green](https://github.com/Comfy-Org/ComfyUI_frontend/actions/runs/39150420673) | Final behavior: every red case above passes | Each of the Cursor reds above also had its own fix commit in between (94daae4, 7a7f72d, 49aacc3, 9f67924), each with a green run. Those tried to handle aliases on DynamicCombo nodes, and 6e9f43c replaces all of them by keeping those nodes on the `configure` path. The test-only commits 6f438e8 and 7c3007f label the cases and pin today's #21297 behavior. The documented-rows part of the limit later moved into #21264 (bc77798c89) to fix a rollback regression there, and this branch picked it up through a merge. This PR's own diff is now just the create path. ## Test Plan - [x] CI red on each test-only commit, with only the new case failing - [x] CI green on the final fix commit - A restored 4-row group (`max: 3`) is created on a fresh follower with all 4 rows and no failure, also when a stray `_extra_9` sits in the doc. The stray key is still reported as a missing widget, which is existing behavior. - On a DynamicCombo node, alias-stored counts and row values restore exactly as before this PR, including with a widget after the group and a fully named catalog order. - Guard: a crafted doc with `rows = 1e9` and no row values is still refused on create (0 rows, one failure). - Known gap: a restored above-max group on a DynamicCombo node still ends with 0 rows and one failure (#21297). A test asserts that, so a change on this path shows up. - Reviewed beyond Cursor's 5-round cap with internal bug-hunter, regression-risk, architecture, christian-style and drjkl-style agents. Their actionable points are folded into 6e9f43c, 6f438e8 and 7c3007f; a second pass found no blocking issues. --- .../liveGraphApplier.changeTracker.test.ts | 272 +++++++++++++++++- .../extensions/agent/crdt/liveGraphApplier.ts | 41 ++- 2 files changed, 306 insertions(+), 7 deletions(-) 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, {