Skip to content

fix(compressor): _prune_old_tool_results boundary direction - #16228

Closed
swithek wants to merge 1 commit into
NousResearch:mainfrom
swithek:fix/prune-tool-results-budget-floor-inversion
Closed

fix(compressor): _prune_old_tool_results boundary direction#16228
swithek wants to merge 1 commit into
NousResearch:mainfrom
swithek:fix/prune-tool-results-budget-floor-inversion

Conversation

@swithek

@swithek swithek commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes a count/index direction error in _prune_old_tool_results. The function's docstring promises "the token budget takes priority and the message count acts as a hard minimum floor", but the line

prune_boundary = max(boundary, len(result) - min_protect)

did the opposite in index-space: when the budget walk reserved space for the whole transcript (boundary == 0), max(0, len - min_protect) truncated protection back down to min_protect messages and rewrote every older tool result with a one-line summary. Long-context Claude / Gemini runs silently shed tens of thousands of tokens of recent tool output the budget had room for.

The fix moves the calculation into count-space, where max reads naturally and matches the docstring's language. Behaviour is identical to the one-character max → min patch on every input, but the next reader doesn't have to re-derive that "min of indices == max of counts" in their head.

Related Issue

N/A

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • agent/context_compressor.py: replace prune_boundary = max(...) (index-space) with a 3-line count-space form; comment explains the inversion so it can't drift back.
  • tests/agent/test_context_compressor.py: add TestTokenBudgetTailProtection::test_generous_budget_protects_everything_floor_does_not_override - a generous-budget transcript with unique tool outputs (so dedup is a no-op) must produce pruned == 0.

How to Test

  1. Run the targeted regression test:
    pytest tests/agent/test_context_compressor.py::TestTokenBudgetTailProtection::test_generous_budget_protects_everything_floor_does_not_override -q
  2. Run the surrounding class to confirm the tight-budget paths still work (6 originals + 1 new = 7 tests):
    pytest tests/agent/test_context_compressor.py::TestTokenBudgetTailProtection -q
  3. Confirm the new test is a real regression guard: stash the patch, re-run step 1, it fails with pruned == 40. git stash pop, re-run, it passes.

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 26

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::TestTokenBudgetTailProtection -q
.......                                                                  [100%]
7 passed, 7 warnings in 1.40s

@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint labels Apr 26, 2026
@swithek
swithek force-pushed the fix/prune-tool-results-budget-floor-inversion branch from 05c6608 to e0552a8 Compare April 27, 2026 07:02
@swithek

swithek commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

hey @alt-glitch @teknium1 @benbarclay bumping this as it's actively affecting us, recent tool output gets silently pruned on long runs and makes the whole thing unreliable

@teknium1

teknium1 commented May 4, 2026

Copy link
Copy Markdown
Contributor

Salvaged via #19725 onto current main - your commit authorship was preserved. Thanks!

@teknium1 teknium1 closed this May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P1 High — major feature broken, no workaround type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants