reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//! ProofGuard Module - Multi-Source Verification
//!
//! Triangulates claims across 3+ independent sources to verify factual accuracy.
//!
//! ## Core Features
//!
//! - **3+ Source Requirement**: Enforces triangulation protocol (CONS-006)
//! - **Contradiction Detection**: Identifies conflicting evidence
//! - **Source Tier Ranking**: Weights evidence by source quality
//! - **Confidence Scoring**: Produces calibrated verification scores
//!
//! ## Source Tiers
//!
//! | Tier | Weight | Examples |
//! |------|--------|----------|
//! | Tier 1 (Primary) | 1.0 | Official docs, peer-reviewed papers, primary sources |
//! | Tier 2 (Secondary) | 0.7 | Reputable news, expert blogs, industry reports |
//! | Tier 3 (Independent) | 0.4 | Community content, forums |
//! | Tier 4 (Unverified) | 0.2 | Social media, unknown sources |
//!
//! ## Usage
//!
//! ```rust,ignore
//! use reasonkit::thinktool::modules::{ProofGuard, ThinkToolContext, ThinkToolModule};
//!
//! let proofguard = ProofGuard::new();
//!
//! // Context with claim and sources (JSON format)
//! let context = ThinkToolContext {
//!     query: r#"{
//!         "claim": "Rust is memory-safe without a garbage collector",
//!         "sources": [
//!             {"name": "Rust Book", "tier": "Primary", "stance": "Support"},
//!             {"name": "ACM Paper", "tier": "Primary", "stance": "Support"},
//!             {"name": "Tech Blog", "tier": "Secondary", "stance": "Support"}
//!         ]
//!     }"#.to_string(),
//!     previous_steps: vec![],
//! };
//!
//! let result = proofguard.execute(&context)?;
//! ```

use super::{ThinkToolContext, ThinkToolModule, ThinkToolModuleConfig, ThinkToolOutput};
use crate::error::Error;
use crate::thinktool::triangulation::{
    IssueSeverity, Source, SourceTier, SourceType, Stance, TriangulationConfig,
    TriangulationIssueType, TriangulationResult, Triangulator, VerificationConfidence,
    VerificationRecommendation,
};
use serde::{Deserialize, Serialize};

/// ProofGuard reasoning module for fact verification.
///
/// Verifies claims using triangulated evidence from multiple sources.
/// Implements the three-source rule (CONS-006) and provides structured
/// verification output with confidence scoring.
pub struct ProofGuard {
    /// Module configuration
    config: ThinkToolModuleConfig,
    /// Triangulation configuration
    triangulation_config: TriangulationConfig,
}

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

/// Input format for ProofGuard verification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofGuardInput {
    /// The claim to verify
    pub claim: String,
    /// Sources supporting or contradicting the claim
    #[serde(default)]
    pub sources: Vec<ProofGuardSource>,
    /// Optional: Minimum number of sources required (default: 3)
    #[serde(default)]
    pub min_sources: Option<usize>,
    /// Optional: Require at least one Tier 1 source (default: true)
    #[serde(default)]
    pub require_tier1: Option<bool>,
}

/// Source input for ProofGuard
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofGuardSource {
    /// Source name/title
    pub name: String,
    /// Source tier (Primary, Secondary, Independent, Unverified)
    #[serde(default = "default_tier")]
    pub tier: String,
    /// Source type (Academic, Documentation, News, Expert, Government, Industry, Community, Social, PrimaryData)
    #[serde(default = "default_source_type")]
    pub source_type: String,
    /// Source stance (Support, Contradict, Neutral, Partial)
    #[serde(default = "default_stance")]
    pub stance: String,
    /// URL if available
    #[serde(default)]
    pub url: Option<String>,
    /// Domain/field
    #[serde(default)]
    pub domain: Option<String>,
    /// Author/organization
    #[serde(default)]
    pub author: Option<String>,
    /// Direct quote supporting/refuting the claim
    #[serde(default)]
    pub quote: Option<String>,
    /// Whether URL has been verified accessible
    #[serde(default)]
    pub verified: bool,
}

fn default_tier() -> String {
    "Unverified".to_string()
}

fn default_source_type() -> String {
    "Documentation".to_string()
}

fn default_stance() -> String {
    "Neutral".to_string()
}

