devsandbox 0.9.0

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
//! Git worktrees for repeat instances of one repo, so working trees are never
//! shared.

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

use anyhow::{bail, Context, Result};

use super::git::{check_repo, host_git};

/// Bind mount for the base repo's `.git` at the identical host path, so a
/// worktree's absolute `gitdir` pointer resolves inside the container.
pub(super) fn git_companion_mount(base: &Path) -> String {
    let git = base.join(".git");
    format!("{}:{}", git.display(), git.display())
}

/// Where a worktree's branch comes from, decided by [`branch_source`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BranchSource {
    /// `refs/heads/<branch>` exists: check it out as is.
    Local,
    /// Only `refs/remotes/origin/<branch>` exists (e.g. a PR head): create a
    /// local branch tracking it, so a plain `git push` updates the remote.
    Remote,
    /// Neither: a fresh branch off the start point.
    New,
}

/// A local branch wins over the remote one (it may hold unpushed work).
fn branch_source(local: bool, remote: bool) -> BranchSource {
    match (local, remote) {
        (true, _) => BranchSource::Local,
        (false, true) => BranchSource::Remote,
        (false, false) => BranchSource::New,
    }
}

/// Create a git worktree on `branch`, reusing it when it exists (see
/// [`BranchSource`]). Returns whether the branch was created (`rm` only offers
/// to delete those). Git refuses a branch checked out in another worktree or
/// the base checkout; that is reported with where it is checked out, and which
/// instance owns it when `state` knows.
///
/// A new branch starts from `base_ref` when given, else the remote's default
/// branch (see [`worktree_start_point`]), not the base repo's HEAD: another
/// agent may be mid-work on a feature branch in the base checkout.
///
/// `explicit_branch` (the user or a dispatcher named the branch) gates a
/// targeted `git fetch` of that one branch when the regular fetch didn't bring
/// it: pattern-generated names (`sandbox/${instance}`) are new by
/// construction, so they never pay for the extra round-trip.
pub(super) fn create_worktree(
    base: &Path,
    worktree: &Path,
    branch: &str,
    base_ref: Option<&str>,
    explicit_branch: bool,
    state: &crate::state::State,
) -> Result<bool> {
    if let Some(parent) = worktree.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("cannot create {}", parent.display()))?;
    }
    let has_ref = |r: &str| git_query(base, &["show-ref", "--verify", "--quiet", r]).is_some();
    let local_ref = format!("refs/heads/{branch}");
    let remote_ref = format!("refs/remotes/origin/{branch}");
    let wt = worktree.to_string_lossy();
    // An existing local branch needs no start point, so no fetch either.
    let source = if has_ref(&local_ref) {
        BranchSource::Local
    } else {
        // Refreshes `origin` (throttled) before the remote-tracking check too.
        let start = worktree_start_point(base, base_ref)?;
        if !has_ref(&remote_ref) && explicit_branch {
            fetch_origin_branch(base, branch);
        }
        match branch_source(false, has_ref(&remote_ref)) {
            BranchSource::New => {
                create_new_branch_worktree(base, worktree, branch, base_ref, &start)?;
                return Ok(true);
            }
            other => other,
        }
    };
    if let Some(r) = base_ref {
        eprintln!("note: `{branch}` already exists; ignoring worktree base `{r}`");
    }
    if source == BranchSource::Local {
        if let Some(at) = checked_out_at(base, branch) {
            let owner = instance_at(state, &at)
                .map(|i| format!(" (instance `{i}`)"))
                .unwrap_or_default();
            bail!(
                "branch `{branch}` is already checked out at `{}`{owner}; git allows a branch \
                 in only one worktree: switch that checkout to another branch, or pick another `--branch`",
                at.display()
            );
        }
    }
    let upstream = format!("origin/{branch}");
    let mut add = host_git(base)?;
    add.args(["worktree", "add"]);
    match source {
        BranchSource::Local => add.args([&*wt, branch]),
        _ => add.args(["--track", "-b", branch, &*wt, upstream.as_str()]),
    };
    let out = add.output().context("failed to run git (is it installed?)")?;
    if !out.status.success() {
        bail!(
            "git worktree add failed for `{}`: {}",
            worktree.display(),
            String::from_utf8_lossy(&out.stderr).trim()
        );
    }
    ensure_populated(worktree)?;
    lock_worktree(base, worktree);
    Ok(false)
}

/// Today's path for a branch that exists nowhere: `-b <branch> --no-track`
/// off `start`, after checking `start` is a commit.
fn create_new_branch_worktree(
    base: &Path,
    worktree: &Path,
    branch: &str,
    base_ref: Option<&str>,
    start: &str,
) -> Result<()> {
    // With an unborn HEAD, `git worktree add -b` infers `--orphan`, exits 0, and
    // lays down a worktree with no files; guard against handing that empty path
    // to the container as a bind mount.
    let head = host_git(base)?
        .args([
            "rev-parse",
            "--verify",
            &format!("{start}^{{commit}}"),
        ])
        .output()
        .context("failed to run git (is it installed?)")?;
    if !head.status.success() {
        // An explicit ref is the user's choice: never silently swap it out.
        if let Some(r) = base_ref {
            bail!("worktree base `{r}` is not a commit in `{}`", base.display());
        }
        bail!(
            "base repo `{}` has no commits; cannot create a worktree",
            base.display()
        );
    }
    let status = host_git(base)?
        .args([
            "worktree",
            "add",
            &worktree.to_string_lossy(),
            "-b",
            branch,
            // Starting from `origin/<default>` would otherwise set it as the
            // upstream, aiming a bare `git push` at main.
            "--no-track",
            start,
        ])
        .status()
        .context("failed to run git (is it installed?)")?;
    if !status.success() {
        bail!("git worktree add failed for `{}`", worktree.display());
    }
    ensure_populated(worktree)?;
    lock_worktree(base, worktree);
    Ok(())
}

/// Worktree of `base` for a `folders` entry: detached at the base checkout's
/// current `HEAD`, so no branch is created (nothing for `rm` to delete, no
/// "already checked out" clash) and it starts where the owner's checkout is.
pub(super) fn create_detached_worktree(base: &Path, worktree: &Path) -> Result<()> {
    if let Some(parent) = worktree.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("cannot create {}", parent.display()))?;
    }
    // An unborn HEAD has nothing to detach at.
    let head = host_git(base)?
        .args(["rev-parse", "--verify", "HEAD^{commit}"])
        .output()
        .context("failed to run git (is it installed?)")?;
    if !head.status.success() {
        bail!(
            "base repo `{}` has no commits; cannot create a worktree",
            base.display()
        );
    }
    let out = host_git(base)?
        .args(["worktree", "add", "--detach", &worktree.to_string_lossy(), "HEAD"])
        .output()
        .context("failed to run git (is it installed?)")?;
    if !out.status.success() {
        bail!(
            "git worktree add failed for `{}`: {}",
            worktree.display(),
            String::from_utf8_lossy(&out.stderr).trim()
        );
    }
    ensure_populated(worktree)?;
    lock_worktree(base, worktree);
    Ok(())
}

/// A real worktree always has a `.git` entry pointing back at the base repo;
/// its absence means git exited 0 without checking anything out.
fn ensure_populated(worktree: &Path) -> Result<()> {
    if !worktree.join(".git").exists() {
        bail!(
            "git worktree add produced an empty worktree at `{}`",
            worktree.display()
        );
    }
    Ok(())
}

