aprender-contracts-cli 0.51.0

CLI for provable-contracts — validate, scaffold, verify, status, audit
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
//! `pv score` — Quantitative contract and codebase scoring.

use std::path::Path;

use std::collections::HashSet;

use provable_contracts::binding::BindingRegistry;
use provable_contracts::query::ContractIndex;
use provable_contracts::schema::parse_contract;
use provable_contracts::scoring;
use provable_contracts::scoring::drift;
use provable_contracts::scoring::pvscore_10dim;
use provable_contracts::scoring::{CodebaseScore, ContractScore, Grade, ScoringWeights};

#[allow(clippy::too_many_arguments)]
pub fn run(
    path: &Path,
    binding: Option<&Path>,
    format: &str,
    min_score: Option<f64>,
    summary: bool,
    top_gaps: usize,
    weights_json: Option<&str>,
    pvscore: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let binding_registry = binding
        .map(|p| {
            let content = std::fs::read_to_string(p)?;
            let reg: BindingRegistry = serde_yaml::from_str(&content)?;
            Ok::<_, Box<dyn std::error::Error>>(reg)
        })
        .transpose()?;

    let weights = match weights_json {
        Some(json) => serde_json::from_str::<ScoringWeights>(json)?,
        None => ScoringWeights::default(),
    };

    if path.is_dir() {
        run_directory(
            path,
            binding_registry.as_ref(),
            binding,
            format,
            min_score,
            summary,
            top_gaps,
            &weights,
            pvscore,
        )
    } else {
        run_single(path, binding_registry.as_ref(), format, min_score, &weights)
    }
}

fn run_single(
    path: &Path,
    binding: Option<&BindingRegistry>,
    format: &str,
    min_score: Option<f64>,
    weights: &ScoringWeights,
) -> Result<(), Box<dyn std::error::Error>> {
    let contract = parse_contract(path)?;
    let stem = path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("unknown");
    let score = scoring::score_contract_weighted(&contract, binding, stem, weights);

    match format {
        "json" => println!("{}", serde_json::to_string_pretty(&score)?),
        "markdown" => print!("{}", score_to_markdown(&score)),
        _ => {
            print!("{score}");
            print_probes(&score);
        }
    }

    if let Some(threshold) = min_score {
        if score.composite < threshold {
            return Err(format!(
                "Score {:.2} below threshold {threshold:.2}",
                score.composite,
            )
            .into());
        }
    }

    Ok(())
}

#[allow(
    clippy::too_many_arguments,
    clippy::too_many_lines,
    clippy::cast_precision_loss
)]
fn run_directory(
    dir: &Path,
    binding: Option<&BindingRegistry>,
    binding_path: Option<&Path>,
    format: &str,
    min_score: Option<f64>,
    summary: bool,
    top_gaps: usize,
    weights: &ScoringWeights,
    pvscore: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut yaml_paths: Vec<std::path::PathBuf> = Vec::new();
    collect_yaml_files(dir, &mut yaml_paths);
    yaml_paths.sort();

    let mut scores = Vec::new();
    for path in &yaml_paths {
        let Ok(contract) = parse_contract(path) else {
            continue;
        };
        let stem = path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown");
        scores.push(scoring::score_contract_weighted(
            &contract, binding, stem, weights,
        ));
    }

    let mean: f64 = if scores.is_empty() {
        0.0
    } else {
        scores.iter().map(|s| s.composite).sum::<f64>() / scores.len() as f64
    };

    if summary {
        print_summary_only(&scores, mean, format)?;
    } else {
        print_directory_scores(&scores, mean, format)?;
    }

    // Show top gaps by lowest score
    if top_gaps > 0 && !scores.is_empty() {
        print_top_gaps(&scores, top_gaps, format);
    }

    // Codebase scoring if binding is provided
    if let Some(binding) = binding {
        let mut parsed = Vec::new();
        for path in &yaml_paths {
            let Ok(contract) = parse_contract(path) else {
                continue;
            };
            let stem = path
                .file_name()
                .and_then(|s| s.to_str())
                .unwrap_or("unknown")
                .to_string();
            parsed.push((stem, contract));
        }
        let refs: Vec<_> = parsed.iter().map(|(s, c)| (s.clone(), c)).collect();

        // Build pagerank from contract index for impact-weighted gap analysis
        let pagerank = ContractIndex::from_directory(dir).ok().map(|idx| {
            idx.entries
                .iter()
                .filter_map(|e| idx.cached_pagerank(&e.stem).map(|s| (e.stem.clone(), s)))
                .collect::<std::collections::HashMap<String, f64>>()
        });

        // CD5: Detect stale contracts via git timestamps
        let drift_score = binding_path.map(|bp| {
            let bound_stems: HashSet<&str> = binding
                .bindings
                .iter()
                .map(|b| b.contract.as_str())
                .collect();
            let stale = drift::detect_stale_contracts(dir, bp, &bound_stems);
            drift::compute_drift(stale.len(), bound_stems.len())
        });

        let codebase = scoring::score_codebase_full(&refs, binding, pagerank.as_ref(), drift_score);

        match format {
            "json" => {
                let output = serde_json::json!({ "codebase": codebase });
                println!("{}", serde_json::to_string_pretty(&output)?);
            }
            "markdown" => {
                println!("\n## Codebase Score\n");
                println!("| Dimension | Value |");
                println!("|-----------|-------|");
                println!("| Coverage | {:.0}% |", codebase.contract_coverage * 100.0);
                println!(
                    "| Binding | {:.0}% |",
                    codebase.binding_completeness * 100.0
                );
                println!("| Mean Score | {:.2} |", codebase.mean_contract_score);
                println!("| Proof Depth | {:.2} |", codebase.proof_depth_dist);
                println!("| Drift | {:.2} |", codebase.drift);
                println!(
                    "\n**Composite:** {:.2} (Grade {})",
                    codebase.composite, codebase.grade
                );
            }
            _ => println!("\n{codebase}"),
        }

        if pvscore {
            print_pvscore(&codebase);
        }
    }

    if let Some(threshold) = min_score {
        if mean < threshold {
            return Err(format!("Mean score {mean:.2} below threshold {threshold:.2}",).into());
        }
    }

    Ok(())
}

