adk-core 3.0.0

Core traits and types for Rust Agent Development Kit (ADK-Rust) agents, tools, sessions, and events
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
//! Conformance cases for [`DurableRunStore`] backends.
//!
//! | Case | Contract | Backend primitive |
//! |------|----------|-------------------|
//! | [`enqueue_claim_finish_round_trip`] | A run reads back as enqueued, is claimed at attempt 1 and epoch 1, and completes; filters and unknown ids behave | Plain reads and writes |
//! | [`idempotent_enqueue_under_concurrency`] | 32 concurrent enqueues of one key across two handles create one run; keys are scoped per queue | Unique index or create-if-absent |
//! | [`on_conflict_policies`] | `Reject`, `Enqueue`, and `Interrupt` treat an unfinished run holding the concurrency key as documented | Read of key holders inside the enqueue transaction |
//! | [`concurrent_claims_have_one_winner`] | 32 concurrent claimers across two handles claim each run exactly once | Row lock, Lua, `findOneAndUpdate`, or a transaction |
//! | [`not_before_is_respected`] | A delayed run is not claimed before its time | Indexed `not_before` filter |
//! | [`expired_lease_recovery`] | A run whose lease expired is reclaimed at the next attempt and epoch; exhausted attempts fail it | Expiry compared inside the claim |
//! | [`stale_epoch_writes_are_refused`] | `heartbeat`, `set_checkpoint`, `wait`, and `finish` with a stale epoch fail with `run_store.stale_epoch`; unknown runs fail with `run_store.not_found` | Epoch compared inside the conditional write |
//! | [`retryable_failure_requeues_until_max_attempts`] | A retryable failure requeues while attempts remain; others are terminal | Conditional write |
//! | [`wait_and_wake`] | `wait` parks a run until its timer or `wake`; a wake that arrives first is not lost | Conditional write |
//! | [`parked_run_keeps_its_checkpoint`] | A run parked until woken on a `Session` checkpoint, as a suspended run is, holds no lease, keeps its attempt and checkpoint across a restart, resumes at the same attempt, and cancels at once | Conditional write |
//! | [`freeze_parked_runs_wake_only_when_woken`] | Runs parked until woken, as a freeze parks them, never expire; `wake` resumes exactly the runs it names and leaves the others parked; a parked run cancels and a later wake does not revive it | Conditional write |
//! | [`cancel_is_visible_through_heartbeat`] | Cancelling a running run is reported by the next heartbeat; queued runs cancel at once | Conditional write |
//! | [`prune_removes_old_finished_runs`] | Prune deletes finished runs older than the cut-off, keeps the rest, and frees their idempotency keys | Range delete |
//! | [`runs_survive_a_restart`] | Runs, leases, and checkpoints read back after a restart, and fencing still holds | Durable storage |
//!
//! Queue names, keys, and identifiers contain `:`, `/`, and non-ASCII characters, so a
//! backend that builds keys from them is checked for escaping.

use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use chrono::{DateTime, TimeZone, Utc};
use serde_json::json;

use super::{base_time, unique_unicode};
use crate::principal::{ActingContext, AgentId, AgentPrincipal, SubjectRef};
use crate::run_store::{
    ClaimedRun, DurableRunStore, Enqueued, NewRun, OnConflict, RUN_NOT_FOUND_CODE,
    RUN_STALE_EPOCH_CODE, RunCheckpointRef, RunFilter, RunOutcome, RunRecord, RunState,
};
use crate::{Clock, ManualClock};

/// Opens a [`DurableRunStore`] backend for the conformance suite.
///
/// # Example
///
/// ```rust
/// use std::sync::Arc;
/// use adk_core::conformance::RunStoreHarness;
/// use adk_core::{Clock, DurableRunStore, InMemoryRunStore, async_trait};
///
/// struct InMemory;
///
/// #[async_trait]
/// impl RunStoreHarness for InMemory {
///     async fn open(&self, clock: Arc<dyn Clock>) -> Arc<dyn DurableRunStore> {
///         Arc::new(InMemoryRunStore::new().with_clock(clock))
///     }
///
///     fn is_durable(&self) -> bool {
///         false
///     }
/// }
/// ```
#[async_trait]
pub trait RunStoreHarness: Send + Sync {
    /// Opens a store that reads `clock`.
    ///
    /// For a durable backend every call returns a new handle over the same stored data,
    /// as a restarted process or a second process would see it. Panics when the backend
    /// cannot be opened.
    async fn open(&self, clock: Arc<dyn Clock>) -> Arc<dyn DurableRunStore>;

    /// Whether data written through one handle is visible through the next.
    ///
    /// `false` skips the restart case and runs the concurrency cases on a single handle.
    fn is_durable(&self) -> bool {
        true
    }
}

const TTL: Duration = Duration::from_secs(30);

fn new_run(queue: &str) -> NewRun {
    NewRun::new(queue, "conformance-app", "user:ΓΌ/1", "agent", json!({ "text": "hΓ©llo" }))
}

async fn two_handles<H: RunStoreHarness + ?Sized>(
    harness: &H,
    clock: &ManualClock,
) -> (Arc<dyn DurableRunStore>, Arc<dyn DurableRunStore>) {
    let first = harness.open(Arc::new(clock.clone())).await;
    let second = if harness.is_durable() {
        harness.open(Arc::new(clock.clone())).await
    } else {
        first.clone()
    };
    (first, second)
}

async fn created(store: &dyn DurableRunStore, run: NewRun) -> RunRecord {
    match store.enqueue(run).await.expect("enqueue") {
        Enqueued::Created(record) => record,
        other => panic!("expected a new run, got {other:?}"),
    }
}

async fn claim(store: &dyn DurableRunStore, queue: &str, worker: &str) -> Option<ClaimedRun> {
    store.claim(queue, worker, TTL).await.expect("claim")
}

async fn get(store: &dyn DurableRunStore, run_id: &str) -> RunRecord {
    store.get(run_id).await.expect("get").expect("the run exists")
}

#[track_caller]
fn assert_code<T: std::fmt::Debug>(result: crate::Result<T>, code: &str, context: &str) {
    let error = result.expect_err(context);
    assert_eq!(error.code, code, "{context}: {error}");
    let category = if code == RUN_STALE_EPOCH_CODE {
        crate::ErrorCategory::Conflict
    } else {
        crate::ErrorCategory::NotFound
    };
    assert_eq!(error.category, category, "{context}: {error}");
}

/// A run reads back exactly as enqueued, is claimed at attempt 1 and epoch 1 by one
/// worker, completes with its result, and is listed by every matching filter.
pub async fn enqueue_claim_finish_round_trip<H: RunStoreHarness + ?Sized>(harness: &H) {
    let clock = ManualClock::new(base_time());
    let store = harness.open(Arc::new(clock.clone())).await;
    let queue = unique_unicode("queue");
    assert!(claim(&*store, &queue, "w1").await.is_none(), "an empty queue has nothing to claim");

    let principal = ActingContext::new(
        AgentPrincipal::new(AgentId::new("urn:adk:agent:conformance/ΓΌ").expect("valid id"))
            .with_tenant("tenant:1"),
    )
    .on_behalf_of(SubjectRef::human("alice@example.com"));
    let run = new_run(&queue)
        .with_session_id(unique_unicode("session"))
        .with_principal(principal)
        .with_max_attempts(4)
        .with_authenticated_user();
    let record = created(&*store, run.clone()).await;
    let expected = RunRecord::queued(record.run_id.clone(), run, base_time());
    assert_eq!(record, expected);
    assert_eq!(get(&*store, &record.run_id).await, expected);

    clock.advance(Duration::from_secs(1));
    let claimed = claim(&*store, &queue, "w1").await.expect("the run is claimable");
    let running = RunRecord {
        state: RunState::Running,
        attempt: 1,
        lease_owner: Some("w1".into()),
        lease_epoch: 1,
        lease_expires_at: Some(clock.now() + chrono::Duration::seconds(30)),
        heartbeat_at: Some(clock.now()),
        updated_at: clock.now(),
        revision: claimed.record.revision,
        ..expected.clone()
    };
    assert_eq!(claimed, ClaimedRun { record: running.clone(), epoch: 1 });
    assert!(claimed.record.revision > expected.revision, "every change moves the revision");
    assert!(claim(&*store, &queue, "w2").await.is_none(), "a held run is not claimed again");

    let done = store
        .finish(&record.run_id, 1, RunOutcome::Completed(json!({ "answer": 42 })))
        .await
        .expect("finish");
    assert_eq!(done.state, RunState::Completed);
    assert_eq!(done.result, Some(json!({ "answer": 42 })));
    assert_eq!((done.lease_owner, done.lease_expires_at), (None, None));
    assert_eq!(get(&*store, &record.run_id).await.state, RunState::Completed);

    let listed = store
        .list(RunFilter::default().queue(&queue).states([RunState::Completed]))
        .await
        .expect("list");
    assert_eq!(
        listed.iter().map(|r| r.run_id.as_str()).collect::<Vec<_>>(),
        [record.run_id.as_str()]
    );
    let none = store
        .list(RunFilter::default().queue(&queue).states([RunState::Queued]))
        .await
        .expect("list");
    assert!(none.is_empty());
    assert_eq!(store.get(&unique_unicode("missing")).await.expect("get"), None);
}

