warmplane 0.23.0

Local control plane that keeps MCP sessions warm with compact capability/resource/prompt facades.
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
// Rust guideline compliant 2026-08-17

//! Append-only WORM audit log storage engine and query interface.
//!
//! Provides in-memory or persisted append-only storage with linear cryptographic hash chaining,
//! fast indexed filtering, and continuous tamper verification.

use anyhow::Result;
use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use tokio::sync::RwLock;

use crate::audit::chain::{compute_event_hash_with_key, verify_record_hash_with_key, GENESIS_HASH};
use crate::audit::models::{
    AuditEvent, AuditEventStatus, AuditEventType, RawAuditEvent, VerificationReport,
};

/// Query filter options for searching audit events.
#[derive(Debug, Clone, Default)]
pub struct AuditQueryFilter {
    /// Filter events with timestamp >= start_time_ns.
    pub start_time_ns: Option<u64>,
    /// Filter events with timestamp <= end_time_ns.
    pub end_time_ns: Option<u64>,
    /// Filter by specific actor ID.
    pub actor_id: Option<String>,
    /// Filter by target upstream server ID.
    pub server_id: Option<String>,
    /// Filter by target capability ID.
    pub capability_id: Option<String>,
    /// Filter by event type.
    pub event_type: Option<AuditEventType>,
    /// Filter by event status.
    pub status: Option<AuditEventStatus>,
    /// Filter by trace ID.
    pub trace_id: Option<String>,
    /// Filter by request ID.
    pub request_id: Option<String>,
    /// Case-insensitive multi-field search substring query.
    pub search: Option<String>,
    /// Maximum number of records to return.
    pub limit: usize,
    /// Offset index for pagination.
    pub offset: usize,
}

/// Maximum number of audit events cached in RAM.
pub const MAX_IN_MEMORY_AUDIT_EVENTS: usize = 20_000;

/// Append-only audit store protecting log records from retroactive tampering.
pub struct AuditStore {
    /// In-memory sequential audit log.
    events: RwLock<Vec<AuditEvent>>,
    /// Most recent chain hash.
    latest_hash: RwLock<String>,
    /// Atomic sequence counter for event ID generation.
    counter: AtomicU64,
    /// Optional file path for appending serialized audit log entries (JSONL).
    file_path: Option<PathBuf>,
    /// Optional HMAC key for calculating keyed audit event digests.
    hmac_key: Option<Vec<u8>>,
}

impl AuditStore {
    /// Creates an empty in-memory audit store.
    pub fn in_memory() -> Self {
        Self::in_memory_with_key(None)
    }

    /// Creates an empty in-memory audit store with an optional HMAC key.
    pub fn in_memory_with_key(hmac_key: Option<Vec<u8>>) -> Self {
        Self {
            events: RwLock::new(Vec::new()),
            latest_hash: RwLock::new(GENESIS_HASH.to_string()),
            counter: AtomicU64::new(1),
            file_path: None,
            hmac_key,
        }
    }

    /// Initializes an audit store backed by an append-only JSONL file on disk.
    /// Loads existing records and validates the integrity of the existing chain upon startup.
    ///
    /// # Arguments
    /// * `path` - File path to the append-only audit log file.
    ///
    /// # Errors
    /// Returns an error if the file exists and its hash chain is corrupted or cannot be read.
    pub fn open_or_create(path: impl AsRef<Path>) -> Result<Self> {
        Self::open_or_create_with_key(path, None)
    }

    /// Initializes an audit store backed by an append-only JSONL file on disk with an optional HMAC key.
    pub fn open_or_create_with_key(
        path: impl AsRef<Path>,
        hmac_key: Option<Vec<u8>>,
    ) -> Result<Self> {
        let path_buf = path.as_ref().to_path_buf();
        let mut events = Vec::new();
        let mut latest_hash = GENESIS_HASH.to_string();
        let mut max_seq = 0u64;

        if path_buf.exists() {
            let file = File::open(&path_buf)?;
            let reader = BufReader::new(file);
            for line_res in reader.lines() {
                let line = line_res?;
                let trimmed = line.trim();
                if trimmed.is_empty() {
                    continue;
                }
                let event: AuditEvent = serde_json::from_str(trimmed)?;

                // Verify record
                if event.prev_hash != latest_hash {
                    anyhow::bail!(
                        "Corrupted audit chain at event ID '{}': expected prev_hash '{}', found '{}'",
                        event.id,
                        latest_hash,
                        event.prev_hash
                    );
                }
                if !verify_record_hash_with_key(&event, hmac_key.as_deref()) {
                    anyhow::bail!("Tampered audit record detected at event ID '{}'", event.id);
                }

                latest_hash = event.hash.clone();
                events.push(event);
                max_seq += 1;
            }
        }

        Ok(Self {
            events: RwLock::new(events),
            latest_hash: RwLock::new(latest_hash),
            counter: AtomicU64::new(max_seq + 1),
            file_path: Some(path_buf),
            hmac_key,
        })
    }

