Skip to content

fix: prevent overflow recovery from bailing when observed tokens unavailable#351

Merged
jalehman merged 2 commits into
Martian-Engineering:mainfrom
kitcommerce:fix/overflow-recovery-compaction-bail
Apr 9, 2026
Merged

fix: prevent overflow recovery from bailing when observed tokens unavailable#351
jalehman merged 2 commits into
Martian-Engineering:mainfrom
kitcommerce:fix/overflow-recovery-compaction-bail

Conversation

@kitcommerce

Copy link
Copy Markdown
Contributor

Summary

  • When the preemptive context overflow guard fires during the tool loop, the error message doesn't include an observed token count, so observedTokens is undefined in the overflow recovery path
  • 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 actually overflowing
  • This causes the session to be nuked and restarted instead of being compacted

Fix

When force=true (overflow recovery) and observedTokens is undefined, pass tokenBudget as currentTokens to compactUntilUnder(). This ensures the convergence loop knows we're at least at the budget and proceeds with compaction.

The fix is 6 lines in compact() in engine.ts.

Reproduction

  1. Use a small-context model like gemini-2.5-flash-lite (150K context window)
  2. Accumulate messages in a session until the preemptive context guard fires
  3. Observe: auto-compaction failed for google/gemini-2.5-flash-lite: already under target followed by session restart
  4. After fix: auto-compaction succeeded for google/gemini-2.5-flash-lite; retrying prompt

Test plan

  • Verified fix in production — compaction now runs 3 leaf passes (108745 → 87472 tokens) and prompt retry succeeds
  • Unit test for compactUntilUnder with force=true and no currentTokens

🤖 Generated with Claude Code

Kit (OpenClaw) and others added 2 commits April 9, 2026 17:21
…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.
@jalehman

jalehman commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Thank you!

@jalehman jalehman merged commit ea1f80d into Martian-Engineering:main Apr 9, 2026
1 check passed
@github-actions github-actions Bot mentioned this pull request Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants