devsandbox 0.7.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
use std::collections::BTreeMap;
use std::io::{IsTerminal, Write};
use std::path::{Path, PathBuf};

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

use crate::config::{build_hash, short_hash, substitute, Config, MountContext, ResolvedSandbox, CONFIG_FILE};
use crate::runtime::{backend, ServiceEndpoint, NAME_PREFIX};
use crate::state::{Instance, State};

use super::{container_drifted, pick, services};

mod editor;
mod folders;
mod git;
mod image;
mod lifecycle;
mod mounts;
mod ssh_agent;
mod worktree;

pub(crate) use editor::write_vscode_name_config;
pub(crate) use git::{check_repo, host_git};
pub(crate) use lifecycle::exec_lifecycle;
pub(crate) use ssh_agent::{
    ssh_agent_dir, ssh_agent_link_path, ssh_agent_refresh, SSH_AGENT_TARGET,
};
use image::{feature_container_opts, image_for, privileged_decision};
use lifecycle::run_host_commands;
use mounts::{
    merge_volumes, provision_shell_history, resolve_caches, resolve_folders, resolve_mounts,
    resolve_shell_rc, shell_rc_wiring, sort_parents_first, write_workspace_file, ResolvedMounts,
};
use ssh_agent::ssh_agent_forward;
use worktree::{copy_worktree_includes, create_worktree, git_companion_mount, link_shared_files};
pub(crate) use worktree::{instance_at, parse_worktree_list};

/// Branch pattern for worktree instances when neither `--branch` nor the
/// sandbox's `worktree-branch` is set. Also the TUI prompt's completion base
/// after `--branch`.
pub const DEFAULT_WORKTREE_BRANCH: &str = "sandbox/${instance}";

/// Per-instance additions from the hidden `run` flags, which a dispatcher's
/// control requests use (`commands::dispatch`). Not part of the config, so
/// not hashed.
#[derive(Debug, Clone, Default)]
pub struct RunExtras {
    /// Extra `-e K=V` for this instance only, after `containerEnv` (so they
    /// win on a duplicate key). Recorded in state (`Instance::extra_env`) so a
    /// `rebuild`, which passes none, keeps them.
    pub env: Vec<(String, String)>,
    /// `instance_id` of the owning dispatcher.
    pub dispatcher: Option<String>,
}

/// Create and start an instance; returns its name (state key), or `None` when
/// there was nothing to run and an example config was written instead.
/// Printing is the caller's: plain `run` prints the name, `run --json` a
/// [`RunRecord`], autostart and the dashboard their own lines.
pub fn run(
    dir: &Path,
    sandbox_name: Option<String>,
    instance_name: Option<String>,
    branch_override: Option<String>,
    base_override: Option<String>,
    extras: RunExtras,
) -> Result<Option<String>> {
    // The dispatcher boundary (`commands::dispatch`) checks this too; `run`'s
    // hidden flags are callable directly, so check again before any work.
    if extras.dispatcher.is_some()
        && branch_override.as_deref().is_some_and(|b| !crate::devsbd::control::valid_branch(b))
    {
        bail!("bad --branch for a dispatcher child: use {}", crate::devsbd::control::BRANCH_RULES);
    }
    if extras.dispatcher.is_some() {
        if let Some((k, _)) = extras.env.iter().find(|(k, _)| crate::devsbd::control::denied_env(k)) {
            bail!("--env `{k}` may not be set by a dispatcher");
        }
    }
    let config = match Config::load(dir) {
        Ok(config) if !config.sandboxes.is_empty() => config,
        _ => return offer_example_config(dir).map(|()| None),
    };

    let sandbox_name = match sandbox_name {
        Some(name) => name,
        None => {
            let names: Vec<&str> = config.sandboxes.keys().map(String::as_str).collect();
            if !std::io::stdin().is_terminal() {
                bail!("no sandbox specified; available: {}", names.join(", "));
            }
            names[pick("Select a sandbox", &names)?].to_string()
        }
    };
    let sandbox = config.resolve_sandbox(&sandbox_name)?;
    let props = &sandbox.properties;

    let ignored = props.ignored();
    if !ignored.is_empty() {
        eprintln!("warning: ignoring unsupported properties: {}", ignored.join(", "));
    }

    let folder = sandbox
        .folder()
        .with_context(|| format!("sandbox `{sandbox_name}` has no `folder`"))?;
    let folder = dir
        .join(folder)
        .canonicalize()
        .with_context(|| format!("sandbox folder `{folder}` does not exist"))?;
    let basename = folder
        .file_name()
        .context("sandbox folder has no basename")?
        .to_string_lossy()
        .into_owned();
    // The mount context is rebuilt inside `materialize` for the actual mounts;
    // here it only substitutes `${instance}` into the worktree branch pattern,
    // which is why instance naming (and id allocation) comes first.
    let config_dir = dir.canonicalize().unwrap_or_else(|_| dir.to_path_buf());
    let config_dir_str = config_dir.to_string_lossy().into_owned();
    let folder_str = folder.to_string_lossy().into_owned();
    let shared_volumes = config_dir.join("shared-volumes").to_string_lossy().into_owned();

    let mut state = State::load()?;
    // Deterministic name: explicit --name, or the sandbox name when free, else
    // the first free ordinal. `run` always creates a fresh instance; `start`
    // restarts a stopped one.
    let instance = match instance_name {
        Some(name) => name,
        None => default_instance_name(&state, &sandbox_name),
    };

    // `run` always creates a fresh instance; it never restarts a stopped one
    // (that is `devsandbox start`).
    if state.instances.contains_key(&instance) {
        bail!(
            "instance `{instance}` already exists; `devsandbox start {instance}` restarts it, \
             `devsandbox rm {instance}` frees the name"
        );
    }
    // Persistent id (see `Instance::instance_id`): the name, unless a rename
    // left that id taken. Everything host/runtime-addressed — container name,
    // `${instance}` substitutions, worktree, services — keys off the id so a
    // later rename moves nothing.
    let instance_id = unique_instance_id(&state, &instance);
    let container_name = format!("{NAME_PREFIX}{instance_id}");
    if backend().is_running(&container_name)?.is_some() {
        let labeled = backend().label(&container_name, DISPATCHER_LABEL)?;
        let leftovers: Vec<PathBuf> = [
            config_dir.join(".worktrees").join(&instance_id),
            config_dir.join(".worktrees").join(format!("{instance_id}.folders")),
        ]
        .into_iter()
        .filter(|p| p.exists())
        .collect();
        orphan_check(&container_name, extras.dispatcher.as_deref(), labeled.as_deref(), &leftovers)?;
        eprintln!(
            "warning: removing orphaned container `{container_name}`: a child of this dispatcher \
             no longer in state, with no worktree left"
        );
        let project = services::project_id(&config_dir)?;
        super::rm::remove_container(&container_name, &project, &instance_id);
    }

    let var_ctx = MountContext {
        config_dir: &config_dir_str,
        workspace_folder: &folder_str,
        workspace_folder_basename: &basename,
        shared_volumes: &shared_volumes,
        instance: &instance_id,
    };

    // A dispatcher's child always gets a worktree, even as the folder's first
    // instance: an unattended agent must never work in the user's checkout.
    let (source, worktree, branch, branch_created) = if base_in_use(&state, &folder) || extras.dispatcher.is_some() {
        // Only a named branch can already exist on the remote; generated
        // pattern names are new by construction (see `create_worktree`).
        let explicit_branch = branch_override.is_some();
        let branch = worktree_branch(
            branch_override,
            extras.dispatcher.is_some(),
            props.worktree_branch.as_deref(),
            &var_ctx,
        );
        // Must be absolute: `create_worktree` runs `git -C <base>`, so a
        // `dir`-relative path would resolve under the base repo instead of here,
        // and the mount/state would point at a different (empty) directory.
        // Keyed by id: a renamed instance keeps its worktree dir, so a new
        // instance reusing the freed name must not collide with it.
        let wt = config_dir.join(".worktrees").join(&instance_id);
        // Start point: `--base`, else the sandbox's `worktree-base`, else
        // detected from the remote (see `worktree_start_point`).
        let base_ref = base_override.or_else(|| props.worktree_base.clone());
        let created =
            create_worktree(&folder, &wt, &branch, base_ref.as_deref(), explicit_branch, &state)?;
        (wt.clone(), Some(wt), Some(branch), created)
    } else {
        (folder.clone(), None, None, true)
    };

    materialize(
        dir,
        &config,
        &sandbox_name,
        &sandbox,
        &instance,
        &instance_id,
        &source,
        &folder,
        worktree,
        branch,
        branch_created,
        &extras,
        &mut state,
    )?;

    Ok(Some(instance))
}

