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
2 changes: 1 addition & 1 deletion packages/graph-model/src/workflowSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ const zSubgraphInstance = z
})
.passthrough()

const zGroup = z
export const zGroup = z
.object({
id: z.number().optional(),
title: z.string(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { mint, nodesMap } from '@comfyorg/comfy-multi-player'
import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player'
import { applyOps, mint, nodesMap } from '@comfyorg/comfy-multi-player'
import type {
Op,
WidgetCatalog,
WorkflowJSON
} from '@comfyorg/comfy-multi-player'
import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest'
import * as Y from 'yjs'

Expand Down Expand Up @@ -46,6 +50,18 @@ const CATALOG: WidgetCatalog = {
types: { TestSource: { widget_order: ['steps', 'seed'] } }
}

const HOST_OP: Pick<Op, 'actor' | 'base_version' | 'stamp'> = {
actor: 'agent:test:turn-1',
base_version: 1,
stamp: [1, 'agent:test:turn-1']
}

function applyHostOp(host: Y.Doc, op: Op) {
expect(applyOps(host, [op], CATALOG).outcomes).toEqual([
{ op_id: op.op_id, outcome: 'applied' }
])
}

type FrameClassification = Pick<ClassifiedDocUpdate, 'actor'> &
Partial<Pick<ClassifiedDocUpdate, 'catchUp'>>

Expand Down Expand Up @@ -368,6 +384,33 @@ describe('AgentCrdtProjection catch-up over a live graph', () => {
expect(graph.links.size).toBe(0)
})

it('removes the groups a host clear drops and keeps a group the document never held', () => {
const { graph } = buildLiveGraph()
const prompt = new LiteGraph.LGraphGroup('Prompt group')
const output = new LiteGraph.LGraphGroup('Output group')
graph.add(prompt)
graph.add(output)
const { host, hostEdit, destroy } = bindAndCatchUp(
graph,
structuredClone(graph.serialize())
)
onTestFinished(destroy)
const drawnAfterMint = new LiteGraph.LGraphGroup('Drawn after mint')
graph.add(drawnAfterMint)

hostEdit(() =>
applyHostOp(host, {
...HOST_OP,
op_id: 'c1ea'.padEnd(32, '0'),
op: 'clear',
removed_nodes: [1, 2]
})
)

expect(graph._nodes).toEqual([])
expect(graph.groups).toEqual([drawnAfterMint])
})

it('keeps only the groups the document holds again when retrying', () => {
const { graph } = buildLiveGraph()
const restored = new LiteGraph.LGraphGroup('Prompt group')
Expand All @@ -379,7 +422,9 @@ describe('AgentCrdtProjection catch-up over a live graph', () => {
structuredClone(graph.serialize())
)
onTestFinished(destroy)
const [restoredGroup] = host.getMap('meta').get('groups') as unknown[]
const mintedGroups = host.getMap('meta').get('groups')
assert(Array.isArray(mintedGroups), 'mint wrote meta.groups')
const [restoredGroup] = mintedGroups

function fail(): never {
throw new Error('remove failed')
Expand All @@ -398,4 +443,113 @@ describe('AgentCrdtProjection catch-up over a live graph', () => {
expect(graph.groups).toEqual([restored])
expect(projection.hasPendingFailure(WORKFLOW_ID)).toBe(false)
})

it('creates the groups an inserted workflow carries, placed with its nodes', () => {
const { graph, source, note } = buildLiveGraph()
source.setSize([200, 100])
note.setSize([200, 100])
const { host, hostEdit, destroy } = bindAndCatchUp(
graph,
structuredClone(graph.serialize())
)
onTestFinished(destroy)

hostEdit(
() =>
applyHostOp(host, {
...HOST_OP,
op_id: '1a5e'.padEnd(32, '0'),
op: 'insert_workflow',
workflow: {
nodes: [
{
id: 1,
type: 'TestSource',
pos: [20, 30],
size: [200, 100],
widgets_values: [20, 7]
}
],
groups: [
{ id: 1, title: 'Inserted group', bounding: [0, 10, 300, 200] }
]
}
}),
{ actor: HOST_OP.actor, catchUp: false }
)

const inserted = graph._nodes.find(
(node) => node !== source && node !== note
)
const group = graph.groups.find(({ title }) => title === 'Inserted group')
assert(inserted && group, 'the insertion reached the canvas')
expect([...inserted.pos]).not.toEqual([20, 30])
expect([
group.pos[0] - inserted.pos[0],
group.pos[1] - inserted.pos[1]
]).toEqual([-20, -20])
expect([...group.size]).toEqual([300, 200])
})
it('places an inserted workflow that carries only groups', () => {
const { graph, source, note } = buildLiveGraph()
source.setSize([200, 100])
note.setSize([200, 100])
const { host, hostEdit, destroy } = bindAndCatchUp(
graph,
structuredClone(graph.serialize())
)
onTestFinished(destroy)

hostEdit(
() =>
applyHostOp(host, {
...HOST_OP,
op_id: '1a5f'.padEnd(32, '0'),
op: 'insert_workflow',
workflow: {
nodes: [],
groups: [
{ id: 1, title: 'Inserted group', bounding: [0, 10, 300, 200] }
]
}
}),
{ actor: HOST_OP.actor, catchUp: false }
)

const group = graph.groups.find(({ title }) => title === 'Inserted group')
assert(group, 'the inserted group reached the canvas')
expect([...group.pos]).toEqual([680, 20])
})
it('places a group-only insertion clear of the groups already on the canvas', () => {
const graph = new LGraph()
const existing = new LiteGraph.LGraphGroup('Existing group')
existing.pos = [0, 10]
existing.size = [300, 200]
graph.add(existing)
const { host, hostEdit, destroy } = bindAndCatchUp(
graph,
structuredClone(graph.serialize())
)
onTestFinished(destroy)

hostEdit(
() =>
applyHostOp(host, {
...HOST_OP,
op_id: '1a61'.padEnd(32, '0'),
op: 'insert_workflow',
workflow: {
nodes: [],
groups: [
{ id: 1, title: 'Inserted group', bounding: [0, 10, 300, 200] }
]
}
}),
{ actor: HOST_OP.actor, catchUp: false }
)

const group = graph.groups.find(({ title }) => title === 'Inserted group')
assert(group, 'the inserted group reached the canvas')
expect([...group.pos]).toEqual([380, 10])
})
})
8 changes: 2 additions & 6 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 '@comfyorg/graph-model/nodeId'

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

function pendingNodeDelta(target: BoundTarget, changes: FrameChanges) {
Expand Down
21 changes: 21 additions & 0 deletions src/workbench/extensions/agent/crdt/docChangeCollector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,27 @@ describe('DocChangeCollector', () => {
expect(changes.nodes.size).toBe(0)
})

it('records the group ids meta.groups gains or loses, ignoring other meta keys', () => {
const { doc, collector } = setup()
const group = (id: number) => ({
id,
title: `Group ${id}`,
bounding: [0, 0, 200, 100]
})
const meta = doc.getMap<unknown>('meta')
doc.transact(() => meta.set('groups', [group(7), group(9)]))
expect(collector.take().groups).toEqual(new Set(['7', '9']))

doc.transact(() => meta.set('last_node_id', 12))
expect(collector.take().groups).toEqual(new Set())

doc.transact(() => meta.set('groups', [group(9), group(11)]))
expect(collector.take().groups).toEqual(new Set(['7', '11']))

doc.transact(() => meta.set('groups', []))
expect(collector.take().groups).toEqual(new Set(['9', '11']))
})

it('records link keys, clears on take, and stops observing after destroy', () => {
const { doc, collector } = setup()

Expand Down
31 changes: 9 additions & 22 deletions src/workbench/extensions/agent/crdt/docChangeCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as Y from 'yjs'

import { definitionsObserverRoot } from './agentSubgraphDefinitions'
import type { FrameChanges, NodeChange } from './liveGraphApplier'
import { SYNCED_NODE_FIELDS } from './liveGraphApplier'
import { readDocGroups, SYNCED_NODE_FIELDS } from './liveGraphApplier'

/**
* Collects what the Y observers on one follower document report between two
Expand All @@ -27,7 +27,7 @@ export class DocChangeCollector {
Map<string, Set<string> | 'all'>
>()
private readonly interiorDefinitions = new Set<string>()
private readonly removedGroups = new Set<string>()
private readonly groups = new Set<string>()
private readonly nodesMap: Y.Map<Y.Map<unknown>>
private readonly linksMap: Y.Map<unknown>
private readonly definitionsMap: Y.Map<unknown>
Expand All @@ -37,7 +37,7 @@ export class DocChangeCollector {
this.nodesMap = nodesMap(doc)
this.linksMap = linksMap(doc)
this.definitionsMap = definitionsObserverRoot(doc)
this.metaMap = doc.getMap('meta')
this.metaMap = doc.getMap<unknown>('meta')
this.nodesMap.observeDeep(this.onNodesChanged)
this.linksMap.observe(this.onLinksChanged)
this.definitionsMap.observeDeep(this.onDefinitionsChanged)
Expand All @@ -60,7 +60,7 @@ export class DocChangeCollector {
])
),
interiorDefinitions: new Set(this.interiorDefinitions),
removedGroups: new Set(this.removedGroups)
groups: new Set(this.groups)
}
}

Expand All @@ -78,7 +78,7 @@ export class DocChangeCollector {
this.links.clear()
this.interiors.clear()
this.interiorDefinitions.clear()
this.removedGroups.clear()
this.groups.clear()
}

destroy(): void {
Expand Down Expand Up @@ -223,22 +223,9 @@ export class DocChangeCollector {
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)
const before = readDocGroups(change.oldValue)
const after = readDocGroups(this.metaMap.get('groups'))
for (const id of before.keys()) if (!after.has(id)) this.groups.add(id)
for (const id of after.keys()) if (!before.has(id)) this.groups.add(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
}
29 changes: 29 additions & 0 deletions src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2796,6 +2796,35 @@ describe('LiveGraphApplier change bracket', () => {
}
)

it('drops the groups a replacement document no longer holds', () => {
const { graph, applier, applyCollected } = setup({
nodes: [],
links: [],
groups: [
{ id: 5, title: 'Dropped', bounding: [0, 0, 100, 100] },
{ id: 6, title: 'Kept', bounding: [200, 0, 100, 100] }
]
})
applyCollected()
const replacement = followedDoc(
{
nodes: [],
links: [],
groups: [{ id: 6, title: 'Kept', bounding: [200, 0, 100, 100] }]
},
CATALOG
)

applier.applyChanges(
replacement.doc,
replacement.collector.take(),
CONTEXT,
'replace'
)

expect(graph.groups.map((group) => group.title)).toEqual(['Kept'])
})

it('brackets a catch-up frame and a lineage replacement once each, and never without a graph', () => {
const { canvas, events } = recordingCanvas()
const { graph, doc, collector, applier } = setup(seed, {
Expand Down
Loading
Loading