mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-07-28 18:19:22 +00:00
Some checks failed
* feat(tools): add WaitTool for pausing on long-running jobs Agents that kick off out-of-band work (a sandbox build, a deployment, an async API job) have no way to let clock time pass: they either poll in a tight loop or give up before the work finishes. WaitTool pauses for a given number of seconds, with an optional reason echoed back for traces. A single call waits at most max_seconds (default 300, configurable). Longer requests are clamped to the cap and the result says so, so the model calls again rather than failing. Sync and async execution are both implemented; stdlib only, no new dependencies. The tool description spells out when to reach for it (builds, deploys, batch jobs, async polling, backoff) and when not to, so models pick it up for the right reason. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(tools): enforce non-negative wait on positional calls, fix doc snippets BaseTool.run() skips args_schema validation when called with positional arguments, so tool.run(-5) reached time.sleep(-5) and failed with an unrelated error. _resolve_duration now enforces the seconds >= 0 contract itself, covered for both run() and arun(). Docs and README examples are now self-contained: check_build_status_tool is defined with the @tool decorator instead of referenced out of nowhere, and the async example awaits inside asyncio.run() rather than at top level. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * docs: point the wait tool card at the edge path Unprefixed links resolve against the default docs version (v1.15.7), where the wait tool page does not exist, so the card 404'd in the broken link check. Prefixing with /edge matches how other edge pages link. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(tools): never cache waits and keep the advertised cap accurate Two issues from review, both confirmed against the code. Waits inherited the default cache_function, which always allows caching. With crew cache enabled, a repeat call with the same arguments returned "Waited N seconds." straight from the cache without sleeping, turning a poll-wait-check loop into a busy loop. WaitTool now declares a cache_function that always refuses. The description advertising the cap was only rebuilt when max_seconds reached __init__ without an explicit description. Passing both (as a platform building from tool.specs.json init params would), calling model_validate, or assigning max_seconds left the text claiming 300 seconds while clamping to something else. A model_validator now derives the description from max_seconds on construction, validation, and assignment, and leaves a caller-supplied description untouched. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(tools): reject NaN waits and pluralize single-second results _resolve_duration now rejects NaN with its own message instead of letting time.sleep raise "Invalid value NaN (not a number)" from a positional call. Infinity keeps clamping to the cap like any other oversized wait. Result and description text no longer says "1 seconds". Tests use the public WaitTool().description as the baseline rather than reaching for module-private helpers. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
182 lines
5.6 KiB
Python
182 lines
5.6 KiB
Python
from unittest.mock import AsyncMock, patch
|
|
|
|
from crewai_tools.tools.wait_tool import WaitTool
|
|
from pydantic import ValidationError
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def tool():
|
|
return WaitTool()
|
|
|
|
|
|
@patch("crewai_tools.tools.wait_tool.wait_tool.time.sleep")
|
|
def test_waits_requested_duration(mock_sleep, tool):
|
|
result = tool.run(seconds=2)
|
|
|
|
mock_sleep.assert_called_once_with(2)
|
|
assert "Waited 2 seconds." in result
|
|
assert "capped" not in result
|
|
|
|
|
|
@patch("crewai_tools.tools.wait_tool.wait_tool.time.sleep")
|
|
def test_caps_long_waits(mock_sleep, tool):
|
|
result = tool.run(seconds=3600)
|
|
|
|
mock_sleep.assert_called_once_with(300)
|
|
assert "Waited 300 seconds." in result
|
|
assert "Requested 3600 seconds, capped at 300 seconds per call" in result
|
|
assert "call this tool again" in result
|
|
|
|
|
|
@patch("crewai_tools.tools.wait_tool.wait_tool.time.sleep")
|
|
def test_custom_max_seconds(mock_sleep):
|
|
tool = WaitTool(max_seconds=10)
|
|
|
|
result = tool.run(seconds=45)
|
|
|
|
mock_sleep.assert_called_once_with(10)
|
|
assert "Waited 10 seconds." in result
|
|
assert "10 seconds" in tool.description
|
|
|
|
|
|
@patch("crewai_tools.tools.wait_tool.wait_tool.time.sleep")
|
|
def test_reason_is_echoed_back(mock_sleep, tool):
|
|
result = tool.run(seconds=1, reason="sandbox build running")
|
|
|
|
assert "Reason: sandbox build running" in result
|
|
|
|
|
|
def test_zero_seconds_is_allowed(tool):
|
|
assert "Waited 0 seconds." in tool.run(seconds=0)
|
|
|
|
|
|
def test_negative_seconds_is_rejected(tool):
|
|
with pytest.raises(ValueError, match="greater than or equal to 0"):
|
|
tool.run(seconds=-5)
|
|
|
|
|
|
@patch("crewai_tools.tools.wait_tool.wait_tool.time.sleep")
|
|
def test_negative_seconds_is_rejected_when_passed_positionally(mock_sleep, tool):
|
|
# BaseTool.run() skips args_schema validation for positional arguments.
|
|
with pytest.raises(ValueError, match="seconds must be zero or greater"):
|
|
tool.run(-5)
|
|
|
|
mock_sleep.assert_not_called()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_negative_seconds_is_rejected_when_passed_positionally(tool):
|
|
with patch(
|
|
"crewai_tools.tools.wait_tool.wait_tool.asyncio.sleep", new_callable=AsyncMock
|
|
) as mock_sleep:
|
|
with pytest.raises(ValueError, match="seconds must be zero or greater"):
|
|
await tool.arun(-5)
|
|
|
|
mock_sleep.assert_not_awaited()
|
|
|
|
|
|
@patch("crewai_tools.tools.wait_tool.wait_tool.time.sleep")
|
|
def test_nan_seconds_is_rejected_when_passed_positionally(mock_sleep, tool):
|
|
with pytest.raises(ValueError, match="seconds must be a number"):
|
|
tool.run(float("nan"))
|
|
|
|
mock_sleep.assert_not_called()
|
|
|
|
|
|
@patch("crewai_tools.tools.wait_tool.wait_tool.time.sleep")
|
|
def test_infinite_seconds_is_capped_like_any_other_long_wait(mock_sleep, tool):
|
|
result = tool.run(float("inf"))
|
|
|
|
mock_sleep.assert_called_once_with(300)
|
|
assert "Waited 300 seconds." in result
|
|
|
|
|
|
@patch("crewai_tools.tools.wait_tool.wait_tool.time.sleep")
|
|
def test_singular_second_is_not_pluralized(mock_sleep):
|
|
tool = WaitTool(max_seconds=1)
|
|
|
|
assert "Waited 1 second." in tool.run(seconds=1)
|
|
assert "capped at 1 second per call" in tool.run(seconds=5)
|
|
assert "at most 1 second." in tool.description
|
|
|
|
|
|
def test_invalid_max_seconds_is_rejected():
|
|
with pytest.raises(ValidationError):
|
|
WaitTool(max_seconds=0)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_wait_caps_long_waits(tool):
|
|
with patch(
|
|
"crewai_tools.tools.wait_tool.wait_tool.asyncio.sleep", new_callable=AsyncMock
|
|
) as mock_sleep:
|
|
result = await tool.arun(seconds=3600, reason="deployment")
|
|
|
|
mock_sleep.assert_awaited_once_with(300)
|
|
assert "Waited 300 seconds." in result
|
|
assert "Reason: deployment" in result
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"build",
|
|
[
|
|
pytest.param(lambda d: WaitTool(max_seconds=10), id="init"),
|
|
pytest.param(lambda d: WaitTool(10), id="init_positional"),
|
|
pytest.param(lambda d: WaitTool(max_seconds=10, description=d), id="init_desc"),
|
|
pytest.param(
|
|
lambda d: WaitTool.model_validate({"max_seconds": 10, "description": d}),
|
|
id="model_validate",
|
|
),
|
|
pytest.param(
|
|
lambda d: WaitTool.model_validate(WaitTool(max_seconds=10).model_dump()),
|
|
id="round_trip",
|
|
),
|
|
],
|
|
)
|
|
def test_advertised_cap_matches_enforced_cap(build):
|
|
tool = build(WaitTool().description)
|
|
|
|
assert tool.max_seconds == 10
|
|
assert "at most 10 seconds" in tool.description
|
|
assert "300 seconds" not in tool.description
|
|
|
|
|
|
def test_advertised_cap_follows_assignment():
|
|
tool = WaitTool()
|
|
tool.max_seconds = 10
|
|
|
|
assert "at most 10 seconds" in tool.description
|
|
|
|
|
|
def test_caller_supplied_description_is_left_alone():
|
|
tool = WaitTool(max_seconds=10, description="Hold on for a bit.")
|
|
|
|
assert tool.description == "Hold on for a bit."
|
|
|
|
|
|
def test_waits_are_never_cached():
|
|
# A cache hit would hand back "Waited N seconds." without any time passing,
|
|
# turning a poll-wait-check loop into a busy loop.
|
|
tool = WaitTool()
|
|
|
|
assert tool.cache_function("{}", "Waited 1 second.") is False
|
|
assert WaitTool.model_validate(tool.model_dump()).cache_function() is False
|
|
|
|
|
|
def test_description_explains_when_to_use_it(tool):
|
|
description = tool.description.lower()
|
|
|
|
assert "sandbox" in description
|
|
assert "deployment" in description
|
|
assert "300 seconds" in description
|
|
assert "call this tool again" in description
|
|
|
|
|
|
def test_exported_from_package():
|
|
from crewai_tools import WaitTool as ExportedWaitTool
|
|
from crewai_tools.tools import WaitTool as ToolsExportedWaitTool
|
|
|
|
assert ExportedWaitTool is WaitTool
|
|
assert ToolsExportedWaitTool is WaitTool
|