@timmeck/brain 1.9.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/README.md +19 -0
  2. package/brain.log +1164 -0
  3. package/{src/cli/commands/dashboard.ts → dashboard.html} +688 -807
  4. package/dist/api/server.d.ts +4 -18
  5. package/dist/api/server.js +4 -173
  6. package/dist/api/server.js.map +1 -1
  7. package/dist/brain.d.ts +1 -0
  8. package/dist/brain.js +6 -1
  9. package/dist/brain.js.map +1 -1
  10. package/dist/cli/colors.d.ts +4 -25
  11. package/dist/cli/colors.js +3 -89
  12. package/dist/cli/colors.js.map +1 -1
  13. package/dist/cli/commands/peers.d.ts +2 -0
  14. package/dist/cli/commands/peers.js +38 -0
  15. package/dist/cli/commands/peers.js.map +1 -0
  16. package/dist/db/connection.d.ts +1 -2
  17. package/dist/db/connection.js +1 -18
  18. package/dist/db/connection.js.map +1 -1
  19. package/dist/index.js +3 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/ipc/__tests__/protocol.test.d.ts +1 -0
  22. package/dist/ipc/__tests__/protocol.test.js +117 -0
  23. package/dist/ipc/__tests__/protocol.test.js.map +1 -0
  24. package/dist/ipc/client.d.ts +1 -16
  25. package/dist/ipc/client.js +1 -100
  26. package/dist/ipc/client.js.map +1 -1
  27. package/dist/ipc/protocol.d.ts +1 -8
  28. package/dist/ipc/protocol.js +1 -28
  29. package/dist/ipc/protocol.js.map +1 -1
  30. package/dist/ipc/router.js +8 -0
  31. package/dist/ipc/router.js.map +1 -1
  32. package/dist/ipc/server.d.ts +1 -22
  33. package/dist/ipc/server.js +1 -163
  34. package/dist/ipc/server.js.map +1 -1
  35. package/dist/mcp/http-server.d.ts +1 -7
  36. package/dist/mcp/http-server.js +6 -117
  37. package/dist/mcp/http-server.js.map +1 -1
  38. package/dist/mcp/server.js +5 -61
  39. package/dist/mcp/server.js.map +1 -1
  40. package/dist/signals/__tests__/fingerprint.test.d.ts +1 -0
  41. package/dist/signals/__tests__/fingerprint.test.js +118 -0
  42. package/dist/signals/__tests__/fingerprint.test.js.map +1 -0
  43. package/dist/types/ipc.types.d.ts +1 -11
  44. package/dist/utils/__tests__/hash.test.d.ts +1 -0
  45. package/dist/utils/__tests__/hash.test.js +32 -0
  46. package/dist/utils/__tests__/hash.test.js.map +1 -0
  47. package/dist/utils/__tests__/paths.test.d.ts +1 -0
  48. package/dist/utils/__tests__/paths.test.js +75 -0
  49. package/dist/utils/__tests__/paths.test.js.map +1 -0
  50. package/dist/utils/events.d.ts +4 -8
  51. package/dist/utils/events.js +2 -14
  52. package/dist/utils/events.js.map +1 -1
  53. package/dist/utils/hash.d.ts +1 -1
  54. package/dist/utils/hash.js +1 -4
  55. package/dist/utils/hash.js.map +1 -1
  56. package/dist/utils/logger.d.ts +3 -2
  57. package/dist/utils/logger.js +8 -35
  58. package/dist/utils/logger.js.map +1 -1
  59. package/dist/utils/paths.d.ts +2 -1
  60. package/dist/utils/paths.js +4 -13
  61. package/dist/utils/paths.js.map +1 -1
  62. package/package.json +2 -1
  63. package/BRAIN_PLAN.md +0 -3324
  64. package/reddit_post.md +0 -45
  65. package/src/api/server.ts +0 -395
  66. package/src/brain.ts +0 -313
  67. package/src/cli/colors.ts +0 -116
  68. package/src/cli/commands/config.ts +0 -169
  69. package/src/cli/commands/doctor.ts +0 -124
  70. package/src/cli/commands/explain.ts +0 -83
  71. package/src/cli/commands/export.ts +0 -31
  72. package/src/cli/commands/import.ts +0 -199
  73. package/src/cli/commands/insights.ts +0 -65
  74. package/src/cli/commands/learn.ts +0 -24
  75. package/src/cli/commands/modules.ts +0 -53
  76. package/src/cli/commands/network.ts +0 -67
  77. package/src/cli/commands/projects.ts +0 -42
  78. package/src/cli/commands/query.ts +0 -120
  79. package/src/cli/commands/start.ts +0 -105
  80. package/src/cli/commands/status.ts +0 -75
  81. package/src/cli/commands/stop.ts +0 -34
  82. package/src/cli/ipc-helper.ts +0 -22
  83. package/src/cli/update-check.ts +0 -63
  84. package/src/code/analyzer.ts +0 -117
  85. package/src/code/fingerprint.ts +0 -87
  86. package/src/code/matcher.ts +0 -129
  87. package/src/code/parsers/generic.ts +0 -29
  88. package/src/code/parsers/python.ts +0 -54
  89. package/src/code/parsers/typescript.ts +0 -65
  90. package/src/code/registry.ts +0 -60
  91. package/src/code/scorer.ts +0 -120
  92. package/src/config.ts +0 -135
  93. package/src/dashboard/server.ts +0 -142
  94. package/src/db/connection.ts +0 -22
  95. package/src/db/migrations/001_core_schema.ts +0 -120
  96. package/src/db/migrations/002_learning_schema.ts +0 -38
  97. package/src/db/migrations/003_code_schema.ts +0 -53
  98. package/src/db/migrations/004_synapses_schema.ts +0 -57
  99. package/src/db/migrations/005_fts_indexes.ts +0 -78
  100. package/src/db/migrations/006_synapses_phase3.ts +0 -17
  101. package/src/db/migrations/007_feedback.ts +0 -13
  102. package/src/db/migrations/008_git_integration.ts +0 -38
  103. package/src/db/migrations/009_embeddings.ts +0 -8
  104. package/src/db/migrations/index.ts +0 -70
  105. package/src/db/repositories/antipattern.repository.ts +0 -66
  106. package/src/db/repositories/code-module.repository.ts +0 -142
  107. package/src/db/repositories/error.repository.ts +0 -189
  108. package/src/db/repositories/insight.repository.ts +0 -99
  109. package/src/db/repositories/notification.repository.ts +0 -66
  110. package/src/db/repositories/project.repository.ts +0 -93
  111. package/src/db/repositories/rule.repository.ts +0 -108
  112. package/src/db/repositories/solution.repository.ts +0 -154
  113. package/src/db/repositories/synapse.repository.ts +0 -163
  114. package/src/db/repositories/terminal.repository.ts +0 -101
  115. package/src/embeddings/engine.ts +0 -238
  116. package/src/hooks/post-tool-use.ts +0 -92
  117. package/src/hooks/post-write.ts +0 -129
  118. package/src/index.ts +0 -63
  119. package/src/ipc/client.ts +0 -118
  120. package/src/ipc/protocol.ts +0 -35
  121. package/src/ipc/router.ts +0 -133
  122. package/src/ipc/server.ts +0 -176
  123. package/src/learning/confidence-scorer.ts +0 -80
  124. package/src/learning/decay.ts +0 -46
  125. package/src/learning/learning-engine.ts +0 -170
  126. package/src/learning/pattern-extractor.ts +0 -90
  127. package/src/learning/rule-generator.ts +0 -74
  128. package/src/main.rs:10:5 +0 -0
  129. package/src/matching/error-matcher.ts +0 -166
  130. package/src/matching/fingerprint.ts +0 -34
  131. package/src/matching/similarity.ts +0 -61
  132. package/src/matching/tfidf.ts +0 -74
  133. package/src/matching/tokenizer.ts +0 -41
  134. package/src/mcp/auto-detect.ts +0 -93
  135. package/src/mcp/http-server.ts +0 -140
  136. package/src/mcp/server.ts +0 -73
  137. package/src/mcp/tools.ts +0 -328
  138. package/src/parsing/error-parser.ts +0 -28
  139. package/src/parsing/parsers/compiler.ts +0 -93
  140. package/src/parsing/parsers/generic.ts +0 -28
  141. package/src/parsing/parsers/go.ts +0 -97
  142. package/src/parsing/parsers/node.ts +0 -69
  143. package/src/parsing/parsers/python.ts +0 -62
  144. package/src/parsing/parsers/rust.ts +0 -50
  145. package/src/parsing/parsers/shell.ts +0 -42
  146. package/src/parsing/types.ts +0 -47
  147. package/src/research/gap-analyzer.ts +0 -135
  148. package/src/research/insight-generator.ts +0 -123
  149. package/src/research/research-engine.ts +0 -116
  150. package/src/research/synergy-detector.ts +0 -126
  151. package/src/research/template-extractor.ts +0 -130
  152. package/src/research/trend-analyzer.ts +0 -127
  153. package/src/services/analytics.service.ts +0 -226
  154. package/src/services/code.service.ts +0 -271
  155. package/src/services/error.service.ts +0 -266
  156. package/src/services/git.service.ts +0 -132
  157. package/src/services/notification.service.ts +0 -41
  158. package/src/services/prevention.service.ts +0 -159
  159. package/src/services/research.service.ts +0 -98
  160. package/src/services/solution.service.ts +0 -174
  161. package/src/services/synapse.service.ts +0 -59
  162. package/src/services/terminal.service.ts +0 -81
  163. package/src/synapses/activation.ts +0 -80
  164. package/src/synapses/decay.ts +0 -38
  165. package/src/synapses/hebbian.ts +0 -69
  166. package/src/synapses/pathfinder.ts +0 -81
  167. package/src/synapses/synapse-manager.ts +0 -113
  168. package/src/types/code.types.ts +0 -52
  169. package/src/types/config.types.ts +0 -103
  170. package/src/types/error.types.ts +0 -67
  171. package/src/types/ipc.types.ts +0 -8
  172. package/src/types/mcp.types.ts +0 -53
  173. package/src/types/research.types.ts +0 -28
  174. package/src/types/solution.types.ts +0 -30
  175. package/src/types/synapse.types.ts +0 -50
  176. package/src/utils/events.ts +0 -45
  177. package/src/utils/hash.ts +0 -5
  178. package/src/utils/logger.ts +0 -48
  179. package/src/utils/paths.ts +0 -19
  180. package/tests/e2e/test_code_intelligence.py +0 -1015
  181. package/tests/e2e/test_error_memory.py +0 -451
  182. package/tests/e2e/test_full_integration.py +0 -534
  183. package/tests/fixtures/code-modules/modules.ts +0 -83
  184. package/tests/fixtures/errors/go.ts +0 -9
  185. package/tests/fixtures/errors/node.ts +0 -24
  186. package/tests/fixtures/errors/python.ts +0 -21
  187. package/tests/fixtures/errors/rust.ts +0 -25
  188. package/tests/fixtures/errors/shell.ts +0 -15
  189. package/tests/fixtures/solutions/solutions.ts +0 -27
  190. package/tests/helpers/setup-db.ts +0 -52
  191. package/tests/integration/code-flow.test.ts +0 -86
  192. package/tests/integration/error-flow.test.ts +0 -83
  193. package/tests/integration/ipc-flow.test.ts +0 -166
  194. package/tests/integration/learning-cycle.test.ts +0 -82
  195. package/tests/integration/synapse-flow.test.ts +0 -117
  196. package/tests/unit/code/analyzer.test.ts +0 -58
  197. package/tests/unit/code/fingerprint.test.ts +0 -51
  198. package/tests/unit/code/scorer.test.ts +0 -55
  199. package/tests/unit/learning/confidence-scorer.test.ts +0 -60
  200. package/tests/unit/learning/decay.test.ts +0 -45
  201. package/tests/unit/learning/pattern-extractor.test.ts +0 -50
  202. package/tests/unit/matching/error-matcher.test.ts +0 -69
  203. package/tests/unit/matching/fingerprint.test.ts +0 -47
  204. package/tests/unit/matching/similarity.test.ts +0 -65
  205. package/tests/unit/matching/tfidf.test.ts +0 -71
  206. package/tests/unit/matching/tokenizer.test.ts +0 -83
  207. package/tests/unit/parsing/parsers.test.ts +0 -113
  208. package/tests/unit/research/gap-analyzer.test.ts +0 -45
  209. package/tests/unit/research/trend-analyzer.test.ts +0 -45
  210. package/tests/unit/synapses/activation.test.ts +0 -80
  211. package/tests/unit/synapses/decay.test.ts +0 -27
  212. package/tests/unit/synapses/hebbian.test.ts +0 -96
  213. package/tests/unit/synapses/pathfinder.test.ts +0 -72
  214. package/tsconfig.json +0 -18
