triblespace-core 0.47.1

The triblespace core implementation.
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
use std::collections::BTreeSet;
use std::error::Error;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

use ed25519_dalek::SigningKey;

use crate::blob::encodings::simplearchive::SimpleArchive;
use crate::blob::{Blob, BlobEncoding, IntoBlob, TryFromBlob};
use crate::capability::{CapabilityProof, CapabilityProofId};
use crate::collection::test_support::{TestImage, TestImageTwo};
use crate::collection::{
    descriptor, read_capability, write_capability, AdmissionPolicy, Collection, CollectionCommit,
    CollectionData, CollectionDerive, CollectionMerge, CollectionPolicy, CollectionRead,
    CollectionRecord, CollectionRecordSelector, CollectionSnapshotExt, CollectionStore,
    CollectionStoreExt,
};
use crate::inline::encodings::hash::Handle;
use crate::inline::{Inline, InlineEncoding};
use crate::repo::memoryrepo::{MemoryRepo, MemoryRepoSnapshot};
use crate::repo::pile::Pile;
use crate::repo::{
    BlobInfo, BlobMetadata, BlobStoreGet, BlobStoreList, BlobStoreMeta, BlobStorePut,
    CapabilityProofRead, SnapshotSource, StoreChanges, StoreSnapshot, WantRead,
};
use crate::trible::{Fragment, Trible, TribleSet, TRIBLE_LEN};

fn policy(signer: &SigningKey) -> CollectionPolicy {
    CollectionPolicy::new(
        AdmissionPolicy::Open,
        AdmissionPolicy::direct(signer.verifying_key()),
    )
}

fn facts(entity: u8, value: u8) -> TribleSet {
    let mut bytes = [value; TRIBLE_LEN];
    bytes[..16].fill(entity);
    bytes[16..32].fill(9);
    let mut facts = TribleSet::new();
    facts.insert(&Trible::force_raw(bytes).unwrap());
    facts
}

fn data<E: BlobEncoding>(blob: &Blob<E>) -> CollectionData
where
    Handle<E>: InlineEncoding,
{
    Handle::<E>::to_hash(blob.get_handle())
}

/// The target descriptor and output are resident; its actual source descriptor,
/// COMMIT, payload and metadata have deliberately not been inserted.
struct OneHop {
    store: MemoryRepo,
    source_descriptor: Fragment,
    source: Collection<SimpleArchive>,
    target: Collection<TestImage>,
    source_blob: Blob<SimpleArchive>,
    metadata: Blob<SimpleArchive>,
    commit: CollectionCommit,
    output: Blob<TestImage>,
    equation: CollectionRecord,
    source_owner: SigningKey,
    producer: SigningKey,
}

fn one_hop() -> OneHop {
    let source_owner = SigningKey::from_bytes(&[11; 32]);
    let producer = SigningKey::from_bytes(&[12; 32]);
    let source_descriptor =
        descriptor::naming::<SimpleArchive>("passive-observation-source", policy(&source_owner));
    let source = Collection::from_handle(source_descriptor.facts().clone().to_blob().get_handle());
    // The producer maintains the view, so its merges are the ones believed.
    let mut store = MemoryRepo::for_host(producer.verifying_key());
    let target = store
        .derive::<TestImage>(source, (), policy(&producer))
        .unwrap();
    let source_blob = facts(1, 21).to_blob();
    let metadata = facts(2, 22).to_blob();
    let commit = CollectionCommit::sign(
        &source_owner,
        source.handle(),
        data(&source_blob),
        metadata.get_handle(),
    );
    let output = TestImage::image(&source_blob);
    store.put::<TestImage, _>(output.clone()).unwrap();
    let equation = CollectionRecord::Derive(CollectionDerive::sign(
        &producer,
        target.handle(),
        crate::collection::SourceLocator::of(commit.data().raw),
        data(&output),
    ));
    OneHop {
        store,
        source_descriptor,
        source,
        target,
        source_blob,
        metadata,
        commit,
        output,
        equation,
        source_owner,
        producer,
    }
}

/// Make the fixture's lineage whole: the source descriptor and the admitted
/// COMMIT the target's one image stands on.
fn admit_source(fixture: &mut OneHop) {
    descriptor::put_closure(&mut fixture.store, &fixture.source_descriptor).unwrap();
    fixture
        .store
        .insert(CollectionRecord::Commit(fixture.commit))
        .unwrap();
}

