clasp-journal 4.0.1

Event log and state persistence for CLASP routers
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
//! SQLite-backed journal implementation

use async_trait::async_trait;
use clasp_core::SignalType;
#[cfg(feature = "integrity")]
use hex;
#[cfg(feature = "integrity")]
use hmac::{Hmac, Mac};
use parking_lot::Mutex;
use rusqlite::{params, Connection};
#[cfg(feature = "integrity")]
use sha2::Sha256;
use std::sync::Arc;
use tokio::sync::{mpsc, oneshot};

use crate::entry::{JournalEntry, ParamSnapshot};
use crate::error::{JournalError, Result};
use crate::journal::Journal;

#[cfg(feature = "integrity")]
type HmacSha256 = Hmac<Sha256>;

/// SQL filter strategy for address patterns.
enum PatternFilter {
    /// No wildcards — use `WHERE address = ?`
    Exact(String),
    /// Ends with `/**` — use `WHERE address LIKE '<prefix>%'`
    Prefix(String),
    /// Ends with `/*` — use `LIKE '<prefix>%' AND NOT LIKE '<prefix>%/%'`
    PrefixOneLevel(String),
    /// `/**` or `**` — no address filter needed
    MatchAll,
    /// Wildcards in middle segments — fall back to Rust glob_match
    Complex,
}

/// SQLite-backed journal for persistent storage.
///
/// Uses WAL mode for concurrent read/write access.
/// Optionally verifies entry integrity via HMAC-SHA256 (requires `integrity` feature).
pub struct SqliteJournal {
    pub(crate) conn: Mutex<Connection>,
    #[cfg(feature = "integrity")]
    hmac_key: Option<[u8; 32]>,
}

impl SqliteJournal {
    /// Create a new SQLite journal at the given path.
    pub fn new(path: &str) -> Result<Self> {
        let conn = Connection::open(path).map_err(|e| JournalError::StorageError(e.to_string()))?;
        let journal = Self {
            conn: Mutex::new(conn),
            #[cfg(feature = "integrity")]
            hmac_key: None,
        };
        journal.init_tables()?;
        Ok(journal)
    }

    /// Create a new SQLite journal with HMAC integrity verification.
    ///
    /// When an HMAC key is provided, all writes compute HMAC-SHA256 over
    /// `address || value_json || timestamp`, and all reads verify the HMAC.
    /// Entries written without HMAC (NULL hmac column) are accepted without verification.
    #[cfg(feature = "integrity")]
    pub fn with_hmac(path: &str, key: [u8; 32]) -> Result<Self> {
        let conn = Connection::open(path).map_err(|e| JournalError::StorageError(e.to_string()))?;
        let journal = Self {
            conn: Mutex::new(conn),
            hmac_key: Some(key),
        };
        journal.init_tables()?;
        Ok(journal)
    }

    /// Create an in-memory SQLite journal (for testing).
    pub fn in_memory() -> Result<Self> {
        let conn =
            Connection::open_in_memory().map_err(|e| JournalError::StorageError(e.to_string()))?;
        let journal = Self {
            conn: Mutex::new(conn),
            #[cfg(feature = "integrity")]
            hmac_key: None,
        };
        journal.init_tables()?;
        Ok(journal)
    }

    /// Create an in-memory journal with HMAC integrity verification (for testing).
    #[cfg(feature = "integrity")]
    pub fn in_memory_with_hmac(key: [u8; 32]) -> Result<Self> {
        let conn =
            Connection::open_in_memory().map_err(|e| JournalError::StorageError(e.to_string()))?;
        let journal = Self {
            conn: Mutex::new(conn),
            hmac_key: Some(key),
        };
        journal.init_tables()?;
        Ok(journal)
    }

