axioval-engine 0.3.0

Trusted capability compiler and deterministic source-neutral validation runtime
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
//! Strict compiler contract tests.
#![allow(missing_docs)]

use axioval_engine::{
    CapabilityEvaluation, CapabilityRegistry, CompiledRule, EngineError, EvidenceSession,
    NotEvaluatedReason, ParameterDescriptor, ParameterType, RuleCapability, RuleContext, Runtime,
    ServiceRegistry, SessionSources, SnapshotBoundService, SourceSnapshot, compile,
};
use axioval_ir::{
    DefinitionPackage, Finding, Object, ObjectId, Project, QuantityDimension, ReportColumn,
    ReportTable, ReportValue, RuleId, RuleSetPackage, Scope, Severity, SourceId,
};

struct Stub;
impl RuleCapability for Stub {
    fn id(&self) -> &'static str {
        "axioval:capability.property-exists"
    }
    fn parameters(&self) -> Vec<ParameterDescriptor> {
        vec![ParameterDescriptor::required(
            "property",
            ParameterType::PropertyReference,
        )]
    }
    fn evaluate(&self, _: &RuleContext<'_>, _: &CompiledRule) -> CapabilityEvaluation {
        CapabilityEvaluation::evaluated(vec![])
    }
}
fn packages() -> (DefinitionPackage, RuleSetPackage) {
    (
        serde_json::from_str(include_str!(
            "../../../../fixtures/schema-v0.1.0/definitions.json"
        ))
        .unwrap(),
        serde_json::from_str(include_str!(
            "../../../../fixtures/schema-v0.1.0/ruleset.json"
        ))
        .unwrap(),
    )
}
#[test]
fn canonical_packages_compile() {
    let (definitions, rules) = packages();
    let registry = CapabilityRegistry::new().register(Stub).unwrap();
    assert_eq!(
        compile(&registry, &[definitions], &rules)
            .unwrap()
            .rules()
            .len(),
        1
    );
}
#[test]
fn compiler_fails_closed_for_missing_required_parameter() {
    let (definitions, mut rules) = packages();
    rules.root.rules[0].parameters.clear();
    let registry = CapabilityRegistry::new().register(Stub).unwrap();
    assert!(compile(&registry, &[definitions], &rules).is_err());
}

#[test]
fn compiler_rejects_unsupported_definition_schema_version() {
    let (mut definitions, rules) = packages();
    definitions.schema_version = "999.0.0".into();
    let registry = CapabilityRegistry::new().register(Stub).unwrap();
    assert!(compile(&registry, &[definitions], &rules).is_err());
}

#[test]
fn compiler_rejects_unsupported_ruleset_schema_version() {
    let (definitions, mut rules) = packages();
    rules.schema_version = "999.0.0".into();
    let registry = CapabilityRegistry::new().register(Stub).unwrap();
    assert!(compile(&registry, &[definitions], &rules).is_err());
}

#[test]
fn runtime_rejects_capability_registry_drift() {
    let (definitions, rules) = packages();
    let compiler_registry = CapabilityRegistry::new().register(Stub).unwrap();
    let plan = compile(&compiler_registry, &[definitions], &rules).unwrap();

    let error = Runtime::new(CapabilityRegistry::new())
        .run(&Project::new(vec![]).unwrap(), plan)
        .unwrap_err();
    assert!(matches!(error, EngineError::UnknownCapability(_)));
}

#[test]
fn compiler_rejects_duplicate_definition_package_ids() {
    let (definitions, rules) = packages();
    let duplicate = definitions.clone();
    let registry = CapabilityRegistry::new().register(Stub).unwrap();

    let error = compile(&registry, &[definitions, duplicate], &rules).unwrap_err();
    assert!(matches!(error, EngineError::DuplicateDefinitionPackage(_)));
}

