tollgate-client 0.32.1

Instance-side quota runtime: background lease refill into the admission layer's LeaseSlot, and a bounded, batched, shed-on-overflow usage writer.
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
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
//! Behavior tests for the lease manager and usage writer (INVARIANTS.md GL-5,
//! GL-6, GL-8, GL-9's client half).
use std::num::NonZeroUsize;
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, AtomicUsize, Ordering};

use jiff::{SignedDuration, Timestamp};

use tollgate_admission::{
    AdmissionEngine, ArcSwapSnapshotMap, LeaseSlot, NoCapacityPermit, NoGate, ReadyToStart,
    SnapshotMap,
};
use tollgate_client::{
    Clock, LeaseManager, LeaseManagerConfig, ManualClock, UsagePermit, UsageWriter,
    UsageWriterConfig,
};
use tollgate_core::{
    AccountId, AccountSnapshot, AccountStatus, CapacityClass, CostTable, CostUnits, DenyReason,
    Generation, LeaseGrant, LocalLease, LocalSharding, OpIndex, PermissionBits, PolicyRevision,
    Principal, RequestId, ResolvedLimits, UsageEvent, UsageSource,
};
#[path = "../../tollgate-store/tests/support/delegating.rs"]
mod delegating;
use delegating::{DelegatingStore, RejectingStore, rejecting};

use tollgate_store::{
    AccountConfig, GrantPolicy, IngestError, IngestReport, LeaseAllocator, MemoryStore,
    ReclaimBatch, StoreError, UsageSink,
};

const ACCOUNT: AccountId = AccountId(1);
const PRINCIPAL: Principal = Principal(1);

#[derive(Clone, Copy)]
struct Operation;

impl OpIndex for Operation {
    fn index(&self) -> usize {
        0
    }
}

fn t(secs: i64) -> Timestamp {
    Timestamp::from_second(secs).unwrap()
}

fn ready_from_grant(
    grant: LeaseGrant,
    permit: UsagePermit,
) -> ReadyToStart<UsagePermit, NoCapacityPermit> {
    let engine = AdmissionEngine::new(ArcSwapSnapshotMap::new());
    let snapshot = Arc::new(
        AccountSnapshot::builder(
            ACCOUNT,
            Generation(1),
            AccountStatus::Active,
            t(1_000),
            PermissionBits::bit(0),
            ResolvedLimits::new(64),
            Arc::new(
                CostTable::builder(CostUnits(50), CostUnits(50))
                    .weight(&Operation, CostUnits(1))
                    .build(),
            ),
        )
        .build(),
    );
    let slot = LeaseSlot::for_account(ACCOUNT);
    drop(slot.replace(Arc::new(LocalLease::new(grant, CostUnits::ZERO))));
    engine.map().install(PRINCIPAL, snapshot, slot).unwrap();
    engine
        .begin(PRINCIPAL, PermissionBits::bit(0), t(0))
        .and_then(|context| context.admit(&[(&Operation, 1)], permit, t(0)))
        .expect("the fixture lease funds one request")
        .acquire_capacity(&NoGate)
        .expect("capacity is disabled in this fixture")
}

fn store(balance: u64) -> Arc<MemoryStore> {
    let store = MemoryStore::new(GrantPolicy {
        shrink_divisor: 1,
        min_grant: CostUnits(1),
        max_ttl: SignedDuration::from_secs(3_600),
        reclaim_grace: SignedDuration::ZERO,
    })
    .unwrap();
    store.create_account(AccountConfig {
        account_id: ACCOUNT,
        initial_balance: CostUnits(balance),
        status: AccountStatus::Active,
        capacity_class: CapacityClass::Assured,
    });
    store
}

fn manager_config() -> LeaseManagerConfig {
    LeaseManagerConfig {
        account: ACCOUNT,
        target_grant: CostUnits(1_000),
        low_water: CostUnits(250),
        lease_ttl: SignedDuration::from_secs(60),
        expiry_safety_margin: SignedDuration::ZERO,
        poll_interval: std::time::Duration::from_millis(5),
        store_call_timeout: std::time::Duration::from_secs(5),
        shutdown_release_deadline: std::time::Duration::from_secs(10),
    }
}

async fn settle() {
    // Paused-clock runtimes auto-advance: this lets background tasks run
    // through several poll intervals deterministically.
    tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}

#[tokio::test(start_paused = true)]
async fn refill_installs_lease_on_cold_start() {
    let store = store(10_000);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager =
        LeaseManager::spawn(store.clone(), Arc::clone(&slot), clock, manager_config()).unwrap();

    settle().await;
    let lease = slot.load().expect("lease installed");
    assert_eq!(lease.grant().units, CostUnits(1_000));
    manager.shutdown().await;
}

/// Issue GL-10: refill used to begin only when the poll timer fired, so a burst
/// could drain a lease between ticks and deny against an account that is
/// funded. The poll interval here is a minute — far longer than the test runs
/// — so a tick cannot explain the rotation. Only the crossing debit can.
#[tokio::test(start_paused = true)]
async fn refill_begins_on_the_crossing_debit_not_the_next_tick() {
    let store = store(10_000);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager = LeaseManager::spawn(
        store.clone(),
        Arc::clone(&slot),
        clock,
        LeaseManagerConfig {
            poll_interval: std::time::Duration::from_secs(60),
            ..manager_config()
        },
    )
    .unwrap();

    // The cold-start acquire still comes from the interval's immediate first
    // tick — there is no lease to spend, so nothing could have signalled.
    settle().await;
    let first = slot.load().expect("cold start installs a lease");
    let first_id = first.grant().lease_id;

    // Cross low water: 1000 - 800 = 200, at or below the 250 mark.
    first.try_debit(CostUnits(800), t(0)).unwrap();
    assert!(first.needs_refill());
    drop(first);

    settle().await;
    let second = slot.load().expect("a replacement must be installed");
    assert_ne!(
        second.grant().lease_id,
        first_id,
        "the crossing debit must have started a refill well inside the \
         60s poll interval; on the polling-only design this is still the \
         original lease"
    );
    manager.shutdown().await;
}

/// The user-visible bug, and the witness for INVARIANTS.md GL-6: sustained
/// spending against a funded account must not start denying just because a
/// rotation fell due between ticks.
#[tokio::test(start_paused = true)]
async fn a_burst_across_a_rotation_never_denies_a_funded_account() {
    let store = store(1_000_000);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager = LeaseManager::spawn(
        store.clone(),
        Arc::clone(&slot),
        clock,
        LeaseManagerConfig {
            // Long enough that polling alone cannot keep this burst funded:
            // the 1000-unit grants below are spent in ~13 iterations.
            poll_interval: std::time::Duration::from_secs(60),
            ..manager_config()
        },
    )
    .unwrap();
    settle().await;

    let mut denied = 0;
    let mut spent = 0u64;
    for _ in 0..200 {
        match slot.load() {
            Some(lease) => match lease.try_debit(CostUnits(75), t(0)) {
                Ok(()) => spent += 75,
                Err(_) => denied += 1,
            },
            None => denied += 1,
        }
        // Let the refill task act on any request the debit above raised.
        tokio::time::sleep(std::time::Duration::from_millis(1)).await;
    }

    assert_eq!(
        denied, 0,
        "a funded account was refused {denied} times across rotations"
    );
    assert_eq!(spent, 200 * 75);
    manager.shutdown().await;
}

#[tokio::test(start_paused = true)]
async fn adaptive_tail_grant_does_not_rotate_while_unspent() {
    let store = MemoryStore::new(GrantPolicy::default()).unwrap();
    store.create_account(AccountConfig {
        account_id: ACCOUNT,
        initial_balance: CostUnits(50),
        status: AccountStatus::Active,
        capacity_class: CapacityClass::Assured,
    });
    let clock = Arc::new(ManualClock::new(t(0)));
    let slot = LeaseSlot::for_account(ACCOUNT);
    let manager = LeaseManager::spawn(
        store.clone(),
        Arc::clone(&slot),
        clock,
        LeaseManagerConfig {
            target_grant: CostUnits(500),
            low_water: CostUnits(100),
            ..manager_config()
        },
    )
    .unwrap();
    tokio::time::sleep(std::time::Duration::from_millis(25)).await;
    let first = slot.load().unwrap();
    assert_eq!(first.grant().units, CostUnits(25));
    let lease_id = first.grant().lease_id;
    drop(first);

    tokio::time::sleep(std::time::Duration::from_millis(100)).await;
    assert_eq!(slot.load().unwrap().grant().lease_id, lease_id);
    manager.shutdown().await;
}

#[tokio::test(start_paused = true)]
async fn rotation_at_low_water_installs_fresh_lease() {
    let store = store(10_000);
    let slot = LeaseSlot::with_sharding(ACCOUNT, LocalSharding::new(NonZeroUsize::new(8).unwrap()));
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager =
        LeaseManager::spawn(store.clone(), Arc::clone(&slot), clock, manager_config()).unwrap();
    settle().await;

    let first = slot.load().unwrap();
    // Spend down to the low-water mark; the manager should rotate.
    first.try_debit(CostUnits(800), t(0)).unwrap();
    assert!(first.needs_refill());
    settle().await;

    let second = slot.load().unwrap();
    assert_ne!(first.grant().lease_id, second.grant().lease_id);
    assert!(second.grant().fencing_token > first.grant().fencing_token);

    // While this test still holds an Arc to the superseded lease (standing in
    // for an in-flight reservation), the manager must NOT release it: its
    // remaining count is not final yet.
    assert_eq!(first.remaining(), CostUnits(200));
    settle().await;
    assert_eq!(store.balance(ACCOUNT), CostUnits(8_000)); // two 1000 grants out

    // Once the last outside Arc drops, the lease has quiesced and its 200
    // unspent units flow back to the account.
    drop(first);
    settle().await;
    assert_eq!(store.balance(ACCOUNT), CostUnits(8_200));
    manager.shutdown().await;
}

#[tokio::test(start_paused = true)]
async fn expired_slot_fails_closed_then_recovers() {
    // 2000 units: the manager's 1000 grant plus 1000 for the drain lease
    // that starves re-acquisition.
    let store = store(2_000);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager = LeaseManager::spawn(
        store.clone(),
        Arc::clone(&slot),
        Arc::clone(&clock) as Arc<dyn Clock>,
        manager_config(),
    )
    .unwrap();
    settle().await;
    assert!(slot.load().is_some());

    // Drain the account so the manager cannot re-acquire, then expire the
    // held lease: the slot must clear (deny) rather than serve a dead lease.
    let drain = store
        .acquire(
            ACCOUNT,
            CostUnits(u64::MAX),
            SignedDuration::from_secs(3_600),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    clock.set(t(120)); // past the manager lease's 60s TTL
    settle().await;
    assert!(slot.load().is_none(), "expired slot must fail closed");

    // Balance returns (the drain lease is released) — recovery is automatic.
    store
        .release(drain.lease_id, drain.fencing_token, drain.units, t(121))
        .await
        .unwrap();
    settle().await;
    assert!(
        slot.load().is_some(),
        "manager recovers once balance exists"
    );
    manager.shutdown().await;
}

#[tokio::test(start_paused = true)]
async fn usability_window_rollover_returns_unspent_capacity() {
    let store = MemoryStore::new(GrantPolicy {
        shrink_divisor: 1,
        min_grant: CostUnits(1),
        max_ttl: SignedDuration::from_secs(3_600),
        reclaim_grace: SignedDuration::from_secs(30),
    })
    .unwrap();
    store.create_account(AccountConfig {
        account_id: ACCOUNT,
        initial_balance: CostUnits(1_000),
        status: AccountStatus::Active,
        capacity_class: CapacityClass::Assured,
    });
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let mut config = manager_config();
    config.expiry_safety_margin = SignedDuration::from_secs(5);
    let manager = LeaseManager::spawn(
        store.clone(),
        Arc::clone(&slot),
        Arc::clone(&clock) as Arc<dyn Clock>,
        config,
    )
    .unwrap();
    settle().await;
    let first = slot.load().unwrap().grant().lease_id;

    clock.set(t(55));
    settle().await;
    let replacement = slot.load().expect("capacity should rotate during grace");
    assert_ne!(replacement.grant().lease_id, first);
    assert_eq!(replacement.remaining(), CostUnits(1_000));
    manager.shutdown().await;
}

#[test]
fn invalid_lease_manager_durations_are_rejected() {
    for ttl in [SignedDuration::ZERO, SignedDuration::from_nanos(-1)] {
        let mut config = manager_config();
        config.lease_ttl = ttl;
        config.expiry_safety_margin = SignedDuration::ZERO;
        assert!(config.validate().is_err());
    }
    let mut config = manager_config();
    config.expiry_safety_margin = SignedDuration::from_secs(-1);
    assert!(config.validate().is_err());

    let mut config = manager_config();
    config.poll_interval = std::time::Duration::ZERO;
    assert!(config.validate().is_err());
}

#[test]
fn fractional_and_wide_lease_ttls_remain_valid_configuration() {
    for ttl in [
        SignedDuration::from_nanos(1),
        SignedDuration::from_millis(500),
        SignedDuration::from_millis(1_500),
        SignedDuration::from_secs(i64::from(u32::MAX) + 1),
        SignedDuration::MAX,
    ] {
        let mut config = manager_config();
        config.lease_ttl = ttl;
        config.expiry_safety_margin = SignedDuration::ZERO;
        assert_eq!(config.validate(), Ok(()), "TTL {ttl}");
    }
}

#[tokio::test(start_paused = true)]
async fn shutdown_releases_unspent_units() {
    let store = store(10_000);
    let slot = LeaseSlot::with_sharding(ACCOUNT, LocalSharding::new(NonZeroUsize::new(8).unwrap()));
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager =
        LeaseManager::spawn(store.clone(), Arc::clone(&slot), clock, manager_config()).unwrap();
    let health = manager.health();
    settle().await;
    slot.load()
        .unwrap()
        .try_debit(CostUnits(300), t(0))
        .unwrap();

    manager.shutdown().await;
    assert!(!*health.borrow());
    assert!(health.has_changed().is_err());
    assert!(slot.load().is_none());
    // 10_000 - 1_000 grant + 700 released = 9_700; the 300 spent stay out
    // (settlement loss until usage lands — callers flush first in real use).
    assert_eq!(store.balance(ACCOUNT), CostUnits(9_700));
}

fn event(request: u128, units: u64, lease: &tollgate_core::LeaseGrant) -> UsageEvent {
    UsageEvent::new(
        RequestId(request),
        lease.account_id,
        UsageSource::Leased {
            lease_id: lease.lease_id,
            fencing_token: lease.fencing_token,
        },
        CostUnits(units),
        t(0),
        PolicyRevision::UNSTATED,
        None,
    )
}

fn writer_config(capacity: usize) -> UsageWriterConfig {
    UsageWriterConfig {
        queue_capacity: capacity,
        max_batch: 4,
        flush_interval: std::time::Duration::from_millis(10),
        retry_backoff: std::time::Duration::from_millis(10),
        shutdown_drain_deadline: std::time::Duration::from_secs(60),
        ingest_timeout: std::time::Duration::from_secs(5),
    }
}

/// Issue GL-4: the refill task's numbers existed only as `tracing` events with
/// nothing to threshold on, and a shutdown report nobody sees while the
/// process runs. A refusal must be attributable to its reason — that is what
/// separates "the account is out of balance" from "the allocator is down".
#[tokio::test(start_paused = true)]
async fn refill_counters_attribute_refusals_to_their_reason() {
    // An account that does not exist: every acquire is refused, always the
    // same way.
    let store = MemoryStore::new(GrantPolicy::default()).unwrap();
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager =
        LeaseManager::spawn(store.clone(), Arc::clone(&slot), clock, manager_config()).unwrap();
    let counters = manager.counters();

    assert_eq!(counters.snapshot().refused(), 0, "nothing tried yet");
    settle().await;

    let stats = counters.snapshot();
    assert_eq!(stats.acquired, 0);
    assert_eq!(stats.acquired_units, 0);
    assert!(stats.refused() > 0, "the allocator refused every acquire");
    let refusals: Vec<_> = stats
        .refusals_by_name()
        .filter(|(_, count)| *count > 0)
        .map(|(name, _)| name)
        .collect();
    assert_eq!(
        refusals,
        vec!["unknown_account"],
        "one reason, and the others left alone: {:?}",
        stats.acquire_refused
    );
    assert_eq!(stats.acquire_timeouts, 0, "a refusal is not a timeout");

    manager.shutdown().await;
}

/// A healthy refill counts what it was granted, not just that it succeeded:
/// adaptive allocation can return less than `target_grant`, so the units are
/// the number that says whether the instance is actually being funded.
#[tokio::test(start_paused = true)]
async fn refill_counters_record_grants_and_releases() {
    let store = store(10_000);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager =
        LeaseManager::spawn(store.clone(), Arc::clone(&slot), clock, manager_config()).unwrap();
    let counters = manager.counters();
    settle().await;

    let stats = counters.snapshot();
    assert_eq!(stats.acquired, 1);
    assert_eq!(stats.acquired_units, 1_000, "the grant, not the request");
    assert_eq!(stats.refused(), 0);
    assert_eq!(stats.released, 0, "nothing returned while still serving");
    assert_eq!(stats.abandoned, 0);

    manager.shutdown().await;
    // Shutdown returns the lease, and the running counter says so too — the
    // report is no longer the only place that knows.
    assert_eq!(counters.snapshot().released, 1);
    assert_eq!(counters.snapshot().abandoned, 0);
}

/// Issue GL-38: the accounting numbers used to exist only as a local on the
/// writer task's stack, so a process that kept running — or died — reported
/// nothing. They are now readable at any time, and `shutdown` reports the very
/// same counters, so the running totals and the final report cannot disagree.
#[tokio::test(start_paused = true)]
async fn running_totals_are_readable_and_match_the_final_report() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(100)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(64)).unwrap();

    // Nothing has happened yet: an ingest age of `None` is how a freshly
    // started writer differs from one whose sink has gone quiet.
    let health = recorder.health();
    assert_eq!(health.stats, tollgate_client::WriterStats::ZERO);
    assert_eq!(health.last_ingest_at, None);
    assert_eq!(health.ingest_age(t(100)), None);
    assert_eq!(health.queue_capacity, 64);

    for i in 0..6u128 {
        let request = if i == 5 { 0 } else { i };
        recorder
            .try_reserve()
            .unwrap()
            .record(event(request, 10, &lease));
    }
    settle().await;

    // Read while the task is still running — the whole point of the issue.
    let mid_flight = recorder.health();
    assert_eq!(mid_flight.stats.accepted, 5);
    assert_eq!(mid_flight.stats.duplicate, 1);
    assert_eq!(mid_flight.stats.lost, 0);
    assert_eq!(
        mid_flight.unaccounted, 0,
        "every event has a billing outcome once flushed"
    );
    assert_eq!(
        mid_flight.last_ingest_at,
        Some(t(100)),
        "the sink answered, at the instant the batch was ingested with"
    );
    assert_eq!(
        mid_flight.ingest_age(t(160)),
        Some(SignedDuration::from_secs(60))
    );

    let stats = writer.shutdown().await.unwrap();
    assert_eq!(
        stats, mid_flight.stats,
        "the final report is a read of the same counters, not a second tally"
    );
}

/// Backpressure must be visible *before* it sheds, which is what the queue
/// gauge is for; and the shed itself is counted where it happens, so an
/// embedder cannot forget to.
#[tokio::test(start_paused = true)]
async fn queue_depth_rises_before_the_shed_and_sheds_are_counted() {
    let store = store(10_000);
    let clock = Arc::new(ManualClock::new(t(0)));
    // Capacity 2, and the permits are held rather than sent, so the queue
    // stays full and the writer cannot drain it.
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(2)).unwrap();

    assert_eq!(recorder.health().queue_depth, 0);
    let first = recorder.try_reserve().unwrap();
    assert_eq!(recorder.health().queue_depth, 1, "a held permit is depth");
    // The writer half reports the same queue, through a weak handle that must
    // not itself keep the channel open. Asserting the depth here — not just
    // that it is nonzero — pins the occupancy arithmetic on both sides.
    assert_eq!(writer.health().queue_depth, 1);
    assert_eq!(writer.health().queue_capacity, 2);

    let second = recorder.try_reserve().unwrap();
    let full = recorder.health();
    assert_eq!(full.queue_depth, full.queue_capacity);
    assert_eq!(full.shed, 0, "full is not yet shed");
    assert_eq!(writer.health().queue_depth, 2);

    assert_eq!(
        recorder.try_reserve().err(),
        Some(DenyReason::AccountingBackpressure)
    );
    assert_eq!(recorder.health().shed, 1);
    assert_eq!(
        recorder.try_reserve().err(),
        Some(DenyReason::AccountingBackpressure)
    );
    assert_eq!(recorder.health().shed, 2);

    drop(first);
    drop(second);
    // The permits were reserved and dropped, never sent: nothing was billed,
    // and a shed is not a charge that went missing.
    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats, tollgate_client::WriterStats::ZERO);
}

