refactor: removes the use of contextvars and refactors the codebase to explicitly pass user_id
#552
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.
Purpose
This pull request removes the use of
contextvarsfor tracking the current user ID throughout the backend orchestration and agent code. Instead, it refactors the codebase to explicitly passuser_idas a function argument or class field wherever needed. This change improves clarity, reduces hidden dependencies, and makes the user context handling more robust and maintainable.Refactoring user context handling:
contextvarsand thecurrent_user_idvariable fromsettings.py,router.py, agent, and orchestration modules, replacing them with explicituser_idparameters and fields. [1] [2] [3] [4] [5] [6] [7]user_idas an argument, including agent creation (create_agent_from_config,get_agents), agent invocation, and orchestration manager initialization. [1] [2] [3]ProxyAgentandHumanApprovalMagenticManagerclasses to store and useuser_iddirectly, removing reliance on global context. [1] [2]WebSocket connection and messaging updates:
process_idand explicituser_idparameters instead of context-based retrieval. [1] [2]user_idrather than fetching from context, ensuring correct routing of messages to users. [1] [2] [3] [4] [5]Background orchestration and agent initialization:
user_iddirectly to orchestration routines and agent factories. [1] [2] [3] [4]These changes make the user context handling explicit and more reliable, reducing the risk of subtle bugs and improving code readability.
Does this introduce a breaking change?
How to Test
What to Check
Verify that the following are valid
Other Information