zc2 0.0.53

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
//! The Docker backend of [`super::Runtime`]: no shared boundary, so every
//! container carries its own `--cpus` / `--memory` (and `--gpus` when the
//! budget passes a GPU through).
//!
//! Every daemon call goes through one [`DockerCli`] (argv in, stdout out),
//! so a fake drives every path in tests on a machine with no Docker.

use std::process::Command;
use std::sync::Arc;

use super::{Gpus, Limits, Probe, Runtime, RuntimeKind, StartSpec, State};

/// The slice of the Docker CLI a backend needs: run one `docker …` argv,
/// hand back its trimmed stdout, or its trimmed stderr as an error.
pub trait DockerCli: Send + Sync {
    fn run(&self, argv: &[String]) -> Result<String, String>;
}

/// The real one: shells out, exactly like `mesh.rs`'s own `docker` helper.
pub struct DockerShell;

impl DockerCli for DockerShell {
    fn run(&self, argv: &[String]) -> Result<String, String> {
        let (prog, rest) = argv.split_first().ok_or("empty docker command")?;
        let out = Command::new(prog)
            .args(rest)
            .output()
            .map_err(|e| format!("docker not available: {e}"))?;
        if out.status.success() {
            Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
        } else {
            Err(String::from_utf8_lossy(&out.stderr).trim().to_string())
        }
    }
}

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

/// The `docker run` arguments for `spec`, without the leading `docker`.
/// The one place a container's flags are decided, for share workers, deploy
/// environments and Space leases alike. Order: `run [--rm] -d --name N
/// [--network NET]`, labels, mounts, hardening, the limits, publishes, env,
/// image, command -- so anything spliced in "before the image" lands after
/// every flag.
pub fn run_argv(spec: &StartSpec) -> Vec<String> {
    let mut a = vec!["run".to_string()];
    if spec.remove_on_exit {
        a.push("--rm".into());
    }
    a.extend(s(&["-d", "--name"]));
    a.push(spec.name.clone());
    if let Some(net) = &spec.network {
        a.push("--network".into());
        a.push(net.clone());
    }
    for (k, v) in &spec.labels {
        a.push("--label".into());
        a.push(format!("{k}={v}"));
    }
    // What this workload holds of the node budget, summed back from the
    // daemon by [`allocations_args`] (zc#565).
    a.push("--label".into());
    a.push(format!(
        "{}={}",
        super::BUDGET_LABEL,
        spec.limits.budget_label()
    ));
    // Which of this machine's nodes started it (extra nodes only: the main
    // node's containers carry no label), so each node sums its own budget.
    if let Some(node) = crate::nodes::explicit_id() {
        a.push("--label".into());
        a.push(format!("{}={node}", crate::nodes::NODE_LABEL));
    }
    for (host, container) in &spec.mounts {
        a.push("-v".into());
        a.push(format!("{host}:{container}"));
    }
    if spec.hardened {
        a.extend(s(&[
            "--pids-limit",
            "4096",
            "--security-opt",
            "no-new-privileges",
        ]));
    }
    a.extend(limit_args(&spec.limits));
    for p in &spec.publish {
        a.push("-p".into());
        a.push(p.clone());
    }
    for (k, v) in &spec.env {
        a.push("-e".into());
        a.push(format!("{k}={v}"));
    }
    a.push(spec.image.clone());
    a.extend(spec.cmd.iter().cloned());
    a
}

/// `--cpus X --memory Ym --memory-swap Ym [--gpus G]`. cpu and memory are
/// always there. `--memory-swap` equal to `--memory` means no swap on top:
/// left out, Docker lets a container swap as much again as its memory cap
/// (where the host has swap), past what the owner lent.
pub fn limit_args(l: &Limits) -> Vec<String> {
    let mut a = vec![
        "--cpus".to_string(),
        l.cpus_flag(),
        "--memory".to_string(),
        l.memory_flag(),
        "--memory-swap".to_string(),
        l.memory_flag(),
    ];
    if let Some(g) = l.gpus.flag_value() {
        a.push("--gpus".into());
        a.push(g);
    }
    a
}

