akribes-sdk 0.22.6

Rust client SDK for the Akribes workflow server
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
//! Wire-level decoder tests for the SDK's `Runtime*` event arms.
//!
//! These tests feed hand-rolled JSON envelopes (the same shape the engine
//! emits over SSE) through [`WorkflowEvent::from_envelope_json`] and assert
//! that the SDK produces the expected typed `WorkflowEvent::Runtime*` arms in
//! order. They run without spinning up a server — the SSE byte-parsing path
//! is exercised by the mockito-backed tests in `lib.rs` already, so here we
//! focus on the typed-arm decode contract for the five new variants.

use akribes_sdk::{EventCategory, RuntimeEvent, WorkflowEvent, runtime::RuntimeErrorKind};
use serde_json::json;

// ── Per-variant envelope decoders ────────────────────────────────────────────

#[test]
fn runtime_start_envelope_decodes_to_typed_arm() {
    let envelope = json!({
        "type": "RuntimeStart",
        "payload": {
            "task_name": "analyse_data",
            "runtime_name": "run_python",
            "language": "python",
        },
    });
    let evt = WorkflowEvent::from_envelope_json(envelope).expect("decode");
    match evt {
        WorkflowEvent::RuntimeStart {
            task_name,
            runtime_name,
            language,
        } => {
            assert_eq!(task_name, "analyse_data");
            assert_eq!(runtime_name, "run_python");
            assert_eq!(language, "python");
        }
        other => panic!("expected RuntimeStart, got {other:?}"),
    }
}

#[test]
fn runtime_stdout_envelope_decodes_to_typed_arm() {
    let envelope = json!({
        "type": "RuntimeStdout",
        "payload": {"task_name": "analyse_data", "chunk": "hello, world\n"},
    });
    let evt = WorkflowEvent::from_envelope_json(envelope).expect("decode");
    match evt {
        WorkflowEvent::RuntimeStdout { task_name, chunk } => {
            assert_eq!(task_name, "analyse_data");
            assert_eq!(chunk, "hello, world\n");
        }
        other => panic!("expected RuntimeStdout, got {other:?}"),
    }
}

#[test]
fn runtime_stderr_envelope_decodes_to_typed_arm() {
    let envelope = json!({
        "type": "RuntimeStderr",
        "payload": {"task_name": "t", "chunk": "DeprecationWarning: foo\n"},
    });
    let evt = WorkflowEvent::from_envelope_json(envelope).expect("decode");
    match evt {
        WorkflowEvent::RuntimeStderr { task_name, chunk } => {
            assert_eq!(task_name, "t");
            assert_eq!(chunk, "DeprecationWarning: foo\n");
        }
        other => panic!("expected RuntimeStderr, got {other:?}"),
    }
}

#[test]
fn runtime_end_envelope_decodes_to_typed_arm() {
    let envelope = json!({
        "type": "RuntimeEnd",
        "payload": {"task_name": "t", "exit_code": 0, "duration_ms": 1234},
    });
    let evt = WorkflowEvent::from_envelope_json(envelope).expect("decode");
    match evt {
        WorkflowEvent::RuntimeEnd {
            task_name,
            exit_code,
            duration_ms,
        } => {
            assert_eq!(task_name, "t");
            assert_eq!(exit_code, 0);
            assert_eq!(duration_ms, 1234);
        }
        other => panic!("expected RuntimeEnd, got {other:?}"),
    }
}

#[test]
fn runtime_end_envelope_preserves_negative_exit_code() {
    // Containerised executors may report signals as negative exit codes
    // (e.g. -9 for SIGKILL on Linux). `exit_code` is `i32` so the wire
    // shape supports it without lossy conversion.
    let envelope = json!({
        "type": "RuntimeEnd",
        "payload": {"task_name": "t", "exit_code": -9, "duration_ms": 50},
    });
    let evt = WorkflowEvent::from_envelope_json(envelope).expect("decode");
    if let WorkflowEvent::RuntimeEnd { exit_code, .. } = evt {
        assert_eq!(exit_code, -9);
    } else {
        panic!("expected RuntimeEnd");
    }
}

#[test]
fn runtime_error_envelope_decodes_to_typed_arm() {
    let envelope = json!({
        "type": "RuntimeError",
        "payload": {
            "task_name": "t",
            "kind": "Timeout",
            "message": "execution exceeded 30s timeout",
        },
    });
    let evt = WorkflowEvent::from_envelope_json(envelope).expect("decode");
    match evt {
        WorkflowEvent::RuntimeError {
            task_name,
            kind,
            message,
        } => {
            assert_eq!(task_name, "t");
            assert_eq!(kind, "Timeout");
            assert_eq!(message, "execution exceeded 30s timeout");
            // The wire `kind` string maps cleanly to the SDK's typed enum.
            assert_eq!(
                RuntimeErrorKind::from_wire(&kind),
                RuntimeErrorKind::Timeout
            );
        }
        other => panic!("expected RuntimeError, got {other:?}"),
    }
}

#[test]
fn cancelled_runtime_error_envelope_round_trips() {
    // `Cancelled` is the engine's terminal-user-stop tag. It distinguishes
    // a user cancel from a sandbox timeout or an opaque `Internal`, so a
    // retry-policy driver can correctly skip retries.
    let envelope = json!({
        "type": "RuntimeError",
        "payload": {
            "task_name": "t",
            "kind": "Cancelled",
            "message": "runtime call cancelled before completion",
        },
    });
    let evt = WorkflowEvent::from_envelope_json(envelope.clone()).expect("decode");
    match &evt {
        WorkflowEvent::RuntimeError { kind, .. } => {
            assert_eq!(kind, "Cancelled");
            assert_eq!(
                RuntimeErrorKind::from_wire(kind),
                RuntimeErrorKind::Cancelled
            );
        }
        other => panic!("expected RuntimeError, got {other:?}"),
    }
    // And the underlying `RuntimeEvent` round-trips through serde so a
    // downstream proxy can decode → re-encode without lossy stringification.
    let payload = envelope["payload"].clone();
    let typed = RuntimeEvent::RuntimeError(akribes_sdk::RuntimeErrorPayload {
        task_name: payload["task_name"].as_str().unwrap().into(),
        kind: payload["kind"].as_str().unwrap().into(),
        message: payload["message"].as_str().unwrap().into(),
    });
    let re_encoded = serde_json::to_value(&typed).unwrap();
    assert_eq!(re_encoded, envelope);
}

// ── Category routing ────────────────────────────────────────────────────────

#[test]
fn runtime_start_and_end_route_to_progress_category() {
    let start = WorkflowEvent::from_envelope_json(json!({
        "type": "RuntimeStart",
        "payload": {"task_name": "t", "runtime_name": "r", "language": "python"},
    }))
    .unwrap();
    assert_eq!(start.category(), EventCategory::Progress);

    let end = WorkflowEvent::from_envelope_json(json!({
        "type": "RuntimeEnd",
        "payload": {"task_name": "t", "exit_code": 0, "duration_ms": 0},
    }))
    .unwrap();
    assert_eq!(end.category(), EventCategory::Progress);
}

#[test]
fn runtime_stdout_and_stderr_route_to_output_category() {
    let stdout = WorkflowEvent::from_envelope_json(json!({
        "type": "RuntimeStdout",
        "payload": {"task_name": "t", "chunk": "x"},
    }))
    .unwrap();
    assert_eq!(stdout.category(), EventCategory::Output);

    let stderr = WorkflowEvent::from_envelope_json(json!({
        "type": "RuntimeStderr",
        "payload": {"task_name": "t", "chunk": "x"},
    }))
    .unwrap();
    assert_eq!(stderr.category(), EventCategory::Output);
}

#[test]
fn runtime_error_routes_to_error_category() {
    let err = WorkflowEvent::from_envelope_json(json!({
        "type": "RuntimeError",
        "payload": {"task_name": "t", "kind": "OomKilled", "message": ""},
    }))
    .unwrap();
    assert_eq!(err.category(), EventCategory::Error);
}

