-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrow_bridge.rs
More file actions
1348 lines (1197 loc) · 44.9 KB
/
Copy patharrow_bridge.rs
File metadata and controls
1348 lines (1197 loc) · 44.9 KB
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Arrow interop bridge for cognitive types.
//!
//! Provides zero-copy conversion patterns between ndarray's cognitive types
//! and Apache Arrow columnar format for Lance integration.
//!
//! NOTE: This module provides the schema definitions and conversion traits.
//! The actual Arrow/Lance dependencies are optional and gated behind features.
use super::bitwise::hamming_distance_raw;
use super::fingerprint::Fingerprint;
use super::plane::{Plane, PLANE_BYTES};
/// Binary fingerprint width in bytes (16384-bit fingerprint).
pub const PLANE_BINARY_BYTES: usize = 2048;
/// Legacy alias for `PLANE_BINARY_BYTES`.
pub const BINARY_BYTES: usize = PLANE_BYTES; // 2048
/// Soaking accumulator length (i8 entries per plane).
pub const SOAKING_DIMS: usize = 16_384;
/// Sigma attention mask width in bytes (16384-bit mask = 2048 bytes).
pub const SIGMA_MASK_BYTES: usize = 2048;
/// Default soaking dimension count.
pub const DEFAULT_SOAKING_DIM: usize = 16_384;
/// Schema field names for the bind_nodes_v2 three-plane layout.
pub mod schema {
pub const S_BINARY: &str = "s_binary";
pub const P_BINARY: &str = "p_binary";
pub const O_BINARY: &str = "o_binary";
pub const S_SOAKING: &str = "s_soaking";
pub const P_SOAKING: &str = "p_soaking";
pub const O_SOAKING: &str = "o_soaking";
pub const SPO_BINARY: &str = "spo_binary";
pub const NODE_ID: &str = "node_id";
}
/// Role provenance bitflags.
pub mod role_provenance {
pub const GRAMMAR: u8 = 0x01;
pub const NSM: u8 = 0x02;
pub const SIGMA: u8 = 0x04;
pub const USER: u8 = 0x08;
}
/// Gate state for superposition management.
///
/// Lifecycle: `Form` (accumulating) -> `Flow` (serving) -> `Freeze` (archived).
///
/// - **Form** (0): soaking active, binaries mutable.
/// - **Flow** (1): serving, soaking nulled, binary immutable.
/// - **Freeze** (2): archived, entire row compressed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum GateState {
/// Accumulating — soaking active, binaries mutable.
Form = 0,
/// Serving — soaking nulled, binary immutable.
Flow = 1,
/// Archived — entire row compressed.
Freeze = 2,
}
impl GateState {
/// Returns `true` when the gate is in serving mode (`Flow`).
#[inline]
pub fn is_serving(&self) -> bool {
*self == GateState::Flow
}
/// Returns `true` when soaking writes are permitted (`Form` only).
#[inline]
pub fn can_write_soaking(&self) -> bool {
*self == GateState::Form
}
}
/// Role selector for per-plane operations on a `BindNodeV2`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Role {
/// Subject plane.
Subject,
/// Predicate plane.
Predicate,
/// Object plane.
Object,
}
/// Soaking buffer: owned int8 buffer for N entries × D dimensions.
#[derive(Debug, Clone)]
pub struct SoakingBuffer {
pub data: Vec<i8>,
pub n_entries: usize,
pub n_dims: usize,
}
impl SoakingBuffer {
pub fn new(n_entries: usize, n_dims: usize) -> Self {
Self {
data: vec![0i8; n_entries * n_dims],
n_entries,
n_dims,
}
}
pub fn entry(&self, idx: usize) -> &[i8] {
let start = idx * self.n_dims;
&self.data[start..start + self.n_dims]
}
pub fn entry_mut(&mut self, idx: usize) -> &mut [i8] {
let start = idx * self.n_dims;
&mut self.data[start..start + self.n_dims]
}
/// Zero-copy columnar view of the entire soaking buffer as a flat `&[i8]` slice.
///
/// Layout: `n_entries × n_dims` row-major. Use with
/// `ArrayView2::from_shape((n_entries, n_dims), buf.as_columnar_slice())`
/// for zero-copy ndarray interop.
#[inline]
pub fn as_columnar_slice(&self) -> &[i8] {
&self.data
}
/// Mutable zero-copy view of the entire soaking buffer.
#[inline]
pub fn as_columnar_slice_mut(&mut self) -> &mut [i8] {
&mut self.data
}
/// Crystallize: convert soaking (int8) to binary fingerprint via sign().
pub fn crystallize(&self, idx: usize) -> Vec<u8> {
let entry = self.entry(idx);
let n_bytes = (entry.len() + 7) / 8;
let mut bits = vec![0u8; n_bytes];
for (i, &val) in entry.iter().enumerate() {
if val > 0 {
bits[i / 8] |= 1 << (i % 8);
}
}
bits
}
}
/// Plane buffer: binary fingerprints + optional soaking layer.
#[derive(Debug, Clone)]
pub struct PlaneBuffer {
pub binary: Vec<u8>,
pub soaking: Option<SoakingBuffer>,
pub n_entries: usize,
pub binary_bytes: usize,
}
impl PlaneBuffer {
pub fn new(n_entries: usize, binary_bytes: usize) -> Self {
Self {
binary: vec![0u8; n_entries * binary_bytes],
soaking: None,
n_entries,
binary_bytes,
}
}
pub fn with_soaking(mut self, n_dims: usize) -> Self {
self.soaking = Some(SoakingBuffer::new(self.n_entries, n_dims));
self
}
pub fn binary_entry(&self, idx: usize) -> &[u8] {
let start = idx * self.binary_bytes;
&self.binary[start..start + self.binary_bytes]
}
pub fn binary_entry_mut(&mut self, idx: usize) -> &mut [u8] {
let start = idx * self.binary_bytes;
&mut self.binary[start..start + self.binary_bytes]
}
/// Zero-copy columnar view of all binary fingerprints as a flat `&[u8]` slice.
///
/// Layout: `n_entries × binary_bytes` row-major. Use with
/// `ArrayView2::from_shape((n_entries, binary_bytes), buf.as_binary_slice())`
/// for zero-copy ndarray interop.
#[inline]
pub fn as_binary_slice(&self) -> &[u8] {
&self.binary
}
}
/// Three-plane fingerprint buffer (S, P, O).
#[derive(Debug, Clone)]
pub struct ThreePlaneFingerprintBuffer {
pub s: PlaneBuffer,
pub p: PlaneBuffer,
pub o: PlaneBuffer,
}
impl ThreePlaneFingerprintBuffer {
pub fn new(n_entries: usize) -> Self {
Self {
s: PlaneBuffer::new(n_entries, BINARY_BYTES),
p: PlaneBuffer::new(n_entries, BINARY_BYTES),
o: PlaneBuffer::new(n_entries, BINARY_BYTES),
}
}
pub fn with_soaking(mut self, n_dims: usize) -> Self {
self.s = self.s.with_soaking(n_dims);
self.p = self.p.with_soaking(n_dims);
self.o = self.o.with_soaking(n_dims);
self
}
}
// ============================================================================
// BindNodeV2 — Three-Plane Lance schema row type
// ============================================================================
/// A single row in the bind_nodes_v2 three-plane Lance schema.
///
/// Each row stores three independently addressable planes (Subject, Predicate,
/// Object) plus a composite SPO XOR fingerprint and an attention mask.
///
/// # Lifecycle
///
/// `Form` (accumulating) -> `Flow` (serving) -> `Freeze` (archived).
///
/// In `Form` state, soaking accumulators are active and can be written.
/// Calling [`crystallize`](Self::crystallize) transitions to `Flow`:
/// soaking values are folded into the binary fingerprints via `sign()` and
/// then nulled out. Calling [`freeze`](Self::freeze) transitions to `Freeze`.
///
/// # Example
///
/// ```
/// use ndarray::hpc::arrow_bridge::BindNodeV2;
/// use ndarray::hpc::plane::Plane;
///
/// let mut s = Plane::new();
/// let mut p = Plane::new();
/// let mut o = Plane::new();
/// s.encounter("subject");
/// p.encounter("predicate");
/// o.encounter("object");
/// let mut node = BindNodeV2::new(&mut s, &mut p, &mut o, "test");
/// assert!(node.gate_state.can_write_soaking());
/// node.crystallize();
/// assert!(node.gate_state.is_serving());
/// ```
#[derive(Debug, Clone)]
pub struct BindNodeV2 {
/// Subject plane binary fingerprint (16384-bit).
pub subject_binary: [u8; PLANE_BINARY_BYTES],
/// Subject soaking accumulator. `None` when gate != `Form`.
pub subject_soaking: Option<Vec<i8>>,
/// Predicate plane binary fingerprint (16384-bit).
pub predicate_binary: [u8; PLANE_BINARY_BYTES],
/// Predicate soaking accumulator. `None` when gate != `Form`.
pub predicate_soaking: Option<Vec<i8>>,
/// Object plane binary fingerprint (16384-bit).
pub object_binary: [u8; PLANE_BINARY_BYTES],
/// Object soaking accumulator. `None` when gate != `Form`.
pub object_soaking: Option<Vec<i8>>,
/// Composite XOR fingerprint: S XOR P XOR O.
pub spo_binary: [u8; PLANE_BINARY_BYTES],
/// 16384-bit attention mask (sigma).
pub sigma_mask: [u8; SIGMA_MASK_BYTES],
/// NARS frequency (u16 fixed-point, 0..65535).
pub nars_frequency: u16,
/// NARS confidence (u16 fixed-point, 0..65535).
pub nars_confidence: u16,
/// Current gate state in the Form -> Flow -> Freeze lifecycle.
pub gate_state: GateState,
/// Provenance label for this binding.
pub role_provenance: String,
}
impl BindNodeV2 {
/// Create a new `BindNodeV2` from three planes.
///
/// Copies each plane's cached bit pattern into the binary columns,
/// copies each plane's raw accumulator into the soaking columns,
/// and computes the SPO XOR composite.
///
/// The node starts in `Form` state with soaking active.
///
/// # Example
///
/// ```
/// use ndarray::hpc::arrow_bridge::BindNodeV2;
/// use ndarray::hpc::plane::Plane;
///
/// let mut s = Plane::new();
/// let mut p = Plane::new();
/// let mut o = Plane::new();
/// s.encounter("hello");
/// p.encounter("world");
/// o.encounter("test");
/// let node = BindNodeV2::new(&mut s, &mut p, &mut o, "grammar");
/// assert_eq!(node.gate_state, ndarray::hpc::arrow_bridge::GateState::Form);
/// ```
pub fn new(subject: &mut Plane, predicate: &mut Plane, object: &mut Plane, provenance: &str) -> Self {
subject.ensure_cache();
predicate.ensure_cache();
object.ensure_cache();
let mut subject_binary = [0u8; PLANE_BINARY_BYTES];
let mut predicate_binary = [0u8; PLANE_BINARY_BYTES];
let mut object_binary = [0u8; PLANE_BINARY_BYTES];
subject_binary.copy_from_slice(subject.bits_bytes_ref());
predicate_binary.copy_from_slice(predicate.bits_bytes_ref());
object_binary.copy_from_slice(object.bits_bytes_ref());
// Copy accumulator values as soaking (truncated/padded to SOAKING_DIMS)
let subject_soaking = Some(Self::acc_to_soaking(subject.acc()));
let predicate_soaking = Some(Self::acc_to_soaking(predicate.acc()));
let object_soaking = Some(Self::acc_to_soaking(object.acc()));
let spo_binary = Self::compute_spo_xor(&subject_binary, &predicate_binary, &object_binary);
let truth = subject.truth();
Self {
subject_binary,
subject_soaking,
predicate_binary,
predicate_soaking,
object_binary,
object_soaking,
spo_binary,
sigma_mask: [0u8; SIGMA_MASK_BYTES],
nars_frequency: truth.frequency,
nars_confidence: truth.confidence,
gate_state: GateState::Form,
role_provenance: provenance.to_string(),
}
}
/// Crystallize: transition from `Form` to `Flow`.
///
/// For each plane whose soaking is active, the sign of each soaking
/// accumulator dimension is folded into the binary fingerprint. After
/// crystallization, soaking is nulled and the gate moves to `Flow`.
///
/// Does nothing if the gate is not in `Form` state.
///
/// # Example
///
/// ```
/// use ndarray::hpc::arrow_bridge::{BindNodeV2, GateState};
/// use ndarray::hpc::plane::Plane;
///
/// let mut s = Plane::new();
/// let mut p = Plane::new();
/// let mut o = Plane::new();
/// s.encounter("a");
/// p.encounter("b");
/// o.encounter("c");
/// let mut node = BindNodeV2::new(&mut s, &mut p, &mut o, "test");
/// node.crystallize();
/// assert_eq!(node.gate_state, GateState::Flow);
/// assert!(node.subject_soaking.is_none());
/// ```
pub fn crystallize(&mut self) {
if self.gate_state != GateState::Form {
return;
}
// Fold soaking sign into binary for each plane
if let Some(ref soaking) = self.subject_soaking {
Self::fold_soaking_into_binary(&mut self.subject_binary, soaking);
}
if let Some(ref soaking) = self.predicate_soaking {
Self::fold_soaking_into_binary(&mut self.predicate_binary, soaking);
}
if let Some(ref soaking) = self.object_soaking {
Self::fold_soaking_into_binary(&mut self.object_binary, soaking);
}
// Recompute SPO XOR
self.spo_binary = Self::compute_spo_xor(
&self.subject_binary,
&self.predicate_binary,
&self.object_binary,
);
// Null soaking
self.subject_soaking = None;
self.predicate_soaking = None;
self.object_soaking = None;
self.gate_state = GateState::Flow;
}
/// Freeze: transition from `Flow` to `Freeze`.
///
/// Does nothing if the gate is not in `Flow` state.
///
/// # Example
///
/// ```
/// use ndarray::hpc::arrow_bridge::{BindNodeV2, GateState};
/// use ndarray::hpc::plane::Plane;
///
/// let mut s = Plane::new();
/// let mut p = Plane::new();
/// let mut o = Plane::new();
/// s.encounter("x"); p.encounter("y"); o.encounter("z");
/// let mut node = BindNodeV2::new(&mut s, &mut p, &mut o, "test");
/// node.crystallize();
/// node.freeze();
/// assert_eq!(node.gate_state, GateState::Freeze);
/// ```
pub fn freeze(&mut self) {
if self.gate_state != GateState::Flow {
return;
}
self.gate_state = GateState::Freeze;
}
/// Compute S XOR P XOR O from the three plane binaries.
///
/// # Example
///
/// ```
/// use ndarray::hpc::arrow_bridge::BindNodeV2;
/// use ndarray::hpc::plane::Plane;
///
/// let mut s = Plane::new();
/// let mut p = Plane::new();
/// let mut o = Plane::new();
/// s.encounter("a"); p.encounter("b"); o.encounter("c");
/// let node = BindNodeV2::new(&mut s, &mut p, &mut o, "test");
/// let xor = node.spo_xor();
/// assert_eq!(xor.len(), 2048);
/// ```
pub fn spo_xor(&self) -> [u8; PLANE_BINARY_BYTES] {
Self::compute_spo_xor(&self.subject_binary, &self.predicate_binary, &self.object_binary)
}
/// Hamming distance on the composite SPO binary fingerprint.
///
/// # Example
///
/// ```
/// use ndarray::hpc::arrow_bridge::BindNodeV2;
/// use ndarray::hpc::plane::Plane;
///
/// let mut s = Plane::new();
/// let mut p = Plane::new();
/// let mut o = Plane::new();
/// s.encounter("a"); p.encounter("b"); o.encounter("c");
/// let node1 = BindNodeV2::new(&mut s, &mut p, &mut o, "t1");
/// let node2 = BindNodeV2::new(&mut s, &mut p, &mut o, "t2");
/// assert_eq!(node1.hamming_to(&node2), 0);
/// ```
pub fn hamming_to(&self, other: &BindNodeV2) -> u64 {
hamming_distance_raw(&self.spo_binary, &other.spo_binary)
}
/// Zero-copy view of a plane's binary column as a `Fingerprint<256>`.
///
/// Returns a reference-based fingerprint by copying the bytes into a
/// `Fingerprint<256>` (the copy is of the fixed-size array, not a heap
/// allocation).
///
/// # Example
///
/// ```
/// use ndarray::hpc::arrow_bridge::{BindNodeV2, Role};
/// use ndarray::hpc::plane::Plane;
///
/// let mut s = Plane::new();
/// let mut p = Plane::new();
/// let mut o = Plane::new();
/// s.encounter("a"); p.encounter("b"); o.encounter("c");
/// let node = BindNodeV2::new(&mut s, &mut p, &mut o, "test");
/// let fp = node.as_fingerprint(Role::Subject);
/// assert_eq!(fp.as_bytes().len(), 2048);
/// ```
pub fn as_fingerprint(&self, role: Role) -> Fingerprint<256> {
let bytes = match role {
Role::Subject => &self.subject_binary[..],
Role::Predicate => &self.predicate_binary[..],
Role::Object => &self.object_binary[..],
};
Fingerprint::from_bytes(bytes)
}
/// Per-role Hamming distance between two `BindNodeV2` nodes.
///
/// Computes the Hamming distance on the binary column selected by `role`.
///
/// # Example
///
/// ```
/// use ndarray::hpc::arrow_bridge::{BindNodeV2, Role};
/// use ndarray::hpc::plane::Plane;
///
/// let mut s = Plane::new();
/// let mut p = Plane::new();
/// let mut o = Plane::new();
/// s.encounter("a"); p.encounter("b"); o.encounter("c");
/// let node1 = BindNodeV2::new(&mut s, &mut p, &mut o, "t1");
/// let node2 = BindNodeV2::new(&mut s, &mut p, &mut o, "t2");
/// assert_eq!(node1.hamming_distance_to(&node2, Role::Subject), 0);
/// ```
pub fn hamming_distance_to(&self, other: &BindNodeV2, role: Role) -> u64 {
let (a, b) = match role {
Role::Subject => (&self.subject_binary[..], &other.subject_binary[..]),
Role::Predicate => (&self.predicate_binary[..], &other.predicate_binary[..]),
Role::Object => (&self.object_binary[..], &other.object_binary[..]),
};
hamming_distance_raw(a, b)
}
/// Migrate from a V1 single-fingerprint layout.
///
/// Copies the single fingerprint to all three plane binaries, sets soaking
/// to `None`, and starts in `Flow` state (already crystallized).
///
/// # Example
///
/// ```
/// use ndarray::hpc::arrow_bridge::{BindNodeV2, GateState, PLANE_BINARY_BYTES};
///
/// let fp = [0xAA_u8; PLANE_BINARY_BYTES];
/// let node = BindNodeV2::from_v1(&fp);
/// assert_eq!(node.gate_state, GateState::Flow);
/// assert_eq!(node.subject_binary, fp);
/// assert_eq!(node.predicate_binary, fp);
/// assert_eq!(node.object_binary, fp);
/// // SPO XOR of identical planes: A ^ A ^ A = A
/// assert_eq!(node.spo_binary, fp);
/// ```
pub fn from_v1(single_fingerprint: &[u8; PLANE_BINARY_BYTES]) -> Self {
let spo_binary = Self::compute_spo_xor(single_fingerprint, single_fingerprint, single_fingerprint);
Self {
subject_binary: *single_fingerprint,
subject_soaking: None,
predicate_binary: *single_fingerprint,
predicate_soaking: None,
object_binary: *single_fingerprint,
object_soaking: None,
spo_binary,
sigma_mask: [0u8; SIGMA_MASK_BYTES],
nars_frequency: 32768,
nars_confidence: 0,
gate_state: GateState::Flow,
role_provenance: String::from("v1_migration"),
}
}
// -- internal helpers --
/// Compute the XOR of three plane binaries.
fn compute_spo_xor(
s: &[u8; PLANE_BINARY_BYTES],
p: &[u8; PLANE_BINARY_BYTES],
o: &[u8; PLANE_BINARY_BYTES],
) -> [u8; PLANE_BINARY_BYTES] {
let mut result = [0u8; PLANE_BINARY_BYTES];
for i in 0..PLANE_BINARY_BYTES {
result[i] = s[i] ^ p[i] ^ o[i];
}
result
}
/// Convert a Plane accumulator (16384 i8) to a soaking vector (SOAKING_DIMS i8).
///
/// With SOAKING_DIMS = 16384, the copy is one-to-one (no truncation, no padding).
fn acc_to_soaking(acc: &[i8; 16384]) -> Vec<i8> {
let mut soaking = vec![0i8; SOAKING_DIMS];
let copy_len = SOAKING_DIMS.min(acc.len());
soaking[..copy_len].copy_from_slice(&acc[..copy_len]);
soaking
}
/// Fold soaking sign bits into a binary fingerprint.
///
/// For each dimension in the soaking vector, if `val > 0` the corresponding
/// bit is set in the binary; if `val <= 0` it is cleared.
fn fold_soaking_into_binary(binary: &mut [u8; PLANE_BINARY_BYTES], soaking: &[i8]) {
let bit_count = (PLANE_BINARY_BYTES * 8).min(soaking.len());
for i in 0..bit_count {
let byte_idx = i / 8;
let bit_idx = i % 8;
if soaking[i] > 0 {
binary[byte_idx] |= 1 << bit_idx;
} else {
binary[byte_idx] &= !(1 << bit_idx);
}
}
}
}
// ============================================================================
// Per-Row Types: ThreePlaneRowBuffer, SoakingRowBuffer, BindNodeV2Row
// ============================================================================
/// Three-plane fingerprint row buffer: holds S/P/O binary fingerprints for
/// a single row, suitable for zero-copy Arrow interop.
///
/// Total: 3 x 2048 = 6144 bytes per row.
#[derive(Debug, Clone)]
pub struct ThreePlaneRowBuffer {
/// Subject binary fingerprint (2048 bytes).
pub s_binary: Vec<u8>,
/// Predicate binary fingerprint (2048 bytes).
pub p_binary: Vec<u8>,
/// Object binary fingerprint (2048 bytes).
pub o_binary: Vec<u8>,
}
impl ThreePlaneRowBuffer {
/// Create a zeroed three-plane row buffer.
pub fn new() -> Self {
Self {
s_binary: vec![0u8; PLANE_BINARY_BYTES],
p_binary: vec![0u8; PLANE_BINARY_BYTES],
o_binary: vec![0u8; PLANE_BINARY_BYTES],
}
}
/// Create from three Plane references (copies their cached bit patterns).
pub fn from_planes(s: &mut Plane, p: &mut Plane, o: &mut Plane) -> Self {
s.ensure_cache();
p.ensure_cache();
o.ensure_cache();
Self {
s_binary: s.bits_bytes_ref().to_vec(),
p_binary: p.bits_bytes_ref().to_vec(),
o_binary: o.bits_bytes_ref().to_vec(),
}
}
/// Compute S XOR P XOR O composite fingerprint.
pub fn xor_spo(&self) -> Vec<u8> {
let mut result = vec![0u8; PLANE_BINARY_BYTES];
for i in 0..PLANE_BINARY_BYTES {
result[i] = self.s_binary[i] ^ self.p_binary[i] ^ self.o_binary[i];
}
result
}
/// Per-plane Hamming distance to another row buffer.
///
/// Returns `(subject_dist, predicate_dist, object_dist)`.
pub fn hamming_distance(&self, other: &ThreePlaneRowBuffer) -> (u64, u64, u64) {
let ds = hamming_distance_raw(&self.s_binary, &other.s_binary);
let dp = hamming_distance_raw(&self.p_binary, &other.p_binary);
let do_ = hamming_distance_raw(&self.o_binary, &other.o_binary);
(ds, dp, do_)
}
/// Total byte size of this row buffer (always 3 * PLANE_BINARY_BYTES).
pub fn total_bytes(&self) -> usize {
3 * PLANE_BINARY_BYTES
}
}
impl Default for ThreePlaneRowBuffer {
fn default() -> Self {
Self::new()
}
}
/// Soaking row buffer: nullable i8 accumulator for a single plane of a single row.
///
/// When `data` is `Some`, the buffer is active (Form state).
/// When `data` is `None`, the buffer has been crystallized or nulled.
#[derive(Debug, Clone)]
pub struct SoakingRowBuffer {
/// Soaking data (None = nulled/crystallized).
pub data: Option<Vec<i8>>,
/// Dimension count.
pub dims: usize,
}
impl SoakingRowBuffer {
/// Create a new active soaking row buffer, zeroed.
pub fn new(dims: usize) -> Self {
Self {
data: Some(vec![0i8; dims]),
dims,
}
}
/// Crystallize: convert soaking (int8) to binary fingerprint via sign().
///
/// Consumes the soaking data and returns a binary vector.
/// After crystallization, the buffer is nulled.
pub fn crystallize(&mut self) -> Vec<u8> {
let soaking = match self.data.take() {
Some(d) => d,
None => return vec![0u8; (self.dims + 7) / 8],
};
let n_bytes = (soaking.len() + 7) / 8;
let mut bits = vec![0u8; n_bytes];
for (i, &val) in soaking.iter().enumerate() {
if val > 0 {
bits[i / 8] |= 1 << (i % 8);
}
}
bits
}
/// Returns `true` when soaking is still active (not nulled/crystallized).
pub fn is_active(&self) -> bool {
self.data.is_some()
}
/// Null out the soaking data (transition to inactive).
pub fn null_out(&mut self) {
self.data = None;
}
}
/// Complete bind_nodes_v2 row type combining fingerprints, soaking, gate,
/// and NARS truth values.
///
/// This is a streamlined per-row type that pairs `ThreePlaneRowBuffer` with
/// per-plane `SoakingRowBuffer`s for the full Lance schema.
#[derive(Debug, Clone)]
pub struct BindNodeV2Row {
/// Three-plane binary fingerprints.
pub fingerprints: ThreePlaneRowBuffer,
/// Subject soaking accumulator.
pub s_soaking: SoakingRowBuffer,
/// Predicate soaking accumulator.
pub p_soaking: SoakingRowBuffer,
/// Object soaking accumulator.
pub o_soaking: SoakingRowBuffer,
/// Gate lifecycle state.
pub gate: GateState,
/// NARS frequency (u16 fixed-point).
pub nars_frequency: u16,
/// NARS confidence (u16 fixed-point).
pub nars_confidence: u16,
}
impl BindNodeV2Row {
/// Create a new row in Form state with active soaking.
pub fn new(dims: usize) -> Self {
Self {
fingerprints: ThreePlaneRowBuffer::new(),
s_soaking: SoakingRowBuffer::new(dims),
p_soaking: SoakingRowBuffer::new(dims),
o_soaking: SoakingRowBuffer::new(dims),
gate: GateState::Form,
nars_frequency: 32768,
nars_confidence: 0,
}
}
/// Crystallize all three soaking buffers, folding sign bits into fingerprints.
/// Transitions from Form to Flow.
pub fn crystallize(&mut self) {
if self.gate != GateState::Form {
return;
}
// Fold each soaking into its binary plane
if let Some(ref soaking) = self.s_soaking.data {
fold_sign_into_binary(&mut self.fingerprints.s_binary, soaking);
}
if let Some(ref soaking) = self.p_soaking.data {
fold_sign_into_binary(&mut self.fingerprints.p_binary, soaking);
}
if let Some(ref soaking) = self.o_soaking.data {
fold_sign_into_binary(&mut self.fingerprints.o_binary, soaking);
}
self.s_soaking.null_out();
self.p_soaking.null_out();
self.o_soaking.null_out();
self.gate = GateState::Flow;
}
/// Freeze: transition from Flow to Freeze.
pub fn freeze(&mut self) {
if self.gate == GateState::Flow {
self.gate = GateState::Freeze;
}
}
}
/// Fold soaking sign bits into a binary fingerprint (shared helper).
fn fold_sign_into_binary(binary: &mut [u8], soaking: &[i8]) {
let bit_count = (binary.len() * 8).min(soaking.len());
for i in 0..bit_count {
let byte_idx = i / 8;
let bit_idx = i % 8;
if soaking[i] > 0 {
binary[byte_idx] |= 1 << bit_idx;
} else {
binary[byte_idx] &= !(1 << bit_idx);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn soaking_buffer_basic() {
let mut buf = SoakingBuffer::new(3, 16);
buf.entry_mut(0).fill(10);
buf.entry_mut(1).fill(-5);
assert_eq!(buf.entry(0)[0], 10);
assert_eq!(buf.entry(1)[0], -5);
}
#[test]
fn crystallize_positive() {
let mut buf = SoakingBuffer::new(1, 8);
buf.entry_mut(0).copy_from_slice(&[1, -1, 1, -1, 1, -1, 1, -1]);
let bits = buf.crystallize(0);
assert_eq!(bits[0], 0b01010101);
}
#[test]
fn plane_buffer_entry() {
let mut pb = PlaneBuffer::new(5, BINARY_BYTES);
pb.binary_entry_mut(0).fill(0xFF);
assert_eq!(pb.binary_entry(0)[0], 0xFF);
assert_eq!(pb.binary_entry(1)[0], 0x00);
}
#[test]
fn three_plane_buffer() {
let buf = ThreePlaneFingerprintBuffer::new(10);
assert_eq!(buf.s.n_entries, 10);
assert_eq!(buf.p.binary_bytes, BINARY_BYTES);
}
#[test]
fn three_plane_with_soaking() {
let buf = ThreePlaneFingerprintBuffer::new(5).with_soaking(100);
assert!(buf.s.soaking.is_some());
assert_eq!(buf.s.soaking.as_ref().unwrap().n_dims, 100);
}
#[test]
fn gate_state_eq() {
assert_eq!(GateState::Flow, GateState::Flow);
assert_ne!(GateState::Form, GateState::Freeze);
}
#[test]
fn binary_bytes_constant() {
assert_eq!(BINARY_BYTES, 2048);
}
// ================================================================
// BindNodeV2 tests
// ================================================================
fn make_test_planes() -> (Plane, Plane, Plane) {
let mut s = Plane::new();
let mut p = Plane::new();
let mut o = Plane::new();
s.encounter("subject-signal");
s.encounter("subject-signal");
s.encounter("subject-signal");
p.encounter("predicate-signal");
p.encounter("predicate-signal");
p.encounter("predicate-signal");
o.encounter("object-signal");
o.encounter("object-signal");
o.encounter("object-signal");
(s, p, o)
}
#[test]
fn schema_constants() {
assert_eq!(PLANE_BINARY_BYTES, 2048);
assert_eq!(SOAKING_DIMS, 16_384);
assert_eq!(SIGMA_MASK_BYTES, 2048);
}
#[test]
fn gate_state_lifecycle_methods() {
assert!(GateState::Form.can_write_soaking());
assert!(!GateState::Form.is_serving());
assert!(GateState::Flow.is_serving());
assert!(!GateState::Flow.can_write_soaking());
assert!(!GateState::Freeze.is_serving());
assert!(!GateState::Freeze.can_write_soaking());
}
#[test]
fn gate_state_repr_values() {
assert_eq!(GateState::Form as u8, 0);
assert_eq!(GateState::Flow as u8, 1);
assert_eq!(GateState::Freeze as u8, 2);
}
#[test]
fn bind_node_v2_new_starts_in_form() {
let (mut s, mut p, mut o) = make_test_planes();
let node = BindNodeV2::new(&mut s, &mut p, &mut o, "test");
assert_eq!(node.gate_state, GateState::Form);
assert!(node.subject_soaking.is_some());
assert!(node.predicate_soaking.is_some());
assert!(node.object_soaking.is_some());
assert_eq!(node.role_provenance, "test");
}
#[test]
fn bind_node_v2_soaking_length() {
let (mut s, mut p, mut o) = make_test_planes();
let node = BindNodeV2::new(&mut s, &mut p, &mut o, "test");
assert_eq!(node.subject_soaking.as_ref().unwrap().len(), SOAKING_DIMS);
assert_eq!(node.predicate_soaking.as_ref().unwrap().len(), SOAKING_DIMS);
assert_eq!(node.object_soaking.as_ref().unwrap().len(), SOAKING_DIMS);
}
#[test]
fn bind_node_v2_spo_xor_is_correct() {
let (mut s, mut p, mut o) = make_test_planes();
let node = BindNodeV2::new(&mut s, &mut p, &mut o, "test");
let expected = BindNodeV2::compute_spo_xor(
&node.subject_binary,
&node.predicate_binary,
&node.object_binary,
);
assert_eq!(node.spo_binary, expected);
assert_eq!(node.spo_xor(), expected);
}
#[test]
fn bind_node_v2_crystallize_lifecycle() {
let (mut s, mut p, mut o) = make_test_planes();
let mut node = BindNodeV2::new(&mut s, &mut p, &mut o, "test");
// Before crystallize: Form state, soaking present
assert_eq!(node.gate_state, GateState::Form);
assert!(node.subject_soaking.is_some());
// Crystallize: Form -> Flow
node.crystallize();
assert_eq!(node.gate_state, GateState::Flow);
assert!(node.subject_soaking.is_none());
assert!(node.predicate_soaking.is_none());
assert!(node.object_soaking.is_none());
// Double-crystallize is a no-op
let binary_before = node.subject_binary;
node.crystallize();
assert_eq!(node.gate_state, GateState::Flow);
assert_eq!(node.subject_binary, binary_before);
}
#[test]
fn bind_node_v2_freeze_lifecycle() {
let (mut s, mut p, mut o) = make_test_planes();
let mut node = BindNodeV2::new(&mut s, &mut p, &mut o, "test");
// Cannot freeze directly from Form
node.freeze();
assert_eq!(node.gate_state, GateState::Form);
// Must crystallize first
node.crystallize();
assert_eq!(node.gate_state, GateState::Flow);
node.freeze();
assert_eq!(node.gate_state, GateState::Freeze);
// Double-freeze is a no-op
node.freeze();
assert_eq!(node.gate_state, GateState::Freeze);
}
#[test]
fn bind_node_v2_full_lifecycle() {
let (mut s, mut p, mut o) = make_test_planes();
let mut node = BindNodeV2::new(&mut s, &mut p, &mut o, "lifecycle");
assert!(node.gate_state.can_write_soaking());
assert!(!node.gate_state.is_serving());
node.crystallize();
assert!(!node.gate_state.can_write_soaking());
assert!(node.gate_state.is_serving());
node.freeze();
assert!(!node.gate_state.can_write_soaking());
assert!(!node.gate_state.is_serving());
assert_eq!(node.gate_state, GateState::Freeze);
}
#[test]
fn bind_node_v2_hamming_self_zero() {
let (mut s, mut p, mut o) = make_test_planes();
let node = BindNodeV2::new(&mut s, &mut p, &mut o, "test");