bote 0.91.0

MCP core service — JSON-RPC 2.0 protocol, tool registry, audit integration, and TypeScript bridge
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
//! Built-in MCP tools for querying, verifying, and exporting libro audit chains.
//!
//! Requires the `audit` feature. These tools operate on the audit chain
//! attached to the dispatcher via [`LibroAudit`](crate::audit::LibroAudit).
//!
//! ## Tools
//!
//! | Tool | Description | Annotations |
//! |------|-------------|-------------|
//! | `libro_query` | Query audit entries by source, severity, action, agent, or time range | read-only |
//! | `libro_verify` | Verify the audit chain's cryptographic integrity | read-only |
//! | `libro_export` | Export the audit chain as JSON Lines or CSV | read-only |
//! | `libro_proof` | Generate a Merkle inclusion proof for an entry | read-only |
//! | `libro_retention` | Apply a retention policy and report pruned entries | destructive |

use std::collections::HashMap;
use std::sync::Arc;

use crate::audit::LibroAudit;
use crate::dispatch::ToolHandler;
use crate::registry::{ToolAnnotations, ToolDef, ToolRegistry, ToolSchema};

/// Register the libro audit tools on a registry and return their handlers.
///
/// The caller should attach these handlers to the dispatcher:
/// ```rust,ignore
/// let audit = Arc::new(LibroAudit::new());
/// let handlers = libro_tools::register(&mut registry, Arc::clone(&audit));
/// for (name, handler) in handlers {
///     dispatcher.handle(name, handler);
/// }
/// ```
pub fn register(registry: &mut ToolRegistry, audit: Arc<LibroAudit>) -> Vec<(String, ToolHandler)> {
    let mut handlers = Vec::new();

    register_query(registry, &audit, &mut handlers);
    register_verify(registry, &audit, &mut handlers);
    register_export(registry, &audit, &mut handlers);
    register_proof(registry, &audit, &mut handlers);
    register_retention(registry, &audit, &mut handlers);

    handlers
}

// ---------------------------------------------------------------------------
// libro_query
// ---------------------------------------------------------------------------

fn register_query(
    registry: &mut ToolRegistry,
    audit: &Arc<LibroAudit>,
    handlers: &mut Vec<(String, ToolHandler)>,
) {
    registry.register(ToolDef {
        name: "libro_query".into(),
        description: "Query audit chain entries by source, severity, action, agent, or time range"
            .into(),
        input_schema: ToolSchema {
            schema_type: "object".into(),
            properties: HashMap::from([
                (
                    "source".into(),
                    serde_json::json!({"type": "string", "description": "Filter by event source"}),
                ),
                (
                    "severity".into(),
                    serde_json::json!({"type": "string", "description": "Filter by exact severity (Debug, Info, Warning, Error, Critical, Security)"}),
                ),
                (
                    "action".into(),
                    serde_json::json!({"type": "string", "description": "Filter by action"}),
                ),
                (
                    "agent_id".into(),
                    serde_json::json!({"type": "string", "description": "Filter by agent ID"}),
                ),
                (
                    "min_severity".into(),
                    serde_json::json!({"type": "string", "description": "Filter by minimum severity (inclusive)"}),
                ),
                (
                    "limit".into(),
                    serde_json::json!({"type": "integer", "description": "Maximum entries to return (default: 100)"}),
                ),
            ]),
            required: vec![],
        },
        version: Some("0.91.0".into()),
        deprecated: None,
        annotations: Some(ToolAnnotations::read_only()),
    });

    let audit_q = Arc::clone(audit);
    handlers.push((
        "libro_query".into(),
        Arc::new(move |params: serde_json::Value| {
            let chain = audit_q.chain();
            let mut filter = libro::QueryFilter::new();

            if let Some(s) = params.get("source").and_then(|v| v.as_str()) {
                filter = filter.source(s);
            }
            if let Some(s) = params.get("action").and_then(|v| v.as_str()) {
                filter = filter.action(s);
            }
            if let Some(s) = params.get("agent_id").and_then(|v| v.as_str()) {
                filter = filter.agent_id(s);
            }
            if let Some(s) = params.get("severity").and_then(|v| v.as_str())
                && let Some(sev) = parse_severity(s)
            {
                filter = filter.severity(sev);
            }
            if let Some(s) = params.get("min_severity").and_then(|v| v.as_str())
                && let Some(sev) = parse_severity(s)
            {
                filter = filter.min_severity(sev);
            }

            let limit = params.get("limit").and_then(|v| v.as_u64()).unwrap_or(100) as usize;

            let results: Vec<&libro::AuditEntry> =
                chain.query(&filter).into_iter().take(limit).collect();
            let json = serde_json::to_string_pretty(&results).unwrap_or_default();

            serde_json::json!({
                "content": [{
                    "type": "text",
                    "text": format!("{} entries matched\n{json}", results.len())
                }]
            })
        }) as ToolHandler,
    ));
}

