fsqlite 0.4.6

Public API facade
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
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
//! Capture and durably receive authenticated database snapshots.
//!
//! The source is captured by `Connection::export_bytes`, not by reading a live
//! main file and WAL independently. Export owns its existing pager quiescence,
//! checkpoint and read-fence protocol. Once capture returns, packet generation
//! owns immutable encoded bytes and no source connection or database locks.
//! This is a whole-image bootstrap, not an incremental replication log.
//!
//! JournaledSnapshotBootstrap joins the saved packet stream to the guarded
//! SQL image installer, including replay of partial transfers after restart.

use std::fmt;

pub use fsqlite_core::replication_sender::SenderConfig;
use fsqlite_core::replication_sender::{PageEntry, ReplicationPacket};
use fsqlite_core::snapshot_shipping::SnapshotSender;
use fsqlite_error::{FrankenError, Result};
use fsqlite_types::cx::Cx;
use fsqlite_vfs::VfsFile;

use super::{
    BootstrapOptions, BootstrapProgress, Destination, SnapshotCheckpoint, SnapshotImageState,
    SnapshotManifest, SnapshotOpen, SnapshotPacketResult, SnapshotSpool, SnapshotSpoolState,
};
use crate::{Connection, ConnectionEnv};

/// Admission policy for the captured image and its packet coding schedule.
///
/// `max_image_bytes` is an image-admission limit, not an allocation/RSS cap:
/// the existing exporter materializes its image before this limit is checked.
/// Encoding subsequently holds page copies and encoded blocks; the codec has
/// its own workspace limit. Size the exporter/environment for the source too.
#[derive(Debug, Clone)]
pub struct CaptureOptions {
    pub max_image_bytes: usize,
    pub coding: SenderConfig,
}

impl Default for CaptureOptions {
    fn default() -> Self {
        Self {
            max_image_bytes: 64 * 1024 * 1024,
            coding: SenderConfig::default(),
        }
    }
}

impl CaptureOptions {
    fn preflight(&self) -> Result<()> {
        if self.max_image_bytes < 512 {
            return Err(FrankenError::TooBig);
        }
        ReplicationPacket::validate_symbol_size(usize::from(self.coding.symbol_size))?;
        if self.coding.max_isi_multiplier == 0 {
            return Err(FrankenError::OutOfRange {
                what: "max_isi_multiplier".to_owned(),
                value: "0".to_owned(),
            });
        }
        Ok(())
    }
}

/// Immutable source content and its fixed, authenticated packet schedule.
///
/// Supply `manifest_id()` to the receiver through a trusted control plane.
/// Sending a digest beside an untrusted manifest does not establish trust.
/// Packet authentication is mandatory here, but it does not encrypt database
/// contents: use a confidential transport where the data requires one.
#[must_use]
pub struct CapturedSnapshot {
    sender: SnapshotSender,
    manifest: SnapshotManifest,
    manifest_id: [u8; 32],
    image_bytes: u64,
    auth_key: [u8; 32],
}

impl fmt::Debug for CapturedSnapshot {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CapturedSnapshot")
            .field("manifest_id", &self.manifest_id)
            .field("image_bytes", &self.image_bytes)
            .field("blocks", &self.manifest.blocks().len())
            .finish_non_exhaustive()
    }
}

fn checkpoint(cx: &Cx) -> Result<()> {
    cx.checkpoint().map_err(|_| FrankenError::Interrupt)
}

fn corrupt(detail: &'static str) -> FrankenError {
    FrankenError::DatabaseCorrupt {
        detail: detail.to_owned(),
    }
}

/// Check the self-contained image envelope before allocating page copies.
/// Like the image installer, respect SQLite's legacy page-count authority rule.
/// This does not certify the B-trees, application schema, or source provenance.
fn image_geometry(image: &[u8], limit: usize) -> Result<(u32, u32)> {
    if image.len() > limit {
        return Err(FrankenError::TooBig);
    }
    if image.len() < 100 || &image[..16] != b"SQLite format 3\0" {
        return Err(corrupt("snapshot source has no SQLite database header"));
    }
    let encoded = u16::from_be_bytes([image[16], image[17]]);
    let page_size = if encoded == 1 {
        65_536
    } else {
        u32::from(encoded)
    };
    if !(512..=65_536).contains(&page_size)
        || !page_size.is_power_of_two()
        || !matches!((image[18], image[19]), (1, 1) | (2, 2))
        || image[21..24] != [64, 32, 32]
    {
        return Err(corrupt(
            "snapshot source has invalid geometry or journal format",
        ));
    }
    let size = usize::try_from(page_size).map_err(|_| FrankenError::TooBig)?;
    if image.len() < size || !image.len().is_multiple_of(size) {
        return Err(corrupt(
            "snapshot source contains an incomplete database page",
        ));
    }
    let page_count = u32::try_from(image.len() / size).map_err(|_| FrankenError::TooBig)?;
    if page_count == u32::MAX {
        return Err(FrankenError::TooBig);
    }
    let declared = u32::from_be_bytes(image[28..32].try_into().expect("header width"));
    if declared != 0 && image[24..28] == image[92..96] && declared != page_count {
        return Err(corrupt(
            "snapshot source header and physical page counts disagree",
        ));
    }
    Ok((page_size, page_count))
}

impl CapturedSnapshot {
    /// Capture `main` through the existing public export boundary.
    ///
    /// The exclusive borrow prevents SQL re-entry through this connection
    /// while capture is pending. Export settles abandoned transaction cleanup,
    /// refuses an active transaction, and may flush/checkpoint committed data.
    /// Contention and export failures propagate; no retry or implicit COMMIT
    /// of a caller's explicit transaction is introduced. TEMP and attachments
    /// are not included, and no database is opened by pathname here.
    ///
    /// Export uses the connection's configured context. `cx` controls admission
    /// and packetization; configure related lineages when both should share
    /// cancellation. Cancellation after export cannot undo its checkpoint.
    /// The image limit is checked *after* export; see `CaptureOptions`.
    pub async fn capture(
        cx: &Cx,
        connection: &mut Connection,
        auth_key: [u8; 32],
        options: CaptureOptions,
    ) -> Result<Self> {
        checkpoint(cx)?;
        options.preflight()?;
        let image = connection.export_bytes().await?;
        Self::from_image(cx, image, auth_key, options)
    }

