neleus-db 0.2.0

Local-first Merkle-DAG database for AI agents with cryptographic proofs and immutable versioning
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
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
use anyhow::{Result, anyhow};
use serde::{Deserialize, Serialize};

use crate::canonical::to_cbor;
use crate::clock::now_unix;
use crate::hash::{Hash, hash_typed};
use crate::object_store::ObjectStore;
use crate::state::StateRoot;

const COMMIT_TAG: &[u8] = b"commit:";
const COMMIT_PAYLOAD_TAG: &[u8] = b"commit_payload:";
const COMMIT_SCHEMA_VERSION: u32 = 1;

pub type CommitHash = Hash;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommitSignature {
    pub scheme: String,
    pub key_id: Option<String>,
    pub signature: Vec<u8>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Commit {
    pub schema_version: u32,
    pub parents: Vec<CommitHash>,
    pub timestamp: u64,
    pub author: String,
    pub message: String,
    pub state_root: StateRoot,
    pub manifests: Vec<Hash>,
    #[serde(default)]
    pub signature: Option<CommitSignature>,
    /// `hash_typed(COMMIT_PAYLOAD_TAG, &to_cbor(&unsigned))` where `unsigned`
    /// is this commit with `signature` and `payload_hash` both set to `None`.
    /// Present only on signed commits; verifiers MUST re-derive and compare.
    ///
    /// `skip_serializing_if` keeps unsigned commits byte-identical to commits
    /// produced by code that predates this field: `None` is encoded by
    /// omitting the key rather than by an explicit `null`. Without this,
    /// adding the field would change every existing unsigned commit's hash.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub payload_hash: Option<Hash>,
}

impl Commit {
    /// Compute the payload hash that a signer signs / a verifier checks.
    /// Strips `signature` and `payload_hash` from `self` to reconstruct the
    /// canonical unsigned form, then hashes its DAG-CBOR encoding.
    pub fn unsigned_payload_hash(&self) -> Result<Hash> {
        let unsigned = Commit {
            signature: None,
            payload_hash: None,
            ..self.clone()
        };
        Ok(hash_typed(COMMIT_PAYLOAD_TAG, &to_cbor(&unsigned)?))
    }
}

pub trait CommitVerifier {
    /// `payload_hash` has already been re-derived from `commit` and confirmed
    /// to match the value stored on the commit. Implementations should perform
    /// only the cryptographic signature check against this hash.
    fn verify(&self, commit_hash: CommitHash, commit: &Commit, payload_hash: Hash) -> Result<()>;
}

pub trait CommitSigner {
    fn sign(&self, payload_hash: Hash, commit: &Commit) -> Result<CommitSignature>;
}

#[derive(Clone, Debug)]
pub struct CommitStore {
    objects: ObjectStore,
}

impl CommitStore {
    pub fn new(objects: ObjectStore) -> Self {
        Self { objects }
    }

    pub fn create_commit(
        &self,
        parents: Vec<CommitHash>,
        state_root: StateRoot,
        manifests: Vec<Hash>,
        author: String,
        message: String,
    ) -> Result<CommitHash> {
        self.validate_references(state_root, &manifests)?;
        let commit = Commit {
            schema_version: COMMIT_SCHEMA_VERSION,
            parents,
            timestamp: now_unix()?,
            author,
            message,
            state_root,
            manifests,
            signature: None,
            payload_hash: None,
        };
        self.objects.put_serialized(COMMIT_TAG, &commit)
    }

    pub fn create_signed_commit<S: CommitSigner>(
        &self,
        signer: &S,
        parents: Vec<CommitHash>,
        state_root: StateRoot,
        manifests: Vec<Hash>,
        author: String,
        message: String,
    ) -> Result<CommitHash> {
        self.validate_references(state_root, &manifests)?;
        let unsigned = Commit {
            schema_version: COMMIT_SCHEMA_VERSION,
            parents,
            timestamp: now_unix()?,
            author,
            message,
            state_root,
            manifests,
            signature: None,
            payload_hash: None,
        };
        let payload_hash = unsigned.unsigned_payload_hash()?;
        let signature = signer.sign(payload_hash, &unsigned)?;

        let signed = Commit {
            signature: Some(signature),
            payload_hash: Some(payload_hash),
            ..unsigned
        };
        self.objects.put_serialized(COMMIT_TAG, &signed)
    }

    pub fn get_commit(&self, hash: CommitHash) -> Result<Commit> {
        let commit: Commit = self.objects.get_deserialized_typed(COMMIT_TAG, hash)?;
        Ok(commit)
    }

    pub fn verify_commit_with<V: CommitVerifier>(
        &self,
        hash: CommitHash,
        verifier: &V,
    ) -> Result<()> {
        let commit = self.get_commit(hash)?;
        let stored = commit
            .payload_hash
            .ok_or_else(|| anyhow!("commit {} is not signed (missing payload_hash)", hash))?;
        let expected = commit.unsigned_payload_hash()?;
        if stored != expected {
            return Err(anyhow!(
                "commit {} payload_hash inconsistent with commit body",
                hash
            ));
        }
        verifier.verify(hash, &commit, expected)
    }

    fn validate_references(&self, state_root: StateRoot, manifests: &[Hash]) -> Result<()> {
        if !self.objects.exists(state_root) {
            return Err(anyhow!("state_root {} does not exist", state_root));
        }
        for manifest in manifests {
            if !self.objects.exists(*manifest) {
                return Err(anyhow!("manifest {} does not exist", manifest));
            }
        }
        Ok(())
    }
}

pub fn create_commit(
    store: &CommitStore,
    parents: Vec<CommitHash>,
    state_root: StateRoot,
    manifests: Vec<Hash>,
    author: String,
    message: String,
) -> Result<CommitHash> {
    store.create_commit(parents, state_root, manifests, author, message)
}

pub fn get_commit(store: &CommitStore, hash: CommitHash) -> Result<Commit> {
    store.get_commit(hash)
}

#[cfg(test)]
mod tests {
    use tempfile::TempDir;

    use super::*;
    use crate::blob_store::BlobStore;
    use crate::object_store::ObjectStore;
    use crate::state::StateStore;
    use crate::wal::Wal;

    fn stores(tmp: &TempDir) -> (CommitStore, StateStore, BlobStore) {
        let objects = ObjectStore::new(tmp.path().join("objects"));
        objects.ensure_dir().unwrap();
        let commit_store = CommitStore::new(objects.clone());

        let blobs = BlobStore::new(tmp.path().join("blobs"));
        blobs.ensure_dir().unwrap();

        let state = StateStore::new(objects, blobs.clone(), Wal::new(tmp.path().join("wal")));
        (commit_store, state, blobs)
    }

    /// Regression: adding `payload_hash` to `Commit` must NOT change the
    /// CBOR encoding of an unsigned commit, because that would change every
    /// existing commit hash on disk. `skip_serializing_if = "Option::is_none"`
    /// on `payload_hash` keeps the unsigned shape byte-identical to commits
    /// produced by code that predated the field.
    #[test]
    fn unsigned_commit_cbor_omits_payload_hash() {
        #[derive(serde::Serialize)]
        struct LegacyCommit {
            schema_version: u32,
            parents: Vec<CommitHash>,
            timestamp: u64,
            author: String,
            message: String,
            state_root: StateRoot,
            manifests: Vec<Hash>,
            signature: Option<CommitSignature>,
        }

        let state_root = hash_typed(b"any:", b"r");
        let manifest = hash_typed(b"any:", b"m");

        let new_form = Commit {
            schema_version: COMMIT_SCHEMA_VERSION,
            parents: vec![],
            timestamp: 1700000000,
            author: "agent".into(),
            message: "msg".into(),
            state_root,
            manifests: vec![manifest],
            signature: None,
            payload_hash: None,
        };

        let legacy_form = LegacyCommit {
            schema_version: COMMIT_SCHEMA_VERSION,
            parents: vec![],
            timestamp: 1700000000,
            author: "agent".into(),
            message: "msg".into(),
            state_root,
            manifests: vec![manifest],
            signature: None,
        };

        let new_bytes = to_cbor(&new_form).unwrap();
        let legacy_bytes = to_cbor(&legacy_form).unwrap();
        assert_eq!(
            new_bytes, legacy_bytes,
            "unsigned commit encoding must match the pre-payload_hash shape"
        );
    }

    #[test]
    fn commit_create_get_roundtrip() {
        let tmp = TempDir::new().unwrap();
        let (cs, state, _) = stores(&tmp);
        let root = state.empty_root().unwrap();
        let h = cs
            .create_commit(vec![], root, vec![], "agent".into(), "msg".into())
            .unwrap();
        let c = cs.get_commit(h).unwrap();
        assert_eq!(c.author, "agent");
        assert_eq!(c.message, "msg");
        assert_eq!(c.schema_version, COMMIT_SCHEMA_VERSION);
    }

    #[test]
    fn commit_hash_changes_with_message() {
        let tmp = TempDir::new().unwrap();
        let (cs, state, _) = stores(&tmp);
        let root = state.empty_root().unwrap();
        let a = cs
            .create_commit(vec![], root, vec![], "a".into(), "m1".into())
            .unwrap();
        let b = cs
            .create_commit(vec![], root, vec![], "a".into(), "m2".into())
            .unwrap();
        assert_ne!(a, b);
    }

    #[test]
    fn commit_parent_reference_preserved() {
        let tmp = TempDir::new().unwrap();
        let (cs, state, _) = stores(&tmp);
        let root = state.empty_root().unwrap();
        let p = cs
            .create_commit(vec![], root, vec![], "a".into(), "p".into())
            .unwrap();
        let c = cs
            .create_commit(vec![p], root, vec![], "a".into(), "c".into())
            .unwrap();
        let out = cs.get_commit(c).unwrap();
        assert_eq!(out.parents, vec![p]);
    }

    #[test]
    fn commit_can_reference_manifests() {
        let tmp = TempDir::new().unwrap();
        let (cs, state, _blobs) = stores(&tmp);
        let root = state.empty_root().unwrap();
        let manifest_hash = state.set(root, b"manifest", b"ref").unwrap();
        let c = cs
            .create_commit(
                vec![],
                root,
                vec![manifest_hash],
                "agent".into(),
                "with manifest".into(),
            )
            .unwrap();
        let out = cs.get_commit(c).unwrap();
        assert_eq!(out.manifests, vec![manifest_hash]);
    }

    #[test]
    fn commit_timestamp_nonzero() {
        let tmp = TempDir::new().unwrap();
        let (cs, state, _) = stores(&tmp);
        let root = state.empty_root().unwrap();
        let h = cs
            .create_commit(vec![], root, vec![], "agent".into(), "msg".into())
            .unwrap();
        let c = cs.get_commit(h).unwrap();
        assert!(c.timestamp > 0);
    }

    #[test]
    fn commit_free_functions_work() {
        let tmp = TempDir::new().unwrap();
        let (cs, state, _) = stores(&tmp);
        let root = state.empty_root().unwrap();

        let h = super::create_commit(&cs, vec![], root, vec![], "a".into(), "m".into()).unwrap();
        let c = super::get_commit(&cs, h).unwrap();
        assert_eq!(c.message, "m");
    }

    /// Real signing scheme used in tests: the signature IS the payload_hash bytes,
    /// keyed by `k1`. A verifier must reject any tampered commit because the
    /// re-derived `payload_hash` changes when the commit body does.
    struct HashEchoSigner;

    impl CommitSigner for HashEchoSigner {
        fn sign(&self, payload_hash: Hash, _commit: &Commit) -> Result<CommitSignature> {
            Ok(CommitSignature {
                scheme: "hash-echo".into(),
                key_id: Some("k1".into()),
                signature: payload_hash.as_bytes().to_vec(),
            })
        }
    }

    struct HashEchoVerifier;

    impl CommitVerifier for HashEchoVerifier {
        fn verify(&self, _hash: CommitHash, commit: &Commit, payload_hash: Hash) -> Result<()> {
            let sig = commit
                .signature
                .as_ref()
                .ok_or_else(|| anyhow::anyhow!("missing signature"))?;
            if sig.scheme != "hash-echo" {
                return Err(anyhow::anyhow!("unexpected scheme {}", sig.scheme));
            }
            if sig.signature.as_slice() != payload_hash.as_bytes() {
                return Err(anyhow::anyhow!("signature does not match payload_hash"));
            }
            Ok(())
        }
    }

    #[test]
    fn signed_commit_round_trip_verifies() {
        let tmp = TempDir::new().unwrap();
        let (cs, state, _) = stores(&tmp);
        let root = state.empty_root().unwrap();
        let h = cs
            .create_signed_commit(
                &HashEchoSigner,
                vec![],
                root,
                vec![],
                "agent".into(),
                "msg".into(),
            )
            .unwrap();
        let c = cs.get_commit(h).unwrap();
        assert!(c.signature.is_some());
        assert!(c.payload_hash.is_some());
        cs.verify_commit_with(h, &HashEchoVerifier).unwrap();
    }

    #[test]
    fn verifier_rejects_unsigned_commit() {
        let tmp = TempDir::new().unwrap();
        let (cs, state, _) = stores(&tmp);
        let root = state.empty_root().unwrap();
        let h = cs
            .create_commit(vec![], root, vec![], "agent".into(), "msg".into())
            .unwrap();
        let err = cs.verify_commit_with(h, &HashEchoVerifier).unwrap_err();
        assert!(err.to_string().contains("not signed"));
    }

    /// Tampering with any field of a signed commit must invalidate it.
    /// Demonstrates that storing `payload_hash` alongside the signature does
    /// not allow an attacker to forge by mutating other fields.
    #[test]
    fn verifier_rejects_tampered_commit_body() {
        let tmp = TempDir::new().unwrap();
        let (cs, state, _) = stores(&tmp);
        let root = state.empty_root().unwrap();
        let h = cs
            .create_signed_commit(
                &HashEchoSigner,
                vec![],
                root,
                vec![],
                "agent".into(),
                "original message".into(),
            )
            .unwrap();

        let mut c = cs.get_commit(h).unwrap();
        let original_payload_hash = c.payload_hash.unwrap();
        c.message = "tampered message".into();

        // payload_hash is now stale relative to the body. The verifier path's
        // consistency check (inside verify_commit_with) is what catches this;
        // here we simulate by calling the helper directly.
        let recomputed = c.unsigned_payload_hash().unwrap();
        assert_ne!(recomputed, original_payload_hash);
    }

    /// Stored payload_hash MUST equal the re-derived hash, even if a signature
    /// would still verify against the stored payload_hash. Otherwise an attacker
    /// could craft a Commit whose body says X but whose signed payload_hash
    /// covers a different X'.
    #[test]
    fn verifier_rejects_inconsistent_payload_hash() {
        use crate::object_store::ObjectStore;

        let tmp = TempDir::new().unwrap();
        let objects = ObjectStore::new(tmp.path().join("objects"));
        objects.ensure_dir().unwrap();
        let cs = CommitStore::new(objects.clone());
        let state_objects = ObjectStore::new(tmp.path().join("objects"));
        let blobs = BlobStore::new(tmp.path().join("blobs"));
        blobs.ensure_dir().unwrap();
        let state = StateStore::new(state_objects, blobs, Wal::new(tmp.path().join("wal")));
        let root = state.empty_root().unwrap();

        // Hand-craft a forged commit: body says "real" but payload_hash
        // covers a different (signed) payload.
        let forged = Commit {
            schema_version: COMMIT_SCHEMA_VERSION,
            parents: vec![],
            timestamp: 1,
            author: "attacker".into(),
            message: "real".into(),
            state_root: root,
            manifests: vec![],
            signature: Some(CommitSignature {
                scheme: "hash-echo".into(),
                key_id: Some("k1".into()),
                signature: hash_typed(b"other:", b"x").as_bytes().to_vec(),
            }),
            payload_hash: Some(hash_typed(b"other:", b"x")),
        };
        let h = objects.put_serialized(COMMIT_TAG, &forged).unwrap();
        let err = cs.verify_commit_with(h, &HashEchoVerifier).unwrap_err();
        assert!(err.to_string().contains("inconsistent"));
    }

    #[test]
    fn create_commit_rejects_missing_state_root() {
        let tmp = TempDir::new().unwrap();
        let (cs, _state, _) = stores(&tmp);
        let missing_root = hash_typed(b"missing:", b"state");
        let err = cs
            .create_commit(vec![], missing_root, vec![], "agent".into(), "msg".into())
            .unwrap_err();
        assert!(err.to_string().contains("state_root"));
    }

    #[test]
    fn create_commit_rejects_missing_manifest() {
        let tmp = TempDir::new().unwrap();
        let (cs, state, _) = stores(&tmp);
        let root = state.empty_root().unwrap();
        let missing_manifest = hash_typed(b"missing:", b"manifest");
        let err = cs
            .create_commit(
                vec![],
                root,
                vec![missing_manifest],
                "agent".into(),
                "msg".into(),
            )
            .unwrap_err();
        assert!(err.to_string().contains("manifest"));
    }
}