engram-core 0.21.1

AI Memory Infrastructure - Persistent memory for AI agents with semantic search
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
//! Attestation chain — blockchain-style chained records
//!
//! Manages an append-only chain of attestation records. Each record includes a
//! `previous_hash` pointing to the preceding record, forming a tamper-evident
//! chain similar to a blockchain.

use chrono::Utc;
use sha2::{Digest, Sha256};
use subtle::ConstantTimeEq;

use crate::error::{EngramError, Result};
use crate::storage::Storage;

use super::types::{AttestationFilter, AttestationRecord, ChainStatus};

/// Genesis sentinel used as `previous_hash` for the first record.
///
/// This is a well-known constant, not a random or signed commitment. It means
/// the chain has no cryptographic anchor to a trusted root: an adversary with
/// direct DB access can insert a plausible first record with
/// `previous_hash = "genesis"` and a valid `record_hash`, indistinguishable
/// from a legitimate genesis record.
///
/// For stronger guarantees, replace this with a per-chain random nonce
/// generated at chain creation time and stored out-of-band (e.g. in a
/// separate `attestation_config` table).
const GENESIS_HASH: &str = "genesis";

/// Maximum allowed document size (100 MB). Documents larger than this would
/// block the thread synchronously during SHA-256 hashing.
const MAX_DOCUMENT_BYTES: usize = 100 * 1024 * 1024;

/// Maximum number of memory IDs per attestation record. Larger slices cause
/// unbounded JSON serialization.
const MAX_MEMORY_IDS: usize = 10_000;

/// Manages the append-only attestation chain
pub struct AttestationChain {
    storage: Storage,
}

impl AttestationChain {
    /// Create a new chain backed by the given storage
    pub fn new(storage: Storage) -> Self {
        Self { storage }
    }

    /// Log a document ingestion, creating a chained attestation record.
    ///
    /// Steps:
    /// 1. Compute SHA-256 hash of `content`
    /// 2. Get the last record's `record_hash` (or `"genesis"` if first)
    /// 3. Build the record with `previous_hash` = last record's hash
    /// 4. Compute `record_hash` = SHA-256 of the canonical representation
    /// 5. Optionally sign with Ed25519
    /// 6. Insert into `attestation_log` and return the created record
    pub fn log_document(
        &self,
        content: &[u8],
        document_name: &str,
        agent_id: Option<&str>,
        memory_ids: &[i64],
        sign_key: Option<&[u8; 32]>,
    ) -> Result<AttestationRecord> {
        if document_name.trim().is_empty() {
            return Err(EngramError::InvalidInput(
                "document_name must not be empty".to_string(),
            ));
        }
        if document_name.len() > 1_000 {
            return Err(EngramError::InvalidInput(
                "document_name too long (max 1000 characters)".to_string(),
            ));
        }
        if document_name.contains('\0') {
            return Err(EngramError::InvalidInput(
                "document_name must not contain null bytes".to_string(),
            ));
        }

        if let Some(id) = agent_id {
            if id.len() > 256 {
                return Err(EngramError::InvalidInput(
                    "agent_id must not exceed 256 characters".to_string(),
                ));
            }
            if id.chars().any(|c| c.is_control()) {
                return Err(EngramError::InvalidInput(
                    "agent_id must not contain control characters".to_string(),
                ));
            }
        }

        // M1: reject documents that would block the thread during hashing
        if content.len() > MAX_DOCUMENT_BYTES {
            return Err(EngramError::InvalidInput(format!(
                "document too large: {} bytes (max {})",
                content.len(),
                MAX_DOCUMENT_BYTES
            )));
        }

        // M2: reject oversized or negative memory_ids
        if memory_ids.len() > MAX_MEMORY_IDS {
            return Err(EngramError::InvalidInput(format!(
                "too many memory_ids: {} (max {})",
                memory_ids.len(),
                MAX_MEMORY_IDS
            )));
        }
        if memory_ids.iter().any(|&id| id < 0) {
            return Err(EngramError::InvalidInput(
                "memory_ids must be non-negative".to_string(),
            ));
        }

        let document_hash = hash_bytes(content);
        let document_size = content.len();
        let ingested_at = Utc::now();
        let memory_ids_vec: Vec<i64> = memory_ids.to_vec();
        let agent_id_owned = agent_id.map(str::to_string);
        let sign_key_owned = sign_key.copied();

        self.storage.with_transaction(|conn| {
            let previous_hash: String = {
                let mut stmt = conn
                    .prepare("SELECT record_hash FROM attestation_log ORDER BY id DESC LIMIT 1")?;
                let mut rows = stmt.query([])?;
                match rows.next()? {
                    Some(row) => row.get(0)?,
                    None => GENESIS_HASH.to_string(),
                }
            };

            let mut record = AttestationRecord {
                id: None,
                document_hash,
                document_name: document_name.to_string(),
                document_size,
                ingested_at,
                agent_id: agent_id_owned,
                memory_ids: memory_ids_vec,
                previous_hash,
                record_hash: String::new(),
                signature: None,
                metadata: serde_json::Value::Object(serde_json::Map::new()),
                created_at: None,
            };

            record.record_hash = Self::compute_record_hash(&record);

            // M3 (key zeroization): `SigningKey` is `ZeroizeOnDrop` in ed25519-dalek 2.x,
            // so the copy created from `key_bytes` is zeroed when it drops here.
            // The caller's source buffer (the `sign_key: Option<&[u8; 32]>` argument)
            // is NOT zeroed automatically. Callers that own the key material should
            // wrap it in `zeroize::Zeroizing<[u8; 32]>` to ensure the source is also
            // cleared after use.
            if let Some(key_bytes) = sign_key_owned.as_ref() {
                record.signature = Some(sign_record_hash(&record.record_hash, key_bytes)?);
            }

            let memory_ids_json =
                serde_json::to_string(&record.memory_ids).map_err(EngramError::Serialization)?;
            let metadata_json =
                serde_json::to_string(&record.metadata).map_err(EngramError::Serialization)?;

            // M4: guard against unbounded metadata at the insert boundary.
            // This is the single point all paths must pass through, so future
            // callers that set metadata on the record before insertion are
            // automatically covered.
            const MAX_METADATA_BYTES: usize = 65_536;
            if metadata_json.len() > MAX_METADATA_BYTES {
                return Err(EngramError::InvalidInput(format!(
                    "metadata too large: {} bytes (max {})",
                    metadata_json.len(),
                    MAX_METADATA_BYTES
                )));
            }

            conn.execute(
                "INSERT INTO attestation_log
                    (document_hash, document_name, document_size, ingested_at,
                     agent_id, memory_ids, previous_hash, record_hash, signature, metadata)
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
                rusqlite::params![
                    record.document_hash,
                    record.document_name,
                    record.document_size as i64,
                    record.ingested_at.to_rfc3339(),
                    record.agent_id,
                    memory_ids_json,
                    record.previous_hash,
                    record.record_hash,
                    record.signature,
                    metadata_json,
                ],
            )?;

            let id = conn.last_insert_rowid();
            let created_at_str: String = conn.query_row(
                "SELECT created_at FROM attestation_log WHERE id = ?1",
                rusqlite::params![id],
                |row| row.get(0),
            )?;
            let created_at = chrono::DateTime::parse_from_rfc3339(&created_at_str)
                .map(|dt| dt.with_timezone(&Utc))
                .ok();

            Ok(AttestationRecord {
                id: Some(id),
                created_at,
                ..record
            })
        })
    }