    fn init_tables(&self) -> Result<()> {
        let conn = self.conn.lock();
        conn.execute_batch(
            "
            PRAGMA journal_mode = WAL;
            PRAGMA synchronous = NORMAL;

            CREATE TABLE IF NOT EXISTS journal_entries (
                seq INTEGER PRIMARY KEY AUTOINCREMENT,
                timestamp INTEGER NOT NULL,
                author TEXT NOT NULL,
                address TEXT NOT NULL,
                signal_type TEXT NOT NULL,
                value_json TEXT NOT NULL,
                revision INTEGER,
                msg_type INTEGER NOT NULL,
                hmac TEXT
            );

            CREATE INDEX IF NOT EXISTS idx_entries_address ON journal_entries(address);
            CREATE INDEX IF NOT EXISTS idx_entries_timestamp ON journal_entries(timestamp);
            CREATE INDEX IF NOT EXISTS idx_entries_signal_type ON journal_entries(signal_type);

            CREATE TABLE IF NOT EXISTS snapshots (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                created_at INTEGER NOT NULL,
                seq_at INTEGER NOT NULL,
                data_json TEXT NOT NULL
            );
            ",
        )
        .map_err(|e| JournalError::StorageError(e.to_string()))?;

        // Migration: add hmac column to existing databases that lack it
        let has_hmac: bool = conn
            .prepare("SELECT hmac FROM journal_entries LIMIT 0")
            .is_ok();
        if !has_hmac {
            conn.execute_batch("ALTER TABLE journal_entries ADD COLUMN hmac TEXT")
                .map_err(|e| JournalError::StorageError(format!("migration failed: {}", e)))?;
        }

        Ok(())
    }

    /// Classify a CLASP address pattern for SQL-level filtering.
    ///
    /// Pushes simple patterns to SQL WHERE clauses instead of fetching all rows
    /// and filtering in Rust, which is O(n) on the full journal.
    fn pattern_to_sql_filter(pattern: &str) -> PatternFilter {
        // /** or ** matches everything
        if pattern == "/**" || pattern == "**" {
            return PatternFilter::MatchAll;
        }

        // No wildcards at all => exact match
        if !pattern.contains('*') {
            return PatternFilter::Exact(pattern.to_string());
        }

        let parts: Vec<&str> = pattern.split('/').collect();
        // Check if only the last segment contains a wildcard
        let wildcard_in_middle = parts[..parts.len().saturating_sub(1)]
            .iter()
            .any(|p| p.contains('*'));

        if wildcard_in_middle {
            return PatternFilter::Complex;
        }

        let last = *parts.last().unwrap_or(&"");
        let prefix = parts[..parts.len() - 1].join("/");
        let prefix_with_slash = format!("{}/", prefix);

        if last == "**" {
            // /foo/** -> LIKE '/foo/%'
            PatternFilter::Prefix(prefix_with_slash)
        } else if last == "*" {
            // /foo/* -> LIKE '/foo/%' AND NOT LIKE '/foo/%/%'
            PatternFilter::PrefixOneLevel(prefix_with_slash)
        } else {
            // Last segment has embedded wildcard like "temp*" — complex
            PatternFilter::Complex
        }
    }

    fn signal_type_to_str(st: &SignalType) -> &'static str {
        match st {
            SignalType::Param => "param",
            SignalType::Event => "event",
            SignalType::Stream => "stream",
            SignalType::Gesture => "gesture",
            SignalType::Timeline => "timeline",
        }
    }

    fn str_to_signal_type(s: &str) -> SignalType {
        match s {
            "param" => SignalType::Param,
            "event" => SignalType::Event,
            "stream" => SignalType::Stream,
            "gesture" => SignalType::Gesture,
            "timeline" => SignalType::Timeline,
            _ => SignalType::Event,
        }
    }

    /// Compute HMAC-SHA256 over address, value_json, and timestamp.
    /// See pentest JNL-01: Journal Tampering
    #[cfg(feature = "integrity")]
    fn compute_hmac(key: &[u8; 32], address: &str, value_json: &str, timestamp: u64) -> String {
        let mut mac = HmacSha256::new_from_slice(key).expect("HMAC accepts any key length");
        mac.update(address.as_bytes());
        mac.update(value_json.as_bytes());
        mac.update(&timestamp.to_le_bytes());
        hex::encode(mac.finalize().into_bytes())
    }

    /// Verify an HMAC against computed value. See pentest JNL-01: Journal Tampering
    #[cfg(feature = "integrity")]
    fn verify_hmac(
        key: &[u8; 32],
        address: &str,
        value_json: &str,
        timestamp: u64,
        expected: &str,
    ) -> bool {
        let computed = Self::compute_hmac(key, address, value_json, timestamp);
        // Constant-time comparison via the hmac crate
        let mut mac = HmacSha256::new_from_slice(key).expect("HMAC accepts any key length");
        mac.update(address.as_bytes());
        mac.update(value_json.as_bytes());
        mac.update(&timestamp.to_le_bytes());
        // Decode expected hex and verify
        if let Ok(expected_bytes) = hex::decode(expected) {
            mac.verify_slice(&expected_bytes).is_ok()
        } else {
            // Invalid hex in stored HMAC
            computed == expected
        }
    }
}

