File size: 10,050 Bytes
224e773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use hyperkitty_core::Glyph;

/// Fixed-width ABI for Rust/C-- boundary
/// All fields must be stable width across targets
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct RouteDecision {
    // ABI version for safety check
    pub abi_version: u32,

    // State representation (0-5 = valid glyphs)
    pub current_state: u8,
    pub previous_state: u8,
    pub next_state: u8,
    pub padding_0: u8,

    // Validity status
    pub accepted: u32,
    pub failed_gate: u32,

    // Entropy (IEEE 754 f64)
    pub entropy: f64,
    pub entropy_ok: u32,
    pub padding_1: u32,

    // Reconciliation status
    pub reconciliation_ok: u32,
    pub route_valid: u32,
    pub invariant_preserved: u32,

    // Trace identity (WORM correlation ID)
    pub trace_id: u64,

    // Proof status
    pub proof_ok: u32,
    pub padding_2: u32,
}

impl RouteDecision {
    pub const ABI_VERSION: u32 = 1;
    pub const SIZE: usize = std::mem::size_of::<RouteDecision>();

    pub fn new(
        current: Glyph,
        previous: Glyph,
        next: Glyph,
        entropy: f64,
        accepted: bool,
        failed_gate: u32,
        entropy_ok: bool,
        reconciliation_ok: bool,
        route_valid: bool,
        invariant_ok: bool,
        trace_id: u64,
        proof_ok: bool,
    ) -> Self {
        RouteDecision {
            abi_version: Self::ABI_VERSION,
            current_state: current.to_byte(),
            previous_state: previous.to_byte(),
            next_state: next.to_byte(),
            padding_0: 0,
            accepted: if accepted { 1 } else { 0 },
            failed_gate,
            entropy,
            entropy_ok: if entropy_ok { 1 } else { 0 },
            padding_1: 0,
            reconciliation_ok: if reconciliation_ok { 1 } else { 0 },
            route_valid: if route_valid { 1 } else { 0 },
            invariant_preserved: if invariant_ok { 1 } else { 0 },
            trace_id,
            proof_ok: if proof_ok { 1 } else { 0 },
            padding_2: 0,
        }
    }

    pub fn from_reconciliation(
        decision: &crate::reconciliation::ReconciliationDecision,
    ) -> Self {
        let next = decision.next_state.unwrap_or(Glyph::Lambda);
        let failed_gate_code = match decision.failed_gate {
            None => 0,
            Some(crate::validity::ValidityGate::Balance) => 1,
            Some(crate::validity::ValidityGate::Invariant) => 2,
            Some(crate::validity::ValidityGate::Entropy) => 3,
            Some(crate::validity::ValidityGate::Proof) => 4,
            Some(crate::validity::ValidityGate::Reconciliation) => 5,
            Some(crate::validity::ValidityGate::Route) => 6,
        };

        RouteDecision::new(
            decision.current_state,
            decision.previous_state,
            next,
            decision.entropy,
            decision.accepted,
            failed_gate_code,
            decision.entropy <= crate::hyperkitty_core::MAX_ENTROPY,
            decision.validity_decision.reconciliation_ok,
            decision.route_valid,
            decision.invariant_preserved,
            decision.trace_id,
            decision.validity_decision.proof_ok,
        )
    }

    /// Validate ABI header before processing
    pub fn validate_header(&self) -> Result<(), String> {
        if self.abi_version != Self::ABI_VERSION {
            return Err(format!(
                "ABI version mismatch: expected {}, got {}",
                Self::ABI_VERSION,
                self.abi_version
            ));
        }
        Ok(())
    }

    /// Validate glyph indices are valid (0-5)
    pub fn validate_glyphs(&self) -> Result<(), String> {
        if self.current_state > 5 {
            return Err(format!("invalid current_state: {}", self.current_state));
        }
        if self.previous_state > 5 {
            return Err(format!("invalid previous_state: {}", self.previous_state));
        }
        if self.next_state > 5 {
            return Err(format!("invalid next_state: {}", self.next_state));
        }
        Ok(())
    }

    /// Validate entropy is finite and within bound
    pub fn validate_entropy(&self) -> Result<(), String> {
        if !self.entropy.is_finite() {
            return Err(format!("nonfinite entropy: {}", self.entropy));
        }
        if self.entropy < 0.0 {
            return Err(format!("negative entropy: {}", self.entropy));
        }
        if self.entropy > crate::hyperkitty_core::MAX_ENTROPY {
            return Err(format!("entropy exceeds bound: {}", self.entropy));
        }
        Ok(())
    }

