holochain_cascade 0.7.0-dev.32

Logic for cascading updates to Holochain state and network interaction
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
use super::*;
use ::fixt::fixt;
use holo_hash::fixt::AgentPubKeyFixturator;
use holochain_zome_types::action::{Action, ActionHashed, ChainTopOrdering};

async fn empty_store() -> holochain_state::dht_store::DhtStore {
    let dna_hash = holo_hash::DnaHash::from_raw_36(vec![42u8; 36]);
    holochain_state::test_utils::test_dht_store(dna_hash).await
}

/// A `dht_get_action` call on a `CascadeImpl` built with a `DhtStore` plus a
/// scratch that holds an authored action must return that action without
/// reaching the network (the cascade has no network handle).
#[tokio::test]
async fn dht_get_action_reflects_scratch_action() {
    let store = empty_store().await;

    // Build a signed action with no associated entry (Dna action type).
    let action = Action::Dna(fixt!(Dna));
    let sah = SignedActionHashed::with_presigned(
        ActionHashed::from_content_sync(action),
        fixt!(Signature),
    );
    let action_hash = sah.as_hash().clone();

    // Put the action into the scratch.
    let mut scratch = Scratch::new();
    scratch.add_action(sah.clone(), ChainTopOrdering::Relaxed);
    let sync_scratch = scratch.into_sync();

    // Construct a cascade with the DhtStore + scratch, but NO network.
    let cascade = CascadeImpl::empty(store).with_scratch(sync_scratch);

    let result = cascade
        .dht_get_action(action_hash.clone(), GetOptions::local())
        .await
        .expect("dht_get_action");

    let record = result.expect("expected Some(Record) from scratch");
    assert_eq!(
        record.action_address(),
        &action_hash,
        "returned action hash must match the scratched action"
    );
}

/// A `dht_get_entry` call on a `CascadeImpl` with a scratch holding a
/// `Create` action + entry returns that record without a network request.
#[tokio::test]
async fn dht_get_entry_reflects_scratch_create() {
    use holochain_zome_types::action::{Create, EntryType};
    use holochain_zome_types::entry::Entry;
    use holochain_zome_types::prelude::EntryHashed;

    let store = empty_store().await;

    // Build an agent entry (simplest public entry type).
    let agent = fixt!(AgentPubKey);
    let entry = Entry::Agent(agent.clone());
    let entry_hash = holo_hash::EntryHash::with_data_sync(&entry);

    let create_action = Action::Create(Create {
        author: agent.clone(),
        timestamp: holochain_zome_types::prelude::Timestamp::from_micros(0),
        action_seq: 1,
        prev_action: holo_hash::ActionHash::from_raw_36(vec![0u8; 36]),
        entry_type: EntryType::AgentPubKey,
        entry_hash: entry_hash.clone(),
        weight: Default::default(),
    });
    let sah = SignedActionHashed::with_presigned(
        ActionHashed::from_content_sync(create_action),
        fixt!(Signature),
    );

    let mut scratch = Scratch::new();
    scratch.add_action(sah.clone(), ChainTopOrdering::Relaxed);
    let entry_hashed = EntryHashed::from_content_sync(entry);
    scratch.add_entry(entry_hashed, ChainTopOrdering::Relaxed);
    let sync_scratch = scratch.into_sync();

    let cascade = CascadeImpl::empty(store).with_scratch(sync_scratch);

    let result = cascade
        .dht_get_entry(entry_hash.clone(), GetOptions::local())
        .await
        .expect("dht_get_entry");

    let record = result.expect("expected Some(Record) for scratch entry");
    assert_eq!(
        record.action().entry_hash(),
        Some(&entry_hash),
        "returned record entry hash must match the scratched entry"
    );
}

