pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
#![cfg_attr(coverage_nightly, coverage(off))]
// Five Whys Root Cause Analyzer - Toyota Way Methodology
//
// GREEN PHASE: Minimal implementation to make tests pass
//
// Integrates with existing PMAT services:
// - Complexity analysis
// - SATD detection
// - Dead code detection
// - Git churn analysis
// - TDG scoring

use crate::models::debug_analysis::*;
use anyhow::{bail, Result};
use serde_json::json;
use std::path::Path;

/// Five Whys analyzer with PMAT tool integration
pub struct FiveWhysAnalyzer {
    // Services will be added as we integrate them
}

impl FiveWhysAnalyzer {
    pub fn new() -> Self {
        Self {}
    }

    /// Analyze an issue using Five Whys methodology
    ///
    /// # Arguments
    /// * `issue` - Description of the issue/symptom
    /// * `path` - Project path to analyze
    /// * `depth` - Number of "why" iterations (1-10)
    ///
    /// # Returns
    /// Complete debug analysis with root cause and recommendations
    pub async fn analyze(&self, issue: &str, path: &Path, depth: u8) -> Result<DebugAnalysis> {
        // Validation
        if issue.is_empty() {
            bail!("Issue description cannot be empty");
        }
        if depth == 0 || depth > 10 {
            bail!("Depth must be between 1 and 10, got {}", depth);
        }
        if !path.exists() {
            bail!("Path does not exist: {}", path.display());
        }

        let mut analysis = DebugAnalysis::new(issue.to_string());

        // Iterate through Why questions
        for i in 1..=depth {
            let why = self.iterate_why(issue, path, i, &analysis.whys).await?;

            // Early termination if high confidence reached (>0.9) after at least 3 iterations
            if i >= 3 && why.confidence > 0.9 {
                analysis.whys.push(why);
                break;
            }

            analysis.whys.push(why);
        }

        // Extract root cause from final Why
        analysis.root_cause = self.extract_root_cause(&analysis.whys)?;

        // Generate recommendations
        analysis.recommendations = self.generate_recommendations(
            &analysis.whys,
            &analysis.root_cause.clone().unwrap_or_default(),
        )?;

        // Summarize evidence
        analysis.evidence_summary = EvidenceSummary::from_whys(&analysis.whys);

        Ok(analysis)
    }

    /// Single Why iteration
    async fn iterate_why(
        &self,
        issue: &str,
        path: &Path,
        depth: u8,
        previous_whys: &[WhyIteration],
    ) -> Result<WhyIteration> {
        // Formulate question
        let question = self.formulate_question(issue, depth, previous_whys)?;

        // Gather evidence from PMAT services
        let evidence = self.gather_evidence(path).await?;

        // Generate hypothesis based on evidence
        let hypothesis = self.generate_hypothesis(&question, &evidence, depth)?;

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

        let mut why = WhyIteration::new(depth, question, hypothesis).with_confidence(confidence);

        why.evidence = evidence;

        Ok(why)
    }

    /// Formulate the "Why?" question for this iteration
    fn formulate_question(
        &self,
        issue: &str,
        depth: u8,
        previous_whys: &[WhyIteration],
    ) -> Result<String> {
        let question = if depth == 1 {
            format!("Why did this occur: {}?", issue)
        } else if let Some(prev) = previous_whys.last() {
            format!("Why {}?", prev.hypothesis.trim_end_matches('.'))
        } else {
            format!("Why did this occur (iteration {})?", depth)
        };

        Ok(question)
    }

    /// Gather evidence from real project data (v2 weights, PMAT-510).
    ///
    /// v2 evidence sources: Complexity (25%), SATD (20%), Git churn (15%),
    /// EvoScore trajectory (15%), Coverage delta (15%), Dead code (10%).
    /// TDG removed (redundant with complexity+churn).
    async fn gather_evidence(&self, path: &Path) -> Result<Vec<Evidence>> {
        let mut evidence = Vec::new();

        // Real SATD evidence: count TODO/FIXME/HACK/WORKAROUND in source
        if let Some(satd_ev) = Self::gather_satd_evidence(path) {
            evidence.push(satd_ev);
        }

        // Real Git churn evidence: count commits in last 30 days
        if let Some(churn_ev) = Self::gather_git_churn_evidence(path) {
            evidence.push(churn_ev);
        }

        // Real complexity evidence: count Rust source files and estimate complexity
        if let Some(cx_ev) = Self::gather_complexity_evidence(path) {
            evidence.push(cx_ev);
        }

        // EvoScore trajectory (CB-142): is the affected area improving or regressing?
        if let Some(evo_ev) = Self::gather_evoscore_evidence(path) {
            evidence.push(evo_ev);
        }

        // Coverage delta: did recent changes decrease test coverage?
        if let Some(cov_ev) = Self::gather_coverage_delta_evidence(path) {
            evidence.push(cov_ev);
        }

        Ok(evidence)
    }

    /// Count SATD markers (TODO, FIXME, HACK, WORKAROUND, XXX) in source files.
    fn gather_satd_evidence(path: &Path) -> Option<Evidence> {
        let src_dir = path.join("src");
        let dir = if src_dir.is_dir() { &src_dir } else { path };
        let count = Self::count_satd_markers(dir);
        let description = if count == 0 {
            "No SATD markers found — codebase is clean of admitted technical debt".to_string()
        } else {
            format!(
                "Found {} TODO/FIXME/HACK markers indicating known technical debt",
                count
            )
        };
        Some(Evidence::new(
            EvidenceSource::SATD,
            path.to_path_buf(),
            "todo_markers".to_string(),
            json!({"count": count}),
            description,
        ))
    }

    const SATD_EXTENSIONS: &'static [&'static str] =
        &["rs", "py", "ts", "js", "go", "lua", "c", "cpp", "java"];
    const SATD_MARKERS: &'static [&'static str] = &["TODO", "FIXME", "HACK", "WORKAROUND", "XXX"];

    fn count_satd_markers(dir: &Path) -> usize {
        let entries = match std::fs::read_dir(dir) {
            Ok(e) => e,
            Err(_) => return 0,
        };
        entries
            .flatten()
            .map(|entry| entry.path())
            .map(|p| {
                if p.is_dir() {
                    return Self::count_satd_markers(&p);
                }
                let is_source = p
                    .extension()
                    .and_then(|e| e.to_str())
                    .is_some_and(|e| Self::SATD_EXTENSIONS.contains(&e));
                if !is_source {
                    return 0;
                }
                std::fs::read_to_string(&p)
                    .unwrap_or_default()
                    .lines()
                    .filter(|line| Self::SATD_MARKERS.iter().any(|m| line.contains(m)))
                    .count()
            })
            .sum()
    }

    /// Count git commits in last 30 days.
    fn gather_git_churn_evidence(path: &Path) -> Option<Evidence> {
        let output = std::process::Command::new("git")
            .args(["rev-list", "--count", "--since=30.days", "HEAD"])
            .current_dir(path)
            .output()
            .ok()?;
        if !output.status.success() {
            return None;
        }
        let count: u64 = String::from_utf8_lossy(&output.stdout)
            .trim()
            .parse()
            .unwrap_or(0);
        let description = if count > 20 {
            format!(
                "High churn: {} commits in 30 days indicates active/unstable area",
                count
            )
        } else if count > 5 {
            format!("Moderate churn: {} commits in 30 days", count)
        } else {
            format!("Low churn: {} commits in 30 days — stable code", count)
        };
        Some(Evidence::new(
            EvidenceSource::GitChurn,
            path.to_path_buf(),
            "commit_count".to_string(),
            json!({"commit_count": count, "days": 30}),
            description,
        ))
    }

    // NOTE: gather_tdg_evidence removed in v2 (PMAT-510).
    // TDG weight set to 0% — redundant with complexity + churn.
    // EvidenceSource::TDG variant kept for backward compat (deserialization).

    /// Estimate complexity by counting Rust source lines and deeply-nested functions.
    fn gather_complexity_evidence(path: &Path) -> Option<Evidence> {
        let src_dir = path.join("src");
        if !src_dir.is_dir() {
            return None;
        }
        let (total_lines, deep_nesting_count) = Self::count_lines_and_nesting(&src_dir);
        let estimated_avg_complexity = if total_lines > 0 {
            // Rough heuristic: deep nesting count per 1000 lines
            (deep_nesting_count as f64 / total_lines as f64 * 1000.0).round() as u64
        } else {
            0
        };
        let description = format!(
            "{} source lines, {} deeply-nested blocks (est. complexity density: {}/1000 lines)",
            total_lines, deep_nesting_count, estimated_avg_complexity
        );
        Some(Evidence::new(
            EvidenceSource::Complexity,
            path.to_path_buf(),
            "estimated_complexity".to_string(),
            json!({"total_lines": total_lines, "deep_nesting": deep_nesting_count, "threshold": 20}),
            description,
        ))
    }

    /// Compute EvoScore trajectory from .pmat-metrics/ test data (CB-142).
    ///
    /// Uses the same gamma-weighted computation as `check_swe_ci_evoscore`.
    /// Returns None (neutral) if insufficient data (<3 commits).
    fn gather_evoscore_evidence(path: &Path) -> Option<Evidence> {
        let metrics_dir = path.join(".pmat-metrics");
        if !metrics_dir.exists() {
            return None;
        }

        // Collect commit test data files
        let mut test_files: Vec<std::path::PathBuf> = Vec::new();
        if let Ok(entries) = std::fs::read_dir(&metrics_dir) {
            for entry in entries.flatten() {
                let p = entry.path();
                if let Some(name) = p.file_name().and_then(|n| n.to_str()) {
                    if name.starts_with("commit-") && name.ends_with("-tests.json") {
                        test_files.push(p);
                    }
                }
            }
        }
        test_files.sort();

        let mut test_data: Vec<(u64, u64)> = Vec::new(); // (pass, total)
        for file_path in &test_files {
            if let Ok(content) = std::fs::read_to_string(file_path) {
                if let Ok(data) = serde_json::from_str::<serde_json::Value>(&content) {
                    let pass = data["pass"].as_u64().unwrap_or(0);
                    let total = data["total"].as_u64().unwrap_or(0);
                    if total > 0 {
                        test_data.push((pass, total));
                    }
                }
            }
        }

        // Need at least 3 commits for meaningful trajectory
        if test_data.len() < 3 {
            return None;
        }

        // Compute EvoScore with gamma = 1.5 (matches CB-142 comply check)
        let gamma: f64 = 1.5;
        let base_pass = test_data[0].0 as f64;
        let oracle_pass = test_data.iter().map(|(p, _)| *p).max().unwrap_or(0) as f64;

        let mut weighted_sum = 0.0;
        let mut weight_total = 0.0;

        for (i, (pass, _total)) in test_data.iter().enumerate().skip(1) {
            let current_pass = *pass as f64;
            let a_c = if current_pass >= base_pass {
                let gap = oracle_pass - base_pass;
                if gap > 0.0 {
                    (current_pass - base_pass) / gap
                } else {
                    1.0
                }
            } else if base_pass > 0.0 {
                (current_pass - base_pass) / base_pass
            } else {
                0.0
            };

            let weight = gamma.powi(i as i32);
            weighted_sum += weight * a_c;
            weight_total += weight;
        }

        let evoscore = if weight_total > 0.0 {
            weighted_sum / weight_total
        } else {
            0.0
        };

        let description = if evoscore >= 0.5 {
            format!(
                "Positive trajectory: EvoScore {:.3} — area is improving",
                evoscore
            )
        } else if evoscore >= 0.0 {
            format!(
                "Mixed trajectory: EvoScore {:.3} — some improvement, some regression",
                evoscore
            )
        } else {
            format!(
                "Negative trajectory: EvoScore {:.3} — area is regressing",
                evoscore
            )
        };

        Some(Evidence::new(
            EvidenceSource::EvoScoreTrajectory,
            path.to_path_buf(),
            "evoscore_trajectory".to_string(),
            json!({"evoscore": evoscore, "commits": test_data.len(), "gamma": gamma}),
            description,
        ))
    }

    /// Compute coverage delta from .pmat/coverage-cache.json.
    ///
    /// Reads cached coverage data and computes a simple coverage ratio.
    /// Returns None if no coverage data is available.
    fn gather_coverage_delta_evidence(path: &Path) -> Option<Evidence> {
        let cache_path = path.join(".pmat/coverage-cache.json");
        let content = std::fs::read_to_string(&cache_path).ok()?;
        let data: serde_json::Value = serde_json::from_str(&content).ok()?;

        let files = data.get("files")?.as_object()?;
        if files.is_empty() {
            return None;
        }

        // Compute aggregate coverage from line hit data
        let mut total_lines: usize = 0;
        let mut covered_lines: usize = 0;

        for (_file_path, line_hits) in files {
            if let Some(hits_map) = line_hits.as_object() {
                for (_line_no, hit_count) in hits_map {
                    total_lines += 1;
                    if hit_count.as_u64().unwrap_or(0) > 0 {
                        covered_lines += 1;
                    }
                }
            }
        }

        let coverage_pct = if total_lines > 0 {
            covered_lines as f64 / total_lines as f64 * 100.0
        } else {
            return None;
        };

        // Delta: compare against 85% baseline (industry standard target)
        // Positive delta = above target, negative = below target
        let delta = coverage_pct - 85.0;

        let description = if delta >= 0.0 {
            format!(
                "Coverage {:.1}% (delta +{:.1}% vs 85% baseline) — above target",
                coverage_pct, delta
            )
        } else {
            format!(
                "Coverage {:.1}% (delta {:.1}% vs 85% baseline) — below target",
                coverage_pct, delta
            )
        };

        Some(Evidence::new(
            EvidenceSource::CoverageDelta,
            path.to_path_buf(),
            "coverage_delta".to_string(),
            json!({"coverage_pct": coverage_pct, "delta": delta, "total_lines": total_lines, "covered_lines": covered_lines}),
            description,
        ))
    }

    fn count_lines_and_nesting(dir: &Path) -> (usize, usize) {
        let entries = match std::fs::read_dir(dir) {
            Ok(e) => e,
            Err(_) => return (0, 0),
        };
        entries
            .flatten()
            .map(|entry| entry.path())
            .fold((0usize, 0usize), |(lines, deep), p| {
                if p.is_dir() {
                    let (l, d) = Self::count_lines_and_nesting(&p);
                    return (lines + l, deep + d);
                }
                let is_rs = p.extension().and_then(|e| e.to_str()) == Some("rs");
                if !is_rs {
                    return (lines, deep);
                }
                let (l, d) = Self::count_file_nesting(&p);
                (lines + l, deep + d)
            })
    }

    fn count_file_nesting(path: &Path) -> (usize, usize) {
        let content = match std::fs::read_to_string(path) {
            Ok(c) => c,
            Err(_) => return (0, 0),
        };
        let mut brace_depth = 0i32;
        let mut deep = 0usize;
        let mut line_count = 0usize;
        for line in content.lines() {
            line_count += 1;
            brace_depth += line.matches('{').count() as i32;
            brace_depth -= line.matches('}').count() as i32;
            if brace_depth > 5 {
                deep += 1;
            }
        }
        (line_count, deep)
    }

    /// Generate hypothesis based on evidence
    fn generate_hypothesis(
        &self,
        _question: &str,
        evidence: &[Evidence],
        depth: u8,
    ) -> Result<String> {
        let signals = EvidenceSignals::from_evidence(evidence);
        Ok(signals.hypothesis_for_depth(depth))
    }
}

