Agent Memory Architecture: How We Built a 4-Layer Progressive Memory System #37
xg-gh-25
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
TL;DR: 大多数 AI Agent 是无状态的 — 每次对话从零开始。我们构建了一个 4 层渐进式记忆系统:Context Directory(全量注入)→ Progressive Index(选择性加载)→ Session Recall(FTS5 搜索历史对话)→ Knowledge RAG(混合向量+关键词检索)。结合三层治理(Principles/Rules/Gates)实现自进化。
三条核心设计哲学
为什么 Memory 是 AI Agent 最被低估的问题
大多数 AI Agent demo 看起来很惊艳 — 因为 demo 都是单轮的。
生产环境不是单轮:
没有 Memory 的 Agent = 永远的实习生。有 Memory 的 Agent = 日益成长的同事。
Agent Memory ≠ Chat History
Overall Architecture: 4-Layer Progressive Memory
架构图文字说明(点击展开)
Layer 4: Knowledge RAG Engine — Triggered on first user message, 150ms budget
Layer 3: Session Recall — FTS5 over past conversations, ~100ms
Layer 2: Progressive Memory Index — Smart selective injection
Layer 1: Context Directory — 11-file system prompt assembly
Write Path (Session Close):
Summarization → DailyActivity → Distillation (git-verified) → MEMORY.md → Embedding Sync → DDD Cultivation
为什么是 4 层而不是一个 RAG Pipeline
传统做法把所有知识扔进一个 vector store,每次 top-K retrieval。我们把确定性注入(L1)、结构化选择(L2)、和 RAG 检索(L3/L4)分离 — 每层有不同的触发条件、延迟预算和准确度保证。
E2E Case: One Prompt Traversing All 4 Layers
一个真实场景:"昨天 forecast gap 的结论是什么?哪几个 BU 风险最大?"
E2E Flow 详细说明(点击展开)
昨天 (Write Path):
今天 Layer 1 (0ms):
今天 Layer 2 (~50ms):
今天 Layer 3 (~100ms):
今天 Layer 4 (~150ms):
Agent Response:
综合 4 层信息 → "MEAGS 风险最大 (-12%),3 笔大单 slip: [A] ¥2.1M 预算冻结..."
Without memory: "我不知道你昨天做了什么,请再描述一次。"
With memory: 精准引用 + 补充细节 + actionable next step | Total overhead: < 200ms
Memory × Self-Evolution: Three-Layer Governance
Memory 不只存"知识" — 也存 Agent 的行为校正历史,并驱动自进化。
治理模型说明(点击展开)
为什么 rule 累加是死路:
三层协同:
关键 insight: Agent Memory 的深层功能不只是"记住做了什么",而是"记住哪些认知模式有 bug,并结构性修复"。每条 correction 是 OS patch,不是 data update。
Cognitive Stack: 5 Layers of Agent Intelligence
Key design: Higher layers are more abstract, longer-lived, harder to change. Lower layers are concrete, ephemeral, auto-managed. The distillation pipeline continuously promotes insights upward.
Design Philosophy: 4 Counter-Intuitive Decisions
1. Markdown + sqlite-vec > Pinecone/Weaviate/Neo4j
在 1M context 时代,核心记忆(~46K tokens)直接全量注入 system prompt — 零检索延迟。Vector DB 的 retrieval overhead 对这个规模完全没有意义。
我们用 sqlite-vec(本地)+ FTS5(本地)做 hybrid search,无网络开销。Markdown 是人类可读的,有 git history,可 diff,可 review。
2. 达尔文主义 > 百科全书
3. Memory Sovereignty > Platform Dependency
所有记忆 self-owned、self-managed。永远不使用平台记忆服务(Claude Memory、OpenAI Memory)。
4. Structured Extraction > Brute-Force Replay (Session Resume)
Claude Code 做完整 JSONL replay(到 12% 时 auto-compact)。我们从 DB 提取 5 层结构化信息注入:
哲学: Resume 的核心不是"记住做了什么",而是"记住发现了什么"。
企业场景映射
Open Discussion Topics
1. "注入 > 检索" 的边界在哪?
当前论断基于一个前提:主流模型保持 100K+ context。如果某些专业场景降级到 32K(边缘部署、成本敏感客户),策略需要动态调整。
我们的回答是动态 token budget(100K/50K/30K/minimal 四档)+ progressive index 自动激活。但更深层的问题是:什么时候 threshold 翻转? 知识量级在什么点上,全量注入的 cost 开始超过 selective retrieval?
一个 model-agnostic 的判断框架:memory tokens / context window < 5% 是舒适区,超过 10% 开始有 attention dilution risk。 我们当前 46K / 1M = 4.6%,正好在舒适区。动态 budget tier 在模型切换时自动适配 — 从 Claude Opus (1M) 降到 Sonnet (200K) 时,budget 从 100K 降到 50K,L2 progressive mode 自然激活。不需要手动调参。
预计 MEMORY.md 自然增长到 ~80K 时(~8% on 1M),L2 selective mode 会成为常态。但这还远 — 达尔文衰减机制在主动控制增速。
2. Memory Sovereignty vs Auditability
文章强调 "memory 不能 vendor-lock"。但企业合规有一个对立需求:记忆的每一次更新都需要审计日志。
我们的方案:
locked_write.py的 fcntl.flock 保证原子写入但一个更深层的观点:企业级 Agent Memory 应该 focus on domain knowledge(项目/业务领域),而不是个人记忆。 个人记忆(个人偏好、工作习惯)有 privacy concern,不适合审计。而 domain knowledge("MEAGS 的 forecast gap 是 -12%"、"这个 API 的正确调用方式")天然需要版本化、可审计、可共享。
DDD 4-doc 结构正是这个分离的实现:domain knowledge 在 Projects/ 目录(可审计、可共享),personal memory 在 MEMORY.md(agent-owned、不外泄)。
一个边界 case:domain knowledge 里可能混入 PII("forecast gap 的原因:某人的工作失误")。我们的防护不是在 schema 里加 sensitivity metadata(over-engineering),而是两道已有的门:(1) MemoryGuard secrets/PII scan 在写入时拦截,(2) Distillation 是 rule-based + git-verified 的 — human-in-the-loop 本身就是 PII filter。结论是结论,归因是八卦 — distillation 自然只保留前者。
3. 引用追踪的粒度问题
"90 天无引用即归档" — 但什么算 "引用"?
我们的实现:只有被 pipeline/决策/对话显式引用才计数。全量注入不算引用(否则所有条目等价)。这避免了虚假强化,但也意味着 L2 selective mode 的激活会自然产生更准确的引用信号 — 被选中的条目才是真正相关的。
4. Multi-Agent Memory Sharing
当前架构是单 Agent 的。10 个 Agent 同时处理相关问题时,怎么让记忆互联?
我们的立场:不共享 MEMORY.md。每个 Agent 独立记忆。协作通过:
类比:公司里的同事共享 wiki(domain knowledge),但不共享私人笔记本(personal memory)。
5. Memory Poisoning 防护细节
MemoryGuard 的 5 层扫描:
[REDACTED])每次写入 MEMORY.md / EVOLUTION.md 都经过这条 pipeline。这是写入时的第一道门。
额外防护:
validate_memory_content()专门检查 MEMORY.md 的结构完整性(section 格式、entry ID 唯一性等)。6. Cold Start 策略
新 Agent(或新 project)第一天:
实践中 cold start 不是问题 — Agent 第一天就能工作(有 SOUL + AGENT + USER),只是没有历史记忆。3-5 个 session 后 MEMORY.md 开始积累,一周后基本到达有效工作状态。关键 insight:Agent 不需要"所有知识"才能工作。它需要的是渐进式 competence 积累。
一个粗略的 maturity timeline(基于我们 2+ 个月的观察):
这个 timeline 对管理用户预期有用 — "为什么我的新 Agent 第一周不如预期" 是一个 legitimate question,答案是 competence 需要时间积累,就像人类同事的 onboarding 一样。
Related Design Documents
Related Articles
Architecture running in production for 2+ months. 32 behavioral corrections captured and structurally resolved. Memory system serving 3 concurrent chat sessions + 1 Slack channel + scheduled jobs, 24/7.
Beta Was this translation helpful? Give feedback.
All reactions