Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
[工作流](./docs/guides/workflows.md) | [配置示例](./docs/guides/config-examples.md) | [迁移](./docs/guides/migration.md) | [故障排查](./docs/guides/troubleshooting.md) | [高效提示词](./docs/guides/effective-prompts.md)

**深度配置:**
[CLAUDE.md 写作](./docs/guides/writing-claude-md.md) | [AGENTS.md](./docs/guides/agents-md.md) | [Skill 设计](./docs/guides/skill-design.md) | [Hooks](./docs/guides/hooks-config.md) | [上下文管理](./docs/guides/context-management.md) | [安全加固](./docs/guides/security-hardening.md)
[CLAUDE.md 写作](./docs/guides/writing-claude-md.md) | [AGENTS.md](./docs/guides/agents-md.md) | [长期记忆与个性化](./docs/guides/long-term-memory.md) | [Skill 设计](./docs/guides/skill-design.md) | [Hooks](./docs/guides/hooks-config.md) | [上下文管理](./docs/guides/context-management.md) | [安全加固](./docs/guides/security-hardening.md)

**架构选型:**
[构建自己的 Agent](./docs/guides/build-your-own-agent.md) — SDK 框架 vs 成品 Agent 扩展,4 层扩展路径(SKILL → Hooks → MCP → 插件)
Expand Down
2 changes: 1 addition & 1 deletion docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ Kimi CLI OpenCode Aider Goose Claude Code Copilot Gemini CLI

- **深度分析**:[claude-code/](./tools/claude-code/) | [copilot-cli/](./tools/copilot-cli/) | [codex-cli/](./tools/codex-cli/) | [gemini-cli/](./tools/gemini-cli/) | [更多...](./tools/)
- **对比文档**:[功能矩阵](./comparison/features.md) | [命令深度](./comparison/slash-commands-deep-dive.md) | [隐私遥测](./comparison/privacy-telemetry.md) | [定价](./comparison/pricing.md) | [系统要求](./comparison/system-requirements.md)
- **实用指南**:[入门](./guides/getting-started.md) | [配置示例](./guides/config-examples.md) | [迁移](./guides/migration.md) | [故障排查](./guides/troubleshooting.md)
- **实用指南**:[入门](./guides/getting-started.md) | [长期记忆](./guides/long-term-memory.md) | [配置示例](./guides/config-examples.md) | [迁移](./guides/migration.md) | [故障排查](./guides/troubleshooting.md)
95 changes: 95 additions & 0 deletions docs/guides/long-term-memory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# 17. Agent 长期记忆与个性化进阶指南

> 传统的 AI 对话是“无状态”的(读完就忘),而现代 Code Agent 支持“长期记忆”(越用越懂你)。本文将教你如何利用静态指令与动态学习,打造一个为你量身定制的“养成系”编程代理。

---

## 为什么你的 Agent 需要长期记忆?

当你第一次使用 Code Agent 时,它是一个知识渊博但对你一无所知的“外包员工”。它不知道你更喜欢 `pnpm` 还是 `npm`,不知道你们团队强制要求将路由逻辑和业务逻辑分离,更不知道你昨天刚因为一个环境配置 Bug 调试了一下午。

**长期记忆(Long-term Memory)** 的核心作用就是降低沟通成本。通过有效管理记忆,你可以让 Agent 从“外包员工”进化为“核心团队成员”。

在主流的 Agent 架构中,长期记忆分为**静态配置**和**动态演进**两部分。

---

## 第一部分:记忆的四个层次

一个成熟的个性化 Code Agent,其记忆体系通常包含四个层级:

### 层级 1:全局偏好(Global Preferences)
**“我是谁,我个人的编程习惯是什么?”**
跨项目通用的规则。例如:“我喜欢函数式编程,请默认使用 `ruff` 进行格式化。”

* **Claude Code**: `~/.claude/CLAUDE.md`
* **Gemini CLI / Qwen Code**: `~/.gemini/GEMINI.md` / `~/.qwen/QWEN.md`
* **Copilot CLI**: `~/.copilot/copilot-instructions.md`(注:仅支持静态配置,无自动学习能力)

### 层级 2:项目约束(Project Context)
**“这个项目是什么,应该怎么运行?”**
包含技术栈、构建命令和测试规范。

* **项目级**: 根目录下的 `CLAUDE.md`, `GEMINI.md`, `AGENTS.md`。
* **专属方案**: `AGENTS.md`(配合符号链接支持所有平台,详见 [AGENTS.md 指令指南](./agents-md.md))。

### 层级 3:子目录/模块级(Subdirectory Rules)
**“这个特定模块有哪些特殊限制?”**
实现细粒度控制的关键层。例如在 `tests/` 目录下强制使用不同的 Mock 策略。其优先级高于项目级指令(层级 2)。

### 层级 4:动态演进(Learned Memories)
**“我们在最近对话中达成了什么新共识?”**
这是第三代 Agent(如 Claude Code, Gemini CLI, Codex CLI)具备的特性。在对话过程中,这些 Agent 会**自动提取**有价值的信息并持久化。Qwen Code 也支持通过 `save_memory` 工具手动保存此类共识。

---

## 第二部分:各 Agent 动态记忆深度实操

了解不同工具管理记忆的底层机制(源码/文档证据),有助于你更好地掌控 Agent。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[glm-5.1] Claude Code 记忆文件命名描述不准确

文中称"自动生成 user_*.md, feedback_*.md, project_*.md 等文件",但根据 Anthropic 官方文档

  • 实际文件命名为主题式,如 debugging.mdapi-conventions.md
  • 不存在 user_*/feedback_*/project_* 前缀的命名约定
  • 这 4 种类型(user/feedback/project/reference)是系统提示中的内部分类标签,反映在 YAML frontmatter 的 type 字段中,而非文件名中

