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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
//! BrutalHonesty Module - Adversarial Self-Critique
//!
//! Red-team analysis that finds flaws before others do, challenges
//! assumptions aggressively, and scores confidence with skepticism.
//!
//! ## Design Philosophy
//!
//! BrutalHonesty applies adversarial thinking to identify weaknesses:
//! - **Assumption Hunter**: Extracts implicit assumptions and questions them
//! - **Flaw Finder**: Categorizes weaknesses by severity and type
//! - **Skeptical Scorer**: Adjusts confidence downward based on issues found
//! - **Devil's Advocate**: Argues against the position to stress-test it
//!
//! ## Usage
//!
//! ```ignore
//! use reasonkit::thinktool::modules::{BrutalHonesty, ThinkToolContext};
//!
//! let module = BrutalHonesty::builder()
//!     .severity(CritiqueSeverity::Ruthless)
//!     .enable_devil_advocate(true)
//!     .build();
//!
//! let context = ThinkToolContext {
//!     query: "Our startup will succeed because we have the best team".to_string(),
//!     previous_steps: vec![],
//! };
//!
//! let result = module.execute(&context)?;
//! ```

use super::{ThinkToolContext, ThinkToolModule, ThinkToolModuleConfig, ThinkToolOutput};
use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use serde_json::json;

/// Severity level for critique analysis.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CritiqueSeverity {
    /// Gentle critique - focus on constructive feedback
    Gentle,
    /// Standard critique - balanced flaw detection
    #[default]
    Standard,
    /// Harsh critique - aggressive assumption challenging
    Harsh,
    /// Ruthless critique - no mercy, find every possible flaw
    Ruthless,
}

impl CritiqueSeverity {
    /// Get the skepticism multiplier for confidence scoring.
    /// Higher skepticism = lower confidence adjustments.
    fn skepticism_multiplier(&self) -> f64 {
        match self {
            Self::Gentle => 0.90,   // 10% skepticism reduction
            Self::Standard => 0.80, // 20% skepticism reduction
            Self::Harsh => 0.65,    // 35% skepticism reduction
            Self::Ruthless => 0.50, // 50% skepticism reduction
        }
    }
}

/// Category of detected flaw.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FlawCategory {
    /// Logical flaw (fallacy, contradiction, non-sequitur)
    Logical,
    /// Evidential flaw (missing data, weak sources, cherry-picking)
    Evidential,
    /// Assumption flaw (unexamined premises, hidden biases)
    Assumption,
    /// Scope flaw (overgeneralization, false dichotomy)
    Scope,
    /// Temporal flaw (recency bias, ignoring history)
    Temporal,
    /// Adversarial flaw (vulnerability to counter-arguments)
    Adversarial,
    /// Completeness flaw (missing considerations, blind spots)
    Completeness,
}

/// Severity of a detected flaw.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FlawSeverity {
    /// Minor issue - worth noting but not critical
    Minor,
    /// Moderate issue - should be addressed
    Moderate,
    /// Major issue - significantly weakens the argument
    Major,
    /// Critical issue - fundamentally undermines the position
    Critical,
}

impl FlawSeverity {
    /// Get the confidence penalty for this severity level.
    fn confidence_penalty(&self) -> f64 {
        match self {
            Self::Minor => 0.02,
            Self::Moderate => 0.08,
            Self::Major => 0.15,
            Self::Critical => 0.30,
        }
    }
}

/// A detected flaw in the reasoning.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetectedFlaw {
    /// Category of the flaw
    pub category: FlawCategory,
    /// Severity of the flaw
    pub severity: FlawSeverity,
    /// Description of the flaw
    pub description: String,
    /// The specific text or aspect that triggered this flaw
    pub trigger: Option<String>,
    /// Suggested remediation
    pub remediation: Option<String>,
}

impl DetectedFlaw {
    /// Create a new detected flaw.
    pub fn new(
        category: FlawCategory,
        severity: FlawSeverity,
        description: impl Into<String>,
    ) -> Self {
        Self {
            category,
            severity,
            description: description.into(),
            trigger: None,
            remediation: None,
        }
    }

    /// Add a trigger to the flaw.
    pub fn with_trigger(mut self, trigger: impl Into<String>) -> Self {
        self.trigger = Some(trigger.into());
        self
    }

