dart-decimate 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/Cargo.lock +572 -0
  2. package/Cargo.toml +41 -0
  3. package/LICENSE +21 -0
  4. package/README.md +617 -0
  5. package/assets/dart-decimate-banner.png +0 -0
  6. package/npm/bin/dart-decimate-mcp.js +48 -0
  7. package/npm/bin/dart-decimate.js +48 -0
  8. package/npm/scripts/postinstall.js +46 -0
  9. package/npm/scripts/test-npx-local.js +54 -0
  10. package/npm/scripts/test-npx-mcp-local.js +56 -0
  11. package/package.json +45 -0
  12. package/src/baseline.rs +643 -0
  13. package/src/bin/dart-decimate-mcp.rs +11 -0
  14. package/src/boundaries/tests.rs +60 -0
  15. package/src/boundaries.rs +317 -0
  16. package/src/changed_scope.rs +117 -0
  17. package/src/ci_template.rs +399 -0
  18. package/src/cli/analyze.rs +462 -0
  19. package/src/cli/analyzer_options.rs +82 -0
  20. package/src/cli/audit_run.rs +327 -0
  21. package/src/cli/boundary_args.rs +44 -0
  22. package/src/cli/ci_template_run.rs +196 -0
  23. package/src/cli/command_name.rs +20 -0
  24. package/src/cli/common_args.rs +167 -0
  25. package/src/cli/config_run.rs +78 -0
  26. package/src/cli/coverage_run.rs +393 -0
  27. package/src/cli/decision_surface_run.rs +99 -0
  28. package/src/cli/default_command.rs +69 -0
  29. package/src/cli/dupes_args.rs +150 -0
  30. package/src/cli/entry_points.rs +121 -0
  31. package/src/cli/error.rs +149 -0
  32. package/src/cli/error_output.rs +68 -0
  33. package/src/cli/explain_run.rs +72 -0
  34. package/src/cli/fix_run.rs +162 -0
  35. package/src/cli/flags_args.rs +23 -0
  36. package/src/cli/health_args.rs +200 -0
  37. package/src/cli/hooks_run.rs +191 -0
  38. package/src/cli/impact_run.rs +95 -0
  39. package/src/cli/init_run.rs +49 -0
  40. package/src/cli/inspect_args.rs +20 -0
  41. package/src/cli/inspect_run.rs +66 -0
  42. package/src/cli/issue_filter_args.rs +240 -0
  43. package/src/cli/list_run.rs +311 -0
  44. package/src/cli/mode_args.rs +72 -0
  45. package/src/cli/output_format.rs +62 -0
  46. package/src/cli/regression_args.rs +156 -0
  47. package/src/cli/request_args.rs +175 -0
  48. package/src/cli/schema_commands.rs +72 -0
  49. package/src/cli/scope_args.rs +174 -0
  50. package/src/cli/security_args.rs +175 -0
  51. package/src/cli/security_gate_run.rs +66 -0
  52. package/src/cli/security_summary_run.rs +43 -0
  53. package/src/cli/summary_args.rs +12 -0
  54. package/src/cli/tests.rs +516 -0
  55. package/src/cli/trace_args.rs +67 -0
  56. package/src/cli/trace_run.rs +131 -0
  57. package/src/cli/trace_tests.rs +235 -0
  58. package/src/cli/unsupported_run.rs +116 -0
  59. package/src/cli/watch_run.rs +153 -0
  60. package/src/cli.rs +603 -0
  61. package/src/config/boundary_config.rs +235 -0
  62. package/src/config/cache.rs +25 -0
  63. package/src/config/dependencies.rs +114 -0
  64. package/src/config/health.rs +237 -0
  65. package/src/config/jsonc.rs +66 -0
  66. package/src/config/policy.rs +91 -0
  67. package/src/config/rule_aliases.rs +487 -0
  68. package/src/config/rules.rs +339 -0
  69. package/src/config.rs +635 -0
  70. package/src/coverage.rs +469 -0
  71. package/src/dart_parser.rs +673 -0
  72. package/src/decision_surface.rs +405 -0
  73. package/src/dependencies/analyze.rs +218 -0
  74. package/src/dependencies/codegen.rs +118 -0
  75. package/src/dependencies/discovery.rs +60 -0
  76. package/src/dependencies/overrides.rs +76 -0
  77. package/src/dependencies/private_src_imports.rs +32 -0
  78. package/src/dependencies/pubspec_document.rs +86 -0
  79. package/src/dependencies/pubspec_entry.rs +96 -0
  80. package/src/dependencies/tests.rs +544 -0
  81. package/src/dependencies/usage.rs +33 -0
  82. package/src/dependencies.rs +553 -0
  83. package/src/dependency_scripts.rs +91 -0
  84. package/src/dupes/lex.rs +236 -0
  85. package/src/dupes/tests.rs +119 -0
  86. package/src/dupes.rs +625 -0
  87. package/src/explain.rs +692 -0
  88. package/src/extract/directives.rs +252 -0
  89. package/src/extract/error_tests.rs +25 -0
  90. package/src/extract/members.rs +210 -0
  91. package/src/extract/primary_constructors.rs +421 -0
  92. package/src/extract/references.rs +151 -0
  93. package/src/extract/routes.rs +691 -0
  94. package/src/extract/signatures.rs +266 -0
  95. package/src/extract/string_tests.rs +18 -0
  96. package/src/extract/strings.rs +107 -0
  97. package/src/extract/tests.rs +697 -0
  98. package/src/extract.rs +680 -0
  99. package/src/feature_flags/tests.rs +76 -0
  100. package/src/feature_flags.rs +453 -0
  101. package/src/finding_identity.rs +60 -0
  102. package/src/fix.rs +551 -0
  103. package/src/generated.rs +53 -0
  104. package/src/graph/augment_tests.rs +212 -0
  105. package/src/graph/conditionals.rs +74 -0
  106. package/src/graph/package_config_tests.rs +100 -0
  107. package/src/graph/parts.rs +248 -0
  108. package/src/graph/pubspec_tests.rs +119 -0
  109. package/src/graph/tests.rs +837 -0
  110. package/src/graph/uri_tests.rs +102 -0
  111. package/src/graph.rs +644 -0
  112. package/src/health/coverage.rs +309 -0
  113. package/src/health/ownership.rs +175 -0
  114. package/src/health/runtime_coverage.rs +616 -0
  115. package/src/health/runtime_intelligence.rs +511 -0
  116. package/src/health/scores.rs +296 -0
  117. package/src/health/tests.rs +240 -0
  118. package/src/health/threshold_types.rs +163 -0
  119. package/src/health/thresholds.rs +229 -0
  120. package/src/health/types.rs +636 -0
  121. package/src/health.rs +597 -0
  122. package/src/hooks/agent_settings.rs +175 -0
  123. package/src/hooks/templates.rs +51 -0
  124. package/src/hooks.rs +569 -0
  125. package/src/impact.rs +287 -0
  126. package/src/init.rs +237 -0
  127. package/src/inspect.rs +142 -0
  128. package/src/intelligence/tests.rs +232 -0
  129. package/src/intelligence.rs +305 -0
  130. package/src/lib.rs +198 -0
  131. package/src/main.rs +5 -0
  132. package/src/manifest/mcp.rs +565 -0
  133. package/src/manifest.rs +552 -0
  134. package/src/mcp/cli_args/value_args.rs +175 -0
  135. package/src/mcp/cli_args.rs +658 -0
  136. package/src/mcp/cli_args_tests.rs +522 -0
  137. package/src/mcp/code_execute.rs +671 -0
  138. package/src/mcp/tools.rs +660 -0
  139. package/src/mcp.rs +325 -0
  140. package/src/output/audit_risk.rs +135 -0
  141. package/src/output/dependency_findings.rs +327 -0
  142. package/src/output/duplication_findings.rs +114 -0
  143. package/src/output/feature_flag_findings.rs +122 -0
  144. package/src/output/format.rs +31 -0
  145. package/src/output/graph_findings.rs +461 -0
  146. package/src/output/health_findings.rs +612 -0
  147. package/src/output/human.rs +27 -0
  148. package/src/output/next_steps.rs +92 -0
  149. package/src/output/route_findings.rs +76 -0
  150. package/src/output/runtime_coverage.rs +515 -0
  151. package/src/output/scope.rs +183 -0
  152. package/src/output/security_findings.rs +390 -0
  153. package/src/output/security_sarif.rs +150 -0
  154. package/src/output/suppressions.rs +323 -0
  155. package/src/output/symbol_findings.rs +257 -0
  156. package/src/output/tests.rs +114 -0
  157. package/src/output/types.rs +604 -0
  158. package/src/output/widget_findings.rs +213 -0
  159. package/src/output.rs +682 -0
  160. package/src/package_map.rs +495 -0
  161. package/src/policy/packs.rs +232 -0
  162. package/src/policy/tests.rs +134 -0
  163. package/src/policy.rs +640 -0
  164. package/src/project_list.rs +376 -0
  165. package/src/report_schema/inventory.rs +418 -0
  166. package/src/report_schema.rs +488 -0
  167. package/src/routes.rs +354 -0
  168. package/src/scan/tests.rs +250 -0
  169. package/src/scan.rs +275 -0
  170. package/src/security/detect.rs +639 -0
  171. package/src/security/tests.rs +178 -0
  172. package/src/security.rs +373 -0
  173. package/src/security_gate.rs +555 -0
  174. package/src/symbols/path_filters.rs +27 -0
  175. package/src/symbols/private_type_leaks.rs +125 -0
  176. package/src/symbols/riverpod.rs +113 -0
  177. package/src/symbols/tests.rs +692 -0
  178. package/src/symbols.rs +693 -0
  179. package/src/trace/tests.rs +225 -0
  180. package/src/trace.rs +690 -0
  181. package/src/unsupported.rs +47 -0
  182. package/src/widgets/lifecycle.rs +484 -0
  183. package/src/widgets/params.rs +236 -0
  184. package/src/widgets/tests.rs +430 -0
  185. package/src/widgets/top_level.rs +110 -0
  186. package/src/widgets/unrendered.rs +374 -0
  187. package/src/widgets.rs +590 -0
  188. package/src/workspace_scope.rs +358 -0
@@ -0,0 +1,92 @@
1
+ use std::path::Path;
2
+
3
+ use super::format::display_path;
4
+ use super::{AnalysisResults, NextStep};
5
+ use crate::DeclarationKind;
6
+
7
+ pub(super) fn next_steps(root: &Path, results: &AnalysisResults) -> Vec<NextStep> {
8
+ let mut steps = Vec::new();
9
+
10
+ if let Some(unused) = results.symbols.as_ref().and_then(|report| {
11
+ report
12
+ .unused_exports
13
+ .iter()
14
+ .find(|unused| unused.kind != DeclarationKind::TypeAlias)
15
+ }) {
16
+ steps.push(NextStep {
17
+ id: "trace-unused-export".to_owned(),
18
+ command: format!(
19
+ "dart-decimate trace-symbol --format json --symbol {}:{}",
20
+ display_path(root, &unused.path),
21
+ unused.name
22
+ ),
23
+ reason: "Trace the first unused export before deleting or suppressing it".to_owned(),
24
+ });
25
+ }
26
+
27
+ if let Some(unused) = results.symbols.as_ref().and_then(|report| {
28
+ report
29
+ .unused_exports
30
+ .iter()
31
+ .find(|unused| unused.kind == DeclarationKind::TypeAlias)
32
+ }) {
33
+ steps.push(NextStep {
34
+ id: "trace-unused-type".to_owned(),
35
+ command: format!(
36
+ "dart-decimate trace-symbol --format json --symbol {}:{}",
37
+ display_path(root, &unused.path),
38
+ unused.name
39
+ ),
40
+ reason: "Trace the first unused type alias before deleting or suppressing it"
41
+ .to_owned(),
42
+ });
43
+ }
44
+
45
+ if let Some(unused) = results
46
+ .dependency_hygiene
47
+ .as_ref()
48
+ .and_then(|report| report.unused_dependencies.first())
49
+ {
50
+ steps.push(NextStep {
51
+ id: "trace-unused-dependency".to_owned(),
52
+ command: format!(
53
+ "dart-decimate trace-dependency --format json --dependency {}",
54
+ unused.dependency
55
+ ),
56
+ reason: "Trace the first unused dependency before editing pubspec.yaml".to_owned(),
57
+ });
58
+ }
59
+
60
+ if let Some(clone) = results
61
+ .duplicates
62
+ .as_ref()
63
+ .and_then(|report| report.clone_groups.first())
64
+ {
65
+ steps.push(NextStep {
66
+ id: "trace-code-duplication".to_owned(),
67
+ command: format!(
68
+ "dart-decimate trace-clone --format json --fingerprint {}",
69
+ clone.fingerprint
70
+ ),
71
+ reason: "Trace the first duplicate code group before extracting shared code".to_owned(),
72
+ });
73
+ }
74
+
75
+ if let Some(health) = &results.health
76
+ && let Some(complexity) = health.complexity.first()
77
+ {
78
+ steps.push(NextStep {
79
+ id: "complexity-breakdown".to_owned(),
80
+ command: format!(
81
+ "dart-decimate health --format json --complexity-breakdown --top 1 --max-cyclomatic {} --max-cognitive {}",
82
+ health.options.max_cyclomatic, health.options.max_cognitive
83
+ ),
84
+ reason: format!(
85
+ "Explain the decision points driving complexity in {}",
86
+ display_path(root, &complexity.path)
87
+ ),
88
+ });
89
+ }
90
+
91
+ steps
92
+ }
@@ -0,0 +1,76 @@
1
+ use std::collections::BTreeSet;
2
+ use std::path::Path;
3
+
4
+ use super::format::display_path;
5
+ use super::{Finding, FindingAction, FindingKind, Severity};
6
+ use crate::{RouteCollision, RouteCollisionKind, RouteCollisionReport};
7
+
8
+ pub(super) fn add_route_findings(
9
+ root: &Path,
10
+ report: &RouteCollisionReport,
11
+ findings: &mut Vec<Finding>,
12
+ ) {
13
+ findings.extend(
14
+ report
15
+ .collisions
16
+ .iter()
17
+ .filter_map(|collision| route_collision_finding(root, collision)),
18
+ );
19
+ }
20
+
21
+ fn route_collision_finding(root: &Path, collision: &RouteCollision) -> Option<Finding> {
22
+ let primary = collision
23
+ .declarations
24
+ .get(1)
25
+ .or_else(|| collision.declarations.first())?;
26
+ let path = display_path(root, &primary.path);
27
+ let files = collision
28
+ .declarations
29
+ .iter()
30
+ .map(|declaration| display_path(root, &declaration.path))
31
+ .collect::<BTreeSet<_>>()
32
+ .into_iter()
33
+ .collect::<Vec<_>>();
34
+ let label = match collision.kind {
35
+ RouteCollisionKind::Path => "path",
36
+ RouteCollisionKind::Name => "name",
37
+ };
38
+
39
+ Some(Finding {
40
+ rule_id: "dart-decimate/route-collision".to_owned(),
41
+ fingerprint: Some(route_collision_fingerprint(collision)),
42
+ kind: FindingKind::RouteCollision,
43
+ severity: Severity::Error,
44
+ message: format!(
45
+ "GoRouter route {label} {} is declared by {} routes",
46
+ collision.value,
47
+ collision.declarations.len()
48
+ ),
49
+ path: path.clone(),
50
+ line: primary.location.line,
51
+ column: primary.location.column,
52
+ safe_to_delete: false,
53
+ files,
54
+ edge: None,
55
+ actions: vec![
56
+ FindingAction::new(
57
+ "review-route-collision",
58
+ "Rename or move one route so GoRouter paths remain unique",
59
+ false,
60
+ )
61
+ .with_target_path(path.clone())
62
+ .with_target_symbol(primary.route_class.clone())
63
+ .with_dart_decimate_args(["inspect", "--format", "json", "--file", path.as_str()])
64
+ .with_suppression_comment("// dart-decimate-ignore-next-line route-collision"),
65
+ ],
66
+ })
67
+ }
68
+
69
+ fn route_collision_fingerprint(collision: &RouteCollision) -> String {
70
+ let mut hash = 0xcbf2_9ce4_8422_2325_u64;
71
+ for byte in format!("{:?}:{}", collision.kind, collision.value).as_bytes() {
72
+ hash ^= u64::from(*byte);
73
+ hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
74
+ }
75
+ format!("route:{:08x}", hash & 0xffff_ffff)
76
+ }
@@ -0,0 +1,515 @@
1
+ use std::path::Path;
2
+
3
+ use serde::{Deserialize, Serialize};
4
+
5
+ use super::format::display_path;
6
+ use crate::{
7
+ LowTrafficThreshold, RuntimeBlastRadius, RuntimeBlastRisk, RuntimeCoverageAction,
8
+ RuntimeCoverageConfidence, RuntimeCoverageFinding, RuntimeCoverageFindingKind,
9
+ RuntimeCoverageFormat, RuntimeCoverageIntelligence, RuntimeCoverageIntelligenceKind,
10
+ RuntimeCoverageReport, RuntimeHotPath, RuntimeImportance, SourceMapConfidence,
11
+ };
12
+
13
+ /// Runtime coverage intelligence serialized in JSON reports.
14
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15
+ pub struct JsonRuntimeCoverage {
16
+ /// Runtime coverage verdict.
17
+ pub verdict: String,
18
+ /// High-level runtime coverage signals present in this report.
19
+ pub signals: Vec<String>,
20
+ /// Runtime coverage summary.
21
+ pub summary: JsonRuntimeCoverageSummary,
22
+ /// Runtime cleanup/review signals.
23
+ pub findings: Vec<JsonRuntimeCoverageFinding>,
24
+ /// Runtime hot paths.
25
+ pub hot_paths: Vec<JsonRuntimeHotPath>,
26
+ /// Static/runtime recommendations for agent review.
27
+ pub coverage_intelligence: Vec<JsonRuntimeCoverageIntelligence>,
28
+ /// Caller blast-radius rows for hot runtime paths.
29
+ pub blast_radius: Vec<JsonRuntimeBlastRadius>,
30
+ /// Runtime-weighted production importance rows.
31
+ pub importance: Vec<JsonRuntimeImportance>,
32
+ /// Agent-friendly candidate buckets.
33
+ pub actionable: JsonRuntimeCoverageActionable,
34
+ /// Runtime coverage input provenance.
35
+ pub provenance: JsonRuntimeCoverageProvenance,
36
+ /// Observation watermark.
37
+ pub watermark: JsonRuntimeCoverageWatermark,
38
+ /// Non-fatal runtime coverage warnings.
39
+ pub warnings: Vec<String>,
40
+ }
41
+
42
+ /// Runtime coverage summary.
43
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
44
+ pub struct JsonRuntimeCoverageSummary {
45
+ /// Files observed in runtime coverage.
46
+ pub observed_files: usize,
47
+ /// Total observed runtime invocations.
48
+ pub total_invocations: usize,
49
+ /// Runtime hot path count.
50
+ pub hot_paths: usize,
51
+ /// Runtime finding count.
52
+ pub findings: usize,
53
+ /// `safe_to_delete` finding count.
54
+ pub safe_to_delete: usize,
55
+ /// `review_required` finding count.
56
+ pub review_required: usize,
57
+ /// Low-traffic finding count.
58
+ pub low_traffic: usize,
59
+ /// Coverage-unavailable finding count.
60
+ pub coverage_unavailable: usize,
61
+ /// Hot path threshold used for this run.
62
+ pub min_invocations_hot: usize,
63
+ /// Observation volume threshold used for this run.
64
+ pub min_observation_volume: usize,
65
+ /// Low-traffic threshold used for this run.
66
+ pub low_traffic_threshold: LowTrafficThreshold,
67
+ }
68
+
69
+ /// Runtime coverage finding serialized in JSON reports.
70
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
71
+ pub struct JsonRuntimeCoverageFinding {
72
+ /// Stable finding id.
73
+ pub id: String,
74
+ /// Runtime finding kind.
75
+ pub kind: String,
76
+ /// Dart file path, root-relative where possible.
77
+ pub path: String,
78
+ /// 1-based line.
79
+ pub line: usize,
80
+ /// Observed runtime invocations.
81
+ pub invocations: usize,
82
+ /// Fraction of all runtime observations.
83
+ pub traffic_fraction: LowTrafficThreshold,
84
+ /// Whether graph plus runtime evidence supports deletion.
85
+ pub safe_to_delete: bool,
86
+ /// Whether a human or agent should review before changing.
87
+ pub review_required: bool,
88
+ /// Runtime confidence for the signal.
89
+ pub confidence: String,
90
+ /// Agent-readable reason.
91
+ pub reason: String,
92
+ }
93
+
94
+ /// Runtime hot path serialized in JSON reports.
95
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
96
+ pub struct JsonRuntimeHotPath {
97
+ /// Dart file path, root-relative where possible.
98
+ pub path: String,
99
+ /// 1-based line when available.
100
+ pub line: Option<usize>,
101
+ /// Runtime function name when available.
102
+ pub symbol: Option<String>,
103
+ /// Observed runtime invocations.
104
+ pub invocations: usize,
105
+ /// Source map confidence.
106
+ pub source_map_confidence: String,
107
+ }
108
+
109
+ /// Runtime coverage recommendation serialized in JSON reports.
110
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
111
+ pub struct JsonRuntimeCoverageIntelligence {
112
+ /// Stable recommendation id.
113
+ pub id: String,
114
+ /// Cross-surface function id.
115
+ pub stable_id: String,
116
+ /// Recommendation kind.
117
+ pub kind: String,
118
+ /// Dart file path, root-relative where possible.
119
+ pub path: String,
120
+ /// 1-based line when available.
121
+ pub line: Option<usize>,
122
+ /// Runtime function name when available.
123
+ pub symbol: Option<String>,
124
+ /// Suggested action.
125
+ pub action: String,
126
+ /// Priority from 0 to 100.
127
+ pub priority: usize,
128
+ /// Runtime confidence for the recommendation.
129
+ pub confidence: String,
130
+ /// Agent-readable reason.
131
+ pub reason: String,
132
+ /// Supporting signal labels.
133
+ pub signals: Vec<String>,
134
+ }
135
+
136
+ /// Runtime blast-radius row serialized in JSON reports.
137
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
138
+ pub struct JsonRuntimeBlastRadius {
139
+ /// Stable blast-radius id.
140
+ pub id: String,
141
+ /// Cross-surface function id.
142
+ pub stable_id: String,
143
+ /// Dart file path, root-relative where possible.
144
+ pub path: String,
145
+ /// 1-based line when available.
146
+ pub line: Option<usize>,
147
+ /// Runtime function name when available.
148
+ pub symbol: Option<String>,
149
+ /// Number of graph callers.
150
+ pub caller_count: usize,
151
+ /// Direct graph callers.
152
+ pub callers: Vec<String>,
153
+ /// Fraction of runtime traffic observed in caller files.
154
+ pub traffic_weighted_caller_reach: LowTrafficThreshold,
155
+ /// Low, medium, or high review risk.
156
+ pub risk: String,
157
+ }
158
+
159
+ /// Runtime importance row serialized in JSON reports.
160
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
161
+ pub struct JsonRuntimeImportance {
162
+ /// Stable importance id.
163
+ pub id: String,
164
+ /// Cross-surface function id.
165
+ pub stable_id: String,
166
+ /// Dart file path, root-relative where possible.
167
+ pub path: String,
168
+ /// 1-based line when available.
169
+ pub line: Option<usize>,
170
+ /// Runtime function name when available.
171
+ pub symbol: Option<String>,
172
+ /// Observed runtime invocations.
173
+ pub invocations: usize,
174
+ /// Fraction of total runtime traffic.
175
+ pub traffic_fraction: LowTrafficThreshold,
176
+ /// Static cyclomatic complexity for the matching function.
177
+ pub cyclomatic_complexity: usize,
178
+ /// Number of graph callers.
179
+ pub caller_count: usize,
180
+ /// Runtime-weighted priority score.
181
+ pub score: usize,
182
+ /// Agent-readable reason.
183
+ pub reason: String,
184
+ }
185
+
186
+ /// Agent-friendly runtime coverage candidate buckets.
187
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
188
+ pub struct JsonRuntimeCoverageActionable {
189
+ /// Finding ids that are safe to delete.
190
+ pub safe_to_delete: Vec<String>,
191
+ /// Finding ids that require review before changing.
192
+ pub review_required: Vec<String>,
193
+ /// Low-traffic finding ids.
194
+ pub low_traffic: Vec<String>,
195
+ /// Coverage-unavailable finding ids.
196
+ pub coverage_unavailable: Vec<String>,
197
+ }
198
+
199
+ /// Runtime coverage input provenance.
200
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
201
+ pub struct JsonRuntimeCoverageProvenance {
202
+ /// Runtime coverage input path.
203
+ pub source_path: String,
204
+ /// Runtime coverage source format.
205
+ pub source_format: String,
206
+ /// Stable hash of the coverage input payload.
207
+ pub source_hash: String,
208
+ /// Local or cloud capture mode.
209
+ pub mode: String,
210
+ /// Capture quality derived from observation volume.
211
+ pub capture_quality: String,
212
+ }
213
+
214
+ /// Runtime coverage observation watermark.
215
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
216
+ pub struct JsonRuntimeCoverageWatermark {
217
+ /// Files observed in runtime coverage.
218
+ pub observed_files: usize,
219
+ /// Total observed runtime invocations.
220
+ pub total_invocations: usize,
221
+ }
222
+
223
+ #[must_use]
224
+ pub fn json_runtime_coverage(root: &Path, report: &RuntimeCoverageReport) -> JsonRuntimeCoverage {
225
+ let findings = report
226
+ .findings
227
+ .iter()
228
+ .map(|finding| json_finding(root, finding))
229
+ .collect::<Vec<_>>();
230
+ let hot_paths = report
231
+ .hot_paths
232
+ .iter()
233
+ .map(|path| json_hot_path(root, path))
234
+ .collect::<Vec<_>>();
235
+ let coverage_intelligence = report
236
+ .coverage_intelligence
237
+ .iter()
238
+ .map(|row| json_coverage_intelligence(root, row))
239
+ .collect();
240
+ let blast_radius = report
241
+ .blast_radius
242
+ .iter()
243
+ .map(|row| json_blast_radius(root, row))
244
+ .collect();
245
+ let importance = report
246
+ .importance
247
+ .iter()
248
+ .map(|row| json_importance(root, row))
249
+ .collect();
250
+ JsonRuntimeCoverage {
251
+ verdict: runtime_verdict(report),
252
+ signals: runtime_signals(report),
253
+ summary: runtime_summary(report),
254
+ actionable: runtime_actionable(&findings),
255
+ provenance: JsonRuntimeCoverageProvenance {
256
+ source_path: display_path(root, &report.source_path),
257
+ source_format: source_format(report.source_format).to_owned(),
258
+ source_hash: report.source_hash.clone(),
259
+ mode: "local".to_owned(),
260
+ capture_quality: capture_quality(report).to_owned(),
261
+ },
262
+ watermark: JsonRuntimeCoverageWatermark {
263
+ observed_files: report.observed_files,
264
+ total_invocations: report.total_invocations,
265
+ },
266
+ findings,
267
+ hot_paths,
268
+ coverage_intelligence,
269
+ blast_radius,
270
+ importance,
271
+ warnings: report.warnings.clone(),
272
+ }
273
+ }
274
+
275
+ fn runtime_summary(report: &RuntimeCoverageReport) -> JsonRuntimeCoverageSummary {
276
+ JsonRuntimeCoverageSummary {
277
+ observed_files: report.observed_files,
278
+ total_invocations: report.total_invocations,
279
+ hot_paths: report.hot_paths.len(),
280
+ findings: report.findings.len(),
281
+ safe_to_delete: report
282
+ .findings
283
+ .iter()
284
+ .filter(|finding| finding.safe_to_delete)
285
+ .count(),
286
+ review_required: report
287
+ .findings
288
+ .iter()
289
+ .filter(|finding| finding.review_required)
290
+ .count(),
291
+ low_traffic: report
292
+ .findings
293
+ .iter()
294
+ .filter(|finding| finding.kind == RuntimeCoverageFindingKind::LowTraffic)
295
+ .count(),
296
+ coverage_unavailable: report
297
+ .findings
298
+ .iter()
299
+ .filter(|finding| finding.kind == RuntimeCoverageFindingKind::CoverageUnavailable)
300
+ .count(),
301
+ min_invocations_hot: report.min_invocations_hot,
302
+ min_observation_volume: report.min_observation_volume,
303
+ low_traffic_threshold: report.low_traffic_threshold,
304
+ }
305
+ }
306
+
307
+ fn json_coverage_intelligence(
308
+ root: &Path,
309
+ row: &RuntimeCoverageIntelligence,
310
+ ) -> JsonRuntimeCoverageIntelligence {
311
+ JsonRuntimeCoverageIntelligence {
312
+ id: row.id.clone(),
313
+ stable_id: row.stable_id.clone(),
314
+ kind: intelligence_kind(row.kind).to_owned(),
315
+ path: display_path(root, &row.path),
316
+ line: row.line,
317
+ symbol: row.symbol.clone(),
318
+ action: runtime_action(row.action).to_owned(),
319
+ priority: row.priority,
320
+ confidence: confidence(row.confidence).to_owned(),
321
+ reason: row.reason.clone(),
322
+ signals: row.signals.clone(),
323
+ }
324
+ }
325
+
326
+ fn json_blast_radius(root: &Path, row: &RuntimeBlastRadius) -> JsonRuntimeBlastRadius {
327
+ JsonRuntimeBlastRadius {
328
+ id: row.id.clone(),
329
+ stable_id: row.stable_id.clone(),
330
+ path: display_path(root, &row.path),
331
+ line: row.line,
332
+ symbol: row.symbol.clone(),
333
+ caller_count: row.caller_count,
334
+ callers: row
335
+ .callers
336
+ .iter()
337
+ .map(|caller| display_path(root, caller))
338
+ .collect(),
339
+ traffic_weighted_caller_reach: row.traffic_weighted_caller_reach,
340
+ risk: blast_risk(row.risk).to_owned(),
341
+ }
342
+ }
343
+
344
+ fn json_importance(root: &Path, row: &RuntimeImportance) -> JsonRuntimeImportance {
345
+ JsonRuntimeImportance {
346
+ id: row.id.clone(),
347
+ stable_id: row.stable_id.clone(),
348
+ path: display_path(root, &row.path),
349
+ line: row.line,
350
+ symbol: row.symbol.clone(),
351
+ invocations: row.invocations,
352
+ traffic_fraction: row.traffic_fraction,
353
+ cyclomatic_complexity: row.cyclomatic_complexity,
354
+ caller_count: row.caller_count,
355
+ score: row.score,
356
+ reason: row.reason.clone(),
357
+ }
358
+ }
359
+
360
+ fn runtime_actionable(findings: &[JsonRuntimeCoverageFinding]) -> JsonRuntimeCoverageActionable {
361
+ JsonRuntimeCoverageActionable {
362
+ safe_to_delete: findings
363
+ .iter()
364
+ .filter(|finding| finding.safe_to_delete)
365
+ .map(|finding| finding.id.clone())
366
+ .collect(),
367
+ review_required: findings
368
+ .iter()
369
+ .filter(|finding| finding.review_required)
370
+ .map(|finding| finding.id.clone())
371
+ .collect(),
372
+ low_traffic: findings
373
+ .iter()
374
+ .filter(|finding| finding.kind == "low-traffic")
375
+ .map(|finding| finding.id.clone())
376
+ .collect(),
377
+ coverage_unavailable: findings
378
+ .iter()
379
+ .filter(|finding| finding.kind == "coverage-unavailable")
380
+ .map(|finding| finding.id.clone())
381
+ .collect(),
382
+ }
383
+ }
384
+
385
+ fn json_finding(root: &Path, finding: &RuntimeCoverageFinding) -> JsonRuntimeCoverageFinding {
386
+ let path = display_path(root, &finding.path);
387
+ JsonRuntimeCoverageFinding {
388
+ id: format!("runtime-coverage:{}:{path}", finding_kind(finding.kind)),
389
+ kind: finding_kind(finding.kind).to_owned(),
390
+ path,
391
+ line: finding.line,
392
+ invocations: finding.invocations,
393
+ traffic_fraction: finding.traffic_fraction,
394
+ safe_to_delete: finding.safe_to_delete,
395
+ review_required: finding.review_required,
396
+ confidence: confidence(finding.confidence).to_owned(),
397
+ reason: finding.reason.clone(),
398
+ }
399
+ }
400
+
401
+ fn json_hot_path(root: &Path, path: &RuntimeHotPath) -> JsonRuntimeHotPath {
402
+ JsonRuntimeHotPath {
403
+ path: display_path(root, &path.path),
404
+ line: path.line,
405
+ symbol: path.symbol.clone(),
406
+ invocations: path.invocations,
407
+ source_map_confidence: source_map_confidence(path.source_map_confidence).to_owned(),
408
+ }
409
+ }
410
+
411
+ fn runtime_verdict(report: &RuntimeCoverageReport) -> String {
412
+ if report.observed_files == 0 || !report.warnings.is_empty() {
413
+ "warn".to_owned()
414
+ } else {
415
+ "pass".to_owned()
416
+ }
417
+ }
418
+
419
+ fn runtime_signals(report: &RuntimeCoverageReport) -> Vec<String> {
420
+ let mut signals = vec!["runtime-coverage".to_owned()];
421
+ if !report.hot_paths.is_empty() {
422
+ signals.push("hot-paths".to_owned());
423
+ }
424
+ if !report.coverage_intelligence.is_empty() {
425
+ signals.push("coverage-intelligence".to_owned());
426
+ }
427
+ if !report.blast_radius.is_empty() {
428
+ signals.push("blast-radius".to_owned());
429
+ }
430
+ if !report.importance.is_empty() {
431
+ signals.push("importance".to_owned());
432
+ }
433
+ if report
434
+ .findings
435
+ .iter()
436
+ .any(|finding| finding.kind == RuntimeCoverageFindingKind::LowTraffic)
437
+ {
438
+ signals.push("low-traffic".to_owned());
439
+ }
440
+ if report
441
+ .findings
442
+ .iter()
443
+ .any(|finding| finding.kind == RuntimeCoverageFindingKind::CoverageUnavailable)
444
+ {
445
+ signals.push("coverage-unavailable".to_owned());
446
+ }
447
+ if report.total_invocations < report.min_observation_volume {
448
+ signals.push("low-observation-volume".to_owned());
449
+ }
450
+ signals
451
+ }
452
+
453
+ fn intelligence_kind(kind: RuntimeCoverageIntelligenceKind) -> &'static str {
454
+ match kind {
455
+ RuntimeCoverageIntelligenceKind::HotPathTouched => "hot-path-touched",
456
+ RuntimeCoverageIntelligenceKind::LowTraffic => "low-traffic",
457
+ RuntimeCoverageIntelligenceKind::CoverageUnavailable => "coverage-unavailable",
458
+ }
459
+ }
460
+
461
+ fn runtime_action(action: RuntimeCoverageAction) -> &'static str {
462
+ match action {
463
+ RuntimeCoverageAction::ReviewRuntime => "review-runtime",
464
+ RuntimeCoverageAction::DeleteColdCode => "delete-cold-code",
465
+ }
466
+ }
467
+
468
+ fn blast_risk(risk: RuntimeBlastRisk) -> &'static str {
469
+ match risk {
470
+ RuntimeBlastRisk::Low => "low",
471
+ RuntimeBlastRisk::Medium => "medium",
472
+ RuntimeBlastRisk::High => "high",
473
+ }
474
+ }
475
+
476
+ fn capture_quality(report: &RuntimeCoverageReport) -> &'static str {
477
+ if report.total_invocations == 0 {
478
+ "low"
479
+ } else if report.total_invocations >= report.min_observation_volume {
480
+ "high"
481
+ } else {
482
+ "medium"
483
+ }
484
+ }
485
+
486
+ fn source_format(format: RuntimeCoverageFormat) -> &'static str {
487
+ match format {
488
+ RuntimeCoverageFormat::Istanbul => "istanbul",
489
+ RuntimeCoverageFormat::V8 => "v8",
490
+ RuntimeCoverageFormat::Mixed => "mixed",
491
+ }
492
+ }
493
+
494
+ fn finding_kind(kind: RuntimeCoverageFindingKind) -> &'static str {
495
+ match kind {
496
+ RuntimeCoverageFindingKind::LowTraffic => "low-traffic",
497
+ RuntimeCoverageFindingKind::CoverageUnavailable => "coverage-unavailable",
498
+ }
499
+ }
500
+
501
+ fn confidence(confidence: RuntimeCoverageConfidence) -> &'static str {
502
+ match confidence {
503
+ RuntimeCoverageConfidence::High => "high",
504
+ RuntimeCoverageConfidence::Medium => "medium",
505
+ RuntimeCoverageConfidence::Low => "low",
506
+ }
507
+ }
508
+
509
+ fn source_map_confidence(confidence: SourceMapConfidence) -> &'static str {
510
+ match confidence {
511
+ SourceMapConfidence::Resolved => "resolved",
512
+ SourceMapConfidence::Fallback => "fallback",
513
+ SourceMapConfidence::Unresolved => "unresolved",
514
+ }
515
+ }