moflo 4.8.32 → 4.8.34

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 (40) hide show
  1. package/bin/generate-code-map.mjs +955 -955
  2. package/bin/index-guidance.mjs +905 -905
  3. package/bin/index-tests.mjs +728 -728
  4. package/bin/setup-project.mjs +252 -252
  5. package/package.json +10 -5
  6. package/src/@claude-flow/cli/dist/src/commands/doctor.js +1339 -1107
  7. package/src/@claude-flow/cli/dist/src/index.js +2 -18
  8. package/src/@claude-flow/cli/dist/src/mcp-tools/hooks-tools.js +17 -0
  9. package/src/@claude-flow/cli/dist/src/memory/memory-initializer.js +4 -7
  10. package/src/@claude-flow/cli/dist/src/version.js +6 -0
  11. package/src/@claude-flow/cli/package.json +1 -1
  12. package/src/@claude-flow/neural/README.md +260 -0
  13. package/src/@claude-flow/neural/dist/algorithms/a2c.js +361 -0
  14. package/src/@claude-flow/neural/dist/algorithms/curiosity.js +392 -0
  15. package/src/@claude-flow/neural/dist/algorithms/decision-transformer.js +415 -0
  16. package/src/@claude-flow/neural/dist/algorithms/dqn.js +303 -0
  17. package/src/@claude-flow/neural/dist/algorithms/index.js +74 -0
  18. package/src/@claude-flow/neural/dist/algorithms/ppo.js +331 -0
  19. package/src/@claude-flow/neural/dist/algorithms/q-learning.js +259 -0
  20. package/src/@claude-flow/neural/dist/algorithms/sarsa.js +297 -0
  21. package/src/@claude-flow/neural/dist/application/index.js +7 -0
  22. package/src/@claude-flow/neural/dist/application/services/neural-application-service.js +161 -0
  23. package/src/@claude-flow/neural/dist/domain/entities/pattern.js +134 -0
  24. package/src/@claude-flow/neural/dist/domain/index.js +8 -0
  25. package/src/@claude-flow/neural/dist/domain/services/learning-service.js +195 -0
  26. package/src/@claude-flow/neural/dist/index.js +201 -0
  27. package/src/@claude-flow/neural/dist/modes/balanced.js +234 -0
  28. package/src/@claude-flow/neural/dist/modes/base.js +77 -0
  29. package/src/@claude-flow/neural/dist/modes/batch.js +316 -0
  30. package/src/@claude-flow/neural/dist/modes/edge.js +310 -0
  31. package/src/@claude-flow/neural/dist/modes/index.js +13 -0
  32. package/src/@claude-flow/neural/dist/modes/real-time.js +196 -0
  33. package/src/@claude-flow/neural/dist/modes/research.js +389 -0
  34. package/src/@claude-flow/neural/dist/pattern-learner.js +603 -0
  35. package/src/@claude-flow/neural/dist/reasoning-bank.js +993 -0
  36. package/src/@claude-flow/neural/dist/reasoningbank-adapter.js +463 -0
  37. package/src/@claude-flow/neural/dist/sona-integration.js +326 -0
  38. package/src/@claude-flow/neural/dist/sona-manager.js +695 -0
  39. package/src/@claude-flow/neural/dist/types.js +11 -0
  40. package/src/@claude-flow/neural/package.json +26 -0
