unitype-runtime 0.2.0

Implementation registry, pluggable executors, and policy dispatch for unitype
Documentation
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
//! Reaching an environment by starting a program.
//!
//! The simplest way to let another language prove something: run it, hand it
//! one JSON message, and read one back. No linking, no bindings, no foreign
//! function interface — a program that can read standard input and write
//! standard output can participate, which is every language worth naming.
//!
//! # The exchange
//!
//! One request in, one response out, and the process exits:
//!
//! ```text
//! → {"unitype":"1","kind":"invoke","signature":"demo::sort",
//!    "entry":"bubble_sort","args":[[3,1,2]]}
//! ← {"ok":true,"value":[1,2,3]}
//! ```
//!
//! Each message is one line, and end of input ends the stream, so a child that
//! reads lines and a child that reads to the end are both correct here. Nothing
//! has to agree on a length prefix, and a child written in ten lines of Python
//! works by construction.
//!
//! Starting a process per call is real overhead — around twenty milliseconds
//! for Python, against microseconds of work — which is why the default policy
//! prefers to stay in this one. A program calling repeatedly should ask for a
//! worker instead; see [`crate::worker`]. This is the version that cannot get
//! stuck holding state it should not have, which is why it stays the default.

use std::process::Stdio;
use std::time::Duration;

use serde::{Deserialize, Serialize};
use tokio::io::AsyncWriteExt;
use tokio::process::Command;
use unitype_core::check::CheckedWorkspace;
use unitype_core::ir::Type;
use unitype_core::value::Value;

use crate::environment::{Environment, Reach};
use crate::error::ExecError;
use crate::implementation::Implementation;

/// The wire protocol version this build speaks.
pub const PROTOCOL_VERSION: &str = "1";

/// How much of what a failing child said is worth keeping.
const STDERR_KEPT: usize = 8 * 1024;

/// What the parent sends.
#[derive(Debug, Serialize, Deserialize)]
pub struct Request {
    /// The protocol version.
    pub unitype: String,
    /// What is being asked for. Only `invoke` for now, but a child that checks
    /// it will keep working when there is more.
    pub kind: String,
    /// The proposition being proved, so a child can log something useful.
    pub signature: String,
    /// Which of the child's functions to call.
    pub entry: String,
    /// The arguments, in declaration order.
    pub args: Vec<serde_json::Value>,
}

/// What the child sends back.
#[derive(Debug, Serialize, Deserialize)]
pub struct Response {
    /// Whether it worked.
    pub ok: bool,
    /// What it produced.
    ///
    /// `null` is an answer — a proposition returning `T?` answering nothing —
    /// and is kept apart from the member being left out, which is no answer
    /// at all.
    #[serde(default, deserialize_with = "present_value")]
    pub value: Option<serde_json::Value>,
    /// What went wrong.
    #[serde(default)]
    pub error: Option<ResponseError>,
}

/// Keeps a `value` that is written as `null`, which serde would otherwise read
/// as the member being absent.
fn present_value<'de, D: serde::Deserializer<'de>>(
    d: D,
) -> Result<Option<serde_json::Value>, D::Error> {
    serde_json::Value::deserialize(d).map(Some)
}

/// A failure the child reported for itself.
#[derive(Debug, Serialize, Deserialize)]
pub struct ResponseError {
    /// A short machine-readable tag.
    #[serde(default)]
    pub code: String,
    /// What happened, for a person.
    pub message: String,
}