#[async_trait]
impl Journal for SqliteJournal {
    async fn append(&self, entry: JournalEntry) -> Result<u64> {
        let conn = self.conn.lock();
        let value_json = serde_json::to_string(&entry.value)
            .map_err(|e| JournalError::SerializationError(e.to_string()))?;

        #[cfg(feature = "integrity")]
        let hmac_value: Option<String> = self
            .hmac_key
            .map(|key| Self::compute_hmac(&key, &entry.address, &value_json, entry.timestamp));
        #[cfg(not(feature = "integrity"))]
        let hmac_value: Option<String> = None;

        conn.execute(
            "INSERT INTO journal_entries (timestamp, author, address, signal_type, value_json, revision, msg_type, hmac)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
            params![
                entry.timestamp as i64,
                entry.author,
                entry.address,
                Self::signal_type_to_str(&entry.signal_type),
                value_json,
                entry.revision.map(|r| r as i64),
                entry.msg_type as i64,
                hmac_value,
            ],
        )
        .map_err(|e| JournalError::StorageError(e.to_string()))?;

        let seq = conn.last_insert_rowid() as u64;
        Ok(seq)
    }

    async fn query(
        &self,
        pattern: &str,
        from: Option<u64>,
        to: Option<u64>,
        limit: Option<u32>,
        types: &[SignalType],
    ) -> Result<Vec<JournalEntry>> {
        let conn = self.conn.lock();

        let mut sql = String::from(
            "SELECT seq, timestamp, author, address, signal_type, value_json, revision, msg_type, hmac FROM journal_entries WHERE 1=1",
        );
        let mut sql_params: Vec<Box<dyn rusqlite::types::ToSql>> = Vec::new();

        // Push pattern filtering to SQL where possible
        let sql_filter = Self::pattern_to_sql_filter(pattern);
        let needs_rust_filter = match &sql_filter {
            PatternFilter::Exact(addr) => {
                sql.push_str(&format!(" AND address = ?{}", sql_params.len() + 1));
                sql_params.push(Box::new(addr.clone()));
                false
            }
            PatternFilter::Prefix(prefix) => {
                // Globstar: /foo/** -> LIKE '/foo/%'
                let like = format!("{}%", prefix);
                sql.push_str(&format!(" AND address LIKE ?{}", sql_params.len() + 1));
                sql_params.push(Box::new(like));
                false
            }
            PatternFilter::PrefixOneLevel(prefix) => {
                // Single wildcard at end: /foo/* -> LIKE '/foo/%' AND NOT LIKE '/foo/%/%'
                let like = format!("{}%", prefix);
                let not_like = format!("{}%/%", prefix);
                sql.push_str(&format!(" AND address LIKE ?{}", sql_params.len() + 1));
                sql_params.push(Box::new(like));
                sql.push_str(&format!(" AND address NOT LIKE ?{}", sql_params.len() + 1));
                sql_params.push(Box::new(not_like));
                false
            }
            PatternFilter::MatchAll => false,
            PatternFilter::Complex => true,
        };

        if let Some(from) = from {
            sql.push_str(&format!(" AND timestamp >= ?{}", sql_params.len() + 1));
            sql_params.push(Box::new(from as i64));
        }
        if let Some(to) = to {
            sql.push_str(&format!(" AND timestamp <= ?{}", sql_params.len() + 1));
            sql_params.push(Box::new(to as i64));
        }
        if !types.is_empty() {
            let type_strs: Vec<String> = types
                .iter()
                .map(|t| format!("'{}'", Self::signal_type_to_str(t)))
                .collect();
            sql.push_str(&format!(" AND signal_type IN ({})", type_strs.join(",")));
        }

        sql.push_str(" ORDER BY seq ASC");

        if let Some(limit) = limit {
            sql.push_str(&format!(" LIMIT ?{}", sql_params.len() + 1));
            sql_params.push(Box::new(limit as i64));
        }

        let param_refs: Vec<&dyn rusqlite::types::ToSql> =
            sql_params.iter().map(|p| p.as_ref()).collect();
        let mut stmt = conn
            .prepare(&sql)
            .map_err(|e| JournalError::StorageError(e.to_string()))?;

        let rows = stmt
            .query_map(param_refs.as_slice(), |row| {
                let value_json: String = row.get(5)?;
                let revision: Option<i64> = row.get(6)?;
                let sig_str: String = row.get(4)?;
                let stored_hmac: Option<String> = row.get(8)?;

                Ok((
                    JournalEntry {
                        seq: row.get::<_, i64>(0)? as u64,
                        timestamp: row.get::<_, i64>(1)? as u64,
                        author: row.get(2)?,
                        address: row.get(3)?,
                        signal_type: Self::str_to_signal_type(&sig_str),
                        value: serde_json::from_str(&value_json).unwrap_or(clasp_core::Value::Null),
                        revision: revision.map(|r| r as u64),
                        msg_type: row.get::<_, i64>(7)? as u8,
                    },
                    value_json,
                    stored_hmac,
                ))
            })
            .map_err(|e| JournalError::StorageError(e.to_string()))?;

        let mut results = Vec::new();
        for row in rows {
            let (entry, _value_json, stored_hmac) =
                row.map_err(|e| JournalError::StorageError(e.to_string()))?;

            // Verify HMAC integrity if key is configured and entry has an HMAC
            #[cfg(feature = "integrity")]
            if let Some(ref key) = self.hmac_key {
                if let Some(ref expected_hmac) = stored_hmac {
                    if !Self::verify_hmac(
                        key,
                        &entry.address,
                        &_value_json,
                        entry.timestamp,
                        expected_hmac,
                    ) {
                        return Err(JournalError::IntegrityViolation {
                            seq: entry.seq,
                            reason: "HMAC mismatch".to_string(),
                        });
                    }
                }
                // If stored_hmac is None, entry was written without integrity — allow it.
                // Migration path: old entries written before HMAC was enabled have NULL hmac.
            }
            let _ = stored_hmac; // Suppress unused warning when integrity feature is off

            // Only fall back to Rust-side glob filtering for complex patterns
            if needs_rust_filter {
                if clasp_core::address::glob_match(pattern, &entry.address) {
                    results.push(entry);
                }
            } else {
                results.push(entry);
            }
        }

        Ok(results)
    }

