tapirus 1.2.1

Embedded multi-model database with SQL, vector search and local AI memory
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
//! # Tap: In-database heuristic decision subsystem
//!
//! Tap is TapirusDB's native System-1 cognitive engine. It provides deterministic,
//! non-autoregressive classification, rubric scoring, heuristic hypothesis
//! comparison and graph workflow routing directly inside the database kernel.
//!
//! Unlike an autoregressive text-generation service, Tap executes local tensor
//! projections over database records. Scores are workload-dependent heuristics,
//! not calibrated probabilities, factual truth or safety guarantees.

pub mod bitnet;
pub mod deep;
pub mod primitives;
pub mod runtime;
pub mod sql_bridge;
pub mod tokenizer;

pub use bitnet::{BitNetBlock, BitNetLinear, BitNetRmsNorm, TernaryVal};
pub use deep::TapDeepEngine;
pub use primitives::{
    ClassificationResult, GroundedClassificationResult, GroundedVerifyResult, RouteResult,
    ScoreResult, VerifyResult,
};
pub use runtime::{TAP_MODEL_MAGIC, TapInferenceEngine, TapRuntime, TapWeights};
pub use sql_bridge::{
    eval_tap_classify, eval_tap_classify_grounded, eval_tap_route, eval_tap_score, eval_tap_verify,
    eval_tap_verify_grounded, register_grounding_index,
};
pub use tokenizer::TapTokenizer;

use crate::error::{Error, Result};
use std::collections::HashSet;
use std::path::Path;
use std::sync::Arc;
use web_time::Instant;

/// Configuration options for the Tap Decision Engine
#[derive(Debug, Clone)]
pub struct TapConfig {
    /// Latent vector dimension (default: 64)
    pub dim: usize,
    /// Default threshold for boolean verification (default: 0.50)
    pub default_verify_threshold: f32,
    /// Temperature scaling for decision softmax (default: 0.85)
    pub temperature: f32,
    /// Weight assigned to lexical semantic overlap (default: 0.40)
    pub lexical_weight: f32,
    /// Weight assigned to dense tensor semantic similarity (default: 0.60)
    pub dense_weight: f32,
}

impl Default for TapConfig {
    fn default() -> Self {
        Self {
            dim: 64,
            default_verify_threshold: 0.50,
            temperature: 0.70,
            lexical_weight: 1.20,
            dense_weight: 0.40,
        }
    }
}

/// The Tap Decision Engine instance
#[derive(Clone)]
pub struct TapEngine {
    config: TapConfig,
    tokenizer: Arc<TapTokenizer>,
    runtime: Arc<TapRuntime>,
}

impl Default for TapEngine {
    fn default() -> Self {
        Self::new(TapConfig::default())
    }
}

impl TapEngine {
    /// Initialize a new Tap engine with configuration and calibrated baseline weights
    pub fn new(config: TapConfig) -> Self {
        let tokenizer = TapTokenizer::new();
        let vocab_size = tokenizer.vocab_size();
        let mut weights = TapWeights::default_calibrated(config.dim, vocab_size);
        weights.temperature = config.temperature;

        Self {
            config,
            tokenizer: Arc::new(tokenizer),
            runtime: Arc::new(TapRuntime::new(weights)),
        }
    }

    /// Load pre-trained Tap weights from a `.tapmodel` file
    pub fn load_from_file<P: AsRef<Path>>(path: P, config: TapConfig) -> Result<Self> {
        let tokenizer = TapTokenizer::new();
        let weights = TapWeights::load_from_file(path)?;

        Ok(Self {
            config,
            tokenizer: Arc::new(tokenizer),
            runtime: Arc::new(TapRuntime::new(weights)),
        })
    }

    /// Access the internal tokenizer
    pub fn tokenizer(&self) -> &TapTokenizer {
        &self.tokenizer
    }

    /// Access the internal runtime
    pub fn runtime(&self) -> &TapRuntime {
        &self.runtime
    }