const LOCK_REASON: &str = "devsandbox: checked out on the host; a container that mounts this repo's \
                           .git sees it as missing, never prune it from there";

/// Lock a worktree devsandbox owns, so `git worktree prune` (and `gc`) keep
/// its record. A container gets the base's whole `.git` but none of the other
/// instances' checkouts: from inside, every other worktree looks missing, and
/// an unlocked prune there deletes their admin dirs, breaking git in each.
/// Called on create and again on every `start`/`run`/`rebuild` (no-op once
/// locked), so instances from before locking get it lazily. Best effort: a
/// missing lock must not stop a sandbox from starting.
pub(crate) fn lock_worktree(base: &Path, worktree: &Path) {
    // Not a linked worktree (yet): nothing git could prune.
    let Some(admin) = admin_dir(worktree) else {
        return;
    };
    if !admin.is_dir() {
        eprintln!(
            "warning: git's record of worktree `{}` is gone ({} was pruned); git won't work in it \
             until the record is restored",
            worktree.display(),
            admin.display()
        );
        return;
    }
    if admin.join("locked").exists() {
        return;
    }
    let out = host_git(base).and_then(|mut git| {
        git.args(["worktree", "lock", "--reason", LOCK_REASON])
            .arg(worktree)
            .output()
            .context("failed to run git (is it installed?)")
    });
    let why = match out {
        Ok(out) if out.status.success() => return,
        Ok(out) => String::from_utf8_lossy(&out.stderr).trim().to_string(),
        Err(e) => format!("{e:#}"),
    };
    eprintln!("warning: cannot lock worktree `{}`: {why}", worktree.display());
}

/// [`lock_worktree`] for every worktree `info` records: the primary and its
/// `folders` ones.
pub(crate) fn lock_instance_worktrees(info: &crate::state::Instance) {
    if let Some(wt) = &info.worktree {
        lock_worktree(&info.base_folder, wt);
    }
    for fm in &info.folders {
        if let Some(wt) = &fm.worktree {
            lock_worktree(&fm.base, wt);
        }
    }
}

/// Undo [`lock_worktree`] before removing or re-adding a worktree: `worktree
/// remove` refuses a locked one (short of a double `--force`), `prune` skips
/// its record, and `add` won't reuse a locked missing path. With the checkout
/// gone its `.git` pointer can't be read, so git is asked anyway; "not
/// locked" and "not a worktree" failures are expected and ignored.
pub(crate) fn unlock_worktree(base: &Path, worktree: &Path) {
    if admin_dir(worktree).is_some_and(|admin| !admin.join("locked").exists()) {
        return;
    }
    if let Ok(mut git) = host_git(base) {
        let _ = git.args(["worktree", "unlock"]).arg(worktree).output();
    }
}

/// The admin dir a linked worktree's `.git` file points at (relative pointers
/// resolve against the worktree, as git does); `None` when there is no such
/// file.
fn admin_dir(worktree: &Path) -> Option<PathBuf> {
    let text = std::fs::read_to_string(worktree.join(".git")).ok()?;
    let gitdir = text.trim().strip_prefix("gitdir:")?.trim();
    Some(worktree.join(gitdir))
}

/// Best-effort, quiet fetch of one branch into `origin/<branch>`. The explicit
/// refspec also covers single-branch clones, whose configured refspec would
/// leave the remote-tracking ref alone. `--no-write-fetch-head` keeps this
/// narrow fetch from marking the full one fresh (see [`fetch_origin`]). A
/// branch missing on the remote (or no `origin`, or offline) just fails.
fn fetch_origin_branch(base: &Path, branch: &str) {
    if git_query(base, &["remote", "get-url", "origin"]).is_none() {
        return;
    }
    let refspec = format!("+refs/heads/{branch}:refs/remotes/origin/{branch}");
    if let Ok(mut git) = host_git(base) {
        let _ = git
            .args(["fetch", "--quiet", "--no-write-fetch-head", "origin", &refspec])
            .output();
    }
}

/// Where `branch` is checked out among `base`'s worktrees (the base checkout
/// included), if anywhere.
fn checked_out_at(base: &Path, branch: &str) -> Option<PathBuf> {
    let list = git_query(base, &["worktree", "list", "--porcelain"])?;
    parse_worktree_list(&list)
        .into_iter()
        .find(|w| w.branch.as_deref() == Some(branch))
        .map(|w| w.path)
}

/// One record of `git worktree list --porcelain`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct WorktreeEntry {
    pub path: PathBuf,
    /// The checked-out branch, `refs/heads/` stripped; `None` when detached
    /// (or bare).
    pub branch: Option<String>,
    /// Its directory is gone (`git worktree prune` would drop it).
    pub prunable: bool,
}

/// Parse `git worktree list --porcelain`: blank-line-separated records, each
/// `worktree <path>` then e.g. `branch refs/heads/<name>`, `detached`,
/// `prunable [reason]`. Git lists the main worktree first; order is kept.
pub(crate) fn parse_worktree_list(porcelain: &str) -> Vec<WorktreeEntry> {
    porcelain
        .split("\n\n")
        .filter_map(|record| {
            let (mut path, mut branch, mut prunable) = (None, None, false);
            for line in record.lines() {
                if let Some(p) = line.strip_prefix("worktree ") {
                    path = Some(PathBuf::from(p));
                } else if let Some(b) = line.strip_prefix("branch ") {
                    branch = b.strip_prefix("refs/heads/").map(str::to_string);
                } else if line == "prunable" || line.starts_with("prunable ") {
                    prunable = true;
                }
            }
            Some(WorktreeEntry { path: path?, branch, prunable })
        })
        .collect()
}

/// The instance whose working tree is `path` (a worktree, or a base-folder
/// instance's checkout). Paths compare canonicalized: git prints real paths.
pub(crate) fn instance_at(state: &crate::state::State, path: &Path) -> Option<String> {
    let canon = |p: &Path| p.canonicalize().unwrap_or_else(|_| p.to_path_buf());
    let want = canon(path);
    state
        .instances
        .iter()
        .find(|(_, i)| canon(&i.folder) == want)
        .map(|(k, _)| k.clone())
}

/// Seed a fresh worktree with the base repo's gitignored files listed in its
/// `.worktreeinclude` (docs/architecture/instances/worktrees.md): a checkout
/// has no untracked files, so `.env` and friends would otherwise be missing.
/// Git does the matching (include patterns, then gitignore), so semantics
/// equal the other tools reading the same file. `patterns` (the sandbox's
/// `worktree-include`) apply after the file, so they can negate it. Best
/// effort: problems warn, never fail `run`.
pub(super) fn copy_worktree_includes(base: &Path, worktree: &Path, patterns: &[String]) {
    let include = base.join(".worktreeinclude");
    let include = include.is_file().then_some(include);
    if include.is_none() && patterns.is_empty() {
        return;
    }
    let paths = match worktree_include_paths(base, include.as_deref(), patterns) {
        Ok(paths) => paths,
        Err(e) => {
            eprintln!("warning: cannot apply worktree includes: {e:#}");
            return;
        }
    };
    for rel in paths {
        if let Err(e) = copy_entry(&base.join(&rel), &worktree.join(&rel)) {
            eprintln!("warning: cannot copy `{rel}` into worktree: {e:#}");
        }
    }
}

