fix: auto-compaction fires on fresh cached token counts (#66520)#2511
Open
BingqingLyu wants to merge 2 commits into
Open
fix: auto-compaction fires on fresh cached token counts (#66520)#2511BingqingLyu wants to merge 2 commits into
BingqingLyu wants to merge 2 commits into
Conversation
…reshold When a provider like Anthropic has a high prompt cache hit rate, totalTokens on the session entry includes cached tokens (input + cacheRead + cacheWrite) and is marked as fresh. Previously, runPreflightCompactionIfNeeded returned early when totalTokensFresh was true WITHOUT checking the compaction threshold, so auto-compaction never triggered — even at 153% of the context window. This change restructures the token resolution so that fresh persisted totals are checked against the compaction threshold (contextWindow - reserveTokens - softThreshold) before deciding whether to compact. The stale/transcript fallback path is preserved unchanged. Fixes openclaw#66520 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The fresh-token branch in runPreflightCompactionIfNeeded ran when shouldUseTranscriptFallback was false, but that predicate treated totalTokensFresh: undefined as NOT needing fallback (undefined === false evaluates to false). For legacy sessions where totalTokensFresh was never set, this caused the fresh-token path to use potentially stale persisted totals, triggering unnecessary compaction. Three changes: 1. Replace shouldUseTranscriptFallback with hasFreshPersistedTokens that requires totalTokensFresh === true explicitly. When undefined (legacy) or false (known-stale), falls through to transcript estimation. 2. In the transcript fallback branch, always run transcript estimation instead of skipping when resolveFreshSessionTotalTokens returns a value (that function also treats undefined freshness as fresh). 3. Add bail-out before shouldRunPreflightCompaction when transcript estimation returns no count and freshness is unconfirmed, preventing the gate function from falling back to stale totals via resolveFreshSessionTotalTokens. Resolves Codex P2 review on PR openclaw#66716. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
3 tasks
Owner
Author
|
| Function | File | Also modified by |
|---|---|---|
createReplyOperation |
src/auto-reply/reply/agent-runner-memory.test.ts |
#2272, #2523 |
runPreflightCompactionIfNeeded |
src/auto-reply/reply/agent-runner-memory.ts |
#2272, #2499 |
Recommendation: Coordinate with #2272, #2499, #2523 before merging.
Auto-detected by codegraph — a code graph analysis tool built on neug.
8 tasks
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
runPreflightCompactionIfNeededreturned early whentotalTokensFresh === truewithout checking the compaction threshold, so auto-compaction never triggered for sessions with fresh token counts — even at 153% of the context windowcacheReadfrom Anthropic prompt caching) are projected forward and checked againstcontextWindow - reserveTokens - softThresholdbefore deciding whether to compactDetails
When Anthropic's prompt cache absorbs nearly all tokens (100% hit rate),
derivePromptTokenscorrectly computesinput + cacheRead + cacheWrite(e.g. 99 + 305,000 + 0 = 305,099), and this is persisted astotalTokenswithtotalTokensFresh: true. However,runPreflightCompactionIfNeededhad this logic:The fix moves the threshold check into both branches (fresh and stale), using the fresh persisted value directly when available.
Edge cases verified
cacheRead=0, sototalTokensis justinput + cacheWrite, same as beforeTest plan
shouldRunPreflightCompactionunit tests: 100% cache hit triggers, below threshold skips, partial cache boundary correctrunPreflightCompactionIfNeededintegration tests: fresh tokens above threshold trigger compaction, below threshold skip, stale tokens use transcript fallback, heartbeat skipsFixes openclaw#66520
🤖 Generated with Claude Code