heal-cli 0.2.1

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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! `heal fix *` — operations on the fix-tracking state attached to
//! `.heal/checks/`. Read-only browsing of the record stream lives at
//! `heal checks`; this command surface focuses on the per-record /
//! per-finding actions a fix workflow needs.
//!
//! Sub-commands:
//! - `fix show <id>`  — detailed render of one record (unstable view;
//!   use `--json` for the stable shape).
//! - `fix diff`       — bucket findings into Resolved / Regressed /
//!   Improved / New / Unchanged. Argument shape mirrors `git diff`:
//!   no args = latest cache vs a live in-memory scan; `<from>` =
//!   `<from>` vs live; `<from> <to>` = two cached records. The live
//!   scan is never written to disk.
//! - `fix mark`       — append a `FixedFinding` line to
//!   `.heal/checks/fixed.jsonl` (the only `fix` command that writes).
//!
//! When the diff runs in "vs live" mode and every finding in the
//! FROM record has been logged to `fixed.jsonl`, the renderer drops
//! a hint suggesting `heal check --refresh` so the reconciliation
//! pass can either retire those marks or surface regressions.

use std::collections::HashMap;
use std::io::{IsTerminal, Write};
use std::path::Path;

use anyhow::{Context, Result};
use chrono::Utc;
use serde::Serialize;

use crate::cli::FixAction;
use crate::commands::check::{render, Filters};
use crate::core::calibration::Calibration;
use crate::core::check_cache::{
    append_fixed, config_hash_from_paths, find_by_id, iter_records, read_latest, CheckRecord,
    FixedFinding,
};
use crate::core::config::load_from_project;
use crate::core::finding::Finding;
use crate::core::severity::Severity;
use crate::core::snapshot::{ansi_wrap, ANSI_CYAN, ANSI_GREEN, ANSI_RED, ANSI_YELLOW};
use crate::core::HealPaths;
use crate::observer::git;

pub fn run(project: &Path, action: FixAction) -> Result<()> {
    let paths = HealPaths::new(project);
    match action {
        FixAction::Show { check_id, json } => run_show(&paths, &check_id, json),
        FixAction::Diff {
            from,
            to,
            all,
            json,
        } => run_diff(project, from.as_deref(), to.as_deref(), all, json),
        FixAction::Mark {
            finding_id,
            commit_sha,
        } => run_mark(&paths, &finding_id, &commit_sha),
    }
}

fn run_mark(paths: &HealPaths, finding_id: &str, commit_sha: &str) -> Result<()> {
    let entry = FixedFinding {
        finding_id: finding_id.to_owned(),
        commit_sha: commit_sha.to_owned(),
        fixed_at: Utc::now(),
    };
    append_fixed(&paths.checks_fixed_log(), &entry)?;
    println!(
        "marked {finding_id} as fixed by {commit_sha} (logged to {})",
        paths.checks_fixed_log().display(),
    );
    Ok(())
}

fn run_show(paths: &HealPaths, check_id: &str, as_json: bool) -> Result<()> {
    let records = iter_records(&paths.checks_dir())?;
    let record = find_by_id(&records, check_id)
        .cloned()
        .ok_or_else(|| anyhow::anyhow!("no CheckRecord with check_id={check_id}"))?;
    if as_json {
        println!(
            "{}",
            serde_json::to_string_pretty(&record).expect("CheckRecord serialization is infallible")
        );
        return Ok(());
    }
    eprintln!(
        "warning: `heal fix show` rendering is unstable; use `--json` for a stable contract.",
    );
    let mut stdout = std::io::stdout();
    let colorize = stdout.is_terminal();
    // Show full detail — turn on `--all` semantics so Medium/Ok aren't hidden.
    let filters = Filters {
        all: true,
        ..Filters::default()
    };
    render(&record, &[], &filters, colorize, &mut stdout)?;
    Ok(())
}