struct Unavailable;
impl RuleCapability for Unavailable {
    fn id(&self) -> &'static str {
        "axioval:capability.property-exists"
    }
    fn parameters(&self) -> Vec<ParameterDescriptor> {
        vec![ParameterDescriptor::required(
            "property",
            ParameterType::PropertyReference,
        )]
    }
    fn evaluate(&self, _: &RuleContext<'_>, _: &CompiledRule) -> CapabilityEvaluation {
        let mut evaluation = CapabilityEvaluation::default();
        evaluation.push_not_evaluated(NotEvaluatedReason::MissingService, "z diagnostic");
        evaluation.push_not_evaluated(NotEvaluatedReason::MissingService, "a diagnostic");
        evaluation
    }
}

struct SessionMarker {
    label: &'static str,
    snapshots: Vec<SourceSnapshot>,
}
impl SnapshotBoundService for SessionMarker {
    fn source_snapshots(&self) -> &[SourceSnapshot] {
        &self.snapshots
    }
}

struct SessionServiceCapability;
impl RuleCapability for SessionServiceCapability {
    fn id(&self) -> &'static str {
        "axioval:capability.property-exists"
    }
    fn parameters(&self) -> Vec<ParameterDescriptor> {
        vec![ParameterDescriptor::required(
            "property",
            ParameterType::PropertyReference,
        )]
    }
    fn evaluate(&self, context: &RuleContext<'_>, _: &CompiledRule) -> CapabilityEvaluation {
        let mut evaluation = CapabilityEvaluation::default();
        let marker = context.services.get::<SessionMarker>().unwrap();
        evaluation.push_not_evaluated(NotEvaluatedReason::MissingService, marker.label);
        evaluation
    }
}

#[test]
fn runtime_reports_capability_unavailability_without_false_pass() {
    let (definitions, rules) = packages();
    let registry = CapabilityRegistry::new().register(Unavailable).unwrap();
    let plan = compile(&registry, &[definitions], &rules).unwrap();
    let report = Runtime::new(registry)
        .run(&Project::new(vec![]).unwrap(), plan)
        .unwrap();
    assert!(report.findings().is_empty());
    assert_eq!(report.not_evaluated().len(), 2);
    assert_eq!(report.not_evaluated()[0].message, "a diagnostic");
    assert_eq!(report.not_evaluated()[1].message, "z diagnostic");
    assert_eq!(
        report.not_evaluated()[0].reason,
        NotEvaluatedReason::MissingService
    );
    assert_eq!(
        report.not_evaluated()[0].rule_id.to_string(),
        "wall-reference-required"
    );
}

/// Pushes object, source and project outcomes in reverse of report order.
struct Scoped;
impl RuleCapability for Scoped {
    fn id(&self) -> &'static str {
        "axioval:capability.property-exists"
    }
    fn parameters(&self) -> Vec<ParameterDescriptor> {
        vec![ParameterDescriptor::required(
            "property",
            ParameterType::PropertyReference,
        )]
    }
    fn evaluate(&self, _: &RuleContext<'_>, rule: &CompiledRule) -> CapabilityEvaluation {
        let source = |document: &str| SourceId::new("test", document).unwrap();
        let object = ObjectId::new(source("a"), "1").unwrap();
        let finding = |scope: Scope| Finding::new(rule.id.clone(), scope, Severity::Error, "m");
        let mut evaluation = CapabilityEvaluation::default();
        evaluation.push_finding(finding(Scope::Object(object.clone())));
        evaluation.push_finding(finding(Scope::Source(source("b"))));
        evaluation.push_finding(finding(Scope::Source(source("a"))));
        evaluation.push_finding(finding(Scope::Project));
        evaluation.push_object_not_evaluated(object, NotEvaluatedReason::BackendUnavailable, "m");
        evaluation.push_source_not_evaluated(
            source("a"),
            NotEvaluatedReason::IncompleteEvidence,
            "m",
        );
        evaluation.push_not_evaluated(NotEvaluatedReason::MissingService, "m");
        evaluation
    }
}

