sim-lib-standard-core 0.1.5

Standard distribution core for SIM capabilities, claims, tests, and profiles.
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
// conformance: bounded scenario execution and content-addressed capture evidence.

use std::sync::{
    Arc,
    atomic::{AtomicUsize, Ordering},
};

use sim_kernel::{
    ClaimKind, ClaimPattern, Cx, Datum, DatumStore, DefaultFactory, Expr, NoopEvalPolicy, Ref,
    Symbol,
    card::{card_for_ref, card_tests_predicate},
    standard::standard_evidence_predicate,
};

use crate::{
    BoundedLane, CanonicalObservation, CanonicalOutcome, CaptureComparisonProjection,
    CharacterizationCapture, CharacterizationScenario, ConformanceHarness, ConformanceOutcome,
    ConformanceTestCase, FidelityBadge, LanguageProfile, OrganUse, ScenarioInput, ScenarioLimits,
    ScenarioObservationLane, ScenarioSpec, StandardTestReport, characterization_capture_kind,
    characterization_capture_predicate, compare_characterization_captures,
    publish_characterization_capture, standard_binding_organ_symbol,
    standard_reported_fidelity_level_predicate, standard_test_capability,
    standard_test_result_predicate, standard_test_run_kind, standard_test_status_predicate,
    standard_test_stub,
};

#[test]
fn captures_intern_by_semantics_and_publish_scenario_evidence() {
    let mut cx = test_cx();
    let scenario = valid_scenario_spec("capture");
    let baseline = CharacterizationCapture::new(
        Symbol::qualified("projection", "canonical/v1"),
        outcome_observation("same"),
    );

    let first = publish_characterization_capture(&mut cx, &scenario, &baseline).unwrap();
    let rendered_differently = baseline.clone();
    let second =
        publish_characterization_capture(&mut cx, &scenario, &rendered_differently).unwrap();

    assert_eq!(first, second, "rendering is outside capture identity");
    assert_has_claim(
        &cx,
        Ref::Symbol(scenario.id.clone()),
        characterization_capture_predicate(),
        first.clone(),
    );
    let Ref::Content(id) = first else {
        panic!("capture must be content addressed");
    };
    let Some(Datum::Node { tag, .. }) = cx.datum_store().get(&id).unwrap() else {
        panic!("capture datum must be interned");
    };
    assert_eq!(tag, &characterization_capture_kind());

    let changed_observation =
        CharacterizationCapture::new(baseline.projection.clone(), outcome_observation("changed"));
    let changed =
        publish_characterization_capture(&mut cx, &scenario, &changed_observation).unwrap();
    assert_ne!(Ref::Content(id), changed);

    let mut changed_projection = baseline.clone();
    changed_projection.projection = Symbol::qualified("projection", "other/v1");
    assert_ne!(
        second,
        publish_characterization_capture(&mut cx, &scenario, &changed_projection).unwrap()
    );
}

#[test]
fn captures_fail_closed_on_schema_bounds_and_incomplete_lanes() {
    let scenario = valid_scenario_spec("rejected");
    let valid = CharacterizationCapture::new(
        Symbol::qualified("projection", "canonical/v1"),
        outcome_observation("same"),
    );

    let mut wrong_schema = valid.clone();
    wrong_schema.schema = Symbol::qualified("standard", "characterization-capture/v2");
    assert!(
        publish_characterization_capture(&mut test_cx(), &scenario, &wrong_schema)
            .unwrap_err()
            .to_string()
            .contains("unsupported characterization capture schema")
    );

    let mut incomplete = valid.clone();
    incomplete.observation.outcome = None;
    assert!(
        publish_characterization_capture(&mut test_cx(), &scenario, &incomplete)
            .unwrap_err()
            .to_string()
            .contains("incomplete value-or-failure")
    );

    let events_scenario = scenario
        .clone()
        .with_limits(ScenarioLimits::new(1, 2))
        .observing(ScenarioObservationLane::Events);
    let mut truncated = valid.clone();
    truncated.observation.events = BoundedLane::Truncated {
        items: vec![Datum::String("kept".to_owned())],
        omitted: 1,
    };
    assert!(
        publish_characterization_capture(&mut test_cx(), &events_scenario, &truncated)
            .unwrap_err()
            .to_string()
            .contains("truncated Events")
    );

    let mut over_limit = valid;
    over_limit.observation.events = BoundedLane::Complete(vec![
        Datum::String("one".to_owned()),
        Datum::String("two".to_owned()),
    ]);
    assert!(
        publish_characterization_capture(&mut test_cx(), &events_scenario, &over_limit)
            .unwrap_err()
            .to_string()
            .contains("exceeds its observation bound")
    );
}

