@timmeck/brain 1.1.1 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +225 -50
- package/dist/api/server.d.ts +19 -0
- package/dist/api/server.js +281 -0
- package/dist/api/server.js.map +1 -0
- package/dist/brain.d.ts +3 -0
- package/dist/brain.js +45 -8
- package/dist/brain.js.map +1 -1
- package/dist/cli/commands/dashboard.js +2 -0
- package/dist/cli/commands/dashboard.js.map +1 -1
- package/dist/cli/commands/doctor.d.ts +2 -0
- package/dist/cli/commands/doctor.js +118 -0
- package/dist/cli/commands/doctor.js.map +1 -0
- package/dist/cli/commands/explain.d.ts +2 -0
- package/dist/cli/commands/explain.js +76 -0
- package/dist/cli/commands/explain.js.map +1 -0
- package/dist/cli/commands/projects.d.ts +2 -0
- package/dist/cli/commands/projects.js +36 -0
- package/dist/cli/commands/projects.js.map +1 -0
- package/dist/code/analyzer.d.ts +6 -0
- package/dist/code/analyzer.js +35 -0
- package/dist/code/analyzer.js.map +1 -1
- package/dist/code/matcher.d.ts +11 -1
- package/dist/code/matcher.js +49 -0
- package/dist/code/matcher.js.map +1 -1
- package/dist/code/scorer.d.ts +1 -0
- package/dist/code/scorer.js +15 -1
- package/dist/code/scorer.js.map +1 -1
- package/dist/config.js +31 -0
- package/dist/config.js.map +1 -1
- package/dist/dashboard/server.d.ts +15 -0
- package/dist/dashboard/server.js +124 -0
- package/dist/dashboard/server.js.map +1 -0
- package/dist/db/migrations/007_feedback.d.ts +2 -0
- package/dist/db/migrations/007_feedback.js +12 -0
- package/dist/db/migrations/007_feedback.js.map +1 -0
- package/dist/db/migrations/008_git_integration.d.ts +2 -0
- package/dist/db/migrations/008_git_integration.js +37 -0
- package/dist/db/migrations/008_git_integration.js.map +1 -0
- package/dist/db/migrations/009_embeddings.d.ts +2 -0
- package/dist/db/migrations/009_embeddings.js +7 -0
- package/dist/db/migrations/009_embeddings.js.map +1 -0
- package/dist/db/migrations/index.js +6 -0
- package/dist/db/migrations/index.js.map +1 -1
- package/dist/db/repositories/code-module.repository.d.ts +16 -0
- package/dist/db/repositories/code-module.repository.js +42 -0
- package/dist/db/repositories/code-module.repository.js.map +1 -1
- package/dist/db/repositories/error.repository.d.ts +5 -0
- package/dist/db/repositories/error.repository.js +27 -0
- package/dist/db/repositories/error.repository.js.map +1 -1
- package/dist/db/repositories/insight.repository.d.ts +2 -0
- package/dist/db/repositories/insight.repository.js +13 -0
- package/dist/db/repositories/insight.repository.js.map +1 -1
- package/dist/embeddings/engine.d.ts +42 -0
- package/dist/embeddings/engine.js +166 -0
- package/dist/embeddings/engine.js.map +1 -0
- package/dist/hooks/post-tool-use.js +2 -0
- package/dist/hooks/post-tool-use.js.map +1 -1
- package/dist/hooks/post-write.js +11 -0
- package/dist/hooks/post-write.js.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/ipc/router.d.ts +2 -0
- package/dist/ipc/router.js +15 -0
- package/dist/ipc/router.js.map +1 -1
- package/dist/learning/confidence-scorer.d.ts +16 -0
- package/dist/learning/confidence-scorer.js +20 -0
- package/dist/learning/confidence-scorer.js.map +1 -1
- package/dist/learning/learning-engine.js +12 -5
- package/dist/learning/learning-engine.js.map +1 -1
- package/dist/matching/error-matcher.d.ts +9 -1
- package/dist/matching/error-matcher.js +50 -5
- package/dist/matching/error-matcher.js.map +1 -1
- package/dist/mcp/http-server.d.ts +14 -0
- package/dist/mcp/http-server.js +117 -0
- package/dist/mcp/http-server.js.map +1 -0
- package/dist/mcp/tools.d.ts +4 -0
- package/dist/mcp/tools.js +41 -14
- package/dist/mcp/tools.js.map +1 -1
- package/dist/services/analytics.service.d.ts +39 -0
- package/dist/services/analytics.service.js +111 -0
- package/dist/services/analytics.service.js.map +1 -1
- package/dist/services/code.service.d.ts +10 -0
- package/dist/services/code.service.js +73 -4
- package/dist/services/code.service.js.map +1 -1
- package/dist/services/error.service.d.ts +17 -1
- package/dist/services/error.service.js +90 -12
- package/dist/services/error.service.js.map +1 -1
- package/dist/services/git.service.d.ts +49 -0
- package/dist/services/git.service.js +112 -0
- package/dist/services/git.service.js.map +1 -0
- package/dist/services/prevention.service.d.ts +7 -0
- package/dist/services/prevention.service.js +38 -0
- package/dist/services/prevention.service.js.map +1 -1
- package/dist/services/research.service.d.ts +1 -0
- package/dist/services/research.service.js +4 -0
- package/dist/services/research.service.js.map +1 -1
- package/dist/services/solution.service.d.ts +10 -0
- package/dist/services/solution.service.js +48 -0
- package/dist/services/solution.service.js.map +1 -1
- package/dist/types/config.types.d.ts +21 -0
- package/dist/types/synapse.types.d.ts +1 -1
- package/package.json +8 -3
- package/src/api/server.ts +321 -0
- package/src/brain.ts +50 -8
- package/src/cli/commands/dashboard.ts +2 -0
- package/src/cli/commands/doctor.ts +118 -0
- package/src/cli/commands/explain.ts +83 -0
- package/src/cli/commands/projects.ts +42 -0
- package/src/code/analyzer.ts +40 -0
- package/src/code/matcher.ts +67 -2
- package/src/code/scorer.ts +13 -1
- package/src/config.ts +24 -0
- package/src/dashboard/server.ts +142 -0
- package/src/db/migrations/007_feedback.ts +13 -0
- package/src/db/migrations/008_git_integration.ts +38 -0
- package/src/db/migrations/009_embeddings.ts +8 -0
- package/src/db/migrations/index.ts +6 -0
- package/src/db/repositories/code-module.repository.ts +53 -0
- package/src/db/repositories/error.repository.ts +40 -0
- package/src/db/repositories/insight.repository.ts +21 -0
- package/src/embeddings/engine.ts +217 -0
- package/src/hooks/post-tool-use.ts +2 -0
- package/src/hooks/post-write.ts +12 -0
- package/src/index.ts +7 -1
- package/src/ipc/router.ts +19 -0
- package/src/learning/confidence-scorer.ts +33 -0
- package/src/learning/learning-engine.ts +13 -5
- package/src/matching/error-matcher.ts +55 -4
- package/src/mcp/http-server.ts +137 -0
- package/src/mcp/tools.ts +52 -14
- package/src/services/analytics.service.ts +136 -0
- package/src/services/code.service.ts +99 -4
- package/src/services/error.service.ts +114 -13
- package/src/services/git.service.ts +132 -0
- package/src/services/prevention.service.ts +40 -0
- package/src/services/research.service.ts +5 -0
- package/src/services/solution.service.ts +58 -0
- package/src/types/config.types.ts +24 -0
- package/src/types/synapse.types.ts +1 -0
|
@@ -6,6 +6,9 @@ import { up as codeSchema } from './003_code_schema.js';
|
|
|
6
6
|
import { up as synapsesSchema } from './004_synapses_schema.js';
|
|
7
7
|
import { up as ftsIndexes } from './005_fts_indexes.js';
|
|
8
8
|
import { up as synapsesPhase3 } from './006_synapses_phase3.js';
|
|
9
|
+
import { up as feedbackSchema } from './007_feedback.js';
|
|
10
|
+
import { up as gitIntegration } from './008_git_integration.js';
|
|
11
|
+
import { up as embeddings } from './009_embeddings.js';
|
|
9
12
|
|
|
10
13
|
interface Migration {
|
|
11
14
|
version: number;
|
|
@@ -20,6 +23,9 @@ const migrations: Migration[] = [
|
|
|
20
23
|
{ version: 4, name: '004_synapses_schema', up: synapsesSchema },
|
|
21
24
|
{ version: 5, name: '005_fts_indexes', up: ftsIndexes },
|
|
22
25
|
{ version: 6, name: '006_synapses_phase3', up: synapsesPhase3 },
|
|
26
|
+
{ version: 7, name: '007_feedback', up: feedbackSchema },
|
|
27
|
+
{ version: 8, name: '008_git_integration', up: gitIntegration },
|
|
28
|
+
{ version: 9, name: '009_embeddings', up: embeddings },
|
|
23
29
|
];
|
|
24
30
|
|
|
25
31
|
function ensureMigrationsTable(db: Database.Database): void {
|
|
@@ -26,6 +26,33 @@ export class CodeModuleRepository {
|
|
|
26
26
|
WHERE code_modules_fts MATCH ?
|
|
27
27
|
ORDER BY rank
|
|
28
28
|
`),
|
|
29
|
+
upsertSimilarity: db.prepare(`
|
|
30
|
+
INSERT INTO module_similarities (module_a_id, module_b_id, similarity_score)
|
|
31
|
+
VALUES (@module_a_id, @module_b_id, @similarity_score)
|
|
32
|
+
ON CONFLICT(module_a_id, module_b_id)
|
|
33
|
+
DO UPDATE SET similarity_score = @similarity_score, computed_at = datetime('now')
|
|
34
|
+
`),
|
|
35
|
+
findSimilarModules: db.prepare(`
|
|
36
|
+
SELECT ms.*, cm.name, cm.file_path, cm.language, cm.reusability_score
|
|
37
|
+
FROM module_similarities ms
|
|
38
|
+
JOIN code_modules cm ON (
|
|
39
|
+
CASE WHEN ms.module_a_id = ? THEN ms.module_b_id ELSE ms.module_a_id END
|
|
40
|
+
) = cm.id
|
|
41
|
+
WHERE ms.module_a_id = ? OR ms.module_b_id = ?
|
|
42
|
+
ORDER BY ms.similarity_score DESC
|
|
43
|
+
LIMIT ?
|
|
44
|
+
`),
|
|
45
|
+
findHighSimilarityPairs: db.prepare(`
|
|
46
|
+
SELECT ms.*,
|
|
47
|
+
a.name as a_name, a.file_path as a_path,
|
|
48
|
+
b.name as b_name, b.file_path as b_path
|
|
49
|
+
FROM module_similarities ms
|
|
50
|
+
JOIN code_modules a ON ms.module_a_id = a.id
|
|
51
|
+
JOIN code_modules b ON ms.module_b_id = b.id
|
|
52
|
+
WHERE ms.similarity_score >= ?
|
|
53
|
+
ORDER BY ms.similarity_score DESC
|
|
54
|
+
LIMIT ?
|
|
55
|
+
`),
|
|
29
56
|
};
|
|
30
57
|
}
|
|
31
58
|
|
|
@@ -86,4 +113,30 @@ export class CodeModuleRepository {
|
|
|
86
113
|
countAll(): number {
|
|
87
114
|
return (this.stmts.countAll.get() as { count: number }).count;
|
|
88
115
|
}
|
|
116
|
+
|
|
117
|
+
upsertSimilarity(moduleAId: number, moduleBId: number, score: number): void {
|
|
118
|
+
// Always store with smaller id first for consistency
|
|
119
|
+
const [a, b] = moduleAId < moduleBId ? [moduleAId, moduleBId] : [moduleBId, moduleAId];
|
|
120
|
+
this.stmts.upsertSimilarity.run({
|
|
121
|
+
module_a_id: a,
|
|
122
|
+
module_b_id: b,
|
|
123
|
+
similarity_score: score,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
findSimilarModules(moduleId: number, limit: number = 10): Array<{ module_id: number; similarity_score: number; name: string; file_path: string }> {
|
|
128
|
+
return this.stmts.findSimilarModules.all(moduleId, moduleId, moduleId, limit) as Array<{
|
|
129
|
+
module_id: number; similarity_score: number; name: string; file_path: string;
|
|
130
|
+
}>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
findHighSimilarityPairs(minScore: number = 0.75, limit: number = 50): Array<{
|
|
134
|
+
module_a_id: number; module_b_id: number; similarity_score: number;
|
|
135
|
+
a_name: string; a_path: string; b_name: string; b_path: string;
|
|
136
|
+
}> {
|
|
137
|
+
return this.stmts.findHighSimilarityPairs.all(minScore, limit) as Array<{
|
|
138
|
+
module_a_id: number; module_b_id: number; similarity_score: number;
|
|
139
|
+
a_name: string; a_path: string; b_name: string; b_path: string;
|
|
140
|
+
}>;
|
|
141
|
+
}
|
|
89
142
|
}
|
|
@@ -14,6 +14,22 @@ export class ErrorRepository {
|
|
|
14
14
|
INSERT INTO errors (project_id, terminal_id, fingerprint, type, message, raw_output, context, file_path, line_number, column_number)
|
|
15
15
|
VALUES (@project_id, @terminal_id, @fingerprint, @type, @message, @raw_output, @context, @file_path, @line_number, @column_number)
|
|
16
16
|
`),
|
|
17
|
+
createChain: this.db.prepare(`
|
|
18
|
+
INSERT OR IGNORE INTO error_chains (parent_error_id, child_error_id, relationship)
|
|
19
|
+
VALUES (@parent_error_id, @child_error_id, @relationship)
|
|
20
|
+
`),
|
|
21
|
+
findChainChildren: this.db.prepare(
|
|
22
|
+
'SELECT e.* FROM errors e JOIN error_chains ec ON e.id = ec.child_error_id WHERE ec.parent_error_id = ?'
|
|
23
|
+
),
|
|
24
|
+
findChainParents: this.db.prepare(
|
|
25
|
+
'SELECT e.* FROM errors e JOIN error_chains ec ON e.id = ec.parent_error_id WHERE ec.child_error_id = ?'
|
|
26
|
+
),
|
|
27
|
+
findRecentByProject: this.db.prepare(
|
|
28
|
+
'SELECT * FROM errors WHERE project_id = ? AND first_seen >= ? ORDER BY first_seen DESC LIMIT ?'
|
|
29
|
+
),
|
|
30
|
+
findAllPaginated: this.db.prepare(
|
|
31
|
+
'SELECT * FROM errors ORDER BY last_seen DESC LIMIT ? OFFSET ?'
|
|
32
|
+
),
|
|
17
33
|
getById: this.db.prepare(`
|
|
18
34
|
SELECT * FROM errors WHERE id = ?
|
|
19
35
|
`),
|
|
@@ -146,4 +162,28 @@ export class ErrorRepository {
|
|
|
146
162
|
incrementOccurrence(id: number): void {
|
|
147
163
|
this.stmts.incrementOccurrence.run(id);
|
|
148
164
|
}
|
|
165
|
+
|
|
166
|
+
createChain(parentErrorId: number, childErrorId: number, relationship: string = 'caused_by_fix'): void {
|
|
167
|
+
this.stmts.createChain.run({
|
|
168
|
+
parent_error_id: parentErrorId,
|
|
169
|
+
child_error_id: childErrorId,
|
|
170
|
+
relationship,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
findChainChildren(errorId: number): ErrorRecord[] {
|
|
175
|
+
return this.stmts.findChainChildren.all(errorId) as ErrorRecord[];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
findChainParents(errorId: number): ErrorRecord[] {
|
|
179
|
+
return this.stmts.findChainParents.all(errorId) as ErrorRecord[];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
findRecentByProject(projectId: number, since: string, limit: number = 10): ErrorRecord[] {
|
|
183
|
+
return this.stmts.findRecentByProject.all(projectId, since, limit) as ErrorRecord[];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
findAll(limit: number = 100, offset: number = 0): ErrorRecord[] {
|
|
187
|
+
return this.stmts.findAllPaginated.all(limit, offset) as ErrorRecord[];
|
|
188
|
+
}
|
|
149
189
|
}
|
|
@@ -75,4 +75,25 @@ export class InsightRepository {
|
|
|
75
75
|
const result = this.stmts.expire.run();
|
|
76
76
|
return result.changes;
|
|
77
77
|
}
|
|
78
|
+
|
|
79
|
+
rate(id: number, rating: number, comment?: string): boolean {
|
|
80
|
+
const stmt = this.db.prepare(
|
|
81
|
+
`UPDATE insights SET rating = ?, rating_comment = ?, rated_at = datetime('now') WHERE id = ?`
|
|
82
|
+
);
|
|
83
|
+
const result = stmt.run(rating, comment ?? null, id);
|
|
84
|
+
return result.changes > 0;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
findRated(minRating?: number): InsightRecord[] {
|
|
88
|
+
if (minRating !== undefined) {
|
|
89
|
+
const stmt = this.db.prepare(
|
|
90
|
+
'SELECT * FROM insights WHERE rating IS NOT NULL AND rating >= ? ORDER BY rating DESC'
|
|
91
|
+
);
|
|
92
|
+
return stmt.all(minRating) as InsightRecord[];
|
|
93
|
+
}
|
|
94
|
+
const stmt = this.db.prepare(
|
|
95
|
+
'SELECT * FROM insights WHERE rating IS NOT NULL ORDER BY rating DESC'
|
|
96
|
+
);
|
|
97
|
+
return stmt.all() as InsightRecord[];
|
|
98
|
+
}
|
|
78
99
|
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import type Database from 'better-sqlite3';
|
|
3
|
+
import { getLogger } from '../utils/logger.js';
|
|
4
|
+
|
|
5
|
+
export interface EmbeddingConfig {
|
|
6
|
+
enabled: boolean;
|
|
7
|
+
modelName: string;
|
|
8
|
+
cacheDir: string;
|
|
9
|
+
sweepIntervalMs: number;
|
|
10
|
+
batchSize: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
type Pipeline = any;
|
|
15
|
+
|
|
16
|
+
export class EmbeddingEngine {
|
|
17
|
+
private pipeline: Pipeline = null;
|
|
18
|
+
private ready = false;
|
|
19
|
+
private loading = false;
|
|
20
|
+
private logger = getLogger();
|
|
21
|
+
private sweepTimer: ReturnType<typeof setInterval> | null = null;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
private config: EmbeddingConfig,
|
|
25
|
+
private db: Database.Database,
|
|
26
|
+
) {}
|
|
27
|
+
|
|
28
|
+
async initialize(): Promise<void> {
|
|
29
|
+
if (!this.config.enabled || this.loading || this.ready) return;
|
|
30
|
+
|
|
31
|
+
this.loading = true;
|
|
32
|
+
try {
|
|
33
|
+
const { pipeline, env } = await import('@huggingface/transformers');
|
|
34
|
+
env.cacheDir = this.config.cacheDir;
|
|
35
|
+
|
|
36
|
+
this.pipeline = await pipeline(
|
|
37
|
+
'feature-extraction',
|
|
38
|
+
this.config.modelName,
|
|
39
|
+
{ dtype: 'q8' },
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
this.ready = true;
|
|
43
|
+
this.logger.info(`Embedding model loaded: ${this.config.modelName}`);
|
|
44
|
+
} catch (err) {
|
|
45
|
+
this.logger.warn(`Failed to load embedding model (will retry): ${err}`);
|
|
46
|
+
this.ready = false;
|
|
47
|
+
} finally {
|
|
48
|
+
this.loading = false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Start background embedding sweep */
|
|
53
|
+
start(): void {
|
|
54
|
+
if (!this.config.enabled) return;
|
|
55
|
+
|
|
56
|
+
// Initialize model in background
|
|
57
|
+
this.initialize().then(() => {
|
|
58
|
+
if (this.ready) {
|
|
59
|
+
// Run initial sweep
|
|
60
|
+
this.sweep().catch(err => this.logger.error('Embedding sweep error:', err));
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Periodic sweep for new entries
|
|
65
|
+
this.sweepTimer = setInterval(() => {
|
|
66
|
+
if (this.ready) {
|
|
67
|
+
this.sweep().catch(err => this.logger.error('Embedding sweep error:', err));
|
|
68
|
+
}
|
|
69
|
+
}, this.config.sweepIntervalMs);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
stop(): void {
|
|
73
|
+
if (this.sweepTimer) {
|
|
74
|
+
clearInterval(this.sweepTimer);
|
|
75
|
+
this.sweepTimer = null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
isReady(): boolean {
|
|
80
|
+
return this.ready;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Generate embedding for a single text */
|
|
84
|
+
async embed(text: string): Promise<Float32Array | null> {
|
|
85
|
+
if (!this.ready || !this.pipeline) return null;
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
const output = await this.pipeline(text, { pooling: 'mean', normalize: true });
|
|
89
|
+
const data = output.tolist()[0] as number[];
|
|
90
|
+
return new Float32Array(data);
|
|
91
|
+
} catch (err) {
|
|
92
|
+
this.logger.error(`Embedding error: ${err}`);
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Generate embeddings for a batch of texts */
|
|
98
|
+
async embedBatch(texts: string[]): Promise<(Float32Array | null)[]> {
|
|
99
|
+
if (!this.ready || !this.pipeline || texts.length === 0) return texts.map(() => null);
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
const output = await this.pipeline(texts, { pooling: 'mean', normalize: true });
|
|
103
|
+
const list = output.tolist() as number[][];
|
|
104
|
+
return list.map(v => new Float32Array(v));
|
|
105
|
+
} catch (err) {
|
|
106
|
+
this.logger.error(`Batch embedding error: ${err}`);
|
|
107
|
+
return texts.map(() => null);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Compute and store embeddings for entries that don't have them yet */
|
|
112
|
+
async sweep(): Promise<{ errors: number; modules: number }> {
|
|
113
|
+
let errorsProcessed = 0;
|
|
114
|
+
let modulesProcessed = 0;
|
|
115
|
+
|
|
116
|
+
// Process errors without embeddings
|
|
117
|
+
const pendingErrors = this.db.prepare(
|
|
118
|
+
'SELECT id, type, message, context FROM errors WHERE embedding IS NULL ORDER BY id DESC LIMIT ?'
|
|
119
|
+
).all(this.config.batchSize) as Array<{ id: number; type: string; message: string; context: string | null }>;
|
|
120
|
+
|
|
121
|
+
if (pendingErrors.length > 0) {
|
|
122
|
+
const texts = pendingErrors.map(e =>
|
|
123
|
+
[e.type, e.message, e.context].filter(Boolean).join(' ')
|
|
124
|
+
);
|
|
125
|
+
const embeddings = await this.embedBatch(texts);
|
|
126
|
+
|
|
127
|
+
const updateStmt = this.db.prepare('UPDATE errors SET embedding = ? WHERE id = ?');
|
|
128
|
+
for (let i = 0; i < pendingErrors.length; i++) {
|
|
129
|
+
const emb = embeddings[i];
|
|
130
|
+
if (emb) {
|
|
131
|
+
updateStmt.run(EmbeddingEngine.serialize(emb), pendingErrors[i]!.id);
|
|
132
|
+
errorsProcessed++;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Process code modules without embeddings
|
|
138
|
+
const pendingModules = this.db.prepare(
|
|
139
|
+
'SELECT id, name, description, file_path FROM code_modules WHERE embedding IS NULL ORDER BY id DESC LIMIT ?'
|
|
140
|
+
).all(this.config.batchSize) as Array<{ id: number; name: string; description: string | null; file_path: string }>;
|
|
141
|
+
|
|
142
|
+
if (pendingModules.length > 0) {
|
|
143
|
+
const texts = pendingModules.map(m =>
|
|
144
|
+
[m.name, m.description, m.file_path].filter(Boolean).join(' ')
|
|
145
|
+
);
|
|
146
|
+
const embeddings = await this.embedBatch(texts);
|
|
147
|
+
|
|
148
|
+
const updateStmt = this.db.prepare('UPDATE code_modules SET embedding = ? WHERE id = ?');
|
|
149
|
+
for (let i = 0; i < pendingModules.length; i++) {
|
|
150
|
+
const emb = embeddings[i];
|
|
151
|
+
if (emb) {
|
|
152
|
+
updateStmt.run(EmbeddingEngine.serialize(emb), pendingModules[i]!.id);
|
|
153
|
+
modulesProcessed++;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (errorsProcessed > 0 || modulesProcessed > 0) {
|
|
159
|
+
this.logger.info(`Embedding sweep: ${errorsProcessed} errors, ${modulesProcessed} modules processed`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return { errors: errorsProcessed, modules: modulesProcessed };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Load vector scores for error matching (sync - reads pre-computed embeddings from DB) */
|
|
166
|
+
computeErrorVectorScores(errorId: number, projectId: number): Map<number, number> {
|
|
167
|
+
const scores = new Map<number, number>();
|
|
168
|
+
|
|
169
|
+
const errorRow = this.db.prepare(
|
|
170
|
+
'SELECT embedding FROM errors WHERE id = ?'
|
|
171
|
+
).get(errorId) as { embedding: Buffer | null } | undefined;
|
|
172
|
+
|
|
173
|
+
if (!errorRow?.embedding) return scores;
|
|
174
|
+
|
|
175
|
+
const incoming = EmbeddingEngine.deserialize(errorRow.embedding);
|
|
176
|
+
|
|
177
|
+
const candidates = this.db.prepare(
|
|
178
|
+
'SELECT id, embedding FROM errors WHERE project_id = ? AND id != ? AND embedding IS NOT NULL'
|
|
179
|
+
).all(projectId, errorId) as Array<{ id: number; embedding: Buffer }>;
|
|
180
|
+
|
|
181
|
+
for (const c of candidates) {
|
|
182
|
+
const candidate = EmbeddingEngine.deserialize(c.embedding);
|
|
183
|
+
scores.set(c.id, EmbeddingEngine.similarity(incoming, candidate));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return scores;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Load vector scores for code module matching (sync) */
|
|
190
|
+
computeModuleVectorScores(query: string, language?: string): Map<number, number> {
|
|
191
|
+
const scores = new Map<number, number>();
|
|
192
|
+
// This needs async embedding — use cached if available
|
|
193
|
+
// For now, return empty (vector search for modules is done during sweep)
|
|
194
|
+
return scores;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Serialize Float32Array to Buffer for SQLite BLOB storage */
|
|
198
|
+
static serialize(embedding: Float32Array): Buffer {
|
|
199
|
+
return Buffer.from(embedding.buffer, embedding.byteOffset, embedding.byteLength);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Deserialize SQLite BLOB to Float32Array */
|
|
203
|
+
static deserialize(buffer: Buffer): Float32Array {
|
|
204
|
+
const copy = Buffer.from(buffer);
|
|
205
|
+
return new Float32Array(copy.buffer, copy.byteOffset, copy.length / 4);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Cosine similarity (embeddings are L2-normalized, so dot product = cosine) */
|
|
209
|
+
static similarity(a: Float32Array, b: Float32Array): number {
|
|
210
|
+
if (a.length !== b.length) return 0;
|
|
211
|
+
let dot = 0;
|
|
212
|
+
for (let i = 0; i < a.length; i++) {
|
|
213
|
+
dot += a[i]! * b[i]!;
|
|
214
|
+
}
|
|
215
|
+
return Math.max(0, Math.min(1, dot));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
@@ -65,6 +65,8 @@ async function main(): Promise<void> {
|
|
|
65
65
|
const result: any = await client.request('error.report', {
|
|
66
66
|
project: 'auto-detected',
|
|
67
67
|
errorOutput: input.tool_output,
|
|
68
|
+
command: input.tool_input?.command,
|
|
69
|
+
workingDirectory: process.cwd(),
|
|
68
70
|
});
|
|
69
71
|
|
|
70
72
|
if (result.matches?.length > 0) {
|
package/src/hooks/post-write.ts
CHANGED
|
@@ -95,6 +95,18 @@ async function main(): Promise<void> {
|
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
+
// Proactive Prevention: check if written code matches error-causing patterns
|
|
99
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
100
|
+
const prevention: any = await client.request('prevention.checkCode', {
|
|
101
|
+
source: input.tool_input.content ?? '',
|
|
102
|
+
filePath,
|
|
103
|
+
});
|
|
104
|
+
if (prevention?.warnings?.length > 0) {
|
|
105
|
+
for (const warning of prevention.warnings.slice(0, 3)) {
|
|
106
|
+
process.stderr.write(`Brain WARNING: ${warning.message}\n`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
98
110
|
// Auto-register the written file into Brain
|
|
99
111
|
const fileName = filePath.replace(/\\/g, '/').split('/').pop() ?? 'unknown';
|
|
100
112
|
const projectName = detectProject(filePath);
|
package/src/index.ts
CHANGED
|
@@ -13,13 +13,16 @@ import { importCommand } from './cli/commands/import.js';
|
|
|
13
13
|
import { dashboardCommand } from './cli/commands/dashboard.js';
|
|
14
14
|
import { learnCommand } from './cli/commands/learn.js';
|
|
15
15
|
import { configCommand } from './cli/commands/config.js';
|
|
16
|
+
import { projectsCommand } from './cli/commands/projects.js';
|
|
17
|
+
import { doctorCommand } from './cli/commands/doctor.js';
|
|
18
|
+
import { explainCommand } from './cli/commands/explain.js';
|
|
16
19
|
|
|
17
20
|
const program = new Command();
|
|
18
21
|
|
|
19
22
|
program
|
|
20
23
|
.name('brain')
|
|
21
24
|
.description('Brain — Adaptive Error Memory & Code Intelligence System')
|
|
22
|
-
.version('1.
|
|
25
|
+
.version('1.8.0');
|
|
23
26
|
|
|
24
27
|
program.addCommand(startCommand());
|
|
25
28
|
program.addCommand(stopCommand());
|
|
@@ -33,6 +36,9 @@ program.addCommand(importCommand());
|
|
|
33
36
|
program.addCommand(dashboardCommand());
|
|
34
37
|
program.addCommand(learnCommand());
|
|
35
38
|
program.addCommand(configCommand());
|
|
39
|
+
program.addCommand(projectsCommand());
|
|
40
|
+
program.addCommand(doctorCommand());
|
|
41
|
+
program.addCommand(explainCommand());
|
|
36
42
|
|
|
37
43
|
// Hidden command: run MCP server (called by Claude Code)
|
|
38
44
|
program
|
package/src/ipc/router.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type { SynapseService } from '../services/synapse.service.js';
|
|
|
10
10
|
import type { ResearchService } from '../services/research.service.js';
|
|
11
11
|
import type { NotificationService } from '../services/notification.service.js';
|
|
12
12
|
import type { AnalyticsService } from '../services/analytics.service.js';
|
|
13
|
+
import type { GitService } from '../services/git.service.js';
|
|
13
14
|
import type { LearningEngine, LearningCycleResult } from '../learning/learning-engine.js';
|
|
14
15
|
|
|
15
16
|
export interface Services {
|
|
@@ -22,6 +23,7 @@ export interface Services {
|
|
|
22
23
|
research: ResearchService;
|
|
23
24
|
notification: NotificationService;
|
|
24
25
|
analytics: AnalyticsService;
|
|
26
|
+
git: GitService;
|
|
25
27
|
learning?: LearningEngine;
|
|
26
28
|
}
|
|
27
29
|
|
|
@@ -67,12 +69,17 @@ export class IpcRouter {
|
|
|
67
69
|
['error.match', (params) => s.error.matchSimilar(p(params).error_id ?? p(params).errorId)],
|
|
68
70
|
['error.resolve', (params) => s.error.resolve(p(params).error_id ?? p(params).errorId, p(params).solution_id ?? p(params).solutionId)],
|
|
69
71
|
['error.get', (params) => s.error.getById(p(params).id)],
|
|
72
|
+
['error.chain', (params) => s.error.getErrorChain(p(params).error_id ?? p(params).errorId ?? p(params).id)],
|
|
70
73
|
|
|
71
74
|
// Solutions
|
|
72
75
|
['solution.report', (params) => s.solution.report(p(params))],
|
|
73
76
|
['solution.query', (params) => s.solution.findForError(p(params).error_id ?? p(params).errorId)],
|
|
74
77
|
['solution.rate', (params) => s.solution.rateOutcome(p(params))],
|
|
75
78
|
['solution.attempt', (params) => s.solution.rateOutcome({ ...p(params), success: false })],
|
|
79
|
+
['solution.efficiency', () => s.solution.analyzeEfficiency()],
|
|
80
|
+
|
|
81
|
+
// Projects
|
|
82
|
+
['project.list', () => s.code.listProjects()],
|
|
76
83
|
|
|
77
84
|
// Code Brain
|
|
78
85
|
['code.analyze', (params) => s.code.analyzeAndRegister(p(params))],
|
|
@@ -84,6 +91,7 @@ export class IpcRouter {
|
|
|
84
91
|
// Prevention
|
|
85
92
|
['prevention.check', (params) => s.prevention.checkRules(p(params).errorType, p(params).message, p(params).projectId)],
|
|
86
93
|
['prevention.antipatterns', (params) => s.prevention.checkAntipatterns(p(params).errorType ?? '', p(params).message ?? p(params).error_output ?? '', p(params).projectId)],
|
|
94
|
+
['prevention.checkCode', (params) => s.prevention.checkCodeForPatterns(p(params).source, p(params).filePath)],
|
|
87
95
|
|
|
88
96
|
// Synapses
|
|
89
97
|
['synapse.context', (params) => s.synapse.getErrorContext(p(params).errorId ?? p(params).error_id ?? p(params).node_id)],
|
|
@@ -93,6 +101,7 @@ export class IpcRouter {
|
|
|
93
101
|
|
|
94
102
|
// Research / Insights
|
|
95
103
|
['research.insights', (params) => s.research.getInsights(p(params))],
|
|
104
|
+
['insight.rate', (params) => s.research.rateInsight(p(params).id, p(params).rating, p(params).comment)],
|
|
96
105
|
['research.suggest', (params) => s.research.getInsights({ limit: 10, activeOnly: true, ...p(params) })],
|
|
97
106
|
['research.trends', (params) => s.research.getTrends(p(params)?.projectId, p(params)?.windowDays)],
|
|
98
107
|
|
|
@@ -103,6 +112,16 @@ export class IpcRouter {
|
|
|
103
112
|
// Analytics
|
|
104
113
|
['analytics.summary', (params) => s.analytics.getSummary(p(params)?.projectId)],
|
|
105
114
|
['analytics.network', (params) => s.analytics.getNetworkOverview(p(params)?.limit)],
|
|
115
|
+
['analytics.health', (params) => s.analytics.computeHealthScore(p(params)?.projectId)],
|
|
116
|
+
['analytics.timeline', (params) => s.analytics.getTimeSeries(p(params)?.projectId, p(params)?.days)],
|
|
117
|
+
['analytics.explain', (params) => s.analytics.explainError(p(params).errorId ?? p(params).error_id)],
|
|
118
|
+
|
|
119
|
+
// Git
|
|
120
|
+
['git.context', (params) => s.git.getGitContext(p(params)?.cwd)],
|
|
121
|
+
['git.linkError', (params) => s.git.linkErrorToCommit(p(params).errorId, p(params).projectId, p(params).commitHash, p(params).relationship)],
|
|
122
|
+
['git.errorCommits', (params) => s.git.findIntroducingCommit(p(params).errorId ?? p(params).error_id)],
|
|
123
|
+
['git.commitErrors', (params) => s.git.findErrorsByCommit(p(params).commitHash ?? p(params).commit_hash)],
|
|
124
|
+
['git.diff', (params) => s.git.captureDiff(p(params)?.cwd)],
|
|
106
125
|
|
|
107
126
|
// Learning
|
|
108
127
|
['learning.run', () => {
|
|
@@ -45,3 +45,36 @@ export function computeConfidence(
|
|
|
45
45
|
if (total === 0) return 0;
|
|
46
46
|
return timeDecayedConfidence(successCount, total, lastUsedAt, halfLifeDays);
|
|
47
47
|
}
|
|
48
|
+
|
|
49
|
+
export interface AdaptiveThresholds {
|
|
50
|
+
minOccurrences: number;
|
|
51
|
+
minSuccessRate: number;
|
|
52
|
+
minConfidence: number;
|
|
53
|
+
pruneThreshold: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Compute adaptive thresholds based on data volume.
|
|
58
|
+
* Users with lots of data need stricter thresholds; users with little data need looser ones.
|
|
59
|
+
*/
|
|
60
|
+
export function computeAdaptiveThresholds(
|
|
61
|
+
totalErrors: number,
|
|
62
|
+
totalSolutions: number,
|
|
63
|
+
baseConfig: { minOccurrences: number; minSuccessRate: number; minConfidence: number; pruneThreshold: number },
|
|
64
|
+
): AdaptiveThresholds {
|
|
65
|
+
// Scale factor: 1.0 at 50 errors, increases with more data
|
|
66
|
+
const errorScale = Math.min(2.0, Math.max(0.5, totalErrors / 50));
|
|
67
|
+
const solutionScale = Math.min(2.0, Math.max(0.5, totalSolutions / 20));
|
|
68
|
+
const dataScale = (errorScale + solutionScale) / 2;
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
// More data → require more occurrences before learning
|
|
72
|
+
minOccurrences: Math.max(2, Math.round(baseConfig.minOccurrences * dataScale)),
|
|
73
|
+
// More data → can afford higher success rate requirement
|
|
74
|
+
minSuccessRate: Math.min(0.95, baseConfig.minSuccessRate * (0.85 + dataScale * 0.15)),
|
|
75
|
+
// More data → higher confidence threshold
|
|
76
|
+
minConfidence: Math.min(0.90, baseConfig.minConfidence * (0.85 + dataScale * 0.15)),
|
|
77
|
+
// More data → stricter pruning
|
|
78
|
+
pruneThreshold: Math.max(0.10, baseConfig.pruneThreshold * (1.1 - dataScale * 0.1)),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -7,6 +7,7 @@ import type { SynapseManager } from '../synapses/synapse-manager.js';
|
|
|
7
7
|
import { extractPatterns } from './pattern-extractor.js';
|
|
8
8
|
import { generateRules, persistRules } from './rule-generator.js';
|
|
9
9
|
import { shouldPruneRule } from './decay.js';
|
|
10
|
+
import { computeAdaptiveThresholds, type AdaptiveThresholds } from './confidence-scorer.js';
|
|
10
11
|
import { getLogger } from '../utils/logger.js';
|
|
11
12
|
|
|
12
13
|
export interface LearningCycleResult {
|
|
@@ -56,12 +57,18 @@ export class LearningEngine {
|
|
|
56
57
|
duration: 0,
|
|
57
58
|
};
|
|
58
59
|
|
|
60
|
+
// Phase 0: Compute adaptive thresholds
|
|
61
|
+
const totalErrors = this.errorRepo.findUnresolved().length + this.errorRepo.countSince(new Date(0).toISOString());
|
|
62
|
+
const totalSolutions = this.solutionRepo.getAll().length;
|
|
63
|
+
const adaptive = computeAdaptiveThresholds(totalErrors, totalSolutions, this.config);
|
|
64
|
+
this.logger.debug(`Adaptive thresholds: minOcc=${adaptive.minOccurrences}, minSuccess=${adaptive.minSuccessRate.toFixed(2)}, minConf=${adaptive.minConfidence.toFixed(2)}`);
|
|
65
|
+
|
|
59
66
|
// Phase 1: Collect recent errors
|
|
60
67
|
const since = this.lastCycleAt ?? new Date(Date.now() - this.config.intervalMs).toISOString();
|
|
61
68
|
const recentErrors = this.errorRepo.findUnresolved();
|
|
62
69
|
|
|
63
70
|
// Phase 2: Extract patterns
|
|
64
|
-
const patterns = extractPatterns(recentErrors,
|
|
71
|
+
const patterns = extractPatterns(recentErrors, adaptive.minSuccessRate);
|
|
65
72
|
result.newPatterns = patterns.length;
|
|
66
73
|
|
|
67
74
|
// Phase 3: Enrich patterns with solution data
|
|
@@ -87,18 +94,19 @@ export class LearningEngine {
|
|
|
87
94
|
);
|
|
88
95
|
}
|
|
89
96
|
|
|
90
|
-
// Phase 4: Generate rules from patterns
|
|
91
|
-
const
|
|
97
|
+
// Phase 4: Generate rules from patterns (using adaptive thresholds)
|
|
98
|
+
const adaptiveConfig = { ...this.config, ...adaptive };
|
|
99
|
+
const rules = generateRules(patterns, adaptiveConfig);
|
|
92
100
|
result.updatedRules = persistRules(rules, this.ruleRepo);
|
|
93
101
|
|
|
94
|
-
// Phase 5: Prune weak rules
|
|
102
|
+
// Phase 5: Prune weak rules (using adaptive thresholds)
|
|
95
103
|
const activeRules = this.ruleRepo.findActive();
|
|
96
104
|
for (const rule of activeRules) {
|
|
97
105
|
if (shouldPruneRule(
|
|
98
106
|
rule.confidence,
|
|
99
107
|
0, // rejection count not tracked yet
|
|
100
108
|
rule.occurrences,
|
|
101
|
-
|
|
109
|
+
adaptive.pruneThreshold,
|
|
102
110
|
this.config.maxRejectionRate,
|
|
103
111
|
)) {
|
|
104
112
|
this.ruleRepo.update(rule.id, { active: 0 });
|