mise 2026.9.3

Dev tools, env vars, and tasks in one CLI
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
//! Capturing checkpoints: the one entry point every capture goes through.
//!
//! Content is deduplicated, records are not: an automatic capture whose
//! snapshot tree and coverage equal the newest checkpoint's records nothing,
//! while a draft carrying metadata of its own (a description, a label, an
//! operation) always writes an ordinary commit, reusing the tracked-file tree.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use eyre::{Result, WrapErr};

use std::collections::BTreeSet;

/// A subtree promotion: the entry's index and the children that were named.
type PartialPromotion = (usize, Vec<PathBuf>);

use super::shadow::{self, HistoryRepo, Overlay};
use super::store::{
    self, Annotation, Changes, Checkpoint, DescriptionSource, Entry, Index, IndexEntry, Machine,
    Operation, TreeInfo, Trigger,
};
use super::tracked::{TrackedEntry, TrackedSet, tree_path_to_display};
use crate::file::display_path;
use crate::lock_file::LockFile;

mod preimages;

/// The most paths a computed description names before `+N more`.
const DESCRIPTION_PATHS: usize = 6;
const DESCRIPTION_MAX: usize = 200;
/// The most paths a `changes` record lists.
const CHANGES_MAX: usize = 2000;

/// What a caller wants captured.
#[derive(Clone, Debug, Default)]
pub(crate) struct Draft {
    pub trigger: Option<Trigger>,
    /// A caller-supplied description; the computed one is kept as `summary`.
    pub description: Option<String>,
    pub description_source: Option<DescriptionSource>,
    pub task: Option<String>,
    pub labels: Vec<String>,
    /// Live contents captured for a `*-before` checkpoint; never promoted.
    pub protective: bool,
    pub operation: Option<Operation>,
    /// A uuid reserved when the operation began, so the marker and the
    /// pending record name the outcome before it exists.
    pub uuid: Option<String>,
    /// Paths named explicitly: manual-save entries covering them are read
    /// live and promoted, becoming their new saved version.
    pub explicit_paths: Vec<PathBuf>,
    /// Explicit enrollment removals; never remove the corresponding live files.
    pub untrack: Vec<PathBuf>,
    /// Paths the watcher is throttling: their files are carried forward
    /// from the newest checkpoint instead of read live, so a capture for
    /// another path never defeats their schedule. Ignored by protective
    /// captures and explicit saves.
    pub held: Vec<PathBuf>,
}

impl Draft {
    pub(crate) fn new(trigger: Trigger) -> Self {
        Self {
            trigger: Some(trigger),
            ..Default::default()
        }
    }

    fn trigger(&self) -> Trigger {
        self.trigger.unwrap_or(Trigger::Edit)
    }

    /// Whether the draft carries something a deduplicated capture would lose.
    fn has_metadata(&self) -> bool {
        !self.trigger().is_automatic()
            || self.description.is_some()
            || self.task.is_some()
            || !self.labels.is_empty()
            || self.protective
            || self.operation.is_some()
    }
}

#[derive(Debug)]
pub(crate) enum Outcome {
    Created(Box<Entry>),
    /// The tracked state equals the newest checkpoint's; nothing recorded.
    Unchanged,
    /// No capture could be taken (no usable git); the reason.
    Unavailable(String),
}

/// An open history store.
#[derive(Debug)]
pub(crate) struct Store {
    state_dir: PathBuf,
    repo: Option<HistoryRepo>,
    unavailable: Option<String>,
    machine: Machine,
}

impl Store {
    pub(crate) fn open_in(state_dir: &Path) -> Result<Self> {
        store::ensure_store_dir_in(state_dir)?;
        let machine = store::machine();
        let (repo, unavailable) = match HistoryRepo::open_or_init_in(state_dir) {
            Ok(Some(repo)) => (Some(repo), None),
            Ok(None) => (None, Some(shadow::unavailable_reason())),
            Err(err) => (None, Some(format!("{err:#}"))),
        };
        let store = Self {
            state_dir: state_dir.to_path_buf(),
            repo,
            unavailable,
            machine,
        };
        // the index is a cache of the repository: rebuild it when it is
        // missing but checkpoints exist
        if !store::index_exists_in(state_dir)
            && let Some(repo) = &store.repo
            && !repo.checkpoint_refs()?.is_empty()
        {
            info!("history: rebuilding the checkpoint index from the repository");
            store.rebuild_index()?;
        }
        Ok(store)
    }

    pub(crate) fn open() -> Result<Self> {
        Self::open_in(&crate::dirs::STATE)
    }

    pub(crate) fn state_dir(&self) -> &Path {
        &self.state_dir
    }

    pub(crate) fn repo(&self) -> Option<&HistoryRepo> {
        self.repo.as_ref()
    }

    pub(crate) fn machine(&self) -> &Machine {
        &self.machine
    }

    /// Why no content can be captured, if git is unusable.
    pub(crate) fn unavailable(&self) -> Option<&str> {
        self.unavailable.as_deref()
    }

    /// Serializes captures and index writes.
    pub(crate) fn lock(&self) -> Result<fslock::LockFile> {
        LockFile::new(&store::store_lock_path_in(&self.state_dir))
            .with_callback(|path| {
                debug!("waiting for the history store lock {}", display_path(path));
            })
            .lock()
    }

    pub(crate) fn list(&self) -> Result<Vec<Entry>> {
        store::list_in(&self.state_dir)
    }