    /// Add a remediation suggestion.
    pub fn with_remediation(mut self, remediation: impl Into<String>) -> Self {
        self.remediation = Some(remediation.into());
        self
    }
}

/// An implicit assumption detected in the reasoning.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImplicitAssumption {
    /// The assumption itself
    pub assumption: String,
    /// How confident we are this is an implicit assumption (0.0-1.0)
    pub confidence: f64,
    /// Why this assumption may be problematic
    pub risk: String,
    /// Whether this assumption is likely valid
    pub likely_valid: bool,
}

/// A strength identified in the reasoning.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IdentifiedStrength {
    /// Description of the strength
    pub description: String,
    /// How significant this strength is (0.0-1.0)
    pub significance: f64,
}

/// Overall verdict from the brutal honesty analysis.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CritiqueVerdict {
    /// The reasoning is solid and defensible
    Solid,
    /// The reasoning has merit but needs improvement
    Promising,
    /// The reasoning has significant issues
    Weak,
    /// The reasoning is fundamentally flawed
    Flawed,
    /// Cannot assess due to insufficient information
    Indeterminate,
}

/// Configuration for the BrutalHonesty module.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrutalHonestyConfig {
    /// Severity level for critique
    pub severity: CritiqueSeverity,
    /// Whether to enable devil's advocate mode
    pub enable_devil_advocate: bool,
    /// Whether to check for confirmation bias
    pub check_confirmation_bias: bool,
    /// Minimum confidence threshold (below this triggers warning)
    pub min_confidence_threshold: f64,
    /// Maximum number of flaws to report
    pub max_flaws_reported: usize,
    /// Focus areas for critique (empty = all areas)
    pub focus_areas: Vec<FlawCategory>,
}

impl Default for BrutalHonestyConfig {
    fn default() -> Self {
        Self {
            severity: CritiqueSeverity::Standard,
            enable_devil_advocate: true,
            check_confirmation_bias: true,
            min_confidence_threshold: 0.50,
            max_flaws_reported: 10,
            focus_areas: vec![],
        }
    }
}

/// Builder for BrutalHonesty module configuration.
#[derive(Debug, Default)]
pub struct BrutalHonestyBuilder {
    config: BrutalHonestyConfig,
}

impl BrutalHonestyBuilder {
    /// Create a new builder with default configuration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the critique severity level.
    pub fn severity(mut self, severity: CritiqueSeverity) -> Self {
        self.config.severity = severity;
        self
    }

    /// Enable or disable devil's advocate mode.
    pub fn enable_devil_advocate(mut self, enable: bool) -> Self {
        self.config.enable_devil_advocate = enable;
        self
    }

    /// Enable or disable confirmation bias checking.
    pub fn check_confirmation_bias(mut self, enable: bool) -> Self {
        self.config.check_confirmation_bias = enable;
        self
    }

    /// Set the minimum confidence threshold.
    pub fn min_confidence_threshold(mut self, threshold: f64) -> Self {
        self.config.min_confidence_threshold = threshold.clamp(0.0, 1.0);
        self
    }

    /// Set the maximum number of flaws to report.
    pub fn max_flaws_reported(mut self, max: usize) -> Self {
        self.config.max_flaws_reported = max;
        self
    }

    /// Set focus areas for critique.
    pub fn focus_areas(mut self, areas: Vec<FlawCategory>) -> Self {
        self.config.focus_areas = areas;
        self
    }

    /// Build the BrutalHonesty module.
    pub fn build(self) -> BrutalHonesty {
        BrutalHonesty::with_config(self.config)
    }
}

/// BrutalHonesty reasoning module for adversarial critique.
///
/// Attacks ideas to identify weaknesses, challenges assumptions,
/// and scores confidence with appropriate skepticism.
pub struct BrutalHonesty {
    /// Module base configuration
    module_config: ThinkToolModuleConfig,
    /// BrutalHonesty-specific configuration
    config: BrutalHonestyConfig,
}

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

impl BrutalHonesty {
    /// Create a new BrutalHonesty module with default configuration.
    pub fn new() -> Self {
        Self::with_config(BrutalHonestyConfig::default())
    }

    /// Create a BrutalHonesty module with custom configuration.
    pub fn with_config(config: BrutalHonestyConfig) -> Self {
        Self {
            module_config: ThinkToolModuleConfig {
                name: "BrutalHonesty".to_string(),
                version: "3.0.0".to_string(),
                description: "Adversarial self-critique with skeptical confidence scoring"
                    .to_string(),
                confidence_weight: 0.15,
            },
            config,
        }
    }

