feat: improve workflow agent tool interation

This commit is contained in:
Alex committed 2026-07-09 10:22:44 +01:00
1 parent 51dc49f0d8
commit 6dd5cf472a
12 files changed
+420 -214

No files matched your search

+48 -95
View File
@@ -1,79 +1,28 @@
from contextlib import contextmanager
from unittest.mock import MagicMock
import pytest
@pytest.mark.unit
class TestToolFilterMixin:
@pytest.fixture()
def _no_db(monkeypatch):
"""Scoped-id resolution opens a read conn; synthetic ids never use it."""
def test_get_user_tools_filters_by_allowed_ids(self):
from application.agents.workflows.node_agent import ToolFilterMixin
@contextmanager
def fake_readonly():
yield MagicMock()
class FakeBase:
def _get_user_tools(self, user="local"):
return {
"t1": {"_id": "id1", "name": "tool1"},
"t2": {"_id": "id2", "name": "tool2"},
"t3": {"_id": "id3", "name": "tool3"},
}
monkeypatch.setattr("application.agents.tool_executor.db_readonly", fake_readonly)
class TestClass(ToolFilterMixin, FakeBase):
pass
obj = TestClass()
obj._allowed_tool_ids = ["id1", "id3"]
result = obj._get_user_tools("user1")
assert "t1" in result
assert "t3" in result
assert "t2" not in result
class _FakeExecutorBase:
"""Stands in for BaseAgent: provides the tool_executor the mixin scopes."""
def test_get_user_tools_returns_empty_when_no_allowed(self):
from application.agents.workflows.node_agent import ToolFilterMixin
def __init__(self, *args, **kwargs):
from application.agents.tool_executor import ToolExecutor
class FakeBase:
def _get_user_tools(self, user="local"):
return {"t1": {"_id": "id1"}}
class TestClass(ToolFilterMixin, FakeBase):
pass
obj = TestClass()
obj._allowed_tool_ids = []
result = obj._get_user_tools()
assert result == {}
def test_get_tools_filters_by_allowed_ids(self):
from application.agents.workflows.node_agent import ToolFilterMixin
class FakeBase:
def _get_tools(self, api_key=None):
return {
"t1": {"_id": "id1"},
"t2": {"_id": "id2"},
}
class TestClass(ToolFilterMixin, FakeBase):
pass
obj = TestClass()
obj._allowed_tool_ids = ["id2"]
result = obj._get_tools("key")
assert "t2" in result
assert "t1" not in result
def test_get_tools_returns_empty_when_no_allowed(self):
from application.agents.workflows.node_agent import ToolFilterMixin
class FakeBase:
def _get_tools(self, api_key=None):
return {"t1": {"_id": "id1"}}
class TestClass(ToolFilterMixin, FakeBase):
pass
obj = TestClass()
obj._allowed_tool_ids = []
result = obj._get_tools()
assert result == {}
self.tool_executor = ToolExecutor()
@pytest.mark.unit
@@ -92,49 +41,53 @@ class TestWorkflowNodeAgentFactory:
)
# =====================================================================
# Coverage gap tests (lines 52-59: _WorkflowNodeMixin.__init__)
# =====================================================================
@pytest.mark.unit
class TestWorkflowNodeMixinInit:
"""The mixin scopes the EXECUTOR: agents fetch tools via tool_executor.get_tools(),
so per-node tool filtering must live there (the old agent-method mixin was dead code)."""
def test_mixin_init_sets_allowed_tool_ids(self):
"""Cover lines 52-59: _WorkflowNodeMixin.__init__ stores tool_ids."""
def _mixed(self, **kwargs):
from application.agents.workflows.node_agent import _WorkflowNodeMixin
class FakeBase:
def __init__(self, *args, **kwargs):
pass
class TestMixin(_WorkflowNodeMixin, FakeBase):
class TestMixin(_WorkflowNodeMixin, _FakeExecutorBase):
pass
obj = TestMixin(
return TestMixin(
endpoint="http://example.com",
llm_name="openai",
model_id="gpt-4",
api_key="key",
tool_ids=["tool1", "tool2"],
**kwargs,
)
assert obj._allowed_tool_ids == ["tool1", "tool2"]
def test_mixin_init_defaults_empty_tool_ids(self):
"""Cover: _WorkflowNodeMixin defaults to empty list."""
from application.agents.workflows.node_agent import _WorkflowNodeMixin
def test_mixin_scopes_executor_to_tool_ids(self):
obj = self._mixed(tool_ids=["tool1", "tool2"])
assert obj.tool_executor.allowed_tool_ids == ["tool1", "tool2"]
class FakeBase:
def __init__(self, *args, **kwargs):
pass
def test_mixin_defaults_to_empty_scope_not_unscoped(self):
# No tool_ids means the node gets NO tools — never the user's full set.
obj = self._mixed()
assert obj.tool_executor.allowed_tool_ids == []
class TestMixin(_WorkflowNodeMixin, FakeBase):
pass
def test_empty_scope_yields_no_tools(self):
obj = self._mixed()
assert obj.tool_executor.get_tools() == {}
obj = TestMixin(
endpoint="http://example.com",
llm_name="openai",
model_id="gpt-4",
api_key="key",
def test_scoped_executor_resolves_builtin_synthetic_ids(self, _no_db):
"""A node whose Tools picker selected a builtin (Artifact / Read Document)
must get that tool at runtime — the P0 this design replaced."""
from application.agents.default_tools import default_tool_id
obj = self._mixed(
tool_ids=[
default_tool_id("artifact_generator"),
default_tool_id("read_document"),
]
)
assert obj._allowed_tool_ids == []
tools = obj.tool_executor.get_tools()
names = {t["name"] for t in tools.values()}
assert names == {"artifact_generator", "read_document"}
def test_scoped_executor_drops_unresolvable_ids(self, _no_db):
obj = self._mixed(tool_ids=["not-a-real-tool-id"])
assert obj.tool_executor.get_tools() == {}