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(default, 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(default, skip_serializing_if = "Vec::is_empty")]
184    pub complex_functions: Vec<EvidenceFunction>,
185    /// Files forming the import cycle (populated for `BreakCircularDependency` targets).
186    #[serde(default, 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    #[serde(serialize_with = "fallow_types::serde_path::serialize")]
207    pub path: std::path::PathBuf,
208    /// Priority score (0–100, higher = more urgent).
209    pub priority: f64,
210    /// Efficiency score (priority / effort). Higher = better quick-win value.
211    /// Surfaces low-effort, high-priority targets first.
212    pub efficiency: f64,
213    /// One-line actionable recommendation.
214    pub recommendation: String,
215    /// Recommendation category for tooling/filtering.
216    pub category: RecommendationCategory,
217    /// Estimated effort to address this target.
218    pub effort: EffortEstimate,
219    /// Confidence in this recommendation based on data source reliability.
220    pub confidence: Confidence,
221    /// Contributing factors that triggered this recommendation. Empty array
222    /// omitted from JSON.
223    #[serde(default, skip_serializing_if = "Vec::is_empty")]
224    #[cfg_attr(feature = "schema", schemars(default))]
225    pub factors: Vec<ContributingFactor>,
226    /// Structured evidence linking to specific analysis data.
227    #[serde(default, skip_serializing_if = "Option::is_none")]
228    pub evidence: Option<TargetEvidence>,
229}
230
231#[cfg(test)]
232mod tests {
233    use super::*;
234
235    #[test]
236    fn category_labels_are_non_empty() {
237        let categories = [
238            RecommendationCategory::UrgentChurnComplexity,
239            RecommendationCategory::BreakCircularDependency,
240            RecommendationCategory::SplitHighImpact,
241            RecommendationCategory::RemoveDeadCode,
242            RecommendationCategory::ExtractComplexFunctions,
243            RecommendationCategory::ExtractDependencies,
244            RecommendationCategory::AddTestCoverage,
245        ];
246        for cat in &categories {
247            assert!(!cat.label().is_empty(), "{cat:?} should have a label");
248        }
249    }
250
251    #[test]
252    fn category_labels_are_unique() {
253        let categories = [
254            RecommendationCategory::UrgentChurnComplexity,
255            RecommendationCategory::BreakCircularDependency,
256            RecommendationCategory::SplitHighImpact,
257            RecommendationCategory::RemoveDeadCode,
258            RecommendationCategory::ExtractComplexFunctions,
259            RecommendationCategory::ExtractDependencies,
260            RecommendationCategory::AddTestCoverage,
261        ];
262        let labels: Vec<&str> = categories
263            .iter()
264            .map(RecommendationCategory::label)
265            .collect();
266        let unique: rustc_hash::FxHashSet<&&str> = labels.iter().collect();
267        assert_eq!(labels.len(), unique.len(), "category labels must be unique");
268    }
269
270    #[test]
271    fn category_serializes_as_snake_case() {
272        let json = serde_json::to_string(&RecommendationCategory::UrgentChurnComplexity).unwrap();
273        assert_eq!(json, r#""urgent_churn_complexity""#);
274
275        let json = serde_json::to_string(&RecommendationCategory::BreakCircularDependency).unwrap();
276        assert_eq!(json, r#""break_circular_dependency""#);
277    }
278
279    #[test]
280    fn refactoring_target_skips_empty_factors() {
281        let target = RefactoringTarget {
282            path: std::path::PathBuf::from("/src/foo.ts"),
283            priority: 75.0,
284            efficiency: 75.0,
285            recommendation: "Test recommendation".into(),
286            category: RecommendationCategory::RemoveDeadCode,
287            effort: EffortEstimate::Low,
288            confidence: Confidence::High,
289            factors: vec![],
290            evidence: None,
291        };
292        let json = serde_json::to_string(&target).unwrap();
293        assert!(!json.contains("factors"));
294        assert!(!json.contains("evidence"));
295    }
296
297    #[test]
298    fn effort_numeric_values() {
299        assert!((EffortEstimate::Low.numeric() - 1.0).abs() < f64::EPSILON);
300        assert!((EffortEstimate::Medium.numeric() - 2.0).abs() < f64::EPSILON);
301        assert!((EffortEstimate::High.numeric() - 3.0).abs() < f64::EPSILON);
302    }
303
304    #[test]
305    fn confidence_labels_are_non_empty() {
306        let levels = [Confidence::High, Confidence::Medium, Confidence::Low];
307        for level in &levels {
308            assert!(!level.label().is_empty(), "{level:?} should have a label");
309        }
310    }
311
312    #[test]
313    fn confidence_serializes_as_snake_case() {
314        let json = serde_json::to_string(&Confidence::High).unwrap();
315        assert_eq!(json, r#""high""#);
316        let json = serde_json::to_string(&Confidence::Medium).unwrap();
317        assert_eq!(json, r#""medium""#);
318        let json = serde_json::to_string(&Confidence::Low).unwrap();
319        assert_eq!(json, r#""low""#);
320    }
321
322    #[test]
323    fn contributing_factor_serializes_correctly() {
324        let factor = ContributingFactor {
325            metric: "fan_in",
326            value: 15.0,
327            threshold: 10.0,
328            detail: "15 files depend on this".into(),
329        };
330        let json = serde_json::to_string(&factor).unwrap();
331        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
332        assert_eq!(parsed["metric"], "fan_in");
333        assert_eq!(parsed["value"], 15.0);
334        assert_eq!(parsed["threshold"], 10.0);
335    }
336
337    #[test]
338    fn category_compact_labels_are_non_empty() {
339        let categories = [
340            RecommendationCategory::UrgentChurnComplexity,
341            RecommendationCategory::BreakCircularDependency,
342            RecommendationCategory::SplitHighImpact,
343            RecommendationCategory::RemoveDeadCode,
344            RecommendationCategory::ExtractComplexFunctions,
345            RecommendationCategory::ExtractDependencies,
346            RecommendationCategory::AddTestCoverage,
347        ];
348        for cat in &categories {
349            assert!(
350                !cat.compact_label().is_empty(),
351                "{cat:?} should have a compact_label"
352            );
353        }
354    }
355
356    #[test]
357    fn category_compact_labels_are_unique() {
358        let categories = [
359            RecommendationCategory::UrgentChurnComplexity,
360            RecommendationCategory::BreakCircularDependency,
361            RecommendationCategory::SplitHighImpact,
362            RecommendationCategory::RemoveDeadCode,
363            RecommendationCategory::ExtractComplexFunctions,
364            RecommendationCategory::ExtractDependencies,
365            RecommendationCategory::AddTestCoverage,
366        ];
367        let labels: Vec<&str> = categories
368            .iter()
369            .map(RecommendationCategory::compact_label)
370            .collect();
371        let unique: rustc_hash::FxHashSet<&&str> = labels.iter().collect();
372        assert_eq!(labels.len(), unique.len(), "compact labels must be unique");
373    }
374
375    #[test]
376    fn category_compact_labels_have_no_spaces() {
377        let categories = [
378            RecommendationCategory::UrgentChurnComplexity,
379            RecommendationCategory::BreakCircularDependency,
380            RecommendationCategory::SplitHighImpact,
381            RecommendationCategory::RemoveDeadCode,
382            RecommendationCategory::ExtractComplexFunctions,
383            RecommendationCategory::ExtractDependencies,
384            RecommendationCategory::AddTestCoverage,
385        ];
386        for cat in &categories {
387            assert!(
388                !cat.compact_label().contains(' '),
389                "compact_label for {:?} should not contain spaces: '{}'",
390                cat,
391                cat.compact_label()
392            );
393        }
394    }
395
396    #[test]
397    fn effort_labels_are_non_empty() {
398        let efforts = [
399            EffortEstimate::Low,
400            EffortEstimate::Medium,
401            EffortEstimate::High,
402        ];
403        for effort in &efforts {
404            assert!(!effort.label().is_empty(), "{effort:?} should have a label");
405        }
406    }
407
408    #[test]
409    fn effort_serializes_as_snake_case() {
410        assert_eq!(
411            serde_json::to_string(&EffortEstimate::Low).unwrap(),
412            r#""low""#
413        );
414        assert_eq!(
415            serde_json::to_string(&EffortEstimate::Medium).unwrap(),
416            r#""medium""#
417        );
418        assert_eq!(
419            serde_json::to_string(&EffortEstimate::High).unwrap(),
420            r#""high""#
421        );
422    }
423
424    #[test]
425    fn target_evidence_skips_empty_fields() {
426        let evidence = TargetEvidence {
427            unused_exports: vec![],
428            complex_functions: vec![],
429            cycle_path: vec![],
430        };
431        let json = serde_json::to_string(&evidence).unwrap();
432        assert!(!json.contains("unused_exports"));
433        assert!(!json.contains("complex_functions"));
434        assert!(!json.contains("cycle_path"));
435    }
436
437    #[test]
438    fn target_evidence_with_data() {
439        let evidence = TargetEvidence {
440            unused_exports: vec!["foo".to_string(), "bar".to_string()],
441            complex_functions: vec![EvidenceFunction {
442                name: "processData".into(),
443                line: 42,
444                cognitive: 30,
445            }],
446            cycle_path: vec![],
447        };
448        let json = serde_json::to_string(&evidence).unwrap();
449        assert!(json.contains("unused_exports"));
450        assert!(json.contains("complex_functions"));
451        assert!(json.contains("processData"));
452        assert!(!json.contains("cycle_path"));
453    }
454}