/// Whether `run` may replace `container`, which exists but has no state
/// entry. Only for a dispatcher's run, when the container carries that
/// dispatcher's label (`labeled`): its children have deterministic names, so
/// without this an orphaned child blocks its key's `ensure` for good, and
/// the dispatcher can't reach the host to clear it. And only with no
/// worktree left (`leftovers`), so nothing `rm`'s dirty-worktree check
/// protects is lost; the work of a devcontainer lives in its worktree, not
/// in the container. Everything else is refused, with what to do on the host.
fn orphan_check(
    container: &str,
    dispatcher: Option<&str>,
    labeled: Option<&str>,
    leftovers: &[PathBuf],
) -> Result<()> {
    let Some(dispatcher) = dispatcher.filter(|d| labeled == Some(*d)) else {
        bail!(
            "container `{container}` already exists but is not in state; \
             remove it or pick another --name"
        );
    };
    if let Some(path) = leftovers.first() {
        bail!(
            "container `{container}` (a child of dispatcher `{dispatcher}`) already exists but is \
             not in state, and its worktree `{}` is still on disk; check it for work, then remove \
             both on the host (`git worktree remove`, then the container)",
            path.display()
        );
    }
    Ok(())
}

/// `run --json` payload: where the new instance lives, so a programmatic
/// caller can exec into it or open its folder without a follow-up `status`.
/// Field names and meanings match [`crate::snapshot::InstanceRow`] where they
/// overlap, except `worktree`, which is the path here (not a flag): a caller
/// that just created the instance needs to know where it is. Runtime-derived
/// fields (status, cpu, drift) are left to `status --json`.
#[derive(Debug, serde::Serialize)]
pub struct RunRecord {
    /// Instance name (state key); what every `<name>` argument accepts.
    pub name: String,
    /// Persistent id (`Instance::instance_id`).
    pub instance_id: String,
    pub sandbox: String,
    pub container: String,
    /// Workspace folder inside the container.
    pub workspace: String,
    /// Host folder mounted as the workspace (the worktree for a worktree instance).
    pub folder: String,
    /// Host base repo folder the instance derives from.
    pub base_folder: String,
    /// Host worktree path, `None` when the instance runs on `base_folder`.
    pub worktree: Option<String>,
    /// Worktree branch, `None` without a worktree.
    pub branch: Option<String>,
}

impl RunRecord {
    pub fn new(name: &str, info: &Instance) -> Self {
        let path = |p: &Path| p.display().to_string();
        RunRecord {
            name: name.to_string(),
            instance_id: info.instance_id.clone(),
            sandbox: info.sandbox.clone(),
            container: info.container.clone(),
            workspace: info.workspace.clone(),
            folder: path(&info.folder),
            base_folder: path(&info.base_folder),
            worktree: info.worktree.as_deref().map(path),
            branch: info.branch.clone(),
        }
    }
}

/// The enveloped `run --json` document for instance `key`, read back from
/// state (what `run` just recorded).
pub fn run_record_json(key: &str) -> Result<String> {
    let state = State::load()?;
    let info = state
        .instances
        .get(key)
        .with_context(|| format!("instance `{key}` missing from state after run"))?;
    serde_json::to_string_pretty(&super::status::Envelope::new(RunRecord::new(key, info)))
        .context("serialize run record")
}

/// Branch for a new worktree: the `--branch` override, else the sandbox's
/// `worktree-branch`, else the default. Patterns get `${instance}` (and the
/// other mount variables) substituted so each instance gets a unique branch.
/// A dispatcher's override is used verbatim: it comes from a container, and
/// substitution would expand `${localEnv:…}` from the host's environment.
fn worktree_branch(
    branch_override: Option<String>,
    dispatched: bool,
    pattern: Option<&str>,
    ctx: &MountContext,
) -> String {
    match branch_override {
        Some(branch) if dispatched => branch,
        Some(pattern) => substitute(&pattern, ctx),
        None => substitute(pattern.unwrap_or(DEFAULT_WORKTREE_BRANCH), ctx),
    }
}

