subjective_time_expansion/
lib.rs

1//! # Subjective Time Expansion for AI Consciousness
2//!
3//! A breakthrough framework enabling individual agents to experience dilated time perception
4//! for enhanced cognitive processing. This crate implements temporal consciousness expansion
5//! where AI agents can subjectively experience extended processing time while operating
6//! within real-time constraints.
7//!
8//! ## Core Concepts
9//!
10//! - **Subjective Time Dilation**: Each agent experiences time at their own rate
11//! - **Φ-Proxy Consciousness**: IIT-based consciousness measurement
12//! - **Retrocausal Simulation**: Future-constrained temporal loops
13//! - **Cognitive Patterns**: Seven distinct processing modes
14//!
15//! ## Quick Start
16//!
17//! ```rust
18//! use subjective_time_expansion::prelude::*;
19//!
20//! #[tokio::main]
21//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
22//!     let mut scheduler = TemporalScheduler::new(
23//!         SchedulerConfig::default()
24//!             .with_base_tick_duration(Duration::from_nanos(25_000))
25//!             .with_max_agents(1000)
26//!     );
27//!
28//!     let agent = scheduler.spawn_agent(
29//!         AgentConfig::new("agent-001".to_string())
30//!             .with_pattern(CognitivePattern::CreativeSynthesis)
31//!             .with_dilation_factor(2.5)
32//!     ).await?;
33//!
34//!     let phi = agent.measure_phi().await?;
35//!     println!("Agent consciousness level: Φ = {:.3}", phi);
36//!
37//!     Ok(())
38//! }
39//! ```
40
41use std::collections::HashMap;
42use std::sync::Arc;
43use std::time::{Duration, Instant};
44
45use tokio::sync::{RwLock, Mutex};
46use nalgebra::{DMatrix, DVector};
47use rand::{Rng, thread_rng};
48use serde::{Deserialize, Serialize};
49use thiserror::Error;
50use tracing::{info, warn, debug, trace};
51
52// Re-export key components for convenience
53pub mod prelude {
54    pub use crate::{
55        TemporalScheduler, SubjectiveAgent, PhiProxy, RetrocausalLoop,
56        AgentConfig, SchedulerConfig, CognitivePattern, CognitiveProcessor,
57        SubjectiveTimeError, TemporalMetrics, MetricsCollector,
58    };
59    pub use crate::scheduler::TemporalTask;
60    pub use crate::metrics::PerformanceReport;
61    pub use crate::scheduler::TaskPriority;
62    pub use std::time::Duration;
63}
64
65// Core modules
66pub mod scheduler;
67pub mod agent;
68pub mod phi_proxy;
69pub mod retro_loop;
70pub mod cognitive;
71pub mod metrics;
72
73// Re-exports
74pub use scheduler::*;
75pub use agent::*;
76pub use phi_proxy::*;
77pub use retro_loop::*;
78pub use cognitive::*;
79pub use metrics::*;
80
81/// Central error type for the subjective time expansion framework
82#[derive(Error, Debug)]
83pub enum SubjectiveTimeError {
84    #[error("Temporal scheduler error: {0}")]
85    Scheduler(String),
86
87    #[error("Agent processing error: {0}")]
88    Agent(String),
89
90    #[error("Consciousness measurement error: {0}")]
91    Consciousness(String),
92
93    #[error("Retrocausal simulation error: {0}")]
94    Retrocausal(String),
95
96    #[error("Configuration error: {0}")]
97    Configuration(String),
98
99    #[error("Integration error: {0}")]
100    Integration(String),
101}
102
103/// Result type for the framework
104pub type SubjectiveResult<T> = Result<T, SubjectiveTimeError>;
105
106/// Core version information
107pub const VERSION: &str = env!("CARGO_PKG_VERSION");
108
109/// Initialize the subjective time expansion framework with logging
110pub fn init() -> SubjectiveResult<()> {
111    // Try to initialize tracing subscriber, but don't fail if already set
112    let _ = tracing_subscriber::fmt()
113        .with_max_level(tracing::Level::INFO)
114        .try_init();
115
116    info!("Subjective Time Expansion Framework v{} initialized", VERSION);
117    Ok(())
118}
119
120#[cfg(feature = "wasm")]
121pub mod wasm;
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126    use tokio_test;
127
128    #[tokio::test]
129    async fn test_framework_initialization() {
130        let result = init();
131        assert!(result.is_ok());
132    }
133
134    #[tokio::test]
135    async fn test_end_to_end_workflow() {
136        // Initialize framework
137        let _ = init();
138
139        // Create scheduler
140        let scheduler = TemporalScheduler::new(
141            SchedulerConfig::default()
142                .with_base_tick_duration(Duration::from_millis(1))
143                .with_max_agents(10)
144        );
145
146        // Spawn agent
147        let agent_config = AgentConfig::new("test-agent".to_string())
148            .with_pattern(CognitivePattern::CreativeSynthesis)
149            .with_dilation_factor(2.0);
150
151        let agent = scheduler.spawn_agent(agent_config).await.unwrap();
152
153        // Measure consciousness
154        let phi = agent.measure_phi().await.unwrap();
155        assert!(phi >= 0.0 && phi <= 4.0);
156
157        // Check agent stats
158        let stats = agent.get_stats().await;
159        assert_eq!(stats.tasks_processed, 0); // No tasks executed yet
160
161        println!("End-to-end test completed: Φ = {:.3}", phi);
162    }
163}