wvq-intelligence 0.1.0-alpha.2

Weavatrix-backed code evidence for Weavatrix Quality. No second parser or code graph.
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Map measured LCOV ranges onto Weavatrix node spans.
//!
//! Missing coverage is never treated as uncovered. Static reachability is
//! never treated as measured coverage.

use serde_json::Value;

use crate::weavatrix::IntelligenceError;
use wvq_domain::{CheckId, QualityFinding, Severity, SubjectRef};
use wvq_runtime::{CoverageArtifact, LineRange};

/// How a node relates to measured coverage.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CoverageMeasurement {
    /// At least one instrumented line in the span was hit.
    Covered,
    /// The span is instrumented and every hit count is zero.
    Uncovered,
    /// No measured report covers this span. Not evidence of absence.
    Unmeasured,
}

/// Measured coverage for one Weavatrix graph node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NodeCoverage {
    /// Graph node id (Weavatrix, not a second graph).
    pub node_id: String,
    /// Measurement kind.
    pub measurement: CoverageMeasurement,
    /// Hit lines overlapping the node span.
    pub covered_lines: u64,
    /// Instrumented lines overlapping the node span (`DA` entries).
    pub instrumented_lines: u64,
}

/// Overlay LCOV ranges onto Weavatrix node spans.
///
/// # Errors
///
/// Fails closed when a node has no id.
pub fn map_coverage_to_nodes(
    coverage: Option<&CoverageArtifact>,
    graph: &Value,
) -> Result<Vec<NodeCoverage>, IntelligenceError> {
    let mut out = Vec::new();
    for node in graph_nodes(graph) {
        let id = node_id(node)
            .ok_or_else(|| IntelligenceError::InvalidEvidence("coverage node missing id".into()))?;
        let Some(span) = node_span(node).or_else(|| file_node_span(node, coverage)) else {
            out.push(NodeCoverage {
                node_id: id,
                measurement: CoverageMeasurement::Unmeasured,
                covered_lines: 0,
                instrumented_lines: 0,
            });
            continue;
        };
        out.push(measure(coverage, &id, &span));
    }
    Ok(out)
}

fn file_node_span(node: &Value, coverage: Option<&CoverageArtifact>) -> Option<NodeSpan> {
    if node.get("kind").and_then(Value::as_str) != Some("file") {
        return None;
    }
    let path = node.get("label").and_then(Value::as_str).or_else(|| {
        node.get("id")
            .and_then(Value::as_str)?
            .strip_prefix("file:")
    })?;
    let file = coverage?
        .files
        .iter()
        .find(|file| paths_eq(&file.path, path))?;
    let mut ranges = file.covered.iter().chain(file.uncovered.iter());
    let first = *ranges.next()?;
    let (start_line, end_line) = ranges.fold((first.start, first.end), |(start, end), range| {
        (start.min(range.start), end.max(range.end))
    });
    Some(NodeSpan {
        file: path.replace('\\', "/"),
        start_line,
        end_line,
    })
}

/// Coverage findings for a dual-revision packet.
///
/// `graph` keys used: `changed`, `risk`, `hot_path`, `new`, `static_selected`,
/// `measured_tests`, `obligations`, `removed_proof_tests`.
///
/// # Errors
///
/// Invalid node identity.
pub fn map_coverage_findings(
    base_cov: Option<&CoverageArtifact>,
    head_cov: Option<&CoverageArtifact>,
    base_graph: &Value,
    head_graph: &Value,
) -> Result<Vec<QualityFinding>, IntelligenceError> {
    let base_nodes = map_coverage_to_nodes(base_cov, base_graph)?;
    let head_nodes = map_coverage_to_nodes(head_cov, head_graph)?;
    let mut findings = Vec::new();
    findings.extend(changed_uncovered(&head_nodes, head_graph));
    findings.extend(impacted_regression(&base_nodes, &head_nodes, head_graph));
    findings.extend(new_unmeasured(&base_nodes, &head_nodes, head_graph));
    findings.extend(static_vs_measured(head_graph));
    findings.extend(hot_path_weak(&head_nodes, head_graph));
    findings.extend(obligation_gaps(head_graph));
    findings.extend(removed_proof_tests(head_graph));
    let _ = base_graph;
    Ok(findings)
}

fn measure(coverage: Option<&CoverageArtifact>, id: &str, span: &NodeSpan) -> NodeCoverage {
    let Some(file) = coverage.and_then(|artifact| {
        artifact
            .files
            .iter()
            .find(|file| paths_eq(&file.path, &span.file))
    }) else {
        return NodeCoverage {
            node_id: id.to_owned(),
            measurement: CoverageMeasurement::Unmeasured,
            covered_lines: 0,
            instrumented_lines: 0,
        };
    };
    let mut covered = 0_u64;
    let mut instrumented = 0_u64;
    for range in &file.covered {
        let n = overlap_len(*range, span.start_line, span.end_line);
        covered = covered.saturating_add(n);
        instrumented = instrumented.saturating_add(n);
    }
    for range in &file.uncovered {
        instrumented =
            instrumented.saturating_add(overlap_len(*range, span.start_line, span.end_line));
    }
    let measurement = if instrumented == 0 {
        CoverageMeasurement::Unmeasured
    } else if covered == 0 {
        CoverageMeasurement::Uncovered
    } else {
        CoverageMeasurement::Covered
    };
    NodeCoverage {
        node_id: id.to_owned(),
        measurement,
        covered_lines: covered,
        instrumented_lines: instrumented,
    }
}

fn changed_uncovered(nodes: &[NodeCoverage], graph: &Value) -> Vec<QualityFinding> {
    let mut out = Vec::new();
    for node in nodes {
        if !flag(graph, &node.node_id, "changed") || !high_risk(graph, &node.node_id) {
            continue;
        }
        if node.measurement != CoverageMeasurement::Uncovered {
            continue;
        }
        out.push(cov_finding(
            "WVQ-COV-001",
            Severity::Warn,
            &node.node_id,
            format!(
                "changed high-risk node {} has measured coverage 0/{} (uncovered, not missing)",
                node.node_id, node.instrumented_lines
            ),
        ));
    }
    out
}

fn impacted_regression(
    base: &[NodeCoverage],
    head: &[NodeCoverage],
    graph: &Value,
) -> Vec<QualityFinding> {
    let mut out = Vec::new();
    for head_node in head {
        if !flag(graph, &head_node.node_id, "changed") {
            continue;
        }
        let Some(base_node) = base.iter().find(|item| item.node_id == head_node.node_id) else {
            continue;
        };
        if base_node.instrumented_lines == 0 || head_node.instrumented_lines == 0 {
            continue;
        }
        let base_m = milles(base_node.covered_lines, base_node.instrumented_lines);
        let head_m = milles(head_node.covered_lines, head_node.instrumented_lines);
        if base_m > head_m && base_m.saturating_sub(head_m) > 20 {
            out.push(cov_finding(
                "WVQ-COV-003",
                Severity::Warn,
                &head_node.node_id,
                format!(
                    "impacted coverage {}/{} → {}/{} (drop over 2%)",
                    base_node.covered_lines,
                    base_node.instrumented_lines,
                    head_node.covered_lines,
                    head_node.instrumented_lines
                ),
            ));
        }
    }
    out
}

fn new_unmeasured(
    base: &[NodeCoverage],
    head: &[NodeCoverage],
    graph: &Value,
) -> Vec<QualityFinding> {
    let mut out = Vec::new();
    for head_node in head {
        if !flag(graph, &head_node.node_id, "new") {
            continue;
        }
        if base.iter().any(|item| item.node_id == head_node.node_id) {
            continue;
        }
        if head_node.measurement != CoverageMeasurement::Unmeasured {
            continue;
        }
        out.push(cov_finding(
            "WVQ-COV-004",
            Severity::Warn,
            &head_node.node_id,
            format!(
                "new region {} is unmeasured (missing evidence, not uncovered)",
                head_node.node_id
            ),
        ));
    }
    out
}

fn static_vs_measured(graph: &Value) -> Vec<QualityFinding> {
    let static_sel = string_set(graph, "static_selected");
    let measured = string_set(graph, "measured_tests");
    if static_sel.is_empty() && measured.is_empty() {
        return Vec::new();
    }
    let only_static = static_sel.difference(&measured).count();
    let only_measured = measured.difference(&static_sel).count();
    if only_static == 0 && only_measured == 0 {
        return Vec::new();
    }
    vec![cov_finding(
        "WVQ-COV-006",
        Severity::Info,
        "static-vs-measured",
        format!(
            "static reachability and measured coverage disagree (static-only {only_static}, measured-only {only_measured}); static is not measured coverage"
        ),
    )]
}

fn hot_path_weak(nodes: &[NodeCoverage], graph: &Value) -> Vec<QualityFinding> {
    let mut out = Vec::new();
    for node in nodes {
        if !flag(graph, &node.node_id, "hot_path") {
            continue;
        }
        if node.instrumented_lines == 0
            || node.covered_lines.saturating_mul(2) >= node.instrumented_lines
        {
            continue;
        }
        out.push(cov_finding(
            "WVQ-COV-007",
            Severity::Warn,
            &node.node_id,
            format!(
                "hot path {} measured {}/{} lines",
                node.node_id, node.covered_lines, node.instrumented_lines
            ),
        ));
    }
    out
}

fn obligation_gaps(graph: &Value) -> Vec<QualityFinding> {
    let mut out = Vec::new();
    let Some(items) = graph.get("obligations").and_then(Value::as_array) else {
        return out;
    };
    for item in items {
        let Some(id) = item.get("id").and_then(Value::as_str) else {
            continue;
        };
        if item.get("has_proof_path") == Some(&Value::Bool(true)) {
            continue;
        }
        out.push(cov_finding(
            "WVQ-COV-002",
            Severity::Error,
            id,
            format!("impacted obligation {id} has no executable proof path"),
        ));
    }
    out
}

fn removed_proof_tests(graph: &Value) -> Vec<QualityFinding> {
    let mut out = Vec::new();
    let Some(items) = graph.get("removed_proof_tests").and_then(Value::as_array) else {
        return out;
    };
    for item in items {
        let Some(id) = item.as_str() else {
            continue;
        };
        out.push(cov_finding(
            "WVQ-COV-005",
            Severity::Error,
            id,
            format!("proof-bearing test {id} removed without replacement"),
        ));
    }
    out
}

struct NodeSpan {
    file: String,
    start_line: u32,
    end_line: u32,
}

fn graph_nodes(graph: &Value) -> &[Value] {
    graph
        .get("nodes")
        .and_then(Value::as_array)
        .map_or(&[], Vec::as_slice)
}

fn node_id(node: &Value) -> Option<String> {
    node.get("id").and_then(Value::as_str).map(str::to_owned)
}

fn node_span(node: &Value) -> Option<NodeSpan> {
    let file = node
        .pointer("/span/file")
        .or_else(|| node.get("file"))
        .and_then(Value::as_str)?;
    let start = node
        .pointer("/span/start_line")
        .or_else(|| node.pointer("/span/start/line"))
        .or_else(|| node.get("start_line"))
        .and_then(Value::as_u64)?;
    let end = node
        .pointer("/span/end_line")
        .or_else(|| node.pointer("/span/end/line"))
        .or_else(|| node.get("end_line"))
        .and_then(Value::as_u64)
        .unwrap_or(start);
    Some(NodeSpan {
        file: file.replace('\\', "/"),
        start_line: u32::try_from(start).ok()?,
        end_line: u32::try_from(end).ok()?.max(u32::try_from(start).ok()?),
    })
}

fn flag(graph: &Value, id: &str, key: &str) -> bool {
    graph_nodes(graph).iter().any(|node| {
        node_id(node).as_deref() == Some(id) && node.get(key) == Some(&Value::Bool(true))
    })
}

fn high_risk(graph: &Value, id: &str) -> bool {
    graph_nodes(graph).iter().any(|node| {
        node_id(node).as_deref() == Some(id)
            && matches!(
                node.get("risk").and_then(Value::as_str),
                Some("high" | "critical")
            )
    })
}

fn string_set(graph: &Value, key: &str) -> std::collections::BTreeSet<String> {
    graph
        .get(key)
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(Value::as_str)
        .map(str::to_owned)
        .collect()
}

fn overlap_len(range: LineRange, start: u32, end: u32) -> u64 {
    let lo = range.start.max(start);
    let hi = range.end.min(end);
    if hi < lo {
        0
    } else {
        u64::from(hi.saturating_sub(lo).saturating_add(1))
    }
}

fn paths_eq(left: &str, right: &str) -> bool {
    left.replace('\\', "/") == right.replace('\\', "/")
}

fn milles(covered: u64, instrumented: u64) -> u64 {
    if instrumented == 0 {
        0
    } else {
        covered.saturating_mul(1000) / instrumented
    }
}

fn cov_finding(check: &str, severity: Severity, id: &str, summary: String) -> QualityFinding {
    let mut finding = QualityFinding::new(
        CheckId::new(check).expect("static WVQ coverage check ids are non-empty"),
        severity,
        SubjectRef::GraphNode(id.to_owned()),
        summary,
    );
    finding.weavatrix_fingerprint = Some(id.to_owned());
    finding
}