bones-core 0.24.0

Core data structures, CRDT event model, and projection engine for bones
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
//! Recovery procedures for corrupt shards, partial writes, and missing DB.
//!
//! This module implements the runtime recovery procedures that restore a bones
//! project to a consistent state after:
//! - Partial/torn writes (process crash mid-append)
//! - Corrupt shard data (bit flips, truncation, invalid content)
//! - Missing or corrupt `SQLite` projection database
//! - Missing or corrupt binary cache files
//! - Locked database (retry with timeout)
//!
//! # Recovery Philosophy
//!
//! - **Deterministic**: same input → same recovery action, every time.
//! - **No silent data loss**: corrupt data is quarantined, never deleted outright.
//! - **Fast common path**: torn-write repair is the typical case (truncate last
//!   incomplete line). Complex cases (quarantine, rebuild) are rarer.
//! - **User-facing messages**: every action emits a diagnostic so operators know
//!   exactly what happened and why.

use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use crate::event::parser;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

/// Report from recovering a corrupt or partially-written shard file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecoveryReport {
    /// Path to the shard that was recovered.
    pub shard_path: PathBuf,
    /// Number of valid events preserved.
    pub events_preserved: usize,
    /// Number of corrupt/invalid events discarded.
    pub events_discarded: usize,
    /// Byte offset where corruption was detected (if applicable).
    pub corruption_offset: Option<u64>,
    /// What action was taken.
    pub action_taken: RecoveryAction,
}

/// The action taken during recovery.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecoveryAction {
    /// Truncated file at the last valid event boundary.
    Truncated {
        /// Number of bytes removed from the end.
        bytes_removed: u64,
    },
    /// Quarantined corrupt data to a `.corrupt` backup file.
    Quarantined {
        /// Path to the backup file containing the corrupt data.
        backup_path: PathBuf,
    },
    /// No action needed — file was valid.
    NoActionNeeded,
}

