rusty-agent-server 0.12.0

The durable agent runtime built in Rust — HTTP/SSE server: an Agent-Protocol subset with threads, background runs, checkpoints, and resumable streaming.
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
//! The credential/connection broker (R0.11 Extension Plane, wave 3):
//! envelope-encrypted connection storage on both backends, the handle
//! issue/resolve/revoke/expire lifecycle, and the journaled evidence
//! chain. The pure contracts live in `rusty_agent_runtime::broker`;
//! this module owns key custody, cryptography, and persistence.
//!
//! Wave 4 adds the OAuth lifecycle: refresh-at-resolution (a token
//! inside its jittered refresh window is rotated by the deployment's
//! [`OAuthProvider`](rusty_agent_runtime::broker::OAuthProvider) before
//! it is served), the background sweeper ([`Broker::sweep_once`] /
//! [`spawn_sweeper`]) that runs the same lifecycle over every
//! connection on an interval, and the terminal refusal path — a
//! provider-classified permanent failure flips the connection to
//! `needs_reauth` (journaled `connection_needs_reauth`) and every use
//! fails closed until a human records a new consent act.
//!
//! ## Key custody
//!
//! The deployment master key lives **outside the store abstraction**,
//! exactly as receipt signing secrets do (`crate::receipts`): one file
//! per master key under `{store_path}/keys/broker-master.{key_id}.secret`
//! (hex, `0600` from the first byte, written once), so the Postgres
//! backend cannot hold what a database must not leak. Key ids are
//! random (`bmk-{16 hex}` — never a hash of the key: a content address
//! of symmetric material is a verifier oracle). Envelopes record the
//! `key_id` that wrapped them, so rotation (the design's open question
//! 3: lazy re-wrap of per-connection data keys, KMS/HSM as the R1.0
//! plug point) lands additively; wave 3 runs a single active key.
//!
//! ## Cryptography
//!
//! XChaCha20-Poly1305 (see `Cargo.toml` for the dependency rationale).
//! Each connection's token material is encrypted under a per-connection
//! data key (32 random bytes, minted at registration); data keys are
//! wrapped by the master key. Both seals authenticate the connection id
//! as associated data, so a ciphertext transplanted between connections
//! fails to open. Handles are HMAC-SHA256-signed claims under a key
//! derived from the master key by HMAC domain separation — validity
//! (expiry, scope binding) is self-contained, and only the connection
//! liveness check hits the store at resolution.
//!
//! ## Evidence
//!
//! Everything journals into the deployment's broker evidence chain, run
//! id [`BROKER_JOURNAL_RUN_ID`] — the `receipt-keys` precedent applied
//! to a second control plane. Hard-fail: a mutation or use that cannot
//! journal does not happen (nothing reaches the store the journal did
//! not record first). Uses and denials name the connection, the handle,
//! and the grant — never the bytes; the run binding travels in the
//! payload rather than in the run's own journal, because a run's
//! journal is not always server-persisted (embedder-driven runs) and
//! the deployment chain is the only target the broker can always write.

use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use chrono::Utc;
use hmac::{Hmac, Mac};
use rusty_agent_runtime::broker::{
    hex_decode, hex_encode, new_connection_id, new_handle_id, scopes_missing, BrokerDenial,
    ClassifiedFailure, ConnectionConsent, ConnectionProvider, ConnectionReauthRequired,
    ConnectionRecord, ConnectionRefresh, ConnectionRevocation, ConnectionStatus, CredentialHandle,
    CredentialUse, HandleClaims, HandleIssuance, OAuthProvider, SealedCredential, StoredConnection,
    TokenMaterial, SEALED_FORMAT_VERSION,
};
use rusty_agent_runtime::journal::{Clock, EventDraft, Journal};
use rusty_agent_runtime::record::{Effect, RunEventKind};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use tokio::sync::Mutex;

use chacha20poly1305::aead::{Aead, KeyInit, OsRng};
use chacha20poly1305::XChaCha20Poly1305;

use crate::server_store::{ConnectionUpdate, ServerStore, StoreResult};

/// The deterministic run id of the deployment's broker journal: the
/// chained record of every connection registration, consent, refresh,
/// revocation, issuance, use, and denial. Deployment-wide (the chain is
/// the control plane's; the tenant travels in each payload), distinct
/// from executor run ids by construction, and free of `/` so the
/// JSON-file layout keeps one file per journal — the
/// `RECEIPTS_JOURNAL_RUN_ID` rules.
pub(crate) const BROKER_JOURNAL_RUN_ID: &str = "credential-broker";

/// The default handle TTL: five minutes — "handles live for minutes"
/// (the design's open question 5 leaning), short enough that expiry is
/// routine, long enough that a run's burst of calls reuses one issuance.
pub(crate) const DEFAULT_HANDLE_TTL: Duration = Duration::from_secs(300);

/// The default OAuth refresh window (R0.11 wave 4): a token expiring
/// within this horizon (jittered, see [`refresh_window_for`]) is rotated
/// before it is served. Five minutes — the same order as the handle TTL,
/// so a resolution-path refresh is routine rather than exceptional.
pub(crate) const DEFAULT_REFRESH_WINDOW: Duration = Duration::from_secs(300);

type HmacSha256 = Hmac<Sha256>;

/// The master key id prefix; the id is the prefix plus 16 lowercase hex
/// chars (random, the `uuid` draw the receipt keys use).
const MASTER_KEY_ID_PREFIX: &str = "bmk-";

/// The domain-separation label deriving the handle-signing key from the
/// master key: one HMAC invocation, so a handle signature can never be
/// repurposed as an envelope operation (or vice versa) — the two keys
/// are independent by construction.
const HANDLE_KEY_DOMAIN: &[u8] = b"rusty-broker-handle-signing-v1";

// --------------------------------------------------------------------- //
// Master key custody (the receipts.rs secret-file discipline)
// --------------------------------------------------------------------- //

fn master_secret_path(root: &Path, key_id: &str) -> PathBuf {
    crate::receipts::keys_dir(root).join(format!("broker-master.{key_id}.secret"))
}

/// Write a freshly generated master key with `0600` permissions from the
/// first byte (`create_new` + mode at creation — there is no window
/// where the file is looser). Written exactly once: rotation mints a new
/// key id, never overwrites.
async fn write_master_secret(root: &Path, key_id: &str, key: &[u8; 32]) -> io::Result<()> {
    let dir = crate::receipts::keys_dir(root);
    tokio::fs::create_dir_all(&dir).await?;
    let path = master_secret_path(root, key_id);
    #[cfg(unix)]
    {
        let mut options = tokio::fs::OpenOptions::new();
        options.write(true).create_new(true).mode(0o600);
        use tokio::io::AsyncWriteExt as _;
        let mut file = options.open(&path).await?;
        file.write_all(hex_encode(key).as_bytes()).await?;
    }
    #[cfg(not(unix))]
    {
        tokio::fs::write(&path, hex_encode(key)).await?;
    }
    Ok(())
}

/// Every master key this host holds, keyed by key id. Files that fail to
/// parse are skipped with a warning (the corrupt-tolerance rule), as are
/// names outside the `broker-master.{id}.secret` shape.
/// The held master keys: `(key_id, key)` pairs, oldest first — the last
/// entry seals, every entry opens (the rotation seam).
type MasterKeys = Vec<(String, [u8; 32])>;

fn load_master_secrets(root: &Path) -> MasterKeys {
    let mut out = Vec::new();
    let Ok(entries) = std::fs::read_dir(crate::receipts::keys_dir(root)) else {
        return out;
    };
    for entry in entries.flatten() {
        let name = entry.file_name().to_string_lossy().into_owned();
        let Some(rest) = name.strip_prefix("broker-master.") else {
            continue;
        };
        let Some(key_id) = rest.strip_suffix(".secret") else {
            continue;
        };
        let parsed = std::fs::read_to_string(entry.path())
            .ok()
            .and_then(|hex| hex_decode(hex.trim()))
            .and_then(|bytes| <[u8; 32]>::try_from(bytes).ok());
        match parsed {
            Some(key) => out.push((key_id.to_owned(), key)),
            None => {
                tracing::warn!(path = %entry.path().display(), "skipping unreadable broker master key file")
            }
        }
    }
    out
}

// --------------------------------------------------------------------- //
// The envelope cryptography
// --------------------------------------------------------------------- //

/// Draw `N` bytes of OS entropy through `AeadCore::generate_nonce`-grade
/// randomness — `OsRng`, the same `getrandom` source `uuid` uses.
fn draw_random<const N: usize>() -> [u8; N] {
    let mut bytes = [0u8; N];
    use chacha20poly1305::aead::rand_core::RngCore as _;
    OsRng.fill_bytes(&mut bytes);
    bytes
}

/// Seal token material under a freshly minted per-connection data key —
/// the registration path. The connection id authenticates both seals
/// (associated data): a wrapped key or ciphertext transplanted to
/// another connection's envelope fails its tag.
fn seal_new(
    master_id: &str,
    master: &[u8; 32],
    connection_id: &str,
    material: &TokenMaterial,
) -> StoreResult<SealedCredential> {
    let data_key = draw_random::<32>();
    seal_with_data_key(master_id, master, connection_id, &data_key, None, material)
}

/// Re-seal under the *existing* envelope's data key — the consent path:
/// the wrapped key and its nonce carry over (the data key does not
/// change when the material does), and the payload nonce is always
/// fresh, so no (key, nonce) pair ever repeats.
fn reseal(
    master_id: &str,
    master: &[u8; 32],
    connection_id: &str,
    existing: &SealedCredential,
    material: &TokenMaterial,
) -> StoreResult<SealedCredential> {
    let data_key = unwrap_data_key(master, connection_id, existing)?;
    seal_with_data_key(
        master_id,
        master,
        connection_id,
        &data_key,
        Some(existing),
        material,
    )
}

fn seal_with_data_key(
    master_id: &str,
    master: &[u8; 32],
    connection_id: &str,
    data_key: &[u8; 32],
    existing: Option<&SealedCredential>,
    material: &TokenMaterial,
) -> StoreResult<SealedCredential> {
    let aad = connection_id.as_bytes();
    let (wrapped_data_key, wrap_nonce) = match existing {
        Some(envelope) => (
            envelope.wrapped_data_key.clone(),
            envelope.wrap_nonce.clone(),
        ),
        None => {
            let wrap_nonce_bytes = draw_random::<24>();
            let wrapped = XChaCha20Poly1305::new(master.into())
                .encrypt(
                    chacha20poly1305::XNonce::from_slice(&wrap_nonce_bytes),
                    chacha20poly1305::aead::Payload { msg: data_key, aad },
                )
                .map_err(|_| "wrap data key: AEAD failure".to_string())?;
            (hex_encode(&wrapped), hex_encode(&wrap_nonce_bytes))
        }
    };
    let plaintext =
        serde_json::to_vec(material).map_err(|e| format!("serialize token material: {e}"))?;
    let nonce_bytes = draw_random::<24>();
    let ciphertext = XChaCha20Poly1305::new(data_key.into())
        .encrypt(
            chacha20poly1305::XNonce::from_slice(&nonce_bytes),
            chacha20poly1305::aead::Payload {
                msg: &plaintext,
                aad,
            },
        )
        .map_err(|_| "seal token material: AEAD failure".to_string())?;
    Ok(SealedCredential {
        format_version: SEALED_FORMAT_VERSION,
        key_id: master_id.to_owned(),
        wrapped_data_key,
        wrap_nonce,
        nonce: hex_encode(&nonce_bytes),
        ciphertext: hex_encode(&ciphertext),
        sealed_at: Utc::now(),
    })
}

