From ca6c1fdeea3c3005f4a7e9eb184492c994ef4e68 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Tue, 8 Sep 2026 22:16:22 +0000 Subject: [PATCH] perf(agent): avoid redundant subgraph definition reads (#16971) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Avoids full subgraph-definition deep copies on unrelated CRDT frames. Registered definitions now take an ID-only fast path.
Full context for agent readers ## Summary `reconcileLiveGraph` first projects top-level and nested definition IDs. It materializes full definitions only when at least one ID is absent from the live root graph; ordinary node/widget frames still reconcile adapters with an empty definition list. Addresses item 3 of #16932 and the performance finding in #16922. ## Review Focus - ID projection traverses nested definition metadata without reading interior node/link/widget bodies. - Missing definitions retain the existing full-reader and registration path. - A regression proves a widget-style applied frame does not invoke the full reader once definitions are registered. ## Evidence - `NODE_OPTIONS="--no-experimental-webstorage" pnpm exec vitest run src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts` — 2 files, 62 tests passed - `pnpm exec eslint src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts` — passed - `pnpm typecheck` — passed - `git diff --check` — passed - Commit hooks: oxfmt, type-aware oxlint, ESLint, typecheck — passed - Pre-push hook: knip — passed
(cherry picked from commit eca7d5601eb217156953fc2b191af7f904c0b41d) --- .../crdt/agentSubgraphDefinitions.test.ts | 15 ++++++++- .../agent/crdt/agentSubgraphDefinitions.ts | 31 +++++++++++++++++++ .../agent/crdt/useAgentCrdtFollower.test.ts | 30 +++++++++++++++++- .../agent/crdt/useAgentCrdtFollower.ts | 15 ++++++--- 4 files changed, 85 insertions(+), 6 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts index ebf9607e0ad..94cb0ec9fcc 100644 --- a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts +++ b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.test.ts @@ -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: { @@ -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 diff --git a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts index 951b562db84..506ba10dfad 100644 --- a/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts +++ b/src/workbench/extensions/agent/crdt/agentSubgraphDefinitions.ts @@ -103,6 +103,37 @@ function readDefinition(source: Y.Map): 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(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, diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index d32c5cf14b7..cc8ab78595b 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -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) })) @@ -117,6 +120,7 @@ vi.mock('./agentNodeMaterializer', () => ({ })) vi.mock('./agentSubgraphDefinitions', () => ({ + readSubgraphDefinitionIds: definitionsState.readSubgraphDefinitionIds, readSubgraphDefinitions: definitionsState.readSubgraphDefinitions })) @@ -207,6 +211,7 @@ describe('useAgentCrdtFollower', () => { sessionStorage.clear() bridgeState.current = null materializerState.reconcileAgentAdapters.mockReset().mockReturnValue([]) + definitionsState.readSubgraphDefinitionIds.mockClear() definitionsState.readSubgraphDefinitions.mockClear() }) @@ -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) @@ -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) diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 03a41715d6c..beeb64f978d 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -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' @@ -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,