exo-exotic 0.1.1

Exotic cognitive experiments: Strange Loops, Dreams, Free Energy, Morphogenesis, Collective Consciousness, Temporal Qualia, Multiple Selves, Cognitive Thermodynamics, Emergence Detection, Cognitive Black Holes
Documentation
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
//! # Cognitive Black Holes
//!
//! Attractor states that trap cognitive processing, modeling rumination,
//! obsession, and escape dynamics in thought space.
//!
//! ## Key Concepts
//!
//! - **Attractor States**: Stable configurations that draw nearby states
//! - **Rumination Loops**: Repetitive thought patterns
//! - **Event Horizons**: Points of no return in thought space
//! - **Escape Velocity**: Energy required to exit an attractor
//! - **Singularities**: Extreme focus points
//!
//! ## Theoretical Basis
//!
//! Inspired by:
//! - Dynamical systems theory (attractors, basins)
//! - Clinical psychology (rumination, OCD)
//! - Physics of black holes as metaphor

use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Cognitive black hole representing an attractor state
#[derive(Debug)]
pub struct CognitiveBlackHole {
    /// Center of the attractor in thought space
    center: Vec<f64>,
    /// Strength of attraction (mass analog)
    strength: f64,
    /// Event horizon radius
    event_horizon: f64,
    /// Captured thoughts
    captured: Vec<CapturedThought>,
    /// Escape attempts
    escape_attempts: Vec<EscapeAttempt>,
    /// Current attraction level
    attraction_level: f64,
    /// Type of cognitive trap
    trap_type: TrapType,
}

/// A thought that has been captured by the black hole
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CapturedThought {
    pub id: Uuid,
    pub content: Vec<f64>,
    pub capture_time: u64,
    pub distance_to_center: f64,
    pub orbit_count: usize,
}

/// An attractor state in cognitive space
#[derive(Debug, Clone)]
pub struct AttractorState {
    pub id: Uuid,
    pub position: Vec<f64>,
    pub basin_radius: f64,
    pub stability: f64,
    pub attractor_type: AttractorType,
}

