leindex 1.9.0

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
//! Observability infrastructure: distributed tracing, metrics collection,
//! error tracking, and log scrubbing for LeIndex.
//!
//! This module provides:
//! - OpenTelemetry-compatible distributed tracing with trace ID propagation
//! - Prometheus-style metrics collection (counters, histograms)
//! - Local error tracking via ring buffer (not yet remote)
//! - Log redaction/sanitization to prevent secrets leaking into logs
//!
//! ## Configuration
//!
//! Set these environment variables to enable observability features:
//! - `OTEL_EXPORTER_OTLP_ENDPOINT`: OTLP collector endpoint for traces
//! - `LEINDEX_METRICS_ENDPOINT`: Prometheus metrics scrape endpoint
//! - `RUST_LOG`: Log level filter (e.g., "info,leindex=debug")
//!
//! ## Feature Flags
//!
//! Observability features are gated by cargo features:
//! - `tracing` (always on via `tracing` crate): structured logging
//! - `metrics`: Prometheus metrics collection
//! - `otel`: OpenTelemetry distributed tracing export

use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Mutex, OnceLock};
use std::time::Instant;

/// A distributed trace span tracking a unit of work.
///
/// Each span has a trace ID (propagated across process boundaries) and
/// a span ID (unique within a trace). Spans nest to form a trace tree.
pub struct TraceSpan {
    /// Trace ID (128-bit hex string, propagated across process boundaries)
    pub trace_id: String,
    /// Span ID (64-bit hex string, unique within this trace)
    pub span_id: String,
    /// Parent span ID (empty for root spans)
    pub parent_span_id: String,
    /// Span name (operation being traced)
    pub name: String,
    /// Start time
    pub start: Instant,
    /// Attributes attached to this span
    pub attributes: HashMap<String, String>,
}

impl TraceSpan {
    /// Creates a new root span (no parent).
    pub fn root(name: &str) -> Self {
        Self {
            trace_id: generate_trace_id(),
            span_id: generate_span_id(),
            parent_span_id: String::new(),
            name: name.to_string(),
            start: Instant::now(),
            attributes: HashMap::new(),
        }
    }

    /// Creates a child span of the given parent.
    pub fn child(name: &str, parent: &Self) -> Self {
        Self {
            trace_id: parent.trace_id.clone(),
            span_id: generate_span_id(),
            parent_span_id: parent.span_id.clone(),
            name: name.to_string(),
            start: Instant::now(),
            attributes: HashMap::new(),
        }
    }

    /// Adds an attribute to this span.
    pub fn with_attribute(mut self, key: &str, value: &str) -> Self {
        self.attributes.insert(key.to_string(), value.to_string());
        self
    }

    /// Returns the elapsed time since span creation.
    pub fn elapsed_ms(&self) -> u64 {
        self.start.elapsed().as_millis() as u64
    }

    /// Returns the X-Trace-Id header value for HTTP propagation.
    pub fn trace_header(&self) -> (String, String) {
        ("X-Trace-Id".to_string(), self.trace_id.clone())
    }

    /// Returns the X-Span-Id header value for HTTP propagation.
    pub fn span_header(&self) -> (String, String) {
        ("X-Span-Id".to_string(), self.span_id.clone())
    }
}

fn generate_trace_id() -> String {
    static COUNTER: AtomicU64 = AtomicU64::new(1);
    let n = COUNTER.fetch_add(1, Ordering::Relaxed);
    let ts = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    format!("{:016x}{:016x}", ts, n)
}

fn generate_span_id() -> String {
    static COUNTER: AtomicU64 = AtomicU64::new(1);
    let n = COUNTER.fetch_add(1, Ordering::Relaxed);
    format!("{:016x}", n)
}

/// Counter metric that only increases.
pub struct Counter {
    name: String,
    value: AtomicU64,
    help: String,
}

impl Counter {
    /// Creates a new counter.
    pub fn new(name: &str, help: &str) -> Self {
        Self {
            name: name.to_string(),
            value: AtomicU64::new(0),
            help: help.to_string(),
        }
    }

    /// Increments the counter by 1.
    pub fn inc(&self) {
        self.value.fetch_add(1, Ordering::Relaxed);
    }

    /// Increments the counter by n.
    pub fn add(&self, n: u64) {
        self.value.fetch_add(n, Ordering::Relaxed);
    }

    /// Returns the current value.
    pub fn get(&self) -> u64 {
        self.value.load(Ordering::Relaxed)
    }

    /// Renders this counter in Prometheus text exposition format.
    pub fn render_prometheus(&self) -> String {
        format!(
            "# HELP {} {}\n# TYPE {} counter\n{} {}",
            self.name,
            self.help,
            self.name,
            self.name,
            self.get()
        )
    }
}