/// The signal that separates "quiet" from "the sink has been down for twenty
/// minutes": a failing sink must not advance the last-answered time, however
/// many attempts it makes.
#[tokio::test(start_paused = true)]
async fn a_failing_sink_does_not_advance_the_last_ingest_time() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    // Never recovers on its own: the outage lasts until this test ends it.
    let failures_left = Arc::new(AtomicU32::new(u32::MAX));
    let sink = flaky_sink(&store.clone(), &failures_left);
    let clock = Arc::new(ManualClock::new(t(500)));
    let (recorder, writer) =
        UsageWriter::spawn(sink.clone(), clock.clone(), writer_config(64)).unwrap();

    recorder.try_reserve().unwrap().record(event(1, 10, &lease));
    // Twenty minutes of retries, all failing.
    settle().await;
    clock.advance(SignedDuration::from_secs(1_200));
    settle().await;
    let during = recorder.health();
    assert_eq!(
        during.last_ingest_at, None,
        "a sink that has never answered leaves no timestamp to age"
    );
    assert_eq!(during.stats.accepted, 0);
    assert_eq!(
        during.unaccounted, 1,
        "the charge is still in the queue with no billing outcome"
    );

    // Recovery: the sink answers, and only then does the time advance.
    failures_left.store(0, Ordering::Relaxed);
    settle().await;
    let after = recorder.health();
    assert_eq!(after.stats.accepted, 1);
    assert_eq!(after.last_ingest_at, Some(t(1_700)));
    assert_eq!(after.unaccounted, 0);

    // A recovered outage is a clean run by the time shutdown reports it —
    // which is exactly why the age above, not `lost`, is the runtime signal.
    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.accepted, 1);
    assert_eq!(stats.lost, 0);
}

/// `rejected` is bounded billing loss the sink has already refused, and the
/// issue singles it out because nothing surfaced it before shutdown. It is a
/// runtime signal now.
#[tokio::test(start_paused = true)]
async fn rejected_events_are_visible_while_running() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(64)).unwrap();

    // A capability token the allocator never attached to this lease: the
    // sink refuses the mismatch.
    let mut mismatched = event(1, 10, &lease);
    mismatched.source = UsageSource::Leased {
        lease_id: lease.lease_id,
        fencing_token: tollgate_core::FencingToken(lease.fencing_token.0 + 99),
    };
    recorder.try_reserve().unwrap().record(mismatched);
    settle().await;

    let health = recorder.health();
    assert_eq!(health.stats.rejected, 1);
    assert_eq!(health.stats.accepted, 0);
    assert_eq!(
        health.unaccounted, 0,
        "refused is still an outcome; it is not left unaccounted"
    );
    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.rejected, 1);
}

#[tokio::test(start_paused = true)]
async fn writer_flushes_batches_idempotently() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(64)).unwrap();

    // Ten events, one duplicated request id: nine accepted, one duplicate.
    for i in 0..10u128 {
        let request = if i == 9 { 0 } else { i };
        recorder
            .try_reserve()
            .unwrap()
            .record(event(request, 10, &lease));
    }
    settle().await;
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits(90));

    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.accepted, 9);
    assert_eq!(stats.duplicate, 1);
    assert_eq!(stats.lost, 0);
}

#[tokio::test(start_paused = true)]
async fn full_queue_sheds_before_admission() {
    let store = store(10_000);
    let clock = Arc::new(ManualClock::new(t(0)));
    // Capacity 2 with the writer effectively idle (nothing sent yet): two
    // permits reserve the whole queue; the third sheds.
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(2)).unwrap();
    let p1 = recorder.try_reserve().unwrap();
    let p2 = recorder.try_reserve().unwrap();
    assert_eq!(
        recorder.try_reserve().err(),
        Some(DenyReason::AccountingBackpressure)
    );
    // Dropping a permit (deny/cancel path) frees its slot.
    drop(p1);
    let _p3 = recorder.try_reserve().unwrap();
    drop(p2);
    writer.shutdown().await.unwrap();
}

/// A sink that fails its first N calls, then delegates to the store.
/// What a capped sink saw, shared with the test.
#[derive(Default)]
struct BatchCap {
    largest: AtomicUsize,
    ingested: AtomicUsize,
}

/// A sink that refuses any batch above `cap`. It has no inner store, so it
/// rejects anything it is not given rather than delegating.
fn batch_capped_sink(cap: usize, seen: &Arc<BatchCap>) -> Arc<DelegatingStore<RejectingStore>> {
    let seen = Arc::clone(seen);
    Arc::new(
        rejecting("a capped-sink fixture answers ingest and nothing else").on_ingest(
            move |_, events, _now| {
                let seen = Arc::clone(&seen);
                async move {
                    seen.largest.fetch_max(events.len(), Ordering::AcqRel);
                    if events.len() > cap {
                        return Err(StoreError("batch exceeds sink limit".into()).into());
                    }
                    seen.ingested.fetch_add(events.len(), Ordering::AcqRel);
                    Ok(IngestReport {
                        accepted: events.len() as u64,
                        ..IngestReport::default()
                    })
                }
            },
        ),
    )
}

#[tokio::test(start_paused = true)]
async fn steady_state_flushes_in_configured_batch_sizes() {
    let seen = Arc::new(BatchCap::default());
    let sink = batch_capped_sink(2, &seen);
    let grant = tollgate_core::LeaseGrant {
        lease_id: tollgate_core::LeaseId(1),
        account_id: ACCOUNT,
        fencing_token: tollgate_core::FencingToken(1),
        units: CostUnits(100),
        expires_at: t(100),
    };
    let (recorder, writer) = UsageWriter::spawn(
        Arc::clone(&sink) as Arc<dyn UsageSink>,
        Arc::new(ManualClock::new(t(0))),
        UsageWriterConfig {
            queue_capacity: 8,
            max_batch: 2,
            flush_interval: std::time::Duration::from_secs(60),
            retry_backoff: std::time::Duration::from_millis(1),
            shutdown_drain_deadline: std::time::Duration::from_secs(60),
            ingest_timeout: std::time::Duration::from_secs(5),
        },
    )
    .unwrap();
    // Let the writer receive one event into a partial batch before the rest
    // arrive. The next bulk receive must be limited to the one remaining
    // slot, even though more events are buffered.
    recorder.try_reserve().unwrap().record(event(0, 1, &grant));
    tokio::task::yield_now().await;
    for request in 1..4 {
        recorder
            .try_reserve()
            .unwrap()
            .record(event(request, 1, &grant));
    }

    settle().await;
    assert_eq!(seen.ingested.load(Ordering::Acquire), 4);
    assert_eq!(seen.largest.load(Ordering::Acquire), 2);

    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.accepted, 4);
    assert_eq!(stats.lost, 0);
}

#[tokio::test(start_paused = true)]
async fn shutdown_flushes_in_configured_batch_sizes() {
    let seen = Arc::new(BatchCap::default());
    let sink = batch_capped_sink(2, &seen);
    let grant = tollgate_core::LeaseGrant {
        lease_id: tollgate_core::LeaseId(1),
        account_id: ACCOUNT,
        fencing_token: tollgate_core::FencingToken(1),
        units: CostUnits(100),
        expires_at: t(100),
    };
    let (recorder, writer) = UsageWriter::spawn(
        Arc::clone(&sink) as Arc<dyn UsageSink>,
        Arc::new(ManualClock::new(t(0))),
        UsageWriterConfig {
            queue_capacity: 8,
            max_batch: 2,
            flush_interval: std::time::Duration::from_secs(60),
            retry_backoff: std::time::Duration::from_millis(1),
            shutdown_drain_deadline: std::time::Duration::from_secs(60),
            ingest_timeout: std::time::Duration::from_secs(5),
        },
    )
    .unwrap();
    for request in 0..6 {
        recorder
            .try_reserve()
            .unwrap()
            .record(event(request, 1, &grant));
    }

    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.accepted, 6);
    assert_eq!(stats.lost, 0);
    assert_eq!(seen.largest.load(Ordering::Acquire), 2);
}

/// Fails a fixed number of ingests, then behaves like the store it wraps.
fn flaky_sink(
    store: &Arc<MemoryStore>,
    failures_left: &Arc<AtomicU32>,
) -> Arc<DelegatingStore<MemoryStore>> {
    let failures_left = Arc::clone(failures_left);
    Arc::new(
        DelegatingStore::wrapping(Arc::clone(store)).on_ingest(move |inner, events, now| {
            let failures_left = Arc::clone(&failures_left);
            async move {
                if failures_left
                    .fetch_update(Ordering::AcqRel, Ordering::Acquire, |n| n.checked_sub(1))
                    .is_ok()
                {
                    return Err(StoreError("injected outage".into()).into());
                }
                inner.ingest(&events, now).await
            }
        }),
    )
}

#[tokio::test(start_paused = true)]
async fn writer_retries_through_outage_without_losing_events() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let failures_left = Arc::new(AtomicU32::new(3));
    let sink = flaky_sink(&store.clone(), &failures_left);
    let (recorder, writer) = UsageWriter::spawn(sink, clock, writer_config(64)).unwrap();

    recorder.try_reserve().unwrap().record(event(1, 25, &lease));
    settle().await;
    settle().await;
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits(25));
    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.accepted, 1);
    assert_eq!(stats.lost, 0);
}

