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.
- package/Cargo.lock +572 -0
- package/Cargo.toml +41 -0
- package/LICENSE +21 -0
- package/README.md +617 -0
- package/assets/dart-decimate-banner.png +0 -0
- package/npm/bin/dart-decimate-mcp.js +48 -0
- package/npm/bin/dart-decimate.js +48 -0
- package/npm/scripts/postinstall.js +46 -0
- package/npm/scripts/test-npx-local.js +54 -0
- package/npm/scripts/test-npx-mcp-local.js +56 -0
- package/package.json +45 -0
- package/src/baseline.rs +643 -0
- package/src/bin/dart-decimate-mcp.rs +11 -0
- package/src/boundaries/tests.rs +60 -0
- package/src/boundaries.rs +317 -0
- package/src/changed_scope.rs +117 -0
- package/src/ci_template.rs +399 -0
- package/src/cli/analyze.rs +462 -0
- package/src/cli/analyzer_options.rs +82 -0
- package/src/cli/audit_run.rs +327 -0
- package/src/cli/boundary_args.rs +44 -0
- package/src/cli/ci_template_run.rs +196 -0
- package/src/cli/command_name.rs +20 -0
- package/src/cli/common_args.rs +167 -0
- package/src/cli/config_run.rs +78 -0
- package/src/cli/coverage_run.rs +393 -0
- package/src/cli/decision_surface_run.rs +99 -0
- package/src/cli/default_command.rs +69 -0
- package/src/cli/dupes_args.rs +150 -0
- package/src/cli/entry_points.rs +121 -0
- package/src/cli/error.rs +149 -0
- package/src/cli/error_output.rs +68 -0
- package/src/cli/explain_run.rs +72 -0
- package/src/cli/fix_run.rs +162 -0
- package/src/cli/flags_args.rs +23 -0
- package/src/cli/health_args.rs +200 -0
- package/src/cli/hooks_run.rs +191 -0
- package/src/cli/impact_run.rs +95 -0
- package/src/cli/init_run.rs +49 -0
- package/src/cli/inspect_args.rs +20 -0
- package/src/cli/inspect_run.rs +66 -0
- package/src/cli/issue_filter_args.rs +240 -0
- package/src/cli/list_run.rs +311 -0
- package/src/cli/mode_args.rs +72 -0
- package/src/cli/output_format.rs +62 -0
- package/src/cli/regression_args.rs +156 -0
- package/src/cli/request_args.rs +175 -0
- package/src/cli/schema_commands.rs +72 -0
- package/src/cli/scope_args.rs +174 -0
- package/src/cli/security_args.rs +175 -0
- package/src/cli/security_gate_run.rs +66 -0
- package/src/cli/security_summary_run.rs +43 -0
- package/src/cli/summary_args.rs +12 -0
- package/src/cli/tests.rs +516 -0
- package/src/cli/trace_args.rs +67 -0
- package/src/cli/trace_run.rs +131 -0
- package/src/cli/trace_tests.rs +235 -0
- package/src/cli/unsupported_run.rs +116 -0
- package/src/cli/watch_run.rs +153 -0
- package/src/cli.rs +603 -0
- package/src/config/boundary_config.rs +235 -0
- package/src/config/cache.rs +25 -0
- package/src/config/dependencies.rs +114 -0
- package/src/config/health.rs +237 -0
- package/src/config/jsonc.rs +66 -0
- package/src/config/policy.rs +91 -0
- package/src/config/rule_aliases.rs +487 -0
- package/src/config/rules.rs +339 -0
- package/src/config.rs +635 -0
- package/src/coverage.rs +469 -0
- package/src/dart_parser.rs +673 -0
- package/src/decision_surface.rs +405 -0
- package/src/dependencies/analyze.rs +218 -0
- package/src/dependencies/codegen.rs +118 -0
- package/src/dependencies/discovery.rs +60 -0
- package/src/dependencies/overrides.rs +76 -0
- package/src/dependencies/private_src_imports.rs +32 -0
- package/src/dependencies/pubspec_document.rs +86 -0
- package/src/dependencies/pubspec_entry.rs +96 -0
- package/src/dependencies/tests.rs +544 -0
- package/src/dependencies/usage.rs +33 -0
- package/src/dependencies.rs +553 -0
- package/src/dependency_scripts.rs +91 -0
- package/src/dupes/lex.rs +236 -0
- package/src/dupes/tests.rs +119 -0
- package/src/dupes.rs +625 -0
- package/src/explain.rs +692 -0
- package/src/extract/directives.rs +252 -0
- package/src/extract/error_tests.rs +25 -0
- package/src/extract/members.rs +210 -0
- package/src/extract/primary_constructors.rs +421 -0
- package/src/extract/references.rs +151 -0
- package/src/extract/routes.rs +691 -0
- package/src/extract/signatures.rs +266 -0
- package/src/extract/string_tests.rs +18 -0
- package/src/extract/strings.rs +107 -0
- package/src/extract/tests.rs +697 -0
- package/src/extract.rs +680 -0
- package/src/feature_flags/tests.rs +76 -0
- package/src/feature_flags.rs +453 -0
- package/src/finding_identity.rs +60 -0
- package/src/fix.rs +551 -0
- package/src/generated.rs +53 -0
- package/src/graph/augment_tests.rs +212 -0
- package/src/graph/conditionals.rs +74 -0
- package/src/graph/package_config_tests.rs +100 -0
- package/src/graph/parts.rs +248 -0
- package/src/graph/pubspec_tests.rs +119 -0
- package/src/graph/tests.rs +837 -0
- package/src/graph/uri_tests.rs +102 -0
- package/src/graph.rs +644 -0
- package/src/health/coverage.rs +309 -0
- package/src/health/ownership.rs +175 -0
- package/src/health/runtime_coverage.rs +616 -0
- package/src/health/runtime_intelligence.rs +511 -0
- package/src/health/scores.rs +296 -0
- package/src/health/tests.rs +240 -0
- package/src/health/threshold_types.rs +163 -0
- package/src/health/thresholds.rs +229 -0
- package/src/health/types.rs +636 -0
- package/src/health.rs +597 -0
- package/src/hooks/agent_settings.rs +175 -0
- package/src/hooks/templates.rs +51 -0
- package/src/hooks.rs +569 -0
- package/src/impact.rs +287 -0
- package/src/init.rs +237 -0
- package/src/inspect.rs +142 -0
- package/src/intelligence/tests.rs +232 -0
- package/src/intelligence.rs +305 -0
- package/src/lib.rs +198 -0
- package/src/main.rs +5 -0
- package/src/manifest/mcp.rs +565 -0
- package/src/manifest.rs +552 -0
- package/src/mcp/cli_args/value_args.rs +175 -0
- package/src/mcp/cli_args.rs +658 -0
- package/src/mcp/cli_args_tests.rs +522 -0
- package/src/mcp/code_execute.rs +671 -0
- package/src/mcp/tools.rs +660 -0
- package/src/mcp.rs +325 -0
- package/src/output/audit_risk.rs +135 -0
- package/src/output/dependency_findings.rs +327 -0
- package/src/output/duplication_findings.rs +114 -0
- package/src/output/feature_flag_findings.rs +122 -0
- package/src/output/format.rs +31 -0
- package/src/output/graph_findings.rs +461 -0
- package/src/output/health_findings.rs +612 -0
- package/src/output/human.rs +27 -0
- package/src/output/next_steps.rs +92 -0
- package/src/output/route_findings.rs +76 -0
- package/src/output/runtime_coverage.rs +515 -0
- package/src/output/scope.rs +183 -0
- package/src/output/security_findings.rs +390 -0
- package/src/output/security_sarif.rs +150 -0
- package/src/output/suppressions.rs +323 -0
- package/src/output/symbol_findings.rs +257 -0
- package/src/output/tests.rs +114 -0
- package/src/output/types.rs +604 -0
- package/src/output/widget_findings.rs +213 -0
- package/src/output.rs +682 -0
- package/src/package_map.rs +495 -0
- package/src/policy/packs.rs +232 -0
- package/src/policy/tests.rs +134 -0
- package/src/policy.rs +640 -0
- package/src/project_list.rs +376 -0
- package/src/report_schema/inventory.rs +418 -0
- package/src/report_schema.rs +488 -0
- package/src/routes.rs +354 -0
- package/src/scan/tests.rs +250 -0
- package/src/scan.rs +275 -0
- package/src/security/detect.rs +639 -0
- package/src/security/tests.rs +178 -0
- package/src/security.rs +373 -0
- package/src/security_gate.rs +555 -0
- package/src/symbols/path_filters.rs +27 -0
- package/src/symbols/private_type_leaks.rs +125 -0
- package/src/symbols/riverpod.rs +113 -0
- package/src/symbols/tests.rs +692 -0
- package/src/symbols.rs +693 -0
- package/src/trace/tests.rs +225 -0
- package/src/trace.rs +690 -0
- package/src/unsupported.rs +47 -0
- package/src/widgets/lifecycle.rs +484 -0
- package/src/widgets/params.rs +236 -0
- package/src/widgets/tests.rs +430 -0
- package/src/widgets/top_level.rs +110 -0
- package/src/widgets/unrendered.rs +374 -0
- package/src/widgets.rs +590 -0
- package/src/workspace_scope.rs +358 -0
package/src/mcp.rs
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
use std::io::{self, BufRead, Write};
|
|
2
|
+
|
|
3
|
+
use serde_json::{Map, Value, json};
|
|
4
|
+
|
|
5
|
+
use crate::cli::{CliError, run_from};
|
|
6
|
+
|
|
7
|
+
mod cli_args;
|
|
8
|
+
#[cfg(test)]
|
|
9
|
+
mod cli_args_tests;
|
|
10
|
+
pub(crate) mod code_execute;
|
|
11
|
+
mod tools;
|
|
12
|
+
|
|
13
|
+
use cli_args::cli_args_for_tool;
|
|
14
|
+
use code_execute::execute as execute_code;
|
|
15
|
+
use tools::tools;
|
|
16
|
+
|
|
17
|
+
const PROTOCOL_VERSION: &str = "2025-11-25";
|
|
18
|
+
|
|
19
|
+
/// Run Dart Decimate's MCP stdio server.
|
|
20
|
+
///
|
|
21
|
+
/// # Errors
|
|
22
|
+
///
|
|
23
|
+
/// Returns an IO error if stdin or stdout cannot be read or written.
|
|
24
|
+
pub fn run_stdio() -> io::Result<()> {
|
|
25
|
+
let stdin = io::stdin();
|
|
26
|
+
let mut stdout = io::stdout().lock();
|
|
27
|
+
for line in stdin.lock().lines() {
|
|
28
|
+
let line = line?;
|
|
29
|
+
if line.trim().is_empty() {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if let Some(response) = handle_message(&line) {
|
|
33
|
+
serde_json::to_writer(&mut stdout, &response)?;
|
|
34
|
+
writeln!(stdout)?;
|
|
35
|
+
stdout.flush()?;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
Ok(())
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// Handle one newline-delimited JSON-RPC MCP message.
|
|
42
|
+
#[must_use]
|
|
43
|
+
pub fn handle_message(message: &str) -> Option<Value> {
|
|
44
|
+
let value = match serde_json::from_str::<Value>(message) {
|
|
45
|
+
Ok(value) => value,
|
|
46
|
+
Err(error) => return Some(response_error(&Value::Null, -32700, error.to_string())),
|
|
47
|
+
};
|
|
48
|
+
let id = value.get("id").cloned();
|
|
49
|
+
let Some(method) = value.get("method").and_then(Value::as_str) else {
|
|
50
|
+
return id.map(|id| response_error(&id, -32600, "missing method"));
|
|
51
|
+
};
|
|
52
|
+
id.as_ref()?;
|
|
53
|
+
let id = id.unwrap_or(Value::Null);
|
|
54
|
+
match method {
|
|
55
|
+
"initialize" => Some(response_result(&id, &initialize_result(&value))),
|
|
56
|
+
"ping" => Some(response_result(&id, &json!({}))),
|
|
57
|
+
"tools/list" => Some(response_result(&id, &json!({ "tools": tools() }))),
|
|
58
|
+
"tools/call" => Some(call_tool(&id, &value)),
|
|
59
|
+
_ => Some(response_error(
|
|
60
|
+
&id,
|
|
61
|
+
-32601,
|
|
62
|
+
format!("unknown method {method}"),
|
|
63
|
+
)),
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
fn initialize_result(message: &Value) -> Value {
|
|
68
|
+
let requested = message
|
|
69
|
+
.get("params")
|
|
70
|
+
.and_then(|params| params.get("protocolVersion"))
|
|
71
|
+
.and_then(Value::as_str)
|
|
72
|
+
.unwrap_or(PROTOCOL_VERSION);
|
|
73
|
+
let protocol_version = if requested == PROTOCOL_VERSION {
|
|
74
|
+
requested
|
|
75
|
+
} else {
|
|
76
|
+
PROTOCOL_VERSION
|
|
77
|
+
};
|
|
78
|
+
json!({
|
|
79
|
+
"protocolVersion": protocol_version,
|
|
80
|
+
"capabilities": {
|
|
81
|
+
"tools": { "listChanged": false }
|
|
82
|
+
},
|
|
83
|
+
"serverInfo": {
|
|
84
|
+
"name": "dart-decimate-mcp",
|
|
85
|
+
"title": "Dart Decimate MCP",
|
|
86
|
+
"version": env!("CARGO_PKG_VERSION")
|
|
87
|
+
},
|
|
88
|
+
"instructions": "Dart and Flutter codebase intelligence. Mutating fix_apply is available only with explicit yes: true confirmation."
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
fn call_tool(id: &Value, message: &Value) -> Value {
|
|
93
|
+
let params = message.get("params").unwrap_or(&Value::Null);
|
|
94
|
+
let Some(name) = params.get("name").and_then(Value::as_str) else {
|
|
95
|
+
return response_error(id, -32602, "tools/call requires params.name");
|
|
96
|
+
};
|
|
97
|
+
let Some(arguments) = params.get("arguments") else {
|
|
98
|
+
return match cli_args_for_tool(name, &Map::new()) {
|
|
99
|
+
Ok(args) => response_result(id, &tool_result(run_tool_json(name, args))),
|
|
100
|
+
Err(message) => response_error(id, -32602, message),
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
let Some(arguments) = arguments.as_object() else {
|
|
104
|
+
return response_error(id, -32602, "tools/call params.arguments must be an object");
|
|
105
|
+
};
|
|
106
|
+
if name == "code_execute" {
|
|
107
|
+
return response_result(id, &tool_result(execute_code(arguments)));
|
|
108
|
+
}
|
|
109
|
+
match cli_args_for_tool(name, arguments) {
|
|
110
|
+
Ok(args) => response_result(id, &tool_result(run_tool_json(name, args))),
|
|
111
|
+
Err(message) => response_error(id, -32602, message),
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
fn run_tool_json(name: &str, args: Vec<String>) -> CliToolOutput {
|
|
116
|
+
let mut output = run_cli_json(args);
|
|
117
|
+
if let Some(structured) = runtime_slice_content(name, output.structured.as_ref()) {
|
|
118
|
+
output.text =
|
|
119
|
+
serde_json::to_string_pretty(&structured).unwrap_or_else(|_| structured.to_string());
|
|
120
|
+
output.structured = Some(structured);
|
|
121
|
+
}
|
|
122
|
+
output
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
fn run_cli_json(args: Vec<String>) -> CliToolOutput {
|
|
126
|
+
let mut output = Vec::new();
|
|
127
|
+
let code = match run_from(args, &mut output) {
|
|
128
|
+
Ok(code) => code,
|
|
129
|
+
Err(error) => {
|
|
130
|
+
return CliToolOutput::json(error_exit_code(&error), cli_error_json(&error), true);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
let text = String::from_utf8_lossy(&output).into_owned();
|
|
134
|
+
let structured = serde_json::from_str::<Value>(&text).ok();
|
|
135
|
+
CliToolOutput::new(code, text, structured, code == 2)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
fn error_exit_code(error: &CliError) -> i32 {
|
|
139
|
+
match error {
|
|
140
|
+
CliError::Clap(error) => error.exit_code(),
|
|
141
|
+
_ => 2,
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
fn cli_error_json(error: &CliError) -> Value {
|
|
146
|
+
json!({
|
|
147
|
+
"error": true,
|
|
148
|
+
"message": error.to_string(),
|
|
149
|
+
"exit_code": error_exit_code(error)
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
fn runtime_slice_content(name: &str, structured: Option<&Value>) -> Option<Value> {
|
|
154
|
+
let runtime = structured?.get("runtime_coverage")?;
|
|
155
|
+
let mut slice = runtime_slice_base(name, structured?, runtime)?;
|
|
156
|
+
match name {
|
|
157
|
+
"get_hot_paths" => {
|
|
158
|
+
slice.insert(
|
|
159
|
+
"hot_paths".to_owned(),
|
|
160
|
+
runtime
|
|
161
|
+
.get("hot_paths")
|
|
162
|
+
.cloned()
|
|
163
|
+
.unwrap_or_else(|| json!([])),
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
"get_blast_radius" => {
|
|
167
|
+
slice.insert(
|
|
168
|
+
"blast_radius".to_owned(),
|
|
169
|
+
runtime
|
|
170
|
+
.get("blast_radius")
|
|
171
|
+
.cloned()
|
|
172
|
+
.unwrap_or_else(|| json!([])),
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
"get_importance" => {
|
|
176
|
+
slice.insert(
|
|
177
|
+
"importance".to_owned(),
|
|
178
|
+
runtime
|
|
179
|
+
.get("importance")
|
|
180
|
+
.cloned()
|
|
181
|
+
.unwrap_or_else(|| json!([])),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
"get_cleanup_candidates" => {
|
|
185
|
+
slice.insert(
|
|
186
|
+
"findings".to_owned(),
|
|
187
|
+
cleanup_findings(runtime.get("findings")),
|
|
188
|
+
);
|
|
189
|
+
slice.insert(
|
|
190
|
+
"coverage_intelligence".to_owned(),
|
|
191
|
+
cleanup_intelligence(runtime.get("coverage_intelligence")),
|
|
192
|
+
);
|
|
193
|
+
slice.insert(
|
|
194
|
+
"actionable".to_owned(),
|
|
195
|
+
runtime
|
|
196
|
+
.get("actionable")
|
|
197
|
+
.cloned()
|
|
198
|
+
.unwrap_or_else(|| json!({})),
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
_ => return None,
|
|
202
|
+
}
|
|
203
|
+
Some(Value::Object(slice))
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
fn runtime_slice_base(
|
|
207
|
+
name: &str,
|
|
208
|
+
structured: &Value,
|
|
209
|
+
runtime: &Value,
|
|
210
|
+
) -> Option<Map<String, Value>> {
|
|
211
|
+
let kind = match name {
|
|
212
|
+
"get_hot_paths" => "runtime-hot-paths",
|
|
213
|
+
"get_blast_radius" => "runtime-blast-radius",
|
|
214
|
+
"get_importance" => "runtime-importance",
|
|
215
|
+
"get_cleanup_candidates" => "runtime-cleanup-candidates",
|
|
216
|
+
_ => return None,
|
|
217
|
+
};
|
|
218
|
+
let mut slice = Map::new();
|
|
219
|
+
for key in ["schema_version", "tool", "command"] {
|
|
220
|
+
if let Some(value) = structured.get(key) {
|
|
221
|
+
slice.insert(key.to_owned(), value.clone());
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
slice.insert("kind".to_owned(), Value::String(kind.to_owned()));
|
|
225
|
+
for key in ["summary", "provenance", "watermark", "warnings"] {
|
|
226
|
+
if let Some(value) = runtime.get(key) {
|
|
227
|
+
slice.insert(key.to_owned(), value.clone());
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
Some(slice)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
fn cleanup_findings(findings: Option<&Value>) -> Value {
|
|
234
|
+
let values = findings
|
|
235
|
+
.and_then(Value::as_array)
|
|
236
|
+
.into_iter()
|
|
237
|
+
.flatten()
|
|
238
|
+
.filter(|finding| {
|
|
239
|
+
finding["safe_to_delete"].as_bool().unwrap_or_default()
|
|
240
|
+
|| matches!(
|
|
241
|
+
finding["kind"].as_str(),
|
|
242
|
+
Some("low-traffic" | "coverage-unavailable")
|
|
243
|
+
)
|
|
244
|
+
})
|
|
245
|
+
.cloned()
|
|
246
|
+
.collect::<Vec<_>>();
|
|
247
|
+
Value::Array(values)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
fn cleanup_intelligence(intelligence: Option<&Value>) -> Value {
|
|
251
|
+
let values = intelligence
|
|
252
|
+
.and_then(Value::as_array)
|
|
253
|
+
.into_iter()
|
|
254
|
+
.flatten()
|
|
255
|
+
.filter(|row| {
|
|
256
|
+
matches!(
|
|
257
|
+
row["kind"].as_str(),
|
|
258
|
+
Some("low-traffic" | "coverage-unavailable")
|
|
259
|
+
)
|
|
260
|
+
})
|
|
261
|
+
.cloned()
|
|
262
|
+
.collect::<Vec<_>>();
|
|
263
|
+
Value::Array(values)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
fn tool_result(output: CliToolOutput) -> Value {
|
|
267
|
+
let mut result = Map::new();
|
|
268
|
+
result.insert(
|
|
269
|
+
"content".to_owned(),
|
|
270
|
+
json!([{ "type": "text", "text": output.text }]),
|
|
271
|
+
);
|
|
272
|
+
result.insert("isError".to_owned(), Value::Bool(output.is_error));
|
|
273
|
+
result.insert("_meta".to_owned(), json!({ "exit_code": output.exit_code }));
|
|
274
|
+
if let Some(structured) = output.structured {
|
|
275
|
+
result.insert("structuredContent".to_owned(), structured);
|
|
276
|
+
}
|
|
277
|
+
Value::Object(result)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
#[derive(Debug)]
|
|
281
|
+
struct CliToolOutput {
|
|
282
|
+
exit_code: i32,
|
|
283
|
+
text: String,
|
|
284
|
+
structured: Option<Value>,
|
|
285
|
+
is_error: bool,
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
impl CliToolOutput {
|
|
289
|
+
fn new(exit_code: i32, text: String, structured: Option<Value>, is_error: bool) -> Self {
|
|
290
|
+
Self {
|
|
291
|
+
exit_code,
|
|
292
|
+
text,
|
|
293
|
+
structured,
|
|
294
|
+
is_error,
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
fn json(exit_code: i32, structured: Value, is_error: bool) -> Self {
|
|
299
|
+
Self::new(
|
|
300
|
+
exit_code,
|
|
301
|
+
structured.to_string(),
|
|
302
|
+
Some(structured),
|
|
303
|
+
is_error,
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
fn response_result(id: &Value, result: &Value) -> Value {
|
|
309
|
+
json!({
|
|
310
|
+
"jsonrpc": "2.0",
|
|
311
|
+
"id": id,
|
|
312
|
+
"result": result
|
|
313
|
+
})
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
fn response_error(id: &Value, code: i32, message: impl Into<String>) -> Value {
|
|
317
|
+
json!({
|
|
318
|
+
"jsonrpc": "2.0",
|
|
319
|
+
"id": id,
|
|
320
|
+
"error": {
|
|
321
|
+
"code": code,
|
|
322
|
+
"message": message.into()
|
|
323
|
+
}
|
|
324
|
+
})
|
|
325
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
use std::collections::BTreeSet;
|
|
2
|
+
use std::path::{Path, PathBuf};
|
|
3
|
+
|
|
4
|
+
use crate::finding_identity::finding_identity;
|
|
5
|
+
|
|
6
|
+
use super::format::display_path;
|
|
7
|
+
use super::types::{
|
|
8
|
+
AuditAttribution, AuditAttributionCounts, AuditRiskLevel, Finding, JsonReport, ReportCommand,
|
|
9
|
+
Severity,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/// Refresh Fallow-style changed-code risk fields for an audit report.
|
|
13
|
+
pub fn apply_audit_risk(
|
|
14
|
+
root: &Path,
|
|
15
|
+
changed_files: &[PathBuf],
|
|
16
|
+
base_finding_identities: &BTreeSet<String>,
|
|
17
|
+
report: &mut JsonReport,
|
|
18
|
+
) {
|
|
19
|
+
if report.command != ReportCommand::Audit {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let changed = changed_files
|
|
24
|
+
.iter()
|
|
25
|
+
.map(|path| display_path(root, path))
|
|
26
|
+
.collect::<BTreeSet<_>>();
|
|
27
|
+
let attribution = audit_attribution(&report.findings, &changed, base_finding_identities);
|
|
28
|
+
let risk_score = risk_score(&attribution);
|
|
29
|
+
let risk_level = risk_level(&attribution);
|
|
30
|
+
|
|
31
|
+
report.summary.risk_score = Some(risk_score);
|
|
32
|
+
report.summary.risk_level = Some(risk_level);
|
|
33
|
+
report.summary.attribution = Some(attribution);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
fn audit_attribution(
|
|
37
|
+
findings: &[Finding],
|
|
38
|
+
changed: &BTreeSet<String>,
|
|
39
|
+
base_finding_identities: &BTreeSet<String>,
|
|
40
|
+
) -> AuditAttribution {
|
|
41
|
+
let mut introduced = AttributionBucket::default();
|
|
42
|
+
let mut pre_existing = AttributionBucket::default();
|
|
43
|
+
|
|
44
|
+
for finding in findings {
|
|
45
|
+
if base_finding_identities.contains(&finding_identity(finding)) {
|
|
46
|
+
pre_existing.add(finding);
|
|
47
|
+
} else if touches_changed_file(finding, changed) {
|
|
48
|
+
introduced.add(finding);
|
|
49
|
+
} else {
|
|
50
|
+
pre_existing.add(finding);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
AuditAttribution {
|
|
55
|
+
introduced: introduced.finish(),
|
|
56
|
+
pre_existing: pre_existing.finish(),
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fn touches_changed_file(finding: &Finding, changed: &BTreeSet<String>) -> bool {
|
|
61
|
+
changed.contains(&finding.path)
|
|
62
|
+
|| finding.files.iter().any(|path| changed.contains(path))
|
|
63
|
+
|| finding
|
|
64
|
+
.edge
|
|
65
|
+
.as_ref()
|
|
66
|
+
.is_some_and(|edge| changed.contains(&edge.from) || changed.contains(&edge.to))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#[derive(Default)]
|
|
70
|
+
struct AttributionBucket {
|
|
71
|
+
findings: usize,
|
|
72
|
+
error_findings: usize,
|
|
73
|
+
warning_findings: usize,
|
|
74
|
+
safe_to_delete: usize,
|
|
75
|
+
files: BTreeSet<String>,
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
impl AttributionBucket {
|
|
79
|
+
fn add(&mut self, finding: &Finding) {
|
|
80
|
+
self.findings += 1;
|
|
81
|
+
match finding.severity {
|
|
82
|
+
Severity::Error => self.error_findings += 1,
|
|
83
|
+
Severity::Warning => self.warning_findings += 1,
|
|
84
|
+
}
|
|
85
|
+
if finding.safe_to_delete {
|
|
86
|
+
self.safe_to_delete += 1;
|
|
87
|
+
}
|
|
88
|
+
add_finding_files(finding, &mut self.files);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn finish(self) -> AuditAttributionCounts {
|
|
92
|
+
AuditAttributionCounts {
|
|
93
|
+
findings: self.findings,
|
|
94
|
+
error_findings: self.error_findings,
|
|
95
|
+
warning_findings: self.warning_findings,
|
|
96
|
+
safe_to_delete: self.safe_to_delete,
|
|
97
|
+
files: self.files.len(),
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
fn add_finding_files(finding: &Finding, files: &mut BTreeSet<String>) {
|
|
103
|
+
if !finding.path.is_empty() {
|
|
104
|
+
files.insert(finding.path.clone());
|
|
105
|
+
}
|
|
106
|
+
files.extend(finding.files.iter().cloned());
|
|
107
|
+
if let Some(edge) = &finding.edge {
|
|
108
|
+
files.insert(edge.from.clone());
|
|
109
|
+
files.insert(edge.to.clone());
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
fn risk_score(attribution: &AuditAttribution) -> usize {
|
|
114
|
+
let introduced = &attribution.introduced;
|
|
115
|
+
let pre_existing = &attribution.pre_existing;
|
|
116
|
+
introduced
|
|
117
|
+
.error_findings
|
|
118
|
+
.saturating_mul(30)
|
|
119
|
+
.saturating_add(introduced.warning_findings.saturating_mul(15))
|
|
120
|
+
.saturating_add(introduced.safe_to_delete.saturating_mul(10))
|
|
121
|
+
.saturating_add(pre_existing.error_findings.saturating_mul(10))
|
|
122
|
+
.saturating_add(pre_existing.warning_findings.saturating_mul(4))
|
|
123
|
+
.saturating_add(pre_existing.safe_to_delete.saturating_mul(2))
|
|
124
|
+
.min(100)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
fn risk_level(attribution: &AuditAttribution) -> AuditRiskLevel {
|
|
128
|
+
if attribution.introduced.error_findings > 0 {
|
|
129
|
+
AuditRiskLevel::Fail
|
|
130
|
+
} else if attribution.introduced.findings > 0 || attribution.pre_existing.findings > 0 {
|
|
131
|
+
AuditRiskLevel::Warn
|
|
132
|
+
} else {
|
|
133
|
+
AuditRiskLevel::Pass
|
|
134
|
+
}
|
|
135
|
+
}
|