fix: added the lifespan of the agent on container stop and checked the already existed agents are not using the previous resource group connection #554
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 introduces a global agent registry to manage the lifecycle of agent instances across the application, ensuring proper cleanup and resource management. The registry tracks agents, registers them when created, and cleans them up on application shutdown. Additionally, agent registration and cleanup logic is integrated into various agent classes and utilities, and connection compatibility is checked when reusing agent definitions.
Agent Registry and Lifecycle Management
AgentRegistryclass inv3/config/agent_registry.pythat tracks all agent instances, provides registration/unregistration methods, and performs asynchronous cleanup of agents during application shutdown. This registry uses a thread-safeWeakSetto avoid memory leaks and includes detailed logging for monitoring. ([src/backend/v3/config/agent_registry.pyR1-R140](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-a4308472fd2dd927edd62e6f62e6108393682875d40f751ce305fc36d61177caR1-R140))app_kernel.pythat triggers cleanup of all agents when the application shuts down. ([[1]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-c839d97b372f2799977f9f197d6c18667f795b2566b1b2c42e27b1479be26173R22-R50),[[2]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-c839d97b372f2799977f9f197d6c18667f795b2566b1b2c42e27b1479be26173L49-R81))Agent Registration and Cleanup Integration
utils_kernel.py,foundry_agent.py, andreasoning_agent.pyto register agents with the global registry upon creation, and log warnings if registration fails. ([[1]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-6d00738f84112e258dc52a208861d569e01c874ad46c8e6ad73c3256faa85c0aR51-R56),[[2]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-42412c8627f3d628249c75eb4cc3d4e1308a00b359a30d7c2ad1398e51dabda6R185-R191),[[3]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-b5605d9d2b5b6405f4b00158b5c6aaeed27a02d84ee21ac4349876bfa241eac3R75-R81))close()method inlifecycle.pyto unregister itself from the registry and ensure Azure resources are cleaned up, with error handling for robustness. ([src/backend/v3/magentic_agents/common/lifecycle.pyR122-R152](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-d979ffe96d7931e935003b641456460177ded30a186f7fb56f645081f469be80R122-R152))Agent Definition and Connection Compatibility
foundry_agent.pyto check if an existing agent definition uses the same Azure AI Search connection as the current configuration, and delete/recreate the definition if incompatible. This prevents resource conflicts and ensures agents are correctly configured. ([[1]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-42412c8627f3d628249c75eb4cc3d4e1308a00b359a30d7c2ad1398e51dabda6R148-R156),[[2]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-42412c8627f3d628249c75eb4cc3d4e1308a00b359a30d7c2ad1398e51dabda6R226-R287))Imports and Code Organization
agent_registryinstance, ensuring all relevant modules can access the registry for agent tracking. ([[1]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-c839d97b372f2799977f9f197d6c18667f795b2566b1b2c42e27b1479be26173R4-R6),[[2]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-6d00738f84112e258dc52a208861d569e01c874ad46c8e6ad73c3256faa85c0aR10-R11),[[3]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-d979ffe96d7931e935003b641456460177ded30a186f7fb56f645081f469be80R9),[[4]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-42412c8627f3d628249c75eb4cc3d4e1308a00b359a30d7c2ad1398e51dabda6R11-R12),[[5]](https://github.com/microsoft/Multi-Agent-Custom-Automation-Engine-Solution-Accelerator/pull/554/files#diff-b5605d9d2b5b6405f4b00158b5c6aaeed27a02d84ee21ac4349876bfa241eac3R10))Does this introduce a breaking change?
How to Test
What to Check
Verify that the following are valid
Other Information