#[allow(clippy::cast_precision_loss)]
fn print_directory_scores(
    scores: &[ContractScore],
    mean: f64,
    format: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    match format {
        "json" => {
            let output = serde_json::json!({
                "contracts": scores.len(),
                "mean_score": mean,
                "mean_grade": scoring::Grade::from_score(mean).to_string(),
                "scores": scores,
            });
            println!("{}", serde_json::to_string_pretty(&output)?);
        }
        "markdown" => {
            println!("## Contract Scores\n");
            println!("| Contract | Score | Grade | Spec | Falsify | Kani | Lean | Bind |");
            println!("|----------|-------|-------|------|---------|------|------|------|");
            for s in scores {
                println!(
                    "| {} | {:.2} | {} | {:.2} | {:.2} | {:.2} | {:.2} | {:.2} |",
                    s.stem,
                    s.composite,
                    s.grade,
                    s.spec_depth,
                    s.falsification_coverage,
                    s.kani_coverage,
                    s.lean_coverage,
                    s.binding_coverage
                );
            }
            println!(
                "\n**{} contracts** — Mean: {mean:.2} (Grade {})",
                scores.len(),
                scoring::Grade::from_score(mean)
            );
        }
        _ => {
            for s in scores {
                print!("{s}");
            }
            println!(
                "\n{} contracts — Mean: {mean:.2} (Grade {})",
                scores.len(),
                scoring::Grade::from_score(mean)
            );
        }
    }
    Ok(())
}

#[allow(clippy::cast_precision_loss)]
fn print_summary_only(
    scores: &[ContractScore],
    mean: f64,
    format: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let grade = scoring::Grade::from_score(mean);
    match format {
        "json" => {
            let output = serde_json::json!({
                "contracts": scores.len(),
                "mean_score": mean,
                "mean_grade": grade.to_string(),
            });
            println!("{}", serde_json::to_string_pretty(&output)?);
        }
        "markdown" => {
            println!(
                "**{} contracts** — Mean: {mean:.2} (Grade {grade})",
                scores.len()
            );
        }
        _ => {
            println!(
                "{} contracts — Mean: {mean:.2} (Grade {grade})",
                scores.len()
            );
        }
    }
    Ok(())
}

