ai-session-search 1.0.0-rc.4

Local-first search, inspection, export, recovery, and MCP recall across supported AI coding-agent sessions.
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
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
// SPDX-FileCopyrightText: 2026 Andrew Hundt
// SPDX-License-Identifier: Apache-2.0

//! Transactional migration of a live SQLite session index.
//!
//! The source is never modified or removed. A migration uses SQLite's online
//! backup API so committed WAL content is included, validates a same-filesystem
//! staging database, syncs it, and atomically publishes it only after every gate.

use std::collections::BTreeMap;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::Duration;

use anyhow::{bail, Context, Result};
use rusqlite::backup::Backup;
use rusqlite::{Connection, OpenFlags};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::config::Config;
use crate::durable_fs::{entry_exists, sync_file, sync_parent, StagedFile};
use crate::hashing::lower_hex;
use crate::indexer::{index_update_lock_path, open_index_update_lock};

#[derive(Debug, Clone)]
pub struct DatabaseMigrationOptions {
    pub source: PathBuf,
    pub destination: PathBuf,
    pub receipt: PathBuf,
    pub pages_per_step: i32,
    pub pause_between_steps: Duration,
}

impl DatabaseMigrationOptions {
    pub fn new(source: PathBuf, destination: PathBuf, receipt: PathBuf) -> Self {
        Self {
            source,
            destination,
            receipt,
            pages_per_step: 256,
            pause_between_steps: Duration::from_millis(10),
        }
    }

    fn validate(&self) -> Result<()> {
        if !self.source.is_file() {
            bail!("source database does not exist: {}", self.source.display());
        }
        if entry_exists(&self.destination)? {
            bail!(
                "destination already exists; refusing to overwrite: {}",
                self.destination.display()
            );
        }
        if entry_exists(&self.receipt)? {
            bail!(
                "migration receipt already exists; refusing ambiguous cutover: {}",
                self.receipt.display()
            );
        }
        let prepared = staging_path(&self.receipt, "prepared");
        if entry_exists(&prepared)? {
            bail!(
                "prepared migration receipt requires recovery: {}",
                prepared.display()
            );
        }
        if self.pages_per_step <= 0 {
            bail!("pages_per_step must be greater than zero");
        }
        if normalized_absolute(&self.source)? == normalized_absolute(&self.destination)? {
            bail!("source and destination must be different paths");
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DatabaseMigrationPhase {
    Prepared,
    Published,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DatabaseMigrationReceipt {
    pub phase: DatabaseMigrationPhase,
    pub source: PathBuf,
    pub destination: PathBuf,
    pub snapshot_sha256: String,
    pub table_rows: BTreeMap<String, i64>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConfigImportReport {
    pub source: PathBuf,
    pub destination: PathBuf,
    pub mapped: Vec<String>,
    pub ignored: Vec<String>,
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct ConfigImport {
    pub config: Config,
    pub report: ConfigImportReport,
}

#[derive(Debug, Clone)]
pub struct ConfigPublishOptions {
    pub destination: PathBuf,
    pub replace_existing: bool,
    pub rollback_copy: Option<PathBuf>,
}

pub fn migrate_database(options: &DatabaseMigrationOptions) -> Result<DatabaseMigrationReceipt> {
    options.validate()?;
    create_parent(&options.destination)?;
    create_parent(&options.receipt)?;

    let mut source_lock = open_index_update_lock(&index_update_lock_path(&options.source))?;
    let _source_guard = source_lock
        .try_write()
        .with_context(|| format!("source database is in use: {}", options.source.display()))?;
    let mut destination_lock =
        open_index_update_lock(&index_update_lock_path(&options.destination))?;
    let _destination_guard = destination_lock.try_write().with_context(|| {
        format!(
            "destination database is in use: {}",
            options.destination.display()
        )
    })?;

    let staging = StagedFile::new(staging_path(&options.destination, "database"));
    if entry_exists(&staging.path)? {
        bail!(
            "stale database staging file requires inspection: {}",
            staging.path.display()
        );
    }
    for sidecar in sqlite_sidecar_paths(&staging.path) {
        if entry_exists(&sidecar)? {
            bail!(
                "stale database staging sidecar requires inspection: {}",
                sidecar.display()
            );
        }
    }
    let _sidecar_cleanup = SqliteSidecarCleanup::new(&staging.path);

    let source = Connection::open_with_flags(&options.source, OpenFlags::SQLITE_OPEN_READ_ONLY)
        .with_context(|| {
            format!(
                "failed to open source database {}",
                options.source.display()
            )
        })?;
    let source_rows = core_table_rows(&source)?;
    {
        let mut destination = Connection::open(&staging.path).with_context(|| {
            format!(
                "failed to create migration staging database {}",
                staging.path.display()
            )
        })?;
        let backup = Backup::new(&source, &mut destination)?;
        backup.run_to_completion(options.pages_per_step, options.pause_between_steps, None)?;
        drop(backup);
        // Guarantee the migrated index is self-consistent BEFORE it becomes a publishable artifact.
        // The verbatim page copy reproduces the source's user_version AND its (possibly broken)
        // derived message-search layout exactly, so a source that was itself a stamped-current
        // hybrid (FTS5 messages_trigram objects missing) or was missing base tables would otherwise
        // pass the integrity + row-count checks below and be republished as a "healthy" migration
        // that then dead-locks at open. Healing here (the subsequent journal_mode=delete conversion
        // folds the rebuild's WAL back into the self-contained staging file) means the published
        // index opens Current and never needs runtime self-heal.
        ensure_migrated_index_is_current(&destination, &staging.path)?;
        destination
            .pragma_update(None, "journal_mode", "delete")
            .with_context(|| {
                format!(
                    "failed to make migration staging database self-contained: {}",
                    staging.path.display()
                )
            })?;
        let journal_mode: String = destination
            .query_row("pragma journal_mode", [], |row| row.get(0))
            .with_context(|| {
                format!(
                    "failed to verify migration staging database journal mode: {}",
                    staging.path.display()
                )
            })?;
        if !journal_mode.eq_ignore_ascii_case("delete") {
            bail!(
                "migration staging database remained dependent on journal sidecars: path={}, journal_mode={journal_mode}",
                staging.path.display()
            );
        }
    }

    let staged = Connection::open_with_flags(&staging.path, OpenFlags::SQLITE_OPEN_READ_ONLY)?;
    validate_integrity(&staged)?;
    let destination_rows = core_table_rows(&staged)?;
    if source_rows != destination_rows {
        bail!(
            "database row counts changed during migration: source={source_rows:?}, destination={destination_rows:?}"
        );
    }
    drop(staged);

    sync_file(&staging.path).with_context(|| {
        format!(
            "failed to flush migration staging database {}",
            staging.path.display()
        )
    })?;
    let snapshot_sha256 = sha256_file(&staging.path)?;
    let prepared_receipt = DatabaseMigrationReceipt {
        phase: DatabaseMigrationPhase::Prepared,
        source: normalized_absolute(&options.source)?,
        destination: normalized_absolute(&options.destination)?,
        snapshot_sha256,
        table_rows: destination_rows,
    };

    let prepared_path = staging_path(&options.receipt, "prepared");
    if entry_exists(&prepared_path)? {
        bail!(
            "prepared migration receipt requires recovery: {}",
            prepared_path.display()
        );
    }
    write_receipt(&prepared_path, &prepared_receipt)?;
    sync_parent(&prepared_path)?;
    staging.publish_new(&options.destination)?;

    let receipt = DatabaseMigrationReceipt {
        phase: DatabaseMigrationPhase::Published,
        ..prepared_receipt
    };
    let receipt_staging = StagedFile::new(staging_path(&options.receipt, "receipt"));
    write_receipt(&receipt_staging.path, &receipt)?;
    receipt_staging.publish_new(&options.receipt)?;
    fs::remove_file(&prepared_path)?;
    sync_parent(&prepared_path)?;
    Ok(receipt)
}

pub fn verify_migration(receipt: &DatabaseMigrationReceipt) -> Result<()> {
    if receipt.phase != DatabaseMigrationPhase::Published {
        bail!("migration is prepared but not published");
    }
    verify_migration_snapshot(receipt, &receipt.destination)
}

/// Resume or finalize a database migration interrupted after its prepared receipt was synced.
///
/// This operation is idempotent once the published receipt exists. It never discards ambiguous
/// evidence: the prepared receipt, staging database, destination, and any staged final receipt
/// must agree before a state transition is performed.
pub fn recover_database_migration(receipt_path: &Path) -> Result<DatabaseMigrationReceipt> {
    let prepared_path = staging_path(receipt_path, "prepared");
    if entry_exists(receipt_path)? {
        let receipt = load_receipt(receipt_path)?;
        let mut source_lock = open_index_update_lock(&index_update_lock_path(&receipt.source))?;
        let _source_guard = source_lock
            .try_write()
            .with_context(|| format!("source database is in use: {}", receipt.source.display()))?;
        let mut destination_lock =
            open_index_update_lock(&index_update_lock_path(&receipt.destination))?;
        let _destination_guard = destination_lock.try_write().with_context(|| {
            format!(
                "destination database is in use: {}",
                receipt.destination.display()
            )
        })?;
        verify_migration(&receipt)?;
        if entry_exists(&prepared_path)? {
            let prepared = load_receipt(&prepared_path)?;
            ensure_prepared_matches_published(&prepared, &receipt)?;
            fs::remove_file(&prepared_path)?;
            sync_parent(&prepared_path)?;
        }
        return Ok(receipt);
    }

    if !entry_exists(&prepared_path)? {
        bail!(
            "no published or prepared migration receipt exists for recovery: {}",
            receipt_path.display()
        );
    }
    let prepared = load_receipt(&prepared_path)?;
    if prepared.phase != DatabaseMigrationPhase::Prepared {
        bail!("prepared migration evidence has an invalid phase");
    }

    let mut source_lock = open_index_update_lock(&index_update_lock_path(&prepared.source))?;
    let _source_guard = source_lock
        .try_write()
        .with_context(|| format!("source database is in use: {}", prepared.source.display()))?;
    let mut destination_lock =
        open_index_update_lock(&index_update_lock_path(&prepared.destination))?;
    let _destination_guard = destination_lock.try_write().with_context(|| {
        format!(
            "destination database is in use: {}",
            prepared.destination.display()
        )
    })?;

    let published = DatabaseMigrationReceipt {
        phase: DatabaseMigrationPhase::Published,
        source: prepared.source.clone(),
        destination: prepared.destination.clone(),
        snapshot_sha256: prepared.snapshot_sha256.clone(),
        table_rows: prepared.table_rows.clone(),
    };
    if entry_exists(&published.destination)? {
        let database_staging = staging_path(&published.destination, "database");
        if entry_exists(&database_staging)? {
            bail!(
                "both migration destination and staging database exist; refusing ambiguous recovery: {}, {}",
                published.destination.display(),
                database_staging.display()
            );
        }
        verify_migration_snapshot(&published, &published.destination)?;
    } else {
        let database_staging = staging_path(&published.destination, "database");
        if !entry_exists(&database_staging)? {
            bail!(
                "prepared migration has neither a destination nor staging database: {}",
                database_staging.display()
            );
        }
        verify_migration_snapshot(&published, &database_staging)?;
        StagedFile::new(database_staging).publish_new(&published.destination)?;
        verify_migration_snapshot(&published, &published.destination)?;
    }

    let receipt_staging_path = staging_path(receipt_path, "receipt");
    if entry_exists(&receipt_staging_path)? {
        let staged_receipt = load_receipt(&receipt_staging_path)?;
        if staged_receipt != published {
            bail!(
                "staged final receipt conflicts with prepared migration evidence: {}",
                receipt_staging_path.display()
            );
        }
    } else {
        write_receipt(&receipt_staging_path, &published)?;
    }
    StagedFile::new(receipt_staging_path).publish_new(receipt_path)?;
    fs::remove_file(&prepared_path)?;
    sync_parent(&prepared_path)?;
    Ok(published)
}

fn ensure_prepared_matches_published(
    prepared: &DatabaseMigrationReceipt,
    published: &DatabaseMigrationReceipt,
) -> Result<()> {
    let expected = DatabaseMigrationReceipt {
        phase: DatabaseMigrationPhase::Published,
        source: prepared.source.clone(),
        destination: prepared.destination.clone(),
        snapshot_sha256: prepared.snapshot_sha256.clone(),
        table_rows: prepared.table_rows.clone(),
    };
    if prepared.phase != DatabaseMigrationPhase::Prepared || expected != *published {
        bail!("prepared migration evidence conflicts with published receipt");
    }
    Ok(())
}

fn verify_migration_snapshot(receipt: &DatabaseMigrationReceipt, snapshot: &Path) -> Result<()> {
    if sha256_file(snapshot)? != receipt.snapshot_sha256 {
        bail!("destination database changed since migration");
    }
    let source = Connection::open_with_flags(&receipt.source, OpenFlags::SQLITE_OPEN_READ_ONLY)?;
    validate_integrity(&source)?;
    if core_table_rows(&source)? != receipt.table_rows {
        bail!("source database changed since migration");
    }
    let destination = Connection::open_with_flags(snapshot, OpenFlags::SQLITE_OPEN_READ_ONLY)?;
    validate_integrity(&destination)?;
    if core_table_rows(&destination)? != receipt.table_rows {
        bail!("destination row counts no longer match migration receipt");
    }
    Ok(())
}

pub fn load_receipt(path: &Path) -> Result<DatabaseMigrationReceipt> {
    let raw = fs::read_to_string(path)
        .with_context(|| format!("failed to read migration receipt {}", path.display()))?;
    serde_json::from_str(&raw)
        .with_context(|| format!("failed to parse migration receipt {}", path.display()))
}

pub fn import_legacy_config(
    source: &Path,
    destination: PathBuf,
    database_path: PathBuf,
    cache_dir: PathBuf,
) -> Result<ConfigImport> {
    let raw = fs::read_to_string(source)
        .with_context(|| format!("failed to read legacy config {}", source.display()))?;
    let value: serde_json::Value = serde_json::from_str(&raw)
        .with_context(|| format!("failed to parse legacy config {}", source.display()))?;
    let object = value
        .as_object()
        .context("legacy config root must be a JSON object")?;
    let mut config = Config::default();
    config.index.db_path = Some(database_path.to_string_lossy().into_owned());
    config.index.cache_dir = Some(cache_dir.to_string_lossy().into_owned());
    let mut report = ConfigImportReport {
        source: normalized_absolute(source)?,
        destination: normalized_absolute(&destination)?,
        mapped: vec!["index.db_path".to_string(), "index.cache_dir".to_string()],
        ignored: Vec::new(),
        warnings: Vec::new(),
    };

    if let Some(claude_dir) = object.get("claude_dir") {
        if let Some(path) = claude_dir.as_str().filter(|path| !path.trim().is_empty()) {
            config.providers.claude.paths = vec![Path::new(path)
                .join("projects")
                .to_string_lossy()
                .into_owned()];
            report
                .mapped
                .push("claude_dir -> providers.claude.paths".to_string());
        } else {
            report
                .warnings
                .push("claude_dir was not a non-empty string".to_string());
        }
    }

    if let Some(source_dirs) = object.get("source_dirs") {
        if let Some(source_dirs) = source_dirs.as_object() {
            for (provider, value) in source_dirs {
                let Some(paths) = json_paths(value) else {
                    report.warnings.push(format!(
                        "source_dirs.{provider} must be a string or string array"
                    ));
                    continue;
                };
                if set_provider_paths(&mut config, provider, paths) {
                    report.mapped.push(format!(
                        "source_dirs.{provider} -> providers.{}.paths",
                        canonical_provider_name(provider)
                    ));
                } else {
                    report.ignored.push(format!("source_dirs.{provider}"));
                }
            }
        } else {
            report
                .warnings
                .push("source_dirs was not a JSON object".to_string());
        }
    }

    for key in object.keys() {
        if !matches!(key.as_str(), "claude_dir" | "source_dirs") {
            report.ignored.push(key.clone());
        }
    }
    report.mapped.sort();
    report.mapped.dedup();
    report.ignored.sort();
    report.ignored.dedup();
    Ok(ConfigImport { config, report })
}

pub fn publish_imported_config(
    import: &ConfigImport,
    options: &ConfigPublishOptions,
) -> Result<()> {
    if options.destination != import.report.destination {
        let absolute = normalized_absolute(&options.destination)?;
        if absolute != import.report.destination {
            bail!("publish destination differs from reviewed import destination");
        }
    }
    create_parent(&options.destination)?;
    let replacing = entry_exists(&options.destination)?;
    if replacing {
        if fs::symlink_metadata(&options.destination)?
            .file_type()
            .is_symlink()
        {
            bail!(
                "refusing to replace symbolic-link config destination: {}",
                options.destination.display()
            );
        }
        if !options.replace_existing {
            bail!(
                "config destination already exists; use explicit replacement with a rollback copy: {}",
                options.destination.display()
            );
        }
        let rollback = options
            .rollback_copy
            .as_deref()
            .context("replacement requires rollback_copy")?;
        if entry_exists(rollback)? {
            bail!("rollback config already exists: {}", rollback.display());
        }
        create_parent(rollback)?;
        fs::copy(&options.destination, rollback).with_context(|| {
            format!(
                "failed to preserve config {} as {}",
                options.destination.display(),
                rollback.display()
            )
        })?;
        sync_file(rollback)
            .with_context(|| format!("failed to flush rollback config {}", rollback.display()))?;
        sync_parent(rollback)?;
    } else if options.rollback_copy.is_some() {
        bail!("rollback_copy is only valid when replacing an existing config");
    }

    let staging = StagedFile::new(staging_path(&options.destination, "config"));
    if entry_exists(&staging.path)? {
        bail!(
            "stale config staging file requires inspection: {}",
            staging.path.display()
        );
    }
    let toml = toml::to_string_pretty(&import.config)?;
    let _: Config = toml::from_str(&toml).context("generated config failed round-trip parsing")?;
    let mut file = OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&staging.path)?;
    file.write_all(toml.as_bytes())?;
    file.sync_all()?;
    drop(file);
    if replacing {
        staging.publish_replace(&options.destination)
    } else {
        staging.publish_new(&options.destination)
    }
}

fn json_paths(value: &serde_json::Value) -> Option<Vec<String>> {
    match value {
        serde_json::Value::String(path) if !path.trim().is_empty() => Some(vec![path.clone()]),
        serde_json::Value::Array(values) => values
            .iter()
            .map(|value| value.as_str().map(str::to_string))
            .collect(),
        _ => None,
    }
}

fn canonical_provider_name(name: &str) -> &str {
    match name {
        "aistudio" | "ai_studio" | "ai-studio" => "ai-studio",
        "gemini" | "gemini_cli" | "gemini-cli" => "gemini-cli",
        "claude_desktop" | "claude-desktop" => "claude-desktop",
        "prime_agent" | "prime-agent" => "prime-agent",
        other => other,
    }
}

fn set_provider_paths(config: &mut Config, name: &str, paths: Vec<String>) -> bool {
    let provider = match canonical_provider_name(name) {
        "claude" => &mut config.providers.claude,
        "claude-desktop" => &mut config.providers.claude_desktop,
        "codex" => &mut config.providers.codex,
        "cursor" => &mut config.providers.cursor,
        "antigravity" => &mut config.providers.antigravity,
        "pi" => &mut config.providers.pi,
        "prime-agent" => &mut config.providers.prime_agent,
        "ai-studio" => &mut config.providers.aistudio,
        "gemini-cli" => &mut config.providers.gemini_cli,
        _ => return false,
    };
    provider.paths = paths;
    true
}

fn create_parent(path: &Path) -> Result<()> {
    if let Some(parent) = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
    {
        fs::create_dir_all(parent)
            .with_context(|| format!("failed to create directory {}", parent.display()))?;
    }
    Ok(())
}

fn normalized_absolute(path: &Path) -> Result<PathBuf> {
    if path.is_absolute() {
        Ok(path.to_path_buf())
    } else {
        Ok(std::env::current_dir()?.join(path))
    }
}

fn staging_path(path: &Path, kind: &str) -> PathBuf {
    let mut name = path
        .file_name()
        .unwrap_or_else(|| std::ffi::OsStr::new("migration"))
        .to_os_string();
    name.push(format!(".{kind}.staging"));
    path.with_file_name(name)
}

fn sqlite_sidecar_path(database: &Path, suffix: &str) -> PathBuf {
    let mut path = database.as_os_str().to_os_string();
    path.push(suffix);
    PathBuf::from(path)
}

fn sqlite_sidecar_paths(database: &Path) -> [PathBuf; 2] {
    [
        sqlite_sidecar_path(database, "-wal"),
        sqlite_sidecar_path(database, "-shm"),
    ]
}

struct SqliteSidecarCleanup {
    database: PathBuf,
}

impl SqliteSidecarCleanup {
    fn new(database: &Path) -> Self {
        Self {
            database: database.to_path_buf(),
        }
    }
}

impl Drop for SqliteSidecarCleanup {
    fn drop(&mut self) {
        for sidecar in sqlite_sidecar_paths(&self.database) {
            let _ = fs::remove_file(sidecar);
        }
    }
}

fn validate_integrity(connection: &Connection) -> Result<()> {
    let result: String = connection.query_row("pragma integrity_check", [], |row| row.get(0))?;
    if result != "ok" {
        bail!("SQLite integrity_check failed: {result}");
    }
    Ok(())
}

/// Guarantee a freshly page-copied migration staging database is self-consistent before publish.
///
/// `migrate_database` uses a verbatim page-level backup that reproduces the source's `user_version`
/// and its derived message-search layout exactly. A source that was itself a stamped-current hybrid
/// (`user_version == SCHEMA_VERSION` but the FTS5 `messages_trigram` objects missing / obsolete
/// `trigram_postings` present) or that was missing base tables would otherwise pass the downstream
/// `integrity_check` + core-row-count equality and be republished as a "healthy" migration — the
/// exact class of index that later dead-locks at open. This gate closes that door:
///
/// * missing base tables (the rows a rebuild would read are gone) → refuse to publish; and
/// * base data intact but a current-stamped derived layout broken → rebuild it in place from the
///   `messages` rows so the published index opens as `Current` and never needs runtime self-heal.
///
/// The offline rebuild leaves the connection in WAL mode; the caller's immediately-following
/// `journal_mode = delete` conversion folds it back into the self-contained staging file.
fn ensure_migrated_index_is_current(conn: &Connection, path: &Path) -> Result<()> {
    let version: i64 = conn.query_row("pragma user_version", [], |row| row.get(0))?;
    // Schema 4 is the first generation that claims the released FTS-derived layout. Schema 5
    // retains that layout and adds parser-derived provenance. Heal either claimed FTS generation,
    // but do not fabricate schema-5 provenance while copying a v4 archive.
    if !matches!(version, 4 | crate::db::SCHEMA_VERSION) {
        return Ok(());
    }
    // A current stamp with a consistent layout is already publishable.
    let layout_problem = if version == 4 {
        crate::indexer::message_search_layout_problem(conn)?
    } else {
        crate::indexer::current_schema_layout_problem(conn)?
    };
    if layout_problem.is_none() {
        return Ok(());
    }
    // Current-stamped but the derived layout is hybrid/incomplete. Heal it in place if the base rows
    // a rebuild would read are present; otherwise refuse to publish an index that declares itself
    // current but is structurally unrepairable by copy.
    if !crate::indexer::base_data_intact(conn)? {
        bail!(
            "refusing to publish migration: staging database {} declares schema v{} but is missing \
             required base tables; the source index is structurally incomplete",
            path.display(),
            crate::db::SCHEMA_VERSION
        );
    }
    crate::fts::migrate_message_search_schema_offline(conn, version)?;
    Ok(())
}

fn core_table_rows(connection: &Connection) -> Result<BTreeMap<String, i64>> {
    let mut counts = BTreeMap::new();
    for table in ["sessions", "messages", "file_edits"] {
        let exists: bool = connection.query_row(
            "select exists(select 1 from sqlite_master where type = 'table' and name = ?1)",
            [table],
            |row| row.get(0),
        )?;
        if exists {
            let count =
                connection.query_row(&format!("select count(*) from {table}"), [], |row| {
                    row.get(0)
                })?;
            counts.insert(table.to_string(), count);
        }
    }
    Ok(counts)
}

fn sha256_file(path: &Path) -> Result<String> {
    let mut file = File::open(path)
        .with_context(|| format!("failed to open {} for checksum", path.display()))?;
    let mut digest = Sha256::new();
    let mut buffer = [0_u8; 64 * 1024];
    loop {
        let read = file.read(&mut buffer)?;
        if read == 0 {
            break;
        }
        digest.update(&buffer[..read]);
    }
    Ok(lower_hex(digest.finalize().as_ref()))
}

fn write_receipt(path: &Path, receipt: &DatabaseMigrationReceipt) -> Result<()> {
    let mut file = OpenOptions::new().write(true).create_new(true).open(path)?;
    serde_json::to_writer_pretty(&mut file, receipt)?;
    file.write_all(b"\n")?;
    file.sync_all()?;
    Ok(())
}

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

    fn options(dir: &Path) -> DatabaseMigrationOptions {
        DatabaseMigrationOptions::new(
            dir.join("legacy/index.db"),
            dir.join("new/index.db"),
            dir.join("new/migration.json"),
        )
    }

    fn source_with_wal(path: &Path) -> Connection {
        create_parent(path).unwrap();
        let connection = Connection::open(path).unwrap();
        connection
            .pragma_update(None, "journal_mode", "wal")
            .unwrap();
        connection
            .execute_batch(
                "create table sessions(id text primary key);\n\
                 create table messages(id integer primary key, content text);\n\
                 create table file_edits(id integer primary key);\n\
                 insert into sessions values ('s1');\n\
                 insert into messages(content) values ('committed WAL content');",
            )
            .unwrap();
        connection
    }

    fn prepare_interrupted_migration(
        options: &DatabaseMigrationOptions,
        destination_published: bool,
    ) -> (DatabaseMigrationReceipt, PathBuf) {
        create_parent(&options.destination).unwrap();
        let snapshot = if destination_published {
            options.destination.clone()
        } else {
            staging_path(&options.destination, "database")
        };
        {
            let source =
                Connection::open_with_flags(&options.source, OpenFlags::SQLITE_OPEN_READ_ONLY)
                    .unwrap();
            let mut destination = Connection::open(&snapshot).unwrap();
            Backup::new(&source, &mut destination)
                .unwrap()
                .run_to_completion(options.pages_per_step, options.pause_between_steps, None)
                .unwrap();
        }
        let prepared = DatabaseMigrationReceipt {
            phase: DatabaseMigrationPhase::Prepared,
            source: normalized_absolute(&options.source).unwrap(),
            destination: normalized_absolute(&options.destination).unwrap(),
            snapshot_sha256: sha256_file(&snapshot).unwrap(),
            table_rows: core_table_rows(&Connection::open(&snapshot).unwrap()).unwrap(),
        };
        let prepared_path = staging_path(&options.receipt, "prepared");
        create_parent(&prepared_path).unwrap();
        write_receipt(&prepared_path, &prepared).unwrap();
        (prepared, snapshot)
    }

    #[test]
    fn online_backup_includes_wal_and_publishes_verified_receipt() {
        let dir = tempfile::tempdir().unwrap();
        let options = options(dir.path());
        let source = source_with_wal(&options.source);
        assert!(options.source.with_extension("db-wal").exists());

        let receipt = migrate_database(&options).unwrap();

        assert!(options.source.exists(), "source remains rollback-ready");
        assert_eq!(receipt.phase, DatabaseMigrationPhase::Published);
        assert!(!staging_path(&options.receipt, "prepared").exists());
        assert_eq!(receipt.table_rows["sessions"], 1);
        assert_eq!(receipt.table_rows["messages"], 1);
        verify_migration(&receipt).unwrap();
        let migrated =
            Connection::open_with_flags(&options.destination, OpenFlags::SQLITE_OPEN_READ_ONLY)
                .unwrap();
        let content: String = migrated
            .query_row("select content from messages", [], |row| row.get(0))
            .unwrap();
        assert_eq!(content, "committed WAL content");
        let database_staging = staging_path(&options.destination, "database");
        for sidecar in sqlite_sidecar_paths(&database_staging) {
            assert!(
                !sidecar.exists(),
                "staging sidecar leaked: {}",
                sidecar.display()
            );
        }
        drop(source);
    }

    #[test]
    fn migrating_a_current_stamped_hybrid_source_publishes_a_healed_index() {
        // A verbatim page copy reproduces a stamped-current hybrid source exactly; without the
        // pre-publish consistency gate the migration would republish it as "healthy" and the index
        // would dead-lock at open. Assert the published index is instead self-consistent (opens
        // Current, FTS5 trigram rebuilt, obsolete custom index dropped, message preserved).
        let dir = tempfile::tempdir().unwrap();
        let source = dir.path().join("legacy/index.db");
        let destination = dir.path().join("new/index.db");
        let receipt = dir.path().join("new/migration.json");
        create_parent(&source).unwrap();

        // Full v4 schema via Db::open, then corrupt into the stamped-current hybrid via a plain
        // connection (the migration test module cannot reach Db's private connection).
        drop(crate::db::Db::open(&source).unwrap());
        let seed = Connection::open(&source).unwrap();
        seed.execute_batch(
            "insert into sessions (id, provider, provider_session_id, preview_text, source_path,
                 parse_version, discovery_source)
                 values ('claude:mig', 'claude', 'mig', '', '/mig.jsonl', 'test', 'fixture');
             insert into messages (session_id, provider, seq, role, kind, tool_name, content)
                 values ('claude:mig', 'claude', 0, 'user', 'message', null, 'migrationhealneedle');
             drop trigger if exists messages_ai;
             drop trigger if exists messages_ad;
             drop trigger if exists messages_au;
             drop table if exists messages_trigram_terms;
             drop table if exists messages_trigram_vocab;
             drop table if exists messages_trigram;
             create table trigram_postings(tg text primary key, ids blob not null, df integer not null);
             create table trigram_meta(key text primary key, value integer not null);
             pragma user_version=4;",
        )
        .unwrap();
        drop(seed);

        let options = DatabaseMigrationOptions::new(source, destination.clone(), receipt);
        let published = migrate_database(&options).unwrap();
        assert_eq!(published.phase, DatabaseMigrationPhase::Published);

        let dest =
            Connection::open_with_flags(&destination, OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap();
        assert!(
            crate::indexer::message_search_layout_problem(&dest)
                .unwrap()
                .is_none(),
            "migrated v4 index must own its claimed FTS layout with no runtime self-heal needed"
        );
        let state: (i64, bool, bool, i64) = dest
            .query_row(
                "select (select user_version from pragma_user_version),
                        exists(select 1 from sqlite_schema where name='messages_trigram'),
                        not exists(select 1 from sqlite_schema where name='trigram_postings'),
                        (select count(*) from messages)",
                [],
                |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
            )
            .unwrap();
        assert_eq!(state, (4, true, true, 1));
    }

    #[test]
    fn migrating_a_current_stamped_source_missing_base_tables_refuses_to_publish() {
        // A source that declares v4 but lacks base tables is structurally unrepairable by copy; it
        // must be refused rather than republished as a "healthy" migration.
        let dir = tempfile::tempdir().unwrap();
        let source = dir.path().join("legacy/index.db");
        let destination = dir.path().join("new/index.db");
        let receipt = dir.path().join("new/migration.json");
        create_parent(&source).unwrap();

        let seed = Connection::open(&source).unwrap();
        seed.execute_batch(
            "create table sessions(id text primary key);
             create table messages(id integer primary key, content text);
             create table file_edits(id integer primary key);
             pragma user_version=4;",
        )
        .unwrap();
        drop(seed);

        let options = DatabaseMigrationOptions::new(source, destination.clone(), receipt.clone());
        let error = migrate_database(&options).unwrap_err().to_string();
        assert!(error.contains("missing required base tables"), "{error}");
        assert!(
            !destination.exists(),
            "an unrepairable index must never be published"
        );
        assert!(!receipt.exists());
    }

    #[test]
    fn compacting_unpublished_backup_reclaims_pages_and_preserves_wal_and_fts() {
        const ALLOCATION_PROBE_BYTES: i64 = 4 * 1024 * 1024;
        let dir = tempfile::tempdir().unwrap();
        let options = options(dir.path());
        let source = source_with_wal(&options.source);
        source
            .execute_batch(
                "create virtual table messages_fts using fts5(content);
                 insert into messages_fts(content) values ('committed WAL content');
                 create table allocation_probe(payload blob not null);",
            )
            .unwrap();
        source
            .execute(
                "insert into allocation_probe(payload) values(zeroblob(?1))",
                [ALLOCATION_PROBE_BYTES],
            )
            .unwrap();
        source.execute("delete from allocation_probe", []).unwrap();

        let snapshot = staging_path(&options.destination, "candidate");
        create_parent(&snapshot).unwrap();
        let source_read =
            Connection::open_with_flags(&options.source, OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap();
        let mut candidate = Connection::open(&snapshot).unwrap();
        Backup::new(&source_read, &mut candidate)
            .unwrap()
            .run_to_completion(options.pages_per_step, options.pause_between_steps, None)
            .unwrap();

        let before_pages: i64 = candidate
            .query_row("pragma page_count", [], |row| row.get(0))
            .unwrap();
        let before_freelist: i64 = candidate
            .query_row("pragma freelist_count", [], |row| row.get(0))
            .unwrap();
        let page_size: i64 = candidate
            .query_row("pragma page_size", [], |row| row.get(0))
            .unwrap();
        assert!(before_freelist > 0);
        assert_eq!(
            candidate
                .query_row(
                    "select count(*) from messages_fts where messages_fts match 'committed'",
                    [],
                    |row| row.get::<_, i64>(0),
                )
                .unwrap(),
            1
        );

        candidate.execute_batch("vacuum").unwrap();

        let after_pages: i64 = candidate
            .query_row("pragma page_count", [], |row| row.get(0))
            .unwrap();
        let after_freelist: i64 = candidate
            .query_row("pragma freelist_count", [], |row| row.get(0))
            .unwrap();
        assert_eq!(after_freelist, 0);
        assert!(after_pages < before_pages);
        assert_eq!(
            candidate
                .query_row("select content from messages", [], |row| row
                    .get::<_, String>(0))
                .unwrap(),
            "committed WAL content"
        );
        assert_eq!(
            candidate
                .query_row(
                    "select count(*) from messages_fts where messages_fts match 'committed'",
                    [],
                    |row| row.get::<_, i64>(0),
                )
                .unwrap(),
            1
        );
        assert!((before_pages - after_pages) * page_size >= before_freelist * page_size);
        drop(candidate);
        drop(source_read);
        drop(source);
    }

    #[test]
    fn existing_destination_is_never_overwritten() {
        let dir = tempfile::tempdir().unwrap();
        let options = options(dir.path());
        let _source = source_with_wal(&options.source);
        create_parent(&options.destination).unwrap();
        fs::write(&options.destination, b"keep me").unwrap();

        let error = migrate_database(&options).unwrap_err();

        assert!(error.to_string().contains("refusing to overwrite"));
        assert_eq!(fs::read(&options.destination).unwrap(), b"keep me");
        assert!(!options.receipt.exists());
    }

    #[test]
    fn destination_claim_race_never_replaces_the_winner() {
        let dir = tempfile::tempdir().unwrap();
        let staging_path = dir.path().join("index.db.database.staging");
        let destination = dir.path().join("index.db");
        fs::write(&staging_path, b"staged database").unwrap();
        let staging = StagedFile::new(staging_path.clone());

        fs::write(&destination, b"concurrent winner").unwrap();
        let error = staging.publish_new(&destination).unwrap_err();

        assert!(error.to_string().contains("atomically claim"));
        assert_eq!(fs::read(destination).unwrap(), b"concurrent winner");
        assert!(!staging_path.exists());
    }

    #[cfg(unix)]
    #[test]
    fn broken_destination_symlink_is_an_existing_entry() {
        use std::os::unix::fs::symlink;

        let dir = tempfile::tempdir().unwrap();
        let options = options(dir.path());
        let _source = source_with_wal(&options.source);
        create_parent(&options.destination).unwrap();
        symlink(dir.path().join("missing.db"), &options.destination).unwrap();

        let error = migrate_database(&options).unwrap_err();

        assert!(error.to_string().contains("refusing to overwrite"));
        assert!(options
            .destination
            .symlink_metadata()
            .unwrap()
            .file_type()
            .is_symlink());
        assert!(!options.receipt.exists());
    }

    #[test]
    fn corrupt_source_never_publishes_partial_files() {
        let dir = tempfile::tempdir().unwrap();
        let options = options(dir.path());
        create_parent(&options.source).unwrap();
        fs::write(&options.source, b"not sqlite").unwrap();

        assert!(migrate_database(&options).is_err());
        assert!(!options.destination.exists());
        assert!(!options.receipt.exists());
        assert!(!staging_path(&options.destination, "database").exists());
    }

    #[test]
    fn held_source_lock_fails_without_waiting_or_publishing() {
        let dir = tempfile::tempdir().unwrap();
        let options = options(dir.path());
        let _source = source_with_wal(&options.source);
        let mut lock = open_index_update_lock(&index_update_lock_path(&options.source)).unwrap();
        let _guard = lock.try_write().unwrap();

        let error = migrate_database(&options).unwrap_err();

        assert!(error.to_string().contains("source database is in use"));
        assert!(!options.destination.exists());
    }

    #[cfg(unix)]
    #[test]
    fn symbolic_link_lock_fails_without_publishing_or_touching_target() {
        use std::os::unix::fs::symlink;

        let dir = tempfile::tempdir().unwrap();
        let options = options(dir.path());
        let _source = source_with_wal(&options.source);
        let target = dir.path().join("unrelated-lock-target");
        fs::write(&target, b"preserve me").unwrap();
        symlink(&target, index_update_lock_path(&options.source)).unwrap();

        let error = migrate_database(&options).unwrap_err();

        assert!(error.to_string().contains("is not a regular file"));
        assert_eq!(fs::read(target).unwrap(), b"preserve me");
        assert!(!options.destination.exists());
        assert!(!options.receipt.exists());
    }

    #[test]
    fn prepared_receipt_blocks_ambiguous_retry() {
        let dir = tempfile::tempdir().unwrap();
        let options = options(dir.path());
        let _source = source_with_wal(&options.source);
        let prepared = staging_path(&options.receipt, "prepared");
        create_parent(&prepared).unwrap();
        fs::write(&prepared, b"recovery evidence").unwrap();

        let error = migrate_database(&options).unwrap_err();

        assert!(error.to_string().contains("requires recovery"));
        assert_eq!(fs::read(prepared).unwrap(), b"recovery evidence");
        assert!(!options.destination.exists());
    }

    #[test]
    fn recovery_resumes_verified_staging_and_is_idempotent() {
        let dir = tempfile::tempdir().unwrap();
        let options = options(dir.path());
        let _source = source_with_wal(&options.source);
        let (_prepared, staging) = prepare_interrupted_migration(&options, false);
        let prepared_path = staging_path(&options.receipt, "prepared");

        let recovered = recover_database_migration(&options.receipt).unwrap();

        assert_eq!(recovered.phase, DatabaseMigrationPhase::Published);
        assert!(options.destination.exists());
        assert!(options.receipt.exists());
        assert!(!staging.exists());
        assert!(!prepared_path.exists());

        let mut destination_lock =
            open_index_update_lock(&index_update_lock_path(&options.destination)).unwrap();
        let destination_guard = destination_lock.try_write().unwrap();
        let error = recover_database_migration(&options.receipt).unwrap_err();
        assert!(error.to_string().contains("destination database is in use"));
        drop(destination_guard);
        assert_eq!(
            recover_database_migration(&options.receipt).unwrap(),
            recovered
        );
    }

    #[test]
    fn recovery_finalizes_published_destination_and_rejects_conflicting_evidence() {
        let dir = tempfile::tempdir().unwrap();
        let options = options(dir.path());
        let _source = source_with_wal(&options.source);
        let (prepared, _snapshot) = prepare_interrupted_migration(&options, true);
        let prepared_path = staging_path(&options.receipt, "prepared");

        let recovered = recover_database_migration(&options.receipt).unwrap();
        verify_migration(&recovered).unwrap();

        fs::remove_file(&options.receipt).unwrap();
        let mut conflicting = prepared;
        conflicting.snapshot_sha256 = "conflict".to_string();
        write_receipt(&prepared_path, &conflicting).unwrap();
        let error = recover_database_migration(&options.receipt).unwrap_err();
        assert!(error
            .to_string()
            .contains("destination database changed since migration"));
        assert!(prepared_path.exists(), "conflicting evidence is preserved");
        assert!(
            options.destination.exists(),
            "published database is preserved"
        );
    }

    #[test]
    fn recovery_preserves_corrupt_and_ambiguous_database_evidence() {
        let corrupt_dir = tempfile::tempdir().unwrap();
        let corrupt_options = options(corrupt_dir.path());
        let _corrupt_source = source_with_wal(&corrupt_options.source);
        let (_prepared, corrupt_staging) = prepare_interrupted_migration(&corrupt_options, false);
        fs::write(&corrupt_staging, b"corrupt staged snapshot").unwrap();

        let error = recover_database_migration(&corrupt_options.receipt).unwrap_err();
        assert!(error.to_string().contains("changed since migration"));
        assert!(corrupt_staging.exists());
        assert!(staging_path(&corrupt_options.receipt, "prepared").exists());

        let ambiguous_dir = tempfile::tempdir().unwrap();
        let ambiguous_options = options(ambiguous_dir.path());
        let _ambiguous_source = source_with_wal(&ambiguous_options.source);
        let (_prepared, ambiguous_staging) =
            prepare_interrupted_migration(&ambiguous_options, false);
        fs::copy(&ambiguous_staging, &ambiguous_options.destination).unwrap();

        let error = recover_database_migration(&ambiguous_options.receipt).unwrap_err();
        assert!(error.to_string().contains("both migration destination"));
        assert!(ambiguous_staging.exists());
        assert!(ambiguous_options.destination.exists());
        assert!(staging_path(&ambiguous_options.receipt, "prepared").exists());
    }

    #[test]
    fn recovery_preserves_conflicting_staged_final_receipt() {
        let dir = tempfile::tempdir().unwrap();
        let options = options(dir.path());
        let _source = source_with_wal(&options.source);
        let (prepared, _snapshot) = prepare_interrupted_migration(&options, true);
        let mut conflicting = DatabaseMigrationReceipt {
            phase: DatabaseMigrationPhase::Published,
            ..prepared
        };
        conflicting.table_rows.insert("sessions".to_string(), 99);
        let receipt_staging = staging_path(&options.receipt, "receipt");
        write_receipt(&receipt_staging, &conflicting).unwrap();

        let error = recover_database_migration(&options.receipt).unwrap_err();

        assert!(error.to_string().contains("staged final receipt conflicts"));
        assert!(receipt_staging.exists());
        assert!(staging_path(&options.receipt, "prepared").exists());
        assert!(options.destination.exists());
        assert!(!options.receipt.exists());
    }

    #[test]
    fn legacy_config_import_maps_providers_and_reports_unsupported_fields() {
        let dir = tempfile::tempdir().unwrap();
        let source = dir.path().join("config.json");
        let destination = dir.path().join("new/config.toml");
        fs::write(
            &source,
            r#"{
                "claude_dir": "~/custom-claude",
                "source_dirs": {
                    "aistudio": ["~/studio-a", "~/studio-b"],
                    "gemini_cli": "~/gemini/tmp",
                    "future_provider": "/future"
                },
                "org_dir": "~/organized",
                "defaults": {"format": "json"}
            }"#,
        )
        .unwrap();

        let import = import_legacy_config(
            &source,
            destination,
            dir.path().join("new/index.db"),
            dir.path().join("new/cache"),
        )
        .unwrap();

        assert_eq!(
            import.config.providers.claude.paths,
            vec![Path::new("~/custom-claude")
                .join("projects")
                .to_string_lossy()
                .into_owned()]
        );
        assert_eq!(
            import.config.providers.aistudio.paths,
            vec!["~/studio-a", "~/studio-b"]
        );
        assert_eq!(
            import.config.providers.gemini_cli.paths,
            vec!["~/gemini/tmp"]
        );
        assert!(import
            .report
            .ignored
            .contains(&"source_dirs.future_provider".to_string()));
        assert!(import.report.ignored.contains(&"org_dir".to_string()));
        assert!(import.report.ignored.contains(&"defaults".to_string()));
    }

    #[test]
    fn config_publish_is_atomic_and_replacement_requires_rollback() {
        let dir = tempfile::tempdir().unwrap();
        let source = dir.path().join("legacy.json");
        let destination = dir.path().join("new/config.toml");
        fs::write(&source, "{}").unwrap();
        let import = import_legacy_config(
            &source,
            destination.clone(),
            dir.path().join("new/index.db"),
            dir.path().join("new/cache"),
        )
        .unwrap();
        let initial = ConfigPublishOptions {
            destination: destination.clone(),
            replace_existing: false,
            rollback_copy: None,
        };
        publish_imported_config(&import, &initial).unwrap();
        let original = fs::read(&destination).unwrap();

        let error = publish_imported_config(&import, &initial).unwrap_err();
        assert!(error.to_string().contains("already exists"));
        let rollback = dir.path().join("rollback/config.toml");
        publish_imported_config(
            &import,
            &ConfigPublishOptions {
                destination: destination.clone(),
                replace_existing: true,
                rollback_copy: Some(rollback.clone()),
            },
        )
        .unwrap();

        assert_eq!(fs::read(rollback).unwrap(), original);
        let parsed: Config = toml::from_str(&fs::read_to_string(destination).unwrap()).unwrap();
        assert_eq!(parsed.db_path(), dir.path().join("new/index.db"));
    }

    #[cfg(unix)]
    #[test]
    fn config_publish_never_replaces_a_symbolic_link() {
        use std::os::unix::fs::symlink;

        let dir = tempfile::tempdir().unwrap();
        let source = dir.path().join("legacy.json");
        let destination = dir.path().join("new/config.toml");
        let rollback = dir.path().join("rollback/config.toml");
        fs::write(&source, "{}").unwrap();
        let import = import_legacy_config(
            &source,
            destination.clone(),
            dir.path().join("new/index.db"),
            dir.path().join("new/cache"),
        )
        .unwrap();
        create_parent(&destination).unwrap();
        symlink(dir.path().join("missing.toml"), &destination).unwrap();

        let error = publish_imported_config(
            &import,
            &ConfigPublishOptions {
                destination: destination.clone(),
                replace_existing: true,
                rollback_copy: Some(rollback.clone()),
            },
        )
        .unwrap_err();

        assert!(error.to_string().contains("symbolic-link"));
        assert!(destination
            .symlink_metadata()
            .unwrap()
            .file_type()
            .is_symlink());
        assert!(!rollback.exists());
    }
}