axiom-truth 0.4.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
// Copyright 2024-2026 Reflective Labs
// SPDX-License-Identifier: MIT

//! Pre-flight simulation for Converge Truths.
//!
//! Analyzes a Truth spec **before** execution to determine whether it has
//! a realistic chance of converging. Catches underspecification, missing
//! resources, and governance gaps early — no agents need to run.
//!
//! # Example
//!
//! ```ignore
//! use axiom_truth::simulation::{simulate, SimulationConfig};
//! use axiom_truth::truths::parse_truth_document;
//!
//! let doc = parse_truth_document(spec)?;
//! let report = simulate(&doc, &SimulationConfig::default());
//! if !report.can_converge() {
//!     for finding in &report.findings {
//!         eprintln!("{}: {}", finding.severity, finding.message);
//!     }
//! }
//! ```

use crate::gherkin::{ValidationError, extract_all_metas, preprocess_truths};
use crate::truths::{TruthDocument, TruthGovernance};

/// Configuration for simulation strictness.
#[derive(Debug, Clone)]
pub struct SimulationConfig {
    /// Require Intent block with at least Outcome.
    pub require_intent: bool,
    /// Require Authority block with at least Actor.
    pub require_authority: bool,
    /// Require Evidence block with at least one Requires field.
    pub require_evidence: bool,
    /// Require at least one scenario with a Then step.
    pub require_assertions: bool,
    /// Require scenario Given steps to reference resources declared in Evidence.
    pub check_resource_availability: bool,
}

impl Default for SimulationConfig {
    fn default() -> Self {
        Self {
            require_intent: true,
            require_authority: true,
            require_evidence: true,
            require_assertions: true,
            check_resource_availability: true,
        }
    }
}

/// Overall simulation verdict.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Verdict {
    /// The spec looks complete enough to converge.
    Ready,
    /// The spec has warnings but might converge.
    Risky,
    /// The spec is underspecified and will not converge.
    WillNotConverge,
}

impl std::fmt::Display for Verdict {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Ready => write!(f, "ready"),
            Self::Risky => write!(f, "risky"),
            Self::WillNotConverge => write!(f, "will-not-converge"),
        }
    }
}

/// Severity of a simulation finding.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FindingSeverity {
    Info,
    Warning,
    Error,
}

impl std::fmt::Display for FindingSeverity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Info => write!(f, "info"),
            Self::Warning => write!(f, "warning"),
            Self::Error => write!(f, "error"),
        }
    }
}

/// A single simulation finding.
#[derive(Debug, Clone)]
pub struct SimulationFinding {
    pub severity: FindingSeverity,
    pub category: &'static str,
    pub message: String,
    pub suggestion: Option<String>,
}

/// The result of simulating a Truth spec.
#[derive(Debug, Clone)]
pub struct SimulationReport {
    pub verdict: Verdict,
    pub findings: Vec<SimulationFinding>,
    pub governance_coverage: GovernanceCoverage,
    pub scenario_count: usize,
    pub resource_summary: ResourceSummary,
}

impl SimulationReport {
    /// Whether the spec has a realistic chance of converging.
    pub fn can_converge(&self) -> bool {
        self.verdict != Verdict::WillNotConverge
    }
}

/// Which governance blocks are present and how complete they are.
#[derive(Debug, Clone, Default)]
pub struct GovernanceCoverage {
    pub has_intent: bool,
    pub has_outcome: bool,
    pub has_authority: bool,
    pub has_actor: bool,
    pub has_approval_gate: bool,
    pub has_constraint: bool,
    pub has_evidence: bool,
    pub evidence_count: usize,
    pub has_exception: bool,
    pub has_escalation_path: bool,
}

/// Summary of resources required vs available.
#[derive(Debug, Clone, Default)]
pub struct ResourceSummary {
    /// Resources declared in Evidence.Requires.
    pub declared_evidence: Vec<String>,
    /// Resources referenced in scenario steps.
    pub referenced_in_scenarios: Vec<String>,
    /// Resources referenced but not declared.
    pub missing: Vec<String>,
}

