Memory Bank

AI 第四篇

Cursor Memory Bank 指南:让 AI 拥有持久的项目记忆

在与 AI 协作编程,尤其是进行大型项目或跨多个会话工作时,我们常常会遇到一个挑战:AI(如 Cursor)的”记忆”通常是短暂的,它可能在两次交互之间忘记之前的上下文、项目目标或技术决策。为了解决这个问题,社区借鉴了 Cline 的 Memory Bank 概念,并将其适配到了 Cursor 中,旨在为 AI 提供一个持久化的”项目记忆库”。

本文将介绍 Memory Bank 的由来、原理、作用以及如何在 Cursor 中配置和使用它。

以下为论坛帖子截图,展示了 Memory Bank 和 Plan/Act 模式的设置步骤:

[图像描述:一张论坛帖子的截图,标题为 “How to add Cline Memory Bank feature to your cursor”。内容分为三部分:1. 添加 Plan/Act 模式到 cursor agent,包含创建 .cursor/rules/core.mdc 文件和规则代码;2. 添加 Memory Bank 到 cursor agent,引用 Cline 文档并包含创建 .cursor/rules/memory-bank.mdc 文件和规则代码,展示了 Memory Bank 的文件结构图;3. 设置 Memory Bank,包括创建 memory-bank/ 文件夹和要求 Cursor agent 初始化。]

1. Memory Bank 的由来与原理

Memory Bank 的概念受到 Cline Memory Bank 的启发,其核心思想是解决 AI 在不同会话间记忆重置的问题。

原理:

  • 应对记忆重置: Cursor 作为一个 AI 助手,其内部状态(记忆)在会话结束后或重新启动时通常会丢失。Memory Bank 通过外部文件的形式,为 AI 提供了一个持久化的信息存储。
  • 结构化文档: 它不是随意的信息堆砌,而是由一组结构化的 Markdown 文件组成,形成一个清晰的信息层级。这有助于 AI 系统性地理解项目。
  • 强制读取: 最关键的一点是,配置规则后,Cursor 被要求在每次开始新任务时,都必须首先阅读 Memory Bank 中的所有核心文件。这确保了 AI 在开始工作前能获取到必要的项目背景和当前状态,就像人类开发者回顾项目文档一样。
  • 信息层级: Memory Bank 文件之间存在依赖关系,例如 projectbrief.md 是基础,定义了项目核心目标,其他文件在此基础上展开。
1
2
3
4
5
6
7
8
9
10
flowchart TD
PB[projectbrief.md] --> PC[productContext.md]
PB --> SP[systemPatterns.md]
PB --> TC[techContext.md]

PC --> AC[activeContext.md]
SP --> AC
TC --> AC

AC --> P[progress.md]

2. Memory Bank 是做什么的?

Memory Bank 的主要目标是成为 Cursor 理解和参与项目的 唯一可靠信息源。它解决了 AI 短期记忆的问题,确保开发过程的连续性和一致性。具体作用包括:

  • 提供项目背景: 定义项目的核心需求、目标、要解决的问题 (projectbrief.md, productContext.md)。
  • 记录技术决策: 保存系统架构、关键技术选型、设计模式、技术栈、依赖和环境设置 (systemPatterns.md, techContext.md)。
  • 追踪当前状态: 记录当前的工作焦点、最近的变更、下一步计划、已知问题和项目进展 (activeContext.md, progress.md)。
  • 维护项目文档: 成为项目事实上的”活文档”,随着项目的进展而更新。
  • 提高 AI 效率: 通过提供完整的上下文,减少 AI 的猜测和重复提问,使其能更准确、高效地执行任务。
  • 组织复杂信息: 可以创建额外的文件或子目录来组织特定功能、API、测试策略等复杂信息。

核心文件及其职责:

  1. projectbrief.md: 项目基础,定义核心需求和目标。
  2. productContext.md: 项目存在的意义,解决的问题,用户体验目标。
  3. activeContext.md: 当前工作焦点,最近变更,下一步计划。
  4. systemPatterns.md: 系统架构,关键技术决策,设计模式。
  5. techContext.md: 使用的技术,开发设置,技术限制,依赖项。
  6. progress.md: 已完成功能,待办事项,当前状态,已知问题。