#[test]
fn capture_comparison_reports_recursive_canonical_paths_and_values() {
    let left_scenario = valid_scenario_spec("compare");
    let mut right_scenario = left_scenario.clone();
    right_scenario.setup = Symbol::qualified("setup", "new/v1");
    right_scenario.inputs[0].datum = Datum::Node {
        tag: Symbol::qualified("test", "input/v1"),
        fields: vec![(Symbol::new("value"), Datum::String("right".to_owned()))],
    };
    right_scenario
        .observation_lanes
        .insert(ScenarioObservationLane::Events);

    let projection = Symbol::qualified("projection", "strict/v1");
    let left = CharacterizationCapture::new(projection.clone(), outcome_observation("left"));
    let mut right = CharacterizationCapture::new(projection.clone(), outcome_observation("right"));
    right.observation.events = BoundedLane::Complete(Vec::new());

    let comparison = compare_characterization_captures(
        &left_scenario,
        &left,
        &right_scenario,
        &right,
        &CaptureComparisonProjection::new(projection.clone()),
    )
    .unwrap();

    assert_eq!(comparison.projection, projection);
    assert_eq!(
        comparison
            .differences
            .iter()
            .map(|difference| difference.path.as_str())
            .collect::<Vec<_>>(),
        [
            "$.setup",
            "$.inputs[0].datum",
            "$.selected-lanes[1]",
            "$.observation.outcome.value",
            "$.observation.events",
        ]
    );
    let outcome = comparison
        .differences
        .iter()
        .find(|difference| difference.path == "$.observation.outcome.value")
        .unwrap();
    assert_eq!(outcome.left, Datum::String("left".to_owned()));
    assert_eq!(outcome.right, Datum::String("right".to_owned()));
}

#[test]
fn capture_projection_is_exact_two_sided_and_part_of_capture_identity() {
    let scenario = valid_scenario_spec("projection");
    let identity = Symbol::qualified("projection", "timestamps/v1");
    let left = CharacterizationCapture::new(identity.clone(), outcome_observation("old"));
    let right = CharacterizationCapture::new(identity.clone(), outcome_observation("new"));
    let projection =
        CaptureComparisonProjection::new(identity.clone()).ignoring("$.observation.outcome.value");

    assert!(
        compare_characterization_captures(&scenario, &left, &scenario, &right, &projection)
            .unwrap()
            .is_same()
    );

    let undeclared = CaptureComparisonProjection::new(identity.clone()).ignoring("$.not-present");
    assert!(
        compare_characterization_captures(&scenario, &left, &scenario, &right, &undeclared)
            .unwrap_err()
            .to_string()
            .contains("declares non-two-sided field")
    );

    let changed =
        CaptureComparisonProjection::new(Symbol::qualified("projection", "timestamps/v2"));
    assert!(
        compare_characterization_captures(&scenario, &left, &scenario, &right, &changed)
            .unwrap_err()
            .to_string()
            .contains("is not recorded by both captures")
    );
}

fn outcome_observation(value: &str) -> CanonicalObservation {
    CanonicalObservation {
        outcome: Some(CanonicalOutcome::Success(Datum::String(value.to_owned()))),
        events: BoundedLane::Absent,
        receipts: BoundedLane::Absent,
        browse: BoundedLane::Absent,
    }
}

