second-brain-sync 0.13.2

Bidirectional sync for second-brain: SSH transport, JSONL change log, conflict resolution
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
use std::fmt;
use std::fs::{self, File};
use std::io::{BufRead, BufReader, BufWriter, Read, Write};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, anyhow, bail, ensure};
use chacha20poly1305::aead::{Aead, KeyInit, OsRng, Payload};
use chacha20poly1305::{XChaCha20Poly1305, XNonce};
use hkdf::Hkdf;
use rand_core::RngCore;
use second_brain_core::keys::{Epoch, IdentityKey, KeyGuard, Subkey, derive_subkey};
use second_brain_core::sqlite_store::{SnapshotRow, SnapshotState};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::protocol::HandshakeKeys;

pub const SNAPSHOT_FORMAT: u32 = 1;

/// Uncompressed JSONL per frame: the exporter holds one frame and the
/// channel's few in memory, never the snapshot.
pub const FRAME_BYTES: usize = 4 * 1024 * 1024;

/// The largest row a snapshot carries, below the 64 MiB request limit
/// that keeps a larger record off the sync wire as well.
const MAX_ROW_BYTES: usize = 48 * 1024 * 1024;

/// A frame longer than this is refused before it is read. A frame over
/// `FRAME_BYTES` holds one row, and zstd grows incompressible input by
/// well under this margin.
const MAX_FRAME_BYTES: usize = MAX_ROW_BYTES + 16 * 1024 * 1024;

const HEADER_PREFIX: &str = "second-brain-snapshot";
const AAD_DOMAIN: &[u8] = b"second-brain snapshot frame v1";
const EPOCH_ZERO_INFO: &[u8] = b"second-brain snapshot key over the epoch 0 wire key v1";
const NONCE_LEN: usize = 24;
const KIND_ROWS: u8 = 1;
const KIND_MANIFEST: u8 = 2;
const ZSTD_LEVEL: i32 = 3;

/// The plain header line ahead of the sealed frames, so a receiver can name
/// the epoch it needs before opening anything.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SnapshotHeader {
    pub format: u32,
    pub snapshot_id: String,
    pub exporter: String,
    pub epoch: Epoch,
    pub sync_protocol: u32,
    pub schema_version: i64,
    pub created_at: i64,
}

/// The sealed trailer: what the exporter stated about the rows, and what
/// the receiver checks the spool against before applying any of them.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Manifest {
    pub state: SnapshotState,
    pub frames: u64,
    /// Hex SHA-256 over every row frame's sealed body, in order.
    pub hash: String,
    /// The exporter's device rows and the envelopes it wrapped for the
    /// receiver.
    pub keys: Option<HandshakeKeys>,
}

/// The `snapshot` subkey of one identity key epoch. Epoch 0 derives only
/// the wire subkey, so its snapshot key is expanded from that one under a
/// label of its own.
pub struct SnapshotKey {
    epoch: Epoch,
    key: KeyGuard<[u8; 32]>,
}

impl fmt::Debug for SnapshotKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SnapshotKey")
            .field("epoch", &self.epoch)
            .finish_non_exhaustive()
    }
}

impl SnapshotKey {
    pub fn derive(identity: &IdentityKey, epoch: &Epoch) -> Result<Self> {
        if epoch.number != 0 {
            return Ok(Self {
                epoch: *epoch,
                key: derive_subkey(identity, epoch, Subkey::Snapshot)?,
            });
        }
        let wire = derive_subkey(identity, epoch, Subkey::Wire)?;
        let mut key = KeyGuard::new([0u8; 32]);
        Hkdf::<Sha256>::new(None, wire.expose())
            .expand(EPOCH_ZERO_INFO, key.expose_mut())
            .map_err(|e| anyhow!("deriving the epoch 0 snapshot key: {e}"))?;
        Ok(Self { epoch: *epoch, key })
    }

    pub fn epoch(&self) -> &Epoch {
        &self.epoch
    }

    fn cipher(&self) -> XChaCha20Poly1305 {
        XChaCha20Poly1305::new(self.key.expose().into())
    }
}

fn aad(header: &SnapshotHeader, kind: u8, index: u64) -> Vec<u8> {
    let mut aad = Vec::with_capacity(AAD_DOMAIN.len() + 13 + header.snapshot_id.len() + 9);
    aad.extend_from_slice(AAD_DOMAIN);
    aad.extend_from_slice(&header.epoch.number.to_be_bytes());
    aad.extend_from_slice(&header.epoch.key_id.0);
    aad.extend_from_slice(header.snapshot_id.as_bytes());
    aad.push(kind);
    aad.extend_from_slice(&index.to_be_bytes());
    aad
}

