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
//! TruthDocument → IntentPacket compilation.
//!
//! Compiles a parsed [`TruthDocument`] into a runtime [`IntentPacket`].
//! Truth lives here in axiom; the IntentPacket is organism's runtime contract.
//! Callers (helms, atelier showcase) parse `.truths` source with
//! [`crate::parse_truth_document`], then call [`compile_intent`] before
//! handing the packet off to organism's runtime admission.

use chrono::{DateTime, Duration, TimeZone, Utc};
use organism_pack::{ExpiryAction, ForbiddenAction, IntentPacket, Reversibility};

use crate::truths::{AuthorityBlock, ConstraintBlock, ExceptionBlock, IntentBlock, TruthDocument};

/// Errors produced when compiling a [`TruthDocument`] into an [`IntentPacket`].
#[derive(Debug, Clone, thiserror::Error)]
pub enum CompileError {
    /// The Truth's `Intent:` block is missing or has no outcome/goal text.
    /// An IntentPacket needs a non-empty outcome to drive resolution.
    #[error("truth document has no Intent: outcome or goal")]
    MissingOutcome,

    /// The `Authority: expires` field was present but could not be parsed as
    /// an RFC-3339 timestamp or `YYYY-MM-DD` date.
    #[error("could not parse Authority.expires '{value}': {message}")]
    ExpiryParse { value: String, message: String },
}

/// Default expiry window applied when the Truth doesn't specify one. Intents
/// without an explicit deadline get one day; the runtime can re-issue the
/// IntentPacket if the work outlives that window.
const DEFAULT_EXPIRY_HOURS: i64 = 24;

/// Compile a parsed [`TruthDocument`] into an [`IntentPacket`].
///
/// Field mapping (Truth governance block → IntentPacket field):
/// - `Intent.outcome` (or `Intent.goal` as fallback) → `outcome`
/// - `Authority.may` → `authority`
/// - `Authority.must_not` ⊕ `Constraint.must_not` → `forbidden`
///   (deduplicated; Authority entries get an `authority` reason, Constraint
///   entries get a `constraint` reason)
/// - `Authority.requires_approval` → folded into `constraints` as
///   `"requires_approval: <action>"` lines
/// - `Authority.expires` → `expires` (RFC-3339; falls back to `YYYY-MM-DD`
///   interpreted as midnight UTC)
/// - `Constraint.budget` ⊕ `Constraint.cost_limit` → `constraints`
/// - `Exception.escalates_to` ⊕ `Exception.requires` → `expiry_action`
///   (presence flips the default `Halt` to `Escalate`)
/// - Reversibility defaults to `Reversible`. Truths can override via a
///   constraint of the form `"reversibility: irreversible"` (case-insensitive).
///
/// The Gherkin body itself is NOT folded into the IntentPacket; it is the
/// validation/simulation surface, not the runtime contract.
///
/// # Errors
///
/// Returns [`CompileError::MissingOutcome`] if neither outcome nor goal is set,
/// and [`CompileError::ExpiryParse`] if `Authority.expires` is malformed.
pub fn compile_intent(doc: &TruthDocument) -> Result<IntentPacket, CompileError> {
    let outcome = extract_outcome(doc.governance.intent.as_ref())?;
    let expires = extract_expiry(doc.governance.authority.as_ref())?;
    let authority = extract_authority(doc.governance.authority.as_ref());
    let forbidden = extract_forbidden(
        doc.governance.authority.as_ref(),
        doc.governance.constraint.as_ref(),
    );
    let constraints = extract_constraints(
        doc.governance.authority.as_ref(),
        doc.governance.constraint.as_ref(),
    );
    let reversibility = extract_reversibility(&constraints);
    let expiry_action = extract_expiry_action(doc.governance.exception.as_ref());

    let packet = IntentPacket::new(outcome, expires)
        .with_authority(authority)
        .with_reversibility(reversibility)
        .with_expiry_action(expiry_action);

    Ok(IntentPacket {
        constraints,
        forbidden,
        ..packet
    })
}

