zc2 0.0.48

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
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
//! Atelier jobs per instance (T2, zc#593): the runner's prepare / run /
//! collect flow with the job's own ephemeral Incus/LXD instance as the
//! boundary, instead of the shared tier's persistent image store.
//!
//! ```text
//! prepare   builder instance (golden, or the cached derived image)
//!             pull the family image, build the layer, push it when asked
//!             -> published as `zk-img-<key12>` (golden + the image), builder deleted
//! run       `zk-job-<id>` from `zk-img-<key12>`: the image is already inside,
//!             nothing is pulled; the work dir is a disk device, so the job's
//!             outputs land on the host as they are written
//! stop      the instance is deleted (it is the boundary)
//! ```
//!
//! The derived-image cache ([`super::ephemeral::Ephemeral::cached_image`]) is
//! what makes the second job with the same image and layer skip the pull and
//! the build. Admission is [`super::job::Admission`]'s, taken by the caller
//! around each instance launch (never around a pull, which takes minutes).
//! Every Incus call goes through [`super::lxc::IncusCli`], so a fake drives
//! each path here.

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

use super::ephemeral::{
    inner_limits, instance_name, parse_nvidia_smi, pick_gpus, Ephemeral, InstanceSpec, CFG_KIND,
    IMG_PREFIX,
};
use super::job::{layer_dockerfile, layer_tag, JobHandle, JobSpec};
use super::Limits;

/// `user.zakuro.kind` of an Atelier job instance.
pub const KIND_ATELIER: &str = "atelier";
/// How long an image built from a mutable tag (`ref:tag`) is trusted;
/// `ZAKURO_SANDBOX_IMG_TTL_HOURS` overrides. A digest pin never expires.
pub const DEFAULT_TAG_TTL: Duration = Duration::from_secs(24 * 3600);

fn s(v: &[&str]) -> Vec<String> {
    v.iter().map(|a| a.to_string()).collect()
}

fn unix_now() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

/// `ZAKURO_SANDBOX_IMG_TTL_HOURS`, as a duration.
pub fn tag_ttl(get: &dyn Fn(&str) -> Option<String>) -> Duration {
    get("ZAKURO_SANDBOX_IMG_TTL_HOURS")
        .and_then(|v| v.trim().parse::<f64>().ok())
        .filter(|h| *h >= 0.0)
        .map(|h| Duration::from_secs((h * 3600.0) as u64))
        .unwrap_or(DEFAULT_TAG_TTL)
}

/// What a job's image is, decided before anything is pulled.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Plan {
    /// `zk-img-<12 hex>`: golden + this image (+ this layer).
    pub alias: String,
    /// The family image as given.
    pub base: String,
    /// The digest in the reference, when it is pinned (`ref@sha256:…`).
    pub pinned_digest: Option<String>,
}

/// The cache key of a job's derived image: the reference, and (with a layer)
/// everything the layer tag is made of. Order-independent over the packages.
pub fn plan(spec: &JobSpec) -> Plan {
    let pinned_digest = spec
        .image
        .split_once('@')
        .map(|(_, d)| d.to_string())
        .filter(|d| d.starts_with("sha256:"));
    let layer = spec.layer.as_ref().map(|l| {
        let mut apt = l.apt.clone();
        apt.sort();
        let mut pip = l.pip.clone();
        pip.sort();
        format!(
            "{}|{}|{}|{}|{}",
            l.venv,
            apt.join(" "),
            pip.join(" "),
            spec.layer_repo.as_deref().unwrap_or("atelier-custom"),
            spec.owner.as_deref().unwrap_or("local")
        )
    });
    let material = format!("{}\n{}", spec.image, layer.unwrap_or_default());
    Plan {
        alias: format!(
            "{IMG_PREFIX}{}",
            &crate::dataset::sha256_hex(material.as_bytes())[..12]
        ),
        base: spec.image.clone(),
        pinned_digest,
    }
}

/// What prepare produced: the image the job runs and where it came from.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Prepared {
    pub alias: String,
    /// The reference the container runs: the layer tag, else the family image.
    pub image: String,
    pub digest: String,
    pub layer_tag: Option<String>,
    /// The derived image was already on the node (nothing pulled or built).
    pub cached: bool,
}

impl Prepared {
    pub fn handle(&self, name: &str) -> JobHandle {
        JobHandle {
            name: name.to_string(),
            image: self.image.clone(),
            image_digest: self.digest.clone(),
            layer_tag: self.layer_tag.clone(),
        }
    }
}

/// The T2 job flow over one [`Ephemeral`].
pub struct InstanceJobs {
    eph: Ephemeral,
    /// Host dir for the Dockerfile and registry config that are pushed in
    /// (never an argv).
    scratch: PathBuf,
    nvidia_smi: Box<dyn Fn() -> Result<String, String> + Send + Sync>,
    now: fn() -> u64,
    ttl: Duration,
    /// A Dockerfile to build instead of the layer's (`{base}` is the family
    /// image): the real test layers a `RUN` on busybox, which no pinned
    /// apt/pip layer can.
    #[cfg(test)]
    dockerfile: Option<String>,
    /// Idmapped work-dir mount; the real test turns it off in the nested slot.
    #[cfg(test)]
    shift: bool,
}

impl InstanceJobs {
    pub fn new(eph: Ephemeral, scratch: PathBuf, ttl: Duration) -> Self {
        Self {
            eph,
            scratch,
            nvidia_smi: Box::new(super::ephemeral::real_nvidia_smi),
            now: unix_now,
            ttl,
            #[cfg(test)]
            dockerfile: None,
            #[cfg(test)]
            shift: true,
        }
    }

    #[cfg(test)]
    fn with(
        mut self,
        smi: Box<dyn Fn() -> Result<String, String> + Send + Sync>,
        now: fn() -> u64,
    ) -> Self {
        self.nvidia_smi = smi;
        self.now = now;
        self
    }

    pub fn ephemeral(&self) -> &Ephemeral {
        &self.eph
    }