/// `docker ps` (without the leading `docker`) listing every running
/// container that holds a share of the node budget, one
/// `<name>|<budget label>` per line. Running only: a stopped or crashed
/// container has released its share.
pub fn allocations_args() -> Vec<String> {
    vec![
        "ps".to_string(),
        "--filter".to_string(),
        format!("label={}", super::BUDGET_LABEL),
        "--format".to_string(),
        format!(
            "{{{{.Names}}}}|{{{{.Label \"{}\"}}}}|{{{{.Label \"{}\"}}}}",
            super::BUDGET_LABEL,
            crate::nodes::NODE_LABEL
        ),
    ]
}

/// Parse [`allocations_args`]' output; a line that is not a budget label is
/// skipped (it was not started by this code).
pub fn parse_allocations(out: &str) -> Vec<super::Allocation> {
    parse_allocations_for(out, &crate::nodes::current_id())
}

/// [`parse_allocations`] for node `me`: only the workloads that node started
/// count against its budget. A container with no node label is the main
/// node's (everything started before nodes existed).
pub fn parse_allocations_for(out: &str, me: &str) -> Vec<super::Allocation> {
    out.lines()
        .filter_map(|l| {
            let mut f = l.trim().splitn(3, '|');
            let (name, label) = (f.next()?, f.next()?);
            let node = f.next().map(str::trim).filter(|n| !n.is_empty());
            if node.unwrap_or(crate::nodes::MAIN) != me {
                return None;
            }
            super::Allocation::from_label(name.trim(), label)
        })
        .collect()
}

/// `docker inspect -f` for the applied limits.
pub fn limits_inspect_argv(name: &str) -> Vec<String> {
    s(&[
        "docker",
        "inspect",
        "-f",
        "{{.HostConfig.NanoCpus}}|{{.HostConfig.Memory}}|{{json .HostConfig.DeviceRequests}}",
        name,
    ])
}

/// Parse [`limits_inspect_argv`]'s output. A container Docker reports with
/// no cpu or no memory cap is an error: that is exactly what this backend
/// exists to rule out.
pub fn parse_inspected_limits(out: &str) -> Result<Limits, String> {
    let mut f = out.trim().splitn(3, '|');
    let bad = || format!("unexpected docker inspect output: {out:?}");
    let nano: u64 = f
        .next()
        .ok_or_else(bad)?
        .trim()
        .parse()
        .map_err(|_| bad())?;
    let mem: u64 = f
        .next()
        .ok_or_else(bad)?
        .trim()
        .parse()
        .map_err(|_| bad())?;
    let dev = f.next().unwrap_or("null").trim();
    if nano == 0 || mem == 0 {
        return Err(format!(
            "container runs without a {} limit",
            if nano == 0 { "cpu" } else { "memory" }
        ));
    }
    let gpus = match serde_json::from_str::<serde_json::Value>(dev) {
        Ok(serde_json::Value::Array(reqs)) if !reqs.is_empty() => {
            let r = &reqs[0];
            let ids: Vec<String> = r["DeviceIDs"]
                .as_array()
                .map(|v| {
                    v.iter()
                        .filter_map(|x| x.as_str().map(String::from))
                        .collect()
                })
                .unwrap_or_default();
            match r["Count"].as_i64() {
                _ if !ids.is_empty() => Gpus::Spec(format!("device={}", ids.join(","))),
                Some(-1) => Gpus::All,
                Some(n) if n > 0 => Gpus::Count(n as u32),
                _ => Gpus::None,
            }
        }
        _ => Gpus::None,
    };
    Ok(Limits {
        cpu_millis: (nano / 1_000_000) as u32,
        memory_bytes: mem,
        gpus,
    })
}

/// Parse `sha256:<64 hex>` (also the tail of `repo@sha256:<64 hex>`), lower
/// case; `None` for anything else, a truncated or padded digest included.
pub fn parse_sha256_digest(v: &str) -> Option<String> {
    let d = v.trim().rsplit('@').next()?.to_ascii_lowercase();
    let hex = d.strip_prefix("sha256:")?;
    (hex.len() == 64 && hex.bytes().all(|b| b.is_ascii_hexdigit())).then_some(d)
}