#[derive(Default)]
struct ReadCounts {
    gets: Mutex<Vec<CollectionData>>,
    metadata: Mutex<Vec<CollectionData>>,
    selections: Mutex<Vec<BTreeSet<CollectionRecordSelector>>>,
    record_enumerations: AtomicUsize,
    proof_queries: AtomicUsize,
}

#[derive(Clone)]
struct CountingSnapshot {
    inner: MemoryRepoSnapshot,
    counts: Arc<ReadCounts>,
}

impl CountingSnapshot {
    fn new(inner: MemoryRepoSnapshot) -> Self {
        Self {
            inner,
            counts: Arc::default(),
        }
    }

    fn assert_no_record_enumeration(&self) {
        // The index answers every read; the only way left to reach an
        // ancestor's rows would be a whole enumeration, and there is none.
        assert_eq!(self.counts.record_enumerations.load(Ordering::SeqCst), 0);
    }

    fn assert_not_loaded(&self, handle: CollectionData) {
        assert!(!self.counts.gets.lock().unwrap().contains(&handle));
        assert!(!self.counts.metadata.lock().unwrap().contains(&handle));
    }
}

impl StoreSnapshot for CountingSnapshot {
    fn changes_since(&self, previous: &Self) -> StoreChanges {
        self.inner.changes_since(&previous.inner)
    }
}

impl BlobStoreGet for CountingSnapshot {
    type GetError<E: Error + Send + Sync + 'static> =
        <MemoryRepoSnapshot as BlobStoreGet>::GetError<E>;

    fn get<T, E>(
        &self,
        handle: Inline<Handle<E>>,
    ) -> Result<T, Self::GetError<<T as TryFromBlob<E>>::Error>>
    where
        E: BlobEncoding + 'static,
        T: TryFromBlob<E>,
        Handle<E>: InlineEncoding,
    {
        self.counts
            .gets
            .lock()
            .unwrap()
            .push(CollectionData::new(handle.raw));
        self.inner.get(handle)
    }
}

impl BlobStoreMeta for CountingSnapshot {
    type MetaError = <MemoryRepoSnapshot as BlobStoreMeta>::MetaError;
    fn metadata<E>(
        &self,
        handle: Inline<Handle<E>>,
    ) -> Result<Option<BlobMetadata>, Self::MetaError>
    where
        E: BlobEncoding + 'static,
        Handle<E>: InlineEncoding,
    {
        self.counts
            .metadata
            .lock()
            .unwrap()
            .push(CollectionData::new(handle.raw));
        self.inner.metadata(handle)
    }
}

impl BlobStoreList for CountingSnapshot {
    type Err = <MemoryRepoSnapshot as BlobStoreList>::Err;
    type Iter<'a>
        = <MemoryRepoSnapshot as BlobStoreList>::Iter<'a>
    where
        Self: 'a;
    fn blobs<'a>(&'a self) -> Self::Iter<'a> {
        self.inner.blobs()
    }
    fn contains_blob<E>(&self, handle: Inline<Handle<E>>) -> Result<bool, Self::Err>
    where
        E: BlobEncoding + 'static,
        Handle<E>: InlineEncoding,
    {
        self.inner.contains_blob(handle)
    }
    fn blob_info<E>(&self, handle: Inline<Handle<E>>) -> Result<Option<BlobInfo>, Self::Err>
    where
        E: BlobEncoding + 'static,
        Handle<E>: InlineEncoding,
    {
        self.inner.blob_info(handle)
    }
    fn blobs_diff<'a>(&'a self, previous: &Self) -> Self::Iter<'a> {
        self.inner.blobs_diff(&previous.inner)
    }
}

impl crate::collection::CoverageRead for CountingSnapshot {
    fn index(
        &self,
        lineage: &BTreeSet<crate::collection::CollectionHandle>,
    ) -> Result<crate::collection::coverage::CoverageIndex, Self::RecordsError> {
        self.inner.index(lineage)
    }
}

