@morningljn/mnemo 0.1.4 → 0.2.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.
- package/dist/retriever.js +30 -42
- package/dist/retriever.js.map +1 -1
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +21 -10
- package/dist/schema.js.map +1 -1
- package/dist/server.js +34 -1
- package/dist/server.js.map +1 -1
- package/dist/store.d.ts +37 -0
- package/dist/store.js +166 -9
- package/dist/store.js.map +1 -1
- package/dist/types.d.ts +4 -1
- package/docs/superpowers/plans/2026-05-16-memory-self-learning.md +932 -0
- package/openspec/changes/memory-self-learning/.openspec.yaml +2 -0
- package/openspec/changes/memory-self-learning/design.md +174 -0
- package/openspec/changes/memory-self-learning/proposal.md +35 -0
- package/openspec/changes/memory-self-learning/specs/fact-retrieval/spec.md +35 -0
- package/openspec/changes/memory-self-learning/specs/fact-summary/spec.md +45 -0
- package/openspec/changes/memory-self-learning/specs/length-penalty/spec.md +27 -0
- package/openspec/changes/memory-self-learning/specs/retrieval-log/spec.md +41 -0
- package/openspec/changes/memory-self-learning/specs/self-learning/spec.md +68 -0
- package/openspec/changes/memory-self-learning/tasks.md +56 -0
- package/package.json +1 -1
- package/src/retriever.ts +32 -44
- package/src/schema.ts +21 -10
- package/src/server.ts +36 -1
- package/src/store.ts +215 -9
- package/src/types.ts +4 -1
- package/tests/retriever.test.ts +53 -0
- package/tests/store.test.ts +112 -0
package/tests/retriever.test.ts
CHANGED
|
@@ -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
|
+
})
|
package/tests/store.test.ts
CHANGED
|
@@ -102,3 +102,115 @@ 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 high retrieval low helpful facts', () => {
|
|
157
|
+
const id = store.addFact('demote me', 'general')
|
|
158
|
+
store.connection.prepare('UPDATE facts SET retrieval_count = 100, helpful_count = 2, 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('promotes high helpful rate facts', () => {
|
|
166
|
+
const id = store.addFact('promote me', 'general')
|
|
167
|
+
store.connection.prepare('UPDATE facts SET retrieval_count = 50, helpful_count = 20, trust_score = 0.5 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).toBeGreaterThan(0.5)
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it('does not adjust facts with low retrieval count', () => {
|
|
174
|
+
const id = store.addFact('new fact', 'general')
|
|
175
|
+
store.connection.prepare('UPDATE facts SET retrieval_count = 5, helpful_count = 0, trust_score = 0.8 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).toBe(0.8)
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it('ages facts not retrieved for 60 days', () => {
|
|
182
|
+
const id = store.addFact('old fact', 'general')
|
|
183
|
+
store.connection.prepare("UPDATE facts SET last_retrieved_at = datetime('now', '-61 days'), trust_score = 0.8 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).toBeLessThan(0.8)
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
it('protects new facts with null last_retrieved_at from aging', () => {
|
|
190
|
+
const id = store.addFact('brand new fact', 'general')
|
|
191
|
+
store.connection.prepare('UPDATE facts SET trust_score = 0.5, last_retrieved_at = NULL 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.5)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it('returns long_facts report', () => {
|
|
198
|
+
const id = store.addFact('x'.repeat(600), 'general')
|
|
199
|
+
const result = store.runLearning()
|
|
200
|
+
expect(result.long_facts.length).toBeGreaterThanOrEqual(1)
|
|
201
|
+
expect(result.long_facts.some((f: any) => f.id === id)).toBe(true)
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
describe('runAudit', () => {
|
|
206
|
+
it('returns quality report without modifying data', () => {
|
|
207
|
+
const id = store.addFact('a'.repeat(600), 'general')
|
|
208
|
+
store.connection.prepare('UPDATE facts SET retrieval_count = 100, helpful_count = 1 WHERE fact_id = ?').run(id)
|
|
209
|
+
const before = (store.connection.prepare('SELECT trust_score FROM facts WHERE fact_id = ?').get(id) as any).trust_score
|
|
210
|
+
const report = store.runAudit()
|
|
211
|
+
const after = (store.connection.prepare('SELECT trust_score FROM facts WHERE fact_id = ?').get(id) as any).trust_score
|
|
212
|
+
expect(before).toBe(after)
|
|
213
|
+
expect(report.total_facts).toBeGreaterThanOrEqual(1)
|
|
214
|
+
expect(report.long_without_summary.length).toBeGreaterThanOrEqual(1)
|
|
215
|
+
})
|
|
216
|
+
})
|