adk-session 3.0.0

Session management and state persistence for Rust Agent Development Kit (ADK-Rust) agents
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
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
//! Firestore-backed [`SpendLedger`](adk_core::SpendLedger), [`ActionLedger`](adk_core::ActionLedger),
//! and [`ApprovalStore`](adk_core::ApprovalStore).
//!
//! Every read-modify-write runs in a Firestore read-write transaction. Firestore
//! transactions are serializable: a transaction that read a document another
//! transaction changed before it committed is aborted, and the `firestore` crate retries
//! it with exponential backoff.
//!
//! | Store | Collection (document ID) | How it stays atomic |
//! |-------|--------------------------|---------------------|
//! | [`FirestoreSpendLedger`] | `adk_spend_entries` (reservation ID), `adk_spend_orgs` (hex of the organization), `adk_spend_orgs/{org}/days` (UTC date) | `reserve` reads the organization document, the current month's day totals, and the live holds, then writes the hold and bumps the organization's `seq`, so two reservations for one organization conflict and one retries |
//! | [`FirestoreActionLedger`] | `adk_action_ledger` (digest of the key) | `begin` is a create, which fails when the document exists; `complete` re-reads the record in a transaction |
//! | [`FirestoreApprovalStore`] | `adk_approvals` (digest of scope, kind, and fingerprint) | `take_decision` reads and deletes the decision in one transaction, so concurrent takers see it once |
//! | [`FirestoreApprovalStore`] requests and delegations | `adk_approval_requests`, `adk_approval_delegations` (digest of the ID) | `create_request` is a create, which fails when the document exists; `replace_request` reads and compares the version in a transaction, so of concurrent replaces at one version one commits and the others retry, see the new version, and report `false` |
//!
//! `list_requests` queries with equality filters only — the application, `open`, or a
//! single state — and orders, pages, and applies the remaining filters in memory, so no
//! composite index is needed; a listing reads every request of the application (or
//! every open request) it filters.
//!
//! Document IDs cannot hold `/` and are length-limited, so idempotency keys and
//! fingerprints are addressed by their SHA-256 in hex. Each document also stores the full key,
//! fingerprint, and scope, and every read checks them: two values that share a digest
//! never read each other's record. A write that would land on a document holding a
//! different value fails instead of overwriting it.
//!
//! Committed spend is kept as per-attribution totals, one document per UTC day plus a
//! lifetime total on the organization document, so a reservation reads at most one
//! month of day documents however many calls were made. Every query uses equality
//! filters or one range filter, which Firestore serves from its automatic single-field
//! indexes; no composite index is needed.
//!
//! Timestamps on action records and approvals are stored as RFC 3339 text with
//! nanoseconds, so they read back exactly.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use adk_core::action_ledger::{already_completed_error, duplicate_key_error, missing_key_error};
use adk_core::principal::SubjectRef;
use adk_core::{
    ActionLedger, ActionOutcome, ActionRecord, AdkError, ApprovalDelegation, ApprovalRequest,
    ApprovalRequestQuery, ApprovalScope, ApprovalStore, Clock, DEFAULT_RESERVATION_TTL,
    ErrorCategory, ErrorComponent, PendingApproval, ReservationId, Result, SpendError, SpendKey,
    SpendLedger, SpendLimits, SpendPeriod, SystemClock, ToolApproval, ToolConfirmationDecision,
    ToolConfirmationRequest,
};
use async_trait::async_trait;
use chrono::{DateTime, NaiveDate, Utc};
use firestore::errors::{BackoffError, FirestoreError};
use firestore::{
    FirestoreDb, FirestoreTransaction, FirestoreTransactionOptions, FirestoreWritePrecondition,
};
use serde::{Deserialize, Serialize};
use serde_json::json;

const SPEND_ENTRIES: &str = "adk_spend_entries";
const SPEND_ORGS: &str = "adk_spend_orgs";
const SPEND_DAYS: &str = "days";
const ACTIONS: &str = "adk_action_ledger";
const APPROVALS: &str = "adk_approvals";
const APPROVAL_REQUESTS: &str = "adk_approval_requests";
const APPROVAL_DELEGATIONS: &str = "adk_approval_delegations";

const STATE_RESERVED: &str = "reserved";
const STATE_COMMITTED: &str = "committed";
const STATE_RELEASED: &str = "released";

const KIND_PENDING: &str = "pending";
const KIND_DECISION: &str = "decision";

/// How long one operation may spend retrying conflicting transactions.
const TRANSACTION_DEADLINE: chrono::TimeDelta = chrono::TimeDelta::seconds(120);

pub(crate) type TxResult<T> = std::result::Result<T, BackoffError<AdkError>>;
pub(crate) type TxFuture<'b, T> = Pin<Box<dyn Future<Output = TxResult<T>> + Send + 'b>>;

pub(crate) fn transaction_options() -> FirestoreTransactionOptions {
    FirestoreTransactionOptions::new().with_max_elapsed_time(TRANSACTION_DEADLINE)
}

/// Whether Firestore reports `error` as retryable, such as a write aborted by contention.
pub(crate) fn is_retryable(error: &FirestoreError) -> bool {
    matches!(error, FirestoreError::DatabaseError(db) if db.retry_possible)
}

/// Runs `attempt` again, with exponential backoff, while it fails with an error
/// `retryable` accepts and [`TRANSACTION_DEADLINE`] has not passed.
///
/// Firestore aborts a write that contends with another for a document and reports the
/// abort as retryable; its client libraries run such a write again, and so does this.
pub(crate) async fn retry_while<T, E, F, Fut>(
    retryable: impl Fn(&E) -> bool,
    mut attempt: F,
) -> std::result::Result<T, E>
where
    E: std::fmt::Display,
    F: FnMut() -> Fut,
    Fut: Future<Output = std::result::Result<T, E>>,
{
    let deadline = std::time::Instant::now()
        + TRANSACTION_DEADLINE.to_std().expect("the transaction deadline is positive");
    let mut delay = Duration::from_millis(50);
    loop {
        match attempt().await {
            Err(error) if retryable(&error) && std::time::Instant::now() < deadline => {
                tracing::debug!(error = %error, delay.ms = delay.as_millis(), "retrying firestore write");
                tokio::time::sleep(delay).await;
                delay = (delay * 2).min(Duration::from_secs(2));
            }
            result => return result,
        }
    }
}

/// Retries a transaction on a read Firestore reports as retryable, such as a contention
/// abort, and gives up on any other failure.
pub(crate) fn retry_or_fail(
    error: FirestoreError,
    fail: impl FnOnce(FirestoreError) -> AdkError,
) -> BackoffError<AdkError> {
    if is_retryable(&error) {
        BackoffError::transient(fail(error))
    } else {
        BackoffError::permanent(fail(error))
    }
}

/// Recovers the [`AdkError`] a transaction body failed with, or reports the transaction
/// failure itself through `fail`.
pub(crate) fn transaction_error(
    error: FirestoreError,
    fail: impl FnOnce(String) -> AdkError,
) -> AdkError {
    match error {
        FirestoreError::ErrorInTransaction(inner) => match inner.source.downcast::<AdkError>() {
            Ok(adk_error) => *adk_error,
            Err(source) => fail(source.to_string()),
        },
        other => fail(other.to_string()),
    }
}

fn storage_error(code: &'static str, message: String) -> AdkError {
    AdkError::new(ErrorComponent::Session, ErrorCategory::Unavailable, code, message)
}

pub(crate) fn to_json<T: Serialize>(
    value: &T,
    fail: impl FnOnce(String) -> AdkError,
) -> Result<String> {
    serde_json::to_string(value).map_err(|e| fail(format!("serialize failed: {e}")))
}

pub(crate) fn from_json<T: serde::de::DeserializeOwned>(
    text: &str,
    what: &str,
    fail: impl FnOnce(String) -> AdkError,
) -> Result<T> {
    serde_json::from_str(text).map_err(|e| fail(format!("stored {what} is corrupt: {e}")))
}

fn hex(text: &str) -> String {
    text.bytes().map(|byte| format!("{byte:02x}")).collect()
}

pub(crate) async fn connect(project_id: &str) -> std::result::Result<FirestoreDb, String> {
    FirestoreDb::new(project_id).await.map_err(|e| format!("firestore connection failed: {e}"))
}

/// The Firestore emulator accepts the `owner` token as an administrator.
struct EmulatorOwnerToken;

#[async_trait]
impl gcloud_sdk::Source for EmulatorOwnerToken {
    async fn token(&self) -> gcloud_sdk::error::Result<gcloud_sdk::Token> {
        Ok(gcloud_sdk::Token::new(
            "Bearer".to_string(),
            "owner".into(),
            Utc::now() + chrono::Duration::hours(1),
        ))
    }
}

/// Connects to the Firestore emulator at `FIRESTORE_EMULATOR_HOST` without Application
/// Default Credentials, for local development and tests.
///
/// Pass the client to `from_db` on any Firestore store.
///
/// # Example
///
/// ```rust,no_run
/// use adk_session::FirestoreSpendLedger;
/// use adk_session::firestore_stores::connect_firestore_emulator;
///
/// # async fn demo() -> adk_core::Result<()> {
/// // FIRESTORE_EMULATOR_HOST=127.0.0.1:8080
/// let ledger = FirestoreSpendLedger::from_db(connect_firestore_emulator("demo-project").await?);
/// # let _ = ledger;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns `session.firestore.emulator` when `FIRESTORE_EMULATOR_HOST` is unset or the
/// client cannot be created. Without the variable the client would reach production
/// Firestore with a token it rejects, so the call fails instead.
pub async fn connect_firestore_emulator(project_id: &str) -> Result<FirestoreDb> {
    let fail = |message: String| storage_error("session.firestore.emulator", message);
    if std::env::var_os("FIRESTORE_EMULATOR_HOST").is_none() {
        return Err(fail(
            "FIRESTORE_EMULATOR_HOST is not set; set it to the emulator's host:port".to_string(),
        ));
    }
    FirestoreDb::with_options_token_source(
        firestore::FirestoreDbOptions::new(project_id.to_string()),
        Vec::new(),
        gcloud_sdk::TokenSourceType::ExternalSource(Box::new(EmulatorOwnerToken)),
    )
    .await
    .map_err(|e| fail(format!("firestore emulator connection failed: {e}")))
}

// ---------------------------------------------------------------------------
// Spend ledger
// ---------------------------------------------------------------------------

fn spend_unavailable(error: impl std::fmt::Display) -> AdkError {
    SpendError::Unavailable(error.to_string()).into()
}

fn stored_amount(amount_micro_usd: u64) -> Result<i64> {
    i64::try_from(amount_micro_usd).map_err(|_| SpendError::AmountTooLarge(amount_micro_usd).into())
}

/// Committed spend for one attribution.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct AttributedTotal {
    #[serde(default)]
    agent: Option<String>,
    #[serde(default)]
    vendor: Option<String>,
    micro_usd: i64,
}

impl AttributedTotal {
    fn key(&self, org: &str) -> SpendKey {
        SpendKey {
            org: org.to_string(),
            agent: self.agent.clone(),
            vendor: self.vendor.clone(),
            period: SpendPeriod::Lifetime,
        }
    }
}

fn add_to_totals(totals: &mut Vec<AttributedTotal>, key: &SpendKey, amount: i64) {
    match totals.iter_mut().find(|total| total.agent == key.agent && total.vendor == key.vendor) {
        Some(total) => total.micro_usd = total.micro_usd.saturating_add(amount),
        None => totals.push(AttributedTotal {
            agent: key.agent.clone(),
            vendor: key.vendor.clone(),
            micro_usd: amount,
        }),
    }
}

fn covered_total<'a>(
    org: &str,
    scope: &SpendKey,
    totals: impl IntoIterator<Item = &'a AttributedTotal>,
) -> u64 {
    totals
        .into_iter()
        .filter(|total| scope.covers(&total.key(org)))
        .fold(0u64, |sum, total| sum.saturating_add(u64::try_from(total.micro_usd).unwrap_or(0)))
}

/// One organization: the lock every reservation for it writes, and lifetime totals.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct OrgDoc {
    org: String,
    #[serde(default)]
    seq: i64,
    #[serde(default)]
    lifetime: Vec<AttributedTotal>,
}

/// Committed spend settled on one UTC day.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct DayDoc {
    /// `YYYY-MM-DD`, so text order is date order.
    day: String,
    #[serde(default)]
    totals: Vec<AttributedTotal>,
}

/// One reservation, kept after settlement for audit.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct EntryDoc {
    org: String,
    #[serde(default)]
    agent: Option<String>,
    #[serde(default)]
    vendor: Option<String>,
    state: String,
    reserved_micro_usd: i64,
    #[serde(default)]
    committed_micro_usd: Option<i64>,
    created_at_us: i64,
    expires_at_us: i64,
    #[serde(default)]
    settled_at_us: Option<i64>,
}

impl EntryDoc {
    fn key(&self) -> SpendKey {
        SpendKey {
            org: self.org.clone(),
            agent: self.agent.clone(),
            vendor: self.vendor.clone(),
            period: SpendPeriod::Lifetime,
        }
    }
}

fn day_id(day: NaiveDate) -> String {
    day.format("%Y-%m-%d").to_string()
}

/// The reads one reservation's limit check needs.
struct OrgSnapshot {
    org: OrgDoc,
    /// Day documents of the current month, up to and including today.
    month: Vec<DayDoc>,
    holds: Vec<EntryDoc>,
}

impl OrgSnapshot {
    fn committed(&self, scope: &SpendKey, period: SpendPeriod, today: &str) -> u64 {
        let org = &self.org.org;
        match period {
            SpendPeriod::Day => covered_total(
                org,
                scope,
                self.month.iter().filter(|day| day.day == today).flat_map(|day| &day.totals),
            ),
            SpendPeriod::Month => {
                covered_total(org, scope, self.month.iter().flat_map(|day| &day.totals))
            }
            SpendPeriod::Lifetime => covered_total(org, scope, &self.org.lifetime),
        }
    }

    fn held(&self, scope: &SpendKey, now_us: i64) -> u64 {
        self.holds
            .iter()
            .filter(|hold| hold.expires_at_us > now_us && scope.covers(&hold.key()))
            .fold(0u64, |sum, hold| {
                sum.saturating_add(u64::try_from(hold.reserved_micro_usd).unwrap_or(0))
            })
    }
}