/// Helper: integrate a `StoreRecord` op for a `Create` action so that
/// `get_record_details_with_scratch` can locate the action via its store gate.
///
/// The entry is included in the op so `retrieve_record` can locate it in
/// the database when assembling `RecordDetails`.
async fn integrate_store_record(
    store: &holochain_state::dht_store::DhtStore,
    seed: u8,
    author: &AgentPubKey,
    entry_hash: holo_hash::EntryHash,
    entry: holochain_zome_types::entry::Entry,
) -> ActionHash {
    use holochain_state::dht_store::{AppOutcome, SysOutcome};
    use holochain_types::dht_op::{ChainOp, DhtOp, DhtOpHashed};
    use holochain_types::prelude::RecordEntry;
    use holochain_zome_types::action::{AppEntryDef, Create, EntryType};
    use holochain_zome_types::entry_def::EntryVisibility;
    use holochain_zome_types::prelude::Signature;

    let action = Action::Create(Create {
        author: author.clone(),
        timestamp: holochain_zome_types::prelude::Timestamp::from_micros(seed as i64 * 1000),
        action_seq: 1,
        prev_action: holo_hash::ActionHash::from_raw_36(vec![seed.wrapping_add(200); 36]),
        entry_type: EntryType::App(AppEntryDef::new(
            0.into(),
            0.into(),
            EntryVisibility::Public,
        )),
        entry_hash: entry_hash.clone(),
        weight: Default::default(),
    });
    let action_hash = holo_hash::ActionHash::with_data_sync(&action);
    let chain_op = ChainOp::StoreRecord(
        Signature::from([seed; 64]),
        action,
        RecordEntry::Present(entry),
    );
    let op = DhtOpHashed::from_content_sync(DhtOp::ChainOp(Box::new(chain_op)));
    let op_hash = op.as_hash().clone();
    store.record_incoming_ops(vec![(op, false)]).await.unwrap();
    store
        .record_chain_op_sys_validation_outcomes(vec![(op_hash.clone(), SysOutcome::Accepted)])
        .await
        .unwrap();
    store
        .record_app_validation_outcomes(vec![(op_hash.clone(), AppOutcome::Accepted)])
        .await
        .unwrap();
    store
        .integrate_ready_ops(holochain_zome_types::prelude::Timestamp::from_micros(1000))
        .await
        .unwrap();
    action_hash
}

/// Helper: integrate a `StoreEntry` op for a `Create` action.
async fn integrate_store_entry(
    store: &holochain_state::dht_store::DhtStore,
    seed: u8,
    author: &AgentPubKey,
    entry_hash: holo_hash::EntryHash,
    entry: holochain_zome_types::entry::Entry,
) -> ActionHash {
    use holochain_state::dht_store::{AppOutcome, SysOutcome};
    use holochain_types::action::NewEntryAction;
    use holochain_types::dht_op::{ChainOp, DhtOp, DhtOpHashed};
    use holochain_zome_types::action::{AppEntryDef, Create, EntryType};
    use holochain_zome_types::entry_def::EntryVisibility;
    use holochain_zome_types::prelude::Signature;

    let action = Action::Create(Create {
        author: author.clone(),
        timestamp: holochain_zome_types::prelude::Timestamp::from_micros(seed as i64 * 1000),
        action_seq: 1,
        prev_action: holo_hash::ActionHash::from_raw_36(vec![seed.wrapping_add(200); 36]),
        entry_type: EntryType::App(AppEntryDef::new(
            0.into(),
            0.into(),
            EntryVisibility::Public,
        )),
        entry_hash: entry_hash.clone(),
        weight: Default::default(),
    });
    let action_hash = holo_hash::ActionHash::with_data_sync(&action);
    let new_entry_action = match action {
        Action::Create(c) => NewEntryAction::Create(c),
        _ => unreachable!(),
    };
    let chain_op = ChainOp::StoreEntry(Signature::from([seed; 64]), new_entry_action, entry);
    let op = DhtOpHashed::from_content_sync(DhtOp::ChainOp(Box::new(chain_op)));
    let op_hash = op.as_hash().clone();
    store.record_incoming_ops(vec![(op, false)]).await.unwrap();
    store
        .record_chain_op_sys_validation_outcomes(vec![(op_hash.clone(), SysOutcome::Accepted)])
        .await
        .unwrap();
    store
        .record_app_validation_outcomes(vec![(op_hash.clone(), AppOutcome::Accepted)])
        .await
        .unwrap();
    store
        .integrate_ready_ops(holochain_zome_types::prelude::Timestamp::from_micros(1000))
        .await
        .unwrap();
    action_hash
}

/// `get_record_details` on a `CascadeImpl` with a `DhtStore` plus a scratch
/// that holds a `Delete` targeting an integrated record must return
/// `RecordDetails` that includes the scratch delete.
#[tokio::test]
async fn get_record_details_reflects_scratch_delete() {
    use holochain_zome_types::action::Delete;
    use holochain_zome_types::entry::Entry;

    let store = empty_store().await;

    let seed = 42u8;
    let author = AgentPubKey::from_raw_36(vec![seed; 36]);

    // Build an Agent entry so retrieve_record can fetch it from the DB.
    let agent_key = AgentPubKey::from_raw_36(vec![seed.wrapping_add(50); 36]);
    let entry = Entry::Agent(agent_key.clone());
    let entry_hash = holo_hash::EntryHash::with_data_sync(&entry);

    // Integrate a StoreRecord op so get_record_details_with_scratch can find it.
    let action_hash =
        integrate_store_record(&store, seed, &author, entry_hash.clone(), entry).await;

    // Put a scratch Delete targeting the integrated action into the scratch.
    let delete = Action::Delete(Delete {
        author: AgentPubKey::from_raw_36(vec![seed.wrapping_add(10); 36]),
        timestamp: holochain_zome_types::prelude::Timestamp::from_micros(seed as i64 * 3000),
        action_seq: 3,
        prev_action: holo_hash::ActionHash::from_raw_36(vec![seed.wrapping_add(160); 36]),
        deletes_address: action_hash.clone(),
        deletes_entry_address: entry_hash.clone(),
        weight: Default::default(),
    });
    let delete_sah = SignedActionHashed::with_presigned(
        ActionHashed::from_content_sync(delete),
        fixt!(Signature),
    );

    let mut scratch = Scratch::new();
    scratch.add_action(delete_sah, ChainTopOrdering::Relaxed);
    let sync_scratch = scratch.into_sync();

    let cascade = CascadeImpl::empty(store).with_scratch(sync_scratch);

    let options = CascadeOptions {
        get_options: GetOptions::local(),
        network_request_options: GetOptions::local().to_network_options(),
    };

    let details = cascade
        .get_record_details(action_hash.clone(), options)
        .await
        .expect("get_record_details")
        .expect("expected Some(RecordDetails) for integrated record");

    assert_eq!(
        details.deletes.len(),
        1,
        "scratch Delete must appear in RecordDetails.deletes"
    );
    assert_eq!(details.updates.len(), 0, "no updates expected");
}

/// `get_entry_details` on a `CascadeImpl` with a `DhtStore` plus a scratch
/// holding a `Delete` targeting the entry must return `EntryDetails` where
/// the scratch delete appears and `entry_dht_status` is `Dead`.
#[tokio::test]
async fn get_entry_details_reflects_scratch_delete() {
    use holochain_zome_types::action::Delete;
    use holochain_zome_types::entry::Entry;
    use holochain_zome_types::metadata::EntryDhtStatus;

    let store = empty_store().await;

    let seed = 43u8;
    let author = AgentPubKey::from_raw_36(vec![seed; 36]);

    // Use an Agent entry as the simplest public entry type.
    let agent_key = AgentPubKey::from_raw_36(vec![seed.wrapping_add(50); 36]);
    let entry = Entry::Agent(agent_key.clone());
    let entry_hash = holo_hash::EntryHash::with_data_sync(&entry);

    // Integrate a StoreEntry op so the entry exists in the store and is Live.
    let store_action_hash =
        integrate_store_entry(&store, seed, &author, entry_hash.clone(), entry).await;

    // Put a scratch Delete targeting that action into the scratch so the
    // entry becomes Dead.
    let delete = Action::Delete(Delete {
        author: AgentPubKey::from_raw_36(vec![seed.wrapping_add(10); 36]),
        timestamp: holochain_zome_types::prelude::Timestamp::from_micros(seed as i64 * 3000),
        action_seq: 3,
        prev_action: holo_hash::ActionHash::from_raw_36(vec![seed.wrapping_add(160); 36]),
        deletes_address: store_action_hash.clone(),
        deletes_entry_address: entry_hash.clone(),
        weight: Default::default(),
    });
    let delete_sah = SignedActionHashed::with_presigned(
        ActionHashed::from_content_sync(delete),
        fixt!(Signature),
    );

    let mut scratch = Scratch::new();
    scratch.add_action(delete_sah, ChainTopOrdering::Relaxed);
    let sync_scratch = scratch.into_sync();

    let cascade = CascadeImpl::empty(store).with_scratch(sync_scratch);

    let options = CascadeOptions {
        get_options: GetOptions::local(),
        network_request_options: GetOptions::local().to_network_options(),
    };

    let details = cascade
        .get_entry_details(entry_hash.clone(), options)
        .await
        .expect("get_entry_details")
        .expect("expected Some(EntryDetails)");

    assert_eq!(
        details.deletes.len(),
        1,
        "scratch Delete must appear in EntryDetails.deletes"
    );
    assert_eq!(
        details.entry_dht_status,
        EntryDhtStatus::Dead,
        "entry must be Dead after scratch Delete"
    );
}

/// Helper: integrate a `RegisterAddLink` op into the store, returning the action hash.
async fn integrate_link_op(
    store: &holochain_state::dht_store::DhtStore,
    base: &holo_hash::AnyLinkableHash,
    zome_index: u8,
    link_type: u8,
    tag_bytes: Vec<u8>,
    seed: u8,
) -> holo_hash::ActionHash {
    use holochain_state::dht_store::{AppOutcome, SysOutcome};
    use holochain_types::dht_op::{ChainOp, DhtOp, DhtOpHashed};
    use holochain_zome_types::action::CreateLink;
    use holochain_zome_types::link::LinkTag;

    let action = Action::CreateLink(CreateLink {
        author: AgentPubKey::from_raw_36(vec![seed; 36]),
        timestamp: holochain_zome_types::prelude::Timestamp::from_micros(seed as i64 * 1000),
        action_seq: 2,
        prev_action: holo_hash::ActionHash::from_raw_36(vec![seed.wrapping_add(60); 36]),
        base_address: base.clone(),
        target_address: holo_hash::AnyLinkableHash::from_raw_36_and_type(
            vec![seed.wrapping_add(20); 36],
            holo_hash::hash_type::AnyLinkable::Entry,
        ),
        zome_index: zome_index.into(),
        link_type: link_type.into(),
        tag: LinkTag(tag_bytes),
        weight: Default::default(),
    });
    let create_link = match action {
        Action::CreateLink(ref cl) => cl.clone(),
        _ => unreachable!(),
    };
    let action_hash = holo_hash::ActionHash::with_data_sync(&action);
    let op = DhtOpHashed::from_content_sync(DhtOp::ChainOp(Box::new(ChainOp::RegisterAddLink(
        Signature::from([seed; 64]),
        create_link,
    ))));
    let op_hash = op.as_hash().clone();
    store.record_incoming_ops(vec![(op, false)]).await.unwrap();
    store
        .record_chain_op_sys_validation_outcomes(vec![(op_hash.clone(), SysOutcome::Accepted)])
        .await
        .unwrap();
    store
        .record_app_validation_outcomes(vec![(op_hash, AppOutcome::Accepted)])
        .await
        .unwrap();
    store
        .integrate_ready_ops(holochain_zome_types::prelude::Timestamp::from_micros(1000))
        .await
        .unwrap();
    action_hash
}

/// Helper: build a scratch `CreateLink` action for `base`.
fn make_scratch_create_link(
    base: &holo_hash::AnyLinkableHash,
    zome_index: u8,
    link_type: u8,
    tag_bytes: Vec<u8>,
    seed: u8,
) -> SignedActionHashed {
    use holochain_zome_types::action::CreateLink;
    use holochain_zome_types::link::LinkTag;

    let action = Action::CreateLink(CreateLink {
        author: AgentPubKey::from_raw_36(vec![seed; 36]),
        timestamp: holochain_zome_types::prelude::Timestamp::from_micros(seed as i64 * 1000),
        action_seq: 2,
        prev_action: holo_hash::ActionHash::from_raw_36(vec![seed.wrapping_add(60); 36]),
        base_address: base.clone(),
        target_address: holo_hash::AnyLinkableHash::from_raw_36_and_type(
            vec![seed.wrapping_add(20); 36],
            holo_hash::hash_type::AnyLinkable::Entry,
        ),
        zome_index: zome_index.into(),
        link_type: link_type.into(),
        tag: LinkTag(tag_bytes),
        weight: Default::default(),
    });
    SignedActionHashed::with_presigned(
        ActionHashed::from_content_sync(action),
        Signature::from([seed; 64]),
    )
}

/// Helper: build a scratch `DeleteLink` action tombstoning `create_link_hash`.
fn make_scratch_delete_link(
    base: &holo_hash::AnyLinkableHash,
    create_link_hash: holo_hash::ActionHash,
    seed: u8,
) -> SignedActionHashed {
    use holochain_zome_types::action::DeleteLink;

    let action = Action::DeleteLink(DeleteLink {
        author: AgentPubKey::from_raw_36(vec![seed; 36]),
        timestamp: holochain_zome_types::prelude::Timestamp::from_micros(seed as i64 * 1000 + 500),
        action_seq: 3,
        prev_action: holo_hash::ActionHash::from_raw_36(vec![seed.wrapping_add(90); 36]),
        base_address: base.clone(),
        link_add_address: create_link_hash,
    });
    SignedActionHashed::with_presigned(
        ActionHashed::from_content_sync(action),
        Signature::from([seed; 64]),
    )
}

/// `dht_get_links` via a `CascadeImpl` with a `DhtStore`:
///
/// - A scratch `CreateLink` appears in the result.
/// - A scratch `DeleteLink` tombstoning an integrated store link removes it.
#[tokio::test]
async fn dht_get_links_reflects_scratch_create_and_delete() {
    use holochain_p2p::actor::GetLinksRequestOptions;
    use holochain_zome_types::prelude::LinkTypeFilter;

    let store = empty_store().await;

    let base = holo_hash::AnyLinkableHash::from_raw_36_and_type(
        vec![50u8; 36],
        holo_hash::hash_type::AnyLinkable::Entry,
    );

    // Integrate a store CreateLink for base (seed 51).
    let store_create_hash = integrate_link_op(&store, &base, 0, 0, vec![1, 2, 3], 51).await;

    // Add a scratch CreateLink for the same base (seed 52).
    let scratch_create_sah = make_scratch_create_link(&base, 0, 0, vec![1, 2, 3], 52);
    let scratch_create_hash = scratch_create_sah.as_hash().clone();

    // Add a scratch DeleteLink tombstoning the store CreateLink (seed 53).
    let scratch_delete_sah = make_scratch_delete_link(&base, store_create_hash.clone(), 53);

    let mut scratch = Scratch::new();
    scratch.add_action(scratch_create_sah, ChainTopOrdering::Relaxed);
    scratch.add_action(scratch_delete_sah, ChainTopOrdering::Relaxed);
    let sync_scratch = scratch.into_sync();

    let cascade = CascadeImpl::empty(store).with_scratch(sync_scratch);

    let key = holochain_types::link::WireLinkKey {
        base: base.clone(),
        type_query: LinkTypeFilter::Dependencies(vec![0.into()]),
        tag: None,
        after: None,
        before: None,
        author: None,
    };
    let options = GetLinksRequestOptions {
        get_options: GetOptions::local(),
        ..Default::default()
    };

    let links = cascade
        .dht_get_links(key, options)
        .await
        .expect("dht_get_links");

    assert_eq!(links.len(), 1, "only the scratch CreateLink must be live");
    assert_eq!(
        links[0].create_link_hash, scratch_create_hash,
        "surviving link must be the scratch CreateLink"
    );
}

/// `get_links_details` via a `CascadeImpl` with a `DhtStore`:
///
/// - Shows a scratch `CreateLink`.
/// - Shows a scratch `DeleteLink` paired with its store `CreateLink`.
#[tokio::test]
async fn get_links_details_reflects_scratch_create_and_delete() {
    use holochain_p2p::actor::GetLinksRequestOptions;
    use holochain_zome_types::prelude::LinkTypeFilter;

    let store = empty_store().await;

    let base = holo_hash::AnyLinkableHash::from_raw_36_and_type(
        vec![60u8; 36],
        holo_hash::hash_type::AnyLinkable::Entry,
    );

    // Integrate a store CreateLink (seed 61) — this will be tombstoned.
    let store_create_hash = integrate_link_op(&store, &base, 0, 0, vec![4, 5, 6], 61).await;

    // Add a scratch CreateLink (seed 62) — this will be live.
    let scratch_create_sah = make_scratch_create_link(&base, 0, 0, vec![4, 5, 6], 62);
    let scratch_create_hash = scratch_create_sah.as_hash().clone();

    // Add a scratch DeleteLink (seed 63) tombstoning the store create.
    let scratch_delete_sah = make_scratch_delete_link(&base, store_create_hash.clone(), 63);
    let scratch_delete_hash = scratch_delete_sah.as_hash().clone();

    let mut scratch = Scratch::new();
    scratch.add_action(scratch_create_sah, ChainTopOrdering::Relaxed);
    scratch.add_action(scratch_delete_sah, ChainTopOrdering::Relaxed);
    let sync_scratch = scratch.into_sync();

    let cascade = CascadeImpl::empty(store).with_scratch(sync_scratch);

    let key = holochain_types::link::WireLinkKey {
        base: base.clone(),
        type_query: LinkTypeFilter::Dependencies(vec![0.into()]),
        tag: None,
        after: None,
        before: None,
        author: None,
    };
    let options = GetLinksRequestOptions {
        get_options: GetOptions::local(),
        ..Default::default()
    };

    let details = cascade
        .get_links_details(key, options)
        .await
        .expect("get_links_details");

    // Expect two creates: store create (tombstoned) + scratch create.
    assert_eq!(details.len(), 2, "both creates must appear in details");

    // Find the store create entry in details.
    let store_entry = details
        .iter()
        .find(|(sah, _)| sah.as_hash() == &store_create_hash)
        .expect("store CreateLink must appear in details");
    assert_eq!(
        store_entry.1.len(),
        1,
        "store CreateLink must have the scratch DeleteLink as its tombstone"
    );
    assert_eq!(
        store_entry.1[0].as_hash(),
        &scratch_delete_hash,
        "tombstone must be the scratch DeleteLink"
    );

    // Find the scratch create entry in details.
    let scratch_entry = details
        .iter()
        .find(|(sah, _)| sah.as_hash() == &scratch_create_hash)
        .expect("scratch CreateLink must appear in details");
    assert_eq!(
        scratch_entry.1.len(),
        0,
        "scratch CreateLink must have no deletes"
    );
}

/// `dht_count_links` via a `CascadeImpl` with a `DhtStore` (no network):
/// the scratch `CreateLink` is counted, and a scratch `DeleteLink`
/// tombstones the store `CreateLink`, so the unique count is 1.
#[tokio::test]
async fn dht_count_links_reflects_scratch_create_and_delete() {
    use holochain_zome_types::prelude::LinkTypeFilter;

    let store = empty_store().await;
    let base = holo_hash::AnyLinkableHash::from_raw_36_and_type(
        vec![60u8; 36],
        holo_hash::hash_type::AnyLinkable::Entry,
    );

    // Store CreateLink (seed 61), a scratch CreateLink (62), and a scratch
    // DeleteLink (63) tombstoning the store create.
    let store_create_hash = integrate_link_op(&store, &base, 0, 0, vec![1, 2, 3], 61).await;
    let scratch_create_sah = make_scratch_create_link(&base, 0, 0, vec![1, 2, 3], 62);
    let scratch_delete_sah = make_scratch_delete_link(&base, store_create_hash, 63);

    let mut scratch = Scratch::new();
    scratch.add_action(scratch_create_sah, ChainTopOrdering::Relaxed);
    scratch.add_action(scratch_delete_sah, ChainTopOrdering::Relaxed);
    let sync_scratch = scratch.into_sync();

    let cascade = CascadeImpl::empty(store).with_scratch(sync_scratch);

    let query = holochain_types::link::WireLinkQuery {
        base: base.clone(),
        link_type: LinkTypeFilter::Dependencies(vec![0.into()]),
        tag_prefix: None,
        before: None,
        after: None,
        author: None,
    };

    let count = cascade
        .dht_count_links(query)
        .await
        .expect("dht_count_links");
    assert_eq!(
        count, 1,
        "store create is tombstoned by the scratch delete; only the scratch create counts"
    );
}

// ---- agent-activity helpers ----

/// Integrate a `RegisterAgentActivity` op for the given `action` into the
/// store so it is marked accepted and integrated.
async fn integrate_activity_op(
    store: &holochain_state::dht_store::DhtStore,
    action: Action,
    seed: u8,
    when: i64,
) -> holo_hash::ActionHash {
    use holochain_state::dht_store::{AppOutcome, SysOutcome};
    use holochain_types::dht_op::{ChainOp, DhtOp, DhtOpHashed};

    let action_hash = holo_hash::ActionHash::with_data_sync(&action);
    let op = DhtOpHashed::from_content_sync(DhtOp::ChainOp(Box::new(
        ChainOp::RegisterAgentActivity(Signature::from([seed; 64]), action),
    )));
    let op_hash = op.as_hash().clone();
    store.record_incoming_ops(vec![(op, false)]).await.unwrap();
    store
        .record_chain_op_sys_validation_outcomes(vec![(op_hash.clone(), SysOutcome::Accepted)])
        .await
        .unwrap();
    store
        .record_app_validation_outcomes(vec![(op_hash, AppOutcome::Accepted)])
        .await
        .unwrap();
    store
        .integrate_ready_ops(holochain_zome_types::prelude::Timestamp::from_micros(when))
        .await
        .unwrap();
    action_hash
}

/// Build a `Create` action (no entry in store) for use as agent activity.
fn make_activity_create(
    author: &AgentPubKey,
    seq: u32,
    prev: &holo_hash::ActionHash,
    seed: u8,
) -> Action {
    use holochain_zome_types::action::{AppEntryDef, Create, EntryType};
    use holochain_zome_types::entry_def::EntryVisibility;

    Action::Create(Create {
        author: author.clone(),
        timestamp: holochain_zome_types::prelude::Timestamp::from_micros((seq as i64 + 1) * 1000),
        action_seq: seq,
        prev_action: prev.clone(),
        entry_type: EntryType::App(AppEntryDef::new(
            0.into(),
            0.into(),
            EntryVisibility::Public,
        )),
        entry_hash: holo_hash::EntryHash::from_raw_36(vec![seed.wrapping_add(100); 36]),
        weight: Default::default(),
    })
}

/// Wrap an action as a `SignedActionHashed` for the scratch.
fn make_scratch_activity(action: Action, seed: u8) -> SignedActionHashed {
    SignedActionHashed::with_presigned(
        holochain_zome_types::action::ActionHashed::from_content_sync(action),
        Signature::from([seed; 64]),
    )
}

// ---- must_get_agent_activity tests ----

/// `must_get_agent_activity` via the cascade respects a scratch chain-top:
/// the chain top is in the scratch (not the store), so the response should
/// not be `ChainTopNotFound`.
#[tokio::test]
async fn must_get_agent_activity_reflects_scratch_chain_top() {
    let store = empty_store().await;
    let author = AgentPubKey::from_raw_36(vec![200u8; 36]);

    // Seq 0 in the store.
    let prev0 = holo_hash::ActionHash::from_raw_36(vec![0u8; 36]);
    let action0 = make_activity_create(&author, 0, &prev0, 200);
    let hash0 = integrate_activity_op(&store, action0, 200, 10).await;

    // Seq 1 (scratch-only): linked from hash0.
    let action1 = make_activity_create(&author, 1, &hash0, 201);
    let scratch_top_hash = holo_hash::ActionHash::with_data_sync(&action1);
    let sah1 = make_scratch_activity(action1, 201);

    let mut scratch = Scratch::new();
    scratch.add_action(sah1, ChainTopOrdering::Relaxed);
    let sync_scratch = scratch.into_sync();

    let cascade = CascadeImpl::empty(store).with_scratch(sync_scratch);

    let filter = ChainFilter::new(scratch_top_hash.clone());
    let resp = cascade
        .must_get_agent_activity(author, filter, NetworkRequestOptions::default())
        .await
        .expect("must_get_agent_activity");

    assert!(
        !matches!(resp, MustGetAgentActivityResponse::ChainTopNotFound(_)),
        "scratch chain-top should have been resolved; got {resp:?}"
    );
}

/// `must_get_agent_activity` via the cascade includes scratch activity in
/// the merged result when the full chain is present (store + scratch).
#[tokio::test]
async fn must_get_agent_activity_reflects_scratch_activity() {
    let store = empty_store().await;
    let author = AgentPubKey::from_raw_36(vec![202u8; 36]);

    // Seqs 0..=1 in the store.
    let prev0 = holo_hash::ActionHash::from_raw_36(vec![0u8; 36]);
    let action0 = make_activity_create(&author, 0, &prev0, 202);
    let hash0 = integrate_activity_op(&store, action0, 202, 10).await;
    let action1 = make_activity_create(&author, 1, &hash0, 203);
    let hash1 = integrate_activity_op(&store, action1, 203, 11).await;

    // Seq 2 scratch-only, linked from hash1.
    let action2 = make_activity_create(&author, 2, &hash1, 204);
    let scratch_top_hash = holo_hash::ActionHash::with_data_sync(&action2);
    let sah2 = make_scratch_activity(action2, 204);

    let mut scratch = Scratch::new();
    scratch.add_action(sah2, ChainTopOrdering::Relaxed);
    let sync_scratch = scratch.into_sync();

    let cascade = CascadeImpl::empty(store).with_scratch(sync_scratch);

    let filter = ChainFilter::new(scratch_top_hash.clone());
    let resp = cascade
        .must_get_agent_activity(author, filter, NetworkRequestOptions::default())
        .await
        .expect("must_get_agent_activity");

    match resp {
        MustGetAgentActivityResponse::Activity { activity, .. } => {
            let seqs: Vec<u32> = activity.iter().map(|a| a.action.seq()).collect();
            assert!(
                seqs.contains(&2),
                "scratch action at seq 2 should be present; got {seqs:?}"
            );
            assert!(
                seqs.contains(&0),
                "store action at seq 0 should be present; got {seqs:?}"
            );
        }
        other => panic!("expected Activity, got {other:?}"),
    }
}

