aither-sandbox 0.12.0

Terminal-first command execution with heel sandboxing
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
//! What a subagent's terminal is allowed to write.
//!
//! A subagent works in a space of its own inside its launcher's space and can
//! read, but not write, everything its launcher can write. The launcher widens
//! that with a [`SubagentGrant`]: directories, usually linked worktrees, the
//! subagent may write, and the worktrees it may commit in.
//!
//! A linked worktree keeps its git data in the main repository, outside the
//! worktree, so a worktree grant alone leaves `git commit` failing. A commit
//! grant gives the worktree a repository of its own, created by the host when
//! the grant is first given, that the worktree's `.git` file names: it holds
//! the worktree's `HEAD`, index, refs and new objects, reads the main object
//! store as a read-only alternate and includes the main repository's
//! configuration. It is the only path the grant makes writable, so nothing a
//! subagent writes can change what the main repository reads. The commits
//! reach the main repository only through the [`CommitLedger`]'s import, which
//! packs them inside the subagent's sandbox and verifies the pack on the host
//! before any ref moves.

mod host_git;
mod import;
mod repository;
mod sandboxed;

use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use futures::future::join_all;

pub use repository::Spelling;
use sandboxed::Bounds;
pub use sandboxed::ExportError;
pub use sandboxed::{SandboxedExit, SandboxedGitImpl, SandboxedRun, SubagentGit};

/// What a launcher grants a subagent beyond its own space.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SubagentGrant {
    worktrees: Vec<WorktreeGrant>,
}

/// One directory a subagent may write, and whether it may commit there.
#[derive(Debug, Clone, PartialEq, Eq)]
struct WorktreeGrant {
    path: PathBuf,
    commit: bool,
}

impl SubagentGrant {
    /// A grant of nothing beyond the subagent's own space.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Lets the subagent write the directory `path`, usually a linked worktree
    /// the launcher created. Its git data stays read-only, so the subagent
    /// cannot commit there.
    #[must_use]
    pub fn with_worktree(mut self, path: impl Into<PathBuf>) -> Self {
        self.worktrees.push(WorktreeGrant {
            path: path.into(),
            commit: false,
        });
        self
    }

    /// Lets the subagent write the linked worktree at `path` and commit in it.
    ///
    /// The worktree's branch must lie in a namespace of its own,
    /// `refs/heads/subagent/<id>/<name>`, holding no other branch when the
    /// subagent launches: the commit grant covers that namespace and no other
    /// ref.
    #[must_use]
    pub fn with_committing_worktree(mut self, path: impl Into<PathBuf>) -> Self {
        self.worktrees.push(WorktreeGrant {
            path: path.into(),
            commit: true,
        });
        self
    }
}

/// Why a [`SubagentGrant`] cannot be given.
#[derive(Debug, thiserror::Error)]
pub enum SubagentGrantError {
    /// The launcher cannot write the path, so it cannot hand it on.
    #[error("{} is not writable by the agent launching the subagent", .0.display())]
    OutsideAuthority(PathBuf),
    /// The launcher is a subagent that may not commit in the worktree, so it
    /// cannot hand that right on.
    #[error(
        "the agent launching the subagent may not commit in {}, so it cannot grant commit there",
        .0.display()
    )]
    CommitOutsideAuthority(PathBuf),
    /// A path of the grant could not be inspected.
    #[error("cannot inspect {}: {source}", .path.display())]
    Inspect {
        /// The path.
        path: PathBuf,
        /// The failure.
        source: io::Error,
    },
    /// The granted path holds a repository checkout, whose git data sits inside
    /// it and so would be writable whatever the commit grant says.
    #[error(
        "{} is a repository checkout, not a linked worktree: its git data would be writable without a commit grant",
        .0.display()
    )]
    RepositoryCheckout(PathBuf),
    /// A linked worktree's `.git` file or its git directory is not what git
    /// writes for a linked worktree of the repository it names.
    #[error("malformed git data of worktree {}: {reason}", .path.display())]
    MalformedGitData {
        /// The worktree.
        path: PathBuf,
        /// What is wrong.
        reason: &'static str,
    },
    /// A commit grant names a path that is not a linked worktree.
    #[error("{} is not a linked worktree, so there is nothing to commit in", .0.display())]
    NotAWorktree(PathBuf),
    /// Git data the commit authorization depends on overlaps a path some
    /// subagent can write, so a subagent could rewrite it.
    #[error(
        "the git data {} of worktree {} overlaps {}, which a subagent can write",
        .git_data.display(),
        .worktree.display(),
        .writable.display()
    )]
    GitDataExposed {
        /// The worktree.
        worktree: PathBuf,
        /// Its git directory or repository.
        git_data: PathBuf,
        /// The writable path it overlaps.
        writable: PathBuf,
    },
    /// The worktree's branch does not lie in a subagent branch namespace.
    #[error(
        "worktree {} must be on a branch `refs/heads/subagent/<id>/<name>` to be granted commit; its HEAD is `{head}`",
        .worktree.display()
    )]
    BranchOutsideNamespace {
        /// The worktree.
        worktree: PathBuf,
        /// What its `HEAD` holds.
        head: String,
    },
    /// The branch namespace of a committing worktree holds other refs, so it
    /// is not that worktree's own.
    #[error(
        "the branch namespace `{namespace}` of worktree {} also holds `{other}`; each committing worktree needs a namespace of its own",
        .worktree.display()
    )]
    SharedNamespace {
        /// The worktree.
        worktree: PathBuf,
        /// The namespace, `refs/heads/subagent/<id>/`.
        namespace: String,
        /// A ref in it that is not the worktree's branch.
        other: String,
    },
    /// The repository stores refs in a reftable, which has no per-namespace
    /// directory a grant could cover.
    #[error(
        "the repository {} stores its refs in a reftable, which a commit grant cannot narrow to one branch namespace",
        .0.display()
    )]
    ReftableRepository(PathBuf),
    /// The refs of a committing worktree could not be read.
    #[error(transparent)]
    Ledger(#[from] CommitLedgerError),
    /// The terminal runs in a container that does not say which host
    /// directory it mounts, so a subagent's paths have no container spelling.
    #[error(
        "the container runtime declares no workspace mount, so a subagent's paths cannot be confined in it"
    )]
    NoContainerMount,
    /// A path the subagent works in is not inside the container's workspace
    /// mount.
    #[error("{} has no path inside the container: {source}", .path.display())]
    OutsideContainerMount {
        /// The host path.
        path: PathBuf,
        /// Why it cannot be mapped.
        source: crate::MountRootError,
    },
    /// The subagent's own space could not be created.
    #[error("cannot create the subagent space {}: {source}", .path.display())]
    Space {
        /// The directory.
        path: PathBuf,
        /// The failure.
        source: io::Error,
    },
    /// The repository of a committing worktree could not be created.
    #[error("cannot create the subagent repository at {}: {source}", .path.display())]
    Repository {
        /// The path being written.
        path: PathBuf,
        /// The failure.
        source: io::Error,
    },
    /// A path the repository of a committing worktree must spell out is not
    /// text on one line.
    #[error("{} cannot be written into a subagent repository", .0.display())]
    UnrepresentablePath(PathBuf),
    /// A path handed out with a plain grant contains the committing
    /// worktree, so a subagent holding that grant could replace the worktree
    /// and make the host write its `.git` file elsewhere.
    #[error(
        "{} contains worktree {}, which is granted commit, and is writable by a subagent",
        .writable.display(),
        .worktree.display()
    )]
    WorktreeExposed {
        /// The worktree.
        worktree: PathBuf,
        /// The writable path that contains it.
        writable: PathBuf,
    },
    /// The git on the host is older than a commit grant works with.
    #[error("a commit grant needs git 2.29 or later; the host has `{found}`")]
    GitTooOld {
        /// What `git --version` printed.
        found: String,
    },
    /// A commit grant was asked for somewhere the terminal can run no git
    /// shim, so the isolation the grant promises could not be kept.
    #[error("a commit grant needs a POSIX shell for its git shim; this platform has none")]
    CommitGrantUnsupported,
}

/// What a launching terminal may hand on.
#[derive(Clone, Copy)]
pub struct Launcher<'a> {
    /// The canonical paths the launcher can write, its working directory
    /// included.
    pub writable: &'a [PathBuf],
    /// The worktrees a subagent launcher may commit in. `None` for the main
    /// agent, whose commit grants are read from the repository and need its
    /// git data inside [`Self::writable`].
    pub committing: Option<&'a [CommittingWorktree]>,
    /// The directory every subagent space of the session lives under.
    pub spaces_root: &'a Path,
    /// How the subagents' backend spells host paths.
    pub(crate) spelling: Spelling<'a>,
}

/// What the main agent's terminal has handed to subagents so far.
///
/// Every subagent can write only paths recorded here (or inside a space
/// under the spaces root), so git data that overlaps none of them cannot have
/// been rewritten by a subagent. A worktree granted commit once keeps the
/// git data resolved then, whatever its `.git` file says later.
#[derive(Debug, Default)]
pub struct LaunchHistory {
    /// Every directory handed out with a plain write grant.
    handed_out: Vec<PathBuf>,
    /// Every worktree granted commit, with the git data resolved for it.
    committed: Vec<CommittingWorktree>,
}

/// The paths a [`SubagentGrant`] resolved to.
#[derive(Debug)]
pub struct ResolvedGrant {
    pub writable: Vec<PathBuf>,
    pub committing: Vec<CommittingWorktree>,
}

/// A linked worktree a subagent may commit in, with the git data the grant
/// covers. Everything here was resolved when the grant was first given and is
/// never re-read from paths a subagent can write.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommittingWorktree {
    /// The worktree.
    worktree: PathBuf,
    /// Its entry in the repository's `worktrees`: git's own files for it,
    /// `HEAD` mirrored by the import. Nothing in it is granted.
    git_dir: PathBuf,
    /// The repository's shared git directory.
    common_dir: PathBuf,
    /// The repository the host created for the worktree, inside its entry.
    /// The one path the commit grant makes writable.
    repository: PathBuf,
    /// The `<id>` of the worktree's branch namespace `refs/heads/subagent/<id>/`.
    namespace: String,
}