/// Convenience: parse Truth source and compile to an IntentPacket in one step.
///
/// # Errors
///
/// Returns the parse error if the Truth source is malformed, or the compile
/// error if it is structurally fine but missing required fields.
pub fn compile_intent_from_source(source: &str) -> Result<IntentPacket, CompileFromSourceError> {
    let doc =
        crate::truths::parse_truth_document(source).map_err(CompileFromSourceError::ParseFailed)?;
    compile_intent(&doc).map_err(CompileFromSourceError::CompileFailed)
}

/// Combined error for the source → IntentPacket convenience path.
#[derive(Debug, thiserror::Error)]
pub enum CompileFromSourceError {
    #[error("truth source did not parse: {0}")]
    ParseFailed(crate::gherkin::ValidationError),
    #[error("truth document did not compile: {0}")]
    CompileFailed(CompileError),
}

fn extract_outcome(intent: Option<&IntentBlock>) -> Result<String, CompileError> {
    let block = intent.ok_or(CompileError::MissingOutcome)?;
    block
        .outcome
        .as_ref()
        .or(block.goal.as_ref())
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .ok_or(CompileError::MissingOutcome)
}

fn extract_expiry(authority: Option<&AuthorityBlock>) -> Result<DateTime<Utc>, CompileError> {
    let Some(value) = authority.and_then(|a| a.expires.as_ref()) else {
        return Ok(Utc::now() + Duration::hours(DEFAULT_EXPIRY_HOURS));
    };
    let trimmed = value.trim();
    if let Ok(dt) = DateTime::parse_from_rfc3339(trimmed) {
        return Ok(dt.with_timezone(&Utc));
    }
    if let Some(dt) = chrono::NaiveDate::parse_from_str(trimmed, "%Y-%m-%d")
        .ok()
        .and_then(|d| d.and_hms_opt(0, 0, 0))
        .and_then(|naive| Utc.from_local_datetime(&naive).single())
    {
        return Ok(dt);
    }
    Err(CompileError::ExpiryParse {
        value: value.clone(),
        message: "expected RFC-3339 timestamp or YYYY-MM-DD date".into(),
    })
}

fn extract_authority(authority: Option<&AuthorityBlock>) -> Vec<String> {
    let Some(block) = authority else {
        return Vec::new();
    };
    let mut entries: Vec<String> = block.may.iter().map(|s| s.trim().to_string()).collect();
    if let Some(actor) = block.actor.as_ref() {
        let actor = actor.trim();
        if !actor.is_empty() {
            entries.insert(0, format!("actor: {actor}"));
        }
    }
    entries
}

fn extract_forbidden(
    authority: Option<&AuthorityBlock>,
    constraint: Option<&ConstraintBlock>,
) -> Vec<ForbiddenAction> {
    let mut forbidden: Vec<ForbiddenAction> = Vec::new();
    let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();

    if let Some(auth) = authority {
        for action in &auth.must_not {
            let action = action.trim().to_string();
            if !action.is_empty() && seen.insert(action.clone()) {
                forbidden.push(ForbiddenAction {
                    action,
                    reason: "authority".into(),
                });
            }
        }
    }

    if let Some(con) = constraint {
        for action in &con.must_not {
            let action = action.trim().to_string();
            if !action.is_empty() && seen.insert(action.clone()) {
                forbidden.push(ForbiddenAction {
                    action,
                    reason: "constraint".into(),
                });
            }
        }
    }

    forbidden
}

fn extract_constraints(
    authority: Option<&AuthorityBlock>,
    constraint: Option<&ConstraintBlock>,
) -> Vec<String> {
    let mut entries: Vec<String> = Vec::new();
    if let Some(con) = constraint {
        entries.extend(con.budget.iter().map(|b| format!("budget: {}", b.trim())));
        entries.extend(
            con.cost_limit
                .iter()
                .map(|c| format!("cost_limit: {}", c.trim())),
        );
    }
    if let Some(auth) = authority {
        entries.extend(
            auth.requires_approval
                .iter()
                .map(|a| format!("requires_approval: {}", a.trim())),
        );
    }
    entries
}

fn extract_reversibility(constraints: &[String]) -> Reversibility {
    for c in constraints {
        let lower = c.to_lowercase();
        if lower.contains("reversibility:") {
            if lower.contains("irreversible") {
                return Reversibility::Irreversible;
            }
            if lower.contains("partial") {
                return Reversibility::Partial;
            }
        }
    }
    Reversibility::Reversible
}