    /// Create a builder for customizing the module.
    pub fn builder() -> BrutalHonestyBuilder {
        BrutalHonestyBuilder::new()
    }

    /// Get the current configuration.
    pub fn brutal_config(&self) -> &BrutalHonestyConfig {
        &self.config
    }

    /// Analyze the input for implicit assumptions.
    fn extract_assumptions(&self, query: &str) -> Vec<ImplicitAssumption> {
        let mut assumptions = Vec::new();

        // Pattern-based assumption detection
        let assumption_patterns = [
            ("will", "Assumes future outcome is certain", 0.75),
            ("always", "Assumes universal applicability", 0.80),
            ("never", "Assumes absolute exclusion", 0.80),
            ("everyone", "Assumes universal agreement", 0.85),
            ("obvious", "Assumes shared understanding", 0.70),
            ("clearly", "Assumes self-evidence", 0.65),
            ("best", "Assumes optimal status without comparison", 0.60),
            ("only", "Assumes exclusivity", 0.70),
            ("must", "Assumes necessity without justification", 0.65),
            ("should", "Assumes normative position", 0.55),
            ("need", "Assumes requirement without evidence", 0.60),
            ("because", "May assume causation from correlation", 0.50),
        ];

        let query_lower = query.to_lowercase();

        for (pattern, risk, confidence) in assumption_patterns {
            if query_lower.contains(pattern) {
                assumptions.push(ImplicitAssumption {
                    assumption: format!(
                        "Use of '{}' implies unstated certainty or universality",
                        pattern
                    ),
                    confidence,
                    risk: risk.to_string(),
                    likely_valid: confidence < 0.65,
                });
            }
        }

        // Check for causal language without evidence
        if query_lower.contains("because")
            && !query_lower.contains("data")
            && !query_lower.contains("evidence")
            && !query_lower.contains("study")
            && !query_lower.contains("research")
        {
            assumptions.push(ImplicitAssumption {
                assumption: "Causal claim made without citing evidence".to_string(),
                confidence: 0.70,
                risk: "Causation may be assumed from correlation".to_string(),
                likely_valid: false,
            });
        }

        // Check for value judgments
        let value_words = ["good", "bad", "right", "wrong", "better", "worse"];
        for word in value_words {
            if query_lower.contains(word) {
                assumptions.push(ImplicitAssumption {
                    assumption: format!("Value judgment '{}' assumes shared moral framework", word),
                    confidence: 0.55,
                    risk: "Value judgments may not be universally shared".to_string(),
                    likely_valid: true, // Values can be valid but should be acknowledged
                });
                break; // Only report once
            }
        }

        assumptions
    }

