diff --git a/browser_tests/tests/agent/agentFollowerLateEndpointLink.spec.ts b/browser_tests/tests/agent/agentFollowerLateEndpointLink.spec.ts new file mode 100644 index 00000000000..87df96fe6c9 --- /dev/null +++ b/browser_tests/tests/agent/agentFollowerLateEndpointLink.spec.ts @@ -0,0 +1,249 @@ +import { expect, mergeTests } from '@playwright/test' + +import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' +import type { ComfyNodeDef } from '@/schemas/nodeDefSchema' + +import { + BLANK_WORKFLOW, + agentTest, + bootAgentApp, + loadIntoBootWorkflow, + mockAgentTurnApi, + mockWorkflowPersistence +} from '@e2e/fixtures/agentPanelFixture' +import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' +import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' +import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers' +import { webSocketFixture } from '@e2e/fixtures/ws' + +const test = mergeTests(agentTest, webSocketFixture) + +const WORKFLOW_ID = 'c1d4e7fa-0000-4000-8000-000000000020' +const MESSAGE_ID = 'c1d4e7fa-0000-4000-8000-000000000021' +const THREAD_ID = 'c1d4e7fa-0000-4000-8000-000000000022' + +const SOURCE_TYPE = 'AgentLateEndpointSource' +const SINK_TYPE = 'AgentLateEndpointSink' + +const SINK_NODE_ID = 2 +const BYSTANDER_NODE_ID = 4 +const LATE_SOURCE_NODE_ID = 3 +const LINK_ID = 7 + +const nodeDefs: Record = { + [SOURCE_TYPE]: { + name: SOURCE_TYPE, + display_name: 'Agent Late Endpoint Source', + description: '', + category: 'test', + python_module: 'test', + output_node: false, + output: ['IMAGE'], + output_is_list: [false], + output_name: ['IMAGE'], + input: {}, + input_order: {} + }, + [SINK_TYPE]: { + name: SINK_TYPE, + display_name: 'Agent Late Endpoint Sink', + description: '', + category: 'test', + python_module: 'test', + output_node: true, + output: [], + output_is_list: [], + output_name: [], + input: { required: { image: ['IMAGE', {}] } }, + input_order: { required: ['image'] } + } +} + +const catalog: WidgetCatalog = { + types: { + [SOURCE_TYPE]: { widget_order: [] }, + [SINK_TYPE]: { widget_order: [] } + } +} + +function sinkNode(id: number, y: number) { + return { + id, + type: SINK_TYPE, + pos: [520, y] as [number, number], + size: [240, 60] as [number, number], + flags: {}, + order: 0, + mode: 0, + inputs: [{ name: 'image', type: 'IMAGE', link: null }], + outputs: [], + properties: {}, + widgets_values: [] + } +} + +const seed: WorkflowJSON = { + nodes: [sinkNode(SINK_NODE_ID, 120), sinkNode(BYSTANDER_NODE_ID, 320)], + links: [], + groups: [], + config: {}, + extra: {}, + version: 0.4 +} + +test.describe( + 'Agent follower on a link whose endpoint arrives in a later frame', + { tag: ['@cloud', '@agent', '@canvas', '@node'] }, + () => { + test.use({ connectWebSocketToServer: false }) + + test('connects the wire once the missing endpoint node materializes', async ({ + page, + getWebSocket + }) => { + test.setTimeout(90_000) + await page.setViewportSize({ width: 1920, height: 1280 }) + + await bootAgentApp(page, true, { + settings: { + 'Comfy.VueNodes.Enabled': true, + 'Comfy.Graph.CanvasInfo': false + }, + objectInfo: nodeDefs, + beforeNavigate: async (page) => { + await mockAgentTurnApi(page, { + message_id: MESSAGE_ID, + thread_id: THREAD_ID, + workflow_id: WORKFLOW_ID + }) + await mockWorkflowPersistence(page, WORKFLOW_ID) + } + }) + await loadIntoBootWorkflow(page, BLANK_WORKFLOW) + + const socket = await getWebSocket() + const outboundFrames: string[] = [] + socket.onMessage((message) => outboundFrames.push(String(message))) + + const vueNodes = new VueNodeHelpers(page) + const agentPanel = new AgentPanel(page) + + await test.step('bind the workflow so the follower subscribes', async () => { + await agentPanel.open() + await agentPanel.selectWorkflow() + await agentPanel.sendMessage('wire the source into the sink') + await expect + .poll(() => outboundFrames, { timeout: 15_000 }) + .toContainEqual(expect.stringContaining('doc_subscribe')) + }) + + const host = new HostDoc(WORKFLOW_ID, seed, catalog) + const sink = vueNodes.getNodeLocator(String(SINK_NODE_ID)) + + await test.step('frame A carries one unresolvable link alongside good nodes: the good nodes still arrive', async () => { + host.replaceLink([ + LINK_ID, + LATE_SOURCE_NODE_ID, + 0, + SINK_NODE_ID, + 0, + 'IMAGE' + ]) + for (const frame of host.initialSync()) + socket.send(JSON.stringify(frame)) + + await expect(sink).toBeVisible({ timeout: 30_000 }) + await expect( + vueNodes.getNodeLocator(String(BYSTANDER_NODE_ID)) + ).toBeVisible({ timeout: 30_000 }) + + await expect + .poll(() => page.evaluate(() => window.app!.graph.nodes.length)) + .toBe(2) + expect(await page.evaluate(() => window.app!.graph.links.size)).toBe(0) + }) + + await test.step('frame B materializes the missing endpoint for the first time', async () => { + socket.send( + JSON.stringify( + host.apply([ + { + op: 'add_node', + node_id: LATE_SOURCE_NODE_ID, + class_type: SOURCE_TYPE, + pos: [120, 120], + node: { + id: LATE_SOURCE_NODE_ID, + type: SOURCE_TYPE, + pos: [120, 120], + size: [240, 60], + mode: 0, + flags: {}, + order: 1, + inputs: [], + outputs: [{ name: 'IMAGE', type: 'IMAGE', links: [LINK_ID] }], + properties: {}, + widgets_values: [] + } + } + ]) + ) + ) + await expect( + vueNodes.getNodeLocator(String(LATE_SOURCE_NODE_ID)) + ).toBeVisible({ timeout: 30_000 }) + }) + + await test.step("the host's document says the two nodes are wired", () => { + expect(host.link(LINK_ID)).toEqual([ + LINK_ID, + LATE_SOURCE_NODE_ID, + 0, + SINK_NODE_ID, + 0, + 'IMAGE' + ]) + }) + + await test.step('and so does the canvas: the wire the user is supposed to see is there', async () => { + await expect + .poll( + () => + vueNodes.isSlotConnected( + vueNodes.getInputSlotRow(String(SINK_NODE_ID), 0) + ), + { timeout: 30_000 } + ) + .toBe(true) + + await expect + .poll(() => + page.evaluate(() => + [...window.app!.graph.links.values()].map((link) => ({ + origin: String(link.origin_id), + originSlot: link.origin_slot, + target: String(link.target_id), + targetSlot: link.target_slot, + type: link.type + })) + ) + ) + .toEqual([ + { + origin: String(LATE_SOURCE_NODE_ID), + originSlot: 0, + target: String(SINK_NODE_ID), + targetSlot: 0, + type: 'IMAGE' + } + ]) + }) + + await test.step('the follower never wrote the shared document (KEEP-ALIVE #6)', () => { + expect( + outboundFrames.filter((frame) => frame.includes('doc_ops')) + ).toEqual([]) + }) + }) + } +) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts index ec8de68d02d..4a451c64fc7 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts @@ -1768,6 +1768,28 @@ describe('LiveGraphApplier', () => { expect(reportError).not.toHaveBeenCalled() }) + it('connects a dangling link when its endpoint arrives in a later frame', () => { + const { graph, doc, applyCollected, applyEdit } = setup({ + nodes: [sourceNode(1), sinkNode(2)], + links: [] + }) + applyCollected() + + applyEdit(() => { + linksMap(doc).set('7', [7, 3, 0, 2, 0, 'IMAGE']) + }) + expect(graph.links.has(toLinkId(7))).toBe(false) + + applyEdit(() => { + nodesMap(doc).set('3', new Y.Map(Object.entries(sourceNode(3)))) + }) + + expect(graph.links.get(toLinkId(7))).toMatchObject({ + origin_id: toNodeId(3), + target_id: toNodeId(2) + }) + }) + it('removes a live link whose document entry now names a slot the node lacks', () => { const { graph, doc, applyCollected, applyEdit } = setup({ nodes: [sourceNode(1), sinkNode(2)], diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index daebfe4b2ce..89f0cd084e0 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -582,7 +582,7 @@ export class LiveGraphApplier { this.try(context, () => { const result = this.upsertNode(graph, doc, id, mode) if (result === 'created') created.push(toNodeId(id)) - if (result === 'recreated') { + if (result === 'created' || result === 'recreated') { for (const link of docLinksIncident(doc, id)) { this.connectLink(graph, doc, link) }