dive-explore 0.1.0

Breadth-first state-space exploration over compiled TLA+ plans
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
//! Opt-in native safety storage. Exact identities are separate from receiver history.

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

use dive_core::frontier_codec::FrontierCodec;
use dive_core::{Error, Handle, Heap};
use dive_store::spill::{MAX_RECORD_BYTES, OrderedFrontier, SpillError};

/// Storage policies for one native safety search. These do not bound total memory.
#[derive(Clone, Debug, Default)]
pub struct Storage {
    /// Resolve state-key collisions using complete mathematical state identities.
    pub exact: bool,
    /// Resident predecessor-log capacity before transient spill.
    pub predecessor_memory_bytes: Option<usize>,
    /// Parent directory for private predecessor files.
    pub predecessor_directory: Option<PathBuf>,
    /// Resident encoded capacity, independently for current and next frontiers.
    pub frontier_memory_bytes: Option<usize>,
    /// Existing parent directory for private frontier files.
    pub frontier_directory: Option<PathBuf>,
    /// Resident encoded candidate bytes per worker; zero immediately spills.
    /// Accepted Store values and active evaluator roots are outside this budget.
    pub candidate_memory_bytes: Option<usize>,
    /// Parent directory for private transient candidate files.
    pub candidate_directory: Option<PathBuf>,
    /// Completed-level trigger for rebuilding the Store from live roots.
    /// Zero forces every boundary. This is not a hard memory limit.
    pub store_compact_bytes: Option<usize>,
    /// The trigger is the caller's default rather than an explicit request.
    /// Reclamation that cannot run on this search is then skipped and recorded
    /// instead of refusing the search; cancellation still stops it.
    pub store_compact_default: bool,
    /// Opt-in fallible disk membership; runs inline and does not bound values.
    pub seen_disk: Option<DiskOptions>,
    /// Test-only: replaces every state key with this value, so every state
    /// collides and only exact identities can tell states apart.
    #[cfg(test)]
    pub collide_keys: Option<u64>,
}

/// Bounds and owned directory for native disk membership.
#[derive(Clone, Debug, Default)]
pub struct DiskOptions {
    /// Existing disk-key workspace, entry and probe bounds.
    pub limits: dive_store::seen::Limits,
    /// Existing parent for private transient files; defaults to system temporary storage.
    pub directory: Option<PathBuf>,
}

/// Ordered position at which disk membership first failed.
#[derive(Clone, Debug)]
pub struct DiskFailure {
    /// Membership, insertion, replay or cleanup phase.
    pub phase: &'static str,
    /// First library failure, including a later cleanup failure.
    pub error: dive_store::seen::SeenError,
    /// Generated count at the original operation.
    pub generated: u64,
    /// Concrete source discovery ID, absent for Init.
    pub predecessor: Option<u64>,
    /// Emission ordinal within the source's ordered successors.
    pub successor_ordinal: u64,
}

/// Actual disk-key storage facts. None of these measure retained state values.
#[derive(Clone, Debug)]
pub struct DiskUsage {
    /// Resolved key and index bounds.
    pub limits: dive_store::seen::Limits,
    /// Successfully committed physical keys, including error-replay admissions.
    pub admitted_keys: u64,
    /// Facts before explicit cleanup.
    pub before_cleanup: Option<dive_store::seen::Stats>,
    /// Facts after cleanup, even when an original cause remains authoritative.
    pub after_cleanup: Option<dive_store::seen::Stats>,
    /// First failed operation and its original ordered position.
    pub first_failure: Option<DiskFailure>,
    /// Search event reached before a later audit or cleanup failure.
    pub deciding_event_before_failure: Option<crate::Verdict>,
    /// A complete retained-record audit finished under the actual run owner.
    pub audit_completed: bool,
}

pub(crate) struct Disk {
    set: dive_store::seen::DiskSeen,
    pub(crate) usage: DiskUsage,
}

impl Disk {
    pub(crate) fn new(
        exact: bool,
        options: &DiskOptions,
    ) -> Result<Self, dive_store::seen::SeenError> {
        let mode = if exact {
            dive_store::StoreMode::Exact
        } else {
            dive_store::StoreMode::Fingerprint
        };
        let parent = options.directory.clone().unwrap_or_else(std::env::temp_dir);
        Ok(Self {
            set: dive_store::seen::DiskSeen::create(mode, &parent, options.limits)?,
            usage: DiskUsage {
                limits: options.limits,
                admitted_keys: 0,
                before_cleanup: None,
                after_cleanup: None,
                first_failure: None,
                deciding_event_before_failure: None,
                audit_completed: false,
            },
        })
    }