/// Output from ProofGuard verification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofGuardOutput {
    /// Overall verification verdict
    pub verdict: ProofGuardVerdict,
    /// Claim being verified
    pub claim: String,
    /// Verification score (0.0-1.0)
    pub verification_score: f64,
    /// Whether the claim is verified
    pub is_verified: bool,
    /// Confidence level
    pub confidence_level: String,
    /// Recommendation for how to treat this claim
    pub recommendation: String,
    /// Sources analyzed
    pub sources: Vec<SourceSummary>,
    /// Detected contradictions
    pub contradictions: Vec<ContradictionInfo>,
    /// Issues found during verification
    pub issues: Vec<IssueInfo>,
    /// Statistics
    pub stats: VerificationStats,
}

/// Verification verdict
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProofGuardVerdict {
    /// Claim verified with high confidence
    Verified,
    /// Claim partially verified, needs qualifier
    PartiallyVerified,
    /// Conflicting evidence found
    Contested,
    /// Insufficient sources
    InsufficientSources,
    /// Claim contradicted by evidence
    Refuted,
    /// Unable to determine
    Inconclusive,
}

impl std::fmt::Display for ProofGuardVerdict {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ProofGuardVerdict::Verified => write!(f, "Verified"),
            ProofGuardVerdict::PartiallyVerified => write!(f, "Partially Verified"),
            ProofGuardVerdict::Contested => write!(f, "Contested"),
            ProofGuardVerdict::InsufficientSources => write!(f, "Insufficient Sources"),
            ProofGuardVerdict::Refuted => write!(f, "Refuted"),
            ProofGuardVerdict::Inconclusive => write!(f, "Inconclusive"),
        }
    }
}

/// Summary of a source used in verification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceSummary {
    /// Source name
    pub name: String,
    /// Source tier label
    pub tier: String,
    /// Tier weight (0.0-1.0)
    pub weight: f64,
    /// Stance on the claim
    pub stance: String,
    /// Whether verified
    pub verified: bool,
    /// Effective weight after modifiers
    pub effective_weight: f64,
}

/// Information about detected contradictions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContradictionInfo {
    /// Sources that support the claim
    pub supporting_sources: Vec<String>,
    /// Sources that contradict the claim
    pub contradicting_sources: Vec<String>,
    /// Severity of the contradiction
    pub severity: String,
    /// Description
    pub description: String,
}

/// Information about verification issues
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IssueInfo {
    /// Issue type
    pub issue_type: String,
    /// Severity (Warning, Error, Critical)
    pub severity: String,
    /// Description
    pub description: String,
}

/// Statistics from verification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationStats {
    /// Total sources analyzed
    pub total_sources: usize,
    /// Number of supporting sources
    pub supporting_count: usize,
    /// Number of contradicting sources
    pub contradicting_count: usize,
    /// Number of neutral sources
    pub neutral_count: usize,
    /// Number of Tier 1 sources
    pub tier1_count: usize,
    /// Number of Tier 2 sources
    pub tier2_count: usize,
    /// Number of Tier 3 sources
    pub tier3_count: usize,
    /// Number of Tier 4 sources
    pub tier4_count: usize,
    /// Source diversity score (0.0-1.0)
    pub source_diversity: f64,
    /// Triangulation weight (support - contradict)
    pub triangulation_weight: f64,
}

impl ProofGuard {
    /// Create a new ProofGuard module instance.
    pub fn new() -> Self {
        Self {
            config: ThinkToolModuleConfig {
                name: "ProofGuard".to_string(),
                version: "2.1.0".to_string(),
                description: "Triangulation-based fact verification with 3+ source requirement"
                    .to_string(),
                confidence_weight: 0.30,
            },
            triangulation_config: TriangulationConfig::default(),
        }
    }

    /// Create with custom triangulation configuration.
    pub fn with_config(triangulation_config: TriangulationConfig) -> Self {
        Self {
            config: ThinkToolModuleConfig {
                name: "ProofGuard".to_string(),
                version: "2.1.0".to_string(),
                description: "Triangulation-based fact verification with 3+ source requirement"
                    .to_string(),
                confidence_weight: 0.30,
            },
            triangulation_config,
        }
    }

    /// Create with strict configuration (requires 2 Tier 1 sources).
    pub fn strict() -> Self {
        let config = TriangulationConfig {
            min_sources: 3,
            min_tier1_sources: 2,
            verification_threshold: 0.7,
            require_verified_urls: true,
            require_domain_diversity: true,
            ..Default::default()
        };
        Self::with_config(config)
    }

