Skip to content

Conversation

@monoxgas
Copy link
Contributor

@monoxgas monoxgas commented Nov 18, 2025

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.

class AsyncCMToolSet(Toolset):
    """
    Scenario 1: A standard, async-native Toolset.
    Tests that __aenter__/__aexit__ are called correctly and only once.
    """

    enter_count: int = 0
    exit_count: int = 0

    async def __aenter__(self) -> "AsyncCMToolSet":
        event_log.append("async_tool_enter_start")
        await asyncio.sleep(0.01)  # Simulate async work
        self.enter_count += 1
        event_log.append("async_tool_enter_end")
        return self

    async def __aexit__(self, *args: object) -> None:
        event_log.append("async_tool_exit_start")
        await asyncio.sleep(0.01)
        self.exit_count += 1
        event_log.append("async_tool_exit_end")

    @tool_method
    async def do_work(self) -> str:
        """A sample method for the agent to call."""
        event_log.append("async_tool_method_called")
        return "async work done"

Generated Summary:

  • Added support for async context management in the Agent and Toolset classes, enhancing concurrency capabilities.
  • Implemented automatic re-entrancy handling for context managers in Toolset, ensuring safe entry/exit behavior.
  • Updated API documentation to replace deprecated "slug" field with a new "identifier" field for organization models.
  • Introduced comprehensive test suite for agent lifecycle management and toolset behavior, verifying context management and idempotency.
  • Minor documentation corrections in model descriptions for clarity.

This summary was generated with ❤️ by rigging

@monoxgas monoxgas requested a review from Copilot November 18, 2025 17:52
@dreadnode-renovate-bot dreadnode-renovate-bot bot added area/docs Changes to documentation and guides area/tests Changes to test files and testing infrastructure type/docs Documentation updates and improvements labels Nov 18, 2025
Copilot finished reviewing on behalf of monoxgas November 18, 2025 17:56
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in Toolset to 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 Organization model (line 504 in dreadnode/api/models.py) is "key". The documentation should match the actual field name:
### key

```python
key: str

URL-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.

@monoxgas monoxgas merged commit 729c1c0 into main Nov 21, 2025
8 checks passed
@monoxgas monoxgas deleted the feat/toolset-context-managers branch November 21, 2025 20:46
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/docs Changes to documentation and guides area/tests Changes to test files and testing infrastructure type/docs Documentation updates and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants