Coverage for app / agents / fixtures.py: 96%
45 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
1"""Scripted tools — an evaluation case's fixed answers, wherever a run would
2call a tool of their name.
4A case that asks about live data (a share price, today's weather) scripts
5what the tool returns (``ToolFixture``), so its right answer stays right and
6a run does not move for reasons unrelated to the agent. A plain agent binds
7its tools in one place (``AgentEngine``); a skill graph binds them per node,
8and a ``tool`` node calls one directly. So that an agent evaluated through
9its skill graph — as it answers in chat — meets the same scripted world as
10one evaluated without, the substitution is defined once, here:
12* a fixture **stands in for** a bound tool of its name, keeping its
13 description and argument schema, so the model calls it exactly as it would
14 the real one;
15* a fixture naming a builtin that is not bound still borrows its schema;
16* in a graph, a fixture no node binds **joins every node that binds tools**
17 (a node built without tools is not handed one), and a ``tool`` node that
18 calls a scripted tool calls the script.
20Nothing a script answers is fetched: a fixture can never reach the network.
21"""
23from __future__ import annotations
25import logging
26from typing import Any, Iterable
28from langchain_core.tools import StructuredTool
30logger = logging.getLogger(__name__)
33def _scripted(fixture: Any, real: StructuredTool | None) -> StructuredTool:
34 from app.tools.registry import BUILTIN_TOOLS, make_scripted_tool, resolve_tool
36 if real is None and fixture.name in BUILTIN_TOOLS:
37 try:
38 real = resolve_tool(fixture.name, {})
39 except ValueError:
40 real = None
41 return make_scripted_tool(
42 fixture.name,
43 fixture.response,
44 fixture.description or "",
45 stands_in_for=real,
46 )
49def apply_to_tools(
50 tools: dict[str, StructuredTool], fixtures: Iterable[Any]
51) -> dict[str, StructuredTool]:
52 """``tools`` (by name) with each fixture standing in for the tool of its
53 name, or joining them."""
54 out = dict(tools)
55 for fixture in fixtures:
56 out[fixture.name] = _scripted(fixture, out.get(fixture.name))
57 return out
60def apply_to_graph(builder: Any, fixtures: Iterable[Any]) -> None:
61 """Script a built skill graph's tools (see the module docstring)."""
62 fixtures = list(fixtures)
63 if not fixtures:
64 return
65 from app.agents.operations import ToolOperationAgent
67 by_name = {fixture.name: fixture for fixture in fixtures}
68 bound: set[str] = set()
69 tool_nodes: list[ToolOperationAgent] = []
70 binders: list[Any] = []
71 for node in builder.nodes.values():
72 agent = node.agent
73 if isinstance(agent, ToolOperationAgent):
74 tool_nodes.append(agent)
75 continue
76 names = {t.name for t in getattr(agent, "tools", None) or []}
77 if names:
78 binders.append(agent)
79 bound |= names & set(by_name)
81 for agent in binders:
82 current = {t.name: t for t in agent.tools}
83 wanted = [
84 fixture
85 for name, fixture in by_name.items()
86 if name in current or name not in bound
87 ]
88 agent.tools = list(apply_to_tools(current, wanted).values())
90 for agent in tool_nodes:
91 fixture = by_name.get(agent.tool_name)
92 if fixture is not None:
93 agent.scripted = _scripted(fixture, None)
94 logger.info(
95 "[fixtures] scripted %d tool(s) across %d node(s) and %d tool node(s)",
96 len(by_name),
97 len(binders),
98 sum(1 for a in tool_nodes if getattr(a, "scripted", None) is not None),
99 )