Skip to content

Commit f7c0c01

Browse files
GWealecopybara-github
authored andcommitted
fix(utils): report an unknown session id in the cache performance analyzer
Both analyzer entry points looked a session up and read its events without checking the lookup succeeded, so an unknown session id raised AttributeError on None rather than naming the session. They now raise ValueError. Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 974117700
1 parent b0bb3dd commit f7c0c01

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/google/adk/utils/cache_performance_analyzer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ async def _get_agent_cache_history(
6060
app_name=app_name,
6161
user_id=user_id,
6262
)
63+
if session is None:
64+
raise ValueError(f"Session not found: {session_id}")
6365
cache_history = []
6466

6567
for event in session.events:
@@ -111,6 +113,8 @@ async def analyze_agent_cache_performance(
111113
app_name=app_name,
112114
user_id=user_id,
113115
)
116+
if session is None:
117+
raise ValueError(f"Session not found: {session_id}")
114118

115119
# Collect token metrics from events
116120
total_prompt_tokens = 0

tests/unittests/utils/test_cache_performance_analyzer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ async def test_get_agent_cache_history_empty_session(self):
9090

9191
assert result == []
9292

93+
async def test_get_agent_cache_history_missing_session(self):
94+
"""An unknown session id is reported, not dereferenced."""
95+
self.mock_session_service.get_session = AsyncMock(return_value=None)
96+
97+
with pytest.raises(ValueError, match="Session not found: no_such_session"):
98+
await self.analyzer._get_agent_cache_history(
99+
"no_such_session", "test_user", "test_app", "test_agent"
100+
)
101+
93102
async def test_get_agent_cache_history_no_cache_events(self):
94103
"""Test getting cache history when no events have cache metadata."""
95104
events = [

0 commit comments

Comments
 (0)