    /// Appends a raw audit event to the append-only log atomically calculating its chain hash.
    ///
    /// # Arguments
    /// * `raw` - Unsigned raw event payload.
    ///
    /// # Returns
    /// The fully signed, chained `AuditEvent`.
    ///
    /// # Errors
    /// Returns an error if writing to disk file fails.
    pub async fn append(&self, raw: RawAuditEvent) -> Result<AuditEvent> {
        let seq = self.counter.fetch_add(1, Ordering::Relaxed);
        let now_ns = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos() as u64;

        let id = format!("aud_{:012}", seq);

        let mut events_guard = self.events.write().await;
        let mut latest_hash_guard = self.latest_hash.write().await;

        let prev_hash = latest_hash_guard.clone();
        let hash =
            compute_event_hash_with_key(&prev_hash, &id, now_ns, &raw, self.hmac_key.as_deref());

        let record = AuditEvent {
            id,
            timestamp_ns: now_ns,
            event_type: raw.event_type,
            trace_id: raw.trace_id,
            request_id: raw.request_id,
            actor_id: raw.actor_id,
            work_item_id: raw.work_item_id,
            client_ip: raw.client_ip,
            server_id: raw.server_id,
            capability_id: raw.capability_id,
            resource_uri: raw.resource_uri,
            sanitized_args: raw.sanitized_args,
            sanitized_response: raw.sanitized_response,
            execution_latency_us: raw.execution_latency_us,
            status: raw.status,
            error_code: raw.error_code,
            error_message: raw.error_message,
            operator_id: raw.operator_id,
            approval_ticket_id: raw.approval_ticket_id,
            prev_hash,
            hash: hash.clone(),
        };

        if let Some(ref path) = self.file_path {
            let mut file = OpenOptions::new().create(true).append(true).open(path)?;
            let serialized = serde_json::to_string(&record)?;
            writeln!(file, "{}", serialized)?;
            file.flush()?;
        }

        *latest_hash_guard = hash;
        if events_guard.len() >= MAX_IN_MEMORY_AUDIT_EVENTS {
            let excess = events_guard.len() - (MAX_IN_MEMORY_AUDIT_EVENTS - 1);
            events_guard.drain(0..excess);
        }
        events_guard.push(record.clone());

        Ok(record)
    }

    /// Appends a batch of raw audit events atomically within a single write lock.
    ///
    /// # Arguments
    /// * `batch` - Slice of raw event payloads.
    ///
    /// # Returns
    /// List of signed, chained `AuditEvent` records.
    pub async fn append_batch(&self, batch: Vec<RawAuditEvent>) -> Result<Vec<AuditEvent>> {
        if batch.is_empty() {
            return Ok(Vec::new());
        }

        let mut events_guard = self.events.write().await;
        let mut latest_hash_guard = self.latest_hash.write().await;

        let mut out = Vec::with_capacity(batch.len());
        let mut disk_lines = Vec::with_capacity(batch.len());

        for raw in batch {
            let seq = self.counter.fetch_add(1, Ordering::Relaxed);
            let now_ns = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos() as u64;

            let id = format!("aud_{:012}", seq);
            let prev_hash = latest_hash_guard.clone();
            let hash = compute_event_hash_with_key(
                &prev_hash,
                &id,
                now_ns,
                &raw,
                self.hmac_key.as_deref(),
            );

            let record = AuditEvent {
                id,
                timestamp_ns: now_ns,
                event_type: raw.event_type,
                trace_id: raw.trace_id,
                request_id: raw.request_id,
                actor_id: raw.actor_id,
                work_item_id: raw.work_item_id,
                client_ip: raw.client_ip,
                server_id: raw.server_id,
                capability_id: raw.capability_id,
                resource_uri: raw.resource_uri,
                sanitized_args: raw.sanitized_args,
                sanitized_response: raw.sanitized_response,
                execution_latency_us: raw.execution_latency_us,
                status: raw.status,
                error_code: raw.error_code,
                error_message: raw.error_message,
                operator_id: raw.operator_id,
                approval_ticket_id: raw.approval_ticket_id,
                prev_hash,
                hash: hash.clone(),
            };

            *latest_hash_guard = hash;
            disk_lines.push(serde_json::to_string(&record)?);
            if events_guard.len() >= MAX_IN_MEMORY_AUDIT_EVENTS {
                let excess = events_guard.len() - (MAX_IN_MEMORY_AUDIT_EVENTS - 1);
                events_guard.drain(0..excess);
            }
            events_guard.push(record.clone());
            out.push(record);
        }

        if let Some(ref path) = self.file_path {
            let mut file = OpenOptions::new().create(true).append(true).open(path)?;
            for line in disk_lines {
                writeln!(file, "{}", line)?;
            }
            file.flush()?;
        }

        Ok(out)
    }

