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