Skip to content

fix(compression): preserve -1 post-compression sentinel to stop re-fire (#36718)#40582

Merged
teknium1 merged 1 commit into
mainfrom
salvage/40246-preflight-sentinel
Jun 7, 2026
Merged

fix(compression): preserve -1 post-compression sentinel to stop re-fire (#36718)#40582
teknium1 merged 1 commit into
mainfrom
salvage/40246-preflight-sentinel

Conversation

@teknium1

@teknium1 teknium1 commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Stops compression from re-firing immediately after a fresh compression. Closes #36718.

compress_context() sets last_prompt_tokens = -1 to signal "no real API usage yet". The preflight display-seed compared _preflight_tokens > (last_prompt_tokens or 0) — but (-1 or 0) is -1 (truthy), so any positive rough estimate was greater and overwrote the sentinel with a schema-inflated count, which then re-triggered compression on the next turn.

Changes

  • agent/conversation_loop.py: treat any negative last_prompt_tokens as "no real data yet" and skip the seed (_last >= 0 and ...).
  • tests: sentinel preserved against a large preflight estimate; real values still revise upward but never downward.

Scope note

The original PR (#40246) also added an _awaiting_suppression_count bounded-window state machine to should_compress() across context_compressor.py + conversation_compression.py. I left that out to keep blast radius minimal on the compression hot path — the sentinel guard alone fixes the root cause. If the usage=None partial-stream edge case (flag never clears) is worth defending, that suppression window can land as a separate, reviewed change.

Validation

TestPreflightSentinelGuard 3 passed. py_compile OK.

Salvaged from #40246 (@davidgut1982), credited; trimmed to the root-cause fix.

… preflight seed (#36718)

compress_context() sets last_prompt_tokens=-1 right after compression to
mark "no real API usage yet". The preflight display-seed used
`_preflight_tokens > (last_prompt_tokens or 0)`, and `(-1 or 0)` is -1
(truthy), so any positive rough estimate clobbered the sentinel with a
schema-inflated count — re-triggering compression on the next turn.
Treat any negative value as "no real data yet" and skip the seed.

Salvaged from #40246 as the minimal root-cause fix. The original also
added an `_awaiting_suppression_count` bounded-window state machine to
should_compress() across 3 files; left out here to keep blast radius
small — the sentinel guard alone fixes the re-fire. The suppression
window can be added separately if the usage=None-stub edge case warrants it.

Co-authored-by: davidgut1982 <davidgut1982@users.noreply.github.com>
@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

🔎 Lint report: salvage/40246-preflight-sentinel vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 9962 on HEAD, 9962 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 5167 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/agent Core agent loop, run_agent.py, prompt builder labels Jun 6, 2026
@teknium1 teknium1 merged commit 3c8f1de into main Jun 7, 2026
23 checks passed
@teknium1 teknium1 deleted the salvage/40246-preflight-sentinel branch June 7, 2026 08:56
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 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.

Bug: Context compression triggers repeatedly after fresh compress — last_prompt_tokens=-1 not updated until next API call

2 participants