diff --git a/src/renderer/core/layout/store/layoutStoreMintDelivery.test.ts b/src/renderer/core/layout/store/layoutStoreMintDelivery.test.ts index 7eabe86b413..4b034e8d544 100644 --- a/src/renderer/core/layout/store/layoutStoreMintDelivery.test.ts +++ b/src/renderer/core/layout/store/layoutStoreMintDelivery.test.ts @@ -127,7 +127,8 @@ describe('mint ports against the real layout store delivery', () => { layoutChanges: (listener) => layoutStore.onChange(listener), localActorPrefix: 'user-', getGraph: () => graph, - boundRootGraphId: () => toRootGraphId(graphId) + boundRootGraphId: () => toRootGraphId(graphId), + docPromotedWidgets: () => null }) }) @@ -358,7 +359,8 @@ describe('attachMintPortWiring: root graph scope across a tab switch', () => { layoutChanges: (listener) => layoutStore.onChange(listener), localActorPrefix: ACTOR_CONFIG.USER_PREFIX, getGraph: () => graphAdapter, - boundRootGraphId: () => boundRootGraphId + boundRootGraphId: () => boundRootGraphId, + docPromotedWidgets: () => null }) }) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 780976096fe..875926fa633 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -837,7 +837,8 @@ const isBoundWorkflowActive = computed(() => { const { status: crdtStatus, debugSnapshot: crdtDebugSnapshot, - enqueueHumanOperations + enqueueHumanOperations, + docPromotedWidgets } = useAgentCrdtFollower( boundWorkflowId, graphMutations, @@ -866,7 +867,8 @@ const mintPortWiring = attachMintPortWiring({ layoutChanges: (listener) => layoutStore.onChange(listener), localActorPrefix: ACTOR_CONFIG.USER_PREFIX, getGraph: () => (app.isGraphReady ? app.rootGraph : null), - boundRootGraphId + boundRootGraphId, + docPromotedWidgets }) const isCrdtDevPanelEnabled = resolveDebugPanelEnabled( agentPanelStore.enabled, diff --git a/src/workbench/extensions/agent/crdt/__fixtures__/crdtSession.ts b/src/workbench/extensions/agent/crdt/__fixtures__/crdtSession.ts index 0fe77853bd7..19503966f67 100644 --- a/src/workbench/extensions/agent/crdt/__fixtures__/crdtSession.ts +++ b/src/workbench/extensions/agent/crdt/__fixtures__/crdtSession.ts @@ -93,7 +93,8 @@ export const crdtTest = baseTest.extend({ layoutChanges: () => () => {}, localActorPrefix: 'user-', getGraph: () => graph, - boundRootGraphId: () => graphScopeOf(graph).rootGraphId + boundRootGraphId: () => graphScopeOf(graph).rootGraphId, + docPromotedWidgets: () => null }) cleanups.push(() => wiring.detach()) let sequence = 0 diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index dd7bb9444de..633a9642cb4 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -871,7 +871,8 @@ describe('reconcileAgentAdapters', () => { layoutChanges: (listener) => layoutStore.onChange(listener), localActorPrefix: 'user-', getGraph: () => graph, - boundRootGraphId: () => toRootGraphId(graph.id) + boundRootGraphId: () => toRootGraphId(graph.id), + docPromotedWidgets: () => null }) }) diff --git a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts index ab968138f5c..dca729b53e7 100644 --- a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts +++ b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts @@ -1,5 +1,7 @@ +import { readFileSync } from 'node:fs' + import { mint } from '@comfyorg/comfy-multi-player' -import type { WidgetCatalog } from '@comfyorg/comfy-multi-player' +import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' import { assert, describe, expect, it } from 'vitest' import * as Y from 'yjs' @@ -11,6 +13,7 @@ import type { } from '@/lib/litegraph/src/types/serialisation' import { + readDocPromotedWidgets, readSubgraphDefinitionIds, readSubgraphDefinitions } from './agentSubgraphDefinitions' @@ -92,6 +95,25 @@ function nestedDefinitionChain(depth: number): Record { return current } +describe('readDocPromotedWidgets', () => { + it('reconstructs a promoted host array from the definition and instance mirror', () => { + const workflow = JSON.parse( + readFileSync( + 'browser_tests/assets/missing/missing_model_promoted_widget.json', + 'utf8' + ) + ) as WorkflowJSON + const doc = mint(workflow, CATALOG) + + expect(readDocPromotedWidgets(doc, '2')).toEqual({ + valueCount: 1, + declaredNames: ['ckpt_name'], + promotedNames: ['ckpt_name'] + }) + doc.destroy() + }) +}) + describe('readSubgraphDefinitions', () => { it('returns nothing for a document without definitions', () => { expect(readSubgraphDefinitions(new Y.Doc())).toEqual([]) diff --git a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts index adbd64dfe28..8b7efbfc18d 100644 --- a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts +++ b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts @@ -1,4 +1,4 @@ -import { OPAQUE_WIDGETS_KEY } from '@comfyorg/comfy-multi-player' +import { OPAQUE_WIDGETS_KEY, nodesMap } from '@comfyorg/comfy-multi-player' import * as Y from 'yjs' import type { ExportedSubgraph } from '@/lib/litegraph/src/types/serialisation' @@ -353,3 +353,88 @@ export function readSubgraphDefinitions(doc: Y.Doc): ExportedSubgraph[] { }) return definitions } + +/** + * What the document knows about a node's promoted widget layout. + * + * `valueCount` sizes the positional array a promoted write indexes. + * `promotedNames` reconstructs that array's exact name order from the + * definition plus the instance's input mirror: a definition input is promoted + * when the mirror either omits it or carries its widget marker. Neither source + * is sufficient alone because the mirror can omit promoted values the array + * still carries. An unreadable array or name sequence is represented by null + * so the minter can fail closed. + * + * `promotedNames` separates ABSENT from UNREADABLE, because they license + * opposite answers on a first write: `undefined` is a document carrying no + * definition for the node, which contradicts no ordering and lets the live + * order build the array, while `null` is a definition whose inputs cannot be + * read, which must fail closed. + */ +export interface DocPromotedWidgets { + valueCount: number | null + declaredNames: readonly string[] + promotedNames: readonly string[] | null | undefined +} + +/** Null when the document holds no such node. */ +export function readDocPromotedWidgets( + doc: Y.Doc, + nodeId: string +): DocPromotedWidgets | null { + const node = nodesMap(doc).get(nodeId) + if (!(node instanceof Y.Map)) return null + const stored = node.get(OPAQUE_WIDGETS_KEY) + return { + valueCount: + stored === undefined + ? 0 + : stored instanceof Y.Array || Array.isArray(stored) + ? stored.length + : null, + declaredNames: declaredInputNames(doc, String(node.get('type') ?? '')), + promotedNames: promotedInputNames(doc, node, String(node.get('type') ?? '')) + } +} + +function namedInputs(source: unknown): Array<[string, unknown]> | null { + const inputs = + source instanceof Y.Array + ? source.toArray() + : Array.isArray(source) + ? source + : null + if (inputs === null) return null + const named: Array<[string, unknown]> = [] + for (const input of inputs) { + const name = readField(input, 'name') + if (typeof name !== 'string') return null + named.push([name, input]) + } + return named +} + +function promotedInputNames( + doc: Y.Doc, + node: Y.Map, + definitionId: string +): string[] | null | undefined { + const definition = definitionsMap(doc)?.get(definitionId) + if (!(definition instanceof Y.Map)) return undefined + const declared = namedInputs(definition.get('inputs')) + const instance = namedInputs(node.get('inputs')) + if (declared === null || instance === null) return null + const instanceByName = new Map(instance) + return declared.flatMap(([name]) => { + const input = instanceByName.get(name) + return input === undefined || readField(input, 'widget') !== undefined + ? [name] + : [] + }) +} + +function declaredInputNames(doc: Y.Doc, definitionId: string): string[] { + const definition = definitionsMap(doc)?.get(definitionId) + if (!(definition instanceof Y.Map)) return [] + return namedInputs(definition.get('inputs'))?.map(([name]) => name) ?? [] +} diff --git a/src/workbench/extensions/agent/crdt/liveWidgetProjection.test.ts b/src/workbench/extensions/agent/crdt/liveWidgetProjection.test.ts index 851c97777f8..c1fc02533dc 100644 --- a/src/workbench/extensions/agent/crdt/liveWidgetProjection.test.ts +++ b/src/workbench/extensions/agent/crdt/liveWidgetProjection.test.ts @@ -56,7 +56,8 @@ function withMintWiring( layoutChanges: () => () => undefined, localActorPrefix: 'user-', getGraph: () => graph, - boundRootGraphId: () => toRootGraphId(graph.id) + boundRootGraphId: () => toRootGraphId(graph.id), + docPromotedWidgets: () => null }) try { run(minted) diff --git a/src/workbench/extensions/agent/crdt/mintPortWiring.test.ts b/src/workbench/extensions/agent/crdt/mintPortWiring.test.ts index ade1fe0aa37..8ef92c24b73 100644 --- a/src/workbench/extensions/agent/crdt/mintPortWiring.test.ts +++ b/src/workbench/extensions/agent/crdt/mintPortWiring.test.ts @@ -104,7 +104,8 @@ describe('attachMintPortWiring', () => { }, localActorPrefix: 'user-', getGraph: () => graph, - boundRootGraphId: () => toRootGraphId(ROOT_ID) + boundRootGraphId: () => toRootGraphId(ROOT_ID), + docPromotedWidgets: () => null }) }) diff --git a/src/workbench/extensions/agent/crdt/mintPortWiring.ts b/src/workbench/extensions/agent/crdt/mintPortWiring.ts index 7f779b0ffb9..432c6239344 100644 --- a/src/workbench/extensions/agent/crdt/mintPortWiring.ts +++ b/src/workbench/extensions/agent/crdt/mintPortWiring.ts @@ -11,6 +11,7 @@ import type { LGraph } from '@/lib/litegraph/src/LGraph' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { RootGraphId } from '@/types/graphScopeId' import type { NodeId } from '@/types/nodeId' +import { toNodeId } from '@/types/nodeId' import type { WorkflowNode } from '@comfyorg/comfy-multi-player' import { useLinkStore } from '@/stores/linkStore' @@ -21,6 +22,7 @@ import { parseWidgetId } from '@/types/widgetId' import { findSubgraphNodePathById } from '@/utils/graphTraversalUtil' import type { GraphOperation } from './graphOperations' +import type { DocPromotedWidgets } from './agentSubgraphDefinitions' import { attachLayoutMintPort } from './layoutMintPort' import type { LayoutChangeView, LayoutMintPort } from './layoutMintPort' import { attachLinkMintPort } from './linkMintPort' @@ -59,6 +61,8 @@ export interface MintPortWiringDeps { * nodes into the old document. */ boundRootGraphId(): RootGraphId | null + /** The bound document's promoted-widget layout for a root node. */ + docPromotedWidgets(nodeId: NodeId): DocPromotedWidgets | null } export interface MintPortWiring { @@ -168,6 +172,7 @@ function valueWidgetsOnly( export function attachMintPortWiring(deps: MintPortWiringDeps): MintPortWiring { const session = createMintSession() + const widgetStore = useWidgetValueStore() const enqueue = (operations: GraphOperation[]) => { const pending = bufferedEnqueues.at(-1) if (pending) pending.push(() => deps.enqueue(operations)) @@ -239,6 +244,13 @@ export function attachMintPortWiring(deps: MintPortWiringDeps): MintPortWiring { if (!graph) return null return graph.rootGraph?.id ?? graph.id }, + rootNode(nodeId) { + return deps.getGraph()?.getNodeById(toNodeId(nodeId)) ?? null + }, + docPromotedWidgets: (nodeId) => deps.docPromotedWidgets(toNodeId(nodeId)), + widgetValue(widgetId) { + return widgetStore.getWidget(widgetId)?.value + }, resolveInteriorPath(owningGraphId) { const graph = deps.getGraph() if (!graph) return null @@ -248,7 +260,6 @@ export function attachMintPortWiring(deps: MintPortWiringDeps): MintPortWiring { }) const linkStore = useLinkStore() - const widgetStore = useWidgetValueStore() const detachLinkActions = linkStore.$onAction(({ name, args, after }) => { // The remote origin travels on the store call itself. Do not rely on the diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index e84a304ec1b..45b4dbb4660 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -13,12 +13,14 @@ import { reportError } from '@/platform/telemetry/reportError' import { api } from '@/scripts/api' import { app } from '@/scripts/app' import type { RemoteMutationContext } from '@/types/graphMutationContext' +import type { NodeId } from '@/types/nodeId' import { createUuidv4 } from '@/utils/uuid' import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' import type { MaterializableGraph } from './agentNodeMaterializer' import { reconcileAgentAdapters } from './agentNodeMaterializer' import { + readDocPromotedWidgets, readSubgraphDefinitionIds, readSubgraphDefinitions } from './agentSubgraphDefinitions' @@ -303,7 +305,9 @@ export function useAgentCrdtFollower( schemaError: null }), enqueueHumanOperations: (operations: GraphOperation[]) => - follower.value?.enqueueHumanOperations(operations) + follower.value?.enqueueHumanOperations(operations), + docPromotedWidgets: (nodeId: NodeId) => + follower.value?.docPromotedWidgets(nodeId) ?? null } } @@ -1000,6 +1004,8 @@ function startAgentCrdtFollower( status: readonly(status), debugSnapshot, enqueueHumanOperations: (operations: GraphOperation[]) => - coalescer.enqueue(operations) + coalescer.enqueue(operations), + docPromotedWidgets: (nodeId: NodeId) => + readDocPromotedWidgets(bridge.follower.doc, String(nodeId)) } } diff --git a/src/workbench/extensions/agent/crdt/widgetMintPort.test.ts b/src/workbench/extensions/agent/crdt/widgetMintPort.test.ts index 31c24335f42..39f6c49b2be 100644 --- a/src/workbench/extensions/agent/crdt/widgetMintPort.test.ts +++ b/src/workbench/extensions/agent/crdt/widgetMintPort.test.ts @@ -1,5 +1,12 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import { reportError } from '@/platform/telemetry/reportError' +import { toNodeId } from '@/types/nodeId' +import { widgetId } from '@/types/widgetId' +import type { WidgetId } from '@/types/widgetId' + +import type { DocPromotedWidgets } from './agentSubgraphDefinitions' import type { GraphOperation } from './graphOperations' import { createMintSession } from './mintSession' import type { MintSession } from './mintSession' @@ -8,6 +15,8 @@ import type { WidgetMintPort, WidgetSetView } from './widgetMintPort' const ROOT = 'root-uuid' +vi.mock('@/platform/telemetry/reportError', () => ({ reportError: vi.fn() })) + function widgetSet(overrides: Partial = {}): WidgetSetView { return { graphId: ROOT, @@ -28,6 +37,9 @@ describe('attachWidgetMintPort', () => { let interiorPaths: Map let session: MintSession let listeners: Set<(set: WidgetSetView) => void> + let rootNodes: Map + let docPromotedWidgets: DocPromotedWidgets | null + let widgetValues: Map function deliver(set: WidgetSetView): void { for (const listener of listeners) listener(set) @@ -41,6 +53,9 @@ describe('attachWidgetMintPort', () => { interiorPaths = new Map() session = createMintSession() listeners = new Set() + rootNodes = new Map() + docPromotedWidgets = null + widgetValues = new Map() port = attachWidgetMintPort({ events: { onSet: (listener) => { @@ -52,12 +67,25 @@ describe('attachWidgetMintPort', () => { isEnabled: () => enabled, isDocBound: () => bound, rootGraphId: () => root, + rootNode: (nodeId) => rootNodes.get(String(nodeId)) ?? null, + docPromotedWidgets: () => docPromotedWidgets, + widgetValue: (id) => widgetValues.get(id), resolveInteriorPath: (owningGraphId) => interiorPaths.get(owningGraphId) ?? null, enqueue: (operations) => minted.push(...operations) }) }) + function promotedHost(names: readonly string[]): LGraphNode { + return { + isSubgraphNode: () => true, + inputs: names.map((name) => ({ + name, + widgetId: widgetId(ROOT, toNodeId(7), name) + })) + } as unknown as LGraphNode + } + it('mints a name-keyed top-level set_widget with the old value', () => { deliver(widgetSet()) @@ -66,6 +94,114 @@ describe('attachWidgetMintPort', () => { ]) }) + it('mints a positional promoted-host write with all live sibling values', () => { + rootNodes.set('7', promotedHost(['prefix', 'text'])) + docPromotedWidgets = { + valueCount: 2, + declaredNames: ['prefix', 'clip', 'text'], + promotedNames: ['prefix', 'text'] + } + widgetValues.set(widgetId(ROOT, toNodeId(7), 'prefix'), 'a prefix') + + deliver(widgetSet({ name: 'text', value: 'a pasted prompt' })) + + expect(minted).toEqual([ + { + op: 'set_widget', + node_id: '7', + widget: 'text', + value: 'a pasted prompt', + old: 3, + promoted: { + value_index: 1, + instance_path: ['7'], + host_widgets_values: ['a prefix', 'a pasted prompt'] + } + } + ]) + }) + + it('seeds the first promoted write in live order when the document declares no definition', () => { + rootNodes.set('7', promotedHost(['prefix', 'text'])) + docPromotedWidgets = { + valueCount: 0, + declaredNames: [], + promotedNames: undefined + } + + deliver(widgetSet({ name: 'prefix', value: 'p' })) + + expect(minted).toEqual([ + expect.objectContaining({ + promoted: { + value_index: 0, + instance_path: ['7'], + host_widgets_values: ['p', undefined] + } + }) + ]) + }) + + it.for([ + { + name: 'a reordered promoted layout', + layout: { + valueCount: 2, + declaredNames: ['text', 'prefix'], + promotedNames: ['text', 'prefix'] + } + }, + { + name: 'a stored array sized for another layout', + layout: { + valueCount: 3, + declaredNames: ['prefix', 'text'], + promotedNames: ['prefix', 'text'] + } + }, + { + name: 'an empty array whose definition orders differently', + layout: { + valueCount: 0, + declaredNames: ['text', 'prefix'], + promotedNames: ['text', 'prefix'] + } + }, + { + name: 'unreadable definition inputs', + layout: { valueCount: 2, declaredNames: [], promotedNames: null } + } + ])('refuses and reports $name once per host', ({ layout }) => { + rootNodes.set('7', promotedHost(['prefix', 'text'])) + docPromotedWidgets = layout + + deliver(widgetSet({ name: 'text', value: 'p' })) + deliver(widgetSet({ name: 'text', value: 'pa' })) + + expect(minted).toEqual([]) + expect(reportError).toHaveBeenCalledOnce() + expect(reportError).toHaveBeenCalledWith( + expect.any(Error), + expect.objectContaining({ + errorType: 'agent_crdt_promoted_widget_order_drift' + }) + ) + }) + + it('drops a subgraph-host widget that is not promoted', () => { + rootNodes.set('7', promotedHost(['prefix', 'text'])) + docPromotedWidgets = { + valueCount: 2, + declaredNames: ['prefix', 'text'], + promotedNames: ['prefix', 'text'] + } + + deliver(widgetSet({ name: 'extra', value: 'ignored' })) + + expect(minted).toEqual([]) + expect(reportError).not.toHaveBeenCalled() + }) + it('never mints with the product flag off', () => { enabled = false deliver(widgetSet()) diff --git a/src/workbench/extensions/agent/crdt/widgetMintPort.ts b/src/workbench/extensions/agent/crdt/widgetMintPort.ts index c57bde20491..07bf8db447d 100644 --- a/src/workbench/extensions/agent/crdt/widgetMintPort.ts +++ b/src/workbench/extensions/agent/crdt/widgetMintPort.ts @@ -1,9 +1,19 @@ /** * set_widget from the widgetValueStore setValue seam - NAME-KEYED by - * construction (WidgetId is graphId:nodeId:name; FE-1904). Subgraph-owned - * writes mint the interior form (path = resolved node-id chain, - * inner_widget = the name); an unresolvable owner surfaces, never drops. + * construction (WidgetId is graphId:nodeId:name; FE-1904). Top-level + * subgraph hosts mint the positional promoted payload their opaque document + * storage accepts. Subgraph-owned writes mint the interior form (path = + * resolved node-id chain, inner_widget = the name); an unresolvable owner + * surfaces, never drops. */ +import type { PromotedHostWrite } from '@comfyorg/comfy-multi-player' + +import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import { isWidgetValue } from '@/lib/litegraph/src/types/widgets' +import { reportError } from '@/platform/telemetry/reportError' +import type { WidgetId } from '@/types/widgetId' + +import type { DocPromotedWidgets } from './agentSubgraphDefinitions' import type { GraphOperation } from './graphOperations' import { shouldMint } from './mintGate' import type { MintSession } from './mintSession' @@ -34,6 +44,12 @@ export interface WidgetMintPortDeps { isDocBound(): boolean /** The active root graph id, or null when no workflow is open. */ rootGraphId(): string | null + /** Resolve a node on the active root graph. */ + rootNode(nodeId: string | number): LGraphNode | null + /** The bound document's promoted-widget layout for a root node. */ + docPromotedWidgets(nodeId: string | number): DocPromotedWidgets | null + /** Read a live widget value without coupling this port to Pinia. */ + widgetValue(widgetId: WidgetId): unknown /** * Subgraph-node id chain from the root to the definition owning * `owningGraphId`, or null when unreachable (wiring resolves it over the @@ -48,7 +64,52 @@ export interface WidgetMintPort { detach(): void } +function documentAcceptsLiveIndex( + doc: DocPromotedWidgets | null, + liveNames: readonly string[] +): boolean { + if (doc === null) return true + const namesMatch = + doc.promotedNames != null && + doc.promotedNames.length === liveNames.length && + doc.promotedNames.every((name, index) => name === liveNames[index]) + if (doc.valueCount === 0) return doc.promotedNames === undefined || namesMatch + return doc.valueCount === liveNames.length && namesMatch +} + +function promotedHostWrite( + node: LGraphNode, + set: WidgetSetView, + doc: DocPromotedWidgets | null, + widgetValue: WidgetMintPortDeps['widgetValue'] +): PromotedHostWrite | null { + const hostInputs = node.inputs.flatMap((input) => + input.widgetId ? [{ name: input.name, widgetId: input.widgetId }] : [] + ) + const valueIndex = hostInputs.findIndex((input) => input.name === set.name) + if (valueIndex === -1) return null + if ( + !documentAcceptsLiveIndex( + doc, + hostInputs.map((input) => input.name) + ) + ) { + return null + } + return { + value_index: valueIndex, + instance_path: [String(set.nodeId)], + host_widgets_values: hostInputs.map((input, index) => { + if (index === valueIndex) return set.value + const value = widgetValue(input.widgetId) + return isWidgetValue(value) ? value : undefined + }) + } +} + export function attachWidgetMintPort(deps: WidgetMintPortDeps): WidgetMintPort { + const reportedDrift = new Set() + function onSet(set: WidgetSetView): void { const mintable = shouldMint({ flagEnabled: deps.isEnabled(), @@ -59,6 +120,53 @@ export function attachWidgetMintPort(deps: WidgetMintPortDeps): WidgetMintPort { const root = deps.rootGraphId() if (root !== null && set.graphId === root) { + const node = deps.rootNode(set.nodeId) + if (node?.isSubgraphNode()) { + const isPromoted = node.inputs.some( + (input) => input.widgetId && input.name === set.name + ) + if (!isPromoted) return + const doc = deps.docPromotedWidgets(set.nodeId) + const promoted = promotedHostWrite(node, set, doc, deps.widgetValue) + if (!promoted) { + if (doc) { + const key = `${root}:${String(set.nodeId)}` + if (!reportedDrift.has(key)) { + reportedDrift.add(key) + const liveNames = node.inputs.flatMap((input) => + input.widgetId ? [input.name] : [] + ) + reportError( + new Error( + `Subgraph host ${String(set.nodeId)} promotes [${liveNames.join(', ')}], which the document's ${doc.valueCount} stored values and declared inputs [${doc.declaredNames.join(', ')}] do not place; refusing to mint a promoted write` + ), + { + errorType: 'agent_crdt_promoted_widget_order_drift', + context: { + nodeId: set.nodeId, + liveNames, + docValueCount: doc.valueCount, + docDeclaredNames: doc.declaredNames, + docPromotedNames: doc.promotedNames + } + } + ) + } + } + return + } + deps.enqueue([ + { + op: 'set_widget', + node_id: set.nodeId, + widget: set.name, + value: set.value, + old: set.old, + promoted + } + ]) + return + } deps.enqueue([ { op: 'set_widget',