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 115c4f59167..10041e4824b 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,57 @@ describe('Store-driven serialization parity', () => { ) }) + // 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 + }) => { + // 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) + + const serialized = graph.serialize() + + // 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', ({ expect }) => { @@ -1055,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 8a3dece8112..f5934cd4ed0 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 }) + this.events.dispatch('node:before-removed', { node, successor }) - 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/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/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'] +> 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/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 6dd86bb51ba..e6736109088 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -419,7 +419,11 @@ const { boundWorkflowId, graphMutations, () => resolvedUserInfo.value?.id ?? null, - isBoundWorkflowActive + isBoundWorkflowActive, + // `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 new file mode 100644 index 00000000000..b95faafb50b --- /dev/null +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -0,0 +1,812 @@ +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, + 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. +// 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 { 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 { UNASSIGNED_NODE_ID, 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' + +vi.mock('@/platform/telemetry/reportError', () => ({ + reportError: vi.fn() +})) + +class DummyNode extends LGraphNode { + constructor() { + super('dummy') + } +} + +class WidgetNode extends LGraphNode { + constructor() { + super('widget-node') + this.addWidget('number', 'value', 0, () => {}) + } +} + +class ThrowsOnAddedNode extends LGraphNode { + constructor() { + super('throws-on-added') + } + + 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 = { + source: 'agent-remote', + 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 + * 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) + remoteMutations(scope).addNode(nodePayload(id, type), { + ...REMOTE, + opId: `op-${id}` + }) + return scope +} + +beforeEach(() => { + setActivePinia(createTestingPinia({ stubActions: false })) + LiteGraph.registerNodeType('dummy', DummyNode) + LiteGraph.registerNodeType('widget-node', WidgetNode) + LiteGraph.registerNodeType('throws-on-added', ThrowsOnAddedNode) +}) + +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() + seedAgentAddedNode(graph, 1) + expect(graph._nodes).toHaveLength(0) + + const materialized = reconcileAgentAdapters(graph) + + 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 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') + }) + }) + + 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)) + + 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) + }) + + 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' }) + + 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('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('runs full removal lifecycle after a remote delete', () => { + const graph = new LGraph() + const scope = seedAgentAddedNode(graph, 1) + reconcileAgentAdapters(graph) + 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) + + expect(reconcileAgentAdapters(graph)).toEqual([]) + expect(graph._nodes).toHaveLength(0) + expect(graph.getNodeById(toNodeId(1))).toBeFalsy() + expect(graph.serialize().nodes).toHaveLength(0) + 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', () => { + 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('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', () => { + 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' + }) + + 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' + }) + ) + }) + + 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) + }) + + 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() + // 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', () => { + 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' + }) + ) + }) + }) + + 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('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('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( + { ...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 + }) + ]) + }) + }) +}) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts new file mode 100644 index 00000000000..c1021e03315 --- /dev/null +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -0,0 +1,210 @@ +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' +import { graphScopeOf } from '@/types/graphScopeId' +import type { NodeId } from '@/types/nodeId' +import type { NodeState } from '@/types/nodeState' +import { widgetId } from '@/types/widgetId' +import type { WidgetStateInit } from '@/types/widgetState' + +import { runMintPortsSuppressed } from './mintPortWiring' + +export type MaterializableGraph = Pick< + LGraph, + 'id' | 'rootGraph' | '_nodes' | '_nodes_by_id' | 'add' | 'remove' +> + +/** + * 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. 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 + * 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[] { + return runMintPortsSuppressed(() => reconcile(graph)) +} + +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, + 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 ( + 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) { + graph.remove(orphan, { preserveCanonicalState: 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 + + 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 + ) + } + if (orphan) graph._nodes_by_id[orphan.id] = orphan + } + + const rollback = (cause: unknown) => { + // `add()` may throw after attaching (from `onAdded`); only then is there + // a live node to take back out. + // + // 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 + } + + // `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) { + // 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 +} + +/** 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/devPanelLog.ts b/src/workbench/extensions/agent/crdt/devPanelLog.ts index 1b7bedae410..57d0fa35d2a 100644 --- a/src/workbench/extensions/agent/crdt/devPanelLog.ts +++ b/src/workbench/extensions/agent/crdt/devPanelLog.ts @@ -36,6 +36,7 @@ export type DevEventKind = | 'stale_probe' | 'doc_gap' | 'doc_stale' + | 'agent_node_adapters_materialized' export interface DevEvent { seq: number diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index c2133ec0aae..fb259d6df01 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('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 + // 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, 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' }) + 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() + host.destroy() + }) + it('keeps follower docs and apply queues isolated by workflow target', () => { const followerA = new FollowerDoc() const followerB = new FollowerDoc() diff --git a/src/workbench/extensions/agent/crdt/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 986339a6ff2..05ff4210b7a 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -8,12 +8,16 @@ */ 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' const bridgeState = vi.hoisted(() => { class FakeBridge extends EventTarget { @@ -49,6 +53,10 @@ const adapterState = vi.hoisted(() => ({ destroy: vi.fn() })) +const materializerState = vi.hoisted(() => ({ + reconcileAgentAdapters: vi.fn(() => [] as NodeId[]) +})) + const apiState = vi.hoisted(() => { const target = new EventTarget() return { @@ -94,6 +102,10 @@ vi.mock('./ecsFollowerAdapter', () => ({ } })) +vi.mock('./agentNodeMaterializer', () => ({ + reconcileAgentAdapters: materializerState.reconcileAgentAdapters +})) + vi.mock('./devPanelLog', () => ({ recordDevEvent: vi.fn() })) @@ -137,7 +149,8 @@ function writeRawRecord(overrides: { function mountFollower( initial: string | null = null, - initiallyActive = true + initiallyActive = true, + getGraph: () => MaterializableGraph | null = () => null ): { unmount: () => void workflowId: Ref @@ -153,7 +166,8 @@ function mountFollower( workflowId, graphMutations, () => null, - isTargetActive + isTargetActive, + getGraph ) exposedStatus = () => status.value as AgentCrdtStatus return () => null @@ -178,6 +192,7 @@ describe('useAgentCrdtFollower', () => { setActivePinia(createPinia()) sessionStorage.clear() bridgeState.current = null + materializerState.reconcileAgentAdapters.mockReset().mockReturnValue([]) }) it('subscribes immediately to a bound workflow and reports it in status', () => { @@ -615,6 +630,151 @@ describe('useAgentCrdtFollower', () => { }) }) + 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 + + it('reconciles the live graph after every applied frame', () => { + const { unmount } = mountFollower('wf-1', true, () => fakeGraph) + + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + + expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledTimes(1) + expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledWith( + fakeGraph + ) + 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. + const { unmount } = mountFollower('wf-1') + + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + + expect(materializerState.reconcileAgentAdapters).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) + + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 }) + expect(materializerState.reconcileAgentAdapters).not.toHaveBeenCalled() + + graph.value = fakeGraph + await nextTick() + + expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledTimes(1) + expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledWith( + fakeGraph + ) + unmount() + }) + + 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) + + graph.value = fakeGraph + await nextTick() + + expect(materializerState.reconcileAgentAdapters).not.toHaveBeenCalled() + 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) + + 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 () => { 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 932668746e9..08654d7d572 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 { reconcileAgentAdapters } from './agentNodeMaterializer' import { recordDevEvent } from './devPanelLog' import { wireLog } from './crdtLog' import type { CrdtDebugSnapshot } from './crdtSnapshot' @@ -191,7 +193,13 @@ export function useAgentCrdtFollower( workflowId: Ref, graphMutations: MutationsForTarget, userId: () => string | null = () => null, - isTargetActive: Ref = ref(true) + isTargetActive: Ref = ref(true), + /** + * 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) const updatesApplied = ref(0) @@ -402,6 +410,7 @@ export function useAgentCrdtFollower( outcomes.value = applied ? { ...outcomes.value, applied: outcomes.value.applied + 1 } : { ...outcomes.value, skipped: outcomes.value.skipped + 1 } + if (applied) reconcileLiveGraph(update.workflowId) recordDevEvent('doc_update', { workflowId: update.workflowId, seq: update.seq, @@ -448,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 @@ -480,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) } } @@ -562,6 +581,30 @@ 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 + }) + } + } + // 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) + } + }) // 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 @@ -573,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 @@ -600,6 +647,7 @@ export function useAgentCrdtFollower( } subscribedWorkflowId.value = persisted retarget(persisted) + if (justActivated) reconcileLiveGraph(persisted) return } clearPersistedDocId() @@ -619,6 +667,7 @@ export function useAgentCrdtFollower( } subscribedWorkflowId.value = next retarget(next) + if (justActivated) reconcileLiveGraph(next) }, { immediate: true } )