    /// Detect flaws in the reasoning.
    fn detect_flaws(&self, query: &str, previous_steps: &[String]) -> Vec<DetectedFlaw> {
        let mut flaws = Vec::new();
        let query_lower = query.to_lowercase();
        let query_len = query.len();

        // Check for overgeneralization
        let universal_quantifiers = ["all", "every", "always", "never", "none", "no one"];
        for quantifier in universal_quantifiers {
            if query_lower.contains(quantifier) {
                flaws.push(
                    DetectedFlaw::new(
                        FlawCategory::Scope,
                        FlawSeverity::Moderate,
                        format!(
                            "Universal quantifier '{}' may indicate overgeneralization",
                            quantifier
                        ),
                    )
                    .with_remediation("Consider whether there are exceptions or edge cases"),
                );
            }
        }

        // Check for appeal to authority without specifics
        if (query_lower.contains("expert")
            || query_lower.contains("studies show")
            || query_lower.contains("research shows"))
            && !query_lower.contains("according to")
            && !query_lower.contains("published")
        {
            flaws.push(
                DetectedFlaw::new(
                    FlawCategory::Evidential,
                    FlawSeverity::Moderate,
                    "Vague appeal to authority without specific citation",
                )
                .with_remediation("Cite specific sources, authors, or publications"),
            );
        }

        // Check for false dichotomy
        if query_lower.contains("either") && query_lower.contains("or") {
            flaws.push(
                DetectedFlaw::new(
                    FlawCategory::Logical,
                    FlawSeverity::Moderate,
                    "Either/or construction may present false dichotomy",
                )
                .with_trigger("either...or")
                .with_remediation("Consider whether other alternatives exist"),
            );
        }

        // Check for recency bias
        if query_lower.contains("nowadays")
            || query_lower.contains("these days")
            || query_lower.contains("modern")
        {
            flaws.push(
                DetectedFlaw::new(
                    FlawCategory::Temporal,
                    FlawSeverity::Minor,
                    "Temporal framing may indicate recency bias",
                )
                .with_remediation("Consider historical patterns and whether 'new' means 'better'"),
            );
        }

        // Check for emotional language that may cloud reasoning
        let emotional_words = [
            "amazing",
            "terrible",
            "disaster",
            "revolutionary",
            "incredible",
            "horrible",
            "catastrophic",
            "miraculous",
            "devastating",
        ];
        for word in emotional_words {
            if query_lower.contains(word) {
                flaws.push(
                    DetectedFlaw::new(
                        FlawCategory::Logical,
                        FlawSeverity::Minor,
                        format!("Emotional language '{}' may indicate bias", word),
                    )
                    .with_trigger(word)
                    .with_remediation("Replace emotional terms with factual descriptions"),
                );
                break;
            }
        }

        // Check for lack of counter-arguments
        let counter_arg_indicators = [
            "however",
            "although",
            "but",
            "on the other hand",
            "conversely",
        ];
        let has_counter = counter_arg_indicators
            .iter()
            .any(|ind| query_lower.contains(ind));

        if !has_counter && query_len > 100 {
            flaws.push(
                DetectedFlaw::new(
                    FlawCategory::Completeness,
                    FlawSeverity::Major,
                    "No counter-arguments or alternative viewpoints presented",
                )
                .with_remediation("Steel-man opposing positions before dismissing them"),
            );
        }

        // Check for consistency with previous steps
        if !previous_steps.is_empty() {
            let prev_combined = previous_steps.join(" ").to_lowercase();

            // Look for potential contradictions
            if (query_lower.contains("not") && !prev_combined.contains("not"))
                || (!query_lower.contains("not") && prev_combined.contains("not "))
            {
                flaws.push(
                    DetectedFlaw::new(
                        FlawCategory::Logical,
                        FlawSeverity::Moderate,
                        "Potential inconsistency detected with previous reasoning steps",
                    )
                    .with_remediation("Review previous steps for logical consistency"),
                );
            }
        }

        // Check for vague claims
        let vague_indicators = ["somewhat", "kind of", "sort of", "basically", "essentially"];
        for vague in vague_indicators {
            if query_lower.contains(vague) {
                flaws.push(
                    DetectedFlaw::new(
                        FlawCategory::Evidential,
                        FlawSeverity::Minor,
                        format!("Vague qualifier '{}' reduces precision", vague),
                    )
                    .with_trigger(vague)
                    .with_remediation("Be more specific and precise in claims"),
                );
            }
        }

        // Check for confirmation bias indicators
        if self.config.check_confirmation_bias
            && query_lower.contains("proves")
            && !query_lower.contains("disproves")
        {
            flaws.push(
                DetectedFlaw::new(
                    FlawCategory::Assumption,
                    FlawSeverity::Moderate,
                    "One-sided evidence presentation may indicate confirmation bias",
                )
                .with_remediation("Actively seek disconfirming evidence"),
            );
        }

        // Limit flaws to configured maximum
        if flaws.len() > self.config.max_flaws_reported {
            // Sort by severity (most severe first) and take top N
            flaws.sort_by(|a, b| b.severity.cmp(&a.severity));
            flaws.truncate(self.config.max_flaws_reported);
        }

        flaws
    }