fn run_diff(
    project: &Path,
    from: Option<&str>,
    to: Option<&str>,
    show_all: bool,
    as_json: bool,
) -> Result<()> {
    let paths = HealPaths::new(project);

    // Defer the (potentially heavy) `iter_records` decode until we know
    // we need it: only an explicit FROM/TO id requires walking the
    // segments. The default FROM reads `latest.json` directly, and the
    // default TO is a fresh observer scan.
    let cached_records = if from.is_some() || to.is_some() {
        Some(iter_records(&paths.checks_dir())?)
    } else {
        None
    };

    let to_record = match to {
        Some(id) => find_by_id(cached_records.as_deref().expect("loaded above"), id)
            .cloned()
            .ok_or_else(|| anyhow::anyhow!("no CheckRecord with check_id={id} (TO)"))?,
        None => build_live_record(project, &paths)?,
    };

    let from_record = match from {
        Some(id) => find_by_id(cached_records.as_deref().expect("loaded above"), id)
            .cloned()
            .ok_or_else(|| anyhow::anyhow!("no CheckRecord with check_id={id} (FROM)"))?,
        None => read_latest(&paths.checks_latest())?.ok_or_else(|| {
            anyhow::anyhow!(
                "no cache yet at {} — run `heal check` first",
                paths.checks_latest().display()
            )
        })?,
    };

    let diff = compute_diff(&from_record, &to_record);

    if as_json {
        println!(
            "{}",
            serde_json::to_string_pretty(&diff).expect("FixDiff serialization is infallible")
        );
        return Ok(());
    }

    let mut stdout = std::io::stdout();
    let colorize = stdout.is_terminal();
    render_diff(
        &from_record,
        &to_record,
        &diff,
        show_all,
        colorize,
        &mut stdout,
    )?;

    // When every finding in the FROM record has been logged to
    // `fixed.jsonl`, the user is sitting on a session whose `mark`s
    // haven't been reconciled yet — `heal check --refresh` either
    // drops those entries (genuinely fixed) or moves them to
    // `regressed.jsonl` (the mark was wrong). Only meaningful in
    // vs-live mode against a non-empty FROM.
    if to.is_none() && !from_record.findings.is_empty() {
        if let Some(hint) = all_marked_hint(&paths, &from_record)? {
            writeln!(stdout, "{hint}")?;
        }
    }
    Ok(())
}

fn build_live_record(project: &Path, paths: &HealPaths) -> Result<CheckRecord> {
    let cfg = load_from_project(project).with_context(|| {
        format!(
            "loading {} (run `heal init` first?)",
            paths.config().display(),
        )
    })?;
    let calibration = Calibration::load(&paths.calibration())
        .ok()
        .map(|c| c.with_overrides(&cfg));
    let head_sha = git::head_sha(project);
    let worktree_clean = git::worktree_clean(project).unwrap_or(false);
    let config_hash = config_hash_from_paths(&paths.config(), &paths.calibration());
    Ok(crate::commands::check::build_fresh_record(
        project,
        &cfg,
        calibration.as_ref(),
        head_sha,
        worktree_clean,
        config_hash,
    ))
}

/// Returns a one-line nudge when every finding in `from` has been
/// logged in `fixed.jsonl`. `Ok(None)` when not all findings are
/// marked, or when `fixed.jsonl` is missing/empty.
fn all_marked_hint(paths: &HealPaths, from: &CheckRecord) -> Result<Option<String>> {
    use crate::core::check_cache::read_fixed;
    let fixed = read_fixed(&paths.checks_fixed_log())?;
    if fixed.is_empty() {
        return Ok(None);
    }
    let marked: std::collections::HashSet<&str> =
        fixed.iter().map(|f| f.finding_id.as_str()).collect();
    if from.findings.iter().all(|f| marked.contains(f.id.as_str())) {
        Ok(Some(
            "Hint: every finding in the cache is marked fixed — run \
             `heal check --refresh` to reconcile fixed.jsonl ↔ regressed.jsonl."
                .to_owned(),
        ))
    } else {
        Ok(None)
    }
}