/// The `run --env` set an instance gets and records: the given one (last
/// value wins on a duplicate key, as with `-e`), or, when none is given (a
/// `rebuild`), the one recorded on the entry being replaced. Recorded values
/// passed the `run` boundary's checks when first given, so they aren't
/// re-checked.
fn effective_extra_env(
    given: &[(String, String)],
    prior: Option<&BTreeMap<String, String>>,
) -> BTreeMap<String, String> {
    if given.is_empty() {
        prior.cloned().unwrap_or_default()
    } else {
        given.iter().cloned().collect()
    }
}

/// Create (and start) the instance container and record it: run
/// `initializeCommand`, bring up services, build the mount set (rebuilding the
/// `${instance}` context from `instance_id` so per-instance mounts/history
/// resolve to the same host paths across renames), start the container, upsert
/// state (saved before the lifecycle chain so a crash mid-lifecycle leaves the
/// container tracked), then run the lifecycle commands. `instance` is the
/// display name (state key); `instance_id` the persistent identity every
/// container/service/mount name derives from. `source` is what gets mounted as
/// the working tree (a worktree when `worktree.is_some()`, else `folder`);
/// `folder` is the canonicalized base folder, used for `base_folder` and the
/// git companion mount. A worktree gets the `.worktreeinclude` copies on every
/// call, `rebuild` included, so adding a pattern and rebuilding (the fix the
/// resulting drift points at) actually delivers the file; the copy never
/// overwrites, so edits in the worktree survive. `branch_created` is recorded for `rm` (see `Instance::branch_created`).
/// `extras.dispatcher` falls back to the entry being replaced, as do the
/// recorded `config_dir` and extra env (`effective_extra_env`), so a
/// `rebuild` keeps all three.
#[allow(clippy::too_many_arguments)]
pub(crate) fn materialize(
    dir: &Path,
    config: &Config,
    sandbox_name: &str,
    sandbox: &ResolvedSandbox,
    instance: &str,
    instance_id: &str,
    source: &Path,
    folder: &Path,
    worktree: Option<PathBuf>,
    branch: Option<String>,
    branch_created: bool,
    extras: &RunExtras,
    state: &mut State,
) -> Result<()> {
    let props = &sandbox.properties;
    let prior = state.instances.get(instance);
    let dispatcher = extras
        .dispatcher
        .clone()
        .or_else(|| prior.and_then(|i| i.dispatcher.clone()));
    let prior_config_dir = prior.and_then(|i| i.config_dir.clone());
    // A rebuild keeps the instance's host ports (docs/port-forwarding.md).
    let forwarded_ports = prior.map(|i| i.forwarded_ports.clone()).unwrap_or_default();
    // A rebuild keeps the done flag: recreating the container doesn't mean
    // the user is back on it (only `undone`/`ensure` clear it).
    let done = prior.and_then(|i| i.done);
    // A rebuild keeps its `folders` worktrees (see `folders::mount_folders`).
    let prior_folders = prior.map(|i| i.folders.clone()).unwrap_or_default();
    let run_env = effective_extra_env(&extras.env, prior.map(|i| &i.extra_env));
    let container_name = format!("{NAME_PREFIX}{instance_id}");
    let basename = folder
        .file_name()
        .context("sandbox folder has no basename")?
        .to_string_lossy()
        .into_owned();
    // `${configDir}` / `${localWorkspaceFolder(Basename)}` / `${sharedVolumes}`
    // / `${instance}` are usable in `workspaceFolder`, `mounts`, and cache
    // sources; rebuild the context from the persistent id so
    // `${instance}`-anchored mounts/history resolve to the same host paths
    // across rebuilds *and* renames.
    let config_dir = dir.canonicalize().unwrap_or_else(|_| dir.to_path_buf());
    let config_dir_str = config_dir.to_string_lossy().into_owned();
    let folder_str = folder.to_string_lossy().into_owned();
    let shared_volumes = config_dir.join("shared-volumes").to_string_lossy().into_owned();
    let var_ctx = MountContext {
        config_dir: &config_dir_str,
        workspace_folder: &folder_str,
        workspace_folder_basename: &basename,
        shared_volumes: &shared_volumes,
        instance: instance_id,
    };
    let workspace = substitute(
        &props
            .workspace_folder
            .clone()
            .unwrap_or_else(|| format!("/workspaces/{basename}")),
        &var_ctx,
    );
    // Extra workspace roots (`folders`): bind-mounted at their container path
    // and listed in the generated `.code-workspace` after the primary folder.
    let extra_folders = resolve_folders(dir, &var_ctx, &workspace, props)?;

    // initializeCommand runs on the host, before anything is created.
    if let Some(cmd) = &props.initialize_command {
        run_host_commands(dir, cmd).context("initializeCommand failed")?;
    }

    // Shared files (`worktree-link`), then per-worktree copies: after
    // initializeCommand so files it generates get adopted, before the
    // container so lifecycle commands see them. Links go first because the
    // copy never overwrites, so a path in both lists ends up linked.
    let links = props.worktree_link.clone().unwrap_or_default();
    let link_store = if links.is_empty() {
        None
    } else {
        let store = link_store(&config_dir, folder, &basename);
        let mut trees = vec![folder];
        if worktree.is_some() {
            trees.push(source);
        }
        link_shared_files(&store, &trees, &links)?;
        Some(store)
    };
    if worktree.is_some() {
        let patterns = props.worktree_include.clone().unwrap_or_default();
        copy_worktree_includes(folder, source, &patterns);
    }

    // `folders` worktrees: after initializeCommand (it may create the repos),
    // before the container, like the primary's links above.
    let folder_mounts = folders::mount_folders(
        config,
        dir,
        &config_dir,
        state,
        instance,
        instance_id,
        dispatcher.is_some(),
        &prior_folders,
        &extra_folders,
    )?;

    // Bring up the sandbox's services (global shared + this instance's isolated)
    // and their networks; the instance container joins them to reach services by
    // name.
    let project = services::project_id(dir)?;
    let service_names = props.services.clone().unwrap_or_default();
    let (networks, endpoints) =
        services::ensure_services(config, dir, &project, instance_id, &service_names)?;

    // The worktree's `.git` file points at `<base>/.git/worktrees/..` by absolute
    // host path; mount the base `.git` at the identical path so git works inside
    // the container.
    let mut extra_mounts = match &worktree {
        Some(_) => vec![git_companion_mount(folder)],
        None => Vec::new(),
    };
    // The links are absolute symlinks into the store; mounting the store dir at
    // the same path makes them resolve in the container. A dir mount, so it
    // works on Apple `container` too.
    if let Some(store) = &link_store {
        extra_mounts.push(format!("{}:{}", store.display(), store.display()));
    }
    // Each `folders` worktree needs its base's `.git` too; once per base (the
    // primary may already mount the same one).
    for fm in folder_mounts.iter().filter(|fm| fm.worktree.is_some()) {
        let companion = git_companion_mount(&fm.base);
        if !extra_mounts.contains(&companion) {
            extra_mounts.push(companion);
        }
    }
    // ssh-agent: relay-first. On unix with an embedded helper we never mount
    // the socket — the in-container `devsbd` daemon serves it over exec stdio
    // (docs/sandbox-helper.md), which also fixes rotation-while-running and
    // works on runtimes that can't bind files. Only builds without a helper
    // (`cargo install`) and non-unix hosts fall back to the bind mount.
    let relay_first = cfg!(unix) && crate::devsbd::embedded();
    let ssh_auth_sock = if relay_first {
        None
    } else {
        match ssh_agent_forward(instance_id)? {
            Some((mount, target)) => {
                extra_mounts.push(mount);
                Some(target)
            }
            None => None,
        }
    };
    let ResolvedMounts { args: mut mounts, volumes: mut instance_volumes } =
        resolve_mounts(dir, folder, &basename, instance_id, sandbox)?;
    for fm in &folder_mounts {
        mounts.push(format!(
            "type=bind,source={},target={}",
            fm.worktree.as_deref().unwrap_or(&fm.base).display(),
            fm.target
        ));
    }
    // Package-manager caches: shared bind mounts + the env vars pointing at them.
    let (cache_mounts, cache_env) = resolve_caches(&config_dir, props)?;
    mounts.extend(cache_mounts);
    // Managed per-instance shell history: mount the host dir, point zsh at it
    // (env var for rc-less shells, rc export below for shells whose rc chain
    // clobbers the env, e.g. VS Code's injected shell integration).
    let mut extra_env = cache_env;
    let shell_history = if props.persist_shell_history == Some(true) {
        let (path, mount, env) = provision_shell_history(dir, instance_id)?;
        mounts.push(mount);
        extra_env.push(env);
        Some(path)
    } else {
        None
    };
    extra_env.extend(run_env.iter().map(|(k, v)| (k.clone(), v.clone())));
    // `shell-rc`: host snippets mounted read-only, sourced by the rc files.
    let shell_rc = resolve_shell_rc(dir, &var_ctx, props)?;
    mounts.extend(shell_rc.iter().map(|(mount, _)| mount.clone()));
    // Feature-declared mounts come last: any target claimed above (or the
    // workspace itself) wins, so the config can override a feature's mount.
    let feature = feature_container_opts(props, &var_ctx, &mounts, &workspace)?;
    mounts.extend(feature.mounts);
    instance_volumes.extend(feature.volumes);
    sort_parents_first(&mut mounts);
    let (privileged, warnings) = privileged_decision(
        props.privileged == Some(true),
        &feature.privileged_by,
        backend().supports_privileged(),
    );
    for warning in warnings {
        eprintln!("warning: {warning}");
    }
    let (restart, warnings) = crate::commands::autostart::restart_decision(
        props.autostart,
        backend().supports_restart_policy(),
        backend().name(),
    );
    for warning in warnings {
        eprintln!("warning: {warning}");
    }
    run_container(
        dir,
        sandbox,
        &container_name,
        source,
        folder,
        &workspace,
        &extra_mounts,
        &mounts,
        &extra_env,
        &networks,
        &endpoints,
        privileged,
        restart,
        dispatcher.as_deref(),
    )?;
    let container = container_name.clone();
    // Before lifecycle commands, so they could already rely on the helper. In
    // relay-first mode a failure means no ssh-agent forwarding at all (no mount
    // fallback), so word the note that way; otherwise the plain install note.
    let devsbd_arch = match crate::devsbd::ensure_or_reason(&container, None) {
        Ok(arch) => Some(arch),
        Err(reason) => {
            if relay_first {
                eprintln!("note: ssh-agent forwarding off in `{container}`: {reason}");
            } else {
                eprintln!("note: {reason}");
            }
            None
        }
    };
    let workspace_file = write_workspace_file(
        &container,
        instance,
        &workspace,
        &extra_folders,
        props.vscode_extensions().unwrap_or(&[]),
    );

    let persist_history = shell_history.is_some();
    // Keep volumes a previous run recorded: a rebuild keeps them on disk even
    // when the config no longer mounts them, and `rm` must still find them.
    let volumes = merge_volumes(
        state.instances.get(instance).map(|i| i.volumes.as_slice()).unwrap_or_default(),
        instance_volumes,
    );
    let record = Instance {
        sandbox: sandbox_name.to_string(),
        instance_id: instance_id.to_string(),
        project,
        container: container.clone(),
        folder: source.to_path_buf(),
        base_folder: folder.to_path_buf(),
        worktree,
        branch,
        branch_created,
        folders: folders::merge_folder_mounts(&prior_folders, folder_mounts),
        shell_history,
        workspace: workspace.clone(),
        workspace_file,
        remote_env: props.remote_env.clone().unwrap_or_default(),
        remote_user: props.remote_user.clone(),
        ssh_auth_sock,
        devsbd_arch,
        volumes,
        dispatcher,
        config_dir: Some(prior_config_dir.unwrap_or(config_dir)),
        extra_env: run_env,
        forwarded_ports,
        done,
        created_unix: Instance::now(),
    };
    // Into the current file, not `state` (loaded before the build): saving that
    // snapshot would drop instances other commands recorded meanwhile.
    State::update(|fresh| {
        fresh.instances.insert(instance.to_string(), record.clone());
        Ok(())
    })?;
    state.instances.insert(instance.to_string(), record);

    let extensions = props.vscode_extensions().unwrap_or(&[]);
    if !extensions.is_empty() || props.remote_user.is_some() {
        write_vscode_name_config(&container, Some(extensions), props.remote_user.as_deref())?;
    }

    // ssh-agent for the lifecycle chain. The instance was just saved, so read
    // the agent rule off it (mount → always; relay → only with a host agent).
    // In relay mode the daemon's bridge (`bridges.ensure`; saved state is what
    // the daemon reads) is waited for before the chain starts (its commands
    // may clone over ssh at once) and spans shell-rc wiring + all five
    // commands, the connection held until this fn returns (`?` paths
    // included). Not ready: one note before the chain, which runs anyway.
    let inst = state.instances.get(instance).expect("instance just inserted");
    // Same env as `devsandbox exec`: remoteEnv, then the saved `--env`.
    let exec_env = inst.exec_env();
    let host_agent = crate::commands::exec::has_host_agent();
    let ssh_auth_sock = crate::commands::exec::ssh_auth_sock_env(inst, host_agent);
    // Record the postStartCommand for the boot hook (the host still runs it
    // below: the hook can't have, the helper wasn't installed yet).
    let boot = boot_spec_for(
        props.autostart,
        devsbd_arch.is_some(),
        props.post_start_command.as_ref(),
        &workspace,
        Some(&exec_env),
        props.remote_user.as_deref(),
        ssh_auth_sock,
    );
    crate::devsbd::sync_boot(&container, boot.as_ref(), false);
    // Nothing to run in the container: no bridge to wait for.
    let lifecycle = !shell_rc.is_empty()
        || persist_history
        || [
            &props.on_create_command,
            &props.update_content_command,
            &props.post_create_command,
            &props.post_start_command,
            &props.post_attach_command,
        ]
        .iter()
        .any(|c| c.is_some());
    #[cfg(unix)]
    let _relay = (lifecycle && crate::devsbd::relay_mode(inst) && host_agent)
        .then(|| crate::commands::exec::AgentRelay::ready(instance, &container));
    #[cfg(not(unix))]
    let _ = lifecycle;

    if !shell_rc.is_empty() || persist_history {
        let paths: Vec<&str> = shell_rc.iter().map(|(_, path)| path.as_str()).collect();
        exec_lifecycle(
            &container,
            &workspace,
            Some(&exec_env),
            props.remote_user.as_deref(),
            ssh_auth_sock,
            &shell_rc_wiring(&paths, persist_history),
        )
        .with_context(|| format!("shell-rc wiring failed (container `{container}` kept)"))?;
    }

    for (name, cmd) in [
        ("onCreateCommand", &props.on_create_command),
        ("updateContentCommand", &props.update_content_command),
        ("postCreateCommand", &props.post_create_command),
        ("postStartCommand", &props.post_start_command),
        ("postAttachCommand", &props.post_attach_command),
    ] {
        if let Some(cmd) = cmd {
            exec_lifecycle(
                &container,
                &workspace,
                Some(&exec_env),
                props.remote_user.as_deref(),
                ssh_auth_sock,
                cmd,
            )
            .with_context(|| format!("{name} failed (container `{container}` kept)"))?;
        }
    }

    Ok(())
}

