second-brain-core 0.13.2

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
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
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
use std::collections::BTreeSet;
use std::fs;
use std::io::{self, ErrorKind};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, anyhow, bail, ensure};

use super::aside::{RESTORE_MARKER, aside_path, carry_over, create_private_dir};
use super::device::sync_dir;
use super::hardware::{HardwareSource, SystemHardware};
use super::storage::create_device_keys_with;
use super::{
    DEVICE_KEY_FILE, DEVICE_PUB_FILE, DeviceKeys, HARDWARE_BINDING_FILE, HeldEpoch, IdentityTag,
    KeyStorage, NEXT_STORE_KEY_FILE, RecoveryPhrase, SEALED_DEVICE_KEY_FILE, STORE_KEY_FILE,
    StoreKey, WRAPPED_STORE_KEY_FILE, found_identity, refresh_wrapped_store_key,
    unwrap_store_key_with_epochs, unwrap_store_key_with_phrase, wrap, wrap_for_recovery,
    write_store_key,
};
use crate::machine::MachineIdentity;
use crate::sqlite_store::SqliteStore;
use crate::sqlite_store::encrypt_migration::{
    FileState, PLAINTEXT_COPY_PREFIX, RewriteTo, db_file_state,
};
use crate::store::StoreAdmin;
use crate::write_queue;

const DB_FILE: &str = "brain.db";
const MACHINE_FILE: &str = "machine.toml";
const CONFIG_FILE: &str = "config.toml";

/// Files a backup never brings along: what names the old device, its keys
/// or its peers, and what belongs to the host it ran on.
const LEFT_BEHIND: &[&str] = &[
    MACHINE_FILE,
    DEVICE_KEY_FILE,
    SEALED_DEVICE_KEY_FILE,
    HARDWARE_BINDING_FILE,
    DEVICE_PUB_FILE,
    "sync.key",
    "migrated-peers.json",
    "api.token",
    ".api.token.lock",
    "sync-peers",
    "clock-reading",
    "service.json",
    "brain.db-shm",
    STORE_KEY_FILE,
    NEXT_STORE_KEY_FILE,
    "brain.db.enc",
    "brain.db.enc-journal",
    "brain.db.enc-wal",
    "brain.db.enc-shm",
];

/// Temporary files the key writers leave on a crash, which may hold the old
/// device's secret.
const LEFT_BEHIND_PREFIXES: &[&str] = &[".device.key.", ".store.key.", PLAINTEXT_COPY_PREFIX];

/// Host files the data dir being replaced keeps across a restore.
const HOST_FILES: &[&str] = &["service.json"];

pub struct RestoreRequest<'a> {
    /// A copy of a device's data dir.
    pub backup: &'a Path,
    /// The data dir to restore into; an existing one is moved aside.
    pub data_dir: &'a Path,
    pub phrase: &'a RecoveryPhrase,
    pub storage: &'a KeyStorage,
    pub now_micros: i64,
}

/// What opens a backup's keys.
#[derive(Clone, Copy)]
#[non_exhaustive]
pub enum BackupKeys<'a> {
    /// A recovery phrase the backed-up device trusted.
    Phrase(&'a RecoveryPhrase),
    /// Epochs of `K_id`: handed over by a surviving device through
    /// `sb pair --restore-keys`, or this device's own past epochs.
    Epochs(&'a [HeldEpoch]),
}

pub struct BackupRestore<'a> {
    /// A copy of a device's data dir.
    pub backup: &'a Path,
    /// The data dir to restore into; an existing one is moved aside.
    pub data_dir: &'a Path,
    pub keys: BackupKeys<'a>,
    pub storage: &'a KeyStorage,
    pub now_micros: i64,
}

#[derive(Debug)]
#[non_exhaustive]
pub struct Restored {
    pub machine: MachineIdentity,
    pub identity: IdentityTag,
    /// The identities whose epochs the phrase or the given epochs opened,
    /// kept decrypt-only.
    pub opened: Vec<IdentityTag>,
    /// The phrase of the new identity, to be shown to the user once.
    pub phrase: RecoveryPhrase,
    /// Where the data dir that was replaced now lives.
    pub moved_aside: Option<PathBuf>,
}

/// Restores a data-dir backup as a new device of a new identity: the copy
/// gets a fresh machine id and device keys first, the phrase opens the
/// backup's epochs (kept decrypt-only, wrapped to the new device), and then
/// the new identity is founded with a new phrase. Nothing in `data_dir`
/// changes until the restored copy is complete; the old one is then moved
/// aside, where the daemon deletes it after 90 days. The daemon must not be
/// running.
pub fn restore_backup(req: &RestoreRequest<'_>) -> Result<Restored> {
    restore_backup_with(&BackupRestore {
        backup: req.backup,
        data_dir: req.data_dir,
        keys: BackupKeys::Phrase(req.phrase),
        storage: req.storage,
        now_micros: req.now_micros,
    })
}

/// `restore_backup` with the backup's keys opened by a phrase or by epochs
/// of `K_id`, which are kept decrypt-only the same way.
pub fn restore_backup_with(req: &BackupRestore<'_>) -> Result<Restored> {
    restore_with_hardware(req, &SystemHardware)
}

fn restore_with_hardware(
    req: &BackupRestore<'_>,
    hardware: &dyn HardwareSource,
) -> Result<Restored> {
    let backup = check_backup(req.backup)?;
    if let Ok(target) = fs::canonicalize(req.data_dir) {
        ensure!(
            target != backup,
            "the backup is the data dir itself; restore from a copy"
        );
    }
    let parent = parent_of(req.data_dir)?;
    fs::create_dir_all(&parent).with_context(|| format!("creating {}", parent.display()))?;
    let base = dir_name(req.data_dir)?;
    let staging_prefix = format!(".{base}.restoring-");
    remove_stale_staging(&parent, &staging_prefix, &backup);
    let staging = parent.join(format!("{staging_prefix}{}", uuid::Uuid::new_v4().simple()));
    let restored = match prepare(req, &backup, &staging, hardware) {
        Ok(restored) => restored,
        Err(e) => return Err(with_staging_removed(e, &staging)),
    };
    let moved_aside = match swap_in(req.data_dir, &staging, &base, req.now_micros, rename) {
        Ok(moved) => moved,
        Err(Swap::Untouched(e)) => return Err(with_staging_removed(e, &staging)),
        Err(Swap::Stranded(e)) => return Err(e),
    };
    if let Err(e) = sync_dir(&parent) {
        tracing::warn!("the restore is in place but not yet flushed to disk: {e:#}");
    }
    Ok(Restored {
        moved_aside,
        ..restored
    })
}

/// Queued writes that the backup's plain device key opens follow the
/// restore; the daemon sets the rest aside as unreadable. The backup is only
/// read, so a key sealed to the old host's TPM is not tried.
fn reseal_backup_queue(backup: &Path, staging: &Path, device: &DeviceKeys) -> Result<()> {
    let queue = staging.join(write_queue::DIR_NAME);
    if !queue.is_dir() {
        return Ok(());
    }
    let old = match DeviceKeys::load(&backup.join(DEVICE_KEY_FILE)) {
        Ok(Some(old)) => old,
        Ok(None) => return Ok(()),
        Err(e) => {
            tracing::warn!("sealed queued writes in the backup stay unreadable: {e:#}");
            return Ok(());
        }
    };
    write_queue::reseal_dir(&queue, old.age_identity(), &[device.age_recipient()])?;
    Ok(())
}

/// The canonical path of `backup` when it holds a store, so a caller can
/// refuse a wrong path before it asks for keys.
pub fn check_backup(backup: &Path) -> Result<PathBuf> {
    let canonical = fs::canonicalize(backup)
        .with_context(|| format!("reading the backup {}", backup.display()))?;
    ensure!(
        canonical.join(DB_FILE).is_file(),
        "{} holds no {DB_FILE}; name a copy of a second-brain data dir",
        canonical.display()
    );
    Ok(canonical)
}

fn prepare(
    req: &BackupRestore<'_>,
    backup: &Path,
    staging: &Path,
    hardware: &dyn HardwareSource,
) -> Result<Restored> {
    if let BackupKeys::Epochs(held) = req.keys {
        ensure!(
            !held.is_empty(),
            "no key epoch was given, so the backup cannot be opened"
        );
    }
    create_private_dir(staging)?;
    copy_tree(backup, staging, true)?;
    let machine = MachineIdentity::load_or_create(staging)?;
    let device = create_device_keys_with(staging, req.storage, hardware)?;
    reseal_backup_queue(backup, staging, &device)?;
    let store = open_backup_store(&staging.join(DB_FILE), staging, req.keys, &machine.id)
        .context("opening the restored database")?;
    store.register_machine(&machine)?;

    let reopened;
    let old_epochs: &[HeldEpoch] = match req.keys {
        BackupKeys::Phrase(phrase) => {
            reopened =
                reopen_old_epochs(&store, phrase, &device, req.now_micros).with_context(|| {
                    format!("opening {} with the recovery phrase", backup.display())
                })?;
            &reopened
        }
        BackupKeys::Epochs(held) => {
            keep_epochs(&store, held, &device, req.now_micros)?;
            held
        }
    };

    let phrase = RecoveryPhrase::generate()?;
    let recipient = phrase.recipient()?;
    let founded = found_identity(
        &store,
        &device,
        &machine.name,
        std::slice::from_ref(&recipient),
        req.now_micros,
    )?;
    for held in old_epochs {
        wrap_for_recovery(
            &store,
            &held.identity,
            &[(held.epoch, &held.key)],
            std::slice::from_ref(&recipient),
        )?;
    }
    if cfg!(feature = "encryption") {
        rekey_restored(&store, staging, &device)?;
    }
    drop(store);
    let opened: BTreeSet<&str> = old_epochs.iter().map(|h| h.identity.as_str()).collect();
    let opened = opened
        .into_iter()
        .map(|t| IdentityTag::from(t.to_string()))
        .collect();
    sync_tree(staging)?;
    Ok(Restored {
        machine,
        identity: founded.tag,
        opened,
        phrase,
        moved_aside: None,
    })
}

/// An encrypted backup opens under the store key its `store.key.wrapped`
/// gives up to the phrase or an epoch; a plaintext one, from before the
/// migration, opens as it is.
fn open_backup_store(
    db: &Path,
    staging: &Path,
    keys: BackupKeys<'_>,
    machine_id: &str,
) -> Result<SqliteStore> {
    match db_file_state(db)? {
        FileState::Encrypted => {
            let unwrapped = match keys {
                BackupKeys::Phrase(phrase) => unwrap_store_key_with_phrase(staging, phrase)?,
                BackupKeys::Epochs(held) => unwrap_store_key_with_epochs(staging, held)?,
            };
            let key = unwrapped.ok_or_else(|| {
                anyhow!(
                    "the backup's {DB_FILE} is encrypted but it holds no \
                     {WRAPPED_STORE_KEY_FILE}; restore a copy of the whole data dir"
                )
            })?;
            SqliteStore::open_with_key(db, machine_id.to_string(), &key)
        }
        _ => SqliteStore::open(db, machine_id.to_string()),
    }
}

/// Moves the restored store to a store key of its own, so whoever holds
/// the backup's key or the old identity's `K_id` opens no copy of the
/// restored device's store.
fn rekey_restored(store: &SqliteStore, staging: &Path, device: &DeviceKeys) -> Result<()> {
    let key = StoreKey::generate()?;
    write_store_key(staging, STORE_KEY_FILE, &key, device)?;
    store
        .rewrite_under_key(RewriteTo::Key(&key))
        .context("rewriting the restored store under a new store key")?;
    refresh_wrapped_store_key(store, staging, device, &key)
        .context("wrapping the new store key to the new identity")?;
    Ok(())
}

/// Opens every envelope the phrase can open and wraps that epoch to the new
/// device under its old identity, where it only decrypts.
fn reopen_old_epochs(
    store: &SqliteStore,
    phrase: &RecoveryPhrase,
    device: &DeviceKeys,
    now_micros: i64,
) -> Result<Vec<HeldEpoch>> {
    let identity = phrase.identity()?;
    let own = store.machine_id().to_string();
    let recipient = device.age_recipient();
    let mut opened = Vec::new();
    for envelope in store.recovery_envelopes(&phrase.recipient()?)? {
        let key = match envelope.open(&identity) {
            Ok(key) => key,
            Err(e) => {
                tracing::warn!("skipping a recovery envelope the phrase cannot open: {e:#}");
                continue;
            }
        };
        let rewrapped = wrap(
            &key,
            envelope.epoch.number,
            &envelope.identity_tag,
            &recipient,
            Some(own.clone()),
        )?;
        store.put_keys_durably(
            &envelope.identity_tag,
            &[envelope.epoch],
            &[rewrapped],
            now_micros,
        )?;
        opened.push(HeldEpoch::new(envelope.identity_tag, envelope.epoch, key));
    }
    if opened.is_empty() {
        bail!(
            "this recovery phrase opens no key in the backup; check the phrase, or restore a \
             backup of a device that trusted it"
        );
    }
    Ok(opened)
}

/// Wraps each epoch to the new device under its own identity, where it
/// only decrypts.
fn keep_epochs(
    store: &SqliteStore,
    held: &[HeldEpoch],
    device: &DeviceKeys,
    now_micros: i64,
) -> Result<()> {
    let own = store.machine_id().to_string();
    let recipient = device.age_recipient();
    for h in held {
        let envelope = wrap(
            &h.key,
            h.epoch.number,
            &h.identity,
            &recipient,
            Some(own.clone()),
        )?;
        store.put_keys_durably(&h.identity, &[h.epoch], &[envelope], now_micros)?;
    }
    Ok(())
}

enum Swap {
    /// `data_dir` is as it was; the staged copy can go.
    Untouched(anyhow::Error),
    /// Neither copy is at `data_dir`; the error names where both are.
    Stranded(anyhow::Error),
}

fn rename(from: &Path, to: &Path) -> io::Result<()> {
    fs::rename(from, to)
}

fn swap_in(
    data_dir: &Path,
    staging: &Path,
    base: &str,
    now_micros: i64,
    rename: impl Fn(&Path, &Path) -> io::Result<()>,
) -> Result<Option<PathBuf>, Swap> {
    let parent = parent_of(data_dir).map_err(Swap::Untouched)?;
    let moved_aside = match fs::symlink_metadata(data_dir) {
        Ok(_) => {
            let carried = HOST_FILES
                .iter()
                .try_for_each(|name| carry_over(&data_dir.join(name), &staging.join(name)))
                .and_then(|()| {
                    if staging.join(CONFIG_FILE).exists() {
                        return Ok(());
                    }
                    carry_over(&data_dir.join(CONFIG_FILE), &staging.join(CONFIG_FILE))
                })
                .and_then(|()| sync_dir(staging));
            carried.map_err(Swap::Untouched)?;
            let aside = aside_path(&parent, base, RESTORE_MARKER, now_micros);
            rename(data_dir, &aside)
                .with_context(|| {
                    format!("moving {} aside to {}", data_dir.display(), aside.display())
                })
                .map_err(Swap::Untouched)?;
            Some(aside)
        }
        Err(e) if e.kind() == ErrorKind::NotFound => None,
        Err(e) => {
            return Err(Swap::Untouched(
                anyhow!(e).context(format!("reading {}", data_dir.display())),
            ));
        }
    };
    let Err(e) = rename(staging, data_dir) else {
        return Ok(moved_aside);
    };
    let error = anyhow!(e).context(format!(
        "moving the restored copy {} into {}",
        staging.display(),
        data_dir.display()
    ));
    let Some(aside) = moved_aside else {
        return Err(Swap::Untouched(error));
    };
    match rename(&aside, data_dir) {
        Ok(()) => Err(Swap::Untouched(error)),
        Err(back) => Err(Swap::Stranded(error.context(format!(
            "the previous data dir is at {} and could not be moved back ({back}); the restored \
             copy is at {}",
            aside.display(),
            staging.display()
        )))),
    }
}

/// Flushes every file and directory under `dir`, so a power cut after the
/// swap cannot keep the rename but lose what it points to.
fn sync_tree(dir: &Path) -> Result<()> {
    for entry in fs::read_dir(dir).with_context(|| format!("reading {}", dir.display()))? {
        let entry = entry?;
        let path = entry.path();
        let kind = entry.file_type()?;
        if kind.is_dir() {
            sync_tree(&path)?;
        } else if kind.is_file() {
            fs::File::open(&path)
                .and_then(|f| f.sync_all())
                .with_context(|| format!("syncing {}", path.display()))?;
        }
    }
    sync_dir(dir)
}

fn copy_tree(from: &Path, to: &Path, top: bool) -> Result<()> {
    for entry in fs::read_dir(from).with_context(|| format!("reading {}", from.display()))? {
        let entry = entry?;
        let name = entry.file_name();
        if top
            && name.to_str().is_some_and(|n| {
                LEFT_BEHIND.contains(&n) || LEFT_BEHIND_PREFIXES.iter().any(|p| n.starts_with(p))
            })
        {
            continue;
        }
        let source = entry.path();
        let target = to.join(&name);
        let kind = entry.file_type()?;
        if kind.is_dir() {
            fs::create_dir(&target).with_context(|| format!("creating {}", target.display()))?;
            copy_tree(&source, &target, false)?;
        } else if kind.is_file() {
            fs::copy(&source, &target)
                .with_context(|| format!("copying {} to {}", source.display(), target.display()))?;
        } else if kind.is_symlink() {
            copy_symlink(&source, &target)?;
        } else {
            tracing::warn!("restore skips {}, which is not a file", source.display());
        }
    }
    Ok(())
}

#[cfg(unix)]
fn copy_symlink(source: &Path, target: &Path) -> Result<()> {
    let link = fs::read_link(source).with_context(|| format!("reading {}", source.display()))?;
    std::os::unix::fs::symlink(&link, target)
        .with_context(|| format!("linking {} to {}", target.display(), link.display()))
}

#[cfg(not(unix))]
fn copy_symlink(source: &Path, _target: &Path) -> Result<()> {
    tracing::warn!("restore skips the link {}", source.display());
    Ok(())
}

/// Removes copies a restore cut short by a crash left beside the data dir,
/// except `backup`, which may be one of them after a failed swap.
fn remove_stale_staging(parent: &Path, prefix: &str, backup: &Path) {
    let Ok(entries) = fs::read_dir(parent) else {
        return;
    };
    for entry in entries.flatten() {
        if !entry.file_name().to_string_lossy().starts_with(prefix)
            || fs::canonicalize(entry.path()).is_ok_and(|p| p == backup)
        {
            continue;
        }
        if let Err(e) = fs::remove_dir_all(entry.path()) {
            tracing::warn!(
                "cannot remove the partial restore {}: {e}",
                entry.path().display()
            );
        }
    }
}

fn with_staging_removed(error: anyhow::Error, staging: &Path) -> anyhow::Error {
    match fs::remove_dir_all(staging) {
        Ok(()) => error,
        Err(e) if e.kind() == ErrorKind::NotFound => error,
        Err(e) => error.context(format!(
            "also failed to remove the partial restore {}: {e}",
            staging.display()
        )),
    }
}

fn parent_of(path: &Path) -> Result<PathBuf> {
    match path.parent() {
        Some(dir) if !dir.as_os_str().is_empty() => Ok(dir.to_path_buf()),
        Some(_) => Ok(PathBuf::from(".")),
        None => bail!("{} has no parent directory", path.display()),
    }
}

fn dir_name(path: &Path) -> Result<String> {
    path.file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .ok_or_else(|| anyhow!("{} has no directory name", path.display()))
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::keys::{DeviceRecord, Epoch, IdentityKey, create_device_keys};
    use crate::schema::{Memory, MemoryType};
    use crate::store::Store;

    const NOW: i64 = 1_793_600_000_000_000;

    struct OldDevice {
        dir: tempfile::TempDir,
        tag: IdentityTag,
        phrase: RecoveryPhrase,
        memory: Memory,
    }

    fn open_restored(data_dir: &Path, machine_id: &str) -> SqliteStore {
        let db = data_dir.join(DB_FILE);
        if db_file_state(&db).unwrap() != FileState::Encrypted {
            return SqliteStore::open(&db, machine_id.to_string()).unwrap();
        }
        let device = super::super::load_device_keys(data_dir).unwrap().unwrap();
        let key = super::super::store_key_for(&db, &device).unwrap();
        SqliteStore::open_with_key(&db, machine_id.to_string(), &key).unwrap()
    }

    #[cfg(feature = "encryption")]
    #[test]
    fn an_encrypted_backup_opens_with_the_phrase_and_comes_back_under_a_new_store_key() {
        let old = old_device();
        let old_machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
        let old_device_keys = super::super::load_device_keys(old.dir.path())
            .unwrap()
            .unwrap();
        {
            let store =
                SqliteStore::open(&old.dir.path().join(DB_FILE), old_machine.id.clone()).unwrap();
            super::super::encrypt_store(&store, old.dir.path(), &old_device_keys).unwrap();
        }
        assert!(old.dir.path().join(WRAPPED_STORE_KEY_FILE).exists());
        let old_store_key =
            super::super::read_store_key(&old.dir.path().join(STORE_KEY_FILE), &old_device_keys)
                .unwrap()
                .unwrap();

        let home = tempfile::tempdir().unwrap();
        let data_dir = home.path().join(".second-brain");
        let restored = restore_backup(&RestoreRequest {
            backup: old.dir.path(),
            data_dir: &data_dir,
            phrase: &old.phrase,
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap();

        let db = data_dir.join(DB_FILE);
        assert_eq!(db_file_state(&db).unwrap(), FileState::Encrypted);
        assert!(
            SqliteStore::open_with_key(&db, restored.machine.id.clone(), &old_store_key).is_err(),
            "the backup's store key must not open the restored store"
        );
        let store = open_restored(&data_dir, &restored.machine.id);
        assert_eq!(
            store.get_memory(old.memory.id).unwrap().unwrap().content,
            old.memory.content
        );
        let new_key = super::super::unwrap_store_key_with_phrase(&data_dir, &restored.phrase)
            .unwrap()
            .expect("the restored data dir wraps its store key to the new phrase");
        assert!(SqliteStore::open_with_key(&db, restored.machine.id.clone(), &new_key).is_ok());
        assert!(
            super::super::unwrap_store_key_with_phrase(&data_dir, &old.phrase).is_err(),
            "the old phrase opens no store key of the new identity"
        );
    }

    const SEALED_ENTRY: &str = "0000000000000000001-0b5c2f9e-5d0e-4c1a-9a57-0c7f4f1f2a10.sealed";

    fn queued_forget() -> write_queue::Entry {
        write_queue::Entry {
            tool: "forget".to_string(),
            method: "DELETE".to_string(),
            path: "/v1/memory/m-1".to_string(),
            query: Some("hard=false&reason=wrong".to_string()),
            body: None,
        }
    }

    /// A data dir of a device that synced with a peer under a migrated
    /// identity and trusted one recovery phrase.
    fn old_device() -> OldDevice {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path();
        let machine = MachineIdentity::load_or_create(path).unwrap();
        let device = create_device_keys(path, &KeyStorage::File).unwrap();
        let store = SqliteStore::open(&path.join(DB_FILE), machine.id.clone()).unwrap();
        store.register_machine(&machine).unwrap();

        let epoch0 = IdentityKey::generate().unwrap();
        let epoch1 = IdentityKey::generate().unwrap();
        let tag = IdentityTag::migrated(&epoch0.key_id());
        let epochs = [
            Epoch {
                number: 0,
                key_id: epoch0.key_id(),
            },
            Epoch {
                number: 1,
                key_id: epoch1.key_id(),
            },
        ];
        let own = machine.id.clone();
        let envelopes = [
            wrap(&epoch0, 0, &tag, &device.age_recipient(), Some(own.clone())).unwrap(),
            wrap(&epoch1, 1, &tag, &device.age_recipient(), Some(own.clone())).unwrap(),
        ];
        let row = DeviceRecord {
            machine_id: own.clone(),
            identity_tag: tag.as_str().to_string(),
            version: 1,
            name: machine.name.clone(),
            age_recipient: device.age_recipient().to_string(),
            ssh_public_key: device.ssh_public_key(),
            signing_public_key: device.verifying_key().to_bytes(),
            recovery_fingerprints: Vec::new(),
            ssh_login: None,
            ssh_port: None,
            ssh_host_key: None,
        }
        .sign(&own, device.signing_key());
        store
            .insert_first_device_record(&row, &own, &device.verifying_key())
            .unwrap();
        store
            .put_keys_durably(&tag, &epochs, &envelopes, NOW)
            .unwrap();

        let phrase = RecoveryPhrase::generate().unwrap();
        let held = [(epochs[0], &epoch0), (epochs[1], &epoch1)];
        wrap_for_recovery(&store, &tag, &held, &[phrase.recipient().unwrap()]).unwrap();

        let memory = Memory::new(
            "the restored store keeps this".to_string(),
            MemoryType::Semantic,
            "test".to_string(),
            String::new(),
        );
        store.store_memory(&memory).unwrap();
        store.record_peer_ack("old-peer", 3, NOW).unwrap();
        store.pin_peer("old-peer", 2).unwrap();
        assert_eq!(store.peer_acks().unwrap().len(), 1);
        drop(store);

        fs::write(path.join("sync-peers"), "someone@old-peer\n").unwrap();
        fs::write(path.join(".device.key.0123.tmp"), "half-written key").unwrap();
        fs::write(path.join("api.token"), "old-token").unwrap();
        fs::write(path.join(DEVICE_PUB_FILE), "age1old\n").unwrap();
        fs::write(path.join("service.json"), "{\"from\":\"backup\"}").unwrap();
        fs::create_dir(path.join("queue")).unwrap();
        fs::write(path.join("queue").join("pending.json"), "{}").unwrap();
        fs::write(
            path.join("queue").join(SEALED_ENTRY),
            queued_forget()
                .to_sealed_file(&[device.age_recipient()])
                .unwrap(),
        )
        .unwrap();
        OldDevice {
            dir,
            tag,
            phrase,
            memory,
        }
    }

    fn pins(store: &SqliteStore) -> i64 {
        store
            .conn()
            .query_row("SELECT COUNT(*) FROM peer_pins", [], |r| r.get(0))
            .unwrap()
    }

    #[test]
    fn the_phrase_restores_a_backup_under_a_new_machine_id_and_identity() {
        let old = old_device();
        let old_machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
        let old_key = fs::read(old.dir.path().join(DEVICE_KEY_FILE)).unwrap();

        let home = tempfile::tempdir().unwrap();
        let data_dir = home.path().join(".second-brain");
        fs::create_dir(&data_dir).unwrap();
        let fresh = MachineIdentity::load_or_create(&data_dir).unwrap();
        SqliteStore::open(&data_dir.join(DB_FILE), fresh.id.clone()).unwrap();
        fs::write(data_dir.join("service.json"), "{\"from\":\"host\"}").unwrap();

        let typed = RecoveryPhrase::parse(&old.phrase.expose_text()).unwrap();
        let restored = restore_backup(&RestoreRequest {
            backup: old.dir.path(),
            data_dir: &data_dir,
            phrase: &typed,
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap();

        let machine = MachineIdentity::load_or_create(&data_dir).unwrap();
        assert_eq!(machine.id, restored.machine.id);
        assert_ne!(machine.id, old_machine.id);
        assert_ne!(machine.id, fresh.id);
        assert_ne!(fs::read(data_dir.join(DEVICE_KEY_FILE)).unwrap(), old_key);
        assert!(!data_dir.join("sync-peers").exists());
        assert!(!data_dir.join(".device.key.0123.tmp").exists());
        assert!(!data_dir.join("api.token").exists());
        assert!(!data_dir.join(DEVICE_PUB_FILE).exists());
        assert_eq!(
            fs::read_to_string(data_dir.join("service.json")).unwrap(),
            "{\"from\":\"host\"}"
        );
        assert!(data_dir.join("queue").join("pending.json").exists());
        let sealed = fs::read(data_dir.join("queue").join(SEALED_ENTRY)).unwrap();
        let device = super::super::load_device_keys(&data_dir).unwrap().unwrap();
        let opened = write_queue::open(&sealed, Some(device.age_identity())).unwrap();
        assert_eq!(opened, queued_forget());
        let aside = restored.moved_aside.clone().unwrap();
        assert!(aside.join(DB_FILE).exists());
        assert_eq!(
            MachineIdentity::load_or_create(&aside).unwrap().id,
            fresh.id
        );
        assert!(
            old.dir.path().join("sync-peers").exists(),
            "the backup is untouched"
        );

        let expected = if cfg!(feature = "encryption") {
            FileState::Encrypted
        } else {
            FileState::Plaintext
        };
        assert_eq!(db_file_state(&data_dir.join(DB_FILE)).unwrap(), expected);
        let store = open_restored(&data_dir, &machine.id);
        assert_eq!(
            store.get_memory(old.memory.id).unwrap().unwrap().content,
            old.memory.content
        );
        let row = store.device_record(&machine.id).unwrap().unwrap();
        assert_eq!(row.current.record.identity_tag, restored.identity.as_str());
        assert!(restored.identity.as_str().starts_with("f-"));
        assert_ne!(restored.identity, old.tag);
        assert_eq!(restored.opened, vec![old.tag.clone()]);
        assert_eq!(
            row.current.record.recovery_fingerprints,
            vec![restored.phrase.fingerprint().unwrap()]
        );

        assert!(
            store.peer_acks().unwrap().is_empty(),
            "old neighbours are cleared"
        );
        assert_eq!(pins(&store), 0);

        let device = super::super::load_device_keys(&data_dir).unwrap().unwrap();
        let mine = store.envelopes_for_machine(&machine.id).unwrap();
        let old_epochs: Vec<u32> = mine
            .iter()
            .filter(|e| e.identity_tag == old.tag)
            .map(|e| e.epoch.number)
            .collect();
        assert_eq!(
            old_epochs,
            vec![0, 1],
            "old epochs stay readable on the new device"
        );
        for envelope in &mine {
            envelope.open(device.age_identity()).unwrap();
        }
        let new_epochs = store.key_epochs(&restored.identity).unwrap();
        assert_eq!(new_epochs.len(), 1);
        assert_eq!(new_epochs[0].epoch.number, 1);

        let shown = RecoveryPhrase::parse(&restored.phrase.expose_text()).unwrap();
        let recipients = store.recovery_recipients(&restored.identity).unwrap();
        assert_eq!(recipients.len(), 1);
        assert_eq!(
            recipients[0].to_string(),
            shown.recipient().unwrap().to_string()
        );
        let envelope = store
            .recovery_envelopes(&shown.recipient().unwrap())
            .unwrap();
        assert_eq!(envelope.len(), 3, "the new epoch and both old ones");
        envelope[0].open(&shown.identity().unwrap()).unwrap();
        assert!(
            store
                .recovery_recipients(&restored.identity)
                .unwrap()
                .iter()
                .all(|r| r.to_string() != old.phrase.recipient().unwrap().to_string()),
            "the old phrase is not trusted by the new identity"
        );
        let leftovers: Vec<_> = fs::read_dir(home.path())
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_name().to_string_lossy().contains("restoring"))
            .collect();
        assert!(leftovers.is_empty());
    }

    #[test]
    fn a_second_restore_with_the_new_phrase_opens_every_earlier_identity() {
        let old = old_device();
        let first_home = tempfile::tempdir().unwrap();
        let first_dir = first_home.path().join(".second-brain");
        let first = restore_backup(&RestoreRequest {
            backup: old.dir.path(),
            data_dir: &first_dir,
            phrase: &old.phrase,
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap();

        let second_home = tempfile::tempdir().unwrap();
        let second_dir = second_home.path().join(".second-brain");
        let second = restore_backup(&RestoreRequest {
            backup: &first_dir,
            data_dir: &second_dir,
            phrase: &first.phrase,
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap();
        let opened: BTreeSet<&str> = second.opened.iter().map(IdentityTag::as_str).collect();
        assert_eq!(
            opened,
            BTreeSet::from([old.tag.as_str(), first.identity.as_str()])
        );

        let store = open_restored(&second_dir, &second.machine.id);
        let device = super::super::load_device_keys(&second_dir)
            .unwrap()
            .unwrap();
        let mine = store.envelopes_for_machine(&second.machine.id).unwrap();
        let held = |tag: &IdentityTag| -> Vec<u32> {
            mine.iter()
                .filter(|e| &e.identity_tag == tag)
                .map(|e| {
                    e.open(device.age_identity()).unwrap();
                    e.epoch.number
                })
                .collect()
        };
        assert_eq!(held(&old.tag), vec![0, 1]);
        assert_eq!(held(&first.identity), vec![1]);
        assert_eq!(held(&second.identity), vec![1]);
        let last = store
            .recovery_envelopes(&second.phrase.recipient().unwrap())
            .unwrap();
        assert_eq!(last.len(), 4, "every epoch reaches the newest phrase");
    }

    #[test]
    fn a_restore_from_a_stranded_copy_keeps_that_copy() {
        let old = old_device();
        let home = tempfile::tempdir().unwrap();
        let stranded = home.path().join("..second-brain.restoring-0123");
        fs::create_dir(&stranded).unwrap();
        copy_tree(old.dir.path(), &stranded, false).unwrap();
        restore_backup(&RestoreRequest {
            backup: &stranded,
            data_dir: &home.path().join(".second-brain"),
            phrase: &old.phrase,
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap();
        assert!(stranded.join(DB_FILE).exists());
    }

    #[test]
    fn a_restore_binds_the_new_device_keys_to_this_hardware_not_the_backups() {
        use crate::keys::{
            HardwareCheck, HardwareId, check_hardware_with, read_binding, write_binding,
        };

        struct Here;
        impl HardwareSource for Here {
            fn read(&self, _data_dir: &Path) -> HardwareId {
                HardwareId::parse("disk:the-new-hardware")
            }
        }

        let old = old_device();
        write_binding(
            old.dir.path(),
            &HardwareId::parse("disk:the-backups-hardware"),
        )
        .unwrap();
        let home = tempfile::tempdir().unwrap();
        let data_dir = home.path().join(".second-brain");
        let restored = restore_with_hardware(
            &BackupRestore {
                backup: old.dir.path(),
                data_dir: &data_dir,
                keys: BackupKeys::Phrase(&old.phrase),
                storage: &KeyStorage::File,
                now_micros: NOW,
            },
            &Here,
        )
        .unwrap();
        assert_eq!(
            read_binding(&data_dir).unwrap(),
            Some(HardwareId::parse("disk:the-new-hardware"))
        );
        assert_eq!(
            check_hardware_with(&data_dir, &data_dir, &KeyStorage::File, &Here).unwrap(),
            HardwareCheck::Bound
        );
        assert_eq!(
            MachineIdentity::load_or_create(&data_dir).unwrap().id,
            restored.machine.id
        );
    }

    #[test]
    fn a_restore_into_a_missing_data_dir_creates_it() {
        let old = old_device();
        let home = tempfile::tempdir().unwrap();
        let data_dir = home.path().join(".second-brain");
        let stale = home.path().join("..second-brain.restoring-0123");
        fs::create_dir(&stale).unwrap();
        fs::write(stale.join(DB_FILE), "left by a crash").unwrap();
        let restored = restore_backup(&RestoreRequest {
            backup: old.dir.path(),
            data_dir: &data_dir,
            phrase: &old.phrase,
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap();
        assert!(restored.moved_aside.is_none());
        assert!(!stale.exists(), "a crashed restore's copy is removed");
        assert!(data_dir.join(DB_FILE).exists());
        assert!(!data_dir.join("service.json").exists());
    }

    fn handed_from(old: &OldDevice) -> Vec<HeldEpoch> {
        let machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
        let device = super::super::load_device_keys(old.dir.path())
            .unwrap()
            .unwrap();
        let store = SqliteStore::open(&old.dir.path().join(DB_FILE), machine.id).unwrap();
        let held = super::super::every_held_epoch(&store, &device).unwrap();
        let transit = DeviceKeys::generate().unwrap();
        let envelopes = super::super::hand_over(&held, &transit.age_recipient()).unwrap();
        super::super::open_handed(&envelopes, &transit).unwrap()
    }

    #[cfg(feature = "encryption")]
    #[test]
    fn epochs_a_surviving_device_hands_over_restore_an_encrypted_backup_as_a_new_identity() {
        let old = old_device();
        let backup = tempfile::tempdir().unwrap();
        {
            let machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
            let device = super::super::load_device_keys(old.dir.path())
                .unwrap()
                .unwrap();
            let store = SqliteStore::open(&old.dir.path().join(DB_FILE), machine.id).unwrap();
            super::super::encrypt_store(&store, old.dir.path(), &device).unwrap();
        }
        copy_tree(old.dir.path(), backup.path(), false).unwrap();
        let held = {
            let machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
            let device = super::super::load_device_keys(old.dir.path())
                .unwrap()
                .unwrap();
            let key = super::super::store_key_for(&old.dir.path().join(DB_FILE), &device).unwrap();
            let store = SqliteStore::open_with_key(&old.dir.path().join(DB_FILE), machine.id, &key)
                .unwrap();
            let held = super::super::every_held_epoch(&store, &device).unwrap();
            let transit = DeviceKeys::generate().unwrap();
            let envelopes = super::super::hand_over(&held, &transit.age_recipient()).unwrap();
            super::super::open_handed(&envelopes, &transit).unwrap()
        };

        let home = tempfile::tempdir().unwrap();
        let data_dir = home.path().join(".second-brain");
        let restored = restore_backup_with(&BackupRestore {
            backup: backup.path(),
            data_dir: &data_dir,
            keys: BackupKeys::Epochs(&held),
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap();

        assert_ne!(restored.identity, old.tag);
        assert_eq!(restored.opened, vec![old.tag.clone()]);
        let store = open_restored(&data_dir, &restored.machine.id);
        assert_eq!(
            store.get_memory(old.memory.id).unwrap().unwrap().content,
            old.memory.content
        );
        let device = super::super::load_device_keys(&data_dir).unwrap().unwrap();
        let kept: Vec<u32> = store
            .envelopes_for_machine(&restored.machine.id)
            .unwrap()
            .iter()
            .filter(|e| e.identity_tag == old.tag)
            .map(|e| e.epoch.number)
            .collect();
        assert_eq!(kept, vec![0, 1], "the handed epochs stay decrypt-only");
        let opened = super::super::every_held_epoch(&store, &device).unwrap();
        assert_eq!(opened.len(), 3, "both old epochs and the new identity's");
        let recovery = store
            .recovery_envelopes(&restored.phrase.recipient().unwrap())
            .unwrap();
        assert_eq!(recovery.len(), 3, "the new phrase opens the old epochs too");
    }

    #[cfg(feature = "encryption")]
    #[test]
    fn a_surviving_device_restores_onto_itself_with_its_own_past_epochs() {
        let old = old_device();
        {
            let machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
            let device = super::super::load_device_keys(old.dir.path())
                .unwrap()
                .unwrap();
            let store = SqliteStore::open(&old.dir.path().join(DB_FILE), machine.id).unwrap();
            super::super::encrypt_store(&store, old.dir.path(), &device).unwrap();
        }
        let backup = tempfile::tempdir().unwrap();
        copy_tree(old.dir.path(), backup.path(), false).unwrap();
        let own = super::super::every_held_epoch_in(old.dir.path(), &KeyStorage::File).unwrap();
        assert_eq!(own.len(), 2);

        let restored = restore_backup_with(&BackupRestore {
            backup: backup.path(),
            data_dir: old.dir.path(),
            keys: BackupKeys::Epochs(&own),
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap();

        let aside = restored
            .moved_aside
            .clone()
            .expect("its own store moves aside");
        assert!(aside.to_string_lossy().contains(".before-restore-"));
        assert!(aside.join(DB_FILE).exists());
        assert!(aside.join(DEVICE_KEY_FILE).exists());
        assert_eq!(restored.opened, vec![old.tag.clone()]);
        assert_eq!(
            db_file_state(&backup.path().join(DB_FILE)).unwrap(),
            FileState::Encrypted,
            "the backup opens only through the wrapped store key"
        );
        let store = open_restored(old.dir.path(), &restored.machine.id);
        assert_eq!(
            store.get_memory(old.memory.id).unwrap().unwrap().content,
            old.memory.content
        );
        fs::remove_dir_all(&aside).unwrap();
    }

    #[cfg(feature = "encryption")]
    #[test]
    fn handed_epochs_that_do_not_seal_the_backup_change_nothing() {
        let old = old_device();
        {
            let machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
            let device = super::super::load_device_keys(old.dir.path())
                .unwrap()
                .unwrap();
            let store = SqliteStore::open(&old.dir.path().join(DB_FILE), machine.id).unwrap();
            super::super::encrypt_store(&store, old.dir.path(), &device).unwrap();
        }
        let held = handed_from(&old_device());
        let home = tempfile::tempdir().unwrap();
        let data_dir = home.path().join(".second-brain");
        fs::create_dir(&data_dir).unwrap();
        let fresh = MachineIdentity::load_or_create(&data_dir).unwrap();

        let err = restore_backup_with(&BackupRestore {
            backup: old.dir.path(),
            data_dir: &data_dir,
            keys: BackupKeys::Epochs(&held),
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap_err();

        assert!(
            format!("{err:#}").contains("none of the keys at hand"),
            "{err:#}"
        );
        assert_eq!(
            MachineIdentity::load_or_create(&data_dir).unwrap().id,
            fresh.id
        );
        let names: Vec<String> = fs::read_dir(home.path())
            .unwrap()
            .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
            .collect();
        assert_eq!(names, vec![".second-brain".to_string()]);
    }

    #[test]
    fn no_handed_epochs_change_nothing() {
        let old = old_device();
        let home = tempfile::tempdir().unwrap();
        let data_dir = home.path().join(".second-brain");
        fs::create_dir(&data_dir).unwrap();
        let fresh = MachineIdentity::load_or_create(&data_dir).unwrap();

        let err = restore_backup_with(&BackupRestore {
            backup: old.dir.path(),
            data_dir: &data_dir,
            keys: BackupKeys::Epochs(&[]),
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap_err();

        assert!(format!("{err:#}").contains("no key"), "{err:#}");
        assert_eq!(
            MachineIdentity::load_or_create(&data_dir).unwrap().id,
            fresh.id
        );
    }

    #[test]
    fn a_wrong_phrase_changes_nothing() {
        let old = old_device();
        let home = tempfile::tempdir().unwrap();
        let data_dir = home.path().join(".second-brain");
        fs::create_dir(&data_dir).unwrap();
        let fresh = MachineIdentity::load_or_create(&data_dir).unwrap();

        let other = RecoveryPhrase::generate().unwrap();
        let err = restore_backup(&RestoreRequest {
            backup: old.dir.path(),
            data_dir: &data_dir,
            phrase: &other,
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap_err();
        assert!(format!("{err:#}").contains("opens no key"), "{err:#}");
        assert_eq!(
            MachineIdentity::load_or_create(&data_dir).unwrap().id,
            fresh.id
        );
        let names: Vec<String> = fs::read_dir(home.path())
            .unwrap()
            .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
            .collect();
        assert_eq!(names, vec![".second-brain".to_string()]);
    }

    #[test]
    fn a_directory_without_a_store_is_not_a_backup() {
        let empty = tempfile::tempdir().unwrap();
        let home = tempfile::tempdir().unwrap();
        let phrase = RecoveryPhrase::generate().unwrap();
        let err = restore_backup(&RestoreRequest {
            backup: empty.path(),
            data_dir: &home.path().join(".second-brain"),
            phrase: &phrase,
            storage: &KeyStorage::File,
            now_micros: NOW,
        })
        .unwrap_err();
        assert!(format!("{err:#}").contains("holds no brain.db"), "{err:#}");
    }

    /// A rename that fails on calls `first..=last`, counting from 1.
    fn failing(first: usize, last: usize) -> impl Fn(&Path, &Path) -> io::Result<()> {
        let calls = std::cell::Cell::new(0);
        move |from, to| {
            calls.set(calls.get() + 1);
            if (first..=last).contains(&calls.get()) {
                return Err(io::Error::other("injected rename failure"));
            }
            fs::rename(from, to)
        }
    }

    fn dirs_with_marker(home: &Path) -> (PathBuf, PathBuf) {
        let data_dir = home.join(".second-brain");
        let staging = home.join(".second-brain.restoring-test");
        fs::create_dir(&data_dir).unwrap();
        fs::write(data_dir.join("marker"), "old").unwrap();
        fs::create_dir(&staging).unwrap();
        fs::write(staging.join("marker"), "restored").unwrap();
        (data_dir, staging)
    }

    #[test]
    fn a_failed_swap_puts_the_old_data_dir_back() {
        let home = tempfile::tempdir().unwrap();
        let (data_dir, staging) = dirs_with_marker(home.path());
        let Err(Swap::Untouched(err)) =
            swap_in(&data_dir, &staging, ".second-brain", NOW, failing(2, 2))
        else {
            panic!("the swap must fail with the data dir back in place");
        };
        assert!(format!("{err:#}").contains("injected"), "{err:#}");
        assert_eq!(fs::read_to_string(data_dir.join("marker")).unwrap(), "old");
        assert!(staging.join("marker").exists());
    }

    #[test]
    fn a_swap_that_cannot_move_back_names_both_copies_and_keeps_them() {
        let home = tempfile::tempdir().unwrap();
        let (data_dir, staging) = dirs_with_marker(home.path());
        let Err(Swap::Stranded(err)) =
            swap_in(&data_dir, &staging, ".second-brain", NOW, failing(2, 3))
        else {
            panic!("the swap must report both copies");
        };
        let shown = format!("{err:#}");
        assert!(shown.contains(&staging.display().to_string()), "{shown}");
        assert!(shown.contains("before-restore"), "{shown}");
        assert!(staging.join("marker").exists());
    }
}