/// Run a pre-flight simulation on a parsed Truth document.
pub fn simulate(doc: &TruthDocument, config: &SimulationConfig) -> SimulationReport {
    let mut findings = Vec::new();

    let governance_coverage = check_governance(&doc.governance, config, &mut findings);
    let scenario_count = check_scenarios(&doc.gherkin, config, &mut findings);
    let resource_summary = check_resources(&doc.governance, &doc.gherkin, config, &mut findings);

    let has_errors = findings
        .iter()
        .any(|f| matches!(f.severity, FindingSeverity::Error));
    let has_warnings = findings
        .iter()
        .any(|f| matches!(f.severity, FindingSeverity::Warning));

    let verdict = if has_errors {
        Verdict::WillNotConverge
    } else if has_warnings {
        Verdict::Risky
    } else {
        Verdict::Ready
    };

    SimulationReport {
        verdict,
        findings,
        governance_coverage,
        scenario_count,
        resource_summary,
    }
}

/// Parse and simulate in one step.
pub fn simulate_spec(
    content: &str,
    config: &SimulationConfig,
) -> Result<SimulationReport, ValidationError> {
    let doc = crate::truths::parse_truth_document(content)?;
    Ok(simulate(&doc, config))
}

fn check_governance(
    gov: &TruthGovernance,
    config: &SimulationConfig,
    findings: &mut Vec<SimulationFinding>,
) -> GovernanceCoverage {
    let mut coverage = GovernanceCoverage::default();

    // Intent
    if let Some(intent) = &gov.intent {
        coverage.has_intent = true;
        coverage.has_outcome = intent.outcome.is_some();
        if intent.outcome.is_none() {
            findings.push(SimulationFinding {
                severity: FindingSeverity::Warning,
                category: "governance",
                message: "Intent block present but missing Outcome field.".into(),
                suggestion: Some("Add `Outcome: <what should happen>` to the Intent block.".into()),
            });
        }
    } else if config.require_intent {
        findings.push(SimulationFinding {
            severity: FindingSeverity::Error,
            category: "governance",
            message: "Missing Intent block — agents have no goal to converge toward.".into(),
            suggestion: Some("Add an Intent block with Outcome and optionally Goal.".into()),
        });
    }

    // Authority
    if let Some(authority) = &gov.authority {
        coverage.has_authority = true;
        coverage.has_actor = authority.actor.is_some();
        coverage.has_approval_gate = !authority.requires_approval.is_empty();
        if authority.actor.is_none() {
            findings.push(SimulationFinding {
                severity: FindingSeverity::Warning,
                category: "governance",
                message: "Authority block present but missing Actor field.".into(),
                suggestion: Some("Add `Actor: <who can approve>` to the Authority block.".into()),
            });
        }
    } else if config.require_authority {
        findings.push(SimulationFinding {
            severity: FindingSeverity::Error,
            category: "governance",
            message: "Missing Authority block — no one is authorized to promote decisions.".into(),
            suggestion: Some(
                "Add an Authority block with Actor and optionally Requires Approval.".into(),
            ),
        });
    }

    // Constraint
    if let Some(constraint) = &gov.constraint {
        coverage.has_constraint = true;
        if constraint.budget.is_empty()
            && constraint.cost_limit.is_empty()
            && constraint.must_not.is_empty()
        {
            findings.push(SimulationFinding {
                severity: FindingSeverity::Info,
                category: "governance",
                message: "Constraint block is empty — agents have no guardrails.".into(),
                suggestion: None,
            });
        }
    }

    // Evidence
    if let Some(evidence) = &gov.evidence {
        coverage.has_evidence = true;
        coverage.evidence_count = evidence.requires.len();
        if evidence.requires.is_empty() {
            findings.push(SimulationFinding {
                severity: FindingSeverity::Warning,
                category: "governance",
                message: "Evidence block present but no Requires fields — nothing to audit.".into(),
                suggestion: Some("Add `Requires: <evidence_name>` fields.".into()),
            });
        }
        if evidence.audit.is_empty() {
            findings.push(SimulationFinding {
                severity: FindingSeverity::Info,
                category: "governance",
                message: "No Audit field in Evidence — decision trail may be incomplete.".into(),
                suggestion: Some("Add `Audit: <log_name>` for traceability.".into()),
            });
        }
    } else if config.require_evidence {
        findings.push(SimulationFinding {
            severity: FindingSeverity::Error,
            category: "governance",
            message: "Missing Evidence block — no proof requirements declared.".into(),
            suggestion: Some("Add an Evidence block with Requires and Audit fields.".into()),
        });
    }

    // Exception
    if let Some(exception) = &gov.exception {
        coverage.has_exception = true;
        coverage.has_escalation_path = !exception.escalates_to.is_empty();
    }

    // Cross-block coherence
    if coverage.has_approval_gate && !coverage.has_evidence {
        findings.push(SimulationFinding {
            severity: FindingSeverity::Warning,
            category: "coherence",
            message:
                "Authority requires approval but no Evidence block — approver has nothing to review."
                    .into(),
            suggestion: Some(
                "Add Evidence.Requires fields so the approver has artifacts to evaluate.".into(),
            ),
        });
    }

    if coverage.has_constraint && !coverage.has_authority {
        findings.push(SimulationFinding {
            severity: FindingSeverity::Warning,
            category: "coherence",
            message: "Constraints declared but no Authority — who enforces the limits?".into(),
            suggestion: Some("Add an Authority block with an Actor.".into()),
        });
    }

    coverage
}