/// The most bytes a subagent's sandbox may send the host while the commits it
/// made are imported: the pack, and every answer to a question about its
/// repository.
///
/// The budget is an operator setting. Every clone is a handle on the same
/// value, so a change through any of them applies to the next import of every
/// terminal that holds one. There is no ceiling, and `0` means the bytes are
/// not bounded at all.
#[derive(Debug, Clone)]
pub struct ImportBudget(Arc<AtomicU64>);

impl ImportBudget {
    /// A budget of `bytes`; `0` is unbounded.
    #[must_use]
    pub fn new(bytes: u64) -> Self {
        Self(Arc::new(AtomicU64::new(bytes)))
    }

    /// Changes the budget for every handle; `0` is unbounded.
    pub fn set(&self, bytes: u64) {
        self.0.store(bytes, Ordering::Relaxed);
    }

    /// The budget now, `0` being unbounded.
    #[must_use]
    pub fn get(&self) -> u64 {
        self.0.load(Ordering::Relaxed)
    }

    /// The byte bound of an import that starts now.
    fn limit(&self) -> Option<u64> {
        Some(self.get()).filter(|bytes| *bytes != 0)
    }
}

/// What bounds one import of a subagent's commits.
#[derive(Debug, Clone)]
pub struct ImportLimits {
    /// The most bytes of pack, or of any answer, the subagent's sandbox may
    /// send the host.
    pub bytes: ImportBudget,
    /// How long each piece of sandboxed work of an import may take: every
    /// question to the subagent's repository, the pack, and the tidying after.
    pub time: Duration,
}

impl ImportLimits {
    /// The bounds an import that starts now runs under.
    fn bounds(&self) -> Bounds {
        Bounds {
            bytes: self.bytes.limit(),
            time: self.time,
        }
    }
}

/// Whether one path lies inside the other.
fn overlaps(a: &Path, b: &Path) -> bool {
    a.starts_with(b) || b.starts_with(a)
}

fn inside_any(path: &Path, roots: &[PathBuf]) -> bool {
    roots.iter().any(|root| path.starts_with(root))
}

impl CommittingWorktree {
    /// The worktree.
    pub(crate) fn worktree(&self) -> &Path {
        &self.worktree
    }

    /// Its entry in the repository's `worktrees`: the host-owned git
    /// directory a main agent's `git` in the worktree resolves to.
    pub(crate) fn git_dir(&self) -> &Path {
        &self.git_dir
    }

    /// `refs/heads/subagent/<id>/`, as git spells the namespace.
    fn ref_prefix(&self) -> String {
        format!("refs/heads/subagent/{}/", self.namespace)
    }

    /// The directories a commit writes: the worktree's repository, and
    /// nothing else.
    pub fn writable(&self) -> Vec<PathBuf> {
        vec![self.repository.clone()]
    }

