From 04468979520e49ff2331ee6801c5a6b44ffd659c Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 14 Sep 2026 22:24:43 +0000 Subject: [PATCH] FE-2033: fix(agent): follow promoted widget edits and slot connects on existing subgraph hosts (#17202) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Short-term fix so the agent can edit promoted widgets (S1) and connect slots (S2) on existing subgraph hosts without the follower wiping the host or mis-wiring the link. Creation/promotion (S3/S4), `insert_workflow`, forking and blueprints stay in the long-term plan: [TDD — Agent Subgraph Lifecycle](https://www.notion.so/3d16d73d365081719b85ed515af98276) (V1 = this PR; V1.5 `insert_workflow` cmp op; V2 promote/demote + `fork_definition`; V3 `define_subgraph` + scoped interior ops; V4 blueprints/doc updates). - Promoted `set_widget` on a host no longer wipes its widgets (host storage flips to opaque in one cmp tx; follower now lets the opaque loop own it). - `connect` to a grown host input resolves the slot by name from the subgraph definition (nested definitions included); an undeclared or duplicate name skips the link and reports `error_reconciling_agent_subgraph_host_slot` instead of landing on a random input. - Follower tests RED→GREEN for S1/S2 plus a helper module with its own tests. Includes the scalar-only `nodeDataStore` update path and shared LiteGraph promoted-widget traversal. Browser spec is follow-up #17224.
Full context for agent readers ### Scope Linear [BE-10305](https://linear.app/comfyorg/issue/BE-10305) / FE-2033: S1 promoted-widget edits and S2 slot connects on existing subgraphs are the immediate scope for FE 1.54. S3 promotion, S4 creation, `insert_workflow` as a cmp op (decided 2026-09-08, TDD ADR-T8, phase V1.5), and definition-change tracking are V2+ in the [TDD](https://www.notion.so/3d16d73d365081719b85ed515af98276). Program tracking: [blocked-on-christian #393](https://github.com/christian-byrne/blocked-on-christian/issues/393); DQ-63 (`insert_workflow`) in [#417](https://github.com/christian-byrne/blocked-on-christian/issues/417). Related: #16922 (S4 materializer fix, merged), #16932 (deferred S4 review findings), #16957 (S4 review debt, blocked on #16644), #17224 (Playwright spec for this PR's S1/S2 paths). ### Root causes (verified against cmp `src/applier.ts`) 1. `applyPromotedHostWrite` does `mdel(target,"widgets")` and `mset(target,"__widgets_opaque",next)` in the same transaction. The follower put the host in both `replacedWidgetMaps` and `replacedOpaqueWidgets`, ran `reconcileNode` (positional names, promoted widgets gone) and skipped the opaque loop. 2. cmp writes only the grown slot into the host's doc `inputs`; `target_slot` indexes that doc-local list, while the live host orders inputs by the subgraph definition. Positional wiring hit the wrong slot. 3. `claimPromotedInput` does not validate `grow.name` against the definition. A bogus name used to fall back to the doc index. ### Changes - `agentSubgraphHostSlots.ts`: `indexSubgraphDefinitions` (top-level + nested `definitions.subgraphs`), `promotedWidgetNames(definition, index)` (memoized per `(definition.id, inputIndex)`, active-set cycle detection → cyclic reference is unpromoted, depth cap 32 with cap-truncated answers left unmemoized), `hostInputs`, `hostSlotIndex` — derive host slot order and promoted widget names from `subgraph_definitions`. Duplicate declared names resolve to no slot. - `agentSubgraphDefinitions.ts`: definition projection rejects unsafe collection shapes before consumers traverse them and ignores a top-level definitions root with the wrong Yjs shared type. - `ecsFollowerAdapter.ts`: `hostTarget` resolves target slot/inputs by name and returns `null` on undeclared/duplicate names → link skipped + `reportError('error_reconciling_agent_subgraph_host_slot')`; a changed link that becomes unreadable is disconnected rather than left live; `applyQueuedFrame` opaque loop owns hosts and sets promoted widgets by name; `replacedWidgetMaps` loop skips nodes also in `replacedOpaqueWidgets`; malformed host input entries are ignored; opaque array length mismatch against the promoted list is skipped and reported as `error_reconciling_agent_subgraph_host_widgets`; in-place `Y.Array` edits to opaque host widgets are observed (defensive path for foreign writers; cmp writes plain arrays); drift errors are deduped per session (`TargetSession.reportedErrors`); a failed/rejected batch re-arms `reconcileNextFrame`; live-host scalar resync includes `resizable` and `showAdvanced`. - Tests: `agentSubgraphFollower.test.ts` S1–S1q (promoted write, empty named map, opaque longer/shorter/malformed, non-host fallback, promoted names across load/re-arm/map replace/storage flip, live-field resync incl. `resizable`/`showAdvanced`, in-place Y.Array edit, per-session error dedupe, failed-batch re-arm, plain→host and host→plain type flips) and S2–S2f (declared connect, slot-less doc host, malformed inputs, undeclared name, same-id retarget onto undeclared slot, omitted doc slot keeps live link, slot error dedupe); `agentSubgraphHostSlots.test.ts` (nested definition index, first-wins on repeated id, duplicate names, depth cap, cyclic fan-out resolves in <50 index lookups). ### Verification Current head: `pnpm vitest run src/workbench/extensions/agent/crdt` 36 files / 543 tests green; `pnpm typecheck`, `pnpm lint`, `pnpm format:check`, `pnpm knip`, and changed-scope Fallow green. Blind reviewer thread independently reproduced both P1s (same-id retarget, nested lookup); both fixed in follow-up commits with regression tests. ### Not verified / follow-ups - No Playwright coverage in this PR. Feasible via `browser_tests/tests/agent/agentPanelMocks.ts` + `browser_tests/fixtures/ws.ts`; needs a subgraph workflow fixture → #17224. - No live agent.comfy.org run in this PR. ### Adversarial review (cross-model, Codex) — deferred or rejected - Definition edits not tracked mid-session: S3/S4, TDD V2. - Reuse `SubgraphNode._resolveInputWidget`: follower reads the Y.Doc, not a live node; needs the ECS lookup the TDD introduces. - Promoted names assumed unique: serializer enforces; follower now also treats duplicates as undeclared. ### Decisions made without explicit sign-off - Host owns the promoted value (ADR-SUBGRAPH-PROMOTION-0009); S1 no longer asserts the interior node value. - Opaque array length mismatch → skip + report, not partial apply. - Omitted doc slots keep their live link; nested definitions index first-wins. - Host-origin positional resolution (host as link source) deferred to V2. - `reconcileNode` fallback kept only for non-subgraph opaque nodes.
--------- Co-authored-by: GitHub Action Co-authored-by: Alexander Brown Co-authored-by: Amp (cherry picked from commit d0dc978a1a4cf670b0a992d8c57381bd56fa9dcf) --- src/core/graph/graphMutations.test.ts | 336 +++++ src/core/graph/graphMutations.ts | 249 +++- .../graph/subgraph/hasPromotedWidgetTarget.ts | 45 + .../subgraph/resolveConcretePromotedWidget.ts | 82 +- src/stores/nodeDataStore.test.ts | 65 +- src/stores/nodeDataStore.ts | 25 +- .../agent/crdt/agentNodeMaterializer.ts | 20 +- .../crdt/agentSubgraphDefinitions.test.ts | 42 + .../agent/crdt/agentSubgraphDefinitions.ts | 97 +- .../agent/crdt/agentSubgraphFollower.test.ts | 1083 +++++++++++++++++ .../agent/crdt/agentSubgraphHostSlots.test.ts | 373 ++++++ .../agent/crdt/agentSubgraphHostSlots.ts | 244 ++++ .../ecsFollowerAdapter.integration.test.ts | 1 + .../agent/crdt/ecsFollowerAdapter.ts | 385 +++++- 14 files changed, 2876 insertions(+), 171 deletions(-) create mode 100644 src/core/graph/subgraph/hasPromotedWidgetTarget.ts create mode 100644 src/workbench/extensions/agent/crdt/agentSubgraphFollower.test.ts create mode 100644 src/workbench/extensions/agent/crdt/agentSubgraphHostSlots.test.ts create mode 100644 src/workbench/extensions/agent/crdt/agentSubgraphHostSlots.ts diff --git a/src/core/graph/graphMutations.test.ts b/src/core/graph/graphMutations.test.ts index d788d68ede1..5b736ca2420 100644 --- a/src/core/graph/graphMutations.test.ts +++ b/src/core/graph/graphMutations.test.ts @@ -254,6 +254,121 @@ describe('graphMutations', () => { expect(createLayout).not.toHaveBeenCalled() }) + it('resyncs scalar fields without touching slots, widgets, or layout', () => { + const graph = mutations() + graph.addNode(node(1, { seed: 1 }), context) + graph.addNode(node(2), context) + expect( + graph.connect( + { + id: 9, + originNodeId: 2, + originSlot: 0, + targetNodeId: 1, + targetSlot: 0, + type: 'IMAGE', + targetInputs: [{ name: 'in', type: 'IMAGE', link: toLinkId(9) }] + }, + context + ) + ).toBe(true) + const [existing] = useNodeDataStore().getGraphNodesFor('root', 'root') + const [inputBefore] = existing.inputs + expect(inputBefore.link).toBe(toLinkId(9)) + createLayout.mockClear() + deleteLayouts.mockClear() + + expect( + graph.batch({ ...context, opId: 'resync' }, (batch) => { + batch.reconcileNodeFields({ + ...node(1), + title: 'Renamed host', + mode: 2, + flags: { collapsed: true }, + properties: { source: 'doc' }, + // Doc slot records are stale on purpose: they must be ignored. + inputs: [{ name: 'in', type: 'IMAGE', link: null }], + widgets_values: {} + }) + }) + ).toBe(true) + + const [resynced] = useNodeDataStore().getGraphNodesFor('root', 'root') + expect(resynced).toBe(existing) + expect(resynced.title).toBe('Renamed host') + expect(resynced.mode).toBe(2) + expect(resynced.flags).toEqual({ collapsed: true }) + expect(resynced.properties).toEqual({ source: 'doc' }) + expect(resynced.inputs[0]).toBe(inputBefore) + expect(resynced.inputs[0].link).toBe(toLinkId(9)) + expect( + useWidgetValueStore().getWidget(widgetId('root', toNodeId(1), 'seed')) + ?.value + ).toBe(1) + expect(deleteLayouts).not.toHaveBeenCalled() + expect(createLayout).not.toHaveBeenCalled() + }) + + it('adds a missing field-reconciled node and rejects incomplete payloads', () => { + const graph = mutations() + graph.addNode(node(1, { seed: 1 }), context) + + expect( + graph.batch(context, (batch) => { + batch.reconcileNodeFields({ ...node(3), title: 'Ghost' }) + }) + ).toBe(true) + expect( + useNodeDataStore().getNode(scope.rootGraphId, toNodeId(3))?.title + ).toBe('Ghost') + expect( + graph.batch(context, (batch) => { + batch.reconcileNodeFields({ ...node(1), type: '' }) + }) + ).toBe(false) + expect( + graph.batch(context, (batch) => { + batch.reconcileNodeFields({ ...node(1), title: 'Live' }) + batch.reconcileNodeFields({ ...node(4), type: '' }) + }) + ).toBe(false) + + expect( + useNodeDataStore().getNode(scope.rootGraphId, toNodeId(1))?.title + ).toBe('Node 1') + }) + + it('replaces a field-reconciled node when its type changes', () => { + const graph = mutations() + graph.addNode(node(1, { seed: 1 }), context) + const existing = useNodeDataStore().getNode(scope.rootGraphId, toNodeId(1)) + createLayout.mockClear() + deleteLayouts.mockClear() + + expect( + graph.batch(context, (batch) => { + batch.reconcileNodeFields({ + ...node(1, { replacement: 2 }), + type: 'Replacement' + }) + }) + ).toBe(true) + + const replacement = useNodeDataStore().getNode( + scope.rootGraphId, + toNodeId(1) + ) + expect(replacement).not.toBe(existing) + expect(replacement?.type).toBe('Replacement') + expect( + useWidgetValueStore().getWidget( + widgetId(scope.rootGraphId, toNodeId(1), 'replacement') + )?.value + ).toBe(2) + expect(deleteLayouts).toHaveBeenCalledOnce() + expect(createLayout).toHaveBeenCalledOnce() + }) + it('updates endpoint slot records while retaining the supplied link id', () => { const graph = mutations() graph.batch(context, (batch) => { @@ -287,6 +402,227 @@ describe('graphMutations', () => { }) }) + it('keeps the live link on target slots whose supplied record omits link', () => { + const graph = mutations() + graph.batch(context, (batch) => { + batch.addNode(node(1)) + batch.addNode(node(2)) + batch.addNode(node(3)) + batch.connect({ + id: 5, + originNodeId: 1, + originSlot: 0, + targetNodeId: 2, + targetSlot: 0, + type: 'IMAGE', + targetInputs: [ + { name: 'in', type: 'IMAGE', link: toLinkId(5) }, + { name: 'grown', type: 'IMAGE', link: null } + ] + }) + }) + + expect( + graph.batch({ ...context, opId: 'op-2' }, (batch) => { + batch.connect({ + id: 9, + originNodeId: 3, + originSlot: 0, + targetNodeId: 2, + targetSlot: 1, + type: 'IMAGE', + targetInputs: [ + { name: 'in', type: 'IMAGE' }, + { name: 'grown', type: 'IMAGE', link: toLinkId(9) } + ] + }) + }) + ).toBe(true) + + const target = useNodeDataStore() + .getGraphNodesFor('root', 'root') + .find(({ id }) => id === toNodeId(2)) + expect(target?.inputs.map(({ link }) => link)).toEqual([ + toLinkId(5), + toLinkId(9) + ]) + }) + + it('lets two connects on one target within a batch both keep their links', () => { + const graph = mutations() + expect( + graph.batch(context, (batch) => { + batch.addNode(node(1)) + batch.addNode(node(2)) + batch.addNode(node(3)) + batch.connect({ + id: 5, + originNodeId: 1, + originSlot: 0, + targetNodeId: 2, + targetSlot: 0, + type: 'IMAGE', + targetInputs: [ + { name: 'in', type: 'IMAGE', link: toLinkId(5) }, + { name: 'grown', type: 'IMAGE' } + ] + }) + batch.connect({ + id: 9, + originNodeId: 3, + originSlot: 0, + targetNodeId: 2, + targetSlot: 1, + type: 'IMAGE', + targetInputs: [ + { name: 'in', type: 'IMAGE' }, + { name: 'grown', type: 'IMAGE', link: toLinkId(9) } + ] + }) + }) + ).toBe(true) + + const target = useNodeDataStore() + .getGraphNodesFor('root', 'root') + .find(({ id }) => id === toNodeId(2)) + expect(target?.inputs.map(({ link }) => link)).toEqual([ + toLinkId(5), + toLinkId(9) + ]) + }) + + it('merges input and output updates for self-links within a batch', () => { + const graph = mutations() + expect( + graph.batch(context, (batch) => { + batch.addNode({ + ...node(1), + inputs: [ + { name: 'in-0', type: 'IMAGE', link: null }, + { name: 'in-1', type: 'IMAGE', link: null } + ], + outputs: [{ name: 'out-0', type: 'IMAGE', links: [] }] + }) + batch.connect({ + id: 5, + originNodeId: 1, + originSlot: 0, + targetNodeId: 1, + targetSlot: 0, + type: 'IMAGE', + originOutputs: [ + { name: 'out-0', type: 'IMAGE', links: [toLinkId(5)] }, + { name: 'out-1', type: 'IMAGE', links: [] } + ], + targetInputs: [ + { name: 'in-0', type: 'IMAGE', link: toLinkId(5) }, + { name: 'in-1', type: 'IMAGE', link: null } + ] + }) + batch.connect({ + id: 9, + originNodeId: 1, + originSlot: 1, + targetNodeId: 1, + targetSlot: 1, + type: 'IMAGE', + targetInputs: [ + { name: 'in-0', type: 'IMAGE', link: toLinkId(5) }, + { name: 'in-1', type: 'IMAGE', link: toLinkId(9) } + ] + }) + }) + ).toBe(true) + + const [state] = useNodeDataStore().getGraphNodesFor('root', 'root') + expect(state.inputs.map(({ link }) => link)).toEqual([ + toLinkId(5), + toLinkId(9) + ]) + expect(state.outputs.map(({ links }) => links)).toEqual([[toLinkId(5)], []]) + expect( + useLinkStore().getTopology(scope.rootGraphId, toLinkId(9)) + ).toBeDefined() + }) + + it('does not restore a removed link from an omitted reconciled slot field', () => { + const graph = mutations() + graph.batch(context, (batch) => { + batch.addNode(node(1)) + batch.addNode(node(2)) + batch.connect({ + id: 5, + originNodeId: 1, + originSlot: 0, + targetNodeId: 2, + targetSlot: 0, + type: 'IMAGE', + originOutputs: [{ name: 'out', type: 'IMAGE', links: [toLinkId(5)] }], + targetInputs: [{ name: 'in', type: 'IMAGE', link: toLinkId(5) }] + }) + }) + + expect( + graph.batch({ ...context, opId: 'reconcile' }, (batch) => { + batch.removeMissing([toNodeId(1), toNodeId(2)], []) + batch.reconcileNode({ + ...node(2), + inputs: [{ name: 'in', type: 'IMAGE' }] + }) + }) + ).toBe(true) + + const target = useNodeDataStore() + .getGraphNodesFor('root', 'root') + .find(({ id }) => id === toNodeId(2)) + expect(target?.inputs[0].link).toBeNull() + expect( + useLinkStore().getTopology(scope.rootGraphId, toLinkId(5)) + ).toBeUndefined() + }) + + it('resolves omitted input links against the existing node on reconcile and to null on add', () => { + const graph = mutations() + graph.batch(context, (batch) => { + batch.addNode({ + ...node(1), + inputs: [{ name: 'in', type: 'IMAGE' }] + }) + batch.addNode(node(2)) + batch.connect({ + id: 5, + originNodeId: 2, + originSlot: 0, + targetNodeId: 1, + targetSlot: 0, + type: 'IMAGE', + targetInputs: [{ name: 'in', type: 'IMAGE', link: toLinkId(5) }] + }) + }) + const find = (id: number) => + useNodeDataStore() + .getGraphNodesFor('root', 'root') + .find((state) => state.id === toNodeId(id)) + expect(find(2)?.inputs[0].link).toBeNull() + expect(find(1)?.inputs[0].link).toBe(toLinkId(5)) + + expect( + graph.batch({ ...context, opId: 'op-2' }, (batch) => { + batch.reconcileNode({ + ...node(1), + title: 'Reconciled', + inputs: [ + { name: 'in', type: 'IMAGE' }, + { name: 'added', type: 'IMAGE' } + ] + }) + }) + ).toBe(true) + + expect(find(1)?.title).toBe('Reconciled') + expect(find(1)?.inputs.map(({ link }) => link)).toEqual([toLinkId(5), null]) + }) + it('re-adds a normalized node id as a fresh widget incarnation', () => { const graph = mutations() graph.addNode(node(1, { seed: 1, stale: 'old' }), context) diff --git a/src/core/graph/graphMutations.ts b/src/core/graph/graphMutations.ts index 40a57f5c277..5021eda5e85 100644 --- a/src/core/graph/graphMutations.ts +++ b/src/core/graph/graphMutations.ts @@ -60,6 +60,12 @@ interface SemanticLayoutMutationPort { interface GraphMutationBatch { addNode(payload: SemanticNodePayload): void reconcileNode(payload: SemanticNodePayload): void + /** + * For a node of the same type, resyncs scalar fields while preserving slots, + * widgets, and layout. A missing node or type mismatch is added or replaced + * from the full payload. + */ + reconcileNodeFields(payload: SemanticNodePayload): void setWidget(nodeId: NodeId, name: string, value: unknown): void connect(link: SemanticLinkPayload): void /** Derived cleanup for an authoritative snapshot; not a wire op. */ @@ -102,6 +108,7 @@ export interface GraphMutationsDeps { type QueuedMutation = | { kind: 'addNode'; payload: SemanticNodePayload } | { kind: 'reconcileNode'; payload: SemanticNodePayload } + | { kind: 'reconcileNodeFields'; payload: SemanticNodePayload } | { kind: 'setWidget'; nodeId: NodeId; name: string; value: unknown } | { kind: 'connect'; link: SemanticLinkPayload } | { @@ -126,6 +133,8 @@ interface PreparedNode { type PreparedMutation = | { kind: 'addNode'; node: PreparedNode } | { kind: 'reconcileNode'; node: PreparedNode } + | { kind: 'replaceNode'; node: PreparedNode } + | { kind: 'reconcileNodeFields'; state: NodeState } | { kind: 'setWidget'; nodeId: NodeId; name: string; value: WidgetValue } | { kind: 'connect' @@ -154,11 +163,21 @@ function cloneRecord(value: unknown): Record { return isRecord(value) ? structuredClone(value) : {} } -function prepareInputSlots(value: unknown): NodeState['inputs'] { +/** + * A supplied input slot whose record has no `link` key carries no link + * information (as opposed to `link: null`, which means unlinked). Such slots + * keep the link of the `existing` slot at the same index, or `null` when the + * node has no slot there yet. + */ +function prepareInputSlots( + value: unknown, + existing?: NodeState['inputs'] +): NodeState['inputs'] { if (!Array.isArray(value)) return [] - return value.filter(isRecord).map((raw) => { + return value.filter(isRecord).map((raw, index) => { const slot = structuredClone(raw) if (typeof slot.link === 'number') slot.link = toLinkId(slot.link) + if (slot.link === undefined) slot.link = existing?.[index]?.link ?? null return { ...slot, boundingRect: [0, 0, 0, 0] @@ -224,7 +243,8 @@ function widgetEntries(payload: SemanticNodePayload): PreparedNode['widgets'] { function prepareNode( payload: SemanticNodePayload, - scope: GraphScope + scope: GraphScope, + existing?: NodeState ): PreparedNode { const id = toNodeId(payload.id) const [x, y] = readPair(payload.pos, [0, 0]) @@ -239,7 +259,7 @@ function prepareNode( ? payload.title : payload.type, flags: cloneRecord(payload.flags), - inputs: prepareInputSlots(payload.inputs), + inputs: prepareInputSlots(payload.inputs, existing?.inputs), outputs: prepareOutputSlots(payload.outputs), mode: Number.isInteger(mode) ? mode : 0, properties: cloneRecord(payload.properties) as NodeState['properties'], @@ -286,17 +306,70 @@ function nodeKey(nodeId: NodeId): string { return String(nodeId) } +function detachedLinkSlots( + nodes: Iterable, + topology: LinkTopology +): Map> { + const nodesById = new Map([...nodes].map((node) => [nodeKey(node.id), node])) + const changed = new Map>() + const slotsFor = (node: NodeState) => { + const prior = changed.get(node.id) + if (prior) return prior + const slots = { inputs: node.inputs, outputs: node.outputs } + changed.set(node.id, slots) + return slots + } + + const origin = nodesById.get(nodeKey(topology.originNodeId)) + if (origin?.outputs[topology.originSlot]) { + const slots = slotsFor(origin) + slots.outputs = slots.outputs.map((output, index) => + index === topology.originSlot + ? { + ...output, + links: output.links?.filter((id) => id !== topology.id) ?? null + } + : output + ) + } + + const target = nodesById.get(nodeKey(topology.targetNodeId)) + if (target?.inputs[topology.targetSlot]?.link === topology.id) { + const slots = slotsFor(target) + slots.inputs = slots.inputs.map((input, index) => + index === topology.targetSlot ? { ...input, link: null } : input + ) + } + + return changed +} + function removeIncidentLinks( + nodes: Map, links: Map, nodeId: NodeId ): void { - for (const [id, topology] of links) { + for (const [id, topology] of [...links]) { if (topology.originNodeId === nodeId || topology.targetNodeId === nodeId) { - links.delete(id) + removeSimulatedLink(nodes, links, id) } } } +function removeSimulatedLink( + nodes: Map, + links: Map, + linkId: LinkId +): void { + const topology = links.get(linkId) + if (!topology) return + links.delete(linkId) + for (const [nodeId, slots] of detachedLinkSlots(nodes.values(), topology)) { + const node = nodes.get(nodeKey(nodeId)) + if (node) nodes.set(nodeKey(nodeId), { ...node, ...slots }) + } +} + /** * Builds the graph-scoped composite used by the remote follower. Every batch * is validated against a simulated final store state before its first write; @@ -337,6 +410,24 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { ) } + const validateNodeUpsert = ( + node: PreparedNode, + key: string + ): string | undefined => { + const incumbent = nodeStore.getNode(scope.rootGraphId, node.state.id) + if (incumbent && incumbent.graphId !== scope.owningGraphId) { + return `node id ${key} belongs to graph ${incumbent.graphId}` + } + if ( + node.widgets.some( + ({ name }) => + !isWidgetId(widgetId(scope.rootGraphId, node.state.id, name)) + ) + ) { + return `node ${key} has an invalid widget name` + } + } + const prepared: PreparedMutation[] = [] for (const mutation of queued) { switch (mutation.kind) { @@ -350,26 +441,58 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { ) { return 'addNode requires a payload id and type' } - const node = prepareNode(mutation.payload, scope) + const node = prepareNode( + mutation.payload, + scope, + nodes.get(nodeKey(toNodeId(mutation.payload.id))) + ) const key = nodeKey(node.state.id) - const incumbent = nodeStore.getNode(scope.rootGraphId, node.state.id) - if (incumbent && incumbent.graphId !== scope.owningGraphId) { - return `node id ${key} belongs to graph ${incumbent.graphId}` - } + const existing = nodes.get(key) + const validationError = validateNodeUpsert(node, key) + if (validationError) return validationError if (mutation.kind === 'addNode' && nodes.has(key)) { return `node id ${key} is already registered` } + nodes.set(key, node.state) + widgets.set(key, new Set(node.widgets.map(({ name }) => name))) + prepared.push({ + kind: + mutation.kind === 'reconcileNode' && + existing && + existing.type !== node.state.type + ? 'replaceNode' + : mutation.kind, + node + }) + break + } + case 'reconcileNodeFields': { if ( - node.widgets.some( - ({ name }) => - !isWidgetId(widgetId(scope.rootGraphId, node.state.id, name)) - ) + (typeof mutation.payload.id !== 'string' && + typeof mutation.payload.id !== 'number') || + typeof mutation.payload.type !== 'string' || + mutation.payload.type.length === 0 ) { - return `node ${key} has an invalid widget name` + return 'reconcileNodeFields requires a payload id and type' + } + const key = nodeKey(toNodeId(mutation.payload.id)) + const existing = nodes.get(key) + const node = prepareNode(mutation.payload, scope, existing) + if (!existing || existing.type !== node.state.type) { + const validationError = validateNodeUpsert(node, key) + if (validationError) return validationError + nodes.set(key, node.state) + widgets.set(key, new Set(node.widgets.map(({ name }) => name))) + prepared.push({ + kind: existing ? 'replaceNode' : 'addNode', + node + }) + } else { + node.state.inputs = existing.inputs + node.state.outputs = existing.outputs + nodes.set(key, node.state) + prepared.push({ kind: mutation.kind, state: node.state }) } - nodes.set(key, node.state) - widgets.set(key, new Set(node.widgets.map(({ name }) => name))) - prepared.push({ kind: mutation.kind, node }) break } case 'setWidget': { @@ -422,7 +545,7 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { ? prepareOutputSlots(mutation.link.originOutputs) : origin.outputs const targetInputs = mutation.link.targetInputs - ? prepareInputSlots(mutation.link.targetInputs) + ? prepareInputSlots(mutation.link.targetInputs, target.inputs) : target.inputs if (topology.originSlot >= originOutputs.length) { return `connect origin slot ${topology.originSlot} does not exist` @@ -430,6 +553,20 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { if (topology.targetSlot >= targetInputs.length) { return `connect target slot ${topology.targetSlot} does not exist` } + if (mutation.link.originOutputs) { + nodes.set(nodeKey(topology.originNodeId), { + ...origin, + outputs: originOutputs + }) + } + if (mutation.link.targetInputs) { + const currentTarget = + nodes.get(nodeKey(topology.targetNodeId)) ?? target + nodes.set(nodeKey(topology.targetNodeId), { + ...currentTarget, + inputs: targetInputs + }) + } links.delete(topology.id) for (const [id, incumbent] of links) { if ( @@ -468,12 +605,12 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { for (const id of nodeIds) { nodes.delete(nodeKey(id)) widgets.delete(nodeKey(id)) - removeIncidentLinks(links, id) + removeIncidentLinks(nodes, links, id) } const linkIds = [...links.keys()].filter( (id) => !retainedLinkIds.has(id) ) - for (const id of linkIds) links.delete(id) + for (const id of linkIds) removeSimulatedLink(nodes, links, id) prepared.push({ kind: mutation.kind, nodeIds, linkIds }) break } @@ -488,7 +625,7 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { if (incumbent && incumbent.graphId !== scope.owningGraphId) { return `link id ${id} belongs to graph ${incumbent.graphId}` } - links.delete(id) + removeSimulatedLink(nodes, links, id) linkIds.push(id) } prepared.push({ @@ -519,8 +656,10 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { } nodes.delete(nodeKey(mutation.nodeId)) widgets.delete(nodeKey(mutation.nodeId)) - removeIncidentLinks(links, mutation.nodeId) - for (const id of removedLinkIds) links.delete(id) + removeIncidentLinks(nodes, links, mutation.nodeId) + for (const id of removedLinkIds) { + removeSimulatedLink(nodes, links, id) + } prepared.push({ kind: mutation.kind, nodeId: mutation.nodeId, @@ -546,42 +685,11 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { topology: LinkTopology, context: RemoteMutationContext ): void { - const nodes = new Map( - nodeStore - .getGraphNodesFor(scope.rootGraphId, scope.owningGraphId) - .map((node) => [nodeKey(node.id), node]) + const nodes = nodeStore.getGraphNodesFor( + scope.rootGraphId, + scope.owningGraphId ) - const changed = new Map>() - const slotsFor = (node: NodeState) => { - const prior = changed.get(node.id) - if (prior) return prior - const slots = { inputs: node.inputs, outputs: node.outputs } - changed.set(node.id, slots) - return slots - } - - const origin = nodes.get(nodeKey(topology.originNodeId)) - if (origin?.outputs[topology.originSlot]) { - const slots = slotsFor(origin) - slots.outputs = slots.outputs.map((output, index) => - index === topology.originSlot - ? { - ...output, - links: output.links?.filter((id) => id !== topology.id) ?? null - } - : output - ) - } - - const target = nodes.get(nodeKey(topology.targetNodeId)) - if (target?.inputs[topology.targetSlot]?.link === topology.id) { - const slots = slotsFor(target) - slots.inputs = slots.inputs.map((input, index) => - index === topology.targetSlot ? { ...input, link: null } : input - ) - } - - for (const [nodeId, slots] of changed) { + for (const [nodeId, slots] of detachedLinkSlots(nodes, topology)) { nodeStore.updateNodeSlots(scope, nodeId, slots, context) } } @@ -626,11 +734,16 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { for (const mutation of prepared) { switch (mutation.kind) { case 'addNode': - case 'reconcileNode': { - const existing = nodeStore.getNode( + case 'reconcileNode': + case 'replaceNode': { + let existing = nodeStore.getNode( scope.rootGraphId, mutation.node.state.id ) + if (mutation.kind === 'replaceNode' && existing) { + deleteNode(scope, existing.id, [], context) + existing = undefined + } if (mutation.kind === 'reconcileNode' && existing) { nodeStore.updateNode( scope, @@ -670,6 +783,15 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { } break } + case 'reconcileNodeFields': { + nodeStore.updateNodeFields( + scope, + mutation.state.id, + mutation.state, + context + ) + break + } case 'setWidget': { const id = widgetId(scope.rootGraphId, mutation.nodeId, mutation.name) if (!widgetStore.getWidget(id)) { @@ -779,6 +901,9 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { reconcileNode(payload) { queued.push({ kind: 'reconcileNode', payload }) }, + reconcileNodeFields(payload) { + queued.push({ kind: 'reconcileNodeFields', payload }) + }, setWidget(nodeId, name, value) { queued.push({ kind: 'setWidget', nodeId, name, value }) }, diff --git a/src/core/graph/subgraph/hasPromotedWidgetTarget.ts b/src/core/graph/subgraph/hasPromotedWidgetTarget.ts new file mode 100644 index 00000000000..efc207fc9b1 --- /dev/null +++ b/src/core/graph/subgraph/hasPromotedWidgetTarget.ts @@ -0,0 +1,45 @@ +interface PromotedWidgetExpansion { + hasWidget: boolean + nested: readonly T[] +} + +export function hasPromotedWidgetTarget( + root: T, + maxDepth: number, + keyOf: (value: T) => unknown, + expand: (value: T) => PromotedWidgetExpansion +): boolean { + const active = new Set() + const resolved = new Map() + + const visit = ( + value: T, + depth: number + ): { found: boolean; truncated: boolean } => { + const key = keyOf(value) + const settled = resolved.get(key) + if (settled !== undefined) return { found: settled, truncated: false } + if (active.has(key)) return { found: false, truncated: false } + active.add(key) + + const { hasWidget, nested } = expand(value) + let found = hasWidget + let truncated = false + for (const child of nested) { + if (found) break + if (depth >= maxDepth) { + truncated = true + continue + } + const result = visit(child, depth + 1) + found = result.found + truncated ||= result.truncated + } + + active.delete(key) + if (found || !truncated) resolved.set(key, found) + return { found, truncated } + } + + return visit(root, 0).found +} diff --git a/src/core/graph/subgraph/resolveConcretePromotedWidget.ts b/src/core/graph/subgraph/resolveConcretePromotedWidget.ts index e4f641702a4..7a96b1d569c 100644 --- a/src/core/graph/subgraph/resolveConcretePromotedWidget.ts +++ b/src/core/graph/subgraph/resolveConcretePromotedWidget.ts @@ -8,6 +8,8 @@ import { createNodeExecutionId } from '@/types/nodeIdentification' import { toNodeId } from '@/types/nodeId' import type { NodeId, SerializedNodeId } from '@/types/nodeId' +import { hasPromotedWidgetTarget } from './hasPromotedWidgetTarget' + type PromotedWidgetResolutionFailure = | 'invalid-host' | 'cycle' @@ -27,56 +29,46 @@ function isNodeActive(node: LGraphNode): boolean { ) } -function hasActiveConsumer( - hostNode: SubgraphNode, - inputName: string, - depth: number, - visitedByHost: WeakMap> +export function hasActivePromotedWidgetConsumer( + hostNode: LGraphNode, + inputName: string ): boolean { - if (depth >= MAX_PROMOTED_WIDGET_CHAIN_DEPTH) return false - - const visited = visitedByHost.get(hostNode) ?? new Set() - if (visited.has(inputName)) return false - visited.add(inputName) - visitedByHost.set(hostNode, visited) - - const inputSlot = hostNode.subgraph.inputNode.slots.find( + if (!hostNode.isSubgraphNode()) return false + const input = hostNode.subgraph.inputNode.slots.find( (slot) => slot.name === inputName ) - if (!inputSlot) return false - - for (const linkId of inputSlot.linkIds) { - const link = hostNode.subgraph.getLink(linkId) - if (!link) continue - - const { inputNode } = link.resolve(hostNode.subgraph) - if (!inputNode || !isNodeActive(inputNode)) continue - const targetInput = inputNode.inputs.find((entry) => entry.link === linkId) - if (!targetInput) continue - - if (inputNode.isSubgraphNode()) { - if ( - targetInput.widgetId && - hasActiveConsumer(inputNode, targetInput.name, depth + 1, visitedByHost) - ) { - return true + if (!input) return false + const root = { host: hostNode, input } + + return hasPromotedWidgetTarget( + root, + MAX_PROMOTED_WIDGET_CHAIN_DEPTH, + ({ input }) => input, + ({ host, input }) => { + const nested: Array = [] + let hasWidget = false + for (const linkId of input.linkIds) { + const link = host.subgraph.getLink(linkId) + if (!link) continue + const { inputNode } = link.resolve(host.subgraph) + if (!inputNode || !isNodeActive(inputNode)) continue + const targetInput = inputNode.inputs.find( + (entry) => entry.link === linkId + ) + if (!targetInput) continue + if (!inputNode.isSubgraphNode()) { + hasWidget ||= Boolean(inputNode.getWidgetFromSlot(targetInput)) + continue + } + const nestedInput = inputNode.subgraph.inputNode.slots.find( + (slot) => slot.name === targetInput.name + ) + if (targetInput.widgetId && nestedInput) { + nested.push({ host: inputNode, input: nestedInput }) + } } - continue + return { hasWidget, nested } } - - if (inputNode.getWidgetFromSlot(targetInput)) return true - } - - return false -} - -export function hasActivePromotedWidgetConsumer( - hostNode: LGraphNode, - inputName: string -): boolean { - return ( - hostNode.isSubgraphNode() && - hasActiveConsumer(hostNode, inputName, 0, new WeakMap()) ) } diff --git a/src/stores/nodeDataStore.test.ts b/src/stores/nodeDataStore.test.ts index 108be3f96b2..f9d256c6d36 100644 --- a/src/stores/nodeDataStore.test.ts +++ b/src/stores/nodeDataStore.test.ts @@ -10,7 +10,11 @@ import { toOwningGraphId, toRootGraphId } from '@/types/graphScopeId' import type { GraphScope } from '@/types/graphScopeId' import type { NodeState } from '@/types/nodeState' import { toNodeId } from '@/types/nodeId' -import { createNodeState } from '@/utils/__tests__/litegraphTestUtils' +import { + createMockNodeInputSlot, + createMockNodeOutputSlot, + createNodeState +} from '@/utils/__tests__/litegraphTestUtils' import type { UUID } from '@/utils/uuid' import { useNodeDataStore } from './nodeDataStore' @@ -133,6 +137,65 @@ describe('useNodeDataStore', () => { expect(store.getGraphNodesFor(rootA, rootA)).toEqual([]) expect(store.getGraphNodesFor(rootA, 'sub-1')).toEqual([reRegistered]) }) + + it('updateNodeFields replaces scalar fields but never the slot arrays', () => { + const store = useNodeDataStore() + const scope = graphScope(rootA, rootA) + const registered = store.registerNode( + scope, + createNodeState({ + id: toNodeId(1), + graphId: rootA, + title: 'Host', + color: '#111', + inputs: [createMockNodeInputSlot({ name: 'in', type: 'IMAGE' })], + outputs: [createMockNodeOutputSlot({ name: 'out', type: 'IMAGE' })] + }) + ) + assert(registered) + const liveInputs = registered.inputs + const liveOutputs = registered.outputs + const [liveInput] = liveInputs + + expect( + store.updateNodeFields( + scope, + registered.id, + createNodeState({ + id: toNodeId(1), + graphId: rootA, + title: 'Renamed', + mode: 2, + properties: { source: 'doc' }, + // Different slot shape on purpose: it must be ignored entirely. + inputs: [ + createMockNodeInputSlot({ name: 'in', type: 'IMAGE' }), + createMockNodeInputSlot({ name: 'extra', type: 'MASK' }) + ], + outputs: [] + }) + ) + ).toBe(true) + + expect(registered.title).toBe('Renamed') + expect(registered.mode).toBe(2) + expect(registered.properties).toEqual({ source: 'doc' }) + expect(registered.color).toBeUndefined() + expect(registered.inputs).toBe(liveInputs) + expect(registered.outputs).toBe(liveOutputs) + expect(registered.inputs).toHaveLength(1) + expect(registered.inputs[0]).toBe(liveInput) + expect(registered.outputs).toHaveLength(1) + + expect( + store.updateNodeFields( + graphScope(rootA, 'sub-1'), + registered.id, + createNodeState({ id: toNodeId(1), graphId: 'sub-1', title: 'Wrong' }) + ) + ).toBe(false) + expect(registered.title).toBe('Renamed') + }) }) describe('nodeDataStore registration via LGraph', () => { diff --git a/src/stores/nodeDataStore.ts b/src/stores/nodeDataStore.ts index 1b19a4ad9a7..5d991493ca1 100644 --- a/src/stores/nodeDataStore.ts +++ b/src/stores/nodeDataStore.ts @@ -147,6 +147,29 @@ export const useNodeDataStore = defineStore('nodeData', () => { state.inputs.splice(0, state.inputs.length, ...replacement.inputs) state.outputs.splice(0, state.outputs.length, ...replacement.outputs) + assignNodeFields(state, replacement) + return true + } + + /** + * Replaces every scalar field of a node from `replacement` while leaving + * its `inputs` and `outputs` arrays untouched. Used when the slot layout is + * owned elsewhere (e.g. a live subgraph host whose promoted slots are bound + * to widgets) and only title/mode/flags/properties/colors may change. + */ + function updateNodeFields( + graphScope: GraphScope, + nodeId: NodeId, + replacement: NodeState, + _context?: RemoteMutationContext + ): boolean { + const state = roots.get(graphScope.rootGraphId)?.byId.get(nodeId) + if (!state || state.graphId !== graphScope.owningGraphId) return false + assignNodeFields(state, replacement) + return true + } + + function assignNodeFields(state: NodeState, replacement: NodeState): void { const { graphId: _graphId, id: _id, @@ -165,7 +188,6 @@ export const useNodeDataStore = defineStore('nodeData', () => { titleMode: undefined, ...next } satisfies Omit) - return true } function clearGraph(rootGraphId: UUID): void { @@ -193,6 +215,7 @@ export const useNodeDataStore = defineStore('nodeData', () => { ownsNode, registerNode, updateNode, + updateNodeFields, updateNodeSlots } }) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts index 24991fed87a..a2472e3f132 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.ts @@ -18,6 +18,7 @@ import type { NodeState } from '@/types/nodeState' import { widgetId } from '@/types/widgetId' import type { WidgetStateInit } from '@/types/widgetState' +import { allSubgraphDefinitions } from './agentSubgraphDefinitions' import { runMintPortsSuppressed } from './mintPortWiring' export type MaterializableGraph = Pick< @@ -91,9 +92,9 @@ function registerSubgraphDefinitions( // Filter after flattening: a live nested definition must not be recreated // just because its outer is missing, and a missing nested definition must // still register when its outer is already live. - const missing = flattenDefinitions(definitions).filter( - (definition) => !rootGraph.subgraphs.has(definition.id) - ) + const missing = allSubgraphDefinitions(definitions) + .map((definition) => ({ ...definition, definitions: undefined })) + .filter((definition) => !rootGraph.subgraphs.has(definition.id)) const pending = new Set(missing.map((definition) => definition.id)) if (missing.length === 0) return pending @@ -162,19 +163,6 @@ function tryCreateSubgraph( } } -/** - * Each definition plus every definition nested under its `definitions`, with - * the nesting stripped so each one registers on its own. - */ -function flattenDefinitions( - definitions: ExportedSubgraph[] -): ExportedSubgraph[] { - return definitions.flatMap((definition) => [ - { ...definition, definitions: undefined }, - ...flattenDefinitions(definition.definitions?.subgraphs ?? []) - ]) -} - /** * Run `fn` with `LGraphNode.configure()` honouring `widgets_values_named`. * diff --git a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts index 94cb0ec9fcc..fb62a0e3945 100644 --- a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts +++ b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts @@ -68,6 +68,14 @@ describe('readSubgraphDefinitions', () => { expect(doc.share.has('definitions')).toBe(false) }) + it('ignores a definitions root with the wrong shared type', () => { + const doc = new Y.Doc() + doc.getArray('definitions') + + expect(readSubgraphDefinitionIds(doc)).toEqual([]) + expect(readSubgraphDefinitions(doc)).toEqual([]) + }) + it('projects a definition back to the shape it was minted from', () => { const definition = createTestSubgraphData({ nodes: [interiorNode(3), interiorNode(1)] as never, @@ -173,6 +181,40 @@ describe('readSubgraphDefinitions', () => { expect(projected[0]?.nodes?.map((node) => node.id)).toEqual([1]) }) + it.for<[string, (definition: Y.Map) => void]>([ + ['inputs', (stored) => stored.set('inputs', 'invalid')], + [ + 'input entry', + (stored) => { + const inputs = new Y.Array() + stored.set('inputs', inputs) + inputs.push([null]) + } + ], + ['nodes', (stored) => stored.set('nodes', 'invalid')], + ['links', (stored) => stored.set('links', 'invalid')], + ['definitions', (stored) => stored.set('definitions', 'invalid')], + [ + 'nested definition', + (stored) => { + const definitions = new Y.Map() + const subgraphs = new Y.Array() + stored.set('definitions', definitions) + definitions.set('subgraphs', subgraphs) + subgraphs.push([null]) + } + ] + ])('skips a definition with invalid %s', ([_label, mutate]) => { + const definition = createTestSubgraphData() + const doc = seed(definition) + const stored = doc + .getMap('definitions') + .get(definition.id) as Y.Map + mutate(stored) + + expect(readSubgraphDefinitions(doc)).toEqual([]) + }) + it('reads a node named twice in the order register once', () => { // mintDefinition pushes one register entry per input node, so two interior // nodes sharing an id leave a two-entry register over a one-key map. diff --git a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts index 506ba10dfad..1332b6024bb 100644 --- a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts +++ b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts @@ -81,7 +81,61 @@ function readInteriorNode(source: unknown): Record | null { return node } -function readDefinition(source: Y.Map): ExportedSubgraph { +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +function hasSafeInputs(value: unknown): boolean { + return ( + value === undefined || + (Array.isArray(value) && + value.every( + (input) => + isRecord(input) && + typeof input.name === 'string' && + (input.linkIds === undefined || Array.isArray(input.linkIds)) + )) + ) +} + +function hasSafeNodes(value: unknown): boolean { + return ( + value === undefined || + (Array.isArray(value) && + value.every( + (node) => + isRecord(node) && + (node.inputs === undefined || + (Array.isArray(node.inputs) && node.inputs.every(isRecord))) + )) + ) +} + +function hasSafeLinks(value: unknown): boolean { + return value === undefined || (Array.isArray(value) && value.every(isRecord)) +} + +function hasSafeNestedDefinitions(value: unknown): boolean { + if (value === undefined) return true + if (!isRecord(value)) return false + const nested = value.subgraphs + return ( + nested === undefined || + (Array.isArray(nested) && nested.every(isSafeDefinition)) + ) +} + +function isSafeDefinition(value: unknown): boolean { + return ( + isRecord(value) && + hasSafeInputs(value.inputs) && + hasSafeNodes(value.nodes) && + hasSafeLinks(value.links) && + hasSafeNestedDefinitions(value.definitions) + ) +} + +function readDefinition(source: Y.Map): ExportedSubgraph | null { const definition: Record = {} source.forEach((value, key) => { if (key === NODE_ORDER || key === LINK_ORDER || !isReadableKey(key)) return @@ -100,7 +154,9 @@ function readDefinition(source: Y.Map): ExportedSubgraph { definition[key] = plain(value) } }) - return definition as unknown as ExportedSubgraph + return isSafeDefinition(definition) + ? (definition as unknown as ExportedSubgraph) + : null } function readField(source: unknown, key: string): unknown { @@ -125,10 +181,30 @@ function collectDefinitionIds(source: unknown, ids: string[]): void { } } +function definitionsMap(doc: Y.Doc): Y.Map | null { + const root = doc.share.get(DEFINITIONS_ROOT) + if (!root) return null + if (root instanceof Y.Map) return root + if (root.constructor !== Y.AbstractType) return null + return doc.getMap(DEFINITIONS_ROOT) +} + +export function allSubgraphDefinitions( + definitions: readonly ExportedSubgraph[] +): ExportedSubgraph[] { + return [ + ...definitions, + ...definitions.flatMap((definition) => + allSubgraphDefinitions(definition.definitions?.subgraphs ?? []) + ) + ] +} + export function readSubgraphDefinitionIds(doc: Y.Doc): string[] { const ids: string[] = [] - if (!doc.share.has(DEFINITIONS_ROOT)) return ids - doc.getMap(DEFINITIONS_ROOT).forEach((value) => { + const root = definitionsMap(doc) + if (!root) return ids + root.forEach((value) => { if (value instanceof Y.Map) collectDefinitionIds(value, ids) }) return ids @@ -144,13 +220,12 @@ export function readSubgraphDefinitionIds(doc: Y.Doc): string[] { */ export function readSubgraphDefinitions(doc: Y.Doc): ExportedSubgraph[] { const definitions: ExportedSubgraph[] = [] - // `doc.getMap` defines the root when it is absent. A document that never - // seeded definitions must keep its shape, so only read a root that exists. - // (For a root that arrived over the wire, `getMap` upgrades the untyped - // shared type in place; that is a read-side view, not new content.) - if (!doc.share.has(DEFINITIONS_ROOT)) return definitions - doc.getMap(DEFINITIONS_ROOT).forEach((value) => { - if (value instanceof Y.Map) definitions.push(readDefinition(value)) + const root = definitionsMap(doc) + if (!root) return definitions + root.forEach((value) => { + if (!(value instanceof Y.Map)) return + const definition = readDefinition(value) + if (definition) definitions.push(definition) }) return definitions } diff --git a/src/workbench/extensions/agent/crdt/agentSubgraphFollower.test.ts b/src/workbench/extensions/agent/crdt/agentSubgraphFollower.test.ts new file mode 100644 index 00000000000..0509a267168 --- /dev/null +++ b/src/workbench/extensions/agent/crdt/agentSubgraphFollower.test.ts @@ -0,0 +1,1083 @@ +import { + OPAQUE_WIDGETS_KEY, + applyOps, + linksMap, + mint, + nodesMap +} from '@comfyorg/comfy-multi-player' +import type { + Op, + WidgetCatalog, + WorkflowJSON +} from '@comfyorg/comfy-multi-player' +import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' +import * as Y from 'yjs' + +import { createGraphMutations } from '@/core/graph/graphMutations' +import { + LGraph, + LGraphNode, + LiteGraph, + SubgraphNode +} from '@/lib/litegraph/src/litegraph' +import { + createTestSubgraph, + createTestSubgraphNode, + enableSubgraphNodeCreation +} from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' +import { reportError } from '@/platform/telemetry/reportError' +import { useWidgetValueStore } from '@/stores/widgetValueStore' +import { graphScopeOf } from '@/types/graphScopeId' +import { toLinkId } from '@/types/linkId' +import { toNodeId } from '@/types/nodeId' + +import { reconcileAgentAdapters } from './agentNodeMaterializer' +import { readSubgraphDefinitions } from './agentSubgraphDefinitions' +import { EcsFollowerAdapter } from './ecsFollowerAdapter' +import { FollowerDoc } from './followerDoc' +import type { GraphOperation } from './graphOperations' + +vi.mock(import('@/platform/telemetry/reportError'), () => ({ + reportError: vi.fn() +})) + +class PromotedWidgetNode extends LGraphNode { + constructor() { + super('promoted-widget') + const input = this.addInput('value', 'NUMBER') + input.widget = { name: 'value' } + this.addWidget('number', 'value', 1, () => {}) + } +} + +class SourceNode extends LGraphNode { + constructor() { + super('source') + this.addOutput('value', 'NUMBER') + } +} + +const INTERIOR_DEFAULT_VALUE = 1 +const HOST_INITIAL_VALUE = 3 + +const CATALOG: WidgetCatalog = { + types: { + 'promoted-widget': { widget_order: ['value'] }, + source: { widget_order: [] } + } +} + +function operation(id: string, version: number, payload: GraphOperation): Op { + return { + op_id: id, + actor: 'agent:test', + base_version: version, + stamp: [version, 'agent:test'], + ...payload + } +} + +interface FixtureOptions { + /** Declare a second input `extra` ahead of `value` in the definition. */ + extraInput?: boolean + /** Strip the host instance's serialized inputs (cmp claimPromotedInput premise). */ + stripHostInputs?: boolean + /** + * Serialize the host with `widgets_values: []`. cmp mints such a host with + * an empty named `widgets` map and converts it to opaque storage on the + * first promoted write (`hostWriteStorage`), deleting `widgets` and setting + * `__widgets_opaque` in one transaction. + */ + emptyHostWidgets?: boolean + /** Add a root-level (non-host) `promoted-widget` node with id 3. */ + rootWidgetNode?: boolean + /** + * Register a second, unused subgraph definition (same `value` input and a + * promoted-widget interior) so a host can be retyped between definitions. + */ + secondDefinition?: boolean + /** + * Mutable gate for the adapter's `getScope`. While `blocked` is true the + * scope resolves to `null`, so `graphMutations.batch` rejects the frame + * (the "no active graph scope" failure the adapter must survive). + */ + scope?: { blocked: boolean } +} + +function promotedWorkflow(options: FixtureOptions = {}): WorkflowJSON { + const graph = new LGraph() + const subgraph = createTestSubgraph({ + rootGraph: graph, + inputs: options.extraInput + ? [ + { name: 'extra', type: 'NUMBER' }, + { name: 'value', type: 'NUMBER' } + ] + : [{ name: 'value', type: 'NUMBER' }] + }) + // createTestSubgraph never registers on the root; serialize() only emits + // definitions for subgraphs present in graph.subgraphs (LGraph.ts ~2761). + graph.subgraphs.set(subgraph.id, subgraph) + // `new PromotedWidgetNode()` leaves `type` unset; only `createNode` stamps + // it, and cmp's mint drops nodes whose serialized `type` is empty. + const interior = LiteGraph.createNode('promoted-widget')! + interior.id = toNodeId(7) + subgraph.add(interior) + const valueSlot = subgraph.inputNode.slots[options.extraInput ? 1 : 0] + valueSlot.connect(interior.inputs[0], interior) + + if (options.secondDefinition) { + const second = createTestSubgraph({ + rootGraph: graph, + inputs: [{ name: 'value', type: 'NUMBER' }] + }) + graph.subgraphs.set(second.id, second) + const secondInterior = LiteGraph.createNode('promoted-widget')! + secondInterior.id = toNodeId(8) + second.add(secondInterior) + second.inputNode.slots[0].connect(secondInterior.inputs[0], secondInterior) + // cmp's mint keeps only definitions some node instantiates, so the second + // definition needs its own host (id 4) to survive into the doc. + graph.add(createTestSubgraphNode(second, { id: 4 })) + } + + const host = createTestSubgraphNode(subgraph, { id: 1 }) + graph.add(host) + // Host value differs from the interior default (1) so an initial load that + // silently falls back to the definition's default is caught. + host.widgets[0].value = HOST_INITIAL_VALUE + + const source = LiteGraph.createNode('source')! + source.id = toNodeId(2) + graph.add(source) + if (options.rootWidgetNode) { + const rootWidget = LiteGraph.createNode('promoted-widget')! + rootWidget.id = toNodeId(3) + graph.add(rootWidget) + } + const serialized = graph.serialize() + const hostNode = serialized.nodes.find((n) => n.id === 1) + if (options.stripHostInputs && hostNode) hostNode.inputs = [] + if (options.emptyHostWidgets && hostNode) hostNode.widgets_values = [] + // Same cast the production path takes: serialized litegraph JSON is the + // workflow shape cmp mints from. + return serialized as unknown as WorkflowJSON +} + +function startFollower(options: FixtureOptions = {}) { + const graph = new LGraph() + const disableSubgraphNodeCreation = enableSubgraphNodeCreation(graph) + const hostDoc = mint(promotedWorkflow(options), CATALOG) + const follower = new FollowerDoc() + const adapter = new EcsFollowerAdapter( + createGraphMutations({ + getScope: () => (options.scope?.blocked ? null : graphScopeOf(graph)), + layout: { createNode: () => {}, deleteNodes: () => {} } + }) + ) + adapter.bind('workflow', follower) + const update = Y.encodeStateAsUpdate(hostDoc) + follower.applyRemoteUpdate(update) + expect(adapter.applyFrame({ workflowId: 'workflow', seq: 1, update })).toBe( + true + ) + reconcileAgentAdapters(graph, readSubgraphDefinitions(follower.doc)) + const instance = graph.getNodeById(toNodeId(1)) as SubgraphNode + expect(instance).toBeInstanceOf(SubgraphNode) + expect(instance.widgets[0]?.value).toBe( + options.emptyHostWidgets ? INTERIOR_DEFAULT_VALUE : HOST_INITIAL_VALUE + ) + expect(instance.inputs.map((i) => i.name)).toEqual( + options.extraInput ? ['extra', 'value'] : ['value'] + ) + onTestFinished(disableSubgraphNodeCreation) + return { + graph, + hostDoc, + follower, + adapter, + instance, + disableSubgraphNodeCreation + } +} + +function deliver( + state: ReturnType, + payload: GraphOperation, + seq: number +) { + const vector = Y.encodeStateVector(state.hostDoc) + const id = `op-${seq}` + const result = applyOps(state.hostDoc, [operation(id, seq, payload)], CATALOG) + expect(result.outcomes).toEqual([{ op_id: id, outcome: 'applied' }]) + const update = Y.encodeStateAsUpdate(state.hostDoc, vector) + state.follower.applyRemoteUpdate(update) + expect( + state.adapter.applyFrame({ + workflowId: 'workflow', + seq: seq + 1, + update, + actor: 'agent:test', + opIds: [id] + }) + ).toBe(true) + reconcileAgentAdapters( + state.graph, + readSubgraphDefinitions(state.follower.doc) + ) +} + +/** + * Forward a raw Y.Doc mutation (no cmp op) to the follower. Models doc shapes + * cmp's validated op path never produces, so the follower's defensive + * branches are exercised directly. + */ +function forwardRaw( + state: ReturnType, + mutate: (nodes: ReturnType) => void, + seq: number +) { + const vector = Y.encodeStateVector(state.hostDoc) + state.hostDoc.transact(() => mutate(nodesMap(state.hostDoc))) + const update = Y.encodeStateAsUpdate(state.hostDoc, vector) + state.follower.applyRemoteUpdate(update) + expect( + state.adapter.applyFrame({ workflowId: 'workflow', seq: seq + 1, update }) + ).toBe(true) + reconcileAgentAdapters( + state.graph, + readSubgraphDefinitions(state.follower.doc) + ) +} + +beforeEach(() => { + LiteGraph.registerNodeType('promoted-widget', PromotedWidgetNode) + LiteGraph.registerNodeType('source', SourceNode) +}) + +describe('agent CRDT follower on a SubgraphNode with promoted widgets', () => { + it('S1 reflects a promoted host widgets_values write on the surface widget', () => { + const state = startFollower() + deliver( + state, + { + op: 'set_widget', + node_id: 1, + widget: 'value', + value: 42, + promoted: { + instance_path: [1], + value_index: 0, + host_widgets_values: [HOST_INITIAL_VALUE] + } + }, + 1 + ) + + // The host owns the promoted value (ADR-SUBGRAPH-PROMOTION-0009): + // execution reads it from the widget value store, not from the interior + // node's widget, so the interior default is intentionally untouched. + const widgetId = state.instance.inputs[0]?.widgetId + expect(widgetId).toBeDefined() + expect(state.instance.widgets[0]?.value).toBe(42) + expect(useWidgetValueStore().getWidget(widgetId!)?.value).toBe(42) + }) + + it('S1b keeps the promoted widget when cmp retires the empty named map in the same transaction', () => { + // cmp `applyPromotedHostWrite` deletes the host's empty `widgets` Y.Map and + // sets `__widgets_opaque` in ONE transaction. The follower must treat that + // as an opaque promoted write, not as a named-map replacement that + // reconciles the host back to definition defaults. + const state = startFollower({ emptyHostWidgets: true }) + deliver( + state, + { + op: 'set_widget', + node_id: 1, + widget: 'value', + value: 42, + promoted: { + instance_path: [1], + value_index: 0, + host_widgets_values: [INTERIOR_DEFAULT_VALUE] + } + }, + 1 + ) + + const widgetId = state.instance.inputs[0]?.widgetId + expect(widgetId).toBeDefined() + expect(state.instance.widgets.map((w) => w.name)).toEqual(['value']) + expect(state.instance.widgets[0]?.value).toBe(42) + expect(useWidgetValueStore().getWidget(widgetId!)?.value).toBe(42) + }) + + it('S2 materializes and connects a declared promoted host input', () => { + const state = startFollower() + deliver( + state, + { + op: 'connect', + link_id: 9, + from_node: 2, + from_slot: 0, + to_node: 1, + link_type: 'NUMBER', + grow: { + side: 'input', + slot: 'value', + name: 'value', + type: 'NUMBER', + promoted: true + } + }, + 1 + ) + + expect(state.graph.links.has(toLinkId(9))).toBe(true) + expect(state.instance.inputs[0]?.link).toBe(9) + expect(state.graph.getNodeById(toNodeId(2))?.outputs[0]?.links).toContain(9) + }) + + it('S2b wires the promoted input by declared name when the doc host carries no slots', () => { + const state = startFollower({ extraInput: true, stripHostInputs: true }) + deliver( + state, + { + op: 'connect', + link_id: 9, + from_node: 2, + from_slot: 0, + to_node: 1, + link_type: 'NUMBER', + grow: { + side: 'input', + slot: 'value', + name: 'value', + type: 'NUMBER', + promoted: true + } + }, + 1 + ) + + const valueInput = state.instance.inputs.find((i) => i.name === 'value') + const extraInput = state.instance.inputs.find((i) => i.name === 'extra') + expect(state.graph.links.has(toLinkId(9))).toBe(true) + expect({ + target_slot: state.graph.links.get(toLinkId(9))?.target_slot, + valueLink: valueInput?.link ?? null, + extraLink: extraInput?.link ?? null + }).toEqual({ + target_slot: state.instance.inputs.indexOf(valueInput!), + valueLink: 9, + extraLink: null + }) + }) + + it('ignores malformed host input entries without aborting reconciliation', () => { + const state = startFollower() + + forwardRaw( + state, + (nodes) => { + const host = nodes.get('1')!.toJSON() as Record + const replacement = new Y.Map() + for (const [key, value] of Object.entries(host)) { + replacement.set(key, value) + } + const inputs = new Y.Array() + replacement.set('inputs', inputs) + nodes.set('1', replacement) + inputs.push([ + null, + { name: 42 }, + { name: 'value', type: 'NUMBER', link: null } + ]) + }, + 1 + ) + + expect(state.instance.inputs.map((input) => input.name)).toEqual(['value']) + }) + + it('S2e keeps a live host link when the doc host slot list omits that declared slot', () => { + // cmp writes host slots only as `grow` claims them, so a doc host's slot + // list can be a strict subset of the definition's declared inputs while a + // link to an omitted slot is still live. Synthesizing the omitted slot as + // `link: null` would sever it on the next node change while the link map + // still holds it; the follower must keep the existing live link instead. + const state = startFollower({ extraInput: true }) + deliver( + state, + { + op: 'connect', + link_id: 8, + from_node: 2, + from_slot: 0, + to_node: 1, + to_slot: 0, + link_type: 'NUMBER' + }, + 1 + ) + const extraInput = () => + state.instance.inputs.find((i) => i.name === 'extra') + expect(extraInput()?.link).toBe(8) + + // Replace the whole host map (an `update` action the follower processes) + // rather than splicing the nested inputs array, whose Y.Array event the + // node observer ignores and which would leave this phase a no-op. + forwardRaw( + state, + (nodes) => { + const host = nodes.get('1')!.toJSON() as Record + const inputs = host.inputs as { name: string }[] + expect(inputs.map((i) => i.name)).toContain('extra') + const replacement = new Y.Map() + for (const [key, value] of Object.entries(host)) { + replacement.set(key, value) + } + replacement.set( + 'inputs', + inputs.filter((i) => i.name !== 'extra') + ) + nodes.set('1', replacement) + }, + 2 + ) + + expect(state.graph.links.has(toLinkId(8))).toBe(true) + expect(state.instance.inputs.map((i) => [i.name, i.link ?? null])).toEqual([ + ['extra', 8], + ['value', null] + ]) + }) + + it('S2c skips a promoted connect whose slot name the definition does not declare', () => { + // cmp `claimPromotedInput` never validates `grow.name` against the + // definition: it appends a `{name: 'bogus'}` slot to the doc host. With the + // doc host otherwise slot-less, that slot sits at doc index 0, which is the + // live `extra` input. A positional fallback would silently wire the link + // onto the wrong input; the follower must refuse instead. + const state = startFollower({ extraInput: true, stripHostInputs: true }) + deliver( + state, + { + op: 'connect', + link_id: 9, + from_node: 2, + from_slot: 0, + to_node: 1, + link_type: 'NUMBER', + grow: { + side: 'input', + slot: 'bogus', + name: 'bogus', + type: 'NUMBER', + promoted: true + } + }, + 1 + ) + + expect(state.graph.links.has(toLinkId(9))).toBe(false) + expect(state.instance.inputs.map((i) => [i.name, i.link ?? null])).toEqual([ + ['extra', null], + ['value', null] + ]) + expect( + state.graph.getNodeById(toNodeId(2))?.outputs[0]?.links ?? [] + ).toEqual([]) + // The doc keeps link 9 while the live graph dropped it, so the drift must + // be surfaced rather than silently ignored. + expect(reportError).toHaveBeenCalledTimes(1) + expect(reportError).toHaveBeenCalledWith( + expect.objectContaining({ + message: expect.stringContaining("'bogus'") + }), + expect.objectContaining({ + errorType: 'error_reconciling_agent_subgraph_host_slot', + context: expect.objectContaining({ + nodeId: '1', + slot: 0, + name: 'bogus' + }) + }) + ) + }) + + it('S2f reports an undeclared promoted slot once per session across frames', () => { + // The undeclared link stays in the doc, so any later frame that re-reads + // it (here: the link entry is rewritten) hits the same refusal. Report the + // drift once per session rather than once per frame. + const state = startFollower({ extraInput: true, stripHostInputs: true }) + deliver( + state, + { + op: 'connect', + link_id: 9, + from_node: 2, + from_slot: 0, + to_node: 1, + link_type: 'NUMBER', + grow: { + side: 'input', + slot: 'bogus', + name: 'bogus', + type: 'NUMBER', + promoted: true + } + }, + 1 + ) + forwardRaw( + state, + () => { + const links = linksMap(state.hostDoc) + links.set('9', structuredClone(links.get('9'))) + }, + 2 + ) + + expect(state.graph.links.has(toLinkId(9))).toBe(false) + expect(reportError).toHaveBeenCalledTimes(1) + }) + + it('S2d removes a live promoted link when the same link id is retargeted onto an undeclared slot', () => { + // cmp `claimLinkIdentity` deletes and re-mints link 9 under the new slot in + // one op, so the doc still holds id 9 but the follower can no longer read + // it (undeclared name). A changed link that is present in the doc yet + // unreadable must be retired live, not left connected to its old slot. + const state = startFollower() + const connect = { + op: 'connect' as const, + link_id: 9, + from_node: 2, + from_slot: 0, + to_node: 1, + link_type: 'NUMBER' + } + deliver( + state, + { + ...connect, + grow: { + side: 'input', + slot: 'value', + name: 'value', + type: 'NUMBER', + promoted: true + } + }, + 1 + ) + expect(state.instance.inputs[0]?.link).toBe(9) + + deliver( + state, + { + ...connect, + grow: { + side: 'input', + slot: 'bogus', + name: 'bogus', + type: 'NUMBER', + promoted: true + } + }, + 2 + ) + + expect(state.graph.links.has(toLinkId(9))).toBe(false) + expect(state.instance.inputs.map((i) => [i.name, i.link ?? null])).toEqual([ + ['value', null] + ]) + expect( + state.graph.getNodeById(toNodeId(2))?.outputs[0]?.links ?? [] + ).toEqual([]) + }) + + it('S1c keeps promoted values and reports drift when the opaque array is longer', () => { + // The definition declares one promoted name. cmp writes the whole + // positional array against that same promoted list, so a longer array + // means writer and reader disagree on the host surface. Mapping the first + // value positionally could land it on the wrong widget; the host keeps + // its current value and the mismatch is reported. + const state = startFollower() + deliver(state, hostSetWidget(44), 1) + forwardRaw( + state, + (nodes) => nodes.get('1')!.set(OPAQUE_WIDGETS_KEY, [42, 'extra']), + 2 + ) + + const widgetId = state.instance.inputs[0]?.widgetId + expect(widgetId).toBeDefined() + expect(storedHostWidgets(state)).toEqual([['value', 44]]) + expect(state.instance.widgets.map((w) => w.name)).toEqual(['value']) + expect(state.instance.widgets[0]?.value).toBe(44) + expect(useWidgetValueStore().getWidget(widgetId!)?.value).toBe(44) + expect(reportError).toHaveBeenCalledTimes(1) + expect(reportError).toHaveBeenCalledWith( + expect.objectContaining({ + message: expect.stringContaining('carries 2 opaque widget values') + }), + expect.objectContaining({ + errorType: 'error_reconciling_agent_subgraph_host_widgets', + context: expect.objectContaining({ expected: 1, actual: 2 }) + }) + ) + }) + + it('S1d falls back to reconcileNode for an opaque write on a non-host node', () => { + // Node 3 is a plain root node (no subgraph definition). Converting its + // named `widgets` map to opaque storage must take the generic + // `reconcileNode` path, not the promoted-host path. + const state = startFollower({ rootWidgetNode: true }) + const rootWidget = state.graph.getNodeById(toNodeId(3))! + expect(rootWidget).not.toBeInstanceOf(SubgraphNode) + expect(rootWidget.widgets?.[0]?.value).toBe(INTERIOR_DEFAULT_VALUE) + + forwardRaw( + state, + (nodes) => { + const node = nodes.get('3')! + node.delete('widgets') + node.set(OPAQUE_WIDGETS_KEY, [9]) + }, + 1 + ) + + const after = state.graph.getNodeById(toNodeId(3))! + expect(after).not.toBeInstanceOf(SubgraphNode) + // `reconcileNode` registers an array payload under positional names + // (`widgetEntries` in graphMutations) and clears the named entries. The + // store is the authoritative contract here; projecting positional values + // onto a plain node's litegraph widgets is the materializer's concern and + // out of scope for the follower (see agentNodeMaterializer.ts). + const stored = useWidgetValueStore() + .getNodeWidgets(graphScopeOf(state.graph).rootGraphId, toNodeId(3)) + .map((w) => [w.name, w.value]) + expect(stored).toEqual([['0', 9]]) + // The host is untouched: the fallback must not bleed into promoted state. + expect(state.instance.widgets[0]?.value).toBe(HOST_INITIAL_VALUE) + }) + + it('S1e keeps promoted host widgets when the opaque payload is malformed', () => { + // A non-array `__widgets_opaque` on a host must be skipped, not routed to + // `reconcileNode`: `widgetEntries` yields no entries for it and `commit` + // clears the node's widget store first, which would wipe every promoted + // value. + const state = startFollower() + const widgetId = state.instance.inputs[0]?.widgetId + expect(widgetId).toBeDefined() + + forwardRaw( + state, + (nodes) => nodes.get('1')!.set(OPAQUE_WIDGETS_KEY, 'bogus'), + 1 + ) + + expect(state.instance.widgets.map((w) => w.name)).toEqual(['value']) + expect(state.instance.widgets[0]?.value).toBe(HOST_INITIAL_VALUE) + expect(useWidgetValueStore().getWidget(widgetId!)?.value).toBe( + HOST_INITIAL_VALUE + ) + }) + + function storedHostWidgets(state: ReturnType) { + return useWidgetValueStore() + .getNodeWidgets(graphScopeOf(state.graph).rootGraphId, toNodeId(1)) + .map((w) => [w.name, w.value]) + } + + it('S1f registers the host under promoted names on initial load', () => { + // `addNode` reads the host's positional `__widgets_opaque`; a positional + // store entry ('0') would later collide with the named `setWidget` path + // and leave the host carrying two widgets for one promoted value. + const state = startFollower() + expect(storedHostWidgets(state)).toEqual([['value', HOST_INITIAL_VALUE]]) + }) + + it('S1g keeps promoted names across a re-armed reconcile frame', () => { + const state = startFollower() + deliver(state, hostSetWidget(42), 1) + // Re-binding arms `reconcileNextFrame`, which routes every node through + // `reconcileNode(readSemanticNode)` — the same positional read as add. + state.adapter.bind('workflow', state.follower) + deliver(state, hostSetWidget(43), 2) + + expect(storedHostWidgets(state)).toEqual([['value', 43]]) + expect(state.instance.widgets.map((w) => w.name)).toEqual(['value']) + expect(state.instance.widgets[0]?.value).toBe(43) + }) + + it('S1h keeps promoted names when the host node map is replaced', () => { + // A wholesale node replacement is an `update` nodeAction: delete then + // `addNode(readSemanticNode)`, bypassing the opaque host loop entirely. + const state = startFollower() + forwardRaw( + state, + (nodes) => { + const previous = nodes.get('1')!.toJSON() as Record + const replacement = new Y.Map() + for (const [key, value] of Object.entries(previous)) { + if (key === OPAQUE_WIDGETS_KEY) continue + replacement.set(key, value) + } + replacement.set(OPAQUE_WIDGETS_KEY, [44]) + replacement.set('title', 'Replaced host') + nodes.set('1', replacement) + }, + 1 + ) + + expect(storedHostWidgets(state)).toEqual([['value', 44]]) + expect(state.instance.widgets.map((w) => w.name)).toEqual(['value']) + expect(state.instance.widgets[0]?.value).toBe(44) + // The replacement is not widget-only: non-widget fields must land too. + expect(state.instance.title).toBe('Replaced host') + }) + + it('S1k resyncs live host fields without rebuilding promoted widgets', () => { + // A doc edit to a host's non-widget fields (title, mode, flags, ...) must + // reach the live SubgraphNode. The live-host path used to return after + // writing promoted values, so these edits were silently dropped. + const state = startFollower() + deliver(state, hostSetWidget(47), 1) + const inputBefore = state.instance.inputs[0] + forwardRaw( + state, + (nodes) => { + const node = nodes.get('1')! + node.set('title', 'Renamed host') + node.set('mode', 2) + node.set('flags', { collapsed: true }) + node.set('properties', { note: 'kept' }) + }, + 2 + ) + + expect(state.instance.title).toBe('Renamed host') + expect(state.instance.mode).toBe(2) + expect(state.instance.flags.collapsed).toBe(true) + expect(state.instance.properties.note).toBe('kept') + // Promoted widget bindings survive: same widget surface, same value, and + // the input slot object (which carries the `_subgraphSlot`/`widgetId` + // bindings) is not replaced. + expect(state.instance.widgets.map((w) => w.name)).toEqual(['value']) + expect(state.instance.widgets[0]?.value).toBe(47) + expect(storedHostWidgets(state)).toEqual([['value', 47]]) + expect(state.instance.inputs[0]).toBe(inputBefore) + }) + + it('S1l resyncs live host resizable and showAdvanced flags', () => { + // `reconcileNodeFields` already copies both booleans onto NodeState; the + // by-key filter must let the edits through, or a doc toggle is dropped. + const state = startFollower() + deliver(state, hostSetWidget(48), 1) + expect(state.instance.resizable).not.toBe(false) + expect(state.instance.showAdvanced).not.toBe(true) + forwardRaw( + state, + (nodes) => { + const node = nodes.get('1')! + node.set('resizable', false) + node.set('showAdvanced', true) + }, + 2 + ) + + expect(state.instance.resizable).toBe(false) + expect(state.instance.showAdvanced).toBe(true) + expect(state.instance.widgets[0]?.value).toBe(48) + }) + + it('S1m applies in-place Y.Array edits to opaque host widgets', () => { + // cmp writes `__widgets_opaque` as a plain array, but the reader accepts a + // shared Y.Array too. A foreign writer mutating that array in place emits + // a Y.YArrayEvent rather than a key replace on the node map; the follower + // must still re-read the host's promoted values. + const state = startFollower() + forwardRaw( + state, + (nodes) => { + const arr = new Y.Array() + nodes.get('1')!.set(OPAQUE_WIDGETS_KEY, arr) + arr.push([45]) + }, + 1 + ) + expect(state.instance.widgets[0]?.value).toBe(45) + + forwardRaw( + state, + (nodes) => { + const arr = nodes.get('1')!.get(OPAQUE_WIDGETS_KEY) as Y.Array + arr.delete(0, 1) + arr.insert(0, [46]) + }, + 2 + ) + + expect(state.instance.widgets[0]?.value).toBe(46) + expect(storedHostWidgets(state)).toEqual([['value', 46]]) + }) + + it('S1i keeps promoted names when the host flips back to named storage', () => { + // Deleting `__widgets_opaque` and writing a named `widgets` map lands in + // the replaced-widget-storage loop, which used to run `reconcileNode` on + // the host and reset its live inputs. + const state = startFollower() + forwardRaw( + state, + (nodes) => { + const node = nodes.get('1')! + node.delete(OPAQUE_WIDGETS_KEY) + node.set('widgets', new Y.Map([['value', 45]])) + }, + 1 + ) + + expect(storedHostWidgets(state)).toEqual([['value', 45]]) + expect(state.instance.widgets.map((w) => w.name)).toEqual(['value']) + expect(state.instance.widgets[0]?.value).toBe(45) + }) + + it('S1j leaves promoted values unchanged when the opaque array shrinks', () => { + // A shorter opaque array carries no value for the declared promoted name. + // The host keeps its current value rather than dropping the widget or + // resetting it: the definition, not the array length, owns the surface. + const state = startFollower() + deliver(state, hostSetWidget(46), 1) + forwardRaw(state, (nodes) => nodes.get('1')!.set(OPAQUE_WIDGETS_KEY, []), 2) + + expect(storedHostWidgets(state)).toEqual([['value', 46]]) + expect(state.instance.widgets.map((w) => w.name)).toEqual(['value']) + expect(state.instance.widgets[0]?.value).toBe(46) + expect(reportError).toHaveBeenCalledWith( + expect.any(Error), + expect.objectContaining({ + errorType: 'error_reconciling_agent_subgraph_host_widgets', + context: expect.objectContaining({ expected: 1, actual: 0 }) + }) + ) + }) + + it('S1n reports a persisting opaque length mismatch once per session', () => { + // Every frame that rewrites the still-short opaque array re-reads the host + // and would re-report the same drift. Sentry should see it once, not once + // per frame. + const state = startFollower() + deliver(state, hostSetWidget(46), 1) + forwardRaw(state, (nodes) => nodes.get('1')!.set(OPAQUE_WIDGETS_KEY, []), 2) + forwardRaw(state, (nodes) => nodes.get('1')!.set(OPAQUE_WIDGETS_KEY, []), 3) + + expect(storedHostWidgets(state)).toEqual([['value', 46]]) + expect(reportError).toHaveBeenCalledTimes(1) + }) + + it('S1o replays a promoted edit dropped by a rejected batch on the next frame', () => { + // `applyQueuedFrame` snapshots and clears the session's pending sets + // before `batch` runs. When `batch` is rejected (no graph scope) the + // snapshot is gone, so the only way the edit can still land is a full + // reconcile on the next frame. The follow-up frame touches a different + // node: a `changedNodeFields` hit on the host itself re-reads the whole + // host (widgets included) and would mask the lost edit. + const scope = { blocked: false } + const state = startFollower({ scope }) + + scope.blocked = true + const vector = Y.encodeStateVector(state.hostDoc) + const id = 'op-1' + const result = applyOps( + state.hostDoc, + [operation(id, 1, hostSetWidget(46))], + CATALOG + ) + expect(result.outcomes).toEqual([{ op_id: id, outcome: 'applied' }]) + const update = Y.encodeStateAsUpdate(state.hostDoc, vector) + state.follower.applyRemoteUpdate(update) + expect( + state.adapter.applyFrame({ + workflowId: 'workflow', + seq: 2, + update, + actor: 'agent:test', + opIds: [id] + }) + ).toBe(false) + expect(state.instance.widgets[0]?.value).toBe(HOST_INITIAL_VALUE) + + scope.blocked = false + forwardRaw(state, (nodes) => nodes.get('2')!.set('title', 'Retitled'), 2) + + expect(state.graph.getNodeById(toNodeId(2))?.title).toBe('Retitled') + expect(storedHostWidgets(state)).toEqual([['value', 46]]) + expect(state.instance.widgets[0]?.value).toBe(46) + }) + + it('S1p rebuilds a plain node whose doc entry becomes a subgraph host', () => { + // "Already live" must mean the same node *type*, not merely the same id. + // When a plain node's map is replaced by a host-typed map, keeping the + // incumbent would leave a promoted-widget node standing in for the host. + const state = startFollower({ rootWidgetNode: true }) + expect(state.graph.getNodeById(toNodeId(3))).not.toBeInstanceOf( + SubgraphNode + ) + forwardRaw(state, retypeNode3AsHost, 1) + + expectNode3RebuiltAsHost(state) + }) + + it('S1r rebuilds a retyped node through the reconcile path after a rejected frame', () => { + // The incremental path deletes + re-adds an `update`d node, but a frame + // rejected by `batch` (no graph scope) loses that action set and the next + // frame runs a full reconcile. `reconcileNode` on an existing node only + // updates its fields, so a plain node whose doc entry became a host would + // survive as a promoted-widget node unless reconcile also compares types. + const scope = { blocked: false } + const state = startFollower({ rootWidgetNode: true, scope }) + + scope.blocked = true + const vector = Y.encodeStateVector(state.hostDoc) + state.hostDoc.transact(() => { + retypeNode3AsHost(state.hostDoc.getMap>('nodes')) + }) + const update = Y.encodeStateAsUpdate(state.hostDoc, vector) + state.follower.applyRemoteUpdate(update) + expect( + state.adapter.applyFrame({ workflowId: 'workflow', seq: 2, update }) + ).toBe(false) + expect(state.graph.getNodeById(toNodeId(3))).not.toBeInstanceOf( + SubgraphNode + ) + + scope.blocked = false + forwardRaw(state, (nodes) => nodes.get('2')!.set('title', 'Retitled'), 2) + + expect(state.graph.getNodeById(toNodeId(2))?.title).toBe('Retitled') + expectNode3RebuiltAsHost(state) + }) + + it('S1s rebuilds a retyped node through the reconcile path after a rebind', () => { + // `bind` re-arms the session so its next frame reconciles everything, so + // a retype arriving in that frame must go through the reconcile branch. + const state = startFollower({ rootWidgetNode: true }) + expect(state.graph.getNodeById(toNodeId(3))).not.toBeInstanceOf( + SubgraphNode + ) + + state.adapter.bind('workflow', state.follower) + forwardRaw(state, retypeNode3AsHost, 1) + + expectNode3RebuiltAsHost(state) + const rebuilt = state.graph.getNodeById(toNodeId(3)) + + // A second rebind reconciles a host whose type already matches: it must + // keep the live instance and its promoted widget value, not rebuild. + state.adapter.bind('workflow', state.follower) + forwardRaw(state, (nodes) => nodes.get('3')!.set('title', 'Host 3'), 3) + expect(state.graph.getNodeById(toNodeId(3))).toBe(rebuilt) + expect(rebuilt?.title).toBe('Host 3') + expectNode3RebuiltAsHost(state) + }) + + it('S1q rebuilds a host whose doc entry becomes a plain node', () => { + const state = startFollower() + forwardRaw( + state, + (nodes) => { + const replacement = new Y.Map() + replacement.set('id', 1) + replacement.set('type', 'source') + replacement.set('pos', [0, 0]) + replacement.set('size', [100, 60]) + replacement.set('mode', 0) + replacement.set('order', 0) + replacement.set('flags', {}) + replacement.set('properties', {}) + replacement.set('inputs', []) + replacement.set('outputs', []) + nodes.set('1', replacement) + }, + 1 + ) + + const rebuilt = state.graph.getNodeById(toNodeId(1)) + expect(rebuilt).toBeDefined() + expect(rebuilt).not.toBeInstanceOf(SubgraphNode) + expect(rebuilt?.type).toBe('source') + }) + + it('S1t rebuilds a host retyped to a different registered definition', () => { + // Both types are registered definitions, so the live-host check must + // compare the live node's type against the payload, not just definition + // membership; otherwise the stale instance is kept and its widget + // reconciled as if the type had not changed. + const state = startFollower({ secondDefinition: true }) + const secondId = readSubgraphDefinitions(state.follower.doc) + .map((d) => d.id) + .find((id) => id !== state.instance.type) + expect(secondId).toBeDefined() + + forwardRaw( + state, + (nodes) => { + const host = nodes.get('1')!.toJSON() as Record + const replacement = new Y.Map() + for (const [key, value] of Object.entries(host)) { + replacement.set(key, value) + } + replacement.set('type', secondId) + nodes.set('1', replacement) + }, + 1 + ) + + const rebuilt = state.graph.getNodeById(toNodeId(1)) + expect(rebuilt).toBeInstanceOf(SubgraphNode) + expect(rebuilt).not.toBe(state.instance) + expect(rebuilt?.type).toBe(secondId) + expect(rebuilt?.widgets?.[0]?.value).toBe(HOST_INITIAL_VALUE) + }) +}) + +/** + * Replace plain node 3's doc entry with a copy of host 1's map (same type, + * inputs, outputs) carrying its own opaque widget value. Requires the + * `rootWidgetNode` fixture so node 3 exists. + */ +function retypeNode3AsHost(nodes: ReturnType) { + const host = nodes.get('1')!.toJSON() as Record + const replacement = new Y.Map() + for (const [key, value] of Object.entries(host)) { + if (key === OPAQUE_WIDGETS_KEY) continue + replacement.set(key, value) + } + replacement.set('id', 3) + replacement.set(OPAQUE_WIDGETS_KEY, [44]) + nodes.set('3', replacement) +} + +function expectNode3RebuiltAsHost(state: ReturnType) { + const rebuilt = state.graph.getNodeById(toNodeId(3)) + expect(rebuilt).toBeInstanceOf(SubgraphNode) + expect(rebuilt?.widgets?.map((w) => w.name)).toEqual(['value']) + expect(rebuilt?.widgets?.[0]?.value).toBe(44) +} + +function hostSetWidget(value: number): GraphOperation { + return { + op: 'set_widget', + node_id: 1, + widget: 'value', + value, + promoted: { + instance_path: [1], + value_index: 0, + host_widgets_values: [HOST_INITIAL_VALUE] + } + } +} diff --git a/src/workbench/extensions/agent/crdt/agentSubgraphHostSlots.test.ts b/src/workbench/extensions/agent/crdt/agentSubgraphHostSlots.test.ts new file mode 100644 index 00000000000..335b40953bc --- /dev/null +++ b/src/workbench/extensions/agent/crdt/agentSubgraphHostSlots.test.ts @@ -0,0 +1,373 @@ +import { describe, expect, it } from 'vitest' + +import { RenderShape } from '@/lib/litegraph/src/types/globalEnums' +import type { ExportedSubgraph } from '@/lib/litegraph/src/types/serialisation' + +import { + hostInputs, + hostSlotIndex, + indexSubgraphDefinitions, + promotedWidgetNames +} from './agentSubgraphHostSlots' + +/** Definition whose interior `nodes` are known to be present. */ +type DefinitionWithNodes = ExportedSubgraph & { + nodes: NonNullable +} + +/** + * Definition with `extra` declared before `value`. Only `value` lands on an + * interior input that carries a widget; `extra` feeds a plain slot and + * `dangling` has no links at all. + */ +function definition(): DefinitionWithNodes { + return { + id: 'sg-1', + version: 1, + state: { lastGroupId: 0, lastNodeId: 2, lastLinkId: 2, lastRerouteId: 0 }, + revision: 0, + config: {}, + name: 'sg', + inputNode: { id: -10, bounding: [0, 0, 0, 0] }, + outputNode: { id: -20, bounding: [0, 0, 0, 0] }, + inputs: [ + { id: 'in-extra', name: 'extra', type: 'NUMBER', linkIds: [1] }, + { id: 'in-value', name: 'value', type: 'NUMBER', linkIds: [2] }, + { id: 'in-dangling', name: 'dangling', type: 'NUMBER' } + ], + outputs: [], + widgets: [], + nodes: [ + { + id: 1, + type: 'plain', + pos: [0, 0], + size: [1, 1], + flags: {}, + order: 0, + mode: 0, + inputs: [{ name: 'extra', type: 'NUMBER', link: 1 }] + }, + { + id: 2, + type: 'promoted-widget', + pos: [0, 0], + size: [1, 1], + flags: {}, + order: 1, + mode: 0, + inputs: [ + { name: 'value', type: 'NUMBER', link: 2, widget: { name: 'value' } } + ] + } + ], + links: [ + { + id: 1, + origin_id: -10, + origin_slot: 0, + target_id: 1, + target_slot: 0, + type: 'NUMBER' + }, + { + id: 2, + origin_id: -10, + origin_slot: 1, + target_id: 2, + target_slot: 0, + type: 'NUMBER' + } + ], + groups: [], + extra: {} + } satisfies DefinitionWithNodes +} + +describe('agentSubgraphHostSlots', () => { + it('indexes definitions by string id', () => { + const def = definition() + const index = indexSubgraphDefinitions([def]) + expect(index.get('sg-1')).toBe(def) + expect(index.has('sg-2')).toBe(false) + }) + + it('indexes definitions nested under an outer definition', () => { + const nested = { ...definition(), id: 'sg-nested' } + const outer = { + ...definition(), + definitions: { subgraphs: [nested] } + } + const index = indexSubgraphDefinitions([outer]) + expect(index.get('sg-1')).toBe(outer) + expect(index.get('sg-nested')).toBe(nested) + }) + + it('keeps the first definition when a nested one repeats an id', () => { + const shadow = { ...definition(), name: 'shadow' } + const outer = { + ...definition(), + id: 'sg-outer', + definitions: { subgraphs: [shadow] } + } + const first = definition() + const index = indexSubgraphDefinitions([first, outer]) + expect(index.get('sg-1')).toBe(first) + }) + + it('prefers a top-level definition over an earlier nested copy', () => { + const nested = { ...definition(), name: 'nested copy' } + const outer = { + ...definition(), + id: 'sg-outer', + definitions: { subgraphs: [nested] } + } + const topLevel = { ...definition(), name: 'top-level authority' } + + const index = indexSubgraphDefinitions([outer, topLevel]) + + expect(index.get('sg-1')).toBe(topLevel) + }) + + it('treats duplicate declared input names as ambiguous', () => { + const base = definition() + const def: ExportedSubgraph = { + ...base, + inputs: [ + ...(base.inputs ?? []), + { id: 'in-value-2', name: 'value', type: 'NUMBER' } + ] + } + expect(hostSlotIndex(def, 'value')).toBe(-1) + expect(hostSlotIndex(def, 'extra')).toBe(0) + const inputs = hostInputs(def, [{ name: 'value', type: 'NUMBER', link: 9 }]) + expect(inputs.map((input) => input.name)).toEqual([ + 'extra', + 'value', + 'dangling', + 'value' + ]) + // No slot is unlinked outright: an ambiguous or omitted declared slot + // leaves `link` undefined so the mutation layer keeps the live link. + expect(inputs.map((input) => input.link)).toEqual([ + undefined, + undefined, + undefined, + undefined + ]) + expect(inputs.every((input) => !('link' in input))).toBe(true) + }) + + it('reports only inputs linked to a widget-bearing interior slot, in order', () => { + const def = definition() + expect(promotedWidgetNames(def, indexSubgraphDefinitions([def]))).toEqual([ + 'value' + ]) + }) + + /** + * Outer definition whose `extra` input feeds a nested subgraph instance + * (node 1 typed `sg-1`) on its `value` input. The nested definition promotes + * `value` to a widget, so the outer `extra` is widget-backed through the + * nested instance even though node 1's own input entry carries no `widget` + * marker (`SubgraphNode._resolveNestedPromotedSource`, comfy-cli + * `promoted_inputs`). The nested instance's own `value` slot has no widget + * marker in the outer definition, so `extra` must precede `value` in the + * positional `__widgets_opaque` order. + */ + function outerWithNestedInstance(): DefinitionWithNodes { + const base = definition() + return { + ...base, + id: 'sg-outer', + nodes: [ + { + ...base.nodes[0], + type: 'sg-1', + inputs: [{ name: 'value', type: 'NUMBER', link: 1 }] + }, + base.nodes[1] + ] + } + } + + it('counts an input promoted through a nested instance as widget-backed', () => { + const outer = outerWithNestedInstance() + const index = indexSubgraphDefinitions([outer, definition()]) + expect(promotedWidgetNames(outer, index)).toEqual(['extra', 'value']) + }) + + it('does not count a nested-instance input the nested definition leaves unpromoted', () => { + const outer = outerWithNestedInstance() + const nested = definition() + outer.nodes[0].inputs = [{ name: 'extra', type: 'NUMBER', link: 1 }] + const index = indexSubgraphDefinitions([outer, nested]) + expect(promotedWidgetNames(outer, index)).toEqual(['value']) + }) + + it('treats a nested instance whose definition is missing as unpromoted', () => { + const outer = outerWithNestedInstance() + const index = indexSubgraphDefinitions([outer]) + expect(promotedWidgetNames(outer, index)).toEqual(['value']) + }) + + it('terminates when nested definitions reference each other cyclically', () => { + const outer = outerWithNestedInstance() + // `sg-1` in turn hosts an `sg-outer` instance on its promoted `value` link. + const nested = definition() + nested.nodes[1] = { + ...nested.nodes[1], + type: 'sg-outer', + inputs: [{ name: 'extra', type: 'NUMBER', link: 2 }] + } + const index = indexSubgraphDefinitions([outer, nested]) + // `extra` chases the cycle until the depth cap and stays unpromoted; + // `value` still lands on a real widget marker inside `sg-outer`. + expect(promotedWidgetNames(outer, index)).toEqual(['value']) + }) + + it('resolves a cyclic fan-out without revisiting definitions exponentially', () => { + // `sg-outer.extra` fans out to two `sg-1` instances (nodes 1 and 3), and + // `sg-1.extra` links back into an `sg-outer` instance. A depth-only walk + // doubles the work at every level (2^32 lookups before the cap); a walk + // that remembers in-flight and resolved inputs settles in a handful. + const outer = outerWithNestedInstance() + outer.inputs![0] = { ...outer.inputs![0], linkIds: [1, 3] } + outer.nodes[0].inputs = [{ name: 'extra', type: 'NUMBER', link: 1 }] + outer.nodes.push({ + ...outer.nodes[0], + id: 3, + inputs: [{ name: 'extra', type: 'NUMBER', link: 3 }] + }) + outer.links!.push({ + id: 3, + origin_id: -10, + origin_slot: 0, + target_id: 3, + target_slot: 0, + type: 'NUMBER' + }) + const nested = definition() + nested.nodes[0] = { + ...nested.nodes[0], + type: 'sg-outer', + inputs: [{ name: 'extra', type: 'NUMBER', link: 1 }] + } + class CountingIndex extends Map { + lookups = 0 + override get(key: string) { + if (++this.lookups > 1000) { + throw new Error(`definition lookups exceeded 1000 (${key})`) + } + return super.get(key) + } + } + const index = new CountingIndex(indexSubgraphDefinitions([outer, nested])) + expect(promotedWidgetNames(outer, index)).toEqual(['value']) + expect(index.lookups).toBeLessThan(50) + }) + + it('excludes a promoted input whose name is declared twice', () => { + // Both `value` slots land on widgets, but the name cannot be mapped back + // to one host slot, so `hostSlotIndex` rejects it. Reporting it as a + // promoted widget would let `setWidget` target a slot we refuse to wire. + const def = definition() + def.inputs!.push({ + id: 'in-value-2', + name: 'value', + type: 'NUMBER', + linkIds: [2] + }) + const index = indexSubgraphDefinitions([def]) + expect(promotedWidgetNames(def, index)).toEqual([]) + expect(hostSlotIndex(def, 'value')).toBe(-1) + }) + + it('stops chasing promotion through a nested chain deeper than the cap', () => { + // A linear chain: `chain-0` hosts `chain-1`, which hosts `chain-2`, … + // `chain-33` finally lands on a real widget. Thirty-three hops exceed + // the 32-deep cap, so the root input must resolve as unpromoted rather + // than walking the whole chain (comfy-cli `_MAX_NESTED_PROMOTION_DEPTH`). + const depth = 33 + const chain: DefinitionWithNodes[] = [] + for (let level = 0; level <= depth; level++) { + const def = definition() + def.id = `chain-${level}` + def.inputs = [ + { id: 'in-value', name: 'value', type: 'NUMBER', linkIds: [2] } + ] + if (level < depth) { + def.nodes[1] = { + ...def.nodes[1], + type: `chain-${level + 1}`, + inputs: [{ name: 'value', type: 'NUMBER', link: 2 }] + } + } + chain.push(def) + } + const index = indexSubgraphDefinitions(chain) + expect(promotedWidgetNames(chain[0], index)).toEqual([]) + expect(promotedWidgetNames(chain[depth - 32], index)).toEqual(['value']) + }) + + it('resolves host slot index from definition order, not doc order', () => { + const def = definition() + expect(hostSlotIndex(def, 'extra')).toBe(0) + expect(hostSlotIndex(def, 'value')).toBe(1) + expect(hostSlotIndex(def, 'missing')).toBe(-1) + }) + + it('expands a single-entry doc input list to the full declared slot list', () => { + // cmp writes only the grown slot after a promoted connect; the doc says + // `value` sits at index 0 but the host's real slot for `value` is 1. + const inputs = hostInputs(definition(), [ + { name: 'value', type: 'NUMBER', link: 9 } + ]) + expect(inputs.map((input) => input.name)).toEqual([ + 'extra', + 'value', + 'dangling' + ]) + // Slots the doc omits are left without a `link` key so the mutation + // layer preserves whatever live link the host already carries there. + expect('link' in inputs[0]).toBe(false) + expect(inputs[1].link).toBe(9) + expect('link' in inputs[2]).toBe(false) + }) + + it('carries declared slot fields onto host inputs, letting doc fields override', () => { + const def = definition() + def.inputs![0] = { + ...def.inputs![0], + label: 'Extra label', + shape: RenderShape.GRID, + localized_name: 'Extra' + } + def.inputs![1] = { ...def.inputs![1], label: 'Definition label' } + const inputs = hostInputs(def, [ + { name: 'value', type: 'NUMBER', link: 9, label: 'Doc label' } + ]) + expect(inputs[0]).toMatchObject({ + name: 'extra', + type: 'NUMBER', + label: 'Extra label', + shape: RenderShape.GRID, + localized_name: 'Extra' + }) + expect('id' in inputs[0]).toBe(false) + expect('linkIds' in inputs[0]).toBe(false) + expect(inputs[1]).toMatchObject({ + name: 'value', + link: 9, + label: 'Doc label' + }) + }) + + it('returns no slots for a definition without inputs', () => { + const def: ExportedSubgraph = { ...definition(), inputs: undefined } + expect(hostInputs(def, [])).toEqual([]) + expect(promotedWidgetNames(def, indexSubgraphDefinitions([def]))).toEqual( + [] + ) + }) +}) diff --git a/src/workbench/extensions/agent/crdt/agentSubgraphHostSlots.ts b/src/workbench/extensions/agent/crdt/agentSubgraphHostSlots.ts new file mode 100644 index 00000000000..25c38459786 --- /dev/null +++ b/src/workbench/extensions/agent/crdt/agentSubgraphHostSlots.ts @@ -0,0 +1,244 @@ +import { hasPromotedWidgetTarget } from '@/core/graph/subgraph/hasPromotedWidgetTarget' +import type { + ExportedSubgraph, + ISerialisableNodeInput +} from '@/lib/litegraph/src/types/serialisation' + +import { allSubgraphDefinitions } from './agentSubgraphDefinitions' + +/** + * Slot-level facts about a SubgraphNode host, derived from the subgraph + * definition the op layer minted rather than from the host's doc entry. + * + * cmp treats a SubgraphNode host as opaque: it stores the host's promoted + * widget values positionally under `__widgets_opaque` and, after a promoted + * `connect`, writes only the single grown slot into the host's `inputs`. The + * definition is the authoritative source for the host's slot order (host + * inputs mirror `subgraph.inputNode.slots`) and for which inputs surface a + * widget, so this module resolves both from it. + */ + +export type SubgraphDefinitionIndex = ReadonlyMap + +/** + * Flattens the definition tree (each definition may carry nested + * `definitions.subgraphs`) into one id-keyed index. The first definition + * seen for an id wins, so a nested copy never shadows a top-level one. + */ +export function indexSubgraphDefinitions( + definitions: readonly ExportedSubgraph[] +): SubgraphDefinitionIndex { + const index = new Map() + for (const definition of allSubgraphDefinitions(definitions)) { + if (!index.has(definition.id)) index.set(definition.id, definition) + } + return index +} + +/** + * Declared input names that appear more than once. A repeated name cannot be + * resolved to a single host slot, so callers treat it as undeclared. + */ +function ambiguousInputNames(definition: ExportedSubgraph): Set { + const seen = new Set() + const ambiguous = new Set() + for (const input of definition.inputs ?? []) { + if (seen.has(input.name)) ambiguous.add(input.name) + seen.add(input.name) + } + return ambiguous +} + +/** + * Mirrors comfy-cli `_MAX_NESTED_PROMOTION_DEPTH`: how many nested subgraph + * instances a promoted input may be chased through before it is treated as + * unpromoted. Also bounds cyclic definition references. + */ +const MAX_NESTED_PROMOTION_DEPTH = 32 + +/** + * Names of the subgraph inputs that surface a widget on the host, in host + * slot order. This is the order cmp's positional `__widgets_opaque` array + * follows, and it must match the rule comfy-cli `promoted_inputs()` and + * `SubgraphNode._resolveInputWidget` share: an input is promoted when one of + * its links lands on + * + * - an interior plain node input that carries a `widget` reference, or + * - a nested subgraph instance (node `type` found in `index`) whose + * same-named input is itself promoted, resolved recursively through + * `_resolveNestedPromotedSource`. + * + * A nested instance whose definition is missing from `index`, or whose chain + * exceeds the depth cap, counts as unpromoted. So does an input whose chain + * leads back to itself: a cyclic definition reference can never reach a real + * widget, and the walk stops the first time it meets an input it is already + * resolving instead of chasing the cycle to the depth cap. Each (definition, + * input) pair is resolved once per call, so a fan-out of nested instances + * costs linear, not exponential, work. + */ +export function promotedWidgetNames( + definition: ExportedSubgraph, + index: SubgraphDefinitionIndex +): string[] { + const lookups = new Map() + // A name declared twice cannot be mapped to one host slot (`hostSlotIndex` + // rejects it), so it must not be reported as a settable promoted widget. + const ambiguous = ambiguousInputNames(definition) + return (definition.inputs ?? []).flatMap((input, inputIndex) => + !ambiguous.has(input.name) && + isPromoted(definition, inputIndex, index, lookups) + ? [input.name] + : [] + ) +} + +interface DefinitionLookups { + linksById: ReadonlyMap[number]> + nodesById: ReadonlyMap[number]> +} + +interface PromotionTarget { + definition: ExportedSubgraph + inputIndex: number +} + +interface PromotionExpansion { + hasWidget: boolean + nested: PromotionTarget[] +} + +function lookupsFor( + definition: ExportedSubgraph, + lookupsByDefinition: Map +): DefinitionLookups { + const existing = lookupsByDefinition.get(definition) + if (existing) return existing + const lookups: DefinitionLookups = { + linksById: new Map((definition.links ?? []).map((link) => [link.id, link])), + nodesById: new Map( + (definition.nodes ?? []).map((node) => [String(node.id), node]) + ) + } + lookupsByDefinition.set(definition, lookups) + return lookups +} + +function promotionTargetForLink( + definition: ExportedSubgraph, + linkId: number, + index: SubgraphDefinitionIndex, + lookups: Map +): PromotionExpansion | undefined { + const { linksById, nodesById } = lookupsFor(definition, lookups) + const link = linksById.get(linkId) + if (!link) return + const target = nodesById.get(String(link.target_id)) + const targetInput = target?.inputs?.[link.target_slot] + if (!target || !targetInput) return + const nestedDefinition = index.get(target.type) + if (!nestedDefinition) { + return { hasWidget: targetInput.widget != null, nested: [] } + } + return { + hasWidget: false, + nested: (nestedDefinition.inputs ?? []).flatMap( + (nestedInput, inputIndex) => + nestedInput.name === targetInput.name + ? [{ definition: nestedDefinition, inputIndex }] + : [] + ) + } +} + +function expandPromotionTarget( + target: PromotionTarget, + index: SubgraphDefinitionIndex, + lookups: Map +): PromotionExpansion { + const input = target.definition.inputs?.[target.inputIndex] + if (!input) return { hasWidget: false, nested: [] } + const expansion: PromotionExpansion = { hasWidget: false, nested: [] } + for (const linkId of input.linkIds ?? []) { + const linkTarget = promotionTargetForLink( + target.definition, + linkId, + index, + lookups + ) + if (!linkTarget) continue + expansion.hasWidget ||= linkTarget.hasWidget + expansion.nested.push(...linkTarget.nested) + } + return expansion +} + +function isPromoted( + definition: ExportedSubgraph, + inputIndex: number, + index: SubgraphDefinitionIndex, + lookups: Map +): boolean { + const root: PromotionTarget = { definition, inputIndex } + return hasPromotedWidgetTarget( + root, + MAX_NESTED_PROMOTION_DEPTH, + ({ definition, inputIndex }) => definition.inputs?.[inputIndex], + (target) => expandPromotionTarget(target, index, lookups) + ) +} + +/** + * The host's full input slot list in definition order. Each slot starts from + * the declared input's presentation fields (label, shape, etc.; never the + * definition-only `id`/`linkIds`), then takes whatever the doc carries for a + * slot of the same name on top. `name` and `type` always come from the + * definition. + * + * `link` is only present when the doc supplies it. Declared inputs the doc + * omits, and inputs whose name is declared more than once, carry no `link` + * key at all: cmp writes only the grown slot after a promoted connect, so an + * omitted slot means "no information", not "unlinked". The graph mutation + * layer resolves an absent `link` against the live host's slot at the same + * index (see `graphMutations` `prepareInputSlots`). + */ +export function hostInputs( + definition: ExportedSubgraph, + docInputs: readonly unknown[] +): ISerialisableNodeInput[] { + const ambiguous = ambiguousInputNames(definition) + const docByName = new Map( + docInputs + .filter( + (input): input is Record & { name: string } => + input !== null && + typeof input === 'object' && + 'name' in input && + typeof input.name === 'string' + ) + .map((input) => [input.name, input]) + ) + return (definition.inputs ?? []).map((input) => { + const { id: _id, linkIds: _linkIds, ...declared } = input + const fromDoc = ambiguous.has(input.name) + ? undefined + : docByName.get(input.name) + return { + ...declared, + ...fromDoc, + name: input.name, + type: input.type + } + }) +} + +/** + * Index of the named input in definition order, or -1 when the name is + * undeclared or declared more than once. + */ +export function hostSlotIndex( + definition: ExportedSubgraph, + name: string +): number { + if (ambiguousInputNames(definition).has(name)) return -1 + return (definition.inputs ?? []).findIndex((input) => input.name === name) +} diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index 0dcf355674f..401708f6d13 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -939,6 +939,7 @@ describe('EcsFollowerAdapter integration', () => { const noopBatch = { addNode: () => undefined, reconcileNode: () => undefined, + reconcileNodeFields: () => undefined, setWidget: () => undefined, connect: () => undefined, removeMissing: () => undefined, diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index 6f72c7f37fe..d26dd9bf21a 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -10,13 +10,40 @@ import type { SemanticLinkPayload, SemanticNodePayload } from '@/core/graph/graphMutations' +import { reportError } from '@/platform/telemetry/reportError' import type { RemoteMutationContext } from '@/types/graphMutationContext' import { toNodeId } from '@/types/nodeId' +import { readSubgraphDefinitions } from './agentSubgraphDefinitions' +import { + hostInputs, + hostSlotIndex, + indexSubgraphDefinitions, + promotedWidgetNames +} from './agentSubgraphHostSlots' +import type { SubgraphDefinitionIndex } from './agentSubgraphHostSlots' import type { DocUpdate } from './docFrameClient' import type { FollowerDoc } from './followerDoc' type NodeRootAction = 'add' | 'update' | 'delete' + +/** + * Node-map keys whose by-key edits trigger a field resync. Structural keys + * (`inputs`, `outputs`, `pos`, `size`, widget storage) are excluded: slots + * are handled by link events and autogrow, layout is not resynced in place. + */ +const RESYNCED_NODE_FIELDS: ReadonlySet = new Set([ + 'title', + 'mode', + 'flags', + 'properties', + 'color', + 'bgcolor', + 'boxcolor', + 'shape', + 'resizable', + 'showAdvanced' +]) export type MutationsForTarget = | GraphMutations | ((workflowId: string) => GraphMutations) @@ -26,14 +53,50 @@ function plain(value: unknown): unknown { return structuredClone(value) } -function readSemanticNode(doc: Y.Doc, id: string): SemanticNodePayload | null { +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +/** + * Doc/live drift (an opaque widget array of the wrong length, a link onto an + * undeclared promoted slot) persists in the doc, so every later frame that + * re-reads the same entry would report it again. Report each distinct drift + * once per target session; the set lives as long as the session does. + */ +function reportOnce( + reported: Set, + key: string, + error: Error, + options: Parameters[1] +): void { + if (reported.has(key)) return + reported.add(key) + reportError(error, options) +} + +/** + * Reads a node's doc entry as a semantic payload. A SubgraphNode host (a node + * whose type names a definition) is stored opaquely by cmp: positional widget + * values under `__widgets_opaque` and only the grown slots under `inputs`. + * Both are re-keyed from the definition so `reconcileNode` registers the + * host's widgets under their promoted names and keeps its full slot list; + * otherwise a reconcile would wipe the promoted widgets and `setWidget` by + * name could never find them again. + */ +function readSemanticNode( + doc: Y.Doc, + id: string, + definitions: () => SubgraphDefinitionIndex, + reported: Set +): SemanticNodePayload | null { const source = nodesMap(doc).get(id) if (!(source instanceof Y.Map)) return null const type = source.get('type') if (typeof type !== 'string' || type.length === 0) return null - const payload: Record = {} + const payload: SemanticNodePayload = { id, type } source.forEach((value, key) => { + if (key === 'id' || key === 'type') return if (key === 'widgets' && value instanceof Y.Map) { payload.widgets_values = value.toJSON() } else if (key === OPAQUE_WIDGETS_KEY) { @@ -42,12 +105,114 @@ function readSemanticNode(doc: Y.Doc, id: string): SemanticNodePayload | null { payload[key] = plain(value) } }) - payload.id = id - payload.type = type - return payload as SemanticNodePayload + + const definition = definitions().get(type) + if (definition) { + const opaque = payload.widgets_values + if (Array.isArray(opaque)) { + const names = promotedWidgetNames(definition, definitions()) + if (opaque.length === names.length) { + payload.widgets_values = Object.fromEntries( + opaque.map((value, index) => [names[index], value]) + ) + } else { + // cmp writes the whole positional array against the same promoted + // list (`promoted_inputs()`), so a length mismatch means the writer + // and this reader disagree on the host surface. Mapping positionally + // would land values on the wrong promoted widget; keep the live + // values and surface the drift instead. + delete payload.widgets_values + reportOnce( + reported, + `widgets:${id}:${names.length}:${opaque.length}`, + new Error( + `Subgraph host ${id} (${type}) carries ${opaque.length} opaque widget values but its definition promotes ${names.length}` + ), + { + errorType: 'error_reconciling_agent_subgraph_host_widgets', + context: { + nodeId: id, + type, + expected: names.length, + actual: opaque.length + } + } + ) + } + } + const docInputs = source.get('inputs') + payload.inputs = hostInputs( + definition, + docInputs instanceof Y.Array ? docInputs.toJSON() : [] + ) + } + return payload +} + +/** + * Resolves a link whose target is a SubgraphNode host. cmp only writes the + * grown slot into the host's doc `inputs`, and its `target_slot` indexes that + * doc-local list. The live host orders its inputs by the subgraph definition, + * so re-derive both the slot index and the full input list from it. + * + * Returns `null` when the doc slot names an input the definition does not + * declare (cmp's `claimPromotedInput` does not validate the grown name). The + * live host has no such slot, so wiring the link positionally would land it + * on an unrelated input; the caller skips the link instead and the drop is + * reported so the doc/live divergence is visible. + */ +function hostTarget( + doc: Y.Doc, + definitions: SubgraphDefinitionIndex, + reported: Set, + targetId: string, + docSlot: number +): Pick | null { + const docInputs = readNodeSlots(doc, targetId, 'inputs') + const type = nodesMap(doc).get(targetId)?.get('type') + const definition = + typeof type === 'string' ? definitions.get(type) : undefined + if (!definition) return { targetSlot: docSlot, targetInputs: docInputs } + + const name = docInputs?.[docSlot]?.name + const slot = name == null ? -1 : hostSlotIndex(definition, name) + if (slot < 0) { + reportInvalidHostTarget(reported, targetId, type, docSlot, name) + return null + } + return { + targetSlot: slot, + targetInputs: hostInputs(definition, docInputs ?? []) + } +} + +function reportInvalidHostTarget( + reported: Set, + targetId: string, + type: unknown, + docSlot: number, + name: string | undefined +): void { + const displayName = name == null ? 'unnamed' : `'${name}'` + reportOnce( + reported, + `slot:${targetId}:${docSlot}:${name ?? ''}`, + new Error( + `Subgraph host ${targetId} (${String(type)}) link targets doc slot ${docSlot} (${displayName}), which its definition does not declare unambiguously` + ), + { + errorType: 'error_reconciling_agent_subgraph_host_slot', + context: { nodeId: targetId, type, slot: docSlot, name: name ?? null } + } + ) } -function readSemanticLink(doc: Y.Doc, id: string): SemanticLinkPayload | null { +function readSemanticLink( + doc: Y.Doc, + id: string, + definitions: SubgraphDefinitionIndex, + reported: Set +): SemanticLinkPayload | null { const raw = linksMap(doc).get(id) const tuple = raw instanceof Y.Array ? raw.toArray() : raw if (!Array.isArray(tuple) || tuple.length < 5) return null @@ -63,21 +228,33 @@ function readSemanticLink(doc: Y.Doc, id: string): SemanticLinkPayload | null { ) { return null } + const targetNodeId = String(tuple[3]) + const target = hostTarget( + doc, + definitions, + reported, + targetNodeId, + targetSlot + ) + if (!target) return null return { id: linkId, originNodeId: String(tuple[1]), originSlot, - targetNodeId: String(tuple[3]), - targetSlot, + targetNodeId, type: typeof tuple[5] === 'string' || typeof tuple[5] === 'number' ? tuple[5] : '*', originOutputs: readNodeSlots(doc, String(tuple[1]), 'outputs'), - targetInputs: readNodeSlots(doc, String(tuple[3]), 'inputs') + ...target } } +function readDefinitions(doc: Y.Doc): SubgraphDefinitionIndex { + return indexSubgraphDefinitions(readSubgraphDefinitions(doc)) +} + function readNodeSlots( doc: Y.Doc, id: string, @@ -112,7 +289,11 @@ interface TargetSession { readonly nodeActions: Map readonly changedWidgets: Map> readonly replacedWidgetMaps: Set + readonly replacedOpaqueWidgets: Set + readonly changedNodeFields: Set readonly changedLinks: Set + /** Drift keys already surfaced via `reportError` for this session. */ + readonly reportedErrors: Set readonly frameQueue: DocUpdate[] onNodesChanged: (events: Y.YEvent>[]) => void onLinksChanged: (event: Y.YMapEvent) => void @@ -202,7 +383,10 @@ export class EcsFollowerAdapter { nodeActions: new Map(), changedWidgets: new Map>(), replacedWidgetMaps: new Set(), + replacedOpaqueWidgets: new Set(), + changedNodeFields: new Set(), changedLinks: new Set(), + reportedErrors: new Set(), frameQueue: [], reconcileNextFrame: true, applying: false, @@ -221,10 +405,18 @@ export class EcsFollowerAdapter { [...session.changedWidgets].map(([id, names]) => [id, new Set(names)]) ) const replacedWidgetMaps = new Set(session.replacedWidgetMaps) + const replacedOpaqueWidgets = new Set(session.replacedOpaqueWidgets) + const changedNodeFields = new Set(session.changedNodeFields) const changedLinkIds = new Set(session.changedLinks) const reconcile = session.reconcileNextFrame this.discardSessionPending(session) + const doc = session.follower.doc + // Most frames touch no links or hosts; index the definitions only when a + // reader first needs them. + let definitionIndex: SubgraphDefinitionIndex | undefined + const definitions = () => (definitionIndex ??= readDefinitions(doc)) + const replacedNodeIds = new Set( [...nodeActions] .filter(([, action]) => action === 'update') @@ -232,7 +424,12 @@ export class EcsFollowerAdapter { ) if (replacedNodeIds.size > 0) { session.links.forEach((_raw, id) => { - const link = readSemanticLink(session.follower.doc, id) + const link = readSemanticLink( + doc, + id, + definitions(), + session.reportedErrors + ) if ( link && (replacedNodeIds.has(String(link.originNodeId)) || @@ -243,43 +440,139 @@ export class EcsFollowerAdapter { }) } - const removedLinkIds = [...changedLinkIds].flatMap((id) => - session.links.has(id) ? [] : [Number(id)] + // A changed link the follower cannot read (gone from the doc, or re-minted + // by cmp onto a promoted slot the definition does not declare) must be + // retired live; otherwise its previous topology stays connected. + const changedLinks = new Map( + [...changedLinkIds].map((id) => [ + id, + session.links.has(id) + ? readSemanticLink(doc, id, definitions(), session.reportedErrors) + : null + ]) + ) + const removedLinkIds = [...changedLinks].flatMap(([id, link]) => + link ? [] : [Number(id)] ) const committed = session.mutations.batch(frameContext(update), (batch) => { + // A SubgraphNode host that is already live must never be rebuilt from + // its doc entry: `reconcileNode` (and delete + `addNode`) replaces the + // host's input list in place, which drops the `widgetId` / + // `_subgraphSlot` bindings its promoted widgets hang off, leaving the + // host with no widgets at all. Resync the host's scalar fields (title, + // mode, flags, properties, colors) and write the promoted values by name + // instead; `readSemanticNode` has already keyed them from the + // definition. A host whose stored values are not a record (malformed + // opaque payload) keeps its widgets untouched rather than wiped. + const isHost = (payload: SemanticNodePayload) => + definitions().has(payload.type) + const upsertNode = ( + payload: SemanticNodePayload, + mode: 'add' | 'reconcile' + ) => { + if (mode === 'add') { + batch.addNode(payload) + return + } + if (!isHost(payload)) { + batch.reconcileNode(payload) + return + } + batch.reconcileNodeFields(payload) + const values = payload.widgets_values + if (!isRecord(values)) return + for (const [name, value] of Object.entries(values)) { + batch.setWidget(toNodeId(payload.id), name, value) + } + } + if (reconcile) { const nodes = [...session.nodes.keys()].flatMap((id) => { - const payload = readSemanticNode(session.follower.doc, id) + const payload = readSemanticNode( + doc, + id, + definitions, + session.reportedErrors + ) return payload ? [payload] : [] }) const links = [...session.links.keys()].flatMap((id) => { - const link = readSemanticLink(session.follower.doc, id) + const link = readSemanticLink( + doc, + id, + definitions(), + session.reportedErrors + ) return link ? [link] : [] }) batch.removeMissing( nodes.map(({ id }) => toNodeId(id)), links.map(({ id }) => id) ) - for (const payload of nodes) batch.reconcileNode(payload) + for (const payload of nodes) upsertNode(payload, 'reconcile') for (const link of links) batch.connect(link) return } batch.removeLinks(removedLinkIds) + const payloads = new Map( + [...nodeActions] + .filter(([, action]) => action !== 'delete') + .map( + ([id]) => + [ + id, + readSemanticNode(doc, id, definitions, session.reportedErrors) + ] as const + ) + ) for (const [id, action] of nodeActions) { - if (action === 'delete' || action === 'update') + if (action === 'delete') { + batch.deleteNode(toNodeId(id)) + continue + } + const payload = payloads.get(id) + if (action === 'update' && !(payload && isHost(payload))) batch.deleteNode(toNodeId(id)) } - for (const [id, action] of nodeActions) { - if (action === 'delete') continue - const payload = readSemanticNode(session.follower.doc, id) + for (const [id, payload] of payloads) { if (!payload) continue - batch.addNode(payload) + upsertNode(payload, nodeActions.get(id) === 'add' ? 'add' : 'reconcile') } - for (const id of replacedWidgetMaps) { + // A node whose widget storage was replaced wholesale, either the named + // `widgets` map or the positional `__widgets_opaque` array (cmp writes + // both in one transaction when a host's storage flips to opaque, and + // deletes the opaque array when it flips back), is re-read in full. + for (const id of new Set([ + ...replacedWidgetMaps, + ...replacedOpaqueWidgets + ])) { if (nodeActions.has(id)) continue - const payload = readSemanticNode(session.follower.doc, id) - if (payload) batch.reconcileNode(payload) + const payload = readSemanticNode( + doc, + id, + definitions, + session.reportedErrors + ) + if (payload) upsertNode(payload, 'reconcile') + } + // A node whose scalar fields were edited by key (title, mode, flags, + // properties, colors) is re-read so a live host resyncs those fields + // without rebuilding its promoted widgets or slots. + for (const id of changedNodeFields) { + if ( + nodeActions.has(id) || + replacedWidgetMaps.has(id) || + replacedOpaqueWidgets.has(id) + ) + continue + const payload = readSemanticNode( + doc, + id, + definitions, + session.reportedErrors + ) + if (payload) upsertNode(payload, 'reconcile') } for (const [id, names] of changedWidgets) { if (nodeActions.has(id) || replacedWidgetMaps.has(id)) continue @@ -287,26 +580,30 @@ export class EcsFollowerAdapter { const widgets = node?.get('widgets') if (!(widgets instanceof Y.Map)) continue if ([...names].some((name) => !widgets.has(name))) { - const payload = readSemanticNode(session.follower.doc, id) - if (payload) batch.reconcileNode(payload) + const payload = readSemanticNode( + doc, + id, + definitions, + session.reportedErrors + ) + if (payload) upsertNode(payload, 'reconcile') continue } for (const name of names) { batch.setWidget(toNodeId(id), name, plain(widgets.get(name))) } } - for (const id of changedLinkIds) { - const link = readSemanticLink(session.follower.doc, id) + for (const link of changedLinks.values()) { if (link) batch.connect(link) } }) - // Only clear the reconciliation flag once the batch actually commits. - // A rejected batch (no scope, or validation failure) must leave - // 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 + // The pending sets were snapshotted and cleared before `batch` ran, so a + // rejected batch (no scope, or validation failure) has already lost the + // incremental record of this frame. Arm a full reconcile for the next + // frame so the dropped edits are re-read from the doc instead of falling + // through to incremental handling that never revisits them. + session.reconcileNextFrame = !committed return committed } @@ -314,6 +611,8 @@ export class EcsFollowerAdapter { session.nodeActions.clear() session.changedWidgets.clear() session.replacedWidgetMaps.clear() + session.replacedOpaqueWidgets.clear() + session.changedNodeFields.clear() session.changedLinks.clear() } @@ -322,6 +621,14 @@ export class EcsFollowerAdapter { events: Y.YEvent>[] ): void { for (const event of events) { + if (event instanceof Y.YArrayEvent) { + // cmp writes `__widgets_opaque` as a plain array, but `plain()` also + // accepts a shared Y.Array. In-place edits to that array arrive as + // array events, not as a key replace on the node map. + if (event.path.length === 2 && event.path[1] === OPAQUE_WIDGETS_KEY) + session.replacedOpaqueWidgets.add(String(event.path[0])) + continue + } if (!(event instanceof Y.YMapEvent)) continue if (event.target === session.nodes) { for (const [id, change] of event.changes.keys) @@ -338,8 +645,16 @@ export class EcsFollowerAdapter { continue } - if (event.path.length === 1 && event.keysChanged.has('widgets')) - session.replacedWidgetMaps.add(id) + if (event.path.length !== 1) continue + if (event.keysChanged.has('widgets')) session.replacedWidgetMaps.add(id) + if (event.keysChanged.has(OPAQUE_WIDGETS_KEY)) + session.replacedOpaqueWidgets.add(id) + for (const key of event.keysChanged) { + if (RESYNCED_NODE_FIELDS.has(key)) { + session.changedNodeFields.add(id) + break + } + } } }