impl CollectionRead for CountingSnapshot {
    type RecordsError = <MemoryRepoSnapshot as CollectionRead>::RecordsError;
    type RecordIter<'a>
        = <MemoryRepoSnapshot as CollectionRead>::RecordIter<'a>
    where
        Self: 'a;
    fn records<'a>(&'a self) -> Result<Self::RecordIter<'a>, Self::RecordsError> {
        self.counts
            .record_enumerations
            .fetch_add(1, Ordering::SeqCst);
        self.inner.records()
    }
    fn collections(&self) -> Result<Vec<crate::collection::CollectionHandle>, Self::RecordsError> {
        self.inner.collections()
    }
    fn select_records(
        &self,
        selectors: &BTreeSet<CollectionRecordSelector>,
    ) -> Result<Vec<CollectionRecord>, Self::RecordsError> {
        self.counts
            .selections
            .lock()
            .unwrap()
            .push(selectors.clone());
        self.inner.select_records(selectors)
    }
}

impl CapabilityProofRead for CountingSnapshot {
    type ProofsError = <MemoryRepoSnapshot as CapabilityProofRead>::ProofsError;
    type ProofIter<'a>
        = <MemoryRepoSnapshot as CapabilityProofRead>::ProofIter<'a>
    where
        Self: 'a;
    fn proofs<'a>(&'a self) -> Result<Self::ProofIter<'a>, Self::ProofsError> {
        self.counts.proof_queries.fetch_add(1, Ordering::SeqCst);
        self.inner.proofs()
    }
    fn proof(&self, id: CapabilityProofId) -> Result<Option<CapabilityProof>, Self::ProofsError> {
        self.counts.proof_queries.fetch_add(1, Ordering::SeqCst);
        self.inner.proof(id)
    }
}

impl WantRead for CountingSnapshot {
    type WantsError = <MemoryRepoSnapshot as WantRead>::WantsError;
    type WantIter<'a>
        = <MemoryRepoSnapshot as WantRead>::WantIter<'a>
    where
        Self: 'a;
    fn wants<'a>(&'a self) -> Result<Self::WantIter<'a>, Self::WantsError> {
        self.inner.wants()
    }
}

#[test]
fn target_reads_its_own_leaf_from_the_index_whatever_its_source_holds() {
    let mut fixture = one_hop();
    fixture.store.insert(fixture.equation).unwrap();
    // Coverage is collection-local: the target's leaf is admitted by the
    // target's own WRITE policy and reads no row of its source. With the
    // source unknown here altogether -- not even its descriptor -- the
    // target still reads its own image, from the index, and nothing
    // beneath it.
    let before = CountingSnapshot::new(fixture.store.snapshot().unwrap());
    assert!(!before.inner.contains_blob(fixture.source.handle()).unwrap());
    let observed = before.collection(fixture.target).unwrap();
    assert_eq!(
        observed.cover().members().collect::<Vec<_>>(),
        [fixture.output.get_handle()]
    );
    assert_eq!(
        observed.support().unwrap().members().collect::<Vec<_>>(),
        [fixture.output.get_handle()]
    );
    let view: TribleSet = observed.view().unwrap();
    assert_eq!(view, facts(1, 21));
    before.assert_no_record_enumeration();
    before.assert_not_loaded(data(&fixture.source_blob));
    before.assert_not_loaded(data(&fixture.metadata));

    // What the leaf's locator names is a question about the source, asked
    // through the leaves by locator. Known but without the input's COMMIT,
    // the source has no foundation the view stands for.
    descriptor::put_closure(&mut fixture.store, &fixture.source_descriptor).unwrap();
    let without_commit = fixture.store.snapshot().unwrap();
    assert!(without_commit
        .select_records(&BTreeSet::from([CollectionRecordSelector::ProducedMember(
            fixture.source.handle(),
            fixture.commit.data(),
        )]))
        .unwrap()
        .is_empty());
    assert!(crate::collection::test_support::stood_for(
        &without_commit.collection(fixture.target).unwrap()
    )
    .is_empty());

    fixture
        .store
        .insert(CollectionRecord::Commit(fixture.commit))
        .unwrap();
    let after = CountingSnapshot::new(fixture.store.snapshot().unwrap());
    let complete = after.collection(fixture.target).unwrap();
    assert_eq!(
        complete.cover().members().collect::<Vec<_>>(),
        [fixture.output.get_handle()]
    );
    let view: TribleSet = complete.view().unwrap();
    assert_eq!(view, facts(1, 21));
    let support = &crate::collection::test_support::stood_for(&complete);
    assert_eq!(support.collection(), fixture.source);
    assert_eq!(support.len(), 1);
    assert!(support.contains(fixture.source_blob.get_handle()));
    // The source payload and metadata are neither resident nor asked for:
    // reading the target loads only its own output.
    assert!(!after
        .inner
        .contains_blob(fixture.source_blob.get_handle())
        .unwrap());
    after.assert_no_record_enumeration();
    after.assert_not_loaded(data(&fixture.source_blob));
    after.assert_not_loaded(data(&fixture.metadata));
    // The earlier observation read the same leaf and keeps it.
    assert_eq!(
        observed.cover().members().collect::<Vec<_>>(),
        [fixture.output.get_handle()]
    );
}

