dive-trace 0.1.0

TLA+ counterexample trace format and independent 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
//! Actual first-Init receiver reconstruction, separate from named-cause validity.

use std::sync::Arc;

use dive_eval::{Ec, ErrorClass, EvalError, HostControl, St, ValueMethod};
use dive_model::Model;

use crate::ordered::{Driver, Event, Site};
use crate::{Check, Failure, State, Validation};

/// Independently reconstructed initial-invariant exception identities.
///
/// Only a fresh [`crate::Session::initial_invariant_class`] can construct this
/// result. A valid named error word alone proves neither identity. This is
/// execution evidence, never a serialized field or a producer certificate.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InitialInvariantClass {
    reported: u32,
    deciding: u32,
}

impl InitialInvariantClass {
    /// Initial-state report produced by the actual ordinary invariant failure.
    #[must_use]
    pub const fn reported_code(self) -> u32 {
        self.reported
    }

    /// Wrapper established by actual diagnostic receiver reconstruction.
    #[must_use]
    pub const fn deciding_code(self) -> u32 {
        self.deciding
    }
}

/// Actual independently reached initial-error lifecycle, before header matching.
/// A receiver stage requires its separate named-word class proof. The two
/// no-word stages include actual matching diagnostic receiver reconstruction.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum InitialErrorStage {
    /// Init fails before emitting any complete candidate.
    Generation,
    /// Its first emitted state reaches a tuple/record receiver invariant error.
    InvariantReceiver,
    /// The existing function-materialization `EvalException` invariant route.
    LegacyInvariant,
}

#[allow(
    clippy::too_many_lines,
    reason = "first-event routing retains both actual no-word diagnostic lifecycles"
)]
pub(crate) fn stage(
    model: &Model,
    host: &crate::validate::TraceHost,
    control: Option<&dyn HostControl>,
) -> Result<(Validation, Option<InitialErrorStage>), EvalError> {
    let empty = Arc::from(vec![None; model.ir.vars().len()]);
    let driver = Driver {
        initial: true,
        model,
        source: &empty,
        level: 0,
        host,
        control,
    };
    if let Some(refusal) = crate::next_error::preflight(model, true, control)? {
        return Ok((refusal, None));
    }
    let (event, emitted) = driver.primary_with_count(false)?;
    driver.poll()?;
    let reached = match event {
        Event::Error {
            site: Site::Init,
            error,
            ..
        } if emitted == 0 && error.value_method().is_some() => {
            let mut yielded = false;
            let diagnostic =
                model
                    .evaluator
                    .init_states_diagnostic(&model.init_actions, &mut |_| {
                        yielded = true;
                        dive_eval::next::Flow::Stop
                    });
            driver.poll()?;
            let secondary = match diagnostic {
                Err(failure)
                    if matches!(
                        failure.error.class(),
                        ErrorClass::Interrupted | ErrorClass::ResourceLimit | ErrorClass::Bug
                    ) =>
                {
                    return Err(failure.error);
                }
                Err(failure) if !yielded && failure.emitted == 0 => failure.error,
                _ => {
                    return Ok((
                        Validation::Unsupported(
                            "diagnostic Init does not reproduce its pre-emission error".into(),
                        ),
                        None,
                    ));
                }
            };
            if !same_receiver(&error, &secondary) {
                if matches!(
                    secondary.class(),
                    ErrorClass::Interrupted | ErrorClass::ResourceLimit | ErrorClass::Bug
                ) {
                    return Err(secondary);
                }
                return Ok((
                    Validation::Unsupported("diagnostic Init receiver class is unproved".into()),
                    None,
                ));
            }
            InitialErrorStage::Generation
        }
        Event::Error {
            site: Site::Invariant,
            error,
            candidate: Some(candidate),
            ..
        } if emitted == 1 => match error
            .value_method()
            .map(dive_eval::ValueMethodFailure::method)
        {
            Some(ValueMethod::TupleEquals | ValueMethod::RecordEquals)
                if error.ec() == Ec::General && !error.is_eval_exception() =>
            {
                InitialErrorStage::InvariantReceiver
            }
            Some(ValueMethod::FunctionMaterialize) if error.is_eval_exception() => {
                let Some(first) = model.invariants.first() else {
                    return Ok((
                        Validation::Unsupported("legacy initial invariant is missing".into()),
                        None,
                    ));
                };
                if !model.constraints.is_empty()
                    || model
                        .ir
                        .node(first.pred.expr)
                        .effects
                        .intersects(dive_ir::Effects::IMPURE)
                    || model.evaluator.scope_has_effects(&first.pred.scope)
                    || !closed_state(model, &candidate, &driver)?
                {
                    return Ok((
                        Validation::Unsupported(
                            "legacy initial invariant history is unproved".into(),
                        ),
                        None,
                    ));
                }
                host.set_replay_level(1);
                let Err(secondary) = model.eval(&first.pred, &St::state(candidate).diagnostic())
                else {
                    return Ok((
                        Validation::Unsupported("legacy diagnostic invariant completes".into()),
                        None,
                    ));
                };
                if !same_receiver(&error, &secondary) {
                    if matches!(
                        secondary.class(),
                        ErrorClass::Interrupted | ErrorClass::ResourceLimit | ErrorClass::Bug
                    ) {
                        return Err(secondary);
                    }
                    return Ok((
                        Validation::Unsupported(
                            "legacy initial invariant diagnostic class is unproved".into(),
                        ),
                        None,
                    ));
                }
                InitialErrorStage::LegacyInvariant
            }
            _ => {
                return Ok((
                    Validation::Unsupported("unproved initial receiver lifecycle".into()),
                    None,
                ));
            }
        },
        Event::Refused(reason) => return Ok((Validation::Unsupported(reason), None)),
        _ => {
            return Ok((
                Validation::Unsupported(
                    "source does not establish a bounded first initial error".into(),
                ),
                None,
            ));
        }
    };
    Ok((Validation::Valid, Some(reached)))
}