/// Errors that can occur during recovery operations.
#[derive(Debug, thiserror::Error)]
pub enum RecoveryError {
    /// I/O error during recovery.
    #[error("recovery I/O error: {0}")]
    Io(#[from] io::Error),

    /// The shard file does not exist.
    #[error("shard file not found: {}", .0.display())]
    ShardNotFound(PathBuf),

    /// The events directory does not exist.
    #[error("events directory not found: {}", .0.display())]
    EventsDirNotFound(PathBuf),

    /// The database path is invalid.
    #[error("invalid database path: {}", .0.display())]
    InvalidDbPath(PathBuf),

    /// Rebuild failed.
    #[error("rebuild failed: {0}")]
    RebuildFailed(String),

    /// Lock timeout exceeded.
    #[error("database locked after {0:?} — another process may hold the lock")]
    LockTimeout(Duration),
}

// ---------------------------------------------------------------------------
// Partial write recovery (torn writes)
// ---------------------------------------------------------------------------

/// Recover from a partial write (e.g., crash mid-append).
///
/// Detects incomplete last line (no trailing newline) and truncates
/// to the last complete event line. This is the fast path — runs on
/// startup before replay.
///
/// # Algorithm
///
/// 1. Read the file contents.
/// 2. If empty or ends with `\n`, nothing to do.
/// 3. Otherwise, find the last `\n` and truncate there.
///
/// # Returns
///
/// The number of bytes removed (0 if file was already clean).
///
/// # Errors
///
/// Returns an error if the file cannot be read or truncated.
pub fn recover_partial_write(path: &Path) -> Result<u64, RecoveryError> {
    if !path.exists() {
        return Err(RecoveryError::ShardNotFound(path.to_path_buf()));
    }

    let content = fs::read(path)?;
    if content.is_empty() || content.last() == Some(&b'\n') {
        return Ok(0);
    }

    // Find last newline
    let last_newline = content.iter().rposition(|&b| b == b'\n');
    let truncate_to = last_newline.map_or(0, |pos| pos + 1);

    let bytes_removed = content.len() - truncate_to;

    // Truncate the file
    let file = fs::OpenOptions::new().write(true).open(path)?;
    file.set_len(truncate_to as u64)?;

    tracing::warn!(
        path = %path.display(),
        bytes_removed,
        "torn write repaired: truncated incomplete trailing line"
    );

    Ok(bytes_removed as u64)
}

// ---------------------------------------------------------------------------
// Corrupt shard recovery
// ---------------------------------------------------------------------------

/// Recover a corrupt shard file by scanning for the last valid event line
/// and quarantining corrupt data to a backup file.
///
/// # Algorithm
///
/// 1. Read the entire shard file.
/// 2. Split into lines; validate each line:
///    - Comment lines (`#`...) and blank lines are always valid.
///    - Data lines must parse successfully via the TSJSON parser.
/// 3. Find the last contiguous block of valid lines from the start.
/// 4. If all lines are valid, return `NoActionNeeded`.
/// 5. Otherwise:
///    a. Write the corrupt tail to `<path>.corrupt` for manual inspection.
///    b. Truncate the original file to the last valid line.
///
/// # Returns
///
/// A [`RecoveryReport`] describing what was found and what action was taken.
///
/// # Panics
///
/// Panics if the internal `first_bad_line` index is unexpectedly `None` after
/// a prior `is_some()` check — this should never happen.
///
/// # Errors
///
/// Returns [`RecoveryError::ShardNotFound`] if the file does not exist, or
/// [`RecoveryError::Io`] if the file cannot be read or written.
pub fn recover_corrupt_shard(path: &Path) -> Result<RecoveryReport, RecoveryError> {
    if !path.exists() {
        return Err(RecoveryError::ShardNotFound(path.to_path_buf()));
    }

    let content = fs::read_to_string(path).map_err(|e| {
        // If we can't even read as UTF-8, the whole file might be binary-corrupt
        tracing::error!(path = %path.display(), error = %e, "shard is not valid UTF-8");
        RecoveryError::Io(e)
    })?;

    if content.is_empty() {
        return Ok(RecoveryReport {
            shard_path: path.to_path_buf(),
            events_preserved: 0,
            events_discarded: 0,
            corruption_offset: None,
            action_taken: RecoveryAction::NoActionNeeded,
        });
    }

    let lines: Vec<&str> = content.lines().collect();
    let mut events_preserved = 0;
    let mut first_bad_line = None;

    for (i, line) in lines.iter().enumerate() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            // Comment or blank — always valid
            continue;
        }

        // Try parsing as a TSJSON event line
        if parser::parse_line(line).is_ok() {
            events_preserved += 1;
        } else {
            first_bad_line = Some(i);
            break;
        }
    }

    // If we broke out on a bad line, count events from the valid prefix
    // Otherwise, all lines valid
    if first_bad_line.is_none() {
        return Ok(RecoveryReport {
            shard_path: path.to_path_buf(),
            events_preserved,
            events_discarded: 0,
            corruption_offset: None,
            action_taken: RecoveryAction::NoActionNeeded,
        });
    }

    // SAFETY: we checked `first_bad_line.is_none()` above and returned early.
    let bad_idx = first_bad_line.expect("checked is_some above");
    let events_discarded = lines[bad_idx..]
        .iter()
        .filter(|l| {
            let t = l.trim();
            !t.is_empty() && !t.starts_with('#')
        })
        .count();

    // Calculate byte offset of corruption
    let corruption_offset: u64 = content
        .lines()
        .take(bad_idx)
        .map(|l| l.len() as u64 + 1) // +1 for the newline
        .sum();

    // Quarantine: write corrupt tail to backup
    let backup_path = path.with_extension("corrupt");
    let corrupt_content: String = lines[bad_idx..].iter().fold(String::new(), |mut acc, l| {
        use std::fmt::Write;
        let _ = writeln!(acc, "{l}");
        acc
    });
    fs::write(&backup_path, &corrupt_content)?;

    // Truncate original to valid prefix
    let valid_content: String = lines[..bad_idx].iter().fold(String::new(), |mut acc, l| {
        use std::fmt::Write;
        let _ = writeln!(acc, "{l}");
        acc
    });
    fs::write(path, &valid_content)?;

    tracing::warn!(
        path = %path.display(),
        events_preserved,
        events_discarded,
        corruption_offset,
        backup = %backup_path.display(),
        "corrupt shard recovered: quarantined bad data to backup file"
    );

    Ok(RecoveryReport {
        shard_path: path.to_path_buf(),
        events_preserved,
        events_discarded,
        corruption_offset: Some(corruption_offset),
        action_taken: RecoveryAction::Quarantined { backup_path },
    })
}

