From 4f089129fd431a9418fdc9e235117f77e51724dc Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 5 Nov 2026 01:03:05 +0000 Subject: [PATCH 1/6] fix(agent): keep a group the agent draws around existing nodes on them An insert_workflow that carries only groups was placed like any inserted batch: a group drawn around nodes already on the canvas collides with them, so a live agent turn moved it to the right of the graph. Asking the agent to put two KSamplers in one group left an empty frame beside them, while the document and the agent's copy still framed the KSamplers (frontend 1.60 QA REG04). A batch that creates no nodes now keeps each group that frames an existing node where the document puts it, as comfy-cli's insert-workflow already does for a group-only template. Groups that frame nothing, and groups that arrive with new nodes, are placed as before. --- .../fixtures/data/agent/agentStageFrames.ts | 23 +++++++++++ ...entStageFramesFromInsertedWorkflow.spec.ts | 31 ++++++++++++++ .../agentCrdtProjection.integration.test.ts | 41 ++++++++++++++++++- .../extensions/agent/crdt/liveGraphApplier.ts | 35 ++++++++++++++-- 4 files changed, 125 insertions(+), 5 deletions(-) diff --git a/browser_tests/fixtures/data/agent/agentStageFrames.ts b/browser_tests/fixtures/data/agent/agentStageFrames.ts index 18e7485cdf9..9e74610803f 100644 --- a/browser_tests/fixtures/data/agent/agentStageFrames.ts +++ b/browser_tests/fixtures/data/agent/agentStageFrames.ts @@ -64,3 +64,26 @@ export const STAGE_FRAMES_CATALOG: WidgetCatalog = { export const STAGE_FRAMES_INSERT_OPS: RecordedGraphOperation[] = [ { op: 'insert_workflow', workflow: STAGE_FRAMES_TEMPLATE } ] + +export const EXISTING_FRAME_TITLE = 'Existing node frame' + +/** A workflow that carries only a group drawn around the existing node. */ +export const EXISTING_FRAME_INSERT_OPS: RecordedGraphOperation[] = [ + { + op: 'insert_workflow', + workflow: { + nodes: [], + links: [], + groups: [ + { + id: 1, + title: EXISTING_FRAME_TITLE, + bounding: [-20, -60, 360, 280], + color: '#3f789e', + font_size: 24, + flags: {} + } + ] + } + } +] diff --git a/browser_tests/tests/agent/agentStageFramesFromInsertedWorkflow.spec.ts b/browser_tests/tests/agent/agentStageFramesFromInsertedWorkflow.spec.ts index 769d2be0373..0c36b698503 100644 --- a/browser_tests/tests/agent/agentStageFramesFromInsertedWorkflow.spec.ts +++ b/browser_tests/tests/agent/agentStageFramesFromInsertedWorkflow.spec.ts @@ -15,6 +15,8 @@ import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' import { Topbar } from '@e2e/fixtures/components/Topbar' import { + EXISTING_FRAME_INSERT_OPS, + EXISTING_FRAME_TITLE, STAGE_FRAME_TITLES, STAGE_FRAMES_CATALOG, STAGE_FRAMES_EXISTING_NODES, @@ -195,5 +197,34 @@ test.describe( ) expect(savedGeometry).toEqual(live.geometry) }) + + test('keeps a group drawn around existing nodes on those nodes', async ({ + page + }) => { + socket.send(JSON.stringify(host.apply(EXISTING_FRAME_INSERT_OPS))) + + await expect + .poll(() => + page.evaluate(() => + window.app!.graph.groups.map((group) => { + group.recomputeInsideNodes() + return { + title: group.title, + bounding: [...group._bounding], + members: group.nodes.map((node) => + String(node.widgets?.[0]?.value ?? '') + ) + } + }) + ) + ) + .toEqual([ + { + title: EXISTING_FRAME_TITLE, + bounding: [-20, -60, 360, 280], + members: ['Existing node'] + } + ]) + }) } ) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts index 2699d154338..b46440cca7e 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts @@ -68,7 +68,10 @@ type FrameClassification = Pick & function toWorkflowJson({ nodes, ...rest }: ISerialisedGraph): WorkflowJSON { return { ...rest, - nodes: nodes.map(({ flags, ...node }) => ({ ...node, flags: { ...flags } })) + nodes: nodes.map(({ flags, ...node }) => ({ + ...node, + flags: { ...flags } + })) } } @@ -490,7 +493,9 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { ]).toEqual([-20, -20]) expect([...group.size]).toEqual([300, 200]) }) - it('places an inserted workflow that carries only groups', () => { + it('keeps a group-only insertion on the existing nodes it frames', () => { + // FE 1.60 QA REG04: "put both KSamplers in one group" inserts a workflow + // that carries only a group drawn around nodes already on the canvas. const { graph, source, note } = buildLiveGraph() source.setSize([200, 100]) note.setSize([200, 100]) @@ -516,6 +521,38 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { { actor: HOST_OP.actor, catchUp: false } ) + const group = graph.groups.find(({ title }) => title === 'Inserted group') + assert(group, 'the inserted group reached the canvas') + expect([...group.pos]).toEqual([0, 10]) + expect([...group.size]).toEqual([300, 200]) + expect([...source.pos]).toEqual([10, 20]) + }) + it('places a group-only insertion that frames no node clear of the canvas content', () => { + const { graph, source, note } = buildLiveGraph() + source.setSize([200, 100]) + note.setSize([200, 100]) + const { host, hostEdit, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()) + ) + onTestFinished(destroy) + + hostEdit( + () => + applyHostOp(host, { + ...HOST_OP, + op_id: '1a60'.padEnd(32, '0'), + op: 'insert_workflow', + workflow: { + nodes: [], + groups: [ + { id: 1, title: 'Inserted group', bounding: [150, 0, 100, 300] } + ] + } + }), + { actor: HOST_OP.actor, catchUp: false } + ) + const group = graph.groups.find(({ title }) => title === 'Inserted group') assert(group, 'the inserted group reached the canvas') expect([...group.pos]).toEqual([680, 20]) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index 3aec6a00e65..61c9b8b8a56 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -2047,14 +2047,26 @@ export class LiveGraphApplier { width: item.size[0], height: item.size[1] }) + const existingNodes = graph._nodes.filter( + (node) => !createdSet.has(node.id) + ) + // A batch that adds no nodes but a group drawn around nodes already on the + // canvas is the agent grouping those nodes: moving it would leave an empty + // frame beside them and a canvas that disagrees with the document. + const placedGroups = + created.length === 0 + ? createdGroups.filter( + (group) => !framesAnyNode(rect(group), existingNodes.map(rect)) + ) + : createdGroups const incoming = [ ...graph._nodes.filter((node) => createdSet.has(node.id)), - ...createdGroups + ...placedGroups ] const offset = placementOffset({ existing: [ - ...graph._nodes.filter((node) => !createdSet.has(node.id)), - ...graph.groups.filter((group) => !createdGroups.includes(group)) + ...existingNodes, + ...graph.groups.filter((group) => !placedGroups.includes(group)) ].map(rect), viewport: this.deps.viewportBounds?.() ?? null, incoming: incoming.map(rect), @@ -2067,6 +2079,23 @@ export class LiveGraphApplier { } } +/** Whether the centre of any of `nodes` lies inside `frame`. */ +function framesAnyNode( + frame: PlacementRect, + nodes: readonly PlacementRect[] +): boolean { + return nodes.some((node) => { + const x = node.x + node.width / 2 + const y = node.y + node.height / 2 + return ( + x >= frame.x && + x <= frame.x + frame.width && + y >= frame.y && + y <= frame.y + frame.height + ) + }) +} + function isLinkPresent( graph: LGraph, origin: LGraphNode, From fc6a07c321e644c8397179b485e2f6cbf6babb19 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 5 Nov 2026 01:24:08 +0000 Subject: [PATCH 2/6] fix(agent): decide group placement per group from rendered membership Review follow-up. A group that frames only existing nodes now keeps its coordinates even when the same insertion adds nodes, and siblings of such a group in a group-only insertion stay with it instead of being split off. Membership uses the node's measured bounds with LiteGraph's containsCentre, the same test recomputeInsideNodes applies. Drops the added comments. --- .../agentCrdtProjection.integration.test.ts | 80 ++++++++++++++++++- .../extensions/agent/crdt/liveGraphApplier.ts | 38 +++------ 2 files changed, 91 insertions(+), 27 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts index b46440cca7e..207a3207f7c 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts @@ -494,8 +494,6 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { expect([...group.size]).toEqual([300, 200]) }) it('keeps a group-only insertion on the existing nodes it frames', () => { - // FE 1.60 QA REG04: "put both KSamplers in one group" inserts a workflow - // that carries only a group drawn around nodes already on the canvas. const { graph, source, note } = buildLiveGraph() source.setSize([200, 100]) note.setSize([200, 100]) @@ -527,6 +525,84 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { expect([...group.size]).toEqual([300, 200]) expect([...source.pos]).toEqual([10, 20]) }) + it('keeps a group around existing nodes in place when the same insertion adds a node', () => { + const { graph, source, note } = buildLiveGraph() + source.setSize([200, 100]) + note.setSize([200, 100]) + const { host, hostEdit, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()) + ) + onTestFinished(destroy) + + hostEdit( + () => + applyHostOp(host, { + ...HOST_OP, + op_id: '1a62'.padEnd(32, '0'), + op: 'insert_workflow', + workflow: { + nodes: [ + { + id: 1, + type: 'TestSource', + pos: [20, 30], + size: [200, 100], + widgets_values: [20, 7] + } + ], + groups: [ + { id: 1, title: 'Around note', bounding: [380, -20, 240, 160] } + ] + } + }), + { actor: HOST_OP.actor, catchUp: false } + ) + + const inserted = graph._nodes.find( + (node) => node !== source && node !== note + ) + const group = graph.groups.find(({ title }) => title === 'Around note') + assert(inserted && group, 'the insertion reached the canvas') + expect([...inserted.pos]).not.toEqual([20, 30]) + expect([...group.pos]).toEqual([380, -20]) + expect([...note.pos]).toEqual([400, 20]) + }) + it('keeps every group of a group-only insertion together when one frames existing nodes', () => { + const { graph, source, note } = buildLiveGraph() + source.setSize([200, 100]) + note.setSize([200, 100]) + const { host, hostEdit, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()) + ) + onTestFinished(destroy) + + hostEdit( + () => + applyHostOp(host, { + ...HOST_OP, + op_id: '1a63'.padEnd(32, '0'), + op: 'insert_workflow', + workflow: { + nodes: [], + groups: [ + { id: 1, title: 'Around source', bounding: [0, -20, 240, 160] }, + { id: 2, title: 'Next stage', bounding: [250, 0, 100, 300] } + ] + } + }), + { actor: HOST_OP.actor, catchUp: false } + ) + + const positions = Object.fromEntries( + graph.groups.map((group) => [group.title, [...group.pos]]) + ) + expect(positions).toEqual({ + 'Around source': [0, -20], + 'Next stage': [250, 0] + }) + }) it('places a group-only insertion that frames no node clear of the canvas content', () => { const { graph, source, note } = buildLiveGraph() source.setSize([200, 100]) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index 61c9b8b8a56..503f75a61bd 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -28,6 +28,7 @@ import { LiteGraph } from '@/lib/litegraph/src/litegraph' import type { LGraph } from '@/lib/litegraph/src/litegraph' +import { containsCentre } from '@/lib/litegraph/src/measure' import { topologicalSortSubgraphs } from '@/lib/litegraph/src/subgraph/subgraphDeduplication' import type { ExportedSubgraph, @@ -2047,22 +2048,18 @@ export class LiveGraphApplier { width: item.size[0], height: item.size[1] }) + const createdNodes = graph._nodes.filter((node) => createdSet.has(node.id)) const existingNodes = graph._nodes.filter( (node) => !createdSet.has(node.id) ) - // A batch that adds no nodes but a group drawn around nodes already on the - // canvas is the agent grouping those nodes: moving it would leave an empty - // frame beside them and a canvas that disagrees with the document. + const framesOnlyExisting = (group: LGraphGroup) => + framesAny(group, existingNodes) && !framesAny(group, createdNodes) + const kept = createdGroups.filter(framesOnlyExisting) const placedGroups = - created.length === 0 - ? createdGroups.filter( - (group) => !framesAnyNode(rect(group), existingNodes.map(rect)) - ) - : createdGroups - const incoming = [ - ...graph._nodes.filter((node) => createdSet.has(node.id)), - ...placedGroups - ] + kept.length > 0 && createdNodes.length === 0 + ? [] + : createdGroups.filter((group) => !kept.includes(group)) + const incoming = [...createdNodes, ...placedGroups] const offset = placementOffset({ existing: [ ...existingNodes, @@ -2079,20 +2076,11 @@ export class LiveGraphApplier { } } -/** Whether the centre of any of `nodes` lies inside `frame`. */ -function framesAnyNode( - frame: PlacementRect, - nodes: readonly PlacementRect[] -): boolean { +function framesAny(group: LGraphGroup, nodes: readonly LGraphNode[]): boolean { + const bounds: [number, number, number, number] = [0, 0, 0, 0] return nodes.some((node) => { - const x = node.x + node.width / 2 - const y = node.y + node.height / 2 - return ( - x >= frame.x && - x <= frame.x + frame.width && - y >= frame.y && - y <= frame.y + frame.height - ) + node.measure(bounds) + return containsCentre(group._bounding, bounds) }) } From 3d15a4b37a7e6f48c0bd2242032a53b148588977 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 5 Nov 2026 01:43:47 +0000 Subject: [PATCH 3/6] fix(agent): judge group membership from existing nodes' rendered bounds Review follow-up. node.measure() without a canvas context resets a collapsed node's measured width, so a group around a collapsed legacy node could be judged as framing nothing and moved away. Existing nodes now use their rendered boundingRect, as recomputeInsideNodes does; nodes created in the same frame have not been drawn yet and use pos and size. --- .../agentCrdtProjection.integration.test.ts | 51 +++++++++++++++++++ .../extensions/agent/crdt/liveGraphApplier.ts | 23 ++++++--- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts index 207a3207f7c..bb1ae20a22e 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts @@ -451,6 +451,8 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { const { graph, source, note } = buildLiveGraph() source.setSize([200, 100]) note.setSize([200, 100]) + source.updateArea() + note.updateArea() const { host, hostEdit, destroy } = bindAndCatchUp( graph, structuredClone(graph.serialize()) @@ -497,6 +499,8 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { const { graph, source, note } = buildLiveGraph() source.setSize([200, 100]) note.setSize([200, 100]) + source.updateArea() + note.updateArea() const { host, hostEdit, destroy } = bindAndCatchUp( graph, structuredClone(graph.serialize()) @@ -529,6 +533,8 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { const { graph, source, note } = buildLiveGraph() source.setSize([200, 100]) note.setSize([200, 100]) + source.updateArea() + note.updateArea() const { host, hostEdit, destroy } = bindAndCatchUp( graph, structuredClone(graph.serialize()) @@ -572,6 +578,8 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { const { graph, source, note } = buildLiveGraph() source.setSize([200, 100]) note.setSize([200, 100]) + source.updateArea() + note.updateArea() const { host, hostEdit, destroy } = bindAndCatchUp( graph, structuredClone(graph.serialize()) @@ -603,10 +611,53 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { 'Next stage': [250, 0] }) }) + it('keeps a group on a collapsed node it frames as rendered', () => { + const { graph, source, note } = buildLiveGraph() + source.setSize([600, 100]) + note.pos = [1000, 20] + source.collapse(true) + const context = { + font: '', + measureText: () => ({ width: 400 }) + } as unknown as CanvasRenderingContext2D + source.updateArea(context) + note.updateArea() + const { host, hostEdit, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()) + ) + onTestFinished(destroy) + + hostEdit( + () => + applyHostOp(host, { + ...HOST_OP, + op_id: '1a64'.padEnd(32, '0'), + op: 'insert_workflow', + workflow: { + nodes: [], + groups: [ + { + id: 1, + title: 'Around collapsed', + bounding: [150, -20, 200, 60] + } + ] + } + }), + { actor: HOST_OP.actor, catchUp: false } + ) + + const group = graph.groups.find(({ title }) => title === 'Around collapsed') + assert(group, 'the inserted group reached the canvas') + expect([...group.pos]).toEqual([150, -20]) + }) it('places a group-only insertion that frames no node clear of the canvas content', () => { const { graph, source, note } = buildLiveGraph() source.setSize([200, 100]) note.setSize([200, 100]) + source.updateArea() + note.updateArea() const { host, hostEdit, destroy } = bindAndCatchUp( graph, structuredClone(graph.serialize()) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index 503f75a61bd..f7c23af96fc 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -63,7 +63,10 @@ import { readSubgraphDefinitions, readSubgraphInteriorWidgets } from './agentSubgraphDefinitions' -import type { Bounds as PlacementRect } from '@comfyorg/shared-frontend-utils/geometry' +import type { + Bounds as PlacementRect, + ReadOnlyRect +} from '@comfyorg/shared-frontend-utils/geometry' import { placementOffset } from './batchPlacement' export type NodeChange = 'add' | 'update' | 'delete' @@ -2052,8 +2055,13 @@ export class LiveGraphApplier { const existingNodes = graph._nodes.filter( (node) => !createdSet.has(node.id) ) + const existingBounds = existingNodes.map((node) => node.boundingRect) + const createdBounds = createdNodes.map((node): ReadOnlyRect => { + const { x, y, width, height } = rect(node) + return [x, y, width, height] + }) const framesOnlyExisting = (group: LGraphGroup) => - framesAny(group, existingNodes) && !framesAny(group, createdNodes) + framesAny(group, existingBounds) && !framesAny(group, createdBounds) const kept = createdGroups.filter(framesOnlyExisting) const placedGroups = kept.length > 0 && createdNodes.length === 0 @@ -2076,12 +2084,11 @@ export class LiveGraphApplier { } } -function framesAny(group: LGraphGroup, nodes: readonly LGraphNode[]): boolean { - const bounds: [number, number, number, number] = [0, 0, 0, 0] - return nodes.some((node) => { - node.measure(bounds) - return containsCentre(group._bounding, bounds) - }) +function framesAny( + group: LGraphGroup, + bounds: readonly ReadOnlyRect[] +): boolean { + return bounds.some((rect) => containsCentre(group._bounding, rect)) } function isLinkPresent( From 84c21dec3d88f9fbfa891ff8aa3d9010b2dbe842 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 5 Nov 2026 02:13:21 +0000 Subject: [PATCH 4/6] fix(agent): include the title in a new node's group-membership bounds Review follow-up. Body-only bounds for nodes created in the same frame could keep a group that, by LiteGraph's rendered membership, also frames a new node, and the new node was then moved away from it. New nodes now use the bounds measure() would give, title included, computed without measure()'s side effect on a collapsed node's width. --- .../agentCrdtProjection.integration.test.ts | 47 +++++++++++++++++++ .../extensions/agent/crdt/liveGraphApplier.ts | 18 +++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts index bb1ae20a22e..272cdb9e074 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts @@ -611,6 +611,53 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { 'Next stage': [250, 0] }) }) + it('places a group with the new node it frames by its rendered title', () => { + const { graph, source, note } = buildLiveGraph() + source.setSize([200, 100]) + note.setSize([200, 100]) + source.updateArea() + note.updateArea() + const { host, hostEdit, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()) + ) + onTestFinished(destroy) + + hostEdit( + () => + applyHostOp(host, { + ...HOST_OP, + op_id: '1a65'.padEnd(32, '0'), + op: 'insert_workflow', + workflow: { + nodes: [ + { + id: 1, + type: 'TestSource', + pos: [20, 30], + size: [200, 100], + widgets_values: [20, 7] + } + ], + groups: [ + { id: 1, title: 'Around both', bounding: [0, -20, 300, 95] } + ] + } + }), + { actor: HOST_OP.actor, catchUp: false } + ) + + const inserted = graph._nodes.find( + (node) => node !== source && node !== note + ) + const group = graph.groups.find(({ title }) => title === 'Around both') + assert(inserted && group, 'the insertion reached the canvas') + expect([...inserted.pos]).not.toEqual([20, 30]) + expect([ + group.pos[0] - inserted.pos[0], + group.pos[1] - inserted.pos[1] + ]).toEqual([-20, -50]) + }) it('keeps a group on a collapsed node it frames as rendered', () => { const { graph, source, note } = buildLiveGraph() source.setSize([600, 100]) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index f7c23af96fc..a47d2eb95d7 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -47,6 +47,7 @@ import { import { transformInputSpecV1ToV2 } from '@comfyorg/object-info-parser/migration' import type { InputSpec as InputSpecV2 } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2' import { reportError } from '@comfyorg/telemetry/reportError' +import { TitleMode } from '@comfyorg/graph-model/globalEnums' import { zComfyNode, zGroup } from '@comfyorg/graph-model/workflowSchema' import { BaseWidget } from '@/lib/litegraph/src/widgets/BaseWidget' import { inputSpecTree } from '@/core/graph/nodeDef/inputSpecInterpreter' @@ -2056,10 +2057,7 @@ export class LiveGraphApplier { (node) => !createdSet.has(node.id) ) const existingBounds = existingNodes.map((node) => node.boundingRect) - const createdBounds = createdNodes.map((node): ReadOnlyRect => { - const { x, y, width, height } = rect(node) - return [x, y, width, height] - }) + const createdBounds = createdNodes.map(unrenderedBounds) const framesOnlyExisting = (group: LGraphGroup) => framesAny(group, existingBounds) && !framesAny(group, createdBounds) const kept = createdGroups.filter(framesOnlyExisting) @@ -2084,6 +2082,18 @@ export class LiveGraphApplier { } } +function unrenderedBounds(node: LGraphNode): ReadOnlyRect { + const titleHeight = + node.title_mode === TitleMode.TRANSPARENT_TITLE || + node.title_mode === TitleMode.NO_TITLE + ? 0 + : LiteGraph.NODE_TITLE_HEIGHT + const top = node.pos[1] - titleHeight + if (node.collapsed && !LiteGraph.vueNodesMode) + return [node.pos[0], top, LiteGraph.NODE_COLLAPSED_WIDTH, titleHeight] + return [node.pos[0], top, node.size[0], node.size[1] + titleHeight] +} + function framesAny( group: LGraphGroup, bounds: readonly ReadOnlyRect[] From 23c73b9e200f77bdb748f47a28167af5da2d20e1 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 4 Nov 2026 18:20:06 -0800 Subject: [PATCH 5/6] fix(agent): match measure() for a new node's membership bounds in the Vue renderer Review follow-up. unrenderedBounds read node.size and a title-mode-dependent collapsed height, so for a node created collapsed while the Vue renderer is active it returned the whole body instead of the collapsed strip measure() reports. A group drawn around existing nodes whose rect overlapped that body was then classified as framing a new node and moved away with it - the defect this PR fixes, in the other renderer. It now reads renderingSize, which already resolves the collapsed case, and the collapsed branch uses NODE_TITLE_HEIGHT as measure() does. New case "keeps a group clear of a new collapsed node it only overlaps unrendered" fails at a356448334 (`expected [1360, 20] to deeply equal [100, -20]`) and passes here. --- .../agentCrdtProjection.integration.test.ts | 50 +++++++++++++++++++ .../extensions/agent/crdt/liveGraphApplier.ts | 10 +++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts index 272cdb9e074..889b8d6c400 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts @@ -658,6 +658,56 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { group.pos[1] - inserted.pos[1] ]).toEqual([-20, -50]) }) + it('keeps a group clear of a new collapsed node it only overlaps unrendered', () => { + LiteGraph.vueNodesMode = true + onTestFinished(() => { + LiteGraph.vueNodesMode = false + }) + const { graph, source, note } = buildLiveGraph() + source.setSize([200, 100]) + note.pos = [1000, 20] + note.setSize([200, 100]) + source.updateArea() + note.updateArea() + const { host, hostEdit, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()) + ) + onTestFinished(destroy) + + hostEdit( + () => + applyHostOp(host, { + ...HOST_OP, + op_id: '1a66'.padEnd(32, '0'), + op: 'insert_workflow', + workflow: { + nodes: [ + { + id: 1, + type: 'TestSource', + pos: [20, 30], + size: [200, 100], + flags: { collapsed: true }, + widgets_values: [20, 7] + } + ], + groups: [ + { id: 1, title: 'Around source', bounding: [100, -20, 300, 200] } + ] + } + }), + { actor: HOST_OP.actor, catchUp: false } + ) + + const inserted = graph._nodes.find( + (node) => node !== source && node !== note + ) + const group = graph.groups.find(({ title }) => title === 'Around source') + assert(inserted && group, 'the insertion reached the canvas') + expect([...inserted.pos]).not.toEqual([20, 30]) + expect([...group.pos]).toEqual([100, -20]) + }) it('keeps a group on a collapsed node it frames as rendered', () => { const { graph, source, note } = buildLiveGraph() source.setSize([600, 100]) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index a47d2eb95d7..2b409fe8cde 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -2090,8 +2090,14 @@ function unrenderedBounds(node: LGraphNode): ReadOnlyRect { : LiteGraph.NODE_TITLE_HEIGHT const top = node.pos[1] - titleHeight if (node.collapsed && !LiteGraph.vueNodesMode) - return [node.pos[0], top, LiteGraph.NODE_COLLAPSED_WIDTH, titleHeight] - return [node.pos[0], top, node.size[0], node.size[1] + titleHeight] + return [ + node.pos[0], + top, + LiteGraph.NODE_COLLAPSED_WIDTH, + LiteGraph.NODE_TITLE_HEIGHT + ] + const size = node.renderingSize + return [node.pos[0], top, size[0], size[1] + titleHeight] } function framesAny( From 506e4d996354797db1fa27d6cf82ed6b1afc20d3 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 5 Nov 2026 03:04:20 +0000 Subject: [PATCH 6/6] fix(agent): measure group membership like the next frame does A new collapsed legacy node was judged at the 80px fallback width, so a group framing its rendered title was classified as existing-only and split from it. Existing nodes that have not been drawn yet had zero bounds, so a group-only insertion around them was moved away. Membership now measures undrawn nodes with LiteGraph's own measure() using the canvas context (title-aware collapsed width), keeps rendered existing nodes' boundingRect, and leaves the collapsed-width cache for the next frame. The canvas-context test double uses typed partial fixtures instead of a double assertion. --- .../agentCrdtProjection.integration.test.ts | 112 +++++++++++++++++- .../extensions/agent/crdt/liveGraphApplier.ts | 49 ++++---- 2 files changed, 135 insertions(+), 26 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts index 889b8d6c400..57bae5bc35e 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts @@ -1,4 +1,5 @@ import { applyOps, mint, nodesMap } from '@comfyorg/comfy-multi-player' +import { fromPartial } from '@total-typescript/shoehorn' import type { Op, WidgetCatalog, @@ -9,6 +10,7 @@ import * as Y from 'yjs' import { assert } from '@/base/assert' import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' +import type { LGraphCanvas } from '@/lib/litegraph/src/litegraph' import type { ISerialisedGraph } from '@comfyorg/graph-model/serialisation' import { useNodeDataStore } from '@/stores/nodeDataStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' @@ -100,6 +102,14 @@ function buildLiveGraph() { return { graph, source, note } } +/** A canvas context whose text measures {@link width} px wide. */ +function measuringContext(width: number) { + return fromPartial({ + font: '', + measureText: () => fromPartial({ width }) + }) +} + function snapshot(graph: LGraph) { const scope = graphScopeOf(graph) const widgetStore = useWidgetValueStore() @@ -135,10 +145,14 @@ function snapshot(graph: LGraph) { } } -function bindAndCatchUp(graph: LGraph, saved: ISerialisedGraph) { +function bindAndCatchUp( + graph: LGraph, + saved: ISerialisedGraph, + deps: ConstructorParameters[1] = {} +) { const host = mint(toWorkflowJson(saved), CATALOG) const follower = new FollowerDoc() - const projection = new AgentCrdtProjection(() => graph) + const projection = new AgentCrdtProjection(() => graph, deps) projection.bind(WORKFLOW_ID, follower) let seq = 0 const deliver = (update: Uint8Array, classification: FrameClassification) => { @@ -708,15 +722,101 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { expect([...inserted.pos]).not.toEqual([20, 30]) expect([...group.pos]).toEqual([100, -20]) }) + it('places a group with a new collapsed node it frames by its rendered title width', () => { + const { graph, source, note } = buildLiveGraph() + source.setSize([200, 100]) + note.pos = [1000, 20] + note.setSize([200, 100]) + source.updateArea() + note.updateArea() + const ctx = measuringContext(400) + const { host, hostEdit, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()), + { + getCanvas: () => + fromPartial({ + ctx, + emitBeforeChange: () => {}, + emitAfterChange: () => {} + }) + } + ) + onTestFinished(destroy) + + hostEdit( + () => + applyHostOp(host, { + ...HOST_OP, + op_id: '1a67'.padEnd(32, '0'), + op: 'insert_workflow', + workflow: { + nodes: [ + { + id: 1, + type: 'TestSource', + pos: [20, 30], + size: [200, 100], + flags: { collapsed: true }, + widgets_values: [20, 7] + } + ], + groups: [ + { id: 1, title: 'Around both', bounding: [100, -20, 300, 200] } + ] + } + }), + { actor: HOST_OP.actor, catchUp: false } + ) + + const inserted = graph._nodes.find( + (node) => node !== source && node !== note + ) + const group = graph.groups.find(({ title }) => title === 'Around both') + assert(inserted && group, 'the insertion reached the canvas') + expect([...inserted.pos]).not.toEqual([20, 30]) + expect([ + group.pos[0] - inserted.pos[0], + group.pos[1] - inserted.pos[1] + ]).toEqual([80, -50]) + }) + it('keeps a group-only insertion on existing nodes that have not been drawn yet', () => { + const { graph, source, note } = buildLiveGraph() + source.pos = [1000, 500] + source.setSize([200, 100]) + note.setSize([200, 100]) + const { host, hostEdit, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()) + ) + onTestFinished(destroy) + + hostEdit( + () => + applyHostOp(host, { + ...HOST_OP, + op_id: '1a68'.padEnd(32, '0'), + op: 'insert_workflow', + workflow: { + nodes: [], + groups: [ + { id: 1, title: 'Around source', bounding: [990, 460, 240, 160] } + ] + } + }), + { actor: HOST_OP.actor, catchUp: false } + ) + + const group = graph.groups.find(({ title }) => title === 'Around source') + assert(group, 'the inserted group reached the canvas') + expect([...group.pos]).toEqual([990, 460]) + }) it('keeps a group on a collapsed node it frames as rendered', () => { const { graph, source, note } = buildLiveGraph() source.setSize([600, 100]) note.pos = [1000, 20] source.collapse(true) - const context = { - font: '', - measureText: () => ({ width: 400 }) - } as unknown as CanvasRenderingContext2D + const context = measuringContext(400) source.updateArea(context) note.updateArea() const { host, hostEdit, destroy } = bindAndCatchUp( diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index 2b409fe8cde..a6969b2c195 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -47,7 +47,6 @@ import { import { transformInputSpecV1ToV2 } from '@comfyorg/object-info-parser/migration' import type { InputSpec as InputSpecV2 } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2' import { reportError } from '@comfyorg/telemetry/reportError' -import { TitleMode } from '@comfyorg/graph-model/globalEnums' import { zComfyNode, zGroup } from '@comfyorg/graph-model/workflowSchema' import { BaseWidget } from '@/lib/litegraph/src/widgets/BaseWidget' import { inputSpecTree } from '@/core/graph/nodeDef/inputSpecInterpreter' @@ -66,7 +65,8 @@ import { } from './agentSubgraphDefinitions' import type { Bounds as PlacementRect, - ReadOnlyRect + ReadOnlyRect, + Rect } from '@comfyorg/shared-frontend-utils/geometry' import { placementOffset } from './batchPlacement' @@ -2056,8 +2056,11 @@ export class LiveGraphApplier { const existingNodes = graph._nodes.filter( (node) => !createdSet.has(node.id) ) - const existingBounds = existingNodes.map((node) => node.boundingRect) - const createdBounds = createdNodes.map(unrenderedBounds) + const ctx = this.deps.getCanvas?.()?.ctx ?? undefined + const existingBounds = existingNodes.map((node) => + isRendered(node) ? node.boundingRect : pendingBounds(node, ctx) + ) + const createdBounds = createdNodes.map((node) => pendingBounds(node, ctx)) const framesOnlyExisting = (group: LGraphGroup) => framesAny(group, existingBounds) && !framesAny(group, createdBounds) const kept = createdGroups.filter(framesOnlyExisting) @@ -2082,22 +2085,28 @@ export class LiveGraphApplier { } } -function unrenderedBounds(node: LGraphNode): ReadOnlyRect { - const titleHeight = - node.title_mode === TitleMode.TRANSPARENT_TITLE || - node.title_mode === TitleMode.NO_TITLE - ? 0 - : LiteGraph.NODE_TITLE_HEIGHT - const top = node.pos[1] - titleHeight - if (node.collapsed && !LiteGraph.vueNodesMode) - return [ - node.pos[0], - top, - LiteGraph.NODE_COLLAPSED_WIDTH, - LiteGraph.NODE_TITLE_HEIGHT - ] - const size = node.renderingSize - return [node.pos[0], top, size[0], size[1] + titleHeight] +/** True once a frame has measured the node; until then its bounds are zero. */ +function isRendered(node: LGraphNode): boolean { + const [, , width, height] = node.boundingRect + return width > 0 || height > 0 +} + +/** + * The bounds the next frame will give a node that has not been drawn yet, + * measured the way LiteGraph does it, including a collapsed node's title width. + * `measure()` caches the collapsed width the node renders at; that cache is + * left for the next frame to write, because without a canvas context it would + * be written as zero. + */ +function pendingBounds( + node: LGraphNode, + ctx: CanvasRenderingContext2D | undefined +): ReadOnlyRect { + const out: Rect = [0, 0, 0, 0] + const collapsedWidth = node._collapsed_width + node.measure(out, ctx) + node._collapsed_width = collapsedWidth + return out } function framesAny(