/// Concurrent enqueues of one idempotency key, split across two handles, create exactly
/// one run and return it to every caller; the same key in another queue is another run.
pub async fn idempotent_enqueue_under_concurrency<H: RunStoreHarness + ?Sized>(harness: &H) {
    let clock = ManualClock::new(base_time());
    let (first, second) = two_handles(harness, &clock).await;
    let queue = unique_unicode("queue");
    let key = unique_unicode("idempotency");

    let tasks: Vec<_> = (0..32)
        .map(|i| {
            let store = if i % 2 == 0 { first.clone() } else { second.clone() };
            let run = new_run(&queue).with_idempotency_key(&key);
            tokio::spawn(async move { store.enqueue(run).await })
        })
        .collect();
    let (mut created_ids, mut existing_ids) = (Vec::new(), Vec::new());
    for task in tasks {
        match task.await.expect("enqueue task completes").expect("enqueue") {
            Enqueued::Created(record) => created_ids.push(record.run_id),
            Enqueued::Existing(record) => existing_ids.push(record.run_id),
            Enqueued::Rejected { blocking_run_id } => panic!("rejected by {blocking_run_id}"),
        }
    }
    assert_eq!(created_ids.len(), 1, "exactly one enqueue creates the run");
    assert_eq!(existing_ids.len(), 31);
    assert!(existing_ids.iter().all(|id| *id == created_ids[0]), "every caller gets the same run");
    let queued = first.list(RunFilter::default().queue(&queue)).await.expect("list");
    assert_eq!(queued.len(), 1);

    let other_queue = unique_unicode("queue");
    let other = created(&*first, new_run(&other_queue).with_idempotency_key(&key)).await;
    assert_ne!(other.run_id, created_ids[0], "keys are unique per queue");
}

/// `Reject` names the oldest unfinished run holding the key; `Enqueue` queues behind it
/// and is claimed only after it finishes; `Interrupt` cancels queued and waiting
/// holders, flags running ones, and queues behind them.
pub async fn on_conflict_policies<H: RunStoreHarness + ?Sized>(harness: &H) {
    let clock = ManualClock::new(base_time());
    let store = harness.open(Arc::new(clock.clone())).await;
    let queue = unique_unicode("queue");
    let key = unique_unicode("thread");
    let keyed = |policy| new_run(&queue).with_concurrency_key(&key, policy);

    let holder = created(&*store, keyed(OnConflict::Enqueue)).await;
    let running = claim(&*store, &queue, "w1").await.expect("holder is claimable");
    assert_eq!(running.record.run_id, holder.run_id);

    clock.advance(Duration::from_secs(1));
    let rejected = store.enqueue(keyed(OnConflict::Reject)).await.expect("enqueue");
    assert_eq!(rejected, Enqueued::Rejected { blocking_run_id: holder.run_id.clone() });

    let behind = created(&*store, keyed(OnConflict::Enqueue)).await;
    let free = created(&*store, new_run(&queue)).await;
    let next = claim(&*store, &queue, "w2").await.expect("an unkeyed run is claimable");
    assert_eq!(next.record.run_id, free.run_id, "the keyed run waits behind its holder");
    assert!(claim(&*store, &queue, "w2").await.is_none(), "the keyed run is still blocked");
    store
        .finish(&free.run_id, next.epoch, RunOutcome::Completed(json!(null)))
        .await
        .expect("finish");

    clock.advance(Duration::from_secs(1));
    let interrupting = created(&*store, keyed(OnConflict::Interrupt)).await;
    assert_eq!(get(&*store, &behind.run_id).await.state, RunState::Cancelled, "queued holder");
    let flagged = get(&*store, &holder.run_id).await;
    assert_eq!((flagged.state, flagged.cancel_requested), (RunState::Running, true));
    assert!(
        store.heartbeat(&holder.run_id, running.epoch, TTL).await.expect("heartbeat"),
        "the running holder sees the cancellation"
    );
    assert!(claim(&*store, &queue, "w2").await.is_none(), "the new run waits for the holder");
    store.finish(&holder.run_id, running.epoch, RunOutcome::Cancelled).await.expect("finish");
    let claimed = claim(&*store, &queue, "w2").await.expect("the interrupting run is next");
    assert_eq!(claimed.record.run_id, interrupting.run_id);

    let after = store.enqueue(keyed(OnConflict::Reject)).await.expect("enqueue");
    assert_eq!(after, Enqueued::Rejected { blocking_run_id: interrupting.run_id.clone() });
}