/// Exact comparison of the digest the hub pinned with what the backend holds
/// for `image`: `inspected` is `{{.Id}}|{{json .RepoDigests}}`. The pinned
/// digest must be a full `sha256:<64 hex>` and equal the image id or one of
/// its repo digests, whole: a substring (a prefix, a digest embedded in other
/// text) never matches.
pub fn check_digest(image: &str, inspected: &str, expected: &str) -> Result<(), String> {
    let want = parse_sha256_digest(expected).ok_or_else(|| {
        format!("expected digest {expected:?} for {image} is not a full sha256:<64 hex> digest")
    })?;
    let (id, repo_digests) = inspected.split_once('|').unwrap_or((inspected, ""));
    let mut have: Vec<String> = parse_sha256_digest(id).into_iter().collect();
    if let Ok(list) = serde_json::from_str::<Vec<String>>(repo_digests.trim()) {
        have.extend(list.iter().filter_map(|d| parse_sha256_digest(d)));
    }
    if have.contains(&want) {
        Ok(())
    } else {
        Err(format!(
            "image {image} does not match the expected digest {want}"
        ))
    }
}

/// Docker as a [`Runtime`].
#[derive(Clone)]
pub struct DockerRuntime {
    cli: Arc<dyn DockerCli>,
}

impl DockerRuntime {
    pub fn new(cli: Arc<dyn DockerCli>) -> Self {
        Self { cli }
    }

    /// Against the real daemon.
    pub fn shell() -> Self {
        Self::new(Arc::new(DockerShell))
    }

    fn docker(&self, rest: &[&str]) -> Result<String, String> {
        let mut argv = vec!["docker".to_string()];
        argv.extend(rest.iter().map(|a| a.to_string()));
        self.cli.run(&argv)
    }
}

impl Runtime for DockerRuntime {
    fn kind(&self) -> RuntimeKind {
        RuntimeKind::Docker
    }

    fn probe(&self) -> Probe {
        match self.docker(&["version", "--format", "{{.Server.Version}}"]) {
            Ok(v) => Probe {
                kind: RuntimeKind::Docker,
                available: true,
                detail: format!("docker daemon responding (server {v})"),
            },
            Err(e) => Probe {
                kind: RuntimeKind::Docker,
                available: false,
                detail: e
                    .lines()
                    .next()
                    .unwrap_or("docker version failed")
                    .to_string(),
            },
        }
    }

    fn verify_image(&self, image: &str, expected_digest: Option<&str>) -> Result<(), String> {
        let out = self
            .docker(&[
                "image",
                "inspect",
                "--format",
                "{{.Id}}|{{json .RepoDigests}}",
                image,
            ])
            .map_err(|e| format!("image {image} is not on this machine: {e}"))?;
        let Some(want) = expected_digest else {
            return Ok(());
        };
        check_digest(image, &out, want)
    }

    fn start(&self, spec: &StartSpec) -> Result<String, String> {
        if spec.create_network {
            if let Some(net) = &spec.network {
                // Idempotent: "already exists" is the common failure, and a
                // real Docker problem surfaces from the run below, which
                // needs the network to exist.
                let _ = self.docker(&["network", "create", net]);
            }
        }
        let mut argv = vec!["docker".to_string()];
        argv.extend(run_argv(spec));
        self.cli.run(&argv)
    }

    fn exec(&self, name: &str, argv: &[String], detach: bool) -> Result<String, String> {
        let mut full = vec!["docker".to_string(), "exec".to_string()];
        if detach {
            full.push("-d".into());
        }
        full.push(name.to_string());
        full.extend(argv.iter().cloned());
        self.cli.run(&full)
    }

    fn stop(&self, name: &str) -> Result<(), String> {
        self.cli.run(&crate::up::docker_rm_argv(name)).map(|_| ())
    }

