ISSUE DESCRIPTION [Bug] team.acontinuerun loss dependencies or sessionstate Description…
- User
ISSUE DESCRIPTION
[Bug] team.acontinue_run loss dependencies or session_state
Description
team.acontinue_run loss dependencies or session_state
Steps to Reproduce
requires_confirmation_tools = [ "group_del", "group_del_stock", ] async def group_del_stock(self, group_id: str, stock_list: list[dict], run_context: RunContext) -> Dict[str, Any]: url = f"{self.live_base_url}/cloud/api/optional/stock/code/del" headers = { "Content-Type": "application/json", "X-Access-Token": run_context.session_state["user_token"] } data = {"targetStockGroupId":group_id,"delVoList":stock_list}example code
session_id = str(uuid.uuid4()) user_id= '00bgh2' session_state = {"user_token":"Bearer "} asyncio.run(main()) async def main(): async for run_event in stock_team.arun( "将茅台从优质股中删除", stream=True, session_id=session_id, user_id=user_id, session_state=session_state ): from agno.run.team import RunPausedEvent as TeamRunPausedEvent # Use isinstance to check for team's pause event (not the member agent's) if isinstance(run_event, TeamRunPausedEvent): print("Team paused - requires confirmation") for req in run_event.active_requirements: if req.needs_confirmation: print(f" Tool: {req.tool_execution.tool_name}") print(f" Args: {req.tool_execution.tool_args}") req.confirm() # Use apprint_run_response for async streaming response = stock_team.acontinue_run( run_id=run_event.run_id, session_id=run_event.session_id, requirements=run_event.requirements, stream=True, user_id=user_id, dependencies=session_state, ) await pprint.apprint_run_response(response)Agent Configuration (if applicable)
No response
Expected Behavior
in group_del_stock, dependencies["user_token"] or session_state["user_token"] should be not None
Actual Behavior
dependencies["user_token"] or session_state["user_token"] is None
Screenshots or Logs (if applicable)
No response
Environment
agno=2.6.9Possible Solutions (optional)
in _aroute_requirements_to_members_stream, The run_context has not been restored https://github.com/agno-agi/agno/blob/55ba3edd1b4ea230afe8a3fcda39582f8a2178ad/libs/agno/agno/team/_run.py#L5145
- Assistant
I'll start by exploring the repository structure to understand the codebase and locate the relevant files. Let's first see what's in the root directory.
- Assistant
Let me understand the repository structure better.
- Assistant
Let me look at the libs directory structure since the issue points to
libs/agno/agno/team/_run.py. - Assistant
Let me look at the specific file mentioned in the issue.
- Assistant
Let me look at the issue more carefully. The problem is that when
acontinue_runis called, thedependenciesorsession_stateis lost. The issue author points to_aroute_requirements_to_members_streamfunction around line 5145 in_run.py.Let me examine that area of the code.
- Assistant
Let me understand the issue better. The problem is that
team.acontinue_runloses dependencies or session_state. The user passessession_stateasdependenciesparameter, but thegroup_del_stockfunction doesn't get thesession_stateinrun_context.session_state.Let me look at the
acontinue_runmethod and the relevant parts of the code to understand how dependencies and session_state flow. - Assistant
Let me look at the
acontinue_runmethod in the Team class to understand how it's called. - Assistant
Let me also check the team.py file for the acontinue_run method to understand its signature.
- Assistant
Now let me look at the
acontinue_run_dispatchfunction to understand how dependencies are passed through. - Assistant
Let me look at
_acontinue_run_streamto see how it handles therun_context. - Assistant
Let me search for how
session_stateis handled inacontinue_runfunctions. - Assistant
Let me look at the
_acontinue_run_streamfunction more closely, particularly where it sets up the session and how it handlessession_state. - Assistant
Let me look at the
_asetup_sessionfunction to see how it handlessession_state. - Assistant
Let me look at the
_asetup_sessionfunction to understand how it handlessession_state. - Assistant
Now let me check the
_load_session_statefunction to understand how session state is loaded. - Assistant
Let me check the
_load_session_statefunction in_storage.py. - Assistant
Let me look at the
_aroute_requirements_to_members_streamfunction more closely to see what context is passed to member agents. - Assistant
Now I can see the issue! Looking at lines 5171-5187, when
_aroute_requirements_to_members_streamcallsmember.acontinue_run, it's not passingdependenciesorrun_contextparameters. This means the member agent won't have access to the session state or dependencies.Let me also check the non-streaming version
_aroute_requirements_to_membersto see if it has the same issue. - Assistant
Let me look at
_aroute_requirements_to_membersas well. - Assistant
Now I see the issue more clearly. In both
_aroute_requirements_to_membersand_aroute_requirements_to_members_stream, when callingmember.acontinue_run(), thedependenciesandrun_contextparameters are not being passed. This means the member agents lose access to the dependencies and session_state that were provided by the user.The fix should be to pass
dependencies(fromrun_context.dependencies) and possiblyrun_contextto the member'sacontinue_run()calls.Let me also check how the initial
aruncall works to understand how dependencies and session_state are passed. - Assistant
Let me check how
arunis called in the codebase to understand how dependencies and session_state are passed. - Assistant
Let me check how the initial
arunpasses dependencies/session_state to member agents. Let me look atadelegate_task_to_memberor similar functions. - Assistant
Let me search for the delegate task function in the team.py file.
- Assistant
Now let me look at how
arun_dispatchsets up run_context, to see how session_state is initially set: - Assistant
Let me look at the arun_dispatch function.
- Assistant
Let me continue reading the
arun_dispatchfunction to see howsession_stateis initialized. - Assistant
Let me now check the Agent's
acontinue_runmethod to see what parameters it accepts. - Assistant
Let me look at the agent's
acontinue_runmethod to see if it acceptssession_stateordependencies. - Assistant
Now let me look at the Agent's
acontinue_run_dispatchfunction to see how it handlesdependenciesandrun_context. - Assistant
Let me continue reading the
acontinue_run_dispatchfunction to see how it handlesdependenciesandrun_context. - Assistant
Let me look at the
_aroute_requirements_to_members_streamfunction more closely to understand what needs to be passed through to member agents. I need to check howdependenciesandsession_stateflow through to member agent calls.Looking at the issue, the problem is that when
_aroute_requirements_to_members_streamcallsmember.acontinue_run(), it doesn't passdependenciesfrom the parent team'srun_context. This means the member agent doesn't have access to the dependencies or session state that the user provided. - Assistant
Now let me check the sync versions of the routing functions to see if they have the same issue.
- Assistant
Let me check the sync version
_route_requirements_to_members_streamto see if it has the same issue. - Assistant
Now let me check the sync non-streaming version
_route_requirements_to_members. - Assistant
Let me check how the routing functions are called from the
acontinue_runand_acontinue_run_streamfunctions to see whatrun_contextis passed. - Assistant
Let me also check how
_asetup_sessionis called in the_acontinue_run_streamto understand the flow. - Assistant
Now let me also look at the
_acontinue_runfunction (non-streaming) to see how it calls the routing function. - Assistant
Now I understand the issue. Let me also look at the
RunContextclass to understand its structure: - Assistant
Let me look at the RunContext class.
- Assistant
Now I have a clear understanding of the issue. The problem is that when
_aroute_requirements_to_members_stream(and_aroute_requirements_to_members) callsmember.acontinue_run(), it doesn't passrun_contextordependencies. This means the member agent creates its own freshRunContextwith emptysession_stateand nodependencies, losing the original context.Let me first create a reproduction script to confirm the issue, then fix it. Let me first explore the repository structure more:
- Assistant
Let me also look at the tests for session_state and dependencies to understand the expected behavior:
- Assistant
Let me also look at some relevant tests:
- Assistant
Now I have a clear understanding of the issue. Let me check what the
_aroute_requirements_to_members_streamand_aroute_requirements_to_membersfunctions look like and how they should passrun_contextto member agents.Let me also look at the sync
_route_requirements_to_membersand_route_requirements_to_members_streamfunctions: - Assistant
Now I have a clear understanding of the issue. The problem is that when the team routes member requirements in
_aroute_requirements_to_members_streamand_aroute_requirements_to_members, themember.acontinue_run()calls don't pass therun_context(which containsdependenciesandsession_state). This means the member agent creates a freshRunContextwith emptysession_state={}and nodependencies.Let me now look at the
_aroute_requirements_to_members_streamfunction signature to understand what needs to be changed, and also examine howadelegate_task_to_member(which works correctly) handles the run_context: - Assistant
Let me search for how member agents are called with run_context in other parts of the code to understand the pattern.
- Assistant
Let me search for how delegate_task_to_member passes run_context to the member agent:
- Assistant
Let me search for delegate_task in the team directory.
- Assistant
Let me look at how
adelegate_task_to_memberpassesrun_contextto the member agent. - Assistant
Now I can see the pattern! In
adelegate_task_to_member(line 704-725), the member agent is called with:dependencies=run_context.dependenciessession_state=member_session_state_copy(a copy ofrun_context.session_state)metadata=run_context.metadataknowledge_filters=run_context.knowledge_filters
But in
_aroute_requirements_to_members_streamand_aroute_requirements_to_members, the member agent'sacontinue_runis called WITHOUT passing these parameters. This is the bug!The fix is to pass the
run_context(or at leastdependencies) to the member'sacontinue_runcalls in all 4 routing functions:_route_requirements_to_members(sync, non-streaming) - line 4870, 4877_route_requirements_to_members_stream(sync, streaming) - line 4961, 4970_aroute_requirements_to_members(async, non-streaming) - line 5069, 5075_aroute_requirements_to_members_stream(async, streaming) - line 5171, 5180
Let me also check if the agent's / accepts a parameter, so we can pass it directly:
- Assistant
Let me check the agent's continue_run signature.
- Assistant
Now I have a clear understanding of the issue. Let me implement the fix. The problem is in 4 routing functions where
member.continue_run()/member.acontinue_run()doesn't passrun_contextfrom the parent team. I need to addrun_contextto all the member agent calls.Let me start with the fix for
_aroute_requirements_to_members_stream: - Assistant
Now I understand the issue completely. Let me trace through the flow:
- User calls
team.acontinue_run()withdependencies=session_state acontinue_run_dispatchcreates aRunContextwithsession_state={}anddependencies=opts.dependencies- The
run_contextis passed to_acontinue_run_stream _acontinue_run_streamcalls_aroute_requirements_to_members_streamwithrun_context- But
_aroute_requirements_to_members_streamdoesn't passrun_contextordependenciestomember.acontinue_run() - So the member agent creates its own fresh
RunContextwithsession_state={}and nodependencies
The fix is to pass
run_context(or at leastdependencies) to the member agent'scontinue_run/acontinue_runcalls in all four routing functions.Now let me implement the fix:
- User calls
- Assistant
Now let me fix the second call in the same function (the
elsebranch): - Assistant
Good. Now let me fix the async non-streaming version
_aroute_requirements_to_members: - Assistant
Now let me fix the
_aroute_requirements_to_membersfunction (async, non-streaming) - lines 5069-5079: - Assistant
Good. Now let me fix the sync versions as well. Let me start with
_route_requirements_to_members: - Assistant
Now let me fix the sync versions too. Let me first fix
_route_requirements_to_members: - Assistant
Now let me fix the sync streaming version
_route_requirements_to_members_stream: - Assistant
Now let me fix the sync streaming version
_route_requirements_to_members_stream: - Assistant
All four routing functions have been updated. Now let me verify the changes by running the existing tests. Let me first check if there are any unit tests related to the continue_run requirements:
- Assistant
Good, the existing tests pass. Let me see the rest of the test output.
- Assistant
All 49 tests pass. Let me also run the other relevant unit tests:
- Assistant
All tests pass. Now let me also check if there are any unit tests specifically related to the team's continue_run routing functions to make sure my changes work correctly:
- Assistant
Wait, I need to reconsider. Passing
run_contextdirectly to the member agent might cause issues because therun_contextcontains the team'srun_idandsession_id, which would be wrong for the member agent. Let me check how the agent'sacontinue_run_dispatchusesrun_context: - Assistant
Looking at the agent's
acontinue_run_dispatch, I see that ifrun_contextis passed, it's used as-is. However, passing the team'srun_contextdirectly might cause issues because:- The team's
run_contexthasrun_idandsession_idfor the team, not the member - But the important part (
dependenciesandsession_state) would be propagated
Actually, I realize I should be more careful. Let me check: does passing
run_contextANDdependenciesat the same time work correctly? Looking at line 3659-3674, ifrun_contextis provided, it's used, thenopts.apply_to_contextapplies dependencies ifdependencies is not None.But the key concern is: will the
run_contextfrom the team have the correctsession_stateafter_asetup_sessionis called? Let me trace through: - The team's
- Assistant
There's no
_asetup_sessionin the agent's_run.py. Let me search for how session state is handled in the agent'sacontinue_run: - Assistant
- Assistant
Now let me re-check the flow more carefully. Looking at the agent's
acontinue_run_dispatch:# Line 3620-3621 session_id = run_response.session_id if run_response else session_id run_id: str = run_response.run_id if run_response else run_id # Line 3659 run_context = run_context or RunContext( run_id=run_id, session_id=session_id, ... )If
run_contextis passed, it's used as-is. Therun_idandsession_idfrom the explicit parameters are NOT used in the RunContext. However, they ARE used for_initialize_sessionand other operations.Actually, I think passing
run_contextdirectly might cause issues because therun_context.run_idwould be the team's run_id, not the member's. Let me check ifrun_context.run_idis used in the agent's_acontinue_run_stream: - Assistant
I see!
run_context.run_idis used in many places in the agent's code (likeregister_run(run_context.run_id)). Passing the team'srun_contextwith the team'srun_idwould cause the member agent to register and use the wrong run ID. This would be a bug.I need to revert my approach and instead only pass
dependencies(notrun_context). Let me update all four routing functions: - Assistant
Good. Now let me update
_aroute_requirements_to_members(async non-streaming): - Assistant
I see there's a problem - it duplicated some code. Let me check the current state:
- Assistant
I see the issue - my edit created duplicate lines in the
elseblock. Let me fix this: - Assistant
I see there's an issue - the original code had a different structure. Let me check the original code and fix this properly:
- Assistant
I see the issue! My last edit accidentally removed the
if member_run_output is None:block in_aroute_requirements_to_members. Let me fix this: - Assistant
The edit created a malformed section. Let me fix the broken part - I see
run_response=member_run_outputwithout the function call prefix: - Assistant
Good, the async non-streaming version is fixed. Now let me also remove
run_context=run_contextfrom the sync versions and only passdependencies: - Assistant
I see there's still
run_context=run_contextin line 4882. Let me fix that: - Assistant
Good. Now let me fix the sync streaming version
_route_requirements_to_members_stream- I need to removerun_context=run_context: - Assistant
I see there's a duplicate in the sync streaming version. Let me fix it:
- Assistant
Now let me verify the full diff to make sure everything is correct:
- Assistant
I see there's one issue - I accidentally removed the comment "# Iterate the member's streaming response — yield intermediate events," from
_route_requirements_to_members_stream. Let me fix that: - Assistant
Wait, I notice that the
elseblock at line 4980 is missingdependencies. Let me check: - Assistant
I see the issue - in the
elseblock of_route_requirements_to_members_stream, thedependenciesparameter is missing. Let me fix this: - Assistant
Good. Now let me check the full diff to make sure all changes are correct:
- Assistant
The diff looks clean now. Let me run the tests again to make sure everything passes:
- Assistant
All 49 tests pass. Let me also run the other team-related unit tests:
- Assistant
The same 3 failures are pre-existing (SQLite import and skills path issues). All 411 other tests pass. Let me verify these failures were pre-existing by checking with git stash:
- Assistant
Good, the 3 failures are pre-existing. Let me restore my changes:
- Assistant
Now let me review the final diff one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.