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
155 changes: 154 additions & 1 deletion src/lib/litegraph/src/LGraph.serialise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { ISerialisedGraph } from '@/lib/litegraph/src/litegraph'
import { useLinkStore } from '@/stores/linkStore'
import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId'
import { toNodeId } from '@comfyorg/graph-model/nodeId'
import { createTestSubgraph } from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers'

import { test } from './__fixtures__/testExtensions'

Expand Down Expand Up @@ -86,6 +87,9 @@ describe('LGraph Serialisation', () => {
graph.add(registered)
const adapterOnly = new LGraphNode('Adapter only')
adapterOnly.id = toNodeId(99)
adapterOnly.graph = graph
adapterOnly._state.graphId = graph.id
adapterOnly._graphScope = graphScopeOf(graph)
graph._nodes.push(adapterOnly)

const serialized = graph.serialize()
Expand All @@ -101,9 +105,158 @@ describe('LGraph Serialisation', () => {
{
surface: 'graph',
errorType: 'graph_serialization_state_mismatch',
tags: { outcome: 'degraded' },
context: {
graphId: graph.id,
mismatch: `live node ${adapterOnly.id} has no stored state`
rootGraphId: graph.rootGraph.id,
mismatch: `live node ${adapterOnly.id} has no stored state`,
mismatchKind: 'live_without_stored_state',
nodeId: adapterOnly.id,
nodeGraphId: graph.id,
stateGraphId: graph.id,
registrationRootGraphId: graph.rootGraph.id,
registrationOwningGraphId: graph.id
}
}
)
})

test('reports absent graph provenance for a detached live node', ({
expect
}) => {
const graph = new LGraph()
const registered = new LGraphNode('Registered')
graph.add(registered)
const detached = new LGraphNode('Detached')
detached.id = toNodeId(99)
graph._nodes.push(detached)

const serialized = graph.serialize()

expect(serialized.nodes.map(({ title }) => title)).toEqual([
'Registered',
'Detached'
])
expect(mockReportError).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
message: 'Graph serialization state mismatch'
}),
{
surface: 'graph',
errorType: 'graph_serialization_state_mismatch',
tags: { outcome: 'degraded' },
context: {
graphId: graph.id,
rootGraphId: graph.rootGraph.id,
mismatch: `live node ${detached.id} has no stored state`,
mismatchKind: 'live_without_stored_state',
nodeId: detached.id,
nodeGraphId: null,
stateGraphId: detached._state.graphId,
registrationRootGraphId: null,
registrationOwningGraphId: null
}
}
)
})

test('reports both sides of mixed stored and live divergence', ({
expect
}) => {
const graph = new LGraph()
const storedOnly = new LGraphNode('Stored only')
graph.add(storedOnly)
graph._nodes = []
storedOnly._state.graphId = 'stored-only-state-graph-id'
const liveOnly = new LGraphNode('Live only')
liveOnly.id = toNodeId(99)
liveOnly.graph = new LGraph()
liveOnly._state.graphId = 'live-only-state-graph-id'
liveOnly._graphScope = graphScopeOf({
id: 'live-only-registration-owning-graph-id',
rootGraph: { id: 'live-only-registration-root-graph-id' }
})
graph._nodes.push(liveOnly)

const serialized = graph.serialize()

expect(serialized.nodes.map(({ title }) => title)).toEqual(['Live only'])
expect(mockReportError).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
message: 'Graph serialization state mismatch'
}),
{
surface: 'graph',
errorType: 'graph_serialization_state_mismatch',
tags: { outcome: 'degraded' },
context: {
graphId: graph.id,
rootGraphId: graph.rootGraph.id,
mismatch: `stored node ${storedOnly.id} has no live adapter; live node ${liveOnly.id} has no stored state`,
mismatchKind: 'stored_and_live_diverged',
storedOnlyNodeId: storedOnly.id,
storedOnlyStateGraphId: 'stored-only-state-graph-id',
liveOnlyNodeId: liveOnly.id,
liveOnlyNodeGraphId: liveOnly.graph.id,
liveOnlyStateGraphId: 'live-only-state-graph-id',
liveOnlyRegistrationRootGraphId:
'live-only-registration-root-graph-id',
liveOnlyRegistrationOwningGraphId:
'live-only-registration-owning-graph-id'
}
}
)
})