/// 32 concurrent claimers across two handles claim each of 8 runs exactly once.
pub async fn concurrent_claims_have_one_winner<H: RunStoreHarness + ?Sized>(harness: &H) {
    let clock = ManualClock::new(base_time());
    let (first, second) = two_handles(harness, &clock).await;
    let queue = unique_unicode("queue");
    let mut enqueued = Vec::new();
    for _ in 0..8 {
        enqueued.push(created(&*first, new_run(&queue)).await.run_id);
    }

    let tasks: Vec<_> = (0..32)
        .map(|i| {
            let store = if i % 2 == 0 { first.clone() } else { second.clone() };
            let queue = queue.clone();
            tokio::spawn(async move { store.claim(&queue, &format!("worker-{i}"), TTL).await })
        })
        .collect();
    let mut claimed = Vec::new();
    for task in tasks {
        if let Some(run) = task.await.expect("claim task completes").expect("claim") {
            assert_eq!(run.epoch, 1);
            claimed.push(run.record.run_id);
        }
    }
    claimed.sort();
    enqueued.sort();
    assert_eq!(claimed, enqueued, "every run is claimed exactly once");
}

/// A run delayed with `not_before` is not claimed before that time and is claimed at it.
pub async fn not_before_is_respected<H: RunStoreHarness + ?Sized>(harness: &H) {
    let clock = ManualClock::new(base_time());
    let store = harness.open(Arc::new(clock.clone())).await;
    let queue = unique_unicode("queue");
    let at = base_time() + chrono::Duration::minutes(5);
    let delayed = created(&*store, new_run(&queue).with_not_before(at)).await;

    assert!(claim(&*store, &queue, "w1").await.is_none(), "before not_before");
    clock.set(at - chrono::Duration::seconds(1));
    assert!(claim(&*store, &queue, "w1").await.is_none(), "a second before not_before");
    clock.set(at);
    let claimed = claim(&*store, &queue, "w1").await.expect("claimable at not_before");
    assert_eq!(claimed.record.run_id, delayed.run_id);
}

/// A run whose lease expired is reclaimed by another worker at the next attempt and
/// epoch, and a recovery past `max_attempts` fails the run instead of claiming it.
pub async fn expired_lease_recovery<H: RunStoreHarness + ?Sized>(harness: &H) {
    let clock = ManualClock::new(base_time());
    let (first, second) = two_handles(harness, &clock).await;
    let queue = unique_unicode("queue");
    let run = created(&*first, new_run(&queue).with_max_attempts(2)).await;

    let lost = claim(&*first, &queue, "w1").await.expect("claim");
    clock.advance(Duration::from_secs(29));
    assert!(claim(&*second, &queue, "w2").await.is_none(), "the lease is still live");
    clock.advance(Duration::from_secs(2));
    let recovered = claim(&*second, &queue, "w2").await.expect("the expired run is recovered");
    assert_eq!(recovered.record.run_id, run.run_id);
    assert_eq!(
        (recovered.epoch, recovered.record.attempt, recovered.record.lease_owner.as_deref()),
        (lost.epoch + 1, 2, Some("w2"))
    );
    assert_code(
        first.heartbeat(&run.run_id, lost.epoch, TTL).await,
        RUN_STALE_EPOCH_CODE,
        "the first worker lost the run",
    );

    clock.advance(Duration::from_secs(31));
    assert!(claim(&*first, &queue, "w3").await.is_none(), "attempts are exhausted");
    let failed = get(&*first, &run.run_id).await;
    assert_eq!((failed.state, failed.attempt), (RunState::Failed, 2));
    assert!(failed.error.is_some(), "the failure says why");
}

/// Every worker write carrying a stale epoch fails with `run_store.stale_epoch` and
/// changes nothing; every operation on an unknown run fails with `run_store.not_found`.
pub async fn stale_epoch_writes_are_refused<H: RunStoreHarness + ?Sized>(harness: &H) {
    let clock = ManualClock::new(base_time());
    let store = harness.open(Arc::new(clock.clone())).await;
    let queue = unique_unicode("queue");
    let run = created(&*store, new_run(&queue)).await;
    let id = run.run_id.as_str();
    assert_code(store.heartbeat(id, 0, TTL).await, RUN_STALE_EPOCH_CODE, "a queued run");

    let claimed = claim(&*store, &queue, "w1").await.expect("claim");
    let stale = claimed.epoch + 1;
    let checkpoint = RunCheckpointRef::Session { session_id: "s/1".into(), invocation_id: None };
    assert_code(store.heartbeat(id, stale, TTL).await, RUN_STALE_EPOCH_CODE, "heartbeat");
    assert_code(
        store.set_checkpoint(id, stale, checkpoint.clone()).await,
        RUN_STALE_EPOCH_CODE,
        "set_checkpoint",
    );
    assert_code(store.wait(id, stale, None).await, RUN_STALE_EPOCH_CODE, "wait");
    assert_code(
        store.finish(id, stale, RunOutcome::Completed(json!(1))).await,
        RUN_STALE_EPOCH_CODE,
        "finish",
    );
    let unchanged = get(&*store, id).await;
    assert_eq!(unchanged, claimed.record, "refused writes change nothing");

    store.set_checkpoint(id, claimed.epoch, checkpoint.clone()).await.expect("current epoch");
    assert_eq!(get(&*store, id).await.checkpoint, Some(checkpoint));
    store.finish(id, claimed.epoch, RunOutcome::Completed(json!(1))).await.expect("finish");
    assert_code(
        store.finish(id, claimed.epoch, RunOutcome::Completed(json!(2))).await,
        RUN_STALE_EPOCH_CODE,
        "a finished run",
    );
    assert_eq!(get(&*store, id).await.result, Some(json!(1)), "the first finish stands");

    let missing = unique_unicode("missing");
    assert_code(store.heartbeat(&missing, 1, TTL).await, RUN_NOT_FOUND_CODE, "heartbeat");
    assert_code(
        store.finish(&missing, 1, RunOutcome::Cancelled).await,
        RUN_NOT_FOUND_CODE,
        "finish",
    );
    assert_code(store.wake(&missing).await, RUN_NOT_FOUND_CODE, "wake");
    assert_code(store.request_cancel(&missing).await, RUN_NOT_FOUND_CODE, "request_cancel");
}