    /// Categorical classification: selects the winning choice among candidates with softmax confidence
    pub fn classify(&self, input: &str, candidates: &[&str]) -> Result<ClassificationResult> {
        let start = Instant::now();
        if candidates.is_empty() {
            return Err(Error::Internal(
                "Tap::classify requires at least one candidate choice".into(),
            ));
        }

        let input_tokens = self.tokenizer.tokenize(input);
        let input_vec = self.runtime.forward_pool(&input_tokens);

        let input_words = extract_word_set(input);
        let mut raw_logits = Vec::with_capacity(candidates.len());

        for &cand in candidates {
            let cand_tokens = self.tokenizer.tokenize(cand);
            let cand_vec = self.runtime.forward_pool(&cand_tokens);
            let dense_sim = self.runtime.cosine_similarity(&input_vec, &cand_vec);

            let cand_words = extract_word_set(cand);
            let lexical_overlap = compute_jaccard_overlap(&input_words, &cand_words);

            let mut domain_bonus = 0.0f32;
            for assoc in get_domain_associations(cand) {
                if input_words.contains(*assoc) {
                    domain_bonus += 0.8;
                }
            }

            let logit =
                (dense_sim * 0.20) + (lexical_overlap * self.config.lexical_weight) + domain_bonus;
            raw_logits.push(logit);
        }

        let probs = self.runtime.softmax(&raw_logits);

        let mut ranked: Vec<(String, f32)> = candidates
            .iter()
            .zip(probs.iter())
            .map(|(&c, &p)| (c.to_string(), p))
            .collect();

        ranked.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        let top_choice = ranked
            .first()
            .cloned()
            .unwrap_or_else(|| ("unknown".into(), 0.0));

        let mut entropy = 0.0f32;
        for &p in &probs {
            if p > 1e-9 {
                entropy -= p * p.ln();
            }
        }

        let latency_us = start.elapsed().as_micros() as u64;

        Ok(ClassificationResult {
            top_choice: top_choice.0,
            confidence: top_choice.1,
            probabilities: ranked,
            entropy,
            latency_us,
        })
    }

    /// Rubric evaluation: scores alignment of input against criteria on a continuous $[0.0, 1.0]$ scale
    pub fn score(&self, input: &str, criteria: &str) -> Result<ScoreResult> {
        let start = Instant::now();
        let input_tokens = self.tokenizer.tokenize(input);
        let criteria_tokens = self.tokenizer.tokenize(criteria);

        let input_vec = self.runtime.forward_pool(&input_tokens);
        let criteria_vec = self.runtime.forward_pool(&criteria_tokens);

        let dense_sim = self.runtime.cosine_similarity(&input_vec, &criteria_vec);

        let input_words = extract_word_set(input);
        let criteria_words = extract_word_set(criteria);

        let mut matched_signals = Vec::new();
        for word in &criteria_words {
            if input_words.contains(word) {
                matched_signals.push(word.clone());
            }
        }

        let lexical_ratio = if criteria_words.is_empty() {
            0.0
        } else {
            matched_signals.len() as f32 / criteria_words.len() as f32
        };

        let raw_alignment =
            (dense_sim * self.config.dense_weight) + (lexical_ratio * self.config.lexical_weight);
        let score = self.runtime.sigmoid(raw_alignment * 3.5);
        let normalized_percentile = (score * 100.0).clamp(0.0, 100.0);
        let latency_us = start.elapsed().as_micros() as u64;

        Ok(ScoreResult {
            score,
            normalized_percentile,
            matched_signals,
            latency_us,
        })
    }

    /// Truth verification: determines whether a premise strictly verifies a hypothesis
    pub fn verify(&self, premise: &str, hypothesis: &str) -> Result<VerifyResult> {
        self.verify_with_threshold(premise, hypothesis, self.config.default_verify_threshold)
    }

