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
410
411
//! `heal metrics` — render observer outputs as a human summary or
//! `--json` payload.
//!
//! Every invocation runs the observers fresh and renders directly —
//! no `.heal/snapshots/` reads, no historical delta. The orchestrator
//! dispatches over the section registry; no metric-specific branching
//! here.

mod code;
mod docs;
mod section;
mod test;

use std::io::{self, Write};
use std::path::Path;

use anyhow::Result;
use serde_json::json;

use crate::cli::MetricKind;
use crate::core::config::load_from_project;
use crate::core::term::write_through_pager;
use crate::core::HealPaths;
use crate::feature::Family;
use crate::observers::run_all;

use section::{all_sections, SectionCtx};

pub fn run(
    project: &Path,
    json_output: bool,
    metric: Option<MetricKind>,
    feature: Option<Family>,
    workspace: Option<&Path>,
    no_pager: bool,
) -> Result<()> {
    let paths = HealPaths::new(project);
    let cfg_exists = paths.config().exists();

    let cfg = if cfg_exists {
        Some(load_from_project(project)?)
    } else {
        None
    };

    // Early-exit when `--feature <disabled>` would otherwise produce
    // an empty payload. Mirrors `heal status`'s contract — skills
    // shell out and read the exit code to decide whether to bail.
    if let (Some(family), Some(cfg_ref)) = (feature, cfg.as_ref()) {
        if !family.is_enabled(cfg_ref) {
            eprintln!(
                "heal metrics: --feature {0} requested but `[features.{0}].enabled = false`. \
                 Edit `.heal/config.toml` (or run `/heal-setup`) to enable the family before re-running.",
                family.name(),
            );
            std::process::exit(1);
        }
    }

    let reports = cfg.as_ref().map(|c| run_all(project, c, metric, workspace));

    let sections = all_sections();

    if json_output {
        println!(
            "{}",
            serde_json::to_string_pretty(&build_json(
                cfg_exists,
                cfg.as_ref(),
                reports.as_ref(),
                metric,
                feature,
                workspace,
                &sections,
            ))?,
        );
        return Ok(());
    }

    if !cfg_exists {
        println!("HEAL is not initialized in this project. Run `heal init` first.");
        return Ok(());
    }
    let cfg = cfg.expect("cfg_exists branch implies cfg loaded");
    let reports = reports.expect("cfg present implies reports built");
    write_through_pager(no_pager, |w, colorize| {
        let ctx = SectionCtx {
            cfg: &cfg,
            reports: &reports,
            colorize,
        };
        write_header(w, project, &paths, workspace)?;
        for s in &sections {
            if !matches_metric(metric, s.metric()) {
                continue;
            }
            if !matches_family(feature, s.metric()) {
                continue;
            }
            s.render_text(&ctx, w)?;
        }
        Ok(())
    })
}

fn write_header(
    w: &mut dyn Write,
    project: &Path,
    paths: &HealPaths,
    workspace: Option<&Path>,
) -> io::Result<()> {
    writeln!(w, "HEAL metrics (project: {})", project.display())?;
    writeln!(w, "  config:            {}", paths.config().display())?;
    if let Some(ws) = workspace {
        writeln!(w, "  workspace:         {}", ws.display())?;
    }
    Ok(())
}

/// `None` means "no filter, print everything"; otherwise print only when
/// the section matches the requested metric.
fn matches_metric(filter: Option<MetricKind>, section: MetricKind) -> bool {
    filter.is_none_or(|f| f == section)
}

/// `None` means "no family filter"; otherwise print only when the
/// section's metric belongs to the requested family. Resolves via
/// `Family::for_metric` so the section list and `Finding.family()`
/// stay in lock-step against one canonical map.
fn matches_family(filter: Option<Family>, section: MetricKind) -> bool {
    filter.is_none_or(|f| Family::for_metric(section.json_key()) == f)
}

