Claude 记忆 skill:保存决策及其原因,决策前先回顾

Dmitriy Solodukha,Caprock 开发者 · 发布于

测试于 (Claude Code 和 Codex)

可用于任何智能体的 Claude 记忆 skill:每条记录保存一项决策及其日期和原因,存入仓库,再次决策前先检索。

memory skill 是一个 Claude 记忆 skill,在 Claude Code、Codex、OpenCode 和 Cursor 中工作方式相同:它将决策、不易察觉的发现或偏好连同理由保存为一条带日期的记录,存入仓库中的 docs/memory/ 或 MEMORY.md,并在做出你可能已经做过的决定前搜索这些记录。记忆是对过去的描述,因此它在遵循每条记忆之前,都会先对照代码核实。

安装 memory skill

每个智能体只需一条命令。skill 只有一个 SKILL.md;安装时不会运行任何内容。

将全部九个 skill 作为插件安装。在 Claude Code 中输入以下内容:

也可以只安装这个 skill,在终端中进入项目文件夹后运行:

添加 -g 即可为所有项目安装。

使用 memory skill 的提示词

你的措辞与 skill 描述匹配时,智能体会选择它;直接指定 skill 名称更有把握。点击提示词即可复制。

Claude 记忆 skill 如何保存和回顾记录

  • 每条只记一个事实:标题包含绝对日期,然后写 Decision、Finding 或 Preference、原因(以及谁说的)、适用范围、取代了什么。旧记录标记为已被取代,绝不删除。
  • 位置:沿用仓库已有的位置(docs/memory/、MEMORY.md、docs/adr/);如果没有,就用 docs/memory/YYYY-MM-DD-short-title.md,每条记录一个文件。每个会话都必须遵守的规则,还要在 CLAUDE.md 或 AGENTS.md 中写一行。
  • 先回忆,再决策:搜索(有 Caprock 时用 current_decisions 和 search_memories;没有时搜索记忆文件和 git log --grep),完整阅读匹配记录,对照代码核实,然后遵循它、标出矛盾,或在你改变主意时用新记录取代它。

记忆文件是仓库的一部分:纳入提交、接受审查,所有能读取仓库的人都能看到。

我们运行了什么,智能体做了什么

每个 skill 都于 2026年11月29日在专门创建的临时仓库中运行,并作为项目 skill 安装。这是一个小仓库,其中的 store/db.py 使用 sqlite3。

Claude Code (claude -p --model sonnet)

Remember this decision: the store stays on SQLite, not Postgres, because it's a single-user local app with no server. Save it so the next session knows.

调用了 memory,按 skill 的格式创建了 docs/memory/2026-11-29-sqlite-store.md,对照 store/db.py 检查,并说明 Caprock 的 2nd Brain 通过 MCP 只能读取,以及如何将文件夹导入其中。它还写入了 Claude Code 自身的自动记忆,位置在仓库之外。

Codex (0.163.0,codex exec)

The same prompt, plus: "what did we already decide about the database?"

读取 skill,调用 Caprock 的 current_decisions 和 search_memories,搜索文件,找到条目,与代码核对,没有重复写入。

已测试的智能体

  • Claude Code:测试于 2026年11月29日,使用 claude -p --model sonnet,skill 作为项目 skill 安装在 .claude/skills/ 中。
  • Codex:于 2026年11月29日使用 Codex 0.163.0(codex exec)测试,skill 位于 .agents/skills/。
  • OpenCode 和 Cursor:未测试。此 skill 是以开放的 Agent Skills 格式编写的纯文本指令,但尚未在这两者上实际运行验证。

限制

  • 它无法保存到 Caprock 的 2nd Brain:Caprock 的 MCP 工具是只读的。你导入其中的文件,只有在你启用相关功能后才会提供给智能体。
  • 记忆文件夹只有在智能体查看时才会读取;每个会话都要遵循的规则应放在 CLAUDE.md 或 AGENTS.md 中。
  • 在公开仓库中,记录也是公开的;skill 会省略私密细节,并说明这一点。

使用和不使用 Caprock 的情况

