Skip to main content

fallow_cli/health_types/
targets.rs

1//! Refactoring target types, recommendations, effort estimates, and evidence.
2
3/// Adaptive thresholds used for refactoring target scoring.
4///
5/// Derived from the project's metric distribution (percentile-based with floors).
6/// Exposed in JSON output so consumers can interpret scores in context.
7#[derive(Debug, Clone, serde::Serialize)]
8#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
9#[allow(
10    clippy::struct_field_names,
11    reason = "triggered in bin but not lib — #[expect] would be unfulfilled in lib"
12)]
13pub struct TargetThresholds {
14    /// Fan-in saturation point for priority formula (p95, floor 5).
15    pub fan_in_p95: f64,
16    /// Fan-in moderate threshold for contributing factors (p75, floor 3).
17    pub fan_in_p75: f64,
18    /// Fan-out saturation point for priority formula (p95, floor 8).
19    pub fan_out_p95: f64,
20    /// Fan-out high threshold for rules and contributing factors (p90, floor 5).
21    pub fan_out_p90: usize,
22}
23
24/// Category of refactoring recommendation.
25#[derive(Debug, Clone, serde::Serialize)]
26#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
27#[serde(rename_all = "snake_case")]
28pub enum RecommendationCategory {
29    /// Actively-changing file with growing complexity — highest urgency.
30    UrgentChurnComplexity,
31    /// File participates in an import cycle with significant blast radius.
32    BreakCircularDependency,
33    /// High fan-in + high complexity — changes here ripple widely.
34    SplitHighImpact,
35    /// Majority of exports are unused — reduce surface area.
36    RemoveDeadCode,
37    /// Contains functions with very high cognitive complexity.
38    ExtractComplexFunctions,
39    /// Excessive imports reduce testability and increase coupling.
40    ExtractDependencies,
41    /// Multiple complex functions lack test dependency path.
42    AddTestCoverage,
43}
44
45impl RecommendationCategory {
46    /// Human-readable label for terminal output.
47    #[must_use]
48    pub const fn label(&self) -> &'static str {
49        match self {
50            Self::UrgentChurnComplexity => "churn+complexity",
51            Self::BreakCircularDependency => "circular dependency",
52            Self::SplitHighImpact => "high impact",
53            Self::RemoveDeadCode => "dead code",
54            Self::ExtractComplexFunctions => "complexity",
55            Self::ExtractDependencies => "coupling",
56            Self::AddTestCoverage => "untested risk",
57        }
58    }
59
60    /// Machine-parseable label for compact output (no spaces).
61    #[must_use]
62    pub const fn compact_label(&self) -> &'static str {
63        match self {
64            Self::UrgentChurnComplexity => "churn_complexity",
65            Self::BreakCircularDependency => "circular_dep",
66            Self::SplitHighImpact => "high_impact",
67            Self::RemoveDeadCode => "dead_code",
68            Self::ExtractComplexFunctions => "complexity",
69            Self::ExtractDependencies => "coupling",
70            Self::AddTestCoverage => "untested_risk",
71        }
72    }
73}
74
75/// A contributing factor that triggered or strengthened a recommendation.
76#[derive(Debug, Clone, serde::Serialize)]
77#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
78pub struct ContributingFactor {
79    /// Metric name (matches JSON field names: `"fan_in"`, `"dead_code_ratio"`, etc.).
80    pub metric: &'static str,
81    /// Raw metric value for programmatic use.
82    pub value: f64,
83    /// Threshold that was exceeded.
84    pub threshold: f64,
85    /// Human-readable explanation.
86    pub detail: String,
87}
88
89/// A ranked refactoring recommendation for a file.
90///
91/// ## Priority Formula
92///
93/// ```text
94/// priority = min(density, 1) × 30 + hotspot_boost × 25 + dead_code × 20 + fan_in_norm × 15 + fan_out_norm × 10
95/// ```
96///
97/// Fan-in and fan-out normalization uses adaptive percentile-based thresholds
98/// (p95 of the project distribution, with floors) instead of fixed constants.
99///
100/// ## Efficiency (default sort)
101///
102/// ```text
103/// efficiency = priority / effort_numeric   (Low=1, Medium=2, High=3)
104/// ```
105///
106/// Surfaces quick wins: high-priority, low-effort targets rank first.
107/// Effort estimate for a refactoring target.
108#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
109#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
110#[serde(rename_all = "snake_case")]
111pub enum EffortEstimate {
112    /// Small file, few functions, low fan-in — quick to address.
113    Low,
114    /// Moderate size or coupling — needs planning.
115    Medium,
116    /// Large file, many functions, or high fan-in — significant effort.
117    High,
118}
119
120impl EffortEstimate {
121    /// Human-readable label for terminal output.
122    #[must_use]
123    pub const fn label(&self) -> &'static str {
124        match self {
125            Self::Low => "low",
126            Self::Medium => "medium",
127            Self::High => "high",
128        }
129    }
130
131    /// Numeric value for arithmetic (efficiency = priority / effort).
132    #[must_use]
133    pub const fn numeric(&self) -> f64 {
134        match self {
135            Self::Low => 1.0,
136            Self::Medium => 2.0,
137            Self::High => 3.0,
138        }
139    }
140}
141
142/// Confidence level for a refactoring recommendation.
143///
144/// Based on the data source reliability:
145/// - **High**: deterministic graph/AST analysis (dead code, circular deps, complexity)
146/// - **Medium**: heuristic thresholds (fan-in/fan-out coupling)
147/// - **Low**: depends on git history quality (churn-based recommendations)
148#[derive(Debug, Clone, serde::Serialize)]
149#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
150#[serde(rename_all = "snake_case")]
151pub enum Confidence {
152    /// Recommendation based on deterministic analysis (graph, AST).
153    High,
154    /// Recommendation based on heuristic thresholds.
155    Medium,
156    /// Recommendation depends on external data quality (git history).
157    Low,
158}
159
160impl Confidence {
161    /// Human-readable label for terminal output.
162    #[must_use]
163    pub const fn label(&self) -> &'static str {
164        match self {
165            Self::High => "high",
166            Self::Medium => "medium",
167            Self::Low => "low",
168        }
169    }
170}
171
172/// Evidence linking a target back to specific analysis data.
173///
174/// Provides enough detail for an AI agent to act on a recommendation
175/// without a second tool call.
176#[derive(Debug, Clone, serde::Serialize)]
177#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
178pub struct TargetEvidence {
179    /// Names of unused exports (populated for `RemoveDeadCode` targets).
180    #[serde(skip_serializing_if = "Vec::is_empty")]
181    pub unused_exports: Vec<String>,
182    /// Complex functions with line numbers and cognitive scores (populated for `ExtractComplexFunctions`).
183    #[serde(skip_serializing_if = "Vec::is_empty")]
184    pub complex_functions: Vec<EvidenceFunction>,
185    /// Files forming the import cycle (populated for `BreakCircularDependency` targets).
186    #[serde(skip_serializing_if = "Vec::is_empty")]
187    pub cycle_path: Vec<String>,
188}
189
190/// A function referenced in target evidence.
191#[derive(Debug, Clone, serde::Serialize)]
192#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
193pub struct EvidenceFunction {
194    /// Function name.
195    pub name: String,
196    /// 1-based line number.
197    pub line: u32,
198    /// Cognitive complexity score.
199    pub cognitive: u16,
200}
201
202#[derive(Debug, Clone, serde::Serialize)]
203#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
204pub struct RefactoringTarget {
205    /// Absolute file path (stripped to relative in output).
206    pub path: std::path::PathBuf,
207    /// Priority score (0–100, higher = more urgent).
208    pub priority: f64,
209    /// Efficiency score (priority / effort). Higher = better quick-win value.
210    /// Surfaces low-effort, high-priority targets first.
211    pub efficiency: f64,
212    /// One-line actionable recommendation.
213    pub recommendation: String,
214    /// Recommendation category for tooling/filtering.
215    pub category: RecommendationCategory,
216    /// Estimated effort to address this target.
217    pub effort: EffortEstimate,
218    /// Confidence in this recommendation based on data source reliability.
219    pub confidence: Confidence,
220    /// Contributing factors that triggered this recommendation. Empty array
221    /// omitted from JSON.
222    #[serde(skip_serializing_if = "Vec::is_empty")]
223    #[cfg_attr(feature = "schema", schemars(default))]
224    pub factors: Vec<ContributingFactor>,
225    /// Structured evidence linking to specific analysis data.
226    #[serde(skip_serializing_if = "Option::is_none")]
227    pub evidence: Option<TargetEvidence>,
228}
229
230#[cfg(test)]
231mod tests {
232    use super::*;
233
234    // --- RecommendationCategory ---
235
236    #[test]
237    fn category_labels_are_non_empty() {
238        let categories = [
239            RecommendationCategory::UrgentChurnComplexity,
240            RecommendationCategory::BreakCircularDependency,
241            RecommendationCategory::SplitHighImpact,
242            RecommendationCategory::RemoveDeadCode,
243            RecommendationCategory::ExtractComplexFunctions,
244            RecommendationCategory::ExtractDependencies,
245            RecommendationCategory::AddTestCoverage,
246        ];
247        for cat in &categories {
248            assert!(!cat.label().is_empty(), "{cat:?} should have a label");
249        }
250    }
251
252    #[test]
253    fn category_labels_are_unique() {
254        let categories = [
255            RecommendationCategory::UrgentChurnComplexity,
256            RecommendationCategory::BreakCircularDependency,
257            RecommendationCategory::SplitHighImpact,
258            RecommendationCategory::RemoveDeadCode,
259            RecommendationCategory::ExtractComplexFunctions,
260            RecommendationCategory::ExtractDependencies,
261            RecommendationCategory::AddTestCoverage,
262        ];
263        let labels: Vec<&str> = categories
264            .iter()
265            .map(RecommendationCategory::label)
266            .collect();
267        let unique: rustc_hash::FxHashSet<&&str> = labels.iter().collect();
268        assert_eq!(labels.len(), unique.len(), "category labels must be unique");
269    }
270
271    // --- Serde serialization ---
272
273    #[test]
274    fn category_serializes_as_snake_case() {
275        let json = serde_json::to_string(&RecommendationCategory::UrgentChurnComplexity).unwrap();
276        assert_eq!(json, r#""urgent_churn_complexity""#);
277
278        let json = serde_json::to_string(&RecommendationCategory::BreakCircularDependency).unwrap();
279        assert_eq!(json, r#""break_circular_dependency""#);
280    }
281
282    #[test]
283    fn refactoring_target_skips_empty_factors() {
284        let target = RefactoringTarget {
285            path: std::path::PathBuf::from("/src/foo.ts"),
286            priority: 75.0,
287            efficiency: 75.0,
288            recommendation: "Test recommendation".into(),
289            category: RecommendationCategory::RemoveDeadCode,
290            effort: EffortEstimate::Low,
291            confidence: Confidence::High,
292            factors: vec![],
293            evidence: None,
294        };
295        let json = serde_json::to_string(&target).unwrap();
296        assert!(!json.contains("factors"));
297        assert!(!json.contains("evidence"));
298    }
299
300    #[test]
301    fn effort_numeric_values() {
302        assert!((EffortEstimate::Low.numeric() - 1.0).abs() < f64::EPSILON);
303        assert!((EffortEstimate::Medium.numeric() - 2.0).abs() < f64::EPSILON);
304        assert!((EffortEstimate::High.numeric() - 3.0).abs() < f64::EPSILON);
305    }
306
307    #[test]
308    fn confidence_labels_are_non_empty() {
309        let levels = [Confidence::High, Confidence::Medium, Confidence::Low];
310        for level in &levels {
311            assert!(!level.label().is_empty(), "{level:?} should have a label");
312        }
313    }
314
315    #[test]
316    fn confidence_serializes_as_snake_case() {
317        let json = serde_json::to_string(&Confidence::High).unwrap();
318        assert_eq!(json, r#""high""#);
319        let json = serde_json::to_string(&Confidence::Medium).unwrap();
320        assert_eq!(json, r#""medium""#);
321        let json = serde_json::to_string(&Confidence::Low).unwrap();
322        assert_eq!(json, r#""low""#);
323    }
324
325    #[test]
326    fn contributing_factor_serializes_correctly() {
327        let factor = ContributingFactor {
328            metric: "fan_in",
329            value: 15.0,
330            threshold: 10.0,
331            detail: "15 files depend on this".into(),
332        };
333        let json = serde_json::to_string(&factor).unwrap();
334        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
335        assert_eq!(parsed["metric"], "fan_in");
336        assert_eq!(parsed["value"], 15.0);
337        assert_eq!(parsed["threshold"], 10.0);
338    }
339
340    // --- RecommendationCategory compact_labels ---
341
342    #[test]
343    fn category_compact_labels_are_non_empty() {
344        let categories = [
345            RecommendationCategory::UrgentChurnComplexity,
346            RecommendationCategory::BreakCircularDependency,
347            RecommendationCategory::SplitHighImpact,
348            RecommendationCategory::RemoveDeadCode,
349            RecommendationCategory::ExtractComplexFunctions,
350            RecommendationCategory::ExtractDependencies,
351            RecommendationCategory::AddTestCoverage,
352        ];
353        for cat in &categories {
354            assert!(
355                !cat.compact_label().is_empty(),
356                "{cat:?} should have a compact_label"
357            );
358        }
359    }
360
361    #[test]
362    fn category_compact_labels_are_unique() {
363        let categories = [
364            RecommendationCategory::UrgentChurnComplexity,
365            RecommendationCategory::BreakCircularDependency,
366            RecommendationCategory::SplitHighImpact,
367            RecommendationCategory::RemoveDeadCode,
368            RecommendationCategory::ExtractComplexFunctions,
369            RecommendationCategory::ExtractDependencies,
370            RecommendationCategory::AddTestCoverage,
371        ];
372        let labels: Vec<&str> = categories
373            .iter()
374            .map(RecommendationCategory::compact_label)
375            .collect();
376        let unique: rustc_hash::FxHashSet<&&str> = labels.iter().collect();
377        assert_eq!(labels.len(), unique.len(), "compact labels must be unique");
378    }
379
380    #[test]
381    fn category_compact_labels_have_no_spaces() {
382        let categories = [
383            RecommendationCategory::UrgentChurnComplexity,
384            RecommendationCategory::BreakCircularDependency,
385            RecommendationCategory::SplitHighImpact,
386            RecommendationCategory::RemoveDeadCode,
387            RecommendationCategory::ExtractComplexFunctions,
388            RecommendationCategory::ExtractDependencies,
389            RecommendationCategory::AddTestCoverage,
390        ];
391        for cat in &categories {
392            assert!(
393                !cat.compact_label().contains(' '),
394                "compact_label for {:?} should not contain spaces: '{}'",
395                cat,
396                cat.compact_label()
397            );
398        }
399    }
400
401    // --- EffortEstimate ---
402
403    #[test]
404    fn effort_labels_are_non_empty() {
405        let efforts = [
406            EffortEstimate::Low,
407            EffortEstimate::Medium,
408            EffortEstimate::High,
409        ];
410        for effort in &efforts {
411            assert!(!effort.label().is_empty(), "{effort:?} should have a label");
412        }
413    }
414
415    #[test]
416    fn effort_serializes_as_snake_case() {
417        assert_eq!(
418            serde_json::to_string(&EffortEstimate::Low).unwrap(),
419            r#""low""#
420        );
421        assert_eq!(
422            serde_json::to_string(&EffortEstimate::Medium).unwrap(),
423            r#""medium""#
424        );
425        assert_eq!(
426            serde_json::to_string(&EffortEstimate::High).unwrap(),
427            r#""high""#
428        );
429    }
430
431    // --- TargetEvidence ---
432
433    #[test]
434    fn target_evidence_skips_empty_fields() {
435        let evidence = TargetEvidence {
436            unused_exports: vec![],
437            complex_functions: vec![],
438            cycle_path: vec![],
439        };
440        let json = serde_json::to_string(&evidence).unwrap();
441        assert!(!json.contains("unused_exports"));
442        assert!(!json.contains("complex_functions"));
443        assert!(!json.contains("cycle_path"));
444    }
445
446    #[test]
447    fn target_evidence_with_data() {
448        let evidence = TargetEvidence {
449            unused_exports: vec!["foo".to_string(), "bar".to_string()],
450            complex_functions: vec![EvidenceFunction {
451                name: "processData".into(),
452                line: 42,
453                cognitive: 30,
454            }],
455            cycle_path: vec![],
456        };
457        let json = serde_json::to_string(&evidence).unwrap();
458        assert!(json.contains("unused_exports"));
459        assert!(json.contains("complex_functions"));
460        assert!(json.contains("processData"));
461        assert!(!json.contains("cycle_path"));
462    }
463}