    /// Identify strengths in the reasoning.
    fn identify_strengths(&self, query: &str) -> Vec<IdentifiedStrength> {
        let mut strengths = Vec::new();
        let query_lower = query.to_lowercase();

        // Check for evidence-based reasoning
        if query_lower.contains("data")
            || query_lower.contains("evidence")
            || query_lower.contains("study")
            || query_lower.contains("research")
        {
            strengths.push(IdentifiedStrength {
                description: "References to data or evidence support claims".to_string(),
                significance: 0.75,
            });
        }

        // Check for nuanced language
        if query_lower.contains("however")
            || query_lower.contains("although")
            || query_lower.contains("on the other hand")
        {
            strengths.push(IdentifiedStrength {
                description: "Acknowledges counter-arguments or nuance".to_string(),
                significance: 0.70,
            });
        }

        // Check for specific examples
        if query_lower.contains("for example")
            || query_lower.contains("for instance")
            || query_lower.contains("specifically")
        {
            strengths.push(IdentifiedStrength {
                description: "Uses specific examples to support arguments".to_string(),
                significance: 0.65,
            });
        }

        // Check for qualified claims
        if query_lower.contains("likely")
            || query_lower.contains("probably")
            || query_lower.contains("may")
            || query_lower.contains("might")
        {
            strengths.push(IdentifiedStrength {
                description: "Uses appropriate epistemic qualifiers".to_string(),
                significance: 0.60,
            });
        }

        // Check for structured reasoning
        if query_lower.contains("first")
            || query_lower.contains("second")
            || query_lower.contains("finally")
            || query_lower.contains("therefore")
        {
            strengths.push(IdentifiedStrength {
                description: "Demonstrates structured reasoning approach".to_string(),
                significance: 0.55,
            });
        }

        strengths
    }

    /// Generate a devil's advocate counter-argument.
    fn devils_advocate(&self, query: &str) -> Option<String> {
        if !self.config.enable_devil_advocate {
            return None;
        }

        let query_lower = query.to_lowercase();

        // Generate contextual counter-arguments
        if query_lower.contains("will succeed") || query_lower.contains("will work") {
            Some("What if the underlying assumptions about market conditions, timing, or execution are wrong? What specific failure modes have been considered?".to_string())
        } else if query_lower.contains("best") {
            Some("By what criteria is 'best' defined? Have alternatives been fairly evaluated? Could 'best' be contingent on circumstances?".to_string())
        } else if query_lower.contains("everyone") || query_lower.contains("all") {
            Some("Are there exceptions or edge cases being overlooked? Is this universality actually validated by data?".to_string())
        } else if query_lower.contains("obvious") || query_lower.contains("clearly") {
            Some("What appears obvious from one perspective may not be from another. Have blind spots been systematically checked?".to_string())
        } else {
            Some("What is the strongest argument against this position? What would make this claim false?".to_string())
        }
    }

    /// Calculate skeptical confidence score.
    fn calculate_skeptical_confidence(
        &self,
        flaws: &[DetectedFlaw],
        strengths: &[IdentifiedStrength],
    ) -> f64 {
        // Start with base confidence
        let mut confidence = 0.75;

        // Apply flaw penalties
        for flaw in flaws {
            confidence -= flaw.severity.confidence_penalty();
        }

        // Apply strength bonuses (but with diminishing returns)
        let mut strength_bonus = 0.0;
        for (i, strength) in strengths.iter().enumerate() {
            // Diminishing returns: each subsequent strength adds less
            let diminish_factor = 1.0 / (1.0 + i as f64 * 0.5);
            strength_bonus += strength.significance * 0.1 * diminish_factor;
        }
        confidence += strength_bonus;

        // Apply severity-based skepticism multiplier
        confidence *= self.config.severity.skepticism_multiplier();

        // Clamp to valid range
        confidence.clamp(0.0, 0.95) // Never return 1.0 - always leave room for doubt
    }

    /// Determine the overall verdict.
    fn determine_verdict(&self, confidence: f64, flaws: &[DetectedFlaw]) -> CritiqueVerdict {
        // Count critical and major flaws
        let critical_count = flaws
            .iter()
            .filter(|f| f.severity == FlawSeverity::Critical)
            .count();
        let major_count = flaws
            .iter()
            .filter(|f| f.severity == FlawSeverity::Major)
            .count();

        if critical_count > 0 {
            return CritiqueVerdict::Flawed;
        }

        if major_count >= 3 || confidence < 0.30 {
            return CritiqueVerdict::Flawed;
        }

        if major_count >= 1 || confidence < 0.50 {
            return CritiqueVerdict::Weak;
        }

        if flaws.len() >= 3 || confidence < 0.70 {
            return CritiqueVerdict::Promising;
        }

        if confidence >= 0.70 && flaws.len() <= 2 {
            return CritiqueVerdict::Solid;
        }

        CritiqueVerdict::Promising
    }

