clawdstrike 0.2.5

Security guards and policy engine for AI agent execution
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
//! Spider-Sense detection module (WASM-compatible).
//!
//! Pure, sync screening of embedding vectors against a pre-computed pattern
//! database using cosine similarity. This module is always compiled (no
//! feature gate) and safe for WASM targets.
//!
//! The full async guard (`SpiderSenseGuard`) in `async_guards::threat_intel`
//! delegates its fast-path screening to [`SpiderSenseDetector::screen`].

use serde::{Deserialize, Serialize};

// ── Configuration ───────────────────────────────────────────────────────

const DEFAULT_SIMILARITY_THRESHOLD: f64 = 0.85;
const DEFAULT_AMBIGUITY_BAND: f64 = 0.10;
const DEFAULT_TOP_K: usize = 5;

/// Configuration for the standalone Spider-Sense detector.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SpiderSenseDetectorConfig {
    /// Cosine similarity threshold above which a match is a threat.
    /// Default: 0.85
    #[serde(default = "default_similarity_threshold")]
    pub similarity_threshold: f64,
    /// Half-width of the ambiguity band around the threshold.
    /// Default: 0.10
    #[serde(default = "default_ambiguity_band")]
    pub ambiguity_band: f64,
    /// Number of top matches to return.
    /// Default: 5
    #[serde(default = "default_top_k")]
    pub top_k: usize,
}

impl Default for SpiderSenseDetectorConfig {
    fn default() -> Self {
        Self {
            similarity_threshold: DEFAULT_SIMILARITY_THRESHOLD,
            ambiguity_band: DEFAULT_AMBIGUITY_BAND,
            top_k: DEFAULT_TOP_K,
        }
    }
}

fn default_similarity_threshold() -> f64 {
    DEFAULT_SIMILARITY_THRESHOLD
}

fn default_ambiguity_band() -> f64 {
    DEFAULT_AMBIGUITY_BAND
}

fn default_top_k() -> usize {
    DEFAULT_TOP_K
}

// ── Pattern Database ────────────────────────────────────────────────────

/// A single entry in the pattern database.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PatternEntry {
    /// Unique identifier for this pattern.
    pub id: String,
    /// Attack category (e.g. `"prompt_injection"`, `"data_exfiltration"`).
    pub category: String,
    /// Spider-Sense stage: perception, cognition, action, feedback.
    pub stage: String,
    /// Human-readable label.
    pub label: String,
    /// Pre-computed embedding vector.
    pub embedding: Vec<f32>,
}

/// A scored match from the pattern database.
#[derive(Clone, Debug, Serialize)]
pub struct PatternMatch {
    /// The matched entry.
    pub entry: PatternEntry,
    /// Cosine similarity score.
    pub score: f64,
}

/// In-memory pattern database for vector similarity search.
#[derive(Clone, Debug)]
pub struct PatternDb {
    entries: Vec<PatternEntry>,
    expected_dim: Option<usize>,
}

impl PatternDb {
    /// Load from a JSON file. Returns an error if parsing fails or dimensions
    /// are inconsistent.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn load_from_json(path: &str) -> Result<Self, String> {
        let data = std::fs::read_to_string(path)
            .map_err(|e| format!("failed to read pattern DB at {path}: {e}"))?;
        Self::parse_json(&data)
    }

    /// Parse a JSON string containing a pattern array.
    pub fn parse_json(json: &str) -> Result<Self, String> {
        let entries: Vec<PatternEntry> =
            serde_json::from_str(json).map_err(|e| format!("failed to parse pattern DB: {e}"))?;

        if entries.is_empty() {
            return Err("pattern DB must contain at least one entry".to_string());
        }

        let dim = entries[0].embedding.len();
        if dim == 0 {
            return Err("pattern DB entries must have non-empty embeddings".to_string());
        }

        for (i, entry) in entries.iter().enumerate() {
            if entry.embedding.len() != dim {
                return Err(format!(
                    "pattern DB dimension mismatch at index {i}: expected {dim}, got {}",
                    entry.embedding.len()
                ));
            }
            if let Some(j) = entry.embedding.iter().position(|v| !v.is_finite()) {
                return Err(format!(
                    "pattern DB entry {i} has non-finite embedding value at dimension {j}"
                ));
            }
        }

        Ok(Self {
            entries,
            expected_dim: Some(dim),
        })
    }

    /// Brute-force cosine similarity search. Returns the top-k matches sorted
    /// by descending similarity score.
    pub fn search(&self, query: &[f32], top_k: usize) -> Vec<PatternMatch> {
        let mut scored: Vec<PatternMatch> = self
            .entries
            .iter()
            .map(|entry| {
                let score = cosine_similarity_f32(query, &entry.embedding);
                PatternMatch {
                    entry: entry.clone(),
                    score,
                }
            })
            .collect();

        scored.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        scored.truncate(top_k);
        scored
    }

    /// Number of entries in the database.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Whether the database is empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Expected embedding dimension, if known.
    pub fn expected_dim(&self) -> Option<usize> {
        self.expected_dim
    }
}