/// A retryable failure requeues the run while attempts remain, claimable once its backoff
/// has passed, the next claim starts the next attempt, and the last attempt's failure is
/// terminal; a non-retryable failure is terminal at once.
pub async fn retryable_failure_requeues_until_max_attempts<H: RunStoreHarness + ?Sized>(
    harness: &H,
) {
    let clock = ManualClock::new(base_time());
    let store = harness.open(Arc::new(clock.clone())).await;
    let queue = unique_unicode("queue");
    let run = created(&*store, new_run(&queue).with_max_attempts(3)).await;
    let failure = |error: &str, retryable, backoff: Option<Duration>| RunOutcome::Failed {
        error: error.into(),
        retryable,
        backoff,
    };

    let first = claim(&*store, &queue, "w1").await.expect("claim");
    let backoff = Duration::from_secs(30);
    let requeued = store
        .finish(&run.run_id, first.epoch, failure("rate limited", true, Some(backoff)))
        .await
        .expect("finish");
    let due = clock.now() + backoff;
    assert_eq!(
        (requeued.state, requeued.attempt, requeued.error.as_deref(), requeued.not_before),
        (RunState::Queued, 1, Some("rate limited"), Some(due))
    );
    assert_eq!(get(&*store, &run.run_id).await.not_before, Some(due), "the backoff is stored");
    clock.advance(Duration::from_secs(29));
    assert!(claim(&*store, &queue, "w2").await.is_none(), "not claimable during its backoff");
    clock.advance(Duration::from_secs(1));
    let retried = claim(&*store, &queue, "w2").await.expect("claimable once the backoff passed");
    assert_eq!((retried.record.attempt, retried.record.not_before), (2, None));
    let requeued = store
        .finish(&run.run_id, retried.epoch, failure("timeout", true, None))
        .await
        .expect("finish");
    assert_eq!((requeued.state, requeued.not_before), (RunState::Queued, None));
    let second = claim(&*store, &queue, "w2").await.expect("the retry is claimable");
    assert_eq!((second.record.attempt, second.epoch), (3, first.epoch + 2));
    let failed = store
        .finish(&run.run_id, second.epoch, failure("timeout", true, Some(backoff)))
        .await
        .expect("finish");
    assert_eq!(failed.state, RunState::Failed, "no attempts left");
    assert!(claim(&*store, &queue, "w3").await.is_none());
    clock.advance(backoff);
    assert!(claim(&*store, &queue, "w3").await.is_none(), "a failed run is never claimed");

    let fatal = created(&*store, new_run(&queue).with_max_attempts(5)).await;
    let claimed = claim(&*store, &queue, "w1").await.expect("claim");
    let failed = store
        .finish(&fatal.run_id, claimed.epoch, failure("bad input", false, None))
        .await
        .expect("finish");
    assert_eq!(failed.state, RunState::Failed, "non-retryable failures are terminal");
}

