From c102e83c99c63424fb6c3cdbda633d4858a1f18e Mon Sep 17 00:00:00 2001 From: Alexander Brown Date: Thu, 22 Oct 2026 23:38:55 +0000 Subject: [PATCH 1/6] fix(agent): observe meta.groups in DocChangeCollector and project groups DocChangeCollector now observes the meta root alongside nodes, links and definitions, and records each group id whose presence in meta.groups changed. LiveGraphApplier reads meta.groups at apply time and adds or removes the canvas group with graph.add/graph.remove, which emit the layout store's createGroup and deleteGroup operations. Groups the document never named are left alone, and inserted groups are offset together with the nodes inserted in the same batch. Fixes a remote clear leaving groups on the canvas and insert_workflow groups never appearing. Supersedes #21572. Amp-Thread-ID: https://ampcode.com/threads/T-01a165f2-b198-7546-8f21-e3b793d02e22 --- .../validation/schemas/workflowSchema.ts | 2 +- .../agentCrdtProjection.integration.test.ts | 129 +++++++++++++++++- .../agent/crdt/docChangeCollector.test.ts | 21 +++ .../agent/crdt/docChangeCollector.ts | 20 ++- .../extensions/agent/crdt/liveGraphApplier.ts | 88 ++++++++++-- 5 files changed, 243 insertions(+), 17 deletions(-) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.ts b/src/platform/workflow/validation/schemas/workflowSchema.ts index c12a406a80d..80747cf047f 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.ts @@ -342,7 +342,7 @@ const zSubgraphInstance = z }) .passthrough() -const zGroup = z +export const zGroup = z .object({ id: z.number().optional(), title: z.string(), diff --git a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts index 339bd00e3bf..f9028977126 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts @@ -1,6 +1,10 @@ -import { mint, nodesMap } from '@comfyorg/comfy-multi-player' -import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' -import { beforeEach, describe, expect, it, onTestFinished } from 'vitest' +import { applyOps, mint, nodesMap } from '@comfyorg/comfy-multi-player' +import type { + Op, + WidgetCatalog, + WorkflowJSON +} from '@comfyorg/comfy-multi-player' +import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' import * as Y from 'yjs' import { assert } from '@/base/assert' @@ -46,6 +50,18 @@ const CATALOG: WidgetCatalog = { types: { TestSource: { widget_order: ['steps', 'seed'] } } } +const HOST_OP: Pick = { + actor: 'agent:test:turn-1', + base_version: 1, + stamp: [1, 'agent:test:turn-1'] +} + +function applyHostOp(host: Y.Doc, op: Op) { + expect(applyOps(host, [op], CATALOG).outcomes).toEqual([ + { op_id: op.op_id, outcome: 'applied' } + ]) +} + type FrameClassification = Pick & Partial> @@ -367,4 +383,111 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { expect(source.title).toBe('Reminted') expect(graph.links.size).toBe(0) }) + + it('removes the groups a host clear drops and keeps a group the document never held', () => { + const { graph } = buildLiveGraph() + const prompt = new LiteGraph.LGraphGroup('Prompt group') + const output = new LiteGraph.LGraphGroup('Output group') + graph.add(prompt) + graph.add(output) + const { host, hostEdit, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()) + ) + onTestFinished(destroy) + const drawnAfterMint = new LiteGraph.LGraphGroup('Drawn after mint') + graph.add(drawnAfterMint) + + hostEdit(() => + applyHostOp(host, { + ...HOST_OP, + op_id: 'c1ea'.padEnd(32, '0'), + op: 'clear', + removed_nodes: [1, 2] + }) + ) + + expect(graph._nodes).toEqual([]) + expect(graph.groups).toEqual([drawnAfterMint]) + }) + + it('keeps only the groups the document holds again when retrying', () => { + const { graph } = buildLiveGraph() + const restored = new LiteGraph.LGraphGroup('Prompt group') + const dropped = new LiteGraph.LGraphGroup('Output group') + graph.add(restored) + graph.add(dropped) + const { host, hostEdit, projection, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()) + ) + onTestFinished(destroy) + const mintedGroups = host.getMap('meta').get('groups') + assert(Array.isArray(mintedGroups), 'mint wrote meta.groups') + const [restoredGroup] = mintedGroups + + function fail(): never { + throw new Error('remove failed') + } + vi.spyOn(graph, 'remove') + .mockImplementationOnce(fail) + .mockImplementationOnce(fail) + hostEdit(() => host.getMap('meta').set('groups', [])) + expect(projection.hasPendingFailure(WORKFLOW_ID)).toBe(true) + expect(graph.groups).toEqual([restored, dropped]) + + hostEdit(() => host.getMap('meta').set('groups', [restoredGroup])) + expect(projection.retryFailed(WORKFLOW_ID)).toMatchObject({ + failureCount: 0 + }) + expect(graph.groups).toEqual([restored]) + expect(projection.hasPendingFailure(WORKFLOW_ID)).toBe(false) + }) + + it('creates the groups an inserted workflow carries, placed with its 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: '1a5e'.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: 'Inserted group', bounding: [0, 10, 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 === 'Inserted group') + 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, -20]) + expect([...group.size]).toEqual([300, 200]) + }) }) diff --git a/src/workbench/extensions/agent/crdt/docChangeCollector.test.ts b/src/workbench/extensions/agent/crdt/docChangeCollector.test.ts index bd49c1cc1ff..7cebea60771 100644 --- a/src/workbench/extensions/agent/crdt/docChangeCollector.test.ts +++ b/src/workbench/extensions/agent/crdt/docChangeCollector.test.ts @@ -162,6 +162,27 @@ describe('DocChangeCollector', () => { expect(changes.nodes.size).toBe(0) }) + it('records the group ids meta.groups gains or loses, ignoring other meta keys', () => { + const { doc, collector } = setup() + const group = (id: number) => ({ + id, + title: `Group ${id}`, + bounding: [0, 0, 200, 100] + }) + const meta = doc.getMap('meta') + doc.transact(() => meta.set('groups', [group(7), group(9)])) + expect(collector.take().groups).toEqual(new Set(['7', '9'])) + + doc.transact(() => meta.set('last_node_id', 12)) + expect(collector.take().groups).toEqual(new Set()) + + doc.transact(() => meta.set('groups', [group(9), group(11)])) + expect(collector.take().groups).toEqual(new Set(['7', '11'])) + + doc.transact(() => meta.set('groups', [])) + expect(collector.take().groups).toEqual(new Set(['9', '11'])) + }) + it('records link keys, clears on take, and stops observing after destroy', () => { const { doc, collector } = setup() diff --git a/src/workbench/extensions/agent/crdt/docChangeCollector.ts b/src/workbench/extensions/agent/crdt/docChangeCollector.ts index ecd8588a1aa..d95fd1e6b81 100644 --- a/src/workbench/extensions/agent/crdt/docChangeCollector.ts +++ b/src/workbench/extensions/agent/crdt/docChangeCollector.ts @@ -7,7 +7,7 @@ import * as Y from 'yjs' import { definitionsObserverRoot } from './agentSubgraphDefinitions' import type { FrameChanges, NodeChange } from './liveGraphApplier' -import { SYNCED_NODE_FIELDS } from './liveGraphApplier' +import { metaMap, readDocGroups, SYNCED_NODE_FIELDS } from './liveGraphApplier' /** * Collects what the Y observers on one follower document report between two @@ -27,17 +27,21 @@ export class DocChangeCollector { Map | 'all'> >() private readonly interiorDefinitions = new Set() + private readonly groups = new Set() private readonly nodesMap: Y.Map> private readonly linksMap: Y.Map private readonly definitionsMap: Y.Map + private readonly metaMap: Y.Map constructor(doc: Y.Doc) { this.nodesMap = nodesMap(doc) this.linksMap = linksMap(doc) this.definitionsMap = definitionsObserverRoot(doc) + this.metaMap = metaMap(doc) this.nodesMap.observeDeep(this.onNodesChanged) this.linksMap.observe(this.onLinksChanged) this.definitionsMap.observeDeep(this.onDefinitionsChanged) + this.metaMap.observe(this.onMetaChanged) } /** Returns everything collected so far without clearing it. */ @@ -53,7 +57,8 @@ export class DocChangeCollector { new Map(nodes) ]) ), - interiorDefinitions: new Set(this.interiorDefinitions) + interiorDefinitions: new Set(this.interiorDefinitions), + groups: new Set(this.groups) } } @@ -71,12 +76,14 @@ export class DocChangeCollector { this.links.clear() this.interiors.clear() this.interiorDefinitions.clear() + this.groups.clear() } destroy(): void { this.nodesMap.unobserveDeep(this.onNodesChanged) this.linksMap.unobserve(this.onLinksChanged) this.definitionsMap.unobserveDeep(this.onDefinitionsChanged) + this.metaMap.unobserve(this.onMetaChanged) this.discard() } @@ -198,4 +205,13 @@ export class DocChangeCollector { private readonly onLinksChanged = (event: Y.YMapEvent): void => { for (const id of event.keysChanged) this.links.add(id) } + + private readonly onMetaChanged = (event: Y.YMapEvent): void => { + const change = event.changes.keys.get('groups') + if (!change) return + const before = readDocGroups(change.oldValue) + const after = readDocGroups(this.metaMap.get('groups')) + for (const id of before.keys()) if (!after.has(id)) this.groups.add(id) + for (const id of after.keys()) if (!before.has(id)) this.groups.add(id) + } } diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index d3f83be05fb..a2bb6f6ac43 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -21,19 +21,27 @@ import type { INodeInputSlot } from '@/lib/litegraph/src/types/slots' import { LGraphCanvas } from '@/lib/litegraph/src/LGraphCanvas' import { withGraphIntentSource } from '@/lib/litegraph/src/graphIntents' import { detachSerialisedLinks } from '@/lib/litegraph/src/linkDeduplication' -import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' +import { + LGraphGroup, + LGraphNode, + LiteGraph +} from '@/lib/litegraph/src/litegraph' import type { LGraph } from '@/lib/litegraph/src/litegraph' import { topologicalSortSubgraphs } from '@/lib/litegraph/src/subgraph/subgraphDeduplication' import type { ExportedSubgraph, ISerialisableNodeInput, ISerialisableNodeOutput, + ISerialisedGroup, ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { isWidgetValue } from '@/lib/litegraph/src/types/widgets' import { reportError } from '@/platform/telemetry/reportError' -import { zComfyNode } from '@/platform/workflow/validation/schemas/workflowSchema' +import { + zComfyNode, + zGroup +} from '@/platform/workflow/validation/schemas/workflowSchema' import { inputSpecTree } from '@/schemas/nodeDef/inputSpecTree' import { isUuidShapedSubgraphId } from '@/schemas/subgraphIdSchema' import type { LinkId } from '@/types/linkId' @@ -73,6 +81,9 @@ export interface FrameChanges { interiors?: InteriorChanges interiorDefinitions?: ReadonlySet + + /** Root group ids that `meta.groups` gained or lost. */ + groups?: ReadonlySet } export interface RemoteApplyContext { @@ -213,6 +224,24 @@ const zDocNodeFields = zComfyNode [OPAQUE_WIDGETS_KEY]: zWidgetRecord.optional() }) +const zDocGroup = zGroup.extend({ id: z.number() }) + +/** The document root holding `groups` among its passthrough keys. */ +export function metaMap(doc: Y.Doc): Y.Map { + return doc.getMap('meta') +} + +/** The `meta.groups` records that configure a canvas group, keyed by id. */ +export function readDocGroups(groups: unknown): Map { + const parsed = new Map() + if (!Array.isArray(groups)) return parsed + for (const record of groups) { + const group = zDocGroup.safeParse(structuredClone(record)) + if (group.success) parsed.set(String(group.data.id), group.data) + } + return parsed +} + /** * Only the actor's kind is reported: the segments after it in * `agent::` and `human::` identify a user. @@ -593,6 +622,13 @@ function positionalWidgetValues( }) } +function addGroup(graph: LGraph, serialised: ISerialisedGroup): LGraphGroup { + const group = new LGraphGroup() + group.configure(serialised) + graph.add(group) + return group +} + function missingNode(docNode: DocNode): LGraphNode { const node = new LGraphNode( docNode.serialised.title || docNode.type || 'Missing Node', @@ -673,8 +709,14 @@ export class LiveGraphApplier { this.applyInteriors(graph, doc, changes, context, mode) this.applyLinks(graph, doc, [...changes.links], context) + const createdGroups = this.applyGroups( + graph, + doc, + changes.groups, + context + ) if (mode === 'merge') - this.placeBatch(graph, created, { + this.placeBatch(graph, created, createdGroups, { moveCollisions: context.liveAgentTurn === true }) return { @@ -820,6 +862,26 @@ export class LiveGraphApplier { if (node) graph.remove(node) } + private applyGroups( + graph: LGraph, + doc: Y.Doc, + ids: ReadonlySet | undefined, + context: RemoteApplyContext + ): LGraphGroup[] { + if (!ids?.size) return [] + const docGroups = readDocGroups(metaMap(doc).get('groups')) + const created: LGraphGroup[] = [] + for (const id of ids) { + this.try(context, () => { + const live = graph.groups.find((group) => String(group.id) === id) + const docGroup = docGroups.get(id) + if (live && !docGroup) graph.remove(live) + else if (!live && docGroup) created.push(addGroup(graph, docGroup)) + }) + } + return created + } + private upsertNode( graph: LGraph, doc: Y.Doc, @@ -1441,17 +1503,21 @@ export class LiveGraphApplier { private placeBatch( graph: LGraph, created: readonly NodeId[], + createdGroups: readonly LGraphGroup[], { moveCollisions }: { moveCollisions: boolean } ): void { if (created.length === 0) return const createdSet = new Set(created) - const rect = (node: LGraphNode): PlacementRect => ({ - x: node.pos[0], - y: node.pos[1], - width: node.size[0], - height: node.size[1] + const rect = (item: LGraphNode | LGraphGroup): PlacementRect => ({ + x: item.pos[0], + y: item.pos[1], + width: item.size[0], + height: item.size[1] }) - const incoming = graph._nodes.filter((node) => createdSet.has(node.id)) + const incoming = [ + ...graph._nodes.filter((node) => createdSet.has(node.id)), + ...createdGroups + ] const offset = placementOffset({ existing: graph._nodes .filter((node) => !createdSet.has(node.id)) @@ -1461,8 +1527,8 @@ export class LiveGraphApplier { moveCollisions }) if (!offset) return - for (const node of incoming) { - node.pos = [node.pos[0] + offset.dx, node.pos[1] + offset.dy] + for (const item of incoming) { + item.pos = [item.pos[0] + offset.dx, item.pos[1] + offset.dy] } } } From b8d62a6aefa947667b7447dba745105b56595ab0 Mon Sep 17 00:00:00 2001 From: Alexander Brown Date: Thu, 22 Oct 2026 23:49:59 +0000 Subject: [PATCH 2/6] fix(agent): place inserted batches that carry only groups Amp-Thread-ID: https://ampcode.com/threads/T-01a165f2-b198-7546-8f21-e3b793d02e22 --- .../agentCrdtProjection.integration.test.ts | 30 +++++++++++++++++++ .../extensions/agent/crdt/liveGraphApplier.ts | 1 - 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts index f9028977126..193e4804859 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts @@ -490,4 +490,34 @@ 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', () => { + 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: '1a5f'.padEnd(32, '0'), + op: 'insert_workflow', + workflow: { + nodes: [], + groups: [ + { id: 1, title: 'Inserted group', bounding: [0, 10, 300, 200] } + ] + } + }), + { 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]).not.toEqual([0, 10]) + }) }) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index a2bb6f6ac43..1bafa496380 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -1506,7 +1506,6 @@ export class LiveGraphApplier { createdGroups: readonly LGraphGroup[], { moveCollisions }: { moveCollisions: boolean } ): void { - if (created.length === 0) return const createdSet = new Set(created) const rect = (item: LGraphNode | LGraphGroup): PlacementRect => ({ x: item.pos[0], From 366821ce50754e1edd1c512315a11758f616eb4f Mon Sep 17 00:00:00 2001 From: Alexander Brown Date: Fri, 23 Oct 2026 00:09:58 +0000 Subject: [PATCH 3/6] test: assert the exact placement of a group-only insertion --- .../agent/crdt/agentCrdtProjection.integration.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts index 193e4804859..d311db6a758 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts @@ -518,6 +518,6 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { const group = graph.groups.find(({ title }) => title === 'Inserted group') assert(group, 'the inserted group reached the canvas') - expect([...group.pos]).not.toEqual([0, 10]) + expect([...group.pos]).toEqual([680, 20]) }) }) From d65b0ad110d2bee01bbd84a8441b1f1e0d08f831 Mon Sep 17 00:00:00 2001 From: Alexander Brown Date: Sun, 1 Nov 2026 02:07:18 +0000 Subject: [PATCH 4/6] fix(agent): inline the meta root lookup Amp-Thread-ID: https://ampcode.com/threads/T-01a165f2-b198-7546-8f21-e3b793d02e22 --- src/workbench/extensions/agent/crdt/docChangeCollector.ts | 4 ++-- src/workbench/extensions/agent/crdt/liveGraphApplier.ts | 7 +------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/docChangeCollector.ts b/src/workbench/extensions/agent/crdt/docChangeCollector.ts index d95fd1e6b81..75788eb5c56 100644 --- a/src/workbench/extensions/agent/crdt/docChangeCollector.ts +++ b/src/workbench/extensions/agent/crdt/docChangeCollector.ts @@ -7,7 +7,7 @@ import * as Y from 'yjs' import { definitionsObserverRoot } from './agentSubgraphDefinitions' import type { FrameChanges, NodeChange } from './liveGraphApplier' -import { metaMap, readDocGroups, SYNCED_NODE_FIELDS } from './liveGraphApplier' +import { readDocGroups, SYNCED_NODE_FIELDS } from './liveGraphApplier' /** * Collects what the Y observers on one follower document report between two @@ -37,7 +37,7 @@ export class DocChangeCollector { this.nodesMap = nodesMap(doc) this.linksMap = linksMap(doc) this.definitionsMap = definitionsObserverRoot(doc) - this.metaMap = metaMap(doc) + this.metaMap = doc.getMap('meta') this.nodesMap.observeDeep(this.onNodesChanged) this.linksMap.observe(this.onLinksChanged) this.definitionsMap.observeDeep(this.onDefinitionsChanged) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index 07f4ccfebe5..633e1b37f5a 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -226,11 +226,6 @@ const zDocNodeFields = zComfyNode const zDocGroup = zGroup.extend({ id: z.number() }) -/** The document root holding `groups` among its passthrough keys. */ -export function metaMap(doc: Y.Doc): Y.Map { - return doc.getMap('meta') -} - /** The `meta.groups` records that configure a canvas group, keyed by id. */ export function readDocGroups(groups: unknown): Map { const parsed = new Map() @@ -967,7 +962,7 @@ export class LiveGraphApplier { context: RemoteApplyContext ): LGraphGroup[] { if (!ids?.size) return [] - const docGroups = readDocGroups(metaMap(doc).get('groups')) + const docGroups = readDocGroups(doc.getMap('meta').get('groups')) const created: LGraphGroup[] = [] for (const id of ids) { this.try(context, () => { From febb594e80b156db6f53e95ab33499ce13f978d5 Mon Sep 17 00:00:00 2001 From: Alexander Brown Date: Sun, 1 Nov 2026 02:20:49 +0000 Subject: [PATCH 5/6] fix(agent): drop groups a replacement document no longer holds Amp-Thread-ID: https://ampcode.com/threads/T-01a165f2-b198-7546-8f21-e3b793d02e22 --- .../agent/crdt/liveGraphApplier.test.ts | 29 +++++++++++++++++++ .../extensions/agent/crdt/liveGraphApplier.ts | 7 +++++ 2 files changed, 36 insertions(+) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts index 05ad7cb6fa4..b0ac62e473c 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts @@ -2261,6 +2261,35 @@ describe('LiveGraphApplier change bracket', () => { } ) + it('drops the groups a replacement document no longer holds', () => { + const { graph, applier, applyCollected } = setup({ + nodes: [], + links: [], + groups: [ + { id: 5, title: 'Dropped', bounding: [0, 0, 100, 100] }, + { id: 6, title: 'Kept', bounding: [200, 0, 100, 100] } + ] + }) + applyCollected() + const replacement = followedDoc( + { + nodes: [], + links: [], + groups: [{ id: 6, title: 'Kept', bounding: [200, 0, 100, 100] }] + }, + CATALOG + ) + + applier.applyChanges( + replacement.doc, + replacement.collector.take(), + CONTEXT, + 'replace' + ) + + expect(graph.groups.map((group) => group.title)).toEqual(['Kept']) + }) + it('brackets a catch-up frame and a lineage replacement once each, and never without a graph', () => { const { canvas, events } = recordingCanvas() const { graph, doc, collector, applier } = setup(seed, { diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index 633e1b37f5a..7b299c92f0c 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -839,6 +839,13 @@ export class LiveGraphApplier { if (docLinks.has(String(id))) continue this.try(context, () => graph.removeLink(id)) } + const docGroups = readDocGroups(doc.getMap('meta').get('groups')) + const absentGroups = graph.groups.filter( + (group) => !docGroups.has(String(group.id)) + ) + for (const group of absentGroups) { + this.try(context, () => graph.remove(group)) + } } private write(graph: LGraph, context: RemoteApplyContext, fn: () => T): T { From aadd4aff751505d8e0a489248d47b1f60c5d5b4f Mon Sep 17 00:00:00 2001 From: Alexander Brown Date: Sun, 1 Nov 2026 02:22:21 +0000 Subject: [PATCH 6/6] fix(agent): place inserted batches clear of existing groups Amp-Thread-ID: https://ampcode.com/threads/T-01a165f2-b198-7546-8f21-e3b793d02e22 --- .../agentCrdtProjection.integration.test.ts | 32 +++++++++++++++++++ .../extensions/agent/crdt/liveGraphApplier.ts | 7 ++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts index d311db6a758..06858fc25d7 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts @@ -520,4 +520,36 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { assert(group, 'the inserted group reached the canvas') expect([...group.pos]).toEqual([680, 20]) }) + it('places a group-only insertion clear of the groups already on the canvas', () => { + const graph = new LGraph() + const existing = new LiteGraph.LGraphGroup('Existing group') + existing.pos = [0, 10] + existing.size = [300, 200] + graph.add(existing) + const { host, hostEdit, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()) + ) + onTestFinished(destroy) + + hostEdit( + () => + applyHostOp(host, { + ...HOST_OP, + op_id: '1a61'.padEnd(32, '0'), + op: 'insert_workflow', + workflow: { + nodes: [], + groups: [ + { id: 1, title: 'Inserted group', bounding: [0, 10, 300, 200] } + ] + } + }), + { 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([380, 10]) + }) }) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index 7b299c92f0c..ca60aac5daf 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -1618,9 +1618,10 @@ export class LiveGraphApplier { ...createdGroups ] const offset = placementOffset({ - existing: graph._nodes - .filter((node) => !createdSet.has(node.id)) - .map(rect), + existing: [ + ...graph._nodes.filter((node) => !createdSet.has(node.id)), + ...graph.groups.filter((group) => !createdGroups.includes(group)) + ].map(rect), viewport: this.deps.viewportBounds?.() ?? null, incoming: incoming.map(rect), moveCollisions