Skip to content

Commit 79e47f6

Browse files
committed
feat: add Merkle root addon for ClamPath Eineindeutigkeit resolution
ClamPath (24b) + MerkleRoot (40b) = word[0] of CogRecord8K. Self-healing Redis: same content = same hash = same key. Eineindeutigkeit: three query paths converge to one address.
1 parent 4b8bef4 commit 79e47f6

1 file changed

Lines changed: 227 additions & 0 deletions

File tree

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
# Addendum 04a: Merkle Root for Eineindeutigkeit Resolution
2+
3+
## Add to: `src/spo/clam_path.rs` (Phase 5, currently building)
4+
5+
## The Problem
6+
7+
Three queries converge on the same concept — semantic, explicit, and relational
8+
all land in same σ-bands. That's Eineindeutigkeit (unique determination).
9+
But WHERE does the concept live?
10+
11+
Without content-addressing, the address depends on which query found it first.
12+
Three paths → three ClamPath addresses → three Redis keys → **duplication**.
13+
14+
## The Solution: ClamPath + MerkleRoot in word[0]
15+
16+
```
17+
word[0] of CogRecord8K (u64, 64 bits):
18+
┌────────────────────┬────────────────────────────────────┐
19+
│ ClamPath (24 bits) │ MerkleRoot (40 bits) │
20+
│ HOW you got here │ WHAT lives here │
21+
│ navigation/lineage │ identity/canonical address │
22+
└────────────────────┴────────────────────────────────────┘
23+
24+
ClamPath = structural path through B-tree (depth + split decisions)
25+
MerkleRoot = truncated blake3 of content fingerprint → canonical identity
26+
```
27+
28+
## Why Both
29+
30+
```
31+
ClamPath alone: path-dependent → same concept gets different addresses
32+
MerkleRoot alone: no lineage, no subtree range queries, no causality chains
33+
Together: navigate via ClamPath, resolve identity via MerkleRoot
34+
```
35+
36+
## MerkleRoot Derivation
37+
38+
The Merkle root is derived from the three-plane binary fingerprints.
39+
This is deterministic: same concept = same fingerprint = same root.
40+
41+
```rust
42+
use blake3;
43+
44+
/// Truncated Merkle root for content-addressed identity.
45+
/// 40 bits = ~1 trillion collision space. Sufficient for BindSpace.
46+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
47+
pub struct MerkleRoot(pub u64); // only lower 40 bits used
48+
49+
impl MerkleRoot {
50+
/// Derive from three-plane binary fingerprints.
51+
/// The canonical address IS the content hash.
52+
pub fn from_planes(
53+
s_binary: &[u8; 2048], // 16384-bit S-plane
54+
p_binary: &[u8; 2048], // 16384-bit P-plane
55+
o_binary: &[u8; 2048], // 16384-bit O-plane
56+
) -> Self {
57+
// Hash each plane separately, then combine.
58+
// This is a Merkle tree with 3 leaves.
59+
let s_hash = blake3::hash(s_binary);
60+
let p_hash = blake3::hash(p_binary);
61+
let o_hash = blake3::hash(o_binary);
62+
63+
// Combine: hash(S || P || O) — order matters (S < P < O is canonical)
64+
let mut hasher = blake3::Hasher::new();
65+
hasher.update(s_hash.as_bytes());
66+
hasher.update(p_hash.as_bytes());
67+
hasher.update(o_hash.as_bytes());
68+
let root = hasher.finalize();
69+
70+
// Truncate to 40 bits
71+
let bytes = root.as_bytes();
72+
let val = u64::from_le_bytes([
73+
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], 0, 0, 0,
74+
]);
75+
MerkleRoot(val & 0xFF_FFFF_FFFF) // mask to 40 bits
76+
}
77+
78+
/// Derive from single composite fingerprint (backward compat).
79+
/// Used when planes aren't separated yet.
80+
pub fn from_fingerprint(fp: &[u8; 2048]) -> Self {
81+
let hash = blake3::hash(fp);
82+
let bytes = hash.as_bytes();
83+
let val = u64::from_le_bytes([
84+
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], 0, 0, 0,
85+
]);
86+
MerkleRoot(val & 0xFF_FFFF_FFFF)
87+
}
88+
89+
/// The Redis key for this concept's canonical address.
90+
pub fn redis_key(&self) -> String {
91+
format!("ada:bind:{:010x}", self.0)
92+
}
93+
}
94+
```
95+
96+
## Packing into word[0]
97+
98+
```rust
99+
impl ClamPath {
100+
/// Pack ClamPath (24 bits) + MerkleRoot (40 bits) into a single u64.
101+
pub fn pack_with_merkle(&self, root: MerkleRoot) -> u64 {
102+
let clam_bits = self.to_u24() as u64; // 24 bits
103+
let merkle_bits = root.0 & 0xFF_FFFF_FFFF; // 40 bits
104+
(clam_bits << 40) | merkle_bits
105+
}
106+
107+
/// Unpack from word[0].
108+
pub fn unpack_with_merkle(word0: u64) -> (ClamPath, MerkleRoot) {
109+
let clam_bits = (word0 >> 40) as u32; // upper 24 bits
110+
let merkle_bits = word0 & 0xFF_FFFF_FFFF; // lower 40 bits
111+
(ClamPath::from_u24(clam_bits), MerkleRoot(merkle_bits))
112+
}
113+
}
114+
```
115+
116+
## Self-Healing Redis Address
117+
118+
This is the key insight for distributed operation:
119+
120+
```
121+
1. Concept arrives → three-plane fingerprints computed
122+
2. MerkleRoot derived deterministically: blake3(S || P || O)
123+
3. Redis key = ada:bind:{merkle_root_hex}
124+
4. ClamPath = structural location in B-tree
125+
126+
If Redis evicts the key:
127+
- Recompute MerkleRoot from fingerprints (deterministic)
128+
- Key is reconstructed: same content → same hash → same address
129+
- Tree heals itself
130+
131+
If two paths find the same concept:
132+
- Both compute same MerkleRoot (content-addressed)
133+
- Both write to same Redis key
134+
- Eineindeutigkeit: last-write-wins is fine because content is identical
135+
- ClamPaths may differ (different navigation routes) — that's OK,
136+
ClamPath is HOW, MerkleRoot is WHAT
137+
138+
If concept is modified (evidence updates the soaking register):
139+
- MerkleRoot changes (new content → new hash)
140+
- Old key naturally expires (TTL or eviction)
141+
- New key created at new address
142+
- No dangling references: anyone who had the old root will miss,
143+
recompute from current fingerprints, find the new address
144+
```
145+
146+
## Merkle Tree for Subtree Integrity (Future)
147+
148+
When the hive goes multi-writer, extend the per-concept MerkleRoot
149+
to a full Merkle tree over the CLAM subtree:
150+
151+
```
152+
root_hash
153+
/ \
154+
left_hash right_hash
155+
/ \ / \
156+
leaf_0 leaf_1 leaf_2 leaf_3
157+
(concept fingerprints)
158+
```
159+
160+
Each CLAM split level has a hash that summarizes its children.
161+
To verify a subtree after network partition:
162+
163+
```rust
164+
/// Verify subtree integrity after reconnection.
165+
/// Compare roots — if they match, entire subtree is consistent.
166+
/// If they diverge, walk down to find the first differing leaf.
167+
pub fn verify_subtree(
168+
local_root: MerkleRoot,
169+
remote_root: MerkleRoot,
170+
clam_path: &ClamPath,
171+
) -> SubtreeStatus {
172+
if local_root == remote_root {
173+
SubtreeStatus::Consistent
174+
} else {
175+
// Walk the Merkle tree to find divergence point
176+
SubtreeStatus::Diverged { at_depth: /* compare level by level */ }
177+
}
178+
}
179+
```
180+
181+
But this is ice cake 21. For now: per-concept MerkleRoot in word[0] is sufficient.
182+
183+
## Dependency
184+
185+
```toml
186+
# Cargo.toml
187+
blake3 = "1"
188+
```
189+
190+
Blake3 is fast (~1GB/s on modern CPUs), deterministic, and the truncated
191+
40-bit output is sufficient for ~1 trillion address space.
192+
193+
## Integration with Existing ClamPath
194+
195+
The existing `04_btree_clam_path_lineage.md` spec defines ClamPath as u16 (16 bits)
196+
with depth in a separate u8. Repack as u24 to fit alongside MerkleRoot in word[0]:
197+
198+
```rust
199+
impl ClamPath {
200+
/// Pack bits (u16) + depth (u8) into 24 bits.
201+
pub fn to_u24(&self) -> u32 {
202+
((self.depth as u32) << 16) | (self.bits as u32)
203+
}
204+
205+
/// Unpack from 24 bits.
206+
pub fn from_u24(packed: u32) -> Self {
207+
ClamPath {
208+
bits: (packed & 0xFFFF) as u16,
209+
depth: ((packed >> 16) & 0xFF) as u8,
210+
}
211+
}
212+
}
213+
```
214+
215+
## Summary
216+
217+
| Component | Bits | Purpose |
218+
|-----------|------|---------|
219+
| ClamPath.bits | 16 | B-tree split decisions (navigation) |
220+
| ClamPath.depth | 8 | Valid bit count (confidence/resolution) |
221+
| MerkleRoot | 40 | Content-addressed canonical identity |
222+
| **Total** | **64** | **= word[0] of CogRecord8K** |
223+
224+
ClamPath = HOW you got here.
225+
MerkleRoot = WHAT lives here.
226+
Redis key = WHERE it's stored.
227+
Self-healing = WHY it works distributed.

0 commit comments

Comments
 (0)