Skip to content
Closed
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
@@ -0,0 +1,229 @@
import { expect } from '@playwright/test'

import type { ComfyPage } from '@e2e/fixtures/ComfyPage'
import { comfyPageFixture as test } from '@e2e/fixtures/ComfyPage'
import type { NodeReference } from '@e2e/fixtures/utils/litegraphUtils'
import { toNodeId } from '@/types/nodeId'

// Reported (Jo Zhang / Christian Byrne): when the in-app AI agent (or a
// manual drag) wires a new node into a subgraph's promoted "prompt" widget,
// the subgraph host's *other* promoted widgets (width/height/seed) render
// duplicated above the prompt box. A sibling symptom (PM-1253/PM-1254): the
// rewired widget itself disappears, and redo misplaces the wire. Undo always
// restores a correct single-widget display, because it fully reloads the
// node from a snapshot.
//
// Linear: PM-1328 (this bug), PM-1253 (parent), PM-1254 (sibling: int node ->
// width).
//
// Two candidate trigger paths are covered as separate scenarios, since they
// go through different code:
// - Interior rewire: rebinding the link feeding the interior node *behind*
// the promoted widget, from inside the subgraph
// (SubgraphInputNode._disconnectNodeInput -> SubgraphNode's
// 'input-disconnected' handler, src/lib/litegraph/src/subgraph/
// SubgraphNode.ts:325-354 / SubgraphInputNode.ts:173-234).
// - External boundary wire: dragging a brand-new node directly onto the
// SubgraphNode's own exposed widget socket from the parent graph -- the
// literal action in the bug report ("wire a text node into the
// subgraph's prompt widget"). This does not go through
// SubgraphNode._setWidget/ensureWidgetRemoved at all (SubgraphNode has no
// onConnectInput/onConnectionsChange override), so it is a genuinely
// different code path from the interior-rewire case above.
//
// Undo-test note: a Vue slot drag-connect records its own undo snapshot on
// drop (useSlotLinkInteraction.ts captureCanvasState, #18117), so undo right
// after the wire action deterministically restores the pre-wire state.

const KSAMPLER_ID = '3'
const EMPTY_LATENT_ID = '5'
const POSITIVE_CLIP_ID = '6'

const PROMOTED_WIDGET_NAMES = ['text', 'seed', 'width', 'height'] as const

/** Builds a subgraph with 4 promoted widgets from 3 different node types:
* 'text' (CLIPTextEncode, the promoted "prompt"), 'seed' (KSampler), and
* 'width'/'height' (EmptyLatentImage) -- the exact widget mix from the bug
* report. */
async function buildBaselineSubgraph(comfyPage: ComfyPage): Promise<string> {
await comfyPage.workflow.loadWorkflow('default')

await comfyPage.page.keyboard.down('Control')
try {
for (const id of [KSAMPLER_ID, EMPTY_LATENT_ID, POSITIVE_CLIP_ID]) {
const node = await comfyPage.nodeOps.getNodeRefById(id)
await node.click('title')
}
} finally {
await comfyPage.page.keyboard.up('Control')
await comfyPage.nextFrame()
}

const subgraphNodeId = await comfyPage.subgraph.convertSelectionToSubgraph()

// 'seed' (name match) and every CLIPTextEncode widget (node-type match)
// are auto-promoted as "recommended" on conversion; width/height are not,
// so promote them explicitly to match the real-world reported workflow.
await comfyPage.vueNodes.enterSubgraph(subgraphNodeId)
const emptyLatent = comfyPage.vueNodes.getNodeLocator(EMPTY_LATENT_ID)
await comfyPage.subgraph.promoteWidget(emptyLatent, 'width')
await comfyPage.subgraph.promoteWidget(emptyLatent, 'height')
await comfyPage.subgraph.exitViaBreadcrumb()

await expectSingleRenderedWidgetOfEach(comfyPage, subgraphNodeId)
return subgraphNodeId
}

function widgetLocators(comfyPage: ComfyPage, subgraphNodeId: string) {
const node = comfyPage.vueNodes.getNodeLocator(subgraphNodeId)
return PROMOTED_WIDGET_NAMES.map(
(name) => [name, node.getByLabel(name, { exact: true })] as const
)
}

/** The bug's core, verifiable symptom: after any rewire behind one promoted
* widget, every promoted widget -- including the ones nothing touched --
* must still render exactly once. */
async function expectSingleRenderedWidgetOfEach(
comfyPage: ComfyPage,
subgraphNodeId: string
) {
for (const [name, locator] of widgetLocators(comfyPage, subgraphNodeId)) {
await expect(
locator,
`"${name}" widget should render exactly once`
).toHaveCount(1)
}
}