/// The boot file a container should carry: the `postStartCommand` when the
/// runtime restarts the container itself (`autostart = "runtime"` on a backend
/// with restart policies) and the helper that runs it is installed; `None`
/// (remove the file) otherwise. `env` is the instance's `Instance::exec_env`.
/// The file is rewritten on `run`/`start`/`rebuild` only (and on `start`
/// after the hook already fired, see `devsbd::sync_boot`), so a saved env an
/// `ensure` changed reaches the hook from the start after the next one.
#[allow(clippy::too_many_arguments)]
pub(crate) fn boot_spec_for(
    autostart: Option<crate::config::Autostart>,
    helper: bool,
    post_start: Option<&crate::config::LifecycleCommand>,
    workspace: &str,
    env: Option<&std::collections::BTreeMap<String, String>>,
    remote_user: Option<&str>,
    ssh_auth_sock: Option<&str>,
) -> Option<crate::devsbd::bootfile::BootSpec> {
    let runtime = crate::commands::autostart::runtime_restarts(
        autostart,
        backend().supports_restart_policy(),
    );
    let cmd = post_start.filter(|_| runtime && helper)?;
    Some(crate::devsbd::boot_spec(cmd, workspace, env, remote_user, ssh_auth_sock))
}

/// The `worktree-link` store for base repo `folder` (canonical). One per repo,
/// not per sandbox: the base checkout holds a single link per path, so two
/// sandboxes on one folder must share the copy it points at. The basename
/// keeps it readable; the path hash keeps two same-named repos apart.
fn link_store(config_dir: &Path, folder: &Path, basename: &str) -> PathBuf {
    let hash = short_hash(&folder.to_string_lossy());
    config_dir.join("shared-files").join(format!("{basename}-{}", &hash[..8]))
}

