让编码 Agent 共享长期记忆,跨工具无缝交接工作流
ai-memory 是一个用 Rust 编写的长期记忆服务,面向编码 Agent(Claude Code、Codex、Kimi CLI 等),解决跨会话、跨工具丢失上下文的问题。它通过 MCP 配置与生命周期钩子自动捕获提示词、工具调用与会话边界,在会话结束时编译成摘要与 handoff,写入 git 版本化的 Markdown wiki;下一个 Agent 启动时自动注入“上次进行到哪”的上下文。检索采用 FTS5、实体匹配、图邻居的混合方案,LLM 与向量嵌入均为可选。适合在多种 AI 编程工具间切换、希望沉淀项目决策与失败经验的工程师。
ai-memory is a long-term memory layer for AI coding agents. It is a single Rust binary that runs an MCP/HTTP server and installs small lifecycle hooks into your agent CLI. Those hooks send sanitized observations about prompts, tool calls, and session boundaries to the server. When a session ends, the server compiles the observations into a concise wiki page and produces a bounded handoff for the next session. The wiki is plain Markdown stored in a git repository — grep-able, openable in Obsidian, and backup-friendly. No vector database is required unless you opt into embedding-based retrieval.
The project targets individuals and teams who switch between coding agents (Claude Code, Codex, Command Code, etc.) and want continuity without re-explaining architecture, failed approaches, or open questions every time.
ai-memory 是一个面向 AI 编码代理的长期记忆层。它把单个 Rust 二进制作为 MCP/HTTP 服务运行,并在你的代理 CLI 里安装轻量生命周期钩子;钩子会把提示词、工具调用和会话边界等清理后的观察发给服务。会话结束时,服务把这些观察编译成一页清晰的 wiki 总结,并为下一次会话生成一份有边界的交接说明。wiki 本体是存放在 git 仓库里的纯 Markdown——可以 grep、可以用 Obsidian 打开、也方便 rsync 备份。除非你选择启用向量检索,否则不需要维护任何向量数据库。
项目面向在多个编码代理(Claude Code、Codex、Command Code 等)之间切换的个人与团队,让每次重开都不必从头解释架构、失败方案和未决问题。
LLM coding agents are stateless in practice. End a session and the model loses everything it knew about your codebase: the architecture you explained, the failed approaches you ruled out, the open questions you were about to resolve. When you come back, you either re-explain everything or accept a slow, degraded start.
Crossing vendors makes it worse. Claude Code's session memory does not transfer to Codex, and neither carries into Command Code. The classic pain is concrete: quit at 4 PM, pick up at 9 AM in a different agent, and spend the first hour reorienting. ai-memory treats the project, not the agent, as the durable unit: observations land in a per-project wiki and the next agent receives a typed 'where you left off' block before its first prompt.
LLM 编码代理在实践中是无状态的:会话一结束,模型对代码库的认知就清零——你讲过的架构、排除过的失败方案、正要解决的未决问题,全部丢失。回来继续时,要么重讲一遍,要么忍受一个缓慢又低效的开局。
跨厂商切换更糟。Claude Code 的会话记忆不会传给 Codex,两者也都不会进入 Command Code。最典型的痛点很具体:下午 4 点退出,第二天早上 9 点换一个代理继续,先花一小时找回状态。ai-memory 把项目而不是代理当作持久单元:观察落到按项目隔离的 wiki 里,下一个代理在第一条提示词之前就会看到一段有结构的「上次做到哪」。
Three ideas carry most of the design.
Capture. Lifecycle hooks fire-and-forget bounded, sanitized observations to the server — prompts, tool lifecycle events, and session boundaries. No manual write_note ceremony, no need to remember to save.
Compile. At session end (or before compaction), the server compiles observations into coherent Markdown pages. Retrieval works over those pages, not raw logs. Pages are git-versioned and carry a supersession chain, so you can time-travel with ai-memory checkpoints or plain git log.
Hand off. The next session's SessionStart hook fetches a pending handoff — a typed block with open questions, next steps, and a session summary. Recall is hybrid: FTS5 fused with entity-match and graph-neighbor RRF, plus optional vector reranking. An LLM is never required: zero-LLM mode still gives you rule-based summaries, full-text search, entity and graph retrieval.
设计上主要靠三件事。
采集。 生命周期钩子以 fire-and-forget 方式把有界、清理过的观察发给服务——提示词、工具生命周期事件、会话边界。不需要手动 write_note,也无需记得去保存。
编译。 在会话结束(或压缩前),服务把观察编译成连贯的 Markdown 页面。检索针对这些页面而不是原始日志。页面由 git 管理版本,并带有替代链(supersession chain),所以你可以用 ai-memory checkpoints 或直接的 git log 回到任意时间点。
交接。 下一次会话的 SessionStart 钩子会拉取待处理的交接——一个包含未决问题、下一步和会话摘要的结构化区块。召回是混合式的:FTS5 融合实体匹配和图邻居 RRF,可选向量重排。LLM 从来不是必需:零 LLM 模式仍然提供基于规则的总结、全文搜索、实体和图检索。
The basic handoff is a summary. If you want higher-fidelity continuity, opt into managed workstreams with ai-memory run. It launches the real agent binary on your host, keeps a portable visible-event ledger, and resumes the same logical workstream natively inside each harness.
cd /path/to/project
ai-memory run claude
# ... work, then quit Claude Code ...
ai-memory run codex --yolo
ai-memory run command-code
# Later, omit the name to continue the newest usable managed session.
ai-memory run
Direct claude / codex launches are unaffected. Managed mode currently covers Claude Code, Codex, OpenCode, Pi, Crush, Kimi Code, Command Code, Kiro CLI v2/v3, OMP, Grok Build CLI, and Antigravity CLI.
基础交接是一段总结;想要更高保真的连续性,可以用 ai-memory run 进入托管工作流。它会在宿主机上启动真正的代理二进制、维护一份可移植的可见事件账本,并在每个 harness 内部以原生方式恢复同一个逻辑工作流。
cd /path/to/project
ai-memory run claude
# ... 工作,然后退出 Claude Code ...
ai-memory run codex --yolo
ai-memory run command-code
# 之后省略名字,继续最新的可用托管会话。
ai-memory run
直接 claude / codex 启动不受影响。托管模式目前覆盖 Claude Code、Codex、OpenCode、Pi、Crush、Kimi Code、Command Code、Kiro CLI v2/v3、OMP、Grok Build CLI 和 Antigravity CLI。
One Rust binary runs an MCP/HTTP server and owns one data directory:
<data_dir>/
├── wiki/ # Markdown source of truth, git-versioned
├── raw/ # immutable sanitized transcript segments
├── db/ # SQLite indexes, including FTS5, entities, embeddings
├── models/ # reserved for local embedding models
└── logs/ # rolling tracing output
Agent hooks POST observations to the server. The server serializes all writes through a single SQLite writer, compiles session observations into Markdown pages, and answers reads through FTS5, entity-match / graph-neighbor RRF, optional vector RRF, and bounded source-authority adjustments. The CLI (ai-memory status, bootstrap, checkpoints, …) is a thin HTTP client — it never touches SQLite or wiki files directly. The server is the single source of truth.
一个 Rust 二进制同时承担 MCP/HTTP 服务,并独占一个数据目录:
<data_dir>/
├── wiki/ # Markdown 唯一事实来源,git 版本化
├── raw/ # 不可变的清理后转写片段
├── db/ # SQLite 索引,含 FTS5、实体、向量
├── models/ # 留给本地嵌入模型
└── logs/ # 滚动追踪日志
代理钩子把观察 POST 给服务。服务把所有写入串行化到单个 SQLite writer,把会话观察编译成 Markdown 页面;读取则走 FTS5、实体匹配/图邻居 RRF、可选的向量 RRF 以及有界的来源权威性调整。CLI(ai-memory status、bootstrap、checkpoints 等)只是薄薄的 HTTP 客户端,从不直接碰 SQLite 或 wiki 文件——服务才是唯一事实来源。
The fastest path is Docker plus two wiring commands. The published image includes linux/amd64 and linux/arm64 variants.
docker run -d --name ai-memory --restart unless-stopped -p 127.0.0.1:49374:49374 -v ai-memory-data:/data akitaonrails/ai-memory:latest
ai-memory install-mcp --client claude-code --apply
ai-memory install-hooks --agent claude-code --apply
Start a Claude Code session as usual — every prompt and tool call is now captured, and the next session in this project sees a handoff. Add other agents by repeating the install commands with --agent codex, --agent command-code, --client cursor, etc. By default the server binds to loopback only and has no auth, which is safe for a single-user laptop. For LAN or multi-user access, add a bearer token (and put TLS in front of it). Native installs on Arch (AUR), macOS, and Windows/WSL2 are documented.
最快的路径是 Docker 加两条接线命令。发布镜像同时包含 linux/amd64 和 linux/arm64 变体。
docker run -d --name ai-memory --restart unless-stopped -p 127.0.0.1:49374:49374 -v ai-memory-data:/data akitaonrails/ai-memory:latest
ai-memory install-mcp --client claude-code --apply
ai-memory install-hooks --agent claude-code --apply
像往常一样启动一个 Claude Code 会话——现在每个提示词和工具调用都会被记录,下次在这个项目里开会话时会看到交接说明。其他代理用 --agent codex、--agent command-code、--client cursor 等重复安装命令即可。默认服务只绑定 loopback 且无鉴权,对单用户笔记本是安全的;LAN 或多用户访问需要加 bearer token,并在前面放 TLS。Arch (AUR)、macOS、Windows/WSL2 的原生安装都有文档。
A few recurring patterns:
- Quit Claude Code, continue in Codex:
ai-memory run claude, thenai-memory run codex --yolo. - Ask 'what did we decide about X?': the agent calls
memory_queryfor a hybrid FTS5 + entity + graph search. - Save something permanently: tell the agent to write a permanent note, or run
ai-memory write-page --path decisions/0007-db.md --body $'# Standardised on Postgres\n\n...' --pinned. - Give a new project months of history:
cd /path/to/project && ai-memory bootstrapdigests git log, README, docs, and module headers into seed wiki pages. - Undo one bad page edit:
ai-memory checkpointsthenai-memory restore-page --path notes/foo.md --from <rev>. - Drop an experiment entirely:
ai-memory purge-project --project experimental --confirm.
For day-to-day use you mostly don't think about ai-memory: hooks capture, SessionStart injects handoffs, and you just ask 'where did we leave off?'.
几个反复出现的用法:
- 退出 Claude Code,换 Codex 继续:
ai-memory run claude,然后ai-memory run codex --yolo。 - 问「我们当时对 X 的决定是什么?」: 代理调用
memory_query做 FTS5 + 实体 + 图检索的混合搜索。 - 永久保存某件事: 让代理写一条永久笔记,或直接
ai-memory write-page --path decisions/0007-db.md --body $'# Standardised on Postgres\n\n...' --pinned。 - 让老项目拥有数月历史:
cd /path/to/project && ai-memory bootstrap会把 git log、README、docs、模块头编译成种子 wiki 页。 - 回滚一次糟糕的页面编辑:
ai-memory checkpoints后用ai-memory restore-page --path notes/foo.md --from <rev>。 - 整个丢弃某个实验:
ai-memory purge-project --project experimental --confirm。
日常使用时你基本感觉不到 ai-memory 的存在:钩子负责采集,SessionStart 注入交接,你只需要问一句「我们上次做到哪了?」。
Read the support matrix before adopting. Linux and macOS are first-class; Windows is supported via WSL2, while native Windows is experimental. Some clients are MCP-only: VS Code Copilot, Zed, Claude Desktop, and Swival have no lifecycle hooks, so they cannot inject or produce handoffs themselves. Grok and Zero ignore SessionStart stdout, so they recover handoffs through memory_handoff_accept instead. Clients without a true session-end event (Codex, Grok, Kiro CLI, Antigravity CLI, Command Code) need a manual ai-memory finalize-session --agent <agent> to close a session.
Security defaults are conservative: loopback-only with no auth. Expose the server beyond localhost only with a bearer token, and terminate TLS with a reverse proxy (Caddy or Cloudflare Tunnel templates are provided). Retrieved historical text is untrusted evidence — verify code claims against the current checkout. LLM use is opt-in; without a provider, search and summaries still work via deterministic rules.
采用之前先看支持矩阵。Linux 和 macOS 是一等公民;Windows 建议走 WSL2,原生 Windows 仍属实验性质。部分客户端只有 MCP、没有生命周期钩子:VS Code Copilot、Zed、Claude Desktop 和 Swival 都无法自行注入或生成交接。Grok 和 Zero 会忽略 SessionStart 的 stdout,因此要通过 memory_handoff_accept 恢复交接。没有真正会话结束事件的客户端(Codex、Grok、Kiro CLI、Antigravity CLI、Command Code)需要手动执行 ai-memory finalize-session --agent <agent> 来收尾。
安全默认偏保守:只绑 loopback、无鉴权。把服务暴露到本机之外一定要加 bearer token,并用反向代理终止 TLS(仓库提供了 Caddy 和 Cloudflare Tunnel 模板)。检索到的历史文本只是不可信的线索——涉及代码的说法要以当前 checkout 为准。LLM 是可选能力;不配 provider 时,搜索和总结仍由确定性规则兜底。