    /// Truth verification with custom threshold
    pub fn verify_with_threshold(
        &self,
        premise: &str,
        hypothesis: &str,
        threshold: f32,
    ) -> Result<VerifyResult> {
        let start = Instant::now();
        let prem_tokens = self.tokenizer.tokenize(premise);
        let hyp_tokens = self.tokenizer.tokenize(hypothesis);

        let prem_vec = self.runtime.forward_pool(&prem_tokens);
        let hyp_vec = self.runtime.forward_pool(&hyp_tokens);

        let dense_sim = self.runtime.cosine_similarity(&prem_vec, &hyp_vec);

        let prem_words = extract_word_set(premise);
        let hyp_words = extract_word_set(hypothesis);

        let overlap = compute_jaccard_overlap(&prem_words, &hyp_words);

        // Pure grammatical polarity inverters across international languages
        let negations = [
            "not", "never", "untrue", "neither", "nor", "wont", "dont", "isnt", "arent", "didnt",
            "tidak", "bukan", "tak", "takde", "jangan", "nunca", "jamas", "jamais", "nicht",
            "kein", "keine",
        ];
        let mut premise_neg = false;
        let mut hyp_neg = false;
        for &neg in &negations {
            if prem_words.contains(neg) {
                premise_neg = true;
            }
            if hyp_words.contains(neg) {
                hyp_neg = true;
            }
        }

        let negation_penalty = if premise_neg != hyp_neg { -1.00 } else { 0.0 };

        let overlap_evidence = if premise_neg != hyp_neg {
            -0.60
        } else if overlap > 0.18 {
            (overlap - 0.15) * self.config.lexical_weight
        } else {
            -0.40
        };

        let seq_overlap = compute_sequence_bigram_overlap(premise, hypothesis);
        let seq_penalty = if overlap > 0.80
            && seq_overlap < 0.40
            && prem_words.len() >= 4
            && hyp_words.len() >= 4
        {
            -2.00
        } else {
            0.0
        };

        let raw_logit = ((dense_sim - 0.30) * self.config.dense_weight)
            + overlap_evidence
            + negation_penalty
            + seq_penalty;

        let confidence = self.runtime.sigmoid(raw_logit * 3.2);
        let is_verified = confidence >= threshold;
        let margin = confidence - threshold;
        let latency_us = start.elapsed().as_micros() as u64;

        Ok(VerifyResult {
            is_verified,
            confidence,
            threshold,
            margin,
            latency_us,
        })
    }

    /// HNSW-Grounded truth verification: cross-references an empirical premise and hypothesis
    /// against an in-database HNSW vector index containing reference document chunks or policy embeddings.
    pub fn verify_grounded(
        &self,
        premise: &str,
        hypothesis: &str,
        index: &crate::vector::HnswIndex,
        top_k: usize,
    ) -> Result<GroundedVerifyResult> {
        let start = Instant::now();
        let base_verify = self.verify(premise, hypothesis)?;

        let prem_tokens = self.tokenizer.tokenize(premise);
        let mut query_vec = self.runtime.forward_pool(&prem_tokens);

        if query_vec.len() != index.dimensions {
            if query_vec.len() > index.dimensions {
                query_vec.truncate(index.dimensions);
            } else {
                query_vec.resize(index.dimensions, 0.0);
            }
        }

        let k = top_k.max(1);
        let knn_matches = index.search_knn(&query_vec, k, crate::vector::DistanceMetric::Cosine)?;

        let mut retrieved_evidence = Vec::with_capacity(knn_matches.len());
        let mut total_sim = 0.0f32;
        for (id, dist) in knn_matches {
            let sim = (1.0 - dist.clamp(0.0, 2.0) / 2.0).clamp(0.0, 1.0);
            retrieved_evidence.push((id, sim));
            total_sim += sim;
        }

        let avg_grounding = if !retrieved_evidence.is_empty() {
            total_sim / retrieved_evidence.len() as f32
        } else {
            0.5
        };

        let grounded_confidence =
            ((base_verify.confidence * 0.60) + (avg_grounding * 0.40)).clamp(0.0, 1.0);
        let is_verified = grounded_confidence >= base_verify.threshold;
        let margin = grounded_confidence - base_verify.threshold;
        let latency_us = start.elapsed().as_micros() as u64;

        Ok(GroundedVerifyResult {
            is_verified,
            confidence: grounded_confidence,
            threshold: base_verify.threshold,
            margin,
            retrieved_evidence,
            grounding_score: avg_grounding,
            latency_us,
        })
    }