    fn layout(&self) -> repository::Layout<'_> {
        repository::Layout {
            worktree: &self.worktree,
            entry: &self.git_dir,
            common: &self.common_dir,
            repository: &self.repository,
            namespace: &self.namespace,
        }
    }

    /// Gives the worktree its repository, if it has none yet, and points the
    /// worktree's `.git` file at it.
    async fn prepare(&self, spelling: Spelling<'_>) -> Result<(), SubagentGrantError> {
        repository::ensure(self.layout(), spelling).await
    }

    /// Reads the git data of the linked worktree at `worktree` from the
    /// repository, for a commit grant the main agent gives.
    ///
    /// `exposed` are the paths a subagent can write that must not overlap the
    /// worktree's git data; `plain` those among them that were handed out
    /// whole, which must not contain the worktree either.
    async fn discover(
        worktree: &Path,
        git: Option<WorktreeGit>,
        exposed: impl Iterator<Item = &Path>,
        plain: impl Iterator<Item = &Path>,
    ) -> Result<Self, SubagentGrantError> {
        let malformed = |reason| SubagentGrantError::MalformedGitData {
            path: worktree.to_path_buf(),
            reason,
        };
        let WorktreeGit {
            git_dir,
            common_dir,
            converted,
        } = git.ok_or_else(|| SubagentGrantError::NotAWorktree(worktree.to_path_buf()))?;

        // The repository must list this worktree back: its git directory is
        // one of the repository's worktree entries, and that entry names this
        // worktree's `.git` file.
        if git_dir.parent() != Some(common_dir.join("worktrees").as_path()) {
            return Err(malformed(
                "its git directory is not an entry of the repository's `worktrees`",
            ));
        }
        let back = read_file(&git_dir.join("gitdir")).await?;
        let back = canonical(&git_dir.join(back.trim())).await?;
        if back != canonical(&worktree.join(".git")).await? {
            return Err(malformed(
                "the repository's worktree entry names another worktree",
            ));
        }
        if async_fs::metadata(common_dir.join("reftable"))
            .await
            .is_ok()
        {
            return Err(SubagentGrantError::ReftableRepository(common_dir));
        }
        for writable in exposed {
            for git_data in [&git_dir, &common_dir] {
                if overlaps(writable, git_data) {
                    return Err(SubagentGrantError::GitDataExposed {
                        worktree: worktree.to_path_buf(),
                        git_data: git_data.clone(),
                        writable: writable.to_path_buf(),
                    });
                }
            }
        }
        // The host writes the worktree's `.git` file; a subagent that could
        // swap the worktree for a symlink first would redirect that write.
        for writable in plain {
            if worktree.starts_with(writable) {
                return Err(SubagentGrantError::WorktreeExposed {
                    worktree: worktree.to_path_buf(),
                    writable: writable.to_path_buf(),
                });
            }
        }

        let repository = git_dir.join(repository::REPOSITORY_DIR);
        if converted {
            // The namespace was fixed when the repository was created, in a
            // file only the host writes; the repository itself says what a
            // subagent made it say.
            let namespace = read_file(&git_dir.join(repository::NAMESPACE_FILE)).await?;
            let namespace = namespace.trim();
            if namespace.is_empty()
                || namespace.contains('/')
                || namespace == "."
                || namespace == ".."
            {
                return Err(malformed("its recorded branch namespace is not valid"));
            }
            return Ok(Self {
                worktree: worktree.to_path_buf(),
                git_dir,
                common_dir,
                repository,
                namespace: namespace.to_string(),
            });
        }

        let head = read_file(&git_dir.join("HEAD")).await?;
        let head = head.trim();
        let namespace = head
            .strip_prefix("ref: refs/heads/subagent/")
            .and_then(|rest| rest.split_once('/'))
            .filter(|(id, name)| !name.is_empty() && !id.is_empty() && *id != "." && *id != "..")
            .map(|(id, _)| id.to_string())
            .ok_or_else(|| SubagentGrantError::BranchOutsideNamespace {
                worktree: worktree.to_path_buf(),
                head: head.to_string(),
            })?;
        let branch = head.trim_start_matches("ref: ").to_string();

        let worktree = Self {
            worktree: worktree.to_path_buf(),
            git_dir,
            common_dir,
            repository,
            namespace,
        };
        // The namespace is the worktree's own: nothing else lives in it, so
        // every ref the subagent moves there is its own work.
        if let Some(other) = worktree
            .namespace_refs()
            .await?
            .into_iter()
            .map(|(_, name)| name)
            .find(|name| *name != branch)
        {
            return Err(SubagentGrantError::SharedNamespace {
                namespace: worktree.ref_prefix(),
                worktree: worktree.worktree,
                other,
            });
        }
        Ok(worktree)
    }

    /// Every ref in the namespace, as `(object id, ref name)`, read from the
    /// repository.
    async fn namespace_refs(&self) -> Result<Vec<(String, String)>, CommitLedgerError> {
        let prefix = self.ref_prefix();
        let output = host_git::git(
            &self.common_dir,
            &[
                "for-each-ref",
                "--format=%(objectname)%00%(refname)",
                prefix.as_str(),
            ],
        )
        .await?;
        output
            .lines()
            .map(|line| {
                line.split_once('\0')
                    .and_then(|(id, name)| Some((object_id(id)?.to_string(), name.to_string())))
                    .ok_or_else(|| CommitLedgerError::Output {
                        repository: self.common_dir.clone(),
                        line: line.to_string(),
                    })
            })
            .collect()
    }

    /// The commits the worktree's refs point at now: every ref in the branch
    /// namespace and a detached `HEAD`. Both live in the main repository,
    /// where only the host writes.
    async fn tips(&self) -> Result<Vec<String>, CommitLedgerError> {
        let mut tips: Vec<String> = self
            .namespace_refs()
            .await?
            .into_iter()
            .map(|(id, _)| id)
            .collect();
        // `HEAD` is read as plain data: a detached commit id is a tip, a
        // symbolic ref names a branch that is either in the namespace or not
        // the subagent's to move.
        let head_path = self.git_dir.join("HEAD");
        match async_fs::read_to_string(&head_path).await {
            Ok(head) => tips.extend(object_id(head.trim()).map(str::to_string)),
            Err(error) if error.kind() == io::ErrorKind::NotFound => {}
            Err(source) => {
                return Err(CommitLedgerError::Read {
                    path: head_path,
                    source,
                });
            }
        }
        tips.sort_unstable();
        tips.dedup();
        Ok(tips)
    }

    /// The commits reachable from `now` and not from `before`, oldest first.
    async fn commits_between(
        &self,
        before: &[String],
        now: &[String],
    ) -> Result<Vec<SubagentCommit>, CommitLedgerError> {
        if now.is_empty() {
            return Ok(Vec::new());
        }
        let mut args = vec![
            "log",
            "--no-show-signature",
            "--no-decorate",
            "--no-color",
            "--reverse",
            "--format=%H%x00%s",
        ];
        args.extend(now.iter().map(String::as_str));
        args.push("--not");
        args.extend(before.iter().map(String::as_str));
        let output = host_git::git(&self.common_dir, &args).await?;
        output
            .lines()
            .map(|line| {
                line.split_once('\0')
                    .and_then(|(id, subject)| {
                        Some(SubagentCommit {
                            worktree: self.worktree.clone(),
                            commit: object_id(id)?.to_string(),
                            subject: subject.to_string(),
                        })
                    })
                    .ok_or_else(|| CommitLedgerError::Output {
                        repository: self.common_dir.clone(),
                        line: line.to_string(),
                    })
            })
            .collect()
    }
}