fn check_scenarios(
    gherkin: &str,
    config: &SimulationConfig,
    findings: &mut Vec<SimulationFinding>,
) -> usize {
    let preprocessed = preprocess_truths(gherkin);
    let metas = extract_all_metas(&preprocessed).unwrap_or_default();

    if metas.is_empty() {
        findings.push(SimulationFinding {
            severity: FindingSeverity::Error,
            category: "scenario",
            message: "No scenarios found — nothing to execute.".into(),
            suggestion: Some("Add at least one Scenario with Given/When/Then steps.".into()),
        });
        return 0;
    }

    // Check for Then steps (assertions)
    if config.require_assertions {
        let has_then = gherkin.lines().any(|line| line.trim().starts_with("Then "));
        if !has_then {
            findings.push(SimulationFinding {
                severity: FindingSeverity::Error,
                category: "scenario",
                message: "No Then steps found — scenarios have no success criteria.".into(),
                suggestion: Some("Add Then steps that assert expected outcomes.".into()),
            });
        }
    }

    // Check for Given steps (preconditions)
    let has_given = gherkin
        .lines()
        .any(|line| line.trim().starts_with("Given "));
    if !has_given {
        findings.push(SimulationFinding {
            severity: FindingSeverity::Warning,
            category: "scenario",
            message: "No Given steps — scenarios have no declared preconditions.".into(),
            suggestion: Some("Add Given steps that establish the initial state.".into()),
        });
    }

    // Check for When steps (actions)
    let has_when = gherkin.lines().any(|line| line.trim().starts_with("When "));
    if !has_when {
        findings.push(SimulationFinding {
            severity: FindingSeverity::Warning,
            category: "scenario",
            message: "No When steps — scenarios have no triggering action.".into(),
            suggestion: Some("Add When steps that describe the action being governed.".into()),
        });
    }

    metas.len()
}

