Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): reject Bash/PowerShell timeout below 1ms (#3309)
Li-john10219fb9abc58f6a
diff --git a/src/agentscope/tool/_builtin/_bash.py b/src/agentscope/tool/_builtin/_bash.pyindex c306c7dfe..e3c5e9836 100644--- a/src/agentscope/tool/_builtin/_bash.py+++ b/src/agentscope/tool/_builtin/_bash.py@@ -122,7 +122,7 @@ easier to review tool calls and give permission.),"default": 120000,"maximum": 600000,- "minimum": 0,+ "minimum": 1,},},"required": ["command"],@@ -722,6 +722,21 @@ easier to review tool calls and give permission.ToolChunk: The tool execution result with stdout/stderr content."""+ if timeout < 1:+ yield ToolChunk(+ content=[+ TextBlock(+ text=(+ f"Error: timeout must be at least 1ms, "+ f"got: {timeout}."+ ),+ ),+ ],+ state=ToolResultState.ERROR,+ is_last=True,+ )+ return+# Clamp timeout to max 600000ms and convert to secondstimeout_ms = min(timeout, 600000)timeout_sec = timeout_ms / 1000.0diff --git a/src/agentscope/tool/_builtin/_powershell.py b/src/agentscope/tool/_builtin/_powershell.pyindex 33ec8d39a..0ae588eaf 100644--- a/src/agentscope/tool/_builtin/_powershell.py+++ b/src/agentscope/tool/_builtin/_powershell.py@@ -80,7 +80,7 @@ are easier for the user to review and authorize:),"default": 120000,"maximum": 600000,- "minimum": 0,+ "minimum": 1,},},"required": ["command"],@@ -186,6 +186,21 @@ are easier for the user to review and authorize:`ToolChunk`:A final chunk containing the command output."""+ if timeout < 1:+ yield ToolChunk(+ content=[+ TextBlock(+ text=(+ f"Error: timeout must be at least 1ms, "+ f"got: {timeout}."+ ),+ ),+ ],+ state=ToolResultState.ERROR,+ is_last=True,+ )+ return+timeout_ms = min(timeout, 600000)encoded_user_command = base64.b64encode(command.encode("utf-16-le"),diff --git a/tests/builtin_bash_test.py b/tests/builtin_bash_test.pyindex 101f07d1e..727f42ce4 100644--- a/tests/builtin_bash_test.py+++ b/tests/builtin_bash_test.py@@ -218,6 +218,52 @@ class BashToolTest(IsolatedAsyncioTestCase):self.assertEqual(chunks[0].state, "error")self.assertIn("timed out", chunks[0].content[0].text.lower())+ async def test_invalid_timeout_rejected(self) -> None:+ """Reject nonpositive timeouts without calling the backend."""+ for timeout in (0, -1):+ with self.subTest(timeout=timeout):+ backend = AsyncMock()+ tool = Bash(backend=backend)+ chunks = [+ chunk.model_dump(+ exclude={+ "id": True,+ "content": {"__all__": {"id", "created_at"}},+ },+ )+ async for chunk in tool.call(+ command='echo "hello world"',+ timeout=timeout,+ )+ ]++ self.assertEqual(+ chunks,+ [+ {+ "content": [+ {+ "type": "text",+ "text": (+ "Error: timeout must be at least "+ f"1ms, got: {timeout}."+ ),+ "finished_at": None,+ },+ ],+ "state": "error",+ "is_last": True,+ "metadata": {},+ },+ ],+ )+ backend.exec_shell.assert_not_awaited()++ async def test_timeout_schema_minimum_is_one(self) -> None:+ """JSON schema minimum for timeout is 1ms (#3307)."""+ timeout_schema = self.bash_tool.input_schema["properties"]["timeout"]+ self.assertEqual(timeout_schema["minimum"], 1)+@unittest.skipIf(sys.platform == "win32",diff --git a/tests/builtin_powershell_test.py b/tests/builtin_powershell_test.pyindex 48b93756e..a30cf3660 100644--- a/tests/builtin_powershell_test.py+++ b/tests/builtin_powershell_test.py@@ -25,6 +25,10 @@ class PowerShellInterfaceTest(IsolatedAsyncioTestCase):self.assertFalse(tool.is_concurrency_safe)self.assertIn("command", tool.input_schema["properties"])self.assertEqual(tool.input_schema["required"], ["command"])+ self.assertEqual(+ tool.input_schema["properties"]["timeout"]["minimum"],+ 1,+ )self.assertEqual(tool._cwd, "workspace")self.assertIs(tool._backend, backend)self.assertIn("Glob", tool.description)@@ -314,6 +318,53 @@ class PowerShellExecutionTest(IsolatedAsyncioTestCase):600.0,)+ async def test_invalid_timeout_rejected(self) -> None:+ """Reject nonpositive timeouts without calling the backend."""+ for timeout in (0, -5):+ with self.subTest(timeout=timeout):+ backend = AsyncMock()+ tool = PowerShell(backend=backend)+ chunks = [+ chunk.model_dump(+ exclude={+ "id": True,+ "content": {"__all__": {"id", "created_at"}},+ },+ )+ async for chunk in tool.call(+ command="Get-Location",+ timeout=timeout,+ )+ ]++ self.assertEqual(+ chunks,+ [+ {+ "content": [+ {+ "type": "text",+ "text": (+ "Error: timeout must be at least "+ f"1ms, got: {timeout}."+ ),+ "finished_at": None,+ },+ ],+ "state": "error",+ "is_last": True,+ "metadata": {},+ },+ ],+ )+ backend.exec_shell.assert_not_awaited()++ async def test_timeout_schema_minimum_is_one(self) -> None:+ """JSON schema minimum for timeout is 1ms (#3307)."""+ tool = PowerShell(backend=AsyncMock())+ timeout_schema = tool.input_schema["properties"]["timeout"]+ self.assertEqual(timeout_schema["minimum"], 1)+@unittest.skipUnless(sys.platform == "win32",