/// Runs an implementation by starting a program and exchanging one message.
///
/// `params` is the type each argument is written against, in the same order.
pub async fn invoke(
    ws: &CheckedWorkspace,
    environment: &Environment,
    implementation: &Implementation,
    args: Vec<Value>,
    params: &[Type],
    expected: &Type,
) -> Result<Value, ExecError> {
    let Reach::Subprocess {
        command,
        args: argv,
        cwd,
        env,
        timeout_ms,
        ..
    } = &environment.reach
    else {
        return Err(ExecError::Unreachable {
            environment: environment.id.to_string(),
            detail: "this is not a subprocess environment".into(),
        });
    };

    // A trailing newline costs nothing here — JSON ignores it — and means the
    // same bytes work for a child that reads lines, which is what a worker
    // needs. One message, framed two ways at once.
    let mut body = request_line(ws, implementation, &args, params)?;
    body.push(b'\n');

    let mut process = Command::new(command);
    process
        .args(argv)
        .envs(env)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        // If this call is abandoned — a timeout, a cancelled caller — the child
        // goes with it rather than being left behind.
        .kill_on_drop(true);
    if let Some(dir) = cwd {
        process.current_dir(dir);
    }

    let mut child = crate::spawn::spawn(&mut process).map_err(|source| ExecError::Unreachable {
        environment: environment.id.to_string(),
        detail: format!("could not start `{command}`: {source}"),
    })?;

    let mut stdin = child.stdin.take().expect("stdin was piped");
    stdin
        .write_all(&body)
        .await
        .map_err(|source| ExecError::Protocol {
            id: implementation.id.to_string(),
            detail: format!("could not send the request: {source}"),
        })?;
    // Closing this is the end of the message. Without it the child waits.
    drop(stdin);

    let output = tokio::time::timeout(Duration::from_millis(*timeout_ms), child.wait_with_output())
        .await
        .map_err(|_| ExecError::Timeout {
            id: implementation.id.to_string(),
            millis: *timeout_ms,
        })?
        .map_err(|source| ExecError::Unreachable {
            environment: environment.id.to_string(),
            detail: format!("could not read from `{command}`: {source}"),
        })?;

    if !output.status.success() {
        return Err(ExecError::Failed {
            id: implementation.id.to_string(),
            message: format!(
                "exited with {}{}",
                output
                    .status
                    .code()
                    .map(|c| c.to_string())
                    .unwrap_or_else(|| "a signal".into()),
                tail(&output.stderr),
            ),
        });
    }

    let text = String::from_utf8_lossy(&output.stdout);
    read_answer_with(ws, implementation, &text, expected, &tail(&output.stderr))
}

/// The bytes of one request, ready to send.
///
/// Shared by every way of reaching a child, so a worker, a one-shot process
/// and a sandbox are sent exactly the same thing. Each argument is written
/// against its parameter's type, in the typed form: a tuple arrives as the
/// array the child's own code expects, not as an escape it would have to
/// know about. An argument with no type given is written as it would be with
/// no type at all.
pub(crate) fn request_line(
    ws: &CheckedWorkspace,
    implementation: &Implementation,
    args: &[Value],
    params: &[Type],
) -> Result<Vec<u8>, ExecError> {
    let encoded: Vec<serde_json::Value> = args
        .iter()
        .enumerate()
        .map(|(i, a)| match params.get(i) {
            Some(ty) => ws.typed_json(a, ty),
            None => a.to_json(),
        })
        .collect::<Result<_, _>>()
        .map_err(|e| ExecError::Protocol {
            id: implementation.id.to_string(),
            detail: format!("an argument could not be encoded: {e}"),
        })?;

    let request = Request {
        unitype: PROTOCOL_VERSION.to_string(),
        kind: "invoke".to_string(),
        signature: implementation.proves.to_string(),
        entry: implementation.entry.clone(),
        args: encoded,
    };
    Ok(serde_json::to_vec(&request).expect("a request serializes"))
}

/// Reads what a child sent back, against the type its signature promises.
pub(crate) fn read_answer(
    ws: &CheckedWorkspace,
    implementation: &Implementation,
    text: &str,
    expected: &Type,
) -> Result<Value, ExecError> {
    read_answer_with(ws, implementation, text, expected, "")
}

