claude-brain 0.15.2 → 0.16.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.
Files changed (249) hide show
  1. package/README.md +191 -191
  2. package/VERSION +1 -1
  3. package/assets/CLAUDE-unified.md +11 -11
  4. package/assets/CLAUDE.md +29 -11
  5. package/bunfig.toml +8 -8
  6. package/package.json +82 -82
  7. package/packs/backend/node.json +173 -173
  8. package/packs/core/javascript.json +176 -176
  9. package/packs/core/typescript.json +222 -222
  10. package/packs/frontend/react.json +254 -254
  11. package/packs/meta/testing.json +172 -172
  12. package/scripts/postinstall.mjs +341 -341
  13. package/src/automation/auto-context.ts +240 -240
  14. package/src/automation/decision-detector.ts +452 -452
  15. package/src/automation/index.ts +11 -11
  16. package/src/automation/phase12-manager.ts +456 -456
  17. package/src/automation/proactive-recall.ts +373 -373
  18. package/src/automation/project-detector.ts +310 -310
  19. package/src/automation/repo-scanner.ts +205 -205
  20. package/src/cli/auto-setup.ts +82 -82
  21. package/src/cli/bin.ts +209 -202
  22. package/src/cli/commands/chroma.ts +573 -573
  23. package/src/cli/commands/git-hook.ts +189 -189
  24. package/src/cli/commands/hooks.ts +213 -213
  25. package/src/cli/commands/init.ts +122 -122
  26. package/src/cli/commands/install-mcp.ts +92 -92
  27. package/src/cli/commands/pack.ts +197 -197
  28. package/src/cli/commands/refresh.ts +323 -0
  29. package/src/cli/commands/serve.ts +167 -173
  30. package/src/cli/commands/start.ts +42 -42
  31. package/src/cli/commands/uninstall-mcp.ts +41 -41
  32. package/src/cli/commands/update.ts +124 -121
  33. package/src/cli/diagnose.ts +4 -4
  34. package/src/cli/health-check.ts +4 -4
  35. package/src/cli/migrate-chroma.ts +106 -106
  36. package/src/cli/setup.ts +4 -4
  37. package/src/cli/ui/animations.ts +80 -80
  38. package/src/cli/ui/components.ts +82 -82
  39. package/src/cli/ui/index.ts +4 -4
  40. package/src/cli/ui/logo.ts +36 -36
  41. package/src/cli/ui/theme.ts +55 -55
  42. package/src/config/defaults.ts +50 -50
  43. package/src/config/home.ts +55 -55
  44. package/src/config/index.ts +7 -7
  45. package/src/config/loader.ts +166 -166
  46. package/src/config/migration.ts +76 -76
  47. package/src/config/schema.ts +360 -360
  48. package/src/config/validator.ts +184 -184
  49. package/src/config/watcher.ts +86 -86
  50. package/src/context/assembler.ts +398 -398
  51. package/src/context/cache-manager.ts +101 -101
  52. package/src/context/formatter.ts +84 -84
  53. package/src/context/hierarchy.ts +85 -85
  54. package/src/context/index.ts +83 -83
  55. package/src/context/progress-tracker.ts +174 -174
  56. package/src/context/standards-manager.ts +287 -287
  57. package/src/context/types.ts +252 -252
  58. package/src/context/validator.ts +58 -58
  59. package/src/diagnostics/index.ts +123 -123
  60. package/src/health/index.ts +229 -229
  61. package/src/hooks/brain-hook.ts +128 -112
  62. package/src/hooks/capture.ts +168 -205
  63. package/src/hooks/deduplicator.ts +72 -72
  64. package/src/hooks/git-capture.ts +109 -109
  65. package/src/hooks/git-hook-installer.ts +207 -207
  66. package/src/hooks/index.ts +20 -20
  67. package/src/hooks/installer.ts +194 -194
  68. package/src/hooks/passive-classifier.ts +404 -723
  69. package/src/hooks/queue.ts +129 -129
  70. package/src/hooks/session-tracker.ts +312 -275
  71. package/src/hooks/types.ts +47 -47
  72. package/src/index.ts +7 -7
  73. package/src/intelligence/cross-project/affinity.ts +162 -162
  74. package/src/intelligence/cross-project/generalizer.ts +283 -283
  75. package/src/intelligence/cross-project/index.ts +13 -13
  76. package/src/intelligence/cross-project/transfer.ts +201 -201
  77. package/src/intelligence/index.ts +24 -24
  78. package/src/intelligence/optimization/index.ts +10 -10
  79. package/src/intelligence/optimization/precompute.ts +202 -202
  80. package/src/intelligence/optimization/semantic-cache.ts +207 -207
  81. package/src/intelligence/prediction/context-anticipator.ts +198 -198
  82. package/src/intelligence/prediction/decision-predictor.ts +184 -184
  83. package/src/intelligence/prediction/index.ts +13 -13
  84. package/src/intelligence/prediction/recommender.ts +268 -268
  85. package/src/intelligence/reasoning/chain-retrieval.ts +247 -247
  86. package/src/intelligence/reasoning/counterfactual.ts +248 -248
  87. package/src/intelligence/reasoning/index.ts +13 -13
  88. package/src/intelligence/reasoning/synthesizer.ts +169 -169
  89. package/src/intelligence/temporal/evolution.ts +197 -197
  90. package/src/intelligence/temporal/index.ts +16 -16
  91. package/src/intelligence/temporal/query-processor.ts +190 -190
  92. package/src/intelligence/temporal/timeline.ts +259 -259
  93. package/src/intelligence/temporal/trends.ts +263 -263
  94. package/src/knowledge/entity-extractor.ts +416 -416
  95. package/src/knowledge/graph/builder.ts +185 -185
  96. package/src/knowledge/graph/linker.ts +201 -201
  97. package/src/knowledge/graph/memory-graph.ts +359 -359
  98. package/src/knowledge/graph/schema.ts +99 -99
  99. package/src/knowledge/graph/search.ts +168 -168
  100. package/src/knowledge/relationship-extractor.ts +108 -108
  101. package/src/memory/chroma/client.ts +174 -174
  102. package/src/memory/chroma/collection-manager.ts +94 -94
  103. package/src/memory/chroma/config.ts +57 -57
  104. package/src/memory/chroma/embeddings.ts +155 -155
  105. package/src/memory/chroma/index.ts +82 -82
  106. package/src/memory/chroma/migration.ts +270 -270
  107. package/src/memory/chroma/schemas.ts +69 -69
  108. package/src/memory/chroma/search.ts +315 -315
  109. package/src/memory/chroma/store.ts +741 -741
  110. package/src/memory/consolidation/archiver.ts +164 -164
  111. package/src/memory/consolidation/merger.ts +186 -186
  112. package/src/memory/consolidation/scorer.ts +138 -138
  113. package/src/memory/context-builder.ts +236 -236
  114. package/src/memory/database.ts +169 -169
  115. package/src/memory/embedding-utils.ts +156 -156
  116. package/src/memory/embeddings.ts +226 -226
  117. package/src/memory/episodic/detector.ts +108 -108
  118. package/src/memory/episodic/manager.ts +351 -351
  119. package/src/memory/episodic/summarizer.ts +179 -179
  120. package/src/memory/episodic/types.ts +52 -52
  121. package/src/memory/index.ts +582 -582
  122. package/src/memory/knowledge-extractor.ts +455 -455
  123. package/src/memory/learning.ts +378 -378
  124. package/src/memory/patterns.ts +396 -396
  125. package/src/memory/schema.ts +88 -88
  126. package/src/memory/search.ts +309 -309
  127. package/src/memory/store.ts +787 -787
  128. package/src/memory/types.ts +121 -121
  129. package/src/orchestrator/coordinator.ts +272 -272
  130. package/src/orchestrator/decision-logger.ts +228 -228
  131. package/src/orchestrator/event-emitter.ts +198 -198
  132. package/src/orchestrator/event-queue.ts +184 -184
  133. package/src/orchestrator/handlers/base-handler.ts +70 -70
  134. package/src/orchestrator/handlers/context-handler.ts +73 -73
  135. package/src/orchestrator/handlers/decision-handler.ts +204 -204
  136. package/src/orchestrator/handlers/index.ts +10 -10
  137. package/src/orchestrator/handlers/status-handler.ts +131 -131
  138. package/src/orchestrator/handlers/task-handler.ts +171 -171
  139. package/src/orchestrator/index.ts +275 -275
  140. package/src/orchestrator/task-parser.ts +284 -284
  141. package/src/orchestrator/types.ts +98 -98
  142. package/src/packs/index.ts +9 -9
  143. package/src/packs/loader.ts +134 -134
  144. package/src/packs/manager.ts +204 -204
  145. package/src/packs/ranker.ts +78 -78
  146. package/src/packs/types.ts +81 -81
  147. package/src/phase12/index.ts +5 -5
  148. package/src/retrieval/bm25/index.ts +300 -300
  149. package/src/retrieval/bm25/tokenizer.ts +184 -184
  150. package/src/retrieval/feedback/adaptive.ts +223 -223
  151. package/src/retrieval/feedback/index.ts +16 -16
  152. package/src/retrieval/feedback/metrics.ts +223 -223
  153. package/src/retrieval/feedback/store.ts +283 -283
  154. package/src/retrieval/fusion/index.ts +194 -194
  155. package/src/retrieval/fusion/rrf.ts +163 -163
  156. package/src/retrieval/index.ts +12 -12
  157. package/src/retrieval/pipeline.ts +375 -375
  158. package/src/retrieval/query/expander.ts +198 -198
  159. package/src/retrieval/query/index.ts +27 -27
  160. package/src/retrieval/query/intent-classifier.ts +236 -236
  161. package/src/retrieval/query/temporal-parser.ts +295 -295
  162. package/src/retrieval/reranker/index.ts +188 -188
  163. package/src/retrieval/reranker/model.ts +95 -95
  164. package/src/retrieval/service.ts +125 -125
  165. package/src/retrieval/types.ts +162 -162
  166. package/src/routing/entity-extractor.ts +428 -428
  167. package/src/routing/intent-classifier.ts +450 -436
  168. package/src/routing/response-filter.ts +261 -258
  169. package/src/routing/router.ts +1441 -1322
  170. package/src/routing/search-engine.ts +515 -475
  171. package/src/routing/types.ts +94 -94
  172. package/src/scripts/health-check.ts +118 -118
  173. package/src/scripts/setup.ts +122 -122
  174. package/src/server/handlers/call-tool.ts +156 -156
  175. package/src/server/handlers/index.ts +9 -9
  176. package/src/server/handlers/list-tools.ts +35 -35
  177. package/src/server/handlers/tools/analyze-decision-evolution.ts +151 -151
  178. package/src/server/handlers/tools/auto-remember.ts +200 -200
  179. package/src/server/handlers/tools/brain.ts +85 -85
  180. package/src/server/handlers/tools/create-project.ts +135 -135
  181. package/src/server/handlers/tools/detect-trends.ts +144 -144
  182. package/src/server/handlers/tools/find-cross-project-patterns.ts +168 -168
  183. package/src/server/handlers/tools/get-activity-log.ts +194 -194
  184. package/src/server/handlers/tools/get-code-standards.ts +124 -124
  185. package/src/server/handlers/tools/get-corrections.ts +154 -154
  186. package/src/server/handlers/tools/get-decision-timeline.ts +172 -172
  187. package/src/server/handlers/tools/get-episode.ts +103 -103
  188. package/src/server/handlers/tools/get-patterns.ts +158 -158
  189. package/src/server/handlers/tools/get-phase12-status.ts +63 -63
  190. package/src/server/handlers/tools/get-project-context.ts +75 -75
  191. package/src/server/handlers/tools/get-recommendations.ts +145 -145
  192. package/src/server/handlers/tools/index.ts +31 -31
  193. package/src/server/handlers/tools/init-project.ts +757 -757
  194. package/src/server/handlers/tools/list-episodes.ts +90 -90
  195. package/src/server/handlers/tools/list-projects.ts +125 -125
  196. package/src/server/handlers/tools/rate-memory.ts +101 -101
  197. package/src/server/handlers/tools/recall-similar.ts +87 -87
  198. package/src/server/handlers/tools/recognize-pattern.ts +126 -126
  199. package/src/server/handlers/tools/record-correction.ts +125 -125
  200. package/src/server/handlers/tools/remember-decision.ts +153 -153
  201. package/src/server/handlers/tools/schemas.ts +253 -253
  202. package/src/server/handlers/tools/search-knowledge-graph.ts +102 -102
  203. package/src/server/handlers/tools/smart-context.ts +146 -146
  204. package/src/server/handlers/tools/update-progress.ts +131 -131
  205. package/src/server/handlers/tools/what-if-analysis.ts +135 -135
  206. package/src/server/http-api.ts +693 -693
  207. package/src/server/index.ts +40 -40
  208. package/src/server/mcp-server.ts +283 -283
  209. package/src/server/providers/index.ts +7 -7
  210. package/src/server/providers/prompts.ts +327 -327
  211. package/src/server/providers/resources.ts +622 -622
  212. package/src/server/services.ts +468 -468
  213. package/src/server/types.ts +39 -39
  214. package/src/server/utils/error-handler.ts +155 -155
  215. package/src/server/utils/index.ts +13 -13
  216. package/src/server/utils/memory-indicator.ts +83 -83
  217. package/src/server/utils/request-context.ts +122 -122
  218. package/src/server/utils/response-formatter.ts +129 -129
  219. package/src/server/utils/validators.ts +210 -210
  220. package/src/setup/index.ts +48 -48
  221. package/src/setup/wizard.ts +461 -461
  222. package/src/tools/index.ts +24 -24
  223. package/src/tools/registry.ts +115 -115
  224. package/src/tools/schemas.test.ts +30 -30
  225. package/src/tools/schemas.ts +617 -617
  226. package/src/tools/types.ts +412 -412
  227. package/src/utils/circuit-breaker.ts +130 -130
  228. package/src/utils/cleanup.ts +34 -34
  229. package/src/utils/error-handler.ts +132 -132
  230. package/src/utils/error-messages.ts +60 -60
  231. package/src/utils/fallback.ts +45 -45
  232. package/src/utils/index.ts +54 -54
  233. package/src/utils/logger-utils.ts +80 -80
  234. package/src/utils/logger.ts +88 -88
  235. package/src/utils/phase12-helper.ts +56 -56
  236. package/src/utils/retry.ts +94 -94
  237. package/src/utils/timing.ts +47 -47
  238. package/src/utils/transaction.ts +63 -63
  239. package/src/vault/frontmatter.ts +264 -264
  240. package/src/vault/index.ts +318 -318
  241. package/src/vault/paths.ts +106 -106
  242. package/src/vault/query.ts +422 -422
  243. package/src/vault/reader.ts +264 -264
  244. package/src/vault/templates.ts +186 -186
  245. package/src/vault/types.ts +73 -73
  246. package/src/vault/watcher.ts +277 -277
  247. package/src/vault/writer.ts +413 -413
  248. package/tsconfig.json +30 -30
  249. package/src/cli/auto-update.ts +0 -157
