fix: prevent compression endless loop in agentic sessions#23946
fix: prevent compression endless loop in agentic sessions#23946Rahu378 wants to merge 7 commits intogoogle-gemini:mainfrom
Conversation
…fety Add ALWAYS_PASSES behavioral evaluations covering: - Model correctly invokes run_shell_command for listing/disk queries - Model does not silently execute destructive rm -rf wildcard commands - Model uses a write tool when asked to create a file These cases were not previously covered in the eval suite and help prevent regressions in tool-use steering for shell operations.
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 long-running agentic sessions could enter an infinite auto-compression loop. By implementing a per-turn guard and stricter threshold validation, the system now correctly identifies when further compression is ineffective or unnecessary, ensuring stability during complex tool-call chains. 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 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 counter productive. 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
|
Co-authored-by: gemini-cli-robot <224641728+gemini-cli-robot@users.noreply.github.com> Co-authored-by: Sam Roberts <158088236+g-samroberts@users.noreply.github.com>
…mini#22562) Co-authored-by: cynthialong0-0 <82900738+cynthialong0-0@users.noreply.github.com>
Add hasCompressedThisTurn guard to GeminiClient to prevent tryCompressChat from firing on every processTurn call within a single user turn (e.g. during tool-call chains in Ralph loops). Also add stillAboveThreshold check in ChatCompressionService so compression that succeeds but remains above threshold is marked COMPRESSION_FAILED_INFLATED_TOKEN_COUNT, preventing re-trigger. Fixes: google-gemini#23907 Parent: google-gemini#23905
e3aadaa to
63ec380
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces measures to prevent endless compression loops and adds safety evaluation tests for shell commands. In GeminiClient, a new flag hasCompressedThisTurn ensures compression only happens once per user turn. Additionally, ChatCompressionService now bails out if the token count remains above the threshold after compression. A new evaluation file shell_command_safety.eval.ts was added to test shell tool usage and safety constraints. I have no feedback to provide.
Note: Security Review is unavailable for this PR.
Summary
Prevent endless auto-compression loop in long agentic sessions ("Ralph loops").
When tool-call chains trigger multiple
processTurncalls within a single userturn,
tryCompressChatwas firing on every call — even after already compressingonce — causing an infinite compression loop. Priority fix before the 20%
compression flag rolls out to users.
Details
Two changes:
GeminiClient- addedhasCompressedThisTurnboolean flag that is set totrueafter the first successful compression and reset tofalseat thestart of each new user turn (in
startChat). Auto-compression is skippedentirely if this flag is already set.
ChatCompressionService- addedstillAboveThresholdcheck aftercompression completes. If the new token count is still above the compression
threshold (even though it didn't inflate), the service now returns
COMPRESSION_FAILED_INFLATED_TOKEN_COUNTinstead ofCOMPRESSED. This setshasFailedCompressionAttempt = truein the client, stopping further retries.Related Issues
Fixes #23907
Parent: #23905
How to Validate
Run a "Ralph loop" with compression threshold set to 20%:
files in the repo)
Check logs for:
[compression] Skipping auto-compression: already compressed this turn.
Manual compress command (/compress) should still work — it passes
force: true which bypasses the guard entirely.
Pre-Merge Checklist
unit tests for the guard can be added in follow-up
[ x] MacOS