/// Review finding GL-3 regression: a panic between commit and response must
/// still bill — the queue permit is the usage slot bound at admission, and
/// `Committed`'s drop emits into it during unwind.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn panic_after_commit_still_bills() {
    let store = store(10_000);
    let grant = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(8)).unwrap();

    let permit = recorder.try_reserve().unwrap();
    let worker = tokio::spawn(async move {
        let charge = ready_from_grant(grant, permit)
            .commit(RequestId(9), t(0))
            .expect("a live lease commits");
        assert_eq!(charge.units(), CostUnits(51));
        panic!("kernel exploded mid-execution");
    });
    assert!(worker.await.is_err(), "the worker must have panicked");

    // Shutdown drains and flushes whatever the unwind enqueued.
    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.accepted, 1);
    assert_eq!(stats.lost, 0);
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits(51));
}

/// The panic boundary a Rayon-style executor must own, exercised the way that
/// executor would: the kernel runs under `catch_unwind` on a plain thread, and
/// the guard is dropped there rather than leaked.
///
/// Tollgate never runs the kernel, so it cannot install this boundary itself —
/// which is exactly why the ownership is documented on `Committed`. What this
/// asserts is Tollgate's half of the contract: when the guard *is* dropped
/// during an unwind, emission still happens, because the event was built at
/// commit and `Drop` takes no lock and allocates nothing.
#[tokio::test]
async fn a_panicking_kernel_under_catch_unwind_still_bills() {
    let store = store(10_000);
    let grant = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(8)).unwrap();

    let permit = recorder.try_reserve().unwrap();
    let ready = ready_from_grant(grant, permit);

    // A consumer executor's shape: commit at execution start on a worker
    // thread, run the kernel inside a panic boundary, drop the guard there.
    let worker = std::thread::spawn(move || {
        std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
            let charge = ready
                .commit(RequestId(9), t(0))
                .expect("a live lease commits");
            assert_eq!(charge.units(), CostUnits(51));
            panic!("kernel exploded mid-execution");
        }))
    });
    let outcome = worker.join().expect("the boundary contains the panic");
    assert!(outcome.is_err(), "the kernel panicked");

    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.accepted, 1, "the committed charge was still emitted");
    assert_eq!(stats.lost, 0);
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits(51));
}

/// Review finding GL-2 regression: shutdown during an *ongoing* outage must
/// still terminate via the bounded final flush, not hang in the retry loop.
#[tokio::test(start_paused = true)]
async fn shutdown_during_outage_terminates_and_reports_loss() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    // Sink that never recovers.
    let failures_left = Arc::new(AtomicU32::new(u32::MAX));
    let sink = flaky_sink(&store.clone(), &failures_left);
    let (recorder, writer) = UsageWriter::spawn(sink, clock, writer_config(64)).unwrap();

    recorder.try_reserve().unwrap().record(event(1, 25, &lease));
    recorder.try_reserve().unwrap().record(event(2, 25, &lease));
    // Let the writer enter its retry loop against the dead sink.
    settle().await;

    // The whole point: this must complete (bounded final flush), not hang.
    let stats = tokio::time::timeout(std::time::Duration::from_secs(60), writer.shutdown())
        .await
        .expect("shutdown must terminate during an outage")
        .unwrap();
    assert_eq!(stats.lost, 2);
    assert_eq!(stats.accepted, 0);
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits::ZERO);
}

/// Companion: a shutdown signalled during the outage still delivers if the
/// sink recovers before the final flush runs out of attempts.
#[tokio::test(start_paused = true)]
async fn shutdown_after_recovery_delivers_everything() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    // Fails long enough to outlast several retry backoffs, then recovers in
    // time for the final flush.
    let failures_left = Arc::new(AtomicU32::new(4));
    let sink = flaky_sink(&store.clone(), &failures_left);
    let (recorder, writer) = UsageWriter::spawn(sink, clock, writer_config(64)).unwrap();
    recorder.try_reserve().unwrap().record(event(1, 25, &lease));
    settle().await;

    let stats = tokio::time::timeout(std::time::Duration::from_secs(60), writer.shutdown())
        .await
        .expect("shutdown must terminate")
        .unwrap();
    assert_eq!(stats.lost, 0);
    assert_eq!(stats.accepted, 1);
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits(25));
}

/// A lease-expiry straggler (GL-136): a holder that outlived an outage flushes
/// after its lease was swept, and the usage is billed against the forfeit
/// rather than dropped. Usage beyond what was forfeited is still rejected and
/// reported, never silently absorbed.
#[tokio::test(start_paused = true)]
async fn a_survivor_flushes_after_reclaim_and_is_billed() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    store.reclaim_expired(t(120)).await.unwrap();

    let clock = Arc::new(ManualClock::new(t(121)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(8)).unwrap();
    recorder.try_reserve().unwrap().record(event(1, 10, &lease));
    recorder
        .try_reserve()
        .unwrap()
        .record(event(2, 991, &lease));
    settle().await;
    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.accepted, 1);
    assert_eq!(stats.rejected, 1, "991 exceeds the 990 still forfeited");
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits(10));
    let c = store.conservation(ACCOUNT).unwrap();
    assert_eq!(c.settlement_loss, CostUnits(990));
    assert!(c.holds(), "{c:?}");
}

// ---- shutdown drain (issue GL-32) -------------------------------------------
//
// A permit reserved before shutdown must resolve — by sending or dropping —
// before the writer returns, bounded by the drain deadline; a deadline expiry
// is reported in `unresolved`, never a clean flush.

/// Shutdown refuses new reservations from the instant it begins, while a
/// permit reserved earlier still delivers into the drain (INVARIANTS.md GL-8).
#[tokio::test(start_paused = true)]
async fn reserve_fails_once_shutdown_begins() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(8)).unwrap();

    let held = recorder.try_reserve().unwrap();
    let shutdown = tokio::spawn(writer.shutdown());
    settle().await; // the writer has closed the channel and is draining

    assert_eq!(
        recorder.try_reserve().err(),
        Some(DenyReason::AccountingBackpressure)
    );
    assert!(recorder.is_closed(), "readiness must observe the shutdown");

    held.record(event(1, 25, &lease));
    let stats = shutdown.await.unwrap().unwrap();
    assert_eq!(stats.accepted, 1);
    assert_eq!(stats.unresolved, 0);
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits(25));
}

/// The GL-32 defect: a permit that records only after shutdown has begun must
/// be ingested, not silently dropped with zero reported loss.
#[tokio::test(start_paused = true)]
async fn shutdown_waits_for_outstanding_permit() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(8)).unwrap();

    let permit = recorder.try_reserve().unwrap();
    let late = event(1, 25, &lease);
    let sender = tokio::spawn(async move {
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        permit.record(late);
    });

    let stats = writer.shutdown().await.unwrap();
    sender.await.unwrap();
    assert_eq!(stats.accepted, 1);
    assert_eq!(stats.lost, 0);
    assert_eq!(stats.unresolved, 0);
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits(25));
}

/// INVARIANTS.md GL-13 across shutdown: a committed guard dropped after
/// shutdown begins still bills.
#[tokio::test(start_paused = true)]
async fn shutdown_waits_for_committed_guard() {
    let store = store(10_000);
    let grant = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(8)).unwrap();

    let permit = recorder.try_reserve().unwrap();
    let holder = tokio::spawn(async move {
        let charge = ready_from_grant(grant, permit)
            .commit(RequestId(9), t(0))
            .expect("a live lease commits");
        assert_eq!(charge.units(), CostUnits(51));
        // The guard outlives the start of shutdown; its drop must still bill.
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        drop(charge);
    });

    let stats = writer.shutdown().await.unwrap();
    holder.await.unwrap();
    assert_eq!(stats.accepted, 1);
    assert_eq!(stats.unresolved, 0);
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits(51));
}

/// A permit dropped (zero-charge path) after shutdown begins completes the
/// drain — no event, no hang, nothing unresolved.
#[tokio::test(start_paused = true)]
async fn late_permit_drop_completes_drain() {
    let store = store(10_000);
    let clock = Arc::new(ManualClock::new(t(0)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(8)).unwrap();

    let permit = recorder.try_reserve().unwrap();
    let dropper = tokio::spawn(async move {
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        drop(permit);
    });

    let stats = writer.shutdown().await.unwrap();
    dropper.await.unwrap();
    assert_eq!(stats, tollgate_client::WriterStats::ZERO);
}

/// Deadline expiry can neither hang nor report a clean flush: a permit that
/// never resolves is counted unresolved.
#[tokio::test(start_paused = true)]
async fn drain_deadline_expiry_reports_unresolved() {
    let store = store(10_000);
    let clock = Arc::new(ManualClock::new(t(0)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(8)).unwrap();

    // A permit that never sends and never drops.
    std::mem::forget(recorder.try_reserve().unwrap());

    let stats = tokio::time::timeout(std::time::Duration::from_secs(120), writer.shutdown())
        .await
        .expect("shutdown must return at the drain deadline, not hang")
        .unwrap();
    assert_eq!(stats.unresolved, 1);
    assert_eq!(stats.lost, 0);
    assert_eq!(stats.accepted, 0);
}

/// The drain's own accounting: duplicates and rejections discovered by the
/// *final* flush are counted as such, not folded into accepted or lost.
#[tokio::test(start_paused = true)]
async fn final_flush_counts_duplicates_and_rejections() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let mut config = writer_config(16);
    // Nothing flushes before shutdown, so every event meets the sink for the
    // first time inside the final flush.
    config.flush_interval = std::time::Duration::from_secs(3_600);
    config.max_batch = 16;
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, config).unwrap();

    recorder.try_reserve().unwrap().record(event(1, 10, &lease));
    recorder.try_reserve().unwrap().record(event(1, 10, &lease));
    let mut mismatched = event(2, 10, &lease);
    mismatched.source = UsageSource::Leased {
        lease_id: lease.lease_id,
        fencing_token: tollgate_core::FencingToken(999),
    };
    recorder.try_reserve().unwrap().record(mismatched);

    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.accepted, 1);
    assert_eq!(stats.duplicate, 1);
    assert_eq!(stats.rejected, 1);
    assert_eq!(stats.lost, 0);
    assert_eq!(stats.unresolved, 0);
}

/// The final flush backs off *between* attempts and never sleeps a backoff
/// no attempt can follow: three attempts, two waits.
#[tokio::test(start_paused = true)]
async fn final_flush_backs_off_only_between_attempts() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let failures_left = Arc::new(AtomicU32::new(u32::MAX));
    let sink = flaky_sink(&store.clone(), &failures_left);
    let backoff = std::time::Duration::from_millis(100);
    let mut config = writer_config(16);
    config.flush_interval = std::time::Duration::from_secs(3_600);
    config.retry_backoff = backoff;
    let (recorder, writer) = UsageWriter::spawn(sink, clock, config).unwrap();
    recorder.try_reserve().unwrap().record(event(1, 10, &lease));

    let start = tokio::time::Instant::now();
    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.lost, 1);
    assert_eq!(start.elapsed(), 2 * backoff, "three attempts, two backoffs");
}