/// The same, with whatever the child said on its error stream to add if the
/// response turns out to be unreadable — which is usually where the reason is.
fn read_answer_with(
    ws: &CheckedWorkspace,
    implementation: &Implementation,
    text: &str,
    expected: &Type,
    context: &str,
) -> Result<Value, ExecError> {
    let response: Response = serde_json::from_str(text).map_err(|source| ExecError::Protocol {
        id: implementation.id.to_string(),
        detail: format!("could not read the response: {source}{context}"),
    })?;

    if !response.ok {
        let reported = response
            .error
            .map(|e| e.message)
            .unwrap_or_else(|| "it said it failed but did not say why".to_string());
        return Err(ExecError::Failed {
            id: implementation.id.to_string(),
            message: reported,
        });
    }

    let Some(json) = response.value else {
        return Err(ExecError::Protocol {
            id: implementation.id.to_string(),
            detail: "it said it worked but sent nothing back".into(),
        });
    };

    // Read against the type the signature promises, so that what comes back is
    // interpreted the same way as anything else entering the system.
    ws.read(&json, expected)
        .map_err(|source| ExecError::Protocol {
            id: implementation.id.to_string(),
            detail: format!(
                "what it sent back is not a `{}`: {source}",
                describe(expected)
            ),
        })
}

fn describe(ty: &Type) -> String {
    unitype_core::canon::canonical_string(ty)
}

