From 19f4bf42b002e22a33bcfd6a1115633ff5845b70 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 13:24:47 +0000 Subject: [PATCH 01/19] test(agent-crdt): regression tests for agent-added nodes with no live LGraph adapter (qa-59) Adds two characterization/regression tests pinning the qa-59 defect ("Cannot serialize graph ... has no live adapter") at both layers of the chain: - src/lib/litegraph/src/LGraph.test.ts: drives graphMutations.addNode directly into a real LGraph's scope and asserts the node is present in the ECS node-data store but graph.serialize() silently drops it (no LGraphNode adapter was ever constructed/attached). - ecsFollowerAdapter.integration.test.ts: drives a real add_node op through applyOps -> FollowerDoc -> EcsFollowerAdapter.applyFrame and confirms the node lands in the ECS store with nothing in this (deliberately litegraph-free) chain ever touching an LGraph adapter. Both tests currently PASS, i.e. they document the bug as it exists today; when the real fix lands (constructing an LGraphNode adapter for remote-added nodes, or making the serializer treat store state as authoritative), the LGraph.test.ts assertion on serialized.nodes will need to flip and should be updated then. See reports/qa/qa-59-code-reading-verification.md in christian-byrne/in-app-agent-program for the full root-cause writeup. --- src/lib/litegraph/src/LGraph.test.ts | 60 ++++++++++++++++ .../ecsFollowerAdapter.integration.test.ts | 69 +++++++++++++++++++ 2 files changed, 129 insertions(+) diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index 115c4f59167..49b3d6ea55f 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -4,6 +4,7 @@ import { createTestingPinia } from '@pinia/testing' import { setActivePinia } from 'pinia' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { createGraphMutations } from '@/core/graph/graphMutations' import type { NodeLifecycleEvent } from '@/lib/litegraph/src/infrastructure/LGraphEventMap' import type { LGraphCanvas } from '@/lib/litegraph/src/LGraphCanvas' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' @@ -31,6 +32,7 @@ import { useEntityIdStore } from '@/stores/entityIdStore' import { useLinkStore } from '@/stores/linkStore' import { useExecutionOrderStore } from '@/stores/executionOrderStore' import { useGraphMetadataStore } from '@/stores/graphMetadataStore' +import { useNodeDataStore } from '@/stores/nodeDataStore' import { usePreviewExposureStore } from '@/stores/previewExposureStore' import { useRerouteStore } from '@/stores/rerouteStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' @@ -863,6 +865,64 @@ describe('Store-driven serialization parity', () => { ) }) + test('drops an agent-added node from serialize() when only the ECS store, not LGraph._nodes, has it (qa-59)', ({ + expect + }) => { + // Regression test for the qa-59 defect: the CRDT follower's addNode path + // (`graphMutations.commit()` -> nodeStore/widgetStore/layout, see + // `src/core/graph/graphMutations.ts`) never constructs an LGraphNode and + // never calls `LGraph.add()`, so the node exists in the ECS node-data + // store (and renders on canvas via the store-driven Vue node path) but + // has no adapter in `LGraph._nodes`. `serialiseStoredNodes()` hits the + // adapter/state mismatch branch and silently serializes only the + // (empty) live-adapter set, so the node is dropped from every save. + const graph = new LGraph() + const scope = graphScopeOf(graph) + const createLayout = vi.fn() + const mutations = createGraphMutations({ + getScope: () => scope, + layout: { createNode: createLayout, deleteNodes: vi.fn() } + }) + + mutations.addNode( + { + id: 1, + type: 'dummy', + pos: [0, 0], + size: [100, 80], + inputs: [], + outputs: [] + }, + { source: 'agent-remote', actor: 'agent:test', opId: 'op-1' } + ) + + // The node is real in the ECS store... + expect( + useNodeDataStore().getGraphNodesFor(graph.rootGraph.id, graph.id) + ).toHaveLength(1) + // ...but LGraph never learned about it. + expect(graph._nodes).toHaveLength(0) + + const serialized = graph.serialize() + + // This is the bug: the store-only node is silently absent from the + // persisted document instead of being included. When this assertion + // starts failing (serialized.nodes has length 1), the fix has landed. + expect(serialized.nodes).toEqual([]) + expect(mockReportError).toHaveBeenCalledExactlyOnceWith( + expect.objectContaining({ + message: 'Graph serialization state mismatch' + }), + { + errorType: 'graph_serialization_state_mismatch', + context: { + graphId: graph.id, + mismatch: expect.stringMatching(/stored node .* has no live adapter/) + } + } + ) + }) + test('rejects additive configuration before mutating a populated graph', ({ expect }) => { diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index c2133ec0aae..8056147626c 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -826,6 +826,75 @@ describe('EcsFollowerAdapter integration', () => { }) }) + it('lands an agent add_node in the ECS store with no corresponding LGraph adapter (qa-59)', () => { + // Regression test for qa-59 ("Cannot serialize graph ... has no live + // adapter"). This drives the real applier + follower + adapter chain + // (no LGraph involved, by design: `graphMutations.ts` is the pure, + // litegraph-free op layer per this repo's CRDT invariants) and confirms + // the observable symptom at this layer's boundary: after an agent + // `add_node`, the ECS node-data store has the node, but nothing in this + // chain ever touches an `LGraph`/`LGraphNode`. The consuming half of the + // defect — that `LGraph.serialize()` then silently drops this node + // because it never gets an adapter — is covered by the companion test + // `drops an agent-added node from serialize() ... (qa-59)` in + // `src/lib/litegraph/src/LGraph.test.ts`, which is able to construct a + // real `LGraph` without pulling litegraph into this pure-op-layer suite. + const host = mint({ nodes: [], links: [] }, catalog) + const follower = new FollowerDoc() + const mutations = createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + }) + const adapter = new EcsFollowerAdapter(mutations) + adapter.bind('wf', follower) + + const result = applyOps( + host, + [ + op('op-1', 1, { + op: 'add_node', + node_id: 1, + class_type: 'Source', + pos: [10, 20], + node: { + id: 1, + type: 'Source', + title: 'Agent-added node', + pos: [10, 20], + size: [180, 90], + widgets_values: { seed: 1 }, + inputs: [], + outputs: [{ name: 'out', type: 'IMAGE', links: [] }] + } + }) + ] as Parameters[1], + catalog + ) + expect(result.outcomes).toEqual([{ op_id: 'op-1', outcome: 'applied' }]) + const update = Y.encodeStateAsUpdate(host) + follower.applyRemoteUpdate(update) + expect( + adapter.applyFrame({ + workflowId: 'wf', + seq: 1, + update, + actor: 'agent:test', + opIds: ['op-1'] + }) + ).toBe(true) + + const [stored] = useNodeDataStore().getGraphNodesFor('root', 'root') + expect(stored).toMatchObject({ id: toNodeId(1), type: 'Source' }) + // `stored` is a plain reactive NodeState, not an LGraphNode: this layer + // has no LGraph instance at all to check `_nodes` against. That absence + // is exactly the gap qa-59 identifies — nothing downstream of this chain + // ever constructs the adapter LGraph.serialize() requires. + + 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() From 654d998eb77922c9b88afcf5aa1caea7c76ae9c1 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 2 Sep 2026 07:08:29 -0700 Subject: [PATCH 02/19] test(agent-crdt): assert the layout port call in the qa-59 companion test The integration test's title claimed the absence of an LGraph adapter but asserted nothing beyond what the existing shared-applier test already pins. Retitle it to the two side effects this layer owns and assert that the injected layout port is called exactly once for the remote add_node, so the test says what it proves. --- .../ecsFollowerAdapter.integration.test.ts | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index 8056147626c..aaafc9f330e 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -826,24 +826,22 @@ describe('EcsFollowerAdapter integration', () => { }) }) - it('lands an agent add_node in the ECS store with no corresponding LGraph adapter (qa-59)', () => { - // Regression test for qa-59 ("Cannot serialize graph ... has no live - // adapter"). This drives the real applier + follower + adapter chain - // (no LGraph involved, by design: `graphMutations.ts` is the pure, - // litegraph-free op layer per this repo's CRDT invariants) and confirms - // the observable symptom at this layer's boundary: after an agent - // `add_node`, the ECS node-data store has the node, but nothing in this - // chain ever touches an `LGraph`/`LGraphNode`. The consuming half of the - // defect — that `LGraph.serialize()` then silently drops this node - // because it never gets an adapter — is covered by the companion test - // `drops an agent-added node from serialize() ... (qa-59)` in - // `src/lib/litegraph/src/LGraph.test.ts`, which is able to construct a - // real `LGraph` without pulling litegraph into this pure-op-layer suite. + it('materializes an agent add_node into the ECS store and layout port only (qa-59 companion)', () => { + // Companion to `drops an agent-added node from serialize() ... (qa-59)` + // in `src/lib/litegraph/src/LGraph.test.ts`. This drives the real + // applier + follower + adapter chain and pins the two side effects this + // layer owns for a remote `add_node`: the node state is registered in + // the ECS node-data store and the injected layout port is asked to + // create exactly one layout entry. `graphMutations.ts` is the pure, + // litegraph-free op layer, so the layout port is the only outbound seam + // here; nothing on this chain can construct an `LGraphNode`, which is + // why the serialize-side symptom is asserted in the companion test. const host = mint({ nodes: [], links: [] }, catalog) const follower = new FollowerDoc() + const createNode = vi.fn() const mutations = createGraphMutations({ getScope: () => scope, - layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + layout: { createNode, deleteNodes: vi.fn() } }) const adapter = new EcsFollowerAdapter(mutations) adapter.bind('wf', follower) @@ -885,10 +883,12 @@ describe('EcsFollowerAdapter integration', () => { const [stored] = useNodeDataStore().getGraphNodesFor('root', 'root') expect(stored).toMatchObject({ id: toNodeId(1), type: 'Source' }) - // `stored` is a plain reactive NodeState, not an LGraphNode: this layer - // has no LGraph instance at all to check `_nodes` against. That absence - // is exactly the gap qa-59 identifies — nothing downstream of this chain - // ever constructs the adapter LGraph.serialize() requires. + expect(createNode).toHaveBeenCalledExactlyOnceWith( + scope, + toNodeId(1), + expect.objectContaining({ position: { x: 10, y: 20 } }), + expect.objectContaining({ source: 'agent-remote', opId: 'op-1' }) + ) adapter.destroy() follower.destroy() From a3f7e7dc6cd53fa341345861daafb64788d813e2 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 17:42:46 +0000 Subject: [PATCH 03/19] fix(agent-crdt): materialize a live LGraphNode adapter for agent-added nodes (qa-59) graphMutations.ts's op layer (litegraph-free by design, CRDT KEEP-ALIVE #3) only ever writes the ECS node-data/widget stores and the layout port for a remote add_node. It never constructs an LGraphNode or calls LGraph.add(), so an agent-added node renders (store-driven canvas) but has no adapter in LGraph._nodes: serialize() hits the adapter/state mismatch branch in serialiseStoredNodes() and silently drops the node from every save. Add the FE-only bridge this needs, in useAgentCrdtFollower.ts (already app-context aware, already owns EcsFollowerAdapter) rather than in the pure op layer: - EcsFollowerAdapter now tracks, per applied frame, which node ids the mutation store just added/reconciled-in (lastAddedNodeIds(workflowId)) -- plain string ids only, no LiteGraph/DOM dependency, so the op layer stays portable. - New agentNodeMaterializer.ts: for each id the adapter just touched that the live graph has no adapter for, constructs one via LiteGraph.createNode(state.type, state.title) + node.configure(the NodeState's own lastSerialization) + graph.add(node). Deletes the store-only record first so attachNodeToStores's registration doesn't treat the node's own existing store state as an id collision (which would otherwise mint a new id, diverging from the CRDT-assigned one). attachNodeLayout already no-ops into an adopt-only path when a layout entry exists for the id, so no duplicate layout write. - useAgentCrdtFollower.ts calls this after every adapter.applyFrame(), given a live graph. AgentPanelRoot.vue wires getGraph the same way mintPortWiring's getGraph already does (app.isGraphReady ? app.rootGraph : null). Tests: 3 new cases in useAgentCrdtFollower.test.ts covering materialize-on- update, skip-when-no-graph, and skip-when-nothing-added. The two existing qa-59 regression tests (LGraph.test.ts, ecsFollowerAdapter.integration.test.ts) are unchanged and still pass -- they pin the op layer's own boundary (it correctly still can't and shouldn't construct an LGraphNode itself), which is exactly why the fix lives one layer up in the FE bridge instead. Scope: root-graph case only, matching the existing regression tests and the row's ask. Subgraph-owned agent-added nodes are not covered by this pass. --- .../extensions/agent/AgentPanelRoot.vue | 5 +- .../agent/crdt/agentNodeMaterializer.ts | 111 ++++++++++++++++++ .../agent/crdt/ecsFollowerAdapter.ts | 36 +++++- .../agent/crdt/useAgentCrdtFollower.test.ts | 63 +++++++++- .../agent/crdt/useAgentCrdtFollower.ts | 29 ++++- 5 files changed, 237 insertions(+), 7 deletions(-) create mode 100644 src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 29ae76c0545..7631118f007 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -407,7 +407,10 @@ const { status: crdtStatus, enqueueHumanOperations } = useAgentCrdtFollower( boundWorkflowId, graphMutations, () => resolvedUserInfo.value?.id ?? null, - isBoundWorkflowActive + isBoundWorkflowActive, + // qa-59: same graph accessor as mintPortWiring's `getGraph` below — gives + // the follower a live LGraph to materialize agent-added node adapters into. + () => (app.isGraphReady ? app.rootGraph : null) ) const mintPortWiring = attachMintPortWiring({ isEnabled: () => agentPanelStore.enabled, diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts new file mode 100644 index 00000000000..e38021bfe30 --- /dev/null +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -0,0 +1,111 @@ +/** + * qa-59 bridge: gives agent-added nodes a live `LGraphNode` adapter. + * + * `graphMutations.ts` (the pure, litegraph-free op layer — see CRDT KEEP-ALIVE + * #3) only ever writes `useNodeDataStore`/`useWidgetValueStore`/the layout + * port for a remote `add_node`. It never constructs an `LGraphNode` or calls + * `LGraph.add()`, because it cannot import either without breaking the + * op-layer's DOM/litegraph-free purity guarantee. The result: an agent-added + * node renders (the canvas is store-driven) but has no entry in + * `LGraph._nodes`, so every `serialize()` hits the adapter/state mismatch + * branch in `serialiseStoredNodes()` and silently drops the node — see + * `LGraph.test.ts` "drops an agent-added node from serialize() ... (qa-59)". + * + * This module is the FE-only, app-context-aware seam that closes the gap. It + * intentionally lives next to `useAgentCrdtFollower.ts` (already Vue/app-layer + * code, already owns the `EcsFollowerAdapter` instance) rather than inside the + * op layer. + * + * Approach: rather than trying to make `LGraphNode` adopt the *existing* + * store `NodeState` in place (risky — `LGraphNode`'s constructor builds a + * fresh `_state` and its input/output slot views are bound to that object's + * `inputs`/`outputs` arrays by reference, so swapping `_state` post- + * construction risks desyncing those views), this deletes the store-only + * record for the id and lets the normal `node.configure()` + + * `attachNodeToStores` + `graph.add()` path recreate it from + * `NodeState.lastSerialization` — which IS the semantic payload that produced + * the current store state, so the recreated record is equivalent. That keeps + * every write on a single, already-tested code path instead of adding a + * second one, and reuses `attachNodeLayout`'s existing "layout already + * exists → adopt, don't recreate" branch (`graphLayoutAttachment.ts`) instead + * of duplicating that logic here. + */ +import { LiteGraph } from '@/lib/litegraph/src/litegraph' +import type { LGraph } from '@/lib/litegraph/src/LGraph' +import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' +import { useNodeDataStore } from '@/stores/nodeDataStore' +import { useWidgetValueStore } from '@/stores/widgetValueStore' +import { graphScopeOf } from '@/types/graphScopeId' +import { parseNodeId } from '@/types/nodeId' + +/** The graph surface this module needs — mirrors `MintableGraph` in + * `mintPortWiring.ts` so the two DI shapes stay compatible for callers that + * already hold a `getGraph()` closure. */ +export interface MaterializableGraph { + id: string + rootGraph: { id: string } + getNodeById(id: ReturnType): LGraphNode | null + add(node: LGraphNode): LGraphNode | null | undefined +} + +/** + * For every id in `nodeIds` that the ECS store knows about but `graph` does + * not yet have a live adapter for, constructs one and attaches it. Ids that + * already have a live adapter, or that the store no longer has (e.g. deleted + * by a later frame in the same batch), are skipped silently — this runs + * after every applied frame, so a node that legitimately isn't there yet + * will be picked up on its own frame. + * + * Returns the ids actually materialized, for callers that want to log/test. + */ +export function materializeMissingAdapters( + graph: MaterializableGraph, + nodeIds: Iterable +): string[] { + const scope = graphScopeOf(graph) + const nodeDataStore = useNodeDataStore() + const widgetValueStore = useWidgetValueStore() + const materialized: string[] = [] + + for (const rawId of nodeIds) { + const nodeId = parseNodeId(rawId) + if (nodeId === null) continue + if (graph.getNodeById(nodeId) !== null) continue // already has a live adapter + + const state = nodeDataStore.getNode(scope.rootGraphId, nodeId) + if (!state) continue // store no longer has it (deleted later in the batch) + if (state.graphId !== scope.owningGraphId) continue // lives in a different (sub)graph + + const serialised = state.lastSerialization as ISerialisedNode | undefined + if (!serialised) continue + + const node = LiteGraph.createNode(state.type, state.title) + if (!node) continue // unregistered node type — nothing to materialize + + // The store record this node is about to recreate is the ONLY thing + // standing in the way of `attachNodeToStores`'s registration succeeding + // with the CRDT-assigned id (`registerNode` sees an incumbent at this id + // and returns undefined, which would otherwise force a mint-a-new-id + // retry — wrong here, since this isn't a real collision, it's the same + // node already having store state). Clear it first so the id round-trips + // unchanged; `configure()` below rebuilds an equivalent record from the + // same payload that produced the one just removed. + nodeDataStore.deleteNode(scope, state) + // Keyed by rootGraphId, matching every other clearNode call site in + // graphMutations.ts's commit() (e.g. the reconcileNode-replace and + // deleteNode branches) — widget records are bucketed per root graph, + // not per owning (sub)graph. + widgetValueStore.clearNode(scope.rootGraphId, nodeId) + + node.configure(serialised) + node.id = nodeId + + const added = graph.add(node) + if (added) materialized.push(rawId) + } + + return materialized +} + +export type { LGraph } diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index 6f72c7f37fe..98ec2b3340b 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -118,6 +118,15 @@ interface TargetSession { onLinksChanged: (event: Y.YMapEvent) => void reconcileNextFrame: boolean applying: boolean + /** + * Node ids this session's mutation store learned about (added or + * reconciled-in) in the most recently committed `applyQueuedFrame` call. + * Plain string ids only — no LiteGraph/DOM dependency — so the pure op + * layer stays portable; a consumer (e.g. the FE-only follower composable) + * uses this to know which store-only nodes may still lack a live LGraph + * adapter, without this layer knowing LGraph exists. + */ + lastAddedNodeIds: Set } /** @@ -174,6 +183,7 @@ export class EcsFollowerAdapter { const session = this.targets.get(workflowId) if (!session) return false this.discardSessionPending(session) + session.lastAddedNodeIds = new Set() return session.mutations.clearSemanticGraph(context) } @@ -206,6 +216,7 @@ export class EcsFollowerAdapter { frameQueue: [], reconcileNextFrame: true, applying: false, + lastAddedNodeIds: new Set(), onNodesChanged: (_events): void => undefined, onLinksChanged: (_event): void => undefined } @@ -246,6 +257,7 @@ export class EcsFollowerAdapter { const removedLinkIds = [...changedLinkIds].flatMap((id) => session.links.has(id) ? [] : [Number(id)] ) + const addedNodeIds = new Set() const committed = session.mutations.batch(frameContext(update), (batch) => { if (reconcile) { const nodes = [...session.nodes.keys()].flatMap((id) => { @@ -260,7 +272,10 @@ export class EcsFollowerAdapter { nodes.map(({ id }) => toNodeId(id)), links.map(({ id }) => id) ) - for (const payload of nodes) batch.reconcileNode(payload) + for (const payload of nodes) { + batch.reconcileNode(payload) + addedNodeIds.add(String(payload.id)) + } for (const link of links) batch.connect(link) return } @@ -275,6 +290,7 @@ export class EcsFollowerAdapter { const payload = readSemanticNode(session.follower.doc, id) if (!payload) continue batch.addNode(payload) + addedNodeIds.add(id) } for (const id of replacedWidgetMaps) { if (nodeActions.has(id)) continue @@ -306,10 +322,26 @@ export class EcsFollowerAdapter { // reconcileNextFrame set so the next frame retries authoritative // cleanup instead of falling through to incremental handling with // stale local-only graph state still present. - if (committed) session.reconcileNextFrame = false + if (committed) { + session.reconcileNextFrame = false + session.lastAddedNodeIds = addedNodeIds + } else { + session.lastAddedNodeIds = new Set() + } return committed } + /** + * Node ids the given session's mutation store learned about (added or + * reconciled-in) on the most recently committed frame. Consumed by the + * FE-only follower composable (qa-59) to materialize a live LGraph + * adapter for agent-added nodes; empty when no session is bound or the + * last frame committed nothing. + */ + lastAddedNodeIds(workflowId: string): ReadonlySet { + return this.targets.get(workflowId)?.lastAddedNodeIds ?? new Set() + } + private discardSessionPending(session: TargetSession): void { session.nodeActions.clear() session.changedWidgets.clear() diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index 986339a6ff2..411778b195e 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -46,7 +46,12 @@ const adapterState = vi.hoisted(() => ({ applyFrame: vi.fn(() => true), clearForReset: vi.fn(), discardPending: vi.fn(), - destroy: vi.fn() + destroy: vi.fn(), + lastAddedNodeIds: vi.fn(() => new Set()) +})) + +const materializerState = vi.hoisted(() => ({ + materializeMissingAdapters: vi.fn(() => [] as string[]) })) const apiState = vi.hoisted(() => { @@ -91,9 +96,14 @@ vi.mock('./ecsFollowerAdapter', () => ({ clearForReset = adapterState.clearForReset discardPending = adapterState.discardPending destroy = adapterState.destroy + lastAddedNodeIds = adapterState.lastAddedNodeIds } })) +vi.mock('./agentNodeMaterializer', () => ({ + materializeMissingAdapters: materializerState.materializeMissingAdapters +})) + vi.mock('./devPanelLog', () => ({ recordDevEvent: vi.fn() })) @@ -137,7 +147,8 @@ function writeRawRecord(overrides: { function mountFollower( initial: string | null = null, - initiallyActive = true + initiallyActive = true, + getGraph: () => unknown = () => null ): { unmount: () => void workflowId: Ref @@ -153,7 +164,9 @@ function mountFollower( workflowId, graphMutations, () => null, - isTargetActive + isTargetActive, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + getGraph as any ) exposedStatus = () => status.value as AgentCrdtStatus return () => null @@ -178,6 +191,8 @@ describe('useAgentCrdtFollower', () => { setActivePinia(createPinia()) sessionStorage.clear() bridgeState.current = null + adapterState.lastAddedNodeIds.mockReset().mockReturnValue(new Set()) + materializerState.materializeMissingAdapters.mockReset().mockReturnValue([]) }) it('subscribes immediately to a bound workflow and reports it in status', () => { @@ -615,6 +630,48 @@ describe('useAgentCrdtFollower', () => { }) }) + it('qa-59: materializes a live adapter for nodes the adapter just added, given a graph', () => { + const fakeGraph = { id: 'root', rootGraph: { id: 'root' } } + adapterState.lastAddedNodeIds.mockReturnValue(new Set(['1'])) + materializerState.materializeMissingAdapters.mockReturnValue(['1']) + + const { unmount } = mountFollower('wf-1', true, () => fakeGraph) + + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + + expect(adapterState.lastAddedNodeIds).toHaveBeenCalledWith('wf-1') + expect(materializerState.materializeMissingAdapters).toHaveBeenCalledWith( + fakeGraph, + new Set(['1']) + ) + unmount() + }) + + it('qa-59: skips materialization when no graph is available yet', () => { + adapterState.lastAddedNodeIds.mockReturnValue(new Set(['1'])) + + // Default getGraph (no override) always returns null — mirrors the + // panel mounting before app.isGraphReady. + const { unmount } = mountFollower('wf-1') + + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + + expect(materializerState.materializeMissingAdapters).not.toHaveBeenCalled() + unmount() + }) + + it('qa-59: skips materialization when the frame added no new nodes', () => { + const fakeGraph = { id: 'root', rootGraph: { id: 'root' } } + adapterState.lastAddedNodeIds.mockReturnValue(new Set()) + + const { unmount } = mountFollower('wf-1', true, () => fakeGraph) + + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + + expect(materializerState.materializeMissingAdapters).not.toHaveBeenCalled() + unmount() + }) + it('suspends a background target and catches up only after it becomes active', async () => { const { unmount, isTargetActive } = mountFollower('wf-a', false) diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 51d41cd2c3e..183aa6d1c65 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -5,6 +5,8 @@ import { api } from '@/scripts/api' import type { RemoteMutationContext } from '@/types/graphMutationContext' import { createUuidv4 } from '@/utils/uuid' +import type { MaterializableGraph } from './agentNodeMaterializer' +import { materializeMissingAdapters } from './agentNodeMaterializer' import { recordDevEvent } from './devPanelLog' import type { DocFrameTransport, DocUpdate } from './docFrameClient' import { DocFrameClient } from './docFrameClient' @@ -188,7 +190,13 @@ export function useAgentCrdtFollower( workflowId: Ref, graphMutations: MutationsForTarget, userId: () => string | null = () => null, - isTargetActive: Ref = ref(true) + isTargetActive: Ref = ref(true), + // qa-59: the live LGraph the composable materializes agent-added node + // adapters into. Optional (defaults to "no graph available") so existing + // callers/tests that don't yet care about the live-adapter bridge keep + // compiling; `AgentPanelRoot.vue` wires the real `app.rootGraph` the same + // way `mintPortWiring`'s `getGraph` already does. + getGraph: () => MaterializableGraph | null = () => null ) { const connected = ref(false) const updatesApplied = ref(0) @@ -399,6 +407,25 @@ export function useAgentCrdtFollower( outcomes.value = applied ? { ...outcomes.value, applied: outcomes.value.applied + 1 } : { ...outcomes.value, skipped: outcomes.value.skipped + 1 } + // qa-59: the op layer above only ever wrote the ECS stores (KEEP-ALIVE + // #3 — it stays litegraph-free), so a remote add_node still has no + // `LGraphNode` adapter at this point. Materialize one for anything the + // adapter just landed, so the node enters `LGraph._nodes` and survives + // serialize()/save. Only the ids this exact frame touched are checked — + // an id already carrying a live adapter (e.g. a widget-only update on an + // existing node) is skipped by `materializeMissingAdapters` itself. + const graph = getGraph() + if (graph) { + const addedIds = adapter.lastAddedNodeIds(update.workflowId) + if (addedIds.size > 0) { + const materialized = materializeMissingAdapters(graph, addedIds) + if (materialized.length > 0) + recordDevEvent('agent_node_adapters_materialized', { + workflowId: update.workflowId, + nodeIds: materialized + }) + } + } recordDevEvent('doc_update', { workflowId: update.workflowId, seq: update.seq, From 59ef1ebc8e264d6ea81b538091a7818ab14d4c77 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 17:44:39 +0000 Subject: [PATCH 04/19] fixup: drop unused LGraph re-export, type mountFollower's getGraph param --- .../agent/crdt/agentNodeMaterializer.ts | 3 --- .../agent/crdt/useAgentCrdtFollower.test.ts | 21 ++++++++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index e38021bfe30..de2bbaef2fc 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -31,7 +31,6 @@ * of duplicating that logic here. */ import { LiteGraph } from '@/lib/litegraph/src/litegraph' -import type { LGraph } from '@/lib/litegraph/src/LGraph' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' import { useNodeDataStore } from '@/stores/nodeDataStore' @@ -107,5 +106,3 @@ export function materializeMissingAdapters( return materialized } - -export type { LGraph } diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index 411778b195e..2c3f4e12677 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -15,6 +15,8 @@ import { render } from '@testing-library/vue' import type { GraphMutations } from '@/core/graph/graphMutations' +import type { MaterializableGraph } from './agentNodeMaterializer' + const bridgeState = vi.hoisted(() => { class FakeBridge extends EventTarget { subscribe = vi.fn() @@ -148,7 +150,7 @@ function writeRawRecord(overrides: { function mountFollower( initial: string | null = null, initiallyActive = true, - getGraph: () => unknown = () => null + getGraph: () => MaterializableGraph | null = () => null ): { unmount: () => void workflowId: Ref @@ -165,8 +167,7 @@ function mountFollower( graphMutations, () => null, isTargetActive, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - getGraph as any + getGraph ) exposedStatus = () => status.value as AgentCrdtStatus return () => null @@ -631,7 +632,12 @@ describe('useAgentCrdtFollower', () => { }) it('qa-59: materializes a live adapter for nodes the adapter just added, given a graph', () => { - const fakeGraph = { id: 'root', rootGraph: { id: 'root' } } + const fakeGraph = { + id: 'root', + rootGraph: { id: 'root' }, + getNodeById: () => null, + add: () => null + } satisfies MaterializableGraph adapterState.lastAddedNodeIds.mockReturnValue(new Set(['1'])) materializerState.materializeMissingAdapters.mockReturnValue(['1']) @@ -661,7 +667,12 @@ describe('useAgentCrdtFollower', () => { }) it('qa-59: skips materialization when the frame added no new nodes', () => { - const fakeGraph = { id: 'root', rootGraph: { id: 'root' } } + const fakeGraph = { + id: 'root', + rootGraph: { id: 'root' }, + getNodeById: () => null, + add: () => null + } satisfies MaterializableGraph adapterState.lastAddedNodeIds.mockReturnValue(new Set()) const { unmount } = mountFollower('wf-1', true, () => fakeGraph) From f229b4690427682ecebede8982df021390f18b27 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 17:46:30 +0000 Subject: [PATCH 05/19] fix(agent-crdt): add missing DevEventKind for qa-59's materialize event vue-tsc caught the gap: recordDevEvent('agent_node_adapters_materialized', ...) in useAgentCrdtFollower.ts didn't match the closed DevEventKind union. --- src/workbench/extensions/agent/crdt/devPanelLog.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/workbench/extensions/agent/crdt/devPanelLog.ts b/src/workbench/extensions/agent/crdt/devPanelLog.ts index 2f013c09b07..22d90f536e9 100644 --- a/src/workbench/extensions/agent/crdt/devPanelLog.ts +++ b/src/workbench/extensions/agent/crdt/devPanelLog.ts @@ -22,6 +22,7 @@ export type DevEventKind = | 'stale_probe' | 'doc_gap' | 'doc_stale' + | 'agent_node_adapters_materialized' export interface DevEvent { seq: number From fa48986fb0d42eb4fcb683ae47f41f48820aedaf Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 20:33:06 +0000 Subject: [PATCH 06/19] fix(agent-crdt): retain pending materialize ids and make materializer rollback-safe (qa-59) Addresses DrJKL's CHANGES_REQUESTED on #16652: 1. useAgentCrdtFollower.ts: pending unmaterialized node ids are now accumulated across frames instead of being replaced by lastAddedNodeIds on every onUpdate call. A node added while getGraph() returns null (panel bound before app.isGraphReady) is retried once a graph becomes available, instead of being dropped from every future check. 2. agentNodeMaterializer.ts: node.configure() now runs before the authoritative nodeDataStore/widgetValueStore records are deleted, and graph.add() is wrapped in try/catch that restores the deleted record on failure. Previously the store record was deleted first, so a throw from configure() (extension code) or graph.add() (MAX_NUMBER_OF_NODES) permanently lost the node from both the live graph and the store/save path. Also fixes the critical bug the github-actions review panel flagged that made both DrJKL fixes unverifiable in practice: LGraph.getNodeById returns undefined (not null) for an absent id, so the existing '!== null' guard never skipped an id, and materializeMissingAdapters always returned []. Switched to a falsiness check. New tests: agentNodeMaterializer.test.ts covers the getNodeById falsiness fix and both rollback paths (configure() throws, graph.add() throws) against a real LGraph. useAgentCrdtFollower.test.ts adds a pending-id-retry-without-another-add case. --- .../agent/crdt/agentNodeMaterializer.test.ts | 113 ++++++++++++++++++ .../agent/crdt/agentNodeMaterializer.ts | 52 ++++++-- .../agent/crdt/useAgentCrdtFollower.test.ts | 30 +++++ .../agent/crdt/useAgentCrdtFollower.ts | 38 ++++-- 4 files changed, 212 insertions(+), 21 deletions(-) create mode 100644 src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts new file mode 100644 index 00000000000..d14d7b88a71 --- /dev/null +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -0,0 +1,113 @@ +import { graphScopeOf } from '@/types/graphScopeId' +import { createTestingPinia } from '@pinia/testing' +import { setActivePinia } from 'pinia' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { createGraphMutations } from '@/core/graph/graphMutations' +import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' +import { useNodeDataStore } from '@/stores/nodeDataStore' +import { toNodeId } from '@/types/nodeId' + +import { materializeMissingAdapters } from './agentNodeMaterializer' + +class DummyNode extends LGraphNode { + constructor() { + super('dummy') + } +} + +beforeEach(() => { + setActivePinia(createTestingPinia({ stubActions: false })) + LiteGraph.registerNodeType('dummy', DummyNode) +}) + +function seedAgentAddedNode(graph: LGraph, id: number) { + const scope = graphScopeOf(graph) + const mutations = createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + }) + mutations.addNode( + { + id, + type: 'dummy', + pos: [0, 0], + size: [100, 80], + inputs: [], + outputs: [] + }, + { source: 'agent-remote', actor: 'agent:test', opId: `op-${id}` } + ) + return scope +} + +describe('materializeMissingAdapters', () => { + it('gives a store-only agent-added node a live LGraph adapter', () => { + const graph = new LGraph() + seedAgentAddedNode(graph, 1) + + expect(graph._nodes).toHaveLength(0) + + const materialized = materializeMissingAdapters(graph, ['1']) + + expect(materialized).toEqual(['1']) + expect(graph._nodes).toHaveLength(1) + expect(graph.getNodeById(toNodeId(1))).not.toBeNull() + }) + + it('is a no-op for an id that already has a live adapter (getNodeById falsiness fix)', () => { + // Regression: LGraph.getNodeById returns `undefined`, not `null`, for an + // absent id (`this._nodes_by_id[id]`), so a strict `!== null` check + // never skips an already-materialized node and always re-runs the + // delete/recreate path. This must not touch the store a second time. + const graph = new LGraph() + graph.add(new DummyNode()) + const liveId = graph._nodes[0].id + const nodeDataStore = useNodeDataStore() + const deleteSpy = vi.spyOn(nodeDataStore, 'deleteNode') + + const materialized = materializeMissingAdapters(graph, [String(liveId)]) + + expect(materialized).toEqual([]) + expect(deleteSpy).not.toHaveBeenCalled() + }) + + it('does not delete the store record when graph.add() throws (rollback-safe)', () => { + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + const nodeDataStore = useNodeDataStore() + const before = nodeDataStore.getNode(scope.rootGraphId, toNodeId(1)) + expect(before).toBeDefined() + + vi.spyOn(graph, 'add').mockImplementation(() => { + throw 'LiteGraph: max number of nodes in a graph reached' + }) + + const materialized = materializeMissingAdapters(graph, ['1']) + + expect(materialized).toEqual([]) + // The authoritative record must survive the failed add — this is the + // DrJKL data-loss report: deleting before the fallible steps loses the + // node from every future save when `graph.add()` throws. + expect(nodeDataStore.getNode(scope.rootGraphId, toNodeId(1))).toBeDefined() + expect(graph._nodes).toHaveLength(0) + }) + + it('does not delete the store record when node.configure() throws', () => { + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + const nodeDataStore = useNodeDataStore() + + const configureSpy = vi + .spyOn(LGraphNode.prototype, 'configure') + .mockImplementation(() => { + throw new Error('bad payload') + }) + + const materialized = materializeMissingAdapters(graph, ['1']) + + expect(materialized).toEqual([]) + expect(nodeDataStore.getNode(scope.rootGraphId, toNodeId(1))).toBeDefined() + configureSpy.mockRestore() + }) +}) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index de2bbaef2fc..1fc794808c4 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -29,6 +29,15 @@ * second one, and reuses `attachNodeLayout`'s existing "layout already * exists → adopt, don't recreate" branch (`graphLayoutAttachment.ts`) instead * of duplicating that logic here. + * + * Ordering is rollback-safe: `node.configure()` (which runs node-class / + * extension code and can throw) happens BEFORE the store record is deleted; + * the store record is only cleared once `configure()` has already succeeded, + * immediately ahead of `graph.add()` (whose own failure, e.g. + * `MAX_NUMBER_OF_NODES`, is caught and the record restored). A throw from + * either fallible step leaves the authoritative store state exactly as it + * was, so the node is never dropped from both the live graph and the store + * at once — it just stays store-only and is retried on the next frame. */ import { LiteGraph } from '@/lib/litegraph/src/litegraph' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' @@ -70,7 +79,11 @@ export function materializeMissingAdapters( for (const rawId of nodeIds) { const nodeId = parseNodeId(rawId) if (nodeId === null) continue - if (graph.getNodeById(nodeId) !== null) continue // already has a live adapter + // `LGraph.getNodeById` indexes `_nodes_by_id` directly and returns + // `undefined` for an absent id despite its `LGraphNode | null` type, so + // this must be a falsiness check, not `!== null` (which is always true + // for `undefined` and made this whole function a no-op in production). + if (graph.getNodeById(nodeId)) continue // already has a live adapter const state = nodeDataStore.getNode(scope.rootGraphId, nodeId) if (!state) continue // store no longer has it (deleted later in the batch) @@ -82,14 +95,26 @@ export function materializeMissingAdapters( const node = LiteGraph.createNode(state.type, state.title) if (!node) continue // unregistered node type — nothing to materialize + // `configure()` runs extension/node-class code (`onConfigure`, and + // `graph.add()` below can trigger `onConnectionsChange`) and can throw — + // do this BEFORE touching the store so a throw here leaves the + // authoritative record untouched. + try { + node.configure(serialised) + } catch { + continue // leaves the store-only record in place; retried next frame + } + node.id = nodeId + // The store record this node is about to recreate is the ONLY thing // standing in the way of `attachNodeToStores`'s registration succeeding // with the CRDT-assigned id (`registerNode` sees an incumbent at this id // and returns undefined, which would otherwise force a mint-a-new-id // retry — wrong here, since this isn't a real collision, it's the same - // node already having store state). Clear it first so the id round-trips - // unchanged; `configure()` below rebuilds an equivalent record from the - // same payload that produced the one just removed. + // node already having store state). Clear it only once `configure()` + // above has already succeeded, and restore it if `graph.add()` (the + // other fallible step, e.g. `MAX_NUMBER_OF_NODES`) throws or declines, + // so a failure here never drops the node from the store and the save. nodeDataStore.deleteNode(scope, state) // Keyed by rootGraphId, matching every other clearNode call site in // graphMutations.ts's commit() (e.g. the reconcileNode-replace and @@ -97,11 +122,20 @@ export function materializeMissingAdapters( // not per owning (sub)graph. widgetValueStore.clearNode(scope.rootGraphId, nodeId) - node.configure(serialised) - node.id = nodeId - - const added = graph.add(node) - if (added) materialized.push(rawId) + let added: LGraphNode | null | undefined + try { + added = graph.add(node) + } catch { + added = undefined + } + if (added) { + materialized.push(rawId) + } else { + // Restore the authoritative record so the node isn't lost from the + // store/save; `registerNode` accepts the same state object back + // (`toRaw(incumbent) === toRaw(state)` in nodeDataStore.registerNode). + nodeDataStore.registerNode(scope, state) + } } return materialized diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index 2c3f4e12677..5448b07e48f 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -666,6 +666,36 @@ describe('useAgentCrdtFollower', () => { unmount() }) + it('qa-59/DrJKL: retries a pending id once a graph becomes available, without another add', () => { + const fakeGraph = { + id: 'root', + rootGraph: { id: 'root' }, + getNodeById: () => null, + add: () => null + } satisfies MaterializableGraph + let graph: MaterializableGraph | null = null + adapterState.lastAddedNodeIds.mockReturnValue(new Set(['1'])) + + const { unmount } = mountFollower('wf-1', true, () => graph) + + // First frame: added while no graph is bound yet — must not be dropped. + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + expect(materializerState.materializeMissingAdapters).not.toHaveBeenCalled() + + // Graph becomes available; a later frame carries no new adds, but the + // pending id from the earlier frame must still be retried. + graph = fakeGraph + adapterState.lastAddedNodeIds.mockReturnValue(new Set()) + materializerState.materializeMissingAdapters.mockReturnValue(['1']) + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 10 }) + + expect(materializerState.materializeMissingAdapters).toHaveBeenCalledWith( + fakeGraph, + new Set(['1']) + ) + unmount() + }) + it('qa-59: skips materialization when the frame added no new nodes', () => { const fakeGraph = { id: 'root', diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 183aa6d1c65..fe85b77a4c5 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -211,6 +211,15 @@ export function useAgentCrdtFollower( reset: 0, dropped: 0 }) + // qa-59 / DrJKL: ids this session has seen added but not yet materialized — + // either because `getGraph()` returned null on their frame (panel bound + // before `app.isGraphReady`), or `materializeMissingAdapters` skipped them + // (unregistered type, etc). Accumulates across frames instead of being + // replaced, so a node added before the graph existed is retried once one + // becomes available rather than being dropped from every future check — + // `materializeMissingAdapters` itself is idempotent for ids that already + // have a live adapter or that the store no longer has. + const pendingMaterializeIds = new Set() // Dev-panel tap (poc-4): log every outbound frame with its delivery result. // Wraps locally instead of modifying the exported apiTransport, whose @@ -411,19 +420,24 @@ export function useAgentCrdtFollower( // #3 — it stays litegraph-free), so a remote add_node still has no // `LGraphNode` adapter at this point. Materialize one for anything the // adapter just landed, so the node enters `LGraph._nodes` and survives - // serialize()/save. Only the ids this exact frame touched are checked — - // an id already carrying a live adapter (e.g. a widget-only update on an - // existing node) is skipped by `materializeMissingAdapters` itself. + // serialize()/save. Ids from this frame join any still-pending ids from + // earlier frames (see `pendingMaterializeIds` above) so an add that + // arrived before a graph existed is retried here, not dropped. + const addedIds = adapter.lastAddedNodeIds(update.workflowId) + for (const id of addedIds) pendingMaterializeIds.add(id) const graph = getGraph() - if (graph) { - const addedIds = adapter.lastAddedNodeIds(update.workflowId) - if (addedIds.size > 0) { - const materialized = materializeMissingAdapters(graph, addedIds) - if (materialized.length > 0) - recordDevEvent('agent_node_adapters_materialized', { - workflowId: update.workflowId, - nodeIds: materialized - }) + if (graph && pendingMaterializeIds.size > 0) { + // Snapshot before calling out — `materializeMissingAdapters` is a pure + // read of whatever set it's given, and mutating `pendingMaterializeIds` + // in place afterward must not retroactively change what was passed. + const attempted = new Set(pendingMaterializeIds) + const materialized = materializeMissingAdapters(graph, attempted) + if (materialized.length > 0) { + for (const id of materialized) pendingMaterializeIds.delete(id) + recordDevEvent('agent_node_adapters_materialized', { + workflowId: update.workflowId, + nodeIds: materialized + }) } } recordDevEvent('doc_update', { From 101cc2153aa442f1be66a27ac45396949e865cf9 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 15:27:08 -0700 Subject: [PATCH 07/19] chore: trigger PR sync after rebase (no-op) From 4e7091790a4a07df4765b56007cdef001ed8a1f0 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 3 Sep 2026 03:52:11 +0000 Subject: [PATCH 08/19] [automated] Apply ESLint and Oxfmt fixes --- src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index 1fc794808c4..5f1cf384554 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -41,7 +41,6 @@ */ import { LiteGraph } from '@/lib/litegraph/src/litegraph' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' import { useNodeDataStore } from '@/stores/nodeDataStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' import { graphScopeOf } from '@/types/graphScopeId' @@ -89,7 +88,7 @@ export function materializeMissingAdapters( if (!state) continue // store no longer has it (deleted later in the batch) if (state.graphId !== scope.owningGraphId) continue // lives in a different (sub)graph - const serialised = state.lastSerialization as ISerialisedNode | undefined + const serialised = state.lastSerialization if (!serialised) continue const node = LiteGraph.createNode(state.type, state.title) From aa3f2f259f3153911a90ef6896951f755d6987dd Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 3 Sep 2026 05:28:27 +0000 Subject: [PATCH 09/19] fix(agent-crdt): address round-2 review threads on qa-59 materializer (#16652) - agentNodeMaterializer.ts: reorder to graph.add() before node.configure() (canonical LGraph.configure() ordering) so link/slot resolution against node.graph works during configure; rollback removes the adapter AND restores the store record on a configure() failure. - Preserve named widget values: derive widgets_values_named from the name-keyed widgets_values payload before configure(), since configure() only restores named values through that field. - Unregistered node type: materialize the same placeholder LGraphNode normal graph loading uses instead of silently dropping the id. - Reconcile (not skip) when an id with a live adapter reappears in the reported set: the op layer reports remote update the same way as add, so skip-if-exists left stale adapters bound after an update. - Report materialization failures via reportError() with a stable errorType for both the add() and configure() failure paths. - ecsFollowerAdapter.ts: accumulate lastAddedNodeIds across every frame applyFrame() drains in one call, not just the triggering frame, so a multi-frame drain doesn't lose earlier frames' additions. - useAgentCrdtFollower.ts: clear pendingMaterializeIds at every follower-target change (co-located with adapter.unbind), so ids from a prior doc are never materialized against a new target's graph/store. - Tests: LGraph.test.ts's qa-59 regression test converted to test.fails pinning the desired assertions; agentNodeMaterializer.test.ts asserts serialize() output and drops the redundant configureSpy.mockRestore(). --- src/lib/litegraph/src/LGraph.test.ts | 35 ++-- .../agent/crdt/agentNodeMaterializer.test.ts | 15 +- .../agent/crdt/agentNodeMaterializer.ts | 155 ++++++++++++++---- .../agent/crdt/ecsFollowerAdapter.ts | 11 ++ .../agent/crdt/useAgentCrdtFollower.ts | 18 +- 5 files changed, 172 insertions(+), 62 deletions(-) diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index 49b3d6ea55f..ac8e1f32748 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -865,7 +865,18 @@ describe('Store-driven serialization parity', () => { ) }) - test('drops an agent-added node from serialize() when only the ECS store, not LGraph._nodes, has it (qa-59)', ({ + // Desired-outcome pin, not the buggy one (DrJKL / coderabbitai, + // https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3914958522, + // #discussion_r3914616366): `agentNodeMaterializer.ts` now closes this gap + // for anything routed through `useAgentCrdtFollower`, but a bare + // `LGraph` + `graphMutations.addNode()` (as below) never calls the + // materializer, so the underlying `LGraph._nodes` gap this test documents + // is still real for any caller that skips the follower composable. + // `test.fails` keeps the assertions expressing the CORRECT behavior; the + // test flips green (and must be converted to a plain `test`) the day + // `LGraph.serialize()`/`addNode()` itself closes the gap without relying on + // the FE-only follower seam. + test.fails('does NOT drop an agent-added node from serialize() when only the ECS store, not LGraph._nodes, has it (qa-59)', ({ expect }) => { // Regression test for the qa-59 defect: the CRDT follower's addNode path @@ -900,27 +911,13 @@ describe('Store-driven serialization parity', () => { expect( useNodeDataStore().getGraphNodesFor(graph.rootGraph.id, graph.id) ).toHaveLength(1) - // ...but LGraph never learned about it. - expect(graph._nodes).toHaveLength(0) const serialized = graph.serialize() - // This is the bug: the store-only node is silently absent from the - // persisted document instead of being included. When this assertion - // starts failing (serialized.nodes has length 1), the fix has landed. - expect(serialized.nodes).toEqual([]) - expect(mockReportError).toHaveBeenCalledExactlyOnceWith( - expect.objectContaining({ - message: 'Graph serialization state mismatch' - }), - { - errorType: 'graph_serialization_state_mismatch', - context: { - graphId: graph.id, - mismatch: expect.stringMatching(/stored node .* has no live adapter/) - } - } - ) + // Desired behavior: the store-only node survives serialize() and no + // mismatch is reported. + expect(serialized.nodes).toHaveLength(1) + expect(mockReportError).not.toHaveBeenCalled() }) test('rejects additive configuration before mutating a populated graph', ({ diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index d14d7b88a71..08dc4d87e41 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -53,6 +53,9 @@ describe('materializeMissingAdapters', () => { expect(materialized).toEqual(['1']) expect(graph._nodes).toHaveLength(1) expect(graph.getNodeById(toNodeId(1))).not.toBeNull() + // `graph._nodes` alone doesn't exercise `LGraph.serialize()` — assert the + // actual save path a regression in `serialiseStoredNodes()` would break. + expect(graph.serialize().nodes).toHaveLength(1) }) it('is a no-op for an id that already has a live adapter (getNodeById falsiness fix)', () => { @@ -98,16 +101,16 @@ describe('materializeMissingAdapters', () => { const scope = seedAgentAddedNode(graph, 1) const nodeDataStore = useNodeDataStore() - const configureSpy = vi - .spyOn(LGraphNode.prototype, 'configure') - .mockImplementation(() => { - throw new Error('bad payload') - }) + // Not restored explicitly: root `vitest.config` sets `restoreMocks: true`, + // so vitest already restores every mock between tests + // (https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3920899113). + vi.spyOn(LGraphNode.prototype, 'configure').mockImplementation(() => { + throw new Error('bad payload') + }) const materialized = materializeMissingAdapters(graph, ['1']) expect(materialized).toEqual([]) expect(nodeDataStore.getNode(scope.rootGraphId, toNodeId(1))).toBeDefined() - configureSpy.mockRestore() }) }) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index 5f1cf384554..11738941e87 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -30,22 +30,54 @@ * exists → adopt, don't recreate" branch (`graphLayoutAttachment.ts`) instead * of duplicating that logic here. * - * Ordering is rollback-safe: `node.configure()` (which runs node-class / - * extension code and can throw) happens BEFORE the store record is deleted; - * the store record is only cleared once `configure()` has already succeeded, - * immediately ahead of `graph.add()` (whose own failure, e.g. - * `MAX_NUMBER_OF_NODES`, is caught and the record restored). A throw from - * either fallible step leaves the authoritative store state exactly as it - * was, so the node is never dropped from both the live graph and the store - * at once — it just stays store-only and is retried on the next frame. + * Ordering matches canonical graph loading (`LGraph.configure()`: "add + * before configure, otherwise configure cannot create links") and stays + * rollback-safe: `graph.add()` runs first — on an EMPTY node, so a throw + * there touches neither the store nor a live adapter — then the store + * record is cleared, then `node.configure()` (which runs node-class / + * extension code, resolves slot/link references against `node.graph`, and + * can throw) runs against the now-attached node. A `configure()` throw + * rolls back both: the node is removed from the graph AND the store record + * is restored, so the node is never dropped from both the live graph and + * the store at once — it just stays store-only and is retried on the next + * frame. */ import { LiteGraph } from '@/lib/litegraph/src/litegraph' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' +import { reportError } from '@/platform/telemetry/reportError' import { useNodeDataStore } from '@/stores/nodeDataStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' import { graphScopeOf } from '@/types/graphScopeId' import { parseNodeId } from '@/types/nodeId' +/** + * `readSemanticNode` (`ecsFollowerAdapter.ts`) builds `widgets_values` as a + * NAME-keyed object from the doc's `widgets` Y.Map, so that's the shape + * `NodeState.lastSerialization` carries. `LGraphNode.configure()` only + * consumes a name-keyed map via `widgets_values_named` — its + * `widgets_values` handling is positional-array-only + * (`Array.from(info.widgets_values ?? [])`), with the named fallback gated + * on `fallbackWidgetsValuesNames`, which most agent-added node types won't + * have. Left alone, an object under `widgets_values` silently becomes `[]` + * and every named widget value is dropped on materialization + * (github-actions HIGH, + * https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3917983350). + * Derive the named map explicitly so configure() restores it regardless of + * whether the target node class declares a fallback. + */ +function withNamedWidgetValues(serialised: ISerialisedNode): ISerialisedNode { + const values = serialised.widgets_values + if ( + values === undefined || + Array.isArray(values) || + serialised.widgets_values_named !== undefined + ) { + return serialised + } + return { ...serialised, widgets_values_named: values } +} + /** The graph surface this module needs — mirrors `MintableGraph` in * `mintPortWiring.ts` so the two DI shapes stay compatible for callers that * already hold a `getGraph()` closure. */ @@ -54,6 +86,7 @@ export interface MaterializableGraph { rootGraph: { id: string } getNodeById(id: ReturnType): LGraphNode | null add(node: LGraphNode): LGraphNode | null | undefined + remove?(node: LGraphNode): void } /** @@ -82,7 +115,8 @@ export function materializeMissingAdapters( // `undefined` for an absent id despite its `LGraphNode | null` type, so // this must be a falsiness check, not `!== null` (which is always true // for `undefined` and made this whole function a no-op in production). - if (graph.getNodeById(nodeId)) continue // already has a live adapter + // + const existing = graph.getNodeById(nodeId) const state = nodeDataStore.getNode(scope.rootGraphId, nodeId) if (!state) continue // store no longer has it (deleted later in the batch) @@ -91,29 +125,78 @@ export function materializeMissingAdapters( const serialised = state.lastSerialization if (!serialised) continue - const node = LiteGraph.createNode(state.type, state.title) - if (!node) continue // unregistered node type — nothing to materialize + if (existing) { + // A live adapter already existing does NOT mean "nothing to do": the + // op layer reports a remote `update` through the same + // `lastAddedNodeIds` channel as `add` (it applies an update as + // delete+add of the STORE record, `ecsFollowerAdapter.ts`), so an id + // can arrive here again after its first successful materialization. + // Skipping unconditionally left the OLD adapter (stale `pos`/widgets, + // possibly the old node class) bound while the store moved on, and + // `serialiseStoredNodes()` paired the stale adapter with the new + // state on save. Reconcile instead: drop the existing adapter and + // fall through to recreate it from the (already-updated) store + // record, same as a fresh add. This only fires once we've confirmed + // the store still has a record for this id (above) — an id with a + // live adapter but NO agent-owned store record (e.g. an ordinary + // user-created node whose id happens to collide, or a caller passing + // stale ids) must never be touched; that's the store check above, + // not this one. `nodeIds` here only ever contains ids the op layer + // JUST reported changed — a successfully materialized id is removed + // from the caller's pending set and only reappears if a later frame + // reports it again — so this never reconciles an id that hasn't + // actually changed. + graph.remove?.(existing) + } - // `configure()` runs extension/node-class code (`onConfigure`, and - // `graph.add()` below can trigger `onConnectionsChange`) and can throw — - // do this BEFORE touching the store so a throw here leaves the - // authoritative record untouched. - try { - node.configure(serialised) - } catch { - continue // leaves the store-only record in place; retried next frame + let node = LiteGraph.createNode(state.type, state.title) + if (!node) { + // Unregistered node type — a supported workflow state that normal + // graph loading preserves via a placeholder (`LGraph.configure()`, + // "in case of error we create a replacement node to avoid losing + // info"; see also `LGraph.ts`'s subgraph-unpack placeholder path). + // Dropping the id silently here would desync the store (which still + // has it) from the graph (which never learns of it) — materialize the + // same placeholder instead so it round-trips through save/load like + // any other unregistered-type node. + node = new LGraphNode( + state.title || state.type || 'Missing Node', + state.type + ) + node.has_errors = true } node.id = nodeId + // Canonical ordering (`LGraph.configure()`: "add before configure, + // otherwise configure cannot create links") — `graph.add()` first, THEN + // `node.configure()`, so `node.graph` is set before slot/link resolution + // runs inside `configure()` (github-actions Medium, + // https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3917983386). + // Both steps stay fallible and rollback-safe: the store record is only + // cleared once `graph.add()` has already succeeded, and restored if + // `configure()` throws afterward. + let added: LGraphNode | null | undefined + try { + added = graph.add(node) + } catch (cause) { + added = undefined + reportError(cause, { + errorType: 'agent_node_materialize_add_failed', + context: { graphId: graph.id, nodeId: String(nodeId) } + }) + } + if (!added) continue // leaves the store-only record in place; retried next frame + // The store record this node is about to recreate is the ONLY thing // standing in the way of `attachNodeToStores`'s registration succeeding // with the CRDT-assigned id (`registerNode` sees an incumbent at this id // and returns undefined, which would otherwise force a mint-a-new-id // retry — wrong here, since this isn't a real collision, it's the same - // node already having store state). Clear it only once `configure()` - // above has already succeeded, and restore it if `graph.add()` (the - // other fallible step, e.g. `MAX_NUMBER_OF_NODES`) throws or declines, - // so a failure here never drops the node from the store and the save. + // node already having store state). Clear it only once `graph.add()` + // above has already succeeded, and restore it if `configure()` (the + // other fallible step — extension/node-class `onConfigure`, + // `onConnectionsChange`) throws, so a failure here never drops the node + // from the store and the save. nodeDataStore.deleteNode(scope, state) // Keyed by rootGraphId, matching every other clearNode call site in // graphMutations.ts's commit() (e.g. the reconcileNode-replace and @@ -121,20 +204,22 @@ export function materializeMissingAdapters( // not per owning (sub)graph. widgetValueStore.clearNode(scope.rootGraphId, nodeId) - let added: LGraphNode | null | undefined try { - added = graph.add(node) - } catch { - added = undefined - } - if (added) { - materialized.push(rawId) - } else { - // Restore the authoritative record so the node isn't lost from the - // store/save; `registerNode` accepts the same state object back - // (`toRaw(incumbent) === toRaw(state)` in nodeDataStore.registerNode). + node.configure(withNamedWidgetValues(serialised)) + } catch (cause) { + // Roll back both the store record AND the just-added live adapter, so + // a configure() failure doesn't leave a half-configured node live on + // the graph while also having dropped the authoritative record. + graph.remove?.(node) nodeDataStore.registerNode(scope, state) + reportError(cause, { + errorType: 'agent_node_materialize_configure_failed', + context: { graphId: graph.id, nodeId: String(nodeId) } + }) + continue } + + materialized.push(rawId) } return materialized diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index 98ec2b3340b..ffdc1bb4a67 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -165,15 +165,26 @@ export class EcsFollowerAdapter { if (session.applying) return true session.applying = true let updateCommitted = false + // Accumulate across every frame this call drains, not just the frame + // that triggered it: `applyQueuedFrame` sets `session.lastAddedNodeIds` + // per-frame, and a caller (the qa-59 materializer) only reads it once + // after `applyFrame` returns, so an earlier frame's additions in the + // same drain must not be clobbered by a later frame's (possibly empty) + // set. Reset only here, at the start of the drain, never inside the loop. + const drainedAddedNodeIds = new Set() try { while (session.frameQueue.length > 0) { const frame = session.frameQueue.shift() if (!frame) continue const committed = this.applyQueuedFrame(session, frame) + if (committed) { + for (const id of session.lastAddedNodeIds) drainedAddedNodeIds.add(id) + } if (frame === update) updateCommitted = committed } } finally { session.applying = false + session.lastAddedNodeIds = drainedAddedNodeIds } return updateCommitted } diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index fe85b77a4c5..94a5f08a4b8 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -616,11 +616,18 @@ export function useAgentCrdtFollower( clearStaleProbe() connected.value = false knownDocNodeIds = new Set() + // qa-59 / coderabbitai (discussion_r3920899129): ids pending + // materialization are scoped to `boundWorkflowId`'s doc — carrying them + // across a follower-target change would materialize a PRIOR doc's + // leftover ids against the NEW target's graph/store. Clear alongside + // every `adapter.unbind` below, at the same point the binding itself + // invalidates. if (!active) { if (next !== null) initialBind = false if (boundWorkflowId !== null) { adapter.unbind(boundWorkflowId) boundWorkflowId = null + pendingMaterializeIds.clear() } subscribedWorkflowId.value = null retarget(null) @@ -632,7 +639,10 @@ export function useAgentCrdtFollower( if (persisted !== null) { recordDevEvent('rebind', { workflowId: persisted }) if (boundWorkflowId !== persisted) { - if (boundWorkflowId !== null) adapter.unbind(boundWorkflowId) + if (boundWorkflowId !== null) { + adapter.unbind(boundWorkflowId) + pendingMaterializeIds.clear() + } adapter.bind(persisted, bridge.follower) boundWorkflowId = persisted } @@ -644,6 +654,7 @@ export function useAgentCrdtFollower( if (boundWorkflowId !== null) { adapter.unbind(boundWorkflowId) boundWorkflowId = null + pendingMaterializeIds.clear() } subscribedWorkflowId.value = null retarget(null) @@ -651,7 +662,10 @@ export function useAgentCrdtFollower( } initialBind = false if (boundWorkflowId !== next) { - if (boundWorkflowId !== null) adapter.unbind(boundWorkflowId) + if (boundWorkflowId !== null) { + adapter.unbind(boundWorkflowId) + pendingMaterializeIds.clear() + } adapter.bind(next, bridge.follower) boundWorkflowId = next } From 54711d9a2a85d590fb258c68b92266ad986894fa Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 3 Sep 2026 06:12:16 +0000 Subject: [PATCH 10/19] fix(agent-crdt): make adapter reconcile transactional against store reminting (qa-59) DrJKL P1 (discussion_r3921406436) + CodeRabbit Major (discussion_r3921409323): graph.add() calls attachNodeToStores -> registerNodeState, which mints and assigns a NEW id whenever an incumbent store record still occupies the target id. On the reconcile path (an id that already has a live adapter, e.g. a remote update), the old code cleared the store record AFTER graph.add() ran, so registerNodeState saw the incumbent, reminted the id, and the CRDT id went right back to having no live adapter -- reproducing the exact bug this module exists to fix. Fix: clear the store record BEFORE graph.add(), so registerNode sees an empty slot and keeps the CRDT id. Preserve rollback-safety the other way: if graph.add() throws or no-ops, restore the store record via registerNode before continuing (mirrors the existing configure()-throw rollback). Adds a regression test that reproduces DrJKL's exact repro via the real LGraph.add()/attachNodeToStores path (not a mocked add()), and confirmed it fails against the pre-fix ordering. --- .../agent/crdt/agentNodeMaterializer.test.ts | 37 +++++++++ .../agent/crdt/agentNodeMaterializer.ts | 77 +++++++++++-------- 2 files changed, 82 insertions(+), 32 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index 08dc4d87e41..d4e0da1af8b 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -113,4 +113,41 @@ describe('materializeMissingAdapters', () => { expect(materialized).toEqual([]) expect(nodeDataStore.getNode(scope.rootGraphId, toNodeId(1))).toBeDefined() }) + + it('materializes under the CRDT id, not a reminted one, on the reconcile path (DrJKL P1)', () => { + // Regression for + // https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921406436 + // and https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921409323: + // `graph.add()` internally calls `attachNodeToStores`/`registerNodeState`, + // which mints and assigns a NEW id whenever it finds an incumbent store + // record still occupying the target id. On the reconcile path (an id + // that already has a live adapter, e.g. a remote `update`), the store + // record must be cleared BEFORE `graph.add()` runs, or the CRDT id gets + // silently reminted and `graph.getNodeById(toNodeId(1))` goes right back + // to having no live adapter — the exact bug this module exists to fix. + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + materializeMissingAdapters(graph, ['1']) // first materialization + + expect(graph.getNodeById(toNodeId(1))).not.toBeNull() + + // Simulate a remote `update`: the op layer replaces the store record for + // the same id (delete+add under the hood) with fresh serialised state, + // then reports the id through the same `lastAddedNodeIds` channel again. + const nodeDataStore = useNodeDataStore() + const state = nodeDataStore.getNode(scope.rootGraphId, toNodeId(1)) + expect(state).toBeDefined() + if (state) state.lastSerialization = { ...state.lastSerialization, id: 1 } + + const materialized = materializeMissingAdapters(graph, ['1']) + + expect(materialized).toEqual(['1']) + // The live adapter must still be reachable under the ORIGINAL CRDT id — + // not undefined because `attachNodeToStores` reminted it. + const node = graph.getNodeById(toNodeId(1)) + expect(node).not.toBeNull() + expect(node?.id).toBe(toNodeId(1)) + expect(graph._nodes).toHaveLength(1) + expect(graph.serialize().nodes).toHaveLength(1) + }) }) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index 11738941e87..1760ac2306b 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -30,17 +30,20 @@ * exists → adopt, don't recreate" branch (`graphLayoutAttachment.ts`) instead * of duplicating that logic here. * - * Ordering matches canonical graph loading (`LGraph.configure()`: "add - * before configure, otherwise configure cannot create links") and stays - * rollback-safe: `graph.add()` runs first — on an EMPTY node, so a throw - * there touches neither the store nor a live adapter — then the store - * record is cleared, then `node.configure()` (which runs node-class / - * extension code, resolves slot/link references against `node.graph`, and - * can throw) runs against the now-attached node. A `configure()` throw - * rolls back both: the node is removed from the graph AND the store record - * is restored, so the node is never dropped from both the live graph and - * the store at once — it just stays store-only and is retried on the next - * frame. + * Ordering: the store record is cleared FIRST, then `graph.add()` (which + * runs `attachNodeToStores`/`registerNodeState`), then `node.configure()` + * (canonical `LGraph.configure()` ordering — "add before configure, + * otherwise configure cannot create links"). Clearing before `add()` is + * load-bearing, not cosmetic: `registerNodeState` mints and assigns a NEW id + * whenever it finds an incumbent record at the target id, so if the old + * record were still present when `add()` ran, the CRDT-assigned id would get + * silently reminted — reproducing the exact "stored node has no live + * adapter" bug this module exists to fix, just one id later. Both fallible + * steps stay rollback-safe: an `add()` throw restores the store record + * (nothing was attached, so there's no adapter to remove); a `configure()` + * throw removes the just-added adapter AND restores the store record. Either + * way the node is never dropped from both the live graph and the store at + * once — it just stays store-only and is retried on the next frame. */ import { LiteGraph } from '@/lib/litegraph/src/litegraph' import { LGraphNode } from '@/lib/litegraph/src/LGraphNode' @@ -167,14 +170,35 @@ export function materializeMissingAdapters( } node.id = nodeId + // The incumbent store record at `nodeId` is cleared BEFORE `graph.add()`, + // not after. `graph.add()` calls `attachNodeToStores` -> + // `registerNodeState`, which only adopts the exact CRDT id if + // `nodeDataStore.registerNode` finds no incumbent at that key; if the old + // record is still there when `add()` runs, `registerNode` returns + // `undefined`, and `attachNodeToStores`'s collision loop mints and + // assigns a NEW id to `node` before `add()` returns — silently + // reproducing the exact "stored node has no live adapter" bug this + // module exists to fix, because the CRDT id now has no adapter again + // (DrJKL, https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921406436; + // CodeRabbit, https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921409323). + // Clearing first makes `registerNode` see an empty slot and register the + // node under the CRDT id unchanged. + // + // Rollback-safety is preserved the other way: if `graph.add()` throws (or + // returns falsy), the store record is restored via `registerNode` before + // continuing, so a failed add() never leaves the id store-less. + nodeDataStore.deleteNode(scope, state) + // Keyed by rootGraphId, matching every other clearNode call site in + // graphMutations.ts's commit() (e.g. the reconcileNode-replace and + // deleteNode branches) — widget records are bucketed per root graph, + // not per owning (sub)graph. + widgetValueStore.clearNode(scope.rootGraphId, nodeId) + // Canonical ordering (`LGraph.configure()`: "add before configure, // otherwise configure cannot create links") — `graph.add()` first, THEN // `node.configure()`, so `node.graph` is set before slot/link resolution // runs inside `configure()` (github-actions Medium, // https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3917983386). - // Both steps stay fallible and rollback-safe: the store record is only - // cleared once `graph.add()` has already succeeded, and restored if - // `configure()` throws afterward. let added: LGraphNode | null | undefined try { added = graph.add(node) @@ -185,24 +209,13 @@ export function materializeMissingAdapters( context: { graphId: graph.id, nodeId: String(nodeId) } }) } - if (!added) continue // leaves the store-only record in place; retried next frame - - // The store record this node is about to recreate is the ONLY thing - // standing in the way of `attachNodeToStores`'s registration succeeding - // with the CRDT-assigned id (`registerNode` sees an incumbent at this id - // and returns undefined, which would otherwise force a mint-a-new-id - // retry — wrong here, since this isn't a real collision, it's the same - // node already having store state). Clear it only once `graph.add()` - // above has already succeeded, and restore it if `configure()` (the - // other fallible step — extension/node-class `onConfigure`, - // `onConnectionsChange`) throws, so a failure here never drops the node - // from the store and the save. - nodeDataStore.deleteNode(scope, state) - // Keyed by rootGraphId, matching every other clearNode call site in - // graphMutations.ts's commit() (e.g. the reconcileNode-replace and - // deleteNode branches) — widget records are bucketed per root graph, - // not per owning (sub)graph. - widgetValueStore.clearNode(scope.rootGraphId, nodeId) + if (!added) { + // `graph.add()` never attached anything on this path (it threw or + // no-op'd before registration), so there is no live adapter to remove + // — only the store record needs restoring. + nodeDataStore.registerNode(scope, state) + continue // leaves the store-only record in place; retried next frame + } try { node.configure(withNamedWidgetValues(serialised)) From ea853f2d512f7aa4a85d44fb91f1356ff96db9b2 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 3 Sep 2026 06:16:17 +0000 Subject: [PATCH 11/19] fix(agent-crdt): remove partially attached adapter when onAdded() throws (qa-59) CodeRabbit Major follow-up (discussion_r3921409323): LGraph.add() pushes the node into _nodes/_nodes_by_id and runs attachNodeToStores BEFORE calling node.onAdded?.(this), so an onAdded throw propagates out of add() after the node is already partially attached, not before. The add()-failure branch was only restoring the store record, leaving that partial adapter live on the graph and diverging from the (also-restored) store record. Fix: call graph.remove?.(node) unconditionally in the add()-catch branch. LGraph.remove() already no-ops safely if the node was never actually added, so this handles both 'threw before attaching' and 'threw after attaching, from onAdded' without needing to distinguish them. Adds a regression test with an onAdded() that throws, confirmed to fail without the graph.remove?.(node) call. --- .../agent/crdt/agentNodeMaterializer.test.ts | 37 ++++++++++++++++++- .../agent/crdt/agentNodeMaterializer.ts | 14 +++++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index d4e0da1af8b..72721e764a5 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -16,12 +16,23 @@ class DummyNode extends LGraphNode { } } +class ThrowsOnAddedNode extends LGraphNode { + constructor() { + super('throws-on-added') + } + + override onAdded(): void { + throw new Error('extension code blew up in onAdded') + } +} + beforeEach(() => { setActivePinia(createTestingPinia({ stubActions: false })) LiteGraph.registerNodeType('dummy', DummyNode) + LiteGraph.registerNodeType('throws-on-added', ThrowsOnAddedNode) }) -function seedAgentAddedNode(graph: LGraph, id: number) { +function seedAgentAddedNode(graph: LGraph, id: number, type = 'dummy') { const scope = graphScopeOf(graph) const mutations = createGraphMutations({ getScope: () => scope, @@ -30,7 +41,7 @@ function seedAgentAddedNode(graph: LGraph, id: number) { mutations.addNode( { id, - type: 'dummy', + type, pos: [0, 0], size: [100, 80], inputs: [], @@ -96,6 +107,28 @@ describe('materializeMissingAdapters', () => { expect(graph._nodes).toHaveLength(0) }) + it('removes the partially attached adapter when node.onAdded() throws inside graph.add()', () => { + // Regression for the CodeRabbit Major follow-up + // (https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921409323): + // `LGraph.add()` pushes the node into `_nodes`/`_nodes_by_id` and runs + // `attachNodeToStores` BEFORE calling `node.onAdded?.(this)`, so an + // `onAdded` throw propagates out of `add()` after the node is already + // partially attached, not before. The add()-failure branch must remove + // that partial adapter, not just restore the store record, or the graph + // and store diverge (adapter live but store also has its own record). + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1, 'throws-on-added') + const nodeDataStore = useNodeDataStore() + + const materialized = materializeMissingAdapters(graph, ['1']) + + expect(materialized).toEqual([]) + expect(nodeDataStore.getNode(scope.rootGraphId, toNodeId(1))).toBeDefined() + // No live adapter must remain bound to the graph after the rollback. + expect(graph._nodes).toHaveLength(0) + expect(graph.getNodeById(toNodeId(1))).toBeFalsy() + }) + it('does not delete the store record when node.configure() throws', () => { const graph = new LGraph() const scope = seedAgentAddedNode(graph, 1) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index 1760ac2306b..8860f94a974 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -204,15 +204,23 @@ export function materializeMissingAdapters( added = graph.add(node) } catch (cause) { added = undefined + // `LGraph.add()` can throw AFTER already attaching the node — it + // pushes into `_nodes`/`_nodes_by_id` and runs `attachNodeToStores` + // before calling `node.onAdded?.(this)`, and an `onAdded` throw + // propagates straight out uncaught (CodeRabbit, + // https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921409323). + // `graph.remove?.(node)` is safe to call unconditionally here: `LGraph. + // remove()` no-ops if the node was never actually added (its own + // `_nodes_by_id[node.id] == null` guard), so this correctly handles + // both "add() threw before attaching anything" and "add() threw after + // attaching, from onAdded" without needing to distinguish them. + graph.remove?.(node) reportError(cause, { errorType: 'agent_node_materialize_add_failed', context: { graphId: graph.id, nodeId: String(nodeId) } }) } if (!added) { - // `graph.add()` never attached anything on this path (it threw or - // no-op'd before registration), so there is no live adapter to remove - // — only the store record needs restoring. nodeDataStore.registerNode(scope, state) continue // leaves the store-only record in place; retried next frame } From c89eb0b49f6c9b6362b021a01c3219fadc797c69 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 3 Sep 2026 06:20:16 +0000 Subject: [PATCH 12/19] test(agent-crdt): use real reconcileNode mutation for the reconcile-path regression test The reconcile-path regression test manually mutated lastSerialization in place, which doesn't match how a real remote update clears/repopulates it (nodeDataStore.updateNode via graphMutations.ts's reconcileNode branch) and tripped a type error (SerializedNodeId vs number for the id override). Drive it through mutations.batch(...batch.reconcileNode(...)) instead, matching the real op-layer write path. Confirmed the test still fails against the pre-fix ordering with the exact DrJKL symptom (node.id undefined instead of the CRDT id). --- .../agent/crdt/agentNodeMaterializer.test.ts | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index 72721e764a5..5c7022ad60e 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -164,13 +164,31 @@ describe('materializeMissingAdapters', () => { expect(graph.getNodeById(toNodeId(1))).not.toBeNull() - // Simulate a remote `update`: the op layer replaces the store record for - // the same id (delete+add under the hood) with fresh serialised state, - // then reports the id through the same `lastAddedNodeIds` channel again. + // Simulate a remote `update`: the op layer reconciles the store record + // for the same id with fresh serialised state (same shape + // `ecsFollowerAdapter.ts` uses), then reports the id through the same + // `lastAddedNodeIds` channel again. + const mutations = createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + }) + mutations.batch( + { source: 'agent-remote', actor: 'agent:test', opId: 'op-1-update' }, + (batch) => + batch.reconcileNode({ + id: 1, + type: 'dummy', + pos: [10, 20], + size: [100, 80], + inputs: [], + outputs: [] + }) + ) + const nodeDataStore = useNodeDataStore() - const state = nodeDataStore.getNode(scope.rootGraphId, toNodeId(1)) - expect(state).toBeDefined() - if (state) state.lastSerialization = { ...state.lastSerialization, id: 1 } + expect( + nodeDataStore.getNode(scope.rootGraphId, toNodeId(1))?.lastSerialization + ).toBeDefined() const materialized = materializeMissingAdapters(graph, ['1']) From 85e748f44942e7490cf521eb3475b424e091a735 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 3 Sep 2026 03:57:19 -0700 Subject: [PATCH 13/19] fix(agent): derive live-node reconcile from store state Replace the id channel between the follower adapter and the materializer with a state-derived reconcile: every record without an owned live node is materialized through LiteGraph.createNode (restoring custom widgets, hit-testing and legacy rendering), orphan adapters left by remote delete/replace are detached without minting ops, and a failed add() restores the record, its widgets and the orphan. - run the reconcile under runMintPortsSuppressed so configure()'s set_widget echo is not re-minted - adopt the layout entry the op layer created with remote provenance - reconcile once the graph becomes ready, not only on the next frame - type the graph seam as a Pick - mint-port integration tests drive the real layout store bridge - strip reviewer names and discussion links from comments --- src/lib/litegraph/src/LGraph.test.ts | 22 +- .../extensions/agent/AgentPanelRoot.vue | 7 +- .../agent/crdt/agentNodeMaterializer.test.ts | 559 +++++++++++++----- .../agent/crdt/agentNodeMaterializer.ts | 409 ++++++------- .../ecsFollowerAdapter.integration.test.ts | 4 +- .../agent/crdt/ecsFollowerAdapter.ts | 47 +- .../extensions/agent/crdt/mintPortWiring.ts | 15 + .../agent/crdt/useAgentCrdtFollower.test.ts | 148 +++-- .../agent/crdt/useAgentCrdtFollower.ts | 82 +-- 9 files changed, 729 insertions(+), 564 deletions(-) diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index ac8e1f32748..e2782992c0c 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -865,21 +865,17 @@ describe('Store-driven serialization parity', () => { ) }) - // Desired-outcome pin, not the buggy one (DrJKL / coderabbitai, - // https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3914958522, - // #discussion_r3914616366): `agentNodeMaterializer.ts` now closes this gap - // for anything routed through `useAgentCrdtFollower`, but a bare - // `LGraph` + `graphMutations.addNode()` (as below) never calls the - // materializer, so the underlying `LGraph._nodes` gap this test documents - // is still real for any caller that skips the follower composable. - // `test.fails` keeps the assertions expressing the CORRECT behavior; the - // test flips green (and must be converted to a plain `test`) the day - // `LGraph.serialize()`/`addNode()` itself closes the gap without relying on - // the FE-only follower seam. - test.fails('does NOT drop an agent-added node from serialize() when only the ECS store, not LGraph._nodes, has it (qa-59)', ({ + // Pins the desired outcome, not the current one. `agentNodeMaterializer.ts` + // closes this gap for anything routed through `useAgentCrdtFollower`, but a + // bare `LGraph` + `graphMutations.addNode()` (as below) never calls the + // materializer, so the `LGraph._nodes` gap this test documents is still + // real for any caller that skips the follower composable. `test.fails` + // keeps the assertions expressing the CORRECT behavior; convert to a plain + // `test` the day `LGraph.serialize()`/`addNode()` itself closes the gap. + test.fails('does NOT drop an agent-added node from serialize() when only the ECS store, not LGraph._nodes, has it', ({ expect }) => { - // Regression test for the qa-59 defect: the CRDT follower's addNode path + // The CRDT follower's addNode path // (`graphMutations.commit()` -> nodeStore/widgetStore/layout, see // `src/core/graph/graphMutations.ts`) never constructs an LGraphNode and // never calls `LGraph.add()`, so the node exists in the ECS node-data diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index fe9fde4708c..8e12ae1d931 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -408,9 +408,10 @@ const { status: crdtStatus, enqueueHumanOperations } = useAgentCrdtFollower( graphMutations, () => resolvedUserInfo.value?.id ?? null, isBoundWorkflowActive, - // qa-59: same graph accessor as mintPortWiring's `getGraph` below — gives - // the follower a live LGraph to materialize agent-added node adapters into. - () => (app.isGraphReady ? app.rootGraph : null) + // `app.isGraphReady` is a plain getter; reading `canvasStore.canvas` (set + // right after `app.setup()`) makes the follower's graph watch fire once the + // root graph exists. + () => (canvasStore.canvas && app.isGraphReady ? app.rootGraph : null) ) const mintPortWiring = attachMintPortWiring({ isEnabled: () => agentPanelStore.enabled, diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index 5c7022ad60e..10c93538035 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -1,14 +1,32 @@ -import { graphScopeOf } from '@/types/graphScopeId' import { createTestingPinia } from '@pinia/testing' import { setActivePinia } from 'pinia' -import { beforeEach, describe, expect, it, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { createGraphMutations } from '@/core/graph/graphMutations' import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' +import { reportError } from '@/platform/telemetry/reportError' +// Mirrors the production bridge in AgentPanelRoot.vue, which takes the same +// exemption to drive the real layout store. +// eslint-disable-next-line import-x/no-restricted-paths +import { layoutStore } from '@/renderer/core/layout/store/layoutStore' +// eslint-disable-next-line import-x/no-restricted-paths +import { LayoutSource } from '@/renderer/core/layout/types' import { useNodeDataStore } from '@/stores/nodeDataStore' +import { useWidgetValueStore } from '@/stores/widgetValueStore' +import type { GraphScope } from '@/types/graphScopeId' +import { graphScopeOf } from '@/types/graphScopeId' +import type { RemoteMutationContext } from '@/types/graphMutationContext' import { toNodeId } from '@/types/nodeId' +import { widgetId } from '@/types/widgetId' + +import { reconcileAgentAdapters } from './agentNodeMaterializer' +import type { GraphOperation } from './graphOperations' +import { attachMintPortWiring } from './mintPortWiring' +import type { MintPortWiring } from './mintPortWiring' -import { materializeMissingAdapters } from './agentNodeMaterializer' +vi.mock('@/platform/telemetry/reportError', () => ({ + reportError: vi.fn() +})) class DummyNode extends LGraphNode { constructor() { @@ -16,6 +34,13 @@ class DummyNode extends LGraphNode { } } +class WidgetNode extends LGraphNode { + constructor() { + super('widget-node') + this.addWidget('number', 'value', 0, () => {}) + } +} + class ThrowsOnAddedNode extends LGraphNode { constructor() { super('throws-on-added') @@ -26,179 +51,415 @@ class ThrowsOnAddedNode extends LGraphNode { } } -beforeEach(() => { - setActivePinia(createTestingPinia({ stubActions: false })) - LiteGraph.registerNodeType('dummy', DummyNode) - LiteGraph.registerNodeType('throws-on-added', ThrowsOnAddedNode) -}) +const REMOTE: RemoteMutationContext = { + source: 'agent-remote', + actor: 'agent:test', + opId: 'op-test' +} +/** + * Same layout port the agent panel wires in production: remote adds create + * the layout entry with remote provenance BEFORE any live node exists, so a + * later `LGraph.add()` adopts it instead of minting a canvas-sourced create. + * A stubbed port would hide the add_node echo this test suite guards against. + */ +function remoteMutations(scope: GraphScope) { + return createGraphMutations({ + getScope: () => scope, + layout: { + createNode(scope, nodeId, { position, size }, context) { + layoutStore.applyOperation({ + type: 'createNode', + graphId: scope.rootGraphId, + ownerGraphId: scope.owningGraphId, + nodeId, + layout: { + id: nodeId, + position, + size, + bounds: { x: position.x, y: position.y, ...size }, + zIndex: layoutStore.allocateZIndex(), + visible: true + }, + source: LayoutSource.AgentRemote, + actor: context.actor, + opId: context.opId, + timestamp: Date.now() + }) + }, + deleteNodes(scope, nodeIds, context) { + const timestamp = Date.now() + layoutStore.applyOperations( + nodeIds.map((nodeId) => ({ + type: 'deleteNode', + graphId: scope.rootGraphId, + ownerGraphId: scope.owningGraphId, + nodeId, + source: LayoutSource.AgentRemote, + actor: context.actor, + opId: context.opId, + timestamp + })) + ) + } + } + }) +} + +function nodePayload(id: number, type = 'dummy') { + return { + id, + type, + pos: [0, 0], + size: [100, 80], + inputs: [], + outputs: [] + } +} + +/** Commit a remote add to the stores only, the way a follower frame does. */ function seedAgentAddedNode(graph: LGraph, id: number, type = 'dummy') { const scope = graphScopeOf(graph) - const mutations = createGraphMutations({ - getScope: () => scope, - layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + remoteMutations(scope).addNode(nodePayload(id, type), { + ...REMOTE, + opId: `op-${id}` }) - mutations.addNode( - { - id, - type, - pos: [0, 0], - size: [100, 80], - inputs: [], - outputs: [] - }, - { source: 'agent-remote', actor: 'agent:test', opId: `op-${id}` } - ) return scope } -describe('materializeMissingAdapters', () => { - it('gives a store-only agent-added node a live LGraph adapter', () => { - const graph = new LGraph() - seedAgentAddedNode(graph, 1) +beforeEach(() => { + setActivePinia(createTestingPinia({ stubActions: false })) + LiteGraph.registerNodeType('dummy', DummyNode) + LiteGraph.registerNodeType('widget-node', WidgetNode) + LiteGraph.registerNodeType('throws-on-added', ThrowsOnAddedNode) +}) - expect(graph._nodes).toHaveLength(0) +describe('reconcileAgentAdapters', () => { + describe('store record without a live node', () => { + it('gives the record a live node under its own id', () => { + const graph = new LGraph() + seedAgentAddedNode(graph, 1) + expect(graph._nodes).toHaveLength(0) - const materialized = materializeMissingAdapters(graph, ['1']) + const materialized = reconcileAgentAdapters(graph) - expect(materialized).toEqual(['1']) - expect(graph._nodes).toHaveLength(1) - expect(graph.getNodeById(toNodeId(1))).not.toBeNull() - // `graph._nodes` alone doesn't exercise `LGraph.serialize()` — assert the - // actual save path a regression in `serialiseStoredNodes()` would break. - expect(graph.serialize().nodes).toHaveLength(1) - }) + expect(materialized).toEqual([toNodeId(1)]) + expect(graph._nodes).toHaveLength(1) + expect(graph.getNodeById(toNodeId(1))?.id).toBe(toNodeId(1)) + // `_nodes` alone does not exercise the save path; a regression in + // `serialiseStoredNodes()` shows up here. + expect(graph.serialize().nodes).toHaveLength(1) + expect(reportError).not.toHaveBeenCalled() + }) + + it('applies the serialised widget values to the new node', () => { + const graph = new LGraph() + const scope = graphScopeOf(graph) + remoteMutations(scope).addNode( + { ...nodePayload(1, 'widget-node'), widgets_values: { value: 7 } }, + REMOTE + ) + + reconcileAgentAdapters(graph) + + const node = graph.getNodeById(toNodeId(1)) + expect(node?.widgets?.[0].value).toBe(7) + expect( + useWidgetValueStore().getWidget( + widgetId(scope.rootGraphId, toNodeId(1), 'value') + )?.value + ).toBe(7) + }) - it('is a no-op for an id that already has a live adapter (getNodeById falsiness fix)', () => { - // Regression: LGraph.getNodeById returns `undefined`, not `null`, for an - // absent id (`this._nodes_by_id[id]`), so a strict `!== null` check - // never skips an already-materialized node and always re-runs the - // delete/recreate path. This must not touch the store a second time. - const graph = new LGraph() - graph.add(new DummyNode()) - const liveId = graph._nodes[0].id - const nodeDataStore = useNodeDataStore() - const deleteSpy = vi.spyOn(nodeDataStore, 'deleteNode') - - const materialized = materializeMissingAdapters(graph, [String(liveId)]) - - expect(materialized).toEqual([]) - expect(deleteSpy).not.toHaveBeenCalled() + it('is idempotent once the node is live', () => { + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + reconcileAgentAdapters(graph) + const node = graph.getNodeById(toNodeId(1)) + const state = node?._state + + expect(reconcileAgentAdapters(graph)).toEqual([]) + expect(graph._nodes).toEqual([node]) + expect(node?._state).toBe(state) + expect(useNodeDataStore().ownsNode(scope, state!)).toBe(true) + }) + + it('leaves a locally added node alone', () => { + const graph = new LGraph() + graph.add(new DummyNode()) + const deleteSpy = vi.spyOn(useNodeDataStore(), 'deleteNode') + + expect(reconcileAgentAdapters(graph)).toEqual([]) + expect(deleteSpy).not.toHaveBeenCalled() + expect(graph._nodes).toHaveLength(1) + }) + + it('builds an error placeholder for an unregistered type', () => { + const graph = new LGraph() + seedAgentAddedNode(graph, 1, 'not-a-registered-type') + + expect(reconcileAgentAdapters(graph)).toEqual([toNodeId(1)]) + const node = graph.getNodeById(toNodeId(1)) + expect(node?.has_errors).toBe(true) + expect(node?.type).toBe('not-a-registered-type') + }) }) - it('does not delete the store record when graph.add() throws (rollback-safe)', () => { - const graph = new LGraph() - const scope = seedAgentAddedNode(graph, 1) - const nodeDataStore = useNodeDataStore() - const before = nodeDataStore.getNode(scope.rootGraphId, toNodeId(1)) - expect(before).toBeDefined() + describe('remote update of a live node', () => { + it('keeps the same live node when the record is updated in place', () => { + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + reconcileAgentAdapters(graph) + const live = graph.getNodeById(toNodeId(1)) - vi.spyOn(graph, 'add').mockImplementation(() => { - throw 'LiteGraph: max number of nodes in a graph reached' + remoteMutations(scope).batch(REMOTE, (batch) => + batch.reconcileNode({ ...nodePayload(1), pos: [10, 20] }) + ) + + expect(reconcileAgentAdapters(graph)).toEqual([]) + expect(graph._nodes).toHaveLength(1) + expect(graph.getNodeById(toNodeId(1))).toBe(live) }) - const materialized = materializeMissingAdapters(graph, ['1']) + it('replaces a node whose record was re-created under the same id', () => { + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + reconcileAgentAdapters(graph) + const stale = graph.getNodeById(toNodeId(1)) + expect(stale).toBeDefined() + + const mutations = remoteMutations(scope) + mutations.deleteNode(toNodeId(1), [], REMOTE) + mutations.addNode(nodePayload(1), { ...REMOTE, opId: 'op-1-again' }) - expect(materialized).toEqual([]) - // The authoritative record must survive the failed add — this is the - // DrJKL data-loss report: deleting before the fallible steps loses the - // node from every future save when `graph.add()` throws. - expect(nodeDataStore.getNode(scope.rootGraphId, toNodeId(1))).toBeDefined() - expect(graph._nodes).toHaveLength(0) + const materialized = reconcileAgentAdapters(graph) + + expect(materialized).toEqual([toNodeId(1)]) + expect(graph._nodes).toHaveLength(1) + const replacement = graph.getNodeById(toNodeId(1)) + expect(replacement?.id).toBe(toNodeId(1)) + expect(replacement).not.toBe(stale) + expect(graph._nodes).not.toContain(stale) + expect(stale?.graph).toBeNull() + expect(graph.serialize().nodes).toHaveLength(1) + }) }) - it('removes the partially attached adapter when node.onAdded() throws inside graph.add()', () => { - // Regression for the CodeRabbit Major follow-up - // (https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921409323): - // `LGraph.add()` pushes the node into `_nodes`/`_nodes_by_id` and runs - // `attachNodeToStores` BEFORE calling `node.onAdded?.(this)`, so an - // `onAdded` throw propagates out of `add()` after the node is already - // partially attached, not before. The add()-failure branch must remove - // that partial adapter, not just restore the store record, or the graph - // and store diverge (adapter live but store also has its own record). - const graph = new LGraph() - const scope = seedAgentAddedNode(graph, 1, 'throws-on-added') - const nodeDataStore = useNodeDataStore() - - const materialized = materializeMissingAdapters(graph, ['1']) - - expect(materialized).toEqual([]) - expect(nodeDataStore.getNode(scope.rootGraphId, toNodeId(1))).toBeDefined() - // No live adapter must remain bound to the graph after the rollback. - expect(graph._nodes).toHaveLength(0) - expect(graph.getNodeById(toNodeId(1))).toBeFalsy() + describe('remote removal of a live node', () => { + it('detaches the node after a remote delete', () => { + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + reconcileAgentAdapters(graph) + const removed = vi.fn() + graph.events.addEventListener('node:removed', removed) + + remoteMutations(scope).deleteNode(toNodeId(1), [], REMOTE) + + expect(reconcileAgentAdapters(graph)).toEqual([]) + expect(graph._nodes).toHaveLength(0) + expect(graph.getNodeById(toNodeId(1))).toBeFalsy() + expect(graph.serialize().nodes).toHaveLength(0) + expect(removed).toHaveBeenCalledOnce() + }) + + it('detaches every node after a remote clear', () => { + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + seedAgentAddedNode(graph, 2) + expect(reconcileAgentAdapters(graph)).toHaveLength(2) + + remoteMutations(scope).clearSemanticGraph(REMOTE) + + expect(reconcileAgentAdapters(graph)).toEqual([]) + expect(graph._nodes).toHaveLength(0) + expect(graph.serialize().nodes).toHaveLength(0) + }) + + it('detaches nodes dropped by an authoritative snapshot', () => { + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + seedAgentAddedNode(graph, 2) + reconcileAgentAdapters(graph) + + remoteMutations(scope).batch(REMOTE, (batch) => + batch.removeMissing([toNodeId(2)], []) + ) + + expect(reconcileAgentAdapters(graph)).toEqual([]) + expect(graph._nodes.map((node) => node.id)).toEqual([toNodeId(2)]) + }) }) - it('does not delete the store record when node.configure() throws', () => { - const graph = new LGraph() - const scope = seedAgentAddedNode(graph, 1) - const nodeDataStore = useNodeDataStore() + describe('failure handling', () => { + it('keeps the record when graph.add() throws', () => { + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + const nodeDataStore = useNodeDataStore() + vi.spyOn(graph, 'add').mockImplementation(() => { + throw 'LiteGraph: max number of nodes in a graph reached' + }) - // Not restored explicitly: root `vitest.config` sets `restoreMocks: true`, - // so vitest already restores every mock between tests - // (https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3920899113). - vi.spyOn(LGraphNode.prototype, 'configure').mockImplementation(() => { - throw new Error('bad payload') + expect(reconcileAgentAdapters(graph)).toEqual([]) + expect( + nodeDataStore.getNode(scope.rootGraphId, toNodeId(1)) + ).toBeDefined() + expect(graph._nodes).toHaveLength(0) + expect(reportError).toHaveBeenCalledWith( + 'LiteGraph: max number of nodes in a graph reached', + expect.objectContaining({ + errorType: 'agent_node_materialize_add_failed' + }) + ) }) - const materialized = materializeMissingAdapters(graph, ['1']) + it('keeps the record widget values when graph.add() throws', () => { + const graph = new LGraph() + const scope = graphScopeOf(graph) + remoteMutations(scope).addNode( + { ...nodePayload(1, 'widget-node'), widgets_values: { value: 7 } }, + REMOTE + ) + vi.spyOn(graph, 'add').mockImplementation(() => { + throw new Error('nope') + }) + + reconcileAgentAdapters(graph) + + expect( + useWidgetValueStore().getWidget( + widgetId(scope.rootGraphId, toNodeId(1), 'value') + )?.value + ).toBe(7) + }) - expect(materialized).toEqual([]) - expect(nodeDataStore.getNode(scope.rootGraphId, toNodeId(1))).toBeDefined() + it('removes the partially attached node when onAdded() throws inside graph.add()', () => { + // `LGraph.add()` attaches the node before calling `onAdded`, so the + // throw leaves a live node behind unless the rollback takes it out. + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1, 'throws-on-added') + + expect(reconcileAgentAdapters(graph)).toEqual([]) + expect( + useNodeDataStore().getNode(scope.rootGraphId, toNodeId(1)) + ).toBeDefined() + expect(graph._nodes).toHaveLength(0) + expect(graph.getNodeById(toNodeId(1))).toBeFalsy() + }) + + it('retries a failed add on the next reconcile', () => { + const graph = new LGraph() + seedAgentAddedNode(graph, 1) + const add = vi.spyOn(graph, 'add').mockImplementationOnce(() => { + throw new Error('transient') + }) + expect(reconcileAgentAdapters(graph)).toEqual([]) + add.mockRestore() + + expect(reconcileAgentAdapters(graph)).toEqual([toNodeId(1)]) + expect(graph._nodes).toHaveLength(1) + }) + + it('keeps the attached node when configure() throws', () => { + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + vi.spyOn(LGraphNode.prototype, 'configure').mockImplementation(() => { + throw new Error('bad payload') + }) + + expect(reconcileAgentAdapters(graph)).toEqual([toNodeId(1)]) + expect(graph._nodes).toHaveLength(1) + expect( + useNodeDataStore().getNode(scope.rootGraphId, toNodeId(1)) + ).toBeDefined() + expect(reportError).toHaveBeenCalledWith( + expect.any(Error), + expect.objectContaining({ + errorType: 'agent_node_materialize_configure_failed' + }) + ) + }) }) - it('materializes under the CRDT id, not a reminted one, on the reconcile path (DrJKL P1)', () => { - // Regression for - // https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921406436 - // and https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921409323: - // `graph.add()` internally calls `attachNodeToStores`/`registerNodeState`, - // which mints and assigns a NEW id whenever it finds an incumbent store - // record still occupying the target id. On the reconcile path (an id - // that already has a live adapter, e.g. a remote `update`), the store - // record must be cleared BEFORE `graph.add()` runs, or the CRDT id gets - // silently reminted and `graph.getNodeById(toNodeId(1))` goes right back - // to having no live adapter — the exact bug this module exists to fix. - const graph = new LGraph() - const scope = seedAgentAddedNode(graph, 1) - materializeMissingAdapters(graph, ['1']) // first materialization - - expect(graph.getNodeById(toNodeId(1))).not.toBeNull() - - // Simulate a remote `update`: the op layer reconciles the store record - // for the same id with fresh serialised state (same shape - // `ecsFollowerAdapter.ts` uses), then reports the id through the same - // `lastAddedNodeIds` channel again. - const mutations = createGraphMutations({ - getScope: () => scope, - layout: { createNode: vi.fn(), deleteNodes: vi.fn() } - }) - mutations.batch( - { source: 'agent-remote', actor: 'agent:test', opId: 'op-1-update' }, - (batch) => - batch.reconcileNode({ - id: 1, - type: 'dummy', - pos: [10, 20], - size: [100, 80], - inputs: [], - outputs: [] + describe('with mint ports attached', () => { + let graph: LGraph + let minted: GraphOperation[] + let wiring: MintPortWiring + + beforeEach(() => { + graph = new LGraph() + minted = [] + wiring = attachMintPortWiring({ + isEnabled: () => true, + isDocBound: () => true, + enqueue: (operations) => minted.push(...operations), + layoutChanges: (listener) => layoutStore.onChange(listener), + localActorPrefix: 'user-', + getGraph: () => graph + }) + }) + + afterEach(() => { + wiring.detach() + }) + + async function settle(): Promise { + await Promise.resolve() + await Promise.resolve() + await Promise.resolve() + } + + it('does not echo a remote add back as local operations', async () => { + const scope = graphScopeOf(graph) + remoteMutations(scope).addNode( + { ...nodePayload(1, 'widget-node'), widgets_values: { value: 7 } }, + REMOTE + ) + + expect(reconcileAgentAdapters(graph)).toEqual([toNodeId(1)]) + await settle() + + expect(minted).toEqual([]) + }) + + it('does not echo a remote re-create or delete back as local operations', async () => { + const scope = graphScopeOf(graph) + const mutations = remoteMutations(scope) + mutations.addNode(nodePayload(1), REMOTE) + reconcileAgentAdapters(graph) + + mutations.deleteNode(toNodeId(1), [], REMOTE) + mutations.addNode(nodePayload(1), { ...REMOTE, opId: 'op-1-again' }) + reconcileAgentAdapters(graph) + mutations.deleteNode(toNodeId(1), [], REMOTE) + reconcileAgentAdapters(graph) + await settle() + + expect(graph._nodes).toHaveLength(0) + expect(minted).toEqual([]) + }) + + it('still mints a later local widget edit on the materialized node', async () => { + const scope = graphScopeOf(graph) + remoteMutations(scope).addNode( + { ...nodePayload(1, 'widget-node'), widgets_values: { value: 7 } }, + REMOTE + ) + reconcileAgentAdapters(graph) + await settle() + + const node = graph.getNodeById(toNodeId(1)) + node!.widgets![0].value = 8 + await settle() + + expect(minted).toEqual([ + expect.objectContaining({ + op: 'set_widget', + node_id: toNodeId(1), + widget: 'value', + value: 8, + old: 7 }) - ) - - const nodeDataStore = useNodeDataStore() - expect( - nodeDataStore.getNode(scope.rootGraphId, toNodeId(1))?.lastSerialization - ).toBeDefined() - - const materialized = materializeMissingAdapters(graph, ['1']) - - expect(materialized).toEqual(['1']) - // The live adapter must still be reachable under the ORIGINAL CRDT id — - // not undefined because `attachNodeToStores` reminted it. - const node = graph.getNodeById(toNodeId(1)) - expect(node).not.toBeNull() - expect(node?.id).toBe(toNodeId(1)) - expect(graph._nodes).toHaveLength(1) - expect(graph.serialize().nodes).toHaveLength(1) + ]) + }) }) }) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index 8860f94a974..a564ec4f9ae 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -1,247 +1,214 @@ -/** - * qa-59 bridge: gives agent-added nodes a live `LGraphNode` adapter. - * - * `graphMutations.ts` (the pure, litegraph-free op layer — see CRDT KEEP-ALIVE - * #3) only ever writes `useNodeDataStore`/`useWidgetValueStore`/the layout - * port for a remote `add_node`. It never constructs an `LGraphNode` or calls - * `LGraph.add()`, because it cannot import either without breaking the - * op-layer's DOM/litegraph-free purity guarantee. The result: an agent-added - * node renders (the canvas is store-driven) but has no entry in - * `LGraph._nodes`, so every `serialize()` hits the adapter/state mismatch - * branch in `serialiseStoredNodes()` and silently drops the node — see - * `LGraph.test.ts` "drops an agent-added node from serialize() ... (qa-59)". - * - * This module is the FE-only, app-context-aware seam that closes the gap. It - * intentionally lives next to `useAgentCrdtFollower.ts` (already Vue/app-layer - * code, already owns the `EcsFollowerAdapter` instance) rather than inside the - * op layer. - * - * Approach: rather than trying to make `LGraphNode` adopt the *existing* - * store `NodeState` in place (risky — `LGraphNode`'s constructor builds a - * fresh `_state` and its input/output slot views are bound to that object's - * `inputs`/`outputs` arrays by reference, so swapping `_state` post- - * construction risks desyncing those views), this deletes the store-only - * record for the id and lets the normal `node.configure()` + - * `attachNodeToStores` + `graph.add()` path recreate it from - * `NodeState.lastSerialization` — which IS the semantic payload that produced - * the current store state, so the recreated record is equivalent. That keeps - * every write on a single, already-tested code path instead of adding a - * second one, and reuses `attachNodeLayout`'s existing "layout already - * exists → adopt, don't recreate" branch (`graphLayoutAttachment.ts`) instead - * of duplicating that logic here. - * - * Ordering: the store record is cleared FIRST, then `graph.add()` (which - * runs `attachNodeToStores`/`registerNodeState`), then `node.configure()` - * (canonical `LGraph.configure()` ordering — "add before configure, - * otherwise configure cannot create links"). Clearing before `add()` is - * load-bearing, not cosmetic: `registerNodeState` mints and assigns a NEW id - * whenever it finds an incumbent record at the target id, so if the old - * record were still present when `add()` ran, the CRDT-assigned id would get - * silently reminted — reproducing the exact "stored node has no live - * adapter" bug this module exists to fix, just one id later. Both fallible - * steps stay rollback-safe: an `add()` throw restores the store record - * (nothing was attached, so there's no adapter to remove); a `configure()` - * throw removes the just-added adapter AND restores the store record. Either - * way the node is never dropped from both the live graph and the store at - * once — it just stays store-only and is retried on the next frame. - */ -import { LiteGraph } from '@/lib/litegraph/src/litegraph' -import { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { LGraph } from '@/lib/litegraph/src/LGraph' +import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' import { reportError } from '@/platform/telemetry/reportError' import { useNodeDataStore } from '@/stores/nodeDataStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' +import type { GraphScope } from '@/types/graphScopeId' import { graphScopeOf } from '@/types/graphScopeId' -import { parseNodeId } from '@/types/nodeId' +import type { NodeId } from '@/types/nodeId' +import type { NodeState } from '@/types/nodeState' +import { widgetId } from '@/types/widgetId' +import type { WidgetStateInit } from '@/types/widgetState' -/** - * `readSemanticNode` (`ecsFollowerAdapter.ts`) builds `widgets_values` as a - * NAME-keyed object from the doc's `widgets` Y.Map, so that's the shape - * `NodeState.lastSerialization` carries. `LGraphNode.configure()` only - * consumes a name-keyed map via `widgets_values_named` — its - * `widgets_values` handling is positional-array-only - * (`Array.from(info.widgets_values ?? [])`), with the named fallback gated - * on `fallbackWidgetsValuesNames`, which most agent-added node types won't - * have. Left alone, an object under `widgets_values` silently becomes `[]` - * and every named widget value is dropped on materialization - * (github-actions HIGH, - * https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3917983350). - * Derive the named map explicitly so configure() restores it regardless of - * whether the target node class declares a fallback. - */ -function withNamedWidgetValues(serialised: ISerialisedNode): ISerialisedNode { - const values = serialised.widgets_values - if ( - values === undefined || - Array.isArray(values) || - serialised.widgets_values_named !== undefined - ) { - return serialised - } - return { ...serialised, widgets_values_named: values } -} +import { runMintPortsSuppressed } from './mintPortWiring' -/** The graph surface this module needs — mirrors `MintableGraph` in - * `mintPortWiring.ts` so the two DI shapes stay compatible for callers that - * already hold a `getGraph()` closure. */ -export interface MaterializableGraph { - id: string - rootGraph: { id: string } - getNodeById(id: ReturnType): LGraphNode | null - add(node: LGraphNode): LGraphNode | null | undefined - remove?(node: LGraphNode): void -} +export type MaterializableGraph = Pick< + LGraph, + | 'id' + | 'rootGraph' + | '_nodes' + | '_nodes_by_id' + | 'add' + | 'remove' + | 'updateExecutionOrder' + | 'setDirtyCanvas' + | 'list_of_graphcanvas' + | 'onNodeRemoved' + | 'events' +> /** - * For every id in `nodeIds` that the ECS store knows about but `graph` does - * not yet have a live adapter for, constructs one and attaches it. Ids that - * already have a live adapter, or that the store no longer has (e.g. deleted - * by a later frame in the same batch), are skipped silently — this runs - * after every applied frame, so a node that legitimately isn't there yet - * will be picked up on its own frame. + * Bring the live graph's node adapters in line with the node records the op + * layer already committed to the stores. * - * Returns the ids actually materialized, for callers that want to log/test. + * Remote frames mutate the stores only. A remote add leaves a record with no + * live node; a remote update re-registers the record under a new state object, + * so the node that was bound to the old state no longer owns anything; a + * remote delete leaves a node bound to nothing. All three are derived from + * store state here, without the op layer telling us which ids changed. + * + * @returns ids that received a new live node. */ -export function materializeMissingAdapters( - graph: MaterializableGraph, - nodeIds: Iterable -): string[] { - const scope = graphScopeOf(graph) - const nodeDataStore = useNodeDataStore() - const widgetValueStore = useWidgetValueStore() - const materialized: string[] = [] - - for (const rawId of nodeIds) { - const nodeId = parseNodeId(rawId) - if (nodeId === null) continue - // `LGraph.getNodeById` indexes `_nodes_by_id` directly and returns - // `undefined` for an absent id despite its `LGraphNode | null` type, so - // this must be a falsiness check, not `!== null` (which is always true - // for `undefined` and made this whole function a no-op in production). - // - const existing = graph.getNodeById(nodeId) +export function reconcileAgentAdapters(graph: MaterializableGraph): NodeId[] { + return runMintPortsSuppressed(() => reconcile(graph)) +} - const state = nodeDataStore.getNode(scope.rootGraphId, nodeId) - if (!state) continue // store no longer has it (deleted later in the batch) - if (state.graphId !== scope.owningGraphId) continue // lives in a different (sub)graph +function reconcile(graph: MaterializableGraph): NodeId[] { + const scope = graphScopeOf(graph) + const nodeStore = useNodeDataStore() + const records = nodeStore.getGraphNodesFor( + scope.rootGraphId, + scope.owningGraphId + ) + const orphans = graph._nodes.filter( + (node) => !nodeStore.ownsNode(scope, node._state) + ) + const orphansById = new Map(orphans.map((node) => [node.id, node])) + const materialized: NodeId[] = [] + for (const state of records) { + const live = graph._nodes_by_id[state.id] + if (live && nodeStore.ownsNode(scope, live._state)) continue const serialised = state.lastSerialization if (!serialised) continue - - if (existing) { - // A live adapter already existing does NOT mean "nothing to do": the - // op layer reports a remote `update` through the same - // `lastAddedNodeIds` channel as `add` (it applies an update as - // delete+add of the STORE record, `ecsFollowerAdapter.ts`), so an id - // can arrive here again after its first successful materialization. - // Skipping unconditionally left the OLD adapter (stale `pos`/widgets, - // possibly the old node class) bound while the store moved on, and - // `serialiseStoredNodes()` paired the stale adapter with the new - // state on save. Reconcile instead: drop the existing adapter and - // fall through to recreate it from the (already-updated) store - // record, same as a fresh add. This only fires once we've confirmed - // the store still has a record for this id (above) — an id with a - // live adapter but NO agent-owned store record (e.g. an ordinary - // user-created node whose id happens to collide, or a caller passing - // stale ids) must never be touched; that's the store check above, - // not this one. `nodeIds` here only ever contains ids the op layer - // JUST reported changed — a successfully materialized id is removed - // from the caller's pending set and only reappears if a later frame - // reports it again — so this never reconciles an id that hasn't - // actually changed. - graph.remove?.(existing) + if ( + materialize(graph, scope, state, serialised, orphansById.get(state.id)) + ) { + materialized.push(state.id) } + } + + const recordIds = new Set(records.map((state) => state.id)) + const detached = orphans.filter( + (orphan) => + graph._nodes_by_id[orphan.id] !== orphan || !recordIds.has(orphan.id) + ) + for (const orphan of detached) detachOrphan(graph, orphan) + if (detached.length > 0) { + graph.updateExecutionOrder() + graph.setDirtyCanvas(true, true) + } + return materialized +} + +function materialize( + graph: MaterializableGraph, + scope: GraphScope, + state: NodeState, + serialised: ISerialisedNode, + orphan: LGraphNode | undefined +): boolean { + const nodeStore = useNodeDataStore() + const widgetStore = useWidgetValueStore() + const node = + LiteGraph.createNode(state.type, state.title) ?? missingNode(state) + node.id = state.id - let node = LiteGraph.createNode(state.type, state.title) - if (!node) { - // Unregistered node type — a supported workflow state that normal - // graph loading preserves via a placeholder (`LGraph.configure()`, - // "in case of error we create a replacement node to avoid losing - // info"; see also `LGraph.ts`'s subgraph-unpack placeholder path). - // Dropping the id silently here would desync the store (which still - // has it) from the graph (which never learns of it) — materialize the - // same placeholder instead so it round-trips through save/load like - // any other unregistered-type node. - node = new LGraphNode( - state.title || state.type || 'Missing Node', - state.type + const widgets = widgetStore.getNodeWidgets(scope.rootGraphId, state.id).map( + (widget): WidgetStateInit => ({ + disabled: widget.disabled, + label: widget.label, + name: widget.name, + options: widget.options, + serialize: widget.serialize, + type: widget.type, + value: widget.value, + y: widget.y + }) + ) + const restore = () => { + nodeStore.registerNode(scope, state) + for (const widget of widgets) { + widgetStore.registerWidget( + widgetId(scope.rootGraphId, state.id, widget.name ?? ''), + widget ) - node.has_errors = true } - node.id = nodeId + if (orphan) graph._nodes_by_id[orphan.id] = orphan + } - // The incumbent store record at `nodeId` is cleared BEFORE `graph.add()`, - // not after. `graph.add()` calls `attachNodeToStores` -> - // `registerNodeState`, which only adopts the exact CRDT id if - // `nodeDataStore.registerNode` finds no incumbent at that key; if the old - // record is still there when `add()` runs, `registerNode` returns - // `undefined`, and `attachNodeToStores`'s collision loop mints and - // assigns a NEW id to `node` before `add()` returns — silently - // reproducing the exact "stored node has no live adapter" bug this - // module exists to fix, because the CRDT id now has no adapter again - // (DrJKL, https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921406436; - // CodeRabbit, https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921409323). - // Clearing first makes `registerNode` see an empty slot and register the - // node under the CRDT id unchanged. - // - // Rollback-safety is preserved the other way: if `graph.add()` throws (or - // returns falsy), the store record is restored via `registerNode` before - // continuing, so a failed add() never leaves the id store-less. - nodeDataStore.deleteNode(scope, state) - // Keyed by rootGraphId, matching every other clearNode call site in - // graphMutations.ts's commit() (e.g. the reconcileNode-replace and - // deleteNode branches) — widget records are bucketed per root graph, - // not per owning (sub)graph. - widgetValueStore.clearNode(scope.rootGraphId, nodeId) + const rollback = (cause: unknown) => { + // `add()` may throw after attaching (from `onAdded`); only then is there + // a live node to take back out. + if (graph._nodes_by_id[node.id] === node) graph.remove(node) + restore() + reportError(cause, { + errorType: 'agent_node_materialize_add_failed', + context: { graphId: graph.id, nodeId: String(state.id) } + }) + return false + } - // Canonical ordering (`LGraph.configure()`: "add before configure, - // otherwise configure cannot create links") — `graph.add()` first, THEN - // `node.configure()`, so `node.graph` is set before slot/link resolution - // runs inside `configure()` (github-actions Medium, - // https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3917983386). - let added: LGraphNode | null | undefined - try { - added = graph.add(node) - } catch (cause) { - added = undefined - // `LGraph.add()` can throw AFTER already attaching the node — it - // pushes into `_nodes`/`_nodes_by_id` and runs `attachNodeToStores` - // before calling `node.onAdded?.(this)`, and an `onAdded` throw - // propagates straight out uncaught (CodeRabbit, - // https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3921409323). - // `graph.remove?.(node)` is safe to call unconditionally here: `LGraph. - // remove()` no-ops if the node was never actually added (its own - // `_nodes_by_id[node.id] == null` guard), so this correctly handles - // both "add() threw before attaching anything" and "add() threw after - // attaching, from onAdded" without needing to distinguish them. - graph.remove?.(node) - reportError(cause, { - errorType: 'agent_node_materialize_add_failed', - context: { graphId: graph.id, nodeId: String(nodeId) } - }) - } - if (!added) { - nodeDataStore.registerNode(scope, state) - continue // leaves the store-only record in place; retried next frame - } + // `add()` only adopts the record's id into an empty slot; with the record + // still registered its collision loop would mint a fresh id instead. + // + // `add()` also adopts the record's layout entry rather than creating one: + // the op layer created it with remote provenance, and the layout store + // delivers changes on a microtask, after the mint-suppression bracket has + // ended. Provenance on the operation, not the bracket, is what keeps the + // layout port quiet here. + nodeStore.deleteNode(scope, state) + let added: LGraphNode | null | undefined + try { + added = graph.add(node) + } catch (cause) { + return rollback(cause) + } + if (!added) return rollback('LGraph.add returned no node') - try { - node.configure(withNamedWidgetValues(serialised)) - } catch (cause) { - // Roll back both the store record AND the just-added live adapter, so - // a configure() failure doesn't leave a half-configured node live on - // the graph while also having dropped the authoritative record. - graph.remove?.(node) - nodeDataStore.registerNode(scope, state) - reportError(cause, { - errorType: 'agent_node_materialize_configure_failed', - context: { graphId: graph.id, nodeId: String(nodeId) } - }) - continue - } + try { + node.configure(withNamedWidgetValues(serialised)) + } catch (cause) { + // The node is attached and consistent with the stores; removing it here + // would also drop the layout entry it adopted. Keep it and report. + reportError(cause, { + errorType: 'agent_node_materialize_configure_failed', + context: { graphId: graph.id, nodeId: String(state.id) } + }) + } + return true +} - materialized.push(rawId) +/** + * Drop a node the stores no longer know about from the live graph without + * going through `LGraph.remove()`: that path unregisters store state, releases + * widget records and emits a layout delete, all of which now belong to the + * record (and replacement node) that took over the id. + */ +function detachOrphan(graph: MaterializableGraph, orphan: LGraphNode): void { + try { + orphan.onRemoved?.() + } catch (cause) { + console.error(cause) + } + const index = graph._nodes.indexOf(orphan) + if (index !== -1) graph._nodes.splice(index, 1) + if (graph._nodes_by_id[orphan.id] === orphan) { + delete graph._nodes_by_id[orphan.id] } + for (const canvas of graph.list_of_graphcanvas ?? []) { + delete canvas.selected_nodes[orphan.id] + canvas.deselect(orphan) + } + orphan.graph = null + orphan._graphScope = undefined + graph.onNodeRemoved?.(orphan) + graph.events.dispatch('node:removed', { node: orphan }) +} - return materialized +/** Same placeholder `LGraph.configure()` builds for an unregistered type. */ +function missingNode(state: NodeState): LGraphNode { + const node = new LGraphNode( + state.title || state.type || 'Missing Node', + state.type + ) + node.has_errors = true + return node +} + +/** + * Op-layer serialisations carry widget values keyed by name; `configure()` + * only reads name-keyed values from `widgets_values_named`. + */ +function withNamedWidgetValues(serialised: ISerialisedNode): ISerialisedNode { + const values = serialised.widgets_values + if ( + values === undefined || + Array.isArray(values) || + serialised.widgets_values_named !== undefined + ) { + return serialised + } + return { ...serialised, widgets_values_named: values } } diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index aaafc9f330e..fb259d6df01 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -826,8 +826,8 @@ describe('EcsFollowerAdapter integration', () => { }) }) - it('materializes an agent add_node into the ECS store and layout port only (qa-59 companion)', () => { - // Companion to `drops an agent-added node from serialize() ... (qa-59)` + it('materializes an agent add_node into the ECS store and layout port only', () => { + // Companion to `does NOT drop an agent-added node from serialize() ...` // in `src/lib/litegraph/src/LGraph.test.ts`. This drives the real // applier + follower + adapter chain and pins the two side effects this // layer owns for a remote `add_node`: the node state is registered in diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index ffdc1bb4a67..6f72c7f37fe 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -118,15 +118,6 @@ interface TargetSession { onLinksChanged: (event: Y.YMapEvent) => void reconcileNextFrame: boolean applying: boolean - /** - * Node ids this session's mutation store learned about (added or - * reconciled-in) in the most recently committed `applyQueuedFrame` call. - * Plain string ids only — no LiteGraph/DOM dependency — so the pure op - * layer stays portable; a consumer (e.g. the FE-only follower composable) - * uses this to know which store-only nodes may still lack a live LGraph - * adapter, without this layer knowing LGraph exists. - */ - lastAddedNodeIds: Set } /** @@ -165,26 +156,15 @@ export class EcsFollowerAdapter { if (session.applying) return true session.applying = true let updateCommitted = false - // Accumulate across every frame this call drains, not just the frame - // that triggered it: `applyQueuedFrame` sets `session.lastAddedNodeIds` - // per-frame, and a caller (the qa-59 materializer) only reads it once - // after `applyFrame` returns, so an earlier frame's additions in the - // same drain must not be clobbered by a later frame's (possibly empty) - // set. Reset only here, at the start of the drain, never inside the loop. - const drainedAddedNodeIds = new Set() try { while (session.frameQueue.length > 0) { const frame = session.frameQueue.shift() if (!frame) continue const committed = this.applyQueuedFrame(session, frame) - if (committed) { - for (const id of session.lastAddedNodeIds) drainedAddedNodeIds.add(id) - } if (frame === update) updateCommitted = committed } } finally { session.applying = false - session.lastAddedNodeIds = drainedAddedNodeIds } return updateCommitted } @@ -194,7 +174,6 @@ export class EcsFollowerAdapter { const session = this.targets.get(workflowId) if (!session) return false this.discardSessionPending(session) - session.lastAddedNodeIds = new Set() return session.mutations.clearSemanticGraph(context) } @@ -227,7 +206,6 @@ export class EcsFollowerAdapter { frameQueue: [], reconcileNextFrame: true, applying: false, - lastAddedNodeIds: new Set(), onNodesChanged: (_events): void => undefined, onLinksChanged: (_event): void => undefined } @@ -268,7 +246,6 @@ export class EcsFollowerAdapter { const removedLinkIds = [...changedLinkIds].flatMap((id) => session.links.has(id) ? [] : [Number(id)] ) - const addedNodeIds = new Set() const committed = session.mutations.batch(frameContext(update), (batch) => { if (reconcile) { const nodes = [...session.nodes.keys()].flatMap((id) => { @@ -283,10 +260,7 @@ export class EcsFollowerAdapter { nodes.map(({ id }) => toNodeId(id)), links.map(({ id }) => id) ) - for (const payload of nodes) { - batch.reconcileNode(payload) - addedNodeIds.add(String(payload.id)) - } + for (const payload of nodes) batch.reconcileNode(payload) for (const link of links) batch.connect(link) return } @@ -301,7 +275,6 @@ export class EcsFollowerAdapter { const payload = readSemanticNode(session.follower.doc, id) if (!payload) continue batch.addNode(payload) - addedNodeIds.add(id) } for (const id of replacedWidgetMaps) { if (nodeActions.has(id)) continue @@ -333,26 +306,10 @@ export class EcsFollowerAdapter { // reconcileNextFrame set so the next frame retries authoritative // cleanup instead of falling through to incremental handling with // stale local-only graph state still present. - if (committed) { - session.reconcileNextFrame = false - session.lastAddedNodeIds = addedNodeIds - } else { - session.lastAddedNodeIds = new Set() - } + if (committed) session.reconcileNextFrame = false return committed } - /** - * Node ids the given session's mutation store learned about (added or - * reconciled-in) on the most recently committed frame. Consumed by the - * FE-only follower composable (qa-59) to materialize a live LGraph - * adapter for agent-added nodes; empty when no session is bound or the - * last frame committed nothing. - */ - lastAddedNodeIds(workflowId: string): ReadonlySet { - return this.targets.get(workflowId)?.lastAddedNodeIds ?? new Set() - } - private discardSessionPending(session: TargetSession): void { session.nodeActions.clear() session.changedWidgets.clear() diff --git a/src/workbench/extensions/agent/crdt/mintPortWiring.ts b/src/workbench/extensions/agent/crdt/mintPortWiring.ts index 14c90046328..a9c3eac0169 100644 --- a/src/workbench/extensions/agent/crdt/mintPortWiring.ts +++ b/src/workbench/extensions/agent/crdt/mintPortWiring.ts @@ -70,6 +70,21 @@ export function notifyMintPortsAfterGraphConfigure(): void { for (const wiring of activeWirings) wiring.onAfterGraphConfigure() } +/** + * Run a graph mutation that replays already-committed remote state (so the + * live graph catches up with the stores) without any active mint port echoing + * it back into the doc as a local op. + */ +export function runMintPortsSuppressed(fn: () => T): T { + const wirings = [...activeWirings] + for (const wiring of wirings) wiring.session.beginGraphTeardown() + try { + return fn() + } finally { + for (const wiring of wirings) wiring.session.endGraphTeardown() + } +} + /** Run a confirmed root-workflow clear through every active mint port. */ export function runMintPortsIntentionalClear(clear: () => T): T { const wirings = [...activeWirings] diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index 5448b07e48f..e557ad489a3 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -8,12 +8,14 @@ */ import { createPinia, setActivePinia } from 'pinia' import { beforeEach, describe, expect, it, vi } from 'vitest' -import { defineComponent, nextTick, ref } from 'vue' +import { defineComponent, nextTick, ref, shallowRef } from 'vue' import type { Ref } from 'vue' import { render } from '@testing-library/vue' import type { GraphMutations } from '@/core/graph/graphMutations' +import type { NodeId } from '@/types/nodeId' +import { toNodeId } from '@/types/nodeId' import type { MaterializableGraph } from './agentNodeMaterializer' @@ -48,12 +50,11 @@ const adapterState = vi.hoisted(() => ({ applyFrame: vi.fn(() => true), clearForReset: vi.fn(), discardPending: vi.fn(), - destroy: vi.fn(), - lastAddedNodeIds: vi.fn(() => new Set()) + destroy: vi.fn() })) const materializerState = vi.hoisted(() => ({ - materializeMissingAdapters: vi.fn(() => [] as string[]) + reconcileAgentAdapters: vi.fn(() => [] as NodeId[]) })) const apiState = vi.hoisted(() => { @@ -98,12 +99,11 @@ vi.mock('./ecsFollowerAdapter', () => ({ clearForReset = adapterState.clearForReset discardPending = adapterState.discardPending destroy = adapterState.destroy - lastAddedNodeIds = adapterState.lastAddedNodeIds } })) vi.mock('./agentNodeMaterializer', () => ({ - materializeMissingAdapters: materializerState.materializeMissingAdapters + reconcileAgentAdapters: materializerState.reconcileAgentAdapters })) vi.mock('./devPanelLog', () => ({ @@ -192,8 +192,7 @@ describe('useAgentCrdtFollower', () => { setActivePinia(createPinia()) sessionStorage.clear() bridgeState.current = null - adapterState.lastAddedNodeIds.mockReset().mockReturnValue(new Set()) - materializerState.materializeMissingAdapters.mockReset().mockReturnValue([]) + materializerState.reconcileAgentAdapters.mockReset().mockReturnValue([]) }) it('subscribes immediately to a bound workflow and reports it in status', () => { @@ -631,86 +630,83 @@ describe('useAgentCrdtFollower', () => { }) }) - it('qa-59: materializes a live adapter for nodes the adapter just added, given a graph', () => { - const fakeGraph = { - id: 'root', - rootGraph: { id: 'root' }, - getNodeById: () => null, - add: () => null - } satisfies MaterializableGraph - adapterState.lastAddedNodeIds.mockReturnValue(new Set(['1'])) - materializerState.materializeMissingAdapters.mockReturnValue(['1']) + describe('live-graph reconcile', () => { + // The materializer is module-mocked, so the graph only needs to be a + // distinct reference the composable hands through. + const fakeGraph = {} as MaterializableGraph - const { unmount } = mountFollower('wf-1', true, () => fakeGraph) + it('reconciles the live graph after every applied frame', () => { + const { unmount } = mountFollower('wf-1', true, () => fakeGraph) - dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) - expect(adapterState.lastAddedNodeIds).toHaveBeenCalledWith('wf-1') - expect(materializerState.materializeMissingAdapters).toHaveBeenCalledWith( - fakeGraph, - new Set(['1']) - ) - unmount() - }) + expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledTimes(1) + expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledWith( + fakeGraph + ) + unmount() + }) - it('qa-59: skips materialization when no graph is available yet', () => { - adapterState.lastAddedNodeIds.mockReturnValue(new Set(['1'])) + it('skips the reconcile while no graph exists', () => { + // Default getGraph (no override) always returns null — mirrors the + // panel mounting before the root graph exists. + const { unmount } = mountFollower('wf-1') - // Default getGraph (no override) always returns null — mirrors the - // panel mounting before app.isGraphReady. - const { unmount } = mountFollower('wf-1') + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) - dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + expect(materializerState.reconcileAgentAdapters).not.toHaveBeenCalled() + unmount() + }) - expect(materializerState.materializeMissingAdapters).not.toHaveBeenCalled() - unmount() - }) + it('reconciles once the graph appears, without waiting for another frame', async () => { + const graph = shallowRef(null) + const { unmount } = mountFollower('wf-1', true, () => graph.value) - it('qa-59/DrJKL: retries a pending id once a graph becomes available, without another add', () => { - const fakeGraph = { - id: 'root', - rootGraph: { id: 'root' }, - getNodeById: () => null, - add: () => null - } satisfies MaterializableGraph - let graph: MaterializableGraph | null = null - adapterState.lastAddedNodeIds.mockReturnValue(new Set(['1'])) - - const { unmount } = mountFollower('wf-1', true, () => graph) - - // First frame: added while no graph is bound yet — must not be dropped. - dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) - expect(materializerState.materializeMissingAdapters).not.toHaveBeenCalled() - - // Graph becomes available; a later frame carries no new adds, but the - // pending id from the earlier frame must still be retried. - graph = fakeGraph - adapterState.lastAddedNodeIds.mockReturnValue(new Set()) - materializerState.materializeMissingAdapters.mockReturnValue(['1']) - dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 10 }) - - expect(materializerState.materializeMissingAdapters).toHaveBeenCalledWith( - fakeGraph, - new Set(['1']) - ) - unmount() - }) + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + expect(materializerState.reconcileAgentAdapters).not.toHaveBeenCalled() - it('qa-59: skips materialization when the frame added no new nodes', () => { - const fakeGraph = { - id: 'root', - rootGraph: { id: 'root' }, - getNodeById: () => null, - add: () => null - } satisfies MaterializableGraph - adapterState.lastAddedNodeIds.mockReturnValue(new Set()) + graph.value = fakeGraph + await nextTick() - const { unmount } = mountFollower('wf-1', true, () => fakeGraph) + expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledTimes(1) + expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledWith( + fakeGraph + ) + unmount() + }) - dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + it('does not reconcile for a graph that appears while the target is inactive', async () => { + const graph = shallowRef(null) + const { unmount } = mountFollower('wf-1', false, () => graph.value) - expect(materializerState.materializeMissingAdapters).not.toHaveBeenCalled() - unmount() + graph.value = fakeGraph + await nextTick() + + expect(materializerState.reconcileAgentAdapters).not.toHaveBeenCalled() + unmount() + }) + + it('records a dev event only when nodes were materialized', async () => { + const { recordDevEvent } = await import('./devPanelLog') + const { unmount } = mountFollower('wf-1', true, () => fakeGraph) + + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + materializerState.reconcileAgentAdapters.mockReturnValue([toNodeId(1)]) + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 10 }) + + const materializedEvents = vi + .mocked(recordDevEvent) + .mock.calls.filter( + ([event]) => event === 'agent_node_adapters_materialized' + ) + expect(materializedEvents).toEqual([ + [ + 'agent_node_adapters_materialized', + { workflowId: 'wf-1', nodeIds: [toNodeId(1)] } + ] + ]) + unmount() + }) }) it('suspends a background target and catches up only after it becomes active', async () => { diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 94a5f08a4b8..42801d10aed 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -6,7 +6,7 @@ import type { RemoteMutationContext } from '@/types/graphMutationContext' import { createUuidv4 } from '@/utils/uuid' import type { MaterializableGraph } from './agentNodeMaterializer' -import { materializeMissingAdapters } from './agentNodeMaterializer' +import { reconcileAgentAdapters } from './agentNodeMaterializer' import { recordDevEvent } from './devPanelLog' import type { DocFrameTransport, DocUpdate } from './docFrameClient' import { DocFrameClient } from './docFrameClient' @@ -191,11 +191,11 @@ export function useAgentCrdtFollower( graphMutations: MutationsForTarget, userId: () => string | null = () => null, isTargetActive: Ref = ref(true), - // qa-59: the live LGraph the composable materializes agent-added node - // adapters into. Optional (defaults to "no graph available") so existing - // callers/tests that don't yet care about the live-adapter bridge keep - // compiling; `AgentPanelRoot.vue` wires the real `app.rootGraph` the same - // way `mintPortWiring`'s `getGraph` already does. + /** + * Live graph that receives node adapters for store-only records. Reactive + * reads inside the getter are tracked, so a `null` → graph flip triggers a + * reconcile without waiting for the next remote frame. + */ getGraph: () => MaterializableGraph | null = () => null ) { const connected = ref(false) @@ -211,15 +211,6 @@ export function useAgentCrdtFollower( reset: 0, dropped: 0 }) - // qa-59 / DrJKL: ids this session has seen added but not yet materialized — - // either because `getGraph()` returned null on their frame (panel bound - // before `app.isGraphReady`), or `materializeMissingAdapters` skipped them - // (unregistered type, etc). Accumulates across frames instead of being - // replaced, so a node added before the graph existed is retried once one - // becomes available rather than being dropped from every future check — - // `materializeMissingAdapters` itself is idempotent for ids that already - // have a live adapter or that the store no longer has. - const pendingMaterializeIds = new Set() // Dev-panel tap (poc-4): log every outbound frame with its delivery result. // Wraps locally instead of modifying the exported apiTransport, whose @@ -416,30 +407,7 @@ export function useAgentCrdtFollower( outcomes.value = applied ? { ...outcomes.value, applied: outcomes.value.applied + 1 } : { ...outcomes.value, skipped: outcomes.value.skipped + 1 } - // qa-59: the op layer above only ever wrote the ECS stores (KEEP-ALIVE - // #3 — it stays litegraph-free), so a remote add_node still has no - // `LGraphNode` adapter at this point. Materialize one for anything the - // adapter just landed, so the node enters `LGraph._nodes` and survives - // serialize()/save. Ids from this frame join any still-pending ids from - // earlier frames (see `pendingMaterializeIds` above) so an add that - // arrived before a graph existed is retried here, not dropped. - const addedIds = adapter.lastAddedNodeIds(update.workflowId) - for (const id of addedIds) pendingMaterializeIds.add(id) - const graph = getGraph() - if (graph && pendingMaterializeIds.size > 0) { - // Snapshot before calling out — `materializeMissingAdapters` is a pure - // read of whatever set it's given, and mutating `pendingMaterializeIds` - // in place afterward must not retroactively change what was passed. - const attempted = new Set(pendingMaterializeIds) - const materialized = materializeMissingAdapters(graph, attempted) - if (materialized.length > 0) { - for (const id of materialized) pendingMaterializeIds.delete(id) - recordDevEvent('agent_node_adapters_materialized', { - workflowId: update.workflowId, - nodeIds: materialized - }) - } - } + reconcileLiveGraph(update.workflowId) recordDevEvent('doc_update', { workflowId: update.workflowId, seq: update.seq, @@ -600,6 +568,24 @@ export function useAgentCrdtFollower( // (a REAL detach, e.g. new chat — drop the persisted id too). let initialBind = true let boundWorkflowId: string | null = null + // The op layer writes remote frames to the stores only; the live graph + // catches up here, after each applied frame and once a graph exists. + function reconcileLiveGraph(docId: string): void { + const graph = getGraph() + if (!graph) return + const nodeIds = reconcileAgentAdapters(graph) + if (nodeIds.length > 0) { + recordDevEvent('agent_node_adapters_materialized', { + workflowId: docId, + nodeIds + }) + } + } + watch(getGraph, (graph) => { + if (graph && boundWorkflowId !== null && isTargetActive.value) { + reconcileLiveGraph(boundWorkflowId) + } + }) // Drive the bridge's intent, then give the sender the same eager signal the // refusal branch gets: `reconcile()` clears send reality synchronously when // the desired doc changes, and a batch minted for the old doc would @@ -616,18 +602,11 @@ export function useAgentCrdtFollower( clearStaleProbe() connected.value = false knownDocNodeIds = new Set() - // qa-59 / coderabbitai (discussion_r3920899129): ids pending - // materialization are scoped to `boundWorkflowId`'s doc — carrying them - // across a follower-target change would materialize a PRIOR doc's - // leftover ids against the NEW target's graph/store. Clear alongside - // every `adapter.unbind` below, at the same point the binding itself - // invalidates. if (!active) { if (next !== null) initialBind = false if (boundWorkflowId !== null) { adapter.unbind(boundWorkflowId) boundWorkflowId = null - pendingMaterializeIds.clear() } subscribedWorkflowId.value = null retarget(null) @@ -639,10 +618,7 @@ export function useAgentCrdtFollower( if (persisted !== null) { recordDevEvent('rebind', { workflowId: persisted }) if (boundWorkflowId !== persisted) { - if (boundWorkflowId !== null) { - adapter.unbind(boundWorkflowId) - pendingMaterializeIds.clear() - } + if (boundWorkflowId !== null) adapter.unbind(boundWorkflowId) adapter.bind(persisted, bridge.follower) boundWorkflowId = persisted } @@ -654,7 +630,6 @@ export function useAgentCrdtFollower( if (boundWorkflowId !== null) { adapter.unbind(boundWorkflowId) boundWorkflowId = null - pendingMaterializeIds.clear() } subscribedWorkflowId.value = null retarget(null) @@ -662,10 +637,7 @@ export function useAgentCrdtFollower( } initialBind = false if (boundWorkflowId !== next) { - if (boundWorkflowId !== null) { - adapter.unbind(boundWorkflowId) - pendingMaterializeIds.clear() - } + if (boundWorkflowId !== null) adapter.unbind(boundWorkflowId) adapter.bind(next, bridge.follower) boundWorkflowId = next } From 599f66b0c849ec76a03d9a3ba00e44c05e0ccb50 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 3 Sep 2026 04:07:31 -0700 Subject: [PATCH 14/19] test(agent): reconcile only applied frames; prove layout survives add rollback Skipped frames (no bound session) leave the stores untouched, so the live-graph reconcile now runs only when the adapter applied the frame. Add tests showing that when `onAdded()` throws inside `graph.add()` the remote layout entry survives the rollback and a later reconcile materializes the node without echoing a local add through mint ports. Document that reconcile is root-scope only, matching where the op layer applies remote operations. --- .../agent/crdt/agentNodeMaterializer.test.ts | 20 +++++++++++++++++++ .../agent/crdt/agentNodeMaterializer.ts | 4 ++++ .../agent/crdt/useAgentCrdtFollower.test.ts | 11 ++++++++++ .../agent/crdt/useAgentCrdtFollower.ts | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index 10c93538035..d38aea3489a 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -344,6 +344,12 @@ describe('reconcileAgentAdapters', () => { ).toBeDefined() expect(graph._nodes).toHaveLength(0) expect(graph.getNodeById(toNodeId(1))).toBeFalsy() + // The remote layout entry was written before add() ran and the throw + // happens before `LGraph.add()` attaches its own layout, so rollback + // must leave it in place for the retry. + expect( + layoutStore.getNodeLayout(scope.rootGraphId, toNodeId(1)) + ).toBeDefined() }) it('retries a failed add on the next reconcile', () => { @@ -438,6 +444,20 @@ describe('reconcileAgentAdapters', () => { expect(minted).toEqual([]) }) + it('retries after onAdded() throws without echoing a local add', async () => { + seedAgentAddedNode(graph, 1, 'throws-on-added') + + expect(reconcileAgentAdapters(graph)).toEqual([]) + vi.spyOn(ThrowsOnAddedNode.prototype, 'onAdded').mockImplementation( + () => {} + ) + expect(reconcileAgentAdapters(graph)).toEqual([toNodeId(1)]) + await settle() + + expect(graph._nodes).toHaveLength(1) + expect(minted).toEqual([]) + }) + it('still mints a later local widget edit on the materialized node', async () => { const scope = graphScopeOf(graph) remoteMutations(scope).addNode( diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index a564ec4f9ae..b653cba5946 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -38,6 +38,10 @@ export type MaterializableGraph = Pick< * remote delete leaves a node bound to nothing. All three are derived from * store state here, without the op layer telling us which ids changed. * + * Only the scope of `graph` is reconciled. The follower passes the root graph, + * which matches the op layer: remote operations are applied against the root + * scope, so subgraph-owned nodes are neither adopted nor detached here. + * * @returns ids that received a new live node. */ export function reconcileAgentAdapters(graph: MaterializableGraph): NodeId[] { diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index e557ad489a3..6f153af1701 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -647,6 +647,17 @@ describe('useAgentCrdtFollower', () => { unmount() }) + it('does not reconcile after a frame the adapter skipped', () => { + adapterState.applyFrame.mockReturnValueOnce(false) + const { unmount, status } = mountFollower('wf-1', true, () => fakeGraph) + + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + + expect(status().outcomes.skipped).toBe(1) + expect(materializerState.reconcileAgentAdapters).not.toHaveBeenCalled() + unmount() + }) + it('skips the reconcile while no graph exists', () => { // Default getGraph (no override) always returns null — mirrors the // panel mounting before the root graph exists. diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 42801d10aed..098d6893f2a 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -407,7 +407,7 @@ export function useAgentCrdtFollower( outcomes.value = applied ? { ...outcomes.value, applied: outcomes.value.applied + 1 } : { ...outcomes.value, skipped: outcomes.value.skipped + 1 } - reconcileLiveGraph(update.workflowId) + if (applied) reconcileLiveGraph(update.workflowId) recordDevEvent('doc_update', { workflowId: update.workflowId, seq: update.seq, From f873f899f8d4732c6c72646377821e6b086504a2 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 3 Sep 2026 12:31:28 +0000 Subject: [PATCH 15/19] fix(agent): materialize remote link adapters --- src/lib/litegraph/src/LLink.ts | 36 +++++ .../agent/crdt/agentNodeMaterializer.test.ts | 152 ++++++++++++++++++ .../agent/crdt/agentNodeMaterializer.ts | 19 ++- 3 files changed, 203 insertions(+), 4 deletions(-) diff --git a/src/lib/litegraph/src/LLink.ts b/src/lib/litegraph/src/LLink.ts index 23d360de302..23c6f50ad47 100644 --- a/src/lib/litegraph/src/LLink.ts +++ b/src/lib/litegraph/src/LLink.ts @@ -52,6 +52,42 @@ export function resolveLinkTopology(topology: LinkTopology): LLink | undefined { return linkByTopology.get(toRaw(topology)) } +/** + * Gives a topology that was registered directly by a renderer-free store + * mutation its live LiteGraph facade. The store remains the identity owner; + * this only installs the adapter used by graph lookup, painting, and link + * interactions. + */ +export function materializeLinkAdapter( + graph: Pick, + topology: LinkTopology +): LLink | undefined { + const rawTopology = toRaw(topology) + const existing = linkByTopology.get(rawTopology) + if (existing) return existing + + const scope = graphScopeOf(graph) + const registered = useLinkStore().getTopology(scope.rootGraphId, topology.id) + if ( + registered?.graphId !== scope.owningGraphId || + toRaw(registered) !== rawTopology + ) { + return + } + + const link = new LLink( + topology.id, + topology.type, + serializeNodeId(topology.originNodeId), + topology.originSlot, + serializeNodeId(topology.targetNodeId), + topology.targetSlot, + topology.parentId + ) + adoptLinkTopology(link, scope, registered) + return link +} + function defineEnumerableTopologyFacade(link: LLink): void { topologyFacadeDescriptors ??= [ 'id', diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index d38aea3489a..d2d3705dcda 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -1,6 +1,14 @@ +import { + applyOps, + linksMap, + mint, + nodesMap +} from '@comfyorg/comfy-multi-player' +import type { WidgetCatalog } from '@comfyorg/comfy-multi-player' import { createTestingPinia } from '@pinia/testing' import { setActivePinia } from 'pinia' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import * as Y from 'yjs' import { createGraphMutations } from '@/core/graph/graphMutations' import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' @@ -11,15 +19,19 @@ import { reportError } from '@/platform/telemetry/reportError' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' // eslint-disable-next-line import-x/no-restricted-paths import { LayoutSource } from '@/renderer/core/layout/types' +import { useLinkStore } from '@/stores/linkStore' import { useNodeDataStore } from '@/stores/nodeDataStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { GraphScope } from '@/types/graphScopeId' import { graphScopeOf } from '@/types/graphScopeId' import type { RemoteMutationContext } from '@/types/graphMutationContext' +import { toLinkId } from '@/types/linkId' import { toNodeId } from '@/types/nodeId' import { widgetId } from '@/types/widgetId' import { reconcileAgentAdapters } from './agentNodeMaterializer' +import { EcsFollowerAdapter } from './ecsFollowerAdapter' +import { FollowerDoc } from './followerDoc' import type { GraphOperation } from './graphOperations' import { attachMintPortWiring } from './mintPortWiring' import type { MintPortWiring } from './mintPortWiring' @@ -56,6 +68,19 @@ const REMOTE: RemoteMutationContext = { actor: 'agent:test', opId: 'op-test' } +const CATALOG: WidgetCatalog = { + types: { dummy: { widget_order: [] } } +} + +function agentOperation(id: string, version: number, payload: object) { + return { + op_id: id, + actor: 'agent:test', + base_version: version, + stamp: [version, 'agent:test', id], + ...payload + } +} /** * Same layout port the agent panel wires in production: remote adds create @@ -135,6 +160,133 @@ beforeEach(() => { }) describe('reconcileAgentAdapters', () => { + it('converges create, connect, save/reload, readback, and delete across every graph surface', () => { + const graph = new LGraph() + const scope = graphScopeOf(graph) + const host = mint({ nodes: [], links: [] }, CATALOG) + const follower = new FollowerDoc() + const adapter = new EcsFollowerAdapter(remoteMutations(scope)) + adapter.bind('workflow', follower) + + let sequence = 0 + let initialFrame = true + const deliver = (payload: object) => { + const stateVector = Y.encodeStateVector(host) + const opId = `agent-op-${++sequence}` + const result = applyOps( + host, + [agentOperation(opId, sequence, payload)] as Parameters< + typeof applyOps + >[1], + CATALOG + ) + expect(result.outcomes).toEqual([{ op_id: opId, outcome: 'applied' }]) + + const update = initialFrame + ? Y.encodeStateAsUpdate(host) + : Y.encodeStateAsUpdate(host, stateVector) + initialFrame = false + follower.applyRemoteUpdate(update) + expect( + adapter.applyFrame({ + workflowId: 'workflow', + seq: sequence, + update, + actor: 'agent:test', + opIds: [opId] + }) + ).toBe(true) + reconcileAgentAdapters(graph) + } + + deliver({ + op: 'add_node', + node_id: 1, + class_type: 'dummy', + pos: [10, 20], + node: { + id: 1, + type: 'dummy', + pos: [10, 20], + size: [100, 80], + inputs: [], + outputs: [{ name: 'image', type: 'IMAGE', links: [] }] + } + }) + deliver({ + op: 'add_node', + node_id: 2, + class_type: 'dummy', + pos: [300, 20], + node: { + id: 2, + type: 'dummy', + pos: [300, 20], + size: [100, 80], + inputs: [{ name: 'image', type: 'IMAGE', link: null }], + outputs: [] + } + }) + deliver({ + op: 'connect', + link_id: 9, + from_node: 1, + from_slot: 0, + to_node: 2, + to_slot: 0, + link_type: 'IMAGE' + }) + + const source = graph.getNodeById(toNodeId(1)) + const target = graph.getNodeById(toNodeId(2)) + expect(source).toBeTruthy() + expect(target).toBeTruthy() + target?.updateArea() + expect(graph.getNodeOnPos(350, 60)).toBe(target) + expect( + useNodeDataStore() + .getGraphNodesFor(scope.rootGraphId, scope.owningGraphId) + .map(({ id }) => id) + ).toEqual(graph._nodes.map(({ id }) => id)) + + const topology = useLinkStore().getInputSlotLink(scope, toNodeId(2), 0) + expect(topology?.id).toBe(toLinkId(9)) + expect(graph.getLink(toLinkId(9))).toMatchObject({ + origin_id: toNodeId(1), + target_id: toNodeId(2) + }) + expect(nodesMap(follower.doc).has('2')).toBe(true) + expect(linksMap(follower.doc).has('9')).toBe(true) + + const saved = structuredClone(graph.asSerialisable({ sortNodes: true })) + expect(saved.nodes.map(({ id }) => id)).toEqual([1, 2]) + expect(saved.links?.map(({ id }) => id)).toEqual([toLinkId(9)]) + + graph.configure(saved) + expect(graph.getNodeById(toNodeId(1))).toBeTruthy() + expect(graph.getNodeById(toNodeId(2))).toBeTruthy() + expect(graph.getLink(toLinkId(9))).toBeTruthy() + + deliver({ op: 'delete_node', node_id: 2, removed_links: [9] }) + + expect(nodesMap(follower.doc).has('2')).toBe(false) + expect(linksMap(follower.doc).has('9')).toBe(false) + expect(graph.getNodeById(toNodeId(1))).toBeTruthy() + expect(graph.getNodeById(toNodeId(2))).toBeFalsy() + expect(graph.getLink(toLinkId(9))).toBeUndefined() + expect( + useNodeDataStore() + .getGraphNodesFor(scope.rootGraphId, scope.owningGraphId) + .map(({ id }) => id) + ).toEqual(graph._nodes.map(({ id }) => id)) + expect(graph.asSerialisable().nodes.map(({ id }) => id)).toEqual([1]) + expect(graph.asSerialisable().links).toBeUndefined() + + adapter.destroy() + follower.destroy() + host.destroy() + }) + describe('store record without a live node', () => { it('gives the record a live node under its own id', () => { const graph = new LGraph() diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index b653cba5946..8bf82fee345 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -1,7 +1,9 @@ import type { LGraph } from '@/lib/litegraph/src/LGraph' +import { materializeLinkAdapter } from '@/lib/litegraph/src/LLink' import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' import { reportError } from '@/platform/telemetry/reportError' +import { useLinkStore } from '@/stores/linkStore' import { useNodeDataStore } from '@/stores/nodeDataStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { GraphScope } from '@/types/graphScopeId' @@ -29,14 +31,16 @@ export type MaterializableGraph = Pick< > /** - * Bring the live graph's node adapters in line with the node records the op - * layer already committed to the stores. + * Bring the live graph's node and link adapters in line with the records the + * op layer already committed to the stores. * * Remote frames mutate the stores only. A remote add leaves a record with no * live node; a remote update re-registers the record under a new state object, * so the node that was bound to the old state no longer owns anything; a - * remote delete leaves a node bound to nothing. All three are derived from - * store state here, without the op layer telling us which ids changed. + * remote delete leaves a node bound to nothing. A remote connect likewise + * registers topology without constructing its live `LLink` facade. All four + * are derived from store state here, without the op layer telling us which ids + * changed. * * Only the scope of `graph` is reconciled. The follower passes the root graph, * which matches the op layer: remote operations are applied against the root @@ -50,6 +54,13 @@ export function reconcileAgentAdapters(graph: MaterializableGraph): NodeId[] { function reconcile(graph: MaterializableGraph): NodeId[] { const scope = graphScopeOf(graph) + // Remote connect registers canonical topology without importing LiteGraph. + // Install its facade before node.configure() can query links; otherwise the + // occupied input is real in the store but graph lookup and painting miss it. + for (const topology of useLinkStore().graphTopologies(scope)) { + materializeLinkAdapter(graph, topology) + } + const nodeStore = useNodeDataStore() const records = nodeStore.getGraphNodesFor( scope.rootGraphId, From 5699bd17766166c4654614261440e3e639398b02 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 3 Sep 2026 09:29:38 -0700 Subject: [PATCH 16/19] fix(agent-crdt): keep materialize rollback atomic when cleanup throws `LGraph.remove()` runs `onRemoved()` uncaught. If `graph.add()` attached the node and then `onAdded()` threw, an extension whose `onRemoved()` also throws made the rollback escape before `restore()`: the store record stayed deleted and the partial adapter stayed live, which is worse than either failure alone. Take the node back out best-effort, always run `restore()`, and report the cleanup failure separately as `agent_node_materialize_rollback_failed`. `ThrowsOnAddedNode` gains a no-op `onRemoved()` so a test can spy on it; `LGraphNode.onRemoved` is optional and absent from the prototype otherwise. Addresses https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3926451582 --- .../agent/crdt/agentNodeMaterializer.test.ts | 44 +++++++++++++++++++ .../agent/crdt/agentNodeMaterializer.ts | 22 +++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index d2d3705dcda..f30d5a93467 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -61,6 +61,11 @@ class ThrowsOnAddedNode extends LGraphNode { override onAdded(): void { throw new Error('extension code blew up in onAdded') } + + // Declared (as a no-op) so a test can make the rollback's own cleanup throw. + // `LGraphNode.onRemoved` is optional, so it is absent from the prototype and + // cannot be spied on otherwise. + override onRemoved(): void {} } const REMOTE: RemoteMutationContext = { @@ -610,6 +615,45 @@ describe('reconcileAgentAdapters', () => { expect(minted).toEqual([]) }) + it('restores the record when the rollback cleanup itself throws', () => { + // `LGraph.remove()` runs `onRemoved()` uncaught, so an extension that + // throws on BOTH halves of the lifecycle used to escape the rollback + // before `restore()`, leaving the record deleted with a partial adapter + // still live -- worse than either failure alone. Cleanup is best-effort + // now; putting the authoritative record back is not. + const scope = seedAgentAddedNode(graph, 1, 'throws-on-added') + const onRemoved = vi + .spyOn(ThrowsOnAddedNode.prototype, 'onRemoved') + .mockImplementation(() => { + throw new Error('extension code blew up in onRemoved') + }) + + expect(() => reconcileAgentAdapters(graph)).not.toThrow() + expect(onRemoved).toHaveBeenCalled() + + // The record the rollback deleted is registered again, so the store still + // owns the node the agent added. + const state = useNodeDataStore().getNode(scope.rootGraphId, toNodeId(1)) + expect(state).toBeDefined() + expect(useNodeDataStore().ownsNode(scope, state!)).toBe(true) + + // Both failures are reported: the original `onAdded` throw, and the + // cleanup that could not complete. + expect(reportError).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + errorType: 'agent_node_materialize_add_failed' + }) + ) + expect(reportError).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + errorType: 'agent_node_materialize_rollback_failed' + }) + ) + expect(minted).toEqual([]) + }) + it('still mints a later local widget edit on the materialized node', async () => { const scope = graphScopeOf(graph) remoteMutations(scope).addNode( diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index 8bf82fee345..ab25ddd6d2e 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -136,12 +136,32 @@ function materialize( const rollback = (cause: unknown) => { // `add()` may throw after attaching (from `onAdded`); only then is there // a live node to take back out. - if (graph._nodes_by_id[node.id] === node) graph.remove(node) + // + // Taking it out is best-effort and `restore()` is not: `LGraph.remove()` + // runs `onRemoved()` uncaught, so an extension that throws on both halves + // of the lifecycle would otherwise escape here and strand the records this + // function deleted -- the store record gone and a partial adapter live, + // which is worse than either failure alone. Put the authoritative state + // back first and report the cleanup failure separately. + let cleanupCause: unknown + let cleanupFailed = false + try { + if (graph._nodes_by_id[node.id] === node) graph.remove(node) + } catch (error) { + cleanupCause = error + cleanupFailed = true + } restore() reportError(cause, { errorType: 'agent_node_materialize_add_failed', context: { graphId: graph.id, nodeId: String(state.id) } }) + if (cleanupFailed) { + reportError(cleanupCause, { + errorType: 'agent_node_materialize_rollback_failed', + context: { graphId: graph.id, nodeId: String(state.id) } + }) + } return false } From 71bd5ecab0cabd149cb6d0fce5bc6a33ff29b84c Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 3 Sep 2026 09:30:07 -0700 Subject: [PATCH 17/19] fix(agent-crdt): reconcile live adapters after a reset and on activation Two readiness paths left stale live adapters standing until some later document frame happened to arrive. Both are observable in a save, since the live adapters are what gets serialised. `clearForReset` (from `doc_reset` and `follower_replaced`) empties the stores only, and `reconcileAgentAdapters()` ran solely after an applied `doc_update` or a `getGraph` identity change, so pre-reset nodes survived the lineage break. Reconcile after each successful clear. The graph-ready-then-activated ordering was also uncovered. `getGraph` does not change when activity flips, so the readiness watcher never re-fires, and adding `isTargetActive` as a source to it would not help: it is created before the binding watcher and would run first, still seeing `boundWorkflowId === null`. Reconcile at the bind site instead, gated on the inactive->active edge so a plain mount or retarget keeps its existing behaviour. Addresses https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3926451598 Addresses https://github.com/Comfy-Org/ComfyUI_frontend/pull/16652#discussion_r3926451601 --- .../agent/crdt/useAgentCrdtFollower.test.ts | 55 +++++++++++++++++++ .../agent/crdt/useAgentCrdtFollower.ts | 26 ++++++++- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index 6f153af1701..05ff4210b7a 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -697,6 +697,61 @@ describe('useAgentCrdtFollower', () => { unmount() }) + it('reconciles when the target is activated after the graph became ready', async () => { + // The other readiness ordering: the graph arrives while inactive, so the + // `getGraph` watcher correctly skips it. Activation does not change the + // graph identity, so nothing re-triggers that watcher -- the reconcile + // has to happen where the active binding is established. + const graph = shallowRef(null) + const { unmount, isTargetActive } = mountFollower( + 'wf-1', + false, + () => graph.value + ) + + graph.value = fakeGraph + await nextTick() + expect(materializerState.reconcileAgentAdapters).not.toHaveBeenCalled() + + isTargetActive.value = true + await nextTick() + + expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledWith( + fakeGraph + ) + unmount() + }) + + it('reconciles after a doc_reset clear, without waiting for another frame', () => { + // `clearForReset` empties the stores only. Every live adapter survives it + // and would be serialised back into a save until some later frame landed. + const { unmount } = mountFollower('wf-1', true, () => fakeGraph) + + dispatchFrame('doc_reset', { + workflowId: 'wf-1', + actor: 'agent:turn', + seq: 43 + }) + + expect(adapterState.clearForReset).toHaveBeenCalled() + expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledWith( + fakeGraph + ) + unmount() + }) + + it('reconciles after a follower_replaced clear', () => { + const { unmount } = mountFollower('wf-1', true, () => fakeGraph) + + dispatchFrame('follower_replaced', { workflowId: 'wf-1' }) + + expect(adapterState.clearForReset).toHaveBeenCalled() + expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledWith( + fakeGraph + ) + unmount() + }) + it('records a dev event only when nodes were materialized', async () => { const { recordDevEvent } = await import('./devPanelLog') const { unmount } = mountFollower('wf-1', true, () => fakeGraph) diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 6b1d7202ce5..08654d7d572 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -457,8 +457,14 @@ export function useAgentCrdtFollower( actor: detail?.actor ?? 'agent-reset', opId: `doc-reset:${detail?.seq ?? 'unknown'}` } - if (detail?.workflowId !== undefined) + if (detail?.workflowId !== undefined) { adapter.clearForReset(detail.workflowId, context) + // A lineage break empties the stores but leaves every live adapter + // standing, and those adapters are what a save serialises. Without a + // reconcile here the pre-reset nodes survive -- and can be written back + // -- until some later frame happens to arrive. + reconcileLiveGraph(detail.workflowId) + } connected.value = false updatesApplied.value = 0 lastFrameType.value = event.type @@ -489,6 +495,10 @@ export function useAgentCrdtFollower( actor: 'agent-lineage', opId: `follower-replaced:${workflowId}` }) + // Same reasoning as `onDocReset`: the clear is store-only, so the stale + // live adapters have to be swept before the replacement doc's frames + // start landing. + reconcileLiveGraph(workflowId) adapter.bind(workflowId, bridge.follower) } } @@ -584,6 +594,12 @@ export function useAgentCrdtFollower( }) } } + // Readiness only. The other ordering -- graph ready first, target activated + // second -- cannot be caught here: `getGraph` does not change when activity + // flips, and even if this watcher also took `isTargetActive` as a source it + // was created before the binding watcher below, so it would run first and + // still see `boundWorkflowId === null`. Activation is therefore reconciled at + // the bind site instead, once the binding actually exists. watch(getGraph, (graph) => { if (graph && boundWorkflowId !== null && isTargetActive.value) { reconcileLiveGraph(boundWorkflowId) @@ -600,7 +616,11 @@ export function useAgentCrdtFollower( } watch( [workflowId, isTargetActive], - ([next, active]) => { + ([next, active], previous) => { + // Only the inactive->active edge, and never the `immediate` first run + // (`previous` is undefined there), so a plain mount or retarget keeps its + // existing "reconcile on frame or on graph readiness" behaviour. + const justActivated = active && previous?.[1] === false clearSubscribeRetry() clearStaleProbe() connected.value = false @@ -627,6 +647,7 @@ export function useAgentCrdtFollower( } subscribedWorkflowId.value = persisted retarget(persisted) + if (justActivated) reconcileLiveGraph(persisted) return } clearPersistedDocId() @@ -646,6 +667,7 @@ export function useAgentCrdtFollower( } subscribedWorkflowId.value = next retarget(next) + if (justActivated) reconcileLiveGraph(next) }, { immediate: true } ) From 22c80eb8acdd7d4eac150a235f5e3c2209af144b Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 3 Sep 2026 17:14:00 +0000 Subject: [PATCH 18/19] fix(agent-crdt): preserve removal lifecycle during reconciliation --- src/lib/litegraph/src/LGraph.ts | 83 ++++++---- .../operations/graphLayoutAttachment.ts | 24 ++- .../agent/crdt/agentNodeMaterializer.test.ts | 143 +++++++++++++++++- .../agent/crdt/agentNodeMaterializer.ts | 45 +----- 4 files changed, 216 insertions(+), 79 deletions(-) diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 8a3dece8112..cdec0a58183 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -21,7 +21,8 @@ import { detachGroupLayout, detachNodeLayout, detachRerouteLayout, - materializeRerouteLayout + materializeRerouteLayout, + releaseNodeLayoutAttachment } from '@/renderer/core/layout/operations/graphLayoutAttachment' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { nodesInRenderOrder } from '@/renderer/core/canvas/litegraph/arrangeForLegacyRender' @@ -222,6 +223,15 @@ export interface GraphAddOptions { dragEvent?: MouseEvent } +/** Options for {@link LGraph.remove} method. */ +export interface GraphRemoveOptions { + /** + * Detach an adapter after another authority has reconciled canonical stores. + * Same-id replacement state is left intact. + */ + preserveCanonicalState?: boolean +} + export interface LGraphExtra extends Dictionary { reroutes?: SerialisableReroute[] linkExtensions?: { id: LinkId; parentId: RerouteId | undefined }[] @@ -1380,7 +1390,10 @@ export class LGraph * Removes a node from the graph * @param node the instance of the node */ - remove(node: LGraphNode | LGraphGroup): void { + remove( + node: LGraphNode | LGraphGroup, + options: GraphRemoveOptions = {} + ): void { // LEGACY: This was changed from constructor === LiteGraph.LGraphGroup if (node instanceof LGraphGroup) { this.canvasAction((c) => c.deselect(node)) @@ -1400,12 +1413,12 @@ export class LGraph if (nodesBeingRemoved.has(node)) return // not found - if (this._nodes_by_id[node.id] == null) { + if (this._nodes_by_id[node.id] == null && !options.preserveCanonicalState) { console.warn('LiteGraph: node not found', node) return } // cannot be removed - if (node.ignore_remove) { + if (node.ignore_remove && !options.preserveCanonicalState) { console.warn('LiteGraph: node cannot be removed', node) return } @@ -1417,38 +1430,47 @@ export class LGraph nodesBeingRemoved.add(node) try { - this.batchVersionUpdates(() => this.removeNode(node)) + this.batchVersionUpdates(() => this.removeNode(node, options)) } finally { nodesBeingRemoved.delete(node) } } - private removeNode(node: LGraphNode): void { + private removeNode(node: LGraphNode, options: GraphRemoveOptions): void { + const successor = + options.preserveCanonicalState && + this._nodes_by_id[node.id] !== node && + this._nodes_by_id[node.id] != null + ? this._nodes_by_id[node.id] + : undefined + // sure? - almost sure is wrong this.beforeChange() this.events.dispatch('node:before-removed', { node }) - const { inputs, outputs } = node + if (!successor) { + const { inputs, outputs } = node - // disconnect inputs - if (inputs) { - for (const [i] of inputs.entries()) { - if (inputHasLink(this, node.id, i)) node.disconnectInput(i, true) + // disconnect inputs + if (inputs) { + for (const [i] of inputs.entries()) { + if (inputHasLink(this, node.id, i)) node.disconnectInput(i, true) + } } - } - // disconnect outputs - if (outputs) { - for (const i of outputs.keys()) { - if (outputHasLinks(this, node.id, i)) node.disconnectOutput(i) + // disconnect outputs + if (outputs) { + for (const i of outputs.keys()) { + if (outputHasLinks(this, node.id, i)) node.disconnectOutput(i) + } } - } - // Floating links - for (const link of this.floatingLinks.values()) { - if (link.origin_id === node.id || link.target_id === node.id) { - this.removeFloatingLink(link) + // Floating links + for (const link of this.floatingLinks.values()) { + if (link.origin_id === node.id || link.target_id === node.id) { + this.removeFloatingLink(link) + } } } @@ -1472,12 +1494,19 @@ export class LGraph // callback node.onRemoved?.() - clearNodeOwnedStoreState(node) + if (!successor) clearNodeOwnedStoreState(node) const order = node.order - useExecutionOrderStore().remove(graphScopeOf(this), node.id) - detachNodeFromStores(this, node) - detachNodeLayout(node) + if (!successor) { + useExecutionOrderStore().remove(graphScopeOf(this), node.id) + } + if (options.preserveCanonicalState) { + node._graphScope = undefined + releaseNodeLayoutAttachment(node) + } else { + detachNodeFromStores(this, node) + detachNodeLayout(node) + } node.graph = null node.order = order @@ -1498,7 +1527,9 @@ export class LGraph const pos = this._nodes.indexOf(node) if (pos != -1) this._nodes.splice(pos, 1) - delete this._nodes_by_id[node.id] + if (this._nodes_by_id[node.id] === node) { + delete this._nodes_by_id[node.id] + } this.onNodeRemoved?.(node) this.events.dispatch('node:removed', { node }) diff --git a/src/renderer/core/layout/operations/graphLayoutAttachment.ts b/src/renderer/core/layout/operations/graphLayoutAttachment.ts index 7fc069cadb8..123a4f57882 100644 --- a/src/renderer/core/layout/operations/graphLayoutAttachment.ts +++ b/src/renderer/core/layout/operations/graphLayoutAttachment.ts @@ -349,7 +349,14 @@ export function transferLayoutAttachment( return true } -export function detachNodeLayout(node: LGraphNode): void { +/** Release a node's adapter attachment without changing canonical layout state. */ +export function releaseNodeLayoutAttachment(node: LGraphNode): void { + takeNodeLayoutAttachment(node) +} + +function takeNodeLayoutAttachment( + node: LGraphNode +): NodeLayoutAttachment | undefined { const attachment = nodeAttachments.get(node) if (!attachment) return const { graphId, id: nodeId, ownerGraphId } = attachment @@ -358,12 +365,19 @@ export function detachNodeLayout(node: LGraphNode): void { layoutStore.readNodeRect(graphId, nodeId, projection.buffer) projection.layoutRef = undefined nodeAttachments.delete(node) - if (!deleteNodeAttachmentOwner(graphId, node)) return + return deleteNodeAttachmentOwner(graphId, node) + ? { graphId, id: nodeId, ownerGraphId } + : undefined +} + +export function detachNodeLayout(node: LGraphNode): void { + const attachment = takeNodeLayoutAttachment(node) + if (!attachment) return layoutStore.applyOperation({ ...canvasOperationMeta(), - graphId, - nodeId, - ownerGraphId, + graphId: attachment.graphId, + nodeId: attachment.id, + ownerGraphId: attachment.ownerGraphId, type: 'deleteNode' }) } diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index f30d5a93467..b95faafb50b 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -11,7 +11,16 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import * as Y from 'yjs' import { createGraphMutations } from '@/core/graph/graphMutations' -import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' +import { + LGraph, + LGraphNode, + LiteGraph, + LLink +} from '@/lib/litegraph/src/litegraph' +import { + createTestSubgraphData, + createTestSubgraphNode +} from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' import { reportError } from '@/platform/telemetry/reportError' // Mirrors the production bridge in AgentPanelRoot.vue, which takes the same // exemption to drive the real layout store. @@ -19,14 +28,16 @@ import { reportError } from '@/platform/telemetry/reportError' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' // eslint-disable-next-line import-x/no-restricted-paths import { LayoutSource } from '@/renderer/core/layout/types' +import { useExecutionOrderStore } from '@/stores/executionOrderStore' import { useLinkStore } from '@/stores/linkStore' import { useNodeDataStore } from '@/stores/nodeDataStore' +import { usePreviewExposureStore } from '@/stores/previewExposureStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { GraphScope } from '@/types/graphScopeId' import { graphScopeOf } from '@/types/graphScopeId' import type { RemoteMutationContext } from '@/types/graphMutationContext' import { toLinkId } from '@/types/linkId' -import { toNodeId } from '@/types/nodeId' +import { UNASSIGNED_NODE_ID, toNodeId } from '@/types/nodeId' import { widgetId } from '@/types/widgetId' import { reconcileAgentAdapters } from './agentNodeMaterializer' @@ -400,15 +411,100 @@ describe('reconcileAgentAdapters', () => { expect(stale?.graph).toBeNull() expect(graph.serialize().nodes).toHaveLength(1) }) + + it('runs stale-node lifecycle without clearing successor-owned state', () => { + const graph = new LGraph() + const scope = graphScopeOf(graph) + const mutations = remoteMutations(scope) + mutations.batch(REMOTE, (batch) => { + batch.addNode({ + ...nodePayload(1, 'widget-node'), + outputs: [{ name: 'value', type: '*', links: [] }], + widgets_values: { value: 7 } + }) + batch.addNode({ + ...nodePayload(2), + inputs: [{ name: 'value', type: '*', link: null }] + }) + batch.connect({ + id: 9, + originNodeId: 1, + originSlot: 0, + targetNodeId: 2, + targetSlot: 0, + type: '*' + }) + }) + reconcileAgentAdapters(graph) + + const stale = graph.getNodeById(toNodeId(1))! + const lifecycle: string[] = [] + graph.events.addEventListener('node:before-removed', (event) => { + if (event.detail.node === stale) lifecycle.push('before-removed') + }) + stale.onRemoved = () => lifecycle.push('onRemoved') + const incumbent = useNodeDataStore().getNode( + scope.rootGraphId, + toNodeId(1) + )! + expect(useNodeDataStore().deleteNode(scope, incumbent, REMOTE)).toBe(true) + expect( + mutations.addNode( + { + ...nodePayload(1, 'widget-node'), + outputs: [{ name: 'value', type: '*', links: [9] }], + widgets_values: { value: 7 } + }, + { ...REMOTE, opId: 'op-replace-1' } + ) + ).toBe(true) + + expect(reconcileAgentAdapters(graph)).toEqual([toNodeId(1)]) + + const replacement = graph.getNodeById(toNodeId(1))! + expect(lifecycle).toEqual(['before-removed', 'onRemoved']) + expect(replacement).not.toBe(stale) + expect(graph._nodes).not.toContain(stale) + expect(graph.getLink(toLinkId(9))).toMatchObject({ + origin_id: toNodeId(1), + target_id: toNodeId(2) + }) + expect( + useWidgetValueStore().getWidget( + widgetId(scope.rootGraphId, toNodeId(1), 'value') + )?.value + ).toBe(7) + expect( + layoutStore.getNodeLayout(scope.rootGraphId, toNodeId(1)) + ).toBeDefined() + expect(useExecutionOrderStore().get(scope, toNodeId(1))).toBeDefined() + }) }) describe('remote removal of a live node', () => { - it('detaches the node after a remote delete', () => { + it('runs full removal lifecycle after a remote delete', () => { const graph = new LGraph() const scope = seedAgentAddedNode(graph, 1) reconcileAgentAdapters(graph) - const removed = vi.fn() - graph.events.addEventListener('node:removed', removed) + const node = graph.getNodeById(toNodeId(1))! + const lifecycle: string[] = [] + graph.events.addEventListener('node:before-removed', () => { + lifecycle.push('before-removed') + }) + node.onRemoved = () => lifecycle.push('onRemoved') + graph.onNodeRemoved = () => lifecycle.push('onNodeRemoved') + graph.events.addEventListener('node:removed', () => { + lifecycle.push('node:removed') + }) + usePreviewExposureStore().addExposure( + scope.rootGraphId, + String(node.id), + { sourceNodeId: node.id, sourcePreviewName: 'preview' } + ) + useExecutionOrderStore().set(scope, node.id, 7) + graph.addFloatingLink( + new LLink(toLinkId(77), '*', node.id, 0, UNASSIGNED_NODE_ID, -1) + ) remoteMutations(scope).deleteNode(toNodeId(1), [], REMOTE) @@ -416,7 +512,20 @@ describe('reconcileAgentAdapters', () => { expect(graph._nodes).toHaveLength(0) expect(graph.getNodeById(toNodeId(1))).toBeFalsy() expect(graph.serialize().nodes).toHaveLength(0) - expect(removed).toHaveBeenCalledOnce() + expect(graph.floatingLinks.size).toBe(0) + expect( + usePreviewExposureStore().getExposures( + scope.rootGraphId, + String(node.id) + ) + ).toEqual([]) + expect(useExecutionOrderStore().get(scope, node.id)).toBeUndefined() + expect(lifecycle).toEqual([ + 'before-removed', + 'onRemoved', + 'onNodeRemoved', + 'node:removed' + ]) }) it('detaches every node after a remote clear', () => { @@ -445,6 +554,28 @@ describe('reconcileAgentAdapters', () => { expect(reconcileAgentAdapters(graph)).toEqual([]) expect(graph._nodes.map((node) => node.id)).toEqual([toNodeId(2)]) }) + + it('releases an orphaned subgraph definition and its inner lifecycle', () => { + const graph = new LGraph() + const subgraph = graph.createSubgraph(createTestSubgraphData()) + const inner = new LGraphNode('inner') + subgraph.add(inner) + const host = createTestSubgraphNode(subgraph, { id: 1 }) + graph.add(host) + const beforeRemoved = vi.fn() + const onRemoved = vi.fn() + subgraph.events.addEventListener('node:before-removed', beforeRemoved) + inner.onRemoved = onRemoved + expect(graph.subgraphs.has(subgraph.id)).toBe(true) + + remoteMutations(graphScopeOf(graph)).deleteNode(host.id, [], REMOTE) + reconcileAgentAdapters(graph) + + expect(graph.subgraphs.has(subgraph.id)).toBe(false) + expect(beforeRemoved).toHaveBeenCalledOnce() + expect(onRemoved).toHaveBeenCalledOnce() + expect(inner._graphScope).toBeUndefined() + }) }) describe('failure handling', () => { diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index ab25ddd6d2e..c1021e03315 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -17,17 +17,7 @@ import { runMintPortsSuppressed } from './mintPortWiring' export type MaterializableGraph = Pick< LGraph, - | 'id' - | 'rootGraph' - | '_nodes' - | '_nodes_by_id' - | 'add' - | 'remove' - | 'updateExecutionOrder' - | 'setDirtyCanvas' - | 'list_of_graphcanvas' - | 'onNodeRemoved' - | 'events' + 'id' | 'rootGraph' | '_nodes' | '_nodes_by_id' | 'add' | 'remove' > /** @@ -89,10 +79,8 @@ function reconcile(graph: MaterializableGraph): NodeId[] { (orphan) => graph._nodes_by_id[orphan.id] !== orphan || !recordIds.has(orphan.id) ) - for (const orphan of detached) detachOrphan(graph, orphan) - if (detached.length > 0) { - graph.updateExecutionOrder() - graph.setDirtyCanvas(true, true) + for (const orphan of detached) { + graph.remove(orphan, { preserveCanonicalState: true }) } return materialized } @@ -195,33 +183,6 @@ function materialize( return true } -/** - * Drop a node the stores no longer know about from the live graph without - * going through `LGraph.remove()`: that path unregisters store state, releases - * widget records and emits a layout delete, all of which now belong to the - * record (and replacement node) that took over the id. - */ -function detachOrphan(graph: MaterializableGraph, orphan: LGraphNode): void { - try { - orphan.onRemoved?.() - } catch (cause) { - console.error(cause) - } - const index = graph._nodes.indexOf(orphan) - if (index !== -1) graph._nodes.splice(index, 1) - if (graph._nodes_by_id[orphan.id] === orphan) { - delete graph._nodes_by_id[orphan.id] - } - for (const canvas of graph.list_of_graphcanvas ?? []) { - delete canvas.selected_nodes[orphan.id] - canvas.deselect(orphan) - } - orphan.graph = null - orphan._graphScope = undefined - graph.onNodeRemoved?.(orphan) - graph.events.dispatch('node:removed', { node: orphan }) -} - /** Same placeholder `LGraph.configure()` builds for an unregistered type. */ function missingNode(state: NodeState): LGraphNode { const node = new LGraphNode( From 234927089e5bd0792546345b6d950f3ed9e1b2d1 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 3 Sep 2026 18:12:45 +0000 Subject: [PATCH 19/19] fix: preserve successor errors during adapter replacement --- .../graph/useErrorClearingHooks.test.ts | 33 +++++++++++++++++++ .../graph/useErrorClearingHooks.ts | 15 ++++++--- src/lib/litegraph/src/LGraph.test.ts | 18 ++++++++++ src/lib/litegraph/src/LGraph.ts | 2 +- .../src/infrastructure/LGraphEventMap.ts | 5 +++ 5 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/composables/graph/useErrorClearingHooks.test.ts b/src/composables/graph/useErrorClearingHooks.test.ts index 76cec0f80bd..2c294bcc9d6 100644 --- a/src/composables/graph/useErrorClearingHooks.test.ts +++ b/src/composables/graph/useErrorClearingHooks.test.ts @@ -968,6 +968,39 @@ describe('onNodeRemoved clears missing asset errors by execution ID', () => { expect(modelStore.missingModelCandidates).toBeNull() }) + it('preserves same-id successor missing model errors', () => { + const graph = new LGraph() + const orphan = new LGraphNode('CheckpointLoaderSimple') + graph.add(orphan) + const successor = new LGraphNode('CheckpointLoaderSimple') + successor.id = orphan.id + graph._nodes.push(successor) + graph._nodes_by_id[orphan.id] = successor + + vi.spyOn(app, 'rootGraph', 'get').mockReturnValue(graph) + installErrorClearingHooks(graph) + + const modelStore = useMissingModelStore() + modelStore.setMissingModels([ + fromAny< + Parameters[0][number], + unknown + >({ + nodeId: String(successor.id), + nodeType: 'CheckpointLoaderSimple', + widgetName: 'ckpt_name', + isAssetSupported: false, + name: 'model.safetensors', + isMissing: true + }) + ]) + + graph.remove(orphan, { preserveCanonicalState: true }) + + expect(graph.getNodeById(successor.id)).toBe(successor) + expect(modelStore.missingModelCandidates).toHaveLength(1) + }) + it('removes missing model errors when the graph is cleared', () => { const graph = new LGraph() const node = new LGraphNode('CheckpointLoaderSimple') diff --git a/src/composables/graph/useErrorClearingHooks.ts b/src/composables/graph/useErrorClearingHooks.ts index 710de16dbe3..179dca40b12 100644 --- a/src/composables/graph/useErrorClearingHooks.ts +++ b/src/composables/graph/useErrorClearingHooks.ts @@ -8,7 +8,10 @@ import { useChainCallback } from '@/composables/functional/useChainCallback' import { resolvePromotedWidgetSource } from '@/core/graph/subgraph/resolvePromotedWidgetSource' import { createPromotionErrorReconciler } from '@/core/graph/subgraph/createPromotionErrorReconciler' -import type { NodeLifecycleEvent } from '@/lib/litegraph/src/infrastructure/LGraphEventMap' +import type { + NodeBeforeRemovedEvent, + NodeLifecycleEvent +} from '@/lib/litegraph/src/infrastructure/LGraphEventMap' import { LiteGraph, Subgraph } from '@/lib/litegraph/src/litegraph' import type { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import { @@ -585,7 +588,9 @@ export function installErrorClearingHooks(graph: LGraph): () => void { // `node:before-removed` covers both single removals and graph.clear(); // `node:removed` fires only from LGraph.remove. - const onNodeRemoved = ({ detail: { node } }: NodeLifecycleEvent) => { + const onNodeRemoved = ({ + detail: { node, successor } + }: NodeBeforeRemovedEvent) => { if (disposed) return for (const scan of pendingScans.get(node) ?? []) scan.cancel() // Derive the execution ID from the graph the hook is installed on plus @@ -593,8 +598,10 @@ export function installErrorClearingHooks(graph: LGraph): () => void { // "parentId:...:nodeId" path that matches how missing asset errors are // keyed; without this, removal falls back to the local ID and misses // subgraph entries. - const execId = getRemovedNodeExecutionId(graph, node.id) - removeNodeErrors(node, execId) + if (!successor) { + const execId = getRemovedNodeExecutionId(graph, node.id) + removeNodeErrors(node, execId) + } scheduleDropOutOfScopeMissingMedia() restoreNodeHooksRecursive(node) promotionErrors.detachNode(node) diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index e2782992c0c..10041e4824b 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -1108,6 +1108,24 @@ describe('node:before-removed event', () => { expect(node.graph).toBeNull() }) + it('identifies the successor when preserving same-id canonical state', () => { + const graph = new LGraph() + const node = new LGraphNode('test') + graph.add(node) + const successor = new LGraphNode('test') + successor.id = node.id + graph._nodes.push(successor) + graph._nodes_by_id[node.id] = successor + + const beforeRemoved = vi.fn() + graph.events.addEventListener('node:before-removed', beforeRemoved) + + graph.remove(node, { preserveCanonicalState: true }) + + expect(beforeRemoved).toHaveBeenCalledOnce() + expect(beforeRemoved.mock.calls[0][0].detail).toEqual({ node, successor }) + }) + it('does not fire node:before-removed for a node not in the graph', () => { const graph = new LGraph() const node = new LGraphNode('test') diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index cdec0a58183..f5934cd4ed0 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -1447,7 +1447,7 @@ export class LGraph // sure? - almost sure is wrong this.beforeChange() - this.events.dispatch('node:before-removed', { node }) + this.events.dispatch('node:before-removed', { node, successor }) if (!successor) { const { inputs, outputs } = node diff --git a/src/lib/litegraph/src/infrastructure/LGraphEventMap.ts b/src/lib/litegraph/src/infrastructure/LGraphEventMap.ts index fe19d3b16f8..283892b2b3f 100644 --- a/src/lib/litegraph/src/infrastructure/LGraphEventMap.ts +++ b/src/lib/litegraph/src/infrastructure/LGraphEventMap.ts @@ -66,6 +66,7 @@ export interface LGraphEventMap { */ 'node:before-removed': { node: LGraphNode + successor?: LGraphNode } /** @@ -92,3 +93,7 @@ export interface LGraphEventMap { export type NodeLifecycleEvent = CustomEvent< LGraphEventMap['node:added' | 'node:removed'] > + +export type NodeBeforeRemovedEvent = CustomEvent< + LGraphEventMap['node:before-removed'] +>