/// Untracked paths (relative to `base`) matching the include patterns that git
/// also ignores. `ls-files` given only these exclude sources ignores
/// `.gitignore`, so negation behaves as written (command-line `--exclude`
/// outranks `--exclude-from`); `check-ignore` then drops anything not
/// gitignored, which also keeps files under an ignored directory.
fn worktree_include_paths(
    base: &Path,
    include: Option<&Path>,
    patterns: &[String],
) -> Result<Vec<String>> {
    let mut ls = host_git(base)?;
    ls.args(["ls-files", "-z", "--others", "--ignored"]);
    if let Some(include) = include {
        ls.arg(format!("--exclude-from={}", include.display()));
    }
    for p in patterns {
        ls.arg(format!("--exclude={p}"));
    }
    let listed = ls.output().context("failed to run git (is it installed?)")?;
    if !listed.status.success() {
        bail!("git ls-files failed: {}", String::from_utf8_lossy(&listed.stderr).trim());
    }
    if listed.stdout.is_empty() {
        return Ok(Vec::new());
    }
    let mut child = host_git(base)?
        .args(["check-ignore", "-z", "--stdin"])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .context("failed to run git (is it installed?)")?;
    // Write on a thread: a large list could fill the stdout pipe while we
    // are still feeding stdin.
    let mut stdin = child.stdin.take().expect("stdin is piped");
    let input = listed.stdout;
    let writer = std::thread::spawn(move || std::io::Write::write_all(&mut stdin, &input));
    let out = child.wait_with_output().context("git check-ignore failed")?;
    writer.join().expect("writer thread panicked").context("git check-ignore failed")?;
    // Exit 1 means "nothing ignored", not an error.
    if !out.status.success() && out.status.code() != Some(1) {
        bail!("git check-ignore failed: {}", String::from_utf8_lossy(&out.stderr).trim());
    }
    Ok(out
        .stdout
        .split(|b| *b == 0)
        .filter(|p| !p.is_empty())
        .map(|p| String::from_utf8_lossy(p).into_owned())
        .collect())
}

/// Copy one file or symlink, never overwriting: an existing destination
/// (dangling symlinks included) is left alone.
fn copy_entry(src: &Path, dst: &Path) -> Result<()> {
    if dst.symlink_metadata().is_ok() {
        return Ok(());
    }
    if let Some(parent) = dst.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let meta = src.symlink_metadata()?;
    #[cfg(unix)]
    if meta.file_type().is_symlink() {
        std::os::unix::fs::symlink(std::fs::read_link(src)?, dst)?;
        return Ok(());
    }
    if meta.is_file() {
        std::fs::copy(src, dst)?;
    }
    Ok(())
}

/// Share `entries` (repo-relative, gitignored paths) live across every
/// instance of a sandbox (docs/architecture/instances/worktrees.md): the real
/// file lives in `store`, each of `trees` (base repo first, then the worktree
/// if any) gets an absolute symlink to it, and the caller mounts `store` at
/// its host path so the links resolve in the container. Idempotent (runs on
/// every `run` and `rebuild`). A bad entry is a config error; anything on
/// disk that doesn't fit (tracked path, conflicting file) only warns and is
/// left untouched.
///
/// `store` sits in a directory of stores (`shared-files/`); a link into a
/// sibling store is left over from the old one-store-per-sandbox layout. It is
/// not migrated, only reported with how to move it by hand (see
/// [`old_store_link`]).
pub(super) fn link_shared_files(store: &Path, trees: &[&Path], entries: &[String]) -> Result<()> {
    let Some((base, _)) = trees.split_first() else { return Ok(()) };
    let stores = store.parent().unwrap_or(store);
    // Every check below would silently read as "not gitignored"; say why.
    if let Err(e) = check_repo(base) {
        eprintln!("warning: worktree-link skipped: {e:#}");
        return Ok(());
    }
    std::fs::create_dir_all(store).with_context(|| format!("cannot create {}", store.display()))?;
    // Validate every entry before touching the disk: a config error must not
    // leave half the entries adopted.
    let rels = entries.iter().map(|e| link_entry_path(e)).collect::<Result<Vec<_>>>()?;
    let mut seen = std::collections::BTreeSet::new();
    let expanded = rels.iter().flat_map(|rel| expand_link_glob(rel, &[*base, store]));
    for rel in expanded {
        if !seen.insert(rel.clone()) {
            continue;
        }
        let entry = rel.display();
        // Replacing a tracked (or merely untracked) path with a symlink would
        // show up in `git status`; only gitignored, untracked paths qualify.
        if !git_ignores(base, &rel) {
            eprintln!(
                "warning: worktree-link `{entry}` skipped: not gitignored (or tracked) in `{}`",
                base.display()
            );
            continue;
        }
        let shared = store.join(&rel);
        if let Err(e) = adopt(&base.join(&rel), &shared, stores) {
            eprintln!("warning: worktree-link `{entry}`: {e:#}");
            continue;
        }
        if shared.symlink_metadata().is_err() {
            // Neither the store nor the base has it yet; a later run links it.
            continue;
        }
        for tree in trees {
            match link_into(&tree.join(&rel), &shared, stores) {
                Ok(true) if !git_ignores(tree, &rel) => eprintln!(
                    "warning: worktree-link `{entry}` shows as untracked in `{}`: a `dir/` \
                     gitignore pattern doesn't match a symlink, drop the trailing `/`",
                    tree.display()
                ),
                Ok(_) => {}
                Err(e) => eprintln!("warning: worktree-link `{entry}` in `{}`: {e:#}", tree.display()),
            }
        }
    }
    Ok(())
}

/// Normalize a `worktree-link` entry to a plain relative path: leading `./`
/// and trailing `/` dropped, interior `.` collapsed. Anything that could
/// escape the tree or the store is rejected, as is `**` (globs are one
/// segment only, see [`expand_link_glob`]).
fn link_entry_path(entry: &str) -> Result<PathBuf> {
    use std::path::Component;
    let mut trimmed = entry.trim_end_matches('/');
    while let Some(rest) = trimmed.strip_prefix("./") {
        trimmed = rest.trim_start_matches('/');
    }
    let mut rel = PathBuf::new();
    for c in Path::new(trimmed).components() {
        match c {
            Component::Normal(n) if n != ".git" && n != "**" => rel.push(n),
            Component::Normal(n) if n == "**" => {
                bail!("invalid worktree-link `{entry}`: `**` is not supported, use `*` per path segment")
            }
            _ => bail!(
                "invalid worktree-link `{entry}`: must be a relative path inside the repo (no `..`, `.git`, or absolute path)"
            ),
        }
    }
    if rel.as_os_str().is_empty() {
        bail!("invalid worktree-link `{entry}`: empty path");
    }
    Ok(rel)
}