/// `wait` releases the lease and parks the run until its timer passes, at the same
/// attempt and the next epoch; `wake` makes a parked run claimable at once; a wake that
/// arrives while the run is still running is kept until it parks.
pub async fn wait_and_wake<H: RunStoreHarness + ?Sized>(harness: &H) {
    let clock = ManualClock::new(base_time());
    let store = harness.open(Arc::new(clock.clone())).await;
    let queue = unique_unicode("queue");
    let run = created(&*store, new_run(&queue)).await;
    let id = run.run_id.as_str();

    let claimed = claim(&*store, &queue, "w1").await.expect("claim");
    let until = base_time() + chrono::Duration::minutes(10);
    store.wait(id, claimed.epoch, Some(until)).await.expect("wait");
    let parked = get(&*store, id).await;
    assert_eq!((parked.state, parked.lease_owner.as_deref()), (RunState::Waiting, None));
    assert_code(store.heartbeat(id, claimed.epoch, TTL).await, RUN_STALE_EPOCH_CODE, "parked");
    clock.advance(Duration::from_secs(60));
    assert!(claim(&*store, &queue, "w2").await.is_none(), "the timer has not passed");
    clock.set(until);
    let resumed = claim(&*store, &queue, "w2").await.expect("the timer passed");
    assert_eq!((resumed.record.attempt, resumed.epoch), (1, claimed.epoch + 1));

    store.wait(id, resumed.epoch, None).await.expect("wait for a signal");
    clock.advance(Duration::from_secs(3600));
    assert!(claim(&*store, &queue, "w3").await.is_none(), "parked until woken");
    store.wake(id).await.expect("wake");
    let woken = claim(&*store, &queue, "w3").await.expect("woken");
    assert_eq!((woken.record.attempt, woken.epoch), (1, resumed.epoch + 1));

    store.wake(id).await.expect("a wake while running is kept");
    store.wait(id, woken.epoch, None).await.expect("wait");
    let early = claim(&*store, &queue, "w4").await.expect("the earlier wake applies");
    assert_eq!(early.record.run_id, run.run_id);
    store.finish(id, early.epoch, RunOutcome::Completed(json!(null))).await.expect("finish");
    store.wake(id).await.expect("waking a finished run is a no-op");
    assert_eq!(get(&*store, id).await.state, RunState::Completed);
}

/// A run parked with `wait(None)` on a `Session` checkpoint β€” how a worker suspends a run
/// (spec 3.2 Β§10 S1) β€” holds no lease, keeps its attempt and checkpoint, and is not claimable
/// until woken, however long it waits and across a restart. The claim after `wake` resumes it
/// at the same attempt and the next epoch with the checkpoint; `request_cancel` on a parked
/// run finishes it `Cancelled`.
pub async fn parked_run_keeps_its_checkpoint<H: RunStoreHarness + ?Sized>(harness: &H) {
    let clock = ManualClock::new(base_time());
    let store = harness.open(Arc::new(clock.clone())).await;
    let queue = unique_unicode("queue");
    let checkpoint = RunCheckpointRef::Session {
        session_id: "session/ΓΌ:1".into(),
        invocation_id: Some("invocation:ΓΌ/1".into()),
    };
    let run = created(&*store, new_run(&queue)).await;
    let id = run.run_id.as_str();

    let claimed = claim(&*store, &queue, "w1").await.expect("claim");
    store.set_checkpoint(id, claimed.epoch, checkpoint.clone()).await.expect("checkpoint");
    store.wait(id, claimed.epoch, None).await.expect("park until woken");
    let parked = get(&*store, id).await;
    assert_eq!(
        (
            parked.state,
            parked.attempt,
            parked.lease_owner.as_deref(),
            parked.lease_expires_at,
            parked.checkpoint.as_ref(),
        ),
        (RunState::Waiting, 1, None, None, Some(&checkpoint)),
        "a parked run holds no lease and keeps its checkpoint"
    );
    clock.advance(Duration::from_secs(24 * 3600));
    assert!(claim(&*store, &queue, "w2").await.is_none(), "parked until woken");

    let store =
        if harness.is_durable() { harness.open(Arc::new(clock.clone())).await } else { store };
    let reopened = get(&*store, id).await;
    assert_eq!(
        (reopened.state, reopened.lease_owner.as_deref(), reopened.checkpoint.as_ref()),
        (RunState::Waiting, None, Some(&checkpoint)),
        "parked across a restart"
    );
    assert!(claim(&*store, &queue, "w2").await.is_none(), "still parked after a restart");

    store.wake(id).await.expect("wake");
    let resumed = claim(&*store, &queue, "w2").await.expect("claimable once woken");
    assert_eq!(
        (resumed.record.attempt, resumed.epoch, resumed.record.checkpoint.as_ref()),
        (1, claimed.epoch + 1, Some(&checkpoint)),
        "resumed at the same attempt with its checkpoint"
    );

    store.wait(id, resumed.epoch, None).await.expect("park again");
    store.request_cancel(id).await.expect("request_cancel");
    let cancelled = get(&*store, id).await;
    assert_eq!((cancelled.state, cancelled.lease_owner), (RunState::Cancelled, None));
    store.wake(id).await.expect("waking a cancelled run is a no-op");
    assert!(claim(&*store, &queue, "w3").await.is_none(), "a cancelled run is not claimed");
}