    /// Create with relaxed configuration (1 Tier 1 source sufficient).
    pub fn relaxed() -> Self {
        let config = TriangulationConfig {
            min_sources: 2,
            min_tier1_sources: 0,
            verification_threshold: 0.5,
            require_verified_urls: false,
            require_domain_diversity: false,
            ..Default::default()
        };
        Self::with_config(config)
    }

    /// Parse the input from context.
    fn parse_input(&self, context: &ThinkToolContext) -> Result<ProofGuardInput, Error> {
        // First try to parse as JSON
        if let Ok(input) = serde_json::from_str::<ProofGuardInput>(&context.query) {
            return Ok(input);
        }

        // If not JSON, treat as plain text claim with no sources
        Ok(ProofGuardInput {
            claim: context.query.clone(),
            sources: Vec::new(),
            min_sources: None,
            require_tier1: None,
        })
    }

    /// Convert ProofGuardSource to triangulation Source.
    fn convert_source(&self, src: &ProofGuardSource) -> Source {
        let tier = match src.tier.to_lowercase().as_str() {
            "primary" | "tier1" | "tier 1" => SourceTier::Primary,
            "secondary" | "tier2" | "tier 2" => SourceTier::Secondary,
            "independent" | "tier3" | "tier 3" => SourceTier::Independent,
            _ => SourceTier::Unverified,
        };

        let source_type = match src.source_type.to_lowercase().as_str() {
            "academic" => SourceType::Academic,
            "documentation" | "docs" => SourceType::Documentation,
            "news" => SourceType::News,
            "expert" | "blog" => SourceType::Expert,
            "government" | "gov" => SourceType::Government,
            "industry" => SourceType::Industry,
            "community" | "forum" => SourceType::Community,
            "social" | "socialmedia" => SourceType::Social,
            "primarydata" | "data" => SourceType::PrimaryData,
            _ => SourceType::Documentation,
        };

        let stance = match src.stance.to_lowercase().as_str() {
            "support" | "supports" | "supporting" => Stance::Support,
            "contradict" | "contradicts" | "contradicting" | "oppose" | "against" => {
                Stance::Contradict
            }
            "partial" | "partially" => Stance::Partial,
            _ => Stance::Neutral,
        };

        let mut source = Source::new(&src.name, tier)
            .with_type(source_type)
            .with_stance(stance);

        if let Some(url) = &src.url {
            source = source.with_url(url);
        }
        if let Some(domain) = &src.domain {
            source = source.with_domain(domain);
        }
        if let Some(author) = &src.author {
            source = source.with_author(author);
        }
        if let Some(quote) = &src.quote {
            source = source.with_quote(quote);
        }
        if src.verified {
            source = source.verified();
        }

        source
    }

