Skip to content

Conversation

bhouston
Copy link
Member

Incremental Resource Cleanup for Agent Lifecycle Management

Changes

This PR implements incremental resource cleanup that's tied to agent lifecycles:

  1. Added a cleanup method to the BackgroundTools class that:

    • Closes browser sessions associated with the agent
    • Terminates shell processes associated with the agent
    • Cleans up sub-agents associated with the agent
  2. The cleanup method is now called:

    • When an agent completes its task successfully
    • When an agent encounters an exception
    • When an agent is explicitly terminated
  3. Enhanced the global cleanup to first attempt to clean up any still-running agents before falling back to the direct resource cleanup

  4. Added comprehensive tests for the new cleanup functionality

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

Testing

  • Added a new test file backgroundTools.cleanup.test.ts that tests various cleanup scenarios
  • All existing tests pass with the new changes

Fixes #236

This commit adds a new cleanup method to the BackgroundTools class that handles
cleaning up browser sessions, shell processes, and sub-agents associated with an
agent when it completes its task, encounters an error, or is terminated.

The changes include:

- Adding a cleanup method to BackgroundTools that cleans up resources
- Calling cleanup when agents complete successfully
- Calling cleanup when agents encounter errors
- Calling cleanup when agents are terminated
- Enhancing global cleanup to first attempt to clean up any still-running agents
- Adding tests for the new cleanup functionality

Fixes #236
This change improves the resource cleanup process by handling browser sessions,
shell processes, and sub-agents in parallel rather than sequentially.

The implementation:

1. Refactors the cleanup method into smaller helper methods for each resource type
2. Uses Promise.all to execute cleanup operations concurrently
3. Filters tools by status during the initial grouping to simplify the code

This approach significantly speeds up the cleanup process, especially when
dealing with multiple resources of different types.
@bhouston bhouston merged commit 92a5f88 into main Mar 12, 2025
1 check passed
Copy link

🎉 This PR is included in version mycoder-agent-v1.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link

🎉 This PR is included in version mycoder-v1.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Incremental Resource Cleanup for Agent Lifecycle Management

1 participant