    /// Check whether a document (by content) has already been attested.
    ///
    /// Returns the first matching record, or `None`.
    pub fn verify_document(&self, content: &[u8]) -> Result<Option<AttestationRecord>> {
        let hash = hash_bytes(content);
        self.storage.with_connection(|conn| {
            let mut stmt = conn.prepare(
                "SELECT id, document_hash, document_name, document_size, ingested_at,
                        agent_id, memory_ids, previous_hash, record_hash, signature,
                        metadata, created_at
                 FROM attestation_log
                 WHERE document_hash = ?1
                 ORDER BY id ASC
                 LIMIT 1",
            )?;
            let mut rows = stmt.query(rusqlite::params![hash])?;
            match rows.next()? {
                Some(row) => Ok(Some(row_to_record(row)?)),
                None => Ok(None),
            }
        })
    }

    /// Verify the integrity of the entire chain.
    ///
    /// Walks all records in insertion order and checks:
    /// - `previous_hash` of each record matches the `record_hash` of the preceding one
    /// - Each `record_hash` is correctly computed from the record's fields
    /// - If `verifying_key` is `Some`, every record must carry a valid Ed25519
    ///   signature for its `record_hash`
    pub fn verify_chain(&self, verifying_key: Option<&[u8; 32]>) -> Result<ChainStatus> {
        // M5: Process rows one at a time (streaming) to avoid loading the entire
        // attestation_log table into memory. Only `expected_previous` and a
        // counter are kept in memory between rows.
        self.storage.with_connection(|conn| {
            let mut stmt = conn.prepare(
                "SELECT id, document_hash, document_name, document_size, ingested_at,
                        agent_id, memory_ids, previous_hash, record_hash, signature,
                        metadata, created_at
                 FROM attestation_log
                 ORDER BY id ASC",
            )?;

            let mut expected_previous = GENESIS_HASH.to_string();
            let mut record_count: usize = 0;
            let mut rows = stmt.query([])?;

            while let Some(row) = rows.next()? {
                let record = row_to_record(row).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        0,
                        rusqlite::types::Type::Text,
                        Box::new(std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            e.to_string(),
                        )),
                    )
                })?;
                record_count += 1;

