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
//! Project-mode check: multi-file resolve + type-check.

use super::super::*;

// ---------------------------------------------------------------------------
// Project-mode check: resolve, type-check, and SMT-verify all .assura files
// ---------------------------------------------------------------------------

/// Map a module key (`a`, `pkg.sub`) to an on-disk `.assura` path under the
/// project root (filesystem-derived modules use dotted path segments).
fn module_key_to_path(project_root: &Path, module_path: &str) -> std::path::PathBuf {
    let mut path = project_root.to_path_buf();
    for segment in module_path.split('.') {
        path.push(segment);
    }
    path.set_extension("assura");
    path
}

pub(crate) fn run_check_project(
    project_dir: &Path,
    output_mode: OutputMode,
    verbosity: Verbosity,
    config: &CompilerConfig,
    showcase_only: bool,
    strict: bool,
) {
    let (project_root, dep_map, dep_warnings) = load_project_deps(project_dir);

    if output_mode == OutputMode::Human {
        eprintln!("Checking project at {}", project_root.display());
        if showcase_only {
            eprintln!("  (showcase-only: files with SHOWCASE header)");
        }
    }
    for w in &dep_warnings {
        if output_mode == OutputMode::Human {
            eprintln!("Warning: {w}");
        }
    }

    let (resolved_files, warnings) =
        match assura_resolve::discover_and_resolve_project_with_deps(&project_root, &dep_map) {
            Ok(pair) => pair,
            Err(errors) => {
                if output_mode == OutputMode::Json {
                    let diags: Vec<_> = errors
                        .iter()
                        .map(|e| {
                            let msg = e.to_string();
                            // Map to catalog codes (A02000 is not a valid code).
                            let code = if msg.to_ascii_lowercase().contains("circular") {
                                "A02005"
                            } else {
                                "A02010"
                            };
                            assura_diagnostics::Diagnostic::error(code, msg, 0..0)
                                .with_file(project_root.display().to_string())
                        })
                        .collect();
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&diags).unwrap_or_default()
                    );
                } else {
                    for e in &errors {
                        eprintln!("Error: {e}");
                    }
                }
                process::exit(1);
            }
        };

    let mut total_errors = 0usize;
    let mut total_modules = 0usize;
    let mut total_bindings = 0usize;
    let mut module_results: Vec<serde_json::Value> = Vec::new();
    let mut all_diags: Vec<assura_diagnostics::Diagnostic> = Vec::new();

    // discover_and_resolve returns per-module resolution failures in `warnings`.
    // Count them as project errors so a missing import fails the check.
    for issue in &warnings {
        total_errors += 1;
        if output_mode == OutputMode::Human {
            eprintln!("Error: {issue}");
        } else {
            let code = if issue.to_ascii_lowercase().contains("circular") {
                "A02005"
            } else {
                "A02010"
            };
            all_diags.push(
                assura_diagnostics::Diagnostic::error(code, issue.clone(), 0..0)
                    .with_file(project_root.display().to_string()),
            );
        }
    }

    // Module map keys are declared `module a.b` names (or filesystem-derived
    // dotted paths), not on-disk paths. Scan source files once for SHOWCASE.
    let showcase_modules = if showcase_only {
        collect_showcase_module_names(&project_root)
    } else {
        std::collections::HashSet::new()
    };

    // Type-check each resolved file with cross-module type information
    let modules_map = resolved_files.clone();
    for (module_path, resolved) in resolved_files {
        if showcase_only && !showcase_modules.contains(&module_path) {
            continue;
        }
        total_modules += 1;
        match assura_types::TypeChecker::new()
            .config(config.type_check.clone())
            .modules(modules_map.clone())
            .check(resolved)
        {
            Ok(typed) => {
                let bindings = typed.type_env.len();
                total_bindings += bindings;
                let symbols = typed.resolved.symbols.symbols.len();

                // SMT verify (same as single-file check). Without this,
                // `assura check <dir>` only resolved/type-checked and
                // silently accepted counterexamples (dogfood R85).
                let file_path = module_key_to_path(&project_root, &module_path);
                let filename = file_path.display().to_string();
                let mut module_verify_errors = 0usize;
                let mut verify_summaries: Vec<serde_json::Value> = Vec::new();
                if config.verify.layer >= 1 {
                    let results = assura_pipeline::verify_typed(&typed, &filename, config);
                    for r in &results {
                        match r {
                            assura_smt::VerificationResult::Verified { .. } => {
                                if output_mode == OutputMode::Human && verbosity != Verbosity::Quiet
                                {
                                    eprintln!("    {} ... verified", r.clause_desc());
                                }
                            }
                            assura_smt::VerificationResult::Counterexample {
                                clause_desc,
                                model,
                                ..
                            } => {
                                module_verify_errors += 1;
                                total_errors += 1;
                                if output_mode == OutputMode::Human {
                                    eprintln!(
                                        "    {clause_desc} ... COUNTEREXAMPLE\n      | {model}"
                                    );
                                }
                                all_diags.push(
                                    assura_diagnostics::Diagnostic::error(
                                        "A05100",
                                        format!(
                                            "verification failed for {clause_desc}: counterexample: {model}"
                                        ),
                                        0..0,
                                    )
                                    .with_file(filename.clone()),
                                );
                            }
                            assura_smt::VerificationResult::Timeout { clause_desc } => {
                                module_verify_errors += 1;
                                total_errors += 1;
                                if output_mode == OutputMode::Human {
                                    eprintln!("    {clause_desc} ... timeout");
                                }
                                all_diags.push(
                                    assura_diagnostics::Diagnostic::error(
                                        "A05101",
                                        format!("verification timeout for {clause_desc}"),
                                        0..0,
                                    )
                                    .with_file(filename.clone()),
                                );
                            }
                            assura_smt::VerificationResult::Unknown {
                                clause_desc,
                                reason,
                            } => {
                                let known = assura_smt::is_known_smt_limitation(reason);
                                if strict || !known {
                                    module_verify_errors += 1;
                                    total_errors += 1;
                                }
                                if output_mode == OutputMode::Human {
                                    let tag = if known { "skipped" } else { "unknown" };
                                    eprintln!("    {clause_desc} ... {tag} ({reason})");
                                }
                                if strict || !known {
                                    all_diags.push(
                                        assura_diagnostics::Diagnostic::error(
                                            "A05102",
                                            format!(
                                                "verification unknown for {clause_desc}: {reason}"
                                            ),
                                            0..0,
                                        )
                                        .with_file(filename.clone()),
                                    );
                                }
                            }
                        }
                        verify_summaries.push(r.to_json_value());
                    }
                }

                let status = if module_verify_errors > 0 {
                    "error"
                } else {
                    "ok"
                };
                if output_mode == OutputMode::Human {
                    if module_verify_errors > 0 {
                        eprintln!(
                            "ERR {module_path}: {symbols} symbol(s), {bindings} binding(s), {module_verify_errors} verify error(s)"
                        );
                    } else {
                        eprintln!("OK  {module_path}: {symbols} symbol(s), {bindings} binding(s)");
                    }
                } else {
                    module_results.push(serde_json::json!({
                        "module": module_path,
                        "status": status,
                        "symbols": symbols,
                        "bindings": bindings,
                        "errors": module_verify_errors,
                        "verification": verify_summaries,
                    }));
                }
            }
            Err((errors, _returned_resolved)) => {
                total_errors += errors.len();
                if output_mode == OutputMode::Human {
                    eprintln!("ERR {module_path}: {} error(s)", errors.len());
                    for err in &errors {
                        eprintln!("  {}: {}", err.code, err.message);
                    }
                } else {
                    module_results.push(serde_json::json!({
                        "module": module_path,
                        "status": "error",
                        "errors": errors.len(),
                        "messages": errors.iter().map(|e| {
                            serde_json::json!({
                                "code": e.code.as_str(),
                                "message": e.message,
                            })
                        }).collect::<Vec<_>>(),
                    }));
                    for err in &errors {
                        all_diags.push(
                            assura_diagnostics::Diagnostic::error(
                                err.code.as_str(),
                                err.message.clone(),
                                err.span.clone(),
                            )
                            .with_file(module_path.clone()),
                        );
                    }
                }
            }
        }
    }

    let vacuous_showcase = showcase_only && total_modules == 0 && total_errors == 0;

    if output_mode == OutputMode::Json {
        let mut report = serde_json::json!({
            "project": project_root.display().to_string(),
            "modules": total_modules,
            "bindings": total_bindings,
            "errors": total_errors,
            "success": total_errors == 0,
            "results": module_results,
            "diagnostics": all_diags,
        });
        if showcase_only {
            report["showcase_only"] = serde_json::json!(true);
        }
        if vacuous_showcase {
            report["vacuous"] = serde_json::json!(true);
            report["vacuous_reason"] = serde_json::json!(
                "no SHOWCASE-marked modules matched; check demos with // SHOWCASE headers"
            );
        }
        println!(
            "{}",
            serde_json::to_string_pretty(&report).unwrap_or_default()
        );
    } else {
        eprintln!();
        eprintln!(
            "Project: {total_modules} module(s), {total_bindings} binding(s), {total_errors} error(s)"
        );
        if vacuous_showcase {
            eprintln!("Warning: --showcase-only matched 0 files (no // SHOWCASE headers found).");
        }
    }

    if total_errors > 0 {
        process::exit(1);
    }
}

