rungbot-exec 0.5.7

Live order execution for rungbot: an idempotent order journal, hard caps, reconcile, and Gate.io, Revolut X and Binance spot clients. Opt-in.
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
//! The monthly-capital deploy layer: fresh stable on a venue becomes resting limit-buy
//! zones below spot ("let price come to us") instead of a market buy the day it lands.
//!
//! Detection runs inside every live `rungbot-exec run` ([`Layer::check`]):
//!
//! ```text
//! total(venue)  = free stable + stable locked in resting orders (venue-reported)
//! expected      = last baseline + journal-explained fills since then
//! new capital   = total - expected   >= deploy_min_usd  -> a tranche deploys
//! ```
//!
//! A cold start records the baseline without deploying. Withdrawals and noise re-baseline
//! silently; positive dust is carried until it sums to a tranche. The baseline is saved
//! **before** anything is journaled or placed, so a crash can only under-deploy.
//!
//! A tranche cancels the venue's unfilled zones and folds their unspent budget in; the
//! price levels still resting are held ([`rungbot_core::deploy::plan_levels`]). Around it:
//!
//! * the Revolut X onramp: EUR → USDC → USD, keeping the pending transfer to Gate in USDC
//!   and ring-fencing it from the ladder, with an in-flight guard while it travels; when
//!   Gate's balances cannot be read the reserve is unknown and the layer fails closed
//!   (staged USDC stays USDC, no free Revolut X cash is laddered that run);
//! * Gate's USDC (the transfer's bridge asset) auto-converted to USDT;
//! * the idle sweep: cash a venue freed by cancelling a zone is re-laddered;
//! * the withhold: the onramp venue re-laddered lighter so the transfer can leave;
//! * the bull sweep: a zone resting too long in a young bull is bought at market (a
//!   cancel whose final status is unreadable adds nothing, and the buy never exceeds
//!   the venue's free cash minus the reserve);
//! * resume: journaled rungs that never reached the venue are placed (or adopted when
//!   they did), a rung short of free balance by pennies is trimmed once.
//!
//! State lives in the deploy state file (JSON, tmp-and-rename); rows in the order
//! journal carry `kind: deploy_buy`. The manual commands are in [`cli`].

pub mod audit;
pub mod churn;
pub mod cli;
pub mod fillodds;

use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;

use rungbot_core::deploy::{self as plan, Pinned, PlanKnobs, ZoneOverride};
use rungbot_core::watch::pyfmt::{round as py_round, sum as py_sum};
use serde_json::{json, Map, Value};

use crate::housekeeping::Persist;
use crate::journal::{Journal, Order};
use crate::pyfmt::{fixed, g};
use crate::reconcile::{price_eq, settle_cancel, Settled, VenueSource};
use crate::run::config::RunConfig;
use crate::run::funding;
use crate::run::market::Market;
use crate::run::RunResult;
use crate::venue::{venue_id_matches, Balance, Venue};

/// Placement retries before a rung is given up on.
pub const MAX_RETRIES: i64 = 5;
/// Manual-command venues, in the order the layer walks them.
pub const VENUES: [&str; 3] = ["binance", "gate", "revx"];
/// A sweep or withhold that could not ladder anything waits this long.
pub const SWEEP_RETRY_S: f64 = 86_400.0;
/// Fee headroom when staging USDC (the taker fee is charged on top of the quote size).
pub const ONRAMP_HEADROOM_USD: f64 = 2.0;
/// A top-up that never shows up on Gate stops being "in flight" after this.
pub const INFLIGHT_MAX_S: f64 = 48.0 * 3600.0;
/// Venue credit time against the run's clock.
pub const INFLIGHT_SLACK_S: f64 = 600.0;
/// Our cancel notes (see [`crate::journal::OUR_CANCEL_NOTES`]).
pub const ROLL_NOTE: &str = "rolled into new tranche";
pub const CANCEL_NOTE: &str = "manual --cancel";
pub const BULL_NOTE: &str = "bull sweep -> market";

/// Where the layer reads the market regime and candles.
pub trait RegimeFeed {
    /// `regime-state.json`'s shape: `{market, coins: {SYM: {running, ...}}}`.
    fn regime(&self) -> Result<Value, String>;
    /// `regime-history.json`'s shape: `{labels: [...], ...}`.
    fn label_history(&self) -> Result<Value, String>;
    /// Daily closes for a venue pair (Revolut X pairs read their proxy venue).
    fn closes(&self, exch: &str, pair: &str, n: usize) -> Result<Vec<f64>, String>;
}

/// The live feed: the run's cached regime files and the public candles.
pub struct LiveRegime<'a> {
    pub cfg: &'a RunConfig,
    pub market: &'a dyn Market,
    pub now: f64,
}

impl RegimeFeed for LiveRegime<'_> {
    fn regime(&self) -> Result<Value, String> {
        Ok(crate::run::regime::get_regime(
            self.cfg,
            self.market,
            self.now,
        ))
    }
    fn label_history(&self) -> Result<Value, String> {
        Ok(crate::run::regime::label_history(
            self.cfg,
            self.market,
            self.now,
        ))
    }
    fn closes(&self, exch: &str, pair: &str, n: usize) -> Result<Vec<f64>, String> {
        crate::run::regime::closes_for_route(self.cfg, self.market, exch, pair, n)
    }
}

/// One planned rung, rounded to the venue.
#[derive(Debug, Clone, PartialEq)]
pub struct Rung {
    /// 1-based position in the profile.
    pub rung: i64,
    pub price: f64,
    pub qty: f64,
    pub usd: f64,
    pub note: String,
    pub spot: f64,
    pub held: bool,
    pub since: Option<f64>,
}

/// A venue's zone plan: coins in allocation order, skip reasons, and whether the
/// allocation named none of the venue's coins (equal split).
#[derive(Debug, Clone, Default, PartialEq)]
pub struct VenuePlan {
    pub plans: Vec<(String, Vec<Rung>)>,
    pub skips: Vec<String>,
    pub fallback: bool,
}

/// A coin set that restricts a re-ladder; empty means every coin.
pub type Only = Option<BTreeSet<String>>;

fn in_only(only: &Only, sym: &str) -> bool {
    only.as_ref()
        .is_none_or(|o| o.is_empty() || o.contains(sym))
}

/// `float(x or 0)` for a JSON value.
pub(crate) fn num(v: Option<&Value>) -> f64 {
    v.and_then(Value::as_f64).unwrap_or(0.0)
}

fn truthy_f(x: Option<f64>) -> bool {
    x.is_some_and(|v| v != 0.0)
}

fn bal(m: &BTreeMap<String, Balance>, cur: &str) -> Balance {
    m.get(cur).copied().unwrap_or_default()
}

/// Read the deploy state. A missing file is a fresh start; one that does not parse
/// stops the layer (a lost baseline would re-deploy everything).
pub fn load_state(path: &Path) -> Result<Map<String, Value>, String> {
    crate::store::read_state(path)
}

/// Write the deploy state (tmp and rename).
pub fn save_state(path: &Path, st: &Map<String, Value>) -> Result<(), String> {
    crate::store::write_atomic(path, &crate::pyfmt::dumps(st, Some(2)))
}

/// Is `err` a venue saying the rung costs a few cents more than the free balance?
pub fn short_by_pennies(err: &str) -> bool {
    let t = err.to_lowercase();
    if t.contains("balance_not_enough") || t.contains("not enough balance") {
        return true;
    }
    t.contains("insufficient") && (t.contains("balance") || t.contains("funds"))
}

/// The layer, bound to one config, venue set, journal and clock.
pub struct Layer<'a> {
    pub cfg: &'a RunConfig,
    pub venues: &'a dyn VenueSource,
    pub regime: &'a dyn RegimeFeed,
    pub clock: &'a dyn Fn() -> f64,
    pub sleep: &'a dyn Fn(f64),
    /// Writes the journal through, the run's own handle: a row is saved before the venue
    /// call that may place it, and again with the venue's answer. Once a write fails the
    /// layer makes no further venue call that places or cancels.
    pub persist: &'a mut Persist,
    /// Where warnings the reference printed on stderr go.
    pub stderr: &'a dyn Fn(&str),
    pub j: &'a mut Journal,
    /// This call's results, in order.
    pub results: Vec<RunResult>,
}

enum Lvl {
    Done,
    Warn,
    Err,
}

impl<'a> Layer<'a> {
    fn push(&mut self, sym: Option<&str>, side: Option<&str>, committed: bool, l: Lvl, t: String) {
        let mut r = RunResult {
            sym: sym.map(str::to_string),
            side: side.map(str::to_string),
            mode: Some("live".into()),
            hk: true,
            deploy: true,
            committed,
            ..Default::default()
        };
        match l {
            Lvl::Done => r.done = Some(t),
            Lvl::Warn => r.warn = Some(t),
            Lvl::Err => r.err = Some(t),
        }
        self.results.push(r);
    }
    fn done(&mut self, t: String) {
        self.push(None, None, false, Lvl::Done, t)
    }
    fn warn(&mut self, t: String) {
        self.push(None, None, false, Lvl::Warn, t)
    }

