fix: prevent overflow recovery from bailing when observed tokens unavailable#351
Merged
jalehman merged 2 commits intoApr 9, 2026
Conversation
…ailable When the preemptive context overflow guard fires during the tool loop, the error message does not include an observed token count. This means observedTokens is undefined when the overflow recovery calls compact() with force=true. compactUntilUnder() then uses only the stored token count (which is low because afterTurn hasn't ingested the current turn yet) and bails with "already under target" — even though the live context is overflowing. Fix: when force=true and observedTokens is undefined, pass tokenBudget as currentTokens so compactUntilUnder knows we're at least at the budget and proceeds with compaction instead of bailing. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add a regression test for PR Martian-Engineering#351's overflow-recovery path when force=true but the runtime does not provide currentTokenCount, and add a patch changeset for the recovery behavior fix. Regeneration-Prompt: | Review PR Martian-Engineering#351, which fixes forced overflow recovery when OpenClaw reports a context overflow during the tool loop without an observed token count. Preserve the runtime fix in src/engine.ts, then add targeted regression coverage proving engine.compact() passes currentTokens equal to tokenBudget into compactUntilUnder() when force=true and currentTokenCount is absent. Keep the existing observed-token test intact, and add a patch changeset because this changes user-visible recovery behavior after overflow.
Contributor
|
Thank you! |
Merged
This was referenced Jun 4, 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.
Summary
observedTokensisundefinedin the overflow recovery pathcompactUntilUnder()then uses only the stored token count (which is low becauseafterTurnhasn't ingested the current turn yet) and bails with "already under target" — even though the live context is actually overflowingFix
When
force=true(overflow recovery) andobservedTokensisundefined, passtokenBudgetascurrentTokenstocompactUntilUnder(). This ensures the convergence loop knows we're at least at the budget and proceeds with compaction.The fix is 6 lines in
compact()inengine.ts.Reproduction
gemini-2.5-flash-lite(150K context window)auto-compaction failed for google/gemini-2.5-flash-lite: already under targetfollowed by session restartauto-compaction succeeded for google/gemini-2.5-flash-lite; retrying promptTest plan
compactUntilUnderwithforce=trueand nocurrentTokens🤖 Generated with Claude Code