mindlore 0.7.0 → 0.7.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.
- package/README.md +30 -3
- package/dist/scripts/bundle-hooks.d.ts +2 -0
- package/dist/scripts/bundle-hooks.d.ts.map +1 -0
- package/dist/scripts/bundle-hooks.js +68 -0
- package/dist/scripts/bundle-hooks.js.map +1 -0
- package/dist/scripts/init.js +0 -3
- package/dist/scripts/init.js.map +1 -1
- package/dist/scripts/lib/constants.d.ts +0 -2
- package/dist/scripts/lib/constants.d.ts.map +1 -1
- package/dist/scripts/lib/constants.js +0 -21
- package/dist/scripts/lib/constants.js.map +1 -1
- package/dist/tests/hook-smoke.test.js +1 -1
- package/dist/tests/hook-smoke.test.js.map +1 -1
- package/dist/tests/search-hook.test.js +1 -1
- package/dist/tests/search-hook.test.js.map +1 -1
- package/hooks/cc-memory-bulk-sync.cjs +592 -0
- package/hooks/cc-session-sync.cjs +842 -0
- package/hooks/hooks.json +149 -0
- package/hooks/lib/mindlore-common.cjs +2 -2
- package/hooks/lib/secure-io.cjs +17 -0
- package/hooks/mindlore-cwd-changed.cjs +19 -34
- package/hooks/mindlore-decision-detector.cjs +40 -31
- package/hooks/mindlore-dont-repeat.cjs +57 -115
- package/hooks/mindlore-fts5-sync.cjs +15 -44
- package/hooks/mindlore-index.cjs +100 -101
- package/hooks/mindlore-model-router.cjs +20 -32
- package/hooks/mindlore-post-compact.cjs +26 -42
- package/hooks/mindlore-post-read.cjs +35 -60
- package/hooks/mindlore-pre-compact.cjs +55 -73
- package/hooks/mindlore-read-guard.cjs +28 -51
- package/hooks/mindlore-research-guard.cjs +63 -101
- package/hooks/mindlore-search.cjs +1142 -93
- package/hooks/mindlore-session-end.cjs +155 -276
- package/hooks/mindlore-session-focus.cjs +639 -110
- package/hooks/src/lib/constants.cjs +15 -0
- package/hooks/src/lib/mindlore-common.cjs +975 -0
- package/hooks/src/lib/mindlore-common.d.cts +72 -0
- package/hooks/src/lib/secure-io.cjs +17 -0
- package/hooks/src/lib/types.d.ts +58 -0
- package/hooks/src/mindlore-cwd-changed.cjs +57 -0
- package/hooks/src/mindlore-decision-detector.cjs +54 -0
- package/hooks/src/mindlore-dont-repeat.cjs +222 -0
- package/hooks/src/mindlore-fts5-sync.cjs +98 -0
- package/hooks/src/mindlore-index.cjs +230 -0
- package/hooks/src/mindlore-model-router.cjs +54 -0
- package/hooks/src/mindlore-post-compact.cjs +69 -0
- package/hooks/src/mindlore-post-read.cjs +106 -0
- package/hooks/src/mindlore-pre-compact.cjs +154 -0
- package/hooks/src/mindlore-read-guard.cjs +105 -0
- package/hooks/src/mindlore-research-guard.cjs +176 -0
- package/hooks/src/mindlore-search.cjs +200 -0
- package/hooks/src/mindlore-session-end.cjs +511 -0
- package/hooks/src/mindlore-session-focus.cjs +256 -0
- package/package.json +7 -3
- package/plugin.json +3 -3
- package/templates/config.json +1 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type BetterSqlite3 from 'better-sqlite3';
|
|
2
|
+
type Database = BetterSqlite3.Database;
|
|
3
|
+
|
|
4
|
+
export const MINDLORE_DIR: string;
|
|
5
|
+
export const GLOBAL_MINDLORE_DIR: string;
|
|
6
|
+
export const DB_NAME: string;
|
|
7
|
+
export const SKIP_FILES: Set<string>;
|
|
8
|
+
export const SQL_FTS_CREATE: string;
|
|
9
|
+
export const SQL_FTS_INSERT: string;
|
|
10
|
+
export const DEFAULT_MODELS: Record<string, string>;
|
|
11
|
+
|
|
12
|
+
export function globalDir(): string;
|
|
13
|
+
export function findMindloreDir(): string | null;
|
|
14
|
+
export function getActiveMindloreDir(): string | null;
|
|
15
|
+
export function getAllDbs(): string[];
|
|
16
|
+
export function getProjectName(): string;
|
|
17
|
+
export function getLatestDelta(diaryDir: string): string | null;
|
|
18
|
+
export function sha256(content: string): string;
|
|
19
|
+
|
|
20
|
+
export interface FrontmatterResult {
|
|
21
|
+
meta: Record<string, unknown>;
|
|
22
|
+
body: string;
|
|
23
|
+
}
|
|
24
|
+
export function parseFrontmatter(content: string): FrontmatterResult;
|
|
25
|
+
|
|
26
|
+
export interface FtsMetadata {
|
|
27
|
+
slug: string;
|
|
28
|
+
description: string;
|
|
29
|
+
type: string;
|
|
30
|
+
category: string;
|
|
31
|
+
title: string;
|
|
32
|
+
tags: string;
|
|
33
|
+
quality: string | null;
|
|
34
|
+
dateCaptured: string | null;
|
|
35
|
+
}
|
|
36
|
+
export function extractFtsMetadata(
|
|
37
|
+
meta: Record<string, unknown>,
|
|
38
|
+
body: string,
|
|
39
|
+
filePath: string,
|
|
40
|
+
baseDir: string,
|
|
41
|
+
): FtsMetadata;
|
|
42
|
+
|
|
43
|
+
export function readHookStdin(fields: string[]): string;
|
|
44
|
+
|
|
45
|
+
export interface FtsEntry {
|
|
46
|
+
path: string;
|
|
47
|
+
slug: string;
|
|
48
|
+
description: string;
|
|
49
|
+
type: string;
|
|
50
|
+
category: string;
|
|
51
|
+
title: string;
|
|
52
|
+
content: string;
|
|
53
|
+
tags: string;
|
|
54
|
+
quality: string | null;
|
|
55
|
+
dateCaptured: string | null;
|
|
56
|
+
project: string;
|
|
57
|
+
}
|
|
58
|
+
export function insertFtsRow(db: Database, entry: FtsEntry): void;
|
|
59
|
+
|
|
60
|
+
export function extractHeadings(content: string, max?: number): string[];
|
|
61
|
+
export function requireDatabase(): typeof import('better-sqlite3');
|
|
62
|
+
export function openDatabase(dbPath: string, opts?: { readonly?: boolean }): Database | null;
|
|
63
|
+
export function getAllMdFiles(dir: string, skip?: Set<string>): string[];
|
|
64
|
+
|
|
65
|
+
export interface MindloreConfig {
|
|
66
|
+
version?: string;
|
|
67
|
+
created?: string;
|
|
68
|
+
models?: Record<string, string>;
|
|
69
|
+
[key: string]: unknown;
|
|
70
|
+
}
|
|
71
|
+
export function readConfig(mindloreDir: string): MindloreConfig | null;
|
|
72
|
+
export function detectSchemaVersion(db: unknown): number;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
|
|
5
|
+
function safeMkdir(dirPath) {
|
|
6
|
+
fs.mkdirSync(dirPath, { recursive: true, mode: 0o700 });
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function safeWriteFile(filePath, data) {
|
|
10
|
+
fs.writeFileSync(filePath, data, { encoding: 'utf8', mode: 0o600 });
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function safeWriteJson(filePath, obj) {
|
|
14
|
+
safeWriteFile(filePath, JSON.stringify(obj, null, 2) + '\n');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = { safeMkdir, safeWriteFile, safeWriteJson };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type declarations for mindlore-common.cjs hook functions.
|
|
3
|
+
* Use with JSDoc @type imports in .cjs hook files.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface FtsMetadata {
|
|
7
|
+
slug: string;
|
|
8
|
+
description: string;
|
|
9
|
+
type: string;
|
|
10
|
+
category: string;
|
|
11
|
+
title: string;
|
|
12
|
+
tags: string;
|
|
13
|
+
quality: string | null;
|
|
14
|
+
dateCaptured: string | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface FtsEntry {
|
|
18
|
+
path: string;
|
|
19
|
+
slug?: string;
|
|
20
|
+
description?: string;
|
|
21
|
+
type?: string;
|
|
22
|
+
category?: string;
|
|
23
|
+
title?: string;
|
|
24
|
+
content?: string;
|
|
25
|
+
tags?: string;
|
|
26
|
+
quality?: string | null;
|
|
27
|
+
dateCaptured?: string | null;
|
|
28
|
+
project?: string | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface ParsedFrontmatter {
|
|
32
|
+
meta: Record<string, string | string[]>;
|
|
33
|
+
body: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface MindloreCommon {
|
|
37
|
+
MINDLORE_DIR: string;
|
|
38
|
+
GLOBAL_MINDLORE_DIR: string;
|
|
39
|
+
DB_NAME: string;
|
|
40
|
+
SKIP_FILES: Set<string>;
|
|
41
|
+
globalDir(): string;
|
|
42
|
+
findMindloreDir(): string | null;
|
|
43
|
+
getActiveMindloreDir(): string;
|
|
44
|
+
getAllDbs(): string[];
|
|
45
|
+
getLatestDelta(diaryDir: string): string | null;
|
|
46
|
+
sha256(content: string): string;
|
|
47
|
+
parseFrontmatter(content: string): ParsedFrontmatter;
|
|
48
|
+
extractFtsMetadata(meta: Record<string, string>, body: string, filePath: string, baseDir: string): FtsMetadata;
|
|
49
|
+
insertFtsRow(db: import('better-sqlite3').Database, entry: FtsEntry): void;
|
|
50
|
+
readHookStdin(fields: string[]): string;
|
|
51
|
+
extractHeadings(content: string, max: number): string[];
|
|
52
|
+
requireDatabase(): typeof import('better-sqlite3') | null;
|
|
53
|
+
openDatabase(dbPath: string, opts?: { readonly?: boolean }): import('better-sqlite3').Database | null;
|
|
54
|
+
getAllMdFiles(dir: string, skip?: Set<string>): string[];
|
|
55
|
+
SQL_FTS_CREATE: string;
|
|
56
|
+
SQL_FTS_INSERT: string;
|
|
57
|
+
getProjectName(): string;
|
|
58
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* mindlore-cwd-changed — CwdChanged hook
|
|
6
|
+
*
|
|
7
|
+
* Fires when user changes working directory.
|
|
8
|
+
* CwdChanged has NO inject to Claude — stdout is swallowed, stderr shown to user.
|
|
9
|
+
*
|
|
10
|
+
* Side effects:
|
|
11
|
+
* 1. Detect scope (global ~/.mindlore/ or none)
|
|
12
|
+
* 2. Write scope state to .mindlore/diary/_scope.json for session-focus to read
|
|
13
|
+
* 3. Show user-facing message via stderr
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const fs = require('fs');
|
|
17
|
+
const path = require('path');
|
|
18
|
+
const { findMindloreDir, globalDir, hookLog, withTelemetry } = require('./lib/mindlore-common.cjs');
|
|
19
|
+
|
|
20
|
+
function main() {
|
|
21
|
+
const cwd = process.cwd();
|
|
22
|
+
const activeDir = findMindloreDir();
|
|
23
|
+
const scope = !activeDir ? 'none' : activeDir.startsWith(globalDir()) ? 'global' : 'project';
|
|
24
|
+
|
|
25
|
+
if (activeDir) {
|
|
26
|
+
const diaryDir = path.join(activeDir, 'diary');
|
|
27
|
+
if (!fs.existsSync(diaryDir)) {
|
|
28
|
+
fs.mkdirSync(diaryDir, { recursive: true });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Dirty-check: skip write if scope hasn't changed
|
|
32
|
+
const scopePath = path.join(diaryDir, '_scope.json');
|
|
33
|
+
if (fs.existsSync(scopePath)) {
|
|
34
|
+
try {
|
|
35
|
+
const existing = JSON.parse(fs.readFileSync(scopePath, 'utf8'));
|
|
36
|
+
if (existing.cwd === cwd && existing.scope === scope) return;
|
|
37
|
+
} catch (_err) {
|
|
38
|
+
// corrupt file — overwrite
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
fs.writeFileSync(scopePath, JSON.stringify({
|
|
43
|
+
scope,
|
|
44
|
+
dir: activeDir,
|
|
45
|
+
cwd,
|
|
46
|
+
timestamp: new Date().toISOString(),
|
|
47
|
+
}, null, 2), 'utf8');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (scope === 'none') {
|
|
51
|
+
process.stderr.write(`[Mindlore] Bu projede mindlore kurulu degil. npx mindlore init calistirin.\n`);
|
|
52
|
+
} else {
|
|
53
|
+
process.stderr.write(`[Mindlore scope: ${scope}] ${activeDir}\n`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
withTelemetry('mindlore-cwd-changed', main).catch(err => { hookLog('cwd-changed', 'error', err?.message ?? String(err)); });
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* mindlore-decision-detector — UserPromptSubmit hook
|
|
6
|
+
*
|
|
7
|
+
* Detects decision signals in user messages (TR + EN).
|
|
8
|
+
* Outputs a suggestion to record the decision via /mindlore-decide.
|
|
9
|
+
* Does NOT block (exit 0) — advisory only.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const { findMindloreDir, readHookStdin, hookLog, withTelemetry } = require('./lib/mindlore-common.cjs');
|
|
13
|
+
|
|
14
|
+
const SIGNALS_TR = [
|
|
15
|
+
'karar verdik', 'karar verildi', 'kararlastirdik', 'kararlaştırdık',
|
|
16
|
+
'şunu seçtik', 'sunu sectik', 'bunu yapmayalım', 'bunu yapmayalim',
|
|
17
|
+
'yerine', 'tercih ettik', 'onaylandi', 'onaylandı', 'kesinleşti', 'kesinlesti',
|
|
18
|
+
'vazgeçtik', 'vazgectik', 'iptal ettik',
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
const SIGNALS_EN = [
|
|
22
|
+
'decided', 'decision made', "let's go with", 'lets go with',
|
|
23
|
+
"we'll use", 'well use', 'approved', 'settled on',
|
|
24
|
+
'going with', 'chosen', 'finalized', 'rejected',
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
function detectDecision(text) {
|
|
28
|
+
const lower = text.toLowerCase();
|
|
29
|
+
for (const signal of SIGNALS_TR) {
|
|
30
|
+
if (lower.includes(signal)) return signal;
|
|
31
|
+
}
|
|
32
|
+
for (const signal of SIGNALS_EN) {
|
|
33
|
+
if (lower.includes(signal)) return signal;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function main() {
|
|
39
|
+
const baseDir = findMindloreDir();
|
|
40
|
+
if (!baseDir) return;
|
|
41
|
+
|
|
42
|
+
const userText = readHookStdin(['prompt', 'content', 'message']);
|
|
43
|
+
if (!userText || userText.length < 10) return;
|
|
44
|
+
|
|
45
|
+
const signal = detectDecision(userText);
|
|
46
|
+
if (signal) {
|
|
47
|
+
process.stdout.write(`[Mindlore: Karar sinyali tespit edildi ("${signal}") — /mindlore-decide record ile kaydetmek ister misin?]\n`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
withTelemetry('mindlore-decision-detector', main).catch(err => {
|
|
52
|
+
hookLog('mindlore-decision-detector', 'error', err?.message ?? String(err));
|
|
53
|
+
process.exit(0);
|
|
54
|
+
});
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* mindlore-dont-repeat — PreToolUse hook (matcher: "Write|Edit")
|
|
6
|
+
*
|
|
7
|
+
* Checks code being written against negative rules (DON'T, NEVER, AVOID, YAPMA, etc.)
|
|
8
|
+
* found in LESSONS files and Mindlore learnings/.
|
|
9
|
+
*
|
|
10
|
+
* Sources checked (in order):
|
|
11
|
+
* 1. ~/.claude/lessons/global.md (global rules)
|
|
12
|
+
* 2. ./LESSONS.md (project-level rules, if exists)
|
|
13
|
+
* 3. .mindlore/learnings/*.md (Mindlore learnings, if exists)
|
|
14
|
+
*
|
|
15
|
+
* Advisory only (exit 0) — does not block, injects additionalContext warning.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const fs = require('fs');
|
|
19
|
+
const path = require('path');
|
|
20
|
+
const os = require('os');
|
|
21
|
+
const { findMindloreDir, getProjectName, hookLog, withTelemetrySync } = require('./lib/mindlore-common.cjs');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* File-persisted pattern cache — survives across process invocations.
|
|
25
|
+
* Cache file: .mindlore/diary/_pattern-cache.json
|
|
26
|
+
* Each entry keyed by source file path, stores mtimeMs + extracted patterns.
|
|
27
|
+
* On hit: stat only, no readFile+parse. On miss: read, parse, update cache.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
let cacheDirty = false;
|
|
31
|
+
|
|
32
|
+
function readCache(cachePath) {
|
|
33
|
+
if (!cachePath) return {};
|
|
34
|
+
try {
|
|
35
|
+
return JSON.parse(fs.readFileSync(cachePath, 'utf8'));
|
|
36
|
+
} catch (_err) {
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function writeCache(cachePath, cache) {
|
|
42
|
+
if (!cachePath || !cacheDirty) return;
|
|
43
|
+
try {
|
|
44
|
+
fs.writeFileSync(cachePath, JSON.stringify(cache), 'utf8');
|
|
45
|
+
} catch (_err) { /* write failure is non-fatal */ }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function loadPatterns(filePath, cache) {
|
|
49
|
+
try {
|
|
50
|
+
const stat = fs.statSync(filePath);
|
|
51
|
+
const mtimeMs = stat.mtimeMs;
|
|
52
|
+
const cached = cache[filePath];
|
|
53
|
+
if (cached && cached.mtimeMs === mtimeMs) return cached.patterns;
|
|
54
|
+
|
|
55
|
+
const patterns = extractNegativePatterns(fs.readFileSync(filePath, 'utf8'));
|
|
56
|
+
cache[filePath] = { mtimeMs, patterns };
|
|
57
|
+
cacheDirty = true;
|
|
58
|
+
return patterns;
|
|
59
|
+
} catch (_err) {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function extractNegativePatterns(content) {
|
|
65
|
+
const patterns = [];
|
|
66
|
+
const lines = content.split('\n');
|
|
67
|
+
|
|
68
|
+
for (const line of lines) {
|
|
69
|
+
const trimmed = line.trim();
|
|
70
|
+
// Multi-language: TR (YAPMA, KRITIK) + EN (DON'T, NEVER, AVOID, DO NOT)
|
|
71
|
+
const isNegativeRule = /^-\s*(YAPMA|KRITIK|DON'?T|NEVER|AVOID|DO NOT):/i.test(trimmed);
|
|
72
|
+
if (!isNegativeRule) continue;
|
|
73
|
+
|
|
74
|
+
// Extract backtick-quoted code patterns: `pattern`
|
|
75
|
+
const backtickMatches = trimmed.match(/`([^`]+)`/g);
|
|
76
|
+
if (backtickMatches) {
|
|
77
|
+
for (const match of backtickMatches) {
|
|
78
|
+
const pattern = match.slice(1, -1).trim();
|
|
79
|
+
// Skip short/generic patterns — too many false positives
|
|
80
|
+
if (pattern.length < 8) continue;
|
|
81
|
+
if (/^[^a-zA-Z0-9]+$/.test(pattern)) continue;
|
|
82
|
+
// Skip single words (too generic: "node", "bash", "any")
|
|
83
|
+
if (/^\w+$/.test(pattern) && pattern.length < 12) continue;
|
|
84
|
+
// Skip file extensions and paths
|
|
85
|
+
if (/^\.\w{1,5}$/.test(pattern)) continue;
|
|
86
|
+
if (pattern.startsWith('/') || pattern.startsWith('~')) continue;
|
|
87
|
+
if (pattern.includes('.md') || pattern.includes('.json')) continue;
|
|
88
|
+
// Skip common false-positive patterns
|
|
89
|
+
if (/^(node|bash|npm|git|process|require|import|export|const|let|var)$/i.test(pattern)) continue;
|
|
90
|
+
|
|
91
|
+
patterns.push({
|
|
92
|
+
pattern,
|
|
93
|
+
rule: trimmed.substring(0, 120),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Extract "quoted strings" as patterns
|
|
99
|
+
const quoteMatches = trimmed.match(/"([^"]+)"/g);
|
|
100
|
+
if (quoteMatches) {
|
|
101
|
+
for (const match of quoteMatches) {
|
|
102
|
+
const quoted = match.slice(1, -1).trim();
|
|
103
|
+
if (quoted.length < 4) continue;
|
|
104
|
+
patterns.push({
|
|
105
|
+
pattern: quoted,
|
|
106
|
+
rule: trimmed.substring(0, 120),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return patterns;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function checkContent(content, patterns) {
|
|
116
|
+
const matches = [];
|
|
117
|
+
for (const p of patterns) {
|
|
118
|
+
try {
|
|
119
|
+
const escaped = p.pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
120
|
+
const regex = new RegExp(escaped, 'i');
|
|
121
|
+
if (regex.test(content)) {
|
|
122
|
+
matches.push(p);
|
|
123
|
+
}
|
|
124
|
+
} catch { /* skip invalid patterns */ }
|
|
125
|
+
}
|
|
126
|
+
return matches;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function main() {
|
|
130
|
+
let input = '';
|
|
131
|
+
const stdinTimeout = setTimeout(() => process.exit(0), 3000);
|
|
132
|
+
process.stdin.setEncoding('utf8');
|
|
133
|
+
process.stdin.on('error', () => process.exit(0));
|
|
134
|
+
process.stdin.on('data', chunk => input += chunk);
|
|
135
|
+
process.stdin.on('end', () => {
|
|
136
|
+
clearTimeout(stdinTimeout);
|
|
137
|
+
try {
|
|
138
|
+
const data = JSON.parse(input || '{}');
|
|
139
|
+
const toolName = data.tool_name || '';
|
|
140
|
+
|
|
141
|
+
if (!['Write', 'Edit'].includes(toolName)) {
|
|
142
|
+
return process.exit(0);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const toolInput = data.tool_input || {};
|
|
146
|
+
const filePath = toolInput.file_path || '';
|
|
147
|
+
|
|
148
|
+
// Skip non-code files
|
|
149
|
+
if (!filePath) return process.exit(0);
|
|
150
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
151
|
+
const codeExts = ['.ts', '.tsx', '.js', '.jsx', '.cjs', '.mjs', '.py', '.go', '.rs', '.java', '.c', '.cpp', '.h', '.sh', '.yaml', '.yml'];
|
|
152
|
+
if (!codeExts.includes(ext)) return process.exit(0);
|
|
153
|
+
|
|
154
|
+
// Skip rule files themselves
|
|
155
|
+
const basename = path.basename(filePath);
|
|
156
|
+
if (basename === 'LESSONS.md' || basename === 'global.md' || basename === 'CLAUDE.md') {
|
|
157
|
+
return process.exit(0);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Collect content being written (skip old_string — that's code being removed, not added)
|
|
161
|
+
const allContent = [
|
|
162
|
+
toolInput.content || '',
|
|
163
|
+
toolInput.new_string || '',
|
|
164
|
+
].join('\n');
|
|
165
|
+
|
|
166
|
+
if (allContent.trim().length < 10) return process.exit(0);
|
|
167
|
+
|
|
168
|
+
// Load patterns from all sources (file-persisted mtime cache)
|
|
169
|
+
const mindloreDir = findMindloreDir();
|
|
170
|
+
const cachePath = mindloreDir ? path.join(mindloreDir, 'diary', `_pattern-cache-${getProjectName()}.json`) : null;
|
|
171
|
+
const cache = readCache(cachePath);
|
|
172
|
+
const allPatterns = [];
|
|
173
|
+
const cwd = process.cwd();
|
|
174
|
+
|
|
175
|
+
// 1. Global lessons
|
|
176
|
+
allPatterns.push(...loadPatterns(path.join(os.homedir(), '.claude', 'lessons', 'global.md'), cache));
|
|
177
|
+
|
|
178
|
+
// 2. Project LESSONS.md
|
|
179
|
+
allPatterns.push(...loadPatterns(path.join(cwd, 'LESSONS.md'), cache));
|
|
180
|
+
|
|
181
|
+
// 3. Mindlore learnings/ directory
|
|
182
|
+
if (mindloreDir) {
|
|
183
|
+
const learningsDir = path.join(mindloreDir, 'learnings');
|
|
184
|
+
try {
|
|
185
|
+
const files = fs.readdirSync(learningsDir).filter(f => f.endsWith('.md'));
|
|
186
|
+
for (const file of files) {
|
|
187
|
+
allPatterns.push(...loadPatterns(path.join(learningsDir, file), cache));
|
|
188
|
+
}
|
|
189
|
+
} catch (_err) { /* learnings/ doesn't exist yet */ }
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
writeCache(cachePath, cache);
|
|
193
|
+
|
|
194
|
+
if (allPatterns.length === 0) return process.exit(0);
|
|
195
|
+
|
|
196
|
+
// Check content against patterns
|
|
197
|
+
const matches = checkContent(allContent, allPatterns);
|
|
198
|
+
if (matches.length === 0) return process.exit(0);
|
|
199
|
+
|
|
200
|
+
// Build warning — max 3 matches shown
|
|
201
|
+
const shown = matches.slice(0, 3);
|
|
202
|
+
const warning = shown.map(m =>
|
|
203
|
+
` - Pattern: \`${m.pattern}\` → ${m.rule}`
|
|
204
|
+
).join('\n');
|
|
205
|
+
const extra = matches.length > 3 ? `\n ... and ${matches.length - 3} more` : '';
|
|
206
|
+
|
|
207
|
+
const msg = `[Mindlore: ${matches.length} dont-repeat rule violation detected]\n${warning}${extra}`;
|
|
208
|
+
|
|
209
|
+
process.stdout.write(JSON.stringify({
|
|
210
|
+
hookSpecificOutput: {
|
|
211
|
+
hookEventName: 'PreToolUse',
|
|
212
|
+
additionalContext: msg
|
|
213
|
+
}
|
|
214
|
+
}));
|
|
215
|
+
} catch {
|
|
216
|
+
// Silent fail
|
|
217
|
+
}
|
|
218
|
+
process.exit(0);
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
try { withTelemetrySync('mindlore-dont-repeat', main); } catch (err) { hookLog('dont-repeat', 'error', err?.message ?? String(err)); }
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* mindlore-fts5-sync — FileChanged hook (incremental re-index)
|
|
6
|
+
*
|
|
7
|
+
* Handles bulk file changes by checking all .mindlore/ .md files
|
|
8
|
+
* against their content hashes and re-indexing only changed ones.
|
|
9
|
+
*
|
|
10
|
+
* Lightweight complement to mindlore-index.cjs which handles single files.
|
|
11
|
+
* This hook catches cases where multiple files change at once (e.g., git pull).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const fs = require('fs');
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const { DB_NAME, sha256, openDatabase, getAllMdFiles, parseFrontmatter, extractFtsMetadata, insertFtsRow, readHookStdin, getActiveMindloreDir, getProjectName, resolveProject, hookLog, withTelemetry, SQL_FTS_SESSIONS_INSERT, isSessionCategory, isInsideMindloreDir } = require('./lib/mindlore-common.cjs');
|
|
17
|
+
|
|
18
|
+
function main() {
|
|
19
|
+
const filePath = readHookStdin(['path', 'file_path']);
|
|
20
|
+
|
|
21
|
+
if (!filePath) return;
|
|
22
|
+
const resolved = path.resolve(filePath);
|
|
23
|
+
if (!isInsideMindloreDir(resolved)) return;
|
|
24
|
+
|
|
25
|
+
// Skip if this is a single .md file change — mindlore-index.cjs handles those.
|
|
26
|
+
// This hook is for bulk changes (git pull, manual batch edits).
|
|
27
|
+
if (filePath.endsWith('.md')) return;
|
|
28
|
+
|
|
29
|
+
const baseDir = getActiveMindloreDir();
|
|
30
|
+
if (!fs.existsSync(baseDir)) return;
|
|
31
|
+
|
|
32
|
+
const dbPath = path.join(baseDir, DB_NAME);
|
|
33
|
+
if (!fs.existsSync(dbPath)) return;
|
|
34
|
+
|
|
35
|
+
const db = openDatabase(dbPath);
|
|
36
|
+
if (!db) return;
|
|
37
|
+
|
|
38
|
+
const mdFiles = getAllMdFiles(baseDir);
|
|
39
|
+
|
|
40
|
+
const allHashes = new Map();
|
|
41
|
+
for (const row of db.prepare('SELECT path, content_hash FROM file_hashes').all()) {
|
|
42
|
+
allHashes.set(row.path, row.content_hash);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const deleteFts = db.prepare('DELETE FROM mindlore_fts WHERE path = ?');
|
|
46
|
+
const deleteFtsSessions = db.prepare('DELETE FROM mindlore_fts_sessions WHERE path = ?');
|
|
47
|
+
const insertFtsSessions = db.prepare(SQL_FTS_SESSIONS_INSERT);
|
|
48
|
+
const upsertHash = db.prepare(`
|
|
49
|
+
INSERT INTO file_hashes (path, content_hash, last_indexed)
|
|
50
|
+
VALUES (?, ?, ?)
|
|
51
|
+
ON CONFLICT(path) DO UPDATE SET
|
|
52
|
+
content_hash = excluded.content_hash,
|
|
53
|
+
last_indexed = excluded.last_indexed
|
|
54
|
+
`);
|
|
55
|
+
|
|
56
|
+
const now = new Date().toISOString();
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
const project = getProjectName();
|
|
60
|
+
|
|
61
|
+
const changedFiles = [];
|
|
62
|
+
for (const file of mdFiles) {
|
|
63
|
+
const content = fs.readFileSync(file, 'utf8').replace(/\r\n/g, '\n');
|
|
64
|
+
const hash = sha256(content);
|
|
65
|
+
|
|
66
|
+
const existingHash = allHashes.get(file);
|
|
67
|
+
if (existingHash === hash) continue;
|
|
68
|
+
|
|
69
|
+
const { meta, body } = parseFrontmatter(content);
|
|
70
|
+
const { slug, description, type, category, title, tags, quality, dateCaptured, project: ftsProject } = extractFtsMetadata(meta, body, file, baseDir);
|
|
71
|
+
const resolvedProject = resolveProject(ftsProject, file, project);
|
|
72
|
+
changedFiles.push({ file, hash, slug, description, type, category, title, tags, quality, dateCaptured, resolvedProject, body });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// No file I/O inside — minimize lock hold time
|
|
76
|
+
const transaction = db.transaction(() => {
|
|
77
|
+
for (const item of changedFiles) {
|
|
78
|
+
deleteFts.run(item.file);
|
|
79
|
+
deleteFtsSessions.run(item.file);
|
|
80
|
+
if (isSessionCategory(item.category)) {
|
|
81
|
+
insertFtsSessions.run(item.file, item.slug, item.description, item.type, item.category, item.title, item.body, item.tags, item.quality ?? null, item.dateCaptured ?? null, item.resolvedProject);
|
|
82
|
+
} else {
|
|
83
|
+
insertFtsRow(db, { path: item.file, slug: item.slug, description: item.description, type: item.type, category: item.category, title: item.title, content: item.body, tags: item.tags, quality: item.quality, dateCaptured: item.dateCaptured, project: item.resolvedProject });
|
|
84
|
+
}
|
|
85
|
+
upsertHash.run(item.file, item.hash, now);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
transaction();
|
|
89
|
+
} finally {
|
|
90
|
+
db.close();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
withTelemetry('mindlore-fts5-sync', main).catch(err => {
|
|
96
|
+
hookLog('mindlore-fts5-sync', 'error', err?.message ?? String(err));
|
|
97
|
+
process.exit(0);
|
|
98
|
+
});
|