use crate::mem8::wave::{MemoryWave, WaveGrid};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ReactiveLayer {
HardwareReflex,
SubcorticalReaction,
EmotionalResponse,
ConsciousDeliberation,
}
impl ReactiveLayer {
pub fn response_window(&self) -> (Duration, Option<Duration>) {
match self {
Self::HardwareReflex => (Duration::ZERO, Some(Duration::from_millis(10))),
Self::SubcorticalReaction => {
(Duration::from_millis(10), Some(Duration::from_millis(50)))
}
Self::EmotionalResponse => {
(Duration::from_millis(50), Some(Duration::from_millis(200)))
}
Self::ConsciousDeliberation => (Duration::from_millis(200), None),
}
}
pub fn from_response_time(elapsed: Duration) -> Self {
match elapsed.as_millis() {
0..=10 => Self::HardwareReflex,
11..=50 => Self::SubcorticalReaction,
51..=200 => Self::EmotionalResponse,
_ => Self::ConsciousDeliberation,
}
}
}
#[derive(Clone)]
pub struct ReactivePattern {
pub id: String,
pub threshold: f32,
pub weight: f32,
pub response: Arc<dyn Fn() -> ReactiveResponse + Send + Sync>,
}
#[derive(Debug, Clone)]
pub struct ReactiveResponse {
pub layer: ReactiveLayer,
pub strength: f32,
pub action: String,
pub latency: Duration,
}
pub struct ReactiveMemory {
wave_grid: Arc<RwLock<WaveGrid>>,
patterns: Vec<Vec<ReactivePattern>>,
start_time: Instant,
}
impl ReactiveMemory {
pub fn new(wave_grid: Arc<RwLock<WaveGrid>>) -> Self {
Self {
wave_grid,
patterns: vec![Vec::new(); 4], start_time: Instant::now(),
}
}
pub fn register_pattern(&mut self, layer: ReactiveLayer, pattern: ReactivePattern) {
let layer_idx = layer as usize;
self.patterns[layer_idx].push(pattern);
}
pub fn process(&self, input: &SensorInput) -> Option<ReactiveResponse> {
let start = Instant::now();
for layer in [
ReactiveLayer::HardwareReflex,
ReactiveLayer::SubcorticalReaction,
ReactiveLayer::EmotionalResponse,
ReactiveLayer::ConsciousDeliberation,
] {
if let Some(response) = self.process_layer(layer, input, start) {
if self.should_bypass(layer, &response) {
return Some(response);
}
}
}
None
}
fn process_layer(
&self,
layer: ReactiveLayer,
input: &SensorInput,
start: Instant,
) -> Option<ReactiveResponse> {
let layer_idx = layer as usize;
let elapsed = start.elapsed();
let (min_time, max_time) = layer.response_window();
if elapsed < min_time || (max_time.is_some() && elapsed > max_time.unwrap()) {
return None;
}
let mut best_response: Option<ReactiveResponse> = None;
let mut best_strength = 0.0;
for pattern in &self.patterns[layer_idx] {
let activation = self.calculate_activation(pattern, input);
if activation > pattern.threshold && activation > best_strength {
best_strength = activation;
best_response = Some((pattern.response)());
}
}
best_response
}
fn calculate_activation(&self, pattern: &ReactivePattern, input: &SensorInput) -> f32 {
match input {
SensorInput::Visual { intensity, .. } => pattern.weight * intensity,
SensorInput::Audio { amplitude, .. } => pattern.weight * amplitude,
SensorInput::Threat { severity, .. } => pattern.weight * severity,
_ => 0.0,
}
}
fn should_bypass(&self, layer: ReactiveLayer, response: &ReactiveResponse) -> bool {
match layer {
ReactiveLayer::HardwareReflex => response.strength > 0.9,
ReactiveLayer::SubcorticalReaction => response.strength > 0.8,
ReactiveLayer::EmotionalResponse => response.strength > 0.7,
ReactiveLayer::ConsciousDeliberation => true, }
}
pub fn bypass_probability(layer: ReactiveLayer, threat_level: f32) -> f32 {
const K: f32 = 2.0; let layer_idx = layer as usize;
1.0 - (-K * (3.0 - layer_idx as f32) * threat_level).exp()
}
}
#[derive(Debug, Clone)]
pub enum SensorInput {
Visual {
intensity: f32,
motion: f32,
looming: bool,
},
Audio {
amplitude: f32,
frequency: f32,
sudden: bool,
},
Threat {
severity: f32,
proximity: f32,
pattern: String,
},
Network {
packet_malformed: bool,
attack_signature: Option<String>,
},
}
pub struct LoomingDetector {
history: Vec<(Instant, f32)>,
threshold: f32,
}
impl LoomingDetector {
pub fn new(threshold: f32) -> Self {
Self {
history: Vec::new(),
threshold,
}
}
pub fn update(&mut self, angular_size: f32) -> Option<f32> {
let now = Instant::now();
self.history.push((now, angular_size));
self.history
.retain(|(t, _)| now.duration_since(*t) < Duration::from_millis(500));
if self.history.len() < 2 {
return None;
}
let (t1, theta1) = self.history[self.history.len() - 2];
let (t2, theta2) = self.history[self.history.len() - 1];
let dt = t2.duration_since(t1).as_secs_f32();
let d_theta = theta2 - theta1;
if dt > 0.0 && theta2 > 0.0 {
let tau_inv = d_theta / (theta2 * dt);
let urgency = 1.0 - (-self.threshold / tau_inv.max(0.001)).exp();
Some(urgency)
} else {
None
}
}
}
pub struct SensorCoherence {
sensors: Vec<(f32, f32)>, }
impl Default for SensorCoherence {
fn default() -> Self {
Self::new()
}
}
impl SensorCoherence {
pub fn new() -> Self {
Self {
sensors: Vec::new(),
}
}
pub fn add_sensor(&mut self, amplitude: f32, phase: f32) {
self.sensors.push((amplitude, phase));
}
pub fn calculate(&self) -> f32 {
if self.sensors.is_empty() {
return 0.0;
}
let mut sum_real = 0.0;
let mut sum_imag = 0.0;
let mut sum_amplitude_sq = 0.0;
for &(amplitude, phase) in &self.sensors {
sum_real += amplitude * phase.cos();
sum_imag += amplitude * phase.sin();
sum_amplitude_sq += amplitude * amplitude;
}
if sum_amplitude_sq > 0.0 {
let magnitude_sq = sum_real * sum_real + sum_imag * sum_imag;
magnitude_sq / sum_amplitude_sq
} else {
0.0
}
}
}
pub struct SubliminalProcessor {
awareness_threshold: f32,
subliminal_range: (f32, f32),
}
impl Default for SubliminalProcessor {
fn default() -> Self {
Self::new()
}
}
impl SubliminalProcessor {
pub fn new() -> Self {
Self {
awareness_threshold: 0.15,
subliminal_range: (0.01, 0.15),
}
}
pub fn is_subliminal(&self, amplitude: f32) -> bool {
amplitude >= self.subliminal_range.0 && amplitude < self.subliminal_range.1
}
pub fn process(&self, wave: &MemoryWave) -> Option<SubconsciousEffect> {
if self.is_subliminal(wave.amplitude) {
Some(SubconsciousEffect {
priming: wave.frequency / 1000.0, emotional_bias: wave.valence * 0.3, pattern_activation: wave.arousal * 0.2,
})
} else {
None
}
}
}
#[derive(Debug, Clone)]
pub struct SubconsciousEffect {
pub priming: f32,
pub emotional_bias: f32,
pub pattern_activation: f32,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_reactive_layers() {
assert_eq!(
ReactiveLayer::from_response_time(Duration::from_millis(5)),
ReactiveLayer::HardwareReflex
);
assert_eq!(
ReactiveLayer::from_response_time(Duration::from_millis(30)),
ReactiveLayer::SubcorticalReaction
);
assert_eq!(
ReactiveLayer::from_response_time(Duration::from_millis(100)),
ReactiveLayer::EmotionalResponse
);
assert_eq!(
ReactiveLayer::from_response_time(Duration::from_millis(300)),
ReactiveLayer::ConsciousDeliberation
);
}
#[test]
fn test_looming_detection() {
let mut detector = LoomingDetector::new(0.5);
detector.update(0.1);
std::thread::sleep(Duration::from_millis(100));
detector.update(0.15);
std::thread::sleep(Duration::from_millis(100));
if let Some(urgency) = detector.update(0.25) {
assert!(urgency > 0.0);
assert!(urgency <= 1.0);
}
}
#[test]
fn test_sensor_coherence() {
let mut coherence = SensorCoherence::new();
coherence.add_sensor(1.0, 0.0);
coherence.add_sensor(0.8, 0.1);
coherence.add_sensor(0.9, -0.1);
let c = coherence.calculate();
assert!(c > 0.9);
coherence.add_sensor(1.0, std::f32::consts::PI); let c2 = coherence.calculate();
assert!(c2 < c); }
}