@sparkleideas/ruv-swarm 1.0.18-patch.1
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 +1565 -0
- package/bin/ruv-swarm-clean.js +1872 -0
- package/bin/ruv-swarm-memory.js +119 -0
- package/bin/ruv-swarm-secure-heartbeat.js +1549 -0
- package/bin/ruv-swarm-secure.js +1689 -0
- package/package.json +221 -0
- package/src/agent.ts +342 -0
- package/src/benchmark.js +267 -0
- package/src/claude-flow-enhanced.js +839 -0
- package/src/claude-integration/advanced-commands.js +561 -0
- package/src/claude-integration/core.js +112 -0
- package/src/claude-integration/docs.js +1548 -0
- package/src/claude-integration/env-template.js +39 -0
- package/src/claude-integration/index.js +209 -0
- package/src/claude-integration/remote.js +408 -0
- package/src/cli-diagnostics.js +364 -0
- package/src/cognitive-pattern-evolution.js +1317 -0
- package/src/daa-cognition.js +977 -0
- package/src/daa-service.d.ts +298 -0
- package/src/daa-service.js +1116 -0
- package/src/diagnostics.js +533 -0
- package/src/errors.js +528 -0
- package/src/github-coordinator/README.md +193 -0
- package/src/github-coordinator/claude-hooks.js +162 -0
- package/src/github-coordinator/gh-cli-coordinator.js +260 -0
- package/src/hooks/cli.js +82 -0
- package/src/hooks/index.js +1900 -0
- package/src/index-enhanced.d.ts +371 -0
- package/src/index-enhanced.js +734 -0
- package/src/index.d.ts +287 -0
- package/src/index.js +405 -0
- package/src/index.ts +457 -0
- package/src/logger.js +182 -0
- package/src/logging-config.js +179 -0
- package/src/mcp-daa-tools.js +735 -0
- package/src/mcp-tools-benchmarks.js +328 -0
- package/src/mcp-tools-enhanced.js +2863 -0
- package/src/memory-config.js +42 -0
- package/src/meta-learning-framework.js +1359 -0
- package/src/neural-agent.js +830 -0
- package/src/neural-coordination-protocol.js +1363 -0
- package/src/neural-models/README.md +118 -0
- package/src/neural-models/autoencoder.js +543 -0
- package/src/neural-models/base.js +269 -0
- package/src/neural-models/cnn.js +497 -0
- package/src/neural-models/gnn.js +447 -0
- package/src/neural-models/gru.js +536 -0
- package/src/neural-models/index.js +273 -0
- package/src/neural-models/lstm.js +551 -0
- package/src/neural-models/neural-presets-complete.js +1306 -0
- package/src/neural-models/presets/graph.js +392 -0
- package/src/neural-models/presets/index.js +279 -0
- package/src/neural-models/presets/nlp.js +328 -0
- package/src/neural-models/presets/timeseries.js +368 -0
- package/src/neural-models/presets/vision.js +387 -0
- package/src/neural-models/resnet.js +534 -0
- package/src/neural-models/transformer.js +515 -0
- package/src/neural-models/vae.js +489 -0
- package/src/neural-network-manager.js +1938 -0
- package/src/neural-network.ts +296 -0
- package/src/neural.js +574 -0
- package/src/performance-benchmarks.js +898 -0
- package/src/performance.js +458 -0
- package/src/persistence-pooled.js +695 -0
- package/src/persistence.js +480 -0
- package/src/schemas.js +864 -0
- package/src/security.js +218 -0
- package/src/singleton-container.js +183 -0
- package/src/sqlite-pool.js +587 -0
- package/src/sqlite-worker.js +141 -0
- package/src/types.ts +164 -0
- package/src/utils.ts +286 -0
- package/src/wasm-loader.js +601 -0
- package/src/wasm-loader2.js +404 -0
- package/src/wasm-memory-optimizer.js +783 -0
- package/src/wasm-types.d.ts +63 -0
- package/wasm/README.md +347 -0
- package/wasm/neuro-divergent.wasm +0 -0
- package/wasm/package.json +18 -0
- package/wasm/ruv-fann.wasm +0 -0
- package/wasm/ruv_swarm_simd.wasm +0 -0
- package/wasm/ruv_swarm_wasm.d.ts +391 -0
- package/wasm/ruv_swarm_wasm.js +2164 -0
- package/wasm/ruv_swarm_wasm_bg.wasm +0 -0
- package/wasm/ruv_swarm_wasm_bg.wasm.d.ts +123 -0
- package/wasm/wasm-bindings-loader.mjs +435 -0
- package/wasm/wasm-updates.md +684 -0
|
@@ -0,0 +1,830 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Neural Agent Module - Integrates ruv-FANN neural network capabilities
|
|
3
|
+
* into agent processing for cognitive diversity and learning
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { EventEmitter } from 'events';
|
|
7
|
+
|
|
8
|
+
// Import these after class definitions to avoid circular dependency
|
|
9
|
+
let MemoryOptimizer, PATTERN_MEMORY_CONFIG;
|
|
10
|
+
|
|
11
|
+
// Cognitive diversity patterns for different agent types
|
|
12
|
+
const COGNITIVE_PATTERNS = {
|
|
13
|
+
CONVERGENT: 'convergent', // Focused problem-solving, analytical
|
|
14
|
+
DIVERGENT: 'divergent', // Creative exploration, idea generation
|
|
15
|
+
LATERAL: 'lateral', // Non-linear thinking, pattern breaking
|
|
16
|
+
SYSTEMS: 'systems', // Holistic view, interconnections
|
|
17
|
+
CRITICAL: 'critical', // Evaluation, judgment, validation
|
|
18
|
+
ABSTRACT: 'abstract', // Conceptual thinking, generalization
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Agent type to cognitive pattern mapping
|
|
22
|
+
const AGENT_COGNITIVE_PROFILES = {
|
|
23
|
+
researcher: {
|
|
24
|
+
primary: COGNITIVE_PATTERNS.DIVERGENT,
|
|
25
|
+
secondary: COGNITIVE_PATTERNS.SYSTEMS,
|
|
26
|
+
learningRate: 0.7,
|
|
27
|
+
momentum: 0.3,
|
|
28
|
+
networkLayers: [64, 128, 64, 32],
|
|
29
|
+
activationFunction: 'sigmoid',
|
|
30
|
+
advancedModel: 'transformer_nlp', // Use transformer for research tasks
|
|
31
|
+
},
|
|
32
|
+
coder: {
|
|
33
|
+
primary: COGNITIVE_PATTERNS.CONVERGENT,
|
|
34
|
+
secondary: COGNITIVE_PATTERNS.LATERAL,
|
|
35
|
+
learningRate: 0.5,
|
|
36
|
+
momentum: 0.2,
|
|
37
|
+
networkLayers: [128, 256, 128, 64],
|
|
38
|
+
activationFunction: 'relu',
|
|
39
|
+
advancedModel: 'gru_sequence', // Use GRU for code generation
|
|
40
|
+
},
|
|
41
|
+
analyst: {
|
|
42
|
+
primary: COGNITIVE_PATTERNS.CRITICAL,
|
|
43
|
+
secondary: COGNITIVE_PATTERNS.ABSTRACT,
|
|
44
|
+
learningRate: 0.6,
|
|
45
|
+
momentum: 0.25,
|
|
46
|
+
networkLayers: [96, 192, 96, 48],
|
|
47
|
+
activationFunction: 'tanh',
|
|
48
|
+
advancedModel: 'cnn_vision', // Use CNN for pattern analysis
|
|
49
|
+
},
|
|
50
|
+
optimizer: {
|
|
51
|
+
primary: COGNITIVE_PATTERNS.SYSTEMS,
|
|
52
|
+
secondary: COGNITIVE_PATTERNS.CONVERGENT,
|
|
53
|
+
learningRate: 0.4,
|
|
54
|
+
momentum: 0.35,
|
|
55
|
+
networkLayers: [80, 160, 80, 40],
|
|
56
|
+
activationFunction: 'sigmoid',
|
|
57
|
+
},
|
|
58
|
+
coordinator: {
|
|
59
|
+
primary: COGNITIVE_PATTERNS.SYSTEMS,
|
|
60
|
+
secondary: COGNITIVE_PATTERNS.CRITICAL,
|
|
61
|
+
learningRate: 0.55,
|
|
62
|
+
momentum: 0.3,
|
|
63
|
+
networkLayers: [112, 224, 112, 56],
|
|
64
|
+
activationFunction: 'relu',
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Neural Network wrapper for agent cognitive processing
|
|
70
|
+
*/
|
|
71
|
+
class NeuralNetwork {
|
|
72
|
+
constructor(config, memoryOptimizer = null) {
|
|
73
|
+
this.config = config;
|
|
74
|
+
this.layers = config.networkLayers;
|
|
75
|
+
this.activationFunction = config.activationFunction;
|
|
76
|
+
this.learningRate = config.learningRate;
|
|
77
|
+
this.momentum = config.momentum;
|
|
78
|
+
this.memoryOptimizer = memoryOptimizer;
|
|
79
|
+
|
|
80
|
+
// Memory-optimized storage
|
|
81
|
+
this.weights = [];
|
|
82
|
+
this.biases = [];
|
|
83
|
+
this.previousWeightDeltas = [];
|
|
84
|
+
this.memoryAllocations = [];
|
|
85
|
+
|
|
86
|
+
this._initializeNetwork();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
_initializeNetwork() {
|
|
90
|
+
// Initialize weights and biases between layers with memory optimization
|
|
91
|
+
for (let i = 0; i < this.layers.length - 1; i++) {
|
|
92
|
+
const inputSize = this.layers[i];
|
|
93
|
+
const outputSize = this.layers[i + 1];
|
|
94
|
+
|
|
95
|
+
// Xavier/Glorot initialization
|
|
96
|
+
const limit = Math.sqrt(6 / (inputSize + outputSize));
|
|
97
|
+
|
|
98
|
+
// Try to allocate from memory pool if available
|
|
99
|
+
if (this.memoryOptimizer && this.memoryOptimizer.isPoolInitialized()) {
|
|
100
|
+
const weightSize = outputSize * inputSize * 4; // 4 bytes per float32
|
|
101
|
+
const biasSize = outputSize * 4;
|
|
102
|
+
|
|
103
|
+
const weightAlloc = this.memoryOptimizer.allocateFromPool('weights', weightSize, this.config.cognitivePattern || 'default');
|
|
104
|
+
const biasAlloc = this.memoryOptimizer.allocateFromPool('weights', biasSize, this.config.cognitivePattern || 'default');
|
|
105
|
+
|
|
106
|
+
if (weightAlloc && biasAlloc) {
|
|
107
|
+
this.memoryAllocations.push(weightAlloc, biasAlloc);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Create matrices (in optimized implementation, these would use pooled memory)
|
|
112
|
+
this.weights[i] = this._createMatrix(outputSize, inputSize, -limit, limit);
|
|
113
|
+
this.biases[i] = this._createVector(outputSize, -0.1, 0.1);
|
|
114
|
+
this.previousWeightDeltas[i] = this._createMatrix(outputSize, inputSize, 0, 0);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
_createMatrix(rows, cols, min, max) {
|
|
119
|
+
const matrix = [];
|
|
120
|
+
for (let i = 0; i < rows; i++) {
|
|
121
|
+
matrix[i] = [];
|
|
122
|
+
for (let j = 0; j < cols; j++) {
|
|
123
|
+
matrix[i][j] = Math.random() * (max - min) + min;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return matrix;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
_createVector(size, min, max) {
|
|
130
|
+
const vector = [];
|
|
131
|
+
for (let i = 0; i < size; i++) {
|
|
132
|
+
vector[i] = Math.random() * (max - min) + min;
|
|
133
|
+
}
|
|
134
|
+
return vector;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
_activation(x, derivative = false) {
|
|
138
|
+
switch (this.activationFunction) {
|
|
139
|
+
case 'sigmoid':
|
|
140
|
+
if (derivative) {
|
|
141
|
+
const sig = 1 / (1 + Math.exp(-x));
|
|
142
|
+
return sig * (1 - sig);
|
|
143
|
+
}
|
|
144
|
+
return 1 / (1 + Math.exp(-x));
|
|
145
|
+
|
|
146
|
+
case 'tanh':
|
|
147
|
+
if (derivative) {
|
|
148
|
+
const tanh = Math.tanh(x);
|
|
149
|
+
return 1 - tanh * tanh;
|
|
150
|
+
}
|
|
151
|
+
return Math.tanh(x);
|
|
152
|
+
|
|
153
|
+
case 'relu':
|
|
154
|
+
if (derivative) {
|
|
155
|
+
return x > 0 ? 1 : 0;
|
|
156
|
+
}
|
|
157
|
+
return Math.max(0, x);
|
|
158
|
+
|
|
159
|
+
default:
|
|
160
|
+
return x;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
forward(input) {
|
|
165
|
+
const activations = [input];
|
|
166
|
+
let currentInput = input;
|
|
167
|
+
|
|
168
|
+
// Forward propagation through layers
|
|
169
|
+
for (let i = 0; i < this.weights.length; i++) {
|
|
170
|
+
const weights = this.weights[i];
|
|
171
|
+
const biases = this.biases[i];
|
|
172
|
+
const output = [];
|
|
173
|
+
|
|
174
|
+
for (let j = 0; j < weights.length; j++) {
|
|
175
|
+
let sum = biases[j];
|
|
176
|
+
for (let k = 0; k < currentInput.length; k++) {
|
|
177
|
+
sum += weights[j][k] * currentInput[k];
|
|
178
|
+
}
|
|
179
|
+
output[j] = this._activation(sum);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
activations.push(output);
|
|
183
|
+
currentInput = output;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
output: currentInput,
|
|
188
|
+
activations,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
train(input, target, learningRate = null) {
|
|
193
|
+
const lr = learningRate || this.learningRate;
|
|
194
|
+
const { activations } = this.forward(input);
|
|
195
|
+
|
|
196
|
+
// Backward propagation
|
|
197
|
+
const errors = [];
|
|
198
|
+
const output = activations[activations.length - 1];
|
|
199
|
+
|
|
200
|
+
// Calculate output layer error
|
|
201
|
+
const outputError = [];
|
|
202
|
+
for (let i = 0; i < output.length; i++) {
|
|
203
|
+
outputError[i] = (target[i] - output[i]) * this._activation(output[i], true);
|
|
204
|
+
}
|
|
205
|
+
errors.unshift(outputError);
|
|
206
|
+
|
|
207
|
+
// Backpropagate errors
|
|
208
|
+
for (let i = this.weights.length - 1; i > 0; i--) {
|
|
209
|
+
const layerError = [];
|
|
210
|
+
const weights = this.weights[i];
|
|
211
|
+
const prevError = errors[0];
|
|
212
|
+
|
|
213
|
+
for (let j = 0; j < this.weights[i - 1].length; j++) {
|
|
214
|
+
let error = 0;
|
|
215
|
+
for (let k = 0; k < weights.length; k++) {
|
|
216
|
+
error += weights[k][j] * prevError[k];
|
|
217
|
+
}
|
|
218
|
+
layerError[j] = error * this._activation(activations[i][j], true);
|
|
219
|
+
}
|
|
220
|
+
errors.unshift(layerError);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Update weights and biases
|
|
224
|
+
for (let i = 0; i < this.weights.length; i++) {
|
|
225
|
+
const weights = this.weights[i];
|
|
226
|
+
const biases = this.biases[i];
|
|
227
|
+
const layerError = errors[i + 1];
|
|
228
|
+
const layerInput = activations[i];
|
|
229
|
+
|
|
230
|
+
for (let j = 0; j < weights.length; j++) {
|
|
231
|
+
// Update bias
|
|
232
|
+
biases[j] += lr * layerError[j];
|
|
233
|
+
|
|
234
|
+
// Update weights with momentum
|
|
235
|
+
for (let k = 0; k < weights[j].length; k++) {
|
|
236
|
+
const delta = lr * layerError[j] * layerInput[k];
|
|
237
|
+
const momentumDelta = this.momentum * this.previousWeightDeltas[i][j][k];
|
|
238
|
+
weights[j][k] += delta + momentumDelta;
|
|
239
|
+
this.previousWeightDeltas[i][j][k] = delta;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return output;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
save() {
|
|
248
|
+
return {
|
|
249
|
+
config: this.config,
|
|
250
|
+
weights: this.weights,
|
|
251
|
+
biases: this.biases,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
load(data) {
|
|
256
|
+
this.weights = data.weights;
|
|
257
|
+
this.biases = data.biases;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Neural Agent class that enhances base agents with neural network capabilities
|
|
263
|
+
*/
|
|
264
|
+
class NeuralAgent extends EventEmitter {
|
|
265
|
+
constructor(agent, agentType, memoryOptimizer = null) {
|
|
266
|
+
super();
|
|
267
|
+
this.agent = agent;
|
|
268
|
+
this.agentType = agentType;
|
|
269
|
+
this.cognitiveProfile = AGENT_COGNITIVE_PROFILES[agentType];
|
|
270
|
+
this.memoryOptimizer = memoryOptimizer || new MemoryOptimizer();
|
|
271
|
+
|
|
272
|
+
// Add cognitive pattern to neural network config for memory optimization
|
|
273
|
+
const networkConfig = {
|
|
274
|
+
...this.cognitiveProfile,
|
|
275
|
+
cognitivePattern: this.cognitiveProfile.primary,
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
// Initialize neural network with memory optimizer
|
|
279
|
+
this.neuralNetwork = new NeuralNetwork(networkConfig, this.memoryOptimizer);
|
|
280
|
+
|
|
281
|
+
// Learning history for feedback loops
|
|
282
|
+
this.learningHistory = [];
|
|
283
|
+
this.taskHistory = [];
|
|
284
|
+
this.performanceMetrics = {
|
|
285
|
+
accuracy: 0,
|
|
286
|
+
speed: 0,
|
|
287
|
+
creativity: 0,
|
|
288
|
+
efficiency: 0,
|
|
289
|
+
memoryEfficiency: 0,
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
// Cognitive state
|
|
293
|
+
this.cognitiveState = {
|
|
294
|
+
attention: 1.0,
|
|
295
|
+
fatigue: 0.0,
|
|
296
|
+
confidence: 0.5,
|
|
297
|
+
exploration: 0.5,
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
// Track memory usage
|
|
301
|
+
this.memoryUsage = {
|
|
302
|
+
baseline: 0,
|
|
303
|
+
current: 0,
|
|
304
|
+
peak: 0,
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
this._initializeMemoryTracking();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Process task through neural network for intelligent routing
|
|
312
|
+
*/
|
|
313
|
+
async analyzeTask(task) {
|
|
314
|
+
// Convert task to neural input vector
|
|
315
|
+
const inputVector = this._taskToVector(task);
|
|
316
|
+
|
|
317
|
+
// Get neural network prediction
|
|
318
|
+
const { output } = this.neuralNetwork.forward(inputVector);
|
|
319
|
+
|
|
320
|
+
// Interpret output for task routing
|
|
321
|
+
const analysis = {
|
|
322
|
+
complexity: output[0],
|
|
323
|
+
urgency: output[1],
|
|
324
|
+
creativity: output[2],
|
|
325
|
+
dataIntensity: output[3],
|
|
326
|
+
collaborationNeeded: output[4],
|
|
327
|
+
confidence: output[5],
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
// Apply cognitive pattern influence
|
|
331
|
+
this._applyCognitivePattern(analysis);
|
|
332
|
+
|
|
333
|
+
return analysis;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Execute task with neural enhancement
|
|
338
|
+
*/
|
|
339
|
+
async executeTask(task) {
|
|
340
|
+
const startTime = Date.now();
|
|
341
|
+
|
|
342
|
+
// Analyze task
|
|
343
|
+
const analysis = await this.analyzeTask(task);
|
|
344
|
+
|
|
345
|
+
// Adjust cognitive state based on task
|
|
346
|
+
this._updateCognitiveState(analysis);
|
|
347
|
+
|
|
348
|
+
// Execute base agent task
|
|
349
|
+
const result = await this.agent.execute({
|
|
350
|
+
...task,
|
|
351
|
+
neuralAnalysis: analysis,
|
|
352
|
+
cognitiveState: this.cognitiveState,
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
// Calculate performance
|
|
356
|
+
const executionTime = Date.now() - startTime;
|
|
357
|
+
const performance = this._calculatePerformance(task, result, executionTime);
|
|
358
|
+
|
|
359
|
+
// Learn from the experience
|
|
360
|
+
await this._learnFromExecution(task, result, performance);
|
|
361
|
+
|
|
362
|
+
// Emit events for monitoring
|
|
363
|
+
this.emit('taskCompleted', {
|
|
364
|
+
task,
|
|
365
|
+
result,
|
|
366
|
+
performance,
|
|
367
|
+
cognitiveState: this.cognitiveState,
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
return result;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Convert task to neural network input vector
|
|
375
|
+
*/
|
|
376
|
+
_taskToVector(task) {
|
|
377
|
+
const vector = [];
|
|
378
|
+
|
|
379
|
+
// Task description features (simplified for example)
|
|
380
|
+
const description = task.description || '';
|
|
381
|
+
vector.push(
|
|
382
|
+
description.length / 1000, // Length normalized
|
|
383
|
+
(description.match(/\b\w+\b/g) || []).length / 100, // Word count
|
|
384
|
+
(description.match(/[A-Z]/g) || []).length / description.length, // Capitalization ratio
|
|
385
|
+
(description.match(/[0-9]/g) || []).length / description.length, // Numeric ratio
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
// Task metadata
|
|
389
|
+
const priorityMap = { low: 0.2, medium: 0.5, high: 0.8, critical: 1.0 };
|
|
390
|
+
vector.push(priorityMap[task.priority] || 0.5);
|
|
391
|
+
|
|
392
|
+
// Dependencies
|
|
393
|
+
vector.push(Math.min(task.dependencies?.length || 0, 10) / 10);
|
|
394
|
+
|
|
395
|
+
// Historical performance on similar tasks
|
|
396
|
+
const similarTasks = this._findSimilarTasks(task);
|
|
397
|
+
if (similarTasks.length > 0) {
|
|
398
|
+
const avgPerformance = similarTasks.reduce((sum, t) => sum + t.performance.overall, 0) / similarTasks.length;
|
|
399
|
+
vector.push(avgPerformance);
|
|
400
|
+
} else {
|
|
401
|
+
vector.push(0.5); // Neutral if no history
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Current cognitive state influence
|
|
405
|
+
vector.push(
|
|
406
|
+
this.cognitiveState.attention,
|
|
407
|
+
this.cognitiveState.fatigue,
|
|
408
|
+
this.cognitiveState.confidence,
|
|
409
|
+
this.cognitiveState.exploration,
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
// Pad or truncate to expected input size
|
|
413
|
+
const inputSize = this.neuralNetwork.layers[0];
|
|
414
|
+
while (vector.length < inputSize) {
|
|
415
|
+
vector.push(0);
|
|
416
|
+
}
|
|
417
|
+
return vector.slice(0, inputSize);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Apply cognitive pattern to analysis
|
|
422
|
+
*/
|
|
423
|
+
_applyCognitivePattern(analysis) {
|
|
424
|
+
const primary = this.cognitiveProfile.primary;
|
|
425
|
+
const secondary = this.cognitiveProfile.secondary;
|
|
426
|
+
|
|
427
|
+
switch (primary) {
|
|
428
|
+
case COGNITIVE_PATTERNS.CONVERGENT:
|
|
429
|
+
analysis.complexity *= 0.9; // Simplify through focus
|
|
430
|
+
analysis.confidence *= 1.1; // Higher confidence in solutions
|
|
431
|
+
break;
|
|
432
|
+
|
|
433
|
+
case COGNITIVE_PATTERNS.DIVERGENT:
|
|
434
|
+
analysis.creativity *= 1.2; // Boost creative requirements
|
|
435
|
+
analysis.exploration = 0.8; // High exploration tendency
|
|
436
|
+
break;
|
|
437
|
+
|
|
438
|
+
case COGNITIVE_PATTERNS.LATERAL:
|
|
439
|
+
analysis.creativity *= 1.15; // Enhance creative thinking
|
|
440
|
+
analysis.complexity *= 1.05; // See hidden complexity
|
|
441
|
+
break;
|
|
442
|
+
|
|
443
|
+
case COGNITIVE_PATTERNS.SYSTEMS:
|
|
444
|
+
analysis.collaborationNeeded *= 1.2; // See interconnections
|
|
445
|
+
analysis.dataIntensity *= 1.1; // Process more context
|
|
446
|
+
break;
|
|
447
|
+
|
|
448
|
+
case COGNITIVE_PATTERNS.CRITICAL:
|
|
449
|
+
analysis.confidence *= 0.9; // More cautious
|
|
450
|
+
analysis.complexity *= 1.1; // See more edge cases
|
|
451
|
+
break;
|
|
452
|
+
|
|
453
|
+
case COGNITIVE_PATTERNS.ABSTRACT:
|
|
454
|
+
analysis.complexity *= 0.95; // Simplify through abstraction
|
|
455
|
+
analysis.creativity *= 1.05; // Abstract thinking is creative
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// Apply secondary pattern with lesser influence
|
|
460
|
+
this._applySecondaryPattern(analysis, secondary);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Update cognitive state based on task execution
|
|
465
|
+
*/
|
|
466
|
+
_updateCognitiveState(analysis) {
|
|
467
|
+
// Fatigue increases with complexity
|
|
468
|
+
this.cognitiveState.fatigue = Math.min(
|
|
469
|
+
this.cognitiveState.fatigue + analysis.complexity * 0.1,
|
|
470
|
+
1.0,
|
|
471
|
+
);
|
|
472
|
+
|
|
473
|
+
// Attention decreases with fatigue
|
|
474
|
+
this.cognitiveState.attention = Math.max(
|
|
475
|
+
1.0 - this.cognitiveState.fatigue * 0.5,
|
|
476
|
+
0.3,
|
|
477
|
+
);
|
|
478
|
+
|
|
479
|
+
// Confidence adjusts based on recent performance
|
|
480
|
+
if (this.learningHistory.length > 0) {
|
|
481
|
+
const recentPerformance = this.learningHistory.slice(-5)
|
|
482
|
+
.reduce((sum, h) => sum + h.performance, 0) / Math.min(this.learningHistory.length, 5);
|
|
483
|
+
this.cognitiveState.confidence = 0.3 + recentPerformance * 0.7;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// Exploration vs exploitation balance
|
|
487
|
+
this.cognitiveState.exploration = 0.2 + (1.0 - this.cognitiveState.confidence) * 0.6;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Calculate performance metrics
|
|
492
|
+
*/
|
|
493
|
+
_calculatePerformance(task, result, executionTime) {
|
|
494
|
+
const performance = {
|
|
495
|
+
speed: Math.max(0, 1 - (executionTime / 60000)), // Normalize to 1 minute
|
|
496
|
+
accuracy: result.success ? 0.8 : 0.2,
|
|
497
|
+
creativity: 0.5, // Default, should be evaluated based on result
|
|
498
|
+
efficiency: 0.5,
|
|
499
|
+
overall: 0.5,
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
// Adjust based on result quality indicators
|
|
503
|
+
if (result.metrics) {
|
|
504
|
+
if (result.metrics.linesOfCode) {
|
|
505
|
+
performance.efficiency = Math.min(1.0, 100 / result.metrics.linesOfCode);
|
|
506
|
+
}
|
|
507
|
+
if (result.metrics.testsPass) {
|
|
508
|
+
performance.accuracy = result.metrics.testsPass;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// Calculate overall performance
|
|
513
|
+
performance.overall = (
|
|
514
|
+
performance.speed * 0.2 +
|
|
515
|
+
performance.accuracy * 0.4 +
|
|
516
|
+
performance.creativity * 0.2 +
|
|
517
|
+
performance.efficiency * 0.2
|
|
518
|
+
);
|
|
519
|
+
|
|
520
|
+
return performance;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Learn from task execution
|
|
525
|
+
*/
|
|
526
|
+
async _learnFromExecution(task, result, performance) {
|
|
527
|
+
// Prepare training data
|
|
528
|
+
const input = this._taskToVector(task);
|
|
529
|
+
const target = [
|
|
530
|
+
performance.overall,
|
|
531
|
+
performance.speed,
|
|
532
|
+
performance.accuracy,
|
|
533
|
+
performance.creativity,
|
|
534
|
+
performance.efficiency,
|
|
535
|
+
result.success ? 1.0 : 0.0,
|
|
536
|
+
];
|
|
537
|
+
|
|
538
|
+
// Train neural network
|
|
539
|
+
this.neuralNetwork.train(input, target);
|
|
540
|
+
|
|
541
|
+
// Store in learning history
|
|
542
|
+
this.learningHistory.push({
|
|
543
|
+
timestamp: Date.now(),
|
|
544
|
+
task: task.id,
|
|
545
|
+
performance: performance.overall,
|
|
546
|
+
input,
|
|
547
|
+
target,
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
// Keep history size manageable
|
|
551
|
+
if (this.learningHistory.length > 1000) {
|
|
552
|
+
this.learningHistory = this.learningHistory.slice(-500);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// Update performance metrics
|
|
556
|
+
this._updatePerformanceMetrics(performance);
|
|
557
|
+
|
|
558
|
+
// Emit learning event
|
|
559
|
+
this.emit('learning', {
|
|
560
|
+
task: task.id,
|
|
561
|
+
performance,
|
|
562
|
+
networkState: this.neuralNetwork.save(),
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Update overall performance metrics
|
|
568
|
+
*/
|
|
569
|
+
_updatePerformanceMetrics(performance) {
|
|
570
|
+
const alpha = 0.1; // Learning rate for exponential moving average
|
|
571
|
+
|
|
572
|
+
this.performanceMetrics.accuracy =
|
|
573
|
+
(1 - alpha) * this.performanceMetrics.accuracy + alpha * performance.accuracy;
|
|
574
|
+
this.performanceMetrics.speed =
|
|
575
|
+
(1 - alpha) * this.performanceMetrics.speed + alpha * performance.speed;
|
|
576
|
+
this.performanceMetrics.creativity =
|
|
577
|
+
(1 - alpha) * this.performanceMetrics.creativity + alpha * performance.creativity;
|
|
578
|
+
this.performanceMetrics.efficiency =
|
|
579
|
+
(1 - alpha) * this.performanceMetrics.efficiency + alpha * performance.efficiency;
|
|
580
|
+
|
|
581
|
+
// Calculate memory efficiency based on task completion vs memory usage
|
|
582
|
+
const memoryRatio = this.memoryUsage.baseline / this.getCurrentMemoryUsage();
|
|
583
|
+
const taskEfficiency = performance.overall;
|
|
584
|
+
this.performanceMetrics.memoryEfficiency =
|
|
585
|
+
(1 - alpha) * this.performanceMetrics.memoryEfficiency + alpha * (memoryRatio * taskEfficiency);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Find similar tasks from history
|
|
590
|
+
*/
|
|
591
|
+
_findSimilarTasks(task, limit = 5) {
|
|
592
|
+
if (this.taskHistory.length === 0) {
|
|
593
|
+
return [];
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// Simple similarity based on task properties
|
|
597
|
+
const similarities = this.taskHistory.map(historicalTask => {
|
|
598
|
+
let similarity = 0;
|
|
599
|
+
|
|
600
|
+
// Priority match
|
|
601
|
+
if (historicalTask.task.priority === task.priority) {
|
|
602
|
+
similarity += 0.3;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// Description similarity (simple word overlap)
|
|
606
|
+
const currentWords = new Set((task.description || '').toLowerCase().split(/\s+/));
|
|
607
|
+
const historicalWords = new Set((historicalTask.task.description || '').toLowerCase().split(/\s+/));
|
|
608
|
+
const intersection = new Set([...currentWords].filter(x => historicalWords.has(x)));
|
|
609
|
+
const union = new Set([...currentWords, ...historicalWords]);
|
|
610
|
+
if (union.size > 0) {
|
|
611
|
+
similarity += 0.7 * (intersection.size / union.size);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
return {
|
|
615
|
+
task: historicalTask,
|
|
616
|
+
similarity,
|
|
617
|
+
};
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
// Return top similar tasks
|
|
621
|
+
return similarities
|
|
622
|
+
.sort((a, b) => b.similarity - a.similarity)
|
|
623
|
+
.slice(0, limit)
|
|
624
|
+
.filter(s => s.similarity > 0.3)
|
|
625
|
+
.map(s => s.task);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Apply secondary cognitive pattern
|
|
630
|
+
*/
|
|
631
|
+
_applySecondaryPattern(analysis, pattern) {
|
|
632
|
+
const influence = 0.5; // Secondary patterns have less influence
|
|
633
|
+
|
|
634
|
+
switch (pattern) {
|
|
635
|
+
case COGNITIVE_PATTERNS.CONVERGENT:
|
|
636
|
+
analysis.complexity *= (1 - influence * 0.1);
|
|
637
|
+
analysis.confidence *= (1 + influence * 0.1);
|
|
638
|
+
break;
|
|
639
|
+
|
|
640
|
+
case COGNITIVE_PATTERNS.DIVERGENT:
|
|
641
|
+
analysis.creativity *= (1 + influence * 0.2);
|
|
642
|
+
break;
|
|
643
|
+
|
|
644
|
+
case COGNITIVE_PATTERNS.LATERAL:
|
|
645
|
+
analysis.creativity *= (1 + influence * 0.15);
|
|
646
|
+
break;
|
|
647
|
+
|
|
648
|
+
case COGNITIVE_PATTERNS.SYSTEMS:
|
|
649
|
+
analysis.collaborationNeeded *= (1 + influence * 0.2);
|
|
650
|
+
break;
|
|
651
|
+
|
|
652
|
+
case COGNITIVE_PATTERNS.CRITICAL:
|
|
653
|
+
analysis.confidence *= (1 - influence * 0.1);
|
|
654
|
+
break;
|
|
655
|
+
|
|
656
|
+
case COGNITIVE_PATTERNS.ABSTRACT:
|
|
657
|
+
analysis.complexity *= (1 - influence * 0.05);
|
|
658
|
+
break;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* Rest the agent to reduce fatigue
|
|
664
|
+
*/
|
|
665
|
+
rest(duration = 1000) {
|
|
666
|
+
return new Promise((resolve) => {
|
|
667
|
+
setTimeout(async() => {
|
|
668
|
+
this.cognitiveState.fatigue = Math.max(0, this.cognitiveState.fatigue - 0.3);
|
|
669
|
+
this.cognitiveState.attention = Math.min(1.0, this.cognitiveState.attention + 0.2);
|
|
670
|
+
|
|
671
|
+
// Perform garbage collection on memory pools during rest
|
|
672
|
+
if (this.memoryOptimizer && this.memoryOptimizer.isPoolInitialized()) {
|
|
673
|
+
const collected = await this.memoryOptimizer.garbageCollect();
|
|
674
|
+
if (collected > 0) {
|
|
675
|
+
// Recalculate memory usage after GC
|
|
676
|
+
const patternConfig = PATTERN_MEMORY_CONFIG[this.cognitiveProfile.primary];
|
|
677
|
+
this.memoryUsage.current = patternConfig.baseMemory * (1 - patternConfig.poolSharing * 0.5);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
resolve();
|
|
682
|
+
}, duration);
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* Initialize memory tracking for the agent
|
|
688
|
+
*/
|
|
689
|
+
_initializeMemoryTracking() {
|
|
690
|
+
const patternConfig = PATTERN_MEMORY_CONFIG[this.cognitiveProfile.primary] || PATTERN_MEMORY_CONFIG.convergent;
|
|
691
|
+
this.memoryUsage.baseline = patternConfig.baseMemory;
|
|
692
|
+
this.memoryUsage.current = patternConfig.baseMemory;
|
|
693
|
+
|
|
694
|
+
// Initialize memory pools if not already done
|
|
695
|
+
if (!this.memoryOptimizer.isPoolInitialized()) {
|
|
696
|
+
this.memoryOptimizer.initializePools().then(() => {
|
|
697
|
+
// Recalculate memory usage with pooling
|
|
698
|
+
this.memoryUsage.current = patternConfig.baseMemory * (1 - patternConfig.poolSharing * 0.5);
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Get current memory usage for this agent
|
|
705
|
+
*/
|
|
706
|
+
getCurrentMemoryUsage() {
|
|
707
|
+
// const patternConfig = PATTERN_MEMORY_CONFIG[this.cognitiveProfile.primary] || PATTERN_MEMORY_CONFIG.convergent;
|
|
708
|
+
let memoryUsage = this.memoryUsage.current;
|
|
709
|
+
|
|
710
|
+
// Adjust based on current activity
|
|
711
|
+
if (this.cognitiveState.fatigue > 0.5) {
|
|
712
|
+
memoryUsage *= 1.1; // 10% more memory when fatigued
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
if (this.taskHistory.length > 100) {
|
|
716
|
+
memoryUsage *= 1.05; // 5% more for large history
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// Update peak if necessary
|
|
720
|
+
if (memoryUsage > this.memoryUsage.peak) {
|
|
721
|
+
this.memoryUsage.peak = memoryUsage;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
return memoryUsage;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* Get agent status including neural state
|
|
729
|
+
*/
|
|
730
|
+
getStatus() {
|
|
731
|
+
return {
|
|
732
|
+
...this.agent,
|
|
733
|
+
neuralState: {
|
|
734
|
+
cognitiveProfile: this.cognitiveProfile,
|
|
735
|
+
cognitiveState: this.cognitiveState,
|
|
736
|
+
performanceMetrics: this.performanceMetrics,
|
|
737
|
+
learningHistory: this.learningHistory.length,
|
|
738
|
+
taskHistory: this.taskHistory.length,
|
|
739
|
+
memoryUsage: {
|
|
740
|
+
current: `${this.getCurrentMemoryUsage().toFixed(0) } MB`,
|
|
741
|
+
baseline: `${this.memoryUsage.baseline.toFixed(0) } MB`,
|
|
742
|
+
peak: `${this.memoryUsage.peak.toFixed(0) } MB`,
|
|
743
|
+
efficiency: this.performanceMetrics.memoryEfficiency.toFixed(2),
|
|
744
|
+
},
|
|
745
|
+
},
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Save neural state for persistence
|
|
751
|
+
*/
|
|
752
|
+
saveNeuralState() {
|
|
753
|
+
return {
|
|
754
|
+
agentType: this.agentType,
|
|
755
|
+
neuralNetwork: this.neuralNetwork.save(),
|
|
756
|
+
cognitiveState: this.cognitiveState,
|
|
757
|
+
performanceMetrics: this.performanceMetrics,
|
|
758
|
+
learningHistory: this.learningHistory.slice(-100), // Keep recent history
|
|
759
|
+
taskHistory: this.taskHistory.slice(-100),
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Load neural state from saved data
|
|
765
|
+
*/
|
|
766
|
+
loadNeuralState(data) {
|
|
767
|
+
if (data.neuralNetwork) {
|
|
768
|
+
this.neuralNetwork.load(data.neuralNetwork);
|
|
769
|
+
}
|
|
770
|
+
if (data.cognitiveState) {
|
|
771
|
+
this.cognitiveState = data.cognitiveState;
|
|
772
|
+
}
|
|
773
|
+
if (data.performanceMetrics) {
|
|
774
|
+
this.performanceMetrics = data.performanceMetrics;
|
|
775
|
+
}
|
|
776
|
+
if (data.learningHistory) {
|
|
777
|
+
this.learningHistory = data.learningHistory;
|
|
778
|
+
}
|
|
779
|
+
if (data.taskHistory) {
|
|
780
|
+
this.taskHistory = data.taskHistory;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* Neural Agent Factory
|
|
787
|
+
*/
|
|
788
|
+
class NeuralAgentFactory {
|
|
789
|
+
static memoryOptimizer = null;
|
|
790
|
+
|
|
791
|
+
static async initializeFactory() {
|
|
792
|
+
if (!this.memoryOptimizer) {
|
|
793
|
+
this.memoryOptimizer = new MemoryOptimizer();
|
|
794
|
+
await this.memoryOptimizer.initializePools();
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
static createNeuralAgent(baseAgent, agentType) {
|
|
799
|
+
if (!AGENT_COGNITIVE_PROFILES[agentType]) {
|
|
800
|
+
throw new Error(`Unknown agent type: ${agentType}`);
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
// Use shared memory optimizer for all agents
|
|
804
|
+
return new NeuralAgent(baseAgent, agentType, this.memoryOptimizer);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
static getCognitiveProfiles() {
|
|
808
|
+
return AGENT_COGNITIVE_PROFILES;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
static getCognitivePatterns() {
|
|
812
|
+
return COGNITIVE_PATTERNS;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// Lazy load to avoid circular dependency
|
|
817
|
+
setImmediate(() => {
|
|
818
|
+
import('./neural.js').then(neural => {
|
|
819
|
+
MemoryOptimizer = neural.MemoryOptimizer;
|
|
820
|
+
PATTERN_MEMORY_CONFIG = neural.PATTERN_MEMORY_CONFIG;
|
|
821
|
+
});
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
export {
|
|
825
|
+
NeuralAgent,
|
|
826
|
+
NeuralAgentFactory,
|
|
827
|
+
NeuralNetwork,
|
|
828
|
+
COGNITIVE_PATTERNS,
|
|
829
|
+
AGENT_COGNITIVE_PROFILES,
|
|
830
|
+
};
|