    async fn since(&self, seq: u64, limit: Option<u32>) -> Result<Vec<JournalEntry>> {
        let conn = self.conn.lock();

        let sql = if let Some(limit) = limit {
            format!(
                "SELECT seq, timestamp, author, address, signal_type, value_json, revision, msg_type, hmac \
                 FROM journal_entries WHERE seq > ?1 ORDER BY seq ASC LIMIT {}",
                limit
            )
        } else {
            "SELECT seq, timestamp, author, address, signal_type, value_json, revision, msg_type, hmac \
             FROM journal_entries WHERE seq > ?1 ORDER BY seq ASC"
                .to_string()
        };

        let mut stmt = conn
            .prepare(&sql)
            .map_err(|e| JournalError::StorageError(e.to_string()))?;

        let rows = stmt
            .query_map(params![seq as i64], |row| {
                let value_json: String = row.get(5)?;
                let revision: Option<i64> = row.get(6)?;
                let sig_str: String = row.get(4)?;
                let stored_hmac: Option<String> = row.get(8)?;

                Ok((
                    JournalEntry {
                        seq: row.get::<_, i64>(0)? as u64,
                        timestamp: row.get::<_, i64>(1)? as u64,
                        author: row.get(2)?,
                        address: row.get(3)?,
                        signal_type: Self::str_to_signal_type(&sig_str),
                        value: serde_json::from_str(&value_json).unwrap_or(clasp_core::Value::Null),
                        revision: revision.map(|r| r as u64),
                        msg_type: row.get::<_, i64>(7)? as u8,
                    },
                    value_json,
                    stored_hmac,
                ))
            })
            .map_err(|e| JournalError::StorageError(e.to_string()))?;

        let mut results = Vec::new();
        for row in rows {
            let (entry, _value_json, stored_hmac) =
                row.map_err(|e| JournalError::StorageError(e.to_string()))?;

            #[cfg(feature = "integrity")]
            if let Some(ref key) = self.hmac_key {
                if let Some(ref expected_hmac) = stored_hmac {
                    if !Self::verify_hmac(
                        key,
                        &entry.address,
                        &_value_json,
                        entry.timestamp,
                        expected_hmac,
                    ) {
                        return Err(JournalError::IntegrityViolation {
                            seq: entry.seq,
                            reason: "HMAC mismatch".to_string(),
                        });
                    }
                }
            }
            let _ = stored_hmac;

            results.push(entry);
        }

        Ok(results)
    }

    async fn latest_seq(&self) -> Result<u64> {
        let conn = self.conn.lock();
        let seq: i64 = conn
            .query_row(
                "SELECT COALESCE(MAX(seq), 0) FROM journal_entries",
                [],
                |row| row.get(0),
            )
            .map_err(|e| JournalError::StorageError(e.to_string()))?;
        Ok(seq as u64)
    }