    /// Reserves the next checkpoint id.
    pub(crate) fn reserve_id(&self) -> Result<u64> {
        let mut index = store::load_index_in(&self.state_dir)?;
        let id = index.next_id.max(1);
        index.next_id = id + 1;
        store::write_index_in(&self.state_dir, &index)?;
        Ok(id)
    }

    /// Captures the tracked set into a checkpoint. Takes the store lock.
    pub(crate) fn attempt(&self, tracked: &TrackedSet, draft: Draft) -> Result<Outcome> {
        let _lock = self.lock()?;
        self.attempt_locked(tracked, draft, None)
    }

    /// Like [`attempt`], with a reserved id and the lock already held.
    pub(crate) fn attempt_locked(
        &self,
        tracked: &TrackedSet,
        draft: Draft,
        reserved_id: Option<u64>,
    ) -> Result<Outcome> {
        if self.repo.is_none() {
            return Ok(Outcome::Unavailable(
                self.unavailable
                    .clone()
                    .unwrap_or_else(shadow::unavailable_reason),
            ));
        }
        let resolved = match &self.repo {
            Some(repo) => super::enrollment::resolve(
                &self.state_dir,
                repo,
                tracked,
                if draft.trigger() == Trigger::Baseline {
                    &draft.explicit_paths
                } else {
                    &[]
                },
                &draft.untrack,
            )?,
            None => tracked.clone(),
        };
        let tracked = &resolved;
        if tracked.entries.is_empty()
            && !draft.has_metadata()
            && self
                .repo
                .as_ref()
                .map(|repo| repo.ref_oid(HistoryRepo::HISTORY_REF))
                .transpose()?
                .flatten()
                .is_none()
        {
            // A service may watch policy before the first enrollment. Do not
            // manufacture an unrelated empty root before --adopt adoption.
            return Ok(Outcome::Unchanged);
        }
        let mut index = store::load_index_in(&self.state_dir)?;
        if let Some(repo) = &self.repo
            && repo.ref_oid(HistoryRepo::HISTORY_REF)?.as_deref()
                != index.entries.last().map(|entry| entry.commit.as_str())
        {
            index = self.rebuild_index_locked()?;
        }
        // Metadata-only records (for example an operation while git was
        // unavailable) must not hide the last usable content baseline.
        let previous_tree = index.entries.iter().rev().find_map(|entry| {
            store::read_meta_cache_in(&self.state_dir, &entry.uuid)
                .ok()
                .flatten()
                .and_then(|checkpoint| {
                    checkpoint
                        .tree
                        .snapshot
                        .clone()
                        .map(|tree| (checkpoint, tree))
                })
        });
        let uuid = draft.uuid.clone().unwrap_or_else(store::new_uuid);
        let (mut walk, walk_error) = match tracked.walk() {
            Ok(walk) => (walk, None),
            Err(err) if draft.operation.is_some() => {
                // Keep the operation journal even when its outcome cannot be
                // captured. Never substitute a partial or empty snapshot.
                warn!("history: snapshot failed: {err:#}");
                (Default::default(), Some(format!("{err:#}")))
            }
            Err(err) => return Err(err),
        };
        for warning in &walk.warnings {
            warn!("history: {warning}");
        }
        // manual-save entries: carried forward from their promoted version
        // unless named explicitly (promoted) or captured protectively
        let promoted: BTreeSet<String> = previous_tree
            .as_ref()
            .map(|(record, _)| {
                record
                    .tree
                    .coverage
                    .entries
                    .iter()
                    .map(|entry| entry.path.clone())
                    .collect()
            })
            .unwrap_or_default();
        let manual = manual_plan(&walk, &draft, &promoted);
        if !manual.carry.is_empty() {
            let carried: BTreeSet<usize> = manual.carry.iter().copied().collect();
            let dropped: BTreeSet<PathBuf> = walk
                .files
                .iter()
                .filter(|(_, (owner, _))| carried.contains(owner))
                .map(|(path, _)| path.clone())
                .collect();
            for path in &dropped {
                walk.files.remove(path);
            }
            for root in &mut walk.roots {
                root.files
                    .retain(|rel| !dropped.contains(&root.path.join(rel)));
            }
        }
        // Live modes plus the ordinary parent's modes for carried entries.
        // Protective commits use the same history, not a separate saved state.
        let mut modes = file_modes(&walk);
        if !manual.carry.is_empty() {
            let carried: Vec<String> = manual
                .carry
                .iter()
                .map(|index| walk.entries[*index].display())
                .collect();
            for (path, bits) in self.saved_modes(&index, &carried) {
                modes.entry(path).or_insert(bits);
            }
        }
        // a held path keeps the mode the previous checkpoint recorded (or
        // none), like its content
        if !draft.held.is_empty() && !draft.protective && draft.explicit_paths.is_empty() {
            for held in &draft.held {
                let display = display_path(held);
                modes.remove(&display);
                if let Some((previous_checkpoint, _)) = &previous_tree
                    && let Some(bits) = previous_checkpoint.tree.modes.get(&display)
                {
                    modes.insert(display, *bits);
                }
            }
        }
        let mut coverage = tracked.coverage(&walk);
        let parent_commit = match &self.repo {
            Some(repo) => repo.ref_oid(HistoryRepo::HISTORY_REF)?,
            None => None,
        };
        let recipients = if walk.files.values().any(|(_, policy)| policy.encrypt) {
            tracked.manifest.recipients.clone()
        } else {
            vec![]
        };
        let (snapshot, roots, available, reason) = match (&self.repo, walk_error) {
            (_, Some(reason)) => (None, vec![], false, Some(reason)),
            (Some(repo), None) => {
                match repo.capture_tracked(&walk, &recipients, console::user_attended_stderr()) {
                    Ok(result) => {
                        coverage.omitted.extend(result.omitted.iter().cloned());
                        for warning in &result.warnings {
                            warn!("history: {warning}");
                        }
                        let (composed, partial) = self.compose_manual(
                            repo,
                            &result.tree,
                            ManualContext {
                                entries: &walk.entries,
                                manual: &manual,
                                parent_commit: parent_commit.as_deref(),
                                draft: &draft,
                            },
                        )?;
                        // a subtree promotion keeps the saved modes of the
                        // siblings it did not name, like their content
                        for (entry_index, children) in &partial {
                            let entry = walk.entries[*entry_index].display();
                            let named: Vec<String> =
                                children.iter().map(crate::file::display_path).collect();
                            let is_named =
                                |path: &str| named.iter().any(|child| under_entry(path, child));
                            modes.retain(|path, _| !under_entry(path, &entry) || is_named(path));
                            for (path, bits) in
                                self.saved_modes(&index, std::slice::from_ref(&entry))
                            {
                                if !is_named(&path) {
                                    modes.entry(path).or_insert(bits);
                                }
                            }
                        }
                        let composed = self.hold_paths(
                            repo,
                            &composed,
                            &draft,
                            previous_tree.as_ref().map(|(_, tree)| tree.as_str()),
                            &walk.entries,
                        )?;
                        let omissions: Vec<_> = coverage
                            .omitted
                            .iter()
                            .chain(&coverage.incomplete)
                            .collect();
                        let composed = retain_omitted(
                            repo,
                            &composed,
                            previous_tree
                                .as_ref()
                                .map(|(record, tree)| (record, tree.as_str())),
                            tracked,
                            &omissions,
                            &mut modes,
                            &walk,
                        )?;
                        let mut manifest = super::manifest::Manifest::read(repo, &composed)?
                            .ok_or_else(|| {
                                eyre::eyre!("captured tree is missing enrollment metadata")
                            })?;
                        manifest.capture_permissions(&walk.entries, &modes)?;
                        let composed = manifest.write(repo, &composed)?;
                        (Some(composed), result.roots, true, None)
                    }
                    Err(err) => {
                        warn!("history: snapshot failed: {err:#}");
                        (None, vec![], false, Some(format!("{err:#}")))
                    }
                }
            }
            (None, None) => (None, vec![], false, self.unavailable.clone()),
        };
        for (index, entry) in walk.entries.iter().enumerate() {
            if entry.policy.autosave {
                continue;
            }
            let Some(record) = coverage
                .entries
                .iter_mut()
                .find(|record| record.path == entry.display())
            else {
                continue;
            };
            if manual.promote.contains(&index) {
                record.state = "live".into();
            } else {
                record.state = "saved".into();
            }
        }
        if !draft.has_metadata()
            && let Some((previous_checkpoint, tree)) = &previous_tree
            && snapshot.as_deref() == Some(tree.as_str())
            && (!cfg!(unix) || previous_checkpoint.tree.modes == modes)
        {
            debug!(
                "history: nothing changed since checkpoint {}",
                previous_checkpoint.uuid
            );
            if let Some(repo) = &self.repo {
                super::enrollment::confirm(&self.state_dir, repo, tracked)?;
            }
            return Ok(Outcome::Unchanged);
        }
        if !draft.has_metadata() && snapshot.is_none() {
            return Ok(Outcome::Unavailable(
                reason.unwrap_or_else(shadow::unavailable_reason),
            ));
        }
        let mut changes = match (&self.repo, &snapshot) {
            (Some(repo), Some(tree)) => {
                let since = previous_tree
                    .as_ref()
                    .map(|(checkpoint, _)| checkpoint.uuid.clone());
                let from = previous_tree.as_ref().map(|(_, tree)| tree.as_str());
                changes_from(repo, from, tree, since)?
            }
            _ => Changes::default(),
        };
        // the same bytes under other permissions is a change the tree diff
        // does not show: a path-scoped `latest` and `--path` must see it
        if snapshot.is_some()
            && let Some((previous_checkpoint, _)) = &previous_tree
        {
            let previous_modes = &previous_checkpoint.tree.modes;
            let mode_only: BTreeSet<&String> = modes
                .keys()
                .chain(previous_modes.keys())
                .filter(|path| modes.get(*path) != previous_modes.get(*path))
                .filter(|path| !changes.touches(path))
                .collect();
            changes.modified.extend(mode_only.into_iter().cloned());
        }
        // a manual-save entry carried forward holds its saved version by
        // definition: a difference against a protective capture of its live
        // contents is not a change this checkpoint made
        if !manual.carry.is_empty() {
            let carried: Vec<String> = manual
                .carry
                .iter()
                .map(|index| walk.entries[*index].display())
                .collect();
            let keep = |path: &String| {
                !carried.iter().any(|entry| {
                    path == entry
                        || path
                            .strip_prefix(entry.as_str())
                            .is_some_and(|rest| rest.starts_with('/'))
                })
            };
            changes.added.retain(keep);
            changes.modified.retain(keep);
            changes.removed.retain(keep);
        }
        let total_files: u64 = roots.iter().map(|root| root.files).sum();
        let summary = describe(&draft, &changes, previous_tree.is_some(), total_files);
        let (description, description_source) = match &draft.description {
            Some(text) => (
                text.clone(),
                draft.description_source.unwrap_or(DescriptionSource::User),
            ),
            None => (summary.clone(), DescriptionSource::Computed),
        };
        let mut operation = draft.operation.clone();
        if let Some(operation) = &mut operation
            && matches!(
                operation.kind,
                store::OperationKind::Bootstrap | store::OperationKind::Capture
            )
        {
            let mut affected: BTreeSet<String> = changes
                .added
                .iter()
                .chain(&changes.modified)
                .chain(&changes.removed)
                .cloned()
                .collect();
            // Display summaries are capped; the recovery contract is not.
            if changes.truncated
                && let (Some(repo), Some(tree)) = (&self.repo, &snapshot)
            {
                affected.extend(
                    repo.changes(previous_tree.as_ref().map(|(_, tree)| tree.as_str()), tree)?
                        .into_iter()
                        .filter(|change| !change.path.starts_with(".mise-history/"))
                        .map(|change| tree_path_to_display(&change.path)),
                );
            }
            operation.affected.clear();
            for path in affected {
                let local = crate::file::replace_path(&path);
                // Enrollment changes are not file deletions. Nor can a newly
                // enrolled file acquire a fictional, absent before version.
                let previously_tracked = previous_tree.as_ref().is_some_and(|(record, _)| {
                    record
                        .tree
                        .coverage
                        .entries
                        .iter()
                        .any(|entry| under_entry(&path, &entry.path))
                });
                if previously_tracked
                    && tracked.would_retain(&local)?
                    && (operation.kind == store::OperationKind::Capture
                        || draft
                            .explicit_paths
                            .iter()
                            .any(|selected| local.starts_with(selected)))
                {
                    operation.affected.push(path);
                }
            }
        }
        let checkpoint = Checkpoint {
            schema_version: store::SCHEMA_VERSION,
            uuid,
            machine: self.machine.clone(),
            created_at: store::now_rfc3339(),
            mise_version: crate::cli::version::VERSION_PLAIN.clone(),
            trigger: draft.trigger(),
            description,
            description_source,
            summary,
            task: draft.task.clone(),
            labels: draft.labels.clone(),
            tree: TreeInfo {
                snapshot: snapshot.clone(),
                available,
                reason,
                roots,
                coverage,
                modes,
            },
            changes,
            operation,
        };
        let entry = self.commit_record_locked(checkpoint, index, reserved_id)?;
        if available
            && snapshot.is_some()
            && let Some(repo) = &self.repo
        {
            super::enrollment::confirm(&self.state_dir, repo, tracked)?;
        }
        Ok(Outcome::Created(entry))
    }