/// `text` when it is a full hexadecimal object id, SHA-1 or SHA-256.
fn object_id(text: &str) -> Option<&str> {
    (matches!(text.len(), 40 | 64) && text.bytes().all(|byte| byte.is_ascii_hexdigit()))
        .then_some(text)
}

/// The git data a linked worktree's `.git` file names.
struct WorktreeGit {
    /// The per-worktree git directory.
    git_dir: PathBuf,
    /// The repository's shared git directory.
    common_dir: PathBuf,
    /// Whether the `.git` file names the worktree's own repository inside
    /// that directory, as a commit grant leaves it, rather than the directory
    /// itself.
    converted: bool,
}

/// `path` resolved to its canonical form, without the Windows verbatim
/// (`\\?\`) prefix, which git and other tools given these paths cannot read.
pub async fn canonical(path: &Path) -> Result<PathBuf, SubagentGrantError> {
    async_fs::canonicalize(path)
        .await
        .map(|canonical| dunce::simplified(&canonical).to_path_buf())
        .map_err(|source| SubagentGrantError::Inspect {
            path: path.to_path_buf(),
            source,
        })
}

async fn read_file(path: &Path) -> Result<String, SubagentGrantError> {
    async_fs::read_to_string(path)
        .await
        .map_err(|source| SubagentGrantError::Inspect {
            path: path.to_path_buf(),
            source,
        })
}

/// The most bytes of a worktree's `.git` file the host reads: a `gitdir:` line
/// holds one path.
const DOT_GIT_LIMIT: u64 = 16 * 1024;

/// What a worktree's `.git` is.
enum DotGit {
    Missing,
    Directory,
    /// A regular file and what it holds.
    File(String),
}

/// Reads the worktree's `.git`, which a subagent can write, without ever
/// following a symlink in its place, waiting on a FIFO, or reading more than
/// [`DOT_GIT_LIMIT`] bytes.
async fn read_dot_git(dot_git: &Path) -> io::Result<DotGit> {
    use std::io::Read as _;

    let path = dot_git.to_path_buf();
    blocking::unblock(move || {
        let mut options = std::fs::OpenOptions::new();
        options.read(true);
        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt as _;
            options.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK);
        }
        let file = match options.open(&path) {
            Ok(file) => file,
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(DotGit::Missing),
            Err(error) => return Err(error),
        };
        let metadata = file.metadata()?;
        if metadata.is_dir() {
            return Ok(DotGit::Directory);
        }
        if !metadata.is_file() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "not a regular file",
            ));
        }
        let mut content = Vec::new();
        file.take(DOT_GIT_LIMIT + 1).read_to_end(&mut content)?;
        if u64::try_from(content.len()).map_or(true, |len| len > DOT_GIT_LIMIT) {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "longer than a gitdir line can be",
            ));
        }
        String::from_utf8(content)
            .map(DotGit::File)
            .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))
    })
    .await
}

/// `path` resolved like [`canonical`], except that its last component may be
/// missing: the repository of a worktree that was deleted, to be created
/// again.
async fn canonical_with_missing_leaf(path: &Path) -> Result<PathBuf, SubagentGrantError> {
    match canonical(path).await {
        Err(SubagentGrantError::Inspect { source, .. })
            if source.kind() == io::ErrorKind::NotFound =>
        {
            match (path.parent(), path.file_name()) {
                (Some(parent), Some(name)) => Ok(canonical(parent).await?.join(name)),
                _ => canonical(path).await,
            }
        }
        other => other,
    }
}

/// Reads the git data the `.git` file of `worktree` names.
///
/// `None` when `worktree` is not a git working tree at all; an error when it is
/// a repository checkout rather than a linked worktree.
async fn read_worktree_git(worktree: &Path) -> Result<Option<WorktreeGit>, SubagentGrantError> {
    let dot_git = worktree.join(".git");
    let content = match read_dot_git(&dot_git).await {
        Ok(DotGit::Missing) => return Ok(None),
        Ok(DotGit::Directory) => {
            return Err(SubagentGrantError::RepositoryCheckout(
                worktree.to_path_buf(),
            ));
        }
        Ok(DotGit::File(content)) => content,
        Err(source) => {
            return Err(SubagentGrantError::Inspect {
                path: dot_git,
                source,
            });
        }
    };
    let malformed = |reason| SubagentGrantError::MalformedGitData {
        path: worktree.to_path_buf(),
        reason,
    };
    let git_dir = content
        .trim()
        .strip_prefix("gitdir:")
        .map(str::trim)
        .ok_or_else(|| malformed("`.git` has no `gitdir:` line"))?;
    let named = canonical_with_missing_leaf(&worktree.join(git_dir)).await?;
    // A worktree granted commit names its own repository, which sits inside
    // its entry in the main repository and has no `commondir` of its own.
    let (git_dir, converted) = match async_fs::metadata(named.join("commondir")).await {
        Ok(_) => (named, false),
        Err(error) if error.kind() == io::ErrorKind::NotFound => {
            match named
                .parent()
                .filter(|_| named.file_name() == Some(repository::REPOSITORY_DIR.as_ref()))
            {
                Some(entry) => (entry.to_path_buf(), true),
                None => return Err(malformed("the git directory has no `commondir`")),
            }
        }
        Err(source) => {
            return Err(SubagentGrantError::Inspect {
                path: named.join("commondir"),
                source,
            });
        }
    };
    let common_dir = match async_fs::read_to_string(git_dir.join("commondir")).await {
        Ok(relative) => canonical(&git_dir.join(relative.trim())).await?,
        Err(error) if error.kind() == io::ErrorKind::NotFound => {
            return Err(malformed("the git directory has no `commondir`"));
        }
        Err(source) => {
            return Err(SubagentGrantError::Inspect {
                path: git_dir.join("commondir"),
                source,
            });
        }
    };
    Ok(Some(WorktreeGit {
        git_dir,
        common_dir,
        converted,
    }))
}

