aion-server 0.30.0

Aion workflow server library: HTTP, gRPC, WebSocket, and worker endpoints. Run it with the `aion` binary from the aion-cli crate.
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
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
//! `POST /workflows/describe-live` end to end.
//!
//! The join is only worth having if each source is really consulted, so every
//! test drives ONE source into a specific state and asserts the response
//! changed with it — paired, wherever the answer is a verdict, with the control
//! that produces the opposite answer from the same request.

use crate::worker::registry::RegistrationOptions;
use std::sync::Arc;
use std::time::{Duration, Instant};

use aion_core::{
    ActivityEvent, ActivityEventKind, ActivityId, Event, EventEnvelope, InterventionCapabilities,
    InterventionPrimitive, MessageRole, Payload,
};
use aion_store::WriteToken;
use axum::{Router, http::StatusCode};
use chrono::{DateTime, TimeDelta, Utc};
use serde_json::json;
use tower::ServiceExt;

use super::super::router::workflow_router;
use super::super::test_support::{
    NAMESPACE, json_request, read_json, runtime_config, server_state, shared_engine, workflow_id,
};
use crate::worker::{AttemptKey, InFlightActivity, WorkerRegistration};
use crate::{
    NamespaceResolver, ServerState, StaticScheduleNamespaces, StaticWorkflowNamespaces,
    config::NamespaceMode,
};

type TestResult = Result<(), Box<dyn std::error::Error>>;

const ACTIVITY: u64 = 4;
const ATTEMPT: u32 = 1;

fn activity_id() -> ActivityId {
    ActivityId::from_sequence_position(ACTIVITY)
}

/// The one run every fixture in this file lives in: the history's
/// `WorkflowStarted`, the published transcript events, and the attempt-owner
/// bind all carry it, exactly as one live generation would.
fn run_id() -> aion_core::RunId {
    aion_core::RunId::new(uuid::Uuid::from_u128(10))
}

fn envelope(seq: u64, recorded_at: DateTime<Utc>) -> EventEnvelope {
    EventEnvelope {
        seq,
        recorded_at,
        workflow_id: workflow_id(),
    }
}

fn payload() -> Result<Payload, aion_core::PayloadError> {
    Payload::from_json(&json!({ "fixture": true }))
}

fn started(at: DateTime<Utc>) -> Result<Event, aion_core::PayloadError> {
    Ok(Event::WorkflowStarted {
        envelope: envelope(1, at),
        workflow_type: "fixture".to_owned(),
        input: payload()?,
        run_id: run_id(),
        parent_run_id: None,
        parent_workflow_id: None,
        package_version: aion_core::PackageVersion::new("a".repeat(64)),
    })
}

fn scheduled(at: DateTime<Utc>) -> Result<Event, aion_core::PayloadError> {
    Ok(Event::ActivityScheduled {
        envelope: envelope(2, at),
        activity_id: activity_id(),
        activity_type: "review".to_owned(),
        input: payload()?,
        task_queue: "agents".to_owned(),
        node: None,
    })
}

fn dispatched(at: DateTime<Utc>) -> Event {
    Event::ActivityStarted {
        envelope: envelope(3, at),
        activity_id: activity_id(),
        attempt: ATTEMPT,
    }
}

/// The history of a run parked inside its dispatched step, with the dispatch
/// recorded at `dispatched_at`.
fn in_flight_history(dispatched_at: DateTime<Utc>) -> Result<Vec<Event>, aion_core::PayloadError> {
    Ok(vec![
        started(dispatched_at)?,
        scheduled(dispatched_at)?,
        dispatched(dispatched_at),
    ])
}