    fn limits(&self, name: &str) -> Result<Limits, String> {
        parse_inspected_limits(&self.cli.run(&limits_inspect_argv(name))?)
    }

    fn allocations(&self) -> Result<Vec<super::Allocation>, String> {
        let mut argv = vec!["docker".to_string()];
        argv.extend(allocations_args());
        Ok(parse_allocations(&self.cli.run(&argv)?))
    }

    fn health(&self, name: &str) -> Result<State, String> {
        let out = self.cli.run(&crate::up::docker_state_argv(name))?;
        crate::up::parse_container_state(&out)
            .ok_or_else(|| format!("unexpected docker inspect output: {out:?}"))
    }
}

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

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

    impl DockerCli 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 spec() -> StartSpec {
        StartSpec {
            name: "n".into(),
            image: "img".into(),
            network: Some("net".into()),
            create_network: true,
            labels: vec![("zakuro.share".into(), "1".into())],
            mounts: vec![],
            publish: vec!["127.0.0.1:3960:3960".into()],
            env: vec![],
            cmd: vec![],
            limits: Limits::new(1.5, 2.0, Gpus::None),
            remove_on_exit: true,
            hardened: false,
        }
    }

    #[test]
    fn every_start_carries_cpu_and_memory() {
        assert_eq!(
            run_argv(&spec()).join(" "),
            "run --rm -d --name n --network net --label zakuro.share=1 \
             --label zakuro.budget=1500m,2147483648 \
             --cpus 1.5 --memory 2048m --memory-swap 2048m -p 127.0.0.1:3960:3960 img"
        );
        let gpu = StartSpec {
            limits: Limits::new(1.0, 1.0, Gpus::All),
            ..spec()
        };
        assert!(run_argv(&gpu)
            .join(" ")
            .contains("--cpus 1 --memory 1024m --memory-swap 1024m --gpus all"));
    }

    #[test]
    fn start_creates_the_network_then_runs() {
        let d = Arc::new(Rec::default());
        let rt = DockerRuntime::new(d.clone());
        rt.start(&spec()).unwrap();
        let calls = d.calls.lock().unwrap().clone();
        assert_eq!(calls[0], "docker network create net");
        assert!(calls[1].starts_with("docker run --rm -d --name n"));
    }

    #[test]
    fn allocations_are_summed_back_from_the_running_containers() {
        let d = Arc::new(Rec::default());
        d.answer.lock().unwrap().push((
            "docker ps --filter label=zakuro.budget --format {{.Names}}|{{.Label \"zakuro.budget\"}}",
            Ok("zk-dep-a-v1|500m,1073741824\nzakuro-share-w0|1000m,2147483648\nodd|garbage".into()),
        ));
        let rt = DockerRuntime::new(d.clone());
        let got = rt.allocations().unwrap();
        assert_eq!(
            got,
            vec![
                super::super::Allocation {
                    name: "zk-dep-a-v1".into(),
                    cpu_millis: 500,
                    memory_bytes: 1 << 30,
                },
                super::super::Allocation {
                    name: "zakuro-share-w0".into(),
                    cpu_millis: 1000,
                    memory_bytes: 2 << 30,
                },
            ]
        );
        d.answer
            .lock()
            .unwrap()
            .insert(0, ("docker ps", Err("Cannot connect".into())));
        assert!(rt.allocations().is_err());
    }

    #[test]
    fn applied_limits_are_read_back() {
        let l = parse_inspected_limits("1500000000|2147483648|null").unwrap();
        assert_eq!(l, Limits::new(1.5, 2.0, Gpus::None));
        let all = parse_inspected_limits(
            r#"1000000000|1073741824|[{"Driver":"","Count":-1,"DeviceIDs":null,"Capabilities":[["gpu"]]}]"#,
        )
        .unwrap();
        assert_eq!(all.gpus, Gpus::All);
        let dev = parse_inspected_limits(
            r#"1000000000|1073741824|[{"Driver":"","Count":0,"DeviceIDs":["0"],"Capabilities":[["gpu"]]}]"#,
        )
        .unwrap();
        assert_eq!(dev.gpus, Gpus::Spec("device=0".into()));
        assert!(parse_inspected_limits("0|0|null")
            .unwrap_err()
            .contains("without a cpu limit"));
        assert!(parse_inspected_limits("1000|0|null")
            .unwrap_err()
            .contains("without a memory limit"));
        assert!(parse_inspected_limits("garbage").is_err());
    }

    const D_ID: &str = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    const D_REPO: &str = "sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";

    #[test]
    fn verify_image_checks_presence_and_digest() {
        let d = Arc::new(Rec::default());
        d.answer
            .lock()
            .unwrap()
            .push(("image inspect", Ok(format!(r#"{D_ID}|["img@{D_REPO}"]"#))));
        let rt = DockerRuntime::new(d.clone());
        assert!(rt.verify_image("img", None).is_ok());
        assert!(rt
            .verify_image("img", Some(&format!("img@{D_REPO}")))
            .is_ok());
        assert!(rt.verify_image("img", Some(D_REPO)).is_ok());
        assert!(rt.verify_image("img", Some(D_ID)).is_ok());
        let other = "sha256:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc";
        assert!(rt
            .verify_image("img", Some(other))
            .unwrap_err()
            .contains("does not match"));
        let missing = Arc::new(Rec::default());
        missing
            .answer
            .lock()
            .unwrap()
            .push(("image inspect", Err("No such image".into())));
        assert!(DockerRuntime::new(missing)
            .verify_image("img", None)
            .unwrap_err()
            .contains("not on this machine"));
    }

    #[test]
    fn a_digest_is_compared_whole_never_as_a_substring() {
        let inspected = format!(r#"{D_ID}|["img@{D_REPO}"]"#);
        // A prefix, a truncation, or the right hex under another algorithm.
        let hex = D_REPO.trim_start_matches("sha256:");
        for bad in [
            &D_REPO[..40],
            &D_REPO[..D_REPO.len() - 1],
            &format!("sha512:{hex}"),
            hex,
            "bbbb",
            "",
            "latest",
            &format!("sha256:{hex}0"),
        ] {
            let e = check_digest("img", &inspected, bad).unwrap_err();
            assert!(
                e.contains("not a full sha256") || e.contains("does not match"),
                "{bad:?}: {e}"
            );
        }
        // A digest that only appears INSIDE another one's text is not a match.
        let embedded = format!("{D_REPO}ffff");
        assert!(check_digest("img", &inspected, &embedded).is_err());
        // Case does not matter for hex; the repo prefix is ignored.
        assert!(check_digest(
            "img",
            &inspected,
            &D_REPO.to_ascii_uppercase().replace("SHA256", "sha256")
        )
        .is_ok());
        assert!(check_digest("img", &inspected, &format!("registry.io/x/img@{D_REPO}")).is_ok());
        assert_eq!(parse_sha256_digest(D_ID).as_deref(), Some(D_ID));
        assert_eq!(parse_sha256_digest("sha256:abc"), None);
    }

    #[test]
    fn exec_stop_and_health_use_the_shared_argv() {
        let d = Arc::new(Rec::default());
        d.answer.lock().unwrap().push((
            "inspect -f {{.State",
            Ok("true|2026-11-07T00:00:00Z|0".into()),
        ));
        let rt = DockerRuntime::new(d.clone());
        rt.exec("n", &["sh".into(), "-c".into(), "true".into()], true)
            .unwrap();
        rt.stop("n").unwrap();
        assert!(rt.health("n").unwrap().running);
        let calls = d.calls.lock().unwrap().clone();
        assert_eq!(calls[0], "docker exec -d n sh -c true");
        assert_eq!(calls[1], "docker rm -f n");
    }

    #[test]
    fn a_failed_probe_says_why() {
        let d = Arc::new(Rec::default());
        d.answer.lock().unwrap().push((
            "version",
            Err("Cannot connect to the Docker daemon at unix:///var/run/docker.sock".into()),
        ));
        let p = DockerRuntime::new(d).probe();
        assert!(!p.available);
        assert!(p.detail.contains("Cannot connect"), "{}", p.detail);
    }

    /// The lab scenario in miniature (zc#510 "a stress job cannot exceed the
    /// limit"), against a real daemon: a container started by this backend
    /// under a 0.5 cpu / 64 MiB budget burns two cores and allocates past its
    /// memory. Its cgroup must cap cpu at half a core and OOM-kill the
    /// allocation. Needs Docker and `alpine`; run with
    /// `ZC_LIVE_DOCKER=1 cargo nextest run -E 'test(live_stress)' --run-ignored all`.
    #[test]
    #[ignore = "needs a Docker daemon; set ZC_LIVE_DOCKER=1"]
    fn live_stress_is_capped_at_the_budget() {
        if std::env::var("ZC_LIVE_DOCKER").ok().as_deref() != Some("1") {
            eprintln!("ZC_LIVE_DOCKER != 1: skipped");
            return;
        }
        let rt = DockerRuntime::shell();
        assert!(rt.probe().available, "{}", rt.probe().detail);
        let name = format!("zc-live-stress-{}", std::process::id());
        let budget = Limits::new(0.5, 64.0 / 1024.0, Gpus::None);
        let spec = StartSpec {
            name: name.clone(),
            image: "alpine:3.20".into(),
            network: None,
            create_network: false,
            labels: vec![("zakuro.test".into(), "stress".into())],
            mounts: vec![],
            publish: vec![],
            env: vec![],
            // Two busy loops for 3 s, then report cpu usage (usec) over the
            // window, then try to hold 256 MiB in memory.
            cmd: vec![
                "sh".into(),
                "-c".into(),
                "a=$(awk '/usage_usec/{print $2}' /sys/fs/cgroup/cpu.stat); \
                 (timeout 3 sh -c 'while :; do :; done' &); \
                 (timeout 3 sh -c 'while :; do :; done' &); sleep 3.2; \
                 b=$(awk '/usage_usec/{print $2}' /sys/fs/cgroup/cpu.stat); \
                 echo cpu_usec=$((b-a)) > /tmp/cpu; sleep 600"
                    .into(),
            ],
            limits: budget.clone(),
            remove_on_exit: false,
            hardened: true,
        };
        let _ = rt.stop(&name);
        rt.start(&spec).expect("start");
        let cleanup = scopeguard(&rt, &name);
        assert_eq!(
            rt.limits(&name).unwrap(),
            budget,
            "docker applied the budget"
        );
        std::thread::sleep(std::time::Duration::from_secs(5));
        let cpu = rt
            .exec(&name, &["cat".into(), "/tmp/cpu".into()], false)
            .expect("cpu sample");
        let usec: u64 = cpu.trim().trim_start_matches("cpu_usec=").parse().unwrap();
        // Uncapped this is ~6 s of cpu (2 loops x 3 s). Capped at 0.5 it is
        // ~1.6 s; allow scheduler slack, but well under one full core.
        assert!(usec < 2_400_000, "cpu not capped: {usec} usec in 3.2 s");
        // A 256 MiB buffer in a 64 MiB container (no swap on top): the
        // kernel OOM-kills it.
        let mem = rt.exec(
            &name,
            &[
                "sh".into(),
                "-c".into(),
                "dd if=/dev/zero of=/dev/null bs=256M count=1 2>/dev/null && echo survived".into(),
            ],
            false,
        );
        assert!(
            !mem.as_deref().unwrap_or("").contains("survived"),
            "memory not capped: {mem:?}"
        );
        drop(cleanup);
    }

    struct Guard<'a>(&'a DockerRuntime, String);
    impl Drop for Guard<'_> {
        fn drop(&mut self) {
            let _ = self.0.stop(&self.1);
        }
    }
    fn scopeguard<'a>(rt: &'a DockerRuntime, name: &str) -> Guard<'a> {
        Guard(rt, name.to_string())
    }
}