mana-core 0.3.2

Core library for mana — task tracker for AI coding agents
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
//! Fast unit index cache.
//!
//! The index (`index.yaml`) is a compact summary of all active units, built
//! by scanning every unit file in the `.mana/` directory. It lets the CLI
//! answer list/filter/graph queries without parsing every individual unit file.
//!
//! The index is rebuilt automatically whenever unit files are newer than the
//! cached `index.yaml`. It is saved atomically to prevent corruption.
//!
//! ## Usage
//!
//! ```rust,no_run
//! use mana_core::index::Index;
//! use std::path::Path;
//!
//! let mana_dir = Path::new("/project/.mana");
//!
//! // Load from disk, rebuilding if stale
//! let index = Index::load_or_rebuild(mana_dir).unwrap();
//! println!("{} units", index.units.len());
//!
//! // Force a full rebuild from unit files
//! let index = Index::build(mana_dir).unwrap();
//! index.save(mana_dir).unwrap();
//! ```

use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::time::{Duration, Instant};

use anyhow::{anyhow, Context, Result};
use chrono::{DateTime, Utc};
use fs2::FileExt;
use serde::{Deserialize, Serialize};

use crate::sqlite;
use crate::unit::{Status, Unit, UnitType};
use crate::util::{atomic_write, natural_cmp};
use crate::yaml;

// ---------------------------------------------------------------------------
// IndexEntry
// ---------------------------------------------------------------------------

/// Default for `created_at` when deserializing old index files that lack the field.
fn default_created_at() -> DateTime<Utc> {
    DateTime::UNIX_EPOCH
}

/// A lightweight summary of a single unit, stored in the index cache.
///
/// `IndexEntry` contains only the fields needed for list/filter/graph
/// operations. For the full unit with description, notes, and history,
/// load the unit file directly via [`crate::unit::Unit::from_file`] or
/// [`crate::api::get_unit`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IndexEntry {
    pub id: String,
    pub title: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub handle: Option<String>,
    pub status: Status,
    pub priority: u8,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dependencies: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub labels: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignee: Option<String>,
    pub updated_at: DateTime<Utc>,
    /// Artifacts this unit produces (for smart dependency inference)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub produces: Vec<String>,
    /// Artifacts this unit requires (for smart dependency inference)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub requires: Vec<String>,
    /// Whether this unit has a verify command (SPECs have verify, GOALs don't)
    #[serde(default)]
    pub has_verify: bool,
    /// The actual verify command string (so agents don't need bn show per-unit)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub verify: Option<String>,
    #[serde(default = "default_created_at")]
    pub created_at: DateTime<Utc>,
    /// Agent or user currently holding a claim on this unit (e.g., "spro:12345" for agent with PID)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub claimed_by: Option<String>,
    /// Number of verify attempts so far
    #[serde(default)]
    pub attempts: u32,
    /// File paths this unit touches (for scope-based blocking)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub paths: Vec<String>,
    /// Explicit unit type.
    pub kind: UnitType,
    /// Whether this unit is a feature (product-level goal, human-only close)
    #[serde(default)]
    pub feature: bool,
    /// Whether this unit has unresolved decisions
    #[serde(default)]
    pub has_decisions: bool,
}

impl From<&Unit> for IndexEntry {
    fn from(unit: &Unit) -> Self {
        Self {
            id: unit.id.clone(),
            title: unit.title.clone(),
            handle: unit.handle.clone(),
            status: unit.status,
            priority: unit.priority,
            parent: unit.parent.clone(),
            dependencies: unit.dependencies.clone(),
            labels: unit.labels.clone(),
            assignee: unit.assignee.clone(),
            updated_at: unit.updated_at,
            produces: unit.produces.clone(),
            requires: unit.requires.clone(),
            has_verify: unit.verify.is_some(),
            verify: unit.verify.clone(),
            created_at: unit.created_at,
            claimed_by: unit.claimed_by.clone(),
            attempts: unit.attempts,
            paths: unit.paths.clone(),
            kind: unit.kind,
            feature: unit.feature,
            has_decisions: !unit.decisions.is_empty(),
        }
    }
}

// ---------------------------------------------------------------------------
// Index
// ---------------------------------------------------------------------------

/// The in-memory and on-disk unit index.
///
/// Holds a flat list of [`IndexEntry`] values for all active (non-archived)
/// units in the project. Archived units are stored separately in
/// `.mana/archive/` and are not included here.
///
/// Obtain an index via [`Index::load_or_rebuild`] (lazy, cached) or
/// [`Index::build`] (always scans unit files from disk).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Index {
    /// All active units, sorted by ID in natural order.
    pub units: Vec<IndexEntry>,
}

// Files to exclude when scanning for unit YAMLs.
const EXCLUDED_FILES: &[&str] = &["config.yaml", "index.yaml", "unit.yaml", "archive.yaml"];

