1+ from importlib import import_module
2+
13import pytest
2- from braintrust import logger , setup_pydantic_ai
4+ from braintrust . integrations . pydantic_ai . _test_harness import make_harness_agent
35from braintrust .span_types import SpanTypeAttribute
4- from braintrust .test_helpers import init_test_logger
5- from pydantic_ai import Agent
66from pydantic_ai .messages import ModelResponse , TextPart , ToolCallPart , ToolReturnPart
77from pydantic_ai .models .function import FunctionModel
88from pydantic_ai .models .test import TestModel
99
1010
11- pytest .importorskip ("pydantic_ai_harness" )
12-
13- PROJECT_NAME = "test-pydantic-ai-harness-decisions"
14-
15-
16- @pytest .fixture
17- def memory_logger ():
18- assert setup_pydantic_ai (project_name = PROJECT_NAME )
19- init_test_logger (PROJECT_NAME )
20- with logger ._internal_with_memory_background_logger () as bgl :
21- yield bgl
22-
23-
2411def _decision_span (spans , name ):
2512 (agent_span ,) = (span for span in spans if span ["span_attributes" ]["name" ] == "agent_run [harness]" )
2613 (decision ,) = (span for span in spans if span ["span_attributes" ]["name" ] == name )
@@ -31,8 +18,8 @@ def _decision_span(spans, name):
3118
3219
3320@pytest .mark .asyncio
34- async def test_harness_compaction_decision (memory_logger ):
35- compaction = pytest . importorskip ("pydantic_ai_harness.compaction" )
21+ async def test_harness_compaction_decision (harness_spans ):
22+ compaction = import_module ("pydantic_ai_harness.compaction" )
3623 tool_results = []
3724
3825 def model (messages , _info ):
@@ -42,9 +29,9 @@ def model(messages, _info):
4229 tool_results .extend (results )
4330 return ModelResponse (parts = [TextPart ("done" )])
4431
45- agent = Agent (
32+ agent = make_harness_agent (
33+ "harness" ,
4634 FunctionModel (model ),
47- name = "harness" ,
4835 capabilities = [compaction .ClearToolResults (max_messages = 1 , keep_pairs = 0 )],
4936 )
5037
@@ -55,62 +42,60 @@ def note():
5542 assert (await agent .run ("call the tool" )).output == "done"
5643 assert tool_results == ["[tool result cleared]" ]
5744
58- decision = _decision_span (memory_logger .pop (), "compact_messages" )
45+ decision = _decision_span (harness_spans .pop (), "compact_messages" )
5946 assert decision ["metadata" ]["strategy" ] == "ClearToolResults"
6047 assert decision ["input" ] == {"messages" : 3 }
6148 assert decision ["output" ] == {"messages" : 3 }
6249
6350
6451@pytest .mark .asyncio
65- async def test_harness_memory_injection_decision (memory_logger ):
66- memory = pytest . importorskip ("pydantic_ai_harness.memory" )
52+ async def test_harness_memory_injection_decision (harness_spans ):
53+ memory = import_module ("pydantic_ai_harness.memory" )
6754 store = memory .InMemoryMemoryStore ()
6855 await store .write ("main/MEMORY.md" , "offline memory token" , expected_version = None )
69- agent = Agent (
56+ agent = make_harness_agent (
57+ "harness" ,
7058 TestModel (call_tools = [], custom_output_text = "done" ),
71- name = "harness" ,
7259 capabilities = [memory .Memory (store = store )],
7360 )
7461
7562 assert (await agent .run ("remember" )).output == "done"
7663
77- decision = _decision_span (memory_logger .pop (), "memory.inject" )
64+ decision = _decision_span (harness_spans .pop (), "memory.inject" )
7865 assert decision ["output" ]["outcome" ] == "ok"
7966 assert decision ["output" ]["appended" ] is True
8067 assert decision ["output" ]["injected_chars" ] >= len ("offline memory token" )
8168 assert "offline memory token" not in str (decision )
8269
8370
8471@pytest .mark .asyncio
85- async def test_harness_guardrail_block_decision (memory_logger ):
86- guardrails = pytest . importorskip ("pydantic_ai_harness.guardrails" )
72+ async def test_harness_guardrail_block_decision (harness_spans ):
73+ guardrails = import_module ("pydantic_ai_harness.guardrails" )
8774
8875 async def deny (_ctx , _output ):
8976 return guardrails .GuardrailResult .block ("blocked by test policy" )
9077
91- agent = Agent (
92- TestModel (custom_output_text = "rejected output" ),
93- name = "harness" ,
78+ agent = make_harness_agent (
79+ "harness" ,
9480 capabilities = [guardrails .OutputGuardrail (deny )],
9581 )
9682
9783 with pytest .raises (guardrails .OutputBlocked , match = "blocked by test policy" ):
9884 await agent .run ("check output" )
9985
100- decision = _decision_span (memory_logger .pop (), "guardrail blocked output" )
86+ decision = _decision_span (harness_spans .pop (), "guardrail blocked output" )
10187 assert decision ["input" ] == {"direction" : "output" }
10288 assert decision ["output" ] == {"action" : "block" , "reason" : "blocked by test policy" }
10389 assert not decision .get ("error" )
10490
10591
10692@pytest .mark .asyncio
10793@pytest .mark .parametrize ("instrument" , [False , True ])
108- async def test_harness_model_router_decision (memory_logger , instrument ):
109- router = pytest .importorskip ("pydantic_ai_harness.model_router" )
110- capabilities = pytest .importorskip ("pydantic_ai.capabilities" )
111- agent = Agent (
112- TestModel (),
113- name = "harness" ,
94+ async def test_harness_model_router_decision (harness_spans , instrument ):
95+ router = import_module ("pydantic_ai_harness.model_router" )
96+ capabilities = import_module ("pydantic_ai.capabilities" )
97+ agent = make_harness_agent (
98+ "harness" ,
11499 capabilities = [
115100 * ([capabilities .Instrumentation ()] if instrument else []),
116101 router .ModelRouter (
@@ -126,7 +111,7 @@ async def test_harness_model_router_decision(memory_logger, instrument):
126111
127112 assert (await agent .run ("route to alternate" )).output == "alternate"
128113
129- spans = memory_logger .pop ()
114+ spans = harness_spans .pop ()
130115 decision = _decision_span (spans , "model_router.select" )
131116 assert decision ["input" ] == {"choices" : ["default" , "alternate" ], "default" : "default" }
132117 assert decision ["metadata" ]["mode" ] == "once"
0 commit comments