fn extract_expiry_action(exception: Option<&ExceptionBlock>) -> ExpiryAction {
    match exception {
        Some(block) if !block.escalates_to.is_empty() || !block.requires.is_empty() => {
            ExpiryAction::Escalate
        }
        _ => ExpiryAction::Halt,
    }
}

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

    fn document(
        intent: Option<IntentBlock>,
        authority: Option<AuthorityBlock>,
        constraint: Option<ConstraintBlock>,
        exception: Option<ExceptionBlock>,
    ) -> TruthDocument {
        TruthDocument {
            gherkin: String::new(),
            governance: TruthGovernance {
                intent,
                authority,
                constraint,
                evidence: None,
                exception,
            },
        }
    }

    #[test]
    fn missing_intent_block_rejected() {
        let doc = document(None, None, None, None);
        assert!(matches!(
            compile_intent(&doc),
            Err(CompileError::MissingOutcome)
        ));
    }

    #[test]
    fn intent_with_only_whitespace_outcome_rejected() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("   ".into()),
                goal: None,
            }),
            None,
            None,
            None,
        );
        assert!(matches!(
            compile_intent(&doc),
            Err(CompileError::MissingOutcome)
        ));
    }

    #[test]
    fn outcome_taken_from_intent_outcome_field() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("qualify inbound leads".into()),
                goal: None,
            }),
            None,
            None,
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert_eq!(packet.outcome, "qualify inbound leads");
    }

    #[test]
    fn outcome_falls_back_to_goal() {
        let doc = document(
            Some(IntentBlock {
                outcome: None,
                goal: Some("qualify inbound leads".into()),
            }),
            None,
            None,
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert_eq!(packet.outcome, "qualify inbound leads");
    }

    #[test]
    fn authority_actor_prefixes_authority_list() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            Some(AuthorityBlock {
                actor: Some("revops_team".into()),
                may: vec!["approve_lead".into(), "request_demo".into()],
                must_not: vec![],
                requires_approval: vec![],
                expires: None,
            }),
            None,
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert_eq!(
            packet.authority,
            vec!["actor: revops_team", "approve_lead", "request_demo"]
        );
    }

    #[test]
    fn forbidden_collects_authority_and_constraint_must_not() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            Some(AuthorityBlock {
                actor: None,
                may: vec![],
                must_not: vec!["delete_account".into()],
                requires_approval: vec![],
                expires: None,
            }),
            Some(ConstraintBlock {
                budget: vec![],
                cost_limit: vec![],
                must_not: vec!["spend_over_500".into()],
            }),
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert_eq!(packet.forbidden.len(), 2);
        assert_eq!(packet.forbidden[0].action, "delete_account");
        assert_eq!(packet.forbidden[0].reason, "authority");
        assert_eq!(packet.forbidden[1].action, "spend_over_500");
        assert_eq!(packet.forbidden[1].reason, "constraint");
    }

    #[test]
    fn forbidden_deduplicates_same_action_across_blocks() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            Some(AuthorityBlock {
                actor: None,
                may: vec![],
                must_not: vec!["delete_account".into()],
                requires_approval: vec![],
                expires: None,
            }),
            Some(ConstraintBlock {
                budget: vec![],
                cost_limit: vec![],
                must_not: vec!["delete_account".into()],
            }),
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert_eq!(packet.forbidden.len(), 1);
        assert_eq!(packet.forbidden[0].reason, "authority");
    }

    #[test]
    fn constraints_carry_budget_cost_and_approval_lines() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            Some(AuthorityBlock {
                actor: None,
                may: vec![],
                must_not: vec![],
                requires_approval: vec!["spend_over_1000".into()],
                expires: None,
            }),
            Some(ConstraintBlock {
                budget: vec!["$500".into()],
                cost_limit: vec!["$100/lead".into()],
                must_not: vec![],
            }),
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert!(packet.constraints.contains(&"budget: $500".to_string()));
        assert!(
            packet
                .constraints
                .contains(&"cost_limit: $100/lead".to_string())
        );
        assert!(
            packet
                .constraints
                .contains(&"requires_approval: spend_over_1000".to_string())
        );
    }

    #[test]
    fn expiry_parses_rfc3339() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            Some(AuthorityBlock {
                actor: None,
                may: vec![],
                must_not: vec![],
                requires_approval: vec![],
                expires: Some("2027-01-15T12:00:00Z".into()),
            }),
            None,
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert_eq!(packet.expires.to_rfc3339(), "2027-01-15T12:00:00+00:00");
    }

    #[test]
    fn expiry_parses_yyyy_mm_dd_as_midnight_utc() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            Some(AuthorityBlock {
                actor: None,
                may: vec![],
                must_not: vec![],
                requires_approval: vec![],
                expires: Some("2027-01-15".into()),
            }),
            None,
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert_eq!(packet.expires.to_rfc3339(), "2027-01-15T00:00:00+00:00");
    }

    #[test]
    fn malformed_expiry_rejected() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            Some(AuthorityBlock {
                actor: None,
                may: vec![],
                must_not: vec![],
                requires_approval: vec![],
                expires: Some("not-a-date".into()),
            }),
            None,
            None,
        );
        assert!(matches!(
            compile_intent(&doc),
            Err(CompileError::ExpiryParse { .. })
        ));
    }

    #[test]
    fn missing_expiry_uses_default_window() {
        let before = Utc::now();
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            None,
            None,
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        let after = Utc::now();
        let expected_min = before + Duration::hours(DEFAULT_EXPIRY_HOURS);
        let expected_max = after + Duration::hours(DEFAULT_EXPIRY_HOURS);
        assert!(packet.expires >= expected_min && packet.expires <= expected_max);
    }

    #[test]
    fn reversibility_irreversible_when_constraint_says_so() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            None,
            Some(ConstraintBlock {
                budget: vec!["reversibility: irreversible".into()],
                cost_limit: vec![],
                must_not: vec![],
            }),
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert_eq!(packet.reversibility, Reversibility::Irreversible);
    }

    #[test]
    fn reversibility_defaults_to_reversible() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            None,
            None,
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert_eq!(packet.reversibility, Reversibility::Reversible);
    }

    #[test]
    fn exception_block_flips_expiry_action_to_escalate() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            None,
            None,
            Some(ExceptionBlock {
                escalates_to: vec!["legal".into()],
                requires: vec![],
            }),
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert_eq!(packet.expiry_action, ExpiryAction::Escalate);
    }

    #[test]
    fn no_exception_block_keeps_default_halt() {
        let doc = document(
            Some(IntentBlock {
                outcome: Some("ship".into()),
                goal: None,
            }),
            None,
            None,
            None,
        );
        let packet = compile_intent(&doc).expect("compiles");
        assert_eq!(packet.expiry_action, ExpiryAction::Halt);
    }

    #[test]
    fn round_trip_from_real_truth_source() {
        let source = r#"Truth: lead qualification

  Intent:
    Outcome: qualify inbound leads end-to-end
    Goal: convert tier-1 leads within SLA

  Authority:
    Actor: revops_team
    May: approve_qualified_lead
    May: request_demo
    Must Not: approve_unverified_lead
    Requires Approval: approve_enterprise_lead
    Expires: 2027-01-15T12:00:00Z

  Constraint:
    Budget: 500_USD/week
    Cost Limit: 50_USD/lead

  Exception:
    Escalates To: sales_director

  @invariant @acceptance
  Scenario: a basic lead arrives
    Given a lead from "acme.com"
    When the lead is qualified
    Then the lead is marked as approved
"#;
        let packet = compile_intent_from_source(source).expect("source parses + compiles");
        assert_eq!(packet.outcome, "qualify inbound leads end-to-end");
        assert!(packet.authority.iter().any(|a| a.contains("revops_team")));
        assert!(
            packet
                .authority
                .iter()
                .any(|a| a == "approve_qualified_lead")
        );
        assert!(
            packet
                .forbidden
                .iter()
                .any(|f| f.action == "approve_unverified_lead" && f.reason == "authority")
        );
        assert!(packet.constraints.iter().any(|c| c.starts_with("budget: ")));
        assert_eq!(packet.expiry_action, ExpiryAction::Escalate);
    }
}