fix(compaction): clear stale usage.totalTokens after compaction to prevent double-compact#26529
Closed
Sid-Qin wants to merge 2 commits intoopenclaw:mainfrom
Closed
fix(compaction): clear stale usage.totalTokens after compaction to prevent double-compact#26529Sid-Qin wants to merge 2 commits intoopenclaw:mainfrom
Sid-Qin wants to merge 2 commits intoopenclaw:mainfrom
Conversation
…event double-compact After compaction, preserved assistant messages still carried their original usage.totalTokens. When a subsequent prompt() call hit _findLastAssistantMessage() → _checkCompaction(), the stale value exceeded the shouldCompact threshold and triggered an immediate second compaction that destroyed all preserved messages (0 kept). Clear usage.totalTokens from preserved assistant messages in handleAutoCompactionEnd when willRetry is false, so the upstream _checkCompaction guard sees no token count and skips the check. Closes openclaw#26458 Co-authored-by: Cursor <cursoragent@cursor.com>
Fixes TS2352 type error in clearStaleUsageOnPreservedMessages. Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
|
A fix for this issue has landed upstream in another PR, closing. |
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
usage.totalTokensfrom before compaction. The upstream_checkCompaction()reads this stale value and immediately triggers a second compaction that destroys all preserved messages.src/agents/pi-embedded-subscribe.handlers.compaction.ts— addedclearStaleUsageOnPreservedMessages()function, called inhandleAutoCompactionEndwhenwillRetryis false. Deletesusage.totalTokensfrom preserved assistant messages so the upstream check skips the stale guard.src/plugins/wired-hooks-compaction.test.ts— added 2 new tests: one verifyingtotalTokensis cleared after final compaction, another verifying it is NOT cleared whenwillRetryis true.inputTokens/outputTokensfields; retry behavior.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
User-visible / Behavior Changes
Security Impact (required)
NoNoNoNoNoRepro + Verification
Environment
Steps
Expected
Actual
inputTokens/outputTokensbut havetotalTokenscleared.Evidence
Human Verification (required)
Compatibility / Migration
YesNoNoFailure Recovery (if this breaks)
src/agents/pi-embedded-subscribe.handlers.compaction.tstotalTokensis needed for other purposes after compaction, clearing it could affect downstream consumers — but the upstream_checkCompactionis the only known consumer.Risks and Mitigations
usage.totalTokensafter compaction. Mitigation: Only_checkCompactionuses this field for the compaction guard;inputTokens/outputTokensare preserved for usage tracking.Greptile Summary
Adds
clearStaleUsageOnPreservedMessages()to remove staleusage.totalTokensfrom assistant messages after compaction completes. The upstream_checkCompaction()guard in pi-agent-core readstotalTokensto determine if another compaction is needed; stale pre-compaction values trigger a spurious second compaction that destroys all preserved messages.Key changes:
totalTokenswhenwillRetryis false (final compaction)inputTokensandoutputTokensfor usage trackingThe fix is minimal and targeted. The
totalTokensfield is derived frominputTokens + outputTokensin usage normalization (src/agents/usage.ts:81), so removing it after compaction doesn't affect downstream usage tracking which can still use the component fields.Confidence Score: 4/5
totalTokensconsumersinputTokens/outputTokensfields are preserved, so usage tracking remains intact. However, deducting one point because: (1) external code might rely ontotalTokensbeing present after compaction (though the PR notes only_checkCompactionis known to use it), and (2) the fix relies on understanding of upstream pi-agent-core behavior that isn't directly tested in this codebaseLast reviewed commit: cd22262