                // 1. Check linkage (constant-time to prevent timing oracle)
                let linkage_ok: bool = record
                    .previous_hash
                    .as_bytes()
                    .ct_eq(expected_previous.as_bytes())
                    .into();
                if !linkage_ok {
                    return Ok(ChainStatus::Broken {
                        at_record_id: record.id.unwrap_or(-1),
                        expected_hash: expected_previous,
                        actual_hash: record.previous_hash.clone(),
                    });
                }

                // 2. Recompute record_hash and compare (constant-time)
                let recomputed = Self::compute_record_hash(&record);
                let hash_ok: bool = recomputed
                    .as_bytes()
                    .ct_eq(record.record_hash.as_bytes())
                    .into();
                if !hash_ok {
                    return Ok(ChainStatus::Broken {
                        at_record_id: record.id.unwrap_or(-1),
                        expected_hash: recomputed,
                        actual_hash: record.record_hash.clone(),
                    });
                }

                // 3. Verify Ed25519 signature when the caller provides a key.
                if let Some(vk_bytes) = verifying_key {
                    let Some(sig_hex) = record.signature.as_deref() else {
                        return Ok(ChainStatus::Broken {
                            at_record_id: record.id.unwrap_or(-1),
                            expected_hash: format!(
                                "Ed25519 signature for record_hash {}",
                                record.record_hash
                            ),
                            actual_hash: "missing signature".to_string(),
                        });
                    };

                    let valid = verify_signature(&record.record_hash, sig_hex, vk_bytes)?;
                    if !valid {
                        return Ok(ChainStatus::Broken {
                            at_record_id: record.id.unwrap_or(-1),
                            expected_hash: format!(
                                "valid Ed25519 signature for record_hash {}",
                                record.record_hash
                            ),
                            actual_hash: "invalid signature".to_string(),
                        });
                    }
                }

                expected_previous = record.record_hash;
            }

            if record_count == 0 {
                Ok(ChainStatus::Empty)
            } else {
                Ok(ChainStatus::Valid { record_count })
            }
        })
    }

    /// List attestation records with optional filters
    pub fn list(&self, filter: &AttestationFilter) -> Result<Vec<AttestationRecord>> {
        let limit = filter.limit.unwrap_or(100) as i64;
        let offset = filter.offset.unwrap_or(0) as i64;
        let agent_id = filter.agent_id.clone();
        let document_name = filter.document_name.clone();

        self.storage.with_connection(|conn| {
            // Build a flexible query with optional filters
            let mut conditions: Vec<String> = Vec::new();
            let mut param_values: Vec<Box<dyn rusqlite::ToSql>> = Vec::new();

            if let Some(ref aid) = agent_id {
                conditions.push(format!("agent_id = ?{}", param_values.len() + 1));
                param_values.push(Box::new(aid.clone()));
            }
            if let Some(ref name) = document_name {
                conditions.push(format!(
                    "document_name LIKE ?{} ESCAPE '\\'",
                    param_values.len() + 1
                ));
                let escaped = name
                    .replace('\\', "\\\\")
                    .replace('%', "\\%")
                    .replace('_', "\\_");
                param_values.push(Box::new(format!("%{}%", escaped)));
            }

            let where_clause = if conditions.is_empty() {
                String::new()
            } else {
                format!("WHERE {}", conditions.join(" AND "))
            };

            let limit_idx = param_values.len() + 1;
            let offset_idx = param_values.len() + 2;
            param_values.push(Box::new(limit));
            param_values.push(Box::new(offset));

            let sql = format!(
                "SELECT id, document_hash, document_name, document_size, ingested_at,
                        agent_id, memory_ids, previous_hash, record_hash, signature,
                        metadata, created_at
                 FROM attestation_log
                 {}
                 ORDER BY id ASC
                 LIMIT ?{} OFFSET ?{}",
                where_clause, limit_idx, offset_idx
            );

            let mut stmt = conn.prepare(&sql)?;
            let refs: Vec<&dyn rusqlite::ToSql> = param_values.iter().map(|b| b.as_ref()).collect();
            let rows = stmt.query_map(refs.as_slice(), |row| {
                row_to_record(row).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        0,
                        rusqlite::types::Type::Text,
                        Box::new(std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            e.to_string(),
                        )),
                    )
                })
            })?;
            rows.collect::<std::result::Result<Vec<_>, _>>()
                .map_err(EngramError::Database)
        })
    }

    // ─── Private helpers ────────────────────────────────────────────────────

    /// Compute the canonical `record_hash` for a record.
    ///
    /// Hash = SHA-256 of:
    /// `document_hash|document_name|document_size|ingested_at|agent_id|memory_ids|previous_hash`
    pub fn compute_record_hash(record: &AttestationRecord) -> String {
        let canonical = format!(
            "{}|{}|{}|{}|{}|{}|{}",
            record.document_hash,
            record.document_name,
            record.document_size,
            record.ingested_at.to_rfc3339(),
            record.agent_id.as_deref().unwrap_or(""),
            serde_json::to_string(&record.memory_ids).unwrap_or_default(),
            record.previous_hash,
        );
        let mut hasher = Sha256::new();
        hasher.update(canonical.as_bytes());
        format!("sha256:{}", hex::encode(hasher.finalize()))
    }
}