    /// Packetize an already coherent, exclusively owned SQLite image.
    ///
    /// The caller is responsible for atomic source capture. Never obtain this
    /// input by copying a live main file without its pager snapshot/fence.
    /// Ownership prevents later caller mutation from changing the manifest or
    /// packets. Structural admission is not an integrity-check certificate.
    pub fn from_image(
        cx: &Cx,
        image: Vec<u8>,
        auth_key: [u8; 32],
        options: CaptureOptions,
    ) -> Result<Self> {
        checkpoint(cx)?;
        options.preflight()?;
        let (page_size, page_count) = image_geometry(&image, options.max_image_bytes)?;
        let image_bytes = u64::try_from(image.len()).map_err(|_| FrankenError::TooBig)?;
        let mut pages = Vec::new();
        pages
            .try_reserve_exact(usize::try_from(page_count).map_err(|_| FrankenError::TooBig)?)
            .map_err(|_| FrankenError::OutOfMemory)?;
        for (index, data) in image.chunks_exact(page_size as usize).enumerate() {
            checkpoint(cx)?;
            let number = u32::try_from(index + 1).map_err(|_| FrankenError::TooBig)?;
            let mut bytes = Vec::new();
            bytes
                .try_reserve_exact(data.len())
                .map_err(|_| FrankenError::OutOfMemory)?;
            bytes.extend_from_slice(data);
            pages.push(PageEntry::new(number, bytes));
        }
        // Do not retain a third database-sized copy during encoding.
        drop(image);
        let sender = SnapshotSender::prepare(page_size, &mut pages, options.coding)?;
        drop(pages);
        checkpoint(cx)?;
        let manifest = sender.manifest()?;
        let manifest_id = manifest.id();
        checkpoint(cx)?;
        Ok(Self {
            sender,
            manifest,
            manifest_id,
            image_bytes,
            auth_key,
        })
    }

    #[must_use]
    pub const fn manifest_id(&self) -> [u8; 32] {
        self.manifest_id
    }

    #[must_use]
    pub const fn manifest(&self) -> &SnapshotManifest {
        &self.manifest
    }

    #[must_use]
    pub const fn image_bytes(&self) -> u64 {
        self.image_bytes
    }

    /// Produce one owned, keyed V2 packet. Retain it until its send attempt is
    /// resolved; retransmitting the same packet is safe for the receiver.
    /// `None` ends this pass, not a receiver/durability acknowledgement.
    pub fn next_packet(&mut self, cx: &Cx) -> Result<Option<ReplicationPacket>> {
        checkpoint(cx)?;
        let Some(mut packet) = self.sender.next_packet(cx)? else {
            return Ok(None);
        };
        packet.attach_auth_tag(&self.auth_key);
        Ok(Some(packet))
    }

    /// Start a fresh pass over the same captured content and identity, without
    /// reopening or recapturing a source that may since have changed.
    pub fn restart(&mut self) {
        self.sender.restart();
    }
}

/// A journal checkpoint and the independently verified SQL installation.
///
/// The checkpoint covers authenticated packet bytes, not SQL-open success.
/// Inspect `installed.connection` separately; an open error does not undo
/// either durable artifact. Journal namespace/directory durability remains
/// the caller's responsibility, as with [`SnapshotSpool::checkpoint`].
#[derive(Debug)]
#[must_use = "inspect the journal checkpoint, image receipt and SQL-open result"]
pub struct JournaledSnapshotOpen {
    pub checkpoint: SnapshotCheckpoint,
    pub installed: SnapshotOpen,
}

/// Receive an authenticated snapshot into both a packet journal and a SQL image.
///
/// Unlike the volatile bootstrap, every newly accepted packet is journaled
/// before its decoded pages are written. Completed blocks are drained after
/// each packet, so the image is not retained in memory. Call `checkpoint` to
/// acknowledge a recoverable prefix, and retain that receipt in trusted state.
/// Neither a received packet nor a decoded block is a durability receipt.
///
/// The borrowed spool is exclusively owned for this attempt. Its file must
/// live OUTSIDE the private bootstrap directory and must not alias a database
/// or any of its sidecars. The caller owns journal namespace exclusion,
/// directory durability and quiescence of abandoned I/O before reopening.
/// Its preconfigured payload/journal limits apply; the bootstrap options'
/// file limit governs the output image, not the journal or decoder memory.
/// Failed staging files are preserved, never deleted or reused in place.
pub struct JournaledSnapshotBootstrap<'a, F: VfsFile> {
    destination: Destination,
    spool: &'a mut SnapshotSpool<F>,
    write_in_flight: bool,
}

impl<F: VfsFile> fmt::Debug for JournaledSnapshotBootstrap<'_, F> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("JournaledSnapshotBootstrap")
            .field("progress", &self.progress())
            .field("journal_records", &self.spool.record_count())
            .finish_non_exhaustive()
    }
}

impl<'a, F: VfsFile> JournaledSnapshotBootstrap<'a, F> {
    /// Bind a newly created, empty spool to a fresh guarded image destination.
    ///
    /// The spool already carries a trusted manifest and packet authentication
    /// key. A nonfresh spool is refused before creating any output artifacts;
    /// previously decoded pages must never disappear through a second owner.
    pub fn begin(
        cx: &Cx,
        options: &BootstrapOptions,
        spool: &'a mut SnapshotSpool<F>,
        expected_id: [u8; 32],
    ) -> Result<Self> {
        checkpoint(cx)?;
        Self::require_fresh(spool, SnapshotSpoolState::Ready)?;
        let destination = Destination::create(
            cx,
            options,
            spool.receiver().manifest().clone(),
            expected_id,
        )?;
        Ok(Self {
            destination,
            spool,
            write_in_flight: false,
        })
    }

