feat: Toolset Context Managers#228
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds automatic context manager support for Toolset classes with re-entrancy protection. Agents now automatically enter/exit tool context managers before execution using AsyncExitStack, preventing duplicate setup/teardown even when manually managed by users.
- Implemented
__init_subclass__hook inToolsetto wrap__aenter__/__aexit__(and sync variants) with re-entrancy protection - Modified
Agent.stream()to automatically enter context managers for all tools before execution - Fixed spelling error in Organization model documentation ("identifer" → "identifier")
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_agent_lifecycle.py | Comprehensive test suite covering async/sync context managers, re-entrancy, and return value handling |
| dreadnode/api/models.py | Fixed typo in Organization.key field documentation |
| dreadnode/agent/tools/base.py | Implemented re-entrancy wrapper logic in Toolset.init_subclass using reference counting |
| dreadnode/agent/agent.py | Added AsyncExitStack to automatically enter tool context managers in stream() method |
| docs/sdk/task.mdx | Fixed logic for output_object_hash initialization and conditional checks |
| docs/sdk/api.mdx | Updated Organization documentation to rename "slug" to "identifier" |
| docs/sdk/agent_tools.mdx | Added documentation for new async context management support |
Comments suppressed due to low confidence (1)
docs/sdk/api.mdx:1794
- The documentation refers to this field as "identifier" but the actual field name in the
Organizationmodel (line 504 indreadnode/api/models.py) is "key". The documentation should match the actual field name:
### key
```python
key: strURL-friendly identifier for the organization.
Unique identifier for the organization.
### is\_active
```python
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
mkultraWasHere
pushed a commit
that referenced
this pull request
Dec 3, 2025
* Add tool lifecycle management for agents * Fixing docs that broke during rebase * Some logic cleanup
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.
Added automatic context manager support for Toolset classes with re-entrancy protection. Agents now automatically enter/exit tool context managers before execution using AsyncExitStack, preventing duplicate setup/teardown even when manually managed by users.
Generated Summary:
This summary was generated with ❤️ by rigging