#[derive(Debug, Clone, PartialEq)]
pub enum AttractorType {
    /// Fixed point - single stable state
    FixedPoint,
    /// Limit cycle - periodic orbit
    LimitCycle,
    /// Strange attractor - chaotic but bounded
    Strange,
    /// Saddle - stable in some dimensions, unstable in others
    Saddle,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TrapType {
    /// Repetitive negative thinking
    Rumination,
    /// Fixation on specific thought
    Obsession,
    /// Anxious loops
    Anxiety,
    /// Depressive spirals
    Depression,
    /// Addictive patterns
    Addiction,
    /// Neutral attractor
    Neutral,
}

/// Dynamics of escaping an attractor
#[derive(Debug)]
pub struct EscapeDynamics {
    /// Current position in thought space
    #[allow(dead_code)]
    position: Vec<f64>,
    /// Current velocity (rate of change)
    #[allow(dead_code)]
    velocity: Vec<f64>,
    /// Escape energy accumulated
    escape_energy: f64,
    /// Required escape velocity
    escape_velocity: f64,
    /// Distance to event horizon
    #[allow(dead_code)]
    horizon_distance: f64,
}

/// Record of an escape attempt
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EscapeAttempt {
    pub id: Uuid,
    pub success: bool,
    pub energy_used: f64,
    pub duration: u64,
    pub method: EscapeMethod,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum EscapeMethod {
    /// Gradual energy accumulation
    Gradual,
    /// Sudden external force
    External,
    /// Reframing the attractor
    Reframe,
    /// Tunneling (quantum-like escape)
    Tunneling,
    /// Attractor destruction
    Destruction,
}

impl CognitiveBlackHole {
    /// Create a new cognitive black hole
    pub fn new() -> Self {
        Self {
            center: vec![0.0; 8],
            strength: 1.0,
            event_horizon: 0.5,
            captured: Vec::new(),
            escape_attempts: Vec::new(),
            attraction_level: 0.0,
            trap_type: TrapType::Neutral,
        }
    }

    /// Create with specific parameters
    pub fn with_params(center: Vec<f64>, strength: f64, trap_type: TrapType) -> Self {
        let event_horizon = (strength * 0.3).clamp(0.1, 1.0);

        Self {
            center,
            strength,
            event_horizon,
            captured: Vec::new(),
            escape_attempts: Vec::new(),
            attraction_level: 0.0,
            trap_type,
        }
    }

    /// Measure current attraction strength
    pub fn measure_attraction(&self) -> f64 {
        self.attraction_level
    }

    /// Check if a thought would be captured
    pub fn would_capture(&self, thought: &[f64]) -> bool {
        let distance = self.distance_to_center(thought);
        distance < self.event_horizon
    }

    fn distance_to_center(&self, point: &[f64]) -> f64 {
        let len = self.center.len().min(point.len());
        let mut sum_sq = 0.0;

        for i in 0..len {
            let diff = self.center[i] - point[i];
            sum_sq += diff * diff;
        }

        sum_sq.sqrt()
    }

    /// Submit a thought to the black hole's influence
    pub fn process_thought(&mut self, thought: Vec<f64>) -> ThoughtResult {
        let distance = self.distance_to_center(&thought);
        let gravitational_pull = self.strength / (distance.powi(2) + 0.01);

        // Update attraction level
        self.attraction_level = gravitational_pull.min(1.0);

        if distance < self.event_horizon {
            // Thought is captured
            self.captured.push(CapturedThought {
                id: Uuid::new_v4(),
                content: thought.clone(),
                capture_time: Self::current_time(),
                distance_to_center: distance,
                orbit_count: 0,
            });

            ThoughtResult::Captured {
                distance,
                attraction: gravitational_pull,
            }
        } else if distance < self.event_horizon * 3.0 {
            // In danger zone
            ThoughtResult::Orbiting {
                distance,
                attraction: gravitational_pull,
                decay_rate: gravitational_pull * 0.1,
            }
        } else {
            // Safe distance
            ThoughtResult::Free {
                distance,
                residual_pull: gravitational_pull,
            }
        }
    }

    /// Attempt to escape from the black hole
    pub fn attempt_escape(&mut self, energy: f64, method: EscapeMethod) -> EscapeResult {
        let escape_velocity = self.compute_escape_velocity();

        let success = match &method {
            EscapeMethod::Gradual => energy >= escape_velocity,
            EscapeMethod::External => energy >= escape_velocity * 0.8,
            EscapeMethod::Reframe => {
                // Reframing reduces the effective strength
                energy >= escape_velocity * 0.5
            }
            EscapeMethod::Tunneling => {
                // Probabilistic escape even with low energy
                let probability = 0.1 * (energy / escape_velocity);
                rand_probability() < probability
            }
            EscapeMethod::Destruction => {
                // Need overwhelming force
                energy >= escape_velocity * 2.0
            }
        };

        self.escape_attempts.push(EscapeAttempt {
            id: Uuid::new_v4(),
            success,
            energy_used: energy,
            duration: 0,
            method: method.clone(),
        });

        if success {
            // Free captured thoughts
            let freed = self.captured.len();
            self.captured.clear();
            self.attraction_level = 0.0;

            EscapeResult::Success {
                freed_thoughts: freed,
                energy_remaining: energy - escape_velocity,
            }
        } else {
            EscapeResult::Failure {
                energy_deficit: escape_velocity - energy,
                suggestion: self.suggest_escape_method(energy),
            }
        }
    }

    fn compute_escape_velocity(&self) -> f64 {
        // v_escape = sqrt(2 * G * M / r)
        // Simplified: stronger black hole = higher escape velocity
        (2.0 * self.strength / self.event_horizon).sqrt()
    }

    fn suggest_escape_method(&self, available_energy: f64) -> EscapeMethod {
        let escape_velocity = self.compute_escape_velocity();

        if available_energy >= escape_velocity * 0.8 {
            EscapeMethod::External
        } else if available_energy >= escape_velocity * 0.5 {
            EscapeMethod::Reframe
        } else {
            EscapeMethod::Tunneling
        }
    }

    /// Simulate one time step of orbital decay
    pub fn tick(&mut self) {
        // Captured thoughts spiral inward
        for thought in &mut self.captured {
            thought.distance_to_center *= 0.99;
            thought.orbit_count += 1;
        }

        // Increase attraction as thoughts accumulate
        if !self.captured.is_empty() {
            self.attraction_level = (self.attraction_level + 0.01).min(1.0);
        }
    }

    /// Get captured thoughts count
    pub fn captured_count(&self) -> usize {
        self.captured.len()
    }

    /// Get escape success rate
    pub fn escape_success_rate(&self) -> f64 {
        if self.escape_attempts.is_empty() {
            return 0.0;
        }

        let successes = self.escape_attempts.iter().filter(|a| a.success).count();
        successes as f64 / self.escape_attempts.len() as f64
    }

    /// Get trap type
    pub fn trap_type(&self) -> &TrapType {
        &self.trap_type
    }

    /// Get statistics
    pub fn statistics(&self) -> BlackHoleStatistics {
        BlackHoleStatistics {
            strength: self.strength,
            event_horizon: self.event_horizon,
            attraction_level: self.attraction_level,
            captured_count: self.captured.len(),
            total_escape_attempts: self.escape_attempts.len(),
            escape_success_rate: self.escape_success_rate(),
            trap_type: self.trap_type.clone(),
        }
    }

    fn current_time() -> u64 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0)
    }
}