/// Check if a filename represents a unit file (not a config/index/template file).
fn is_unit_filename(filename: &str) -> bool {
    if EXCLUDED_FILES.contains(&filename) {
        return false;
    }
    let ext = std::path::Path::new(filename)
        .extension()
        .and_then(|e| e.to_str());
    match ext {
        Some("md") => filename.contains('-'), // New format: {id}-{slug}.md
        Some("yaml") => true,                 // Legacy format: {id}.yaml
        _ => false,
    }
}

/// Count unit files by format in the units directory.
/// Returns (md_count, yaml_count) tuple.
pub fn count_unit_formats(mana_dir: &Path) -> Result<(usize, usize)> {
    let mut md_count = 0;
    let mut yaml_count = 0;

    let dir_entries = fs::read_dir(mana_dir)
        .with_context(|| format!("Failed to read directory: {}", mana_dir.display()))?;

    for entry in dir_entries {
        let entry = entry?;
        let path = entry.path();

        let filename = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or_default();

        if !is_unit_filename(filename) {
            continue;
        }

        let ext = path.extension().and_then(|e| e.to_str());
        match ext {
            Some("md") => md_count += 1,
            Some("yaml") => yaml_count += 1,
            _ => {}
        }
    }

    Ok((md_count, yaml_count))
}

impl Index {
    /// Build the index by reading all unit files from the units directory.
    /// Supports both new format ({id}-{slug}.md) and legacy format ({id}.yaml).
    /// Excludes config.yaml, index.yaml, and unit.yaml.
    /// Sorts entries by ID using natural ordering.
    /// Returns an error if duplicate unit IDs are detected.
    pub fn build(mana_dir: &Path) -> Result<Self> {
        let mut entries = Vec::new();
        // Track which files define each ID to detect duplicates
        let mut id_to_files: HashMap<String, Vec<String>> = HashMap::new();

        let dir_entries = fs::read_dir(mana_dir)
            .with_context(|| format!("Failed to read directory: {}", mana_dir.display()))?;

        for entry in dir_entries {
            let entry = entry?;
            let path = entry.path();

            let filename = path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or_default();

            if !is_unit_filename(filename) {
                continue;
            }

            let unit = Unit::from_file(&path)
                .with_context(|| format!("Failed to parse unit: {}", path.display()))?;

            // Track this ID's file for duplicate detection
            id_to_files
                .entry(unit.id.clone())
                .or_default()
                .push(filename.to_string());

            entries.push(IndexEntry::from(&unit));
        }

        // Check for duplicate IDs
        let duplicates: Vec<_> = id_to_files
            .iter()
            .filter(|(_, files)| files.len() > 1)
            .collect();

        if !duplicates.is_empty() {
            let mut msg = String::from("Duplicate unit IDs detected:\n");
            for (id, files) in duplicates {
                msg.push_str(&format!("  ID '{}' defined in: {}\n", id, files.join(", ")));
            }
            return Err(anyhow!(msg));
        }

        entries.sort_by(|a, b| natural_cmp(&a.id, &b.id));

        Ok(Index { units: entries })
    }

    /// Check whether the cached index is stale.
    /// Returns true if the index file is missing or if any unit file (.md or .yaml)
    /// in the units directory has been modified after the index was last written.
    pub fn is_stale(mana_dir: &Path) -> Result<bool> {
        let index_path = mana_dir.join("index.yaml");

        // If index doesn't exist, it's stale
        if !index_path.exists() {
            return Ok(true);
        }

        let index_mtime = fs::metadata(&index_path)
            .with_context(|| "Failed to read index.yaml metadata")?
            .modified()
            .with_context(|| "Failed to get index.yaml mtime")?;

        let dir_entries = fs::read_dir(mana_dir)
            .with_context(|| format!("Failed to read directory: {}", mana_dir.display()))?;

        for entry in dir_entries {
            let entry = entry?;
            let path = entry.path();

            let filename = path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or_default();

            if !is_unit_filename(filename) {
                continue;
            }

            let file_mtime = fs::metadata(&path)
                .with_context(|| format!("Failed to read metadata: {}", path.display()))?
                .modified()
                .with_context(|| format!("Failed to get mtime: {}", path.display()))?;

            if file_mtime > index_mtime {
                return Ok(true);
            }
        }

        Ok(false)
    }

    /// Load the cached index or rebuild it if stale.
    /// This is the main entry point for read-heavy commands.
    pub fn load_or_rebuild(mana_dir: &Path) -> Result<Self> {
        if Self::is_stale(mana_dir)? {
            let index = Self::build(mana_dir)?;
            index.save(mana_dir)?;
            Ok(index)
        } else {
            match Self::load(mana_dir) {
                Ok(index) => Ok(index),
                Err(_) => {
                    let index = Self::build(mana_dir)?;
                    index.save(mana_dir)?;
                    Ok(index)
                }
            }
        }
    }

    /// Load the index from the cached index.yaml file.
    pub fn load(mana_dir: &Path) -> Result<Self> {
        let index_path = mana_dir.join("index.yaml");
        let contents = fs::read_to_string(&index_path)
            .with_context(|| format!("Failed to read {}", index_path.display()))?;
        let index: Index =
            yaml::from_str(&contents).with_context(|| "Failed to parse index.yaml")?;
        Ok(index)
    }