    #[allow(
        clippy::too_many_arguments,
        reason = "one positioned fallible membership operation over an actual canonical row"
    )]
    pub(crate) fn operation(
        &mut self,
        exact: bool,
        key: u64,
        row: &[Handle],
        heap: &Heap,
        insert: bool,
        position: (&'static str, u64, Option<u64>, u64),
        poll: &(dyn Fn() -> bool + Sync),
    ) -> Result<bool, Error> {
        use dive_core::key::KeyError;
        use dive_store::seen::{InsertOutcome, Key, Operation, SeenError};
        let bytes = if exact {
            match dive_core::key::canonical(heap, row, self.usage.limits.key_bytes, &|| !poll()) {
                Ok(bytes) => bytes,
                Err(KeyError::Interrupted) => return Err(Error::stop()),
                Err(KeyError::Unsupported(feature)) => return Err(Error::unsupported(feature)),
                Err(KeyError::Internal(message)) => return Err(Error::internal(message)),
                Err(KeyError::Limit(message)) => {
                    let error = SeenError {
                        operation: Operation::Admission,
                        message,
                        cleanup_failure: None,
                    };
                    self.record(position, error.clone());
                    return Err(Error::internal(error.to_string()));
                }
            }
        } else {
            Vec::new()
        };
        let key = if exact {
            Key::Exact(&bytes)
        } else {
            Key::Fingerprint(super::Seen::fix(key))
        };
        let result = if insert {
            self.set
                .insert(key)
                .map(|outcome| outcome == InsertOutcome::Inserted)
        } else {
            self.set.contains(key)
        };
        self.usage.admitted_keys = self.set.stats().physical_entries;
        result.map_err(|error| {
            self.record(position, error.clone());
            Error::internal(error.to_string())
        })
    }

    fn record(
        &mut self,
        position: (&'static str, u64, Option<u64>, u64),
        error: dive_store::seen::SeenError,
    ) {
        self.usage.first_failure.get_or_insert(DiskFailure {
            phase: position.0,
            error,
            generated: position.1,
            predecessor: position.2,
            successor_ordinal: position.3,
        });
    }

    pub(crate) fn finish(
        &mut self,
        validate: bool,
        poll: &(dyn Fn() -> bool + Sync),
        deciding: crate::Verdict,
    ) -> bool {
        self.usage.before_cleanup = Some(self.set.stats());
        let result = self.set.close_controlled(validate, &|| !poll());
        self.usage.after_cleanup = Some(self.set.stats());
        match result {
            Ok((_, complete)) => {
                self.usage.audit_completed = complete;
                !validate || complete
            }
            Err(error) => {
                self.usage.deciding_event_before_failure = Some(deciding);
                if let Some(first) = &mut self.usage.first_failure {
                    if first.error.operation == error.operation
                        && first.error.message == error.message
                    {
                        first.error.cleanup_failure = first
                            .error
                            .cleanup_failure
                            .clone()
                            .or(error.cleanup_failure);
                    } else {
                        first
                            .error
                            .cleanup_failure
                            .get_or_insert_with(|| error.to_string());
                    }
                } else {
                    self.record(("Cleanup", 0, None, 0), error);
                }
                false
            }
        }
    }
}

/// Actual native safety storage facts. These describe components, not RSS.
#[derive(Clone, Debug, Default)]
pub struct Usage {
    /// Completed-level persistent-value reclamation, independently of RSS.
    pub reclamation: ReclamationUsage,
    /// Encoded candidate payload and occurrence storage, not total memory.
    pub candidates: CandidateUsage,
    /// Opt-in disk membership and its first positioned failure.
    pub seen: Option<DiskUsage>,
    /// Exact mathematical identities retained by the collision index.
    pub exact_states: usize,
    /// Predecessor storage before explicit cleanup.
    pub predecessor: dive_store::predecessor::Usage,
    /// Actual predecessor storage failures.
    pub predecessor_failures: Vec<String>,
    /// Search event reached before predecessor storage prevented certification.
    pub predecessor_deciding_event: Option<String>,
    /// Explicit predecessor cleanup finished successfully.
    pub predecessor_cleanup_completed: bool,
    /// Actual encoded frontier component facts; decoded chunks and Store excluded.
    pub frontier: FrontierUsage,
}

/// Persistent-value reclamation facts; live roots and fixed startup remain mandatory.
#[derive(Clone, Debug, Default)]
pub struct ReclamationUsage {
    /// Actual reclamation/key workspace allocation failures.
    pub failures: Vec<String>,
    /// Requested measured-Store-byte trigger; zero forces every boundary.
    pub trigger_bytes: Option<usize>,
    /// The trigger came from the default policy rather than a request.
    pub default_trigger: bool,
    /// Why a default trigger stopped reclaiming; the search continued with the
    /// resident Store unchanged.
    pub skipped: Option<String>,
    /// Completed replacement transactions.
    pub collections: u64,
    /// Cumulative values released from replaced Stores.
    pub reclaimed_nodes: u64,
    /// Largest measured Store snapshot before a completed transaction.
    pub peak_before_bytes: u64,
    /// Largest measured Store snapshot after a completed transaction.
    pub peak_after_bytes: u64,
    /// Largest number of nodes retained after a completed transaction.
    pub peak_after_nodes: u64,
}

/// Actual candidate queue accounting, including occurrence metadata.
#[derive(Clone, Debug, Default)]
pub struct CandidateUsage {
    /// Requested encoded resident bytes per worker's pending source queue.
    pub memory_bytes_per_worker: Option<usize>,
    /// Peak aggregate resident encoded queue capacity across workers.
    pub peak_retained_bytes: u64,
    /// Current resident encoded queue capacity after cleanup.
    pub retained_bytes: u64,
    /// Peak aggregate retained codec constructor-origin index capacity.
    pub peak_origin_index_bytes: u64,
    /// Current retained codec origin-index capacity after cleanup.
    pub origin_index_bytes: u64,
    /// Confirmed complete transient data and index bytes written.
    pub spilled_bytes: u64,
    /// Actual encoding, filesystem, decoding and cleanup failures.
    pub failures: Vec<String>,
    /// Search result reached before a storage failure prevented certification.
    pub deciding_event_before_failure: Option<String>,
}

/// Encoded frontier accounting. This is neither RSS nor a total memory budget.
#[derive(Clone, Debug, Default)]
pub struct FrontierUsage {
    /// Requested resident encoded bytes per queue.
    pub memory_bytes_per_queue: Option<usize>,
    /// Peak combined current/next resident encoded capacities.
    pub peak_retained_bytes: u64,
    /// Resident encoded bytes after explicit cleanup.
    pub retained_bytes: u64,
    /// Confirmed complete writes; failed writes can leave additional partial bytes.
    pub spilled_bytes: u64,
    /// Actual codec, read, write, validation or cleanup failures.
    pub failures: Vec<String>,
    /// Search event reached before storage prevented certification.
    pub deciding_event_before_failure: Option<String>,
}

#[derive(Debug, Default)]
pub(crate) struct Chunk {
    pub(crate) identity: Vec<Handle>,
    pub(crate) original: Vec<Handle>,
    pub(crate) ids: Vec<u64>,
}

/// Two ordered encoded queues sharing a fixed live Store/codec owner.
pub(crate) struct Frontiers {
    codec: FrontierCodec,
    variables: usize,
    memory_bytes: usize,
    directory: PathBuf,
    next: OrderedFrontier,
    current: Option<OrderedFrontier>,
    next_first: Option<u64>,
    current_first: Option<u64>,
    pub(crate) usage: FrontierUsage,
}

impl Frontiers {
    pub(crate) fn new(
        store: &Arc<dive_core::Store>,
        variables: usize,
        options: &Storage,
    ) -> Result<Self, Error> {
        let memory_bytes = options.frontier_memory_bytes.unwrap_or(0);
        if variables
            .checked_mul(16)
            .and_then(|n| n.checked_add(24))
            .is_none_or(|n| n > MAX_RECORD_BYTES)
        {
            return Err(Error::unsupported(
                "native frontier row exceeds the encoded record limit",
            ));
        }
        let directory = options
            .frontier_directory
            .clone()
            .unwrap_or_else(std::env::temp_dir);
        Ok(Self {
            codec: FrontierCodec::new(Arc::clone(store), variables)?,
            variables,
            memory_bytes,
            next: OrderedFrontier::new(memory_bytes, &directory),
            directory,
            current: None,
            next_first: None,
            current_first: None,
            usage: FrontierUsage {
                memory_bytes_per_queue: Some(memory_bytes),
                ..FrontierUsage::default()
            },
        })
    }

    pub(crate) fn next_len(&self) -> usize {
        self.next.len()
    }

    pub(crate) fn current_len(&self) -> usize {
        self.current.as_ref().map_or(0, OrderedFrontier::len)
    }

    /// Streams the live next queue into a fresh Store-bound codec. Old sources
    /// are audited and closed before their owner is released; readers remain
    /// bounded to one paired row independently of the full frontier width.
    pub(crate) fn remap(
        &mut self,
        remap: &mut dive_core::store::compact::Remap,
        poll: &(dyn Fn() -> bool + Sync),
    ) -> Result<Self, Error> {
        let mut fresh = Self::new(
            remap.store(),
            self.variables,
            &Storage {
                frontier_memory_bytes: Some(self.memory_bytes),
                frontier_directory: Some(self.directory.clone()),
                ..Storage::default()
            },
        )?;
        let result = (|| {
            for i in 0..self.next.len() {
                if i.is_multiple_of(128) && poll() {
                    return Err(Error::stop());
                }
                let read = self.next.read(i);
                let (id, bytes) = self.observe(read)?;
                if self.next_first.and_then(|n| n.checked_add(i as u64)) != Some(id) {
                    return self.observe(Err(frontier_error("remap discovery position")));
                }
                let read = self.codec.decode(&bytes).map_err(frontier_error);
                let mut rows = self.observe(read)?;
                for h in rows.identity.iter_mut().chain(&mut rows.original) {
                    *h = remap.copy(*h, &|| !poll())?;
                }
                fresh.push(id, &rows.identity, &rows.original)?;
            }
            if !self.finish(true, poll) {
                if self.usage.failures.is_empty() {
                    return Err(Error::stop());
                }
                return Err(Error::internal(self.usage.failures.join("; ")));
            }
            Ok(())
        })();
        if let Err(error) = result {
            fresh.finish(false, poll);
            self.usage.spilled_bytes += fresh.usage.spilled_bytes;
            self.usage.peak_retained_bytes = self
                .usage
                .peak_retained_bytes
                .max(fresh.usage.peak_retained_bytes);
            self.usage.failures.extend(fresh.usage.failures);
            return Err(error);
        }
        fresh.usage.spilled_bytes += self.usage.spilled_bytes;
        fresh.usage.peak_retained_bytes = fresh
            .usage
            .peak_retained_bytes
            .max(self.usage.peak_retained_bytes);
        fresh.usage.failures.append(&mut self.usage.failures);
        Ok(fresh)
    }

    fn observe<T>(&mut self, result: Result<T, SpillError>) -> Result<T, Error> {
        result.map_err(|error| {
            self.usage.failures.push(error.to_string());
            Error::internal(error.to_string())
        })
    }

    fn refresh(&mut self) {
        let bytes = self.next.stats().retained_bytes.saturating_add(
            self.current
                .as_ref()
                .map_or(0, |q| q.stats().retained_bytes),
        );
        self.usage.retained_bytes = bytes;
        self.usage.peak_retained_bytes = self.usage.peak_retained_bytes.max(bytes);
    }

    pub(crate) fn push(
        &mut self,
        id: u64,
        identity: &[Handle],
        original: &[Handle],
    ) -> Result<(), Error> {
        let before = self.next.stats().spilled_bytes;
        let result = (|| {
            if self
                .next_first
                .is_some_and(|first| first.checked_add(self.next.len() as u64) != Some(id))
            {
                return Err(frontier_error("discovery IDs are not consecutive"));
            }
            let bytes = self
                .codec
                .encode(identity, original)
                .map_err(frontier_error)?;
            self.next.push(id, &bytes)
        })();
        self.next_first.get_or_insert(id);
        self.usage.spilled_bytes = self
            .usage
            .spilled_bytes
            .saturating_add(self.next.stats().spilled_bytes.saturating_sub(before));
        self.refresh();
        self.observe(result)
    }

    fn validate(
        &self,
        queue: &OrderedFrontier,
        first: Option<u64>,
        poll: &(dyn Fn() -> bool + Sync),
    ) -> Result<bool, SpillError> {
        for index in 0..queue.len() {
            if index % 128 == 0 && poll() {
                return Ok(false);
            }
            let (id, bytes) = queue.read(index)?;
            if first.and_then(|n| n.checked_add(index as u64)) != Some(id) {
                return Err(frontier_error(
                    "record discovery ID differs from its ordered position",
                ));
            }
            self.codec.decode(&bytes).map_err(frontier_error)?;
        }
        Ok(true)
    }

    fn close_queue(
        &mut self,
        queue: &mut OrderedFrontier,
        first: Option<u64>,
        validate: bool,
        poll: &(dyn Fn() -> bool + Sync),
    ) -> Result<bool, Error> {
        let checked = if validate {
            self.validate(queue, first, poll)
        } else {
            Ok(true)
        };
        let closed = queue.close();
        let check = self.observe(checked);
        let close = self.observe(closed);
        check.and_then(|complete| close.map(|()| complete))
    }

    pub(crate) fn advance(&mut self, poll: &(dyn Fn() -> bool + Sync)) -> Result<bool, Error> {
        if let Some(mut old) = self.current.take()
            && !self.close_queue(&mut old, self.current_first, true, poll)?
        {
            self.refresh();
            return Ok(false);
        }
        let fresh = OrderedFrontier::new(self.memory_bytes, &self.directory);
        self.current = Some(std::mem::replace(&mut self.next, fresh));
        self.current_first = self.next_first.take();
        self.refresh();
        Ok(true)
    }

    pub(crate) fn chunk(
        &mut self,
        start: usize,
        end: usize,
        poll: &(dyn Fn() -> bool + Sync),
    ) -> Result<Option<Chunk>, Error> {
        let result = (|| {
            let queue = self
                .current
                .as_ref()
                .ok_or_else(|| frontier_error("missing current queue"))?;
            let mut chunk = Chunk::default();
            let handles = end
                .checked_sub(start)
                .and_then(|n| n.checked_mul(self.variables))
                .ok_or_else(|| frontier_error("decoded chunk size overflow"))?;
            chunk
                .identity
                .try_reserve_exact(handles)
                .map_err(frontier_error)?;
            chunk
                .original
                .try_reserve_exact(handles)
                .map_err(frontier_error)?;
            chunk
                .ids
                .try_reserve_exact(end - start)
                .map_err(frontier_error)?;
            for index in start..end {
                if (index - start).is_multiple_of(128) && poll() {
                    return Ok(None);
                }
                let (id, bytes) = queue.read(index)?;
                if self.current_first.and_then(|n| n.checked_add(index as u64)) != Some(id) {
                    return Err(frontier_error(
                        "record discovery ID differs from its ordered position",
                    ));
                }
                let rows = self.codec.decode(&bytes).map_err(frontier_error)?;
                chunk.identity.extend(rows.identity);
                chunk.original.extend(rows.original);
                chunk.ids.push(id);
            }
            Ok(Some(chunk))
        })();
        self.observe(result)
    }

    pub(crate) fn finish(&mut self, validate: bool, poll: &(dyn Fn() -> bool + Sync)) -> bool {
        let mut complete = true;
        if let Some(mut current) = self.current.take() {
            complete &= self
                .close_queue(&mut current, self.current_first, validate, poll)
                .unwrap_or(false);
        }
        let mut next = std::mem::replace(
            &mut self.next,
            OrderedFrontier::new(self.memory_bytes, &self.directory),
        );
        complete &= self
            .close_queue(&mut next, self.next_first, validate && complete, poll)
            .unwrap_or(false);
        self.refresh();
        complete
    }
}

fn frontier_error(cause: impl std::fmt::Display) -> SpillError {
    SpillError {
        code: "DIVE-S0200",
        message: format!("native frontier: {cause}"),
    }
}

/// The outcome of inserting a state into the seen set.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Inserted {
    /// The state was absent and is now present.
    New,
    /// The state was already present: its discovery id when the set retains
    /// ids (in-memory exact identities), otherwise `None`.
    Seen(Option<u64>),
}