#[test]
fn invalid_scenario_registry_fails_before_any_driver_effect() {
    let effects = Arc::new(AtomicUsize::new(0));
    let mut harness = ConformanceHarness::new()
        .with_supported_scenario_lanes([ScenarioObservationLane::ValueOrFailure]);
    harness
        .register_scenario(scenario(
            "valid",
            valid_scenario_spec("valid"),
            effects.clone(),
        ))
        .unwrap();
    harness
        .register_scenario(scenario(
            "invalid",
            valid_scenario_spec("invalid")
                .with_limits(ScenarioLimits::new(1, 2))
                .observing(ScenarioObservationLane::Events),
            effects.clone(),
        ))
        .unwrap();

    let error = harness.run_scenarios(&mut test_cx()).unwrap_err();

    assert!(error.to_string().contains("unsupported lane"));
    assert_eq!(effects.load(Ordering::SeqCst), 0);
}

#[test]
fn scenario_preflight_rejects_duplicate_missing_bounds_and_undeclared_authority() {
    let effects = Arc::new(AtomicUsize::new(0));
    let mut duplicate = ConformanceHarness::new();
    duplicate
        .register_scenario(scenario(
            "same",
            valid_scenario_spec("same"),
            effects.clone(),
        ))
        .unwrap();
    let duplicate_error = duplicate
        .register_scenario(scenario(
            "same",
            valid_scenario_spec("same"),
            effects.clone(),
        ))
        .unwrap_err();
    assert!(
        duplicate_error
            .to_string()
            .contains("duplicate scenario id")
    );

    for invalid in [
        ScenarioSpec::new(scenario_symbol("missing"), setup_symbol())
            .with_authority(authority_symbol())
            .observing(ScenarioObservationLane::ValueOrFailure),
        ScenarioSpec::new(scenario_symbol("authority"), setup_symbol())
            .with_limits(ScenarioLimits::new(1, 1))
            .with_input(ScenarioInput::new(
                Symbol::new("input"),
                authority_symbol(),
                sim_kernel::Datum::String("value".to_owned()),
            ))
            .observing(ScenarioObservationLane::ValueOrFailure),
    ] {
        let mut harness = ConformanceHarness::new();
        harness
            .register_scenario(scenario("invalid", invalid, effects.clone()))
            .unwrap();
        assert!(harness.run_scenarios(&mut test_cx()).is_err());
    }
    assert_eq!(effects.load(Ordering::SeqCst), 0);
}

#[test]
fn bounded_scenarios_run_in_stable_identity_order() {
    let effects = Arc::new(AtomicUsize::new(0));
    let mut harness = ConformanceHarness::new();
    harness
        .register_scenario(scenario(
            "second",
            valid_scenario_spec("second"),
            effects.clone(),
        ))
        .unwrap();
    harness
        .register_scenario(scenario(
            "first",
            valid_scenario_spec("first"),
            effects.clone(),
        ))
        .unwrap();

    let completed = harness.run_scenarios(&mut test_cx()).unwrap();

    assert_eq!(
        completed,
        vec![scenario_symbol("first"), scenario_symbol("second")]
    );
    assert_eq!(effects.load(Ordering::SeqCst), 2);
}

#[test]
fn standard_test_reports_per_organ_pass_fail() {
    let mut cx = test_cx();
    cx.grant(standard_test_capability());
    let profile = conformance_profile(profile_symbol());

    let report = standard_test_stub(&mut cx, &conformance_harness(false), &profile).unwrap();

    assert!(!report.passed());
    assert_eq!(report.result_count(), 2);
    assert!(organ_report(&report, control_organ()).passed());
    let binding = organ_report(&report, standard_binding_organ_symbol());
    assert!(!binding.passed());
    assert_eq!(
        binding.tests[0].detail.as_deref(),
        Some("binding regression")
    );
}

#[test]
fn failed_organ_tests_lower_reported_badge() {
    let mut cx = test_cx();
    cx.grant(standard_test_capability());
    let profile = conformance_profile(profile_symbol());

    let report = standard_test_stub(&mut cx, &conformance_harness(false), &profile).unwrap();

    let control_badge = reported_badge(&report, Symbol::qualified("standard", "control"));
    assert_eq!(control_badge.level, 2);
    assert_eq!(control_badge.evidence, Ref::Symbol(control_test_symbol()));

    let binding_badge = reported_badge(&report, binding_badge_symbol());
    let failed_evidence = organ_report(&report, standard_binding_organ_symbol()).tests[0]
        .evidence
        .clone();
    assert_eq!(binding_badge.level, 1);
    assert_eq!(binding_badge.evidence, failed_evidence);
}