    async fn snapshot(&self, state: &[ParamSnapshot]) -> Result<u64> {
        let conn = self.conn.lock();
        let seq: i64 = conn
            .query_row(
                "SELECT COALESCE(MAX(seq), 0) FROM journal_entries",
                [],
                |row| row.get(0),
            )
            .map_err(|e| JournalError::StorageError(e.to_string()))?;

        let data_json = serde_json::to_string(state)
            .map_err(|e| JournalError::SerializationError(e.to_string()))?;

        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_micros() as i64)
            .unwrap_or(0);

        conn.execute(
            "INSERT INTO snapshots (created_at, seq_at, data_json) VALUES (?1, ?2, ?3)",
            params![now, seq, data_json],
        )
        .map_err(|e| JournalError::StorageError(e.to_string()))?;

        Ok(seq as u64)
    }

    async fn load_snapshot(&self) -> Result<Option<Vec<ParamSnapshot>>> {
        let conn = self.conn.lock();

        let result: std::result::Result<String, rusqlite::Error> = conn.query_row(
            "SELECT data_json FROM snapshots ORDER BY id DESC LIMIT 1",
            [],
            |row| row.get(0),
        );

        match result {
            Ok(json) => {
                let snapshots: Vec<ParamSnapshot> = serde_json::from_str(&json)
                    .map_err(|e| JournalError::SerializationError(e.to_string()))?;
                Ok(Some(snapshots))
            }
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(JournalError::StorageError(e.to_string())),
        }
    }

    async fn compact(&self, before_seq: u64) -> Result<u64> {
        let conn = self.conn.lock();
        let removed = conn
            .execute(
                "DELETE FROM journal_entries WHERE seq < ?1",
                params![before_seq as i64],
            )
            .map_err(|e| JournalError::StorageError(e.to_string()))?;
        Ok(removed as u64)
    }

    async fn len(&self) -> Result<usize> {
        let conn = self.conn.lock();
        let count: i64 = conn
            .query_row("SELECT COUNT(*) FROM journal_entries", [], |row| row.get(0))
            .map_err(|e| JournalError::StorageError(e.to_string()))?;
        Ok(count as usize)
    }
}

/// Command sent to the batching writer thread.
enum BatchCommand {
    Write(JournalEntry, oneshot::Sender<Result<u64>>),
    Shutdown,
}

/// Batching wrapper around `SqliteJournal`.
///
/// Writes are sent to a dedicated writer task that batches multiple INSERTs
/// into a single SQLite transaction, significantly improving write throughput.
/// Reads are delegated directly to the underlying `SqliteJournal`.
pub struct BatchingSqliteJournal {
    writer_tx: mpsc::Sender<BatchCommand>,
    inner: Arc<SqliteJournal>,
}

impl BatchingSqliteJournal {
    /// Create a new batching journal wrapping a `SqliteJournal`.
    ///
    /// `batch_size` — max entries per batch (default 100).
    /// `batch_timeout_ms` — max wait time before flushing a partial batch (default 10ms).
    pub fn new(inner: SqliteJournal, batch_size: usize, batch_timeout_ms: u64) -> Self {
        let inner = Arc::new(inner);
        let (tx, rx) = mpsc::channel::<BatchCommand>(4096);

        let write_inner = Arc::clone(&inner);
        tokio::spawn(async move {
            Self::writer_loop(write_inner, rx, batch_size, batch_timeout_ms).await;
        });

        Self {
            writer_tx: tx,
            inner,
        }
    }

    /// Create with default batch parameters (100 entries, 10ms timeout).
    pub fn with_defaults(inner: SqliteJournal) -> Self {
        Self::new(inner, 100, 10)
    }

    async fn writer_loop(
        inner: Arc<SqliteJournal>,
        mut rx: mpsc::Receiver<BatchCommand>,
        batch_size: usize,
        batch_timeout_ms: u64,
    ) {
        let timeout = std::time::Duration::from_millis(batch_timeout_ms);
        let mut batch: Vec<(JournalEntry, oneshot::Sender<Result<u64>>)> =
            Vec::with_capacity(batch_size);

        loop {
            // Wait for the first message
            let cmd = rx.recv().await;
            match cmd {
                Some(BatchCommand::Write(entry, tx)) => {
                    batch.push((entry, tx));
                }
                Some(BatchCommand::Shutdown) | None => {
                    // Flush remaining and exit
                    if !batch.is_empty() {
                        Self::flush_batch(&inner, &mut batch);
                    }
                    return;
                }
            }

            // Collect more messages up to batch_size or timeout
            let deadline = tokio::time::Instant::now() + timeout;
            while batch.len() < batch_size {
                match tokio::time::timeout_at(deadline, rx.recv()).await {
                    Ok(Some(BatchCommand::Write(entry, tx))) => {
                        batch.push((entry, tx));
                    }
                    Ok(Some(BatchCommand::Shutdown)) | Ok(None) => {
                        Self::flush_batch(&inner, &mut batch);
                        return;
                    }
                    Err(_) => break, // Timeout — flush what we have
                }
            }

            Self::flush_batch(&inner, &mut batch);
        }
    }