/// Histogram metric for tracking distributions (latency, etc.).
pub struct Histogram {
    name: String,
    buckets: Vec<f64>,
    counts: Vec<AtomicU64>,
    sum: Mutex<f64>,
    count: AtomicU64,
}

impl Histogram {
    /// Creates a new histogram with the given bucket boundaries.
    pub fn new(name: &str, buckets: Vec<f64>) -> Self {
        let b = buckets.len();
        Self {
            name: name.to_string(),
            buckets,
            counts: (0..=b).map(|_| AtomicU64::new(0)).collect(),
            sum: Mutex::new(0.0),
            count: AtomicU64::new(0),
        }
    }

    /// Records an observation.
    pub fn observe(&self, value: f64) {
        self.count.fetch_add(1, Ordering::Relaxed);
        if let Ok(mut s) = self.sum.lock() {
            *s += value;
        }
        for (i, &bound) in self.buckets.iter().enumerate() {
            if value <= bound {
                self.counts[i].fetch_add(1, Ordering::Relaxed);
                return;
            }
        }
        self.counts[self.buckets.len()].fetch_add(1, Ordering::Relaxed);
    }

    /// Renders this histogram in Prometheus text exposition format.
    pub fn render_prometheus(&self) -> String {
        let mut out = format!("# TYPE {} histogram\n", self.name);
        let mut cumulative: u64 = 0;
        for (i, &bound) in self.buckets.iter().enumerate() {
            cumulative += self.counts[i].load(Ordering::Relaxed);
            out.push_str(&format!(
                "{}_bucket{{le=\"{}\"}} {}\n",
                self.name, bound, cumulative
            ));
        }
        cumulative += self.counts[self.buckets.len()].load(Ordering::Relaxed);
        out.push_str(&format!(
            "{}_bucket{{le=\"+Inf\"}} {}\n",
            self.name, cumulative
        ));
        let sum_val = self.sum.lock().map(|v| *v).unwrap_or(0.0);
        out.push_str(&format!("{}_sum {}\n", self.name, sum_val));
        out.push_str(&format!(
            "{}_count {}\n",
            self.name,
            self.count.load(Ordering::Relaxed)
        ));
        out
    }
}

/// Error event sent to error tracking (Sentry, Bugsnag, etc.)
pub struct ErrorEvent {
    /// The error message
    pub message: String,
    /// The error level
    pub level: ErrorLevel,
    /// Trace ID for correlating with distributed traces
    pub trace_id: Option<String>,
    /// Source file and line
    pub location: Option<String>,
    /// Additional context
    pub context: HashMap<String, String>,
}

/// Error severity level.
#[derive(Debug, Clone, Copy)]
pub enum ErrorLevel {
    /// Error-level event: something went wrong.
    Error,
    /// Warning-level event: potential issue.
    Warning,
    /// Info-level event: informational.
    Info,
}

/// Error tracker that captures events and (optionally) forwards them
/// to Sentry or a compatible backend.
pub struct ErrorTracker {
    events: std::sync::Mutex<Vec<ErrorEvent>>,
}

static ERROR_TRACKER: OnceLock<ErrorTracker> = OnceLock::new();

impl ErrorTracker {
    fn new() -> Self {
        Self {
            events: std::sync::Mutex::new(Vec::new()),
        }
    }

    /// Gets the global error tracker instance.
    pub fn global() -> &'static Self {
        ERROR_TRACKER.get_or_init(ErrorTracker::new)
    }

    /// Captures an error event.
    ///
    /// The event is appended to the local ring buffer for later retrieval,
    /// **best-effort**: it is silently dropped if the buffer is at its 1000-entry
    /// cap or if the lock is poisoned. (In all cases a `tracing::error!` is
    /// emitted.)
    ///
    /// **NOTE:** Remote forwarding is not yet implemented. The DSN environment
    /// variable is no longer parsed — errors are only logged locally via `tracing`.
    /// This is a local-only error tracking implementation for debugging and
    /// diagnostics.
    pub fn capture(&self, event: ErrorEvent) {
        if self.events.lock().unwrap().len() < 1000 {
            // Local ring buffer for error tracking
            tracing::error!(
                message = %event.message,
                trace_id = ?event.trace_id,
                "Error captured for tracking"
            );
        } else {
            tracing::error!(
                message = %event.message,
                trace_id = ?event.trace_id,
                "Error captured (no remote tracker configured)"
            );
        }
        if let Ok(mut events) = self.events.lock() {
            if events.len() < 1000 {
                events.push(event);
            }
        }
    }
}

