[FEATURE]: Advanced Dual-Scope Memory System for OpenCode
Feature hasn't been suggested before.
- [x] I have verified this feature I'm about to request hasn't been suggested before.
Describe the enhancement you want to request
📋 Summary
I have developed and tested a robust Memory System for OpenCode that solves the issues of context pollution, manual file management, and unsafe edits. This system introduces a Dual-Scope Architecture (Global vs. Project), ID-Based Management, and Automatic Templating.
I propose integrating this system (or similar logic) into OpenCode to transform it from a stateless CLI into a persistent, context-aware Engineer.
🛑 The Problem
Currently, managing long-term context in OpenCode is manual and fragile:
-
Global Pollution: Saving project-specific facts (e.g., "This app uses Vite") to the global
AGENTS.mdclutters the agent's brain for all future sessions. - Unsafe Edits: Asking the AI to "remove the memory about testing" often leads to fuzzy text matching that deletes the wrong lines.
-
Context Amnesia: Starting a new project requires manually teaching the agent the architecture every time, or manually creating a
./AGENTS.mdfile.
🛠️ The Solution: Dual-Scope Memory Engine
I have built a custom toolset (memory.ts) and Agent Protocol that implements the following architecture:
1. Dual-Scope Context
The system distinguishes between User Wisdom and Project Facts.
| Scope | Location | Purpose | Example |
|---|---|---|---|
| Global | ~/.config/.../AGENTS.md |
User preferences, Life lessons | "Always use Dark Mode", "I prefer concise code" |
| Project | ./AGENTS.md |
Architecture, Tech Stack, patterns | "Auth is handled by Clerk", "API is at /v1" |
Why this matters:
- Switching projects gives the Agent a "Fresh Brain" for code, but keeps the "Personality" of the user.
- No more "hallucinating" React code while working in a Vue project.
2. ID-Based Management (CRUD)
Instead of fuzzy text search, every memory is assigned a deterministic short-hash ID (e.g., a1b2).
-
Read: Returns
[ID:a1b2] - Content... -
Update:
memory_update(id='a1b2', content='...')→ Atomic, safe updates. -
Remove:
memory_remove(target='a1b2')→ 100% precision deletion.
3. Smart Auto-Templating
If a user asks to save a project fact ("Remember this stack") and no ./AGENTS.md exists, the tool automatically initializes the file with a professional template:
# 📂 Project Context: [Name]
## 🏗️ Architecture
## 📝 Code Standards
## 🧠 Project Context System (Auto-managed)
4. Protected "Safe Zones"
The tool respects a specific block in the markdown file:
<!-- MANAGED BY TOOLS - DO NOT EDIT MANUALLY -->
[... Memory Tool only touches lines here ...]
<!-- END MEMORY SYSTEM -->
This ensures the tool never overwrites the user's manual instructions or custom rules outside this block.
🧠 The Workflow
- User: "Remember this is a Next.js app."
-
Agent: Detects this is a code fact → Selects
scope='project'. -
System: Checks for
./AGENTS.md. If missing, creates it with the Template. Saves memory. -
Result: Next session, the Agent reads
./AGENTS.mdand instantly knows the stack.
🎯 Impact
This transforms OpenCode from a "Smart CLI" into a "Senior Partner" that:
- Remembers your preferences everywhere (Global).
- Understands your specific project deeply (Project).
- Manages its own knowledge base safely (ID/Protected Blocks).
I have been using this system successfully to manage complex refactors and architecture planning. I believe it would be a valuable standard feature or official plugin for the community.