    /// The prepared image when the derived image is already here and nothing
    /// needs doing with it (no push asked): the second job with the same
    /// image skips the pull and the build.
    pub fn ready(&self, spec: &JobSpec, plan: &Plan) -> Option<Prepared> {
        if spec.push_to.is_some() {
            return None;
        }
        self.cached(spec, plan)
    }

    fn cached(&self, spec: &JobSpec, plan: &Plan) -> Option<Prepared> {
        let hit = self.eph.cached_image(&plan.alias, self.ttl, (self.now)())?;
        if hit.digest.is_empty() {
            return None;
        }
        let tag = Some(hit.tag.clone()).filter(|t| !t.is_empty() && spec.layer.is_some());
        Some(Prepared {
            alias: plan.alias.clone(),
            image: tag.clone().unwrap_or_else(|| spec.image.clone()),
            digest: hit.digest,
            layer_tag: tag,
            cached: true,
        })
    }

    /// Launch the builder instance (the caller holds the admission lock):
    /// from the cached derived image when there is one (a push needs the
    /// image), else from the golden one. `limits` are the admitted limits;
    /// the builder holds no GPU.
    pub fn launch_builder(
        &self,
        spec: &JobSpec,
        plan: &Plan,
        limits: &Limits,
    ) -> Result<String, String> {
        let mut l = limits.clone();
        l.gpus = super::Gpus::None;
        let from = self.cached(spec, plan).map(|p| p.alias);
        let mut is = InstanceSpec::new(
            &format!("{}-build", spec.name),
            l,
            self.eph.node_disk()?,
            vec![],
            vec![],
        );
        is.image = from;
        // The builder is stopped and published, so it must not delete itself.
        is.persistent = true;
        is.config = vec![(CFG_KIND.to_string(), KIND_ATELIER.to_string())];
        self.eph.launch(&is)
    }

    fn nerdctl(&self, inst: &str, args: &[&str]) -> Result<String, String> {
        self.eph.nerdctl(inst, &s(args))
    }

    fn present(&self, inst: &str, image: &str) -> bool {
        self.nerdctl(inst, &["image", "inspect", "--format", "{{.Id}}", image])
            .is_ok()
    }

    /// Pull, build the layer, push when asked, publish the result as the
    /// derived image, and delete the builder, whatever happens. A failure
    /// here is before the job runs, so it is never billed.
    pub fn finish_prepare(
        &self,
        builder: &str,
        spec: &JobSpec,
        plan: &Plan,
    ) -> Result<Prepared, String> {
        let r = self.prepare_in(builder, spec, plan);
        let _ = self.eph.destroy(builder);
        r
    }

    fn prepare_in(&self, inst: &str, spec: &JobSpec, plan: &Plan) -> Result<Prepared, String> {
        let had = self
            .eph
            .cached_images()
            .iter()
            .any(|i| i.alias == plan.alias);
        if let Some(auth) = &spec.push_to {
            let f = self.scratch_file(&spec.name, "config.json")?;
            let body =
                super::ephemeral::docker_config_json(&auth.registry, &auth.username, &auth.token);
            let pushed = write_private(&f, &body)
                .map_err(|e| format!("registry config: {e}"))
                .and_then(|_| self.eph.push_docker_config(inst, &f));
            let _ = std::fs::remove_file(&f);
            pushed?;
        }
        if !self.present(inst, &spec.image) {
            self.nerdctl(inst, &["pull", "--quiet", &spec.image])
                .map_err(|e| format!("pull {}: {e}", spec.image))?;
        }
        let out = self.nerdctl(
            inst,
            &[
                "image",
                "inspect",
                "--format",
                "{{json .RepoDigests}} {{.Id}}",
                &spec.image,
            ],
        )?;
        let digest = super::job::digest_from_inspect(&out);
        let (image, tag) = match &spec.layer {
            None => (spec.image.clone(), None),
            Some(layer) => {
                let repo = spec.layer_repo.as_deref().unwrap_or("atelier-custom");
                let owner = spec.owner.as_deref().unwrap_or("local");
                let tag = layer_tag(repo, owner, &digest, layer);
                if !self.present(inst, &tag) {
                    self.build(inst, spec, &spec.image, layer, &tag)?;
                }
                (tag.clone(), Some(tag))
            }
        };
        if spec.push_to.is_some() {
            if let Some(t) = &tag {
                self.nerdctl(inst, &["push", "--quiet", t])
                    .map_err(|e| format!("push {t}: {e}"))?;
            }
        }
        if !had {
            // The registry login is not part of the image.
            let _ = self.eph.run(&[
                "exec",
                inst,
                "--",
                "rm",
                "-rf",
                "/root/.docker",
                "/root/Dockerfile",
                "/root/ctx",
            ]);
            self.eph
                .run(&["stop", inst])
                .map_err(|e| format!("stopping {inst} failed: {e}"))?;
            self.eph.publish_derived(
                inst,
                &plan.alias,
                &digest,
                tag.as_deref().unwrap_or(""),
                plan.pinned_digest.is_some(),
                (self.now)(),
            )?;
        }
        Ok(Prepared {
            alias: plan.alias.clone(),
            image,
            digest,
            layer_tag: tag,
            cached: false,
        })
    }

    fn build(
        &self,
        inst: &str,
        spec: &JobSpec,
        base: &str,
        layer: &super::job::Layer,
        tag: &str,
    ) -> Result<(), String> {
        let dockerfile = layer_dockerfile(base, layer)?;
        #[cfg(test)]
        let dockerfile = self
            .dockerfile
            .as_ref()
            .map(|d| d.replace("{base}", base))
            .unwrap_or(dockerfile);
        let f = self.scratch_file(&spec.name, "Dockerfile")?;
        let pushed = write_private(&f, &dockerfile)
            .map_err(|e| format!("Dockerfile: {e}"))
            .and_then(|_| {
                self.eph.run(&[
                    "file",
                    "push",
                    "--create-dirs",
                    &f.display().to_string(),
                    &format!("{inst}/root/Dockerfile"),
                ])
            });
        let _ = std::fs::remove_file(&f);
        pushed?;
        self.eph
            .run(&["exec", inst, "--", "mkdir", "-p", "/root/ctx"])?;
        self.nerdctl(
            inst,
            &[
                "build",
                "--quiet",
                "-t",
                tag,
                "-f",
                "/root/Dockerfile",
                "/root/ctx",
            ],
        )
        .map(|_| ())
        .map_err(|e| format!("layer build failed: {}", last_lines(&e, 20)))
    }

