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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
//! Durable runs: a queue of agent or workflow runs that workers claim under fenced leases.
//!
//! A [`DurableRunStore`] holds the durable truth about every run: its input, state,
//! attempt count, lease, checkpoint, and outcome. Workers [`claim`](DurableRunStore::claim)
//! runs, [`heartbeat`](DurableRunStore::heartbeat) while they work, and
//! [`finish`](DurableRunStore::finish) them. Every worker write carries the run's lease
//! epoch, and a store refuses a write whose epoch is not current with
//! `run_store.stale_epoch`, so a worker that stalled and lost its run can never
//! overwrite the work of the worker that recovered it.
//!
//! # Lifecycle
//!
//! | From | Event | To | Attempt | Epoch |
//! |------|-------|----|---------|-------|
//! | — | `enqueue` | `Queued` | 0 | 0 |
//! | `Queued` | `claim` (`not_before` passed, no earlier run holds its concurrency key) | `Running` | + 1 | + 1 |
//! | `Running` | `claim` after the lease expired (recovery) | `Running` | + 1 | + 1 |
//! | `Running` | `wait` | `Waiting` (lease released) | — | — |
//! | `Waiting` | `claim` once the wait's `until` passed or `wake` was called | `Running` | — | + 1 |
//! | `Running` | `finish(Completed)` | `Completed` | — | — |
//! | `Running` | `finish(Failed { retryable: true, backoff })` with attempts left and no cancel request | `Queued`, claimable once `backoff` has passed | — | — |
//! | `Running` | `finish(Failed)` otherwise | `Failed` | — | — |
//! | `Running` | `finish(Cancelled)` | `Cancelled` | — | — |
//! | `Queued`, `Waiting` | `request_cancel` | `Cancelled` | — | — |
//! | `Running` | `request_cancel` | `Running`, `cancel_requested` (seen by the next heartbeat) | — | — |
//!
//! A recovery claim of a run that already used `max_attempts` attempts fails the run
//! with an `attempts exhausted` error instead of claiming it.
//!
//! A `wake` that arrives while the run is still `Running` (an approval decided before
//! the worker parked the run) is kept in `wake_pending`, and the next `wait` parks the
//! run as already woken, so the wake is never lost.
//!
//! # Double texting
//!
//! Runs that share a `concurrency_key` in one queue run one at a time, in enqueue
//! order. [`OnConflict`] picks what `enqueue` does when an unfinished run holds the key:
//!
//! | Policy | Effect |
//! |--------|--------|
//! | [`Reject`](OnConflict::Reject) | Returns [`Enqueued::Rejected`] naming the oldest unfinished run |
//! | [`Enqueue`](OnConflict::Enqueue) | Queues the run behind the unfinished ones |
//! | [`Interrupt`](OnConflict::Interrupt) | Cancels queued and waiting runs, requests cancellation of running ones, and queues the run behind them |
//!
//! # Backend primitives
//!
//! | Operation | SQL | Redis | MongoDB | Neo4j, Firestore |
//! |-----------|-----|-------|---------|------------------|
//! | Idempotent `enqueue` | Unique index on `(queue, idempotency_key)`, `INSERT ... ON CONFLICT DO NOTHING` then read | Lua: check the key index, then write | Unique index, `insert_one`, read on duplicate-key error | Transaction over the key node or document |
//! | Single-winner `claim` | `SELECT ... FOR UPDATE SKIP LOCKED` (PostgreSQL) or `BEGIN IMMEDIATE` (SQLite), then `UPDATE` | One Lua script per claim | `findOneAndUpdate` on state and epoch | Transaction that reads the candidate and writes the new epoch |
//! | Fenced writes | `UPDATE ... WHERE run_id = $1 AND lease_epoch = $2 AND state = 'running'` | Lua compare of the epoch field | `updateOne` filtered on `leaseEpoch` | Transaction that checks the epoch |
//! | `prune` | `DELETE ... WHERE state IN (...) AND updated_at < $1` | Sorted set of finished runs by time | `deleteMany` | Batched transactional deletes |

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

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::sync::Mutex;

use crate::lease::deadline;
use crate::principal::ActingContext;
use crate::{AdkError, Clock, ErrorCategory, ErrorComponent, Result, SystemClock};

/// Error code returned when a worker write carries an epoch that is not current.
pub const RUN_STALE_EPOCH_CODE: &str = "run_store.stale_epoch";
/// Error code returned for an unknown run identifier.
pub const RUN_NOT_FOUND_CODE: &str = "run_store.not_found";
/// Error code returned for a [`NewRun`] that fails validation.
pub const INVALID_RUN_CODE: &str = "run_store.invalid_run";

/// Where a run is in its lifecycle.
///
/// # Example
///
/// ```rust
/// use adk_core::run_store::RunState;
///
/// assert!(RunState::Completed.is_terminal());
/// assert!(!RunState::Waiting.is_terminal());
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum RunState {
    /// Waiting for a worker to claim it.
    Queued,
    /// Claimed by a worker under a lease.
    Running,
    /// Parked until a timer passes or `wake` is called; holds no lease.
    Waiting,
    /// Finished with a result.
    Completed,
    /// Finished with an error.
    Failed,
    /// Cancelled before or during execution.
    Cancelled,
}

impl RunState {
    /// Whether the run has finished and never changes state again.
    pub fn is_terminal(self) -> bool {
        matches!(self, Self::Completed | Self::Failed | Self::Cancelled)
    }
}