/// Log scrubber for redacting sensitive data from log output.
///
/// Replaces patterns matching common secret formats with `[REDACTED]`.
pub struct LogScrubber;

impl LogScrubber {
    /// Redacts known sensitive patterns from a log message.
    ///
    /// Patterns redacted:
    /// - Bearer tokens: `Bearer xxx` -> `Bearer [REDACTED]`
    /// - API keys: `key=xxx` or `api_key=xxx` -> `key=[REDACTED]`
    /// - Auth tokens: `token=xxx` or `auth_token=xxx` -> `token=[REDACTED]`
    /// - Passwords: `password=xxx` -> `password=[REDACTED]`
    /// - Connection strings: `://user:pass@` -> `://[REDACTED]:[REDACTED]@`
    /// - Long hex/base64 strings in auth contexts
    pub fn redact(input: &str) -> String {
        let mut result = input.to_string();

        // Redact Bearer tokens
        if let Some(pos) = result.find("Bearer ") {
            let start = pos + 7;
            if let Some(end) = result[start..].find(' ') {
                result.replace_range(start..start + end, "[REDACTED]");
            } else {
                result.truncate(start);
                result.push_str("[REDACTED]");
            }
        }

        // Redact key=value patterns for sensitive keys
        for pattern in [
            "api_key=",
            "apikey=",
            "API_KEY=",
            "token=",
            "auth_token=",
            "AUTH_TOKEN=",
            "password=",
            "PASSWORD=",
            "passwd=",
            "secret=",
            "SECRET=",
            "TURSO_AUTH=",
        ] {
            if let Some(pos) = result.find(pattern) {
                let start = pos + pattern.len();
                let end = result[start..]
                    .find(|c: char| c.is_whitespace() || c == '&' || c == '"' || c == '\'')
                    .map(|e| start + e)
                    .unwrap_or(result.len());
                if end > start {
                    result.replace_range(start..end, "[REDACTED]");
                }
            }
        }

        // Redact credentials in URLs: scheme://user:pass@host
        if let Some(at_pos) = result.find("://") {
            let after_scheme = at_pos + 3;
            if let Some(at_sign) = result[after_scheme..].find('@') {
                let cred_start = after_scheme;
                let cred_end = after_scheme + at_sign;
                // Check there's a colon (user:pass) between scheme:// and @
                if result[cred_start..cred_end].contains(':') {
                    result.replace_range(cred_start..cred_end, "[REDACTED]:[REDACTED]");
                }
            }
        }

        result
    }
}

/// Standard metric counters for the LeIndex application.
pub struct Metrics {
    /// Number of search queries executed
    pub search_queries: Counter,
    /// Number of files indexed
    pub files_indexed: Counter,
    /// Number of MCP tool calls
    pub mcp_tool_calls: Counter,
    /// Number of errors encountered
    pub errors: Counter,
    /// Search query latency in milliseconds
    pub search_latency: Histogram,
}

impl Default for Metrics {
    fn default() -> Self {
        Self {
            search_queries: Counter::new("leindex_search_queries_total", "Total search queries"),
            files_indexed: Counter::new("leindex_files_indexed_total", "Total files indexed"),
            mcp_tool_calls: Counter::new("leindex_mcp_tool_calls_total", "Total MCP tool calls"),
            errors: Counter::new("leindex_errors_total", "Total errors"),
            search_latency: Histogram::new(
                "leindex_search_latency_ms",
                vec![1.0, 5.0, 10.0, 25.0, 50.0, 100.0, 250.0, 500.0, 1000.0],
            ),
        }
    }
}

static METRICS: OnceLock<Metrics> = OnceLock::new();

/// Product analytics event tracker for understanding feature usage
/// without external dependencies. Events are collected via the
/// error-insight.yml workflow's product-metrics job.
pub struct ProductAnalytics {
    events: Mutex<Vec<AnalyticsEvent>>,
}

/// A single product analytics event.
pub struct AnalyticsEvent {
    /// Event name (e.g., "search_executed", "index_completed")
    pub name: String,
    /// Timestamp (Unix epoch nanos)
    pub timestamp_nanos: u128,
    /// Event properties
    pub properties: HashMap<String, String>,
}

static PRODUCT_ANALYTICS: OnceLock<ProductAnalytics> = OnceLock::new();

impl ProductAnalytics {
    fn new() -> Self {
        Self {
            events: Mutex::new(Vec::new()),
        }
    }