// ─── Standalone helpers ──────────────────────────────────────────────────────

/// Compute SHA-256 hash of raw bytes and return as `"sha256:{hex}"` string
fn hash_bytes(data: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(data);
    format!("sha256:{}", hex::encode(hasher.finalize()))
}

/// Sign the `record_hash` string with an Ed25519 secret key.
/// Returns hex-encoded signature.
///
/// This is only compiled when `agent-portability` is active (which is the only
/// context in which this module exists), so no additional cfg gate is needed.
fn sign_record_hash(record_hash: &str, secret_key_bytes: &[u8; 32]) -> Result<String> {
    use ed25519_dalek::{Signature, Signer, SigningKey};

    let signing_key = SigningKey::from_bytes(secret_key_bytes);
    let signature: Signature = signing_key.sign(record_hash.as_bytes());
    Ok(hex::encode(signature.to_bytes()))
}

/// Verify an Ed25519 signature stored as a hex string.
///
/// Returns `Ok(true)` if valid, `Ok(false)` if the signature is invalid, and
/// `Err` if the key bytes or signature bytes are malformed.
fn verify_signature(
    record_hash: &str,
    signature_hex: &str,
    verifying_key_bytes: &[u8; 32],
) -> Result<bool> {
    use ed25519_dalek::{Signature, Verifier, VerifyingKey};

    let key = VerifyingKey::from_bytes(verifying_key_bytes)
        .map_err(|e| EngramError::InvalidInput(format!("invalid verifying key: {e}")))?;

    let sig_bytes = hex::decode(signature_hex)
        .map_err(|e| EngramError::InvalidInput(format!("invalid signature hex: {e}")))?;

    let sig_array: [u8; 64] = sig_bytes
        .as_slice()
        .try_into()
        .map_err(|_| EngramError::InvalidInput("signature must be 64 bytes".to_string()))?;

    let signature = Signature::from_bytes(&sig_array);
    Ok(key.verify(record_hash.as_bytes(), &signature).is_ok())
}

