OpenCode Working Memory implements a three-layer memory architecture designed to preserve context across OpenCode session compactions.
┌─────────────────────────────────────────────────────────────┐
│ LAYER 1: WORKSPACE MEMORY (Long-term, cross-session) │
│ • Persistent storage: ~/.local/share/opencode-working-... │
│ • Types: feedback | project | decision | reference │
│ • Sources: explicit | compaction | manual │
│ • Render limits: 3600 chars / 28 entries │
│ • Survives: session reset, compaction (same workspace) │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ LAYER 2: HOT SESSION STATE (Short-term, per-session) │
│ • Session-scoped tracking: active files, open errors │
│ • Storage: sessions/{sessionID}.json │
│ • Auto-extracted from tool usage patterns │
│ • Cleared: on new session start │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ LAYER 3: NATIVE OPENCODE STATE │
│ • Uses OpenCode's built-in todos during compaction │
│ • No additional storage required │
│ • Delegated to OpenCode's native features │
└─────────────────────────────────────────────────────────────┘
Long-term memory that persists across sessions within the same workspace. Perfect for:
- Project conventions and patterns
- Important decisions that span sessions
- User preferences for this codebase
- Location:
~/.local/share/opencode-working-memory/workspaces/{workspaceKey}/workspace-memory.json - Workspace Key: First 16 chars of
sha256(realpath(workspaceRoot)) - Schema:
{ version: 1, workspace: { root: string, key: string }, limits: { maxRenderedChars: 3600, maxEntries: 28 }, entries: LongTermMemoryEntry[], lastActivityAt?: string, updatedAt: string }
| Type | Purpose | Example |
|---|---|---|
feedback |
User preferences | "User prefers functional React components" |
project |
Project-level info | "This monorepo uses turborepo" |
decision |
Important decisions | "Use PostgreSQL for primary database" |
reference |
Key references | "API endpoints defined in src/api/" |
| Source | Confidence | How Added |
|---|---|---|
explicit |
1.0 | User said "remember this" |
compaction |
0.75 | Extracted during compaction |
manual |
varies | Programmatically added |
During compaction, OpenCode Working Memory scans for Memory candidates: sections:
Memory candidates:
- [decision] Use npm cache for plugin loading
- [project] This repo uses TypeScript with strict mode
Legacy Format: OpenCode Working Memory also accepts <workspace_memory_candidates> XML blocks for backward compatibility, but this format is deprecated.
Quality Gate: Not all candidates become memories. OpenCode Working Memory rejects:
- Git commit hashes (e.g.,
abc1234) - Raw errors (e.g.,
Error: something failed) - Stack traces
- Path-heavy facts (>50% paths)
- Very short text (<20 chars)
Memories are deduplicated and consolidated with accounting:
- Normalize exact text: lowercase, strip punctuation, collapse whitespace.
- Group project/reference entries by identity where possible.
- Keep decision and feedback entries on exact canonical matching to avoid broad semantic merges.
- Keep the best surviving entry by source, confidence, specificity, and freshness tie-breakers.
- Emit accounting events so pending memories can be classified as promoted, absorbed, superseded, or rejected.
This prevents absorbed or superseded pending memories from retrying forever while still preserving the active surviving memory.
Retention then decides which active memories are rendered into prompt context. It does not hard-delete old memories by age.
strength = initialStrength * 2 ** (-effectiveAgeDays / effectiveHalfLifeDays)Initial strength is based on memory type, source, and optional user importance. Confidence remains stored for compatibility but is not part of retention scoring.
Rendered candidates are selected in this order:
- Exclude
status: "superseded"entries. - Compute current retention strength.
- Sort by strength descending.
- Apply per-type caps.
- Keep the top 28 rendered entries under the workspace memory character budget.
Default type caps:
| Type | Rendered cap |
|---|---|
feedback |
10 |
decision |
10 |
project |
8 |
reference |
6 |
The type-cap total is 34, intentionally above the global 28-entry cap. These are maximums, not quotas.
Dormant workspaces age more slowly: after 14 inactive days, additional dormant time counts at 0.25x for retention decay. Repeated duplicate memories reinforce the surviving entry and slow future decay, but same-session and under-one-hour repeats do not stack reinforcement.
The safetyCritical field on LongTermMemoryEntry is deprecated as of the retention v1.5.1 model update. It no longer affects retention strength or type-cap bypass. The field is preserved in the type definition for backward compatibility with existing workspace memory JSON files, but has no active behavior. Safety rules should be maintained in user-controlled files such as agent.md rather than in system memory.
Workspace memory is injected at the top of every message:
Workspace memory (cross-session, verify if stale):
decision:
- Use npm cache for plugin loading, not npm link
project:
- This repo uses the opencode-agenthub plugin system
reference:
- Storage: ~/.local/share/opencode-working-memory/...
Track current session context automatically:
- What files are you working on?
- What errors are currently open?
- What decisions were made recently?
- Location:
~/.local/share/opencode-working-memory/workspaces/{workspaceKey}/sessions/{hashedSessionID}.json - Schema:
{ version: 1, sessionID: string, turn: number, updatedAt: string, activeFiles: ActiveFile[], openErrors: OpenError[], recentDecisions: SessionDecision[] }
Automatically tracked from tool.execute.after events:
| Action | Weight |
|---|---|
edit |
50 |
write |
45 |
grep |
30 |
read |
20 |
Files are ranked by: ACTION_WEIGHT[action] + count * 3
Tracked from tool.execute.after events when exitCode !== 0:
| Category | Trigger Pattern |
|---|---|
typecheck |
TS####: or TypeScript errors |
test |
Test failures |
lint |
ESLint warnings/errors |
build |
Build failures |
runtime |
Error:, TypeError:, etc. |
False Positive Guards:
- Commands like
git log,catwith "error" in output are ignored - Only actual command failures (
exitCode !== 0) trigger errors exitCode === undefinedis ignored (no error created, no error cleared)
Errors are fingerprinted by:
- Extract error message summary
- Generate fingerprint:
first 12 chars of sha256(summary) - Group similar errors by fingerprint
Short-term decisions made this session. Candidates for promotion to workspace memory during compaction.
Hot session state is injected after workspace memory:
---
Hot session state (current session):
active_files:
- src/plugin.ts (edit, 18x)
- tests/plugin.test.ts (edit, 5x)
open_errors: (none)
recent_decisions:
- Use frozen workspace memory snapshots for cache stability
pending_memories:
- [decision] Parser supports 3 candidate formats
Delegate task tracking to OpenCode's native features.
- Uses OpenCode's built-in
todosduring compaction - No additional storage or injection required
- Allows the agent to manage task lists natively
OpenCode Working Memory hooks into OpenCode lifecycle events:
Injects workspace memory and hot session state into system prompt.
- Tracks active files (read, grep, edit, write actions)
- Tracks open errors from failed commands
- Clears errors when commands succeed
- Ignores
exitCode === undefined(successful commands without explicit exit codes)
Extracts workspace memory candidates from conversation. Applies quality gate, redaction, migration, consolidation accounting, deduplication, and source priority.
session.compacted: Promote session decisions to workspace memorysession.deleted: Clean up session state files
Promotion uses accounting results from workspace memory normalization. Pending memories that are kept are promoted; duplicate memories are absorbed; exact decision replacements can be superseded; over-capacity compaction memories are rejected. Stale-marked memories are not hard-pruned by age; they lose rendered space through retention strength and cap competition.
// Bad: Would create false positive
"Error: something failed" in output
// Good: Actually failed
exitCode === 1 && output.includes("Error")
// Good: Actually succeeded
exitCode === 0 (clears errors for that category)
// Good: Ignore ambiguous cases
exitCode === undefined → skip error tracking// Correctly interpreted
"don't remember this" → NOT added to memory
"不要記住這個" → NOT added to memory
"remember this" → added to memory candidates// Same memory (after normalization)
"Use npm cache for plugins"
"USE NPM CACHE for plugins!!"
"use npm cache for plugins."
// All map to same canonical key
canonical("Use npm cache for plugins") === "use npm cache for plugins"// Rejected (not valuable as long-term memory)
"4832b38 fix: something" // git hash
"Error: something failed" // raw error
"at Object.method (file.ts:42)" // stack trace
"/Users/x/project/file.ts /Users/x/project/other.ts" // path-heavy
// Accepted
"[decision] Use npm cache for plugin loading" // good pattern~/.local/share/opencode-working-memory/
└── workspaces/
└── {workspaceKey}/
├── workspace-memory.json # Long-term memory
└── sessions/
└── {hashedSessionID}.json # Session state
// First 16 chars of SHA-256 hash of workspace root realpath
const workspaceKey = sha256(realpath(workspaceRoot)).slice(0, 16)All read-modify-write updates go through updateJSON(), which combines an in-process promise queue with an on-disk .lock file. The lock file uses exclusive creation, heartbeat refreshes while held, stale-lock recovery after 30 seconds, and a 5 second wait timeout for live contention. Direct readJSON() is fallback-oriented and does not mutate data except when corrupt JSON is quarantined.
| Layer | Max Chars | Max Entries |
|---|---|---|
| Workspace Memory | 3600 | 28 |
| Hot Session State | 700 | 8 files, 3 errors |
- Workspace memory: usually under ~2000 chars in observed rendered output
- Hot session state: usually under ~500 chars in observed rendered output
- Total: typically well below the configured maximums
- Workspace memory: ~2-5 KB per workspace
- Session state: ~1-3 KB per session
- Auto-cleanup on workspace/session deletion
Add new types in src/types.ts:
export type LongTermType = "feedback" | "project" | "decision" | "reference" | "custom";Add new categories in src/types.ts:
export type ErrorCategory = "typecheck" | "test" | "lint" | "build" | "runtime" | "custom";Modify src/extractors.ts to add new extraction patterns.
OpenCode Working Memory automatically migrates old format files to the new three-layer architecture. No manual intervention needed.
Last Updated: April 2026
Implementation: src/plugin.ts, src/extractors.ts, src/workspace-memory.ts, src/session-state.ts