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
//! Vital signs: project-wide metrics for trend tracking and snapshots.
use crate::CoverageModel;
/// Current snapshot schema version. Independent of the report's SCHEMA_VERSION.
/// v2: Added `score` and `grade` fields.
/// v3: Added `coverage_model` field.
/// v4: Added risk profiles (`unit_size_profile`, `unit_interfacing_profile`) and
/// coupling concentration (`p95_fan_in`, `coupling_high_pct`).
/// v5: Added duplication penalty to health score formula.
/// v6: Added `total_loc` to vital signs (always computed from parsed modules).
/// v7: MI formula dampening for small files (values change for files < 50 lines).
/// v8: Added scale-invariant tail/density metrics for health score calibration.
/// v9: Added render fan-in concentration (`p95_render_fan_in`,
/// `render_fan_in_high_pct`, `max_render_fan_in`), the component-graph
/// analogue of module fan-in / coupling concentration. Additive optional
/// fields (matches the v4 precedent that added coupling concentration).
/// v10: Added `analysis_identity` for type-aware trend compatibility.
/// v11: Added optional `groups` (per-group vital signs, counts and score of a
/// `--group-by` run). Additive: an older binary ignores the field, and
/// this binary reads v1 to v10 files with `groups` absent.
/// v12: Added optional `score_formula_version` for project and group score
/// compatibility. Older snapshots retain unknown formula identity.
pub const SNAPSHOT_SCHEMA_VERSION: u32 = 12;
/// Project-wide vital signs: a fixed set of metrics for trend tracking.
///
/// Metrics are `Option` when the data source was not available in the current run
/// (e.g., `duplication_pct` is `None` unless the duplication pipeline was run,
/// `hotspot_count` is `None` without git history).
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct VitalSigns {
/// Percentage of files not reachable from any entry point.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub dead_file_pct: Option<f64>,
/// Percentage of exports never imported by other modules.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub dead_export_pct: Option<f64>,
/// Average cyclomatic complexity across authored functions, module-scope
/// units, and template units. See `cyclomatic_population` for the denominator.
pub avg_cyclomatic: f64,
/// Percentage of complexity units at or above the critical cyclomatic threshold.
/// Used by the scale-invariant health score.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub critical_complexity_pct: Option<f64>,
/// 90th percentile cyclomatic complexity across the same unit population.
pub p90_cyclomatic: u32,
/// Population behind the cyclomatic mean, percentile, and critical share.
/// Present on current analyses; absent on older saved snapshots.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cyclomatic_population: Option<CyclomaticPopulation>,
/// Code duplication percentage (None if duplication pipeline was not run).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub duplication_pct: Option<f64>,
/// Number of hotspot files (score >= 50). None if git history unavailable.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub hotspot_count: Option<u32>,
/// Number of files in the top 1% of the within-project hotspot ranking.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub hotspot_top_pct_count: Option<u32>,
/// Average maintainability index across all scored files (0-100).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub maintainability_avg: Option<f64>,
/// Percentage of scored files with maintainability index below 70. Null if
/// file scores were not computed.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub maintainability_low_pct: Option<f64>,
/// Number of unused dependencies (dependencies + devDependencies + optional).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub unused_dep_count: Option<u32>,
/// Unused dependencies per 1,000 files. Null if dead code analysis did not
/// run.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub unused_deps_per_k_files: Option<f64>,
/// Number of circular dependency chains.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub circular_dep_count: Option<u32>,
/// Circular dependency chains per 1,000 files. Null if dead code analysis
/// did not run.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub circular_deps_per_k_files: Option<f64>,
/// Raw counts backing the percentages (for orientation header display).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub counts: Option<VitalSignsCounts>,
/// Function size risk profile: percentage of functions in each size bin.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub unit_size_profile: Option<RiskProfile>,
/// Functions above 60 LOC per 1,000 functions. Null if no functions
/// analyzed.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub functions_over_60_loc_per_k: Option<f64>,
/// Parameter count risk profile: percentage of functions in each param bin.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub unit_interfacing_profile: Option<RiskProfile>,
/// 95th percentile fan-in across all files. Null if file scores not
/// computed.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub p95_fan_in: Option<u32>,
/// Percentage of files with fan-in above the project's p95 threshold.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub coupling_high_pct: Option<f64>,
/// Number of located prop-drilling chains (React/Preact props forwarded
/// unchanged through 3+ pass-through components). `None` unless the opt-in
/// `prop-drilling` rule is enabled (it defaults to off), so the small capped
/// penalty and the hotspot surface are dormant by default.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub prop_drilling_chain_count: Option<u32>,
/// The deepest located prop-drilling chain's depth (forwarding hops). `None`
/// when no chains were found or the rule is off. Descriptive context only.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub prop_drilling_max_depth: Option<u32>,
/// 95th-percentile DISTINCT-PARENTS render fan-in across React/Preact
/// components (the component-graph analogue of `p95_fan_in`, which percentiles
/// per-FILE module fan-in). `None` on non-React runs. Descriptive
/// blast-radius context, NOT a gate. Mirrors `compute_coupling_concentration`.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub p95_render_fan_in: Option<u32>,
/// Percentage of components whose render fan-in exceeds the project's
/// `max(p95, 10)` threshold (reuses the coupling-concentration floor; NO new
/// tunable constant). `None` on non-React runs. Mirrors `coupling_high_pct`.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub render_fan_in_high_pct: Option<f64>,
/// The single highest DISTINCT-PARENTS count across all components (the
/// headline blast-radius number: the most distinct render LOCATIONS any one
/// component is rendered from, the honest edit-ripple count). `render_sites`
/// (incl. repeats) is secondary per-component context, never the headline.
/// `None` on non-React runs. Descriptive context, no threshold.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub max_render_fan_in: Option<u32>,
/// The highest-fan-in React/Preact components, located (component name +
/// project-relative path + render-site / distinct-parent counts), sorted by
/// distinct parents (the honest headline axis) descending, tie-broken on
/// render sites descending, and capped at a small N. Lets a consumer see
/// WHICH component carries the headline `max_render_fan_in`, not just the
/// number. Empty (and omitted from JSON) on non-React runs, so the contract
/// stays byte-identical there. Descriptive blast-radius context, NOT a gate.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub top_render_fan_in: Vec<RenderFanInTopComponent>,
/// Total lines of code across all parsed modules.
#[serde(default)]
pub total_loc: u64,
}
/// Disjoint populations feeding the cyclomatic distribution. Counts and sums
/// across all three groups reconstruct its weighted mean. Module-scope units
/// contribute to aggregates only and do not produce function findings.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct CyclomaticPopulation {
/// Authored functions, excluding synthetic module and template units.
pub functions: CyclomaticUnitPopulation,
/// Synthetic module-scope units, emitted only when top-level code branches.
pub modules: CyclomaticUnitPopulation,
/// Synthetic template and snippet units.
pub templates: CyclomaticUnitPopulation,
}
/// Cyclomatic measurements for one kind of complexity unit.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct CyclomaticUnitPopulation {
/// Number of units measured, including those below finding thresholds.
pub count: usize,
/// Sum of cyclomatic complexity, before rounding or threshold filtering.
pub sum: u64,
/// Highest cyclomatic complexity, or null when this population is empty.
pub max: Option<u16>,
}
/// One located high-fan-in React/Preact component for the descriptive
/// `top_render_fan_in` blast-radius list on [`VitalSigns`].
///
/// The component-graph analogue of a high-fan-in module: `distinct_parents` is
/// the HEADLINE axis (the honest count of distinct parent components / render
/// LOCATIONS that render this component), `render_sites` is secondary "incl.
/// repeats" context (every JSX render SITE, so a single parent rendering one
/// child five times is five sites but one parent). Undercount-safe like the
/// underlying metric: a child rendered via a JSX spread / dynamic /
/// member-expression tag resolves to no component, so a true high-fan-in
/// component can only be undersold.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct RenderFanInTopComponent {
/// The component name.
pub component: String,
/// Project-relative path of the file declaring the component. Serialized with
/// forward slashes (same serializer the other relativized health paths use).
#[serde(serialize_with = "fallow_types::serde_path::serialize")]
pub path: std::path::PathBuf,
/// Total JSX render SITES that resolve to this component across the project.
/// SECONDARY "incl. repeats" context, not the headline (see `distinct_parents`).
pub render_sites: u32,
/// Distinct `(parent_file, parent_component)` keys that render this component.
/// The HEADLINE blast-radius axis: distinct render LOCATIONS.
pub distinct_parents: u32,
}
/// Risk profile: percentage of functions in each risk bin.
///
/// Bins are defined by thresholds that depend on the measured property:
/// - **Unit size**: low risk (1-15 LOC), medium risk (16-30), high risk (31-60), very high risk (>60)
/// - **Unit interfacing**: low risk (0-2 params), medium risk (3-4), high risk (5-6), very high risk (>=7)
///
/// Percentages sum to approximately 100.0 (subject to rounding).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct RiskProfile {
/// Percentage of functions in the low-risk bin.
pub low_risk: f64,
/// Percentage of functions in the medium-risk bin.
pub medium_risk: f64,
/// Percentage of functions in the high-risk bin.
pub high_risk: f64,
/// Percentage of functions in the very-high-risk bin.
pub very_high_risk: f64,
}
/// Raw counts backing the vital signs percentages.
///
/// Stored alongside `VitalSigns` in snapshots so that Phase 2b trend reporting
/// can decompose percentage changes into numerator vs denominator shifts.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct VitalSignsCounts {
/// Total number of discovered source files.
pub total_files: usize,
/// Total number of exports across all files.
pub total_exports: usize,
/// Number of unreachable files.
pub dead_files: usize,
/// Number of unused exports.
pub dead_exports: usize,
/// Lines inside detected clones; absent when duplication did not run.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub duplicated_lines: Option<usize>,
/// Lines scanned by duplication; absent when duplication did not run.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub total_lines: Option<usize>,
/// Files with a computed maintainability score; absent when file scoring
/// did not run.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub files_scored: Option<usize>,
/// Total declared dependencies across manifest sections.
pub total_deps: usize,
}
/// A point-in-time snapshot of project vital signs, persisted to disk.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct VitalSignsSnapshot {
/// Schema version for snapshot format (independent of report schema_version).
pub snapshot_schema_version: u32,
/// Fallow version that produced this snapshot.
pub version: String,
/// ISO 8601 timestamp.
pub timestamp: String,
/// Git commit SHA at time of snapshot (None if not in a git repo).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub git_sha: Option<String>,
/// Git branch name (None if not in a git repo or detached HEAD).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub git_branch: Option<String>,
/// Whether the repository is a shallow clone.
#[serde(default)]
pub shallow_clone: bool,
/// The vital signs metrics.
pub vital_signs: VitalSigns,
/// Raw counts for trend decomposition.
pub counts: VitalSignsCounts,
/// Project health score (0-100). Added in schema v2.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub score: Option<f64>,
/// Letter grade (A/B/C/D/F). Added in schema v2.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub grade: Option<String>,
/// Formula used for the stored project and group scores. Absent on legacy
/// snapshots and when no score was computed; never inferred from tool version.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub score_formula_version: Option<u32>,
/// Coverage model used for CRAP computation. Added in schema v3.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub coverage_model: Option<CoverageModel>,
/// Compatibility identity for the analysis that produced this snapshot.
/// Legacy snapshots deserialize as syntactic analysis.
#[serde(default)]
pub analysis_identity: fallow_types::semantic::SemanticAnalysisIdentity,
/// Per-group data of a `--group-by` run. Added in schema v11. Absent on
/// ungrouped runs and on older snapshots.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub groups: Option<SnapshotGrouping>,
}
/// Group data stored in a snapshot of a `--group-by` run.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SnapshotGrouping {
/// Grouping mode label (`owner`, `directory`, `package`, `section`).
pub grouped_by: String,
/// The `--group` selector patterns of the run, when given. A group that the
/// selector removed is not in `groups`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub group_filter: Option<Vec<String>>,
/// One entry per group that the run kept.
pub groups: Vec<GroupSnapshot>,
}
/// Stored metrics of one group.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct GroupSnapshot {
/// Group key produced by the resolver.
pub key: String,
/// Files in this group.
pub files_analyzed: usize,
/// The vital signs of this group.
pub vital_signs: VitalSigns,
/// Raw counts of this group for trend decomposition.
pub counts: VitalSignsCounts,
/// Group health score (0-100), when the run computed a score.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub score: Option<f64>,
/// Group letter grade, when the run computed a score.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub grade: Option<String>,
/// Critical-severity findings in this group, before `--top`.
#[serde(default)]
pub severity_critical_count: usize,
/// Ranked hotspot entries in this group, before `--top`.
#[serde(default)]
pub hotspot_count: usize,
}
#[cfg(test)]
#[expect(
clippy::unwrap_used,
reason = "tests use unwrap to keep serialization assertions concise"
)]
mod tests {
use super::*;
#[test]
fn legacy_vital_signs_preserve_unknown_cyclomatic_population() {
let legacy: VitalSigns =
serde_json::from_str(r#"{"avg_cyclomatic":16.0,"p90_cyclomatic":31}"#).unwrap();
assert!(legacy.cyclomatic_population.is_none());
assert!(
serde_json::to_value(legacy)
.unwrap()
.get("cyclomatic_population")
.is_none()
);
}
#[test]
fn vital_signs_optional_fields_are_omitted() {
let vs = VitalSigns {
avg_cyclomatic: 5.0,
p90_cyclomatic: 10,
..VitalSigns::default()
};
let json = serde_json::to_string(&vs).unwrap();
assert!(json.contains("avg_cyclomatic"));
assert!(json.contains("p90_cyclomatic"));
assert!(!json.contains("dead_file_pct"));
assert!(!json.contains("top_render_fan_in"));
}
#[test]
fn snapshot_deserializes_old_shape_with_default_score_and_grade() {
let json = r#"{
"snapshot_schema_version": 1,
"version": "1.5.0",
"timestamp": "2025-01-01T00:00:00Z",
"shallow_clone": false,
"vital_signs": {
"avg_cyclomatic": 2.0,
"p90_cyclomatic": 5
},
"counts": {
"total_files": 100,
"total_exports": 500,
"dead_files": 0,
"dead_exports": 0,
"total_deps": 20
}
}"#;
let snap: VitalSignsSnapshot = serde_json::from_str(json).unwrap();
assert!(snap.score.is_none());
assert!(snap.grade.is_none());
assert_eq!(snap.snapshot_schema_version, 1);
}
}