Skip to content

Agent Zero AI framework

fix(response): reject malformed response arguments

Alessandrod3a81ac65bbe
diff --git a/tests/test_response_tool_validation.py b/tests/test_response_tool_validation.py
new file mode 100644
index 000000000..feb2fa4fd
--- /dev/null
+++ b/tests/test_response_tool_validation.py
@@ -0,0 +1,43 @@
+import sys
+from pathlib import Path
+
+import pytest
+
+
+PROJECT_ROOT = Path(__file__).resolve().parents[1]
+if str(PROJECT_ROOT) not in sys.path:
+ sys.path.insert(0, str(PROJECT_ROOT))
+
+from helpers.errors import RepairableException
+from tools.response import ResponseTool
+
+
[email protected]("args", [{"text": "ok"}, {"message": "ok"}])
+async def test_response_tool_accepts_text_or_message(args) -> None:
+ tool = ResponseTool(None, "response", None, args, "", None)
+
+ response = await tool.execute()
+
+ assert response.message == "ok"
+ assert response.break_loop is True
+
+
+async def test_response_tool_rejects_nested_response_args() -> None:
+ tool = ResponseTool(
+ None,
+ "response",
+ None,
+ {
+ "thoughts": [],
+ "headline": "Providing exact payload",
+ "tool_name": "response",
+ "tool_args": {"text": "nested"},
+ },
+ "",
+ None,
+ )
+
+ with pytest.raises(RepairableException, match="top-level text or message"):
+ await tool.execute()
diff --git a/tools/response.py b/tools/response.py
index c45485743..ec5fd4d16 100644
--- a/tools/response.py
+++ b/tools/response.py
@@ -1,10 +1,17 @@
+from helpers.errors import RepairableException
from helpers.tool import Tool, Response
class ResponseTool(Tool):
async def execute(self, **kwargs):
- return Response(message=self.args["text"] if "text" in self.args else self.args["message"], break_loop=True)
+ for key in ("text", "message"):
+ message = self.args.get(key)
+ if isinstance(message, str):
+ return Response(message=message, break_loop=True)
+ raise RepairableException(
+ "response tool requires a top-level text or message string argument"
+ )
async def before_execution(self, **kwargs):
# self.log = self.agent.context.log.log(type="response", heading=f"{self.agent.agent_name}: Responding", content=self.args.get("text", ""))
diff --git a/tools/response.py.dox.md b/tools/response.py.dox.md
index a2d18ac0d..2edcee1b9 100644
--- a/tools/response.py.dox.md
+++ b/tools/response.py.dox.md
@@ -22,7 +22,10 @@
- Update this file whenever tool arguments, output shape, `break_loop` behavior, intervention handling, prompt instructions, or side effects change.
- `ResponseTool` is a `Tool`.
- `ResponseTool` defines `execute(...)`.
-- Imported dependency areas include: `helpers.tool`.
+- `ResponseTool` requires a top-level string `text` or legacy `message` argument.
+ Invalid arguments raise `RepairableException` so the agent can surface a correction
+ warning and retry rather than crash.
+- Imported dependency areas include: `helpers.errors`, `helpers.tool`.
## Key Concepts