#[allow(
    clippy::too_many_lines,
    reason = "one actual primary/diagnostic receiver lifecycle retains private history"
)]
pub(crate) fn replay(
    model: &Model,
    state: &State,
    name: &str,
    ec: &str,
    host: &crate::validate::TraceHost,
    control: Option<&dyn HostControl>,
) -> Result<(Validation, Option<InitialInvariantClass>), EvalError> {
    let driver = Driver {
        initial: true,
        model,
        source: state,
        level: 0,
        host,
        control,
    };
    driver.poll()?;
    if let Some(refusal) = crate::next_error::preflight(model, true, control)? {
        return Ok((refusal, None));
    }
    let Some(first) = model.invariants.first().filter(|first| first.name == name) else {
        return Ok(refused(
            "initial receiver replay requires the first configured invariant",
        ));
    };
    if !model.constraints.is_empty()
        || model
            .ir
            .node(first.pred.expr)
            .effects
            .intersects(dive_ir::Effects::IMPURE)
        || model.evaluator.scope_has_effects(&first.pred.scope)
    {
        return Ok(refused(
            "initial receiver replay with constraints or invariant effects",
        ));
    }
    let (primary, original) = match driver.primary(false)? {
        Event::Error {
            site: Site::Invariant,
            error,
            candidate: Some(candidate),
            ..
        } => (error, candidate),
        Event::Refused(reason) => return Ok((Validation::Unsupported(reason), None)),
        Event::Failed { check, detail } => {
            return Ok((
                Validation::Invalid(Failure {
                    state: 0,
                    check,
                    detail,
                }),
                None,
            ));
        }
        _ => {
            return Ok(refused(
                "the first Init operation does not fail at the claimed invariant",
            ));
        }
    };
    if !closed_state(model, &original, &driver)? || original != *state {
        return Ok(refused(
            "the first actual Init emission differs or retains unproved value history",
        ));
    }
    if primary.class() != ErrorClass::Eval || primary.ec().name() != ec {
        return Ok((
            invalid("the actual first invariant has a different underlying cause"),
            None,
        ));
    }
    let Some(_receiver) = primary.value_method().filter(|receiver| {
        matches!(
            receiver.method(),
            ValueMethod::TupleEquals | ValueMethod::RecordEquals
        )
    }) else {
        return Ok(refused(
            "the initial invariant has no certified tuple/record receiver catch",
        ));
    };
    // The sealed value-layer catch identifies this constructor independently
    // of the optional generic tool/module exception marker. Initial reporting
    // uses the actual EvalException bit; it must take the recorded-state arm.
    if primary.ec() != Ec::General || primary.is_eval_exception() {
        return Ok(refused(
            "the original initial receiver exception constructor is unproved",
        ));
    }
    // Reuse this privately reconstructed primary runtime: constants/caches
    // retain their actual lifetime, while the diagnostic Init generator tags
    // only newly constructed receivers. No witness metadata supplies sources.
    host.set_replay_level(0);
    let mut diagnostic = None;
    let mut stopped = None;
    let generated = model
        .evaluator
        .init_states_diagnostic(&model.init_actions, &mut |candidate| {
            let attempt = (|| {
                driver.poll()?;
                if candidate.ordinal != 0
                    || !candidate.complete()
                    || !closed_state(model, candidate.state, &driver)?
                    || candidate.state != original.as_ref()
                {
                    return Ok(None);
                }
                host.set_replay_level(1);
                let context = St::state(Arc::from(candidate.state)).diagnostic();
                match model.eval(&first.pred, &context) {
                    Err(error)
                        if matches!(
                            error.class(),
                            ErrorClass::Interrupted | ErrorClass::ResourceLimit | ErrorClass::Bug
                        ) =>
                    {
                        Err(error)
                    }
                    Err(error) => Ok(Some(error)),
                    Ok(_) => Ok(None),
                }
            })();
            match attempt {
                Ok(error) => diagnostic = error,
                Err(error) => stopped = Some(error),
            }
            dive_eval::next::Flow::Stop
        });
    driver.poll()?;
    if let Some(error) = stopped {
        return Err(error);
    }
    if let Err(failure) = generated {
        return if matches!(
            failure.error.class(),
            ErrorClass::Interrupted | ErrorClass::ResourceLimit | ErrorClass::Bug
        ) {
            Err(failure.error)
        } else {
            Ok(refused(
                "diagnostic Init fails before the original invariant receiver",
            ))
        };
    }
    let Some(diagnostic) = diagnostic else {
        return Ok(refused(
            "diagnostic Init cannot reconstruct the first invariant failure",
        ));
    };
    if !same_receiver(&primary, &diagnostic) {
        return Ok(refused(
            "diagnostic receiver construction/source lifetime is not certified",
        ));
    }
    driver.poll()?;
    Ok((
        Validation::Valid,
        Some(InitialInvariantClass {
            reported: 2102,
            deciding: 2147,
        }),
    ))
}

