Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): reject invalid glob helper output (#3038)
SIcheng Han9c4d238825c7
diff --git a/src/agentscope/tool/_builtin/_glob.py b/src/agentscope/tool/_builtin/_glob.pyindex 88cfc7c24..6c5f9def6 100644--- a/src/agentscope/tool/_builtin/_glob.py+++ b/src/agentscope/tool/_builtin/_glob.py@@ -324,10 +324,24 @@ Use head_limit to cap the number of results returned.""" # ignore: E501try:matches = json.loads(- result.stdout.decode("utf-8", errors="replace"),+ result.stdout.decode("utf-8"),+ )+ except ValueError:+ matches = None++ if not isinstance(matches, list) or not all(+ isinstance(path, str) for path in matches+ ):+ return ToolChunk(+ content=[+ TextBlock(+ text="Glob helper returned invalid output: "+ "expected a JSON array of file paths.",+ ),+ ],+ state=ToolResultState.ERROR,+ is_last=True,)- except (json.JSONDecodeError, ValueError):- matches = []if len(matches) == 0:return ToolChunk(diff --git a/tests/builtin_glob_test.py b/tests/builtin_glob_test.pyindex a91111f60..4a59ac527 100644--- a/tests/builtin_glob_test.py+++ b/tests/builtin_glob_test.py@@ -3,9 +3,10 @@import osimport tempfilefrom unittest.async_case import IsolatedAsyncioTestCase+from unittest.mock import AsyncMockfrom utils import AnyString-from agentscope.tool import Glob+from agentscope.tool import ExecResult, Glob, LocalBackendfrom agentscope.permission import (PermissionContext,PermissionBehavior,@@ -74,6 +75,45 @@ class GlobToolTest(IsolatedAsyncioTestCase):# Read/Glob/Grep are read-only, return PASSTHROUGHself.assertEqual(decision.behavior, PermissionBehavior.PASSTHROUGH)+ async def test_invalid_helper_output(self) -> None:+ """Malformed helper output must not masquerade as no matches."""+ backend = LocalBackend()+ backend.is_dir = AsyncMock(return_value=True)+ backend.exec_shell = AsyncMock()+ tool = Glob(backend=backend)+ for output in (+ b"not-json",+ b"null",+ b"{}",+ b'"path.py"',+ b"[1]",+ b"[null]",+ ):+ with self.subTest(output=output):+ backend.exec_shell.return_value = ExecResult(0, output, b"")+ chunk = await tool(pattern="*.py", path=self.temp_dir)+ self.assertEqual(+ chunk.model_dump(+ exclude={+ "id": True,+ "content": {"__all__": {"id", "created_at"}},+ },+ ),+ {+ "content": [+ {+ "type": "text",+ "text": "Glob helper returned invalid output: "+ "expected a JSON array of file paths.",+ "finished_at": None,+ },+ ],+ "state": "error",+ "is_last": True,+ "metadata": {},+ },+ )+async def test_simple_pattern(self) -> None:"""Test simple glob pattern."""chunk = await self.glob_tool(