mindlore 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +1 -1
  2. package/dist/scripts/init.js +45 -3
  3. package/dist/scripts/init.js.map +1 -1
  4. package/dist/scripts/lib/constants.d.ts +11 -0
  5. package/dist/scripts/lib/constants.d.ts.map +1 -1
  6. package/dist/scripts/lib/constants.js +12 -1
  7. package/dist/scripts/lib/constants.js.map +1 -1
  8. package/dist/scripts/lib/migrations-v051.d.ts +3 -0
  9. package/dist/scripts/lib/migrations-v051.d.ts.map +1 -0
  10. package/dist/scripts/lib/migrations-v051.js +24 -0
  11. package/dist/scripts/lib/migrations-v051.js.map +1 -0
  12. package/dist/scripts/lib/migrations.d.ts +1 -0
  13. package/dist/scripts/lib/migrations.d.ts.map +1 -1
  14. package/dist/scripts/lib/migrations.js +3 -1
  15. package/dist/scripts/lib/migrations.js.map +1 -1
  16. package/dist/scripts/lib/privacy-filter.d.ts +3 -0
  17. package/dist/scripts/lib/privacy-filter.d.ts.map +1 -0
  18. package/dist/scripts/lib/privacy-filter.js +28 -0
  19. package/dist/scripts/lib/privacy-filter.js.map +1 -0
  20. package/dist/scripts/lib/similarity.d.ts +12 -0
  21. package/dist/scripts/lib/similarity.d.ts.map +1 -0
  22. package/dist/scripts/lib/similarity.js +64 -0
  23. package/dist/scripts/lib/similarity.js.map +1 -0
  24. package/dist/scripts/mindlore-fts5-index.js +17 -3
  25. package/dist/scripts/mindlore-fts5-index.js.map +1 -1
  26. package/dist/scripts/mindlore-health-check.js +53 -0
  27. package/dist/scripts/mindlore-health-check.js.map +1 -1
  28. package/dist/scripts/quality-populate.js +8 -4
  29. package/dist/scripts/quality-populate.js.map +1 -1
  30. package/dist/tests/cc-memory-sync.test.d.ts +2 -0
  31. package/dist/tests/cc-memory-sync.test.d.ts.map +1 -0
  32. package/dist/tests/cc-memory-sync.test.js +121 -0
  33. package/dist/tests/cc-memory-sync.test.js.map +1 -0
  34. package/dist/tests/episode-file.test.d.ts +2 -0
  35. package/dist/tests/episode-file.test.d.ts.map +1 -0
  36. package/dist/tests/episode-file.test.js +79 -0
  37. package/dist/tests/episode-file.test.js.map +1 -0
  38. package/dist/tests/helpers/db.d.ts +1 -0
  39. package/dist/tests/helpers/db.d.ts.map +1 -1
  40. package/dist/tests/helpers/db.js +10 -0
  41. package/dist/tests/helpers/db.js.map +1 -1
  42. package/dist/tests/hook-logging.test.d.ts +2 -0
  43. package/dist/tests/hook-logging.test.d.ts.map +1 -0
  44. package/dist/tests/hook-logging.test.js +108 -0
  45. package/dist/tests/hook-logging.test.js.map +1 -0
  46. package/dist/tests/index-cli-embed.test.d.ts +7 -0
  47. package/dist/tests/index-cli-embed.test.d.ts.map +1 -0
  48. package/dist/tests/index-cli-embed.test.js +128 -0
  49. package/dist/tests/index-cli-embed.test.js.map +1 -0
  50. package/dist/tests/privacy-filter.test.d.ts +2 -0
  51. package/dist/tests/privacy-filter.test.d.ts.map +1 -0
  52. package/dist/tests/privacy-filter.test.js +56 -0
  53. package/dist/tests/privacy-filter.test.js.map +1 -0
  54. package/dist/tests/schema-version.test.js +28 -0
  55. package/dist/tests/schema-version.test.js.map +1 -1
  56. package/dist/tests/search-cli-hybrid.test.d.ts +6 -0
  57. package/dist/tests/search-cli-hybrid.test.d.ts.map +1 -0
  58. package/dist/tests/search-cli-hybrid.test.js +103 -0
  59. package/dist/tests/search-cli-hybrid.test.js.map +1 -0
  60. package/dist/tests/search-hook.test.js +33 -0
  61. package/dist/tests/search-hook.test.js.map +1 -1
  62. package/dist/tests/similarity.test.d.ts +2 -0
  63. package/dist/tests/similarity.test.d.ts.map +1 -0
  64. package/dist/tests/similarity.test.js +61 -0
  65. package/dist/tests/similarity.test.js.map +1 -0
  66. package/dist/tests/token-budget.test.d.ts +2 -0
  67. package/dist/tests/token-budget.test.d.ts.map +1 -0
  68. package/dist/tests/token-budget.test.js +32 -0
  69. package/dist/tests/token-budget.test.js.map +1 -0
  70. package/hooks/lib/mindlore-common.cjs +88 -0
  71. package/hooks/mindlore-index.cjs +82 -2
  72. package/hooks/mindlore-search.cjs +49 -39
  73. package/hooks/mindlore-session-end.cjs +129 -39
  74. package/hooks/mindlore-session-focus.cjs +24 -3
  75. package/package.json +4 -4
  76. package/plugin.json +1 -1
  77. package/skills/mindlore-ingest/SKILL.md +7 -1
  78. package/templates/config.json +6 -1