    /// Gets the global analytics instance.
    pub fn global() -> &'static Self {
        PRODUCT_ANALYTICS.get_or_init(ProductAnalytics::new)
    }

    /// Tracks a product analytics event.
    pub fn track(&self, name: &str, properties: HashMap<String, String>) {
        let event = AnalyticsEvent {
            name: name.to_string(),
            timestamp_nanos: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos(),
            properties,
        };
        if let Ok(mut events) = self.events.lock() {
            if events.len() < 5000 {
                events.push(event);
            }
        }
    }

    /// Returns the total number of tracked events.
    pub fn event_count(&self) -> usize {
        self.events.lock().map(|e| e.len()).unwrap_or(0)
    }
}

impl Metrics {
    /// Gets the global metrics instance.
    pub fn global() -> &'static Self {
        METRICS.get_or_init(Metrics::default)
    }

    /// Renders all metrics in Prometheus text exposition format.
    pub fn render_prometheus(&self) -> String {
        let mut out = String::new();
        out.push_str(&self.search_queries.render_prometheus());
        out.push('\n');
        out.push_str(&self.files_indexed.render_prometheus());
        out.push('\n');
        out.push_str(&self.mcp_tool_calls.render_prometheus());
        out.push('\n');
        out.push_str(&self.errors.render_prometheus());
        out.push('\n');
        out.push_str(&self.search_latency.render_prometheus());
        out
    }
}

/// Circuit breaker for protecting against cascading failures from external
/// dependencies (remote embedding APIs, Turso/libSQL, etc.).
///
/// Trips open after `failure_threshold` consecutive failures, blocking
/// further calls for `reset_timeout` seconds. After the timeout, one
/// trial call is allowed (half-open state). If it succeeds the breaker
/// closes; if it fails the breaker re-opens.
pub struct CircuitBreaker {
    failure_count: AtomicU64,
    failure_threshold: u64,
    last_failure_time: Mutex<Option<Instant>>,
    reset_timeout_secs: u64,
    state: Mutex<CircuitState>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum CircuitState {
    Closed,
    Open,
    HalfOpen,
}

impl CircuitBreaker {
    /// Creates a new breaker with the given failure threshold and reset timeout.
    pub fn new(failure_threshold: u32, reset_timeout_secs: u64) -> Self {
        Self {
            failure_count: AtomicU64::new(0),
            failure_threshold: failure_threshold as u64,
            last_failure_time: Mutex::new(None),
            reset_timeout_secs,
            state: Mutex::new(CircuitState::Closed),
        }
    }

    /// Returns true if a call is allowed (breaker is closed or half-open).
    pub fn allow_call(&self) -> bool {
        let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
        match *state {
            CircuitState::Closed => true,
            CircuitState::Open => {
                let last = self
                    .last_failure_time
                    .lock()
                    .unwrap_or_else(|e| e.into_inner());
                if let Some(t) = *last {
                    if t.elapsed().as_secs() >= self.reset_timeout_secs {
                        *state = CircuitState::HalfOpen;
                        return true;
                    }
                }
                false
            }
            CircuitState::HalfOpen => true,
        }
    }

    /// Records a successful call: resets failure count and closes the breaker.
    pub fn record_success(&self) {
        self.failure_count.store(0, Ordering::Relaxed);
        let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
        *state = CircuitState::Closed;
    }

    /// Records a failed call: increments failure count, opens if threshold hit.
    pub fn record_failure(&self) {
        let count = self.failure_count.fetch_add(1, Ordering::Relaxed) + 1;
        let mut last = self
            .last_failure_time
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        *last = Some(Instant::now());
        drop(last);
        if count >= self.failure_threshold {
            let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
            *state = CircuitState::Open;
        }
    }

    /// Returns the current state as a string for metrics/logging.
    pub fn state(&self) -> &'static str {
        let state = self.state.lock().unwrap_or_else(|e| e.into_inner());
        match *state {
            CircuitState::Closed => "closed",
            CircuitState::Open => "open",
            CircuitState::HalfOpen => "half_open",
        }
    }
}

/// Health check result for the MCP server and sub-systems.
#[derive(Debug)]
pub struct HealthStatus {
    /// Overall health: true if all sub-checks passed.
    pub healthy: bool,
    /// Individual check results: (name, passed, detail_message).
    pub checks: Vec<(String, bool, String)>,
}

