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
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
//! Enhanced BrutalHonesty Module with Extended Analysis
//!
//! Advanced adversarial self-critique that builds upon the base BrutalHonesty
//! module with additional analysis capabilities:
//!
//! - **Multi-Cultural Bias Detection**: Identifies cultural assumptions
//! - **Cognitive Bias Catalog**: Checks for common cognitive biases
//! - **Argument Mapping**: Structures the argument for analysis
//! - **Steelmanning**: Constructs the strongest version of opposing arguments
//!
//! ## Design Philosophy
//!
//! The enhanced module provides deeper analysis for high-stakes decisions
//! where thorough adversarial review is critical. It is designed to be
//! used in conjunction with or as a replacement for the base module.
//!
//! ## Usage
//!
//! ```ignore
//! use reasonkit::thinktool::modules::{BrutalHonestyEnhanced, ThinkToolContext};
//!
//! let module = BrutalHonestyEnhanced::builder()
//!     .enable_cultural_analysis(true)
//!     .enable_steelmanning(true)
//!     .cognitive_bias_depth(CognitiveBiasDepth::Deep)
//!     .build();
//!
//! let context = ThinkToolContext {
//!     query: "We should expand to international markets immediately".to_string(),
//!     previous_steps: vec![],
//! };
//!
//! let result = module.execute(&context)?;
//! ```

use super::brutalhonesty::{
    BrutalHonesty, BrutalHonestyConfig, CritiqueSeverity, CritiqueVerdict, FlawSeverity,
};
use super::{ThinkToolContext, ThinkToolModule, ThinkToolModuleConfig, ThinkToolOutput};
use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use serde_json::json;

/// Depth of cognitive bias analysis.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CognitiveBiasDepth {
    /// Basic bias detection (5 most common biases)
    Basic,
    /// Standard bias detection (15 common biases)
    #[default]
    Standard,
    /// Deep bias detection (30+ biases with sub-categories)
    Deep,
}

/// A detected cognitive bias.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CognitiveBias {
    /// Name of the bias
    pub name: String,
    /// Category of the bias
    pub category: BiasCategory,
    /// Description of how this bias manifests
    pub manifestation: String,
    /// Confidence that this bias is present (0.0-1.0)
    pub confidence: f64,
    /// Debiasing strategy
    pub debiasing_strategy: String,
}

/// Category of cognitive bias.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BiasCategory {
    /// Decision-making biases (anchoring, availability, etc.)
    DecisionMaking,
    /// Social biases (in-group, halo effect, etc.)
    Social,
    /// Memory biases (hindsight, rosy retrospection, etc.)
    Memory,
    /// Probability biases (gambler's fallacy, base rate neglect, etc.)
    Probability,
    /// Self-related biases (overconfidence, self-serving, etc.)
    SelfRelated,
}

/// A cultural assumption detected in reasoning.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CulturalAssumption {
    /// The assumption
    pub assumption: String,
    /// Cultural context where this assumption holds
    pub context: String,
    /// Cultures where this assumption may not hold
    pub exceptions: Vec<String>,
    /// Risk of misapplication
    pub risk_level: FlawSeverity,
}

/// A steelmanned counter-argument.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SteelmanArgument {
    /// The strongest version of the opposing argument
    pub argument: String,
    /// Key premises of the steelmanned argument
    pub premises: Vec<String>,
    /// How this argument challenges the original position
    pub challenge: String,
    /// Strength of the steelmanned argument (0.0-1.0)
    pub strength: f64,
}

/// Structured argument map.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArgumentMap {
    /// Main claim being analyzed
    pub claim: String,
    /// Supporting premises
    pub premises: Vec<String>,
    /// Evidence cited
    pub evidence: Vec<String>,
    /// Warrants (logical connections)
    pub warrants: Vec<String>,
    /// Qualifiers (conditions or limitations)
    pub qualifiers: Vec<String>,
    /// Rebuttals (counter-arguments acknowledged)
    pub rebuttals: Vec<String>,
}

/// Configuration for the enhanced BrutalHonesty module.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnhancedConfig {
    /// Base BrutalHonesty configuration
    pub base_config: BrutalHonestyConfig,
    /// Enable cultural assumption analysis
    pub enable_cultural_analysis: bool,
    /// Enable steelmanning of counter-arguments
    pub enable_steelmanning: bool,
    /// Enable argument mapping
    pub enable_argument_mapping: bool,
    /// Depth of cognitive bias analysis
    pub cognitive_bias_depth: CognitiveBiasDepth,
    /// Target cultures for cultural analysis
    pub target_cultures: Vec<String>,
}

impl Default for EnhancedConfig {
    fn default() -> Self {
        Self {
            base_config: BrutalHonestyConfig::default(),
            enable_cultural_analysis: true,
            enable_steelmanning: true,
            enable_argument_mapping: true,
            cognitive_bias_depth: CognitiveBiasDepth::Standard,
            target_cultures: vec![
                "Western".to_string(),
                "East Asian".to_string(),
                "South Asian".to_string(),
                "Middle Eastern".to_string(),
                "Latin American".to_string(),
            ],
        }
    }
}

/// Builder for enhanced BrutalHonesty module.
#[derive(Debug, Default)]
pub struct EnhancedBuilder {
    config: EnhancedConfig,
}

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

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

    /// Enable or disable cultural analysis.
    pub fn enable_cultural_analysis(mut self, enable: bool) -> Self {
        self.config.enable_cultural_analysis = enable;
        self
    }

    /// Enable or disable steelmanning.
    pub fn enable_steelmanning(mut self, enable: bool) -> Self {
        self.config.enable_steelmanning = enable;
        self
    }

    /// Enable or disable argument mapping.
    pub fn enable_argument_mapping(mut self, enable: bool) -> Self {
        self.config.enable_argument_mapping = enable;
        self
    }

    /// Set cognitive bias analysis depth.
    pub fn cognitive_bias_depth(mut self, depth: CognitiveBiasDepth) -> Self {
        self.config.cognitive_bias_depth = depth;
        self
    }

    /// Set target cultures for cultural analysis.
    pub fn target_cultures(mut self, cultures: Vec<String>) -> Self {
        self.config.target_cultures = cultures;
        self
    }

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