/// A server state over a shared in-memory engine, seeded with `history`.
async fn state_with(
    history: &[Event],
    max_stream_events: u64,
) -> Result<ServerState, Box<dyn std::error::Error>> {
    let (engine, store, _visibility) = shared_engine().await?;
    if !history.is_empty() {
        store
            .append(WriteToken::recorder(), &workflow_id(), history, 0)
            .await?;
    }
    let ownership = StaticWorkflowNamespaces::default();
    ownership.record(workflow_id(), NAMESPACE)?;
    let resolver = NamespaceResolver::from_parts(
        NamespaceMode::SharedEngine,
        Some(engine),
        Arc::new(ownership),
        Arc::new(StaticScheduleNamespaces::default()),
    );
    let mut runtime = runtime_config();
    runtime.observability.max_stream_events = max_stream_events;
    server_state(resolver, runtime).await
}

/// Publish `count` transcript records onto the current attempt's stream.
async fn publish_transcript(
    state: &ServerState,
    count: u64,
) -> Result<(), Box<dyn std::error::Error>> {
    for worker_seq in 0..count {
        state
            .transcript_publisher()
            .publish(&ActivityEvent {
                workflow_id: workflow_id(),
                run_id: run_id(),
                activity_id: activity_id(),
                attempt: ATTEMPT,
                agent_id: uuid::Uuid::from_u128(42),
                agent_role: "orchestrator".to_owned(),
                emitted_at: DateTime::<Utc>::UNIX_EPOCH,
                worker_seq,
                store_seq: None,
                ephemeral: false,
                kind: ActivityEventKind::Message {
                    role: MessageRole::Assistant,
                    text: format!("line-{worker_seq}"),
                },
            })
            .await?;
    }
    Ok(())
}

/// Register a worker and make it the tracked owner of the current attempt.
///
/// Returns the registration guard: dropping it deregisters the worker, so the
/// caller must hold it for as long as the worker is meant to be live.
fn own_the_attempt(
    state: &ServerState,
    capabilities: InterventionCapabilities,
) -> Result<WorkerRegistration, Box<dyn std::error::Error>> {
    let (sender, _receiver) = tokio::sync::mpsc::channel(1);
    let activity_types = [String::from("review")];
    let registration = state.worker_registry().register_delivery(
        [NAMESPACE.to_owned()],
        aion_core::DEFAULT_TASK_QUEUE,
        None,
        activity_types.iter(),
        crate::worker::WorkerDelivery::Grpc(sender),
        RegistrationOptions::identified(String::from("describe-live-tests-worker"))
            .with_intervention_capabilities(capabilities),
    )?;
    let worker_id = registration
        .worker_id()
        .ok_or("registration did not assign a worker id")?;
    state.attempt_owners().bind(
        AttemptKey::new(workflow_id(), run_id(), activity_id(), ATTEMPT),
        worker_id,
    );
    state.heartbeat_tracker().track_task(
        worker_id,
        InFlightActivity {
            workflow_id: workflow_id(),
            activity_id: activity_id(),
            attempt: ATTEMPT,
            completion_token: crate::worker::CompletionToken::for_test(),
        },
        Instant::now(),
    )?;
    Ok(registration)
}

/// Record a progress note on the current attempt from its owning worker.
fn report_note(
    state: &ServerState,
    registration: &WorkerRegistration,
    note: &serde_json::Value,
) -> Result<(), Box<dyn std::error::Error>> {
    let worker_id = registration
        .worker_id()
        .ok_or("registration did not assign a worker id")?;
    state.heartbeat_tracker().record_heartbeat(
        worker_id,
        aion_proto::ProtoHeartbeat {
            workflow_id: Some(aion_proto::ProtoWorkflowId::from(workflow_id())),
            activity_id: Some(aion_proto::ProtoActivityId::from(activity_id())),
            progress: Some(aion_proto::ProtoPayload::from(Payload::from_json(note)?)),
        },
        Instant::now(),
    )?;
    Ok(())
}

async fn describe_live(
    router: &Router,
    fields: serde_json::Value,
) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
    let mut request = match fields {
        serde_json::Value::Object(request) => request,
        _other => return Err("describe-live request fields must be an object".into()),
    };
    request.insert("namespace".to_owned(), json!(NAMESPACE));
    request.insert("workflow_id".to_owned(), json!(workflow_id().to_string()));
    let response = router
        .clone()
        .oneshot(json_request("/workflows/describe-live", &request)?)
        .await?;
    if response.status() != StatusCode::OK {
        return Err(format!("describe-live failed with {}", response.status()).into());
    }
    read_json(response).await
}

