Memory Defragmenter
Clean, merge, and optimize memory files to keep them lean and effective.
Purpose
Memory files grow over time and become:
- - Bloated with duplicates
- Stale with outdated info
- Scattered across tiers
- Inconsistent in format
This skill defragments them.
When to Run
| Trigger | Action |
|---|
| Weekly maintenance | Full defragment |
| Before context optimization |
Clean first |
| After many sessions | Remove stale entries |
| Explicit request | Clean specified files |
Defragmentation Workflow
Step 1: Analyze Memory State
CODEBLOCK0
Reports:
- - Files analyzed and sizes
- Duplicate detection
- Stale entry detection (30+ days old)
- Tier distribution
- Formatting consistency
Step 2: Generate Defragmentation Plan
CODEBLOCK1
Creates defragment-plan.md with:
- - Entries to merge
- Entries to archive
- Entries to delete
- Entries to promote/demote
Step 3: Review Plan
Read the plan and approve/modify before execution.
Step 4: Execute Defragmentation
CODEBLOCK2
Backs up original files first, then:
- - Merges duplicate entries
- Archives stale content to INLINECODE1
- Promotes hot entries to HOT tier
- Demotes cold entries to archive
- Normalizes formatting
Step 5: Verify
CODEBLOCK3
Checks:
- - All files readable
- No broken links
- Consistent formatting
- Tier limits respected
Memory Tiers Reference
| Tier | Location | Max Size | Age Policy |
|---|
| HOT | INLINECODE2 , INLINECODE3 | ≤100 lines each | Review monthly |
| WARM |
memory/*.md,
~/self-improving/domains/ | ≤200 lines each | Review quarterly |
| COLD |
archive/ | Unlimited | Archive at 90 days |
Safety Rules
- 1. Always backup before executing
- Review plan before applying
- Never delete - only archive
- Keep originals for 24h after
- Verify after every defragment
Files
- -
scripts/analyze_memory.py — Analyze memory state - INLINECODE8 — Plan and execute cleanup
- INLINECODE9 — Verify integrity
- INLINECODE10 — Defragmentation rules and patterns
技能名称: memory-defragmenter
详细描述:
内存碎片整理
清理、合并并优化内存文件,使其保持精简高效。
目的
内存文件会随时间增长,并出现以下问题:
- - 重复内容导致臃肿
- 过时信息导致陈旧
- 跨层级分散存储
- 格式不一致
本技能可对其进行碎片整理。
执行时机
先执行清理 |
| 多次会话后 | 移除陈旧条目 |
| 明确请求 | 清理指定文件 |
碎片整理工作流
步骤 1:分析内存状态
bash
python3 scripts/analyze_memory.py
报告内容:
- - 已分析文件及大小
- 重复内容检测
- 陈旧条目检测(超过30天)
- 层级分布情况
- 格式一致性
步骤 2:生成碎片整理计划
bash
python3 scripts/defragment.py --plan
创建 defragment-plan.md 文件,包含:
- - 需合并的条目
- 需归档的条目
- 需删除的条目
- 需升级/降级的条目
步骤 3:审查计划
在执行前阅读计划并批准/修改。
步骤 4:执行碎片整理
bash
python3 scripts/defragment.py --execute
首先备份原始文件,然后:
- - 合并重复条目
- 将陈旧内容归档至 archive/
- 将热点条目升级至 HOT 层级
- 将冷门条目降级至归档
- 规范化格式
步骤 5:验证
bash
python3 scripts/verify_memory.py
检查内容:
内存层级参考
| 层级 | 位置 | 最大容量 | 时效策略 |
|---|
| HOT | memory.md, ~/self-improving/memory.md | 各≤100行 | 每月审查 |
| WARM |
memory/*.md, ~/self-improving/domains/ | 各≤200行 | 每季度审查 |
| COLD | archive/ | 无限制 | 90天后归档 |
安全规则
- 1. 执行前务必备份
- 应用前审查计划
- 绝不删除——仅归档
- 保留原始文件24小时
- 每次碎片整理后验证
文件
- - scripts/analyzememory.py — 分析内存状态
- scripts/defragment.py — 规划并执行清理
- scripts/verifymemory.py — 验证完整性
- references/rules.md — 碎片整理规则与模式