pollardai 0.2.0

Native Rust runtime for Pollard governed execution trees, shared budgets, and verified replay
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
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
use crate::identity::{safe_amount, MAX_SAFE_INTEGER};
use crate::{
    canonical_bytes, digest_payload, verify, ActionSpec, Decision, Error, MemoryStore, Node,
    NodeKind, Policy, PolicyContext, RecordingStore, Registry, Result,
};
use rust_decimal::{prelude::ToPrimitive, Decimal};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::cell::{Cell, Ref, RefCell};
use std::collections::{BTreeMap, BTreeSet};
use std::rc::Rc;
use std::time::Instant;

/// Additional budgets and charges, named exactly as their configured meter.
pub type MeterCharges = BTreeMap<String, f64>;
pub type MeterBudget = BTreeMap<String, f64>;
pub type NodeCallback = Rc<dyn Fn(&Node) -> Result<()>>;

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct RunReport {
    pub spent: MeterCharges,
    pub avoided: MeterCharges,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReplayMode {
    Record,
    Hybrid,
    Replay,
}

/// Integer budgets. Depth is absolute ancestry depth; root depth is zero.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Budget {
    pub steps: Option<u64>,
    pub tokens: Option<u64>,
    pub depth: Option<u64>,
}
impl Budget {
    pub fn validate(&self) -> Result<()> {
        for (name, value) in [
            ("steps", self.steps),
            ("tokens", self.tokens),
            ("depth", self.depth),
        ] {
            if let Some(n) = value {
                safe_amount(n, name)?;
            }
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Charges {
    pub steps: u64,
    pub tokens: u64,
}
impl Charges {
    fn add(&mut self, other: Self) -> Result<()> {
        self.steps = self
            .steps
            .checked_add(other.steps)
            .ok_or_else(|| Error::Integrity("step total overflow".into()))?;
        self.tokens = self
            .tokens
            .checked_add(other.tokens)
            .ok_or_else(|| Error::Integrity("token total overflow".into()))?;
        safe_amount(self.steps, "step total")?;
        safe_amount(self.tokens, "token total")?;
        Ok(())
    }
}

/// Supply a conservative total input + output token estimate for token gates.
#[derive(Debug, Clone, Copy, Default)]
pub struct CallOptions {
    pub attempt: u64,
    pub estimated_tokens: Option<u64>,
}

/// Identity for one deliberate live comparison. Supply an observation ID once;
/// reusing it at the same recording cursor is rejected before dispatch.
#[derive(Debug, Clone)]
pub struct RevalidationOptions {
    pub observation_id: String,
    pub call: CallOptions,
    pub live_payload: Option<Value>,
    /// Retain original chunks when using streaming revalidation.
    pub keep_chunks: bool,
}
impl RevalidationOptions {
    pub fn new(observation_id: impl Into<String>) -> Self {
        Self {
            observation_id: observation_id.into(),
            call: CallOptions::default(),
            live_payload: None,
            keep_chunks: false,
        }
    }
}

struct StoreCell {
    inner: RefCell<Box<dyn RecordingStore>>,
    busy: Cell<bool>,
    index: RefCell<RuntimeIndex>,
}
#[derive(Default)]
struct RuntimeIndex {
    revision: Option<crate::StoreRevision>,
    verified: BTreeSet<String>,
    ancestry: BTreeMap<String, (Option<String>, String, u64)>,
    charges: BTreeMap<String, MeterCharges>,
    totals: BTreeMap<String, MeterCharges>,
}
impl RuntimeIndex {
    fn remember(&mut self, node: &Node) -> Result<()> {
        let info = if let Some(parent) = &node.parent {
            self.ancestry
                .get(parent)
                .map(|(_, root, depth)| (node.parent.clone(), root.clone(), depth + 1))
        } else {
            Some((None, node.id.clone(), 0))
        };
        if let Some(info) = info {
            self.ancestry.insert(node.id.clone(), info);
        }
        self.charges
            .insert(node.id.clone(), meter_charges_from_meta(&node.meta)?);
        Ok(())
    }
    fn descends(&self, node: &str, anchor: &str) -> bool {
        let Some((_, root, _)) = self.ancestry.get(node) else {
            return false;
        };
        if root == anchor {
            return true;
        }
        let mut current = Some(node);
        while let Some(id) = current {
            if id == anchor {
                return true;
            }
            current = self
                .ancestry
                .get(id)
                .and_then(|(parent, _, _)| parent.as_deref());
        }
        false
    }
}
/// Share this handle across runtimes to share records and a reentrancy lock.
/// This synchronous API is intentionally single-threaded.
#[derive(Clone)]
pub struct SharedStore(Rc<StoreCell>);
impl SharedStore {
    pub fn new(store: impl RecordingStore + 'static) -> Self {
        Self(Rc::new(StoreCell {
            inner: RefCell::new(Box::new(store)),
            busy: Cell::new(false),
            index: RefCell::new(RuntimeIndex::default()),
        }))
    }
    pub fn borrow(&self) -> Ref<'_, dyn RecordingStore> {
        Ref::map(self.0.inner.borrow(), |s| s.as_ref())
    }
    pub(crate) fn lock(&self) -> Result<OperationGuard> {
        if self.0.busy.replace(true) {
            return Err(Error::Busy);
        }
        Ok(OperationGuard(self.clone()))
    }
    fn synchronize_index(&self) -> bool {
        let revision = self.borrow().cache_revision();
        let mut index = self.0.index.borrow_mut();
        if index.revision != revision || revision.is_none() {
            *index = RuntimeIndex {
                revision,
                ..Default::default()
            };
        }
        revision.is_some()
    }
}
pub(crate) struct OperationGuard(SharedStore);
impl Drop for OperationGuard {
    fn drop(&mut self) {
        self.0 .0.busy.set(false);
    }
}

#[derive(Clone)]
pub struct Runtime {
    store: SharedStore,
    mode: ReplayMode,
    registry: Option<Registry>,
    policies: Vec<Policy>,
    refuse_duplicate_recordings: bool,
    dry_run: bool,
    meters: Vec<Rc<dyn crate::Meter>>,
    core_meters: bool,
    on_node: Option<NodeCallback>,
    callback_errors: Rc<RefCell<Vec<String>>>,
    cleanup_errors: Rc<RefCell<Vec<String>>>,
    reservation_lease_seconds: f64,
}
impl Runtime {
    pub fn new(store: impl RecordingStore + 'static, mode: ReplayMode) -> Self {
        Self::from_shared(SharedStore::new(store), mode)
    }
    pub fn memory(mode: ReplayMode) -> Self {
        Self::new(MemoryStore::new(), mode)
    }
    pub fn from_shared(store: SharedStore, mode: ReplayMode) -> Self {
        Self {
            store,
            mode,
            registry: None,
            policies: Vec::new(),
            refuse_duplicate_recordings: false,
            dry_run: false,
            meters: vec![Rc::new(crate::WallClockMeter)],
            core_meters: true,
            on_node: None,
            callback_errors: Rc::new(RefCell::new(Vec::new())),
            cleanup_errors: Rc::new(RefCell::new(Vec::new())),
            reservation_lease_seconds: 60.0,
        }
    }
    pub fn with_registry(mut self, registry: Registry) -> Self {
        self.registry = Some(registry);
        self
    }
    pub fn with_policy(mut self, policy: Policy) -> Self {
        self.policies.push(policy);
        self
    }
    pub fn with_refuse_duplicate_recordings(mut self, refuse: bool) -> Self {
        self.refuse_duplicate_recordings = refuse;
        self
    }
    pub fn with_dry_run(mut self, dry_run: bool) -> Self {
        self.dry_run = dry_run;
        self
    }
    /// Add or replace one meter by name. Built-in steps/tokens remain enabled.
    pub fn with_meter(mut self, meter: impl crate::Meter + 'static) -> Self {
        self.meters.retain(|m| m.name() != meter.name());
        self.meters.push(Rc::new(meter));
        self
    }
    /// Replace the meter list. An empty list restores default meters, as in Python.
    pub fn with_meters(mut self, meters: Vec<Rc<dyn crate::Meter>>) -> Self {
        if meters.is_empty() {
            self.meters = vec![Rc::new(crate::WallClockMeter)];
            self.core_meters = true;
        } else {
            self.meters = meters;
            self.core_meters = false;
        }
        self
    }
    pub fn with_reservation_lease_seconds(mut self, seconds: f64) -> Result<Self> {
        if !seconds.is_finite() || seconds <= 0.0 {
            return Err(Error::Invalid(
                "reservation lease seconds must be positive and finite".into(),
            ));
        }
        self.reservation_lease_seconds = seconds;
        Ok(self)
    }
    pub fn with_on_node(mut self, callback: NodeCallback) -> Self {
        self.on_node = Some(callback);
        self
    }
    /// Observer errors do not roll back an already committed recording.
    pub fn callback_errors(&self) -> Vec<String> {
        self.callback_errors.borrow().clone()
    }
    /// Measurement cleanup failures, including secondary errors while preserving
    /// a provider's primary error and errors observed during async cancellation.
    pub fn cleanup_errors(&self) -> Vec<String> {
        self.cleanup_errors.borrow().clone()
    }
    fn notify_node(&self, node: &Node) {
        if let Some(callback) = &self.on_node {
            if let Err(error) = callback(node) {
                self.callback_errors.borrow_mut().push(error.to_string());
            }
        }
    }
    fn write_node(&self, node: Node, finalize: bool) -> Result<()> {
        let trusted = self.store.synchronize_index();
        let before = self.store.0.index.borrow().revision;
        if finalize {
            self.store.0.inner.borrow_mut().finalize(node.clone())?;
        } else if node.meta.get("state").and_then(Value::as_str) == Some("pending") {
            self.store
                .0
                .inner
                .borrow_mut()
                .stage_pending(node.clone())?;
        } else {
            self.store.0.inner.borrow_mut().put(node.clone())?;
        }
        if trusted {
            self.refresh_index(&node.id, before.expect("cache token"))?;
        }
        Ok(())
    }
    fn update_meta(&self, id: &str, patch: Value) -> Result<()> {
        let trusted = self.store.synchronize_index();
        let before = self.store.0.index.borrow().revision;
        self.store.0.inner.borrow_mut().update_meta(id, patch)?;
        if trusted {
            self.refresh_index(id, before.expect("cache token"))?;
        }
        Ok(())
    }
    fn refresh_index(&self, id: &str, before: crate::StoreRevision) -> Result<()> {
        let after = self.store.borrow().cache_revision();
        if after.is_none() || after.is_some_and(|token| token.external != before.external) {
            self.store.synchronize_index();
            return Ok(());
        }
        let node = self.store.borrow().get(id)?;
        if self.store.borrow().cache_revision() != after {
            self.store.synchronize_index();
            return Ok(());
        }
        let mut index = self.store.0.index.borrow_mut();
        let old = index.charges.get(id).cloned().unwrap_or_default();
        index.remember(&node)?;
        let new = index.charges.get(id).cloned().unwrap_or_default();
        let anchors: Vec<_> = index
            .totals
            .keys()
            .filter(|anchor| index.descends(id, anchor))
            .cloned()
            .collect();
        for anchor in anchors {
            let total = index.totals.get_mut(&anchor).expect("known anchor");
            for (name, amount) in &old {
                if let Some(value) = total.get_mut(name) {
                    *value = decimal_subtract(*value, *amount)?.max(0.0);
                }
            }
            add_amounts(total, &new)?;
            total.retain(|_, amount| *amount != 0.0);
        }
        if node.meta.get("state").and_then(Value::as_str) == Some("pending") {
            index.verified.remove(id);
        }
        // Preserve the sampled token. A commit after this check is detected on
        // the next operation instead of accidentally accepting stale ancestors.
        index.revision = after;
        Ok(())
    }
    pub fn shared_store(&self) -> SharedStore {
        self.store.clone()
    }
    pub fn store(&self) -> Ref<'_, dyn RecordingStore> {
        self.store.borrow()
    }
    pub fn mode(&self) -> ReplayMode {
        self.mode
    }

    pub fn run(
        &self,
        label: impl Into<String>,
        budget: Option<Budget>,
        attempt: u64,
    ) -> Result<Run> {
        let _guard = self.store.lock()?;
        if let Some(b) = &budget {
            b.validate()?;
        }
        let label = label.into();
        let candidate = Node::make(
            NodeKind::Root,
            None,
            attempt,
            json!({"run":label}),
            None,
            json!({}),
        )?;
        let root = self.structural(candidate)?;
        if let Some(registry) = &self.registry {
            let existing = root.meta.get("registry_digest");
            if existing.is_some_and(|d| d.as_str() != Some(registry.registry_digest())) {
                return Err(Error::Integrity(
                    "run is already bound to a different registry".into(),
                ));
            }
            if existing.is_none() {
                if self.mode == ReplayMode::Replay {
                    return Err(Error::Integrity(
                        "replay root is not bound to registry".into(),
                    ));
                }
                self.update_meta(
                    &root.id,
                    json!({"registry_digest":registry.registry_digest()}),
                )?;
            }
        }
        let scopes = budget
            .into_iter()
            .map(|budget| Scope {
                budget,
                anchor: root.id.clone(),
            })
            .collect();
        Ok(Run {
            runtime: self.clone(),
            root_id: root.id.clone(),
            cursor_id: root.id,
            label,
            scopes,
            pending: BTreeMap::new(),
            avoided: Charges::default(),
            avoided_meters: BTreeMap::new(),
            meter_scopes: Vec::new(),
        })
    }

    pub fn run_with_meters(
        &self,
        label: impl Into<String>,
        budget: Option<Budget>,
        meter_budget: MeterBudget,
        attempt: u64,
    ) -> Result<Run> {
        validate_amounts(&meter_budget)?;
        let mut run = self.run(label, budget, attempt)?;
        run.meter_scopes.push((run.root_id.clone(), meter_budget));
        Ok(run)
    }

    /// Resume at the deepest non-pruned leaf; equal depths choose the lower ID.
    pub fn resume(
        &self,
        label: impl Into<String>,
        budget: Option<Budget>,
        attempt: u64,
    ) -> Result<Run> {
        let label = label.into();
        let root = Node::make(
            NodeKind::Root,
            None,
            attempt,
            json!({"run":label}),
            None,
            json!({}),
        )?;
        if !self.store.borrow().try_exists(&root.id)? {
            return Err(Error::NotFound(root.id));
        }
        let mut run = self.run(label, budget, attempt)?;
        let mut best = (0, run.root_id.clone());
        for node in self.store.borrow().walk(&run.root_id)? {
            if node.meta.get("pruned") == Some(&json!(true)) {
                continue;
            }
            let mut active_child = false;
            for child in self.store.borrow().children(&node.id)? {
                if self.store.borrow().get(&child)?.meta.get("pruned") != Some(&json!(true)) {
                    active_child = true;
                    break;
                }
            }
            if active_child {
                continue;
            }
            self.verified(&node.id)?;
            let depth = run.depth_at(&node.id)?;
            if depth > best.0 || (depth == best.0 && node.id < best.1) {
                best = (depth, node.id);
            }
        }
        run.cursor_id = best.1;
        Ok(run)
    }

    pub fn resume_with_meters(
        &self,
        label: impl Into<String>,
        budget: Option<Budget>,
        meter_budget: MeterBudget,
        attempt: u64,
    ) -> Result<Run> {
        validate_amounts(&meter_budget)?;
        let mut run = self.resume(label, budget, attempt)?;
        run.meter_scopes.push((run.root_id.clone(), meter_budget));
        Ok(run)
    }

    fn verified(&self, id: &str) -> Result<Node> {
        for _ in 0..3 {
            if !self.store.synchronize_index() {
                let store = self.store.borrow();
                let report = verify(&*store, id);
                if !report.ok {
                    return Err(Error::Integrity(
                        report
                            .findings
                            .iter()
                            .map(|f| format!("{}: {}", f.node_id, f.message))
                            .collect::<Vec<_>>()
                            .join("; "),
                    ));
                }
                return store.get(id);
            }
            let revision = self.store.0.index.borrow().revision;
            let mut nodes = Vec::new();
            let mut seen = BTreeSet::new();
            let mut current = Some(id.to_owned());
            while let Some(next) = current {
                if !seen.insert(next.clone()) {
                    return Err(Error::Integrity("cycle in ancestry".into()));
                }
                if next != id && self.store.0.index.borrow().verified.contains(&next) {
                    break;
                }
                let node = self.store.borrow().get(&next)?;
                if node.id != next {
                    return Err(Error::Integrity("lookup key differs from node id".into()));
                }
                node.validate()?;
                current = node.parent.clone();
                nodes.push(node);
            }
            // External commits can race individual SELECTs. Never cache or use a
            // prefix assembled across revisions. Sustained contention falls back
            // to a backend snapshot rather than treating valid writes as tampering.
            if self.store.borrow().cache_revision() != revision {
                self.store.synchronize_index();
                continue;
            }
            let result = nodes.first().expect("requested node checked").clone();
            let mut index = self.store.0.index.borrow_mut();
            for node in nodes.into_iter().rev() {
                index.remember(&node)?;
                if node.meta.get("state").and_then(Value::as_str) != Some("pending") {
                    index.verified.insert(node.id);
                }
            }
            return Ok(result);
        }
        *self.store.0.index.borrow_mut() = RuntimeIndex::default();
        if let Some(nodes) = self.ancestry_snapshot(id)? {
            // Never assign a historical snapshot to the current live revision.
            return Ok(nodes.into_iter().next().expect("snapshot is nonempty"));
        }
        Err(Error::Integrity(
            "recording changed repeatedly during verification".into(),
        ))
    }

    fn ancestry_snapshot(&self, id: &str) -> Result<Option<Vec<Node>>> {
        let Some(nodes) = self.store.borrow().ancestry_snapshot(id)? else {
            return Ok(None);
        };
        let mut expected = Some(id);
        let mut seen = BTreeSet::new();
        for node in &nodes {
            if expected != Some(node.id.as_str()) || !seen.insert(&node.id) {
                return Err(Error::Integrity("invalid ancestry snapshot".into()));
            }
            node.validate()?;
            expected = node.parent.as_deref();
        }
        if expected.is_some() || nodes.is_empty() {
            return Err(Error::Integrity("incomplete ancestry snapshot".into()));
        }
        Ok(Some(nodes))
    }

    fn structural(&self, candidate: Node) -> Result<Node> {
        if self.store.borrow().try_exists(&candidate.id)? {
            return self.verified(&candidate.id);
        }
        if self.mode == ReplayMode::Replay {
            return Err(Error::MissingRecording(candidate.id));
        }
        self.write_node(candidate.clone(), false)?;
        self.notify_node(&candidate);
        Ok(candidate)
    }
}

#[derive(Clone)]
struct Scope {
    budget: Budget,
    anchor: String,
}
#[derive(Clone)]
struct Pending {
    parent: String,
    payload: Value,
    args: Value,
    spec: ActionSpec,
    options: CallOptions,
}

pub struct Run {
    runtime: Runtime,
    root_id: String,
    cursor_id: String,
    label: String,
    scopes: Vec<Scope>,
    pending: BTreeMap<String, Pending>,
    avoided: Charges,
    avoided_meters: MeterCharges,
    meter_scopes: Vec<(String, MeterBudget)>,
}
impl Run {
    pub fn root_id(&self) -> &str {
        &self.root_id
    }
    pub fn cursor_id(&self) -> &str {
        &self.cursor_id
    }
    pub fn cursor(&self) -> Result<Node> {
        self.runtime.store.borrow().get(&self.cursor_id)
    }
    pub fn spent(&self) -> Result<Charges> {
        Ok(self.accounting(&self.root_id)?.0)
    }
    pub fn avoided(&self) -> Charges {
        self.avoided
    }
    pub fn report(&self) -> Result<RunReport> {
        Ok(RunReport {
            spent: self.meter_accounting(&self.root_id)?,
            avoided: self.avoided_meters.clone(),
        })
    }
    pub(crate) fn operation_lock(&self) -> Result<OperationGuard> {
        self.runtime.store.lock()
    }
    pub(crate) fn ensure_unfenced_tool(&self) -> Result<()> {
        if self.runtime.registry.is_some() {
            return Err(Error::Invalid(
                "use registered_tool_call with a registry".into(),
            ));
        }
        Ok(())
    }
    pub fn prune(&mut self) -> Result<()> {
        let _guard = self.operation_lock()?;
        if self.runtime.mode == ReplayMode::Replay {
            return Err(Error::Invalid("replay mode is read-only".into()));
        }
        self.runtime
            .update_meta(&self.cursor_id, json!({"pruned":true}))
    }
    pub fn rollback_steps(&mut self, steps: usize) -> Result<Node> {
        let mut target = self.cursor_id.clone();
        for _ in 0..steps {
            match self.runtime.store.borrow().get(&target)?.parent {
                Some(parent) => target = parent,
                None => break,
            }
        }
        self.rollback(&target)
    }

