fix(trajectory-compressor): net-savings guard — skip compaction when the region is no larger than the summary (salvage #56415) - #70243
Merged
Conversation
compress_trajectory (and _async) replaced the compressible middle region with a [CONTEXT SUMMARY] turn without checking that the region is actually larger than the summary. When a large protected system prompt dominates the budget, the compressible middle can be tiny; replacing e.g. a 2-token middle with a ~60-token summary GROWS the trajectory (tokens_saved negative), marks it was_compressed, and still spends a summarization call — the opposite of the intent, on exactly the hard over-budget cases. Add a net-savings guard mirroring the code's own comment (net_savings = region_tokens - summary_target_tokens): if the safely-compressible region is no larger than summary_target_tokens, return the trajectory unchanged. Applied to both the sync and async paths. Add sync+async regression tests.
Contributor
૮ >ﻌ< ა ci reviewran on 980b666 all good! |
This was referenced Jul 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Salvages PR #56415 by @golldyck onto current main (clean cherry-pick, authorship preserved).
Problem
trajectory_compressor.py(the root-level training-trajectory compressor — a separate subsystem fromagent/context_compressor.py) computestarget_tokens_to_compressbut never verifies the compressible middle region is actually larger than the summary that will replace it. The only guards were nothing-to-compress andcompress_until <= compress_start.When a large protected head (system + first human) dominates the budget, the compressible middle can be tiny — replacing e.g. a ~2-token middle with a ~60-token
[CONTEXT SUMMARY]turn grows the trajectory (observed 406→465 tokens), markswas_compressed=True, and still burns a summarization call — the opposite of intent, on exactly the hard over-budget cases. The same gap existed in the async twincompress_trajectory_async.Fix (contributor's, cherry-picked)
Net-savings guard in both sync and async paths: if
sum(turn_tokens[compress_start:compress_until]) <= summary_target_tokens, return the trajectory unchanged (metrics report no compression,still_over_limitset honestly) before spending any summarizer call. Mirrors the code's ownnet_savings = region_tokens - summary_target_tokenscomment.Diff is exactly
trajectory_compressor.py+tests/test_trajectory_compressor.py— no drive-by changes.Validation
scripts/run_tests.sh tests/test_trajectory_compressor.pyorigin/mainsource)git diff --stat origin/main..HEADCredit
All substantive work by @golldyck (original PR #56415); commit cherry-picked with authorship preserved. Noreply email — no contributor mapping needed.
Infographic