#[test]
fn unauthorized_target_producers_neither_admit_outputs_nor_hide_authorized_inputs() {
    let mut fixture = one_hop();
    admit_source(&mut fixture);
    fixture.store.insert(fixture.equation).unwrap();
    let outsider = SigningKey::from_bytes(&[31; 32]);
    let other_source: Blob<SimpleArchive> = facts(3, 23).to_blob();
    let other_commit = CollectionCommit::sign(
        &outsider,
        fixture.source.handle(),
        data(&other_source),
        fixture.metadata.get_handle(),
    );
    let wrong = TestImage::image(&other_source);
    fixture.store.put::<TestImage, _>(wrong.clone()).unwrap();
    fixture
        .store
        .insert(CollectionRecord::Derive(CollectionDerive::sign(
            &outsider,
            fixture.target.handle(),
            crate::collection::SourceLocator::of(other_commit.data().raw),
            data(&wrong),
        )))
        .unwrap();
    fixture
        .store
        .insert(CollectionRecord::Merge(
            CollectionMerge::sign(
                &outsider,
                fixture.target.handle(),
                // Another key's absorption of the authorized output: not
                // the store's host, so never folded.
                [data(&fixture.output), data(&wrong)],
                data(&wrong),
            )
            .unwrap(),
        ))
        .unwrap();
    let snapshot = fixture.store.snapshot().unwrap();
    let observed = snapshot.collection(fixture.target).unwrap();
    assert_eq!(
        observed.cover().members().collect::<Vec<_>>(),
        [fixture.output.get_handle()]
    );
    assert_eq!(observed.view::<TribleSet>().unwrap(), facts(1, 21));
    assert_eq!(snapshot.proofs().unwrap().count(), 0);
}

#[test]
fn absent_target_parent_is_read_through_its_resident_merge_inputs() {
    let mut fixture = one_hop();
    admit_source(&mut fixture);
    fixture.store.insert(fixture.equation).unwrap();
    let other_source: Blob<SimpleArchive> = facts(3, 23).to_blob();
    let other_commit = CollectionCommit::sign(
        &fixture.source_owner,
        fixture.source.handle(),
        data(&other_source),
        fixture.metadata.get_handle(),
    );
    fixture
        .store
        .insert(CollectionRecord::Commit(other_commit))
        .unwrap();
    let other_output = TestImage::image(&other_source);
    fixture
        .store
        .put::<TestImage, _>(other_output.clone())
        .unwrap();
    let other_equation = CollectionRecord::Derive(CollectionDerive::sign(
        &fixture.producer,
        fixture.target.handle(),
        crate::collection::SourceLocator::of(other_commit.data().raw),
        data(&other_output),
    ));
    fixture.store.insert(other_equation).unwrap();
    let mut union = facts(1, 21);
    union.union(facts(3, 23));
    let union_blob = TestImage::image(&union.clone().to_blob());
    // The parent's record is here; its bytes are not. The frontier is the
    // parent alone, and the read descends through the MERGE that produced
    // it to the two resident images beneath.
    let parent = CollectionRecord::Merge(
        CollectionMerge::sign(
            &fixture.producer,
            fixture.target.handle(),
            [data(&fixture.output), data(&other_output)],
            data(&union_blob),
        )
        .unwrap(),
    );
    fixture.store.insert(parent).unwrap();
    let both = fixture
        .source
        .cover([fixture.source_blob.get_handle(), other_source.get_handle()]);
    let before = fixture
        .store
        .snapshot()
        .unwrap()
        .collection(fixture.target)
        .unwrap();
    assert_eq!(before.cover().len(), 2);
    assert!(before.cover().contains(fixture.output.get_handle()));
    assert!(before.cover().contains(other_output.get_handle()));
    assert_eq!(crate::collection::test_support::stood_for(&before), both);
    assert_eq!(before.view::<TribleSet>().unwrap(), union);

    fixture
        .store
        .put::<TestImage, _>(union_blob.clone())
        .unwrap();
    let after = fixture
        .store
        .snapshot()
        .unwrap()
        .collection(fixture.target)
        .unwrap();
    assert_eq!(
        after.cover().members().collect::<Vec<_>>(),
        [union_blob.get_handle()]
    );
    assert_eq!(crate::collection::test_support::stood_for(&after), both);
    assert_eq!(after.view::<TribleSet>().unwrap(), union);
    assert_eq!(before.cover().len(), 2);
    assert_eq!(before.view::<TribleSet>().unwrap(), union);
}

