etdl-compiler 0.4.0

ETDL compiler: IEC 61025 fault tree resolution, MOCUS cut sets, ECEL type-checking, semantic validation, and code generation for event-driven microservices
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
//! Integration test for reliability-aware compilation: an `.etdl` document
//! declares the reliability supplement and an external artifact; the compiler
//! resolves the artifact to deterministic probabilities that feed fault-tree
//! evaluation, and emits a build manifest.

use std::path::PathBuf;

fn fixture_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("reliability_fixtures")
}

#[test]
fn external_artifact_resolves_into_fault_tree() {
    let base = fixture_dir();
    let doc = etdl_parser::parse_document_from_file(&base.join("external.etdl"))
        .expect("document parses");
    let registry = etdl_parser::load_asyncapi_imports(&doc, &base).expect("imports load");
    let compiler = etdl_compiler::Compiler::new();
    let result = compiler.compile_with_base(&doc, &registry, &base);

    let errors: Vec<_> = result.diagnostics.iter().filter(|d| d.is_error()).collect();
    assert!(
        errors.is_empty(),
        "expected no errors, got {:?}",
        errors
            .iter()
            .map(|d| (&d.code, &d.message))
            .collect::<Vec<_>>()
    );

    // The generated code should embed the resolved probabilities.
    let _rust = result.rust_output.expect("rust output present");
    let manifest = result
        .build_manifest
        .expect("build manifest present for reliability build");
    let resolved = manifest["resolved_probabilities"].as_array().unwrap();
    assert_eq!(resolved.len(), 2);

    // Verify the resolved values appear in the manifest with provenance.
    let values: Vec<f64> = resolved
        .iter()
        .map(|r| r["value"].as_f64().unwrap())
        .collect();
    assert!(values.contains(&0.0027));
    assert!(values.contains(&0.0012));

    // Fault-tree evaluation must use the resolved values: OR(0.0027, 0.0012).
    let mut diags = Vec::new();
    let probs = etdl_compiler::fault_tree::resolve_fault_trees_with_overrides(
        &doc,
        &etdl_compiler::fault_tree::BasicEventOverrides::from([
            (
                etdl_compiler::fault_tree::override_key("PaymentGatewayFailure", "GatewayTimeout"),
                0.0027,
            ),
            (
                etdl_compiler::fault_tree::override_key(
                    "PaymentGatewayFailure",
                    "GatewayUnreachable",
                ),
                0.0012,
            ),
        ]),
        &mut diags,
    );
    let expected = 1.0 - (1.0 - 0.0027) * (1.0 - 0.0012);
    let actual = probs["PaymentGatewayFailure"];
    assert!(
        (actual - expected).abs() < 1e-9,
        "expected {expected}, got {actual}"
    );
}

#[test]
fn backward_compat_no_supplement_unchanged() {
    // The worked example, with no supplement, must resolve exactly as before.
    let base = fixture_dir();
    let doc = etdl_parser::parse_document_from_file(&base.join("core-only.etdl")).expect("parses");
    let registry = etdl_parser::load_asyncapi_imports(&doc, &base).expect("imports load");
    let compiler = etdl_compiler::Compiler::new();
    let result = compiler.compile(&doc, &registry);

    let errors: Vec<_> = result.diagnostics.iter().filter(|d| d.is_error()).collect();
    assert!(errors.is_empty(), "core-only should validate: {:?}", errors);
    assert!(
        result.build_manifest.is_none(),
        "no reliability supplement => no manifest"
    );
    // Fault-tree probability unchanged: OR(0.008, 1-e^(-0.00021*24)).
    let mut diags = Vec::new();
    let probs = etdl_compiler::fault_tree::resolve_fault_trees(&doc, &mut diags);
    let expected = 1.0 - (1.0 - 0.008) * (1.0 - 0.005027);
    let actual = probs["PaymentGatewayFailure"];
    assert!(
        (actual - expected).abs() < 0.0001,
        "expected ~{expected}, got {actual}"
    );
}