#[derive(Debug, Clone, Serialize, Default)]
pub(super) struct FixDiff {
    pub resolved: Vec<DiffEntry>,
    pub regressed: Vec<DiffEntry>,
    pub improved: Vec<DiffEntry>,
    pub new_findings: Vec<DiffEntry>,
    pub unchanged: Vec<DiffEntry>,
    /// `resolved.len() / from.findings.len()` as a `[0.0, 1.0]` ratio.
    /// Picked so the math matches the TODO mock: "3 resolved / 12 total
    /// → 25%". `total` here is the prior-run finding count.
    pub progress_pct: f64,
}

#[derive(Debug, Clone, Serialize)]
pub(super) struct DiffEntry {
    pub finding_id: String,
    pub metric: String,
    pub file: String,
    /// Prior Severity (None for `new_findings`).
    pub from_severity: Option<Severity>,
    /// Current Severity (None for `resolved`).
    pub to_severity: Option<Severity>,
    pub hotspot: bool,
}

fn compute_diff(from: &CheckRecord, to: &CheckRecord) -> FixDiff {
    let from_by_id: HashMap<&str, &Finding> =
        from.findings.iter().map(|f| (f.id.as_str(), f)).collect();
    let to_by_id: HashMap<&str, &Finding> =
        to.findings.iter().map(|f| (f.id.as_str(), f)).collect();

    let mut diff = FixDiff::default();

    for (id, prev) in &from_by_id {
        match to_by_id.get(id) {
            None => diff.resolved.push(DiffEntry::from_pair(prev, None)),
            Some(curr) if curr.severity > prev.severity => {
                diff.regressed.push(DiffEntry::from_pair(prev, Some(curr)));
            }
            Some(curr) if curr.severity < prev.severity => {
                diff.improved.push(DiffEntry::from_pair(prev, Some(curr)));
            }
            Some(curr) => diff.unchanged.push(DiffEntry::from_pair(prev, Some(curr))),
        }
    }
    for (id, curr) in &to_by_id {
        if !from_by_id.contains_key(id) {
            diff.new_findings.push(DiffEntry::from_new(curr));
        }
    }

    let total = from.findings.len();
    diff.progress_pct = if total == 0 {
        0.0
    } else {
        #[allow(clippy::cast_precision_loss)]
        let pct = diff.resolved.len() as f64 / total as f64;
        pct
    };
    diff
}

impl DiffEntry {
    fn from_pair(prev: &Finding, curr: Option<&Finding>) -> Self {
        Self {
            finding_id: prev.id.clone(),
            metric: prev.metric.clone(),
            file: prev.location.file.display().to_string(),
            from_severity: Some(prev.severity),
            to_severity: curr.map(|c| c.severity),
            hotspot: curr.map_or(prev.hotspot, |c| c.hotspot),
        }
    }
    fn from_new(curr: &Finding) -> Self {
        Self {
            finding_id: curr.id.clone(),
            metric: curr.metric.clone(),
            file: curr.location.file.display().to_string(),
            from_severity: None,
            to_severity: Some(curr.severity),
            hotspot: curr.hotspot,
        }
    }
}