impl CommittingWorktree {
    /// The committing worktree `launcher` may hand on at `path`: one it holds
    /// itself when it is a subagent; for the main agent, the one granted
    /// before or, the first time, the one read from the repository.
    async fn granted(
        path: &Path,
        git: Option<WorktreeGit>,
        launcher: Launcher<'_>,
        history: &LaunchHistory,
        granted: &[PathBuf],
        plain: &[PathBuf],
    ) -> Result<Self, SubagentGrantError> {
        if let Some(held) = launcher.committing {
            return held
                .iter()
                .find(|held| held.worktree == path)
                .cloned()
                .ok_or_else(|| SubagentGrantError::CommitOutsideAuthority(path.to_path_buf()));
        }
        if let Some(held) = history.committed.iter().find(|held| held.worktree == path) {
            return Ok(held.clone());
        }
        Self::discover(
            path,
            git,
            history
                .handed_out
                .iter()
                .chain(granted)
                .map(PathBuf::as_path)
                .chain([launcher.spaces_root]),
            history
                .handed_out
                .iter()
                .chain(plain)
                .map(PathBuf::as_path)
                .chain([launcher.spaces_root]),
        )
        .await
    }
}

impl SubagentGrant {
    /// Resolves the grant against what `launcher` may hand on.
    ///
    /// Every path the subagent will be able to write, git data included, must
    /// lie inside what the launcher can write; a subagent launcher hands on
    /// only the commit grants it holds itself. The main agent's grants are
    /// recorded in `history`.
    #[allow(clippy::too_many_lines)]
    pub(crate) async fn resolve(
        &self,
        launcher: Launcher<'_>,
        history: &mut LaunchHistory,
    ) -> Result<ResolvedGrant, SubagentGrantError> {
        let mut paths = Vec::with_capacity(self.worktrees.len());
        for grant in &self.worktrees {
            let path = canonical(&grant.path).await?;
            if !inside_any(&path, launcher.writable) {
                return Err(SubagentGrantError::OutsideAuthority(path));
            }
            paths.push((path, grant.commit));
        }
        let granted: Vec<PathBuf> = paths.iter().map(|(path, _)| path.clone()).collect();
        let plain: Vec<PathBuf> = paths
            .iter()
            .filter(|(_, commit)| !commit)
            .map(|(path, _)| path.clone())
            .collect();

        let mut writable = Vec::new();
        let mut committing: Vec<CommittingWorktree> = Vec::new();
        for (path, commit) in &paths {
            // A worktree already granted commit has its git data resolved;
            // its `.git`, which a subagent may have replaced, is not read
            // again.
            let known = commit
                .then(|| {
                    launcher
                        .committing
                        .unwrap_or(&history.committed)
                        .iter()
                        .find(|held| held.worktree == *path)
                })
                .flatten();
            let git = if known.is_some() {
                None
            } else {
                read_worktree_git(path).await?
            };
            // A worktree's git data inside another granted path would be
            // writable whatever the commit grant says.
            let data = known
                .map(|held| (&held.git_dir, &held.common_dir))
                .or_else(|| git.as_ref().map(|git| (&git.git_dir, &git.common_dir)));
            if let Some((git_dir, common_dir)) = data
                && let Some(exposing) = granted
                    .iter()
                    .find(|granted| overlaps(granted, git_dir) || overlaps(granted, common_dir))
            {
                return Err(SubagentGrantError::GitDataExposed {
                    worktree: path.clone(),
                    git_data: common_dir.clone(),
                    writable: exposing.clone(),
                });
            }
            if !commit {
                // A plain write grant never reaches into git data some
                // subagent's commits are listed from.
                if let Some(held) = history
                    .committed
                    .iter()
                    .find(|held| overlaps(path, &held.common_dir) || overlaps(path, &held.git_dir))
                {
                    return Err(SubagentGrantError::GitDataExposed {
                        worktree: held.worktree.clone(),
                        git_data: held.common_dir.clone(),
                        writable: path.clone(),
                    });
                }
                // Nor does it contain a committing worktree, which a subagent
                // could then replace under the host's `.git` file.
                if let Some(held) = history
                    .committed
                    .iter()
                    .find(|held| held.worktree.starts_with(path))
                {
                    return Err(SubagentGrantError::WorktreeExposed {
                        worktree: held.worktree.clone(),
                        writable: path.clone(),
                    });
                }
                writable.push(path.clone());
                continue;
            }

            if launcher.committing.is_none() {
                host_git::require_supported_version().await?;
            }
            let worktree =
                CommittingWorktree::granted(path, git, launcher, history, &granted, &plain).await?;
            if let Some(part) = worktree
                .writable()
                .into_iter()
                .find(|part| !inside_any(part, launcher.writable))
            {
                return Err(SubagentGrantError::OutsideAuthority(part));
            }
            if let Some(shared) = committing.iter().find(|other| {
                other.common_dir == worktree.common_dir && other.namespace == worktree.namespace
            }) {
                return Err(SubagentGrantError::SharedNamespace {
                    worktree: worktree.worktree.clone(),
                    namespace: worktree.ref_prefix(),
                    other: shared.worktree.display().to_string(),
                });
            }
            // Off Unix the terminal runs no `sh`, so nothing guards a
            // committing worktree's git data — refused only once every real
            // refusal had its say.
            if cfg!(unix) {
                if launcher.committing.is_none() {
                    worktree.prepare(launcher.spelling).await?;
                }
                writable.push(path.clone());
                committing.push(worktree);
            } else {
                return Err(SubagentGrantError::CommitGrantUnsupported);
            }
        }

        if launcher.committing.is_none() {
            history.handed_out.extend(
                paths
                    .iter()
                    .filter(|(_, commit)| !commit)
                    .map(|(path, _)| path.clone()),
            );
            for worktree in &committing {
                if !history.committed.contains(worktree) {
                    history.committed.push(worktree.clone());
                }
            }
        }
        writable.extend(committing.iter().flat_map(CommittingWorktree::writable));
        Ok(ResolvedGrant {
            writable,
            committing,
        })
    }
}

