claude-code/docs/context/project-memory.mdx
2026-04-01 09:16:41 +08:00

59 lines
2.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "项目记忆"
description: "让 AI 跨对话记住你的偏好和项目上下文"
---
{/* 本章目标:解释记忆系统如何让 AI 变得'有记忆' */}
## AI 的记忆困境
大语言模型没有真正的记忆。每次新对话,它都是一张白纸。用户不得不反复解释"我的项目用 Bun 不用 Node"、"commit 消息用中文"。
## 记忆系统的解决方案
Claude Code 通过一个基于文件的持久化记忆系统来模拟"跨会话记忆"
<CardGroup cols={2}>
<Card title="用户记忆" icon="user">
关于用户的信息:角色、偏好、技术背景
</Card>
<Card title="反馈记忆" icon="message">
用户对 AI 行为的纠正和肯定
</Card>
<Card title="项目记忆" icon="folder">
项目中的非代码信息:谁负责什么、截止日期
</Card>
<Card title="参考记忆" icon="link">
外部资源的位置Issue tracker、Dashboard URL
</Card>
</CardGroup>
## 记忆的读写时机
| 时机 | 动作 |
|------|------|
| 每次对话开始 | 加载记忆索引MEMORY.md相关记忆注入 System Prompt |
| 用户纠正 AI | AI 自动判断是否值得记住,写入反馈记忆 |
| 用户说"记住这个" | 立即保存到对应类型的记忆文件 |
| 用户说"忘掉这个" | 找到并删除对应的记忆条目 |
| 记忆可能过期时 | 使用前先验证(文件还在?函数还存在?),过期则更新或删除 |
## 记忆 vs 代码注释 vs CLAUDE.md
| | 记忆 | 代码注释 | CLAUDE.md |
|---|---|---|---|
| 存储位置 | `~/.claude/` 目录 | 代码文件中 | 项目目录中 |
| 谁能看到 | 只有当前用户 | 所有开发者 | 所有使用 Claude Code 的人 |
| 适合存什么 | 个人偏好、非公开的上下文 | 代码逻辑解释 | 项目约定、开发指南 |
| 跨项目 | 是 | 否 | 否 |
## 不该存什么
记忆系统明确规定了不应存储的内容:
- 代码结构和架构(读代码就知道)
- git 历史(`git log` 就能查)
- 调试方案(修复已在代码中)
- CLAUDE.md 里已有的内容(避免重复)
- 临时性任务状态(用任务系统)