impl HealthStatus {
    /// Runs health checks for core sub-systems. Currently verifies that
    /// the metrics instance and error tracker are responsive.
    /// Extensible: add database connectivity, ONNX model availability, etc.
    pub fn check() -> Self {
        let mut checks = Vec::new();

        let metrics_ok =
            std::panic::catch_unwind(|| Metrics::global().search_queries.get()).is_ok();
        checks.push((
            "metrics".to_string(),
            metrics_ok,
            if metrics_ok {
                "ok".to_string()
            } else {
                "panic".to_string()
            },
        ));

        let tracker_ok = std::panic::catch_unwind(ErrorTracker::global).is_ok();
        checks.push((
            "error_tracker".to_string(),
            tracker_ok,
            if tracker_ok {
                "ok".to_string()
            } else {
                "panic".to_string()
            },
        ));

        let healthy = checks.iter().all(|(_, ok, _)| *ok);
        Self { healthy, checks }
    }

    /// Renders as JSON for an HTTP health endpoint.
    pub fn to_json(&self) -> String {
        let checks_json: Vec<String> = self
            .checks
            .iter()
            .map(|(name, ok, msg)| {
                format!(r#"{{"name":"{}","ok":{},"detail":"{}"}}"#, name, ok, msg)
            })
            .collect();
        format!(
            r#"{{"healthy":{},"checks":[{}]}}"#,
            self.healthy,
            checks_json.join(",")
        )
    }
}

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

    #[test]
    fn test_trace_span_propagation() {
        let root = TraceSpan::root("index_request");
        let child = TraceSpan::child("parse_file", &root);
        assert_eq!(root.trace_id, child.trace_id);
        assert_eq!(child.parent_span_id, root.span_id);
        assert!(!root.trace_id.is_empty());
        assert!(!root.span_id.is_empty());
    }

    #[test]
    fn test_counter() {
        let c = Counter::new("test_counter", "test");
        c.inc();
        c.inc();
        c.add(10);
        assert_eq!(c.get(), 12);
        assert!(c.render_prometheus().contains("test_counter 12"));
    }

    #[test]
    fn test_histogram() {
        let h = Histogram::new("test_h", vec![1.0, 5.0, 10.0]);
        h.observe(0.5);
        h.observe(3.0);
        h.observe(15.0);
        let prom = h.render_prometheus();
        assert!(prom.contains("test_h_count 3"));
        assert!(prom.contains("test_h_sum 18.5"));
    }

    #[test]
    fn test_log_scrubber_bearer() {
        let input = "Authorization: Bearer abc123secret";
        let result = LogScrubber::redact(input);
        assert!(result.contains("[REDACTED]"));
        assert!(!result.contains("abc123secret"));
    }

    #[test]
    fn test_log_scrubber_api_key() {
        let input = "api_key=mysecretkey123";
        let result = LogScrubber::redact(input);
        assert!(result.contains("[REDACTED]"));
        assert!(!result.contains("mysecretkey123"));
    }

    #[test]
    fn test_log_scrubber_url_creds() {
        let input = "postgres://admin:pass123@db.local:5432";
        let result = LogScrubber::redact(input);
        assert!(result.contains("[REDACTED]"));
        assert!(!result.contains("pass123"));
        assert!(!result.contains("admin"));
    }

    #[test]
    fn test_log_scrubber_preserves_non_sensitive() {
        let input = "Indexed 42 files in /home/user/project";
        let result = LogScrubber::redact(input);
        assert_eq!(input, result);
    }

    #[test]
    fn test_log_scrubber_turso_auth() {
        let input = "TURSO_AUTH=eyJhbGciOi";
        let result = LogScrubber::redact(input);
        assert!(result.contains("[REDACTED]"));
        assert!(!result.contains("eyJhbGciOi"));
    }

    #[test]
    fn test_circuit_breaker_trips() {
        let cb = CircuitBreaker::new(3, 60);
        assert!(cb.allow_call());
        assert_eq!(cb.state(), "closed");

        cb.record_failure();
        cb.record_failure();
        assert!(cb.allow_call());

        cb.record_failure();
        assert!(!cb.allow_call());
        assert_eq!(cb.state(), "open");
    }

    #[test]
    fn test_circuit_breaker_half_open() {
        let cb = CircuitBreaker::new(1, 0);
        cb.record_failure();
        assert_eq!(cb.state(), "open");
        std::thread::sleep(std::time::Duration::from_millis(1100));
        assert!(cb.allow_call());
        assert_eq!(cb.state(), "half_open");
        cb.record_success();
        assert_eq!(cb.state(), "closed");
    }

    #[test]
    fn test_circuit_breaker_reset_on_success() {
        let cb = CircuitBreaker::new(3, 60);
        cb.record_failure();
        cb.record_failure();
        cb.record_success();
        assert_eq!(cb.state(), "closed");
    }

    #[test]
    fn test_health_status_json() {
        let h = HealthStatus::check();
        let json = h.to_json();
        assert!(json.contains("healthy"));
        assert!(json.contains("checks"));
    }
}