/// Extracted evidence signals to reduce cognitive complexity in hypothesis generation.
struct EvidenceSignals {
    high_complexity: bool,
    satd_present: bool,
    high_churn: bool,
    regressing_evoscore: bool,
    low_coverage: bool,
}

impl EvidenceSignals {
    fn from_evidence(evidence: &[Evidence]) -> Self {
        Self {
            high_complexity: evidence.iter().any(|e| {
                e.source == EvidenceSource::Complexity
                    && e.value
                        .get("deep_nesting")
                        .or_else(|| e.value.get("value"))
                        .and_then(|v| v.as_f64())
                        .unwrap_or(0.0)
                        > 20.0
            }),
            satd_present: evidence.iter().any(|e| e.source == EvidenceSource::SATD),
            high_churn: evidence.iter().any(|e| {
                e.source == EvidenceSource::GitChurn
                    && e.value
                        .get("commit_count")
                        .and_then(|v| v.as_u64())
                        .unwrap_or(0)
                        > 10
            }),
            regressing_evoscore: evidence.iter().any(|e| {
                e.source == EvidenceSource::EvoScoreTrajectory
                    && e.value
                        .get("evoscore")
                        .and_then(|v| v.as_f64())
                        .unwrap_or(0.0)
                        < 0.0
            }),
            low_coverage: evidence.iter().any(|e| {
                e.source == EvidenceSource::CoverageDelta
                    && e.value.get("delta").and_then(|v| v.as_f64()).unwrap_or(0.0) < 0.0
            }),
        }
    }