    pub fn model_call<F>(
        &mut self,
        payload: Value,
        options: CallOptions,
        handler: F,
    ) -> Result<Node>
    where
        F: FnOnce(Value) -> Result<Value>,
    {
        let _guard = self.runtime.store.lock()?;
        self.call(
            NodeKind::ModelCall,
            payload.clone(),
            payload,
            options,
            handler,
        )
    }

    /// Compare a stored model result with a separately recorded live observation.
    /// The continuation cursor returns to the original recording after comparison.
    pub fn revalidate_model_call<F>(
        &mut self,
        payload: Value,
        contract: &crate::ReplayContract,
        options: RevalidationOptions,
        handler: F,
    ) -> Result<crate::RevalidationReport>
    where
        F: FnOnce(Value) -> Result<Value>,
    {
        self.revalidate_model_call_with_comparator(
            payload,
            contract,
            options,
            &crate::NormalizedModelComparator,
            handler,
        )
    }

    pub fn revalidate_model_call_with_comparator<F>(
        &mut self,
        payload: Value,
        contract: &crate::ReplayContract,
        options: RevalidationOptions,
        comparator: &dyn crate::RevalidationComparator,
        handler: F,
    ) -> Result<crate::RevalidationReport>
    where
        F: FnOnce(Value) -> Result<Value>,
    {
        let _guard = self.operation_lock()?;
        let prepared = self.prepare_revalidation(payload, contract, options, comparator)?;
        let dispatch = self.prepare_observation_dispatch(&prepared)?;
        let live = self.finish_call(dispatch, handler(prepared.live_payload.clone()))?;
        self.finish_revalidation(prepared, live, comparator)
    }