/// Where a run's progress is checkpointed, so a recovering worker resumes it.
///
/// # Example
///
/// ```rust
/// use adk_core::run_store::RunCheckpointRef;
///
/// let checkpoint = RunCheckpointRef::Graph { thread_id: "t1".into(), checkpoint_id: Some("c7".into()) };
/// assert_eq!(serde_json::to_value(&checkpoint).unwrap()["kind"], "graph");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
#[non_exhaustive]
pub enum RunCheckpointRef {
    /// A session, optionally at one invocation.
    Session {
        /// The session identifier.
        session_id: String,
        /// The last invocation recorded in the session.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        invocation_id: Option<String>,
    },
    /// A session whose run is parked until tool calls held for approval are decided.
    ///
    /// A worker records it before [`wait`](DurableRunStore::wait)ing; the claim that
    /// follows a [`wake`](DurableRunStore::wake) resumes the same session, and the agent
    /// dispatches each held call that has a decision.
    AwaitingApproval {
        /// The session identifier.
        session_id: String,
        /// The invocation that held the calls.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        invocation_id: Option<String>,
        /// The fingerprints of the held calls, the keys their decisions are recorded under.
        fingerprints: Vec<String>,
        /// When the hold expires; `None` waits until woken.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        expires_at: Option<DateTime<Utc>>,
    },
    /// A graph thread, optionally at one checkpoint.
    Graph {
        /// The graph thread identifier.
        thread_id: String,
        /// The checkpoint within the thread.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        checkpoint_id: Option<String>,
    },
}

/// What `enqueue` does when an unfinished run holds the same concurrency key.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum OnConflict {
    /// Refuse the new run.
    Reject,
    /// Queue the new run behind the unfinished ones.
    #[default]
    Enqueue,
    /// Cancel the unfinished runs and queue the new one behind them.
    Interrupt,
}

/// A run to enqueue.
///
/// # Example
///
/// ```rust
/// use adk_core::run_store::{NewRun, OnConflict};
/// use serde_json::json;
///
/// let run = NewRun::new("default", "billing", "user-1", "invoice_agent", json!({"text": "hi"}))
///     .with_idempotency_key("webhook:evt_123")
///     .with_concurrency_key("thread:42", OnConflict::Interrupt)
///     .with_max_attempts(5);
/// assert!(run.validate().is_ok());
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NewRun {
    /// The queue workers claim from.
    pub queue: String,
    /// The application the run belongs to.
    pub app_name: String,
    /// The user the run belongs to.
    pub user_id: String,
    /// The session the run continues, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
    /// The agent or workflow name to run.
    pub target: String,
    /// The run's input.
    pub input: Value,
    /// Unique per queue: enqueueing an existing key returns the existing run until it is pruned.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub idempotency_key: Option<String>,
    /// Runs sharing a key in one queue run one at a time.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub concurrency_key: Option<String>,
    /// What happens when an unfinished run holds `concurrency_key`.
    #[serde(default)]
    pub on_conflict: OnConflict,
    /// The run is not claimed before this time.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub not_before: Option<DateTime<Utc>>,
    /// The most attempts the run may take, counting recoveries; at least 1.
    pub max_attempts: u32,
    /// The agent identity the run acts under.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub principal: Option<ActingContext>,
    /// Whether `user_id` is an identity a server authenticated when the run was enqueued, so
    /// the run executes as an authenticated user (spec 3.1 §9.2 I6). Set it with
    /// [`with_authenticated_user`](Self::with_authenticated_user).
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub authenticated: bool,
}

impl NewRun {
    /// The `max_attempts` a run gets from [`NewRun::new`].
    pub const DEFAULT_MAX_ATTEMPTS: u32 = 3;

    /// Creates a run with no keys, no delay, no principal, [`OnConflict::Enqueue`], and
    /// [`DEFAULT_MAX_ATTEMPTS`](Self::DEFAULT_MAX_ATTEMPTS).
    pub fn new(
        queue: impl Into<String>,
        app_name: impl Into<String>,
        user_id: impl Into<String>,
        target: impl Into<String>,
        input: Value,
    ) -> Self {
        Self {
            queue: queue.into(),
            app_name: app_name.into(),
            user_id: user_id.into(),
            session_id: None,
            target: target.into(),
            input,
            idempotency_key: None,
            concurrency_key: None,
            on_conflict: OnConflict::Enqueue,
            not_before: None,
            max_attempts: Self::DEFAULT_MAX_ATTEMPTS,
            principal: None,
            authenticated: false,
        }
    }

    /// Sets the session the run continues.
    pub fn with_session_id(mut self, session_id: impl Into<String>) -> Self {
        self.session_id = Some(session_id.into());
        self
    }

    /// Sets the idempotency key.
    pub fn with_idempotency_key(mut self, key: impl Into<String>) -> Self {
        self.idempotency_key = Some(key.into());
        self
    }

    /// Sets the concurrency key and the policy applied when it is held.
    pub fn with_concurrency_key(mut self, key: impl Into<String>, on_conflict: OnConflict) -> Self {
        self.concurrency_key = Some(key.into());
        self.on_conflict = on_conflict;
        self
    }

    /// Delays the run until `at`.
    pub fn with_not_before(mut self, at: DateTime<Utc>) -> Self {
        self.not_before = Some(at);
        self
    }

    /// Sets the most attempts the run may take.
    pub fn with_max_attempts(mut self, max_attempts: u32) -> Self {
        self.max_attempts = max_attempts;
        self
    }

    /// Sets the agent identity the run acts under.
    ///
    /// A worker honours a stored principal only when it may act as the principal's agent
    /// and the principal's subject is the run's user (or a delegation rule allows it);
    /// see `DurableWorker` in `adk-server`.
    pub fn with_principal(mut self, principal: ActingContext) -> Self {
        self.principal = Some(principal);
        self
    }

    /// Marks the run as created by a service rather than a person: it acts for its
    /// `user_id` as a [`SubjectKind::Service`](crate::SubjectKind::Service).
    ///
    /// A run with no principal gets the derived principal of its target acting for the
    /// service; a principal that names no subject gains the service as its subject; a
    /// principal that names one is kept. Schedulers, webhook ingress, and queue ingress
    /// mark the runs they create.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_core::{AgentPrincipal, NewRun, SubjectRef};
    /// use serde_json::json;
    ///
    /// let run = NewRun::new("default", "ops", "nightly-close", "closer", json!({}))
    ///     .with_service_subject();
    /// let principal = run.principal.unwrap();
    /// assert_eq!(principal.agent, AgentPrincipal::derived("ops", "closer"));
    /// assert_eq!(principal.on_behalf_of, Some(SubjectRef::service("nightly-close")));
    /// ```
    #[must_use]
    pub fn with_service_subject(mut self) -> Self {
        let subject = crate::SubjectRef::service(&self.user_id);
        match &mut self.principal {
            Some(principal) => {
                principal.on_behalf_of.get_or_insert(subject);
            }
            None => {
                self.principal = Some(
                    ActingContext::new(crate::AgentPrincipal::derived(
                        &self.app_name,
                        &self.target,
                    ))
                    .on_behalf_of(subject),
                );
            }
        }
        self
    }

    /// Marks `user_id` as an identity a server authenticated when it enqueued the run, so a
    /// worker runs it as an authenticated user: its tools see
    /// [`is_authenticated`](crate::ReadonlyContext::is_authenticated) answer `true`, as an
    /// interactive run on that server does (spec 3.1 §9.2 I6). `adk-server` marks the runs and
    /// cron jobs an authenticated caller submits; only code that authenticated the user
    /// should call it.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_core::NewRun;
    /// use serde_json::json;
    ///
    /// let run = NewRun::new("default", "ops", "alice", "closer", json!({})).with_authenticated_user();
    /// assert!(run.authenticated);
    /// ```
    #[must_use]
    pub fn with_authenticated_user(mut self) -> Self {
        self.authenticated = true;
        self
    }

    /// Checks the fields every store requires.
    ///
    /// # Errors
    ///
    /// Returns [`INVALID_RUN_CODE`] when `queue`, `app_name`, `user_id`, or `target` is
    /// empty, a key is present but empty, or `max_attempts` is zero.
    pub fn validate(&self) -> Result<()> {
        let empty = [
            ("queue", self.queue.is_empty()),
            ("appName", self.app_name.is_empty()),
            ("userId", self.user_id.is_empty()),
            ("target", self.target.is_empty()),
            ("idempotencyKey", self.idempotency_key.as_deref() == Some("")),
            ("concurrencyKey", self.concurrency_key.as_deref() == Some("")),
        ];
        if let Some((field, _)) = empty.iter().find(|(_, is_empty)| *is_empty) {
            return Err(invalid_run_error(&format!("{field} must not be empty")));
        }
        if self.max_attempts == 0 {
            return Err(invalid_run_error("maxAttempts must be at least 1"));
        }
        Ok(())
    }
}

/// A run as the store holds it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RunRecord {
    /// The store-assigned run identifier.
    pub run_id: String,
    /// The queue workers claim from.
    pub queue: String,
    /// The application the run belongs to.
    pub app_name: String,
    /// The user the run belongs to.
    pub user_id: String,
    /// The session the run continues, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
    /// The agent or workflow name to run.
    pub target: String,
    /// The run's input.
    pub input: Value,
    /// The idempotency key the run was enqueued with.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub idempotency_key: Option<String>,
    /// The concurrency key the run was enqueued with.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub concurrency_key: Option<String>,
    /// The double-texting policy the run was enqueued with.
    #[serde(default)]
    pub on_conflict: OnConflict,
    /// Not claimed before this time; while `Waiting`, the time the wait ends.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub not_before: Option<DateTime<Utc>>,
    /// The most attempts the run may take.
    pub max_attempts: u32,
    /// The agent identity the run acts under.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub principal: Option<ActingContext>,
    /// Whether `user_id` was authenticated when the run was enqueued; see
    /// [`NewRun::authenticated`].
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub authenticated: bool,
    /// Where the run is in its lifecycle.
    pub state: RunState,
    /// Attempts started so far, counting recoveries; 0 until first claimed.
    pub attempt: u32,
    /// The worker holding the lease while `Running`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lease_owner: Option<String>,
    /// The fencing epoch of the latest claim; 0 until first claimed.
    pub lease_epoch: u64,
    /// When the current lease lapses.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lease_expires_at: Option<DateTime<Utc>>,
    /// The last claim or heartbeat.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub heartbeat_at: Option<DateTime<Utc>>,
    /// Where the run's progress is checkpointed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub checkpoint: Option<RunCheckpointRef>,
    /// Whether cancellation was requested while the run was `Running`.
    #[serde(default)]
    pub cancel_requested: bool,
    /// Whether `wake` arrived while the run was `Running`, before it parked.
    #[serde(default)]
    pub wake_pending: bool,
    /// The result of a completed run.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub result: Option<Value>,
    /// The error of the latest failed attempt.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    /// When the run was enqueued.
    pub created_at: DateTime<Utc>,
    /// When the run last changed; for a finished run, when it finished.
    pub updated_at: DateTime<Utc>,
    /// Increases by one on every change.
    pub revision: u64,
}

impl RunRecord {
    /// Builds the record a store writes for a newly enqueued run.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_core::run_store::{NewRun, RunRecord, RunState};
    ///
    /// let run = NewRun::new("q", "app", "user", "agent", serde_json::Value::Null);
    /// let record = RunRecord::queued("run-1", run, chrono::Utc::now());
    /// assert_eq!((record.state, record.attempt, record.lease_epoch, record.revision), (RunState::Queued, 0, 0, 1));
    /// ```
    pub fn queued(run_id: impl Into<String>, run: NewRun, now: DateTime<Utc>) -> Self {
        Self {
            run_id: run_id.into(),
            queue: run.queue,
            app_name: run.app_name,
            user_id: run.user_id,
            session_id: run.session_id,
            target: run.target,
            input: run.input,
            idempotency_key: run.idempotency_key,
            concurrency_key: run.concurrency_key,
            on_conflict: run.on_conflict,
            not_before: run.not_before,
            max_attempts: run.max_attempts,
            principal: run.principal,
            authenticated: run.authenticated,
            state: RunState::Queued,
            attempt: 0,
            lease_owner: None,
            lease_epoch: 0,
            lease_expires_at: None,
            heartbeat_at: None,
            checkpoint: None,
            cancel_requested: false,
            wake_pending: false,
            result: None,
            error: None,
            created_at: now,
            updated_at: now,
            revision: 1,
        }
    }
}

/// The result of [`DurableRunStore::enqueue`].
#[derive(Debug, Clone, PartialEq)]
pub enum Enqueued {
    /// A new run was created.
    Created(RunRecord),
    /// A run with the same idempotency key already exists in the queue.
    Existing(RunRecord),
    /// [`OnConflict::Reject`] refused the run because an unfinished run holds its concurrency key.
    Rejected {
        /// The oldest unfinished run holding the key.
        blocking_run_id: String,
    },
}

impl Enqueued {
    /// Returns the created or existing record, or `None` when rejected.
    pub fn record(&self) -> Option<&RunRecord> {
        match self {
            Self::Created(record) | Self::Existing(record) => Some(record),
            Self::Rejected { .. } => None,
        }
    }
}

/// A run a worker claimed, with the epoch its writes must carry.
#[derive(Debug, Clone, PartialEq)]
pub struct ClaimedRun {
    /// The run after the claim.
    pub record: RunRecord,
    /// The fencing epoch of this claim.
    pub epoch: u64,
}

/// How a worker ends an attempt.
#[derive(Debug, Clone, PartialEq)]
pub enum RunOutcome {
    /// The run finished with a result.
    Completed(Value),
    /// The attempt failed; `retryable` failures requeue while attempts remain.
    Failed {
        /// What went wrong.
        error: String,
        /// Whether another attempt may succeed.
        retryable: bool,
        /// How long a requeued run waits before its next attempt, as its `not_before`;
        /// `None` makes it claimable at once. [`RetryBackoff::delay`] computes one.
        backoff: Option<Duration>,
    },
    /// The run stopped after a cancellation request.
    Cancelled,
}

/// Exponential backoff with jitter between the attempts of a run whose failure is retryable,
/// so a provider that refuses with `429` or `503` is not asked again at once.
///
/// | Input | Wait before the next attempt |
/// |-------|------------------------------|
/// | Attempt `n` failed | `initial × multiplier^(n−1)`, capped at `max`, then a random part of its upper half dropped, so runs that failed together retry apart |
/// | The error carries a retry hint (`Retry-After`) | At least the hint, still capped at `max` |
///
/// The defaults wait about 5, 10 and 20 seconds after the first three attempts, never
/// longer than 10 minutes. [`none`](Self::none) retries at once.
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
/// use adk_core::run_store::RetryBackoff;
///
/// let backoff = RetryBackoff::new(Duration::from_secs(2), Duration::from_secs(60));
/// let first = backoff.delay(1, None);
/// assert!(first >= Duration::from_secs(1) && first <= Duration::from_secs(2));
/// // A provider that asks for 30 seconds gets them.
/// assert!(backoff.delay(1, Some(Duration::from_secs(30))) >= Duration::from_secs(30));
/// assert_eq!(RetryBackoff::none().delay(3, None), Duration::ZERO);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct RetryBackoff {
    /// The wait after the first attempt fails, before jitter.
    pub initial: Duration,
    /// The longest wait, retry hints included.
    pub max: Duration,
    /// What each further attempt multiplies the wait by.
    pub multiplier: f64,
}

impl Default for RetryBackoff {
    fn default() -> Self {
        Self::new(Duration::from_secs(5), Duration::from_secs(600))
    }
}

impl RetryBackoff {
    /// Waits from `initial`, doubling per attempt, at most `max`.
    pub fn new(initial: Duration, max: Duration) -> Self {
        Self { initial, max, multiplier: 2.0 }
    }

    /// No wait: a retryable failure is claimable again at once.
    pub fn none() -> Self {
        Self::new(Duration::ZERO, Duration::ZERO)
    }

