Repository navigation
Conversation
copybara-service Bot
pushed a commit
that referenced
this pull request
Oct 8, 2026
Merge #7400 PiperOrigin-RevId: 995595142
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
No existing issue found; the bug and reproduction are described below.
Problem:
CachePerformanceAnalyzeraddsinvocations_usedfrom every event, but that field is a cumulative counter for a cache instance. A cache used across three invocations produces snapshots1, 2, 3; the analyzer reports six invocations and an average of two per cache instead of three for both. Multiple model turns in one invocation repeat the same counter and inflate the result further.To reproduce:
InMemorySessionServicesession.Eventrecords for the same agent and cache resource, withCacheMetadata.invocations_usedset to1,2, and3.analyze_agent_cache_performance()for that session and agent.Expected:
total_invocations=3,avg_invocations_used=3.0.Actual:
total_invocations=6,avg_invocations_used=2.0.Reproduced on ADK main
86a47f6, on Windows and Linux. No model, LiteLLM, cloud credentials, or paid API calls are needed.Solution:
Keep the highest cumulative counter for each cache resource name, then calculate the existing total and mean from those per-cache values. Refreshed caches remain distinct even when their fingerprints match.
This fixes the reported reuse statistics without changing cache creation, reuse, invalidation, request/token metrics, or public signatures and result keys.
Testing Plan
Unit Tests:
Eight added cases cover successive invocations, repeated turns, cache refresh, out-of-order snapshots, single-snapshot controls, fingerprint-only records, and zero counters. Existing tests are unchanged.
toxsuites pass on Python 3.10, 3.11, 3.12, 3.13, and 3.14: 17,427 passed on 3.11 and 17,418 passed on each other version. Existing skips and xfail/xpass results remain.uv buildsucceeds.Manual End-to-End (E2E) Tests:
Ran an offline integration using the real
ContextCacheRequestProcessor, session service, events, and analyzer. Cache creation was represented by initial metadata at the cloud boundary. Three invocations, including an extra turn in the second invocation, produce snapshots[1, 2, 2, 3]:Also installed the built wheel in a clean environment and repeated the offline integration. This does not exercise a live model or cache-provider API.
Checklist
Additional context
The existing docstring already describes per-cache statistics; this restores that behavior without adding a new metric or changing documentation contracts.
The first Python 3.12 run used the host's system interpreter and failed two import-allowlist tests because it automatically loads
sitecustomize. Both also fail on unmodified main in that environment. The full 3.12 suite was rerun successfully using an isolated, uv-managed Python 3.12.13 interpreter, without changing project code or the allowlist.