    pub async fn revalidate_model_call_async<F, Fut>(
        &mut self,
        payload: Value,
        contract: &crate::ReplayContract,
        options: RevalidationOptions,
        handler: F,
    ) -> Result<crate::RevalidationReport>
    where
        F: FnOnce(Value) -> Fut,
        Fut: std::future::Future<Output = Result<Value>>,
    {
        self.revalidate_model_call_async_with_comparator(
            payload,
            contract,
            options,
            &crate::NormalizedModelComparator,
            handler,
        )
        .await
    }

    pub async fn revalidate_model_call_async_with_comparator<F, Fut>(
        &mut self,
        payload: Value,
        contract: &crate::ReplayContract,
        options: RevalidationOptions,
        comparator: &dyn crate::RevalidationComparator,
        handler: F,
    ) -> Result<crate::RevalidationReport>
    where
        F: FnOnce(Value) -> Fut,
        Fut: std::future::Future<Output = Result<Value>>,
    {
        let _guard = self.operation_lock()?;
        let prepared = self.prepare_revalidation(payload, contract, options, comparator)?;
        let dispatch = self.prepare_observation_dispatch(&prepared)?;
        let outcome = handler(prepared.live_payload.clone()).await;
        let live = self.finish_call(dispatch, outcome)?;
        self.finish_revalidation(prepared, live, comparator)
    }

    /// Stream a live observation, then compare it without replacing the golden recording.
    pub fn revalidate_model_stream<F, I, O>(
        &mut self,
        payload: Value,
        contract: &crate::ReplayContract,
        options: RevalidationOptions,
        handler: F,
        on_delta: O,
    ) -> Result<crate::RevalidationReport>
    where
        F: FnOnce(Value) -> Result<I>,
        I: IntoIterator<Item = Result<Value>>,
        O: FnMut(&Value) -> Result<()>,
    {
        self.revalidate_model_stream_with_comparator(
            payload,
            contract,
            options,
            &crate::NormalizedModelComparator,
            handler,
            on_delta,
        )
    }

    pub fn revalidate_model_stream_with_comparator<F, I, O>(
        &mut self,
        payload: Value,
        contract: &crate::ReplayContract,
        options: RevalidationOptions,
        comparator: &dyn crate::RevalidationComparator,
        handler: F,
        on_delta: O,
    ) -> Result<crate::RevalidationReport>
    where
        F: FnOnce(Value) -> Result<I>,
        I: IntoIterator<Item = Result<Value>>,
        O: FnMut(&Value) -> Result<()>,
    {
        let keep_chunks = options.keep_chunks;
        self.revalidate_model_call_with_comparator(
            payload,
            contract,
            options,
            comparator,
            |live_payload| crate::consume_stream(handler(live_payload)?, keep_chunks, on_delta),
        )
    }

    /// Construct the asynchronous pull stream only after governance gates pass.
    pub async fn revalidate_model_stream_async<F, N, Fut, O, OFut>(
        &mut self,
        payload: Value,
        contract: &crate::ReplayContract,
        options: RevalidationOptions,
        handler: F,
        on_delta: O,
    ) -> Result<crate::RevalidationReport>
    where
        F: FnOnce(Value) -> Result<N>,
        N: FnMut() -> Fut,
        Fut: std::future::Future<Output = Result<Option<Value>>>,
        O: FnMut(Value) -> OFut,
        OFut: std::future::Future<Output = Result<()>>,
    {
        self.revalidate_model_stream_async_with_comparator(
            payload,
            contract,
            options,
            &crate::NormalizedModelComparator,
            handler,
            on_delta,
        )
        .await
    }

    pub async fn revalidate_model_stream_async_with_comparator<F, N, Fut, O, OFut>(
        &mut self,
        payload: Value,
        contract: &crate::ReplayContract,
        options: RevalidationOptions,
        comparator: &dyn crate::RevalidationComparator,
        handler: F,
        on_delta: O,
    ) -> Result<crate::RevalidationReport>
    where
        F: FnOnce(Value) -> Result<N>,
        N: FnMut() -> Fut,
        Fut: std::future::Future<Output = Result<Option<Value>>>,
        O: FnMut(Value) -> OFut,
        OFut: std::future::Future<Output = Result<()>>,
    {
        let keep_chunks = options.keep_chunks;
        self.revalidate_model_call_async_with_comparator(
            payload,
            contract,
            options,
            comparator,
            |live_payload| async move {
                crate::consume_stream_async(handler(live_payload)?, keep_chunks, on_delta).await
            },
        )
        .await
    }

    fn prepare_observation_dispatch(
        &mut self,
        prepared: &PreparedRevalidation,
    ) -> Result<PendingDispatch> {
        match self.prepare_call_metered(
            NodeKind::ModelCall,
            prepared.observation_payload.clone(),
            CallOptions {
                attempt: 0,
                estimated_tokens: prepared.options.call.estimated_tokens,
            },
            Some(prepared.live_payload.clone()),
        )? {
            CallPreparation::Recorded(_) => unreachable!("new observation identity"),
            CallPreparation::Dispatch(dispatch) => Ok(dispatch),
        }
    }

    fn prepare_revalidation(
        &self,
        payload: Value,
        contract: &crate::ReplayContract,
        options: RevalidationOptions,
        comparator: &dyn crate::RevalidationComparator,
    ) -> Result<PreparedRevalidation> {
        if self.runtime.mode != ReplayMode::Record || self.runtime.dry_run {
            return Err(Error::Invalid(
                "live revalidation requires record mode without dry_run".into(),
            ));
        }
        contract.validate()?;
        if comparator.name().trim().is_empty() {
            return Err(Error::Invalid("comparator name cannot be empty".into()));
        }
        let candidate = Node::make(
            NodeKind::ModelCall,
            Some(&self.cursor_id),
            options.call.attempt,
            payload.clone(),
            None,
            json!({}),
        )?;
        if !self.runtime.store.borrow().try_exists(&candidate.id)? {
            return Err(Error::MissingRecording(candidate.id));
        }
        let recorded = self.runtime.verified(&candidate.id)?;
        if !recorded.result.as_ref().is_some_and(Value::is_object)
            || recorded.result_digest.is_none()
        {
            return Err(Error::Integrity(
                "recorded model result is not a replayable object".into(),
            ));
        }
        let recorded_contract = crate::extract_replay_contract(&recorded.payload)?;
        let live_contract = contract.to_value();
        let live_payload = options.live_payload.clone().unwrap_or(payload);
        if options.live_payload.is_some() {
            if let Some(bound) = crate::extract_replay_contract(&live_payload)? {
                if bound != live_contract {
                    return Err(Error::Invalid(
                        "live payload replay contract does not match live contract".into(),
                    ));
                }
            }
        }
        let observation_payload = crate::make_revalidation_payload(
            &live_payload,
            &options.observation_id,
            &recorded,
            contract,
            comparator.name(),
        )?;
        let observation = Node::make(
            NodeKind::ModelCall,
            Some(&self.cursor_id),
            0,
            observation_payload.clone(),
            None,
            json!({}),
        )?;
        if self.runtime.store.borrow().try_exists(&observation.id)? {
            return Err(Error::Integrity(format!(
                "revalidation observation already exists: {}",
                options.observation_id
            )));
        }
        Ok(PreparedRevalidation {
            recorded,
            recorded_contract,
            live_contract,
            live_payload,
            observation_payload,
            options,
        })
    }

    fn finish_revalidation(
        &mut self,
        prepared: PreparedRevalidation,
        live: Node,
        comparator: &dyn crate::RevalidationComparator,
    ) -> Result<crate::RevalidationReport> {
        let PreparedRevalidation {
            recorded,
            recorded_contract,
            live_contract,
            options,
            ..
        } = prepared;
        let outcome = (|| -> Result<crate::RevalidationReport> {
            let comparison = comparator.compare(
                recorded.result.as_ref().expect("validated recorded result"),
                live.result.as_ref().expect("validated live result"),
            )?;
            comparison.validate()?;
            let exact_match = recorded.result_text == live.result_text;
            let mut evidence_payload = json!({"event":"model_revalidation","format":"pollard/revalidation/v1",
                "observation_id":options.observation_id,"recorded_node_id":recorded.id,"live_node_id":live.id,
                "recorded_result_digest":recorded.result_digest,"live_result_digest":live.result_digest,
                "comparator":comparator.name(),"comparison":comparison.to_value(),"exact_match":exact_match,
                "live_contract":live_contract});
            if let Some(bound) = &recorded_contract {
                evidence_payload["recorded_contract"] = bound.clone();
            }
            let evidence = self.runtime.structural(Node::make(
                NodeKind::Note,
                Some(&live.id),
                0,
                evidence_payload,
                None,
                json!({}),
            )?)?;
            Ok(crate::RevalidationReport {
                observation_id: options.observation_id.clone(),
                recorded_node_id: recorded.id.clone(),
                live_node_id: live.id.clone(),
                evidence_node_id: evidence.id,
                comparator: comparator.name().into(),
                matched: comparison.matched,
                exact_match,
                recorded_result_digest: recorded.result_digest.clone().expect("validated digest"),
                live_result_digest: live.result_digest.clone().expect("live digest"),
                difference_paths: comparison.difference_paths,
                differences_truncated: comparison.truncated,
                recorded_contract,
                live_contract,
                charges: meter_charges_from_meta(&live.meta)?,
            })
        })();
        if outcome.is_err() {
            let failure = Node::make(
                NodeKind::Note,
                Some(&live.id),
                0,
                json!({
                "event":"model_revalidation_comparison_failed","format":"pollard/revalidation/v1",
                "observation_id":options.observation_id,"recorded_node_id":recorded.id,"live_node_id":live.id,
                "recorded_result_digest":recorded.result_digest,"live_result_digest":live.result_digest,
                "comparator":comparator.name(),"error_type":"ComparatorError"}),
                None,
                json!({}),
            );
            if let Ok(failure) = failure {
                let _ = self.runtime.structural(failure);
            }
        }
        self.cursor_id = recorded.id;
        outcome
    }