/// Runs parked with `wait(None)` on their `Session` checkpoints β€” how a worker parks the runs
/// a freeze stops (spec 3.2 Β§10 S3) β€” never expire: after a month, none is claimable. `wake`
/// resumes exactly the runs it names, each at its same attempt and next epoch with its
/// checkpoint, and leaves the others parked, however long; `request_cancel` finishes a parked
/// run `Cancelled`, and a later wake does not revive it.
pub async fn freeze_parked_runs_wake_only_when_woken<H: RunStoreHarness + ?Sized>(harness: &H) {
    let clock = ManualClock::new(base_time());
    let store = harness.open(Arc::new(clock.clone())).await;
    let queue = unique_unicode("queue");

    let mut parked = Vec::new();
    for index in 0..3 {
        let checkpoint = RunCheckpointRef::Session {
            session_id: format!("session/ΓΌ:{index}"),
            invocation_id: Some(format!("invocation:ΓΌ/{index}")),
        };
        let run = created(&*store, new_run(&queue)).await;
        let claimed = claim(&*store, &queue, "w1").await.expect("claim");
        assert_eq!(claimed.record.run_id, run.run_id, "claimed oldest first");
        store
            .set_checkpoint(&run.run_id, claimed.epoch, checkpoint.clone())
            .await
            .expect("checkpoint");
        store.wait(&run.run_id, claimed.epoch, None).await.expect("park until woken");
        parked.push((run.run_id, claimed.epoch, checkpoint));
        clock.advance(Duration::from_secs(1));
    }
    for (run_id, _, checkpoint) in &parked {
        let record = get(&*store, run_id).await;
        assert_eq!(
            (record.state, record.lease_owner.as_deref(), record.checkpoint.as_ref()),
            (RunState::Waiting, None, Some(checkpoint)),
            "parked without a lease, on its checkpoint"
        );
    }
    clock.advance(Duration::from_secs(30 * 24 * 3600));
    assert!(claim(&*store, &queue, "w2").await.is_none(), "a freeze park never expires");

    let store =
        if harness.is_durable() { harness.open(Arc::new(clock.clone())).await } else { store };
    store.wake(&parked[0].0).await.expect("wake");
    store.wake(&parked[1].0).await.expect("wake");
    let mut resumed = Vec::new();
    for _ in 0..2 {
        let claimed = claim(&*store, &queue, "w2").await.expect("a woken run is claimable");
        let (_, epoch, checkpoint) =
            parked.iter().find(|(id, _, _)| *id == claimed.record.run_id).expect("a woken run");
        assert_eq!(
            (claimed.record.attempt, claimed.epoch, claimed.record.checkpoint.as_ref()),
            (1, epoch + 1, Some(checkpoint)),
            "resumed at the same attempt with its checkpoint"
        );
        resumed.push((claimed.record.run_id, claimed.epoch));
    }
    let mut resumed_ids: Vec<_> = resumed.iter().map(|(id, _)| id.clone()).collect();
    resumed_ids.sort();
    let mut woken = vec![parked[0].0.clone(), parked[1].0.clone()];
    woken.sort();
    assert_eq!(resumed_ids, woken, "exactly the woken runs resume");
    assert!(claim(&*store, &queue, "w2").await.is_none(), "the third run stays parked");
    // Finished before the clock moves, so their leases never expire into a recovery.
    for (run_id, epoch) in &resumed {
        store.finish(run_id, *epoch, RunOutcome::Completed(json!(null))).await.expect("finish");
    }
    clock.advance(Duration::from_secs(24 * 3600));
    assert!(claim(&*store, &queue, "w2").await.is_none(), "still parked a day later");

    let (third, _, _) = &parked[2];
    store.request_cancel(third).await.expect("request_cancel");
    let cancelled = get(&*store, third).await;
    assert_eq!((cancelled.state, cancelled.lease_owner), (RunState::Cancelled, None));
    store.wake(third).await.expect("waking a cancelled run is a no-op");
    assert!(claim(&*store, &queue, "w3").await.is_none(), "a cancelled run is not claimed");
    assert_eq!(get(&*store, third).await.state, RunState::Cancelled);
}

