dive-api 0.1.0

Embeddable Rust API of the dive TLA+ tools: parsing, semantic analysis, model checking, simulation and trace validation
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! Temporal properties on Core.
//!
//! Core explores and reports its native rows and CSR
//! ([`crate::core_native_graph::NativeSink`]); `dive-liveness`'s planning,
//! completed-level and final checks
//! ([`dive_liveness::analyze::periodic::external::GraphCheck`]), validation
//! and report finish the run. A safety ending is reported as the safety
//! route reports it. What Core does not reproduce exactly is refused by
//! name: `dive_runtime::capture`'s refusals, the options its route
//! excludes, other behavior-graph stops, and temporal failures whose report
//! Core does not build (planning, evaluation and validation failures).
//! Budget and resource stops in temporal analysis are reported as stops.

use std::sync::Arc;
use std::time::Instant;

use dive_ir::Ir;
use dive_liveness::analyze::periodic::Schedule;
use dive_liveness::analyze::periodic::external::{GraphCheck, NativeAnalysis};
use dive_runtime::budget::{EvaluationControl, Phase};
use dive_runtime::capture::{CsrCapture, CsrComplete, CsrPrefix};
use dive_runtime::{CheckOptions, CheckResult, Verdict};

use crate::core_engine::{CoreOutcome, CoreRun, Prepared, Search};
use crate::core_native_graph::{NativeGraph, NativeSink};
use crate::core_native_values::NativeValues;

/// How a temporal run's checks are scheduled: `Some` for the default
/// profile's completed-level checks, `None` for the final check only
/// (`--compat=tlc`).
pub(crate) type Route = Option<Schedule>;

/// The refusal of a temporal validation failure, whose report Core does not
/// build (TLC's start-up and planning errors among them).
pub(crate) const VALIDATION_FAILURE: &str =
    "a temporal validation failure (core): its report is not implemented on Core";

fn refused(feature: impl Into<String>, options: &CheckOptions, ir: &Ir) -> CoreOutcome {
    CoreOutcome::Checked(Box::new(crate::core_engine::result(
        Verdict::Unsupported {
            feature: feature.into(),
        },
        options,
        ir,
    )))
}

/// Actual privately bound native and reference domains, never a source-name
/// projection. Prepared is constructed only from IR/config via fresh binding;
/// it accepts no external bound model, value provider or callable.
fn fixed_name_domain(prepared: &Prepared) -> bool {
    let model = &prepared.model;
    let strings = prepared.store.strings();
    let count = model.evaluator.startup_names();
    strings.is_sealed()
        && strings.order() == dive_core::strings::StringOrder::Token
        && strings.len() == count
        && model.names.len() == count
        && model.validate_context().is_ok()
        && (0..count).all(|i| {
            u32::try_from(i).is_ok_and(|token| {
                matches!((strings.text(token), model.names.resolve(token)),
                    (Some(text), Some(atom)) if text.as_ref() == atom.as_str())
            })
        })
}

/// Why Core does not run this temporal check before its search.
pub(crate) fn static_refusal(
    ir: &Arc<Ir>,
    config: &dive_config::ModelConfig,
    options: &CheckOptions,
    prepared: &Prepared,
) -> Option<String> {
    if let Some(reason) = dive_runtime::admission::route_exclusion(ir, config, options) {
        return Some(format!("temporal properties with {reason} (core)"));
    }
    let guarded_names =
        fixed_name_domain(prepared) && dive_runtime::capture::runtime_names_only(&prepared.model);
    if let Some(reason) = dive_runtime::capture::refusal(
        &prepared.model,
        options,
        options.workers > 1 && !guarded_names,
    ) {
        return Some(format!("temporal properties: {reason} (core)"));
    }
    if prepared.plan.effects.prints {
        return Some("Print or PrintT with temporal properties (core)".into());
    }
    if prepared.plan.effects.draws {
        return Some("RandomElement with temporal properties (core)".into());
    }
    if prepared.plan.effects.registers {
        return Some("TLCSet or TLCGet with temporal properties (core)".into());
    }
    None
}

