@morningljn/mnemo 0.1.4 → 0.2.1

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.
Files changed (50) hide show
  1. package/dist/dream.d.ts +2 -0
  2. package/dist/dream.js +20 -0
  3. package/dist/dream.js.map +1 -0
  4. package/dist/init.js +4 -24
  5. package/dist/init.js.map +1 -1
  6. package/dist/resources.d.ts +22 -8
  7. package/dist/resources.js +66 -20
  8. package/dist/resources.js.map +1 -1
  9. package/dist/retriever.js +42 -47
  10. package/dist/retriever.js.map +1 -1
  11. package/dist/schema.d.ts +1 -1
  12. package/dist/schema.js +21 -10
  13. package/dist/schema.js.map +1 -1
  14. package/dist/server.js +73 -6
  15. package/dist/server.js.map +1 -1
  16. package/dist/store.d.ts +59 -1
  17. package/dist/store.js +308 -10
  18. package/dist/store.js.map +1 -1
  19. package/dist/types.d.ts +30 -1
  20. package/docs/superpowers/plans/2026-05-16-memory-dreaming.md +626 -0
  21. package/docs/superpowers/plans/2026-05-16-memory-self-learning.md +932 -0
  22. package/openspec/changes/archive/2026-05-16-memory-dreaming/.openspec.yaml +2 -0
  23. package/openspec/changes/archive/2026-05-16-memory-dreaming/design.md +71 -0
  24. package/openspec/changes/archive/2026-05-16-memory-dreaming/proposal.md +32 -0
  25. package/openspec/changes/archive/2026-05-16-memory-dreaming/specs/compact-search/spec.md +16 -0
  26. package/openspec/changes/archive/2026-05-16-memory-dreaming/specs/dream-cycle/spec.md +38 -0
  27. package/openspec/changes/archive/2026-05-16-memory-dreaming/tasks.md +27 -0
  28. package/openspec/changes/memory-self-learning/.openspec.yaml +2 -0
  29. package/openspec/changes/memory-self-learning/design.md +174 -0
  30. package/openspec/changes/memory-self-learning/proposal.md +35 -0
  31. package/openspec/changes/memory-self-learning/specs/fact-retrieval/spec.md +35 -0
  32. package/openspec/changes/memory-self-learning/specs/fact-summary/spec.md +45 -0
  33. package/openspec/changes/memory-self-learning/specs/length-penalty/spec.md +27 -0
  34. package/openspec/changes/memory-self-learning/specs/retrieval-log/spec.md +41 -0
  35. package/openspec/changes/memory-self-learning/specs/self-learning/spec.md +68 -0
  36. package/openspec/changes/memory-self-learning/tasks.md +56 -0
  37. package/openspec/specs/compact-search/spec.md +16 -0
  38. package/openspec/specs/dream-cycle/spec.md +38 -0
  39. package/package.json +3 -2
  40. package/src/dream.ts +20 -0
  41. package/src/init.ts +4 -24
  42. package/src/resources.ts +77 -21
  43. package/src/retriever.ts +41 -49
  44. package/src/schema.ts +21 -10
  45. package/src/server.ts +81 -7
  46. package/src/store.ts +378 -11
  47. package/src/types.ts +28 -1
  48. package/tests/resource.test.ts +25 -23
  49. package/tests/retriever.test.ts +53 -0
  50. package/tests/store.test.ts +239 -0
@@ -21,42 +21,44 @@ afterEach(() => {
21
21
  })
22
22
 
23
23
  describe('ResourceManager', () => {
24
- it('returns empty array for empty category', () => {
25
- const facts = manager.getFacts('identity')
26
- expect(facts).toEqual([])
24
+ it('returns empty markdown for empty category', () => {
25
+ const text = readResource('identity')
26
+ expect(text).toContain('身份与行为设定')
27
+ expect(text).not.toContain('## 你的身份')
27
28
  })
28
29
 
29
- it('returns facts ordered by trust score', () => {
30
+ it('formats identity facts as instructions', () => {
31
+ store.addFact('AI角色设定:大名暖暖,身份是用户的编程助手', 'identity')
32
+ const text = readResource('identity')
33
+ expect(text).toContain('你的身份')
34
+ expect(text).toContain('暖暖')
35
+ })
36
+
37
+ it('formats non-identity facts as reference', () => {
30
38
  store.addFact('用户偏好深色主题', 'tool_pref')
31
- store.addFact('用户喜欢 VS Code', 'tool_pref')
32
- const facts = manager.getFacts('tool_pref')
33
- expect(facts.length).toBe(2)
34
- // listFacts returns trust_score DESC, first fact added gets default trust
35
- expect(facts.length).toBeGreaterThan(0)
39
+ const text = readResource('tool_pref')
40
+ expect(text).toContain('工具偏好')
41
+ expect(text).toContain('深色主题')
36
42
  })
37
43
 
38
44
  it('caches results', () => {
39
45
  store.addFact('测试事实', 'general')
40
- manager.getFacts('general')
46
+ readResource('general')
47
+ expect(manager.cacheSize()).toBe(1)
48
+ readResource('general')
41
49
  expect(manager.cacheSize()).toBe(1)
42
- // Second call should hit cache
43
- const facts2 = manager.getFacts('general')
44
- expect(facts2.length).toBe(1)
45
50
  })
46
51
 
47
- it('invalidates cache on write', () => {
52
+ it('invalidates cache', () => {
48
53
  store.addFact('测试事实', 'general')
49
- manager.getFacts('general')
54
+ readResource('general')
50
55
  expect(manager.cacheSize()).toBe(1)
51
56
  manager.invalidate()
52
57
  expect(manager.cacheSize()).toBe(0)
53
58
  })
54
-
55
- it('limits to top 10 facts', () => {
56
- for (let i = 0; i < 15; i++) {
57
- store.addFact(`事实 ${i}`, 'general')
58
- }
59
- const facts = manager.getFacts('general')
60
- expect(facts.length).toBe(10)
61
- })
62
59
  })
60
+
61
+ function readResource(category: string): string {
62
+ const result = (manager as any).readCategory(category)
63
+ return result.contents[0].text
64
+ }
@@ -53,3 +53,56 @@ describe('FactRetriever', () => {
53
53
  expect(results.length).toBeGreaterThan(0)
54
54
  })
55
55
  })
56
+
57
+ describe('static weights (no dynamic)', () => {
58
+ it('uses same weights for short and long queries', () => {
59
+ store.addFact('用户偏好 VS Code 编辑器', 'tool_pref')
60
+ const shortResults = retriever.search('VS Code')
61
+ const longResults = retriever.search('为什么 VS Code 编辑器总是报错说找不到模块')
62
+ // Both should return the same fact — static weights don't change by query length
63
+ expect(shortResults.some(r => r.content.includes('VS Code'))).toBe(true)
64
+ expect(longResults.some(r => r.content.includes('VS Code'))).toBe(true)
65
+ })
66
+ })
67
+
68
+ describe('length penalty', () => {
69
+ it('penalizes long facts without summary', () => {
70
+ const longContent = '用户偏好 ' + '详细说明'.repeat(200) // ~800 chars
71
+ store.addFact(longContent, 'tool_pref')
72
+ store.addFact('用户偏好 VS Code', 'tool_pref')
73
+ const results = retriever.search('用户偏好')
74
+ // Short fact should rank higher
75
+ if (results.length >= 2) {
76
+ const shortFact = results.find(r => r.content === '用户偏好 VS Code')
77
+ const longFact = results.find(r => r.content.length > 500)
78
+ if (shortFact && longFact) {
79
+ expect(shortFact.score).toBeGreaterThan(longFact.score)
80
+ }
81
+ }
82
+ })
83
+
84
+ it('does not penalize long facts with short summary', () => {
85
+ // 长内容包含搜索词(确保 FTS5 能匹配),同时设置短 summary
86
+ const longContent = '用户偏好的详细内容' + '补充说明'.repeat(200)
87
+ const id = store.addFact(longContent, 'general')
88
+ store.connection.prepare('UPDATE facts SET summary = ? WHERE fact_id = ?').run('用户偏好', id)
89
+ // 短事实也包含搜索词,作为对比
90
+ store.addFact('用户偏好 VS Code', 'tool_pref')
91
+ const results = retriever.search('用户偏好')
92
+ const summaryFact = results.find(r => r.factId === id)
93
+ // 有 summary 的事实应该被找到(不会被 length penalty 过度惩罚)
94
+ expect(summaryFact).toBeTruthy()
95
+ // summary 事实的 score 应该合理(不会被 content 长度拖累)
96
+ if (summaryFact) {
97
+ expect(summaryFact.score).toBeGreaterThan(0)
98
+ }
99
+ })
100
+ })
101
+
102
+ describe('no relevance gate', () => {
103
+ it('returns results even with low scores', () => {
104
+ store.addFact('完全不相关关于天气', 'general')
105
+ const results = retriever.search('天气')
106
+ expect(results.length).toBeGreaterThanOrEqual(1)
107
+ })
108
+ })
@@ -102,3 +102,242 @@ describe('MemoryStore', () => {
102
102
  })
103
103
  })
104
104
  })
105
+
106
+ describe('schema migration', () => {
107
+ it('has summary column on facts table', () => {
108
+ const cols = store.connection.pragma('table_info(facts)') as Array<{ name: string }>
109
+ const colNames = cols.map(c => c.name)
110
+ expect(colNames).toContain('summary')
111
+ expect(colNames).toContain('last_retrieved_at')
112
+ })
113
+
114
+ it('has retrieval_log table', () => {
115
+ const tables = store.connection.prepare(
116
+ "SELECT name FROM sqlite_master WHERE type='table' AND name='retrieval_log'"
117
+ ).get()
118
+ expect(tables).toBeTruthy()
119
+ })
120
+
121
+ it('summary defaults to null', () => {
122
+ const id = store.addFact('test summary fact', 'general')
123
+ const row = store.connection.prepare('SELECT summary FROM facts WHERE fact_id = ?').get(id) as { summary: string | null }
124
+ expect(row.summary).toBeNull()
125
+ })
126
+ })
127
+
128
+ describe('logRetrieval', () => {
129
+ it('writes retrieval log with results', () => {
130
+ const id = store.addFact('test fact for logging', 'general')
131
+ store.logRetrieval('test query', [{ id, score: 0.8 }])
132
+ const rows = store.connection.prepare('SELECT * FROM retrieval_log').all() as Array<any>
133
+ expect(rows.length).toBe(1)
134
+ expect(rows[0].query).toBe('test query')
135
+ expect(JSON.parse(rows[0].results)).toEqual([{ id, score: 0.8 }])
136
+ })
137
+
138
+ it('updates last_retrieved_at for returned facts', () => {
139
+ const id = store.addFact('test fact for timestamp', 'general')
140
+ store.logRetrieval('query', [{ id, score: 0.5 }])
141
+ const row = store.connection.prepare('SELECT last_retrieved_at FROM facts WHERE fact_id = ?').get(id) as any
142
+ expect(row.last_retrieved_at).not.toBeNull()
143
+ })
144
+
145
+ it('prunes log to max entries', () => {
146
+ for (let i = 0; i < 12; i++) {
147
+ store.logRetrieval(`query ${i}`, [])
148
+ }
149
+ store.pruneRetrievalLog(10)
150
+ const count = (store.connection.prepare('SELECT COUNT(*) as c FROM retrieval_log').get() as any).c
151
+ expect(count).toBe(10)
152
+ })
153
+ })
154
+
155
+ describe('runLearning', () => {
156
+ it('demotes moderate retrieval low helpful facts', () => {
157
+ const id = store.addFact('demote me', 'general')
158
+ store.connection.prepare('UPDATE facts SET retrieval_count = 50, helpful_count = 1, trust_score = 1.0 WHERE fact_id = ?').run(id)
159
+ const result = store.runLearning()
160
+ const row = store.connection.prepare('SELECT trust_score FROM facts WHERE fact_id = ?').get(id) as any
161
+ expect(row.trust_score).toBeLessThan(1.0)
162
+ expect(result.demoted).toBeGreaterThanOrEqual(1)
163
+ })
164
+
165
+ it('protects high frequency facts from demotion', () => {
166
+ const id = store.addFact('高频角色设定', 'identity')
167
+ store.connection.prepare('UPDATE facts SET retrieval_count = 200, helpful_count = 2, trust_score = 0.9 WHERE fact_id = ?').run(id)
168
+ store.runLearning()
169
+ const row = store.connection.prepare('SELECT trust_score FROM facts WHERE fact_id = ?').get(id) as any
170
+ expect(row.trust_score).toBe(0.9)
171
+ })
172
+
173
+ it('still demotes moderate frequency facts with low trust', () => {
174
+ const id = store.addFact('中频低信任', 'general')
175
+ store.connection.prepare('UPDATE facts SET retrieval_count = 50, helpful_count = 0, trust_score = 0.3 WHERE fact_id = ?').run(id)
176
+ store.runLearning()
177
+ const row = store.connection.prepare('SELECT trust_score FROM facts WHERE fact_id = ?').get(id) as any
178
+ expect(row.trust_score).toBeLessThan(0.3)
179
+ })
180
+
181
+ it('promotes high helpful rate facts', () => {
182
+ const id = store.addFact('promote me', 'general')
183
+ store.connection.prepare('UPDATE facts SET retrieval_count = 50, helpful_count = 20, trust_score = 0.5 WHERE fact_id = ?').run(id)
184
+ store.runLearning()
185
+ const row = store.connection.prepare('SELECT trust_score FROM facts WHERE fact_id = ?').get(id) as any
186
+ expect(row.trust_score).toBeGreaterThan(0.5)
187
+ })
188
+
189
+ it('does not adjust facts with low retrieval count', () => {
190
+ const id = store.addFact('new fact', 'general')
191
+ store.connection.prepare('UPDATE facts SET retrieval_count = 5, helpful_count = 0, trust_score = 0.8 WHERE fact_id = ?').run(id)
192
+ store.runLearning()
193
+ const row = store.connection.prepare('SELECT trust_score FROM facts WHERE fact_id = ?').get(id) as any
194
+ expect(row.trust_score).toBe(0.8)
195
+ })
196
+
197
+ it('ages facts not retrieved for 60 days', () => {
198
+ const id = store.addFact('old fact', 'general')
199
+ store.connection.prepare("UPDATE facts SET last_retrieved_at = datetime('now', '-61 days'), trust_score = 0.8 WHERE fact_id = ?").run(id)
200
+ store.runLearning()
201
+ const row = store.connection.prepare('SELECT trust_score FROM facts WHERE fact_id = ?').get(id) as any
202
+ expect(row.trust_score).toBeLessThan(0.8)
203
+ })
204
+
205
+ it('protects new facts with null last_retrieved_at from aging', () => {
206
+ const id = store.addFact('brand new fact', 'general')
207
+ store.connection.prepare('UPDATE facts SET trust_score = 0.5, last_retrieved_at = NULL WHERE fact_id = ?').run(id)
208
+ store.runLearning()
209
+ const row = store.connection.prepare('SELECT trust_score FROM facts WHERE fact_id = ?').get(id) as any
210
+ expect(row.trust_score).toBe(0.5)
211
+ })
212
+
213
+ it('returns long_facts report', () => {
214
+ const id = store.addFact('x'.repeat(600), 'general')
215
+ const result = store.runLearning()
216
+ expect(result.long_facts.length).toBeGreaterThanOrEqual(1)
217
+ expect(result.long_facts.some((f: any) => f.id === id)).toBe(true)
218
+ })
219
+ })
220
+
221
+ describe('runAudit', () => {
222
+ it('returns quality report without modifying data', () => {
223
+ const id = store.addFact('a'.repeat(600), 'general')
224
+ store.connection.prepare('UPDATE facts SET retrieval_count = 100, helpful_count = 1 WHERE fact_id = ?').run(id)
225
+ const before = (store.connection.prepare('SELECT trust_score FROM facts WHERE fact_id = ?').get(id) as any).trust_score
226
+ const report = store.runAudit()
227
+ const after = (store.connection.prepare('SELECT trust_score FROM facts WHERE fact_id = ?').get(id) as any).trust_score
228
+ expect(before).toBe(after)
229
+ expect(report.total_facts).toBeGreaterThanOrEqual(1)
230
+ expect(report.long_without_summary.length).toBeGreaterThanOrEqual(1)
231
+ })
232
+ })
233
+
234
+ describe('dream - backup', () => {
235
+ it('creates backup before dream', async () => {
236
+ store.addFact('test fact for backup', 'general')
237
+ const result = await store.backupDatabase()
238
+ expect(result).toBeTruthy()
239
+ expect(result).toContain('dream-')
240
+ expect(result).toContain('.db')
241
+ })
242
+ })
243
+
244
+ describe('dream - compress', () => {
245
+ it('generates summary for long facts without summary', () => {
246
+ const longContent = '用户偏好使用 TypeScript 开发前端项目。偏好 React 框架进行组件化开发。' + '额外补充说明'.repeat(50)
247
+ store.addFact(longContent, 'coding_style')
248
+ const result = store.compressLongFacts()
249
+ expect(result).toBeGreaterThanOrEqual(1)
250
+ const row = store.connection.prepare('SELECT summary FROM facts WHERE content = ?').get(longContent) as any
251
+ expect(row.summary).toBeTruthy()
252
+ expect(row.summary.length).toBeLessThanOrEqual(150)
253
+ expect(row.summary).toContain('TypeScript')
254
+ })
255
+
256
+ it('skips facts with existing summary', () => {
257
+ const longContent = 'x'.repeat(300)
258
+ const id = store.addFact(longContent, 'general')
259
+ store.connection.prepare('UPDATE facts SET summary = ? WHERE fact_id = ?').run('existing summary', id)
260
+ const result = store.compressLongFacts()
261
+ expect(result).toBe(0)
262
+ const row = store.connection.prepare('SELECT summary FROM facts WHERE fact_id = ?').get(id) as any
263
+ expect(row.summary).toBe('existing summary')
264
+ })
265
+
266
+ it('skips short facts', () => {
267
+ store.addFact('short fact', 'general')
268
+ const result = store.compressLongFacts()
269
+ expect(result).toBe(0)
270
+ })
271
+ })
272
+
273
+ describe('dream - merge', () => {
274
+ it('merges overlapping facts in same category', () => {
275
+ store.addFact('用户偏好使用 TypeScript 编写前端代码', 'coding_style')
276
+ store.addFact('用户偏好使用 TypeScript 编写后端代码', 'coding_style')
277
+ const result = store.mergeOverlappingFacts()
278
+ expect(result.merged).toBeGreaterThanOrEqual(1)
279
+ expect(result.details.length).toBeGreaterThanOrEqual(1)
280
+ })
281
+
282
+ it('protects high frequency facts from deletion', () => {
283
+ const id1 = store.addFact('用户偏好使用 TypeScript 编写前端代码', 'coding_style')
284
+ const id2 = store.addFact('用户偏好使用 TypeScript 编写前端代码扩展', 'coding_style')
285
+ store.connection.prepare('UPDATE facts SET retrieval_count = 200 WHERE fact_id = ?').run(id1)
286
+ const result = store.mergeOverlappingFacts()
287
+ const kept = store.connection.prepare('SELECT fact_id FROM facts WHERE fact_id = ?').get(id1) as any
288
+ expect(kept).toBeTruthy()
289
+ })
290
+
291
+ it('does not merge facts across categories', () => {
292
+ store.addFact('用户偏好使用 TypeScript 编写前端代码', 'coding_style')
293
+ store.addFact('用户偏好使用 TypeScript 编写前端代码', 'general')
294
+ const result = store.mergeOverlappingFacts()
295
+ expect(result.merged).toBe(0)
296
+ })
297
+
298
+ it('does not merge when both facts are high frequency', () => {
299
+ const id1 = store.addFact('高频测试事实 AAA 长内容匹配', 'coding_style')
300
+ const id2 = store.addFact('高频测试事实 BBB 长内容匹配', 'coding_style')
301
+ store.connection.prepare('UPDATE facts SET retrieval_count = 200 WHERE fact_id IN (?, ?)').run(id1, id2)
302
+ const result = store.mergeOverlappingFacts()
303
+ // Both facts should still exist
304
+ const row1 = store.connection.prepare('SELECT fact_id FROM facts WHERE fact_id = ?').get(id1) as any
305
+ const row2 = store.connection.prepare('SELECT fact_id FROM facts WHERE fact_id = ?').get(id2) as any
306
+ expect(row1).toBeTruthy()
307
+ expect(row2).toBeTruthy()
308
+ })
309
+ })
310
+
311
+ describe('dream - reclassify', () => {
312
+ it('moves miscategorized facts by keywords', () => {
313
+ const id = store.addFact('编码规范:文件不超过 500 行', 'identity')
314
+ const result = store.reclassifyFacts()
315
+ expect(result).toBeGreaterThanOrEqual(1)
316
+ const row = store.connection.prepare('SELECT category FROM facts WHERE fact_id = ?').get(id) as any
317
+ expect(row.category).toBe('coding_style')
318
+ })
319
+
320
+ it('skips correctly categorized facts', () => {
321
+ store.addFact('用户偏好使用 VS Code 编辑器', 'tool_pref')
322
+ const result = store.reclassifyFacts()
323
+ expect(result).toBe(0)
324
+ })
325
+ })
326
+
327
+ describe('dream - runDream', () => {
328
+ it('runs full dream cycle and returns report', async () => {
329
+ // 长文 fact(触发压缩)
330
+ store.addFact('用户偏好使用 TypeScript 开发前端。使用 React 框架。' + 'x'.repeat(250), 'coding_style')
331
+ // 重叠 fact(触发合并)
332
+ store.addFact('用户偏好使用 TypeScript 开发前端代码', 'coding_style')
333
+ // 分类错误 fact(触发重分类)
334
+ store.addFact('编码规范:文件不超过 500 行', 'identity')
335
+
336
+ const report = await store.runDream({ skipBackup: true })
337
+ expect(report.compressed).toBeGreaterThanOrEqual(0)
338
+ expect(report.merged).toBeGreaterThanOrEqual(0)
339
+ expect(report.reclassified).toBeGreaterThanOrEqual(0)
340
+ expect(report.health.total).toBeGreaterThanOrEqual(1)
341
+ expect(report.health.coverage).toBeTruthy()
342
+ })
343
+ })