    /// Save the index to .mana/index.yaml.
    pub fn save(&self, mana_dir: &Path) -> Result<()> {
        let index_path = mana_dir.join("index.yaml");
        let yaml = serde_yml::to_string(self).with_context(|| "Failed to serialize index")?;
        atomic_write(&index_path, &yaml)
            .with_context(|| format!("Failed to write {}", index_path.display()))?;
        if let Err(error) = sqlite::Index::rebuild(mana_dir) {
            let _ = sqlite::Index::open(mana_dir).and_then(|index| {
                index.mark_stale(&format!("index.yaml save hook failed: {error}"))
            });
        }
        Ok(())
    }

    /// Collect all archived units from .mana/archive/ directory.
    /// Walks through year/month subdirectories and loads all unit files.
    /// Returns IndexEntry items for archived units.
    pub fn collect_archived(mana_dir: &Path) -> Result<Vec<IndexEntry>> {
        let mut entries = Vec::new();
        let archive_dir = mana_dir.join("archive");

        if !archive_dir.is_dir() {
            return Ok(entries);
        }

        // Walk through archive directory recursively
        Self::walk_archive_dir(&archive_dir, &mut entries)?;

        Ok(entries)
    }

    /// Recursively walk archive directory and collect unit entries.
    /// Unit::from_file now guards against YAML parser panics from corrupt files,
    /// so normal Result handling is enough here.
    fn walk_archive_dir(dir: &Path, entries: &mut Vec<IndexEntry>) -> Result<()> {
        use crate::unit::Unit;

        if !dir.is_dir() {
            return Ok(());
        }

        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.is_dir() {
                Self::walk_archive_dir(&path, entries)?;
            } else if path.is_file() {
                if let Some(filename) = path.file_name().and_then(|n| n.to_str()) {
                    if is_unit_filename(filename) {
                        if let Ok(unit) = Unit::from_file(&path) {
                            entries.push(IndexEntry::from(&unit));
                        }
                    }
                }
            }
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// ArchiveIndex — cached index of archived units
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ArchiveIndex {
    pub units: Vec<IndexEntry>,
}

impl ArchiveIndex {
    /// Build the archive index by walking `.mana/archive/` recursively.
    /// Reuses `Index::collect_archived` to find all archived unit files,
    /// then sorts entries by ID using natural ordering.
    pub fn build(mana_dir: &Path) -> Result<Self> {
        let mut entries = Index::collect_archived(mana_dir)?;
        entries.sort_by(|a, b| natural_cmp(&a.id, &b.id));
        Ok(ArchiveIndex { units: entries })
    }

    /// Load the archive index from `.mana/archive.yaml`.
    pub fn load(mana_dir: &Path) -> Result<Self> {
        let path = mana_dir.join("archive.yaml");
        let contents = fs::read_to_string(&path)
            .with_context(|| format!("Failed to read {}", path.display()))?;
        let index: ArchiveIndex =
            yaml::from_str(&contents).with_context(|| "Failed to parse archive.yaml")?;
        Ok(index)
    }

    /// Save the archive index to `.mana/archive.yaml`.
    pub fn save(&self, mana_dir: &Path) -> Result<()> {
        let path = mana_dir.join("archive.yaml");
        let yaml =
            serde_yml::to_string(self).with_context(|| "Failed to serialize archive index")?;
        atomic_write(&path, &yaml)
            .with_context(|| format!("Failed to write {}", path.display()))?;
        Ok(())
    }

    /// Load cached archive index or rebuild if stale.
    pub fn load_or_rebuild(mana_dir: &Path) -> Result<Self> {
        let archive_yaml = mana_dir.join("archive.yaml");
        if Self::is_stale(mana_dir)? {
            let index = Self::build(mana_dir)?;
            // Only save if there are entries or the file already exists
            // (avoids creating archive.yaml when there's no archive dir)
            if !index.units.is_empty() || archive_yaml.exists() {
                index.save(mana_dir)?;
            }
            Ok(index)
        } else if archive_yaml.exists() {
            Self::load(mana_dir)
        } else {
            // No archive dir and no archive.yaml — return empty
            Ok(ArchiveIndex { units: Vec::new() })
        }
    }

    /// Check whether the cached archive index is stale.
    /// Returns true if archive.yaml is missing (and archive dir exists),
    /// or if any file in the archive tree has been modified after archive.yaml.
    pub fn is_stale(mana_dir: &Path) -> Result<bool> {
        let archive_yaml = mana_dir.join("archive.yaml");
        let archive_dir = mana_dir.join("archive");

        if !archive_yaml.exists() {
            // If the archive dir doesn't exist either, nothing to index
            return Ok(archive_dir.is_dir());
        }

        if !archive_dir.is_dir() {
            return Ok(false);
        }

        let index_mtime = fs::metadata(&archive_yaml)
            .with_context(|| "Failed to read archive.yaml metadata")?
            .modified()
            .with_context(|| "Failed to get archive.yaml mtime")?;

        Self::any_file_newer(&archive_dir, index_mtime)
    }

    /// Check if any file in the given directory tree is newer than the reference time.
    fn any_file_newer(dir: &Path, reference: std::time::SystemTime) -> Result<bool> {
        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.is_dir() {
                if Self::any_file_newer(&path, reference)? {
                    return Ok(true);
                }
            } else if path.is_file() {
                let mtime = fs::metadata(&path)?.modified()?;
                if mtime > reference {
                    return Ok(true);
                }
            }
        }
        Ok(false)
    }

    /// Append an entry, deduplicating by ID (replaces any existing entry with the same ID).
    pub fn append(&mut self, entry: IndexEntry) {
        self.units.retain(|e| e.id != entry.id);
        self.units.push(entry);
        self.units.sort_by(|a, b| natural_cmp(&a.id, &b.id));
    }

    /// Remove an entry by ID.
    pub fn remove(&mut self, id: &str) {
        self.units.retain(|e| e.id != id);
    }
}

// ---------------------------------------------------------------------------
// LockedIndex — exclusive access during read-modify-write
// ---------------------------------------------------------------------------

/// Default timeout for acquiring the index lock.
const LOCK_TIMEOUT: Duration = Duration::from_secs(5);

/// Exclusive handle to the index, backed by an advisory flock on `.mana/index.lock`.
///
/// Prevents concurrent read-modify-write races when multiple agents run in parallel.
/// The lock is held from acquisition until `save_and_release` is called or the
/// `LockedIndex` is dropped.
///
/// ```no_run
/// # use anyhow::Result;
/// # use std::path::Path;
/// # fn example(mana_dir: &Path) -> Result<()> {
/// use mana_core::index::LockedIndex;
/// let mut locked = LockedIndex::acquire(mana_dir)?;
/// locked.index.units[0].title = "Updated".to_string();
/// locked.save_and_release()?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct LockedIndex {
    pub index: Index,
    lock_file: fs::File,
    mana_dir: std::path::PathBuf,
}

