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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import * as Y from 'yjs'
import { createTestSubgraphData } from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers'
import type { ExportedSubgraph } from '@/lib/litegraph/src/types/serialisation'

import { readSubgraphDefinitions } from './agentSubgraphDefinitions'
import {
readSubgraphDefinitionIds,
readSubgraphDefinitions
} from './agentSubgraphDefinitions'

const CATALOG: WidgetCatalog = {
types: {
Expand Down Expand Up @@ -138,6 +141,16 @@ describe('readSubgraphDefinitions', () => {
expect(projected.definitions).toEqual({ subgraphs: [inner] })
})

it('projects top-level and nested definition ids without reading bodies', () => {
const inner = createTestSubgraphData({ nodes: [interiorNode(1)] as never })
const outer = createTestSubgraphData({
nodes: [interiorNode(2, inner.id)] as never,
definitions: { subgraphs: [inner] }
})

expect(readSubgraphDefinitionIds(seed(outer))).toEqual([outer.id, inner.id])
})

it('skips definition and node entries that are not records', () => {
const definition = createTestSubgraphData({
nodes: [interiorNode(1)] as never
Expand Down
31 changes: 31 additions & 0 deletions src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,37 @@ function readDefinition(source: Y.Map<unknown>): ExportedSubgraph {
return definition as unknown as ExportedSubgraph
}

function readField(source: unknown, key: string): unknown {
if (source instanceof Y.Map) return source.get(key)
if (typeof source !== 'object' || source === null || !(key in source)) {
return undefined
}
return Reflect.get(source, key)
}

function readList(source: unknown): unknown[] {
if (source instanceof Y.Array) return source.toArray()
return Array.isArray(source) ? source : []
}

function collectDefinitionIds(source: unknown, ids: string[]): void {
const id = readField(source, 'id')
if (typeof id === 'string') ids.push(id)
const nested = readField(readField(source, 'definitions'), 'subgraphs')
for (const definition of readList(nested)) {
collectDefinitionIds(definition, ids)
}
}

export function readSubgraphDefinitionIds(doc: Y.Doc): string[] {
const ids: string[] = []
if (!doc.share.has(DEFINITIONS_ROOT)) return ids
doc.getMap<unknown>(DEFINITIONS_ROOT).forEach((value) => {
if (value instanceof Y.Map) collectDefinitionIds(value, ids)
})
return ids
}

/**
* Project the subgraph definitions the op layer minted into the follower doc
* back to the `ExportedSubgraph` shape `LGraph.createSubgraphs()` consumes,
Expand Down
30 changes: 29 additions & 1 deletion src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ const definitionsState = vi.hoisted(() => ({
fakeDefinitions: [
{ id: '11111111-1111-4111-8111-111111111111' } as ExportedSubgraph
],
readSubgraphDefinitionIds: vi.fn(() => [
'11111111-1111-4111-8111-111111111111'
]),
readSubgraphDefinitions: vi.fn(() => definitionsState.fakeDefinitions)
}))

Expand Down Expand Up @@ -117,6 +120,7 @@ vi.mock('./agentNodeMaterializer', () => ({
}))

vi.mock('./agentSubgraphDefinitions', () => ({
readSubgraphDefinitionIds: definitionsState.readSubgraphDefinitionIds,
readSubgraphDefinitions: definitionsState.readSubgraphDefinitions
}))

Expand Down Expand Up @@ -207,6 +211,7 @@ describe('useAgentCrdtFollower', () => {
sessionStorage.clear()
bridgeState.current = null
materializerState.reconcileAgentAdapters.mockReset().mockReturnValue([])
definitionsState.readSubgraphDefinitionIds.mockClear()
definitionsState.readSubgraphDefinitions.mockClear()
})

Expand Down Expand Up @@ -669,8 +674,10 @@ describe('useAgentCrdtFollower', () => {
describe('live-graph reconcile', () => {
// The materializer is module-mocked, so the graph only needs to be a
// distinct reference the composable hands through.
const fakeGraph = {} as MaterializableGraph
const { fakeDefinitions } = definitionsState
const fakeGraph = {
rootGraph: { subgraphs: new Map() }
} as unknown as MaterializableGraph

it('reconciles the live graph after every applied frame', () => {
const { unmount } = mountFollower('wf-1', true, () => fakeGraph)
Expand All @@ -690,6 +697,27 @@ describe('useAgentCrdtFollower', () => {
unmount()
})

it('does not deep-copy definitions for a frame when all are registered', () => {
const registeredGraph = {
rootGraph: {
subgraphs: new Map([[fakeDefinitions[0].id, {}]])
}
} as unknown as MaterializableGraph
const { unmount } = mountFollower('wf-1', true, () => registeredGraph)

dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 9 })

expect(definitionsState.readSubgraphDefinitionIds).toHaveBeenCalledWith(
bridge().follower.doc
)
expect(definitionsState.readSubgraphDefinitions).not.toHaveBeenCalled()
expect(materializerState.reconcileAgentAdapters).toHaveBeenCalledWith(
registeredGraph,
[]
)
unmount()
})

it('does not reconcile after a frame the adapter skipped', () => {
adapterState.applyFrame.mockReturnValueOnce(false)
const { unmount, status } = mountFollower('wf-1', true, () => fakeGraph)
Expand Down
15 changes: 11 additions & 4 deletions src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { createUuidv4 } from '@/utils/uuid'

import type { MaterializableGraph } from './agentNodeMaterializer'
import { reconcileAgentAdapters } from './agentNodeMaterializer'
import { readSubgraphDefinitions } from './agentSubgraphDefinitions'
import {
readSubgraphDefinitionIds,
readSubgraphDefinitions
} from './agentSubgraphDefinitions'
import { recordDevEvent } from './devPanelLog'
import { wireLog } from './crdtLog'
import type { CrdtDebugSnapshot } from './crdtSnapshot'
Expand Down Expand Up @@ -580,10 +583,14 @@ export function useAgentCrdtFollower(
function reconcileLiveGraph(docId: string): void {
const graph = getGraph()
if (!graph) return
const nodeIds = reconcileAgentAdapters(
graph,
readSubgraphDefinitions(bridge.follower.doc)
const definitionIds = readSubgraphDefinitionIds(bridge.follower.doc)
const hasMissingDefinition = definitionIds.some(
(id) => !graph.rootGraph.subgraphs.has(id)
)
const definitions = hasMissingDefinition
? readSubgraphDefinitions(bridge.follower.doc)
: []
const nodeIds = reconcileAgentAdapters(graph, definitions)
if (nodeIds.length > 0) {
recordDevEvent('agent_node_adapters_materialized', {
workflowId: docId,
Expand Down
Loading