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,413 +1,413 @@
1
- /**
2
- * Vault File Writer
3
- * Utilities for safely updating vault content with atomic writes and backups
4
- */
5
-
6
- import fs from 'fs/promises'
7
- import path from 'path'
8
- import matter from 'gray-matter'
9
- import type { Logger } from 'pino'
10
- import type { Frontmatter } from './types'
11
- import { TEMPLATES, renderTemplate, createProjectVariables, getTodayDate } from './templates'
12
- import { RetryManager } from '@/utils'
13
-
14
- export interface WriteOptions {
15
- createBackup?: boolean
16
- ensureDirectory?: boolean
17
- }
18
-
19
- export class VaultWriter {
20
- private logger: Logger
21
- private backupDir: string
22
- private retryManager: RetryManager
23
-
24
- constructor(logger: Logger, backupDir: string) {
25
- this.logger = logger.child({ component: 'vault-writer' })
26
- this.backupDir = backupDir
27
- this.retryManager = new RetryManager(logger)
28
- }
29
-
30
- /**
31
- * Write markdown file with frontmatter atomically
32
- * Falls back to direct write if rename fails (e.g., Obsidian has file open)
33
- */
34
- async writeMarkdownFile(
35
- filePath: string,
36
- frontmatter: Frontmatter,
37
- content: string,
38
- options: WriteOptions = {}
39
- ): Promise<void> {
40
- return this.retryManager.execute(
41
- async () => {
42
- const { createBackup = true, ensureDirectory = true } = options
43
-
44
- try {
45
- if (createBackup && (await this.fileExists(filePath))) {
46
- await this.createBackup(filePath)
47
- }
48
-
49
- const fileContent = matter.stringify(content, frontmatter)
50
-
51
- if (ensureDirectory) {
52
- await fs.mkdir(path.dirname(filePath), { recursive: true })
53
- }
54
-
55
- // Try atomic write first (temp file + rename)
56
- const tempPath = `${filePath}.tmp`
57
- try {
58
- await fs.writeFile(tempPath, fileContent, 'utf-8')
59
- await fs.rename(tempPath, filePath)
60
- } catch (renameError) {
61
- // If rename fails (EPERM - file locked by Obsidian), write directly
62
- const isPermError = (renameError as NodeJS.ErrnoException).code === 'EPERM'
63
- if (isPermError) {
64
- this.logger.warn({ filePath }, 'Atomic write failed (file locked), using direct write')
65
- await fs.writeFile(filePath, fileContent, 'utf-8')
66
- // Clean up temp file
67
- try {
68
- await fs.unlink(tempPath)
69
- } catch {}
70
- } else {
71
- throw renameError
72
- }
73
- }
74
-
75
- this.logger.info({ filePath }, 'File written successfully')
76
- } catch (error) {
77
- try {
78
- await fs.unlink(`${filePath}.tmp`)
79
- } catch {
80
- }
81
-
82
- this.logger.error({ error, filePath }, 'Failed to write file')
83
- throw new Error(`Failed to write file ${filePath}: ${error}`)
84
- }
85
- },
86
- {
87
- maxRetries: 3,
88
- retryCondition: RetryManager.isRetryableError,
89
- onRetry: (error, attempt) => {
90
- this.logger.warn(
91
- { error: error.message, attempt, filePath },
92
- 'Retrying file write'
93
- )
94
- }
95
- }
96
- )
97
- }
98
-
99
- /**
100
- * Update only the frontmatter of a file
101
- */
102
- async updateFrontmatter(
103
- filePath: string,
104
- updates: Partial<Frontmatter>
105
- ): Promise<void> {
106
- try {
107
- // Read existing file
108
- const rawContent = await fs.readFile(filePath, 'utf-8')
109
- const { data, content } = matter(rawContent)
110
-
111
- // Merge updates and update timestamp
112
- const newFrontmatter: Frontmatter = {
113
- ...(data as Frontmatter),
114
- ...updates,
115
- updated: getTodayDate()
116
- }
117
-
118
- // Write back
119
- await this.writeMarkdownFile(filePath, newFrontmatter, content, {
120
- createBackup: true
121
- })
122
-
123
- this.logger.info({ filePath, updates }, 'Frontmatter updated')
124
- } catch (error) {
125
- this.logger.error({ error, filePath }, 'Failed to update frontmatter')
126
- throw error
127
- }
128
- }
129
-
130
- /**
131
- * Append content to a file
132
- * Creates the file (and parent directories) if it doesn't exist
133
- */
134
- async appendContent(
135
- filePath: string,
136
- newContent: string,
137
- separator: string = '\n\n'
138
- ): Promise<void> {
139
- try {
140
- let rawContent: string
141
- try {
142
- rawContent = await fs.readFile(filePath, 'utf-8')
143
- } catch (readError: any) {
144
- if (readError.code === 'ENOENT') {
145
- // File doesn't exist — create it with the new content
146
- const today = getTodayDate()
147
- const frontmatter: Frontmatter = {
148
- created: today,
149
- updated: today
150
- }
151
- await this.writeMarkdownFile(filePath, frontmatter, newContent, {
152
- createBackup: false,
153
- ensureDirectory: true
154
- })
155
- this.logger.info({ filePath }, 'File created with initial content')
156
- return
157
- }
158
- throw readError
159
- }
160
-
161
- const { data, content } = matter(rawContent)
162
-
163
- // Append new content
164
- const updatedContent = content.trim() + separator + newContent
165
-
166
- // Update timestamp in frontmatter
167
- const today = getTodayDate()
168
- const updatedFrontmatter: Frontmatter = {
169
- ...(data as Frontmatter),
170
- updated: today,
171
- last_updated: today
172
- }
173
-
174
- // Write back
175
- await this.writeMarkdownFile(filePath, updatedFrontmatter, updatedContent, {
176
- createBackup: true
177
- })
178
-
179
- this.logger.info({ filePath }, 'Content appended')
180
- } catch (error) {
181
- this.logger.error({ error, filePath }, 'Failed to append content')
182
- throw error
183
- }
184
- }
185
-
186
- /**
187
- * Prepend content to a file (after frontmatter)
188
- */
189
- async prependContent(
190
- filePath: string,
191
- newContent: string,
192
- separator: string = '\n\n'
193
- ): Promise<void> {
194
- try {
195
- // Read existing file
196
- const rawContent = await fs.readFile(filePath, 'utf-8')
197
- const { data, content } = matter(rawContent)
198
-
199
- // Prepend new content
200
- const updatedContent = newContent + separator + content.trim()
201
-
202
- // Update timestamp in frontmatter
203
- const updatedFrontmatter: Frontmatter = {
204
- ...(data as Frontmatter),
205
- updated: getTodayDate()
206
- }
207
-
208
- // Write back
209
- await this.writeMarkdownFile(filePath, updatedFrontmatter, updatedContent, {
210
- createBackup: true
211
- })
212
-
213
- this.logger.info({ filePath }, 'Content prepended')
214
- } catch (error) {
215
- this.logger.error({ error, filePath }, 'Failed to prepend content')
216
- throw error
217
- }
218
- }
219
-
220
- /**
221
- * Replace a section in a file by header
222
- */
223
- async replaceSection(
224
- filePath: string,
225
- sectionHeader: string,
226
- newSectionContent: string
227
- ): Promise<boolean> {
228
- try {
229
- const rawContent = await fs.readFile(filePath, 'utf-8')
230
- const { data, content } = matter(rawContent)
231
-
232
- // Find the section using regex
233
- const headerPattern = new RegExp(
234
- `(^|\\n)(#{1,6}\\s*${this.escapeRegex(sectionHeader)}\\s*\\n)`,
235
- 'i'
236
- )
237
- const match = content.match(headerPattern)
238
-
239
- if (!match || !match[1] || !match[2]) {
240
- this.logger.warn({ filePath, sectionHeader }, 'Section not found')
241
- return false
242
- }
243
-
244
- // Find the end of the section (next header of same or higher level)
245
- const headerMatch = match[2].match(/^#+/)
246
- const headerLevel = headerMatch?.[0]?.length ?? 1
247
- const startIndex = (match.index ?? 0) + match[0].length
248
- const remainingContent = content.substring(startIndex)
249
-
250
- const nextHeaderPattern = new RegExp(`\\n#{1,${headerLevel}}\\s+`)
251
- const nextMatch = remainingContent.match(nextHeaderPattern)
252
- const endIndex = nextMatch
253
- ? startIndex + (nextMatch.index ?? 0)
254
- : content.length
255
-
256
- // Build new content
257
- const before = content.substring(0, (match.index ?? 0) + match[1].length)
258
- const sectionTitle = match[2]
259
- const after = content.substring(endIndex)
260
- const updatedContent = before + sectionTitle + newSectionContent + after
261
-
262
- // Update frontmatter timestamp
263
- const updatedFrontmatter: Frontmatter = {
264
- ...(data as Frontmatter),
265
- updated: getTodayDate()
266
- }
267
-
268
- await this.writeMarkdownFile(filePath, updatedFrontmatter, updatedContent, {
269
- createBackup: true
270
- })
271
-
272
- this.logger.info({ filePath, sectionHeader }, 'Section replaced')
273
- return true
274
- } catch (error) {
275
- this.logger.error({ error, filePath }, 'Failed to replace section')
276
- throw error
277
- }
278
- }
279
-
280
- /**
281
- * Create a backup of a file
282
- */
283
- private async createBackup(filePath: string): Promise<string | null> {
284
- try {
285
- const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
286
- const basename = path.basename(filePath)
287
- const backupPath = path.join(this.backupDir, `${basename}.${timestamp}.bak`)
288
-
289
- await fs.mkdir(this.backupDir, { recursive: true })
290
- await fs.copyFile(filePath, backupPath)
291
-
292
- this.logger.debug({ filePath, backupPath }, 'Backup created')
293
- return backupPath
294
- } catch (error) {
295
- this.logger.warn({ error, filePath }, 'Failed to create backup')
296
- // Don't throw - backup failure shouldn't prevent write
297
- return null
298
- }
299
- }
300
-
301
- /**
302
- * Check if file exists
303
- */
304
- private async fileExists(filePath: string): Promise<boolean> {
305
- try {
306
- await fs.access(filePath)
307
- return true
308
- } catch {
309
- return false
310
- }
311
- }
312
-
313
- /**
314
- * Delete a file
315
- */
316
- async deleteFile(
317
- filePath: string,
318
- createBackup: boolean = true
319
- ): Promise<void> {
320
- try {
321
- if (createBackup) {
322
- await this.createBackup(filePath)
323
- }
324
-
325
- await fs.unlink(filePath)
326
- this.logger.info({ filePath }, 'File deleted')
327
- } catch (error) {
328
- this.logger.error({ error, filePath }, 'Failed to delete file')
329
- throw error
330
- }
331
- }
332
-
333
- /**
334
- * Create directory structure for a new project
335
- */
336
- async createProjectStructure(
337
- projectPath: string,
338
- projectName: string
339
- ): Promise<void> {
340
- try {
341
- await fs.mkdir(projectPath, { recursive: true })
342
-
343
- const variables = createProjectVariables(projectName)
344
-
345
- // Create each file from template
346
- const files = [
347
- { name: 'context.md', template: TEMPLATES.PROJECT_CONTEXT },
348
- { name: 'decisions.md', template: TEMPLATES.DECISIONS_LOG },
349
- { name: 'progress.md', template: TEMPLATES.PROGRESS_TRACKER },
350
- { name: 'standards.md', template: TEMPLATES.CODING_STANDARDS },
351
- { name: 'patterns.md', template: TEMPLATES.PATTERNS_LOG },
352
- { name: 'corrections.md', template: TEMPLATES.CORRECTIONS_LOG }
353
- ]
354
-
355
- for (const file of files) {
356
- const content = renderTemplate(file.template, variables)
357
- const filePath = path.join(projectPath, file.name)
358
-
359
- // Parse the rendered template to separate frontmatter and content
360
- const { data, content: body } = matter(content)
361
-
362
- await this.writeMarkdownFile(filePath, data as Frontmatter, body, {
363
- createBackup: false,
364
- ensureDirectory: false
365
- })
366
- }
367
-
368
- this.logger.info({ projectPath, projectName }, 'Project structure created')
369
- } catch (error) {
370
- this.logger.error({ error, projectPath }, 'Failed to create project')
371
- throw error
372
- }
373
- }
374
-
375
- /**
376
- * Clean old backups (keep only recent ones)
377
- */
378
- async cleanOldBackups(maxAge: number = 7 * 24 * 60 * 60 * 1000): Promise<number> {
379
- try {
380
- const entries = await fs.readdir(this.backupDir, { withFileTypes: true })
381
- const now = Date.now()
382
- let cleaned = 0
383
-
384
- for (const entry of entries) {
385
- if (!entry.isFile() || !entry.name.endsWith('.bak')) continue
386
-
387
- const filePath = path.join(this.backupDir, entry.name)
388
- const stats = await fs.stat(filePath)
389
-
390
- if (now - stats.mtimeMs > maxAge) {
391
- await fs.unlink(filePath)
392
- cleaned++
393
- }
394
- }
395
-
396
- if (cleaned > 0) {
397
- this.logger.info({ cleaned }, 'Old backups cleaned')
398
- }
399
-
400
- return cleaned
401
- } catch (error) {
402
- this.logger.warn({ error }, 'Failed to clean old backups')
403
- return 0
404
- }
405
- }
406
-
407
- /**
408
- * Escape special regex characters
409
- */
410
- private escapeRegex(str: string): string {
411
- return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
412
- }
413
- }
1
+ /**
2
+ * Vault File Writer
3
+ * Utilities for safely updating vault content with atomic writes and backups
4
+ */
5
+
6
+ import fs from 'fs/promises'
7
+ import path from 'path'
8
+ import matter from 'gray-matter'
9
+ import type { Logger } from 'pino'
10
+ import type { Frontmatter } from './types'
11
+ import { TEMPLATES, renderTemplate, createProjectVariables, getTodayDate } from './templates'
12
+ import { RetryManager } from '@/utils'
13
+
14
+ export interface WriteOptions {
15
+ createBackup?: boolean
16
+ ensureDirectory?: boolean
17
+ }
18
+
19
+ export class VaultWriter {
20
+ private logger: Logger
21
+ private backupDir: string
22
+ private retryManager: RetryManager
23
+
24
+ constructor(logger: Logger, backupDir: string) {
25
+ this.logger = logger.child({ component: 'vault-writer' })
26
+ this.backupDir = backupDir
27
+ this.retryManager = new RetryManager(logger)
28
+ }
29
+
30
+ /**
31
+ * Write markdown file with frontmatter atomically
32
+ * Falls back to direct write if rename fails (e.g., Obsidian has file open)
33
+ */
34
+ async writeMarkdownFile(
35
+ filePath: string,
36
+ frontmatter: Frontmatter,
37
+ content: string,
38
+ options: WriteOptions = {}
39
+ ): Promise<void> {
40
+ return this.retryManager.execute(
41
+ async () => {
42
+ const { createBackup = true, ensureDirectory = true } = options
43
+
44
+ try {
45
+ if (createBackup && (await this.fileExists(filePath))) {
46
+ await this.createBackup(filePath)
47
+ }
48
+
49
+ const fileContent = matter.stringify(content, frontmatter)
50
+
51
+ if (ensureDirectory) {
52
+ await fs.mkdir(path.dirname(filePath), { recursive: true })
53
+ }
54
+
55
+ // Try atomic write first (temp file + rename)
56
+ const tempPath = `${filePath}.tmp`
57
+ try {
58
+ await fs.writeFile(tempPath, fileContent, 'utf-8')
59
+ await fs.rename(tempPath, filePath)
60
+ } catch (renameError) {
61
+ // If rename fails (EPERM - file locked by Obsidian), write directly
62
+ const isPermError = (renameError as NodeJS.ErrnoException).code === 'EPERM'
63
+ if (isPermError) {
64
+ this.logger.warn({ filePath }, 'Atomic write failed (file locked), using direct write')
65
+ await fs.writeFile(filePath, fileContent, 'utf-8')
66
+ // Clean up temp file
67
+ try {
68
+ await fs.unlink(tempPath)
69
+ } catch {}
70
+ } else {
71
+ throw renameError
72
+ }
73
+ }
74
+
75
+ this.logger.info({ filePath }, 'File written successfully')
76
+ } catch (error) {
77
+ try {
78
+ await fs.unlink(`${filePath}.tmp`)
79
+ } catch {
80
+ }
81
+
82
+ this.logger.error({ error, filePath }, 'Failed to write file')
83
+ throw new Error(`Failed to write file ${filePath}: ${error}`)
84
+ }
85
+ },
86
+ {
87
+ maxRetries: 3,
88
+ retryCondition: RetryManager.isRetryableError,
89
+ onRetry: (error, attempt) => {
90
+ this.logger.warn(
91
+ { error: error.message, attempt, filePath },
92
+ 'Retrying file write'
93
+ )
94
+ }
95
+ }
96
+ )
97
+ }
98
+
99
+ /**
100
+ * Update only the frontmatter of a file
101
+ */
102
+ async updateFrontmatter(
103
+ filePath: string,
104
+ updates: Partial<Frontmatter>
105
+ ): Promise<void> {
106
+ try {
107
+ // Read existing file
108
+ const rawContent = await fs.readFile(filePath, 'utf-8')
109
+ const { data, content } = matter(rawContent)
110
+
111
+ // Merge updates and update timestamp
112
+ const newFrontmatter: Frontmatter = {
113
+ ...(data as Frontmatter),
114
+ ...updates,
115
+ updated: getTodayDate()
116
+ }
117
+
118
+ // Write back
119
+ await this.writeMarkdownFile(filePath, newFrontmatter, content, {
120
+ createBackup: true
121
+ })
122
+
123
+ this.logger.info({ filePath, updates }, 'Frontmatter updated')
124
+ } catch (error) {
125
+ this.logger.error({ error, filePath }, 'Failed to update frontmatter')
126
+ throw error
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Append content to a file
132
+ * Creates the file (and parent directories) if it doesn't exist
133
+ */
134
+ async appendContent(
135
+ filePath: string,
136
+ newContent: string,
137
+ separator: string = '\n\n'
138
+ ): Promise<void> {
139
+ try {
140
+ let rawContent: string
141
+ try {
142
+ rawContent = await fs.readFile(filePath, 'utf-8')
143
+ } catch (readError: any) {
144
+ if (readError.code === 'ENOENT') {
145
+ // File doesn't exist — create it with the new content
146
+ const today = getTodayDate()
147
+ const frontmatter: Frontmatter = {
148
+ created: today,
149
+ updated: today
150
+ }
151
+ await this.writeMarkdownFile(filePath, frontmatter, newContent, {
152
+ createBackup: false,
153
+ ensureDirectory: true
154
+ })
155
+ this.logger.info({ filePath }, 'File created with initial content')
156
+ return
157
+ }
158
+ throw readError
159
+ }
160
+
161
+ const { data, content } = matter(rawContent)
162
+
163
+ // Append new content
164
+ const updatedContent = content.trim() + separator + newContent
165
+
166
+ // Update timestamp in frontmatter
167
+ const today = getTodayDate()
168
+ const updatedFrontmatter: Frontmatter = {
169
+ ...(data as Frontmatter),
170
+ updated: today,
171
+ last_updated: today
172
+ }
173
+
174
+ // Write back
175
+ await this.writeMarkdownFile(filePath, updatedFrontmatter, updatedContent, {
176
+ createBackup: true
177
+ })
178
+
179
+ this.logger.info({ filePath }, 'Content appended')
180
+ } catch (error) {
181
+ this.logger.error({ error, filePath }, 'Failed to append content')
182
+ throw error
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Prepend content to a file (after frontmatter)
188
+ */
189
+ async prependContent(
190
+ filePath: string,
191
+ newContent: string,
192
+ separator: string = '\n\n'
193
+ ): Promise<void> {
194
+ try {
195
+ // Read existing file
196
+ const rawContent = await fs.readFile(filePath, 'utf-8')
197
+ const { data, content } = matter(rawContent)
198
+
199
+ // Prepend new content
200
+ const updatedContent = newContent + separator + content.trim()
201
+
202
+ // Update timestamp in frontmatter
203
+ const updatedFrontmatter: Frontmatter = {
204
+ ...(data as Frontmatter),
205
+ updated: getTodayDate()
206
+ }
207
+
208
+ // Write back
209
+ await this.writeMarkdownFile(filePath, updatedFrontmatter, updatedContent, {
210
+ createBackup: true
211
+ })
212
+
213
+ this.logger.info({ filePath }, 'Content prepended')
214
+ } catch (error) {
215
+ this.logger.error({ error, filePath }, 'Failed to prepend content')
216
+ throw error
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Replace a section in a file by header
222
+ */
223
+ async replaceSection(
224
+ filePath: string,
225
+ sectionHeader: string,
226
+ newSectionContent: string
227
+ ): Promise<boolean> {
228
+ try {
229
+ const rawContent = await fs.readFile(filePath, 'utf-8')
230
+ const { data, content } = matter(rawContent)
231
+
232
+ // Find the section using regex
233
+ const headerPattern = new RegExp(
234
+ `(^|\\n)(#{1,6}\\s*${this.escapeRegex(sectionHeader)}\\s*\\n)`,
235
+ 'i'
236
+ )
237
+ const match = content.match(headerPattern)
238
+
239
+ if (!match || !match[1] || !match[2]) {
240
+ this.logger.warn({ filePath, sectionHeader }, 'Section not found')
241
+ return false
242
+ }
243
+
244
+ // Find the end of the section (next header of same or higher level)
245
+ const headerMatch = match[2].match(/^#+/)
246
+ const headerLevel = headerMatch?.[0]?.length ?? 1
247
+ const startIndex = (match.index ?? 0) + match[0].length
248
+ const remainingContent = content.substring(startIndex)
249
+
250
+ const nextHeaderPattern = new RegExp(`\\n#{1,${headerLevel}}\\s+`)
251
+ const nextMatch = remainingContent.match(nextHeaderPattern)
252
+ const endIndex = nextMatch
253
+ ? startIndex + (nextMatch.index ?? 0)
254
+ : content.length
255
+
256
+ // Build new content
257
+ const before = content.substring(0, (match.index ?? 0) + match[1].length)
258
+ const sectionTitle = match[2]
259
+ const after = content.substring(endIndex)
260
+ const updatedContent = before + sectionTitle + newSectionContent + after
261
+
262
+ // Update frontmatter timestamp
263
+ const updatedFrontmatter: Frontmatter = {
264
+ ...(data as Frontmatter),
265
+ updated: getTodayDate()
266
+ }
267
+
268
+ await this.writeMarkdownFile(filePath, updatedFrontmatter, updatedContent, {
269
+ createBackup: true
270
+ })
271
+
272
+ this.logger.info({ filePath, sectionHeader }, 'Section replaced')
273
+ return true
274
+ } catch (error) {
275
+ this.logger.error({ error, filePath }, 'Failed to replace section')
276
+ throw error
277
+ }
278
+ }
279
+
280
+ /**
281
+ * Create a backup of a file
282
+ */
283
+ private async createBackup(filePath: string): Promise<string | null> {
284
+ try {
285
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
286
+ const basename = path.basename(filePath)
287
+ const backupPath = path.join(this.backupDir, `${basename}.${timestamp}.bak`)
288
+
289
+ await fs.mkdir(this.backupDir, { recursive: true })
290
+ await fs.copyFile(filePath, backupPath)
291
+
292
+ this.logger.debug({ filePath, backupPath }, 'Backup created')
293
+ return backupPath
294
+ } catch (error) {
295
+ this.logger.warn({ error, filePath }, 'Failed to create backup')
296
+ // Don't throw - backup failure shouldn't prevent write
297
+ return null
298
+ }
299
+ }
300
+
301
+ /**
302
+ * Check if file exists
303
+ */
304
+ private async fileExists(filePath: string): Promise<boolean> {
305
+ try {
306
+ await fs.access(filePath)
307
+ return true
308
+ } catch {
309
+ return false
310
+ }
311
+ }
312
+
313
+ /**
314
+ * Delete a file
315
+ */
316
+ async deleteFile(
317
+ filePath: string,
318
+ createBackup: boolean = true
319
+ ): Promise<void> {
320
+ try {
321
+ if (createBackup) {
322
+ await this.createBackup(filePath)
323
+ }
324
+
325
+ await fs.unlink(filePath)
326
+ this.logger.info({ filePath }, 'File deleted')
327
+ } catch (error) {
328
+ this.logger.error({ error, filePath }, 'Failed to delete file')
329
+ throw error
330
+ }
331
+ }
332
+
333
+ /**
334
+ * Create directory structure for a new project
335
+ */
336
+ async createProjectStructure(
337
+ projectPath: string,
338
+ projectName: string
339
+ ): Promise<void> {
340
+ try {
341
+ await fs.mkdir(projectPath, { recursive: true })
342
+
343
+ const variables = createProjectVariables(projectName)
344
+
345
+ // Create each file from template
346
+ const files = [
347
+ { name: 'context.md', template: TEMPLATES.PROJECT_CONTEXT },
348
+ { name: 'decisions.md', template: TEMPLATES.DECISIONS_LOG },
349
+ { name: 'progress.md', template: TEMPLATES.PROGRESS_TRACKER },
350
+ { name: 'standards.md', template: TEMPLATES.CODING_STANDARDS },
351
+ { name: 'patterns.md', template: TEMPLATES.PATTERNS_LOG },
352
+ { name: 'corrections.md', template: TEMPLATES.CORRECTIONS_LOG }
353
+ ]
354
+
355
+ for (const file of files) {
356
+ const content = renderTemplate(file.template, variables)
357
+ const filePath = path.join(projectPath, file.name)
358
+
359
+ // Parse the rendered template to separate frontmatter and content
360
+ const { data, content: body } = matter(content)
361
+
362
+ await this.writeMarkdownFile(filePath, data as Frontmatter, body, {
363
+ createBackup: false,
364
+ ensureDirectory: false
365
+ })
366
+ }
367
+
368
+ this.logger.info({ projectPath, projectName }, 'Project structure created')
369
+ } catch (error) {
370
+ this.logger.error({ error, projectPath }, 'Failed to create project')
371
+ throw error
372
+ }
373
+ }
374
+
375
+ /**
376
+ * Clean old backups (keep only recent ones)
377
+ */
378
+ async cleanOldBackups(maxAge: number = 7 * 24 * 60 * 60 * 1000): Promise<number> {
379
+ try {
380
+ const entries = await fs.readdir(this.backupDir, { withFileTypes: true })
381
+ const now = Date.now()
382
+ let cleaned = 0
383
+
384
+ for (const entry of entries) {
385
+ if (!entry.isFile() || !entry.name.endsWith('.bak')) continue
386
+
387
+ const filePath = path.join(this.backupDir, entry.name)
388
+ const stats = await fs.stat(filePath)
389
+
390
+ if (now - stats.mtimeMs > maxAge) {
391
+ await fs.unlink(filePath)
392
+ cleaned++
393
+ }
394
+ }
395
+
396
+ if (cleaned > 0) {
397
+ this.logger.info({ cleaned }, 'Old backups cleaned')
398
+ }
399
+
400
+ return cleaned
401
+ } catch (error) {
402
+ this.logger.warn({ error }, 'Failed to clean old backups')
403
+ return 0
404
+ }
405
+ }
406
+
407
+ /**
408
+ * Escape special regex characters
409
+ */
410
+ private escapeRegex(str: string): string {
411
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
412
+ }
413
+ }