/// Expand `*` / `?` in `rel`, one path segment at a time, against what exists
/// under any of `roots` (the base repo and the store, so a file already moved
/// into the store still matches). Only the directories the pattern names are
/// listed, never a recursive walk: this runs on every `run`. Wildcards match
/// dotfiles (as in gitignore) but never `.git`. A literal entry passes through
/// unchanged, existing or not; a glob yields only existing matches.
fn expand_link_glob(rel: &Path, roots: &[&Path]) -> Vec<PathBuf> {
    let mut frontier = vec![PathBuf::new()];
    let segments: Vec<_> = rel.components().map(|c| c.as_os_str().to_string_lossy().into_owned()).collect();
    for (i, seg) in segments.iter().enumerate() {
        if !seg.contains(['*', '?']) {
            frontier.iter_mut().for_each(|p| p.push(seg));
            continue;
        }
        let last = i + 1 == segments.len();
        let mut next = std::collections::BTreeSet::new();
        for prefix in &frontier {
            for root in roots {
                let Ok(dir) = std::fs::read_dir(root.join(prefix)) else { continue };
                for entry in dir.flatten() {
                    let name = entry.file_name().to_string_lossy().into_owned();
                    // Intermediate segments must lead somewhere (`metadata`
                    // follows symlinks, so a linked dir still counts).
                    let descends = last || entry.path().metadata().is_ok_and(|m| m.is_dir());
                    if name != ".git" && descends && wildcard_match(seg, &name) {
                        next.insert(prefix.join(&name));
                    }
                }
            }
        }
        frontier = next.into_iter().collect();
    }
    frontier
}

/// Shell-style match of one path segment: `*` any run of chars, `?` one char,
/// everything else literal.
fn wildcard_match(pattern: &str, name: &str) -> bool {
    let (p, n): (Vec<char>, Vec<char>) = (pattern.chars().collect(), name.chars().collect());
    let (mut pi, mut ni) = (0, 0);
    // Backtrack point: pattern index after the last `*`, and the name index it
    // is currently assumed to have consumed up to.
    let mut star: Option<(usize, usize)> = None;
    while ni < n.len() {
        match p.get(pi) {
            Some('*') => {
                star = Some((pi + 1, ni));
                pi += 1;
            }
            Some(&c) if c == '?' || c == n[ni] => {
                pi += 1;
                ni += 1;
            }
            _ => match star {
                Some((sp, sn)) => {
                    pi = sp;
                    ni = sn + 1;
                    star = Some((sp, sn + 1));
                }
                None => return false,
            },
        }
    }
    p[pi..].iter().all(|&c| c == '*')
}

/// Whether git ignores `rel` in `tree` (false for tracked paths: `check-ignore`
/// only reports untracked ones). A missing git, or a repo [`host_git`]
/// refuses, counts as not ignored.
fn git_ignores(tree: &Path, rel: &Path) -> bool {
    host_git(tree).is_ok_and(|mut git| {
        git.args(["check-ignore", "-q", "--"])
            .arg(rel)
            .status()
            .is_ok_and(|s| s.success())
    })
}

/// First sighting: move the base repo's real file/dir into the empty store
/// slot (its symlink is laid down by [`link_into`]). No-op once the store has
/// it, or when the base has nothing (or a foreign symlink) there; a link into
/// an old per-sandbox store is an error so the entry is skipped with a hint.
fn adopt(base_path: &Path, shared: &Path, stores: &Path) -> Result<()> {
    if shared.symlink_metadata().is_ok() {
        return Ok(());
    }
    let Ok(meta) = base_path.symlink_metadata() else { return Ok(()) };
    if meta.file_type().is_symlink() {
        if let Some(old) = old_store_link(base_path, shared, stores) {
            return Err(old_layout_error(base_path, &old, shared));
        }
        return Ok(());
    }
    if let Some(parent) = shared.parent() {
        std::fs::create_dir_all(parent)?;
    }
    if std::fs::rename(base_path, shared).is_ok() {
        return Ok(());
    }
    // rename fails across filesystems (repo and config on different mounts);
    // a file can be copied over, a dir is left for the user to move.
    if !meta.is_file() {
        bail!(
            "cannot move `{}` into `{}`; move it there yourself",
            base_path.display(),
            shared.display()
        );
    }
    std::fs::copy(base_path, shared)?;
    std::fs::remove_file(base_path)?;
    Ok(())
}

/// Where `path` links to, when that is another devsandbox store under
/// `stores` rather than `shared`: a link left by the old one-store-per-sandbox
/// layout (0.4.0). Only detected, never rewritten: an automatic move
/// broke every instance it didn't relink in the same run.
fn old_store_link(path: &Path, shared: &Path, stores: &Path) -> Option<PathBuf> {
    let target = std::fs::read_link(path).ok()?;
    (target.is_absolute() && target.starts_with(stores) && target != shared).then_some(target)
}

fn old_layout_error(path: &Path, old: &Path, shared: &Path) -> anyhow::Error {
    anyhow::anyhow!(
        "`{}` links into an old per-sandbox store (`{}`); move the file to `{}` \
         (keep one copy if several old stores differ), delete the link, then `rebuild`",
        path.display(),
        old.display(),
        shared.display()
    )
}

/// Point `path` at `shared` with an absolute symlink. `Ok(true)` when it
/// created the link, `Ok(false)` when the right link already exists. Anything
/// else at `path` (a real file, a foreign symlink, a link into an old store)
/// is an error, left alone.
fn link_into(path: &Path, shared: &Path, stores: &Path) -> Result<bool> {
    match path.symlink_metadata() {
        Err(_) => {}
        Ok(m) if m.file_type().is_symlink() && std::fs::read_link(path)? == shared => {
            return Ok(false)
        }
        Ok(m) if m.file_type().is_symlink() => {
            if let Some(old) = old_store_link(path, shared, stores) {
                return Err(old_layout_error(path, &old, shared));
            }
            bail!(
                "`{}` already exists and is not a link to `{}`; merge it into the shared copy and delete it",
                path.display(),
                shared.display()
            )
        }
        Ok(_) => bail!(
            "`{}` already exists and is not a link to `{}`; merge it into the shared copy and delete it",
            path.display(),
            shared.display()
        ),
    }
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    #[cfg(unix)]
    std::os::unix::fs::symlink(shared, path)?;
    #[cfg(not(unix))]
    bail!("symlinks are only supported on unix hosts");
    #[cfg(unix)]
    Ok(true)
}

/// Git's output for `git -C base <args>` on success, trimmed; None on failure.
fn git_query(base: &Path, args: &[&str]) -> Option<String> {
    let out = host_git(base)
        .ok()?
        .args(args)
        .output()
        .ok()?;
    out.status
        .success()
        .then(|| String::from_utf8_lossy(&out.stdout).trim().to_string())
}

/// Skip window for [`fetch_origin`]: back-to-back `run`s (or an IDE's
/// auto-fetch) shouldn't each pay a network round-trip.
const FETCH_FRESH_FOR: std::time::Duration = std::time::Duration::from_secs(120);

/// Whether a fetch that last wrote `FETCH_HEAD` at `modified` is recent enough
/// to skip another. A future mtime (clock skew) counts as fresh.
fn fetch_is_fresh(modified: std::time::SystemTime, now: std::time::SystemTime) -> bool {
    now.duration_since(modified).map_or(true, |age| age < FETCH_FRESH_FOR)
}

/// `git fetch origin` unless one ran within [`FETCH_FRESH_FOR`]. Git records no
/// fetch timestamp, but every fetch rewrites `FETCH_HEAD`, so its mtime stands
/// in (missing file = never fetched). `--git-path` resolves it for a base that
/// is itself a linked worktree.
fn fetch_origin(base: &Path) -> Result<()> {
    let fresh = git_query(base, &["rev-parse", "--git-path", "FETCH_HEAD"])
        .and_then(|p| std::fs::metadata(base.join(p)).ok())
        .and_then(|m| m.modified().ok())
        .is_some_and(|t| fetch_is_fresh(t, std::time::SystemTime::now()));
    if fresh {
        return Ok(());
    }
    let fetch = host_git(base)?
        .args(["fetch", "--quiet", "origin"])
        .output()
        .context("failed to run git (is it installed?)")?;
    if !fetch.status.success() {
        eprintln!(
            "warning: `git fetch origin` failed in `{}`; worktree may start from a stale ref: {}",
            base.display(),
            String::from_utf8_lossy(&fetch.stderr).trim()
        );
    }
    Ok(())
}