fn seal(
    key: &SnapshotKey,
    header: &SnapshotHeader,
    kind: u8,
    index: u64,
    plain: &[u8],
) -> Result<Vec<u8>> {
    let mut nonce = [0u8; NONCE_LEN];
    OsRng.fill_bytes(&mut nonce);
    let ct = key
        .cipher()
        .encrypt(
            XNonce::from_slice(&nonce),
            Payload {
                msg: plain,
                aad: &aad(header, kind, index),
            },
        )
        .map_err(|e| anyhow!("sealing a snapshot frame: {e}"))?;
    let mut body = Vec::with_capacity(NONCE_LEN + ct.len());
    body.extend_from_slice(&nonce);
    body.extend_from_slice(&ct);
    Ok(body)
}

fn open(
    key: &SnapshotKey,
    header: &SnapshotHeader,
    kind: u8,
    index: u64,
    body: &[u8],
) -> Result<Vec<u8>> {
    ensure!(
        body.len() > NONCE_LEN,
        "snapshot frame {index} is too short"
    );
    let (nonce, ct) = body.split_at(NONCE_LEN);
    key.cipher()
        .decrypt(
            XNonce::from_slice(nonce),
            Payload {
                msg: ct,
                aad: &aad(header, kind, index),
            },
        )
        .map_err(|e| anyhow!("snapshot frame {index} does not open (tampered or reordered): {e}"))
}

fn write_frame<W: Write>(out: &mut W, kind: u8, body: &[u8]) -> Result<()> {
    out.write_all(&[kind])?;
    out.write_all(&u32::try_from(body.len())?.to_be_bytes())?;
    out.write_all(body)?;
    Ok(())
}

/// One frame's kind and sealed body, or None at a clean end of stream.
fn read_frame<R: Read>(input: &mut R) -> Result<Option<(u8, Vec<u8>)>> {
    let mut kind = [0u8; 1];
    match input.read_exact(&mut kind) {
        Ok(()) => {}
        Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return Ok(None),
        Err(e) => return Err(e.into()),
    }
    let mut len = [0u8; 4];
    input
        .read_exact(&mut len)
        .context("the snapshot stream ended inside a frame")?;
    let len = u32::from_be_bytes(len) as usize;
    ensure!(
        len <= MAX_FRAME_BYTES,
        "snapshot frame of {len} bytes is too large"
    );
    let mut body = vec![0u8; len];
    input
        .read_exact(&mut body)
        .context("the snapshot stream ended inside a frame")?;
    Ok(Some((kind[0], body)))
}

pub fn write_header<W: Write>(out: &mut W, header: &SnapshotHeader) -> Result<()> {
    writeln!(out, "{HEADER_PREFIX} {}", serde_json::to_string(header)?)?;
    Ok(())
}

pub fn read_header<R: BufRead>(input: &mut R) -> Result<SnapshotHeader> {
    let mut line = String::new();
    if input.read_line(&mut line)? == 0 {
        bail!("the snapshot stream is empty");
    }
    let json = line
        .trim()
        .strip_prefix(HEADER_PREFIX)
        .context("the stream is not a second-brain snapshot")?;
    let header: SnapshotHeader =
        serde_json::from_str(json.trim()).context("parsing the snapshot header")?;
    ensure!(
        header.format == SNAPSHOT_FORMAT,
        "snapshot format {} is not the {SNAPSHOT_FORMAT} this binary reads",
        header.format
    );
    Ok(header)
}

/// Writes rows as sealed, compressed frames of up to `FRAME_BYTES` each.
pub struct SnapshotWriter<W: Write> {
    out: W,
    key: SnapshotKey,
    header: SnapshotHeader,
    buf: Vec<u8>,
    frames: u64,
    hash: Sha256,
}

impl<W: Write> SnapshotWriter<W> {
    pub fn new(mut out: W, key: SnapshotKey, header: SnapshotHeader) -> Result<Self> {
        ensure!(
            key.epoch == header.epoch,
            "the snapshot key is not the header's epoch"
        );
        write_header(&mut out, &header)?;
        Ok(Self {
            out,
            key,
            header,
            buf: Vec::with_capacity(FRAME_BYTES),
            frames: 0,
            hash: Sha256::new(),
        })
    }