// ── Cosine Similarity ───────────────────────────────────────────────────

/// Compute cosine similarity between two f32 vectors, using f64 precision
/// for the accumulation. Returns 0.0 if either vector has zero norm or if
/// any element is non-finite (NaN, Inf). This ensures fail-closed behavior:
/// corrupted embeddings produce a zero score rather than propagating NaN.
pub fn cosine_similarity_f32(a: &[f32], b: &[f32]) -> f64 {
    if a.len() != b.len() {
        return 0.0;
    }

    let mut dot: f64 = 0.0;
    let mut norm_a: f64 = 0.0;
    let mut norm_b: f64 = 0.0;

    for (x, y) in a.iter().zip(b.iter()) {
        let xd = f64::from(*x);
        let yd = f64::from(*y);
        dot += xd * yd;
        norm_a += xd * xd;
        norm_b += yd * yd;
    }

    let denom = norm_a.sqrt() * norm_b.sqrt();
    if !denom.is_normal() {
        return 0.0;
    }

    let result = dot / denom;
    if result.is_finite() {
        result
    } else {
        0.0
    }
}

// ── Screening ───────────────────────────────────────────────────────────

/// Verdict from the fast-path screening.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ScreeningVerdict {
    /// Clear threat — above upper bound.
    Deny,
    /// Ambiguous — within the band around the threshold.
    Ambiguous,
    /// Clear benign — below lower bound.
    Allow,
}

/// Result of a [`SpiderSenseDetector::screen`] call.
#[derive(Clone, Debug, Serialize)]
pub struct ScreeningResult {
    /// The screening verdict.
    pub verdict: ScreeningVerdict,
    /// Top similarity score (0.0 if no patterns).
    pub top_score: f64,
    /// Configured similarity threshold.
    pub threshold: f64,
    /// Configured ambiguity band.
    pub ambiguity_band: f64,
    /// Top-k pattern matches.
    pub top_matches: Vec<PatternMatch>,
}

/// Standalone Spider-Sense detector for embedding-based screening.
///
/// Wraps a [`PatternDb`] and screening thresholds. Operates synchronously
/// with no I/O — the caller is responsible for obtaining embeddings.
pub struct SpiderSenseDetector {
    pattern_db: PatternDb,
    upper_bound: f64,
    lower_bound: f64,
    top_k: usize,
    threshold: f64,
    ambiguity_band: f64,
}

impl SpiderSenseDetector {
    /// Create a new detector. Returns an error if the config is invalid.
    pub fn new(pattern_db: PatternDb, config: &SpiderSenseDetectorConfig) -> Result<Self, String> {
        let (upper_bound, lower_bound) = validate_detector_config(config)?;
        Ok(Self {
            pattern_db,
            upper_bound,
            lower_bound,
            top_k: config.top_k,
            threshold: config.similarity_threshold,
            ambiguity_band: config.ambiguity_band,
        })
    }

    /// Screen an embedding vector against the pattern database.
    ///
    /// This is a pure, sync operation with no I/O.
    pub fn screen(&self, embedding: &[f32]) -> ScreeningResult {
        if let Some(expected_dim) = self.pattern_db.expected_dim() {
            if embedding.len() != expected_dim {
                return ScreeningResult {
                    verdict: ScreeningVerdict::Deny,
                    top_score: 0.0,
                    threshold: self.threshold,
                    ambiguity_band: self.ambiguity_band,
                    top_matches: vec![],
                };
            }
        }
        if embedding.iter().any(|v| !v.is_finite()) {
            return ScreeningResult {
                verdict: ScreeningVerdict::Deny,
                top_score: 0.0,
                threshold: self.threshold,
                ambiguity_band: self.ambiguity_band,
                top_matches: vec![],
            };
        }

        let matches = self.pattern_db.search(embedding, self.top_k);
        let top_score = matches.first().map(|m| m.score).unwrap_or(0.0);

        let verdict = if top_score >= self.upper_bound {
            ScreeningVerdict::Deny
        } else if top_score <= self.lower_bound {
            ScreeningVerdict::Allow
        } else {
            ScreeningVerdict::Ambiguous
        };

        ScreeningResult {
            verdict,
            top_score,
            threshold: self.threshold,
            ambiguity_band: self.ambiguity_band,
            top_matches: matches,
        }
    }