/// Whether some instance (running or stopped — a stopped one can be started
/// anytime) already mounts `folder` directly, as its working tree or as a
/// `folders` entry without a worktree; the new instance then gets a git
/// worktree so two containers never share a working tree. Matched on the
/// mounted source, not `base_folder`: worktree instances carry the base's
/// `base_folder` but have their own working tree, so they must not keep the
/// base checkout reserved after its direct-mount instance is removed.
fn base_in_use(state: &State, folder: &Path) -> bool {
    folders::folder_directly_mounted(state, folder, None)
}

/// Default instance name: the sandbox name when it is free in state, else the
/// first free `<sandbox>-<n>` ordinal (n >= 2). Stopped instances keep their
/// name (`start` restarts them), so a taken name always means a new ordinal.
fn default_instance_name(state: &State, sandbox: &str) -> String {
    if !state.instances.contains_key(sandbox) {
        return sandbox.to_string();
    }
    first_free_ordinal(state, sandbox)
}

/// First free `<sandbox>-<n>` (n >= 2) not present in state.
fn first_free_ordinal(state: &State, sandbox: &str) -> String {
    (2..)
        .map(|n| format!("{sandbox}-{n}"))
        .find(|name| !state.instances.contains_key(name))
        .expect("infinite range yields a free name")
}

/// Persistent id for a new instance named `name`: `name` itself when no
/// existing instance holds that id, else `name-<i>` (first free i >= 1). Ids
/// survive renames, so a rename can free a *name* whose id is still taken —
/// the suffix keeps ids (and everything derived from them: container, mounts,
/// worktree, services) unique while the instance still displays as `name`.
fn unique_instance_id(state: &State, name: &str) -> String {
    let taken = |id: &str| state.instances.values().any(|i| i.instance_id == id);
    if !taken(name) {
        return name.to_string();
    }
    (1..)
        .map(|i| format!("{name}-{i}"))
        .find(|id| !taken(id))
        .expect("infinite range yields a free id")
}