// ---------------------------------------------------------------------------
// Missing DB recovery
// ---------------------------------------------------------------------------

/// Recover from a missing or corrupt `SQLite` projection by triggering a full
/// rebuild from the event log.
///
/// This is the "auto-heal" path when `bones.db` is absent, corrupt, or
/// fails integrity checks. Delegates to [`crate::db::rebuild::rebuild`].
///
/// # Arguments
///
/// * `events_dir` — Path to `.bones/events/` directory.
/// * `db_path` — Path to `.bones/bones.db`.
///
/// # Errors
///
/// Returns an error if the events directory doesn't exist or rebuild fails.
pub fn recover_missing_db(
    events_dir: &Path,
    db_path: &Path,
) -> Result<RecoveryReport, RecoveryError> {
    if !events_dir.exists() {
        return Err(RecoveryError::EventsDirNotFound(events_dir.to_path_buf()));
    }

    // Delete corrupt DB if it exists (rebuild will create fresh)
    let db_existed = db_path.exists();
    if db_existed {
        // Back up corrupt DB before deleting
        let backup_path = db_path.with_extension("db.corrupt");
        if let Err(e) = fs::copy(db_path, &backup_path) {
            tracing::warn!(
                error = %e,
                "could not back up corrupt DB before rebuild"
            );
        }
    }

    let rebuild_result = crate::db::rebuild::rebuild(events_dir, db_path)
        .map_err(|e| RecoveryError::RebuildFailed(e.to_string()))?;

    let action = if db_existed {
        let backup_path = db_path.with_extension("db.corrupt");
        tracing::info!(
            events = rebuild_result.event_count,
            items = rebuild_result.item_count,
            elapsed_ms = rebuild_result.elapsed.as_millis(),
            "rebuilt corrupt projection from event log"
        );
        RecoveryAction::Quarantined { backup_path }
    } else {
        tracing::info!(
            events = rebuild_result.event_count,
            items = rebuild_result.item_count,
            elapsed_ms = rebuild_result.elapsed.as_millis(),
            "rebuilt missing projection from event log"
        );
        RecoveryAction::NoActionNeeded
    };

    Ok(RecoveryReport {
        shard_path: db_path.to_path_buf(),
        events_preserved: rebuild_result.event_count,
        events_discarded: 0,
        corruption_offset: None,
        action_taken: action,
    })
}

// ---------------------------------------------------------------------------
// Corrupt cache recovery
// ---------------------------------------------------------------------------

/// Recover from a corrupt or missing binary cache by deleting it.
///
/// The cache will be rebuilt lazily on next access (it's a pure
/// performance optimization derived from the event log).
///
/// # Returns
///
/// `true` if a cache file was deleted, `false` if it didn't exist.
///
/// # Errors
///
/// Returns [`RecoveryError::Io`] if the cache file cannot be deleted.
pub fn recover_corrupt_cache(cache_path: &Path) -> Result<bool, RecoveryError> {
    if !cache_path.exists() {
        return Ok(false);
    }

    fs::remove_file(cache_path)?;

    tracing::info!(
        path = %cache_path.display(),
        "deleted corrupt binary cache — will be rebuilt on next access"
    );

    Ok(true)
}

// ---------------------------------------------------------------------------
// Locked DB retry
// ---------------------------------------------------------------------------

/// Attempt to open a `SQLite` database with retry and timeout for lock contention.
///
/// If the database is locked by another process, retries with exponential
/// backoff up to `timeout`. Returns the connection on success or a
/// [`RecoveryError::LockTimeout`] on failure.
///
/// # Arguments
///
/// * `db_path` — Path to the `SQLite` database.
/// * `timeout` — Maximum time to wait for the lock.
///
/// # Errors
///
/// Returns `LockTimeout` if the lock is not released within the timeout.
/// Returns `Io` for other I/O errors.
pub fn open_db_with_retry(
    db_path: &Path,
    timeout: Duration,
) -> Result<rusqlite::Connection, RecoveryError> {
    let start = Instant::now();
    let mut delay = Duration::from_millis(50);
    let max_delay = Duration::from_secs(2);

    loop {
        match crate::db::open_projection(db_path) {
            Ok(conn) => {
                // Test that we can actually query (not just open the file)
                match conn.execute_batch("SELECT 1") {
                    Ok(()) => return Ok(conn),
                    Err(e) if is_locked_error(&e) => {
                        // Fall through to retry
                        tracing::debug!(
                            elapsed_ms = start.elapsed().as_millis(),
                            "database locked, retrying..."
                        );
                    }
                    Err(e) => {
                        return Err(RecoveryError::Io(io::Error::other(e.to_string())));
                    }
                }
            }
            Err(e) => {
                let err_str = e.to_string();
                if err_str.contains("locked") || err_str.contains("busy") {
                    tracing::debug!(
                        elapsed_ms = start.elapsed().as_millis(),
                        "database locked on open, retrying..."
                    );
                } else {
                    return Err(RecoveryError::Io(io::Error::other(err_str)));
                }
            }
        }

        if start.elapsed() >= timeout {
            return Err(RecoveryError::LockTimeout(timeout));
        }

        std::thread::sleep(delay);
        delay = (delay * 2).min(max_delay);
    }
}

/// Check if a rusqlite error is a lock/busy error.
fn is_locked_error(e: &rusqlite::Error) -> bool {
    if let rusqlite::Error::SqliteFailure(err, _) = e {
        matches!(
            err.code,
            rusqlite::ffi::ErrorCode::DatabaseBusy | rusqlite::ffi::ErrorCode::DatabaseLocked
        )
    } else {
        let s = e.to_string();
        s.contains("locked") || s.contains("busy")
    }
}

// ---------------------------------------------------------------------------
// Full project health check and auto-recovery
// ---------------------------------------------------------------------------

/// Result of a full project health check.
#[derive(Debug, Clone)]
pub struct HealthCheckResult {
    /// Whether the project directory exists and looks valid.
    pub project_valid: bool,
    /// Torn-write recovery results (one per shard).
    pub torn_write_repairs: Vec<(PathBuf, u64)>,
    /// Whether the DB was rebuilt.
    pub db_rebuilt: bool,
    /// Number of cache files cleaned.
    pub caches_cleaned: usize,
    /// Errors encountered (non-fatal).
    pub warnings: Vec<String>,
}

