cognitum-gate-kernel 0.1.1

No-std WASM kernel for 256-tile coherence gate fabric
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Delta types for incremental graph updates
//!
//! Defines the message types that tiles receive from the coordinator.
//! All types are `#[repr(C)]` for FFI compatibility and fixed-size
//! for deterministic memory allocation.

#![allow(missing_docs)]

use core::mem::size_of;

/// Compact vertex identifier (16-bit for tile-local addressing)
pub type TileVertexId = u16;

/// Compact edge identifier (16-bit for tile-local addressing)
pub type TileEdgeId = u16;

/// Fixed-point weight (16-bit, 0.01 precision)
/// Actual weight = raw_weight / 100.0
pub type FixedWeight = u16;

/// Convert fixed-point weight to f32
#[inline(always)]
pub const fn weight_to_f32(w: FixedWeight) -> f32 {
    (w as f32) / 100.0
}

/// Convert f32 weight to fixed-point (saturating)
#[inline(always)]
pub const fn f32_to_weight(w: f32) -> FixedWeight {
    let scaled = (w * 100.0) as i32;
    if scaled < 0 {
        0
    } else if scaled > 65535 {
        65535
    } else {
        scaled as u16
    }
}

/// Delta operation tag
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum DeltaTag {
    /// No operation (padding/sentinel)
    Nop = 0,
    /// Add an edge to the graph
    EdgeAdd = 1,
    /// Remove an edge from the graph
    EdgeRemove = 2,
    /// Update the weight of an existing edge
    WeightUpdate = 3,
    /// Observation for evidence accumulation
    Observation = 4,
    /// Batch boundary marker
    BatchEnd = 5,
    /// Checkpoint request
    Checkpoint = 6,
    /// Reset tile state
    Reset = 7,
}

impl From<u8> for DeltaTag {
    fn from(v: u8) -> Self {
        match v {
            1 => DeltaTag::EdgeAdd,
            2 => DeltaTag::EdgeRemove,
            3 => DeltaTag::WeightUpdate,
            4 => DeltaTag::Observation,
            5 => DeltaTag::BatchEnd,
            6 => DeltaTag::Checkpoint,
            7 => DeltaTag::Reset,
            _ => DeltaTag::Nop,
        }
    }
}

/// Edge addition delta
#[derive(Debug, Clone, Copy, Default)]
#[repr(C)]
pub struct EdgeAdd {
    /// Source vertex (tile-local ID)
    pub source: TileVertexId,
    /// Target vertex (tile-local ID)
    pub target: TileVertexId,
    /// Edge weight (fixed-point)
    pub weight: FixedWeight,
    /// Edge flags (reserved for future use)
    pub flags: u16,
}

impl EdgeAdd {
    /// Create a new edge addition
    #[inline]
    pub const fn new(source: TileVertexId, target: TileVertexId, weight: FixedWeight) -> Self {
        Self {
            source,
            target,
            weight,
            flags: 0,
        }
    }

    /// Create from f32 weight
    #[inline]
    pub const fn with_f32_weight(source: TileVertexId, target: TileVertexId, weight: f32) -> Self {
        Self::new(source, target, f32_to_weight(weight))
    }
}

/// Edge removal delta
#[derive(Debug, Clone, Copy, Default)]
#[repr(C)]
pub struct EdgeRemove {
    /// Source vertex (tile-local ID)
    pub source: TileVertexId,
    /// Target vertex (tile-local ID)
    pub target: TileVertexId,
    /// Reserved padding for alignment
    pub _reserved: u32,
}

impl EdgeRemove {
    /// Create a new edge removal
    #[inline]
    pub const fn new(source: TileVertexId, target: TileVertexId) -> Self {
        Self {
            source,
            target,
            _reserved: 0,
        }
    }
}

/// Weight update delta
#[derive(Debug, Clone, Copy, Default)]
#[repr(C)]
pub struct WeightUpdate {
    /// Source vertex (tile-local ID)
    pub source: TileVertexId,
    /// Target vertex (tile-local ID)
    pub target: TileVertexId,
    /// New weight (fixed-point)
    pub new_weight: FixedWeight,
    /// Delta mode: 0 = absolute, 1 = relative add, 2 = relative multiply
    pub mode: u8,
    /// Reserved padding
    pub _reserved: u8,
}

impl WeightUpdate {
    /// Absolute weight update mode
    pub const MODE_ABSOLUTE: u8 = 0;
    /// Relative addition mode
    pub const MODE_ADD: u8 = 1;
    /// Relative multiply mode (fixed-point: value/100)
    pub const MODE_MULTIPLY: u8 = 2;

