Skip to content

fix(agent): add time-based and growth-based recovery to compression anti-thrashing protection (#14694)#15432

Open
Tranquil-Flow wants to merge 2 commits into
NousResearch:mainfrom
Tranquil-Flow:fix/compression-anti-thrash-recovery
Open

fix(agent): add time-based and growth-based recovery to compression anti-thrashing protection (#14694)#15432
Tranquil-Flow wants to merge 2 commits into
NousResearch:mainfrom
Tranquil-Flow:fix/compression-anti-thrash-recovery

Conversation

@Tranquil-Flow

@Tranquil-Flow Tranquil-Flow commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

The anti-thrashing guard in ContextCompressor permanently disables auto-compression after 2 consecutive ineffective compressions (each saving <10%). The counter _ineffective_compression_count only resets when a compression is effective or on session reset — there is no time-based or growth-based recovery. In long sessions, once 2 bad compressions happen, auto-compression is dead for the remainder of the session.

This PR adds time-based and growth-based recovery: the counter resets if 300+ seconds have elapsed since the last compression, or if prompt tokens have grown by 20%+ since then. Both conditions indicate enough new context has accumulated to make another compression attempt worthwhile.

Related Issue

Fixes #14694

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 🔒 Security fix
  • 📝 Documentation update
  • ✅ Tests (adding or improving test coverage)
  • ♻️ Refactor (no behavior change)
  • 🎯 New skill (bundled or hub)

Changes Made

  • agent/context_compressor.py:
    • Added _last_compression_time field to track when the last compression occurred
    • Added _last_compression_prompt_tokens field to track prompt size at last compression
    • In should_compress(), before the >= 2 check, reset the counter if either (a) 300+ seconds have elapsed since the last compression, or (b) prompt tokens have grown by 20%+ since the last compression.
    • Record both fields after each compression in compress()
    • Reset both fields in on_session_reset()

How to Test

Tests: tests/agent/test_context_compressor.py — 5 new tests in TestAntiThrashingRecovery:

  1. Anti-thrashing blocks after 2 ineffective compressions (baseline)
  2. Recovery after cooldown expiry (300s)
  3. Recovery after prompt growth (20%+)
  4. Still blocked within cooldown and without growth
  5. No recovery when no compression has ever run (timestamp == 0)

Tested on macOS (Python 3.11).

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS (Python 3.11)

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Screenshots / Logs

pytest tests/agent/test_context_compressor.py::TestAntiThrashingRecovery -q
5 passed

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder labels Apr 25, 2026
@Tranquil-Flow Tranquil-Flow force-pushed the fix/compression-anti-thrash-recovery branch from 55a4c53 to 6a38520 Compare May 18, 2026 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Anti-thrashing protection permanently disables auto-compression with no recovery

2 participants