axiom-truth 0.8.1

Axiom — the truth layer: validation, simulation, guidance, and policy lens
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
// Copyright 2024-2026 Reflective Labs
// SPDX-License-Identifier: MIT

//! Governance workflow commands: validate, digest, ack, escalate, assign.
//!
//! These implement the uniquely "Converge" parts of the workflow:
//! - Validation produces findings in .converge/findings/
//! - Acknowledgements, escalations, and assignments create audit artifacts
//!
//! File conventions:
//! - .converge/findings/<id>.json       - Validation findings
//! - .converge/acks/<finding-id>.yaml   - Acknowledgement records
//! - .converge/escalations/<id>.yaml    - Escalation records
//! - .converge/assignments/<id>.yaml    - Assignment records
//! - .converge/policy/                  - Cedar policies and schema

use crate::cli::{AckArgs, AssignArgs, DigestArgs, EscalateArgs, OutputFormat, ValidateArgs};
use crate::commands::{CmdError, CmdResult, find_workspace_root};
use axiom_truth::{
    GherkinValidator, IssueCategory, Severity, StaticChatBackend, ValidationConfig, ValidationIssue,
};
use chrono::Utc;
use colored::Colorize;
use converge_provider::{ChatBackendSelectionConfig, DynChatBackend, SelectionCriteria};
use manifold::select_healthy_chat_backend;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;

// =============================================================================
// Data Structures
// =============================================================================

#[derive(Debug, Serialize, Deserialize)]
struct Finding {
    id: String,
    file: String,
    location: String,
    category: String,
    severity: String,
    message: String,
    suggestion: Option<String>,
    created_at: String,
    status: String,
}

