1010from google .adk .tools .mcp_tool .mcp_toolset import MCPToolset
1111
1212from agentsts .adk import ADKSTSIntegration , ADKTokenPropagationPlugin
13- from agentsts .adk ._base import HEADERS_KEY , MAX_CACHE_ENTRIES , MAX_CACHE_TTL_SECONDS
13+ from agentsts .adk ._base import HEADERS_KEY , MAX_CACHE_ENTRIES , MAX_CACHE_TTL_SECONDS , MAX_ENTRIES_PER_SESSION
1414from agentsts .adk ._base import _cache_key as build_cache_key
1515from agentsts .adk ._base import _extract_jwt_expiry as extract_jwt_expiry
1616from agentsts .adk ._base import _extract_jwt_from_headers as extract_jwt_from_headers
@@ -1335,27 +1335,41 @@ async def test_a_concurrent_run_does_not_sweep_an_in_flight_caller(self):
13351335 assert plugin .header_provider (in_flight_ctx ) == {"Authorization" : "Bearer delegated-opaque-alice" }
13361336
13371337 @pytest .mark .asyncio
1338- async def test_capacity_eviction_keeps_the_entry_of_an_in_flight_caller (self ):
1338+ @pytest .mark .parametrize (
1339+ ("burst_session" , "capacity" ),
1340+ [
1341+ pytest .param (lambda index : "sess-burst" , MAX_ENTRIES_PER_SESSION , id = "session" ),
1342+ pytest .param (lambda index : f"sess-burst-{ index } " , MAX_CACHE_ENTRIES , id = "cache" ),
1343+ ],
1344+ )
1345+ async def test_capacity_eviction_keeps_the_entry_of_an_in_flight_caller (self , burst_session , capacity ):
13391346 """Case: a burst of distinct credentials must not evict the entry a run is
1340- still making tool calls with."""
1347+ still making tool calls with, under either capacity bound ."""
13411348 plugin = ADKTokenPropagationPlugin (sts_integration = None )
13421349
13431350 in_flight = self ._make_invocation_context ("sess-burst" , headers = {"Authorization" : "Bearer opaque-alice" })
13441351 await plugin .before_run_callback (invocation_context = in_flight )
13451352 in_flight_ctx = self ._make_readonly_context (in_flight )
13461353
1347- for index in range (MAX_CACHE_ENTRIES + 25 ):
1354+ for index in range (capacity + 25 ):
13481355 # Each new caller is more recent than the last tool call of the
13491356 # in-flight run, so only a renewed entry survives the burst.
13501357 assert plugin .header_provider (in_flight_ctx ) == {"Authorization" : "Bearer opaque-alice" }
1351- ic = self ._make_invocation_context ("sess-burst" , headers = {"Authorization" : f"Bearer token-{ index } " })
1358+ ic = self ._make_invocation_context (burst_session ( index ) , headers = {"Authorization" : f"Bearer token-{ index } " })
13521359 await plugin .before_run_callback (invocation_context = ic )
13531360
1354- assert len (plugin .token_cache ) == MAX_CACHE_ENTRIES
1361+ assert len (plugin .token_cache ) == capacity
13551362 assert plugin .header_provider (in_flight_ctx ) == {"Authorization" : "Bearer opaque-alice" }
13561363
13571364 @pytest .mark .asyncio
1358- async def test_capacity_eviction_drops_a_caller_that_makes_no_tool_call (self ):
1365+ @pytest .mark .parametrize (
1366+ ("burst_session" , "capacity" ),
1367+ [
1368+ pytest .param (lambda index : "sess-idle-burst" , MAX_ENTRIES_PER_SESSION , id = "session" ),
1369+ pytest .param (lambda index : f"sess-idle-burst-{ index } " , MAX_CACHE_ENTRIES , id = "cache" ),
1370+ ],
1371+ )
1372+ async def test_capacity_eviction_drops_a_caller_that_makes_no_tool_call (self , burst_session , capacity ):
13591373 """Case: use order is the only signal the capacity bound has, so a run
13601374 that pauses long enough loses its entry and injects no credential.
13611375
@@ -1370,11 +1384,11 @@ async def test_capacity_eviction_drops_a_caller_that_makes_no_tool_call(self):
13701384 assert plugin .header_provider (idle_ctx ) == {"Authorization" : "Bearer opaque-alice" }
13711385
13721386 # The run waits on a model turn, so it renews nothing while the burst runs.
1373- for index in range (MAX_CACHE_ENTRIES + 5 ):
1374- ic = self ._make_invocation_context ("sess-idle-burst" , headers = {"Authorization" : f"Bearer token-{ index } " })
1387+ for index in range (capacity + 5 ):
1388+ ic = self ._make_invocation_context (burst_session ( index ) , headers = {"Authorization" : f"Bearer token-{ index } " })
13751389 await plugin .before_run_callback (invocation_context = ic )
13761390
1377- assert len (plugin .token_cache ) == MAX_CACHE_ENTRIES
1391+ assert len (plugin .token_cache ) == capacity
13781392 assert plugin .cache_key (idle ) not in plugin .token_cache
13791393 assert plugin .header_provider (idle_ctx ) == {}
13801394
@@ -1528,19 +1542,49 @@ async def test_missing_authorization_header_is_reported_once(self):
15281542 assert mock_logger .warning .call_args_list == []
15291543
15301544 @pytest .mark .asyncio
1531- async def test_cache_is_capped_by_entry_count (self ):
1532- """Case: one session seeing many distinct credentials is capped, since the
1533- sweep never runs while a single run holds them."""
1545+ @pytest .mark .parametrize (
1546+ ("burst_session" , "capacity" ),
1547+ [
1548+ pytest .param (lambda index : "one-session" , MAX_ENTRIES_PER_SESSION , id = "session" ),
1549+ pytest .param (lambda index : f"session-{ index } " , MAX_CACHE_ENTRIES , id = "cache" ),
1550+ ],
1551+ )
1552+ async def test_cache_is_capped_by_entry_count (self , burst_session , capacity ):
1553+ """Case: many distinct credentials are capped, since the sweep never runs
1554+ while the runs hold them."""
15341555 plugin = ADKTokenPropagationPlugin (sts_integration = None )
15351556
1536- for index in range (MAX_CACHE_ENTRIES + 25 ):
1537- ic = self ._make_invocation_context ("one-session" , headers = {"Authorization" : f"Bearer token-{ index } " })
1557+ for index in range (capacity + 25 ):
1558+ ic = self ._make_invocation_context (burst_session ( index ) , headers = {"Authorization" : f"Bearer token-{ index } " })
15381559 await plugin .before_run_callback (invocation_context = ic )
15391560
1540- assert len (plugin .token_cache ) == MAX_CACHE_ENTRIES
1561+ assert len (plugin .token_cache ) == capacity
15411562 # The run that just cached keeps its own entry.
15421563 assert plugin .cache_key (ic ) in plugin .token_cache
15431564
1565+ @pytest .mark .asyncio
1566+ async def test_a_session_burst_does_not_evict_another_sessions_entry (self ):
1567+ """Case: one session presenting many distinct credentials evicts its own
1568+ entries, not those of an unrelated session.
1569+
1570+ The victim run is between tool calls, so it renews nothing during the
1571+ burst. before_run_callback has already run, so a lost entry would leave
1572+ the rest of that run with no delegated identity.
1573+ """
1574+ plugin = ADKTokenPropagationPlugin (sts_integration = None )
1575+
1576+ victim = self ._make_invocation_context ("sess-victim" , headers = {"Authorization" : "Bearer opaque-alice" })
1577+ await plugin .before_run_callback (invocation_context = victim )
1578+ victim_ctx = self ._make_readonly_context (victim )
1579+ assert plugin .header_provider (victim_ctx ) == {"Authorization" : "Bearer opaque-alice" }
1580+
1581+ for index in range (MAX_CACHE_ENTRIES + 25 ):
1582+ ic = self ._make_invocation_context ("sess-noisy" , headers = {"Authorization" : f"Bearer token-{ index } " })
1583+ await plugin .before_run_callback (invocation_context = ic )
1584+
1585+ assert len (plugin .token_cache ) == MAX_ENTRIES_PER_SESSION + 1
1586+ assert plugin .header_provider (victim_ctx ) == {"Authorization" : "Bearer opaque-alice" }
1587+
15441588 def test_header_provider_without_context_fails_closed (self ):
15451589 """Case: a tool call with no invocation context gets no header instead of raising."""
15461590 plugin = ADKTokenPropagationPlugin (sts_integration = None )
0 commit comments