#[test]
fn direct_commit_in_a_derived_target_does_not_become_foundational_membership() {
    let mut fixture = one_hop();
    admit_source(&mut fixture);
    fixture
        .store
        .insert(CollectionRecord::Commit(CollectionCommit::sign(
            &fixture.producer,
            fixture.target.handle(),
            data(&fixture.output),
            fixture.metadata.get_handle(),
        )))
        .unwrap();
    let before = fixture
        .store
        .snapshot()
        .unwrap()
        .collection(fixture.target)
        .unwrap();
    assert!(before.cover().is_empty());
    assert!(before.support().unwrap().is_empty());
    assert!(before.view::<TribleSet>().unwrap().is_empty());
    fixture.store.insert(fixture.equation).unwrap();
    let after = fixture
        .store
        .snapshot()
        .unwrap()
        .collection(fixture.target)
        .unwrap();
    assert_eq!(
        after.cover().members().collect::<Vec<_>>(),
        [fixture.output.get_handle()]
    );
    assert!(before.cover().is_empty());
}

/// A second hop on top of the fixture: the Rank9 image of the target's one
/// output, produced by `final_owner`.
fn second_hop(
    fixture: &mut OneHop,
    final_owner: &SigningKey,
) -> (Collection<TestImageTwo>, Blob<TestImageTwo>) {
    let final_target = fixture
        .store
        .derive::<TestImageTwo>(fixture.target, (), policy(final_owner))
        .unwrap();
    let accelerated = TestImageTwo::image(&fixture.output);
    fixture
        .store
        .put::<TestImageTwo, _>(accelerated.clone())
        .unwrap();
    fixture
        .store
        .insert(CollectionRecord::Derive(CollectionDerive::sign(
            final_owner,
            final_target.handle(),
            crate::collection::SourceLocator::of(data(&fixture.output).raw),
            data(&accelerated),
        )))
        .unwrap();
    (final_target, accelerated)
}

#[test]
fn multihop_read_takes_cover_and_support_from_the_index_without_payload_reads() {
    let mut fixture = one_hop();
    admit_source(&mut fixture);
    fixture.store.insert(fixture.equation).unwrap();
    let final_owner = SigningKey::from_bytes(&[42; 32]);
    let (final_target, accelerated) = second_hop(&mut fixture, &final_owner);
    let inner = fixture.store.snapshot().unwrap();
    // The source's one commit payload is not resident, so the source itself
    // has no resident cover; the images above it are read all the same.
    assert!(inner.collection(fixture.source).unwrap().cover().is_empty());
    assert_eq!(
        inner
            .collection(fixture.target)
            .unwrap()
            .cover()
            .members()
            .collect::<Vec<_>>(),
        [fixture.output.get_handle()]
    );
    assert_eq!(inner.proofs().unwrap().count(), 0);
    let snapshot = CountingSnapshot::new(inner);
    let observed = snapshot.collection(final_target).unwrap();
    assert_eq!(
        observed.cover().members().collect::<Vec<_>>(),
        [accelerated.get_handle()]
    );
    let view: TribleSet = observed.view().unwrap();
    assert_eq!(view, facts(1, 21));
    snapshot.assert_no_record_enumeration();
    snapshot.assert_not_loaded(data(&fixture.source_blob));
    snapshot.assert_not_loaded(data(&fixture.metadata));
    // Following the leaves of every hop down to the root, the final image
    // stands for the one root commit.
    let stood = &crate::collection::test_support::stood_for(&observed);
    assert_eq!(stood.collection(), fixture.source);
    assert!(stood.contains(fixture.source_blob.get_handle()));
    assert_eq!(stood.len(), 1);
    snapshot.assert_no_record_enumeration();
    snapshot.assert_not_loaded(data(&fixture.source_blob));
    snapshot.assert_not_loaded(data(&fixture.metadata));
    // The observation's own support is the row the index already held when
    // it attached: its own leaf image. Asking for it, of the observation or
    // of a clone, reads nothing at all: no record is selected, no blob
    // loaded, no proof asked for.
    let selections = snapshot.counts.selections.lock().unwrap().len();
    let blob_reads = snapshot.counts.gets.lock().unwrap().len();
    let proof_queries = snapshot.counts.proof_queries.load(Ordering::SeqCst);
    let support = observed.support().unwrap().clone();
    assert_eq!(
        support.members().collect::<Vec<_>>(),
        [accelerated.get_handle()]
    );
    let cloned = observed.clone();
    assert_eq!(cloned.support().unwrap(), &support);
    assert_eq!(snapshot.counts.selections.lock().unwrap().len(), selections);
    assert_eq!(snapshot.counts.gets.lock().unwrap().len(), blob_reads);
    assert_eq!(
        snapshot.counts.proof_queries.load(Ordering::SeqCst),
        proof_queries
    );
}