    fn scratch_file(&self, job: &str, what: &str) -> Result<PathBuf, String> {
        std::fs::create_dir_all(&self.scratch)
            .map_err(|e| format!("{}: {e}", self.scratch.display()))?;
        Ok(self.scratch.join(format!("{job}.{what}")))
    }

    /// Start the job's container in its own instance (the caller holds the
    /// admission lock): GPUs by PCI address (one job per GPU), the image
    /// already inside, the work dir a disk device so outputs are on the host
    /// as they are written, the container under the inner limits.
    pub fn run(
        &self,
        spec: &JobSpec,
        prepared: &Prepared,
        limits: &Limits,
        user: Option<(u32, u32)>,
    ) -> Result<(), String> {
        let picked = if limits.gpus.flag_value().is_some() {
            let all = parse_nvidia_smi(&(self.nvidia_smi)()?);
            pick_gpus(&limits.gpus, &all, &self.eph.taken_gpus()?)
                .map_err(|e| format!("{}: {e}", spec.name))?
        } else {
            vec![]
        };
        let inner_dir = format!(
            "/zakuro/mnt/{}",
            &crate::dataset::sha256_hex(spec.workdir.display().to_string().as_bytes())[..16]
        );
        let mut is = InstanceSpec::new(
            &spec.name,
            limits.clone(),
            self.eph.node_disk()?,
            picked.iter().map(|g| g.pci.clone()).collect(),
            vec![(spec.workdir.display().to_string(), inner_dir.clone())],
        );
        is.image = Some(prepared.alias.clone());
        #[cfg(test)]
        {
            is.shift = self.shift;
        }
        is.config = vec![(CFG_KIND.to_string(), KIND_ATELIER.to_string())];
        let inst = self.eph.launch(&is)?;
        let mut inner_spec = spec.clone();
        inner_spec.workdir = PathBuf::from(&inner_dir);
        let inner = inner_limits(limits, !picked.is_empty());
        let argv = super::job::start_argv(&inner_spec, &prepared.image, user, &inner);
        match self.eph.nerdctl(&inst, &argv) {
            Ok(_) => Ok(()),
            Err(e) => {
                let _ = self.eph.destroy(&inst);
                Err(format!("start {}: {e}", spec.name))
            }
        }
    }

    /// `(running, exit code)` of the job's container.
    pub fn inspect(&self, name: &str) -> Result<String, String> {
        self.nerdctl(
            &instance_name(name),
            &[
                "inspect",
                "--format",
                "{{.State.Status}} {{.State.ExitCode}}",
                name,
            ],
        )
    }

    pub fn logs(&self, name: &str) -> Result<String, String> {
        self.nerdctl(&instance_name(name), &["logs", name])
    }

    /// Delete the job's instance (the boundary); idempotent.
    pub fn stop(&self, name: &str) -> Result<(), String> {
        self.eph.destroy(&instance_name(name))
    }

    /// Delete every Atelier job instance whose job is not in `known`; the
    /// job names removed.
    pub fn reconcile(&self, known: &[String]) -> Result<Vec<String>, String> {
        let mut removed = vec![];
        for i in self.eph.list()? {
            if i.kind.as_deref() != Some(KIND_ATELIER) {
                continue;
            }
            let job = i.lease.clone().unwrap_or_default();
            // A builder is `<job>-build`: it belongs to its job's prepare.
            let owner = job.strip_suffix("-build").unwrap_or(&job);
            if !known.iter().any(|k| k == owner) {
                self.eph.destroy(&i.name)?;
                removed.push(job);
            }
        }
        Ok(removed)
    }

    /// Whether `name`'s instance exists (a job started per instance).
    pub fn exists(&self, name: &str) -> bool {
        let inst = instance_name(name);
        self.eph
            .list()
            .map(|l| l.iter().any(|i| i.name == inst))
            .unwrap_or(false)
    }
}

fn last_lines(text: &str, n: usize) -> String {
    let lines: Vec<&str> = text.lines().collect();
    lines[lines.len().saturating_sub(n)..].join("\n")
}