// ---------------------------------------------------------------------------
// libro_verify — returns structured ChainReview JSON
// ---------------------------------------------------------------------------

fn register_verify(
    registry: &mut ToolRegistry,
    audit: &Arc<LibroAudit>,
    handlers: &mut Vec<(String, ToolHandler)>,
) {
    registry.register(ToolDef {
        name: "libro_verify".into(),
        description: "Verify the audit chain's cryptographic integrity — returns structured review with integrity status, entry count, time range, and source/severity/agent distributions".into(),
        input_schema: ToolSchema {
            schema_type: "object".into(),
            properties: HashMap::new(),
            required: vec![],
        },
        version: Some("0.91.0".into()),
        deprecated: None,
        annotations: Some(ToolAnnotations::read_only()),
    });

    let audit_v = Arc::clone(audit);
    handlers.push((
        "libro_verify".into(),
        Arc::new(move |_params: serde_json::Value| {
            let chain = audit_v.chain();
            let review = chain.review();

            // Return structured JSON — ChainReview is Serialize.
            let review_json = serde_json::to_value(&review).unwrap_or_default();

            serde_json::json!({
                "content": [{
                    "type": "text",
                    "text": serde_json::to_string_pretty(&review_json).unwrap_or_default()
                }],
                "_meta": {
                    "review": review_json
                }
            })
        }) as ToolHandler,
    ));
}

// ---------------------------------------------------------------------------
// libro_export
// ---------------------------------------------------------------------------

fn register_export(
    registry: &mut ToolRegistry,
    audit: &Arc<LibroAudit>,
    handlers: &mut Vec<(String, ToolHandler)>,
) {
    registry.register(ToolDef {
        name: "libro_export".into(),
        description: "Export the audit chain as JSON Lines or CSV".into(),
        input_schema: ToolSchema {
            schema_type: "object".into(),
            properties: HashMap::from([(
                "format".into(),
                serde_json::json!({"type": "string", "enum": ["jsonl", "csv"], "description": "Export format (default: jsonl)"}),
            )]),
            required: vec![],
        },
        version: Some("0.91.0".into()),
        deprecated: None,
        annotations: Some(ToolAnnotations::read_only()),
    });

    let audit_e = Arc::clone(audit);
    handlers.push((
        "libro_export".into(),
        Arc::new(move |params: serde_json::Value| {
            let chain = audit_e.chain();
            let format = params
                .get("format")
                .and_then(|v| v.as_str())
                .unwrap_or("jsonl");

            let mut buf = Vec::new();
            let result = match format {
                "csv" => libro::to_csv(chain.entries(), &mut buf),
                _ => libro::to_jsonl(chain.entries(), &mut buf),
            };

            match result {
                Ok(()) => {
                    let text = String::from_utf8_lossy(&buf).into_owned();
                    serde_json::json!({
                        "content": [{
                            "type": "text",
                            "text": text
                        }]
                    })
                }
                Err(e) => serde_json::json!({
                    "content": [{
                        "type": "text",
                        "text": format!("export failed: {e}")
                    }],
                    "isError": true
                }),
            }
        }) as ToolHandler,
    ));
}

// ---------------------------------------------------------------------------
// libro_proof — Merkle inclusion proof
// ---------------------------------------------------------------------------