#[test]
fn multihop_read_stands_on_its_own_writer_above_an_unadmitted_ancestor() {
    let mut fixture = one_hop();
    descriptor::put_closure(&mut fixture.store, &fixture.source_descriptor).unwrap();
    // The historical source commit and the target's image of it are signed
    // by a key nothing here admits. The final producer is admitted, and its
    // record is believed.
    let historical = SigningKey::from_bytes(&[41; 32]);
    fixture
        .store
        .insert(CollectionRecord::Commit(CollectionCommit::sign(
            &historical,
            fixture.source.handle(),
            data(&fixture.source_blob),
            fixture.metadata.get_handle(),
        )))
        .unwrap();
    fixture
        .store
        .insert(CollectionRecord::Derive(CollectionDerive::sign(
            &historical,
            fixture.target.handle(),
            crate::collection::SourceLocator::of(fixture.commit.data().raw),
            data(&fixture.output),
        )))
        .unwrap();
    let final_owner = SigningKey::from_bytes(&[42; 32]);
    let (final_target, accelerated) = second_hop(&mut fixture, &final_owner);
    let snapshot = CountingSnapshot::new(fixture.store.snapshot().unwrap());
    // The final image stands on its own admitted writer: its leaf reads no
    // row beneath it, whoever signed the records there.
    let observed = snapshot.collection(final_target).unwrap();
    assert_eq!(
        observed.cover().members().collect::<Vec<_>>(),
        [accelerated.get_handle()]
    );
    assert_eq!(
        observed.support().unwrap().members().collect::<Vec<_>>(),
        [accelerated.get_handle()]
    );
    assert_eq!(observed.view::<TribleSet>().unwrap(), facts(1, 21));
    // Nothing walked the chain to read it: no enumeration, no root payload.
    snapshot.assert_no_record_enumeration();
    snapshot.assert_not_loaded(data(&fixture.source_blob));
    snapshot.assert_not_loaded(data(&fixture.metadata));
    // Followed through the believed leaves of every hop, though, it stands
    // for nothing its root admits: neither the source commit nor the middle
    // image beneath it is believed.
    assert!(crate::collection::test_support::stood_for(&observed).is_empty());

    // Admitted records for the same payloads make the chain stand.
    fixture
        .store
        .insert(CollectionRecord::Commit(fixture.commit))
        .unwrap();
    fixture.store.insert(fixture.equation).unwrap();
    let admitted = fixture
        .store
        .snapshot()
        .unwrap()
        .collection(final_target)
        .unwrap();
    assert_eq!(
        admitted.cover().members().collect::<Vec<_>>(),
        [accelerated.get_handle()]
    );
    assert_eq!(
        crate::collection::test_support::stood_for(&admitted),
        fixture.source.cover([fixture.source_blob.get_handle()])
    );
    // The earlier observation read the same image and keeps it.
    assert_eq!(
        observed.cover().members().collect::<Vec<_>>(),
        [accelerated.get_handle()]
    );
}