fn same_receiver(primary: &EvalError, diagnostic: &EvalError) -> bool {
    primary.class() == ErrorClass::Eval
        && diagnostic.class() == ErrorClass::Eval
        && primary.ec() == diagnostic.ec()
        && primary.is_eval_exception() == diagnostic.is_eval_exception()
        && primary.exception_kind() == diagnostic.exception_kind()
        && primary.origin().is_some()
        && primary.origin() == diagnostic.origin()
        && primary.value_method().is_some_and(|receiver| {
            diagnostic.value_method().is_some_and(|actual| {
                actual.method() == receiver.method()
                    && actual.constructor() == receiver.constructor()
                    && actual.source().is_some()
            })
        })
}

fn closed_state(
    model: &Model,
    state: &[Option<dive_value::Value>],
    driver: &Driver<'_>,
) -> Result<bool, EvalError> {
    let mut stopped = None;
    let mut proceed = || match driver.poll() {
        Ok(()) => true,
        Err(error) => {
            stopped = Some(error);
            false
        }
    };
    let closed = state.iter().all(|value| {
        value.as_ref().is_some_and(|value| {
            value
                .closed_function_cache_heap_bytes(&model.names, &mut proceed)
                .is_some()
        })
    });
    if let Some(error) = stopped {
        Err(error)
    } else {
        Ok(closed)
    }
}

fn refused(reason: &str) -> (Validation, Option<InitialInvariantClass>) {
    (Validation::Unsupported(reason.into()), None)
}

fn invalid(detail: &str) -> Validation {
    Validation::Invalid(Failure {
        state: 0,
        check: Check::Ending,
        detail: detail.into(),
    })
}