async fn read_org(db: &FirestoreDb, org: &str) -> std::result::Result<OrgDoc, FirestoreError> {
    let stored: Option<OrgDoc> =
        db.fluent().select().by_id_in(SPEND_ORGS).obj::<OrgDoc>().one(hex(org)).await?;
    Ok(stored.unwrap_or_else(|| OrgDoc { org: org.to_string(), ..OrgDoc::default() }))
}

async fn read_month(
    db: &FirestoreDb,
    org: &str,
    now: DateTime<Utc>,
) -> std::result::Result<Vec<DayDoc>, FirestoreError> {
    let parent = db.parent_path(SPEND_ORGS, hex(org))?;
    let month_start = SpendPeriod::Month
        .window_start(now)
        .map(|start| day_id(start.date_naive()))
        .unwrap_or_default();
    let today = day_id(now.date_naive());
    let days: Vec<DayDoc> = db
        .fluent()
        .select()
        .from(SPEND_DAYS)
        .parent(&parent)
        .filter(|q| q.for_all([q.field("day").greater_than_or_equal(month_start.as_str())]))
        .obj::<DayDoc>()
        .query()
        .await?;
    Ok(days.into_iter().filter(|day| day.day <= today).collect())
}

async fn read_holds(
    db: &FirestoreDb,
    org: &str,
) -> std::result::Result<Vec<EntryDoc>, FirestoreError> {
    db.fluent()
        .select()
        .from(SPEND_ENTRIES)
        .filter(|q| q.for_all([q.field("org").eq(org), q.field("state").eq(STATE_RESERVED)]))
        .obj::<EntryDoc>()
        .query()
        .await
}

/// Firestore-backed [`SpendLedger`].
///
/// See the [module documentation](self) for the data model.
///
/// # Example
///
/// ```rust,no_run
/// use adk_core::{SpendKey, SpendLimits, SpendPeriod};
/// use adk_session::FirestoreSpendLedger;
///
/// # async fn demo() -> adk_core::Result<()> {
/// let ledger = FirestoreSpendLedger::new("my-gcp-project")
///     .await?
///     .with_limits(
///         SpendLimits::new().limit(SpendKey::org("acme").per(SpendPeriod::Day), 50_000_000),
///     );
/// ledger.migrate().await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct FirestoreSpendLedger {
    db: FirestoreDb,
    limits: SpendLimits,
    ttl: Duration,
    clock: Arc<dyn Clock>,
}

impl std::fmt::Debug for FirestoreSpendLedger {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FirestoreSpendLedger")
            .field("limits", &self.limits)
            .field("ttl", &self.ttl)
            .field("clock", &self.clock)
            .finish_non_exhaustive()
    }
}

impl FirestoreSpendLedger {
    /// Connects to `project_id` with Application Default Credentials, no limits, and
    /// [`DEFAULT_RESERVATION_TTL`]. Honors `FIRESTORE_EMULATOR_HOST`.
    ///
    /// # Errors
    ///
    /// Returns `spend.ledger_unavailable` when the client cannot be created.
    pub async fn new(project_id: &str) -> Result<Self> {
        Ok(Self::from_db(connect(project_id).await.map_err(spend_unavailable)?))
    }

    /// Uses an existing client, such as the one a `FirestoreSessionService` holds.
    pub fn from_db(db: FirestoreDb) -> Self {
        Self {
            db,
            limits: SpendLimits::new(),
            ttl: DEFAULT_RESERVATION_TTL,
            clock: Arc::new(SystemClock),
        }
    }

    /// Sets the limits every reservation is checked against.
    pub fn with_limits(mut self, limits: SpendLimits) -> Self {
        self.limits = limits;
        self
    }

    /// Sets how long an unsettled reservation holds budget.
    pub fn with_reservation_ttl(mut self, ttl: Duration) -> Self {
        self.ttl = ttl;
        self
    }

    /// Sets the clock that picks period windows and expires reservations.
    pub fn with_clock(mut self, clock: Arc<dyn Clock>) -> Self {
        self.clock = clock;
        self
    }

    /// Prepares the ledger's storage.
    ///
    /// Firestore creates collections on first write and serves every query the ledger
    /// makes from automatic single-field indexes, so there is nothing to create; the
    /// method exists so every backend is set up the same way.
    ///
    /// # Errors
    ///
    /// Never returns an error.
    pub async fn migrate(&self) -> Result<()> {
        Ok(())
    }

    /// Settles a reserved entry as `state`, recording `committed` when it is a commit.
    async fn settle(&self, id: ReservationId, committed: Option<i64>) -> Result<()> {
        let now = self.clock.now();
        let id_text = id.as_str().to_string();
        let settled = self
            .db
            .run_transaction_with_options(
                move |db, transaction| -> TxFuture<'_, bool> {
                    let id_text = id_text.clone();
                    Box::pin(async move {
                        settle_in_transaction(&db, transaction, &id_text, committed, now).await
                    })
                },
                transaction_options(),
            )
            .await
            .map_err(|e| transaction_error(e, spend_unavailable))?;
        if settled { Ok(()) } else { Err(SpendError::UnknownReservation(id).into()) }
    }
}

async fn settle_in_transaction(
    db: &FirestoreDb,
    transaction: &mut FirestoreTransaction<'_>,
    id: &str,
    committed: Option<i64>,
    now: DateTime<Utc>,
) -> TxResult<bool> {
    let fail = |e: FirestoreError| spend_unavailable(e);
    let entry: Option<EntryDoc> = db
        .fluent()
        .select()
        .by_id_in(SPEND_ENTRIES)
        .obj::<EntryDoc>()
        .one(id)
        .await
        .map_err(|e| retry_or_fail(e, fail))?;
    let Some(entry) = entry.filter(|entry| entry.state == STATE_RESERVED) else {
        return Ok(false);
    };
    let settled = EntryDoc {
        state: if committed.is_some() { STATE_COMMITTED } else { STATE_RELEASED }.to_string(),
        committed_micro_usd: committed,
        settled_at_us: Some(now.timestamp_micros()),
        ..entry.clone()
    };

    let mut writes = Vec::new();
    if let Some(amount) = committed {
        let mut org = read_org(db, &entry.org).await.map_err(|e| retry_or_fail(e, fail))?;
        let parent =
            db.parent_path(SPEND_ORGS, hex(&entry.org)).map_err(|e| retry_or_fail(e, fail))?;
        let today = day_id(now.date_naive());
        let mut day: DayDoc = db
            .fluent()
            .select()
            .by_id_in(SPEND_DAYS)
            .parent(&parent)
            .obj::<DayDoc>()
            .one(&today)
            .await
            .map_err(|e| retry_or_fail(e, fail))?
            .unwrap_or_else(|| DayDoc { day: today.clone(), totals: Vec::new() });
        add_to_totals(&mut day.totals, &entry.key(), amount);
        add_to_totals(&mut org.lifetime, &entry.key(), amount);
        writes.push((parent, today, day, org));
    }

    db.fluent()
        .update()
        .in_col(SPEND_ENTRIES)
        .precondition(FirestoreWritePrecondition::Exists(true))
        .document_id(id)
        .object(&settled)
        .add_to_transaction(transaction)
        .map_err(|e| retry_or_fail(e, fail))?;
    for (parent, today, day, org) in &writes {
        db.fluent()
            .update()
            .in_col(SPEND_DAYS)
            .document_id(today)
            .parent(parent)
            .object(day)
            .add_to_transaction(transaction)
            .map_err(|e| retry_or_fail(e, fail))?;
        db.fluent()
            .update()
            .in_col(SPEND_ORGS)
            .document_id(hex(&org.org))
            .object(org)
            .add_to_transaction(transaction)
            .map_err(|e| retry_or_fail(e, fail))?;
    }
    Ok(true)
}