    /// Rebuild a fresh image from a reopened journal, then continue live receive.
    ///
    /// Pass an untouched `SnapshotSpool::open` owner with the independently
    /// trusted manifest/key and last acknowledged checkpoint. Replay verifies
    /// the ENTIRE saved prefix, including that checkpoint, before returning a
    /// live owner. It need not contain a complete snapshot: partial decoder
    /// state survives replay and previously decoded blocks are reapplied to
    /// the new image. Retransmitted packets remain idempotent.
    /// Open the journal writable when more packets will be received; use the
    /// existing spool restore API for a complete read-only transfer.
    ///
    /// A torn suffix never becomes appendable. On `BusyRecovery` with the
    /// spool in `TornTail`, preserve it and call `fork_verified_prefix` with a
    /// fresh journal file, then resume that fork into another empty directory.
    /// A complete torn journal can instead use `restore_spool_and_open`.
    ///
    /// Errors/dropped futures preserve the borrowed journal but can leave its
    /// replay cursor advanced and the new image partially written. Quiesce
    /// old I/O, reopen from the beginning, and use a new destination rather
    /// than reusing that cursor or an abandoned image. No hidden retry,
    /// truncation, checkpoint waiver or background I/O is introduced.
    pub async fn resume(
        cx: &Cx,
        options: &BootstrapOptions,
        spool: &'a mut SnapshotSpool<F>,
        expected_id: [u8; 32],
    ) -> Result<Self> {
        checkpoint(cx)?;
        Self::require_fresh(spool, SnapshotSpoolState::Replaying)?;
        let destination = Destination::create(
            cx,
            options,
            spool.receiver().manifest().clone(),
            expected_id,
        )?;
        let mut bootstrap = Self {
            destination,
            spool,
            write_in_flight: false,
        };
        // Do not stop merely because the receiver has decoded every block:
        // a later record or the required checkpoint can still invalidate the
        // journal. No externally usable bootstrap exists until replay ends.
        while bootstrap.spool.replay_next(cx).await?.is_some() {
            bootstrap.drain_blocks(cx).await?;
        }
        if bootstrap.spool.state() != SnapshotSpoolState::Ready {
            return Err(FrankenError::BusyRecovery);
        }
        Ok(bootstrap)
    }

    fn require_fresh(spool: &SnapshotSpool<F>, state: SnapshotSpoolState) -> Result<()> {
        if spool.state() != state
            || spool.record_count() != 0
            || spool.receiver().blocks_decoded() != 0
            || spool.receiver().retained_payload_bytes() != 0
        {
            return Err(FrankenError::BusyRecovery);
        }
        Ok(())
    }

    #[must_use]
    pub fn progress(&self) -> BootstrapProgress {
        BootstrapProgress {
            blocks_decoded: self.spool.receiver().blocks_decoded(),
            blocks_written: self.destination.image.blocks_applied(),
            blocks_total: self.spool.receiver().manifest().blocks().len(),
            retained_payload_bytes: self.spool.receiver().retained_payload_bytes(),
            poisoned: self.write_in_flight
                || self.spool.state() == SnapshotSpoolState::Poisoned
                || self.destination.image.state() == SnapshotImageState::Poisoned,
        }
    }

    /// Private staging pathname; never open it before successful finalization.
    #[must_use]
    pub fn database_path(&self) -> &std::path::Path {
        &self.destination.path
    }

    async fn drain_blocks(&mut self, cx: &Cx) -> Result<()> {
        // Arm BEFORE taking output: cancellation at apply_block's entry must
        // not discard a decoded block while leaving finalization admissible.
        self.write_in_flight = true;
        for block in self.spool.take_decoded_blocks() {
            self.destination.image.apply_block(cx, &block).await?;
        }
        self.write_in_flight = false;
        Ok(())
    }

    /// Append an admitted packet before applying any newly decoded pages.
    ///
    /// Rejected/duplicate packets do not grow the journal. Preflight admission
    /// errors leave the owner retryable; failed or abandoned journal/image
    /// writes poison it. Restart from the journal rather than retrying an
    /// in-doubt image write. No durability acknowledgement is implicit here.
    pub async fn receive_packet(
        &mut self,
        cx: &Cx,
        packet: &ReplicationPacket,
    ) -> Result<SnapshotPacketResult> {
        if self.progress().poisoned {
            return Err(FrankenError::BusyRecovery);
        }
        let result = self.spool.append(cx, packet).await?;
        self.drain_blocks(cx).await?;
        Ok(result)
    }

    /// Sync the saved packet prefix, without claiming the image is installed.
    pub fn checkpoint(&mut self, cx: &Cx) -> Result<SnapshotCheckpoint> {
        if self.progress().poisoned {
            return Err(FrankenError::BusyRecovery);
        }
        self.spool.checkpoint(cx)
    }

    pub async fn finish_and_open(self, cx: &Cx) -> Result<JournaledSnapshotOpen> {
        self.finish_and_open_with_env(cx, ConnectionEnv::default())
            .await
    }