    /// Convert triangulation result to ProofGuard output.
    fn convert_result(&self, result: &TriangulationResult) -> ProofGuardOutput {
        // Determine verdict
        let verdict = self.determine_verdict(result);

        // Build source summaries
        let sources: Vec<SourceSummary> = result
            .sources
            .iter()
            .map(|s| SourceSummary {
                name: s.name.clone(),
                tier: s.tier.label().to_string(),
                weight: s.tier.weight() as f64,
                stance: format!("{:?}", s.stance),
                verified: s.verified,
                effective_weight: s.effective_weight() as f64,
            })
            .collect();

        // Build contradiction info if present
        let contradictions = if result.contradict_count > 0 && result.support_count > 0 {
            let supporting: Vec<String> = result
                .sources
                .iter()
                .filter(|s| matches!(s.stance, Stance::Support | Stance::Partial))
                .map(|s| s.name.clone())
                .collect();
            let contradicting: Vec<String> = result
                .sources
                .iter()
                .filter(|s| matches!(s.stance, Stance::Contradict))
                .map(|s| s.name.clone())
                .collect();

            vec![ContradictionInfo {
                supporting_sources: supporting,
                contradicting_sources: contradicting,
                severity: if result.contradict_count >= result.support_count {
                    "High".to_string()
                } else {
                    "Medium".to_string()
                },
                description: format!(
                    "{} sources support while {} sources contradict the claim",
                    result.support_count, result.contradict_count
                ),
            }]
        } else {
            vec![]
        };

        // Build issue info
        let issues: Vec<IssueInfo> = result
            .issues
            .iter()
            .map(|i| IssueInfo {
                issue_type: format!("{:?}", i.issue_type),
                severity: match i.severity {
                    IssueSeverity::Warning => "Warning".to_string(),
                    IssueSeverity::Error => "Error".to_string(),
                    IssueSeverity::Critical => "Critical".to_string(),
                },
                description: i.description.clone(),
            })
            .collect();

        // Calculate tier counts
        let tier1_count = result
            .sources
            .iter()
            .filter(|s| s.tier == SourceTier::Primary)
            .count();
        let tier2_count = result
            .sources
            .iter()
            .filter(|s| s.tier == SourceTier::Secondary)
            .count();
        let tier3_count = result
            .sources
            .iter()
            .filter(|s| s.tier == SourceTier::Independent)
            .count();
        let tier4_count = result
            .sources
            .iter()
            .filter(|s| s.tier == SourceTier::Unverified)
            .count();
        let neutral_count = result
            .sources
            .iter()
            .filter(|s| matches!(s.stance, Stance::Neutral))
            .count();

        // Build stats
        let stats = VerificationStats {
            total_sources: result.sources.len(),
            supporting_count: result.support_count,
            contradicting_count: result.contradict_count,
            neutral_count,
            tier1_count,
            tier2_count,
            tier3_count,
            tier4_count,
            source_diversity: result.source_diversity as f64,
            triangulation_weight: result.triangulation_weight as f64,
        };

        // Format recommendation
        let recommendation = match &result.recommendation {
            VerificationRecommendation::AcceptAsFact => "Accept as fact".to_string(),
            VerificationRecommendation::AcceptWithQualifier(q) => {
                format!("Accept with qualifier: {}", q)
            }
            VerificationRecommendation::NeedsMoreSources => {
                "Need more sources before making this claim".to_string()
            }
            VerificationRecommendation::PresentBothSides => {
                "Present both supporting and contradicting evidence".to_string()
            }
            VerificationRecommendation::Reject => {
                "Evidence does not support this claim".to_string()
            }
            VerificationRecommendation::Inconclusive => {
                "Unable to determine - more research needed".to_string()
            }
        };

        ProofGuardOutput {
            verdict,
            claim: result.claim.clone(),
            verification_score: result.verification_score as f64,
            is_verified: result.is_verified,
            confidence_level: format!("{:?}", result.confidence),
            recommendation,
            sources,
            contradictions,
            issues,
            stats,
        }
    }

    /// Determine the verdict based on triangulation result.
    fn determine_verdict(&self, result: &TriangulationResult) -> ProofGuardVerdict {
        // Check for critical issues first
        if result
            .issues
            .iter()
            .any(|i| i.issue_type == TriangulationIssueType::InsufficientSources)
        {
            return ProofGuardVerdict::InsufficientSources;
        }

        // Check for contradictions
        if result.contradict_count > 0 && result.support_count > 0 {
            if result.contradict_count > result.support_count {
                return ProofGuardVerdict::Refuted;
            }
            return ProofGuardVerdict::Contested;
        }

        // Check if refuted (only contradictions, no support)
        if result.contradict_count > 0 && result.support_count == 0 {
            return ProofGuardVerdict::Refuted;
        }

        // Check verification status
        match result.confidence {
            VerificationConfidence::High => {
                if result.is_verified {
                    ProofGuardVerdict::Verified
                } else {
                    ProofGuardVerdict::PartiallyVerified
                }
            }
            VerificationConfidence::Medium => {
                if result.is_verified {
                    ProofGuardVerdict::PartiallyVerified
                } else {
                    ProofGuardVerdict::Inconclusive
                }
            }
            VerificationConfidence::Low => ProofGuardVerdict::Inconclusive,
            VerificationConfidence::Unverifiable => ProofGuardVerdict::Inconclusive,
        }
    }

    /// Calculate overall confidence for ThinkToolOutput.
    fn calculate_confidence(&self, result: &TriangulationResult) -> f64 {
        // Base confidence from verification score
        let base_confidence = result.verification_score as f64;

        // Adjust based on confidence level
        let level_modifier = match result.confidence {
            VerificationConfidence::High => 1.0,
            VerificationConfidence::Medium => 0.85,
            VerificationConfidence::Low => 0.6,
            VerificationConfidence::Unverifiable => 0.3,
        };

        // Penalize for issues
        let issue_penalty: f64 = result
            .issues
            .iter()
            .map(|i| match i.severity {
                IssueSeverity::Critical => 0.3,
                IssueSeverity::Error => 0.15,
                IssueSeverity::Warning => 0.05,
            })
            .sum();

        // Boost for source diversity
        let diversity_boost = result.source_diversity as f64 * 0.1;

        // Calculate final confidence - return directly
        (base_confidence * level_modifier - issue_penalty + diversity_boost).clamp(0.0, 1.0)
    }
}

