#21217: fix: memory prune command to prevent unbounded MEMORY.md growth
docs
cli
size: M
Cluster:
Memory Management Enhancements
## Summary
Closes #20720
- Add `openclaw memory prune` command that archives older MEMORY.md sections to `memory/YYYY-MM-DD-archived.md` when the file exceeds a configurable threshold
- Surface MEMORY.md file size in `openclaw memory status` with warnings when approaching the 20k bootstrap truncation limit
- Add `agents.defaults.memory.warnChars` and `agents.defaults.memory.pruneKeepChars` config options
- Update AGENTS.md and healthcheck skill docs with size management guidance
## Test plan
- Unit tests pass (12/12) — `pnpm test src/memory/prune.test.ts`
- Build passes — `pnpm build`
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR implements a memory pruning command to prevent `MEMORY.md` from growing beyond the 20k character bootstrap truncation limit. The implementation adds a `openclaw memory prune` CLI command that archives older sections to dated files in the `memory/` directory while keeping the most recent content. The `memory status` command now displays file size with warnings when approaching limits.
Key changes:
- Added `pruneMemoryFile()` function with smart heading-boundary splitting to preserve markdown structure
- CLI command supports `--dry-run`, configurable thresholds, and JSON output
- Config schema adds `agents.defaults.memory.warnChars` (default: 15k) and `agents.defaults.memory.pruneKeepChars` (default: 10k)
- Updated docs to guide users on memory management
- Comprehensive unit tests (12/12 passing) cover edge cases including dry-run, heading splits, appending to existing archives, and error handling
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with high confidence
- The implementation is well-tested (12/12 unit tests passing), follows existing codebase patterns, handles edge cases properly (missing files, empty files, existing archives), and includes comprehensive documentation. The atomic file write pattern (write to `.tmp`, then rename) prevents data loss. The feature addresses a real need (preventing unbounded MEMORY.md growth) without breaking changes.
- No files require special attention
<sub>Last reviewed commit: 6678ce7</sub>
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#11761: fix(gateway): expose memory/ directory files in agents.files RPC
by advaitpaliwal · 2026-02-08
78.5%
#23566: Gateway:expose memory/*.md in agents.files RPC
by SepehrShapouri · 2026-02-22
78.5%
#2884: fix: Create memory directory and symlink identity files during work...
by webdevtodayjason · 2026-01-27
78.2%
#21240: fix: GH#20607 prevent doctor from dropping custom config sections
by theognis1002 · 2026-02-19
77.4%
#17624: Fix memory flush YYYY-MM-DD placeholder resolution
by grunt3714-lgtm · 2026-02-16
77.2%
#15307: fix(memory): handle mixed/no-results QMD query output
by MohammadErfan-Jabbari · 2026-02-13
77.1%
#11364: fix(memory/qmd): prevent cascading failure when query fails or retu...
by blazerui · 2026-02-07
76.8%
#6060: feat(onboarding): add Memory Optimization step to onboarding wizard
by GodsBoy · 2026-02-01
76.5%
#14576: Fix/memory loss bugs
by ENCHIGO · 2026-02-12
76.4%
#19341: Mind Memory Fork
by juliopx · 2026-02-17
76.4%