sublinear_solver/temporal_nexus/core/
mod.rs1pub mod scheduler;
10pub mod temporal_window;
11pub mod strange_loop;
12pub mod identity;
13
14pub use scheduler::NanosecondScheduler;
15pub use temporal_window::{TemporalWindow, WindowOverlapManager};
16pub use strange_loop::{StrangeLoopOperator, ContractionMetrics};
17pub use identity::{IdentityContinuityTracker, ContinuityMetrics};
18
19#[derive(Debug, Clone)]
21pub struct TemporalConfig {
22 pub window_overlap_percent: f64,
24 pub max_scheduling_overhead_ns: u64,
26 pub lipschitz_bound: f64,
28 pub max_contraction_iterations: usize,
30 pub tsc_frequency_hz: u64,
32}
33
34impl Default for TemporalConfig {
35 fn default() -> Self {
36 Self {
37 window_overlap_percent: 75.0,
38 max_scheduling_overhead_ns: 1_000, lipschitz_bound: 0.95,
40 max_contraction_iterations: 10,
41 tsc_frequency_hz: 3_000_000_000, }
43 }
44}
45
46#[derive(Debug, Clone, PartialEq)]
48pub enum ConsciousnessTask {
49 Perception { priority: u8, data: Vec<u8> },
51 MemoryIntegration { session_id: String, state: Vec<u8> },
53 IdentityPreservation { continuity_check: bool },
55 StrangeLoopProcessing { iteration: usize, state: Vec<f64> },
57 WindowManagement { window_id: u64, overlap_target: f64 },
59}
60
61#[derive(Debug, thiserror::Error)]
63pub enum TemporalError {
64 #[error("Scheduling overhead exceeded limit: {actual_ns}ns > {limit_ns}ns")]
65 SchedulingOverhead { actual_ns: u64, limit_ns: u64 },
66
67 #[error("Window overlap below threshold: {actual}% < {required}%")]
68 WindowOverlapTooLow { actual: f64, required: f64 },
69
70 #[error("Contraction failed to converge in {iterations} iterations")]
71 ContractionNoConvergence { iterations: usize },
72
73 #[error("Identity continuity broken: gap = {gap_ns}ns")]
74 IdentityContinuityBreak { gap_ns: u64 },
75
76 #[error("TSC timing error: {message}")]
77 TscTimingError { message: String },
78
79 #[error("Task queue overflow: {current_size}/{max_size}")]
80 TaskQueueOverflow { current_size: usize, max_size: usize },
81}
82
83pub type TemporalResult<T> = Result<T, TemporalError>;
84
85#[cfg(any(
89 not(any(target_arch = "x86_64", target_arch = "aarch64")),
90 all(target_arch = "aarch64", not(target_feature = "neon")),
91))]
92fn generic_now_ns() -> u64 {
93 use std::sync::OnceLock;
94 use std::time::Instant;
95 static START: OnceLock<Instant> = OnceLock::new();
96 let start = START.get_or_init(Instant::now);
97 start.elapsed().as_nanos() as u64
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
102pub struct TscTimestamp(pub u64);
103
104impl TscTimestamp {
105 #[inline]
118 pub fn now() -> Self {
119 #[cfg(target_arch = "x86_64")]
120 unsafe {
121 Self(core::arch::x86_64::_rdtsc())
122 }
123 #[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
124 unsafe {
125 let val: u64;
126 core::arch::asm!("mrs {0}, cntvct_el0", out(reg) val);
127 Self(val)
128 }
129 #[cfg(all(target_arch = "aarch64", not(target_feature = "neon")))]
130 unsafe {
131 Self(generic_now_ns())
135 }
136 #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
137 {
138 Self(generic_now_ns())
139 }
140 }
141
142 pub fn nanos_since(&self, other: TscTimestamp, tsc_freq_hz: u64) -> u64 {
144 let tsc_diff = self.0.saturating_sub(other.0);
145 (tsc_diff * 1_000_000_000) / tsc_freq_hz
146 }
147
148 pub fn add_nanos(&self, nanos: u64, tsc_freq_hz: u64) -> Self {
150 let tsc_increment = (nanos * tsc_freq_hz) / 1_000_000_000;
151 Self(self.0 + tsc_increment)
152 }
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158
159 #[test]
160 fn test_temporal_config_default() {
161 let config = TemporalConfig::default();
162 assert!(config.window_overlap_percent >= 50.0);
163 assert!(config.window_overlap_percent <= 100.0);
164 assert!(config.lipschitz_bound < 1.0);
165 assert!(config.max_scheduling_overhead_ns <= 1_000);
166 }
167
168 #[test]
169 fn test_tsc_timestamp() {
170 let ts1 = TscTimestamp::now();
171 std::thread::sleep(std::time::Duration::from_nanos(1000));
172 let ts2 = TscTimestamp::now();
173 assert!(ts2 > ts1);
174
175 let nanos = ts2.nanos_since(ts1, 3_000_000_000);
176 assert!(nanos > 0);
177 }
178}