/// Every unresolved permit is counted, not merely detected.
#[tokio::test(start_paused = true)]
async fn drain_deadline_reports_every_unresolved_permit() {
    let store = store(10_000);
    let clock = Arc::new(ManualClock::new(t(0)));
    let (recorder, writer) = UsageWriter::spawn(store.clone(), clock, writer_config(8)).unwrap();

    std::mem::forget(recorder.try_reserve().unwrap());
    std::mem::forget(recorder.try_reserve().unwrap());

    let stats = tokio::time::timeout(std::time::Duration::from_secs(120), writer.shutdown())
        .await
        .expect("shutdown must return at the drain deadline")
        .unwrap();
    assert_eq!(stats.unresolved, 2);
}

// ---- a dead writer never reports zero loss (issue GL-41) --------------------

/// A sink that panics once it has been called `panic_after` times.
/// Panics once its budget of clean calls runs out.
fn panicking_sink(
    store: &Arc<MemoryStore>,
    calls_before_panic: u32,
) -> Arc<DelegatingStore<MemoryStore>> {
    let budget = Arc::new(AtomicU32::new(calls_before_panic));
    Arc::new(
        DelegatingStore::wrapping(Arc::clone(store)).on_ingest(move |inner, events, now| {
            let budget = Arc::clone(&budget);
            async move {
                if budget
                    .fetch_update(Ordering::AcqRel, Ordering::Acquire, |n| n.checked_sub(1))
                    .is_err()
                {
                    panic!("sink exploded mid-ingest");
                }
                inner.ingest(&events, now).await
            }
        }),
    )
}

/// The GL-41 defect: a writer that dies holding committed charges must say so,
/// not return a zeroed report indistinguishable from a clean shutdown.
#[tokio::test(start_paused = true)]
async fn panicked_writer_reports_unaccounted_charges() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let sink = panicking_sink(&store.clone(), 0);
    let mut config = writer_config(16);
    config.flush_interval = std::time::Duration::from_secs(3_600);
    let (recorder, writer) = UsageWriter::spawn(sink, clock, config).unwrap();

    for request in 0..3u128 {
        recorder
            .try_reserve()
            .unwrap()
            .record(event(request, 10, &lease));
    }

    let error = writer
        .shutdown()
        .await
        .expect_err("a panicked writer must not report a clean shutdown");
    assert!(error.panicked);
    assert_eq!(error.unaccounted, 3);
    assert!(
        error.to_string().contains("no billing record"),
        "operator-facing message must name the consequence: {error}"
    );
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits::ZERO);
}

/// The count is what is still unaccounted, not everything ever enqueued:
/// charges already given a billing outcome are excluded.
#[tokio::test(start_paused = true)]
async fn panic_after_partial_flush_counts_only_unflushed() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    // The first batch is ingested; the second kills the task.
    let sink = panicking_sink(&store.clone(), 1);
    let mut config = writer_config(16);
    config.max_batch = 2;
    config.flush_interval = std::time::Duration::from_secs(3_600);
    let (recorder, writer) = UsageWriter::spawn(sink, clock, config).unwrap();

    for request in 0..2u128 {
        recorder
            .try_reserve()
            .unwrap()
            .record(event(request, 10, &lease));
    }
    settle().await; // first full batch flushes cleanly
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits(20));

    for request in 2..5u128 {
        recorder
            .try_reserve()
            .unwrap()
            .record(event(request, 10, &lease));
    }

    let error = writer.shutdown().await.expect_err("the sink panicked");
    assert_eq!(
        error.unaccounted, 3,
        "the two billed charges must not be counted again"
    );
}

// ---- wall-clock bounds on a wedged backend (issue GL-34) --------------------

/// A sink whose `ingest` never resolves — a backend that is hung rather than
/// erroring, which retry *counts* alone cannot bound.
/// An ingest that never returns.
fn hanging_sink() -> Arc<DelegatingStore<RejectingStore>> {
    Arc::new(
        rejecting("a hanging-sink fixture answers ingest and nothing else")
            .on_ingest(|_, _events, _now| async { std::future::pending().await }),
    )
}

/// The GL-34 defect: a hung ingest parked the writer task forever, and with it
/// every later shutdown step. The drain must terminate and report.
#[tokio::test(start_paused = true)]
async fn hung_ingest_cannot_stall_shutdown() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let mut config = writer_config(16);
    config.flush_interval = std::time::Duration::from_secs(3_600);
    let (recorder, writer) =
        UsageWriter::spawn(hanging_sink() as Arc<dyn UsageSink>, clock, config).unwrap();
    recorder.try_reserve().unwrap().record(event(1, 25, &lease));

    let stats = tokio::time::timeout(std::time::Duration::from_secs(300), writer.shutdown())
        .await
        .expect("a hung sink must not stall shutdown")
        .unwrap();

    // Undeliverable is undeliverable: counted lost, never accepted.
    assert_eq!(stats.lost, 1);
    assert_eq!(stats.accepted, 0);
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits::ZERO);
}

/// Steady state: a hung ingest times out into the ordinary retry path rather
/// than parking the task, so a later shutdown still observes the signal.
#[tokio::test(start_paused = true)]
async fn hung_ingest_times_out_into_the_retry_path() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let mut config = writer_config(16);
    config.ingest_timeout = std::time::Duration::from_millis(100);
    let (recorder, writer) =
        UsageWriter::spawn(hanging_sink() as Arc<dyn UsageSink>, clock, config).unwrap();
    recorder.try_reserve().unwrap().record(event(1, 25, &lease));

    // Several timeout+backoff rounds elapse; the task stays responsive.
    settle().await;
    let stats = tokio::time::timeout(std::time::Duration::from_secs(300), writer.shutdown())
        .await
        .expect("the retry loop must remain interruptible")
        .unwrap();
    assert_eq!(stats.accepted, 0);
    assert_eq!(stats.lost, 1);
}

/// A sink that is slow but healthy must still be delivered to: the timeout
/// bounds a hang, it does not shorten the budget of a working call.
/// Delays, then behaves like the store it wraps.
fn slow_sink(
    store: &Arc<MemoryStore>,
    delay: std::time::Duration,
) -> Arc<DelegatingStore<MemoryStore>> {
    Arc::new(DelegatingStore::wrapping(Arc::clone(store)).on_ingest(
        move |inner, events, now| async move {
            tokio::time::sleep(delay).await;
            inner.ingest(&events, now).await
        },
    ))
}

#[tokio::test(start_paused = true)]
async fn slow_but_healthy_sink_still_delivers_at_shutdown() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let sink = slow_sink(&store.clone(), std::time::Duration::from_millis(50));
    let mut config = writer_config(16);
    config.flush_interval = std::time::Duration::from_secs(3_600);
    config.ingest_timeout = std::time::Duration::from_secs(5);
    let (recorder, writer) = UsageWriter::spawn(sink as Arc<dyn UsageSink>, clock, config).unwrap();
    recorder.try_reserve().unwrap().record(event(1, 25, &lease));

    let stats = writer.shutdown().await.unwrap();
    assert_eq!(stats.accepted, 1, "a slow call inside its budget delivers");
    assert_eq!(stats.lost, 0);
    assert_eq!(store.usage_recorded(ACCOUNT), CostUnits(25));
}

/// An allocator that never answers an acquire. Distinct from refusing: the
/// grant may well have been made, so the client cannot treat this as a
/// domain answer.
/// An `acquire` that never returns, over no store at all.
///
/// `reclaim_expired` is not hooked and needs no inner store: the wrapper
/// inherits the trait default, which drains through the `reclaim_expired_batch`
/// hook below and so still answers with an empty batch.
fn hanging_acquire() -> Arc<DelegatingStore<RejectingStore>> {
    Arc::new(
        rejecting("this fixture never consolidates")
            .on_acquire(|_, _account, _requested, _ttl, _now| async {
                std::future::pending().await
            })
            .on_release(|_, _lease, _fence, _unspent, _now| async { Ok(()) })
            .on_reclaim_expired_batch(|_, _now, limit| async move {
                ReclaimBatch::try_new(Vec::new(), limit)
            }),
    )
}

/// INVARIANTS.md GL-18, issue GL-78: the shutdown signal is observed inside a
/// hung `acquire`, not after it. The release pass is the louder half of that
/// finding, but `acquire` is the loop's other long await and a signal
/// arriving inside it was equally invisible until the next loop top.
#[tokio::test(start_paused = true)]
async fn shutdown_during_a_hung_acquire_is_not_delayed_by_it() {
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    // A long per-call timeout with a short budget: if the signal is only
    // observed at the loop top, shutdown waits out the acquire and blows the
    // budget. The production shape has the same relationship, more slowly.
    let config = LeaseManagerConfig {
        store_call_timeout: std::time::Duration::from_secs(120),
        shutdown_release_deadline: std::time::Duration::from_secs(10),
        ..manager_config()
    };
    let manager = LeaseManager::spawn(
        hanging_acquire() as Arc<dyn LeaseAllocator>,
        Arc::clone(&slot),
        clock,
        config,
    )
    .unwrap();
    settle().await;
    assert!(slot.load().is_none(), "the acquire is still unanswered");

    let bound = config.shutdown_release_deadline + std::time::Duration::from_secs(1);
    let began = tokio::time::Instant::now();
    let report = tokio::time::timeout(bound, manager.shutdown())
        .await
        .expect("a hung acquire must not hold the shutdown signal");
    let took = began.elapsed();

    assert!(!report.task_died);
    assert_eq!(report.released, 0, "there was never a lease to return");
    assert_eq!(report.abandoned, 0);
    assert!(
        took < config.store_call_timeout,
        "shutdown took {took:?}, which is the hung acquire's timeout, not the signal"
    );
}