    /// Unfenced tool callback. Disallowed when a registry is attached.
    pub fn tool_call<F>(
        &mut self,
        name: &str,
        args: Value,
        options: CallOptions,
        handler: F,
    ) -> Result<Node>
    where
        F: FnOnce(Value) -> Result<Value>,
    {
        let _guard = self.runtime.store.lock()?;
        if self.runtime.registry.is_some() {
            return Err(Error::Invalid(
                "use registered_tool_call with a registry".into(),
            ));
        }
        if !args.is_object() {
            return Err(Error::Invalid("tool args must be object".into()));
        }
        let payload = json!({"tool":name,"args":args});
        self.call(
            NodeKind::ToolCall,
            payload.clone(),
            payload,
            options,
            handler,
        )
    }

    pub fn registered_tool_call(
        &mut self,
        name: &str,
        version: Option<&str>,
        args: Value,
        options: CallOptions,
    ) -> Result<Node> {
        let _guard = self.operation_lock()?;
        match self.prepare_registered_tool(name, version, args, options, false)? {
            RegisteredPreparation::Ready(node) => Ok(node),
            RegisteredPreparation::Dispatch {
                dispatch,
                args,
                handler: PreparedHandler::Sync(handler),
            } => self.finish_call(dispatch, handler(args)),
            RegisteredPreparation::Dispatch {
                handler: PreparedHandler::Async(_),
                ..
            } => unreachable!("sync preparation rejects async-only handlers"),
        }
    }

    pub async fn registered_tool_call_async(
        &mut self,
        name: &str,
        version: Option<&str>,
        args: Value,
        options: CallOptions,
    ) -> Result<Node> {
        let _guard = self.operation_lock()?;
        match self.prepare_registered_tool(name, version, args, options, true)? {
            RegisteredPreparation::Ready(node) => Ok(node),
            RegisteredPreparation::Dispatch {
                dispatch,
                args,
                handler,
            } => {
                let outcome = match handler {
                    PreparedHandler::Sync(handler) => handler(args),
                    PreparedHandler::Async(handler) => handler(args).await,
                };
                self.finish_call(dispatch, outcome)
            }
        }
    }

    fn prepare_registered_tool(
        &mut self,
        name: &str,
        version: Option<&str>,
        args: Value,
        options: CallOptions,
        allow_async: bool,
    ) -> Result<RegisteredPreparation> {
        canonical_bytes(&args)?;
        if !args.is_object() {
            return Err(Error::Invalid("tool args must be object".into()));
        }
        let registry = self
            .runtime
            .registry
            .clone()
            .ok_or_else(|| Error::Invalid("no registry attached".into()))?;
        let spec = match registry.get(name, None) {
            Ok(spec) => spec.clone(),
            Err(_) => {
                return self.refuse_policy(
                    format!(
                        "unknown registered action: {}",
                        version
                            .map(|v| format!("{name}@{v}"))
                            .unwrap_or_else(|| name.into())
                    ),
                    &json!({"tool":name,"args":args}),
                )
            }
        };
        let audit_args = spec.redact_args(&args)?;
        let blocked = json!({"tool":name,"args":audit_args});
        if version.is_some_and(|v| v != spec.version()) {
            return self.refuse_policy(
                format!(
                    "unknown registered action: {name}@{}",
                    version.unwrap_or_default()
                ),
                &blocked,
            );
        }
        if let Err(e) = spec.validate_args(&args) {
            let detail = match e {
                Error::Invalid(detail) => detail,
                other => other.to_string(),
            };
            return self.refuse_policy(format!("schema validation failed: {detail}"), &blocked);
        }
        let payload = json!({"tool":spec.name(),"version":spec.version(),"args":audit_args,"spec_digest":spec.spec_digest(),"registry_digest":registry.registry_digest()});
        if self.runtime.mode == ReplayMode::Replay {
            return match self.prepare_call(NodeKind::ToolCall, payload, options)? {
                CallPreparation::Recorded(node) => Ok(RegisteredPreparation::Ready(node)),
                CallPreparation::Dispatch(_) => unreachable!("strict replay never dispatches"),
            };
        }
        let candidate = Node::make(
            NodeKind::ToolCall,
            Some(&self.cursor_id),
            options.attempt,
            payload.clone(),
            None,
            json!({}),
        )?;
        if self.runtime.mode == ReplayMode::Record
            && self.runtime.refuse_duplicate_recordings
            && self.runtime.store.borrow().try_exists(&candidate.id)?
        {
            return Err(Error::DuplicateRecording(candidate.id));
        }
        for policy in self.runtime.policies.clone() {
            let ctx = PolicyContext {
                spec: spec.clone(),
                args: args.clone(),
                cursor_id: self.cursor_id.clone(),
                run_label: self.label.clone(),
                counters: self.report()?.spent,
            };
            match policy(&ctx) {
                Decision::Allow => {}
                Decision::Deny => return self.refuse_policy("denied by policy".into(), &payload),
                Decision::Confirm => {
                    self.pending.insert(
                        candidate.id.clone(),
                        Pending {
                            parent: self.cursor_id.clone(),
                            payload,
                            args,
                            spec,
                            options,
                        },
                    );
                    return Err(Error::ConfirmationRequired {
                        token: candidate.id,
                    });
                }
            }
        }
        if self.runtime.mode == ReplayMode::Hybrid
            && self.runtime.store.borrow().try_exists(&candidate.id)?
        {
            return match self.prepare_call(NodeKind::ToolCall, payload, options)? {
                CallPreparation::Recorded(node) => Ok(RegisteredPreparation::Ready(node)),
                CallPreparation::Dispatch(_) => unreachable!("existing hybrid recording"),
            };
        }
        if self.runtime.dry_run && spec.side_effects() {
            let estimates = self.precheck(NodeKind::ToolCall, &payload, options)?;
            let reservation = self.reserve_dispatch(NodeKind::ToolCall, &payload, &estimates)?;
            let node = Node::make(
                NodeKind::ToolCall,
                Some(&self.cursor_id),
                options.attempt,
                payload,
                None,
                json!({"dry_run":true,"charges":{"steps":1}}),
            )?;
            if let Some(reservation) = &reservation {
                self.runtime.store.0.inner.borrow_mut().settle_budget(
                    &reservation.id,
                    &BTreeMap::from([("steps".into(), Decimal::ONE)]),
                )?;
            }
            let node = self.runtime.structural(node)?;
            self.cursor_id = node.id.clone();
            return Ok(RegisteredPreparation::Ready(node));
        }
        let handler = if allow_async {
            spec.async_handler()
                .map(PreparedHandler::Async)
                .or_else(|| spec.handler().map(PreparedHandler::Sync))
        } else {
            spec.handler().map(PreparedHandler::Sync)
        };
        let Some(handler) = handler else {
            return self.refuse_policy("registered action has no handler".into(), &payload);
        };
        match self.prepare_call(NodeKind::ToolCall, payload, options)? {
            CallPreparation::Recorded(node) => Ok(RegisteredPreparation::Ready(node)),
            CallPreparation::Dispatch(dispatch) => Ok(RegisteredPreparation::Dispatch {
                dispatch,
                args,
                handler,
            }),
        }
    }

    /// Consume a policy-issued token exactly once at its original cursor.
    pub fn confirm(&mut self, token: &str) -> Result<Node> {
        let _guard = self.runtime.store.lock()?;
        let pending = self
            .pending
            .remove(token)
            .ok_or_else(|| Error::NotFound(token.into()))?;
        if self.cursor_id != pending.parent {
            return Err(Error::Invalid("cannot confirm after cursor moved".into()));
        }
        let Some(handler) = pending.spec.handler() else {
            return self.refuse_policy("registered action has no handler".into(), &pending.payload);
        };
        self.call(
            NodeKind::ToolCall,
            pending.payload,
            pending.args,
            pending.options,
            move |args| handler(args),
        )
    }

    pub async fn confirm_async(&mut self, token: &str) -> Result<Node> {
        let _guard = self.operation_lock()?;
        let pending = self
            .pending
            .remove(token)
            .ok_or_else(|| Error::NotFound(token.into()))?;
        if self.cursor_id != pending.parent {
            return Err(Error::Invalid("cannot confirm after cursor moved".into()));
        }
        let handler = pending
            .spec
            .async_handler()
            .map(PreparedHandler::Async)
            .or_else(|| pending.spec.handler().map(PreparedHandler::Sync));
        let Some(handler) = handler else {
            return self.refuse_policy("registered action has no handler".into(), &pending.payload);
        };
        match self.prepare_call(NodeKind::ToolCall, pending.payload, pending.options)? {
            CallPreparation::Recorded(node) => Ok(node),
            CallPreparation::Dispatch(dispatch) => {
                let outcome = match handler {
                    PreparedHandler::Sync(handler) => handler(pending.args),
                    PreparedHandler::Async(handler) => handler(pending.args).await,
                };
                self.finish_call(dispatch, outcome)
            }
        }
    }

    pub fn note(&mut self, payload: Value, attempt: u64) -> Result<Node> {
        let _guard = self.runtime.store.lock()?;
        self.note_inner(payload, attempt)
    }
    fn note_inner(&mut self, payload: Value, attempt: u64) -> Result<Node> {
        let candidate = Node::make(
            NodeKind::Note,
            Some(&self.cursor_id),
            attempt,
            payload.clone(),
            None,
            json!({}),
        )?;
        if self.runtime.mode != ReplayMode::Replay {
            self.precheck(NodeKind::Note, &payload, CallOptions::default())?;
        }
        let node = self.runtime.structural(candidate)?;
        self.cursor_id = node.id.clone();
        Ok(node)
    }

    /// Branch shares stored charges and budget scopes. Adopt explicitly to move
    /// the parent's cursor; abandoning a branch never refunds dispatched work.
    pub fn branch(&mut self, attempt: u64, budget: Option<Budget>) -> Result<Self> {
        let _guard = self.runtime.store.lock()?;
        if let Some(b) = &budget {
            b.validate()?;
        }
        let old_cursor = self.cursor_id.clone();
        let anchor = self.note_inner(json!({"branch":true}), attempt)?;
        self.cursor_id = old_cursor;
        let mut scopes = self.scopes.clone();
        if let Some(budget) = budget {
            scopes.push(Scope {
                budget,
                anchor: anchor.id.clone(),
            });
        }
        Ok(Self {
            runtime: self.runtime.clone(),
            root_id: self.root_id.clone(),
            cursor_id: anchor.id,
            label: self.label.clone(),
            scopes,
            pending: BTreeMap::new(),
            avoided: Charges::default(),
            avoided_meters: BTreeMap::new(),
            meter_scopes: self.meter_scopes.clone(),
        })
    }
    pub fn branch_with_meters(
        &mut self,
        attempt: u64,
        budget: Option<Budget>,
        meter_budget: MeterBudget,
    ) -> Result<Self> {
        validate_amounts(&meter_budget)?;
        let mut branch = self.branch(attempt, budget)?;
        branch
            .meter_scopes
            .push((branch.cursor_id.clone(), meter_budget));
        Ok(branch)
    }
    pub fn adopt(&mut self, branch: &Self) -> Result<Node> {
        let _guard = self.runtime.store.lock()?;
        if !Rc::ptr_eq(&self.runtime.store.0, &branch.runtime.store.0)
            || self.root_id != branch.root_id
        {
            return Err(Error::Invalid(
                "branch belongs to another run or store".into(),
            ));
        }
        if !self.is_ancestor(&self.cursor_id, &branch.cursor_id)? {
            return Err(Error::Invalid(
                "branch does not descend from current cursor".into(),
            ));
        }
        self.cursor_id = branch.cursor_id.clone();
        self.cursor()
    }
    pub fn rollback(&mut self, target: &str) -> Result<Node> {
        let _guard = self.runtime.store.lock()?;
        if !self.is_ancestor(target, &self.cursor_id)? {
            return Err(Error::Invalid("rollback target must be an ancestor".into()));
        }
        self.cursor_id = target.into();
        self.cursor()
    }

    fn is_ancestor(&self, target: &str, from: &str) -> Result<bool> {
        let mut current = Some(from.to_owned());
        let mut seen = BTreeSet::new();
        while let Some(id) = current {
            if !seen.insert(id.clone()) {
                return Err(Error::Integrity("cycle in ancestry".into()));
            }
            if id == target {
                return Ok(true);
            }
            current = self.runtime.store.borrow().get(&id)?.parent;
        }
        Ok(false)
    }

    fn accounting(&self, anchor: &str) -> Result<(Charges, bool)> {
        let total = self.meter_accounting(anchor)?;
        Ok((
            charges_from_meta(&json!({"charges":amounts_json(&total)}))?,
            false,
        ))
    }

    fn meter_accounting(&self, anchor: &str) -> Result<MeterCharges> {
        for _ in 0..3 {
            let trusted = self.runtime.store.synchronize_index();
            if trusted {
                if let Some(total) = self.runtime.store.0.index.borrow().totals.get(anchor) {
                    return Ok(total.clone());
                }
                self.runtime.verified(anchor)?;
            }
            let revision = self.runtime.store.0.index.borrow().revision;
            let nodes = self.runtime.store.borrow().walk(anchor)?;
            let mut total = MeterCharges::new();
            for node in &nodes {
                node.validate()?;
                add_amounts(&mut total, &meter_charges_from_meta(&node.meta)?)?;
            }
            if trusted {
                if self.runtime.store.borrow().cache_revision() != revision {
                    self.runtime.store.synchronize_index();
                    continue;
                }
                let mut index = self.runtime.store.0.index.borrow_mut();
                for node in &nodes {
                    index.remember(node)?;
                }
                index.totals.insert(anchor.into(), total.clone());
            }
            return Ok(total);
        }
        *self.runtime.store.0.index.borrow_mut() = RuntimeIndex::default();
        if let Some(nodes) = self.runtime.store.borrow().subtree_snapshot(anchor)? {
            let mut seen = BTreeSet::new();
            let mut total = MeterCharges::new();
            for node in &nodes {
                if (seen.is_empty() && node.id != anchor)
                    || (!seen.is_empty()
                        && !node.parent.as_ref().is_some_and(|id| seen.contains(id)))
                    || !seen.insert(node.id.clone())
                {
                    return Err(Error::Integrity("invalid subtree snapshot".into()));
                }
                node.validate()?;
                add_amounts(&mut total, &meter_charges_from_meta(&node.meta)?)?;
            }
            if nodes.is_empty() {
                return Err(Error::Integrity("incomplete subtree snapshot".into()));
            }
            return Ok(total);
        }
        Err(Error::Integrity(
            "recording changed repeatedly during accounting".into(),
        ))
    }

    fn depth(&self) -> Result<u64> {
        self.depth_at(&self.cursor_id)
    }
    fn depth_at(&self, node_id: &str) -> Result<u64> {
        if self.runtime.store.synchronize_index() {
            self.runtime.verified(node_id)?;
            if let Some((_, _, depth)) = self.runtime.store.0.index.borrow().ancestry.get(node_id) {
                return Ok(*depth);
            }
        }

        if let Some(nodes) = self.runtime.ancestry_snapshot(node_id)? {
            return u64::try_from(nodes.len() - 1)
                .map_err(|_| Error::Integrity("depth overflow".into()));
        }

        let mut current = Some(node_id.to_owned());
        let mut count = 0u64;
        let mut seen = BTreeSet::new();
        while let Some(id) = current {
            if !seen.insert(id.clone()) {
                return Err(Error::Integrity("cycle in ancestry".into()));
            }
            current = self.runtime.store.borrow().get(&id)?.parent;
            if current.is_some() {
                count = count
                    .checked_add(1)
                    .ok_or_else(|| Error::Integrity("depth overflow".into()))?;
            }
        }
        Ok(count)
    }

    fn precheck(
        &mut self,
        kind: NodeKind,
        payload: &Value,
        options: CallOptions,
    ) -> Result<MeterCharges> {
        self.runtime.verified(&self.cursor_id)?;
        let call = matches!(kind, NodeKind::ModelCall | NodeKind::ToolCall);
        let mut estimates = MeterCharges::new();
        if call && self.runtime.core_meters {
            estimates.insert("steps".into(), 1.0);
        }
        for meter in self.runtime.meters.clone() {
            let estimate = match meter.estimate(kind, payload) {
                Ok(estimate) => estimate,
                Err(Error::MeterPrecheckRefusal(refusal)) => {
                    refusal.validate()?;
                    let mut audit = json!({"reason":refusal.reason,"meter":meter.name(),"blocked_kind":kind.as_str(),
                        "blocked_payload_digest":digest_payload(payload)?,"detail":refusal.detail});
                    if let Some(requested) = &refusal.requested {
                        audit["requested"] = json!(requested);
                    }
                    if let Some(remaining) = &refusal.remaining {
                        audit["remaining"] = json!(remaining);
                    }
                    let node = self.runtime.structural(Node::make(
                        NodeKind::Refusal,
                        Some(&self.cursor_id),
                        0,
                        audit,
                        None,
                        refusal.audit_meta.clone(),
                    )?)?;
                    self.cursor_id = node.id.clone();
                    return Err(Error::BudgetExceeded {
                        meter: meter.name().into(),
                        node_id: node.id,
                    });
                }
                Err(error) => return Err(error),
            };
            if let Some(amount) = estimate {
                validate_amount(amount, meter.name())?;
                estimates.insert(meter.name().into(), amount);
            }
        }
        if call {
            if let Some(tokens) = options.estimated_tokens {
                safe_amount(tokens, "estimated tokens")?;
                estimates.insert("tokens".into(), tokens as f64);
            }
        }
        let next_depth = if self.scopes.iter().any(|scope| scope.budget.depth.is_some())
            || self
                .meter_scopes
                .iter()
                .any(|(_, budget)| budget.contains_key("depth"))
        {
            self.depth()?
                .checked_add(1)
                .ok_or_else(|| Error::Integrity("depth overflow".into()))?
        } else {
            0
        };
        for scope in self.scopes.clone() {
            let spent = self.meter_accounting(&scope.anchor)?;
            for (meter, limit, already, requested) in [
                (
                    "steps",
                    scope.budget.steps,
                    *spent.get("steps").unwrap_or(&0.0),
                    *estimates.get("steps").unwrap_or(&0.0),
                ),
                (
                    "tokens",
                    scope.budget.tokens,
                    *spent.get("tokens").unwrap_or(&0.0),
                    *estimates.get("tokens").unwrap_or(&0.0),
                ),
                ("depth", scope.budget.depth, 0.0, next_depth as f64),
            ] {
                if let Some(limit) = limit {
                    if exceeds_limit(limit as f64, already, requested)? {
                        return self.refuse_budget(
                            kind,
                            payload,
                            meter,
                            requested,
                            decimal_subtract(limit as f64, already)?,
                            meter == "tokens" && call,
                            "budget",
                        );
                    }
                }
            }
        }
        for (anchor, budget) in self.meter_scopes.clone() {
            let spent = self.meter_accounting(&anchor)?;
            for (meter, limit) in budget {
                let already = if meter == "depth" {
                    0.0
                } else {
                    *spent.get(&meter).unwrap_or(&0.0)
                };
                let requested = if meter == "depth" {
                    next_depth as f64
                } else {
                    *estimates.get(&meter).unwrap_or(&0.0)
                };
                if exceeds_limit(limit, already, requested)? {
                    return self.refuse_budget(
                        kind,
                        payload,
                        &meter,
                        requested,
                        decimal_subtract(limit, already)?,
                        meter != "steps" && meter != "depth",
                        "budget",
                    );
                }
            }
        }
        for meter in self.runtime.meters.clone() {
            if let Some(window) = meter.window() {
                let ledger_key = meter
                    .window_ledger_key(&self.root_id)?
                    .ok_or_else(|| Error::Invalid("window meter requires a ledger key".into()))?;
                let cutoff = unix_seconds() - window.seconds;
                let mut spent = 0.0;
                for node in self.runtime.store.borrow().walk(&self.root_id)? {
                    node.validate()?;
                    if let Some(events) = node.meta.get("window_events").and_then(Value::as_array) {
                        for event in events {
                            if event
                                .get("keys")
                                .and_then(|keys| keys.get(meter.name()))
                                .and_then(Value::as_str)
                                == Some(ledger_key.as_str())
                                && event
                                    .get("time")
                                    .and_then(Value::as_f64)
                                    .is_some_and(|time| time > cutoff)
                            {
                                spent = decimal_add(
                                    spent,
                                    meter_charges_from_meta(event)?
                                        .get(meter.name())
                                        .copied()
                                        .unwrap_or(0.0),
                                )?;
                            }
                        }
                    } else if node
                        .meta
                        .get("window_keys")
                        .and_then(|keys| keys.get(meter.name()))
                        .and_then(Value::as_str)
                        == Some(ledger_key.as_str())
                        && node
                            .meta
                            .get("created_at_unix")
                            .and_then(Value::as_f64)
                            .is_some_and(|time| time > cutoff)
                    {
                        spent = decimal_add(
                            spent,
                            meter_charges_from_meta(&node.meta)?
                                .get(meter.name())
                                .copied()
                                .unwrap_or(0.0),
                        )?;
                    }
                }
                let requested = estimates.get(meter.name()).copied().unwrap_or(0.0);
                if exceeds_limit(window.limit, spent, requested)? {
                    let payload = json!({"reason":"window","meter":meter.name(),"blocked_kind":kind.as_str(),
                        "blocked_payload_digest":digest_payload(payload)?,"requested":requested.to_string(),
                        "remaining":decimal_subtract(window.limit,spent)?.to_string(), "window_seconds":if window.seconds.fract()==0.0 { json!(window.seconds as u64) } else { json!(window.seconds.to_string()) }});
                    let refusal = self.runtime.structural(Node::make(
                        NodeKind::Refusal,
                        Some(&self.cursor_id),
                        0,
                        payload,
                        None,
                        json!({}),
                    )?)?;
                    self.cursor_id = refusal.id.clone();
                    return Err(Error::BudgetExceeded {
                        meter: meter.name().into(),
                        node_id: refusal.id,
                    });
                }
            }
        }
        Ok(estimates)
    }