    /// Create an absolute weight update
    #[inline]
    pub const fn absolute(source: TileVertexId, target: TileVertexId, weight: FixedWeight) -> Self {
        Self {
            source,
            target,
            new_weight: weight,
            mode: Self::MODE_ABSOLUTE,
            _reserved: 0,
        }
    }

    /// Create a relative weight addition
    #[inline]
    pub const fn add(source: TileVertexId, target: TileVertexId, delta: FixedWeight) -> Self {
        Self {
            source,
            target,
            new_weight: delta,
            mode: Self::MODE_ADD,
            _reserved: 0,
        }
    }
}

/// Observation for evidence accumulation
///
/// Represents a measurement or event that affects the e-value calculation.
#[derive(Debug, Clone, Copy, Default)]
#[repr(C)]
pub struct Observation {
    /// Vertex or region this observation applies to
    pub vertex: TileVertexId,
    /// Observation type/category
    pub obs_type: u8,
    /// Observation flags
    pub flags: u8,
    /// Observation value (interpretation depends on obs_type)
    pub value: u32,
}

impl Observation {
    /// Observation type: connectivity evidence
    pub const TYPE_CONNECTIVITY: u8 = 0;
    /// Observation type: cut membership evidence
    pub const TYPE_CUT_MEMBERSHIP: u8 = 1;
    /// Observation type: flow evidence
    pub const TYPE_FLOW: u8 = 2;
    /// Observation type: witness evidence
    pub const TYPE_WITNESS: u8 = 3;

    /// Create a connectivity observation
    #[inline]
    pub const fn connectivity(vertex: TileVertexId, connected: bool) -> Self {
        Self {
            vertex,
            obs_type: Self::TYPE_CONNECTIVITY,
            flags: if connected { 1 } else { 0 },
            value: 0,
        }
    }

    /// Create a cut membership observation
    #[inline]
    pub const fn cut_membership(vertex: TileVertexId, side: u8, confidence: u16) -> Self {
        Self {
            vertex,
            obs_type: Self::TYPE_CUT_MEMBERSHIP,
            flags: side,
            value: confidence as u32,
        }
    }
}

/// Unified delta message (8 bytes, cache-aligned for batching)
///
/// Tagged union for all delta types. The layout is optimized for
/// WASM memory access patterns.
#[derive(Clone, Copy)]
#[repr(C)]
pub union DeltaPayload {
    /// Edge addition payload
    pub edge_add: EdgeAdd,
    /// Edge removal payload
    pub edge_remove: EdgeRemove,
    /// Weight update payload
    pub weight_update: WeightUpdate,
    /// Observation payload
    pub observation: Observation,
    /// Raw bytes for custom payloads
    pub raw: [u8; 8],
}

impl Default for DeltaPayload {
    fn default() -> Self {
        Self { raw: [0u8; 8] }
    }
}

/// Complete delta message with tag
#[derive(Clone, Copy)]
#[repr(C, align(16))]
pub struct Delta {
    /// Delta operation tag
    pub tag: DeltaTag,
    /// Sequence number for ordering
    pub sequence: u8,
    /// Source tile ID (for cross-tile deltas)
    pub source_tile: u8,
    /// Reserved for future use
    pub _reserved: u8,
    /// Timestamp (lower 32 bits of tick counter)
    pub timestamp: u32,
    /// Delta payload
    pub payload: DeltaPayload,
}

impl Default for Delta {
    fn default() -> Self {
        Self {
            tag: DeltaTag::Nop,
            sequence: 0,
            source_tile: 0,
            _reserved: 0,
            timestamp: 0,
            payload: DeltaPayload::default(),
        }
    }
}

impl Delta {
    /// Create a NOP delta
    #[inline]
    pub const fn nop() -> Self {
        Self {
            tag: DeltaTag::Nop,
            sequence: 0,
            source_tile: 0,
            _reserved: 0,
            timestamp: 0,
            payload: DeltaPayload { raw: [0u8; 8] },
        }
    }

    /// Create an edge add delta
    #[inline]
    pub fn edge_add(source: TileVertexId, target: TileVertexId, weight: FixedWeight) -> Self {
        Self {
            tag: DeltaTag::EdgeAdd,
            sequence: 0,
            source_tile: 0,
            _reserved: 0,
            timestamp: 0,
            payload: DeltaPayload {
                edge_add: EdgeAdd::new(source, target, weight),
            },
        }
    }

