Skip to content

fix(agent): clamp compression threshold below context_length to ensure compression can trigger (#14690)#15431

Open
Tranquil-Flow wants to merge 1 commit into
NousResearch:mainfrom
Tranquil-Flow:fix/compress-threshold-14690
Open

fix(agent): clamp compression threshold below context_length to ensure compression can trigger (#14690)#15431
Tranquil-Flow wants to merge 1 commit into
NousResearch:mainfrom
Tranquil-Flow:fix/compress-threshold-14690

Conversation

@Tranquil-Flow

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

Copy link
Copy Markdown
Contributor

What does this PR do?

When context_length == MINIMUM_CONTEXT_LENGTH (64000), the threshold calculation max(int(context_length * 0.50), MINIMUM_CONTEXT_LENGTH) produces max(32000, 64000) = 64000, setting the compression threshold to 100% of the context window. The API errors before tokens ever reach that value, so auto-compression never triggers for models at the minimum context length.

The fix clamps threshold_tokens to at most 95% of context_length after the existing floor, preserving the MINIMUM_CONTEXT_LENGTH floor for large-context models (where the clamp is a no-op) while ensuring compression can always trigger before the API limit.

Related Issue

Fixes #14690

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: After the existing max() floor, clamp threshold_tokens to at most 95% of context_length. Applied in both __init__ and update_model(). Additionally, update_model() now recomputes all derived budgets (tail_token_budget, max_summary_tokens) — previously it only updated threshold_tokens, leaving stale budget values after model switches.

How to Test

  1. Run:
    python -m pytest tests/agent/test_context_compressor.py -v
  2. 4 new tests in TestThresholdClampAtMinimumContext:
    • Threshold is below context_length at MINIMUM_CONTEXT_LENGTH
    • should_compress() triggers correctly at threshold boundary
    • update_model() produces a working threshold
    • Large-context models are unaffected by the clamp

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 15 (Darwin 24.6.0)

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

python -m pytest tests/agent/test_context_compressor.py -v
# 4 new tests in TestThresholdClampAtMinimumContext pass

@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
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #14878 — both fix the same issue (#14690): compression threshold unreachable at 64K context. Also overlaps with omnibus PR #14696.

@Tranquil-Flow Tranquil-Flow force-pushed the fix/compress-threshold-14690 branch from 72ca28d to 387dffe Compare May 25, 2026 13:59
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: Context auto-compression never triggers when context_length == MINIMUM_CONTEXT_LENGTH (64000)

2 participants