fn unwrap_data_key(
    master: &[u8; 32],
    connection_id: &str,
    envelope: &SealedCredential,
) -> StoreResult<[u8; 32]> {
    let wrapped = hex_decode(&envelope.wrapped_data_key)
        .ok_or_else(|| "corrupt envelope: wrapped key is not hex".to_string())?;
    let nonce = hex_decode(&envelope.wrap_nonce)
        .ok_or_else(|| "corrupt envelope: wrap nonce is not hex".to_string())?;
    if nonce.len() != 24 {
        return Err("corrupt envelope: wrap nonce is not 24 bytes".to_string());
    }
    let data_key = XChaCha20Poly1305::new(master.into())
        .decrypt(
            chacha20poly1305::XNonce::from_slice(&nonce),
            chacha20poly1305::aead::Payload {
                msg: &wrapped,
                aad: connection_id.as_bytes(),
            },
        )
        .map_err(|_| "the wrapped data key failed its authentication tag".to_string())?;
    <[u8; 32]>::try_from(data_key)
        .map_err(|_| "corrupt envelope: data key is not 32 bytes".to_string())
}

/// Open an envelope: unwrap the data key, decrypt, parse. Any failure —
/// wrong master key, tampered ciphertext, transplanted envelope, corrupt
/// hex — is the same refusal: the envelope does not open, and nothing
/// about *why* leaks to the caller (the unknown-handle posture).
fn open(
    master: &[u8; 32],
    connection_id: &str,
    envelope: &SealedCredential,
) -> StoreResult<TokenMaterial> {
    if envelope.format_version != SEALED_FORMAT_VERSION {
        return Err(format!(
            "envelope format version {} is not {SEALED_FORMAT_VERSION}",
            envelope.format_version
        ));
    }
    let data_key = unwrap_data_key(master, connection_id, envelope)?;
    let ciphertext = hex_decode(&envelope.ciphertext)
        .ok_or_else(|| "corrupt envelope: ciphertext is not hex".to_string())?;
    let nonce = hex_decode(&envelope.nonce)
        .ok_or_else(|| "corrupt envelope: nonce is not hex".to_string())?;
    if nonce.len() != 24 {
        return Err("corrupt envelope: nonce is not 24 bytes".to_string());
    }
    let plaintext = XChaCha20Poly1305::new((&data_key).into())
        .decrypt(
            chacha20poly1305::XNonce::from_slice(&nonce),
            chacha20poly1305::aead::Payload {
                msg: &ciphertext,
                aad: connection_id.as_bytes(),
            },
        )
        .map_err(|_| "the sealed credential failed its authentication tag".to_string())?;
    serde_json::from_slice(&plaintext).map_err(|e| format!("corrupt token material: {e}"))
}

// --------------------------------------------------------------------- //
// Handle signing
// --------------------------------------------------------------------- //

/// The handle-signing key, derived from a master key by one HMAC under
/// the domain label. No HKDF dependency: the input key is already
/// uniform 32 bytes, so a single HMAC extraction is the whole derivation
/// (the `uuid`-draws-entropy posture — reach for the primitive the
/// moment actually needs).
fn handle_signing_key(master: &[u8; 32]) -> [u8; 32] {
    let mut mac = <HmacSha256 as Mac>::new_from_slice(master).expect("HMAC takes any key length");
    mac.update(HANDLE_KEY_DOMAIN);
    mac.finalize().into_bytes().into()
}

fn sign_claims(signing_key: &[u8; 32], claims: &HandleClaims) -> StoreResult<String> {
    let message =
        serde_json::to_vec(claims).map_err(|e| format!("serialize handle claims: {e}"))?;
    let mut mac =
        <HmacSha256 as Mac>::new_from_slice(signing_key).expect("HMAC takes any key length");
    mac.update(&message);
    Ok(hex_encode(&mac.finalize().into_bytes()))
}

/// Verify a presented signature over the claims — recompute and compare
/// through the MAC's own constant-time check, never `==` on hex.
fn verify_claims(
    signing_key: &[u8; 32],
    claims: &HandleClaims,
    signature_hex: &str,
) -> StoreResult<bool> {
    let message =
        serde_json::to_vec(claims).map_err(|e| format!("serialize handle claims: {e}"))?;
    let Some(presented) = hex_decode(signature_hex) else {
        return Ok(false);
    };
    let mut mac =
        <HmacSha256 as Mac>::new_from_slice(signing_key).expect("HMAC takes any key length");
    mac.update(&message);
    Ok(mac.verify_slice(&presented).is_ok())
}

// --------------------------------------------------------------------- //
// The JSON-file layout (the registry.rs hashed-filename rule)
// --------------------------------------------------------------------- //

/// The connections directory under the store root
/// (`{store_path}/connections`; `connections` is a reserved layout name,
/// see [`crate::RESERVED_NAMES`]).
pub(crate) fn connections_dir(root: &Path) -> PathBuf {
    root.join("connections")
}

/// The connection file's body: the stored record plus the scoped key it
/// was written under. The filename is the key's hash — scoped ids
/// contain `/` for named tenants, and a one-way filename needs the true
/// key recorded somewhere (the version-pointer envelope's rule,
/// verbatim).
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ConnectionFile {
    /// The tenant-scoped connection id (`{tenant}/conn-…` for named
    /// tenants).
    key: String,
    /// The stored connection (record + sealed credential).
    record: StoredConnection,
}

fn connection_file_name(scoped_id: &str) -> String {
    rusty_agent_runtime::record::sha256_hex(scoped_id.as_bytes())
}

/// Persist one stored connection atomically (temp file + rename) — the
/// durability discipline every file record in the server shares.
pub(crate) async fn persist_connection(
    root: &Path,
    scoped_id: &str,
    record: &StoredConnection,
) -> io::Result<()> {
    let dir = connections_dir(root);
    tokio::fs::create_dir_all(&dir).await?;
    let file = ConnectionFile {
        key: scoped_id.to_string(),
        record: record.clone(),
    };
    let bytes = serde_json::to_vec_pretty(&file)
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
    let name = connection_file_name(scoped_id);
    let tmp = dir.join(format!("{name}.tmp"));
    tokio::fs::write(&tmp, bytes).await?;
    tokio::fs::rename(&tmp, dir.join(format!("{name}.json"))).await
}

/// Remove a connection's file; `false` when it was not there.
pub(crate) async fn delete_connection_file(root: &Path, scoped_id: &str) -> io::Result<bool> {
    let path = connections_dir(root).join(format!("{}.json", connection_file_name(scoped_id)));
    match tokio::fs::remove_file(&path).await {
        Ok(()) => Ok(true),
        Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(false),
        Err(e) => Err(e),
    }
}

/// Load all stored connections, keyed by the scoped id carried in each
/// file's envelope. A file whose envelope key does not hash back to its
/// filename is corrupt (or a collision) and is skipped with a warning —
/// the fail-closed loader rule every hashed-filename layout here shares.
pub(crate) fn load_connections(root: &Path) -> std::collections::HashMap<String, StoredConnection> {
    let dir = connections_dir(root);
    let mut out = std::collections::HashMap::new();
    let Ok(entries) = std::fs::read_dir(&dir) else {
        return out;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("json") {
            continue;
        }
        let parsed = std::fs::read_to_string(&path)
            .ok()
            .and_then(|raw| serde_json::from_str::<ConnectionFile>(&raw).ok());
        let matches_name = parsed.as_ref().is_some_and(|file| {
            path.file_stem().and_then(|s| s.to_str()) == Some(&*connection_file_name(&file.key))
        });
        match (parsed, matches_name) {
            (Some(file), true) => {
                out.insert(file.key, file.record);
            }
            _ => tracing::warn!(path = %path.display(), "skipping unreadable connection file"),
        }
    }
    out
}

// --------------------------------------------------------------------- //
// The broker
// --------------------------------------------------------------------- //

/// The outcome of [`Broker::record_consent`].
#[derive(Debug)]
pub(crate) enum ConsentOutcome {
    /// The consent (or refresh) applied; the journaled event kind says
    /// which it was.
    Applied {
        /// The updated record.
        record: ConnectionRecord,
        /// The journaled kind (`connection_consented` when the scope set
        /// changed, `connection_refreshed` when only the material did).
        journaled: RunEventKind,
    },
    /// Nothing changed — same scopes, no new material (the idempotent
    /// create's rule: re-recording the same fact converges).
    Converged(ConnectionRecord),
    /// No such connection in this tenant.
    Unknown,
    /// A concurrent mutation won the read-modify-write race.
    Conflict,
}

/// The outcome of [`Broker::revoke`].
#[derive(Debug)]
pub(crate) enum RevokeOutcome {
    /// The revocation applied and journaled.
    Applied {
        /// The revoked record.
        record: ConnectionRecord,
        /// The journaled event's id.
        event_id: String,
    },
    /// Already revoked — re-revocation converges without a second event
    /// (the fact already journaled once).
    Converged(ConnectionRecord),
    /// No such connection in this tenant.
    Unknown,
    /// A concurrent mutation won the race.
    Conflict,
}

/// The deployment's credential broker. Cheap to share (`Arc`); the
/// master key resolves lazily on first use and is held thereafter, and
/// one mutex serializes appends to the deployment journal so concurrent
/// resolutions cannot clobber each other's freshly journaled events
/// (whole-snapshot persistence — the `journal_locks` reasoning, applied
/// to a journal this module owns).
pub(crate) struct Broker {
    store: Arc<dyn ServerStore>,
    store_path: PathBuf,
    masters: Mutex<Option<MasterKeys>>,
    handle_ttl: Duration,
    /// The OAuth lifecycle (R0.11 wave 4): the deployment's provider and
    /// the refresh window. Absent means no lifecycle — OAuth connections
    /// register and serve static material exactly as static kinds do, and
    /// the exchange endpoints answer 409 (the evaluator-absent
    /// precedent).
    oauth: Option<(Arc<dyn OAuthProvider>, Duration)>,
    journal_lock: Mutex<()>,
}

impl std::fmt::Debug for Broker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Broker")
            .field("store_path", &self.store_path)
            .field("handle_ttl", &self.handle_ttl)
            .field(
                "oauth",
                &self
                    .oauth
                    .as_ref()
                    .map(|(provider, window)| format!("{provider:?} (window {window:?})")),
            )
            .finish()
    }
}