#[test]
fn source_and_project_outcomes_order_before_object_outcomes() {
    let (definitions, rules) = packages();
    let registry = CapabilityRegistry::new().register(Scoped).unwrap();
    let plan = compile(&registry, &[definitions], &rules).unwrap();
    let report = Runtime::new(registry)
        .run(&Project::new(vec![]).unwrap(), plan)
        .unwrap();
    let scopes: Vec<String> = report
        .findings()
        .iter()
        .map(|finding| finding.scope.to_string())
        .collect();
    assert_eq!(
        scopes,
        ["project", "source test:a", "source test:b", "test:a/1"]
    );
    let scopes: Vec<String> = report
        .not_evaluated()
        .iter()
        .map(|outcome| outcome.scope.to_string())
        .collect();
    assert_eq!(scopes, ["project", "source test:a", "test:a/1"]);
}

/// Pushes tables out of name order, one without rows, and optionally one
/// name twice.
struct Tabled {
    duplicate: bool,
}
impl RuleCapability for Tabled {
    fn id(&self) -> &'static str {
        "axioval:capability.property-exists"
    }
    fn parameters(&self) -> Vec<ParameterDescriptor> {
        vec![ParameterDescriptor::required(
            "property",
            ParameterType::PropertyReference,
        )]
    }
    fn evaluate(&self, _: &RuleContext<'_>, _: &CompiledRule) -> CapabilityEvaluation {
        let object = |local: &str| ObjectId::new(SourceId::new("test", "a").unwrap(), local);
        // The capability names another rule; the runtime binds the compiled one.
        let table = |name: &str| {
            let mut table = ReportTable::new(
                RuleId::new("elsewhere").unwrap(),
                name,
                vec![ReportColumn::quantity("height", QuantityDimension::Length)],
            )
            .unwrap();
            for (local, height) in [("2", 3.5), ("1", 3.0)] {
                table
                    .push_row(object(local).unwrap(), vec![ReportValue::exact(height)])
                    .unwrap();
            }
            table
        };
        let mut evaluation = CapabilityEvaluation::default();
        evaluation.push_table(table("spaces"));
        evaluation.push_table(
            ReportTable::new(
                RuleId::new("elsewhere").unwrap(),
                "empty",
                vec![ReportColumn::number("ratio")],
            )
            .unwrap(),
        );
        evaluation.push_table(table("levels"));
        if self.duplicate {
            evaluation.push_table(table("levels"));
        }
        assert_eq!(evaluation.tables().len(), 2 + usize::from(self.duplicate));
        evaluation
    }
}

#[test]
fn tables_are_bound_to_their_rule_and_ordered_by_rule_and_name() {
    let (definitions, mut rules) = packages();
    let mut earlier = rules.root.rules[0].clone();
    earlier.id = "a-first-rule".into();
    rules.root.rules.push(earlier);
    let registry = CapabilityRegistry::new()
        .register(Tabled { duplicate: false })
        .unwrap();
    let plan = compile(&registry, &[definitions], &rules).unwrap();
    let report = Runtime::new(registry)
        .run(&Project::new(vec![]).unwrap(), plan)
        .unwrap();
    let tables: Vec<(String, &str)> = report
        .tables()
        .iter()
        .map(|table| (table.rule_id().to_string(), table.name()))
        .collect();
    assert_eq!(
        tables,
        [
            ("a-first-rule".to_owned(), "levels"),
            ("a-first-rule".to_owned(), "spaces"),
            ("wall-reference-required".to_owned(), "levels"),
            ("wall-reference-required".to_owned(), "spaces"),
        ]
    );
    let rows: Vec<String> = report.tables()[0]
        .rows()
        .iter()
        .map(|row| row.scope().to_string())
        .collect();
    assert_eq!(rows, ["test:a/1", "test:a/2"]);
}