#[test]
fn harness_is_profile_agnostic_and_organ_keyed() {
    let mut cx = test_cx();
    cx.grant(standard_test_capability());
    let mut harness = ConformanceHarness::new();
    harness.register_test(pass_case(control_test_symbol(), control_organ()));

    assert_eq!(harness.test_count(), 1);
    assert_eq!(harness.tests_for_organ(&control_organ()).len(), 1);
    assert!(
        harness
            .tests_for_organ(&standard_binding_organ_symbol())
            .is_empty()
    );

    let first =
        standard_test_stub(&mut cx, &harness, &conformance_profile(profile_symbol())).unwrap();
    let second = standard_test_stub(
        &mut cx,
        &harness,
        &conformance_profile(Symbol::qualified("lang", "other-conformance/v1")),
    )
    .unwrap();
    assert_eq!(first.result_count(), 1);
    assert_eq!(second.result_count(), 1);
    assert!(first.passed());
    assert!(second.passed());
}

#[test]
fn standard_test_publishes_cards_and_evidence_claims() {
    let mut cx = test_cx();
    cx.grant(standard_test_capability());
    let profile = conformance_profile(profile_symbol());

    let report = standard_test_stub(&mut cx, &conformance_harness(false), &profile).unwrap();
    let failed_evidence = organ_report(&report, standard_binding_organ_symbol()).tests[0]
        .evidence
        .clone();
    let evidence_card = card_for_ref(&mut cx, failed_evidence.clone())
        .unwrap()
        .object()
        .as_expr(&mut cx)
        .unwrap();

    assert_eq!(
        table_value(&evidence_card, "kind"),
        Some(&Expr::Symbol(standard_test_run_kind()))
    );
    assert_list_contains_symbol(
        table_value(&evidence_card, "tests").unwrap(),
        binding_test_symbol(),
    );
    assert_has_claim(
        &cx,
        failed_evidence.clone(),
        card_tests_predicate(),
        Ref::Symbol(binding_test_symbol()),
    );
    assert_has_claim(
        &cx,
        Ref::Symbol(profile.symbol.clone()),
        standard_test_result_predicate(),
        failed_evidence.clone(),
    );
    assert_has_claim(
        &cx,
        Ref::Symbol(profile.symbol.clone()),
        standard_evidence_predicate(),
        failed_evidence.clone(),
    );
    assert_has_claim(
        &cx,
        failed_evidence.clone(),
        standard_test_status_predicate(),
        Ref::Symbol(Symbol::qualified("standard/test", "fail")),
    );
    let level_claims = cx
        .query_facts(ClaimPattern::exact(
            Ref::Symbol(profile.symbol.clone()),
            standard_reported_fidelity_level_predicate(),
            Ref::Symbol(Symbol::qualified("standard/fidelity-level", "1")),
        ))
        .unwrap();
    assert_eq!(level_claims.len(), 1);
    assert_eq!(level_claims[0].kind, ClaimKind::Observed);
    assert_eq!(level_claims[0].evidence, vec![failed_evidence]);
}

fn conformance_profile(profile: Symbol) -> LanguageProfile {
    LanguageProfile::new(profile.clone())
        .with_reader(Symbol::qualified("codec", "lisp"))
        .with_lowering(Symbol::qualified("standard", "identity-lowering"))
        .with_eval_policy(Symbol::qualified("eval", "noop"))
        .with_organ(OrganUse::new(control_organ()))
        .with_organ(OrganUse::new(standard_binding_organ_symbol()))
        .with_conformance_test(control_test_symbol())
        .with_conformance_test(binding_test_symbol())
        .with_fidelity_badge(FidelityBadge::new(
            Ref::Symbol(profile.clone()),
            Symbol::qualified("standard", "control"),
            2,
            Ref::Symbol(control_test_symbol()),
        ))
        .with_fidelity_badge(FidelityBadge::new(
            Ref::Symbol(profile),
            binding_badge_symbol(),
            2,
            Ref::Symbol(binding_test_symbol()),
        ))
}