impl ThinkToolModule for ProofGuard {
    fn config(&self) -> &ThinkToolModuleConfig {
        &self.config
    }

    fn execute(&self, context: &ThinkToolContext) -> Result<ThinkToolOutput, Error> {
        // Parse input
        let input = self.parse_input(context)?;

        // Apply any input-level configuration overrides
        let mut config = self.triangulation_config.clone();
        if let Some(min) = input.min_sources {
            config.min_sources = min;
        }
        if let Some(req) = input.require_tier1 {
            config.min_tier1_sources = if req { 1 } else { 0 };
        }

        // Create triangulator
        let mut triangulator = Triangulator::with_config(config);
        triangulator.set_claim(&input.claim);

        // Convert and add sources
        for src in &input.sources {
            let source = self.convert_source(src);
            triangulator.add_source(source);
        }

        // Execute verification
        let result = triangulator.verify();

        // Convert to ProofGuard output
        let output = self.convert_result(&result);

        // Calculate confidence
        let confidence = self.calculate_confidence(&result);

        // Return structured output
        Ok(ThinkToolOutput {
            module: self.config.name.clone(),
            confidence,
            output: serde_json::to_value(&output).map_err(|e| {
                Error::ThinkToolExecutionError(format!("Failed to serialize output: {}", e))
            })?,
        })
    }
}

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

    #[test]
    fn test_proofguard_new() {
        let pg = ProofGuard::new();
        assert_eq!(pg.config.name, "ProofGuard");
        assert_eq!(pg.config.version, "2.1.0");
    }

    #[test]
    fn test_proofguard_default() {
        let pg = ProofGuard::default();
        assert_eq!(pg.config.name, "ProofGuard");
    }

    #[test]
    fn test_proofguard_strict() {
        let pg = ProofGuard::strict();
        assert_eq!(pg.triangulation_config.min_tier1_sources, 2);
        assert!(pg.triangulation_config.require_verified_urls);
    }

    #[test]
    fn test_proofguard_relaxed() {
        let pg = ProofGuard::relaxed();
        assert_eq!(pg.triangulation_config.min_sources, 2);
        assert_eq!(pg.triangulation_config.min_tier1_sources, 0);
    }

    #[test]
    fn test_execute_with_json_input() {
        let pg = ProofGuard::new();
        let input_json = r#"{
            "claim": "Rust is memory-safe without garbage collection",
            "sources": [
                {"name": "Rust Book", "tier": "Primary", "stance": "Support", "verified": true},
                {"name": "ACM Paper", "tier": "Primary", "stance": "Support", "domain": "PL"},
                {"name": "Tech Blog", "tier": "Secondary", "stance": "Support"}
            ]
        }"#;

        let context = ThinkToolContext {
            query: input_json.to_string(),
            previous_steps: vec![],
        };

        let result = pg.execute(&context).unwrap();

        assert_eq!(result.module, "ProofGuard");
        assert!(result.confidence > 0.0);

        // Parse the output
        let output: ProofGuardOutput = serde_json::from_value(result.output).unwrap();
        assert_eq!(
            output.claim,
            "Rust is memory-safe without garbage collection"
        );
        assert_eq!(output.stats.total_sources, 3);
        assert_eq!(output.stats.tier1_count, 2);
        assert!(output.is_verified);
    }

    #[test]
    fn test_execute_with_plain_text_claim() {
        let pg = ProofGuard::new();
        let context = ThinkToolContext {
            query: "This is a plain text claim".to_string(),
            previous_steps: vec![],
        };

        let result = pg.execute(&context).unwrap();

        // Should return insufficient sources
        let output: ProofGuardOutput = serde_json::from_value(result.output).unwrap();
        assert_eq!(output.verdict, ProofGuardVerdict::InsufficientSources);
        assert!(!output.is_verified);
    }

    #[test]
    fn test_execute_with_contradictions() {
        let pg = ProofGuard::new();
        let input_json = r#"{
            "claim": "AI will achieve AGI by 2030",
            "sources": [
                {"name": "Optimist Paper", "tier": "Primary", "stance": "Support"},
                {"name": "Skeptic Paper", "tier": "Primary", "stance": "Contradict"},
                {"name": "Neutral Review", "tier": "Secondary", "stance": "Partial"}
            ]
        }"#;

        let context = ThinkToolContext {
            query: input_json.to_string(),
            previous_steps: vec![],
        };

        let result = pg.execute(&context).unwrap();
        let output: ProofGuardOutput = serde_json::from_value(result.output).unwrap();

        assert_eq!(output.verdict, ProofGuardVerdict::Contested);
        assert!(!output.contradictions.is_empty());
        assert!(output.stats.contradicting_count > 0);
    }

    #[test]
    fn test_execute_refuted_claim() {
        let pg = ProofGuard::new();
        let input_json = r#"{
            "claim": "The Earth is flat",
            "sources": [
                {"name": "NASA", "tier": "Primary", "stance": "Contradict"},
                {"name": "ESA", "tier": "Primary", "stance": "Contradict"},
                {"name": "Physics Journal", "tier": "Primary", "stance": "Contradict"}
            ]
        }"#;

        let context = ThinkToolContext {
            query: input_json.to_string(),
            previous_steps: vec![],
        };

        let result = pg.execute(&context).unwrap();
        let output: ProofGuardOutput = serde_json::from_value(result.output).unwrap();

        assert_eq!(output.verdict, ProofGuardVerdict::Refuted);
        assert!(!output.is_verified);
    }

    #[test]
    fn test_source_tier_parsing() {
        let pg = ProofGuard::new();

        // Test various tier formats
        let test_cases = vec![
            ("Primary", SourceTier::Primary),
            ("primary", SourceTier::Primary),
            ("Tier1", SourceTier::Primary),
            ("tier 1", SourceTier::Primary),
            ("Secondary", SourceTier::Secondary),
            ("tier2", SourceTier::Secondary),
            ("Independent", SourceTier::Independent),
            ("tier 3", SourceTier::Independent),
            ("Unverified", SourceTier::Unverified),
            ("unknown", SourceTier::Unverified),
        ];

        for (input, expected) in test_cases {
            let src = ProofGuardSource {
                name: "Test".to_string(),
                tier: input.to_string(),
                source_type: default_source_type(),
                stance: default_stance(),
                url: None,
                domain: None,
                author: None,
                quote: None,
                verified: false,
            };
            let converted = pg.convert_source(&src);
            assert_eq!(converted.tier, expected, "Failed for input: {}", input);
        }
    }

    #[test]
    fn test_stance_parsing() {
        let pg = ProofGuard::new();

        let test_cases = vec![
            ("Support", Stance::Support),
            ("supports", Stance::Support),
            ("Contradict", Stance::Contradict),
            ("against", Stance::Contradict),
            ("Partial", Stance::Partial),
            ("Neutral", Stance::Neutral),
            ("unknown", Stance::Neutral),
        ];

        for (input, expected) in test_cases {
            let src = ProofGuardSource {
                name: "Test".to_string(),
                tier: default_tier(),
                source_type: default_source_type(),
                stance: input.to_string(),
                url: None,
                domain: None,
                author: None,
                quote: None,
                verified: false,
            };
            let converted = pg.convert_source(&src);
            assert_eq!(converted.stance, expected, "Failed for input: {}", input);
        }
    }

    #[test]
    fn test_verdict_display() {
        assert_eq!(format!("{}", ProofGuardVerdict::Verified), "Verified");
        assert_eq!(
            format!("{}", ProofGuardVerdict::PartiallyVerified),
            "Partially Verified"
        );
        assert_eq!(format!("{}", ProofGuardVerdict::Contested), "Contested");
        assert_eq!(
            format!("{}", ProofGuardVerdict::InsufficientSources),
            "Insufficient Sources"
        );
        assert_eq!(format!("{}", ProofGuardVerdict::Refuted), "Refuted");
        assert_eq!(
            format!("{}", ProofGuardVerdict::Inconclusive),
            "Inconclusive"
        );
    }

    #[test]
    fn test_min_sources_override() {
        let pg = ProofGuard::new();
        let input_json = r#"{
            "claim": "Some claim",
            "sources": [
                {"name": "Source A", "tier": "Primary", "stance": "Support"}
            ],
            "min_sources": 1
        }"#;

        let context = ThinkToolContext {
            query: input_json.to_string(),
            previous_steps: vec![],
        };

        let result = pg.execute(&context).unwrap();
        let output: ProofGuardOutput = serde_json::from_value(result.output).unwrap();

        // With min_sources=1, should not be InsufficientSources
        assert_ne!(output.verdict, ProofGuardVerdict::InsufficientSources);
    }
}