neuronlayer 0.1.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 (78) hide show
  1. package/CONTRIBUTING.md +127 -0
  2. package/LICENSE +21 -0
  3. package/README.md +305 -0
  4. package/dist/index.js +38016 -0
  5. package/esbuild.config.js +26 -0
  6. package/package.json +63 -0
  7. package/src/cli/commands.ts +382 -0
  8. package/src/core/adr-exporter.ts +253 -0
  9. package/src/core/architecture/architecture-enforcement.ts +228 -0
  10. package/src/core/architecture/duplicate-detector.ts +288 -0
  11. package/src/core/architecture/index.ts +6 -0
  12. package/src/core/architecture/pattern-learner.ts +306 -0
  13. package/src/core/architecture/pattern-library.ts +403 -0
  14. package/src/core/architecture/pattern-validator.ts +324 -0
  15. package/src/core/change-intelligence/bug-correlator.ts +444 -0
  16. package/src/core/change-intelligence/change-intelligence.ts +221 -0
  17. package/src/core/change-intelligence/change-tracker.ts +334 -0
  18. package/src/core/change-intelligence/fix-suggester.ts +340 -0
  19. package/src/core/change-intelligence/index.ts +5 -0
  20. package/src/core/code-verifier.ts +843 -0
  21. package/src/core/confidence/confidence-scorer.ts +251 -0
  22. package/src/core/confidence/conflict-checker.ts +289 -0
  23. package/src/core/confidence/index.ts +5 -0
  24. package/src/core/confidence/source-tracker.ts +263 -0
  25. package/src/core/confidence/warning-detector.ts +241 -0
  26. package/src/core/context-rot/compaction.ts +284 -0
  27. package/src/core/context-rot/context-health.ts +243 -0
  28. package/src/core/context-rot/context-rot-prevention.ts +213 -0
  29. package/src/core/context-rot/critical-context.ts +221 -0
  30. package/src/core/context-rot/drift-detector.ts +255 -0
  31. package/src/core/context-rot/index.ts +7 -0
  32. package/src/core/context.ts +263 -0
  33. package/src/core/decision-extractor.ts +339 -0
  34. package/src/core/decisions.ts +69 -0
  35. package/src/core/deja-vu.ts +421 -0
  36. package/src/core/engine.ts +1455 -0
  37. package/src/core/feature-context.ts +726 -0
  38. package/src/core/ghost-mode.ts +412 -0
  39. package/src/core/learning.ts +485 -0
  40. package/src/core/living-docs/activity-tracker.ts +296 -0
  41. package/src/core/living-docs/architecture-generator.ts +428 -0
  42. package/src/core/living-docs/changelog-generator.ts +348 -0
  43. package/src/core/living-docs/component-generator.ts +230 -0
  44. package/src/core/living-docs/doc-engine.ts +110 -0
  45. package/src/core/living-docs/doc-validator.ts +282 -0
  46. package/src/core/living-docs/index.ts +8 -0
  47. package/src/core/project-manager.ts +297 -0
  48. package/src/core/summarizer.ts +267 -0
  49. package/src/core/test-awareness/change-validator.ts +499 -0
  50. package/src/core/test-awareness/index.ts +5 -0
  51. package/src/index.ts +49 -0
  52. package/src/indexing/ast.ts +563 -0
  53. package/src/indexing/embeddings.ts +85 -0
  54. package/src/indexing/indexer.ts +245 -0
  55. package/src/indexing/watcher.ts +78 -0
  56. package/src/server/gateways/aggregator.ts +374 -0
  57. package/src/server/gateways/index.ts +473 -0
  58. package/src/server/gateways/memory-ghost.ts +343 -0
  59. package/src/server/gateways/memory-query.ts +452 -0
  60. package/src/server/gateways/memory-record.ts +346 -0
  61. package/src/server/gateways/memory-review.ts +410 -0
  62. package/src/server/gateways/memory-status.ts +517 -0
  63. package/src/server/gateways/memory-verify.ts +392 -0
  64. package/src/server/gateways/router.ts +434 -0
  65. package/src/server/gateways/types.ts +610 -0
  66. package/src/server/mcp.ts +154 -0
  67. package/src/server/resources.ts +85 -0
  68. package/src/server/tools.ts +2261 -0
  69. package/src/storage/database.ts +262 -0
  70. package/src/storage/tier1.ts +135 -0
  71. package/src/storage/tier2.ts +764 -0
  72. package/src/storage/tier3.ts +123 -0
  73. package/src/types/documentation.ts +619 -0
  74. package/src/types/index.ts +222 -0
  75. package/src/utils/config.ts +193 -0
  76. package/src/utils/files.ts +117 -0
  77. package/src/utils/time.ts +37 -0
  78. package/src/utils/tokens.ts +52 -0