    /// Append one ordinary record while the store lock is held.
    fn commit_record_locked(
        &self,
        mut checkpoint: Checkpoint,
        mut index: Index,
        reserved_id: Option<u64>,
    ) -> Result<Box<Entry>> {
        let id = reserved_id.unwrap_or_else(|| {
            let id = index.next_id.max(1);
            index.next_id = id + 1;
            id
        });
        let commit = match &self.repo {
            Some(repo) => repo
                .write_checkpoint(checkpoint.tree.snapshot.as_deref(), &checkpoint)
                .wrap_err("writing the checkpoint")?,
            None => String::new(),
        };
        if let Some(repo) = &self.repo {
            checkpoint = repo.read_meta(&commit)?;
        }
        store::write_meta_cache_in(&self.state_dir, &checkpoint)?;
        index.entries.push(IndexEntry {
            id,
            uuid: checkpoint.uuid.clone(),
            commit: commit.clone(),
            created_at: checkpoint.created_at.clone(),
            trigger: checkpoint.trigger,
        });
        if index.next_id <= id {
            index.next_id = id + 1;
        }
        store::write_index_in(&self.state_dir, &index)?;
        debug!(
            "history: recorded checkpoint {id} ({}): {}",
            checkpoint.trigger.as_str(),
            checkpoint.description
        );
        Ok(Box::new(Entry {
            id,
            commit,
            checkpoint,
        }))
    }