impl Finding {
    fn from_issue(issue: &ValidationIssue, file: &str, id: &str) -> Self {
        Self {
            id: id.to_string(),
            file: file.to_string(),
            location: issue.location.clone(),
            category: format!("{:?}", issue.category),
            severity: format!("{:?}", issue.severity),
            message: issue.message.clone(),
            suggestion: issue.suggestion.clone(),
            created_at: Utc::now().to_rfc3339(),
            status: "open".to_string(),
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
struct AckRecord {
    finding_id: String,
    acknowledged_by: String,
    acknowledged_at: String,
    note: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
struct EscalationRecord {
    finding_id: String,
    escalated_by: String,
    escalated_to: Option<String>,
    escalated_at: String,
    note: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
struct AssignmentRecord {
    finding_id: String,
    assigned_by: String,
    assigned_to: String,
    assigned_at: String,
    note: Option<String>,
}

// =============================================================================
// Validate Command
// =============================================================================

/// Validate specs and produce findings.
pub async fn validate(args: ValidateArgs) -> CmdResult {
    let root =
        find_workspace_root().ok_or_else(|| CmdError::new("Could not find workspace root"))?;

    println!();
    println!("{}", "cz validate".bright_blue().bold());
    println!();

    // Ensure .converge directory structure exists
    ensure_converge_dirs(&root)?;

    // Find all .truths/.truth/.feature files
    let mut truth_files = Vec::new();
    for path in &args.paths {
        let full_path = if Path::new(path).is_absolute() {
            Path::new(path).to_path_buf()
        } else {
            root.join(path)
        };
        find_truth_files(&full_path, &mut truth_files);
    }

    if truth_files.is_empty() {
        println!(
            "  {} No .truths, .truth, or .feature files found",
            "".yellow()
        );
        return Ok(());
    }

    println!("  Found {} spec files", truth_files.len());
    println!();

    // Create the validator
    let (provider, config) = create_validator_config(&args).await?;
    let validator = GherkinValidator::new(provider, config);

    // Validate each file
    let mut total_errors = 0;
    let mut total_warnings = 0;
    let mut all_findings: Vec<Finding> = Vec::new();

    for file in &truth_files {
        let relative = file.strip_prefix(&root).unwrap_or(file);
        let file_str = relative.display().to_string();

        match validator.validate_file(file).await {
            Ok(result) => {
                let errors = result
                    .issues
                    .iter()
                    .filter(|i| i.severity == Severity::Error)
                    .count();
                let warnings = result
                    .issues
                    .iter()
                    .filter(|i| i.severity == Severity::Warning)
                    .count();

                total_errors += errors;
                total_warnings += warnings;

                // Print file result
                if result.is_valid {
                    println!(
                        "  {} {} ({} scenarios)",
                        "".green(),
                        file_str,
                        result.scenario_count
                    );
                } else {
                    println!(
                        "  {} {} ({} errors, {} warnings)",
                        "".red(),
                        file_str,
                        errors,
                        warnings
                    );
                }

                // Print issues
                for issue in &result.issues {
                    print_issue(issue);

                    // Create finding for persistence
                    if args.persist {
                        let finding_id = format!(
                            "{}-{}-{}",
                            sanitize_filename(&file_str),
                            sanitize_filename(&issue.location),
                            all_findings.len()
                        );
                        all_findings.push(Finding::from_issue(issue, &file_str, &finding_id));
                    }
                }
            }
            Err(e) => {
                println!("  {} {} - {}", "".red(), file_str, e);
                total_errors += 1;
            }
        }
    }

    // Persist findings
    if args.persist && !all_findings.is_empty() {
        let findings_dir = root.join(".converge").join("findings");
        for finding in &all_findings {
            let finding_file = findings_dir.join(format!("{}.json", finding.id));
            let json = serde_json::to_string_pretty(&finding)
                .map_err(|e| CmdError::new(format!("Failed to serialize finding: {e}")))?;
            fs::write(&finding_file, json)
                .map_err(|e| CmdError::new(format!("Failed to write finding: {e}")))?;
        }
        println!();
        println!(
            "  {} {} findings written to .converge/findings/",
            "".dimmed(),
            all_findings.len()
        );
    }

    // Summary
    println!();
    println!("───────────────────────────────────────────────────────────");
    if total_errors > 0 {
        println!(
            "  {} {} errors, {} warnings",
            "".red(),
            total_errors.to_string().red(),
            total_warnings
        );
    } else if total_warnings > 0 {
        println!(
            "  {} {} warnings",
            "".yellow(),
            total_warnings.to_string().yellow()
        );
    } else {
        println!("  {} All specs valid", "".green());
    }
    println!();

    if args.enforce {
        println!(
            "  {} Cedar policy enforcement requested but not yet implemented",
            "".yellow()
        );
        println!();
    }

    if total_errors > 0 {
        Err(CmdError::new(format!("{total_errors} validation error(s)")))
    } else {
        Ok(())
    }
}

async fn create_validator_config(
    args: &ValidateArgs,
) -> Result<(Arc<dyn DynChatBackend>, ValidationConfig), CmdError> {
    // Determine what checks to run
    let wants_llm_checks =
        !args.conventions_only && (!args.skip_business_sense || !args.skip_compilability);

    let config = ValidationConfig {
        check_business_sense: !args.conventions_only && !args.skip_business_sense,
        check_compilability: !args.conventions_only && !args.skip_compilability,
        check_conventions: true,
        min_confidence: 0.7,
    };

    // If LLM checks are requested, check for API key
    if wants_llm_checks {
        let has_any_llm_key = std::env::var_os("ANTHROPIC_API_KEY").is_some()
            || std::env::var_os("OPENAI_API_KEY").is_some()
            || std::env::var_os("GEMINI_API_KEY").is_some();

        if !has_any_llm_key {
            // No API key - fall back to conventions-only with a warning
            println!(
                "  {} No LLM API key configured, running conventions-only mode",
                "".yellow()
            );
            println!(
                "  {} Set ANTHROPIC_API_KEY, OPENAI_API_KEY, or GEMINI_API_KEY for business sense and compilability checks",
                "".dimmed()
            );
            println!();

            let config = ValidationConfig {
                check_business_sense: false,
                check_compilability: false,
                check_conventions: true,
                min_confidence: 0.7,
            };
            let backend: Arc<dyn DynChatBackend> = Arc::new(StaticChatBackend::constant("VALID"));
            return Ok((backend, config));
        }

        let mut selection_config = ChatBackendSelectionConfig::from_env()
            .map_err(|e| CmdError::new(format!("Invalid LLM selection config: {e}")))?;
        if std::env::var_os("CONVERGE_LLM_PROFILE").is_none() {
            selection_config.criteria = SelectionCriteria::analysis();
        }

        let selected = select_healthy_chat_backend(&selection_config)
            .await
            .map_err(|e| CmdError::new(format!("Failed to select LLM backend: {e}")))?;
        return Ok((selected.backend, config));
    }

    // Conventions-only mode
    let backend: Arc<dyn DynChatBackend> = Arc::new(StaticChatBackend::constant("VALID"));
    Ok((backend, config))
}

fn print_issue(issue: &ValidationIssue) {
    let (icon, color) = match issue.severity {
        Severity::Error => ("", "red"),
        Severity::Warning => ("", "yellow"),
        Severity::Info => ("", "blue"),
    };

    let icon_str = match color {
        "red" => format!("    {icon} ").red().to_string(),
        "yellow" => format!("    {icon} ").yellow().to_string(),
        "blue" => format!("    {icon} ").blue().to_string(),
        _ => format!("    {icon} "),
    };

    let category = match issue.category {
        IssueCategory::BusinessSense => "[business]",
        IssueCategory::Compilability => "[compile]",
        IssueCategory::Convention => "[convention]",
        IssueCategory::Syntax => "[syntax]",
        IssueCategory::NotRelatedError => "[error]",
    };

    println!(
        "{}{} {} {}",
        icon_str,
        category.dimmed(),
        issue.location.cyan(),
        issue.message
    );

    if let Some(suggestion) = &issue.suggestion {
        println!("      {} {}", "".dimmed(), suggestion.dimmed());
    }
}

fn sanitize_filename(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_alphanumeric() || c == '-' || c == '_' {
                c
            } else {
                '-'
            }
        })
        .collect::<String>()
        .to_lowercase()
}

// =============================================================================
// Digest Command
// =============================================================================

/// Produce "what needs attention" summary.
pub async fn digest(args: DigestArgs) -> CmdResult {
    let root =
        find_workspace_root().ok_or_else(|| CmdError::new("Could not find workspace root"))?;

    println!();
    println!("{}", "cz digest".bright_blue().bold());
    println!();

    let converge_dir = root.join(".converge");
    if !converge_dir.exists() {
        println!("  {} No .converge directory found", "".yellow());
        println!("  {} Run 'cz validate' first", "".dimmed());
        return Ok(());
    }

    // Load and count findings
    let findings_dir = converge_dir.join("findings");
    let acks_dir = converge_dir.join("acks");
    let escalations_dir = converge_dir.join("escalations");
    let assignments_dir = converge_dir.join("assignments");

    let findings = load_findings(&findings_dir);
    let acks = count_files(&acks_dir, "yaml");
    let escalations = count_files(&escalations_dir, "yaml");
    let assignments = count_files(&assignments_dir, "yaml");

    let open_findings: Vec<_> = findings
        .iter()
        .filter(|f| !acks_dir.join(format!("{}.yaml", f.id)).exists())
        .collect();

    let errors: Vec<_> = open_findings
        .iter()
        .filter(|f| f.severity == "Error")
        .collect();
    let warnings: Vec<_> = open_findings
        .iter()
        .filter(|f| f.severity == "Warning")
        .collect();

    match args.format {
        OutputFormat::Pretty => {
            println!("  Open findings:");
            println!(
                "    {} errors",
                if errors.is_empty() {
                    "0".green().to_string()
                } else {
                    errors.len().to_string().red().to_string()
                }
            );
            println!(
                "    {} warnings",
                if warnings.is_empty() {
                    "0".green().to_string()
                } else {
                    warnings.len().to_string().yellow().to_string()
                }
            );
            println!();
            println!("  Acknowledged: {}", acks.to_string().green());
            println!("  Escalated:    {}", escalations.to_string().yellow());
            println!("  Assigned:     {}", assignments.to_string().cyan());

            if !errors.is_empty() {
                println!();
                println!("  {} Open errors:", "Errors:".red().bold());
                for finding in errors.iter().take(5) {
                    println!(
                        "    {} {} - {}",
                        finding.id.cyan(),
                        finding.file.dimmed(),
                        finding.message
                    );
                }
                if errors.len() > 5 {
                    println!("    ... and {} more", errors.len() - 5);
                }
            }
        }
        OutputFormat::Json => {
            let summary = serde_json::json!({
                "open_errors": errors.len(),
                "open_warnings": warnings.len(),
                "acknowledged": acks,
                "escalated": escalations,
                "assigned": assignments,
                "findings": open_findings,
            });
            println!("{}", serde_json::to_string_pretty(&summary).unwrap());
        }
        OutputFormat::Quiet => {}
    }

    println!();
    Ok(())
}

fn load_findings(dir: &Path) -> Vec<Finding> {
    if !dir.exists() {
        return Vec::new();
    }

    let mut findings = Vec::new();
    if let Ok(entries) = fs::read_dir(dir) {
        for entry in entries.flatten() {
            if entry.path().extension().is_some_and(|e| e == "json")
                && let Ok(content) = fs::read_to_string(entry.path())
                && let Ok(finding) = serde_json::from_str::<Finding>(&content)
            {
                findings.push(finding);
            }
        }
    }
    findings
}

// =============================================================================
// Ack, Escalate, Assign Commands
// =============================================================================

/// Acknowledge a finding.
pub async fn ack(args: AckArgs) -> CmdResult {
    let root =
        find_workspace_root().ok_or_else(|| CmdError::new("Could not find workspace root"))?;

    ensure_converge_dirs(&root)?;

    // Verify finding exists
    let findings_dir = root.join(".converge").join("findings");
    let finding_file = findings_dir.join(format!("{}.json", args.finding_id));
    if !finding_file.exists() {
        return Err(CmdError::new(format!(
            "Finding not found: {}",
            args.finding_id
        )));
    }

    let record = AckRecord {
        finding_id: args.finding_id.clone(),
        acknowledged_by: get_current_user(),
        acknowledged_at: Utc::now().to_rfc3339(),
        note: args.note,
    };

    let acks_dir = root.join(".converge").join("acks");
    let ack_file = acks_dir.join(format!("{}.yaml", args.finding_id));

    let yaml = serde_yaml::to_string(&record)
        .map_err(|e| CmdError::new(format!("Failed to serialize: {e}")))?;

    fs::write(&ack_file, yaml)
        .map_err(|e| CmdError::new(format!("Failed to write ack file: {e}")))?;

    println!();
    println!(
        "{} Acknowledged finding: {}",
        "".green(),
        args.finding_id.cyan()
    );
    println!("  {} {}", "File:".dimmed(), ack_file.display());
    println!();

    Ok(())
}

/// Escalate a finding.
pub async fn escalate(args: EscalateArgs) -> CmdResult {
    let root =
        find_workspace_root().ok_or_else(|| CmdError::new("Could not find workspace root"))?;

    ensure_converge_dirs(&root)?;

    let record = EscalationRecord {
        finding_id: args.finding_id.clone(),
        escalated_by: get_current_user(),
        escalated_to: args.to,
        escalated_at: Utc::now().to_rfc3339(),
        note: args.note,
    };

    let escalations_dir = root.join(".converge").join("escalations");
    let escalation_file = escalations_dir.join(format!("{}.yaml", args.finding_id));

    let yaml = serde_yaml::to_string(&record)
        .map_err(|e| CmdError::new(format!("Failed to serialize: {e}")))?;

    fs::write(&escalation_file, yaml)
        .map_err(|e| CmdError::new(format!("Failed to write escalation file: {e}")))?;

    println!();
    println!(
        "{} Escalated finding: {}",
        "".yellow(),
        args.finding_id.cyan()
    );
    println!("  {} {}", "File:".dimmed(), escalation_file.display());
    println!();

    Ok(())
}

/// Assign a finding to an owner.
pub async fn assign(args: AssignArgs) -> CmdResult {
    let root =
        find_workspace_root().ok_or_else(|| CmdError::new("Could not find workspace root"))?;

    ensure_converge_dirs(&root)?;

    let record = AssignmentRecord {
        finding_id: args.finding_id.clone(),
        assigned_by: get_current_user(),
        assigned_to: args.owner.clone(),
        assigned_at: Utc::now().to_rfc3339(),
        note: args.note,
    };

    let assignments_dir = root.join(".converge").join("assignments");
    let assignment_file = assignments_dir.join(format!("{}.yaml", args.finding_id));

    let yaml = serde_yaml::to_string(&record)
        .map_err(|e| CmdError::new(format!("Failed to serialize: {e}")))?;

    fs::write(&assignment_file, yaml)
        .map_err(|e| CmdError::new(format!("Failed to write assignment file: {e}")))?;

    println!();
    println!(
        "{} Assigned finding {} to {}",
        "".cyan(),
        args.finding_id.cyan(),
        args.owner.green()
    );
    println!("  {} {}", "File:".dimmed(), assignment_file.display());
    println!();

    Ok(())
}

// =============================================================================
// Helpers
// =============================================================================

fn ensure_converge_dirs(root: &Path) -> CmdResult {
    let converge_dir = root.join(".converge");
    let dirs = [
        converge_dir.join("findings"),
        converge_dir.join("acks"),
        converge_dir.join("escalations"),
        converge_dir.join("assignments"),
        converge_dir.join("policy"),
    ];

    for dir in &dirs {
        fs::create_dir_all(dir)
            .map_err(|e| CmdError::new(format!("Failed to create {}: {e}", dir.display())))?;
    }

    Ok(())
}

fn find_truth_files(path: &Path, files: &mut Vec<PathBuf>) {
    if path.is_file() {
        if let Some(ext) = path.extension().and_then(|e| e.to_str())
            && (ext == "truths" || ext == "truth" || ext == "feature")
        {
            files.push(path.to_path_buf());
        }
    } else if path.is_dir()
        && let Ok(entries) = fs::read_dir(path)
    {
        for entry in entries.flatten() {
            let entry_path = entry.path();
            if let Some(name) = entry_path.file_name().and_then(|n| n.to_str())
                && (name.starts_with('.') || name == "node_modules" || name == "target")
            {
                continue;
            }
            find_truth_files(&entry_path, files);
        }
    }
}

fn count_files(dir: &Path, extension: &str) -> usize {
    if !dir.exists() {
        return 0;
    }

    fs::read_dir(dir)
        .map(|entries| {
            entries
                .flatten()
                .filter(|e| e.path().extension().is_some_and(|ext| ext == extension))
                .count()
        })
        .unwrap_or(0)
}

fn get_current_user() -> String {
    std::env::var("USER")
        .or_else(|_| std::env::var("USERNAME"))
        .unwrap_or_else(|_| "unknown".to_string())
}

#[cfg(test)]
mod tests {
    use converge_provider::{ChatBackendSelectionConfig, SelectionCriteria};

    #[test]
    fn selection_config_defaults_to_interactive_profile() {
        let config = ChatBackendSelectionConfig::default();
        assert_eq!(config.criteria, SelectionCriteria::interactive());
    }

    #[test]
    fn analysis_profile_is_available_for_governance() {
        let config =
            ChatBackendSelectionConfig::default().with_criteria(SelectionCriteria::analysis());
        assert_eq!(config.criteria, SelectionCriteria::analysis());
    }
}