    /// Captures an external anchor checkpoint summary of the current tail state of the audit log.
    pub async fn get_checkpoint(&self) -> crate::audit::models::AuditCheckpoint {
        let events = self.events.read().await;
        let latest_hash = self.latest_hash.read().await.clone();
        let last_event_id = events.last().map(|e| e.id.clone());
        let now_ns = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos() as u64;

        crate::audit::models::AuditCheckpoint {
            tail_hash: latest_hash,
            last_event_id,
            total_records: events.len(),
            timestamp_ns: now_ns,
        }
    }

    /// Verifies the complete cryptographic hash chain across all stored events.
    ///
    /// # Returns
    /// `VerificationReport` indicating whether all records are untampered or where tampering occurred.
    pub async fn verify_chain(&self) -> VerificationReport {
        let events = self.events.read().await;
        let mut expected_prev_hash = GENESIS_HASH.to_string();

        for (idx, record) in events.iter().enumerate() {
            if record.prev_hash != expected_prev_hash {
                return VerificationReport {
                    is_valid: false,
                    total_records: events.len(),
                    corrupted_at_index: Some(idx),
                    corrupted_record_id: Some(record.id.clone()),
                    message: Some(format!(
                        "Broken hash link at record #{}: prev_hash '{}' != expected '{}'",
                        idx, record.prev_hash, expected_prev_hash
                    )),
                };
            }

            if !verify_record_hash_with_key(record, self.hmac_key.as_deref()) {
                return VerificationReport {
                    is_valid: false,
                    total_records: events.len(),
                    corrupted_at_index: Some(idx),
                    corrupted_record_id: Some(record.id.clone()),
                    message: Some(format!(
                        "Hash signature mismatch at record #{} (ID: '{}')",
                        idx, record.id
                    )),
                };
            }

            expected_prev_hash = record.hash.clone();
        }

        VerificationReport {
            is_valid: true,
            total_records: events.len(),
            corrupted_at_index: None,
            corrupted_record_id: None,
            message: None,
        }
    }

