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
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
#![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 {
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
/// Create a new instance.
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
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
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(issue, 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, issue: &str, path: &Path) -> Result<Vec<Evidence>> {
let mut evidence = Vec::new();
// Evidence about the reported issue. Everything below this line is a
// repo-wide metric that is identical whatever the issue was, so this is
// the only source that makes the analysis about the question asked.
if let Some(loc_ev) = Self::gather_issue_location_evidence(issue, path) {
evidence.push(loc_ev);
}
// 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"];
/// Words too common to identify anything, dropped from issue terms.
const ISSUE_STOPWORDS: &'static [&'static str] = &[
"the", "this", "that", "with", "when", "from", "into", "have", "does", "than", "then",
"them", "they", "there", "which", "while", "will", "would", "could", "should", "been",
"being", "before", "after", "because", "always", "never", "error", "fails", "failed",
"failure", "issue", "problem", "broken", "wrong", "silent", "silently", "reported",
"returns", "return", "value", "values", "code", "test", "tests",
];
/// Most matching locations to report; enough to orient, few enough to read.
const MAX_ISSUE_LOCATIONS: usize = 12;
/// Highest confidence an analysis may claim when it never located the issue.
///
/// Repo-wide metrics describe the repository, not the defect. Collecting
/// more of them must not raise confidence in a causal claim about a
/// specific issue.
pub const NO_ISSUE_EVIDENCE_CEILING: f64 = 0.35;
/// Did any evidence actually pertain to the reported issue?
fn has_issue_evidence(evidence: &[Evidence]) -> bool {
evidence
.iter()
.any(|e| e.source == EvidenceSource::IssueLocation)
}
/// Distinctive terms from the issue text, used to locate relevant source.
///
/// Keeps tokens of 4+ characters that are not stopwords, so
/// "MCP stdio server drops responses when stdin reaches EOF" yields
/// `stdio`, `server`, `drops`, `responses`, `stdin`, `reaches` — terms that
/// can actually be found in code.
fn issue_terms(issue: &str) -> Vec<String> {
let mut terms: Vec<String> = issue
.split(|c: char| !(c.is_alphanumeric() || c == '_'))
.filter(|t| t.len() >= 4)
.map(str::to_lowercase)
.filter(|t| !Self::ISSUE_STOPWORDS.contains(&t.as_str()))
.collect();
terms.sort();
terms.dedup();
terms
}
/// Locate source lines mentioning the issue's distinctive terms.
///
/// This is the only evidence source tied to the *reported issue*; every
/// other one is a repo-wide metric identical for any input. Before this
/// existed, asking about an EOF race in the MCP transport produced the same
/// four repo metrics as asking about anything else, and the analysis
/// concluded "Frequent changes indicate unstable or poorly understood code"
/// at 100% confidence — a statement about the repository, not the defect
/// (GH #637).
///
/// Returns `None` when nothing matches, which callers must treat as "the
/// issue was not located" rather than as an absence of problems.
fn gather_issue_location_evidence(issue: &str, path: &Path) -> Option<Evidence> {
let terms = Self::issue_terms(issue);
if terms.is_empty() {
return None;
}
let src_dir = path.join("src");
let dir = if src_dir.is_dir() { &src_dir } else { path };
let mut hits = Vec::new();
Self::collect_term_matches(dir, &terms, &mut hits);
if hits.is_empty() {
return None;
}
// Rank by how many distinct issue terms a file matches: a file
// mentioning several of them is likelier to be the subject.
hits.sort_by(|a, b| b.2.cmp(&a.2).then_with(|| a.0.cmp(&b.0)));
hits.truncate(Self::MAX_ISSUE_LOCATIONS);
let locations: Vec<serde_json::Value> = hits
.iter()
.map(|(file, line, score, term)| {
json!({"file": file, "line": line, "terms_matched": score, "term": term})
})
.collect();
let top = hits
.iter()
.take(3)
.map(|(f, l, _, _)| format!("{f}:{l}"))
.collect::<Vec<_>>()
.join(", ");
Some(Evidence::new(
EvidenceSource::IssueLocation,
path.to_path_buf(),
"issue_terms".to_string(),
json!({"terms": terms, "locations": locations}),
format!(
"Located {} source line(s) matching issue terms; strongest: {top}",
hits.len()
),
))
}
/// Walk `dir`, recording `(file, line_no, distinct_terms_on_line, term)`.
fn collect_term_matches(
dir: &Path,
terms: &[String],
out: &mut Vec<(String, usize, usize, String)>,
) {
// Bail once we have plenty; this walks a whole source tree.
if out.len() >= Self::MAX_ISSUE_LOCATIONS * 8 {
return;
}
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let p = entry.path();
if p.is_dir() {
Self::collect_term_matches(&p, terms, out);
continue;
}
let is_source = p
.extension()
.and_then(|e| e.to_str())
.is_some_and(|e| Self::SATD_EXTENSIONS.contains(&e));
if !is_source {
continue;
}
let Ok(content) = std::fs::read_to_string(&p) else {
continue;
};
for (idx, line) in content.lines().enumerate() {
let lower = line.to_lowercase();
let matched: Vec<&String> = terms.iter().filter(|t| lower.contains(*t)).collect();
// Two or more distinct issue terms on one line is a real
// signal; one is usually an incidental word.
if matched.len() >= 2 {
out.push((
p.display().to_string(),
idx + 1,
matched.len(),
matched
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
.join("+"),
));
}
}
}
}
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,
/// `(file, total_locations)` for the strongest issue-term match, when the
/// issue was located at all. Hypotheses must cite this when present:
/// without it they describe the repository, not the reported defect.
located: Option<(String, usize)>,
}
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
}),
located: evidence
.iter()
.find(|e| e.source == EvidenceSource::IssueLocation)
.and_then(|e| {
let locs = e.value.get("locations")?.as_array()?;
let first = locs.first()?;
let file = first.get("file")?.as_str()?.to_string();
let line = first.get("line").and_then(serde_json::Value::as_u64);
Some((
line.map_or(file.clone(), |l| format!("{file}:{l}")),
locs.len(),
))
}),
}
}
fn hypothesis_for_depth(&self, depth: u8) -> String {
// When the issue was located, lead with that: it is the only statement
// here derived from the issue rather than from repo-wide metrics. The
// deeper rungs remain repo-level and say so, instead of being presented
// as the cause of a specific defect (GH #637).
if let Some((where_, count)) = &self.located {
return match depth {
1 => format!(
"The issue's terms concentrate at {where_} ({count} matching location(s)) — \
the defect most likely originates in that code path"
),
2 => format!(
"{} (repo-level signal, not specific to {where_})",
self.depth_2_hypothesis()
),
3 => format!(
"{} (repo-level signal, not specific to {where_})",
self.depth_3_hypothesis()
),
4 => "Requirements or constraints were not fully specified (repo-level signal)"
.to_string(),
_ => {
"Systematic process gap in development workflow (repo-level signal)".to_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.
///
/// # Two corrections (GH #637)
///
/// **The score could only ever be 1.0.** Each source contributed
/// `weight * (1.0 + severity)`, and the total was divided by the sum of the
/// weights alone — so the ratio was always at least 1.0 and the final
/// `clamp(0.0, 1.0)` pinned it to exactly 100%. Severity is now in `[0, 1]`
/// so the score genuinely varies. `test_calculate_confidence_increases_with
/// _severity` asserted only `high >= low`, which `1.0 >= 1.0` satisfied, so
/// it could not detect this; it now asserts strict inequality.
///
/// **Confidence measured volume, not relevance.** Every source except
/// [`EvidenceSource::IssueLocation`] is a repo-wide metric that is identical
/// whatever issue was reported. Collecting five of them said nothing about
/// the question asked, yet produced 100%. Without at least one
/// issue-specific location the score is now capped at
/// [`Self::NO_ISSUE_EVIDENCE_CEILING`].
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "score_range")]
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, 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, severity)
}
// v2: TDG removed (redundant with complexity+churn). Weight = 0.
EvidenceSource::TDG => (0.0, 0.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, severity)
}
EvidenceSource::DeadCode => (0.10, 0.5),
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
// Regression is a stronger signal than improvement, but
// neither is evidence about a specific reported issue.
let severity = if evoscore < 0.0 {
(-evoscore).min(1.0)
} else {
0.0
};
(0.15, severity)
}
EvidenceSource::IssueLocation => {
// Severity scales with how many locations matched two or
// more distinct issue terms.
let found = ev
.value
.get("locations")
.and_then(|v| v.as_array())
.map_or(0, Vec::len);
let severity = (found as f64 / 6.0).min(1.0);
(0.35, 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 {
(-delta / 85.0).min(1.0) // Scale by baseline
} else {
0.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
};
// Repo-wide metrics alone cannot support a confident causal claim about
// a specific issue, however many of them were collected.
if Self::has_issue_evidence(evidence) {
Ok(normalized)
} else {
Ok(normalized.min(Self::NO_ISSUE_EVIDENCE_CEILING))
}
}
/// Extract root cause from Why iterations
/// Extract root cause from Why iterations.
///
/// Returns `None` when no evidence pertained to the reported issue. The
/// final hypothesis is drawn from a fixed ladder keyed on repo-wide
/// metrics, so presenting it as *the root cause* of an unlocated issue
/// states a conclusion that was never derived — this is what produced
/// "Frequent changes indicate unstable or poorly understood code" as the
/// root cause of an EOF race in the MCP transport (GH #637).
///
/// Withholding follows the precedent set by
/// `FalsificationResult::unmeasured()` in v3.26.0: a receipt that says
/// nothing is better than one that overstates.
fn extract_root_cause(&self, whys: &[WhyIteration]) -> Result<Option<String>> {
let Some(last_why) = whys.last() else {
return Ok(None);
};
let located = whys
.iter()
.any(|why| Self::has_issue_evidence(&why.evidence));
if !located {
return Ok(None);
}
// Report the deepest hypothesis that was actually derived from the
// issue. The deeper rungs of the ladder are repo-wide signals (churn,
// SATD, coverage) tagged as such; presenting one of those as "the root
// cause" is what made an EOF race read as "Frequent changes indicate
// unstable or poorly understood code" (GH #637).
const REPO_LEVEL_TAG: &str = "repo-level signal";
let derived = whys
.iter()
.rev()
.find(|why| !why.hypothesis.contains(REPO_LEVEL_TAG))
.unwrap_or(last_why);
if derived.hypothesis.contains(REPO_LEVEL_TAG) {
return Ok(None);
}
Ok(Some(format!(
"{}\n\nBeyond localisation no causal chain was derived: the remaining \
\"why\" steps are repo-wide signals, not findings about this defect. \
Confirm by reading the cited locations.",
derived.hypothesis
)))
}
/// Generate actionable recommendations (v2 evidence sources, PMAT-510)
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
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,
));
}
// Root cause fix recommendation — only when there *is* one.
//
// `analyze` passes `root_cause.unwrap_or_default()`, so a withheld cause
// arrives here as an empty string and this printed a bare
// "Address root cause: " with nothing after it. When the issue could not
// be located, the actionable advice is to make it locatable.
if root_cause.trim().is_empty() {
recommendations.push(Recommendation::high(
"No root cause was determined — the reported issue could not be \
located in the source. Re-run with terms that appear in the code \
(identifiers, module or file names, a log string)."
.to_string(),
None,
));
} else {
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)]