diff --git a/browser_tests/fixtures/agentConversationFixture.ts b/browser_tests/fixtures/agentConversationFixture.ts index f83a571ac36..a1beb1fdc99 100644 --- a/browser_tests/fixtures/agentConversationFixture.ts +++ b/browser_tests/fixtures/agentConversationFixture.ts @@ -73,7 +73,7 @@ import { mockSavedWorkflowPersistence } from '@e2e/fixtures/utils/savedWorkflowP import { loadSeedIntoActiveTab } from '@e2e/fixtures/utils/seedActiveTab' import { nextFrame } from '@e2e/fixtures/utils/timing' -const THREAD_ID = 'e9a2f3d1-7c44-4b2e-9a01-5f6d8c7b3a10' +export const THREAD_ID = 'e9a2f3d1-7c44-4b2e-9a01-5f6d8c7b3a10' // One synthetic message id per turn; the recorded ids never reach the page. const turnId = (turn: number): string => `0c5b1e77-2d4a-4f9e-8b63-1a2c3d4e5${turn.toString(16).padStart(3, '0')}` @@ -936,6 +936,39 @@ export class AgentConversationHarness { ]) } + applyOpsOnHostUnsent(operations: RecordedGraphOperation[]): void { + this.host.apply(operations) + for (const id of Object.keys(this.host.graph().nodes)) this.seenIds.add(id) + } + + /** Holds `threadId`'s transcript reads until `release` is called. */ + async holdTranscript( + threadId: string + ): Promise<{ requested: Promise; release: () => void }> { + let release!: () => void + let reached!: () => void + const held = new Promise((resolve) => (release = resolve)) + const requested = new Promise((resolve) => (reached = resolve)) + await this.page.route( + `**/api/agent/threads/${threadId}/messages`, + async (route) => { + if (route.request().method() !== 'GET') return route.fallback() + reached() + await held + return route.fallback() + } + ) + return { requested, release } + } + + /** Serves a second workflow's document beside the recorded one. */ + serveWorkflow(workflowId: string, seed: WorkflowJSON): void { + this.hostSocket.addWorkflow( + workflowId, + new HostDoc(workflowId, seed, this.conversation.workflow.catalog) + ) + } + // Rises once per follower subscribe; a tab return re-subscribes and the // host answers with the catch-up frame this counter has just sent. subscribeCount(workflowId?: string): number { diff --git a/browser_tests/fixtures/data/agent/backgroundTabEdits.ts b/browser_tests/fixtures/data/agent/backgroundTabEdits.ts new file mode 100644 index 00000000000..c8c9c3e0fb2 --- /dev/null +++ b/browser_tests/fixtures/data/agent/backgroundTabEdits.ts @@ -0,0 +1,99 @@ +import type { ComfyWorkflowJSON } from '@comfyorg/graph-model/workflowSchema' + +import type { RecordedGraphOperation } from '@e2e/fixtures/data/agent/agentConversation' +import { + agentThreadSummary, + agentUserTranscript +} from '@e2e/fixtures/data/agent/agentThreadList' +import type { AgentWorkflowChatsMock } from '@e2e/fixtures/helpers/agentWorkflowChats' + +type WorkflowChats = ConstructorParameters[0] + +interface ChatWorkflow { + id: string + name: string + threadId: string + request: string +} + +export const BACKGROUND_TAB_CASE = 'agent-rec-set-widget-existing' +export const FOREGROUND_NAME = 'Unsaved Workflow' +export const BACKGROUND: ChatWorkflow = { + id: '3c1e5b7a-9d2f-4e8b-a6c4-0f1e2d3c4b5a', + name: 'Other Study', + threadId: '7a2b4c6d-8e0f-4a1b-9c3d-5e7f9a1b3c5d', + request: 'Tidy up the other study' +} +export const BACKGROUND_NODES = [ + { id: 801, title: 'b1', x: 1400 }, + { id: 802, title: 'b2', x: 1700 }, + { id: 803, title: 'b3', x: 2000 } +] + +const EMPTY_GRAPH: ComfyWorkflowJSON = { + last_node_id: 0, + last_link_id: 0, + nodes: [], + links: [], + groups: [], + config: {}, + extra: {}, + version: 0.4 +} + +export function backgroundEdits(): RecordedGraphOperation[] { + const type = 'EmptyLatentImage' + return BACKGROUND_NODES.flatMap(({ id, title, x }) => [ + { + op: 'add_node', + node_id: id, + class_type: type, + pos: [x, 600], + node: { + id, + type, + pos: [x, 600], + size: [240, 134], + flags: {}, + order: 0, + mode: 0, + inputs: [], + outputs: [{ name: 'LATENT', type: 'LATENT', links: [] }], + properties: {}, + widgets_values: [512, 512, 1] + } + }, + { op: 'set_node_field', node_id: id, field: 'title', value: title } + ]) +} + +export function backgroundTabChats( + foreground: Omit +): WorkflowChats { + const chats = [ + { ...foreground, name: FOREGROUND_NAME, at: '2026-11-02T09:00:00Z' }, + { ...BACKGROUND, at: '2026-11-02T10:00:00Z' } + ] + return { + workflows: chats.map(({ id, name }) => ({ + id, + name, + content: EMPTY_GRAPH + })), + threads: chats.map(({ id, name, threadId, request, at }) => + agentThreadSummary(threadId, `${name} chat`, { + preview: request, + workflow_id: id, + last_message_at: at + }) + ), + transcripts: Object.fromEntries( + chats.map(({ id, threadId, request }) => [ + threadId, + agentUserTranscript(threadId, id, request) + ]) + ) + } +} + +export const USER_NOTE_POSITION: [number, number] = [400, 400] diff --git a/browser_tests/tests/agent/agentBackgroundTabEditsLand.spec.ts b/browser_tests/tests/agent/agentBackgroundTabEditsLand.spec.ts new file mode 100644 index 00000000000..2084a0d9028 --- /dev/null +++ b/browser_tests/tests/agent/agentBackgroundTabEditsLand.spec.ts @@ -0,0 +1,146 @@ +import { expect } from '@playwright/test' + +import { + agentConversationTest as test, + THREAD_ID +} from '@e2e/fixtures/agentConversationFixture' +import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' +import { + BACKGROUND, + BACKGROUND_NODES, + BACKGROUND_TAB_CASE, + backgroundEdits, + backgroundTabChats, + FOREGROUND_NAME, + USER_NOTE_POSITION +} from '@e2e/fixtures/data/agent/backgroundTabEdits' +import { AgentWorkflowChatsMock } from '@e2e/fixtures/helpers/agentWorkflowChats' + +test.describe( + 'Agent edits to a background workflow tab', + { tag: ['@cloud', '@agent', '@vue-nodes'] }, + () => { + test.use({ conversationCase: BACKGROUND_TAB_CASE }) + + test.beforeEach(async ({ agentConversation, page }) => { + test.setTimeout(120_000) + const panel = new AgentPanel(page) + const topbar = agentConversation.topbar + const aRequest = agentConversation.conversation.turns[0].request.content + + await test.step('agent edits workflow A', () => + agentConversation.runTurns()) + + await test.step('workflow B has a saved chat of its own', async () => { + agentConversation.serveWorkflow(BACKGROUND.id, { + ...agentConversation.conversation.workflow.seed, + nodes: [], + links: [] + }) + await new AgentWorkflowChatsMock( + backgroundTabChats({ + id: agentConversation.conversation.workflow.id, + threadId: THREAD_ID, + request: aRequest + }) + ).route(page) + }) + + await test.step('user opens workflow B and its chat', async () => { + await panel.historyButton.click() + await panel.root + .getByRole('button', { name: `${BACKGROUND.name} chat`, exact: true }) + .click() + await expect(panel.userMessageBubbles).toHaveText([BACKGROUND.request]) + await expect( + topbar.getWorkflowTab(BACKGROUND.name).and(topbar.getActiveTab()) + ).toBeVisible() + }) + + await test.step('user returns to A by its tab', async () => { + await topbar.getWorkflowTab(FOREGROUND_NAME).click() + await expect(panel.userMessageBubbles).toHaveText([aRequest]) + }) + }) + + test('are on the canvas when the user returns to that tab', async ({ + agentConversation, + page + }) => { + const panel = new AgentPanel(page) + const topbar = agentConversation.topbar + const aRequest = agentConversation.conversation.turns[0].request.content + + await test.step('user switches to B by its tab', async () => { + await topbar.getWorkflowTab(BACKGROUND.name).click() + await expect(panel.userMessageBubbles).toHaveText([BACKGROUND.request]) + await expect( + topbar.getWorkflowTab(BACKGROUND.name).and(topbar.getActiveTab()) + ).toBeVisible() + }) + + await test.step('agent adds three titled nodes to A while B is shown', () => + agentConversation.applyOpsOnHostUnsent(backgroundEdits())) + + await test.step('returning to A shows every background edit', async () => { + await topbar.getWorkflowTab(FOREGROUND_NAME).click() + await expect( + topbar.getWorkflowTab(FOREGROUND_NAME).and(topbar.getActiveTab()) + ).toBeVisible() + await expect(panel.userMessageBubbles).toHaveText([aRequest]) + for (const { id, title } of BACKGROUND_NODES) + await expect( + agentConversation.vueNodes + .getNodeLocator(String(id)) + .getByTestId('node-title') + ).toHaveText(title) + }) + }) + + test('do not rebind a tab the user edited while its chat was loading', async ({ + agentConversation, + page + }) => { + const panel = new AgentPanel(page) + const topbar = agentConversation.topbar + const subscribesBefore = agentConversation.subscribeCount(BACKGROUND.id) + const transcript = await agentConversation.holdTranscript( + BACKGROUND.threadId + ) + + const noteId = + await test.step('user edits B before its chat loads', async () => { + await topbar.getWorkflowTab(BACKGROUND.name).click() + await transcript.requested + const id = await agentConversation.addNoteThroughSearchBox({ + x: USER_NOTE_POSITION[0], + y: USER_NOTE_POSITION[1] + }) + await expect( + agentConversation.vueNodes.getNodeLocator(id) + ).toBeVisible() + return id + }) + + await test.step('B keeps the edit once its chat loads', async () => { + transcript.release() + await expect(panel.userMessageBubbles).toHaveText([BACKGROUND.request]) + await expect( + agentConversation.vueNodes.getNodeLocator(noteId) + ).toBeVisible() + }) + + await test.step('the next subscribe, to A, finds B never re-subscribed', async () => { + const aId = agentConversation.conversation.workflow.id + const aSubscribes = agentConversation.subscribeCount(aId) + await topbar.getWorkflowTab(FOREGROUND_NAME).click() + await expect + .poll(() => agentConversation.subscribeCount(aId)) + .toBeGreaterThan(aSubscribes) + expect(agentConversation.subscribeCount(BACKGROUND.id)).toBe( + subscribesBefore + ) + }) + }) + } +) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 04a0c1128c6..9ca9ad86f48 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -1026,12 +1026,23 @@ async function onWorkflowRestoredAndBound( isCurrent ) if (committedWorkflowId === null || !isCurrent()) return ready - if (hasUnsyncedHumanOps(committedWorkflowId)) return ready - if (boundWorkflowId.value !== committedWorkflowId) - bindWorkflow(committedWorkflowId) + bindRestoredWorkflow(committedWorkflowId) return ready } +function bindRestoredWorkflow(workflowId: string): void { + if (hasUnsyncedHumanOps(workflowId)) return + if (boundWorkflowId.value !== workflowId) bindWorkflow(workflowId) +} + +function adoptFollowedWorkflow( + workflow: ComfyWorkflow, + workflowId: string +): void { + adoptWorkflowTarget(workflow, workflowId) + bindRestoredWorkflow(workflowId) +} + function warnWorkflowUnavailable(): void { toast.warning(t('agent.targetNavigationUnavailable'), { duration: 5000 }) } @@ -1256,10 +1267,18 @@ function boundRootGraphId(): RootGraphId | null { function followerCanDeliver(): boolean { return isFollowerActive.value && subscriptionSent() } +function noteUnboundVisibleIntent(): void { + const visible = workflowStore.activeWorkflow + const workflowId = visible + ? bindingStore.workflowIdFor(visible.path) + : undefined + if (workflowId !== undefined) noteUnboundLocalIntent(workflowId) +} const docOpMinter = attachDocOpMinter({ isEnabled: () => agentPanelStore.enabled, isDocBound: followerCanDeliver, onUnboundLocalIntent: noteUnboundLocalIntent, + onUnboundVisibleIntent: noteUnboundVisibleIntent, enqueue: enqueueHumanOperations, getGraph: () => (app.isGraphReady ? app.rootGraph : null), boundRootGraphId, @@ -1901,7 +1920,7 @@ const workflowChatFollow = useAgentWorkflowChatFollow({ trackThreadStarted: () => useTelemetry()?.trackAgentThreadStarted({ source: 'history_select' }), restoreThreadWorkflow, - adoptWorkflowTarget, + adoptWorkflowTarget: adoptFollowedWorkflow, adoptRestoredWorkflowTab }) diff --git a/src/workbench/extensions/agent/crdt/docOpMinter.test.ts b/src/workbench/extensions/agent/crdt/docOpMinter.test.ts index f325ea4404d..dd7aa3fb042 100644 --- a/src/workbench/extensions/agent/crdt/docOpMinter.test.ts +++ b/src/workbench/extensions/agent/crdt/docOpMinter.test.ts @@ -302,6 +302,7 @@ describe('attachDocOpMinter', () => { let seededPromotedDocs: Set> let dynamicComboDocs: Set> let unboundLocalIntents: number + let unboundVisibleIntents: number function registeredDynamicComboScenario() { const scenario = dynamicComboScenario(graph) @@ -326,10 +327,12 @@ describe('attachDocOpMinter', () => { seededPromotedDocs = new Set() dynamicComboDocs = new Set() unboundLocalIntents = 0 + unboundVisibleIntents = 0 minter = attachDocOpMinter({ isEnabled: () => enabled, isDocBound: () => bound, onUnboundLocalIntent: () => unboundLocalIntents++, + onUnboundVisibleIntent: () => unboundVisibleIntents++, enqueue: (operations) => minted.push(...operations), getGraph: () => (graphAvailable ? graph : null), boundRootGraphId: () => (unbound ? null : rootGraphId), @@ -587,6 +590,20 @@ describe('attachDocOpMinter', () => { expect(minted).toEqual([]) }) + it('marks an unbound edit to the visible workflow while another one is bound', async () => { + const { source } = seedGraph(graph) + bound = false + rootGraphId = toRootGraphId('other-root-graph') + source.widgets![0].value = 5 + unbound = true + source.widgets![0].value = 6 + await afterFlush() + + expect(unboundLocalIntents).toBe(0) + expect(unboundVisibleIntents).toBe(2) + expect(minted).toEqual([]) + }) + it('snapshots a pasted node after its same-tick configure, folding the widget and field writes into add_node', async () => { const node = new TestSource() graph.add(node) diff --git a/src/workbench/extensions/agent/crdt/docOpMinter.ts b/src/workbench/extensions/agent/crdt/docOpMinter.ts index a22c3260b3e..303ffa51b54 100644 --- a/src/workbench/extensions/agent/crdt/docOpMinter.ts +++ b/src/workbench/extensions/agent/crdt/docOpMinter.ts @@ -65,6 +65,8 @@ export interface DocOpMinterDeps { isDocBound(): boolean /** Records a local edit that cannot yet enter the semantic document. */ onUnboundLocalIntent?(): void + /** Records a local edit to the visible workflow while another one, or none, is bound. */ + onUnboundVisibleIntent?(): void /** Receives minted semantic operations (the sender's inbox). */ enqueue(operations: GraphOperation[]): void /** The live root graph, or null when no workflow is open. */ @@ -1193,17 +1195,23 @@ export function attachDocOpMinter(deps: DocOpMinterDeps): DocOpMinter { heldPromotedWrites.delete(heldWriteKey(event)) } + function intentRootGraphId( + event: GraphIntentEvent, + graph: LGraph | null + ): string | null { + if ('graph' in event) return event.graph.rootGraph.id + return graph !== null && reachableIntentGraph(graph, event.graphId) !== null + ? graph.id + : null + } + function noteUnboundLocalIntent(event: GraphIntentEvent): void { const graph = deps.getGraph() - const boundRootGraphId = deps.boundRootGraphId() - const eventRootGraphId = - 'graph' in event - ? event.graph.rootGraph.id - : graph !== null && reachableIntentGraph(graph, event.graphId) !== null - ? graph.id - : null - if (boundRootGraphId !== null && eventRootGraphId === boundRootGraphId) + const eventRootGraphId = intentRootGraphId(event, graph) + if (eventRootGraphId === null) return + if (eventRootGraphId === deps.boundRootGraphId()) deps.onUnboundLocalIntent?.() + else if (eventRootGraphId === graph?.id) deps.onUnboundVisibleIntent?.() } function onIntent(event: GraphIntentEvent): void { diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index e0bdadf5d67..e9defb40c07 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -376,7 +376,8 @@ export function useAgentCrdtFollower( }), enqueueHumanOperations: (operations: GraphOperation[]) => follower.value?.enqueueHumanOperations(operations), - noteUnboundLocalIntent: () => follower.value?.noteUnboundLocalIntent(), + noteUnboundLocalIntent: (target?: string) => + follower.value?.noteUnboundLocalIntent(target), retrySubscription: () => follower.value?.retrySubscription(), docInputNames: (nodeId: NodeId) => follower.value?.docInputNames(nodeId) ?? null, @@ -1292,8 +1293,7 @@ function startAgentCrdtFollower( projection.noteLocalWrites(operations) coalescer.enqueue(operations) }, - noteUnboundLocalIntent: () => { - const target = workflowId.value + noteUnboundLocalIntent: (target = workflowId.value) => { if (target === null) return workflowsWithUnsyncedOps.add(target) recordUnsynced()