-
Notifications
You must be signed in to change notification settings - Fork 51
Closed
Labels
Description
Incremental Resource Cleanup for Agent Lifecycle Management
Problem
Currently, the system uses a single cleanupResources
function in packages/cli/src/utils/cleanup.ts
that attempts to clean up all browser sessions and shell processes at application exit. This approach has several limitations:
- It doesn't properly clean up resources when sub-agents complete their tasks
- Resources from terminated agents can linger until the application exits
- There's no robust handling for cleaning up resources when agents encounter exceptions
- The cleanup is not tied to the agent lifecycle, which can lead to resource leaks
Current Implementation
The current implementation:
- Stores browser sessions in a global singleton
BrowserManager
- Stores shell processes in a global
processStates
map - Tracks background tools (browsers, shells, sub-agents) in per-agent
BackgroundTools
instances - Has no mechanism to clean up resources when agents complete or fail
Proposed Solution
Implement incremental resource cleanup that's tied to agent lifecycles:
-
Add a
cleanup
method to theBackgroundTools
class that:- Closes browser sessions associated with the agent
- Terminates shell processes associated with the agent
- Cleans up sub-agents associated with the agent
-
Call this cleanup method:
- When an agent completes its task successfully
- When an agent encounters an exception
- When an agent is explicitly terminated
-
Enhance exception handling to ensure cleanup occurs even when agents fail
-
Keep the existing global cleanup as a fallback for application exit
This approach will ensure resources are cleaned up promptly when they're no longer needed, rather than waiting for application exit.
Benefits
- Reduced resource usage as agents complete their tasks
- More robust cleanup in the presence of exceptions
- Clearer ownership of resources by agents
- Improved reliability of cleanup operations