    /// HNSW-Grounded categorical classification: cross-references candidate labels
    /// with empirical neighbor records in an in-database HNSW vector index.
    pub fn classify_grounded(
        &self,
        input: &str,
        candidates: &[&str],
        index: &crate::vector::HnswIndex,
        top_k: usize,
    ) -> Result<GroundedClassificationResult> {
        let start = Instant::now();
        let base_class = self.classify(input, candidates)?;

        let input_tokens = self.tokenizer.tokenize(input);
        let mut query_vec = self.runtime.forward_pool(&input_tokens);
        if query_vec.len() != index.dimensions {
            if query_vec.len() > index.dimensions {
                query_vec.truncate(index.dimensions);
            } else {
                query_vec.resize(index.dimensions, 0.0);
            }
        }

        let k = top_k.max(1);
        let knn_matches = index.search_knn(&query_vec, k, crate::vector::DistanceMetric::Cosine)?;

        let mut retrieved_evidence = Vec::with_capacity(knn_matches.len());
        let mut total_sim = 0.0f32;
        for (id, dist) in knn_matches {
            let sim = (1.0 - dist.clamp(0.0, 2.0) / 2.0).clamp(0.0, 1.0);
            retrieved_evidence.push((id, sim));
            total_sim += sim;
        }

        let avg_grounding = if !retrieved_evidence.is_empty() {
            total_sim / retrieved_evidence.len() as f32
        } else {
            0.5
        };

        let modulated_conf =
            ((base_class.confidence * 0.65) + (avg_grounding * 0.35)).clamp(0.0, 1.0);
        let latency_us = start.elapsed().as_micros() as u64;

        Ok(GroundedClassificationResult {
            top_choice: base_class.top_choice,
            confidence: modulated_conf,
            probabilities: base_class.probabilities,
            retrieved_evidence,
            grounding_score: avg_grounding,
            entropy: base_class.entropy,
            latency_us,
        })
    }

    /// Agent workflow and graph traversal branch routing
    pub fn route(&self, state: &str, route_options: &[&str]) -> Result<RouteResult> {
        let start = Instant::now();
        if route_options.is_empty() {
            return Err(Error::Internal(
                "Tap::route requires at least one route option".into(),
            ));
        }

        let classification = self.classify(state, route_options)?;
        let selected_route = classification.top_choice;
        let confidence = classification.confidence;

        let alternative_routes: Vec<String> = classification
            .probabilities
            .into_iter()
            .filter(|(name, _)| name != &selected_route)
            .map(|(name, _)| name)
            .collect();

        let latency_us = start.elapsed().as_micros() as u64;

        Ok(RouteResult {
            selected_route,
            priority: 1,
            confidence,
            alternative_routes,
            latency_us,
        })
    }
}

pub(crate) fn extract_word_set(text: &str) -> HashSet<String> {
    text.to_lowercase()
        .split(|c: char| c.is_whitespace() || c.is_ascii_punctuation())
        .filter(|s| !s.is_empty())
        .map(|s| s.to_string())
        .collect()
}

pub(crate) fn compute_jaccard_overlap(a: &HashSet<String>, b: &HashSet<String>) -> f32 {
    if a.is_empty() || b.is_empty() {
        return 0.0;
    }
    let mut score = 0.0f32;
    for wa in a {
        for wb in b {
            if wa == wb {
                score += 1.0;
            } else if (wa.len() >= 4 && wb.len() >= 4) && (wa.starts_with(wb) || wb.starts_with(wa))
            {
                score += 0.7;
            }
        }
    }
    let normalizer = (a.len().min(b.len()) as f32).max(1.0);
    (score / normalizer).min(1.0)
}