    /// Full validation before C-- commits
    pub fn validate_all(&self) -> Result<(), String> {
        self.validate_header()?;
        self.validate_glyphs()?;
        self.validate_entropy()?;

        // Consistency check: accepted requires all gates OK
        if self.accepted == 1 {
            if self.entropy_ok == 0 {
                return Err("accepted but entropy_ok=0".to_string());
            }
            if self.reconciliation_ok == 0 {
                return Err("accepted but reconciliation_ok=0".to_string());
            }
            if self.route_valid == 0 {
                return Err("accepted but route_valid=0".to_string());
            }
            if self.proof_ok == 0 {
                return Err("accepted but proof_ok=0".to_string());
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_route_decision_abi_size() {
        // Ensure fixed width for ABI safety
        assert_eq!(RouteDecision::SIZE, 88);
    }

    #[test]
    fn test_route_decision_construction() {
        let decision = RouteDecision::new(
            Glyph::Pi,
            Glyph::Gamma,
            Glyph::Delta,
            0.15,
            true,
            0,
            true,
            true,
            true,
            true,
            42,
            true,
        );

        assert_eq!(decision.abi_version, RouteDecision::ABI_VERSION);
        assert_eq!(decision.accepted, 1);
        assert_eq!(decision.entropy, 0.15);
    }

    #[test]
    fn test_route_decision_validate_header_success() {
        let decision = RouteDecision::new(
            Glyph::Pi,
            Glyph::Gamma,
            Glyph::Delta,
            0.15,
            true,
            0,
            true,
            true,
            true,
            true,
            42,
            true,
        );
        assert!(decision.validate_header().is_ok());
    }

    #[test]
    fn test_route_decision_validate_header_fail() {
        let mut decision = RouteDecision::new(
            Glyph::Pi,
            Glyph::Gamma,
            Glyph::Delta,
            0.15,
            true,
            0,
            true,
            true,
            true,
            true,
            42,
            true,
        );
        decision.abi_version = 999;
        assert!(decision.validate_header().is_err());
    }

    #[test]
    fn test_route_decision_validate_glyphs_success() {
        let decision = RouteDecision::new(
            Glyph::Pi,
            Glyph::Gamma,
            Glyph::Delta,
            0.15,
            true,
            0,
            true,
            true,
            true,
            true,
            42,
            true,
        );
        assert!(decision.validate_glyphs().is_ok());
    }

    #[test]
    fn test_route_decision_validate_glyphs_fail() {
        let mut decision = RouteDecision::new(
            Glyph::Pi,
            Glyph::Gamma,
            Glyph::Delta,
            0.15,
            true,
            0,
            true,
            true,
            true,
            true,
            42,
            true,
        );
        decision.current_state = 99;
        assert!(decision.validate_glyphs().is_err());
    }

    #[test]
    fn test_route_decision_validate_entropy_success() {
        let decision = RouteDecision::new(
            Glyph::Pi,
            Glyph::Gamma,
            Glyph::Delta,
            0.15,
            true,
            0,
            true,
            true,
            true,
            true,
            42,
            true,
        );
        assert!(decision.validate_entropy().is_ok());
    }

    #[test]
    fn test_route_decision_validate_entropy_high() {
        let mut decision = RouteDecision::new(
            Glyph::Pi,
            Glyph::Gamma,
            Glyph::Delta,
            0.15,
            true,
            0,
            true,
            true,
            true,
            true,
            42,
            true,
        );
        decision.entropy = 0.25; // Exceeds MAX_ENTROPY (0.20)
        assert!(decision.validate_entropy().is_err());
    }

    #[test]
    fn test_route_decision_validate_entropy_nonfinite() {
        let mut decision = RouteDecision::new(
            Glyph::Pi,
            Glyph::Gamma,
            Glyph::Delta,
            0.15,
            true,
            0,
            true,
            true,
            true,
            true,
            42,
            true,
        );
        decision.entropy = f64::NAN;
        assert!(decision.validate_entropy().is_err());
    }

    #[test]
    fn test_route_decision_validate_all_success() {
        let decision = RouteDecision::new(
            Glyph::Pi,
            Glyph::Gamma,
            Glyph::Delta,
            0.15,
            true,
            0,
            true,
            true,
            true,
            true,
            42,
            true,
        );
        assert!(decision.validate_all().is_ok());
    }

    #[test]
    fn test_route_decision_validate_all_consistency_fail() {
        let decision = RouteDecision::new(
            Glyph::Pi,
            Glyph::Gamma,
            Glyph::Delta,
            0.15,
            true, // accepted=1
            0,
            false, // entropy_ok=0 (contradiction!)
            true,
            true,
            true,
            42,
            true,
        );
        assert!(decision.validate_all().is_err());
    }
}