reddb-io-server 1.2.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
//! Operator-grade event bus for high-severity system conditions.
//!
//! # Operator / developer split
//!
//! RedDB telemetry has two audiences:
//!
//! - **Developer signal** (`tracing` spans at `DEBUG` / `INFO`): ephemeral,
//!   high-volume, lives in `red.log` or stdout. Helps engineers trace request
//!   flows and understand runtime behaviour during development.
//!
//! - **Operator-grade events** (this module): low-volume, high-severity
//!   conditions that a production operator *must* see and act on.
//!   Persisted to the tamper-evident audit log first so they survive process
//!   crashes; a `tracing::warn!` breadcrumb lands in the normal log channel
//!   as a secondary copy; `eprintln!` fallback ensures the event is never
//!   silently swallowed even if both sinks fail.
//!
//! `OperatorEvent::emit` always runs synchronously — it is intentionally
//! *not* async so callers in crash paths, signal handlers, and `Drop` impls
//! can call it without an async runtime.

use std::sync::{Arc, OnceLock};

use crate::runtime::audit_log::{
    AuditAuthSource, AuditEvent, AuditField, AuditFieldEscaper, AuditLogger, Outcome,
};

// ---------------------------------------------------------------------------
// Process-wide sink
// ---------------------------------------------------------------------------
//
// The OperatorEvent enum is defined in `telemetry/` but the deepest
// emit sites (storage layer, replication apply loop, signal handlers,
// drop impls) cannot thread an `&AuditLogger` reference through their
// call stacks without a sweeping refactor. To stay surgical (#205) we
// expose a process-wide sink that the runtime registers at startup and
// every emit site consults via `OperatorEvent::emit_global`.
//
// Trade-off: the sink is a `OnceLock<Arc<AuditLogger>>`, which means
// emits that fire *before* the runtime registers the logger fall back
// to `tracing::warn!` + `eprintln!` only — the audit copy is lost. The
// tamper-evident audit copy is the primary record; the breadcrumb /
// stderr fallbacks are the safety net the original `emit(&AuditLogger)`
// shape already accepted, so the degradation is the same one already
// documented in the module rustdoc.

static GLOBAL_SINK: OnceLock<Arc<AuditLogger>> = OnceLock::new();

/// Install the process-wide [`AuditLogger`] used by
/// [`OperatorEvent::emit_global`]. Called once at runtime startup; a
/// second call is a no-op (the first registration wins) so test
/// harnesses that build multiple in-memory runtimes cannot stomp on
/// each other's loggers — they fall back to tracing+eprintln.
pub fn install_global_audit_sink(logger: Arc<AuditLogger>) {
    let _ = GLOBAL_SINK.set(logger);
}

// ---------------------------------------------------------------------------
// OperatorEvent
// ---------------------------------------------------------------------------

