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
//! Job spec v1 (ADR ZC-0021 ยง1): one TOML or JSON document describing a
//! container job for the hub's Spaces queue. Parsing and validation name the
//! field at fault, because a spec is edited by hand and a typo in a price or
//! an input path must not become money spent or a job that dies an hour later.

use ring::digest::{Context, SHA256};
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::BTreeMap;
use std::io::Read;
use std::path::{Path, PathBuf};

/// The largest input file one job takes. Uploads are one presigned PUT each,
/// read into memory, so this also bounds the agent's memory.
pub const MAX_INPUT_BYTES: u64 = 512 * 1024 * 1024;
/// The hub takes at most this many batches per upload call.
pub const MAX_BATCHES: usize = 100_000;

#[derive(Debug, Clone, PartialEq)]
pub struct SpecError {
    pub field: String,
    pub message: String,
}

impl SpecError {
    pub fn new(field: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            field: field.into(),
            message: message.into(),
        }
    }
}

impl std::fmt::Display for SpecError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.field.is_empty() {
            write!(f, "{}", self.message)
        } else {
            write!(f, "{}: {}", self.field, self.message)
        }
    }
}

/// TOML integers and floats are different types; a price of `50` must work.
fn de_num<'de, D: Deserializer<'de>>(d: D) -> Result<f64, D::Error> {
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum N {
        I(i64),
        F(f64),
    }
    Ok(match N::deserialize(d)? {
        N::I(i) => i as f64,
        N::F(f) => f,
    })
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Resources {
    pub cpus: u32,
    #[serde(deserialize_with = "de_num")]
    pub memory_gb: f64,
    #[serde(default)]
    pub gpus: u32,
    #[serde(default, deserialize_with = "de_num")]
    pub min_vram_gb: f64,
    pub max_runtime_s: u64,
    /// The hub's ETA input; defaults to `max_runtime_s`.
    #[serde(default)]
    pub est_runtime_s: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct BatchSpec {
    /// Name inside the container โ†’ local file.
    pub inputs: BTreeMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct JobSpec {
    pub name: String,
    #[serde(default)]
    pub description: Option<String>,
    /// A digest-pinned reference, or a tag the hub resolves and pins.
    pub image: String,
    /// Not carried by the hub's Spaces API today: refused rather than ignored.
    #[serde(default)]
    pub cmd: Option<Vec<String>>,
    /// `public` / `private` (visibility), or a squad id.
    #[serde(default)]
    pub squad: Option<String>,
    /// Yen. The hub holds exactly `price ร— batches`; the outbox never raises it.
    #[serde(deserialize_with = "de_num")]
    pub max_price_per_batch: f64,
    pub resources: Resources,
    #[serde(default, rename = "batch", alias = "batches")]
    pub batch: Vec<BatchSpec>,
    /// Fail the job when a batch's receipt cannot be fetched (default: say so).
    #[serde(default)]
    pub require_receipts: bool,
    /// Resubmitting the same id returns the job already made.
    #[serde(default)]
    pub request_id: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Format {
    Json,
    Toml,
}

pub fn parse(text: &str, format: Option<Format>) -> Result<JobSpec, SpecError> {
    let format = format.unwrap_or_else(|| {
        if text.trim_start().starts_with('{') {
            Format::Json
        } else {
            Format::Toml
        }
    });
    match format {
        Format::Json => serde_json::from_str(text).map_err(|e| SpecError::new("", e.to_string())),
        Format::Toml => toml::from_str(text).map_err(|e| SpecError::new("", e.to_string())),
    }
}

/// `repo[:tag]` or `repo@sha256:<64 hex>`: what was given, and the digest if
/// it was pinned.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImageRef {
    pub as_given: String,
    pub pinned_digest: Option<String>,
}

pub fn parse_image(image: &str) -> Result<ImageRef, SpecError> {
    let f = "image";
    if image.is_empty() {
        return Err(SpecError::new(f, "must not be empty"));
    }
    if image.len() > 512 || image.chars().any(char::is_whitespace) {
        return Err(SpecError::new(
            f,
            "must be one reference of at most 512 characters, without spaces",
        ));
    }
    match image.split_once('@') {
        None => Ok(ImageRef {
            as_given: image.into(),
            pinned_digest: None,
        }),
        Some((repo, digest)) => {
            let hex = digest.strip_prefix("sha256:").unwrap_or("");
            if repo.is_empty() || hex.len() != 64 || !hex.bytes().all(|b| b.is_ascii_hexdigit()) {
                return Err(SpecError::new(
                    f,
                    "a digest-pinned image is `repo@sha256:` followed by 64 hex characters",
                ));
            }
            Ok(ImageRef {
                as_given: image.into(),
                pinned_digest: Some(format!("sha256:{}", hex.to_ascii_lowercase())),
            })
        }
    }
}

/// How the spec's `squad` maps onto the hub's `visibility` and `squad_id`.
pub fn visibility_and_squad(spec: &JobSpec) -> (&'static str, Option<String>) {
    match spec.squad.as_deref().map(str::trim) {
        None | Some("") | Some("public") => ("public", None),
        Some("private") => ("private", None),
        Some(id) => ("public", Some(id.to_string())),
    }
}

/// Decimal places the hub keeps for a price (`decimal_places=6`).
fn at_most_six_places(p: f64) -> bool {
    ((p * 1e6).round() / 1e6 - p).abs() < 1e-9
}

/// One local file a job uploads, identified by content.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct InputFile {
    pub path: String,
    pub sha256: String,
    pub size: u64,
}

/// A spec that passed validation, with every input hashed.
#[derive(Debug)]
pub struct Checked {
    pub image: ImageRef,
    /// Distinct files, by content.
    pub files: Vec<InputFile>,
    /// Per batch: container name โ†’ index into `files`.
    pub batches: Vec<BTreeMap<String, usize>>,
}

pub fn sha256_file(path: &Path) -> std::io::Result<(String, u64)> {
    let mut f = std::fs::File::open(path)?;
    let mut ctx = Context::new(&SHA256);
    let mut buf = vec![0u8; 64 * 1024];
    let mut size = 0u64;
    loop {
        let n = f.read(&mut buf)?;
        if n == 0 {
            break;
        }
        ctx.update(&buf[..n]);
        size += n as u64;
    }
    Ok((hex::encode(ctx.finish().as_ref()), size))
}

/// Resolve relative input paths against `base` (the spec file's directory):
/// the CLI does this before it sends the spec, because the agent does not
/// share the caller's working directory.
pub fn resolve_paths(spec: &mut JobSpec, base: &Path) {
    for b in &mut spec.batch {
        for p in b.inputs.values_mut() {
            let path = PathBuf::from(&*p);
            if path.is_relative() {
                *p = base.join(path).display().to_string();
            }
        }
    }
}

fn valid_input_name(n: &str) -> bool {
    !n.is_empty() && n.len() <= 255 && !n.contains('/') && n != "." && n != ".."
}

pub fn validate(spec: &JobSpec) -> Result<Checked, SpecError> {
    fn e(f: impl Into<String>, m: impl Into<String>) -> SpecError {
        SpecError::new(f, m)
    }
    let name = spec.name.trim();
    if name.is_empty() || spec.name.chars().count() > 128 || spec.name.chars().any(char::is_control)
    {
        return Err(e("name", "1 to 128 characters, no control characters"));
    }
    if spec.description.as_ref().is_some_and(|d| d.len() > 3000) {
        return Err(e("description", "at most 3000 characters"));
    }
    let image = parse_image(&spec.image)?;
    if spec.cmd.is_some() {
        return Err(e(
            "cmd",
            "the hub's Spaces queue has no command override (a Space is an image plus batches); \
             bake the command into the image's entrypoint",
        ));
    }
    if let Some(s) = &spec.squad {
        if s.len() > 36 {
            return Err(e(
                "squad",
                "`public`, `private` or a squad id (at most 36 characters)",
            ));
        }
    }
    let p = spec.max_price_per_batch;
    if !p.is_finite() || p <= 0.0 {
        return Err(e("max_price_per_batch", "must be a price above 0 (yen)"));
    }
    if p > 1e9 || !at_most_six_places(p) {
        return Err(e(
            "max_price_per_batch",
            "at most 1000000000, with at most 6 decimal places",
        ));
    }
    let r = &spec.resources;
    if !(1..=4096).contains(&r.cpus) {
        return Err(e("resources.cpus", "between 1 and 4096"));
    }
    if !r.memory_gb.is_finite() || r.memory_gb <= 0.0 || r.memory_gb > 65536.0 {
        return Err(e("resources.memory_gb", "above 0 and at most 65536"));
    }
    if r.gpus > 64 {
        return Err(e("resources.gpus", "at most 64"));
    }
    if !r.min_vram_gb.is_finite() || !(0.0..=4096.0).contains(&r.min_vram_gb) {
        return Err(e("resources.min_vram_gb", "between 0 and 4096"));
    }
    if r.max_runtime_s == 0 || r.max_runtime_s > 7 * 86400 {
        return Err(e("resources.max_runtime_s", "between 1 second and 7 days"));
    }
    if let Some(est) = r.est_runtime_s {
        if est == 0 || est > r.max_runtime_s {
            return Err(e(
                "resources.est_runtime_s",
                "above 0 and not over max_runtime_s",
            ));
        }
    }
    if let Some(id) = &spec.request_id {
        if id.is_empty() || id.len() > 128 {
            return Err(e("request_id", "1 to 128 characters"));
        }
    }
    if spec.batch.is_empty() {
        return Err(e("batch", "at least one [[batch]] is needed"));
    }
    if spec.batch.len() > MAX_BATCHES {
        return Err(e("batch", format!("at most {MAX_BATCHES} batches")));
    }
    let mut files: Vec<InputFile> = Vec::new();
    // Hash each distinct path once: a corpus read by every batch is read once.
    let mut by_path: BTreeMap<String, usize> = BTreeMap::new();
    let mut batches = Vec::with_capacity(spec.batch.len());
    for (i, b) in spec.batch.iter().enumerate() {
        if b.inputs.is_empty() {
            return Err(e(format!("batch[{i}].inputs"), "at least one input file"));
        }
        let mut resolved = BTreeMap::new();
        for (name, path) in &b.inputs {
            let field = format!("batch[{i}].inputs.{name}");
            if !valid_input_name(name) {
                return Err(e(
                    format!("batch[{i}].inputs"),
                    format!("the name `{name}` must be non-empty, without `/`"),
                ));
            }
            let pb = Path::new(path);
            if !pb.is_absolute() {
                return Err(e(
                    field,
                    format!(
                        "`{path}` is not an absolute path (the agent does not share your working directory)"
                    ),
                ));
            }
            let idx = match by_path.get(path) {
                Some(i) => *i,
                None => {
                    let meta = std::fs::metadata(pb)
                        .map_err(|err| e(field.clone(), format!("`{path}`: {err}")))?;
                    if !meta.is_file() {
                        return Err(e(field, format!("`{path}` is not a regular file")));
                    }
                    if meta.len() > MAX_INPUT_BYTES {
                        return Err(e(
                            field,
                            format!("`{path}` is over {} MiB", MAX_INPUT_BYTES >> 20),
                        ));
                    }
                    let (sha256, size) = sha256_file(pb).map_err(|err| {
                        e(field.clone(), format!("`{path}` is not readable: {err}"))
                    })?;
                    files.push(InputFile {
                        path: path.clone(),
                        sha256,
                        size,
                    });
                    by_path.insert(path.clone(), files.len() - 1);
                    files.len() - 1
                }
            };
            resolved.insert(name.clone(), idx);
        }
        batches.push(resolved);
    }
    Ok(Checked {
        image,
        files,
        batches,
    })
}

#[cfg(test)]
pub(crate) fn test_dir(tag: &str) -> PathBuf {
    let d = std::env::temp_dir().join(format!("zc-outbox-{tag}-{}-{}", std::process::id(), {
        use std::sync::atomic::{AtomicU64, Ordering};
        static N: AtomicU64 = AtomicU64::new(0);
        N.fetch_add(1, Ordering::Relaxed)
    }));
    std::fs::create_dir_all(&d).unwrap();
    d
}

/// A valid two-batch spec over real files in `dir`, for tests.
#[cfg(test)]
pub(crate) fn sample_spec(dir: &Path) -> JobSpec {
    for (n, body) in [("a.txt", "alpha\n"), ("b.txt", "beta\n")] {
        std::fs::write(dir.join(n), body).unwrap();
    }
    let toml = format!(
        r#"
name = "digest-run"
image = "registry.example/app:1"
max_price_per_batch = 50
[resources]
cpus = 1
memory_gb = 1
max_runtime_s = 180
[[batch]]
inputs = {{ "part.txt" = "{a}" }}
[[batch]]
inputs = {{ "part.txt" = "{b}" }}
"#,
        a = dir.join("a.txt").display(),
        b = dir.join("b.txt").display()
    );
    parse(&toml, None).unwrap()
}

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

    #[test]
    fn toml_and_json_give_the_same_spec() {
        let dir = test_dir("spec-fmt");
        let t = sample_spec(&dir);
        let j = serde_json::to_string(&t).unwrap();
        assert_eq!(parse(&j, None).unwrap(), t);
        assert_eq!(parse(&j, Some(Format::Json)).unwrap(), t);
        assert_eq!(t.max_price_per_batch, 50.0, "an integer price is a price");
        assert_eq!(t.batch.len(), 2);
    }

    #[test]
    fn a_valid_spec_hashes_each_distinct_file_once() {
        let dir = test_dir("spec-ok");
        let mut s = sample_spec(&dir);
        s.batch.push(s.batch[0].clone()); // third batch re-reads a.txt
        let c = validate(&s).unwrap();
        assert_eq!(c.files.len(), 2);
        assert_eq!(c.batches.len(), 3);
        assert_eq!(c.batches[0], c.batches[2]);
        let (sha, _) = sha256_file(&dir.join("a.txt")).unwrap();
        assert_eq!((c.files[0].sha256.clone(), c.files[0].size), (sha, 6));
        assert_eq!(c.image.pinned_digest, None);
        assert_eq!(c.image.as_given, "registry.example/app:1");
    }

    #[test]
    fn errors_name_the_field() {
        let dir = test_dir("spec-err");
        let ok = sample_spec(&dir);
        let field = |s: &JobSpec| validate(s).unwrap_err().field;

        let mut s = ok.clone();
        s.name = " ".into();
        assert_eq!(field(&s), "name");
        let mut s = ok.clone();
        s.max_price_per_batch = 0.0;
        assert_eq!(field(&s), "max_price_per_batch");
        let mut s = ok.clone();
        s.max_price_per_batch = 0.0000001;
        assert_eq!(field(&s), "max_price_per_batch");
        let mut s = ok.clone();
        s.resources.cpus = 0;
        assert_eq!(field(&s), "resources.cpus");
        let mut s = ok.clone();
        s.resources.est_runtime_s = Some(999);
        assert_eq!(field(&s), "resources.est_runtime_s");
        let mut s = ok.clone();
        s.batch.clear();
        assert_eq!(field(&s), "batch");
        let mut s = ok.clone();
        s.batch[1].inputs.clear();
        assert_eq!(field(&s), "batch[1].inputs");
        let mut s = ok.clone();
        s.batch[0].inputs.insert("a/b".into(), "/x".into());
        assert_eq!(field(&s), "batch[0].inputs");
        let mut s = ok.clone();
        s.cmd = Some(vec!["true".into()]);
        let err = validate(&s).unwrap_err();
        assert_eq!(err.field, "cmd");
        assert!(err.message.contains("entrypoint"));
        let mut s = ok.clone();
        s.image = "a b".into();
        assert_eq!(field(&s), "image");
    }

    #[test]
    fn input_files_must_exist_be_regular_and_be_absolute() {
        let dir = test_dir("spec-files");
        let ok = sample_spec(&dir);
        let mut s = ok.clone();
        s.batch[1].inputs.insert(
            "part.txt".into(),
            dir.join("nope.txt").display().to_string(),
        );
        let e = validate(&s).unwrap_err();
        assert_eq!(e.field, "batch[1].inputs.part.txt");
        assert!(e.message.contains("nope.txt"), "{e}");

        let mut s = ok.clone();
        s.batch[0]
            .inputs
            .insert("part.txt".into(), dir.display().to_string());
        assert!(validate(&s).unwrap_err().message.contains("regular file"));

        let mut s = ok.clone();
        s.batch[0].inputs.insert("part.txt".into(), "a.txt".into());
        assert!(validate(&s).unwrap_err().message.contains("absolute"));

        // The CLI resolves against the spec file's directory first.
        resolve_paths(&mut s, &dir);
        validate(&s).unwrap();
    }

    #[test]
    fn an_unreadable_input_is_refused_at_submit() {
        use std::os::unix::fs::PermissionsExt;
        let dir = test_dir("spec-perm");
        let s = sample_spec(&dir);
        let a = dir.join("a.txt");
        std::fs::set_permissions(&a, std::fs::Permissions::from_mode(0o000)).unwrap();
        // Root reads anything; only assert when the mode really bites.
        if std::fs::File::open(&a).is_err() {
            let e = validate(&s).unwrap_err();
            assert_eq!(e.field, "batch[0].inputs.part.txt");
            assert!(e.message.contains("not readable"), "{e}");
        }
        std::fs::set_permissions(&a, std::fs::Permissions::from_mode(0o644)).unwrap();
    }

    #[test]
    fn images_are_digest_pinned_or_recorded_as_given() {
        let d = "a".repeat(64);
        let p = parse_image(&format!("r.io/app@sha256:{d}")).unwrap();
        assert_eq!(p.pinned_digest, Some(format!("sha256:{d}")));
        let t = parse_image("r.io/app:latest").unwrap();
        assert_eq!(
            (t.as_given.as_str(), t.pinned_digest),
            ("r.io/app:latest", None)
        );
        assert!(parse_image("r.io/app@sha256:abc").is_err());
        assert!(parse_image("r.io/app@md5:abc").is_err());
        assert!(parse_image("").is_err());
    }

    #[test]
    fn unknown_keys_are_refused_so_a_typo_cannot_drop_the_price_ceiling() {
        let e = parse(
            "name='x'\nimage='i'\nmax_price='5'\nmax_price_per_batch=1\n[resources]\ncpus=1\nmemory_gb=1\nmax_runtime_s=1\n",
            None,
        )
        .unwrap_err();
        assert!(e.message.contains("max_price"), "{e}");
    }

    #[test]
    fn squad_maps_to_visibility_or_a_squad_id() {
        let dir = test_dir("spec-squad");
        let mut s = sample_spec(&dir);
        assert_eq!(visibility_and_squad(&s), ("public", None));
        s.squad = Some("public".into());
        assert_eq!(visibility_and_squad(&s), ("public", None));
        s.squad = Some("private".into());
        assert_eq!(visibility_and_squad(&s), ("private", None));
        s.squad = Some("sq-1".into());
        assert_eq!(visibility_and_squad(&s), ("public", Some("sq-1".into())));
    }
}