    /// Write the journal before a venue call: `Err` means make no call.
    fn save(&mut self) -> Result<(), String> {
        self.persist.journal(self.j)
    }

    /// Write the journal after a venue call. The order may be at the venue now, so a
    /// failure does not abort the step: it sticks in the handle, every later [`Self::save`]
    /// fails, and nothing more is placed or cancelled.
    fn save_after(&mut self) {
        let _ = self.persist.journal(self.j);
    }

    /// The journal write that failed, when one did: no venue call may place or cancel.
    fn write_failed(&self) -> Option<String> {
        self.persist.failed().map(str::to_string)
    }

    /// Write the deploy state after venue calls: a failure is reported, not raised.
    fn save_state_after(&mut self, path: &Path, st: &Map<String, Value>) {
        if let Err(e) = save_state(path, st) {
            self.push(
                None,
                None,
                false,
                Lvl::Err,
                format!("deploy state write failed: {e}"),
            );
        }
    }

    fn now(&self) -> f64 {
        (self.clock)()
    }

    /// The client for `exch`, or the error the reference's `clients[exch]` raised.
    fn client(&self, exch: &str) -> Result<&'a dyn Venue, String> {
        let v: &'a dyn VenueSource = self.venues;
        v.venue(exch).map_err(|_| format!("'{exch}'"))
    }

    fn alloc_weight(&self, sym: &str) -> f64 {
        self.cfg
            .deploy_alloc
            .iter()
            .find(|(s, _)| s.to_uppercase() == sym)
            .map_or(0.0, |(_, w)| *w)
    }

    fn overrides(&self) -> BTreeMap<String, ZoneOverride> {
        self.cfg
            .deploy_zones
            .iter()
            .map(|(k, z)| {
                (
                    k.to_uppercase(),
                    ZoneOverride {
                        depths: z.depths.clone(),
                        weights: z.weights.clone(),
                    },
                )
            })
            .collect()
    }

    /// Quote asset per venue: the routed one (the last route wins), Revolut X USD and
    /// Binance USDC when nothing routes there.
    pub fn quotes(cfg: &RunConfig) -> BTreeMap<String, String> {
        let mut q = BTreeMap::new();
        for (_, r) in &cfg.routing {
            q.insert(r.exch.clone(), r.quote.clone());
        }
        q.entry("revx".into()).or_insert_with(|| "USD".into());
        q.entry("binance".into()).or_insert_with(|| "USDC".into());
        q
    }

    /// A coin's pair on a venue: its route there, else its Revolut X listing.
    pub fn pair_for(cfg: &RunConfig, exch: &str, sym: &str) -> Result<String, String> {
        let routed = cfg.route(sym);
        if exch == "revx" && routed.map(|r| r.exch.as_str()) != Some("revx") {
            return cfg
                .revx_pair(sym)
                .map(str::to_string)
                .ok_or_else(|| format!("'{sym}'"));
        }
        routed
            .map(|r| r.pair.clone())
            .ok_or_else(|| format!("'{sym}'"))
    }

    /// The coins a venue ladders: those routed there, or the Revolut X listings when
    /// nothing routes to it.
    pub fn venue_syms(cfg: &RunConfig, exch: &str) -> Vec<String> {
        let mut syms: Vec<String> = cfg
            .routing
            .iter()
            .filter(|(_, r)| r.exch == exch)
            .map(|(s, _)| s.clone())
            .collect();
        if exch == "revx" && syms.is_empty() {
            syms = cfg.revx_pairs.iter().map(|(s, _)| s.clone()).collect();
        }
        syms
    }

    /// The regime label, `chop` when it cannot be read.
    pub fn regime_label(&self) -> String {
        match self.regime.regime() {
            Ok(r) => match r.get("market") {
                None => "chop".into(),
                Some(Value::String(s)) => s.clone(),
                Some(v) => crate::pyfmt::value_str(v),
            },
            Err(_) => "chop".into(),
        }
    }

    fn structure(
        &self,
        exch: &str,
        pair: &str,
        cache: &mut BTreeMap<String, (Option<f64>, Option<f64>)>,
    ) -> (Option<f64>, Option<f64>) {
        if let Some(s) = cache.get(pair) {
            return *s;
        }
        let s = match self.regime.closes(exch, pair, 35) {
            Ok(c) => plan::structure(&c),
            Err(_) => (None, None),
        };
        cache.insert(pair.to_string(), s);
        s
    }

    fn venue_mins(client: &dyn Venue, pair: &str) -> Result<(f64, f64), String> {
        let l = client.limits(pair).map_err(|e| e.to_string())?;
        Ok((l.min_base, l.min_quote.max(1.0)))
    }

    fn fit_qty(client: &dyn Venue, pair: &str, budget: f64, price: f64) -> Result<f64, String> {
        let step = client.qty_step(pair).map_err(|e| e.to_string())?;
        plan::fit_qty(budget, price, step, |x| {
            client.round_amount(pair, x).map_err(|e| e.to_string())
        })
    }

    /// One coin's zones: `Ok(Ok(rungs))`, `Ok(Err(skip reason))`, or `Err` for a venue
    /// failure the layer does not catch.
    #[allow(clippy::too_many_arguments)]
    pub fn plan_coin(
        &self,
        client: &dyn Venue,
        exch: &str,
        pair: &str,
        amount: f64,
        label: &str,
        cache: &mut BTreeMap<String, (Option<f64>, Option<f64>)>,
        sym: Option<&str>,
        pinned: Option<&BTreeMap<i64, Pinned>>,
    ) -> Result<Result<Vec<Rung>, String>, String> {
        let amount = plan::floor_cents(amount);
        let got = client
            .price(pair)
            .map_err(|e| e.to_string())
            .and_then(|spot| Ok((spot, Self::venue_mins(client, pair)?)));
        let (spot, (minb, minq)) = match got {
            Ok(x) => x,
            Err(e) => return Ok(Err(format!("venue data: {e}"))),
        };
        if !(spot.is_finite() && spot > 0.0) {
            return Ok(Err(format!(
                "venue data: spot {} is not a positive price",
                crate::pyfmt::float_repr(spot)
            )));
        }
        let (profile, warn) = plan::zone_profile(label, sym, &self.overrides());
        if let Some(w) = warn {
            (self.stderr)(&w);
        }
        let st = self.structure(exch, pair, cache);
        let knobs = PlanKnobs {
            max_depth_pct: self.cfg.deploy_max_depth_pct,
            pin_prices: self.cfg.deploy_pin_prices,
        };
        let levels = plan::plan_levels(spot, amount, &profile, st, minq, knobs, pinned);
        let mut out = Vec::new();
        for (i, r) in levels.iter().enumerate() {
            if r.usd <= 0.0 {
                continue;
            }
            let price = client
                .round_price(pair, r.price)
                .map_err(|e| e.to_string())?;
            let qty = Self::fit_qty(client, pair, r.usd, price)?;
            if qty <= 0.0 || qty < minb || qty * price < minq {
                continue;
            }
            out.push(Rung {
                rung: i as i64 + 1,
                price,
                qty,
                usd: qty * price,
                note: r.note.clone(),
                spot,
                held: r.held,
                since: if r.held { r.since } else { None },
            });
        }
        if out.is_empty() {
            return Ok(Err(format!(
                "${} below venue minimum (~${}/order)",
                fixed(amount, 2),
                fixed(minq, 2)
            )));
        }
        Ok(Ok(out))
    }

    /// Split a venue budget over its coins by allocation weight, renormalised within the
    /// venue; a coin whose share cannot clear the venue minimum drops out and its share
    /// goes to the rest.
    pub fn venue_plan(
        &self,
        client: &dyn Venue,
        exch: &str,
        budget: f64,
        label: &str,
        only: &Only,
        pinned: &BTreeMap<String, BTreeMap<i64, Pinned>>,
    ) -> Result<VenuePlan, String> {
        let syms: Vec<String> = Self::venue_syms(self.cfg, exch)
            .into_iter()
            .filter(|s| in_only(only, s))
            .collect();
        let mut weights: Vec<(String, f64)> = syms
            .iter()
            .filter(|s| self.alloc_weight(s) > 0.0)
            .map(|s| (s.clone(), self.alloc_weight(s)))
            .collect();
        let fallback = weights.is_empty();
        if fallback {
            weights = syms.iter().map(|s| (s.clone(), 1.0)).collect();
        }
        let mut cache = BTreeMap::new();
        let mut skips = Vec::new();
        loop {
            let tw = py_sum(weights.iter().map(|(_, w)| *w));
            let mut plans = Vec::new();
            let mut dropped = Vec::new();
            for (s, wt) in &weights {
                let pair = Self::pair_for(self.cfg, exch, s)?;
                match self.plan_coin(
                    client,
                    exch,
                    &pair,
                    budget * wt / tw,
                    label,
                    &mut cache,
                    Some(s),
                    pinned.get(s),
                )? {
                    Err(skip) => {
                        dropped.push(s.clone());
                        skips.push(format!("{s}: {skip}"));
                    }
                    Ok(r) => plans.push((s.clone(), r)),
                }
            }
            if dropped.is_empty() || plans.is_empty() {
                return Ok(VenuePlan {
                    plans,
                    skips,
                    fallback,
                });
            }
            weights.retain(|(s, _)| !dropped.contains(s));
        }
    }

    /// The price levels still resting on a venue, which a re-ladder holds. Rungs placed
    /// under another regime label are left out: a regime flip is when the ladder moves.
    pub fn resting_levels(
        &self,
        exch: &str,
        only: &Only,
    ) -> BTreeMap<String, BTreeMap<i64, Pinned>> {
        let label = self.regime_label();
        let mut out: BTreeMap<String, BTreeMap<i64, Pinned>> = BTreeMap::new();
        for o in self.j.open_orders(Some("deploy_buy")) {
            if o.exch != exch || !has_id(o) || !truthy_i(o.rung) {
                continue;
            }
            if !in_only(only, &o.sym) {
                continue;
            }
            if o.label
                .as_deref()
                .is_some_and(|l| !l.is_empty() && l != label)
            {
                continue;
            }
            if !truthy_f(o.price) {
                continue;
            }
            let since = if truthy_f(o.first_ts) {
                o.first_ts
            } else {
                o.ts
            };
            out.entry(o.sym.clone()).or_default().insert(
                o.rung.unwrap_or(0),
                Pinned {
                    price: o.price.unwrap_or(0.0),
                    note: o.note.clone(),
                    since,
                },
            );
        }
        out
    }

    /// Quote a cancelled zone never spent.
    fn unspent(o: &Order, settled: &Settled) -> f64 {
        let from_settle = match settled {
            Settled::Booked(row) => row.filled_quote.filter(|x| *x != 0.0),
            _ => None,
        };
        let spent = from_settle
            .or(o.part_quote.filter(|x| *x != 0.0))
            .unwrap_or(0.0);
        (o.quote.unwrap_or(0.0) - spent).max(0.0)
    }

    /// Cancel `o` as one of ours with `note`, and book whatever it filled. The inner
    /// error is the venue refusing the cancel; the outer one a journal write failing.
    fn cancel_ours(
        &mut self,
        client: &dyn Venue,
        o: &Order,
        note: &str,
    ) -> Result<Result<Settled, String>, String> {
        if let Some(e) = self.write_failed() {
            return Ok(Err(format!("not cancelled, {e}")));
        }
        let oid = o.order_id.clone().unwrap_or_default();
        if let Err(e) = client.cancel(&o.pair, &oid) {
            return Ok(Err(e.to_string()));
        }
        self.j.update(&o.client_id, |r| {
            r.status = "canceled".into();
            r.note = Some(note.into());
        });
        self.save_after();
        let now = self.now();
        let s = settle_cancel(self.j, client, &o.client_id, now);
        if !matches!(s, Settled::NothingFilled | Settled::NotPlaced) {
            self.save_after();
        }
        Ok(Ok(s))
    }

    /// Roll the venue's unfilled zones: cancel them and return their unspent budget.
    pub fn cancel_open_zones(
        &mut self,
        client: &dyn Venue,
        exch: &str,
        only: &Only,
    ) -> Result<f64, String> {
        let mut rolled = 0.0;
        let rows: Vec<Order> = self
            .j
            .open_orders(Some("deploy_buy"))
            .into_iter()
            .cloned()
            .collect();
        for o in rows {
            if o.exch != exch || !has_id(&o) || !in_only(only, &o.sym) || !truthy_i(o.rung) {
                continue;
            }
            match self.cancel_ours(client, &o, ROLL_NOTE)? {
                Ok(s) => rolled += Self::unspent(&o, &s),
                Err(e) => self.push(
                    Some(&o.sym),
                    None,
                    false,
                    Lvl::Warn,
                    format!(
                        "deploy rollover: cancel failed (may have just filled, reconciles next run): {e}"
                    ),
                ),
            }
        }
        Ok(rolled)
    }

    /// Journal a plan's rungs (skipping ids already journaled) and return the new ids.
    pub fn journal_and_place(
        &mut self,
        exch: &str,
        plans: &[(String, Vec<Rung>)],
        ts_cut: f64,
        label: Option<&str>,
    ) -> Result<Vec<String>, String> {
        let mut placements = Vec::new();
        let prefix = if exch == "revx" { "depx" } else { "dep" };
        for (s, rungs) in plans {
            let pair = Self::pair_for(self.cfg, exch, s)?;
            for r in rungs {
                let cid = format!("{prefix}{s}{}r{}", ts_cut as i64, r.rung);
                if self.j.exists(&cid) {
                    continue;
                }
                self.j.record(Order {
                    client_id: cid.clone(),
                    sym: s.clone(),
                    exch: exch.into(),
                    pair: pair.clone(),
                    side: "buy".into(),
                    kind: "deploy_buy".into(),
                    status: "pending".into(),
                    quote: Some(r.usd),
                    price: Some(r.price),
                    base: Some(r.qty),
                    rung: Some(r.rung),
                    ts: Some(ts_cut),
                    note: Some(r.note.clone()),
                    label: label.map(str::to_string),
                    first_ts: Some(if truthy_f(r.since) {
                        r.since.unwrap_or(ts_cut)
                    } else {
                        ts_cut
                    }),
                    ..Default::default()
                });
                if let Err(e) = self.save() {
                    // Not on disk, so not placed: a failed placement a later run retries.
                    self.j.update(&cid, |o| {
                        o.status = "error".into();
                        o.last_error = Some(format!("not placed: {e}"));
                    });
                    self.push(
                        Some(s),
                        Some("buy"),
                        false,
                        Lvl::Err,
                        format!("DEPLOY {s}: not placed, {e}"),
                    );
                    return Ok(placements);
                }
                placements.push(cid);
            }
            let legs: Vec<String> = rungs
                .iter()
                .map(|r| format!("${} @ ${} ({})", fixed(r.usd, 2), g(r.price, 6), r.note))
                .collect();
            self.push(
                Some(s),
                Some("buy"),
                true,
                Lvl::Done,
                format!(
                    "DEPLOY {s} [spot ${}]: {}",
                    g(rungs[0].spot, 6),
                    legs.join("; ")
                ),
            );
        }
        Ok(placements)
    }

    /// Roll the venue's zones and ladder `tranche` plus what they held. A negative
    /// tranche re-ladders lighter (the withhold). `only` re-ladders just those coins.
    pub fn deploy_tranche(
        &mut self,
        client: &dyn Venue,
        exch: &str,
        quote: &str,
        tranche: f64,
        ts_cut: f64,
        only: &Only,
    ) -> Result<Vec<String>, String> {
        self.deploy_tranche_rolled(client, exch, quote, tranche, ts_cut, only)
            .map(|(cids, _)| cids)
    }

    /// [`Layer::deploy_tranche`], also returning the unspent budget it rolled in.
    pub fn deploy_tranche_rolled(
        &mut self,
        client: &dyn Venue,
        exch: &str,
        quote: &str,
        tranche: f64,
        ts_cut: f64,
        only: &Only,
    ) -> Result<(Vec<String>, f64), String> {
        let label = self.regime_label();
        let pinned = if self.cfg.deploy_pin_prices {
            self.resting_levels(exch, only)
        } else {
            BTreeMap::new()
        };
        let rolled = self.cancel_open_zones(client, exch, only)?;
        let budget = tranche + rolled;
        let vp = self.venue_plan(client, exch, budget, &label, only, &pinned)?;
        if vp.fallback && self.cfg.deploy_alloc.iter().any(|(_, w)| *w != 0.0) {
            self.warn(format!(
                "DEPLOY_ALLOC_JSON names no {exch} coin — falling back to equal split for this venue"
            ));
        }
        for sk in &vp.skips {
            self.warn(format!("deploy skip {sk}"));
        }
        let mut text = if tranche < 0.0 {
            format!(
                "DEPLOY re-ladder on {exch}: ${} rolled from unfilled zones minus ${} withheld for the Gate top-up",
                fixed(rolled, 2),
                fixed(-tranche, 2)
            )
        } else {
            let mut t = format!(
                "DEPLOY tranche on {exch}: ${} fresh {quote}",
                fixed(tranche, 2)
            );
            if rolled != 0.0 {
                t.push_str(&format!(
                    " + ${} rolled from unfilled zones",
                    fixed(rolled, 2)
                ));
            }
            t
        };
        text.push_str(&format!(
            " -> {} coins, regime {}",
            vp.plans.len(),
            label.to_uppercase()
        ));
        if self.cfg.deploy_pin_prices {
            let held = vp
                .plans
                .iter()
                .flat_map(|(_, rs)| rs.iter())
                .filter(|r| r.held)
                .count();
            text.push_str(&format!("; held {held} resting price level(s)"));
        }
        self.push(None, None, !vp.plans.is_empty(), Lvl::Done, text);
        if vp.plans.is_empty() {
            return Ok((Vec::new(), rolled));
        }
        let cids = self.journal_and_place(exch, &vp.plans, ts_cut, Some(&label))?;
        Ok((cids, rolled))
    }

    /// Re-fit a rung to the venue's real free balance when only pennies are missing
    /// (at least 99% of the notional kept); a bigger gap stays a loud failure.
    fn trim_to_free(&mut self, client: &dyn Venue, o: &mut Order) -> Result<bool, String> {
        let (exch, pair, sym) = (o.exch.clone(), o.pair.clone(), o.sym.clone());
        let price = o.price.unwrap_or(0.0);
        let base = o.base.unwrap_or(0.0);
        let quote = o.quote.unwrap_or(0.0);
        if exch.is_empty() || pair.is_empty() || price <= 0.0 || base <= 0.0 {
            return Ok(false);
        }
        let cur = match self.cfg.route(&sym).map(|r| r.quote.clone()) {
            Some(q) if !q.is_empty() => q,
            _ => {
                if pair.contains('/') {
                    pair.rsplit('/').next().unwrap_or_default().to_string()
                } else {
                    pair.rsplit('_').next().unwrap_or_default().to_string()
                }
            }
        };
        let got = (|| -> Result<(f64, f64, (f64, f64)), String> {
            let full = client.balances_full().map_err(|e| e.to_string())?;
            let free = bal(&full, &cur).free;
            let qty = Self::fit_qty(client, &pair, free, price)?;
            Ok((free, qty, Self::venue_mins(client, &pair)?))
        })();
        let Ok((free, qty, (minb, minq))) = got else {
            return Ok(false);
        };
        if !(0.0 < qty && qty < base) || qty < minb || qty * price < minq.max(0.99 * quote) {
            return Ok(false);
        }
        let note = format!(
            "{} (trimmed to free ${})",
            o.note.clone().unwrap_or_default(),
            fixed(free, 2)
        )
        .trim()
        .to_string();
        self.j.update(&o.client_id, |r| {
            r.base = Some(qty);
            r.quote = Some(qty * price);
            r.note = Some(note);
        });
        if self.save().is_err() {
            // The trimmed rung is not on disk: it is not placed this run.
            return Ok(false);
        }
        self.push(
            Some(&sym),
            None,
            false,
            Lvl::Done,
            format!(
                "deploy buy {sym} rung trimmed ${} -> ${} to fit free ${}",
                fixed(quote, 2),
                fixed(qty * price, 2),
                fixed(free, 2)
            ),
        );
        o.base = Some(qty);
        o.quote = Some(qty * price);
        Ok(true)
    }

    /// Place one journaled rung. `Ok(false)` when the venue refused it (the row is marked
    /// `error` and retried by a later run).
    pub fn place_entry(
        &mut self,
        client: &dyn Venue,
        o: &mut Order,
        trimmed: bool,
    ) -> Result<bool, String> {
        if self.write_failed().is_some() {
            // The journal cannot record what the venue would answer: place nothing.
            return Ok(false);
        }
        let cid = o.client_id.clone();
        let placed = client
            .limit_buy(
                &o.pair,
                o.base.unwrap_or(0.0),
                o.price.unwrap_or(0.0),
                Some(&cid),
            )
            .and_then(|raw| client.parse_order(&raw));
        match placed {
            Ok(lf) => {
                self.j.update(&cid, |r| {
                    r.status = if lf.status.is_empty() {
                        "open".into()
                    } else {
                        lf.status.clone()
                    };
                    r.order_id = Some(lf.order_id.clone()).filter(|s| !s.is_empty());
                    r.last_error = None;
                });
                self.save_after();
                Ok(true)
            }
            Err(e) => {
                let e = e.to_string();
                if !trimmed && short_by_pennies(&e) && self.trim_to_free(client, o)? {
                    return self.place_entry(client, o, true);
                }
                let tries = o.retries.unwrap_or(0) + 1;
                self.j.update(&cid, |r| {
                    r.status = "error".into();
                    r.retries = Some(tries);
                    r.last_error = Some(e.clone());
                });
                self.save_after();
                let sym = o.sym.clone();
                self.push(
                    Some(&sym),
                    Some("buy"),
                    false,
                    Lvl::Warn,
                    format!(
                        "deploy limit buy {sym} ${} @ ${} failed (auto-retries): {e}",
                        fixed(o.quote.unwrap_or(0.0), 2),
                        g(o.price.unwrap_or(0.0), 6)
                    ),
                );
                Ok(false)
            }
        }
    }

    /// Place the journaled ids in `cids` that have no venue id yet.
    pub fn place_unplaced(&mut self, client: &dyn Venue, cids: &[String]) -> Result<(), String> {
        for cid in cids {
            if let Some(mut o) = self.j.get(cid).cloned() {
                if !has_id(&o) {
                    self.place_entry(client, &mut o, false)?;
                }
            }
        }
        Ok(())
    }

    /// Crash and error recovery: place journaled rungs that never reached the venue,
    /// adopting an order that already rests there (the placement raced an error).
    pub fn resume_pending(&mut self) -> Result<(), String> {
        let mut rows: Vec<Order> = self
            .j
            .open_orders(Some("deploy_buy"))
            .into_iter()
            .cloned()
            .collect();
        rows.extend(self.j.errored(Some("deploy_buy")).into_iter().cloned());
        for mut o in rows {
            if has_id(&o) || !truthy_i(o.rung) {
                continue;
            }
            let (cid, sym, exch, pair) = (
                o.client_id.clone(),
                o.sym.clone(),
                o.exch.clone(),
                o.pair.clone(),
            );
            if cid.is_empty() || sym.is_empty() || exch.is_empty() || pair.is_empty() {
                continue;
            }
            let tries = o.retries.unwrap_or(0);
            if tries >= MAX_RETRIES {
                if o.gave_up != Some(true) {
                    self.j.update(&cid, |r| r.gave_up = Some(true));
                    self.save_after();
                    self.push(
                        Some(&sym),
                        None,
                        false,
                        Lvl::Warn,
                        format!(
                            "deploy buy failed {tries}x, giving up — place {} {sym} @ ${} manually",
                            g(o.base.unwrap_or(0.0), 6),
                            g(o.price.unwrap_or(0.0), 6)
                        ),
                    );
                }
                continue;
            }
            let Ok(client) = self.client(&exch) else {
                continue;
            };
            let Ok(resting) = client.open_orders(&pair) else {
                continue;
            };
            let claimed: BTreeSet<String> = self
                .j
                .orders
                .values()
                .filter_map(|x| x.order_id.clone().filter(|s| !s.is_empty()))
                .collect();
            let mut adopted = resting
                .iter()
                .find(|x| venue_id_matches(&exch, &x.client_id, &cid))
                .cloned();
            if adopted.is_none() {
                let same: Vec<_> = resting
                    .iter()
                    .filter(|x| !claimed.contains(&x.order_id))
                    .filter(|x| x.side.to_lowercase() == "buy")
                    .filter(|x| price_eq(x.price, o.price))
                    .collect();
                if same.len() == 1 {
                    adopted = Some(same[0].clone());
                }
            }
            if let Some(a) = adopted {
                self.j.update(&cid, |r| {
                    r.status = "open".into();
                    r.order_id = Some(a.order_id.clone()).filter(|s| !s.is_empty());
                    r.last_error = None;
                });
                self.save_after();
                self.push(
                    Some(&sym),
                    None,
                    false,
                    Lvl::Done,
                    format!(
                        "adopted resting deploy buy {sym} @ ${}",
                        g(a.price.unwrap_or(0.0), 6)
                    ),
                );
                continue;
            }
            if self.place_entry(client, &mut o, false)? {
                self.push(
                    Some(&sym),
                    Some("buy"),
                    true,
                    Lvl::Done,
                    format!(
                        "RETRY OK: deploy buy ${} {sym} @ ${}",
                        fixed(o.quote.unwrap_or(0.0), 2),
                        g(o.price.unwrap_or(0.0), 6)
                    ),
                );
            }
        }
        Ok(())
    }

    /// Stable credited on Gate since `since`, from Gate's own deposit history; `None`
    /// when the venue cannot say.
    fn gate_landed(&self, since: f64) -> Option<f64> {
        let gate = self.client("gate").ok()?;
        let rows = gate.deposits(since)?.ok()?;
        Some(py_sum(
            rows.iter()
                .filter(|r| {
                    matches!(r.currency.as_deref(), Some("USDC") | Some("USDT"))
                        && r.status.as_deref() == Some("DONE")
                        && r.ts >= since
                })
                .map(|r| r.amount),
        ))
    }

    fn reserve(&self, rx: f64, gate: f64) -> f64 {
        let routing: Vec<(String, String)> = self
            .cfg
            .routing
            .iter()
            .map(|(s, r)| (s.clone(), r.exch.clone()))
            .collect();
        let alloc: Vec<(String, f64)> = self
            .cfg
            .deploy_alloc
            .iter()
            .map(|(s, w)| (s.to_uppercase(), *w))
            .collect();
        funding::onramp_reserve(rx, gate, &alloc, &routing)
    }

    /// USDC that must stay free on Revolut X for the pending Gate top-up, or 0 while a
    /// top-up travels (the in-flight guard; see the module docs).
    fn onramp_keep(
        &mut self,
        st: &mut Map<String, Value>,
        rxf: &BTreeMap<String, Balance>,
        gtf: &BTreeMap<String, Balance>,
    ) -> f64 {
        if let Some(v) = st
            .get("inflight")
            .filter(|v| truthy_obj(v) && !v.is_object())
        {
            // A hand-edited or corrupt marker: read it as no top-up in flight.
            let shown = crate::pyfmt::dumps(v, None);
            st.remove("inflight");
            self.warn(format!(
                "deploy state: `inflight` is not an object ({shown}), treated as no top-up in flight"
            ));
        }
        let usdc = bal(rxf, "USDC");
        let usdc_now = usdc.free + usdc.locked;
        let gate_stable = funding::gate_stable_from(gtf);
        let prev = st
            .get("offquote")
            .and_then(|o| o.get("revx:USDC"))
            .and_then(Value::as_f64);
        let prev_run = match num(st.get("ts")) {
            x if x != 0.0 => x,
            _ => self.now() - 1800.0,
        };
        let min = self.cfg.deploy_min_usd;
        if let Some(prev) = prev.filter(|p| p - usdc_now >= min) {
            let out = prev - usdc_now;
            let since = (prev_run - INFLIGHT_SLACK_S).max(0.0);
            match self.gate_landed(since) {
                Some(landed) if landed >= 0.8 * out => self.done(format!(
                    "REVX: ${} USDC left the venue and ${} already landed on Gate -> no in-flight hold, reserve tracking continues",
                    fixed(out, 2),
                    fixed(landed, 2)
                )),
                _ => {
                    let present = st
                        .get_mut("inflight")
                        .filter(|v| inflight_marker(v))
                        .and_then(Value::as_object_mut);
                    if let Some(fl) = present {
                        let amt = fl.get("amount").and_then(Value::as_f64).unwrap_or(0.0) + out;
                        fl.insert("amount".into(), json!(amt));
                    } else {
                        let at = match st.get("stable").and_then(|s| s.get("gate")) {
                            Some(v) => v.as_f64().unwrap_or(0.0),
                            None => gate_stable,
                        };
                        st.insert(
                            "inflight".into(),
                            json!({"amount": out, "ts": self.now(), "since": since,
                                   "gate_stable_at": at}),
                        );
                    }
                    self.warn(format!(
                        "REVX: ${} USDC left the venue -> Gate top-up in flight; the reserve is paused until it lands on Gate",
                        fixed(out, 2)
                    ));
                }
            }
        }
        if let Some(fl) = st.get("inflight").filter(|v| inflight_marker(v)).cloned() {
            let fts = fl.get("ts").and_then(Value::as_f64).unwrap_or(0.0);
            let since = match num(fl.get("since")) {
                x if x != 0.0 => x,
                _ => fts - 1800.0,
            };
            let (landed, how) = match self.gate_landed(since) {
                Some(l) => (l, "Gate deposits"),
                None => (
                    gate_stable
                        - fl.get("gate_stable_at")
                            .and_then(Value::as_f64)
                            .unwrap_or(0.0),
                    "Gate stable",
                ),
            };
            let age = self.now() - fts;
            let amount = fl.get("amount").and_then(Value::as_f64).unwrap_or(0.0);
            if landed >= 0.8 * amount || age >= INFLIGHT_MAX_S {
                st.remove("inflight");
                self.done(format!(
                    "REVX: Gate top-up landed ({how} +${}) -> reserve tracking resumes",
                    fixed(landed, 2)
                ));
            } else {
                return 0.0;
            }
        }
        self.reserve(funding::revx_stable_from(rxf), gate_stable)
    }

    /// Turn free USD into USDC, up to `want`, leaving fee headroom. Not journaled.
    fn stage_usdc(&mut self, client: &dyn Venue, want: f64, usd_free: f64) -> Result<f64, String> {
        let stage = py_round(want.min(usd_free * 0.998 - ONRAMP_HEADROOM_USD), 2);
        if stage < self.cfg.deploy_min_usd {
            return Ok(0.0);
        }
        client
            .market_buy("USDC/USD", stage, None)
            .map_err(|e| e.to_string())?;
        self.push(
            None,
            None,
            true,
            Lvl::Done,
            format!(
                "REVX onramp: staged ${} USDC for the pending Gate top-up (send it from the app; the bot keeps it in USDC)",
                fixed(stage, 2)
            ),
        );
        (self.sleep)(2.0);
        Ok(stage)
    }

    /// The Revolut X onramp pre-step: EUR → USDC, then USDC → USD except what the Gate
    /// top-up keeps, or stage the top-up as USDC. Returns the kept amount and whether
    /// the reserve is unknown (Gate's balances could not be read, or Gate has no client):
    /// then nothing moves out of USDC this run.
    fn onramp(&mut self, st: &mut Map<String, Value>) -> Result<(f64, bool), String> {
        let min = self.cfg.deploy_min_usd;
        let revx = self.client("revx")?;
        let mut rxb = revx.balances().map_err(|e| e.to_string())?;
        let rx_eur = rxb.get("EUR").copied().unwrap_or(0.0);
        if rx_eur >= min {
            let spend = py_round(rx_eur * 0.998, 2);
            revx.market_buy("USDC/EUR", spend, None)
                .map_err(|e| e.to_string())?;
            self.push(
                None,
                None,
                true,
                Lvl::Done,
                format!(
                    "REVX onramp: converted EUR {} -> USDC for auto-deploy",
                    fixed(spend, 2)
                ),
            );
            (self.sleep)(2.0);
            rxb = revx.balances().map_err(|e| e.to_string())?;
        }
        let rx_usdc = rxb.get("USDC").copied().unwrap_or(0.0);
        let mut keep = 0.0;
        let mut unknown = false;
        let mut rxf = BTreeMap::new();
        let both = (|| -> Result<_, String> {
            let a = revx.balances_full().map_err(|e| e.to_string())?;
            let b = self
                .client("gate")?
                .balances_full()
                .map_err(|e| e.to_string())?;
            Ok((a, b))
        })();
        match both {
            Ok((a, b)) => {
                rxf = a;
                keep = self.onramp_keep(st, &rxf, &b);
            }
            Err(e) => {
                // Fail closed: without Gate's balance the reserve is unknown, and selling
                // the staged USDC back to USD would let this run ladder the top-up.
                unknown = true;
                self.warn(format!(
                    "Gate top-up reserve unavailable in the revx pre-step; staged USDC stays USDC this run: {e}"
                ))
            }
        }
        let rx_usdc_out = bal(&rxf, "USDC").locked;
        if unknown {
            return Ok((keep, true));
        }
        if rx_usdc - keep >= min {
            let qty = revx
                .round_amount("USDC/USD", rx_usdc - keep)
                .map_err(|e| e.to_string())?;
            revx.market_sell("USDC/USD", qty, None)
                .map_err(|e| e.to_string())?;
            let mut t = format!(
                "REVX onramp: converted {} USDC -> USD for auto-deploy",
                g(qty, 6)
            );
            if keep != 0.0 {
                t.push_str(&format!(
                    " (kept ${} USDC back for the pending Gate top-up)",
                    fixed(keep, 2)
                ));
            }
            self.push(None, None, true, Lvl::Done, t);
            (self.sleep)(2.0);
        } else if keep - rx_usdc - rx_usdc_out >= min {
            let usd = rxb.get("USD").copied().unwrap_or(0.0);
            self.stage_usdc(revx, keep - rx_usdc - rx_usdc_out, usd)?;
        }
        Ok((keep, false))
    }

    /// Gate: USDC that landed (the top-up's bridge asset) becomes USDT. Not journaled.
    fn gate_convert(&mut self) -> Result<(), String> {
        let gate = self.client("gate")?;
        let full = gate.balances_full().map_err(|e| e.to_string())?;
        let g_usdc = bal(&full, "USDC").free;
        if g_usdc >= self.cfg.deploy_min_usd {
            let qty = gate
                .round_amount("USDC_USDT", g_usdc)
                .map_err(|e| e.to_string())?;
            gate.market_sell("USDC_USDT", qty, None)
                .map_err(|e| e.to_string())?;
            self.push(
                None,
                None,
                true,
                Lvl::Done,
                format!(
                    "GATE: auto-converted {} USDC -> USDT (revx top-up bridge asset) for auto-deploy",
                    g(qty, 6)
                ),
            );
            (self.sleep)(2.0);
        }
        Ok(())
    }

    /// The bull sweep: market-buy the unspent budget of zones resting at least
    /// `deploy_bull_sweep_days`, per coin, while the bull is young and the coin runs.
    /// The buy is capped at the venue's free quote minus what `reserves` holds there.
    pub fn bull_sweep(
        &mut self,
        clients: &[&str],
        now: f64,
        reserves: &BTreeMap<&str, f64>,
    ) -> Result<Vec<String>, String> {
        let cfg = self.cfg;
        if !cfg.deploy_bull_sweep {
            return Ok(Vec::new());
        }
        let got = self
            .regime
            .regime()
            .and_then(|r| Ok((r, self.regime.label_history()?)));
        let (reg, hist) = match got {
            Ok(x) => x,
            Err(e) => {
                self.warn(format!("bull sweep skipped: regime unreadable ({e})"));
                return Ok(Vec::new());
            }
        };
        if reg.get("market").and_then(Value::as_str) != Some("bull") {
            return Ok(Vec::new());
        }
        let labels: Vec<String> = hist
            .get("labels")
            .and_then(Value::as_array)
            .map(|a| a.iter().map(crate::pyfmt::value_str).collect())
            .unwrap_or_default();
        let Some(age) = plan::bull_age_days(&labels) else {
            return Ok(Vec::new());
        };
        if age as f64 > cfg.deploy_bull_sweep_max_age {
            return Ok(Vec::new());
        }
        let days = cfg.deploy_bull_sweep_days;
        let mut stale: BTreeMap<(String, String), Vec<Order>> = BTreeMap::new();
        for o in self.j.open_orders(Some("deploy_buy")) {
            if !truthy_i(o.rung) || !has_id(o) {
                continue;
            }
            let at = if truthy_f(o.first_ts) {
                o.first_ts.unwrap_or(now)
            } else if truthy_f(o.ts) {
                o.ts.unwrap_or(now)
            } else {
                now
            };
            if now - at < days * 86_400.0 {
                continue;
            }
            stale
                .entry((o.exch.clone(), o.sym.clone()))
                .or_default()
                .push(o.clone());
        }
        let min = cfg.deploy_min_usd;
        let mut done = Vec::new();
        for ((exch, sym), rows) in stale {
            let running = reg
                .get("coins")
                .and_then(|c| c.get(&sym))
                .and_then(|c| c.get("running"))
                .is_some_and(|v| crate::pyfmt::truthy(Some(v)));
            if !running {
                continue;
            }
            if !clients.contains(&exch.as_str()) {
                continue;
            }
            let Ok(client) = self.client(&exch) else {
                continue;
            };
            let mut budget = 0.0;
            for o in &rows {
                match self.cancel_ours(client, o, BULL_NOTE)? {
                    Ok(Settled::Unreadable(_)) => {
                        // Final status unreadable: it may have filled since the last poll.
                        // A market buy of the stale unspent would spend other free cash.
                        self.push(
                            Some(&sym),
                            None,
                            false,
                            Lvl::Warn,
                            format!(
                                "bull sweep: {} final status unreadable, its budget is not swept",
                                o.client_id
                            ),
                        );
                    }
                    Ok(s) => budget += Self::unspent(o, &s),
                    Err(e) => self.push(
                        Some(&sym),
                        None,
                        false,
                        Lvl::Warn,
                        format!(
                            "bull sweep: cancel failed (may have just filled, reconciles next run): {e}"
                        ),
                    ),
                }
            }
            let mut mkt = plan::floor_cents(budget * 0.997);
            if mkt >= min {
                // Never spend more than the venue holds free beyond the top-up reserve:
                // the journal's unspent can be stale (a fill the venue has not reported).
                let q = Self::quotes(cfg).get(&exch).cloned().unwrap_or_default();
                match client.balances_full() {
                    Ok(b) => {
                        let held = reserves.get(exch.as_str()).copied().unwrap_or(0.0);
                        let spendable =
                            plan::floor_cents((bal(&b, &q).free - held).max(0.0) * 0.997);
                        if spendable < mkt {
                            self.push(
                                Some(&sym),
                                None,
                                false,
                                Lvl::Warn,
                                format!(
                                    "bull sweep: {sym} on {exch} capped at ${} (free {q} ${} minus ${} held) instead of ${}",
                                    fixed(spendable, 2),
                                    fixed(bal(&b, &q).free, 2),
                                    fixed(held, 2),
                                    fixed(mkt, 2)
                                ),
                            );
                            mkt = spendable;
                        }
                    }
                    Err(e) => {
                        self.push(
                            Some(&sym),
                            None,
                            false,
                            Lvl::Warn,
                            format!(
                                "bull sweep: {exch} balances unreadable, {sym} cash left free: {e}"
                            ),
                        );
                        continue;
                    }
                }
            }
            if mkt < min {
                if budget != 0.0 {
                    self.push(
                        Some(&sym),
                        None,
                        false,
                        Lvl::Warn,
                        format!(
                            "bull sweep: ${} {sym} on {exch} under ${}, left free",
                            fixed(budget, 2),
                            g(min, 6)
                        ),
                    );
                }
                continue;
            }
            let pair = rows[0].pair.clone();
            let prefix = if exch == "revx" { "depx" } else { "dep" };
            let cid = format!("{prefix}{sym}{}s", now as i64);
            self.j.record(Order {
                client_id: cid.clone(),
                sym: sym.clone(),
                exch: exch.clone(),
                pair: pair.clone(),
                side: "buy".into(),
                kind: "deploy_buy".into(),
                status: "pending".into(),
                quote: Some(mkt),
                rung: Some(0),
                ts: Some(now),
                note: Some(format!(
                    "bull sweep: {} zone(s) resting >= {}d, bull {age}d old",
                    rows.len(),
                    g(days, 6)
                )),
                ..Default::default()
            });
            if let Err(e) = self.save() {
                self.j.update(&cid, |o| {
                    o.status = "error".into();
                    o.last_error = Some(format!("not placed: {e}"));
                });
                self.push(
                    Some(&sym),
                    None,
                    false,
                    Lvl::Err,
                    format!(
                        "bull sweep: market buy {sym} ${} not placed, cash left free: {e}",
                        fixed(mkt, 2)
                    ),
                );
                continue;
            }
            let r = client
                .market_buy(&pair, mkt, Some(&cid))
                .and_then(|raw| client.parse_order(&raw));
            match r {
                Ok(lf) => {
                    self.j.update(&cid, |o| {
                        o.order_id = Some(lf.order_id.clone()).filter(|s| !s.is_empty());
                        o.status = "new".into();
                        o.last_error = None;
                    });
                    self.save_after();
                    self.push(
                        Some(&sym),
                        Some("buy"),
                        true,
                        Lvl::Done,
                        format!(
                            "BULL SWEEP {sym} on {exch}: market ${} from {} zone(s) resting >= {}d (bull {age}d old, cutoff {}d)",
                            fixed(mkt, 2),
                            rows.len(),
                            g(days, 6),
                            g(cfg.deploy_bull_sweep_max_age, 6)
                        ),
                    );
                    done.push(cid);
                }
                Err(e) => {
                    let e = e.to_string();
                    self.j.update(&cid, |o| {
                        o.status = "error".into();
                        o.last_error = Some(e.clone());
                    });
                    self.save_after();
                    self.push(
                        Some(&sym),
                        None,
                        false,
                        Lvl::Warn,
                        format!(
                            "bull sweep: market buy {sym} ${} FAILED, cash left free: {e}",
                            fixed(mkt, 2)
                        ),
                    );
                }
            }
        }
        Ok(done)
    }

    /// Journal-explained quote deltas per venue since the baseline in `st` (fills since
    /// its `ts`, partial fills already netted while open counted once), and the partial
    /// fills now on open zones (the next `part`).
    pub fn explained(
        &self,
        st: &Map<String, Value>,
        venues: &[&str],
    ) -> (BTreeMap<String, f64>, Map<String, Value>) {
        let prev_ts = num(st.get("ts"));
        let part_prev: Map<String, Value> = st
            .get("part")
            .and_then(Value::as_object)
            .cloned()
            .unwrap_or_default();
        let mut explained: BTreeMap<String, f64> =
            venues.iter().map(|e| (e.to_string(), 0.0)).collect();
        if prev_ts != 0.0 {
            for o in self.j.filled_since(prev_ts, false) {
                let Some(x) = explained.get_mut(o.exch.as_str()) else {
                    continue;
                };
                let fq = o.filled_quote.unwrap_or(0.0);
                if o.side == "buy" {
                    *x -= (fq - num(part_prev.get(&o.client_id))).max(0.0);
                } else {
                    *x += fq;
                }
            }
        }
        let mut part_cur = Map::new();
        for o in self.j.open_orders(Some("deploy_buy")) {
            let pq = o.part_quote.unwrap_or(0.0);
            if let Some(x) = explained.get_mut(o.exch.as_str()) {
                part_cur.insert(o.client_id.clone(), json!(pq));
                *x -= (pq - num(part_prev.get(&o.client_id))).max(0.0);
            }
        }
        (explained, part_cur)
    }

    /// Why the layer may not trade, if it may not.
    pub fn blocked(cfg: &RunConfig) -> Option<String> {
        if !cfg.live_trading_enabled {
            return Some("LIVE_TRADING_ENABLED is not 'yes'".into());
        }
        if cfg.halt_file.exists() {
            return Some(format!("halt file present ({})", cfg.halt_file.display()));
        }
        None
    }

    /// The venues that have a client, in the layer's order.
    pub fn clients(&self) -> Vec<&'static str> {
        VENUES
            .iter()
            .copied()
            .filter(|e| self.venues.venue(e).is_ok())
            .collect()
    }

    /// One run of the layer. Appends its results to `self.results`. `Err` is a failure
    /// the layer does not catch (an unreadable state file, a venue read the reference let
    /// propagate); the run reports it as `deploy layer: …`.
    pub fn check(&mut self) -> Result<(), String> {
        let cfg = self.cfg;
        if cfg.deploy != "live" {
            return Ok(());
        }
        let spath = cfg.deploy_state_path();
        let mut st = load_state(&spath)?;
        if let Some(reason) = Self::blocked(cfg) {
            if !st
                .get("blocked")
                .is_some_and(|v| crate::pyfmt::truthy(Some(v)))
            {
                st.insert("blocked".into(), json!(reason));
                save_state(&spath, &st)?;
                self.warn(format!("deploy layer idle: {reason}"));
            }
            return Ok(());
        }
        if st
            .remove("blocked")
            .is_some_and(|v| crate::pyfmt::truthy(Some(&v)))
        {
            save_state(&spath, &st)?;
        }
        let clients = self.clients();
        let ts_cut = self.now();
        let min = cfg.deploy_min_usd;

        self.resume_pending()?;
        if self.write_failed().is_some() {
            // A rung may be at a venue that the journal on disk does not show: move
            // nothing more. The run reports the failed write.
            return Ok(());
        }

        let (onramp_keep, mut reserve_unknown) = match self.onramp(&mut st) {
            Ok(k) => k,
            Err(e) => {
                self.warn(format!(
                    "revx onramp conversion failed (deposit keeps its currency, retried next run): {e}"
                ));
                (0.0, false)
            }
        };
        let inflight_active = st.get("inflight").is_some_and(inflight_marker);

        if let Err(e) = self.gate_convert() {
            self.warn(format!(
                "gate USDC -> USDT conversion failed (top-up keeps its currency, retried next run): {e}"
            ));
        }

        let quotes = Self::quotes(cfg);

        let mut full: BTreeMap<&str, Option<BTreeMap<String, Balance>>> = BTreeMap::new();
        for &exch in &clients {
            match self.client(exch)?.balances_full() {
                Ok(b) => {
                    full.insert(exch, Some(b));
                }
                Err(e) => {
                    full.insert(exch, None);
                    self.push(
                        None,
                        None,
                        false,
                        Lvl::Err,
                        format!("deploy: {exch} balances: {e}"),
                    );
                }
            }
        }

        let (explained, part_cur) = self.explained(&st, &clients);
        let open_zones: Vec<Order> = self
            .j
            .open_orders(Some("deploy_buy"))
            .into_iter()
            .cloned()
            .collect();

        let baselines: Map<String, Value> = st
            .get("stable")
            .and_then(Value::as_object)
            .cloned()
            .unwrap_or_default();
        let mut offquote: Map<String, Value> = st
            .get("offquote")
            .and_then(Value::as_object)
            .cloned()
            .unwrap_or_default();

        let mut reserves: BTreeMap<&str, f64> = BTreeMap::new();
        let mut rx_reserve_full = 0.0;
        let rx_full = full
            .get("revx")
            .and_then(Option::as_ref)
            .filter(|b| !b.is_empty());
        let gt_full = full
            .get("gate")
            .and_then(Option::as_ref)
            .filter(|b| !b.is_empty());
        if rx_full.is_some() && gt_full.is_none() {
            // Gate unreadable or without a client: the reserve is unknown.
            reserve_unknown = true;
        }
        if let (Some(rxb), Some(gtb)) = (rx_full, gt_full) {
            let rx_stable = funding::revx_stable_from(rxb);
            let gate_stable = funding::gate_stable_from(gtb);
            let u = bal(rxb, "USDC");
            let staged = u.free + u.locked;
            rx_reserve_full = if inflight_active {
                0.0
            } else {
                self.reserve(rx_stable, gate_stable)
            };
            let held = (rx_reserve_full - staged).max(0.0);
            if held > 0.0 {
                reserves.insert("revx", held);
            }
        }
        if let (true, Some(rxb)) = (reserve_unknown, rx_full) {
            // Fail closed: hold ALL free revx stable this run. New capital is not absorbed
            // into the baseline (it carries to the next run), nothing is swept or laddered.
            let q = quotes.get("revx").cloned().unwrap_or_default();
            reserves.insert("revx", bal(rxb, &q).free);
            self.warn(
                "Gate top-up reserve unknown (Gate balance unreadable): no new capital laddered on revx this run"
                    .into(),
            );
        }

        type Tranche = (&'static str, String, f64);
        type Sweep = (&'static str, String, f64, Only, Vec<String>);
        let mut new_baseline = Map::new();
        let mut tranches: Vec<Tranche> = Vec::new();
        let mut sweeps: Vec<Sweep> = Vec::new();
        let mut withholds: Vec<Tranche> = Vec::new();
        for &exch in &clients {
            let Some(Some(fb)) = full.get(exch).cloned() else {
                if let Some(b) = baselines.get(exch) {
                    new_baseline.insert(exch.into(), b.clone());
                }
                continue;
            };
            let q = quotes.get(exch).cloned().unwrap_or_default();
            let b = bal(&fb, &q);
            let total = b.free + b.locked;
            if !baselines.contains_key(exch) {
                new_baseline.insert(exch.into(), json!(total));
                self.done(format!(
                    "DEPLOY baseline initialized: {exch} {q} ${} — deposits from now on auto-ladder into buy zones",
                    fixed(total, 2)
                ));
            } else {
                let expected = num(baselines.get(exch)) + explained[exch];
                let unexplained = total - expected;
                let held = reserves.get(exch).copied().unwrap_or(0.0);
                let tranche = unexplained
                    .min(cfg.deploy_max_tranche_usd)
                    .min((b.free - held).max(0.0));
                if unexplained >= min && tranche >= min {
                    tranches.push((exch, q.clone(), tranche));
                    new_baseline.insert(exch.into(), json!(total - (unexplained - tranche)));
                    if held != 0.0 {
                        self.warn(format!(
                            "holding ${} free on {exch} for the pending Gate top-up — laddered ${} of ${} new capital",
                            fixed(held, 2),
                            fixed(tranche, 2),
                            fixed(unexplained, 2)
                        ));
                    }
                } else if unexplained > 0.0 {
                    new_baseline.insert(exch.into(), json!(expected));
                    if held != 0.0 && unexplained >= min {
                        self.warn(format!(
                            "${} new capital on {exch} left UNDEPLOYED — ${} is reserved for the pending Gate top-up. Send it; the rest ladders on the next run.",
                            fixed(unexplained, 2),
                            fixed(held, 2)
                        ));
                    }
                } else {
                    new_baseline.insert(exch.into(), json!(total));
                }
                let stuck_at = num(st.get("sweep_stuck").and_then(|s| s.get(exch)));
                let freed_rows: Vec<Order> = self
                    .j
                    .venue_cancelled_unswept(Some("deploy_buy"))
                    .into_iter()
                    .filter(|o| o.exch == exch)
                    .cloned()
                    .collect();
                let freed = py_sum(
                    freed_rows
                        .iter()
                        .map(|o| o.quote.unwrap_or(0.0) - o.part_quote.unwrap_or(0.0)),
                );
                let idle = (b.free - held).min(freed);
                if cfg.deploy_sweep_idle
                    && unexplained < min
                    && idle >= min
                    && ts_cut - stuck_at >= SWEEP_RETRY_S
                {
                    let zoned: BTreeSet<&str> = open_zones
                        .iter()
                        .filter(|o| o.exch == exch && truthy_i(o.rung))
                        .map(|o| o.sym.as_str())
                        .collect();
                    let bare: BTreeSet<String> = Self::venue_syms(cfg, exch)
                        .into_iter()
                        .filter(|s| !zoned.contains(s.as_str()))
                        .collect();
                    let what = if bare.is_empty() {
                        "the whole venue".to_string()
                    } else {
                        format!(
                            "{} (coins without zones)",
                            bare.iter().cloned().collect::<Vec<_>>().join(", ")
                        )
                    };
                    sweeps.push((
                        exch,
                        q.clone(),
                        idle,
                        (!bare.is_empty()).then_some(bare),
                        freed_rows.iter().map(|o| o.client_id.clone()).collect(),
                    ));
                    new_baseline.insert(exch.into(), json!(total));
                    self.push(
                        None,
                        None,
                        true,
                        Lvl::Done,
                        format!(
                            "DEPLOY sweep: ${} {q} sitting FREE on {exch} with no new capital behind it (a zone was cancelled venue-side or left over) -> re-laddering {what}",
                            fixed(idle, 2)
                        ),
                    );
                }
            }
            if exch == "revx"
                && baselines.contains_key(exch)
                && rx_reserve_full > 0.0
                && !tranches.iter().any(|t| t.0 == exch)
                && !sweeps.iter().any(|s| s.0 == exch)
            {
                let u = bal(&fb, "USDC");
                let free_stable = b.free + (u.free + u.locked);
                let zones: Vec<&Order> = open_zones
                    .iter()
                    .filter(|o| o.exch == exch && truthy_i(o.rung))
                    .collect();
                let resting = py_sum(
                    zones
                        .iter()
                        .map(|o| (o.quote.unwrap_or(0.0) - o.part_quote.unwrap_or(0.0)).max(0.0)),
                );
                let short = (rx_reserve_full - free_stable).min(resting);
                let stuck_at = num(st.get("withhold_stuck").and_then(|s| s.get(exch)));
                if short >= min && !zones.is_empty() && ts_cut - stuck_at >= SWEEP_RETRY_S {
                    withholds.push((exch, q.clone(), short));
                    self.push(
                        None,
                        None,
                        true,
                        Lvl::Done,
                        format!(
                            "DEPLOY withhold on {exch}: Gate top-up ${} due but only ${} free -> re-laddering the venue ${} lighter so it can leave as USDC",
                            fixed(rx_reserve_full, 2),
                            fixed(free_stable, 2),
                            fixed(short, 2)
                        ),
                    );
                }
            }
            let oq = if q == "USDC" { "USDT" } else { "USDC" };
            let ob = bal(&fb, oq);
            let ototal = ob.free + ob.locked;
            let okey = format!("{exch}:{oq}");
            if let Some(prev) = offquote.get(&okey).map(|v| num(Some(v))) {
                if ototal - prev >= min && !(exch == "revx" && ototal <= onramp_keep + min) {
                    self.warn(format!(
                        "${} {oq} landed on {exch}, but its pairs are {q}-quoted — convert to {q} for auto-deploy",
                        fixed(ototal - prev, 2)
                    ));
                }
            }
            offquote.insert(okey, json!(ototal));
        }

        // A venue with no client this run keeps its baseline: dropping it would read the
        // venue's whole balance as a cold start (or, once re-baselined, lose a deposit).
        for (exch, b) in &baselines {
            if !clients.contains(&exch.as_str()) {
                new_baseline
                    .entry(exch.clone())
                    .or_insert_with(|| b.clone());
            }
        }
        // The baseline commits before anything is journaled or placed.
        st.insert("ts".into(), json!(ts_cut));
        st.insert("stable".into(), Value::Object(new_baseline));
        st.insert("part".into(), Value::Object(part_cur));
        st.insert("offquote".into(), Value::Object(offquote));
        if let Err(e) = save_state(&spath, &st) {
            // Without the new baseline on disk the next run would ladder the same money
            // again: ladder nothing now. The onramp steps above are not journaled.
            self.push(
                None,
                None,
                false,
                Lvl::Err,
                format!("deploy state write failed, nothing laddered this run: {e}"),
            );
            return Ok(());
        }

        let mut placements: Vec<(&'static str, String)> = Vec::new();
        for (exch, q, tranche) in &tranches {
            let client = self.client(exch)?;
            for cid in self.deploy_tranche(client, exch, q, *tranche, ts_cut, &None)? {
                placements.push((exch, cid));
            }
        }
        for (exch, q, idle, only, freed) in &sweeps {
            let client = self.client(exch)?;
            let cids = self.deploy_tranche(client, exch, q, *idle, ts_cut, only)?;
            let stuck = st
                .entry("sweep_stuck")
                .or_insert_with(|| json!({}))
                .as_object_mut()
                .ok_or("sweep_stuck is not an object")?;
            if cids.is_empty() {
                stuck.insert((*exch).into(), json!(ts_cut));
                self.warn(format!(
                    "DEPLOY sweep: ${} {q} on {exch} could not be laddered (see skips above) — retrying in 24h; if a pair is gone, move the cash or route the coin elsewhere",
                    fixed(*idle, 2)
                ));
            } else {
                stuck.remove(*exch);
                self.j.mark_swept(freed);
                self.save_after();
            }
            self.save_state_after(&spath, &st);
            placements.extend(cids.into_iter().map(|c| (*exch, c)));
        }
        for (exch, q, short) in &withholds {
            let client = self.client(exch)?;
            let cids = self.deploy_tranche(client, exch, q, -short, ts_cut, &None)?;
            let stuck = st
                .entry("withhold_stuck")
                .or_insert_with(|| json!({}))
                .as_object_mut()
                .ok_or("withhold_stuck is not an object")?;
            if cids.is_empty() {
                stuck.insert((*exch).into(), json!(ts_cut));
                self.warn(format!(
                    "DEPLOY withhold: {exch} could not be re-laddered ${} lighter (see skips above) — retrying in 24h; the cash stays free meanwhile",
                    fixed(*short, 2)
                ));
            } else {
                stuck.remove(*exch);
            }
            self.save_state_after(&spath, &st);
            placements.extend(cids.into_iter().map(|c| (*exch, c)));
        }
        for (exch, cid) in &placements {
            let client = self.client(exch)?;
            self.place_unplaced(client, std::slice::from_ref(cid))?;
        }

        self.bull_sweep(&clients, ts_cut, &reserves)?;

        let touched_revx = tranches.iter().any(|t| t.0 == "revx")
            || sweeps.iter().any(|s| s.0 == "revx")
            || withholds.iter().any(|w| w.0 == "revx");
        if onramp_keep > 0.0 && touched_revx {
            if let Err(e) = self.stage_after_roll(&mut st, &spath, onramp_keep) {
                self.warn(format!(
                    "revx USDC staging after the re-ladder failed (retried next run): {e}"
                ));
            }
        }
        Ok(())
    }

    fn stage_after_roll(
        &mut self,
        st: &mut Map<String, Value>,
        spath: &Path,
        keep: f64,
    ) -> Result<(), String> {
        if let Some(e) = self.write_failed() {
            return Err(e);
        }
        let revx = self.client("revx")?;
        let rxf = revx.balances_full().map_err(|e| e.to_string())?;
        let u = bal(&rxf, "USDC");
        let usdc = u.free + u.locked;
        if keep - usdc >= self.cfg.deploy_min_usd
            && self.stage_usdc(revx, keep - usdc, bal(&rxf, "USD").free)? != 0.0
        {
            let rxf = revx.balances_full().map_err(|e| e.to_string())?;
            let u = bal(&rxf, "USDC");
            let oq = st
                .entry("offquote")
                .or_insert_with(|| json!({}))
                .as_object_mut()
                .ok_or("offquote is not an object")?;
            oq.insert("revx:USDC".into(), json!(u.free + u.locked));
            save_state(spath, st)?;
        }
        Ok(())
    }
}

fn has_id(o: &Order) -> bool {
    o.order_id.as_deref().is_some_and(|s| !s.is_empty())
}

fn truthy_i(x: Option<i64>) -> bool {
    x.is_some_and(|v| v != 0)
}

fn truthy_obj(v: &Value) -> bool {
    crate::pyfmt::truthy(Some(v))
}

/// A top-up-in-flight marker: a non-empty object. Anything else is no marker.
fn inflight_marker(v: &Value) -> bool {
    v.as_object().is_some_and(|m| !m.is_empty())
}

/// The deploy hook a live run calls, bound to its config.
#[derive(Debug, Default, Clone, Copy)]
pub struct DeployLayer;

impl crate::run::hooks::DeployHook for DeployLayer {
    fn check(
        &mut self,
        ctx: &mut crate::run::hooks::HookCtx,
        execution: &mut Vec<RunResult>,
    ) -> Result<(), String> {
        let feed = LiveRegime {
            cfg: ctx.cfg,
            market: ctx.market,
            now: ctx.now,
        };
        let stderr = |s: &str| eprintln!("{s}");
        let mut layer = Layer {
            cfg: ctx.cfg,
            venues: ctx.venues,
            regime: &feed,
            clock: ctx.clock,
            sleep: ctx.sleep,
            persist: &mut *ctx.persist,
            stderr: &stderr,
            j: ctx.journal,
            results: Vec::new(),
        };
        let r = layer.check();
        execution.append(&mut layer.results);
        r
    }
}