fix(compaction): force/truncated bypass the minMessages guard - #73
Merged
Conversation
Autocompaction was unreachable on the first turn even when the model ballooned the input past 200k tokens, because shouldCompact()'s minMessages>=6 guard returned false BEFORE the force/contextWasTruncated short-circuit ran. Symptom in production: 3 separate sessions ran a single assistant turn with many tool calls (clone + explore + read…) that pushed totalTokenInput past 199k. messageCount stayed at 2 (user + assistant). compactionCount stayed at 0. The 'wrap-up' system injection fired when the budget warning hit ~80%, the model emitted a 'context exhausted' summary, and the session went idle without ever compacting. Fix: move the force/contextWasTruncated check above the minMessages guard. They are emergency signals — the caller has already determined compaction is warranted. We still require >=2 real messages because compacting one or zero messages is a no-op or crash. Tests: - New regression test: force=true with 2 messages now returns true - New regression test: contextWasTruncated=true with 3 messages now returns true - New edge case: force=true with 1 message still returns false - All 21 existing compaction tests still pass
jonnyparris
marked this pull request as ready for review
May 24, 2026 20:18
This was referenced May 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.
What
Move the
force/contextWasTruncatedshort-circuit above the minMessages guard inshouldCompact().Why
Autocompaction is wired in three places (loop-entry, mid-loop, pre-step) but it was unreachable on the first turn when the model ballooned input tokens — exactly the scenario where compaction matters most.
Reproduced this morning by running 3 prompts on fresh sessions. Each had:
messageCount: 2(user + assistant)totalTokenInput: 199,963 / 199,662 / 213,691compactionCount: 0Trace through the code:
onChatMessageruns one streamText iterationrole:assistantrow —messageCountstays at 2runWatchdogCheck/ wrap-up / pre-step compaction all callmaybeCompactContext({ force: true })maybeCompactContextcallsshouldCompact({ messageCount: 2, force: true, … })shouldCompactreturns false becausemessageCount < 6, ignoringforceThe minMessages guard was added to avoid pointlessly compacting brand-new sessions (compacting one message is a no-op). But
forceandcontextWasTruncatedare emergency signals — the caller has already decided compaction is warranted. They should bypass the guard.How
Reordered the two checks.
force/contextWasTruncatednow short-circuit immediately, requiring onlyrealMessageCount >= 2(you need something to compact). Non-emergency compaction still respects the minMessages threshold.Tests
force=true bypasses the minMessages guard (2 messages is enough)contextWasTruncated=true bypasses the minMessages guardforce=true with only 1 real message still returns false (nothing to compact)compaction-policy-unit,compaction-pipeline,compaction-e2e)Linked context
memory/learnings/dodo-orchestrator-model-matters.mdbeep-boop-🤖