/// The join composes: summary + status, the folded current step with its
/// stamped address, and the open-step list — all from one call.
#[tokio::test]
async fn the_join_reports_the_summary_and_the_folded_current_step() -> TestResult {
    let state = state_with(&in_flight_history(Utc::now())?, 20_000).await?;
    let router = workflow_router(state);

    let body = describe_live(&router, json!({})).await?;
    assert_eq!(body["summary"]["workflow_id"], workflow_id().to_string());
    assert_eq!(body["summary"]["status"], "Running");
    assert_eq!(body["current_step"]["step"]["activity_type"], "review");
    assert_eq!(body["current_step"]["step"]["task_queue"], "agents");
    assert_eq!(body["current_step"]["step"]["state"]["state"], "Dispatched");
    assert_eq!(body["current_step"]["step"]["state"]["attempt"], 1);
    assert_eq!(body["current_step"]["attempt"]["attempt"], 1);
    assert_eq!(
        body["open_steps"]
            .as_array()
            .ok_or("open_steps missing")?
            .len(),
        1
    );
    Ok(())
}

/// A run with nothing open answers `null` — the run is genuinely not inside an
/// activity, and the response says so rather than omitting the field.
#[tokio::test]
async fn a_run_inside_no_activity_reports_no_current_step() -> TestResult {
    let state = state_with(&[started(Utc::now())?], 20_000).await?;
    let router = workflow_router(state);

    let body = describe_live(&router, json!({})).await?;
    assert_eq!(body["current_step"], serde_json::Value::Null);
    assert!(
        body["open_steps"]
            .as_array()
            .ok_or("open_steps")?
            .is_empty()
    );
    assert!(
        body["unserved"].as_array().ok_or("unserved")?.is_empty(),
        "a run with no dispatch has nothing unserved"
    );
    Ok(())
}

/// A step scheduled but not dispatched has no attempt at all, so liveness, note
/// and transcript are structurally absent rather than reported as empty.
#[tokio::test]
async fn a_scheduled_step_has_no_attempt_to_report() -> TestResult {
    let now = Utc::now();
    let state = state_with(&[started(now)?, scheduled(now)?], 20_000).await?;
    let router = workflow_router(state);

    let body = describe_live(&router, json!({ "tail": 5 })).await?;
    assert_eq!(body["current_step"]["step"]["state"]["state"], "Scheduled");
    assert_eq!(body["current_step"]["attempt"], serde_json::Value::Null);
    Ok(())
}

/// NOTE STATE 1 — reported: the owning worker's note comes back verbatim.
#[tokio::test]
async fn a_reported_note_is_returned_with_its_payload() -> TestResult {
    let state = state_with(&in_flight_history(Utc::now())?, 20_000).await?;
    let registration = own_the_attempt(&state, InterventionCapabilities::none())?;
    report_note(
        &state,
        &registration,
        &json!({ "doing": "reading the brief" }),
    )?;
    let router = workflow_router(state);

    let body = describe_live(&router, json!({})).await?;
    let note = &body["current_step"]["attempt"]["note"];
    assert_eq!(note["note"], "Reported");
    let payload: Payload = serde_json::from_value(note["payload"].clone())?;
    assert_eq!(payload.to_json()?["doing"], "reading the brief");
    assert!(
        note["reported_at"].is_string(),
        "a reported note carries when this process received it"
    );
    drop(registration);
    Ok(())
}

/// NOTE STATE 2 — none sent: the attempt IS tracked here and the worker has
/// reported nothing. This is a measurement, and the response says so.
#[tokio::test]
async fn a_tracked_attempt_with_no_note_reports_silence() -> TestResult {
    let state = state_with(&in_flight_history(Utc::now())?, 20_000).await?;
    let registration = own_the_attempt(&state, InterventionCapabilities::none())?;
    let router = workflow_router(state);

    let body = describe_live(&router, json!({})).await?;
    assert_eq!(body["current_step"]["attempt"]["note"]["note"], "NoneSent");
    drop(registration);
    Ok(())
}