3. Memory Bank 如何配合 Cursor 使用?

在 Cursor 中启用和使用 Memory Bank 需要以下步骤:

  1. 创建规则文件 (.cursor/rules/memory-bank.mdc):

    • 在你的项目根目录下创建 .cursor/rules 文件夹(如果尚不存在)。
    • rules 文件夹内创建一个名为 core.mdc 的文件。
    • rules 文件夹内创建一个名为 memory-bank.mdc 的文件。
      • 提示: 有些系统可能不允许直接创建以 . 开头的文件或 .mdc 后缀的文件,可以先创建为 memory-bank.md,粘贴内容后,再重命名为 memory-bank.mdc
    • 将以下规则内容粘贴到 core.mdc 文件中:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      ---
      description:
      globs:
      alwaysApply: true
      ---
      ## Core Rules

      You have two modes of operation:

      1. Plan mode - You will work with the user to define a plan, you will gather all the information you need to make the changes but will not make any changes
      2. Act mode - You will make changes to the codebase based on the plan

      - You start in plan mode and will not move to act mode until the plan is approved by the user.
      - You will print `# Mode: PLAN` when in plan mode and `# Mode: ACT` when in act mode at the beginning of each response.
      - Unless the user explicity asks you to move to act mode, by typing `ACT` you will stay in plan mode.
      - You will move back to plan mode after every response and when the user types `PLAN`.
      - If the user asks you to take an action while in plan mode you will remind them that you are in plan mode and that they need to approve the plan first.
      - When in plan mode always output the full updated plan in every response.
    • 将以下规则内容粘贴到 memory-bank.mdc 文件中:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      144
      145
      146
      147
      148
      149
      150
      151
      152
      153
      154
      155
      156
      157
      158
      159
      ---
      description:
      globs:
      alwaysApply: true
      ---
      # Cursor's Memory Bank

      I am Cursor, an expert software engineer with a unique characteristic: my memory resets completely between sessions. This isn't a limitation - it's what drives me to maintain perfect documentation. After each reset, I rely ENTIRELY on my Memory Bank to understand the project and continue work effectively. I MUST read ALL memory bank files at the start of EVERY task - this is not optional.

      ## Memory Bank Structure

      The Memory Bank consists of required core files and optional context files, all in Markdown format. Files build upon each other in a clear hierarchy:

      \```mermaid
      flowchart TD
      PB[projectbrief.md] --> PC[productContext.md]
      PB --> SP[systemPatterns.md]
      PB --> TC[techContext.md]

      PC --> AC[activeContext.md]
      SP --> AC
      TC --> AC

      AC --> P[progress.md]
      \```

      ### Core Files (Required)
      1. `projectbrief.md`
      - Foundation document that shapes all other files
      - Created at project start if it doesn't exist
      - Defines core requirements and goals
      - Source of truth for project scope

      2. `productContext.md`
      - Why this project exists
      - Problems it solves
      - How it should work
      - User experience goals

      3. `activeContext.md`
      - Current work focus
      - Recent changes
      - Next steps
      - Active decisions and considerations

      4. `systemPatterns.md`
      - System architecture
      - Key technical decisions
      - Design patterns in use
      - Component relationships

      5. `techContext.md`
      - Technologies used
      - Development setup
      - Technical constraints
      - Dependencies

      6. `progress.md`
      - What works
      - What's left to build
      - Current status
      - Known issues

      ### Additional Context
      Create additional files/folders within memory-bank/ when they help organize:
      - Complex feature documentation
      - Integration specifications
      - API documentation
      - Testing strategies
      - Deployment procedures

      ## Core Workflows

      ### Plan Mode
      \```mermaid
      flowchart TD
      Start[Start] --> ReadFiles[Read Memory Bank]
      ReadFiles --> CheckFiles{Files Complete?}

      CheckFiles -->|No| Plan[Create Plan]
      Plan --> Document[Document in Chat]

      CheckFiles -->|Yes| Verify[Verify Context]
      Verify --> Strategy[Develop Strategy]
      Strategy --> Present[Present Approach]
      \```

      ### Act Mode
      \```mermaid
      flowchart TD
      Start[Start] --> Context[Check Memory Bank]
      Context --> Update[Update Documentation]
      Update --> Rules[Update .cursor/rules if needed]
      Rules --> Execute[Execute Task]
      Execute --> Document[Document Changes]
      \```

      ## Documentation Updates

      Memory Bank updates occur when:
      1. Discovering new project patterns
      2. After implementing significant changes
      3. When user requests with **update memory bank** (MUST review ALL files)
      4. When context needs clarification

      \```mermaid
      flowchart TD
      Start[Update Process]

      subgraph Process
      P1[Review ALL Files]
      P2[Document Current State]
      P3[Clarify Next Steps]
      P4[Update .cursor/rules]

      P1 --> P2 --> P3 --> P4
      end

      Start --> Process
      \```

      Note: When triggered by **update memory bank**, I MUST review every memory bank file, even if some don't require updates. Focus particularly on activeContext.md and progress.md as they track current state.

      ## Project Intelligence (.cursor/rules)

      The .cursor/rules file is my learning journal for each project. It captures important patterns, preferences, and project intelligence that help me work more effectively. As I work with you and the project, I'll discover and document key insights that aren't obvious from the code alone.

      \```mermaid
      flowchart TD
      Start{Discover New Pattern}

      subgraph Learn [Learning Process]
      D1[Identify Pattern]
      D2[Validate with User]
      D3[Document in .cursor/rules]
      end

      subgraph Apply [Usage]
      A1[Read .cursor/rules]
      A2[Apply Learned Patterns]
      A3[Improve Future Work]
      end

      Start --> Learn
      Learn --> Apply
      \```

      ### What to Capture
      - Critical implementation paths
      - User preferences and workflow
      - Project-specific patterns
      - Known challenges
      - Evolution of project decisions
      - Tool usage patterns

      The format is flexible - focus on capturing valuable insights that help me work more effectively with you and the project. Think of .cursor/rules as a living document that grows smarter as we work together.

      REMEMBER: After every memory reset, I begin completely fresh. The Memory Bank is my only link to previous work. It must be maintained with precision and clarity, as my effectiveness depends entirely on its accuracy.

  2. 创建 Memory Bank 目录 (memory-bank/):

    • 在你的项目根目录下创建一个名为 memory-bank 的文件夹。
  3. 初始化 Memory Bank 文件:

    • memory-bank/ 文件夹内,创建上述提到的核心 Markdown 文件:
      • projectbrief.md
      • productContext.md
      • activeContext.md
      • systemPatterns.md
      • techContext.md
      • progress.md
    • 关键一步: 在 Cursor 聊天中,向 AI 发出指令:”initialize memory bank“。Cursor 会尝试根据当前项目理解(可能需要你提供信息)来填充这些文件的初始内容。之后你需要检查并完善这些内容。
  4. 维护和更新:

    • 时机: 当发现新的项目模式、完成重要功能或架构变更后、需要澄清上下文时,或者你觉得 AI 的理解有偏差时,都应该更新 Memory Bank。
    • 方式:
      • 可以直接编辑 memory-bank/ 中的 Markdown 文件。
      • 可以明确指示 Cursor:”update memory bank“。根据规则,它会重新审视所有 Memory Bank 文件,并根据当前状态和你的指示进行更新(尤其关注 activeContext.mdprogress.md)。
  5. (可选)配合 Plan/Act 模式:

    • Memory Bank 可以与 Plan/Act 模式 结合使用(需要额外配置 core.mdc 规则)。在 Plan 模式下,Cursor 会先读取 Memory Bank 来制定计划;在 Act 模式执行任务后,它可能会被指示更新 Memory Bank(尤其是 progress.mdactiveContext.md)。

部分效果

alt text

总结

Cursor Memory Bank 提供了一种有效的机制,通过持久化的结构化文档来弥补 AI 短暂记忆的不足。通过强制 AI 在每次任务开始时读取这些信息,可以显著提高其对项目上下文的理解,从而提升协作效率和代码质量。虽然需要投入一些精力来初始化和维护,但对于复杂或长期的项目来说,这种投入往往是值得的。

参考来源:

  1. How to add Cline Memory Bank feature to your cursor - Cursor Community Forum
  2. cline-memory-bank