Bug Description
When switching between models with different context window sizes (e.g., 200K → 1M tokens or vice versa), the memoryFlush.softThresholdTokens value (default: 4000) becomes ineffective because it's an absolute value rather than relative to the context window.
Steps to Reproduce
- Start a session with a model that has a ~200K context window (e.g., Claude Sonnet)
- Switch to a model with a 1M context window (e.g., Claude Opus)
- Have a long conversation until auto-compaction triggers
- Observe that
memoryFlushCompactionCount is never set — the memory flush never fires
Expected Behavior
Memory flush should trigger before compaction regardless of context window size, saving durable memories to disk before context is compacted.
Actual Behavior
With default softThresholdTokens: 4000:
- 200K context: flush threshold =
200K - 20K - 4K = 176K ✅ (works fine)
- 1M context: flush threshold =
1M - 20K - 4K = 976K ❌ (never reached)
Compaction is triggered reactively by API context overflow (e.g., a large tool result pushing past the limit), which typically happens around ~170K tokens. The flush threshold at 976K is never reached.
This also breaks in the reverse direction: switching from a large context model back to a smaller one doesn't recalibrate the threshold.
Root Cause
In src/auto-reply/reply/memory-flush.ts, shouldRunMemoryFlush() computes:
threshold = contextWindowTokens - reserveTokensFloor - softThresholdTokens
This threshold is absolute. The softThresholdTokens default of 4000 was presumably tuned for ~200K context windows and doesn't scale.
Suggested Fix
Consider one or more of:
- Make
softThresholdTokens a percentage of the context window (e.g., softThresholdRatio: 0.15 → flush at 85% of context)
- Auto-scale the default based on context window size (e.g.,
max(4000, contextWindow * 0.15))
- Recalculate on model switch — when the active model changes, recompute the effective threshold
- Document the interaction — at minimum, warn users that switching to large-context models requires manual
softThresholdTokens adjustment
Workaround
Manually set a large softThresholdTokens in openclaw.json:
{
"agents": {
"defaults": {
"compaction": {
"memoryFlush": {
"enabled": true,
"softThresholdTokens": 850000
}
}
}
}
}
This sets the flush threshold to ~130K tokens, which fires before typical compaction at ~170K.
Environment
- OpenClaw v2026.2.13
- Model: Claude Opus 4 (1M context) / Claude Sonnet 4.5 (200K context)
- Compaction mode: safeguard
- Session had 10 compactions with
memoryFlushCompactionCount never set
Impact
Every user who switches to a large-context model (or starts with one) silently loses the pre-compaction memory flush protection, leading to context loss on compaction.
Bug Description
When switching between models with different context window sizes (e.g., 200K → 1M tokens or vice versa), the
memoryFlush.softThresholdTokensvalue (default: 4000) becomes ineffective because it's an absolute value rather than relative to the context window.Steps to Reproduce
memoryFlushCompactionCountis never set — the memory flush never firesExpected Behavior
Memory flush should trigger before compaction regardless of context window size, saving durable memories to disk before context is compacted.
Actual Behavior
With default
softThresholdTokens: 4000:200K - 20K - 4K = 176K✅ (works fine)1M - 20K - 4K = 976K❌ (never reached)Compaction is triggered reactively by API context overflow (e.g., a large tool result pushing past the limit), which typically happens around ~170K tokens. The flush threshold at 976K is never reached.
This also breaks in the reverse direction: switching from a large context model back to a smaller one doesn't recalibrate the threshold.
Root Cause
In
src/auto-reply/reply/memory-flush.ts,shouldRunMemoryFlush()computes:This threshold is absolute. The
softThresholdTokensdefault of 4000 was presumably tuned for ~200K context windows and doesn't scale.Suggested Fix
Consider one or more of:
softThresholdTokensa percentage of the context window (e.g.,softThresholdRatio: 0.15→ flush at 85% of context)max(4000, contextWindow * 0.15))softThresholdTokensadjustmentWorkaround
Manually set a large
softThresholdTokensinopenclaw.json:{ "agents": { "defaults": { "compaction": { "memoryFlush": { "enabled": true, "softThresholdTokens": 850000 } } } } }This sets the flush threshold to ~130K tokens, which fires before typical compaction at ~170K.
Environment
memoryFlushCompactionCountnever setImpact
Every user who switches to a large-context model (or starts with one) silently loses the pre-compaction memory flush protection, leading to context loss on compaction.