test('reports the owning, node-state, and registration graph ids separately', ({
expect
}) => {
const rootGraph = new LGraph()
const subgraph = createTestSubgraph({ rootGraph })
const attachedGraph = new LGraph()
const registered = new LGraphNode('Registered')
subgraph.add(registered)
const staleScope = new LGraphNode('Stale scope')
staleScope.id = toNodeId(99)
staleScope.graph = attachedGraph
staleScope._state.graphId = 'state-graph-id'
staleScope._graphScope = graphScopeOf({
id: 'registration-owning-graph-id',
rootGraph: { id: 'registration-root-graph-id' }
})
subgraph._nodes.push(staleScope)

expect(
new Set([
subgraph.id,
rootGraph.id,
attachedGraph.id,
'state-graph-id',
'registration-owning-graph-id',
'registration-root-graph-id'
]).size
).toBe(6)

subgraph.asSerialisable()

expect(mockReportError).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
message: 'Graph serialization state mismatch'
}),
{
surface: 'graph',
errorType: 'graph_serialization_state_mismatch',
tags: { outcome: 'degraded' },
context: {
graphId: subgraph.id,
rootGraphId: rootGraph.id,
mismatch: `live node ${staleScope.id} has no stored state`,
mismatchKind: 'live_without_stored_state',
nodeId: staleScope.id,
nodeGraphId: attachedGraph.id,
stateGraphId: 'state-graph-id',
registrationRootGraphId: 'registration-root-graph-id',
registrationOwningGraphId: 'registration-owning-graph-id'
}
}
)
Expand Down
18 changes: 16 additions & 2 deletions src/lib/litegraph/src/LGraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,18 @@ describe('Store-driven serialization parity', () => {
test('falls back safely when a stored node has no live adapter', ({
expect
}) => {
const graph = createGraph(new DummyNode())
const stranded = new DummyNode()
const rootGraph = new LGraph()
const graph = createTestSubgraph({ rootGraph })
graph.add(stranded)
const strandedId = stranded.id
stranded._state.graphId = 'stored-state-graph-id'
graph._nodes = []

expect(
new Set([graph.id, rootGraph.id, stranded._state.graphId]).size
).toBe(3)

expect(graph.asSerialisable().nodes).toEqual([])
expect(mockReportError).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
Expand All @@ -933,9 +942,14 @@ describe('Store-driven serialization parity', () => {
{
surface: 'graph',
errorType: 'graph_serialization_state_mismatch',
tags: { outcome: 'degraded' },
context: {
graphId: graph.id,
mismatch: expect.stringMatching(/stored node .* has no live adapter/)
rootGraphId: rootGraph.id,
mismatch: `stored node ${strandedId} has no live adapter`,
mismatchKind: 'stored_without_live_adapter',
nodeId: strandedId,
stateGraphId: 'stored-state-graph-id'
}
}
)
Expand Down
60 changes: 48 additions & 12 deletions src/lib/litegraph/src/LGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,21 +478,57 @@ function serialiseStoredNodes(owner: LGraph, sortNodes: boolean) {
ordered.length !== adapters.size
) {
const liveNodes = [...adapters.values()]
const missingState = ordered.find((state) => !adapters.has(state.id))
let missingAdapter: LGraphNode | undefined
if (missingState === undefined) {
const stateIds = new Set(ordered.map((state) => state.id))
missingAdapter = liveNodes.find((adapter) => !stateIds.has(adapter.id))
}
const mismatch = missingState
? `stored node ${missingState.id} has no live adapter`
: missingAdapter
? `live node ${missingAdapter.id} has no stored state`
: `${ordered.length} stored nodes do not match ${adapters.size} live nodes`
const stateIds = new Set(ordered.map((state) => state.id))
const storedOnlyState = ordered.find((state) => !adapters.has(state.id))
const liveOnlyAdapter = liveNodes.find(
(adapter) => !stateIds.has(adapter.id)
)
const mismatchDetails = storedOnlyState
? liveOnlyAdapter
? {
mismatch: `stored node ${storedOnlyState.id} has no live adapter; live node ${liveOnlyAdapter.id} has no stored state`,
mismatchKind: 'stored_and_live_diverged',
storedOnlyNodeId: storedOnlyState.id,
storedOnlyStateGraphId: storedOnlyState.graphId,
liveOnlyNodeId: liveOnlyAdapter.id,
liveOnlyNodeGraphId: liveOnlyAdapter.graph?.id ?? null,
liveOnlyStateGraphId: liveOnlyAdapter._state.graphId,
liveOnlyRegistrationRootGraphId:
liveOnlyAdapter._graphScope?.rootGraphId ?? null,
liveOnlyRegistrationOwningGraphId:
liveOnlyAdapter._graphScope?.owningGraphId ?? null
}
: {
mismatch: `stored node ${storedOnlyState.id} has no live adapter`,
mismatchKind: 'stored_without_live_adapter',
nodeId: storedOnlyState.id,
stateGraphId: storedOnlyState.graphId
}
: liveOnlyAdapter
? {
mismatch: `live node ${liveOnlyAdapter.id} has no stored state`,
mismatchKind: 'live_without_stored_state',
nodeId: liveOnlyAdapter.id,
nodeGraphId: liveOnlyAdapter.graph?.id ?? null,
stateGraphId: liveOnlyAdapter._state.graphId,
registrationRootGraphId:
liveOnlyAdapter._graphScope?.rootGraphId ?? null,
registrationOwningGraphId:
liveOnlyAdapter._graphScope?.owningGraphId ?? null
}
: {
mismatch: `${ordered.length} stored nodes do not match ${adapters.size} live nodes`,
mismatchKind: 'count_mismatch'
}
reportError(new Error('Graph serialization state mismatch'), {
surface: 'graph',
errorType: 'graph_serialization_state_mismatch',
context: { graphId: owner.id, mismatch }
tags: { outcome: 'degraded' },
context: {
graphId: owner.id,
rootGraphId: owner.rootGraph.id,
...mismatchDetails
}
})
const nodes = sortNodes
? [...liveNodes].sort((a, b) => compareNodeIds(a.id, b.id))
Expand Down
Loading