#[async_trait]
impl SpendLedger for FirestoreSpendLedger {
    async fn reserve(&self, key: &SpendKey, amount_micro_usd: u64) -> Result<ReservationId> {
        let amount = stored_amount(amount_micro_usd)?;
        let now = self.clock.now();
        let ttl = i64::try_from(self.ttl.as_micros()).unwrap_or(i64::MAX);
        let request = Arc::new((key.clone(), self.limits.clone()));
        let id = ReservationId::generate();
        let id_text = id.as_str().to_string();
        let refused = self
            .db
            .run_transaction_with_options(
                move |db, transaction| -> TxFuture<'_, Option<AdkError>> {
                    let request = request.clone();
                    let id_text = id_text.clone();
                    Box::pin(async move {
                        let (key, limits) = &*request;
                        let fail = |e: FirestoreError| spend_unavailable(e);
                        let snapshot = OrgSnapshot {
                            org: read_org(&db, &key.org)
                                .await
                                .map_err(|e| retry_or_fail(e, fail))?,
                            month: read_month(&db, &key.org, now)
                                .await
                                .map_err(|e| retry_or_fail(e, fail))?,
                            holds: read_holds(&db, &key.org)
                                .await
                                .map_err(|e| retry_or_fail(e, fail))?,
                        };
                        let now_us = now.timestamp_micros();
                        let today = day_id(now.date_naive());
                        for limit in limits.applicable(key) {
                            let consumed = snapshot
                                .committed(&limit.key, limit.key.period, &today)
                                .saturating_add(snapshot.held(&limit.key, now_us));
                            if consumed.saturating_add(amount_micro_usd) > limit.max_micro_usd {
                                return Ok(Some(
                                    SpendError::LimitExceeded {
                                        limit: limit.key.clone(),
                                        max_micro_usd: limit.max_micro_usd,
                                        consumed_micro_usd: consumed,
                                        requested_micro_usd: amount_micro_usd,
                                    }
                                    .into(),
                                ));
                            }
                        }
                        let entry = EntryDoc {
                            org: key.org.clone(),
                            agent: key.agent.clone(),
                            vendor: key.vendor.clone(),
                            state: STATE_RESERVED.to_string(),
                            reserved_micro_usd: amount,
                            committed_micro_usd: None,
                            created_at_us: now_us,
                            expires_at_us: now_us.saturating_add(ttl),
                            settled_at_us: None,
                        };
                        // Every reservation for the organization writes its document, so
                        // two that read the same totals conflict and one retries.
                        let org =
                            OrgDoc { seq: snapshot.org.seq.saturating_add(1), ..snapshot.org };
                        db.fluent()
                            .update()
                            .in_col(SPEND_ENTRIES)
                            .precondition(FirestoreWritePrecondition::Exists(false))
                            .document_id(&id_text)
                            .object(&entry)
                            .add_to_transaction(transaction)
                            .map_err(|e| retry_or_fail(e, fail))?;
                        db.fluent()
                            .update()
                            .in_col(SPEND_ORGS)
                            .document_id(hex(&key.org))
                            .object(&org)
                            .add_to_transaction(transaction)
                            .map_err(|e| retry_or_fail(e, fail))?;
                        Ok(None)
                    })
                },
                transaction_options(),
            )
            .await
            .map_err(|e| transaction_error(e, spend_unavailable))?;
        match refused {
            Some(error) => Err(error),
            None => Ok(id),
        }
    }

    async fn commit(&self, id: ReservationId, actual_micro_usd: u64) -> Result<()> {
        let amount = stored_amount(actual_micro_usd)?;
        self.settle(id, Some(amount)).await
    }

    async fn release(&self, id: ReservationId) -> Result<()> {
        self.settle(id, None).await
    }

    async fn spent(&self, key: &SpendKey, period: SpendPeriod) -> Result<u64> {
        let now = self.clock.now();
        let today = day_id(now.date_naive());
        let totals: Vec<AttributedTotal> = match period {
            SpendPeriod::Lifetime => {
                read_org(&self.db, &key.org).await.map_err(spend_unavailable)?.lifetime
            }
            SpendPeriod::Month | SpendPeriod::Day => read_month(&self.db, &key.org, now)
                .await
                .map_err(spend_unavailable)?
                .into_iter()
                .filter(|day| period == SpendPeriod::Month || day.day == today)
                .flat_map(|day| day.totals)
                .collect(),
        };
        Ok(covered_total(&key.org, key, &totals))
    }
}

// ---------------------------------------------------------------------------
// Action ledger
// ---------------------------------------------------------------------------

fn action_error(message: String) -> AdkError {
    storage_error("session.action_ledger.storage", message)
}

/// One ledgered call. The record is stored as JSON text so its timestamps are exact.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ActionDoc {
    key: String,
    record: String,
}

/// SHA-256 of `value`'s JSON text, in hex: path-safe, fixed length, and collision
/// resistant, so a crafted key cannot occupy another call's document.
pub(crate) fn document_id(value: &serde_json::Value) -> String {
    use sha2::{Digest, Sha256};
    use std::fmt::Write;

    Sha256::digest(value.to_string().as_bytes()).iter().fold(
        String::with_capacity(64),
        |mut id, byte| {
            let _ = write!(id, "{byte:02x}");
            id
        },
    )
}

fn action_id(key: &str) -> String {
    document_id(&json!(key))
}

/// Firestore-backed [`ActionLedger`], so a non-idempotent tool call is never repeated
/// across a process restart.
///
/// Each record stores the idempotency key, tool name, argument digest, effect, start
/// time, and outcome — never the arguments or result themselves. See the
/// [module documentation](self) for the data model.
///
/// # Example
///
/// ```rust,no_run
/// use adk_core::RunConfig;
/// use adk_session::FirestoreActionLedger;
/// use std::sync::Arc;
///
/// # async fn example() -> adk_core::Result<()> {
/// let ledger = FirestoreActionLedger::new("my-gcp-project").await?;
/// ledger.migrate().await?;
/// let config = RunConfig::builder().action_ledger(Arc::new(ledger)).build();
/// # let _ = config;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct FirestoreActionLedger {
    db: FirestoreDb,
}

impl std::fmt::Debug for FirestoreActionLedger {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FirestoreActionLedger").finish_non_exhaustive()
    }
}

impl FirestoreActionLedger {
    /// Connects to `project_id` with Application Default Credentials. Honors
    /// `FIRESTORE_EMULATOR_HOST`.
    ///
    /// # Errors
    ///
    /// Returns `session.action_ledger.storage` when the client cannot be created.
    pub async fn new(project_id: &str) -> Result<Self> {
        Ok(Self::from_db(connect(project_id).await.map_err(action_error)?))
    }

    /// Uses an existing client, such as the one a `FirestoreSessionService` holds.
    pub fn from_db(db: FirestoreDb) -> Self {
        Self { db }
    }

    /// Prepares the ledger's storage.
    ///
    /// Firestore creates collections on first write and the ledger reads documents only
    /// by ID, so there is nothing to create; the method exists so every backend is set
    /// up the same way.
    ///
    /// # Errors
    ///
    /// Never returns an error.
    pub async fn migrate(&self) -> Result<()> {
        Ok(())
    }
}

