Repository navigation
fix(core): exclude evicted binary payloads from exempt retrieval byte budget - #30473
bitwizard25 wants to merge 3 commits into
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an issue where large binary payloads in older chat turns were causing the exempt retrieval budget to be exhausted prematurely. By modifying how turn sizes are calculated during the budget check, the system now correctly ignores evicted binary data, ensuring that important text-based history is not unnecessarily collapsed. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
📊 PR Size: size/L
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new helper function calculateRetainedTurnByteSize in chatCompressionService.ts to compute the byte size of older chat history turns after binary payloads are evicted, preventing evicted data from incorrectly exhausting the exempt retrieval budget. It also adds corresponding unit tests to verify this behavior for both nested and sibling layouts. A review comment identifies a potential issue where calculateRetainedTurnByteSize ignores nested parts when part.functionResponse.response is undefined but parts are present, which could lead to budget underestimation. A code suggestion is provided to correctly account for these nested parts.
| function calculateRetainedTurnByteSize(turn: Content): number { | ||
| let bytes = 0; | ||
| if (!turn.parts) { | ||
| return bytes; | ||
| } | ||
| for (const part of turn.parts) { | ||
| if (part.inlineData) { | ||
| bytes += OMITTED_BINARY_MESSAGE_BYTES; | ||
| continue; | ||
| } | ||
| if (part.text) { | ||
| bytes += Buffer.byteLength(part.text, 'utf8'); | ||
| } | ||
| if (part.functionResponse?.response) { | ||
| const resp = part.functionResponse.response; | ||
| if (typeof resp === 'string') { | ||
| bytes += Buffer.byteLength(resp, 'utf8'); | ||
| } else if (typeof resp === 'object' && resp !== null) { | ||
| try { | ||
| bytes += Buffer.byteLength(JSON.stringify(resp), 'utf8'); | ||
| } catch { | ||
| // ignore JSON errors | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return bytes; | ||
| } |
There was a problem hiding this comment.
If part.functionResponse.response is undefined but part.functionResponse.parts is defined, collapseOlderFunctionResponses will return the part unmodified (meaning the nested parts are retained). However, calculateRetainedTurnByteSize currently counts 0 bytes for this functionResponse, completely ignoring the retained nested parts. We should update the function to count the nested parts when response is undefined, ensuring consistency with calculateHistoryByteSize and preventing budget underestimation.
function calculateRetainedTurnByteSize(turn: Content): number {
let bytes = 0;
if (!turn.parts) {
return bytes;
}
for (const part of turn.parts) {
if (part.inlineData) {
bytes += OMITTED_BINARY_MESSAGE_BYTES;
continue;
}
if (part.text) {
bytes += Buffer.byteLength(part.text, 'utf8');
}
if (part.functionResponse) {
const resp = part.functionResponse.response;
if (resp !== undefined && resp !== null) {
if (typeof resp === 'string') {
bytes += Buffer.byteLength(resp, 'utf8');
} else if (typeof resp === 'object') {
try {
bytes += Buffer.byteLength(JSON.stringify(resp), 'utf8');
} catch {
// ignore JSON errors
}
}
} else if (part.functionResponse.parts) {
for (const nestedPart of part.functionResponse.parts) {
if (nestedPart && typeof nestedPart === 'object') {
if (
'text' in nestedPart &&
typeof nestedPart.text === 'string' &&
nestedPart.text
) {
bytes += Buffer.byteLength(nestedPart.text, 'utf8');
}
if (nestedPart.inlineData?.data) {
bytes += Buffer.byteLength(nestedPart.inlineData.data, 'utf8');
}
}
}
}
}
}
return bytes;
}There was a problem hiding this comment.
Good catch — updated calculateRetainedTurnByteSize in 8a03170 to count retained nested functionResponse.parts when response is absent, and added a regression test covering that case.
There was a problem hiding this comment.
Good catch — fixed in 8a03170. calculateRetainedTurnByteSize now counts nested functionResponse.parts whenever part.functionResponse.response is absent (matching the early return in collapseOlderFunctionResponses and calculateHistoryByteSize), and I added a regression test covering that case.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a more accurate byte size calculation for older chat turns during history compression. Specifically, it adds the calculateRetainedTurnByteSize function to compute the size of a turn after binary payloads (such as sibling inlineData and nested functionResponse parts) are evicted, preventing these evicted payloads from prematurely exhausting the exempt retrieval budget. It also includes comprehensive unit tests to verify this behavior under nested and sibling layouts, as well as when nested parts are retained due to an absent response. I have no additional feedback to provide as the implementation is clean and well-tested.
chrikrah
left a comment
There was a problem hiding this comment.
@bitwizard25 evicted binaries no longer count, and two of the three new cases fail on base 60f8082. Ready as far as I can test.
non-blocking: a non-exempt part in the same turn still charges in full, so a 70 KB shell output next to a read_file collapses the older 15 KB read. Charging only exempt parts keeps it:
# probe: [read_file 15 KB] [model] [read_file "small" + run_shell_command 70 KB, one turn] [3 recent shell turns]
$ npx vitest run src/context/zzProbeR4.test.ts --coverage.enabled=false # head 8a03170; values from the assertion message
kb=70 first_intact=false first_len=2139 shell_retained=2139
kb=70 first_intact=true first_len=15600 shell_retained=2139 # with the diff below
$ npx vitest run src/context/chatCompressionService.test.ts src/agents/memory-leak-regression.test.ts --coverage.enabled=false
Tests 57 passed (57) # head, and again with the diff below
Tests 2 failed | 55 passed (57) # chatCompressionService.ts from 60f8082: nested and sibling cases
$ npx vitest run --coverage.enabled=false # packages/core, head
Tests 8713 passed | 57 skipped (8770) # 7 files fail to load: no workspace build, so @google/gemini-cli-core does not resolve
# eslint 9.24.0 --max-warnings 0, prettier --check and tsc --noEmit (packages/core): clean at head
-function calculateRetainedTurnByteSize(turn: Content): number {
+function calculateRetainedTurnByteSize(
+ turn: Content,
+ exemptTools: ReadonlySet<string>,
+): number {
@@
- if (part.functionResponse) {
+ if (
+ part.functionResponse &&
+ isExemptRetrievalTool(part.functionResponse.name, exemptTools)
+ ) {
@@
- const turnBytes = calculateRetainedTurnByteSize(turn);
+ const turnBytes = calculateRetainedTurnByteSize(turn, exemptTools);Cost: non-exempt parts then charge nothing, yet keep a ~2 KB preview, or everything when response is absent. The helper is new and private. Want the probe as a test?
…rieval byte budget
TLDR
Fixes #30446.
collapseOlderFunctionResponsesnow sizes older retrieval turns using their retained byte size (after evicted siblinginlineDataand nestedfunctionResponse.partsare excluded) instead ofcalculateHistoryByteSize([turn]), preventing a single older imageread_filefrom exhausting the 64 KBMAX_EXEMPT_RETRIEVAL_HISTORY_BYTESbudget and collapsing earlier text reads.Dive Deeper
collapseOlderFunctionResponsesalways evicts binary payloads from older turns (replacing siblingpart.inlineDatawithOMITTED_BINARY_MESSAGEand stripping nestedpart.functionResponse.parts), even for preserved exempt retrieval turns. However, the budget loop measured each candidate turn withcalculateHistoryByteSize([turn]), which counts those evicted binary bytes. One >64 KB screenshotread_fileexhausted the entire exempt retrieval budget and caused older textread_file/grepoutputs to collapse to 2 KB previews.calculateRetainedTurnByteSize(turn)helper inpackages/core/src/context/chatCompressionService.tsused only in the exempt-budget loop. It countsOMITTED_BINARY_MESSAGE_BYTESfor siblinginlineDataparts and omits nestedfunctionResponse.parts, while leavingcalculateHistoryByteSizeuntouched so memory-pressure thresholds continue measuring full binary size.packages/core/src/context/chatCompressionService.test.tscovering bothnested(functionResponse.parts[].inlineData) andsibling(part.inlineData) layouts. Both cases fail onmainand pass with this fix.Reviewer Test Plan
packages/core/src/context/chatCompressionService.test.ts: 38/38 passedpackages/core/src/agents/memory-leak-regression.test.ts: 18/18 passedprettier --check&eslint --max-warnings 0on modified files: passed