/// Warn when the reused container's recorded config or build hash differs from
/// the freshly resolved one (the shared [`container_drifted`] rule, so a
/// dockerfile edit warns too). Containers without the labels (None) — skip.
pub(crate) fn warn_on_drift(dir: &Path, container: &str, sandbox: &ResolvedSandbox) -> Result<()> {
    let build = build_hash(dir, sandbox.properties.build.as_ref());
    if container_drifted(container, &sandbox.config_hash, &build)? {
        // Suggest a copy-pasteable command: strip the container prefix to the
        // instance name, falling back to the container name if it is unprefixed.
        let instance = container.strip_prefix(NAME_PREFIX).unwrap_or(container);
        eprintln!(
            "warning: config for `{container}` changed since it was created; \
             run `devsandbox rebuild {instance}` to apply changes"
        );
    }
    Ok(())
}

fn run_container(
    dir: &Path,
    sandbox: &ResolvedSandbox,
    container: &str,
    source: &Path,
    base_folder: &Path,
    workspace: &str,
    extra_mounts: &[String],
    mounts: &[String],
    extra_env: &[(String, String)],
    networks: &[String],
    endpoints: &[ServiceEndpoint],
    privileged: bool,
    restart: Option<&str>,
    dispatcher: Option<&str>,
) -> Result<()> {
    let props = &sandbox.properties;
    let image = image_for(dir, sandbox)?;
    let mount = format!("{}:{workspace}", source.display());
    let sandbox_label = format!("devsandbox.sandbox={}", sandbox.name);
    let instance = container.strip_prefix(NAME_PREFIX).unwrap_or(container);
    let instance_label = format!("devsandbox.instance={instance}");
    let hash_label = format!("devsandbox.config_hash={}", sandbox.config_hash);
    let build_hash_label = format!(
        "devsandbox.build_hash={}",
        build_hash(dir, sandbox.properties.build.as_ref())
    );
    let base_label = format!("devsandbox.base_folder={}", base_folder.display());
    let boot_hook_label = format!("{BOOT_HOOK_LABEL}=1");
    let mut args: Vec<String> = [
        "run", "-d", "--name", container,
        "--label", &sandbox_label, "--label", &instance_label,
        "--label", &hash_label, "--label", &build_hash_label, "--label", &base_label,
        "--label", &boot_hook_label,
        "-v", &mount, "-w", workspace,
    ]
    .map(str::to_string)
    .into();
    if props.init == Some(true) {
        args.push("--init".into());
    }
    if privileged {
        args.push("--privileged".into());
    }
    if let Some(policy) = restart {
        args.push("--restart".into());
        args.push(policy.into());
    }
    if let Some(id) = dispatcher {
        args.push("--label".into());
        args.push(format!("{DISPATCHER_LABEL}={id}"));
    }
    if let Some(user) = &props.container_user {
        args.push("--user".into());
        args.push(user.clone());
    }
    for mount in extra_mounts {
        args.push("-v".into());
        args.push(mount.clone());
    }
    for mount in mounts {
        args.push("--mount".into());
        args.push(mount.clone());
    }
    // Runtimes differ in how many networks a container can start on; the
    // backend attaches the rest after creation.
    args.extend(backend().network_run_args(networks));
    if let Some(env) = &props.container_env {
        for (key, value) in env {
            args.push("-e".into());
            args.push(format!("{key}={value}"));
        }
    }
    for (key, value) in extra_env {
        args.push("-e".into());
        args.push(format!("{key}={value}"));
    }
    args.push(image);
    args.extend(container_command());

    let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect();
    backend().run_checked(&arg_refs)?;

    backend().connect_networks(container, networks)?;
    backend().wire_service_dns(container, endpoints)?;
    Ok(())
}

/// Label marking containers whose command runs the boot hook
/// ([`container_command`]); older ones run a bare `sleep infinity`, so `start`
/// must keep running their `postStartCommand` from the host.
pub(crate) const BOOT_HOOK_LABEL: &str = "devsandbox.boot_hook";

/// Label carrying the owning dispatcher's `instance_id` on a child container.
pub(crate) const DISPATCHER_LABEL: &str = "devsandbox.dispatcher";

/// The container command. Same trick as devcontainers: keep the container
/// alive, work happens via exec. Before the keep-alive, in the background,
/// `devsbd boot` brings back what the host set up via exec (the daemon, and
/// `postStartCommand` in runtime mode) when the runtime restarts the container
/// by itself (docs/automations.md). On first create the helper isn't installed
/// yet, so the `-x` test makes the hook a no-op. It reaches a feature
/// entrypoint chain as its `"$@"` like any command.
fn container_command() -> Vec<String> {
    let bin = crate::devsbd::BIN;
    vec!["sh".into(), "-c".into(), format!("[ -x {bin} ] && {bin} boot & exec sleep infinity")]
}

fn offer_example_config(dir: &Path) -> Result<()> {
    let existing = crate::config::find(dir)?;
    let path = existing.clone().unwrap_or_else(|| dir.join(CONFIG_FILE));
    eprintln!("No sandboxes defined in {}.", path.display());
    if !std::io::stdin().is_terminal() {
        bail!("nothing to run");
    }
    eprint!("Generate an example config? [y/N] ");
    std::io::stderr().flush()?;
    let mut answer = String::new();
    std::io::stdin().read_line(&mut answer)?;
    if !matches!(answer.trim(), "y" | "Y") {
        bail!("nothing to run");
    }
    if existing.is_some() {
        bail!("{} already exists, not overwriting", path.display());
    }
    std::fs::write(&path, EXAMPLE_CONFIG)
        .with_context(|| format!("cannot write {}", path.display()))?;
    eprintln!("Wrote {}. Edit it, then re-run `devsandbox run`.", path.display());
    Ok(())
}

const EXAMPLE_CONFIG: &str = r#"# devsandbox example config

[template.base]
caches = ["pnpm"]

