-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconftest.py
More file actions
134 lines (106 loc) · 4.21 KB
/
conftest.py
File metadata and controls
134 lines (106 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Fixtures for LangGraph React agent evals."""
from __future__ import annotations
import asyncio
import logging
import os
import time
import warnings
from pathlib import Path
from typing import Any, AsyncGenerator, Callable, Coroutine
import httpx
import pytest
import yaml
from harness.fixtures import load_golden as _load_golden_from
from harness.runner import TaskConfig, TaskResult, run_task
try:
from harness.mlflow_client import MLflowTraceClient
except ImportError:
MLflowTraceClient = None # type: ignore[misc,assignment]
def _find_repo_root() -> Path:
"""Walk up from this file to find the repository root.
Uses the presence of tests/behavioral/configs/thresholds.yaml as
the sentinel to distinguish the repo root from agent-level directories
that also contain pyproject.toml and tests/behavioral/.
"""
path = Path(__file__).resolve().parent
while path.parent != path:
candidate = path / "tests" / "behavioral" / "configs" / "thresholds.yaml"
if candidate.is_file():
return path
path = path.parent
raise FileNotFoundError(
"Could not find repo root (no tests/behavioral/configs/thresholds.yaml)"
)
FIXTURES_DIR = Path(__file__).parent / "fixtures"
def load_golden(category: str | None = None) -> list[dict[str, Any]]:
"""Load golden queries from the fixtures directory, optionally filtering by category."""
return _load_golden_from(FIXTURES_DIR, category)
@pytest.fixture
def agent_url() -> str:
"""React agent URL from REACT_AGENT_URL env var or default localhost:8000."""
return os.environ.get("REACT_AGENT_URL", "http://localhost:8000")
@pytest.fixture
async def http_client() -> AsyncGenerator[httpx.AsyncClient, None]:
"""Provide an async httpx client that is closed after the test."""
async with httpx.AsyncClient() as client:
yield client
@pytest.fixture
def eval_config() -> dict[str, Any]:
"""Load threshold configuration from the shared configs directory."""
config_path = (
_find_repo_root() / "tests" / "behavioral" / "configs" / "thresholds.yaml"
)
with open(config_path, encoding="utf-8") as f:
return yaml.safe_load(f)
@pytest.fixture
def known_tools() -> list[str]:
"""Tools available on the LangGraph React agent."""
return ["search"]
@pytest.fixture
def react_thresholds(eval_config: dict[str, Any]) -> dict[str, Any]:
"""Load the langgraph_react section from the shared thresholds config."""
return eval_config["langgraph_react"]
@pytest.fixture
def run_eval(
agent_url: str, http_client: httpx.AsyncClient
) -> Callable[..., Coroutine[Any, Any, TaskResult]]:
"""Run eval with automatic MLflow enrichment when available.
Overrides the root run_eval fixture to add MLflow trace data
(tool calls, token usage) after each request.
"""
mlflow = None
if MLflowTraceClient is not None:
tracking_uri = os.environ.get("MLFLOW_TRACKING_URI")
experiment = os.environ.get("MLFLOW_EXPERIMENT_NAME")
if tracking_uri and experiment:
mlflow = MLflowTraceClient(tracking_uri, experiment)
async def _run(
query: str,
expected_tools: list[str] | None = None,
timeout_seconds: float = 30.0,
max_tokens_budget: int | None = None,
model: str | None = None,
stream: bool = False,
) -> TaskResult:
config = TaskConfig(
agent_url=agent_url,
query=query,
expected_tools=expected_tools,
timeout_seconds=timeout_seconds,
max_tokens_budget=max_tokens_budget,
model=model,
stream=stream,
)
request_start_ms = int(time.time() * 1000)
result = await run_task(config, client=http_client)
if mlflow is not None and result.success:
try:
await asyncio.to_thread(
mlflow.enrich_eval_result, result, since_ms=request_start_ms
)
except Exception:
msg = "MLflow enrichment failed — tool scoring will degrade to content heuristics"
logging.getLogger(__name__).warning(msg, exc_info=True)
warnings.warn(msg, stacklevel=2)
return result
return _run