fix(workflow): deep copy initial_state to prevent mutation leaks across runs#21780
Merged
logan-markewich merged 1 commit intoMay 28, 2026
Merged
Conversation
…ss runs AgentWorkflow stores initial_state in the context by reference (multi-agent path) or via shallow copy (single-agent path). This causes in-place state mutations during one run() to persist into subsequent runs, violating the documented stateless-between-runs behavior. Fix: use copy.deepcopy() in both paths to ensure each run gets an independent copy of initial_state, preventing nested mutable values (lists, dicts) from leaking across invocations. Fixes run-llama#21774 Signed-off-by: Gaurav Kumar Sinha <gaurav@substrai.dev>
logan-markewich
approved these changes
May 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes
AgentWorkflowleakinginitial_statemutations acrossrun()calls when the same workflow instance is reused.Problem
AgentWorkflow): storesself.initial_statein the context by direct reference, so in-place mutations modify the workflow's own stateBaseWorkflowAgent): uses.copy()(shallow copy), which avoids top-level leakage but still leaks nested mutable values (lists, dicts)This violates the documented behavior that workflows are stateless between runs unless a
Contextis explicitly reused.Fix
Use
copy.deepcopy()in both paths to ensure eachrun()gets a fully independent copy ofinitial_state.Changes
multi_agent_workflow.py:copy.deepcopy(self.initial_state)instead ofself.initial_statebase_agent.py:copy.deepcopy(self.initial_state)instead ofself.initial_state.copy()Reproduction (from issue)
Fixes #21774