/// Deserialise a database row into an `AttestationRecord`
fn row_to_record(row: &rusqlite::Row<'_>) -> Result<AttestationRecord> {
    let id: i64 = row.get(0)?;
    let document_hash: String = row.get(1)?;
    let document_name: String = row.get(2)?;
    let document_size: i64 = row.get(3)?;
    let ingested_at_str: String = row.get(4)?;
    let agent_id: Option<String> = row.get(5)?;
    let memory_ids_json: String = row.get(6)?;
    let previous_hash: String = row.get(7)?;
    let record_hash: String = row.get(8)?;
    let signature: Option<String> = row.get(9)?;
    let metadata_json: String = row.get(10)?;
    let created_at_str: Option<String> = row.get(11)?;

    // M6: rusqlite's `Error::display()` does not embed the DB file path in its
    // string representation, so these errors are safe to propagate as-is.
    // However, they should be mapped to generic user-facing messages at the
    // API boundary (MCP handler level) rather than surfaced directly, to avoid
    // leaking internal schema details.
    let ingested_at = chrono::DateTime::parse_from_rfc3339(&ingested_at_str)
        .map(|dt| dt.with_timezone(&Utc))
        .map_err(|e| EngramError::Storage(format!("invalid ingested_at: {e}")))?;

    let memory_ids: Vec<i64> = serde_json::from_str(&memory_ids_json)
        .map_err(|e| EngramError::Storage(format!("invalid memory_ids JSON: {e}")))?;

    let metadata: serde_json::Value = serde_json::from_str(&metadata_json)
        .map_err(|e| EngramError::Storage(format!("invalid metadata JSON: {e}")))?;

    let created_at = created_at_str.and_then(|s| {
        chrono::DateTime::parse_from_rfc3339(&s)
            .map(|dt| dt.with_timezone(&Utc))
            .ok()
    });

    Ok(AttestationRecord {
        id: Some(id),
        document_hash,
        document_name,
        document_size: document_size as usize,
        ingested_at,
        agent_id,
        memory_ids,
        previous_hash,
        record_hash,
        signature,
        metadata,
        created_at,
    })
}

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

    fn test_chain() -> AttestationChain {
        let storage = Storage::open_in_memory().unwrap();
        AttestationChain::new(storage)
    }

    #[test]
    fn test_log_and_verify_document() {
        let chain = test_chain();
        let content = b"hello, world";
        let record = chain
            .log_document(content, "hello.txt", Some("agent-1"), &[1, 2, 3], None)
            .unwrap();

        assert!(record.id.is_some());
        assert_eq!(record.document_name, "hello.txt");
        assert_eq!(record.agent_id.as_deref(), Some("agent-1"));
        assert_eq!(record.memory_ids, vec![1, 2, 3]);
        assert_eq!(record.previous_hash, GENESIS_HASH);
        assert!(!record.record_hash.is_empty());

        // verify_document should find it
        let found = chain.verify_document(content).unwrap();
        assert!(found.is_some());
        assert_eq!(found.unwrap().document_name, "hello.txt");
    }

    #[test]
    fn test_chain_linkage() {
        let chain = test_chain();

        let r1 = chain
            .log_document(b"doc1", "doc1.txt", None, &[], None)
            .unwrap();
        let r2 = chain
            .log_document(b"doc2", "doc2.txt", None, &[], None)
            .unwrap();

        assert_eq!(r1.previous_hash, GENESIS_HASH);
        assert_eq!(r2.previous_hash, r1.record_hash);
    }

    #[test]
    fn test_verify_chain_valid() {
        let chain = test_chain();
        chain.log_document(b"a", "a.txt", None, &[], None).unwrap();
        chain.log_document(b"b", "b.txt", None, &[], None).unwrap();

        match chain.verify_chain(None).unwrap() {
            ChainStatus::Valid { record_count } => assert_eq!(record_count, 2),
            other => panic!("expected Valid, got {other:?}"),
        }
    }

    #[test]
    fn test_verify_chain_empty() {
        let chain = test_chain();
        assert!(matches!(
            chain.verify_chain(None).unwrap(),
            ChainStatus::Empty
        ));
    }

    #[test]
    fn test_chain_stays_linear_under_concurrent_append() {
        use std::sync::Arc;

        let storage = crate::storage::Storage::open_in_memory().unwrap();
        let chain = Arc::new(AttestationChain::new(storage));

        let handles: Vec<_> = (0..4)
            .map(|i| {
                let c = Arc::clone(&chain);
                std::thread::spawn(move || {
                    c.log_document(
                        format!("content-{i}").as_bytes(),
                        &format!("doc-{i}.txt"),
                        None,
                        &[],
                        None,
                    )
                    .unwrap()
                })
            })
            .collect();

        for h in handles {
            h.join().unwrap();
        }

        match chain.verify_chain(None).unwrap() {
            ChainStatus::Valid { record_count } => assert_eq!(record_count, 4),
            other => panic!("expected Valid, got {other:?}"),
        }
    }

    #[test]
    fn test_verify_chain_rejects_tampered_signature() {
        use ed25519_dalek::{SigningKey, VerifyingKey};

        let chain = test_chain();
        let secret = [42u8; 32];
        let verifying_key_bytes: [u8; 32] =
            VerifyingKey::from(&SigningKey::from_bytes(&secret)).to_bytes();

        chain
            .log_document(b"data", "doc.txt", None, &[], Some(&secret))
            .unwrap();

        let zero_signature = "00".repeat(64);
        chain
            .storage
            .with_transaction(|conn| {
                conn.execute(
                    "UPDATE attestation_log SET signature = ?1",
                    rusqlite::params![zero_signature],
                )?;
                Ok(())
            })
            .unwrap();

        match chain.verify_chain(Some(&verifying_key_bytes)).unwrap() {
            ChainStatus::Broken { .. } => {}
            other => panic!("expected Broken, got {other:?}"),
        }
    }

    #[test]
    fn test_verify_chain_rejects_stripped_signature_when_key_provided() {
        use ed25519_dalek::{SigningKey, VerifyingKey};

        let chain = test_chain();
        let secret = [42u8; 32];
        let verifying_key_bytes: [u8; 32] =
            VerifyingKey::from(&SigningKey::from_bytes(&secret)).to_bytes();

        chain
            .log_document(b"data", "doc.txt", None, &[], Some(&secret))
            .unwrap();

        chain
            .storage
            .with_transaction(|conn| {
                conn.execute("UPDATE attestation_log SET signature = NULL", [])?;
                Ok(())
            })
            .unwrap();

        match chain.verify_chain(Some(&verifying_key_bytes)).unwrap() {
            ChainStatus::Broken { .. } => {}
            other => panic!("expected Broken, got {other:?}"),
        }
    }

    #[test]
    fn test_verify_chain_accepts_valid_signature() {
        use ed25519_dalek::{SigningKey, VerifyingKey};

        let chain = test_chain();
        let secret = [42u8; 32];
        let verifying_key_bytes: [u8; 32] =
            VerifyingKey::from(&SigningKey::from_bytes(&secret)).to_bytes();

        chain
            .log_document(b"data", "doc.txt", None, &[], Some(&secret))
            .unwrap();

        match chain.verify_chain(Some(&verifying_key_bytes)).unwrap() {
            ChainStatus::Valid { record_count: 1 } => {}
            other => panic!("expected Valid(1), got {other:?}"),
        }
    }

    #[test]
    fn test_verify_chain_skips_sig_check_when_no_key_provided() {
        let chain = test_chain();
        chain
            .log_document(b"data", "doc.txt", None, &[], None)
            .unwrap();

        match chain.verify_chain(None).unwrap() {
            ChainStatus::Valid { record_count: 1 } => {}
            other => panic!("expected Valid(1), got {other:?}"),
        }
    }

    #[test]
    fn test_list_with_filter() {
        let chain = test_chain();
        chain
            .log_document(b"x", "x.txt", Some("agent-A"), &[], None)
            .unwrap();
        chain
            .log_document(b"y", "y.txt", Some("agent-B"), &[], None)
            .unwrap();

        let filter = AttestationFilter {
            agent_id: Some("agent-A".to_string()),
            ..Default::default()
        };
        let results = chain.list(&filter).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].document_name, "x.txt");
    }

    #[test]
    fn test_empty_document_name_rejected() {
        let chain = test_chain();
        let err = chain.log_document(b"data", "", None, &[], None);
        assert!(err.is_err());
    }

    #[test]
    fn test_list_document_name_filter_no_wildcard_expansion() {
        // H3: LIKE metacharacters in document_name must not expand
        let chain = test_chain();
        chain
            .log_document(b"a", "report_final.txt", None, &[], None)
            .unwrap();
        chain
            .log_document(b"b", "reportXfinal.txt", None, &[], None)
            .unwrap();

        // Searching for "report_final" should only match "report_final.txt",
        // NOT "reportXfinal.txt" (SQLite `_` is a single-char wildcard if unescaped)
        let filter = super::super::types::AttestationFilter {
            document_name: Some("report_final".to_string()),
            ..Default::default()
        };
        let results = chain.list(&filter).unwrap();
        assert_eq!(
            results.len(),
            1,
            "unescaped _ should not act as wildcard; got {results:?}"
        );
        assert_eq!(results[0].document_name, "report_final.txt");
    }

    #[test]
    fn test_list_document_name_filter_percent_literal() {
        // H3: literal % in document_name filter must be matched as literal
        let chain = test_chain();
        chain
            .log_document(b"a", "100% complete.txt", None, &[], None)
            .unwrap();
        chain
            .log_document(b"b", "other.txt", None, &[], None)
            .unwrap();

        let filter = super::super::types::AttestationFilter {
            document_name: Some("100%".to_string()),
            ..Default::default()
        };
        let results = chain.list(&filter).unwrap();
        assert_eq!(
            results.len(),
            1,
            "percent should be literal, got {results:?}"
        );
        assert_eq!(results[0].document_name, "100% complete.txt");
    }

    #[test]
    fn test_agent_id_too_long_rejected() {
        // H4: agent_id > 256 chars must be rejected
        let chain = test_chain();
        let long_id = "a".repeat(257);
        let err = chain.log_document(b"data", "doc.txt", Some(&long_id), &[], None);
        assert!(err.is_err(), "agent_id > 256 chars should be rejected");
    }

    #[test]
    fn test_agent_id_control_char_rejected() {
        // H4: agent_id with control characters must be rejected
        let chain = test_chain();
        let bad_id = "agent\x00null";
        let err = chain.log_document(b"data", "doc.txt", Some(bad_id), &[], None);
        assert!(
            err.is_err(),
            "agent_id with control chars should be rejected"
        );
    }

    #[test]
    fn test_agent_id_valid_256_chars_accepted() {
        // H4: exactly 256 chars is valid
        let chain = test_chain();
        let id_256 = "a".repeat(256);
        let result = chain.log_document(b"data", "doc.txt", Some(&id_256), &[], None);
        assert!(result.is_ok(), "agent_id of 256 chars should be accepted");
    }

    #[test]
    fn test_compute_record_hash_deterministic() {
        let chain = test_chain();
        let r = chain
            .log_document(b"stable", "stable.txt", None, &[], None)
            .unwrap();
        let recomputed = AttestationChain::compute_record_hash(&r);
        assert_eq!(r.record_hash, recomputed);
    }

    #[test]
    fn test_chain_integrity_5_docs() {
        let chain = test_chain();

        // Log 5 documents
        let docs = [
            (b"alpha" as &[u8], "alpha.txt"),
            (b"beta", "beta.txt"),
            (b"gamma", "gamma.txt"),
            (b"delta", "delta.txt"),
            (b"epsilon", "epsilon.txt"),
        ];

        let mut records = Vec::new();
        for (content, name) in &docs {
            let r = chain
                .log_document(content, name, Some("agent-x"), &[], None)
                .unwrap();
            records.push(r);
        }

        // Verify each record's previous_hash forms a proper chain
        assert_eq!(records[0].previous_hash, GENESIS_HASH);
        for i in 1..5 {
            assert_eq!(
                records[i].previous_hash,
                records[i - 1].record_hash,
                "record {i} previous_hash should point to record {} record_hash",
                i - 1
            );
        }

        // Full chain verification
        match chain.verify_chain(None).unwrap() {
            ChainStatus::Valid { record_count } => assert_eq!(record_count, 5),
            other => panic!("expected Valid with 5 records, got {other:?}"),
        }
    }

    // ── M1: document size cap ────────────────────────────────────────────────

    #[test]
    fn test_document_too_large_rejected() {
        // M1: content exceeding MAX_DOCUMENT_BYTES must be rejected before hashing
        let chain = test_chain();
        // Allocate a vec just over 100 MB
        let big = vec![0u8; 100 * 1024 * 1024 + 1];
        let err = chain.log_document(&big, "big.bin", None, &[], None);
        assert!(err.is_err(), "document > 100 MB should be rejected");
        let msg = err.unwrap_err().to_string();
        assert!(
            msg.contains("too large"),
            "error should mention 'too large', got: {msg}"
        );
    }

    #[test]
    fn test_document_at_limit_accepted() {
        // M1: content exactly at 100 MB must be accepted
        let chain = test_chain();
        let at_limit = vec![0u8; 100 * 1024 * 1024];
        let result = chain.log_document(&at_limit, "limit.bin", None, &[], None);
        assert!(
            result.is_ok(),
            "document at exactly 100 MB should be accepted"
        );
    }

    // ── M2: memory_ids cap + negative rejection ──────────────────────────────

    #[test]
    fn test_too_many_memory_ids_rejected() {
        // M2: more than 10_000 memory_ids must be rejected
        let chain = test_chain();
        let ids: Vec<i64> = (0..10_001).collect();
        let err = chain.log_document(b"data", "doc.txt", None, &ids, None);
        assert!(
            err.is_err(),
            "more than 10_000 memory_ids should be rejected"
        );
        let msg = err.unwrap_err().to_string();
        assert!(
            msg.contains("too many"),
            "error should mention 'too many', got: {msg}"
        );
    }

    #[test]
    fn test_memory_ids_at_limit_accepted() {
        // M2: exactly 10_000 memory_ids must be accepted
        let chain = test_chain();
        let ids: Vec<i64> = (0..10_000).collect();
        let result = chain.log_document(b"data", "doc.txt", None, &ids, None);
        assert!(
            result.is_ok(),
            "exactly 10_000 memory_ids should be accepted"
        );
    }

    #[test]
    fn test_negative_memory_id_rejected() {
        // M2: negative memory_ids must be rejected
        let chain = test_chain();
        let ids = vec![1i64, 2, -1, 4];
        let err = chain.log_document(b"data", "doc.txt", None, &ids, None);
        assert!(err.is_err(), "negative memory_id should be rejected");
        let msg = err.unwrap_err().to_string();
        assert!(
            msg.contains("non-negative"),
            "error should mention 'non-negative', got: {msg}"
        );
    }

    #[test]
    fn test_memory_ids_all_non_negative_accepted() {
        // M2: non-negative memory_ids must be accepted
        let chain = test_chain();
        let ids = vec![0i64, 1, 100, 999];
        let result = chain.log_document(b"data", "doc.txt", None, &ids, None);
        assert!(result.is_ok(), "non-negative memory_ids should be accepted");
    }

    // ── M4: metadata size cap ────────────────────────────────────────────────

    #[test]
    fn test_metadata_too_large_rejected() {
        // M4: metadata serialized > 64 KB must be rejected
        use serde_json::Value;
        let chain = test_chain();
        // Build a JSON string value larger than 64 KB
        let big_string = "x".repeat(66_000);
        let _metadata = Value::String(big_string);
        // We can't pass metadata to log_document yet (it doesn't accept it),
        // so we test the validation helper directly.
        // For now, build a record with oversized metadata and call the internal
        // validator path — the easiest way is to expose it or test via a
        // future API. Since log_document always sets metadata = empty object
        // (always ≤ 64 KB), this test validates the future-facing contract:
        // the serialized empty object is well under 64 KB.
        let result = chain.log_document(b"data", "doc.txt", None, &[], None);
        assert!(result.is_ok());
        let record = result.unwrap();
        let serialized = serde_json::to_string(&record.metadata).unwrap();
        assert!(
            serialized.len() <= 65_536,
            "default metadata must be within 64 KB"
        );
    }

    // ── M5: streaming verify_chain ───────────────────────────────────────────
    // The existing verify_chain tests (test_verify_chain_valid,
    // test_verify_chain_empty, test_chain_tamper_detection, etc.) serve as the
    // regression harness for the streaming refactor. The behavior must be
    // identical after the refactor.

    // ── L3: document_name validation — null bytes and length cap ────────────────

    #[test]
    fn test_document_name_null_byte_rejected() {
        // L3: document_name containing null byte must be rejected
        let chain = test_chain();
        let err = chain.log_document(b"data", "doc\x00.txt", None, &[], None);
        assert!(
            err.is_err(),
            "document_name with null byte must be rejected"
        );
        let msg = err.unwrap_err().to_string();
        assert!(
            msg.contains("null"),
            "error should mention null bytes, got: {msg}"
        );
    }

    #[test]
    fn test_document_name_too_long_rejected() {
        // L3: document_name longer than 1000 chars must be rejected
        let chain = test_chain();
        let long_name = "a".repeat(1_001);
        let err = chain.log_document(b"data", &long_name, None, &[], None);
        assert!(err.is_err(), "document_name > 1000 chars must be rejected");
        let msg = err.unwrap_err().to_string();
        assert!(
            msg.contains("too long") || msg.contains("1000"),
            "error should mention length limit, got: {msg}"
        );
    }

    #[test]
    fn test_document_name_exactly_1000_chars_accepted() {
        // L3: exactly 1000 chars must be accepted
        let chain = test_chain();
        let name_1000 = "a".repeat(1_000);
        let result = chain.log_document(b"data", &name_1000, None, &[], None);
        assert!(
            result.is_ok(),
            "document_name of exactly 1000 chars should be accepted"
        );
    }

    // ── L1: corrupt metadata JSON must not be silently swallowed ───────────────

    #[test]
    fn test_corrupt_metadata_json_returns_error() {
        // L1: verify_chain (which calls row_to_record) must return Err when
        // metadata_json stored in the DB is malformed, not silently substitute {}.
        let storage = Storage::open_in_memory().unwrap();
        let chain = AttestationChain::new(storage.clone());

        chain
            .log_document(b"data", "doc.txt", None, &[], None)
            .unwrap();

        // Corrupt the metadata column directly
        storage
            .with_transaction(|conn| {
                conn.execute("UPDATE attestation_log SET metadata = '{broken'", [])?;
                Ok(())
            })
            .unwrap();

        // verify_chain must return Err, not Ok with broken ChainStatus
        let result = chain.verify_chain(None);
        assert!(
            result.is_err(),
            "corrupt metadata JSON must propagate an error, got: {result:?}"
        );
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("invalid metadata JSON") || msg.contains("metadata"),
            "error should mention metadata, got: {msg}"
        );
    }

    #[test]
    fn test_chain_tamper_detection() {
        let storage = Storage::open_in_memory().unwrap();
        let chain = AttestationChain::new(storage.clone());

        chain
            .log_document(b"first", "first.txt", None, &[], None)
            .unwrap();
        let r2 = chain
            .log_document(b"second", "second.txt", None, &[], None)
            .unwrap();

        // Chain is valid before tamper
        assert!(matches!(
            chain.verify_chain(None).unwrap(),
            ChainStatus::Valid { .. }
        ));

        // Directly modify the record_hash of the second record in the DB
        let r2_id = r2.id.unwrap();
        storage
            .with_transaction(|conn| {
                conn.execute(
                    "UPDATE attestation_log SET record_hash = 'sha256:0000tampered' WHERE id = ?1",
                    rusqlite::params![r2_id],
                )?;
                Ok(())
            })
            .expect("tamper record");

        // Chain should now be broken
        match chain.verify_chain(None).unwrap() {
            ChainStatus::Broken { at_record_id, .. } => {
                // The breakage can be detected at r2 (hash mismatch) or at a subsequent record
                assert!(at_record_id > 0);
            }
            other => panic!("expected Broken chain after tamper, got {other:?}"),
        }
    }
}