fn check_resources(
    gov: &TruthGovernance,
    gherkin: &str,
    config: &SimulationConfig,
    findings: &mut Vec<SimulationFinding>,
) -> ResourceSummary {
    let mut summary = ResourceSummary::default();

    // Collect declared evidence resources
    if let Some(evidence) = &gov.evidence {
        summary.declared_evidence = evidence.requires.clone();
    }

    // Extract resource references from scenario steps
    let resource_pattern = regex::Regex::new(r#"[a-z][a-z0-9_]*(?:_[a-z0-9]+)+"#).ok();

    if let Some(pattern) = &resource_pattern {
        for line in gherkin.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("Given ")
                || trimmed.starts_with("When ")
                || trimmed.starts_with("Then ")
                || trimmed.starts_with("And ")
            {
                for m in pattern.find_iter(trimmed) {
                    let resource = m.as_str().to_string();
                    if !summary.referenced_in_scenarios.contains(&resource) {
                        summary.referenced_in_scenarios.push(resource);
                    }
                }
            }
        }
    }

    // Find references that match evidence naming patterns but aren't declared
    if config.check_resource_availability && !summary.declared_evidence.is_empty() {
        for referenced in &summary.referenced_in_scenarios {
            let looks_like_evidence = referenced.ends_with("_assessment")
                || referenced.ends_with("_analysis")
                || referenced.ends_with("_report")
                || referenced.ends_with("_review")
                || referenced.ends_with("_log")
                || referenced.ends_with("_record")
                || referenced.ends_with("_bundle");

            if looks_like_evidence && !summary.declared_evidence.contains(referenced) {
                summary.missing.push(referenced.clone());
            }
        }

        if !summary.missing.is_empty() {
            findings.push(SimulationFinding {
                severity: FindingSeverity::Warning,
                category: "resources",
                message: format!(
                    "Scenario references evidence-like resources not declared in Evidence block: {}",
                    summary.missing.join(", ")
                ),
                suggestion: Some(
                    "Add these as `Requires:` fields in the Evidence block, or rename to avoid evidence naming patterns.".into(),
                ),
            });
        }
    }

    // Check if authority actors are referenced in scenarios
    if let Some(authority) = &gov.authority {
        if let Some(actor) = &authority.actor {
            let actor_referenced = gherkin.contains(actor);
            if !actor_referenced {
                findings.push(SimulationFinding {
                    severity: FindingSeverity::Info,
                    category: "resources",
                    message: format!(
                        "Authority actor `{actor}` is declared but not referenced in any scenario."
                    ),
                    suggestion: Some(
                        "Consider adding a scenario step that involves the authorized actor."
                            .into(),
                    ),
                });
            }
        }
    }

    summary
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::truths::parse_truth_document;

    #[test]
    fn complete_spec_is_ready() {
        let content = r#"Truth: Vendor selection is governed

Intent:
  Outcome: Select a vendor with auditable rationale.
  Goal: Evaluate candidates on cost and compliance.

Authority:
  Actor: governance_review_board
  Requires Approval: final_vendor_selection

Constraint:
  Cost Limit: first-year spend must stay within budget.

Evidence:
  Requires: security_assessment
  Requires: pricing_analysis
  Audit: decision_log

Scenario: Vendors produce traceable outcomes
  Given candidate vendors "Acme AI, Beta ML"
  And each vendor has a security_assessment and pricing_analysis
  When the governance_review_board evaluates each vendor
  Then each vendor should produce a compliance screening result
"#;
        let doc = parse_truth_document(content).unwrap();
        let report = simulate(&doc, &SimulationConfig::default());
        assert_eq!(report.verdict, Verdict::Ready);
        assert!(report.can_converge());
    }

    #[test]
    fn missing_intent_will_not_converge() {
        let content = r#"Truth: No intent

Scenario: Something happens
  Given a precondition
  When an action occurs
  Then a result is observed
"#;
        let doc = parse_truth_document(content).unwrap();
        let report = simulate(&doc, &SimulationConfig::default());
        assert_eq!(report.verdict, Verdict::WillNotConverge);
        assert!(!report.can_converge());
        assert!(
            report
                .findings
                .iter()
                .any(|f| f.message.contains("Missing Intent"))
        );
    }

    #[test]
    fn missing_then_steps_will_not_converge() {
        let content = r#"Truth: No assertions

Intent:
  Outcome: Do something.

Authority:
  Actor: admin

Evidence:
  Requires: report

Scenario: Missing outcome
  Given a shortlist of vendors
  When the workflow ranks them
"#;
        let doc = parse_truth_document(content).unwrap();
        let report = simulate(&doc, &SimulationConfig::default());
        assert_eq!(report.verdict, Verdict::WillNotConverge);
        assert!(
            report
                .findings
                .iter()
                .any(|f| f.message.contains("No Then steps"))
        );
    }

    #[test]
    fn approval_without_evidence_is_risky() {
        let content = r#"Truth: Approval gate without evidence

Intent:
  Outcome: Approve a vendor.

Authority:
  Actor: board
  Requires Approval: cfo_sign_off

Scenario: Approval happens
  Given a vendor is shortlisted
  When the board reviews
  Then the vendor is approved
"#;
        let doc = parse_truth_document(content).unwrap();
        let report = simulate(&doc, &SimulationConfig::default());
        assert_eq!(report.verdict, Verdict::WillNotConverge);
        assert!(
            report
                .findings
                .iter()
                .any(|f| f.message.contains("approver has nothing to review"))
        );
    }

    #[test]
    fn simulate_spec_convenience() {
        let content = r#"Truth: Quick test

Intent:
  Outcome: Works.

Authority:
  Actor: admin

Evidence:
  Requires: proof

Scenario: It works
  Given something exists
  When validated
  Then it passes
"#;
        let report = simulate_spec(content, &SimulationConfig::default()).unwrap();
        assert!(report.can_converge());
    }
}