    /// Queries audit events matching the provided filter criteria.
    ///
    /// # Arguments
    /// * `filter` - Search and pagination options.
    ///
    /// # Returns
    /// (Matching records subset, total matching count).
    pub async fn query(&self, filter: &AuditQueryFilter) -> (Vec<AuditEvent>, usize) {
        let events = self.events.read().await;
        let search_pattern = filter.search.as_ref().map(|s| s.to_lowercase());

        let filtered: Vec<&AuditEvent> = events
            .iter()
            .rev() // Newest first
            .filter(|e| {
                if let Some(st) = filter.start_time_ns {
                    if e.timestamp_ns < st {
                        return false;
                    }
                }
                if let Some(et) = filter.end_time_ns {
                    if e.timestamp_ns > et {
                        return false;
                    }
                }
                if let Some(ref act) = filter.actor_id {
                    if e.actor_id.as_deref() != Some(act.as_str()) {
                        return false;
                    }
                }
                if let Some(ref srv) = filter.server_id {
                    if e.server_id.as_deref() != Some(srv.as_str()) {
                        return false;
                    }
                }
                if let Some(ref cap) = filter.capability_id {
                    if e.capability_id.as_deref() != Some(cap.as_str()) {
                        return false;
                    }
                }
                if let Some(ref et) = filter.event_type {
                    if &e.event_type != et {
                        return false;
                    }
                }
                if let Some(ref st) = filter.status {
                    if &e.status != st {
                        return false;
                    }
                }
                if let Some(ref tid) = filter.trace_id {
                    if &e.trace_id != tid {
                        return false;
                    }
                }
                if let Some(ref rid) = filter.request_id {
                    if e.request_id.as_deref() != Some(rid.as_str()) {
                        return false;
                    }
                }
                if let Some(ref needle) = search_pattern {
                    let matches = e.id.to_lowercase().contains(needle)
                        || e.trace_id.to_lowercase().contains(needle)
                        || e.request_id
                            .as_deref()
                            .is_some_and(|v| v.to_lowercase().contains(needle))
                        || e.actor_id
                            .as_deref()
                            .is_some_and(|v| v.to_lowercase().contains(needle))
                        || e.server_id
                            .as_deref()
                            .is_some_and(|v| v.to_lowercase().contains(needle))
                        || e.capability_id
                            .as_deref()
                            .is_some_and(|v| v.to_lowercase().contains(needle))
                        || e.resource_uri
                            .as_deref()
                            .is_some_and(|v| v.to_lowercase().contains(needle))
                        || e.error_code
                            .as_deref()
                            .is_some_and(|v| v.to_lowercase().contains(needle))
                        || e.error_message
                            .as_deref()
                            .is_some_and(|v| v.to_lowercase().contains(needle))
                        || e.operator_id
                            .as_deref()
                            .is_some_and(|v| v.to_lowercase().contains(needle))
                        || e.approval_ticket_id
                            .as_deref()
                            .is_some_and(|v| v.to_lowercase().contains(needle));
                    if !matches {
                        return false;
                    }
                }
                true
            })
            .collect();

        let total_matched = filtered.len();
        let limit = if filter.limit == 0 { 50 } else { filter.limit };
        let paginated = filtered
            .into_iter()
            .skip(filter.offset)
            .take(limit)
            .cloned()
            .collect();

        (paginated, total_matched)
    }

    /// Retrieves an audit event by its unique ID.
    pub async fn get_by_id(&self, id: &str) -> Option<AuditEvent> {
        let events = self.events.read().await;
        events.iter().find(|e| e.id == id).cloned()
    }

    /// Returns the total count of audit events in the store.
    pub async fn count(&self) -> usize {
        self.events.read().await.len()
    }
}

