Estimate compaction pressure from active context
Changed no-usage compaction preflight to estimate the canonical active model context rather than the full persisted display transcript.
openclaw/openclaw · #150623
Agent-runtime correctness
Healthy compacted sessions no longer trigger another budget compaction because superseded display history is mistaken for active model context.
Problem
A session could retain a large display history behind an existing compaction boundary while its actual model context contained only the summary and retained tail. When provider usage was unavailable, preflight counted the superseded history and performed an unnecessary second compaction.
Approach
Uses the canonical model-context reader for no-usage estimates, retaining the summary, active tail, tool pairs, and newer messages while excluding superseded history. Legacy projections keep their fallback, and explicitly selected incognito databases remain with their process-local owner.
Impact and scope
- Prevents repeated compaction of healthy sessions while preserving compaction for genuinely oversized active context.
- Aligns admission estimates with the exact context sent to the model instead of a user-facing persistence projection.
- Preserves persisted-session formats and legacy read behavior without a schema or migration change.
- Extends the context reader to recognize explicitly selected incognito databases without reopening them as empty worker-local stores.
Validation
- The SQLite regression failed on the baseline and passed all nine focused cases with the fix; the strengthened Gateway regression proved that no second compaction entry was persisted.
- A 650,000-character superseded history estimated at 162,512 tokens on the baseline and 18 tokens from active context with the fix; a 93,765-token active-context control still compacted.
- All 38 context-reader and 222 reply-agent tests passed after the incognito correction, together with isolated runtime builds and real Gateway turns. The exact-head aggregate CI was not fully green at merge: six checks failed, including a test-types shard and aggregate gates, while 72 checks passed.