impl Inserted {
    pub(crate) const fn is_new(self) -> bool {
        matches!(self, Self::New)
    }
}

/// Retained identities with equal keys, each with its discovery id.
type Bucket<T> = Vec<(T, u64)>;

/// Retained canonical rows, bucketed by the existing state hash. Hashes select
/// candidates only; even a complete collision cannot discard a distinct state.
/// Each retained identity carries the discovery id it was inserted with, so
/// equal keys of distinct states still resolve to distinct discoveries.
#[derive(Debug, Default)]
pub(crate) struct Exact {
    rows: HashMap<u64, Bucket<Vec<Handle>>>,
    encoded: Option<HashMap<u64, Bucket<Vec<u8>>>>,
    len: usize,
}

impl Exact {
    pub(crate) fn detached() -> Self {
        Self {
            encoded: Some(HashMap::new()),
            ..Self::default()
        }
    }

    pub(crate) const fn is_detached(&self) -> bool {
        self.encoded.is_some()
    }

    /// The discovery id of `row`, when it is present.
    pub(crate) fn lookup_controlled(
        &self,
        key: u64,
        row: &[Handle],
        heap: &Heap,
        poll: &(dyn Fn() -> bool + Sync),
    ) -> Result<Option<u64>, Error> {
        let Some(encoded) = &self.encoded else {
            return Ok(self.lookup(key, row, heap));
        };
        let bytes = detached_key(heap, row, poll)?;
        Ok(encoded.get(&key).and_then(|bucket| {
            bucket
                .iter()
                .find(|(kept, _)| *kept == bytes)
                .map(|&(_, id)| id)
        }))
    }

