Skip to content

Conversation

@Dhruvkumar-Microsoft
Copy link
Contributor

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

  • Added a new global AgentRegistry class in v3/config/agent_registry.py that tracks all agent instances, provides registration/unregistration methods, and performs asynchronous cleanup of agents during application shutdown. This registry uses a thread-safe WeakSet to 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))
  • Integrated the agent registry into the FastAPI app lifecycle by adding a lifespan handler in app_kernel.py that 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

  • Updated agent creation logic in utils_kernel.py, foundry_agent.py, and reasoning_agent.py to 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))
  • Enhanced the agent close() method in lifecycle.py to 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

  • Added logic in foundry_agent.py to 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

  • Updated imports throughout the codebase to use the new agent_registry instance, 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?

  • Yes
  • No

How to Test

  • Get the code
git clone [repo-address]
cd [repo-name]
git checkout [branch-name]
npm install
  • Test the code

What to Check

Verify that the following are valid

  • ...

Other Information

@Roopan-Microsoft Roopan-Microsoft merged commit ddf8c44 into dev-v3 Oct 8, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants