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
49 changes: 49 additions & 0 deletions browser_tests/fixtures/agentNonValueWidgetRig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ import {
mockWorkflowPersistence
} from '@e2e/fixtures/agentPanelFixture'
import { AgentPanel } from '@e2e/fixtures/components/AgentPanel'
import { nextFrame } from '@e2e/fixtures/utils/timing'
import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers'

/** Binary WS frame type 3 (`progress_text`): [u32 type][u32 idLen][id][text]. */
export function progressTextFrame(nodeId: string, text: string): Buffer {
const id = Buffer.from(nodeId, 'utf8')
const body = Buffer.from(text, 'utf8')
const frame = Buffer.alloc(8 + id.length + body.length)
frame.writeUInt32BE(3, 0)
frame.writeUInt32BE(id.length, 4)
id.copy(frame, 8)
body.copy(frame, 8 + id.length)
return frame
}

interface NonValueWidgetRigConfig {
catalog: WidgetCatalog
messageId: string
Expand Down Expand Up @@ -87,6 +100,42 @@ export class AgentNonValueWidgetRig {
return rig
}

/**
* One execution progress tick on a node, through the ephemeral text-preview
* widget's real setter.
*/
async streamProgressText(nodeId: number, text: string): Promise<void> {
this.hostSocket.sendExecutionBinary(progressTextFrame(String(nodeId), text))
await nextFrame(this.page)
}

/**
* Clears the live widget's own `serialize` flag to `undefined` while leaving
* the key present, as a widget built by object spread from a template that
* never set it does (`{ ...template, serialize: template.serialize }`). The
* store's registered flag is untouched, so this is the state in which the
* minter has to fall back to the store rather than read the live `undefined`
* as "serialize unless told otherwise".
*/
clearLiveSerializeFlag(nodeId: number, name: string): Promise<void> {
return this.page.evaluate(
({ nodeId, name }) => {
const node = window.app!.graph.nodes.find(
(candidate) => String(candidate.id) === nodeId
)
const widget = node?.widgets?.find(
(candidate) => candidate.name === name
)
if (!widget) throw new Error(`Expected ${name} on node ${nodeId}`)
widget.serialize = undefined
if (!('serialize' in widget)) {
throw new Error(`serialize stayed absent on ${name}`)
}
},
{ nodeId: String(nodeId), name }
)
}

widgetValue(nodeId: number, name: string): Promise<unknown> {
return this.page.evaluate(
({ nodeId, name }) =>
Expand Down
41 changes: 7 additions & 34 deletions browser_tests/tests/agent/agentProgressTextWidgetNotMinted.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { expect } from '@playwright/test'
import type { Page } from '@playwright/test'

import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player'
import type { ComfyNodeDef } from '@/schemas/nodeDefSchema'

import { AgentNonValueWidgetRig } from '@e2e/fixtures/agentNonValueWidgetRig'
import type { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket'
import {
AgentNonValueWidgetRig,
progressTextFrame
} from '@e2e/fixtures/agentNonValueWidgetRig'
import { agentTest as test } from '@e2e/fixtures/agentPanelFixture'
import { ToastHelper } from '@e2e/fixtures/helpers/ToastHelper'
import { nextFrame } from '@e2e/fixtures/utils/timing'

/**
* `$$node-text-preview` is a `serialize: false` widget written by binary
Expand Down Expand Up @@ -73,18 +73,6 @@ const seed: WorkflowJSON = {
version: 0.4
}

/** Binary WS frame type 3 (`progress_text`): [u32 type][u32 idLen][id][text]. */
function progressTextFrame(nodeId: string, text: string): Buffer {
const id = Buffer.from(nodeId, 'utf8')
const body = Buffer.from(text, 'utf8')
const frame = Buffer.alloc(8 + id.length + body.length)
frame.writeUInt32BE(3, 0)
frame.writeUInt32BE(id.length, 4)
id.copy(frame, 8)
body.copy(frame, 8 + id.length)
return frame
}

const rigConfig = {
catalog,
messageId: MESSAGE_ID,
Expand All @@ -97,16 +85,6 @@ const rigConfig = {
workflowId: WORKFLOW_ID
}

/** One execution progress tick, through the widget's real setter. */
async function streamProgress(
page: Page,
hostSocket: AgentFollowerHostSocket,
text: string
): Promise<void> {
hostSocket.sendExecutionBinary(progressTextFrame(String(NODE_ID), text))
await nextFrame(page)
}

test.describe(
'Agent CRDT: an ephemeral progress widget is never minted outbound',
{ tag: ['@cloud', '@agent', '@vue-nodes', '@widget'] },
Expand All @@ -119,9 +97,9 @@ test.describe(
const { hostSocket } = rig

await test.step('stream three progress ticks into the ephemeral widget', async () => {
await streamProgress(page, hostSocket, PROGRESS_TEXT)
await streamProgress(page, hostSocket, LATER_PROGRESS_TEXT)
await streamProgress(page, hostSocket, 'Status: Done')
await rig.streamProgressText(NODE_ID, PROGRESS_TEXT)
await rig.streamProgressText(NODE_ID, LATER_PROGRESS_TEXT)
await rig.streamProgressText(NODE_ID, 'Status: Done')

// The widget really was written, so this test cannot pass by never
// exercising the path at all.
Expand All @@ -144,11 +122,6 @@ test.describe(
await expect
.poll(() => hostSocket.humanOpOutcomes().map((o) => o.outcome))
.toEqual(['applied'])

await streamProgress(page, hostSocket, 'Status: Settled')
await expect
.poll(() => rig.widgetValue(NODE_ID, PREVIEW_WIDGET))
.toBe('Status: Settled')
})

await test.step('no rejection toast, hand edit still on screen', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import { expect } from '@playwright/test'

import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player'
import type { ComfyNodeDef } from '@/schemas/nodeDefSchema'

import { AgentNonValueWidgetRig } from '@e2e/fixtures/agentNonValueWidgetRig'
import { agentTest as test } from '@e2e/fixtures/agentPanelFixture'
import { ToastHelper } from '@e2e/fixtures/helpers/ToastHelper'

/**
* The sibling `agentProgressTextWidgetNotMinted` spec covers the ephemeral
* widget while its own `serialize: false` is intact, which both the old and the
* new precedence rule read correctly. This spec covers the one case they
* disagree on: a live widget whose `serialize` key is PRESENT and `undefined`.
*
* `'serialize' in widget` is true for a present-but-undefined key, so the old
* rule short-circuited to that `undefined`, skipped the store's registered
* `serialize: false`, and minted a `set_widget` for a widget the document does
* not carry. `widget?.serialize ?? stored?.serialize` treats absent and
* present-but-undefined alike, which is what `IBaseWidget.serialize?: boolean`
* already means.
*
* Carrier: frontend #20027.
*/

const NODE_TYPE = 'KSampler'
const NODE_ID = 711
const WORKFLOW_ID = 'e7a4c6d2-0000-4000-8000-000000000080'
const MESSAGE_ID = 'e7a4c6d2-0000-4000-8000-000000000081'
const THREAD_ID = 'e7a4c6d2-0000-4000-8000-000000000082'
const SOCKET_SID = 'e7a4c6d2-0000-4000-8000-000000000083'

const PREVIEW_WIDGET = '$$node-text-preview'
const SEED_VALUE = 777777
const EDITED_SEED_VALUE = 888888
const FIRST_PROGRESS_TEXT = 'Status: Running\nTime elapsed: 2s'
const LATER_PROGRESS_TEXT = 'Status: Running\nTime elapsed: 7s'
const STEPS_VALUE = 20

const nodeDef: ComfyNodeDef = {
name: NODE_TYPE,
display_name: 'KSampler',
description: '',
category: 'sampling',
python_module: 'nodes',
output_node: false,
input: {
required: {
seed: ['INT', { default: 0, min: 0, max: 2147483647 }],
steps: ['INT', { default: STEPS_VALUE, min: 1, max: 10000 }]
}
},
output: ['LATENT'],
output_is_list: [false],
output_name: ['LATENT']
}

/**
* The pinned catalog carries only the two value widgets, so an op naming
* `$$node-text-preview` is a widget the applier has no entry for.
*/
const catalog: WidgetCatalog = {
types: { [NODE_TYPE]: { widget_order: ['seed', 'steps'] } }
}

const seed: WorkflowJSON = {
nodes: [
{
id: NODE_ID,
type: NODE_TYPE,
pos: [0, 0],
size: [270, 140],
flags: {},
order: 0,
mode: 0,
inputs: [],
outputs: [{ name: 'LATENT', type: 'LATENT', links: [] }],
properties: {},
widgets_values: [SEED_VALUE, STEPS_VALUE]
}
],
links: [],
groups: [],
config: {},
extra: {},
version: 0.4
}

const rigConfig = {
catalog,
messageId: MESSAGE_ID,
nodeDefs: { [NODE_TYPE]: nodeDef },
samplerNodeId: NODE_ID,
seed,
socketSid: SOCKET_SID,
threadId: THREAD_ID,
visibleNodeId: NODE_ID,
workflowId: WORKFLOW_ID
}

test.describe(
'Agent CRDT: a widget whose live serialize flag is undefined is never minted outbound',
{ tag: ['@cloud', '@agent', '@vue-nodes', '@widget'] },
() => {
test('an ephemeral widget that leaves serialize undefined mints nothing and never tells the user an edit was rejected', async ({
page
}) => {
test.setTimeout(60_000)
const rig = await AgentNonValueWidgetRig.boot(page, rigConfig)
const { host, hostSocket } = rig

await test.step('create the ephemeral widget with its serialize flag intact', async () => {
await rig.streamProgressText(NODE_ID, FIRST_PROGRESS_TEXT)
await expect
.poll(() => rig.widgetValue(NODE_ID, PREVIEW_WIDGET))
.toBe(FIRST_PROGRESS_TEXT)
})

await test.step('leave the live flag present but undefined', async () => {
// The store keeps the `serialize: false` it was registered with; only
// the live widget stops stating it.
await rig.clearLiveSerializeFlag(NODE_ID, PREVIEW_WIDGET)
})

await test.step('stream another tick through the widget', async () => {
await rig.streamProgressText(NODE_ID, LATER_PROGRESS_TEXT)

// The widget really was written, so this test cannot pass by never
// exercising the command site at all.
await expect
.poll(() => rig.widgetValue(NODE_ID, PREVIEW_WIDGET))
.toBe(LATER_PROGRESS_TEXT)
})

await test.step('commit a hand edit as the settlement barrier', async () => {
// `opSender` sends batches in order and the host judges them in order,
// so once this edit's verdict is in, anything the ephemeral write
// minted ahead of it has already been judged and is already in
// `humanOpOutcomes()`. That turns "nothing was minted" from a negative
// assertion that passes while ops are still in flight into a positive
// one on the whole collection.
await rig.editSeed(EDITED_SEED_VALUE)
})

await test.step('only the hand edit reached the applier', async () => {
await expect
.poll(() => hostSocket.humanOpOutcomes().map((o) => o.outcome))
.toEqual(['applied'])
})

await test.step('the document carries the hand edit and no preview text', async () => {
expect(
host.projection().nodes.find((node) => node.id === NODE_ID)
?.widgets_values
).toEqual([EDITED_SEED_VALUE, STEPS_VALUE])
})

await test.step('no rejection toast, hand edit still on screen', async () => {
const rejectionToast = new ToastHelper(page).toastErrors.filter({
hasText: 'Widget edit was rejected and was not saved'
})
await expect(rejectionToast).toHaveCount(0)
await expect(rig.seedField()).toHaveValue(String(EDITED_SEED_VALUE))
})
})
}
)
28 changes: 28 additions & 0 deletions src/workbench/extensions/agent/crdt/docOpMinter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,34 @@ describe('attachDocOpMinter', () => {
expect(minted).toEqual([])
})

it('uses the store type when the live widget leaves it undefined', async () => {
const { source } = seedGraph(graph)
const button = source.widgets![1]
const stored = useWidgetValueStore().getWidget(
widgetId(graph.id, source.id, button.name)
)
assert.exists(stored)
expect(stored.type).toBe('button')
// `type` is required on `IBaseWidget`, so only a non-conforming runtime
// object reaches this state — which is the point: the guard has to answer
// for one anyway. Assigned through `Object.assign` rather than a
// suppression, since the subject here is runtime fallback and not a
// compiler diagnostic.
Object.assign(button, { type: undefined })

emitGraphIntent({
type: 'set_widget',
graphId: graph.id,
nodeId: source.id,
name: 'upload',
value: 'clicked',
previous: 'button-slot'
})
await afterFlush()

expect(minted).toEqual([])
})

it('uses the same store fallback when filtering an add-node snapshot', () => {
const { source } = seedGraph(graph)
const widget = source.widgets![0]
Expand Down
8 changes: 6 additions & 2 deletions src/workbench/extensions/agent/crdt/docOpMinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,12 @@ function isValueWidget(
stored?: WidgetState
): boolean {
if (!widget && !stored) return false
const type = widget ? widget.type : stored?.type
const serialize = widget?.serialize ?? stored?.serialize
// A destructuring default fires on absent AND present-but-undefined alike,
// which is what `IBaseWidget`'s own optional fields mean: the live widget
// did not state this, so the registration the intent was keyed by answers.
// One construct for both fields, so the two halves of the guard cannot
// drift apart again.
const { type = stored?.type, serialize = stored?.serialize } = widget ?? {}
return type !== 'button' && serialize !== false
}

Expand Down
Loading