    fn flush_batch(
        inner: &SqliteJournal,
        batch: &mut Vec<(JournalEntry, oneshot::Sender<Result<u64>>)>,
    ) {
        let conn = inner.conn.lock();

        // Execute all INSERTs in a single transaction
        let tx_result = conn.execute_batch("BEGIN");
        if let Err(e) = tx_result {
            let err_msg = format!("batch BEGIN failed: {}", e);
            for (_, sender) in batch.drain(..) {
                let _ = sender.send(Err(JournalError::StorageError(err_msg.clone())));
            }
            return;
        }

        let mut results: Vec<std::result::Result<u64, String>> = Vec::with_capacity(batch.len());

        for (entry, _) in batch.iter() {
            let value_json = match serde_json::to_string(&entry.value) {
                Ok(j) => j,
                Err(e) => {
                    results.push(Err(e.to_string()));
                    continue;
                }
            };

            #[cfg(feature = "integrity")]
            let hmac_value: Option<String> = inner.hmac_key.map(|key| {
                SqliteJournal::compute_hmac(&key, &entry.address, &value_json, entry.timestamp)
            });
            #[cfg(not(feature = "integrity"))]
            let hmac_value: Option<String> = None;

            match conn.execute(
                "INSERT INTO journal_entries (timestamp, author, address, signal_type, value_json, revision, msg_type, hmac)
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
                params![
                    entry.timestamp as i64,
                    entry.author,
                    entry.address,
                    SqliteJournal::signal_type_to_str(&entry.signal_type),
                    value_json,
                    entry.revision.map(|r| r as i64),
                    entry.msg_type as i64,
                    hmac_value,
                ],
            ) {
                Ok(_) => results.push(Ok(conn.last_insert_rowid() as u64)),
                Err(e) => results.push(Err(e.to_string())),
            }
        }

        let commit_result = conn.execute_batch("COMMIT");
        drop(conn); // Release lock before sending results

        if let Err(e) = commit_result {
            let err_msg = format!("batch COMMIT failed: {}", e);
            for (_, sender) in batch.drain(..) {
                let _ = sender.send(Err(JournalError::StorageError(err_msg.clone())));
            }
            return;
        }

        for ((_, sender), result) in batch.drain(..).zip(results) {
            let _ = sender.send(match result {
                Ok(seq) => Ok(seq),
                Err(e) => Err(JournalError::StorageError(e)),
            });
        }
    }
}

#[async_trait]
impl Journal for BatchingSqliteJournal {
    async fn append(&self, entry: JournalEntry) -> Result<u64> {
        let (tx, rx) = oneshot::channel();
        self.writer_tx
            .send(BatchCommand::Write(entry, tx))
            .await
            .map_err(|_| JournalError::StorageError("writer channel closed".to_string()))?;
        rx.await
            .map_err(|_| JournalError::StorageError("writer dropped response".to_string()))?
    }

    async fn query(
        &self,
        pattern: &str,
        from: Option<u64>,
        to: Option<u64>,
        limit: Option<u32>,
        types: &[SignalType],
    ) -> Result<Vec<JournalEntry>> {
        self.inner.query(pattern, from, to, limit, types).await
    }

    async fn since(&self, seq: u64, limit: Option<u32>) -> Result<Vec<JournalEntry>> {
        self.inner.since(seq, limit).await
    }

    async fn latest_seq(&self) -> Result<u64> {
        self.inner.latest_seq().await
    }

    async fn snapshot(&self, state: &[ParamSnapshot]) -> Result<u64> {
        self.inner.snapshot(state).await
    }

    async fn load_snapshot(&self) -> Result<Option<Vec<ParamSnapshot>>> {
        self.inner.load_snapshot().await
    }

    async fn compact(&self, before_seq: u64) -> Result<u64> {
        self.inner.compact(before_seq).await
    }