    /// Sets what each further attempt multiplies the wait by.
    #[must_use]
    pub fn with_multiplier(mut self, multiplier: f64) -> Self {
        self.multiplier = multiplier;
        self
    }

    /// The wait before the attempt after `attempt` (1-based) failed, with the error's
    /// `retry_after` hint when it has one.
    pub fn delay(&self, attempt: u32, retry_after: Option<Duration>) -> Duration {
        let exponent = i32::try_from(attempt.saturating_sub(1)).unwrap_or(i32::MAX);
        let base = self.initial.as_secs_f64() * self.multiplier.max(1.0).powi(exponent);
        let base = base.min(self.max.as_secs_f64());
        let jittered = Duration::from_secs_f64(base * (0.5 + 0.5 * unit_random()));
        jittered.max(retry_after.unwrap_or_default()).min(self.max)
    }
}

/// A random number in `[0, 1)`, from the standard library's randomly keyed hasher.
fn unit_random() -> f64 {
    use std::hash::{BuildHasher, Hasher};
    let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
    hasher.write_u128(
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |elapsed| elapsed.as_nanos()),
    );
    (hasher.finish() >> 11) as f64 / (1_u64 << 53) as f64
}

/// Selects runs for [`DurableRunStore::list`]. Every set field must match.
///
/// # Example
///
/// ```rust
/// use adk_core::run_store::{RunFilter, RunState};
///
/// let filter = RunFilter::default().queue("default").states([RunState::Queued, RunState::Running]).limit(50);
/// assert_eq!(filter.limit, Some(50));
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct RunFilter {
    /// Only runs in this queue.
    pub queue: Option<String>,
    /// Only runs in one of these states; empty matches every state.
    pub states: Vec<RunState>,
    /// Only runs of this application.
    pub app_name: Option<String>,
    /// Only runs of this user.
    pub user_id: Option<String>,
    /// Only runs continuing this session.
    pub session_id: Option<String>,
    /// Only runs holding this concurrency key.
    pub concurrency_key: Option<String>,
    /// At most this many runs, oldest first.
    pub limit: Option<usize>,
}

impl RunFilter {
    /// Only runs in `queue`.
    pub fn queue(mut self, queue: impl Into<String>) -> Self {
        self.queue = Some(queue.into());
        self
    }

    /// Only runs in one of `states`.
    pub fn states(mut self, states: impl IntoIterator<Item = RunState>) -> Self {
        self.states = states.into_iter().collect();
        self
    }

    /// Only runs of `app_name`.
    pub fn app_name(mut self, app_name: impl Into<String>) -> Self {
        self.app_name = Some(app_name.into());
        self
    }

    /// Only runs of `user_id`.
    pub fn user_id(mut self, user_id: impl Into<String>) -> Self {
        self.user_id = Some(user_id.into());
        self
    }

    /// Only runs continuing `session_id`.
    pub fn session_id(mut self, session_id: impl Into<String>) -> Self {
        self.session_id = Some(session_id.into());
        self
    }

    /// Only runs holding `concurrency_key`.
    pub fn concurrency_key(mut self, key: impl Into<String>) -> Self {
        self.concurrency_key = Some(key.into());
        self
    }

    /// At most `limit` runs.
    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Whether `record` matches every set field except `limit`.
    pub fn matches(&self, record: &RunRecord) -> bool {
        fn field(want: &Option<String>, have: Option<&str>) -> bool {
            want.as_deref().is_none_or(|want| have == Some(want))
        }
        field(&self.queue, Some(&record.queue))
            && (self.states.is_empty() || self.states.contains(&record.state))
            && field(&self.app_name, Some(&record.app_name))
            && field(&self.user_id, Some(&record.user_id))
            && field(&self.session_id, record.session_id.as_deref())
            && field(&self.concurrency_key, record.concurrency_key.as_deref())
    }
}

/// The durable queue of runs.
///
/// Named `DurableRunStore` so it does not clash with `adk_server::background::RunStore`,
/// the server's process-local record of background runs.
///
/// Every backend passes `adk_core::run_store_conformance_tests!` (feature `conformance`).
/// The [module documentation](crate::run_store) lists the lifecycle and the primitive
/// each backend uses for each guarantee.
#[async_trait]
pub trait DurableRunStore: Send + Sync {
    /// Adds a run.
    ///
    /// An existing run with the same `(queue, idempotency_key)` is returned as
    /// [`Enqueued::Existing`] before any concurrency check. Concurrent enqueues of one
    /// key create exactly one run.
    ///
    /// # Errors
    ///
    /// Returns [`INVALID_RUN_CODE`] when [`NewRun::validate`] fails.
    async fn enqueue(&self, run: NewRun) -> Result<Enqueued>;

    /// Claims the oldest claimable run in `queue` for `worker` with a lease of `ttl`.
    ///
    /// Claimable, oldest enqueued first: a `Queued` run whose `not_before` passed and
    /// that no earlier unfinished run with its concurrency key precedes; a `Waiting` run
    /// whose wait ended or that was woken; a `Running` run whose lease expired
    /// (recovery). The run's epoch increases by one on every claim. Concurrent claims
    /// never return the same run at the same epoch.
    ///
    /// # Errors
    ///
    /// Returns `run_store.invalid_ttl` for a zero `ttl`.
    async fn claim(&self, queue: &str, worker: &str, ttl: Duration) -> Result<Option<ClaimedRun>>;

    /// Extends the lease to `ttl` from now and returns whether cancellation was requested.
    ///
    /// # Errors
    ///
    /// Returns [`RUN_STALE_EPOCH_CODE`] when the run is not `Running` at `epoch`, and
    /// [`RUN_NOT_FOUND_CODE`] for an unknown run.
    async fn heartbeat(&self, run_id: &str, epoch: u64, ttl: Duration) -> Result<bool>;

    /// Records where the run's progress is checkpointed.
    ///
    /// # Errors
    ///
    /// Same as [`heartbeat`](Self::heartbeat).
    async fn set_checkpoint(
        &self,
        run_id: &str,
        epoch: u64,
        checkpoint: RunCheckpointRef,
    ) -> Result<()>;

    /// Releases the lease and parks the run until `until` passes or [`wake`](Self::wake)
    /// is called. `None` parks it until woken.
    ///
    /// # Errors
    ///
    /// Same as [`heartbeat`](Self::heartbeat).
    async fn wait(&self, run_id: &str, epoch: u64, until: Option<DateTime<Utc>>) -> Result<()>;

    /// Makes a `Waiting` run claimable now; records a pending wake for a `Running` run;
    /// does nothing for a queued or finished run.
    ///
    /// # Errors
    ///
    /// Returns [`RUN_NOT_FOUND_CODE`] for an unknown run.
    async fn wake(&self, run_id: &str) -> Result<()>;

    /// Ends the attempt with `outcome` and returns the updated run.
    ///
    /// `Failed { retryable: true }` requeues the run while `attempt < max_attempts` and
    /// no cancellation was requested; every other outcome is terminal.
    ///
    /// # Errors
    ///
    /// Same as [`heartbeat`](Self::heartbeat).
    async fn finish(&self, run_id: &str, epoch: u64, outcome: RunOutcome) -> Result<RunRecord>;

    /// Cancels a queued or waiting run, or flags a running one for its worker to stop.
    /// A finished run is left unchanged.
    ///
    /// # Errors
    ///
    /// Returns [`RUN_NOT_FOUND_CODE`] for an unknown run.
    async fn request_cancel(&self, run_id: &str) -> Result<()>;

    /// Returns a run, or `None` when it does not exist or was pruned.
    async fn get(&self, run_id: &str) -> Result<Option<RunRecord>>;

    /// Returns the runs matching `filter`, oldest enqueued first.
    async fn list(&self, filter: RunFilter) -> Result<Vec<RunRecord>>;

    /// Deletes finished runs whose `updated_at` is before `finished_before` and returns
    /// how many were deleted. Their idempotency keys become free again.
    async fn prune(&self, finished_before: DateTime<Utc>) -> Result<u64>;
}

/// Builds the error a run store returns when a worker write carries a stale epoch.
///
/// # Example
///
/// ```rust
/// let error = adk_core::run_store::run_stale_epoch_error("run-1", 2);
/// assert_eq!(error.code, adk_core::run_store::RUN_STALE_EPOCH_CODE);
/// ```
pub fn run_stale_epoch_error(run_id: &str, epoch: u64) -> AdkError {
    AdkError::new(
        ErrorComponent::Agent,
        ErrorCategory::Conflict,
        RUN_STALE_EPOCH_CODE,
        format!(
            "run '{run_id}' is no longer held at epoch {epoch}; another worker recovered it or it \
             finished, so stop work on it without writing results"
        ),
    )
}

/// Builds the error a run store returns for an unknown run.
///
/// # Example
///
/// ```rust
/// let error = adk_core::run_store::run_not_found_error("run-1");
/// assert!(error.is_not_found());
/// ```
pub fn run_not_found_error(run_id: &str) -> AdkError {
    AdkError::not_found(
        ErrorComponent::Agent,
        RUN_NOT_FOUND_CODE,
        format!("run store has no run '{run_id}'; it was never enqueued or was pruned"),
    )
}

/// Builds the error a run store returns for an invalid [`NewRun`].
///
/// # Example
///
/// ```rust
/// let error = adk_core::run_store::invalid_run_error("queue must not be empty");
/// assert_eq!(error.code, adk_core::run_store::INVALID_RUN_CODE);
/// ```
pub fn invalid_run_error(reason: &str) -> AdkError {
    AdkError::new(
        ErrorComponent::Agent,
        ErrorCategory::InvalidInput,
        INVALID_RUN_CODE,
        format!("cannot enqueue run: {reason}"),
    )
}

#[derive(Debug)]
struct RunEntry {
    record: RunRecord,
    seq: u64,
}

impl RunEntry {
    fn order(&self) -> (DateTime<Utc>, u64) {
        (self.record.created_at, self.seq)
    }
}

#[derive(Debug, Default)]
struct RunTable {
    runs: HashMap<String, RunEntry>,
    by_idempotency_key: HashMap<(String, String), String>,
    next_seq: u64,
}

impl RunTable {
    /// Unfinished runs in `queue` holding `key`, oldest first.
    fn holders(&self, queue: &str, key: &str) -> Vec<(String, (DateTime<Utc>, u64))> {
        let mut holders: Vec<_> = self
            .runs
            .values()
            .filter(|entry| {
                entry.record.queue == queue
                    && entry.record.concurrency_key.as_deref() == Some(key)
                    && !entry.record.state.is_terminal()
            })
            .map(|entry| (entry.record.run_id.clone(), entry.order()))
            .collect();
        holders.sort_by_key(|(_, order)| *order);
        holders
    }