impl LockedIndex {
    /// Acquire an exclusive lock on the index, then load or rebuild it.
    /// Uses the default 5-second timeout.
    pub fn acquire(mana_dir: &Path) -> Result<Self> {
        Self::acquire_with_timeout(mana_dir, LOCK_TIMEOUT)
    }

    /// Acquire an exclusive lock with a custom timeout.
    pub fn acquire_with_timeout(mana_dir: &Path, timeout: Duration) -> Result<Self> {
        let lock_path = mana_dir.join("index.lock");
        let lock_file = fs::File::create(&lock_path)
            .with_context(|| format!("Failed to create lock file: {}", lock_path.display()))?;

        Self::flock_with_timeout(&lock_file, timeout)?;

        let index = Index::load_or_rebuild(mana_dir)?;

        Ok(Self {
            index,
            lock_file,
            mana_dir: mana_dir.to_path_buf(),
        })
    }

    /// Save the modified index and release the lock.
    pub fn save_and_release(self) -> Result<()> {
        self.index.save(&self.mana_dir)?;
        // self drops here, releasing the flock via Drop
        Ok(())
    }

    /// Poll for an exclusive flock with timeout.
    fn flock_with_timeout(file: &fs::File, timeout: Duration) -> Result<()> {
        let start = Instant::now();
        loop {
            match file.try_lock_exclusive() {
                Ok(()) => return Ok(()),
                Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                    if start.elapsed() >= timeout {
                        return Err(anyhow!(
                            "Timed out after {}s waiting for .mana/index.lock — \
                             another mana process may be running. \
                             If no other process is active, delete .mana/index.lock and retry.",
                            timeout.as_secs()
                        ));
                    }
                    std::thread::sleep(Duration::from_millis(50));
                }
                Err(e) => {
                    return Err(anyhow!("Failed to acquire index lock: {}", e));
                }
            }
        }
    }
}