fn render_diff(
    from: &CheckRecord,
    to: &CheckRecord,
    diff: &FixDiff,
    show_all: bool,
    colorize: bool,
    out: &mut impl Write,
) -> Result<()> {
    let title = ansi_wrap(ANSI_CYAN, "── HEAL fix diff", colorize);
    let bar: String = "".repeat(45);
    writeln!(out, "{title} {bar}")?;
    writeln!(
        out,
        "  from: {}  HEAD={}  ({} findings)",
        from.started_at.format("%Y-%m-%d %H:%M"),
        from.head_sha.as_deref().unwrap_or(""),
        from.findings.len(),
    )?;
    writeln!(
        out,
        "  to:   {}  HEAD={}  ({} findings)",
        to.started_at.format("%Y-%m-%d %H:%M"),
        to.head_sha.as_deref().unwrap_or(""),
        to.findings.len(),
    )?;
    writeln!(out)?;

    render_bucket("✅ Resolved", ANSI_GREEN, &diff.resolved, colorize, out)?;
    render_bucket("⚠️  Regressed", ANSI_RED, &diff.regressed, colorize, out)?;
    render_bucket("➕ New", ANSI_YELLOW, &diff.new_findings, colorize, out)?;
    if show_all {
        render_bucket("🟢 Improved", ANSI_GREEN, &diff.improved, colorize, out)?;
        render_bucket("▫️ Unchanged", ANSI_CYAN, &diff.unchanged, colorize, out)?;
    } else {
        let hidden = diff.improved.len() + diff.unchanged.len();
        if hidden > 0 {
            writeln!(
                out,
                "  [Improved + Unchanged: {hidden} hidden — pass --all]"
            )?;
        }
    }
    writeln!(out)?;
    let resolved = diff.resolved.len();
    let total = from.findings.len();
    if total == 0 {
        writeln!(out, "  Progress: n/a (prior run had no findings)")?;
    } else {
        writeln!(
            out,
            "  Progress: {} resolved / {} total → {:.0}% complete",
            resolved,
            total,
            diff.progress_pct * 100.0,
        )?;
    }
    let close: String = "".repeat(60);
    writeln!(out, "{close}")?;
    Ok(())
}