fn valid_scenario_spec(name: &str) -> ScenarioSpec {
    ScenarioSpec::new(scenario_symbol(name), setup_symbol())
        .with_authority(authority_symbol())
        .with_limits(ScenarioLimits::new(1, 1))
        .with_input(ScenarioInput::new(
            Symbol::new("input"),
            authority_symbol(),
            sim_kernel::Datum::String("value".to_owned()),
        ))
        .observing(ScenarioObservationLane::ValueOrFailure)
}

fn scenario(
    _name: &str,
    spec: ScenarioSpec,
    effects: Arc<AtomicUsize>,
) -> CharacterizationScenario {
    CharacterizationScenario::new(
        spec,
        Arc::new(move |_, _| {
            effects.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }),
    )
}

fn scenario_symbol(name: &str) -> Symbol {
    Symbol::qualified("scenario", name)
}

fn setup_symbol() -> Symbol {
    Symbol::qualified("setup", "standard-core")
}

fn authority_symbol() -> Symbol {
    Symbol::qualified("authority", "evaluate")
}

fn conformance_harness(binding_passes: bool) -> ConformanceHarness {
    let mut harness = ConformanceHarness::new();
    harness.register_test(pass_case(control_test_symbol(), control_organ()));
    let binding = if binding_passes {
        pass_case(binding_test_symbol(), standard_binding_organ_symbol())
    } else {
        fail_case(
            binding_test_symbol(),
            standard_binding_organ_symbol(),
            "binding regression",
        )
        .affecting_badge(binding_badge_symbol())
    };
    harness.register_test(binding);
    harness
}

fn pass_case(test: Symbol, organ: Symbol) -> ConformanceTestCase {
    ConformanceTestCase::new(test, organ, Arc::new(|_, _| Ok(ConformanceOutcome::pass())))
}

fn fail_case(test: Symbol, organ: Symbol, detail: &'static str) -> ConformanceTestCase {
    ConformanceTestCase::new(
        test,
        organ,
        Arc::new(move |_, _| Ok(ConformanceOutcome::fail(detail))),
    )
}

fn organ_report(report: &StandardTestReport, organ: Symbol) -> &crate::OrganTestReport {
    report
        .organs
        .iter()
        .find(|reported| reported.organ == organ)
        .unwrap()
}

fn reported_badge(report: &StandardTestReport, badge: Symbol) -> &FidelityBadge {
    report
        .reported_badges
        .iter()
        .find(|reported| reported.badge == badge)
        .unwrap()
}

fn control_organ() -> Symbol {
    Symbol::qualified("organ", "control")
}

fn profile_symbol() -> Symbol {
    Symbol::qualified("lang", "conformance/v1")
}

fn control_test_symbol() -> Symbol {
    Symbol::qualified("test", "control-pass")
}

fn binding_test_symbol() -> Symbol {
    Symbol::qualified("test", "binding-fail")
}

fn binding_badge_symbol() -> Symbol {
    Symbol::qualified("standard", "binding")
}

fn test_cx() -> Cx {
    Cx::new(Arc::new(NoopEvalPolicy), Arc::new(DefaultFactory))
}

fn table_value<'a>(expr: &'a Expr, key: &str) -> Option<&'a Expr> {
    let Expr::Map(entries) = expr else {
        return None;
    };
    entries.iter().find_map(|(entry_key, entry_value)| {
        let Expr::Symbol(entry_key) = entry_key else {
            return None;
        };
        (entry_key == &Symbol::new(key)).then_some(entry_value)
    })
}

fn assert_list_contains_symbol(expr: &Expr, expected: Symbol) {
    let Expr::List(items) = expr else {
        panic!("expected list");
    };
    assert!(
        items
            .iter()
            .any(|item| item == &Expr::Symbol(expected.clone())),
        "expected list to contain {expected}"
    );
}

fn assert_has_claim(cx: &Cx, subject: Ref, predicate: Symbol, object: Ref) {
    let claims = cx
        .query_facts(ClaimPattern::exact(subject, predicate, object))
        .unwrap();
    assert_eq!(claims.len(), 1);
}