@@ -1,42 +0,0 @@
1
- import { Command } from 'commander';
2
- import { withIpc } from '../ipc-helper.js';
3
- import { c, icons, header, divider, table } from '../colors.js';
4
-
5
- export function projectsCommand(): Command {
6
- return new Command('projects')
7
- .description('List all imported projects with stats')
8
- .action(async () => {
9
- await withIpc(async (client) => {
10
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
- const projects = await client.request('project.list', {}) as any[];
12
-
13
- console.log(header('Projects', icons.module));
14
-
15
- if (projects.length === 0) {
16
- console.log(`\n ${c.dim('No projects imported yet.')} Use ${c.cyan('brain import <dir>')} to get started.`);
17
- console.log(`\n${divider()}`);
18
- return;
19
- }
20
-
21
- console.log();
22
-
23
- const rows: string[][] = [
24
- [c.dim(' #'), c.dim('Name'), c.dim('Language'), c.dim('Modules'), c.dim('Path')],
25
- ];
26
-
27
- for (const p of projects) {
28
- rows.push([
29
- c.dimmer(` ${p.id}`),
30
- c.value(p.name),
31
- p.language ? c.cyan(p.language) : c.dim('—'),
32
- c.green(String(p.moduleCount)),
33
- p.path ? c.dim(p.path) : c.dim('—'),
34
- ]);
35
- }
36
-
37
- console.log(table(rows, [5, 24, 14, 9, 40]));
38
- console.log(`\n ${c.label('Total:')} ${c.value(String(projects.length))} projects, ${c.green(String(projects.reduce((s: number, p: any) => s + p.moduleCount, 0)))} modules`);
39
- console.log(`\n${divider()}`);
40
- });
41
- });
42
- }
@@ -1,120 +0,0 @@
1
- import { Command } from 'commander';
2
- import { withIpc } from '../ipc-helper.js';
3
- import { c, icons, header, statusBadge, divider } from '../colors.js';
4
-
5
- export function queryCommand(): Command {
6
- return new Command('query')
7
- .description('Search errors, code modules, and insights')
8
- .argument('<search>', 'Search term')
9
- .option('-l, --limit <n>', 'Maximum results per category', '10')
10
- .option('--errors-only', 'Only search errors')
11
- .option('--modules-only', 'Only search code modules')
12
- .option('--insights-only', 'Only search insights')
13
- .option('--page <n>', 'Page number (starting from 1)', '1')
14
- .action(async (search: string, opts) => {
15
- await withIpc(async (client) => {
16
- const limit = parseInt(opts.limit, 10);
17
- const page = parseInt(opts.page, 10) || 1;
18
- const offset = (page - 1) * limit;
19
- const searchAll = !opts.errorsOnly && !opts.modulesOnly && !opts.insightsOnly;
20
- let totalResults = 0;
21
-
22
- // --- Errors ---
23
- if (searchAll || opts.errorsOnly) {
24
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
- const results: any = await client.request('error.query', {
26
- search,
27
- limit: limit + offset,
28
- });
29
-
30
- const errors = Array.isArray(results) ? results.slice(offset, offset + limit) : [];
31
- if (errors.length > 0) {
32
- totalResults += errors.length;
33
- console.log(header(`Errors matching "${search}"`, icons.error));
34
-
35
- for (const err of errors) {
36
- const badge = statusBadge(err.resolved ? 'resolved' : 'open');
37
- const typeTag = c.purple(err.errorType ?? 'unknown');
38
- console.log(` ${c.dim(`#${err.id}`)} ${badge} ${typeTag}`);
39
- console.log(` ${c.dim((err.message ?? '').slice(0, 120))}`);
40
-
41
- // Get solutions for this error
42
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
- const solutions: any = await client.request('solution.query', { error_id: err.id });
44
- if (solutions?.length > 0) {
45
- console.log(` ${c.green(`${icons.check} ${solutions.length} solution(s)`)}`);
46
- for (const sol of solutions.slice(0, 3)) {
47
- console.log(` ${c.dim(icons.corner)} ${c.dim((sol.description ?? '').slice(0, 100))}`);
48
- }
49
- }
50
- console.log();
51
- }
52
- }
53
- }
54
-
55
- // --- Code Modules ---
56
- if (searchAll || opts.modulesOnly) {
57
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
- const modules: any = await client.request('code.find', {
59
- query: search,
60
- limit: limit + offset,
61
- });
62
-
63
- const mods = Array.isArray(modules) ? modules.slice(offset, offset + limit) : [];
64
- if (mods.length > 0) {
65
- totalResults += mods.length;
66
- console.log(header(`Modules matching "${search}"`, icons.module));
67
-
68
- for (const mod of mods) {
69
- const score = mod.reusability_score ?? mod.reusabilityScore ?? 0;
70
- const scoreColor = score >= 0.7 ? c.green : score >= 0.4 ? c.orange : c.red;
71
- console.log(` ${c.dim(`#${mod.id}`)} ${c.cyan(`[${mod.language}]`)} ${c.value(mod.name)}`);
72
- console.log(` ${c.label('File:')} ${c.dim(mod.file_path ?? mod.filePath)} ${c.label('Score:')} ${scoreColor(typeof score === 'number' ? score.toFixed(2) : score)}`);
73
- if (mod.description) {
74
- console.log(` ${c.dim(mod.description.slice(0, 120))}`);
75
- }
76
- console.log();
77
- }
78
- }
79
- }
80
-
81
- // --- Insights ---
82
- if (searchAll || opts.insightsOnly) {
83
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
84
- const insights: any = await client.request('research.insights', {
85
- activeOnly: true,
86
- limit: 100,
87
- });
88
-
89
- // Client-side search since insights API may not support text search
90
- const allInsights = Array.isArray(insights) ? insights : [];
91
- const searchLower = search.toLowerCase();
92
- const matched = allInsights.filter((i: { title?: string; description?: string }) =>
93
- (i.title ?? '').toLowerCase().includes(searchLower) ||
94
- (i.description ?? '').toLowerCase().includes(searchLower)
95
- ).slice(offset, offset + limit);
96
-
97
- if (matched.length > 0) {
98
- totalResults += matched.length;
99
- console.log(header(`Insights matching "${search}"`, icons.insight));
100
-
101
- for (const ins of matched) {
102
- const typeTag = c.cyan(`[${ins.type}]`);
103
- console.log(` ${typeTag} ${c.value(ins.title)}`);
104
- if (ins.description) {
105
- console.log(` ${c.dim(ins.description.slice(0, 150))}`);
106
- }
107
- console.log();
108
- }
109
- }
110
- }
111
-
112
- if (totalResults === 0) {
113
- console.log(`\n${icons.search} ${c.dim(`No results found for "${search}".`)}`);
114
- } else {
115
- console.log(` ${c.dim(`Page ${page} — showing ${totalResults} result(s). Use --page ${page + 1} for more.`)}`);
116
- console.log(divider());
117
- }
118
- });
119
- });
120
- }
@@ -1,105 +0,0 @@
1
- import { Command } from 'commander';
2
- import { spawn, type ChildProcess } from 'node:child_process';
3
- import fs from 'node:fs';
4
- import path from 'node:path';
5
- import { getDataDir } from '../../utils/paths.js';
6
- import { c, icons } from '../colors.js';
7
- import { checkForUpdate } from '../update-check.js';
8
-
9
- const MAX_RESTARTS = 5;
10
- const RESTART_WINDOW_MS = 5 * 60 * 1000; // 5 minutes
11
- const BASE_BACKOFF_MS = 1000;
12
-
13
- function spawnDaemon(entryPoint: string, args: string[]): ChildProcess {
14
- const child = spawn(process.execPath, [entryPoint, ...args], {
15
- detached: true,
16
- stdio: 'ignore',
17
- });
18
- child.unref();
19
- return child;
20
- }
21
-
22
- function startWatchdog(entryPoint: string, args: string[], pidPath: string): void {
23
- const restartTimes: number[] = [];
24
-
25
- function launch(): void {
26
- const child = spawnDaemon(entryPoint, args);
27
- console.log(`${icons.brain} ${c.info('Brain daemon starting')} ${c.dim(`(PID: ${child.pid})`)}`);
28
-
29
- child.on('exit', (code) => {
30
- // Normal shutdown (code 0 or SIGTERM) — don't restart
31
- if (code === 0 || code === null) return;
32
-
33
- const now = Date.now();
34
- restartTimes.push(now);
35
-
36
- // Only count restarts within the window
37
- const recentRestarts = restartTimes.filter((t) => now - t < RESTART_WINDOW_MS);
38
- restartTimes.length = 0;
39
- restartTimes.push(...recentRestarts);
40
-
41
- if (recentRestarts.length > MAX_RESTARTS) {
42
- console.error(`${icons.error} ${c.error(`Brain daemon crashed ${MAX_RESTARTS} times in 5 minutes — giving up.`)}`);
43
- // Clean up stale PID file
44
- try { fs.unlinkSync(pidPath); } catch { /* ignore */ }
45
- return;
46
- }
47
-
48
- const backoff = BASE_BACKOFF_MS * Math.pow(2, recentRestarts.length - 1);
49
- console.log(`${icons.warn} ${c.warn(`Brain daemon exited (code ${code}) — restarting in ${backoff / 1000}s...`)}`);
50
-
51
- setTimeout(launch, backoff);
52
- });
53
- }
54
-
55
- launch();
56
-
57
- // Wait briefly for PID file to appear
58
- setTimeout(async () => {
59
- if (fs.existsSync(pidPath)) {
60
- console.log(`${icons.ok} ${c.success('Brain daemon started successfully.')} ${c.dim('(watchdog active)')}`);
61
- } else {
62
- console.log(`${icons.clock} ${c.warn('Brain daemon may still be starting.')} Check: ${c.cyan('brain status')}`);
63
- }
64
- await checkForUpdate();
65
- }, 1000);
66
- }
67
-
68
- export function startCommand(): Command {
69
- return new Command('start')
70
- .description('Start the Brain daemon')
71
- .option('-f, --foreground', 'Run in foreground (no detach)')
72
- .option('-c, --config <path>', 'Config file path')
73
- .action((opts) => {
74
- const pidPath = path.join(getDataDir(), 'brain.pid');
75
-
76
- // Check if already running
77
- if (fs.existsSync(pidPath)) {
78
- const pid = parseInt(fs.readFileSync(pidPath, 'utf8').trim(), 10);
79
- try {
80
- process.kill(pid, 0); // Check if process exists
81
- console.log(`${icons.brain} Brain daemon is ${c.green('already running')} ${c.dim(`(PID: ${pid})`)}`);
82
- return;
83
- } catch {
84
- // PID file stale, remove it
85
- fs.unlinkSync(pidPath);
86
- }
87
- }
88
-
89
- if (opts.foreground) {
90
- // Run in foreground — import dynamically to avoid loading everything at CLI parse time
91
- import('../../brain.js').then(({ BrainCore }) => {
92
- const core = new BrainCore();
93
- core.start(opts.config);
94
- });
95
- return;
96
- }
97
-
98
- // Spawn detached daemon with watchdog
99
- const args = ['daemon'];
100
- if (opts.config) args.push('-c', opts.config);
101
- const entryPoint = path.resolve(import.meta.dirname, '../../index.js');
102
-
103
- startWatchdog(entryPoint, args, pidPath);
104
- });
105
- }
@@ -1,75 +0,0 @@
1
- import { Command } from 'commander';
2
- import fs from 'node:fs';
3
- import path from 'node:path';
4
- import { getDataDir } from '../../utils/paths.js';
5
- import { withIpc } from '../ipc-helper.js';
6
- import { c, icons, header, keyValue, divider } from '../colors.js';
7
- import { checkForUpdate, getCurrentVersion } from '../update-check.js';
8
-
9
- export function statusCommand(): Command {
10
- return new Command('status')
11
- .description('Show Brain daemon status')
12
- .action(async () => {
13
- const pidPath = path.join(getDataDir(), 'brain.pid');
14
-
15
- if (!fs.existsSync(pidPath)) {
16
- console.log(`${icons.brain} Brain Daemon: ${c.red.bold('NOT RUNNING')}`);
17
- return;
18
- }
19
-
20
- const pid = parseInt(fs.readFileSync(pidPath, 'utf8').trim(), 10);
21
- let running = false;
22
- try {
23
- process.kill(pid, 0);
24
- running = true;
25
- } catch { /* not running */ }
26
-
27
- if (!running) {
28
- console.log(`${icons.brain} Brain Daemon: ${c.red.bold('NOT RUNNING')} ${c.dim('(stale PID file)')}`);
29
- return;
30
- }
31
-
32
- console.log(header(`Brain Status v${getCurrentVersion()}`, icons.brain));
33
- console.log(` ${c.green(`${icons.dot} RUNNING`)} ${c.dim(`(PID ${pid})`)}`);
34
-
35
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
- await withIpc(async (client) => {
37
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
- const summary: any = await client.request('analytics.summary', {});
39
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
- const network: any = await client.request('synapse.stats', {});
41
-
42
- const dbPath = path.join(getDataDir(), 'brain.db');
43
- let dbSize = '?';
44
- try {
45
- const stat = fs.statSync(dbPath);
46
- dbSize = `${(stat.size / 1024 / 1024).toFixed(1)} MB`;
47
- } catch { /* ignore */ }
48
-
49
- console.log(keyValue('Database', `${dbPath} (${dbSize})`));
50
- console.log();
51
-
52
- console.log(` ${icons.error} ${c.purple.bold('Error Brain')}`);
53
- console.log(` ${c.label('Errors:')} ${c.value(summary.errors?.total ?? 0)} total, ${c.red(summary.errors?.unresolved ?? 0)} unresolved, ${c.dim(`${summary.errors?.last7d ?? 0} last 7d`)}`);
54
- console.log(` ${c.label('Solutions:')} ${c.value(summary.solutions?.total ?? 0)}`);
55
- console.log(` ${c.label('Rules:')} ${c.green(summary.rules?.active ?? 0)} active`);
56
- console.log(` ${c.label('Anti-Pat.:')} ${c.value(summary.antipatterns?.total ?? 0)}`);
57
- console.log();
58
-
59
- console.log(` ${icons.module} ${c.blue.bold('Code Brain')}`);
60
- console.log(` ${c.label('Modules:')} ${c.value(summary.modules?.total ?? 0)} registered`);
61
- console.log();
62
-
63
- console.log(` ${icons.synapse} ${c.cyan.bold('Synapse Network')}`);
64
- console.log(` ${c.label('Synapses:')} ${c.value(network.totalSynapses ?? 0)}`);
65
- console.log(` ${c.label('Avg weight:')} ${c.value((network.avgWeight ?? 0).toFixed(2))}`);
66
- console.log();
67
-
68
- console.log(` ${icons.insight} ${c.orange.bold('Research Brain')}`);
69
- console.log(` ${c.label('Insights:')} ${c.value(summary.insights?.active ?? 0)} active`);
70
-
71
- await checkForUpdate();
72
- console.log(`\n${divider()}`);
73
- });
74
- });
75
- }
@@ -1,34 +0,0 @@
1
- import { Command } from 'commander';
2
- import fs from 'node:fs';
3
- import path from 'node:path';
4
- import { getDataDir } from '../../utils/paths.js';
5
- import { c, icons } from '../colors.js';
6
-
7
- export function stopCommand(): Command {
8
- return new Command('stop')
9
- .description('Stop the Brain daemon')
10
- .action(() => {
11
- const pidPath = path.join(getDataDir(), 'brain.pid');
12
-
13
- if (!fs.existsSync(pidPath)) {
14
- console.log(`${icons.brain} ${c.dim('Brain daemon is not running (no PID file found).')}`);
15
- return;
16
- }
17
-
18
- const pid = parseInt(fs.readFileSync(pidPath, 'utf8').trim(), 10);
19
-
20
- try {
21
- process.kill(pid, 'SIGTERM');
22
- console.log(`${icons.brain} ${c.success('Brain daemon stopped')} ${c.dim(`(PID: ${pid})`)}`);
23
- } catch (err) {
24
- if ((err as NodeJS.ErrnoException).code === 'ESRCH') {
25
- console.log(`${icons.brain} ${c.dim('Brain daemon was not running (stale PID file removed).')}`);
26
- } else {
27
- console.error(`${icons.error} ${c.error(`Failed to stop daemon: ${err}`)}`);
28
- }
29
- }
30
-
31
- // Clean up PID file
32
- try { fs.unlinkSync(pidPath); } catch { /* ignore */ }
33
- });
34
- }
@@ -1,22 +0,0 @@
1
- import { IpcClient } from '../ipc/client.js';
2
- import { getPipeName } from '../utils/paths.js';
3
- import { c, icons } from './colors.js';
4
-
5
- export async function withIpc<T>(fn: (client: IpcClient) => Promise<T>): Promise<T> {
6
- const client = new IpcClient(getPipeName(), 5000);
7
- try {
8
- await client.connect();
9
- return await fn(client);
10
- } catch (err) {
11
- if (err instanceof Error && err.message.includes('ENOENT')) {
12
- console.error(`${icons.error} ${c.error('Brain daemon is not running.')} Start it with: ${c.cyan('brain start')}`);
13
- } else if (err instanceof Error && err.message.includes('ECONNREFUSED')) {
14
- console.error(`${icons.error} ${c.error('Brain daemon is not responding.')} Try: ${c.cyan('brain stop && brain start')}`);
15
- } else {
16
- console.error(`${icons.error} ${c.error(err instanceof Error ? err.message : String(err))}`);
17
- }
18
- process.exit(1);
19
- } finally {
20
- client.disconnect();
21
- }
22
- }
@@ -1,63 +0,0 @@
1
- import https from 'node:https';
2
- import { c, icons } from './colors.js';
3
-
4
- // Read current version from package.json at build time
5
- import { createRequire } from 'node:module';
6
- const require = createRequire(import.meta.url);
7
- const pkg = require('../../package.json');
8
- const CURRENT_VERSION: string = pkg.version;
9
-
10
- export function getCurrentVersion(): string {
11
- return CURRENT_VERSION;
12
- }
13
-
14
- function fetchLatestVersion(): Promise<string | null> {
15
- return new Promise((resolve) => {
16
- const timeout = setTimeout(() => resolve(null), 3000);
17
-
18
- const req = https.get(
19
- 'https://registry.npmjs.org/@timmeck/brain/latest',
20
- { headers: { Accept: 'application/json' } },
21
- (res) => {
22
- let data = '';
23
- res.on('data', (chunk) => { data += chunk; });
24
- res.on('end', () => {
25
- clearTimeout(timeout);
26
- try {
27
- const json = JSON.parse(data);
28
- resolve(json.version ?? null);
29
- } catch {
30
- resolve(null);
31
- }
32
- });
33
- },
34
- );
35
- req.on('error', () => {
36
- clearTimeout(timeout);
37
- resolve(null);
38
- });
39
- });
40
- }
41
-
42
- function isNewer(latest: string, current: string): boolean {
43
- const l = latest.split('.').map(Number);
44
- const c = current.split('.').map(Number);
45
- for (let i = 0; i < 3; i++) {
46
- if ((l[i] ?? 0) > (c[i] ?? 0)) return true;
47
- if ((l[i] ?? 0) < (c[i] ?? 0)) return false;
48
- }
49
- return false;
50
- }
51
-
52
- export async function checkForUpdate(): Promise<void> {
53
- try {
54
- const latest = await fetchLatestVersion();
55
- if (latest && isNewer(latest, CURRENT_VERSION)) {
56
- console.log();
57
- console.log(` ${icons.star} ${c.orange.bold(`Update available: v${CURRENT_VERSION} → v${latest}`)}`);
58
- console.log(` Run: ${c.cyan('npm update -g @timmeck/brain')}`);
59
- }
60
- } catch {
61
- // silently ignore — update check is best-effort
62
- }
63
- }
@@ -1,117 +0,0 @@
1
- import type { ExportInfo } from '../types/code.types.js';
2
- import * as tsParser from './parsers/typescript.js';
3
- import * as pyParser from './parsers/python.js';
4
- import * as genericParser from './parsers/generic.js';
5
-
6
- export interface AnalysisResult {
7
- exports: ExportInfo[];
8
- externalDeps: string[];
9
- internalDeps: string[];
10
- isPure: boolean;
11
- hasTypeAnnotations: boolean;
12
- linesOfCode: number;
13
- complexity: number;
14
- }
15
-
16
- const SIDE_EFFECT_PATTERNS = [
17
- 'fs.', 'process.exit', 'process.env', 'console.', 'fetch(',
18
- 'XMLHttpRequest', 'document.', 'window.',
19
- 'global.', 'require(',
20
- ];
21
-
22
- function getParser(language: string) {
23
- switch (language) {
24
- case 'typescript':
25
- case 'javascript':
26
- return tsParser;
27
- case 'python':
28
- return pyParser;
29
- default:
30
- return genericParser;
31
- }
32
- }
33
-
34
- export function analyzeCode(source: string, language: string): AnalysisResult {
35
- const parser = getParser(language);
36
- const exports = parser.extractExports(source);
37
- const { external, internal } = parser.extractImports(source);
38
- const isPure = checkPurity(source);
39
- const typed = parser.hasTypeAnnotations(source);
40
- const linesOfCode = source.split('\n').filter(l => l.trim().length > 0).length;
41
- const complexity = computeCyclomaticComplexity(source, language);
42
-
43
- return {
44
- exports,
45
- externalDeps: external,
46
- internalDeps: internal,
47
- isPure,
48
- hasTypeAnnotations: typed,
49
- linesOfCode,
50
- complexity,
51
- };
52
- }
53
-
54
- /**
55
- * Computes cyclomatic complexity: counts decision points in the code.
56
- * CC = 1 + number of decision points (if, else if, for, while, case, catch, &&, ||, ?:)
57
- */
58
- export function computeCyclomaticComplexity(source: string, language: string): number {
59
- // Remove comments and strings to avoid false positives
60
- const cleaned = source
61
- .replace(/\/\/.*$/gm, '') // single-line comments
62
- .replace(/\/\*[\s\S]*?\*\//g, '') // multi-line comments
63
- .replace(/#.*$/gm, '') // Python comments
64
- .replace(/(["'`])(?:(?!\1|\\).|\\.)*\1/g, '""'); // strings
65
-
66
- let complexity = 1; // Base complexity
67
-
68
- // Language-agnostic decision point patterns
69
- const patterns = [
70
- /\bif\b/g,
71
- /\belse\s+if\b/g,
72
- /\belif\b/g,
73
- /\bfor\b/g,
74
- /\bwhile\b/g,
75
- /\bcase\b/g,
76
- /\bcatch\b/g,
77
- /\bexcept\b/g,
78
- /&&/g,
79
- /\|\|/g,
80
- /\?\s*[^:]/g, // ternary operator (not type annotations)
81
- ];
82
-
83
- for (const pattern of patterns) {
84
- const matches = cleaned.match(pattern);
85
- if (matches) complexity += matches.length;
86
- }
87
-
88
- return complexity;
89
- }
90
-
91
- export function checkPurity(source: string): boolean {
92
- return !SIDE_EFFECT_PATTERNS.some(p => source.includes(p));
93
- }
94
-
95
- export function measureCohesion(exports: ExportInfo[]): number {
96
- if (exports.length <= 1) return 1.0;
97
-
98
- const names = exports.map(e =>
99
- e.name
100
- .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
101
- .replace(/([a-z\d])([A-Z])/g, '$1 $2')
102
- .toLowerCase()
103
- .split(/\s+/)
104
- );
105
-
106
- const vocab = new Set<string>();
107
- names.forEach(tokens => tokens.forEach(t => vocab.add(t)));
108
-
109
- let sharedTokens = 0;
110
- for (const token of vocab) {
111
- const count = names.filter(n => n.includes(token)).length;
112
- if (count > 1) sharedTokens += count;
113
- }
114
-
115
- const maxPossible = names.length * vocab.size;
116
- return maxPossible === 0 ? 0 : sharedTokens / maxPossible;
117
- }
@@ -1,87 +0,0 @@
1
- import { sha256 } from '../utils/hash.js';
2
-
3
- export function fingerprintCode(source: string, language: string): string {
4
- let normalized = stripComments(source, language);
5
- normalized = normalized.replace(/\s+/g, ' ').trim();
6
- normalized = normalizeIdentifiers(normalized, language);
7
- normalized = normalized.replace(/'[^']*'/g, "'<STR>'");
8
- normalized = normalized.replace(/"[^"]*"/g, '"<STR>"');
9
- normalized = normalized.replace(/`[^`]*`/g, '`<STR>`');
10
- normalized = normalized.replace(/\b\d+\b/g, '<NUM>');
11
- return sha256(normalized);
12
- }
13
-
14
- export function stripComments(source: string, language: string): string {
15
- switch (language) {
16
- case 'typescript':
17
- case 'javascript':
18
- case 'java':
19
- case 'go':
20
- case 'rust':
21
- case 'c':
22
- case 'cpp':
23
- return source
24
- .replace(/\/\/.*$/gm, '')
25
- .replace(/\/\*[\s\S]*?\*\//g, '');
26
- case 'python':
27
- return source
28
- .replace(/#.*$/gm, '')
29
- .replace(/"""[\s\S]*?"""/g, '')
30
- .replace(/'''[\s\S]*?'''/g, '');
31
- default:
32
- return source
33
- .replace(/\/\/.*$/gm, '')
34
- .replace(/#.*$/gm, '');
35
- }
36
- }
37
-
38
- function normalizeIdentifiers(source: string, language: string): string {
39
- const importNames = extractImportNames(source, language);
40
- const keywords = getLanguageKeywords(language);
41
- const preserve = new Set([...importNames, ...keywords]);
42
-
43
- return source.replace(/\b[a-zA-Z_]\w*\b/g, (match) => {
44
- if (preserve.has(match)) return match;
45
- if (match[0] === match[0]!.toUpperCase() && match[0] !== match[0]!.toLowerCase()) return '<CLASS>';
46
- return '<VAR>';
47
- });
48
- }
49
-
50
- function extractImportNames(source: string, language: string): string[] {
51
- const names: string[] = [];
52
-
53
- if (language === 'typescript' || language === 'javascript') {
54
- const re = /import\s+(?:\{([^}]+)\}|\*\s+as\s+(\w+)|(\w+))/g;
55
- let m: RegExpExecArray | null;
56
- while ((m = re.exec(source)) !== null) {
57
- if (m[1]) names.push(...m[1].split(',').map(s => s.trim().split(/\s+as\s+/).pop()!));
58
- if (m[2]) names.push(m[2]);
59
- if (m[3]) names.push(m[3]);
60
- }
61
- } else if (language === 'python') {
62
- const re = /(?:from\s+\S+\s+)?import\s+(.+)/g;
63
- let m: RegExpExecArray | null;
64
- while ((m = re.exec(source)) !== null) {
65
- names.push(...m[1]!.split(',').map(s => {
66
- const parts = s.trim().split(/\s+as\s+/);
67
- return parts[parts.length - 1]!;
68
- }));
69
- }
70
- }
71
-
72
- return names.filter(Boolean);
73
- }
74
-
75
- function getLanguageKeywords(language: string): string[] {
76
- const common = ['if', 'else', 'for', 'while', 'return', 'break', 'continue', 'switch', 'case', 'default', 'try', 'catch', 'throw', 'new', 'delete', 'true', 'false', 'null', 'undefined', 'void'];
77
-
78
- const langKeywords: Record<string, string[]> = {
79
- typescript: [...common, 'const', 'let', 'var', 'function', 'class', 'interface', 'type', 'enum', 'import', 'export', 'from', 'async', 'await', 'extends', 'implements', 'readonly', 'private', 'public', 'protected', 'static', 'abstract', 'as', 'is', 'in', 'of', 'typeof', 'keyof', 'infer', 'never', 'unknown', 'any', 'string', 'number', 'boolean', 'symbol', 'object'],
80
- javascript: [...common, 'const', 'let', 'var', 'function', 'class', 'import', 'export', 'from', 'async', 'await', 'extends', 'typeof', 'instanceof', 'in', 'of', 'this', 'super', 'yield'],
81
- python: ['def', 'class', 'import', 'from', 'if', 'elif', 'else', 'for', 'while', 'return', 'yield', 'break', 'continue', 'try', 'except', 'finally', 'raise', 'with', 'as', 'pass', 'lambda', 'True', 'False', 'None', 'and', 'or', 'not', 'in', 'is', 'global', 'nonlocal', 'assert', 'async', 'await', 'self'],
82
- rust: [...common, 'fn', 'let', 'mut', 'pub', 'struct', 'enum', 'impl', 'trait', 'use', 'mod', 'crate', 'self', 'super', 'match', 'loop', 'move', 'ref', 'where', 'async', 'await', 'dyn', 'Box', 'Vec', 'String', 'Option', 'Result', 'Some', 'None', 'Ok', 'Err'],
83
- go: [...common, 'func', 'package', 'import', 'type', 'struct', 'interface', 'map', 'chan', 'go', 'defer', 'select', 'range', 'var', 'const', 'nil', 'make', 'len', 'append', 'cap', 'copy', 'close'],
84
- };
85
-
86
- return langKeywords[language] ?? common;
87
- }