    fn fenced(&mut self, run_id: &str, epoch: u64) -> Result<&mut RunRecord> {
        let entry = self.runs.get_mut(run_id).ok_or_else(|| run_not_found_error(run_id))?;
        if entry.record.state != RunState::Running || entry.record.lease_epoch != epoch {
            return Err(run_stale_epoch_error(run_id, epoch));
        }
        Ok(&mut entry.record)
    }
}

fn touch(record: &mut RunRecord, now: DateTime<Utc>) {
    record.updated_at = now;
    record.revision += 1;
}

fn release_lease(record: &mut RunRecord) {
    record.lease_owner = None;
    record.lease_expires_at = None;
}

/// Process-local [`DurableRunStore`].
///
/// Runs do not survive a restart. Use it for tests and single-process deployments;
/// use a durable backend when runs must outlive the process.
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
/// use adk_core::run_store::{DurableRunStore, InMemoryRunStore, NewRun, RunOutcome, RunState};
/// use serde_json::json;
///
/// # futures::executor::block_on(async {
/// let store = InMemoryRunStore::new();
/// store.enqueue(NewRun::new("default", "app", "user", "agent", json!({}))).await?;
/// let claimed = store.claim("default", "worker-1", Duration::from_secs(30)).await?.unwrap();
/// let done = store.finish(&claimed.record.run_id, claimed.epoch, RunOutcome::Completed(json!("ok"))).await?;
/// assert_eq!(done.state, RunState::Completed);
/// # Ok::<(), adk_core::AdkError>(())
/// # }).unwrap();
/// ```
#[derive(Debug)]
pub struct InMemoryRunStore {
    table: Mutex<RunTable>,
    clock: Arc<dyn Clock>,
}

impl Default for InMemoryRunStore {
    fn default() -> Self {
        Self::new()
    }
}

impl InMemoryRunStore {
    /// Creates an empty store that reads the system clock.
    pub fn new() -> Self {
        Self { table: Mutex::new(RunTable::default()), clock: Arc::new(SystemClock) }
    }

    /// Sets the clock that decides delays, waits, and lease expiry.
    pub fn with_clock(mut self, clock: Arc<dyn Clock>) -> Self {
        self.clock = clock;
        self
    }
}

#[async_trait]
impl DurableRunStore for InMemoryRunStore {
    async fn enqueue(&self, run: NewRun) -> Result<Enqueued> {
        run.validate()?;
        let now = self.clock.now();
        let mut table = self.table.lock().await;
        if let Some(key) = &run.idempotency_key
            && let Some(existing) = table.by_idempotency_key.get(&(run.queue.clone(), key.clone()))
            && let Some(entry) = table.runs.get(existing)
        {
            return Ok(Enqueued::Existing(entry.record.clone()));
        }
        if let Some(key) = &run.concurrency_key {
            let holders = table.holders(&run.queue, key);
            match run.on_conflict {
                OnConflict::Reject => {
                    if let Some((blocking_run_id, _)) = holders.into_iter().next() {
                        return Ok(Enqueued::Rejected { blocking_run_id });
                    }
                }
                OnConflict::Interrupt => {
                    for (run_id, _) in holders {
                        if let Some(entry) = table.runs.get_mut(&run_id) {
                            let record = &mut entry.record;
                            if record.state == RunState::Running {
                                record.cancel_requested = true;
                            } else {
                                record.state = RunState::Cancelled;
                                release_lease(record);
                            }
                            touch(record, now);
                        }
                    }
                }
                OnConflict::Enqueue => {}
            }
        }
        let run_id = uuid::Uuid::new_v4().to_string();
        if let Some(key) = &run.idempotency_key {
            table.by_idempotency_key.insert((run.queue.clone(), key.clone()), run_id.clone());
        }
        let record = RunRecord::queued(run_id.clone(), run, now);
        let seq = table.next_seq;
        table.next_seq += 1;
        table.runs.insert(run_id, RunEntry { record: record.clone(), seq });
        Ok(Enqueued::Created(record))
    }

    async fn claim(&self, queue: &str, worker: &str, ttl: Duration) -> Result<Option<ClaimedRun>> {
        let now = self.clock.now();
        let expires_at = deadline(now, ttl, "run_store.invalid_ttl")?;
        let mut table = self.table.lock().await;
        let mut candidates: Vec<_> = table
            .runs
            .values()
            .filter(|entry| entry.record.queue == queue && !entry.record.state.is_terminal())
            .map(|entry| (entry.order(), entry.record.run_id.clone()))
            .collect();
        candidates.sort();
        for (order, run_id) in candidates {
            let Some(entry) = table.runs.get(&run_id) else { continue };
            let record = &entry.record;
            let started = match record.state {
                RunState::Queued => {
                    let delayed = record.not_before.is_some_and(|at| at > now);
                    let blocked = record.concurrency_key.as_deref().is_some_and(|key| {
                        table.holders(queue, key).first().is_some_and(|(_, first)| *first < order)
                    });
                    if delayed || blocked {
                        continue;
                    }
                    1
                }
                RunState::Waiting => {
                    if record.not_before.is_none_or(|at| at > now) {
                        continue;
                    }
                    0
                }
                RunState::Running => {
                    if record.lease_expires_at.is_some_and(|at| at > now) {
                        continue;
                    }
                    1
                }
                RunState::Completed | RunState::Failed | RunState::Cancelled => continue,
            };
            let Some(entry) = table.runs.get_mut(&run_id) else { continue };
            let record = &mut entry.record;
            if record.state == RunState::Running && record.attempt >= record.max_attempts {
                record.state = RunState::Failed;
                record.error = Some(format!(
                    "attempts exhausted: the lease of attempt {} of {} expired",
                    record.attempt, record.max_attempts
                ));
                release_lease(record);
                touch(record, now);
                continue;
            }
            record.state = RunState::Running;
            record.attempt += started;
            record.lease_epoch += 1;
            record.lease_owner = Some(worker.to_string());
            record.lease_expires_at = Some(expires_at);
            record.heartbeat_at = Some(now);
            record.not_before = None;
            touch(record, now);
            return Ok(Some(ClaimedRun { epoch: record.lease_epoch, record: record.clone() }));
        }
        Ok(None)
    }