impl Drop for LockedIndex {
    fn drop(&mut self) {
        // Use fs2's unlock explicitly (std's File::unlock stabilized in 1.89, above our MSRV)
        let _ = fs2::FileExt::unlock(&self.lock_file);
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use std::cmp::Ordering;
    use std::fs;
    use std::thread;
    use std::time::Duration;
    use tempfile::TempDir;

    /// Helper: create a .mana directory with some unit YAML files.
    fn setup_mana_dir() -> (TempDir, std::path::PathBuf) {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        // Create a few units
        let unit1 = Unit::new("1", "First task");
        let unit2 = Unit::new("2", "Second task");
        let unit10 = Unit::new("10", "Tenth task");
        let mut unit3_1 = Unit::new("3.1", "Subtask");
        unit3_1.parent = Some("3".to_string());
        unit3_1.labels = vec!["backend".to_string()];
        unit3_1.dependencies = vec!["1".to_string()];

        unit1.to_file(mana_dir.join("1.yaml")).unwrap();
        unit2.to_file(mana_dir.join("2.yaml")).unwrap();
        unit10.to_file(mana_dir.join("10.yaml")).unwrap();
        unit3_1.to_file(mana_dir.join("3.1.yaml")).unwrap();

        // Create files that should be excluded
        fs::write(mana_dir.join("config.yaml"), "project: test\nnext_id: 11\n").unwrap();

        (dir, mana_dir)
    }

    // -- natural_cmp tests --

    #[test]
    fn natural_sort_basic() {
        assert_eq!(natural_cmp("1", "2"), Ordering::Less);
        assert_eq!(natural_cmp("2", "1"), Ordering::Greater);
        assert_eq!(natural_cmp("1", "1"), Ordering::Equal);
    }

    #[test]
    fn natural_sort_numeric_not_lexicographic() {
        // Lexicographic: "10" < "2", but natural: "10" > "2"
        assert_eq!(natural_cmp("2", "10"), Ordering::Less);
        assert_eq!(natural_cmp("10", "2"), Ordering::Greater);
    }

    #[test]
    fn natural_sort_dotted_ids() {
        assert_eq!(natural_cmp("3", "3.1"), Ordering::Less);
        assert_eq!(natural_cmp("3.1", "3.2"), Ordering::Less);
        assert_eq!(natural_cmp("3.2", "10"), Ordering::Less);
    }

    #[test]
    fn natural_sort_full_sequence() {
        let mut ids = vec!["10", "3.2", "1", "3", "3.1", "2"];
        ids.sort_by(|a, b| natural_cmp(a, b));
        assert_eq!(ids, vec!["1", "2", "3", "3.1", "3.2", "10"]);
    }

    // -- build tests --

    #[test]
    fn build_reads_all_units_and_excludes_config() {
        let (_dir, mana_dir) = setup_mana_dir();
        let index = Index::build(&mana_dir).unwrap();

        // Should have 4 units: 1, 2, 3.1, 10
        assert_eq!(index.units.len(), 4);

        // Should be naturally sorted
        let ids: Vec<&str> = index.units.iter().map(|e| e.id.as_str()).collect();
        assert_eq!(ids, vec!["1", "2", "3.1", "10"]);
    }

    #[test]
    fn build_extracts_fields_correctly() {
        let (_dir, mana_dir) = setup_mana_dir();
        let index = Index::build(&mana_dir).unwrap();

        let entry = index.units.iter().find(|e| e.id == "3.1").unwrap();
        assert_eq!(entry.title, "Subtask");
        assert_eq!(entry.status, Status::Open);
        assert_eq!(entry.priority, 2);
        assert_eq!(entry.parent, Some("3".to_string()));
        assert_eq!(entry.dependencies, vec!["1".to_string()]);
        assert_eq!(entry.labels, vec!["backend".to_string()]);
    }

    #[test]
    fn index_entry_preserves_kind() {
        let mut unit = Unit::new("1", "Epic unit");
        unit.kind = crate::unit::UnitType::Epic;

        let entry = IndexEntry::from(&unit);
        assert_eq!(entry.kind, crate::unit::UnitType::Epic);
    }

    #[test]
    fn build_excludes_index_and_unit_yaml() {
        let (_dir, mana_dir) = setup_mana_dir();

        // Create index.yaml and unit.yaml — these should be excluded
        fs::write(mana_dir.join("index.yaml"), "units: []\n").unwrap();
        fs::write(
            mana_dir.join("unit.yaml"),
            "id: template\ntitle: Template\n",
        )
        .unwrap();

        let index = Index::build(&mana_dir).unwrap();
        assert_eq!(index.units.len(), 4);
        assert!(!index.units.iter().any(|e| e.id == "template"));
    }

    #[test]
    fn build_detects_duplicate_ids() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        // Create two units with the same ID in different files
        let unit_a = Unit::new("99", "Unit A");
        let unit_b = Unit::new("99", "Unit B");

        unit_a.to_file(mana_dir.join("99-a.md")).unwrap();
        unit_b.to_file(mana_dir.join("99-b.md")).unwrap();

        let result = Index::build(&mana_dir);
        assert!(result.is_err());

        let err = result.unwrap_err().to_string();
        assert!(err.contains("Duplicate unit IDs detected"));
        assert!(err.contains("99"));
        assert!(err.contains("99-a.md"));
        assert!(err.contains("99-b.md"));
    }

    #[test]
    fn save_rebuilds_sqlite_index() {
        let (_dir, mana_dir) = setup_mana_dir();
        let index = Index::build(&mana_dir).unwrap();

        index.save(&mana_dir).unwrap();

        let sqlite = sqlite::Index::open(&mana_dir).unwrap();
        assert!(!sqlite.is_stale().unwrap());
        assert!(sqlite.unit_exists("1").unwrap());
        assert!(sqlite.unit_exists("3.1").unwrap());
    }

    #[test]
    fn build_detects_multiple_duplicate_ids() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        // Create duplicates for two different IDs
        Unit::new("1", "First A")
            .to_file(mana_dir.join("1-a.md"))
            .unwrap();
        Unit::new("1", "First B")
            .to_file(mana_dir.join("1-b.md"))
            .unwrap();
        Unit::new("2", "Second A")
            .to_file(mana_dir.join("2-a.md"))
            .unwrap();
        Unit::new("2", "Second B")
            .to_file(mana_dir.join("2-b.md"))
            .unwrap();

