[AAASM-1184] ✨ (pydantic_ai): Wrap Tool._run execution in spawn_context_scope#41
Merged
Chisanan232 merged 3 commits intoMay 9, 2026
Merged
Conversation
Each allowed tool call now sets a SpawnContext with spawned_by_tool=tool_name and delegation_reason="tool:<name>" so child agents inherit full lineage. Also fixes pre-existing UP035/B010/B009/I001 ruff violations in the file. Closes #AAASM-1184
TestApplyToolRunPatch (9 tests): spawned_by_tool, delegation_reason, parent_agent_id from ctx, depth increment, reset on completion and exception, deny skips spawn ctx, idempotent apply, revert restores original. Integration Test 9: end-to-end pydantic_ai tool spawn context through patched _run verifying all SpawnContext fields.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Thread-safe in-memory graph keyed on agent_id → parent_agent_id. record(), children_of(), ancestors_of() enable querying the spawn tree built from SpawnContext lineage data. Exported from agent_assembly.core. Integration Test 10 proves the full chain: patched Pydantic AI Tool._run sets _SPAWN_CTX → child agent reads parent_agent_id → LineageRegistry records the edge → children_of and ancestors_of return correct lineage. Closes AC #5 of AAASM-1184.
|
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.



What changed
Each allowed Pydantic AI tool call now executes inside a
spawn_context_scopesoany child agent initialised during tool execution inherits full lineage
(
spawned_by_tool,delegation_reason,parent_agent_id,depth).A new
LineageRegistryprovideschildren_of/ancestors_ofgraph queries overthe parent–child relationships built from
SpawnContextdata.Pre-existing ruff violations in
pydantic_ai/patch.py(UP035/B010/B009/I001) arefixed in the same commit.
Why
AAASM-1184 — the final subtask of AAASM-212. The three earlier subtasks already
covered Agent.run spawn interception (AAASM-1182), CrewAI team_id / delegation_reason
/ hierarchical kickoff (AAASM-1183), and cross-framework integration tests
(AAASM-1144). This subtask completes the lineage chain at the tool-call boundary so
every sub-agent spawned by a tool is traceable back to its tool invocation, and
operators can query the spawn tree via
children_of/ancestors_of.Changes
agent_assembly/adapters/pydantic_ai/patch.pypatched_run(inside_apply_tool_run_patch), after governance allow,wraps
original_runcall inspawn_context_scope(SpawnContext(...))with:spawned_by_tool = tool_namedelegation_reason = f"tool:{tool_name}"parent_agent_id = agent_id or ""depth = _current_spawn_depth()Mappingfromcollections.abc),B010 (
agent_cls.run =/agent_cls.run_sync =/tool_cls._run =),B009 (
args.model_dump), I001 (import sort).agent_assembly/core/lineage.py(new)LineageRegistry— thread-safe in-memory graph:record(agent_id, parent_agent_id),children_of(parent_id) → list[str],ancestors_of(agent_id) → list[str].agent_assembly.core.agent_assembly/core/__init__.pyLineageRegistryalongside existinginit_assembly/AssemblyContext.test/unit/adapters/pydantic_ai/test_pydantic_ai_spawn_patch.pyTestApplyToolRunPatchclass (9 tests):spawned_by_tool,delegation_reason,parent_agent_idfrom ctx, depth increment, reset on completion and exception,denied tool skips spawn ctx, idempotent apply, revert restores original.
test/unit/core/test_lineage_registry.py(new)record,children_of,ancestors_of,__len__,overwrite, unknown agents, multi-level chains.
test/integration/test_spawn_lineage_integration.pyTool._run,verifying all
SpawnContextfields and post-call reset._SPAWN_CTX, child agentreads
parent_agent_idfrom it and callsregistry.record(), thenchildren_ofandancestors_ofreturn correct lineage.Acceptance criteria
@agent.toolfunctions set_SPAWN_CTXbefore invoking tool bodyspawned_by_tool= tool function namedelegation_reason = f"tool:{tool_name}"Agent.run/Agent.run_syncunchanged (no regression)children_of/ancestors_ofreturn correct lineageHow to verify
Closes #AAASM-1184