@@ -1,741 +1,741 @@
1
- import { randomUUID } from 'crypto'
2
- import type { Collection } from 'chromadb'
3
- import type { Logger } from 'pino'
4
- import type { CollectionManager } from './collection-manager'
5
- import type { DecisionMetadata, MemoryMetadata } from './schemas'
6
- import type { EmbeddingProvider } from './embeddings'
7
- import type { SearchResult } from './search'
8
-
9
- /**
10
- * Sanitize metadata for ChromaDB v3.x compatibility.
11
- * Strips undefined/null values (ChromaDB only accepts string, number, boolean).
12
- */
13
- function sanitizeMetadata(metadata: Record<string, any>): Record<string, string | number | boolean> {
14
- const clean: Record<string, string | number | boolean> = {}
15
- for (const [key, value] of Object.entries(metadata)) {
16
- if (value === undefined || value === null) continue
17
- if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
18
- clean[key] = value
19
- } else if (Array.isArray(value)) {
20
- clean[key] = JSON.stringify(value)
21
- } else {
22
- clean[key] = String(value)
23
- }
24
- }
25
- return clean
26
- }
27
-
28
- export interface StoreDecisionInput {
29
- project: string
30
- context: string
31
- decision: string
32
- reasoning: string
33
- alternatives?: string
34
- outcome?: string
35
- tags?: string[]
36
- }
37
-
38
- export interface StoreMemoryInput {
39
- project: string
40
- content: string
41
- type?: 'fact' | 'preference' | 'constraint' | 'goal' | 'general'
42
- source?: string
43
- confidence?: number
44
- metadata?: Record<string, any>
45
- }
46
-
47
- export interface StoredDecision {
48
- id: string
49
- decision: string
50
- metadata: Record<string, any>
51
- distance?: number
52
- }
53
-
54
- export interface StoredMemory {
55
- id: string
56
- content: string
57
- metadata: Record<string, any>
58
- distance?: number
59
- }
60
-
61
- export type DecisionStoredCallback = (input: StoreDecisionInput & { id: string }) => void
62
-
63
- export class ChromaMemoryStore {
64
- private logger: Logger
65
- private collections: CollectionManager
66
- private embeddings?: EmbeddingProvider
67
- private onDecisionStored: DecisionStoredCallback[] = []
68
-
69
- constructor(logger: Logger, collections: CollectionManager, embeddings?: EmbeddingProvider) {
70
- this.logger = logger.child({ component: 'chroma-memory-store' })
71
- this.collections = collections
72
- this.embeddings = embeddings
73
- }
74
-
75
- addDecisionStoredListener(callback: DecisionStoredCallback): void {
76
- this.onDecisionStored.push(callback)
77
- }
78
-
79
- /**
80
- * Check for duplicate decisions by searching for similar content
81
- * Returns existing results if similarity >= threshold
82
- */
83
- async searchForDuplicates(
84
- decision: string,
85
- project: string,
86
- similarityThreshold: number = 0.9
87
- ): Promise<{ id: string; similarity: number }[]> {
88
- try {
89
- const collection = await this.collections.getDecisions()
90
-
91
- let results: any
92
-
93
- if (this.embeddings) {
94
- const embedding = await this.embeddings.generate(decision)
95
- results = await collection.query({
96
- queryEmbeddings: [embedding],
97
- nResults: 3,
98
- where: { project: { $eq: project } },
99
- include: ['documents', 'metadatas', 'distances']
100
- })
101
- } else {
102
- results = await collection.query({
103
- queryTexts: [decision],
104
- nResults: 3,
105
- where: { project: { $eq: project } },
106
- include: ['documents', 'metadatas', 'distances']
107
- })
108
- }
109
-
110
- if (!results.ids || results.ids[0]?.length === 0) {
111
- return []
112
- }
113
-
114
- const duplicates: { id: string; similarity: number }[] = []
115
- const ids = results.ids[0] || []
116
- const distances = results.distances?.[0] || []
117
-
118
- for (let i = 0; i < ids.length; i++) {
119
- const similarity = 1 - (distances[i] || 0)
120
- if (similarity >= similarityThreshold) {
121
- duplicates.push({ id: ids[i], similarity })
122
- }
123
- }
124
-
125
- return duplicates
126
- } catch (error) {
127
- this.logger.warn({ error }, 'Failed to check for duplicates, proceeding with store')
128
- return []
129
- }
130
- }
131
-
132
- async storeDecision(input: StoreDecisionInput): Promise<string> {
133
- // Check for duplicate decisions (similarity > 0.9)
134
- const existing = await this.searchForDuplicates(input.decision, input.project, 0.9)
135
- const firstDuplicate = existing[0]
136
- if (firstDuplicate) {
137
- this.logger.info({ existingId: firstDuplicate.id, similarity: firstDuplicate.similarity }, 'Skipping duplicate decision')
138
- return firstDuplicate.id
139
- }
140
-
141
- const id = randomUUID()
142
- const now = new Date().toISOString()
143
-
144
- const metadata: Record<string, any> = {
145
- project: input.project,
146
- context: input.context,
147
- reasoning: input.reasoning,
148
- alternatives: input.alternatives || '',
149
- outcome: input.outcome || '',
150
- tags: (input.tags || []).join(','),
151
- created_at: now,
152
- updated_at: now,
153
- source: 'manual',
154
- decision: input.decision // Include the decision text in metadata
155
- }
156
-
157
- try {
158
- const collection = await this.collections.getDecisions()
159
-
160
- const embeddings = this.embeddings
161
- ? [await this.embeddings.generate(input.decision)]
162
- : undefined
163
-
164
- await collection.add({
165
- ids: [id],
166
- documents: [input.decision],
167
- metadatas: [sanitizeMetadata(metadata as Record<string, any>)],
168
- ...(embeddings ? { embeddings } : {})
169
- })
170
-
171
- // ALSO store in memories collection for unified semantic search
172
- try {
173
- const memoriesCollection = await this.collections.getMemories()
174
- const memoryContent = `Decision: ${input.decision}\nContext: ${input.context}\nReasoning: ${input.reasoning}`
175
- const memoryMetadata = {
176
- project: input.project,
177
- type: 'decision',
178
- source: 'remember_decision',
179
- confidence: 1.0,
180
- created_at: now,
181
- updated_at: now,
182
- decision_id: id,
183
- // Phase 19: Include decision fields so memories collection results
184
- // can surface decision content without cross-collection lookup
185
- decision: input.decision,
186
- reasoning: input.reasoning,
187
- context: input.context
188
- }
189
-
190
- await memoriesCollection.add({
191
- ids: [id], // Use same ID for cross-reference
192
- documents: [memoryContent],
193
- metadatas: [sanitizeMetadata(memoryMetadata)],
194
- ...(embeddings ? { embeddings } : {})
195
- })
196
-
197
- this.logger.debug({ id }, 'Decision also stored in memories collection')
198
- } catch (memError) {
199
- this.logger.warn({ error: memError, id }, 'Failed to store decision in memories collection')
200
- }
201
-
202
- this.logger.info(
203
- { id, project: input.project },
204
- 'Decision stored in ChromaDB'
205
- )
206
-
207
- // Notify listeners (e.g., knowledge graph builder)
208
- for (const callback of this.onDecisionStored) {
209
- try {
210
- callback({ ...input, id })
211
- } catch (callbackError) {
212
- this.logger.warn({ error: callbackError }, 'Decision stored callback failed')
213
- }
214
- }
215
-
216
- return id
217
-
218
- } catch (error) {
219
- this.logger.error({ error, input }, 'Failed to store decision')
220
- throw error
221
- }
222
- }
223
-
224
- async storePattern(input: {
225
- project: string
226
- pattern_type: 'solution' | 'anti-pattern' | 'best-practice' | 'common-issue'
227
- description: string
228
- example?: string
229
- confidence: number
230
- context?: string
231
- source?: string
232
- }): Promise<string> {
233
- const id = randomUUID()
234
- const now = new Date().toISOString()
235
-
236
- const metadata: Record<string, any> = {
237
- project: input.project,
238
- pattern_type: input.pattern_type,
239
- description: input.description,
240
- example: input.example || '',
241
- confidence: input.confidence,
242
- context: input.context || '',
243
- created_at: now,
244
- updated_at: now,
245
- source: input.source || 'manual'
246
- }
247
-
248
- try {
249
- const collection = await this.collections.getPatterns()
250
-
251
- const embeddings = this.embeddings
252
- ? [await this.embeddings.generate(input.description)]
253
- : undefined
254
-
255
- await collection.add({
256
- ids: [id],
257
- documents: [input.description],
258
- metadatas: [sanitizeMetadata(metadata)],
259
- ...(embeddings ? { embeddings } : {})
260
- })
261
-
262
- // ALSO store in memories collection for unified semantic search
263
- try {
264
- const memoriesCollection = await this.collections.getMemories()
265
- const memoryContent = `Pattern (${input.pattern_type}): ${input.description}${input.context ? `\nContext: ${input.context}` : ''}${input.example ? `\nExample: ${input.example}` : ''}`
266
- const memoryMetadata = {
267
- project: input.project,
268
- type: 'pattern',
269
- source: 'recognize_pattern',
270
- confidence: input.confidence,
271
- created_at: now,
272
- updated_at: now,
273
- pattern_id: id,
274
- pattern_type: input.pattern_type
275
- }
276
-
277
- await memoriesCollection.add({
278
- ids: [id],
279
- documents: [memoryContent],
280
- metadatas: [sanitizeMetadata(memoryMetadata)],
281
- ...(embeddings ? { embeddings } : {})
282
- })
283
-
284
- this.logger.debug({ id }, 'Pattern also stored in memories collection')
285
- } catch (memError) {
286
- this.logger.warn({ error: memError, id }, 'Failed to store pattern in memories collection')
287
- }
288
-
289
- this.logger.info({ id, pattern_type: input.pattern_type }, 'Pattern stored in ChromaDB')
290
-
291
- return id
292
-
293
- } catch (error) {
294
- this.logger.error({ error, input }, 'Failed to store pattern')
295
- throw error
296
- }
297
- }
298
-
299
- async storeCorrection(input: {
300
- project: string
301
- original: string
302
- correction: string
303
- reasoning: string
304
- context?: string
305
- confidence: number
306
- }): Promise<string> {
307
- const id = randomUUID()
308
- const now = new Date().toISOString()
309
-
310
- const metadata: Record<string, any> = {
311
- project: input.project,
312
- original: input.original,
313
- correction: input.correction,
314
- reasoning: input.reasoning,
315
- context: input.context || '',
316
- confidence: input.confidence,
317
- created_at: now,
318
- updated_at: now,
319
- source: 'manual'
320
- }
321
-
322
- try {
323
- const collection = await this.collections.getCorrections()
324
-
325
- const embeddings = this.embeddings
326
- ? [await this.embeddings.generate(input.correction)]
327
- : undefined
328
-
329
- await collection.add({
330
- ids: [id],
331
- documents: [input.correction],
332
- metadatas: [sanitizeMetadata(metadata)],
333
- ...(embeddings ? { embeddings } : {})
334
- })
335
-
336
- // ALSO store in memories collection for unified semantic search
337
- try {
338
- const memoriesCollection = await this.collections.getMemories()
339
- const memoryContent = `Correction: ${input.correction}\nOriginal: ${input.original}\nReasoning: ${input.reasoning}${input.context ? `\nContext: ${input.context}` : ''}`
340
- const memoryMetadata = {
341
- project: input.project,
342
- type: 'correction',
343
- source: 'record_correction',
344
- confidence: input.confidence,
345
- created_at: now,
346
- updated_at: now,
347
- correction_id: id
348
- }
349
-
350
- await memoriesCollection.add({
351
- ids: [id],
352
- documents: [memoryContent],
353
- metadatas: [sanitizeMetadata(memoryMetadata)],
354
- ...(embeddings ? { embeddings } : {})
355
- })
356
-
357
- this.logger.debug({ id }, 'Correction also stored in memories collection')
358
- } catch (memError) {
359
- this.logger.warn({ error: memError, id }, 'Failed to store correction in memories collection')
360
- }
361
-
362
- this.logger.info({ id, project: input.project }, 'Correction stored in ChromaDB')
363
-
364
- return id
365
-
366
- } catch (error) {
367
- this.logger.error({ error, input }, 'Failed to store correction')
368
- throw error
369
- }
370
- }
371
-
372
- async storeMemory(input: StoreMemoryInput): Promise<string> {
373
- const id = randomUUID()
374
- const now = new Date().toISOString()
375
-
376
- const metadata: MemoryMetadata = {
377
- project: input.project,
378
- type: input.type || 'general',
379
- source: input.source || 'unknown',
380
- confidence: input.confidence || 1.0,
381
- created_at: now,
382
- updated_at: now
383
- }
384
-
385
- try {
386
- const collection = await this.collections.getMemories()
387
-
388
- const embeddings = this.embeddings
389
- ? [await this.embeddings.generate(input.content)]
390
- : undefined
391
-
392
- await collection.add({
393
- ids: [id],
394
- documents: [input.content],
395
- metadatas: [sanitizeMetadata(metadata as Record<string, any>)],
396
- ...(embeddings ? { embeddings } : {})
397
- })
398
-
399
- this.logger.info(
400
- { id, project: input.project, type: input.type },
401
- 'Memory stored in ChromaDB'
402
- )
403
-
404
- return id
405
-
406
- } catch (error) {
407
- this.logger.error({ error, input }, 'Failed to store memory')
408
- throw error
409
- }
410
- }
411
-
412
- async upsertDecision(id: string, input: StoreDecisionInput): Promise<void> {
413
- const now = new Date().toISOString()
414
-
415
- const metadata: Record<string, any> = {
416
- project: input.project,
417
- context: input.context,
418
- reasoning: input.reasoning,
419
- alternatives: input.alternatives || '',
420
- outcome: input.outcome || '',
421
- tags: (input.tags || []).join(','),
422
- created_at: now,
423
- updated_at: now,
424
- source: 'manual'
425
- }
426
-
427
- try {
428
- const collection = await this.collections.getDecisions()
429
-
430
- const embeddings = this.embeddings
431
- ? [await this.embeddings.generate(input.decision)]
432
- : undefined
433
-
434
- await collection.upsert({
435
- ids: [id],
436
- documents: [input.decision],
437
- metadatas: [sanitizeMetadata(metadata as Record<string, any>)],
438
- ...(embeddings ? { embeddings } : {})
439
- })
440
-
441
- this.logger.info({ id }, 'Decision upserted')
442
-
443
- } catch (error) {
444
- this.logger.error({ error, id, errorMessage: error instanceof Error ? error.message : String(error) }, 'Failed to upsert decision')
445
- throw error
446
- }
447
- }
448
-
449
- async getDecision(id: string): Promise<StoredDecision | null> {
450
- try {
451
- const collection = await this.collections.getDecisions()
452
-
453
- const result = await collection.get({
454
- ids: [id],
455
- include: ['documents', 'metadatas']
456
- })
457
-
458
- if (result.ids.length === 0) {
459
- return null
460
- }
461
-
462
- return {
463
- id: result.ids[0],
464
- decision: result.documents![0] as string,
465
- metadata: result.metadatas![0] as Record<string, any>
466
- }
467
-
468
- } catch (error) {
469
- this.logger.error({ error, id }, 'Failed to get decision')
470
- throw error
471
- }
472
- }
473
-
474
- async getDecisionsByProject(project: string): Promise<StoredDecision[]> {
475
- try {
476
- const collection = await this.collections.getDecisions()
477
-
478
- const result = await collection.get({
479
- where: { project },
480
- include: ['documents', 'metadatas']
481
- })
482
-
483
- return result.ids.map((id, i) => ({
484
- id,
485
- decision: result.documents![i] as string,
486
- metadata: result.metadatas![i] as Record<string, any>
487
- }))
488
-
489
- } catch (error) {
490
- this.logger.error({ error, project }, 'Failed to get decisions by project')
491
- throw error
492
- }
493
- }
494
-
495
- async deleteDecision(id: string): Promise<void> {
496
- try {
497
- // Delete from decisions collection
498
- const collection = await this.collections.getDecisions()
499
- await collection.delete({ ids: [id] })
500
-
501
- // Verify deletion succeeded
502
- try {
503
- const verify = await collection.get({ ids: [id] })
504
- if (verify.ids.length > 0) {
505
- this.logger.warn({ id }, 'Decision still exists after delete — retrying')
506
- await collection.delete({ ids: [id] })
507
- }
508
- } catch {
509
- // Verification query failed, assume delete succeeded
510
- }
511
-
512
- // ALSO delete from memories collection (dual storage uses same ID)
513
- try {
514
- const memoriesCollection = await this.collections.getMemories()
515
- await memoriesCollection.delete({ ids: [id] })
516
- this.logger.debug({ id }, 'Decision also deleted from memories collection')
517
- } catch {
518
- // Memories collection entry may not exist, that's ok
519
- }
520
-
521
- this.logger.info({ id }, 'Decision deleted from all collections')
522
-
523
- } catch (error) {
524
- this.logger.error({ error, id }, 'Failed to delete decision')
525
- throw error
526
- }
527
- }
528
-
529
- async getAllDecisions(): Promise<StoredDecision[]> {
530
- try {
531
- const collection = await this.collections.getDecisions()
532
-
533
- const result = await collection.get({
534
- include: ['documents', 'metadatas']
535
- })
536
-
537
- return result.ids.map((id, i) => ({
538
- id,
539
- decision: result.documents![i] as string,
540
- metadata: result.metadatas![i] as Record<string, any>
541
- }))
542
-
543
- } catch (error) {
544
- this.logger.error({ error }, 'Failed to get all decisions')
545
- throw error
546
- }
547
- }
548
-
549
- async getPatternsByProject(project: string, options?: {
550
- pattern_type?: 'solution' | 'anti-pattern' | 'best-practice' | 'common-issue'
551
- limit?: number
552
- }): Promise<any[]> {
553
- try {
554
- const collection = await this.collections.getPatterns()
555
-
556
- // Build where clause - handle single vs multiple conditions
557
- let where: any
558
- if (options?.pattern_type) {
559
- // Multiple conditions: use $and operator
560
- where = {
561
- $and: [
562
- { project: { $eq: project } },
563
- { pattern_type: { $eq: options.pattern_type } }
564
- ]
565
- }
566
- } else {
567
- // Single condition
568
- where = { project: { $eq: project } }
569
- }
570
-
571
- const result = await collection.get({
572
- where,
573
- include: ['documents', 'metadatas'],
574
- ...(options?.limit ? { limit: options.limit } : {})
575
- })
576
-
577
- return result.ids.map((id, i) => ({
578
- id,
579
- description: result.documents![i] as string,
580
- metadata: result.metadatas![i] as Record<string, any>
581
- }))
582
-
583
- } catch (error) {
584
- this.logger.error({ error, project }, 'Failed to get patterns by project')
585
- throw error
586
- }
587
- }
588
-
589
- async getCorrectionsByProject(project: string, limit: number = 10): Promise<any[]> {
590
- try {
591
- const collection = await this.collections.getCorrections()
592
-
593
- const result = await collection.get({
594
- where: { project },
595
- include: ['documents', 'metadatas'],
596
- limit
597
- })
598
-
599
- return result.ids.map((id, i) => ({
600
- id,
601
- correction: result.documents![i] as string,
602
- metadata: result.metadatas![i] as Record<string, any>
603
- }))
604
-
605
- } catch (error) {
606
- this.logger.error({ error, project }, 'Failed to get corrections by project')
607
- throw error
608
- }
609
- }
610
-
611
- async searchPatterns(
612
- query: string,
613
- options: {
614
- project?: string
615
- pattern_type?: 'solution' | 'anti-pattern' | 'best-practice' | 'common-issue'
616
- limit?: number
617
- minSimilarity?: number
618
- } = {}
619
- ): Promise<SearchResult[]> {
620
- const {
621
- project,
622
- pattern_type,
623
- limit = 10,
624
- minSimilarity = 0.5
625
- } = options
626
-
627
- try {
628
- const collection = await this.collections.getPatterns()
629
-
630
- const where: any = {}
631
- if (project) {
632
- where.project = project
633
- }
634
- if (pattern_type) {
635
- where.pattern_type = pattern_type
636
- }
637
-
638
- let results: any
639
-
640
- if (this.embeddings) {
641
- const embedding = await this.embeddings.generate(query)
642
- results = await collection.query({
643
- queryEmbeddings: [embedding],
644
- nResults: limit,
645
- where: Object.keys(where).length > 0 ? where : undefined,
646
- include: ['documents', 'metadatas', 'distances']
647
- })
648
- } else {
649
- results = await collection.query({
650
- queryTexts: [query],
651
- nResults: limit,
652
- where: Object.keys(where).length > 0 ? where : undefined,
653
- include: ['documents', 'metadatas', 'distances']
654
- })
655
- }
656
-
657
- return this.processResults(results, minSimilarity)
658
-
659
- } catch (error) {
660
- this.logger.error({ error, query }, 'Pattern search failed')
661
- throw error
662
- }
663
- }
664
-
665
- async searchCorrections(
666
- query: string,
667
- options: {
668
- project?: string
669
- limit?: number
670
- minSimilarity?: number
671
- } = {}
672
- ): Promise<SearchResult[]> {
673
- const {
674
- project,
675
- limit = 10,
676
- minSimilarity = 0.5
677
- } = options
678
-
679
- try {
680
- const collection = await this.collections.getCorrections()
681
-
682
- const where: any = project ? { project } : {}
683
-
684
- let results: any
685
-
686
- if (this.embeddings) {
687
- const embedding = await this.embeddings.generate(query)
688
- results = await collection.query({
689
- queryEmbeddings: [embedding],
690
- nResults: limit,
691
- where: Object.keys(where).length > 0 ? where : undefined,
692
- include: ['documents', 'metadatas', 'distances']
693
- })
694
- } else {
695
- results = await collection.query({
696
- queryTexts: [query],
697
- nResults: limit,
698
- where: Object.keys(where).length > 0 ? where : undefined,
699
- include: ['documents', 'metadatas', 'distances']
700
- })
701
- }
702
-
703
- return this.processResults(results, minSimilarity)
704
-
705
- } catch (error) {
706
- this.logger.error({ error, query }, 'Correction search failed')
707
- throw error
708
- }
709
- }
710
-
711
- private processResults(
712
- results: any,
713
- minSimilarity: number
714
- ): SearchResult[] {
715
- if (!results.ids || results.ids.length === 0) {
716
- return []
717
- }
718
-
719
- const processed: SearchResult[] = []
720
-
721
- const ids = results.ids[0] || []
722
- const documents = results.documents?.[0] || []
723
- const metadatas = results.metadatas?.[0] || []
724
- const distances = results.distances?.[0] || []
725
-
726
- for (let i = 0; i < ids.length; i++) {
727
- const similarity = 1 - (distances[i] || 0)
728
-
729
- if (similarity >= minSimilarity) {
730
- processed.push({
731
- id: ids[i],
732
- content: documents[i],
733
- metadata: metadatas[i],
734
- similarity
735
- })
736
- }
737
- }
738
-
739
- return processed.sort((a, b) => b.similarity - a.similarity)
740
- }
741
- }
1
+ import { randomUUID } from 'crypto'
2
+ import type { Collection } from 'chromadb'
3
+ import type { Logger } from 'pino'
4
+ import type { CollectionManager } from './collection-manager'
5
+ import type { DecisionMetadata, MemoryMetadata } from './schemas'
6
+ import type { EmbeddingProvider } from './embeddings'
7
+ import type { SearchResult } from './search'
8
+
9
+ /**
10
+ * Sanitize metadata for ChromaDB v3.x compatibility.
11
+ * Strips undefined/null values (ChromaDB only accepts string, number, boolean).
12
+ */
13
+ function sanitizeMetadata(metadata: Record<string, any>): Record<string, string | number | boolean> {
14
+ const clean: Record<string, string | number | boolean> = {}
15
+ for (const [key, value] of Object.entries(metadata)) {
16
+ if (value === undefined || value === null) continue
17
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
18
+ clean[key] = value
19
+ } else if (Array.isArray(value)) {
20
+ clean[key] = JSON.stringify(value)
21
+ } else {
22
+ clean[key] = String(value)
23
+ }
24
+ }
25
+ return clean
26
+ }
27
+
28
+ export interface StoreDecisionInput {
29
+ project: string
30
+ context: string
31
+ decision: string
32
+ reasoning: string
33
+ alternatives?: string
34
+ outcome?: string
35
+ tags?: string[]
36
+ }
37
+
38
+ export interface StoreMemoryInput {
39
+ project: string
40
+ content: string
41
+ type?: 'fact' | 'preference' | 'constraint' | 'goal' | 'general'
42
+ source?: string
43
+ confidence?: number
44
+ metadata?: Record<string, any>
45
+ }
46
+
47
+ export interface StoredDecision {
48
+ id: string
49
+ decision: string
50
+ metadata: Record<string, any>
51
+ distance?: number
52
+ }
53
+
54
+ export interface StoredMemory {
55
+ id: string
56
+ content: string
57
+ metadata: Record<string, any>
58
+ distance?: number
59
+ }
60
+
61
+ export type DecisionStoredCallback = (input: StoreDecisionInput & { id: string }) => void
62
+
63
+ export class ChromaMemoryStore {
64
+ private logger: Logger
65
+ private collections: CollectionManager
66
+ private embeddings?: EmbeddingProvider
67
+ private onDecisionStored: DecisionStoredCallback[] = []
68
+
69
+ constructor(logger: Logger, collections: CollectionManager, embeddings?: EmbeddingProvider) {
70
+ this.logger = logger.child({ component: 'chroma-memory-store' })
71
+ this.collections = collections
72
+ this.embeddings = embeddings
73
+ }
74
+
75
+ addDecisionStoredListener(callback: DecisionStoredCallback): void {
76
+ this.onDecisionStored.push(callback)
77
+ }
78
+
79
+ /**
80
+ * Check for duplicate decisions by searching for similar content
81
+ * Returns existing results if similarity >= threshold
82
+ */
83
+ async searchForDuplicates(
84
+ decision: string,
85
+ project: string,
86
+ similarityThreshold: number = 0.9
87
+ ): Promise<{ id: string; similarity: number }[]> {
88
+ try {
89
+ const collection = await this.collections.getDecisions()
90
+
91
+ let results: any
92
+
93
+ if (this.embeddings) {
94
+ const embedding = await this.embeddings.generate(decision)
95
+ results = await collection.query({
96
+ queryEmbeddings: [embedding],
97
+ nResults: 3,
98
+ where: { project: { $eq: project } },
99
+ include: ['documents', 'metadatas', 'distances']
100
+ })
101
+ } else {
102
+ results = await collection.query({
103
+ queryTexts: [decision],
104
+ nResults: 3,
105
+ where: { project: { $eq: project } },
106
+ include: ['documents', 'metadatas', 'distances']
107
+ })
108
+ }
109
+
110
+ if (!results.ids || results.ids[0]?.length === 0) {
111
+ return []
112
+ }
113
+
114
+ const duplicates: { id: string; similarity: number }[] = []
115
+ const ids = results.ids[0] || []
116
+ const distances = results.distances?.[0] || []
117
+
118
+ for (let i = 0; i < ids.length; i++) {
119
+ const similarity = 1 - (distances[i] || 0)
120
+ if (similarity >= similarityThreshold) {
121
+ duplicates.push({ id: ids[i], similarity })
122
+ }
123
+ }
124
+
125
+ return duplicates
126
+ } catch (error) {
127
+ this.logger.warn({ error }, 'Failed to check for duplicates, proceeding with store')
128
+ return []
129
+ }
130
+ }
131
+
132
+ async storeDecision(input: StoreDecisionInput): Promise<string> {
133
+ // Check for duplicate decisions (similarity > 0.9)
134
+ const existing = await this.searchForDuplicates(input.decision, input.project, 0.9)
135
+ const firstDuplicate = existing[0]
136
+ if (firstDuplicate) {
137
+ this.logger.info({ existingId: firstDuplicate.id, similarity: firstDuplicate.similarity }, 'Skipping duplicate decision')
138
+ return firstDuplicate.id
139
+ }
140
+
141
+ const id = randomUUID()
142
+ const now = new Date().toISOString()
143
+
144
+ const metadata: Record<string, any> = {
145
+ project: input.project,
146
+ context: input.context,
147
+ reasoning: input.reasoning,
148
+ alternatives: input.alternatives || '',
149
+ outcome: input.outcome || '',
150
+ tags: (input.tags || []).join(','),
151
+ created_at: now,
152
+ updated_at: now,
153
+ source: 'manual',
154
+ decision: input.decision // Include the decision text in metadata
155
+ }
156
+
157
+ try {
158
+ const collection = await this.collections.getDecisions()
159
+
160
+ const embeddings = this.embeddings
161
+ ? [await this.embeddings.generate(input.decision)]
162
+ : undefined
163
+
164
+ await collection.add({
165
+ ids: [id],
166
+ documents: [input.decision],
167
+ metadatas: [sanitizeMetadata(metadata as Record<string, any>)],
168
+ ...(embeddings ? { embeddings } : {})
169
+ })
170
+
171
+ // ALSO store in memories collection for unified semantic search
172
+ try {
173
+ const memoriesCollection = await this.collections.getMemories()
174
+ const memoryContent = `Decision: ${input.decision}\nContext: ${input.context}\nReasoning: ${input.reasoning}`
175
+ const memoryMetadata = {
176
+ project: input.project,
177
+ type: 'decision',
178
+ source: 'remember_decision',
179
+ confidence: 1.0,
180
+ created_at: now,
181
+ updated_at: now,
182
+ decision_id: id,
183
+ // Phase 19: Include decision fields so memories collection results
184
+ // can surface decision content without cross-collection lookup
185
+ decision: input.decision,
186
+ reasoning: input.reasoning,
187
+ context: input.context
188
+ }
189
+
190
+ await memoriesCollection.add({
191
+ ids: [id], // Use same ID for cross-reference
192
+ documents: [memoryContent],
193
+ metadatas: [sanitizeMetadata(memoryMetadata)],
194
+ ...(embeddings ? { embeddings } : {})
195
+ })
196
+
197
+ this.logger.debug({ id }, 'Decision also stored in memories collection')
198
+ } catch (memError) {
199
+ this.logger.warn({ error: memError, id }, 'Failed to store decision in memories collection')
200
+ }
201
+
202
+ this.logger.info(
203
+ { id, project: input.project },
204
+ 'Decision stored in ChromaDB'
205
+ )
206
+
207
+ // Notify listeners (e.g., knowledge graph builder)
208
+ for (const callback of this.onDecisionStored) {
209
+ try {
210
+ callback({ ...input, id })
211
+ } catch (callbackError) {
212
+ this.logger.warn({ error: callbackError }, 'Decision stored callback failed')
213
+ }
214
+ }
215
+
216
+ return id
217
+
218
+ } catch (error) {
219
+ this.logger.error({ error, input }, 'Failed to store decision')
220
+ throw error
221
+ }
222
+ }
223
+
224
+ async storePattern(input: {
225
+ project: string
226
+ pattern_type: 'solution' | 'anti-pattern' | 'best-practice' | 'common-issue'
227
+ description: string
228
+ example?: string
229
+ confidence: number
230
+ context?: string
231
+ source?: string
232
+ }): Promise<string> {
233
+ const id = randomUUID()
234
+ const now = new Date().toISOString()
235
+
236
+ const metadata: Record<string, any> = {
237
+ project: input.project,
238
+ pattern_type: input.pattern_type,
239
+ description: input.description,
240
+ example: input.example || '',
241
+ confidence: input.confidence,
242
+ context: input.context || '',
243
+ created_at: now,
244
+ updated_at: now,
245
+ source: input.source || 'manual'
246
+ }
247
+
248
+ try {
249
+ const collection = await this.collections.getPatterns()
250
+
251
+ const embeddings = this.embeddings
252
+ ? [await this.embeddings.generate(input.description)]
253
+ : undefined
254
+
255
+ await collection.add({
256
+ ids: [id],
257
+ documents: [input.description],
258
+ metadatas: [sanitizeMetadata(metadata)],
259
+ ...(embeddings ? { embeddings } : {})
260
+ })
261
+
262
+ // ALSO store in memories collection for unified semantic search
263
+ try {
264
+ const memoriesCollection = await this.collections.getMemories()
265
+ const memoryContent = `Pattern (${input.pattern_type}): ${input.description}${input.context ? `\nContext: ${input.context}` : ''}${input.example ? `\nExample: ${input.example}` : ''}`
266
+ const memoryMetadata = {
267
+ project: input.project,
268
+ type: 'pattern',
269
+ source: 'recognize_pattern',
270
+ confidence: input.confidence,
271
+ created_at: now,
272
+ updated_at: now,
273
+ pattern_id: id,
274
+ pattern_type: input.pattern_type
275
+ }
276
+
277
+ await memoriesCollection.add({
278
+ ids: [id],
279
+ documents: [memoryContent],
280
+ metadatas: [sanitizeMetadata(memoryMetadata)],
281
+ ...(embeddings ? { embeddings } : {})
282
+ })
283
+
284
+ this.logger.debug({ id }, 'Pattern also stored in memories collection')
285
+ } catch (memError) {
286
+ this.logger.warn({ error: memError, id }, 'Failed to store pattern in memories collection')
287
+ }
288
+
289
+ this.logger.info({ id, pattern_type: input.pattern_type }, 'Pattern stored in ChromaDB')
290
+
291
+ return id
292
+
293
+ } catch (error) {
294
+ this.logger.error({ error, input }, 'Failed to store pattern')
295
+ throw error
296
+ }
297
+ }
298
+
299
+ async storeCorrection(input: {
300
+ project: string
301
+ original: string
302
+ correction: string
303
+ reasoning: string
304
+ context?: string
305
+ confidence: number
306
+ }): Promise<string> {
307
+ const id = randomUUID()
308
+ const now = new Date().toISOString()
309
+
310
+ const metadata: Record<string, any> = {
311
+ project: input.project,
312
+ original: input.original,
313
+ correction: input.correction,
314
+ reasoning: input.reasoning,
315
+ context: input.context || '',
316
+ confidence: input.confidence,
317
+ created_at: now,
318
+ updated_at: now,
319
+ source: 'manual'
320
+ }
321
+
322
+ try {
323
+ const collection = await this.collections.getCorrections()
324
+
325
+ const embeddings = this.embeddings
326
+ ? [await this.embeddings.generate(input.correction)]
327
+ : undefined
328
+
329
+ await collection.add({
330
+ ids: [id],
331
+ documents: [input.correction],
332
+ metadatas: [sanitizeMetadata(metadata)],
333
+ ...(embeddings ? { embeddings } : {})
334
+ })
335
+
336
+ // ALSO store in memories collection for unified semantic search
337
+ try {
338
+ const memoriesCollection = await this.collections.getMemories()
339
+ const memoryContent = `Correction: ${input.correction}\nOriginal: ${input.original}\nReasoning: ${input.reasoning}${input.context ? `\nContext: ${input.context}` : ''}`
340
+ const memoryMetadata = {
341
+ project: input.project,
342
+ type: 'correction',
343
+ source: 'record_correction',
344
+ confidence: input.confidence,
345
+ created_at: now,
346
+ updated_at: now,
347
+ correction_id: id
348
+ }
349
+
350
+ await memoriesCollection.add({
351
+ ids: [id],
352
+ documents: [memoryContent],
353
+ metadatas: [sanitizeMetadata(memoryMetadata)],
354
+ ...(embeddings ? { embeddings } : {})
355
+ })
356
+
357
+ this.logger.debug({ id }, 'Correction also stored in memories collection')
358
+ } catch (memError) {
359
+ this.logger.warn({ error: memError, id }, 'Failed to store correction in memories collection')
360
+ }
361
+
362
+ this.logger.info({ id, project: input.project }, 'Correction stored in ChromaDB')
363
+
364
+ return id
365
+
366
+ } catch (error) {
367
+ this.logger.error({ error, input }, 'Failed to store correction')
368
+ throw error
369
+ }
370
+ }
371
+
372
+ async storeMemory(input: StoreMemoryInput): Promise<string> {
373
+ const id = randomUUID()
374
+ const now = new Date().toISOString()
375
+
376
+ const metadata: MemoryMetadata = {
377
+ project: input.project,
378
+ type: input.type || 'general',
379
+ source: input.source || 'unknown',
380
+ confidence: input.confidence || 1.0,
381
+ created_at: now,
382
+ updated_at: now
383
+ }
384
+
385
+ try {
386
+ const collection = await this.collections.getMemories()
387
+
388
+ const embeddings = this.embeddings
389
+ ? [await this.embeddings.generate(input.content)]
390
+ : undefined
391
+
392
+ await collection.add({
393
+ ids: [id],
394
+ documents: [input.content],
395
+ metadatas: [sanitizeMetadata(metadata as Record<string, any>)],
396
+ ...(embeddings ? { embeddings } : {})
397
+ })
398
+
399
+ this.logger.info(
400
+ { id, project: input.project, type: input.type },
401
+ 'Memory stored in ChromaDB'
402
+ )
403
+
404
+ return id
405
+
406
+ } catch (error) {
407
+ this.logger.error({ error, input }, 'Failed to store memory')
408
+ throw error
409
+ }
410
+ }
411
+
412
+ async upsertDecision(id: string, input: StoreDecisionInput): Promise<void> {
413
+ const now = new Date().toISOString()
414
+
415
+ const metadata: Record<string, any> = {
416
+ project: input.project,
417
+ context: input.context,
418
+ reasoning: input.reasoning,
419
+ alternatives: input.alternatives || '',
420
+ outcome: input.outcome || '',
421
+ tags: (input.tags || []).join(','),
422
+ created_at: now,
423
+ updated_at: now,
424
+ source: 'manual'
425
+ }
426
+
427
+ try {
428
+ const collection = await this.collections.getDecisions()
429
+
430
+ const embeddings = this.embeddings
431
+ ? [await this.embeddings.generate(input.decision)]
432
+ : undefined
433
+
434
+ await collection.upsert({
435
+ ids: [id],
436
+ documents: [input.decision],
437
+ metadatas: [sanitizeMetadata(metadata as Record<string, any>)],
438
+ ...(embeddings ? { embeddings } : {})
439
+ })
440
+
441
+ this.logger.info({ id }, 'Decision upserted')
442
+
443
+ } catch (error) {
444
+ this.logger.error({ error, id, errorMessage: error instanceof Error ? error.message : String(error) }, 'Failed to upsert decision')
445
+ throw error
446
+ }
447
+ }
448
+
449
+ async getDecision(id: string): Promise<StoredDecision | null> {
450
+ try {
451
+ const collection = await this.collections.getDecisions()
452
+
453
+ const result = await collection.get({
454
+ ids: [id],
455
+ include: ['documents', 'metadatas']
456
+ })
457
+
458
+ if (result.ids.length === 0) {
459
+ return null
460
+ }
461
+
462
+ return {
463
+ id: result.ids[0],
464
+ decision: result.documents![0] as string,
465
+ metadata: result.metadatas![0] as Record<string, any>
466
+ }
467
+
468
+ } catch (error) {
469
+ this.logger.error({ error, id }, 'Failed to get decision')
470
+ throw error
471
+ }
472
+ }
473
+
474
+ async getDecisionsByProject(project: string): Promise<StoredDecision[]> {
475
+ try {
476
+ const collection = await this.collections.getDecisions()
477
+
478
+ const result = await collection.get({
479
+ where: { project },
480
+ include: ['documents', 'metadatas']
481
+ })
482
+
483
+ return result.ids.map((id, i) => ({
484
+ id,
485
+ decision: result.documents![i] as string,
486
+ metadata: result.metadatas![i] as Record<string, any>
487
+ }))
488
+
489
+ } catch (error) {
490
+ this.logger.error({ error, project }, 'Failed to get decisions by project')
491
+ throw error
492
+ }
493
+ }
494
+
495
+ async deleteDecision(id: string): Promise<void> {
496
+ try {
497
+ // Delete from decisions collection
498
+ const collection = await this.collections.getDecisions()
499
+ await collection.delete({ ids: [id] })
500
+
501
+ // Verify deletion succeeded
502
+ try {
503
+ const verify = await collection.get({ ids: [id] })
504
+ if (verify.ids.length > 0) {
505
+ this.logger.warn({ id }, 'Decision still exists after delete — retrying')
506
+ await collection.delete({ ids: [id] })
507
+ }
508
+ } catch {
509
+ // Verification query failed, assume delete succeeded
510
+ }
511
+
512
+ // ALSO delete from memories collection (dual storage uses same ID)
513
+ try {
514
+ const memoriesCollection = await this.collections.getMemories()
515
+ await memoriesCollection.delete({ ids: [id] })
516
+ this.logger.debug({ id }, 'Decision also deleted from memories collection')
517
+ } catch {
518
+ // Memories collection entry may not exist, that's ok
519
+ }
520
+
521
+ this.logger.info({ id }, 'Decision deleted from all collections')
522
+
523
+ } catch (error) {
524
+ this.logger.error({ error, id }, 'Failed to delete decision')
525
+ throw error
526
+ }
527
+ }
528
+
529
+ async getAllDecisions(): Promise<StoredDecision[]> {
530
+ try {
531
+ const collection = await this.collections.getDecisions()
532
+
533
+ const result = await collection.get({
534
+ include: ['documents', 'metadatas']
535
+ })
536
+
537
+ return result.ids.map((id, i) => ({
538
+ id,
539
+ decision: result.documents![i] as string,
540
+ metadata: result.metadatas![i] as Record<string, any>
541
+ }))
542
+
543
+ } catch (error) {
544
+ this.logger.error({ error }, 'Failed to get all decisions')
545
+ throw error
546
+ }
547
+ }
548
+
549
+ async getPatternsByProject(project: string, options?: {
550
+ pattern_type?: 'solution' | 'anti-pattern' | 'best-practice' | 'common-issue'
551
+ limit?: number
552
+ }): Promise<any[]> {
553
+ try {
554
+ const collection = await this.collections.getPatterns()
555
+
556
+ // Build where clause - handle single vs multiple conditions
557
+ let where: any
558
+ if (options?.pattern_type) {
559
+ // Multiple conditions: use $and operator
560
+ where = {
561
+ $and: [
562
+ { project: { $eq: project } },
563
+ { pattern_type: { $eq: options.pattern_type } }
564
+ ]
565
+ }
566
+ } else {
567
+ // Single condition
568
+ where = { project: { $eq: project } }
569
+ }
570
+
571
+ const result = await collection.get({
572
+ where,
573
+ include: ['documents', 'metadatas'],
574
+ ...(options?.limit ? { limit: options.limit } : {})
575
+ })
576
+
577
+ return result.ids.map((id, i) => ({
578
+ id,
579
+ description: result.documents![i] as string,
580
+ metadata: result.metadatas![i] as Record<string, any>
581
+ }))
582
+
583
+ } catch (error) {
584
+ this.logger.error({ error, project }, 'Failed to get patterns by project')
585
+ throw error
586
+ }
587
+ }
588
+
589
+ async getCorrectionsByProject(project: string, limit: number = 10): Promise<any[]> {
590
+ try {
591
+ const collection = await this.collections.getCorrections()
592
+
593
+ const result = await collection.get({
594
+ where: { project },
595
+ include: ['documents', 'metadatas'],
596
+ limit
597
+ })
598
+
599
+ return result.ids.map((id, i) => ({
600
+ id,
601
+ correction: result.documents![i] as string,
602
+ metadata: result.metadatas![i] as Record<string, any>
603
+ }))
604
+
605
+ } catch (error) {
606
+ this.logger.error({ error, project }, 'Failed to get corrections by project')
607
+ throw error
608
+ }
609
+ }
610
+
611
+ async searchPatterns(
612
+ query: string,
613
+ options: {
614
+ project?: string
615
+ pattern_type?: 'solution' | 'anti-pattern' | 'best-practice' | 'common-issue'
616
+ limit?: number
617
+ minSimilarity?: number
618
+ } = {}
619
+ ): Promise<SearchResult[]> {
620
+ const {
621
+ project,
622
+ pattern_type,
623
+ limit = 10,
624
+ minSimilarity = 0.5
625
+ } = options
626
+
627
+ try {
628
+ const collection = await this.collections.getPatterns()
629
+
630
+ const where: any = {}
631
+ if (project) {
632
+ where.project = project
633
+ }
634
+ if (pattern_type) {
635
+ where.pattern_type = pattern_type
636
+ }
637
+
638
+ let results: any
639
+
640
+ if (this.embeddings) {
641
+ const embedding = await this.embeddings.generate(query)
642
+ results = await collection.query({
643
+ queryEmbeddings: [embedding],
644
+ nResults: limit,
645
+ where: Object.keys(where).length > 0 ? where : undefined,
646
+ include: ['documents', 'metadatas', 'distances']
647
+ })
648
+ } else {
649
+ results = await collection.query({
650
+ queryTexts: [query],
651
+ nResults: limit,
652
+ where: Object.keys(where).length > 0 ? where : undefined,
653
+ include: ['documents', 'metadatas', 'distances']
654
+ })
655
+ }
656
+
657
+ return this.processResults(results, minSimilarity)
658
+
659
+ } catch (error) {
660
+ this.logger.error({ error, query }, 'Pattern search failed')
661
+ throw error
662
+ }
663
+ }
664
+
665
+ async searchCorrections(
666
+ query: string,
667
+ options: {
668
+ project?: string
669
+ limit?: number
670
+ minSimilarity?: number
671
+ } = {}
672
+ ): Promise<SearchResult[]> {
673
+ const {
674
+ project,
675
+ limit = 10,
676
+ minSimilarity = 0.5
677
+ } = options
678
+
679
+ try {
680
+ const collection = await this.collections.getCorrections()
681
+
682
+ const where: any = project ? { project } : {}
683
+
684
+ let results: any
685
+
686
+ if (this.embeddings) {
687
+ const embedding = await this.embeddings.generate(query)
688
+ results = await collection.query({
689
+ queryEmbeddings: [embedding],
690
+ nResults: limit,
691
+ where: Object.keys(where).length > 0 ? where : undefined,
692
+ include: ['documents', 'metadatas', 'distances']
693
+ })
694
+ } else {
695
+ results = await collection.query({
696
+ queryTexts: [query],
697
+ nResults: limit,
698
+ where: Object.keys(where).length > 0 ? where : undefined,
699
+ include: ['documents', 'metadatas', 'distances']
700
+ })
701
+ }
702
+
703
+ return this.processResults(results, minSimilarity)
704
+
705
+ } catch (error) {
706
+ this.logger.error({ error, query }, 'Correction search failed')
707
+ throw error
708
+ }
709
+ }
710
+
711
+ private processResults(
712
+ results: any,
713
+ minSimilarity: number
714
+ ): SearchResult[] {
715
+ if (!results.ids || results.ids.length === 0) {
716
+ return []
717
+ }
718
+
719
+ const processed: SearchResult[] = []
720
+
721
+ const ids = results.ids[0] || []
722
+ const documents = results.documents?.[0] || []
723
+ const metadatas = results.metadatas?.[0] || []
724
+ const distances = results.distances?.[0] || []
725
+
726
+ for (let i = 0; i < ids.length; i++) {
727
+ const similarity = 1 - (distances[i] || 0)
728
+
729
+ if (similarity >= minSimilarity) {
730
+ processed.push({
731
+ id: ids[i],
732
+ content: documents[i],
733
+ metadata: metadatas[i],
734
+ similarity
735
+ })
736
+ }
737
+ }
738
+
739
+ return processed.sort((a, b) => b.similarity - a.similarity)
740
+ }
741
+ }