/// Write `body` to `path`, readable by the owner only.
fn write_private(path: &Path, body: &str) -> std::io::Result<()> {
    use std::io::Write;
    #[cfg(unix)]
    use std::os::unix::fs::OpenOptionsExt;
    let mut o = std::fs::OpenOptions::new();
    o.write(true).create(true).truncate(true);
    #[cfg(unix)]
    o.mode(0o600);
    o.open(path)?.write_all(body.as_bytes())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sandbox::ephemeral::provision_version;
    use crate::sandbox::job::{Layer, RegistryAuth};
    use crate::sandbox::lxc::{IncusCli, SandboxSize, DEFAULT_IMAGE};
    use crate::sandbox::{Gpus, Limits};
    use std::collections::BTreeMap;
    use std::sync::{Arc, Mutex};

    #[derive(Default)]
    struct Rec {
        calls: Mutex<Vec<String>>,
        answer: Mutex<Vec<(&'static str, Result<String, String>)>>,
    }

    impl Rec {
        fn on(&self, needle: &'static str, a: Result<&str, &str>) {
            self.answer
                .lock()
                .unwrap()
                .push((needle, a.map(String::from).map_err(String::from)));
        }
        fn calls(&self) -> Vec<String> {
            self.calls.lock().unwrap().clone()
        }
    }

    impl IncusCli for Rec {
        fn run(&self, argv: &[String]) -> Result<String, String> {
            let j = argv.join(" ");
            self.calls.lock().unwrap().push(j.clone());
            for (needle, a) in self.answer.lock().unwrap().iter() {
                if j.contains(needle) {
                    return a.clone();
                }
            }
            Ok(String::new())
        }
    }

    fn golden_ok(rec: &Rec) {
        rec.on(
            "image get-property zakuro-sandbox-base",
            Ok(Box::leak(provision_version(DEFAULT_IMAGE).into_boxed_str())),
        );
    }

    fn jobs(rec: &Arc<Rec>, dir: &Path) -> InstanceJobs {
        let eph = Ephemeral::new(
            rec.clone(),
            DEFAULT_IMAGE,
            Ok(SandboxSize {
                limits: Limits::new(8.0, 16.0, Gpus::None),
                disk_bytes: 20 << 30,
            }),
        );
        let eph = {
            // Same fast polling as the ephemeral tests.
            let mut e = eph;
            e.fast_for_tests();
            e
        };
        InstanceJobs::new(eph, dir.to_path_buf(), DEFAULT_TAG_TTL)
            .with(Box::new(|| Ok(String::new())), || 1_000_000)
    }

    fn spec(layer: bool) -> JobSpec {
        JobSpec {
            name: "atl-1".into(),
            image: "docker.io/zakuroai/atelier-vision@sha256:aaaa".into(),
            layer: layer.then(|| Layer {
                apt: vec![],
                pip: vec!["timm==1.0.15".into()],
                venv: "/opt/v".into(),
            }),
            layer_repo: Some("r".into()),
            owner: Some("4".into()),
            push_to: None,
            workdir: PathBuf::from("/var/lib/atelier/work/atl-1"),
            mount_at: None,
            prepare_only: false,
            env: BTreeMap::new(),
            gpus: String::new(),
            cpus: None,
            memory_gb: None,
            cmd: s(&["python", "-m", "sakura.atelier", "run", "/work/spec.yaml"]),
        }
    }

    fn lim() -> Limits {
        Limits::new(2.0, 4.0, Gpus::None)
    }

    fn tmp(tag: &str) -> PathBuf {
        let d = std::env::temp_dir().join(format!("zc-jobinst-{tag}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&d);
        d
    }

    fn images_json(
        alias: &str,
        golden: &str,
        digest: &str,
        tag: &str,
        pinned: bool,
        at: u64,
    ) -> String {
        format!(
            r#"[{{"fingerprint":"ff","size":1000,"last_used_at":"2026-11-10T00:00:00Z",
            "aliases":[{{"name":"{alias}"}}],
            "properties":{{"zakuro.golden":"{golden}","zakuro.digest":"{digest}","zakuro.tag":"{tag}",
            "zakuro.pinned":"{}","zakuro.cached_at":"{at}"}}}}]"#,
            u8::from(pinned)
        )
    }

    #[test]
    fn the_cache_key_is_the_image_and_the_layer_whatever_the_package_order() {
        let a = plan(&spec(true));
        assert!(
            a.alias.starts_with("zk-img-") && a.alias.len() == 7 + 12,
            "{a:?}"
        );
        assert_eq!(a.pinned_digest.as_deref(), Some("sha256:aaaa"));
        let mut b = spec(true);
        b.layer.as_mut().unwrap().pip = vec!["timm==1.0.15".into()];
        assert_eq!(plan(&b).alias, a.alias);
        b.layer.as_mut().unwrap().pip.push("numpy==2.0.0".into());
        assert_ne!(plan(&b).alias, a.alias, "another layer is another image");
        assert_ne!(
            plan(&spec(false)).alias,
            a.alias,
            "no layer is another image"
        );
        let mut c = spec(false);
        c.image = "docker.io/zakuroai/atelier-vision:abc-cpu".into();
        assert_eq!(plan(&c).pinned_digest, None, "a tag is not a pin");
        let mut d = spec(true);
        d.owner = Some("5".into());
        assert_ne!(plan(&d).alias, a.alias, "the owner is in the layer tag");
    }

    #[test]
    fn a_cached_image_is_a_hit_that_pulls_nothing_and_a_stale_one_is_not() {
        let rec = Arc::new(Rec::default());
        golden_ok(&rec);
        let p = plan(&spec(true));
        let ver = provision_version(DEFAULT_IMAGE);
        rec.on(
            "image list -f json",
            Ok(Box::leak(
                images_json(&p.alias, &ver, "sha256:aaaa", "r:4-1234", true, 5).into_boxed_str(),
            )),
        );
        let j = jobs(&rec, &tmp("hit"));
        let got = j.ready(&spec(true), &p).expect("hit");
        assert!(got.cached);
        assert_eq!(got.image, "r:4-1234");
        assert_eq!(got.layer_tag.as_deref(), Some("r:4-1234"));
        assert_eq!(got.digest, "sha256:aaaa");
        assert!(!rec.calls().iter().any(|c| c.contains("pull")));
        // A push is asked for: the image is needed in an instance, so no shortcut.
        let mut push = spec(true);
        push.push_to = Some(RegistryAuth {
            registry: "docker.io".into(),
            username: "u".into(),
            token: "t".into(),
        });
        assert!(j.ready(&push, &p).is_none());

        // Built on an older golden image: deleted, not used.
        let old = Arc::new(Rec::default());
        golden_ok(&old);
        old.on(
            "image list -f json",
            Ok(Box::leak(
                images_json(&p.alias, "0123456789ab", "sha256:aaaa", "", true, 5).into_boxed_str(),
            )),
        );
        assert!(jobs(&old, &tmp("old")).ready(&spec(false), &p).is_none());
        assert!(old
            .calls()
            .iter()
            .any(|c| c == &format!("incus image delete {}", p.alias)));

        // A mutable tag past its TTL goes stale; a pin does not.
        let tagged = Arc::new(Rec::default());
        golden_ok(&tagged);
        tagged.on(
            "image list -f json",
            Ok(Box::leak(
                images_json(&p.alias, &ver, "sha256:b", "", false, 5).into_boxed_str(),
            )),
        );
        let jt = InstanceJobs::new(
            jobs(&tagged, &tmp("tag")).eph.clone(),
            tmp("tag"),
            Duration::from_secs(60),
        )
        .with(Box::new(|| Ok(String::new())), || 1_000_000);
        assert!(
            jt.ready(&spec(false), &p).is_none(),
            "tag-built image past its TTL"
        );
    }

    #[test]
    fn prepare_pulls_builds_publishes_the_derived_image_and_deletes_the_builder() {
        let rec = Arc::new(Rec::default());
        golden_ok(&rec);
        rec.on("image list -f json", Ok("[]"));
        rec.on(
            "image inspect --format {{.Id}} docker.io",
            Err("no such image"),
        );
        rec.on("image inspect --format {{.Id}} r:4-", Err("no such image"));
        rec.on(
            "{{json .RepoDigests}}",
            Ok(r#"["docker.io/zakuroai/atelier-vision@sha256:aaaa"] sha256:id"#),
        );
        let dir = tmp("prep");
        let j = jobs(&rec, &dir);
        let sp = spec(true);
        let p = plan(&sp);
        let builder = j.launch_builder(&sp, &p, &lim()).unwrap();
        let got = j.finish_prepare(&builder, &sp, &p).unwrap();
        assert!(!got.cached);
        assert_eq!(got.digest, "sha256:aaaa");
        let tag = got.layer_tag.clone().unwrap();
        assert!(tag.starts_with("r:4-"), "{tag}");
        let calls = rec.calls();
        let pos = |needle: &str| {
            calls
                .iter()
                .position(|c| c.contains(needle))
                .unwrap_or_else(|| panic!("{needle} missing in {calls:#?}"))
        };
        // The builder is persistent (not --ephemeral) and carries the Atelier kind.
        let init = &calls[pos("incus init zakuro-sandbox-base")];
        assert!(!init.contains("--ephemeral"), "{init}");
        assert!(init.contains("-c user.zakuro.kind=atelier"), "{init}");
        assert!(init.contains("-c user.zakuro.lease=atl-1-build"), "{init}");
        assert!(pos("nerdctl pull --quiet docker.io/zakuroai") < pos("nerdctl build --quiet -t"));
        assert!(calls[pos("nerdctl build")].contains("-f /root/Dockerfile /root/ctx"));
        assert!(pos("file push") < pos("nerdctl build"));
        assert!(pos("nerdctl build") < pos("incus stop"));
        let publish = &calls[pos("incus publish")];
        assert!(
            publish.starts_with(&format!("incus publish {builder} --alias {}", p.alias)),
            "{publish}"
        );
        assert!(publish.contains("zakuro.digest=sha256:aaaa"));
        assert!(publish.contains(&format!("zakuro.tag={tag}")));
        assert!(publish.contains("zakuro.pinned=1"));
        assert!(publish.contains("zakuro.cached_at=1000000"));
        assert!(pos("incus publish") < calls.len() - 1);
        assert_eq!(calls.last().unwrap(), &format!("incus delete -f {builder}"));
        assert!(
            !dir.join("atl-1.Dockerfile").exists(),
            "no Dockerfile left on the host"
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn a_failed_pull_or_build_deletes_the_builder_and_publishes_nothing() {
        let rec = Arc::new(Rec::default());
        golden_ok(&rec);
        rec.on("image list -f json", Ok("[]"));
        rec.on("image inspect", Err("no such image"));
        rec.on("nerdctl pull", Err("manifest unknown"));
        let j = jobs(&rec, &tmp("fail"));
        let sp = spec(false);
        let p = plan(&sp);
        let b = j.launch_builder(&sp, &p, &lim()).unwrap();
        let e = j.finish_prepare(&b, &sp, &p).unwrap_err();
        assert!(e.contains("pull") && e.contains("manifest unknown"), "{e}");
        assert!(!rec.calls().iter().any(|c| c.starts_with("incus publish")));
        assert_eq!(rec.calls().last().unwrap(), &format!("incus delete -f {b}"));
    }

    #[test]
    fn a_push_logs_in_with_a_pushed_config_never_an_argv() {
        let rec = Arc::new(Rec::default());
        golden_ok(&rec);
        rec.on("image list -f json", Ok("[]"));
        rec.on("image inspect --format {{.Id}}", Err("no such image"));
        rec.on("{{json .RepoDigests}}", Ok(r#"["x@sha256:aaaa"] sha256:i"#));
        let dir = tmp("push");
        let j = jobs(&rec, &dir);
        let mut sp = spec(true);
        sp.push_to = Some(RegistryAuth {
            registry: "docker.io".into(),
            username: "bot".into(),
            token: "s3cret-token".into(),
        });
        let p = plan(&sp);
        let b = j.launch_builder(&sp, &p, &lim()).unwrap();
        j.finish_prepare(&b, &sp, &p).unwrap();
        let calls = rec.calls();
        assert!(
            !calls.iter().any(|c| c.contains("s3cret-token")),
            "{calls:#?}"
        );
        assert!(calls
            .iter()
            .any(|c| c.contains(&format!("{b}/root/.docker/config.json"))));
        let push = calls
            .iter()
            .position(|c| c.contains("nerdctl push --quiet r:4-"))
            .unwrap();
        let build = calls
            .iter()
            .position(|c| c.contains("nerdctl build"))
            .unwrap();
        assert!(build < push);
        let rm = calls
            .iter()
            .position(|c| c.contains("rm -rf /root/.docker"))
            .unwrap();
        let publish = calls
            .iter()
            .position(|c| c.starts_with("incus publish"))
            .unwrap();
        assert!(rm < publish, "the login is not part of the published image");
        assert!(!dir.join("atl-1.config.json").exists());
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn a_job_runs_in_its_own_instance_from_the_derived_image_with_the_workdir_as_a_disk() {
        let rec = Arc::new(Rec::default());
        golden_ok(&rec);
        let j = jobs(&rec, &tmp("run"));
        let sp = spec(true);
        let prep = Prepared {
            alias: plan(&sp).alias,
            image: "r:4-1234".into(),
            digest: "sha256:aaaa".into(),
            layer_tag: Some("r:4-1234".into()),
            cached: true,
        };
        j.run(&sp, &prep, &lim(), Some((1000, 1000))).unwrap();
        let name = instance_name("atl-1");
        let calls = rec.calls();
        let init = calls.iter().find(|c| c.starts_with("incus init")).unwrap();
        assert!(
            init.starts_with(&format!("incus init {} {name} --ephemeral", prep.alias)),
            "{init}"
        );
        assert!(init.contains("-c user.zakuro.kind=atelier"));
        assert!(init.contains("-c limits.cpu=2"));
        assert!(init.contains("-d root,size=20480MiB"), "{init}");
        let mnt = calls.iter().find(|c| c.contains("zakuro-mnt-0")).unwrap();
        assert!(
            mnt.contains("source=/var/lib/atelier/work/atl-1 path=/zakuro/mnt/"),
            "{mnt}"
        );
        assert!(mnt.contains("shift=true"));
        // Egress rules are in before the job container.
        let egress = calls
            .iter()
            .position(|c| c.contains("ZAKURO-EGRESS"))
            .unwrap();
        let run = calls
            .iter()
            .position(|c| {
                c.starts_with(&format!("incus exec {name} -- nerdctl run -d --name atl-1"))
            })
            .unwrap_or_else(|| panic!("{calls:#?}"));
        assert!(egress < run);
        let r = &calls[run];
        assert!(r.contains("--user 1000:1000"));
        assert!(r.contains("--memory "), "{r}");
        assert!(
            r.contains(" r:4-1234 python -m sakura.atelier run /work/spec.yaml"),
            "{r}"
        );
        assert!(
            r.contains(":/work"),
            "the container sees the dir at the job's mount: {r}"
        );
        assert!(
            !calls.iter().any(|c| c.contains("nerdctl pull")),
            "the image is already inside"
        );
        let _ = std::fs::remove_dir_all(tmp("run"));
    }

    #[test]
    fn a_start_that_fails_inside_deletes_the_instance() {
        let rec = Arc::new(Rec::default());
        golden_ok(&rec);
        rec.on("nerdctl run -d", Err("no such image"));
        let j = jobs(&rec, &tmp("runfail"));
        let sp = spec(false);
        let prep = Prepared {
            alias: plan(&sp).alias,
            image: sp.image.clone(),
            digest: "sha256:aaaa".into(),
            layer_tag: None,
            cached: true,
        };
        let e = j.run(&sp, &prep, &lim(), None).unwrap_err();
        assert!(e.contains("no such image"), "{e}");
        assert_eq!(
            rec.calls().last().unwrap(),
            &format!("incus delete -f {}", instance_name("atl-1"))
        );
    }

    #[test]
    fn a_gpu_job_gets_a_free_card_and_none_when_all_are_held() {
        let rec = Arc::new(Rec::default());
        golden_ok(&rec);
        let eph = jobs(&rec, &tmp("gpu")).eph.clone();
        let j = InstanceJobs::new(eph, tmp("gpu"), DEFAULT_TAG_TTL).with(
            Box::new(|| Ok("0, GPU-aaa, 00000000:01:00.0\n".to_string())),
            || 1,
        );
        let sp = spec(false);
        let prep = Prepared {
            alias: plan(&sp).alias,
            image: sp.image.clone(),
            digest: "sha256:aaaa".into(),
            layer_tag: None,
            cached: true,
        };
        let mut l = lim();
        l.gpus = Gpus::All;
        j.run(&sp, &prep, &l, None).unwrap();
        let calls = rec.calls();
        assert!(
            calls
                .iter()
                .any(|c| c.contains("gputype=physical pci=0000:01:00.0")),
            "{calls:#?}"
        );
        assert!(calls.iter().any(|c| c.contains("--gpus all")), "{calls:#?}");

        let held = Arc::new(Rec::default());
        golden_ok(&held);
        held.on(
            "list zk-job-",
            Ok(r#"[{"name":"zk-job-aaaaaaaa","status":"Running","config":{"user.zakuro.gpus":"0000:01:00.0"}}]"#),
        );
        let eph = jobs(&held, &tmp("gpu2")).eph.clone();
        let j = InstanceJobs::new(eph, tmp("gpu2"), DEFAULT_TAG_TTL).with(
            Box::new(|| Ok("0, GPU-aaa, 00000000:01:00.0\n".to_string())),
            || 1,
        );
        let e = j.run(&sp, &prep, &l, None).unwrap_err();
        assert!(e.contains("free"), "{e}");
        assert!(!held.calls().iter().any(|c| c.starts_with("incus init")));
    }

    #[test]
    fn inspect_logs_and_stop_address_the_jobs_instance() {
        let rec = Arc::new(Rec::default());
        let j = jobs(&rec, &tmp("ops"));
        j.inspect("atl-1").unwrap();
        j.logs("atl-1").unwrap();
        j.stop("atl-1").unwrap();
        let name = instance_name("atl-1");
        let calls = rec.calls();
        assert_eq!(
            calls[0],
            format!("incus exec {name} -- nerdctl inspect --format {{{{.State.Status}}}} {{{{.State.ExitCode}}}} atl-1")
        );
        assert_eq!(calls[1], format!("incus exec {name} -- nerdctl logs atl-1"));
        assert_eq!(calls[2], format!("incus delete -f {name}"));
    }

    #[test]
    fn reconcile_removes_only_atelier_instances_no_job_owns() {
        let rec = Arc::new(Rec::default());
        rec.on(
            "list zk-job-",
            Ok(r#"[
              {"name":"zk-job-11111111","status":"Running","config":{"user.zakuro.kind":"atelier","user.zakuro.lease":"keep"}},
              {"name":"zk-job-22222222","status":"Running","config":{"user.zakuro.kind":"atelier","user.zakuro.lease":"gone"}},
              {"name":"zk-job-33333333","status":"Running","config":{"user.zakuro.kind":"atelier","user.zakuro.lease":"keep-build"}},
              {"name":"zk-job-44444444","status":"Running","config":{"user.zakuro.lease":"zk-space-x"}}
            ]"#),
        );
        let j = jobs(&rec, &tmp("rec"));
        let gone = j.reconcile(&["keep".to_string()]).unwrap();
        assert_eq!(gone, vec!["gone".to_string()]);
        let calls = rec.calls();
        assert!(calls.contains(&"incus delete -f zk-job-22222222".to_string()));
        assert!(
            !calls
                .iter()
                .any(|c| c.contains("zk-job-44444444") && c.contains("delete")),
            "a Space lease is not an Atelier job"
        );
        assert!(!calls
            .iter()
            .any(|c| c.contains("zk-job-11111111") && c.contains("delete")));
        assert!(!calls
            .iter()
            .any(|c| c.contains("zk-job-33333333") && c.contains("delete")));
    }

    /// A shared-tier CLI that must never be called when jobs run per instance.
    struct NoCli;
    impl crate::sandbox::job::JobCli for NoCli {
        fn run(&self, argv: &[String]) -> Result<String, String> {
            panic!("the shared tier was touched: {argv:?}")
        }
        fn run_stdin(&self, argv: &[String], _: &str) -> Result<String, String> {
            panic!("the shared tier was touched: {argv:?}")
        }
    }

    fn runtime(j: InstanceJobs, budget: Limits) -> crate::sandbox::job::JobRuntime<NoCli> {
        let j = Arc::new(j);
        crate::sandbox::job::JobRuntime::new(
            NoCli,
            PathBuf::from("/nonexistent"),
            crate::sandbox::job::Admission {
                budget: Box::new(move || Ok(budget.clone())),
                allocations: Box::new(|| Ok(vec![])),
                lock_path: None,
            },
        )
        .with_tier(Box::new(move || Some(j.clone())))
    }

    #[test]
    fn the_runtime_prepares_once_and_a_second_job_hits_the_cache_without_a_pull() {
        // First job: a miss. A builder is launched, the image built and
        // published; the shared tier is never touched.
        let rec = Arc::new(Rec::default());
        golden_ok(&rec);
        rec.on("image list -f json", Ok("[]"));
        rec.on("image inspect --format {{.Id}}", Err("no such image"));
        rec.on("{{json .RepoDigests}}", Ok(r#"["x@sha256:aaaa"] sha256:i"#));
        let rt = runtime(jobs(&rec, &tmp("rt1")), Limits::new(4.0, 8.0, Gpus::None));
        let mut sp = spec(true);
        sp.prepare_only = true;
        let h = rt.start(&sp).unwrap();
        assert!(h.layer_tag.as_deref().unwrap().starts_with("r:4-"));
        assert_eq!(h.image_digest, "sha256:aaaa");
        let calls = rec.calls();
        assert!(calls.iter().any(|c| c.starts_with("incus publish")));
        assert!(
            !calls
                .iter()
                .any(|c| c.contains(&instance_name("atl-1")) && c.starts_with("incus init")),
            "prepare-only runs nothing"
        );

        // Second job, same image + layer: the derived image is there.
        let p = plan(&spec(true));
        let ver = provision_version(DEFAULT_IMAGE);
        let rec2 = Arc::new(Rec::default());
        golden_ok(&rec2);
        rec2.on(
            "image list -f json",
            Ok(Box::leak(
                images_json(
                    &p.alias,
                    &ver,
                    "sha256:aaaa",
                    &h.layer_tag.clone().unwrap(),
                    true,
                    5,
                )
                .into_boxed_str(),
            )),
        );
        let rt2 = runtime(jobs(&rec2, &tmp("rt2")), Limits::new(4.0, 8.0, Gpus::None));
        let h2 = rt2.start(&spec(true)).unwrap();
        assert_eq!(h2.image, h.image, "same derived image");
        let calls = rec2.calls();
        assert!(
            !calls.iter().any(|c| c.contains("pull")
                || c.contains("nerdctl build")
                || c.starts_with("incus publish")),
            "{calls:#?}"
        );
        assert!(
            !calls.iter().any(|c| c.contains("-build")),
            "no builder on a hit"
        );
        let init = calls.iter().find(|c| c.starts_with("incus init")).unwrap();
        assert!(
            init.starts_with(&format!(
                "incus init {} {} --ephemeral",
                p.alias,
                instance_name("atl-1")
            )),
            "{init}"
        );
        assert!(calls
            .iter()
            .any(|c| c.contains("nerdctl run -d --name atl-1")));
    }

    #[test]
    fn a_job_past_the_budget_is_refused_before_any_instance_exists() {
        let rec = Arc::new(Rec::default());
        golden_ok(&rec);
        rec.on("image list -f json", Ok("[]"));
        let mut sp = spec(false);
        sp.cpus = Some(64.0);
        sp.gpus = "1".into();
        // The node passes no GPU: refused, nothing launched.
        let rt = runtime(jobs(&rec, &tmp("rt3")), Limits::new(4.0, 8.0, Gpus::None));
        let e = rt.start(&sp).unwrap_err();
        assert!(e.starts_with("budget:"), "{e}");
        assert!(
            !rec.calls().iter().any(|c| c.starts_with("incus init")),
            "{:#?}",
            rec.calls()
        );
    }

    #[test]
    fn status_logs_and_stop_of_a_per_instance_job_go_to_its_instance() {
        let rec = Arc::new(Rec::default());
        let name = instance_name("atl-1");
        rec.on(
            "list zk-job-",
            Ok(Box::leak(
                format!(r#"[{{"name":"{name}","status":"Running","config":{{}}}}]"#)
                    .into_boxed_str(),
            )),
        );
        rec.on("nerdctl inspect", Ok("exited 3"));
        rec.on("nerdctl logs", Ok("a\nb"));
        let rt = runtime(jobs(&rec, &tmp("rt4")), Limits::new(4.0, 8.0, Gpus::None));
        assert_eq!(rt.inspect("atl-1").unwrap(), (false, Some(3)));
        assert_eq!(rt.logs("atl-1", 1).unwrap(), (vec!["b".to_string()], 2));
        rt.stop("atl-1").unwrap();
        assert!(rec.calls().contains(&format!("incus delete -f {name}")));
        // A job with no instance is an error, not a shared-tier lookup.
        assert!(rt.inspect("nope").is_err());
    }

    /// An Atelier-shaped job in a real instance: `ZAKURO_TEST_REAL_INCUS=1
    /// cargo test -p zc2 real_incus_atelier -- --ignored --nocapture`.
    #[test]
    #[ignore]
    fn real_incus_atelier_job_in_its_own_instance() {
        if std::env::var("ZAKURO_TEST_REAL_INCUS").ok().as_deref() != Some("1") {
            eprintln!("skipped: set ZAKURO_TEST_REAL_INCUS=1");
            return;
        }
        struct Spy(Mutex<Vec<String>>);
        impl IncusCli for Spy {
            fn run(&self, argv: &[String]) -> Result<String, String> {
                self.0.lock().unwrap().push(argv.join(" "));
                crate::sandbox::lxc::IncusShell.run(argv)
            }
        }
        let spy = Arc::new(Spy(Mutex::new(vec![])));
        let image = std::env::var("ZAKURO_SANDBOX_IMAGE").unwrap_or_else(|_| {
            crate::sandbox::lxc::default_image(crate::sandbox::lxc::cli_binary()).to_string()
        });
        let eph = Ephemeral::new(
            spy.clone(),
            &image,
            Ok(SandboxSize {
                limits: Limits::new(2.0, 4.0, Gpus::None),
                disk_bytes: 12 << 30,
            }),
        );
        let t = std::time::Instant::now();
        eph.ensure_golden().expect("golden");
        println!("REAL golden ready in {:?}", t.elapsed());
        let pid = std::process::id();
        let root = std::env::temp_dir().join(format!("zc-real-atelier-{pid}"));
        let _ = std::fs::remove_dir_all(&root);
        let mut ij = InstanceJobs::new(eph.clone(), root.join("scratch"), DEFAULT_TAG_TTL);
        // Idmapped mounts need the initial user namespace: not in the nested
        // CI slot. There the work dir is world-writable instead.
        ij.shift = std::env::var("ZAKURO_TEST_NO_IDMAP").ok().as_deref() != Some("1");
        ij.dockerfile = Some("FROM {base}\nRUN echo layered-by-buildkit > /layer.txt\n".into());
        let ij = Arc::new(ij);
        let ij2 = ij.clone();
        let eph2 = eph.clone();
        let rt = crate::sandbox::job::JobRuntime::new(
            NoCli,
            root.join("docker"),
            crate::sandbox::job::Admission {
                budget: Box::new(|| Ok(Limits::new(2.0, 4.0, Gpus::None))),
                allocations: Box::new(move || eph2.allocations()),
                lock_path: None,
            },
        )
        .with_tier(Box::new(move || Some(ij2.clone())));
        let job = |n: &str| {
            let work = root.join(n);
            std::fs::create_dir_all(&work).unwrap();
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                std::fs::set_permissions(&work, std::fs::Permissions::from_mode(0o777)).unwrap();
            }
            let mut sp = spec(true);
            sp.name = format!("real-{n}-{pid}");
            sp.image = "docker.io/library/busybox:latest".into();
            sp.layer.as_mut().unwrap().pip.clear();
            sp.layer.as_mut().unwrap().venv = "/opt/none".into();
            sp.workdir = work.clone();
            sp.cmd = s(&[
                "sh",
                "-c",
                "cat /layer.txt > /work/out.txt; echo ran-$(hostname) >> /work/out.txt",
            ]);
            (sp, work)
        };
        let alias = plan(&job("p").0).alias;
        let _ = eph.run(&["image", "delete", &alias]);
        let wait_exit = |name: &str| {
            for _ in 0..60 {
                if let Ok((false, code)) = rt.inspect(name) {
                    return code;
                }
                std::thread::sleep(Duration::from_secs(1));
            }
            panic!("job {name} did not finish");
        };

        // 1. A miss: builder, pull, buildkit layer, publish, then the run.
        let (sp, work) = job("a");
        let t = std::time::Instant::now();
        let h = rt.start(&sp).expect("start (miss)");
        println!(
            "REAL job {} started in {:?} (image {})",
            sp.name,
            t.elapsed(),
            h.image
        );
        assert_eq!(wait_exit(&sp.name), Some(0));
        let out = std::fs::read_to_string(work.join("out.txt")).expect("output on the host");
        println!("REAL output on host: {}", out.replace('\n', " | "));
        assert!(out.contains("layered-by-buildkit"));
        let (logs, _) = rt.logs(&sp.name, 0).unwrap();
        println!("REAL logs readable: {} line(s)", logs.len());
        assert!(eph.cached_images().iter().any(|i| i.alias == alias));
        println!("REAL derived image {alias} cached");
        rt.stop(&sp.name).unwrap();
        let live: Vec<String> = eph.list().unwrap().into_iter().map(|i| i.name).collect();
        println!("REAL instances after job 1: {live:?}");
        assert!(live.is_empty(), "no instance left: {live:?}");

        // 2. A hit: same image + layer, nothing pulled, built or published.
        let before = spy.0.lock().unwrap().len();
        let (sp2, work2) = job("b");
        let t = std::time::Instant::now();
        rt.start(&sp2).expect("start (hit)");
        println!(
            "REAL job {} started in {:?} (cache hit)",
            sp2.name,
            t.elapsed()
        );
        assert_eq!(wait_exit(&sp2.name), Some(0));
        assert!(std::fs::read_to_string(work2.join("out.txt"))
            .unwrap()
            .contains("layered-by-buildkit"));
        let calls: Vec<String> = spy.0.lock().unwrap()[before..].to_vec();
        let pulled = calls.iter().any(|c| {
            c.contains("nerdctl pull")
                || c.contains("nerdctl build")
                || c.starts_with("incus publish")
        });
        println!("REAL second run pulled/built/published: {pulled}");
        assert!(!pulled, "{calls:#?}");
        rt.stop(&sp2.name).unwrap();
        assert!(eph.list().unwrap().is_empty());
        println!("REAL instances after job 2: none");
        let _ = eph.run(&["image", "delete", &alias]);
        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn the_tag_ttl_is_tunable() {
        assert_eq!(tag_ttl(&|_| None), DEFAULT_TAG_TTL);
        assert_eq!(
            tag_ttl(&|k| (k == "ZAKURO_SANDBOX_IMG_TTL_HOURS").then(|| "2".into())),
            Duration::from_secs(7200)
        );
    }
}