/// Enhanced BrutalHonesty module with extended analysis.
pub struct BrutalHonestyEnhanced {
    /// Module base configuration
    module_config: ThinkToolModuleConfig,
    /// Enhanced configuration
    config: EnhancedConfig,
    /// Base BrutalHonesty module for core analysis
    base_module: BrutalHonesty,
}

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

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

    /// Create an enhanced module with custom configuration.
    pub fn with_config(config: EnhancedConfig) -> Self {
        let base_module = BrutalHonesty::with_config(config.base_config.clone());

        Self {
            module_config: ThinkToolModuleConfig {
                name: "BrutalHonestyEnhanced".to_string(),
                version: "3.0.0".to_string(),
                description:
                    "Enhanced adversarial critique with cognitive bias detection and steelmanning"
                        .to_string(),
                confidence_weight: 0.18, // Slightly higher weight due to deeper analysis
            },
            config,
            base_module,
        }
    }

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

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

    /// Detect cognitive biases in the reasoning.
    fn detect_cognitive_biases(&self, query: &str) -> Vec<CognitiveBias> {
        let mut biases = Vec::new();
        let query_lower = query.to_lowercase();

        // Define bias patterns based on depth
        let basic_biases = self.get_basic_biases(&query_lower);
        biases.extend(basic_biases);

        if matches!(
            self.config.cognitive_bias_depth,
            CognitiveBiasDepth::Standard | CognitiveBiasDepth::Deep
        ) {
            let standard_biases = self.get_standard_biases(&query_lower);
            biases.extend(standard_biases);
        }

        if matches!(self.config.cognitive_bias_depth, CognitiveBiasDepth::Deep) {
            let deep_biases = self.get_deep_biases(&query_lower);
            biases.extend(deep_biases);
        }

        biases
    }

    /// Get basic (most common) cognitive biases.
    fn get_basic_biases(&self, query: &str) -> Vec<CognitiveBias> {
        let mut biases = Vec::new();

        // Confirmation bias
        if query.contains("proves")
            || query.contains("confirms")
            || (query.contains("evidence") && !query.contains("counter"))
        {
            biases.push(CognitiveBias {
                name: "Confirmation Bias".to_string(),
                category: BiasCategory::DecisionMaking,
                manifestation: "Seeking or interpreting information to confirm existing beliefs"
                    .to_string(),
                confidence: 0.70,
                debiasing_strategy:
                    "Actively seek disconfirming evidence; assign someone to argue the opposite"
                        .to_string(),
            });
        }

        // Anchoring bias
        if query.contains("first") || query.contains("initial") || query.contains("started with") {
            biases.push(CognitiveBias {
                name: "Anchoring Bias".to_string(),
                category: BiasCategory::DecisionMaking,
                manifestation: "Over-relying on initial information as reference point".to_string(),
                confidence: 0.55,
                debiasing_strategy:
                    "Consider multiple starting points; recalculate from different anchors"
                        .to_string(),
            });
        }

        // Availability heuristic
        if query.contains("recently") || query.contains("just saw") || query.contains("in the news")
        {
            biases.push(CognitiveBias {
                name: "Availability Heuristic".to_string(),
                category: BiasCategory::Memory,
                manifestation: "Overweighting easily recalled examples".to_string(),
                confidence: 0.65,
                debiasing_strategy:
                    "Seek base rate statistics; consider examples from multiple time periods"
                        .to_string(),
            });
        }

        // Overconfidence
        if query.contains("certain") || query.contains("definitely") || query.contains("no doubt") {
            biases.push(CognitiveBias {
                name: "Overconfidence Bias".to_string(),
                category: BiasCategory::SelfRelated,
                manifestation: "Excessive confidence in own judgment or predictions".to_string(),
                confidence: 0.75,
                debiasing_strategy: "Create prediction intervals; track calibration over time"
                    .to_string(),
            });
        }

        // Sunk cost fallacy
        if query.contains("already invested")
            || query.contains("too far to stop")
            || query.contains("wasted if")
        {
            biases.push(CognitiveBias {
                name: "Sunk Cost Fallacy".to_string(),
                category: BiasCategory::DecisionMaking,
                manifestation: "Continuing due to past investment rather than future value"
                    .to_string(),
                confidence: 0.80,
                debiasing_strategy: "Evaluate decisions based only on future costs and benefits"
                    .to_string(),
            });
        }

        biases
    }

    /// Get standard cognitive biases.
    fn get_standard_biases(&self, query: &str) -> Vec<CognitiveBias> {
        let mut biases = Vec::new();

        // Hindsight bias
        if query.contains("should have known")
            || query.contains("was obvious")
            || query.contains("predictable")
        {
            biases.push(CognitiveBias {
                name: "Hindsight Bias".to_string(),
                category: BiasCategory::Memory,
                manifestation: "Believing past events were more predictable than they were"
                    .to_string(),
                confidence: 0.70,
                debiasing_strategy:
                    "Document predictions before outcomes; review original uncertainty".to_string(),
            });
        }

        // Bandwagon effect
        if query.contains("everyone") || query.contains("popular") || query.contains("trend") {
            biases.push(CognitiveBias {
                name: "Bandwagon Effect".to_string(),
                category: BiasCategory::Social,
                manifestation: "Following the crowd rather than independent analysis".to_string(),
                confidence: 0.60,
                debiasing_strategy: "Evaluate based on fundamentals; consider contrarian positions"
                    .to_string(),
            });
        }

        // Status quo bias
        if query.contains("always done") || query.contains("traditional") || query.contains("usual")
        {
            biases.push(CognitiveBias {
                name: "Status Quo Bias".to_string(),
                category: BiasCategory::DecisionMaking,
                manifestation: "Preferring current state regardless of merit".to_string(),
                confidence: 0.65,
                debiasing_strategy: "Explicitly compare status quo costs vs change benefits"
                    .to_string(),
            });
        }

        // Fundamental attribution error
        if query.contains("they failed because they") || query.contains("their fault") {
            biases.push(CognitiveBias {
                name: "Fundamental Attribution Error".to_string(),
                category: BiasCategory::Social,
                manifestation: "Over-attributing behavior to personality vs situation".to_string(),
                confidence: 0.55,
                debiasing_strategy:
                    "Consider situational factors; ask 'what would I do in their position?'"
                        .to_string(),
            });
        }

        // Optimism bias
        if query.contains("will succeed")
            || query.contains("bound to")
            || query.contains("can't fail")
        {
            biases.push(CognitiveBias {
                name: "Optimism Bias".to_string(),
                category: BiasCategory::SelfRelated,
                manifestation: "Overestimating likelihood of positive outcomes".to_string(),
                confidence: 0.70,
                debiasing_strategy: "Conduct pre-mortem analysis; use reference class forecasting"
                    .to_string(),
            });
        }

        // Framing effect
        if query.contains("savings") || query.contains("loss") || query.contains("gain") {
            biases.push(CognitiveBias {
                name: "Framing Effect".to_string(),
                category: BiasCategory::DecisionMaking,
                manifestation: "Decision influenced by how information is presented".to_string(),
                confidence: 0.50,
                debiasing_strategy: "Reframe the problem multiple ways; use absolute numbers"
                    .to_string(),
            });
        }

        biases
    }

    /// Get deep cognitive biases.
    fn get_deep_biases(&self, query: &str) -> Vec<CognitiveBias> {
        let mut biases = Vec::new();

        // Planning fallacy
        if query.contains("estimate") || query.contains("timeline") || query.contains("schedule") {
            biases.push(CognitiveBias {
                name: "Planning Fallacy".to_string(),
                category: BiasCategory::Probability,
                manifestation: "Underestimating time, costs, and risks".to_string(),
                confidence: 0.75,
                debiasing_strategy: "Use reference class forecasting; add buffer based on past projects".to_string(),
            });
        }

        // Survivorship bias
        if query.contains("successful companies")
            || query.contains("winners")
            || query.contains("examples of success")
        {
            biases.push(CognitiveBias {
                name: "Survivorship Bias".to_string(),
                category: BiasCategory::Probability,
                manifestation: "Focusing on survivors while ignoring failures".to_string(),
                confidence: 0.65,
                debiasing_strategy: "Study failures; consider base rates of success vs failure"
                    .to_string(),
            });
        }

        // Narrative fallacy
        if query.contains("story") || query.contains("journey") || query.contains("narrative") {
            biases.push(CognitiveBias {
                name: "Narrative Fallacy".to_string(),
                category: BiasCategory::Memory,
                manifestation: "Creating false causal chains in retrospect".to_string(),
                confidence: 0.55,
                debiasing_strategy: "Focus on data; be skeptical of neat explanations".to_string(),
            });
        }

        // Dunning-Kruger effect
        if query.contains("simple") || query.contains("easy") || query.contains("just need to") {
            biases.push(CognitiveBias {
                name: "Dunning-Kruger Effect".to_string(),
                category: BiasCategory::SelfRelated,
                manifestation: "Overestimating competence in areas of limited knowledge"
                    .to_string(),
                confidence: 0.50,
                debiasing_strategy: "Consult domain experts; acknowledge knowledge gaps"
                    .to_string(),
            });
        }

        // Affect heuristic
        if query.contains("feel") || query.contains("gut") || query.contains("intuition") {
            biases.push(CognitiveBias {
                name: "Affect Heuristic".to_string(),
                category: BiasCategory::DecisionMaking,
                manifestation: "Letting emotions drive risk/benefit judgments".to_string(),
                confidence: 0.60,
                debiasing_strategy: "Separate emotional response from analytical evaluation"
                    .to_string(),
            });
        }

        biases
    }

    /// Detect cultural assumptions in the reasoning.
    fn detect_cultural_assumptions(&self, query: &str) -> Vec<CulturalAssumption> {
        if !self.config.enable_cultural_analysis {
            return Vec::new();
        }

        let mut assumptions = Vec::new();
        let query_lower = query.to_lowercase();

        // Individualism vs Collectivism
        if query_lower.contains("individual")
            || query_lower.contains("personal achievement")
            || query_lower.contains("self-made")
        {
            assumptions.push(CulturalAssumption {
                assumption: "Individual achievement and autonomy are primary values".to_string(),
                context: "Western, particularly American, cultural context".to_string(),
                exceptions: vec![
                    "East Asian cultures (group harmony)".to_string(),
                    "Latin American cultures (family ties)".to_string(),
                    "African cultures (community focus)".to_string(),
                ],
                risk_level: FlawSeverity::Moderate,
            });
        }

        // Direct communication assumption
        if query_lower.contains("straightforward")
            || query_lower.contains("direct")
            || query_lower.contains("clear communication")
        {
            assumptions.push(CulturalAssumption {
                assumption: "Direct, explicit communication is preferable".to_string(),
                context: "Low-context cultures (Northern European, American)".to_string(),
                exceptions: vec![
                    "High-context cultures (Japan, China)".to_string(),
                    "Middle Eastern cultures".to_string(),
                    "Many Asian cultures".to_string(),
                ],
                risk_level: FlawSeverity::Minor,
            });
        }

        // Time orientation
        if query_lower.contains("deadline")
            || query_lower.contains("punctual")
            || query_lower.contains("time is money")
        {
            assumptions.push(CulturalAssumption {
                assumption: "Linear, monochronic view of time".to_string(),
                context: "Northern European, North American cultures".to_string(),
                exceptions: vec![
                    "Polychronic cultures (Mediterranean, Latin America)".to_string(),
                    "Relationship-focused cultures".to_string(),
                ],
                risk_level: FlawSeverity::Minor,
            });
        }

        // Hierarchical assumptions
        if query_lower.contains("flat organization")
            || query_lower.contains("anyone can")
            || query_lower.contains("speak up")
        {
            assumptions.push(CulturalAssumption {
                assumption: "Low power distance is desirable".to_string(),
                context: "Scandinavian, Dutch, American cultures".to_string(),
                exceptions: vec![
                    "High power distance cultures (Japan, India)".to_string(),
                    "Traditional hierarchical societies".to_string(),
                ],
                risk_level: FlawSeverity::Moderate,
            });
        }

        assumptions
    }

    /// Generate a steelmanned counter-argument.
    fn generate_steelman(&self, query: &str) -> Option<SteelmanArgument> {
        if !self.config.enable_steelmanning {
            return None;
        }

        let query_lower = query.to_lowercase();

        // Generate contextual steelman arguments
        if query_lower.contains("should")
            || query_lower.contains("must")
            || query_lower.contains("need to")
        {
            Some(SteelmanArgument {
                argument: "The strongest case against this action is that the opportunity costs and risks may outweigh the benefits, and that the current state, while imperfect, has proven stability.".to_string(),
                premises: vec![
                    "Change carries inherent risk and transition costs".to_string(),
                    "Status quo has survived past challenges".to_string(),
                    "Resources spent here cannot be used elsewhere".to_string(),
                ],
                challenge: "This challenges whether the proposed action is truly necessary or optimal given alternatives".to_string(),
                strength: 0.70,
            })
        } else if query_lower.contains("will succeed") || query_lower.contains("will work") {
            Some(SteelmanArgument {
                argument: "Even well-planned initiatives with strong teams fail due to unforeseeable market shifts, timing issues, or execution challenges that compound unexpectedly.".to_string(),
                premises: vec![
                    "Base rates of success are often lower than expected".to_string(),
                    "External factors can override internal capabilities".to_string(),
                    "Complex systems have emergent failure modes".to_string(),
                ],
                challenge: "This challenges the assumption that good inputs guarantee good outcomes".to_string(),
                strength: 0.75,
            })
        } else if query_lower.contains("better") || query_lower.contains("best") {
            Some(SteelmanArgument {
                argument: "What is 'better' depends on criteria that may differ across stakeholders, time horizons, and contexts. The supposedly inferior alternative may optimize for different, equally valid goals.".to_string(),
                premises: vec![
                    "Value judgments depend on evaluation criteria".to_string(),
                    "Stakeholders have different preferences".to_string(),
                    "Trade-offs are inherent in most choices".to_string(),
                ],
                challenge: "This challenges whether the comparison criteria are appropriate and complete".to_string(),
                strength: 0.65,
            })
        } else {
            Some(SteelmanArgument {
                argument: "A sophisticated opponent would argue that this position overlooks key factors, relies on assumptions that may not hold, and underestimates alternatives.".to_string(),
                premises: vec![
                    "Complex issues have multiple valid perspectives".to_string(),
                    "Our information may be incomplete".to_string(),
                    "Intelligent people disagree for good reasons".to_string(),
                ],
                challenge: "This challenges the completeness and robustness of the original argument".to_string(),
                strength: 0.60,
            })
        }
    }

    /// Build an argument map from the input.
    fn build_argument_map(&self, query: &str) -> Option<ArgumentMap> {
        if !self.config.enable_argument_mapping {
            return None;
        }

        let sentences: Vec<&str> = query.split(['.', '!', '?']).collect();
        let query_lower = query.to_lowercase();

        // Extract claim (usually first substantial sentence or sentence with "should/must/will")
        let claim = sentences
            .iter()
            .find(|s| {
                let lower = s.to_lowercase();
                lower.contains("should")
                    || lower.contains("must")
                    || lower.contains("will")
                    || lower.contains("is the")
                    || lower.contains("are the")
            })
            .unwrap_or(sentences.first().unwrap_or(&""))
            .trim()
            .to_string();

        // Extract premises (sentences with "because", "since", "as")
        let premises: Vec<String> = sentences
            .iter()
            .filter(|s| {
                let lower = s.to_lowercase();
                lower.contains("because") || lower.contains("since") || lower.contains(" as ")
            })
            .map(|s| s.trim().to_string())
            .collect();

        // Extract evidence references
        let evidence: Vec<String> = sentences
            .iter()
            .filter(|s| {
                let lower = s.to_lowercase();
                lower.contains("data")
                    || lower.contains("study")
                    || lower.contains("research")
                    || lower.contains("evidence")
                    || lower.contains("according to")
            })
            .map(|s| s.trim().to_string())
            .collect();

        // Extract qualifiers
        let qualifiers: Vec<String> = sentences
            .iter()
            .filter(|s| {
                let lower = s.to_lowercase();
                lower.contains("unless")
                    || lower.contains("except")
                    || lower.contains("if")
                    || lower.contains("when")
                    || lower.contains("provided")
            })
            .map(|s| s.trim().to_string())
            .collect();

        // Extract rebuttals (acknowledged counter-arguments)
        let rebuttals: Vec<String> = sentences
            .iter()
            .filter(|s| {
                let lower = s.to_lowercase();
                lower.contains("however")
                    || lower.contains("although")
                    || lower.contains("but")
                    || lower.contains("despite")
                    || lower.contains("critics")
            })
            .map(|s| s.trim().to_string())
            .collect();

        // Extract warrants (logical connections)
        let warrants: Vec<String> = if query_lower.contains("therefore")
            || query_lower.contains("thus")
            || query_lower.contains("hence")
        {
            vec!["Explicit logical connection present".to_string()]
        } else if premises.is_empty() {
            vec!["Implicit warrant: premises assumed to support claim".to_string()]
        } else {
            vec![]
        };

        Some(ArgumentMap {
            claim,
            premises,
            evidence,
            warrants,
            qualifiers,
            rebuttals,
        })
    }

    /// Calculate enhanced confidence incorporating all analyses.
    fn calculate_enhanced_confidence(
        &self,
        base_confidence: f64,
        biases: &[CognitiveBias],
        cultural_assumptions: &[CulturalAssumption],
    ) -> f64 {
        let mut confidence = base_confidence;

        // Apply cognitive bias penalties
        for bias in biases {
            confidence -= bias.confidence * 0.05;
        }

        // Apply cultural assumption penalties
        for assumption in cultural_assumptions {
            match assumption.risk_level {
                FlawSeverity::Critical => confidence -= 0.10,
                FlawSeverity::Major => confidence -= 0.06,
                FlawSeverity::Moderate => confidence -= 0.03,
                FlawSeverity::Minor => confidence -= 0.01,
            }
        }

        confidence.clamp(0.0, 0.90) // Slightly lower max than base due to deeper skepticism
    }
}