    pub(crate) fn contains_controlled(
        &self,
        key: u64,
        row: &[Handle],
        heap: &Heap,
        poll: &(dyn Fn() -> bool + Sync),
    ) -> Result<bool, Error> {
        Ok(self.lookup_controlled(key, row, heap, poll)?.is_some())
    }

    /// Inserts `row` with discovery id `id` unless it is present.
    pub(crate) fn insert_controlled(
        &mut self,
        key: u64,
        row: &[Handle],
        heap: &Heap,
        id: u64,
        poll: &(dyn Fn() -> bool + Sync),
    ) -> Result<Inserted, Error> {
        let Some(encoded) = &mut self.encoded else {
            return self.insert(key, row, heap, id);
        };
        let bytes = detached_key(heap, row, poll)?;
        if let Some(&(_, seen)) = encoded
            .get(&key)
            .and_then(|bucket| bucket.iter().find(|(kept, _)| *kept == bytes))
        {
            return Ok(Inserted::Seen(Some(seen)));
        }
        encoded
            .try_reserve(1)
            .map_err(|_| Error::internal("DIVE-S0203: detached exact state index allocation"))?;
        let bucket = encoded.entry(key).or_default();
        bucket.try_reserve(1).map_err(|_| {
            Error::internal("DIVE-S0203: detached exact collision bucket allocation")
        })?;
        bucket.push((bytes, id));
        self.len += 1;
        Ok(Inserted::New)
    }