#[async_trait]
impl ActionLedger for FirestoreActionLedger {
    async fn begin(&self, record: &ActionRecord) -> Result<()> {
        let doc = ActionDoc { key: record.key.clone(), record: to_json(record, action_error)? };
        let id = action_id(&record.key);
        let mut attempt = 0;
        loop {
            let created = self
                .db
                .fluent()
                .insert()
                .into(ACTIONS)
                .document_id(&id)
                .object(&doc)
                .execute::<ActionDoc>()
                .await;
            let error = match created {
                Ok(_) => return Ok(()),
                Err(FirestoreError::DataConflictError(_)) => {
                    return Err(duplicate_key_error(&record.key));
                }
                Err(error) => error,
            };
            // A contended create can fail without saying whether another writer won, so
            // the document decides: present means this call must not run.
            let existing: Option<ActionDoc> = self
                .db
                .fluent()
                .select()
                .by_id_in(ACTIONS)
                .obj::<ActionDoc>()
                .one(&id)
                .await
                .map_err(|e| action_error(format!("action ledger begin failed: {e}")))?;
            if existing.is_some() {
                return Err(duplicate_key_error(&record.key));
            }
            attempt += 1;
            if attempt >= 5 {
                return Err(action_error(format!("action ledger begin failed: {error}")));
            }
            tokio::time::sleep(std::time::Duration::from_millis(20 * attempt)).await;
        }
    }

    async fn complete(&self, key: &str, outcome: ActionOutcome) -> Result<()> {
        let now = Utc::now();
        let request = Arc::new((key.to_string(), outcome));
        let result = self
            .db
            .run_transaction_with_options(
                move |db, transaction| -> TxFuture<'_, Option<AdkError>> {
                    let request = request.clone();
                    Box::pin(async move {
                        let (key, outcome) = &*request;
                        let fail = |e: FirestoreError| {
                            action_error(format!("action ledger complete failed: {e}"))
                        };
                        let id = action_id(key);
                        let stored: Option<ActionDoc> = db
                            .fluent()
                            .select()
                            .by_id_in(ACTIONS)
                            .obj::<ActionDoc>()
                            .one(&id)
                            .await
                            .map_err(|e| retry_or_fail(e, fail))?;
                        let Some(stored) = stored.filter(|doc| doc.key == *key) else {
                            return Ok(Some(missing_key_error(key)));
                        };
                        let mut record: ActionRecord =
                            from_json(&stored.record, "action record", action_error)
                                .map_err(BackoffError::permanent)?;
                        if record.outcome.is_some() {
                            return Ok(Some(already_completed_error(key)));
                        }
                        record.outcome = Some(outcome.clone());
                        record.completed_at = Some(now);
                        let doc = ActionDoc {
                            key: key.clone(),
                            record: to_json(&record, action_error)
                                .map_err(BackoffError::permanent)?,
                        };
                        db.fluent()
                            .update()
                            .in_col(ACTIONS)
                            .precondition(FirestoreWritePrecondition::Exists(true))
                            .document_id(&id)
                            .object(&doc)
                            .add_to_transaction(transaction)
                            .map_err(|e| retry_or_fail(e, fail))?;
                        Ok(None)
                    })
                },
                transaction_options(),
            )
            .await
            .map_err(|e| transaction_error(e, action_error))?;
        match result {
            Some(error) => Err(error),
            None => Ok(()),
        }
    }

    async fn get(&self, key: &str) -> Result<Option<ActionRecord>> {
        let stored: Option<ActionDoc> = self
            .db
            .fluent()
            .select()
            .by_id_in(ACTIONS)
            .obj::<ActionDoc>()
            .one(action_id(key))
            .await
            .map_err(|e| action_error(format!("action ledger read failed: {e}")))?;
        stored
            .filter(|doc| doc.key == key)
            .map(|doc| from_json(&doc.record, "action record", action_error))
            .transpose()
    }
}

// ---------------------------------------------------------------------------
// Approval store
// ---------------------------------------------------------------------------

fn approval_error(message: String) -> AdkError {
    storage_error("session.approval_store.storage", message)
}

/// A pending request or a decision for one fingerprint in one scope.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ApprovalDoc {
    app_name: String,
    user_id: String,
    session_id: String,
    kind: String,
    fingerprint: String,
    /// JSON of a [`PendingApproval`] or a [`ToolApproval`].
    payload: String,
}

impl ApprovalDoc {
    fn is_for(&self, scope: &ApprovalScope, kind: &str, fingerprint: &str) -> bool {
        self.app_name == scope.app_name
            && self.user_id == scope.user_id
            && self.session_id == scope.session_id
            && self.kind == kind
            && self.fingerprint == fingerprint
    }
}

fn approval_id(scope: &ApprovalScope, kind: &str, fingerprint: &str) -> String {
    document_id(&json!([scope.app_name, scope.user_id, scope.session_id, kind, fingerprint]))
}

fn digest_collision(fingerprint: &str) -> AdkError {
    approval_error(format!(
        "approval document for fingerprint '{fingerprint}' holds a different call with the same \
         digest; the decision was not recorded"
    ))
}

/// An [`ApprovalRequest`] with the fields its queries filter on.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct RequestDoc {
    id: String,
    app: String,
    state: String,
    open: bool,
    created_us: i64,
    version: i64,
    /// JSON of the [`ApprovalRequest`].
    record: String,
}

impl RequestDoc {
    fn new(request: &ApprovalRequest) -> Result<Self> {
        Ok(Self {
            id: request.id.clone(),
            app: request.scope.app_name.clone(),
            state: request.state.as_str().to_string(),
            open: request.is_open(),
            created_us: request.created_at.timestamp_micros(),
            version: i64::try_from(request.version).map_err(|_| {
                approval_error(format!(
                    "approval request version {} is out of range",
                    request.version
                ))
            })?,
            record: to_json(request, approval_error)?,
        })
    }

    fn request(&self) -> Result<ApprovalRequest> {
        from_json(&self.record, "approval request", approval_error)
    }
}

/// An [`ApprovalDelegation`] with its delegate as query fields.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct DelegationDoc {
    id: String,
    to_kind: String,
    to_id: String,
    /// JSON of the [`ApprovalDelegation`].
    record: String,
}

fn subject_kind(subject: &SubjectRef) -> Result<String> {
    match serde_json::to_value(subject.kind) {
        Ok(serde_json::Value::String(kind)) => Ok(kind),
        _ => Err(approval_error("could not encode a subject kind".to_string())),
    }
}

fn request_doc_id(id: &str) -> String {
    document_id(&json!(["request", id]))
}

fn delegation_doc_id(id: &str) -> String {
    document_id(&json!(["delegation", id]))
}

/// Firestore-backed [`ApprovalStore`].
///
/// Pending requests and decisions survive restarts and are shared by every process that
/// uses the same database. See the [module documentation](self) for the data model.
///
/// # Example
///
/// ```rust,no_run
/// use adk_core::RunConfig;
/// use adk_session::FirestoreApprovalStore;
/// use std::sync::Arc;
///
/// # async fn example() -> adk_core::Result<()> {
/// let store = FirestoreApprovalStore::new("my-gcp-project").await?;
/// store.migrate().await?;
/// let config = RunConfig::builder().approval_store(Arc::new(store)).build();
/// # let _ = config;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct FirestoreApprovalStore {
    db: FirestoreDb,
}