    /// Sync the complete journal before image verification/publication/SQL open.
    ///
    /// Uses the existing guarded install path and preserves its separate
    /// image/open result. A failed or abandoned finalizer can leave durable
    /// bytes without a returned receipt; it never deletes either artifact.
    pub async fn finish_and_open_with_env(
        self,
        cx: &Cx,
        env: ConnectionEnv,
    ) -> Result<JournaledSnapshotOpen> {
        let progress = self.progress();
        if progress.poisoned {
            return Err(FrankenError::BusyRecovery);
        }
        if !progress.ready_to_finish() {
            return Err(FrankenError::Busy);
        }
        let saved = self.spool.checkpoint(cx)?;
        let installed = self.destination.finish_and_open(cx, env).await?;
        Ok(JournaledSnapshotOpen {
            checkpoint: saved,
            installed,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::super::{BootstrapOptions, SnapshotBootstrap};
    use super::*;
    use crate::SqliteValue;
    use fsqlite_types::flags::VfsOpenFlags;
    use fsqlite_vfs::unix::{UnixFile, UnixVfs};
    use fsqlite_vfs::{Vfs, host_fs};

    const KEY: [u8; 32] = [0x6B; 32];

    fn with_runtime<F: std::future::Future>(future: F) -> F::Output {
        asupersync::runtime::RuntimeBuilder::current_thread()
            .blocking_threads(1, 2)
            .build()
            .unwrap()
            .block_on(future)
    }

    fn context() -> Cx {
        let cx = Cx::new();
        cx.set_native_cx(asupersync::Cx::current().unwrap());
        cx
    }

    fn image(page_size: u32) -> Vec<u8> {
        let root = tempfile::tempdir().unwrap().keep();
        let path = root.join("source.db");
        let stock = rusqlite::Connection::open(&path).unwrap();
        stock
            .execute_batch(&format!(
                "PRAGMA page_size={page_size}; CREATE TABLE t(id INTEGER PRIMARY KEY, v TEXT); \
             INSERT INTO t VALUES(1,'before');"
            ))
            .unwrap();
        stock.close().unwrap();
        fsqlite_vfs::host_fs::read(&path).unwrap()
    }

    async fn install(cx: &Cx, source: &mut CapturedSnapshot) -> Connection {
        let root = tempfile::tempdir().unwrap().keep();
        let options = BootstrapOptions::new(root.join("replica"));
        let mut destination = SnapshotBootstrap::begin(
            cx,
            &options,
            source.manifest().clone(),
            source.manifest_id(),
            KEY,
        )
        .unwrap();
        while let Some(packet) = source.next_packet(cx).unwrap() {
            assert!(packet.verify_integrity(Some(&KEY)));
            destination.receive_packet(cx, &packet).await.unwrap();
        }
        destination
            .finish_and_open(cx)
            .await
            .unwrap()
            .connection
            .unwrap()
    }

    #[test]
    fn live_capture_releases_source_and_installs_only_captured_rows() {
        with_runtime(async {
            let cx = context();
            let root = tempfile::tempdir().unwrap().keep();
            let path = root.join("live.db");
            let mut source = Connection::open(path.to_str().unwrap()).await.unwrap();
            source
                .execute(
                    "CREATE TABLE t(id INTEGER PRIMARY KEY, v TEXT UNIQUE); \
                CREATE VIEW names AS SELECT v FROM t; \
                BEGIN; INSERT INTO t VALUES(1,'before'); COMMIT;",
                )
                .await
                .unwrap();
            let mut captured =
                CapturedSnapshot::capture(&cx, &mut source, KEY, CaptureOptions::default())
                    .await
                    .unwrap();
            let id = captured.manifest_id();
            // A peer can write before delivery; it cannot change captured bytes.
            let peer = Connection::open(path.to_str().unwrap()).await.unwrap();
            peer.execute("BEGIN; UPDATE t SET v='after'; INSERT INTO t VALUES(2,'later'); COMMIT;")
                .await
                .unwrap();
            peer.close().await.unwrap();
            source.close().await.unwrap();
            let replica = install(&cx, &mut captured).await;
            assert_eq!(
                replica
                    .query_row("SELECT v FROM names")
                    .await
                    .unwrap()
                    .get(0),
                Some(&SqliteValue::Text("before".into()))
            );
            assert_eq!(captured.manifest_id(), id);
            replica
                .execute("BEGIN; INSERT INTO t VALUES(3,'replica-write'); COMMIT;")
                .await
                .unwrap();
            assert_eq!(
                replica
                    .query_row("PRAGMA integrity_check")
                    .await
                    .unwrap()
                    .get(0),
                Some(&SqliteValue::Text("ok".into()))
            );
            replica.close().await.unwrap();
        });
    }

    #[test]
    fn capture_refuses_active_transaction_without_committing_its_writes() {
        with_runtime(async {
            let cx = context();
            let mut conn = Connection::open(":memory:").await.unwrap();
            conn.execute("CREATE TABLE t(id INTEGER PRIMARY KEY); BEGIN; INSERT INTO t VALUES(1);")
                .await
                .unwrap();
            assert!(
                CapturedSnapshot::capture(&cx, &mut conn, KEY, CaptureOptions::default())
                    .await
                    .is_err()
            );
            assert!(conn.in_transaction());
            conn.execute("ROLLBACK;").await.unwrap();
            let mut captured =
                CapturedSnapshot::capture(&cx, &mut conn, KEY, CaptureOptions::default())
                    .await
                    .unwrap();
            let replica = install(&cx, &mut captured).await;
            assert_eq!(
                replica
                    .query_row("SELECT count(*) FROM t")
                    .await
                    .unwrap()
                    .get(0),
                Some(&SqliteValue::Integer(0))
            );
            replica.close().await.unwrap();
            conn.close().await.unwrap();
        });
    }

    #[test]
    fn source_restart_repeats_authenticated_packets_and_fixed_identity() {
        let cx = Cx::new();
        let mut captured = CapturedSnapshot::from_image(
            &cx,
            image(512),
            KEY,
            CaptureOptions {
                coding: SenderConfig {
                    symbol_size: 256,
                    max_isi_multiplier: 1,
                },
                ..CaptureOptions::default()
            },
        )
        .unwrap();
        let id = captured.manifest_id();
        let mut first = Vec::new();
        while let Some(packet) = captured.next_packet(&cx).unwrap() {
            assert!(packet.verify_integrity(Some(&KEY)));
            assert!(!packet.verify_integrity(Some(&[0x77; 32])));
            first.push(packet.to_bytes().unwrap());
        }
        captured.restart();
        for bytes in first {
            assert_eq!(
                captured
                    .next_packet(&cx)
                    .unwrap()
                    .unwrap()
                    .to_bytes()
                    .unwrap(),
                bytes
            );
        }
        assert!(captured.next_packet(&cx).unwrap().is_none());
        assert_eq!(captured.manifest_id(), id);
        assert!(!format!("{captured:?}").contains("auth_key"));
    }

    #[test]
    fn image_admission_handles_page_sizes_and_legacy_page_count_authority() {
        for page_size in [512, 8192, 65_536] {
            let original = image(page_size);
            let expected = (
                page_size,
                u32::try_from(original.len() / page_size as usize).unwrap(),
            );
            assert_eq!(image_geometry(&original, original.len()).unwrap(), expected);
            let mut legacy = original.clone();
            legacy[28..32].copy_from_slice(&0_u32.to_be_bytes());
            assert_eq!(image_geometry(&legacy, legacy.len()).unwrap(), expected);
            legacy[28..32].copy_from_slice(&u32::MAX.to_be_bytes());
            legacy[92..96].copy_from_slice(&[0xFF; 4]);
            assert_eq!(image_geometry(&legacy, legacy.len()).unwrap(), expected);
            let changed_counter: [u8; 4] = legacy[24..28].try_into().unwrap();
            legacy[92..96].copy_from_slice(&changed_counter);
            assert!(image_geometry(&legacy, legacy.len()).is_err());
            assert!(matches!(
                image_geometry(&original, original.len() - 1),
                Err(FrankenError::TooBig)
            ));
        }
    }

    #[test]
    fn source_rejects_truncated_malformed_and_cancelled_inputs() {
        let cx = Cx::new();
        let bytes = image(512);
        for length in [0, 15, 99, 100, 511, bytes.len() - 1] {
            assert!(
                CapturedSnapshot::from_image(
                    &cx,
                    bytes[..length].to_vec(),
                    KEY,
                    CaptureOptions::default(),
                )
                .is_err()
            );
        }
        for (offset, value) in [(0, b'X'), (16, 3), (18, 0), (19, 2), (21, 0)] {
            let mut invalid = bytes.clone();
            invalid[offset] = value;
            assert!(
                CapturedSnapshot::from_image(&cx, invalid, KEY, CaptureOptions::default()).is_err()
            );
        }
        cx.cancel();
        assert!(matches!(
            CapturedSnapshot::from_image(&cx, bytes, KEY, CaptureOptions::default(),),
            Err(FrankenError::Interrupt)
        ));
    }

    async fn create_journal(
        cx: &Cx,
        path: &std::path::Path,
        manifest: &SnapshotManifest,
        limit: u64,
    ) -> SnapshotSpool<UnixFile> {
        let vfs = UnixVfs::new();
        let (file, _) = vfs
            .open(
                cx,
                Some(path),
                VfsOpenFlags::CREATE | VfsOpenFlags::EXCLUSIVE | VfsOpenFlags::READWRITE,
            )
            .unwrap();
        let receiver = super::super::ManifestSnapshotReceiver::new(
            manifest.clone(),
            manifest.id(),
            KEY,
            1 << 20,
        )
        .unwrap();
        let spool = SnapshotSpool::create(cx, file, receiver, limit)
            .await
            .unwrap();
        vfs.sync_parent_directory(cx, path).unwrap();
        spool
    }

    async fn reopen_journal(
        cx: &Cx,
        path: &std::path::Path,
        manifest: &SnapshotManifest,
        saved: SnapshotCheckpoint,
        writable: bool,
    ) -> SnapshotSpool<UnixFile> {
        let flags = if writable {
            VfsOpenFlags::READWRITE
        } else {
            VfsOpenFlags::READONLY
        };
        let (file, _) = UnixVfs::new().open(cx, Some(path), flags).unwrap();
        let receiver = super::super::ManifestSnapshotReceiver::new(
            manifest.clone(),
            manifest.id(),
            KEY,
            1 << 20,
        )
        .unwrap();
        SnapshotSpool::open(cx, file, receiver, 1 << 20, Some(saved))
            .await
            .unwrap()
    }

    fn captured_image(cx: &Cx) -> CapturedSnapshot {
        CapturedSnapshot::from_image(
            cx,
            image(512),
            KEY,
            CaptureOptions {
                coding: SenderConfig {
                    symbol_size: 256,
                    max_isi_multiplier: 1,
                },
                ..CaptureOptions::default()
            },
        )
        .unwrap()
    }

    #[test]
    fn journaled_live_bootstrap_checkpoints_and_recovers_sql_image() {
        with_runtime(async {
            let cx = context();
            let root = tempfile::tempdir().unwrap().keep();
            let mut source = captured_image(&cx);
            let manifest = source.manifest().clone();
            let path = root.join("transfer.spool");
            let mut spool = create_journal(&cx, &path, &manifest, 1 << 20).await;
            let options = BootstrapOptions::new(root.join("replica"));
            let mut bootstrap =
                JournaledSnapshotBootstrap::begin(&cx, &options, &mut spool, manifest.id())
                    .unwrap();
            assert_eq!(bootstrap.checkpoint(&cx).unwrap().record_count, 0);
            let first = source.next_packet(&cx).unwrap().unwrap();
            assert!(first.k_source > 1);
            bootstrap.receive_packet(&cx, &first).await.unwrap();
            let partial = bootstrap.checkpoint(&cx).unwrap();
            assert_eq!(partial.record_count, 1);
            assert!(!bootstrap.progress().ready_to_finish());
            while let Some(packet) = source.next_packet(&cx).unwrap() {
                bootstrap.receive_packet(&cx, &packet).await.unwrap();
            }
            assert!(bootstrap.progress().ready_to_finish());
            assert_eq!(bootstrap.progress().retained_payload_bytes, 0);
            let result = bootstrap.finish_and_open(&cx).await.unwrap();
            assert!(result.checkpoint.record_count > partial.record_count);
            assert_eq!(result.installed.image.byte_len, source.image_bytes());
            let conn = result.installed.connection.unwrap();
            assert_eq!(
                conn.query_row("SELECT v FROM t").await.unwrap().get(0),
                Some(&SqliteValue::Text("before".into()))
            );
            conn.execute("BEGIN; INSERT INTO t VALUES(2,'replica-only'); COMMIT;")
                .await
                .unwrap();
            conn.close().await.unwrap();
            spool.into_file().close(&cx).unwrap();

            // A reopened owner can use the acknowledged packet journal alone.
            // Recovery must not pick up writes made later on the first replica.
            let original = host_fs::read(&path).unwrap();
            let mut reopened =
                reopen_journal(&cx, &path, &manifest, result.checkpoint, false).await;
            let restored = super::super::restore_spool_and_open(
                &cx,
                &BootstrapOptions::new(root.join("restored")),
                &mut reopened,
                manifest.id(),
            )
            .await
            .unwrap();
            let conn = restored.connection.unwrap();
            assert_eq!(
                conn.query_row("SELECT count(*) FROM t")
                    .await
                    .unwrap()
                    .get(0),
                Some(&SqliteValue::Integer(1))
            );
            assert_eq!(
                conn.query_row("PRAGMA integrity_check")
                    .await
                    .unwrap()
                    .get(0),
                Some(&SqliteValue::Text("ok".into()))
            );
            conn.close().await.unwrap();
            reopened.into_file().close(&cx).unwrap();
            assert_eq!(host_fs::read(&path).unwrap(), original);
        });
    }

    #[test]
    fn journaled_admission_is_retryable_and_never_acknowledges_rejected_packets() {
        with_runtime(async {
            let cx = context();
            let root = tempfile::tempdir().unwrap().keep();
            let mut source = captured_image(&cx);
            let manifest = source.manifest().clone();
            let first = source.next_packet(&cx).unwrap().unwrap();
            let second = source.next_packet(&cx).unwrap().unwrap();
            let wire = first.to_bytes().unwrap();
            // Header + exactly one record (sequence/length, packet, chain hash).
            let limit = 40 + 12 + u64::try_from(wire.len()).unwrap() + 32;
            let mut spool =
                create_journal(&cx, &root.join("bounded.spool"), &manifest, limit).await;
            let options = BootstrapOptions::new(root.join("bounded-image"));
            let mut bootstrap =
                JournaledSnapshotBootstrap::begin(&cx, &options, &mut spool, manifest.id())
                    .unwrap();
            let mut bad = ReplicationPacket::from_bytes(&wire).unwrap();
            bad.symbol_data[0] ^= 1;
            assert_eq!(
                bootstrap.receive_packet(&cx, &bad).await.unwrap(),
                SnapshotPacketResult::Rejected
            );
            assert_eq!(bootstrap.checkpoint(&cx).unwrap().record_count, 0);
            assert_eq!(
                bootstrap.receive_packet(&cx, &first).await.unwrap(),
                SnapshotPacketResult::Accepted
            );
            let saved = bootstrap.checkpoint(&cx).unwrap();
            assert_eq!(
                bootstrap.receive_packet(&cx, &first).await.unwrap(),
                SnapshotPacketResult::Duplicate
            );
            assert!(matches!(
                bootstrap.receive_packet(&cx, &second).await,
                Err(FrankenError::TooBig)
            ));
            assert!(!bootstrap.progress().poisoned);
            assert_eq!(bootstrap.checkpoint(&cx).unwrap(), saved);
            assert!(matches!(
                bootstrap.finish_and_open(&cx).await,
                Err(FrankenError::Busy)
            ));
            assert_eq!(spool.state(), SnapshotSpoolState::Ready);
            assert_eq!(spool.record_count(), 1);
            spool.into_file().close(&cx).unwrap();
        });
    }

    #[test]
    fn journaled_begin_refuses_wrong_identity_and_used_spools_before_output_creation() {
        with_runtime(async {
            let cx = context();
            let root = tempfile::tempdir().unwrap().keep();
            let mut source = captured_image(&cx);
            let manifest = source.manifest().clone();
            let mut spool = create_journal(&cx, &root.join("used.spool"), &manifest, 1 << 20).await;
            let options = BootstrapOptions::new(root.join("refused-image"));
            let mut wrong_id = manifest.id();
            wrong_id[0] ^= 1;
            assert!(
                JournaledSnapshotBootstrap::begin(&cx, &options, &mut spool, wrong_id).is_err()
            );
            assert!(host_fs::metadata(&options.directory).is_err());
            let first = source.next_packet(&cx).unwrap().unwrap();
            spool.append(&cx, &first).await.unwrap();
            assert!(matches!(
                JournaledSnapshotBootstrap::begin(&cx, &options, &mut spool, manifest.id()),
                Err(FrankenError::BusyRecovery)
            ));
            assert!(host_fs::metadata(&options.directory).is_err());
            assert_eq!(spool.record_count(), 1);
            spool.into_file().close(&cx).unwrap();
        });
    }

    async fn multiblock_packets(
        cx: &Cx,
        root: &std::path::Path,
        repairs: bool,
    ) -> (SnapshotManifest, Vec<ReplicationPacket>, [u8; 32]) {
        use fsqlite_core::snapshot_shipping::manifest::{SnapshotFileSender, SnapshotSourceLimits};

        let (file, _) = UnixVfs::new()
            .open(
                cx,
                Some(&root.join("frozen.db")),
                VfsOpenFlags::CREATE | VfsOpenFlags::EXCLUSIVE | VfsOpenFlags::READWRITE,
            )
            .unwrap();
        file.write(cx, &image(512), 0).await.unwrap();
        // An exclusively owned, immutable image; one database page per block
        // makes restart boundaries cover both complete and partial decoders.
        let mut sender = SnapshotFileSender::open(
            cx,
            file,
            SenderConfig {
                symbol_size: 256,
                max_isi_multiplier: if repairs { 4 } else { 1 },
            },
            SnapshotSourceLimits {
                max_image_bytes: 1 << 20,
                max_block_bytes: fsqlite_core::replication_sender::CHANGESET_HEADER_SIZE + 512 + 12,
            },
        )
        .await
        .unwrap();
        let manifest = sender.manifest().clone();
        let hash = sender.image_blake3();
        assert!(manifest.blocks().len() > 1);
        let mut packets = Vec::new();
        while let Some(mut packet) = sender.next_packet(cx).await.unwrap() {
            packet.attach_auth_tag(&KEY);
            packets.push(packet);
        }
        sender.into_file().close(cx).unwrap();
        (manifest, packets, hash)
    }

    #[test]
    fn journaled_resume_continues_every_multiblock_packet_prefix() {
        with_runtime(async {
            let cx = context();
            let root = tempfile::tempdir().unwrap().keep();
            let (manifest, packets, hash) = multiblock_packets(&cx, &root, false).await;
            // Includes an empty journal, a decoded first block plus partial
            // next block, and a fully received but not yet installed image.
            for split in 0..=packets.len() {
                let path = root.join(format!("prefix-{split}.spool"));
                let mut spool = create_journal(&cx, &path, &manifest, 1 << 20).await;
                let abandoned = BootstrapOptions::new(root.join(format!("abandoned-{split}")));
                let mut bootstrap =
                    JournaledSnapshotBootstrap::begin(&cx, &abandoned, &mut spool, manifest.id())
                        .unwrap();
                for packet in &packets[..split] {
                    bootstrap.receive_packet(&cx, packet).await.unwrap();
                }
                let progress = bootstrap.progress();
                let saved = bootstrap.checkpoint(&cx).unwrap();
                drop(bootstrap);
                spool.into_file().close(&cx).unwrap();

                let mut reopened = reopen_journal(&cx, &path, &manifest, saved, true).await;
                let options = BootstrapOptions::new(root.join(format!("resumed-{split}")));
                let mut resumed =
                    JournaledSnapshotBootstrap::resume(&cx, &options, &mut reopened, manifest.id())
                        .await
                        .unwrap();
                assert_eq!(resumed.progress(), progress);
                if split > 0 {
                    let duplicate = resumed.receive_packet(&cx, &packets[0]).await.unwrap();
                    assert!(matches!(
                        duplicate,
                        SnapshotPacketResult::Duplicate | SnapshotPacketResult::BlockAlreadyDecoded
                    ));
                }
                assert_eq!(resumed.checkpoint(&cx).unwrap(), saved);
                for packet in &packets[split..] {
                    resumed.receive_packet(&cx, packet).await.unwrap();
                }
                assert!(resumed.progress().ready_to_finish());
                assert_eq!(resumed.progress().retained_payload_bytes, 0);
                let opened = resumed.finish_and_open(&cx).await.unwrap();
                assert_eq!(opened.installed.image.image_blake3, hash);
                let conn = opened.installed.connection.unwrap();
                assert_eq!(
                    conn.query_row("SELECT v FROM t WHERE id=1")
                        .await
                        .unwrap()
                        .get(0),
                    Some(&SqliteValue::Text("before".into()))
                );
                conn.execute("BEGIN; INSERT INTO t VALUES(2,'resumed'); COMMIT;")
                    .await
                    .unwrap();
                assert_eq!(
                    conn.query_row("PRAGMA integrity_check")
                        .await
                        .unwrap()
                        .get(0),
                    Some(&SqliteValue::Text("ok".into()))
                );
                conn.close().await.unwrap();
                reopened.into_file().close(&cx).unwrap();
                assert!(host_fs::metadata(&abandoned.directory).unwrap().is_dir());
            }
        });
    }

    #[test]
    fn journaled_resume_preserves_torn_source_and_continues_a_verified_fork_with_repairs() {
        with_runtime(async {
            let cx = context();
            let root = tempfile::tempdir().unwrap().keep();
            let (manifest, mut packets, hash) = multiblock_packets(&cx, &root, true).await;
            // Permanently erase source symbol zero from EVERY source block.
            packets.retain(|packet| packet.esi != 0);
            let path = root.join("torn.spool");
            let mut spool = create_journal(&cx, &path, &manifest, 1 << 20).await;
            let initial = BootstrapOptions::new(root.join("initial"));
            let mut bootstrap =
                JournaledSnapshotBootstrap::begin(&cx, &initial, &mut spool, manifest.id())
                    .unwrap();
            bootstrap.receive_packet(&cx, &packets[0]).await.unwrap();
            let saved = bootstrap.checkpoint(&cx).unwrap();
            drop(bootstrap);
            let mut file = spool.into_file();
            file.write(&cx, b"torn", saved.end_offset).await.unwrap();
            file.close(&cx).unwrap();
            let original = host_fs::read(&path).unwrap();
            let mut reopened = reopen_journal(&cx, &path, &manifest, saved, false).await;
            assert!(matches!(
                JournaledSnapshotBootstrap::resume(
                    &cx,
                    &BootstrapOptions::new(root.join("torn-attempt")),
                    &mut reopened,
                    manifest.id(),
                )
                .await,
                Err(FrankenError::BusyRecovery)
            ));
            assert_eq!(reopened.state(), SnapshotSpoolState::TornTail);

            let fork_path = root.join("fork.spool");
            let (file, _) = UnixVfs::new()
                .open(
                    &cx,
                    Some(&fork_path),
                    VfsOpenFlags::CREATE | VfsOpenFlags::EXCLUSIVE | VfsOpenFlags::READWRITE,
                )
                .unwrap();
            let mut fork = reopened.fork_verified_prefix(&cx, file).await.unwrap();
            UnixVfs::new()
                .sync_parent_directory(&cx, &fork_path)
                .unwrap();
            let options = BootstrapOptions::new(root.join("fork-image"));
            let mut resumed =
                JournaledSnapshotBootstrap::resume(&cx, &options, &mut fork, manifest.id())
                    .await
                    .unwrap();
            assert_eq!(resumed.checkpoint(&cx).unwrap(), saved);
            // Retransmit the saved symbol as well; it must not add a record.
            for packet in &packets {
                resumed.receive_packet(&cx, packet).await.unwrap();
            }
            assert!(resumed.progress().ready_to_finish());
            let opened = resumed.finish_and_open(&cx).await.unwrap();
            assert!(opened.checkpoint.record_count > saved.record_count);
            assert_eq!(opened.installed.image.image_blake3, hash);
            let conn = opened.installed.connection.unwrap();
            assert_eq!(
                conn.query_row("SELECT v FROM t").await.unwrap().get(0),
                Some(&SqliteValue::Text("before".into()))
            );
            conn.close().await.unwrap();
            fork.into_file().close(&cx).unwrap();
            reopened.into_file().close(&cx).unwrap();
            assert_eq!(host_fs::read(&path).unwrap(), original);
        });
    }

    #[test]
    fn journaled_resume_requires_the_checkpoint_even_after_all_blocks_decode() {
        with_runtime(async {
            let cx = context();
            let root = tempfile::tempdir().unwrap().keep();
            let (manifest, packets, _) = multiblock_packets(&cx, &root, false).await;
            let path = root.join("complete.spool");
            let mut spool = create_journal(&cx, &path, &manifest, 1 << 20).await;
            for packet in &packets {
                spool.append(&cx, packet).await.unwrap();
                drop(spool.take_decoded_blocks());
            }
            let saved = spool.checkpoint(&cx).unwrap();
            spool.into_file().close(&cx).unwrap();
            let original = host_fs::read(&path).unwrap();
            let mut wrong = saved;
            wrong.chain_hash[0] ^= 1;
            let mut reopened = reopen_journal(&cx, &path, &manifest, wrong, false).await;
            let options = BootstrapOptions::new(root.join("bad-checkpoint"));
            assert!(matches!(
                JournaledSnapshotBootstrap::resume(&cx, &options, &mut reopened, manifest.id(),)
                    .await,
                Err(FrankenError::DatabaseCorrupt { .. })
            ));
            assert!(reopened.receiver().is_complete());
            assert_eq!(reopened.record_count(), saved.record_count);
            assert_eq!(reopened.state(), SnapshotSpoolState::Poisoned);
            // The failed owner has dropped; inspecting its preserved file no
            // longer closes a descriptor alongside an active database lock.
            let failed = host_fs::read(&options.directory.join("database.db")).unwrap();
            assert!(!failed.starts_with(b"SQLite format 3\0"));
            reopened.into_file().close(&cx).unwrap();
            assert_eq!(host_fs::read(&path).unwrap(), original);

            let mut fresh = reopen_journal(&cx, &path, &manifest, saved, false).await;
            let refused = BootstrapOptions::new(root.join("refused-resume"));
            let cancelled = Cx::new();
            cancelled.cancel();
            assert!(matches!(JournaledSnapshotBootstrap::resume(
                &cancelled, &refused, &mut fresh, manifest.id(),
            ).await, Err(FrankenError::Interrupt)));
            assert!(host_fs::metadata(&refused.directory).is_err());
            assert_eq!(fresh.record_count(), 0);
            fresh.replay_next(&cx).await.unwrap().unwrap();
            assert!(matches!(
                JournaledSnapshotBootstrap::resume(&cx, &refused, &mut fresh, manifest.id(),).await,
                Err(FrankenError::BusyRecovery)
            ));
            assert!(host_fs::metadata(&refused.directory).is_err());
            fresh.into_file().close(&cx).unwrap();
        });
    }

    #[test]
    fn journaled_cancelled_drain_blocks_publication_but_preserves_packet_recovery() {
        with_runtime(async {
            let cx = context();
            let root = tempfile::tempdir().unwrap().keep();
            let mut source = captured_image(&cx);
            let manifest = source.manifest().clone();
            let path = root.join("drain.spool");
            let mut spool = create_journal(&cx, &path, &manifest, 1 << 20).await;
            let options = BootstrapOptions::new(root.join("failed-drain"));
            let mut bootstrap =
                JournaledSnapshotBootstrap::begin(&cx, &options, &mut spool, manifest.id())
                    .unwrap();
            // Reach the exact post-append/pre-drain boundary deterministically.
            while let Some(packet) = source.next_packet(&cx).unwrap() {
                bootstrap.spool.append(&cx, &packet).await.unwrap();
            }
            assert!(bootstrap.spool.receiver().is_complete());
            let cancelled = Cx::new();
            cancelled.cancel();
            assert!(matches!(
                bootstrap.drain_blocks(&cancelled).await,
                Err(FrankenError::Abort)
            ));
            assert!(bootstrap.progress().poisoned);
            assert!(matches!(
                bootstrap.checkpoint(&cx),
                Err(FrankenError::BusyRecovery)
            ));
            assert!(matches!(
                bootstrap.finish_and_open(&cx).await,
                Err(FrankenError::BusyRecovery)
            ));
            // Image failure is terminal for that owner, not loss of the saved
            // authenticated packets. Reopen them into a different destination.
            let saved = spool.checkpoint(&cx).unwrap();
            spool.into_file().close(&cx).unwrap();
            let mut reopened = reopen_journal(&cx, &path, &manifest, saved, true).await;
            let fresh = BootstrapOptions::new(root.join("recovered-drain"));
            let resumed =
                JournaledSnapshotBootstrap::resume(&cx, &fresh, &mut reopened, manifest.id())
                    .await
                    .unwrap();
            assert!(resumed.progress().ready_to_finish());
            let conn = resumed
                .finish_and_open(&cx)
                .await
                .unwrap()
                .installed
                .connection
                .unwrap();
            assert_eq!(
                conn.query_row("SELECT v FROM t").await.unwrap().get(0),
                Some(&SqliteValue::Text("before".into()))
            );
            conn.close().await.unwrap();
            reopened.into_file().close(&cx).unwrap();
        });
    }
}