Skip to content

fix(trajectory-compressor): net-savings guard — skip compaction when the region is no larger than the summary (salvage #56415) - #70243

Merged
teknium1 merged 1 commit into
mainfrom
salv/56415-trajectory-net-savings
Jul 23, 2026
Merged

fix(trajectory-compressor): net-savings guard — skip compaction when the region is no larger than the summary (salvage #56415)#70243
teknium1 merged 1 commit into
mainfrom
salv/56415-trajectory-net-savings

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

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 from agent/context_compressor.py) computes target_tokens_to_compress but never verifies the compressible middle region is actually larger than the summary that will replace it. The only guards were nothing-to-compress and compress_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), marks was_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 twin compress_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_limit set honestly) before spending any summarizer call. Mirrors the code's own net_savings = region_tokens - summary_target_tokens comment.

Diff is exactly trajectory_compressor.py + tests/test_trajectory_compressor.py — no drive-by changes.

Validation

Check Result
Cherry-pick onto main Clean, zero conflicts (file untouched since #52336)
Sync guard present (L~834)
Async guard present (L~961)
scripts/run_tests.sh tests/test_trajectory_compressor.py ✅ 37/37 passed (incl. 2 new sync+async regression tests)
Counterfactual (tests vs pre-fix origin/main source) ✅ both new tests FAIL on unfixed main, pass with fix
git diff --stat origin/main..HEAD 2 files, +88 only

Credit

All substantive work by @golldyck (original PR #56415); commit cherry-picked with authorship preserved. Noreply email — no contributor mapping needed.

Infographic

net-savings-guard

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.
@github-actions

github-actions Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

૮ >ﻌ< ა ci review

ran on 980b666

all good!

@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint labels Jul 23, 2026
@teknium1
teknium1 merged commit 69339aa into main Jul 23, 2026
40 checks passed
@teknium1
teknium1 deleted the salv/56415-trajectory-net-savings branch July 23, 2026 23:21
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 P3 Low — cosmetic, nice to have type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants