Skip to content

Agent Zero AI framework

Preserve parallel tool args for late secret unmasking

spark4d94c3b173ee
diff --git a/extensions/python/tool_execute_before/_10_unmask_secrets.py b/extensions/python/tool_execute_before/_10_unmask_secrets.py
index 41f8eebe1..3ec649d55 100644
--- a/extensions/python/tool_execute_before/_10_unmask_secrets.py
+++ b/extensions/python/tool_execute_before/_10_unmask_secrets.py
@@ -1,28 +1,7 @@
-from collections.abc import Callable
-from typing import Any
-
from helpers.extension import Extension
from helpers.secrets import get_secrets_manager
-def replace_placeholders_recursive(
- value: Any,
- replace: Callable[[str], str],
-) -> Any:
- if isinstance(value, str):
- return replace(value)
- if isinstance(value, dict):
- return {
- key: replace_placeholders_recursive(item, replace)
- for key, item in value.items()
- }
- if isinstance(value, list):
- return [replace_placeholders_recursive(item, replace) for item in value]
- if isinstance(value, tuple):
- return tuple(replace_placeholders_recursive(item, replace) for item in value)
- return value
-
-
class UnmaskToolSecrets(Extension):
async def execute(self, **kwargs):
@@ -38,6 +17,5 @@ class UnmaskToolSecrets(Extension):
# Unmask placeholders in args for actual tool execution
for k, v in tool_args.items():
- tool_args[k] = replace_placeholders_recursive(
- v, secrets_mgr.replace_placeholders
- )
+ if isinstance(v, str):
+ tool_args[k] = secrets_mgr.replace_placeholders(v)
diff --git a/helpers/parallel_tools.py b/helpers/parallel_tools.py
index 6790a92c1..685598773 100644
--- a/helpers/parallel_tools.py
+++ b/helpers/parallel_tools.py
@@ -640,7 +640,7 @@ def _resolve_parallel_tool(
return None
try:
- tool.args = dict(tool_args)
+ tool.args = tool_args
except Exception:
pass
diff --git a/tests/test_parallel_tool_args.py b/tests/test_parallel_tool_args.py
new file mode 100644
index 000000000..bed8b6e12
--- /dev/null
+++ b/tests/test_parallel_tool_args.py
@@ -0,0 +1,77 @@
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+import pytest
+
+from helpers import parallel_tools
+
+
+class _Response:
+ message = "done"
+
+
+async def test_parallel_execute_applies_hook_mutations_to_tool_args(monkeypatch) -> None:
+ observed = {}
+
+ class FakeTool:
+ def __init__(self, args):
+ self.args = args
+
+ def get_log_object(self):
+ return None
+
+ async def before_execution(self, **kwargs):
+ pass
+
+ async def execute(self, **kwargs):
+ observed["tool_args"] = self.args
+ observed["kwargs"] = kwargs
+ return _Response()
+
+ async def after_execution(self, response):
+ pass
+
+ class FakeAgent:
+ context = SimpleNamespace()
+ loop_data = SimpleNamespace(current_tool=None)
+
+ def get_tool(self, **kwargs):
+ return FakeTool(kwargs["args"])
+
+ async def handle_intervention(self):
+ pass
+
+ async def unmask_nested_secret(*_args, **kwargs):
+ if not _args or _args[0] != "tool_execute_before":
+ return
+ tool_args = kwargs["tool_args"]
+ tool_args["tool_calls"][0]["tool_args"]["code"] = tool_args["tool_calls"][0][
+ "tool_args"
+ ]["code"].replace("§§secret(TOKEN)", "resolved-secret")
+
+ monkeypatch.setattr(
+ parallel_tools, "call_extensions_async", unmask_nested_secret
+ )
+
+ result = await parallel_tools.execute_tool_call(
+ FakeAgent(), # type: ignore[arg-type]
+ "code_execution_tool",
+ {
+ "tool_calls": [
+ {
+ "tool_name": "code_execution_tool",
+ "tool_args": {
+ "code": "curl -H 'Authorization: Bearer §§secret(TOKEN)'"
+ },
+ }
+ ]
+ },
+ )
+
+ assert result == "done"
+ assert observed["tool_args"]["tool_calls"][0]["tool_args"]["code"] == (
+ "curl -H 'Authorization: Bearer resolved-secret'"
+ )
+ assert observed["kwargs"] == observed["tool_args"]
diff --git a/tests/test_unmask_tool_secrets.py b/tests/test_unmask_tool_secrets.py
deleted file mode 100644
index b8f127c42..000000000
--- a/tests/test_unmask_tool_secrets.py
+++ /dev/null
@@ -1,63 +0,0 @@
-from __future__ import annotations
-
-import importlib.util
-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))
-
-MODULE_PATH = (
- PROJECT_ROOT
- / "extensions"
- / "python"
- / "tool_execute_before"
- / "_10_unmask_secrets.py"
-)
-SPEC = importlib.util.spec_from_file_location("test_unmask_tool_secrets", MODULE_PATH)
-assert SPEC and SPEC.loader
-unmask_tool_secrets = importlib.util.module_from_spec(SPEC)
-SPEC.loader.exec_module(unmask_tool_secrets)
-
-
-class _FakeSecretsManager:
- def replace_placeholders(self, text: str) -> str:
- return text.replace("§§secret(TOKEN)", "resolved-secret")
-
-
-class _FakeAgent:
- context = object()
-
-
-async def test_unmask_tool_secrets_replaces_nested_parallel_arguments(monkeypatch):
- monkeypatch.setattr(
- unmask_tool_secrets,
- "get_secrets_manager",
- lambda _context: _FakeSecretsManager(),
- )
- tool_args = {
- "tool_calls": [
- {
- "tool_name": "code_execution_tool",
- "tool_args": {
- "code": "curl -H 'Authorization: Bearer §§secret(TOKEN)'",
- "options": ("§§secret(TOKEN)", 3),
- },
- }
- ],
- "metadata": {"token": "§§secret(TOKEN)"},
- }
-
- extension = unmask_tool_secrets.UnmaskToolSecrets(_FakeAgent())
- await extension.execute(tool_args=tool_args)
-
- nested_args = tool_args["tool_calls"][0]["tool_args"]
- assert nested_args["code"] == (
- "curl -H 'Authorization: Bearer resolved-secret'"
- )
- assert nested_args["options"] == ("resolved-secret", 3)
- assert tool_args["metadata"] == {"token": "resolved-secret"}