sublinear_solver/temporal_nexus/core/
mod.rs1pub mod identity;
10pub mod scheduler;
11pub mod strange_loop;
12pub mod temporal_window;
13
14pub use identity::{ContinuityMetrics, IdentityContinuityTracker};
15pub use scheduler::NanosecondScheduler;
16pub use strange_loop::{ContractionMetrics, StrangeLoopOperator};
17pub use temporal_window::{TemporalWindow, WindowOverlapManager};
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 {
81 current_size: usize,
82 max_size: usize,
83 },
84}
85
86pub type TemporalResult<T> = Result<T, TemporalError>;
87
88#[cfg(any(
92 not(any(target_arch = "x86_64", target_arch = "aarch64")),
93 all(target_arch = "aarch64", not(target_feature = "neon")),
94))]
95fn generic_now_ns() -> u64 {
96 use std::sync::OnceLock;
97 use std::time::Instant;
98 static START: OnceLock<Instant> = OnceLock::new();
99 let start = START.get_or_init(Instant::now);
100 start.elapsed().as_nanos() as u64
101}
102
103#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
105pub struct TscTimestamp(pub u64);
106
107impl TscTimestamp {
108 #[inline]
121 pub fn now() -> Self {
122 #[cfg(target_arch = "x86_64")]
123 unsafe {
124 Self(core::arch::x86_64::_rdtsc())
125 }
126 #[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
127 unsafe {
128 let val: u64;
129 core::arch::asm!("mrs {0}, cntvct_el0", out(reg) val);
130 Self(val)
131 }
132 #[cfg(all(target_arch = "aarch64", not(target_feature = "neon")))]
133 unsafe {
134 Self(generic_now_ns())
138 }
139 #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
140 {
141 Self(generic_now_ns())
142 }
143 }
144
145 pub fn nanos_since(&self, other: TscTimestamp, tsc_freq_hz: u64) -> u64 {
147 let tsc_diff = self.0.saturating_sub(other.0);
148 (tsc_diff * 1_000_000_000) / tsc_freq_hz
149 }
150
151 pub fn add_nanos(&self, nanos: u64, tsc_freq_hz: u64) -> Self {
153 let tsc_increment = (nanos * tsc_freq_hz) / 1_000_000_000;
154 Self(self.0 + tsc_increment)
155 }
156}
157
158#[cfg(test)]
159mod tests {
160 use super::*;
161
162 #[test]
163 fn test_temporal_config_default() {
164 let config = TemporalConfig::default();
165 assert!(config.window_overlap_percent >= 50.0);
166 assert!(config.window_overlap_percent <= 100.0);
167 assert!(config.lipschitz_bound < 1.0);
168 assert!(config.max_scheduling_overhead_ns <= 1_000);
169 }
170
171 #[test]
172 fn test_tsc_timestamp() {
173 let ts1 = TscTimestamp::now();
174 std::thread::sleep(std::time::Duration::from_nanos(1000));
175 let ts2 = TscTimestamp::now();
176 assert!(ts2 > ts1);
177
178 let nanos = ts2.nanos_since(ts1, 3_000_000_000);
179 assert!(nanos > 0);
180 }
181}