        let result = Index::build(&mana_dir);
        assert!(result.is_err());

        let err = result.unwrap_err().to_string();
        assert!(err.contains("ID '1'"));
        assert!(err.contains("ID '2'"));
    }

    // -- is_stale tests --

    #[test]
    fn is_stale_when_index_missing() {
        let (_dir, mana_dir) = setup_mana_dir();
        assert!(Index::is_stale(&mana_dir).unwrap());
    }

    #[test]
    fn is_stale_when_yaml_newer_than_index() {
        let (_dir, mana_dir) = setup_mana_dir();

        // Build and save the index first
        let index = Index::build(&mana_dir).unwrap();
        index.save(&mana_dir).unwrap();

        // Wait a moment to ensure distinct mtimes
        thread::sleep(Duration::from_millis(50));

        // Modify a unit file — this makes it newer than the index
        let unit = Unit::new("1", "Modified first task");
        unit.to_file(mana_dir.join("1.yaml")).unwrap();

        assert!(Index::is_stale(&mana_dir).unwrap());
    }

    #[test]
    fn not_stale_when_index_is_fresh() {
        let (_dir, mana_dir) = setup_mana_dir();

        // Build and save
        let index = Index::build(&mana_dir).unwrap();
        index.save(&mana_dir).unwrap();

        // The index was just written, so it should not be stale
        // (index.yaml mtime >= all other yaml mtimes)
        assert!(!Index::is_stale(&mana_dir).unwrap());
    }

    // -- load_or_rebuild tests --

    #[test]
    fn load_or_rebuild_builds_when_no_index() {
        let (_dir, mana_dir) = setup_mana_dir();

        let index = Index::load_or_rebuild(&mana_dir).unwrap();
        assert_eq!(index.units.len(), 4);

        // Should have created index.yaml
        assert!(mana_dir.join("index.yaml").exists());
    }

    #[test]
    fn load_or_rebuild_loads_when_fresh() {
        let (_dir, mana_dir) = setup_mana_dir();

        // Build + save
        let original = Index::build(&mana_dir).unwrap();
        original.save(&mana_dir).unwrap();

        // load_or_rebuild should load without rebuilding
        let loaded = Index::load_or_rebuild(&mana_dir).unwrap();
        assert_eq!(original, loaded);
    }

    #[test]
    fn load_or_rebuild_rebuilds_when_fresh_cached_index_panics_parser() {
        let (_dir, mana_dir) = setup_mana_dir();

        Index::build(&mana_dir).unwrap().save(&mana_dir).unwrap();
        fs::write(mana_dir.join("index.yaml"), "units: *missing_alias\n").unwrap();

        let loaded = Index::load_or_rebuild(&mana_dir).unwrap();
        assert_eq!(loaded.units.len(), 4);
    }

    // -- save / load round-trip --

    #[test]
    fn save_and_load_round_trip() {
        let (_dir, mana_dir) = setup_mana_dir();

        let index = Index::build(&mana_dir).unwrap();
        index.save(&mana_dir).unwrap();

        let loaded = Index::load(&mana_dir).unwrap();
        assert_eq!(index, loaded);
    }

    // -- empty directory --

    #[test]
    fn build_empty_directory() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        let index = Index::build(&mana_dir).unwrap();
        assert!(index.units.is_empty());
    }

    // -- LockedIndex tests --

    #[test]
    fn locked_index_acquire_and_save() {
        let (_dir, mana_dir) = setup_mana_dir();

        let mut locked = LockedIndex::acquire(&mana_dir).unwrap();
        assert_eq!(locked.index.units.len(), 4);

        // Modify a title
        locked.index.units[0].title = "Modified".to_string();
        locked.save_and_release().unwrap();

        // Verify the change persisted
        let index = Index::load(&mana_dir).unwrap();
        assert_eq!(index.units[0].title, "Modified");
    }

    #[test]
    fn locked_index_blocks_concurrent_access() {
        let (_dir, mana_dir) = setup_mana_dir();

        // First lock
        let _locked = LockedIndex::acquire(&mana_dir).unwrap();

        // Second lock should fail with timeout
        let result = LockedIndex::acquire_with_timeout(&mana_dir, Duration::from_millis(200));
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("Timed out"),
            "Expected timeout error, got: {}",
            err
        );
    }

    #[test]
    fn locked_index_released_on_drop() {
        let (_dir, mana_dir) = setup_mana_dir();

        {
            let _locked = LockedIndex::acquire(&mana_dir).unwrap();
            // lock held in this scope
        }
        // lock released on drop

        // Should be able to acquire again
        let _locked = LockedIndex::acquire(&mana_dir).unwrap();
    }

    #[test]
    fn locked_index_creates_lock_file() {
        let (_dir, mana_dir) = setup_mana_dir();

        let _locked = LockedIndex::acquire(&mana_dir).unwrap();
        assert!(mana_dir.join("index.lock").exists());
    }

    // -- is_stale ignores non-yaml files --

    #[test]
    fn is_stale_ignores_non_yaml() {
        let (_dir, mana_dir) = setup_mana_dir();

        let index = Index::build(&mana_dir).unwrap();
        index.save(&mana_dir).unwrap();

        // Create a non-yaml file after the index
        thread::sleep(Duration::from_millis(50));
        fs::write(mana_dir.join("notes.txt"), "some notes").unwrap();

        // Should NOT be stale — non-yaml files don't count
        assert!(!Index::is_stale(&mana_dir).unwrap());
    }
}

#[cfg(test)]
mod archive_tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn collect_archived_finds_units() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        // Create archive structure
        let archive_dir = mana_dir.join("archive").join("2026").join("02");
        fs::create_dir_all(&archive_dir).unwrap();

        // Create an archived unit
        let mut unit = crate::unit::Unit::new("1", "Archived task");
        unit.status = crate::unit::Status::Closed;
        unit.to_file(archive_dir.join("1-archived-task.md"))
            .unwrap();

        let archived = Index::collect_archived(&mana_dir).unwrap();
        assert_eq!(archived.len(), 1);
        assert_eq!(archived[0].id, "1");
        assert_eq!(archived[0].status, crate::unit::Status::Closed);
    }

    #[test]
    fn collect_archived_empty_when_no_archive() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        let archived = Index::collect_archived(&mana_dir).unwrap();
        assert!(archived.is_empty());
    }
}

#[cfg(test)]
mod format_count_tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn count_unit_formats_only_yaml() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        // Create only yaml files
        let unit1 = crate::unit::Unit::new("1", "Task 1");
        let unit2 = crate::unit::Unit::new("2", "Task 2");
        unit1.to_file(mana_dir.join("1.yaml")).unwrap();
        unit2.to_file(mana_dir.join("2.yaml")).unwrap();

        let (md_count, yaml_count) = count_unit_formats(&mana_dir).unwrap();
        assert_eq!(md_count, 0);
        assert_eq!(yaml_count, 2);
    }

    #[test]
    fn count_unit_formats_only_md() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        // Create only md files
        let unit1 = crate::unit::Unit::new("1", "Task 1");
        let unit2 = crate::unit::Unit::new("2", "Task 2");
        unit1.to_file(mana_dir.join("1-task-1.md")).unwrap();
        unit2.to_file(mana_dir.join("2-task-2.md")).unwrap();

        let (md_count, yaml_count) = count_unit_formats(&mana_dir).unwrap();
        assert_eq!(md_count, 2);
        assert_eq!(yaml_count, 0);
    }

    #[test]
    fn count_unit_formats_mixed() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        // Create mixed formats
        let unit1 = crate::unit::Unit::new("1", "Task 1");
        let unit2 = crate::unit::Unit::new("2", "Task 2");
        let unit3 = crate::unit::Unit::new("3", "Task 3");
        unit1.to_file(mana_dir.join("1.yaml")).unwrap();
        unit2.to_file(mana_dir.join("2-task-2.md")).unwrap();
        unit3.to_file(mana_dir.join("3-task-3.md")).unwrap();

        let (md_count, yaml_count) = count_unit_formats(&mana_dir).unwrap();
        assert_eq!(md_count, 2);
        assert_eq!(yaml_count, 1);
    }

    #[test]
    fn count_unit_formats_excludes_config_files() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        // Create excluded yaml files (config.yaml, index.yaml)
        fs::write(mana_dir.join("config.yaml"), "project: test").unwrap();
        fs::write(mana_dir.join("index.yaml"), "units: []").unwrap();

        // Create one actual unit
        let unit1 = crate::unit::Unit::new("1", "Task 1");
        unit1.to_file(mana_dir.join("1-task-1.md")).unwrap();

        let (md_count, yaml_count) = count_unit_formats(&mana_dir).unwrap();
        assert_eq!(md_count, 1);
        assert_eq!(yaml_count, 0); // config.yaml and index.yaml are excluded
    }

    #[test]
    fn count_unit_formats_empty_dir() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        let (md_count, yaml_count) = count_unit_formats(&mana_dir).unwrap();
        assert_eq!(md_count, 0);
        assert_eq!(yaml_count, 0);
    }
}

#[cfg(test)]
mod archive_index_tests {
    use super::*;
    use tempfile::TempDir;

    fn setup_mana_dir_with_archive() -> (TempDir, std::path::PathBuf) {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        let archive_dir = mana_dir.join("archive").join("2026").join("03");
        fs::create_dir_all(&archive_dir).unwrap();

        let mut unit1 = crate::unit::Unit::new("5", "Archived task five");
        unit1.status = crate::unit::Status::Closed;
        unit1.is_archived = true;
        unit1
            .to_file(archive_dir.join("5-archived-task-five.md"))
            .unwrap();

        let mut unit2 = crate::unit::Unit::new("3", "Archived task three");
        unit2.status = crate::unit::Status::Closed;
        unit2.is_archived = true;
        unit2
            .to_file(archive_dir.join("3-archived-task-three.md"))
            .unwrap();

        (dir, mana_dir)
    }