[sandbox.example]
extends = "base"
folder = "../example"
image = "mcr.microsoft.com/devcontainers/base:ubuntu"
"#;

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

    pub(super) fn instance(name: &str) -> Instance {
        Instance {
            sandbox: "repo".into(),
            instance_id: name.into(),
            project: "proj1234".into(),
            container: format!("{NAME_PREFIX}{name}"),
            folder: "/tmp/repo".into(),
            base_folder: "/tmp/repo".into(),
            worktree: None,
            branch: None,
            branch_created: true,
            folders: Vec::new(),
            shell_history: None,
            workspace: "/workspaces/repo".into(),
            workspace_file: None,
            remote_env: Default::default(),
            remote_user: None,
            ssh_auth_sock: None,
            devsbd_arch: None,
            volumes: Vec::new(),
            dispatcher: None,
            config_dir: None,
            extra_env: Default::default(),
            forwarded_ports: Default::default(),
            done: None,
            created_unix: 0,
        }
    }

    /// Only this dispatcher's own orphaned child, with no worktree left, is
    /// replaced; anything else keeps the "not in state" refusal.
    #[test]
    fn orphan_check_reclaims_only_a_bare_child_of_this_dispatcher() {
        let c = "devsandbox-web-pr-1";
        let err = |d, l, left: &[PathBuf]| orphan_check(c, d, l, left).unwrap_err().to_string();
        orphan_check(c, Some("disp"), Some("disp"), &[]).unwrap();
        // A user's run, or a container with no or another dispatcher's label.
        assert!(err(None, Some("disp"), &[]).contains("not in state; remove it or pick another --name"));
        assert!(err(Some("disp"), None, &[]).contains("pick another --name"));
        assert!(err(Some("disp"), Some("other"), &[]).contains("pick another --name"));
        // Its worktree is still there: may hold work, so the host decides.
        let wt = PathBuf::from("/cfg/.worktrees/web-pr-1");
        let msg = err(Some("disp"), Some("disp"), &[wt]);
        assert!(msg.contains("worktree `/cfg/.worktrees/web-pr-1` is still on disk"), "{msg}");
    }

    /// The `run --json` document: schema-1 envelope, host paths as strings,
    /// `worktree`/`branch` null without a worktree and set with one.
    #[test]
    fn run_record_serializes_in_the_envelope() {
        let doc = |key: &str, info: &Instance| {
            let json = serde_json::to_string(&crate::commands::status::Envelope::new(RunRecord::new(key, info)));
            serde_json::from_str::<serde_json::Value>(&json.unwrap()).unwrap()
        };
        let base = doc("repo", &instance("repo"));
        assert_eq!(
            base,
            serde_json::json!({"schema": 1, "data": {
                "name": "repo",
                "instance_id": "repo",
                "sandbox": "repo",
                "container": "devsandbox-repo",
                "workspace": "/workspaces/repo",
                "folder": "/tmp/repo",
                "base_folder": "/tmp/repo",
                "worktree": null,
                "branch": null,
            }})
        );

        let mut wt = instance("repo-2");
        wt.folder = "/cfg/.worktrees/repo-2".into();
        wt.worktree = Some("/cfg/.worktrees/repo-2".into());
        wt.branch = Some("sandbox/repo-2".into());
        let data = &doc("renamed", &wt)["data"];
        assert_eq!(data["name"], "renamed");
        assert_eq!(data["instance_id"], "repo-2");
        assert_eq!(data["folder"], "/cfg/.worktrees/repo-2");
        assert_eq!(data["worktree"], "/cfg/.worktrees/repo-2");
        assert_eq!(data["branch"], "sandbox/repo-2");
    }

    #[test]
    fn extra_env_falls_back_to_the_prior_entry_only_when_none_given() {
        let prior = BTreeMap::from([("PR_NUMBER".to_string(), "7".to_string())]);
        // `rebuild` passes no env: the recorded one is kept.
        assert_eq!(effective_extra_env(&[], Some(&prior)), prior);
        assert!(effective_extra_env(&[], None).is_empty());
        // Given env replaces the recorded one (not merged); a duplicate key
        // keeps the last value, as `-e` would.
        let given = vec![
            ("A".to_string(), "1".to_string()),
            ("A".to_string(), "2".to_string()),
        ];
        assert_eq!(
            effective_extra_env(&given, Some(&prior)),
            BTreeMap::from([("A".to_string(), "2".to_string())])
        );
    }

    #[test]
    fn worktree_branch_substitutes_only_trusted_patterns() {
        let ctx = MountContext {
            config_dir: "/cfg",
            workspace_folder: "/src/repo",
            workspace_folder_basename: "repo",
            shared_volumes: "/cfg/shared-volumes",
            instance: "web-1",
        };
        let own = |s: &str| Some(s.to_string());
        // Config pattern and the default: substituted.
        assert_eq!(worktree_branch(None, true, Some("agent/${instance}"), &ctx), "agent/web-1");
        assert_eq!(worktree_branch(None, false, None, &ctx), "sandbox/web-1");
        // A user's own `--branch` is a pattern too.
        assert_eq!(worktree_branch(own("u/${instance}"), false, Some("x"), &ctx), "u/web-1");
        // A dispatcher's override is data, never expanded.
        assert_eq!(worktree_branch(own("d/${instance}"), true, Some("x"), &ctx), "d/${instance}");
        assert_eq!(
            worktree_branch(own("x-${localEnv:HOME}"), true, None, &ctx),
            "x-${localEnv:HOME}"
        );
    }

    #[test]
    fn run_rejects_a_bad_dispatcher_branch_before_any_work() {
        let extras = RunExtras { dispatcher: Some("d".into()), ..Default::default() };
        for bad in ["x-${localEnv:HOME}", "-x", "a..b"] {
            let dir = Path::new("/nonexistent/devsandbox-test");
            let err = run(dir, Some("web".into()), None, Some(bad.into()), None, extras.clone())
                .unwrap_err()
                .to_string();
            assert!(err.starts_with("bad --branch for a dispatcher child"), "{bad:?}: {err}");
        }
    }

    #[test]
    fn run_rejects_a_denied_dispatcher_env_before_any_work() {
        let dir = Path::new("/nonexistent/devsandbox-test");
        for name in ["PATH", "LD_PRELOAD", "Path", "GIT_SSH_COMMAND"] {
            let extras = RunExtras {
                env: vec![("OK".into(), "1".into()), (name.into(), "/tmp/p".into())],
                dispatcher: Some("d".into()),
            };
            let err = run(dir, Some("web".into()), None, None, None, extras).unwrap_err().to_string();
            assert_eq!(err, format!("--env `{name}` may not be set by a dispatcher"));
        }
    }

    #[test]
    fn ordinal_naming_skips_taken_names() {
        let mut state = State::default();
        state.instances.insert("repo".into(), instance("repo"));
        state.instances.insert("repo-2".into(), instance("repo-2"));
        assert_eq!(first_free_ordinal(&state, "repo"), "repo-3");
        // Deterministic: no random component.
        assert_eq!(first_free_ordinal(&state, "repo"), "repo-3");
    }

    #[test]
    fn default_name_is_sandbox_when_absent() {
        let state = State::default();
        assert_eq!(default_instance_name(&state, "repo"), "repo");
    }

    #[test]
    fn link_store_is_per_repo() {
        let cfg = Path::new("/cfg");
        let a = link_store(cfg, Path::new("/src/a/app"), "app");
        assert!(a.starts_with("/cfg/shared-files"), "{}", a.display());
        assert!(a.file_name().unwrap().to_string_lossy().starts_with("app-"));
        // Same folder, same store (whichever sandbox asks); same basename
        // elsewhere, a different one.
        assert_eq!(a, link_store(cfg, Path::new("/src/a/app"), "app"));
        assert_ne!(a, link_store(cfg, Path::new("/src/b/app"), "app"));
    }

    #[test]
    fn base_in_use_ignores_worktree_instances() {
        let base = Path::new("/tmp/repo");
        let mut state = State::default();
        // A worktree instance references the base via `base_folder` but mounts
        // its own tree: the base stays reclaimable for a direct mount.
        let mut wt = instance("repo-2");
        wt.folder = "/cfg/.worktrees/repo-2".into();
        wt.worktree = Some("/cfg/.worktrees/repo-2".into());
        state.instances.insert("repo-2".into(), wt);
        assert!(!base_in_use(&state, base));

        // A `folders` entry mounted through a worktree doesn't hold it either...
        let mut other = instance("other");
        other.folder = "/tmp/other".into();
        other.base_folder = "/tmp/other".into();
        other.folders.push(crate::state::FolderMount {
            target: "/workspaces/repo".into(),
            base: base.into(),
            worktree: Some("/cfg/.worktrees/other.folders/repo-12345678".into()),
        });
        state.instances.insert("other".into(), other);
        assert!(!base_in_use(&state, base));

        // ...but a direct `folders` mount does.
        state.instances.get_mut("other").unwrap().folders[0].worktree = None;
        assert!(base_in_use(&state, base));
        state.instances.remove("other");

        // A direct-mount instance (even stopped) reserves it.
        state.instances.insert("repo".into(), instance("repo"));
        assert!(base_in_use(&state, base));
    }

    #[test]
    fn default_name_ordinal_when_taken_even_if_stopped() {
        // A stopped instance keeps its name (`start` restarts it); `run` must
        // pick the next ordinal, never reuse it.
        let mut state = State::default();
        state.instances.insert("repo".into(), instance("repo"));
        assert_eq!(default_instance_name(&state, "repo"), "repo-2");
    }

    #[test]
    fn instance_id_is_name_when_free() {
        let state = State::default();
        assert_eq!(unique_instance_id(&state, "repo"), "repo");
    }

    #[test]
    fn instance_id_suffixes_when_rename_kept_the_id() {
        // `repo` was created (id `repo`) then renamed to `other`: the *name*
        // is free again but the id is not — a new `repo` gets `repo-1`.
        let mut state = State::default();
        state.instances.insert("other".into(), instance("repo"));
        assert_eq!(unique_instance_id(&state, "repo"), "repo-1");

        // A second collision (id `repo-1` also taken) moves to `repo-2`.
        state.instances.insert("other-2".into(), instance("repo-1"));
        assert_eq!(unique_instance_id(&state, "repo"), "repo-2");
    }

    // --- folders / generated workspace file ---

    #[test]
    fn container_command_hooks_then_keeps_alive() {
        assert_eq!(
            container_command(),
            vec![
                "sh",
                "-c",
                "[ -x /run/devsandbox/bin/devsbd ] && /run/devsandbox/bin/devsbd boot & exec sleep infinity",
            ]
        );
    }

    /// Docker-gated: a container running the hook command, with the helper and
    /// a boot file installed, reruns the recorded command (as the recorded
    /// user, with its env) and brings the daemon back on a runtime restart.
    #[test_utils::docker_test(helper)]
    fn boot_hook_reruns_post_start_on_restart_with_docker() -> Result<(), &'static str> {
        use std::process::Command;
        use std::time::{Duration, Instant};
        let stamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let name = format!("devsandbox-boot-test-{stamp}");
        let mut args = vec!["run".to_string(), "-d".into(), "--name".into(), name.clone(), "alpine:3.20".into()];
        args.extend(container_command());
        let up = Command::new("docker").args(&args).output().unwrap();
        assert!(up.status.success(), "{}", String::from_utf8_lossy(&up.stderr));
        let in_container = |script: &str| {
            let out = Command::new("docker").args(["exec", &name, "sh", "-c", script]).output().unwrap();
            out.status.success().then(|| String::from_utf8_lossy(&out.stdout).trim().to_string())
        };
        let poll = |script: &str| {
            let deadline = Instant::now() + Duration::from_secs(15);
            loop {
                if let Some(out) = in_container(script) {
                    return Some(out);
                }
                if Instant::now() > deadline {
                    return None;
                }
                std::thread::sleep(Duration::from_millis(100));
            }
        };

        let cleanup = || {
            let _ = Command::new("docker").args(["rm", "-f", &name]).output();
        };
        crate::test_support::with_cleanup(cleanup, || {
            crate::devsbd::install(&name, None).unwrap();
            let spec = crate::devsbd::bootfile::BootSpec {
                user: Some("nobody".into()),
                cwd: Some("/tmp".into()),
                env: vec![("MARK".into(), "ok".into())],
                cmds: vec![vec!["sh".into(), "-c".into(), "id -u > marker-$MARK; echo ran".into()]],
            };
            crate::devsbd::sync_boot(&name, Some(&spec), false);
            // First start had no helper: the hook was a no-op.
            assert_eq!(in_container("cat /tmp/marker-ok"), None);

            let restart = Command::new("docker").args(["restart", "-t", "0", &name]).output().unwrap();
            assert!(restart.status.success(), "{}", String::from_utf8_lossy(&restart.stderr));
            assert_eq!(poll("cat /tmp/marker-ok").as_deref(), Some("65534"), "postStartCommand not rerun");
            assert!(
                poll("kill -0 \"$(cut -d' ' -f1 /run/devsandbox/devsbd.pid)\"").is_some(),
                "daemon not running after restart"
            );
            let log = in_container("cat /run/devsandbox/boot.log").unwrap_or_default();
            assert!(log.contains("devsbd boot ===") && log.contains("\nran"), "{log}");

            // No boot file: the hook only restarts the daemon.
            crate::devsbd::sync_boot(&name, None, false);
            assert_eq!(in_container("test -e /run/devsandbox/boot && echo present"), None);
        });
        Ok(())
    }
}