    #[allow(clippy::too_many_arguments)]
    fn refuse_budget<T>(
        &mut self,
        kind: NodeKind,
        payload: &Value,
        meter: &str,
        requested: f64,
        remaining: f64,
        estimated: bool,
        detail: &str,
    ) -> Result<T> {
        let mut audit = json!({"reason":"budget","meter":meter,"blocked_kind":kind.as_str(),"blocked_payload_digest":digest_payload(payload)?,"requested":requested.to_string(),"remaining":remaining.to_string()});
        if estimated {
            audit["estimated"] = json!("true");
        }
        if detail != "budget" {
            audit["detail"] = json!(detail);
        }
        let candidate = Node::make(
            NodeKind::Refusal,
            Some(&self.cursor_id),
            0,
            audit,
            None,
            json!({}),
        )?;
        let node = self.runtime.structural(candidate)?;
        self.cursor_id = node.id.clone();
        Err(Error::BudgetExceeded {
            meter: meter.into(),
            node_id: node.id,
        })
    }

    fn refuse_policy<T>(&mut self, detail: String, payload: &Value) -> Result<T> {
        let mut audit = json!({"reason":"policy","detail":detail,"blocked_kind":"tool_call","blocked_payload_digest":digest_payload(payload)?});
        if let Some(registry) = &self.runtime.registry {
            audit["registry_digest"] = json!(registry.registry_digest());
        }
        let candidate = Node::make(
            NodeKind::Refusal,
            Some(&self.cursor_id),
            0,
            audit,
            None,
            json!({}),
        )?;
        let node = self.runtime.structural(candidate)?;
        self.cursor_id = node.id.clone();
        Err(Error::PolicyViolation {
            detail,
            node_id: node.id,
        })
    }

    fn call<F>(
        &mut self,
        kind: NodeKind,
        payload: Value,
        handler_args: Value,
        options: CallOptions,
        handler: F,
    ) -> Result<Node>
    where
        F: FnOnce(Value) -> Result<Value>,
    {
        match self.prepare_call(kind, payload, options)? {
            CallPreparation::Recorded(node) => Ok(node),
            CallPreparation::Dispatch(dispatch) => {
                let outcome = handler(handler_args);
                self.finish_call(dispatch, outcome)
            }
        }
    }

    fn reserve_dispatch(
        &mut self,
        kind: NodeKind,
        payload: &Value,
        estimates: &MeterCharges,
    ) -> Result<Option<DispatchReservation>> {
        if !self.runtime.store.borrow().supports_reservations() {
            return Ok(None);
        }
        let mut limits: BTreeMap<String, MeterBudget> = BTreeMap::new();
        for scope in &self.scopes {
            let entry = limits.entry(scope.anchor.clone()).or_default();
            for (name, limit) in [
                ("steps", scope.budget.steps),
                ("tokens", scope.budget.tokens),
            ] {
                if let Some(limit) = limit {
                    entry.insert(name.into(), limit as f64);
                }
            }
        }
        for (anchor, extra) in &self.meter_scopes {
            let entry = limits.entry(anchor.clone()).or_default();
            for (name, limit) in extra {
                if name != "depth" {
                    entry
                        .entry(name.clone())
                        .and_modify(|old| *old = old.min(*limit))
                        .or_insert(*limit);
                }
            }
        }
        let mut budgets = Vec::new();
        for (anchor, limits) in limits {
            if limits.is_empty() {
                continue;
            }
            let mut baseline = MeterCharges::new();
            for node in self.runtime.store.borrow().walk(&anchor)? {
                node.validate()?;
                // Outstanding reservations are included atomically by the arbiter.
                if node.meta.get("state").and_then(Value::as_str) == Some("pending")
                    && node.meta.get("reservation_id").is_some()
                {
                    continue;
                }
                add_amounts(&mut baseline, &meter_charges_from_meta(&node.meta)?)?;
            }
            budgets.push(crate::BudgetReservation {
                scope_id: anchor,
                limits: decimal_amounts(&limits)?,
                baseline: decimal_amounts(&baseline)?,
                estimates: decimal_amounts(estimates)?,
            });
        }
        let mut windows = Vec::new();
        for meter in &self.runtime.meters {
            if let Some(window) = meter.window() {
                windows.push(crate::WindowReservation {
                    ledger_key: meter.window_ledger_key(&self.root_id)?.ok_or_else(|| {
                        Error::Invalid("window meter requires a ledger key".into())
                    })?,
                    meter: meter.name().into(),
                    limit: decimal_amount(window.limit)?,
                    amount: decimal_amount(estimates.get(meter.name()).copied().unwrap_or(0.0))?,
                    window_seconds: window.seconds,
                });
            }
        }
        if budgets.is_empty() && windows.is_empty() {
            return Ok(None);
        }
        static SERIAL: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let serial = SERIAL.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos();
        let id = digest_payload(
            &json!({"root":self.root_id,"process":std::process::id(),"time":now.to_string(),"serial":serial.to_string()}),
        )?;
        let check = self.runtime.store.0.inner.borrow_mut().reserve_budget(
            &id,
            &budgets,
            &windows,
            self.runtime.reservation_lease_seconds,
        )?;
        let Some(check) = check else {
            return Ok(None);
        };
        if !check.ok {
            let meter = check.meter.unwrap_or_else(|| "unknown".into());
            let mut audit = json!({"reason":check.reason,"meter":meter,"blocked_kind":kind.as_str(),
                "blocked_payload_digest":digest_payload(payload)?,"requested":check.requested.normalize().to_string(),
                "remaining":check.remaining.normalize().to_string()});
            if let Some(seconds) = check.window_seconds {
                audit["window_seconds"] = if seconds.fract() == 0.0 {
                    json!(seconds as u64)
                } else {
                    json!(seconds.to_string())
                };
            }
            let node = self.runtime.structural(Node::make(
                NodeKind::Refusal,
                Some(&self.cursor_id),
                0,
                audit,
                None,
                json!({}),
            )?)?;
            self.cursor_id = node.id.clone();
            return Err(Error::BudgetExceeded {
                meter,
                node_id: node.id,
            });
        }
        let heartbeat = self.runtime.store.borrow().lease_renewer().map(|renew| {
            LeaseHeartbeat::start(id.clone(), self.runtime.reservation_lease_seconds, renew)
        });
        Ok(Some(DispatchReservation {
            id,
            heartbeat,
            store: self.runtime.store.clone(),
            prepared: false,
        }))
    }

    pub(crate) fn prepare_call(
        &mut self,
        kind: NodeKind,
        payload: Value,
        options: CallOptions,
    ) -> Result<CallPreparation> {
        self.prepare_call_metered(kind, payload, options, None)
    }
    fn prepare_call_metered(
        &mut self,
        kind: NodeKind,
        payload: Value,
        options: CallOptions,
        meter_payload: Option<Value>,
    ) -> Result<CallPreparation> {
        let meter_payload = meter_payload.unwrap_or_else(|| payload.clone());
        let candidate = Node::make(
            kind,
            Some(&self.cursor_id),
            options.attempt,
            payload.clone(),
            None,
            json!({}),
        )?;
        let exists = self.runtime.store.borrow().try_exists(&candidate.id)?;
        if exists {
            let node = self.runtime.verified(&candidate.id)?;
            let incomplete = node.result_text.is_none()
                || node
                    .meta
                    .get("state")
                    .and_then(Value::as_str)
                    .is_some_and(|s| s != "completed");
            if self.runtime.mode == ReplayMode::Record {
                if self.runtime.refuse_duplicate_recordings || incomplete {
                    return Err(Error::DuplicateRecording(candidate.id));
                }
            } else {
                if incomplete {
                    return Err(if self.runtime.mode == ReplayMode::Replay {
                        Error::MissingRecording(node.id)
                    } else {
                        Error::DuplicateRecording(node.id)
                    });
                }
                let mut avoided = MeterCharges::new();
                if self.runtime.core_meters {
                    avoided.insert("steps".into(), 1.0);
                    if let Some(tokens) = node.result.as_ref().and_then(usage_tokens) {
                        if tokens != 0 {
                            avoided.insert("tokens".into(), tokens as f64);
                        }
                    }
                }
                let mut replay_meta = node.meta.clone();
                for meter in &self.runtime.meters {
                    let amount = meter.charge_with_meta(
                        kind,
                        &payload,
                        node.result.as_ref().expect("replayable result"),
                        &mut replay_meta,
                    )?;
                    validate_amount(amount, meter.name())?;
                    if amount != 0.0 {
                        avoided.insert(meter.name().into(), amount);
                    } else {
                        avoided.remove(meter.name());
                    }
                }
                self.avoided.add(charges_from_meta(
                    &json!({"charges":amounts_json(&avoided)}),
                )?)?;
                add_amounts(&mut self.avoided_meters, &avoided)?;
                if self.runtime.mode == ReplayMode::Hybrid {
                    let mut recorded_avoided = node
                        .meta
                        .get("avoided")
                        .cloned()
                        .unwrap_or_else(|| json!({}));
                    let mut total = meter_charges_from_meta(&json!({"charges":recorded_avoided}))?;
                    add_amounts(&mut total, &avoided)?;
                    recorded_avoided = amounts_json(&total);
                    self.runtime
                        .update_meta(&node.id, json!({"avoided":recorded_avoided}))?;
                }
                self.cursor_id = node.id.clone();
                return Ok(CallPreparation::Recorded(node));
            }
        }
        if self.runtime.mode == ReplayMode::Replay {
            return Err(Error::MissingRecording(candidate.id));
        }
        let estimates = self.precheck(kind, &meter_payload, options)?;
        let mut reservation = self.reserve_dispatch(kind, &meter_payload, &estimates)?;
        let mut pending = Node::make(
            kind,
            Some(&self.cursor_id),
            options.attempt,
            payload,
            None,
            json!({"state":"pending","charges":amounts_json(&estimates),
                "accounting_unknown":true,"created_at_unix":unix_seconds()}),
        )?;
        let mut window_keys = serde_json::Map::new();
        for meter in &self.runtime.meters {
            if let Some(key) = meter.window_ledger_key(&self.root_id)? {
                window_keys.insert(meter.name().into(), json!(key));
            }
        }
        if !window_keys.is_empty() {
            pending.meta["window_keys"] = Value::Object(window_keys);
        }
        if let Some(reservation) = &reservation {
            pending.meta["reservation_id"] = json!(reservation.id);
        }
        let started = Instant::now();
        // Establish every context before staging or invoking a callback. A
        // failed setup releases the reservation and leaves the cursor intact.
        let measurements =
            Measurements::start(&self.runtime.meters, self.runtime.cleanup_errors.clone())?;
        if !exists {
            // The unprepared reservation guard releases once on staging error.
            self.runtime.write_node(pending.clone(), false)?;
            self.cursor_id = pending.id.clone();
        }
        if let Some(reservation) = &mut reservation {
            reservation.prepared = true;
        }
        Ok(CallPreparation::Dispatch(PendingDispatch {
            pending,
            estimates,
            duplicate: exists,
            started,
            measurements,
            reservation,
            meter_payload,
            store: self.runtime.store.clone(),
            armed: true,
            token_estimate_override: options.estimated_tokens.is_some(),
        }))
    }

