zc2 0.0.51

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
//! `/v2/submissions` (ADR ZC-0021 ยง6): behind the same guard layer as the
//! rest of `/v2`, with the same `{"error":{"code","message"}}` envelope.

use super::store::{Job, State};
use super::{spec::JobSpec, SubmitError};
use crate::agent::core::Core;
use axum::extract::rejection::JsonRejection;
use axum::extract::{Path, State as AxState};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Json, Router};
use std::sync::Arc;

pub fn routes() -> Router<Arc<Core>> {
    Router::new()
        .route(
            "/v2/submissions",
            post(post_submission).get(list_submissions),
        )
        .route(
            "/v2/submissions/:id",
            get(get_submission).delete(delete_submission),
        )
}

fn error(status: u16, code: &str, message: &str) -> Response {
    (
        StatusCode::from_u16(status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
        Json(serde_json::json!({ "error": { "code": code, "message": message } })),
    )
        .into_response()
}

fn announce(core: &Core, job: &Job) {
    core.events.publish(
        "submission.state",
        job.summary(core.outbox.store.cancel_requested(&job.id)),
    );
}

/// `POST /v2/submissions`: the body is a job spec as JSON (input paths
/// absolute: `zc job submit` resolves them). Answers 202 with the job id once
/// the job is on disk.
async fn post_submission(
    AxState(core): AxState<Arc<Core>>,
    body: Result<Json<JobSpec>, JsonRejection>,
) -> Response {
    let spec = match body {
        Ok(Json(s)) => s,
        Err(e) => return error(400, "invalid_input", &e.body_text()),
    };
    let c = core.clone();
    // Validation hashes the input files: keep it off the async threads.
    let res = tokio::task::spawn_blocking(move || c.outbox.submit(spec)).await;
    match res {
        Ok(Ok((job, duplicate))) => {
            if !duplicate {
                announce(&core, &job);
            }
            (
                StatusCode::ACCEPTED,
                Json(serde_json::json!({
                    "id": job.id,
                    "state": job.state.as_str(),
                    "duplicate": duplicate,
                    "submission": job.summary(false),
                })),
            )
                .into_response()
        }
        Ok(Err(SubmitError::Spec(e))) => (
            StatusCode::BAD_REQUEST,
            Json(serde_json::json!({ "error": {
                "code": "invalid_input",
                "message": e.to_string(),
                "field": e.field,
            } })),
        )
            .into_response(),
        Ok(Err(SubmitError::Io(m))) => error(500, "outbox_error", &m),
        Err(e) => error(500, "outbox_error", &e.to_string()),
    }
}

/// `GET /v2/submissions`: newest first.
async fn list_submissions(AxState(core): AxState<Arc<Core>>) -> Response {
    let store = &core.outbox.store;
    let mut jobs = store.list();
    jobs.reverse();
    let rows: Vec<_> = jobs
        .iter()
        .map(|j| j.summary(store.cancel_requested(&j.id)))
        .collect();
    Json(serde_json::json!({ "submissions": rows })).into_response()
}

async fn get_submission(AxState(core): AxState<Arc<Core>>, Path(id): Path<String>) -> Response {
    let store = &core.outbox.store;
    match store.load(&id) {
        Some(j) => {
            Json(j.detail(store.cancel_requested(&id), store.results_root())).into_response()
        }
        None => error(404, "not_found", "no such submission"),
    }
}

/// `DELETE /v2/submissions/{id}`: cancel. Asks the dispatcher to stop the job
/// and have the hub cancel the Space (refunding the unused hold); answers 202
/// because the dispatcher does the work. A job already over, or already
/// collecting finished work, is a 409.
async fn delete_submission(AxState(core): AxState<Arc<Core>>, Path(id): Path<String>) -> Response {
    let store = &core.outbox.store;
    let Some(job) = store.load(&id) else {
        return error(404, "not_found", "no such submission");
    };
    if job.state.terminal() {
        return error(
            409,
            "conflict",
            &format!("the submission is already {}", job.state.as_str()),
        );
    }
    if job.state == State::Collecting {
        return error(
            409,
            "conflict",
            "the work is finished and its results are being collected; nothing is left to cancel",
        );
    }
    if let Err(e) = store.request_cancel(&id) {
        return error(500, "outbox_error", &e.to_string());
    }
    core.outbox.wake.notify_one();
    announce(&core, &job);
    (
        StatusCode::ACCEPTED,
        Json(serde_json::json!({
            "id": id,
            "state": job.state.as_str(),
            "cancel_requested": true,
        })),
    )
        .into_response()
}

#[cfg(test)]
mod tests {
    use super::super::spec::{sample_spec, test_dir};
    use super::{Arc, Core};
    use crate::agent::core::tests::core_with;
    use crate::agent::server::router;
    use std::time::Duration;

    fn serve(core: Arc<Core>, token: &str) -> u16 {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        listener.set_nonblocking(true).unwrap();
        let app = router(core, token.to_string(), port);
        std::thread::spawn(move || {
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .unwrap();
            rt.block_on(async move {
                let l = tokio::net::TcpListener::from_std(listener).unwrap();
                axum::serve(l, app).await.unwrap();
            });
        });
        port
    }

    fn call(
        method: &str,
        port: u16,
        path: &str,
        token: Option<&str>,
        body: Option<&serde_json::Value>,
    ) -> (u16, serde_json::Value) {
        let agent = ureq::Agent::new_with_config(
            ureq::Agent::config_builder()
                .http_status_as_error(false)
                .timeout_global(Some(Duration::from_secs(10)))
                .build(),
        );
        let url = format!("http://127.0.0.1:{port}{path}");
        let auth = token.map(|t| format!("Bearer {t}")).unwrap_or_default();
        let resp = match (method, body) {
            ("GET", _) => agent
                .get(&url)
                .header("Authorization", auth.as_str())
                .call(),
            ("DELETE", _) => agent
                .delete(&url)
                .header("Authorization", auth.as_str())
                .call(),
            (_, Some(b)) => agent
                .post(&url)
                .header("Authorization", auth.as_str())
                .send_json(b),
            (_, None) => agent
                .post(&url)
                .header("Authorization", auth.as_str())
                .send_empty(),
        }
        .unwrap();
        let status = resp.status().as_u16();
        (
            status,
            resp.into_body()
                .read_json()
                .unwrap_or(serde_json::Value::Null),
        )
    }

    #[test]
    fn submissions_are_guarded_validated_stored_listed_and_cancelled() {
        let core = core_with(0);
        let port = serve(core.clone(), "tok");
        let inputs = test_dir("routes-in");
        let spec = serde_json::to_value(sample_spec(&inputs)).unwrap();

        // Same guard as the rest of /v2.
        assert_eq!(
            call("POST", port, "/v2/submissions", None, Some(&spec)).0,
            401
        );
        assert_eq!(call("GET", port, "/v2/submissions", None, None).0, 401);

        // A bad spec names the field and stores nothing.
        let mut bad = spec.clone();
        bad["max_price_per_batch"] = serde_json::json!(0);
        let (st, v) = call("POST", port, "/v2/submissions", Some("tok"), Some(&bad));
        assert_eq!(st, 400);
        assert_eq!(v["error"]["code"], "invalid_input");
        assert_eq!(v["error"]["field"], "max_price_per_batch");
        let (_, v) = call("GET", port, "/v2/submissions", Some("tok"), None);
        assert_eq!(v["submissions"].as_array().unwrap().len(), 0);
        let (st, _) = call(
            "POST",
            port,
            "/v2/submissions",
            Some("tok"),
            Some(&serde_json::json!({"nope": 1})),
        );
        assert_eq!(st, 400);

        // A good one is on disk when the answer comes.
        let before = core.events.last_id();
        let (st, v) = call("POST", port, "/v2/submissions", Some("tok"), Some(&spec));
        assert_eq!(st, 202, "{v}");
        let id = v["id"].as_str().unwrap().to_string();
        assert_eq!(v["state"], "queued");
        assert!(core.outbox.store.load(&id).is_some());
        assert!(
            core.events.last_id() > before,
            "submission.state was published"
        );

        let (st, v) = call(
            "GET",
            port,
            &format!("/v2/submissions/{id}"),
            Some("tok"),
            None,
        );
        assert_eq!(st, 200);
        assert_eq!(v["batches"], 2);
        assert_eq!(v["max_price_per_batch"], 50.0);
        let (_, v) = call("GET", port, "/v2/submissions", Some("tok"), None);
        assert_eq!(v["submissions"][0]["id"], id.as_str());
        assert_eq!(
            call(
                "GET",
                port,
                "/v2/submissions/j_000000000000",
                Some("tok"),
                None
            )
            .0,
            404
        );
        assert_eq!(
            call("GET", port, "/v2/submissions/..%2Fx", Some("tok"), None).0,
            404
        );

        // Cancel is a request the dispatcher acts on.
        let (st, v) = call(
            "DELETE",
            port,
            &format!("/v2/submissions/{id}"),
            Some("tok"),
            None,
        );
        assert_eq!(
            (st, v["cancel_requested"].clone()),
            (202, serde_json::json!(true))
        );
        assert!(core.outbox.store.cancel_requested(&id));
        assert_eq!(
            call(
                "DELETE",
                port,
                "/v2/submissions/j_000000000000",
                Some("tok"),
                None
            )
            .0,
            404
        );

        // A job that is over cannot be cancelled.
        let mut j = core.outbox.store.load(&id).unwrap();
        j.state = super::State::Done;
        core.outbox.store.save(&j).unwrap();
        assert_eq!(
            call(
                "DELETE",
                port,
                &format!("/v2/submissions/{id}"),
                Some("tok"),
                None
            )
            .0,
            409
        );
    }

    #[test]
    fn a_repeated_request_id_returns_the_job_already_made() {
        let core = core_with(0);
        let port = serve(core.clone(), "tok");
        let inputs = test_dir("routes-rid");
        let mut s = sample_spec(&inputs);
        s.request_id = Some("once".into());
        let spec = serde_json::to_value(&s).unwrap();
        let (_, a) = call("POST", port, "/v2/submissions", Some("tok"), Some(&spec));
        let (_, b) = call("POST", port, "/v2/submissions", Some("tok"), Some(&spec));
        assert_eq!(a["id"], b["id"]);
        assert_eq!(
            (a["duplicate"].clone(), b["duplicate"].clone()),
            (serde_json::json!(false), serde_json::json!(true))
        );
        assert_eq!(core.outbox.store.list().len(), 1);
    }

    #[test]
    fn the_cli_submits_lists_and_cancels_through_the_agent() {
        use crate::agent::client::AgentClient;
        use crate::agent::files::{save_json, AgentFile, AGENT_FILE};
        use crate::agent::outbox::cli::{execute, Cmd};
        let core = core_with(0);
        let port = serve(core.clone(), "tok");
        let dir = test_dir("routes-cli");
        save_json(
            &dir.join(AGENT_FILE),
            &AgentFile {
                v: 1,
                port,
                token: "tok".into(),
                pid: 1,
                version: "t".into(),
                rev: 0,
            },
        )
        .unwrap();
        let client = AgentClient::from_dir(&dir).unwrap();
        // A spec file with a RELATIVE input path, resolved against the spec's directory.
        let specs = test_dir("routes-cli-spec");
        std::fs::create_dir_all(specs.join("data")).unwrap();
        std::fs::write(specs.join("data/p.txt"), "hello").unwrap();
        let file = specs.join("job.toml");
        std::fs::write(
            &file,
            "name='cli'\nimage='r.io/a:1'\nmax_price_per_batch=7\n[resources]\ncpus=1\nmemory_gb=1\nmax_runtime_s=60\n[[batch]]\ninputs={ 'p.txt' = 'data/p.txt' }\n",
        )
        .unwrap();
        let sub = Cmd::Submit {
            spec: file.display().to_string(),
        };
        assert_eq!(execute(&client, &dir, sub, true), 0);
        let jobs = core.outbox.store.list();
        assert_eq!(jobs.len(), 1);
        assert!(
            jobs[0].files[0].path.ends_with("data/p.txt") && jobs[0].files[0].path.starts_with('/')
        );
        let id = jobs[0].id.clone();
        assert_eq!(execute(&client, &dir, Cmd::Ls, false), 0);
        assert_eq!(
            execute(&client, &dir, Cmd::Status { id: id.clone() }, false),
            0
        );
        assert_eq!(
            execute(
                &client,
                &dir,
                Cmd::Status {
                    id: "j_000000000000".into()
                },
                false
            ),
            1
        );
        assert_eq!(
            execute(
                &client,
                &dir,
                Cmd::Results {
                    id: id.clone(),
                    out: None
                },
                false
            ),
            1,
            "no results yet"
        );
        assert_eq!(
            execute(&client, &dir, Cmd::Cancel { id: id.clone() }, false),
            0
        );
        assert!(core.outbox.store.cancel_requested(&id));
        // A bad spec file is refused by the agent with the field named.
        std::fs::write(
            &file,
            std::fs::read_to_string(&file).unwrap().replace("=7", "=0"),
        )
        .unwrap();
        assert_eq!(
            execute(
                &client,
                &dir,
                Cmd::Submit {
                    spec: file.display().to_string()
                },
                false
            ),
            1
        );
    }
}