Memory Fortress 🏰
A unified memory management system that prevents agents from forgetting what they did yesterday.
Built from five proven patterns:
- - Compaction-aware saving → disk is truth, write before you lose it
- Boot sequence + state files → Dory-proof pattern
- Domain organization → bloat prevention, ≤10KB MEMORY.md
- Memory types + priority → contradiction detection, scoring
- Promote / demote → natural memory decay over time
Mental model
CODEBLOCK0
1. Boot sequence — run at the start of every session
Order matters. Agents execute this automatically.
CODEBLOCK1
Post-boot status line:
CODEBLOCK2
What is NOT a boot step
- - Domain files → only via
memory_search, on-demand - Archive → rarely, on explicit request
- Other agents' memory → NEVER
2. Dory-proof pattern — when a task arrives
When the user gives you a task:
- 1. IMMEDIATELY write their EXACT words → INLINECODE1
- Then interpret what it means
- Then do the work
- Done → mark complete, remove from ACTIVE
Why: Paraphrasing causes drift. Exact words preserve intent even after context flush.
state/ACTIVE.md format
CODEBLOCK3
state/HOLD.md format
[YYYY-MM-DD HH:MM | session] Item — reason for blocking
⚠️
Every agent checks this at boot — FIRST step! If something is on HOLD, do NOT proceed.
state/DECISIONS.md format
[YYYY-MM-DD HH:MM | session] Decision description — context/rationale
Archive after 48 hours → daily log or MEMORY.md (if durable).
3. Compaction-aware saving — "Write Before Lose"
Session RAM is lost on compaction. Save before that happens.
When to write to files immediately (don't wait for compaction):
| Event | Where to write |
|---|
| Decision made | INLINECODE2 + daily log |
| User preference discovered |
MEMORY.md (if durable) or daily log |
| Task received |
state/ACTIVE.md (Dory-proof) |
| Something is blocked |
state/HOLD.md |
| Error occurred + lesson learned | Daily log +
MEMORY.md if generalizable |
| Important fact discovered | Daily log, promote if durable |
| Work completed |
state/ACTIVE.md update + log entry |
Rule: if it matters → write to file IMMEDIATELY. Don't defer to end of session.
4. Memory scoring — what goes into MEMORY.md?
Not every memory deserves a spot in MEMORY.md. Score it on 4 axes (0–3):
| Axis | 0 | 1 | 2 | 3 |
|---|
| Durability | Invalid tomorrow | Valid for weeks | Valid for months | Valid for years+ |
| Reuse |
One-time | Occasional | Frequent | Every session |
|
Impact | Trivial | Good to know | Changes output | Changes decisions |
|
Uniqueness | Obvious | Slightly useful | Hard to re-derive | Impossible without it |
Goes into MEMORY.md if:
- - Sum ≥ 8, OR
- Any axis = 3 AND sum ≥ 6
Everything else → daily log (and from there to a domain file if it fits thematically).
5. Domain organization — keeping MEMORY.md ≤10KB
MEMORY.md must stay ≤10KB. If it grows larger:
Split into domain files
CODEBLOCK6
Rules:
- - MEMORY.md = summaries, references, most important facts
- domains/*.md = details, searched via INLINECODE8
- archive/ = old logs, rarely needed
MEMORY.md section format (with domain references):
CODEBLOCK7
Maintenance (weekly or during heartbeat):
- 1. Check MEMORY.md size — if >10KB, split
- Remove or archive stale entries
- Find duplicates (daily log vs MEMORY.md)
- Update domain files from daily logs
6. Memory types and priority
Every saved memory gets a type and priority.
Types (ID prefixes)
| Prefix | Type | Example |
|---|
| INLINECODE9 | Decision | "We use PostgreSQL in production" |
| INLINECODE10 |
Preference | "English, direct tone" |
|
FACT | Durable fact | "Voice PE = ESP32-S3, 16MB flash" |
|
POLICY | Rule/invariant | "MEMORY.md never >10KB" |
|
LESSON | Lesson learned | "VLA unsafe on 4KB stack" |
|
ERR | Known error | "OIDC trailing slash incompatible" |
Priority (1–10)
| Level | Meaning | Example |
|---|
| 1–3 | Low, good to know | A tool version number |
| 4–6 |
Medium, useful | Project status |
| 7–8 | High, important | Architectural decision |
| 9–10 | Critical, never forget | Security rule, user preference |
7. Promote / Demote — memory lifecycle
Promote (strengthen)
A memory is promoted when:
- - It becomes relevant again (found and used in search)
- The user references it
- It grounds a decision
Promote action: raise priority, move from domain file → MEMORY.md summary
Demote (decay)
A memory weakens when:
- - Not referenced in >30 days
- Outdated (project closed, tool replaced)
- Contradicted by a newer memory
Demote action: MEMORY.md → domain file, or domain → archive
Contradiction handling
When two memories contradict each other:
- 1. The newer one takes priority (unless explicitly overridden)
- Mark the old one INLINECODE15
- Daily log note: why it changed
8. Search strategy
Core rule: always memory_search → memory_get
- 1.
memory_search(query) — max 6 results - Pick the best 1–2 matches
- INLINECODE19 — exact line range
- Inject only the minimum necessary text into context
Search order:
- 1.
state/ files (loaded at boot, no search needed) - INLINECODE21 (loaded at boot in main session)
- INLINECODE22 (memorysearch)
- INLINECODE23 (memorysearch)
- INLINECODE24 (last resort)
Don't do this:
- - ❌ Read entire files "just in case"
- ❌ Load all domain files at boot
- ❌ Answer questions about the past without searching first
9. Multi-agent rules
When multiple agents share a workspace, memory stays isolated.
| Rule | Details |
|---|
| Own workspace | Every agent writes to its own workspace |
| No cross-read |
Don't read another agent's MEMORY.md |
|
Communication | Use
sessions_send, not memory files |
|
Shared state | Only explicitly shared folders (e.g.
shared/roundtable/) |
|
Private context | DM-originated info stays private, not placed in shared locations |
10. Anti-patterns — don't do this
| ❌ Anti-pattern | ✅ Instead |
|---|
| "I'll mentally remember this" | Write to file IMMEDIATELY |
| Paste chat transcript into MEMORY.md |
Daily log + summary |
| MEMORY.md >10KB | Split into domain files |
| Answer questions about the past without searching |
memory_search →
memory_get |
| 10 tasks in ACTIVE.md | 1 active task, rest in STAGING or project files |
| Paraphrase in ACTIVE.md | User's EXACT words |
| Store a secret key | Store only that it
exists, not its value |
| Never archive daily logs | 14+ days old →
memory/archive/YYYY-MM/ |
| Every memory priority 10 | Score it! Most things are 4–6 |
| Skip HOLD.md at boot | It's the FIRST step at every boot |
11. .learnings/ — error and lesson logging
The memory/.learnings/ folder provides structured logging for continuous agent improvement.
Detection triggers — watch for these
| User says | Action |
|---|
| "No, that's wrong..." / "Actually..." | LEARNINGS.md, category: INLINECODE31 |
| "Can you also..." / "I wish you could..." |
FEATURE_REQUESTS.md |
| "That's outdated..." / "It changed since..." | LEARNINGS.md, category:
knowledge_gap |
| Command exits with non-zero code | ERRORS.md |
| Found a better approach | LEARNINGS.md, category:
best_practice |
When to write here (direct append, not memory_search):
| Event | File | Category |
|---|
| Tool/command fails | INLINECODE34 | — |
| User corrects you |
LEARNINGS.md |
correction |
| Something didn't work, you fixed it |
LEARNINGS.md |
best_practice |
| Missing capability requested |
FEATURE_REQUESTS.md | — |
| Knowledge was outdated |
LEARNINGS.md |
knowledge_gap |
Structured ID format
Every entry gets a unique ID:
CODEBLOCK8
- - TYPE:
LRN (learning), ERR (error), FEAT (feature request) - YYYYMMDD: current date
- XXX: sequential number or 3 random chars (
001, A7B)
Entry header:
CODEBLOCK9
When resolved:
CODEBLOCK10
Quick review commands
CODEBLOCK11
Promotion logic (after 3 occurrences):
- - Behavioral pattern → agent personality file (e.g.
SOUL.md) - Workflow improvement → operational manual (e.g.
AGENTS.md) - Tool gotcha → tool config file (e.g.
TOOLS.md) - Generally useful →
MEMORY.md (with scoring)
Pattern-Key tracking:
If the same error/pattern recurs: add
See Also: ERR-YYYYMMDD-XXX link + increment
Recurrence-Count.
3+ occurrences in 2+ different contexts →
mandatory promotion.
Skill extraction — when to turn a learning into a skill?
A learning qualifies for skill extraction when ANY of these apply:
| Criterion | Description |
|---|
| Recurring | 2+ See Also links to similar entries |
| Verified |
Status is
resolved, solution works |
|
Non-obvious | Required actual debugging/investigation |
|
Broadly applicable | Not project-specific; useful elsewhere |
|
User-flagged | User says "save this as a skill" or similar |
Quality gates (check before extraction):
- - [ ] Solution is tested and working
- [ ] Description is clear without original context
- [ ] No hardcoded values (paths, API keys, names)
- [ ] Lowercase, hyphenated filename (e.g.
api-retry-pattern) - [ ] Single SKILL.md file — no README.md alongside
Extraction steps:
- 1. Create
skills/<skill-name>/SKILL.md from the learning - Proper frontmatter:
name + single-line INLINECODE58 - Update original entry: INLINECODE59
- Publish if broadly useful
12. Maintenance schedule
| Frequency | Task |
|---|
| Every session | Execute boot sequence |
| Every task |
Dory-proof pattern (ACTIVE.md) |
|
On important events | Immediate file write |
|
Daily | Review daily log, clean state files |
|
Weekly | MEMORY.md size check, update domain files |
|
Biweekly | Archive old daily logs |
|
Monthly | Demote stale memories, resolve contradictions |
|
On error |
.learnings/ERRORS.md entry immediately |
|
On correction |
.learnings/LEARNINGS.md correction entry immediately |
Installation
1. Create the state folder
CODEBLOCK12
2. Create the memory folder structure
CODEBLOCK13
3. Add to your agent's operational manual
Add the boot sequence to your
AGENTS.md (or equivalent config file):
- - Load
state/HOLD.md, state/ACTIVE.md, state/DECISIONS.md first - Load
memory/YYYY-MM-DD.md (today + yesterday) - Load
MEMORY.md only in the main/direct session
4. Add the "Write Before Lose" rule
Ensure your agent config instructs writing to files immediately on: decisions, preferences, task receipt, blockers, errors.
"Memory is what separates a tool from an ally."
记忆堡垒 🏰
一个统一的记忆管理系统,防止智能体忘记昨天做过的事。
由五种经过验证的模式构建:
- - 压缩感知保存 → 磁盘即真相,在丢失前写入
- 启动序列 + 状态文件 → 多莉防失忆模式
- 领域组织 → 防止膨胀,MEMORY.md ≤10KB
- 记忆类型 + 优先级 → 矛盾检测,评分机制
- 提升/降级 → 随时间自然遗忘
心智模型
┌─────────────────────────────────────────────────────┐
│ 记忆堡垒 │
├─────────────────────────────────────────────────────┤
│ │
│ 🔴 会话内存 (SESSION RAM) │
│ 短期记忆,压缩时丢失 │
│ → 重要内容立即写入文件 │
│ │
│ 🟡 状态文件 (state/) │
│ 当前任务、阻塞项、近期决策 │
│ → 每次会话启动时加载 │
│ │
│ 🟢 每日日志 (memory/YYYY-MM-DD.md) │
│ raw追加式日志 │
│ → 启动时加载今天+昨天的日志 │
│ │
│ 🔵 MEMORY.md (≤10KB!) │
│ curated持久知识 — 事实、决策 │
│ → 仅主会话使用;通过search+get访问 │
│ │
│ ⚪ 领域文件 (memory/domains/*.md) │
│ 特定主题的详细信息 │
│ → 通过memory_search按需获取 │
│ │
│ 📦 归档 (memory/archive/) │
│ 超过14天的每日日志 │
│ → 极少需要 │
│ │
└─────────────────────────────────────────────────────┘
1. 启动序列 — 每次会话开始时执行
顺序很重要。智能体自动执行此流程。
- 1. state/HOLD.md — 什么被阻塞?(不要做这些!)
- state/ACTIVE.md — 有当前任务吗?
- state/DECISIONS.md — 近期决策(最近48小时)
- memory/YYYY-MM-DD.md — 今天+昨天的日志
- MEMORY.md — 长期知识(仅主会话!)
启动后状态行:
🏰 启动:ACTIVE=[任务|无] | HOLD=[n] | DECISIONS=[n 最近48h]
哪些不是启动步骤
- - 领域文件 → 仅通过 memory_search 按需获取
- 归档 → 极少使用,仅在明确请求时
- 其他智能体的记忆 → 绝不
2. 多莉防失忆模式 — 当任务到达时
当用户给你一个任务时:
- 1. 立即 将用户的 原话 写入 → state/ACTIVE.md
- 然后解释其含义
- 然后执行工作
- 完成 → 标记完成,从ACTIVE中移除
原因: 转述会导致偏差。原话能在上下文刷新后保留意图。
state/ACTIVE.md 格式
markdown
当前任务
用户说: [原话引用]
解释: [你认为的含义]
状态:
更新于: YYYY-MM-DD HH:MM
state/HOLD.md 格式
markdown
[YYYY-MM-DD HH:MM | 会话] 项目 — 阻塞原因
⚠️ 每个智能体在启动时检查此文件 — 第一步! 如果某事项在HOLD中,不要继续。
state/DECISIONS.md 格式
markdown
[YYYY-MM-DD HH:MM | 会话] 决策描述 — 背景/理由
48小时后归档 → 每日日志或MEMORY.md(如果持久)
3. 压缩感知保存 — 在丢失前写入
会话内存在压缩时丢失。在发生之前保存。
何时立即写入文件(不要等待压缩):
| 事件 | 写入位置 |
|---|
| 做出决策 | state/DECISIONS.md + 每日日志 |
| 发现用户偏好 |
MEMORY.md(如果持久)或每日日志 |
| 收到任务 | state/ACTIVE.md(多莉防失忆) |
| 某事项被阻塞 | state/HOLD.md |
| 发生错误 + 学到教训 | 每日日志 + 如果可泛化则写入 MEMORY.md |
| 发现重要事实 | 每日日志,如果持久则提升 |
| 工作完成 | 更新 state/ACTIVE.md + 日志条目 |
规则:如果重要 → 立即写入文件。不要推迟到会话结束。
4. 记忆评分 — 什么进入MEMORY.md?
并非每条记忆都值得在MEMORY.md中占有一席之地。从4个维度评分(0–3):
| 维度 | 0 | 1 | 2 | 3 |
|---|
| 持久性 | 明天就失效 | 有效数周 | 有效数月 | 有效数年+ |
| 复用性 |
一次性 | 偶尔 | 频繁 | 每次会话 |
|
影响度 | 微不足道 | 知道也好 | 改变输出 | 改变决策 |
|
独特性 | 显而易见 | 略有帮助 | 难以重新推导 | 没有它不可能 |
进入MEMORY.md的条件:
- - 总分 ≥ 8,或
- 任一维度 = 3 且总分 ≥ 6
其他所有内容 → 每日日志(如果主题合适,再从那里进入领域文件)
5. 领域组织 — 保持MEMORY.md ≤10KB
MEMORY.md 必须保持 ≤10KB。如果增长过大:
拆分为领域文件
memory/
├── domains/
│ ├── projects.md — 活跃项目详情
│ ├── infrastructure.md — 服务器、部署、网络
│ ├── people.md — 联系人、偏好
│ ├── skills-tools.md — 工具、技能、配置
│ └── lessons.md — 经验教训、反模式
├── archive/
│ └── YYYY-MM/ — 超过14天的每日日志
└── YYYY-MM-DD.md — 每日日志
规则:
- - MEMORY.md = 摘要、引用、最重要的事实
- domains/*.md = 详情,通过 memory_search 搜索
- archive/ = 旧日志,极少需要
MEMORY.md 章节格式(带领域引用):
markdown
项目
→ 详情:memory/domains/projects.md
- - 项目Alpha — ESP32固件,活跃开发中
- ERP部署 — ✅ 已部署,12个容器
- Pipeline v2 — 规划进行中
维护(每周或在心跳期间):
- 1. 检查MEMORY.md大小 — 如果 >10KB,拆分
- 移除或归档过时条目
- 查找重复项(每日日志 vs MEMORY.md)
- 从每日日志更新领域文件
6. 记忆类型和优先级
每条保存的记忆都有类型和优先级。
类型(ID前缀)
| 前缀 | 类型 | 示例 |
|---|
| DEC | 决策 | 我们在生产环境使用PostgreSQL |
| PREF |
偏好 | 英语,直接语气 |
| FACT | 持久事实 | 语音PE = ESP32-S3,16MB闪存 |
| POLICY | 规则/不变式 | MEMORY.md 永远不超过10KB |
| LESSON | 经验教训 | VLA在4KB栈上不安全 |
| ERR | 已知错误 | OIDC尾部斜杠不兼容 |
优先级(1–10)
中,有用 | 项目状态 |
| 7–8 | 高,重要 | 架构决策 |
| 9–10 | 关键,永不忘记 | 安全规则、用户偏好 |
7. 提升/降级 — 记忆生命周期
提升(强化)
记忆在以下情况被提升:
- - 再次变得相关(在搜索中被找到和使用)
- 用户引用它
- 它支撑了一个决策
提升操作: 提高优先级,从领域文件 → MEMORY.md摘要
降级(衰减)
记忆在以下情况减弱: