second-brain-sync 0.13.4

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
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
use std::fmt;
use std::io::BufRead;

use anyhow::{Context, Result};
use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use second_brain_core::keys::{
    DeviceRoute, Envelope, Epoch, EpochCause, IdentityTag, SignedRecord, SignedRetirement,
    SignedRevocation,
};
use second_brain_core::sqlite_store::{SCHEMA_VERSION, SqliteStore};
use serde::{Deserialize, Serialize};

/// Bump whenever the sync wire format or the meaning of a field changes:
/// the record envelope, the header line, the line framing or the crypto frame.
/// `wire_format_matches_the_protocol_version` pins the current format.
/// Releases up to second-brain 0.10.0 sent no header and count as protocol 1.
pub const SYNC_PROTOCOL_VERSION: u32 = 9;

const UNVERSIONED_PROTOCOL: u32 = 1;

/// Makes the header line neither base64 nor JSON: binaries up to 0.10.0 pass
/// any non-base64 line through as a plaintext JSON record, and only a parse
/// failure on the first line stops them before they apply anything.
const HEADER_PREFIX: &str = "second-brain-sync";

/// First line of every sync stream: `HEADER_PREFIX`, a space, then plaintext
/// JSON, so a peer holding the wrong key still gets a version error. It carries
/// no data, is not a change-log entry and has no sequence number.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SyncHeader {
    pub sync_protocol: u32,
    /// `PRAGMA user_version` of the store whose records the stream carries.
    pub schema_version: i64,
    /// The sender's clock handshake; `ensure_matches` ignores it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub hello: Option<Hello>,
}

/// What each side of a sync tells the other about its clock and how much of
/// the other's log it holds, so neither reissues a sequence number or an
/// origin key the other has already seen.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Hello {
    pub machine_id: String,
    /// Microseconds since the Unix epoch.
    pub wall_micros: i64,
    /// The sender's clock is NTP-synchronised: the signal itself, not merely
    /// a clock gate that has opened.
    pub synced: bool,
    /// The sender's cursor on the receiver's log.
    pub cursor: Option<u64>,
    /// The highest origin_seq of the receiver's own records the sender holds.
    pub held_origin_seq: Option<i64>,
    /// None from a sender on sync protocol 3 or older.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub keys: Option<HandshakeKeys>,
    /// The highest seq of the receiver's log that the receiver's pushes into
    /// the sender have applied.
    #[serde(default)]
    pub peer_in: Option<u64>,
    /// The sender's log holds no rows at or below this seq.
    #[serde(default)]
    pub log_floor: u64,
    /// The highest seq the sender has minted.
    #[serde(default)]
    pub head: u64,
    /// How far into the receiver's log the sender has applied without a
    /// gap, below `cursor` while a record from it still fails.
    #[serde(default)]
    pub applied: Option<u64>,
    /// A snapshot merge the sender has not finished; a pusher neither
    /// streams another nor advances while one runs.
    #[serde(default)]
    pub merging: Option<Merging>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Merging {
    pub snapshot_id: String,
    pub exporter: String,
    /// `S`, the seq the snapshot is consistent at.
    pub seq: u64,
}

/// The line `sb sync-export` writes instead of a header when the exporter's
/// log no longer holds the rows after the puller's cursor, so the puller
/// merges a snapshot.
const BELOW_FLOOR_PREFIX: &str = "second-brain-sync-below-floor";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct BelowFloorLine {
    pub after_seq: u64,
    pub log_floor: u64,
}

impl BelowFloorLine {
    pub fn to_line(&self) -> String {
        format!(
            "{BELOW_FLOOR_PREFIX} {}",
            serde_json::to_string(self).expect("BelowFloorLine serializes")
        )
    }

    pub fn parse_line(line: &str) -> Option<Self> {
        let json = line.trim().strip_prefix(BELOW_FLOOR_PREFIX)?;
        serde_json::from_str(json.trim()).ok()
    }
}

/// The line a peer writes instead of a header or a push reply when its daemon
/// refuses the sync, so the initiator gets the peer's own error rather than
/// only its exit status.
const REFUSED_PREFIX: &str = "second-brain-sync-refused";

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RefusedLine {
    pub error: String,
    #[serde(default)]
    pub identity: bool,
}

impl RefusedLine {
    pub fn to_line(&self) -> String {
        format!(
            "{REFUSED_PREFIX} {}",
            serde_json::to_string(self).expect("RefusedLine serializes")
        )
    }

    pub fn parse_line(line: &str) -> Option<Self> {
        let json = line.trim().strip_prefix(REFUSED_PREFIX)?;
        serde_json::from_str(json.trim()).ok()
    }

    /// A daemon's 409 body, unless it asks for a snapshot or a wait instead.
    pub fn from_refusal_body(body: &str) -> Option<Self> {
        let value: serde_json::Value = serde_json::from_str(body).ok()?;
        if value["below_floor"] == true || value["merging"] == true {
            return None;
        }
        Some(Self {
            error: value["error"].as_str()?.to_string(),
            identity: value["identity"] == true,
        })
    }
}

/// The sender's sync identity, carried in plaintext: nothing here is secret,
/// since envelopes are sealed to their recipient and rows are signed.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct HandshakeKeys {
    /// None when the sender holds no identity key yet.
    pub identity_tag: Option<IdentityTag>,
    /// Every epoch of `identity_tag` the sender can open.
    #[serde(default)]
    pub epochs: Vec<Epoch>,
    /// The signed device rows of `identity_tag` the sender holds.
    #[serde(default)]
    pub devices: Vec<SignedRecord>,
    /// Epochs the sender wrapped for the receiver.
    #[serde(default)]
    pub envelopes: Vec<Envelope>,
    /// The revocations of `identity_tag` the sender holds.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub revocations: Vec<SignedRevocation>,
    /// The recovery phrase retirements of `identity_tag` the sender holds.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub retirements: Vec<SignedRetirement>,
    /// The revocations behind each epoch in `epochs` that has any; an
    /// envelope is taken only once its epoch's revocations are applied.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub causes: Vec<(Epoch, EpochCause)>,
    /// How to reach the sender and the active devices it knows; unsigned
    /// hints, since host keys are pinned per machine id.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub routes: Vec<DeviceRoute>,
}

impl Hello {
    /// How far into the receiver's log the sender has applied, by its own
    /// pulls or by the receiver's pushes.
    pub fn applied_of_receiver(&self) -> Option<u64> {
        self.cursor.max(self.peer_in)
    }

    /// `applied_of_receiver` without the gaps: a record the sender still
    /// fails to apply holds this below its cursor, so the floor keeps it.
    pub fn acked_by_receiver(&self) -> Option<u64> {
        self.applied.or(self.cursor).max(self.peer_in)
    }

    /// One shell word, for `sb sync-export --hello` over ssh.
    pub fn to_arg(&self) -> String {
        URL_SAFE_NO_PAD.encode(serde_json::to_vec(self).expect("Hello serializes"))
    }

    /// Refuses a hello from a binary that predates sync identities, naming
    /// the upgrade rather than pairing as the fix.
    pub fn keys(&self) -> Result<&HandshakeKeys, VersionError> {
        self.keys
            .as_ref()
            .ok_or_else(|| VersionError::KeylessHello {
                peer: self.machine_id.clone(),
            })
    }

    pub fn from_arg(arg: &str) -> Result<Self> {
        let json = URL_SAFE_NO_PAD
            .decode(arg.trim())
            .context("decoding the sync hello")?;
        serde_json::from_slice(&json).context("parsing the sync hello")
    }
}

/// The last line `sb sync-import` writes back to the pusher: the highest seq
/// of the pusher's log it applied without a gap.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct PushApplied {
    pub applied: u64,
}

impl PushApplied {
    pub fn to_line(&self) -> String {
        serde_json::to_string(self).expect("PushApplied serializes")
    }

    pub fn parse_line(line: &str) -> Result<Self> {
        serde_json::from_str(line.trim()).context("parsing the push result line")
    }
}

impl SyncHeader {
    pub fn current(schema_version: i64) -> Self {
        Self {
            sync_protocol: SYNC_PROTOCOL_VERSION,
            schema_version,
            hello: None,
        }
    }

    /// The versions this binary was built with.
    pub fn local() -> Self {
        Self::current(SCHEMA_VERSION)
    }

    pub fn to_line(&self) -> String {
        let json = serde_json::to_string(self).expect("SyncHeader serializes");
        format!("{HEADER_PREFIX} {json}")
    }

    pub fn parse_line(line: &str) -> Result<Self, VersionError> {
        line.trim()
            .strip_prefix(HEADER_PREFIX)
            .and_then(|rest| serde_json::from_str(rest).ok())
            .ok_or(VersionError::MissingHeader)
    }

    /// Refuses unless both sides speak the same protocol over the same schema.
    /// `local_name` and `peer_name` name the two sides in the error message.
    pub fn ensure_matches(
        &self,
        local_name: &str,
        peer: &Self,
        peer_name: &str,
    ) -> Result<(), VersionError> {
        if (self.sync_protocol, self.schema_version) == (peer.sync_protocol, peer.schema_version) {
            return Ok(());
        }
        Err(VersionError::Mismatch {
            local: self.clone(),
            local_name: local_name.to_string(),
            peer: peer.clone(),
            peer_name: peer_name.to_string(),
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VersionError {
    /// A stream or request without a header: its sender predates versioning.
    MissingHeader,
    /// The local daemon has no version route: it predates versioning.
    UnversionedDaemon,
    /// A hello without a sync identity: its sender predates them.
    KeylessHello { peer: String },
    Mismatch {
        local: SyncHeader,
        local_name: String,
        peer: SyncHeader,
        peer_name: String,
    },
}

impl fmt::Display for VersionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            VersionError::MissingHeader => write_unversioned(f, "the sender"),
            VersionError::UnversionedDaemon => write_unversioned(f, "the local daemon"),
            VersionError::KeylessHello { peer } => write!(
                f,
                "{peer} sent a sync hello without a sync identity, so it speaks sync protocol 3 \
                 or older and this side speaks protocol {SYNC_PROTOCOL_VERSION}; upgrade {peer}, \
                 nothing was applied"
            ),
            VersionError::Mismatch {
                local,
                local_name,
                peer,
                peer_name,
            } => {
                let older = if (local.sync_protocol, local.schema_version)
                    < (peer.sync_protocol, peer.schema_version)
                {
                    local_name
                } else {
                    peer_name
                };
                write!(
                    f,
                    "sync version mismatch: {local_name} has sync protocol {} and schema {}, \
                     {peer_name} has sync protocol {} and schema {}; upgrade {older}, \
                     nothing was applied",
                    local.sync_protocol,
                    local.schema_version,
                    peer.sync_protocol,
                    peer.schema_version
                )
            }
        }
    }
}

fn write_unversioned(f: &mut fmt::Formatter<'_>, side: &str) -> fmt::Result {
    write!(
        f,
        "{side} declared no sync version, so it speaks sync protocol {UNVERSIONED_PROTOCOL} \
         (second-brain 0.10.0 or older) and this side speaks protocol {SYNC_PROTOCOL_VERSION}; \
         upgrade {side}, nothing was applied"
    )
}

impl std::error::Error for VersionError {}

/// Reads the header line that must open a sync stream, skipping blank lines.
/// An empty stream is refused like a header-less one: a current sender always
/// writes the header, even when it has no records.
pub fn read_header<R: BufRead>(reader: &mut R) -> Result<SyncHeader> {
    let mut line = String::new();
    loop {
        line.clear();
        if reader
            .read_line(&mut line)
            .context("reading sync stream header")?
            == 0
        {
            return Err(VersionError::MissingHeader.into());
        }
        if !line.trim().is_empty() {
            return Ok(SyncHeader::parse_line(&line)?);
        }
    }
}

/// The versions this daemon syncs at. A binary older than the database it
/// serves is refused, because applying records it would drop the columns it
/// does not know.
pub fn store_header(sqlite: &SqliteStore) -> Result<SyncHeader> {
    let store = SyncHeader::current(sqlite.schema_version()?);
    SyncHeader::current(SCHEMA_VERSION).ensure_matches(
        "this daemon's binary",
        &store,
        "its database",
    )?;
    Ok(store)
}

pub fn check_store_header(
    sqlite: &SqliteStore,
    declared: Option<&SyncHeader>,
    sender: &str,
) -> Result<()> {
    let ours = store_header(sqlite)?;
    let theirs = declared.ok_or(VersionError::MissingHeader)?;
    ours.ensure_matches("this daemon", theirs, sender)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::io::BufReader;

    use base64::Engine;
    use base64::engine::general_purpose::STANDARD as B64;
    use chrono::{TimeZone, Utc};
    use second_brain_core::keys::DeviceRecord;
    use second_brain_core::schema::{SyncNodeType, SyncOp, SyncRecord};

    use super::*;
    use crate::crypto::{XChaChaEncryptor, seal_line};

    fn header(protocol: u32, schema: i64) -> SyncHeader {
        SyncHeader {
            sync_protocol: protocol,
            schema_version: schema,
            hello: None,
        }
    }

    #[test]
    fn header_line_round_trips() {
        let h = SyncHeader::current(7);
        let line = h.to_line();
        assert!(!line.contains('\n'));
        assert_eq!(SyncHeader::parse_line(&line).unwrap(), h);
    }

    #[test]
    fn header_line_aborts_an_unversioned_reader() {
        let line = SyncHeader::current(7).to_line();
        assert!(
            serde_json::from_str::<serde_json::Value>(&line).is_err(),
            "{line}"
        );
        assert!(B64.decode(line.trim()).is_err(), "{line}");
    }

    #[test]
    fn read_header_consumes_only_the_header_line() {
        let enc = XChaChaEncryptor::new([3; 32]);
        let record = seal_line(&enc, b"{\"local_seq\":1}").unwrap();
        let stream = format!("\n{}\n{record}\n", SyncHeader::current(7).to_line());
        let mut reader = BufReader::new(stream.as_bytes());
        assert_eq!(read_header(&mut reader).unwrap(), SyncHeader::current(7));
        let rest: Vec<String> = reader.lines().map(|l| l.unwrap()).collect();
        assert_eq!(
            rest,
            vec![record],
            "records after the header are left unread"
        );
    }

    #[test]
    fn stream_starting_with_a_record_has_no_header() {
        let enc = XChaChaEncryptor::new([3; 32]);
        let record = seal_line(&enc, b"{\"local_seq\":1}").unwrap();
        let stream = format!("{record}\n");
        let mut reader = BufReader::new(stream.as_bytes());
        let err = read_header(&mut reader).unwrap_err();
        assert_eq!(
            err.downcast_ref::<VersionError>(),
            Some(&VersionError::MissingHeader)
        );
        assert!(err.to_string().contains("protocol 1"), "{err}");
    }

    #[test]
    fn legacy_plaintext_record_is_not_a_header() {
        let legacy = r#"{"local_seq":1,"origin_machine_id":"m","origin_seq":1,"op":"create","node_type":"memory","node_id":"x","timestamp":"2026-01-01T00:00:00Z","data":null}"#;
        assert_eq!(
            SyncHeader::parse_line(legacy),
            Err(VersionError::MissingHeader)
        );
    }

    #[test]
    fn empty_stream_has_no_header() {
        let mut reader = BufReader::new(&b""[..]);
        let err = read_header(&mut reader).unwrap_err();
        assert_eq!(
            err.downcast_ref::<VersionError>(),
            Some(&VersionError::MissingHeader)
        );
    }

    #[test]
    fn matching_headers_pass() {
        let h = SyncHeader::current(7);
        assert!(h.ensure_matches("this machine", &h, "peer").is_ok());
    }

    fn sample_hello() -> Hello {
        Hello {
            machine_id: "m".to_string(),
            wall_micros: 1_767_225_600_000_000,
            synced: true,
            cursor: Some(5),
            held_origin_seq: Some((1 << 32) | 1),
            keys: Some(HandshakeKeys {
                identity_tag: Some(IdentityTag::from("m-0001020304050607".to_string())),
                epochs: vec![Epoch {
                    number: 1,
                    key_id: "08090a0b0c0d0e0f".parse().unwrap(),
                }],
                devices: vec![sample_row()],
                envelopes: vec![Envelope {
                    identity_tag: IdentityTag::from("m-0001020304050607".to_string()),
                    epoch: Epoch {
                        number: 1,
                        key_id: "08090a0b0c0d0e0f".parse().unwrap(),
                    },
                    recipient: "age1recipient".to_string(),
                    machine_id: Some("m".to_string()),
                    ciphertext: vec![1, 2, 3],
                }],
                revocations: Vec::new(),
                retirements: Vec::new(),
                causes: Vec::new(),
                routes: Vec::new(),
            }),
            peer_in: Some(3),
            log_floor: 2,
            head: 9,
            applied: None,
            merging: None,
        }
    }

    fn sample_row() -> SignedRecord {
        let key = ed25519_dalek::SigningKey::from_bytes(&[1; 32]);
        DeviceRecord {
            machine_id: "m".to_string(),
            identity_tag: "m-0001020304050607".to_string(),
            version: 1,
            name: "n".to_string(),
            age_recipient: "age1recipient".to_string(),
            ssh_public_key: "ssh-ed25519 AAAA".to_string(),
            signing_public_key: key.verifying_key().to_bytes(),
            recovery_fingerprints: vec!["fp".to_string()],
            ssh_login: Some("user".to_string()),
            ssh_port: Some(22),
            ssh_host_key: Some("ssh-ed25519 BBBB".to_string()),
        }
        .sign("m", &key)
    }

    fn with_hello(schema: i64) -> SyncHeader {
        SyncHeader {
            hello: Some(sample_hello()),
            ..SyncHeader::current(schema)
        }
    }

    #[test]
    fn header_line_with_a_hello_round_trips() {
        let h = with_hello(7);
        let line = h.to_line();
        assert!(!line.contains('\n'));
        assert_eq!(SyncHeader::parse_line(&line).unwrap(), h);
    }

    #[test]
    fn header_line_without_a_hello_omits_it() {
        let line = SyncHeader::current(7).to_line();
        assert!(!line.contains("hello"), "{line}");
        assert_eq!(SyncHeader::parse_line(&line).unwrap().hello, None);
    }

    #[test]
    fn ensure_matches_ignores_the_hello() {
        let bare = SyncHeader::current(7);
        let greeted = with_hello(7);
        assert!(
            bare.ensure_matches("this machine", &greeted, "peer")
                .is_ok()
        );
        assert!(
            greeted
                .ensure_matches("this machine", &bare, "peer")
                .is_ok()
        );
        assert!(
            with_hello(8)
                .ensure_matches("this machine", &greeted, "peer")
                .is_err()
        );
    }

    #[test]
    fn a_hello_from_before_sync_identities_asks_for_an_upgrade_not_pairing() {
        let old = r#"{"machine_id":"mac","wall_micros":1,"synced":false,"cursor":null,"held_origin_seq":null}"#;
        let hello: Hello = serde_json::from_str(old).unwrap();
        let err = hello.keys().unwrap_err().to_string();
        assert!(err.contains("upgrade mac"), "{err}");
        assert!(!err.contains("pair"), "{err}");
        assert!(sample_hello().keys().is_ok());
    }

    #[test]
    fn hello_argument_round_trips_in_one_shell_word() {
        let hello = sample_hello();
        let arg = hello.to_arg();
        assert!(
            arg.chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'),
            "{arg}"
        );
        assert_eq!(Hello::from_arg(&arg).unwrap(), hello);
        assert!(Hello::from_arg("not a hello").is_err());
    }

    #[test]
    fn protocol_mismatch_names_both_versions_and_the_older_side() {
        let err = header(2, 7)
            .ensure_matches("this machine", &header(3, 7), "pi")
            .unwrap_err()
            .to_string();
        assert!(
            err.contains("this machine has sync protocol 2 and schema 7"),
            "{err}"
        );
        assert!(err.contains("pi has sync protocol 3 and schema 7"), "{err}");
        assert!(err.contains("upgrade this machine"), "{err}");
    }

    #[test]
    fn schema_mismatch_names_the_older_side() {
        let err = header(2, 8)
            .ensure_matches("this machine", &header(2, 7), "pi")
            .unwrap_err()
            .to_string();
        assert!(
            err.contains("schema 8") && err.contains("schema 7"),
            "{err}"
        );
        assert!(err.contains("upgrade pi"), "{err}");
    }

    fn every_op() -> Vec<SyncOp> {
        let ops = vec![SyncOp::Create, SyncOp::Update, SyncOp::Delete];
        for op in &ops {
            match op {
                SyncOp::Create | SyncOp::Update | SyncOp::Delete => {}
            }
        }
        ops
    }

    fn every_node_type() -> Vec<SyncNodeType> {
        let types = vec![
            SyncNodeType::Memory,
            SyncNodeType::Entity,
            SyncNodeType::Conversation,
            SyncNodeType::Relation,
            SyncNodeType::Distillation,
        ];
        for t in &types {
            match t {
                SyncNodeType::Memory
                | SyncNodeType::Entity
                | SyncNodeType::Conversation
                | SyncNodeType::Relation
                | SyncNodeType::Distillation => {}
            }
        }
        types
    }

    fn wire_format() -> String {
        let record = SyncRecord {
            local_seq: 1,
            origin_machine_id: "m".to_string(),
            origin_seq: 2,
            op: SyncOp::Create,
            node_type: SyncNodeType::Memory,
            node_id: "n".to_string(),
            timestamp: Utc.with_ymd_and_hms(2026, 1, 2, 3, 4, 5).unwrap(),
            data: Some(serde_json::json!({"k": "v"})),
        };
        let ops: Vec<String> = every_op()
            .iter()
            .map(|o| serde_json::to_string(o).unwrap())
            .collect();
        let types: Vec<String> = every_node_type()
            .iter()
            .map(|t| serde_json::to_string(t).unwrap())
            .collect();
        format!(
            "header={}\nhello_header={}\npush_applied={}\nrecord={}\nops={}\nnode_types={}\nframe={}\nbelow_floor={}\nrefused={}\nsnapshot={}",
            SyncHeader::current(7).to_line(),
            with_hello(7).to_line(),
            PushApplied { applied: 4 }.to_line(),
            serde_json::to_string(&record).unwrap(),
            ops.join(","),
            types.join(","),
            crate::crypto::frame_format(),
            BelowFloorLine {
                after_seq: 1,
                log_floor: 2
            }
            .to_line(),
            RefusedLine {
                error: "e".into(),
                identity: true
            }
            .to_line(),
            crate::snapshot::snapshot_format(),
        )
    }

    #[test]
    fn refused_line_round_trips_and_is_not_a_header() {
        let refused = RefusedLine {
            error: "machine-b holds a different sync identity; run `sb pair`".into(),
            identity: true,
        };
        let line = refused.to_line();
        assert_eq!(RefusedLine::parse_line(&line), Some(refused));
        assert!(SyncHeader::parse_line(&line).is_err());
        assert_eq!(BelowFloorLine::parse_line(&line), None);
        assert_eq!(
            RefusedLine::parse_line(&SyncHeader::local().to_line()),
            None
        );
    }

    #[test]
    fn a_refusal_body_reads_its_error_and_identity_flag() {
        assert_eq!(
            RefusedLine::from_refusal_body(r#"{"error":"run `sb pair`","identity":true}"#),
            Some(RefusedLine {
                error: "run `sb pair`".into(),
                identity: true
            })
        );
        assert_eq!(
            RefusedLine::from_refusal_body(r#"{"error":"sync version mismatch"}"#),
            Some(RefusedLine {
                error: "sync version mismatch".into(),
                identity: false
            })
        );
    }

    #[test]
    fn a_refusal_body_asking_for_a_snapshot_or_a_wait_is_not_a_refusal() {
        for body in [
            r#"{"error":"below","below_floor":true,"after_seq":1,"log_floor":2}"#,
            r#"{"error":"merging","merging":true,"snapshot":{}}"#,
            r#"{"error":7}"#,
            r#"{"message":"no error field"}"#,
            "not json",
        ] {
            assert_eq!(RefusedLine::from_refusal_body(body), None, "{body}");
        }
    }

    #[test]
    fn wire_format_matches_the_protocol_version() {
        const PINNED: (u32, &str) = (
            9,
            "header=second-brain-sync {\"sync_protocol\":9,\"schema_version\":7}\n\
             hello_header=second-brain-sync {\"sync_protocol\":9,\"schema_version\":7,\"hello\
             \":{\"machine_id\":\"m\",\"wall_micros\":1767225600000000,\"synced\":true,\"curs\
             or\":5,\"held_origin_seq\":4294967297,\"keys\":{\"identity_tag\":\"m-00010203040\
             50607\",\"epochs\":[{\"number\":1,\"key_id\":\"08090a0b0c0d0e0f\"}],\"devices\":\
             [{\"record\":{\"machine_id\":\"m\",\"identity_tag\":\"m-0001020304050607\",\"ver\
             sion\":1,\"name\":\"n\",\"age_recipient\":\"age1recipient\",\"ssh_public_key\":\"\
             ssh-ed25519 AAAA\",\"signing_public_key\":\"8a88e3dd7409f195fd52db2d3cba5d72ca67\
             09bf1d94121bf3748801b40f6f5c\",\"recovery_fingerprints\":[\"fp\"],\"ssh_login\":\
             \"user\",\"ssh_port\":22,\"ssh_host_key\":\"ssh-ed25519 BBBB\"},\"signatures\":[\
             {\"signer\":\"m\",\"signature\":\"2db3155897e491f055ffdba51499bd9f272db9f0c4ff20\
             134883805b167ca8257a583705b6613ad5649f4cc23cab8a495630b1b8e279f17967257ba14a6c2f\
             0d\"}]}],\"envelopes\":[{\"identity_tag\":\"m-0001020304050607\",\"epoch\":{\"nu\
             mber\":1,\"key_id\":\"08090a0b0c0d0e0f\"},\"recipient\":\"age1recipient\",\"mach\
             ine_id\":\"m\",\"ciphertext\":[1,2,3]}]},\"peer_in\":3,\"log_floor\":2,\"head\":9,\"applied\":null,\"mergin\
             g\":null}}\n\
             push_applied={\"applied\":4}\n\
             record={\"local_seq\":1,\"origin_machine_id\":\"m\",\"origin_seq\":2,\"op\":\"create\",\
             \"node_type\":\"memory\",\"node_id\":\"n\",\"timestamp\":\"2026-01-02T03:04:05Z\",\
             \"data\":{\"k\":\"v\"}}\n\
             ops=\"create\",\"update\",\"delete\"\n\
             node_types=\"memory\",\"entity\",\"conversation\",\"relation\",\"distillation\"\n\
             frame=base64(0x02 | epoch u32 be | key id[8] | nonce[24] | xchacha20poly1305 \
             ciphertext, aad = first 13 bytes)\n\
             below_floor=second-brain-sync-below-floor {\"after_seq\":1,\"log_floor\":2}\n\
             refused=second-brain-sync-refused {\"error\":\"e\",\"identity\":true}\n\
             snapshot=second-brain-snapshot <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 = \"second-brain snapshot frame v1\" | epoch u32 be | key id[8] | \
             snapshot id | kind | frame index u64 be",
        );
        assert_eq!(
            (SYNC_PROTOCOL_VERSION, wire_format().as_str()),
            PINNED,
            "the sync wire format changed: bump SYNC_PROTOCOL_VERSION and re-pin this test"
        );
    }
}