opencode-working-memory 1.0.0

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.
@@ -0,0 +1,376 @@
1
+ # Configuration Guide
2
+
3
+ ## Overview
4
+
5
+ The Working Memory Plugin works out-of-the-box with sensible defaults. Advanced users can customize behavior by modifying constants in `index.ts`.
6
+
7
+ ## Core Memory Limits
8
+
9
+ ```typescript
10
+ const CORE_MEMORY_LIMITS = {
11
+ goal: 1000, // ONE specific task (not project-wide goals)
12
+ progress: 2000, // Checklist format (✅ done, ⏳ in-progress, ❌ blocked)
13
+ context: 1500, // Current working files + key patterns
14
+ };
15
+ ```
16
+
17
+ **Recommendations**:
18
+ - Keep **goal** focused on current task (clear when completed)
19
+ - Use **progress** for checklists (avoid line numbers, commit hashes, API signatures)
20
+ - Use **context** for files you're actively editing (avoid type definitions, function signatures)
21
+
22
+ ## Working Memory Configuration
23
+
24
+ ### Slot Limits
25
+
26
+ ```typescript
27
+ const SLOT_CONFIG: Record<SlotType, number> = {
28
+ error: 3, // Recent errors needing fixes
29
+ decision: 5, // Important decisions made
30
+ todo: 3, // Current task checklist
31
+ dependency: 3, // File/package dependencies
32
+ };
33
+ ```
34
+
35
+ **Tuning**:
36
+ - Increase slot limits if you need more items tracked
37
+ - Decrease for stricter memory budgets
38
+ - Total overhead: ~100-200 chars per item
39
+
40
+ ### Memory Pool Decay
41
+
42
+ ```typescript
43
+ const POOL_DECAY_GAMMA = 0.85; // Exponential decay rate (15% per event)
44
+ const POOL_MIN_SCORE = 0.01; // Items below this score are pruned
45
+ ```
46
+
47
+ **Formula**: `score = exp(-γ * age) + mentionCount`
48
+
49
+ **Tuning**:
50
+ - Lower `γ` (e.g., 0.75) → faster decay, more aggressive pruning
51
+ - Higher `γ` (e.g., 0.90) → slower decay, items stay longer
52
+ - Lower `POOL_MIN_SCORE` (e.g., 0.005) → more items retained
53
+
54
+ ### Pool Size Limits
55
+
56
+ ```typescript
57
+ const POOL_MAX_ITEMS = 50; // Hard limit on pool size
58
+ ```
59
+
60
+ **Tuning**:
61
+ - Increase for longer sessions with more context
62
+ - Decrease for stricter memory budgets
63
+ - Each item adds ~50-150 chars to system prompt
64
+
65
+ ## Pressure Monitoring
66
+
67
+ ### Thresholds
68
+
69
+ ```typescript
70
+ const PRESSURE_THRESHOLDS = {
71
+ moderate: 70, // Warning appears in system prompt
72
+ high: 85, // Aggressive pruning activates
73
+ critical: 95, // Intervention sent to agent
74
+ };
75
+ ```
76
+
77
+ **Tuning**:
78
+ - Increase thresholds for more relaxed monitoring
79
+ - Decrease for earlier warnings and interventions
80
+
81
+ ### Context Limit Estimate
82
+
83
+ ```typescript
84
+ const ESTIMATED_CONTEXT_LIMIT = 180000; // Conservative estimate (chars)
85
+ ```
86
+
87
+ **Note**: OpenCode actual limit varies by model. Adjust based on your observations.
88
+
89
+ ## Smart Pruning
90
+
91
+ ### Line Thresholds
92
+
93
+ ```typescript
94
+ // Normal mode (pressure < 75%)
95
+ const PRUNE_THRESHOLD_NORMAL = 50;
96
+
97
+ // Aggressive mode (75% ≤ pressure < 90%)
98
+ const PRUNE_THRESHOLD_AGGRESSIVE = 30;
99
+
100
+ // Hyper-aggressive mode (pressure ≥ 90%)
101
+ const PRUNE_THRESHOLD_HYPER = 15;
102
+ ```
103
+
104
+ **Tuning**:
105
+ - Increase thresholds to keep more tool output
106
+ - Decrease for more aggressive pruning
107
+
108
+ ### Keep Lines
109
+
110
+ ```typescript
111
+ // Normal mode
112
+ const KEEP_LINES_NORMAL = 30; // Keep first/last 30 lines
113
+
114
+ // Aggressive mode
115
+ const KEEP_LINES_AGGRESSIVE = 20; // Keep first/last 20 lines
116
+
117
+ // Hyper-aggressive mode
118
+ const KEEP_LINES_HYPER = 10; // Keep first/last 10 lines
119
+ ```
120
+
121
+ **Tuning**:
122
+ - Increase to preserve more context from tool outputs
123
+ - Decrease for stricter truncation
124
+
125
+ ## Storage Governance
126
+
127
+ ### Session Cleanup
128
+
129
+ Automatically triggered on `experimental.session.deleted` hook. No configuration needed.
130
+
131
+ ### Tool Output Cache Sweep
132
+
133
+ ```typescript
134
+ const SWEEP_INTERVAL = 500; // Trigger every N events
135
+ const SWEEP_MAX_FILES = 300; // Keep most recent N files
136
+ const SWEEP_TTL_DAYS = 7; // Delete files older than N days
137
+ ```
138
+
139
+ **Tuning**:
140
+ - Increase `SWEEP_INTERVAL` for less frequent sweeps (lower overhead)
141
+ - Increase `SWEEP_MAX_FILES` to cache more tool outputs (more disk usage)
142
+ - Increase `SWEEP_TTL_DAYS` to keep older files longer
143
+
144
+ ## Compaction Behavior
145
+
146
+ ### Item Preservation
147
+
148
+ ```typescript
149
+ const COMPACTION_KEEP_ITEMS = 10; // Preserve N most recent items on compaction
150
+ ```
151
+
152
+ **Tuning**:
153
+ - Increase to preserve more working memory across compactions
154
+ - Decrease for stricter memory reset on compaction
155
+
156
+ ## System Prompt Injection
157
+
158
+ ### Core Memory Format
159
+
160
+ ```typescript
161
+ // Injected as:
162
+ <core_memory>
163
+ <goal chars="87/1000">...</goal>
164
+ <progress chars="560/2000">...</progress>
165
+ <context chars="479/1500">...</context>
166
+ </core_memory>
167
+ ```
168
+
169
+ **Customization**: Modify `formatCoreMemoryForPrompt()` in `index.ts` to change format.
170
+
171
+ ### Working Memory Format
172
+
173
+ ```typescript
174
+ // Injected as:
175
+ <working_memory>
176
+ Recent session context (auto-managed, sorted by relevance):
177
+
178
+ ⚠️ Errors:
179
+ - item content
180
+
181
+ 📁 Key Files:
182
+ - file path
183
+
184
+ (N items shown, updated: HH:MM:SS AM)
185
+ </working_memory>
186
+ ```
187
+
188
+ **Customization**: Modify `formatWorkingMemoryForPrompt()` in `index.ts` to change:
189
+ - Section emoji/icons
190
+ - Display format
191
+ - Item ordering
192
+
193
+ ### Pressure Warning Format
194
+
195
+ ```typescript
196
+ // Injected as:
197
+ [Memory Pressure: 87% (high) - 156,600/180,000 chars]
198
+ ⚠️ High memory pressure detected. Consider:
199
+ - Action item 1
200
+ - Action item 2
201
+ ```
202
+
203
+ **Customization**: Modify `formatPressureWarning()` in `index.ts`.
204
+
205
+ ## Auto-Extraction Heuristics
206
+
207
+ ### File Path Detection
208
+
209
+ ```typescript
210
+ // Detects:
211
+ - Absolute paths: /users/name/project/file.ts
212
+ - Relative paths: src/components/Button.tsx
213
+ - Dot paths: ./utils/helpers.ts
214
+ - Tilde paths: ~/project/file.ts
215
+ ```
216
+
217
+ **Customization**: Modify regex in `extractFilePaths()`.
218
+
219
+ ### Error Detection
220
+
221
+ ```typescript
222
+ // Detects:
223
+ - "Error:", "ERROR:", "error:"
224
+ - Stack traces with "at " prefix
225
+ - TypeScript errors with "TS####:"
226
+ ```
227
+
228
+ **Customization**: Modify `extractErrors()` heuristics.
229
+
230
+ ### Decision Detection
231
+
232
+ ```typescript
233
+ // Detects:
234
+ - "decided to...", "decision:", "chose to..."
235
+ - "using X instead of Y"
236
+ - "will use X approach"
237
+ ```
238
+
239
+ **Customization**: Modify `extractDecisions()` heuristics.
240
+
241
+ ## Environment Variables
242
+
243
+ Currently, the plugin does not support environment variables. All configuration is done via constants in `index.ts`.
244
+
245
+ **Future Enhancement**: Consider adding `.env` support for:
246
+ ```
247
+ OPENCODE_WM_CORE_GOAL_LIMIT=1000
248
+ OPENCODE_WM_POOL_DECAY_GAMMA=0.85
249
+ OPENCODE_WM_SWEEP_INTERVAL=500
250
+ ```
251
+
252
+ ## Performance Tuning
253
+
254
+ ### High-Frequency Sessions (500+ messages)
255
+
256
+ ```typescript
257
+ // Aggressive pruning
258
+ const PRUNE_THRESHOLD_NORMAL = 30;
259
+ const PRUNE_THRESHOLD_AGGRESSIVE = 20;
260
+
261
+ // Faster decay
262
+ const POOL_DECAY_GAMMA = 0.75;
263
+
264
+ // More frequent sweeps
265
+ const SWEEP_INTERVAL = 250;
266
+ ```
267
+
268
+ ### Long-Running Sessions (Multi-day)
269
+
270
+ ```typescript
271
+ // Preserve more context
272
+ const POOL_MAX_ITEMS = 100;
273
+ const COMPACTION_KEEP_ITEMS = 20;
274
+
275
+ // Slower decay
276
+ const POOL_DECAY_GAMMA = 0.90;
277
+
278
+ // Longer TTL
279
+ const SWEEP_TTL_DAYS = 14;
280
+ ```
281
+
282
+ ### Memory-Constrained Environments
283
+
284
+ ```typescript
285
+ // Strict limits
286
+ const CORE_MEMORY_LIMITS = {
287
+ goal: 500,
288
+ progress: 1000,
289
+ context: 800,
290
+ };
291
+
292
+ const POOL_MAX_ITEMS = 20;
293
+
294
+ // Aggressive pruning
295
+ const PRUNE_THRESHOLD_NORMAL = 20;
296
+ ```
297
+
298
+ ## Debugging Configuration
299
+
300
+ ### Enable Verbose Logging
301
+
302
+ Add `console.log()` statements in key functions:
303
+
304
+ ```typescript
305
+ // In loadCoreMemory()
306
+ console.log("[Core Memory] Loaded:", memory);
307
+
308
+ // In applyDecay()
309
+ console.log("[Pool Decay] Pruned items:", prunedCount);
310
+
311
+ // In sweepToolOutputCache()
312
+ console.log("[Sweep] Deleted files:", deletedCount);
313
+ ```
314
+
315
+ ### Inspect Memory Files
316
+
317
+ ```bash
318
+ # Core memory
319
+ cat .opencode/memory-core/<sessionID>.json | jq
320
+
321
+ # Working memory
322
+ cat .opencode/memory-working/<sessionID>.json | jq
323
+
324
+ # Pressure state
325
+ cat .opencode/memory-working/<sessionID>_pressure.json | jq
326
+
327
+ # Sweep log
328
+ cat .opencode/memory-working/<sessionID>_sweep.json | jq
329
+ ```
330
+
331
+ ## Migration Notes
332
+
333
+ ### Upgrading from Pre-Phase 3
334
+
335
+ Old format files are automatically migrated:
336
+
337
+ ```typescript
338
+ // Old format
339
+ { items: Array<Item> }
340
+
341
+ // New format (auto-migrated)
342
+ { slots: { error: [], decision: [], ... }, pool: [...] }
343
+ ```
344
+
345
+ No manual intervention required.
346
+
347
+ ### Upgrading from Phase 3 to Phase 4.5
348
+
349
+ Storage governance is backward compatible. No migration needed.
350
+
351
+ ## Best Practices
352
+
353
+ 1. **Core Memory Discipline**:
354
+ - Clear `goal` immediately after task completion
355
+ - Keep `progress` concise (use checklist format)
356
+ - Only put actively edited files in `context`
357
+
358
+ 2. **Working Memory Hygiene**:
359
+ - Clear `error` slot after fixing all errors (`working_memory_clear_slot`)
360
+ - Let pool decay naturally (avoid manual removal unless necessary)
361
+ - Review working memory periodically (use `working_memory_read`)
362
+
363
+ 3. **Pressure Management**:
364
+ - Respond to "moderate" warnings proactively
365
+ - Compress core memory at "high" pressure
366
+ - Clear working memory at "critical" pressure
367
+
368
+ 4. **Storage Maintenance**:
369
+ - Let sweep run automatically (no manual intervention)
370
+ - Delete old session files manually if needed
371
+ - Monitor `.opencode/` directory size periodically
372
+
373
+ ---
374
+
375
+ **Last Updated**: February 2026
376
+ **Configuration File**: `index.ts` (constants section)
@@ -0,0 +1,131 @@
1
+ # Installation Guide
2
+
3
+ ## Prerequisites
4
+
5
+ - **OpenCode** 1.0.0 or higher
6
+ - **Node.js** 18+ (for development only)
7
+
8
+ ## Quick Install (For Users)
9
+
10
+ ### Option 1: Install from npm (Recommended)
11
+
12
+ ```bash
13
+ npm install opencode-working-memory
14
+ ```
15
+
16
+ Then add to your `.opencode/package.json`:
17
+
18
+ ```json
19
+ {
20
+ "plugins": [
21
+ "opencode-working-memory"
22
+ ]
23
+ }
24
+ ```
25
+
26
+ ### Option 2: Install from GitHub
27
+
28
+ Add to your `.opencode/package.json`:
29
+
30
+ ```json
31
+ {
32
+ "dependencies": {
33
+ "opencode-working-memory": "github:sdwolf4103/opencode-working-memory"
34
+ },
35
+ "plugins": [
36
+ "opencode-working-memory"
37
+ ]
38
+ }
39
+ ```
40
+
41
+ Then run:
42
+
43
+ ```bash
44
+ cd .opencode
45
+ npm install
46
+ ```
47
+
48
+ ### Option 3: Local Development Install
49
+
50
+ Clone the repository:
51
+
52
+ ```bash
53
+ git clone https://github.com/sdwolf4103/opencode-working-memory.git
54
+ cd opencode-working-memory
55
+ npm install
56
+ ```
57
+
58
+ Link to your OpenCode project:
59
+
60
+ ```bash
61
+ cd /path/to/your/project/.opencode
62
+ npm link /path/to/opencode-working-memory
63
+ ```
64
+
65
+ Add to `.opencode/package.json`:
66
+
67
+ ```json
68
+ {
69
+ "plugins": [
70
+ "opencode-working-memory"
71
+ ]
72
+ }
73
+ ```
74
+
75
+ ## Verification
76
+
77
+ After installation, start an OpenCode session and run:
78
+
79
+ ```
80
+ core_memory_update goal "Test installation"
81
+ ```
82
+
83
+ You should see a success message. Check `.opencode/memory-core/` for the session file.
84
+
85
+ ## Configuration
86
+
87
+ The plugin works out-of-the-box with sensible defaults. For advanced configuration, see [configuration.md](./configuration.md).
88
+
89
+ ## Troubleshooting
90
+
91
+ ### Plugin Not Loading
92
+
93
+ **Symptom**: No `core_memory_update` tool available
94
+
95
+ **Solution**:
96
+ 1. Check `.opencode/package.json` includes plugin in `"plugins": []` array
97
+ 2. Verify `npm install` completed successfully
98
+ 3. Restart OpenCode session
99
+
100
+ ### Memory Files Not Created
101
+
102
+ **Symptom**: No `.opencode/memory-core/` or `.opencode/memory-working/` directories
103
+
104
+ **Solution**:
105
+ 1. Ensure OpenCode has write permissions in project directory
106
+ 2. Check plugin hooks are registered (look for "Working Memory Plugin" in session logs)
107
+ 3. Trigger memory operations (e.g., use `core_memory_update` tool)
108
+
109
+ ### Type Errors During Development
110
+
111
+ **Symptom**: TypeScript errors when modifying plugin
112
+
113
+ **Solution**:
114
+ 1. Ensure `@opencode-ai/plugin` is installed: `npm install @opencode-ai/plugin`
115
+ 2. Run type checking: `npx tsc --noEmit`
116
+ 3. See [AGENTS.md](../AGENTS.md) for code style guidelines
117
+
118
+ ## Uninstallation
119
+
120
+ ```bash
121
+ cd .opencode
122
+ npm uninstall opencode-working-memory
123
+ ```
124
+
125
+ Remove from `.opencode/package.json` plugins array. Memory files in `.opencode/memory-*` will persist unless manually deleted.
126
+
127
+ ## Next Steps
128
+
129
+ - Read [Architecture Documentation](./architecture.md) to understand how memory tiers work
130
+ - See [Configuration Guide](./configuration.md) for customization options
131
+ - Check [AGENTS.md](../AGENTS.md) for development guidelines