    #[test]
    fn archive_index_build_from_archive_dir() {
        let (_dir, mana_dir) = setup_mana_dir_with_archive();
        let archive = ArchiveIndex::build(&mana_dir).unwrap();

        assert_eq!(archive.units.len(), 2);
        // Should be sorted by natural ordering: "3" before "5"
        assert_eq!(archive.units[0].id, "3");
        assert_eq!(archive.units[1].id, "5");
        assert_eq!(archive.units[0].status, crate::unit::Status::Closed);
        assert_eq!(archive.units[1].status, crate::unit::Status::Closed);
    }

    #[test]
    fn archive_index_build_empty_when_no_archive_dir() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        let archive = ArchiveIndex::build(&mana_dir).unwrap();
        assert!(archive.units.is_empty());
    }

    #[test]
    fn archive_index_save_load_roundtrip() {
        let (_dir, mana_dir) = setup_mana_dir_with_archive();
        let original = ArchiveIndex::build(&mana_dir).unwrap();
        original.save(&mana_dir).unwrap();

        let loaded = ArchiveIndex::load(&mana_dir).unwrap();
        assert_eq!(original, loaded);
    }

    #[test]
    fn archive_index_append_deduplicates() {
        let (_dir, mana_dir) = setup_mana_dir_with_archive();
        let mut archive = ArchiveIndex::build(&mana_dir).unwrap();
        assert_eq!(archive.units.len(), 2);

        // Append a new entry
        let mut new_unit = crate::unit::Unit::new("7", "New archived");
        new_unit.status = crate::unit::Status::Closed;
        archive.append(IndexEntry::from(&new_unit));
        assert_eq!(archive.units.len(), 3);

        // Append again with same ID — should replace, not duplicate
        let mut updated_unit = crate::unit::Unit::new("7", "Updated title");
        updated_unit.status = crate::unit::Status::Closed;
        archive.append(IndexEntry::from(&updated_unit));
        assert_eq!(archive.units.len(), 3);

        let entry = archive.units.iter().find(|e| e.id == "7").unwrap();
        assert_eq!(entry.title, "Updated title");
    }

    #[test]
    fn archive_index_remove() {
        let (_dir, mana_dir) = setup_mana_dir_with_archive();
        let mut archive = ArchiveIndex::build(&mana_dir).unwrap();
        assert_eq!(archive.units.len(), 2);

        archive.remove("3");
        assert_eq!(archive.units.len(), 1);
        assert_eq!(archive.units[0].id, "5");

        // Removing non-existent ID is a no-op
        archive.remove("999");
        assert_eq!(archive.units.len(), 1);
    }

    #[test]
    fn archive_index_is_stale_when_no_archive_yaml() {
        let (_dir, mana_dir) = setup_mana_dir_with_archive();
        // Archive dir exists but archive.yaml doesn't
        assert!(ArchiveIndex::is_stale(&mana_dir).unwrap());
    }

    #[test]
    fn archive_index_not_stale_when_no_archive_dir() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();
        // Neither archive dir nor archive.yaml exist
        assert!(!ArchiveIndex::is_stale(&mana_dir).unwrap());
    }

    #[test]
    fn archive_index_not_stale_after_build_and_save() {
        let (_dir, mana_dir) = setup_mana_dir_with_archive();
        let archive = ArchiveIndex::build(&mana_dir).unwrap();
        archive.save(&mana_dir).unwrap();
        assert!(!ArchiveIndex::is_stale(&mana_dir).unwrap());
    }

    #[test]
    fn archive_index_stale_when_file_newer() {
        let (_dir, mana_dir) = setup_mana_dir_with_archive();
        let archive = ArchiveIndex::build(&mana_dir).unwrap();
        archive.save(&mana_dir).unwrap();

        // Wait and add a new file to the archive
        std::thread::sleep(std::time::Duration::from_millis(50));
        let archive_dir = mana_dir.join("archive").join("2026").join("03");
        let mut new_unit = crate::unit::Unit::new("9", "Newer");
        new_unit.status = crate::unit::Status::Closed;
        new_unit.is_archived = true;
        new_unit.to_file(archive_dir.join("9-newer.md")).unwrap();

        assert!(ArchiveIndex::is_stale(&mana_dir).unwrap());
    }

    #[test]
    fn archive_index_load_or_rebuild_builds_when_stale() {
        let (_dir, mana_dir) = setup_mana_dir_with_archive();
        let archive = ArchiveIndex::load_or_rebuild(&mana_dir).unwrap();
        assert_eq!(archive.units.len(), 2);
        // Should have created archive.yaml
        assert!(mana_dir.join("archive.yaml").exists());
    }

    #[test]
    fn archive_index_load_or_rebuild_returns_empty_when_no_archive() {
        let dir = TempDir::new().unwrap();
        let mana_dir = dir.path().join(".mana");
        fs::create_dir(&mana_dir).unwrap();

        let archive = ArchiveIndex::load_or_rebuild(&mana_dir).unwrap();
        assert!(archive.units.is_empty());
        // Should NOT create archive.yaml when there's nothing to index
        assert!(!mana_dir.join("archive.yaml").exists());
    }
}