44from braintrust import setup_pydantic_ai
55from braintrust .span_types import SpanTypeAttribute
66from braintrust .test_helpers import init_test_logger
7- from pydantic_ai import Agent , ModelSettings
7+ from pydantic_ai import Agent , ModelSettings , RunContext
8+ from pydantic_ai .messages import ModelRequest , UserPromptPart
9+ from pydantic_ai .models import ModelRequestContext , ModelRequestParameters , infer_model
10+ from pydantic_ai .usage import RunUsage
811
912
1013MODEL = "openai-chat:gpt-4.1-mini"
@@ -61,6 +64,28 @@ def add(x: int, y: int) -> int:
6164 )
6265
6366
67+ @pytest .mark .asyncio
68+ async def test_harness_background_compaction_retrieval_is_not_traced (decision_spans ):
69+ compaction = import_module ("pydantic_ai_harness.compaction" )
70+ model = infer_model (MODEL )
71+ messages = [ModelRequest (parts = [UserPromptPart (prompt )]) for prompt in ("older context " * 10 , "current request" )]
72+ ctx = RunContext (deps = None , model = model , usage = RunUsage (), messages = messages , conversation_id = "background" )
73+ request = ModelRequestContext (
74+ model = model , messages = messages .copy (), model_settings = None , model_request_parameters = ModelRequestParameters ()
75+ )
76+ fallback = compaction .FallbackCompaction (
77+ [compaction .SlidingWindowCompaction (keep_messages = 1 , preserve_first_user_message = False )],
78+ max_tokens = 100 ,
79+ background_start = 0.1 ,
80+ )
81+
82+ assert await fallback .before_model_request (ctx , request ) is request
83+ compacted = await fallback .take_background_compaction (messages , model = model , conversation_id = "background" )
84+
85+ assert compacted == messages [- 1 :]
86+ assert not decision_spans .pop ()
87+
88+
6489@pytest .mark .asyncio
6590@pytest .mark .vcr
6691async def test_harness_memory_injection_decision (decision_spans ):
@@ -92,7 +117,7 @@ async def test_harness_guardrail_block_decision(decision_spans):
92117
93118 async def check_output (_ctx , output ):
94119 if "restricted" in output .lower ():
95- return guardrails .GuardrailResult .block ("blocked by test policy" )
120+ return guardrails .GuardrailResult .block (f "blocked by test policy: { output } " )
96121 return guardrails .GuardrailResult .allow ()
97122
98123 agent = Agent (
@@ -102,12 +127,13 @@ async def check_output(_ctx, output):
102127 capabilities = [guardrails .OutputGuardrail (check_output )],
103128 )
104129
105- with pytest .raises (guardrails .OutputBlocked , match = "blocked by test policy" ):
130+ with pytest .raises (guardrails .OutputBlocked , match = "blocked by test policy: restricted " ):
106131 await agent .run ("Reply with only the word 'restricted'." )
107132
108133 decision = _decision_span (decision_spans .pop (), "guardrail blocked output" )
109134 assert decision ["input" ] == {"direction" : "output" }
110- assert decision ["output" ] == {"action" : "block" , "reason" : "blocked by test policy" }
135+ assert decision ["output" ] == {"action" : "block" }
136+ assert "blocked by test policy" not in str (decision )
111137 assert not decision .get ("error" )
112138
113139
@@ -141,6 +167,7 @@ async def test_harness_model_router_decision(decision_spans, instrument):
141167 decision = _decision_span (spans , "model_router.select" )
142168 assert decision ["input" ] == {"choices" : ["default" , "alternate" ], "default" : "default" }
143169 assert decision ["metadata" ]["mode" ] == "once"
170+ assert set (decision ["metadata" ]) <= {"mode" , "run_step" , "probability" , "error.type" }
144171 assert decision ["output" ] == {"choice" : "alternate" , "fallback_reason" : "none" }
145172 (router_agent ,) = (span for span in spans if span ["span_attributes" ]["name" ] == "agent_run [model_router]" )
146173 assert router_agent ["span_parents" ] == [decision ["span_id" ]]
0 commit comments