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
12 changes: 10 additions & 2 deletions browser_tests/fixtures/agentConversationFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,7 @@ interface ConversationFixtures {
bootAssets: ListAssetsResponse
// Opt in only for cases that assert telemetry through hostTelemetryFixture.
telemetryEnabled: boolean
seedPassthrough: Omit<Partial<WorkflowJSON>, 'nodes' | 'links'>
comfyPage: ComfyPage
agentConversation: AgentConversationHarness
}
Expand All @@ -1433,6 +1434,7 @@ export const agentConversationTest = agentTest.extend<ConversationFixtures>({
humanOpsHost: ['hold', { option: true }],
bootAssets: [{ assets: [], total: 0, has_more: false }, { option: true }],
telemetryEnabled: [false, { option: true }],
seedPassthrough: [{}, { option: true }],
comfyPage: async ({ page, request }, use) => {
await use(new ComfyPage(page, request))
},
Expand All @@ -1454,7 +1456,8 @@ export const agentConversationTest = agentTest.extend<ConversationFixtures>({
extraNodeDefs,
humanOpsHost,
bootAssets,
telemetryEnabled
telemetryEnabled,
seedPassthrough
},
use,
testInfo
Expand All @@ -1466,10 +1469,15 @@ export const agentConversationTest = agentTest.extend<ConversationFixtures>({
throw new Error(
`a conversation replay is judged on Vue nodes; tag the test ${VUE_NODES_TAG}`
)
const conversation = loadAgentConversation(conversationCase)
conversation.workflow.seed = {
...conversation.workflow.seed,
...seedPassthrough
}
const harness = new AgentConversationHarness(
page,
comfyPage,
loadAgentConversation(conversationCase),
conversation,
replayTiming,
conversationCase,
extraNodeDefs,
Expand Down
52 changes: 52 additions & 0 deletions browser_tests/tests/agent/agentDeleteAndClearWorkflow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,58 @@ test.describe(
expect(await agentConversation.isRecoveryGraphUnchanged()).toBe(true)
})

test.describe('with groups on the canvas', () => {
test.use({
seedPassthrough: {
groups: [
{
id: 7,
title: 'Prompt group',
bounding: [0, 0, 420, 260],
color: '#3f789e',
font_size: 24,
flags: {}
},
{
id: 9,
title: 'Output group',
bounding: [520, 0, 320, 220],
color: '#b58b2a',
font_size: 24,
flags: {}
}
]
}
})

test('removes the groups along with the nodes', async ({
agentConversation,
page
}) => {
test.setTimeout(90_000)
const groupTitles = () =>
page.evaluate(() =>
window.app!.graph.groups.map((group) => group.title).sort()
)

await expect
.poll(groupTitles)
.toEqual(['Output group', 'Prompt group'])
await agentConversation.runTurns({
beforeGraphOps: async (ops) => {
if (isClear(ops))
expect(await groupTitles()).toEqual([
'Output group',
'Prompt group'
])
}
})

await expect(agentConversation.vueNodes.nodes).toHaveCount(0)
await expect.poll(groupTitles).toEqual([])
})
})

// PM-1500 / PM-1504: a clear that landed while the bound tab's graph id
// was unresolvable used to be rejected and left stale nodes on screen.
test('clears the canvas on the clear frame even when the workflow scope is dropped', async ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mint, nodesMap } from '@comfyorg/comfy-multi-player'
import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player'
import { beforeEach, describe, expect, it, onTestFinished } from 'vitest'
import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest'
import * as Y from 'yjs'

import { assert } from '@/base/assert'
Expand Down Expand Up @@ -367,4 +367,35 @@ describe('AgentCrdtProjection catch-up over a live graph', () => {
expect(source.title).toBe('Reminted')
expect(graph.links.size).toBe(0)
})

it('keeps only the groups the document holds again when retrying', () => {
const { graph } = buildLiveGraph()
const restored = new LiteGraph.LGraphGroup('Prompt group')
const dropped = new LiteGraph.LGraphGroup('Output group')
graph.add(restored)
graph.add(dropped)
const { host, hostEdit, projection, destroy } = bindAndCatchUp(
graph,
structuredClone(graph.serialize())
)
onTestFinished(destroy)
const [restoredGroup] = host.getMap('meta').get('groups') as unknown[]

function fail(): never {
throw new Error('remove failed')
}
vi.spyOn(graph, 'remove')
.mockImplementationOnce(fail)
.mockImplementationOnce(fail)
hostEdit(() => host.getMap('meta').set('groups', []))
expect(projection.hasPendingFailure(WORKFLOW_ID)).toBe(true)
expect(graph.groups).toEqual([restored, dropped])

hostEdit(() => host.getMap('meta').set('groups', [restoredGroup]))
expect(projection.retryFailed(WORKFLOW_ID)).toMatchObject({
failureCount: 0
})
expect(graph.groups).toEqual([restored])
expect(projection.hasPendingFailure(WORKFLOW_ID)).toBe(false)
})
})
8 changes: 6 additions & 2 deletions src/workbench/extensions/agent/crdt/agentCrdtProjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { LGraph } from '@/lib/litegraph/src/litegraph'
import type { NodeId } from '@/types/nodeId'

import { recordDevEvent } from './devPanelLog'
import { DocChangeCollector } from './docChangeCollector'
import { DocChangeCollector, groupIds } from './docChangeCollector'
import type { DocUpdate } from './docFrameClient'
import type { FollowerDoc } from './followerDoc'
import type { GraphOperation } from './graphOperations'
Expand Down Expand Up @@ -75,7 +75,11 @@ function changesAgainstDoc(doc: Y.Doc, changes: FrameChanges): FrameChanges {
if (!docNodes.has(id)) nodes.set(id, 'delete')
else nodes.set(id, change === 'delete' ? 'update' : change)
}
return { ...changes, nodes }
const docGroups = groupIds(doc.getMap('meta').get('groups'))
const removedGroups = new Set(
[...(changes.removedGroups ?? [])].filter((id) => !docGroups.has(id))
)
return { ...changes, nodes, removedGroups }
}

function pendingNodeDelta(target: BoundTarget, changes: FrameChanges) {
Expand Down
31 changes: 30 additions & 1 deletion src/workbench/extensions/agent/crdt/docChangeCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ export class DocChangeCollector {
Map<string, Set<string> | 'all'>
>()
private readonly interiorDefinitions = new Set<string>()
private readonly removedGroups = new Set<string>()
private readonly nodesMap: Y.Map<Y.Map<unknown>>
private readonly linksMap: Y.Map<unknown>
private readonly definitionsMap: Y.Map<unknown>
private readonly metaMap: Y.Map<unknown>

constructor(doc: Y.Doc) {
this.nodesMap = nodesMap(doc)
this.linksMap = linksMap(doc)
this.definitionsMap = definitionsObserverRoot(doc)
this.metaMap = doc.getMap('meta')
this.nodesMap.observeDeep(this.onNodesChanged)
this.linksMap.observe(this.onLinksChanged)
this.definitionsMap.observeDeep(this.onDefinitionsChanged)
this.metaMap.observe(this.onMetaChanged)
}

/** Returns everything collected so far without clearing it. */
Expand All @@ -53,7 +57,8 @@ export class DocChangeCollector {
new Map(nodes)
])
),
interiorDefinitions: new Set(this.interiorDefinitions)
interiorDefinitions: new Set(this.interiorDefinitions),
removedGroups: new Set(this.removedGroups)
}
}