impl Broker {
    /// A broker over `store` with master keys under `{store_path}/keys/`
    /// and handle TTL `handle_ttl`.
    pub(crate) fn new(
        store: Arc<dyn ServerStore>,
        store_path: PathBuf,
        handle_ttl: Duration,
    ) -> Self {
        Self {
            store,
            store_path,
            masters: Mutex::new(None),
            handle_ttl,
            oauth: None,
            journal_lock: Mutex::new(()),
        }
    }

    /// Builder-style: plug the OAuth provider and the refresh window
    /// (R0.11 wave 4). Resolution-time refresh, the sweeper, and the
    /// authorization-code exchange endpoints all read this pair.
    pub(crate) fn with_oauth_provider(
        mut self,
        provider: Arc<dyn OAuthProvider>,
        refresh_window: Duration,
    ) -> Self {
        self.oauth = Some((provider, refresh_window));
        self
    }

    /// The OAuth pair, when the deployment plugged one.
    pub(crate) fn oauth(&self) -> Option<&(Arc<dyn OAuthProvider>, Duration)> {
        self.oauth.as_ref()
    }

    /// The master keys, ensuring at least one exists: serve the cached
    /// set, else load the local secrets, else generate the genesis key
    /// (written `0600`, never through the store abstraction). The active
    /// key for *sealing* is the last entry; every loaded key stays
    /// available for *opening*, so envelopes wrapped under an older key
    /// keep resolving (the rotation seam).
    async fn masters(&self) -> StoreResult<MasterKeys> {
        let mut guard = self.masters.lock().await;
        if let Some(masters) = guard.as_ref() {
            return Ok(masters.clone());
        }
        let mut masters = load_master_secrets(&self.store_path);
        if masters.is_empty() {
            let key_id = format!(
                "{}{}",
                MASTER_KEY_ID_PREFIX,
                &uuid::Uuid::new_v4().simple().to_string()[..16]
            );
            let key = draw_random::<32>();
            write_master_secret(&self.store_path, &key_id, &key)
                .await
                .map_err(|e| format!("write broker master key: {e}"))?;
            masters.push((key_id, key));
        }
        *guard = Some(masters.clone());
        Ok(masters)
    }

    /// The active master key (for sealing and signing).
    async fn active_master(&self) -> StoreResult<(String, [u8; 32])> {
        let masters = self.masters().await?;
        masters
            .last()
            .cloned()
            .ok_or_else(|| "no broker master key".to_string())
    }

    /// The master key an envelope names (for opening).
    async fn master_for(&self, key_id: &str) -> StoreResult<[u8; 32]> {
        self.masters()
            .await?
            .into_iter()
            .find(|(id, _)| id == key_id)
            .map(|(_, key)| key)
            .ok_or_else(|| format!("master key `{key_id}` is not held by this host"))
    }

    /// Append one event to the deployment's broker journal, persisting
    /// the grown snapshot — serialized by `journal_lock`, integrity
    /// re-verified on load (a tampered broker journal fails the append
    /// rather than silently forking the chain). Hard-fail by contract:
    /// callers treat an `Err` here as "the mutation did not happen".
    async fn journal(&self, draft: EventDraft) -> StoreResult<String> {
        let _guard = self.journal_lock.lock().await;
        let journal = match self.store.get_journal(BROKER_JOURNAL_RUN_ID).await? {
            Some(snapshot) => Journal::from_snapshot(snapshot, Clock::System)
                .map_err(|e| format!("the broker journal failed its integrity check: {e}"))?,
            None => Journal::new(BROKER_JOURNAL_RUN_ID, BROKER_JOURNAL_RUN_ID, Clock::System),
        };
        let event_id = journal.record(draft);
        self.store.put_journal(&journal.snapshot()).await?;
        Ok(event_id)
    }

    /// Journal a denial (best-effort: the denial stands either way — it
    /// is already the fail-closed answer — but the evidence should not
    /// be lost without a trace).
    async fn journal_denial(&self, denial: &BrokerDenial) {
        let Ok(output) = serde_json::to_value(denial) else {
            return;
        };
        if let Err(e) = self
            .journal(EventDraft::new(RunEventKind::CredentialDenied, Effect::Pure).output(output))
            .await
        {
            tracing::warn!(%e, "broker denial could not be journaled");
        }
    }

    /// Register a connection: validate, seal the material under a fresh
    /// data key, journal, then persist — nothing reaches the store the
    /// journal did not record first.
    pub(crate) async fn register(
        &self,
        tenant: &str,
        provider: ConnectionProvider,
        subject: Option<String>,
        scopes: std::collections::BTreeSet<String>,
        material: &TokenMaterial,
    ) -> StoreResult<ConnectionRecord> {
        let now = Utc::now();
        let record = ConnectionRecord {
            connection_id: new_connection_id(),
            provider,
            subject,
            scopes,
            status: ConnectionStatus::Active,
            health: Default::default(),
            created_at: now,
            updated_at: now,
        };
        record.validate().map_err(|e| e.to_string())?;
        let (key_id, master) = self.active_master().await?;
        let credential = seal_new(&key_id, &master, &record.connection_id, material)?;
        self.journal(
            EventDraft::new(RunEventKind::ConnectionRegistered, Effect::Pure)
                .output(serde_json::to_value(&record).map_err(|e| e.to_string())?),
        )
        .await?;
        self.store
            .put_connection(
                tenant,
                &StoredConnection {
                    record: record.clone(),
                    credential,
                },
            )
            .await?;
        Ok(record)
    }

    /// Record a consent act: a new scope ceiling and/or fresh token
    /// material. The human's grant is the approval — executed at the
    /// provider, recorded here, journaled — and this is the *only* way a
    /// consented set changes. A consent also re-activates: it is the
    /// re-auth path out of `needs_reauth`. Scope-set changes journal
    /// `connection_consented`; material-only updates journal
    /// `connection_refreshed`.
    pub(crate) async fn record_consent(
        &self,
        tenant: &str,
        connection_id: &str,
        scopes: Option<std::collections::BTreeSet<String>>,
        material: Option<&TokenMaterial>,
    ) -> StoreResult<ConsentOutcome> {
        let Some(stored) = self.store.get_connection(tenant, connection_id).await? else {
            return Ok(ConsentOutcome::Unknown);
        };
        let scopes_changed = scopes.as_ref().is_some_and(|s| *s != stored.record.scopes);
        if !scopes_changed && material.is_none() {
            return Ok(ConsentOutcome::Converged(stored.record));
        }
        let now = Utc::now();
        let mut record = stored.record.clone();
        if let Some(scopes) = scopes {
            record.scopes = scopes;
        }
        record.status = ConnectionStatus::Active;
        record.health.consecutive_failures = 0;
        record.updated_at = now;
        record.validate().map_err(|e| e.to_string())?;
        let credential = match material {
            Some(material) => {
                record.health.last_refresh_at = Some(now);
                let (key_id, master) = self.active_master().await?;
                reseal(
                    &key_id,
                    &master,
                    connection_id,
                    &stored.credential,
                    material,
                )?
            }
            None => stored.credential.clone(),
        };
        let (kind, output) = if scopes_changed {
            (
                RunEventKind::ConnectionConsented,
                serde_json::to_value(ConnectionConsent {
                    connection_id: connection_id.to_owned(),
                    subject: record.subject.clone(),
                    scopes: record.scopes.clone(),
                    recorded_at: now,
                })
                .map_err(|e| e.to_string())?,
            )
        } else {
            (
                RunEventKind::ConnectionRefreshed,
                serde_json::to_value(ConnectionRefresh {
                    connection_id: connection_id.to_owned(),
                    refreshed_at: now,
                    expires_at: material.and_then(|m| m.expires_at),
                })
                .map_err(|e| e.to_string())?,
            )
        };
        // Evidence first: the journaled act, then the state change. On a
        // lost CAS race the event records an intent that never applied —
        // the caller's retry journals anew; the answer is `Conflict`,
        // never a silent divergence.
        self.journal(EventDraft::new(kind, Effect::Pure).output(output))
            .await?;
        let updated = StoredConnection {
            record: record.clone(),
            credential,
        };
        match self
            .store
            .update_connection(tenant, connection_id, &stored, &updated)
            .await?
        {
            ConnectionUpdate::Applied => Ok(ConsentOutcome::Applied {
                record,
                journaled: kind,
            }),
            ConnectionUpdate::Unknown => Ok(ConsentOutcome::Unknown),
            ConnectionUpdate::Conflict => Ok(ConsentOutcome::Conflict),
        }
    }

    /// Revoke: the status flip and its journaled event commit together
    /// (event first), and outstanding handles fail at their next use —
    /// resolution reads live state, so revocation takes effect at the
    /// next call, not the next deploy.
    pub(crate) async fn revoke(
        &self,
        tenant: &str,
        connection_id: &str,
        reason: Option<String>,
    ) -> StoreResult<RevokeOutcome> {
        let Some(stored) = self.store.get_connection(tenant, connection_id).await? else {
            return Ok(RevokeOutcome::Unknown);
        };
        if stored.record.status == ConnectionStatus::Revoked {
            return Ok(RevokeOutcome::Converged(stored.record));
        }
        let now = Utc::now();
        let mut record = stored.record.clone();
        record.status = ConnectionStatus::Revoked;
        record.updated_at = now;
        let event_id = self
            .journal(
                EventDraft::new(RunEventKind::ConnectionRevoked, Effect::Pure).output(
                    serde_json::to_value(ConnectionRevocation {
                        connection_id: connection_id.to_owned(),
                        grant: record.scopes.clone(),
                        revoked_at: now,
                        reason,
                    })
                    .map_err(|e| e.to_string())?,
                ),
            )
            .await?;
        let updated = StoredConnection {
            record: record.clone(),
            credential: stored.credential.clone(),
        };
        match self
            .store
            .update_connection(tenant, connection_id, &stored, &updated)
            .await?
        {
            ConnectionUpdate::Applied => Ok(RevokeOutcome::Applied { record, event_id }),
            ConnectionUpdate::Unknown => Ok(RevokeOutcome::Unknown),
            ConnectionUpdate::Conflict => Ok(RevokeOutcome::Conflict),
        }
    }

    /// Delete a connection: revoke first when still live (the evidence
    /// trail — a deleted connection's grant stopped holding *here*),
    /// then erase the stored record, sealed material included (the
    /// memory-forget posture: real deletion of derived state). A deleted
    /// connection fails closed exactly like a revoked one — resolution
    /// answers `unknown_connection`.
    pub(crate) async fn delete(&self, tenant: &str, connection_id: &str) -> StoreResult<bool> {
        let Some(stored) = self.store.get_connection(tenant, connection_id).await? else {
            return Ok(false);
        };
        if stored.record.status != ConnectionStatus::Revoked {
            match self
                .revoke(tenant, connection_id, Some("connection deleted".to_owned()))
                .await?
            {
                RevokeOutcome::Applied { .. } | RevokeOutcome::Converged(_) => {}
                RevokeOutcome::Unknown => return Ok(false),
                RevokeOutcome::Conflict => {
                    return Err("connection changed under deletion; retry".to_string())
                }
            }
        }
        self.store.delete_connection(tenant, connection_id).await
    }

