|
23 | 23 | from __future__ import annotations |
24 | 24 |
|
25 | 25 | import os |
| 26 | +import re |
26 | 27 |
|
27 | 28 | from google.adk.agents import ManagedAgent |
28 | 29 | from google.adk.runners import Runner |
|
31 | 32 | from google.adk.tools import RemoteMcpServer |
32 | 33 | from google.adk.utils.context_utils import Aclosing |
33 | 34 | from google.genai import types |
| 35 | +import httpx |
34 | 36 | import pytest |
35 | 37 |
|
36 | 38 | _AGENT_ID = 'antigravity-preview-05-2026' |
@@ -91,6 +93,86 @@ async def test_google_search_project_hail_mary(): |
91 | 93 | ), f'expected the grounded answer to contain "James Ortiz"; got: {answer!r}' |
92 | 94 |
|
93 | 95 |
|
| 96 | +@pytest.mark.asyncio |
| 97 | +async def test_interactions_request_carries_managed_agent_suffix(monkeypatch): |
| 98 | + """The outgoing Interactions request must carry google-adk/<ver>+managed_agent. |
| 99 | +
|
| 100 | + Guards the per-request tracking suffix at runtime. Unit tests only prove ADK |
| 101 | + builds the right extra_headers dict; this proves the suffix survives onto the |
| 102 | + actual outgoing HTTP request. Hooks httpx.AsyncClient.send (the layer every |
| 103 | + google-genai transport funnels through), runs a real ManagedAgent turn, and |
| 104 | + inspects the first interaction request's headers. Runs on both backends by |
| 105 | + default (see conftest llm_backend), covering the Gemini Developer API and |
| 106 | + Vertex interaction endpoints. |
| 107 | + """ |
| 108 | + captured: list[dict[str, str]] = [] |
| 109 | + orig_send = httpx.AsyncClient.send |
| 110 | + |
| 111 | + async def _spy_send(self, request, **kwargs): |
| 112 | + if 'interaction' in str(request.url).lower(): |
| 113 | + captured.append({ |
| 114 | + 'x-goog-api-client': request.headers.get('x-goog-api-client', ''), |
| 115 | + 'user-agent': request.headers.get('user-agent', ''), |
| 116 | + }) |
| 117 | + return await orig_send(self, request, **kwargs) |
| 118 | + |
| 119 | + monkeypatch.setattr(httpx.AsyncClient, 'send', _spy_send) |
| 120 | + |
| 121 | + agent = ManagedAgent( |
| 122 | + name='managed_header_agent', |
| 123 | + agent_id=_AGENT_ID, |
| 124 | + environment={'type': 'remote'}, |
| 125 | + tools=[google_search], |
| 126 | + ) |
| 127 | + session_service = InMemorySessionService() |
| 128 | + runner = Runner( |
| 129 | + app_name='managed_agent_it', |
| 130 | + agent=agent, |
| 131 | + session_service=session_service, |
| 132 | + ) |
| 133 | + session = await session_service.create_session( |
| 134 | + app_name='managed_agent_it', user_id='test_user' |
| 135 | + ) |
| 136 | + |
| 137 | + # Request headers are set before any response is produced, so stop streaming |
| 138 | + # as soon as an interaction request is captured. This keeps the test fast and |
| 139 | + # independent of (non-deterministic) model output; any error after capture is |
| 140 | + # irrelevant to the assertion. |
| 141 | + run_error = None |
| 142 | + try: |
| 143 | + async with Aclosing( |
| 144 | + runner.run_async( |
| 145 | + user_id='test_user', |
| 146 | + session_id=session.id, |
| 147 | + new_message=types.Content( |
| 148 | + role='user', parts=[types.Part.from_text(text='Say hi.')] |
| 149 | + ), |
| 150 | + ) |
| 151 | + ) as agen: |
| 152 | + async for _ in agen: |
| 153 | + if captured: |
| 154 | + break |
| 155 | + except Exception as e: # noqa: BLE001 - header is captured before any later error |
| 156 | + run_error = e |
| 157 | + |
| 158 | + assert captured, ( |
| 159 | + 'no Interactions request was observed on the wire; ' |
| 160 | + f'run raised: {run_error!r}' |
| 161 | + ) |
| 162 | + api_client = captured[0]['x-goog-api-client'] |
| 163 | + user_agent = captured[0]['user-agent'] |
| 164 | + print('\n=== captured interaction request headers ===') |
| 165 | + print('x-goog-api-client:', api_client) |
| 166 | + print('user-agent: ', user_agent) |
| 167 | + assert re.search(r'google-adk/[^ ]*\+managed_agent', api_client), ( |
| 168 | + 'expected google-adk/<version>+managed_agent in x-goog-api-client; ' |
| 169 | + f'got: {api_client!r}' |
| 170 | + ) |
| 171 | + assert ( |
| 172 | + '+managed_agent' in user_agent |
| 173 | + ), f'expected +managed_agent in user-agent; got: {user_agent!r}' |
| 174 | + |
| 175 | + |
94 | 176 | @pytest.mark.asyncio |
95 | 177 | async def test_code_execution_prime_sum(): |
96 | 178 | agent = ManagedAgent( |
|
0 commit comments