fn register_proof(
    registry: &mut ToolRegistry,
    audit: &Arc<LibroAudit>,
    handlers: &mut Vec<(String, ToolHandler)>,
) {
    registry.register(ToolDef {
        name: "libro_proof".into(),
        description: "Generate a Merkle inclusion proof for an audit entry by index — enables O(log N) verification without the full chain".into(),
        input_schema: ToolSchema {
            schema_type: "object".into(),
            properties: HashMap::from([(
                "index".into(),
                serde_json::json!({"type": "integer", "description": "Entry index (0-based)"}),
            )]),
            required: vec!["index".into()],
        },
        version: Some("0.91.0".into()),
        deprecated: None,
        annotations: Some(ToolAnnotations::read_only()),
    });

    let audit_p = Arc::clone(audit);
    handlers.push((
        "libro_proof".into(),
        Arc::new(move |params: serde_json::Value| {
            let chain = audit_p.chain();
            let entries = chain.entries();

            let index = match params.get("index").and_then(|v| v.as_u64()) {
                Some(i) => i as usize,
                None => {
                    return serde_json::json!({
                        "content": [{"type": "text", "text": "missing required 'index' parameter"}],
                        "isError": true
                    });
                }
            };

            if index >= entries.len() {
                return serde_json::json!({
                    "content": [{"type": "text", "text": format!("index {index} out of range (chain has {} entries)", entries.len())}],
                    "isError": true
                });
            }

            let tree = match libro::MerkleTree::build(entries) {
                Some(t) => t,
                None => {
                    return serde_json::json!({
                        "content": [{"type": "text", "text": "chain is empty — no Merkle tree"}],
                        "isError": true
                    });
                }
            };

            match tree.proof(index) {
                Some(proof) => {
                    let verified = libro::merkle::verify_proof(&proof);
                    let proof_json = serde_json::to_value(&proof).unwrap_or_default();
                    serde_json::json!({
                        "content": [{
                            "type": "text",
                            "text": format!(
                                "Merkle proof for entry {index}:\n  Leaf: {}\n  Root: {}\n  Path length: {}\n  Verified: {verified}",
                                proof.leaf_hash, proof.root, proof.path.len()
                            )
                        }],
                        "_meta": {
                            "proof": proof_json,
                            "verified": verified
                        }
                    })
                }
                None => serde_json::json!({
                    "content": [{"type": "text", "text": format!("failed to generate proof for index {index}")}],
                    "isError": true
                }),
            }
        }) as ToolHandler,
    ));
}

// ---------------------------------------------------------------------------
// libro_retention — apply compliance retention policies
// ---------------------------------------------------------------------------