#[test]
fn supplement_declared_but_optional_and_unsupported_warns() {
    let yaml = r#"
etdl: "1.0.0"
info:
  title: "T"
  version: "1.0.0"
  domain: "D"
asyncapi_imports: {}
supplements:
  - id: etdl.definitely-not-implemented
    version: "1.0"
eventTrees:
  T:
    initiatingEvent: { id: I, message: "a#/m", next: C }
    nodes:
      C: { type: consequence, operation: terminate }
"#;
    let doc: etdl_parser::ast::EtlDocument = serde_yaml::from_str(yaml).unwrap();
    let registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
    let compiler = etdl_compiler::Compiler::new();
    let result = compiler.validate(&doc, &registry);
    assert!(
        result.iter().any(|d| d.code == "W-407"),
        "optional unsupported supplement should warn W-407, got {:?}",
        result
    );
}

#[test]
fn golden_worked_example_probability_is_exact() {
    // The canonical worked example resolves the two Basic Events through their
    // OR gate to ~0.012987. This MUST be unchanged with no reliability
    // supplement involved (spec §13, Section 5.16).
    let base = fixture_dir();
    let doc = etdl_parser::parse_document_from_file(&base.join("core-only.etdl")).unwrap();
    let mut diags = Vec::new();
    let probs = etdl_compiler::fault_tree::resolve_fault_trees(&doc, &mut diags);
    let p = probs["PaymentGatewayFailure"];
    let expected = 1.0 - (1.0 - 0.008) * (1.0 - 0.005027);
    assert!(
        (p - expected).abs() < 0.0001,
        "worked example probability drifted: expected ~{expected}, got {p}"
    );
    // The generated constant format is 6 decimals -> 0.012987.
    assert!((p - 0.012987).abs() < 0.00001, "expected 0.012987, got {p}");
}

#[test]
fn required_unsupported_supplement_errors() {
    let yaml = r#"
etdl: "1.0.0"
info:
  title: "T"
  version: "1.0.0"
  domain: "D"
asyncapi_imports: {}
supplements:
  - id: etdl.definitely-not-implemented
    version: "1.0"
    required: true
eventTrees:
  T:
    initiatingEvent: { id: I, message: "a#/m", next: C }
    nodes:
      C: { type: consequence, operation: terminate }
"#;
    let doc: etdl_parser::ast::EtlDocument = serde_yaml::from_str(yaml).unwrap();
    let registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
    let compiler = etdl_compiler::Compiler::new();
    let result = compiler.validate(&doc, &registry);
    assert!(
        result.iter().any(|d| d.code == "E-108"),
        "required unsupported supplement should error E-108, got {:?}",
        result
    );
}

#[test]
fn multi_fault_tree_overrides_do_not_collide() {
    // Two fault trees that each define a basic event named "PowerLoss".
    // Overrides keyed by fault-tree id must not leak across trees.
    let doc = r#"
etdl: "1.0.0"
info:
  title: "Multi-tree"
  version: "1.0.0"
  domain: "D"
asyncapi_imports: {}
eventTrees:
  T:
    initiatingEvent: { id: I, message: "a#/m", next: C }
    nodes:
      C: { type: consequence, operation: terminate }
faultTrees:
  A:
    topEvent:
      id: TopA
      description: "top A"
      rootCause: PowerLoss
    basicEvents:
      PowerLoss:
        description: "power"
        probability: 0.5
  B:
    topEvent:
      id: TopB
      description: "top B"
      rootCause: PowerLoss
    basicEvents:
      PowerLoss:
        description: "power"
        probability: 0.5
"#;
    let doc: etdl_parser::ast::EtlDocument = serde_yaml::from_str(doc).unwrap();

    let mut diags = Vec::new();
    let probs = etdl_compiler::fault_tree::resolve_fault_trees_with_overrides(
        &doc,
        &etdl_compiler::fault_tree::BasicEventOverrides::from([
            (
                etdl_compiler::fault_tree::override_key("A", "PowerLoss"),
                0.001,
            ),
            (
                etdl_compiler::fault_tree::override_key("B", "PowerLoss"),
                0.9,
            ),
        ]),
        &mut diags,
    );
    assert!(diags.is_empty(), "unexpected diagnostics: {:?}", diags);
    assert!(
        (probs["A"] - 0.001).abs() < 1e-9,
        "tree A got {}",
        probs["A"]
    );
    assert!((probs["B"] - 0.9).abs() < 1e-9, "tree B got {}", probs["B"]);
}