    async fn heartbeat(&self, run_id: &str, epoch: u64, ttl: Duration) -> Result<bool> {
        let now = self.clock.now();
        let expires_at = deadline(now, ttl, "run_store.invalid_ttl")?;
        let mut table = self.table.lock().await;
        let record = table.fenced(run_id, epoch)?;
        record.lease_expires_at = Some(expires_at);
        record.heartbeat_at = Some(now);
        touch(record, now);
        Ok(record.cancel_requested)
    }

    async fn set_checkpoint(
        &self,
        run_id: &str,
        epoch: u64,
        checkpoint: RunCheckpointRef,
    ) -> Result<()> {
        let now = self.clock.now();
        let mut table = self.table.lock().await;
        let record = table.fenced(run_id, epoch)?;
        record.checkpoint = Some(checkpoint);
        touch(record, now);
        Ok(())
    }

    async fn wait(&self, run_id: &str, epoch: u64, until: Option<DateTime<Utc>>) -> Result<()> {
        let now = self.clock.now();
        let mut table = self.table.lock().await;
        let record = table.fenced(run_id, epoch)?;
        record.state = RunState::Waiting;
        record.not_before = if record.wake_pending { Some(now) } else { until };
        record.wake_pending = false;
        release_lease(record);
        touch(record, now);
        Ok(())
    }

    async fn wake(&self, run_id: &str) -> Result<()> {
        let now = self.clock.now();
        let mut table = self.table.lock().await;
        let entry = table.runs.get_mut(run_id).ok_or_else(|| run_not_found_error(run_id))?;
        let record = &mut entry.record;
        match record.state {
            RunState::Waiting => record.not_before = Some(now),
            RunState::Running => record.wake_pending = true,
            RunState::Queued | RunState::Completed | RunState::Failed | RunState::Cancelled => {
                return Ok(());
            }
        }
        touch(record, now);
        Ok(())
    }

    async fn finish(&self, run_id: &str, epoch: u64, outcome: RunOutcome) -> Result<RunRecord> {
        let now = self.clock.now();
        let mut table = self.table.lock().await;
        let record = table.fenced(run_id, epoch)?;
        match outcome {
            RunOutcome::Completed(result) => {
                record.state = RunState::Completed;
                record.result = Some(result);
            }
            RunOutcome::Failed { error, retryable, backoff } => {
                let retry =
                    retryable && record.attempt < record.max_attempts && !record.cancel_requested;
                record.state = if retry { RunState::Queued } else { RunState::Failed };
                if retry {
                    record.not_before = backoff.map(|backoff| now + backoff);
                }
                record.error = Some(error);
            }
            RunOutcome::Cancelled => record.state = RunState::Cancelled,
        }
        record.wake_pending = false;
        release_lease(record);
        touch(record, now);
        Ok(record.clone())
    }

    async fn request_cancel(&self, run_id: &str) -> Result<()> {
        let now = self.clock.now();
        let mut table = self.table.lock().await;
        let entry = table.runs.get_mut(run_id).ok_or_else(|| run_not_found_error(run_id))?;
        let record = &mut entry.record;
        match record.state {
            RunState::Queued | RunState::Waiting => {
                record.state = RunState::Cancelled;
                release_lease(record);
            }
            RunState::Running => record.cancel_requested = true,
            RunState::Completed | RunState::Failed | RunState::Cancelled => return Ok(()),
        }
        touch(record, now);
        Ok(())
    }

    async fn get(&self, run_id: &str) -> Result<Option<RunRecord>> {
        Ok(self.table.lock().await.runs.get(run_id).map(|entry| entry.record.clone()))
    }

    async fn list(&self, filter: RunFilter) -> Result<Vec<RunRecord>> {
        let table = self.table.lock().await;
        let mut matching: Vec<_> =
            table.runs.values().filter(|entry| filter.matches(&entry.record)).collect();
        matching.sort_by_key(|entry| entry.order());
        let limit = filter.limit.unwrap_or(usize::MAX);
        Ok(matching.into_iter().take(limit).map(|entry| entry.record.clone()).collect())
    }

    async fn prune(&self, finished_before: DateTime<Utc>) -> Result<u64> {
        let mut table = self.table.lock().await;
        let pruned: Vec<_> = table
            .runs
            .values()
            .filter(|entry| {
                entry.record.state.is_terminal() && entry.record.updated_at < finished_before
            })
            .map(|entry| entry.record.run_id.clone())
            .collect();
        for run_id in &pruned {
            if let Some(entry) = table.runs.remove(run_id)
                && let Some(key) = entry.record.idempotency_key
            {
                table.by_idempotency_key.remove(&(entry.record.queue, key));
            }
        }
        Ok(pruned.len() as u64)
    }
}