// ── Envelope decoder fallback ───────────────────────────────────────────────

#[test]
fn non_runtime_envelope_falls_through_to_engine_decoder() {
    // A regular `TaskStart` envelope must still decode — the runtime
    // decoder only short-circuits on the five Runtime* tags; everything
    // else flows through `EngineEvent` and the existing `From<EngineEvent>`
    // projection.
    let envelope = json!({
        "type": "TaskStart",
        "payload": ["summarise", null],
    });
    let evt = WorkflowEvent::from_envelope_json(envelope).expect("decode");
    match evt {
        WorkflowEvent::TaskStart { task, on_error } => {
            assert_eq!(task, "summarise");
            assert!(on_error.is_none());
        }
        other => panic!("expected TaskStart, got {other:?}"),
    }
}

#[test]
fn invalid_runtime_payload_surfaces_runtime_decode_error() {
    // `"type"` is a known runtime tag but `payload` is missing required
    // fields. The decoder must NOT fall through to `EngineEvent` — that
    // would silently bury the wire-shape bug. Surface as Runtime variant.
    use akribes_sdk::EnvelopeDecodeError;
    let envelope = json!({
        "type": "RuntimeStart",
        "payload": {"task_name": "t"},  // missing runtime_name + language
    });
    match WorkflowEvent::from_envelope_json(envelope) {
        Err(EnvelopeDecodeError::Runtime(_)) => {}
        other => panic!("expected Runtime decode error, got {other:?}"),
    }
}

// ── End-to-end mixed stream ─────────────────────────────────────────────────
//
// Feeds a realistic sequence of SSE-like envelopes through the decoder and
// asserts they produce the expected typed variants in order. Mirrors what a
// consumer would do reading a real `runtime` task's event stream.

#[test]
fn mixed_stream_decodes_in_order() {
    let envelopes = [
        json!({"type": "WorkflowStart", "payload": 1}),
        json!({"type": "TaskStart", "payload": ["analyse", null]}),
        json!({
            "type": "RuntimeStart",
            "payload": {
                "task_name": "analyse",
                "runtime_name": "run_python",
                "language": "python",
            },
        }),
        json!({
            "type": "RuntimeStdout",
            "payload": {"task_name": "analyse", "chunk": "hello "},
        }),
        json!({
            "type": "RuntimeStdout",
            "payload": {"task_name": "analyse", "chunk": "world\n"},
        }),
        json!({
            "type": "RuntimeStderr",
            "payload": {"task_name": "analyse", "chunk": "DeprecationWarning\n"},
        }),
        json!({
            "type": "RuntimeEnd",
            "payload": {"task_name": "analyse", "exit_code": 0, "duration_ms": 42},
        }),
        // TaskEnd uses Value's tagged serialization for `value` — see
        // `lib.rs::mock_events_json` for the existing convention.
        json!({
            "type": "TaskEnd",
            "payload": {
                "task": "analyse",
                "on_error_label": null,
                "value": "Null",
                "value_type": null,
                "duration": {"secs": 0, "nanos": 42_000_000},
                "attempt": 1,
                "usage": null,
            },
        }),
        json!({"type": "WorkflowEnd", "payload": "Null"}),
    ];

    let decoded: Vec<WorkflowEvent> = envelopes
        .iter()
        .cloned()
        .map(|e| WorkflowEvent::from_envelope_json(e).expect("decode"))
        .collect();

    // Tag-only assertion list — keeps the test readable while still
    // proving the ordering and variant identity.
    let tags: Vec<&'static str> = decoded
        .iter()
        .map(|e| match e {
            WorkflowEvent::Start { .. } => "Start",
            WorkflowEvent::TaskStart { .. } => "TaskStart",
            WorkflowEvent::RuntimeStart { .. } => "RuntimeStart",
            WorkflowEvent::RuntimeStdout { .. } => "RuntimeStdout",
            WorkflowEvent::RuntimeStderr { .. } => "RuntimeStderr",
            WorkflowEvent::RuntimeEnd { .. } => "RuntimeEnd",
            WorkflowEvent::TaskEnd { .. } => "TaskEnd",
            WorkflowEvent::End { .. } => "End",
            other => panic!("unexpected variant in mixed stream: {other:?}"),
        })
        .collect();
    assert_eq!(
        tags,
        vec![
            "Start",
            "TaskStart",
            "RuntimeStart",
            "RuntimeStdout",
            "RuntimeStdout",
            "RuntimeStderr",
            "RuntimeEnd",
            "TaskEnd",
            "End",
        ]
    );

    // The two consecutive stdout chunks should preserve the bytes intact —
    // a consumer concatenating in order recovers the original output.
    let stdout_chunks: Vec<&str> = decoded
        .iter()
        .filter_map(|e| match e {
            WorkflowEvent::RuntimeStdout { chunk, .. } => Some(chunk.as_str()),
            _ => None,
        })
        .collect();
    assert_eq!(stdout_chunks, vec!["hello ", "world\n"]);

    // RuntimeEnd carries the exit_code + duration as typed fields, not as
    // a serde_json::Value bag.
    let runtime_end = decoded.iter().find_map(|e| match e {
        WorkflowEvent::RuntimeEnd {
            task_name,
            exit_code,
            duration_ms,
        } => Some((task_name.as_str(), *exit_code, *duration_ms)),
        _ => None,
    });
    assert_eq!(runtime_end, Some(("analyse", 0, 42)));
}

