| 1 | # User Memory |
| 2 | |
| 3 | > 阅读简体中文版:[zh_hans/MEMORY.md](zh_hans/MEMORY.md)。 |
| 4 | |
| 5 | User memory gives the model a small, persistent, local store of |
| 6 | preferences and conventions that should survive across sessions — |
| 7 | "I prefer pytest over unittest", "this codebase uses 4-space |
| 8 | indentation" — without repeating them in every conversation. |
| 9 | |
| 10 | The **native memory store** is the only memory system. |
| 11 | It is a local SQLite database, fully offline, scoped by |
| 12 | a hash of the repo's git origin. The legacy single-file |
| 13 | (`~/.deepseek/memory.md`) push/inject path and the planned Moraine MCP |
| 14 | backend were both removed: no Moraine server ever shipped in-repo, |
| 15 | and the native store already provides the same architecture (durable |
| 16 | local database plus rebuildable search indexes). |
| 17 | |
| 18 | Memory is **opt-in**. When disabled (the default), nothing is loaded, |
| 19 | nothing is intercepted, and the `remember` tool isn't surfaced to the |
| 20 | model. |
| 21 | |
| 22 | ## Enabling memory |
| 23 | |
| 24 | Either set the env var: |
| 25 | |
| 26 | ```bash |
| 27 | export DEEPSEEK_MEMORY=on |
| 28 | ``` |
| 29 | |
| 30 | Accepted truthy values are `1`, `on`, `true`, `yes`, `y`, and |
| 31 | `enabled`. |
| 32 | |
| 33 | …or add to `~/.codewhale/config.toml`: |
| 34 | |
| 35 | ```toml |
| 36 | [memory] |
| 37 | enabled = true |
| 38 | ``` |
| 39 | |
| 40 | Restart the TUI after toggling. Disabling is the same in reverse. |
| 41 | |
| 42 | ## Layout |
| 43 | |
| 44 | The store lives under a `memory/` directory next to where the legacy |
| 45 | `memory_path` anchor points — by default `memory_path = "~/.codewhale/memory.md"` |
| 46 | re-roots to `~/.codewhale/memory/`: |
| 47 | |
| 48 | ```text |
| 49 | ~/.codewhale/memory/ |
| 50 | └── store.sqlite3 # authoritative structured database |
| 51 | ``` |
| 52 | |
| 53 | The database is authoritative (`NativeMemoryStore::index_path` in |
| 54 | `crates/runtime/src/native_memory.rs`). Do not edit it as text or delete it |
| 55 | as a cache; `/memory reindex` rebuilds only its derived search indexes. |
| 56 | `global/MEMORY.md` and `workspace/<id>/MEMORY.md` appear as the source label |
| 57 | of an entry's scope; Codewhale does not write those files. A configured |
| 58 | `memory_path` is an **anchor only**: the filename is discarded and the store |
| 59 | lives in a `memory/` directory beside it. The shipped example keeps |
| 60 | `~/.codewhale/memory.md` so the store lands at |
| 61 | `~/.codewhale/memory/store.sqlite3`. |
| 62 | |
| 63 | ## What gets injected |
| 64 | |
| 65 | When memory is enabled, the system prompt carries a bounded, |
| 66 | provenance-bearing block of memory entries (up to 32 entries / |
| 67 | 12,000 chars, global plus current-workspace scope). The block is |
| 68 | wrapped to mark it as **untrusted user data**, not a second |
| 69 | instruction layer. For depth beyond the injected head, the model can |
| 70 | call the `memory_search` tool against the FTS5 index (`query` to search, |
| 71 | `id` to read one entry). |
| 72 | |
| 73 | The initial memory snapshot belongs to the session's frozen prompt prefix. |
| 74 | Changes discovered during the session are appended as history, as described |
| 75 | in [CACHE.md](CACHE.md); neither a native note update nor an external recall |
| 76 | may rewrite the system prompt or tool catalog on every turn. |
| 77 | |
| 78 | Sub-agents inherit the same packet: a child's system prompt carries the |
| 79 | parent session's block plus the `## Memory Hygiene` guidance, so pinned rules |
| 80 | reach the agent that actually edits files. The child re-composes the packet |
| 81 | from the inherited `memory_path` (no separate flag, no empty envelope) and |
| 82 | records no context receipt of its own. See |
| 83 | [SUBAGENTS.md](SUBAGENTS.md#memory-and-the-remember-tool-489). |
| 84 | |
| 85 | ## Three ways to add to memory |
| 86 | |
| 87 | ### 1. The `# ` composer prefix (#492) |
| 88 | |
| 89 | Type a single line that starts with `#` (but not `##` or `#!`) in |
| 90 | the composer: |
| 91 | |
| 92 | ``` |
| 93 | # remember to use 4-space indentation in this repo |
| 94 | ``` |
| 95 | |
| 96 | The TUI intercepts the input and saves the note as a **global** |
| 97 | candidate via the same `NativeMemoryStore::remember` path the model's |
| 98 | tool uses; it stays a candidate until you review it in Context Lens. |
| 99 | **No turn fires** — your input is consumed, the status line confirms |
| 100 | the capture (it names the global scope's `global/MEMORY.md` source |
| 101 | label, not a file on disk), and you can keep typing your real question. |
| 102 | |
| 103 | Multi-`#` prefixes deliberately fall through to normal turn |
| 104 | submission so you can paste Markdown headings without surprise. |
| 105 | |
| 106 | ### 2. The `/memory` slash command |
| 107 | |
| 108 | Inspect and maintain the native store. `/memory native …` is a |
| 109 | compatibility alias for the same subcommands |
| 110 | (`crates/tui/src/commands/groups/memory/memory.rs`). |
| 111 | |
| 112 | | Subcommand | Effect | |
| 113 | |----------------------------------------------|------------------------------------------------------| |
| 114 | | `/memory`, `/memory show`, `/memory status` | Store root and the authoritative database path | |
| 115 | | `/memory path` | Store root | |
| 116 | | `/memory search <query>` | Search reviewed, current entries in this scope | |
| 117 | | `/memory get <numeric-id>` | Read one entry | |
| 118 | | `/memory remember [global\|workspace] <note>` | Save a note to that scope (`global` by default) | |
| 119 | | `/memory import` | Import the legacy single file as candidates; the file is kept | |
| 120 | | `/memory export` | Dump entries as non-authoritative Markdown | |
| 121 | | `/memory reindex` | Rebuild the derived search indexes | |
| 122 | | `/memory clear <global\|workspace\|all> --confirm` | Delete that scope's entries (`delete` is an alias) | |
| 123 | | `/memory edit` | Points to Context Lens; the database is not edited as Markdown | |
| 124 | | `/memory help` | Show command-specific help | |
| 125 | |
| 126 | Anything else prints the help. There is no `/memory add`; use `/memory remember`. |
| 127 | |
| 128 | ### 3. The `remember` tool (auto-capture, #489) |
| 129 | |
| 130 | When memory is enabled the model gets a `remember` tool: |
| 131 | |
| 132 | ```json |
| 133 | { |
| 134 | "name": "remember", |
| 135 | "input_schema": { |
| 136 | "type": "object", |
| 137 | "additionalProperties": false, |
| 138 | "properties": { |
| 139 | "action": { "type": "string", "enum": ["append", "revise", "list"], "default": "append" }, |
| 140 | "note": { "type": "string", "minLength": 1, "maxLength": 8192 }, |
| 141 | "scope": { "type": "string", "enum": ["global", "workspace"] }, |
| 142 | "replaces": { "type": "string" }, |
| 143 | "evidence": { "type": "string" }, |
| 144 | "limit": { "type": "integer", "minimum": 1, "maximum": 20, "default": 8 } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | ``` |
| 149 | |
| 150 | The model uses this when it notices a durable preference, convention, |
| 151 | or fact worth keeping across sessions. `append` (the default) proposes |
| 152 | a new note; `revise` proposes a correction and needs the exact current |
| 153 | note in `replaces` plus `evidence`; `list` returns up to `limit` |
| 154 | current notes. Every capture is a candidate for review in Context Lens, |
| 155 | not active context, and the tool never replaces or deletes existing |
| 156 | memory. The tool is auto-approved: it can only propose candidates, and |
| 157 | approval, correction and forgetting stay with Context Lens. For |
| 158 | `append` and `revise`, scope defaults to `workspace`. |
| 159 | |
| 160 | ## What stays out of memory |
| 161 | |
| 162 | Memory is for **durable** signal. Things that should NOT live there: |
| 163 | |
| 164 | - **Secrets** — no API keys, tokens, passwords. The database is not |
| 165 | encrypted at rest and entries are injected into the system prompt. |
| 166 | - **Transient task state** — "I'm currently working on the parser" |
| 167 | changes every session; it doesn't belong in cross-session memory. |
| 168 | - **Conversation snippets** — quote-style notes belong in `/note` |
| 169 | (`.codewhale/notes.md`), not memory. |
| 170 | - **Long instructions** — anything over a few sentences should live |
| 171 | in `AGENTS.md` (project-level) or in a skill. |
| 172 | |
| 173 | ## Privacy and scope |
| 174 | |
| 175 | The native store lives on your machine and does not synchronize itself to |
| 176 | a cloud memory service. Recalled entries are sent to the selected model |
| 177 | provider as prompt context when memory is enabled. Keep secrets out of it. |
| 178 | Workspace-scoped memory is keyed by a hash of the repo's git |
| 179 | origin, so notes from one repo never leak into another repo's prompt. |
| 180 | |
| 181 | ## External memory services |
| 182 | |
| 183 | Persistent memory already works through the native store. First-class backend |
| 184 | selection currently accepts only `native` and `off`; there is no supported |
| 185 | `external`, `mem0`, or `memcode` backend setting. |
| 186 | |
| 187 | A third-party memory service can expose tools through the existing |
| 188 | [MCP](MCP.md) or [plugin](PLUGINS.md) integration. Those are the service's own |
| 189 | tools: they do not replace `remember`, `memory_search`, `/memory native`, or |
| 190 | the `#` quick-add path. Review the plugin's permissions and the service's data |
| 191 | destination before enabling it. An unavailable external service must report |
| 192 | its failure rather than silently send notes to another backend. |
| 193 | |
| 194 | A future first-class backend must cover capture, search, correction, deletion, |
| 195 | scope and error reporting across every memory entry point. It must also keep |
| 196 | volatile recall in append-only history under the cache contract above. That |
| 197 | complete migration is tracked in [#6050](https://github.com/codewhale-hq/CodeWhale/issues/6050); |
| 198 | Codewhale does not claim that migration or a commercial memory integration. |
| 199 | |
| 200 | ## Configuration reference |
| 201 | |
| 202 | ```toml |
| 203 | # ~/.codewhale/config.toml |
| 204 | [memory] |
| 205 | enabled = true # default false; or set DEEPSEEK_MEMORY=on |
| 206 | # Optional explicit backend selection: |
| 207 | # backend = "native" # "native" or "off" (default: off) |
| 208 | ``` |
| 209 | |
| 210 | | Setting | Default | Override | |
| 211 | |-----------------------|-------------------------------|---------------------------------------| |
| 212 | | Memory enabled | `false` | `[memory] enabled = true` or `DEEPSEEK_MEMORY=on` | |
| 213 | | Backend | `off` | `[memory] backend = "native"` | |
| 214 | | Store root | `~/.codewhale/memory/` | derived from `memory_path` | |
| 215 | |
| 216 | ## Related |
| 217 | |
| 218 | - `docs/SUBAGENTS.md` — sub-agents inherit memory and can use the |
| 219 | `remember` tool too. |
| 220 | - `docs/CONFIGURATION.md` — full config reference. |
| 221 | - Issue [#489](https://github.com/codewhale-hq/CodeWhale/issues/489) |
| 222 | — phase-1 EPIC tracking the work. |
| 223 |