    /// Create an edge remove delta
    #[inline]
    pub fn edge_remove(source: TileVertexId, target: TileVertexId) -> Self {
        Self {
            tag: DeltaTag::EdgeRemove,
            sequence: 0,
            source_tile: 0,
            _reserved: 0,
            timestamp: 0,
            payload: DeltaPayload {
                edge_remove: EdgeRemove::new(source, target),
            },
        }
    }

    /// Create a weight update delta
    #[inline]
    pub fn weight_update(source: TileVertexId, target: TileVertexId, weight: FixedWeight) -> Self {
        Self {
            tag: DeltaTag::WeightUpdate,
            sequence: 0,
            source_tile: 0,
            _reserved: 0,
            timestamp: 0,
            payload: DeltaPayload {
                weight_update: WeightUpdate::absolute(source, target, weight),
            },
        }
    }

    /// Create an observation delta
    #[inline]
    pub fn observation(obs: Observation) -> Self {
        Self {
            tag: DeltaTag::Observation,
            sequence: 0,
            source_tile: 0,
            _reserved: 0,
            timestamp: 0,
            payload: DeltaPayload { observation: obs },
        }
    }

    /// Create a batch end marker
    #[inline]
    pub const fn batch_end() -> Self {
        Self {
            tag: DeltaTag::BatchEnd,
            sequence: 0,
            source_tile: 0,
            _reserved: 0,
            timestamp: 0,
            payload: DeltaPayload { raw: [0u8; 8] },
        }
    }

    /// Check if this is a NOP
    #[inline]
    pub const fn is_nop(&self) -> bool {
        matches!(self.tag, DeltaTag::Nop)
    }

    /// Get the edge add payload (unsafe: caller must verify tag)
    #[inline]
    pub unsafe fn get_edge_add(&self) -> &EdgeAdd {
        unsafe { &self.payload.edge_add }
    }

    /// Get the edge remove payload (unsafe: caller must verify tag)
    #[inline]
    pub unsafe fn get_edge_remove(&self) -> &EdgeRemove {
        unsafe { &self.payload.edge_remove }
    }

    /// Get the weight update payload (unsafe: caller must verify tag)
    #[inline]
    pub unsafe fn get_weight_update(&self) -> &WeightUpdate {
        unsafe { &self.payload.weight_update }
    }

    /// Get the observation payload (unsafe: caller must verify tag)
    #[inline]
    pub unsafe fn get_observation(&self) -> &Observation {
        unsafe { &self.payload.observation }
    }
}

// Compile-time size assertions
const _: () = assert!(size_of::<EdgeAdd>() == 8, "EdgeAdd must be 8 bytes");
const _: () = assert!(size_of::<EdgeRemove>() == 8, "EdgeRemove must be 8 bytes");
const _: () = assert!(
    size_of::<WeightUpdate>() == 8,
    "WeightUpdate must be 8 bytes"
);
const _: () = assert!(size_of::<Observation>() == 8, "Observation must be 8 bytes");
const _: () = assert!(size_of::<Delta>() == 16, "Delta must be 16 bytes");

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

    #[test]
    fn test_weight_conversion() {
        assert_eq!(weight_to_f32(100), 1.0);
        assert_eq!(weight_to_f32(50), 0.5);
        assert_eq!(weight_to_f32(0), 0.0);

        assert_eq!(f32_to_weight(1.0), 100);
        assert_eq!(f32_to_weight(0.5), 50);
        assert_eq!(f32_to_weight(0.0), 0);
    }

    #[test]
    fn test_delta_tag_roundtrip() {
        for i in 0..=7 {
            let tag = DeltaTag::from(i);
            assert_eq!(tag as u8, i);
        }
    }

    #[test]
    fn test_edge_add_creation() {
        let ea = EdgeAdd::new(1, 2, 150);
        assert_eq!(ea.source, 1);
        assert_eq!(ea.target, 2);
        assert_eq!(ea.weight, 150);
    }

    #[test]
    fn test_delta_edge_add() {
        let delta = Delta::edge_add(5, 10, 200);
        assert_eq!(delta.tag, DeltaTag::EdgeAdd);
        unsafe {
            let ea = delta.get_edge_add();
            assert_eq!(ea.source, 5);
            assert_eq!(ea.target, 10);
            assert_eq!(ea.weight, 200);
        }
    }

    #[test]
    fn test_observation_creation() {
        let obs = Observation::connectivity(42, true);
        assert_eq!(obs.vertex, 42);
        assert_eq!(obs.obs_type, Observation::TYPE_CONNECTIVITY);
        assert_eq!(obs.flags, 1);
    }
}