impl std::fmt::Debug for FirestoreApprovalStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FirestoreApprovalStore").finish_non_exhaustive()
    }
}

impl FirestoreApprovalStore {
    /// Connects to `project_id` with Application Default Credentials. Honors
    /// `FIRESTORE_EMULATOR_HOST`.
    ///
    /// # Errors
    ///
    /// Returns `session.approval_store.storage` when the client cannot be created.
    pub async fn new(project_id: &str) -> Result<Self> {
        Ok(Self::from_db(connect(project_id).await.map_err(approval_error)?))
    }

    /// Uses an existing client, such as the one a `FirestoreSessionService` holds.
    pub fn from_db(db: FirestoreDb) -> Self {
        Self { db }
    }

    /// Prepares the store's storage.
    ///
    /// Firestore creates collections on first write and serves the pending-list query
    /// from automatic single-field indexes, so there is nothing to create; the method
    /// exists so every backend is set up the same way.
    ///
    /// # Errors
    ///
    /// Never returns an error.
    pub async fn migrate(&self) -> Result<()> {
        Ok(())
    }

    async fn read(
        &self,
        scope: &ApprovalScope,
        kind: &str,
        fingerprint: &str,
    ) -> Result<Option<ApprovalDoc>> {
        let stored: Option<ApprovalDoc> = self
            .db
            .fluent()
            .select()
            .by_id_in(APPROVALS)
            .obj::<ApprovalDoc>()
            .one(approval_id(scope, kind, fingerprint))
            .await
            .map_err(|e| approval_error(format!("approval store read failed: {e}")))?;
        Ok(stored.filter(|doc| doc.is_for(scope, kind, fingerprint)))
    }

    async fn read_request(&self, id: &str) -> Result<Option<RequestDoc>> {
        let stored: Option<RequestDoc> = self
            .db
            .fluent()
            .select()
            .by_id_in(APPROVAL_REQUESTS)
            .obj::<RequestDoc>()
            .one(request_doc_id(id))
            .await
            .map_err(|e| approval_error(format!("approval store read failed: {e}")))?;
        Ok(stored.filter(|doc| doc.id == id))
    }

    async fn read_delegation(&self, id: &str) -> Result<Option<DelegationDoc>> {
        let stored: Option<DelegationDoc> = self
            .db
            .fluent()
            .select()
            .by_id_in(APPROVAL_DELEGATIONS)
            .obj::<DelegationDoc>()
            .one(delegation_doc_id(id))
            .await
            .map_err(|e| approval_error(format!("approval store read failed: {e}")))?;
        Ok(stored.filter(|doc| doc.id == id))
    }
}

#[async_trait]
impl ApprovalStore for FirestoreApprovalStore {
    async fn record_pending(
        &self,
        scope: &ApprovalScope,
        request: &ToolConfirmationRequest,
    ) -> Result<()> {
        let fingerprint = request.fingerprint();
        let entry = PendingApproval {
            fingerprint: fingerprint.clone(),
            request: request.clone(),
            requested_at: Utc::now(),
        };
        let doc = ApprovalDoc {
            app_name: scope.app_name.clone(),
            user_id: scope.user_id.clone(),
            session_id: scope.session_id.clone(),
            kind: KIND_PENDING.to_string(),
            fingerprint: fingerprint.clone(),
            payload: to_json(&entry, approval_error)?,
        };
        let created = self
            .db
            .fluent()
            .insert()
            .into(APPROVALS)
            .document_id(approval_id(scope, KIND_PENDING, &fingerprint))
            .object(&doc)
            .execute::<ApprovalDoc>()
            .await;
        match created {
            Ok(_) => Ok(()),
            // Already pending, or decided since the create was attempted: the original
            // request and time are kept either way.
            Err(FirestoreError::DataConflictError(_)) => Ok(()),
            Err(error) => Err(approval_error(format!("approval store write failed: {error}"))),
        }
    }

    async fn pending(&self, scope: &ApprovalScope) -> Result<Vec<PendingApproval>> {
        let docs: Vec<ApprovalDoc> = self
            .db
            .fluent()
            .select()
            .from(APPROVALS)
            .filter(|q| {
                q.for_all([
                    q.field("app_name").eq(scope.app_name.as_str()),
                    q.field("user_id").eq(scope.user_id.as_str()),
                    q.field("session_id").eq(scope.session_id.as_str()),
                    q.field("kind").eq(KIND_PENDING),
                ])
            })
            .obj::<ApprovalDoc>()
            .query()
            .await
            .map_err(|e| approval_error(format!("approval store query failed: {e}")))?;
        let mut pending = docs
            .iter()
            .filter(|doc| doc.is_for(scope, KIND_PENDING, &doc.fingerprint))
            .map(|doc| {
                from_json::<PendingApproval>(&doc.payload, "pending approval", approval_error)
            })
            .collect::<Result<Vec<_>>>()?;
        pending.sort_by_key(|entry| entry.requested_at);
        Ok(pending)
    }