test.describe(
'Subgraph promoted widget duplication on rewire (PM-1328 / PM-1253 / PM-1254)',
{ tag: ['@subgraph', '@widget', '@vue-nodes'] },
() => {
test.use({ initialSettings: { 'Comfy.Canvas.SelectionToolbox': true } })

test.describe('Interior rewire (rebinding the link behind the promoted widget)', () => {
test('keeps sibling promoted widgets rendered exactly once on the subgraph host', async ({
comfyPage
}) => {
const subgraphNodeId = await buildBaselineSubgraph(comfyPage)

await comfyPage.vueNodes.enterSubgraph(subgraphNodeId)
const [interiorClip] = await comfyPage.nodeOps.getNodeRefsByType(
'CLIPTextEncode',
true
)
await comfyPage.subgraph.rebindPromotedInput(interiorClip, 'text')
await comfyPage.subgraph.exitViaBreadcrumb()

await expectSingleRenderedWidgetOfEach(comfyPage, subgraphNodeId)
})

test('undo restores exactly one of each promoted widget', async ({
comfyPage
}) => {
const subgraphNodeId = await buildBaselineSubgraph(comfyPage)

await comfyPage.vueNodes.enterSubgraph(subgraphNodeId)
const [interiorClip] = await comfyPage.nodeOps.getNodeRefsByType(
'CLIPTextEncode',
true
)
await comfyPage.subgraph.rebindPromotedInput(interiorClip, 'text')
await comfyPage.subgraph.exitViaBreadcrumb()

await comfyPage.keyboard.undo()
await comfyPage.nextFrame()

// Undo fully reloads the node from a snapshot (ChangeTracker ->
// app.loadGraphData -> SubgraphNode.configure), so this must pass
// even though the rewire itself (above) does not.
await expectSingleRenderedWidgetOfEach(comfyPage, subgraphNodeId)
})
})

test.describe("External boundary wire (new node onto the host node's promoted widget socket)", () => {
test('keeps sibling promoted widgets rendered exactly once on the subgraph host', async ({
comfyPage
}) => {
const subgraphNodeId = await buildBaselineSubgraph(comfyPage)

const subgraphNodeRef: NodeReference =
await comfyPage.nodeOps.getNodeRefById(subgraphNodeId)
const textWidgetIndex = (await subgraphNodeRef.getWidgetByName('text'))
.index

const source: NodeReference = await comfyPage.nodeOps.addNode(
'PrimitiveNode',
{},
{ x: 100, y: 800 }
)
await comfyPage.nextFrame()

// The literal user/agent action from the bug report: drag a new
// node's output directly onto the SubgraphNode's own exposed
// "prompt" widget socket, from the parent graph -- not from inside
// the subgraph.
await source.connectWidget(0, subgraphNodeRef, textWidgetIndex)
await comfyPage.nextFrame()

await expectSingleRenderedWidgetOfEach(comfyPage, subgraphNodeId)
})

test('undo keeps rendered promoted widgets consistent with the host model', async ({
comfyPage
}) => {
const subgraphNodeId = await buildBaselineSubgraph(comfyPage)

const subgraphNodeRef: NodeReference =
await comfyPage.nodeOps.getNodeRefById(subgraphNodeId)
const textWidgetIndex = (await subgraphNodeRef.getWidgetByName('text'))
.index

const source: NodeReference = await comfyPage.nodeOps.addNode(
'PrimitiveNode',
{},
{ x: 100, y: 800 }
)
await comfyPage.nextFrame()
await source.connectWidget(0, subgraphNodeRef, textWidgetIndex)
await comfyPage.nextFrame()

await comfyPage.keyboard.undo()
await comfyPage.nextFrame()

// Which snapshot undo targets depends on change-tracker capture
// timing this test does not control (captures fire on window
// mouseup, before the click handlers that apply promotions), so
// assert render/model consistency instead of a fixed widget set:
// whatever state undo restored, the host's model must hold each
// promoted widget at most once and the grid must render exactly
// one row per model widget. Both reported symptoms -- duplicated
// rows and a disappeared widget -- violate this invariant.
const modelWidgetNames = await comfyPage.page.evaluate((id) => {
const node = window.app!.canvas.graph!.getNodeById(id)
return node
? (node.widgets?.map((widget) => widget.name) ?? [])
: null
}, toNodeId(subgraphNodeId))

expect(modelWidgetNames, 'host node should survive undo').not.toBeNull()
expect(
new Set(modelWidgetNames).size,
'model should hold no duplicate widgets'
).toBe(modelWidgetNames!.length)

for (const [name, locator] of widgetLocators(
comfyPage,
subgraphNodeId
)) {
const expected = modelWidgetNames!.includes(name) ? 1 : 0
await expect(
locator,
`"${name}" should render ${expected === 1 ? 'exactly once' : 'not at all'}`
).toHaveCount(expected)
}
})
})
}
)
1 change: 0 additions & 1 deletion browser_tests/tests/subgraph/subgraphPromotion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ test.describe(
await comfyPage.subgraph.exitViaBreadcrumb()
await comfyPage.vueNodes.waitForNodes()

test.fail()
await expect(promotedTextarea).toHaveValue(hostValue)
})
})
Expand Down
13 changes: 11 additions & 2 deletions src/composables/graph/useErrorClearingHooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ describe('onNodeRemoved clears missing asset errors by execution ID', () => {
expect(scopeSpy).toHaveBeenCalledTimes(survivingNodes.length)
})