/// Refresh `origin` and pick the ref a new worktree branch starts from.
///
/// `fetch` only moves `refs/remotes/origin/*` and adds objects — the base
/// checkout's HEAD, index and files are untouched, so it is safe while
/// another agent works there. It is skipped when one ran in the last two
/// minutes (see [`fetch_origin`]). Its output is captured; a failed fetch (offline, auth) only warns and falls back to
/// the last-fetched refs.
///
/// Resolution: `explicit` (`--base`/`worktree-base`) → `origin/HEAD` →
/// `origin/main` → `origin/master` → `HEAD`. The fetch still runs for an
/// explicit ref so e.g. `origin/develop` is fresh too. `origin/HEAD` is unset
/// for repos that added the remote after `git init`; we don't `remote
/// set-head` since that writes to the base repo. Repos with no `origin` keep
/// the old start-from-HEAD behavior.
fn worktree_start_point(base: &Path, explicit: Option<&str>) -> Result<String> {
    let fallback = || explicit.unwrap_or("HEAD").to_string();
    if git_query(base, &["remote", "get-url", "origin"]).is_none() {
        return Ok(fallback());
    }
    fetch_origin(base)?;
    if let Some(r) = explicit {
        return Ok(r.to_string());
    }
    if let Some(r) = git_query(base, &["symbolic-ref", "--quiet", "--short", "refs/remotes/origin/HEAD"]) {
        return Ok(r);
    }
    for r in ["origin/main", "origin/master"] {
        let full = format!("refs/remotes/{r}");
        if git_query(base, &["show-ref", "--verify", "--quiet", &full]).is_some() {
            return Ok(r.to_string());
        }
    }
    Ok("HEAD".to_string())
}

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

    #[test]
    fn git_companion_mount_uses_identical_paths() {
        let mount = git_companion_mount(Path::new("/home/u/repo"));
        assert_eq!(mount, "/home/u/repo/.git:/home/u/repo/.git");
    }

    fn git(dir: &Path, args: &[&str]) -> String {
        let out = std::process::Command::new("git")
            .arg("-C")
            .arg(dir)
            .args(["-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false"])
            .args(args)
            .output()
            .unwrap();
        assert!(out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr));
        String::from_utf8_lossy(&out.stdout).trim().to_string()
    }

    #[test]
    fn worktree_includes_copy_only_matching_ignored_files() {
        let root = std::env::temp_dir().join(format!("devsandbox-wti-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        let (base, wt) = (root.join("base"), root.join("wt"));
        std::fs::create_dir_all(&base).unwrap();
        git(&base, &["init", "-q", "-b", "main"]);
        let write = |rel: &str, body: &str| {
            let p = base.join(rel);
            std::fs::create_dir_all(p.parent().unwrap()).unwrap();
            std::fs::write(p, body).unwrap();
        };
        write(".gitignore", ".env*\nsecrets/\n.vscode/\n");
        write(".worktreeinclude", "# secrets\n.env*\n!.env.skip\nsecrets/\nnotes.txt\n.vscode/tasks.json\n");
        // `-f`: a tracked file matching both lists must still not be copied.
        write(".env.example", "tracked");
        git(&base, &["add", ".gitignore", ".worktreeinclude"]);
        git(&base, &["add", "-f", ".env.example"]);
        git(&base, &["commit", "-q", "-m", "init"]);
        write(".env", "base");
        write(".env.local", "local");
        write(".env.skip", "negated");
        write("secrets/x/key", "k");
        write("notes.txt", "untracked, not ignored");
        write(".vscode/tasks.json", "{}");
        write(".vscode/other.json", "{}");
        create_worktree(&base, &wt, "sandbox/i", None, false, &Default::default()).unwrap();
        // Pre-existing destination is never overwritten.
        std::fs::write(wt.join(".env.local"), "mine").unwrap();

        copy_worktree_includes(&base, &wt, &[]);

        let read = |rel: &str| std::fs::read_to_string(wt.join(rel)).ok();
        assert_eq!(read(".env").as_deref(), Some("base"));
        assert_eq!(read(".env.local").as_deref(), Some("mine"));
        assert_eq!(read("secrets/x/key").as_deref(), Some("k"));
        assert_eq!(read(".vscode/tasks.json").as_deref(), Some("{}"));
        assert_eq!(read(".env.skip"), None);
        assert_eq!(read("notes.txt"), None);
        assert_eq!(read(".vscode/other.json"), None);
        assert_eq!(git(&wt, &["status", "--porcelain"]), "");

        std::fs::remove_dir_all(&root).unwrap();
    }

    /// A committed repo at `root/base` with `.gitignore` = `ignore`.
    fn repo(root: &Path, ignore: &str) -> std::path::PathBuf {
        let _ = std::fs::remove_dir_all(root);
        let base = root.join("base");
        std::fs::create_dir_all(&base).unwrap();
        git(&base, &["init", "-q", "-b", "main"]);
        std::fs::write(base.join(".gitignore"), ignore).unwrap();
        git(&base, &["add", ".gitignore"]);
        git(&base, &["commit", "-q", "-m", "init"]);
        base
    }

    #[test]
    fn worktree_include_config_patterns_extend_and_negate_file() {
        let root = std::env::temp_dir().join(format!("devsandbox-wtip-{}", std::process::id()));
        let base = repo(&root, ".env*\n");
        let wt = root.join("wt");
        std::fs::write(base.join(".worktreeinclude"), ".env\n.env.local\n").unwrap();
        for f in [".env", ".env.local", ".env.test"] {
            std::fs::write(base.join(f), f).unwrap();
        }
        create_worktree(&base, &wt, "sandbox/p", None, false, &Default::default()).unwrap();

        copy_worktree_includes(&base, &wt, &["!.env.local".into(), ".env.test".into()]);

        assert!(wt.join(".env").is_file());
        assert!(!wt.join(".env.local").exists(), "config negation must win over the file");
        assert!(wt.join(".env.test").is_file());
        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn link_shared_files_adopts_links_and_guards() {
        let root = std::env::temp_dir().join(format!("devsandbox-wtl-{}", std::process::id()));
        let base = repo(&root, ".env*\nsecrets\n");
        let (wt, store) = (root.join("wt"), root.join("store"));
        std::fs::write(base.join(".env"), "base").unwrap();
        std::fs::create_dir_all(base.join("secrets")).unwrap();
        std::fs::write(base.join("secrets/key"), "k").unwrap();
        std::fs::write(base.join("tracked.txt"), "t").unwrap();
        git(&base, &["add", "tracked.txt"]);
        git(&base, &["commit", "-q", "-m", "t"]);
        create_worktree(&base, &wt, "sandbox/l", None, false, &Default::default()).unwrap();
        // The worktree already holds its own `.env.local`: a conflict to keep.
        std::fs::write(wt.join(".env.local"), "mine").unwrap();
        std::fs::create_dir_all(&store).unwrap();
        std::fs::write(store.join(".env.local"), "shared").unwrap();
        let entries: Vec<String> =
            [".env", "secrets/", ".env.local", "tracked.txt", ".env.missing", ".env"]
                .map(String::from)
                .into();

        for _ in 0..2 {
            // Twice: the second pass must be a no-op.
            link_shared_files(&store, &[&base, &wt], &entries).unwrap();
        }

        let link = |p: &Path| std::fs::read_link(p).ok();
        assert_eq!(std::fs::read_to_string(store.join(".env")).unwrap(), "base");
        assert_eq!(link(&base.join(".env")), Some(store.join(".env")));
        assert_eq!(link(&wt.join(".env")), Some(store.join(".env")));
        assert_eq!(link(&wt.join("secrets")), Some(store.join("secrets")));
        assert_eq!(std::fs::read_to_string(wt.join("secrets/key")).unwrap(), "k");
        // Store copy linked into the base; the worktree's own file is kept.
        assert_eq!(link(&base.join(".env.local")), Some(store.join(".env.local")));
        assert_eq!(std::fs::read_to_string(wt.join(".env.local")).unwrap(), "mine");
        // Tracked path untouched, missing everywhere skipped.
        assert!(link(&base.join("tracked.txt")).is_none());
        assert!(!store.join("tracked.txt").exists());
        assert!(base.join(".env.missing").symlink_metadata().is_err());
        // Live: an edit through one tree is seen by the other.
        std::fs::write(wt.join(".env"), "rotated").unwrap();
        assert_eq!(std::fs::read_to_string(base.join(".env")).unwrap(), "rotated");
        assert_eq!(git(&base, &["status", "--porcelain"]), "");
        assert_eq!(git(&wt, &["status", "--porcelain"]), "");

        for bad in ["../x", "/abs", ".git/config", "a/../../x", "", "./", "**/.env", "a/**"] {
            let err = link_shared_files(&store, &[&base], &[bad.into()]).unwrap_err();
            assert!(err.to_string().contains("invalid worktree-link"), "{bad}: {err}");
        }
        // A bad entry anywhere fails before any other entry is adopted.
        std::fs::write(base.join(".env.late"), "x").unwrap();
        assert!(link_shared_files(&store, &[&base], &[".env.late".into(), "../x".into()]).is_err());
        assert!(!store.join(".env.late").exists());
        std::fs::remove_dir_all(&root).unwrap();
    }

    /// Links into an old per-sandbox store (`shared-files/<sandbox>/`) are
    /// reported, never moved or rewritten: migrating one run's trees silently
    /// broke every other instance still pointing at the old store.
    #[test]
    fn link_shared_files_leaves_old_store_links_alone() {
        let root = std::env::temp_dir().join(format!("devsandbox-wtm-{}", std::process::id()));
        let base = repo(&root, ".env*\n");
        let stores = root.join("shared-files");
        let (old, store) = (stores.join("web"), stores.join("repo-1234"));
        let wt = root.join("wt");
        create_worktree(&base, &wt, "sandbox/a", None, false, &Default::default()).unwrap();
        std::fs::create_dir_all(&old).unwrap();
        std::fs::write(old.join(".env"), "a").unwrap();
        std::fs::write(old.join(".env.b"), "b").unwrap();
        let symlink = |target: &Path, at: &Path| std::os::unix::fs::symlink(target, at).unwrap();
        // Base itself on the old layout: nothing adopted.
        symlink(&old.join(".env"), &base.join(".env"));
        symlink(&old.join(".env"), &wt.join(".env"));
        // Base already a real file, a worktree still on the old layout.
        std::fs::write(base.join(".env.b"), "new").unwrap();
        symlink(&old.join(".env.b"), &wt.join(".env.b"));
        let entries: Vec<String> = [".env", ".env.b"].map(String::from).into();

        link_shared_files(&store, &[&base, &wt], &entries).unwrap();

        let link = |p: &Path| std::fs::read_link(p).ok();
        assert_eq!(link(&base.join(".env")), Some(old.join(".env")));
        assert_eq!(link(&wt.join(".env")), Some(old.join(".env")));
        assert_eq!(std::fs::read_to_string(old.join(".env")).unwrap(), "a");
        assert!(store.join(".env").symlink_metadata().is_err());
        assert_eq!(link(&base.join(".env.b")), Some(store.join(".env.b")));
        assert_eq!(link(&wt.join(".env.b")), Some(old.join(".env.b")));
        assert_eq!(std::fs::read_to_string(old.join(".env.b")).unwrap(), "b");

        let err = link_into(&wt.join(".env.b"), &store.join(".env.b"), &stores).unwrap_err();
        assert!(err.to_string().contains("old per-sandbox store"), "{err}");
        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn link_entry_path_normalizes() {
        for (entry, want) in [
            ("./packages/bolt/.env", "packages/bolt/.env"),
            ("././.env", ".env"),
            (".//x/", "x"),
            ("a/./b", "a/b"),
            ("packages/*/.env", "packages/*/.env"),
        ] {
            assert_eq!(link_entry_path(entry).unwrap(), Path::new(want), "{entry}");
        }
    }

    #[test]
    fn wildcard_matches_one_segment() {
        for (p, n, ok) in [
            ("*", "bolt", true),
            ("*", ".hidden", true),
            ("*", "", true),
            (".env*", ".env.local", true),
            (".env*", ".en", false),
            ("a?c", "abc", true),
            ("a?c", "ac", false),
            ("*-proxy", "claude-code-proxy", true),
            ("*a*b", "xaxxb", true),
            ("*a*b", "xaxxbc", false),
            ("lit", "lit", true),
            ("lit", "lits", false),
        ] {
            assert_eq!(wildcard_match(p, n), ok, "{p} vs {n}");
        }
    }

    #[test]
    fn link_glob_expands_against_base_and_store() {
        let root = std::env::temp_dir().join(format!("devsandbox-wtg-{}", std::process::id()));
        let base = repo(&root, ".env\nnotadir\n");
        let (wt, store) = (root.join("wt"), root.join("store"));
        for pkg in ["bolt", "proxy", "empty"] {
            std::fs::create_dir_all(base.join("packages").join(pkg)).unwrap();
        }
        std::fs::write(base.join("packages/bolt/.env"), "b").unwrap();
        std::fs::write(base.join("packages/proxy/.env"), "p").unwrap();
        // A plain file where a directory is expected is not descended into.
        std::fs::write(base.join("packages/notadir"), "").unwrap();
        // Only in the store (base copy gone): the glob must still find it.
        std::fs::create_dir_all(store.join("packages/gone")).unwrap();
        std::fs::write(store.join("packages/gone/.env"), "g").unwrap();
        create_worktree(&base, &wt, "sandbox/g", None, false, &Default::default()).unwrap();

        let mut got = expand_link_glob(Path::new("packages/*/.env"), &[&base, &store]);
        got.sort();
        let want: Vec<PathBuf> = ["bolt", "empty", "gone", "proxy"]
            .iter()
            .map(|p| Path::new("packages").join(p).join(".env"))
            .collect();
        assert_eq!(got, want);

        link_shared_files(&store, &[&base, &wt], &["./packages/*/.env".into()]).unwrap();

        let link = |p: PathBuf| std::fs::read_link(p).ok();
        for pkg in ["bolt", "proxy", "gone"] {
            let rel = format!("packages/{pkg}/.env");
            assert_eq!(link(wt.join(&rel)), Some(store.join(&rel)), "{rel}");
        }
        assert_eq!(std::fs::read_to_string(wt.join("packages/gone/.env")).unwrap(), "g");
        // No file anywhere: nothing created, not even an empty store slot.
        assert!(wt.join("packages/empty/.env").symlink_metadata().is_err());
        assert!(!store.join("packages/empty").exists());
        assert_eq!(git(&base, &["status", "--porcelain"]), "");
        std::fs::remove_dir_all(&root).unwrap();
    }

    /// The security review's PoC: a sandbox plants a hook, or command-running
    /// config, in the shared `.git`; host git must run neither.
    #[cfg(unix)]
    #[test]
    fn create_worktree_runs_no_repo_controlled_code() {
        use std::os::unix::fs::PermissionsExt;
        let root = std::env::temp_dir().join(format!("devsandbox-wtsec-{}", std::process::id()));
        let base = repo(&root, "");
        let marker = root.join("pwned");
        let hook = base.join(".git/hooks/post-checkout");
        std::fs::write(&hook, format!("#!/bin/sh\ntouch '{}'\n", marker.display())).unwrap();
        std::fs::set_permissions(&hook, std::fs::Permissions::from_mode(0o755)).unwrap();

        create_worktree(&base, &root.join("wt"), "sandbox/h", None, false, &Default::default()).unwrap();
        assert!(!marker.exists(), "post-checkout hook ran on the host");

        git(&base, &["config", "core.fsmonitor", &hook.to_string_lossy()]);
        let err = create_worktree(&base, &root.join("wt2"), "sandbox/f", None, false, &Default::default()).unwrap_err().to_string();
        assert!(err.contains("refusing to run git on"), "{err}");
        assert!(err.contains(".git/config sets core.fsmonitor"), "{err}");
        assert!(!marker.exists(), "fsmonitor ran on the host");
        assert!(!root.join("wt2").exists());
        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn detached_worktree_starts_at_base_head_without_a_branch() {
        let root = std::env::temp_dir().join(format!("devsandbox-wtdet-{}", std::process::id()));
        let base = repo(&root, "");
        let branches = git(&base, &["branch", "--list"]);
        let wt = root.join("nested/wt");

        create_detached_worktree(&base, &wt).unwrap();
        assert_eq!(git(&wt, &["rev-parse", "HEAD"]), git(&base, &["rev-parse", "HEAD"]));
        let symbolic = std::process::Command::new("git")
            .arg("-C")
            .arg(&wt)
            .args(["symbolic-ref", "-q", "HEAD"])
            .status()
            .unwrap();
        assert!(!symbolic.success(), "worktree HEAD is not detached");
        assert_eq!(git(&base, &["branch", "--list"]), branches, "a branch was created");
        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn detached_worktree_needs_a_commit() {
        let root = std::env::temp_dir().join(format!("devsandbox-wtdet0-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        let base = root.join("base");
        std::fs::create_dir_all(&base).unwrap();
        git(&base, &["init", "-q", "-b", "main"]);
        let err = create_detached_worktree(&base, &root.join("wt")).unwrap_err().to_string();
        assert!(err.contains("has no commits"), "{err}");
        assert!(!root.join("wt").exists());
        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn worktree_includes_absent_file_is_noop() {
        let root = std::env::temp_dir().join(format!("devsandbox-wti0-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        let wt = root.join("wt");
        std::fs::create_dir_all(&root).unwrap();
        copy_worktree_includes(&root, &wt, &[]);
        assert!(!wt.exists());
        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn fetch_freshness_window() {
        use std::time::{Duration, SystemTime};
        let now = SystemTime::now();
        assert!(fetch_is_fresh(now - Duration::from_secs(60), now));
        assert!(!fetch_is_fresh(now - Duration::from_secs(120), now));
        assert!(!fetch_is_fresh(now - Duration::from_secs(3600), now));
        assert!(fetch_is_fresh(now + Duration::from_secs(5), now));
    }

    #[test]
    fn worktree_starts_from_fetched_origin_default_untracked() {
        let root = std::env::temp_dir().join(format!("devsandbox-wt-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        let (origin, base, wt) = (root.join("origin"), root.join("base"), root.join("wt"));
        std::fs::create_dir_all(&origin).unwrap();
        git(&origin, &["init", "-q", "-b", "main"]);
        git(&origin, &["commit", "-q", "--allow-empty", "-m", "one"]);
        git(&root, &["clone", "-q", &origin.to_string_lossy(), "base"]);
        // Base checkout sits on a feature branch, as a working agent's would.
        git(&base, &["checkout", "-q", "-b", "feature"]);
        git(&base, &["commit", "-q", "--allow-empty", "-m", "wip"]);
        let feature_tip = git(&base, &["rev-parse", "HEAD"]);
        // Upstream moves on after the clone: only a fetch can see this.
        git(&origin, &["commit", "-q", "--allow-empty", "-m", "two"]);
        let main_tip = git(&origin, &["rev-parse", "HEAD"]);

        create_worktree(&base, &wt, "sandbox/x", None, false, &Default::default()).unwrap();

        assert_eq!(git(&wt, &["rev-parse", "HEAD"]), main_tip);
        let upstream = std::process::Command::new("git")
            .arg("-C")
            .arg(&wt)
            .args(["rev-parse", "--abbrev-ref", "@{upstream}"])
            .output()
            .unwrap();
        assert!(!upstream.status.success(), "branch must not track origin");
        assert_eq!(git(&base, &["rev-parse", "HEAD"]), feature_tip);
        assert_eq!(git(&base, &["branch", "--show-current"]), "feature");

        // An explicit base wins over detection; a bad one errors, no fallback.
        let wt2 = root.join("wt2");
        create_worktree(&base, &wt2, "sandbox/y", Some("feature"), false, &Default::default()).unwrap();
        assert_eq!(git(&wt2, &["rev-parse", "HEAD"]), feature_tip);
        let err = create_worktree(&base, &root.join("wt3"), "sandbox/z", Some("nope"), false, &Default::default())
            .unwrap_err()
            .to_string();
        assert!(err.contains("worktree base `nope`"), "{err}");

        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn branch_source_prefers_local_then_remote() {
        assert_eq!(branch_source(true, true), BranchSource::Local);
        assert_eq!(branch_source(true, false), BranchSource::Local);
        assert_eq!(branch_source(false, true), BranchSource::Remote);
        assert_eq!(branch_source(false, false), BranchSource::New);
    }

    #[test]
    fn worktree_list_porcelain_finds_branch() {
        let list = "worktree /r\nHEAD 1111\nbranch refs/heads/main\n\n\
                    worktree /w/a\nHEAD 2222\ndetached\n\n\
                    worktree /w/b\nHEAD 3333\nbranch refs/heads/feat/x\n\n\
                    worktree /w/gone\nHEAD 4444\nbranch refs/heads/old\nprunable gitdir file points to non-existent location\n\n\
                    worktree /w/gone2\nHEAD 5555\ndetached\nlocked\nprunable\n\n";
        let entry = |path: &str, branch: Option<&str>, prunable| WorktreeEntry {
            path: path.into(),
            branch: branch.map(str::to_string),
            prunable,
        };
        assert_eq!(
            parse_worktree_list(list),
            vec![
                entry("/r", Some("main"), false),
                entry("/w/a", None, false),
                entry("/w/b", Some("feat/x"), false),
                entry("/w/gone", Some("old"), true),
                entry("/w/gone2", None, true),
            ]
        );
        assert_eq!(parse_worktree_list(""), vec![]);
    }

    fn head_branch(wt: &Path) -> String {
        git(wt, &["rev-parse", "--abbrev-ref", "HEAD"])
    }

    #[test]
    fn worktree_reuses_existing_local_branch() {
        let root = std::env::temp_dir().join(format!("devsandbox-wtlocal-{}", std::process::id()));
        let base = repo(&root, "");
        git(&base, &["branch", "feat/a"]);
        git(&base, &["checkout", "-q", "feat/a"]);
        git(&base, &["commit", "-q", "--allow-empty", "-m", "wip"]);
        let tip = git(&base, &["rev-parse", "HEAD"]);
        git(&base, &["checkout", "-q", "main"]);
        let branches = git(&base, &["for-each-ref", "--format=%(refname)", "refs/heads"]);
        let wt = root.join("wt");

        // An explicit base is ignored (noted), not an error.
        let created = create_worktree(&base, &wt, "feat/a", Some("main"), true, &Default::default()).unwrap();

        assert!(!created);
        assert_eq!(head_branch(&wt), "feat/a");
        assert_eq!(git(&wt, &["rev-parse", "HEAD"]), tip);
        assert_eq!(git(&base, &["for-each-ref", "--format=%(refname)", "refs/heads"]), branches);
        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn worktree_refuses_branch_checked_out_elsewhere_naming_where() {
        let root = std::env::temp_dir().join(format!("devsandbox-wtbusy-{}", std::process::id()));
        let base = repo(&root, "");
        let canon = base.canonicalize().unwrap();

        let err = create_worktree(&base, &root.join("wt"), "main", None, true, &Default::default())
            .unwrap_err()
            .to_string();
        assert!(err.contains(&format!("already checked out at `{}`", canon.display())), "{err}");
        assert!(!err.contains("instance"), "{err}");
        assert!(!root.join("wt").join(".git").exists());

        // Known to state: the owning instance is named too.
        let state: crate::state::State = toml::from_str(&format!(
            "[instance.web]\nsandbox = \"web\"\ncontainer = \"c\"\nfolder = {:?}\nworkspace = \"/w\"\ncreated_unix = 0\n",
            base.to_string_lossy()
        ))
        .unwrap();
        let err = create_worktree(&base, &root.join("wt"), "main", None, true, &state)
            .unwrap_err()
            .to_string();
        assert!(err.contains("(instance `web`)"), "{err}");
        std::fs::remove_dir_all(&root).unwrap();
    }

    fn upstream_of(wt: &Path, branch: &str) -> Option<String> {
        let out = std::process::Command::new("git")
            .arg("-C")
            .arg(wt)
            .args(["rev-parse", "--abbrev-ref", &format!("{branch}@{{upstream}}")])
            .output()
            .unwrap();
        out.status.success().then(|| String::from_utf8_lossy(&out.stdout).trim().to_string())
    }

    #[test]
    fn worktree_tracks_remote_only_branch() {
        let root = std::env::temp_dir().join(format!("devsandbox-wtremote-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        let (origin, seed, base) = (root.join("origin.git"), root.join("seed"), root.join("base"));
        std::fs::create_dir_all(&root).unwrap();
        git(&root, &["init", "-q", "--bare", "-b", "main", &origin.to_string_lossy()]);
        git(&root, &["clone", "-q", &origin.to_string_lossy(), "seed"]);
        git(&seed, &["checkout", "-q", "-b", "main"]);
        git(&seed, &["commit", "-q", "--allow-empty", "-m", "init"]);
        git(&seed, &["push", "-q", "origin", "main"]);
        git(&seed, &["checkout", "-q", "-b", "pr/1"]);
        git(&seed, &["commit", "-q", "--allow-empty", "-m", "pr"]);
        git(&seed, &["push", "-q", "origin", "pr/1"]);
        let pr_tip = git(&seed, &["rev-parse", "HEAD"]);
        git(&root, &["clone", "-q", &origin.to_string_lossy(), "base"]);

        let wt = root.join("wt");
        let created = create_worktree(&base, &wt, "pr/1", None, true, &Default::default()).unwrap();
        assert!(!created);
        assert_eq!(head_branch(&wt), "pr/1");
        assert_eq!(git(&wt, &["rev-parse", "HEAD"]), pr_tip);
        assert_eq!(upstream_of(&wt, "pr/1").as_deref(), Some("origin/pr/1"));

        // Pushed after the last full fetch, which is still fresh (so skipped):
        // only the targeted fetch of a named branch can find it.
        git(&base, &["fetch", "-q", "origin"]);
        for b in ["pr/2", "sandbox/gen"] {
            git(&seed, &["checkout", "-q", "-b", b, "main"]);
            git(&seed, &["commit", "-q", "--allow-empty", "-m", b]);
            git(&seed, &["push", "-q", "origin", b]);
        }
        let wt2 = root.join("wt2");
        assert!(!create_worktree(&base, &wt2, "pr/2", None, true, &Default::default()).unwrap());
        assert_eq!(upstream_of(&wt2, "pr/2").as_deref(), Some("origin/pr/2"));
        // A pattern-generated name is never fetched for: a fresh branch.
        let wt3 = root.join("wt3");
        assert!(create_worktree(&base, &wt3, "sandbox/gen", None, false, &Default::default()).unwrap());
        assert_eq!(upstream_of(&wt3, "sandbox/gen"), None);

        // Neither local nor remote: today's fresh, untracked branch.
        let wt4 = root.join("wt4");
        assert!(create_worktree(&base, &wt4, "feat/new", None, true, &Default::default()).unwrap());
        assert_eq!(head_branch(&wt4), "feat/new");
        assert_eq!(upstream_of(&wt4, "feat/new"), None);
        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn worktrees_are_locked_so_prune_keeps_them() {
        let root = std::env::temp_dir().join(format!("devsandbox-wtlock-{}", std::process::id()));
        let base = repo(&root, "");
        let (wt, detached) = (root.join("wt"), root.join("detached"));
        create_worktree(&base, &wt, "sandbox/k", None, false, &Default::default()).unwrap();
        create_detached_worktree(&base, &detached).unwrap();
        // Seen from a container: the checkouts are missing, and prune runs.
        let hidden = root.join("hidden");
        std::fs::create_dir_all(&hidden).unwrap();
        for p in [&wt, &detached] {
            std::fs::rename(p, hidden.join(p.file_name().unwrap())).unwrap();
        }
        git(&base, &["worktree", "prune"]);
        for p in [&wt, &detached] {
            std::fs::rename(hidden.join(p.file_name().unwrap()), p).unwrap();
            assert!(git(p, &["status", "--porcelain"]).is_empty(), "{} lost its record", p.display());
        }
        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn lock_is_lazy_and_idempotent_and_unlock_undoes_it() {
        let root = std::env::temp_dir().join(format!("devsandbox-wtlazy-{}", std::process::id()));
        let base = repo(&root, "");
        let wt = root.join("wt");
        // Made before locking existed.
        git(&base, &["worktree", "add", "-q", &wt.to_string_lossy(), "-b", "old"]);
        let locked = || admin_dir(&wt).unwrap().join("locked").exists();
        assert!(!locked());
        lock_worktree(&base, &wt);
        assert!(locked());
        lock_worktree(&base, &wt);
        assert!(locked());
        unlock_worktree(&base, &wt);
        assert!(!locked());
        // Not locked: a no-op.
        unlock_worktree(&base, &wt);
        std::fs::remove_dir_all(&root).unwrap();
    }
}