fix: gate token-based compaction on latest prompt size, not event sum#474
fix: gate token-based compaction on latest prompt size, not event sum#474phillip-hicks-siro wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
@googlebot I signed it! |
d21d8f4 to
cfa5513
Compare
|
Hi @kalenkevich, I have checked the issue and validated the proposed changes. I was able to reproduce the bug. Could you please review this PR? |
|
Hi @phillip-hicks-siro, We noticed that a few of the pre-checks are failing on this PR. Could you please resolve them? |
shouldCompact previously summed usageMetadata.promptTokenCount across all active events. Each of those values is the full prompt size of the call that produced it, so the sum re-counts the same history once per event and grows with call count rather than context size: sessions cross any threshold after a handful of calls regardless of actual context pressure, and after a compaction the retained events still carry pre-compaction counts, leaving the sum permanently above the threshold (compact-every-call thrashing). Port the Python ADK semantics (apps/compaction.py): use the most recently observed promptTokenCount - the measured size of the last request, which already includes system instruction, tools, and full history - and fall back to a chars/4 estimate over the effective request contents (getContents) when no usage metadata is available. Fixes google#473
cfa5513 to
ad6f98f
Compare
|
@Varun-S10 Pre-check error resolved! |
|
The |
Fixes #473.
TokenBasedContextCompactor.shouldCompactsummedusageMetadata.promptTokenCountacross active events. Each of those values is the full prompt size of the call that produced it (system instruction + tools + entire history), so the sum re-counts the same history once per event and grows with call count rather than context size — and after a compaction, retained tail events still carry pre-compaction counts, leaving the sum permanently above any threshold (compact-every-call thrashing). Details and production impact in #473.This ports the Python ADK's semantics (
apps/compaction.py::_latest_prompt_token_count):promptTokenCount— the measured size of the last real requestgetContents, mirroring the content request processor) when no usage metadata is availablecompact()and retention behavior are unchanged.Testing: rewrote the threshold unit test against the new semantics and added: a latest-not-sum regression case, newest-metadata-wins, estimate fallback (over and under threshold), and the no-signal case. Full
unit:coresuite passes (954 tests).