fn register_retention(
    registry: &mut ToolRegistry,
    audit: &Arc<LibroAudit>,
    handlers: &mut Vec<(String, ToolHandler)>,
) {
    registry.register(ToolDef {
        name: "libro_retention".into(),
        description: "Apply a retention policy to the audit chain — archives entries outside the retention window. Supports PCI-DSS (1yr), HIPAA (6yr), SOX (7yr), or custom count/duration policies.".into(),
        input_schema: ToolSchema {
            schema_type: "object".into(),
            properties: HashMap::from([
                (
                    "policy".into(),
                    serde_json::json!({"type": "string", "enum": ["pci_dss", "hipaa", "sox", "keep_count"], "description": "Retention policy preset"}),
                ),
                (
                    "count".into(),
                    serde_json::json!({"type": "integer", "description": "Number of entries to keep (for keep_count policy)"}),
                ),
            ]),
            required: vec!["policy".into()],
        },
        version: Some("0.91.0".into()),
        deprecated: None,
        annotations: None, // destructive — not read-only
    });

    let audit_r = Arc::clone(audit);
    handlers.push((
        "libro_retention".into(),
        Arc::new(move |params: serde_json::Value| {
            let policy_name = params.get("policy").and_then(|v| v.as_str()).unwrap_or("");

            let policy = match policy_name {
                "pci_dss" => libro::RetentionPolicy::pci_dss(),
                "hipaa" => libro::RetentionPolicy::hipaa(),
                "sox" => libro::RetentionPolicy::sox(),
                "keep_count" => {
                    let count = params.get("count").and_then(|v| v.as_u64()).unwrap_or(1000) as usize;
                    libro::RetentionPolicy::KeepCount(count)
                }
                _ => {
                    return serde_json::json!({
                        "content": [{"type": "text", "text": format!("unknown policy: {policy_name}. Use: pci_dss, hipaa, sox, keep_count")}],
                        "isError": true
                    });
                }
            };

            let mut chain = audit_r.chain();
            let before = chain.len();
            let archive = chain.apply_retention(&policy);
            let after = chain.len();
            let archived_count = archive.map(|a| a.entries.len()).unwrap_or(0);

            serde_json::json!({
                "content": [{
                    "type": "text",
                    "text": format!(
                        "Retention policy '{policy_name}' applied:\n  Before: {before} entries\n  After: {after} entries\n  Archived: {archived_count} entries"
                    )
                }],
                "_meta": {
                    "policy": policy_name,
                    "before": before,
                    "after": after,
                    "archived": archived_count
                }
            })
        }) as ToolHandler,
    ));
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn parse_severity(s: &str) -> Option<libro::EventSeverity> {
    match s {
        "Debug" => Some(libro::EventSeverity::Debug),
        "Info" => Some(libro::EventSeverity::Info),
        "Warning" => Some(libro::EventSeverity::Warning),
        "Error" => Some(libro::EventSeverity::Error),
        "Critical" => Some(libro::EventSeverity::Critical),
        "Security" => Some(libro::EventSeverity::Security),
        _ => None,
    }
}

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

    fn make_audit_with_entries(n: usize) -> Arc<LibroAudit> {
        let audit = Arc::new(LibroAudit::new());
        for i in 0..n {
            let event = crate::audit::ToolCallEvent::new(
                format!("tool_{i}"),
                i as u64 * 10,
                i % 3 != 0, // every 3rd call fails
                if i % 3 == 0 {
                    Some("simulated failure".into())
                } else {
                    None
                },
                Some(format!("agent-{}", i % 2)),
            );
            audit.log(&event);
        }
        audit
    }

    // --- libro_query ---

    #[test]
    fn libro_query_no_filter() {
        let audit = make_audit_with_entries(5);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[0].1;

        let result = handler(serde_json::json!({}));
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.starts_with("5 entries matched"));
    }

    #[test]
    fn libro_query_with_source_filter() {
        let audit = make_audit_with_entries(5);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[0].1;

        let result = handler(serde_json::json!({"source": "bote"}));
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("5 entries matched"));
    }

    #[test]
    fn libro_query_with_severity_filter() {
        let audit = make_audit_with_entries(6);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[0].1;

        let result = handler(serde_json::json!({"severity": "Error"}));
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("2 entries matched"));
    }

    #[test]
    fn libro_query_with_limit() {
        let audit = make_audit_with_entries(10);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[0].1;

        let result = handler(serde_json::json!({"limit": 3}));
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.starts_with("3 entries matched"));
    }

    // --- libro_verify ---

    #[test]
    fn libro_verify_returns_structured_review() {
        let audit = make_audit_with_entries(3);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[1].1;

        let result = handler(serde_json::json!({}));
        // Text output contains structured JSON
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("\"integrity\""));
        assert!(text.contains("\"entry_count\""));

        // _meta contains structured review
        let meta = &result["_meta"]["review"];
        assert_eq!(meta["entry_count"], 3);
        assert_eq!(meta["integrity"], "Valid");
        assert!(meta["head_hash"].is_string());
    }

    #[test]
    fn libro_verify_empty_chain() {
        let audit = make_audit_with_entries(0);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[1].1;

        let result = handler(serde_json::json!({}));
        let meta = &result["_meta"]["review"];
        assert_eq!(meta["entry_count"], 0);
        assert_eq!(meta["integrity"], "Empty");
    }

    // --- libro_export ---

    #[test]
    fn libro_export_jsonl() {
        let audit = make_audit_with_entries(3);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[2].1;

        let result = handler(serde_json::json!({}));
        let text = result["content"][0]["text"].as_str().unwrap();
        let lines: Vec<&str> = text.trim().lines().collect();
        assert_eq!(lines.len(), 3);
    }

    #[test]
    fn libro_export_csv() {
        let audit = make_audit_with_entries(3);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[2].1;

        let result = handler(serde_json::json!({"format": "csv"}));
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.starts_with("id,timestamp,severity,"));
        let lines: Vec<&str> = text.trim().lines().collect();
        assert_eq!(lines.len(), 4); // header + 3 entries
    }

    // --- libro_proof ---

    #[test]
    fn libro_proof_generates_valid_proof() {
        let audit = make_audit_with_entries(5);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[3].1;

        let result = handler(serde_json::json!({"index": 2}));
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("Merkle proof for entry 2"));
        assert!(text.contains("Verified: true"));

        // _meta contains structured proof
        let meta = &result["_meta"];
        assert_eq!(meta["verified"], true);
        assert!(meta["proof"]["leaf_hash"].is_string());
        assert!(meta["proof"]["root"].is_string());
        assert!(meta["proof"]["path"].is_array());
    }

    #[test]
    fn libro_proof_index_out_of_range() {
        let audit = make_audit_with_entries(3);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[3].1;

        let result = handler(serde_json::json!({"index": 10}));
        assert_eq!(result["isError"], true);
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("out of range"));
    }

    #[test]
    fn libro_proof_empty_chain() {
        let audit = make_audit_with_entries(0);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[3].1;

        let result = handler(serde_json::json!({"index": 0}));
        assert_eq!(result["isError"], true);
    }

    #[test]
    fn libro_proof_missing_index() {
        let audit = make_audit_with_entries(3);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[3].1;

        let result = handler(serde_json::json!({}));
        assert_eq!(result["isError"], true);
    }

    // --- libro_retention ---

    #[test]
    fn libro_retention_keep_count() {
        let audit = make_audit_with_entries(10);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[4].1;

        let result = handler(serde_json::json!({"policy": "keep_count", "count": 3}));
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("Before: 10"));
        assert!(text.contains("After: 3"));
        assert!(text.contains("Archived: 7"));

        let meta = &result["_meta"];
        assert_eq!(meta["before"], 10);
        assert_eq!(meta["after"], 3);
        assert_eq!(meta["archived"], 7);
    }

    #[test]
    fn libro_retention_unknown_policy() {
        let audit = make_audit_with_entries(3);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[4].1;

        let result = handler(serde_json::json!({"policy": "nonsense"}));
        assert_eq!(result["isError"], true);
    }

    #[test]
    fn libro_retention_pci_dss_on_fresh_chain() {
        // Fresh chain — all entries are recent, nothing to prune
        let audit = make_audit_with_entries(5);
        let mut registry = ToolRegistry::new();
        let handlers = register(&mut registry, Arc::clone(&audit));
        let handler = &handlers[4].1;

        let result = handler(serde_json::json!({"policy": "pci_dss"}));
        let meta = &result["_meta"];
        assert_eq!(meta["before"], 5);
        assert_eq!(meta["after"], 5); // nothing pruned, all recent
        assert_eq!(meta["archived"], 0);
    }

    // --- registration ---

    #[test]
    fn all_tools_registered() {
        let audit = make_audit_with_entries(0);
        let mut registry = ToolRegistry::new();
        let _ = register(&mut registry, audit);

        assert!(registry.get("libro_query").is_some());
        assert!(registry.get("libro_verify").is_some());
        assert!(registry.get("libro_export").is_some());
        assert!(registry.get("libro_proof").is_some());
        assert!(registry.get("libro_retention").is_some());
    }

    #[test]
    fn read_only_tools_annotated() {
        let audit = make_audit_with_entries(0);
        let mut registry = ToolRegistry::new();
        let _ = register(&mut registry, audit);

        for name in ["libro_query", "libro_verify", "libro_export", "libro_proof"] {
            let def = registry.get(name).unwrap();
            assert_eq!(
                def.annotations.as_ref().unwrap().read_only_hint,
                Some(true),
                "{name} should be read-only"
            );
        }

        // libro_retention is NOT read-only (destructive)
        let ret_def = registry.get("libro_retention").unwrap();
        assert!(ret_def.annotations.is_none());
    }
}