google-agents-cli-extensions 1.14.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- core/claude/README.md +35 -0
- core/claude/template/.agents/skills/agents-cli-claude/SKILL.md +106 -0
- core/claude/template/.agents/skills/agents-cli-claude/references/claude.md +386 -0
- core/claude/template/.agents/skills/agents-cli-claude/references/migrate-claude-agent.md +318 -0
- core/claude/template/.agents/skills/agents-cli-claude/references/migrate-from-aws.md +132 -0
- core/claude/template/.dockerignore +28 -0
- core/claude/template/.env +18 -0
- core/claude/template/.env.example +17 -0
- core/claude/template/.template/templateconfig.yaml +31 -0
- core/claude/template/AGENTS.md +33 -0
- core/claude/template/Dockerfile +55 -0
- core/claude/template/agent_config/README.md +9 -0
- core/claude/template/agents-cli-extension.yaml +49 -0
- core/claude/template/app/__init__.py +15 -0
- core/claude/template/app/agent.py +49 -0
- core/claude/template/app/app_utils/a2a.py +245 -0
- core/claude/template/app/app_utils/bq_analytics/__init__.py +124 -0
- core/claude/template/app/app_utils/bq_analytics/recorder.py +593 -0
- core/claude/template/app/app_utils/bq_analytics/redaction.py +209 -0
- core/claude/template/app/app_utils/bq_analytics/schema.py +394 -0
- core/claude/template/app/app_utils/bq_analytics/tables.py +104 -0
- core/claude/template/app/app_utils/bq_analytics/uploader.py +188 -0
- core/claude/template/app/app_utils/claude.py +297 -0
- core/claude/template/app/app_utils/completions.py +225 -0
- core/claude/template/app/app_utils/conversation.py +29 -0
- core/claude/template/app/app_utils/memory_bank.py +308 -0
- core/claude/template/app/app_utils/project_settings.py +91 -0
- core/claude/template/app/app_utils/reasoning_engine.py +237 -0
- core/claude/template/app/app_utils/remote_agents.py +349 -0
- core/claude/template/app/app_utils/remote_auth.py +159 -0
- core/claude/template/app/app_utils/replies.py +143 -0
- core/claude/template/app/app_utils/session_store.py +174 -0
- core/claude/template/app/app_utils/settings.py +24 -0
- core/claude/template/app/app_utils/telemetry/__init__.py +114 -0
- core/claude/template/app/app_utils/telemetry/call_counts.py +44 -0
- core/claude/template/app/app_utils/telemetry/cli_relay.py +266 -0
- core/claude/template/app/app_utils/telemetry/config.py +133 -0
- core/claude/template/app/app_utils/telemetry/logs.py +175 -0
- core/claude/template/app/app_utils/telemetry/metrics.py +270 -0
- core/claude/template/app/app_utils/telemetry/request_flush.py +104 -0
- core/claude/template/app/app_utils/telemetry/subagent_spans.py +80 -0
- core/claude/template/app/app_utils/telemetry/traces.py +166 -0
- core/claude/template/app/app_utils/turn_events.py +463 -0
- core/claude/template/app/app_utils/turn_options.py +219 -0
- core/claude/template/app/app_utils/workspaces.py +65 -0
- core/claude/template/app/fast_api_app.py +71 -0
- core/claude/template/app/feature_tools.py +27 -0
- core/claude/template/pyproject.toml +100 -0
- core/claude/template/scripts/eval_generate.py +340 -0
- core/claude/template/scripts/publish.py +66 -0
- core/claude/template/scripts/run.py +85 -0
- core/claude/template/scripts/unsupported.py +48 -0
- core/claude/template/tests/eval/datasets/basic-dataset.json +31 -0
- core/claude/template/tests/eval/eval_config.yaml +17 -0
- core/claude/template/tests/eval/response_quality.py +60 -0
- core/claude/template/tests/integration/test_server_e2e.py +168 -0
- core/claude/template/tests/load_test/.results/.placeholder +0 -0
- core/claude/template/tests/load_test/README.md +24 -0
- core/claude/template/tests/load_test/load_test.py +118 -0
- core/claude/template/tests/unit/conftest.py +63 -0
- core/claude/template/tests/unit/helpers.py +32 -0
- core/claude/template/tests/unit/test_a2a.py +432 -0
- core/claude/template/tests/unit/test_bq_analytics.py +928 -0
- core/claude/template/tests/unit/test_bq_redaction.py +89 -0
- core/claude/template/tests/unit/test_claude.py +825 -0
- core/claude/template/tests/unit/test_completions.py +369 -0
- core/claude/template/tests/unit/test_fast_api_app.py +59 -0
- core/claude/template/tests/unit/test_feature_tools.py +55 -0
- core/claude/template/tests/unit/test_memory_bank.py +625 -0
- core/claude/template/tests/unit/test_reasoning_engine.py +295 -0
- core/claude/template/tests/unit/test_remote_agents.py +517 -0
- core/claude/template/tests/unit/test_telemetry.py +991 -0
- core/claude/template/tests/unit/test_turn_events.py +309 -0
- core/claude/template/tests/unit/test_turn_options.py +394 -0
- core/claude/template/uv.lock +3029 -0
- core/langchain/README.md +54 -0
- core/langchain/template/.agents/skills/agents-cli-langchain/SKILL.md +84 -0
- core/langchain/template/.agents/skills/agents-cli-langchain/references/langchain.md +120 -0
- core/langchain/template/.agents/skills/agents-cli-langchain/references/samples.md +42 -0
- core/langchain/template/.template/templateconfig.yaml +32 -0
- core/langchain/template/AGENTS.md +35 -0
- core/langchain/template/agents-cli-extension.yaml +54 -0
- core/langchain/template/app/__init__.py +19 -0
- core/langchain/template/app/agent.py +61 -0
- core/langchain/template/app/app_utils/a2a.py +176 -0
- core/langchain/template/app/app_utils/content.py +37 -0
- core/langchain/template/app/app_utils/telemetry.py +154 -0
- core/langchain/template/app/fast_api_app.py +68 -0
- core/langchain/template/langgraph.json +7 -0
- core/langchain/template/pyproject.toml +102 -0
- core/langchain/template/scripts/_content.py +40 -0
- core/langchain/template/scripts/eval_generate.py +202 -0
- core/langchain/template/scripts/publish.py +59 -0
- core/langchain/template/scripts/run.py +59 -0
- core/langchain/template/scripts/unsupported.py +48 -0
- core/langchain/template/tests/eval/datasets/basic-dataset.json +16 -0
- core/langchain/template/tests/eval/eval_config.yaml +32 -0
- core/langchain/template/tests/eval/response_quality.py +60 -0
- core/langchain/template/tests/integration/test_server_e2e.py +173 -0
- core/langchain/template/tests/load_test/.results/.placeholder +0 -0
- core/langchain/template/tests/load_test/README.md +20 -0
- core/langchain/template/tests/load_test/load_test.py +82 -0
- core/langchain/template/uv.lock +3820 -0
- google_agents_cli_extensions-1.14.2.dist-info/METADATA +133 -0
- google_agents_cli_extensions-1.14.2.dist-info/RECORD +107 -0
- google_agents_cli_extensions-1.14.2.dist-info/WHEEL +4 -0
- google_agents_cli_extensions-1.14.2.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""`remote_agent` against a real a2a-sdk server served in process over httpx."""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import functools
|
|
20
|
+
import logging
|
|
21
|
+
import ssl
|
|
22
|
+
from typing import ClassVar
|
|
23
|
+
from unittest.mock import patch
|
|
24
|
+
|
|
25
|
+
import anyio
|
|
26
|
+
import google.oauth2.credentials
|
|
27
|
+
import httpx
|
|
28
|
+
import pytest
|
|
29
|
+
from a2a.helpers import new_task_from_user_message
|
|
30
|
+
from a2a.server.agent_execution import AgentExecutor, RequestContext
|
|
31
|
+
from a2a.server.events import EventQueue
|
|
32
|
+
from a2a.server.request_handlers import DefaultRequestHandler
|
|
33
|
+
from a2a.server.routes import (
|
|
34
|
+
add_a2a_routes_to_fastapi,
|
|
35
|
+
create_agent_card_routes,
|
|
36
|
+
create_jsonrpc_routes,
|
|
37
|
+
)
|
|
38
|
+
from a2a.server.tasks import InMemoryTaskStore, TaskUpdater
|
|
39
|
+
from a2a.types import (
|
|
40
|
+
AgentCapabilities,
|
|
41
|
+
AgentCard,
|
|
42
|
+
AgentInterface,
|
|
43
|
+
AgentSkill,
|
|
44
|
+
Part,
|
|
45
|
+
)
|
|
46
|
+
from a2a.utils.constants import AGENT_CARD_WELL_KNOWN_PATH, PROTOCOL_VERSION_1_0
|
|
47
|
+
from claude_agent_sdk import ClaudeAgentOptions
|
|
48
|
+
from fastapi import FastAPI
|
|
49
|
+
from mcp import ClientSession
|
|
50
|
+
from mcp.shared.memory import create_client_server_memory_streams
|
|
51
|
+
from opentelemetry.sdk.trace import TracerProvider
|
|
52
|
+
|
|
53
|
+
from app.app_utils import claude
|
|
54
|
+
from app.app_utils.conversation import current_conversation
|
|
55
|
+
from app.app_utils.remote_agents import remote_agent
|
|
56
|
+
|
|
57
|
+
URL = "https://callee-xyz.a.run.app/a2a/app"
|
|
58
|
+
# Not Google-hosted, so no token by default.
|
|
59
|
+
THIRD_PARTY_URL = "https://callee.example/a2a/app"
|
|
60
|
+
# An Agent Runtime agent's /api passthrough, which serves the app's routes.
|
|
61
|
+
AGENT_RUNTIME_API = (
|
|
62
|
+
"/reasoningEngines/v1/projects/p/locations/us-central1/reasoningEngines/1/api"
|
|
63
|
+
)
|
|
64
|
+
AGENT_RUNTIME_URL = (
|
|
65
|
+
f"https://us-central1-aiplatform.googleapis.com{AGENT_RUNTIME_API}/a2a/app"
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class FakeAgent(AgentExecutor):
|
|
70
|
+
"""Replies "It's sunny." in two streamed chunks; fails on "fail"."""
|
|
71
|
+
|
|
72
|
+
def __init__(self) -> None:
|
|
73
|
+
self.requests: list[tuple[str, dict[str, str]]] = []
|
|
74
|
+
|
|
75
|
+
async def execute(self, context: RequestContext, event_queue: EventQueue) -> None:
|
|
76
|
+
self.requests.append(
|
|
77
|
+
(context.context_id, context.call_context.state["headers"])
|
|
78
|
+
)
|
|
79
|
+
task = context.current_task
|
|
80
|
+
if task is None:
|
|
81
|
+
task = new_task_from_user_message(context.message)
|
|
82
|
+
await event_queue.enqueue_event(task)
|
|
83
|
+
updater = TaskUpdater(event_queue, task.id, task.context_id)
|
|
84
|
+
await updater.start_work()
|
|
85
|
+
if context.get_user_input() == "fail":
|
|
86
|
+
await updater.failed(
|
|
87
|
+
updater.new_agent_message([Part(text="out of credits")])
|
|
88
|
+
)
|
|
89
|
+
return
|
|
90
|
+
for i, text in enumerate(["It's ", "sunny."]):
|
|
91
|
+
await updater.add_artifact(
|
|
92
|
+
[Part(text=text)],
|
|
93
|
+
artifact_id="a",
|
|
94
|
+
name="response",
|
|
95
|
+
append=i > 0,
|
|
96
|
+
last_chunk=False,
|
|
97
|
+
)
|
|
98
|
+
await updater.complete()
|
|
99
|
+
|
|
100
|
+
async def cancel(self, context: RequestContext, event_queue: EventQueue) -> None:
|
|
101
|
+
raise NotImplementedError
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class RecordingTransport(httpx.AsyncBaseTransport):
|
|
105
|
+
"""Serves `app` in process and records every request's URL and headers."""
|
|
106
|
+
|
|
107
|
+
def __init__(self, app: FastAPI) -> None:
|
|
108
|
+
self._asgi = httpx.ASGITransport(app=app)
|
|
109
|
+
self.requests: list[httpx.Request] = []
|
|
110
|
+
|
|
111
|
+
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
|
|
112
|
+
self.requests.append(request)
|
|
113
|
+
return await self._asgi.handle_async_request(request)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _serve(monkeypatch, prefix: str = "") -> FakeAgent:
|
|
117
|
+
"""Serve a FakeAgent's A2A routes under `prefix` to every httpx client."""
|
|
118
|
+
agent = FakeAgent()
|
|
119
|
+
# The card sends clients to another host: requests must stay on URL's.
|
|
120
|
+
card = AgentCard(
|
|
121
|
+
name="callee",
|
|
122
|
+
description="Weather.",
|
|
123
|
+
supported_interfaces=[
|
|
124
|
+
AgentInterface(
|
|
125
|
+
url=f"https://elsewhere.example{prefix}/a2a/app",
|
|
126
|
+
protocol_binding="JSONRPC",
|
|
127
|
+
protocol_version=PROTOCOL_VERSION_1_0,
|
|
128
|
+
)
|
|
129
|
+
],
|
|
130
|
+
version="1",
|
|
131
|
+
default_input_modes=["text/plain"],
|
|
132
|
+
default_output_modes=["text/plain"],
|
|
133
|
+
capabilities=AgentCapabilities(streaming=True),
|
|
134
|
+
skills=[
|
|
135
|
+
AgentSkill(
|
|
136
|
+
id="weather", name="weather", description="Weather.", tags=["weather"]
|
|
137
|
+
)
|
|
138
|
+
],
|
|
139
|
+
)
|
|
140
|
+
app = FastAPI()
|
|
141
|
+
add_a2a_routes_to_fastapi(
|
|
142
|
+
app,
|
|
143
|
+
agent_card_routes=create_agent_card_routes(
|
|
144
|
+
card, card_url=f"/a2a/app{AGENT_CARD_WELL_KNOWN_PATH}"
|
|
145
|
+
),
|
|
146
|
+
jsonrpc_routes=create_jsonrpc_routes(
|
|
147
|
+
DefaultRequestHandler(
|
|
148
|
+
agent_executor=agent, task_store=InMemoryTaskStore(), agent_card=card
|
|
149
|
+
),
|
|
150
|
+
rpc_url="/a2a/app",
|
|
151
|
+
),
|
|
152
|
+
)
|
|
153
|
+
if prefix:
|
|
154
|
+
outer = FastAPI()
|
|
155
|
+
outer.mount(prefix, app)
|
|
156
|
+
app = outer
|
|
157
|
+
agent.transport = RecordingTransport(app)
|
|
158
|
+
monkeypatch.setattr(
|
|
159
|
+
httpx,
|
|
160
|
+
"AsyncClient",
|
|
161
|
+
functools.partial(httpx.AsyncClient, transport=agent.transport),
|
|
162
|
+
)
|
|
163
|
+
return agent
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
@pytest.fixture(autouse=True)
|
|
167
|
+
def _no_client_certificate(monkeypatch):
|
|
168
|
+
"""Keep a workstation's client certificate (and mTLS) out of the tests."""
|
|
169
|
+
monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@pytest.fixture
|
|
173
|
+
def agent(monkeypatch):
|
|
174
|
+
return _serve(monkeypatch)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def ask(server, message: str, conversation: str = "conv-1"):
|
|
178
|
+
async def call():
|
|
179
|
+
token = current_conversation.set(conversation)
|
|
180
|
+
try:
|
|
181
|
+
instance = server["instance"]
|
|
182
|
+
async with create_client_server_memory_streams() as (client, served):
|
|
183
|
+
async with anyio.create_task_group() as tg:
|
|
184
|
+
tg.start_soon(
|
|
185
|
+
instance.run, *served, instance.create_initialization_options()
|
|
186
|
+
)
|
|
187
|
+
async with ClientSession(*client) as session:
|
|
188
|
+
await session.initialize()
|
|
189
|
+
result = await session.call_tool("ask", {"message": message})
|
|
190
|
+
tg.cancel_scope.cancel()
|
|
191
|
+
return result
|
|
192
|
+
finally:
|
|
193
|
+
current_conversation.reset(token)
|
|
194
|
+
|
|
195
|
+
return anyio.run(call)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def text_of(result) -> str:
|
|
199
|
+
return "".join(block.text for block in result.content)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def is_error(result) -> bool:
|
|
203
|
+
# The field is isError on mcp 1.x and is_error on 2.x; both dump to isError.
|
|
204
|
+
return result.model_dump(by_alias=True)["isError"]
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
# A service account key: the token comes from fetch_id_token.
|
|
208
|
+
@patch("google.auth.default", return_value=(object(), "test-project"))
|
|
209
|
+
@patch("google.oauth2.id_token.fetch_id_token", return_value="id-token")
|
|
210
|
+
class TestRemoteAgent:
|
|
211
|
+
def test_returns_the_remote_reply(self, fetch_id_token, default, agent):
|
|
212
|
+
result = ask(remote_agent("weather", URL), "Weather in Paris?")
|
|
213
|
+
assert not is_error(result)
|
|
214
|
+
assert text_of(result) == "It's sunny."
|
|
215
|
+
|
|
216
|
+
def test_failed_task_is_a_tool_error_with_the_reason(
|
|
217
|
+
self, fetch_id_token, default, agent
|
|
218
|
+
):
|
|
219
|
+
result = ask(remote_agent("weather", URL), "fail")
|
|
220
|
+
assert is_error(result)
|
|
221
|
+
assert "failed: out of credits" in text_of(result)
|
|
222
|
+
|
|
223
|
+
def test_unreachable_agent_is_a_tool_error(
|
|
224
|
+
self, fetch_id_token, default, monkeypatch
|
|
225
|
+
):
|
|
226
|
+
def refuse(request):
|
|
227
|
+
raise httpx.ConnectError("connection refused")
|
|
228
|
+
|
|
229
|
+
monkeypatch.setattr(
|
|
230
|
+
httpx,
|
|
231
|
+
"AsyncClient",
|
|
232
|
+
functools.partial(httpx.AsyncClient, transport=httpx.MockTransport(refuse)),
|
|
233
|
+
)
|
|
234
|
+
result = ask(remote_agent("weather", URL), "hi")
|
|
235
|
+
assert is_error(result)
|
|
236
|
+
assert "connection refused" in text_of(result)
|
|
237
|
+
|
|
238
|
+
@pytest.mark.parametrize("on_agent_runtime", [True, False])
|
|
239
|
+
def test_401_from_agent_runtime_names_bind_id_token(
|
|
240
|
+
self, fetch_id_token, default, monkeypatch, on_agent_runtime
|
|
241
|
+
):
|
|
242
|
+
monkeypatch.setattr(
|
|
243
|
+
httpx,
|
|
244
|
+
"AsyncClient",
|
|
245
|
+
functools.partial(
|
|
246
|
+
httpx.AsyncClient,
|
|
247
|
+
transport=httpx.MockTransport(lambda request: httpx.Response(401)),
|
|
248
|
+
),
|
|
249
|
+
)
|
|
250
|
+
if on_agent_runtime:
|
|
251
|
+
monkeypatch.setenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", "123")
|
|
252
|
+
else:
|
|
253
|
+
monkeypatch.delenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", raising=False)
|
|
254
|
+
result = ask(remote_agent("weather", URL), "hi")
|
|
255
|
+
assert is_error(result)
|
|
256
|
+
assert ("bind_id_token=False" in text_of(result)) is on_agent_runtime
|
|
257
|
+
|
|
258
|
+
def test_id_token_goes_only_to_the_declared_origin(
|
|
259
|
+
self, fetch_id_token, default, agent
|
|
260
|
+
):
|
|
261
|
+
ask(remote_agent("weather", URL), "hi")
|
|
262
|
+
fetch_id_token.assert_called_once_with(
|
|
263
|
+
fetch_id_token.call_args.args[0], "https://callee-xyz.a.run.app"
|
|
264
|
+
)
|
|
265
|
+
hosts = {r.url.host for r in agent.transport.requests}
|
|
266
|
+
assert hosts == {"callee-xyz.a.run.app"}
|
|
267
|
+
assert all(
|
|
268
|
+
r.headers["authorization"] == "Bearer id-token"
|
|
269
|
+
for r in agent.transport.requests
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
def test_auth_opt_out_and_custom_headers(self, fetch_id_token, default, agent):
|
|
273
|
+
ask(remote_agent("weather", URL, auth=False, headers={"X-Key": "k"}), "hi")
|
|
274
|
+
fetch_id_token.assert_not_called()
|
|
275
|
+
request = agent.transport.requests[-1]
|
|
276
|
+
assert "authorization" not in request.headers
|
|
277
|
+
assert request.headers["x-key"] == "k"
|
|
278
|
+
|
|
279
|
+
@pytest.mark.parametrize(
|
|
280
|
+
"url",
|
|
281
|
+
[
|
|
282
|
+
THIRD_PARTY_URL,
|
|
283
|
+
# run.app in the host's middle, or over plain http.
|
|
284
|
+
"https://callee.run.app.evil.example/a2a/app",
|
|
285
|
+
"http://callee-xyz.a.run.app/a2a/app",
|
|
286
|
+
],
|
|
287
|
+
)
|
|
288
|
+
def test_no_token_by_default_to_a_host_not_google_hosted(
|
|
289
|
+
self, fetch_id_token, default, agent, url
|
|
290
|
+
):
|
|
291
|
+
result = ask(remote_agent("weather", url), "hi")
|
|
292
|
+
assert text_of(result) == "It's sunny."
|
|
293
|
+
default.assert_not_called()
|
|
294
|
+
fetch_id_token.assert_not_called()
|
|
295
|
+
assert all("authorization" not in r.headers for r in agent.transport.requests)
|
|
296
|
+
|
|
297
|
+
def test_auth_true_sends_a_token_to_any_host(
|
|
298
|
+
self, fetch_id_token, default, agent, caplog
|
|
299
|
+
):
|
|
300
|
+
with caplog.at_level(logging.WARNING):
|
|
301
|
+
ask(remote_agent("weather", THIRD_PARTY_URL, auth=True), "hi")
|
|
302
|
+
fetch_id_token.assert_called_once_with(
|
|
303
|
+
fetch_id_token.call_args.args[0], "https://callee.example"
|
|
304
|
+
)
|
|
305
|
+
_, headers = agent.requests[-1]
|
|
306
|
+
assert headers["authorization"] == "Bearer id-token"
|
|
307
|
+
# A service account key's token is bound to the host: no warning.
|
|
308
|
+
assert not caplog.records
|
|
309
|
+
|
|
310
|
+
def test_follow_ups_reuse_the_remote_context(self, fetch_id_token, default, agent):
|
|
311
|
+
server = remote_agent("weather", URL)
|
|
312
|
+
ask(server, "first", conversation="conv-1")
|
|
313
|
+
ask(server, "follow-up", conversation="conv-1")
|
|
314
|
+
ask(server, "other", conversation="conv-2")
|
|
315
|
+
first, follow_up, other = (context for context, _ in agent.requests)
|
|
316
|
+
assert follow_up == first
|
|
317
|
+
assert other != first
|
|
318
|
+
|
|
319
|
+
def test_does_not_carry_the_trace_context(self, fetch_id_token, default, agent):
|
|
320
|
+
# The remote agent records a trace of its own.
|
|
321
|
+
tracer = TracerProvider().get_tracer(__name__)
|
|
322
|
+
with tracer.start_as_current_span("turn"):
|
|
323
|
+
ask(remote_agent("weather", URL), "hi")
|
|
324
|
+
_, headers = agent.requests[-1]
|
|
325
|
+
assert "traceparent" not in headers
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
class FakeMetadataIDToken:
|
|
329
|
+
"""compute_engine.IDTokenCredentials, recording how it was asked for."""
|
|
330
|
+
|
|
331
|
+
created: ClassVar[list[tuple[str, dict]]] = []
|
|
332
|
+
|
|
333
|
+
def __init__(self, request, target_audience, **kwargs) -> None:
|
|
334
|
+
self.created.append((target_audience, kwargs))
|
|
335
|
+
self.token: str | None = None
|
|
336
|
+
|
|
337
|
+
def refresh(self, request) -> None:
|
|
338
|
+
self.token = "metadata-id-token"
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
@pytest.mark.parametrize("bind", [True, False])
|
|
342
|
+
@patch("google.auth.compute_engine.IDTokenCredentials", FakeMetadataIDToken)
|
|
343
|
+
def test_metadata_server_id_token_binding_follows_bind_id_token(agent, bind):
|
|
344
|
+
# With Agent Identity, google-auth binds ID tokens to the agent's certificate,
|
|
345
|
+
# which Cloud Run rejects, so callers to Cloud Run turn binding off.
|
|
346
|
+
from google.auth import compute_engine
|
|
347
|
+
|
|
348
|
+
FakeMetadataIDToken.created.clear()
|
|
349
|
+
with patch("google.auth.default", return_value=(compute_engine.Credentials(), "p")):
|
|
350
|
+
result = ask(remote_agent("weather", URL, bind_id_token=bind), "hi")
|
|
351
|
+
|
|
352
|
+
assert text_of(result) == "It's sunny."
|
|
353
|
+
audience, kwargs = FakeMetadataIDToken.created[0]
|
|
354
|
+
assert audience == "https://callee-xyz.a.run.app"
|
|
355
|
+
assert kwargs == {"use_metadata_identity_endpoint": True, "bind_id_token": bind}
|
|
356
|
+
_, headers = agent.requests[-1]
|
|
357
|
+
assert headers["authorization"] == "Bearer metadata-id-token"
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
class FakeUserCredentials(google.oauth2.credentials.Credentials):
|
|
361
|
+
"""`gcloud auth application-default login` credentials and their ID token."""
|
|
362
|
+
|
|
363
|
+
def refresh(self, request) -> None:
|
|
364
|
+
self._id_token = "user-id-token"
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
class TestUserCredentials:
|
|
368
|
+
@patch("google.auth.default", return_value=(FakeUserCredentials(None), "p"))
|
|
369
|
+
def test_auth_true_to_another_host_warns_the_token_is_unbound(
|
|
370
|
+
self, default, agent, caplog
|
|
371
|
+
):
|
|
372
|
+
server = remote_agent("weather", THIRD_PARTY_URL, auth=True)
|
|
373
|
+
with caplog.at_level(logging.WARNING):
|
|
374
|
+
ask(server, "hi")
|
|
375
|
+
ask(server, "again")
|
|
376
|
+
assert all(
|
|
377
|
+
r.headers["authorization"] == "Bearer user-id-token"
|
|
378
|
+
for r in agent.transport.requests
|
|
379
|
+
)
|
|
380
|
+
(record,) = caplog.records
|
|
381
|
+
assert "callee.example" in record.getMessage()
|
|
382
|
+
assert "isn't bound" in record.getMessage()
|
|
383
|
+
|
|
384
|
+
@patch("google.auth.default", return_value=(FakeUserCredentials(None), "p"))
|
|
385
|
+
def test_cloud_run_by_default_without_a_warning(self, default, agent, caplog):
|
|
386
|
+
with caplog.at_level(logging.WARNING):
|
|
387
|
+
ask(remote_agent("weather", URL), "hi")
|
|
388
|
+
_, headers = agent.requests[-1]
|
|
389
|
+
assert headers["authorization"] == "Bearer user-id-token"
|
|
390
|
+
assert not caplog.records
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
class FakeCredentials:
|
|
394
|
+
"""ADC that hand out a new access token on each refresh."""
|
|
395
|
+
|
|
396
|
+
def __init__(self) -> None:
|
|
397
|
+
self.valid = False
|
|
398
|
+
self.token: str | None = None
|
|
399
|
+
self.refreshes = 0
|
|
400
|
+
|
|
401
|
+
def refresh(self, request) -> None:
|
|
402
|
+
self.refreshes += 1
|
|
403
|
+
self.token = f"access-token-{self.refreshes}"
|
|
404
|
+
self.valid = True
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
@patch("google.oauth2.id_token.fetch_id_token", return_value="id-token")
|
|
408
|
+
class TestAgentRuntimeAgent:
|
|
409
|
+
def test_sends_a_fresh_access_token_to_the_passthrough(
|
|
410
|
+
self, fetch_id_token, monkeypatch
|
|
411
|
+
):
|
|
412
|
+
agent = _serve(monkeypatch, AGENT_RUNTIME_API)
|
|
413
|
+
credentials = FakeCredentials()
|
|
414
|
+
tracer = TracerProvider().get_tracer(__name__)
|
|
415
|
+
with patch("google.auth.default", return_value=(credentials, "p")) as default:
|
|
416
|
+
server = remote_agent("weather", AGENT_RUNTIME_URL)
|
|
417
|
+
with tracer.start_as_current_span("turn"):
|
|
418
|
+
assert text_of(ask(server, "hi")) == "It's sunny."
|
|
419
|
+
# An expired token is refreshed, not reused.
|
|
420
|
+
credentials.valid = False
|
|
421
|
+
ask(server, "again")
|
|
422
|
+
|
|
423
|
+
fetch_id_token.assert_not_called()
|
|
424
|
+
assert default.call_args.kwargs["scopes"] == [
|
|
425
|
+
"https://www.googleapis.com/auth/cloud-platform"
|
|
426
|
+
]
|
|
427
|
+
first, second = agent.requests
|
|
428
|
+
assert "traceparent" not in first[1]
|
|
429
|
+
assert first[1]["authorization"] == "Bearer access-token-1"
|
|
430
|
+
assert second[1]["authorization"] == "Bearer access-token-2"
|
|
431
|
+
# The card's off-origin endpoint is pinned to the passthrough host.
|
|
432
|
+
assert {r.url.host for r in agent.transport.requests} == {
|
|
433
|
+
"us-central1-aiplatform.googleapis.com"
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
def test_goes_over_mtls_with_an_agent_identity_certificate(
|
|
437
|
+
self, fetch_id_token, monkeypatch
|
|
438
|
+
):
|
|
439
|
+
agent = _serve(monkeypatch, AGENT_RUNTIME_API)
|
|
440
|
+
context = ssl.create_default_context()
|
|
441
|
+
clients: list[dict] = []
|
|
442
|
+
serve = httpx.AsyncClient # already bound to the in-process transport
|
|
443
|
+
|
|
444
|
+
def client(**kwargs):
|
|
445
|
+
clients.append(kwargs)
|
|
446
|
+
return serve(**kwargs)
|
|
447
|
+
|
|
448
|
+
monkeypatch.setattr(httpx, "AsyncClient", client)
|
|
449
|
+
with (
|
|
450
|
+
patch("google.auth.default", return_value=(FakeCredentials(), "p")),
|
|
451
|
+
patch(
|
|
452
|
+
"google.auth.transport.mtls.get_default_ssl_context",
|
|
453
|
+
return_value=context,
|
|
454
|
+
),
|
|
455
|
+
):
|
|
456
|
+
result = ask(remote_agent("weather", AGENT_RUNTIME_URL), "hi")
|
|
457
|
+
|
|
458
|
+
assert text_of(result) == "It's sunny."
|
|
459
|
+
assert clients[0]["verify"] is context
|
|
460
|
+
assert {r.url.host for r in agent.transport.requests} == {
|
|
461
|
+
"us-central1-aiplatform.mtls.googleapis.com"
|
|
462
|
+
}
|
|
463
|
+
assert all(
|
|
464
|
+
r.headers["authorization"] == "Bearer access-token-1"
|
|
465
|
+
for r in agent.transport.requests
|
|
466
|
+
)
|
|
467
|
+
|
|
468
|
+
def test_resource_url_without_the_passthrough_is_rejected(self, fetch_id_token):
|
|
469
|
+
resource = (
|
|
470
|
+
"https://us-central1-aiplatform.googleapis.com/v1/projects/p/"
|
|
471
|
+
"locations/us-central1/reasoningEngines/1"
|
|
472
|
+
)
|
|
473
|
+
with pytest.raises(ValueError, match="passthrough URL"):
|
|
474
|
+
remote_agent("weather", resource)
|
|
475
|
+
|
|
476
|
+
def test_an_access_token_never_goes_to_another_host(
|
|
477
|
+
self, fetch_id_token, monkeypatch
|
|
478
|
+
):
|
|
479
|
+
# Agent Runtime's host and path, in the path of some other host.
|
|
480
|
+
prefix = "/us-central1-aiplatform.googleapis.com/reasoningEngines/1/api"
|
|
481
|
+
agent = _serve(monkeypatch, prefix)
|
|
482
|
+
lookalike = f"https://callee-xyz.a.run.app{prefix}/a2a/app"
|
|
483
|
+
with patch("google.auth.default", return_value=(object(), "p")):
|
|
484
|
+
ask(remote_agent("weather", lookalike), "hi")
|
|
485
|
+
fetch_id_token.assert_called_once()
|
|
486
|
+
_, headers = agent.requests[-1]
|
|
487
|
+
assert headers["authorization"] == "Bearer id-token"
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
def test_tools_see_the_turns_conversation(monkeypatch, tmp_path):
|
|
491
|
+
"""Two callers sending the same context id don't share a remote conversation."""
|
|
492
|
+
monkeypatch.setenv("ANTHROPIC_VERTEX_PROJECT_ID", "test-project")
|
|
493
|
+
monkeypatch.setattr(claude, "load_dotenv", lambda: None) # keep .env out
|
|
494
|
+
seen = []
|
|
495
|
+
|
|
496
|
+
async def query(prompt, options):
|
|
497
|
+
seen.append(current_conversation.get())
|
|
498
|
+
return
|
|
499
|
+
yield
|
|
500
|
+
|
|
501
|
+
async def turn(user_id=None):
|
|
502
|
+
async for _ in claude.stream_turn(
|
|
503
|
+
"hi",
|
|
504
|
+
ClaudeAgentOptions(model="claude-test", cwd=str(tmp_path)),
|
|
505
|
+
"c-9",
|
|
506
|
+
user_id,
|
|
507
|
+
):
|
|
508
|
+
pass
|
|
509
|
+
|
|
510
|
+
with patch("claude_agent_sdk.query", query):
|
|
511
|
+
anyio.run(turn)
|
|
512
|
+
anyio.run(turn, "ana")
|
|
513
|
+
anyio.run(turn, "ben")
|
|
514
|
+
anyio.run(turn, "ana")
|
|
515
|
+
assert seen[0] == "c-9"
|
|
516
|
+
assert seen[1] == seen[3] != seen[2]
|
|
517
|
+
assert current_conversation.get() is None
|