fix: Resolve RAI reuse bug (#28329)#737
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request enhances agent lifecycle management by improving agent ID resolution and reuse, particularly for RAI agents, while refactoring the initialization flow to consistently handle project_client throughout the codebase.
Key changes:
- Introduced a new
resolve_agent_idmethod that prioritizes the Projects SDK for RAI agents with fallback to AgentsClient, including comprehensive logging - Refactored agent initialization to pass
project_clientas a parameter, eliminating duplicate retrievals and ensuring consistency - Enhanced the save logic to check for ID changes before writing to the database, reducing unnecessary operations
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/backend/v4/magentic_agents/foundry_agent.py | Moves project_client initialization before super().__init__() and passes it to parent, removes conditional save logic from _after_open, adds save call during streaming in invoke method |
| src/backend/v4/magentic_agents/common/lifecycle.py | Adds project_client parameter to constructors, implements resolve_agent_id method for dual-client agent resolution, refactors get_database_team_agent to use new resolution logic with client preference based on agent type, enhances save_database_team_agent with ID change detection |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+303
to
+304
| if self._agent.chat_client.agent_id: | ||
| await self.save_database_team_agent() |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Roopan-Microsoft
approved these changes
Dec 23, 2025
|
🎉 This PR is included in version 4.0.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
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.
Purpose
This pull request enhances the agent lifecycle management by improving how agent IDs are resolved and reused, ensuring better integration with both the Projects SDK and AgentsClient. It also introduces more robust logic for saving and restoring agent state, and refactors how the
project_clientis handled throughout the agent initialization process.Agent ID resolution and reuse improvements:
resolve_agent_idasync method to prefer resolving agent IDs via the Projects SDK (for RAI agents) with a fallback to the AgentsClient, including detailed logging for success and failure cases.get_database_team_agentmethod to useresolve_agent_id, improving reliability when restoring agents and ensuring the correct client is used based on agent type.Agent state saving enhancements:
save_database_team_agentto only save when the agent ID has changed, reducing unnecessary writes and adding more informative logging.Refactoring and initialization changes:
project_clientparameter, ensuring it is available where needed and removing duplicate retrievals. [1] [2] [3] [4] [5]Does this introduce a breaking change?
How to Test
What to Check
Verify that the following are valid
Other Information