ruvector 0.2.21 → 0.2.23
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 +2 -2
- package/bin/cli.js +160 -0
- package/package.json +9 -5
- package/src/decompiler/api-prober.js +302 -0
- package/src/decompiler/model-decompiler.js +423 -0
- package/dist/analysis/complexity.d.ts +0 -52
- package/dist/analysis/complexity.d.ts.map +0 -1
- package/dist/analysis/complexity.js +0 -146
- package/dist/analysis/index.d.ts +0 -15
- package/dist/analysis/index.d.ts.map +0 -1
- package/dist/analysis/index.js +0 -38
- package/dist/analysis/patterns.d.ts +0 -71
- package/dist/analysis/patterns.d.ts.map +0 -1
- package/dist/analysis/patterns.js +0 -243
- package/dist/analysis/security.d.ts +0 -51
- package/dist/analysis/security.d.ts.map +0 -1
- package/dist/analysis/security.js +0 -139
- package/dist/core/adaptive-embedder.d.ts +0 -156
- package/dist/core/adaptive-embedder.d.ts.map +0 -1
- package/dist/core/adaptive-embedder.js +0 -838
- package/dist/core/agentdb-fast.d.ts +0 -149
- package/dist/core/agentdb-fast.d.ts.map +0 -1
- package/dist/core/agentdb-fast.js +0 -301
- package/dist/core/ast-parser.d.ts +0 -108
- package/dist/core/ast-parser.d.ts.map +0 -1
- package/dist/core/ast-parser.js +0 -602
- package/dist/core/attention-fallbacks.d.ts +0 -321
- package/dist/core/attention-fallbacks.d.ts.map +0 -1
- package/dist/core/attention-fallbacks.js +0 -552
- package/dist/core/cluster-wrapper.d.ts +0 -148
- package/dist/core/cluster-wrapper.d.ts.map +0 -1
- package/dist/core/cluster-wrapper.js +0 -271
- package/dist/core/coverage-router.d.ts +0 -88
- package/dist/core/coverage-router.d.ts.map +0 -1
- package/dist/core/coverage-router.js +0 -315
- package/dist/core/diff-embeddings.d.ts +0 -93
- package/dist/core/diff-embeddings.d.ts.map +0 -1
- package/dist/core/diff-embeddings.js +0 -334
- package/dist/core/gnn-wrapper.d.ts +0 -143
- package/dist/core/gnn-wrapper.d.ts.map +0 -1
- package/dist/core/gnn-wrapper.js +0 -213
- package/dist/core/graph-algorithms.d.ts +0 -83
- package/dist/core/graph-algorithms.d.ts.map +0 -1
- package/dist/core/graph-algorithms.js +0 -514
- package/dist/core/graph-wrapper.d.ts +0 -147
- package/dist/core/graph-wrapper.d.ts.map +0 -1
- package/dist/core/graph-wrapper.js +0 -299
- package/dist/core/index.d.ts +0 -48
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js +0 -89
- package/dist/core/intelligence-engine.d.ts +0 -258
- package/dist/core/intelligence-engine.d.ts.map +0 -1
- package/dist/core/intelligence-engine.js +0 -1030
- package/dist/core/learning-engine.d.ts +0 -160
- package/dist/core/learning-engine.d.ts.map +0 -1
- package/dist/core/learning-engine.js +0 -589
- package/dist/core/neural-embeddings.d.ts +0 -393
- package/dist/core/neural-embeddings.d.ts.map +0 -1
- package/dist/core/neural-embeddings.js +0 -1091
- package/dist/core/neural-perf.d.ts +0 -331
- package/dist/core/neural-perf.d.ts.map +0 -1
- package/dist/core/neural-perf.js +0 -704
- package/dist/core/onnx/loader.js +0 -348
- package/dist/core/onnx/pkg/LICENSE +0 -21
- package/dist/core/onnx/pkg/loader.js +0 -348
- package/dist/core/onnx/pkg/package.json +0 -3
- package/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm.d.ts +0 -112
- package/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm.js +0 -5
- package/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm_bg.js +0 -638
- package/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm_bg.wasm +0 -0
- package/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm_bg.wasm.d.ts +0 -29
- package/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm_cjs.js +0 -127
- package/dist/core/onnx-embedder.d.ts +0 -105
- package/dist/core/onnx-embedder.d.ts.map +0 -1
- package/dist/core/onnx-embedder.js +0 -410
- package/dist/core/onnx-llm.d.ts +0 -206
- package/dist/core/onnx-llm.d.ts.map +0 -1
- package/dist/core/onnx-llm.js +0 -430
- package/dist/core/onnx-optimized.d.ts +0 -109
- package/dist/core/onnx-optimized.d.ts.map +0 -1
- package/dist/core/onnx-optimized.js +0 -419
- package/dist/core/parallel-intelligence.d.ts +0 -109
- package/dist/core/parallel-intelligence.d.ts.map +0 -1
- package/dist/core/parallel-intelligence.js +0 -340
- package/dist/core/parallel-workers.d.ts +0 -177
- package/dist/core/parallel-workers.d.ts.map +0 -1
- package/dist/core/parallel-workers.js +0 -671
- package/dist/core/router-wrapper.d.ts +0 -62
- package/dist/core/router-wrapper.d.ts.map +0 -1
- package/dist/core/router-wrapper.js +0 -209
- package/dist/core/rvf-wrapper.d.ts +0 -86
- package/dist/core/rvf-wrapper.d.ts.map +0 -1
- package/dist/core/rvf-wrapper.js +0 -102
- package/dist/core/sona-wrapper.d.ts +0 -226
- package/dist/core/sona-wrapper.d.ts.map +0 -1
- package/dist/core/sona-wrapper.js +0 -282
- package/dist/core/tensor-compress.d.ts +0 -134
- package/dist/core/tensor-compress.d.ts.map +0 -1
- package/dist/core/tensor-compress.js +0 -432
- package/dist/index.d.ts +0 -105
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -221
- package/dist/services/embedding-service.d.ts +0 -136
- package/dist/services/embedding-service.d.ts.map +0 -1
- package/dist/services/embedding-service.js +0 -294
- package/dist/services/index.d.ts +0 -6
- package/dist/services/index.d.ts.map +0 -1
- package/dist/services/index.js +0 -26
- package/dist/types.d.ts +0 -145
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/workers/benchmark.d.ts +0 -44
- package/dist/workers/benchmark.d.ts.map +0 -1
- package/dist/workers/benchmark.js +0 -230
- package/dist/workers/index.d.ts +0 -10
- package/dist/workers/index.d.ts.map +0 -1
- package/dist/workers/index.js +0 -25
- package/dist/workers/native-worker.d.ts +0 -76
- package/dist/workers/native-worker.d.ts.map +0 -1
- package/dist/workers/native-worker.js +0 -490
- package/dist/workers/types.d.ts +0 -69
- package/dist/workers/types.d.ts.map +0 -1
- package/dist/workers/types.js +0 -7
|
@@ -1,1091 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Neural Embedding System - Frontier Embedding Intelligence
|
|
4
|
-
*
|
|
5
|
-
* Implements late-2025 research concepts treating embeddings as:
|
|
6
|
-
* 1. CONTROL SIGNALS - Semantic drift detection, reflex triggers
|
|
7
|
-
* 2. MEMORY PHYSICS - Forgetting curves, interference, consolidation
|
|
8
|
-
* 3. PROGRAM STATE - Agent state management via geometry
|
|
9
|
-
* 4. COORDINATION PRIMITIVES - Multi-agent swarm alignment
|
|
10
|
-
* 5. SAFETY MONITORS - Coherence detection, misalignment alerts
|
|
11
|
-
* 6. NEURAL SUBSTRATE - Synthetic nervous system layer
|
|
12
|
-
*
|
|
13
|
-
* Based on:
|
|
14
|
-
* - TinyTE (EMNLP 2025): Embedding-layer steering
|
|
15
|
-
* - DoRA (ICML 2024): Magnitude-direction decomposition
|
|
16
|
-
* - S-LoRA/Punica: Multi-adapter serving patterns
|
|
17
|
-
* - MMTEB: Multilingual embedding benchmarks
|
|
18
|
-
*/
|
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.NeuralSubstrate = exports.CoherenceMonitor = exports.SwarmCoordinator = exports.EmbeddingStateMachine = exports.MemoryPhysics = exports.SemanticDriftDetector = exports.silentLogger = exports.defaultLogger = exports.NEURAL_CONSTANTS = void 0;
|
|
21
|
-
// ============================================================================
|
|
22
|
-
// Constants - Replace magic numbers with named constants
|
|
23
|
-
// ============================================================================
|
|
24
|
-
exports.NEURAL_CONSTANTS = {
|
|
25
|
-
// Drift Detection
|
|
26
|
-
MAX_DRIFT_EVENTS: 1000,
|
|
27
|
-
MAX_HISTORY_SIZE: 500,
|
|
28
|
-
DEFAULT_DRIFT_THRESHOLD: 0.15,
|
|
29
|
-
DEFAULT_DRIFT_WINDOW_MS: 60000,
|
|
30
|
-
DRIFT_CRITICAL_MULTIPLIER: 2,
|
|
31
|
-
VELOCITY_WINDOW_SIZE: 10,
|
|
32
|
-
// Memory Physics
|
|
33
|
-
MAX_MEMORIES: 10000,
|
|
34
|
-
MAX_CONTENT_LENGTH: 10000,
|
|
35
|
-
MAX_ID_LENGTH: 256,
|
|
36
|
-
DEFAULT_MEMORY_DECAY_RATE: 0.01,
|
|
37
|
-
DEFAULT_INTERFERENCE_THRESHOLD: 0.8,
|
|
38
|
-
DEFAULT_CONSOLIDATION_RATE: 0.1,
|
|
39
|
-
MEMORY_FORGET_THRESHOLD: 0.01,
|
|
40
|
-
CONSOLIDATION_SCORE_THRESHOLD: 0.5,
|
|
41
|
-
MEMORY_CLEANUP_PERCENT: 0.1,
|
|
42
|
-
RECALL_STRENGTH_BOOST: 0.1,
|
|
43
|
-
MAX_TIME_JUMP_MINUTES: 1440,
|
|
44
|
-
// Agent State
|
|
45
|
-
MAX_AGENTS: 1000,
|
|
46
|
-
MAX_SPECIALTY_LENGTH: 100,
|
|
47
|
-
AGENT_TIMEOUT_MS: 3600000, // 1 hour
|
|
48
|
-
DEFAULT_AGENT_ENERGY: 1.0,
|
|
49
|
-
TRAJECTORY_DAMPING: 0.1,
|
|
50
|
-
MAX_TRAJECTORY_STEPS: 100,
|
|
51
|
-
// Swarm Coordination
|
|
52
|
-
MAX_CLUSTER_AGENTS: 500,
|
|
53
|
-
DEFAULT_CLUSTER_THRESHOLD: 0.7,
|
|
54
|
-
// Coherence Monitoring
|
|
55
|
-
DEFAULT_WINDOW_SIZE: 100,
|
|
56
|
-
MIN_CALIBRATION_OBSERVATIONS: 10,
|
|
57
|
-
STABILITY_WINDOW_SIZE: 10,
|
|
58
|
-
ALIGNMENT_WINDOW_SIZE: 50,
|
|
59
|
-
RECENT_OBSERVATIONS_SIZE: 20,
|
|
60
|
-
DRIFT_WARNING_THRESHOLD: 0.3,
|
|
61
|
-
STABILITY_WARNING_THRESHOLD: 0.5,
|
|
62
|
-
ALIGNMENT_WARNING_THRESHOLD: 0.6,
|
|
63
|
-
COHERENCE_WARNING_THRESHOLD: 0.5,
|
|
64
|
-
// Math
|
|
65
|
-
EPSILON: 1e-8,
|
|
66
|
-
ZERO_VECTOR_THRESHOLD: 1e-10,
|
|
67
|
-
// Defaults
|
|
68
|
-
DEFAULT_DIMENSION: 384,
|
|
69
|
-
DEFAULT_REFLEX_LATENCY_MS: 10,
|
|
70
|
-
};
|
|
71
|
-
/** Default console logger */
|
|
72
|
-
exports.defaultLogger = {
|
|
73
|
-
log(level, message, data) {
|
|
74
|
-
const prefix = `[Neural:${level.toUpperCase()}]`;
|
|
75
|
-
if (data) {
|
|
76
|
-
console[level === 'debug' ? 'log' : level](`${prefix} ${message}`, data);
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
console[level === 'debug' ? 'log' : level](`${prefix} ${message}`);
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
/** Silent logger for suppressing output */
|
|
84
|
-
exports.silentLogger = {
|
|
85
|
-
log() { },
|
|
86
|
-
};
|
|
87
|
-
// ============================================================================
|
|
88
|
-
// 1. SEMANTIC DRIFT DETECTOR - Embeddings as Control Signals
|
|
89
|
-
// ============================================================================
|
|
90
|
-
/**
|
|
91
|
-
* Detects semantic drift and triggers reflexes based on embedding movement.
|
|
92
|
-
* Instead of asking "what is similar", asks "how far did we move".
|
|
93
|
-
*/
|
|
94
|
-
class SemanticDriftDetector {
|
|
95
|
-
constructor(config = {}) {
|
|
96
|
-
this.baseline = null;
|
|
97
|
-
this.history = [];
|
|
98
|
-
this.driftEvents = [];
|
|
99
|
-
// Reflex callbacks
|
|
100
|
-
this.reflexes = new Map();
|
|
101
|
-
this.config = {
|
|
102
|
-
dimension: config.dimension ?? exports.NEURAL_CONSTANTS.DEFAULT_DIMENSION,
|
|
103
|
-
driftThreshold: config.driftThreshold ?? exports.NEURAL_CONSTANTS.DEFAULT_DRIFT_THRESHOLD,
|
|
104
|
-
driftWindowMs: config.driftWindowMs ?? exports.NEURAL_CONSTANTS.DEFAULT_DRIFT_WINDOW_MS,
|
|
105
|
-
};
|
|
106
|
-
this.logger = config.logger ?? exports.defaultLogger;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Set the baseline embedding (reference point)
|
|
110
|
-
*/
|
|
111
|
-
setBaseline(embedding) {
|
|
112
|
-
this.baseline = embedding instanceof Float32Array
|
|
113
|
-
? new Float32Array(embedding)
|
|
114
|
-
: new Float32Array(embedding);
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Observe a new embedding and detect drift
|
|
118
|
-
*/
|
|
119
|
-
observe(embedding, source) {
|
|
120
|
-
const emb = embedding instanceof Float32Array
|
|
121
|
-
? embedding
|
|
122
|
-
: new Float32Array(embedding);
|
|
123
|
-
const now = Date.now();
|
|
124
|
-
// Add to history
|
|
125
|
-
this.history.push({ embedding: new Float32Array(emb), timestamp: now });
|
|
126
|
-
// Prune old history (with size limit)
|
|
127
|
-
const cutoff = now - this.config.driftWindowMs;
|
|
128
|
-
this.history = this.history.filter(h => h.timestamp > cutoff);
|
|
129
|
-
// Security: Enforce maximum history size
|
|
130
|
-
if (this.history.length > exports.NEURAL_CONSTANTS.MAX_HISTORY_SIZE) {
|
|
131
|
-
this.history = this.history.slice(-exports.NEURAL_CONSTANTS.MAX_HISTORY_SIZE);
|
|
132
|
-
}
|
|
133
|
-
// If no baseline, set first observation as baseline
|
|
134
|
-
if (!this.baseline) {
|
|
135
|
-
this.baseline = new Float32Array(emb);
|
|
136
|
-
return null;
|
|
137
|
-
}
|
|
138
|
-
// Calculate drift from baseline
|
|
139
|
-
const drift = this.calculateDrift(emb, this.baseline);
|
|
140
|
-
// Determine category
|
|
141
|
-
let category = 'normal';
|
|
142
|
-
if (drift.magnitude > this.config.driftThreshold * exports.NEURAL_CONSTANTS.DRIFT_CRITICAL_MULTIPLIER) {
|
|
143
|
-
category = 'critical';
|
|
144
|
-
}
|
|
145
|
-
else if (drift.magnitude > this.config.driftThreshold) {
|
|
146
|
-
category = 'warning';
|
|
147
|
-
}
|
|
148
|
-
const event = {
|
|
149
|
-
timestamp: now,
|
|
150
|
-
magnitude: drift.magnitude,
|
|
151
|
-
direction: drift.direction,
|
|
152
|
-
category,
|
|
153
|
-
source,
|
|
154
|
-
};
|
|
155
|
-
// Record event if significant (with size limit)
|
|
156
|
-
if (category !== 'normal') {
|
|
157
|
-
this.driftEvents.push(event);
|
|
158
|
-
// Security: Prevent unbounded growth
|
|
159
|
-
if (this.driftEvents.length > exports.NEURAL_CONSTANTS.MAX_DRIFT_EVENTS) {
|
|
160
|
-
this.driftEvents = this.driftEvents.slice(-exports.NEURAL_CONSTANTS.MAX_DRIFT_EVENTS);
|
|
161
|
-
}
|
|
162
|
-
this.triggerReflexes(event);
|
|
163
|
-
}
|
|
164
|
-
return event;
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Calculate drift between two embeddings
|
|
168
|
-
*/
|
|
169
|
-
calculateDrift(current, reference) {
|
|
170
|
-
const direction = new Float32Array(current.length);
|
|
171
|
-
let magnitudeSq = 0;
|
|
172
|
-
for (let i = 0; i < current.length; i++) {
|
|
173
|
-
const diff = current[i] - reference[i];
|
|
174
|
-
direction[i] = diff;
|
|
175
|
-
magnitudeSq += diff * diff;
|
|
176
|
-
}
|
|
177
|
-
const magnitude = Math.sqrt(magnitudeSq);
|
|
178
|
-
// Normalize direction
|
|
179
|
-
if (magnitude > 0) {
|
|
180
|
-
for (let i = 0; i < direction.length; i++) {
|
|
181
|
-
direction[i] /= magnitude;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
return { magnitude, direction };
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Register a reflex callback for drift events
|
|
188
|
-
*/
|
|
189
|
-
registerReflex(name, callback) {
|
|
190
|
-
this.reflexes.set(name, callback);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Trigger registered reflexes
|
|
194
|
-
*/
|
|
195
|
-
triggerReflexes(event) {
|
|
196
|
-
const errors = [];
|
|
197
|
-
for (const [name, callback] of this.reflexes) {
|
|
198
|
-
try {
|
|
199
|
-
callback(event);
|
|
200
|
-
}
|
|
201
|
-
catch (e) {
|
|
202
|
-
// Security: Track reflex failures but don't break execution
|
|
203
|
-
errors.push({ reflex: name, error: e });
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
// Security: Warn if multiple reflexes fail (potential attack or system issue)
|
|
207
|
-
if (errors.length > 0 && errors.length >= this.reflexes.size / 2) {
|
|
208
|
-
this.logger.log('warn', `${errors.length}/${this.reflexes.size} reflexes failed`, {
|
|
209
|
-
failedReflexes: errors.map(e => e.reflex),
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Get recent drift velocity (rate of change)
|
|
215
|
-
*/
|
|
216
|
-
getVelocity() {
|
|
217
|
-
if (this.history.length < 2)
|
|
218
|
-
return 0;
|
|
219
|
-
const recent = this.history.slice(-exports.NEURAL_CONSTANTS.VELOCITY_WINDOW_SIZE);
|
|
220
|
-
if (recent.length < 2)
|
|
221
|
-
return 0;
|
|
222
|
-
let totalDrift = 0;
|
|
223
|
-
for (let i = 1; i < recent.length; i++) {
|
|
224
|
-
const drift = this.calculateDrift(recent[i].embedding, recent[i - 1].embedding);
|
|
225
|
-
totalDrift += drift.magnitude;
|
|
226
|
-
}
|
|
227
|
-
const timeSpan = recent[recent.length - 1].timestamp - recent[0].timestamp;
|
|
228
|
-
return timeSpan > 0 ? totalDrift / timeSpan * 1000 : 0; // drift per second
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Get drift statistics
|
|
232
|
-
*/
|
|
233
|
-
getStats() {
|
|
234
|
-
const currentDrift = this.history.length > 0 && this.baseline
|
|
235
|
-
? this.calculateDrift(this.history[this.history.length - 1].embedding, this.baseline).magnitude
|
|
236
|
-
: 0;
|
|
237
|
-
return {
|
|
238
|
-
currentDrift,
|
|
239
|
-
velocity: this.getVelocity(),
|
|
240
|
-
criticalEvents: this.driftEvents.filter(e => e.category === 'critical').length,
|
|
241
|
-
warningEvents: this.driftEvents.filter(e => e.category === 'warning').length,
|
|
242
|
-
historySize: this.history.length,
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Reset baseline to current position
|
|
247
|
-
*/
|
|
248
|
-
recenter() {
|
|
249
|
-
if (this.history.length > 0) {
|
|
250
|
-
this.baseline = new Float32Array(this.history[this.history.length - 1].embedding);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
exports.SemanticDriftDetector = SemanticDriftDetector;
|
|
255
|
-
// ============================================================================
|
|
256
|
-
// 2. MEMORY PHYSICS - Forgetting, Interference, Consolidation
|
|
257
|
-
// ============================================================================
|
|
258
|
-
/**
|
|
259
|
-
* Implements hippocampal-like memory dynamics in embedding space.
|
|
260
|
-
* Memory strength decays, similar memories interfere, consolidation strengthens.
|
|
261
|
-
*/
|
|
262
|
-
class MemoryPhysics {
|
|
263
|
-
constructor(config = {}) {
|
|
264
|
-
this.memories = new Map();
|
|
265
|
-
this.lastUpdate = Date.now();
|
|
266
|
-
this.config = {
|
|
267
|
-
dimension: config.dimension ?? exports.NEURAL_CONSTANTS.DEFAULT_DIMENSION,
|
|
268
|
-
memoryDecayRate: config.memoryDecayRate ?? exports.NEURAL_CONSTANTS.DEFAULT_MEMORY_DECAY_RATE,
|
|
269
|
-
interferenceThreshold: config.interferenceThreshold ?? exports.NEURAL_CONSTANTS.DEFAULT_INTERFERENCE_THRESHOLD,
|
|
270
|
-
consolidationRate: config.consolidationRate ?? exports.NEURAL_CONSTANTS.DEFAULT_CONSOLIDATION_RATE,
|
|
271
|
-
};
|
|
272
|
-
this.logger = config.logger ?? exports.defaultLogger;
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Encode a new memory
|
|
276
|
-
*/
|
|
277
|
-
encode(id, embedding, content) {
|
|
278
|
-
// Security: Validate inputs
|
|
279
|
-
if (typeof id !== 'string' || id.length === 0 || id.length > exports.NEURAL_CONSTANTS.MAX_ID_LENGTH) {
|
|
280
|
-
throw new Error(`Invalid memory ID: must be string of 1-${exports.NEURAL_CONSTANTS.MAX_ID_LENGTH} characters`);
|
|
281
|
-
}
|
|
282
|
-
if (typeof content !== 'string' || content.length > exports.NEURAL_CONSTANTS.MAX_CONTENT_LENGTH) {
|
|
283
|
-
throw new Error(`Content exceeds maximum length: ${exports.NEURAL_CONSTANTS.MAX_CONTENT_LENGTH}`);
|
|
284
|
-
}
|
|
285
|
-
if (this.memories.size >= exports.NEURAL_CONSTANTS.MAX_MEMORIES && !this.memories.has(id)) {
|
|
286
|
-
// Force cleanup of weak memories before adding new one
|
|
287
|
-
this.forceCleanup();
|
|
288
|
-
}
|
|
289
|
-
const emb = embedding instanceof Float32Array
|
|
290
|
-
? new Float32Array(embedding)
|
|
291
|
-
: new Float32Array(embedding);
|
|
292
|
-
// Security: Validate embedding dimension
|
|
293
|
-
if (emb.length !== this.config.dimension) {
|
|
294
|
-
throw new Error(`Embedding dimension mismatch: expected ${this.config.dimension}, got ${emb.length}`);
|
|
295
|
-
}
|
|
296
|
-
const now = Date.now();
|
|
297
|
-
// Check for interference with existing memories
|
|
298
|
-
let interference = 0;
|
|
299
|
-
for (const existing of this.memories.values()) {
|
|
300
|
-
const similarity = this.cosineSimilarity(emb, existing.embedding);
|
|
301
|
-
if (similarity > this.config.interferenceThreshold) {
|
|
302
|
-
interference += similarity - this.config.interferenceThreshold;
|
|
303
|
-
existing.interference += (similarity - this.config.interferenceThreshold) * 0.5;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
const entry = {
|
|
307
|
-
id,
|
|
308
|
-
embedding: emb,
|
|
309
|
-
content,
|
|
310
|
-
strength: 1.0 - interference * 0.3, // New memories weaker if interfered
|
|
311
|
-
lastAccess: now,
|
|
312
|
-
accessCount: 1,
|
|
313
|
-
consolidationLevel: 0,
|
|
314
|
-
interference,
|
|
315
|
-
};
|
|
316
|
-
this.memories.set(id, entry);
|
|
317
|
-
return entry;
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Recall memories similar to a query (strengthens accessed memories)
|
|
321
|
-
*/
|
|
322
|
-
recall(query, k = 5) {
|
|
323
|
-
const q = query instanceof Float32Array ? query : new Float32Array(query);
|
|
324
|
-
const now = Date.now();
|
|
325
|
-
// Apply decay before recall
|
|
326
|
-
this.applyDecay();
|
|
327
|
-
// Score memories
|
|
328
|
-
const scored = [];
|
|
329
|
-
for (const entry of this.memories.values()) {
|
|
330
|
-
const similarity = this.cosineSimilarity(q, entry.embedding);
|
|
331
|
-
// Effective score combines similarity and strength
|
|
332
|
-
const score = similarity * Math.sqrt(entry.strength);
|
|
333
|
-
scored.push({ entry, score });
|
|
334
|
-
}
|
|
335
|
-
// Sort and get top-k
|
|
336
|
-
scored.sort((a, b) => b.score - a.score);
|
|
337
|
-
const results = scored.slice(0, k).map(s => s.entry);
|
|
338
|
-
// Strengthen recalled memories (retrieval practice effect)
|
|
339
|
-
for (const entry of results) {
|
|
340
|
-
entry.lastAccess = now;
|
|
341
|
-
entry.accessCount++;
|
|
342
|
-
entry.strength = Math.min(1.0, entry.strength + exports.NEURAL_CONSTANTS.RECALL_STRENGTH_BOOST);
|
|
343
|
-
}
|
|
344
|
-
return results;
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Apply time-based decay to all memories
|
|
348
|
-
*/
|
|
349
|
-
applyDecay() {
|
|
350
|
-
const now = Date.now();
|
|
351
|
-
const elapsed = Math.max(0, now - this.lastUpdate) / 60000; // minutes, prevent negative
|
|
352
|
-
// Security: Cap maximum elapsed time to prevent manipulation
|
|
353
|
-
const cappedElapsed = Math.min(elapsed, exports.NEURAL_CONSTANTS.MAX_TIME_JUMP_MINUTES);
|
|
354
|
-
if (elapsed > exports.NEURAL_CONSTANTS.MAX_TIME_JUMP_MINUTES) {
|
|
355
|
-
this.logger.log('warn', `Large time jump detected: ${elapsed.toFixed(0)} minutes`);
|
|
356
|
-
}
|
|
357
|
-
this.lastUpdate = now;
|
|
358
|
-
const decayFactor = Math.exp(-this.config.memoryDecayRate * cappedElapsed);
|
|
359
|
-
for (const entry of this.memories.values()) {
|
|
360
|
-
// Decay is slower for consolidated memories
|
|
361
|
-
const effectiveDecay = decayFactor + entry.consolidationLevel * (1 - decayFactor) * 0.8;
|
|
362
|
-
entry.strength = Math.max(0, entry.strength * effectiveDecay);
|
|
363
|
-
// Very weak memories are forgotten
|
|
364
|
-
if (entry.strength < exports.NEURAL_CONSTANTS.MEMORY_FORGET_THRESHOLD) {
|
|
365
|
-
this.memories.delete(entry.id);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Consolidate memories (like sleep consolidation)
|
|
371
|
-
* Strengthens frequently accessed, weakly interfered memories
|
|
372
|
-
*/
|
|
373
|
-
consolidate() {
|
|
374
|
-
let consolidated = 0;
|
|
375
|
-
let forgotten = 0;
|
|
376
|
-
for (const entry of this.memories.values()) {
|
|
377
|
-
// Consolidation score based on access pattern and low interference
|
|
378
|
-
const consolidationScore = Math.log(entry.accessCount + 1) * entry.strength * (1 - entry.interference * 0.5);
|
|
379
|
-
if (consolidationScore > exports.NEURAL_CONSTANTS.CONSOLIDATION_SCORE_THRESHOLD) {
|
|
380
|
-
entry.consolidationLevel = Math.min(1.0, entry.consolidationLevel + this.config.consolidationRate);
|
|
381
|
-
entry.strength = Math.min(1.0, entry.strength + 0.05);
|
|
382
|
-
consolidated++;
|
|
383
|
-
}
|
|
384
|
-
else if (entry.strength < exports.NEURAL_CONSTANTS.MEMORY_CLEANUP_PERCENT) {
|
|
385
|
-
this.memories.delete(entry.id);
|
|
386
|
-
forgotten++;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
return { consolidated, forgotten };
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* Get memory statistics
|
|
393
|
-
*/
|
|
394
|
-
getStats() {
|
|
395
|
-
if (this.memories.size === 0) {
|
|
396
|
-
return { totalMemories: 0, avgStrength: 0, avgConsolidation: 0, avgInterference: 0 };
|
|
397
|
-
}
|
|
398
|
-
let sumStrength = 0, sumConsolidation = 0, sumInterference = 0;
|
|
399
|
-
for (const entry of this.memories.values()) {
|
|
400
|
-
sumStrength += entry.strength;
|
|
401
|
-
sumConsolidation += entry.consolidationLevel;
|
|
402
|
-
sumInterference += entry.interference;
|
|
403
|
-
}
|
|
404
|
-
const n = this.memories.size;
|
|
405
|
-
return {
|
|
406
|
-
totalMemories: n,
|
|
407
|
-
avgStrength: sumStrength / n,
|
|
408
|
-
avgConsolidation: sumConsolidation / n,
|
|
409
|
-
avgInterference: sumInterference / n,
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
cosineSimilarity(a, b) {
|
|
413
|
-
let dot = 0, normA = 0, normB = 0;
|
|
414
|
-
for (let i = 0; i < a.length; i++) {
|
|
415
|
-
dot += a[i] * b[i];
|
|
416
|
-
normA += a[i] * a[i];
|
|
417
|
-
normB += b[i] * b[i];
|
|
418
|
-
}
|
|
419
|
-
const denom = Math.sqrt(normA * normB);
|
|
420
|
-
if (denom < 1e-10)
|
|
421
|
-
return 0; // Handle zero vectors
|
|
422
|
-
return Math.max(-1, Math.min(1, dot / denom)); // Clamp to valid range
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* Force cleanup of weak memories when limit reached
|
|
426
|
-
*/
|
|
427
|
-
forceCleanup() {
|
|
428
|
-
const entries = Array.from(this.memories.entries())
|
|
429
|
-
.sort((a, b) => a[1].strength - b[1].strength);
|
|
430
|
-
const removeCount = Math.ceil(this.memories.size * exports.NEURAL_CONSTANTS.MEMORY_CLEANUP_PERCENT);
|
|
431
|
-
for (let i = 0; i < removeCount; i++) {
|
|
432
|
-
this.memories.delete(entries[i][0]);
|
|
433
|
-
}
|
|
434
|
-
this.logger.log('debug', `Force cleanup removed ${removeCount} weak memories`);
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
exports.MemoryPhysics = MemoryPhysics;
|
|
438
|
-
// ============================================================================
|
|
439
|
-
// 3. EMBEDDING STATE MACHINE - Agent State via Geometry
|
|
440
|
-
// ============================================================================
|
|
441
|
-
/**
|
|
442
|
-
* Manages agent state as movement through embedding space.
|
|
443
|
-
* Decisions become geometric - no explicit state machine.
|
|
444
|
-
*/
|
|
445
|
-
class EmbeddingStateMachine {
|
|
446
|
-
constructor(config = {}) {
|
|
447
|
-
this.agents = new Map();
|
|
448
|
-
this.modeRegions = new Map();
|
|
449
|
-
this.lastCleanup = Date.now();
|
|
450
|
-
this.config = {
|
|
451
|
-
dimension: config.dimension ?? exports.NEURAL_CONSTANTS.DEFAULT_DIMENSION,
|
|
452
|
-
};
|
|
453
|
-
this.logger = config.logger ?? exports.defaultLogger;
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Create or update an agent
|
|
457
|
-
*/
|
|
458
|
-
updateAgent(id, embedding) {
|
|
459
|
-
// Periodically clean up stale agents
|
|
460
|
-
this.cleanupStaleAgents();
|
|
461
|
-
// Security: Enforce agent limit
|
|
462
|
-
if (!this.agents.has(id) && this.agents.size >= exports.NEURAL_CONSTANTS.MAX_AGENTS) {
|
|
463
|
-
throw new Error(`Agent limit reached: ${exports.NEURAL_CONSTANTS.MAX_AGENTS}`);
|
|
464
|
-
}
|
|
465
|
-
const position = embedding instanceof Float32Array
|
|
466
|
-
? new Float32Array(embedding)
|
|
467
|
-
: new Float32Array(embedding);
|
|
468
|
-
const existing = this.agents.get(id);
|
|
469
|
-
const now = Date.now();
|
|
470
|
-
if (existing) {
|
|
471
|
-
// Calculate velocity (direction of movement)
|
|
472
|
-
for (let i = 0; i < position.length; i++) {
|
|
473
|
-
existing.velocity[i] = position[i] - existing.position[i];
|
|
474
|
-
}
|
|
475
|
-
existing.position = position;
|
|
476
|
-
existing.lastUpdate = now;
|
|
477
|
-
// Update mode based on nearest region
|
|
478
|
-
existing.mode = this.determineMode(position);
|
|
479
|
-
}
|
|
480
|
-
else {
|
|
481
|
-
// New agent
|
|
482
|
-
const state = {
|
|
483
|
-
id,
|
|
484
|
-
position,
|
|
485
|
-
velocity: new Float32Array(this.config.dimension),
|
|
486
|
-
attention: new Float32Array(this.config.dimension).fill(1 / this.config.dimension),
|
|
487
|
-
energy: exports.NEURAL_CONSTANTS.DEFAULT_AGENT_ENERGY,
|
|
488
|
-
mode: this.determineMode(position),
|
|
489
|
-
lastUpdate: now,
|
|
490
|
-
};
|
|
491
|
-
this.agents.set(id, state);
|
|
492
|
-
return state;
|
|
493
|
-
}
|
|
494
|
-
return existing;
|
|
495
|
-
}
|
|
496
|
-
/**
|
|
497
|
-
* Remove stale agents that haven't been updated recently
|
|
498
|
-
*/
|
|
499
|
-
cleanupStaleAgents() {
|
|
500
|
-
const now = Date.now();
|
|
501
|
-
// Only run cleanup every minute
|
|
502
|
-
if (now - this.lastCleanup < 60000)
|
|
503
|
-
return;
|
|
504
|
-
this.lastCleanup = now;
|
|
505
|
-
const cutoff = now - exports.NEURAL_CONSTANTS.AGENT_TIMEOUT_MS;
|
|
506
|
-
let removed = 0;
|
|
507
|
-
for (const [id, state] of this.agents) {
|
|
508
|
-
if (state.lastUpdate < cutoff) {
|
|
509
|
-
this.agents.delete(id);
|
|
510
|
-
removed++;
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
if (removed > 0) {
|
|
514
|
-
this.logger.log('debug', `Cleaned up ${removed} stale agents`);
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
/**
|
|
518
|
-
* Manually remove an agent
|
|
519
|
-
*/
|
|
520
|
-
removeAgent(id) {
|
|
521
|
-
return this.agents.delete(id);
|
|
522
|
-
}
|
|
523
|
-
/**
|
|
524
|
-
* Define a mode region in embedding space
|
|
525
|
-
*/
|
|
526
|
-
defineMode(name, centroid, radius = 0.3) {
|
|
527
|
-
const c = centroid instanceof Float32Array
|
|
528
|
-
? new Float32Array(centroid)
|
|
529
|
-
: new Float32Array(centroid);
|
|
530
|
-
this.modeRegions.set(name, { centroid: c, radius });
|
|
531
|
-
}
|
|
532
|
-
/**
|
|
533
|
-
* Determine which mode an agent is in based on position
|
|
534
|
-
*/
|
|
535
|
-
determineMode(position) {
|
|
536
|
-
let bestMode = 'unknown';
|
|
537
|
-
let bestScore = -Infinity;
|
|
538
|
-
for (const [name, region] of this.modeRegions) {
|
|
539
|
-
const distance = this.euclideanDistance(position, region.centroid);
|
|
540
|
-
const score = region.radius - distance;
|
|
541
|
-
if (score > bestScore) {
|
|
542
|
-
bestScore = score;
|
|
543
|
-
bestMode = name;
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
return bestScore > 0 ? bestMode : 'exploring';
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* Get agent trajectory prediction
|
|
550
|
-
*/
|
|
551
|
-
predictTrajectory(id, steps = 5) {
|
|
552
|
-
// Security: Limit trajectory steps
|
|
553
|
-
if (!Number.isInteger(steps) || steps < 1) {
|
|
554
|
-
throw new Error('Steps must be a positive integer');
|
|
555
|
-
}
|
|
556
|
-
const limitedSteps = Math.min(steps, exports.NEURAL_CONSTANTS.MAX_TRAJECTORY_STEPS);
|
|
557
|
-
const agent = this.agents.get(id);
|
|
558
|
-
if (!agent)
|
|
559
|
-
return [];
|
|
560
|
-
const trajectory = [];
|
|
561
|
-
let current = new Float32Array(agent.position);
|
|
562
|
-
for (let i = 0; i < limitedSteps; i++) {
|
|
563
|
-
const next = new Float32Array(current.length);
|
|
564
|
-
for (let j = 0; j < current.length; j++) {
|
|
565
|
-
next[j] = current[j] + agent.velocity[j] * (1 - i * exports.NEURAL_CONSTANTS.TRAJECTORY_DAMPING);
|
|
566
|
-
}
|
|
567
|
-
trajectory.push(next);
|
|
568
|
-
current = next;
|
|
569
|
-
}
|
|
570
|
-
return trajectory;
|
|
571
|
-
}
|
|
572
|
-
/**
|
|
573
|
-
* Apply attention to agent state
|
|
574
|
-
*/
|
|
575
|
-
attendTo(agentId, focusEmbedding) {
|
|
576
|
-
const agent = this.agents.get(agentId);
|
|
577
|
-
if (!agent)
|
|
578
|
-
return;
|
|
579
|
-
const focus = focusEmbedding instanceof Float32Array
|
|
580
|
-
? focusEmbedding
|
|
581
|
-
: new Float32Array(focusEmbedding);
|
|
582
|
-
// Update attention weights based on similarity to focus
|
|
583
|
-
let sum = 0;
|
|
584
|
-
for (let i = 0; i < agent.attention.length; i++) {
|
|
585
|
-
agent.attention[i] = Math.abs(focus[i]) + 0.01;
|
|
586
|
-
sum += agent.attention[i];
|
|
587
|
-
}
|
|
588
|
-
// Normalize
|
|
589
|
-
for (let i = 0; i < agent.attention.length; i++) {
|
|
590
|
-
agent.attention[i] /= sum;
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
/**
|
|
594
|
-
* Get all agents in a specific mode
|
|
595
|
-
*/
|
|
596
|
-
getAgentsInMode(mode) {
|
|
597
|
-
return Array.from(this.agents.values()).filter(a => a.mode === mode);
|
|
598
|
-
}
|
|
599
|
-
euclideanDistance(a, b) {
|
|
600
|
-
let sum = 0;
|
|
601
|
-
for (let i = 0; i < a.length; i++) {
|
|
602
|
-
const diff = a[i] - b[i];
|
|
603
|
-
sum += diff * diff;
|
|
604
|
-
}
|
|
605
|
-
return Math.sqrt(sum);
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
exports.EmbeddingStateMachine = EmbeddingStateMachine;
|
|
609
|
-
// ============================================================================
|
|
610
|
-
// 4. SWARM COORDINATOR - Multi-Agent Coordination via Embeddings
|
|
611
|
-
// ============================================================================
|
|
612
|
-
/**
|
|
613
|
-
* Enables multi-agent coordination through shared embedding space.
|
|
614
|
-
* Swarm behavior emerges from geometry, not protocol.
|
|
615
|
-
*/
|
|
616
|
-
class SwarmCoordinator {
|
|
617
|
-
constructor(config = {}) {
|
|
618
|
-
this.agents = new Map();
|
|
619
|
-
this.config = { dimension: config.dimension ?? exports.NEURAL_CONSTANTS.DEFAULT_DIMENSION };
|
|
620
|
-
this.sharedContext = new Float32Array(this.config.dimension);
|
|
621
|
-
this.logger = config.logger ?? exports.defaultLogger;
|
|
622
|
-
}
|
|
623
|
-
/**
|
|
624
|
-
* Register an agent with the swarm
|
|
625
|
-
*/
|
|
626
|
-
register(id, embedding, specialty = 'general') {
|
|
627
|
-
// Security: Validate inputs
|
|
628
|
-
if (typeof id !== 'string' || id.length === 0 || id.length > exports.NEURAL_CONSTANTS.MAX_ID_LENGTH) {
|
|
629
|
-
throw new Error(`Invalid agent ID: must be string of 1-${exports.NEURAL_CONSTANTS.MAX_ID_LENGTH} characters`);
|
|
630
|
-
}
|
|
631
|
-
if (typeof specialty !== 'string' || specialty.length > exports.NEURAL_CONSTANTS.MAX_SPECIALTY_LENGTH) {
|
|
632
|
-
throw new Error(`Specialty exceeds maximum length: ${exports.NEURAL_CONSTANTS.MAX_SPECIALTY_LENGTH}`);
|
|
633
|
-
}
|
|
634
|
-
if (this.agents.size >= exports.NEURAL_CONSTANTS.MAX_AGENTS && !this.agents.has(id)) {
|
|
635
|
-
throw new Error(`Agent limit reached: ${exports.NEURAL_CONSTANTS.MAX_AGENTS}`);
|
|
636
|
-
}
|
|
637
|
-
const position = embedding instanceof Float32Array
|
|
638
|
-
? new Float32Array(embedding)
|
|
639
|
-
: new Float32Array(embedding);
|
|
640
|
-
// Security: Validate embedding dimension
|
|
641
|
-
if (position.length !== this.config.dimension) {
|
|
642
|
-
throw new Error(`Embedding dimension mismatch: expected ${this.config.dimension}, got ${position.length}`);
|
|
643
|
-
}
|
|
644
|
-
this.agents.set(id, {
|
|
645
|
-
position,
|
|
646
|
-
velocity: new Float32Array(this.config.dimension),
|
|
647
|
-
lastUpdate: Date.now(),
|
|
648
|
-
specialty,
|
|
649
|
-
});
|
|
650
|
-
this.updateSharedContext();
|
|
651
|
-
}
|
|
652
|
-
/**
|
|
653
|
-
* Update agent position (from their work/observations)
|
|
654
|
-
*/
|
|
655
|
-
update(id, embedding) {
|
|
656
|
-
const agent = this.agents.get(id);
|
|
657
|
-
if (!agent)
|
|
658
|
-
return;
|
|
659
|
-
const newPosition = embedding instanceof Float32Array
|
|
660
|
-
? embedding
|
|
661
|
-
: new Float32Array(embedding);
|
|
662
|
-
// Calculate velocity
|
|
663
|
-
for (let i = 0; i < agent.position.length; i++) {
|
|
664
|
-
agent.velocity[i] = newPosition[i] - agent.position[i];
|
|
665
|
-
agent.position[i] = newPosition[i];
|
|
666
|
-
}
|
|
667
|
-
agent.lastUpdate = Date.now();
|
|
668
|
-
this.updateSharedContext();
|
|
669
|
-
}
|
|
670
|
-
/**
|
|
671
|
-
* Update shared context (centroid of all agents)
|
|
672
|
-
*/
|
|
673
|
-
updateSharedContext() {
|
|
674
|
-
if (this.agents.size === 0)
|
|
675
|
-
return;
|
|
676
|
-
this.sharedContext.fill(0);
|
|
677
|
-
for (const agent of this.agents.values()) {
|
|
678
|
-
for (let i = 0; i < this.sharedContext.length; i++) {
|
|
679
|
-
this.sharedContext[i] += agent.position[i];
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
for (let i = 0; i < this.sharedContext.length; i++) {
|
|
683
|
-
this.sharedContext[i] /= this.agents.size;
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
/**
|
|
687
|
-
* Get coordination signal for an agent (how to align with swarm)
|
|
688
|
-
*/
|
|
689
|
-
getCoordinationSignal(id) {
|
|
690
|
-
const agent = this.agents.get(id);
|
|
691
|
-
if (!agent)
|
|
692
|
-
return new Float32Array(this.config.dimension);
|
|
693
|
-
// Signal points toward shared context
|
|
694
|
-
const signal = new Float32Array(this.config.dimension);
|
|
695
|
-
for (let i = 0; i < signal.length; i++) {
|
|
696
|
-
signal[i] = this.sharedContext[i] - agent.position[i];
|
|
697
|
-
}
|
|
698
|
-
return signal;
|
|
699
|
-
}
|
|
700
|
-
/**
|
|
701
|
-
* Find agents working on similar things (for collaboration)
|
|
702
|
-
*/
|
|
703
|
-
findCollaborators(id, k = 3) {
|
|
704
|
-
const agent = this.agents.get(id);
|
|
705
|
-
if (!agent)
|
|
706
|
-
return [];
|
|
707
|
-
const scored = [];
|
|
708
|
-
for (const [otherId, other] of this.agents) {
|
|
709
|
-
if (otherId === id)
|
|
710
|
-
continue;
|
|
711
|
-
const similarity = this.cosineSimilarity(agent.position, other.position);
|
|
712
|
-
scored.push({ id: otherId, similarity, specialty: other.specialty });
|
|
713
|
-
}
|
|
714
|
-
scored.sort((a, b) => b.similarity - a.similarity);
|
|
715
|
-
return scored.slice(0, k);
|
|
716
|
-
}
|
|
717
|
-
/**
|
|
718
|
-
* Detect emergent clusters (specialization)
|
|
719
|
-
*/
|
|
720
|
-
detectClusters(threshold = exports.NEURAL_CONSTANTS.DEFAULT_CLUSTER_THRESHOLD) {
|
|
721
|
-
// Security: Validate threshold
|
|
722
|
-
if (threshold < 0 || threshold > 1) {
|
|
723
|
-
throw new Error('Threshold must be between 0 and 1');
|
|
724
|
-
}
|
|
725
|
-
// Security: Limit clustering for performance (O(n²) algorithm)
|
|
726
|
-
if (this.agents.size > exports.NEURAL_CONSTANTS.MAX_CLUSTER_AGENTS) {
|
|
727
|
-
this.logger.log('warn', `Too many agents for clustering: ${this.agents.size} > ${exports.NEURAL_CONSTANTS.MAX_CLUSTER_AGENTS}`);
|
|
728
|
-
// Return single cluster with all agents
|
|
729
|
-
return new Map([['all', Array.from(this.agents.keys())]]);
|
|
730
|
-
}
|
|
731
|
-
const clusters = new Map();
|
|
732
|
-
const assigned = new Set();
|
|
733
|
-
for (const [id, agent] of this.agents) {
|
|
734
|
-
if (assigned.has(id))
|
|
735
|
-
continue;
|
|
736
|
-
const cluster = [id];
|
|
737
|
-
assigned.add(id);
|
|
738
|
-
for (const [otherId, other] of this.agents) {
|
|
739
|
-
if (assigned.has(otherId))
|
|
740
|
-
continue;
|
|
741
|
-
const similarity = this.cosineSimilarity(agent.position, other.position);
|
|
742
|
-
if (similarity > threshold) {
|
|
743
|
-
cluster.push(otherId);
|
|
744
|
-
assigned.add(otherId);
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
clusters.set(id, cluster);
|
|
748
|
-
}
|
|
749
|
-
return clusters;
|
|
750
|
-
}
|
|
751
|
-
/**
|
|
752
|
-
* Get swarm coherence (how aligned are agents)
|
|
753
|
-
*/
|
|
754
|
-
getCoherence() {
|
|
755
|
-
if (this.agents.size < 2)
|
|
756
|
-
return 1.0;
|
|
757
|
-
let totalSimilarity = 0;
|
|
758
|
-
let pairs = 0;
|
|
759
|
-
const agentList = Array.from(this.agents.values());
|
|
760
|
-
for (let i = 0; i < agentList.length; i++) {
|
|
761
|
-
for (let j = i + 1; j < agentList.length; j++) {
|
|
762
|
-
totalSimilarity += this.cosineSimilarity(agentList[i].position, agentList[j].position);
|
|
763
|
-
pairs++;
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
return pairs > 0 ? totalSimilarity / pairs : 1.0;
|
|
767
|
-
}
|
|
768
|
-
cosineSimilarity(a, b) {
|
|
769
|
-
let dot = 0, normA = 0, normB = 0;
|
|
770
|
-
for (let i = 0; i < a.length; i++) {
|
|
771
|
-
dot += a[i] * b[i];
|
|
772
|
-
normA += a[i] * a[i];
|
|
773
|
-
normB += b[i] * b[i];
|
|
774
|
-
}
|
|
775
|
-
const denom = Math.sqrt(normA * normB);
|
|
776
|
-
if (denom < exports.NEURAL_CONSTANTS.ZERO_VECTOR_THRESHOLD)
|
|
777
|
-
return 0;
|
|
778
|
-
return Math.max(-1, Math.min(1, dot / denom));
|
|
779
|
-
}
|
|
780
|
-
/**
|
|
781
|
-
* Remove an agent from the swarm
|
|
782
|
-
*/
|
|
783
|
-
removeAgent(id) {
|
|
784
|
-
const removed = this.agents.delete(id);
|
|
785
|
-
if (removed) {
|
|
786
|
-
this.updateSharedContext();
|
|
787
|
-
}
|
|
788
|
-
return removed;
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
exports.SwarmCoordinator = SwarmCoordinator;
|
|
792
|
-
// ============================================================================
|
|
793
|
-
// 5. COHERENCE MONITOR - Safety and Alignment Detection
|
|
794
|
-
// ============================================================================
|
|
795
|
-
/**
|
|
796
|
-
* Monitors system coherence via embedding patterns.
|
|
797
|
-
* Detects degradation, poisoning, misalignment before explicit failures.
|
|
798
|
-
*/
|
|
799
|
-
class CoherenceMonitor {
|
|
800
|
-
constructor(config = {}) {
|
|
801
|
-
this.history = [];
|
|
802
|
-
this.baselineDistribution = null;
|
|
803
|
-
this.config = {
|
|
804
|
-
dimension: config.dimension ?? exports.NEURAL_CONSTANTS.DEFAULT_DIMENSION,
|
|
805
|
-
windowSize: config.windowSize ?? exports.NEURAL_CONSTANTS.DEFAULT_WINDOW_SIZE,
|
|
806
|
-
};
|
|
807
|
-
this.logger = config.logger ?? exports.defaultLogger;
|
|
808
|
-
}
|
|
809
|
-
/**
|
|
810
|
-
* Record an observation
|
|
811
|
-
*/
|
|
812
|
-
observe(embedding, source = 'unknown') {
|
|
813
|
-
const emb = embedding instanceof Float32Array
|
|
814
|
-
? new Float32Array(embedding)
|
|
815
|
-
: new Float32Array(embedding);
|
|
816
|
-
this.history.push({
|
|
817
|
-
embedding: emb,
|
|
818
|
-
timestamp: Date.now(),
|
|
819
|
-
source,
|
|
820
|
-
});
|
|
821
|
-
// Keep window size
|
|
822
|
-
while (this.history.length > this.config.windowSize * 2) {
|
|
823
|
-
this.history.shift();
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
/**
|
|
827
|
-
* Establish baseline distribution
|
|
828
|
-
*/
|
|
829
|
-
calibrate() {
|
|
830
|
-
if (this.history.length < exports.NEURAL_CONSTANTS.MIN_CALIBRATION_OBSERVATIONS) {
|
|
831
|
-
throw new Error(`Need at least ${exports.NEURAL_CONSTANTS.MIN_CALIBRATION_OBSERVATIONS} observations to calibrate`);
|
|
832
|
-
}
|
|
833
|
-
const mean = new Float32Array(this.config.dimension);
|
|
834
|
-
const variance = new Float32Array(this.config.dimension);
|
|
835
|
-
// Calculate mean
|
|
836
|
-
for (const obs of this.history) {
|
|
837
|
-
for (let i = 0; i < mean.length; i++) {
|
|
838
|
-
mean[i] += obs.embedding[i];
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
for (let i = 0; i < mean.length; i++) {
|
|
842
|
-
mean[i] /= this.history.length;
|
|
843
|
-
}
|
|
844
|
-
// Calculate variance
|
|
845
|
-
for (const obs of this.history) {
|
|
846
|
-
for (let i = 0; i < variance.length; i++) {
|
|
847
|
-
const diff = obs.embedding[i] - mean[i];
|
|
848
|
-
variance[i] += diff * diff;
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
for (let i = 0; i < variance.length; i++) {
|
|
852
|
-
variance[i] /= this.history.length;
|
|
853
|
-
}
|
|
854
|
-
this.baselineDistribution = { mean, variance };
|
|
855
|
-
}
|
|
856
|
-
/**
|
|
857
|
-
* Generate coherence report
|
|
858
|
-
*/
|
|
859
|
-
report() {
|
|
860
|
-
const anomalies = [];
|
|
861
|
-
// Drift score: how much has distribution shifted
|
|
862
|
-
const driftScore = this.calculateDriftScore();
|
|
863
|
-
if (driftScore > exports.NEURAL_CONSTANTS.DRIFT_WARNING_THRESHOLD) {
|
|
864
|
-
anomalies.push({
|
|
865
|
-
type: 'distribution_drift',
|
|
866
|
-
severity: driftScore,
|
|
867
|
-
description: 'Embedding distribution has shifted significantly from baseline',
|
|
868
|
-
});
|
|
869
|
-
}
|
|
870
|
-
// Stability score: variance in recent observations
|
|
871
|
-
const stabilityScore = this.calculateStabilityScore();
|
|
872
|
-
if (stabilityScore < exports.NEURAL_CONSTANTS.STABILITY_WARNING_THRESHOLD) {
|
|
873
|
-
anomalies.push({
|
|
874
|
-
type: 'instability',
|
|
875
|
-
severity: 1 - stabilityScore,
|
|
876
|
-
description: 'High variance in recent embeddings suggests instability',
|
|
877
|
-
});
|
|
878
|
-
}
|
|
879
|
-
// Alignment score: consistency of embeddings from same source
|
|
880
|
-
const alignmentScore = this.calculateAlignmentScore();
|
|
881
|
-
if (alignmentScore < exports.NEURAL_CONSTANTS.ALIGNMENT_WARNING_THRESHOLD) {
|
|
882
|
-
anomalies.push({
|
|
883
|
-
type: 'misalignment',
|
|
884
|
-
severity: 1 - alignmentScore,
|
|
885
|
-
description: 'Embeddings from same source show inconsistent patterns',
|
|
886
|
-
});
|
|
887
|
-
}
|
|
888
|
-
// Overall score
|
|
889
|
-
const overallScore = ((1 - driftScore) * 0.3 +
|
|
890
|
-
stabilityScore * 0.3 +
|
|
891
|
-
alignmentScore * 0.4);
|
|
892
|
-
return {
|
|
893
|
-
timestamp: Date.now(),
|
|
894
|
-
overallScore,
|
|
895
|
-
driftScore,
|
|
896
|
-
stabilityScore,
|
|
897
|
-
alignmentScore,
|
|
898
|
-
anomalies,
|
|
899
|
-
};
|
|
900
|
-
}
|
|
901
|
-
calculateDriftScore() {
|
|
902
|
-
if (!this.baselineDistribution || this.history.length < exports.NEURAL_CONSTANTS.RECENT_OBSERVATIONS_SIZE)
|
|
903
|
-
return 0;
|
|
904
|
-
const recent = this.history.slice(-exports.NEURAL_CONSTANTS.RECENT_OBSERVATIONS_SIZE);
|
|
905
|
-
const recentMean = new Float32Array(this.config.dimension);
|
|
906
|
-
for (const obs of recent) {
|
|
907
|
-
for (let i = 0; i < recentMean.length; i++) {
|
|
908
|
-
recentMean[i] += obs.embedding[i];
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
for (let i = 0; i < recentMean.length; i++) {
|
|
912
|
-
recentMean[i] /= recent.length;
|
|
913
|
-
}
|
|
914
|
-
// Calculate distance between means
|
|
915
|
-
let distance = 0;
|
|
916
|
-
for (let i = 0; i < recentMean.length; i++) {
|
|
917
|
-
const diff = recentMean[i] - this.baselineDistribution.mean[i];
|
|
918
|
-
distance += diff * diff;
|
|
919
|
-
}
|
|
920
|
-
return Math.min(1, Math.sqrt(distance));
|
|
921
|
-
}
|
|
922
|
-
calculateStabilityScore() {
|
|
923
|
-
if (this.history.length < exports.NEURAL_CONSTANTS.STABILITY_WINDOW_SIZE)
|
|
924
|
-
return 1.0;
|
|
925
|
-
const recent = this.history.slice(-exports.NEURAL_CONSTANTS.STABILITY_WINDOW_SIZE);
|
|
926
|
-
let totalVariance = 0;
|
|
927
|
-
// Calculate pairwise distances
|
|
928
|
-
for (let i = 1; i < recent.length; i++) {
|
|
929
|
-
let distance = 0;
|
|
930
|
-
for (let j = 0; j < recent[i].embedding.length; j++) {
|
|
931
|
-
const diff = recent[i].embedding[j] - recent[i - 1].embedding[j];
|
|
932
|
-
distance += diff * diff;
|
|
933
|
-
}
|
|
934
|
-
totalVariance += Math.sqrt(distance);
|
|
935
|
-
}
|
|
936
|
-
const avgVariance = totalVariance / (recent.length - 1);
|
|
937
|
-
return Math.max(0, 1 - avgVariance * 2);
|
|
938
|
-
}
|
|
939
|
-
calculateAlignmentScore() {
|
|
940
|
-
// Group by source and check consistency
|
|
941
|
-
const bySource = new Map();
|
|
942
|
-
for (const obs of this.history.slice(-exports.NEURAL_CONSTANTS.ALIGNMENT_WINDOW_SIZE)) {
|
|
943
|
-
if (!bySource.has(obs.source)) {
|
|
944
|
-
bySource.set(obs.source, []);
|
|
945
|
-
}
|
|
946
|
-
bySource.get(obs.source).push(obs.embedding);
|
|
947
|
-
}
|
|
948
|
-
if (bySource.size < 2)
|
|
949
|
-
return 1.0;
|
|
950
|
-
let totalConsistency = 0;
|
|
951
|
-
let count = 0;
|
|
952
|
-
for (const embeddings of bySource.values()) {
|
|
953
|
-
if (embeddings.length < 2)
|
|
954
|
-
continue;
|
|
955
|
-
// Calculate average pairwise similarity within source
|
|
956
|
-
for (let i = 0; i < embeddings.length; i++) {
|
|
957
|
-
for (let j = i + 1; j < embeddings.length; j++) {
|
|
958
|
-
totalConsistency += this.cosineSimilarity(embeddings[i], embeddings[j]);
|
|
959
|
-
count++;
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
return count > 0 ? totalConsistency / count : 1.0;
|
|
964
|
-
}
|
|
965
|
-
cosineSimilarity(a, b) {
|
|
966
|
-
let dot = 0, normA = 0, normB = 0;
|
|
967
|
-
for (let i = 0; i < a.length; i++) {
|
|
968
|
-
dot += a[i] * b[i];
|
|
969
|
-
normA += a[i] * a[i];
|
|
970
|
-
normB += b[i] * b[i];
|
|
971
|
-
}
|
|
972
|
-
return dot / (Math.sqrt(normA * normB) + 1e-8);
|
|
973
|
-
}
|
|
974
|
-
}
|
|
975
|
-
exports.CoherenceMonitor = CoherenceMonitor;
|
|
976
|
-
// ============================================================================
|
|
977
|
-
// 6. NEURAL SUBSTRATE - Synthetic Nervous System
|
|
978
|
-
// ============================================================================
|
|
979
|
-
/**
|
|
980
|
-
* Unified neural embedding substrate combining all components.
|
|
981
|
-
* Acts like a synthetic nervous system with reflexes, memory, and coordination.
|
|
982
|
-
*/
|
|
983
|
-
class NeuralSubstrate {
|
|
984
|
-
constructor(config = {}) {
|
|
985
|
-
this.logger = config.logger ?? exports.defaultLogger;
|
|
986
|
-
this.config = {
|
|
987
|
-
dimension: config.dimension ?? exports.NEURAL_CONSTANTS.DEFAULT_DIMENSION,
|
|
988
|
-
driftThreshold: config.driftThreshold ?? exports.NEURAL_CONSTANTS.DEFAULT_DRIFT_THRESHOLD,
|
|
989
|
-
driftWindowMs: config.driftWindowMs ?? exports.NEURAL_CONSTANTS.DEFAULT_DRIFT_WINDOW_MS,
|
|
990
|
-
memoryDecayRate: config.memoryDecayRate ?? exports.NEURAL_CONSTANTS.DEFAULT_MEMORY_DECAY_RATE,
|
|
991
|
-
interferenceThreshold: config.interferenceThreshold ?? exports.NEURAL_CONSTANTS.DEFAULT_INTERFERENCE_THRESHOLD,
|
|
992
|
-
consolidationRate: config.consolidationRate ?? exports.NEURAL_CONSTANTS.DEFAULT_CONSOLIDATION_RATE,
|
|
993
|
-
reflexLatencyMs: config.reflexLatencyMs ?? exports.NEURAL_CONSTANTS.DEFAULT_REFLEX_LATENCY_MS,
|
|
994
|
-
logger: this.logger,
|
|
995
|
-
};
|
|
996
|
-
this.reflexLatency = this.config.reflexLatencyMs;
|
|
997
|
-
// Pass logger to all sub-components
|
|
998
|
-
this.drift = new SemanticDriftDetector(this.config);
|
|
999
|
-
this.memory = new MemoryPhysics(this.config);
|
|
1000
|
-
this.state = new EmbeddingStateMachine(this.config);
|
|
1001
|
-
this.swarm = new SwarmCoordinator(this.config);
|
|
1002
|
-
this.coherence = new CoherenceMonitor(this.config);
|
|
1003
|
-
// Wire up default reflexes
|
|
1004
|
-
this.drift.registerReflex('memory_consolidation', (event) => {
|
|
1005
|
-
if (event.category === 'critical') {
|
|
1006
|
-
// Consolidate memory on critical drift
|
|
1007
|
-
this.memory.consolidate();
|
|
1008
|
-
}
|
|
1009
|
-
});
|
|
1010
|
-
this.drift.registerReflex('coherence_check', (event) => {
|
|
1011
|
-
if (event.category !== 'normal') {
|
|
1012
|
-
// Check coherence on any significant drift
|
|
1013
|
-
const report = this.coherence.report();
|
|
1014
|
-
if (report.overallScore < exports.NEURAL_CONSTANTS.COHERENCE_WARNING_THRESHOLD) {
|
|
1015
|
-
this.logger.log('warn', 'Neural substrate coherence warning', {
|
|
1016
|
-
overallScore: report.overallScore,
|
|
1017
|
-
driftScore: report.driftScore,
|
|
1018
|
-
stabilityScore: report.stabilityScore,
|
|
1019
|
-
alignmentScore: report.alignmentScore,
|
|
1020
|
-
anomalyCount: report.anomalies.length,
|
|
1021
|
-
});
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
});
|
|
1025
|
-
}
|
|
1026
|
-
/**
|
|
1027
|
-
* Process an embedding through the entire substrate
|
|
1028
|
-
*/
|
|
1029
|
-
process(embedding, options = {}) {
|
|
1030
|
-
const emb = embedding instanceof Float32Array
|
|
1031
|
-
? embedding
|
|
1032
|
-
: new Float32Array(embedding);
|
|
1033
|
-
// 1. Observe for drift
|
|
1034
|
-
const driftEvent = this.drift.observe(emb, options.source);
|
|
1035
|
-
// 2. Encode to memory if content provided
|
|
1036
|
-
let memoryEntry = null;
|
|
1037
|
-
if (options.memoryId && options.content) {
|
|
1038
|
-
memoryEntry = this.memory.encode(options.memoryId, emb, options.content);
|
|
1039
|
-
}
|
|
1040
|
-
// 3. Update agent state if ID provided
|
|
1041
|
-
let agentState = null;
|
|
1042
|
-
if (options.agentId) {
|
|
1043
|
-
agentState = this.state.updateAgent(options.agentId, emb);
|
|
1044
|
-
this.swarm.register(options.agentId, emb);
|
|
1045
|
-
}
|
|
1046
|
-
// 4. Record for coherence monitoring
|
|
1047
|
-
this.coherence.observe(emb, options.source);
|
|
1048
|
-
return { drift: driftEvent, memory: memoryEntry, state: agentState };
|
|
1049
|
-
}
|
|
1050
|
-
/**
|
|
1051
|
-
* Query the substrate
|
|
1052
|
-
*/
|
|
1053
|
-
query(embedding, k = 5) {
|
|
1054
|
-
const emb = embedding instanceof Float32Array
|
|
1055
|
-
? embedding
|
|
1056
|
-
: new Float32Array(embedding);
|
|
1057
|
-
return {
|
|
1058
|
-
memories: this.memory.recall(emb, k),
|
|
1059
|
-
collaborators: [], // Would need agent context
|
|
1060
|
-
coherence: this.coherence.report(),
|
|
1061
|
-
};
|
|
1062
|
-
}
|
|
1063
|
-
/**
|
|
1064
|
-
* Get overall system health
|
|
1065
|
-
*/
|
|
1066
|
-
health() {
|
|
1067
|
-
return {
|
|
1068
|
-
driftStats: this.drift.getStats(),
|
|
1069
|
-
memoryStats: this.memory.getStats(),
|
|
1070
|
-
swarmCoherence: this.swarm.getCoherence(),
|
|
1071
|
-
coherenceReport: this.coherence.report(),
|
|
1072
|
-
};
|
|
1073
|
-
}
|
|
1074
|
-
/**
|
|
1075
|
-
* Run consolidation (like "sleep")
|
|
1076
|
-
*/
|
|
1077
|
-
consolidate() {
|
|
1078
|
-
return this.memory.consolidate();
|
|
1079
|
-
}
|
|
1080
|
-
/**
|
|
1081
|
-
* Calibrate coherence baseline
|
|
1082
|
-
*/
|
|
1083
|
-
calibrate() {
|
|
1084
|
-
this.coherence.calibrate();
|
|
1085
|
-
}
|
|
1086
|
-
}
|
|
1087
|
-
exports.NeuralSubstrate = NeuralSubstrate;
|
|
1088
|
-
// ============================================================================
|
|
1089
|
-
// Exports
|
|
1090
|
-
// ============================================================================
|
|
1091
|
-
exports.default = NeuralSubstrate;
|