    /// Read one connection's public record (never the sealed material —
    /// the read paths serve metadata; the bytes move only at resolution).
    pub(crate) async fn get(
        &self,
        tenant: &str,
        connection_id: &str,
    ) -> StoreResult<Option<ConnectionRecord>> {
        Ok(self
            .store
            .get_connection(tenant, connection_id)
            .await?
            .map(|stored| stored.record))
    }

    /// Every connection record the tenant holds (order unspecified;
    /// callers sort).
    pub(crate) async fn list(&self, tenant: &str) -> StoreResult<Vec<ConnectionRecord>> {
        Ok(self
            .store
            .list_connections(tenant)
            .await?
            .into_iter()
            .map(|stored| stored.record)
            .collect())
    }

    /// Issue a handle: check the connection's live state and the consent
    /// ceiling, mint and sign the claims, journal the issuance —
    /// hard-fail, so a handle that exists was always journaled.
    pub(crate) async fn issue(
        &self,
        request: &rusty_agent_runtime::broker::IssueRequest,
    ) -> Result<CredentialHandle, BrokerDenial> {
        let stored = self
            .store
            .get_connection(&request.tenant, &request.requirement.connection_id)
            .await
            .map_err(|e| BrokerDenial::unavailable(format!("the connection read failed: {e}")))?;
        let Some(stored) = stored else {
            let denial = BrokerDenial {
                connection_id: Some(request.requirement.connection_id.clone()),
                handle_id: None,
                tenant: Some(request.tenant.clone()),
                reason: rusty_agent_runtime::broker::BrokerDenialReason::UnknownConnection,
                detail: format!(
                    "connection `{}` is unknown to tenant `{}`",
                    request.requirement.connection_id, request.tenant
                ),
            };
            self.journal_denial(&denial).await;
            return Err(denial);
        };
        let record = &stored.record;
        let state_denial = match record.status {
            ConnectionStatus::Active => None,
            ConnectionStatus::Revoked => Some(BrokerDenial::connection_revoked(
                record,
                &request.tenant,
                "unissued",
            )),
            ConnectionStatus::NeedsReauth => Some(BrokerDenial::connection_needs_reauth(
                record,
                &request.tenant,
                "unissued",
            )),
        };
        if let Some(denial) = state_denial {
            self.journal_denial(&denial).await;
            return Err(denial);
        }
        // The narrowing rule: an empty request asks for the whole consent
        // set; anything else must be covered by it. Beyond the ceiling is
        // a denial *here*, never at the provider.
        let narrowed = if request.requirement.scopes.is_empty() {
            record.scopes.clone()
        } else {
            let missing = scopes_missing(&record.scopes, &request.requirement.scopes);
            if !missing.is_empty() {
                let denial = BrokerDenial::scope_not_granted(
                    Some((&record.connection_id, None, &request.tenant)),
                    missing,
                    format!(
                        "connection `{}` consented to {}; the request asks beyond that ceiling",
                        record.connection_id,
                        serde_json::to_string(&record.scopes).unwrap_or_default(),
                    ),
                );
                self.journal_denial(&denial).await;
                return Err(denial);
            }
            request.requirement.scopes.clone()
        };
        let now = Utc::now();
        let claims = HandleClaims {
            handle_id: new_handle_id(),
            connection_id: record.connection_id.clone(),
            tenant: request.tenant.clone(),
            run_id: request.run_id.clone(),
            scopes: narrowed,
            issued_at: now,
            expires_at: now
                + chrono::Duration::from_std(self.handle_ttl)
                    .map_err(|e| BrokerDenial::unavailable(format!("invalid handle TTL: {e}")))?,
        };
        let (_, master) = self.active_master().await.map_err(|e| {
            BrokerDenial::unavailable(format!("the master key is unavailable: {e}"))
        })?;
        let signature = sign_claims(&handle_signing_key(&master), &claims)
            .map_err(BrokerDenial::unavailable)?;
        self.journal(
            EventDraft::new(RunEventKind::CredentialHandleIssued, Effect::Pure).output(
                serde_json::to_value(HandleIssuance {
                    claims: claims.clone(),
                })
                .map_err(|e| BrokerDenial::unavailable(e.to_string()))?,
            ),
        )
        .await
        .map_err(|e| {
            BrokerDenial::unavailable(format!("the issuance could not be journaled: {e}"))
        })?;
        Ok(CredentialHandle::from_parts(claims, signature))
    }

    /// Resolve a handle at use: verify, check expiry and scope coverage
    /// from the self-contained claims, read the connection's *live*
    /// state, open the envelope for the connector, and journal the use —
    /// hard-fail, so a resolution that happened was always journaled.
    /// Every denial is journaled too.
    pub(crate) async fn resolve(
        &self,
        token: &str,
        scopes: &std::collections::BTreeSet<String>,
    ) -> Result<rusty_agent_runtime::broker::ResolvedCredential, BrokerDenial> {
        let (claims, signature) = match CredentialHandle::parse_token(token) {
            Ok(parts) => parts,
            Err(denial) => {
                self.journal_denial(&denial).await;
                return Err(denial);
            }
        };
        let (_, master) = self.active_master().await.map_err(|e| {
            BrokerDenial::unavailable(format!("the master key is unavailable: {e}"))
        })?;
        let signing_key = handle_signing_key(&master);
        match verify_claims(&signing_key, &claims, &signature) {
            Ok(true) => {}
            Ok(false) => {
                let denial =
                    BrokerDenial::unknown_handle("the handle signature did not verify".to_owned());
                self.journal_denial(&denial).await;
                return Err(denial);
            }
            Err(e) => return Err(BrokerDenial::unavailable(e)),
        }
        if claims.is_expired(Utc::now()) {
            let denial = BrokerDenial::handle_expired(&claims);
            self.journal_denial(&denial).await;
            return Err(denial);
        }
        let missing = scopes_missing(&claims.scopes, scopes);
        if !missing.is_empty() {
            let denial = BrokerDenial::scope_not_granted(
                Some((
                    &claims.connection_id,
                    Some(&claims.handle_id),
                    &claims.tenant,
                )),
                missing,
                "the call asked beyond the handle's narrowed set".to_owned(),
            );
            self.journal_denial(&denial).await;
            return Err(denial);
        }
        let stored = self
            .store
            .get_connection(&claims.tenant, &claims.connection_id)
            .await
            .map_err(|e| BrokerDenial::unavailable(format!("the connection read failed: {e}")))?;
        let Some(stored) = stored else {
            let denial = BrokerDenial::unknown_connection(&claims);
            self.journal_denial(&denial).await;
            return Err(denial);
        };
        match stored.record.status {
            ConnectionStatus::Active => {}
            ConnectionStatus::Revoked => {
                let denial = BrokerDenial::connection_revoked(
                    &stored.record,
                    &claims.tenant,
                    &claims.handle_id,
                );
                self.journal_denial(&denial).await;
                return Err(denial);
            }
            ConnectionStatus::NeedsReauth => {
                let denial = BrokerDenial::connection_needs_reauth(
                    &stored.record,
                    &claims.tenant,
                    &claims.handle_id,
                );
                self.journal_denial(&denial).await;
                return Err(denial);
            }
        }
        let master = self
            .master_for(&stored.credential.key_id)
            .await
            .map_err(BrokerDenial::unavailable)?;
        let material = open(&master, &claims.connection_id, &stored.credential)
            .map_err(|e| BrokerDenial::unavailable(format!("the envelope did not open: {e}")))?;
        // The OAuth lifecycle (R0.11 wave 4): a token inside its jittered
        // refresh window rotates before it is served, so the bytes that
        // reach the connector are never the ones about to die. No
        // lifecycle configured, a static provider kind, or a token
        // outside its window returns the opened material unchanged.
        let material = match self
            .refresh_at_resolution(&claims.tenant, &claims.handle_id, &stored, material)
            .await
        {
            Ok(material) => material,
            Err(denial) => {
                self.journal_denial(&denial).await;
                return Err(denial);
            }
        };
        self.journal(
            EventDraft::new(RunEventKind::CredentialUse, Effect::ReadOnly).output(
                serde_json::to_value(CredentialUse {
                    handle_id: claims.handle_id.clone(),
                    connection_id: claims.connection_id.clone(),
                    tenant: claims.tenant.clone(),
                    run_id: claims.run_id.clone(),
                    scopes_checked: scopes.clone(),
                    used_at: Utc::now(),
                })
                .map_err(|e| BrokerDenial::unavailable(e.to_string()))?,
            ),
        )
        .await
        .map_err(|e| BrokerDenial::unavailable(format!("the use could not be journaled: {e}")))?;
        Ok(rusty_agent_runtime::broker::ResolvedCredential {
            connection_id: claims.connection_id,
            handle_id: claims.handle_id,
            scopes: scopes.clone(),
            material,
        })
    }
}

/// The core seam, implemented so `CredentialMediator` /
/// `BrokeredCapsuleHost` mediate against the real broker in-process
/// (the A2A capsule path and embedders building on the server crate).
#[async_trait::async_trait]
impl rusty_agent_runtime::broker::CredentialBroker for Broker {
    async fn issue(
        &self,
        request: &rusty_agent_runtime::broker::IssueRequest,
    ) -> Result<CredentialHandle, BrokerDenial> {
        Broker::issue(self, request).await
    }

    async fn resolve(
        &self,
        token: &str,
        scopes: &std::collections::BTreeSet<String>,
    ) -> Result<rusty_agent_runtime::broker::ResolvedCredential, BrokerDenial> {
        Broker::resolve(self, token, scopes).await
    }
}

// --------------------------------------------------------------------- //
// The OAuth lifecycle (R0.11 Extension Plane, wave 4)
// --------------------------------------------------------------------- //

/// The per-connection refresh horizon: `window` scaled by a deterministic
/// jitter in [0.5, 1.0) drawn from the connection id's hash. Deterministic
/// — no RNG at resolution, so the same connection always refreshes at the
/// same horizon and a recorded run's refresh cadence reproduces — and
/// spread across connections, so a fleet registered together does not
/// stampede the provider together.
fn refresh_window_for(connection_id: &str, window: Duration) -> chrono::Duration {
    let digest = Sha256::digest(connection_id.as_bytes());
    let draw = u64::from_be_bytes(digest[..8].try_into().expect("a sha256 has 8 bytes"));
    let fraction = 0.5 + 0.5 * (draw as f64 / u64::MAX as f64);
    chrono::Duration::from_std(window.mul_f64(fraction)).unwrap_or(chrono::Duration::MAX)
}