fn print_top_gaps(scores: &[ContractScore], n: usize, format: &str) {
    let mut sorted: Vec<_> = scores.iter().collect();
    sorted.sort_by(|a, b| {
        a.composite
            .partial_cmp(&b.composite)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    let top: Vec<_> = sorted.into_iter().take(n).collect();

    match format {
        "json" => {} // Already in JSON output
        "markdown" => {
            println!("\n### Top {} Gaps\n", top.len());
            for s in &top {
                println!("- **{}** — {:.2} ({})", s.stem, s.composite, s.grade);
            }
        }
        _ => {
            println!("\nTop {} gaps:", top.len());
            for s in &top {
                println!("  {}{:.2} ({})", s.stem, s.composite, s.grade);
            }
        }
    }
}

fn print_pvscore(codebase: &CodebaseScore) {
    let pv = pvscore_10dim(codebase);
    let grade = Grade::from_score(pv / 100.0);
    println!("\nPVScore: {pv:.1} (Grade: {grade})");
    println!(
        "  D1  Spec Depth:        {:.1}",
        codebase.contract_coverage * 100.0
    );
    println!(
        "  D2  Falsification:     {:.1}",
        codebase.binding_completeness * 100.0
    );
    println!(
        "  D3  Mean Score:        {:.1}",
        codebase.mean_contract_score * 100.0
    );
    println!(
        "  D4  Proof Depth:       {:.1}",
        codebase.proof_depth_dist * 100.0
    );
    println!("  D5  Drift:             {:.1}", codebase.drift * 100.0);
    println!(
        "  D6  Reverse Coverage:  {:.1}{}",
        codebase.reverse_coverage * 100.0,
        default_suffix(codebase.reverse_coverage)
    );
    println!(
        "  D7  Mutation Testing:  {:.1}{}",
        codebase.mutation_testing * 100.0,
        default_suffix(codebase.mutation_testing)
    );
    println!(
        "  D8  CI Pipeline:       {:.1}{}",
        codebase.ci_pipeline_depth * 100.0,
        default_suffix(codebase.ci_pipeline_depth)
    );
    println!(
        "  D9  Proof Freshness:   {:.1}{}",
        codebase.proof_freshness * 100.0,
        default_suffix(codebase.proof_freshness)
    );
    println!(
        "  D10 Defect Patterns:   {:.1}{}",
        codebase.defect_patterns * 100.0,
        default_suffix(codebase.defect_patterns)
    );
}

fn default_suffix(value: f64) -> &'static str {
    if value == 0.0 {
        " (default)"
    } else {
        ""
    }
}

fn score_to_markdown(score: &ContractScore) -> String {
    format!(
        "### {}\n\n- **Score:** {:.2} (Grade {})\n- Spec: {:.2} | Falsify: {:.2} | Kani: {:.2} | Lean: {:.2} | Bind: {:.2}\n",
        score.stem,
        score.composite,
        score.grade,
        score.spec_depth,
        score.falsification_coverage,
        score.kani_coverage,
        score.lean_coverage,
        score.binding_coverage
    )
}

/// Print probe-level score decomposition for a single contract.
///
/// Groups probes by dimension and prints each with a pass/fail indicator,
/// showing what contributed to each dimension's score.
fn print_probes(score: &ContractScore) {
    if score.probes.is_empty() {
        return;
    }

    // Dimension display order and labels
    let dimensions = [
        ("spec_depth", "D1 Spec Depth", score.spec_depth),
        (
            "falsification",
            "D2 Falsification",
            score.falsification_coverage,
        ),
        ("kani", "D3 Kani", score.kani_coverage),
        ("lean", "D4 Lean", score.lean_coverage),
        ("binding", "D5 Binding", score.binding_coverage),
    ];

    println!("  Probes:");
    for (dim_key, dim_label, dim_score) in &dimensions {
        let dim_probes: Vec<_> = score
            .probes
            .iter()
            .filter(|p| p.dimension == *dim_key)
            .collect();
        if dim_probes.is_empty() {
            continue;
        }
        println!("  {dim_label:20} {dim_score:.2}");
        for p in &dim_probes {
            let icon = if p.outcome { "+" } else { "-" };
            println!("    {icon} {}: {}", p.probe, p.detail);
        }
    }
}

/// Recursively collect all `.yaml` contract files from a directory.
///
/// Skips:
/// - `binding.yaml` (binding registries, not contracts)
/// - `kaizen/` directories (work items, not contracts)
/// - `legacy/` directories (deprecated contracts)
/// - `pipelines/` directories (pipeline contracts, different schema)
fn collect_yaml_files(dir: &Path, out: &mut Vec<std::path::PathBuf>) {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            let dirname = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
            // Skip non-contract directories
            if dirname == "kaizen" || dirname == "legacy" || dirname == "pipelines" {
                continue;
            }
            collect_yaml_files(&path, out);
        } else if path.extension().and_then(|e| e.to_str()) == Some("yaml")
            && path.file_name().and_then(|n| n.to_str()) != Some("binding.yaml")
        {
            out.push(path);
        }
    }
}