openclaw - 💡(How to fix) Fix compaction can immediately re-enter on stale totals when no trustworthy post-compaction snapshot is persisted [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#61864Fetched 2026-04-08 02:53:19
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Author
Participants

After auto memory-flush compaction, the next turn can immediately re-enter compaction if the session store does not persist a trustworthy post-compaction token snapshot or explicitly invalidate stale totals.

This is not just a /status display issue. It affects the actual compaction gate.

Root Cause

This can cause:

  • repeated unnecessary compactions
  • confusing operator state (Compactions keeps increasing even during ordinary chat)
  • degraded continuity because the session keeps being compacted more often than intended
RAW_BUFFERClick to expand / collapse

Summary

After auto memory-flush compaction, the next turn can immediately re-enter compaction if the session store does not persist a trustworthy post-compaction token snapshot or explicitly invalidate stale totals.

This is not just a /status display issue. It affects the actual compaction gate.

User-visible symptoms

In our repro on a Telegram thread session:

  • a turn triggered auto memory-flush compaction
  • compactionCount increased
  • the next normal message immediately triggered another compaction again
  • this could repeat even though the live prompt had already been reduced

The session looked like it was compacting successfully, but the gate still behaved as if the old oversized prompt were active.

Repro shape

  1. Start with a session that is genuinely over the compaction threshold.
  2. Let auto memory-flush compaction run.
  3. End compaction without a trustworthy tokensAfter snapshot being persisted into the session store.
  4. Send the next normal user turn.

Observed:

  • the next turn can immediately re-enter compaction based on stale totals / fallback estimates from before compaction

Expected:

  • after compaction, the session should either:
    • persist a trustworthy post-compaction prompt/context snapshot, or
    • explicitly invalidate the old snapshot so the next turn does not reuse stale totals as fresh truth

Why this seems to happen

The compaction lifecycle updates compactionCount, but there are cases where the session store still ends up with effectively stale token state (for example totalTokens=null, totalTokensFresh=false, or an old larger snapshot still being preferred).

Once that happens, the next turn can re-enter compaction even though the prompt was already reduced.

Why this matters

This can cause:

  • repeated unnecessary compactions
  • confusing operator state (Compactions keeps increasing even during ordinary chat)
  • degraded continuity because the session keeps being compacted more often than intended

Suggested fix

Treat post-compaction token state as an invariant:

  • if compaction can produce a trustworthy tokensAfter, persist it immediately
  • otherwise explicitly invalidate stale totals before the next turn
  • the next-turn compaction gate should never treat a pre-compaction snapshot as fresh after compaction has already completed

extent analysis

TL;DR

Ensure that post-compaction token state is treated as an invariant by persisting a trustworthy tokensAfter snapshot or explicitly invalidating stale totals before the next turn.

Guidance

  • Verify that the session store is correctly updating the totalTokens and totalTokensFresh fields after compaction, and that these values are being used to determine whether to re-enter compaction.
  • Check the compaction lifecycle to ensure that it is properly handling cases where the tokensAfter snapshot is not trustworthy, and that it is explicitly invalidating stale totals in such cases.
  • Review the logic for determining when to re-enter compaction, and ensure that it is not relying on pre-compaction snapshots as fresh after compaction has completed.
  • Consider adding logging or debugging statements to track the values of compactionCount, totalTokens, and totalTokensFresh throughout the compaction process to help identify where the issue is occurring.

Example

No code snippet is provided as the issue does not include specific code references.

Notes

The suggested fix implies that the issue is related to the handling of post-compaction token state, and that treating this state as an invariant can resolve the problem. However, the exact implementation details will depend on the specific code and system architecture.

Recommendation

Apply the suggested fix by treating post-compaction token state as an invariant, as this is likely to resolve the issue of repeated unnecessary compactions and degraded continuity.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING