heal-cli 0.4.0

Hook-driven Evaluation & Autonomous Loop — code-health harness CLI for AI coding agents
Documentation
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
//! lcov.info reader. lcov is a flat per-record format the major
//! coverage reporters all converge on:
//!
//! - `cargo llvm-cov` → `target/llvm-cov/lcov.info`
//! - `pytest --cov` → `coverage/lcov.info` (via `coverage.lcov`)
//! - `nyc` (Istanbul) → `coverage/lcov.info` or `lcov-report/lcov.info`
//! - `scoverage` → `target/scoverage-report/scoverage.xml` + lcov export
//!
//! Format (one record per source file, terminated by `end_of_record`):
//! ```text
//! SF:<path>
//! DA:<line>,<hits>            # one per executable line
//! LF:<count>                  # total instrumented lines
//! LH:<count>                  # lines hit
//! BRDA:<line>,<block>,<branch>,<taken>
//! BRF:<count>                 # total branches
//! BRH:<count>                 # branches hit
//! end_of_record
//! ```
//!
//! Reporter dialects diverge on which records are emitted (some skip
//! per-line `DA` and only summarise via `LF`/`LH`; some omit `BRDA`).
//! The reader is permissive: unknown record kinds are ignored, and
//! a missing `LF`/`LH` is recovered from the `DA` lines when present.

use std::fs;
use std::path::{Path, PathBuf};

use crate::core::error::{Error, Result};

/// Coverage summary for one source file. `path` is rendered exactly as
/// the lcov record carried it (some reporters write absolute paths,
/// some project-relative); the caller is responsible for normalising
/// against the project root via [`normalise_lcov_path`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LcovFile {
    pub path: PathBuf,
    pub lines_found: u32,
    pub lines_hit: u32,
    pub branches_found: u32,
    pub branches_hit: u32,
}

impl LcovFile {
    /// Line coverage as a 0.0–1.0 ratio. Files with no instrumented
    /// lines (`lines_found == 0`) return `1.0` — by convention, a
    /// file with no executable lines is fully covered (it carries no
    /// risk, so penalising it would create dead-coverage signal).
    #[must_use]
    pub fn line_coverage_ratio(&self) -> f64 {
        if self.lines_found == 0 {
            return 1.0;
        }
        f64::from(self.lines_hit) / f64::from(self.lines_found)
    }

    /// Coverage as a 0.0–100.0 percentage; the form fed into
    /// `[calibration.coverage_pct]` after inversion.
    #[must_use]
    pub fn line_coverage_pct(&self) -> f64 {
        self.line_coverage_ratio() * 100.0
    }
}

/// Parsed lcov file as a flat list of per-source records. Order
/// matches the file order; duplicates (same `SF`) are merged additively
/// because some reporters emit one record per test entry-point and
/// expect the consumer to sum them.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct LcovReport {
    pub files: Vec<LcovFile>,
}

impl LcovReport {
    /// Read and parse `path`. Missing files are surfaced as
    /// [`Error::Io`] — callers (the Coverage observer's path-probe
    /// loop) treat absence as "no record" and continue.
    pub fn read(path: &Path) -> Result<Self> {
        let raw = fs::read_to_string(path).map_err(|e| Error::Io {
            path: path.to_path_buf(),
            source: e,
        })?;
        Ok(Self::parse(&raw))
    }

    /// Parse a raw lcov body. Permissive: unknown record kinds and
    /// malformed numeric fields are skipped silently. The only fatal
    /// case is "no `end_of_record` ever, no `SF` ever" — which yields
    /// an empty report, also non-fatal.
    #[must_use]
    pub fn parse(body: &str) -> Self {
        let mut files: Vec<LcovFile> = Vec::new();
        let mut cur: Option<LcovRecord> = None;
        for raw_line in body.lines() {
            let line = raw_line.trim();
            if line.is_empty() {
                continue;
            }
            if line == "end_of_record" {
                if let Some(rec) = cur.take() {
                    files.push(rec.finalise());
                }
                continue;
            }
            let Some((kind, value)) = line.split_once(':') else {
                continue;
            };
            // Unknown record kinds (e.g. `TN:<test_name>` test-name
            // headers, `VER:`, reporter-private extensions) fall through
            // the `_ => {}` arm and are silently ignored.
            match kind {
                "SF" => {
                    if let Some(rec) = cur.take() {
                        files.push(rec.finalise());
                    }
                    cur = Some(LcovRecord::new(PathBuf::from(value)));
                }
                "DA" => {
                    if let Some(rec) = cur.as_mut() {
                        rec.note_da(value);
                    }
                }
                "LF" => {
                    if let Some(rec) = cur.as_mut() {
                        if let Ok(n) = value.parse::<u32>() {
                            rec.lf = Some(n);
                        }
                    }
                }
                "LH" => {
                    if let Some(rec) = cur.as_mut() {
                        if let Ok(n) = value.parse::<u32>() {
                            rec.lh = Some(n);
                        }
                    }
                }
                "BRF" => {
                    if let Some(rec) = cur.as_mut() {
                        if let Ok(n) = value.parse::<u32>() {
                            rec.brf = Some(n);
                        }
                    }
                }
                "BRH" => {
                    if let Some(rec) = cur.as_mut() {
                        if let Ok(n) = value.parse::<u32>() {
                            rec.brh = Some(n);
                        }
                    }
                }
                "BRDA" => {
                    if let Some(rec) = cur.as_mut() {
                        rec.note_brda(value);
                    }
                }
                _ => {}
            }
        }
        // Some reporters omit a final `end_of_record`. Don't drop the tail.
        if let Some(rec) = cur.take() {
            files.push(rec.finalise());
        }
        Self::merge_duplicates(files)
    }

    /// Merge repeat records for the same `SF` by taking the max of
    /// each counter (some reporters emit one record per test
    /// entry-point and would double-count under naive sum).
    /// Fast-paths the common case where every `SF` is unique to skip
    /// the merge map entirely.
    fn merge_duplicates(files: Vec<LcovFile>) -> Self {
        let mut seen: std::collections::HashSet<PathBuf> =
            std::collections::HashSet::with_capacity(files.len());
        if files.iter().all(|f| seen.insert(f.path.clone())) {
            return Self { files };
        }
        let mut by_path: std::collections::HashMap<PathBuf, usize> =
            std::collections::HashMap::with_capacity(files.len());
        let mut merged: Vec<LcovFile> = Vec::with_capacity(files.len());
        for f in files {
            if let Some(idx) = by_path.get(&f.path).copied() {
                let e = &mut merged[idx];
                e.lines_found = e.lines_found.max(f.lines_found);
                e.lines_hit = e.lines_hit.max(f.lines_hit);
                e.branches_found = e.branches_found.max(f.branches_found);
                e.branches_hit = e.branches_hit.max(f.branches_hit);
            } else {
                by_path.insert(f.path.clone(), merged.len());
                merged.push(f);
            }
        }
        Self { files: merged }
    }
}

/// In-progress record state. Aggregates `DA` / `BRDA` lines as the
/// parser walks the file; finalise resolves `LF` / `LH` either from
/// the explicit summary records or from the `DA` aggregate.
struct LcovRecord {
    path: PathBuf,
    da_total: u32,
    da_hit: u32,
    brda_total: u32,
    brda_hit: u32,
    lf: Option<u32>,
    lh: Option<u32>,
    brf: Option<u32>,
    brh: Option<u32>,
}

impl LcovRecord {
    fn new(path: PathBuf) -> Self {
        Self {
            path,
            da_total: 0,
            da_hit: 0,
            brda_total: 0,
            brda_hit: 0,
            lf: None,
            lh: None,
            brf: None,
            brh: None,
        }
    }

    fn note_da(&mut self, value: &str) {
        // `DA:<line>,<hits>[,<checksum>]` — checksum optional.
        let Some((_line, rest)) = value.split_once(',') else {
            return;
        };
        let hits_str = rest.split_once(',').map_or(rest, |(h, _)| h);
        let Ok(hits) = hits_str.parse::<u32>() else {
            return;
        };
        self.da_total = self.da_total.saturating_add(1);
        if hits > 0 {
            self.da_hit = self.da_hit.saturating_add(1);
        }
    }

    fn note_brda(&mut self, value: &str) {
        // `BRDA:<line>,<block>,<branch>,<taken>` — `taken` is `-` for
        // never-evaluated, integer otherwise.
        let Some(taken) = value.rsplit(',').next() else {
            return;
        };
        self.brda_total = self.brda_total.saturating_add(1);
        if taken != "-" && taken != "0" {
            self.brda_hit = self.brda_hit.saturating_add(1);
        }
    }

    fn finalise(self) -> LcovFile {
        LcovFile {
            path: self.path,
            lines_found: self.lf.unwrap_or(self.da_total),
            lines_hit: self.lh.unwrap_or(self.da_hit),
            branches_found: self.brf.unwrap_or(self.brda_total),
            branches_hit: self.brh.unwrap_or(self.brda_hit),
        }
    }
}

/// Resolve an `SF:` path against the project root. lcov reporters
/// disagree on absolute vs relative — `cargo llvm-cov` writes
/// absolute, `nyc` writes relative-to-cwd. We strip the project prefix
/// when present so downstream observers can join with their own
/// project-relative paths.
#[must_use]
pub fn normalise_lcov_path(project: &Path, lcov_path: &Path) -> PathBuf {
    if let Ok(stripped) = lcov_path.strip_prefix(project) {
        return stripped.to_path_buf();
    }
    lcov_path.to_path_buf()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_minimal_two_file_record() {
        let raw = "\
SF:src/foo.rs
DA:1,1
DA:2,1
DA:3,0
LF:3
LH:2
end_of_record
SF:src/bar.rs
DA:1,0
DA:2,0
LF:2
LH:0
end_of_record
";
        let report = LcovReport::parse(raw);
        assert_eq!(report.files.len(), 2);
        assert_eq!(report.files[0].path, PathBuf::from("src/foo.rs"));
        assert_eq!(report.files[0].lines_found, 3);
        assert_eq!(report.files[0].lines_hit, 2);
        assert!((report.files[0].line_coverage_ratio() - 2.0 / 3.0).abs() < 1e-9);
        assert_eq!(report.files[1].lines_hit, 0);
        assert!(report.files[1].line_coverage_ratio() < 1e-9);
    }

    #[test]
    fn recovers_lf_lh_from_da_when_summary_missing() {
        // `pytest-cov` historically omits `LF`/`LH`; the reader infers
        // from `DA` lines.
        let raw = "\
SF:api/users.py
DA:10,5
DA:11,5
DA:12,0
end_of_record
";
        let report = LcovReport::parse(raw);
        assert_eq!(report.files[0].lines_found, 3);
        assert_eq!(report.files[0].lines_hit, 2);
    }

    #[test]
    fn unknown_records_and_test_names_are_ignored() {
        let raw = "\
TN:my_test_suite
SF:src/lib.rs
VER:1.0
DA:1,1
DA:2,3
LF:2
LH:2
end_of_record
";
        let report = LcovReport::parse(raw);
        assert_eq!(report.files.len(), 1);
        assert_eq!(report.files[0].lines_hit, 2);
    }

    #[test]
    fn missing_final_end_of_record_still_emits_record() {
        let raw = "\
SF:src/lib.rs
DA:1,1
LF:1
LH:1
";
        let report = LcovReport::parse(raw);
        assert_eq!(report.files.len(), 1);
    }

    #[test]
    fn duplicate_sf_records_merge_taking_max() {
        // Some reporters emit one record per entry-point. Take the max
        // because adding would risk double-counting when two entry
        // points exercise overlapping lines.
        let raw = "\
SF:src/lib.rs
LF:10
LH:6
end_of_record
SF:src/lib.rs
LF:10
LH:8
end_of_record
";
        let report = LcovReport::parse(raw);
        assert_eq!(report.files.len(), 1);
        assert_eq!(report.files[0].lines_hit, 8);
    }

    #[test]
    fn branch_coverage_records_are_aggregated() {
        let raw = "\
SF:src/lib.rs
BRDA:10,0,0,3
BRDA:10,0,1,-
BRDA:11,0,0,0
BRF:3
BRH:1
end_of_record
";
        let report = LcovReport::parse(raw);
        assert_eq!(report.files[0].branches_found, 3);
        assert_eq!(report.files[0].branches_hit, 1);
    }

    #[test]
    fn no_instrumented_lines_returns_full_coverage() {
        let f = LcovFile {
            path: PathBuf::from("src/empty.rs"),
            lines_found: 0,
            lines_hit: 0,
            branches_found: 0,
            branches_hit: 0,
        };
        assert!((f.line_coverage_ratio() - 1.0).abs() < 1e-9);
    }

    #[test]
    fn normalise_lcov_path_strips_project_prefix() {
        let project = PathBuf::from("/work/repo");
        let abs = PathBuf::from("/work/repo/src/lib.rs");
        let rel = PathBuf::from("src/lib.rs");
        assert_eq!(normalise_lcov_path(&project, &abs), rel);
        assert_eq!(normalise_lcov_path(&project, &rel), rel);
    }
}