From a0a0d466c8a720d42943f7bed04f6d47d7bde4f9 Mon Sep 17 00:00:00 2001 From: bymyself Date: Mon, 31 Aug 2026 04:32:36 +0000 Subject: [PATCH 01/15] test(agent-crdt): cover follower snapshot reload projection --- .../ecsFollowerAdapter.integration.test.ts | 111 ++++++++++++++++++ .../agent/crdt/ecsFollowerAdapter.ts | 2 + 2 files changed, 113 insertions(+) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index d0b44c27991..8f67b356aaf 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -30,6 +30,36 @@ const scope = { owningGraphId: toOwningGraphId('root') } +function graphSnapshot() { + return { + nodes: useNodeDataStore() + .getGraphNodesFor('root', 'root') + .map((node) => ({ + id: node.id, + type: node.type, + title: node.title, + inputs: node.inputs, + outputs: node.outputs + })), + links: [...useLinkStore().graphTopologies(scope)].map((link) => ({ + id: link.id, + originNodeId: link.originNodeId, + originSlot: link.originSlot, + targetNodeId: link.targetNodeId, + targetSlot: link.targetSlot, + type: link.type + })), + widgets: [toNodeId(1), toNodeId(2)].flatMap((nodeId) => + ['seed', 'stale'].flatMap((name) => { + const widget = useWidgetValueStore().getWidget( + widgetId('root', nodeId, name) + ) + return widget ? [{ nodeId, name, value: widget.value }] : [] + }) + ) + } +} + function op(id: string, baseVersion: number, payload: object) { return { op_id: id, @@ -210,6 +240,87 @@ describe('EcsFollowerAdapter integration', () => { host.destroy() }) + it('reprojects a saved follower snapshot after reload without byte drift', () => { + const host = mint( + { + nodes: [ + { + id: 1, + type: 'Source', + title: 'Saved source', + pos: [10, 20], + size: [180, 90], + inputs: [], + outputs: [{ name: 'out', type: 'IMAGE', links: [9] }], + widgets_values: { seed: 42 } + }, + { + id: 2, + type: 'Sink', + pos: [300, 20], + inputs: [{ name: 'in', type: 'IMAGE', link: 9 }], + outputs: [] + } + ], + links: [[9, 1, 0, 2, 0, 'IMAGE']] + }, + catalog + ) + const update = Y.encodeStateAsUpdate(host) + const follower = new FollowerDoc() + const adapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + }) + ) + adapter.bind('wf', follower) + follower.applyRemoteUpdate(update) + expect( + adapter.applyFrame({ + workflowId: 'wf', + seq: 1, + update, + actor: 'agent:test', + opIds: ['initial-save'] + }) + ).toBe(true) + + const projectedBeforeReload = graphSnapshot() + const savedSnapshot = Y.encodeStateAsUpdate(follower.doc) + const savedVector = Y.encodeStateVector(follower.doc) + adapter.destroy() + follower.destroy() + + setActivePinia(createTestingPinia({ stubActions: false })) + const reloadedFollower = new FollowerDoc() + reloadedFollower.applyRemoteUpdate(savedSnapshot) + expect(Y.encodeStateAsUpdate(reloadedFollower.doc)).toEqual(savedSnapshot) + expect(Y.encodeStateVector(reloadedFollower.doc)).toEqual(savedVector) + + const reloadedAdapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + }) + ) + reloadedAdapter.bind('wf', reloadedFollower) + expect( + reloadedAdapter.applyFrame({ + workflowId: 'wf', + seq: 2, + update: savedSnapshot, + actor: 'agent:replay', + opIds: ['reload-replay'] + }) + ).toBe(true) + expect(graphSnapshot()).toEqual(projectedBeforeReload) + + reloadedAdapter.destroy() + reloadedFollower.destroy() + host.destroy() + }) + it('applies an implicit disconnect when delete-wins installs no replacement', () => { const host = mint({ nodes: [], links: [] }, catalog) const follower = new FollowerDoc() diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index 4ed49cddd6a..2383956ef53 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -205,6 +205,8 @@ export class EcsFollowerAdapter { onLinksChanged: (_event): void => undefined } + session.nodes.forEach((_node, id) => session.nodeActions.set(id, 'add')) + session.links.forEach((_link, id) => session.changedLinks.add(id)) session.onNodesChanged = (events) => this.onNodesChanged(session, events) session.onLinksChanged = (event) => this.onLinksChanged(session, event) return session From 214c02cee64c75a3beaf416383db1b06becd8d7a Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Mon, 31 Aug 2026 22:44:15 -0700 Subject: [PATCH 02/15] test: normalize follower graph snapshots Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16350#discussion_r3891730554 --- .../agent/crdt/ecsFollowerAdapter.integration.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index 8f67b356aaf..a4b8ea9ba2a 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -40,7 +40,8 @@ function graphSnapshot() { title: node.title, inputs: node.inputs, outputs: node.outputs - })), + })) + .sort((left, right) => String(left.id).localeCompare(String(right.id))), links: [...useLinkStore().graphTopologies(scope)].map((link) => ({ id: link.id, originNodeId: link.originNodeId, @@ -48,7 +49,7 @@ function graphSnapshot() { targetNodeId: link.targetNodeId, targetSlot: link.targetSlot, type: link.type - })), + })).sort((left, right) => Number(left.id) - Number(right.id)), widgets: [toNodeId(1), toNodeId(2)].flatMap((nodeId) => ['seed', 'stale'].flatMap((name) => { const widget = useWidgetValueStore().getWidget( From 596f9927442a7dd7390dd11a93ee384fa9bb6543 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Mon, 31 Aug 2026 22:44:25 -0700 Subject: [PATCH 03/15] test: verify follower bytes after snapshot replay Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16350#discussion_r3891730557 --- .../agent/crdt/ecsFollowerAdapter.integration.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index a4b8ea9ba2a..36e77014dba 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -316,6 +316,8 @@ describe('EcsFollowerAdapter integration', () => { }) ).toBe(true) expect(graphSnapshot()).toEqual(projectedBeforeReload) + expect(Y.encodeStateAsUpdate(reloadedFollower.doc)).toEqual(savedSnapshot) + expect(Y.encodeStateVector(reloadedFollower.doc)).toEqual(savedVector) reloadedAdapter.destroy() reloadedFollower.destroy() From f6ff92dd24414b1b3305a1b74659bfb27ff6e066 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Mon, 31 Aug 2026 22:45:36 -0700 Subject: [PATCH 04/15] fix: project saved follower snapshots during bind Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16350#issuecomment-5473908411 --- .../ecsFollowerAdapter.integration.test.ts | 27 +++++++------- .../agent/crdt/ecsFollowerAdapter.ts | 36 +++++++++++++++++-- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index 36e77014dba..a40a9cb66ae 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -42,14 +42,16 @@ function graphSnapshot() { outputs: node.outputs })) .sort((left, right) => String(left.id).localeCompare(String(right.id))), - links: [...useLinkStore().graphTopologies(scope)].map((link) => ({ - id: link.id, - originNodeId: link.originNodeId, - originSlot: link.originSlot, - targetNodeId: link.targetNodeId, - targetSlot: link.targetSlot, - type: link.type - })).sort((left, right) => Number(left.id) - Number(right.id)), + links: [...useLinkStore().graphTopologies(scope)] + .map((link) => ({ + id: link.id, + originNodeId: link.originNodeId, + originSlot: link.originSlot, + targetNodeId: link.targetNodeId, + targetSlot: link.targetSlot, + type: link.type + })) + .sort((left, right) => Number(left.id) - Number(right.id)), widgets: [toNodeId(1), toNodeId(2)].flatMap((nodeId) => ['seed', 'stale'].flatMap((name) => { const widget = useWidgetValueStore().getWidget( @@ -305,14 +307,11 @@ describe('EcsFollowerAdapter integration', () => { layout: { createNode: vi.fn(), deleteNodes: vi.fn() } }) ) - reloadedAdapter.bind('wf', reloadedFollower) expect( - reloadedAdapter.applyFrame({ - workflowId: 'wf', - seq: 2, - update: savedSnapshot, + reloadedAdapter.bind('wf', reloadedFollower, { + source: 'agent-remote', actor: 'agent:replay', - opIds: ['reload-replay'] + opId: 'reload-snapshot' }) ).toBe(true) expect(graphSnapshot()).toEqual(projectedBeforeReload) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index 2383956ef53..a6d47c5dbf4 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -130,12 +130,19 @@ export class EcsFollowerAdapter { constructor(private readonly mutations: MutationsForTarget) {} - bind(workflowId: string, follower: FollowerDoc): void { + bind( + workflowId: string, + follower: FollowerDoc, + baselineContext?: RemoteMutationContext + ): boolean { this.unbind(workflowId) const session = this.createSession(workflowId, follower) this.targets.set(workflowId, session) session.nodes.observeDeep(session.onNodesChanged) session.links.observe(session.onLinksChanged) + return baselineContext + ? this.projectBaseline(session, baselineContext) + : true } unbind(workflowId: string): void { @@ -205,13 +212,36 @@ export class EcsFollowerAdapter { onLinksChanged: (_event): void => undefined } - session.nodes.forEach((_node, id) => session.nodeActions.set(id, 'add')) - session.links.forEach((_link, id) => session.changedLinks.add(id)) session.onNodesChanged = (events) => this.onNodesChanged(session, events) session.onLinksChanged = (event) => this.onLinksChanged(session, event) return session } + private projectBaseline( + session: TargetSession, + context: RemoteMutationContext + ): boolean { + const nodes = [...session.nodes.keys()].flatMap((id) => { + const payload = readSemanticNode(session.follower.doc, id) + return payload ? [payload] : [] + }) + const nodeIds = new Set(nodes.map(({ id }) => String(id))) + const links = [...session.links.keys()].flatMap((id) => { + const link = readSemanticLink(session.follower.doc, id) + return link && + nodeIds.has(String(link.originNodeId)) && + nodeIds.has(String(link.targetNodeId)) + ? [link] + : [] + }) + const applied = session.mutations.batch(context, (batch) => { + for (const node of nodes) batch.reconcileNode(node) + for (const link of links) batch.connect(link) + }) + if (applied) session.reconcileNextFrame = false + return applied + } + private applyQueuedFrame(session: TargetSession, update: DocUpdate): void { const nodeActions = new Map(session.nodeActions) const changedWidgets = new Map( From fac9c6bd1e5c17cb704cdd0fe0fc7f02401779c3 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Mon, 31 Aug 2026 22:47:13 -0700 Subject: [PATCH 05/15] test: isolate baseline projection by workflow Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16350#discussion_r3894264491 --- .../ecsFollowerAdapter.integration.test.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index a40a9cb66ae..2236b9c1790 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -323,6 +323,34 @@ describe('EcsFollowerAdapter integration', () => { host.destroy() }) + it('does not project a preloaded document for an unrelated workflow', () => { + const host = mint( + { + nodes: [{ id: 1, type: 'Source', pos: [10, 20] }], + links: [] + }, + catalog + ) + const follower = new FollowerDoc() + follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) + const createLayout = vi.fn() + const adapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => scope, + layout: { createNode: createLayout, deleteNodes: vi.fn() } + }) + ) + + adapter.bind('wf-b', follower) + + expect(useNodeDataStore().getGraphNodesFor('root', 'root')).toEqual([]) + expect(createLayout).not.toHaveBeenCalled() + + adapter.destroy() + follower.destroy() + host.destroy() + }) + it('applies an implicit disconnect when delete-wins installs no replacement', () => { const host = mint({ nodes: [], links: [] }, catalog) const follower = new FollowerDoc() From 85caf43af296d9de359b8dbc74cdf625a9873604 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Mon, 31 Aug 2026 22:47:51 -0700 Subject: [PATCH 06/15] test: preserve populated stores across follower rebind Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16350#discussion_r3894264500 --- .../ecsFollowerAdapter.integration.test.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index 2236b9c1790..fb45f5cbf8c 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -351,6 +351,55 @@ describe('EcsFollowerAdapter integration', () => { host.destroy() }) + it('leaves an existing projection untouched when rebound', () => { + const host = mint( + { + nodes: [ + { + id: 1, + type: 'Source', + pos: [10, 20], + widgets_values: { seed: 42 } + } + ], + links: [] + }, + catalog + ) + const follower = new FollowerDoc() + const createLayout = vi.fn() + const deleteLayouts = vi.fn() + const adapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => scope, + layout: { createNode: createLayout, deleteNodes: deleteLayouts } + }) + ) + adapter.bind('wf', follower) + const update = Y.encodeStateAsUpdate(host) + follower.applyRemoteUpdate(update) + adapter.applyFrame({ workflowId: 'wf', seq: 1, update }) + const [projected] = useNodeDataStore().getGraphNodesFor('root', 'root') + createLayout.mockClear() + deleteLayouts.mockClear() + + adapter.bind('wf', follower) + + expect(useNodeDataStore().getGraphNodesFor('root', 'root')).toEqual([ + projected + ]) + expect( + useWidgetValueStore().getWidget(widgetId('root', toNodeId(1), 'seed')) + ?.value + ).toBe(42) + expect(createLayout).not.toHaveBeenCalled() + expect(deleteLayouts).not.toHaveBeenCalled() + + adapter.destroy() + follower.destroy() + host.destroy() + }) + it('applies an implicit disconnect when delete-wins installs no replacement', () => { const host = mint({ nodes: [], links: [] }, catalog) const follower = new FollowerDoc() From 1c8e7d39583f80e79a9343842e2e1c3f85d7b6c2 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Mon, 31 Aug 2026 22:48:28 -0700 Subject: [PATCH 07/15] test: tolerate dangling links in follower baselines Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16350#discussion_r3894264505 --- .../ecsFollowerAdapter.integration.test.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index fb45f5cbf8c..09e2ecf8b55 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -400,6 +400,49 @@ describe('EcsFollowerAdapter integration', () => { host.destroy() }) + it('projects baseline nodes while ignoring dangling links', () => { + const host = mint( + { + nodes: [ + { + id: 1, + type: 'Source', + pos: [10, 20], + outputs: [{ name: 'out', type: 'IMAGE', links: [9] }] + } + ], + links: [[9, 1, 0, 2, 0, 'IMAGE']] + }, + catalog + ) + const follower = new FollowerDoc() + follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) + const adapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + }) + ) + + expect( + adapter.bind('wf', follower, { + source: 'agent-remote', + actor: 'agent:replay', + opId: 'reload-snapshot' + }) + ).toBe(true) + expect( + useNodeDataStore() + .getGraphNodesFor('root', 'root') + .map(({ id }) => id) + ).toEqual([toNodeId(1)]) + expect([...useLinkStore().graphTopologies(scope)]).toEqual([]) + + adapter.destroy() + follower.destroy() + host.destroy() + }) + it('applies an implicit disconnect when delete-wins installs no replacement', () => { const host = mint({ nodes: [], links: [] }, catalog) const follower = new FollowerDoc() From 87c1d2a7a1437d04982efa5cd086b1bff6ffe1ed Mon Sep 17 00:00:00 2001 From: glary-bot Date: Tue, 1 Sep 2026 05:52:10 +0000 Subject: [PATCH 08/15] fix(agent-crdt): make follower baseline projection explicit bind() seeded pending reconciliation from whatever the follower doc held. The bridge keeps one long-lived doc across workflow switches, so binding a new workflow staged the previous one's nodes and links and projected them into the new scope on its next frame, and an ordinary unbind/rebind re-reconciled every node -- resetting position, size, z-order and local widget values. Seeding every link also made the first drain one all-or-nothing batch, which prepare() rejects whole on the first dangling link. Replace it with projectBaseline(), an opt-in drain that commits under its own provenance instead of riding on the next frame, skips links whose endpoints are absent, and reports the batch result. --- .../ecsFollowerAdapter.integration.test.ts | 262 ++++++++++++------ .../agent/crdt/ecsFollowerAdapter.ts | 72 ++--- 2 files changed, 221 insertions(+), 113 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index 09e2ecf8b55..aca2996d326 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -1,4 +1,4 @@ -import { applyOps, mint } from '@comfyorg/comfy-multi-player' +import { applyOps, linksMap, mint } from '@comfyorg/comfy-multi-player' import type { WidgetCatalog } from '@comfyorg/comfy-multi-player' import { createTestingPinia } from '@pinia/testing' import { setActivePinia } from 'pinia' @@ -30,20 +30,27 @@ const scope = { owningGraphId: toOwningGraphId('root') } +function byId(entries: T[]): T[] { + return entries.sort((left, right) => + String(left.id).localeCompare(String(right.id)) + ) +} + function graphSnapshot() { return { - nodes: useNodeDataStore() - .getGraphNodesFor('root', 'root') - .map((node) => ({ - id: node.id, - type: node.type, - title: node.title, - inputs: node.inputs, - outputs: node.outputs - })) - .sort((left, right) => String(left.id).localeCompare(String(right.id))), - links: [...useLinkStore().graphTopologies(scope)] - .map((link) => ({ + nodes: byId( + useNodeDataStore() + .getGraphNodesFor('root', 'root') + .map((node) => ({ + id: node.id, + type: node.type, + title: node.title, + inputs: node.inputs, + outputs: node.outputs + })) + ), + links: byId( + [...useLinkStore().graphTopologies(scope)].map((link) => ({ id: link.id, originNodeId: link.originNodeId, originSlot: link.originSlot, @@ -51,7 +58,7 @@ function graphSnapshot() { targetSlot: link.targetSlot, type: link.type })) - .sort((left, right) => Number(left.id) - Number(right.id)), + ), widgets: [toNodeId(1), toNodeId(2)].flatMap((nodeId) => ['seed', 'stale'].flatMap((name) => { const widget = useWidgetValueStore().getWidget( @@ -307,11 +314,26 @@ describe('EcsFollowerAdapter integration', () => { layout: { createNode: vi.fn(), deleteNodes: vi.fn() } }) ) + reloadedAdapter.bind('wf', reloadedFollower) + expect(graphSnapshot()).toEqual({ nodes: [], links: [], widgets: [] }) + expect( - reloadedAdapter.bind('wf', reloadedFollower, { + reloadedAdapter.projectBaseline('wf', { source: 'agent-remote', actor: 'agent:replay', - opId: 'reload-snapshot' + opId: 'reload-replay' + }) + ).toBe(true) + expect(graphSnapshot()).toEqual(projectedBeforeReload) + + // A redelivered snapshot carries no Yjs delta: it must change nothing. + expect( + reloadedAdapter.applyFrame({ + workflowId: 'wf', + seq: 2, + update: savedSnapshot, + actor: 'agent:replay', + opIds: ['reload-replay'] }) ).toBe(true) expect(graphSnapshot()).toEqual(projectedBeforeReload) @@ -323,35 +345,7 @@ describe('EcsFollowerAdapter integration', () => { host.destroy() }) - it('does not project a preloaded document for an unrelated workflow', () => { - const host = mint( - { - nodes: [{ id: 1, type: 'Source', pos: [10, 20] }], - links: [] - }, - catalog - ) - const follower = new FollowerDoc() - follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) - const createLayout = vi.fn() - const adapter = new EcsFollowerAdapter( - createGraphMutations({ - getScope: () => scope, - layout: { createNode: createLayout, deleteNodes: vi.fn() } - }) - ) - - adapter.bind('wf-b', follower) - - expect(useNodeDataStore().getGraphNodesFor('root', 'root')).toEqual([]) - expect(createLayout).not.toHaveBeenCalled() - - adapter.destroy() - follower.destroy() - host.destroy() - }) - - it('leaves an existing projection untouched when rebound', () => { + it('skips baseline links whose endpoints are missing from the doc', () => { const host = mint( { nodes: [ @@ -359,48 +353,58 @@ describe('EcsFollowerAdapter integration', () => { id: 1, type: 'Source', pos: [10, 20], - widgets_values: { seed: 42 } + inputs: [], + outputs: [{ name: 'out', type: 'IMAGE', links: [9] }] + }, + { + id: 2, + type: 'Sink', + pos: [300, 20], + inputs: [{ name: 'in', type: 'IMAGE', link: 9 }], + outputs: [] } ], - links: [] + links: [[9, 1, 0, 2, 0, 'IMAGE']] }, catalog ) const follower = new FollowerDoc() - const createLayout = vi.fn() - const deleteLayouts = vi.fn() + follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) + linksMap(follower.doc).set('10', [10, '404', 0, '2', 0, 'IMAGE']) + const adapter = new EcsFollowerAdapter( createGraphMutations({ getScope: () => scope, - layout: { createNode: createLayout, deleteNodes: deleteLayouts } + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } }) ) adapter.bind('wf', follower) - const update = Y.encodeStateAsUpdate(host) - follower.applyRemoteUpdate(update) - adapter.applyFrame({ workflowId: 'wf', seq: 1, update }) - const [projected] = useNodeDataStore().getGraphNodesFor('root', 'root') - createLayout.mockClear() - deleteLayouts.mockClear() - - adapter.bind('wf', follower) + expect( + adapter.projectBaseline('wf', { + source: 'agent-remote', + actor: 'agent:replay', + opId: 'baseline' + }) + ).toBe(true) - expect(useNodeDataStore().getGraphNodesFor('root', 'root')).toEqual([ - projected - ]) expect( - useWidgetValueStore().getWidget(widgetId('root', toNodeId(1), 'seed')) - ?.value - ).toBe(42) - expect(createLayout).not.toHaveBeenCalled() - expect(deleteLayouts).not.toHaveBeenCalled() + useNodeDataStore() + .getGraphNodesFor('root', 'root') + .map(({ id }) => id) + ).toEqual([toNodeId(1), toNodeId(2)]) + expect( + useLinkStore().getTopology(scope.rootGraphId, toLinkId(9)) + ).toBeDefined() + expect( + useLinkStore().getTopology(scope.rootGraphId, toLinkId(10)) + ).toBeUndefined() adapter.destroy() follower.destroy() host.destroy() }) - it('projects baseline nodes while ignoring dangling links', () => { + it('reconciles already-projected nodes when a baseline is reprojected', () => { const host = mint( { nodes: [ @@ -408,35 +412,60 @@ describe('EcsFollowerAdapter integration', () => { id: 1, type: 'Source', pos: [10, 20], - outputs: [{ name: 'out', type: 'IMAGE', links: [9] }] + size: [180, 90], + inputs: [], + outputs: [{ name: 'out', type: 'IMAGE', links: [] }] } ], - links: [[9, 1, 0, 2, 0, 'IMAGE']] + links: [] }, catalog ) const follower = new FollowerDoc() follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) + const createLayout = vi.fn() + const deleteLayouts = vi.fn() const adapter = new EcsFollowerAdapter( createGraphMutations({ getScope: () => scope, - layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + layout: { createNode: createLayout, deleteNodes: deleteLayouts } }) ) + const context = { + source: 'agent-remote', + actor: 'agent:replay', + opId: 'baseline' + } as const + adapter.bind('wf', follower) + expect(adapter.projectBaseline('wf', context)).toBe(true) + const projected = graphSnapshot() + expect(deleteLayouts).not.toHaveBeenCalled() + createLayout.mockClear() + + adapter.unbind('wf') + adapter.bind('wf', follower) expect( - adapter.bind('wf', follower, { - source: 'agent-remote', - actor: 'agent:replay', - opId: 'reload-snapshot' + adapter.applyFrame({ + workflowId: 'wf', + seq: 1, + update: new Uint8Array(), + actor: 'agent:test', + opIds: ['unrelated'] }) ).toBe(true) - expect( - useNodeDataStore() - .getGraphNodesFor('root', 'root') - .map(({ id }) => id) - ).toEqual([toNodeId(1)]) - expect([...useLinkStore().graphTopologies(scope)]).toEqual([]) + expect(createLayout).not.toHaveBeenCalled() + expect(deleteLayouts).not.toHaveBeenCalled() + expect(graphSnapshot()).toEqual(projected) + + expect(adapter.projectBaseline('wf', context)).toBe(true) + expect(deleteLayouts).toHaveBeenCalledWith( + scope, + [toNodeId(1)], + expect.objectContaining({ opId: 'baseline' }) + ) + expect(createLayout).toHaveBeenCalledTimes(1) + expect(graphSnapshot()).toEqual(projected) adapter.destroy() follower.destroy() @@ -725,6 +754,79 @@ describe('EcsFollowerAdapter integration', () => { host.destroy() }) + it('never carries a reused doc into the next workflow bound to it', () => { + const host = mint( + { + nodes: [ + { + id: 1, + type: 'Source', + pos: [10, 20], + inputs: [], + outputs: [{ name: 'out', type: 'IMAGE', links: [9] }] + }, + { + id: 2, + type: 'Sink', + pos: [300, 20], + inputs: [{ name: 'in', type: 'IMAGE', link: 9 }], + outputs: [] + } + ], + links: [[9, 1, 0, 2, 0, 'IMAGE']] + }, + catalog + ) + const follower = new FollowerDoc() + follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) + + const projectedNodes: string[] = [] + const projectedLinks: string[] = [] + const adapter = new EcsFollowerAdapter( + (workflowId): GraphMutations => ({ + batch: (_context, define) => { + define({ + addNode: ({ id }) => projectedNodes.push(`${workflowId}:${id}`), + reconcileNode: ({ id }) => + projectedNodes.push(`${workflowId}:${id}`), + setWidget: () => undefined, + connect: ({ id }) => projectedLinks.push(`${workflowId}:${id}`), + removeLinks: () => undefined, + deleteNode: () => undefined, + clearSemanticGraph: () => undefined + }) + return true + }, + addNode: () => true, + setWidget: () => true, + connect: () => true, + deleteNode: () => true, + clearSemanticGraph: () => true + }) + ) + + // The bridge reuses one doc across a switch, so wf-b binds onto wf-a's. + adapter.bind('wf-a', follower) + adapter.unbind('wf-a') + adapter.bind('wf-b', follower) + expect( + adapter.applyFrame({ + workflowId: 'wf-b', + seq: 1, + update: new Uint8Array(), + actor: 'agent:test', + opIds: ['b'] + }) + ).toBe(true) + + expect(projectedNodes).toEqual([]) + expect(projectedLinks).toEqual([]) + + adapter.destroy() + follower.destroy() + host.destroy() + }) + it('keeps follower docs and apply queues isolated by workflow target', () => { const followerA = new FollowerDoc() const followerB = new FollowerDoc() diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index a6d47c5dbf4..f1afaed0526 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -130,19 +130,12 @@ export class EcsFollowerAdapter { constructor(private readonly mutations: MutationsForTarget) {} - bind( - workflowId: string, - follower: FollowerDoc, - baselineContext?: RemoteMutationContext - ): boolean { + bind(workflowId: string, follower: FollowerDoc): void { this.unbind(workflowId) const session = this.createSession(workflowId, follower) this.targets.set(workflowId, session) session.nodes.observeDeep(session.onNodesChanged) session.links.observe(session.onLinksChanged) - return baselineContext - ? this.projectBaseline(session, baselineContext) - : true } unbind(workflowId: string): void { @@ -172,6 +165,44 @@ export class EcsFollowerAdapter { return true } + /** + * Commit the bound doc's current content under its own provenance, for a doc + * preloaded from a saved snapshot: Yjs observers only report what changes + * after `bind`, so that content would otherwise stay unprojected until an + * unrelated later delta arrived. + * + * Opt-in, never done by `bind`, because the bridge reuses one long-lived + * follower doc across workflow switches: seeding at bind would project the + * previous workflow's graph into this one, and re-reconcile every node on an + * ordinary unbind/rebind of an unchanged doc. + * + * A link whose endpoints are missing is skipped rather than batched, since + * `prepare` rejects a whole batch on its first invalid entry. + */ + projectBaseline(workflowId: string, context: RemoteMutationContext): boolean { + const session = this.targets.get(workflowId) + if (!session) return false + + const nodes = [...session.nodes.keys()].flatMap((id) => { + const payload = readSemanticNode(session.follower.doc, id) + return payload ? [payload] : [] + }) + const projectedIds = new Set(nodes.map(({ id }) => String(id))) + const links = [...session.links.keys()].flatMap((id) => { + const link = readSemanticLink(session.follower.doc, id) + if (!link) return [] + const connectable = + projectedIds.has(String(link.originNodeId)) && + projectedIds.has(String(link.targetNodeId)) + return connectable ? [link] : [] + }) + + return session.mutations.batch(context, (batch) => { + for (const node of nodes) batch.reconcileNode(node) + for (const link of links) batch.connect(link) + }) + } + /** Explicit lineage reset only; reconnect/gap recovery never calls it. */ clearForReset(workflowId: string, context: RemoteMutationContext): boolean { const session = this.targets.get(workflowId) @@ -217,31 +248,6 @@ export class EcsFollowerAdapter { return session } - private projectBaseline( - session: TargetSession, - context: RemoteMutationContext - ): boolean { - const nodes = [...session.nodes.keys()].flatMap((id) => { - const payload = readSemanticNode(session.follower.doc, id) - return payload ? [payload] : [] - }) - const nodeIds = new Set(nodes.map(({ id }) => String(id))) - const links = [...session.links.keys()].flatMap((id) => { - const link = readSemanticLink(session.follower.doc, id) - return link && - nodeIds.has(String(link.originNodeId)) && - nodeIds.has(String(link.targetNodeId)) - ? [link] - : [] - }) - const applied = session.mutations.batch(context, (batch) => { - for (const node of nodes) batch.reconcileNode(node) - for (const link of links) batch.connect(link) - }) - if (applied) session.reconcileNextFrame = false - return applied - } - private applyQueuedFrame(session: TargetSession, update: DocUpdate): void { const nodeActions = new Map(session.nodeActions) const changedWidgets = new Map( From 7c3ae7d23b9ee9ca9dc2de63e30c50e49bba4454 Mon Sep 17 00:00:00 2001 From: glary-bot Date: Tue, 1 Sep 2026 07:11:34 +0000 Subject: [PATCH 09/15] fix(agent-crdt): harden the baseline projection The link filter only guarded one of the three ways prepare() rejects a connect, and prepare fails the whole batch on its first bad entry -- so a negative slot, or an out-of-range one, still discarded every node and link in the baseline. That is terminal for a preloaded doc, which gets no second projection. Mirror all three, counting only the slot records prepare keeps: readSemanticLink always supplies both slot arrays, so an empty one is truthy and every link off a node whose JSON omits its slots would fail. Clear the staged observer state and the reconcile flag after a successful baseline: a restore applied after bind stages the whole snapshot, and leaving it queued re-reconciled every node on the next frame. Gate on assertReadableSchema. Restores arrive out of band, so unlike applyFrame there is no bridge dispatch upstream to fail closed for them. The redelivery assertions never applied the snapshot, so they held because the doc was untouched rather than because Yjs deduplicated it. --- .../ecsFollowerAdapter.integration.test.ts | 121 ++++++++++++++++-- .../agent/crdt/ecsFollowerAdapter.ts | 53 ++++++-- 2 files changed, 158 insertions(+), 16 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index aca2996d326..34e4fd9981e 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -1,4 +1,9 @@ -import { applyOps, linksMap, mint } from '@comfyorg/comfy-multi-player' +import { + SCHEMA_VERSION, + applyOps, + linksMap, + mint +} from '@comfyorg/comfy-multi-player' import type { WidgetCatalog } from '@comfyorg/comfy-multi-player' import { createTestingPinia } from '@pinia/testing' import { setActivePinia } from 'pinia' @@ -326,7 +331,8 @@ describe('EcsFollowerAdapter integration', () => { ).toBe(true) expect(graphSnapshot()).toEqual(projectedBeforeReload) - // A redelivered snapshot carries no Yjs delta: it must change nothing. + // A redelivered snapshot integrates no new struct: it must change nothing. + reloadedFollower.applyRemoteUpdate(savedSnapshot) expect( reloadedAdapter.applyFrame({ workflowId: 'wf', @@ -345,7 +351,7 @@ describe('EcsFollowerAdapter integration', () => { host.destroy() }) - it('skips baseline links whose endpoints are missing from the doc', () => { + it('skips baseline links the graph would reject, keeping the rest', () => { const host = mint( { nodes: [ @@ -362,7 +368,8 @@ describe('EcsFollowerAdapter integration', () => { pos: [300, 20], inputs: [{ name: 'in', type: 'IMAGE', link: 9 }], outputs: [] - } + }, + { id: 3, type: 'Sink', pos: [500, 20] } ], links: [[9, 1, 0, 2, 0, 'IMAGE']] }, @@ -370,7 +377,12 @@ describe('EcsFollowerAdapter integration', () => { ) const follower = new FollowerDoc() follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) - linksMap(follower.doc).set('10', [10, '404', 0, '2', 0, 'IMAGE']) + const links = linksMap(follower.doc) + links.set('10', [10, '404', 0, '2', 0, 'IMAGE']) + links.set('11', [11, '1', 7, '2', 0, 'IMAGE']) + links.set('12', [12, '1', 0, '2', 7, 'IMAGE']) + links.set('13', [13, '1', 0, '3', 0, 'IMAGE']) + links.set('14', [14, '1', -1, '2', 0, 'IMAGE']) const adapter = new EcsFollowerAdapter( createGraphMutations({ @@ -391,13 +403,106 @@ describe('EcsFollowerAdapter integration', () => { useNodeDataStore() .getGraphNodesFor('root', 'root') .map(({ id }) => id) - ).toEqual([toNodeId(1), toNodeId(2)]) + ).toEqual([toNodeId(1), toNodeId(2), toNodeId(3)]) expect( useLinkStore().getTopology(scope.rootGraphId, toLinkId(9)) ).toBeDefined() + for (const rejected of [10, 11, 12, 13, 14]) { + expect( + useLinkStore().getTopology(scope.rootGraphId, toLinkId(rejected)) + ).toBeUndefined() + } + + adapter.destroy() + follower.destroy() + host.destroy() + }) + + it('does not replay the baseline again on the next frame', () => { + const host = mint( + { + nodes: [ + { + id: 1, + type: 'Source', + pos: [10, 20], + size: [180, 90], + inputs: [], + outputs: [{ name: 'out', type: 'IMAGE', links: [] }] + } + ], + links: [] + }, + catalog + ) + const createLayout = vi.fn() + const deleteLayouts = vi.fn() + const follower = new FollowerDoc() + const adapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => scope, + layout: { createNode: createLayout, deleteNodes: deleteLayouts } + }) + ) + + // Restoring after bind stages every node the snapshot carries. + adapter.bind('wf', follower) + follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) expect( - useLinkStore().getTopology(scope.rootGraphId, toLinkId(10)) - ).toBeUndefined() + adapter.projectBaseline('wf', { + source: 'agent-remote', + actor: 'agent:replay', + opId: 'baseline' + }) + ).toBe(true) + const projected = graphSnapshot() + createLayout.mockClear() + deleteLayouts.mockClear() + + expect( + adapter.applyFrame({ + workflowId: 'wf', + seq: 1, + update: new Uint8Array(), + actor: 'agent:test', + opIds: ['unrelated'] + }) + ).toBe(true) + + expect(createLayout).not.toHaveBeenCalled() + expect(deleteLayouts).not.toHaveBeenCalled() + expect(graphSnapshot()).toEqual(projected) + + adapter.destroy() + follower.destroy() + host.destroy() + }) + + it('refuses to project a doc this build cannot read', () => { + const host = mint( + { nodes: [{ id: 1, type: 'Source', pos: [0, 0] }], links: [] }, + catalog + ) + const follower = new FollowerDoc() + follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) + follower.doc.getMap('meta').set('schema_version', SCHEMA_VERSION + 1) + + const adapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + }) + ) + adapter.bind('wf', follower) + + expect( + adapter.projectBaseline('wf', { + source: 'agent-remote', + actor: 'agent:replay', + opId: 'baseline' + }) + ).toBe(false) + expect(useNodeDataStore().getGraphNodesFor('root', 'root')).toEqual([]) adapter.destroy() follower.destroy() diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index f1afaed0526..83a1f7a3d02 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -15,6 +15,7 @@ import { toNodeId } from '@/types/nodeId' import type { DocUpdate } from './docFrameClient' import type { FollowerDoc } from './followerDoc' +import { FollowerSchemaError, assertReadableSchema } from './schemaGuard' type NodeRootAction = 'add' | 'update' | 'delete' export type MutationsForTarget = @@ -93,6 +94,29 @@ function readNodeSlots( : 'originOutputs'] } +/** Slots `prepare` will keep: it drops every non-record entry before indexing. */ +function connectableSlots(slots: readonly unknown[] | undefined): number { + return (slots ?? []).filter( + (slot) => typeof slot === 'object' && slot !== null && !Array.isArray(slot) + ).length +} + +/** Every way `prepare` rejects a `connect`, mirrored: it fails the whole batch. */ +function isConnectable( + link: SemanticLinkPayload, + projectedIds: ReadonlySet +): boolean { + return ( + link.id >= 0 && + link.originSlot >= 0 && + link.targetSlot >= 0 && + projectedIds.has(String(link.originNodeId)) && + projectedIds.has(String(link.targetNodeId)) && + link.originSlot < connectableSlots(link.originOutputs) && + link.targetSlot < connectableSlots(link.targetInputs) + ) +} + function frameContext(update: DocUpdate): RemoteMutationContext { const opIds = update.opIds?.filter((id) => id.length > 0) return { @@ -176,12 +200,22 @@ export class EcsFollowerAdapter { * previous workflow's graph into this one, and re-reconcile every node on an * ordinary unbind/rebind of an unchanged doc. * - * A link whose endpoints are missing is skipped rather than batched, since - * `prepare` rejects a whole batch on its first invalid entry. + * An unprojectable link is skipped rather than batched, since `prepare` + * rejects a whole batch on its first invalid entry. + * + * Returns false when nothing is bound, when the doc fails the KA-11 read + * gate, or when the batch was rejected. Restores arrive out of band, so this + * is the only place that gate can run for them. */ projectBaseline(workflowId: string, context: RemoteMutationContext): boolean { const session = this.targets.get(workflowId) if (!session) return false + try { + assertReadableSchema(session.follower.doc) + } catch (error) { + if (!(error instanceof FollowerSchemaError)) throw error + return false + } const nodes = [...session.nodes.keys()].flatMap((id) => { const payload = readSemanticNode(session.follower.doc, id) @@ -190,17 +224,20 @@ export class EcsFollowerAdapter { const projectedIds = new Set(nodes.map(({ id }) => String(id))) const links = [...session.links.keys()].flatMap((id) => { const link = readSemanticLink(session.follower.doc, id) - if (!link) return [] - const connectable = - projectedIds.has(String(link.originNodeId)) && - projectedIds.has(String(link.targetNodeId)) - return connectable ? [link] : [] + return link && isConnectable(link, projectedIds) ? [link] : [] }) - return session.mutations.batch(context, (batch) => { + const projected = session.mutations.batch(context, (batch) => { for (const node of nodes) batch.reconcileNode(node) for (const link of links) batch.connect(link) }) + if (!projected) return false + + // The baseline is the whole doc, so whatever the observers staged is + // already in it; leaving it queued re-reconciles every node next frame. + this.discardSessionPending(session) + session.reconcileNextFrame = false + return true } /** Explicit lineage reset only; reconnect/gap recovery never calls it. */ From e1a69923646524f34cfaf6eca445bf3fd783c113 Mon Sep 17 00:00:00 2001 From: glary-bot Date: Tue, 1 Sep 2026 07:33:28 +0000 Subject: [PATCH 10/15] fix(agent-crdt): keep staged deletions across a baseline projection Clearing all pending state after a baseline dropped staged 'delete' node actions and stale link ids. Those are exactly what reading the doc cannot rediscover -- the doc no longer holds them -- so the next frame stopped emitting deleteNode/removeLinks and the entity became a permanent ghost. Drop only the ids this batch committed. Stop consuming reconcileNextFrame too. An empty doc passes the schema gate and its empty batch succeeds, so the flag was spent projecting nothing; the next frame carrying an already-present id then took addNode, which prepare rejects as 'already registered' -- discarding the whole frame after its pending state was cleared. graphSnapshot held the store's own slot arrays, which updateNode splices in place, so the reprojection assertion compared a snapshot to itself. Also: state what isConnectable does not mirror, disclose that no production caller exists yet, and report skipped entries instead of diverging silently. --- .../ecsFollowerAdapter.integration.test.ts | 72 ++++++++++++++++++- .../agent/crdt/ecsFollowerAdapter.ts | 39 +++++++--- 2 files changed, 100 insertions(+), 11 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index 34e4fd9981e..b24e73933ba 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -2,7 +2,8 @@ import { SCHEMA_VERSION, applyOps, linksMap, - mint + mint, + nodesMap } from '@comfyorg/comfy-multi-player' import type { WidgetCatalog } from '@comfyorg/comfy-multi-player' import { createTestingPinia } from '@pinia/testing' @@ -50,8 +51,10 @@ function graphSnapshot() { id: node.id, type: node.type, title: node.title, - inputs: node.inputs, - outputs: node.outputs + // Detached: updateNode splices these arrays in place, so holding the + // store's own arrays would compare a later snapshot to itself. + inputs: node.inputs.map((slot) => ({ ...slot })), + outputs: node.outputs.map((slot) => ({ ...slot })) })) ), links: byId( @@ -478,6 +481,69 @@ describe('EcsFollowerAdapter integration', () => { host.destroy() }) + it('leaves entities the doc never mentions, and still drains a staged delete', () => { + const host = mint( + { + nodes: [ + { id: 1, type: 'Source', pos: [0, 0], inputs: [], outputs: [] }, + { id: 2, type: 'Sink', pos: [200, 0], inputs: [], outputs: [] } + ], + links: [] + }, + catalog + ) + const follower = new FollowerDoc() + follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) + const deleteLayouts = vi.fn() + const adapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: deleteLayouts } + }) + ) + const context = { + source: 'agent-remote', + actor: 'agent:replay', + opId: 'baseline' + } as const + + adapter.bind('wf', follower) + expect(adapter.projectBaseline('wf', context)).toBe(true) + + // Node 2 leaves the doc after bind, so the observer stages its deletion. + nodesMap(follower.doc).delete('2') + expect(adapter.projectBaseline('wf', context)).toBe(true) + expect( + useNodeDataStore() + .getGraphNodesFor('root', 'root') + .map(({ id }) => id) + ).toEqual([toNodeId(1), toNodeId(2)]) + + expect( + adapter.applyFrame({ + workflowId: 'wf', + seq: 1, + update: new Uint8Array(), + actor: 'agent:test', + opIds: ['later'] + }) + ).toBe(true) + expect( + useNodeDataStore() + .getGraphNodesFor('root', 'root') + .map(({ id }) => id) + ).toEqual([toNodeId(1)]) + expect(deleteLayouts).toHaveBeenCalledWith( + scope, + [toNodeId(2)], + expect.objectContaining({ opId: 'later' }) + ) + + adapter.destroy() + follower.destroy() + host.destroy() + }) + it('refuses to project a doc this build cannot read', () => { const host = mint( { nodes: [{ id: 1, type: 'Source', pos: [0, 0] }], links: [] }, diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index 83a1f7a3d02..695f433ba3a 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -101,7 +101,11 @@ function connectableSlots(slots: readonly unknown[] | undefined): number { ).length } -/** Every way `prepare` rejects a `connect`, mirrored: it fails the whole batch. */ +/** + * The `prepare` connect rejections observable from the doc alone, mirrored + * because it fails the whole batch on the first one. Graph ownership is not + * among them: only the link store knows an id already belongs elsewhere. + */ function isConnectable( link: SemanticLinkPayload, projectedIds: ReadonlySet @@ -203,9 +207,16 @@ export class EcsFollowerAdapter { * An unprojectable link is skipped rather than batched, since `prepare` * rejects a whole batch on its first invalid entry. * - * Returns false when nothing is bound, when the doc fails the KA-11 read - * gate, or when the batch was rejected. Restores arrive out of band, so this - * is the only place that gate can run for them. + * Additive: ECS entities the doc does not mention are left alone, so this + * converges the graph on the doc rather than replacing it. A caller wanting a + * true replace must clear first, accepting that the two batches are separate. + * + * Returns false when nothing is bound, when the doc fails the KA-11 read gate + * with a {@link FollowerSchemaError}, or when the batch was rejected. + * Restores arrive out of band, so this is the only place that gate can run + * for them. + * + * No production caller yet: nothing persists or restores Yjs bytes today. */ projectBaseline(workflowId: string, context: RemoteMutationContext): boolean { const session = this.targets.get(workflowId) @@ -227,16 +238,28 @@ export class EcsFollowerAdapter { return link && isConnectable(link, projectedIds) ? [link] : [] }) + const skipped = + session.nodes.size - nodes.length + (session.links.size - links.length) + if (skipped > 0) { + console.warn( + `[agent-crdt] baseline for ${workflowId} skipped ${skipped} unprojectable entries` + ) + } + const projected = session.mutations.batch(context, (batch) => { for (const node of nodes) batch.reconcileNode(node) for (const link of links) batch.connect(link) }) if (!projected) return false - // The baseline is the whole doc, so whatever the observers staged is - // already in it; leaving it queued re-reconciles every node next frame. - this.discardSessionPending(session) - session.reconcileNextFrame = false + // Drop only what this batch committed, so a restore after bind does not + // re-reconcile next frame. A staged deletion is absent from the doc and so + // never listed here: it must survive, the baseline cannot rediscover it. + for (const id of projectedIds) { + session.nodeActions.delete(id) + session.changedWidgets.delete(id) + } + for (const link of links) session.changedLinks.delete(String(link.id)) return true } From fb643b1f0ea6aa92a6b0608bdc4bc395a80f3e18 Mon Sep 17 00:00:00 2001 From: glary-bot Date: Tue, 1 Sep 2026 07:54:59 +0000 Subject: [PATCH 11/15] fix(agent-crdt): clear skipped baseline entries from the pending set A link the baseline skipped stayed in changedLinks, so the next frame retried it. applyQueuedFrame has no endpoint filter, prepare rejects the whole batch on that entry, and discardSessionPending has already run -- so the frame's legitimate mutations were lost, and resubscribe cannot replay them because the follower already integrated those structs. Drain every id the doc still holds. Staged deletions are absent from the doc, so they are still never enumerated and still survive. Also name the skipped node and link ids rather than summing them, and record in the JSDoc that reconcileNode splices slot records over the store's, which leaves a store link in a doc-omitted slot registered with nothing referencing it -- applyQueuedFrame shares that hole on its own reconcile path, and closing either needs linkStore access. --- .../ecsFollowerAdapter.integration.test.ts | 77 +++++++++++++++++++ .../agent/crdt/ecsFollowerAdapter.ts | 36 ++++++--- 2 files changed, 101 insertions(+), 12 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index b24e73933ba..770736e6035 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -544,6 +544,83 @@ describe('EcsFollowerAdapter integration', () => { host.destroy() }) + it('does not let a skipped link poison the next frame', () => { + const host = mint( + { + nodes: [ + { + id: 1, + type: 'Source', + pos: [0, 0], + inputs: [], + outputs: [{ name: 'out', type: 'IMAGE', links: [] }] + } + ], + links: [] + }, + catalog + ) + const follower = new FollowerDoc() + const adapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + }) + ) + + adapter.bind('wf', follower) + follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) + linksMap(follower.doc).set('99', [99, '1', 0, '404', 0, 'IMAGE']) + expect( + adapter.projectBaseline('wf', { + source: 'agent-remote', + actor: 'agent:replay', + opId: 'baseline' + }) + ).toBe(true) + + // The live path has no endpoint filter, so a skipped link left staged + // would fail this frame's whole batch and take the new node with it. + nodesMap(follower.doc).set('2', nodesMap(host).get('1')!.clone()) + expect( + adapter.applyFrame({ + workflowId: 'wf', + seq: 1, + update: new Uint8Array(), + actor: 'agent:test', + opIds: ['later'] + }) + ).toBe(true) + expect( + useNodeDataStore() + .getGraphNodesFor('root', 'root') + .map(({ id }) => id) + ).toEqual([toNodeId(1), toNodeId(2)]) + + adapter.destroy() + follower.destroy() + host.destroy() + }) + + it('returns false when no session is bound to the target', () => { + const adapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + }) + ) + + expect( + adapter.projectBaseline('never-bound', { + source: 'agent-remote', + actor: 'agent:replay', + opId: 'baseline' + }) + ).toBe(false) + + adapter.destroy() + }) + it('refuses to project a doc this build cannot read', () => { const host = mint( { nodes: [{ id: 1, type: 'Source', pos: [0, 0] }], links: [] }, diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index 695f433ba3a..32144dd2eda 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -207,9 +207,15 @@ export class EcsFollowerAdapter { * An unprojectable link is skipped rather than batched, since `prepare` * rejects a whole batch on its first invalid entry. * - * Additive: ECS entities the doc does not mention are left alone, so this - * converges the graph on the doc rather than replacing it. A caller wanting a - * true replace must clear first, accepting that the two batches are separate. + * Additive for nodes: ECS nodes the doc does not mention are left alone, so + * this converges the graph on the doc rather than replacing it. A caller + * wanting a true replace must clear first, accepting two separate batches. + * + * Not additive for the slots of a node it reconciles: `reconcileNode` splices + * the doc's slot records over the store's, so a store link in a slot the doc + * omits is left registered with no slot referencing it. `applyQueuedFrame` + * has the same hole on its own reconcile path; both need `linkStore` access + * to close, so a caller must not restore onto a populated graph until it is. * * Returns false when nothing is bound, when the doc fails the KA-11 read gate * with a {@link FollowerSchemaError}, or when the batch was rejected. @@ -238,11 +244,17 @@ export class EcsFollowerAdapter { return link && isConnectable(link, projectedIds) ? [link] : [] }) - const skipped = - session.nodes.size - nodes.length + (session.links.size - links.length) - if (skipped > 0) { + const skippedNodes = [...session.nodes.keys()].filter( + (id) => !projectedIds.has(id) + ) + const connectedIds = new Set(links.map(({ id }) => String(id))) + const skippedLinks = [...session.links.keys()].filter( + (id) => !connectedIds.has(id) + ) + if (skippedNodes.length > 0 || skippedLinks.length > 0) { console.warn( - `[agent-crdt] baseline for ${workflowId} skipped ${skipped} unprojectable entries` + `[agent-crdt] baseline for ${workflowId} skipped unprojectable nodes ` + + `[${skippedNodes.join(', ')}] and links [${skippedLinks.join(', ')}]` ) } @@ -252,14 +264,14 @@ export class EcsFollowerAdapter { }) if (!projected) return false - // Drop only what this batch committed, so a restore after bind does not - // re-reconcile next frame. A staged deletion is absent from the doc and so - // never listed here: it must survive, the baseline cannot rediscover it. - for (const id of projectedIds) { + // Everything the doc holds, skipped included: a skipped entry left staged + // is retried next frame, whose whole batch `prepare` then rejects. A staged + // deletion is not in the doc, so it survives — nothing else can find it. + for (const id of session.nodes.keys()) { session.nodeActions.delete(id) session.changedWidgets.delete(id) } - for (const link of links) session.changedLinks.delete(String(link.id)) + for (const id of session.links.keys()) session.changedLinks.delete(id) return true } From 556fe2c70b4db0dd151b4fa877f1e3175a5ada50 Mon Sep 17 00:00:00 2001 From: glary-bot Date: Tue, 1 Sep 2026 08:15:11 +0000 Subject: [PATCH 12/15] fix(agent-crdt): re-arm reconcile after a baseline, tighten skip reporting Once a baseline has registered every doc node, a later frame staging an 'add' for one of them -- a remint reuses the id -- hits prepare's 'already registered' rejection and loses its whole batch. Re-arm the flag that exists for exactly that condition. Collect skipped link keys where they are skipped, rather than rebuilding the complement from tuple ids that need not equal the doc's map keys. Record that skipping a link can leave a projected node's slot mirror naming a topology the link store does not hold, and order the node-id assertions so they no longer depend on Y.Map iteration order. --- .../ecsFollowerAdapter.integration.test.ts | 93 +++++++++++++++---- .../agent/crdt/ecsFollowerAdapter.ts | 21 +++-- 2 files changed, 86 insertions(+), 28 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index 770736e6035..5a56c30d52b 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -37,9 +37,14 @@ const scope = { } function byId(entries: T[]): T[] { - return entries.sort((left, right) => - String(left.id).localeCompare(String(right.id)) - ) + return entries.sort((left, right) => Number(left.id) - Number(right.id)) +} + +function projectedNodeIds() { + return useNodeDataStore() + .getGraphNodesFor('root', 'root') + .map(({ id }) => Number(id)) + .sort((left, right) => left - right) } function graphSnapshot() { @@ -402,11 +407,7 @@ describe('EcsFollowerAdapter integration', () => { }) ).toBe(true) - expect( - useNodeDataStore() - .getGraphNodesFor('root', 'root') - .map(({ id }) => id) - ).toEqual([toNodeId(1), toNodeId(2), toNodeId(3)]) + expect(projectedNodeIds()).toEqual([1, 2, 3]) expect( useLinkStore().getTopology(scope.rootGraphId, toLinkId(9)) ).toBeDefined() @@ -513,11 +514,7 @@ describe('EcsFollowerAdapter integration', () => { // Node 2 leaves the doc after bind, so the observer stages its deletion. nodesMap(follower.doc).delete('2') expect(adapter.projectBaseline('wf', context)).toBe(true) - expect( - useNodeDataStore() - .getGraphNodesFor('root', 'root') - .map(({ id }) => id) - ).toEqual([toNodeId(1), toNodeId(2)]) + expect(projectedNodeIds()).toEqual([1, 2]) expect( adapter.applyFrame({ @@ -528,11 +525,7 @@ describe('EcsFollowerAdapter integration', () => { opIds: ['later'] }) ).toBe(true) - expect( - useNodeDataStore() - .getGraphNodesFor('root', 'root') - .map(({ id }) => id) - ).toEqual([toNodeId(1)]) + expect(projectedNodeIds()).toEqual([1]) expect(deleteLayouts).toHaveBeenCalledWith( scope, [toNodeId(2)], @@ -591,15 +584,75 @@ describe('EcsFollowerAdapter integration', () => { opIds: ['later'] }) ).toBe(true) + expect(projectedNodeIds()).toEqual([1, 2]) + + adapter.destroy() + follower.destroy() + host.destroy() + }) + + it('lets a later frame re-add a node the baseline already projected', () => { + const host = mint( + { + nodes: [ + { id: 1, type: 'Source', title: 'Saved', pos: [0, 0], inputs: [] } + ], + links: [] + }, + catalog + ) + const reminted = mint( + { + nodes: [ + { id: 1, type: 'Source', title: 'Reminted', pos: [9, 9], inputs: [] } + ], + links: [] + }, + catalog + ) + const follower = new FollowerDoc() + follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) + const adapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + }) + ) + + adapter.bind('wf', follower) + expect( + adapter.projectBaseline('wf', { + source: 'agent-remote', + actor: 'agent:replay', + opId: 'baseline' + }) + ).toBe(true) + + // A new incarnation of the same id stages as 'add', which prepare rejects + // outright once the baseline has already registered that node. + nodesMap(follower.doc).delete('1') + nodesMap(follower.doc).set('1', nodesMap(reminted).get('1')!.clone()) + expect( + adapter.applyFrame({ + workflowId: 'wf', + seq: 1, + update: new Uint8Array(), + actor: 'agent:test', + opIds: ['remint'] + }) + ).toBe(true) + + expect(projectedNodeIds()).toEqual([1]) expect( useNodeDataStore() .getGraphNodesFor('root', 'root') - .map(({ id }) => id) - ).toEqual([toNodeId(1), toNodeId(2)]) + .find(({ id }) => id === toNodeId(1))?.title + ).toBe('Reminted') adapter.destroy() follower.destroy() host.destroy() + reminted.destroy() }) it('returns false when no session is bound to the target', () => { diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index 32144dd2eda..a4a9de721d8 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -205,7 +205,9 @@ export class EcsFollowerAdapter { * ordinary unbind/rebind of an unchanged doc. * * An unprojectable link is skipped rather than batched, since `prepare` - * rejects a whole batch on its first invalid entry. + * rejects a whole batch on its first invalid entry. A self-inconsistent doc + * can therefore land a node whose slot mirror still names a skipped link, + * leaving that slot pointing at a topology the link store does not hold. * * Additive for nodes: ECS nodes the doc does not mention are left alone, so * this converges the graph on the doc rather than replacing it. A caller @@ -239,18 +241,17 @@ export class EcsFollowerAdapter { return payload ? [payload] : [] }) const projectedIds = new Set(nodes.map(({ id }) => String(id))) - const links = [...session.links.keys()].flatMap((id) => { + const links: SemanticLinkPayload[] = [] + const skippedLinks: string[] = [] + for (const id of session.links.keys()) { const link = readSemanticLink(session.follower.doc, id) - return link && isConnectable(link, projectedIds) ? [link] : [] - }) + if (link && isConnectable(link, projectedIds)) links.push(link) + else skippedLinks.push(id) + } const skippedNodes = [...session.nodes.keys()].filter( (id) => !projectedIds.has(id) ) - const connectedIds = new Set(links.map(({ id }) => String(id))) - const skippedLinks = [...session.links.keys()].filter( - (id) => !connectedIds.has(id) - ) if (skippedNodes.length > 0 || skippedLinks.length > 0) { console.warn( `[agent-crdt] baseline for ${workflowId} skipped unprojectable nodes ` + @@ -272,6 +273,10 @@ export class EcsFollowerAdapter { session.changedWidgets.delete(id) } for (const id of session.links.keys()) session.changedLinks.delete(id) + + // Re-arm, not redundant: the store now holds every doc node, so a later + // frame re-adding one is rejected as registered and loses its whole batch. + session.reconcileNextFrame = true return true } From f7fc75ad6ef9d77ba464850ce09d8c181191d557 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Tue, 1 Sep 2026 15:12:15 -0700 Subject: [PATCH 13/15] fix(agent-crdt): keep baseline reconciliation one-shot Replace the synthetic two-transaction remint test with a real wire delta and preserve incident links through the update path. Addresses https://github.com/Comfy-Org/ComfyUI_frontend/pull/16486#issuecomment-5501076931 --- .../ecsFollowerAdapter.integration.test.ts | 45 ++++++++++++++----- .../agent/crdt/ecsFollowerAdapter.ts | 6 +-- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index 5a56c30d52b..9054692bb9e 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -591,20 +591,41 @@ describe('EcsFollowerAdapter integration', () => { host.destroy() }) - it('lets a later frame re-add a node the baseline already projected', () => { + it('applies a remint wire delta after baseline projection', () => { const host = mint( { nodes: [ - { id: 1, type: 'Source', title: 'Saved', pos: [0, 0], inputs: [] } + { + id: 1, + type: 'Source', + title: 'Saved', + pos: [0, 0], + inputs: [], + outputs: [{ name: 'out', type: 'IMAGE', links: [9] }] + }, + { + id: 2, + type: 'Sink', + pos: [200, 0], + inputs: [{ name: 'in', type: 'IMAGE', link: 9 }], + outputs: [] + } ], - links: [] + links: [[9, 1, 0, 2, 0, 'IMAGE']] }, catalog ) const reminted = mint( { nodes: [ - { id: 1, type: 'Source', title: 'Reminted', pos: [9, 9], inputs: [] } + { + id: 1, + type: 'Source', + title: 'Reminted', + pos: [9, 9], + inputs: [], + outputs: [{ name: 'out', type: 'IMAGE', links: [9] }] + } ], links: [] }, @@ -628,26 +649,30 @@ describe('EcsFollowerAdapter integration', () => { }) ).toBe(true) - // A new incarnation of the same id stages as 'add', which prepare rejects - // outright once the baseline has already registered that node. - nodesMap(follower.doc).delete('1') - nodesMap(follower.doc).set('1', nodesMap(reminted).get('1')!.clone()) + const beforeRemint = follower.stateVector() + nodesMap(host).delete('1') + nodesMap(host).set('1', nodesMap(reminted).get('1')!.clone()) + const update = Y.encodeStateAsUpdate(host, beforeRemint) + follower.applyRemoteUpdate(update) expect( adapter.applyFrame({ workflowId: 'wf', seq: 1, - update: new Uint8Array(), + update, actor: 'agent:test', opIds: ['remint'] }) ).toBe(true) - expect(projectedNodeIds()).toEqual([1]) + expect(projectedNodeIds()).toEqual([1, 2]) expect( useNodeDataStore() .getGraphNodesFor('root', 'root') .find(({ id }) => id === toNodeId(1))?.title ).toBe('Reminted') + expect( + useLinkStore().getTopology(scope.rootGraphId, toLinkId(9)) + ).toMatchObject({ originNodeId: toNodeId(1), targetNodeId: toNodeId(2) }) adapter.destroy() follower.destroy() diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index a4a9de721d8..8f6a9bb66f4 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -274,9 +274,9 @@ export class EcsFollowerAdapter { } for (const id of session.links.keys()) session.changedLinks.delete(id) - // Re-arm, not redundant: the store now holds every doc node, so a later - // frame re-adding one is rejected as registered and loses its whole batch. - session.reconcileNextFrame = true + // This baseline performed the initial reconciliation. Later wire deltas + // must use the normal add/update paths so links are updated atomically. + session.reconcileNextFrame = false return true } From 9e5926dd20e84fb7cd7c568665e7b13c2d3ade13 Mon Sep 17 00:00:00 2001 From: bymyself Date: Tue, 1 Sep 2026 23:33:46 +0000 Subject: [PATCH 14/15] test(agent-crdt): assert wf-a baseline isolation before workflow switch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses CodeRabbit finding on FE #16486 review at f7fc75ad6e: projects a baseline for wf-a via projectBaseline before unbind, and asserts the recorded nodes/links carry the wf-a: prefix and survive unchanged into the wf-b frame — proving isolation across workflows, not just an empty starting state. Review: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16486#pullrequestreview-inline-comment --- .../crdt/ecsFollowerAdapter.integration.test.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index 9054692bb9e..22cf2d925be 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -1133,6 +1133,16 @@ describe('EcsFollowerAdapter integration', () => { // The bridge reuses one doc across a switch, so wf-b binds onto wf-a's. adapter.bind('wf-a', follower) + expect( + adapter.projectBaseline('wf-a', { + source: 'agent-remote', + actor: 'agent:test', + opId: 'baseline-a' + }) + ).toBe(true) + expect(projectedNodes.sort()).toEqual(['wf-a:1', 'wf-a:2']) + expect(projectedLinks).toEqual(['wf-a:9']) + adapter.unbind('wf-a') adapter.bind('wf-b', follower) expect( @@ -1145,8 +1155,8 @@ describe('EcsFollowerAdapter integration', () => { }) ).toBe(true) - expect(projectedNodes).toEqual([]) - expect(projectedLinks).toEqual([]) + expect(projectedNodes.sort()).toEqual(['wf-a:1', 'wf-a:2']) + expect(projectedLinks).toEqual(['wf-a:9']) adapter.destroy() follower.destroy() From adb82e0a023db5c37734d5ea2346f7761eda6a3f Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 03:38:27 +0000 Subject: [PATCH 15/15] fix: retain initial reconcile after empty baseline Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16486#discussion_r3908920587 --- .../ecsFollowerAdapter.integration.test.ts | 67 +++++++++++++++++++ .../agent/crdt/ecsFollowerAdapter.ts | 4 +- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index 22cf2d925be..4c15eacad40 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -699,6 +699,73 @@ describe('EcsFollowerAdapter integration', () => { adapter.destroy() }) + it('keeps the first frame armed after projecting an empty baseline', () => { + const host = mint({ nodes: [], links: [] }, catalog) + const follower = new FollowerDoc() + follower.applyRemoteUpdate(Y.encodeStateAsUpdate(host)) + const addNode = vi.fn() + const reconcileNode = vi.fn() + const adapter = new EcsFollowerAdapter(() => ({ + batch: (_context, define) => { + define({ + addNode, + reconcileNode, + setWidget: vi.fn(), + connect: vi.fn(), + removeLinks: vi.fn(), + deleteNode: vi.fn(), + clearSemanticGraph: vi.fn() + }) + return true + }, + addNode: () => true, + setWidget: () => true, + connect: () => true, + deleteNode: () => true, + clearSemanticGraph: () => true + })) + adapter.bind('wf', follower) + expect( + adapter.projectBaseline('wf', { + source: 'agent-remote', + actor: 'agent:replay', + opId: 'baseline' + }) + ).toBe(true) + + const stateVector = Y.encodeStateVector(host) + applyOps( + host, + [ + op('first-node', 1, { + op: 'add_node', + node_id: 1, + class_type: 'Source', + pos: [0, 0], + node: { id: 1, type: 'Source', pos: [0, 0] } + }) + ] as Parameters[1], + catalog + ) + const update = Y.encodeStateAsUpdate(host, stateVector) + follower.applyRemoteUpdate(update) + expect( + adapter.applyFrame({ + workflowId: 'wf', + seq: 1, + update, + actor: 'agent:test', + opIds: ['first-frame'] + }) + ).toBe(true) + expect(reconcileNode).toHaveBeenCalledOnce() + expect(addNode).not.toHaveBeenCalled() + + adapter.destroy() + follower.destroy() + host.destroy() + }) + it('refuses to project a doc this build cannot read', () => { const host = mint( { nodes: [{ id: 1, type: 'Source', pos: [0, 0] }], links: [] }, diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index 8f6a9bb66f4..1e68de99f39 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -276,7 +276,9 @@ export class EcsFollowerAdapter { // This baseline performed the initial reconciliation. Later wire deltas // must use the normal add/update paths so links are updated atomically. - session.reconcileNextFrame = false + if (nodes.length > 0 || links.length > 0) { + session.reconcileNextFrame = false + } return true }