impl ThinkToolModule for BrutalHonestyEnhanced {
    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(
                "BrutalHonestyEnhanced requires non-empty query input",
            ));
        }

        // Run base BrutalHonesty analysis
        let base_result = self.base_module.execute(context)?;
        let base_output = &base_result.output;

        // Extract base analysis results
        let base_analysis = base_output.get("analysis").cloned().unwrap_or(json!({}));
        let base_verdict = base_output
            .get("verdict")
            .cloned()
            .unwrap_or(json!("indeterminate"));
        let devils_advocate = base_output.get("devils_advocate").cloned();
        let critical_fix = base_output.get("critical_fix").cloned();

        // Perform enhanced analyses
        let cognitive_biases = self.detect_cognitive_biases(&context.query);
        let cultural_assumptions = self.detect_cultural_assumptions(&context.query);
        let steelman = self.generate_steelman(&context.query);
        let argument_map = self.build_argument_map(&context.query);

        // Calculate enhanced confidence
        let enhanced_confidence = self.calculate_enhanced_confidence(
            base_result.confidence,
            &cognitive_biases,
            &cultural_assumptions,
        );

        // Determine enhanced verdict
        let enhanced_verdict = if cognitive_biases.len() >= 3 || cultural_assumptions.len() >= 2 {
            CritiqueVerdict::Weak
        } else {
            // Parse base verdict and potentially downgrade
            match base_verdict.as_str() {
                Some("solid") if !cognitive_biases.is_empty() => CritiqueVerdict::Promising,
                Some("solid") => CritiqueVerdict::Solid,
                Some("promising") => CritiqueVerdict::Promising,
                Some("weak") => CritiqueVerdict::Weak,
                Some("flawed") => CritiqueVerdict::Flawed,
                _ => CritiqueVerdict::Indeterminate,
            }
        };

        // Build output
        let output = json!({
            "verdict": enhanced_verdict,
            "confidence": enhanced_confidence,
            "base_analysis": base_analysis,
            "base_verdict": base_verdict,
            "enhanced_analysis": {
                "cognitive_biases": cognitive_biases,
                "bias_count": cognitive_biases.len(),
                "cultural_assumptions": cultural_assumptions,
                "cultural_assumption_count": cultural_assumptions.len(),
                "steelman": steelman,
                "argument_map": argument_map,
            },
            "devils_advocate": devils_advocate,
            "critical_fix": critical_fix,
            "metadata": {
                "input_length": context.query.len(),
                "previous_steps_count": context.previous_steps.len(),
                "analysis_depth": format!("{:?}", self.config.cognitive_bias_depth),
                "cultural_analysis_enabled": self.config.enable_cultural_analysis,
                "steelmanning_enabled": self.config.enable_steelmanning,
            }
        });

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

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

    #[test]
    fn test_default_enhanced_module() {
        let module = BrutalHonestyEnhanced::new();
        assert_eq!(module.config().name, "BrutalHonestyEnhanced");
        assert_eq!(module.config().version, "3.0.0");
        assert!(module.enhanced_config().enable_cultural_analysis);
        assert!(module.enhanced_config().enable_steelmanning);
    }

    #[test]
    fn test_builder_pattern() {
        let module = BrutalHonestyEnhanced::builder()
            .severity(CritiqueSeverity::Ruthless)
            .enable_cultural_analysis(false)
            .cognitive_bias_depth(CognitiveBiasDepth::Deep)
            .build();

        assert_eq!(
            module.enhanced_config().base_config.severity,
            CritiqueSeverity::Ruthless
        );
        assert!(!module.enhanced_config().enable_cultural_analysis);
        assert_eq!(
            module.enhanced_config().cognitive_bias_depth,
            CognitiveBiasDepth::Deep
        );
    }

    #[test]
    fn test_cognitive_bias_detection() {
        let module = BrutalHonestyEnhanced::new();
        let biases = module.detect_cognitive_biases(
            "I'm certain this will succeed. We've already invested too much to stop.",
        );

        assert!(!biases.is_empty());

        // Should detect overconfidence
        let has_overconfidence = biases.iter().any(|b| b.name == "Overconfidence Bias");
        assert!(has_overconfidence);

        // Should detect sunk cost fallacy
        let has_sunk_cost = biases.iter().any(|b| b.name == "Sunk Cost Fallacy");
        assert!(has_sunk_cost);
    }

    #[test]
    fn test_cultural_assumption_detection() {
        let module = BrutalHonestyEnhanced::new();
        let assumptions = module.detect_cultural_assumptions(
            "We need direct communication and individual achievement focus.",
        );

        assert!(!assumptions.is_empty());
    }

    #[test]
    fn test_steelman_generation() {
        let module = BrutalHonestyEnhanced::new();
        let steelman = module.generate_steelman("We should expand immediately");

        assert!(steelman.is_some());
        let s = steelman.unwrap();
        assert!(!s.argument.is_empty());
        assert!(!s.premises.is_empty());
    }

    #[test]
    fn test_argument_mapping() {
        let module = BrutalHonestyEnhanced::new();
        let map = module.build_argument_map(
            "We should launch now because the market is ready. Data shows strong demand. However, there are risks.",
        );

        assert!(map.is_some());
        let m = map.unwrap();
        assert!(!m.claim.is_empty());
    }

    #[test]
    fn test_execute_valid_input() {
        let module = BrutalHonestyEnhanced::new();
        let context = ThinkToolContext {
            query:
                "We're certain this will succeed because everyone agrees it's the best approach."
                    .to_string(),
            previous_steps: vec![],
        };

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

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

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

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

    #[test]
    fn test_bias_depth_affects_detection() {
        let basic = BrutalHonestyEnhanced::builder()
            .cognitive_bias_depth(CognitiveBiasDepth::Basic)
            .build();
        let deep = BrutalHonestyEnhanced::builder()
            .cognitive_bias_depth(CognitiveBiasDepth::Deep)
            .build();

        let query =
            "I'm certain this simple plan with a clear timeline will succeed. Everyone agrees.";
        let basic_biases = basic.detect_cognitive_biases(query);
        let deep_biases = deep.detect_cognitive_biases(query);

        // Deep analysis should find more biases
        assert!(deep_biases.len() >= basic_biases.len());
    }

    #[test]
    fn test_enhanced_confidence_reduction() {
        let module = BrutalHonestyEnhanced::new();

        // With biases and cultural assumptions, confidence should be lower
        let biases = vec![CognitiveBias {
            name: "Test Bias".to_string(),
            category: BiasCategory::DecisionMaking,
            manifestation: "Test".to_string(),
            confidence: 0.8,
            debiasing_strategy: "Test".to_string(),
        }];
        let assumptions = vec![CulturalAssumption {
            assumption: "Test".to_string(),
            context: "Test".to_string(),
            exceptions: vec![],
            risk_level: FlawSeverity::Major,
        }];

        let confidence = module.calculate_enhanced_confidence(0.70, &biases, &assumptions);
        assert!(confidence < 0.70);
    }
}