Expand All @@ -71,12 +76,14 @@ export class DocChangeCollector {
this.links.clear()
this.interiors.clear()
this.interiorDefinitions.clear()
this.removedGroups.clear()
}

destroy(): void {
this.nodesMap.unobserveDeep(this.onNodesChanged)
this.linksMap.unobserve(this.onLinksChanged)
this.definitionsMap.unobserveDeep(this.onDefinitionsChanged)
this.metaMap.unobserve(this.onMetaChanged)
this.discard()
}

Expand Down Expand Up @@ -198,4 +205,26 @@ export class DocChangeCollector {
private readonly onLinksChanged = (event: Y.YMapEvent<unknown>): void => {
for (const id of event.keysChanged) this.links.add(id)
}

private readonly onMetaChanged = (event: Y.YMapEvent<unknown>): void => {
const change = event.changes.keys.get('groups')
if (!change) return
const current = groupIds(this.metaMap.get('groups'))
for (const id of groupIds(change.oldValue))
if (!current.has(id)) this.removedGroups.add(id)
for (const id of current) this.removedGroups.delete(id)
}
}

export function groupIds(groups: unknown): Set<string> {
const ids = new Set<string>()
if (!Array.isArray(groups)) return ids
for (const group of groups) {
const id: unknown =
typeof group === 'object' && group !== null
? (group as { id?: unknown }).id
: undefined
if (typeof id === 'number' || typeof id === 'string') ids.add(String(id))
}
return ids
}
18 changes: 18 additions & 0 deletions src/workbench/extensions/agent/crdt/liveGraphApplier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ export interface FrameChanges {
interiors?: InteriorChanges

interiorDefinitions?: ReadonlySet<string>

/** Root group ids the document's `meta.groups` held and no longer does. */
removedGroups?: ReadonlySet<string>
}

export interface RemoteApplyContext {
Expand Down Expand Up @@ -640,6 +643,8 @@ export class LiveGraphApplier {
})
}

this.deleteGroups(graph, changes.removedGroups, context)

for (const id of changes.resyncNodes) {
if (touchedNodes.has(id)) continue
this.try(context, () => this.syncFields(graph, doc, id))
Expand Down Expand Up @@ -805,6 +810,19 @@ export class LiveGraphApplier {
if (node) graph.remove(node)
}

private deleteGroups(
graph: LGraph,
ids: ReadonlySet<string> | undefined,
context: RemoteApplyContext
): void {
for (const id of ids ?? []) {
this.try(context, () => {
const group = graph.groups.find((group) => String(group.id) === id)
if (group) graph.remove(group)
})
}
}

private upsertNode(
graph: LGraph,
doc: Y.Doc,
Expand Down
Loading