assura 0.4.0

Contract-first AI-native language. Write what it should do. AI proves it does.
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
use super::*;

// `assura coverage` -- contract coverage report
// ---------------------------------------------------------------------------

pub(crate) fn run_coverage(
    path: &str,
    contracts_dir: &str,
    format: &str,
    min_coverage: Option<f64>,
    as_json: bool,
) {
    validate_human_json_format(format, "coverage", as_json);
    let root = Path::new(path);
    let src_dir = root.join("src");

    if !src_dir.exists() {
        // Contracts-only projects (e.g. `assura init`) have no src/ yet.
        // Coverage measures public Rust functions that have Assura contracts.
        let contracts_path = root.join(contracts_dir);
        let mut contract_names: std::collections::HashSet<String> =
            std::collections::HashSet::new();
        let mut contract_files: std::collections::HashMap<String, String> =
            std::collections::HashMap::new();
        if contracts_path.exists() {
            collect_contract_names_from_dir(
                &contracts_path,
                &mut contract_names,
                &mut contract_files,
            );
        }
        for extra_dir in &[".", "assura", "specs"] {
            let d = root.join(extra_dir);
            if d.exists() && d != contracts_path {
                collect_contract_names_from_dir(&d, &mut contract_names, &mut contract_files);
            }
        }
        let n = contract_names.len();
        if format == "json" {
            let report = serde_json::json!({
                "error": "no_src_directory",
                "path": root.display().to_string(),
                "message": "coverage needs a src/ tree of Rust sources; this project has contracts only",
                "contracts_found": n,
                "contract_names": contract_names.into_iter().collect::<Vec<_>>(),
                "hint": "add src/ (e.g. assura infer / rustc crate) or run coverage from a Rust project root",
            });
            println!("{}", serde_json::to_string_pretty(&report).unwrap());
        } else {
            eprintln!("Error: no src/ directory found at {}", root.display());
            if n > 0 {
                eprintln!(
                    "Found {n} Assura contract(s), but coverage reports which public Rust functions have contracts."
                );
                eprintln!(
                    "Hint: add a src/ tree (for example after `assura infer` or in a Rust crate), then re-run coverage."
                );
            } else {
                eprintln!(
                    "Hint: coverage expects a Rust project with src/ plus optional .assura contracts."
                );
            }
        }
        process::exit(2);
    }

    // Phase 1: Discover all public Rust functions
    let rs_files = discover_rs_files(&src_dir);
    let mut all_fns: Vec<(String, String)> = Vec::new(); // (file, fn_name)
    // Functions with inline `/// @requires` / `@ensures` (check-rust path).
    let mut inline_annotated: std::collections::HashSet<(String, String)> =
        std::collections::HashSet::new();
    // Codegen modules: file path -> Assura contract names from `/// Contract:`.
    let mut codegen_contracts_by_file: std::collections::HashMap<String, Vec<String>> =
        std::collections::HashMap::new();

    for rs_file in &rs_files {
        let rel_path = rs_file
            .strip_prefix(root)
            .unwrap_or(rs_file.as_path())
            .to_string_lossy()
            .to_string();

        let source = match fs::read_to_string(rs_file) {
            Ok(s) => s,
            Err(_) => continue,
        };

        let sigs = extract_rust_fn_signatures(&source);
        for sig in sigs {
            if sig.is_pub {
                all_fns.push((rel_path.clone(), sig.name));
            }
        }

        let codegen_names = codegen_contract_names(&source);
        if !codegen_names.is_empty() {
            codegen_contracts_by_file.insert(rel_path.clone(), codegen_names);
        }

        // Count check-rust style doc annotations as coverage.
        if let Ok(items) = assura_rust_analyzer::parse_rust_file(rs_file) {
            for name in public_fns_with_inline_contracts(&items) {
                inline_annotated.insert((rel_path.clone(), name));
            }
        }
    }

    // Phase 2: Discover all contract/bind names from .assura files
    let contracts_path = root.join(contracts_dir);
    let mut contract_names: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut contract_files: std::collections::HashMap<String, String> =
        std::collections::HashMap::new();

    // Scan contracts directory
    if contracts_path.exists() {
        collect_contract_names_from_dir(&contracts_path, &mut contract_names, &mut contract_files);
    }
    // Also scan for .assura files in the project root and common locations
    for extra_dir in &[".", "assura", "specs"] {
        let d = root.join(extra_dir);
        if d.exists() && d != contracts_path {
            collect_contract_names_from_dir(&d, &mut contract_names, &mut contract_files);
        }
    }

    if all_fns.is_empty() {
        eprintln!("No public functions found in {}", src_dir.display());
        process::exit(1);
    }

    // Phase 3: Cross-reference
    let mut covered: Vec<(String, String, String)> = Vec::new(); // (file, fn, contract_file)
    let mut uncovered: Vec<(String, String, usize)> = Vec::new(); // (file, fn, param_count)

    for (file, fn_name) in &all_fns {
        if contract_names.contains(fn_name.as_str()) {
            let cf = contract_files
                .get(fn_name.as_str())
                .cloned()
                .unwrap_or_else(|| "?".to_string());
            covered.push((file.clone(), fn_name.clone(), cf));
        } else if fn_name == "check" {
            // Generated Assura bodies are always `pub fn check` (#961):
            // - single-file: `/// Contract: Name` in the same source
            // - multi-file: `src/contract_<snake>.rs` for contract `Name`
            let mut matched: Option<String> = None;
            if let Some(names) = codegen_contracts_by_file.get(file) {
                for n in names {
                    if contract_names.contains(n.as_str()) {
                        matched = Some(
                            contract_files
                                .get(n.as_str())
                                .cloned()
                                .unwrap_or_else(|| format!("codegen ({n})")),
                        );
                        break;
                    }
                }
                if matched.is_none() && !names.is_empty() {
                    matched = Some(format!("codegen ({})", names.join(", ")));
                }
            }
            if matched.is_none() {
                matched = contract_from_codegen_filename(file, &contract_names, &contract_files);
            }
            if let Some(cf) = matched {
                covered.push((file.clone(), fn_name.clone(), cf));
            } else {
                uncovered.push((file.clone(), fn_name.clone(), 0));
            }
        } else if inline_annotated.contains(&(file.clone(), fn_name.clone())) {
            covered.push((
                file.clone(),
                fn_name.clone(),
                "inline (/// @requires/@ensures)".to_string(),
            ));
        } else {
            // Get param count for prioritization
            let param_count = rs_files
                .iter()
                .find(|f| {
                    f.strip_prefix(root)
                        .unwrap_or(f.as_path())
                        .to_string_lossy()
                        == *file
                })
                .and_then(|f| fs::read_to_string(f).ok())
                .map(|src| {
                    extract_rust_fn_signatures(&src)
                        .iter()
                        .find(|s| s.name == *fn_name)
                        .map(|s| s.params.len())
                        .unwrap_or(0)
                })
                .unwrap_or(0);
            uncovered.push((file.clone(), fn_name.clone(), param_count));
        }
    }

    // Sort uncovered by param count descending (more params = more complex = higher priority)
    uncovered.sort_by_key(|b| std::cmp::Reverse(b.2));

    let total = all_fns.len();
    let covered_count = covered.len();
    let pct = if total > 0 {
        (covered_count as f64 / total as f64) * 100.0
    } else {
        0.0
    };

    let is_json = format == "json";

    let below_min = coverage_below_minimum(pct, min_coverage);
    if is_json {
        let mut report = serde_json::json!({
            "ok": !below_min,
            "total_functions": total,
            "covered": covered_count,
            "uncovered": uncovered.len(),
            "coverage_percent": (pct * 10.0).round() / 10.0,
            "covered_functions": covered.iter().map(|(f, n, cf)| serde_json::json!({
                "file": f, "function": n, "contract_file": cf
            })).collect::<Vec<_>>(),
            "uncovered_functions": uncovered.iter().map(|(f, n, pc)| serde_json::json!({
                "file": f, "function": n, "param_count": pc
            })).collect::<Vec<_>>(),
        });
        if let Some(min) = min_coverage {
            report["min_coverage"] = serde_json::json!(min);
            report["below_minimum"] = serde_json::json!(below_min);
            if below_min {
                report["message"] =
                    serde_json::json!(format!("Coverage {:.1}% is below minimum {:.1}%", pct, min));
            }
        }
        println!("{}", serde_json::to_string_pretty(&report).unwrap());
    } else {
        println!("Contract Coverage: {}/", src_dir.display());
        println!("  Total public functions:  {}", total);
        println!("  With contracts:          {} ({:.1}%)", covered_count, pct);
        println!("  Without contracts:       {}", uncovered.len());

        if !covered.is_empty() {
            println!();
            println!("  Covered:");
            for (file, name, cf) in &covered {
                println!("    {file}::{name}  <-  {cf}");
            }
        }

        if !uncovered.is_empty() {
            println!();
            println!("  Uncovered (by complexity):");
            for (file, name, pc) in uncovered.iter().take(20) {
                println!("    {file}::{name}  ({pc} params)");
            }
            if uncovered.len() > 20 {
                println!("    ... and {} more", uncovered.len() - 20);
            }
        }
    }

    // Exit 1 if below min coverage (JSON already included ok/below_minimum).
    if below_min {
        if !is_json {
            let min = min_coverage.unwrap_or(0.0);
            eprintln!();
            eprintln!("Coverage {pct:.1}% is below minimum {min:.1}%");
        }
        process::exit(1);
    }
}

/// Whether coverage percent fails an optional minimum threshold.
pub(crate) fn coverage_below_minimum(pct: f64, min_coverage: Option<f64>) -> bool {
    min_coverage.is_some_and(|min| pct < min)
}

/// Assura contract names embedded by codegen (`/// Contract: SafeDivision`).
fn codegen_contract_names(source: &str) -> Vec<String> {
    let mut names = Vec::new();
    for line in source.lines() {
        let t = line.trim();
        if let Some(rest) = t.strip_prefix("/// Contract:") {
            let name = rest.trim();
            if !name.is_empty() {
                names.push(name.to_string());
            }
        }
    }
    names
}

/// Multi-file codegen writes `contract_{name.to_lowercase()}.rs` for a contract.
fn contract_from_codegen_filename(
    file: &str,
    contract_names: &std::collections::HashSet<String>,
    contract_files: &std::collections::HashMap<String, String>,
) -> Option<String> {
    let stem = std::path::Path::new(file)
        .file_stem()
        .and_then(|s| s.to_str())?;
    let lowered = stem.strip_prefix("contract_")?;
    for name in contract_names {
        if name.to_lowercase() == lowered {
            return Some(
                contract_files
                    .get(name.as_str())
                    .cloned()
                    .unwrap_or_else(|| format!("codegen ({name})")),
            );
        }
    }
    None
}

