#8919: Pr/memory flush improvements
stale
Cluster:
Memory Management Enhancements
OpenClaw's current memory flush mechanism has two significant gaps that lead to context loss for long-running agents:
1. Single-shot flush before compaction — The existing softThresholdTokens trigger fires once, giving the agent a single opportunity to persist context. If it doesn't save everything in that one shot, that context is gone forever.
2. No flush on session reset — When a session resets (/new, rotation), there's zero opportunity to save. The session is destroyed immediately. For agents maintaining long-running context (personal assistants, project managers, coding agents mid-task), this is a significant data loss vector.
Solution
Two complementary, opt-in features:
1. Multi-threshold checkpoints
Configure multiple percentage-based checkpoints that fire as the context window fills:
compaction:
memoryFlush:
checkpoints:
- percent: 50
prompt: "Save key decisions and context so far."
- percent: 75
prompt: "Save recent work items and pending tasks."
- percent: 90
prompt: "Emergency save — context window almost full."
2. Pre-destructive before-clear flush
One flush turn before session reset:
compaction:
memoryFlush:
beforeClear: true
minTokensForFlush: 1000
beforeClearPrompt: "Session is resetting. Save anything important."
Implementation: 2 new files, 6 modified — all additive
Testing: 29 new tests, 578+281 existing all pass
Backward compatible: all opt-in, zero behavior change without config
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Adds two new memory-flush mechanisms to reduce context loss for long-running agents: (1) percentage-based multi-threshold “checkpoint” flush turns as the context window fills, and (2) a pre-destructive “before clear” flush turn intended to run when sessions reset. This wires new decision logic into the reply runner (`runMemoryFlushIfNeeded` now checks checkpoint thresholds before the legacy single-shot soft-threshold flush; `runReplyAgent` can invoke a new `runBeforeClearFlush` path) and extends session state to track which checkpoints have fired.
Configuration support is added via agent defaults schema (`compaction.memoryFlush.checkpoints`, `beforeClear`, and related prompts/minTokens), plus new tests covering checkpoint selection, fired-checkpoint suppression, and before-clear gating.
<h3>Confidence Score: 3/5</h3>
- Generally safe, but there are a couple behavioral mismatches with the PR’s stated intent that should be addressed before merging.
- Core logic is straightforward and well-tested, but (1) `beforeClear` currently defaults on, which contradicts the opt-in claim and can introduce extra model calls, and (2) the before-clear flush is only triggered for explicit `/new` resets, not rotation/freshness resets, so one of the advertised scenarios is missed. Checkpoint parsing also bypasses schema constraints at runtime.
- src/auto-reply/reply/memory-flush.ts, src/auto-reply/reply/agent-runner.ts, src/auto-reply/reply/session.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#10644: feat: add runDuringHeartbeats option for memory flush (AI Assisted)
by tripphillips · 2026-02-06
84.6%
#14576: Fix/memory loss bugs
by ENCHIGO · 2026-02-12
82.7%
#18663: feat: progressive compaction escalation and mechanical flush fallback
by Adamya05 · 2026-02-16
81.0%
#8900: fix: improve default memory flush prompts with urgency and specifics
by joetomasone · 2026-02-04
80.5%
#9012: fix(memory): resilient flush for large sessions [AI-assisted]
by cheenu1092-oss · 2026-02-04
80.1%
#4999: fix(memory-flush): use contextTokens instead of totalTokens for thr...
by Farfadium · 2026-01-30
80.0%
#13792: Claude/openclaw token efficiency q ni2s
by imecostalima · 2026-02-11
79.9%
#14021: feat(compaction): optional memory flush before manual /compact
by phenomenoner · 2026-02-11
79.5%
#15945: fix(memory-flush): only write memoryFlushCompactionCount when compa...
by aldoeliacim · 2026-02-14
78.9%
#10273: fix(agents): detect and auto-compact mid-run context overflow
by terryops · 2026-02-06
78.8%