// ---- get_agent_activity tests ----

/// `get_agent_activity` via the cascade (requester, Local strategy) returns
/// scratch activity merged with the store.
#[tokio::test]
async fn get_agent_activity_reflects_scratch_activity() {
    let store = empty_store().await;
    let author = AgentPubKey::from_raw_36(vec![210u8; 36]);

    // Seq 0 in the store.
    let prev0 = holo_hash::ActionHash::from_raw_36(vec![0u8; 36]);
    let action0 = make_activity_create(&author, 0, &prev0, 210);
    let hash0 = integrate_activity_op(&store, action0, 210, 10).await;

    // Seq 1 scratch-only, chained to the integrated seq-0 action.
    let action1 = make_activity_create(&author, 1, &hash0, 211);
    let sah1 = make_scratch_activity(action1, 211);

    let mut scratch = Scratch::new();
    scratch.add_action(sah1, ChainTopOrdering::Relaxed);
    let sync_scratch = scratch.into_sync();

    let cascade = CascadeImpl::empty(store).with_scratch(sync_scratch);

    let options = GetActivityOptions {
        include_valid_activity: true,
        include_rejected_activity: false,
        include_warrants: false,
        include_full_records: false,
        get_options: GetOptions::local(),
        ..Default::default()
    };
    let resp = cascade
        .get_agent_activity(author, ChainQueryFilter::new(), options)
        .await
        .expect("get_agent_activity");

    match &resp.valid_activity {
        ChainItems::Hashes(h) => {
            let seqs: Vec<u32> = h.iter().map(|(seq, _)| *seq).collect();
            assert!(
                seqs.contains(&0),
                "store action at seq 0 should be in valid activity; got {seqs:?}"
            );
            assert!(
                seqs.contains(&1),
                "scratch action at seq 1 should be in valid activity; got {seqs:?}"
            );
        }
        other => panic!("expected ChainItems::Hashes, got {other:?}"),
    }
}

/// A status-only request (`include_valid_activity` and `include_rejected_activity`
/// both false) must still return the real chain status, not `Empty`. Ported from
/// the pre-v2 cascade `get_agent_activity` integration tests.
#[tokio::test]
async fn get_agent_activity_status_only_returns_real_status() {
    let store = empty_store().await;
    let author = AgentPubKey::from_raw_36(vec![213u8; 36]);

    // Two integrated actions; seq 1 is the chain head.
    let prev0 = holo_hash::ActionHash::from_raw_36(vec![0u8; 36]);
    let action0 = make_activity_create(&author, 0, &prev0, 213);
    let hash0 = integrate_activity_op(&store, action0, 213, 10).await;

    let action1 = make_activity_create(&author, 1, &hash0, 214);
    let hash1 = integrate_activity_op(&store, action1, 214, 11).await;

    let cascade = CascadeImpl::empty(store);

    let options = GetActivityOptions {
        include_valid_activity: false,
        include_rejected_activity: false,
        include_warrants: false,
        include_full_records: false,
        get_options: GetOptions::local(),
        ..Default::default()
    };
    let resp = cascade
        .get_agent_activity(author, ChainQueryFilter::new(), options)
        .await
        .expect("get_agent_activity");

    assert_eq!(
        resp.status,
        ChainStatus::Valid(ChainHead {
            action_seq: 1,
            hash: hash1,
        }),
        "status-only request must return the real chain status"
    );
    assert_eq!(resp.valid_activity, ChainItems::NotRequested);
    assert_eq!(resp.rejected_activity, ChainItems::NotRequested);
}