Skip to content

fix: prevent compression endless loop in agentic sessions#23946

Open
Rahu378 wants to merge 7 commits intogoogle-gemini:mainfrom
Rahu378:fix/compression-endless-loop
Open

fix: prevent compression endless loop in agentic sessions#23946
Rahu378 wants to merge 7 commits intogoogle-gemini:mainfrom
Rahu378:fix/compression-endless-loop

Conversation

@Rahu378
Copy link
Copy Markdown

@Rahu378 Rahu378 commented Mar 26, 2026

Summary

Prevent endless auto-compression loop in long agentic sessions ("Ralph loops").
When tool-call chains trigger multiple processTurn calls within a single user
turn, tryCompressChat was firing on every call — even after already compressing
once — causing an infinite compression loop. Priority fix before the 20%
compression flag rolls out to users.

Details

Two changes:

  1. GeminiClient - added hasCompressedThisTurn boolean flag that is set to
    true after the first successful compression and reset to false at the
    start of each new user turn (in startChat). Auto-compression is skipped
    entirely if this flag is already set.

  2. ChatCompressionService - added stillAboveThreshold check after
    compression 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_COUNT instead of COMPRESSED. This sets
    hasFailedCompressionAttempt = true in the client, stopping further retries.

Related Issues

Fixes #23907
Parent: #23905

How to Validate

  1. Run a "Ralph loop" with compression threshold set to 20%:

    • In settings, set compressionThreshold: 0.2
    • Run a long agentic script that fills context (e.g. repeatedly read/summarise
      files in the repo)
    • Without fix: compression triggers on every tool-call follow-up turn
    • With fix: compression fires at most once per user turn, loop stops
  2. Check logs for:
    [compression] Skipping auto-compression: already compressed this turn.

  3. Manual compress command (/compress) should still work — it passes
    force: true which bypasses the guard entirely.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • [ x ] Added/updated tests (if needed) : existing compression tests pass;
    unit tests for the guard can be added in follow-up
  • Noted breaking changes (if any): no breaking changes
  • Validated on required platforms/methods:
    • [ x] MacOS

      • [ x] npm run

Rahu378 added 2 commits March 26, 2026 12:54
…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.
@Rahu378 Rahu378 requested review from a team as code owners March 26, 2026 21:29
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • Compression Guard: Introduced a hasCompressedThisTurn flag in GeminiClient to ensure auto-compression triggers at most once per user turn, preventing infinite loops.
  • Threshold Validation: Updated ChatCompressionService to treat compression attempts that remain above the token threshold as failures, effectively stopping further retry attempts.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-cli gemini-cli Bot added priority/p0 Critical and urgent (e.g., critical security vulnerability, major breakage). area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. labels Mar 26, 2026
gemini-cli-robot and others added 3 commits March 26, 2026 17:35
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
@Rahu378 Rahu378 force-pushed the fix/compression-endless-loop branch from e3aadaa to 63ec380 Compare March 26, 2026 21:36
@Rahu378 Rahu378 requested a review from a team as a code owner March 26, 2026 21:36
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. priority/p0 Critical and urgent (e.g., critical security vulnerability, major breakage).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate the Bug (Compression Endless Loop)

3 participants