    pub(crate) fn contains_encoded(
        &self,
        key: u64,
        bytes: &[u8],
        heap: &Heap,
        limit: usize,
        poll: &(dyn Fn() -> bool + Sync),
    ) -> Result<bool, Error> {
        let Some(rows) = self.rows.get(&key) else {
            return Ok(false);
        };
        for (row, _) in rows {
            let encoded =
                dive_core::key::canonical(heap, row, limit, &|| !poll()).map_err(|error| {
                    if error == dive_core::key::KeyError::Interrupted {
                        Error::stop()
                    } else {
                        Error::internal(format!("DIVE-S0500: distributed exact key: {error:?}"))
                    }
                })?;
            if encoded == bytes {
                return Ok(true);
            }
        }
        Ok(false)
    }
    pub(crate) const fn len(&self) -> usize {
        self.len
    }

    /// The discovery id of `row`, when it is present.
    pub(crate) fn lookup(&self, key: u64, row: &[Handle], heap: &Heap) -> Option<u64> {
        self.rows.get(&key).and_then(|bucket| {
            bucket
                .iter()
                .find(|(kept, _)| {
                    kept.len() == row.len() && kept.iter().zip(row).all(|(&a, &b)| heap.eq(a, b))
                })
                .map(|&(_, id)| id)
        })
    }