/// Names of public functions that carry inline `/// @requires` / `@ensures`
/// (or other) contract annotations, for coverage cross-reference.
pub(crate) fn public_fns_with_inline_contracts(
    items: &[assura_rust_analyzer::AnnotatedItem],
) -> Vec<String> {
    let mut names = Vec::new();
    for item in items {
        if item.contract.clause_count() == 0 {
            continue;
        }
        if let assura_rust_analyzer::AnnotatedItemKind::Function {
            name, is_public, ..
        } = &item.kind
            && *is_public
        {
            names.push(name.clone());
        }
    }
    names
}

/// Collect contract/bind names from all .assura files in a directory.
pub(crate) fn collect_contract_names_from_dir(
    dir: &Path,
    names: &mut std::collections::HashSet<String>,
    files: &mut std::collections::HashMap<String, String>,
) {
    let assura_files = discover_assura_files(dir);
    for af in &assura_files {
        let rel = af.to_string_lossy().to_string();
        let source = match fs::read_to_string(af) {
            Ok(s) => s,
            Err(_) => continue,
        };
        let (parsed, _) = assura_parser::parse(&source);
        if let Some(file) = parsed {
            use assura_parser::ast::{
                BindDecl, ContractDecl, DeclVisitor, FnDef, ServiceDecl, walk_decls,
            };
            struct CoverageNames<'a> {
                names: &'a mut std::collections::HashSet<String>,
                files: &'a mut std::collections::HashMap<String, String>,
                rel: &'a str,
            }
            impl DeclVisitor for CoverageNames<'_> {
                fn visit_contract(&mut self, c: &ContractDecl) {
                    self.names.insert(c.name.clone());
                    self.files.insert(c.name.clone(), self.rel.to_string());
                }
                fn visit_bind(&mut self, b: &BindDecl) {
                    self.names.insert(b.name.clone());
                    self.files.insert(b.name.clone(), self.rel.to_string());
                }
                fn visit_fn_def(&mut self, f: &FnDef) {
                    self.names.insert(f.name.clone());
                    self.files.insert(f.name.clone(), self.rel.to_string());
                }
                fn visit_service(&mut self, s: &ServiceDecl) {
                    self.names.insert(s.name.clone());
                    self.files.insert(s.name.clone(), self.rel.to_string());
                }
            }
            let mut visitor = CoverageNames {
                names,
                files,
                rel: &rel,
            };
            walk_decls(&mut visitor, &file.decls);
        }
    }
}

