@timmeck/brain 1.2.0 → 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/explain.d.ts +2 -0
- package/dist/cli/commands/explain.js +76 -0
- package/dist/cli/commands/explain.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 +3 -1
- package/dist/index.js.map +1 -1
- package/dist/ipc/router.d.ts +2 -0
- package/dist/ipc/router.js +13 -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 +2 -0
- package/dist/services/code.service.js +62 -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/explain.ts +83 -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 +3 -1
- package/src/ipc/router.ts +16 -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 +87 -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
|
@@ -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
|
@@ -15,13 +15,14 @@ import { learnCommand } from './cli/commands/learn.js';
|
|
|
15
15
|
import { configCommand } from './cli/commands/config.js';
|
|
16
16
|
import { projectsCommand } from './cli/commands/projects.js';
|
|
17
17
|
import { doctorCommand } from './cli/commands/doctor.js';
|
|
18
|
+
import { explainCommand } from './cli/commands/explain.js';
|
|
18
19
|
|
|
19
20
|
const program = new Command();
|
|
20
21
|
|
|
21
22
|
program
|
|
22
23
|
.name('brain')
|
|
23
24
|
.description('Brain — Adaptive Error Memory & Code Intelligence System')
|
|
24
|
-
.version('1.
|
|
25
|
+
.version('1.8.0');
|
|
25
26
|
|
|
26
27
|
program.addCommand(startCommand());
|
|
27
28
|
program.addCommand(stopCommand());
|
|
@@ -37,6 +38,7 @@ program.addCommand(learnCommand());
|
|
|
37
38
|
program.addCommand(configCommand());
|
|
38
39
|
program.addCommand(projectsCommand());
|
|
39
40
|
program.addCommand(doctorCommand());
|
|
41
|
+
program.addCommand(explainCommand());
|
|
40
42
|
|
|
41
43
|
// Hidden command: run MCP server (called by Claude Code)
|
|
42
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,14 @@ 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()],
|
|
76
80
|
|
|
77
81
|
// Projects
|
|
78
82
|
['project.list', () => s.code.listProjects()],
|
|
@@ -87,6 +91,7 @@ export class IpcRouter {
|
|
|
87
91
|
// Prevention
|
|
88
92
|
['prevention.check', (params) => s.prevention.checkRules(p(params).errorType, p(params).message, p(params).projectId)],
|
|
89
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)],
|
|
90
95
|
|
|
91
96
|
// Synapses
|
|
92
97
|
['synapse.context', (params) => s.synapse.getErrorContext(p(params).errorId ?? p(params).error_id ?? p(params).node_id)],
|
|
@@ -96,6 +101,7 @@ export class IpcRouter {
|
|
|
96
101
|
|
|
97
102
|
// Research / Insights
|
|
98
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)],
|
|
99
105
|
['research.suggest', (params) => s.research.getInsights({ limit: 10, activeOnly: true, ...p(params) })],
|
|
100
106
|
['research.trends', (params) => s.research.getTrends(p(params)?.projectId, p(params)?.windowDays)],
|
|
101
107
|
|
|
@@ -106,6 +112,16 @@ export class IpcRouter {
|
|
|
106
112
|
// Analytics
|
|
107
113
|
['analytics.summary', (params) => s.analytics.getSummary(p(params)?.projectId)],
|
|
108
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)],
|
|
109
125
|
|
|
110
126
|
// Learning
|
|
111
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 });
|
|
@@ -21,7 +21,8 @@ interface MatchSignal {
|
|
|
21
21
|
compute: (a: ErrorRecord, b: ErrorRecord) => number;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
// Base signals (used when vector search is NOT available)
|
|
25
|
+
const SIGNALS_BASE: MatchSignal[] = [
|
|
25
26
|
{ name: 'fingerprint', weight: 0.30, compute: fingerprintMatch },
|
|
26
27
|
{ name: 'message_similarity', weight: 0.20, compute: messageSimilarity },
|
|
27
28
|
{ name: 'type_match', weight: 0.15, compute: typeMatch },
|
|
@@ -30,16 +31,41 @@ const SIGNALS: MatchSignal[] = [
|
|
|
30
31
|
{ name: 'context_similarity', weight: 0.10, compute: contextSimilarity },
|
|
31
32
|
];
|
|
32
33
|
|
|
34
|
+
// Hybrid signals (used when vector search IS available — vector gets 20% weight)
|
|
35
|
+
const SIGNALS_HYBRID: MatchSignal[] = [
|
|
36
|
+
{ name: 'fingerprint', weight: 0.25, compute: fingerprintMatch },
|
|
37
|
+
{ name: 'message_similarity', weight: 0.15, compute: messageSimilarity },
|
|
38
|
+
{ name: 'type_match', weight: 0.12, compute: typeMatch },
|
|
39
|
+
{ name: 'stack_similarity', weight: 0.12, compute: stackSimilarity },
|
|
40
|
+
{ name: 'file_similarity', weight: 0.08, compute: fileSimilarity },
|
|
41
|
+
{ name: 'context_similarity', weight: 0.08, compute: contextSimilarity },
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
const VECTOR_WEIGHT = 0.20;
|
|
33
45
|
const MATCH_THRESHOLD = 0.70;
|
|
34
46
|
const STRONG_MATCH_THRESHOLD = 0.90;
|
|
35
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Hybrid error matching: TF-IDF signals + optional vector similarity + synapse boost.
|
|
50
|
+
*
|
|
51
|
+
* @param incoming - The error to match
|
|
52
|
+
* @param candidates - Candidate errors to compare against
|
|
53
|
+
* @param vectorScores - Pre-computed vector similarity scores (errorId → score)
|
|
54
|
+
* @param synapseScores - Pre-computed synapse proximity scores (errorId → score)
|
|
55
|
+
*/
|
|
36
56
|
export function matchError(
|
|
37
57
|
incoming: ErrorRecord,
|
|
38
58
|
candidates: ErrorRecord[],
|
|
59
|
+
vectorScores?: Map<number, number>,
|
|
60
|
+
synapseScores?: Map<number, number>,
|
|
39
61
|
): MatchResult[] {
|
|
62
|
+
const useHybrid = vectorScores && vectorScores.size > 0;
|
|
63
|
+
const useSynapse = synapseScores && synapseScores.size > 0;
|
|
64
|
+
const signals = useHybrid ? SIGNALS_HYBRID : SIGNALS_BASE;
|
|
65
|
+
|
|
40
66
|
return candidates
|
|
41
67
|
.map(candidate => {
|
|
42
|
-
const
|
|
68
|
+
const signalResults = signals.map(signal => {
|
|
43
69
|
const score = signal.compute(incoming, candidate);
|
|
44
70
|
return {
|
|
45
71
|
signal: signal.name,
|
|
@@ -48,12 +74,37 @@ export function matchError(
|
|
|
48
74
|
};
|
|
49
75
|
});
|
|
50
76
|
|
|
51
|
-
|
|
77
|
+
// Add vector similarity signal (if available)
|
|
78
|
+
if (useHybrid) {
|
|
79
|
+
const vectorScore = vectorScores.get(candidate.id) ?? 0;
|
|
80
|
+
signalResults.push({
|
|
81
|
+
signal: 'vector_similarity',
|
|
82
|
+
score: vectorScore,
|
|
83
|
+
weighted: vectorScore * VECTOR_WEIGHT,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let totalScore = signalResults.reduce((sum, s) => sum + s.weighted, 0);
|
|
88
|
+
|
|
89
|
+
// Synapse boost: if errors are already connected in the synapse network,
|
|
90
|
+
// give up to 5% bonus (doesn't create false positives, only reinforces)
|
|
91
|
+
if (useSynapse) {
|
|
92
|
+
const synapseScore = synapseScores.get(candidate.id) ?? 0;
|
|
93
|
+
if (synapseScore > 0) {
|
|
94
|
+
const bonus = Math.min(synapseScore * 0.05, 0.05);
|
|
95
|
+
totalScore = Math.min(1.0, totalScore + bonus);
|
|
96
|
+
signalResults.push({
|
|
97
|
+
signal: 'synapse_boost',
|
|
98
|
+
score: synapseScore,
|
|
99
|
+
weighted: bonus,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
52
103
|
|
|
53
104
|
return {
|
|
54
105
|
errorId: candidate.id,
|
|
55
106
|
score: totalScore,
|
|
56
|
-
signals,
|
|
107
|
+
signals: signalResults,
|
|
57
108
|
isStrong: totalScore >= STRONG_MATCH_THRESHOLD,
|
|
58
109
|
};
|
|
59
110
|
})
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
4
|
+
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|
5
|
+
import { getLogger } from '../utils/logger.js';
|
|
6
|
+
import type { IpcRouter } from '../ipc/router.js';
|
|
7
|
+
import { registerToolsDirect } from './tools.js';
|
|
8
|
+
|
|
9
|
+
export class McpHttpServer {
|
|
10
|
+
private server: http.Server | null = null;
|
|
11
|
+
private transports = new Map<string, SSEServerTransport>();
|
|
12
|
+
private logger = getLogger();
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
private port: number,
|
|
16
|
+
private router: IpcRouter,
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
start(): void {
|
|
20
|
+
this.server = http.createServer((req, res) => {
|
|
21
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
22
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS');
|
|
23
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
24
|
+
|
|
25
|
+
if (req.method === 'OPTIONS') {
|
|
26
|
+
res.writeHead(204);
|
|
27
|
+
res.end();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const url = new URL(req.url ?? '/', `http://localhost:${this.port}`);
|
|
32
|
+
|
|
33
|
+
if (url.pathname === '/sse' && req.method === 'GET') {
|
|
34
|
+
this.handleSSE(res);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (url.pathname === '/messages' && req.method === 'POST') {
|
|
39
|
+
this.handleMessage(req, res, url);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (url.pathname === '/' && req.method === 'GET') {
|
|
44
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
45
|
+
res.end(JSON.stringify({
|
|
46
|
+
name: 'brain',
|
|
47
|
+
version: '1.7.0',
|
|
48
|
+
protocol: 'MCP',
|
|
49
|
+
transport: 'sse',
|
|
50
|
+
endpoints: {
|
|
51
|
+
sse: '/sse',
|
|
52
|
+
messages: '/messages',
|
|
53
|
+
},
|
|
54
|
+
clients: this.transports.size,
|
|
55
|
+
}));
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
60
|
+
res.end('Not Found');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
this.server.listen(this.port, () => {
|
|
64
|
+
this.logger.info(`MCP HTTP server (SSE) started on http://localhost:${this.port}`);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
stop(): void {
|
|
69
|
+
this.transports.clear();
|
|
70
|
+
this.server?.close();
|
|
71
|
+
this.server = null;
|
|
72
|
+
this.logger.info('MCP HTTP server stopped');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
getClientCount(): number {
|
|
76
|
+
return this.transports.size;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private async handleSSE(res: http.ServerResponse): Promise<void> {
|
|
80
|
+
try {
|
|
81
|
+
const transport = new SSEServerTransport('/messages', res);
|
|
82
|
+
const sessionId = transport.sessionId ?? randomUUID();
|
|
83
|
+
this.transports.set(sessionId, transport);
|
|
84
|
+
|
|
85
|
+
const server = new McpServer({
|
|
86
|
+
name: 'brain',
|
|
87
|
+
version: '1.7.0',
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
registerToolsDirect(server, this.router);
|
|
91
|
+
|
|
92
|
+
res.on('close', () => {
|
|
93
|
+
this.transports.delete(sessionId);
|
|
94
|
+
this.logger.debug(`MCP SSE client disconnected: ${sessionId}`);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
await server.connect(transport);
|
|
98
|
+
this.logger.info(`MCP SSE client connected: ${sessionId}`);
|
|
99
|
+
} catch (err) {
|
|
100
|
+
this.logger.error('MCP SSE connection error:', err);
|
|
101
|
+
if (!res.headersSent) {
|
|
102
|
+
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
103
|
+
res.end('Internal Server Error');
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private async handleMessage(
|
|
109
|
+
req: http.IncomingMessage,
|
|
110
|
+
res: http.ServerResponse,
|
|
111
|
+
url: URL,
|
|
112
|
+
): Promise<void> {
|
|
113
|
+
try {
|
|
114
|
+
const sessionId = url.searchParams.get('sessionId');
|
|
115
|
+
if (!sessionId) {
|
|
116
|
+
res.writeHead(400, { 'Content-Type': 'text/plain' });
|
|
117
|
+
res.end('Missing sessionId parameter');
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const transport = this.transports.get(sessionId);
|
|
122
|
+
if (!transport) {
|
|
123
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
124
|
+
res.end('Session not found. Connect to /sse first.');
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await transport.handlePostMessage(req, res);
|
|
129
|
+
} catch (err) {
|
|
130
|
+
this.logger.error('MCP message error:', err);
|
|
131
|
+
if (!res.headersSent) {
|
|
132
|
+
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
133
|
+
res.end('Internal Server Error');
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|