/// A commit a subagent made.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubagentCommit {
    /// The worktree it was made in.
    pub worktree: PathBuf,
    /// The commit id.
    pub commit: String,
    /// The first line of the commit message.
    pub subject: String,
}

/// Why the commits of a subagent could not be listed.
#[derive(Debug, thiserror::Error)]
pub enum CommitLedgerError {
    /// `git` could not be run.
    #[error("cannot run git on {}: {source}", .repository.display())]
    Spawn {
        /// The repository.
        repository: PathBuf,
        /// The failure.
        source: io::Error,
    },
    /// `git` failed.
    #[error("git failed on {}: {stderr}", .repository.display())]
    Git {
        /// The repository.
        repository: PathBuf,
        /// What git printed.
        stderr: String,
    },
    /// `git` printed a line that is not what the query asked for.
    #[error("unexpected git output on {}: {line:?}", .repository.display())]
    Output {
        /// The repository.
        repository: PathBuf,
        /// The line.
        line: String,
    },
    /// A worktree's `HEAD` could not be read.
    #[error("cannot read {}: {source}", .path.display())]
    Read {
        /// The file.
        path: PathBuf,
        /// The failure.
        source: io::Error,
    },
    /// A subagent's repository reported a ref or object id that is malformed
    /// or lies outside the subagent's branch namespace.
    #[error("the subagent repository {} reported {line:?}, which is not a ref of its namespace. {}", .repository.display(), unimported(.repository))]
    Report {
        /// The subagent's repository.
        repository: PathBuf,
        /// What it reported.
        line: String,
    },
    /// Git could not run in the subagent's sandbox, failed there, or did not
    /// finish in time.
    #[error("cannot export from the subagent repository {}: {source}. {}", .repository.display(), unimported(.repository))]
    Export {
        /// The subagent's repository.
        repository: PathBuf,
        /// Why.
        source: ExportError,
    },
    /// The pack the subagent's sandbox sent was refused: an object that is
    /// not what it claims to be, a broken link, or more than the import
    /// allows.
    #[error("the pack from the subagent repository {} was refused: {stderr}. {}", .repository.display(), unimported(.repository))]
    Rejected {
        /// The subagent's repository.
        repository: PathBuf,
        /// What git said.
        stderr: String,
    },
    /// A tip the subagent reported is missing after indexing, is no commit,
    /// or reaches objects that are not there.
    #[error("the subagent repository {} reported commits that did not arrive whole: {detail}. {}", .repository.display(), unimported(.repository))]
    Incomplete {
        /// The subagent's repository.
        repository: PathBuf,
        /// The object or the git message that says what is missing.
        detail: String,
    },
    /// A ref of the main repository moved while the import was under way.
    #[error("`{reference}` moved in the main repository while the commits of {} were imported", .repository.display())]
    Moved {
        /// The subagent's repository.
        repository: PathBuf,
        /// The ref.
        reference: String,
    },
    /// The directory a pack is indexed in could not be set up, or the indexed
    /// pack could not be moved into the main store.
    #[error("cannot use {} to import a pack: {source}", .path.display())]
    Quarantine {
        /// The path.
        path: PathBuf,
        /// The failure.
        source: io::Error,
    },
}

/// What becomes of commits a repository holds that could not be imported.
fn unimported(repository: &Path) -> String {
    format!(
        "Its commits that were not imported stay in {}; deleting that directory discards them, and granting the worktree again gives it a fresh repository",
        repository.display()
    )
}