it('removes host-keyed missing media when its sole promoted consumer is deleted', () => {
it('removes host-keyed missing media when its sole promoted consumer is deleted', async () => {
const {
rootGraph,
subgraph,
Expand All @@ -1172,11 +1172,15 @@ describe('onNodeRemoved clears missing asset errors by execution ID', () => {
expect(host.widgets).toHaveLength(1)
subgraph.remove(sourceNodes[0])

// Widget demotion is deferred by a microtask so a same-tick reconnect
// (a rewire) can cancel it; this genuine removal completes once it runs.
await Promise.resolve()

expect(host.widgets).toHaveLength(0)
expect(mediaStore.missingMediaCandidates).toBeNull()
})

it('keeps promoted missing media until the last fanout consumer is deleted', () => {
it('keeps promoted missing media until the last fanout consumer is deleted', async () => {
const {
rootGraph,
subgraph,
Expand All @@ -1191,14 +1195,19 @@ describe('onNodeRemoved clears missing asset errors by execution ID', () => {
mediaStore.setMissingMedia([candidate])

subgraph.remove(sourceNodes[0])
await Promise.resolve()
expect(host.widgets).toHaveLength(1)
expect(mediaStore.missingMediaCandidates).toEqual([candidate])

subgraph.remove(sourceNodes[2])
await Promise.resolve()
expect(host.widgets).toHaveLength(1)
expect(mediaStore.missingMediaCandidates).toEqual([candidate])

subgraph.remove(sourceNodes[1])
// Widget demotion is deferred by a microtask so a same-tick reconnect
// (a rewire) can cancel it; this genuine removal completes once it runs.
await Promise.resolve()
expect(host.widgets).toHaveLength(0)
expect(mediaStore.missingMediaCandidates).toBeNull()
})
Expand Down
6 changes: 5 additions & 1 deletion src/core/graph/subgraph/promotionUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ describe('demoteWidget — axiomatic projection retraction', () => {
return { host, interiorNode, interiorWidget }
}

it('drops projection but keeps slot and external link when host slot is externally connected', () => {
it('drops projection but keeps slot and external link when host slot is externally connected', async () => {
const { host, interiorNode, interiorWidget } = setupPromotedWidget()
const hostInput = host.inputs[0]
const source = new LGraphNode('External Source')
Expand All @@ -819,6 +819,10 @@ describe('demoteWidget — axiomatic projection retraction', () => {

demoteWidget(interiorNode, interiorWidget, [host])

// Demotion is deferred by a microtask so a same-tick reconnect (a
// rewire) can cancel it; this genuine demote completes once it runs.
await Promise.resolve()

expect(host.subgraph.inputs).toHaveLength(1)
expect(host.inputs[0]?.link).toBe(externalLink.id)
expect(host.inputs[0]?._widget).toBeUndefined()
Expand Down
7 changes: 7 additions & 0 deletions src/lib/litegraph/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ export interface INodeInputSlot extends INodeSlot {
* Internal use only; API is not finalised and may change at any time.
*/
_widget?: IBaseWidget

/**
* Internal use only. Set while a promoted widget's demotion is deferred by
* a microtask, so a same-tick reconnect (a rewire) can cancel it instead
* of the widget being torn down and immediately rebuilt.
*/
_pendingDemotionToken?: symbol
}

export interface IWidgetInputSlot extends INodeInputSlot {
Expand Down
41 changes: 29 additions & 12 deletions src/lib/litegraph/src/subgraph/SubgraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph {
'input-connected',
(e) => {
input.shape = this.getSlotShape(subgraphInput, e.detail.input)
// A reconnect landing before the deferred demotion below runs is a
// rewire, not a real disconnect: cancel the pending demotion so the
// widget is never torn down.
input._pendingDemotionToken = undefined
if (!e.detail.widget || !e.detail.node) return

this._setWidget(
Expand All @@ -336,20 +340,33 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph {
return
}

if (input._widget) this.ensureWidgetRemoved(input._widget)
if (input.widgetId) {
const id = input.widgetId
queueMicrotask(() => {
if (this.inputs.some((input) => input.widgetId === id)) return
// Defer the actual demotion by a microtask instead of clearing
// input.widgetId/.widget/._widget here: a rewire (this disconnect
// immediately followed by a reconnect of the same input, e.g.
// dragging a new source onto an already-promoted widget) would
// otherwise drop the widget from `this.widgets` for one tick, which
// is exactly the window the Vue widget grid can render from. The
// 'input-connected' listener above cancels this token on a
// same-tick reconnect.
const widget = input._widget
const id = input.widgetId
const token = Symbol()
input._pendingDemotionToken = token
queueMicrotask(() => {
if (input._pendingDemotionToken !== token) return
input._pendingDemotionToken = undefined

if (widget) this.ensureWidgetRemoved(widget)
if (id && !this.inputs.some((i) => i.widgetId === id)) {
useWidgetValueStore().deleteWidget(id)
})
}
}

input.pos = undefined
input.widget = undefined
input.widgetId = undefined
input._widget = undefined
this.invalidatePromotedViews()
input.pos = undefined
input.widget = undefined
input.widgetId = undefined
input._widget = undefined
this.invalidatePromotedViews()
})
},
{ signal }
)
Expand Down
Loading
Loading