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,625 @@
|
|
|
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
|
+
"""Memory Bank in `app/app_utils/memory_bank.py`, with the SDK's `query` and the
|
|
16
|
+
Agent Platform client faked."""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import datetime
|
|
21
|
+
import json
|
|
22
|
+
import sys
|
|
23
|
+
import time
|
|
24
|
+
from unittest.mock import AsyncMock, MagicMock, patch
|
|
25
|
+
|
|
26
|
+
import anyio
|
|
27
|
+
import fsspec
|
|
28
|
+
import pytest
|
|
29
|
+
from agentplatform import types
|
|
30
|
+
from claude_agent_sdk import (
|
|
31
|
+
AssistantMessage,
|
|
32
|
+
ClaudeAgentOptions,
|
|
33
|
+
ResultMessage,
|
|
34
|
+
TextBlock,
|
|
35
|
+
ThinkingBlock,
|
|
36
|
+
ToolResultBlock,
|
|
37
|
+
ToolUseBlock,
|
|
38
|
+
UserMessage,
|
|
39
|
+
)
|
|
40
|
+
from openinference.instrumentation import TraceConfig
|
|
41
|
+
from openinference.instrumentation.claude_agent_sdk import ClaudeAgentSDKInstrumentor
|
|
42
|
+
from opentelemetry.sdk.trace import TracerProvider
|
|
43
|
+
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
|
|
44
|
+
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
|
|
45
|
+
InMemorySpanExporter,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
from app.app_utils import bq_analytics, claude, completions, memory_bank, turn_options
|
|
49
|
+
from app.app_utils.telemetry.config import agent_name
|
|
50
|
+
|
|
51
|
+
pytestmark = pytest.mark.usefixtures("isolated")
|
|
52
|
+
|
|
53
|
+
_UPDATED = datetime.datetime(2026, 1, 2, 3, 4, 5, tzinfo=datetime.UTC)
|
|
54
|
+
_MEMORIES = [
|
|
55
|
+
types.RetrieveMemoriesResponseRetrievedMemory(
|
|
56
|
+
memory=types.Memory(fact="Likes green tea.", update_time=_UPDATED)
|
|
57
|
+
),
|
|
58
|
+
types.RetrieveMemoriesResponseRetrievedMemory(
|
|
59
|
+
memory=types.Memory(fact="Lives in Oslo.")
|
|
60
|
+
),
|
|
61
|
+
# Skipped: nothing to show.
|
|
62
|
+
types.RetrieveMemoriesResponseRetrievedMemory(memory=types.Memory()),
|
|
63
|
+
types.RetrieveMemoriesResponseRetrievedMemory(),
|
|
64
|
+
]
|
|
65
|
+
_LINES = f"Time: {_UPDATED.isoformat()}\nuser: Likes green tea.\nuser: Lives in Oslo."
|
|
66
|
+
_PAST = (
|
|
67
|
+
"The following content is from your previous conversations with the user.\n"
|
|
68
|
+
"They may be useful for answering the user's current query.\n"
|
|
69
|
+
f"<PAST_CONVERSATIONS>\n{_LINES}\n</PAST_CONVERSATIONS>\n"
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@pytest.fixture
|
|
74
|
+
def memory_on(isolated, monkeypatch):
|
|
75
|
+
"""Memory on, in an Agent Runtime deployment."""
|
|
76
|
+
monkeypatch.setenv("MEMORY_BANK_ENABLED", "true")
|
|
77
|
+
monkeypatch.setenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", "123")
|
|
78
|
+
monkeypatch.setenv("GOOGLE_CLOUD_AGENT_ENGINE_LOCATION", "us-central1")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _fake_client(client_class: MagicMock, memories=()) -> MagicMock:
|
|
82
|
+
"""Give the patched agentplatform.Client a Memory Bank; return its async client."""
|
|
83
|
+
aio = client_class.return_value.aio
|
|
84
|
+
aio.__aenter__.return_value = aio
|
|
85
|
+
aio.memory_banks.memories.retrieve = AsyncMock(
|
|
86
|
+
side_effect=lambda **_: _pager(memories)
|
|
87
|
+
)
|
|
88
|
+
aio.memory_banks.ingest_events = AsyncMock()
|
|
89
|
+
return aio
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
async def _pager(items):
|
|
93
|
+
for item in items:
|
|
94
|
+
yield item
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _result(**overrides) -> ResultMessage:
|
|
98
|
+
fields = {
|
|
99
|
+
"subtype": "success",
|
|
100
|
+
"duration_ms": 1,
|
|
101
|
+
"duration_api_ms": 1,
|
|
102
|
+
"is_error": False,
|
|
103
|
+
"num_turns": 1,
|
|
104
|
+
"session_id": "s-1",
|
|
105
|
+
"result": "Done.",
|
|
106
|
+
}
|
|
107
|
+
return ResultMessage(**{**fields, **overrides})
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _turn(options: ClaudeAgentOptions | None = None) -> None:
|
|
111
|
+
"""One turn of alice's through stream_turn, then its pending ingest."""
|
|
112
|
+
|
|
113
|
+
async def run() -> None:
|
|
114
|
+
async for _ in claude.stream_turn(
|
|
115
|
+
"Any tea?", options or ClaudeAgentOptions(), "conv-1", "alice"
|
|
116
|
+
):
|
|
117
|
+
pass
|
|
118
|
+
await memory_bank.flush()
|
|
119
|
+
|
|
120
|
+
anyio.run(run)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _system_prompt_sent(query: MagicMock, options: ClaudeAgentOptions) -> object:
|
|
124
|
+
"""The system prompt the CLI got for one turn run with `options`.
|
|
125
|
+
|
|
126
|
+
`query` is the test's patched `claude_agent_sdk.query`.
|
|
127
|
+
"""
|
|
128
|
+
seen = []
|
|
129
|
+
|
|
130
|
+
async def record(*, prompt, options):
|
|
131
|
+
seen.append(options.system_prompt)
|
|
132
|
+
yield _result()
|
|
133
|
+
|
|
134
|
+
query.side_effect = record
|
|
135
|
+
_turn(options)
|
|
136
|
+
[system_prompt] = seen
|
|
137
|
+
return system_prompt
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@pytest.mark.parametrize(
|
|
141
|
+
"system_prompt, expected",
|
|
142
|
+
[
|
|
143
|
+
pytest.param(
|
|
144
|
+
"Be brief.",
|
|
145
|
+
{"type": "custom", "prompt": f"Be brief.\n\n{_PAST}", "snapshot": False},
|
|
146
|
+
id="string",
|
|
147
|
+
),
|
|
148
|
+
# The SDK sends no system prompt as an empty custom one.
|
|
149
|
+
pytest.param(
|
|
150
|
+
None, {"type": "custom", "prompt": _PAST, "snapshot": False}, id="none"
|
|
151
|
+
),
|
|
152
|
+
pytest.param(
|
|
153
|
+
{"type": "preset", "preset": "claude_code"},
|
|
154
|
+
{
|
|
155
|
+
"type": "preset",
|
|
156
|
+
"preset": "claude_code",
|
|
157
|
+
"append": _PAST,
|
|
158
|
+
"snapshot": False,
|
|
159
|
+
},
|
|
160
|
+
id="preset",
|
|
161
|
+
),
|
|
162
|
+
pytest.param(
|
|
163
|
+
{"type": "preset", "preset": "claude_code", "append": "Be brief."},
|
|
164
|
+
{
|
|
165
|
+
"type": "preset",
|
|
166
|
+
"preset": "claude_code",
|
|
167
|
+
"append": f"Be brief.\n\n{_PAST}",
|
|
168
|
+
"snapshot": False,
|
|
169
|
+
},
|
|
170
|
+
id="preset_with_append",
|
|
171
|
+
),
|
|
172
|
+
pytest.param(
|
|
173
|
+
{"type": "custom", "prompt": "Be brief."},
|
|
174
|
+
{"type": "custom", "prompt": f"Be brief.\n\n{_PAST}", "snapshot": False},
|
|
175
|
+
id="custom",
|
|
176
|
+
),
|
|
177
|
+
],
|
|
178
|
+
)
|
|
179
|
+
@pytest.mark.usefixtures("memory_on")
|
|
180
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
181
|
+
@patch("agentplatform.Client")
|
|
182
|
+
def test_the_users_memories_are_recalled_into_the_system_prompt(
|
|
183
|
+
client_class, query, system_prompt, expected
|
|
184
|
+
):
|
|
185
|
+
aio = _fake_client(client_class, _MEMORIES)
|
|
186
|
+
|
|
187
|
+
sent = _system_prompt_sent(query, ClaudeAgentOptions(system_prompt=system_prompt))
|
|
188
|
+
|
|
189
|
+
assert sent == expected
|
|
190
|
+
aio.memory_banks.memories.retrieve.assert_awaited_once_with(
|
|
191
|
+
name="reasoningEngines/123",
|
|
192
|
+
scope={"app_name": agent_name(), "user_id": "alice"},
|
|
193
|
+
similarity_search_params={"search_query": "Any tea?"},
|
|
194
|
+
)
|
|
195
|
+
client_class.assert_called_with(location="us-central1")
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
_ANSWER = [
|
|
199
|
+
AssistantMessage(
|
|
200
|
+
content=[TextBlock(text="Green tea.")], model="m", message_id="m1"
|
|
201
|
+
),
|
|
202
|
+
_result(),
|
|
203
|
+
]
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@pytest.mark.usefixtures("memory_on")
|
|
207
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
208
|
+
@patch("agentplatform.Client")
|
|
209
|
+
@patch.object(bq_analytics, "writer")
|
|
210
|
+
def test_bigquery_logs_the_recalled_memories_in_the_models_system_prompt(
|
|
211
|
+
writer, client_class, query
|
|
212
|
+
):
|
|
213
|
+
_fake_client(client_class, _MEMORIES)
|
|
214
|
+
rows: list[dict] = []
|
|
215
|
+
writer.return_value = MagicMock(put=rows.append)
|
|
216
|
+
query.side_effect = _fake_query(_ANSWER)
|
|
217
|
+
|
|
218
|
+
_turn(ClaudeAgentOptions(system_prompt="Be brief."))
|
|
219
|
+
|
|
220
|
+
request = next(r for r in rows if r["event_type"] == "LLM_REQUEST")
|
|
221
|
+
assert json.loads(request["content"])["system_prompt"] == f"Be brief.\n\n{_PAST}"
|
|
222
|
+
# As ADK's, the agent's instruction leaves the memories out.
|
|
223
|
+
starting = next(r for r in rows if r["event_type"] == "AGENT_STARTING")
|
|
224
|
+
assert json.loads(starting["content"]) == {"text_summary": "Be brief."}
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
@pytest.mark.usefixtures("memory_on")
|
|
228
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
229
|
+
@patch("agentplatform.Client")
|
|
230
|
+
def test_the_prompt_log_has_the_recalled_memories_in_the_system_instruction(
|
|
231
|
+
client_class, query, monkeypatch
|
|
232
|
+
):
|
|
233
|
+
_fake_client(client_class, _MEMORIES)
|
|
234
|
+
monkeypatch.setenv("OTEL_INSTRUMENTATION_GENAI_COMPLETION_HOOK", "upload")
|
|
235
|
+
monkeypatch.setenv(
|
|
236
|
+
"OTEL_INSTRUMENTATION_GENAI_UPLOAD_BASE_PATH", "memory://logs/completions"
|
|
237
|
+
)
|
|
238
|
+
events: list = []
|
|
239
|
+
monkeypatch.setattr(completions.telemetry, "emit_event", events.append)
|
|
240
|
+
query.side_effect = _fake_query(_ANSWER)
|
|
241
|
+
|
|
242
|
+
_turn(ClaudeAgentOptions(system_prompt="Be brief."))
|
|
243
|
+
|
|
244
|
+
[event] = events
|
|
245
|
+
assert _uploaded(event.attributes["gen_ai.system_instructions_ref"]) == [
|
|
246
|
+
{"type": "text", "content": f"Be brief.\n\n{_PAST}"}
|
|
247
|
+
]
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _uploaded(path: str) -> object:
|
|
251
|
+
"""The JSON file at `path`, once the hook's upload thread has written it."""
|
|
252
|
+
fs, _ = fsspec.url_to_fs(path)
|
|
253
|
+
deadline = time.monotonic() + 5
|
|
254
|
+
# The memory filesystem creates the file, empty, when it's opened to write.
|
|
255
|
+
while not fs.exists(path) or not fs.cat_file(path):
|
|
256
|
+
assert time.monotonic() < deadline, f"{path} was never uploaded"
|
|
257
|
+
time.sleep(0.01)
|
|
258
|
+
return json.loads(fs.cat_file(path))
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
@pytest.mark.parametrize(
|
|
262
|
+
"capture, memories, system_prompt, expected",
|
|
263
|
+
[
|
|
264
|
+
pytest.param(
|
|
265
|
+
"true",
|
|
266
|
+
_MEMORIES,
|
|
267
|
+
"Be brief.",
|
|
268
|
+
{
|
|
269
|
+
"ClaudeAgentSDK.query": [
|
|
270
|
+
{"type": "text", "content": f"Be brief.\n\n{_PAST}"}
|
|
271
|
+
]
|
|
272
|
+
},
|
|
273
|
+
id="capture_on",
|
|
274
|
+
),
|
|
275
|
+
pytest.param(None, _MEMORIES, "Be brief.", {}, id="capture_off"),
|
|
276
|
+
pytest.param("true", [], None, {}, id="no_system_prompt"),
|
|
277
|
+
],
|
|
278
|
+
)
|
|
279
|
+
@pytest.mark.usefixtures("memory_on")
|
|
280
|
+
# The instrumentor wraps query() where the SDK defines it, then
|
|
281
|
+
# re-exports the wrapper; uninstrument() re-exports the original.
|
|
282
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
283
|
+
@patch.object(sys.modules["claude_agent_sdk.query"], "query")
|
|
284
|
+
@patch("agentplatform.Client")
|
|
285
|
+
def test_the_turn_span_has_the_system_prompt_with_memories_only_on_capture(
|
|
286
|
+
client_class,
|
|
287
|
+
defined_query,
|
|
288
|
+
_exported_query,
|
|
289
|
+
monkeypatch,
|
|
290
|
+
capture,
|
|
291
|
+
memories,
|
|
292
|
+
system_prompt,
|
|
293
|
+
expected,
|
|
294
|
+
):
|
|
295
|
+
_fake_client(client_class, memories)
|
|
296
|
+
defined_query.side_effect = _fake_query(_SUBAGENT_TURN)
|
|
297
|
+
if capture:
|
|
298
|
+
monkeypatch.setenv(
|
|
299
|
+
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT", capture
|
|
300
|
+
)
|
|
301
|
+
else:
|
|
302
|
+
monkeypatch.delenv(
|
|
303
|
+
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT", raising=False
|
|
304
|
+
)
|
|
305
|
+
monkeypatch.setenv("OPENINFERENCE_ENABLE_GENAI_SEMCONV", "true")
|
|
306
|
+
exporter = InMemorySpanExporter()
|
|
307
|
+
provider = TracerProvider()
|
|
308
|
+
provider.add_span_processor(SimpleSpanProcessor(exporter))
|
|
309
|
+
instrumentor = ClaudeAgentSDKInstrumentor()
|
|
310
|
+
instrumentor.instrument(tracer_provider=provider, config=TraceConfig())
|
|
311
|
+
try:
|
|
312
|
+
_turn(ClaudeAgentOptions(system_prompt=system_prompt))
|
|
313
|
+
finally:
|
|
314
|
+
instrumentor.uninstrument()
|
|
315
|
+
|
|
316
|
+
spans = exporter.get_finished_spans()
|
|
317
|
+
assert {"ClaudeAgentSDK.query", "get_weather"} <= {s.name for s in spans}
|
|
318
|
+
assert {
|
|
319
|
+
s.name: json.loads(s.attributes["gen_ai.system_instructions"])
|
|
320
|
+
for s in spans
|
|
321
|
+
if s.attributes and "gen_ai.system_instructions" in s.attributes
|
|
322
|
+
} == expected
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
@pytest.mark.usefixtures("memory_on")
|
|
326
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
327
|
+
@patch("agentplatform.Client")
|
|
328
|
+
def test_without_memories_the_system_prompt_is_unchanged(client_class, query):
|
|
329
|
+
_fake_client(client_class, [])
|
|
330
|
+
|
|
331
|
+
sent = _system_prompt_sent(query, ClaudeAgentOptions(system_prompt="Be brief."))
|
|
332
|
+
|
|
333
|
+
assert sent == "Be brief."
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
@pytest.mark.usefixtures("memory_on")
|
|
337
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
338
|
+
@patch("agentplatform.Client")
|
|
339
|
+
def test_a_failed_recall_runs_the_turn_without_memories(client_class, query, caplog):
|
|
340
|
+
aio = _fake_client(client_class)
|
|
341
|
+
aio.memory_banks.memories.retrieve.side_effect = RuntimeError("quota exceeded")
|
|
342
|
+
|
|
343
|
+
sent = _system_prompt_sent(query, ClaudeAgentOptions(system_prompt="Be brief."))
|
|
344
|
+
|
|
345
|
+
assert sent == "Be brief."
|
|
346
|
+
assert "quota exceeded" in caplog.text
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
@pytest.mark.usefixtures("memory_on")
|
|
350
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
351
|
+
@patch("agentplatform.Client")
|
|
352
|
+
def test_a_system_prompt_file_gets_no_memories(client_class, query, caplog):
|
|
353
|
+
_fake_client(client_class, _MEMORIES)
|
|
354
|
+
from_file = {"type": "file", "path": "prompt.md"}
|
|
355
|
+
|
|
356
|
+
assert _system_prompt_sent(query, ClaudeAgentOptions(system_prompt=from_file)) == (
|
|
357
|
+
from_file
|
|
358
|
+
)
|
|
359
|
+
assert "system prompt file" in caplog.text
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
@pytest.mark.parametrize("enabled", [None, "false"])
|
|
363
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
364
|
+
@patch("agentplatform.Client")
|
|
365
|
+
def test_with_memory_off_the_turn_never_reaches_memory_bank(
|
|
366
|
+
client_class, query, monkeypatch, enabled
|
|
367
|
+
):
|
|
368
|
+
if enabled is not None:
|
|
369
|
+
monkeypatch.setenv("MEMORY_BANK_ENABLED", enabled)
|
|
370
|
+
|
|
371
|
+
sent = _system_prompt_sent(query, ClaudeAgentOptions(system_prompt="Be brief."))
|
|
372
|
+
|
|
373
|
+
assert sent == "Be brief."
|
|
374
|
+
client_class.assert_not_called()
|
|
375
|
+
assert not memory_bank.enabled()
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
@pytest.mark.usefixtures("memory_on")
|
|
379
|
+
def test_with_memory_on_the_search_tool_is_offered():
|
|
380
|
+
assert memory_bank.enabled()
|
|
381
|
+
servers = memory_bank.mcp_servers()
|
|
382
|
+
|
|
383
|
+
assert list(servers) == ["memory"]
|
|
384
|
+
assert servers["memory"]["type"] == "sdk"
|
|
385
|
+
assert memory_bank.allowed_tools() == ["mcp__memory__search_memory"]
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
@pytest.mark.parametrize(
|
|
389
|
+
"engine_name, display_name",
|
|
390
|
+
[(None, agent_name()), ("shared-memory", "shared-memory")],
|
|
391
|
+
)
|
|
392
|
+
@pytest.mark.parametrize("exists", [True, False])
|
|
393
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
394
|
+
@patch("agentplatform.Client")
|
|
395
|
+
def test_off_agent_runtime_the_engine_is_found_by_name_or_created_once(
|
|
396
|
+
client_class, query, monkeypatch, engine_name, display_name, exists
|
|
397
|
+
):
|
|
398
|
+
monkeypatch.setenv("MEMORY_BANK_ENABLED", "true")
|
|
399
|
+
if engine_name:
|
|
400
|
+
monkeypatch.setenv("MEMORY_BANK_ENGINE_NAME", engine_name)
|
|
401
|
+
name = "projects/p/locations/us-east1/reasoningEngines/9"
|
|
402
|
+
engine = MagicMock(api_resource=types.ReasoningEngine(name=name))
|
|
403
|
+
runtimes = client_class.return_value.runtimes
|
|
404
|
+
runtimes.list.return_value = [engine] if exists else []
|
|
405
|
+
runtimes.create.return_value = engine
|
|
406
|
+
aio = _fake_client(client_class, _MEMORIES)
|
|
407
|
+
|
|
408
|
+
for _ in range(2):
|
|
409
|
+
_system_prompt_sent(query, ClaudeAgentOptions())
|
|
410
|
+
|
|
411
|
+
runtimes.list.assert_called_once_with(
|
|
412
|
+
config={"filter": f"display_name={display_name}"}
|
|
413
|
+
)
|
|
414
|
+
if exists:
|
|
415
|
+
runtimes.create.assert_not_called()
|
|
416
|
+
else:
|
|
417
|
+
runtimes.create.assert_called_once_with(config={"display_name": display_name})
|
|
418
|
+
retrieves = aio.memory_banks.memories.retrieve.call_args_list
|
|
419
|
+
assert [call.kwargs["name"] for call in retrieves] == [name, name]
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
@pytest.mark.parametrize(
|
|
423
|
+
"env, location",
|
|
424
|
+
[
|
|
425
|
+
pytest.param(
|
|
426
|
+
{
|
|
427
|
+
"GOOGLE_CLOUD_AGENT_ENGINE_LOCATION": "europe-west4",
|
|
428
|
+
"GOOGLE_CLOUD_LOCATION": "us-central1",
|
|
429
|
+
},
|
|
430
|
+
"europe-west4",
|
|
431
|
+
id="agent_runtime",
|
|
432
|
+
),
|
|
433
|
+
pytest.param(
|
|
434
|
+
{"GOOGLE_CLOUD_LOCATION": "us-central1"}, "us-central1", id="regional"
|
|
435
|
+
),
|
|
436
|
+
# Agent Runtime has no global location.
|
|
437
|
+
pytest.param({"GOOGLE_CLOUD_LOCATION": "global"}, "us-east1", id="global"),
|
|
438
|
+
],
|
|
439
|
+
)
|
|
440
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
441
|
+
@patch("agentplatform.Client")
|
|
442
|
+
def test_the_engine_is_in_an_agent_runtime_region(
|
|
443
|
+
client_class, query, monkeypatch, env, location
|
|
444
|
+
):
|
|
445
|
+
monkeypatch.setenv("MEMORY_BANK_ENABLED", "true")
|
|
446
|
+
monkeypatch.delenv("GOOGLE_CLOUD_AGENT_ENGINE_LOCATION", raising=False)
|
|
447
|
+
for key, value in env.items():
|
|
448
|
+
monkeypatch.setenv(key, value)
|
|
449
|
+
client_class.return_value.runtimes.list.return_value = [
|
|
450
|
+
MagicMock(api_resource=types.ReasoningEngine(name="reasoningEngines/9"))
|
|
451
|
+
]
|
|
452
|
+
_fake_client(client_class)
|
|
453
|
+
|
|
454
|
+
_system_prompt_sent(query, ClaudeAgentOptions())
|
|
455
|
+
|
|
456
|
+
assert {call.kwargs["location"] for call in client_class.call_args_list} == {
|
|
457
|
+
location
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
_DELEGATE = {"subagent_type": "forecaster", "prompt": "Weather in Oslo?"}
|
|
462
|
+
# The main agent starts a background subagent, which calls a tool of its own.
|
|
463
|
+
_SUBAGENT_TURN = [
|
|
464
|
+
AssistantMessage(
|
|
465
|
+
content=[
|
|
466
|
+
TextBlock(text="Asking the forecaster."),
|
|
467
|
+
ToolUseBlock(id="a1", name="Agent", input=_DELEGATE),
|
|
468
|
+
],
|
|
469
|
+
model="m",
|
|
470
|
+
message_id="m1",
|
|
471
|
+
),
|
|
472
|
+
UserMessage(content=[ToolResultBlock(tool_use_id="a1", content="Started.")]),
|
|
473
|
+
AssistantMessage(
|
|
474
|
+
content=[ToolUseBlock(id="t1", name="get_weather", input={"city": "Oslo"})],
|
|
475
|
+
model="m",
|
|
476
|
+
message_id="s1",
|
|
477
|
+
parent_tool_use_id="a1",
|
|
478
|
+
),
|
|
479
|
+
UserMessage(
|
|
480
|
+
content=[ToolResultBlock(tool_use_id="t1", content="Sunny.")],
|
|
481
|
+
parent_tool_use_id="a1",
|
|
482
|
+
),
|
|
483
|
+
AssistantMessage(
|
|
484
|
+
content=[TextBlock(text="It's sunny.")], model="m", message_id="m2"
|
|
485
|
+
),
|
|
486
|
+
_result(),
|
|
487
|
+
]
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
def _fake_query(messages):
|
|
491
|
+
async def query(*, prompt, options):
|
|
492
|
+
for message in messages:
|
|
493
|
+
yield message
|
|
494
|
+
|
|
495
|
+
return query
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
@pytest.mark.usefixtures("memory_on")
|
|
499
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
500
|
+
@patch("agentplatform.Client")
|
|
501
|
+
def test_a_finished_turn_is_sent_to_memory_bank_without_its_subagents(
|
|
502
|
+
client_class, query
|
|
503
|
+
):
|
|
504
|
+
aio = _fake_client(client_class)
|
|
505
|
+
query.side_effect = _fake_query(_SUBAGENT_TURN)
|
|
506
|
+
|
|
507
|
+
# run_turn returns once the ingest is sent: its process may end next.
|
|
508
|
+
anyio.run(claude.run_turn, "Weather in Oslo?", ClaudeAgentOptions(), "conv-1")
|
|
509
|
+
|
|
510
|
+
aio.memory_banks.ingest_events.assert_awaited_once()
|
|
511
|
+
request = aio.memory_banks.ingest_events.call_args.kwargs
|
|
512
|
+
events = request.pop("direct_contents_source")["events"]
|
|
513
|
+
assert request == {
|
|
514
|
+
"name": "reasoningEngines/123",
|
|
515
|
+
# A turn without a caller is scoped like an unauthenticated A2A one.
|
|
516
|
+
"scope": {"app_name": agent_name(), "user_id": "A2A_USER_conv-1"},
|
|
517
|
+
"stream_id": turn_options.session_id_for("conv-1"),
|
|
518
|
+
}
|
|
519
|
+
call = {"id": "a1", "name": "Agent", "args": _DELEGATE}
|
|
520
|
+
started = {"id": "a1", "name": "Agent", "response": {"result": "Started."}}
|
|
521
|
+
assert [event["content"] for event in events] == [
|
|
522
|
+
{"role": "user", "parts": [{"text": "Weather in Oslo?"}]},
|
|
523
|
+
{
|
|
524
|
+
"role": "model",
|
|
525
|
+
"parts": [{"text": "Asking the forecaster."}, {"function_call": call}],
|
|
526
|
+
},
|
|
527
|
+
{"role": "user", "parts": [{"function_response": started}]},
|
|
528
|
+
{"role": "model", "parts": [{"text": "It's sunny."}]},
|
|
529
|
+
]
|
|
530
|
+
ids = [event["event_id"] for event in events]
|
|
531
|
+
assert len(set(ids)) == len(ids)
|
|
532
|
+
assert all(event["event_time"].tzinfo for event in events)
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
@pytest.mark.usefixtures("memory_on")
|
|
536
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
537
|
+
@patch("agentplatform.Client")
|
|
538
|
+
def test_every_memory_bank_client_a_turn_opens_is_closed(client_class, query):
|
|
539
|
+
aio = _fake_client(client_class, _MEMORIES)
|
|
540
|
+
query.side_effect = _fake_query(_SUBAGENT_TURN)
|
|
541
|
+
|
|
542
|
+
anyio.run(claude.run_turn, "Weather in Oslo?", ClaudeAgentOptions(), "conv-1")
|
|
543
|
+
|
|
544
|
+
# One for the recall, one for the ingest; an unclosed one leaks its HTTP session.
|
|
545
|
+
assert aio.__aenter__.await_count == 2
|
|
546
|
+
assert aio.__aexit__.await_count == 2
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
@pytest.mark.usefixtures("memory_on")
|
|
550
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
551
|
+
@patch("agentplatform.Client")
|
|
552
|
+
def test_a_model_call_with_nothing_to_remember_is_left_out(client_class, query):
|
|
553
|
+
aio = _fake_client(client_class)
|
|
554
|
+
thinking_only = [
|
|
555
|
+
AssistantMessage(
|
|
556
|
+
content=[ThinkingBlock(thinking="Hmm.", signature="s")], model="m"
|
|
557
|
+
),
|
|
558
|
+
_result(),
|
|
559
|
+
]
|
|
560
|
+
query.side_effect = _fake_query(thinking_only)
|
|
561
|
+
|
|
562
|
+
anyio.run(claude.run_turn, "hi", ClaudeAgentOptions(), "conv-1")
|
|
563
|
+
|
|
564
|
+
events = aio.memory_banks.ingest_events.call_args.kwargs["direct_contents_source"][
|
|
565
|
+
"events"
|
|
566
|
+
]
|
|
567
|
+
assert [event["content"]["role"] for event in events] == ["user"]
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
@pytest.mark.usefixtures("memory_on")
|
|
571
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
572
|
+
@patch("agentplatform.Client")
|
|
573
|
+
def test_a_failed_turn_is_not_sent_to_memory_bank(client_class, query):
|
|
574
|
+
aio = _fake_client(client_class)
|
|
575
|
+
failing = [_result(is_error=True, result="max turns")]
|
|
576
|
+
query.side_effect = _fake_query(failing)
|
|
577
|
+
|
|
578
|
+
with pytest.raises(claude.AgentError, match="max turns"):
|
|
579
|
+
anyio.run(claude.run_turn, "hi", ClaudeAgentOptions(), "conv-1")
|
|
580
|
+
|
|
581
|
+
aio.memory_banks.ingest_events.assert_not_awaited()
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
@pytest.mark.usefixtures("memory_on")
|
|
585
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
586
|
+
@patch("agentplatform.Client")
|
|
587
|
+
def test_a_failed_ingest_is_logged_and_never_fails_the_turn(
|
|
588
|
+
client_class, query, caplog
|
|
589
|
+
):
|
|
590
|
+
aio = _fake_client(client_class)
|
|
591
|
+
aio.memory_banks.ingest_events.side_effect = RuntimeError("permission denied")
|
|
592
|
+
query.side_effect = _fake_query([_result()])
|
|
593
|
+
|
|
594
|
+
reply = anyio.run(claude.run_turn, "hi", ClaudeAgentOptions(), "conv-1")
|
|
595
|
+
|
|
596
|
+
assert reply.text == "Done."
|
|
597
|
+
assert "permission denied" in caplog.text
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
@pytest.mark.parametrize(
|
|
601
|
+
"memories, text", [(_MEMORIES, _LINES), ([], "No memories found.")]
|
|
602
|
+
)
|
|
603
|
+
@pytest.mark.usefixtures("memory_on")
|
|
604
|
+
@patch.object(claude.claude_agent_sdk, "query")
|
|
605
|
+
@patch("agentplatform.Client")
|
|
606
|
+
def test_the_search_tool_searches_the_running_turns_user(
|
|
607
|
+
client_class, query, memories, text
|
|
608
|
+
):
|
|
609
|
+
aio = _fake_client(client_class, memories)
|
|
610
|
+
results = []
|
|
611
|
+
|
|
612
|
+
async def search_in_turn(*, prompt, options):
|
|
613
|
+
# As the CLI calls the in-process tool, within the turn.
|
|
614
|
+
results.append(await memory_bank.search_memory.handler({"query": "tea"}))
|
|
615
|
+
yield _result()
|
|
616
|
+
|
|
617
|
+
query.side_effect = search_in_turn
|
|
618
|
+
_turn()
|
|
619
|
+
|
|
620
|
+
assert results == [{"content": [{"type": "text", "text": text}]}]
|
|
621
|
+
assert aio.memory_banks.memories.retrieve.call_args.kwargs == {
|
|
622
|
+
"name": "reasoningEngines/123",
|
|
623
|
+
"scope": {"app_name": agent_name(), "user_id": "alice"},
|
|
624
|
+
"similarity_search_params": {"search_query": "tea"},
|
|
625
|
+
}
|