/// What one refresh attempt produced. `resolve` serves the
/// material-carrying variants and maps the rest to fail-closed denials;
/// [`Broker::sweep_once`] counts them.
enum RefreshAttempt {
    /// Rotation applied, journaled, persisted: serve the new material.
    Rotated(TokenMaterial),
    /// The provider failed transiently; the health counters moved
    /// (bookkeeping — transitions journal, counters don't) and the token
    /// is still valid: serve it.
    TransientServe(TokenMaterial),
    /// The provider failed transiently and the token has expired:
    /// nothing servable remains.
    TransientExpired,
    /// The token expired and the connection has no refresh material (no
    /// refresh token, no client secret): nothing servable remains.
    ExpiredUnservable,
    /// The provider refused terminally: the connection flipped to
    /// `needs_reauth` (journaled `connection_needs_reauth`) and every
    /// use fails closed until a human records a new consent act.
    NeedsReauth,
}

/// What one sweeper pass observed (R0.11 wave 4).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct SweepReport {
    /// OAuth connections in `active` status the pass examined.
    pub scanned: usize,
    /// Connections whose material rotated and persisted.
    pub refreshed: usize,
    /// Connections that flipped to `needs_reauth` (journaled).
    pub needs_reauth: usize,
    /// Connections the pass could not refresh (transient provider
    /// failures, expired tokens with no refresh path, envelopes that
    /// would not open): retried next pass, or at resolution — whichever
    /// comes first.
    pub failed: usize,
}

impl Broker {
    /// Run the refresh lifecycle for one connection whose envelope just
    /// opened: `None` when nothing is due (serve the opened material),
    /// else the attempt's outcome. `now` is the caller's clock reading —
    /// resolution passes `Utc::now()`; the sweeper reads its own once so
    /// one pass judges every connection against the same instant.
    ///
    /// The lifecycle is deliberately *not* retried here: one attempt per
    /// resolution (or sweep pass), then the outcome stands — a stale
    /// credential retried looks exactly like an attack retried, and the
    /// next resolution is the retry.
    async fn refresh_if_due(
        &self,
        tenant: &str,
        stored: &StoredConnection,
        material: TokenMaterial,
        now: chrono::DateTime<Utc>,
    ) -> StoreResult<Option<RefreshAttempt>> {
        let Some((provider, window)) = &self.oauth else {
            return Ok(None);
        };
        let record = &stored.record;
        if !record.provider.is_oauth() {
            return Ok(None);
        }
        // A token with no declared expiry is never due — the provider
        // said nothing, so there is no horizon to preempt.
        let Some(expires_at) = material.expires_at else {
            return Ok(None);
        };
        if expires_at > now + refresh_window_for(&record.connection_id, *window) {
            return Ok(None);
        }
        // The two OAuth flows refresh on different material: an
        // authorization-code connection presents its refresh token, a
        // client-credentials connection re-presents its client secret.
        let has_refresh_path = match record.provider {
            ConnectionProvider::Oauth2AuthorizationCode => material.refresh_token.is_some(),
            ConnectionProvider::Oauth2ClientCredentials => material.client_secret.is_some(),
            _ => unreachable!("the is_oauth gate passed"),
        };
        if !has_refresh_path {
            return Ok(if expires_at > now {
                // Due but not yet dead, and nothing to refresh with:
                // serve while valid — the sweeper (or a consent act) has
                // until expiry.
                None
            } else {
                Some(RefreshAttempt::ExpiredUnservable)
            });
        }
        match provider.refresh(record, &material).await {
            Ok(grant) => {
                let rotated = TokenMaterial {
                    access_token: grant.access_token,
                    // Providers that rotate refresh tokens return a new
                    // one; providers that don't return `None` and the
                    // presented one carries over. The client secret is
                    // custody, not a grant output — it persists.
                    refresh_token: grant
                        .refresh_token
                        .or_else(|| material.refresh_token.clone()),
                    client_secret: material.client_secret.clone(),
                    expires_at: grant.expires_at,
                };
                let (key_id, master) = self.active_master().await?;
                let credential = reseal(
                    &key_id,
                    &master,
                    &record.connection_id,
                    &stored.credential,
                    &rotated,
                )?;
                let mut updated_record = record.clone();
                updated_record.health.last_refresh_at = Some(now);
                updated_record.health.consecutive_failures = 0;
                updated_record.updated_at = now;
                // Evidence first, the module's rule: the journaled
                // refresh, then the state change.
                self.journal(
                    EventDraft::new(RunEventKind::ConnectionRefreshed, Effect::Pure).output(
                        serde_json::to_value(ConnectionRefresh {
                            connection_id: record.connection_id.clone(),
                            refreshed_at: now,
                            expires_at: rotated.expires_at,
                        })
                        .map_err(|e| e.to_string())?,
                    ),
                )
                .await?;
                let updated = StoredConnection {
                    record: updated_record,
                    credential,
                };
                match self
                    .store
                    .update_connection(tenant, &record.connection_id, stored, &updated)
                    .await?
                {
                    ConnectionUpdate::Applied => Ok(Some(RefreshAttempt::Rotated(rotated))),
                    // A concurrent mutation won (a second refresh, or a
                    // consent): re-read once and serve the winner's
                    // material — it is at least as fresh as what we
                    // minted. The row vanishing mid-refresh means a
                    // delete raced us; serve what we minted — liveness
                    // was checked before the envelope opened, and the
                    // next resolution re-reads.
                    ConnectionUpdate::Conflict | ConnectionUpdate::Unknown => {
                        let fresh = self
                            .store
                            .get_connection(tenant, &record.connection_id)
                            .await?;
                        match fresh {
                            Some(fresh) => {
                                let master = self.master_for(&fresh.credential.key_id).await?;
                                let material =
                                    open(&master, &record.connection_id, &fresh.credential)?;
                                Ok(Some(RefreshAttempt::Rotated(material)))
                            }
                            None => Ok(Some(RefreshAttempt::Rotated(rotated))),
                        }
                    }
                }
            }
            Err(failure) if failure.permanent => {
                let classified = ClassifiedFailure {
                    class: failure.class,
                    detail: failure.detail,
                    at: now,
                };
                // The terminal refusal is a *transition*: journaled
                // before the state change (evidence first). On a lost
                // CAS race the event records the refusal the provider
                // gave — which stands either way; the winner's row
                // governs the next call.
                self.journal(
                    EventDraft::new(RunEventKind::ConnectionNeedsReauth, Effect::Pure).output(
                        serde_json::to_value(ConnectionReauthRequired {
                            connection_id: record.connection_id.clone(),
                            failure: classified.clone(),
                            grant: record.scopes.clone(),
                            recorded_at: now,
                        })
                        .map_err(|e| e.to_string())?,
                    ),
                )
                .await?;
                let mut updated_record = record.clone();
                updated_record.status = ConnectionStatus::NeedsReauth;
                updated_record.health.last_failure = Some(classified);
                updated_record.updated_at = now;
                let updated = StoredConnection {
                    record: updated_record,
                    credential: stored.credential.clone(),
                };
                let _ = self
                    .store
                    .update_connection(tenant, &record.connection_id, stored, &updated)
                    .await?;
                Ok(Some(RefreshAttempt::NeedsReauth))
            }
            Err(failure) => {
                // A transient failure moves the health counters and
                // journals NOTHING: counters are bookkeeping, transitions
                // are evidence (the wave-4 refinement — a provider's
                // flaky minute must not write a journal page per
                // connection per pass).
                let mut updated_record = record.clone();
                updated_record.health.consecutive_failures += 1;
                updated_record.health.last_failure = Some(ClassifiedFailure {
                    class: failure.class,
                    detail: failure.detail,
                    at: now,
                });
                updated_record.updated_at = now;
                let updated = StoredConnection {
                    record: updated_record,
                    credential: stored.credential.clone(),
                };
                // A lost race loses the counter bump, nothing more — the
                // winner's mutation carries its own bookkeeping.
                let _ = self
                    .store
                    .update_connection(tenant, &record.connection_id, stored, &updated)
                    .await?;
                Ok(Some(if expires_at > now {
                    RefreshAttempt::TransientServe(material)
                } else {
                    RefreshAttempt::TransientExpired
                }))
            }
        }
    }

    /// The resolution-path wrapper: serve the (possibly rotated)
    /// material, or fail closed with the denial the attempt maps to.
    async fn refresh_at_resolution(
        &self,
        tenant: &str,
        handle_id: &str,
        stored: &StoredConnection,
        material: TokenMaterial,
    ) -> Result<TokenMaterial, BrokerDenial> {
        let attempt = self
            .refresh_if_due(tenant, stored, material.clone(), Utc::now())
            .await
            .map_err(BrokerDenial::unavailable)?;
        match attempt {
            None => Ok(material),
            Some(RefreshAttempt::Rotated(rotated)) => Ok(rotated),
            Some(RefreshAttempt::TransientServe(material)) => Ok(material),
            Some(RefreshAttempt::TransientExpired) => Err(BrokerDenial::unavailable(format!(
                "connection `{}`: the provider refresh failed transiently and the access token \
                 has expired — retry, or record a new consent act",
                stored.record.connection_id
            ))),
            Some(RefreshAttempt::ExpiredUnservable) => Err(BrokerDenial::unavailable(format!(
                "connection `{}`: the access token expired and the connection has no refresh \
                 path — record a new consent act",
                stored.record.connection_id
            ))),
            Some(RefreshAttempt::NeedsReauth) => Err(BrokerDenial::connection_needs_reauth(
                &stored.record,
                tenant,
                handle_id,
            )),
        }
    }

    /// One sweeper pass (R0.11 wave 4): the same lifecycle the
    /// resolution path runs, over every active OAuth connection in the
    /// deployment. The pass's concurrency control is the connection
    /// row's own CAS — two sweepers (or a sweeper and a resolution)
    /// cannot double-rotate: the loser's update conflicts, it re-reads,
    /// and the winner's material serves.
    ///
    /// A connection whose envelope will not open (a master key this host
    /// does not hold, corruption) counts as `failed` and the pass
    /// continues — one bad row must not stall the fleet.
    pub(crate) async fn sweep_once(&self, now: chrono::DateTime<Utc>) -> StoreResult<SweepReport> {
        let mut report = SweepReport::default();
        if self.oauth.is_none() {
            return Ok(report);
        }
        for (tenant, stored) in self.store.list_all_connections().await? {
            if stored.record.status != ConnectionStatus::Active
                || !stored.record.provider.is_oauth()
            {
                continue;
            }
            report.scanned += 1;
            let opened = async {
                let master = self.master_for(&stored.credential.key_id).await?;
                open(&master, &stored.record.connection_id, &stored.credential)
            }
            .await;
            let material = match opened {
                Ok(material) => material,
                Err(e) => {
                    tracing::warn!(
                        connection_id = %stored.record.connection_id,
                        %e,
                        "broker sweep could not open the connection's envelope"
                    );
                    report.failed += 1;
                    continue;
                }
            };
            match self.refresh_if_due(&tenant, &stored, material, now).await? {
                None => {}
                Some(RefreshAttempt::Rotated(_)) => report.refreshed += 1,
                Some(RefreshAttempt::NeedsReauth) => report.needs_reauth += 1,
                Some(
                    RefreshAttempt::TransientServe(_)
                    | RefreshAttempt::TransientExpired
                    | RefreshAttempt::ExpiredUnservable,
                ) => report.failed += 1,
            }
        }
        Ok(report)
    }
}