/// NOTE STATE 3 — unavailable, with the restart named: the attempt was
/// dispatched BEFORE this process began holding notes, so the absence of a note
/// says nothing about the worker. Nothing is tracked, exactly as after a
/// restart.
#[tokio::test]
async fn an_attempt_older_than_this_process_reports_unavailable_notes() -> TestResult {
    let long_ago = Utc::now()
        .checked_sub_signed(TimeDelta::hours(1))
        .ok_or("clock underflow")?;
    let state = state_with(&in_flight_history(long_ago)?, 20_000).await?;
    let held_since = state.heartbeat_tracker().notes_held_since();
    let router = workflow_router(state);

    let body = describe_live(&router, json!({})).await?;
    let note = &body["current_step"]["attempt"]["note"];
    assert_eq!(
        note["note"], "Unavailable",
        "a cold store must never be reported as a silent worker"
    );
    assert_eq!(note["restarted_since_attempt_began"], true);
    assert_eq!(note["notes_held_since"], json!(held_since));
    Ok(())
}

/// The restart flag is a MEASUREMENT, not a constant: an attempt dispatched
/// after this process began holding notes, but not tracked by it, is still
/// unavailable — and says a restart does NOT explain it.
#[tokio::test]
async fn an_untracked_recent_attempt_is_unavailable_without_blaming_a_restart() -> TestResult {
    // The dispatch instant is stamped when the state (and its tracker) already
    // exists, so it is strictly after `notes_held_since`.
    let state = state_with(&[], 20_000).await?;
    let dispatched_at = Utc::now();
    let engine = state.engine()?;
    engine
        .store()
        .append(
            WriteToken::recorder(),
            &workflow_id(),
            &in_flight_history(dispatched_at)?,
            0,
        )
        .await?;
    let held_since = state.heartbeat_tracker().notes_held_since();
    assert!(
        dispatched_at > held_since,
        "the fixture must dispatch after the tracker existed"
    );
    let router = workflow_router(state);

    let body = describe_live(&router, json!({})).await?;
    let note = &body["current_step"]["attempt"]["note"];
    assert_eq!(note["note"], "Unavailable");
    assert_eq!(
        note["restarted_since_attempt_began"], false,
        "no restart happened, so none is claimed"
    );
    Ok(())
}

/// Liveness comes from the live attempt→owner index, and reports the owning
/// worker's advertised primitives.
#[tokio::test]
async fn a_live_owner_is_reported_with_its_advertised_capabilities() -> TestResult {
    let state = state_with(&in_flight_history(Utc::now())?, 20_000).await?;
    let registration = own_the_attempt(
        &state,
        InterventionCapabilities::from_primitives([InterventionPrimitive::InjectMessage]),
    )?;
    let router = workflow_router(state);

    let body = describe_live(&router, json!({})).await?;
    let liveness = &body["current_step"]["attempt"]["liveness"];
    assert_eq!(liveness["liveness"], "Live");
    assert_eq!(
        liveness["capabilities"]["supported"][0]["primitive"],
        "InjectMessage"
    );
    drop(registration);
    Ok(())
}

/// The control for the above: with nothing owning the attempt, the same request
/// reports no live owner rather than an empty capability set.
#[tokio::test]
async fn an_unowned_attempt_reports_no_live_owner() -> TestResult {
    let state = state_with(&in_flight_history(Utc::now())?, 20_000).await?;
    let router = workflow_router(state);

    let body = describe_live(&router, json!({})).await?;
    assert_eq!(
        body["current_step"]["attempt"]["liveness"]["liveness"],
        "NoLiveOwner"
    );
    Ok(())
}