fn get_domain_associations(label: &str) -> &'static [&'static str] {
    let clean = label.to_lowercase();
    let parts: Vec<&str> = clean
        .split(|c: char| c == '_' || c == '-' || is_sep(c))
        .collect();
    for p in parts {
        match p {
            "infrastructure" | "infra" => {
                return &[
                    "server", "disk", "cpu", "memory", "storage", "database", "cluster", "node",
                    "network", "cloud", "host", "downtime", "outage", "load", "latency",
                    "hardware", "capacity", "timeout",
                ];
            }
            "billing" | "finance" => {
                return &[
                    "charge",
                    "charged",
                    "invoice",
                    "payment",
                    "card",
                    "credit",
                    "cost",
                    "price",
                    "refund",
                    "subscription",
                    "fee",
                    "receipt",
                    "dollar",
                    "money",
                    "pricing",
                    "bill",
                    "pay",
                ];
            }
            "technical" | "tech" | "bug" => {
                return &[
                    "bug",
                    "crash",
                    "crashes",
                    "error",
                    "exception",
                    "freeze",
                    "code",
                    "glitch",
                    "failure",
                    "broken",
                    "app",
                    "login",
                    "patch",
                    "stack",
                    "null",
                    "panic",
                    "issue",
                    "version",
                ];
            }
            "sales" => {
                return &[
                    "purchase",
                    "pricing",
                    "quote",
                    "buy",
                    "lead",
                    "deal",
                    "enterprise",
                    "discount",
                    "demo",
                    "upgrade",
                    "contact",
                    "procurement",
                    "contract",
                ];
            }
            "support" => {
                return &[
                    "help",
                    "ticket",
                    "question",
                    "assistance",
                    "inquiry",
                    "agent",
                    "service",
                    "guide",
                    "triage",
                ];
            }
            "security" | "fraud" => {
                return &[
                    "breach",
                    "hack",
                    "compromised",
                    "unauthorized",
                    "stolen",
                    "phishing",
                    "takeover",
                    "attacker",
                    "malware",
                    "suspicious",
                    "credential",
                ];
            }
            "weather" => {
                return &[
                    "storm",
                    "hail",
                    "wind",
                    "rain",
                    "flood",
                    "hurricane",
                    "tornado",
                    "snow",
                    "climate",
                    "freeze",
                    "lightning",
                    "hailstorm",
                ];
            }
            _ => {}
        }
    }
    &[]
}

fn is_sep(c: char) -> bool {
    c.is_whitespace() || c.is_ascii_punctuation()
}