    /// Expected embedding dimension from the pattern DB.
    pub fn expected_dim(&self) -> Option<usize> {
        self.pattern_db.expected_dim()
    }

    /// Number of patterns in the database.
    pub fn pattern_count(&self) -> usize {
        self.pattern_db.len()
    }
}

fn validate_detector_config(config: &SpiderSenseDetectorConfig) -> Result<(f64, f64), String> {
    if !config.similarity_threshold.is_finite() {
        return Err("similarity_threshold must be a finite number".to_string());
    }
    if !(0.0..=1.0).contains(&config.similarity_threshold) {
        return Err(format!(
            "similarity_threshold must be in [0.0, 1.0], got {}",
            config.similarity_threshold
        ));
    }

    if !config.ambiguity_band.is_finite() {
        return Err("ambiguity_band must be a finite number".to_string());
    }
    if !(0.0..=1.0).contains(&config.ambiguity_band) {
        return Err(format!(
            "ambiguity_band must be in [0.0, 1.0], got {}",
            config.ambiguity_band
        ));
    }

    let upper_bound = config.similarity_threshold + config.ambiguity_band;
    let lower_bound = config.similarity_threshold - config.ambiguity_band;
    if !(0.0..=1.0).contains(&lower_bound) || !(0.0..=1.0).contains(&upper_bound) {
        return Err(format!(
            "threshold/band produce invalid decision range: lower={lower_bound:.3}, upper={upper_bound:.3}; expected both in [0.0, 1.0]"
        ));
    }

    if config.top_k == 0 {
        return Err("top_k must be at least 1".to_string());
    }

    Ok((upper_bound, lower_bound))
}