/// High-severity system conditions that require operator attention.
///
/// Every variant carries typed [`crate::runtime::audit_log::AuditValue`]
/// fields so adversarial bytes (CRLF, NUL, quote, non-UTF-8) are
/// escape-safe at the audit boundary (ADR 0010).
#[derive(Debug)]
pub enum OperatorEvent {
    /// A replication stream to a follower/replica broke unexpectedly.
    ReplicationBroken { peer: String, reason: String },
    /// Replication state diverged: the follower's committed LSN or
    /// checksum disagrees with the leader.
    Divergence {
        peer: String,
        leader_lsn: u64,
        follower_lsn: u64,
    },
    /// The WAL fsync call failed. Data may be at risk on the current host.
    WalFsyncFailed { path: String, error: String },
    /// Available disk space fell below the configured critical threshold.
    DiskSpaceCritical {
        path: String,
        available_bytes: u64,
        threshold_bytes: u64,
    },
    /// An authentication bypass was detected (e.g. auth gate returned
    /// `allow` for a request that should have been rejected).
    AuthBypass {
        principal: String,
        resource: String,
        detail: String,
    },
    /// An admin capability was granted to a principal at runtime.
    AdminCapabilityGranted {
        granted_to: String,
        capability: String,
        granted_by: String,
    },
    /// Secret rotation failed; the current secret may be stale.
    SecretRotationFailed { secret_ref: String, error: String },
    /// A runtime configuration change was applied to a live instance.
    ConfigChanged {
        key: String,
        old_value: String,
        new_value: String,
        changed_by: String,
    },
    /// The server process failed to start cleanly.
    StartupFailed { phase: String, error: String },
    /// The server process was forced to shut down (e.g. OOM killer,
    /// SIGKILL, unrecoverable error).
    ShutdownForced { reason: String },
    /// On-disk schema metadata is corrupt or inconsistent.
    SchemaCorruption { collection: String, detail: String },
    /// A scheduled or triggered checkpoint failed to complete.
    CheckpointFailed { lsn: u64, error: String },
    /// An admin intent that was started but never reached a terminal phase
    /// (completed or aborted). Emitted by [`super::admin_intent_log::AdminIntentLog::scan_and_report`]
    /// at startup so operators can investigate interrupted operations.
    DanglingAdminIntent {
        id: crate::crypto::uuid::Uuid,
        op: crate::telemetry::admin_intent_log::IntentOp,
        /// Unix milliseconds when the intent was started.
        started_at_ms: u64,
        last_phase: crate::telemetry::admin_intent_log::IntentPhase,
    },
    /// A config-file change was detected but one or more changed fields
    /// require a full server restart to take effect. The change was NOT
    /// applied; the operator must restart to pick it up.
    ConfigChangeRequiresRestart { fields_changed: String },
    /// An ALTER TABLE on a collection with active event subscriptions
    /// added or removed columns. Downstream consumers may see a different
    /// payload shape starting at `lsn`.
    SubscriptionSchemaChange {
        collection: String,
        /// Comma-separated subscription names affected.
        subscription_names: String,
        /// Comma-separated columns added (empty string if none).
        fields_added: String,
        /// Comma-separated columns removed (empty string if none).
        fields_removed: String,
        lsn: u64,
    },
    /// An event could not be delivered to its target queue after all
    /// retry attempts, and was routed to the dead-letter queue instead.
    OutboxDlqActivated {
        queue: String,
        dlq: String,
        reason: String,
    },
}

