Skip to main content

qualia_core_db/q42/m64_matter/
phase_juncture.rs

1//! Mode 0x03: Fixed 64-Byte Phase Juncture & Action Record (`M64PhaseJunctureRecord`).
2//!
3//! Encodes stationary-action phase junctures, including dynamic tool excavations,
4//! structural collapses, and parameterized cartoon kinetics (such as volume deficits
5//! for squash-and-stretch reinflation and passive actor provenance).
6
7use super::chunk_directory::M64Error;
8use crate::container_10d::crc32c::crc32c;
9
10pub const PHASE_JUNCTURE_RECORD_BYTES: usize = 64;
11
12pub const FLAG_TOOL_EXCAVATION: u32 = 1 << 0;
13pub const FLAG_STRATUM_COLLAPSE: u32 = 1 << 1;
14pub const FLAG_SURFACE_FLOOD: u32 = 1 << 2;
15pub const FLAG_CARTOON_WAFER: u32 = 1 << 3;
16pub const FLAG_SPECULATIVE_BRANCH: u32 = 1 << 4;
17
18/// Fixed 64-Byte Phase Juncture Record (Mode 0x03).
19///
20/// Fields are ordered by descending alignment requirements (8B -> 4B -> 2B -> 1B)
21/// to ensure exactly 64 bytes with zero compiler-inserted padding.
22#[repr(C, align(64))]
23#[derive(Copy, Clone, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
24pub struct M64PhaseJunctureRecord {
25    // --- 8-byte aligned fields (16 bytes, offsets 0..16) ---
26    /// 8B: FNV-1a hash of actor DID responsible for state change (zero-blocker provenance).
27    pub provenance_did_hash: u64,
28    /// 8B: Monotonic Lamport timestamp of modification.
29    pub lamport_clock: u64,
30
31    // --- 4-byte aligned fields (40 bytes, offsets 16..56) ---
32    /// 12B: Focal spatial center (x, y, z) in mm relative to geodetic datum.
33    pub anchor_pos_mm: [i32; 3],
34    /// 4B: Simulation branch q (0 = ground truth, >0 = speculative/counterfactual).
35    pub quantum_branch_id: u32,
36    /// 4B: Boundary condition flags (tool excavation, collapse, flood, cartoon wafer).
37    pub boundary_flags: u32,
38    /// 4B: Stationary-action value; holds volume deficit ΔV (m³) for cartoon reinflation.
39    pub action_integral_s: f32,
40    /// 16B: Relativistic T_μν / continuum Cauchy stress components [σ_xx, σ_yy, σ_zz, τ_xy].
41    pub stress_energy_tensor: [f32; 4],
42
43    // --- 2-byte & 1-byte aligned fields (4 bytes, offsets 56..60) ---
44    /// 2B: Spherical/CSG radius of tool excavation cavity in mm.
45    pub excavation_radius_mm: u16,
46    /// 1B: LUT index of material removed or transformed.
47    pub excavated_lut_material: u8,
48    /// 1B: Simulation ticks remaining before cartoon reinflation dynamics trigger.
49    pub re_inflation_delay_ticks: u8,
50
51    // --- Checksum (4 bytes, offsets 60..64) ---
52    /// 4B: CRC-32C checksum of bytes 0..59 (Total = 64B).
53    pub record_crc32c: u32,
54}
55
56impl Default for M64PhaseJunctureRecord {
57    fn default() -> Self {
58        Self {
59            provenance_did_hash: 0,
60            lamport_clock: 0,
61            anchor_pos_mm: [0, 0, 0],
62            quantum_branch_id: 0,
63            boundary_flags: 0,
64            action_integral_s: 0.0,
65            stress_energy_tensor: [0.0; 4],
66            excavation_radius_mm: 0,
67            excavated_lut_material: 0,
68            re_inflation_delay_ticks: 0,
69            record_crc32c: 0,
70        }
71    }
72}
73
74impl M64PhaseJunctureRecord {
75    /// Compute the CRC-32C over the first 60 bytes of the record.
76    #[inline]
77    pub fn compute_crc32c(&self) -> u32 {
78        let bytes: &[u8; PHASE_JUNCTURE_RECORD_BYTES] = bytemuck::cast_ref(self);
79        crc32c(&bytes[..60])
80    }
81
82    /// Update `record_crc32c` with the computed CRC-32C.
83    #[inline]
84    pub fn update_crc(&mut self) {
85        self.record_crc32c = self.compute_crc32c();
86    }
87
88    /// Validate the CRC-32C of this phase juncture record.
89    #[inline]
90    pub fn validate(&self) -> Result<(), M64Error> {
91        let computed = self.compute_crc32c();
92        if self.record_crc32c == computed {
93            Ok(())
94        } else {
95            Err(M64Error::ChecksumMismatch {
96                stored: self.record_crc32c,
97                computed,
98            })
99        }
100    }
101
102    /// Returns true if this record belongs to the veridical ground-truth branch (q = 0).
103    #[inline]
104    pub fn is_ground_truth(&self) -> bool {
105        self.quantum_branch_id == 0
106    }
107
108    /// Returns the volume deficit ΔV in m³ when boundary flags indicate cartoon wafer deformation.
109    #[inline]
110    pub fn volume_deficit_m3(&self) -> f32 {
111        self.action_integral_s
112    }
113
114    /// Sets the volume deficit ΔV in m³ and marks the cartoon wafer flag.
115    #[inline]
116    pub fn set_cartoon_wafer_deficit(&mut self, deficit_m3: f32, delay_ticks: u8) {
117        self.boundary_flags |= FLAG_CARTOON_WAFER;
118        self.action_integral_s = deficit_m3;
119        self.re_inflation_delay_ticks = delay_ticks;
120    }
121}