fn empty_pile(dir: &tempfile::TempDir) -> Pile {
    let path = dir.path().join("observation.pile");
    std::fs::File::create(&path).unwrap();
    Pile::open(&path).unwrap()
}

#[test]
fn pile_observation_tracks_only_consulted_lineage_and_target_changes() {
    let fixture = one_hop();
    let dir = tempfile::tempdir().unwrap();
    let mut pile = empty_pile(&dir);
    assert_eq!(
        pile.derive::<TestImage>(fixture.source, (), policy(&fixture.producer))
            .unwrap(),
        fixture.target
    );
    descriptor::put_closure(&mut pile, &fixture.source_descriptor).unwrap();
    pile.put::<TestImage, _>(fixture.output.clone()).unwrap();
    pile.insert(fixture.equation).unwrap();
    let before = pile.snapshot().unwrap();
    // The image is ahead of its input, and that no longer blocks anything:
    // the target's leaf reads no source row, so the read stands on the
    // target's own records and evidence, and those are all it depends on.
    let observed = before.collection(fixture.target).unwrap();
    assert_eq!(
        observed.cover().members().collect::<Vec<_>>(),
        [fixture.output.get_handle()]
    );
    assert!(observed.is_current(&before));

    let unrelated_descriptor = descriptor::naming::<SimpleArchive>(
        "unrelated-observation-source",
        policy(&fixture.producer),
    );
    let unrelated = descriptor::put_closure(&mut pile, &unrelated_descriptor).unwrap();
    let unrelated_blob: Blob<SimpleArchive> = facts(7, 27).to_blob();
    pile.put::<SimpleArchive, _>(unrelated_blob.clone())
        .unwrap();
    pile.insert(CollectionRecord::Commit(CollectionCommit::sign(
        &fixture.producer,
        unrelated,
        data(&unrelated_blob),
        fixture.metadata.get_handle(),
    )))
    .unwrap();
    let after_unrelated = pile.snapshot().unwrap();
    assert!(observed.is_current(&after_unrelated));

    // The input's COMMIT landing in the source is not something the target
    // read: the observation stays current, and a new one reads the same.
    pile.insert(CollectionRecord::Commit(fixture.commit))
        .unwrap();
    let after_commit = pile.snapshot().unwrap();
    assert!(observed.is_current(&after_commit));
    let complete = after_commit.collection(fixture.target).unwrap();
    assert_eq!(complete.cover(), observed.cover());
    assert!(complete.is_current(&after_commit));

    // Nor does a second source commit, even once what the view stands for
    // in its source is asked: that is read through the leaves by locator
    // from the store, and its own support charges nothing beyond its rows.
    let second_blob: Blob<SimpleArchive> = facts(8, 28).to_blob();
    pile.insert(CollectionRecord::Commit(CollectionCommit::sign(
        &fixture.source_owner,
        fixture.source.handle(),
        data(&second_blob),
        fixture.metadata.get_handle(),
    )))
    .unwrap();
    let after_second = pile.snapshot().unwrap();
    assert!(complete.is_current(&after_second));
    assert_eq!(complete.support().unwrap().len(), 1);
    let support = &crate::collection::test_support::stood_for(&complete);
    assert_eq!(support.collection(), fixture.source);
    assert_eq!(support.len(), 1);
    assert!(support.contains(fixture.source_blob.get_handle()));
    assert!(!after_second
        .contains_blob(fixture.source_blob.get_handle())
        .unwrap());
    assert!(complete.is_current(&after_second));
    assert!(complete.is_current(&after_commit));

    // A new target equation with the same visible bytes invalidates the
    // observation too: target membership, not only output hashes, was read.
    let fresh = after_second.collection(fixture.target).unwrap();
    pile.insert(CollectionRecord::Merge(
        CollectionMerge::sign(
            &fixture.producer,
            fixture.target.handle(),
            // An absorbing merge whose other input is not here: it adds no
            // row, yet it is a new target record the observation read past.
            [
                data(&fixture.output),
                crate::collection::CollectionData::new([0xAB; 32]),
            ],
            data(&fixture.output),
        )
        .unwrap(),
    ))
    .unwrap();
    let after_target = pile.snapshot().unwrap();
    assert!(!fresh.is_current(&after_target));
    assert!(fresh.is_current(&after_second));
    assert_eq!(
        after_target
            .collection(fixture.target)
            .unwrap()
            .view::<TribleSet>()
            .unwrap(),
        facts(1, 21)
    );
}