impl OperatorEvent {
    /// All variant names as CamelCase strings, in declaration order.
    pub fn all_variant_names() -> &'static [&'static str] {
        &[
            "ReplicationBroken",
            "Divergence",
            "WalFsyncFailed",
            "DiskSpaceCritical",
            "AuthBypass",
            "AdminCapabilityGranted",
            "SecretRotationFailed",
            "ConfigChanged",
            "StartupFailed",
            "ShutdownForced",
            "SchemaCorruption",
            "CheckpointFailed",
            "DanglingAdminIntent",
            "ConfigChangeRequiresRestart",
            "SubscriptionSchemaChange",
            "OutboxDlqActivated",
        ]
    }

    /// Return the CamelCase variant name for routing table lookup.
    pub(super) fn variant_name(&self) -> &'static str {
        match self {
            Self::ReplicationBroken { .. } => "ReplicationBroken",
            Self::Divergence { .. } => "Divergence",
            Self::WalFsyncFailed { .. } => "WalFsyncFailed",
            Self::DiskSpaceCritical { .. } => "DiskSpaceCritical",
            Self::AuthBypass { .. } => "AuthBypass",
            Self::AdminCapabilityGranted { .. } => "AdminCapabilityGranted",
            Self::SecretRotationFailed { .. } => "SecretRotationFailed",
            Self::ConfigChanged { .. } => "ConfigChanged",
            Self::StartupFailed { .. } => "StartupFailed",
            Self::ShutdownForced { .. } => "ShutdownForced",
            Self::SchemaCorruption { .. } => "SchemaCorruption",
            Self::CheckpointFailed { .. } => "CheckpointFailed",
            Self::DanglingAdminIntent { .. } => "DanglingAdminIntent",
            Self::ConfigChangeRequiresRestart { .. } => "ConfigChangeRequiresRestart",
            Self::SubscriptionSchemaChange { .. } => "SubscriptionSchemaChange",
            Self::OutboxDlqActivated { .. } => "OutboxDlqActivated",
        }
    }

    /// Emit the event.
    ///
    /// Execution order (per issue #202):
    /// 1. Persist to `audit` — tamper-evident, durable.
    /// 2. `tracing::warn!` breadcrumb — lands in `red.log` / stderr.
    /// 3. `eprintln!` fallback — fires only if the audit write fails,
    ///    ensuring the event is never silently lost.
    ///
    /// Emit the event using the process-wide sink installed by the
    /// runtime at startup. When no sink is installed (early boot,
    /// tests without an audit logger), the tracing breadcrumb and
    /// eprintln fallback still fire so the event is never silently
    /// lost.
    pub fn emit_global(self) {
        match GLOBAL_SINK.get() {
            Some(logger) => self.emit(logger.as_ref()),
            None => {
                let (_, _, summary) = self.decompose();
                tracing::warn!(target: "reddb::operator", "{summary}");
                eprintln!("[reddb::operator] (no audit sink) {summary}");
            }
        }
    }

    pub fn emit(self, audit: &AuditLogger) {
        let (action, fields, summary) = self.decompose();

        let ev = AuditEvent::builder(action)
            .source(AuditAuthSource::System)
            .outcome(Outcome::Error)
            .fields(fields)
            .build();

        // 1. Audit log (primary, tamper-evident).
        let audit_ok = {
            // `record_event` is infallible from the caller's perspective
            // (it falls back to sync write internally), so we treat it as
            // always succeeding for the breadcrumb decision.
            audit.record_event(ev);
            true
        };

        // 2. tracing breadcrumb.
        tracing::warn!(target: "reddb::operator", "{summary}");

        // 3. eprintln fallback — guard against silent loss when audit
        //    is unhealthy (e.g. disk full, writer thread dead).
        if !audit_ok {
            eprintln!("[reddb::operator] {summary}");
        }
    }

    /// Decompose `self` into `(action, audit_fields, human_summary)`.
    pub(super) fn decompose(self) -> (&'static str, Vec<AuditField>, String) {
        match self {
            Self::ReplicationBroken { peer, reason } => {
                let summary = format!("replication broken: peer={peer} reason={reason}");
                let fields = vec![
                    AuditFieldEscaper::field("peer", peer),
                    AuditFieldEscaper::field("reason", reason),
                ];
                ("operator/replication_broken", fields, summary)
            }
            Self::Divergence {
                peer,
                leader_lsn,
                follower_lsn,
            } => {
                let summary = format!(
                    "replication divergence: peer={peer} leader_lsn={leader_lsn} follower_lsn={follower_lsn}"
                );
                let fields = vec![
                    AuditFieldEscaper::field("peer", peer),
                    AuditFieldEscaper::field("leader_lsn", leader_lsn),
                    AuditFieldEscaper::field("follower_lsn", follower_lsn),
                ];
                ("operator/divergence", fields, summary)
            }
            Self::WalFsyncFailed { path, error } => {
                let summary = format!("WAL fsync failed: path={path} error={error}");
                let fields = vec![
                    AuditFieldEscaper::field("path", path),
                    AuditFieldEscaper::field("error", error),
                ];
                ("operator/wal_fsync_failed", fields, summary)
            }
            Self::DiskSpaceCritical {
                path,
                available_bytes,
                threshold_bytes,
            } => {
                let summary = format!(
                    "disk space critical: path={path} available={available_bytes} threshold={threshold_bytes}"
                );
                let fields = vec![
                    AuditFieldEscaper::field("path", path),
                    AuditFieldEscaper::field("available_bytes", available_bytes),
                    AuditFieldEscaper::field("threshold_bytes", threshold_bytes),
                ];
                ("operator/disk_space_critical", fields, summary)
            }
            Self::AuthBypass {
                principal,
                resource,
                detail,
            } => {
                let summary =
                    format!("auth bypass detected: principal={principal} resource={resource}");
                let fields = vec![
                    AuditFieldEscaper::field("principal", principal),
                    AuditFieldEscaper::field("resource", resource),
                    AuditFieldEscaper::field("detail", detail),
                ];
                ("operator/auth_bypass", fields, summary)
            }
            Self::AdminCapabilityGranted {
                granted_to,
                capability,
                granted_by,
            } => {
                let summary = format!(
                    "admin capability granted: to={granted_to} capability={capability} by={granted_by}"
                );
                let fields = vec![
                    AuditFieldEscaper::field("granted_to", granted_to),
                    AuditFieldEscaper::field("capability", capability),
                    AuditFieldEscaper::field("granted_by", granted_by),
                ];
                ("operator/admin_capability_granted", fields, summary)
            }
            Self::SecretRotationFailed { secret_ref, error } => {
                let summary = format!("secret rotation failed: ref={secret_ref} error={error}");
                let fields = vec![
                    AuditFieldEscaper::field("secret_ref", secret_ref),
                    AuditFieldEscaper::field("error", error),
                ];
                ("operator/secret_rotation_failed", fields, summary)
            }
            Self::ConfigChanged {
                key,
                old_value,
                new_value,
                changed_by,
            } => {
                let summary = format!(
                    "config changed: key={key} old={old_value} new={new_value} by={changed_by}"
                );
                let fields = vec![
                    AuditFieldEscaper::field("key", key),
                    AuditFieldEscaper::field("old_value", old_value),
                    AuditFieldEscaper::field("new_value", new_value),
                    AuditFieldEscaper::field("changed_by", changed_by),
                ];
                ("operator/config_changed", fields, summary)
            }
            Self::StartupFailed { phase, error } => {
                let summary = format!("startup failed: phase={phase} error={error}");
                let fields = vec![
                    AuditFieldEscaper::field("phase", phase),
                    AuditFieldEscaper::field("error", error),
                ];
                ("operator/startup_failed", fields, summary)
            }
            Self::ShutdownForced { reason } => {
                let summary = format!("shutdown forced: reason={reason}");
                let fields = vec![AuditFieldEscaper::field("reason", reason)];
                ("operator/shutdown_forced", fields, summary)
            }
            Self::SchemaCorruption { collection, detail } => {
                let summary = format!("schema corruption: collection={collection} detail={detail}");
                let fields = vec![
                    AuditFieldEscaper::field("collection", collection),
                    AuditFieldEscaper::field("detail", detail),
                ];
                ("operator/schema_corruption", fields, summary)
            }
            Self::CheckpointFailed { lsn, error } => {
                let summary = format!("checkpoint failed: lsn={lsn} error={error}");
                let fields = vec![
                    AuditFieldEscaper::field("lsn", lsn),
                    AuditFieldEscaper::field("error", error),
                ];
                ("operator/checkpoint_failed", fields, summary)
            }
            Self::DanglingAdminIntent {
                id,
                op,
                started_at_ms,
                last_phase,
            } => {
                let summary = format!(
                    "dangling admin intent: id={id} op={op} started_at_ms={started_at_ms} last_phase={last_phase}"
                );
                let fields = vec![
                    AuditFieldEscaper::field("id", id.to_string()),
                    AuditFieldEscaper::field("op", op.to_string()),
                    AuditFieldEscaper::field("started_at_ms", started_at_ms),
                    AuditFieldEscaper::field("last_phase", last_phase.to_string()),
                ];
                ("operator/dangling_admin_intent", fields, summary)
            }
            Self::ConfigChangeRequiresRestart { fields_changed } => {
                let summary = format!("config change requires restart: fields={fields_changed}");
                let fields = vec![AuditFieldEscaper::field("fields_changed", fields_changed)];
                ("operator/config_change_requires_restart", fields, summary)
            }
            Self::SubscriptionSchemaChange {
                collection,
                subscription_names,
                fields_added,
                fields_removed,
                lsn,
            } => {
                let summary = format!(
                    "subscription schema change: collection={collection} subscriptions=[{subscription_names}] added=[{fields_added}] removed=[{fields_removed}] lsn={lsn}"
                );
                let fields = vec![
                    AuditFieldEscaper::field("collection", collection),
                    AuditFieldEscaper::field("subscription_names", subscription_names),
                    AuditFieldEscaper::field("fields_added", fields_added),
                    AuditFieldEscaper::field("fields_removed", fields_removed),
                    AuditFieldEscaper::field("lsn", lsn),
                ];
                ("operator/subscription_schema_change", fields, summary)
            }
            Self::OutboxDlqActivated { queue, dlq, reason } => {
                let summary =
                    format!("outbox DLQ activated: queue={queue} dlq={dlq} reason={reason}");
                let fields = vec![
                    AuditFieldEscaper::field("queue", queue),
                    AuditFieldEscaper::field("dlq", dlq),
                    AuditFieldEscaper::field("reason", reason),
                ];
                ("operator/outbox_dlq_activated", fields, summary)
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::*;
    use crate::runtime::audit_log::AuditLogger;

    fn make_logger() -> (AuditLogger, std::path::PathBuf) {
        let mut dir = std::env::temp_dir();
        dir.push(format!(
            "reddb-op-event-{}-{}",
            std::process::id(),
            crate::utils::now_unix_nanos()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join(".audit.log");
        let logger = AuditLogger::with_path(path.clone());
        (logger, path)
    }

    fn drain(logger: &AuditLogger) {
        assert!(
            logger.wait_idle(Duration::from_secs(2)),
            "audit logger drain timed out"
        );
    }

    fn read_last_line(path: &std::path::Path) -> crate::json::Value {
        let body = std::fs::read_to_string(path).unwrap();
        let line = body.lines().last().expect("at least one audit line");
        crate::json::from_str(line).expect("valid JSON")
    }

    // ------------------------------------------------------------------
    // One test per variant — verifies action string + a representative field
    // ------------------------------------------------------------------

    #[test]
    fn replication_broken_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::ReplicationBroken {
            peer: "replica-1".into(),
            reason: "TCP reset".into(),
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/replication_broken")
        );
        let peer = v
            .get("detail")
            .and_then(|d| d.get("peer"))
            .and_then(|x| x.as_str());
        assert_eq!(peer, Some("replica-1"));
    }

    #[test]
    fn divergence_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::Divergence {
            peer: "replica-2".into(),
            leader_lsn: 1000,
            follower_lsn: 999,
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/divergence")
        );
        let lsn = v
            .get("detail")
            .and_then(|d| d.get("leader_lsn"))
            .and_then(|x| x.as_i64());
        assert_eq!(lsn, Some(1000));
    }

    #[test]
    fn wal_fsync_failed_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::WalFsyncFailed {
            path: "/data/wal".into(),
            error: "EIO".into(),
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/wal_fsync_failed")
        );
        let err = v
            .get("detail")
            .and_then(|d| d.get("error"))
            .and_then(|x| x.as_str());
        assert_eq!(err, Some("EIO"));
    }

    #[test]
    fn disk_space_critical_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::DiskSpaceCritical {
            path: "/data".into(),
            available_bytes: 1024,
            threshold_bytes: 10240,
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/disk_space_critical")
        );
        let avail = v
            .get("detail")
            .and_then(|d| d.get("available_bytes"))
            .and_then(|x| x.as_i64());
        assert_eq!(avail, Some(1024));
    }

    #[test]
    fn auth_bypass_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::AuthBypass {
            principal: "alice".into(),
            resource: "/admin/drop".into(),
            detail: "scope check skipped".into(),
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/auth_bypass")
        );
        let res = v
            .get("detail")
            .and_then(|d| d.get("resource"))
            .and_then(|x| x.as_str());
        assert_eq!(res, Some("/admin/drop"));
    }

    #[test]
    fn admin_capability_granted_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::AdminCapabilityGranted {
            granted_to: "bob".into(),
            capability: "ADMIN_WRITE".into(),
            granted_by: "root".into(),
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/admin_capability_granted")
        );
        let cap = v
            .get("detail")
            .and_then(|d| d.get("capability"))
            .and_then(|x| x.as_str());
        assert_eq!(cap, Some("ADMIN_WRITE"));
    }

    #[test]
    fn secret_rotation_failed_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::SecretRotationFailed {
            secret_ref: "jwt-signing-key".into(),
            error: "HSM unreachable".into(),
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/secret_rotation_failed")
        );
        let r = v
            .get("detail")
            .and_then(|d| d.get("secret_ref"))
            .and_then(|x| x.as_str());
        assert_eq!(r, Some("jwt-signing-key"));
    }

    #[test]
    fn config_changed_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::ConfigChanged {
            key: "max_connections".into(),
            old_value: "100".into(),
            new_value: "200".into(),
            changed_by: "ops-bot".into(),
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/config_changed")
        );
        let nv = v
            .get("detail")
            .and_then(|d| d.get("new_value"))
            .and_then(|x| x.as_str());
        assert_eq!(nv, Some("200"));
    }

    #[test]
    fn startup_failed_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::StartupFailed {
            phase: "wal_recovery".into(),
            error: "corrupt frame".into(),
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/startup_failed")
        );
        let phase = v
            .get("detail")
            .and_then(|d| d.get("phase"))
            .and_then(|x| x.as_str());
        assert_eq!(phase, Some("wal_recovery"));
    }

    #[test]
    fn shutdown_forced_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::ShutdownForced {
            reason: "OOM".into(),
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/shutdown_forced")
        );
        let r = v
            .get("detail")
            .and_then(|d| d.get("reason"))
            .and_then(|x| x.as_str());
        assert_eq!(r, Some("OOM"));
    }

    #[test]
    fn schema_corruption_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::SchemaCorruption {
            collection: "users".into(),
            detail: "unknown type tag 0xFF".into(),
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/schema_corruption")
        );
        let coll = v
            .get("detail")
            .and_then(|d| d.get("collection"))
            .and_then(|x| x.as_str());
        assert_eq!(coll, Some("users"));
    }

    #[test]
    fn checkpoint_failed_emits() {
        let (logger, path) = make_logger();
        OperatorEvent::CheckpointFailed {
            lsn: 42_000,
            error: "write stall".into(),
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(
            v.get("action").and_then(|x| x.as_str()),
            Some("operator/checkpoint_failed")
        );
        let lsn = v
            .get("detail")
            .and_then(|d| d.get("lsn"))
            .and_then(|x| x.as_i64());
        assert_eq!(lsn, Some(42_000));
    }

    // ------------------------------------------------------------------
    // Adversarial corpus: CRLF / NUL / quote / non-UTF-8-ish in fields
    // ------------------------------------------------------------------

    #[test]
    fn adversarial_fields_are_escape_safe() {
        let payloads: &[(&str, &str)] = &[
            ("crlf", "line1\r\nline2"),
            ("nul", "before\0after"),
            ("quote", r#"she said "hi""#),
            ("json_inject", r#"{"injected":true}"#),
            ("low_ctrl", "\x01\x02\x07\x1f"),
            ("backslash", "C:\\path\\file"),
            ("mixed", "name=\"x\"\n\\path\t\x01end"),
        ];

        for (label, payload) in payloads {
            let (logger, path) = make_logger();
            OperatorEvent::SchemaCorruption {
                collection: payload.to_string(),
                detail: payload.to_string(),
            }
            .emit(&logger);
            drain(&logger);

            let body = std::fs::read_to_string(&path).unwrap();
            let line = body.lines().last().unwrap_or("");

            // Single JSONL row — no embedded newline.
            assert!(
                !line.contains('\n'),
                "{label}: embedded newline in JSONL row"
            );

            let v: crate::json::Value = crate::json::from_str(line)
                .unwrap_or_else(|e| panic!("{label}: audit line not valid JSON: {e}\n{line:?}"));
            let recovered = v
                .get("detail")
                .and_then(|d| d.get("collection"))
                .and_then(|x| x.as_str())
                .unwrap_or("");
            assert_eq!(recovered, *payload, "{label}: round-trip mismatch");
        }
    }

    // ------------------------------------------------------------------
    // Outcome is always Error; source is always System
    // ------------------------------------------------------------------

    #[test]
    fn emit_sets_outcome_error_and_source_system() {
        let (logger, path) = make_logger();
        OperatorEvent::ShutdownForced {
            reason: "test".into(),
        }
        .emit(&logger);
        drain(&logger);
        let v = read_last_line(&path);
        assert_eq!(v.get("outcome").and_then(|x| x.as_str()), Some("error"));
        assert_eq!(v.get("source").and_then(|x| x.as_str()), Some("system"));
    }
}