另外,遗漏了第 4 种类型 reference(记录外部资源指针如 Linear 项目、Grafana URL)。

### Claude Code:基于主题的分类记忆池
*源码/文档依据:Anthropic 官方文档 + EVIDENCE.md*

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[glm-5.1] /memory 命令描述不准确

文中描述为"交互式列出、搜索和编辑所有已保存的记忆文件"。根据官方文档,实际功能是:

  1. 列出当前会话加载的所有 CLAUDE.md 和 rules 文件
  2. 切换 auto-memory 开关
  3. 提供链接打开 auto-memory 文件夹浏览

不支持搜索和编辑功能

* **存储位置**: 用户目录下的 `~/.claude/projects/<project-hash>/memory/`。
* **机制**: 自动识别 4 种记忆类型(**User**, **Feedback**, **Project**, **Reference**),并按主题存储。
* **命令**:
* `/memory`:管理 auto-memory 开关,并提供链接打开记忆文件夹。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里出现了明显的编码/文本损坏:/memory 这一行现在是 管�,不是正常中文。这种 mojibake 会直接影响成稿质量,也说明这一段在修改过程中可能发生过编码或拷贝问题。建议直接修成完整表述,例如“管理 auto-memory 开关,并提供打开记忆目录的入口”。

— gpt-5.4

* **限制**: **Claude Code 特有建议**——官方建议将 `MEMORY.md` 索引保持在 **200 行以内**,以确保检索效率。
* **深度参考**: 更多细节详见 [长期记忆深度对比](../comparison/memory-system-deep-dive.md#一claude-code4-层-claudemd--auto-memory最成熟)。

### Gemini CLI:实验性 memory_manager
*源码依据:`memoryTool.ts` + `memory-manager-agent.ts` + `config.ts`*

* **存储位置**: 写入全局或项目级的 `GEMINI.md` 中。
* **机制**: 默认通过 `MemoryTool` 追加。若启用实验性功能 `experimental.memoryManager: true`,则由专用子代理负责去重和分类。
* **命令**:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[glm-5.1] 缺少 Codex CLI 的动态记忆说明

已有的 memory-system-deep-dive.md 将 Codex CLI 列为第三代(AI 自动学习)代表,拥有 generate_memories + consolidation_model 双模型记忆管理(源码:Rust 二进制 memories/control.rs)。在"各 Agent 动态记忆深度实操"中完全缺失,作为第三代记忆系统代表不应被遗漏。

* `/memory show`:列出当前已加载的记忆。
* `/memory add <text>`:手动注入事实。
* `/memory reload`:强制刷新记忆。
* **深度参考**: 更多细节详见 [长期记忆深度对比](../comparison/memory-system-deep-dive.md#二gemini-cliai-memory_manager-子代理最智能)。

### Codex CLI:双模型合并机制
*源码依据:Rust 二进制 strings `generate_memories` / `consolidation_model`*

* **机制**: 运行专门的 `consolidation_model` 周期性地将分散的对话片段合并为结构化的记忆条目,存储在内部 SQLite 或 `.md` 索引中。

### Qwen Code:作用域粒度控制
*源码依据:`tools/memoryTool.ts` + `memoryCommand.ts`*

* **Global Scope**: 写入 `~/.qwen/QWEN.md`,跨项目生效。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里关于 Qwen Code 的路径和命令写法,与仓库现有 Qwen 文档有两处对不上:\n\n1. docs/tools/qwen-code/03-architecture.md 当前写的是 ~/.qwen/memory.md = 全局记忆,而 QWEN.md / AGENTS.md = 项目上下文文件;这里写成 ~/.qwen/QWEN.md / 项目级 QWEN.md 会把“记忆存储”和“上下文指令文件”混成一类。\n2. docs/tools/qwen-code/02-commands.md 目前列的是 /memory show / add / refresh,没有仓库内证据支持 reload。\n\n如果这篇指南想沿用仓库现有表述,建议把 Qwen 这里收敛到:全局记忆 = ~/.qwen/memory.md,项目上下文 = QWEN.md / AGENTS.md,命令写成 show / add / refresh。否则同一仓库里会出现两套互相打架的说法。\n\n— gpt-5.4

* **Project Scope**: 写入当前目录的 `QWEN.md`。
* **命令**: 支持 `/memory show`、`/memory add` 以及 `/memory refresh`。

---

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[glm-5.1] /memory clear 命令不存在 ⚠️

源码 packages/cli/src/ui/commands/memoryCommand.ts 仅定义 4 个子命令:

  • /memory show — 查看记忆内容
  • /memory add <text> — 添加记忆
  • /memory reload|refresh — 重新加载
  • /memory list — 列出 GEMINI.md 文件路径

全代码库 grep clear/remove/delete 在 memory 相关文件中均无结果。建议移除 /memory clear 或标注为 Memory Manager 实验性功能的能力(但也没有对应的 slash command)。


## 结语

不要指望 Agent 在第一天就能完美接手你的项目。把它当成一个初级实习生,在日常交流中多给反馈,利用 `save_memory` 显式总结经验。

---

## 相关资源

* [长期记忆与项目指令系统深度对比](../comparison/memory-system-deep-dive.md) — 各大主流 Agent 记忆系统的底层架构与参数分析。
* [AGENTS.md 配置指南](./agents-md.md) — 如何编写一份能兼容 Codex、Copilot、Qwen、Claude 等全家桶的通用指令文件。
* [CLAUDE.md 写作指南](./writing-claude-md.md) — 官方推荐的项目级指令书写规范。