/// An omitted `tail` reads no transcript and claims nothing about the stream.
#[tokio::test]
async fn an_omitted_tail_claims_nothing_about_the_transcript() -> TestResult {
    let state = state_with(&in_flight_history(Utc::now())?, 20_000).await?;
    publish_transcript(&state, 3).await?;
    let router = workflow_router(state);

    let body = describe_live(&router, json!({})).await?;
    assert_eq!(
        body["current_step"]["attempt"]["transcript"]["transcript"],
        "NotRequested"
    );
    // The stream heads are still reported: enumeration is not the tail.
    assert_eq!(body["transcript_streams"][0]["head_seq"], 3);
    Ok(())
}

/// A requested tail returns the LAST records and says how many it left behind.
#[tokio::test]
async fn a_requested_tail_returns_the_last_records_and_names_what_it_omitted() -> TestResult {
    let state = state_with(&in_flight_history(Utc::now())?, 20_000).await?;
    publish_transcript(&state, 5).await?;
    let router = workflow_router(state);

    let body = describe_live(&router, json!({ "tail": 2 })).await?;
    let transcript = &body["current_step"]["attempt"]["transcript"];
    assert_eq!(transcript["transcript"], "Window");
    let events = transcript["events"].as_array().ok_or("events missing")?;
    assert_eq!(events.len(), 2);
    assert_eq!(events[0]["store_seq"], 3);
    assert_eq!(events[1]["store_seq"], 4);
    assert_eq!(transcript["head_seq"], 5);
    assert_eq!(transcript["omitted_before"], 3);
    assert_eq!(
        transcript["retention_truncated"], false,
        "a stream well inside the cap is not truncated"
    );
    Ok(())
}

/// TRUNCATION HONESTY: with the retention cap crossed, both the tail and the
/// stream head SAY the stream was truncated. The events after the cap exist
/// only on the live socket, and a reader must not read the last retained record
/// as the agent falling silent.
#[tokio::test]
async fn a_capped_stream_says_truncated_rather_than_showing_silence() -> TestResult {
    let state = state_with(&in_flight_history(Utc::now())?, 3).await?;
    // Five events against a cap of three: three are retained, the fourth writes
    // the cap marker, and everything after is live-only.
    publish_transcript(&state, 5).await?;
    let router = workflow_router(state);

    let body = describe_live(&router, json!({ "tail": 10 })).await?;
    let transcript = &body["current_step"]["attempt"]["transcript"];
    assert_eq!(
        transcript["retention_truncated"], true,
        "the tail must say the retention stopped, not imply the agent did"
    );
    assert_eq!(transcript["head_seq"], 4, "three records plus the marker");
    assert_eq!(
        body["transcript_streams"][0]["retention_truncated"], true,
        "the stream enumeration says it too"
    );
    // The marker itself is retained and names the cap, so the last record a
    // reader sees explains the ending.
    let events = transcript["events"].as_array().ok_or("events missing")?;
    let last = events.last().ok_or("the marker is retained")?;
    assert!(
        last["kind"]["detail"]["text"]
            .as_str()
            .unwrap_or_default()
            .contains("retention cap"),
        "the last retained record explains the ending: {last}"
    );
    Ok(())
}