fn render_bucket(
    label: &str,
    color: &str,
    items: &[DiffEntry],
    colorize: bool,
    out: &mut impl Write,
) -> std::io::Result<()> {
    if items.is_empty() {
        return Ok(());
    }
    writeln!(
        out,
        "{} ({})",
        ansi_wrap(color, label, colorize),
        items.len()
    )?;
    for e in items {
        let arrow = match (e.from_severity, e.to_severity) {
            (Some(from), Some(to)) if from != to => format!("({from:?}{to:?})"),
            (Some(from), Some(_)) => format!("({from:?})"),
            (Some(from), None) => format!("({from:?} → ✓)"),
            (None, Some(to)) => format!("(new {to:?})"),
            (None, None) => String::new(),
        };
        let hot = if e.hotspot { " 🔥" } else { "" };
        writeln!(out, "  {} {} {arrow}{hot}", e.metric, e.file)?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::finding::{Finding, Location};
    use crate::core::severity::Severity;
    use std::path::PathBuf;

    fn finding(id_seed: &str, severity: Severity) -> Finding {
        let mut f = Finding::new(
            "ccn",
            Location {
                file: PathBuf::from(format!("src/{id_seed}.ts")),
                line: Some(1),
                symbol: Some(id_seed.to_owned()),
            },
            "CCN=10 fn (rust)".into(),
            id_seed,
        );
        f.severity = severity;
        f
    }

    fn record(findings: Vec<Finding>) -> CheckRecord {
        CheckRecord::new(Some("abc".into()), true, "h".into(), findings)
    }

    #[test]
    fn diff_buckets_resolved_regressed_new_unchanged() {
        let stay = finding("stay", Severity::High);
        let dropped = finding("dropped", Severity::Medium);
        let regressed_a = finding("hot", Severity::High);
        let regressed_b = {
            // Same id (same content_seed + location), higher severity.
            let mut f = finding("hot", Severity::Critical);
            f.severity = Severity::Critical;
            assert_eq!(f.id, regressed_a.id);
            f
        };
        let new_one = finding("new", Severity::High);

        let from = record(vec![dropped.clone(), regressed_a.clone(), stay.clone()]);
        let to = record(vec![regressed_b.clone(), stay.clone(), new_one.clone()]);

        let diff = compute_diff(&from, &to);

        assert_eq!(diff.resolved.len(), 1);
        assert_eq!(diff.resolved[0].file, "src/dropped.ts");
        assert_eq!(diff.regressed.len(), 1);
        assert_eq!(diff.regressed[0].file, "src/hot.ts");
        assert_eq!(diff.regressed[0].from_severity, Some(Severity::High));
        assert_eq!(diff.regressed[0].to_severity, Some(Severity::Critical));
        assert_eq!(diff.unchanged.len(), 1);
        assert_eq!(diff.unchanged[0].file, "src/stay.ts");
        assert_eq!(diff.new_findings.len(), 1);
        assert_eq!(diff.new_findings[0].file, "src/new.ts");
        assert!(diff.improved.is_empty());

        // 1 resolved out of 3 prior = 33%.
        assert!((diff.progress_pct - (1.0 / 3.0)).abs() < 1e-9);
    }

    #[test]
    fn diff_buckets_improved_when_severity_drops() {
        let mut prev = finding("calm", Severity::Critical);
        prev.severity = Severity::Critical;
        let mut curr = finding("calm", Severity::Medium);
        curr.severity = Severity::Medium;
        assert_eq!(prev.id, curr.id);
        let diff = compute_diff(&record(vec![prev]), &record(vec![curr]));
        assert_eq!(diff.improved.len(), 1);
        assert!(diff.regressed.is_empty());
        // Improved counts in `unchanged + improved` but not in resolved → 0%.
        assert!((diff.progress_pct - 0.0).abs() < 1e-9);
    }

    #[test]
    fn diff_progress_zero_when_prior_empty() {
        let diff = compute_diff(
            &record(Vec::new()),
            &record(vec![finding("only", Severity::High)]),
        );
        assert!((diff.progress_pct - 0.0).abs() < 1e-9);
        assert_eq!(diff.new_findings.len(), 1);
    }

    #[test]
    fn mark_appends_entry_with_supplied_metadata() {
        use crate::core::check_cache::read_fixed;
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let paths = HealPaths::new(tmp.path());
        paths.ensure().unwrap();

        run_mark(&paths, "ccn:src/a.rs:foo:abc", "deadbeef").unwrap();
        run_mark(&paths, "ccn:src/b.rs:bar:def", "cafebabe").unwrap();

        let entries = read_fixed(&paths.checks_fixed_log()).unwrap();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].finding_id, "ccn:src/a.rs:foo:abc");
        assert_eq!(entries[0].commit_sha, "deadbeef");
        assert_eq!(entries[1].finding_id, "ccn:src/b.rs:bar:def");
        assert_eq!(entries[1].commit_sha, "cafebabe");
    }

    #[test]
    fn all_marked_hint_fires_only_when_every_finding_is_in_fixed_log() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let paths = HealPaths::new(tmp.path());
        paths.ensure().unwrap();

        let a = finding("a", Severity::High);
        let b = finding("b", Severity::High);
        let from = record(vec![a.clone(), b.clone()]);

        // No marks yet → no hint.
        assert!(all_marked_hint(&paths, &from).unwrap().is_none());

        // Mark only one of two → still no hint.
        run_mark(&paths, &a.id, "abc1234").unwrap();
        assert!(all_marked_hint(&paths, &from).unwrap().is_none());

        // Mark the second → hint fires.
        run_mark(&paths, &b.id, "def5678").unwrap();
        let hint = all_marked_hint(&paths, &from).unwrap().expect("hint");
        assert!(
            hint.contains("heal check --refresh"),
            "hint should reference the refresh command: {hint}",
        );
    }

    #[test]
    fn all_marked_hint_skipped_for_empty_fixed_log() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let paths = HealPaths::new(tmp.path());
        paths.ensure().unwrap();

        // Empty FROM with no fixed.jsonl → no hint (avoids a false
        // positive when the user has nothing to reconcile).
        let from = record(Vec::new());
        assert!(all_marked_hint(&paths, &from).unwrap().is_none());
    }
}