    fn hypothesis_for_depth(&self, depth: u8) -> String {
        match depth {
            1 => self.depth_1_hypothesis(),
            2 => self.depth_2_hypothesis(),
            3 => self.depth_3_hypothesis(),
            4 => "Requirements or constraints were not fully specified".to_string(),
            _ => "Root cause: Systematic process gap in development workflow".to_string(),
        }
    }

    fn depth_1_hypothesis(&self) -> String {
        if self.high_complexity {
            "Code complexity exceeds acceptable thresholds".to_string()
        } else if self.satd_present {
            "Known technical debt markers present in codebase".to_string()
        } else {
            "Issue manifested due to code quality factors".to_string()
        }
    }

    fn depth_2_hypothesis(&self) -> String {
        if self.low_coverage {
            "Insufficient test coverage allowed defect to slip through".to_string()
        } else if self.high_complexity {
            "Complex control flow makes code difficult to understand and maintain".to_string()
        } else {
            "Code structure contributed to the problem".to_string()
        }
    }

    fn depth_3_hypothesis(&self) -> String {
        if self.regressing_evoscore {
            "Quality trajectory is declining — area has been getting worse over time".to_string()
        } else if self.high_churn {
            "Frequent changes indicate unstable or poorly understood code".to_string()
        } else if self.satd_present {
            "Technical debt accumulated, indicating deferred maintenance".to_string()
        } else {
            "Architectural constraints led to current state".to_string()
        }
    }
}