/// Cancelling a running run is reported by its next heartbeat and blocks a retry;
/// queued and waiting runs cancel at once; a finished run is unchanged.
pub async fn cancel_is_visible_through_heartbeat<H: RunStoreHarness + ?Sized>(harness: &H) {
    let clock = ManualClock::new(base_time());
    let store = harness.open(Arc::new(clock.clone())).await;
    let queue = unique_unicode("queue");

    let running = created(&*store, new_run(&queue)).await;
    let claimed = claim(&*store, &queue, "w1").await.expect("claim");
    assert!(!store.heartbeat(&running.run_id, claimed.epoch, TTL).await.expect("heartbeat"));
    store.request_cancel(&running.run_id).await.expect("request_cancel");
    assert!(store.heartbeat(&running.run_id, claimed.epoch, TTL).await.expect("heartbeat"));
    let failed = store
        .finish(
            &running.run_id,
            claimed.epoch,
            RunOutcome::Failed { error: "stopped".into(), retryable: true, backoff: None },
        )
        .await
        .expect("finish");
    assert_eq!(failed.state, RunState::Failed, "a cancelled run is not retried");

    let queued = created(&*store, new_run(&queue)).await;
    store.request_cancel(&queued.run_id).await.expect("request_cancel");
    assert_eq!(get(&*store, &queued.run_id).await.state, RunState::Cancelled);
    assert!(claim(&*store, &queue, "w2").await.is_none(), "a cancelled run is not claimed");

    let waiting = created(&*store, new_run(&queue)).await;
    let claimed = claim(&*store, &queue, "w2").await.expect("claim");
    store.wait(&waiting.run_id, claimed.epoch, None).await.expect("wait");
    store.request_cancel(&waiting.run_id).await.expect("request_cancel");
    assert_eq!(get(&*store, &waiting.run_id).await.state, RunState::Cancelled);

    let finished = get(&*store, &running.run_id).await;
    store.request_cancel(&running.run_id).await.expect("cancelling a finished run is a no-op");
    assert_eq!(get(&*store, &running.run_id).await, finished);
}

/// Prune deletes finished runs last changed before the cut-off and keeps newer and
/// unfinished ones; a pruned run's idempotency key creates a new run.
pub async fn prune_removes_old_finished_runs<H: RunStoreHarness + ?Sized>(harness: &H) {
    // Long before every other case's base time, so the cut-off never reaches their runs.
    let start = Utc.with_ymd_and_hms(2001, 1, 1, 0, 0, 0).single().expect("valid instant");
    let clock = ManualClock::new(start);
    let store = harness.open(Arc::new(clock.clone())).await;
    let queue = unique_unicode("queue");
    let key = unique_unicode("idempotency");

    let old = created(&*store, new_run(&queue).with_idempotency_key(&key)).await;
    let claimed = claim(&*store, &queue, "w1").await.expect("claim");
    store
        .finish(&old.run_id, claimed.epoch, RunOutcome::Completed(json!(1)))
        .await
        .expect("finish");
    let unfinished = created(&*store, new_run(&queue)).await;

    clock.advance(Duration::from_secs(3600));
    let cutoff: DateTime<Utc> = clock.now();
    let recent = created(&*store, new_run(&queue)).await;
    store.request_cancel(&recent.run_id).await.expect("cancel");

    let pruned = store.prune(cutoff).await.expect("prune");
    assert!(pruned >= 1, "the old finished run is pruned");
    assert_eq!(store.get(&old.run_id).await.expect("get"), None);
    assert!(store.get(&unfinished.run_id).await.expect("get").is_some(), "unfinished runs stay");
    assert!(store.get(&recent.run_id).await.expect("get").is_some(), "newer finished runs stay");

    let again = store.enqueue(new_run(&queue).with_idempotency_key(&key)).await.expect("enqueue");
    assert!(matches!(again, Enqueued::Created(_)), "the pruned run's key is free: {again:?}");
}

/// Runs, leases, and checkpoints written before a restart read back after it, the old
/// epoch still works, and a stale one is still refused.
pub async fn runs_survive_a_restart<H: RunStoreHarness + ?Sized>(harness: &H) {
    if !harness.is_durable() {
        return;
    }
    let clock = ManualClock::new(base_time());
    let queue = unique_unicode("queue");
    let checkpoint = RunCheckpointRef::Graph {
        thread_id: "thread/ΓΌ".into(),
        checkpoint_id: Some("c:1".into()),
    };
    let (record, claimed) = {
        let before = harness.open(Arc::new(clock.clone())).await;
        let record = created(
            &*before,
            new_run(&queue).with_idempotency_key("restart").with_authenticated_user(),
        )
        .await;
        let claimed = claim(&*before, &queue, "w1").await.expect("claim");
        before
            .set_checkpoint(&record.run_id, claimed.epoch, checkpoint.clone())
            .await
            .expect("checkpoint");
        (record, claimed)
    };

    let after = harness.open(Arc::new(clock.clone())).await;
    let read = get(&*after, &record.run_id).await;
    assert_eq!(read.checkpoint, Some(checkpoint));
    assert!(read.authenticated, "the authenticated flag survives a restart");
    assert_eq!((read.state, read.lease_epoch), (RunState::Running, claimed.epoch));
    let existing =
        after.enqueue(new_run(&queue).with_idempotency_key("restart")).await.expect("enqueue");
    assert_eq!(existing.record().map(|r| r.run_id.as_str()), Some(record.run_id.as_str()));
    assert_code(
        after.heartbeat(&record.run_id, claimed.epoch + 1, TTL).await,
        RUN_STALE_EPOCH_CODE,
        "fencing holds after a restart",
    );
    after.heartbeat(&record.run_id, claimed.epoch, TTL).await.expect("the epoch survives");
}