/// Where a ledger's worktrees stood at one moment: the commits their branch
/// namespaces and detached heads pointed at.
#[derive(Debug, Clone)]
pub struct CommitMark {
    tips: Vec<Vec<String>>,
    setbacks: Arc<[CommitLedgerError]>,
}

impl CommitMark {
    /// What could not be done to the subagent's repositories before the run.
    ///
    /// A repository the host cannot refresh or import never keeps a
    /// subagent from running; it keeps its commits in itself, out of the main
    /// repository, and the run is told here. The commits that failed to
    /// import are listed with the next run's when a later import succeeds.
    #[must_use]
    pub fn setbacks(&self) -> &[CommitLedgerError] {
        &self.setbacks
    }
}

/// Lists the commits a subagent holding a commit grant made, and brings them
/// into the main repository.
///
/// The main repository's refs, and so the listing, change only in the import
/// that [`Self::since`] runs; it reads nothing a subagent wrote, and the
/// subagent's own git runs only inside the subagent's sandbox.
#[derive(Debug, Clone)]
pub struct CommitLedger {
    worktrees: Vec<CommittingWorktree>,
    git: SubagentGit,
    limits: ImportLimits,
}

impl CommitLedger {
    /// The ledger of `worktrees`; `None` when there are none.
    pub(crate) fn new(
        worktrees: &[CommittingWorktree],
        git: SubagentGit,
        limits: ImportLimits,
    ) -> Option<Self> {
        (!worktrees.is_empty()).then(|| Self {
            worktrees: worktrees.to_vec(),
            git,
            limits,
        })
    }

    /// Starts a run: refreshes each worktree's view of the main repository's
    /// refs inside the subagent's sandbox, imports what its repository
    /// already holds, then records where every worktree stands. A branch not
    /// yet born simply has no tip.
    ///
    /// What the subagent's repository does or holds never keeps a run from
    /// starting. A repository that cannot be refreshed or imported (a stalled
    /// or refused one) is left as it is, the run starts from what the main
    /// repository holds, and the failure is in [`CommitMark::setbacks`] for the
    /// run's report; its commits stay in that repository, which the error
    /// names. The mark's tips are always the main repository's, so a later
    /// import that succeeds lists the commits an earlier holder left too,
    /// never dropping them.
    ///
    /// # Errors
    /// Fails when git cannot list the main repository's refs.
    pub async fn mark(&self) -> Result<CommitMark, CommitLedgerError> {
        // Every worktree is carried through before an error is looked at, so
        // none is dropped partway.
        let prepared = join_all(self.worktrees.iter().map(|worktree| async move {
            refresh(&self.git, worktree, &self.limits).await?;
            // Whatever an earlier holder of the repository left is imported
            // now, so it counts as before this run, not as made in it.
            worktree.import(&self.git, &self.limits).await
        }))
        .await;
        let setbacks: Vec<CommitLedgerError> =
            prepared.into_iter().filter_map(Result::err).collect();
        for setback in &setbacks {
            tracing::warn!(%setback, "a subagent repository could not be prepared for a run");
        }
        let tips = join_all(self.worktrees.iter().map(CommittingWorktree::tips))
            .await
            .into_iter()
            .collect::<Result<_, _>>()?;
        Ok(CommitMark {
            tips,
            setbacks: setbacks.into(),
        })
    }

    /// The commits made since `mark`, oldest first per worktree: everything
    /// the worktree's branch namespace or detached head reaches now and did
    /// not reach then, whatever the subagent did with `HEAD` in between.
    ///
    /// The subagent's commits are imported into the main repository first.
    /// Every worktree's import runs to its end before an error is returned.
    ///
    /// # Errors
    /// Fails when a subagent's commits cannot be imported or git cannot list
    /// a worktree's refs or commits.
    pub async fn since(&self, mark: &CommitMark) -> Result<Vec<SubagentCommit>, CommitLedgerError> {
        let listed = join_all(self.worktrees.iter().zip(&mark.tips).map(
            |(worktree, before)| async move {
                worktree.import(&self.git, &self.limits).await?;
                let now = worktree.tips().await?;
                worktree.commits_between(before, &now).await
            },
        ))
        .await;
        let mut commits = Vec::new();
        for listed in listed {
            commits.extend(listed?);
        }
        Ok(commits)
    }
}

/// Refreshes the subagent repository's snapshot of the main repository's refs
/// with a fetch that runs in the subagent's sandbox, so whatever it does stays
/// inside what the subagent can write.
async fn refresh(
    git: &SubagentGit,
    worktree: &CommittingWorktree,
    limits: &ImportLimits,
) -> Result<(), CommitLedgerError> {
    git.succeed(
        &worktree.repository,
        &[
            "fetch",
            "--quiet",
            "--prune",
            "--no-write-fetch-head",
            "--no-recurse-submodules",
            repository::MAIN_REMOTE,
        ],
        limits.bounds(),
    )
    .await
    .map(drop)
    .map_err(|source| CommitLedgerError::Export {
        repository: worktree.repository.clone(),
        source,
    })
}

/// The null device, which stands in for the global git configuration.
#[cfg(windows)]
const NULL_DEVICE: &str = "NUL";
#[cfg(not(windows))]
const NULL_DEVICE: &str = "/dev/null";

#[cfg(test)]
pub mod tests;