    /// Inserts `row` with discovery id `id` unless it is present.
    pub(crate) fn insert(
        &mut self,
        key: u64,
        row: &[Handle],
        heap: &Heap,
        id: u64,
    ) -> Result<Inserted, Error> {
        if let Some(seen) = self.lookup(key, row, heap) {
            return Ok(Inserted::Seen(Some(seen)));
        }
        self.rows
            .try_reserve(1)
            .map_err(|_| Error::unsupported("exact state index allocation"))?;
        let mut kept = Vec::new();
        kept.try_reserve_exact(row.len())
            .map_err(|_| Error::unsupported("exact state row allocation"))?;
        kept.extend_from_slice(row);
        let bucket = self.rows.entry(key).or_default();
        bucket
            .try_reserve(1)
            .map_err(|_| Error::unsupported("exact state collision bucket allocation"))?;
        bucket.push((kept, id));
        self.len += 1;
        Ok(Inserted::New)
    }
}

fn detached_key(
    heap: &Heap,
    row: &[Handle],
    poll: &(dyn Fn() -> bool + Sync),
) -> Result<Vec<u8>, Error> {
    use dive_core::key::KeyError;
    dive_core::key::canonical(heap, row, usize::MAX, &|| !poll()).map_err(|error| match error {
        KeyError::Interrupted => Error::stop(),
        KeyError::Internal(message) => Error::internal(message),
        KeyError::Unsupported(message) => Error::unsupported(message),
        KeyError::Limit(message) => Error::internal(format!("DIVE-S0203: {message}")),
    })
}

#[cfg(test)]
#[allow(clippy::unwrap_used, reason = "exact collision controls")]
mod tests {
    use std::fs;
    use std::io::{Seek, SeekFrom, Write};
    use std::sync::Arc;

    use dive_core::Store;

    use super::*;

    fn directory() -> PathBuf {
        static NEXT_DIRECTORY: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let path = std::env::temp_dir().join(format!(
            "dive-native-frontier-{}-{}",
            std::process::id(),
            NEXT_DIRECTORY.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
        ));
        fs::create_dir(&path).unwrap();
        path
    }