pub type SharedAuditStore = Arc<AuditStore>;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::audit::models::AuditEventStatus;

    #[tokio::test]
    async fn test_store_append_and_verify() {
        let store = AuditStore::in_memory();

        let raw1 = RawAuditEvent {
            event_type: AuditEventType::ToolExecution,
            trace_id: "trace-1".to_string(),
            request_id: Some("req-1".to_string()),
            actor_id: Some("agent-1".to_string()),
            work_item_id: None,
            client_ip: None,
            server_id: Some("srv".to_string()),
            capability_id: Some("srv.tool1".to_string()),
            resource_uri: None,
            sanitized_args: Some(serde_json::json!({"x": 1})),
            sanitized_response: Some(serde_json::json!({"res": "ok"})),
            execution_latency_us: Some(100),
            status: AuditEventStatus::Success,
            error_code: None,
            error_message: None,
            operator_id: None,
            approval_ticket_id: None,
        };

        let raw2 = RawAuditEvent {
            event_type: AuditEventType::ApprovalGranted,
            trace_id: "trace-2".to_string(),
            request_id: None,
            actor_id: Some("agent-2".to_string()),
            work_item_id: None,
            client_ip: None,
            server_id: None,
            capability_id: Some("srv.tool2".to_string()),
            resource_uri: None,
            sanitized_args: None,
            sanitized_response: None,
            execution_latency_us: None,
            status: AuditEventStatus::Success,
            error_code: None,
            error_message: None,
            operator_id: Some("operator-alice".to_string()),
            approval_ticket_id: Some("appr-123".to_string()),
        };

        let ev1 = store.append(raw1).await.unwrap();
        assert_eq!(ev1.prev_hash, GENESIS_HASH);

        let ev2 = store.append(raw2).await.unwrap();
        assert_eq!(ev2.prev_hash, ev1.hash);

        let report = store.verify_chain().await;
        assert!(report.is_valid);
        assert_eq!(report.total_records, 2);

        let (queried, total) = store
            .query(&AuditQueryFilter {
                actor_id: Some("agent-1".to_string()),
                ..Default::default()
            })
            .await;
        assert_eq!(total, 1);
        assert_eq!(queried[0].id, ev1.id);
    }

    #[tokio::test]
    async fn test_store_filter_status_server_search_and_pagination() {
        let store = AuditStore::in_memory();

        let raw1 = RawAuditEvent {
            event_type: AuditEventType::ToolExecution,
            trace_id: "trace-101".to_string(),
            request_id: Some("req-101".to_string()),
            actor_id: Some("agent-alpha".to_string()),
            work_item_id: None,
            client_ip: None,
            server_id: Some("github-mcp".to_string()),
            capability_id: Some("github-mcp.create_issue".to_string()),
            resource_uri: None,
            sanitized_args: Some(serde_json::json!({"title": "Bug in query"})),
            sanitized_response: None,
            execution_latency_us: Some(250),
            status: AuditEventStatus::Success,
            error_code: None,
            error_message: None,
            operator_id: None,
            approval_ticket_id: None,
        };

        let raw2 = RawAuditEvent {
            event_type: AuditEventType::PolicyViolation,
            trace_id: "trace-102".to_string(),
            request_id: Some("req-102".to_string()),
            actor_id: Some("agent-beta".to_string()),
            work_item_id: None,
            client_ip: None,
            server_id: Some("postgres-mcp".to_string()),
            capability_id: Some("postgres-mcp.drop_table".to_string()),
            resource_uri: None,
            sanitized_args: None,
            sanitized_response: None,
            execution_latency_us: Some(10),
            status: AuditEventStatus::Denied,
            error_code: Some("POLICY_DENY".to_string()),
            error_message: Some("Dangerous operation forbidden by security policy".to_string()),
            operator_id: None,
            approval_ticket_id: None,
        };

        let raw3 = RawAuditEvent {
            event_type: AuditEventType::ToolInterceptedHitl,
            trace_id: "trace-103".to_string(),
            request_id: Some("req-103".to_string()),
            actor_id: Some("agent-gamma".to_string()),
            work_item_id: None,
            client_ip: None,
            server_id: Some("postgres-mcp".to_string()),
            capability_id: Some("postgres-mcp.delete_records".to_string()),
            resource_uri: None,
            sanitized_args: None,
            sanitized_response: None,
            execution_latency_us: Some(15),
            status: AuditEventStatus::Intercepted,
            error_code: None,
            error_message: None,
            operator_id: Some("admin-bob".to_string()),
            approval_ticket_id: Some("ticket-999".to_string()),
        };

        let ev1 = store.append(raw1).await.unwrap();
        let ev2 = store.append(raw2).await.unwrap();
        let ev3 = store.append(raw3).await.unwrap();

        // Filter by status = Denied
        let (denied, total_denied) = store
            .query(&AuditQueryFilter {
                status: Some(AuditEventStatus::Denied),
                ..Default::default()
            })
            .await;
        assert_eq!(total_denied, 1);
        assert_eq!(denied[0].id, ev2.id);

        // Filter by server_id = postgres-mcp
        let (pg_events, total_pg) = store
            .query(&AuditQueryFilter {
                server_id: Some("postgres-mcp".to_string()),
                ..Default::default()
            })
            .await;
        assert_eq!(total_pg, 2);
        assert_eq!(pg_events[0].id, ev3.id);
        assert_eq!(pg_events[1].id, ev2.id);

        // Search by keyword "ticket-999"
        let (searched, total_searched) = store
            .query(&AuditQueryFilter {
                search: Some("ticket-999".to_string()),
                ..Default::default()
            })
            .await;
        assert_eq!(total_searched, 1);
        assert_eq!(searched[0].id, ev3.id);

        // Search case-insensitively for "FORBIDDEN"
        let (searched_err, total_err) = store
            .query(&AuditQueryFilter {
                search: Some("forbidden".to_string()),
                ..Default::default()
            })
            .await;
        assert_eq!(total_err, 1);
        assert_eq!(searched_err[0].id, ev2.id);

        // Pagination: limit 1, offset 1
        let (paged, total_all) = store
            .query(&AuditQueryFilter {
                limit: 1,
                offset: 1,
                ..Default::default()
            })
            .await;
        assert_eq!(total_all, 3);
        assert_eq!(paged.len(), 1);
        assert_eq!(paged[0].id, ev2.id);

        // Pagination: limit 1, offset 2
        let (paged2, _) = store
            .query(&AuditQueryFilter {
                limit: 1,
                offset: 2,
                ..Default::default()
            })
            .await;
        assert_eq!(paged2.len(), 1);
        assert_eq!(paged2[0].id, ev1.id);
    }
}