← Back to PRs

#8919: Pr/memory flush improvements

by shortbus open 2026-02-04 16:00 View on GitHub →
stale
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