/// Stream enumeration is annotated: each stream names its activity type and
/// whether it is the current attempt's — the two facts a bare `(activity,
/// attempt, head)` list leaves a reader to guess.
#[tokio::test]
async fn transcript_streams_name_their_activity_and_mark_the_current_one() -> TestResult {
    let now = Utc::now();
    // An earlier step with its own retained stream, then the current one. The
    // envelope sequence is contiguous because the store enforces it.
    let history = vec![
        started(now)?,
        Event::ActivityScheduled {
            envelope: envelope(2, now),
            activity_id: ActivityId::from_sequence_position(1),
            activity_type: "plan".to_owned(),
            input: payload()?,
            task_queue: "agents".to_owned(),
            node: None,
        },
        Event::ActivityScheduled {
            envelope: envelope(3, now),
            activity_id: activity_id(),
            activity_type: "review".to_owned(),
            input: payload()?,
            task_queue: "agents".to_owned(),
            node: None,
        },
        Event::ActivityStarted {
            envelope: envelope(4, now),
            activity_id: activity_id(),
            attempt: ATTEMPT,
        },
    ];
    let state = state_with(&history, 20_000).await?;
    publish_transcript(&state, 2).await?;
    state
        .transcript_publisher()
        .publish(&ActivityEvent {
            workflow_id: workflow_id(),
            run_id: run_id(),
            activity_id: ActivityId::from_sequence_position(1),
            attempt: ATTEMPT,
            agent_id: uuid::Uuid::from_u128(42),
            agent_role: "orchestrator".to_owned(),
            emitted_at: DateTime::<Utc>::UNIX_EPOCH,
            worker_seq: 0,
            store_seq: None,
            ephemeral: false,
            kind: ActivityEventKind::Message {
                role: MessageRole::Assistant,
                text: "planning".to_owned(),
            },
        })
        .await?;
    let router = workflow_router(state);

    let body = describe_live(&router, json!({})).await?;
    let streams = body["transcript_streams"]
        .as_array()
        .ok_or("transcript_streams missing")?;
    assert_eq!(streams.len(), 2);
    let plan = streams
        .iter()
        .find(|stream| stream["activity_id"] == json!(1))
        .ok_or("the earlier step's stream is enumerated")?;
    assert_eq!(plan["activity_type"], "plan");
    assert_eq!(plan["current"], false);
    let review = streams
        .iter()
        .find(|stream| stream["activity_id"] == json!(ACTIVITY))
        .ok_or("the current step's stream is enumerated")?;
    assert_eq!(review["activity_type"], "review");
    assert_eq!(
        review["current"], true,
        "the current attempt's stream is marked as such"
    );
    Ok(())
}

/// A caller scoped to another namespace gets the guard's anti-leak answer, not
/// a step, a note, or a transcript.
#[tokio::test]
async fn a_foreign_workflow_is_refused_before_anything_is_read() -> TestResult {
    let state = state_with(&in_flight_history(Utc::now())?, 20_000).await?;
    let router = workflow_router(state);

    let response = router
        .oneshot(json_request(
            "/workflows/describe-live",
            &json!({
                "namespace": "tenant-b",
                "workflow_id": workflow_id().to_string(),
            }),
        )?)
        .await?;
    assert_ne!(
        response.status(),
        StatusCode::OK,
        "a foreign namespace must not receive a live view"
    );
    Ok(())
}

/// A malformed workflow id is a typed input error, not a 500.
#[tokio::test]
async fn a_malformed_workflow_id_is_a_typed_input_error() -> TestResult {
    let state = state_with(&in_flight_history(Utc::now())?, 20_000).await?;
    let router = workflow_router(state);

    let response = router
        .oneshot(json_request(
            "/workflows/describe-live",
            &json!({ "namespace": NAMESPACE, "workflow_id": "not-a-uuid" }),
        )?)
        .await?;
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    let error: aion_proto::WireError = read_json(response).await?;
    assert_eq!(error.code, aion_proto::WireErrorCode::InvalidInput);
    Ok(())
}

/// The heartbeat window the tracker was built with is irrelevant to the note
/// read, but the fixture pins it so a future change to the default cannot
/// silently expire the tracked attempt mid-test.
#[tokio::test]
async fn the_tracked_attempt_survives_the_read() -> TestResult {
    let state = state_with(&in_flight_history(Utc::now())?, 20_000).await?;
    assert_eq!(
        state.runtime_config().worker.heartbeat_window,
        Duration::from_secs(30)
    );
    let registration = own_the_attempt(&state, InterventionCapabilities::none())?;
    report_note(&state, &registration, &json!({ "doing": "work" }))?;
    let router = workflow_router(state);

    let first = describe_live(&router, json!({})).await?;
    let second = describe_live(&router, json!({})).await?;
    assert_eq!(
        first["current_step"]["attempt"]["note"], second["current_step"]["attempt"]["note"],
        "reading the note must not consume it"
    );
    drop(registration);
    Ok(())
}