// ── Direct RuntimeEvent roundtrip ───────────────────────────────────────────
//
// Locks the wire shape: serialising a `RuntimeEvent` produces exactly the
// `{type, payload}` envelope shape, byte-identically to what the engine
// emits. Catches accidental field renames at SDK build time.

#[test]
fn runtime_event_serializes_to_canonical_envelope() {
    let cases = [
        (
            RuntimeEvent::RuntimeStart(akribes_sdk::RuntimeStartPayload {
                task_name: "t".into(),
                runtime_name: "r".into(),
                language: "python".into(),
            }),
            json!({
                "type": "RuntimeStart",
                "payload": {"task_name": "t", "runtime_name": "r", "language": "python"},
            }),
        ),
        (
            RuntimeEvent::RuntimeStdout(akribes_sdk::RuntimeStdoutPayload {
                task_name: "t".into(),
                chunk: "x".into(),
            }),
            json!({
                "type": "RuntimeStdout",
                "payload": {"task_name": "t", "chunk": "x"},
            }),
        ),
        (
            RuntimeEvent::RuntimeStderr(akribes_sdk::RuntimeStderrPayload {
                task_name: "t".into(),
                chunk: "x".into(),
            }),
            json!({
                "type": "RuntimeStderr",
                "payload": {"task_name": "t", "chunk": "x"},
            }),
        ),
        (
            RuntimeEvent::RuntimeEnd(akribes_sdk::RuntimeEndPayload {
                task_name: "t".into(),
                exit_code: 1,
                duration_ms: 99,
            }),
            json!({
                "type": "RuntimeEnd",
                "payload": {"task_name": "t", "exit_code": 1, "duration_ms": 99},
            }),
        ),
        (
            RuntimeEvent::RuntimeError(akribes_sdk::RuntimeErrorPayload {
                task_name: "t".into(),
                kind: "OomKilled".into(),
                message: "container exceeded 512MB".into(),
            }),
            json!({
                "type": "RuntimeError",
                "payload": {
                    "task_name": "t",
                    "kind": "OomKilled",
                    "message": "container exceeded 512MB",
                },
            }),
        ),
    ];
    for (evt, expected) in cases {
        assert_eq!(serde_json::to_value(&evt).unwrap(), expected);
        let back: RuntimeEvent = serde_json::from_value(expected).unwrap();
        assert_eq!(back, evt);
    }
}