    pub(crate) fn finish_call(
        &mut self,
        dispatch: PendingDispatch,
        outcome: Result<Value>,
    ) -> Result<Node> {
        let completed_or_unknown = match &outcome {
            Ok(result) => result.is_object(),
            Err(error) => error.is_post_dispatch_outcome_unknown(),
        };
        // Every failure after a usable provider result must preserve retry
        // classification, including serialization, settlement and store I/O.
        self.finish_call_inner(dispatch, outcome).map_err(|error| {
            if completed_or_unknown && !error.is_post_dispatch_outcome_unknown() {
                Error::OutcomeUnknown(Box::new(error))
            } else {
                error
            }
        })
    }

    fn finish_call_inner(
        &mut self,
        mut dispatch: PendingDispatch,
        outcome: Result<Value>,
    ) -> Result<Node> {
        // Python's duration includes context setup, but excludes context exit.
        let duration = dispatch.started.elapsed().as_secs_f64();
        let outcome = outcome.and_then(|result| {
            if result.is_object() {
                Ok(result)
            } else {
                Err(Error::Handler("callback must return a JSON object".into()))
            }
        });
        let cleanup_error = dispatch.measurements.stop(outcome.as_ref().err());
        let lease_lost = dispatch
            .reservation
            .as_mut()
            .and_then(|reservation| reservation.heartbeat.as_mut())
            .and_then(LeaseHeartbeat::stop);
        let result = match outcome {
            Ok(result) => result,
            Err(Error::OutcomeUnknown(error)) => {
                self.fail_pending(&dispatch, "outcome_unknown")?;
                dispatch.armed = false;
                return Err(Error::OutcomeUnknown(error));
            }
            Err(error) => {
                self.discard_pending(&dispatch);
                dispatch.armed = false;
                return Err(error);
            }
        };
        if let Some(error) = cleanup_error {
            self.fail_pending(&dispatch, "measurement_cleanup_error")?;
            dispatch.armed = false;
            return Err(Error::OutcomeUnknown(Box::new(error)));
        }
        let mut meta = json!({"state":"completed","duration_s":duration,
            "created_at_unix":unix_seconds()});
        match dispatch.measurements.readings() {
            Ok(readings) => {
                meta.as_object_mut()
                    .expect("runtime metadata")
                    .extend(readings);
            }
            Err(error) => {
                self.fail_pending(&dispatch, "measurement_readings_error")?;
                dispatch.armed = false;
                return Err(Error::OutcomeUnknown(Box::new(error)));
            }
        }
        let mut charges = MeterCharges::new();
        if self.runtime.core_meters {
            charges.insert("steps".into(), 1.0);
        }
        let actual = usage_tokens(&result);
        if self.runtime.core_meters {
            if let Some(tokens) = actual {
                charges.insert("tokens".into(), tokens as f64);
            } else if let Some(estimate) = dispatch.estimates.get("tokens") {
                charges.insert("tokens".into(), *estimate);
                meta["accounting_fallbacks"] = json!({"tokens":{
                "reason":"missing_or_invalid_provider_usage","source":"precheck_estimate"}});
            }
        }
        if let Some(usage) = result.get("usage").filter(|v| v.is_object()) {
            meta["usage"] = usage.clone();
        }
        for meter in self.runtime.meters.clone() {
            let charged = meter.charge_with_meta(
                dispatch.pending.kind,
                &dispatch.meter_payload,
                &result,
                &mut meta,
            );
            let mut amount = match charged.and_then(|amount| {
                validate_amount(amount, meter.name())?;
                Ok(amount)
            }) {
                Ok(amount) => amount,
                Err(error) => {
                    self.fail_pending(&dispatch, "meter_error")?;
                    dispatch.armed = false;
                    return Err(error);
                }
            };
            if amount == 0.0
                && (meter.precheck_is_estimate()
                    || (meter.name() == "tokens" && dispatch.token_estimate_override))
            {
                let reason = meter
                    .precheck_fallback_reason(
                        dispatch.pending.kind,
                        &dispatch.meter_payload,
                        &result,
                        &meta,
                    )
                    .or_else(|| {
                        actual
                            .is_none()
                            .then(|| "missing_or_invalid_provider_usage".into())
                    });
                if let (Some(estimate), Some(reason)) =
                    (dispatch.estimates.get(meter.name()), reason)
                {
                    amount = *estimate;
                    if !meta["accounting_fallbacks"].is_object() {
                        meta["accounting_fallbacks"] = json!({});
                    }
                    meta["accounting_fallbacks"][meter.name()] = json!({
                        "reason":reason,"source":"precheck_estimate"});
                }
            }
            charges.insert(meter.name().into(), amount);
        }
        meta["charges"] = amounts_json(&charges);
        if let Some(detail) = &lease_lost {
            meta["reservation_lease"] = json!({"status":"lost","detail":detail});
        }
        let window_charges: MeterCharges = self
            .runtime
            .meters
            .iter()
            .filter(|meter| meter.window().is_some())
            .filter_map(|meter| {
                charges
                    .get(meter.name())
                    .map(|amount| (meter.name().into(), *amount))
            })
            .collect();
        let window_event = json!({"time":unix_seconds(),"charges":amounts_json(&window_charges),"keys":dispatch.pending.meta.get("window_keys")});
        if !window_charges.is_empty() {
            meta["window_events"] = json!([window_event]);
        }
        let node = Node::make(
            dispatch.pending.kind,
            dispatch.pending.parent.as_deref(),
            dispatch.pending.attempt,
            dispatch.pending.payload.clone(),
            Some(result),
            meta,
        )?;
        if let Some(reservation) = &dispatch.reservation {
            self.runtime
                .store
                .0
                .inner
                .borrow_mut()
                .settle_budget(&reservation.id, &decimal_amounts(&charges)?)?;
        }
        if dispatch.duplicate {
            self.runtime.write_node(node.clone(), false)?;
            if !window_charges.is_empty() {
                let stored = self.runtime.store.borrow().get(&node.id)?;
                let mut events = stored
                    .meta
                    .get("window_events")
                    .and_then(Value::as_array)
                    .cloned()
                    .unwrap_or_default();
                if events.is_empty() {
                    events.push(json!({"time":stored.meta.get("created_at_unix"),"charges":stored.meta.get("charges"),"keys":stored.meta.get("window_keys")}));
                }
                events.push(window_event);
                self.runtime
                    .update_meta(&node.id, json!({"window_events":events}))?;
            }
        } else {
            self.runtime.write_node(node.clone(), true)?;
            self.runtime.notify_node(&node);
        }
        let stored = self.runtime.store.borrow().get(&node.id)?;
        self.cursor_id = stored.id.clone();
        dispatch.armed = false;
        if lease_lost.is_some() {
            return Err(Error::Integrity(format!(
                "reservation lease lost while call was running: {}",
                stored.id
            )));
        }
        Ok(stored)
    }

    fn discard_pending(&mut self, dispatch: &PendingDispatch) {
        if let Some(reservation) = &dispatch.reservation {
            let _ = self
                .runtime
                .store
                .0
                .inner
                .borrow_mut()
                .release_budget(&reservation.id);
        }
        if dispatch.duplicate {
            return;
        }
        let ids = BTreeSet::from([dispatch.pending.id.clone()]);
        let removed = self.runtime.store.0.inner.borrow_mut().drop_nodes(&ids);
        if removed.is_ok() {
            self.runtime.store.synchronize_index();
            if let Some(parent) = &dispatch.pending.parent {
                self.cursor_id = parent.clone();
            }
        } else {
            // Backends without removal support retain a durable conservative record.
            let _ = self.fail_pending(dispatch, "handler_error_cleanup_failed");
        }
    }

    fn fail_pending(&self, dispatch: &PendingDispatch, reason: &str) -> Result<()> {
        if let Some(reservation) = &dispatch.reservation {
            self.runtime
                .store
                .0
                .inner
                .borrow_mut()
                .settle_budget(&reservation.id, &decimal_amounts(&dispatch.estimates)?)?;
        }
        if dispatch.duplicate {
            let stored = self.runtime.store.borrow().get(&dispatch.pending.id)?;
            let mut events = stored
                .meta
                .get("window_events")
                .and_then(Value::as_array)
                .cloned()
                .unwrap_or_default();
            if events.is_empty() {
                events.push(json!({"time":stored.meta.get("created_at_unix"),"charges":stored.meta.get("charges"),"keys":stored.meta.get("window_keys")}));
            }
            events.push(json!({"time":unix_seconds(),"charges":amounts_json(&dispatch.estimates),"keys":dispatch.pending.meta.get("window_keys")}));
            self.runtime
                .update_meta(&stored.id, json!({"window_events":events}))?;
            return Ok(());
        }
        let pending = &dispatch.pending;
        let failed = Node::make(
            pending.kind,
            pending.parent.as_deref(),
            pending.attempt,
            pending.payload.clone(),
            None,
            json!({"state":"failed",
                "charges":amounts_json(&dispatch.estimates),"accounting_unknown":true,"error":reason,"created_at_unix":unix_seconds(),"window_keys":pending.meta.get("window_keys")}),
        )?;
        self.runtime.write_node(failed.clone(), true)?;
        self.runtime.notify_node(&failed);
        Ok(())
    }
}

enum PreparedHandler {
    Sync(crate::Handler),
    Async(crate::AsyncHandler),
}
// Dispatch is held briefly on the stack; avoid a heap allocation on every call.
#[allow(clippy::large_enum_variant)]
enum RegisteredPreparation {
    Ready(Node),
    Dispatch {
        dispatch: PendingDispatch,
        args: Value,
        handler: PreparedHandler,
    },
}
struct PreparedRevalidation {
    recorded: Node,
    recorded_contract: Option<Value>,
    live_contract: Value,
    live_payload: Value,
    observation_payload: Value,
    options: RevalidationOptions,
}

// Keep the staged callback path allocation-free beyond the owned JSON values.
#[allow(clippy::large_enum_variant)]
pub(crate) enum CallPreparation {
    Recorded(Node),
    Dispatch(PendingDispatch),
}

struct Measurements {
    contexts: Vec<Box<dyn crate::MeterMeasurement>>,
    stopped: bool,
    errors: Rc<RefCell<Vec<String>>>,
}
impl Measurements {
    fn start(meters: &[Rc<dyn crate::Meter>], errors: Rc<RefCell<Vec<String>>>) -> Result<Self> {
        let mut active = Self {
            contexts: Vec::new(),
            stopped: false,
            errors,
        };
        for meter in meters {
            let setup = meter.measure().and_then(|context| {
                if let Some(mut context) = context {
                    context.start()?;
                    active.contexts.push(context);
                }
                Ok(())
            });
            if let Err(error) = setup {
                active.stop(Some(&error));
                return Err(error);
            }
        }
        Ok(active)
    }
    fn stop(&mut self, error: Option<&Error>) -> Option<Error> {
        if self.stopped {
            return None;
        }
        self.stopped = true;
        let mut first_error = None;
        for context in self.contexts.iter_mut().rev() {
            let result =
                std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| context.finish(error)))
                    .unwrap_or_else(|_| Err(Error::Handler("measurement cleanup panicked".into())));
            if let Err(error) = result {
                self.errors.borrow_mut().push(error.to_string());
                if first_error.is_none() {
                    first_error = Some(error);
                }
            }
        }
        first_error
    }
    fn readings(&self) -> Result<serde_json::Map<String, Value>> {
        let mut readings = serde_json::Map::new();
        for context in &self.contexts {
            let value = context.readings()?;
            let object = value.as_object().ok_or_else(|| {
                Error::Invalid("measurement readings must be a JSON object".into())
            })?;
            readings.extend(object.clone());
        }
        Ok(readings)
    }
}
impl Drop for Measurements {
    fn drop(&mut self) {
        self.stop(Some(&Error::OutcomeUnknown(Box::new(Error::Handler(
            "measurement scope cancelled or unwound".into(),
        )))));
    }
}