// ── Unit Tests ──────────────────────────────────────────────────────────

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

    fn test_pattern_db() -> PatternDb {
        PatternDb::parse_json(
            r#"[
            { "id": "p1", "category": "prompt_injection", "stage": "perception", "label": "ignore previous", "embedding": [1.0, 0.0, 0.0] },
            { "id": "p2", "category": "data_exfiltration", "stage": "action", "label": "exfil data", "embedding": [0.0, 1.0, 0.0] },
            { "id": "p3", "category": "privilege_escalation", "stage": "cognition", "label": "escalate", "embedding": [0.0, 0.0, 1.0] }
        ]"#,
        )
        .expect("test pattern DB should parse")
    }

    #[test]
    fn cosine_identical_vectors() {
        let a = vec![1.0, 0.0, 0.0];
        let sim = cosine_similarity_f32(&a, &a);
        assert!((sim - 1.0).abs() < 1e-10);
    }

    #[test]
    fn cosine_orthogonal_vectors() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![0.0, 1.0, 0.0];
        assert!(cosine_similarity_f32(&a, &b).abs() < 1e-10);
    }

    #[test]
    fn cosine_opposite_vectors() {
        let a = vec![1.0, 0.0];
        let b = vec![-1.0, 0.0];
        assert!((cosine_similarity_f32(&a, &b) - (-1.0)).abs() < 1e-10);
    }

    #[test]
    fn cosine_zero_vector() {
        let a = vec![0.0, 0.0, 0.0];
        let b = vec![1.0, 2.0, 3.0];
        assert_eq!(cosine_similarity_f32(&a, &b), 0.0);
    }

    #[test]
    fn cosine_different_lengths() {
        let a = vec![1.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        assert_eq!(cosine_similarity_f32(&a, &b), 0.0);
    }

    #[test]
    fn pattern_db_parse_valid() {
        let db = test_pattern_db();
        assert_eq!(db.len(), 3);
        assert_eq!(db.expected_dim(), Some(3));
    }

    #[test]
    fn pattern_db_parse_allows_extra_metadata_fields() {
        let json = r#"[
            {
                "id": "p1",
                "category": "prompt_injection",
                "stage": "perception",
                "label": "ignore previous instructions",
                "embedding": [0.1, 0.2, 0.3],
                "description": "extra metadata should be ignored",
                "severity": "critical",
                "source": "custom-db",
                "created_at": "2026-03-04T00:00:00Z"
            }
        ]"#;
        let db = PatternDb::parse_json(json).expect("pattern DB with extra metadata should parse");
        assert_eq!(db.len(), 1);
        assert_eq!(db.expected_dim(), Some(3));
    }

    #[test]
    fn pattern_db_parse_empty() {
        let err = PatternDb::parse_json("[]").expect_err("empty pattern DB must fail closed");
        assert!(err.contains("must contain at least one entry"));
    }

    #[test]
    fn pattern_db_parse_dimension_mismatch() {
        let json = r#"[
            { "id": "p1", "category": "a", "stage": "b", "label": "c", "embedding": [0.1, 0.2] },
            { "id": "p2", "category": "a", "stage": "b", "label": "d", "embedding": [0.1] }
        ]"#;
        let result = PatternDb::parse_json(json);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("dimension mismatch"));
    }

    #[test]
    fn pattern_db_search_returns_top_k() {
        let db = test_pattern_db();
        let query = vec![1.0, 0.0, 0.0];
        let results = db.search(&query, 2);
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].entry.id, "p1");
        assert!((results[0].score - 1.0).abs() < 1e-6);
    }

    #[test]
    fn detector_screen_deny() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig {
            similarity_threshold: 0.85,
            ambiguity_band: 0.10,
            top_k: 5,
        };
        let detector = SpiderSenseDetector::new(db, &config).unwrap();
        // Identical vector → score 1.0, above upper bound 0.95
        let result = detector.screen(&[1.0, 0.0, 0.0]);
        assert_eq!(result.verdict, ScreeningVerdict::Deny);
        assert!((result.top_score - 1.0).abs() < 1e-6);
    }

    #[test]
    fn detector_screen_allow() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig {
            similarity_threshold: 0.85,
            ambiguity_band: 0.10,
            top_k: 5,
        };
        let detector = SpiderSenseDetector::new(db, &config).unwrap();
        // Orthogonal to all patterns → score ~0.0, below lower bound 0.75
        let result = detector.screen(&[0.577, 0.577, 0.577]);
        assert_eq!(result.verdict, ScreeningVerdict::Allow);
    }

    #[test]
    fn detector_screen_ambiguous() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig {
            similarity_threshold: 0.50,
            ambiguity_band: 0.10,
            top_k: 5,
        };
        let detector = SpiderSenseDetector::new(db, &config).unwrap();
        // Partially similar → score ~0.577, within band [0.40, 0.60]
        let result = detector.screen(&[0.577, 0.577, 0.577]);
        assert_eq!(result.verdict, ScreeningVerdict::Ambiguous);
    }

    #[test]
    fn detector_screen_dimension_mismatch_denies() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig::default();
        let detector = SpiderSenseDetector::new(db, &config).unwrap();
        let result = detector.screen(&[1.0, 0.0]);
        assert_eq!(result.verdict, ScreeningVerdict::Deny);
        assert_eq!(result.top_score, 0.0);
        assert!(result.top_matches.is_empty());
    }

    #[test]
    fn detector_screen_non_finite_embedding_denies() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig::default();
        let detector = SpiderSenseDetector::new(db, &config).unwrap();
        let result = detector.screen(&[f32::NAN, 0.0, 0.0]);
        assert_eq!(result.verdict, ScreeningVerdict::Deny);
        assert_eq!(result.top_score, 0.0);
        assert!(result.top_matches.is_empty());
    }

    #[test]
    fn detector_screen_exact_lower_bound_is_allow() {
        let db = PatternDb::parse_json(
            r#"[
                { "id": "p1", "category": "a", "stage": "s", "label": "x", "embedding": [1.0, 0.0, 0.0] }
            ]"#,
        )
        .unwrap();
        let config = SpiderSenseDetectorConfig {
            similarity_threshold: 0.10,
            ambiguity_band: 0.10,
            top_k: 5,
        };
        let detector = SpiderSenseDetector::new(db, &config).unwrap();
        let lower = config.similarity_threshold - config.ambiguity_band;
        let query = [0.0, 1.0, 0.0];
        let result = detector.screen(&query);
        assert_eq!(result.verdict, ScreeningVerdict::Allow);
        assert!((result.top_score - lower).abs() < 1e-6);
    }

    #[test]
    fn detector_config_rejects_invalid_threshold() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig {
            similarity_threshold: 1.5,
            ambiguity_band: 0.10,
            top_k: 5,
        };
        assert!(SpiderSenseDetector::new(db, &config).is_err());
    }

    #[test]
    fn detector_config_rejects_zero_top_k() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig {
            similarity_threshold: 0.85,
            ambiguity_band: 0.10,
            top_k: 0,
        };
        assert!(SpiderSenseDetector::new(db, &config).is_err());
    }

    #[test]
    fn detector_config_rejects_out_of_range_bounds() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig {
            similarity_threshold: 0.95,
            ambiguity_band: 0.10,
            top_k: 5,
        };
        assert!(SpiderSenseDetector::new(db, &config).is_err());
    }

    #[test]
    fn detector_expected_dim() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig::default();
        let detector = SpiderSenseDetector::new(db, &config).unwrap();
        assert_eq!(detector.expected_dim(), Some(3));
        assert_eq!(detector.pattern_count(), 3);
    }

    #[test]
    fn screening_result_serializes() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig::default();
        let detector = SpiderSenseDetector::new(db, &config).unwrap();
        let result = detector.screen(&[1.0, 0.0, 0.0]);
        let json = serde_json::to_string(&result).expect("should serialize");
        assert!(json.contains("\"verdict\""));
        assert!(json.contains("\"top_score\""));
    }

    #[test]
    fn cosine_nan_returns_zero() {
        let a = vec![f32::NAN, 1.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        assert_eq!(
            cosine_similarity_f32(&a, &b),
            0.0,
            "NaN input must return 0 (fail-closed)"
        );
    }

    #[test]
    fn cosine_infinity_returns_zero() {
        let a = vec![f32::INFINITY, 0.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        assert_eq!(
            cosine_similarity_f32(&a, &b),
            0.0,
            "Inf input must return 0 (fail-closed)"
        );
    }

    #[test]
    fn cosine_neg_infinity_returns_zero() {
        let a = vec![f32::NEG_INFINITY, 0.0];
        let b = vec![1.0, 0.0];
        assert_eq!(
            cosine_similarity_f32(&a, &b),
            0.0,
            "negative Inf must return 0 (fail-closed)"
        );
    }

    #[test]
    fn cosine_empty_vectors() {
        let a: Vec<f32> = vec![];
        let b: Vec<f32> = vec![];
        assert_eq!(
            cosine_similarity_f32(&a, &b),
            0.0,
            "empty vectors must return 0"
        );
    }

    #[test]
    fn pattern_db_rejects_nan_embedding() {
        let json = r#"[
            { "id": "p1", "category": "a", "stage": "b", "label": "c", "embedding": [1.0, NaN, 0.0] }
        ]"#;
        // NaN is not valid JSON, so serde_json will reject it at parse time.
        assert!(PatternDb::parse_json(json).is_err());
    }

    #[test]
    fn pattern_db_rejects_infinity_embedding() {
        // Infinity is not valid JSON, so serde_json rejects it.
        let json = r#"[
            { "id": "p1", "category": "a", "stage": "b", "label": "c", "embedding": [1.0, Infinity, 0.0] }
        ]"#;
        assert!(PatternDb::parse_json(json).is_err());
    }

    #[test]
    fn pattern_db_search_with_mismatched_query_dim() {
        let db = test_pattern_db();
        // Query has 2 dims, DB has 3 dims — cosine_similarity returns 0.0 for each
        let results = db.search(&[1.0, 0.0], 3);
        assert_eq!(results.len(), 3);
        for m in &results {
            assert_eq!(m.score, 0.0, "dimension mismatch should produce zero score");
        }
    }

    #[test]
    fn pattern_db_search_top_k_larger_than_db() {
        let db = test_pattern_db();
        let results = db.search(&[1.0, 0.0, 0.0], 100);
        assert_eq!(
            results.len(),
            3,
            "top_k > entries should return all entries"
        );
    }

    #[test]
    fn detector_config_rejects_negative_threshold() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig {
            similarity_threshold: -0.1,
            ambiguity_band: 0.05,
            top_k: 5,
        };
        assert!(SpiderSenseDetector::new(db, &config).is_err());
    }

    #[test]
    fn detector_config_rejects_nan_ambiguity_band() {
        let db = test_pattern_db();
        let config = SpiderSenseDetectorConfig {
            similarity_threshold: 0.5,
            ambiguity_band: f64::NAN,
            top_k: 5,
        };
        assert!(SpiderSenseDetector::new(db, &config).is_err());
    }

    #[test]
    fn default_config_roundtrips_through_serde() {
        let config = SpiderSenseDetectorConfig::default();
        let json = serde_json::to_string(&config).expect("should serialize");
        let parsed: SpiderSenseDetectorConfig =
            serde_json::from_str(&json).expect("should deserialize");
        assert!((parsed.similarity_threshold - config.similarity_threshold).abs() < f64::EPSILON);
        assert!((parsed.ambiguity_band - config.ambiguity_band).abs() < f64::EPSILON);
        assert_eq!(parsed.top_k, config.top_k);
    }
}