impl FiveWhysAnalyzer {
    /// Calculate confidence score based on evidence strength (v2 weights, PMAT-510).
    ///
    /// v2 weights: Complexity 25%, SATD 20%, GitChurn 15%,
    /// EvoScoreTrajectory 15%, CoverageDelta 15%, DeadCode 10%.
    /// TDG weight removed (0%) — redundant with complexity+churn.
    pub fn calculate_confidence(&self, evidence: &[Evidence]) -> Result<f64> {
        if evidence.is_empty() {
            return Ok(0.3); // Low confidence with no evidence
        }

        let mut confidence = 0.0;
        let mut weight_sum = 0.0;

        for ev in evidence {
            let (evidence_weight, severity_multiplier) = match ev.source {
                EvidenceSource::Complexity => {
                    // Accept both "deep_nesting" (real evidence) and "value" (legacy/tests)
                    let metric = ev
                        .value
                        .get("deep_nesting")
                        .or_else(|| ev.value.get("value"))
                        .and_then(|v| v.as_f64())
                        .unwrap_or(0.0);
                    let threshold = ev
                        .value
                        .get("threshold")
                        .and_then(|v| v.as_f64())
                        .unwrap_or(20.0);
                    let severity = if threshold > 0.0 {
                        (metric - threshold).max(0.0) / threshold
                    } else {
                        0.0
                    };
                    (0.25, 1.0 + severity.min(1.0))
                }
                EvidenceSource::SATD => {
                    let count = ev.value.get("count").and_then(|v| v.as_u64()).unwrap_or(1);
                    let severity = (count as f64).min(10.0) / 10.0;
                    (0.20, 1.0 + severity)
                }
                // v2: TDG removed (redundant with complexity+churn). Weight = 0.
                EvidenceSource::TDG => (0.0, 1.0),
                EvidenceSource::GitChurn => {
                    let commits = ev
                        .value
                        .get("commit_count")
                        .and_then(|v| v.as_u64())
                        .unwrap_or(0);
                    let severity = (commits as f64).min(20.0) / 20.0;
                    (0.15, 1.0 + severity)
                }
                EvidenceSource::DeadCode => (0.10, 1.0),
                EvidenceSource::ManualInspection => (0.15, 1.0),
                EvidenceSource::EvoScoreTrajectory => {
                    let evoscore = ev
                        .value
                        .get("evoscore")
                        .and_then(|v| v.as_f64())
                        .unwrap_or(0.0);
                    // Negative evoscore = regressing = higher severity
                    // Positive evoscore = improving = lower severity
                    let severity = if evoscore < 0.0 {
                        1.0 + (-evoscore).min(1.0) // Regression amplifies confidence
                    } else {
                        1.0 // Improvement is neutral
                    };
                    (0.15, severity)
                }
                EvidenceSource::CoverageDelta => {
                    let delta = ev
                        .value
                        .get("delta")
                        .and_then(|v| v.as_f64())
                        .unwrap_or(0.0);
                    // Negative delta = below 85% baseline = higher severity
                    let severity = if delta < 0.0 {
                        1.0 + (-delta / 85.0).min(1.0) // Scale by baseline
                    } else {
                        1.0
                    };
                    (0.15, severity)
                }
            };

            confidence += evidence_weight * severity_multiplier;
            weight_sum += evidence_weight;
        }

        // Normalize and clamp
        let normalized = if weight_sum > 0.0 {
            (confidence / weight_sum).clamp(0.0, 1.0)
        } else {
            0.5
        };

        Ok(normalized)
    }

