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
35 changes: 34 additions & 1 deletion browser_tests/fixtures/agentConversationFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`
Expand Down Expand Up @@ -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<void>; release: () => void }> {
let release!: () => void
let reached!: () => void
const held = new Promise<void>((resolve) => (release = resolve))
const requested = new Promise<void>((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
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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 {
Expand Down
99 changes: 99 additions & 0 deletions browser_tests/fixtures/data/agent/backgroundTabEdits.ts
Original file line number Diff line number Diff line change
@@ -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<typeof AgentWorkflowChatsMock>[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<ChatWorkflow, 'name'>
): 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]
146 changes: 146 additions & 0 deletions browser_tests/tests/agent/agentBackgroundTabEditsLand.spec.ts
Original file line number Diff line number Diff line change
@@ -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
)
})
})
}
)
27 changes: 23 additions & 4 deletions src/workbench/extensions/agent/AgentPanelRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
bindRestoredWorkflow(workflowId)
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
}

function warnWorkflowUnavailable(): void {
toast.warning(t('agent.targetNavigationUnavailable'), { duration: 5000 })
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1901,7 +1920,7 @@ const workflowChatFollow = useAgentWorkflowChatFollow({
trackThreadStarted: () =>
useTelemetry()?.trackAgentThreadStarted({ source: 'history_select' }),
restoreThreadWorkflow,
adoptWorkflowTarget,
adoptWorkflowTarget: adoptFollowedWorkflow,
adoptRestoredWorkflowTab
})

Expand Down
17 changes: 17 additions & 0 deletions src/workbench/extensions/agent/crdt/docOpMinter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ describe('attachDocOpMinter', () => {
let seededPromotedDocs: Set<ReturnType<typeof mint>>
let dynamicComboDocs: Set<ReturnType<typeof mint>>
let unboundLocalIntents: number
let unboundVisibleIntents: number

function registeredDynamicComboScenario() {
const scenario = dynamicComboScenario(graph)
Expand All @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
Loading
Loading