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
8 changes: 5 additions & 3 deletions src/compiled-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ export class CompiledGraph<
stateMiddleware: GraphSDK.StateMiddleware<State, NodeKeys>[] = [],
eventMiddleware: GraphSDK.EventMiddleware<State, NodeKeys>[] = []
) {
this.nodeRegistry = nodeRegistry
this.edgeRegistry = edgeRegistry
this.subgraphRegistry = subgraphRegistry
this.nodeRegistry = new Map(nodeRegistry)
this.edgeRegistry = new Map(
Array.from(edgeRegistry.entries()).map(([k, v]) => [k, [...v]])
)
this.subgraphRegistry = new Map(subgraphRegistry)
this.storage = options.storage ?? new InMemoryStorage()
this.graphMiddleware = graphMiddleware
this.nodeMiddleware = nodeMiddleware
Expand Down
24 changes: 24 additions & 0 deletions tests/graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,30 @@ describe('Graph - State Streaming via data-state events', () => {
})
})

describe('Graph - Immutability after compile()', () => {
test('mutating graph after compile() does not affect compiled graph', async () => {
const executionOrder: string[] = []

const g = graph<{ value: number }>()
.node('a', () => { executionOrder.push('a') })
.edge('START', 'a')
.edge('a', 'END')

const compiled = g.compile()

// Mutate the original graph after compilation — add node 'b' and re-route a→b→END
g.node('b', () => { executionOrder.push('b') })
.edge('a', 'b')
.edge('b', 'END')

// Compiled graph should still run the original topology: START→a→END
await runGraph(compiled.stream('run-1', { value: 0 }))

expect(executionOrder).toEqual(['a'])
expect(executionOrder).not.toContain('b')
})
})

describe('Graph - Headless execute()', () => {
test('execute() returns final state', async () => {
const g = graph<{ value: number }>()
Expand Down