    /// Extract root cause from Why iterations
    fn extract_root_cause(&self, whys: &[WhyIteration]) -> Result<Option<String>> {
        if whys.is_empty() {
            return Ok(None);
        }

        // Root cause is the hypothesis from the final Why
        let last_why = whys.last().expect("internal error");
        Ok(Some(last_why.hypothesis.clone()))
    }

    /// Generate actionable recommendations (v2 evidence sources, PMAT-510)
    pub fn generate_recommendations(
        &self,
        whys: &[WhyIteration],
        root_cause: &str,
    ) -> Result<Vec<Recommendation>> {
        let mut recommendations = Vec::new();

        // Analyze evidence across all whys to generate recommendations
        let has_high_complexity = whys.iter().any(|w| {
            w.evidence.iter().any(|e| {
                e.source == EvidenceSource::Complexity
                    && e.value.get("value").and_then(|v| v.as_f64()).unwrap_or(0.0) > 20.0
            })
        });

        let has_satd = whys
            .iter()
            .any(|w| w.evidence.iter().any(|e| e.source == EvidenceSource::SATD));

        let has_high_churn = whys.iter().any(|w| {
            w.evidence.iter().any(|e| {
                e.source == EvidenceSource::GitChurn
                    && e.value
                        .get("commit_count")
                        .and_then(|v| v.as_u64())
                        .unwrap_or(0)
                        > 10
            })
        });

        let has_regressing_evoscore = whys.iter().any(|w| {
            w.evidence.iter().any(|e| {
                e.source == EvidenceSource::EvoScoreTrajectory
                    && e.value
                        .get("evoscore")
                        .and_then(|v| v.as_f64())
                        .unwrap_or(0.0)
                        < 0.0
            })
        });

        let has_low_coverage = whys.iter().any(|w| {
            w.evidence.iter().any(|e| {
                e.source == EvidenceSource::CoverageDelta
                    && e.value.get("delta").and_then(|v| v.as_f64()).unwrap_or(0.0) < 0.0
            })
        });

        // Generate recommendations based on evidence
        if has_high_complexity {
            recommendations.push(Recommendation::high(
                "Refactor complex functions to reduce cyclomatic complexity below 20".to_string(),
                None,
            ));
        }

        if has_satd {
            recommendations.push(Recommendation::high(
                "Resolve technical debt markers (TODO/FIXME) in next sprint".to_string(),
                None,
            ));
        }

        if has_low_coverage {
            recommendations.push(Recommendation::high(
                "Add comprehensive test coverage (target: >=85%) using EXTREME TDD".to_string(),
                None,
            ));
        }

        if has_regressing_evoscore {
            recommendations.push(Recommendation::high(
                "Quality trajectory is declining — investigate and reverse regression trend"
                    .to_string(),
                None,
            ));
        }

        if has_high_churn {
            recommendations.push(Recommendation::medium(
                "Stabilize frequently changed code through better design patterns".to_string(),
                None,
            ));
        }

        // Always add root cause fix recommendation
        recommendations.push(Recommendation::high(
            format!("Address root cause: {}", root_cause),
            None,
        ));

        // Add specification recommendation
        recommendations.push(Recommendation::medium(
            "Document requirements and constraints in specification".to_string(),
            None,
        ));

        Ok(recommendations)
    }
}

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

// Tests extracted to five_whys_analyzer_tests.rs for file health (CB-040).
include!("five_whys_analyzer_tests.rs");

// Design-by-contract specifications (Verus-style)
// #[requires(project_path.is_dir())]
// #[ensures(result.is_ok() ==> ret.len() > 0)]