#[test]
fn pile_observation_tracks_a_missing_selected_output_occurrence() {
    let fixture = one_hop();
    let dir = tempfile::tempdir().unwrap();
    let mut pile = empty_pile(&dir);
    assert_eq!(
        pile.derive::<TestImage>(fixture.source, (), policy(&fixture.producer))
            .unwrap(),
        fixture.target
    );
    descriptor::put_closure(&mut pile, &fixture.source_descriptor).unwrap();
    pile.insert(CollectionRecord::Commit(fixture.commit))
        .unwrap();
    pile.insert(fixture.equation).unwrap();
    let before = pile.snapshot().unwrap();
    let observed = before.collection(fixture.target).unwrap();
    assert!(observed.cover().is_empty());
    assert!(observed.view::<TribleSet>().unwrap().is_empty());
    assert!(observed.is_current(&before));

    pile.put::<SimpleArchive, _>(facts(8, 28).to_blob())
        .unwrap();
    let unrelated = pile.snapshot().unwrap();
    assert!(observed.is_current(&unrelated));
    pile.put::<TestImage, _>(fixture.output.clone()).unwrap();
    let after = pile.snapshot().unwrap();
    assert!(!observed.is_current(&after));
    assert!(observed.is_current(&before));
    assert!(observed.cover().is_empty());
    assert!(!before.contains_blob(fixture.output.get_handle()).unwrap());
    assert_eq!(
        before
            .records()
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap(),
        after
            .records()
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap()
    );
    let recovered = after.collection(fixture.target).unwrap();
    assert!(recovered.cover().contains(fixture.output.get_handle()));
    assert_eq!(recovered.view::<TribleSet>().unwrap(), facts(1, 21));
}

#[test]
fn pile_observation_tracks_missing_target_write_definition_without_new_records() {
    let mut fixture = one_hop();
    let dir = tempfile::tempdir().unwrap();
    let mut pile = empty_pile(&dir);
    let fixture_snapshot = fixture.store.snapshot().unwrap();
    let target_descriptor: Blob<SimpleArchive> =
        fixture_snapshot.get(fixture.target.handle()).unwrap();
    let read_definition: Blob<SimpleArchive> = fixture_snapshot.get(read_capability()).unwrap();
    let write_definition: Blob<SimpleArchive> = fixture_snapshot.get(write_capability()).unwrap();
    // Copy exact descriptor bytes, not their closure, to isolate one missing
    // policy definition. The signed records and the target payload are
    // already here.
    pile.put::<SimpleArchive, _>(target_descriptor).unwrap();
    pile.put::<SimpleArchive, _>(fixture.source_descriptor.facts().clone())
        .unwrap();
    pile.put::<SimpleArchive, _>(read_definition).unwrap();
    pile.put::<TestImage, _>(fixture.output.clone()).unwrap();
    pile.insert(CollectionRecord::Commit(fixture.commit))
        .unwrap();
    pile.insert(fixture.equation).unwrap();
    let before = pile.snapshot().unwrap();
    assert!(!before.contains_blob(write_capability()).unwrap());
    let observed = before.collection(fixture.target).unwrap();
    assert!(observed.cover().is_empty());
    assert!(observed.is_current(&before));
    pile.put::<SimpleArchive, _>(facts(9, 29).to_blob())
        .unwrap();
    let unrelated = pile.snapshot().unwrap();
    assert!(observed.is_current(&unrelated));

    pile.put::<SimpleArchive, _>(write_definition).unwrap();
    let after = pile.snapshot().unwrap();
    assert!(!observed.is_current(&after));
    assert!(observed.is_current(&before));
    assert!(observed.cover().is_empty());
    assert!(!before.contains_blob(write_capability()).unwrap());
    assert_eq!(
        before
            .records()
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap(),
        after
            .records()
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap()
    );
    assert_eq!(after.proofs().unwrap().count(), 0);
    let recovered = after.collection(fixture.target).unwrap();
    assert!(recovered.cover().contains(fixture.output.get_handle()));
    assert_eq!(recovered.view::<TribleSet>().unwrap(), facts(1, 21));
}