    async fn len(&self) -> Result<usize> {
        self.inner.len().await
    }
}

impl Drop for BatchingSqliteJournal {
    fn drop(&mut self) {
        let _ = self.writer_tx.try_send(BatchCommand::Shutdown);
    }
}

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

    #[tokio::test]
    async fn test_sqlite_append_and_query() {
        let journal = SqliteJournal::in_memory().unwrap();

        let entry = JournalEntry::from_set(
            "/test/value".to_string(),
            Value::Float(0.5),
            1,
            "session1".to_string(),
            1000000,
        );

        let seq = journal.append(entry).await.unwrap();
        assert_eq!(seq, 1);

        let results = journal.query("/**", None, None, None, &[]).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].address, "/test/value");
        assert_eq!(results[0].seq, 1);
    }

    #[tokio::test]
    async fn test_sqlite_since() {
        let journal = SqliteJournal::in_memory().unwrap();

        for i in 0..5 {
            let entry = JournalEntry::from_set(
                format!("/test/{}", i),
                Value::Int(i),
                (i + 1) as u64,
                "s1".to_string(),
                1000 * i as u64,
            );
            journal.append(entry).await.unwrap();
        }

        let results = journal.since(3, None).await.unwrap();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].seq, 4);
        assert_eq!(results[1].seq, 5);
    }

    #[tokio::test]
    async fn test_sqlite_snapshot() {
        let journal = SqliteJournal::in_memory().unwrap();

        let snapshots = vec![ParamSnapshot {
            address: "/test/a".to_string(),
            value: Value::Float(1.0),
            revision: 5,
            writer: "s1".to_string(),
            timestamp: 1000,
        }];

        journal.snapshot(&snapshots).await.unwrap();

        let loaded = journal.load_snapshot().await.unwrap().unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].address, "/test/a");
    }

    #[tokio::test]
    async fn test_sqlite_compact() {
        let journal = SqliteJournal::in_memory().unwrap();

        for i in 0..10 {
            let entry = JournalEntry::from_set(
                format!("/test/{}", i),
                Value::Int(i),
                (i + 1) as u64,
                "s1".to_string(),
                1000 * i as u64,
            );
            journal.append(entry).await.unwrap();
        }

        let removed = journal.compact(6).await.unwrap();
        assert_eq!(removed, 5);

        let len = journal.len().await.unwrap();
        assert_eq!(len, 5);
    }

    #[tokio::test]
    async fn test_sqlite_empty_snapshot() {
        let journal = SqliteJournal::in_memory().unwrap();
        let loaded = journal.load_snapshot().await.unwrap();
        assert!(loaded.is_none());
    }

    #[tokio::test]
    async fn test_sql_pattern_filtering() {
        let journal = SqliteJournal::in_memory().unwrap();

        // Insert entries at various addresses
        for (addr, val) in [
            ("/sensors/temp", 1),
            ("/sensors/humidity", 2),
            ("/sensors/temp/room1", 3),
            ("/sensors/temp/room2", 4),
            ("/lights/room1", 5),
        ] {
            let entry = JournalEntry::from_set(
                addr.to_string(),
                Value::Int(val),
                val as u64,
                "s1".to_string(),
                1000 * val as u64,
            );
            journal.append(entry).await.unwrap();
        }

        // Exact match
        let results = journal
            .query("/sensors/temp", None, None, None, &[])
            .await
            .unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].address, "/sensors/temp");

        // Globstar — /sensors/**
        let results = journal
            .query("/sensors/**", None, None, None, &[])
            .await
            .unwrap();
        assert_eq!(results.len(), 4);

        // Single wildcard — /sensors/*
        let results = journal
            .query("/sensors/*", None, None, None, &[])
            .await
            .unwrap();
        assert_eq!(results.len(), 2); // temp, humidity (not temp/room1, temp/room2)

        // Match all — /**
        let results = journal.query("/**", None, None, None, &[]).await.unwrap();
        assert_eq!(results.len(), 5);

        // Exact miss
        let results = journal
            .query("/sensors/pressure", None, None, None, &[])
            .await
            .unwrap();
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_pattern_to_sql_filter() {
        // Exact
        match SqliteJournal::pattern_to_sql_filter("/sensors/temp") {
            PatternFilter::Exact(s) => assert_eq!(s, "/sensors/temp"),
            _ => panic!("expected Exact"),
        }
        // Globstar
        match SqliteJournal::pattern_to_sql_filter("/sensors/**") {
            PatternFilter::Prefix(s) => assert_eq!(s, "/sensors/"),
            _ => panic!("expected Prefix"),
        }
        // Single wildcard
        match SqliteJournal::pattern_to_sql_filter("/sensors/*") {
            PatternFilter::PrefixOneLevel(s) => assert_eq!(s, "/sensors/"),
            _ => panic!("expected PrefixOneLevel"),
        }
        // Match all
        assert!(matches!(
            SqliteJournal::pattern_to_sql_filter("/**"),
            PatternFilter::MatchAll
        ));
        // Complex (wildcard in middle)
        assert!(matches!(
            SqliteJournal::pattern_to_sql_filter("/sensors/*/temp"),
            PatternFilter::Complex
        ));
    }

    #[tokio::test]
    async fn test_batching_journal_basic() {
        let inner = SqliteJournal::in_memory().unwrap();
        let journal = BatchingSqliteJournal::with_defaults(inner);

        let entry = JournalEntry::from_set(
            "/test/batch".to_string(),
            Value::Float(1.0),
            1,
            "s1".to_string(),
            1000,
        );
        let seq = journal.append(entry).await.unwrap();
        assert!(seq > 0);

        let results = journal.query("/**", None, None, None, &[]).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].address, "/test/batch");
    }

    #[tokio::test]
    async fn test_batching_journal_concurrent_writes() {
        let inner = SqliteJournal::in_memory().unwrap();
        let journal = Arc::new(BatchingSqliteJournal::with_defaults(inner));

        // Spawn many concurrent writes to exercise batching
        let mut handles = Vec::new();
        for i in 0..50 {
            let j = Arc::clone(&journal);
            handles.push(tokio::spawn(async move {
                let entry = JournalEntry::from_set(
                    format!("/test/{}", i),
                    Value::Int(i),
                    (i + 1) as u64,
                    "s1".to_string(),
                    1000 * i as u64,
                );
                j.append(entry).await.unwrap()
            }));
        }

        for h in handles {
            let seq = h.await.unwrap();
            assert!(seq > 0);
        }

        let len = journal.len().await.unwrap();
        assert_eq!(len, 50);
    }

    #[cfg(feature = "integrity")]
    #[tokio::test]
    async fn test_hmac_integrity_roundtrip() {
        let key = [42u8; 32];
        let journal = SqliteJournal::in_memory_with_hmac(key).unwrap();

        let entry = JournalEntry::from_set(
            "/test/hmac".to_string(),
            Value::Float(3.14),
            1,
            "author1".to_string(),
            1000000,
        );
        journal.append(entry).await.unwrap();

        // Query should succeed — HMAC is valid
        let results = journal.query("/**", None, None, None, &[]).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].address, "/test/hmac");
    }

    #[cfg(feature = "integrity")]
    #[tokio::test]
    async fn test_hmac_detects_tampering() {
        let key = [42u8; 32];
        let journal = SqliteJournal::in_memory_with_hmac(key).unwrap();

        let entry = JournalEntry::from_set(
            "/test/tamper".to_string(),
            Value::Int(100),
            1,
            "author1".to_string(),
            1000000,
        );
        journal.append(entry).await.unwrap();

        // Tamper with the stored value
        {
            let conn = journal.conn.lock();
            conn.execute(
                "UPDATE journal_entries SET value_json = '999' WHERE address = '/test/tamper'",
                [],
            )
            .unwrap();
        }

        // Query should fail with IntegrityViolation
        let result = journal.query("/**", None, None, None, &[]).await;
        assert!(result.is_err());
        match result.unwrap_err() {
            JournalError::IntegrityViolation { seq, reason } => {
                assert_eq!(seq, 1);
                assert_eq!(reason, "HMAC mismatch");
            }
            other => panic!("Expected IntegrityViolation, got: {:?}", other),
        }
    }

    #[cfg(feature = "integrity")]
    #[tokio::test]
    async fn test_hmac_allows_legacy_entries() {
        let key = [42u8; 32];
        let journal = SqliteJournal::in_memory_with_hmac(key).unwrap();

        // Insert entry without HMAC (simulating legacy data)
        {
            let conn = journal.conn.lock();
            conn.execute(
                "INSERT INTO journal_entries (timestamp, author, address, signal_type, value_json, revision, msg_type, hmac)
                 VALUES (1000, 'legacy', '/test/old', 'param', '42', 1, 33, NULL)",
                [],
            )
            .unwrap();
        }

        // Query should succeed — NULL HMAC entries are accepted
        let results = journal.query("/**", None, None, None, &[]).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].address, "/test/old");
    }
}