impl Default for CognitiveBlackHole {
    fn default() -> Self {
        Self::new()
    }
}

impl AttractorState {
    /// Create a new attractor state
    pub fn new(position: Vec<f64>, attractor_type: AttractorType) -> Self {
        Self {
            id: Uuid::new_v4(),
            position,
            basin_radius: 1.0,
            stability: 0.5,
            attractor_type,
        }
    }

    /// Check if a point is in the basin of attraction
    pub fn in_basin(&self, point: &[f64]) -> bool {
        let distance = self.distance_to(point);
        distance < self.basin_radius
    }

    fn distance_to(&self, point: &[f64]) -> f64 {
        let len = self.position.len().min(point.len());
        let mut sum_sq = 0.0;

        for i in 0..len {
            let diff = self.position[i] - point[i];
            sum_sq += diff * diff;
        }

        sum_sq.sqrt()
    }

    /// Get attraction strength at a point
    pub fn attraction_at(&self, point: &[f64]) -> f64 {
        let distance = self.distance_to(point);
        if distance < 0.01 {
            return 1.0;
        }

        self.stability / distance
    }
}

impl EscapeDynamics {
    /// Create new escape dynamics
    pub fn new(position: Vec<f64>, black_hole: &CognitiveBlackHole) -> Self {
        let distance = {
            let len = position.len().min(black_hole.center.len());
            let mut sum_sq = 0.0;
            for i in 0..len {
                let diff = position[i] - black_hole.center[i];
                sum_sq += diff * diff;
            }
            sum_sq.sqrt()
        };

        Self {
            position,
            velocity: vec![0.0; 8],
            escape_energy: 0.0,
            escape_velocity: (2.0 * black_hole.strength / distance.max(0.1)).sqrt(),
            horizon_distance: distance - black_hole.event_horizon,
        }
    }

    /// Add escape energy
    pub fn add_energy(&mut self, amount: f64) {
        self.escape_energy += amount;
    }

    /// Check if we have escape velocity
    pub fn can_escape(&self) -> bool {
        self.escape_energy >= self.escape_velocity * 0.5
    }

    /// Get progress towards escape (0-1)
    pub fn escape_progress(&self) -> f64 {
        (self.escape_energy / self.escape_velocity).min(1.0)
    }
}

/// Result of processing a thought
#[derive(Debug, Clone)]
pub enum ThoughtResult {
    Captured {
        distance: f64,
        attraction: f64,
    },
    Orbiting {
        distance: f64,
        attraction: f64,
        decay_rate: f64,
    },
    Free {
        distance: f64,
        residual_pull: f64,
    },
}

/// Result of an escape attempt
#[derive(Debug, Clone)]
pub enum EscapeResult {
    Success {
        freed_thoughts: usize,
        energy_remaining: f64,
    },
    Failure {
        energy_deficit: f64,
        suggestion: EscapeMethod,
    },
}

/// Statistics about the black hole
#[derive(Debug, Clone)]
pub struct BlackHoleStatistics {
    pub strength: f64,
    pub event_horizon: f64,
    pub attraction_level: f64,
    pub captured_count: usize,
    pub total_escape_attempts: usize,
    pub escape_success_rate: f64,
    pub trap_type: TrapType,
}

