Skip to main content

maple_runtime/types/
temporal.rs

1//! Temporal coordination types
2//!
3//! The Resonance Architecture does NOT assume synchronized clocks.
4//! Time is defined relationally through temporal anchors.
5
6use super::ids::{AnchorId, CommitmentId};
7use chrono::{DateTime, Utc};
8use serde::{Deserialize, Serialize};
9
10/// A temporal anchor - any event that allows ordering interactions
11///
12/// Temporal anchors enable causal ordering without global clocks.
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct TemporalAnchor {
15    /// Unique identifier
16    pub id: AnchorId,
17
18    /// Local timestamp (for this Resonator only)
19    ///
20    /// This is NOT a global timestamp - it's only meaningful
21    /// within the context of a single Resonator's timeline.
22    pub local_time: LocalTimestamp,
23
24    /// Causal dependencies (what happened before this)
25    ///
26    /// This is what enables causal ordering across Resonators.
27    pub causal_deps: Vec<AnchorId>,
28
29    /// Associated commitment (if any)
30    pub commitment: Option<CommitmentId>,
31}
32
33impl TemporalAnchor {
34    /// Create a new temporal anchor with current local time
35    pub fn now() -> Self {
36        Self {
37            id: AnchorId::generate(),
38            local_time: LocalTimestamp::now(),
39            causal_deps: Vec::new(),
40            commitment: None,
41        }
42    }
43
44    /// Create a temporal anchor with explicit dependencies
45    pub fn with_deps(causal_deps: Vec<AnchorId>) -> Self {
46        Self {
47            id: AnchorId::generate(),
48            local_time: LocalTimestamp::now(),
49            causal_deps,
50            commitment: None,
51        }
52    }
53
54    /// Add a causal dependency
55    pub fn add_dep(&mut self, dep: AnchorId) {
56        if !self.causal_deps.contains(&dep) {
57            self.causal_deps.push(dep);
58        }
59    }
60}
61
62/// Local timestamp - only meaningful within a single Resonator's timeline
63#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
64pub struct LocalTimestamp {
65    /// Local monotonic counter
66    pub sequence: u64,
67
68    /// Wall clock time (for human-readable debugging)
69    ///
70    /// NOT used for causal ordering - only for debugging and logging.
71    pub wall_clock: DateTime<Utc>,
72}
73
74impl LocalTimestamp {
75    pub fn now() -> Self {
76        Self {
77            sequence: 0, // Will be assigned by timeline
78            wall_clock: Utc::now(),
79        }
80    }
81
82    pub fn with_sequence(sequence: u64) -> Self {
83        Self {
84            sequence,
85            wall_clock: Utc::now(),
86        }
87    }
88}
89
90impl Default for LocalTimestamp {
91    fn default() -> Self {
92        Self::now()
93    }
94}
95
96/// Configuration for temporal coordination
97#[derive(Debug, Clone, Serialize, Deserialize)]
98pub struct TemporalConfig {
99    /// Enable wall clock timestamps for debugging?
100    pub enable_wall_clock: bool,
101
102    /// Maximum causal dependency chain length
103    pub max_causal_chain_length: usize,
104
105    /// Enable causal verification?
106    pub enable_causal_verification: bool,
107}
108
109impl Default for TemporalConfig {
110    fn default() -> Self {
111        Self {
112            enable_wall_clock: true,
113            max_causal_chain_length: 1000,
114            enable_causal_verification: true,
115        }
116    }
117}