    /// Generate the most critical fix recommendation.
    fn critical_fix(&self, flaws: &[DetectedFlaw]) -> Option<String> {
        // Find the most severe flaw
        flaws.iter().max_by_key(|f| &f.severity).and_then(|f| {
            f.remediation.clone().or_else(|| {
                Some(format!(
                    "Address {} issue: {}",
                    match f.category {
                        FlawCategory::Logical => "logical",
                        FlawCategory::Evidential => "evidential",
                        FlawCategory::Assumption => "assumption",
                        FlawCategory::Scope => "scope",
                        FlawCategory::Temporal => "temporal",
                        FlawCategory::Adversarial => "adversarial",
                        FlawCategory::Completeness => "completeness",
                    },
                    f.description
                ))
            })
        })
    }
}

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

    fn execute(&self, context: &ThinkToolContext) -> Result<ThinkToolOutput> {
        // Validate input
        if context.query.trim().is_empty() {
            return Err(Error::validation(
                "BrutalHonesty requires non-empty query input",
            ));
        }

        // Perform adversarial analysis
        let assumptions = self.extract_assumptions(&context.query);
        let flaws = self.detect_flaws(&context.query, &context.previous_steps);
        let strengths = self.identify_strengths(&context.query);
        let devils_advocate = self.devils_advocate(&context.query);

        // Calculate skeptical confidence
        let confidence = self.calculate_skeptical_confidence(&flaws, &strengths);

        // Determine verdict
        let verdict = self.determine_verdict(confidence, &flaws);

        // Get critical fix
        let critical_fix = self.critical_fix(&flaws);

        // Build confidence warning
        let confidence_warning = if confidence < self.config.min_confidence_threshold {
            Some(format!(
                "Confidence {:.0}% is below threshold {:.0}%",
                confidence * 100.0,
                self.config.min_confidence_threshold * 100.0
            ))
        } else {
            None
        };

        // Construct output
        let output = json!({
            "verdict": verdict,
            "confidence": confidence,
            "confidence_warning": confidence_warning,
            "severity_applied": format!("{:?}", self.config.severity),
            "analysis": {
                "assumptions": assumptions,
                "flaws": flaws,
                "strengths": strengths,
                "flaw_count": flaws.len(),
                "strength_count": strengths.len(),
            },
            "devils_advocate": devils_advocate,
            "critical_fix": critical_fix,
            "metadata": {
                "input_length": context.query.len(),
                "previous_steps_count": context.previous_steps.len(),
                "skepticism_multiplier": self.config.severity.skepticism_multiplier(),
            }
        });

        Ok(ThinkToolOutput {
            module: self.module_config.name.clone(),
            confidence,
            output,
        })
    }
}

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

    #[test]
    fn test_default_module() {
        let module = BrutalHonesty::new();
        assert_eq!(module.config().name, "BrutalHonesty");
        assert_eq!(module.config().version, "3.0.0");
        assert_eq!(module.brutal_config().severity, CritiqueSeverity::Standard);
    }

    #[test]
    fn test_builder_pattern() {
        let module = BrutalHonesty::builder()
            .severity(CritiqueSeverity::Ruthless)
            .enable_devil_advocate(false)
            .min_confidence_threshold(0.60)
            .build();

        assert_eq!(module.brutal_config().severity, CritiqueSeverity::Ruthless);
        assert!(!module.brutal_config().enable_devil_advocate);
        assert!((module.brutal_config().min_confidence_threshold - 0.60).abs() < 0.001);
    }

    #[test]
    fn test_assumption_extraction() {
        let module = BrutalHonesty::new();
        let assumptions = module.extract_assumptions("Our product will always be the best");

        assert!(!assumptions.is_empty());
        let has_will = assumptions.iter().any(|a| a.assumption.contains("will"));
        let has_always = assumptions.iter().any(|a| a.assumption.contains("always"));
        let has_best = assumptions.iter().any(|a| a.assumption.contains("best"));

        assert!(has_will || has_always || has_best);
    }

    #[test]
    fn test_flaw_detection() {
        let module = BrutalHonesty::new();
        let flaws = module.detect_flaws(
            "Either we succeed or we fail completely. All experts agree this is amazing.",
            &[],
        );

        assert!(!flaws.is_empty());

        // Should detect false dichotomy
        let has_dichotomy = flaws.iter().any(|f| f.category == FlawCategory::Logical);
        assert!(has_dichotomy);

        // Should detect overgeneralization
        let has_scope = flaws.iter().any(|f| f.category == FlawCategory::Scope);
        assert!(has_scope);
    }

    #[test]
    fn test_strength_identification() {
        let module = BrutalHonesty::new();
        let strengths = module.identify_strengths(
            "The data shows, for example, that our approach is likely effective. However, there are limitations.",
        );

        assert!(!strengths.is_empty());
        assert!(strengths.len() >= 2); // data + for example + however + likely
    }

    #[test]
    fn test_skeptical_confidence() {
        let module = BrutalHonesty::new();

        // With flaws, confidence should be reduced
        let flaws = vec![DetectedFlaw::new(
            FlawCategory::Logical,
            FlawSeverity::Major,
            "Test flaw",
        )];
        let confidence = module.calculate_skeptical_confidence(&flaws, &[]);
        assert!(confidence < 0.60); // Major flaw + skepticism should reduce significantly

        // No flaws with strengths should be higher
        let strengths = vec![IdentifiedStrength {
            description: "Test strength".to_string(),
            significance: 0.8,
        }];
        let confidence_with_strength = module.calculate_skeptical_confidence(&[], &strengths);
        assert!(confidence_with_strength > confidence);
    }

    #[test]
    fn test_verdict_determination() {
        let module = BrutalHonesty::new();

        // Critical flaw should result in Flawed verdict
        let flaws = vec![DetectedFlaw::new(
            FlawCategory::Logical,
            FlawSeverity::Critical,
            "Critical issue",
        )];
        let verdict = module.determine_verdict(0.5, &flaws);
        assert_eq!(verdict, CritiqueVerdict::Flawed);

        // No flaws with high confidence should be Solid
        let verdict = module.determine_verdict(0.85, &[]);
        assert_eq!(verdict, CritiqueVerdict::Solid);
    }

    #[test]
    fn test_execute_empty_input() {
        let module = BrutalHonesty::new();
        let context = ThinkToolContext {
            query: "".to_string(),
            previous_steps: vec![],
        };

        let result = module.execute(&context);
        assert!(result.is_err());
    }

    #[test]
    fn test_execute_valid_input() {
        let module = BrutalHonesty::new();
        let context = ThinkToolContext {
            query: "Our startup will succeed because we have the best team".to_string(),
            previous_steps: vec![],
        };

        let result = module.execute(&context).unwrap();
        assert_eq!(result.module, "BrutalHonesty");
        assert!(result.confidence > 0.0);
        assert!(result.confidence <= 0.95);

        // Check output structure
        let output = &result.output;
        assert!(output.get("verdict").is_some());
        assert!(output.get("analysis").is_some());
        assert!(output.get("devils_advocate").is_some());
    }

    #[test]
    fn test_severity_affects_confidence() {
        let gentle = BrutalHonesty::builder()
            .severity(CritiqueSeverity::Gentle)
            .build();
        let ruthless = BrutalHonesty::builder()
            .severity(CritiqueSeverity::Ruthless)
            .build();

        let context = ThinkToolContext {
            query: "This approach will work well".to_string(),
            previous_steps: vec![],
        };

        let gentle_result = gentle.execute(&context).unwrap();
        let ruthless_result = ruthless.execute(&context).unwrap();

        // Ruthless should have lower confidence
        assert!(ruthless_result.confidence < gentle_result.confidence);
    }

    #[test]
    fn test_devils_advocate() {
        let module = BrutalHonesty::new();

        // Should generate counter-argument
        let counter = module.devils_advocate("We will succeed");
        assert!(counter.is_some());
        assert!(counter.unwrap().contains("?"));

        // With devil's advocate disabled
        let no_devil = BrutalHonesty::builder()
            .enable_devil_advocate(false)
            .build();
        let counter = no_devil.devils_advocate("We will succeed");
        assert!(counter.is_none());
    }

    #[test]
    fn test_flaw_severity_ordering() {
        assert!(FlawSeverity::Critical > FlawSeverity::Major);
        assert!(FlawSeverity::Major > FlawSeverity::Moderate);
        assert!(FlawSeverity::Moderate > FlawSeverity::Minor);
    }

    #[test]
    fn test_max_flaws_limit() {
        let module = BrutalHonesty::builder().max_flaws_reported(2).build();

        // Input with many potential flaws
        let flaws = module.detect_flaws(
            "All experts always agree that this amazing product will never fail because it's obviously the best",
            &[],
        );

        assert!(flaws.len() <= 2);
    }
}