Memory Guardian
Prevent memory loss and context overflow. 3-layer architecture with automated health checks.
Architecture
CODEBLOCK0
Health Checks
Run python3 scripts/memory_check.py on heartbeat or manually. Catches:
- - Files over 300 lines (split needed)
- Total memory over 3,000 lines (yellow alert → prune)
- Stale session files (>7 days → promote + delete)
- Duplicate content across files
- MEMORY.md index inconsistency
- Orphan files not referenced anywhere
Danger Zones
| Total Lines | Risk | Action |
|---|
| < 2,000 | 🟢 Green | Normal operations |
| 2,000-3,000 |
🟡 Yellow | Prune sessions, compress old entries |
| 3,000-5,000 | 🟠 Orange | Aggressive promotion to permanent, archive sessions |
| 5,000+ | 🔴 Red | Semantic search returns noise → migrate to FAISS |
Promotion Protocol
Before deleting ANY session file, extract:
- 1. Credentials/keys → permanent (NEVER lose these)
- Architecture decisions → permanent/business-strategy.md
- Infrastructure changes → permanent/outreach-infrastructure.md
- New project summaries → permanent/projects/[name].md
- User preferences → permanent/user-system.md
Everything else (debug logs, intermediate results) → delete.
Migration Triggers
| Trigger | Action |
|---|
| permanent/ > 5,000 lines | Migrate to FAISS vector store |
| Batch value-stack > 100 items |
Add embeddings for semantic retrieval |
| ChatGPT corpus loaded | FAISS mandatory |
| Cross-batch connections > 50 | Consider Neo4j knowledge graph |
Emergency Recovery
Everything is git-tracked. If memory corrupts:
- 1.
git log memory/ → find last good state - INLINECODE2 → restore
- Rebuild MEMORY.md index from INLINECODE3
Context Budget
| Component | Tokens | Notes |
|---|
| System prompt | ~2,000 | Fixed |
| MEMORY.md |
~1,500 | Keep lean |
| Active session | ~1,000 | Today only |
| memory_search | ~500 | On-demand |
|
Total overhead |
~5,000 | Of 200K+ available |
记忆守护者
防止记忆丢失和上下文溢出。采用三层架构并配备自动化健康检查。
架构
第一层:工作记忆(会话文件,保留7天)
↓ 删除前将持久化事实提升
第二层:永久记忆(永不修剪,仅手动操作)
↓ 达到5000行时迁移至向量存储
第三层:存档(批量文档、价值堆栈——永久存储于磁盘)
健康检查
在心跳检测或手动时运行 python3 scripts/memory_check.py。可检测:
- - 超过300行的文件(需拆分)
- 总记忆超过3000行(黄色警报→修剪)
- 过期的会话文件(超过7天→提升并删除)
- 跨文件重复内容
- MEMORY.md索引不一致
- 未被引用的孤立文件
危险区域
| 总行数 | 风险 | 操作 |
|---|
| < 2,000 | 🟢 绿色 | 正常运行 |
| 2,000-3,000 |
🟡 黄色 | 修剪会话,压缩旧条目 |
| 3,000-5,000 | 🟠 橙色 | 积极提升至永久记忆,归档会话 |
| 5,000+ | 🔴 红色 | 语义搜索产生噪音→迁移至FAISS |
提升协议
删除任何会话文件前,提取:
- 1. 凭证/密钥→永久记忆(绝不可丢失)
- 架构决策→永久记忆/business-strategy.md
- 基础设施变更→永久记忆/outreach-infrastructure.md
- 新项目摘要→永久记忆/projects/[name].md
- 用户偏好→永久记忆/user-system.md
其余内容(调试日志、中间结果)→删除。
迁移触发条件
| 触发条件 | 操作 |
|---|
| permanent/ > 5,000行 | 迁移至FAISS向量存储 |
| 批量价值堆栈 > 100项 |
添加嵌入向量用于语义检索 |
| 加载ChatGPT语料库 | 强制使用FAISS |
| 跨批次连接 > 50 | 考虑使用Neo4j知识图谱 |
紧急恢复
所有内容均通过Git追踪。若记忆损坏:
- 1. git log memory/ → 查找最后一个正常状态
- git checkout -- memory/ → 恢复
- 通过 ls memory/permanent/ 重建MEMORY.md索引
上下文预算
| 组件 | Token数 | 备注 |
|---|
| 系统提示词 | ~2,000 | 固定 |
| MEMORY.md |
~1,500 | 保持精简 |
| 活跃会话 | ~1,000 | 仅限今日 |
| memory_search | ~500 | 按需调用 |
|
总开销 |
~5,000 | 可用200K+中的部分 |