fix(agents): prevent parent authorized_imports from leaking into managed agents on from_dict#2218
Open
VANDRANKI wants to merge 3 commits intohuggingface:mainfrom
Open
Conversation
…gs in from_dict When a CodeAgent with managed agents is deserialized via from_dict(), CodeAgent.from_dict() extracts additional_authorized_imports from the current agent's dict and passes them in code_agent_kwargs to super().from_dict(). MultiStepAgent.from_dict() then forwards those same kwargs to every managed agent's from_dict() call. Because code_agent_kwargs.update(kwargs) runs before the managed agent's own authorized_imports are set, the parent's import allowlist overwrites the child's own. Fix: remove additional_authorized_imports from the initial code_agent_kwargs dict and re-apply it from the agent's own serialized data after update(kwargs), so each agent always reconstructs with its own import allowlist regardless of what the parent propagated. Fixes: huggingface#1849
…ged agents When a CodeAgent with managed sub-agents is deserialized via from_dict(), CodeAgent.from_dict() builds code_agent_kwargs that includes additional_authorized_imports from the parent's own serialized dict, then calls super().from_dict(agent_dict, **code_agent_kwargs). MultiStepAgent.- from_dict() forwarded those kwargs unchanged to every managed agent, causing each child's additional_authorized_imports to be overwritten by the parent's. Fix: in MultiStepAgent.from_dict(), filter out CodeAgent-specific keys (additional_authorized_imports, executor_type, executor_kwargs, max_print_outputs_length, code_block_tags) before passing kwargs to managed agents. Generic overrides like model= are still propagated tree-wide. This preserves the ability to call from_dict(dict, model=new_model) and have the new model apply to all agents in the tree, while ensuring each agent's import allowlist and executor config are always reconstructed from its own serialized data. Fixes: huggingface#1849
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.
Summary
When a
CodeAgentwith managed sub-agents is deserialized viafrom_dict(), the parent agent'sadditional_authorized_importsoverwrites every child agent's own import allowlist. Reproducer from issue #1849:Root cause
CodeAgent.from_dict()buildscode_agent_kwargscontainingadditional_authorized_importsfrom the current agent's serialized dict, then callssuper().from_dict(agent_dict, **code_agent_kwargs).MultiStepAgent.from_dict()forwarded those kwargs unchanged to every managed agent viaagent_class.from_dict(managed_agent_dict, **kwargs). Because the parent'sadditional_authorized_importsis in kwargs, it overwrites the child's own value.Fix
In
MultiStepAgent.from_dict(), filter out CodeAgent-specific keys (additional_authorized_imports,executor_type,executor_kwargs,max_print_outputs_length,code_block_tags) before passing kwargs to managed agents. Generic overrides likemodel=are still forwarded tree-wide.This preserves the existing behavior where callers can do
from_dict(d, model=new_model)to inject a model into the whole agent tree, while ensuring each agent's import allowlist is always reconstructed from its own serialized data.Test
Added
test_from_dict_managed_agent_authorized_imports_not_leakedinTestCodeAgentthat verifies a child agent with['sympy', 'math']imports retains them after a parent with['numpy', 'pandas']is deserialized.Fixes #1849