/// Recursively discover all .assura files under a directory.
pub(crate) fn discover_assura_files(dir: &Path) -> Vec<std::path::PathBuf> {
    let mut found = Vec::new();
    if let Ok(entries) = fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                found.extend(discover_assura_files(&path));
            } else if path.extension().is_some_and(|ext| ext == "assura") {
                found.push(path);
            }
        }
    }
    found.sort();
    found
}

// ---------------------------------------------------------------------------

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

    #[test]
    fn discover_assura_files_finds_only_assura() {
        let tmp = TempDir::new().unwrap();
        fs::write(tmp.path().join("foo.assura"), "contract Foo {}").unwrap();
        fs::write(tmp.path().join("bar.rs"), "fn bar() {}").unwrap();
        fs::write(tmp.path().join("baz.txt"), "hello").unwrap();

        let found = discover_assura_files(tmp.path());
        assert_eq!(found.len(), 1);
        assert!(found[0].file_name().unwrap() == "foo.assura");
    }

    #[test]
    fn discover_assura_files_recursive_and_sorted() {
        let tmp = TempDir::new().unwrap();
        let sub = tmp.path().join("sub");
        fs::create_dir(&sub).unwrap();
        fs::write(tmp.path().join("b.assura"), "").unwrap();
        fs::write(sub.join("a.assura"), "").unwrap();

        let found = discover_assura_files(tmp.path());
        assert_eq!(found.len(), 2);
        // Results should be sorted
        let names: Vec<_> = found
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();
        assert!(names.contains(&"a.assura".to_string()));
        assert!(names.contains(&"b.assura".to_string()));
    }

    #[test]
    fn discover_assura_files_empty_dir() {
        let tmp = TempDir::new().unwrap();
        let found = discover_assura_files(tmp.path());
        assert!(found.is_empty());
    }

    #[test]
    fn collect_contract_names_extracts_names() {
        let tmp = TempDir::new().unwrap();
        let source = r#"
contract SafeDiv {
    input(a: Int, b: Int)
    output(result: Int)
    requires { b != 0 }
}
"#;
        fs::write(tmp.path().join("div.assura"), source).unwrap();

        let mut names = std::collections::HashSet::new();
        let mut files = std::collections::HashMap::new();
        collect_contract_names_from_dir(tmp.path(), &mut names, &mut files);

        assert!(names.contains("SafeDiv"), "should find SafeDiv contract");
    }

    #[test]
    fn coverage_below_minimum_threshold() {
        assert!(!coverage_below_minimum(80.0, None));
        assert!(!coverage_below_minimum(80.0, Some(80.0)));
        assert!(!coverage_below_minimum(90.0, Some(80.0)));
        assert!(coverage_below_minimum(79.9, Some(80.0)));
        assert!(coverage_below_minimum(0.0, Some(1.0)));
    }

    #[test]
    fn public_fns_with_inline_contracts_detects_doc_annotations() {
        let source = r#"
/// @requires x > 0
/// @ensures result >= x
pub fn annotated(x: i32) -> i32 {
    x
}

/// no contract tags
pub fn plain(x: i32) -> i32 {
    x
}

/// @requires true
fn private_annotated(x: i32) -> i32 {
    x
}
"#;
        let items = assura_rust_analyzer::parse_rust_source(source).unwrap();
        let names = public_fns_with_inline_contracts(&items);
        assert!(
            names.contains(&"annotated".to_string()),
            "public fn with @requires/@ensures should count as covered: {names:?}"
        );
        assert!(
            !names.contains(&"plain".to_string()),
            "public fn without contract tags must not count: {names:?}"
        );
        assert!(
            !names.contains(&"private_annotated".to_string()),
            "private annotated fn is outside coverage universe: {names:?}"
        );
    }
}