连接 Caprock MCP 服务器后,skill 通过 current_decisions、search_memories、get_memory 和 next_session_handoff 回忆信息,这些工具读取 Caprock 的 2nd Brain 中的决策和发现。无论是否连接,它都会保存到文件。Caprock 是一款免费开源应用,记录你电脑上的每个编程智能体会话。

获取 Caprock →

完整的 SKILL.md

这是 dspv/skills 中修订版本 dfce799 的文件原样。手动安装时,将它保存在项目中:Claude Code 使用 .claude/skills/memory/SKILL.md,Codex 使用 .agents/skills/memory/SKILL.md。

skills/memory/SKILL.md
---
name: memory
description: >-
  Keep decisions and findings across coding-agent sessions, and recall them before deciding again.
  Use when the user says "remember this", "save this decision", "note that", "what did we decide
  about X", "did we already try this", "why did we do it this way", or when a session settles a
  decision, finds a non-obvious fact or hears a preference worth keeping. A claude memory skill
  that works the same in Claude Code, Codex, OpenCode and Cursor: one dated fact per entry with
  its why, stored in a docs/memory/ folder or a MEMORY.md in the repository, and read through
  Caprock 2nd Brain's MCP tools (search_memories, get_memory, current_decisions) when present.
license: Apache-2.0
---

# Memory

Keep what a later session would otherwise have to rediscover or ask again, and look it up before
deciding anything the user may already have decided. A memory is a claim about the past: check it
against the code before relying on it.

## When to save

Save one entry when any of these happens:

- **A decision and its why.** "Use SQLite, not Postgres: one user, no server." The why is the part
  that stops a later session from reopening it.
- **A non-obvious fact.** Something that cost time to learn and is not visible in the code: a
  flaky test and its cause, an API quirk, a command that must run first, a dead end already tried.
- **A user preference.** How they want work done: "never commit to master", "short replies",
  "ask before adding a dependency".
- **The user asks**: "remember", "save", "note that", "keep in mind".

Do not save:

- What the code, `git log` or the docs already say. Point to them instead.
- Secrets, tokens, passwords, personal data of other people. Never, even when asked; say why.
- Guesses, plans that were not agreed, or the step-by-step of the current task.
- Anything that holds only for this session.

## How to write an entry

One fact per entry, so it can be found, read and replaced on its own:

```markdown
## 2026-11-29: Use SQLite for the local store

- Decision: the local store is SQLite in `data/app.db`, not Postgres.
- Why: one user per install, no server to run; the user said so on 2026-11-29.
- Scope: `store/`. Revisit if the app gets a hosted multi-user mode.
- Supersedes: none.
```

Rules for every entry:

- **Absolute date** (`2026-11-29`), never "today" or "last week".
- **Kind** in the first word of the body: Decision, Finding or Preference.
- **Why**, and who said it when it came from the user.
- **Where it applies**: files, module or command, so a reader can check it.
- **Supersedes** when it replaces an older entry. Mark the old one
  `Superseded by 2026-11-29: <title>`; do not delete it, the history is the why.
- Short: a title and two to five lines. A long story goes in the repository's docs, and the entry
  links to it.

## Where to save

Pick the first that applies, and say which one you used:

1. **The place the repository already uses.** Look for `docs/memory/`, `MEMORY.md`,
   `docs/decisions/`, `docs/adr/` or a decisions section in `CLAUDE.md` or `AGENTS.md`. Follow its
   format, even when it differs from the one above.
2. **Nothing yet**: create `docs/memory/` with one file per entry, named
   `YYYY-MM-DD-short-title.md` (for example `docs/memory/2026-11-29-sqlite-store.md`). One file per
   entry keeps merges clean and lets each entry be replaced alone.
3. **A small repository, or the user prefers one file**: a `MEMORY.md` at the root, newest entry
   first.

A rule that every session must follow (a preference like "never push to master") also gets one line
in the agent instruction file the repository uses (`CLAUDE.md`, `AGENTS.md`), because that file is
loaded into every session and a memory folder is read only when someone looks.