/// Spawn the background sweeper (R0.11 wave 4): [`Broker::sweep_once`]
/// on `interval`, with the outbox relay's drain semantics — a pass in
/// flight when shutdown starts completes, and a delayed tick delays
/// rather than bursting into a provider stampede. Nothing is due
/// *because* the sweeper exists: resolution runs the same lifecycle,
/// so a stopped sweeper degrades to refresh-at-use, never to expiry.
pub(crate) fn spawn_sweeper(
    broker: Arc<Broker>,
    interval: Duration,
    shutdown: tokio_util::sync::CancellationToken,
) {
    tokio::spawn(async move {
        let mut ticker = tokio::time::interval(interval);
        ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
        loop {
            tokio::select! {
                _ = ticker.tick() => {}
                _ = shutdown.cancelled() => {
                    tracing::info!("broker sweeper shutting down; due tokens refresh at resolution or on the next process");
                    break;
                }
            }
            match broker.sweep_once(Utc::now()).await {
                Ok(report)
                    if report.refreshed > 0 || report.needs_reauth > 0 || report.failed > 0 =>
                {
                    tracing::info!(?report, "broker sweep completed");
                }
                Ok(_) => {}
                Err(error) => {
                    tracing::warn!(%error, "broker sweep failed; will retry");
                }
            }
        }
    });
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::server_store::JsonFileStore;
    use chrono::DateTime;
    use rusty_agent_runtime::broker::{BrokerDenialReason, CredentialRequirement, IssueRequest};
    use std::collections::BTreeSet;

    /// Unique temp store root, removed at the end of each test (best
    /// effort).
    fn temp_store() -> PathBuf {
        std::env::temp_dir().join(format!("rusty-broker-test-{}", uuid::Uuid::new_v4()))
    }

    fn material() -> TokenMaterial {
        TokenMaterial {
            access_token: "sk-live-MARKER-9f2e".into(),
            refresh_token: Some("rt-MARKER-41ab".into()),
            client_secret: None,
            expires_at: Some(DateTime::<Utc>::from_timestamp_millis(1_800_003_600_000).unwrap()),
        }
    }

    fn broker(root: &Path) -> (Broker, Arc<dyn ServerStore>) {
        let store: Arc<dyn ServerStore> = Arc::new(JsonFileStore::load(root));
        (
            Broker::new(Arc::clone(&store), root.to_path_buf(), DEFAULT_HANDLE_TTL),
            store,
        )
    }

    async fn register(b: &Broker) -> ConnectionRecord {
        b.register(
            "acme",
            ConnectionProvider::Oauth2AuthorizationCode,
            Some("user-7".into()),
            BTreeSet::from(["drive.readonly".to_owned(), "drive.write".to_owned()]),
            &material(),
        )
        .await
        .unwrap()
    }

    #[tokio::test]
    async fn wrong_key_and_tampering_fail_to_open() {
        let root = temp_store();
        let (key_id, master) = ("bmk-testmaster0001".to_string(), draw_random::<32>());
        let envelope = seal_new(&key_id, &master, "conn-abc", &material()).unwrap();
        // A wrong master key fails the wrap tag.
        let wrong = draw_random::<32>();
        assert!(open(&wrong, "conn-abc", &envelope).is_err());
        // A transplanted envelope (same bytes, another connection's AAD)
        // fails.
        assert!(open(&master, "conn-other", &envelope).is_err());
        // Tampered ciphertext fails the tag.
        let mut tampered = envelope.clone();
        let mut bytes = hex_decode(&tampered.ciphertext).unwrap();
        let last = bytes.len() - 1;
        bytes[last] ^= 1;
        tampered.ciphertext = hex_encode(&bytes);
        assert!(open(&master, "conn-abc", &tampered).is_err());
        // A tampered wrapped key fails the wrap tag.
        let mut tampered = envelope.clone();
        let mut bytes = hex_decode(&tampered.wrapped_data_key).unwrap();
        bytes[0] ^= 1;
        tampered.wrapped_data_key = hex_encode(&bytes);
        assert!(open(&master, "conn-abc", &tampered).is_err());
        // The honest envelope opens byte-exact.
        assert_eq!(open(&master, "conn-abc", &envelope).unwrap(), material());
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn reseal_keeps_the_data_key_and_freshens_the_nonce() {
        let (key_id, master) = ("bmk-testmaster0002".to_string(), draw_random::<32>());
        let first = seal_new(&key_id, &master, "conn-abc", &material()).unwrap();
        let rotated = TokenMaterial {
            access_token: "sk-live-ROTATED".into(),
            refresh_token: None,
            client_secret: None,
            expires_at: None,
        };
        let second = reseal(&key_id, &master, "conn-abc", &first, &rotated).unwrap();
        assert_eq!(first.wrapped_data_key, second.wrapped_data_key);
        assert_ne!(first.nonce, second.nonce);
        assert_eq!(open(&master, "conn-abc", &second).unwrap(), rotated);
    }

    #[tokio::test]
    async fn master_key_file_is_0600_and_outside_the_store() {
        let root = temp_store();
        let (b, _) = broker(&root);
        register(&b).await;
        let keys: Vec<_> = std::fs::read_dir(crate::receipts::keys_dir(&root))
            .unwrap()
            .flatten()
            .collect();
        assert_eq!(keys.len(), 1);
        let name = keys[0].file_name().to_string_lossy().into_owned();
        assert!(name.starts_with("broker-master.bmk-"), "got: {name}");
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt as _;
            let mode = keys[0].metadata().unwrap().permissions().mode();
            assert_eq!(mode & 0o777, 0o600, "master key file mode: {mode:o}");
        }
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn store_holds_ciphertext_only_and_restart_resolves() {
        let root = temp_store();
        let record = {
            let (b, _) = broker(&root);
            register(&b).await
        };
        // The on-disk record contains no plaintext credential: scan the
        // raw bytes of every file under the store root for both markers.
        let mut raw = Vec::new();
        for entry in std::fs::read_dir(connections_dir(&root)).unwrap().flatten() {
            raw.extend(std::fs::read(entry.path()).unwrap());
        }
        let raw = String::from_utf8_lossy(&raw);
        assert!(!raw.contains("sk-live-MARKER-9f2e"), "plaintext at rest");
        assert!(!raw.contains("rt-MARKER-41ab"), "plaintext at rest");
        // A fresh process (new store index, no cached master key)
        // resolves: the master key file and the ciphertext both persist.
        let (b2, _) = broker(&root);
        let request = IssueRequest {
            tenant: "acme".into(),
            run_id: Some("run-1".into()),
            requirement: CredentialRequirement {
                connection_id: record.connection_id.clone(),
                scopes: BTreeSet::from(["drive.readonly".to_owned()]),
            },
        };
        let handle = rusty_agent_runtime::broker::CredentialBroker::issue(&b2, &request)
            .await
            .unwrap();
        let resolved = rusty_agent_runtime::broker::CredentialBroker::resolve(
            &b2,
            &handle.token(),
            &BTreeSet::from(["drive.readonly".to_owned()]),
        )
        .await
        .unwrap();
        assert_eq!(resolved.material, material());
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn forged_and_tampered_handles_fail_closed() {
        let root = temp_store();
        let (b, _) = broker(&root);
        let record = register(&b).await;
        let request = IssueRequest {
            tenant: "acme".into(),
            run_id: None,
            requirement: CredentialRequirement {
                connection_id: record.connection_id.clone(),
                scopes: BTreeSet::new(),
            },
        };
        let handle = rusty_agent_runtime::broker::CredentialBroker::issue(&b, &request)
            .await
            .unwrap();
        let scopes = BTreeSet::new();
        // A tampered token (claims edited underneath the signature) fails.
        let mut tampered_claims = handle.claims().clone();
        tampered_claims.scopes.insert("drive.admin".into());
        let forged = CredentialHandle::from_parts(tampered_claims, handle.signature().to_owned());
        let err =
            rusty_agent_runtime::broker::CredentialBroker::resolve(&b, &forged.token(), &scopes)
                .await
                .unwrap_err();
        assert!(
            matches!(err.reason, BrokerDenialReason::UnknownHandle),
            "got: {err}"
        );
        // Garbage fails the same way (parse failure and forgery are one
        // refusal).
        let err =
            rusty_agent_runtime::broker::CredentialBroker::resolve(&b, "v1.garbage.sig", &scopes)
                .await
                .unwrap_err();
        assert!(
            matches!(err.reason, BrokerDenialReason::UnknownHandle),
            "got: {err}"
        );
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn revocation_fails_closed_typed_and_journaled() {
        let root = temp_store();
        let (b, store) = broker(&root);
        let record = register(&b).await;
        let request = IssueRequest {
            tenant: "acme".into(),
            run_id: Some("run-1".into()),
            requirement: CredentialRequirement {
                connection_id: record.connection_id.clone(),
                scopes: BTreeSet::from(["drive.readonly".to_owned()]),
            },
        };
        let handle = rusty_agent_runtime::broker::CredentialBroker::issue(&b, &request)
            .await
            .unwrap();
        // The handle resolves while live...
        rusty_agent_runtime::broker::CredentialBroker::resolve(
            &b,
            &handle.token(),
            &BTreeSet::from(["drive.readonly".to_owned()]),
        )
        .await
        .unwrap();
        // ...and the very next use after revocation is a typed denial.
        match b
            .revoke("acme", &record.connection_id, Some("offboarded".into()))
            .await
            .unwrap()
        {
            RevokeOutcome::Applied { .. } => {}
            other => panic!(
                "expected applied, got {}",
                match other {
                    RevokeOutcome::Converged(_) => "converged",
                    RevokeOutcome::Unknown => "unknown",
                    RevokeOutcome::Conflict => "conflict",
                    RevokeOutcome::Applied { .. } => unreachable!(),
                }
            ),
        }
        let err = rusty_agent_runtime::broker::CredentialBroker::resolve(
            &b,
            &handle.token(),
            &BTreeSet::from(["drive.readonly".to_owned()]),
        )
        .await
        .unwrap_err();
        match &err.reason {
            BrokerDenialReason::ConnectionRevoked { grant } => {
                assert_eq!(
                    grant,
                    &vec!["drive.readonly".to_owned(), "drive.write".to_owned()]
                )
            }
            other => panic!("expected revoked, got {other:?}"),
        }
        assert_eq!(
            err.connection_id.as_deref(),
            Some(record.connection_id.as_str())
        );

        // The chain: registered, issued, used, revoked, denied — every
        // transition in order, none carrying the marker bytes.
        let snapshot = store
            .get_journal(BROKER_JOURNAL_RUN_ID)
            .await
            .unwrap()
            .expect("the broker journal exists");
        let kinds: Vec<RunEventKind> = snapshot.events.iter().map(|e| e.kind).collect();
        assert_eq!(
            kinds,
            vec![
                RunEventKind::ConnectionRegistered,
                RunEventKind::CredentialHandleIssued,
                RunEventKind::CredentialUse,
                RunEventKind::ConnectionRevoked,
                RunEventKind::CredentialDenied,
            ],
            "journal kinds: {kinds:?}"
        );
        let whole = serde_json::to_string(&snapshot).unwrap();
        assert!(
            !whole.contains("sk-live-MARKER-9f2e"),
            "bytes in the journal"
        );
        assert!(!whole.contains("rt-MARKER-41ab"), "bytes in the journal");
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn cross_tenant_handles_and_scope_escalation_are_denied() {
        let root = temp_store();
        let (b, _) = broker(&root);
        let record = register(&b).await;
        // Beyond the consented set: denied at issuance, journaled.
        let request = IssueRequest {
            tenant: "acme".into(),
            run_id: None,
            requirement: CredentialRequirement {
                connection_id: record.connection_id.clone(),
                scopes: BTreeSet::from(["drive.admin".to_owned()]),
            },
        };
        let err = rusty_agent_runtime::broker::CredentialBroker::issue(&b, &request)
            .await
            .unwrap_err();
        match &err.reason {
            BrokerDenialReason::ScopeNotGranted { missing } => {
                assert_eq!(missing, &vec!["drive.admin".to_owned()])
            }
            other => panic!("expected scope denial, got {other:?}"),
        }
        // A handle issued to one tenant resolves nothing in another —
        // the liveness read is tenant-scoped, so the claim's tenant names
        // a connection that tenant does not hold.
        let request = IssueRequest {
            tenant: "acme".into(),
            run_id: None,
            requirement: CredentialRequirement {
                connection_id: record.connection_id.clone(),
                scopes: BTreeSet::new(),
            },
        };
        // A baseline issuance succeeds; the adversarial cases are below.
        rusty_agent_runtime::broker::CredentialBroker::issue(&b, &request)
            .await
            .unwrap();
        // Forge the tenant in the claims (the signature covers it, so a
        // real cross-tenant presentation would need the signing key —
        // the read being scoped is the second wall, tested here through
        // an honestly re-tenant-ed issuance request against globex).
        let err = rusty_agent_runtime::broker::CredentialBroker::issue(
            &b,
            &IssueRequest {
                tenant: "globex".into(),
                run_id: None,
                requirement: CredentialRequirement {
                    connection_id: record.connection_id.clone(),
                    scopes: BTreeSet::new(),
                },
            },
        )
        .await
        .unwrap_err();
        assert!(
            matches!(err.reason, BrokerDenialReason::UnknownConnection),
            "got: {err}"
        );
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn consent_narrows_widens_only_by_journaled_act_and_refreshes() {
        let root = temp_store();
        let (b, _) = broker(&root);
        let record = register(&b).await;
        // Material-only update: a refresh beneath the same ceiling —
        // the old handle's scopes still resolve, against new bytes.
        let rotated = TokenMaterial {
            access_token: "sk-live-ROTATED".into(),
            refresh_token: None,
            client_secret: None,
            expires_at: None,
        };
        match b
            .record_consent("acme", &record.connection_id, None, Some(&rotated))
            .await
            .unwrap()
        {
            ConsentOutcome::Applied { journaled, .. } => {
                assert_eq!(journaled, RunEventKind::ConnectionRefreshed)
            }
            _ => panic!("expected applied"),
        }
        let handle = rusty_agent_runtime::broker::CredentialBroker::issue(
            &b,
            &IssueRequest {
                tenant: "acme".into(),
                run_id: None,
                requirement: CredentialRequirement {
                    connection_id: record.connection_id.clone(),
                    scopes: BTreeSet::new(),
                },
            },
        )
        .await
        .unwrap();
        let resolved = rusty_agent_runtime::broker::CredentialBroker::resolve(
            &b,
            &handle.token(),
            &BTreeSet::new(),
        )
        .await
        .unwrap();
        assert_eq!(resolved.material.access_token, "sk-live-ROTATED");
        // A scope change is a consent act, journaled as such.
        match b
            .record_consent(
                "acme",
                &record.connection_id,
                Some(BTreeSet::from(["drive.readonly".to_owned()])),
                None,
            )
            .await
            .unwrap()
        {
            ConsentOutcome::Applied { journaled, record } => {
                assert_eq!(journaled, RunEventKind::ConnectionConsented);
                assert_eq!(record.scopes, BTreeSet::from(["drive.readonly".to_owned()]));
            }
            _ => panic!("expected applied"),
        }
        // Re-recording the same fact converges without a new event.
        match b
            .record_consent("acme", &record.connection_id, None, None)
            .await
            .unwrap()
        {
            ConsentOutcome::Converged(_) => {}
            _ => panic!("expected converged"),
        }
        let _ = std::fs::remove_dir_all(root);
    }

    // ---------- the OAuth lifecycle (R0.11 wave 4) ----------

    use rusty_agent_runtime::broker::{
        CredentialBroker, OAuthFailure, ResolvedCredential, ScriptedOAuthProvider,
    };
    use rusty_agent_runtime::durable::ErrorClass;

    fn oauth_broker(
        root: &Path,
        provider: Arc<ScriptedOAuthProvider>,
        window: Duration,
    ) -> (Broker, Arc<dyn ServerStore>) {
        let store: Arc<dyn ServerStore> = Arc::new(JsonFileStore::load(root));
        (
            Broker::new(Arc::clone(&store), root.to_path_buf(), DEFAULT_HANDLE_TTL)
                .with_oauth_provider(provider, window),
            store,
        )
    }

    /// Material inside any reasonable refresh window, with a refresh
    /// path (an authorization-code connection's refresh token).
    fn due_material() -> TokenMaterial {
        TokenMaterial {
            access_token: "sk-live-DUE".into(),
            refresh_token: Some("rt-due".into()),
            client_secret: None,
            expires_at: Some(Utc::now() + chrono::Duration::seconds(60)),
        }
    }

    /// Material far outside any refresh window.
    fn fresh_material() -> TokenMaterial {
        TokenMaterial {
            expires_at: Some(Utc::now() + chrono::Duration::hours(2)),
            ..due_material()
        }
    }

    async fn journal_kinds(store: &Arc<dyn ServerStore>) -> Vec<RunEventKind> {
        store
            .get_journal(BROKER_JOURNAL_RUN_ID)
            .await
            .unwrap()
            .map(|snapshot| snapshot.events.iter().map(|e| e.kind).collect())
            .unwrap_or_default()
    }

    async fn issue_and_resolve(
        b: &Broker,
        connection_id: &str,
    ) -> Result<ResolvedCredential, BrokerDenial> {
        let handle = CredentialBroker::issue(
            b,
            &IssueRequest {
                tenant: "acme".into(),
                run_id: None,
                requirement: CredentialRequirement {
                    connection_id: connection_id.to_owned(),
                    scopes: BTreeSet::new(),
                },
            },
        )
        .await?;
        CredentialBroker::resolve(b, &handle.token(), &BTreeSet::new()).await
    }

    #[test]
    fn refresh_window_jitter_is_deterministic_bounded_and_spread() {
        let window = Duration::from_secs(600);
        let full = chrono::Duration::from_std(window).unwrap();
        let a = refresh_window_for("conn-a", window);
        // Deterministic: no RNG at resolution, so a recorded run's
        // refresh cadence reproduces.
        assert_eq!(a, refresh_window_for("conn-a", window));
        for connection_id in ["conn-a", "conn-b", "conn-c", "conn-d"] {
            let jittered = refresh_window_for(connection_id, window);
            assert!(jittered >= full / 2, "{connection_id}: {jittered}");
            assert!(jittered < full, "{connection_id}: {jittered}");
        }
    }

    #[tokio::test]
    async fn resolution_refreshes_a_due_token_beneath_the_stable_id() {
        let root = temp_store();
        let provider = Arc::new(ScriptedOAuthProvider::new());
        let (b, store) = oauth_broker(&root, Arc::clone(&provider), DEFAULT_REFRESH_WINDOW);
        let record = {
            let now = Utc::now();
            let record = ConnectionRecord {
                connection_id: new_connection_id(),
                provider: ConnectionProvider::Oauth2AuthorizationCode,
                subject: None,
                scopes: BTreeSet::from(["drive.readonly".to_owned()]),
                status: ConnectionStatus::Active,
                health: Default::default(),
                created_at: now,
                updated_at: now,
            };
            b.register(
                "acme",
                record.provider,
                record.subject.clone(),
                record.scopes.clone(),
                &due_material(),
            )
            .await
            .unwrap()
        };
        let resolved = issue_and_resolve(&b, &record.connection_id).await.unwrap();
        // The rotation: new access token, the presented refresh token
        // carried over, and the connection id untouched.
        assert_eq!(resolved.material.access_token, "scripted-token-0");
        assert_eq!(resolved.material.refresh_token.as_deref(), Some("rt-due"));
        assert_eq!(resolved.connection_id, record.connection_id);
        assert_eq!(provider.call_counts(), (0, 1));
        // The record's health half moved; the status stands.
        let after = b.get("acme", &record.connection_id).await.unwrap().unwrap();
        assert_eq!(after.status, ConnectionStatus::Active);
        assert!(after.health.last_refresh_at.is_some());
        assert_eq!(after.health.consecutive_failures, 0);
        // The refresh journaled before the use it served.
        assert_eq!(
            journal_kinds(&store).await,
            vec![
                RunEventKind::ConnectionRegistered,
                RunEventKind::CredentialHandleIssued,
                RunEventKind::ConnectionRefreshed,
                RunEventKind::CredentialUse,
            ]
        );
        // The new grant lives an hour: the next resolution serves it
        // untouched — no second refresh.
        let again = issue_and_resolve(&b, &record.connection_id).await.unwrap();
        assert_eq!(again.material.access_token, "scripted-token-0");
        assert_eq!(provider.call_counts(), (0, 1));
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn a_token_outside_its_window_serves_untouched() {
        let root = temp_store();
        let provider = Arc::new(ScriptedOAuthProvider::new());
        let (b, store) = oauth_broker(&root, Arc::clone(&provider), DEFAULT_REFRESH_WINDOW);
        let record = b
            .register(
                "acme",
                ConnectionProvider::Oauth2AuthorizationCode,
                None,
                BTreeSet::from(["drive.readonly".to_owned()]),
                &fresh_material(),
            )
            .await
            .unwrap();
        let resolved = issue_and_resolve(&b, &record.connection_id).await.unwrap();
        assert_eq!(resolved.material.access_token, "sk-live-DUE");
        assert_eq!(provider.call_counts(), (0, 0));
        assert!(!journal_kinds(&store)
            .await
            .contains(&RunEventKind::ConnectionRefreshed));
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn permanent_refresh_failure_flips_needs_reauth_and_fails_closed() {
        let root = temp_store();
        let provider = Arc::new(ScriptedOAuthProvider::new().then_refresh_failure(
            OAuthFailure::permanent("invalid_grant: the refresh token was revoked at the provider"),
        ));
        let (b, store) = oauth_broker(&root, Arc::clone(&provider), DEFAULT_REFRESH_WINDOW);
        let record = b
            .register(
                "acme",
                ConnectionProvider::Oauth2AuthorizationCode,
                None,
                BTreeSet::from(["drive.readonly".to_owned()]),
                &due_material(),
            )
            .await
            .unwrap();
        let err = issue_and_resolve(&b, &record.connection_id)
            .await
            .unwrap_err();
        assert!(
            matches!(err.reason, BrokerDenialReason::ConnectionNeedsReauth),
            "got: {err}"
        );
        // The flip: status, the classified failure, and the journaled
        // transition — the denial journaled after it.
        let after = b.get("acme", &record.connection_id).await.unwrap().unwrap();
        assert_eq!(after.status, ConnectionStatus::NeedsReauth);
        let failure = after.health.last_failure.as_ref().unwrap();
        assert_eq!(failure.class, ErrorClass::InvalidInput);
        assert_eq!(
            journal_kinds(&store).await,
            vec![
                RunEventKind::ConnectionRegistered,
                RunEventKind::CredentialHandleIssued,
                RunEventKind::ConnectionNeedsReauth,
                RunEventKind::CredentialDenied,
            ]
        );
        // Issuance fails closed the same way — every path out of
        // `needs_reauth` runs through a recorded consent act.
        let err = CredentialBroker::issue(
            &b,
            &IssueRequest {
                tenant: "acme".into(),
                run_id: None,
                requirement: CredentialRequirement {
                    connection_id: record.connection_id.clone(),
                    scopes: BTreeSet::new(),
                },
            },
        )
        .await
        .unwrap_err();
        assert!(matches!(
            err.reason,
            BrokerDenialReason::ConnectionNeedsReauth
        ));
        // The re-auth path: a recorded consent act re-activates, and the
        // next resolution serves the new material (steady-state rotation
        // resumes — the scripted permanent failure is consumed).
        match b
            .record_consent("acme", &record.connection_id, None, Some(&fresh_material()))
            .await
            .unwrap()
        {
            ConsentOutcome::Applied { record, .. } => {
                assert_eq!(record.status, ConnectionStatus::Active)
            }
            _ => panic!("expected applied"),
        }
        let resolved = issue_and_resolve(&b, &record.connection_id).await.unwrap();
        assert_eq!(resolved.material.access_token, "sk-live-DUE");
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn transient_refresh_failure_serves_a_still_valid_token_and_journals_nothing() {
        let root = temp_store();
        let provider = Arc::new(ScriptedOAuthProvider::new().then_refresh_failure(
            OAuthFailure::transient(ErrorClass::DependencyFailure, "token endpoint 503"),
        ));
        let (b, store) = oauth_broker(&root, Arc::clone(&provider), DEFAULT_REFRESH_WINDOW);
        let record = b
            .register(
                "acme",
                ConnectionProvider::Oauth2AuthorizationCode,
                None,
                BTreeSet::from(["drive.readonly".to_owned()]),
                &due_material(),
            )
            .await
            .unwrap();
        // The token is still valid: serve it. The counters moved; the
        // journal did not — bookkeeping, not a transition.
        let resolved = issue_and_resolve(&b, &record.connection_id).await.unwrap();
        assert_eq!(resolved.material.access_token, "sk-live-DUE");
        assert_eq!(provider.call_counts(), (0, 1));
        let after = b.get("acme", &record.connection_id).await.unwrap().unwrap();
        assert_eq!(after.health.consecutive_failures, 1);
        assert_eq!(
            after.health.last_failure.as_ref().unwrap().class,
            ErrorClass::DependencyFailure
        );
        assert_eq!(
            journal_kinds(&store).await,
            vec![
                RunEventKind::ConnectionRegistered,
                RunEventKind::CredentialHandleIssued,
                RunEventKind::CredentialUse,
            ]
        );
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn expired_tokens_with_no_servable_path_fail_closed() {
        let root = temp_store();
        let provider = Arc::new(ScriptedOAuthProvider::new());
        let (b, _) = oauth_broker(&root, Arc::clone(&provider), DEFAULT_REFRESH_WINDOW);
        let expired = Utc::now() - chrono::Duration::seconds(60);
        // Expired, no refresh path: nothing to rotate with.
        let no_path = b
            .register(
                "acme",
                ConnectionProvider::Oauth2AuthorizationCode,
                None,
                BTreeSet::from(["drive.readonly".to_owned()]),
                &TokenMaterial {
                    access_token: "sk-live-DEAD".into(),
                    refresh_token: None,
                    client_secret: None,
                    expires_at: Some(expired),
                },
            )
            .await
            .unwrap();
        let err = issue_and_resolve(&b, &no_path.connection_id)
            .await
            .unwrap_err();
        assert!(
            matches!(err.reason, BrokerDenialReason::BrokerUnavailable),
            "got: {err}"
        );
        assert!(err.detail.contains("no refresh path"), "got: {err}");
        assert_eq!(provider.call_counts(), (0, 0));
        let _ = std::fs::remove_dir_all(root);

        // Expired, refresh path, transient failure: nothing servable
        // remains.
        let root = temp_store();
        let provider = Arc::new(ScriptedOAuthProvider::new().then_refresh_failure(
            OAuthFailure::transient(ErrorClass::Transient, "connection reset"),
        ));
        let (b, _) = oauth_broker(&root, Arc::clone(&provider), DEFAULT_REFRESH_WINDOW);
        let dead = b
            .register(
                "acme",
                ConnectionProvider::Oauth2AuthorizationCode,
                None,
                BTreeSet::from(["drive.readonly".to_owned()]),
                &TokenMaterial {
                    expires_at: Some(expired),
                    ..due_material()
                },
            )
            .await
            .unwrap();
        let err = issue_and_resolve(&b, &dead.connection_id)
            .await
            .unwrap_err();
        assert!(
            matches!(err.reason, BrokerDenialReason::BrokerUnavailable),
            "got: {err}"
        );
        assert_eq!(provider.call_counts(), (0, 1));
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn client_credentials_refresh_rotates_on_the_client_secret() {
        let root = temp_store();
        let provider = Arc::new(ScriptedOAuthProvider::new());
        let (b, _) = oauth_broker(&root, Arc::clone(&provider), DEFAULT_REFRESH_WINDOW);
        let record = b
            .register(
                "acme",
                ConnectionProvider::Oauth2ClientCredentials,
                None,
                BTreeSet::from(["drive.readonly".to_owned()]),
                &TokenMaterial {
                    access_token: "cc-token-0".into(),
                    refresh_token: None,
                    client_secret: Some("cc-secret-MARKER".into()),
                    expires_at: Some(Utc::now() + chrono::Duration::seconds(60)),
                },
            )
            .await
            .unwrap();
        let resolved = issue_and_resolve(&b, &record.connection_id).await.unwrap();
        // Rotated: a fresh access token, no refresh token (the flow has
        // none), and the client secret — custody, not a grant output —
        // carried over for the next rotation.
        assert_eq!(resolved.material.access_token, "scripted-token-0");
        assert_eq!(resolved.material.refresh_token, None);
        assert_eq!(
            resolved.material.client_secret.as_deref(),
            Some("cc-secret-MARKER")
        );
        assert_eq!(provider.call_counts(), (0, 1));
        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn the_sweeper_refreshes_due_connections_and_counts_outcomes() {
        let root = temp_store();
        let provider = Arc::new(ScriptedOAuthProvider::new());
        let (b, store) = oauth_broker(&root, Arc::clone(&provider), DEFAULT_REFRESH_WINDOW);
        // One due, one fresh (both OAuth), one static kind — the pass
        // scans the OAuth two and refreshes the due one.
        let due = b
            .register(
                "acme",
                ConnectionProvider::Oauth2AuthorizationCode,
                None,
                BTreeSet::from(["drive.readonly".to_owned()]),
                &due_material(),
            )
            .await
            .unwrap();
        b.register(
            "acme",
            ConnectionProvider::Oauth2AuthorizationCode,
            None,
            BTreeSet::from(["drive.readonly".to_owned()]),
            &fresh_material(),
        )
        .await
        .unwrap();
        b.register(
            "acme",
            ConnectionProvider::ApiKey,
            None,
            BTreeSet::new(),
            &TokenMaterial {
                access_token: "sk-static".into(),
                refresh_token: None,
                client_secret: None,
                expires_at: None,
            },
        )
        .await
        .unwrap();
        let report = b.sweep_once(Utc::now()).await.unwrap();
        assert_eq!(
            report,
            SweepReport {
                scanned: 2,
                refreshed: 1,
                needs_reauth: 0,
                failed: 0,
            }
        );
        let after = b.get("acme", &due.connection_id).await.unwrap().unwrap();
        assert!(after.health.last_refresh_at.is_some());
        assert!(journal_kinds(&store)
            .await
            .contains(&RunEventKind::ConnectionRefreshed));

        // A second due connection whose refresh the provider refuses
        // terminally: the pass flips it (journaled) and counts it.
        let provider2 = Arc::new(ScriptedOAuthProvider::new().then_refresh_failure(
            OAuthFailure::permanent("invalid_grant: refresh token expired"),
        ));
        let root2 = temp_store();
        let (b2, _) = oauth_broker(&root2, Arc::clone(&provider2), DEFAULT_REFRESH_WINDOW);
        let terminal = b2
            .register(
                "acme",
                ConnectionProvider::Oauth2AuthorizationCode,
                None,
                BTreeSet::from(["drive.readonly".to_owned()]),
                &due_material(),
            )
            .await
            .unwrap();
        let report = b2.sweep_once(Utc::now()).await.unwrap();
        assert_eq!(
            report,
            SweepReport {
                scanned: 1,
                refreshed: 0,
                needs_reauth: 1,
                failed: 0,
            }
        );
        let after = b2
            .get("acme", &terminal.connection_id)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(after.status, ConnectionStatus::NeedsReauth);
        // Out of `active`, so the next pass does not even scan it.
        let report = b2.sweep_once(Utc::now()).await.unwrap();
        assert_eq!(report, SweepReport::default());
        let _ = std::fs::remove_dir_all(root);
        let _ = std::fs::remove_dir_all(root2);
    }
}