/// Runs the temporal check of a prepared model on Core.
#[allow(
    clippy::too_many_lines,
    reason = "the stages of a temporal core run, in order"
)]
pub(crate) fn check(
    ir: &Arc<Ir>,
    config: &dive_config::ModelConfig,
    options: &CheckOptions,
    (profile_tlc, core_opts): (bool, dive_core::Opts),
    prepared: Prepared,
    route: Route,
    run: &mut CoreRun,
) -> CoreOutcome {
    check_configured(
        ir,
        config,
        options,
        (profile_tlc, core_opts),
        prepared,
        route,
        run,
        crate::temporal::core_file_configuration(),
    )
}

#[allow(
    clippy::too_many_arguments,
    reason = "same owned temporal-operation boundary"
)]
fn check_configured(
    ir: &Arc<Ir>,
    config: &dive_config::ModelConfig,
    options: &CheckOptions,
    profile: (bool, dive_core::Opts),
    prepared: Prepared,
    route: Route,
    run: &mut CoreRun,
    configuration: crate::temporal::Configuration,
) -> CoreOutcome {
    // Hold the exact reference Arc used by GraphCheck and its model's scopes.
    // It is append-only; no successful branch can hide an intervening new token.
    let names = Arc::clone(&prepared.model.names);
    let guarded = fixed_name_domain(&prepared);
    let count = names.len();
    let outcome = check_configured_inner(
        ir,
        config,
        options,
        profile,
        prepared,
        route,
        run,
        configuration,
    );
    guard_outcome(guarded, &names, count, outcome, options, ir)
}

fn guard_outcome(
    guarded: bool,
    names: &dive_value::Interner,
    count: usize,
    outcome: CoreOutcome,
    options: &CheckOptions,
    ir: &Ir,
) -> CoreOutcome {
    if !guarded || names.len() == count {
        return outcome;
    }
    match &outcome {
        CoreOutcome::TemporalFailure(_) => outcome,
        CoreOutcome::Checked(result)
            if matches!(
                result.verdict,
                Verdict::Stopped(_)
                    | Verdict::ResourceLimit { .. }
                    | Verdict::BudgetMeasurement { .. }
                    | Verdict::InternalError { .. }
            ) =>
        {
            outcome
        }
        _ => refused(
            "temporal reference evaluation created a name outside the proved startup domain (core)",
            options,
            ir,
        ),
    }
}