fn compute_sequence_bigram_overlap(s1: &str, s2: &str) -> f32 {
    let stopwords = [
        "the", "a", "an", "is", "are", "was", "were", "to", "of", "in", "on", "at", "by", "for",
        "with", "about",
    ];
    let w1: Vec<String> = s1
        .split(|c: char| c.is_whitespace() || c.is_ascii_punctuation())
        .filter(|w| !w.is_empty())
        .map(|w| w.to_lowercase())
        .filter(|w| !stopwords.contains(&w.as_str()))
        .collect();
    let w2: Vec<String> = s2
        .split(|c: char| c.is_whitespace() || c.is_ascii_punctuation())
        .filter(|w| !w.is_empty())
        .map(|w| w.to_lowercase())
        .filter(|w| !stopwords.contains(&w.as_str()))
        .collect();
    if w1.len() < 2 || w2.len() < 2 {
        return 1.0;
    }
    let b1: HashSet<(String, String)> = w1
        .windows(2)
        .map(|w| (w[0].clone(), w[1].clone()))
        .collect();
    let b2: HashSet<(String, String)> = w2
        .windows(2)
        .map(|w| (w[0].clone(), w[1].clone()))
        .collect();
    let matches = b1.intersection(&b2).count();
    let denom = b1.len().min(b2.len()).max(1);
    matches as f32 / denom as f32
}

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

    #[test]
    fn test_tap_classify() {
        let tap = TapEngine::default();
        let res = tap
            .classify(
                "Refund requested because product was broken",
                &["refund", "billing_support", "sales"],
            )
            .unwrap();
        assert_eq!(res.top_choice, "refund");
        assert!(res.confidence > 0.4);
    }

    #[test]
    fn test_tap_verify() {
        let tap = TapEngine::default();
        let res = tap
            .verify(
                "The user has an active premium subscription",
                "user is active subscriber",
            )
            .unwrap();
        assert!(res.is_verified);
        assert!(res.confidence >= 0.50);
    }

    #[test]
    fn test_tap_score() {
        let tap = TapEngine::default();
        let res = tap
            .score(
                "Server down critical emergency production failure",
                "emergency severity",
            )
            .unwrap();
        assert!(res.score > 0.5);
        assert!(res.matched_signals.contains(&"emergency".to_string()));
    }

    #[test]
    fn test_tap_route() {
        let tap = TapEngine::default();
        let res = tap
            .route(
                "High risk fraudulent transfer detected: block immediately",
                &["block_transfer", "standard_review", "allow"],
            )
            .unwrap();
        assert_eq!(res.selected_route, "block_transfer");
    }

    #[test]
    fn test_tap_throughput_and_latency_benchmark() {
        let tap = TapEngine::default();
        let iterations = 2000;

        let start = web_time::Instant::now();
        for _ in 0..iterations {
            let _ = tap
                .classify(
                    "Refund requested because product was broken on delivery",
                    &["refund", "billing_support", "sales"],
                )
                .unwrap();
        }
        let classify_total = start.elapsed();
        let classify_us = classify_total.as_micros() as f64 / iterations as f64;
        let classify_qps = iterations as f64 / classify_total.as_secs_f64();

        let start_v = web_time::Instant::now();
        for _ in 0..iterations {
            let _ = tap
                .verify(
                    "The user has an active premium subscription",
                    "user is active subscriber",
                )
                .unwrap();
        }
        let verify_total = start_v.elapsed();
        let verify_us = verify_total.as_micros() as f64 / iterations as f64;
        let verify_qps = iterations as f64 / verify_total.as_secs_f64();

        println!("\n=== TAP DECISION CORE EMPIRICAL BENCHMARK ===");
        println!(
            "Classify Latency: {:.2} µs ({:.0} decisions/sec)",
            classify_us, classify_qps
        );
        println!(
            "Verify Latency:   {:.2} µs ({:.0} verifications/sec)",
            verify_us, verify_qps
        );
        println!("==============================================\n");

        assert!(
            classify_us.is_finite(),
            "Classify measurement must be finite"
        );
        assert!(verify_us.is_finite(), "Verify measurement must be finite");
    }

    #[test]
    fn test_tap_multilingual_decision() {
        let tap = TapEngine::default();

        // 1. Bahasa Melayu / Pasar
        let bm_res = tap
            .classify(
                "Barang rosak dan pecah, nak refund balik duit",
                &["refund", "tanya_soalan", "daftar_akaun"],
            )
            .unwrap();
        assert_eq!(bm_res.top_choice, "refund");

        // 2. Multilingual negation check (Melayu: 'tidak')
        let neg_res = tap
            .verify(
                "Pengguna tidak bersetuju dengan syarat perjanjian",
                "pengguna bersetuju dengan syarat perjanjian",
            )
            .unwrap();
        assert!(
            !neg_res.is_verified,
            "Negation 'tidak' must prevent false truth verification"
        );

        // 3. Spanish decision
        let es_res = tap
            .classify(
                "Por favor cancelar mi cuenta inmediatamente",
                &["cancelar", "soporte", "pagar"],
            )
            .unwrap();
        assert_eq!(es_res.top_choice, "cancelar");

        // 4. French decision
        let fr_res = tap
            .classify(
                "Demande urgente de remboursement pour commande perdue",
                &["remboursement", "compte", "securite"],
            )
            .unwrap();
        assert_eq!(fr_res.top_choice, "remboursement");
    }

    #[test]
    fn test_tap_grounded_with_hnsw() {
        use crate::traits::VectorIndexEngine;
        use crate::vector::{DistanceMetric, HnswIndex};

        let tap = TapEngine::default();
        let dim = tap.runtime.dim();
        let mut index = HnswIndex::new(dim, DistanceMetric::Cosine);

        // Populate HNSW index with policy vectors
        let tokens_refund = tap
            .tokenizer
            .tokenize("Valid warranty return policy for damaged delivery item");
        let vec_refund = tap.runtime.forward_pool(&tokens_refund);
        index.insert_vector(101, &vec_refund).unwrap();

        let tokens_fraud = tap
            .tokenizer
            .tokenize("Fraudulent stolen credit card suspicious account activity");
        let vec_fraud = tap.runtime.forward_pool(&tokens_fraud);
        index.insert_vector(202, &vec_fraud).unwrap();

        // Test Grounded Truth Verification
        let grounded_v = tap
            .verify_grounded(
                "Customer provided photo proof of damaged delivery item",
                "valid warranty return damaged delivery",
                &index,
                2,
            )
            .unwrap();

        assert!(grounded_v.is_verified);
        assert!(!grounded_v.retrieved_evidence.is_empty());
        assert_eq!(grounded_v.retrieved_evidence[0].0, 101); // Node 101 matched as closest policy
        assert!(grounded_v.grounding_score > 0.5);

        // Test Grounded Classification
        let grounded_c = tap
            .classify_grounded(
                "Customer claims parcel arrived broken with shattered parts",
                &["warranty_return", "fraud_investigation"],
                &index,
                2,
            )
            .unwrap();

        assert!(!grounded_c.retrieved_evidence.is_empty());
        assert!(grounded_c.confidence > 0.5);
    }
}