/// Run a full health check and auto-recovery on a bones project directory.
///
/// This is called on startup to ensure the project is in a consistent state.
///
/// # Steps
///
/// 1. Verify `.bones/` directory exists.
/// 2. Recover torn writes on all shard files.
/// 3. Check if `SQLite` DB exists and is valid; rebuild if not.
/// 4. Clean corrupt cache files.
///
/// # Arguments
///
/// * `bones_dir` — Path to the `.bones/` directory.
///
/// # Errors
///
/// Returns a [`RecoveryError`] if a critical recovery step fails.
/// Non-fatal issues are recorded in `HealthCheckResult::warnings`.
pub fn auto_recover(bones_dir: &Path) -> Result<HealthCheckResult, RecoveryError> {
    let mut result = HealthCheckResult {
        project_valid: false,
        torn_write_repairs: Vec::new(),
        db_rebuilt: false,
        caches_cleaned: 0,
        warnings: Vec::new(),
    };

    // 1. Verify project directory
    if !bones_dir.exists() || !bones_dir.is_dir() {
        return Ok(result); // project_valid = false signals "not a bones project"
    }
    result.project_valid = true;

    let events_dir = bones_dir.join("events");
    let db_path = bones_dir.join("bones.db");
    let cache_dir = bones_dir.join("cache");

    // 2. Recover torn writes on all shard files
    if events_dir.exists() {
        match fs::read_dir(&events_dir) {
            Ok(entries) => {
                for entry in entries.flatten() {
                    let path = entry.path();
                    if path.extension().and_then(|e| e.to_str()) == Some("events") {
                        match recover_partial_write(&path) {
                            Ok(bytes) if bytes > 0 => {
                                result.torn_write_repairs.push((path, bytes));
                            }
                            Ok(_) => {} // clean file
                            Err(e) => {
                                result.warnings.push(format!(
                                    "torn-write check failed for {}: {e}",
                                    path.display()
                                ));
                            }
                        }
                    }
                }
            }
            Err(e) => {
                result.warnings.push(format!("cannot read events dir: {e}"));
            }
        }
    }

    // 3. Check/rebuild SQLite DB
    if events_dir.exists() {
        let need_rebuild = !db_path.exists()
            || crate::db::open_projection(&db_path).map_or(true, |conn| {
                // Try a simple query to verify DB isn't corrupt
                conn.execute_batch("SELECT COUNT(*) FROM items").is_err()
            });

        if need_rebuild {
            match recover_missing_db(&events_dir, &db_path) {
                Ok(_report) => {
                    result.db_rebuilt = true;
                }
                Err(e) => {
                    result.warnings.push(format!("DB rebuild failed: {e}"));
                }
            }
        }
    }

    // 4. Clean corrupt cache files
    if cache_dir.exists() {
        let cache_events_bin = cache_dir.join("events.bin");
        if cache_events_bin.exists() {
            // Validate cache header (first 4 bytes should be magic)
            let is_valid = fs::read(&cache_events_bin)
                .map(|data| data.len() >= 4 && &data[..4] == b"BCEV")
                .unwrap_or(false);

            if !is_valid {
                match recover_corrupt_cache(&cache_events_bin) {
                    Ok(true) => result.caches_cleaned += 1,
                    Ok(false) => {}
                    Err(e) => {
                        result.warnings.push(format!("cache cleanup failed: {e}"));
                    }
                }
            }
        }
    }

    tracing::info!(
        torn_writes = result.torn_write_repairs.len(),
        db_rebuilt = result.db_rebuilt,
        caches_cleaned = result.caches_cleaned,
        warnings = result.warnings.len(),
        "auto-recovery complete"
    );

    Ok(result)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::TempDir;

    // ---- Partial write tests ----

    #[test]
    fn partial_write_clean_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.events");
        fs::write(&path, "line1\nline2\n").unwrap();

        let bytes = recover_partial_write(&path).unwrap();
        assert_eq!(bytes, 0);

        let content = fs::read_to_string(&path).unwrap();
        assert_eq!(content, "line1\nline2\n");
    }

    #[test]
    fn partial_write_truncates_incomplete_line() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.events");
        fs::write(&path, "line1\nline2\npartial").unwrap();

        let bytes = recover_partial_write(&path).unwrap();
        assert_eq!(bytes, 7); // "partial" = 7 bytes

        let content = fs::read_to_string(&path).unwrap();
        assert_eq!(content, "line1\nline2\n");
    }

    #[test]
    fn partial_write_no_complete_lines() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.events");
        fs::write(&path, "no newline at all").unwrap();

        let bytes = recover_partial_write(&path).unwrap();
        assert_eq!(bytes, 17);

        let content = fs::read_to_string(&path).unwrap();
        assert_eq!(content, "");
    }

    #[test]
    fn partial_write_empty_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.events");
        fs::write(&path, "").unwrap();

        let bytes = recover_partial_write(&path).unwrap();
        assert_eq!(bytes, 0);
    }

    #[test]
    fn partial_write_nonexistent_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("nope.events");

        let result = recover_partial_write(&path);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            RecoveryError::ShardNotFound(_)
        ));
    }

    // ---- Corrupt shard tests ----

    #[test]
    fn corrupt_shard_clean_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.events");
        // Only comments and blank lines → valid
        fs::write(&path, "# bones event log v1\n# comment\n\n").unwrap();

        let report = recover_corrupt_shard(&path).unwrap();
        assert_eq!(report.events_preserved, 0);
        assert_eq!(report.events_discarded, 0);
        assert_eq!(report.action_taken, RecoveryAction::NoActionNeeded);
    }

    #[test]
    fn corrupt_shard_empty_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.events");
        fs::write(&path, "").unwrap();

        let report = recover_corrupt_shard(&path).unwrap();
        assert_eq!(report.events_preserved, 0);
        assert_eq!(report.action_taken, RecoveryAction::NoActionNeeded);
    }

    #[test]
    fn corrupt_shard_with_bad_data() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.events");
        // Header + a line that won't parse as TSJSON
        fs::write(&path, "# header\nthis is garbage data\nmore garbage\n").unwrap();

        let report = recover_corrupt_shard(&path).unwrap();
        assert_eq!(report.events_preserved, 0);
        assert_eq!(report.events_discarded, 2);
        assert!(report.corruption_offset.is_some());

        match &report.action_taken {
            RecoveryAction::Quarantined { backup_path } => {
                assert!(backup_path.exists());
                let backup = fs::read_to_string(backup_path).unwrap();
                assert!(backup.contains("garbage data"));
            }
            _ => panic!("expected Quarantined"),
        }

        // Original should only have the header
        let content = fs::read_to_string(&path).unwrap();
        assert_eq!(content, "# header\n");
    }

    #[test]
    fn corrupt_shard_nonexistent_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("nope.events");

        let result = recover_corrupt_shard(&path);
        assert!(result.is_err());
    }

    // ---- Cache recovery tests ----

    #[test]
    fn cache_recovery_deletes_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("events.bin");
        fs::write(&path, "corrupt data").unwrap();

        let deleted = recover_corrupt_cache(&path).unwrap();
        assert!(deleted);
        assert!(!path.exists());
    }

    #[test]
    fn cache_recovery_nonexistent_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("events.bin");

        let deleted = recover_corrupt_cache(&path).unwrap();
        assert!(!deleted);
    }

    // ---- Missing DB recovery tests ----

    #[test]
    fn missing_db_no_events_dir() {
        let dir = TempDir::new().unwrap();
        let events_dir = dir.path().join("events");
        let db_path = dir.path().join("bones.db");

        let result = recover_missing_db(&events_dir, &db_path);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            RecoveryError::EventsDirNotFound(_)
        ));
    }

    #[test]
    fn missing_db_empty_events() {
        let dir = TempDir::new().unwrap();
        let bones_dir = dir.path();

        // Set up minimal bones structure
        let shard_mgr = crate::shard::ShardManager::new(bones_dir);
        shard_mgr.ensure_dirs().expect("ensure dirs");
        shard_mgr.init().expect("init");

        let events_dir = bones_dir.join("events");
        let db_path = bones_dir.join("bones.db");

        let report = recover_missing_db(&events_dir, &db_path).unwrap();
        assert_eq!(report.events_preserved, 0);
        assert!(db_path.exists());
    }

    #[test]
    fn missing_db_with_events_rebuilds() {
        let dir = TempDir::new().unwrap();
        let bones_dir = dir.path();

        // Set up bones with some events
        let shard_mgr = crate::shard::ShardManager::new(bones_dir);
        shard_mgr.ensure_dirs().expect("ensure dirs");
        shard_mgr.init().expect("init");

        // Write a create event
        use crate::event::Event;
        use crate::event::data::*;
        use crate::event::types::EventType;
        use crate::event::writer;
        use crate::model::item::{Kind, Size, Urgency};
        use crate::model::item_id::ItemId;
        use std::collections::BTreeMap;

        let mut event = Event {
            wall_ts_us: 1000,
            agent: "test".into(),
            itc: "itc:AQ".into(),
            parents: vec![],
            event_type: EventType::Create,
            item_id: ItemId::new_unchecked("bn-001"),
            data: EventData::Create(CreateData {
                title: "Test item".into(),
                kind: Kind::Task,
                size: Some(Size::M),
                urgency: Urgency::Default,
                labels: vec![],
                parent: None,
                causation: None,
                description: None,
                extra: BTreeMap::new(),
            }),
            event_hash: String::new(),
        };
        writer::write_event(&mut event).expect("hash");
        let line = writer::write_line(&event).expect("serialize");
        let (year, month) = shard_mgr.active_shard().unwrap().unwrap();
        shard_mgr.append_raw(year, month, &line).expect("append");

        let events_dir = bones_dir.join("events");
        let db_path = bones_dir.join("bones.db");

        let report = recover_missing_db(&events_dir, &db_path).unwrap();
        assert_eq!(report.events_preserved, 1);
        assert!(db_path.exists());

        // Verify the item is in the rebuilt DB
        let conn = crate::db::open_projection(&db_path).unwrap();
        let title: String = conn
            .query_row(
                "SELECT title FROM items WHERE item_id = 'bn-001'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(title, "Test item");
    }

    #[test]
    fn corrupt_db_is_backed_up_before_rebuild() {
        let dir = TempDir::new().unwrap();
        let bones_dir = dir.path();

        let shard_mgr = crate::shard::ShardManager::new(bones_dir);
        shard_mgr.ensure_dirs().expect("ensure dirs");
        shard_mgr.init().expect("init");

        let events_dir = bones_dir.join("events");
        let db_path = bones_dir.join("bones.db");

        // Write something pretending to be a corrupt DB
        fs::write(&db_path, "this is not sqlite").unwrap();

        let report = recover_missing_db(&events_dir, &db_path).unwrap();

        // Corrupt DB should be backed up
        let backup_path = db_path.with_extension("db.corrupt");
        match &report.action_taken {
            RecoveryAction::Quarantined { backup_path: bp } => {
                assert_eq!(bp, &backup_path);
                assert!(backup_path.exists());
                let backup_content = fs::read_to_string(&backup_path).unwrap();
                assert_eq!(backup_content, "this is not sqlite");
            }
            _ => panic!("expected Quarantined action"),
        }
    }

    // ---- Auto-recovery tests ----

    #[test]
    fn auto_recover_nonexistent_project() {
        let dir = TempDir::new().unwrap();
        let bones_dir = dir.path().join(".bones");

        let result = auto_recover(&bones_dir).unwrap();
        assert!(!result.project_valid);
    }

    #[test]
    fn auto_recover_healthy_project() {
        let dir = TempDir::new().unwrap();
        let bones_dir = dir.path();

        // Set up minimal healthy project
        let shard_mgr = crate::shard::ShardManager::new(bones_dir);
        shard_mgr.ensure_dirs().expect("ensure dirs");
        shard_mgr.init().expect("init");

        // Create the DB with rebuild
        let events_dir = bones_dir.join("events");
        let db_path = bones_dir.join("bones.db");
        crate::db::rebuild::rebuild(&events_dir, &db_path).unwrap();

        let result = auto_recover(bones_dir).unwrap();
        assert!(result.project_valid);
        assert!(result.torn_write_repairs.is_empty());
        assert!(!result.db_rebuilt);
        assert_eq!(result.caches_cleaned, 0);
        assert!(result.warnings.is_empty());
    }

    #[test]
    fn auto_recover_repairs_torn_write() {
        let dir = TempDir::new().unwrap();
        let bones_dir = dir.path();

        let shard_mgr = crate::shard::ShardManager::new(bones_dir);
        shard_mgr.ensure_dirs().expect("ensure dirs");
        shard_mgr.init().expect("init");

        // Create DB first
        let events_dir = bones_dir.join("events");
        let db_path = bones_dir.join("bones.db");
        crate::db::rebuild::rebuild(&events_dir, &db_path).unwrap();

        // Simulate torn write: append incomplete data to active shard
        let (year, month) = shard_mgr.active_shard().unwrap().unwrap();
        let shard_path = events_dir.join(format!("{year:04}-{month:02}.events"));
        let mut file = fs::OpenOptions::new()
            .append(true)
            .open(&shard_path)
            .unwrap();
        file.write_all(b"incomplete line without newline").unwrap();

        let result = auto_recover(bones_dir).unwrap();
        assert!(result.project_valid);
        assert_eq!(result.torn_write_repairs.len(), 1);
        // "incomplete line without newline" = 30 bytes, but shard header may
        // affect exact count. Just verify some bytes were repaired.
        assert!(result.torn_write_repairs[0].1 > 0);
    }

    #[test]
    fn auto_recover_rebuilds_missing_db() {
        let dir = TempDir::new().unwrap();
        let bones_dir = dir.path();

        let shard_mgr = crate::shard::ShardManager::new(bones_dir);
        shard_mgr.ensure_dirs().expect("ensure dirs");
        shard_mgr.init().expect("init");

        // Don't create DB — auto_recover should rebuild it
        let result = auto_recover(bones_dir).unwrap();
        assert!(result.project_valid);
        assert!(result.db_rebuilt);
    }

    #[test]
    fn auto_recover_cleans_corrupt_cache() {
        let dir = TempDir::new().unwrap();
        let bones_dir = dir.path();

        let shard_mgr = crate::shard::ShardManager::new(bones_dir);
        shard_mgr.ensure_dirs().expect("ensure dirs");
        shard_mgr.init().expect("init");

        // Create DB
        let events_dir = bones_dir.join("events");
        let db_path = bones_dir.join("bones.db");
        crate::db::rebuild::rebuild(&events_dir, &db_path).unwrap();

        // Create corrupt cache
        let cache_dir = bones_dir.join("cache");
        fs::create_dir_all(&cache_dir).unwrap();
        fs::write(cache_dir.join("events.bin"), "not a valid cache").unwrap();

        let result = auto_recover(bones_dir).unwrap();
        assert!(result.project_valid);
        assert_eq!(result.caches_cleaned, 1);
        assert!(!cache_dir.join("events.bin").exists());
    }

    // ---- Locked DB retry tests ----

    #[test]
    fn open_db_with_retry_succeeds_immediately() {
        let dir = TempDir::new().unwrap();
        let db_path = dir.path().join("test.db");

        // Create a valid DB first
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        conn.execute_batch("CREATE TABLE test (id INTEGER)")
            .unwrap();
        drop(conn);

        // Should open immediately
        let result = open_db_with_retry(&db_path, Duration::from_secs(1));
        assert!(result.is_ok());
    }

    #[test]
    fn open_db_with_retry_handles_missing_db() {
        let dir = TempDir::new().unwrap();
        let db_path = dir.path().join("test.db");

        // Should create the DB (SQLite creates on open)
        let result = open_db_with_retry(&db_path, Duration::from_secs(1));
        assert!(result.is_ok());
    }

    // ---- RecoveryReport Display ----

    #[test]
    fn recovery_action_debug() {
        let action = RecoveryAction::Truncated { bytes_removed: 42 };
        let debug = format!("{action:?}");
        assert!(debug.contains("42"));

        let action = RecoveryAction::Quarantined {
            backup_path: PathBuf::from("/tmp/test.corrupt"),
        };
        let debug = format!("{action:?}");
        assert!(debug.contains("test.corrupt"));
    }

    #[test]
    fn recovery_error_display() {
        let err = RecoveryError::ShardNotFound(PathBuf::from("/tmp/test.events"));
        let display = format!("{err}");
        assert!(display.contains("not found"));

        let err = RecoveryError::LockTimeout(Duration::from_secs(30));
        let display = format!("{err}");
        assert!(display.contains("30s"));
    }
}