| 173 | } |
| 174 | |
| 175 | fn observation(report: &Value, node: &Value) -> Result<(String, String, String, Value), String> { |
| 176 | let mut properties = node["properties"].clone(); |
| 177 | let source = properties["change"] |
| 178 | .as_str() |
| 179 | .filter(|s| !s.is_empty()) |
| 180 | .ok_or("recorded source is missing")? |
| 181 | .to_string(); |
| 182 | let path = properties["path"] |
| 183 | .as_str() |
| 184 | .filter(|s| !s.is_empty()) |
| 185 | .ok_or("recorded path is missing")? |
| 186 | .to_string(); |
| 187 | if path.split('/').any(|part| part == ".vault") { |
| 188 | return Err("vault metadata is not code evidence".into()); |
| 189 | } |
| 190 | match node["kind"].as_str() { |
| 191 | Some("check") => { |
| 192 | let check: Check = serde_json::from_value(properties).map_err(|e| e.to_string())?; |
| 193 | if check.status != CheckStatus::Completed { |
| 194 | return Err("audit was not completed".into()); |
| 195 | } |
| 196 | if check.detail.trim().is_empty() { |
| 197 | return Err("audit has no recorded observation detail".into()); |
| 198 | } |
| 199 | if !atomic_audit::AUDIT_CARDS |
| 200 | .iter() |
| 201 | .any(|card| card.id == check.rule) |
| 202 | { |
| 203 | return Err("audit rule is not registered".into()); |
| 204 | } |
| 205 | let findings: Vec<_> = report["graph"]["findings"].as_array().into_iter().flatten().filter_map(|item| { |
| 206 | if item["finding"]["requirement_or_rule"]["id"] != check.rule { return None; } |
| 207 | let finding: crate::review_finding::ReviewFinding = serde_json::from_value(item["finding"].clone()).ok()?; |
| 208 | if finding.validate().is_err() || !finding.locations.iter().any(|location| location.path == check.path) { return None; } |
| 209 | let in_source = finding.change == check.change || report["view_comparison"]["files"].as_array().into_iter().flatten().any(|file| { |
| 210 | file["source_ref"] == finding.change && file["analysis"]["checks"].as_array().into_iter().flatten().any(|candidate| candidate["change"] == check.change && candidate["path"] == check.path && candidate["rule"] == check.rule) |
| 211 | }); |
| 212 | if !in_source || item["id"].as_str().is_none_or(|id| id.is_empty()) { return None; } |
| 213 | Some(json!({"id":item["id"],"finding":finding,"comparison":item.get("comparison")})) |
| 214 | }).collect(); |
| 215 | if findings.is_empty() { |
| 216 | return Err("completed audit has no concrete pinned observation; execution coverage is not review work".into()); |
| 217 | } |
| 218 | Ok(( |
| 219 | source, |
| 220 | path, |
| 221 | check.rule.clone(), |
| 222 | json!({"audit":check.rule,"observations":findings}), |
| 223 | )) |
| 224 | } |
| 225 | Some("metric") => { |
| 226 | let object = properties |
| 227 | .as_object_mut() |
| 228 | .ok_or("metric properties missing")?; |
| 229 | object.remove("change"); |
| 230 | object.remove("scope_kind"); |
| 231 | let metric: atomic_audit::metrics::ExtractedMetric = |
| 232 | serde_json::from_value(properties).map_err(|e| e.to_string())?; |