@@ -0,0 +1,326 @@
1
+ /**
2
+ * SONA Integration for V3 Neural Module
3
+ *
4
+ * Wraps @ruvector/sona package for V3 usage with:
5
+ * - Trajectory tracking and verdict judgment
6
+ * - Pattern extraction and memory distillation
7
+ * - Sub-0.05ms learning performance target
8
+ * - Clean TypeScript API with proper types
9
+ *
10
+ * @module sona-integration
11
+ */
12
+ import { SonaEngine } from '@ruvector/sona';
13
+ // Default configs for when getModeConfig is unavailable (e.g., cold start, test environments)
14
+ const DEFAULT_MODE_CONFIGS = {
15
+ 'balanced': { mode: 'balanced', loraRank: 4, learningRate: 0.002, batchSize: 32, trajectoryCapacity: 3000, patternClusters: 50, qualityThreshold: 0.5, maxLatencyMs: 18, memoryBudgetMb: 50, ewcLambda: 2000 },
16
+ 'real-time': { mode: 'real-time', loraRank: 2, learningRate: 0.001, batchSize: 32, trajectoryCapacity: 1000, patternClusters: 25, qualityThreshold: 0.7, maxLatencyMs: 0.5, memoryBudgetMb: 25, ewcLambda: 2000 },
17
+ 'research': { mode: 'research', loraRank: 16, learningRate: 0.002, batchSize: 64, trajectoryCapacity: 10000, patternClusters: 100, qualityThreshold: 0.2, maxLatencyMs: 100, memoryBudgetMb: 100, ewcLambda: 2500 },
18
+ 'edge': { mode: 'edge', loraRank: 1, learningRate: 0.001, batchSize: 16, trajectoryCapacity: 200, patternClusters: 15, qualityThreshold: 0.8, maxLatencyMs: 1, memoryBudgetMb: 5, ewcLambda: 1500 },
19
+ 'batch': { mode: 'batch', loraRank: 8, learningRate: 0.002, batchSize: 128, trajectoryCapacity: 5000, patternClusters: 75, qualityThreshold: 0.4, maxLatencyMs: 50, memoryBudgetMb: 75, ewcLambda: 2000 },
20
+ };
21
+ // =============================================================================
22
+ // Mode Configuration Mapping
23
+ // =============================================================================
24
+ /**
25
+ * Convert V3 SONA mode to @ruvector/sona config
26
+ */
27
+ function modeToConfig(mode, modeConfig) {
28
+ const baseConfig = {
29
+ hiddenDim: 768, // Standard transformer dimension
30
+ embeddingDim: 768,
31
+ microLoraRank: modeConfig.loraRank <= 2 ? modeConfig.loraRank : 1,
32
+ baseLoraRank: modeConfig.loraRank,
33
+ microLoraLr: modeConfig.learningRate,
34
+ baseLoraLr: modeConfig.learningRate * 0.1,
35
+ ewcLambda: modeConfig.ewcLambda,
36
+ patternClusters: modeConfig.patternClusters,
37
+ trajectoryCapacity: modeConfig.trajectoryCapacity,
38
+ qualityThreshold: modeConfig.qualityThreshold,
39
+ enableSimd: true,
40
+ };
41
+ // Mode-specific adjustments
42
+ switch (mode) {
43
+ case 'real-time':
44
+ return {
45
+ ...baseConfig,
46
+ microLoraRank: 1,
47
+ backgroundIntervalMs: 60000, // 1 minute
48
+ };
49
+ case 'edge':
50
+ return {
51
+ ...baseConfig,
52
+ hiddenDim: 384, // Smaller for edge devices
53
+ embeddingDim: 384,
54
+ microLoraRank: 1,
55
+ patternClusters: 25,
56
+ backgroundIntervalMs: 300000, // 5 minutes
57
+ };
58
+ case 'research':
59
+ return {
60
+ ...baseConfig,
61
+ baseLoraRank: 16,
62
+ backgroundIntervalMs: 3600000, // 1 hour
63
+ };
64
+ case 'batch':
65
+ return {
66
+ ...baseConfig,
67
+ backgroundIntervalMs: 7200000, // 2 hours
68
+ };
69
+ case 'balanced':
70
+ default:
71
+ return {
72
+ ...baseConfig,
73
+ backgroundIntervalMs: 1800000, // 30 minutes
74
+ };
75
+ }
76
+ }
77
+ // =============================================================================
78
+ // SONA Learning Engine
79
+ // =============================================================================
80
+ /**
81
+ * SONA Learning Engine - wraps @ruvector/sona for V3 usage
82
+ *
83
+ * Performance targets:
84
+ * - learn(): <0.05ms
85
+ * - adapt(): <0.1ms
86
+ * - Full learning cycle: <10ms
87
+ */
88
+ export class SONALearningEngine {
89
+ engine;
90
+ trajectoryMap = new Map();
91
+ adaptationTimeMs = 0;
92
+ learningTimeMs = 0;
93
+ mode;
94
+ modeConfig;
95
+ constructor(mode, modeConfig) {
96
+ this.mode = mode;
97
+ this.modeConfig = modeConfig;
98
+ const config = modeToConfig(mode, modeConfig);
99
+ this.engine = SonaEngine.withConfig(config);
100
+ }
101
+ /**
102
+ * Learn from a trajectory
103
+ *
104
+ * Performance target: <0.05ms
105
+ *
106
+ * @param trajectory - Trajectory to learn from
107
+ */
108
+ async learn(trajectory) {
109
+ const startTime = performance.now();
110
+ try {
111
+ // Begin trajectory recording
112
+ const queryEmbedding = this.trajectoryToQueryEmbedding(trajectory);
113
+ const trajectoryId = this.engine.beginTrajectory(Array.from(queryEmbedding));
114
+ // Add trajectory steps
115
+ for (const step of trajectory.steps) {
116
+ const activations = this.stateToActivations(step.stateBefore);
117
+ const attentionWeights = this.stateToAttentionWeights(step.stateAfter);
118
+ this.engine.addTrajectoryStep(trajectoryId, Array.from(activations), Array.from(attentionWeights), step.reward);
119
+ }
120
+ // Set context if available
121
+ if (trajectory.domain) {
122
+ this.engine.addTrajectoryContext(trajectoryId, trajectory.domain);
123
+ }
124
+ // Complete trajectory with quality score
125
+ const quality = this.calculateQuality(trajectory);
126
+ this.engine.endTrajectory(trajectoryId, quality);
127
+ // Flush instant updates
128
+ this.engine.flush();
129
+ this.learningTimeMs = performance.now() - startTime;
130
+ }
131
+ catch (error) {
132
+ throw new Error(`SONA learning failed: ${error}`);
133
+ }
134
+ }
135
+ /**
136
+ * Adapt behavior based on context
137
+ *
138
+ * @param context - Current context for adaptation
139
+ * @returns Adapted behavior with transformed embeddings
140
+ */
141
+ async adapt(context) {
142
+ const startTime = performance.now();
143
+ try {
144
+ // Apply micro-LoRA transformation
145
+ const transformedQuery = this.engine.applyMicroLora(Array.from(context.queryEmbedding));
146
+ // Find similar patterns
147
+ const patterns = this.engine.findPatterns(Array.from(context.queryEmbedding), 5);
148
+ // Determine suggested route from patterns
149
+ const suggestedRoute = this.inferRoute(patterns, context);
150
+ const confidence = patterns.length > 0 ? patterns[0].avgQuality : 0.5;
151
+ this.adaptationTimeMs = performance.now() - startTime;
152
+ return {
153
+ transformedQuery: new Float32Array(transformedQuery),
154
+ patterns,
155
+ suggestedRoute,
156
+ confidence,
157
+ };
158
+ }
159
+ catch (error) {
160
+ throw new Error(`SONA adaptation failed: ${error}`);
161
+ }
162
+ }
163
+ /**
164
+ * Get last adaptation time
165
+ *
166
+ * @returns Adaptation time in milliseconds
167
+ */
168
+ getAdaptationTime() {
169
+ return this.adaptationTimeMs;
170
+ }
171
+ /**
172
+ * Get last learning time
173
+ *
174
+ * @returns Learning time in milliseconds
175
+ */
176
+ getLearningTime() {
177
+ return this.learningTimeMs;
178
+ }
179
+ /**
180
+ * Reset learning state
181
+ */
182
+ resetLearning() {
183
+ // Create a new engine with the same config
184
+ const config = modeToConfig(this.mode, this.modeConfig);
185
+ this.engine = SonaEngine.withConfig(config);
186
+ this.trajectoryMap.clear();
187
+ this.adaptationTimeMs = 0;
188
+ this.learningTimeMs = 0;
189
+ }
190
+ /**
191
+ * Force immediate learning cycle
192
+ *
193
+ * @returns Status message
194
+ */
195
+ forceLearning() {
196
+ return this.engine.forceLearn();
197
+ }
198
+ /**
199
+ * Tick background learning (call periodically)
200
+ *
201
+ * @returns Status message if learning occurred
202
+ */
203
+ tick() {
204
+ return this.engine.tick();
205
+ }
206
+ /**
207
+ * Get engine statistics
208
+ *
209
+ * @returns SONA engine statistics
210
+ */
211
+ getStats() {
212
+ const statsJson = this.engine.getStats();
213
+ const stats = JSON.parse(statsJson);
214
+ return {
215
+ totalTrajectories: stats.total_trajectories || 0,
216
+ patternsLearned: stats.patterns_learned || 0,
217
+ avgQuality: stats.avg_quality || 0,
218
+ lastLearningMs: this.learningTimeMs,
219
+ enabled: this.engine.isEnabled(),
220
+ };
221
+ }
222
+ /**
223
+ * Enable or disable the engine
224
+ *
225
+ * @param enabled - Whether to enable the engine
226
+ */
227
+ setEnabled(enabled) {
228
+ this.engine.setEnabled(enabled);
229
+ }
230
+ /**
231
+ * Check if engine is enabled
232
+ *
233
+ * @returns Whether the engine is enabled
234
+ */
235
+ isEnabled() {
236
+ return this.engine.isEnabled();
237
+ }
238
+ /**
239
+ * Find learned patterns similar to query
240
+ *
241
+ * @param queryEmbedding - Query embedding
242
+ * @param k - Number of patterns to return
243
+ * @returns Learned patterns
244
+ */
245
+ findPatterns(queryEmbedding, k = 5) {
246
+ return this.engine.findPatterns(Array.from(queryEmbedding), k);
247
+ }
248
+ // =============================================================================
249
+ // Private Helpers
250
+ // =============================================================================
251
+ /**
252
+ * Convert trajectory to query embedding
253
+ */
254
+ trajectoryToQueryEmbedding(trajectory) {
255
+ // Use the first step's state as query
256
+ if (trajectory.steps.length > 0) {
257
+ return trajectory.steps[0].stateBefore;
258
+ }
259
+ // Fallback to zero embedding
260
+ return new Float32Array(768);
261
+ }
262
+ /**
263
+ * Convert state embedding to activations
264
+ */
265
+ stateToActivations(state) {
266
+ // For now, use state directly as activations
267
+ // In a real implementation, this would extract layer activations
268
+ return state;
269
+ }
270
+ /**
271
+ * Convert state embedding to attention weights
272
+ */
273
+ stateToAttentionWeights(state) {
274
+ // For now, use normalized state as attention weights
275
+ // In a real implementation, this would extract attention patterns
276
+ const sum = state.reduce((acc, val) => acc + Math.abs(val), 0);
277
+ if (sum === 0)
278
+ return state;
279
+ const weights = new Float32Array(state.length);
280
+ for (let i = 0; i < state.length; i++) {
281
+ weights[i] = Math.abs(state[i]) / sum;
282
+ }
283
+ return weights;
284
+ }
285
+ /**
286
+ * Calculate quality score for trajectory
287
+ */
288
+ calculateQuality(trajectory) {
289
+ if (trajectory.qualityScore !== undefined) {
290
+ return trajectory.qualityScore;
291
+ }
292
+ // Calculate from steps
293
+ if (trajectory.steps.length === 0)
294
+ return 0.5;
295
+ const avgReward = trajectory.steps.reduce((sum, step) => sum + step.reward, 0) /
296
+ trajectory.steps.length;
297
+ // Normalize to [0, 1]
298
+ return Math.max(0, Math.min(1, (avgReward + 1) / 2));
299
+ }
300
+ /**
301
+ * Infer suggested route from patterns and context
302
+ */
303
+ inferRoute(patterns, context) {
304
+ if (patterns.length === 0)
305
+ return undefined;
306
+ // Use the highest quality pattern's type as route
307
+ const bestPattern = patterns.reduce((best, pattern) => pattern.avgQuality > best.avgQuality ? pattern : best);
308
+ return bestPattern.patternType || `${context.domain}-default`;
309
+ }
310
+ }
311
+ // =============================================================================
312
+ // Factory Functions
313
+ // =============================================================================
314
+ /**
315
+ * Create a SONA learning engine
316
+ *
317
+ * @param mode - SONA learning mode
318
+ * @param modeConfig - Mode configuration
319
+ * @returns SONA learning engine instance
320
+ */
321
+ export function createSONALearningEngine(mode, modeConfig) {
322
+ const resolvedMode = mode ?? 'balanced';
323
+ const resolvedConfig = modeConfig ?? DEFAULT_MODE_CONFIGS[resolvedMode] ?? DEFAULT_MODE_CONFIGS['balanced'];
324
+ return new SONALearningEngine(resolvedMode, resolvedConfig);
325
+ }
326
+ //# sourceMappingURL=sona-integration.js.map