/// Walk `.assura` files under `project_root` and return module keys whose
/// first few lines contain `SHOWCASE` (must-pass demos).
///
/// Keys match resolve's discovery: declared `module a.b` path, else the
/// filesystem-derived dotted path.
fn collect_showcase_module_names(project_root: &Path) -> std::collections::HashSet<String> {
    let mut names = std::collections::HashSet::new();
    let mut files = Vec::new();
    collect_assura_files_under(project_root, &mut files);
    for file_path in files {
        let Ok(src) = std::fs::read_to_string(&file_path) else {
            continue;
        };
        let head: String = src.lines().take(8).collect::<Vec<_>>().join("\n");
        if !head.contains("SHOWCASE") {
            continue;
        }
        let fs_path = file_path
            .strip_prefix(project_root)
            .unwrap_or(&file_path)
            .with_extension("")
            .to_string_lossy()
            .replace(['/', '\\'], ".");
        let key = match assura_parser::parse(&src) {
            (Some(ast), _) => ast
                .module
                .as_ref()
                .map(|m| m.path.join("."))
                .unwrap_or(fs_path),
            (None, _) => fs_path,
        };
        names.insert(key);
    }
    names
}

fn collect_assura_files_under(dir: &Path, files: &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() {
            // Skip generated/target noise if present under a project tree.
            if let Some(name) = path.file_name().and_then(|n| n.to_str())
                && (name == "target" || name == "generated" || name == ".git")
            {
                continue;
            }
            collect_assura_files_under(&path, files);
        } else if path.extension().and_then(|e| e.to_str()) == Some("assura") {
            files.push(path);
        }
    }
}

#[cfg(test)]
mod showcase_path_tests {
    use super::collect_showcase_module_names;
    use std::fs;

    #[test]
    fn collect_showcase_uses_declared_module_name() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(
            dir.path().join("hb.assura"),
            "// SHOWCASE\nmodule tls.heartbeat;\ncontract C { requires { true } ensures { true } }\n",
        )
        .unwrap();
        fs::write(
            dir.path().join("other.assura"),
            "module other;\ncontract D { requires { true } ensures { true } }\n",
        )
        .unwrap();
        let names = collect_showcase_module_names(dir.path());
        assert!(
            names.contains("tls.heartbeat"),
            "expected declared module name, got {names:?}"
        );
        assert!(!names.contains("other"));
    }

    #[test]
    fn collect_showcase_falls_back_to_file_path() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(
            dir.path().join("echo.assura"),
            "// SHOWCASE\ncontract E { requires { true } ensures { true } }\n",
        )
        .unwrap();
        let names = collect_showcase_module_names(dir.path());
        assert!(
            names.contains("echo"),
            "expected filesystem module key, got {names:?}"
        );
    }
}