    /// Carries the draft's held paths forward from the previous checkpoint:
    /// their live content is not what this capture records. Protective
    /// captures and explicit saves hold nothing.
    /// Held paths (files whose save is not due yet) take their previous
    /// version, or previous absence, from the newest checkpoint: a change,
    /// a creation, and a deletion are all held until the path is due. Held
    /// paths are files, matched exactly.
    fn hold_paths(
        &self,
        repo: &HistoryRepo,
        tree: &str,
        draft: &Draft,
        previous: Option<&str>,
        entries: &[TrackedEntry],
    ) -> Result<String> {
        if draft.held.is_empty() || draft.protective || !draft.explicit_paths.is_empty() {
            return Ok(tree.to_string());
        }
        let mut overlays = vec![];
        for held in &draft.held {
            let Some(entry) = entries
                .iter()
                .filter(|entry| held.starts_with(&entry.path))
                .max_by_key(|entry| entry.path.components().count())
            else {
                continue;
            };
            let tree_path = entry.tree_path(held)?;
            overlays.push(Overlay {
                object: previous
                    .map(|parent| repo.object_at(parent, &tree_path))
                    .transpose()?
                    .flatten(),
                path: tree_path,
            });
        }
        repo.compose(tree, &overlays)
    }

    fn compose_manual(
        &self,
        repo: &HistoryRepo,
        live_tree: &str,
        context: ManualContext<'_>,
    ) -> Result<(String, Vec<PartialPromotion>)> {
        let ManualContext {
            entries,
            manual,
            parent_commit,
            draft,
        } = context;
        let mut overlays = vec![];
        let mut partial_entries = vec![];
        for index in &manual.carry {
            let entry = &entries[*index];
            let path = entry.tree_path(&entry.path)?;
            let object = parent_commit
                .map(|head| repo.object_at(head, &path))
                .transpose()?
                .flatten();
            overlays.push(Overlay { path, object });
        }
        // A selective save replaces only named children of a manual directory.
        // Its siblings come from the ordinary parent, not a promotion branch.
        if let Some(head) = parent_commit {
            for index in &manual.promote {
                let entry = &entries[*index];
                let children: Vec<PathBuf> = draft
                    .explicit_paths
                    .iter()
                    .filter(|path| path.starts_with(&entry.path) && **path != entry.path)
                    .cloned()
                    .collect();
                if children.is_empty()
                    || draft
                        .explicit_paths
                        .iter()
                        .any(|path| entry.path.starts_with(path))
                {
                    continue;
                }
                let path = entry.tree_path(&entry.path)?;
                overlays.push(Overlay {
                    object: repo.object_at(head, &path)?,
                    path,
                });
                for child in &children {
                    let path = entry.tree_path(child)?;
                    overlays.push(Overlay {
                        object: repo.object_at(live_tree, &path)?,
                        path,
                    });
                }
                partial_entries.push((*index, children));
            }
        }
        Ok((repo.compose(live_tree, &overlays)?, partial_entries))
    }

