feat(bias): implement ToolBiasEngine and integrate into ai_client orchestration loop

This commit is contained in:
2026-03-10 09:53:59 -04:00
parent ddc148ca4e
commit cad04bfbfc
4 changed files with 282 additions and 85 deletions

View File

@@ -0,0 +1,47 @@
import pytest
from src import ai_client
from src.models import ToolPreset, Tool, BiasProfile
from unittest.mock import MagicMock, patch
def test_system_prompt_biasing():
# Setup
preset = ToolPreset(name="TestPreset", categories={
"General": [Tool(name="read_file", weight=5)]
})
bias = BiasProfile(name="TestBias", category_multipliers={"General": 1.5})
with patch("src.ai_client._active_tool_preset", preset):
with patch("src.ai_client._active_bias_profile", bias):
prompt = ai_client._get_combined_system_prompt()
assert "Tooling Strategy" in prompt
assert "read_file" in prompt
assert "General" in prompt
def test_tool_declaration_biasing_anthropic():
preset = ToolPreset(name="TestPreset", categories={
"General": [Tool(name="read_file", weight=5)]
})
with patch("src.ai_client._active_tool_preset", preset):
with patch("src.ai_client._agent_tools", {"read_file": True}):
# _get_anthropic_tools calls _build_anthropic_tools which should now use the bias engine
with patch("src.ai_client._CACHED_ANTHROPIC_TOOLS", None):
tools = ai_client._get_anthropic_tools()
read_file_tool = next(t for t in tools if t["name"] == "read_file")
assert "[HIGH PRIORITY]" in read_file_tool["description"]
@pytest.mark.asyncio
async def test_set_tool_preset_with_objects():
# This tests that set_tool_preset correctly handles the new Tool objects
preset = ToolPreset(name="ObjTest", categories={
"General": [Tool(name="read_file", approval="auto")]
})
with patch("src.tool_presets.ToolPresetManager.load_all", return_value={"ObjTest": preset}):
ai_client.set_tool_preset("ObjTest")
assert ai_client._agent_tools["read_file"] is True
assert ai_client._tool_approval_modes["read_file"] == "auto"
assert ai_client._active_tool_preset == preset

50
tests/test_tool_bias.py Normal file
View File

@@ -0,0 +1,50 @@
import pytest
from src.tool_bias import ToolBiasEngine
from src.models import ToolPreset, Tool, BiasProfile
def test_apply_semantic_nudges():
engine = ToolBiasEngine()
preset = ToolPreset(name="test", categories={
"General": [
Tool(name="read_file", weight=5),
Tool(name="list_directory", weight=1)
]
})
# Mock tool definitions (simplified MCP_TOOL_SPECS)
tool_defs = [
{"name": "read_file", "description": "Read file content.", "parameters": {"properties": {"path": {"description": "Path to file"}}}},
{"name": "list_directory", "description": "List dir.", "parameters": {"properties": {"path": {"description": "Path to dir"}}}}
]
nudged = engine.apply_semantic_nudges(tool_defs, preset)
assert "[HIGH PRIORITY]" in nudged[0]["description"]
assert "[LOW PRIORITY]" in nudged[1]["description"]
def test_parameter_bias_nudging():
engine = ToolBiasEngine()
preset = ToolPreset(name="test", categories={
"General": [
Tool(name="read_file", parameter_bias={"path": "PREFERRED"})
]
})
tool_defs = [
{"name": "read_file", "description": "Read file.", "parameters": {"properties": {"path": {"description": "Path."}}, "required": ["path"]}}
]
nudged = engine.apply_semantic_nudges(tool_defs, preset)
assert "[PREFERRED]" in nudged[0]["parameters"]["properties"]["path"]["description"]
def test_generate_tooling_strategy():
engine = ToolBiasEngine()
preset = ToolPreset(name="test", categories={
"General": [Tool(name="read_file", weight=5)]
})
bias = BiasProfile(name="test", category_multipliers={"General": 2.0})
strategy = engine.generate_tooling_strategy(preset, bias)
assert "Tooling Strategy" in strategy
assert "read_file" in strategy
assert "General" in strategy