    async fn decide(
        &self,
        scope: &ApprovalScope,
        fingerprint: &str,
        approval: ToolApproval,
    ) -> Result<()> {
        let decision = ApprovalDoc {
            app_name: scope.app_name.clone(),
            user_id: scope.user_id.clone(),
            session_id: scope.session_id.clone(),
            kind: KIND_DECISION.to_string(),
            fingerprint: fingerprint.to_string(),
            payload: to_json(&approval, approval_error)?,
        };
        let request = Arc::new((scope.clone(), decision));
        let refused = self
            .db
            .run_transaction_with_options(
                move |db, transaction| -> TxFuture<'_, Option<AdkError>> {
                    let request = request.clone();
                    Box::pin(async move {
                        let (scope, decision) = &*request;
                        let fail = |e: FirestoreError| {
                            approval_error(format!("approval store decide failed: {e}"))
                        };
                        let fingerprint = decision.fingerprint.as_str();
                        let decision_id = approval_id(scope, KIND_DECISION, fingerprint);
                        let pending_id = approval_id(scope, KIND_PENDING, fingerprint);
                        let existing: Option<ApprovalDoc> = db
                            .fluent()
                            .select()
                            .by_id_in(APPROVALS)
                            .obj::<ApprovalDoc>()
                            .one(&decision_id)
                            .await
                            .map_err(|e| retry_or_fail(e, fail))?;
                        if existing
                            .is_some_and(|doc| !doc.is_for(scope, KIND_DECISION, fingerprint))
                        {
                            return Ok(Some(digest_collision(fingerprint)));
                        }
                        let pending: Option<ApprovalDoc> = db
                            .fluent()
                            .select()
                            .by_id_in(APPROVALS)
                            .obj::<ApprovalDoc>()
                            .one(&pending_id)
                            .await
                            .map_err(|e| retry_or_fail(e, fail))?;
                        if pending.is_some_and(|doc| doc.is_for(scope, KIND_PENDING, fingerprint)) {
                            db.fluent()
                                .delete()
                                .from(APPROVALS)
                                .document_id(&pending_id)
                                .add_to_transaction(transaction)
                                .map_err(|e| retry_or_fail(e, fail))?;
                        }
                        db.fluent()
                            .update()
                            .in_col(APPROVALS)
                            .document_id(&decision_id)
                            .object(decision)
                            .add_to_transaction(transaction)
                            .map_err(|e| retry_or_fail(e, fail))?;
                        Ok(None)
                    })
                },
                transaction_options(),
            )
            .await
            .map_err(|e| transaction_error(e, approval_error))?;
        match refused {
            Some(error) => Err(error),
            None => Ok(()),
        }
    }

    async fn take_decision(
        &self,
        scope: &ApprovalScope,
        fingerprint: &str,
    ) -> Result<Option<ToolConfirmationDecision>> {
        Ok(self.take_approval(scope, fingerprint).await?.map(|approval| approval.decision))
    }

    async fn take_approval(
        &self,
        scope: &ApprovalScope,
        fingerprint: &str,
    ) -> Result<Option<ToolApproval>> {
        let request = Arc::new((scope.clone(), fingerprint.to_string()));
        let taken = self
            .db
            .run_transaction_with_options(
                move |db, transaction| -> TxFuture<'_, Option<ToolApproval>> {
                    let request = request.clone();
                    Box::pin(async move {
                        let (scope, fingerprint) = &*request;
                        let fail = |e: FirestoreError| {
                            approval_error(format!("approval store take failed: {e}"))
                        };
                        let id = approval_id(scope, KIND_DECISION, fingerprint);
                        let stored: Option<ApprovalDoc> = db
                            .fluent()
                            .select()
                            .by_id_in(APPROVALS)
                            .obj::<ApprovalDoc>()
                            .one(&id)
                            .await
                            .map_err(|e| retry_or_fail(e, fail))?;
                        let Some(stored) =
                            stored.filter(|doc| doc.is_for(scope, KIND_DECISION, fingerprint))
                        else {
                            return Ok(None);
                        };
                        let approval: ToolApproval =
                            from_json(&stored.payload, "decision", approval_error)
                                .map_err(BackoffError::permanent)?;
                        db.fluent()
                            .delete()
                            .from(APPROVALS)
                            .document_id(&id)
                            .precondition(FirestoreWritePrecondition::Exists(true))
                            .add_to_transaction(transaction)
                            .map_err(|e| retry_or_fail(e, fail))?;
                        Ok(Some(approval))
                    })
                },
                transaction_options(),
            )
            .await
            .map_err(|e| transaction_error(e, approval_error))?;
        Ok(taken.filter(|approval| approval.is_live(Utc::now())))
    }

    async fn has_decision(&self, scope: &ApprovalScope, fingerprint: &str) -> Result<bool> {
        let Some(stored) = self.read(scope, KIND_DECISION, fingerprint).await? else {
            return Ok(false);
        };
        let approval: ToolApproval = from_json(&stored.payload, "decision", approval_error)?;
        Ok(approval.is_live(Utc::now()))
    }

    async fn create_request(&self, request: &ApprovalRequest) -> Result<ApprovalRequest> {
        let doc = RequestDoc::new(request)?;
        let created = self
            .db
            .fluent()
            .insert()
            .into(APPROVAL_REQUESTS)
            .document_id(request_doc_id(&request.id))
            .object(&doc)
            .execute::<RequestDoc>()
            .await;
        match created {
            Ok(_) => Ok(request.clone()),
            Err(FirestoreError::DataConflictError(_)) => {
                match self.read_request(&request.id).await? {
                    Some(existing) => existing.request(),
                    None => Err(approval_error(format!(
                        "approval request document for '{}' holds a different request with the same \
                     digest",
                        request.id
                    ))),
                }
            }
            Err(error) => Err(approval_error(format!("approval store write failed: {error}"))),
        }
    }

    async fn get_request(&self, id: &str) -> Result<Option<ApprovalRequest>> {
        self.read_request(id).await?.map(|doc| doc.request()).transpose()
    }

    async fn replace_request(
        &self,
        request: &ApprovalRequest,
        expected_version: u64,
    ) -> Result<bool> {
        let doc = RequestDoc::new(request)?;
        let Ok(expected) = i64::try_from(expected_version) else {
            return Ok(false);
        };
        let shared = Arc::new(doc);
        self.db
            .run_transaction_with_options(
                move |db, transaction| -> TxFuture<'_, bool> {
                    let doc = shared.clone();
                    Box::pin(async move {
                        let fail = |e: FirestoreError| {
                            approval_error(format!("approval store replace failed: {e}"))
                        };
                        let doc_id = request_doc_id(&doc.id);
                        let stored: Option<RequestDoc> = db
                            .fluent()
                            .select()
                            .by_id_in(APPROVAL_REQUESTS)
                            .obj::<RequestDoc>()
                            .one(&doc_id)
                            .await
                            .map_err(|e| retry_or_fail(e, fail))?;
                        if !stored
                            .is_some_and(|stored| stored.id == doc.id && stored.version == expected)
                        {
                            return Ok(false);
                        }
                        db.fluent()
                            .update()
                            .in_col(APPROVAL_REQUESTS)
                            .document_id(&doc_id)
                            .object(&*doc)
                            .add_to_transaction(transaction)
                            .map_err(|e| retry_or_fail(e, fail))?;
                        Ok(true)
                    })
                },
                transaction_options(),
            )
            .await
            .map_err(|e| transaction_error(e, approval_error))
    }

    async fn list_requests(&self, query: &ApprovalRequestQuery) -> Result<Vec<ApprovalRequest>> {
        let single_state = match query.states.as_slice() {
            [state] => Some(state.as_str()),
            _ => None,
        };
        let app = query.app_name.clone();
        let open_only = query.open_only;
        let docs: Vec<RequestDoc> = self
            .db
            .fluent()
            .select()
            .from(APPROVAL_REQUESTS)
            .filter(move |q| {
                q.for_all([
                    app.as_deref().and_then(|app| q.field("app").eq(app)),
                    open_only.then(|| q.field("open").eq(true)).flatten(),
                    single_state.and_then(|state| q.field("state").eq(state)),
                ])
            })
            .obj::<RequestDoc>()
            .query()
            .await
            .map_err(|e| approval_error(format!("approval store query failed: {e}")))?;
        let mut requests = Vec::new();
        for doc in docs {
            let request = doc.request()?;
            if query.matches(&request) {
                requests.push(request);
            }
        }
        requests.sort_by(|a, b| a.created_at.cmp(&b.created_at).then_with(|| a.id.cmp(&b.id)));
        requests.truncate(query.limit);
        Ok(requests)
    }

    async fn put_delegation(&self, delegation: &ApprovalDelegation) -> Result<()> {
        let doc = DelegationDoc {
            id: delegation.id.clone(),
            to_kind: subject_kind(&delegation.to)?,
            to_id: delegation.to.id.clone(),
            record: to_json(delegation, approval_error)?,
        };
        self.db
            .fluent()
            .update()
            .in_col(APPROVAL_DELEGATIONS)
            .document_id(delegation_doc_id(&delegation.id))
            .object(&doc)
            .execute::<DelegationDoc>()
            .await
            .map_err(|e| approval_error(format!("approval store write failed: {e}")))?;
        Ok(())
    }

    async fn delegations_to(&self, to: &SubjectRef) -> Result<Vec<ApprovalDelegation>> {
        let kind = subject_kind(to)?;
        let docs: Vec<DelegationDoc> = self
            .db
            .fluent()
            .select()
            .from(APPROVAL_DELEGATIONS)
            .filter(|q| {
                q.for_all([
                    q.field("to_kind").eq(kind.as_str()),
                    q.field("to_id").eq(to.id.as_str()),
                ])
            })
            .obj::<DelegationDoc>()
            .query()
            .await
            .map_err(|e| approval_error(format!("approval store query failed: {e}")))?;
        let mut delegations = docs
            .iter()
            .filter(|doc| doc.to_kind == kind && doc.to_id == to.id)
            .map(|doc| from_json::<ApprovalDelegation>(&doc.record, "delegation", approval_error))
            .collect::<Result<Vec<_>>>()?;
        delegations.sort_by(|a, b| a.id.cmp(&b.id));
        Ok(delegations)
    }

    async fn delete_delegation(&self, id: &str) -> Result<bool> {
        if self.read_delegation(id).await?.is_none() {
            return Ok(false);
        }
        match self
            .db
            .fluent()
            .delete()
            .from(APPROVAL_DELEGATIONS)
            .document_id(delegation_doc_id(id))
            .precondition(FirestoreWritePrecondition::Exists(true))
            .execute()
            .await
        {
            Ok(()) => Ok(true),
            // Deleted concurrently between the read and the delete.
            Err(FirestoreError::DataNotFoundError(_)) => Ok(false),
            Err(error) => Err(approval_error(format!("approval store delete failed: {error}"))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use firestore::errors::{FirestoreDatabaseError, FirestoreErrorPublicGenericDetails};
    use std::sync::atomic::{AtomicUsize, Ordering};

    /// The error the Firestore emulator returns when a contended lock times out.
    fn aborted(retry_possible: bool) -> FirestoreError {
        FirestoreError::DatabaseError(FirestoreDatabaseError {
            public: FirestoreErrorPublicGenericDetails { code: "Aborted".to_string() },
            details: "Transaction lock timeout.".to_string(),
            retry_possible,
        })
    }

    #[tokio::test]
    async fn a_write_firestore_aborted_runs_again_until_it_succeeds() {
        let attempts = AtomicUsize::new(0);
        let result = retry_while(is_retryable, || async {
            if attempts.fetch_add(1, Ordering::SeqCst) < 3 { Err(aborted(true)) } else { Ok(7) }
        })
        .await;
        assert_eq!((result.ok(), attempts.into_inner()), (Some(7), 4));
    }

    #[tokio::test]
    async fn a_failure_firestore_does_not_retry_is_returned_at_once() {
        let attempts = AtomicUsize::new(0);
        let result: std::result::Result<(), _> = retry_while(is_retryable, || async {
            attempts.fetch_add(1, Ordering::SeqCst);
            Err(aborted(false))
        })
        .await;
        assert!(matches!(result, Err(FirestoreError::DatabaseError(_))), "{result:?}");
        assert_eq!(attempts.into_inner(), 1);
    }

    fn snapshot(org: &str) -> OrgSnapshot {
        let total = |agent: Option<&str>, vendor: Option<&str>, micro_usd| AttributedTotal {
            agent: agent.map(str::to_string),
            vendor: vendor.map(str::to_string),
            micro_usd,
        };
        OrgSnapshot {
            org: OrgDoc {
                org: org.to_string(),
                seq: 3,
                lifetime: vec![total(Some("a"), Some("v"), 500), total(None, None, 7)],
            },
            month: vec![
                DayDoc { day: "2026-10-01".into(), totals: vec![total(Some("a"), Some("v"), 40)] },
                DayDoc { day: "2026-10-15".into(), totals: vec![total(Some("a"), None, 5)] },
            ],
            holds: vec![
                EntryDoc {
                    org: org.to_string(),
                    agent: Some("a".into()),
                    vendor: Some("v".into()),
                    state: STATE_RESERVED.into(),
                    reserved_micro_usd: 11,
                    committed_micro_usd: None,
                    created_at_us: 0,
                    expires_at_us: 100,
                    settled_at_us: None,
                },
                EntryDoc {
                    org: org.to_string(),
                    agent: Some("b".into()),
                    vendor: None,
                    state: STATE_RESERVED.into(),
                    reserved_micro_usd: 13,
                    committed_micro_usd: None,
                    created_at_us: 0,
                    expires_at_us: 10,
                    settled_at_us: None,
                },
            ],
        }
    }

    #[test]
    fn windows_sum_the_covered_totals() {
        let snapshot = snapshot("acme");
        let org = SpendKey::org("acme");
        let vendor = SpendKey::org("acme").with_vendor("v");
        assert_eq!(snapshot.committed(&org, SpendPeriod::Day, "2026-10-15"), 5);
        assert_eq!(snapshot.committed(&org, SpendPeriod::Month, "2026-10-15"), 45);
        assert_eq!(snapshot.committed(&org, SpendPeriod::Lifetime, "2026-10-15"), 507);
        assert_eq!(snapshot.committed(&vendor, SpendPeriod::Month, "2026-10-15"), 40);
        assert_eq!(snapshot.committed(&SpendKey::org("other"), SpendPeriod::Lifetime, ""), 0);
    }

    #[test]
    fn only_live_covered_holds_count() {
        let snapshot = snapshot("acme");
        assert_eq!(snapshot.held(&SpendKey::org("acme"), 5), 24);
        assert_eq!(snapshot.held(&SpendKey::org("acme"), 50), 11, "the second hold expired");
        assert_eq!(snapshot.held(&SpendKey::org("acme").with_agent("b"), 50), 0);
    }

    #[test]
    fn totals_accumulate_per_attribution() {
        let mut totals = Vec::new();
        let key = SpendKey::org("acme").with_agent("a");
        add_to_totals(&mut totals, &key, 10);
        add_to_totals(&mut totals, &key, 5);
        add_to_totals(&mut totals, &SpendKey::org("acme"), 1);
        assert_eq!(
            totals,
            vec![
                AttributedTotal { agent: Some("a".into()), vendor: None, micro_usd: 15 },
                AttributedTotal { agent: None, vendor: None, micro_usd: 1 },
            ]
        );
    }

    #[test]
    fn document_ids_are_path_safe_and_scope_bound() {
        let scope = ApprovalScope::new("app/1", "user:1", "session/x");
        let id = approval_id(&scope, KIND_DECISION, "transfer({\"a\":1})");
        assert!(!id.contains('/'));
        assert_ne!(id, approval_id(&scope, KIND_PENDING, "transfer({\"a\":1})"));
        assert!(!action_id("app/user/session/inv/call").contains('/'));
        assert_eq!(hex("a/b"), "612f62");
        let doc = ApprovalDoc {
            app_name: "app/1".into(),
            user_id: "user:1".into(),
            session_id: "session/x".into(),
            kind: KIND_DECISION.into(),
            fingerprint: "f".into(),
            payload: String::new(),
        };
        assert!(doc.is_for(&scope, KIND_DECISION, "f"));
        assert!(!doc.is_for(&scope, KIND_DECISION, "g"), "a digest collision does not match");
        assert!(!doc.is_for(&ApprovalScope::new("app/1", "user:1", "other"), KIND_DECISION, "f"));
    }

    #[test]
    fn day_ids_sort_by_date() {
        let early = day_id(NaiveDate::from_ymd_opt(2026, 9, 30).expect("date"));
        let late = day_id(NaiveDate::from_ymd_opt(2026, 10, 1).expect("date"));
        assert_eq!(early, "2026-09-30");
        assert!(early < late);
    }
}