#[test]
fn a_rule_reporting_one_table_twice_fails_the_run() {
    let (definitions, rules) = packages();
    let registry = CapabilityRegistry::new()
        .register(Tabled { duplicate: true })
        .unwrap();
    let plan = compile(&registry, &[definitions], &rules).unwrap();
    let error = Runtime::new(registry)
        .run(&Project::new(vec![]).unwrap(), plan)
        .unwrap_err();
    assert_eq!(
        error,
        EngineError::DuplicateReportTable {
            rule: "wall-reference-required".into(),
            table: "levels".into(),
        }
    );
}

#[test]
fn session_services_are_authoritative_for_session_runs() {
    let (definitions, rules) = packages();
    let registry = CapabilityRegistry::new()
        .register(SessionServiceCapability)
        .unwrap();
    let plan = compile(&registry, &[definitions], &rules).unwrap();
    let source = SourceId::new("test", "runtime-session").unwrap();
    let snapshot =
        SourceSnapshot::try_new(source.clone(), "revision-1", "sha256:runtime-session").unwrap();
    let project = Project::new(vec![Object::new(
        ObjectId::new(source, "object-1").unwrap(),
        "wall",
    )])
    .unwrap();
    let mut runtime_services = ServiceRegistry::new();
    runtime_services
        .register(SessionMarker {
            label: "runtime",
            snapshots: vec![snapshot.clone()],
        })
        .unwrap();
    let session = EvidenceSession::try_new(project, [snapshot.clone()])
        .unwrap()
        .with_service(SessionMarker {
            label: "session",
            snapshots: vec![snapshot],
        })
        .unwrap();

    let report = Runtime::new(registry)
        .with_services(runtime_services)
        .run_session(&session, plan)
        .unwrap();

    assert_eq!(report.not_evaluated()[0].message, "session");
}

/// Reports the run's sources as one project outcome.
struct ListsSources;
impl RuleCapability for ListsSources {
    fn id(&self) -> &'static str {
        "axioval:capability.property-exists"
    }
    fn parameters(&self) -> Vec<ParameterDescriptor> {
        vec![ParameterDescriptor::required(
            "property",
            ParameterType::PropertyReference,
        )]
    }
    fn evaluate(&self, context: &RuleContext<'_>, _: &CompiledRule) -> CapabilityEvaluation {
        let sources = context.services.get::<SessionSources>().unwrap();
        let listed: Vec<String> = sources.iter().map(ToString::to_string).collect();
        let mut evaluation = CapabilityEvaluation::default();
        evaluation.push_not_evaluated(NotEvaluatedReason::MissingService, listed.join(" "));
        evaluation
    }
}

#[test]
fn the_runtime_lists_every_session_source_including_an_empty_one() {
    let (definitions, rules) = packages();
    let registry = CapabilityRegistry::new().register(ListsSources).unwrap();
    let session_plan = compile(&registry, &[definitions.clone()], &rules).unwrap();
    let bare_plan = compile(&registry, &[definitions], &rules).unwrap();
    let source = |document: &str| SourceId::new("test", document).unwrap();
    let snapshot =
        |document: &str| SourceSnapshot::try_new(source(document), "r1", "sha256:0").unwrap();
    let project = || {
        Project::new(vec![Object::new(
            ObjectId::new(source("full"), "1").unwrap(),
            "wall",
        )])
        .unwrap()
    };
    // A host copy naming only one source is replaced, never trusted.
    let mut host = ServiceRegistry::new();
    host.register(SessionSources::new([source("full")]))
        .unwrap();
    let runtime = Runtime::new(registry).with_services(host);

    let session =
        EvidenceSession::try_new(project(), [snapshot("full"), snapshot("empty")]).unwrap();
    let report = runtime.run_session(&session, session_plan).unwrap();
    assert_eq!(report.not_evaluated()[0].message, "test:empty test:full");

    // A bare project knows only the sources its objects name.
    let report = runtime.run(&project(), bare_plan).unwrap();
    assert_eq!(report.not_evaluated()[0].message, "test:full");
}