/// Simple probability function
fn rand_probability() -> f64 {
    use std::time::SystemTime;
    let seed = SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(12345) as u64;

    // Simple LCG
    let result = seed
        .wrapping_mul(6364136223846793005)
        .wrapping_add(1442695040888963407);
    (result as f64) / (u64::MAX as f64)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_black_hole_creation() {
        let bh = CognitiveBlackHole::new();
        assert_eq!(bh.captured_count(), 0);
        assert_eq!(bh.measure_attraction(), 0.0);
    }

    #[test]
    fn test_thought_capture() {
        let mut bh = CognitiveBlackHole::with_params(vec![0.0; 8], 2.0, TrapType::Rumination);

        // Close thought should be captured
        let close_thought = vec![0.1; 8];
        let result = bh.process_thought(close_thought);

        assert!(matches!(result, ThoughtResult::Captured { .. }));
        assert_eq!(bh.captured_count(), 1);
    }

    #[test]
    fn test_thought_orbiting() {
        let mut bh = CognitiveBlackHole::with_params(vec![0.0; 8], 1.0, TrapType::Neutral);

        // Medium distance thought
        let thought = vec![0.8; 8];
        let result = bh.process_thought(thought);

        assert!(matches!(
            result,
            ThoughtResult::Orbiting { .. } | ThoughtResult::Free { .. }
        ));
    }

    #[test]
    fn test_escape_attempt() {
        let mut bh = CognitiveBlackHole::with_params(vec![0.0; 8], 1.0, TrapType::Anxiety);

        // Capture some thoughts
        for _ in 0..3 {
            bh.process_thought(vec![0.1; 8]);
        }

        // Attempt escape with high energy
        let result = bh.attempt_escape(10.0, EscapeMethod::External);

        if let EscapeResult::Success { freed_thoughts, .. } = result {
            assert_eq!(freed_thoughts, 3);
            assert_eq!(bh.captured_count(), 0);
        }
    }

    #[test]
    fn test_escape_failure() {
        let mut bh = CognitiveBlackHole::with_params(
            vec![0.0; 8],
            5.0, // Strong black hole
            TrapType::Depression,
        );

        bh.process_thought(vec![0.1; 8]);

        // Attempt escape with low energy
        let result = bh.attempt_escape(0.1, EscapeMethod::Gradual);

        assert!(matches!(result, EscapeResult::Failure { .. }));
    }

    #[test]
    fn test_attractor_state() {
        let attractor = AttractorState::new(vec![0.0; 4], AttractorType::FixedPoint);

        let close_point = vec![0.1; 4];
        let far_point = vec![5.0; 4];

        assert!(attractor.in_basin(&close_point));
        assert!(!attractor.in_basin(&far_point));
    }

    #[test]
    fn test_escape_dynamics() {
        let bh = CognitiveBlackHole::new();
        let mut dynamics = EscapeDynamics::new(vec![0.3; 8], &bh);

        assert!(!dynamics.can_escape());

        dynamics.add_energy(10.0);
        assert!(dynamics.escape_progress() > 0.0);
    }

    #[test]
    fn test_tick_decay() {
        let mut bh = CognitiveBlackHole::with_params(
            vec![0.0; 8],
            2.0, // Higher strength
            TrapType::Neutral,
        );
        // Use a close thought that will definitely be captured
        bh.process_thought(vec![0.1; 8]);

        assert!(!bh.captured.is_empty(), "Thought should be captured");
        let initial_distance = bh.captured[0].distance_to_center;
        bh.tick();
        let final_distance = bh.captured[0].distance_to_center;

        assert!(final_distance < initial_distance);
    }

    #[test]
    fn test_statistics() {
        let mut bh = CognitiveBlackHole::with_params(vec![0.0; 8], 1.5, TrapType::Obsession);

        bh.process_thought(vec![0.1; 8]);
        bh.attempt_escape(0.5, EscapeMethod::Tunneling);

        let stats = bh.statistics();
        assert_eq!(stats.captured_count, 1);
        assert_eq!(stats.total_escape_attempts, 1);
        assert_eq!(stats.trap_type, TrapType::Obsession);
    }
}