Skip to content

fix: deepcopy agent configs in AgentWorkflow to prevent shared mutable state#22150

Open
C1-BA-B1-F3 wants to merge 1 commit into
run-llama:mainfrom
C1-BA-B1-F3:fix-agentworkflow-deepcopy
Open

fix: deepcopy agent configs in AgentWorkflow to prevent shared mutable state#22150
C1-BA-B1-F3 wants to merge 1 commit into
run-llama:mainfrom
C1-BA-B1-F3:fix-agentworkflow-deepcopy

Conversation

@C1-BA-B1-F3

Copy link
Copy Markdown

Problem

AgentWorkflow.__init__ stores agent configs by reference:

self.agents = {cfg.name: cfg for cfg in agents}

This means if the same BaseTool instance or agent config is shared across multiple AgentWorkflow instances, they share mutable state — mutating one workflow's agent affects the other.

Fix

Apply copy.deepcopy() to each agent config before storing:

self.agents = {cfg.name: copy.deepcopy(cfg) for cfg in agents}

copy was already imported in the file.

Test

Added test_agents_stored_by_deepcopy which creates two AgentWorkflow instances from the same agent, mutates one, and verifies the other is unaffected.

Fixes #22146

🤖 Generated with Claude Code

…e state

AgentWorkflow.__init__ stored agent configs by reference, so sharing
the same BaseTool or agent config across multiple AgentWorkflow
instances would cause them to share mutable state. Apply
copy.deepcopy() to each agent config before storing.

Fixes run-llama#22146

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: AgentWorkflow stores agents by reference; one BaseTool instance shared across agents leaks per-tool mutations

1 participant