    fn queues(path: &std::path::Path) -> Frontiers {
        let mut queues = Frontiers::new(
            &Arc::new(Store::new(true)),
            1,
            &Storage {
                frontier_memory_bytes: Some(0),
                frontier_directory: Some(path.to_path_buf()),
                ..Storage::default()
            },
        )
        .unwrap();
        for id in 0_u32..3 {
            let value = Handle::int(i64::from(id)).unwrap();
            queues.push(u64::from(id), &[value], &[value]).unwrap();
        }
        queues
    }

    #[test]
    fn remapping_cancels_and_reports_corruption_write_and_cleanup_failures() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        for fault in ["cancel", "corrupt", "write", "cleanup"] {
            let path = directory();
            let store = Arc::new(Store::new(true));
            let permanent =
                dive_core::store::compact::Permanent::capture(&store, &|| true).unwrap();
            let mut remap = permanent.begin(&store, &|| true).unwrap();
            let mut queues = Frontiers::new(
                &store,
                1,
                &Storage {
                    frontier_memory_bytes: Some(0),
                    frontier_directory: Some(path.clone()),
                    ..Storage::default()
                },
            )
            .unwrap();
            for id in 0_u32..4 {
                let h = Handle::int(i64::from(id)).unwrap();
                queues.push(u64::from(id), &[h], &[h]).unwrap();
            }
            let owned = queues.next.disk_directory().unwrap().to_path_buf();
            if fault == "write" {
                let blocked = path.join("file");
                fs::write(&blocked, b"not a directory").unwrap();
                queues.directory = blocked;
            }
            let calls = AtomicUsize::new(0);
            let poll = || {
                // The first row has already been copied and written when the
                // next root polls. Mutations therefore exercise partial output.
                if calls.fetch_add(1, Ordering::Relaxed) == 3 {
                    match fault {
                        "cancel" => return true,
                        "corrupt" => {
                            let mut file = fs::OpenOptions::new()
                                .write(true)
                                .open(owned.join("states"))
                                .unwrap();
                            file.seek(SeekFrom::End(-1)).unwrap();
                            file.write_all(&[255]).unwrap();
                        }
                        "cleanup" => {
                            fs::rename(&owned, path.join("moved")).unwrap();
                            fs::write(&owned, b"blocks cleanup").unwrap();
                        }
                        _ => {}
                    }
                }
                false
            };
            let result = queues.remap(&mut remap, &poll);
            assert!(result.is_err(), "{fault}");
            if fault == "cancel" {
                assert_eq!(result.err().unwrap().kind(), dive_core::ErrorKind::Stop);
                assert_eq!(queues.usage.failures, [] as [String; 0]);
            } else {
                assert!(!queues.usage.failures.is_empty(), "{fault}");
            }
            queues.finish(false, &|| false);
            if fault == "cleanup" {
                assert!(queues.usage.failures.iter().any(|s| s.contains("cleanup")));
            }
            assert_eq!(queues.usage.retained_bytes, 0);
            fs::remove_dir_all(path).unwrap();
        }
    }

    #[test]
    fn detached_exact_keys_survive_replacement_and_resolve_complete_collisions() {
        let store = Arc::new(Store::new(true));
        let permanent = dive_core::store::compact::Permanent::capture(&store, &|| true).unwrap();
        let mut heap = Heap::new(Arc::clone(&store), true, false);
        let one = Handle::int(1).unwrap();
        let two = Handle::int(2).unwrap();
        let a = heap.tuple_literal(&[one, two]);
        let a = heap.intern(a).unwrap();
        let b = heap.tuple_literal(&[two, one]);
        let b = heap.intern(b).unwrap();
        let mut exact = Exact::detached();
        assert_eq!(
            exact
                .insert_controlled(0, &[a], &heap, 10, &|| false)
                .unwrap(),
            Inserted::New
        );
        assert_eq!(
            exact
                .insert_controlled(0, &[b], &heap, 11, &|| false)
                .unwrap(),
            Inserted::New
        );
        let mut remap = permanent.begin(&store, &|| true).unwrap();
        let mapped = remap.copy(b, &|| true).unwrap();
        let replacement = Heap::new(Arc::clone(remap.store()), true, false);
        assert!(
            exact
                .contains_controlled(0, &[mapped], &replacement, &|| false)
                .unwrap()
        );
        assert_eq!(
            exact
                .insert_controlled(0, &[mapped], &replacement, 12, &|| false)
                .unwrap(),
            Inserted::Seen(Some(11)),
            "a detached collision resolves to the discovery id of its own identity"
        );
        assert_eq!(
            exact
                .lookup_controlled(0, &[mapped], &replacement, &|| false)
                .unwrap(),
            Some(11)
        );
        assert!(
            exact.rows.is_empty(),
            "detached keys must retain no old Store handles"
        );
        assert_eq!(exact.len(), 2);
    }

    #[test]
    fn missing_current_file_and_unvisited_corruption_invalidate_storage() {
        for missing in [false, true] {
            let path = directory();
            let mut queues = queues(&path);
            queues.advance(&|| false).unwrap();
            assert!(queues.chunk(0, 1, &|| false).unwrap().is_some());
            let owned = queues
                .current
                .as_ref()
                .unwrap()
                .disk_directory()
                .unwrap()
                .to_path_buf();
            if missing {
                fs::remove_file(owned.join("states")).unwrap();
                assert!(queues.chunk(1, 2, &|| false).is_err());
            } else {
                let mut file = fs::OpenOptions::new()
                    .write(true)
                    .open(owned.join("states"))
                    .unwrap();
                file.seek(SeekFrom::End(-1)).unwrap();
                file.write_all(&[255]).unwrap();
            }
            queues.finish(true, &|| false);
            assert_ne!(queues.usage.failures, [] as [String; 0]);
            assert!(!owned.exists());
            assert_eq!(queues.usage.retained_bytes, 0);
            fs::remove_dir(path).unwrap();
        }
    }

    #[test]
    fn already_stopped_cleanup_does_not_read_or_poll_unprocessed_records() {
        let path = directory();
        let mut queues = queues(&path);
        queues.advance(&|| false).unwrap();
        let owned = queues
            .current
            .as_ref()
            .unwrap()
            .disk_directory()
            .unwrap()
            .to_path_buf();
        fs::remove_file(owned.join("states")).unwrap();
        queues.finish(false, &|| true);
        assert_eq!(queues.usage.failures, [] as [String; 0]);
        assert!(!owned.exists());
        fs::remove_dir(path).unwrap();
    }

    #[test]
    fn cleanup_failure_is_explicit_even_without_a_certifiable_ending() {
        let path = directory();
        let mut queues = queues(&path);
        let owned = queues.next.disk_directory().unwrap().to_path_buf();
        let moved = path.join("moved");
        fs::rename(&owned, &moved).unwrap();
        fs::write(&owned, b"file blocks directory cleanup").unwrap();
        queues.finish(false, &|| false);
        assert!(
            queues
                .usage
                .failures
                .iter()
                .any(|error| error.contains("cleanup"))
        );
        fs::remove_file(owned).unwrap();
        fs::remove_dir_all(path).unwrap();
    }

    #[test]
    fn complete_hash_collision_preserves_distinct_rows_and_zero_key() {
        let mut heap = Heap::new(Arc::new(Store::new(true)), true, false);
        let mut seen = Exact::default();
        let one = Handle::int(1).unwrap();
        let two = Handle::int(2).unwrap();
        let a = heap.tuple(&[one, two]);
        let a = heap.intern(a).unwrap();
        let b = heap.tuple(&[two, one]);
        let b = heap.intern(b).unwrap();
        assert_eq!(seen.insert(0, &[a], &heap, 0).unwrap(), Inserted::New);
        assert_eq!(seen.insert(0, &[b], &heap, 1).unwrap(), Inserted::New);
        assert_eq!(
            seen.insert(0, &[a], &heap, 2).unwrap(),
            Inserted::Seen(Some(0))
        );
        assert_eq!(
            seen.insert(0, &[b], &heap, 2).unwrap(),
            Inserted::Seen(Some(1))
        );
        assert_eq!(seen.lookup(0, &[a], &heap), Some(0));
        assert_eq!(seen.lookup(0, &[b], &heap), Some(1));
        assert_eq!(seen.lookup(1, &[a], &heap), None);
        assert_eq!(seen.len, 2);
    }

    #[test]
    fn exact_equality_ignores_literal_class_and_hashconsing_without_erasing_views() {
        for hashcons in [false, true] {
            let mut heap = Heap::new(Arc::new(Store::new(hashcons)), true, false);
            let one = Handle::int(1).unwrap();
            let two = Handle::int(2).unwrap();
            let tuple = heap.tuple_literal(&[one, two]);
            let original = heap.intern_exact(tuple).unwrap();
            let canonical_tuple = heap.intern(original).unwrap();
            let function = heap.fcn_sorted(&[one, two], &[one, two]);
            let canonical_function = heap.intern(function).unwrap();
            let mut seen = Exact::default();
            assert_eq!(
                seen.insert(7, &[canonical_tuple], &heap, 3).unwrap(),
                Inserted::New
            );
            assert_eq!(
                seen.insert(7, &[canonical_function], &heap, 4).unwrap(),
                Inserted::Seen(Some(3))
            );
            assert!(heap.view(original).is_tuple_literal());
            assert!(!heap.view(canonical_tuple).is_tuple_literal());
            assert!(!heap.view(canonical_function).is_tuple_literal());
        }
    }
}