@@ -590,6 +590,20 @@ function sanitizeKeyword(kw) {
590
590
  return clean.length >= 2 ? `"${clean}"` : null;
591
591
  }
592
592
 
593
+ // Matches DIRECTORIES in scripts/lib/constants.ts + 'memory' (v0.5.1 CC memory sync)
594
+ const SHARED_EXPORT_DIRS = ['raw', 'sources', 'domains', 'analyses', 'insights', 'connections', 'learnings', 'diary', 'decisions', 'memory'];
595
+
596
+ function resolveWin32Bin(name) {
597
+ if (process.platform === 'win32') {
598
+ try {
599
+ return require('child_process')
600
+ .execSync(`where ${name}`, { encoding: 'utf8', timeout: 3000 })
601
+ .trim().split('\n')[0].trim();
602
+ } catch (_e) { /* fall through */ }
603
+ }
604
+ return name;
605
+ }
606
+
593
607
  module.exports = {
594
608
  MINDLORE_DIR,
595
609
  GLOBAL_MINDLORE_DIR,
@@ -636,6 +650,12 @@ module.exports = {
636
650
  // Hybrid search helpers (v0.5.0)
637
651
  loadSqliteVecCjs,
638
652
  hasVecTableCjs,
653
+ // Hook logging (v0.5.1)
654
+ hookLog,
655
+ getRecentHookErrors,
656
+ // Shared helpers (v0.5.1)
657
+ SHARED_EXPORT_DIRS,
658
+ resolveWin32Bin,
639
659
  };
640
660
 
641
661
  /**
@@ -666,3 +686,71 @@ function hasVecTableCjs(db) {
666
686
  return false;
667
687
  }
668
688
  }
689
+
690
+ // --- Hook Logging (v0.5.1) ---
691
+
692
+ function hookLogPath() { return path.join(globalDir(), 'diary', '_hook-log.jsonl'); }
693
+
694
+ /**
695
+ * Append a structured log entry for any mindlore hook.
696
+ * JSONL format — one JSON object per line.
697
+ * Levels: 'info' | 'warn' | 'error'
698
+ * @param {string} hook - Hook name (e.g. 'session-end', 'search', 'read-guard')
699
+ * @param {'info'|'warn'|'error'} level
700
+ * @param {string} message
701
+ */
702
+ const HOOK_LOG_MAX_BYTES = 512 * 1024; // 500KB
703
+ const HOOK_LOG_KEEP_LINES = 500;
704
+
705
+ function hookLog(hook, level, message) {
706
+ try {
707
+ const logFile = hookLogPath();
708
+ const entry = JSON.stringify({
709
+ ts: new Date().toISOString(),
710
+ hook,
711
+ level,
712
+ msg: message,
713
+ pid: process.pid,
714
+ });
715
+ // Rotate if file exceeds threshold
716
+ try {
717
+ const stat = fs.statSync(logFile);
718
+ if (stat.size > HOOK_LOG_MAX_BYTES) {
719
+ const lines = fs.readFileSync(logFile, 'utf8').trim().split('\n');
720
+ fs.writeFileSync(logFile, lines.slice(-HOOK_LOG_KEEP_LINES).join('\n') + '\n');
721
+ }
722
+ } catch (_rotateErr) { /* file may not exist yet */ }
723
+ fs.appendFileSync(logFile, entry + '\n');
724
+ } catch (_err) {
725
+ // Best effort — never crash a hook for logging
726
+ }
727
+ }
728
+
729
+ /**
730
+ * Read recent hook errors/warnings since a given ISO date.
731
+ * Returns array of { ts, hook, level, msg } for level 'error' or 'warn'.
732
+ * Used by SessionStart to inject warnings into CC context.
733
+ * @param {string} [since] - ISO date string, defaults to 24h ago
734
+ * @param {number} [limit=10]
735
+ * @returns {Array<{ts: string, hook: string, level: string, msg: string}>}
736
+ */
737
+ function getRecentHookErrors(since, limit) {
738
+ const maxEntries = limit ?? 10;
739
+ const cutoff = since ?? new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();
740
+ const results = [];
741
+ try {
742
+ if (!fs.existsSync(hookLogPath())) return results;
743
+ const lines = fs.readFileSync(hookLogPath(), 'utf8').trim().split('\n');
744
+ for (let i = lines.length - 1; i >= 0 && results.length < maxEntries; i--) {
745
+ if (!lines[i]) continue;
746
+ try {
747
+ const entry = JSON.parse(lines[i]);
748
+ if (entry.ts < cutoff) break; // JSONL is chronological, stop early
749
+ if (entry.level === 'error' || entry.level === 'warn') {
750
+ results.push(entry);
751
+ }
752
+ } catch (_parseErr) { /* skip malformed lines */ }
753
+ }
754
+ } catch (_err) { /* silent */ }
755
+ return results.reverse(); // chronological order
756
+ }
@@ -10,7 +10,7 @@
10
10
 
11
11
  const fs = require('fs');
12
12
  const path = require('path');
13
- const { MINDLORE_DIR, DB_NAME, SKIP_FILES, sha256, openDatabase, parseFrontmatter, extractFtsMetadata, insertFtsRow, readHookStdin, getProjectName } = require('./lib/mindlore-common.cjs');
13
+ const { MINDLORE_DIR, DB_NAME, SKIP_FILES, sha256, openDatabase, parseFrontmatter, extractFtsMetadata, insertFtsRow, readHookStdin, getProjectName, globalDir } = require('./lib/mindlore-common.cjs');
14
14
 
15
15
  function main() {
16
16
  const filePath = readHookStdin(['path', 'file_path']);
@@ -19,7 +19,17 @@ function main() {
19
19
  // Only process .md files inside .mindlore/ (resolved path check prevents traversal)
20
20
  if (!filePath.endsWith('.md')) return;
21
21
  const resolvedFile = path.resolve(filePath);
22
- if (!resolvedFile.includes(path.sep + MINDLORE_DIR + path.sep) && !resolvedFile.includes(path.sep + MINDLORE_DIR)) return;
22
+ if (!resolvedFile.includes(path.sep + MINDLORE_DIR + path.sep) && !resolvedFile.includes(path.sep + MINDLORE_DIR)) {
23
+ // CC memory path (~/.claude/projects/*/memory/*.md) — index to global mindlore DB
24
+ const isCcMemory = resolvedFile.includes(path.sep + '.claude' + path.sep + 'projects' + path.sep)
25
+ && resolvedFile.includes(path.sep + 'memory' + path.sep)
26
+ && resolvedFile.endsWith('.md');
27
+ if (!isCcMemory) return;
28
+
29
+ // CC memory path — index to global mindlore DB
30
+ indexCcMemory(resolvedFile);
31
+ return;
32
+ }
23
33
 
24
34
  const fileName = path.basename(filePath);
25
35
  if (SKIP_FILES.has(fileName)) return;
@@ -80,4 +90,74 @@ function main() {
80
90
  }
81
91
  }
82
92
 
93
+ function indexCcMemory(filePath) {
94
+ const CC_MEMORY_CATEGORY = 'cc-memory';
95
+ // CC memory constants live in TS (scripts/lib/constants.ts) — CJS hooks can't require TS directly
96
+ const globalBase = globalDir();
97
+ const dbPath = path.join(globalBase, DB_NAME);
98
+
99
+ const content = fs.readFileSync(filePath, 'utf8').replace(/\r\n/g, '\n');
100
+ if (!content.trim()) return;
101
+
102
+ // Privacy filter — redact secrets before DB write
103
+ let cleaned = content;
104
+ try {
105
+ const { redactSecrets } = require('../dist/scripts/lib/privacy-filter.js');
106
+ cleaned = redactSecrets(content);
107
+ } catch (_err) {
108
+ // privacy-filter not built — use raw content
109
+ }
110
+
111
+ // SHA256 dedup
112
+ const hash = sha256(cleaned);
113
+ const db = openDatabase(dbPath);
114
+ if (!db) return;
115
+
116
+ try {
117
+ const existing = db.prepare('SELECT content_hash FROM file_hashes WHERE path = ?').get(filePath);
118
+ if (existing && existing.content_hash === hash) {
119
+ return; // unchanged — finally handles db.close()
120
+ }
121
+
122
+ const { meta, body } = parseFrontmatter(cleaned);
123
+ const memType = String(meta.type || 'unknown');
124
+
125
+ // Extract project scope from path: ~/.claude/projects/C--Users-X-proj/memory/
126
+ const projMatch = filePath.match(/projects[/\\]([^/\\]+)[/\\]memory/);
127
+ const projectScope = projMatch ? projMatch[1] : null;
128
+
129
+ const ftsData = extractFtsMetadata(meta, body, filePath, globalBase);
130
+
131
+ // Update FTS5 + hash atomically
132
+ const updateIndex = db.transaction(() => {
133
+ db.prepare('DELETE FROM mindlore_fts WHERE path = ?').run(filePath);
134
+ insertFtsRow(db, {
135
+ path: filePath,
136
+ ...ftsData,
137
+ category: CC_MEMORY_CATEGORY,
138
+ type: memType,
139
+ project: projectScope,
140
+ });
141
+ db.prepare(
142
+ `INSERT INTO file_hashes (path, content_hash, last_indexed, source_type, project_scope)
143
+ VALUES (?, ?, ?, ?, ?)
144
+ ON CONFLICT(path) DO UPDATE SET
145
+ content_hash = excluded.content_hash,
146
+ last_indexed = excluded.last_indexed,
147
+ source_type = excluded.source_type,
148
+ project_scope = excluded.project_scope`
149
+ ).run(filePath, hash, new Date().toISOString(), CC_MEMORY_CATEGORY, projectScope);
150
+ });
151
+ updateIndex();
152
+
153
+ // Copy to ~/.mindlore/memory/{project}/ for git-sync + obsidian
154
+ const memoryDir = path.join(globalBase, 'memory', projectScope || '_global');
155
+ fs.mkdirSync(memoryDir, { recursive: true });
156
+ const destPath = path.join(memoryDir, path.basename(filePath));
157
+ fs.writeFileSync(destPath, cleaned, 'utf8');
158
+ } finally {
159
+ db.close();
160
+ }
161
+ }
162
+
83
163
  main();
@@ -10,11 +10,10 @@
10
10
 
11
11
  const fs = require('fs');
12
12
  const path = require('path');
13
- const { getAllDbs, requireDatabase, extractHeadings, readHookStdin, extractKeywords, readConfig, loadSqliteVecCjs, hasVecTableCjs } = require('./lib/mindlore-common.cjs');
13
+ const { getAllDbs, requireDatabase, extractHeadings, readHookStdin, extractKeywords, sanitizeKeyword, readConfig, loadSqliteVecCjs, hasVecTableCjs, hookLog } = require('./lib/mindlore-common.cjs');
14
14
 
15
15
  const MAX_RESULTS = 3;
16
16
  const MIN_QUERY_WORDS = 3;
17
- const MIN_KEYWORD_HITS = 2;
18
17
 
19
18
  // Try to load hybrid search module (built TS)
20
19
  let hybridSearchMod;
@@ -81,35 +80,30 @@ function searchDb(dbPath, keywords, Database) {
81
80
  }
82
81
  }
83
82
 
83
+ // FTS5-only fallback: OR-joined single query (replaces O(docs×keywords) nested loop)
84
84
  try {
85
- const allPaths = db.prepare('SELECT DISTINCT path FROM mindlore_fts').all();
86
- const matchStmt = db.prepare('SELECT rank FROM mindlore_fts WHERE path = ? AND mindlore_fts MATCH ?');
87
- const metaStmt = db.prepare(
88
- 'SELECT slug, description, category, title, tags FROM mindlore_fts WHERE path = ?'
89
- );
85
+ const sanitized = keywords.map(sanitizeKeyword).filter(Boolean);
86
+ if (sanitized.length === 0) { db.close(); return results; }
90
87
 
91
- for (const row of allPaths) {
92
- let hits = 0;
93
- let totalRank = 0;
94
-
95
- for (const kw of keywords) {
96
- try {
97
- const sanitized = kw.replace(/["*(){}[\]^~:]/g, '');
98
- if (!sanitized) continue;
99
- const r = matchStmt.get(row.path, '"' + sanitized + '"');
100
- if (r) {
101
- hits++;
102
- totalRank += r.rank;
103
- }
104
- } catch (_err) {
105
- // FTS5 query error for this keyword — skip
106
- }
107
- }
108
-
109
- if (hits >= MIN_KEYWORD_HITS) {
110
- const meta = metaStmt.get(row.path) || {};
111
- results.push({ path: row.path, hits, totalRank, baseDir, meta });
112
- }
88
+ const ftsQuery = sanitized.join(' OR ');
89
+ const rows = db.prepare(
90
+ `SELECT path, slug, description, category, title, tags, rank
91
+ FROM mindlore_fts WHERE mindlore_fts MATCH ? ORDER BY rank LIMIT ?`
92
+ ).all(ftsQuery, MAX_RESULTS * 2);
93
+
94
+ for (const r of rows) {
95
+ results.push({
96
+ path: r.path || '',
97
+ slug: r.slug,
98
+ description: r.description || '',
99
+ category: r.category || '',
100
+ title: r.title || '',
101
+ tags: r.tags || '',
102
+ headings: [], // populated later in main() after slicing
103
+ hits: sanitized.length,
104
+ rank: r.rank,
105
+ baseDir,
106
+ });
113
107
  }
114
108
  } catch (_err) {
115
109
  // FTS5 query error — silently skip
@@ -126,7 +120,7 @@ function searchDb(dbPath, keywords, Database) {
126
120
  */
127
121
  function searchEpisodesFts(db, keywords) {
128
122
  try {
129
- const ftsQuery = keywords.map(kw => '"' + kw.replace(/["*(){}[\]^~:]/g, '') + '"').filter(q => q !== '""').join(' OR ');
123
+ const ftsQuery = keywords.map(sanitizeKeyword).filter(Boolean).join(' OR ');
130
124
  const rows = db.prepare(
131
125
  "SELECT title, category, slug, tags FROM mindlore_fts WHERE type = 'episode' AND mindlore_fts MATCH ? LIMIT 2"
132
126
  ).all(ftsQuery);
@@ -176,17 +170,32 @@ function main() {
176
170
  const relevant = unique.slice(0, MAX_RESULTS);
177
171
  if (relevant.length === 0) return;
178
172
 
173
+ // Populate headings only for final results (avoid reading extra files)
174
+ for (const r of relevant) {
175
+ if (r.path && r.headings.length === 0 && fs.existsSync(r.path)) {
176
+ try {
177
+ const content = fs.readFileSync(r.path, 'utf8');
178
+ r.headings = extractHeadings(content, 3);
179
+ } catch (_err) { /* skip */ }
180
+ }
181
+ }
182
+
183
+ // Token budget from config
184
+ const config = readConfig(path.dirname(dbPaths[0]));
185
+ const budget = (config && config.tokenBudget) || {};
186
+ // Defaults match DEFAULT_TOKEN_BUDGET in scripts/lib/constants.ts
187
+ const perResultChars = ((budget.perResult || 500) * 4); // ~4 chars/token
188
+ const totalChars = ((budget.searchResults || 1500) * 4);
189
+
179
190
  // Build rich inject output
180
191
  const output = [];
192
+ let totalUsed = 0;
181
193
  for (const r of relevant) {
194
+ if (totalUsed >= totalChars) break;
182
195
  const meta = r.meta || {};
183
196
  const relativePath = path.relative(r.baseDir, r.path).replace(/\\/g, '/');
184
197
 
185
- let headings = [];
186
- if (fs.existsSync(r.path)) {
187
- const content = fs.readFileSync(r.path, 'utf8');
188
- headings = extractHeadings(content, 5);
189
- }
198
+ const headings = r.headings || [];
190
199
 
191
200
  const category = meta.category || path.dirname(relativePath).split('/')[0];
192
201
  const title = meta.title || meta.slug || path.basename(r.path, '.md');
@@ -194,9 +203,10 @@ function main() {
194
203
 
195
204
  const headingStr = headings.length > 0 ? `\nBasliklar: ${headings.join(', ')}` : '';
196
205
  const tagsStr = meta.tags ? `\nTags: ${meta.tags}` : '';
197
- output.push(
198
- `[Mindlore: ${category}/${title}] ${description}\nDosya: ${relativePath}${tagsStr}${headingStr}`
199
- );
206
+ const entry = `[Mindlore: ${category}/${title}] ${description}\nDosya: ${relativePath}${tagsStr}${headingStr}`;
207
+ const truncated = entry.slice(0, perResultChars);
208
+ totalUsed += truncated.length;
209
+ output.push(truncated);
200
210
  }
201
211
 
202
212
  // v0.4.0: Search episode mirrors in FTS5 (reuses searchDb's DB path, no extra open)
@@ -219,4 +229,4 @@ function main() {
219
229
  }
220
230
  }
221
231
 
222
- main();
232
+ try { main(); } catch (err) { hookLog('search', 'error', err?.message ?? String(err)); }
@@ -13,22 +13,48 @@ const fs = require('fs');
13
13
  const path = require('path');
14
14
  const os = require('os');
15
15
  const { execSync, spawn } = require('child_process');
16
- const { findMindloreDir, globalDir, getProjectName, openDatabase, ensureEpisodesTable, hasEpisodesTable, insertBareEpisode, insertFtsRow } = require('./lib/mindlore-common.cjs');
16
+ const { findMindloreDir, globalDir, getProjectName, openDatabase, ensureEpisodesTable, hasEpisodesTable, insertBareEpisode, insertFtsRow, hookLog, SHARED_EXPORT_DIRS, resolveWin32Bin } = require('./lib/mindlore-common.cjs');
17
+
18
+ const EXPORT_DIRS = SHARED_EXPORT_DIRS;
17
19
 
18
20
  // --worker mode: heavy ops run in detached child process (survives parent exit)
19
21
  if (process.argv.includes('--worker')) {
22
+ hookLog('session-end', 'info', 'worker started, pid=' + process.pid);
20
23
  const dataPath = process.argv[process.argv.indexOf('--worker') + 1];
24
+ let payload;
21
25
  try {
22
26
  const raw = fs.readFileSync(dataPath, 'utf8');
23
- fs.unlinkSync(dataPath); // cleanup temp file before any processing
24
- const { baseDir, project, commits, changedFiles, reads } = JSON.parse(raw);
25
- writeBareEpisode(baseDir, project, commits, changedFiles, reads);
26
- syncObsidian(baseDir);
27
- syncGlobalRepo();
27
+ fs.unlinkSync(dataPath);
28
+ payload = JSON.parse(raw);
28
29
  } catch (_err) {
29
- // Graceful fail worker errors are silent
30
+ hookLog('session-end', 'error', 'payload read failed: ' + (_err?.message ?? _err));
31
+ process.exit(0);
30
32
  }
31
- process.exit(0);
33
+ const { baseDir, project, commits, changedFiles, reads } = payload;
34
+
35
+ async function safeRunAsync(fn, label) {
36
+ try {
37
+ await fn();
38
+ hookLog('session-end', 'info', label + ' OK');
39
+ } catch (e) {
40
+ hookLog('session-end', 'error', label + ' FAIL: ' + e?.message);
41
+ }
42
+ }
43
+
44
+ (async () => {
45
+ // Episode writes share DB — run sequentially first
46
+ await safeRunAsync(() => writeBareEpisode(baseDir, project, commits, changedFiles, reads), 'episode');
47
+ await safeRunAsync(() => writeEpisodeFile(baseDir, project, commits, changedFiles, reads), 'episode-file');
48
+
49
+ // Obsidian + git-sync are independent — run in parallel
50
+ await Promise.allSettled([
51
+ safeRunAsync(() => syncObsidian(baseDir), 'obsidian'),
52
+ safeRunAsync(() => syncGlobalRepo(), 'git-sync'),
53
+ ]);
54
+
55
+ hookLog('session-end', 'info', 'worker done');
56
+ process.exit(0);
57
+ })();
32
58
  }
33
59
 
34
60
  function formatDate(date) {
@@ -47,9 +73,10 @@ function formatDate(date) {
47
73
  function getRecentGitInfo() {
48
74
  try {
49
75
  // --name-only includes file names after each commit entry
50
- const raw = execSync('git log --oneline -5 --name-only 2>/dev/null', {
76
+ const raw = execSync('git log --oneline -5 --name-only', {
51
77
  encoding: 'utf8',
52
78
  timeout: 5000,
79
+ stdio: ['pipe', 'pipe', 'pipe'],
53
80
  }).trim();
54
81
  if (!raw) return { commits: [], changedFiles: [] };
55
82
 
@@ -164,7 +191,11 @@ function main() {
164
191
  const workerData = JSON.stringify({ baseDir, project, commits, changedFiles, reads });
165
192
  const tmpFile = path.join(os.tmpdir(), `mindlore-worker-${Date.now()}.json`);
166
193
  fs.writeFileSync(tmpFile, workerData, 'utf8');
167
- const child = spawn(process.execPath, [__filename, '--worker', tmpFile], {
194
+ // Use system node instead of process.execPath CC's embedded Node
195
+ // may not work as a standalone binary for detached worker processes.
196
+ // Resolve full path to avoid shell:true deprecation warning on Windows.
197
+ const nodeBin = resolveWin32Bin('node');
198
+ const child = spawn(nodeBin, [__filename, '--worker', tmpFile], {
168
199
  detached: true,
169
200
  stdio: 'ignore',
170
201
  cwd: process.cwd(),
@@ -173,6 +204,7 @@ function main() {
173
204
  } catch (_err) {
174
205
  // Fallback: run inline if spawn fails
175
206
  writeBareEpisode(baseDir, project, commits, changedFiles, reads);
207
+ writeEpisodeFile(baseDir, project, commits, changedFiles, reads);
176
208
  syncObsidian(baseDir);
177
209
  syncGlobalRepo();
178
210
  }
@@ -247,11 +279,60 @@ function writeBareEpisode(baseDir, project, commits, changedFiles, reads) {
247
279
 
248
280
  db.close();
249
281
  } catch (err) {
250
- process.stderr.write(`[mindlore] episode write failed: ${err?.message ?? err}\n`);
282
+ hookLog('session-end', 'error', `episode write failed: ${err?.message ?? err}`);
251
283
  }
252
284
  }
253
285
 
254
- const EXPORT_DIRS = ['analyses', 'decisions', 'diary', 'raw', 'sources', 'domains', 'connections', 'insights', 'learnings'];
286
+ /**
287
+ * Write episode as .md file to diary/{project}/ for human-readable browsing.
288
+ * Complements the DB episode — same content, different medium.
289
+ */
290
+ function writeEpisodeFile(baseDir, project, commits, changedFiles, reads) {
291
+ const projDir = path.join(baseDir, 'diary', project || 'unknown');
292
+ if (!fs.existsSync(projDir)) fs.mkdirSync(projDir, { recursive: true });
293
+
294
+ const now = new Date();
295
+ const ts = formatDate(now);
296
+ const filePath = path.join(projDir, `episode-${ts}.md`);
297
+ if (fs.existsSync(filePath)) return; // idempotent
298
+
299
+ const lines = [
300
+ '---',
301
+ `slug: episode-${ts}`,
302
+ 'type: episode',
303
+ `date: ${now.toISOString().slice(0, 10)}`,
304
+ `project: ${project || 'unknown'}`,
305
+ '---',
306
+ '',
307
+ `# Episode — ${ts}`,
308
+ '',
309
+ ];
310
+
311
+ if (commits.length > 0) {
312
+ lines.push('## Commits');
313
+ for (const c of commits) lines.push(`- ${c}`);
314
+ lines.push('');
315
+ }
316
+
317
+ if (changedFiles.length > 0) {
318
+ lines.push('## Changed Files');
319
+ for (const f of changedFiles) lines.push(`- ${f}`);
320
+ lines.push('');
321
+ }
322
+
323
+ if (reads) {
324
+ lines.push('## Read Stats');
325
+ lines.push(`- ${reads.count} files read, ${reads.repeats} repeated`);
326
+ lines.push('');
327
+ }
328
+
329
+ if (commits.length === 0 && changedFiles.length === 0) {
330
+ lines.push('_Read-only session — no commits or file changes._');
331
+ lines.push('');
332
+ }
333
+
334
+ fs.writeFileSync(filePath, lines.join('\n'), 'utf8');
335
+ }
255
336
 
256
337
  /**
257
338
  * Load obsidian-helpers from compiled dist (single source of truth for wikilink conversion).
@@ -316,27 +397,40 @@ function syncObsidian(baseDir) {
316
397
  if (!fs.existsSync(srcDir)) continue;
317
398
 
318
399
  const destDir = path.join(destBase, dir);
319
- if (!fs.existsSync(destDir)) fs.mkdirSync(destDir, { recursive: true });
400
+ fs.mkdirSync(destDir, { recursive: true });
320
401
 
321
402
  for (const file of fs.readdirSync(srcDir).filter(f => f.endsWith('.md') && !f.startsWith('_'))) {
322
403
  if (exportMdFile(path.join(srcDir, file), path.join(destDir, file), convertFn)) exported++;
323
404
  }
405
+
406
+ // One-level subdirectories (e.g. diary/mindlore/)
407
+ for (const entry of fs.readdirSync(srcDir, { withFileTypes: true })) {
408
+ if (!entry.isDirectory() || entry.name.startsWith('_') || entry.name.startsWith('.')) continue;
409
+ const subSrc = path.join(srcDir, entry.name);
410
+ const subDest = path.join(destDir, entry.name);
411
+ fs.mkdirSync(subDest, { recursive: true });
412
+ for (const file of fs.readdirSync(subSrc).filter(f => f.endsWith('.md') && !f.startsWith('_'))) {
413
+ if (exportMdFile(path.join(subSrc, file), path.join(subDest, file), convertFn)) exported++;
414
+ }
415
+ }
324
416
  }
325
417
 
326
418
  for (const rootFile of ['INDEX.md', 'log.md']) {
327
419
  const srcPath = path.join(baseDir, rootFile);
328
420
  if (!fs.existsSync(srcPath)) continue;
329
- if (!fs.existsSync(destBase)) fs.mkdirSync(destBase, { recursive: true });
421
+ fs.mkdirSync(destBase, { recursive: true });
330
422
  if (exportMdFile(srcPath, path.join(destBase, rootFile), convertFn)) exported++;
331
423
  }
332
424
 
425
+ hookLog('session-end', 'info', `obsidian exported=${exported}, dirs=${EXPORT_DIRS.length}, vault=${vaultPath}`);
333
426
  if (exported > 0) {
334
427
  config.obsidian.lastExport = new Date().toISOString();
335
428
  config.obsidian.lastExportCount = exported;
336
429
  fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');
337
430
  }
338
431
  } catch (err) {
339
- process.stderr.write(`[mindlore] obsidian sync failed: ${err?.message ?? err}\n`);
432
+ hookLog('session-end', 'error', `obsidian internal: ${err?.message ?? err}`);
433
+ throw err; // re-throw so safeRun logs FAIL
340
434
  }
341
435
  }
342
436
 
@@ -345,38 +439,34 @@ function syncObsidian(baseDir) {
345
439
  * Only runs for the global scope — project .mindlore/ is in the project's own git.
346
440
  * Push failure is graceful (offline support).
347
441
  */
442
+ function resolveGitBin() {
443
+ return resolveWin32Bin('git');
444
+ }
445
+
348
446
  function syncGlobalRepo() {
349
447
  const gDir = globalDir();
350
448
  const gitDir = path.join(gDir, '.git');
351
449
  if (!fs.existsSync(gitDir)) return;
352
450
 
353
- try {
354
- // Check for changes
355
- const status = execSync('git status --porcelain', {
356
- cwd: gDir,
357
- encoding: 'utf8',
358
- timeout: 5000,
359
- }).trim();
451
+ const git = resolveGitBin();
452
+ const execOpts = (timeout) => ({ cwd: gDir, encoding: 'utf8', timeout, stdio: 'pipe' });
360
453
 
361
- if (!status) return; // nothing to commit
454
+ // Check for changes
455
+ const status = execSync(`"${git}" status --porcelain`, execOpts(5000)).trim();
456
+ if (!status) return; // nothing to commit
362
457
 
363
- execSync('git add *.md mindlore.db config.json diary/ sources/ domains/ analyses/ decisions/ raw/ connections/ insights/ learnings/', { cwd: gDir, timeout: 5000, stdio: 'pipe' });
364
- const now = new Date().toISOString().slice(0, 19);
365
- execSync(`git commit -m "mindlore auto-sync ${now}"`, {
366
- cwd: gDir,
367
- timeout: 10000,
368
- stdio: 'pipe',
369
- });
458
+ execSync(`"${git}" add *.md mindlore.db diary/ sources/ domains/ analyses/ decisions/ raw/ connections/ insights/ learnings/`, execOpts(10000));
459
+ const now = new Date().toISOString().slice(0, 19);
460
+ execSync(`"${git}" commit -m "mindlore auto-sync ${now}"`, execOpts(15000));
370
461
 
371
- // Push — graceful fail if no remote or offline
372
- try {
373
- execSync('git push', { cwd: gDir, timeout: 15000, stdio: 'pipe' });
374
- } catch (_pushErr) {
375
- // Offline or no remote silently continue
376
- }
377
- } catch (_err) {
378
- // Git not available or commit failed — silently continue
462
+ // Push — graceful fail if no remote or offline
463
+ try {
464
+ execSync(`"${git}" push`, execOpts(15000));
465
+ } catch (_pushErr) {
466
+ hookLog('session-end', 'warn', 'git push failed (offline?): ' + (_pushErr?.message ?? '').slice(0, 100));
379
467
  }
380
468
  }
381
469
 
382
- main();
470
+ if (!process.argv.includes('--worker')) {
471
+ main();
472
+ }
@@ -10,7 +10,7 @@
10
10
 
11
11
  const fs = require('fs');
12
12
  const path = require('path');
13
- const { findMindloreDir, readConfig, openDatabase, hasEpisodesTable, queryRecentEpisodes, querySupersededChains, formatSupersededChains, queryMultiSessionEpisodes, formatMultiSessionEpisodes, getAllMdFiles } = require('./lib/mindlore-common.cjs');
13
+ const { findMindloreDir, readConfig, openDatabase, hasEpisodesTable, queryRecentEpisodes, querySupersededChains, formatSupersededChains, queryMultiSessionEpisodes, formatMultiSessionEpisodes, getAllMdFiles, getRecentHookErrors, hookLog } = require('./lib/mindlore-common.cjs');
14
14
 
15
15
  function main() {
16
16
  const baseDir = findMindloreDir();
@@ -117,8 +117,29 @@ function main() {
117
117
  }
118
118
  } catch (_healthErr) { /* skip */ }
119
119
 
120
- if (output.length > 0) {
121
- process.stdout.write(output.join('\n\n') + '\n');
120
+ // Check for recent hook errors — inject warnings into CC context
121
+ try {
122
+ const errors = getRecentHookErrors();
123
+ if (errors.length > 0) {
124
+ const lines = errors.map(e => `- [${e.ts.slice(0, 19)}] **${e.hook}** (${e.level}): ${e.msg}`);
125
+ output.push(`[Mindlore Hook Alerts]\n${lines.join('\n')}`);
126
+ }
127
+ } catch (_hookLogErr) { /* skip */ }
128
+
129
+ hookLog('session-focus', 'info', 'session started');
130
+
131
+ // Token budget for session inject
132
+ // Defaults match DEFAULT_TOKEN_BUDGET in scripts/lib/constants.ts
133
+ const budgetConfig = config?.tokenBudget ?? {};
134
+ const maxInjectChars = (budgetConfig.sessionInject || 2000) * 4;
135
+
136
+ let joined = output.join('\n\n');
137
+ if (joined.length > maxInjectChars) {
138
+ joined = joined.slice(0, maxInjectChars) + '\n[...truncated by token budget]';
139
+ }
140
+
141
+ if (joined.length > 0) {
142
+ process.stdout.write(joined + '\n');
122
143
  }
123
144
  }
124
145
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mindlore",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "AI-native knowledge system for Claude Code",
5
5
  "type": "commonjs",
6
6
  "bin": {
@@ -43,7 +43,7 @@
43
43
  "node": ">=20.0.0"
44
44
  },
45
45
  "dependencies": {
46
- "better-sqlite3": "^11.0.0",
46
+ "better-sqlite3": "^12.9.0",
47
47
  "zod": "^4.3.6"
48
48
  },
49
49
  "devDependencies": {
@@ -53,8 +53,8 @@
53
53
  "@typescript-eslint/eslint-plugin": "^8.58.1",
54
54
  "@typescript-eslint/parser": "^8.58.1",
55
55
  "@xenova/transformers": "^2.17.2",
56
- "eslint": "^9.0.0",
57
- "globals": "^15.0.0",
56
+ "eslint": "^10.2.0",
57
+ "globals": "^17.5.0",
58
58
  "jest": "^29.7.0",
59
59
  "sqlite-vec": "^0.1.9",
60
60
  "ts-jest": "^29.4.9",