#[test]
fn override_key_is_compound_and_unescapable() {
    assert_eq!(
        etdl_compiler::fault_tree::override_key("ft", "be"),
        "ft::be"
    );
    // The compound form distinguishes trees with the same basic-event name.
    assert_ne!(
        etdl_compiler::fault_tree::override_key("A", "X"),
        etdl_compiler::fault_tree::override_key("B", "X")
    );
}

fn unknown_policy_doc() -> etdl_parser::ast::EtlDocument {
    let yaml = r#"
etdl: "1.0.0"
info:
  title: "Unknown policy"
  version: "1.0.0"
  domain: "D"
asyncapi_imports:
  api: "./api.yaml"
supplements:
  - id: etdl.reliability
    version: "1.0"
eventTrees:
  T:
    initiatingEvent: { id: I, message: "api#/components/messages/Event", next: C }
    nodes:
      C: { type: consequence, operation: terminate }
faultTrees:
  FT:
    topEvent:
      id: Top
      description: "top"
      rootCause: X
    gates:
      X:
        type: OR
        inputs: [A, B]
    basicEvents:
      A:
        description: "a"
        probability: 0.01
        x-reliability:
          source: gw
          estimate: est.a
      B:
        description: "b"
        probability: 0.02
        x-reliability:
          source: gw
          estimate: est.b
x-reliability:
  unknownPolicy: error
  sources:
    - id: gw
      type: external
      file: "./unknown.rprob"
"#;
    serde_yaml::from_str(yaml).unwrap()
}

#[test]
fn unknown_estimate_policy_error_fails_build() {
    let base = fixture_dir();
    let doc = unknown_policy_doc();
    let registry = etdl_parser::load_asyncapi_imports(&doc, &base).expect("imports load");
    let compiler = etdl_compiler::Compiler::new();
    let result = compiler.compile_with_base(&doc, &registry, &base);
    let errors: Vec<_> = result.diagnostics.iter().filter(|d| d.is_error()).collect();
    assert!(
        errors.iter().any(|d| d.code == "E-112"),
        "expected E-112, got {:?}",
        errors.iter().map(|d| &d.code).collect::<Vec<_>>()
    );
}