Memory files are part of the repository: they are committed, reviewed and visible to everyone who
can read it. Before writing to a public repository, leave out anything private and say so.

## Caprock 2nd Brain

When the Caprock MCP tools are available (a server named `caprock`), use them to recall. Caprock
(<https://caprock.dev>, free, open source, runs locally) records the agent sessions on the computer
and keeps a 2nd Brain of their decisions and findings.

Its MCP tools are read-only: an agent cannot save into 2nd Brain through them. Save to files as
above. 2nd Brain gets memories in its own ways: it builds them from finished sessions when the user
has switched that on, from lines the user drops in by hand, and from a Markdown folder imported on
its 2nd Brain screen (Import from Obsidian, which also takes a plain folder such as `docs/memory/`).
An imported folder reaches agents only after the user turns on "Let agents use these notes" for it.
Do not tell the user a file you wrote is in 2nd Brain unless one of those is true.

Tools to use, with their arguments:

- `current_decisions(project?, limit?)`: the decisions in force for a project, newest first, with
  superseded ones left out. `project` is the repository folder's name; omitted, the repository the
  server was started in.
- `search_memories(query, project?, limit?)`: ranked candidates with kind, date, project and a
  summary. Titles may be in the user's language; keywords are English. Judge by meaning; if nothing
  fits, retry with other words, fewer words or the component's name.
- `get_memory(id)`: one memory in full, with its links. A memory with `superseded_by` is no longer
  current: read the one it names.
- `next_session_handoff(repo?)`: what Caprock hands a new session in this repository.

## Recall before deciding

Before choosing a library, changing an approach, reverting something or asking the user a question
they may have answered:

1. **Search.** With Caprock: `current_decisions` for the project, then `search_memories` with the
   subject's words. Without it: search the memory files (`docs/memory/`, `MEMORY.md`, decision
   records, `CLAUDE.md`, `AGENTS.md`) for the subject's words, and `git log --grep=<word>` for the
   commit that made the change.
2. **Read the match in full** (`get_memory`, or open the file). Follow any supersedes link to the
   newest entry.
3. **Check it against the code.** Memories go stale: the file moved, the dependency was swapped,
   the flag was removed. Open the files the entry names and confirm it still holds.
4. **Act on what you found**:
   - It holds: follow it, and say "Following the decision of 2026-11-29: ...".
   - The code contradicts it: say so, follow the code for now, and ask whether to update the
     memory. When the user agrees, write a new entry that supersedes the old one.
   - The user wants to change it: do what they say, then write the new decision with
     `Supersedes:` pointing at the old one.
   - Nothing found: decide as usual. If it is worth keeping, save it.

Stop searching after two or three phrasings with no fitting result; say nothing was found rather
than stretching a weak match.

## Output

After saving, one or two lines:

```text
Saved to docs/memory/2026-11-29-sqlite-store.md: Decision, use SQLite for the local store
(why: one user, no server). Supersedes nothing.
```

After recalling, cite the source and its date, and whether it was checked:

```text
Found: Decision 2026-11-29, "Use SQLite for the local store" (docs/memory/2026-11-29-sqlite-store.md).
Checked: store/db.go still opens data/app.db. Following it.
```

关于 memory skill 的问题

什么是 Claude memory skill?
这个 skill 告诉 Claude Code 在会话之间保留什么以及如何保留:每条记录保存一项决策或发现,附上日期和原因,存放在下一个会话会查找的位置,再次决策前先检索。
这个记忆 skill 与 CLAUDE.md 有何不同?
`CLAUDE.md` 会加载到每个会话中,因此用来存放规则。memory skill 将决策和发现分条保存,需要时再搜索;只有每个会话都必须遵循的规则,才会在 `CLAUDE.md` 中加上一行。
memory skill 能用于 Codex 吗?
可以。在 2026年11月29日的测试中,Codex 0.163.0 通过它找回了已保存的决策,没有重复写入。
它会保存我粘贴的机密信息吗?
不会。访问令牌、密码和他人的个人数据绝不保存,即使你要求也不保存;skill 会说明原因。

来源

应用本身是英文界面。