/// INVARIANTS.md GL-18: an allocator that hangs rather than answering is
/// bounded by `store_call_timeout`. It is counted apart from every refusal,
/// because it is not a domain answer — treating it as one would assert the
/// lease was not granted, which this client cannot know.
#[tokio::test(start_paused = true)]
async fn a_hung_acquire_is_counted_as_a_timeout_not_a_refusal() {
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager = LeaseManager::spawn(
        hanging_acquire() as Arc<dyn LeaseAllocator>,
        Arc::clone(&slot),
        clock,
        LeaseManagerConfig {
            // Short enough that the bound is reached inside a settle; the
            // shared config's five seconds is a production-shaped value.
            store_call_timeout: std::time::Duration::from_millis(10),
            ..manager_config()
        },
    )
    .unwrap();
    let counters = manager.counters();

    settle().await;
    let stats = counters.snapshot();
    assert!(
        stats.acquire_timeouts > 0,
        "the refill loop must not park on a hung allocator"
    );
    assert_eq!(
        stats.refused(),
        0,
        "a timeout is not a refusal: {:?}",
        stats.acquire_refused
    );
    assert_eq!(stats.acquired, 0);
    assert!(slot.load().is_none(), "fail closed while unanswered");

    manager.shutdown().await;
}

/// A `release` that never returns, over a real store.
///
/// `consolidate` keeps its `unreachable!()`: delegation would quietly give
/// this fixture a working exchange it is not supposed to have.
fn hanging_release(store: &Arc<MemoryStore>) -> Arc<DelegatingStore<MemoryStore>> {
    Arc::new(
        DelegatingStore::wrapping(Arc::clone(store))
            .on_release(|_, _lease, _fence, _unspent, _now| async { std::future::pending().await })
            .on_consolidate(|_, _, _, _, _, _, _, _| async {
                unreachable!("this fixture never consolidates")
            }),
    )
}

/// INVARIANTS.md GL-6, issue GL-78: a refill must not queue behind the release
/// pass. With leases parked against a backend whose `release` hangs, the old
/// loop paid `parked.len()` sequential timeouts before it issued `acquire`,
/// which put refill latency back on a floor that grew with the parked count —
/// the very floor GL-6 exists to remove.
#[tokio::test(start_paused = true)]
async fn a_refill_does_not_wait_behind_the_release_pass() {
    let store = store(100_000);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let allocator = hanging_release(&store.clone());
    let manager = LeaseManager::spawn(
        allocator as Arc<dyn LeaseAllocator>,
        Arc::clone(&slot),
        Arc::clone(&clock) as Arc<dyn tollgate_client::Clock>,
        manager_config(),
    )
    .unwrap();
    settle().await;

    // Park a queue by rotating on expiry; a hung release keeps every
    // superseded lease on the books.
    //
    // The fixture waits on the observable state rather than on a fixed sleep,
    // so that a regression shows up in the measurement below instead of
    // breaking the setup: a slower loop must still reach a fresh lease here,
    // and then be caught by the ceiling.
    for tick in 1..=5 {
        clock.set(t(tick * 120));
        let installed = tokio::time::timeout(std::time::Duration::from_secs(600), async {
            loop {
                match slot.load() {
                    Some(lease) if lease.grant().expires_at > clock.now() => break,
                    _ => tokio::time::sleep(std::time::Duration::from_millis(50)).await,
                }
            }
        })
        .await;
        installed.expect("the manager must install a lease after each rotation");
    }
    assert_eq!(manager.counters().snapshot().acquired, 6);
    assert_eq!(manager.counters().snapshot().released, 0);
    let before = slot.load().expect("a lease is installed");
    assert!(
        !before.needs_refill(),
        "the fixture must start from a lease that has not already crossed"
    );

    // Ring the low-water doorbell and time the refill. Waiting on the fresh
    // lease rather than on a fixed sleep is what makes the ceiling below the
    // thing under test: a slow refill is measured, not deadlocked.
    let began = tokio::time::Instant::now();
    before.try_debit(CostUnits(800), clock.now()).unwrap();
    assert!(before.needs_refill());
    tokio::time::timeout(std::time::Duration::from_secs(600), async {
        loop {
            match slot.load() {
                Some(fresh) if fresh.grant().lease_id != before.grant().lease_id => break,
                _ => tokio::time::sleep(std::time::Duration::from_millis(10)).await,
            }
        }
    })
    .await
    .expect("the crossing debit must be followed by a fresh lease");
    let waited = began.elapsed();
    // One pass budget, not one per parked lease. `settle()` bounds the wait
    // from above anyway, so the assertion is about the shape: five parked
    // leases must not cost five timeouts.
    // Generous enough to absorb finishing a pass already in flight plus the
    // next one's head lease; five parked leases at one timeout each — the
    // pre-GL-78 cost — is 25s and does not fit.
    let ceiling = manager_config().store_call_timeout * 3;
    assert!(
        waited < ceiling,
        "refill waited {waited:?} behind the release pass, over the {ceiling:?} ceiling"
    );
    manager.shutdown().await;
}

/// A clean shutdown accounts for the leases it returned.
#[tokio::test(start_paused = true)]
async fn shutdown_reports_released_leases() {
    let store = store(10_000);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager =
        LeaseManager::spawn(store.clone(), Arc::clone(&slot), clock, manager_config()).unwrap();
    settle().await;
    assert!(slot.load().is_some());

    let report = manager.shutdown().await;
    assert_eq!(report.released, 1);
    assert_eq!(report.abandoned, 0);
    assert!(!report.task_died);
    assert_eq!(store.balance(ACCOUNT), CostUnits(10_000), "units came back");
}

/// A hung release cannot stall shutdown, and what it could not return is
/// reported rather than assumed settled (INVARIANTS.md GL-18).
#[tokio::test(start_paused = true)]
async fn hung_release_cannot_stall_shutdown() {
    let store = store(10_000);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let allocator = hanging_release(&store.clone());
    let manager = LeaseManager::spawn(
        allocator as Arc<dyn LeaseAllocator>,
        Arc::clone(&slot),
        Arc::clone(&clock) as Arc<dyn tollgate_client::Clock>,
        manager_config(),
    )
    .unwrap();
    let counters = manager.counters();
    settle().await;
    assert!(slot.load().is_some());
    assert_eq!(counters.snapshot().abandoned, 0, "nothing abandoned yet");

    // In-flight readers retain each superseded grant while the slot rotates.
    // Once they finish, every parked grant reaches the hanging backend. This
    // is the shape GL-78 bounds: the old loop paid `parked.len()` sequential
    // timeouts before it could even inspect the shutdown signal.
    let mut readers = Vec::new();
    for tick in 1..=5 {
        readers.push(
            slot.load()
                .expect("the preceding rotation installed a grant"),
        );
        clock.set(t(tick * 120));
        settle().await;
    }
    drop(readers);
    let parked = counters.snapshot();
    assert_eq!(parked.released, 0, "a hung release settles nothing");

    // The stated budget, not an arbitrary ceiling. `shutdown()` documents
    // itself as bounded by `shutdown_release_deadline`, and that is only true
    // if the signal ends the release pass where it arrives: a pass that runs
    // to its own budget first would add `store_call_timeout` on top, which
    // this bound deliberately does not allow for. The extra second is
    // scheduling slack, not budget.
    let config = manager_config();
    let bound = config.shutdown_release_deadline + std::time::Duration::from_secs(1);
    let began = tokio::time::Instant::now();
    let report = tokio::time::timeout(bound, manager.shutdown())
        .await
        .expect("a hung release must not stall shutdown past its stated budget");
    let took = began.elapsed();

    assert!(
        report.abandoned >= 2,
        "the test must exercise a multi-lease parked list, got {}",
        report.abandoned
    );
    assert_eq!(report.released, 0);
    assert!(!report.task_died);
    assert!(
        took <= bound,
        "shutdown took {took:?} against a stated {:?} budget",
        config.shutdown_release_deadline
    );
    // The same numbers outside the report: stranded units are visible to a
    // scrape, not only to whoever awaited this shutdown.
    let stats = counters.snapshot();
    assert_eq!(stats.abandoned, report.abandoned);
    assert_eq!(stats.released, 0);
}

/// INVARIANTS.md GL-16: no writer field is silently repaired.
#[tokio::test]
async fn invalid_writer_config_is_rejected() {
    let zero = std::time::Duration::ZERO;
    let mut zero_capacity = writer_config(8);
    zero_capacity.queue_capacity = 0;
    let mut zero_batch = writer_config(8);
    zero_batch.max_batch = 0;
    let mut zero_flush = writer_config(8);
    zero_flush.flush_interval = zero;
    let mut zero_backoff = writer_config(8);
    zero_backoff.retry_backoff = zero;
    let mut zero_drain = writer_config(8);
    zero_drain.shutdown_drain_deadline = zero;
    let mut zero_ingest = writer_config(8);
    zero_ingest.ingest_timeout = zero;

    for (field, config) in [
        ("queue_capacity", zero_capacity),
        ("max_batch", zero_batch),
        ("flush_interval", zero_flush),
        ("retry_backoff", zero_backoff),
        ("shutdown_drain_deadline", zero_drain),
        ("ingest_timeout", zero_ingest),
    ] {
        assert!(
            config.validate().is_err(),
            "zero {field} must be rejected, not coerced"
        );
        let store = store(10_000);
        let clock = Arc::new(ManualClock::new(t(0)));
        assert!(
            UsageWriter::spawn(store, clock, config).is_err(),
            "zero {field} must not start a task"
        );
    }
}

/// INVARIANTS.md GL-16, manager half: the two new bounds are contracts too.
#[test]
fn invalid_lease_manager_timeouts_are_rejected() {
    let mut config = manager_config();
    config.store_call_timeout = std::time::Duration::ZERO;
    assert!(config.validate().is_err());

    let mut config = manager_config();
    config.shutdown_release_deadline = std::time::Duration::ZERO;
    assert!(config.validate().is_err());
}

/// Issue GL-62, INVARIANTS GL-1 and GL-6: shutdown must not release a lease an
/// in-flight request can still spend.
///
/// `parked` at shutdown holds, by construction, the leases the last pass
/// determined were *not* quiesced — `release_quiesced` re-parks exactly those.
/// Releasing them read `remaining()` at an instant that was not final: the
/// account is credited, the request then debits and commits, and total
/// committed usage exceeds the allocation with a usage event to prove it.
///
/// The documented lifecycle says an embedder quiesces first (GL-13), but that is
/// caller discipline — the tier the standards call drift-prone — and the
/// predicate that makes it unnecessary already existed in the same file.
///
/// Held here by keeping an `Arc<LocalLease>` across `shutdown()`, which is
/// what a request task in flight looks like from the manager's side.
#[tokio::test(start_paused = true)]
async fn shutdown_abandons_a_lease_an_in_flight_request_still_holds() {
    let store = store(10_000);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager = LeaseManager::spawn(
        store.clone(),
        Arc::clone(&slot),
        Arc::clone(&clock) as Arc<dyn Clock>,
        manager_config(),
    )
    .unwrap();
    settle().await;

    // The in-flight request: a view loaded before shutdown and still held
    // across it, exactly as a handler holding a reservation would.
    let in_flight = slot.load().expect("a lease is installed");
    let balance_before = store.balance(ACCOUNT);

    let report = manager.shutdown().await;

    assert_eq!(
        report.released, 0,
        "a lease still reachable by a request must not be released"
    );
    assert_eq!(
        report.abandoned, 1,
        "it is abandoned instead, and reported — never silently dropped"
    );
    assert_eq!(
        store.balance(ACCOUNT),
        balance_before,
        "no units were credited back while a request could still spend them; \
         crediting early is what lets committed usage exceed the allocation"
    );

    // The request can still spend what it holds, which is the whole reason
    // the units were not returned.
    assert!(in_flight.try_debit(CostUnits(50), t(0)).is_ok());
}

/// Issue GL-63, INVARIANTS GL-8: the drain deadline is the drain's total wall
/// clock, backoffs included.
///
/// `final_flush_backs_off_only_between_attempts` pins the backoff *count*, but
/// runs with a 60s deadline and a 100ms backoff — so it can never observe the
/// two interacting. The exposing configuration is the opposite one, and it is
/// not exotic: the module doc tells an operator to size the drain inside
/// `expiry_safety_margin + reclaim_grace`, which makes a short deadline beside
/// a longer backoff the ordinary shape.
///
/// Overrunning here is not merely a slow shutdown. The lease manager releases
/// after the writer drains, so the overrun spends the margin that keeps a
/// straggler billable: events that do land arrive against a lease the
/// allocator has re-granted and are refused — the outcome GL-12's budget exists
/// to prevent.
#[tokio::test(start_paused = true)]
async fn the_final_flush_backoff_cannot_overrun_the_drain_deadline() {
    let store = store(10_000);
    let lease = store
        .acquire(
            ACCOUNT,
            CostUnits(1_000),
            SignedDuration::from_secs(60),
            t(0),
        )
        .await
        .unwrap()
        .grant;
    let clock = Arc::new(ManualClock::new(t(0)));
    let failures_left = Arc::new(AtomicU32::new(u32::MAX));
    let sink = flaky_sink(&store.clone(), &failures_left);
    let mut config = writer_config(16);
    config.flush_interval = std::time::Duration::from_secs(3_600);
    // The shape that exposes it: a backoff longer than the whole budget.
    config.shutdown_drain_deadline = std::time::Duration::from_secs(2);
    config.retry_backoff = std::time::Duration::from_secs(5);
    config.ingest_timeout = std::time::Duration::from_millis(50);
    let (recorder, writer) = UsageWriter::spawn(sink, clock, config).unwrap();
    recorder.try_reserve().unwrap().record(event(1, 10, &lease));

    let start = tokio::time::Instant::now();
    let stats = writer.shutdown().await.unwrap();
    let took = start.elapsed();

    assert_eq!(
        stats.lost, 1,
        "an undeliverable event is counted, not dropped"
    );
    assert!(
        took <= config.shutdown_drain_deadline + config.ingest_timeout,
        "the drain took {took:?} against a {:?} budget; a backoff that sleeps \
         past the deadline spends the margin that keeps a straggler billable",
        config.shutdown_drain_deadline
    );
}

/// The sink refuses whatever it is handed, so the event's shape is irrelevant
/// — only that it is a real event the writer will try to deliver.
fn refused_event(request: u128) -> UsageEvent {
    UsageEvent::new(
        RequestId(request),
        ACCOUNT,
        UsageSource::Overage,
        CostUnits(50),
        t(0),
        PolicyRevision::UNSTATED,
        None,
    )
}

/// A sink that refuses every batch and will keep refusing it, as an
/// over-limit body or an undecodable event does.
/// A sink that refuses every batch with the same permanent error.
fn always_refuses_sink(attempts: &Arc<AtomicUsize>) -> Arc<DelegatingStore<RejectingStore>> {
    let attempts = Arc::clone(attempts);
    Arc::new(
        rejecting("a refusing-sink fixture answers ingest and nothing else").on_ingest(
            move |_, _events, _now| {
                let attempts = Arc::clone(&attempts);
                async move {
                    attempts.fetch_add(1, Ordering::AcqRel);
                    Err(IngestError::Refused(StoreError(
                        "413 batch-too-large: request body exceeds this endpoint's limit".into(),
                    )))
                }
            },
        ),
    )
}

/// Issue GL-61: a batch the sink will never accept must not block every later
/// event behind it.
///
/// The writer retried any failed batch forever, which is right for an outage
/// and wrong for a refusal: the queue fills, later requests shed as
/// `AccountingBackpressure`, `/readyz` stays 200 because the task is alive and
/// spinning, and `lost` stays zero because loss is declared only at the final
/// flush. A permanent, deterministic error became an unbounded billing and
/// availability outage.
///
/// Two assertions, and the second is the one that matters: the batch is
/// attempted *once*, and a later event still bills.
/// Issue GL-61, INVARIANTS GL-16: a `max_batch` the ingest endpoint would refuse
/// is a startup error, not a discovery made in production.
///
/// `validate` previously checked only that it was non-zero, so an embedder
/// raising it to cut round-trips was accepted and then had every batch
/// refused forever.
#[test]
fn a_max_batch_beyond_the_ingest_limit_is_rejected() {
    let mut oversized = writer_config(8);
    oversized.max_batch = tollgate_store::MAX_INGEST_BATCH + 1;
    assert_eq!(
        oversized.validate().unwrap_err().to_string(),
        "max_batch exceeds the ingest endpoint's documented limit"
    );

    let mut at_limit = writer_config(8);
    at_limit.max_batch = tollgate_store::MAX_INGEST_BATCH;
    assert!(
        at_limit.validate().is_ok(),
        "the documented limit itself must be usable, or it is not the limit"
    );
}

#[tokio::test(start_paused = true)]
async fn a_refused_batch_is_counted_lost_rather_than_retried_forever() {
    let attempts = Arc::new(AtomicUsize::new(0));
    let sink = always_refuses_sink(&attempts);
    let clock = Arc::new(ManualClock::new(t(0)));
    let (recorder, writer) = UsageWriter::spawn(sink, clock, writer_config(8)).unwrap();

    recorder.try_reserve().unwrap().record(refused_event(1));
    settle().await;

    assert_eq!(
        attempts.load(Ordering::Acquire),
        1,
        "a refused batch must be attempted once, not retried"
    );

    assert_eq!(
        recorder.health().unaccounted,
        0,
        "a reported loss is a completed accounting outcome"
    );

    // The queue keeps working: a later event is not stuck behind the refusal.
    recorder
        .try_reserve()
        .expect("the queue drained rather than filling with a wedged batch")
        .record(refused_event(2));
    settle().await;

    let stats = writer.shutdown().await.unwrap();
    assert!(
        stats.lost >= 2,
        "refused events are counted lost, never silently dropped; got {stats:?}"
    );
}

/// Hangs, and reports how many ingests are parked inside it.
fn observed_hung_sink(active: &Arc<AtomicUsize>) -> Arc<DelegatingStore<RejectingStore>> {
    let active = Arc::clone(active);
    Arc::new(
        rejecting("a hung-sink fixture answers ingest and nothing else").on_ingest(
            move |_, _events, _now| {
                let active = Arc::clone(&active);
                async move {
                    struct Active(Arc<AtomicUsize>);
                    impl Drop for Active {
                        fn drop(&mut self) {
                            self.0.fetch_sub(1, Ordering::AcqRel);
                        }
                    }
                    active.fetch_add(1, Ordering::AcqRel);
                    let _active = Active(Arc::clone(&active));
                    std::future::pending().await
                }
            },
        ),
    )
}

#[tokio::test(start_paused = true)]
async fn cancelling_writer_shutdown_aborts_the_owned_ingest_task() {
    let active = Arc::new(AtomicUsize::new(0));
    let mut config = writer_config(8);
    config.max_batch = 1;
    config.ingest_timeout = std::time::Duration::from_secs(120);
    config.shutdown_drain_deadline = std::time::Duration::from_secs(60);
    let (recorder, writer) = UsageWriter::spawn(
        observed_hung_sink(&active),
        Arc::new(ManualClock::new(t(0))),
        config,
    )
    .unwrap();
    recorder.try_reserve().unwrap().record(refused_event(1));
    settle().await;
    assert_eq!(active.load(Ordering::Acquire), 1);
    assert!(
        tokio::time::timeout(std::time::Duration::from_millis(1), writer.shutdown())
            .await
            .is_err()
    );
    for _ in 0..10 {
        tokio::task::yield_now().await;
    }
    assert_eq!(
        active.load(Ordering::Acquire),
        0,
        "cancelled shutdown must not detach its writer"
    );
}

#[tokio::test(start_paused = true)]
async fn shutdown_interrupts_normal_ingest_before_its_long_timeout() {
    let active = Arc::new(AtomicUsize::new(0));
    let mut config = writer_config(8);
    config.max_batch = 1;
    config.ingest_timeout = std::time::Duration::from_secs(120);
    config.shutdown_drain_deadline = std::time::Duration::from_millis(10);
    let (recorder, writer) = UsageWriter::spawn(
        observed_hung_sink(&active),
        Arc::new(ManualClock::new(t(0))),
        config,
    )
    .unwrap();
    recorder.try_reserve().unwrap().record(refused_event(1));
    settle().await;
    assert_eq!(active.load(Ordering::Acquire), 1);
    let result = tokio::time::timeout(std::time::Duration::from_millis(11), writer.shutdown())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(result.lost, 1);
    assert_eq!(active.load(Ordering::Acquire), 0);
}

