@@ -793,6 +793,65 @@ async def search_tool(query: str) -> str:
793793 assert captured_steps [0 ]["type" ] == "tool"
794794
795795
796+ class TestEvaluateSessionContext :
797+ """Tests for session context forwarding in the decorator evaluation path."""
798+
799+ @pytest .mark .asyncio
800+ async def test_evaluate_forwards_session_target_and_client_state (self ):
801+ """The decorator path should use the same session target as evaluate_controls()."""
802+ from agent_control .control_decorators import _evaluate
803+ from agent_control .runtime_auth import RuntimeTokenCache
804+ from agent_control_models import EvaluationResult
805+
806+ captured_client_kwargs = {}
807+ captured_eval_kwargs = {}
808+ runtime_token_cache = RuntimeTokenCache ()
809+
810+ class FakeClient :
811+ def __init__ (self , ** kwargs ):
812+ captured_client_kwargs .update (kwargs )
813+
814+ async def __aenter__ (self ):
815+ return self
816+
817+ async def __aexit__ (self , exc_type , exc , tb ):
818+ return None
819+
820+ async def fake_check_evaluation_with_local (** kwargs ):
821+ captured_eval_kwargs .update (kwargs )
822+ return EvaluationResult (is_safe = True , confidence = 1.0 )
823+
824+ with (
825+ patch ("agent_control.control_decorators.AgentControlClient" , FakeClient ),
826+ patch (
827+ "agent_control.control_decorators.check_evaluation_with_local" ,
828+ side_effect = fake_check_evaluation_with_local ,
829+ ),
830+ patch ("agent_control.control_decorators.state.api_key" , "session-key" ),
831+ patch ("agent_control.control_decorators.state.api_key_header" , "X-Custom-Key" ),
832+ patch (
833+ "agent_control.control_decorators.state.runtime_token_cache" ,
834+ runtime_token_cache ,
835+ ),
836+ patch ("agent_control.control_decorators.state.target_type" , "env" ),
837+ patch ("agent_control.control_decorators.state.target_id" , "prod" ),
838+ ):
839+ result = await _evaluate (
840+ agent_name = "agent" ,
841+ step = {"type" : "llm" , "name" : "chat" , "input" : "hello" },
842+ stage = "pre" ,
843+ server_url = "http://server.test" ,
844+ controls = [{"id" : 1 , "name" : "control" , "control" : {}}],
845+ )
846+
847+ assert result ["is_safe" ] is True
848+ assert captured_client_kwargs ["api_key" ] == "session-key"
849+ assert captured_client_kwargs ["api_key_header" ] == "X-Custom-Key"
850+ assert captured_client_kwargs ["runtime_token_cache" ] is runtime_token_cache
851+ assert captured_eval_kwargs ["target_type" ] == "env"
852+ assert captured_eval_kwargs ["target_id" ] == "prod"
853+
854+
796855# =============================================================================
797856# STEERING CONTEXT TESTS
798857# =============================================================================
0 commit comments