pub(crate) struct PendingDispatch {
    pending: Node,
    meter_payload: Value,
    estimates: MeterCharges,
    duplicate: bool,
    started: Instant,
    measurements: Measurements,
    reservation: Option<DispatchReservation>,
    store: SharedStore,
    armed: bool,
    token_estimate_override: bool,
}
impl Drop for PendingDispatch {
    fn drop(&mut self) {
        if !self.armed {
            return;
        }
        self.measurements
            .stop(Some(&Error::OutcomeUnknown(Box::new(Error::Handler(
                "call cancelled or unwound".into(),
            )))));
        if let Some(heartbeat) = self
            .reservation
            .as_mut()
            .and_then(|reservation| reservation.heartbeat.as_mut())
        {
            heartbeat.stop();
        }
        let Ok(mut store) = self.store.0.inner.try_borrow_mut() else {
            return;
        };
        if let Some(reservation) = &self.reservation {
            if let Ok(charges) = decimal_amounts(&self.estimates) {
                let _ = store.settle_budget(&reservation.id, &charges);
            }
        }
        if self.duplicate {
            if self.pending.meta.get("window_keys").is_some() {
                if let Ok(stored) = store.get(&self.pending.id) {
                    let mut events = stored
                        .meta
                        .get("window_events")
                        .and_then(Value::as_array)
                        .cloned()
                        .unwrap_or_default();
                    if events.is_empty() {
                        events.push(json!({"time":stored.meta.get("created_at_unix"),"charges":stored.meta.get("charges"),"keys":stored.meta.get("window_keys")}));
                    }
                    events.push(json!({"time":unix_seconds(),"charges":amounts_json(&self.estimates),"keys":self.pending.meta.get("window_keys")}));
                    let _ = store.update_meta(&stored.id, json!({"window_events":events}));
                }
            }
            return;
        }
        if let Ok(failed) = Node::make(
            self.pending.kind,
            self.pending.parent.as_deref(),
            self.pending.attempt,
            self.pending.payload.clone(),
            None,
            json!({"state":"failed","charges":amounts_json(&self.estimates),
                "accounting_unknown":true,"error":"outcome_unknown","created_at_unix":unix_seconds(),"window_keys":self.pending.meta.get("window_keys")}),
        ) {
            let _ = store.finalize(failed);
        }
    }
}
struct DispatchReservation {
    id: String,
    heartbeat: Option<LeaseHeartbeat>,
    store: SharedStore,
    prepared: bool,
}
impl Drop for DispatchReservation {
    fn drop(&mut self) {
        if !self.prepared {
            if let Ok(mut store) = self.store.0.inner.try_borrow_mut() {
                let _ = store.release_budget(&self.id);
            }
        }
    }
}
struct LeaseHeartbeat {
    shutdown: Option<std::sync::mpsc::Sender<()>>,
    lost: std::sync::Arc<std::sync::Mutex<Option<String>>>,
    finished: std::sync::mpsc::Receiver<()>,
}
impl LeaseHeartbeat {
    fn start(id: String, seconds: f64, renew: crate::store::LeaseRenewer) -> Self {
        let (send, receive) = std::sync::mpsc::channel();
        let (complete, finished) = std::sync::mpsc::channel();
        let lost = std::sync::Arc::new(std::sync::Mutex::new(None));
        let failures = lost.clone();
        let interval = std::time::Duration::from_secs_f64((seconds / 3.0).clamp(0.001, 20.0));
        std::thread::spawn(move || {
            loop {
                match receive.recv_timeout(interval) {
                    Ok(()) | Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
                    Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
                        let detail =
                            match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                                renew(&id, seconds)
                            })) {
                                Ok(Ok(true)) => continue,
                                Ok(Ok(false)) => "reservation expired or disappeared".to_owned(),
                                Ok(Err(error)) => error.to_string(),
                                Err(_) => "reservation renewal panicked".to_owned(),
                            };
                        if let Ok(mut failure) = failures.lock() {
                            *failure = Some(detail);
                        }
                        break;
                    }
                }
            }
            let _ = complete.send(());
        });
        Self {
            shutdown: Some(send),
            lost,
            finished,
        }
    }
    fn stop(&mut self) -> Option<String> {
        if let Some(send) = self.shutdown.take() {
            let _ = send.send(());
            if matches!(
                self.finished
                    .recv_timeout(std::time::Duration::from_millis(100)),
                Err(std::sync::mpsc::RecvTimeoutError::Timeout)
            ) {
                if let Ok(mut lost) = self.lost.lock() {
                    *lost = Some("reservation renewal did not stop promptly".into());
                }
            }
        }
        self.lost.lock().ok().and_then(|lost| lost.clone())
    }
}
impl Drop for LeaseHeartbeat {
    fn drop(&mut self) {
        self.stop();
    }
}
fn decimal_add(left: f64, right: f64) -> Result<f64> {
    crate::decimal::exact_add(decimal_amount(left)?, decimal_amount(right)?)
        .and_then(|amount| amount.to_f64())
        .ok_or_else(|| Error::Integrity("meter total overflow".into()))
}
fn decimal_subtract(left: f64, right: f64) -> Result<f64> {
    crate::decimal::exact_subtract(decimal_amount(left)?, decimal_amount(right)?)
        .and_then(|amount| amount.to_f64())
        .ok_or_else(|| Error::Integrity("meter total overflow".into()))
}
fn exceeds_limit(limit: f64, already: f64, requested: f64) -> Result<bool> {
    let remaining =
        crate::decimal::exact_subtract(decimal_amount(limit)?, decimal_amount(already)?)
            .ok_or_else(|| Error::Integrity("budget amount overflow".into()))?;
    Ok(decimal_amount(requested)? > remaining)
}
fn decimal_amount(amount: f64) -> Result<Decimal> {
    crate::parse_decimal_exact(&amount.to_string())
        .map_err(|error| Error::Invalid(format!("meter amount out of decimal range: {error}")))
}
fn decimal_amounts(amounts: &MeterCharges) -> Result<BTreeMap<String, Decimal>> {
    amounts
        .iter()
        .map(|(name, amount)| Ok((name.clone(), decimal_amount(*amount)?)))
        .collect()
}

fn validate_amount(amount: f64, name: &str) -> Result<()> {
    if !amount.is_finite() || amount < 0.0 {
        return Err(Error::Invalid(format!(
            "{name} amount must be finite and nonnegative"
        )));
    }
    decimal_amount(amount)?;
    Ok(())
}
fn validate_amounts(amounts: &MeterCharges) -> Result<()> {
    for (name, amount) in amounts {
        if name.trim().is_empty() {
            return Err(Error::Invalid("meter name cannot be empty".into()));
        }
        validate_amount(*amount, name)?;
    }
    Ok(())
}
fn unix_seconds() -> f64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs_f64()
}
fn amounts_json(amounts: &MeterCharges) -> Value {
    let mut object = serde_json::Map::new();
    for (name, amount) in amounts {
        if *amount == 0.0 {
            continue;
        }
        let value = if amount.fract() == 0.0 && *amount <= MAX_SAFE_INTEGER as f64 {
            json!(*amount as u64)
        } else {
            json!(*amount)
        };
        object.insert(name.clone(), value);
    }
    Value::Object(object)
}
fn add_amounts(total: &mut MeterCharges, charges: &MeterCharges) -> Result<()> {
    for (name, amount) in charges {
        let next = decimal_add(total.get(name).copied().unwrap_or(0.0), *amount)?;
        validate_amount(next, name)?;
        if next != 0.0 {
            total.insert(name.clone(), next);
        }
    }
    Ok(())
}
fn meter_charges_from_meta(meta: &Value) -> Result<MeterCharges> {
    let Some(charges) = meta.get("charges") else {
        return Ok(MeterCharges::new());
    };
    let charges = charges
        .as_object()
        .ok_or_else(|| Error::Integrity("charges must be object".into()))?;
    let mut result = MeterCharges::new();
    for (name, amount) in charges {
        crate::parse_decimal_exact(&amount.to_string()).map_err(|error| {
            Error::Integrity(format!("{name} charge out of decimal range: {error}"))
        })?;
        let amount = amount
            .as_f64()
            .ok_or_else(|| Error::Integrity(format!("{name} charge must be numeric")))?;
        validate_amount(amount, name).map_err(|error| Error::Integrity(error.to_string()))?;
        if amount != 0.0 {
            result.insert(name.clone(), amount);
        }
    }
    Ok(result)
}

fn charges_from_meta(meta: &Value) -> Result<Charges> {
    let Some(charges) = meta.get("charges") else {
        return Ok(Charges::default());
    };
    if !charges.is_object() {
        return Err(Error::Integrity("charges must be object".into()));
    }
    let amount = |key: &str| -> Result<u64> {
        let Some(value) = charges.get(key) else {
            return Ok(0);
        };
        crate::parse_decimal_exact(&value.to_string()).map_err(|error| {
            Error::Integrity(format!("{key} charge out of decimal range: {error}"))
        })?;
        let amount = value
            .as_f64()
            .ok_or_else(|| Error::Integrity(format!("{key} charge must be nonnegative integer")))?;
        if !amount.is_finite()
            || amount < 0.0
            || amount.fract() != 0.0
            || amount > MAX_SAFE_INTEGER as f64
        {
            return Err(Error::Integrity(format!(
                "{key} charge must be nonnegative safe integer"
            )));
        }
        let n = amount as u64;
        safe_amount(n, key).map_err(|e| Error::Integrity(e.to_string()))?;
        Ok(n)
    };
    Ok(Charges {
        steps: amount("steps")?,
        tokens: amount("tokens")?,
    })
}
fn usage_tokens(result: &Value) -> Option<u64> {
    let usage = result.get("usage")?.as_object()?;
    let input = usage.get("input_tokens")?.as_u64()?;
    let output = usage.get("output_tokens")?.as_u64()?;
    let total = input.checked_add(output)?;
    if total > MAX_SAFE_INTEGER {
        None
    } else {
        Some(total)
    }
}