    /// Manual files carry permission metadata from the ordinary parent.
    fn saved_modes(&self, index: &store::Index, entries: &[String]) -> BTreeMap<String, u32> {
        index
            .entries
            .last()
            .and_then(|entry| {
                store::read_meta_cache_in(&self.state_dir, &entry.uuid)
                    .ok()
                    .flatten()
            })
            .map(|record| {
                record
                    .tree
                    .modes
                    .into_iter()
                    .filter(|(path, _)| entries.iter().any(|entry| under_entry(path, entry)))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Rebuilds the local index from ordinary commit ancestry.
    pub(crate) fn rebuild_index(&self) -> Result<Index> {
        let _lock = self.lock()?;
        self.rebuild_index_locked()
    }

    fn rebuild_index_locked(&self) -> Result<Index> {
        let Some(repo) = &self.repo else {
            return store::load_index_in(&self.state_dir);
        };
        let existing = store::load_index_in(&self.state_dir)?;
        let mut entries = vec![];
        let commits: Vec<_> = repo.checkpoint_refs()?.into_iter().rev().collect();
        let mut annotations: BTreeMap<String, Vec<Annotation>> = BTreeMap::new();
        for (_, commit) in &commits {
            if let Some((target, annotation)) = repo.read_annotation(commit)? {
                annotations.entry(target).or_default().push(annotation);
            }
        }
        for (uuid, commit) in commits {
            // Annotation commits remain in Git ancestry, but are not new file
            // checkpoints and must not move `latest` in the history browser.
            if repo.read_annotation(&commit)?.is_some() {
                continue;
            }
            let mut checkpoint = repo.read_meta(&commit)?;
            if let Some(annotations) = annotations.get(&commit) {
                for annotation in annotations {
                    annotation.apply_to(&mut checkpoint);
                }
            }
            store::write_meta_cache_in(&self.state_dir, &checkpoint)?;
            let id = existing.by_uuid(&uuid).map(|entry| entry.id);
            entries.push((id, checkpoint, commit));
        }
        let mut next_id = existing.next_id.max(1);
        let mut index = Index {
            next_id,
            entries: vec![],
        };
        for (id, checkpoint, commit) in entries {
            let id = id.unwrap_or_else(|| {
                let id = next_id;
                next_id += 1;
                id
            });
            index.entries.push(IndexEntry {
                id,
                uuid: checkpoint.uuid,
                commit,
                created_at: checkpoint.created_at,
                trigger: checkpoint.trigger,
            });
        }
        index.next_id = next_id.max(index.entries.iter().map(|e| e.id + 1).max().unwrap_or(1));
        store::write_index_in(&self.state_dir, &index)?;
        Ok(index)
    }
}

/// Append annotation metadata to ordinary history and rebuild its derived view.
pub(crate) fn annotate(store: &Store, entry: &Entry, annotation: Annotation) -> Result<()> {
    let _lock = store.lock()?;
    if let Some(repo) = store.repo()
        && !entry.commit.is_empty()
    {
        repo.write_annotation(&entry.commit, &annotation)?;
        store.rebuild_index_locked()?;
        return Ok(());
    }
    let mut checkpoint = entry.checkpoint.clone();
    annotation.apply_to(&mut checkpoint);
    store::write_meta_cache_in(store.state_dir(), &checkpoint)
}

/// Everything composing manual-save entries into a snapshot needs.
struct ManualContext<'a> {
    entries: &'a [TrackedEntry],
    manual: &'a ManualPlan,
    parent_commit: Option<&'a str>,
    draft: &'a Draft,
}

/// Which manual-save entries a capture carries forward and which it
/// promotes.
#[derive(Debug, Default)]
struct ManualPlan {
    carry: Vec<usize>,
    promote: Vec<usize>,
}

fn manual_plan(
    walk: &super::tracked::Walk,
    draft: &Draft,
    promoted: &std::collections::BTreeSet<String>,
) -> ManualPlan {
    let mut plan = ManualPlan::default();
    for (index, entry) in walk.entries.iter().enumerate() {
        if entry.policy.autosave {
            continue;
        }
        let named = draft
            .explicit_paths
            .iter()
            .any(|path| path.starts_with(&entry.path) || entry.path.starts_with(path));
        // an entry that was never promoted has no saved version to carry
        // forward: its first capture is its baseline
        let never_promoted = !promoted.contains(&entry.display());
        if named || never_promoted {
            plan.promote.push(index);
        } else {
            plan.carry.push(index);
        }
    }
    plan
}

/// Whether `path` is the entry itself or below it.
fn under_entry(path: &str, entry: &str) -> bool {
    path == entry
        || path
            .strip_prefix(entry)
            .is_some_and(|rest| rest.starts_with('/'))
}

/// A failed observation is not evidence of deletion. Carry only saved objects
/// still permitted by explicit enrollment and exclusions; successful reads win.
fn retain_omitted(
    repo: &HistoryRepo,
    tree: &str,
    previous: Option<(&Checkpoint, &str)>,
    tracked: &TrackedSet,
    omissions: &[&store::PathReason],
    modes: &mut BTreeMap<String, u32>,
    walk: &super::tracked::Walk,
) -> Result<String> {
    let Some((record, parent)) = previous.filter(|_| !omissions.is_empty()) else {
        return Ok(tree.into());
    };
    let roots = super::sync::layout::Roots::current();
    let omitted: Vec<_> = omissions
        .iter()
        .map(|failure| crate::file::replace_path(&failure.path))
        .collect();
    let mut overlays = vec![];
    let observed_directories: BTreeSet<_> = walk
        .files
        .keys()
        .flat_map(|path| path.ancestors().skip(1))
        .collect();
    for file in repo.ls_tree(parent)? {
        let located = roots.locate(&file.path);
        let Some(path) = located.path() else { continue };
        if !omitted.iter().any(|omitted| path.starts_with(omitted))
            || !tracked.would_retain(path)?
            || repo.object_at(tree, &file.path)?.is_some()
        {
            continue;
        }
        let Some(entry) = tracked.entry_for(path) else {
            continue;
        };
        if entry.tree_path(path)? != file.path {
            continue;
        }
        if entry.policy.encrypt && !repo.blob_starts_with(&file.oid, b"mise-encrypted-file-v1\n")? {
            eyre::bail!(
                "cannot retain an unreadable plaintext version of newly encrypted {}",
                display_path(path)
            );
        }
        let display = display_path(path);
        modes.remove(&display);
        if let Some(mode) = record.tree.modes.get(&display) {
            modes.insert(display, *mode);
        }
        for parent in path
            .ancestors()
            .skip(1)
            .take_while(|parent| parent.starts_with(&entry.path))
        {
            if !observed_directories.contains(parent) {
                let display = display_path(parent);
                if let Some(mode) = record.tree.modes.get(&display) {
                    modes.insert(display, *mode);
                }
            }
        }
        overlays.push(Overlay {
            path: file.path,
            object: Some((file.mode, file.oid)),
        });
    }
    repo.compose(tree, &overlays)
}

/// The permission bits of captured regular files that git cannot record
/// (anything but `0644` and `0755`), so a restore can put them back.
#[cfg(unix)]
fn file_modes(walk: &super::tracked::Walk) -> BTreeMap<String, u32> {
    use std::os::unix::fs::PermissionsExt;
    let mut modes = BTreeMap::new();
    let mut dirs = BTreeSet::new();
    for (path, (owner, _)) in &walk.files {
        let Ok(meta) = std::fs::symlink_metadata(path) else {
            continue;
        };
        if meta.is_file() {
            let mode = meta.permissions().mode() & 0o777;
            if mode != 0o644 && mode != 0o755 {
                modes.insert(display_path(path), mode);
            }
        }
        // Directory permissions belong to an explicitly enrolled directory,
        // not to its parents. Enrolling an individual file must not capture
        // unrelated metadata about home or a custom configuration root.
        let enrolled = &walk.entries[*owner].path;
        for ancestor in path.ancestors().skip(1) {
            if !ancestor.starts_with(enrolled) {
                break;
            }
            dirs.insert(ancestor.to_path_buf());
        }
    }
    for dir in dirs {
        let Ok(meta) = std::fs::symlink_metadata(&dir) else {
            continue;
        };
        if meta.is_dir() {
            let mode = meta.permissions().mode() & 0o777;
            if mode != 0o755 {
                modes.insert(display_path(&dir), mode);
            }
        }
    }
    modes
}

#[cfg(not(unix))]
fn file_modes(_walk: &super::tracked::Walk) -> BTreeMap<String, u32> {
    BTreeMap::new()
}

fn changes_from(
    repo: &HistoryRepo,
    from: Option<&str>,
    to: &str,
    since: Option<String>,
) -> Result<Changes> {
    let mut changes = Changes {
        since,
        ..Default::default()
    };
    let mut count = 0usize;
    for change in repo.changes(from, to)? {
        if change.path.starts_with(".mise-history/") {
            continue;
        }
        count += 1;
        if count > CHANGES_MAX {
            changes.truncated = true;
            break;
        }
        let path = tree_path_to_display(&change.path);
        match change.status {
            'A' => changes.added.push(path),
            'D' => changes.removed.push(path),
            _ => changes.modified.push(path),
        }
    }
    Ok(changes)
}

/// The computed one-line description of a checkpoint.
pub(crate) fn describe(
    draft: &Draft,
    changes: &Changes,
    has_previous: bool,
    total_files: u64,
) -> String {
    if let Some(operation) = &draft.operation {
        let what = match operation.message.as_deref() {
            Some(message) => format!(": {message}"),
            None if !changes.is_empty() => format!(": {}", describe_changes(changes)),
            None => String::new(),
        };
        return truncate(format!("{}{what}", operation.kind.as_str()));
    }
    if draft.protective {
        return truncate(format!(
            "before {}",
            draft.trigger().as_str().trim_end_matches("-before")
        ));
    }
    if !has_previous {
        return format!("initial checkpoint ({total_files} files)");
    }
    if changes.is_empty() {
        return "no file changes".to_string();
    }
    truncate(describe_changes(changes))
}

pub(crate) fn describe_changes(changes: &Changes) -> String {
    let mut budget = DESCRIPTION_PATHS;
    let mut groups = vec![];
    let mut more = 0usize;
    for (verb, paths) in [
        ("edited", &changes.modified),
        ("added", &changes.added),
        ("removed", &changes.removed),
    ] {
        if paths.is_empty() {
            continue;
        }
        let mut sorted: Vec<&String> = paths.iter().collect();
        sorted.sort();
        let take = budget.min(sorted.len());
        more += sorted.len() - take;
        if take == 0 {
            continue;
        }
        budget -= take;
        let names: Vec<String> = sorted[..take].iter().map(|path| short_path(path)).collect();
        groups.push(format!("{verb} {}", names.join(", ")));
    }
    let mut text = groups.join("; ");
    if more > 0 {
        text.push_str(&format!(" +{more} more"));
    }
    text
}

/// `~/.config/hypr/bindings.lua` -> `hypr/bindings.lua`; other paths as is.
fn short_path(path: &str) -> String {
    path.strip_prefix("~/.config/")
        .map(str::to_string)
        .unwrap_or_else(|| path.to_string())
}

fn truncate(text: String) -> String {
    if text.chars().count() <= DESCRIPTION_MAX {
        return text;
    }
    let mut cut: String = text.chars().take(DESCRIPTION_MAX - 1).collect();
    if let Some(boundary) = cut.rfind(", ") {
        cut.truncate(boundary);
    }
    cut.push('…');
    cut
}

/// A minimal record for repository tests.
#[cfg(test)]
pub(crate) fn test_checkpoint(uuid: &str, snapshot: Option<&str>) -> Checkpoint {
    Checkpoint {
        schema_version: store::SCHEMA_VERSION,
        uuid: uuid.to_string(),
        machine: Machine {
            id: "machine".into(),
            name: "test".into(),
        },
        created_at: store::now_rfc3339(),
        mise_version: "0".into(),
        trigger: Trigger::Save,
        description: "test".into(),
        description_source: DescriptionSource::Computed,
        summary: "test".into(),
        task: None,
        labels: vec![],
        tree: TreeInfo {
            snapshot: snapshot.map(str::to_string),
            available: snapshot.is_some(),
            reason: None,
            roots: vec![],
            coverage: Default::default(),
            modes: Default::default(),
        },
        changes: Changes::default(),
        operation: None,
    }
}

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

    #[test]
    fn automatic_capture_without_enrollment_does_not_create_a_root() -> Result<()> {
        let temp = tempfile::tempdir()?;
        let store = Store::open_in(temp.path())?;
        assert!(matches!(
            store.attempt(&TrackedSet::default(), Draft::new(Trigger::Edit))?,
            Outcome::Unchanged
        ));
        assert!(
            store
                .repo()
                .unwrap()
                .ref_oid(HistoryRepo::HISTORY_REF)?
                .is_none()
        );
        let mut labeled = Draft::new(Trigger::Agent);
        labeled.description = Some("explicit operation boundary".into());
        assert!(matches!(
            store.attempt(&TrackedSet::default(), labeled)?,
            Outcome::Created(_)
        ));
        Ok(())
    }

    #[test]
    fn unavailable_git_does_not_create_a_parallel_metadata_history() -> Result<()> {
        let temp = tempfile::tempdir()?;
        let mut store = Store::open_in(temp.path())?;
        store.repo = None;
        store.unavailable = Some("Git is unavailable".into());
        let mut draft = Draft::new(Trigger::Agent);
        draft.description = Some("labeled operation".into());
        assert!(matches!(
            store.attempt(&TrackedSet::default(), draft)?,
            Outcome::Unavailable(_)
        ));
        assert!(store::load_index_in(temp.path())?.entries.is_empty());
        Ok(())
    }

    #[test]
    fn numeric_commit_prefixes_cannot_silently_select_another_checkpoint() -> Result<()> {
        let temp = tempfile::tempdir()?;
        let store = Store::open_in(temp.path())?;
        let Outcome::Created(mut first) =
            store.attempt(&TrackedSet::default(), Draft::new(Trigger::Agent))?
        else {
            panic!("expected a checkpoint");
        };
        first.id = 42;
        first.checkpoint.uuid = "123abc".into();
        let mut second = first.clone();
        second.id = 123;
        second.checkpoint.uuid = "abcdef".into();
        let entries = vec![*first, *second];
        assert!(store::resolve_ref("12", &entries).is_err());
        assert_eq!(store::resolve_ref("42", &entries)?, 42);
        assert_eq!(store::resolve_ref("123", &entries)?, 123);
        assert_eq!(store::resolve_ref("commit:12", &entries)?, 42);
        assert_eq!(store::resolve_ref("commit:123", &entries)?, 42);
        assert!(store::resolve_ref("commit:", &entries).is_err());
        assert!(store::resolve_ref("", &entries).is_err());
        assert!(store::resolve_ref("1234567890123456789012345678901234567890", &entries).is_err());
        assert_eq!(store::resolve_ref("123abc", &entries)?, 42);
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn permission_capture_stops_at_explicit_enrollment() {
        use super::super::tracked::{TrackedEntry, Walk};
        use crate::system::files::{FileMode, FilePolicy};
        use std::os::unix::fs::PermissionsExt;

        let temp = tempfile::tempdir().unwrap();
        let parent = temp.path().join("private-parent");
        let enrolled = parent.join("enrolled");
        std::fs::create_dir_all(&enrolled).unwrap();
        let path = enrolled.join("config");
        std::fs::write(&path, "config").unwrap();
        for directory in [&parent, &enrolled] {
            std::fs::set_permissions(directory, std::fs::Permissions::from_mode(0o700)).unwrap();
        }
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
        let policy = FilePolicy::for_mode(FileMode::Track);
        let mut walk = Walk {
            entries: vec![TrackedEntry::new(enrolled.clone(), "track", policy)],
            files: BTreeMap::from([(path.clone(), (0, policy))]),
            ..Default::default()
        };
        assert_eq!(
            file_modes(&walk),
            BTreeMap::from([
                (display_path(&enrolled), 0o700),
                (display_path(&path), 0o600),
            ])
        );
        walk.entries[0].path = path.clone();
        assert_eq!(
            file_modes(&walk),
            BTreeMap::from([(display_path(&path), 0o600)])
        );
    }

    fn changes(modified: &[&str], added: &[&str], removed: &[&str]) -> Changes {
        Changes {
            since: None,
            added: added.iter().map(|s| s.to_string()).collect(),
            modified: modified.iter().map(|s| s.to_string()).collect(),
            removed: removed.iter().map(|s| s.to_string()).collect(),
            truncated: false,
        }
    }

    #[test]
    fn descriptions_group_sort_and_cap() {
        let draft = Draft::new(Trigger::Edit);
        assert_eq!(
            describe(&draft, &Changes::default(), false, 12),
            "initial checkpoint (12 files)"
        );
        assert_eq!(
            describe(&draft, &Changes::default(), true, 12),
            "no file changes"
        );
        let c = changes(
            &["~/.config/hypr/monitors.lua", "~/.config/hypr/bindings.lua"],
            &["~/.config/omarchy/hooks/post-theme"],
            &["~/.XCompose"],
        );
        assert_eq!(
            describe(&draft, &c, true, 0),
            "edited hypr/bindings.lua, hypr/monitors.lua; added omarchy/hooks/post-theme; removed ~/.XCompose"
        );
        let many: Vec<String> = (0..10).map(|i| format!("~/.f{i}")).collect();
        let refs: Vec<&str> = many.iter().map(String::as_str).collect();
        let text = describe(&draft, &changes(&refs, &[], &[]), true, 0);
        assert!(text.ends_with("+4 more"), "{text}");
        assert!(text.chars().count() <= DESCRIPTION_MAX);
    }

    #[test]
    fn operation_descriptions_name_the_kind() {
        let mut draft = Draft::new(Trigger::Bootstrap);
        draft.operation = Some(Operation {
            id: "test-operation".into(),
            kind: store::OperationKind::Bootstrap,
            status: store::OperationStatus::Completed,
            command: "bootstrap".into(),
            argv: vec![],
            cwd: PathBuf::new(),
            user: None,
            finished_at: None,
            error: None,
            before: None,
            to: None,
            undoes: None,
            applied: None,
            affected: vec![],
            sources: vec![],
            directories: vec![],
            directory_modes: Default::default(),
            message: None,
            journal: vec![],
        });
        let c = changes(&["~/.zshrc"], &[], &[]);
        assert_eq!(describe(&draft, &c, true, 0), "bootstrap: edited ~/.zshrc");
        let mut protective = Draft::new(Trigger::BootstrapBefore);
        protective.protective = true;
        assert_eq!(describe(&protective, &c, true, 0), "before bootstrap");
    }

    #[test]
    fn metadata_decides_deduplication() {
        assert!(!Draft::new(Trigger::Edit).has_metadata());
        assert!(!Draft::new(Trigger::Save).has_metadata());
        assert!(Draft::new(Trigger::Agent).has_metadata());
        let mut described = Draft::new(Trigger::Edit);
        described.description = Some("x".into());
        assert!(described.has_metadata());
    }
}