@lotargo/memory_plugin 1.6.7 → 1.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,586 +1,585 @@
1
- <div align="center">
2
-
3
- <img src="https://raw.githubusercontent.com/Lotargo/memory_pugin/main/assets/hero.jpg" alt="@lotargo/memory_plugin" width="800" style="max-width: 100%; border-radius: 12px; margin-bottom: 16px;">
4
-
5
- <br>
6
-
7
- <img src="https://raw.githubusercontent.com/Lotargo/memory_pugin/main/assets/title.svg" alt="@lotargo/memory_plugin" width="520" style="max-width: 100%; margin-bottom: 12px;">
8
-
9
- <br>
10
-
11
- [![npm version](https://img.shields.io/npm/v/@lotargo/memory_plugin)](https://www.npmjs.com/package/@lotargo/memory_plugin)
12
- [![npm downloads](https://img.shields.io/npm/dt/@lotargo/memory_plugin)](https://www.npmjs.com/package/@lotargo/memory_plugin)
13
- [![license](https://img.shields.io/badge/license-MIT-blue)](./LICENSE)
14
- [![node version](https://img.shields.io/badge/node-%3E%3D22.5.0-brightgreen)](https://nodejs.org)
15
- [![mcp](https://img.shields.io/badge/MCP-Supported-8A2BE2)](https://modelcontextprotocol.io)
16
- [![storage](https://img.shields.io/badge/Storage-Local%20%2B%20Cloud%20Sync-success)](#storage-privacy-and-security)
17
-
18
- <br>
19
-
20
- **Local-first long-term memory, cold episodic notes, and hybrid RAG for AI coding agents**
21
-
22
- One memory system for OpenCode, Codex, Claude Code, Gemini CLI, Antigravity, Google Jules, and other MCP clients.
23
-
24
- </div>
25
-
26
- ---
27
-
28
- ## Why This Project Exists
29
-
30
- AI coding assistants forget user preferences, architectural decisions, investigations, and project context when a session ends. They also tend to mix very different kinds of information into one oversized prompt.
31
-
32
- `@lotargo/memory_plugin` separates persistent knowledge into the right storage class:
33
-
34
- | What you want to preserve | Tool | Storage behavior |
35
- | :--- | :--- | :--- |
36
- | Concise facts, preferences, constraints, conventions, and persona settings | `remember` | **Hot Notebook memory**; available during session initialization |
37
- | Detailed decisions, research, investigations, experiments, and handoffs | `remember_note` | **Cold RAG Memory Note**; searchable but not injected into every session |
38
- | Files, URLs, documentation, reports, specifications, and code | `ingest_document` | **Curated external knowledge** in the RAG index |
39
-
40
- The same engine adds Git-based project isolation, semantic search, full raw-source expansion, explicit fact-to-document links, optional Turso synchronization, and native OpenCode auto-injection.
41
-
42
- ### Highlights
43
-
44
- - Human-readable Markdown Notebook facts with stable IDs, TTL, protection, tags, superseding, and explicit `fact` / `directive` semantics.
45
- - Agent-authored long-form RAG Memory Notes for cold or episodic context.
46
- - Hybrid SQLite FTS5 BM25 + local ONNX vector retrieval with RSF/RRF fusion.
47
- - Compact semantic TOC discovery through `resultMode: "index"`, followed by deliberate full-source expansion.
48
- - PDF, DOCX, XLSX/XLS/CSV, Markdown, text, HTML/URL, and source-code ingestion.
49
- - Three-tier document hierarchy, retrieval-policy expansion for tables/code, and GraphRAG Lite symbol extraction.
50
- - Git-identity project scopes that follow a repository across directories, machines, and operating systems.
51
- - Active persona overlays shared across OpenCode, Codex, Claude Code, Gemini CLI, and Antigravity.
52
- - Local-only, cloud-only, and bidirectional hybrid-sync modes, including portable raw RAG blobs and deletion tombstones.
53
- - No Docker, external vector database, hosted embedding API, or telemetry.
54
-
55
- > This is a practical agent-memory system, not a claim of generalized benchmark superiority. Repository benchmark results describe the included evaluation corpus and configuration.
56
-
57
- ---
58
-
59
- ## Quick Start
60
-
61
- ### Requirements
62
-
63
- - Node.js `22.5.0` or newer; the project uses the built-in `node:sqlite` module.
64
- - npm/npx.
65
- - OpenCode, Codex, Claude Code, Gemini CLI, Antigravity, Google Jules, or another MCP-capable client.
66
-
67
- CPU execution with `Xenova/multilingual-e5-small` is the recommended stable default. WebGPU execution is experimental.
68
-
69
- ### Install and Configure
70
-
71
- Configure every supported client location:
72
-
73
- ```bash
74
- npm install -g @lotargo/memory_plugin
75
- memory_plugin setup
76
- ```
77
-
78
- Or run setup without a permanent global installation:
79
-
80
- ```bash
81
- npx @lotargo/memory_plugin setup
82
- ```
83
-
84
- Target one client when needed:
85
-
86
- ```bash
87
- memory_plugin setup --opencode
88
- memory_plugin setup --codex
89
- memory_plugin setup --claude
90
- memory_plugin setup --antigravity
91
- memory_plugin setup --gemini # Gemini CLI (~/.gemini/settings.json)
92
- ```
93
-
94
- Use `--local` with Antigravity setup to create the workspace-local `.agents/mcp_config.json` even when `.agents/` does not yet exist.
95
-
96
- Claude Code, Gemini CLI, and Codex setup/uninstall use their native MCP lifecycle commands when available. An ownership-checked config edit is retained as a compatibility fallback for missing, older, or non-functional client CLIs. Antigravity remains a separate integration because it uses a different config layout.
97
-
98
- Setup also installs the bundled `using-memory` skill and managed memory instructions for the selected clients. Existing unrelated configuration is preserved.
99
-
100
- ### Uninstall
101
-
102
- Remove the plugin from one or all clients without deleting Notebook/RAG data:
103
-
104
- ```bash
105
- memory_plugin uninstall --dry-run # preview
106
- memory_plugin uninstall # remove all clients, keep data
107
- memory_plugin uninstall --purge --yes # also delete local data (MEMORY_DIR, prompt state)
108
- memory_plugin uninstall --opencode --purge-cache # explicitly remove this plugin's OpenCode cache
109
- memory_plugin uninstall --opencode --claude # only selected clients
110
- npx @lotargo/memory_plugin uninstall --dry-run
111
- memory_plugin setup --uninstall --purge # alias
112
- ```
113
-
114
- What `uninstall` removes by default (without `--purge`):
115
-
116
- - `~/.config/opencode/opencode.json` plugin entry (including `file://` dev link)
117
- - `~/.claude.json` — `mcpServers.memory-agent`
118
- - `~/.gemini/settings.json` — Gemini CLI `mcpServers.memory-agent`
119
- - `~/.gemini/config/mcp_config.json` and `.agents/mcp_config.json` Antigravity `mcpServers.memory-agent`
120
- - `~/.codex/config.toml` — `[mcp_servers.memory-agent]` (only if owned by this plugin)
121
- - Managed prompt blocks from `~/.codex/AGENTS.md`, `~/.claude/CLAUDE.md`, `~/.gemini/GEMINI.md`, and `~/.gemini/config/AGENTS.md`
122
- - `using-memory` skill from each client's `skills/` directory
123
-
124
- Existing user content outside the managed prompt/persona markers is preserved. Foreign `memory-agent` registrations, modified/non-owned `using-memory` skills, unrelated file plugins, and other packages in the `@lotargo` OpenCode cache namespace are left untouched.
125
-
126
- Normal uninstall keeps OpenCode's package cache, matching the host lifecycle. `--purge-cache` removes only exact cache directories owned by this package; unrelated packages, including other packages in the `@lotargo` namespace, remain untouched.
127
-
128
- With `--purge` it also deletes `MEMORY_DIR` (`~/.config/opencode/memory` by default) and the memory-agent prompt state. Purge resolves and validates every target before changing client configuration, rejects filesystem roots, home/workspace/config roots and broad top-level paths, follows symlinks for validation, and prints the exact targets before interactive confirmation. The npm package itself is removed separately with `npm uninstall -g @lotargo/memory_plugin`. Restart clients after uninstall.
129
-
130
- On Linux/macOS, `XDG_CONFIG_HOME` and `XDG_CACHE_HOME` are respected for OpenCode configuration, prompt state, and package-cache cleanup. `OPENCODE_CONFIG_DIR` and `MEMORY_DIR` remain explicit overrides on every platform.
131
-
132
- ### Verify Codex
133
-
134
- Codex uses a direct executable chain (`node` -> `mcp-server/boot.js`) instead of an `npx`/`.cmd` launcher, avoiding Windows stdio handshake failures. Setup safely migrates legacy registrations in `~/.codex/config.toml`.
135
-
136
- ```bash
137
- memory_plugin doctor --codex
138
- ```
139
-
140
- The doctor validates the configured Node runtime, MCP initialization, tool discovery, and real `memory_info` and `recall(scope: "all")` calls.
141
-
142
- ### Headless / CI Setup
143
-
144
- ```bash
145
- # Authenticate with a Turso account token and enable hybrid sync
146
- memory_plugin setup --api-key <TURSO_API_TOKEN> --mode hybrid-sync
147
-
148
- # Or change mode when credentials already exist
149
- memory_plugin setup --mode only-cloud
150
- ```
151
-
152
- Prefer `TURSO_API_TOKEN`, `TURSO_DB_URL`, and `TURSO_DB_TOKEN` environment variables over command-line secrets because shell arguments may appear in process lists and history.
153
-
154
- ### Local Repository Development
155
-
156
- ```bash
157
- npm install
158
- npm run dev:link
159
- ```
160
-
161
- `dev:link` performs an npm global link for the `memory_plugin`, `memory-agent`, and `memory-cli` binaries; rewrites only this plugin's OpenCode entry to an absolute `file://` URL for `opencode-plugin/main.js`; creates `opencode.json.memory-dev-backup` on first use; synchronizes managed prompts; and copies the current skill to all client skill locations.
162
-
163
- After code changes, restart OpenCode to reload the module. Codex, Claude Code, Gemini CLI, and Antigravity load prompt and skill files at session start, so open a new task/session after synchronization. Publishing to npm is not required for local testing.
164
-
165
- ---
166
-
167
- ## Memory Architecture
168
-
169
- ### 1. Hot Notebook Memory
170
-
171
- Notebook memory stores concise, high-signal context in Markdown:
172
-
173
- ```text
174
- - [2026-08-22 10:00] **API Convention** — Use Fastify and Zod for new services <!-- id:a1b2c3, keep:1, tags:arch, kind:fact -->
175
- ```
176
-
177
- Supported metadata includes:
178
-
179
- - `id`: stable short identifier used by `get_fact`, `update_fact`, and `forget`.
180
- - `kind`: `fact` for descriptive context or `directive` for active personalization/working instructions.
181
- - `ttl`: `90d`, `2w`, `24h`, `12m`, or a bare day count. Expired entries are retained and marked `[EXPIRED]`.
182
- - `keep`: protects an entry from ordinary deletion.
183
- - `tags`: recall filters and legacy classification metadata.
184
- - `supersedes` / `supersededBy`: preserves version history while excluding obsolete facts from active recall.
185
-
186
- `recall(scope: "all")` returns global facts plus only the current Git-linked project's facts. Full bodies are the default and should be used for session initialization; `mode: "headers"` is only for compact inventories.
187
-
188
- ### 2. Cold RAG Memory Notes
189
-
190
- Use `remember_note` when the reusable value is in the detailed record itself:
191
-
192
- ```text
193
- remember_note(
194
- title: "Authentication Investigation",
195
- content: "Detailed symptoms, experiments, rejected explanations, and final cause...",
196
- kind: "research",
197
- tags: "auth,incident",
198
- scope: "project"
199
- )
200
- ```
201
-
202
- Supported note kinds are `decision`, `research`, `context`, `handoff`, and `note`. Notes are represented as virtual RAG documents with stable `docId` and content-addressed `blobHash`. They are searchable with the same engine as external sources but are not injected into every session.
203
-
204
- Recommended discovery flow:
205
-
206
- ```text
207
- query_knowledge_base(query: "authentication token decryption investigation", resultMode: "index")
208
- -> inspect compact candidates and stable doc_id values
209
- manage_knowledge_base(action: "read_document", docId: "selected-id")
210
- -> expand the complete raw note only when needed
211
- ```
212
-
213
- Use `resultMode: "snippet"` when retrieved passages are immediately useful. Use `resultMode: "index"` when first identifying the correct source; index mode intentionally omits bodies and disables large policy expansion.
214
-
215
- ### 3. Curated External Knowledge
216
-
217
- `ingest_document` accepts:
218
-
219
- - Raw text or Markdown (`type: "text"`).
220
- - Local files (`type: "file"`), including PDF, DOCX, XLSX, XLS, CSV, text, Markdown, and source code.
221
- - Web pages (`type: "url"`), which are fetched and normalized instead of indexing the URL string.
222
-
223
- RAG is a curated library, not an automatic archive. Ingest reliable sources likely to matter again, particularly current documentation or project specifications. Project scope is the default; use global scope only for intentionally reusable cross-project knowledge.
224
-
225
- ### Hot + Cold Linking
226
-
227
- When a decision needs both quick orientation and detailed history:
228
-
229
- 1. Save the concise conclusion with `remember`.
230
- 2. Save the rationale or investigation with `remember_note`.
231
- 3. Connect them with `link_knowledge`, using the note's returned `docId`.
232
-
233
- This keeps startup context small while preserving the complete reasoning trail without duplicating the note body into Notebook memory.
234
-
235
- ---
236
-
237
- ## Persona and Agent Personalization
238
-
239
- Notebook entries have explicit semantics:
240
-
241
- ```text
242
- kind: "fact" # descriptive context
243
- kind: "directive" # active user-approved personality or working configuration
244
- ```
245
-
246
- Use `kind: "directive"` for personality, behavior, tone, communication style, preferences, or working conventions the agent should actively apply. Explicit `kind` is authoritative; persuasive wording alone does not turn a fact into an instruction.
247
-
248
- ### OpenCode
249
-
250
- The native plugin performs complete session initialization automatically:
251
-
252
- - Global and current-project descriptive entries are injected into `<MEMORY_FACTS>`.
253
- - Active global directives are separated into `<PERSONAL_AGENT_OVERLAY>`.
254
- - Directives are promoted through OpenCode's system-prompt transform.
255
- - Agents are instructed not to perform a redundant startup `recall`; manual or filtered recall remains available.
256
-
257
- ### Codex, Claude Code, Gemini CLI, and Antigravity
258
-
259
- These clients receive plugin-owned instruction and persona blocks in:
260
-
261
- - `~/.codex/AGENTS.md`
262
- - `~/.claude/CLAUDE.md`
263
- - `~/.gemini/GEMINI.md`
264
- - `~/.gemini/config/AGENTS.md`
265
-
266
- The global Notebook is the source of truth. Managed prompt blocks are generated views and update automatically after global directive changes, relevant cloud pulls, setup, or `dev:link`.
267
-
268
- Manual synchronization:
269
-
270
- ```bash
271
- memory-cli sync-persona
272
- npm run persona:sync # from the repository
273
- ```
274
-
275
- Legacy entries using `persona`, `behavior`, `speech`, `style`, `tone`, `preference(s)`, `instruction(s)`, `directive`, or `inject:1` metadata remain compatible. Permanently classify them as explicit directives with the idempotent migration:
276
-
277
- ```bash
278
- memory-cli migrate-persona --dry-run
279
- memory-cli migrate-persona
280
- npm run persona:migrate # from the repository
281
- ```
282
-
283
- Higher-priority platform and safety instructions remain authoritative.
284
-
285
- ---
286
-
287
- ## Project Identity and Scope Isolation
288
-
289
- Project memory is Git-first:
290
-
291
- - Repositories with a remote use `git:<normalized-host-and-path>`, for example `git:github.com/owner/repo`.
292
- - Repositories without a remote use `git:local:<repository-name>`.
293
- - Every subdirectory of the same repository resolves to the same identity.
294
- - Outside Git, project memory is not created; global memory remains available.
295
-
296
- The SQLite identity registry stores remote, path, and basename aliases. It supports moving a repository between directories or operating systems without changing its logical memory identity.
297
-
298
- | Tool | Purpose |
299
- | :--- | :--- |
300
- | `link_project_memory` | Register the current Git identity and merge compatible legacy path/basename facts and RAG scope data. |
301
- | `unlink_project_memory` | Remove a path alias; optionally purge the identity record. |
302
- | `relink_project_memory` | Move/merge facts and RAG scope data to a new normalized remote identity. |
303
-
304
- For both Notebook and RAG retrieval, `all` means **global + current project**, never all known projects. Unrelated project memories and documents are isolated.
305
-
306
- ---
307
-
308
- ## Retrieval and Knowledge Graph
309
-
310
- ### Hybrid Retrieval
311
-
312
- The local retrieval pipeline combines:
313
-
314
- - SQLite FTS5 BM25 lexical search.
315
- - Local ONNX dense embeddings (`Xenova/multilingual-e5-small` by default).
316
- - RSF (default), RRF, semantic-only, or lexical-only ranking.
317
- - Optional cross-encoder reranking.
318
- - Batched query embeddings through `batch_query_knowledge_base`.
319
- - Optional fixed vector dimensions and an experimental WebGPU execution mode.
320
-
321
- Queries should be short, concept-dense phrases. For multi-part research or comparisons, use `batch_query_knowledge_base`; all query embeddings are computed in one ONNX pass.
322
-
323
- ### Three-Tier Chunking and Policy Expansion
324
-
325
- Each document is partitioned into three retrieval levels: section-level big chunks, medium blocks, and micro chunks. Tables receive compact summaries and code blocks receive signature chunks. With `policyExpansion: true` (default), matching summaries/signatures expand to their full source blocks for content-rich retrieval. Set the configuration to `false` when pure micro-chunk precision is preferred.
326
-
327
- Re-ingesting an updated path/URL preserves its stable document ID and knowledge links while rebuilding chunks, vectors, policies, and structural edges. Ingesting the same source in another scope adds a scope association without duplicating the document.
328
-
329
- ### GraphRAG Lite
330
-
331
- The SQLite graph layer requires no external graph database or ingestion-time LLM:
332
-
333
- | Relation | Meaning |
334
- | :--- | :--- |
335
- | `CONTAINS` | Document -> Section -> Micro Chunk graph hierarchy |
336
- | `DEFINES_SYMBOL` | A document section defines an extracted code symbol |
337
- | `LINKS_TO` and custom relations | A Notebook fact points to a document, note, section, or line range |
338
-
339
- Code-symbol extraction covers JavaScript/TypeScript, Python, Go, Rust, C++, Java/Kotlin, C#, PHP, and Ruby patterns.
340
-
341
- ---
342
-
343
- ## Cloud Synchronization
344
-
345
- Cloud support uses Turso / LibSQL and is optional.
346
-
347
- | Mode | Behavior |
348
- | :--- | :--- |
349
- | `only-local` (default) | Markdown notebooks, SQLite index, CAS blobs, and models remain local. |
350
- | `only-cloud` | Notebook and database operations use Turso directly; raw RAG blobs are materialized into a verified local cache when read. |
351
- | `hybrid-sync` | Local-first reads/writes with background push, reverse synchronization, and conflict resolution. |
352
-
353
- Hybrid synchronization covers Notebook stores and complete RAG state: documents, scopes, sections, chunks, vectors, retrieval policies, graph edges, fact links, compressed raw CAS blobs, and deletion tombstones. Raw notes/documents can therefore be expanded on another device rather than returning metadata without source content.
354
-
355
- Notebook conflict strategies:
356
-
357
- - `merge` (default): union fact lines with local order first and deduplication.
358
- - `cloud-wins`.
359
- - `local-wins`.
360
-
361
- Cloud operations retry with timeouts and can switch to `failoverUrl` after repeated primary failures. In `hybrid-sync`, local SQLite continues serving reads during an outage. In `only-cloud`, an unavailable primary with no failover surfaces as an error.
362
-
363
- ### Authentication
364
-
365
- ```bash
366
- memory-cli login
367
- memory-cli login --api-token # hidden prompt if value omitted
368
- memory-cli login --from-env
369
- memory-cli login --db-url <URL> # token from prompt or TURSO_DB_TOKEN
370
- memory-cli auth-status
371
- memory-cli logout
372
- ```
373
-
374
- Stored tokens live in `auth_secrets.enc`, not `config.json`. They are encrypted with AES-256-GCM using PBKDF2-HMAC-SHA256 (600,000 iterations) over a stable machine fingerprint and written with owner-only permissions where supported.
375
-
376
- This is not an OS keychain. It protects against casual inspection/file-only exfiltration, not a compromised local user account. Encrypted secrets are machine-bound. The headless `.env` fallback stores credentials in plaintext by design.
377
-
378
- ---
379
-
380
- ## Tool Reference
381
-
382
- The MCP server exposes **16 tools**. The native OpenCode plugin exposes the same 16 plus two OpenCode-specific helpers, for **18 total**.
383
-
384
- ### Notebook and Cold Memory
385
-
386
- | Tool | Important parameters | Purpose |
387
- | :--- | :--- | :--- |
388
- | `remember` | `fact`, `title`, `kind`, `scope`, `directory`, `ttl`, `keep`, `tags`, `supersedes`, optional link fields | Save a concise hot fact or directive. |
389
- | `recall` | `scope`, `directory`, `query`, `tags`, `since`, `until`, `mode`, `offset`, `limit`, `includeSuperseded` | Load/filter Notebook facts and linked-document references. |
390
- | `get_fact` | `id`, `scope`, `directory` | Read one fact and all metadata by stable ID. |
391
- | `update_fact` | `id`, `newText`, `title`, `kind`, `scope`, `directory` | Update/reclassify a fact while preserving date, metadata, and links. |
392
- | `forget` | `query`, `scope`, `directory`, `force` | Delete by index, range, ID, or text; `force` overrides `[KEEP]`. |
393
- | `memory_info` | `directory` | Show version, storage paths/counts, Git identity/registry state, and RAG statistics. |
394
- | `remember_note` | `title`, `content`, `kind`, `tags`, `scope`, `directory`, `generateEmbeddings` | Save a detailed cold/episodic note into RAG. |
395
-
396
- ### Identity and Knowledge Graph
397
-
398
- | Tool | Important parameters | Purpose |
399
- | :--- | :--- | :--- |
400
- | `link_project_memory` | `directory`, `remote` | Register Git identity and migrate compatible legacy data. |
401
- | `unlink_project_memory` | `directory`, `purge` | Remove an alias or purge its registry identity. |
402
- | `relink_project_memory` | `directory`, `remote` | Move/merge memory into a new Git remote identity. |
403
- | `link_knowledge` | `action`, `factText`, `docId`, `scope`, `startLine`, `endLine`, `relationType` | Link facts to documents/notes or inspect graph links. |
404
-
405
- ### RAG Knowledge Base
406
-
407
- | Tool | Important parameters | Purpose |
408
- | :--- | :--- | :--- |
409
- | `ingest_document` | `content`, `type`, `title`, `path`, `scope`, `directory`, `generateEmbeddings` | Ingest raw text, a local file, or a URL. |
410
- | `query_knowledge_base` | `query`, `scope`, `limit`, `instruction`, `resultMode`, `generateEmbeddings`, `directory` | Run one hybrid query in snippet or compact index mode. |
411
- | `batch_query_knowledge_base` | `queries`, `scope`, `limit`, `instruction`, `resultMode`, `generateEmbeddings`, `directory` | Run several queries with one embedding batch. |
412
- | `manage_knowledge_base` | `action`, `scope`, `docId`, `snapshotPath`, `directory` | Stats, list, full raw read, scoped delete/unlink, snapshot export/import. |
413
- | `reindex_knowledge_base` | `model`, `dimension` | Rebuild vectors after changing model/dimension while preserving source and graph data. |
414
-
415
- ### OpenCode-Only Helpers
416
-
417
- | Tool | Purpose |
418
- | :--- | :--- |
419
- | `list-mcp-tools` | Show connected MCP servers and their intended roles. |
420
- | `mcp-reminder` | Suggest a connected MCP/tool family for a described task. |
421
-
422
- ---
423
-
424
- ## CLI Reference
425
-
426
- `memory_plugin` and `memory-agent` are MCP stdio entry points. `memory_plugin setup` performs client installation, while `memory_plugin cli` or `memory-cli` opens the interactive control panel. Direct administration commands should use `memory-cli`.
427
-
428
- | Command | Purpose |
429
- | :--- | :--- |
430
- | `memory_plugin setup [client flags] [--mode <mode>]` | Configure clients, skills, prompts, and optional cloud mode/auth. |
431
- | `memory_plugin doctor --codex` | Validate Codex configuration and live MCP behavior. |
432
- | `memory-cli` | Open the interactive TUI. |
433
- | `memory-cli login ...` / `logout` / `auth-status` | Manage Turso authentication. |
434
- | `memory-cli link --dir <path> [--remote <url>]` | Link a Git project identity. |
435
- | `memory-cli unlink --dir <path> [--purge]` | Remove an alias or registry identity. |
436
- | `memory-cli relink --dir <path> --remote <url>` | Move/merge into a new remote identity. |
437
- | `memory-cli identity --dir <path>` | Inspect resolved Git identity. |
438
- | `memory-cli migrate_titles [--key <key>]` | Add titles to legacy Notebook entries. |
439
- | `memory-cli enable-prompt` / `disable-prompt` | Add/remove only plugin-owned memory instruction blocks. |
440
- | `memory-cli sync-persona` | Regenerate managed persona blocks from global directives. |
441
- | `memory-cli migrate-persona [--dry-run]` | Convert legacy persona metadata to explicit `kind:directive`. |
442
- | `memory-cli dev-link` | Link the installed binaries/OpenCode plugin to the working repository. |
443
- | `memory-cli uninstall [--purge] [--purge-cache] [--dry-run] [--yes] [client flags]` | Remove plugin, MCP entries, prompts and skills; `--purge` deletes local data, while `--purge-cache` explicitly removes only this plugin's OpenCode cache. |
444
-
445
- The TUI provides retrieval configuration, model management, Notebook/RAG browsing, reindexing, snapshots, cloud settings, prompt integration, diagnostics, and reset actions. Use Up/Down, Enter, and Backspace to navigate.
446
-
447
- ---
448
-
449
- ## Client Integration
450
-
451
- | Client | Integration | Session initialization | Tool count |
452
- | :--- | :--- | :--- | ---: |
453
- | OpenCode | Native plugin in `~/.config/opencode/opencode.json` | Full memory auto-injection + system persona transform | 18 |
454
- | Codex | MCP server in `~/.codex/config.toml` | Managed prompt requires full `recall(scope: "all")` | 16 |
455
- | Claude Code | MCP server in `~/.claude.json` | Managed prompt requires full `recall(scope: "all")` | 16 |
456
- | Gemini CLI | MCP server in `~/.gemini/settings.json` | Managed `~/.gemini/GEMINI.md` prompt requires full `recall(scope: "all")` | 16 |
457
- | Antigravity | MCP server in `~/.gemini/config/mcp_config.json` and optional `.agents/mcp_config.json` | Managed prompt requires full `recall(scope: "all")` | 16 |
458
- | Google Jules / generic MCP | MCP stdio server | Client instructions should initialize with full recall | 16 |
459
-
460
- The bundled [`using-memory` skill](./skills/using-memory/SKILL.md) teaches agents to:
461
-
462
- 1. Avoid duplicate recall when OpenCode already auto-injected memory.
463
- 2. Perform full unfiltered recall first in clients without auto-injection.
464
- 3. Apply `kind:directive` entries as active configuration.
465
- 4. Register unlinked Git identities with `link_project_memory`.
466
- 5. Route concise facts, long internal notes, and external sources to the correct store.
467
- 6. Use semantic index discovery before expanding a full note/document.
468
- 7. Save high-signal knowledge proactively and avoid transient noise.
469
-
470
- ---
471
-
472
- ## Configuration
473
-
474
- Configuration is stored in `<memory-dir>/config.json`.
475
-
476
- | Key | Default | Meaning |
477
- | :--- | :--- | :--- |
478
- | `mode` | `only-local` | `only-local`, `only-cloud`, or `hybrid-sync` |
479
- | `conflictStrategy` | `merge` | Notebook conflict policy: `merge`, `cloud-wins`, `local-wins` |
480
- | `fusionAlgorithm` | `rsf` | `rsf`, `rrf`, `semantic_only`, or `lexical_only` |
481
- | `alpha` | `0.5` | Dense-vector weight for RSF |
482
- | `embeddingModel` | `Xenova/multilingual-e5-small` | Local Hugging Face/ONNX embedding model |
483
- | `vectorDimension` | `0` | Fixed vector size; `0` auto-detects model output |
484
- | `vectorScanLimit` | `50000` | Maximum vector candidates; `0` is unlimited |
485
- | `rerankerModel` | `none` | Optional cross-encoder model |
486
- | `rerankerEnabled` | `false` | Enable cross-encoder reranking |
487
- | `batchSize` | `12` | Ingestion embedding batch size |
488
- | `policyExpansion` | `true` | Expand matched table summaries/code signatures |
489
- | `executionDevice` | `cpu` | `cpu` or experimental `webgpu` |
490
- | `gpuAttentionBudget` | `2000000` | Experimental GPU micro-batch budget |
491
- | `onnxThreads` | `0` | WASM thread count; `0` auto-detects |
492
- | `tursoUrl` | `""` | Primary LibSQL endpoint populated by login |
493
- | `failoverUrl` | `""` | Optional secondary cloud endpoint |
494
- | `authorized` | `false` | Whether cloud authorization completed |
495
- | `username` | `""` | Authenticated Turso username |
496
- | `ingestAllowedPaths` | `[]` | Additional directories allowed for local-file ingestion |
497
- | `ingestAllowAnyPath` | `false` | Unsafe escape hatch allowing arbitrary file reads |
498
-
499
- `ingest_document(type: "file")` is restricted to the current working directory, the plugin data directory, and explicitly allowed paths. This prevents a prompt-injected agent from silently indexing unrelated secrets such as SSH keys or `.env` files.
500
-
501
- ---
502
-
503
- ## Storage, Privacy, and Security
504
-
505
- The data-directory resolution order is:
506
-
507
- 1. `MEMORY_DIR`.
508
- 2. `$OPENCODE_CONFIG_DIR/memory`.
509
- 3. Existing legacy `~/.config/opencode/memory`.
510
- 4. `%LOCALAPPDATA%/opencode/memory` on Windows.
511
- 5. `$XDG_CONFIG_HOME/opencode/memory` or `~/.config/opencode/memory` elsewhere.
512
-
513
- Important paths inside it:
514
-
515
- ```text
516
- global.md global Notebook facts/directives
517
- git_<identity>.md per-project Notebook facts
518
- config.json non-secret configuration
519
- auth_secrets.enc encrypted cloud credentials
520
- storage/memory.sqlite RAG, graph, identity registry, sync state
521
- storage/blobs/ content-addressed compressed raw sources
522
- storage/models/ cached ONNX models
523
- exports/ snapshots/exports
524
- ```
525
-
526
- - No telemetry or analytics are sent.
527
- - Model weights download from Hugging Face on first use and remain cached afterward.
528
- - Network access is otherwise limited to explicit URL ingestion and configured Turso cloud modes.
529
- - Snapshot path validation and local ingestion allowlists restrict arbitrary filesystem access.
530
- - SQLite uses foreign keys, migrations, transactions, and a busy timeout for concurrent access.
531
-
532
- ### Dependency Advisories
533
-
534
- At the time of this README update, `npm audit` reports three high-severity findings with no npm-available fix:
535
-
536
- | Package | Exposure in this project | Mitigation |
537
- | :--- | :--- | :--- |
538
- | `xlsx` | Spreadsheet parsing when the user explicitly ingests XLSX/XLS/CSV | Do not ingest untrusted spreadsheets. Advisories: [prototype pollution](https://github.com/advisories/GHSA-4r6h-8v6p-xvw6), [ReDoS](https://github.com/advisories/GHSA-5pgg-2g8v-p4x9). |
539
- | `sharp` via `@huggingface/transformers` | Transformers dependency includes image decoding, while this project supplies text to embedding/reranking pipelines | Normal text-memory use does not exercise the image path. Advisory: [libvips inherited vulnerabilities](https://github.com/advisories/GHSA-f88m-g3jw-g9cj). |
540
-
541
- ---
542
-
543
- ## Testing and Benchmarks
544
-
545
- ```bash
546
- npm test # 25 unit, integration, and simulated-cloud suites
547
- npm run smoke # real ONNX vectors and end-to-end memory journey
548
- npm run test:rag # retrieval quality evaluation
549
- npm run benchmark # full search benchmark report
550
- npm run benchmark:table-code
551
- ```
552
-
553
- The fast suites use `generateEmbeddings: false` in retrieval paths for deterministic offline coverage. `npm run smoke` covers the dense-vector path with real cached/downloaded model weights and checks multilingual semantic retrieval. Both modes are needed: lexical-only tests cannot catch a broken vector serialization or ONNX execution path.
554
-
555
- The unified suites cover fact formatting, typed directives, persona migration/synchronization, client prompt safety, Codex launcher compatibility, Git identity isolation, RAG scopes, policy expansion, RAG Memory Notes, semantic index output, raw blob portability, reverse sync, tombstones, snapshots, MCP contracts, and cloud authentication workflows.
556
-
557
- See [`docs/BENCHMARKS.md`](./docs/BENCHMARKS.md) for methodology and detailed reports.
558
-
559
- ### Included Search Evaluation
560
-
561
- The stored 32-document / 21-query technical corpus produced:
562
-
563
- | Strategy | MRR@5 | Recall@5 | NDCG@5 |
564
- | :--- | :---: | :---: | :---: |
565
- | BM25 lexical only | 0.6706 | 76.19% | 0.6934 |
566
- | Dense ONNX only | 0.8135 | 100.00% | 0.8612 |
567
- | Hybrid RRF (`k=60`) | 0.8810 | 95.24% | 0.8997 |
568
- | **Hybrid RSF (`alpha=0.5`)** | **0.9286** | **100.00%** | **0.9473** |
569
-
570
- ---
571
-
572
- ## Troubleshooting
573
-
574
- - **`No such built-in module: node:sqlite`**: install Node.js `22.5.0` or newer.
575
- - **Codex tools are missing**: run `memory_plugin setup --codex`, then `memory_plugin doctor --codex`, and open a new Codex task.
576
- - **OpenCode still runs old code**: restart OpenCode. For repository development, confirm `npm run dev:link` points its plugin entry to `opencode-plugin/main.js`.
577
- - **Persona changes are not visible**: run `memory-cli sync-persona`, then start a new CLI session/task. Use `memory-cli migrate-persona --dry-run` for legacy entries.
578
- - **Project recall is empty**: call `memory_info`; if a Git identity is `Registry: unlinked`, run `link_project_memory` or `memory-cli link --dir <repo>`.
579
- - **A raw note/document exists only in cloud**: `manage_knowledge_base(action: "read_document")` automatically materializes and verifies its CAS blob locally when cloud credentials are available.
580
- - **Embedding model changed**: run `reindex_knowledge_base` or use the TUI `[REINDEX]` action.
581
-
582
- ---
583
-
584
- ## License
585
-
586
- [MIT](./LICENSE)
1
+ <div align="center">
2
+
3
+ <img src="https://raw.githubusercontent.com/Lotargo/memory_plugin/main/assets/hero.jpg" alt="@lotargo/memory_plugin" width="800" style="max-width: 100%; border-radius: 12px; margin-bottom: 16px;">
4
+
5
+ <br>
6
+
7
+ <img src="https://raw.githubusercontent.com/Lotargo/memory_plugin/main/assets/title.svg" alt="@lotargo/memory_plugin" width="520" style="max-width: 100%; margin-bottom: 12px;">
8
+
9
+ <br>
10
+
11
+ [![npm version](https://img.shields.io/npm/v/@lotargo/memory_plugin)](https://www.npmjs.com/package/@lotargo/memory_plugin)
12
+ [![npm downloads](https://img.shields.io/npm/dt/@lotargo/memory_plugin)](https://www.npmjs.com/package/@lotargo/memory_plugin)
13
+ [![license](https://img.shields.io/badge/license-MIT-blue)](./LICENSE)
14
+ [![node version](https://img.shields.io/badge/node-%3E%3D22.5.0-brightgreen)](https://nodejs.org)
15
+ [![mcp](https://img.shields.io/badge/MCP-Supported-8A2BE2)](https://modelcontextprotocol.io)
16
+ [![storage](https://img.shields.io/badge/Storage-Local%20%2B%20Cloud%20Sync-success)](#storage-privacy-and-security)
17
+
18
+ <br>
19
+
20
+ **Local-first long-term memory, hybrid RAG, and agent personalization for AI coding agents**
21
+
22
+ One memory system for OpenCode, Codex, Claude Code, Gemini CLI, Antigravity, Google Jules, and other MCP clients.
23
+
24
+ </div>
25
+
26
+ # @lotargo/memory_plugin — Local-First Memory, Hybrid RAG & Agent Personalization
27
+
28
+ ---
29
+
30
+ ## Why This Project Exists
31
+
32
+ AI coding assistants forget user preferences, architectural decisions, investigations, and project context when a session ends. They also tend to mix very different kinds of information into one oversized prompt.
33
+
34
+ `@lotargo/memory_plugin` separates persistent knowledge into the right storage class:
35
+
36
+ | What you want to preserve | Tool | Storage behavior |
37
+ | :--- | :--- | :--- |
38
+ | Concise facts, preferences, constraints, conventions, and persona settings | `remember` | **Hot Notebook memory**; available during session initialization |
39
+ | Detailed decisions, research, investigations, experiments, and handoffs | `remember_note` | **Cold RAG Memory Note**; searchable but not injected into every session |
40
+ | Files, URLs, documentation, reports, specifications, and code | `ingest_document` | **Curated external knowledge** in the RAG index |
41
+
42
+ The same engine adds Git-based project isolation, semantic search, full raw-source expansion, explicit fact-to-document links, optional Turso synchronization, and native OpenCode auto-injection.
43
+
44
+ ### Highlights
45
+
46
+ - Human-readable Markdown Notebook facts with stable IDs, TTL, protection, tags, superseding, and explicit `fact` / `directive` semantics.
47
+ - Agent-authored long-form RAG Memory Notes for cold or episodic context.
48
+ - Hybrid SQLite FTS5 BM25 + local ONNX vector retrieval with RSF/RRF fusion.
49
+ - Compact semantic TOC discovery through `resultMode: "index"`, followed by deliberate full-source expansion.
50
+ - PDF, DOCX, XLSX/XLS/CSV, Markdown, text, HTML/URL, and source-code ingestion.
51
+ - Three-tier document hierarchy, retrieval-policy expansion for tables/code, and GraphRAG Lite symbol extraction.
52
+ - Git-identity project scopes that follow a repository across directories, machines, and operating systems.
53
+ - Active persona overlays shared across OpenCode, Codex, Claude Code, Gemini CLI, and Antigravity.
54
+ - Local-only, cloud-only, and bidirectional hybrid-sync modes, including portable raw RAG blobs and deletion tombstones.
55
+ - No Docker, external vector database, hosted embedding API, or telemetry.
56
+
57
+ > This is a practical agent-memory system, not a claim of generalized benchmark superiority. Repository benchmark results describe the included evaluation corpus and configuration.
58
+
59
+ ---
60
+
61
+ ## Quick Start
62
+
63
+ ### Requirements
64
+
65
+ - Node.js `22.5.0` or newer; the project uses the built-in `node:sqlite` module.
66
+ - npm/npx.
67
+ - OpenCode, Codex, Claude Code, Gemini CLI, Antigravity, Google Jules, or another MCP-capable client.
68
+
69
+ CPU execution with `Xenova/multilingual-e5-small` is the recommended stable default. WebGPU execution is experimental.
70
+
71
+ ### Install and Configure
72
+
73
+ Configure every supported client location:
74
+
75
+ ```bash
76
+ npm install -g @lotargo/memory_plugin
77
+ memory_plugin setup
78
+ ```
79
+
80
+ Or run setup without a permanent global installation:
81
+
82
+ ```bash
83
+ npx @lotargo/memory_plugin setup
84
+ ```
85
+
86
+ Target one client when needed:
87
+
88
+ ```bash
89
+ memory_plugin setup --opencode
90
+ memory_plugin setup --codex
91
+ memory_plugin setup --claude
92
+ memory_plugin setup --antigravity
93
+ memory_plugin setup --gemini # Gemini CLI (~/.gemini/settings.json)
94
+ ```
95
+
96
+ Use `--local` with Antigravity setup to create the workspace-local `.agents/mcp_config.json` even when `.agents/` does not yet exist.
97
+
98
+ Claude Code, Gemini CLI, and Codex setup/uninstall use their native MCP lifecycle commands when available. An ownership-checked config edit is retained as a compatibility fallback for missing, older, or non-functional client CLIs. Antigravity remains a separate integration because it uses a different config layout.
99
+
100
+ Setup also installs the bundled `using-memory` skill and managed memory instructions for the selected clients. Existing unrelated configuration is preserved.
101
+
102
+ ### Uninstall
103
+
104
+ Remove the plugin from one or all clients without deleting Notebook/RAG data:
105
+
106
+ ```bash
107
+ memory_plugin uninstall --dry-run # preview
108
+ memory_plugin uninstall # remove all clients, keep data
109
+ memory_plugin uninstall --purge --yes # also delete local data (MEMORY_DIR, prompt state)
110
+ memory_plugin uninstall --opencode --purge-cache # explicitly remove this plugin's OpenCode cache
111
+ memory_plugin uninstall --opencode --claude # only selected clients
112
+ npx @lotargo/memory_plugin uninstall --dry-run
113
+ memory_plugin setup --uninstall --purge # alias
114
+ ```
115
+
116
+ What `uninstall` removes by default (without `--purge`):
117
+
118
+ - `~/.config/opencode/opencode.json` — plugin entry (including `file://` dev link)
119
+ - `~/.claude.json` — `mcpServers.memory-agent`
120
+ - `~/.gemini/settings.json` — Gemini CLI `mcpServers.memory-agent`
121
+ - `~/.gemini/config/mcp_config.json` and `.agents/mcp_config.json` Antigravity `mcpServers.memory-agent`
122
+ - `~/.codex/config.toml` — `[mcp_servers.memory-agent]` (only if owned by this plugin)
123
+ - Managed prompt blocks from `~/.codex/AGENTS.md`, `~/.claude/CLAUDE.md`, `~/.gemini/GEMINI.md`, and `~/.gemini/config/AGENTS.md`
124
+ - `using-memory` skill from each client's `skills/` directory
125
+
126
+ Existing user content outside the managed prompt/persona markers is preserved. Foreign `memory-agent` registrations, modified/non-owned `using-memory` skills, unrelated file plugins, and other packages in the `@lotargo` OpenCode cache namespace are left untouched.
127
+
128
+ Normal uninstall keeps OpenCode's package cache, matching the host lifecycle. `--purge-cache` removes only exact cache directories owned by this package; unrelated packages, including other packages in the `@lotargo` namespace, remain untouched.
129
+
130
+ With `--purge` it also deletes `MEMORY_DIR` (`~/.config/opencode/memory` by default) and the memory-agent prompt state. Purge resolves and validates every target before changing client configuration, rejects filesystem roots, home/workspace/config roots and broad top-level paths, follows symlinks for validation, and prints the exact targets before interactive confirmation. The npm package itself is removed separately with `npm uninstall -g @lotargo/memory_plugin`. Restart clients after uninstall.
131
+
132
+ On Linux/macOS, `XDG_CONFIG_HOME` and `XDG_CACHE_HOME` are respected for OpenCode configuration, prompt state, and package-cache cleanup. `OPENCODE_CONFIG_DIR` and `MEMORY_DIR` remain explicit overrides on every platform.
133
+
134
+ ### Verify Codex
135
+
136
+ Codex uses a direct executable chain (`node` -> `mcp-server/boot.js`) instead of an `npx`/`.cmd` launcher, avoiding Windows stdio handshake failures. Setup safely migrates legacy registrations in `~/.codex/config.toml`.
137
+
138
+ ```bash
139
+ memory_plugin doctor --codex
140
+ ```
141
+
142
+ The doctor validates the configured Node runtime, MCP initialization, tool discovery, and real `memory_info` and `recall(scope: "all")` calls.
143
+
144
+ ### Headless / CI Setup
145
+
146
+ ```bash
147
+ # Authenticate with a Turso account token and enable hybrid sync
148
+ memory_plugin setup --api-key <TURSO_API_TOKEN> --mode hybrid-sync
149
+
150
+ # Or change mode when credentials already exist
151
+ memory_plugin setup --mode only-cloud
152
+ ```
153
+
154
+ Prefer `TURSO_API_TOKEN`, `TURSO_DB_URL`, and `TURSO_DB_TOKEN` environment variables over command-line secrets because shell arguments may appear in process lists and history.
155
+
156
+ ### Local Repository Development
157
+
158
+ ```bash
159
+ npm install
160
+ npm run dev:link
161
+ ```
162
+
163
+ `dev:link` performs an npm global link for the `memory_plugin`, `memory-agent`, and `memory-cli` binaries; rewrites only this plugin's OpenCode entry to an absolute `file://` URL for `opencode-plugin/main.js`; creates `opencode.json.memory-dev-backup` on first use; synchronizes managed prompts; and copies the current skill to all client skill locations.
164
+
165
+ After code changes, restart OpenCode to reload the module. Codex, Claude Code, Gemini CLI, and Antigravity load prompt and skill files at session start, so open a new task/session after synchronization. Publishing to npm is not required for local testing.
166
+
167
+ ---
168
+
169
+ ## Memory Architecture
170
+
171
+ ### 1. Hot Notebook Memory
172
+
173
+ Notebook memory stores concise, high-signal context in Markdown:
174
+
175
+ ```text
176
+ - [2026-08-22 10:00] **API Convention** — Use Fastify and Zod for new services <!-- id:a1b2c3, keep:1, tags:arch, kind:fact -->
177
+ ```
178
+
179
+ Supported metadata includes:
180
+
181
+ - `id`: stable short identifier used by `get_fact`, `update_fact`, and `forget`.
182
+ - `kind`: `fact` for descriptive context or `directive` for active personalization/working instructions.
183
+ - `ttl`: `90d`, `2w`, `24h`, `12m`, or a bare day count. Expired entries are retained and marked `[EXPIRED]`.
184
+ - `keep`: protects an entry from ordinary deletion.
185
+ - `tags`: recall filters and legacy classification metadata.
186
+ - `supersedes` / `supersededBy`: preserves version history while excluding obsolete facts from active recall.
187
+
188
+ `recall(scope: "all")` returns global facts plus only the current Git-linked project's facts. Full bodies are the default and should be used for session initialization; `mode: "headers"` is only for compact inventories.
189
+
190
+ ### 2. Cold RAG Memory Notes
191
+
192
+ Use `remember_note` when the reusable value is in the detailed record itself:
193
+
194
+ ```text
195
+ remember_note(
196
+ title: "Authentication Investigation",
197
+ content: "Detailed symptoms, experiments, rejected explanations, and final cause...",
198
+ kind: "research",
199
+ tags: "auth,incident",
200
+ scope: "project"
201
+ )
202
+ ```
203
+
204
+ Supported note kinds are `decision`, `research`, `context`, `handoff`, and `note`. Notes are represented as virtual RAG documents with stable `docId` and content-addressed `blobHash`. They are searchable with the same engine as external sources but are not injected into every session.
205
+
206
+ Recommended discovery flow:
207
+
208
+ ```text
209
+ query_knowledge_base(query: "authentication token decryption investigation", resultMode: "index")
210
+ -> inspect compact candidates and stable doc_id values
211
+ manage_knowledge_base(action: "read_document", docId: "selected-id")
212
+ -> expand the complete raw note only when needed
213
+ ```
214
+
215
+ Use `resultMode: "snippet"` when retrieved passages are immediately useful. Use `resultMode: "index"` when first identifying the correct source; index mode intentionally omits bodies and disables large policy expansion.
216
+
217
+ ### 3. Curated External Knowledge
218
+
219
+ `ingest_document` accepts:
220
+
221
+ - Raw text or Markdown (`type: "text"`).
222
+ - Local files (`type: "file"`), including PDF, DOCX, XLSX, XLS, CSV, text, Markdown, and source code.
223
+ - Web pages (`type: "url"`), which are fetched and normalized instead of indexing the URL string.
224
+
225
+ RAG is a curated library, not an automatic archive. Ingest reliable sources likely to matter again, particularly current documentation or project specifications. Project scope is the default; use global scope only for intentionally reusable cross-project knowledge.
226
+
227
+ ### Hot + Cold Linking
228
+
229
+ When a decision needs both quick orientation and detailed history:
230
+
231
+ 1. Save the concise conclusion with `remember`.
232
+ 2. Save the rationale or investigation with `remember_note`.
233
+ 3. Connect them with `link_knowledge`, using the note's returned `docId`.
234
+
235
+ This keeps startup context small while preserving the complete reasoning trail without duplicating the note body into Notebook memory.
236
+
237
+ ---
238
+
239
+ ## Persona and Agent Personalization
240
+
241
+ Notebook entries have explicit semantics:
242
+
243
+ ```text
244
+ kind: "fact" # descriptive context
245
+ kind: "directive" # active user-approved personality or working configuration
246
+ ```
247
+
248
+ Use `kind: "directive"` for personality, behavior, tone, communication style, preferences, or working conventions the agent should actively apply. Explicit `kind` is authoritative; persuasive wording alone does not turn a fact into an instruction.
249
+
250
+ ### OpenCode
251
+
252
+ The native plugin performs complete session initialization automatically:
253
+
254
+ - Global and current-project descriptive entries are injected into `<MEMORY_FACTS>`.
255
+ - Active global directives are separated into `<PERSONAL_AGENT_OVERLAY>`.
256
+ - Directives are promoted through OpenCode's system-prompt transform.
257
+ - Agents are instructed not to perform a redundant startup `recall`; manual or filtered recall remains available.
258
+
259
+ ### Codex, Claude Code, Gemini CLI, and Antigravity
260
+
261
+ These clients receive plugin-owned instruction and persona blocks in:
262
+
263
+ - `~/.codex/AGENTS.md`
264
+ - `~/.claude/CLAUDE.md`
265
+ - `~/.gemini/GEMINI.md`
266
+ - `~/.gemini/config/AGENTS.md`
267
+
268
+ The global Notebook is the source of truth. Managed prompt blocks are generated views and update automatically after global directive changes, relevant cloud pulls, setup, or `dev:link`.
269
+
270
+ Manual synchronization:
271
+
272
+ ```bash
273
+ memory-cli sync-persona
274
+ npm run persona:sync # from the repository
275
+ ```
276
+
277
+ Legacy entries using `persona`, `behavior`, `speech`, `style`, `tone`, `preference(s)`, `instruction(s)`, `directive`, or `inject:1` metadata remain compatible. Permanently classify them as explicit directives with the idempotent migration:
278
+
279
+ ```bash
280
+ memory-cli migrate-persona --dry-run
281
+ memory-cli migrate-persona
282
+ npm run persona:migrate # from the repository
283
+ ```
284
+
285
+ Higher-priority platform and safety instructions remain authoritative.
286
+
287
+ ---
288
+
289
+ ## Project Identity and Scope Isolation
290
+
291
+ Project memory is Git-first:
292
+
293
+ - Repositories with a remote use `git:<normalized-host-and-path>`, for example `git:github.com/owner/repo`.
294
+ - Repositories without a remote use `git:local:<repository-name>`.
295
+ - Every subdirectory of the same repository resolves to the same identity.
296
+ - Outside Git, project memory is not created; global memory remains available.
297
+
298
+ The SQLite identity registry stores remote, path, and basename aliases. It supports moving a repository between directories or operating systems without changing its logical memory identity.
299
+
300
+ | Tool | Purpose |
301
+ | :--- | :--- |
302
+ | `link_project_memory` | Register the current Git identity and merge compatible legacy path/basename facts and RAG scope data. |
303
+ | `unlink_project_memory` | Remove a path alias; optionally purge the identity record. |
304
+ | `relink_project_memory` | Move/merge facts and RAG scope data to a new normalized remote identity. |
305
+
306
+ For both Notebook and RAG retrieval, `all` means **global + current project**, never all known projects. Unrelated project memories and documents are isolated.
307
+
308
+ ---
309
+
310
+ ## Retrieval and Knowledge Graph
311
+
312
+ ### Hybrid Retrieval
313
+
314
+ The local retrieval pipeline combines:
315
+
316
+ - SQLite FTS5 BM25 lexical search.
317
+ - Local ONNX dense embeddings (`Xenova/multilingual-e5-small` by default).
318
+ - RSF (default), RRF, semantic-only, or lexical-only ranking.
319
+ - Optional cross-encoder reranking.
320
+ - Batched query embeddings through `batch_query_knowledge_base`.
321
+ - Optional fixed vector dimensions and an experimental WebGPU execution mode.
322
+
323
+ Queries should be short, concept-dense phrases. For multi-part research or comparisons, use `batch_query_knowledge_base`; all query embeddings are computed in one ONNX pass.
324
+
325
+ ### Three-Tier Chunking and Policy Expansion
326
+
327
+ Each document is partitioned into three retrieval levels: section-level big chunks, medium blocks, and micro chunks. Tables receive compact summaries and code blocks receive signature chunks. With `policyExpansion: true` (default), matching summaries/signatures expand to their full source blocks for content-rich retrieval. Set the configuration to `false` when pure micro-chunk precision is preferred.
328
+
329
+ Re-ingesting an updated path/URL preserves its stable document ID and knowledge links while rebuilding chunks, vectors, policies, and structural edges. Ingesting the same source in another scope adds a scope association without duplicating the document.
330
+
331
+ ### GraphRAG Lite
332
+
333
+ The SQLite graph layer requires no external graph database or ingestion-time LLM:
334
+
335
+ | Relation | Meaning |
336
+ | :--- | :--- |
337
+ | `CONTAINS` | Document -> Section -> Micro Chunk graph hierarchy |
338
+ | `DEFINES_SYMBOL` | A document section defines an extracted code symbol |
339
+ | `LINKS_TO` and custom relations | A Notebook fact points to a document, note, section, or line range |
340
+
341
+ Code-symbol extraction covers JavaScript/TypeScript, Python, Go, Rust, C++, Java/Kotlin, C#, PHP, and Ruby patterns.
342
+
343
+ ---
344
+
345
+ ## Cloud Synchronization
346
+
347
+ Cloud support uses Turso / LibSQL and is optional.
348
+
349
+ | Mode | Behavior |
350
+ | :--- | :--- |
351
+ | `only-local` (default) | Markdown notebooks, SQLite index, CAS blobs, and models remain local. |
352
+ | `only-cloud` | Notebook and database operations use Turso directly; raw RAG blobs are materialized into a verified local cache when read. |
353
+ | `hybrid-sync` | Local-first reads/writes with background push, reverse synchronization, and conflict resolution. |
354
+
355
+ Hybrid synchronization covers Notebook stores and complete RAG state: documents, scopes, sections, chunks, vectors, retrieval policies, graph edges, fact links, compressed raw CAS blobs, and deletion tombstones. Raw notes/documents can therefore be expanded on another device rather than returning metadata without source content.
356
+
357
+ Notebook conflict strategies:
358
+
359
+ - `merge` (default): union fact lines with local order first and deduplication.
360
+ - `cloud-wins`.
361
+ - `local-wins`.
362
+
363
+ Cloud operations retry with timeouts and can switch to `failoverUrl` after repeated primary failures. In `hybrid-sync`, local SQLite continues serving reads during an outage. In `only-cloud`, an unavailable primary with no failover surfaces as an error.
364
+
365
+ ### Authentication
366
+
367
+ ```bash
368
+ memory-cli login
369
+ memory-cli login --api-token # hidden prompt if value omitted
370
+ memory-cli login --from-env
371
+ memory-cli login --db-url <URL> # token from prompt or TURSO_DB_TOKEN
372
+ memory-cli auth-status
373
+ memory-cli logout
374
+ ```
375
+
376
+ Stored tokens live in `auth_secrets.enc`, not `config.json`. They are encrypted with AES-256-GCM using PBKDF2-HMAC-SHA256 (600,000 iterations) over a stable machine fingerprint and written with owner-only permissions where supported.
377
+
378
+ This is not an OS keychain. It protects against casual inspection/file-only exfiltration, not a compromised local user account. Encrypted secrets are machine-bound. The headless `.env` fallback stores credentials in plaintext by design.
379
+
380
+ ---
381
+
382
+ ## Tool Reference
383
+
384
+ The MCP server exposes **16 tools**. The native OpenCode plugin exposes the same 16 plus two OpenCode-specific helpers, for **18 total**.
385
+
386
+ ### Notebook and Cold Memory
387
+
388
+ | Tool | Important parameters | Purpose |
389
+ | :--- | :--- | :--- |
390
+ | `remember` | `fact`, `title`, `kind`, `scope`, `directory`, `ttl`, `keep`, `tags`, `supersedes`, optional link fields | Save a concise hot fact or directive. |
391
+ | `recall` | `scope`, `directory`, `query`, `tags`, `since`, `until`, `mode`, `offset`, `limit`, `includeSuperseded` | Load/filter Notebook facts and linked-document references. |
392
+ | `get_fact` | `id`, `scope`, `directory` | Read one fact and all metadata by stable ID. |
393
+ | `update_fact` | `id`, `newText`, `title`, `kind`, `scope`, `directory` | Update/reclassify a fact while preserving date, metadata, and links. |
394
+ | `forget` | `query`, `scope`, `directory`, `force` | Delete by index, range, ID, or text; `force` overrides `[KEEP]`. |
395
+ | `memory_info` | `directory` | Show version, storage paths/counts, Git identity/registry state, and RAG statistics. |
396
+ | `remember_note` | `title`, `content`, `kind`, `tags`, `scope`, `directory`, `generateEmbeddings` | Save a detailed cold/episodic note into RAG. |
397
+
398
+ ### Identity and Knowledge Graph
399
+
400
+ | Tool | Important parameters | Purpose |
401
+ | :--- | :--- | :--- |
402
+ | `link_project_memory` | `directory`, `remote` | Register Git identity and migrate compatible legacy data. |
403
+ | `unlink_project_memory` | `directory`, `purge` | Remove an alias or purge its registry identity. |
404
+ | `relink_project_memory` | `directory`, `remote` | Move/merge memory into a new normalized remote identity. |
405
+ | `link_knowledge` | `action`, `factText`, `docId`, `scope`, `startLine`, `endLine`, `relationType` | Link facts to documents/notes or inspect graph links. |
406
+
407
+ ### RAG Knowledge Base
408
+
409
+ | Tool | Important parameters | Purpose |
410
+ | :--- | :--- | :--- |
411
+ | `ingest_document` | `content`, `type`, `title`, `path`, `scope`, `directory`, `generateEmbeddings` | Ingest raw text, a local file, or a URL. |
412
+ | `query_knowledge_base` | `query`, `scope`, `limit`, `instruction`, `resultMode`, `generateEmbeddings`, `directory` | Run one hybrid query in snippet or compact index mode. |
413
+ | `batch_query_knowledge_base` | `queries`, `scope`, `limit`, `instruction`, `resultMode`, `generateEmbeddings`, `directory` | Run several queries with one embedding batch. |
414
+ | `manage_knowledge_base` | `action`, `scope`, `docId`, `snapshotPath`, `directory` | Stats, list, full raw read, scoped delete/unlink, snapshot export/import. |
415
+ | `reindex_knowledge_base` | `model`, `dimension` | Rebuild vectors after changing model/dimension while preserving source and graph data. |
416
+
417
+ ### OpenCode-Only Helpers
418
+
419
+ | Tool | Purpose |
420
+ | :--- | :--- |
421
+ | `list-mcp-tools` | Show connected MCP servers and their intended roles. |
422
+ | `mcp-reminder` | Suggest a connected MCP/tool family for a described task. |
423
+
424
+ ---
425
+
426
+ ## CLI Reference
427
+
428
+ `memory_plugin` and `memory-agent` are MCP stdio entry points. `memory_plugin setup` performs client installation, while `memory_plugin cli` or `memory-cli` opens the interactive control panel. Direct administration commands should use `memory-cli`.
429
+
430
+ | Command | Purpose |
431
+ | :--- | :--- |
432
+ | `memory_plugin setup [client flags] [--mode <mode>]` | Configure clients, skills, prompts, and optional cloud mode/auth. |
433
+ | `memory_plugin doctor --codex` | Validate Codex configuration and live MCP behavior. |
434
+ | `memory-cli` | Open the interactive TUI. |
435
+ | `memory-cli login ...` / `logout` / `auth-status` | Manage Turso authentication. |
436
+ | `memory-cli link --dir <path> [--remote <url>]` | Link a Git project identity. |
437
+ | `memory-cli unlink --dir <path> [--purge]` | Remove an alias or registry identity. |
438
+ | `memory-cli relink --dir <path> --remote <url>` | Move/merge into a new remote identity. |
439
+ | `memory-cli identity --dir <path>` | Inspect resolved Git identity. |
440
+ | `memory-cli migrate_titles [--key <key>]` | Add titles to legacy Notebook entries. |
441
+ | `memory-cli enable-prompt` / `disable-prompt` | Add/remove only plugin-owned memory instruction blocks. |
442
+ | `memory-cli sync-persona` | Regenerate managed persona blocks from global directives. |
443
+ | `memory-cli migrate-persona [--dry-run]` | Convert legacy persona metadata to explicit `kind:directive`. |
444
+ | `memory-cli dev-link` | Link the installed binaries/OpenCode plugin to the working repository. |
445
+ | `memory-cli uninstall [--purge] [--purge-cache] [--dry-run] [--yes] [client flags]` | Remove plugin, MCP entries, prompts and skills; `--purge` deletes local data, while `--purge-cache` explicitly removes only this plugin's OpenCode cache. |
446
+
447
+ The TUI provides retrieval configuration, model management, Notebook/RAG browsing, reindexing, snapshots, cloud settings, prompt integration, diagnostics, and reset actions. Use Up/Down, Enter, and Backspace to navigate.
448
+
449
+ ---
450
+
451
+ ## Client Integration
452
+
453
+ | Client | Integration | Session initialization | Tool count |
454
+ | :--- | :--- | :--- | ---: |
455
+ | OpenCode | Native plugin in `~/.config/opencode/opencode.json` | Full memory auto-injection + system persona transform | 18 |
456
+ | Codex | MCP server in `~/.codex/config.toml` | Managed prompt requires full `recall(scope: "all")` | 16 |
457
+ | Claude Code | MCP server in `~/.claude.json` | Managed prompt requires full `recall(scope: "all")` | 16 |
458
+ | Gemini CLI | MCP server in `~/.gemini/settings.json` | Managed `~/.gemini/GEMINI.md` prompt requires full `recall(scope: "all")` | 16 |
459
+ | Antigravity | MCP server in `~/.gemini/config/mcp_config.json` and optional `.agents/mcp_config.json` | Managed prompt requires full `recall(scope: "all")` | 16 |
460
+ | Google Jules / generic MCP | MCP stdio server | Client instructions should initialize with full recall | 16 |
461
+
462
+ The bundled [`using-memory` skill](./skills/using-memory/SKILL.md) teaches agents to:
463
+
464
+ 1. Avoid duplicate recall when OpenCode already auto-injected memory.
465
+ 2. Perform full unfiltered recall first in clients without auto-injection.
466
+ 3. Apply `kind:directive` entries as active configuration.
467
+ 4. Register unlinked Git identities with `link_project_memory`.
468
+ 5. Route concise facts, long internal notes, and external sources to the correct store.
469
+ 6. Use semantic index discovery before expanding a full note/document.
470
+ 7. Save high-signal knowledge proactively and avoid transient noise.
471
+
472
+ ---
473
+
474
+ ## Configuration
475
+
476
+ Configuration is stored in `<memory-dir>/config.json`.
477
+
478
+ | Key | Default | Meaning |
479
+ | :--- | :--- | :--- |
480
+ | `mode` | `only-local` | `only-local`, `only-cloud`, or `hybrid-sync` |
481
+ | `conflictStrategy` | `merge` | Notebook conflict policy: `merge`, `cloud-wins`, `local-wins` |
482
+ | `fusionAlgorithm` | `rsf` | `rsf`, `rrf`, `semantic_only`, or `lexical_only` |
483
+ | `alpha` | `0.5` | Dense-vector weight for RSF |
484
+ | `embeddingModel` | `Xenova/multilingual-e5-small` | Local Hugging Face/ONNX embedding model |
485
+ | `vectorDimension` | `0` | Fixed vector size; `0` auto-detects model output |
486
+ | `vectorScanLimit` | `50000` | Maximum vector candidates; `0` is unlimited |
487
+ | `rerankerModel` | `none` | Optional cross-encoder model |
488
+ | `rerankerEnabled` | `false` | Enable cross-encoder reranking |
489
+ | `batchSize` | `12` | Ingestion embedding batch size |
490
+ | `policyExpansion` | `true` | Expand matched table summaries/code signatures |
491
+ | `executionDevice` | `cpu` | `cpu` or experimental `webgpu` |
492
+ | `gpuAttentionBudget` | `2000000` | Experimental GPU micro-batch budget |
493
+ | `onnxThreads` | `0` | WASM thread count; `0` auto-detects |
494
+ | `tursoUrl` | `""` | Primary LibSQL endpoint populated by login |
495
+ | `failoverUrl` | `""` | Optional secondary cloud endpoint |
496
+ | `authorized` | `false` | Whether cloud authorization completed |
497
+ | `username` | `""` | Authenticated Turso username |
498
+ | `ingestAllowedPaths` | `[]` | Additional directories allowed for local-file ingestion |
499
+ | `ingestAllowAnyPath` | `false` | Unsafe escape hatch allowing arbitrary file reads |
500
+
501
+ `ingest_document(type: "file")` is restricted to the current working directory, the plugin data directory, and explicitly allowed paths. This prevents a prompt-injected agent from silently indexing unrelated secrets such as SSH keys or `.env` files.
502
+
503
+ ---
504
+
505
+ ## Storage, Privacy, and Security
506
+
507
+ The data-directory resolution order is:
508
+
509
+ 1. `MEMORY_DIR`.
510
+ 2. `$OPENCODE_CONFIG_DIR/memory`.
511
+ 3. Existing legacy `~/.config/opencode/memory`.
512
+ 4. `%LOCALAPPDATA%/opencode/memory` on Windows.
513
+ 5. `$XDG_CONFIG_HOME/opencode/memory` or `~/.config/opencode/memory` elsewhere.
514
+
515
+ Important paths inside it:
516
+
517
+ ```text
518
+ global.md global Notebook facts/directives
519
+ git_<identity>.md per-project Notebook facts
520
+ config.json non-secret configuration
521
+ auth_secrets.enc encrypted cloud credentials
522
+ storage/memory.sqlite RAG, graph, identity registry, sync state
523
+ storage/blobs/ content-addressed compressed raw sources
524
+ storage/models/ cached ONNX models
525
+ exports/ snapshots/exports
526
+ ```
527
+
528
+ - No telemetry or analytics are sent.
529
+ - Model weights download from Hugging Face on first use and remain cached afterward.
530
+ - Network access is otherwise limited to explicit URL ingestion and configured Turso cloud modes.
531
+ - Snapshot path validation and local ingestion allowlists restrict arbitrary filesystem access.
532
+ - SQLite uses foreign keys, migrations, transactions, and a busy timeout for concurrent access.
533
+
534
+ ### Dependency Advisories
535
+
536
+ Spreadsheet ingestion uses SheetJS CE `0.20.3` from the official SheetJS CDN rather than the stale `xlsx@0.18.5` package in the public npm registry. This version is outside the affected ranges for the known [prototype pollution](https://github.com/advisories/GHSA-4r6h-8v6p-xvw6) and [ReDoS](https://github.com/advisories/GHSA-5pgg-2g8v-p4x9) advisories.
537
+
538
+ `npm audit` may still report the high-severity `sharp` / libvips advisory inherited through `@huggingface/transformers`. The project uses Transformers only for text feature extraction and explicitly sets `env.sharp = false`; it does not pass images through the Transformers image-decoding path. The upstream dependency currently constrains `sharp` below the patched `0.35.x` line, so this warning remains transitive until Transformers updates its dependency. Advisory: [GHSA-f88m-g3jw-g9cj](https://github.com/advisories/GHSA-f88m-g3jw-g9cj).
539
+
540
+ ---
541
+
542
+ ## Testing and Benchmarks
543
+
544
+ ```bash
545
+ npm test # unified unit, integration, and simulated-cloud suites
546
+ npm run smoke # real ONNX vectors and end-to-end memory journey
547
+ npm run test:rag # retrieval quality evaluation
548
+ npm run benchmark # full search benchmark report
549
+ npm run benchmark:table-code
550
+ ```
551
+
552
+ The fast suites use `generateEmbeddings: false` in retrieval paths for deterministic offline coverage. `npm run smoke` covers the dense-vector path with real cached/downloaded model weights and checks multilingual semantic retrieval. Both modes are needed: lexical-only tests cannot catch a broken vector serialization or ONNX execution path.
553
+
554
+ The unified suites cover fact formatting, typed directives, persona migration/synchronization, client prompt safety, Codex launcher compatibility, Git identity isolation, RAG scopes, policy expansion, RAG Memory Notes, semantic index output, raw blob portability, reverse sync, tombstones, snapshots, MCP contracts, and cloud authentication workflows.
555
+
556
+ See [`docs/BENCHMARKS.md`](./docs/BENCHMARKS.md) for methodology and detailed reports.
557
+
558
+ ### Included Search Evaluation
559
+
560
+ The stored 32-document / 21-query technical corpus produced:
561
+
562
+ | Strategy | MRR@5 | Recall@5 | NDCG@5 |
563
+ | :--- | :---: | :---: | :---: |
564
+ | BM25 lexical only | 0.6706 | 76.19% | 0.6934 |
565
+ | Dense ONNX only | 0.8135 | 100.00% | 0.8612 |
566
+ | Hybrid RRF (`k=60`) | 0.8810 | 95.24% | 0.8997 |
567
+ | **Hybrid RSF (`alpha=0.5`)** | **0.9286** | **100.00%** | **0.9473** |
568
+
569
+ ---
570
+
571
+ ## Troubleshooting
572
+
573
+ - **`No such built-in module: node:sqlite`**: install Node.js `22.5.0` or newer.
574
+ - **Codex tools are missing**: run `memory_plugin setup --codex`, then `memory_plugin doctor --codex`, and open a new Codex task.
575
+ - **OpenCode still runs old code**: restart OpenCode. For repository development, confirm `npm run dev:link` points its plugin entry to `opencode-plugin/main.js`.
576
+ - **Persona changes are not visible**: run `memory-cli sync-persona`, then start a new CLI session/task. Use `memory-cli migrate-persona --dry-run` for legacy entries.
577
+ - **Project recall is empty**: call `memory_info`; if a Git identity is `Registry: unlinked`, run `link_project_memory` or `memory-cli link --dir <repo>`.
578
+ - **A raw note/document exists only in cloud**: `manage_knowledge_base(action: "read_document")` automatically materializes and verifies its CAS blob locally when cloud credentials are available.
579
+ - **Embedding model changed**: run `reindex_knowledge_base` or use the TUI `[REINDEX]` action.
580
+
581
+ ---
582
+
583
+ ## License
584
+
585
+ [MIT](./LICENSE)