Skip to main content

fallow_types/
output_health.rs

1//! Per-action types attached to each health finding by the JSON output
2//! layer.
3//!
4//! These types are the typed wire shape for the `actions[]` array on health
5//! findings, hotspots, refactoring targets, and coverage-gap entries. The
6//! JSON emission path constructs them through typed wrappers (for example
7//! `UntestedFileFinding` in `crates/cli/src/health_types/coverage.rs`) and
8//! serializes them via serde; the schemars derive renders the matching
9//! per-action shape in `docs/output-schema.json`.
10//!
11//! Whenever a new action variant or optional field is added, update the
12//! matching type here so the drift gate flags the divergence before review.
13
14use serde::Serialize;
15
16/// Suggested action attached to a [`ComplexityViolation`].
17///
18/// Each complexity finding carries an array of these on the JSON wire
19/// (`findings[].actions[]`). The action selector in
20/// `crates/cli/src/report/json.rs::build_health_finding_actions` picks the
21/// primary action based on which thresholds triggered the finding and the
22/// bucketed coverage tier. See [`HealthFindingActionType`] for the full
23/// discriminant list.
24///
25/// `note`, `comment`, and `placement` are populated per-variant: refactor
26/// actions carry a `note`, suppress-line / suppress-file actions carry
27/// `comment` plus `placement`, and the coverage-leaning actions
28/// (`add-tests`, `increase-coverage`) carry only `note`.
29///
30/// [`ComplexityViolation`]: ../../fallow-cli/src/health_types/scores.rs
31#[derive(Debug, Clone, Serialize)]
32#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
33pub struct HealthFindingAction {
34    /// Action type identifier. A single finding's `actions` array can carry
35    /// MULTIPLE entries of different types: e.g., a finding that exceeded
36    /// both cyclomatic and CRAP at `coverage_tier`: partial will get BOTH
37    /// `increase-coverage` AND `refactor-function`, plus `suppress-line`.
38    /// Consumers that select a single action should treat the FIRST
39    /// non-`suppress-{line,file}` action as primary. `add-tests` is emitted
40    /// when CRAP triggered the finding, the function has no test coverage
41    /// (`coverage_tier`: none), and full coverage can bring CRAP below
42    /// `max_crap_threshold` (cyclomatic < threshold, since CRAP bottoms out
43    /// at CC at 100% coverage). `increase-coverage` is emitted when CRAP
44    /// triggered the finding, some coverage exists (`coverage_tier`: partial
45    /// or high), and full coverage can bring CRAP below `max_crap_threshold`;
46    /// the description steers toward targeted branch coverage rather than
47    /// scaffolding new tests. `refactor-function` is emitted when
48    /// cyclomatic/cognitive triggered the finding, when full coverage still
49    /// cannot bring CRAP below `max_crap_threshold` (cyclomatic >=
50    /// threshold), or as a secondary action when cyclomatic is within 5 of
51    /// the cyclomatic threshold AND cognitive is at or above
52    /// `max_cognitive_threshold / 2` (the cognitive floor suppresses false
53    /// positives on flat type-tag dispatchers and JSX render maps where
54    /// high cyclomatic comes from a single switch with near-zero cognitive
55    /// load). `suppress-file` is emitted instead of `suppress-line` for
56    /// synthetic Angular `<template>` findings on `.html` files, because
57    /// line-suppression comments cannot be expressed in HTML; the `comment`
58    /// field carries `<!-- fallow-ignore-file complexity -->` and
59    /// `placement` is `top-of-template`.
60    #[serde(rename = "type")]
61    pub kind: HealthFindingActionType,
62    /// Whether `fallow fix` can auto-apply this action. Today every health
63    /// finding action is manual, but the field is non-singleton so a future
64    /// auto-applier (e.g., an LLM-driven `refactor-function` worker) does
65    /// not need a schema change.
66    pub auto_fixable: bool,
67    /// Human-readable description of the action.
68    pub description: String,
69    /// Additional context (e.g., the canonical CRAP formula, or a hint
70    /// about which branch type to extract). Present on most action types;
71    /// dropped only when the description carries the full ask.
72    #[serde(default, skip_serializing_if = "Option::is_none")]
73    pub note: Option<String>,
74    /// The inline comment to insert (e.g.,
75    /// `// fallow-ignore-next-line complexity` or
76    /// `<!-- fallow-ignore-file complexity -->`). Present on
77    /// `suppress-line` and `suppress-file` action variants.
78    #[serde(default, skip_serializing_if = "Option::is_none")]
79    pub comment: Option<String>,
80    /// Where to insert the suppress comment
81    /// (e.g., `above-function-declaration`, `above-angular-decorator`,
82    /// `above-component-worst-method`, or `top-of-template`). Present on
83    /// `suppress-line` and `suppress-file` action variants.
84    #[serde(default, skip_serializing_if = "Option::is_none")]
85    pub placement: Option<String>,
86    /// Project-relative path the action should target when the finding's
87    /// remediation lives in a different file from where the finding is
88    /// anchored. Currently populated on the `increase-coverage` action for
89    /// synthetic Angular `<template>` findings whose CRAP is inherited from
90    /// the owning `.component.ts`: the action points at the component file
91    /// (where the user actually adds tests) rather than the `.html` template
92    /// (where the finding is anchored but which is not directly testable).
93    /// Absent when the action's target is the finding's own file.
94    #[serde(default, skip_serializing_if = "Option::is_none")]
95    pub target_path: Option<String>,
96}
97
98/// Discriminant for [`HealthFindingAction::kind`]. Mirrors the action types
99/// emitted by `build_health_finding_actions`. A single finding's `actions`
100/// array may carry multiple entries of different types: a finding that
101/// exceeded both cyclomatic and CRAP at `coverage_tier: partial` will get
102/// BOTH `increase-coverage` AND `refactor-function`, plus the trailing
103/// `suppress-line`.
104#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
105#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
106#[serde(rename_all = "kebab-case")]
107pub enum HealthFindingActionType {
108    /// Refactor the function to reduce complexity. Emitted when
109    /// cyclomatic/cognitive triggered the finding, when full coverage
110    /// still cannot bring CRAP below `max_crap_threshold`, or as a
111    /// secondary action when cyclomatic is within 5 of the cyclomatic
112    /// threshold AND cognitive is at or above the cognitive floor.
113    RefactorFunction,
114    /// Add tests for a CRAP-triggered finding whose coverage tier is
115    /// `none` (no test path reaches the function).
116    AddTests,
117    /// Increase test coverage for a CRAP-triggered finding whose coverage
118    /// tier is `partial` or `high` (some test path exists; add targeted
119    /// assertions for uncovered branches).
120    IncreaseCoverage,
121    /// Suppress with an HTML comment at the top of the template. Used for
122    /// synthetic Angular `<template>` findings on `.html` files where a
123    /// line suppression cannot be expressed.
124    SuppressFile,
125    /// Suppress with an inline `// fallow-ignore-next-line complexity`
126    /// comment above the function or Angular decorator.
127    SuppressLine,
128}
129
130/// Suggested action attached to a [`HotspotEntry`].
131///
132/// The action list always begins with `refactor-file` plus `add-tests`.
133/// Ownership-derived variants (`low-bus-factor`, `unowned-hotspot`,
134/// `ownership-drift`) are appended only when `--ownership` is active AND
135/// the corresponding signal fires for the hotspot.
136///
137/// [`HotspotEntry`]: ../../fallow-cli/src/health_types/scores.rs
138#[derive(Debug, Clone, Serialize)]
139#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
140pub struct HotspotAction {
141    /// Action type identifier.
142    #[serde(rename = "type")]
143    pub kind: HotspotActionType,
144    /// Whether `fallow fix` can auto-apply this action. Today every
145    /// hotspot action is manual.
146    pub auto_fixable: bool,
147    /// Human-readable description of the action.
148    pub description: String,
149    /// Additional context for the action. Absent on `low-bus-factor` when
150    /// the finding's description already carries the full ask (no
151    /// suggested reviewers and not a low-commit file).
152    #[serde(default, skip_serializing_if = "Option::is_none")]
153    pub note: Option<String>,
154    /// Suggested CODEOWNERS pattern. Present only on `unowned-hotspot`
155    /// actions. Derived per the [`heuristic`](Self::heuristic) field;
156    /// consumers should branch on [`heuristic`](Self::heuristic) rather
157    /// than assume a stable algorithm.
158    #[serde(default, skip_serializing_if = "Option::is_none")]
159    pub suggested_pattern: Option<String>,
160    /// Strategy used to derive [`suggested_pattern`](Self::suggested_pattern).
161    /// Reserved for future evolution (`codeowners-cluster`, etc.).
162    #[serde(default, skip_serializing_if = "Option::is_none")]
163    pub heuristic: Option<HotspotActionHeuristic>,
164}
165
166/// Discriminant for [`HotspotAction::kind`].
167#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
168#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
169#[serde(rename_all = "kebab-case")]
170pub enum HotspotActionType {
171    /// Refactor the hotspot file (high complexity plus frequent change).
172    RefactorFile,
173    /// Add test coverage to reduce change risk on the hotspot file.
174    AddTests,
175    /// Bus factor of 1: a single recent contributor owns the file.
176    /// Emitted only with `--ownership`.
177    LowBusFactor,
178    /// Hotspot matches no CODEOWNERS rule (a rules file exists but no
179    /// pattern matches). Emitted only with `--ownership`.
180    UnownedHotspot,
181    /// Ownership has drifted from the original author to a new top
182    /// contributor. Emitted only with `--ownership`.
183    OwnershipDrift,
184}
185
186/// Strategy discriminant for the suggested CODEOWNERS pattern attached to
187/// an `unowned-hotspot` action.
188#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
189#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
190#[serde(rename_all = "kebab-case")]
191pub enum HotspotActionHeuristic {
192    /// Suggest the deepest directory containing the file (e.g.,
193    /// `/src/api/users/`). Keeps the suggestion reviewable while staying
194    /// a directory pattern rather than a per-file rule.
195    DirectoryDeepest,
196}
197
198/// Suggested action attached to a [`RefactoringTarget`].
199///
200/// The list always begins with `apply-refactoring`. A trailing
201/// `suppress-line` is appended only when the target carries `evidence`
202/// linking to specific functions (e.g., `extract_complex_functions`,
203/// `add_test_coverage`).
204///
205/// Unlike [`HealthFindingAction`], the `suppress-line` variant emitted
206/// here does NOT carry a `placement` field: the parent
207/// [`RefactoringTarget`] points at a file (not a specific function
208/// declaration site), so a per-line placement hint would have no
209/// referent. Consumers that want the placement metadata should follow
210/// the target's `evidence.complex_functions` back to the matching
211/// `ComplexityViolation` and read placement from THAT action instead.
212///
213/// [`RefactoringTarget`]: ../../fallow-cli/src/health_types/targets.rs
214#[derive(Debug, Clone, Serialize)]
215#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
216pub struct RefactoringTargetAction {
217    /// Action type identifier.
218    #[serde(rename = "type")]
219    pub kind: RefactoringTargetActionType,
220    /// Whether `fallow fix` can auto-apply this action. Today both
221    /// variants are manual.
222    pub auto_fixable: bool,
223    /// Human-readable description of the action. For `apply-refactoring`
224    /// this is the target's own `recommendation` string; for
225    /// `suppress-line` it is the suppression prompt.
226    pub description: String,
227    /// Recommendation category for `apply-refactoring` actions. Mirrors
228    /// the parent target's
229    /// [`category`](../../fallow-cli/src/health_types/targets.rs.html)
230    /// field so consumers can route on the action alone.
231    #[serde(default, skip_serializing_if = "Option::is_none")]
232    pub category: Option<String>,
233    /// The inline comment to insert. Present on `suppress-line` actions
234    /// when evidence exists.
235    #[serde(default, skip_serializing_if = "Option::is_none")]
236    pub comment: Option<String>,
237}
238
239/// Discriminant for [`RefactoringTargetAction::kind`].
240#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
241#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
242#[serde(rename_all = "kebab-case")]
243pub enum RefactoringTargetActionType {
244    /// Apply the recommended refactoring (extract, split, decouple, etc.).
245    ApplyRefactoring,
246    /// Suppress the underlying complexity finding with an inline comment.
247    SuppressLine,
248}
249
250/// Suggested action attached to an [`UntestedFile`] coverage-gap finding.
251///
252/// `build_untested_file_actions` emits a two-entry array on every
253/// untested-file item: an `add-tests` primary action (scaffold tests for
254/// the runtime file) and a `suppress-file` action
255/// (`// fallow-ignore-file coverage-gaps`). Both variants share the same
256/// struct shape; the field that is populated (`note` for `add-tests`,
257/// `comment` for `suppress-file`) depends on the `kind`.
258///
259/// [`UntestedFile`]: ../../fallow-cli/src/health_types/coverage.rs
260#[derive(Debug, Clone, Serialize)]
261#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
262pub struct UntestedFileAction {
263    /// Action type identifier.
264    #[serde(rename = "type")]
265    pub kind: UntestedFileActionType,
266    /// Whether `fallow fix` can auto-apply this action. Today both
267    /// variants are manual.
268    pub auto_fixable: bool,
269    /// Human-readable description of the action.
270    pub description: String,
271    /// Additional context for the `add-tests` variant (explains why no
272    /// test path reaches this file). Absent on `suppress-file`.
273    #[serde(default, skip_serializing_if = "Option::is_none")]
274    pub note: Option<String>,
275    /// The file-level comment to insert. Present on `suppress-file`
276    /// (`// fallow-ignore-file coverage-gaps`). Absent on `add-tests`.
277    #[serde(default, skip_serializing_if = "Option::is_none")]
278    pub comment: Option<String>,
279}
280
281/// Discriminant for [`UntestedFileAction::kind`]. Mirrors the action types
282/// emitted by `build_untested_file_actions`.
283#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
284#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
285#[serde(rename_all = "kebab-case")]
286pub enum UntestedFileActionType {
287    /// Scaffold tests that exercise the runtime file.
288    AddTests,
289    /// Suppress coverage-gap reporting for this file with a file-level
290    /// comment.
291    SuppressFile,
292}
293
294/// Suggested action attached to an [`UntestedExport`] coverage-gap
295/// finding.
296///
297/// `build_untested_export_actions` emits a two-entry array on every
298/// untested-export item: an `add-test-import` primary action (import the
299/// export from a test-reachable module) and a `suppress-file` action
300/// (`// fallow-ignore-file coverage-gaps`). The export-specific variant
301/// `add-test-import` reflects that a test-reachable reference chain, not
302/// just any test coverage, is what closes the gap.
303///
304/// [`UntestedExport`]: ../../fallow-cli/src/health_types/coverage.rs
305#[derive(Debug, Clone, Serialize)]
306#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
307pub struct UntestedExportAction {
308    /// Action type identifier.
309    #[serde(rename = "type")]
310    pub kind: UntestedExportActionType,
311    /// Whether `fallow fix` can auto-apply this action. Today both
312    /// variants are manual.
313    pub auto_fixable: bool,
314    /// Human-readable description of the action.
315    pub description: String,
316    /// Additional context for the `add-test-import` variant (explains the
317    /// runtime-reachable / test-unreachable asymmetry). Absent on
318    /// `suppress-file`.
319    #[serde(default, skip_serializing_if = "Option::is_none")]
320    pub note: Option<String>,
321    /// The file-level comment to insert. Present on `suppress-file`
322    /// (`// fallow-ignore-file coverage-gaps`). Absent on
323    /// `add-test-import`.
324    #[serde(default, skip_serializing_if = "Option::is_none")]
325    pub comment: Option<String>,
326}
327
328/// Discriminant for [`UntestedExportAction::kind`]. Mirrors the action
329/// types emitted by `build_untested_export_actions`.
330#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
331#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
332#[serde(rename_all = "kebab-case")]
333pub enum UntestedExportActionType {
334    /// Import and exercise the export from a test-reachable module.
335    AddTestImport,
336    /// Suppress coverage-gap reporting for the export's file with a
337    /// file-level comment.
338    SuppressFile,
339}