/// The end of what a failing child said, which is where the reason usually is.
fn tail(stderr: &[u8]) -> String {
    let text = String::from_utf8_lossy(stderr);
    let trimmed = text.trim();
    if trimmed.is_empty() {
        return String::new();
    }
    let kept = if trimmed.len() > STDERR_KEPT {
        &trimmed[trimmed.len() - STDERR_KEPT..]
    } else {
        trimmed
    };
    format!("; it said: {kept}")
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::BTreeMap;
    use unitype_core::check::check_workspace;
    use unitype_core::{BigO, Ident, ImplProfile, Workspace};

    fn ws() -> CheckedWorkspace {
        check_workspace(
            Workspace::from_json(
                r#"{"universes":[{"name":"demo","functions":[
                    {"name":"sort","params":[{"name":"xs","ty":"[i64]"}],"ret":"[i64]"}]}]}"#,
            )
            .expect("reads"),
        )
        .expect("checks")
    }

    /// An environment that runs a shell snippet, so these tests need nothing
    /// installed beyond a shell.
    fn shell(script: &str, timeout_ms: u64) -> Environment {
        Environment {
            id: Ident::new("shell").unwrap(),
            language: Ident::new("sh").unwrap(),
            doc: None,
            reach: Reach::Subprocess {
                command: "/bin/sh".into(),
                args: vec!["-c".into(), script.into()],
                cwd: None,
                env: BTreeMap::new(),
                timeout_ms,
                persistent: false,
            },
            meta: unitype_core::Metadata::default(),
        }
    }

    fn implementation() -> Implementation {
        Implementation::new(
            "sort.shell",
            "demo::sort".parse().unwrap(),
            Ident::new("shell").unwrap(),
            "bubble_sort",
            ImplProfile::new(BigO::ON2, BigO::O1),
        )
    }

    #[test]
    fn an_absent_answer_is_an_answer_and_no_answer_is_not() {
        let ws = ws();
        let maybe = Type::I64.optional();
        let absent = read_answer(
            &ws,
            &implementation(),
            r#"{"ok":true,"value":null}"#,
            &maybe,
        )
        .expect("null is what a proposition returning `T?` answers");
        assert_eq!(absent, Value::Null);

        let none = read_answer(&ws, &implementation(), r#"{"ok":true}"#, &maybe)
            .expect_err("no value at all is a broken exchange");
        assert!(none.to_string().contains("sent nothing back"), "{none}");
    }

    /// Generous on purpose: these tests are about the exchange, not about how
    /// busy the machine is. Only the test that is *about* giving up sets a
    /// short limit.
    const PATIENT: u64 = 30_000;

    async fn run(script: &str) -> Result<Value, ExecError> {
        run_with(script, PATIENT).await
    }

    async fn run_with(script: &str, timeout_ms: u64) -> Result<Value, ExecError> {
        let ws = ws();
        invoke(
            &ws,
            &shell(script, timeout_ms),
            &implementation(),
            vec![Value::List(vec![Value::Int(3), Value::Int(1)])],
            &[Type::I64.list()],
            &Type::I64.list(),
        )
        .await
    }

    /// Short calls started alongside a child that sticks around.
    ///
    /// This is the arrangement that used to hang. Starting a process means
    /// making pipes and then forking, and where those cannot be one atomic
    /// step, a fork landing between them inherits a pipe it has no business
    /// holding. The parent closes its own end to say the request is over, and
    /// the child keeps waiting, because a stranger is still holding the other
    /// copy open.
    ///
    /// Both halves matter, which is why the test has both. `cat` is the
    /// victim: a child reading to end of input hangs where one reading a line
    /// would not notice. The sleeper is the thief, and has to outlive the
    /// patience of the calls it is stealing from — with only short-lived
    /// children around, an inherited descriptor is let go again within
    /// milliseconds and nobody waits long enough to care.
    ///
    /// It is a race, so a green run is weak evidence on its own. The value is
    /// in running it forever after.
    #[tokio::test(flavor = "multi_thread", worker_threads = 8)]
    async fn a_long_lived_child_does_not_hold_another_call_s_input_open() {
        // Long enough to outlast the patience below, so a stolen descriptor
        // shows up as a call that never finishes rather than one that is
        // merely slow.
        let thief = tokio::spawn(run_with(
            r#"cat > /dev/null; sleep 2; echo '{"ok":true,"value":[]}'"#,
            30_000,
        ));

        let victims = (0..48).map(|n| async move {
            run_with(
                &format!(r#"cat > /dev/null; echo '{{"ok":true,"value":[{n}]}}'"#),
                900,
            )
            .await
        });

        for (n, got) in futures_join(victims).await.into_iter().enumerate() {
            assert_eq!(
                got.unwrap_or_else(|e| panic!("call {n} should have answered: {e}")),
                Value::List(vec![Value::Int(n as i64)]),
                "each call got its own answer, not somebody else's"
            );
        }
        thief.await.expect("it did not panic").expect("it answers");
    }

    /// Runs everything given to it at once and collects the results in order.
    ///
    /// Hand-rolled because this crate does not depend on `futures`, and one
    /// join is not worth one.
    async fn futures_join<F>(futures: impl IntoIterator<Item = F>) -> Vec<F::Output>
    where
        F: std::future::Future + Send + 'static,
        F::Output: Send + 'static,
    {
        let handles: Vec<_> = futures.into_iter().map(tokio::spawn).collect();
        let mut out = Vec::with_capacity(handles.len());
        for handle in handles {
            out.push(handle.await.expect("the call did not panic"));
        }
        out
    }

    #[tokio::test]
    async fn a_program_that_answers_is_believed() {
        let out = run(r#"cat > /dev/null; echo '{"ok":true,"value":[1,3]}'"#)
            .await
            .expect("answers");
        assert_eq!(out, Value::List(vec![Value::Int(1), Value::Int(3)]));
    }

    #[tokio::test]
    async fn the_request_reaches_the_program_intact() {
        // The child echoes back what it was sent, as a string, and we check the
        // parts that matter are in it.
        let ws = ws();
        let out = invoke(
            &ws,
            &shell(
                r#"body=$(cat); printf '{"ok":true,"value":%s}' "$(printf '%s' "$body" | sed 's/\\/\\\\/g; s/"/\\"/g; s/^/"/; s/$/"/')""#,
                PATIENT,
            ),
            &implementation(),
            vec![Value::List(vec![Value::Int(3), Value::Int(1)])],
            &[Type::I64.list()],
            &Type::String,
        )
        .await
        .expect("answers");

        let Value::Str(seen) = out else {
            panic!("expected the request back as a string, got {out:?}");
        };
        assert!(seen.contains(r#""unitype":"1""#), "{seen}");
        assert!(seen.contains(r#""signature":"demo::sort""#), "{seen}");
        assert!(seen.contains(r#""entry":"bubble_sort""#), "{seen}");
        assert!(
            seen.contains("[3,1]"),
            "the arguments should be there: {seen}"
        );
    }

    #[test]
    fn an_argument_is_sent_written_against_its_type() {
        // A child reads JSON the way its own language does, and every language
        // reads a pair as an array. Without the type, the pair would have to
        // say it is one.
        let ws = ws();
        let pair = Value::Tuple(vec![Value::Int(1), Value::Str("a".into())]);
        let pair_type = Type::Tuple {
            items: vec![Type::I64, Type::String],
        };
        let line = request_line(
            &ws,
            &implementation(),
            std::slice::from_ref(&pair),
            &[pair_type],
        )
        .expect("encodes");
        let sent: serde_json::Value = serde_json::from_slice(&line).expect("JSON");
        assert_eq!(sent["args"], serde_json::json!([[1, "a"]]));

        let untyped = request_line(&ws, &implementation(), &[pair], &[]).expect("encodes");
        let sent: serde_json::Value = serde_json::from_slice(&untyped).expect("JSON");
        assert_eq!(sent["args"], serde_json::json!([{"$tuple": [1, "a"]}]));
    }

    #[tokio::test]
    async fn a_program_that_is_not_there_is_an_environment_problem() {
        let ws = ws();
        let err = invoke(
            &ws,
            &Environment::subprocess("nope", "sh", "unitype_no_such_program", &[]),
            &implementation(),
            vec![Value::List(vec![])],
            &[Type::I64.list()],
            &Type::I64.list(),
        )
        .await
        .unwrap_err();

        assert!(
            err.is_environmental(),
            "nothing is wrong with the proof; the machine is missing it: {err}"
        );
    }

    #[tokio::test]
    async fn a_program_reporting_its_own_failure_is_quoted() {
        let err = run(r#"cat > /dev/null; echo '{"ok":false,"error":{"code":"bad","message":"list was too long"}}'"#)
            .await
            .unwrap_err();
        assert!(err.to_string().contains("list was too long"), "{err}");
        assert!(
            !err.is_environmental(),
            "the program worked; what it was asked to do did not"
        );
    }

    #[tokio::test]
    async fn a_program_that_crashes_has_its_last_words_kept() {
        let err = run(r#"cat > /dev/null; echo "something went badly" >&2; exit 3"#)
            .await
            .unwrap_err();
        let message = err.to_string();
        assert!(message.contains("exited with 3"), "{message}");
        assert!(message.contains("something went badly"), "{message}");
    }

    #[tokio::test]
    async fn a_program_that_talks_nonsense_is_a_protocol_failure() {
        let err = run(r#"cat > /dev/null; echo 'not json at all'"#)
            .await
            .unwrap_err();
        assert!(matches!(err, ExecError::Protocol { .. }), "{err}");
        assert!(
            !err.is_environmental(),
            "a program that cannot speak the protocol will not learn by being asked again"
        );
    }

    #[tokio::test]
    async fn a_program_that_answers_with_the_wrong_shape_is_caught_here_too() {
        let err = run(r#"cat > /dev/null; echo '{"ok":true,"value":"not a list"}'"#)
            .await
            .unwrap_err();
        assert!(err.to_string().contains("is not a"), "{err}");
    }

    #[tokio::test]
    async fn a_program_that_never_answers_is_given_up_on() {
        let err = run_with(r#"cat > /dev/null; sleep 30"#, 150)
            .await
            .unwrap_err();
        assert!(matches!(err, ExecError::Timeout { .. }), "{err}");
        assert!(
            err.is_environmental(),
            "a hung machine is worth trying around"
        );
    }

    #[tokio::test]
    async fn closing_the_input_is_what_tells_the_program_to_get_on_with_it() {
        // This child reads to the end before answering. If the parent did not
        // close its writing end, it would wait for ever.
        let out = run(r#"cat > /dev/null && echo '{"ok":true,"value":[7]}'"#)
            .await
            .expect("answers");
        assert_eq!(out, Value::List(vec![Value::Int(7)]));
    }
}