|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""LLM routing controls and generation observability.""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from collections.abc import Iterator |
| 8 | +from typing import Any, cast |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from nooa import Agent, strategy |
| 13 | +from nooa.runtime.hooks import set_hooks |
| 14 | +from nooa.strategies import PredictStrategy |
| 15 | +from nooa.unifiedllm import FakeLLMClient, LLMResponse |
| 16 | + |
| 17 | + |
| 18 | +def _resp(value: str) -> LLMResponse: |
| 19 | + return LLMResponse( |
| 20 | + raw_response=None, |
| 21 | + content=f'{{"value": "{value}"}}', |
| 22 | + tool_calls=[], |
| 23 | + finish_reason="stop", |
| 24 | + assistant_message={"role": "assistant", "content": f'{{"value": "{value}"}}'}, |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +def _llm(model: str, value: str) -> FakeLLMClient: |
| 29 | + client = FakeLLMClient(scripted_responses=[_resp(value)]) |
| 30 | + client.model = model |
| 31 | + return client |
| 32 | + |
| 33 | + |
| 34 | +class RoutingHooks: |
| 35 | + """Capture generation hook metadata for routing assertions.""" |
| 36 | + |
| 37 | + def __init__(self) -> None: |
| 38 | + self.generations: list[dict[str, Any]] = [] |
| 39 | + |
| 40 | + def before_generation( |
| 41 | + self, |
| 42 | + agent: Any, |
| 43 | + method_name: str, |
| 44 | + strategy: str, |
| 45 | + generation_id: str, |
| 46 | + parent_generation_id: str | None, |
| 47 | + **kwargs: Any, |
| 48 | + ) -> dict[str, Any]: |
| 49 | + self.generations.append( |
| 50 | + { |
| 51 | + "method_name": method_name, |
| 52 | + "strategy": strategy, |
| 53 | + "generation_id": generation_id, |
| 54 | + **kwargs, |
| 55 | + } |
| 56 | + ) |
| 57 | + return {"generation_id": generation_id} |
| 58 | + |
| 59 | + def after_generation( |
| 60 | + self, |
| 61 | + agent: Any, |
| 62 | + method_name: str, |
| 63 | + result: Any, |
| 64 | + exception: Exception | None, |
| 65 | + context: Any, |
| 66 | + generation_id: str, |
| 67 | + ) -> None: |
| 68 | + pass |
| 69 | + |
| 70 | + def on_messages_built( |
| 71 | + self, |
| 72 | + agent: Any, |
| 73 | + method_name: str, |
| 74 | + messages: list[dict[str, Any]], |
| 75 | + generation_id: str, |
| 76 | + **kwargs: Any, |
| 77 | + ) -> None: |
| 78 | + pass |
| 79 | + |
| 80 | + def before_agent_call( |
| 81 | + self, |
| 82 | + agent: Any, |
| 83 | + method_name: str, |
| 84 | + args: tuple[Any, ...], |
| 85 | + kwargs: dict[str, Any], |
| 86 | + call_id: str, |
| 87 | + parent_call_id: str | None, |
| 88 | + **extra: Any, |
| 89 | + ) -> dict[str, Any]: |
| 90 | + return {"call_id": call_id} |
| 91 | + |
| 92 | + def after_agent_call( |
| 93 | + self, |
| 94 | + agent: Any, |
| 95 | + method_name: str, |
| 96 | + result: Any, |
| 97 | + exception: Exception | None, |
| 98 | + context: Any, |
| 99 | + **kwargs: Any, |
| 100 | + ) -> None: |
| 101 | + pass |
| 102 | + |
| 103 | + def before_code_execution( |
| 104 | + self, |
| 105 | + agent: Any, |
| 106 | + code: str, |
| 107 | + execution_id: str, |
| 108 | + generation_id: str | None = None, |
| 109 | + **kwargs: Any, |
| 110 | + ) -> dict[str, Any]: |
| 111 | + return {"execution_id": execution_id} |
| 112 | + |
| 113 | + def after_code_execution( |
| 114 | + self, |
| 115 | + agent: Any, |
| 116 | + code: str, |
| 117 | + result: Any, |
| 118 | + exception: Exception | None, |
| 119 | + context: Any, |
| 120 | + execution_id: str, |
| 121 | + **kwargs: Any, |
| 122 | + ) -> None: |
| 123 | + pass |
| 124 | + |
| 125 | + def before_method_invocation( |
| 126 | + self, |
| 127 | + agent: Any, |
| 128 | + method_name: str, |
| 129 | + args: tuple[Any, ...], |
| 130 | + kwargs: dict[str, Any], |
| 131 | + invocation_id: str, |
| 132 | + **extra: Any, |
| 133 | + ) -> dict[str, Any]: |
| 134 | + return {"invocation_id": invocation_id} |
| 135 | + |
| 136 | + def after_method_invocation( |
| 137 | + self, |
| 138 | + agent: Any, |
| 139 | + method_name: str, |
| 140 | + result: Any, |
| 141 | + exception: Exception | None, |
| 142 | + context: Any, |
| 143 | + invocation_id: str, |
| 144 | + **kwargs: Any, |
| 145 | + ) -> None: |
| 146 | + pass |
| 147 | + |
| 148 | + def before_tool_execution( |
| 149 | + self, |
| 150 | + agent: Any, |
| 151 | + tool_name: str, |
| 152 | + arguments: dict[str, Any], |
| 153 | + execution_id: str, |
| 154 | + generation_id: str | None = None, |
| 155 | + **kwargs: Any, |
| 156 | + ) -> dict[str, Any]: |
| 157 | + return {"execution_id": execution_id} |
| 158 | + |
| 159 | + def after_tool_execution( |
| 160 | + self, |
| 161 | + agent: Any, |
| 162 | + tool_name: str, |
| 163 | + arguments: dict[str, Any], |
| 164 | + result: Any, |
| 165 | + exception: Exception | None, |
| 166 | + context: Any, |
| 167 | + execution_id: str, |
| 168 | + **kwargs: Any, |
| 169 | + ) -> None: |
| 170 | + pass |
| 171 | + |
| 172 | + |
| 173 | +@pytest.fixture |
| 174 | +def routing_hooks() -> Iterator[RoutingHooks]: |
| 175 | + hooks = RoutingHooks() |
| 176 | + set_hooks(cast(Any, hooks)) |
| 177 | + try: |
| 178 | + yield hooks |
| 179 | + finally: |
| 180 | + set_hooks(None) |
| 181 | + |
| 182 | + |
| 183 | +@pytest.mark.asyncio |
| 184 | +async def test_call_site_llm_override_wins_and_is_observable(routing_hooks: RoutingHooks) -> None: |
| 185 | + default_llm = _llm("default-model", "default") |
| 186 | + decorator_llm = _llm("decorator-model", "decorator") |
| 187 | + override_llm = _llm("override-model", "override") |
| 188 | + |
| 189 | + class RoutingAgent(Agent, llm=default_llm): |
| 190 | + @strategy(PredictStrategy(), llm=decorator_llm) |
| 191 | + async def summarize(self, text: str) -> str: |
| 192 | + """Summarize text.""" |
| 193 | + ... |
| 194 | + |
| 195 | + agent = RoutingAgent() |
| 196 | + |
| 197 | + assert await agent.summarize("hello", llm=override_llm) == "override" # pyright: ignore[reportCallIssue] |
| 198 | + |
| 199 | + assert default_llm.call_count == 0 |
| 200 | + assert decorator_llm.call_count == 0 |
| 201 | + assert override_llm.call_count == 1 |
| 202 | + assert routing_hooks.generations[-1]["llm.model_name"] == "override-model" |
| 203 | + assert routing_hooks.generations[-1]["llm.selection_source"] == "call_site" |
| 204 | + |
| 205 | + |
| 206 | +@pytest.mark.asyncio |
| 207 | +async def test_decorator_llm_selection_is_observable(routing_hooks: RoutingHooks) -> None: |
| 208 | + default_llm = _llm("default-model", "default") |
| 209 | + decorator_llm = _llm("decorator-model", "decorator") |
| 210 | + |
| 211 | + class RoutingAgent(Agent, llm=default_llm): |
| 212 | + @strategy(PredictStrategy(), llm=decorator_llm) |
| 213 | + async def summarize(self, text: str) -> str: |
| 214 | + """Summarize text.""" |
| 215 | + ... |
| 216 | + |
| 217 | + agent = RoutingAgent() |
| 218 | + |
| 219 | + assert await agent.summarize("hello") == "decorator" |
| 220 | + |
| 221 | + assert default_llm.call_count == 0 |
| 222 | + assert decorator_llm.call_count == 1 |
| 223 | + assert routing_hooks.generations[-1]["llm.model_name"] == "decorator-model" |
| 224 | + assert routing_hooks.generations[-1]["llm.selection_source"] == "decorator" |
| 225 | + |
| 226 | + |
| 227 | +@pytest.mark.asyncio |
| 228 | +async def test_agent_default_llm_selection_is_observable(routing_hooks: RoutingHooks) -> None: |
| 229 | + default_llm = _llm("default-model", "default") |
| 230 | + |
| 231 | + class RoutingAgent(Agent, llm=default_llm): |
| 232 | + @strategy(PredictStrategy()) |
| 233 | + async def summarize(self, text: str) -> str: |
| 234 | + """Summarize text.""" |
| 235 | + ... |
| 236 | + |
| 237 | + agent = RoutingAgent() |
| 238 | + |
| 239 | + assert await agent.summarize("hello") == "default" |
| 240 | + |
| 241 | + assert default_llm.call_count == 1 |
| 242 | + assert routing_hooks.generations[-1]["llm.model_name"] == "default-model" |
| 243 | + assert routing_hooks.generations[-1]["llm.selection_source"] == "agent_default" |
| 244 | + |
| 245 | + |
| 246 | +@pytest.mark.asyncio |
| 247 | +async def test_user_parameter_named_llm_is_not_consumed_as_framework_override( |
| 248 | + routing_hooks: RoutingHooks, |
| 249 | +) -> None: |
| 250 | + default_llm = _llm("default-model", "ok") |
| 251 | + |
| 252 | + class RoutingAgent(Agent, llm=default_llm): |
| 253 | + @strategy(PredictStrategy()) |
| 254 | + async def summarize(self, llm: str) -> str: |
| 255 | + """Summarize using the user-supplied label.""" |
| 256 | + ... |
| 257 | + |
| 258 | + agent = RoutingAgent() |
| 259 | + |
| 260 | + assert await agent.summarize(llm="customer-visible-label") == "ok" |
| 261 | + |
| 262 | + assert default_llm.call_count == 1 |
| 263 | + assert routing_hooks.generations[-1]["llm.model_name"] == "default-model" |
| 264 | + assert routing_hooks.generations[-1]["llm.selection_source"] == "agent_default" |
0 commit comments