    pub fn row(&mut self, row: &SnapshotRow) -> Result<()> {
        let mut line = serde_json::to_vec(row)?;
        line.push(b'\n');
        if line.len() > MAX_ROW_BYTES {
            tracing::warn!(
                section = row.section(),
                bytes = line.len(),
                "a snapshot row larger than sync carries was left out"
            );
            return Ok(());
        }
        if !self.buf.is_empty() && self.buf.len() + line.len() > FRAME_BYTES {
            self.flush_rows()?;
        }
        self.buf.extend_from_slice(&line);
        if self.buf.len() >= FRAME_BYTES {
            self.flush_rows()?;
        }
        Ok(())
    }

    fn flush_rows(&mut self) -> Result<()> {
        if self.buf.is_empty() {
            return Ok(());
        }
        let compressed = zstd::encode_all(self.buf.as_slice(), ZSTD_LEVEL)?;
        let body = seal(&self.key, &self.header, KIND_ROWS, self.frames, &compressed)?;
        self.hash.update(&body);
        write_frame(&mut self.out, KIND_ROWS, &body)?;
        self.out.flush()?;
        self.frames += 1;
        self.buf.clear();
        Ok(())
    }

    pub fn finish(mut self, state: SnapshotState, keys: Option<HandshakeKeys>) -> Result<W> {
        self.flush_rows()?;
        let manifest = Manifest {
            state,
            frames: self.frames,
            hash: hex(&self.hash.clone().finalize()),
            keys,
        };
        let body = seal(
            &self.key,
            &self.header,
            KIND_MANIFEST,
            self.frames,
            &serde_json::to_vec(&manifest)?,
        )?;
        write_frame(&mut self.out, KIND_MANIFEST, &body)?;
        self.out.flush()?;
        Ok(self.out)
    }
}

fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

/// Spools a snapshot stream to `path`, then checks it against its sealed
/// manifest, so nothing is applied from a stream that was cut or altered
/// and the exporter's read transaction is pinned only for the transfer.
/// The spool appears at `path` only once it checks out.
pub fn spool<R: Read>(
    input: R,
    path: &Path,
    key_for: &dyn Fn(&SnapshotHeader) -> Result<SnapshotKey>,
) -> Result<(SnapshotHeader, Manifest)> {
    let part = part_path(path);
    let checked = copy_to(input, &part).and_then(|()| verify(&part, key_for));
    match checked {
        Ok(checked) => {
            fs::rename(&part, path)
                .with_context(|| format!("moving the spool to {}", path.display()))?;
            Ok(checked)
        }
        Err(e) => {
            if let Err(removed) = fs::remove_file(&part)
                && removed.kind() != std::io::ErrorKind::NotFound
            {
                tracing::warn!("removing the snapshot spool {}: {removed}", part.display());
            }
            Err(e)
        }
    }
}

fn copy_to<R: Read>(input: R, part: &Path) -> Result<()> {
    let mut input = BufReader::new(input);
    let header = read_header(&mut input)?;
    let mut out =
        BufWriter::new(File::create(part).with_context(|| format!("creating {}", part.display()))?);
    write_header(&mut out, &header)?;
    let mut ended = false;
    while let Some((kind, body)) = read_frame(&mut input)? {
        ensure!(
            !ended,
            "the snapshot stream carries frames after its manifest"
        );
        write_frame(&mut out, kind, &body)?;
        ended = kind == KIND_MANIFEST;
    }
    ensure!(ended, "the snapshot stream ended before its manifest");
    out.into_inner()
        .map_err(|e| anyhow!("writing the snapshot spool: {e}"))?
        .sync_all()?;
    Ok(())
}

fn part_path(path: &Path) -> PathBuf {
    let mut name = path.file_name().unwrap_or_default().to_os_string();
    name.push(".part");
    path.with_file_name(name)
}

/// Reads the spool's header and manifest, checking every row frame's hash
/// and the frame count against the manifest; opens no row frame.
pub fn verify(
    path: &Path,
    key_for: &dyn Fn(&SnapshotHeader) -> Result<SnapshotKey>,
) -> Result<(SnapshotHeader, Manifest)> {
    let mut input = BufReader::new(File::open(path)?);
    let header = read_header(&mut input)?;
    let key = key_for(&header)?;
    let mut hash = Sha256::new();
    let mut frames = 0u64;
    while let Some((kind, body)) = read_frame(&mut input)? {
        match kind {
            KIND_ROWS => {
                hash.update(&body);
                frames += 1;
            }
            KIND_MANIFEST => {
                let plain = open(&key, &header, KIND_MANIFEST, frames, &body).context(
                    "the snapshot's manifest does not open at its place: it was cut or altered",
                )?;
                let manifest: Manifest =
                    serde_json::from_slice(&plain).context("parsing the snapshot manifest")?;
                ensure!(
                    manifest.frames == frames && manifest.hash == hex(&hash.finalize()),
                    "the snapshot does not match its manifest: it was cut or altered"
                );
                return Ok((header, manifest));
            }
            other => bail!("unknown snapshot frame kind {other}"),
        }
    }
    bail!("the snapshot spool has no manifest")
}

/// Every row of a verified spool, in order, one frame in memory at a time.
pub fn spool_rows(
    path: &Path,
    key_for: &dyn Fn(&SnapshotHeader) -> Result<SnapshotKey>,
    each: &mut dyn FnMut(SnapshotRow) -> Result<()>,
) -> Result<()> {
    let mut input = BufReader::new(File::open(path)?);
    let header = read_header(&mut input)?;
    let key = key_for(&header)?;
    let mut index = 0u64;
    while let Some((kind, body)) = read_frame(&mut input)? {
        if kind != KIND_ROWS {
            break;
        }
        let compressed = open(&key, &header, KIND_ROWS, index, &body)?;
        let jsonl = zstd::decode_all(compressed.as_slice())?;
        for line in jsonl.split(|b| *b == b'\n').filter(|l| !l.is_empty()) {
            each(serde_json::from_slice(line).context("parsing a snapshot row")?)?;
        }
        index += 1;
    }
    Ok(())
}

/// Pinned by `wire_format_matches_the_protocol_version`.
#[cfg(test)]
pub(crate) fn snapshot_format() -> String {
    format!(
        "{HEADER_PREFIX} <json header> \\n, then frames of kind u8 (1 rows, 2 manifest) | len u32 be \
         | nonce[24] | xchacha20poly1305(zstd(jsonl rows) or json manifest), aad = {:?} | epoch \
         u32 be | key id[8] | snapshot id | kind | frame index u64 be",
        String::from_utf8_lossy(AAD_DOMAIN)
    )
}

#[cfg(test)]
mod tests {
    use second_brain_core::keys::KeyId;

    use super::*;

    fn key(number: u32) -> (IdentityKey, Epoch) {
        let identity = IdentityKey::generate().unwrap();
        let epoch = Epoch {
            number,
            key_id: identity.key_id(),
        };
        (identity, epoch)
    }

    fn header(epoch: Epoch) -> SnapshotHeader {
        SnapshotHeader {
            format: SNAPSHOT_FORMAT,
            snapshot_id: "s1".to_string(),
            exporter: "a".to_string(),
            epoch,
            sync_protocol: crate::protocol::SYNC_PROTOCOL_VERSION,
            schema_version: 12,
            created_at: 0,
        }
    }

    fn rows(n: usize) -> Vec<SnapshotRow> {
        (0..n)
            .map(|i| SnapshotRow::Dedup {
                origin: format!("origin-{i}"),
                lo: i as i64,
                hi: i as i64 + 10,
            })
            .collect()
    }

    fn stream(identity: &IdentityKey, epoch: Epoch, rows: &[SnapshotRow]) -> Vec<u8> {
        let mut writer = SnapshotWriter::new(
            Vec::new(),
            SnapshotKey::derive(identity, &epoch).unwrap(),
            header(epoch),
        )
        .unwrap();
        for row in rows {
            writer.row(row).unwrap();
        }
        writer
            .finish(
                SnapshotState {
                    seq: 9,
                    ..SnapshotState::default()
                },
                None,
            )
            .unwrap()
    }

    fn noise(len: usize) -> String {
        let mut state = 0x9e37_79b9_7f4a_7c15u64;
        (0..len)
            .map(|_| {
                state ^= state << 13;
                state ^= state >> 7;
                state ^= state << 17;
                char::from(b'!' + (state % 90) as u8)
            })
            .collect()
    }

    fn large(origin: String) -> SnapshotRow {
        SnapshotRow::Dedup {
            origin,
            lo: 0,
            hi: 1,
        }
    }

    #[test]
    fn a_row_too_large_for_a_shared_frame_travels_in_one_of_its_own() {
        let (identity, epoch) = key(1);
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("snapshot.spool");
        let big = noise(3 * FRAME_BYTES);
        let mut sent = rows(2);
        sent.insert(1, large(big.clone()));
        let bytes = stream(&identity, epoch, &sent);

        spool(bytes.as_slice(), &path, &|h| {
            SnapshotKey::derive(&identity, &h.epoch)
        })
        .unwrap();

        let got = origins(&path, &identity);
        assert_eq!(
            got,
            vec!["origin-0".to_string(), big, "origin-1".to_string()]
        );
    }

    #[test]
    fn a_row_larger_than_sync_carries_is_left_out_and_the_rest_arrive() {
        let (identity, epoch) = key(1);
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("snapshot.spool");
        let mut sent = rows(2);
        sent.insert(1, large("x".repeat(MAX_ROW_BYTES)));
        let bytes = stream(&identity, epoch, &sent);

        spool(bytes.as_slice(), &path, &|h| {
            SnapshotKey::derive(&identity, &h.epoch)
        })
        .unwrap();

        assert_eq!(origins(&path, &identity), vec!["origin-0", "origin-1"]);
    }

    fn origins(path: &Path, identity: &IdentityKey) -> Vec<String> {
        let mut seen = Vec::new();
        spool_rows(
            path,
            &|h| SnapshotKey::derive(identity, &h.epoch),
            &mut |row| {
                if let SnapshotRow::Dedup { origin, .. } = row {
                    seen.push(origin);
                }
                Ok(())
            },
        )
        .unwrap();
        seen
    }

    #[test]
    fn a_spooled_stream_round_trips_under_epoch_zero_and_later_epochs() {
        for number in [0, 1] {
            let (identity, epoch) = key(number);
            let dir = tempfile::tempdir().unwrap();
            let path = dir.path().join("snapshot.spool");
            let rows = rows(3);
            let bytes = stream(&identity, epoch, &rows);

            let (header, manifest) = spool(bytes.as_slice(), &path, &|h| {
                SnapshotKey::derive(&identity, &h.epoch)
            })
            .unwrap();

            assert_eq!(header.snapshot_id, "s1");
            assert_eq!(manifest.state.seq, 9);
            assert_eq!(
                origins(&path, &identity),
                vec!["origin-0", "origin-1", "origin-2"]
            );
        }
    }

    #[test]
    fn rows_beyond_one_frame_split_into_several() {
        let (identity, epoch) = key(1);
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("snapshot.spool");
        let long = "x".repeat(FRAME_BYTES / 3);
        let rows: Vec<SnapshotRow> = (0..5)
            .map(|i| SnapshotRow::Dedup {
                origin: format!("{i}{long}"),
                lo: 0,
                hi: 0,
            })
            .collect();
        let bytes = stream(&identity, epoch, &rows);
        let (_, manifest) = spool(bytes.as_slice(), &path, &|h| {
            SnapshotKey::derive(&identity, &h.epoch)
        })
        .unwrap();
        assert!(manifest.frames >= 2, "{}", manifest.frames);
        assert_eq!(origins(&path, &identity).len(), 5);
    }

    #[test]
    fn a_cut_stream_leaves_no_spool() {
        let (identity, epoch) = key(1);
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("snapshot.spool");
        let bytes = stream(&identity, epoch, &rows(3));
        let cut = &bytes[..bytes.len() - 5];

        assert!(spool(cut, &path, &|h| SnapshotKey::derive(&identity, &h.epoch)).is_err());
        assert!(!path.exists() && !part_path(&path).exists());
    }

    #[test]
    fn a_stream_missing_a_row_frame_fails_its_manifest() {
        let (identity, epoch) = key(1);
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("snapshot.spool");
        let long = "y".repeat(FRAME_BYTES);
        let rows: Vec<SnapshotRow> = (0..2)
            .map(|i| SnapshotRow::Dedup {
                origin: format!("{i}{long}"),
                lo: 0,
                hi: 0,
            })
            .collect();
        let bytes = stream(&identity, epoch, &rows);
        let header_end = bytes.iter().position(|b| *b == b'\n').unwrap() + 1;
        let mut input = &bytes[header_end..];
        let (_, first) = read_frame(&mut input).unwrap().unwrap();
        let mut dropped = bytes[..header_end].to_vec();
        dropped.extend_from_slice(input);
        assert!(!first.is_empty());

        let err = spool(dropped.as_slice(), &path, &|h| {
            SnapshotKey::derive(&identity, &h.epoch)
        })
        .unwrap_err();
        assert!(format!("{err:#}").contains("manifest"), "{err:#}");
        assert!(!path.exists());
    }

    #[test]
    fn another_identitys_key_opens_nothing() {
        let (identity, epoch) = key(1);
        let (other, _) = key(1);
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("snapshot.spool");
        let bytes = stream(&identity, epoch, &rows(1));
        let foreign = |h: &SnapshotHeader| {
            SnapshotKey::derive(
                &other,
                &Epoch {
                    number: h.epoch.number,
                    key_id: other.key_id(),
                },
            )
        };
        let _ = KeyId([0; 8]);
        assert!(spool(bytes.as_slice(), &path, &foreign).is_err());
    }
}