Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})

Expand Down Expand Up @@ -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
})
})

Expand Down
6 changes: 4 additions & 2 deletions src/workbench/extensions/agent/AgentPanelRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,8 @@ const isBoundWorkflowActive = computed(() => {
const {
status: crdtStatus,
debugSnapshot: crdtDebugSnapshot,
enqueueHumanOperations
enqueueHumanOperations,
docPromotedWidgets
} = useAgentCrdtFollower(
boundWorkflowId,
graphMutations,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ export const crdtTest = baseTest.extend<CrdtFixtures>({
layoutChanges: () => () => {},
localActorPrefix: 'user-',
getGraph: () => graph,
boundRootGraphId: () => graphScopeOf(graph).rootGraphId
boundRootGraphId: () => graphScopeOf(graph).rootGraphId,
docPromotedWidgets: () => null
})
cleanups.push(() => wiring.detach())
let sequence = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -11,6 +13,7 @@ import type {
} from '@/lib/litegraph/src/types/serialisation'

import {
readDocPromotedWidgets,
readSubgraphDefinitionIds,
readSubgraphDefinitions
} from './agentSubgraphDefinitions'
Expand Down Expand Up @@ -92,6 +95,25 @@ function nestedDefinitionChain(depth: number): Record<string, unknown> {
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([])
Expand Down
87 changes: 86 additions & 1 deletion src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<unknown>,
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) ?? []
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/workbench/extensions/agent/crdt/mintPortWiring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ describe('attachMintPortWiring', () => {
},
localActorPrefix: 'user-',
getGraph: () => graph,
boundRootGraphId: () => toRootGraphId(ROOT_ID)
boundRootGraphId: () => toRootGraphId(ROOT_ID),
docPromotedWidgets: () => null
})
})

Expand Down
13 changes: 12 additions & 1 deletion src/workbench/extensions/agent/crdt/mintPortWiring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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))
}
}
Loading
Loading