Adaptive Memory
Hierarchical memory management for AI agents. Three layers — daily notes, active context, and long-term memory — with periodic distillation to keep knowledge fresh and relevant.
Problem This Solves
AI agents lose context between sessions and after context compaction. Without structured memory:
- - Decisions get re-debated
- Completed work gets redone
- Lessons learned are forgotten
- Active tasks fall through the cracks
Memory Architecture
CODEBLOCK0
Layer 1: Daily Notes (memory/YYYY-MM-DD.md)
Raw log of what happened each day. Append-only, minimal editing.
CODEBLOCK1
Rules:
- - Create
memory/ directory if it doesn't exist - One file per day, named INLINECODE2
- Append throughout the day, don't restructure
- Include: decisions, discoveries, errors, context that future-you needs
- Exclude: secrets, tokens, passwords, API keys (reference file paths instead)
Layer 2: Active Context (memory/active_context.md)
Working memory — what's in progress right now. Updated as tasks start, complete, or block.
CODEBLOCK2
Rules:
- - Keep current — stale entries erode trust
- Move completed items to "Recently Completed" (prune after a few days)
- Always check this file at session start — it's the fastest way to resume context
- Any channel, any session should be able to read this and understand what's happening
Layer 3: Long-Term Memory (MEMORY.md)
Curated knowledge distilled from daily notes. The agent's permanent memory.
CODEBLOCK3
Rules:
- - This is curated, not a dump — every entry should justify its space
- Review and update periodically (see Distillation Cycle)
- Organize by topic, not by date
- No secrets or credentials — reference file paths only (e.g., "Auth: see
~/.secrets/service.env")
Optional: Channel Context (memory/channel_context/{name}.md)
For multi-channel setups (Slack, Discord, etc.), maintain per-channel summaries so context survives compaction.
CODEBLOCK4
Rules:
- - Update at natural conversation boundaries (topic complete, day change)
- Keep concise — this is a summary, not a transcript
- One file per channel
Optional: Task Tracker (memory/pending_tasks.json)
Structured tracking for tasks that must not be forgotten.
CODEBLOCK5
Valid statuses: pending, in_progress, blocked, INLINECODE11
Session Start Routine
At the beginning of every session, load context in this order:
- 1.
memory/active_context.md — what's in progress memory/YYYY-MM-DD.md (today + yesterday) — recent eventsMEMORY.md — long-term knowledge (main/private sessions only)- Channel context (if applicable) — INLINECODE15
memory/pending_tasks.json — unfinished tasks
Do not respond to messages until context is loaded. "I don't know what you're talking about" is never acceptable when the answer is in these files.
Writing Guidelines
What to Capture
| Write it down | Skip it |
|---|
| Decisions and their reasoning | Routine operations that went smoothly |
| Errors and how they were fixed |
Intermediate debugging steps |
| Key facts about the environment | Information already in code comments |
| User preferences and patterns | Temporary values that change hourly |
| Lessons that prevent future mistakes | Obvious things any model would know |
Security Rules
- - Never write secrets (API keys, passwords, tokens) to memory files
- Reference paths instead: "Auth config:
~/.secrets/service.env" - If a credential appears in chat, acknowledge it without repeating the value
- Memory files may be shared or version-controlled — treat them as semi-public
Distillation Cycle
Periodically consolidate daily notes into long-term memory. Recommended: weekly or when daily notes accumulate (3+ unprocessed files).
Four-Phase Process
Phase 1: Orient
Read
MEMORY.md to understand current state. Note what's already captured.
Phase 2: Gather
Read recent daily notes (
memory/YYYY-MM-DD.md) that haven't been consolidated yet.
Phase 3: Consolidate
For each daily note, extract what deserves long-term storage:
- - New systems or tools built
- Lessons learned (especially from mistakes)
- Decisions with lasting impact
- Changed preferences or workflows
- Facts about the environment that won't change soon
Add these to the appropriate section in MEMORY.md.
Phase 4: Prune
Remove from
MEMORY.md:
- - Entries that are no longer relevant
- Information superseded by newer entries
- Overly detailed entries that can be summarized
Tracking Distillation
Record when distillation last ran to avoid redundant work:
In memory/heartbeat-state.json (or a similar state file):
CODEBLOCK6
Automation
Distillation can be triggered by:
- - Cron job — weekly scheduled task (recommended)
- Heartbeat — check if 48h+ since last distillation and 3+ unprocessed daily notes
- Manual — user requests "consolidate memory" or "review notes"
Integration with Session-Recall
This skill manages what gets stored. A retrieval skill like session-recall (which searches transcripts, memory files, and channel context) manages how to find it. They complement each other:
- - adaptive-memory → organizes memory into searchable layers
- session-recall → searches those layers when context is missing
Using both together provides full coverage: structured storage + intelligent retrieval.
Quick Start
- 1. Initialize the memory directory structure:
CODEBLOCK7
- 2. Add to your
AGENTS.md or session start routine:
CODEBLOCK8
- 3. Start logging to daily notes as you work
- 4. Set up weekly distillation (cron, heartbeat, or manual)
The system grows organically from here.
自适应记忆
面向AI智能体的分层记忆管理系统。包含三个层级——日常笔记、活跃上下文和长期记忆——通过定期提炼保持知识的鲜活与相关性。
解决的问题
AI智能体在会话之间以及上下文压缩后会丢失上下文。没有结构化记忆会导致:
- - 决策被反复争论
- 已完成的工作被重复执行
- 经验教训被遗忘
- 活跃任务被遗漏
记忆架构
memory/
├── YYYY-MM-DD.md # 日常笔记(原始记录,仅追加)
├── active_context.md # 工作记忆(当前任务、阻塞项)
├── channel_context/ # 按频道划分的对话摘要(可选)
│ └── {channel-name}.md
└── pending_tasks.json # 任务追踪器(结构化)
MEMORY.md # 长期记忆(精选提炼)
第一层:日常笔记(memory/YYYY-MM-DD.md)
每天发生事件的原始日志。仅追加,极少编辑。
markdown
2026-04-01
任务
- - 实现了项目X的登录流程
- 修复了cron调度器中的时区bug
决策
- - 选择SQLite而非JSON进行数据存储(大规模性能考量)
- API速率限制:100次/分钟,配合指数退避
经验
- - 库Y需要v3+版本才能支持异步
- 浏览器cookie不跨配置文件共享
阻塞项
规则:
- - 如果memory/目录不存在则创建
- 每天一个文件,命名为YYYY-MM-DD.md
- 全天持续追加,不重构内容
- 包含:决策、发现、错误、未来自己需要的上下文
- 排除:密钥、令牌、密码、API密钥(改用文件路径引用)
第二层:活跃上下文(memory/active_context.md)
工作记忆——当前正在进行的任务。随任务开始、完成或阻塞而更新。
markdown
活跃上下文
进行中
- 下一步:令牌刷新逻辑
阻塞/等待中
- - 服务Z的API密钥:2026-03-30已申请,等待审批
最近完成
- - 时区修复:已部署,cron任务现在正确触发(2026-04-01)
规则:
- - 保持最新——过时条目会削弱信任度
- 将已完成项移至最近完成(几天后清理)
- 每次会话开始时务必检查此文件——这是恢复上下文最快的方式
- 任何频道、任何会话都应能读取此文件并了解当前进展
第三层:长期记忆(MEMORY.md)
从日常笔记中提炼的精选知识。智能体的永久记忆。
markdown
长期记忆
已构建的系统
- - 数据管道:基于SQLite,每天凌晨6点运行,存储在project.db中
- 监控系统:三级告警体系(信息→警告→严重)
经验教训
- 1. 超过100条记录时,SQLite优于JSON
- 始终为HTTP请求设置显式超时
- 浏览器自动化:抓取前检查虚拟滚动
关键决策
- - 选择框架A而非B(原因:更好的异步支持,MIT许可证)
- API集成使用webhook推送,而非轮询
规则:
- - 这是精选内容,不是数据转储——每条记录都应证明其存在价值
- 定期审查和更新(参见提炼周期)
- 按主题而非日期组织
- 不包含密钥或凭证——仅引用文件路径(例如:认证配置:参见~/.secrets/service.env)
可选:频道上下文(memory/channel_context/{name}.md)
针对多频道设置(Slack、Discord等),维护每个频道的摘要,使上下文在压缩后仍能保留。
markdown
频道名称
当前话题
近期决策
- - 批准了新的CI流水线配置(2026-04-01)
未解决事项
- - 端点/api/users出现性能回归——正在调查中
规则:
- - 在自然的对话边界处更新(话题完成、日期变更)
- 保持简洁——这是摘要,不是转录文本
- 每个频道一个文件
可选:任务追踪器(memory/pending_tasks.json)
用于追踪不可遗忘任务的结构化记录。
json
{
lastUpdated: 2026-04-01T10:00:00Z,
tasks: [
{
id: 唯一标识,
title: 简短描述,
status: in_progress,
priority: high,
createdAt: 2026-04-01T09:00:00Z,
note: 额外上下文
}
]
}
有效状态:pending(待处理)、in_progress(进行中)、blocked(阻塞)、done(已完成)
会话启动流程
每次会话开始时,按以下顺序加载上下文:
- 1. memory/activecontext.md——当前进行中的任务
- memory/YYYY-MM-DD.md(今天+昨天)——近期事件
- MEMORY.md——长期知识(仅限主/私有会话)
- 频道上下文(如适用)——memory/channelcontext/{name}.md
- memory/pending_tasks.json——未完成任务
在上下文加载完成前不要回复消息。当答案就在这些文件中时,我不知道你在说什么是绝对不能接受的。
编写指南
应记录的内容
| 需要记录 | 跳过不记 |
|---|
| 决策及其理由 | 顺利进行的常规操作 |
| 错误及其修复方法 |
中间调试步骤 |
| 环境的关键事实 | 代码注释中已有的信息 |
| 用户偏好和模式 | 每小时变化的临时值 |
| 能防止未来错误的经验 | 任何模型都知道的显而易见的事情 |
安全规则
- - 切勿将密钥(API密钥、密码、令牌)写入记忆文件
- 改用路径引用:认证配置:~/.secrets/service.env
- 如果聊天中出现凭证信息,确认其存在但不重复具体值
- 记忆文件可能被共享或进行版本控制——将其视为半公开内容
提炼周期
定期将日常笔记整合到长期记忆中。建议:每周一次,或当日常笔记积累较多时(3个以上未处理文件)。
四阶段流程
第一阶段:定位
阅读MEMORY.md了解当前状态。注意哪些内容已被记录。
第二阶段:收集
阅读尚未整合的近期日常笔记(memory/YYYY-MM-DD.md)。
第三阶段:整合
针对每条日常笔记,提取值得长期存储的内容:
- - 新构建的系统或工具
- 经验教训(尤其是从错误中获得的)
- 具有持久影响的决策
- 变更的偏好或工作流程
- 短期内不会改变的环境事实
将这些内容添加到MEMORY.md的相应部分。
第四阶段:精简
从MEMORY.md中移除:
- - 不再相关的条目
- 已被新条目取代的信息
- 可以概括的过于详细的条目
追踪提炼进度
记录上次提炼的时间,避免重复工作:
在memory/heartbeat-state.json(或类似的状态文件)中:
json
{
lastConsolidatedAt: 2026-04-01T10:00:00Z
}
自动化
提炼可通过以下方式触发:
- - 定时任务——每周计划任务(推荐)
- 心跳检测——检查是否距离上次提炼超过48小时且有3个以上未处理的日常笔记
- 手动触发——用户请求整合记忆或审查笔记
与会话回忆的集成
本技能管理存储什么内容。而像session-recall这样的检索技能(搜索转录文本、记忆文件和频道上下文)则管理如何找到内容。两者相辅相成:
- - adaptive-memory → 将记忆组织成可搜索的层级
- session-recall → 在上下文缺失时搜索这些层级
同时使用两者可提供全面覆盖:结构化存储+智能检索。
快速入门
- 1. 初始化记忆目录结构:
bash
# 使用捆绑脚本(推荐)
./scripts/init_memory.sh
# 或手动创建
mkdir -p memory/channel_context
touch memory/active_context.md
echo {lastUpdated:,tasks:[]} > memory/pending_tasks.json
- 2. 添加到你的AGENTS.md或会话启动流程:
在回复前,请阅读:
1. memory/active_context.md
2. memory/YYYY-MM-DD.md(今天+昨天)
3.