@@ -0,0 +1,343 @@
1
+ /**
2
+ * Memory Ghost Gateway
3
+ *
4
+ * The "Super Intelligent Brain" - provides proactive intelligence about current work.
5
+ *
6
+ * Modes:
7
+ * - full: Complete ghost data (ghost insight + déjà vu + resurrection)
8
+ * - conflicts: Check for conflicts with past decisions
9
+ * - dejavu: Find similar past problems
10
+ * - resurrect: Get context from last session
11
+ */
12
+
13
+ import type { MemoryLayerEngine, GhostInsight, ConflictWarning, DejaVuMatch, ResurrectedContext } from '../../core/engine.js';
14
+
15
+ export type MemoryGhostMode = 'full' | 'conflicts' | 'dejavu' | 'resurrect';
16
+
17
+ export interface MemoryGhostInput {
18
+ /** What to check (default: full) */
19
+ mode?: MemoryGhostMode;
20
+ /** Code to check for conflicts/déjà vu */
21
+ code?: string;
22
+ /** Current file being worked on */
23
+ file?: string;
24
+ /** Query for déjà vu search */
25
+ query?: string;
26
+ /** Feature name for resurrection */
27
+ feature_name?: string;
28
+ /** Maximum results for déjà vu */
29
+ max_results?: number;
30
+ }
31
+
32
+ export interface MemoryGhostResponse {
33
+ /** Mode that was executed */
34
+ mode: MemoryGhostMode;
35
+ /** Sources used to generate response */
36
+ sources_used: string[];
37
+ /** Ghost insight - current work awareness */
38
+ ghost?: {
39
+ active_files: string[];
40
+ recent_decisions: Array<{
41
+ id: string;
42
+ title: string;
43
+ description: string;
44
+ }>;
45
+ suggestions: string[];
46
+ };
47
+ /** Conflict warnings */
48
+ conflicts?: {
49
+ has_conflicts: boolean;
50
+ warnings: Array<{
51
+ decision_id: string;
52
+ decision_title: string;
53
+ warning: string;
54
+ severity: 'low' | 'medium' | 'high';
55
+ matched_terms: string[];
56
+ }>;
57
+ };
58
+ /** Déjà vu matches - similar past problems */
59
+ deja_vu?: {
60
+ has_matches: boolean;
61
+ matches: Array<{
62
+ type: 'query' | 'solution' | 'fix' | 'pattern';
63
+ similarity: number;
64
+ when: string;
65
+ file: string;
66
+ snippet: string;
67
+ message: string;
68
+ context?: string;
69
+ }>;
70
+ };
71
+ /** Context resurrection - session continuity */
72
+ resurrection?: {
73
+ active_files: string[];
74
+ last_queries: string[];
75
+ last_edited_file: string | null;
76
+ last_edit_time: string | null;
77
+ possible_blocker: string | null;
78
+ suggested_actions: string[];
79
+ summary: string;
80
+ time_since_last_active: string;
81
+ };
82
+ /** Resurrectable contexts */
83
+ resurrectable_contexts?: Array<{
84
+ id: string;
85
+ name: string;
86
+ last_active: string;
87
+ summary: string;
88
+ }>;
89
+ /** Stats */
90
+ stats?: {
91
+ deja_vu: {
92
+ total_queries: number;
93
+ useful_queries: number;
94
+ avg_usefulness: number;
95
+ };
96
+ };
97
+ }
98
+
99
+ /**
100
+ * Handle a memory_ghost gateway call
101
+ */
102
+ export async function handleMemoryGhost(
103
+ engine: MemoryLayerEngine,
104
+ input: MemoryGhostInput
105
+ ): Promise<MemoryGhostResponse> {
106
+ const mode = input.mode || 'full';
107
+ const sourcesUsed: string[] = [];
108
+
109
+ switch (mode) {
110
+ case 'conflicts':
111
+ return handleConflictsMode(engine, input, sourcesUsed);
112
+
113
+ case 'dejavu':
114
+ return handleDejaVuMode(engine, input, sourcesUsed);
115
+
116
+ case 'resurrect':
117
+ return handleResurrectMode(engine, input, sourcesUsed);
118
+
119
+ case 'full':
120
+ default:
121
+ return handleFullMode(engine, input, sourcesUsed);
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Full ghost mode - everything
127
+ */
128
+ async function handleFullMode(
129
+ engine: MemoryLayerEngine,
130
+ input: MemoryGhostInput,
131
+ sourcesUsed: string[]
132
+ ): Promise<MemoryGhostResponse> {
133
+ sourcesUsed.push('ghost_insight', 'deja_vu', 'resurrect_context');
134
+
135
+ // Notify file access if provided
136
+ if (input.file) {
137
+ await engine.notifyFileAccess(input.file);
138
+ }
139
+
140
+ // Get full ghost data
141
+ const fullData = await engine.getFullGhostData('full', {
142
+ code: input.code,
143
+ file: input.file,
144
+ query: input.query,
145
+ });
146
+
147
+ const response: MemoryGhostResponse = {
148
+ mode: 'full',
149
+ sources_used: sourcesUsed,
150
+ };
151
+
152
+ // Add ghost insight
153
+ if (fullData.ghost) {
154
+ response.ghost = formatGhostInsight(fullData.ghost);
155
+ }
156
+
157
+ // Add conflicts
158
+ if (fullData.conflicts && fullData.conflicts.length > 0) {
159
+ response.conflicts = formatConflicts(fullData.conflicts);
160
+ }
161
+
162
+ // Add déjà vu
163
+ if (fullData.dejaVu && fullData.dejaVu.length > 0) {
164
+ response.deja_vu = formatDejaVu(fullData.dejaVu);
165
+ }
166
+
167
+ // Add resurrection
168
+ if (fullData.resurrection) {
169
+ response.resurrection = formatResurrection(fullData.resurrection);
170
+ }
171
+
172
+ // Add resurrectable contexts
173
+ const contexts = engine.getResurrectableContexts();
174
+ if (contexts.length > 0) {
175
+ response.resurrectable_contexts = contexts.map(c => ({
176
+ id: c.id,
177
+ name: c.name,
178
+ last_active: c.lastActive.toISOString(),
179
+ summary: c.summary,
180
+ }));
181
+ }
182
+
183
+ // Add stats
184
+ response.stats = {
185
+ deja_vu: engine.getDejaVuStats(),
186
+ };
187
+
188
+ return response;
189
+ }
190
+
191
+ /**
192
+ * Conflicts mode - check for decision conflicts
193
+ */
194
+ async function handleConflictsMode(
195
+ engine: MemoryLayerEngine,
196
+ input: MemoryGhostInput,
197
+ sourcesUsed: string[]
198
+ ): Promise<MemoryGhostResponse> {
199
+ sourcesUsed.push('check_ghost_conflicts');
200
+
201
+ if (!input.code) {
202
+ return {
203
+ mode: 'conflicts',
204
+ sources_used: sourcesUsed,
205
+ conflicts: {
206
+ has_conflicts: false,
207
+ warnings: [],
208
+ },
209
+ };
210
+ }
211
+
212
+ // Notify file access if provided
213
+ if (input.file) {
214
+ await engine.notifyFileAccess(input.file);
215
+ }
216
+
217
+ const warnings = engine.checkGhostConflicts(input.code, input.file);
218
+
219
+ return {
220
+ mode: 'conflicts',
221
+ sources_used: sourcesUsed,
222
+ conflicts: formatConflicts(warnings),
223
+ };
224
+ }
225
+
226
+ /**
227
+ * Déjà vu mode - find similar past problems
228
+ */
229
+ async function handleDejaVuMode(
230
+ engine: MemoryLayerEngine,
231
+ input: MemoryGhostInput,
232
+ sourcesUsed: string[]
233
+ ): Promise<MemoryGhostResponse> {
234
+ sourcesUsed.push('find_deja_vu');
235
+
236
+ const searchText = input.query || input.code || '';
237
+
238
+ if (!searchText) {
239
+ return {
240
+ mode: 'dejavu',
241
+ sources_used: sourcesUsed,
242
+ deja_vu: {
243
+ has_matches: false,
244
+ matches: [],
245
+ },
246
+ };
247
+ }
248
+
249
+ const matches = await engine.findDejaVu(searchText, input.max_results || 5);
250
+
251
+ return {
252
+ mode: 'dejavu',
253
+ sources_used: sourcesUsed,
254
+ deja_vu: formatDejaVu(matches),
255
+ stats: {
256
+ deja_vu: engine.getDejaVuStats(),
257
+ },
258
+ };
259
+ }
260
+
261
+ /**
262
+ * Resurrect mode - get context from last session
263
+ */
264
+ async function handleResurrectMode(
265
+ engine: MemoryLayerEngine,
266
+ input: MemoryGhostInput,
267
+ sourcesUsed: string[]
268
+ ): Promise<MemoryGhostResponse> {
269
+ sourcesUsed.push('resurrect_context');
270
+
271
+ const resurrection = engine.resurrectContext({
272
+ featureName: input.feature_name,
273
+ });
274
+
275
+ const contexts = engine.getResurrectableContexts();
276
+
277
+ return {
278
+ mode: 'resurrect',
279
+ sources_used: sourcesUsed,
280
+ resurrection: formatResurrection(resurrection),
281
+ resurrectable_contexts: contexts.map(c => ({
282
+ id: c.id,
283
+ name: c.name,
284
+ last_active: c.lastActive.toISOString(),
285
+ summary: c.summary,
286
+ })),
287
+ };
288
+ }
289
+
290
+ // ========== Formatters ==========
291
+
292
+ function formatGhostInsight(insight: GhostInsight): MemoryGhostResponse['ghost'] {
293
+ return {
294
+ active_files: insight.activeFiles,
295
+ recent_decisions: insight.recentDecisions.map(d => ({
296
+ id: d.id,
297
+ title: d.title,
298
+ description: d.description.slice(0, 200),
299
+ })),
300
+ suggestions: insight.suggestions,
301
+ };
302
+ }
303
+
304
+ function formatConflicts(warnings: ConflictWarning[]): NonNullable<MemoryGhostResponse['conflicts']> {
305
+ return {
306
+ has_conflicts: warnings.length > 0,
307
+ warnings: warnings.map(w => ({
308
+ decision_id: w.decision.id,
309
+ decision_title: w.decision.title,
310
+ warning: w.warning,
311
+ severity: w.severity,
312
+ matched_terms: w.matchedTerms,
313
+ })),
314
+ };
315
+ }
316
+
317
+ function formatDejaVu(matches: DejaVuMatch[]): NonNullable<MemoryGhostResponse['deja_vu']> {
318
+ return {
319
+ has_matches: matches.length > 0,
320
+ matches: matches.map(m => ({
321
+ type: m.type,
322
+ similarity: m.similarity,
323
+ when: m.when.toISOString(),
324
+ file: m.file,
325
+ snippet: m.snippet,
326
+ message: m.message,
327
+ context: m.context,
328
+ })),
329
+ };
330
+ }
331
+
332
+ function formatResurrection(resurrection: ResurrectedContext): NonNullable<MemoryGhostResponse['resurrection']> {
333
+ return {
334
+ active_files: resurrection.activeFiles,
335
+ last_queries: resurrection.lastQueries,
336
+ last_edited_file: resurrection.lastEditedFile,
337
+ last_edit_time: resurrection.lastEditTime?.toISOString() || null,
338
+ possible_blocker: resurrection.possibleBlocker,
339
+ suggested_actions: resurrection.suggestedActions,
340
+ summary: resurrection.summary,
341
+ time_since_last_active: resurrection.timeSinceLastActive,
342
+ };
343
+ }