#[allow(
    clippy::too_many_arguments,
    clippy::too_many_lines,
    reason = "one producer route with injectable exact caller resource controls"
)]
fn check_configured_inner(
    ir: &Arc<Ir>,
    config: &dive_config::ModelConfig,
    options: &CheckOptions,
    (profile_tlc, core_opts): (bool, dive_core::Opts),
    prepared: Prepared,
    route: Route,
    run: &mut CoreRun,
    configuration: crate::temporal::Configuration,
) -> CoreOutcome {
    let started = Instant::now();
    if let Some(feature) = static_refusal(ir, config, options, &prepared) {
        return refused(feature, options, ir);
    }
    let Prepared {
        model,
        host: _,
        start,
        store,
        plan,
        compile_time,
    } = prepared;
    let mut graph_check = match GraphCheck::prepare(
        &model,
        options,
        configuration.limits,
        configuration.acceptance,
        configuration.graph_labels,
        route,
    ) {
        Ok(check) => check,
        Err(failure) => {
            return report_outcome(
                crate::temporal::core_native_report(
                    ir,
                    config,
                    options,
                    Err(*failure),
                    route.is_some(),
                    model.symmetry_liveness().clone(),
                    started,
                ),
                options,
                ir,
            );
        }
    };
    let workers = options.workers.max(1);
    let budget = &options.budget;
    let poll = || !matches!(budget.poll(), Ok(None));
    let explore_started = Instant::now();
    let control = match EvaluationControl::new(budget, Phase::Capture) {
        Ok(control) => control,
        Err(error) => return internal(&error.to_string(), options, ir),
    };
    let source_setup = match graph_check.source_rows_possible() {
        Ok(true) => Some(crate::core_native_graph::SourceRowSetup {
            ir: ir.clone(),
            config: config.clone(),
            options: options.clone(),
            limits: configuration.limits,
            graph_labels: configuration.graph_labels,
        }),
        Ok(false) => None,
        Err(error) => {
            let verdict = crate::core_native_graph::source_error(error);
            let safety = crate::core_engine::result(verdict.clone(), options, ir);
            return CoreOutcome::TemporalFailure(Box::new(crate::temporal::core_capture_stop(
                verdict,
                &safety,
                false,
                started,
                graph_check.periodic_checks(),
            )));
        }
    };
    let mut atoms = match crate::core_native_atoms::NativeAtoms::prepare(
        &model,
        &store,
        &graph_check,
        &control,
        core_opts,
    ) {
        Ok(atoms) => atoms,
        Err(verdict) => {
            let safety = crate::core_engine::result(verdict.clone(), options, ir);
            return CoreOutcome::TemporalFailure(Box::new(crate::temporal::core_capture_stop(
                verdict,
                &safety,
                false,
                started,
                graph_check.periodic_checks(),
            )));
        }
    };
    // With every atom native, values are read only for an actual fallback,
    // source-row check or report; admission waits for that read.
    let defer_admission = atoms.all_native();
    let mut on_level = |level: u64, graph: &NativeGraph, values: &mut NativeValues<'_>, stats| {
        // Final-only and not-yet-due levels do not scan any retained CSR.
        if !graph_check.native_level_due(stats) {
            return Ok(false);
        }
        let csr = CsrCapture::checked(graph.input(), graph.limits(), &control)?;
        let mut read = crate::core_native_atoms::Reader::new(graph, values, &mut atoms);
        Ok(graph_check.level_native_with_reader(level, &csr, stats, &mut read))
    };
    let mut sink = match NativeSink::new(
        &model,
        Arc::clone(&store),
        configuration.limits.capture,
        options,
        Some(&mut on_level),
    ) {
        Ok(sink) => sink,
        Err(verdict @ (Verdict::Stopped(_) | Verdict::BudgetMeasurement { .. })) => {
            let mut safety = crate::core_engine::result(verdict.clone(), options, ir);
            safety.symmetry_liveness = Some(model.symmetry_liveness().clone());
            return CoreOutcome::TemporalFailure(Box::new(crate::temporal::core_capture_stop(
                verdict,
                &safety,
                false,
                started,
                graph_check.periodic_checks(),
            )));
        }
        Err(Verdict::InternalError { message }) => return internal(&message, options, ir),
        Err(reason) => {
            return refused(
                format!("the behavior graph on the core stopped: {reason:?}"),
                options,
                ir,
            );
        }
    };
    if defer_admission {
        sink.defer_admission();
    }
    let exact = options.store == dive_store::StoreMode::Exact;
    if exact {
        sink.use_exact_identities();
    }
    if let Some(setup) = source_setup {
        sink.prepare_source_rows(setup);
    }
    run.searched = true;
    // Exact storage keeps full state identities, so equal 64-bit keys of
    // distinct states stay distinct vertices (ADR 0158).
    let mut out = dive_explore::explore_with_storage(
        &plan,
        &store,
        dive_explore::Config {
            opts: core_opts,
            // A ceiling stops capture before the state beyond it: the run
            // reports `stopped` in graph capture, never a holding result
            // (ADR 0158).
            max_states: options.max_states,
            max_depth: options.max_depth,
            workers,
            warn: options.eval.warnings,
            sample: None,
        },
        &poll,
        Some(&mut sink),
        Some(&crate::core_engine::search_work_control(budget)),
        &dive_explore::storage::Storage {
            exact,
            ..dive_explore::storage::Storage::default()
        },
    );
    let graph_error = sink.take_error();
    let source_error = sink.take_source_error();
    let source_rows = sink.take_source_rows();
    let (graph, mut values) = sink.finish();
    if let Some(message) = atoms.audit_mismatch() {
        return internal(message, options, ir);
    }
    run.metrics = crate::core_engine::metrics_json(&plan, &out.metrics, compile_time);
    let prefix = match out.verdict {
        dive_explore::Verdict::PrefixStop { level } => {
            // The successful committed-prefix factory checks its own actual
            // queue/shape facts; this cannot create completed EOF evidence.
            out.verdict = dive_explore::Verdict::Success;
            Some(level)
        }
        _ => None,
    };
    let graph_stopped = out.verdict == dive_explore::Verdict::GraphStop;
    if graph_stopped
        && matches!(
            &graph_error,
            Some(Verdict::Stopped(_) | Verdict::BudgetMeasurement { .. })
        )
    {
        // The sink's owned control actually observed this run's exhausted or
        // unmeasurable budget. Use the ordinary budget report path for real
        // counters/output/worker metadata, then retain its exact capture cause.
        out.verdict = dive_explore::Verdict::Budget;
    }
    let search = Search {
        ir,
        config,
        options,
        profile_tlc,
        workers,
        explore_started,
    };
    let producer_stats = out.stats;
    let source_trace = source_rows
        .as_ref()
        .filter(|rows| rows.has_error())
        .and(out.trace.clone());
    let mut safety = crate::core_engine::safety_result(&search, &model, &store, &plan, start, out);
    if let Some(rows) =
        source_rows.filter(dive_liveness::analyze::owned::SourceRowDriver::has_error)
    {
        if !matches!(&graph_error, Some(Verdict::Unsupported { feature }) if feature == crate::core_native_graph::SOURCE_ROW_STOP)
        {
            return internal("native source row lost its actual graph stop", options, ir);
        }
        let word = match source_trace {
            Some(Ok(trace)) => match crate::core_engine::counterexample(&model, &store, &trace) {
                Ok(word) => word,
                Err(error) => return internal(&error, options, ir),
            },
            Some(Err(error)) => return internal(&error, options, ir),
            None => {
                return internal(
                    "native source row has no reconstructed source word",
                    options,
                    ir,
                );
            }
        };
        safety.verdict = Verdict::Unsupported {
            feature: crate::core_native_graph::SOURCE_ROW_STOP.into(),
        };
        safety.stats = Some(dive_runtime::Stats {
            generated: producer_stats.generated,
            distinct: producer_stats.distinct,
            depth: producer_stats.depth,
            left_on_queue: producer_stats.left_on_queue,
            initial_generated: producer_stats.initial_generated,
        });
        safety.counterexample = Some(word);
        let state = match rows.finish(&safety) {
            Ok(Some(state)) => state,
            Ok(None) => return internal("native source row lost its sealed evidence", options, ir),
            Err(error) => return internal(&error.to_string(), options, ir),
        };
        let failure = crate::temporal::core_source_row_failure(
            ir,
            config,
            options,
            state,
            &safety,
            graph_check.periodic_checks(),
            started,
        );
        record_metrics(run, &graph, &values, &atoms);
        return CoreOutcome::TemporalFailure(Box::new(failure));
    }
    if let Some(error) = source_error {
        safety.verdict = error.clone();
        safety.stats = Some(dive_runtime::Stats {
            generated: producer_stats.generated,
            distinct: producer_stats.distinct,
            depth: producer_stats.depth,
            left_on_queue: producer_stats.left_on_queue,
            initial_generated: producer_stats.initial_generated,
        });
        let failure = crate::temporal::core_capture_stop(
            error,
            &safety,
            false,
            started,
            graph_check.periodic_checks(),
        );
        record_metrics(run, &graph, &values, &atoms);
        return CoreOutcome::TemporalFailure(Box::new(failure));
    }
    if matches!(safety.verdict, Verdict::BudgetMeasurement { .. }) {
        safety.stats = Some(dive_runtime::Stats {
            generated: producer_stats.generated,
            distinct: producer_stats.distinct,
            left_on_queue: producer_stats.left_on_queue,
            depth: producer_stats.depth,
            initial_generated: producer_stats.initial_generated,
        });
    }
    if graph_stopped {
        match graph_error {
            Some(verdict @ (Verdict::Stopped(_) | Verdict::BudgetMeasurement { .. })) => {
                safety.verdict = verdict;
            }
            Some(Verdict::InternalError { message }) => {
                record_metrics(run, &graph, &values, &atoms);
                return internal(&message, options, ir);
            }
            reason => {
                record_metrics(run, &graph, &values, &atoms);
                let reason = reason.map_or_else(|| "no reason".to_owned(), |v| format!("{v:?}"));
                return refused(
                    format!("the behavior graph on the core stopped: {reason}"),
                    options,
                    ir,
                );
            }
        }
    }
    if let Verdict::Stopped(stopped) = &mut safety.verdict {
        stopped.phase = Phase::Capture;
    }
    dive_runtime::capture::refuse_unproved_temporal_next_error(&model, &mut safety);
    if safety.verdict != Verdict::Success {
        record_metrics(run, &graph, &values, &atoms);
        safety.symmetry_liveness = Some(model.symmetry_liveness().clone());
        if matches!(
            safety.verdict,
            Verdict::Stopped(_) | Verdict::BudgetMeasurement { .. }
        ) {
            return CoreOutcome::TemporalFailure(Box::new(crate::temporal::core_capture_stop(
                safety.verdict.clone(),
                &safety,
                false,
                started,
                graph_check.periodic_checks(),
            )));
        }
        // A safety ending, a stop or a refusal: reported as a safety
        // result, never a temporal verdict, with
        // the bound model's symmetry resolution.
        safety.symmetry_liveness = Some(model.symmetry_liveness().clone());
        return CoreOutcome::Checked(Box::new(safety));
    }
    let captured_safety = safety.clone();
    let checks = graph_check.periodic_checks();
    let analysis = finish(
        &graph,
        &mut values,
        (graph_check, &mut atoms),
        safety,
        prefix,
        options,
        &control,
    );
    record_metrics(run, &graph, &values, &atoms);
    if let Some(message) = atoms.audit_mismatch() {
        return internal(message, options, ir);
    }
    let analysis = match analysis {
        Ok(a) => a,
        Err(verdict @ (Verdict::Stopped(_) | Verdict::BudgetMeasurement { .. })) => {
            return CoreOutcome::TemporalFailure(Box::new(crate::temporal::core_capture_stop(
                verdict,
                &captured_safety,
                prefix.is_none(),
                started,
                checks,
            )));
        }
        Err(Verdict::InternalError { message }) => return internal(&message, options, ir),
        Err(v) => {
            return refused(
                format!("the behavior graph on the core: {v:?}"),
                options,
                ir,
            );
        }
    };
    if analysis.as_ref().is_err_and(|failure| {
        !matches!(
            failure.failure.failure().error,
            dive_liveness::analyze::AnalysisError::Stopped(_)
                | dive_liveness::analyze::AnalysisError::BudgetMeasurement(_)
                | dive_liveness::analyze::AnalysisError::BudgetStartup(_)
        )
    }) {
        return refused(
            "a temporal failure (core): its report is not implemented on Core",
            options,
            ir,
        );
    }
    // Analysis owns only the selected converted word and sealed facts now.
    // Release the native capture/store before fresh independent rebinding.
    drop(atoms);
    drop(values);
    drop(graph);
    drop(plan);
    drop(store);
    report_outcome(
        crate::temporal::core_native_report(
            ir,
            config,
            options,
            analysis,
            route.is_some(),
            model.symmetry_liveness().clone(),
            started,
        ),
        options,
        ir,
    )
}