fn build_json(
    cfg_exists: bool,
    cfg: Option<&crate::core::config::Config>,
    reports: Option<&crate::observers::ObserverReports>,
    metric: Option<MetricKind>,
    feature: Option<Family>,
    workspace: Option<&Path>,
    sections: &[Box<dyn section::MetricSection>],
) -> serde_json::Value {
    let mut payload = serde_json::Map::new();
    payload.insert("initialized".into(), json!(cfg_exists));
    if let Some(m) = metric {
        payload.insert("metric".into(), json!(m.json_key()));
    }
    if let Some(f) = feature {
        payload.insert(
            "feature".into(),
            json!(match f {
                Family::Code => "code",
                Family::Test => "test",
                Family::Docs => "docs",
            }),
        );
    }
    if let Some(ws) = workspace {
        payload.insert("workspace".into(), json!(ws.display().to_string()));
    }
    if let (Some(cfg), Some(reports)) = (cfg, reports) {
        let ctx = SectionCtx {
            cfg,
            reports,
            colorize: false,
        };
        // Raw reports balloon for large repos (the `worst` precomputation
        // already captures what filtered consumers need); only emit them
        // in the unfiltered path so `--metric X --json` stays lean for
        // skill consumption. `--feature X` keeps the raw shape but
        // narrows the included keys to that family's metrics.
        if metric.is_none() {
            for s in sections {
                if !matches_family(feature, s.metric()) {
                    continue;
                }
                payload.insert(s.metric().json_key().into(), s.raw_json(&ctx));
            }
        } else if let Some(m) = metric {
            if let Some(s) = sections.iter().find(|s| s.metric() == m) {
                let (top_n, worst) = s.worst_json(&ctx);
                payload.insert("top_n".into(), json!(top_n));
                payload.insert("worst".into(), worst);
            }
        }
    }
    serde_json::Value::Object(payload)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::observers::run_all;
    use crate::test_support::init_project_with_config;
    use tempfile::TempDir;

    fn init_project(dir: &Path) {
        // Path-only delegation: every caller below uses HealPaths::new
        // independently, so we discard the returned handle.
        let _ = init_project_with_config(dir, "pub fn add(a: i32, b: i32) -> i32 { a + b }\n");
    }

    #[test]
    fn matches_metric_passes_when_filter_none_or_equal() {
        assert!(matches_metric(None, MetricKind::Loc));
        assert!(matches_metric(Some(MetricKind::Loc), MetricKind::Loc));
        assert!(!matches_metric(Some(MetricKind::Loc), MetricKind::Churn));
    }

    #[test]
    fn matches_family_routes_via_family_for_metric() {
        assert!(matches_family(None, MetricKind::Loc));
        assert!(matches_family(Some(Family::Code), MetricKind::Loc));
        assert!(!matches_family(Some(Family::Test), MetricKind::Loc));
        assert!(matches_family(Some(Family::Test), MetricKind::CoveragePct));
        assert!(matches_family(Some(Family::Docs), MetricKind::DocFreshness));
    }

    #[test]
    fn run_text_uninitialized_returns_ok() {
        let dir = TempDir::new().unwrap();
        // No `heal init` — exercise the "HEAL is not initialized" branch.
        run(dir.path(), false, None, None, None, true).unwrap();
    }

    #[test]
    fn run_json_uninitialized_returns_initialized_false() {
        let dir = TempDir::new().unwrap();
        let cfg_exists = HealPaths::new(dir.path()).config().exists();
        assert!(!cfg_exists);
        let payload = build_json(cfg_exists, None, None, None, None, None, &all_sections());
        assert_eq!(payload["initialized"], serde_json::Value::Bool(false));
        // Without cfg/reports, neither metric data nor the `worst`
        // payload should be present — only the `initialized` flag.
        assert!(payload.get("loc").is_none());
        assert!(payload.get("worst").is_none());
    }

    #[test]
    fn run_text_initialized_walks_every_section_render() {
        let dir = TempDir::new().unwrap();
        init_project(dir.path());
        // Text mode end-to-end. `no_pager = true` so tests never spawn
        // `less`. Asserts that every section's `render_text` returns
        // `Ok(())` against a real (but minimal) project.
        run(dir.path(), false, None, None, None, true).unwrap();
    }

    #[test]
    fn run_json_initialized_unfiltered_returns_ok() {
        let dir = TempDir::new().unwrap();
        init_project(dir.path());
        run(dir.path(), true, None, None, None, true).unwrap();
    }

    #[test]
    fn run_json_with_metric_filter_returns_ok() {
        let dir = TempDir::new().unwrap();
        init_project(dir.path());
        run(dir.path(), true, Some(MetricKind::Loc), None, None, true).unwrap();
    }

    #[test]
    fn run_json_with_feature_filter_returns_ok() {
        let dir = TempDir::new().unwrap();
        init_project(dir.path());
        run(dir.path(), true, None, Some(Family::Code), None, true).unwrap();
    }

    #[test]
    fn build_json_unfiltered_includes_every_section_key() {
        let dir = TempDir::new().unwrap();
        init_project(dir.path());
        let cfg = load_from_project(dir.path()).unwrap();
        let reports = run_all(dir.path(), &cfg, None, None);
        let sections = all_sections();

        let payload = build_json(
            true,
            Some(&cfg),
            Some(&reports),
            None,
            None,
            None,
            &sections,
        );
        assert_eq!(payload["initialized"], serde_json::Value::Bool(true));
        // Every section's `json_key` must appear under the unfiltered
        // path, regardless of whether its observer produced signal.
        // Code-family observers always run; docs/test default to off
        // and serialize as `null` — both cases are valid keys.
        for s in &sections {
            let key = s.metric().json_key();
            assert!(
                payload.get(key).is_some(),
                "unfiltered payload must include `{key}`",
            );
        }
        // The Loc observer always emits a non-null report for any
        // walkable project, so `payload[\"loc\"]` is the canary.
        assert!(
            !payload["loc"].is_null(),
            "loc must serialize as a non-null report",
        );
    }

    #[test]
    fn build_json_metric_filter_emits_top_n_and_worst() {
        let dir = TempDir::new().unwrap();
        init_project(dir.path());
        let cfg = load_from_project(dir.path()).unwrap();
        let reports = run_all(dir.path(), &cfg, Some(MetricKind::Loc), None);
        let sections = all_sections();

        let payload = build_json(
            true,
            Some(&cfg),
            Some(&reports),
            Some(MetricKind::Loc),
            None,
            None,
            &sections,
        );
        assert_eq!(payload["metric"], serde_json::Value::String("loc".into()));
        assert!(
            payload["top_n"].is_number(),
            "metric-filter path must echo the configured top_n",
        );
        assert!(
            payload.get("worst").is_some(),
            "metric-filter path must emit the worst payload",
        );
        // Per-section raw keys are excluded under `--metric`; only
        // `top_n` + `worst` carry the slice the skill consumes.
        assert!(payload.get("loc").is_none());
        assert!(payload.get("complexity").is_none());
    }

    #[test]
    fn build_json_feature_filter_narrows_to_family_keys() {
        let dir = TempDir::new().unwrap();
        init_project(dir.path());
        let cfg = load_from_project(dir.path()).unwrap();
        let reports = run_all(dir.path(), &cfg, None, None);
        let sections = all_sections();

        let payload = build_json(
            true,
            Some(&cfg),
            Some(&reports),
            None,
            Some(Family::Code),
            None,
            &sections,
        );
        assert_eq!(payload["feature"], serde_json::Value::String("code".into()));
        // Code-family keys present.
        for k in [
            "loc",
            "complexity",
            "churn",
            "change_coupling",
            "duplication",
            "hotspot",
            "lcom",
        ] {
            assert!(payload.get(k).is_some(), "code-family key `{k}` missing");
        }
        // Docs / test family keys absent.
        for k in [
            "doc_freshness",
            "doc_drift",
            "doc_coverage",
            "doc_link_health",
            "orphan_pages",
            "todo_density",
            "doc_hotspot",
            "coverage_pct",
            "skip_ratio",
            "test_hotspot",
        ] {
            assert!(
                payload.get(k).is_none(),
                "non-code key `{k}` leaked under `--feature code`",
            );
        }
    }

    #[test]
    fn build_json_workspace_echoes_path() {
        let dir = TempDir::new().unwrap();
        init_project(dir.path());
        let ws = dir.path().join("crates/foo");
        let cfg = load_from_project(dir.path()).unwrap();
        let reports = run_all(dir.path(), &cfg, None, Some(&ws));
        let sections = all_sections();
        let payload = build_json(
            true,
            Some(&cfg),
            Some(&reports),
            None,
            None,
            Some(&ws),
            &sections,
        );
        assert_eq!(
            payload["workspace"],
            serde_json::Value::String(ws.display().to_string()),
        );
    }
}