#[test]
fn unknown_estimate_policy_warning_falls_back_to_declared() {
    let doc_yml = r#"
etdl: "1.0.0"
info:
  title: "Unknown policy"
  version: "1.0.0"
  domain: "D"
asyncapi_imports:
  api: "./api.yaml"
supplements:
  - id: etdl.reliability
    version: "1.0"
eventTrees:
  T:
    initiatingEvent: { id: I, message: "api#/components/messages/Event", next: C }
    nodes:
      C: { type: consequence, operation: terminate }
faultTrees:
  FT:
    topEvent:
      id: Top
      description: "top"
      rootCause: X
    gates:
      X:
        type: OR
        inputs: [A, B]
    basicEvents:
      A:
        description: "a"
        probability: 0.01
        x-reliability:
          source: gw
          estimate: est.a
      B:
        description: "b"
        probability: 0.02
        x-reliability:
          source: gw
          estimate: est.b
x-reliability:
  unknownPolicy: warning
  sources:
    - id: gw
      type: external
      file: "./unknown.rprob"
"#;
    let doc = serde_yaml::from_str(doc_yml).unwrap();
    let base = fixture_dir();
    let registry = etdl_parser::load_asyncapi_imports(&doc, &base).expect("imports load");
    let compiler = etdl_compiler::Compiler::new();
    let result = compiler.compile_with_base(&doc, &registry, &base);

    let errors: Vec<_> = result.diagnostics.iter().filter(|d| d.is_error()).collect();
    assert!(
        errors.is_empty(),
        "warning policy must not error, got {:?}",
        errors.iter().map(|d| &d.code).collect::<Vec<_>>()
    );
    // W-408 warns about the fallback.
    assert!(
        result.diagnostics.iter().any(|d| d.code == "W-408"),
        "expected W-408, got {:?}",
        result
            .diagnostics
            .iter()
            .map(|d| &d.code)
            .collect::<Vec<_>>()
    );
    // No override was applied for the unknown estimate.
    let manifest = result.build_manifest.expect("manifest present");
    let resolved = manifest["resolved_probabilities"].as_array().unwrap();
    assert!(
        resolved.is_empty(),
        "unknown estimates must not become overrides, got {resolved:?}"
    );
    // The fault tree evaluates from the declared values.
    let mut diags = Vec::new();
    let probs = etdl_compiler::fault_tree::resolve_fault_trees_with_overrides(
        &doc,
        &etdl_compiler::fault_tree::BasicEventOverrides::new(),
        &mut diags,
    );
    let expected = 1.0 - (1.0 - 0.01) * (1.0 - 0.02);
    let actual = probs["FT"];
    assert!(
        (actual - expected).abs() < 1e-9,
        "expected fallback {expected}, got {actual}"
    );
}

#[test]
fn path_traversal_in_reliability_source_rejected() {
    let yaml = r#"
etdl: "1.0.0"
info:
  title: "Traversal"
  version: "1.0.0"
  domain: "D"
asyncapi_imports:
  api: "./api.yaml"
supplements:
  - id: etdl.reliability
    version: "1.0"
eventTrees:
  T:
    initiatingEvent: { id: I, message: "api#/components/messages/Event", next: C }
    nodes:
      C: { type: consequence, operation: terminate }
x-reliability:
  sources:
    - id: gw
      type: external
      file: "../../etc/secret.rprob"
"#;
    let doc: etdl_parser::ast::EtlDocument = serde_yaml::from_str(yaml).unwrap();
    let base = fixture_dir();
    let registry = etdl_parser::load_asyncapi_imports(&doc, &base).expect("imports load");
    let compiler = etdl_compiler::Compiler::new();
    let result = compiler.compile_with_base(&doc, &registry, &base);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code == "E-110" && d.message.contains("..")),
        "expected E-110 traversal error, got {:?}",
        result
            .diagnostics
            .iter()
            .map(|d| (&d.code, &d.message))
            .collect::<Vec<_>>()
    );
}

#[test]
fn multi_source_resolution_uses_compound_keys() {
    // A document with two sources and two fault trees sharing a basic-event
    // name; each tree's override must come from its own artifact.
    let base = fixture_dir();
    let doc = etdl_parser::parse_document_from_file(&base.join("multisource.etdl"))
        .expect("document parses");
    let registry = etdl_parser::load_asyncapi_imports(&doc, &base).expect("imports load");
    let compiler = etdl_compiler::Compiler::new();
    let result = compiler.compile_with_base(&doc, &registry, &base);
    let errors: Vec<_> = result.diagnostics.iter().filter(|d| d.is_error()).collect();
    assert!(
        errors.is_empty(),
        "expected no errors, got {:?}",
        errors
            .iter()
            .map(|d| (&d.code, &d.message))
            .collect::<Vec<_>>()
    );
    let manifest = result.build_manifest.expect("manifest present");
    let resolved = manifest["resolved_probabilities"].as_array().unwrap();
    assert_eq!(resolved.len(), 2, "expected 2 resolved entries");
    // Each entry must carry its fault tree id.
    let ft_ids: Vec<&str> = resolved
        .iter()
        .map(|r| r["fault_tree"].as_str().unwrap())
        .collect();
    assert!(ft_ids.contains(&"Alpha"));
    assert!(ft_ids.contains(&"Beta"));
}