fn report_outcome(
    result: Result<crate::temporal::Report, crate::temporal::Failure>,
    options: &CheckOptions,
    ir: &Ir,
) -> CoreOutcome {
    match result {
        Ok(report) => match report.outcome() {
            crate::temporal::Outcome::Safety(result) => CoreOutcome::Checked(result.clone()),
            _ => CoreOutcome::Temporal(Box::new(report)),
        },
        Err(failure)
            if matches!(
                failure.error(),
                crate::temporal::Error::Analysis(
                    dive_liveness::analyze::AnalysisError::Stopped(_)
                        | dive_liveness::analyze::AnalysisError::BudgetMeasurement(_)
                        | dive_liveness::analyze::AnalysisError::BudgetStartup(_)
                )
            ) =>
        {
            CoreOutcome::TemporalFailure(Box::new(failure))
        }
        Err(_) => refused(VALIDATION_FAILURE, options, ir),
    }
}

type Analysis = Result<
    (NativeAnalysis, dive_liveness::analyze::periodic::Checks),
    dive_liveness::analyze::periodic::PeriodicFailure,
>;

/// Completes the graph and runs the final check, or finishes the prefix
/// the level checks stopped at.
fn finish(
    graph: &NativeGraph,
    values: &mut NativeValues<'_>,
    driver: (GraphCheck<'_>, &mut crate::core_native_atoms::NativeAtoms),
    safety: CheckResult,
    prefix: Option<u64>,
    options: &CheckOptions,
    control: &EvaluationControl,
) -> Result<Analysis, Verdict> {
    let (mut graph_check, atoms) = driver;
    if prefix.is_some()
        && let Some(failure) = graph_check.take_native_prefix_failure(&safety)
    {
        return Ok(Err(*failure));
    }
    let mut read = crate::core_native_atoms::Reader::new(graph, values, atoms);
    if let Some(level) = prefix {
        let mut read =
            |row, control: &EvaluationControl| read.values.read_retained(graph, row, control);
        let prefix = CsrPrefix::from_producer(
            graph.input(),
            level,
            safety,
            options.clone(),
            graph.limits(),
            control,
        )?;
        Ok(graph_check
            .finish_native_prefix(prefix, &mut read)
            .map_err(|f| *f))
    } else {
        let complete = CsrComplete::from_producer(
            graph.input(),
            safety,
            options.clone(),
            graph.limits(),
            control,
        )?;
        Ok(graph_check
            .finish_native_with_reader(complete, &mut read)
            .map_err(|f| *f))
    }
}

fn record_metrics(
    run: &mut CoreRun,
    graph: &NativeGraph,
    values: &NativeValues<'_>,
    atoms: &crate::core_native_atoms::NativeAtoms,
) {
    if let Some(metrics) = run.metrics.as_object_mut() {
        let memory = graph.memory();
        let conversion = values.metrics();
        metrics.insert(
            "liveness".into(),
            serde_json::json!({
                "storage": "native-csr", "graph_states": graph.state_count(),
                "retained_rows": graph.input().retained_rows,
                "convert_ms": conversion["convert_ms"],
                "converted_nodes": conversion["converted_values"],
                "memo_entries": conversion["cache_entries"],
                "values": conversion, "atoms": atoms.metrics(),
                "memory": {
                    "fixed_bytes": memory.fixed_bytes,
                    "buffer_bytes": memory.buffer_bytes,
                    "growth_overlap_upper_bound_bytes": memory.growth_overlap_upper_bound_bytes,
                    "capture_key_entries": memory.key_entries,
                    "capture_key_capacity": memory.key_capacity,
                    "key_index_retained_after_capture": false,
                    "accounting_complete": false,
                    "scope": "native collector buffers and action-label capacities; reserve overlap is an upper bound, not observed RSS; capture key index released before final analysis; opaque hash/allocator/Arc storage, Store, explorer frontiers/waves and transient conversion/analysis storage require separate accounting; no capacity or peak-memory claim",
                },
            }),
        );
    }
}

fn internal(message: &str, options: &CheckOptions, ir: &Ir) -> CoreOutcome {
    CoreOutcome::Checked(Box::new(crate::core_engine::result(
        Verdict::InternalError {
            message: format!("core temporal graph: {message}"),
        },
        options,
        ir,
    )))
}

#[cfg(test)]
#[path = "core_temporal_resource_tests.rs"]
mod resource_tests;

#[cfg(test)]
#[path = "core_temporal_name_tests.rs"]
mod name_tests;