From 6908e41e686491f9523d28c7781af9ad8c876803 Mon Sep 17 00:00:00 2001 From: kishore Date: Wed, 14 Oct 2026 21:23:26 -0700 Subject: [PATCH] [backport cloud/1.56] fix(agent): mint a promoted host write so prompt-node edits aren't rejected (#20377) (cherry picked from commit a0261ac4e7da58a528c0355463da55678734d196) --- .../extensions/agent/AgentPanelRoot.vue | 6 +- .../agent/crdt/agentSubgraphDefinitions.ts | 87 +++- .../extensions/agent/crdt/docOpMinter.test.ts | 470 +++++++++++++++++- .../extensions/agent/crdt/docOpMinter.ts | 140 +++++- ...linkMintOutboundPositionCorruption.test.ts | 6 +- ...gentCrdtFollower.humanAddTabSwitch.test.ts | 3 +- .../agent/crdt/useAgentCrdtFollower.ts | 9 +- 7 files changed, 703 insertions(+), 18 deletions(-) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index a830ebf1d5c..9568c69064c 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -906,7 +906,8 @@ const { status: crdtStatus, debugSnapshot: crdtDebugSnapshot, enqueueHumanOperations, - docInputNames + docInputNames, + docPromotedWidgets } = useAgentCrdtFollower( boundWorkflowId, () => resolvedUserInfo.value?.id ?? null, @@ -965,7 +966,8 @@ const docOpMinter = attachDocOpMinter({ enqueue: enqueueHumanOperations, getGraph: () => (app.isGraphReady ? app.rootGraph : null), boundRootGraphId, - docInputNames + docInputNames, + docPromotedWidgets }) const restoreOpMinter = attachRestoreOpMinter({ isEnabled: () => agentPanelStore.enabled, diff --git a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts index 69b525ac60d..e5675528c3a 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' @@ -384,3 +384,88 @@ export function readSubgraphDefinitions( }) 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/docOpMinter.test.ts b/src/workbench/extensions/agent/crdt/docOpMinter.test.ts index 772ac32ab43..cc7c65af391 100644 --- a/src/workbench/extensions/agent/crdt/docOpMinter.test.ts +++ b/src/workbench/extensions/agent/crdt/docOpMinter.test.ts @@ -1,3 +1,4 @@ +import { readFileSync } from 'node:fs' import { applyOps, mint, project } from '@comfyorg/comfy-multi-player' import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' import { fromPartial } from '@total-typescript/shoehorn' @@ -32,6 +33,7 @@ import { createUuidv4 } from '@/utils/uuid' import { attachDocOpMinter, wireNodeSnapshot } from './docOpMinter' import type { DocOpMinter, DocOpMinterDeps } from './docOpMinter' import type { GraphOperation } from './graphOperations' +import { readDocPromotedWidgets } from './agentSubgraphDefinitions' import { readDocSlotNames } from './liveGraphApplier' import { mintWireOps } from './opEnvelope' @@ -88,11 +90,23 @@ class TestNote extends LGraphNode { } } +/** A widget-backed input, so a subgraph can promote it onto its instance. */ +class TestPrompt extends LGraphNode { + constructor() { + super('Test Prompt') + const input = this.addInput('text', 'STRING') + input.widget = { name: 'text' } + this.addWidget('text', 'text', 'an interior default', () => {}) + this.serialize_widgets = true + } +} + const CATALOG: WidgetCatalog = { types: { TestSource: { widget_order: ['steps'] }, TestSink: { widget_order: [] }, - TestAutogrowSink: { widget_order: [] } + TestAutogrowSink: { widget_order: [] }, + TestPrompt: { widget_order: ['text'] } } } @@ -106,12 +120,16 @@ const zDocInputs = z.array( z.object({ name: z.string(), link: z.number().nullable() }) ) -function applyMinted(doc: ReturnType, ops: GraphOperation[]) { +function applyOutcomes(doc: ReturnType, ops: GraphOperation[]) { return applyOps( doc, mintWireOps(ops, { actor: 'human:user:tab', baseVersion: 1 }), CATALOG - ).outcomes.map((outcome) => outcome.outcome) + ).outcomes +} + +function applyMinted(doc: ReturnType, ops: GraphOperation[]) { + return applyOutcomes(doc, ops).map((outcome) => outcome.outcome) } function docInputs(doc: ReturnType, nodeId: unknown) { @@ -143,6 +161,7 @@ beforeEach(() => { LiteGraph.registerNodeType('TestSink', TestSink) LiteGraph.registerNodeType('TestAutogrowSink', TestAutogrowSink) LiteGraph.registerNodeType('TestNote', TestNote) + LiteGraph.registerNodeType('TestPrompt', TestPrompt) }) describe('attachDocOpMinter', () => { @@ -153,6 +172,7 @@ describe('attachDocOpMinter', () => { let enabled: boolean let bound: boolean let docInputNames: DocOpMinterDeps['docInputNames'] + let docPromotedWidgets: DocOpMinterDeps['docPromotedWidgets'] beforeEach(() => { vi.mocked(reportError).mockClear() @@ -162,13 +182,15 @@ describe('attachDocOpMinter', () => { enabled = true bound = true docInputNames = () => null + docPromotedWidgets = () => null minter = attachDocOpMinter({ isEnabled: () => enabled, isDocBound: () => bound, enqueue: (operations) => minted.push(...operations), getGraph: () => graph, boundRootGraphId: () => rootGraphId, - docInputNames: (nodeId) => docInputNames(nodeId) + docInputNames: (nodeId) => docInputNames(nodeId), + docPromotedWidgets: (nodeId) => docPromotedWidgets(nodeId) }) }) @@ -456,7 +478,8 @@ describe('attachDocOpMinter', () => { enqueue, getGraph: () => graph, boundRootGraphId: () => rootGraphId, - docInputNames: () => null + docInputNames: () => null, + docPromotedWidgets: () => null }) const added = new TestSink() @@ -666,6 +689,443 @@ describe('attachDocOpMinter', () => { ]) }) + function seedPromotedHost(hostWidgetValues?: unknown[]) { + const subgraph = createTestSubgraph({ + rootGraph: graph, + inputs: [ + { name: 'prefix', type: 'STRING' }, + { name: 'text', type: 'STRING' } + ] + }) + graph.subgraphs.set(subgraph.id, subgraph) + const host = createTestSubgraphNode(subgraph) + withGraphIntentSource('load', () => { + graph.add(host) + for (const index of [0, 1]) { + const interior = LiteGraph.createNode('TestPrompt') + assert.exists(interior) + subgraph.add(interior) + subgraph.inputNode.slots[index].connect(interior.inputs[0], interior) + } + }) + const serialized = graph.serialize() as unknown as WorkflowJSON + if (hostWidgetValues) { + const hostNode = serialized.nodes.find( + (node) => String(node.id) === String(host.id) + ) + assert.exists(hostNode) + hostNode.widgets_values = hostWidgetValues + } + const doc = mint(serialized, CATALOG) + docPromotedWidgets = (nodeId) => readDocPromotedWidgets(doc, String(nodeId)) + return { host, doc } + } + + it('PM-1995: mints a promoted host write the doc host accepts', async () => { + const { host, doc } = seedPromotedHost() + expect(host.inputs.map((input) => input.name)).toEqual(['prefix', 'text']) + + host.widgets[1].value = 'a prompt pasted while the agent panel is open' + await afterFlush() + + expect(minted).toEqual([ + { + op: 'set_widget', + node_id: host.id, + widget: 'text', + value: 'a prompt pasted while the agent panel is open', + old: 'an interior default', + promoted: { + value_index: 1, + instance_path: [String(host.id)], + host_widgets_values: [ + 'an interior default', + 'a prompt pasted while the agent panel is open' + ] + } + } + ]) + const [write] = minted + assert(write.op === 'set_widget' && write.path == null) + const { promoted: _dropped, ...named } = write + expect(applyOutcomes(doc, [named])).toEqual([ + expect.objectContaining({ + outcome: 'rejected', + reason: expect.objectContaining({ code: 'opaque_widgets' }) + }) + ]) + + expect(applyMinted(doc, minted)).toEqual(['applied']) + expect( + project(doc, CATALOG).nodes.find( + (node) => String(node.id) === String(host.id) + )?.widgets_values + ).toEqual([ + 'an interior default', + 'a prompt pasted while the agent panel is open' + ]) + doc.destroy() + }) + + it('builds the array from the host values when the document holds none', async () => { + const { host, doc } = seedPromotedHost([]) + + host.widgets[1].value = 'pasted' + await afterFlush() + + expect(minted).toEqual([ + expect.objectContaining({ + promoted: { + value_index: 1, + instance_path: [String(host.id)], + host_widgets_values: ['an interior default', 'pasted'] + } + }) + ]) + expect(applyMinted(doc, minted)).toEqual(['applied']) + expect( + project(doc, CATALOG).nodes.find( + (node) => String(node.id) === String(host.id) + )?.widgets_values + ).toEqual(['an interior default', 'pasted']) + doc.destroy() + }) + + it.for([ + { + name: 'sizes its array for a different set of widgets', + doc: { + valueCount: 3, + declaredNames: [], + promotedNames: ['prefix', 'text', 'other'] + } + }, + { + name: 'declares the promoted widgets in another order', + doc: { + valueCount: 2, + declaredNames: ['text', 'clip', 'prefix'], + promotedNames: ['text', 'prefix'] + } + }, + { + name: 'stores another same-size promoted widget sequence', + doc: { + valueCount: 2, + declaredNames: ['prefix', 'clip', 'text'], + promotedNames: ['prefix', 'clip'] + } + }, + { + name: 'cannot read the promoted widget sequence', + doc: { valueCount: 2, declaredNames: [], promotedNames: null } + }, + { + name: 'cannot read the stored widget array', + doc: { valueCount: null, declaredNames: [], promotedNames: null } + } + ])( + 'keeps the refusal rather than misplacing a value when the document $name', + async ({ doc: docWidgets }) => { + const { host, doc } = seedPromotedHost() + docPromotedWidgets = () => docWidgets + + host.widgets[1].value = 'pasted' + await afterFlush() + + expect(minted).toEqual([]) + expect(reportError).toHaveBeenCalledWith( + expect.any(Error), + expect.objectContaining({ + errorType: 'agent_crdt_promoted_widget_order_drift' + }) + ) + doc.destroy() + } + ) + + it('drops a drifted host write without poisoning its same-tick batch', async () => { + const source = new TestSource() + withGraphIntentSource('load', () => graph.add(source)) + const { host, doc } = seedPromotedHost() + docPromotedWidgets = () => ({ + valueCount: 2, + declaredNames: ['text', 'prefix'], + promotedNames: ['text', 'prefix'] + }) + + host.widgets[1].value = 'misplaced' + source.widgets![0].value = 42 + await afterFlush() + + expect(minted).toEqual([ + { + op: 'set_widget', + node_id: source.id, + widget: 'steps', + value: 42, + old: 20 + } + ]) + expect(applyMinted(doc, minted)).toEqual(['applied']) + doc.destroy() + }) + + it('drops an unpromoted host widget without poisoning its same-tick batch', async () => { + const source = new TestSource() + withGraphIntentSource('load', () => graph.add(source)) + const { host, doc } = seedPromotedHost() + const extra = host.addWidget('text', 'extra', 'before', () => {}) + + extra.value = 'after' + source.widgets![0].value = 42 + await afterFlush() + + expect(minted).toEqual([ + { + op: 'set_widget', + node_id: source.id, + widget: 'steps', + value: 42, + old: 20 + } + ]) + expect(applyMinted(doc, minted)).toEqual(['applied']) + doc.destroy() + }) + + it('seeds an unset sibling exactly as serializing the host would', async () => { + const { host, doc } = seedPromotedHost([]) + host.widgets[0].value = undefined + await afterFlush() + minted.length = 0 + + host.widgets[1].value = 'pasted' + await afterFlush() + + const [write] = minted + assert(write.op === 'set_widget' && write.path == null) + expect(write.promoted?.host_widgets_values).toEqual( + host.serialize().widgets_values + ) + + // Over the wire, where an unset sibling becomes the same `null` a saved + // workflow carries for it (`widgetValueNullContract`). + const overTheWire = JSON.parse(JSON.stringify(minted)) as GraphOperation[] + expect(applyMinted(doc, overTheWire)).toEqual(['applied']) + expect( + project(doc, CATALOG).nodes.find( + (node) => String(node.id) === String(host.id) + )?.widgets_values + ).toEqual([null, 'pasted']) + doc.destroy() + }) + + it('reports a drifted host once, not once per keystroke', async () => { + const { host, doc } = seedPromotedHost() + docPromotedWidgets = () => ({ + valueCount: 2, + declaredNames: ['text', 'prefix'], + promotedNames: ['text', 'prefix'] + }) + + for (const value of ['p', 'pa', 'pas']) { + host.widgets[1].value = value + await afterFlush() + } + + expect(minted).toHaveLength(0) + expect( + vi + .mocked(reportError) + .mock.calls.filter( + ([, options]) => + options.errorType === 'agent_crdt_promoted_widget_order_drift' + ) + ).toHaveLength(1) + doc.destroy() + }) + + it('reports the same drifted node id again in another workflow', async () => { + const first = seedPromotedHost() + docPromotedWidgets = () => ({ + valueCount: 2, + declaredNames: ['text', 'prefix'], + promotedNames: ['text', 'prefix'] + }) + first.host.widgets[1].value = 'first workflow' + await afterFlush() + + graph = new LGraph() + rootGraphId = toRootGraphId(graph.id) + const second = seedPromotedHost() + docPromotedWidgets = () => ({ + valueCount: 2, + declaredNames: ['text', 'prefix'], + promotedNames: ['text', 'prefix'] + }) + second.host.widgets[1].value = 'second workflow' + await afterFlush() + + expect( + vi + .mocked(reportError) + .mock.calls.filter( + ([, options]) => + options.errorType === 'agent_crdt_promoted_widget_order_drift' + ) + ).toHaveLength(2) + first.doc.destroy() + second.doc.destroy() + }) + + it('reads a shipped host whose inputs mirror under-reports its values', () => { + const workflow = JSON.parse( + readFileSync( + 'browser_tests/assets/subgraphs/agent-subgraph-with-two-promoted-widgets.json', + 'utf8' + ) + ) as WorkflowJSON + const doc = mint(workflow, CATALOG) + const host = workflow.nodes.find((node) => String(node.id) === '11') + + // `text` holds a value and is declared by the definition, but the + // instance's own `inputs` mirror omits it โ€” which is why the definition, + // not the mirror, is what places a write. + expect(host?.widgets_values).toEqual(['a photo of a pier', 0]) + expect(readDocPromotedWidgets(doc, '11')).toEqual({ + valueCount: 2, + declaredNames: [ + 'text', + 'clip', + 'model', + 'positive', + 'negative', + 'latent_image', + 'seed' + ], + promotedNames: ['text', 'seed'] + }) + doc.destroy() + }) + + it('marks stored values unreadable when their definition is unavailable', () => { + const workflow = JSON.parse( + readFileSync( + 'browser_tests/assets/subgraphs/agent-subgraph-with-two-promoted-widgets.json', + 'utf8' + ) + ) as WorkflowJSON + const doc = mint(workflow, CATALOG) + const host = workflow.nodes.find((node) => String(node.id) === '11') + assert(typeof host?.type === 'string') + doc.getMap('definitions').delete(host.type) + + expect(readDocPromotedWidgets(doc, '11')).toEqual({ + valueCount: 2, + declaredNames: [], + promotedNames: undefined + }) + doc.destroy() + }) + + it('mints for a shipped host whose inputs mirror omits a promoted widget', async () => { + const { host, doc } = seedPromotedHost() + docPromotedWidgets = () => ({ + valueCount: 2, + declaredNames: ['prefix', 'clip', 'text'], + promotedNames: ['prefix', 'text'] + }) + + host.widgets[1].value = 'pasted' + await afterFlush() + + expect(minted).toEqual([ + expect.objectContaining({ + widget: 'text', + promoted: expect.objectContaining({ value_index: 1 }) + }) + ]) + expect(applyMinted(doc, minted)).toEqual(['applied']) + doc.destroy() + }) + + it('mints against the live order when the document declares no definition', async () => { + const { host, doc } = seedPromotedHost() + docPromotedWidgets = () => ({ + valueCount: 0, + declaredNames: [], + promotedNames: undefined + }) + + host.widgets[1].value = 'pasted' + await afterFlush() + + expect(minted).toEqual([ + expect.objectContaining({ + promoted: expect.objectContaining({ value_index: 1 }) + }) + ]) + expect(applyMinted(doc, minted)).toEqual(['applied']) + doc.destroy() + }) + + it.for([ + { + name: 'declares another promoted order', + promotedNames: ['text', 'prefix'] + }, + { name: 'cannot read its declared inputs', promotedNames: null } + ])( + 'refuses to seed an empty document array when the definition $name', + async ({ promotedNames }) => { + const { host, doc } = seedPromotedHost([]) + docPromotedWidgets = () => ({ + valueCount: 0, + declaredNames: [], + promotedNames + }) + + host.widgets[1].value = 'pasted' + await afterFlush() + + expect(minted).toEqual([]) + doc.destroy() + } + ) + + it('leaves a promoted widget on a nested host on the interior route', async () => { + const outer = createTestSubgraph({ rootGraph: graph }) + graph.subgraphs.set(outer.id, outer) + const inner = createTestSubgraph({ + rootGraph: graph, + inputs: [{ name: 'text', type: 'STRING' }] + }) + graph.subgraphs.set(inner.id, inner) + const outerHost = createTestSubgraphNode(outer) + const nestedHost = createTestSubgraphNode(inner, { parentGraph: outer }) + withGraphIntentSource('load', () => { + graph.add(outerHost) + outer.add(nestedHost) + const interior = LiteGraph.createNode('TestPrompt') + assert.exists(interior) + inner.add(interior) + inner.inputNode.slots[0].connect(interior.inputs[0], interior) + }) + + nestedHost.widgets[0].value = 'pasted' + await afterFlush() + + expect(minted).toEqual([ + expect.objectContaining({ + op: 'set_widget', + path: [String(outerHost.id), String(nestedHost.id)], + inner_widget: 'text' + }) + ]) + expect(minted[0]).not.toHaveProperty('promoted') + }) + it('does not let an ephemeral widget write roll a hand edit back with it', async () => { const { source } = seedGraph(graph) // Built the way useNodeProgressText builds it: `serialize: false` and diff --git a/src/workbench/extensions/agent/crdt/docOpMinter.ts b/src/workbench/extensions/agent/crdt/docOpMinter.ts index 4291d275bd2..4b984d604f2 100644 --- a/src/workbench/extensions/agent/crdt/docOpMinter.ts +++ b/src/workbench/extensions/agent/crdt/docOpMinter.ts @@ -13,6 +13,7 @@ */ import type { NodeId as WireNodeId, + PromotedHostWrite, WorkflowNode } from '@comfyorg/comfy-multi-player' @@ -26,6 +27,7 @@ import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { LLink } from '@/lib/litegraph/src/LLink' import type { Subgraph } from '@/lib/litegraph/src/subgraph/Subgraph' import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' +import { isWidgetValue } from '@/lib/litegraph/src/types/widgets' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { reportError } from '@/platform/telemetry/reportError' import { useWidgetValueStore } from '@/stores/widgetValueStore' @@ -41,6 +43,7 @@ import { } from '@/utils/graphTraversalUtil' import type { GraphOperation } from './graphOperations' +import type { DocPromotedWidgets } from './agentSubgraphDefinitions' export interface DocOpMinterDeps { /** Slice 00's product gate. */ @@ -64,6 +67,11 @@ export interface DocOpMinterDeps { * flight, or no document is subscribed). */ docInputNames(nodeId: NodeId): readonly (string | undefined)[] | null + /** + * The bound document's view of a node's promoted widget layout, or null + * when the document holds no such node. + */ + docPromotedWidgets(nodeId: NodeId): DocPromotedWidgets | null } export interface DocOpMinter { @@ -178,11 +186,97 @@ function isValueWidgetWrite( return isValueWidget(widget, stored) } +/** + * The positional payload a write to a PROMOTED widget needs (schema Amendment + * A15). A subgraph instance's `type` is a definition UUID, so the pinned + * catalog never describes it and the document stores its promoted values as + * one opaque positional array (schema ยง1.2) that no name can address: a named + * `set_widget` against such a node is rejected outright (`opaque_widgets`), + * which bounced edits to a promoted widget while the agent held the document + * (PM-1995). A host nested inside another definition still takes the interior + * route below and is still refused. + * + * `value_index` is the widget's position among the node's widget-backed + * inputs โ€” the order `SubgraphNode.serialize` builds `widgets_values` in and + * the order `applyHostWidgets` reads it back in. The document is consulted + * only to REFUSE a write it would misplace, never as the index itself: its + * `inputs` mirror can widget-mark fewer slots than `widgets_values` has + * entries, so an index taken from it lands on another widget's value. + */ +function promotedHostWrite( + node: LGraphNode | null, + event: IntentOf<'set_widget'>, + docPromotedWidgets: () => DocPromotedWidgets | null, + onOrderDrift: (names: readonly string[], doc: DocPromotedWidgets) => void +): PromotedHostWrite | null { + if (!node?.isSubgraphNode()) return null + const hostInputs = node.inputs.flatMap((input) => + input.widgetId ? [{ name: input.name, widgetId: input.widgetId }] : [] + ) + const valueIndex = hostInputs.findIndex((input) => input.name === event.name) + if (valueIndex === -1) return null + const liveNames = hostInputs.map((input) => input.name) + const doc = docPromotedWidgets() + if (!documentAcceptsLiveIndex(doc, liveNames)) { + if (doc) onOrderDrift(liveNames, doc) + return null + } + const widgetValueStore = useWidgetValueStore() + return { + value_index: valueIndex, + instance_path: [String(event.nodeId)], + host_widgets_values: hostInputs.map((input, index) => { + if (index === valueIndex) return event.value + const value = widgetValueStore.getWidget(input.widgetId)?.value + return isWidgetValue(value) ? value : undefined + }) + } +} + +/** + * Whether a live index is a safe index into the document's array. + * + * Two independent ways it is not, and the document answers both: + * + * - The array is sized for a different set of widgets. Checked against + * `valueCount`, the same cardinality invariant `applyHostWidgets` enforces + * on the way in. + * - The promoted name sequence is different. Definition order combined with + * the instance's stored input mirror identifies the exact names behind the + * positional array, catching same-cardinality demote/promote swaps as well + * as `reorderSubgraphInputsByWidgetOrder` permutations. + * + * A document holding no array yet is sized by nothing, but it is NOT ordered + * by nothing: the first write seeds the whole array from `host_widgets_values` + * in live order, and a reload reads it back in the DEFINITION's order + * (`_applyPromotedWidgetValues`). A reorder permutes the live definition + * without minting, so the document's copy still holds the old order โ€” seeding + * against it would land every value on a neighbour. Only a document that + * declares no definition at all leaves the live order unopposed. + * + * A refused write is dropped before enqueue so its `opaque_widgets` rejection + * cannot abort unrelated operations in the same batch. + */ +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 routedWidgetOperation( graph: LGraph, rootGraphId: string, event: IntentOf<'set_widget'>, - node: LGraphNode | null + node: LGraphNode | null, + docPromotedWidgets: () => DocPromotedWidgets | null, + onOrderDrift: (names: readonly string[], doc: DocPromotedWidgets) => void ): GraphOperation | null { const operation = { op: 'set_widget', @@ -192,7 +286,16 @@ function routedWidgetOperation( old: event.previous } as const const owningGraphId = node?.graph?.id ?? event.graphId - if (owningGraphId === rootGraphId) return operation + if (owningGraphId === rootGraphId) { + if (!node?.isSubgraphNode()) return operation + const promoted = promotedHostWrite( + node, + event, + docPromotedWidgets, + onOrderDrift + ) + return promoted ? { ...operation, promoted } : null + } const subgraphNodePath = findSubgraphNodePathById(graph, owningGraphId) if (subgraphNodePath === null || subgraphNodePath.length === 0) { console.error( @@ -266,6 +369,10 @@ export function attachDocOpMinter(deps: DocOpMinterDeps): DocOpMinter { let pending: PendingOp[] = [] const pendingAdds = new Map() const reported = new Set() + // Budgeted for the minter's whole life, not per flush: this one sits on the + // keystroke-paced widget path, where a per-flush budget reports every + // character typed into a drifted host. + const reportedDrift = new Set() let flushScheduled = false let detached = false @@ -314,10 +421,11 @@ export function attachDocOpMinter(deps: DocOpMinterDeps): DocOpMinter { key: string, message: string, errorType: string, - context: Record + context: Record, + budget: Set = reported ): void { - if (reported.has(key)) return - reported.add(key) + if (budget.has(key)) return + budget.add(key) reportError(new Error(message), { surface: 'agent', errorType, context }) } @@ -360,7 +468,27 @@ export function attachDocOpMinter(deps: DocOpMinterDeps): DocOpMinter { : null if (!isValueWidgetWrite(owner, event)) return const rootGraphId = deps.boundRootGraphId() ?? graph.id - const operation = routedWidgetOperation(graph, rootGraphId, event, owner) + const operation = routedWidgetOperation( + graph, + rootGraphId, + event, + owner, + () => deps.docPromotedWidgets(event.nodeId), + (liveNames, doc) => + reportOnce( + `promoted_drift:${rootGraphId}:${String(event.nodeId)}`, + `Subgraph host ${String(event.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`, + 'agent_crdt_promoted_widget_order_drift', + { + nodeId: event.nodeId, + liveNames, + docValueCount: doc.valueCount, + docDeclaredNames: doc.declaredNames, + docPromotedNames: doc.promotedNames + }, + reportedDrift + ) + ) if (operation) schedule({ kind: 'op', operation }) } diff --git a/src/workbench/extensions/agent/crdt/linkMintOutboundPositionCorruption.test.ts b/src/workbench/extensions/agent/crdt/linkMintOutboundPositionCorruption.test.ts index a01c0d71329..411caee2127 100644 --- a/src/workbench/extensions/agent/crdt/linkMintOutboundPositionCorruption.test.ts +++ b/src/workbench/extensions/agent/crdt/linkMintOutboundPositionCorruption.test.ts @@ -29,6 +29,7 @@ import { emitGraphIntent } from '@/lib/litegraph/src/graphIntents' import type { LGraph } from '@/lib/litegraph/src/LGraph' import { LGraphNode } from '@/lib/litegraph/src/litegraph' import type { LLink } from '@/lib/litegraph/src/LLink' +import { readDocPromotedWidgets } from './agentSubgraphDefinitions' import { readDocSlotNames } from './liveGraphApplier' import { toRootGraphId } from '@/types/graphScopeId' import { toNodeId } from '@/types/nodeId' @@ -188,7 +189,10 @@ describe('agent CRDT outbound leg: link mint by live position vs. doc order', () enqueue: (operations) => minted.push(...operations), getGraph: () => rootGraph, boundRootGraphId: () => ROOT_GRAPH_ID, - docInputNames: (nodeId) => readDocSlotNames(doc, String(nodeId), 'inputs') + docInputNames: (nodeId) => + readDocSlotNames(doc, String(nodeId), 'inputs'), + docPromotedWidgets: (nodeId) => + readDocPromotedWidgets(doc, String(nodeId)) }) emitGraphIntent({ diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.humanAddTabSwitch.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.humanAddTabSwitch.test.ts index ee9ca4df77a..5c63d9304ba 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.humanAddTabSwitch.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.humanAddTabSwitch.test.ts @@ -131,7 +131,8 @@ describe('a human-added node across a tab switch', () => { enqueue: followerApi.enqueueHumanOperations, getGraph: () => graph, boundRootGraphId: () => null, - docInputNames: followerApi.docInputNames + docInputNames: followerApi.docInputNames, + docPromotedWidgets: followerApi.docPromotedWidgets }) const cleanup = () => { minter.detach() diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 09e58bfcb77..0cb8e414093 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -36,6 +36,7 @@ import type { GraphOperation } from './graphOperations' import type { ClassifiedDocUpdate } from './layoutFollowerBridge' import { LayoutFollowerBridge } from './layoutFollowerBridge' import type { LiveGraphApplierDeps } from './liveGraphApplier' +import { readDocPromotedWidgets } from './agentSubgraphDefinitions' import { readDocSlotNames } from './liveGraphApplier' import { createOpCoalescer } from './opCoalescer' import { createOpSender } from './opSender' @@ -341,7 +342,9 @@ export function useAgentCrdtFollower( enqueueHumanOperations: (operations: GraphOperation[]) => follower.value?.enqueueHumanOperations(operations), docInputNames: (nodeId: NodeId) => - follower.value?.docInputNames(nodeId) ?? null + follower.value?.docInputNames(nodeId) ?? null, + docPromotedWidgets: (nodeId: NodeId) => + follower.value?.docPromotedWidgets(nodeId) ?? null } } @@ -893,6 +896,8 @@ function startAgentCrdtFollower( coalescer.enqueue(operations) }, docInputNames: (nodeId: NodeId) => - readDocSlotNames(bridge.follower.doc, String(nodeId), 'inputs') + readDocSlotNames(bridge.follower.doc, String(nodeId), 'inputs'), + docPromotedWidgets: (nodeId: NodeId) => + readDocPromotedWidgets(bridge.follower.doc, String(nodeId)) } }