#[tokio::test(start_paused = true)]
async fn cancelling_lease_shutdown_aborts_the_owned_release_task() {
    let store = store(10_000);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let manager = LeaseManager::spawn(
        hanging_release(&store),
        slot.clone(),
        Arc::new(ManualClock::new(t(0))),
        manager_config(),
    )
    .unwrap();
    let health = manager.health();
    settle().await;
    assert!(slot.load().is_some());
    assert!(
        tokio::time::timeout(std::time::Duration::from_millis(1), manager.shutdown())
            .await
            .is_err()
    );
    for _ in 0..10 {
        tokio::task::yield_now().await;
    }
    assert!(
        health.has_changed().is_err(),
        "cancelled shutdown must not detach its manager"
    );
    assert!(!*health.borrow(), "cancelled refill task must retain false");
}

/// Issue GL-109: near the end of an allowance the allocator shrinks the grant,
/// and `install_lease` caps low water below it, so the tail lease sits *above*
/// its own mark. Nothing crosses, and before this change a refused debit said
/// nothing either — the instance answered `LeaseExhausted` until the TTL while
/// lease and ledger together could fund the request.
///
/// The reporter's numbers exactly: allowance 160, target 100, low water 50,
/// three 51-unit requests. Grants run 100 → 60 → 49, and the third request
/// meets a 49-unit lease with 9 units still in the ledger.
#[tokio::test(start_paused = true)]
async fn a_refused_lease_consolidates_rather_than_stranding_the_tail() {
    let store = store(160);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager = LeaseManager::spawn(
        store.clone(),
        Arc::clone(&slot),
        clock,
        LeaseManagerConfig {
            target_grant: CostUnits(100),
            low_water: CostUnits(50),
            // A minute is far longer than this test runs, so no tick can
            // explain a rotation. Only the refusal can.
            poll_interval: std::time::Duration::from_secs(60),
            ..manager_config()
        },
    )
    .unwrap();

    settle().await;
    for spent in 1..=2 {
        let lease = slot.load().expect("a lease is installed");
        lease
            .try_debit(CostUnits(51), t(0))
            .unwrap_or_else(|e| panic!("debit {spent} should be funded: {e:?}"));
        drop(lease);
        settle().await;
    }

    let tail = slot.load().expect("a lease is installed");
    assert_eq!(
        tail.remaining(),
        CostUnits(49),
        "the shrunken tail grant is the state the defect needs"
    );
    assert!(
        !tail.needs_refill(),
        "and it sits above its own capped low-water mark, so nothing crosses"
    );
    let refused = tail.try_debit(CostUnits(51), t(0));
    assert!(
        matches!(refused, Err(DenyReason::LeaseExhausted { .. })),
        "this one debit is still refused: {refused:?}"
    );
    drop(tail);

    settle().await;
    let consolidated = slot.load().expect("a lease is installed");
    assert_eq!(
        consolidated.remaining(),
        CostUnits(58),
        "the refusal folded the tail's 49 unspent units back in with the \
         ledger's 9, so the account's whole remaining balance is reachable"
    );
    consolidated
        .try_debit(CostUnits(51), t(0))
        .expect("the request the account could always fund is now admitted");
    drop(consolidated);

    let stats = manager.counters().snapshot();
    assert_eq!(stats.consolidated, 1, "exactly one consolidating rotation");
    assert_eq!(
        stats.consolidations_deferred, 0,
        "nothing was in flight to defer it"
    );
    manager.shutdown().await;
}

/// The units are folded in *atomically*, which is the half a holder cannot do
/// for itself. With the default `shrink_divisor` of 2, releasing 49 into a
/// balance of 9 and then acquiring re-grants only 29 — a consolidation that
/// shrank the holder. The floor is what forbids that.
#[tokio::test(start_paused = true)]
async fn consolidation_under_a_shrinking_policy_never_returns_less_than_it_folded() {
    let store = Arc::new(
        MemoryStore::new(GrantPolicy {
            shrink_divisor: 2,
            min_grant: CostUnits(1),
            max_ttl: SignedDuration::from_secs(3_600),
            reclaim_grace: SignedDuration::ZERO,
        })
        .unwrap(),
    );
    store.create_account(AccountConfig {
        account_id: ACCOUNT,
        initial_balance: CostUnits(58),
        status: AccountStatus::Active,
        capacity_class: CapacityClass::Assured,
    });
    let grant = store
        .acquire(ACCOUNT, CostUnits(49), SignedDuration::from_secs(60), t(0))
        .await
        .expect("the first grant is funded")
        .grant;
    assert_eq!(grant.units, CostUnits(29), "the policy shrinks it by half");

    let folded = store
        .consolidate(
            grant.lease_id,
            grant.fencing_token,
            grant.units,
            CostUnits(100),
            CostUnits::ZERO,
            SignedDuration::from_secs(60),
            t(1),
        )
        .await
        .expect("consolidation is funded by the units it returns")
        .grant;
    assert!(
        folded.units >= grant.units,
        "a consolidation may grow the holding or leave it alone, never shrink it: \
         {} folded in, {} granted",
        grant.units.get(),
        folded.units.get()
    );
    assert_eq!(
        folded.units,
        CostUnits(29),
        "the policy still caps growth at balance/2; the floor only forbids the downgrade"
    );
}

#[tokio::test(start_paused = true)]
async fn refill_publishes_exhaustion_and_a_topup_clears_it() {
    let store = store(0);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager =
        LeaseManager::spawn(store.clone(), slot.clone(), clock, manager_config()).unwrap();
    settle().await;
    assert!(
        slot.funding_evidence(t(0))
            .is_some_and(|remaining| remaining.is_zero())
    );
    tollgate_store::AdminStore::deposit(&*store, ACCOUNT, CostUnits(100))
        .await
        .unwrap();
    settle().await;
    assert!(
        !slot
            .funding_evidence(t(0))
            .is_some_and(|remaining| remaining.is_zero())
    );
    assert_eq!(slot.load_observed().unwrap().remaining(), CostUnits(100));
    manager.shutdown().await;
}

/// GL-131 end to end with `GrantPolicy::default()`: 60 units grant 30, and
/// before GL-131 every refusal-driven consolidation re-granted 30, so a 51-unit
/// quote the account could fund was refused until the period ended.
#[tokio::test(start_paused = true)]
async fn a_shrinking_policy_funds_a_quote_above_half_the_balance() {
    let store = MemoryStore::new(GrantPolicy::default()).unwrap();
    store.create_account(AccountConfig {
        account_id: ACCOUNT,
        initial_balance: CostUnits(60),
        status: AccountStatus::Active,
        capacity_class: CapacityClass::Assured,
    });
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager = LeaseManager::spawn(
        store.clone(),
        Arc::clone(&slot),
        clock,
        LeaseManagerConfig {
            // Only the refusal can explain a rotation in this test.
            poll_interval: std::time::Duration::from_secs(60),
            ..manager_config()
        },
    )
    .unwrap();
    settle().await;
    let half = slot.load().expect("a lease is installed");
    assert_eq!(half.remaining(), CostUnits(30));
    assert!(matches!(
        half.try_debit(CostUnits(51), t(0)),
        Err(DenyReason::LeaseExhausted { .. })
    ));
    drop(half);
    settle().await;

    let grown = slot.load().expect("a lease is installed");
    assert_eq!(
        grown.remaining(),
        CostUnits(51),
        "grown to the refused quote"
    );
    grown
        .try_debit(CostUnits(51), t(0))
        .expect("the quote the account could always fund is admitted");
    drop(grown);
    assert_eq!(manager.counters().snapshot().consolidated, 1);
    manager.shutdown().await;
}

/// GL-130 end to end: one unit left and a quote of 252. The refusal-driven
/// consolidation re-grants the tail, whose evidence says the account cannot
/// fund the quote; a top-up reaches the slot through the next refusal.
#[tokio::test(start_paused = true)]
async fn refill_publishes_shortfall_and_a_topup_clears_it() {
    let store = store(1);
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager = LeaseManager::spawn(
        store.clone(),
        slot.clone(),
        clock,
        LeaseManagerConfig {
            // Only a refusal can explain a rotation in this test.
            poll_interval: std::time::Duration::from_secs(60),
            ..manager_config()
        },
    )
    .unwrap();
    settle().await;
    assert_eq!(slot.load_observed().unwrap().remaining(), CostUnits(1));
    assert_eq!(slot.funding_evidence(t(0)), Some(CostUnits(1)));

    let refuse_252 = || {
        let lease = slot.load().expect("a lease is installed");
        assert!(matches!(
            lease.try_debit(CostUnits(252), t(0)),
            Err(DenyReason::LeaseExhausted { .. })
        ));
    };
    refuse_252();
    settle().await;
    assert_eq!(
        slot.funding_evidence(t(0)),
        Some(CostUnits(1)),
        "the consolidated tail carries the same attested remainder"
    );

    tollgate_store::AdminStore::deposit(&*store, ACCOUNT, CostUnits(300))
        .await
        .unwrap();
    refuse_252();
    settle().await;
    assert_eq!(slot.funding_evidence(t(0)), Some(CostUnits(301)));
    slot.load()
        .expect("a lease is installed")
        .try_debit(CostUnits(252), t(0))
        .expect("the top-up funds the quote");
    manager.shutdown().await;
}

#[tokio::test(start_paused = true)]
async fn a_refill_gap_with_credit_held_elsewhere_is_not_exhaustion() {
    let store = store(100);
    let held = store
        .acquire(ACCOUNT, CostUnits(100), SignedDuration::from_secs(60), t(0))
        .await
        .unwrap()
        .grant;
    let slot = LeaseSlot::for_account(ACCOUNT);
    let clock = Arc::new(ManualClock::new(t(0)));
    let manager =
        LeaseManager::spawn(store.clone(), slot.clone(), clock, manager_config()).unwrap();
    settle().await;
    // The refusal attests the 100 units the other lease holds: any quote
    // within them keeps the lease gap's transient advice.
    assert_eq!(slot.funding_evidence(t(0)), Some(CostUnits(100)));
    assert!(slot.load_observed().is_none());
    store
        .release(held.lease_id, held.fencing_token, held.units, t(0))
        .await
        .unwrap();
    settle().await;
    assert!(slot.load_observed().is_some());
    assert!(
        !slot
            .funding_evidence(t(0))
            .is_some_and(|remaining| remaining.is_zero())
    );
    manager.shutdown().await;
}