adk-kernel 3.0.0

Agent kernel for ADK-Rust: a resident supervisor with a process model, wake sources, an event bus and a control plane over durable runs
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
//! The supervision state a kernel shares between its process table, its worker hooks and
//! its loops (spec 3.2 §7.2, §7.3, §7.6, §9.3).

use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, OnceLock};

use adk_core::audit_log::{AuditActor, AuditRecordDraft, AuditRecordKind, AuditTrail};
use adk_core::principal::SubjectRef;
use adk_core::schedule::{DEFAULT_MISFIRE_GRACE, MissedFirePolicy, ScheduleSpec, plan_fires};
use adk_core::{
    Clock, DurableRunStore, GovernanceControl, Part, RunCheckpointRef, RunFilter, RunRecord,
    RunState, SpendKey, UsageUnit, UsageVector,
};
use adk_server::background::{DurableWorker, ParkReason, WorkerHandle, WorkerHooks};
use adk_session::{GetRequest, SessionService};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde_json::{Value, json};

use crate::agenda::{AgendaItem, AgendaState, AgendaStore, ProactivityPolicy};
use crate::audit;
use crate::bus::{EventBus, KernelEvent, SpendDelta};
use crate::partition::PartitionId;
use crate::process::{ProcessKind, ProcessMeta, ProgressSource, RunUsage, Suspension};
use crate::sched::QueueClass;
use crate::sched::{
    Admission, AdmissionInput, ProcessClass, QuietHours, Quotas, admit, youngest_background,
};
use crate::stores::{ProcessFilter, ProcessStore, UsageTotals};
use crate::wake::KERNEL_WAKE_TARGET;
use crate::wake::{Wake, WakeSet};

/// The target `adk-harness` runs goals under (`adk_harness::GOAL_TARGET`); the kernel names
/// it without depending on the harness (spec 3.2 §6 D-1).
pub(crate) const GOAL_TARGET: &str = "harness.goal";

/// The idempotency-key prefix of a run [`ProcessTable::spawn`](crate::ProcessTable::spawn)
/// enqueued asleep. The run itself carries the intent, so its first claim parks it before
/// its first model call however long its side-table row takes to write.
pub(crate) const ASLEEP_KEY_PREFIX: &str = "kernel:asleep:";
/// The idempotency-key prefix of a run `spawn` enqueued awake.
pub(crate) const SPAWN_KEY_PREFIX: &str = "kernel:spawn:";
/// The key, after [`ASLEEP_KEY_PREFIX`], of a cron sleeper's successor: `recur:<pid of the
/// finished sleeper>` (spec 3.2 §7.4).
pub(crate) const RECUR_KEY_PREFIX: &str = "recur:";

/// When the cron series of the sleeper `run` began (spec 3.2 §7.4): its own creation, or
/// `None` for a successor the kernel re-armed, which continues its predecessor's series.
pub(crate) fn series_start(run: &RunRecord) -> Option<DateTime<Utc>> {
    let continues = run
        .idempotency_key
        .as_deref()
        .and_then(|key| key.strip_prefix(ASLEEP_KEY_PREFIX))
        .is_some_and(|key| key.starts_with(RECUR_KEY_PREFIX));
    (!continues).then_some(run.created_at)
}

/// Whether `ProcessTable::spawn` enqueued `run`, so `spawn`, not a hook, writes its row.
pub(crate) fn spawned_by_kernel(run: &RunRecord) -> bool {
    run.idempotency_key
        .as_deref()
        .is_some_and(|key| key.starts_with(ASLEEP_KEY_PREFIX) || key.starts_with(SPAWN_KEY_PREFIX))
}

/// Whether the kernel writes `run`'s row itself after enqueueing it, so no hook may write an
/// inferred one: a run `spawn` enqueued, or one a trigger in `triggers` fired, keyed
/// `<event key>#<trigger name>` ([`keys::trigger`](crate::keys::trigger)). A claim can
/// overtake that write, and an inferred row written then would replace the recorded one.
pub(crate) fn writes_own_row(run: &RunRecord, triggers: &BTreeSet<String>) -> bool {
    spawned_by_kernel(run)
        || (run.target != KERNEL_WAKE_TARGET
            && run
                .idempotency_key
                .as_deref()
                .and_then(|key| key.rsplit_once('#'))
                .is_some_and(|(_, trigger)| triggers.contains(trigger)))
}

/// Whether a claim of `run` must park it before its first model call (K-I2): its process
/// sleeps until a wake consumes its wake set. The process row says so once `spawn` wrote
/// it, and the run's key until then. A run can become claimable without a wake — a freeze
/// hands a claimed run back, a resume after a drain wakes what the drain parked, a lost
/// lease is recovered — so neither a checkpoint nor a cleared sleep request means the
/// process was woken; only the consumed wake set does, and every wake clears it before it
/// makes the run claimable. A process [held by admission](held_by_admission) is the
/// exception: admission decides on it again instead.
pub(crate) fn asleep_at_claim(run: &RunRecord, stored: Option<&ProcessMeta>) -> bool {
    match stored {
        Some(meta) => meta.wake.is_some(),
        None => {
            run.idempotency_key.as_deref().is_some_and(|key| key.starts_with(ASLEEP_KEY_PREFIX))
        }
    }
}

/// Whether admission holds `meta` (spec 3.2 §7.9): it carries the [`ADMISSION_LABEL`] and
/// the wake set admission gave it, and nothing suspended it. Such a process is deferred, not
/// waiting for an event: any claim of it decides on it again, so a changed setting releases
/// it by waking its run alone, whether it has parked yet or not (spec 3.3 §10 K3).
pub(crate) fn held_by_admission(meta: &ProcessMeta) -> bool {
    meta.wake.is_some() && meta.suspended_by.is_none() && meta.labels.contains_key(ADMISSION_LABEL)
}

/// How often, and how many times, a claim waits for the row of a run `spawn` enqueued.
const SPAWN_ROW_POLL: std::time::Duration = std::time::Duration::from_millis(20);
const SPAWN_ROW_POLLS: usize = 50;

/// How many spend samples a running process keeps for its burn rate.
const SAMPLES: usize = 64;
/// How many queued maintenance runs the tick's probe reads, oldest first.
const EXTERNAL_WAKE_PROBE: usize = 8;

/// What this replica knows about a process its own workers run.
#[derive(Debug, Clone)]
pub(crate) struct LocalRun {
    pub(crate) run: RunRecord,
    pub(crate) partition: PartitionId,
    pub(crate) claimed_at: DateTime<Utc>,
    /// The hosted agent's usage counter for the run's session when the claim started.
    pub(crate) baseline: Option<RunUsage>,
    /// The counter at the last attribution.
    pub(crate) last: Option<RunUsage>,
    /// Spend attributed to this claim so far, and when, for the burn rate.
    pub(crate) samples: VecDeque<(DateTime<Utc>, u64)>,
    pub(crate) attributed: u64,
    /// What the claim's agent turn used, counted from the usage its events carry, for a
    /// hosted agent whose progress source keeps no counter (a plain `Runner`).
    pub(crate) turn: RunUsage,
    /// The part of `turn` already attributed.
    pub(crate) turn_attributed: RunUsage,
}

/// What every kernel part shares: the stores, the bus, the kill switch, the workers.
pub(crate) struct Supervision {
    pub(crate) app_name: String,
    pub(crate) kernel_id: String,
    pub(crate) queues: Vec<QueueClass>,
    pub(crate) runs: Arc<dyn DurableRunStore>,
    pub(crate) processes: Arc<dyn ProcessStore>,
    pub(crate) bus: EventBus,
    pub(crate) governance: GovernanceControl,
    pub(crate) audit: Option<AuditTrail>,
    pub(crate) sessions: Option<Arc<dyn SessionService>>,
    pub(crate) progress: Option<Arc<dyn ProgressSource>>,
    /// Each partition's own progress source (spec 3.2 §7.8,
    /// [`KernelBuilder::partition_progress_source`](crate::KernelBuilder::partition_progress_source)).
    pub(crate) partition_progress: BTreeMap<PartitionId, Arc<dyn ProgressSource>>,
    /// One worker per queue class, set once the hooks that reference this state exist.
    pub(crate) workers: OnceLock<Vec<(QueueClass, DurableWorker)>>,
    /// The claim loops while the kernel runs.
    pub(crate) handles: Mutex<Vec<(QueueClass, WorkerHandle)>>,
    pub(crate) draining: AtomicBool,
    pub(crate) local: Mutex<HashMap<String, LocalRun>>,
    /// Signalled when a process may need a file root watched that no source covers yet.
    pub(crate) roots_changed: tokio::sync::Notify,
    /// One file wake source per watched root, while the kernel runs (spec 3.2 §7.4).
    pub(crate) watched: Mutex<BTreeMap<PathBuf, tokio::task::JoinHandle<()>>>,
    /// The kernel's clock ([`KernelConfig::clock`](crate::KernelConfig::clock)).
    pub(crate) clock: Arc<dyn Clock>,
    /// Every partition's proactivity limits (spec 3.2 §7.5).
    pub(crate) proactivity: ProactivityPolicy,
    /// The kernel's quiet hours, beside the proactivity policy's (spec 3.2 §7.2, §7.9).
    pub(crate) quiet_hours: Option<QuietHours>,
    /// Every partition's quotas (spec 3.2 §7.9).
    pub(crate) quotas: Quotas,
    /// The agenda's items (spec 3.2 §7.5).
    pub(crate) agenda: Arc<dyn AgendaStore>,
    /// Serializes admission decisions on this replica, so two claims never both take the
    /// partition's last slot.
    pub(crate) admission: tokio::sync::Mutex<()>,
    /// The standing rules the daily review reads (spec 3.2 §9.1).
    pub(crate) rule_source: Option<Arc<dyn crate::review::StandingRuleSource>>,
    /// The UTC day this replica last reviewed.
    pub(crate) reviewed: Mutex<Option<chrono::NaiveDate>>,
    /// The cron expressions of sleepers the kernel fires itself, each with where this
    /// replica stands on it (spec 3.2 §7.4).
    pub(crate) crons: Mutex<BTreeMap<String, CronWatch>>,
    /// Where the cron wakes of sleepers keep their cursors, under `kernel:<expression>`
    /// ([`KernelBuilder::watermark_store`](crate::KernelBuilder::watermark_store)).
    pub(crate) watermarks: Arc<dyn crate::watermark::CronWatermarkStore>,
    /// When this kernel's first maintenance pass since its start listed the sleepers:
    /// occurrences after a cursor and up to then came due while no kernel fired them.
    pub(crate) cron_started: Mutex<Option<DateTime<Utc>>>,
    /// Delays each cron tick of sleepers, salted with the application name
    /// ([`KernelConfig::cron_jitter`](crate::KernelConfig::cron_jitter)).
    pub(crate) cron_jitter: Option<adk_core::schedule::Jitter>,
    /// The names of the router's [`Trigger`](crate::Trigger)s, whose runs the router
    /// records itself.
    pub(crate) triggers: BTreeSet<String>,
    /// The daily baseline pass the maintenance tick enqueues (spec 3.2 §7.10).
    #[cfg(feature = "proc-baselines")]
    pub(crate) proc: Option<Arc<crate::proc::baselines::ProcRuntime>>,
    /// The partition each partition agent's target runs (spec 3.2 §7.8,
    /// [`KernelBuilder::partition_agent`](crate::KernelBuilder::partition_agent)), and the
    /// agents a person added, in their areas (spec 3.3 §5.7).
    pub(crate) targets: Arc<crate::agents::Targets>,
    /// The partition table, whose run grants a finished run releases (spec 3.2 §13 Q3).
    pub(crate) partitions: crate::partition::Partitions,
    /// The settings a person changed while the kernel runs, which admission reads in place
    /// of the start ones (spec 3.3 §10 K3).
    pub(crate) control: Arc<dyn crate::control::ControlStore>,
}

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

impl Supervision {
    /// The progress source of a process of `target` in `partition`: an added agent's own
    /// (spec 3.3 §5.7), else the partition's, else the main one (spec 3.2 §7.8).
    pub(crate) fn progress_for(
        &self,
        target: &str,
        partition: &PartitionId,
    ) -> Option<Arc<dyn ProgressSource>> {
        self.targets
            .progress_of(target)
            .or_else(|| self.partition_progress.get(partition).or(self.progress.as_ref()).cloned())
    }

    /// Every distinct progress source, the main one first.
    pub(crate) fn progress_sources(&self) -> Vec<Arc<dyn ProgressSource>> {
        let mut sources: Vec<Arc<dyn ProgressSource>> = Vec::new();
        for source in self.progress.iter().chain(self.partition_progress.values()) {
            if !sources.iter().any(|seen| Arc::ptr_eq(seen, source)) {
                sources.push(Arc::clone(source));
            }
        }
        sources
    }

    /// The side table a run gets when nothing recorded one: the kind read from its target
    /// and idempotency key, in the default partition.
    pub(crate) fn infer_meta(run: &RunRecord) -> ProcessMeta {
        let key = run.idempotency_key.as_deref().unwrap_or_default();
        let source = |prefix: &str| {
            key.strip_prefix(prefix)
                .and_then(|rest| rest.split(':').next())
                .map(|name| format!("{}{name}", prefix))
        };
        let kind = if run.target == KERNEL_WAKE_TARGET || run.queue == QueueClass::MAINTENANCE {
            ProcessKind::Maintenance
        } else if run.target == GOAL_TARGET {
            ProcessKind::Goal
        } else if run.input.get(adk_core::subagent::SUBAGENT_INPUT_KEY).is_some() {
            ProcessKind::Subagent
        } else if key.starts_with("schedule:") {
            ProcessKind::Scheduled
        } else if let Some(source) =
            ["hook:", "queue:", "cron:", "file:", "run:"].iter().find_map(|prefix| source(prefix))
        {
            ProcessKind::Triggered { source }
        } else {
            ProcessKind::Interactive
        };
        ProcessMeta::new(run.run_id.clone(), QueueClass::from_name(&run.queue), kind)
    }

    /// [`infer_meta`](Self::infer_meta) in the partition `run`'s target is bound to (spec
    /// 3.2 §7.8).
    pub(crate) fn inferred_meta(&self, run: &RunRecord) -> ProcessMeta {
        let mut meta = Self::infer_meta(run);
        if let Some(partition) = self.targets.partition_of(&run.target) {
            meta.partition = partition;
        }
        meta
    }

    /// The partition a process of `target` runs in when `asked` is requested: the target's
    /// partition when it is bound to one and `asked` is it or the default, `asked` for a
    /// target bound to none (spec 3.2 §7.8, K-I15).
    ///
    /// # Errors
    ///
    /// [`KernelError::Config`](crate::KernelError::Config) when `target` runs another
    /// partition's agent.
    pub(crate) fn partition_for(
        &self,
        target: &str,
        asked: PartitionId,
    ) -> crate::error::Result<PartitionId> {
        match self.targets.partition_of(target) {
            None => Ok(asked),
            Some(bound) if bound == asked || asked.as_str() == PartitionId::DEFAULT => Ok(bound),
            Some(bound) => Err(crate::error::KernelError::Config {
                reason: format!(
                    "target `{target}` runs the agent of partition `{bound}`, so a process of \
                     partition `{asked}` cannot run it; spawn it with that partition's target \
                     (spec 3.2 K-I15)"
                ),
            }),
        }
    }

    /// Refuses a run of a partition agent's target that continues a session another
    /// partition's agent ran, so no agent reads another partition's transcript (spec 3.2
    /// K-I15).
    ///
    /// # Errors
    ///
    /// [`KernelError::Config`](crate::KernelError::Config) naming the other partition, or
    /// the run store's error.
    pub(crate) async fn check_session_partition(
        &self,
        run: &adk_core::NewRun,
        partition: &PartitionId,
    ) -> crate::error::Result<()> {
        let Some(session) = &run.session_id else { return Ok(()) };
        if !self.targets.binds_partitions() {
            return Ok(());
        }
        let filter = adk_core::RunFilter::default()
            .app_name(&run.app_name)
            .user_id(&run.user_id)
            .session_id(session);
        for earlier in self.runs.list(filter).await? {
            if let Some(other) = self.targets.partition_of(&earlier.target)
                && other != *partition
            {
                return Err(crate::error::KernelError::Config {
                    reason: format!(
                        "session `{session}` belongs to partition `{other}`; a process of \
                         partition `{partition}` cannot continue it (spec 3.2 K-I15)"
                    ),
                });
            }
        }
        Ok(())
    }

    /// The stored side table of `run`, or `None`.
    pub(crate) async fn stored_meta(&self, run: &RunRecord) -> Option<ProcessMeta> {
        match self.processes.get(&run.run_id).await {
            Ok(meta) => meta,
            Err(error) => {
                tracing::warn!(run.id = %run.run_id, error = %error, "could not read the process side table");
                None
            }
        }
    }

    /// Writes `meta`, logging a failure: a hook cannot fail the attempt it observes.
    pub(crate) async fn put_meta(&self, meta: ProcessMeta) {
        let pid = meta.pid.clone();
        if let Err(error) = self.processes.put(meta).await {
            tracing::warn!(process.pid = %pid, error = %error, "could not write the process side table");
        }
    }

    /// Publishes `event` on the bus.
    pub(crate) fn publish(&self, event: KernelEvent) {
        self.bus.publish(event);
    }

    /// The actor of a record about `run`: its acting context, on this replica.
    pub(crate) fn actor_for(&self, run: Option<&RunRecord>) -> AuditActor {
        let mut actor = match run.and_then(|run| run.principal.as_ref()) {
            Some(principal) => AuditActor::new(principal, ""),
            None => AuditActor::default(),
        };
        actor.instance = Some(self.kernel_id.clone());
        actor
    }

    /// Writes a `kernel.*` audit record, when the kernel has a trail (spec 3.2 §9.3).
    pub(crate) async fn record(&self, action: &str, actor: AuditActor, fields: Value) {
        let Some(trail) = &self.audit else { return };
        let mut draft = AuditRecordDraft::new(
            AuditRecordKind::Other(audit::KERNEL_AUDIT_KIND.to_string()),
            action,
        )
        .actor(actor);
        if let Value::Object(fields) = fields {
            for (key, value) in fields {
                draft = draft.ext(key, value);
            }
        }
        if let Err(error) = trail.record(draft).await {
            tracing::warn!(audit.action = action, error = %error, "could not write a kernel audit record");
        }
    }

    /// The fields of a `kernel.process.*` record (spec 3.2 §9.3).
    pub(crate) fn process_fields(
        meta: &ProcessMeta,
        reason: Option<&str>,
        by: Option<&str>,
    ) -> Value {
        json!({
            "pid": meta.pid,
            "parent": meta.parent,
            "partition": meta.partition,
            "queue": meta.queue,
            "kind": meta.kind,
            "wake": meta.wake,
            "suspendedBy": meta.suspended_by,
            "reason": reason,
            "by": by,
        })
    }

    /// The workers of every queue class.
    pub(crate) fn workers(&self) -> &[(QueueClass, DurableWorker)] {
        self.workers.get().map(Vec::as_slice).unwrap_or_default()
    }

    /// The lease owner names this replica's workers write.
    pub(crate) fn local_worker_ids(&self) -> Vec<String> {
        self.workers().iter().map(|(_, worker)| worker.config().worker_id.clone()).collect()
    }

    /// Asks the attempt of `pid` running on this replica to stop at its next model-call
    /// boundary; `false` when none of this replica's workers runs it.
    pub(crate) fn suspend_local(&self, pid: &str) -> bool {
        self.workers().iter().any(|(_, worker)| worker.suspend(pid))
    }

    /// The roots this kernel watches for file wakes.
    #[cfg(test)]
    pub(crate) fn watched_roots(&self) -> Vec<PathBuf> {
        self.watched.lock().unwrap_or_else(|e| e.into_inner()).keys().cloned().collect()
    }

    /// Makes every claim loop try a claim now.
    pub(crate) fn nudge(&self) {
        for (_, handle) in self.handles.lock().unwrap_or_else(|e| e.into_inner()).iter() {
            handle.wake();
        }
    }

    /// Makes every in-flight attempt heartbeat now, so a cancellation is seen at once.
    pub(crate) fn heartbeat_now(&self) {
        for (_, handle) in self.handles.lock().unwrap_or_else(|e| e.into_inner()).iter() {
            handle.heartbeat_now();
        }
    }

    /// Whether a `kernel.wake` run is due on the maintenance queue: one the worker would
    /// claim at its next poll. Reads at most a few rows, oldest first.
    pub(crate) async fn external_wakes_due(&self) -> bool {
        if !self.queues.iter().any(|queue| queue.as_str() == QueueClass::MAINTENANCE) {
            return false;
        }
        let filter = RunFilter::default()
            .queue(QueueClass::MAINTENANCE)
            .states([RunState::Queued])
            .limit(EXTERNAL_WAKE_PROBE);
        let now = self.clock.now();
        match self.runs.list(filter).await {
            Ok(queued) => queued.iter().any(|run| {
                run.target == KERNEL_WAKE_TARGET && run.not_before.is_none_or(|at| at <= now)
            }),
            Err(error) => {
                tracing::debug!(error = %error, "could not look for queued wake runs");
                false
            }
        }
    }

    /// The running runs of the kernel's queues.
    pub(crate) async fn running(&self) -> Vec<RunRecord> {
        let mut running = Vec::new();
        for queue in &self.queues {
            let filter = RunFilter::default().queue(queue.as_str()).states([RunState::Running]);
            match self.runs.list(filter).await {
                Ok(runs) => running.extend(runs),
                Err(error) => {
                    tracing::warn!(queue = %queue, error = %error, "could not list running runs")
                }
            }
        }
        running
    }

    /// Wakes every `Waiting` process parked by `suspension` (a freeze or a drain) and clears
    /// the mark; returns how many it woke. A manual or preempted suspension is never woken
    /// here (K-I21).
    pub(crate) async fn wake_suspended(&self, suspension: &Suspension) -> usize {
        let metas = match self.processes.list(&ProcessFilter::default()).await {
            Ok(metas) => metas,
            Err(error) => {
                tracing::warn!(error = %error, "could not list processes to wake");
                return 0;
            }
        };
        let mut woken = 0;
        for mut meta in
            metas.into_iter().filter(|meta| meta.suspended_by.as_ref() == Some(suspension))
        {
            let run = match self.runs.get(&meta.pid).await {
                Ok(Some(run)) => run,
                Ok(None) => continue,
                Err(error) => {
                    tracing::warn!(process.pid = %meta.pid, error = %error, "could not read a parked run");
                    continue;
                }
            };
            if run.state == RunState::Waiting {
                match self.runs.wake(&meta.pid).await {
                    Ok(()) => woken += 1,
                    Err(error) => {
                        tracing::warn!(process.pid = %meta.pid, error = %error, "could not wake a parked run");
                        continue;
                    }
                }
            }
            if run.state != RunState::Running {
                meta.suspended_by = None;
                self.put_meta(meta).await;
            }
        }
        if woken > 0 {
            self.nudge();
        }
        woken
    }

    /// Asks this replica's workers to suspend every running process with a pending request
    /// they lease, and clears requests whose run already parked or ended (spec 3.2 §7.3).
    pub(crate) async fn mirror_suspend_requests(&self) {
        let pending =
            match self.processes.list(&ProcessFilter::default().suspend_requested(true)).await {
                Ok(pending) => pending,
                Err(error) => {
                    tracing::debug!(error = %error, "could not list pending suspensions");
                    return;
                }
            };
        if pending.is_empty() {
            return;
        }
        let ours = self.local_worker_ids();
        for meta in pending {
            let Ok(Some(run)) = self.runs.get(&meta.pid).await else { continue };
            match run.state {
                RunState::Running => {
                    if run.lease_owner.as_ref().is_some_and(|owner| ours.contains(owner)) {
                        self.suspend_local(&meta.pid);
                    }
                }
                // A queued run parks when it is claimed.
                RunState::Queued => {}
                _ => {
                    if let Err(error) = self.processes.set_suspend_requested(&meta.pid, false).await
                    {
                        tracing::debug!(process.pid = %meta.pid, error = %error, "could not clear a suspension request");
                    }
                }
            }
        }
    }

    /// Attributes the spend the hosted agent counted since the last attribution to `pid`,
    /// on today's row (spec 3.2 §7.10): the counter of its [`ProgressSource`] when that
    /// keeps one, else the usage the turn's events carried.
    pub(crate) async fn attribute(&self, pid: &str) {
        let Some(local) = self.local.lock().unwrap_or_else(|e| e.into_inner()).get(pid).cloned()
        else {
            return;
        };
        let reading = match self.progress_for(&local.run.target, &local.partition) {
            Some(progress) => match progress.usage(&local.run).await {
                Ok(reading) => reading,
                Err(error) => {
                    tracing::debug!(process.pid = pid, error = %error, "could not read the run's usage");
                    return;
                }
            },
            None => None,
        };
        let counted = local.last.is_some() || local.baseline.is_some();
        let (usage, previous, from_source) = match reading {
            // What the turn's events already attributed is part of the source's first reading.
            Some(usage) => {
                let previous = local
                    .last
                    .clone()
                    .or(local.baseline.clone())
                    .unwrap_or_else(|| local.turn_attributed.clone());
                (usage, previous, true)
            }
            None if !counted => (local.turn.clone(), local.turn_attributed.clone(), false),
            None => return,
        };
        // A counter that went down was restarted with the agent; everything since counts.
        let since = |now: u64, before: u64| if now >= before { now - before } else { now };
        let units: UsageVector = usage
            .units
            .iter()
            .map(|(unit, amount)| (unit, since(amount, previous.units.get(unit))))
            .collect();
        let delta = SpendDelta::new(since(usage.cost_micro_usd, previous.cost_micro_usd))
            .with_model_calls(since(usage.model_calls, previous.model_calls))
            .with_total_tokens(since(usage.total_tokens, previous.total_tokens))
            .with_units(units);
        let now = self.clock.now();
        {
            let mut table = self.local.lock().unwrap_or_else(|e| e.into_inner());
            if let Some(entry) = table.get_mut(pid) {
                if from_source {
                    entry.last = Some(usage);
                } else {
                    entry.turn_attributed = usage;
                }
                entry.attributed = entry.attributed.saturating_add(delta.micro_usd);
                entry.samples.push_back((now, entry.attributed));
                if entry.samples.len() > SAMPLES {
                    entry.samples.pop_front();
                }
            }
        }
        if delta.is_empty() {
            return;
        }
        if let Err(error) =
            self.processes.add_spend(pid, &local.partition, now.date_naive(), delta.clone()).await
        {
            tracing::warn!(process.pid = pid, error = %error, "could not record the process's spend");
        }
        let mut key = SpendKey::org(self.app_name.clone());
        if let Some(principal) = &local.run.principal {
            key = key.with_agent(principal.agent.id.to_string());
        }
        self.publish(KernelEvent::Spend { partition: local.partition.clone(), key, delta });
    }

    /// Attributes the spend of every process this replica runs.
    pub(crate) async fn attribute_all(&self) {
        let pids: Vec<String> =
            self.local.lock().unwrap_or_else(|e| e.into_inner()).keys().cloned().collect();
        for pid in pids {
            self.attribute(&pid).await;
        }
    }

    /// Ends this replica's watch of `pid`, attributing what it spent since the last tick.
    pub(crate) async fn release(&self, pid: &str) {
        self.attribute(pid).await;
        self.local.lock().unwrap_or_else(|e| e.into_inner()).remove(pid);
    }
}

/// The label of a process spawned asleep on a wake set with a cron wake: the wake set, as
/// JSON, its successor sleeps on (spec 3.2 §7.4, §8.1 `adk-rust wake --cron`).
pub(crate) const RECUR_LABEL: &str = "kernel.recur";

/// The label a process deferred by admission carries: `quiet_hours`, `budget`, `quota`, or
/// [`AGENT_LIMIT_HOLD`] (spec 3.2 §7.9, spec 3.3 §5.7).
pub(crate) const ADMISSION_LABEL: &str = "kernel.admission";

/// The [`ADMISSION_LABEL`] of unattended work its added agent's own daily limit holds (spec
/// 3.3 §5.7).
pub(crate) const AGENT_LIMIT_HOLD: &str = "the agent's daily limit";

/// Whether `run` is one of the kernel's own `kernel.wake` runs, which admission never
/// counts or holds.
fn is_wake_run(run: &RunRecord) -> bool {
    run.target == KERNEL_WAKE_TARGET
}

/// How admission treats a process (spec 3.2 §7.9): a human turn on the `interactive`
/// queue; agenda follow-ups and the kernel's maintenance processes as proactive; the rest
/// as background.
pub(crate) fn class_of(run: &RunRecord, meta: &ProcessMeta) -> ProcessClass {
    match meta.kind {
        ProcessKind::Interactive => ProcessClass::Interactive,
        ProcessKind::Proposed | ProcessKind::Maintenance => ProcessClass::Proactive,
        _ if run.queue == QueueClass::INTERACTIVE => ProcessClass::Interactive,
        _ => ProcessClass::Background,
    }
}

/// Totals read as spent in full, for a store that cannot be read: admission then holds work
/// instead of letting it spend past a cap.
fn exhausted() -> UsageTotals {
    UsageTotals {
        micro_usd: u64::MAX,
        model_calls: u64::MAX,
        total_tokens: u64::MAX,
        units: UsageUnit::ALL.into_iter().map(|unit| (unit, u64::MAX)).collect(),
    }
}

impl Supervision {
    /// The partition's usage today — spend, model calls, tokens and units — from the
    /// kernel's daily totals.
    pub(crate) async fn partition_usage(&self, partition: &PartitionId) -> UsageTotals {
        let today = self.clock.now().date_naive();
        let scope = crate::stores::SpendScope::Partition(partition.clone());
        self.processes.usage(&scope, today, today).await.unwrap_or_else(|error| {
            tracing::warn!(partition = %partition, error = %error, "could not read the partition's usage; treating it as spent");
            exhausted()
        })
    }

    /// What the added agent `target` spent today, in micro-USD: the daily totals of its
    /// processes in `partition` (spec 3.3 §5.7).
    pub(crate) async fn agent_spent(&self, target: &str, partition: &PartitionId) -> u64 {
        let today = self.clock.now().date_naive();
        let filter = ProcessFilter::default().partition(partition.clone());
        let Ok(metas) = self.processes.list(&filter).await else { return u64::MAX };
        let mut spent: u64 = 0;
        for meta in metas {
            if !matches!(self.runs.get(&meta.pid).await, Ok(Some(run)) if run.target == target) {
                continue;
            }
            let scope = crate::stores::SpendScope::Process(meta.pid.clone());
            spent = spent.saturating_add(
                self.processes.spend(&scope, today, today).await.unwrap_or(u64::MAX),
            );
        }
        spent
    }

    /// The partition's proactive usage today: the daily totals of its agenda and maintenance
    /// processes — spend, model calls and tokens — that the daily proactive budget caps.
    pub(crate) async fn proactive_usage(&self, partition: &PartitionId) -> UsageTotals {
        let today = self.clock.now().date_naive();
        let metas = match self
            .processes
            .list(&ProcessFilter::default().partition(partition.clone()))
            .await
        {
            Ok(metas) => metas,
            Err(error) => {
                tracing::warn!(partition = %partition, error = %error, "could not list the partition's processes; treating its budget as spent");
                return exhausted();
            }
        };
        let mut total = UsageTotals::default();
        for meta in metas
            .iter()
            .filter(|meta| matches!(meta.kind, ProcessKind::Proposed | ProcessKind::Maintenance))
        {
            let scope = crate::stores::SpendScope::Process(meta.pid.clone());
            match self.processes.usage(&scope, today, today).await {
                Ok(used) => total.merge(&used),
                Err(error) => {
                    tracing::warn!(process.pid = %meta.pid, error = %error, "could not read a process's usage; treating the budget as spent");
                    return exhausted();
                }
            }
        }
        total
    }

    /// The processes of `partition` queued on the kernel's queues, its `kernel.wake` runs
    /// aside (spec 3.2 §7.9 `max_queued`).
    pub(crate) async fn queued_in(&self, partition: &PartitionId) -> usize {
        let mut queued = 0;
        for queue in &self.queues {
            let filter = RunFilter::default().queue(queue.as_str()).states([RunState::Queued]);
            let Ok(runs) = self.runs.list(filter).await else { continue };
            for run in runs.iter().filter(|run| !is_wake_run(run)) {
                let meta = self.stored_meta(run).await.unwrap_or_else(|| Self::infer_meta(run));
                if meta.partition == *partition {
                    queued += 1;
                }
            }
        }
        queued
    }

    /// The cost cap of the daily proactive budget.
    fn daily_budget_cost(&self) -> Option<u64> {
        self.proactivity.daily_budget.max_cost_micro_usd
    }

    /// The model-call and token caps of the daily proactive budget.
    fn daily_budget_calls_and_tokens(&self) -> (Option<u64>, Option<u64>) {
        let budget = &self.proactivity.daily_budget;
        (budget.max_model_calls, budget.max_total_tokens)
    }

    /// Whether the daily proactive budget sets a cap admission applies.
    fn daily_budget_caps(&self) -> bool {
        self.daily_budget_cost().is_some() || self.daily_budget_calls_and_tokens() != (None, None)
    }

    /// Every quiet-hours window that applies in `partition`: the kernel's, the proactivity
    /// policy's and the partition's own (spec 3.2 §7.8, §7.9), the person's changed window
    /// in place of the kernel's and the partition's (spec 3.3 §10 K3).
    pub(crate) async fn quiet_windows(&self, partition: &PartitionId) -> Vec<QuietHours> {
        let record = self.partition_record(partition).await;
        let changed = self.changed_settings(partition).await;
        crate::control::settings::quiet_windows(
            self.quiet_hours,
            self.proactivity.quiet_hours,
            record.as_ref(),
            changed.as_ref(),
        )
    }

    /// The quotas of `partition`: the kernel's, each lowered by the partition's own (spec
    /// 3.2 §7.8, §7.9), and the person's changed daily limit in place of both caps (spec
    /// 3.3 §10 K3).
    pub(crate) async fn quotas_of(&self, partition: &PartitionId) -> Quotas {
        let mut quotas = match self.partition_record(partition).await {
            Some(record) => self.quotas.stricter(&record.quotas),
            None => self.quotas.clone(),
        };
        let changed = self.changed_settings(partition).await;
        quotas.daily_spend_cap =
            crate::control::settings::spend_cap(quotas.daily_spend_cap, changed.as_ref());
        quotas
    }

    /// The settings the person changed for `partition` while the kernel runs: those read
    /// last when the store fails, and `None` when it never answered, so the start settings
    /// apply (spec 3.3 §10 K3).
    async fn changed_settings(
        &self,
        partition: &PartitionId,
    ) -> Option<crate::control::AreaSettings> {
        match self.control.area_settings(partition).await {
            Ok(settings) => settings,
            Err(error) => {
                tracing::warn!(partition = %partition, error = %error, "could not read the changed settings; the start settings apply");
                None
            }
        }
    }

    /// The mode the kernel's own work in `partition` runs in: `start`, the mode the area's
    /// harness started in, or the person's changed mode when that is stricter, so a mode
    /// tightened while the kernel runs holds the overnight tidy-up and the kernel's
    /// questions back (spec 3.3 §10 K3).
    pub(crate) async fn area_mode(
        &self,
        partition: &PartitionId,
        start: crate::partition::PartitionMode,
    ) -> crate::partition::PartitionMode {
        let changed = self.changed_settings(partition).await;
        match changed.as_ref().and_then(|settings| settings.mode_over(Some(start))) {
            Some(change) => crate::control::settings::stricter(start, change.value),
            None => start,
        }
    }

    /// Whether the kernel or a partition caps the processes running at once.
    pub(crate) async fn caps_running(&self) -> bool {
        self.quotas.max_running.is_some()
            || self.partitions.list().await.is_ok_and(|partitions| {
                partitions.iter().any(|partition| partition.quotas.max_running.is_some())
            })
    }

    /// The stored record of `partition`; `None` without one or when the store fails, so
    /// the kernel's own limits apply.
    async fn partition_record(
        &self,
        partition: &PartitionId,
    ) -> Option<crate::partition::Partition> {
        match self.partitions.get(partition).await {
            Ok(record) => record,
            Err(error) => {
                tracing::warn!(partition = %partition, error = %error, "could not read the partition's record; the kernel's limits apply");
                None
            }
        }
    }

    /// The earliest instant at or after `now` outside every quiet-hours window of
    /// `partition`: when a proactive process may start there (spec 3.2 §7.9).
    pub(crate) async fn quiet_release(
        &self,
        partition: &PartitionId,
        now: DateTime<Utc>,
    ) -> DateTime<Utc> {
        let quiet = self.quiet_windows(partition).await;
        let probe = AdmissionInput {
            class: ProcessClass::Proactive,
            approval_woken: false,
            now,
            quiet_hours: &quiet,
            quotas: &Quotas::default(),
            running: 0,
            preemptible: false,
            partition_spent: 0,
            partition_units: UsageVector::new(),
            proactive_spent: 0,
            proactive_calls_and_tokens: (0, 0),
            daily_budget: None,
            daily_calls_and_tokens: (None, None),
            need: 0,
        };
        match admit(&probe) {
            Admission::QuietHours { until } => until,
            _ => now,
        }
    }

    /// The admitted processes of `partition` running now, `except` excluded: their run is
    /// `Running`, it is not a `kernel.wake` run, and it is not on its way to park. Returns
    /// their count and the `background` ones a human turn may preempt, with their creation
    /// time.
    async fn admitted(
        &self,
        partition: &PartitionId,
        except: &str,
    ) -> (usize, Vec<(String, DateTime<Utc>)>) {
        let mut count = 0;
        let mut background = Vec::new();
        for run in self.running().await {
            if run.run_id == except || is_wake_run(&run) {
                continue;
            }
            let meta = self.stored_meta(&run).await.unwrap_or_else(|| Self::infer_meta(&run));
            if meta.partition != *partition
                || meta.wake.is_some()
                || meta.suspend_requested
                || meta.suspended_by.is_some()
            {
                continue;
            }
            count += 1;
            if run.queue == QueueClass::BACKGROUND {
                background.push((run.run_id.clone(), run.created_at));
            }
        }
        (count, background)
    }

    /// The cost cap of the budget of the agenda item `meta` runs; 0 without one.
    async fn item_need(&self, meta: &ProcessMeta) -> u64 {
        let Some(id) = &meta.agenda_item else { return 0 };
        match self.agenda.get(id).await {
            Ok(Some(item)) => item.budget.and_then(|budget| budget.max_cost_micro_usd).unwrap_or(0),
            _ => 0,
        }
    }

    /// Admits the claimed `run` or defers it (spec 3.2 §7.9, K-I11): a deferred process gets
    /// a wake set — the end of quiet hours, its partition's `BudgetReplenished`, or a slot —
    /// and the [`ADMISSION_LABEL`], written before the gate is released, and parks at its
    /// first model-call boundary. An interactive process that finds its partition at quota
    /// preempts the youngest background process first.
    pub(crate) async fn admit_claim(&self, run: &RunRecord, meta: &mut ProcessMeta) -> Admission {
        if is_wake_run(run) {
            return Admission::Admit;
        }
        let class = class_of(run, meta);
        // The settings are read and a hold is written under the gate that `readmit` takes, so
        // a change either reaches this decision or finds the hold it made.
        let _gate = self.admission.lock().await;
        // An added agent's own daily limit holds its unattended work until the next UTC day
        // (spec 3.3 §5.7, K-I11).
        if class != ProcessClass::Interactive
            && let Some(limit) = self.targets.daily_limit(&run.target)
            && self.agent_spent(&run.target, &meta.partition).await >= limit
        {
            let next_day = self.clock.now().date_naive() + chrono::Days::new(1);
            meta.wake = Some(WakeSet::any_of([Wake::At(
                next_day.and_time(chrono::NaiveTime::MIN).and_utc(),
            )]));
            meta.labels.insert(ADMISSION_LABEL.to_string(), AGENT_LIMIT_HOLD.to_string());
            self.put_meta(meta.clone()).await;
            self.suspend_local(&run.run_id);
            return Admission::Budget;
        }
        let quiet = self.quiet_windows(&meta.partition).await;
        let quotas = self.quotas_of(&meta.partition).await;
        let (running, background) = self.admitted(&meta.partition, &run.run_id).await;
        let budgeted = class != ProcessClass::Interactive
            && (quotas.caps_daily_usage()
                || (class == ProcessClass::Proactive && self.daily_budget_caps()));
        let (partition, proactive, need) = if budgeted {
            (
                self.partition_usage(&meta.partition).await,
                if class == ProcessClass::Proactive {
                    self.proactive_usage(&meta.partition).await
                } else {
                    UsageTotals::default()
                },
                self.item_need(meta).await,
            )
        } else {
            (UsageTotals::default(), UsageTotals::default(), 0)
        };
        let decision = admit(&AdmissionInput {
            class,
            approval_woken: matches!(
                run.checkpoint,
                Some(RunCheckpointRef::AwaitingApproval { .. })
            ),
            now: self.clock.now(),
            quiet_hours: &quiet,
            quotas: &quotas,
            running,
            preemptible: !background.is_empty(),
            partition_spent: partition.micro_usd,
            partition_units: partition.units,
            proactive_spent: proactive.micro_usd,
            proactive_calls_and_tokens: (proactive.model_calls, proactive.total_tokens),
            daily_budget: self.daily_budget_cost(),
            daily_calls_and_tokens: self.daily_budget_calls_and_tokens(),
            need,
        });
        let wake = match decision {
            Admission::Admit => {
                if meta.labels.remove(ADMISSION_LABEL).is_some() {
                    self.put_meta(meta.clone()).await;
                }
                return decision;
            }
            Admission::QuietHours { until } => Wake::At(until),
            Admission::Budget => Wake::BudgetReplenished { partition: meta.partition.clone() },
            Admission::Quota { preempt } => {
                if preempt
                    && let Some(victim) =
                        youngest_background(background.iter().map(|(pid, at)| (pid.as_str(), *at)))
                {
                    // The victim makes no further model call, so its slot is this run's now,
                    // while it winds down to its checkpoint.
                    self.preempt(victim, &run.run_id).await;
                    if meta.labels.remove(ADMISSION_LABEL).is_some() {
                        self.put_meta(meta.clone()).await;
                    }
                    return Admission::Admit;
                }
                Wake::Manual
            }
        };
        meta.wake = Some(WakeSet::any_of([wake]));
        if let Some(reason) = decision.reason() {
            meta.labels.insert(ADMISSION_LABEL.to_string(), reason.to_string());
        }
        self.put_meta(meta.clone()).await;
        self.suspend_local(&run.run_id);
        tracing::info!(process.pid = %run.run_id, admission = ?decision, "process deferred by admission");
        decision
    }

    /// Suspends `victim` at its next checkpoint for `by`, an interactive process (spec 3.2
    /// §7.9): `Waiting` with [`Suspension::Preempted`] and a `RunFinished` wake on `by`, so it
    /// is requeued when `by` finishes.
    async fn preempt(&self, victim: &str, by: &str) {
        let Ok(Some(run)) = self.runs.get(victim).await else { return };
        let mut meta = self.stored_meta(&run).await.unwrap_or_else(|| Self::infer_meta(&run));
        meta.suspended_by = Some(Suspension::Preempted { by: by.to_string() });
        meta.wake = Some(WakeSet::any_of([Wake::RunFinished { pid: by.to_string() }]));
        meta.suspend_requested = true;
        self.put_meta(meta).await;
        // Another replica's worker leases it otherwise; its mirror reads the request.
        self.suspend_local(victim);
        tracing::info!(process.pid = victim, preempted.by = by, "background process preempted");
    }

    /// Requeues the processes `by` preempted, now that it finished (spec 3.2 §7.9).
    pub(crate) async fn requeue_preempted(&self, by: Option<&str>) {
        let metas = match self.processes.list(&ProcessFilter::default()).await {
            Ok(metas) => metas,
            Err(error) => {
                tracing::debug!(error = %error, "could not list preempted processes");
                return;
            }
        };
        let mut woken = 0;
        for mut meta in metas {
            let Some(Suspension::Preempted { by: preemptor }) = &meta.suspended_by else {
                continue;
            };
            let finished = match by {
                Some(by) => preemptor == by,
                None => match self.runs.get(preemptor).await {
                    Ok(Some(run)) => run.state.is_terminal(),
                    Ok(None) => true,
                    Err(_) => false,
                },
            };
            if !finished {
                continue;
            }
            let Ok(Some(run)) = self.runs.get(&meta.pid).await else { continue };
            if run.state != RunState::Waiting {
                continue;
            }
            let preemptor = preemptor.clone();
            meta.suspended_by = None;
            meta.wake = None;
            meta.suspend_requested = false;
            self.put_meta(meta.clone()).await;
            match self.runs.wake(&meta.pid).await {
                Ok(()) => {
                    woken += 1;
                    self.record(
                        audit::PROCESS_REQUEUED,
                        self.actor_for(Some(&run)),
                        Self::process_fields(&meta, Some("preemptor finished"), Some(&preemptor)),
                    )
                    .await;
                }
                Err(error) => {
                    tracing::warn!(process.pid = %meta.pid, error = %error, "could not requeue a preempted process")
                }
            }
        }
        if woken > 0 {
            self.nudge();
        }
    }

    /// Wakes the processes admission holds for a slot, while their partition has one: a
    /// human turn first, then oldest first (spec 3.2 §7.9).
    pub(crate) async fn admit_waiters(&self) {
        let metas = match self.processes.list(&ProcessFilter::default()).await {
            Ok(metas) => metas,
            Err(error) => {
                tracing::debug!(error = %error, "could not list processes waiting for a slot");
                return;
            }
        };
        let mut waiting: BTreeMap<PartitionId, Vec<(ProcessMeta, RunRecord)>> = BTreeMap::new();
        for meta in metas {
            if meta.labels.get(ADMISSION_LABEL).map(String::as_str) != Some("quota")
                || meta.suspended_by.is_some()
            {
                continue;
            }
            let Ok(Some(run)) = self.runs.get(&meta.pid).await else { continue };
            if run.state == RunState::Waiting {
                waiting.entry(meta.partition.clone()).or_default().push((meta, run));
            }
        }
        let mut woken = 0;
        for (partition, mut waiters) in waiting {
            let quotas = self.quotas_of(&partition).await;
            let _gate = self.admission.lock().await;
            let (running, _) = self.admitted(&partition, "").await;
            let free = quotas.max_running.map_or(usize::MAX, |max| max.saturating_sub(running));
            waiters.sort_by(|(a, x), (b, y)| {
                let human = |meta: &ProcessMeta, run: &RunRecord| {
                    class_of(run, meta) != ProcessClass::Interactive
                };
                human(a, x)
                    .cmp(&human(b, y))
                    .then(x.created_at.cmp(&y.created_at))
                    .then(x.run_id.cmp(&y.run_id))
            });
            for (mut meta, run) in waiters.into_iter().take(free) {
                meta.wake = None;
                meta.labels.remove(ADMISSION_LABEL);
                self.put_meta(meta.clone()).await;
                match self.runs.wake(&run.run_id).await {
                    Ok(()) => woken += 1,
                    Err(error) => {
                        tracing::warn!(process.pid = %run.run_id, error = %error, "could not wake a process waiting for a slot")
                    }
                }
            }
        }
        if woken > 0 {
            self.nudge();
        }
    }

    /// Wakes the processes admission holds for one of `reasons` among those `filter` selects,
    /// by partition and target, so admission decides on them again under a changed setting
    /// (spec 3.3 §10 K3): one still held sleeps again, until the new end of quiet time or a
    /// budget with room. A process admission has just deferred, still on its way to park, gets
    /// a pending wake and is claimed again the moment it parks. No row changes, so a wake that
    /// fails leaves the process asleep on its own wake set, for that wake or the next change.
    /// Returns how many it woke.
    pub(crate) async fn readmit(&self, filter: &ProcessFilter, reasons: &[&str]) -> usize {
        let mut woken = 0;
        {
            // A hold decided under the old setting is written before this lists it.
            let _gate = self.admission.lock().await;
            let metas = match self.processes.list(filter).await {
                Ok(metas) => metas,
                Err(error) => {
                    tracing::warn!(error = %error, "could not list the processes a changed setting releases");
                    return 0;
                }
            };
            for meta in metas.iter().filter(|meta| {
                held_by_admission(meta)
                    && meta
                        .labels
                        .get(ADMISSION_LABEL)
                        .is_some_and(|reason| reasons.contains(&reason.as_str()))
            }) {
                let Ok(Some(run)) = self.runs.get(&meta.pid).await else { continue };
                if !matches!(run.state, RunState::Waiting | RunState::Running)
                    || filter.target.as_ref().is_some_and(|target| *target != run.target)
                {
                    continue;
                }
                match self.runs.wake(&run.run_id).await {
                    Ok(()) => woken += 1,
                    Err(error) => {
                        tracing::warn!(process.pid = %run.run_id, error = %error, "could not wake a process a changed setting releases")
                    }
                }
            }
        }
        if woken > 0 {
            self.nudge();
        }
        woken
    }

    /// The partitions with processes asleep on `BudgetReplenished` whose budget has room
    /// again (spec 3.2 §7.9) — a new UTC day, or a cap raised or lifted — each with a tag of
    /// the processes asleep there and their claims. The budget wake's key carries the tag, so
    /// one wake goes out for the work asleep now, and work held again after it gets the next.
    pub(crate) async fn replenished_partitions(&self) -> Vec<(PartitionId, String)> {
        /// The processes asleep on one partition's `BudgetReplenished`.
        struct Asleep {
            proactive: bool,
            /// Whether admission holds one of them: a cap held it, so a lifted cap frees it.
            held: bool,
            need: u64,
            claims: Vec<String>,
        }
        let metas = match self.processes.list(&ProcessFilter::default()).await {
            Ok(metas) => metas,
            Err(_) => return Vec::new(),
        };
        let mut partitions: BTreeMap<PartitionId, Asleep> = BTreeMap::new();
        for meta in metas.iter().filter(|meta| meta.suspended_by.is_none()) {
            let Some(wake) = &meta.wake else { continue };
            let budgets: Vec<&PartitionId> = wake
                .any_of
                .iter()
                .filter_map(|wake| match wake {
                    Wake::BudgetReplenished { partition } => Some(partition),
                    _ => None,
                })
                .collect();
            if budgets.is_empty() {
                continue;
            }
            // The router wakes a parked sleeper only.
            let run = match self.runs.get(&meta.pid).await {
                Ok(Some(run)) if run.state == RunState::Waiting => run,
                _ => continue,
            };
            let need = self.item_need(meta).await;
            for partition in budgets {
                let asleep = partitions.entry(partition.clone()).or_insert(Asleep {
                    proactive: false,
                    held: false,
                    need: u64::MAX,
                    claims: Vec::new(),
                });
                asleep.proactive |=
                    matches!(meta.kind, ProcessKind::Proposed | ProcessKind::Maintenance);
                asleep.held |= held_by_admission(meta);
                asleep.need = asleep.need.min(need);
                asleep.claims.push(format!("{}@{}", meta.pid, run.lease_epoch));
            }
        }
        let mut replenished = Vec::new();
        for (partition, mut asleep) in partitions {
            let budgeted = asleep.proactive && self.daily_budget_caps();
            let quotas = self.quotas_of(&partition).await;
            let room = if !quotas.caps_daily_usage() && !budgeted {
                // No cap is left. Work a cap held has room; another sleeper waits on its
                // other wakes.
                asleep.held
            } else {
                let need = asleep.need;
                let fits = |cap: Option<u64>, spent: u64| {
                    cap.is_none_or(|cap| spent.saturating_add(need.max(1)) <= cap)
                };
                let used = self.partition_usage(&partition).await;
                let mut room = fits(quotas.daily_spend_cap, used.micro_usd)
                    && quotas.daily_caps.iter().all(|(unit, max)| used.units.get(*unit) < *max);
                if budgeted {
                    let proactive = self.proactive_usage(&partition).await;
                    let (max_calls, max_tokens) = self.daily_budget_calls_and_tokens();
                    room &= fits(self.daily_budget_cost(), proactive.micro_usd)
                        && max_calls.is_none_or(|max| proactive.model_calls < max)
                        && max_tokens.is_none_or(|max| proactive.total_tokens < max);
                }
                room
            };
            if room {
                use sha2::Digest as _;

                asleep.claims.sort();
                let digest = sha2::Sha256::digest(asleep.claims.join("\n").as_bytes());
                let tag = digest[..8].iter().map(|byte| format!("{byte:02x}")).collect();
                replenished.push((partition, tag));
            }
        }
        replenished
    }

    /// Moves the stored `item` from a state of `from`'s kind to `item.state`, then publishes
    /// the change and writes its audit record; `false` when another writer moved it first
    /// (spec 3.2 §7.5, §9.3).
    pub(crate) async fn move_item(
        &self,
        item: &AgendaItem,
        from: &AgendaState,
        reason: Option<&str>,
        by: Option<&SubjectRef>,
    ) -> crate::error::Result<bool> {
        if !self.agenda.transition(item.clone(), from.kind()).await? {
            return Ok(false);
        }
        self.agenda_changed(item, from, reason, by).await;
        Ok(true)
    }

    /// Publishes an agenda change and writes the audit record its new state has.
    pub(crate) async fn agenda_changed(
        &self,
        item: &AgendaItem,
        from: &AgendaState,
        reason: Option<&str>,
        by: Option<&SubjectRef>,
    ) {
        self.publish(KernelEvent::Agenda {
            item: item.id.clone(),
            from: from.clone(),
            to: item.state.clone(),
            reason: reason.map(str::to_string),
        });
        let action = match (&item.state, from) {
            (AgendaState::Proposed, AgendaState::Proposed) => Some(audit::AGENDA_PROPOSED),
            (AgendaState::Approved, _) => Some(audit::AGENDA_APPROVED),
            (AgendaState::Rejected, _) => Some(audit::AGENDA_REJECTED),
            (AgendaState::Scheduled { .. }, _) => Some(audit::AGENDA_SCHEDULED),
            (AgendaState::Expired, _) => Some(audit::AGENDA_EXPIRED),
            _ => None,
        };
        if let Some(action) = action {
            let mut actor = match by {
                Some(subject) => AuditActor::subject(subject.clone()),
                None => AuditActor::default(),
            };
            actor.instance = Some(self.kernel_id.clone());
            let budget = item.budget.as_ref().map(|budget| {
                json!({
                    "maxModelCalls": budget.max_model_calls,
                    "maxTotalTokens": budget.max_total_tokens,
                    "maxCostMicroUsd": budget.max_cost_micro_usd,
                })
            });
            self.record(
                action,
                actor,
                json!({
                    "item": item.id,
                    "partition": item.partition,
                    "origin": item.origin.to_string(),
                    "by": by.map(|subject| subject.id.clone()).unwrap_or_else(|| "kernel".to_string()),
                    "budget": budget,
                    "reason": reason,
                }),
            )
            .await;
        }
    }

    /// Marks the agenda item `meta` runs `Running` once its process is admitted.
    pub(crate) async fn item_started(&self, meta: &ProcessMeta) {
        let Some(id) = &meta.agenda_item else { return };
        let Ok(Some(item)) = self.agenda.get(id).await else { return };
        if !matches!(item.state, AgendaState::Scheduled { .. }) {
            return;
        }
        let from = item.state.clone();
        let next = item.with_state(AgendaState::Running { pid: meta.pid.clone() });
        if let Err(error) = self.move_item(&next, &from, Some("admitted"), None).await {
            tracing::warn!(agenda.item = %id, error = %error, "could not mark an agenda item running");
        }
    }

    /// Marks the agenda item `meta` ran `Done` once its process finished.
    pub(crate) async fn item_finished(&self, meta: &ProcessMeta, state: RunState) {
        let Some(id) = &meta.agenda_item else { return };
        let Ok(Some(item)) = self.agenda.get(id).await else { return };
        if !matches!(item.state, AgendaState::Scheduled { .. } | AgendaState::Running { .. }) {
            return;
        }
        let from = item.state.clone();
        let state = serde_json::to_value(state)
            .ok()
            .and_then(|value| value.as_str().map(str::to_owned))
            .unwrap_or_default();
        let reason = format!("process {} {state}", meta.pid);
        let next = item.with_state(AgendaState::Done);
        if let Err(error) = self.move_item(&next, &from, Some(&reason), None).await {
            tracing::warn!(agenda.item = %id, error = %error, "could not mark an agenda item done");
        }
    }
}

impl Supervision {
    /// The daily review (spec 3.2 §7.4, §9.1): once per UTC day, for every partition, the
    /// standing rules without an expiry, the sleepers and the freeze-parked processes older
    /// than the review period, published and audited when there are any. Changes nothing.
    pub(crate) async fn review(&self) {
        let now = self.clock.now();
        let today = now.date_naive();
        {
            let mut reviewed = self.reviewed.lock().unwrap_or_else(|e| e.into_inner());
            if *reviewed == Some(today) {
                return;
            }
            *reviewed = Some(today);
        }
        let period = self.proactivity.review_period;
        let Some(cutoff) =
            chrono::Duration::from_std(period).ok().and_then(|age| now.checked_sub_signed(age))
        else {
            return;
        };
        let age = |since: DateTime<Utc>| (now - since).to_std().unwrap_or_default();
        let metas = match self.processes.list(&ProcessFilter::default()).await {
            Ok(metas) => metas,
            Err(error) => {
                tracing::warn!(error = %error, "could not list processes for the review");
                return;
            }
        };
        let empty = |partition: &PartitionId| crate::review::ReviewReport {
            partition: partition.clone(),
            review_period: period,
            at: now,
            rules: Vec::new(),
            sleepers: Vec::new(),
            frozen: Vec::new(),
            crossings: Vec::new(),
        };
        let mut reports: BTreeMap<PartitionId, crate::review::ReviewReport> =
            std::iter::once(PartitionId::default())
                .chain(metas.iter().map(|meta| meta.partition.clone()))
                .map(|partition| (partition.clone(), empty(&partition)))
                .collect();
        for meta in &metas {
            let Ok(Some(run)) = self.runs.get(&meta.pid).await else { continue };
            if run.state != RunState::Waiting || is_wake_run(&run) || run.updated_at > cutoff {
                continue;
            }
            let aged =
                crate::review::AgedProcess { pid: meta.pid.clone(), age: age(run.updated_at) };
            let Some(entry) = reports.get_mut(&meta.partition) else { continue };
            if meta.suspended_by == Some(Suspension::Freeze) {
                entry.frozen.push(aged);
            } else {
                entry.sleepers.push(aged);
            }
        }
        // Crossings that recur are reported in the partition they come from (spec 3.2 §7.8).
        let since = chrono::Duration::from_std(crate::review::ReviewReport::CROSSING_WINDOW)
            .ok()
            .and_then(|window| now.checked_sub_signed(window))
            .unwrap_or(now);
        match self
            .partitions
            .recurring_crossings(since, crate::review::ReviewReport::CROSSING_THRESHOLD)
            .await
        {
            Ok(recurring) => {
                for crossing in recurring {
                    reports
                        .entry(crossing.from.clone())
                        .or_insert_with(|| empty(&crossing.from))
                        .crossings
                        .push(crossing);
                }
            }
            Err(error) => {
                tracing::warn!(error = %error, "could not read the crossing ledger for the review")
            }
        }
        // Standing rules carry no partition: the default partition's report lists them.
        if let Some(source) = &self.rule_source {
            let users: std::collections::BTreeSet<String> =
                match self.runs.list(RunFilter::default().app_name(self.app_name.clone())).await {
                    Ok(runs) => runs.into_iter().map(|run| run.user_id).collect(),
                    Err(error) => {
                        tracing::warn!(error = %error, "could not list the users for the review");
                        Default::default()
                    }
                };
            let Some(entry) = reports.get_mut(&PartitionId::default()) else { return };
            for user in users {
                match source.rules(&self.app_name, &user).await {
                    Ok(rules) => entry.rules.extend(
                        rules
                            .into_iter()
                            .filter(|rule| rule.expires_at.is_none() && rule.created_at <= cutoff)
                            .map(|rule| crate::review::StaleRule {
                                id: rule.id,
                                user: rule.user,
                                age: age(rule.created_at),
                            }),
                    ),
                    Err(error) => {
                        tracing::warn!(user = %user, error = %error, "could not read standing rules for the review")
                    }
                }
            }
        }
        for report in reports.into_values().filter(|report| !report.is_empty()) {
            tracing::info!(
                partition = %report.partition,
                review.rules = report.rules.len(),
                review.sleepers = report.sleepers.len(),
                review.frozen = report.frozen.len(),
                review.crossings = report.crossings.len(),
                "review found work older than the review period or recurring crossings"
            );
            let mut fields = serde_json::to_value(&report).unwrap_or_default();
            if let Value::Object(map) = &mut fields {
                map.remove("at");
                map.insert("ask".into(), json!(report.renewal_ask()));
                map.insert("remedies".into(), json!(report.crossing_remedies()));
            }
            self.record(audit::REVIEW, self.actor_for(None), fields).await;
            self.publish(KernelEvent::Review(report));
        }
    }
}

/// Where a replica stands on one cron expression it fires for sleepers (spec 3.2 §7.4).
#[derive(Debug, Clone)]
pub(crate) struct CronWatch {
    /// Through when this replica fired the expression's occurrences as they came due.
    since: DateTime<Utc>,
    /// Whether the occurrences missed while no kernel fired them are still going out.
    catching_up: bool,
}

/// Whether `wake` holds a cron wake on `expr`.
fn names_cron(wake: Option<&WakeSet>, expr: &str) -> bool {
    wake.into_iter()
        .flat_map(|wake| wake.any_of.iter())
        .any(|wake| matches!(wake, Wake::Cron { expr: theirs, .. } if theirs == expr))
}

/// What one pass of [`Supervision::fire_crons`] read.
struct CronPass<'a> {
    router: &'a crate::wake::WakeRouter,
    /// Every process row.
    metas: &'a [ProcessMeta],
    /// This kernel's first pass since its start.
    started: DateTime<Utc>,
    now: DateTime<Utc>,
}

impl Supervision {
    /// Fires the cron wakes of sleepers on the kernel's clock (spec 3.2 §7.4), for each
    /// expression a sleeper's wake set names and no standing trigger uses (a trigger's cron
    /// expression has its registered `CronWakeSource`). Each tick is submitted as a
    /// `kernel.wake` event keyed as a cron source keys it, so replicas fire it once.
    ///
    /// The expression's cursor is kept in the kernel's [`CronWatermarkStore`] under
    /// `kernel:<expression>`, moved past each tick after it is submitted:
    ///
    /// | Occurrences | What fires |
    /// |---|---|
    /// | Due while this replica watched the expression | The latest since the last pass, to every sleeper |
    /// | After the cursor, up to this kernel's first pass since its start | The missed ones, planned with the widest `missed` policy of the expression's sleepers and ranked from the latest, one at a time once the sleepers the previous one went to sleep again, so each wakes for those its own policy picks |
    /// | Before a sleeper's series began, or due while this kernel ran and nothing slept on the expression | None |
    ///
    /// An expression with no cursor starts at its first watch. An expression no sleeper
    /// names any more is forgotten by the replica and keeps its cursor.
    ///
    /// [`CronWatermarkStore`]: crate::CronWatermarkStore
    pub(crate) async fn fire_crons(&self, router: &crate::wake::WakeRouter) {
        let now = self.clock.now();
        let triggered: BTreeSet<&str> = router
            .triggers()
            .iter()
            .filter_map(|trigger| match &trigger.wake {
                Wake::Cron { expr, .. } => Some(expr.as_str()),
                _ => None,
            })
            .collect();
        let metas = match self.processes.list(&ProcessFilter::default()).await {
            Ok(metas) => metas,
            Err(error) => {
                tracing::debug!(error = %error, "could not list the sleepers' cron wakes");
                return;
            }
        };
        let started =
            *self.cron_started.lock().unwrap_or_else(|e| e.into_inner()).get_or_insert(now);
        let mut needed: BTreeMap<String, Vec<(&ProcessMeta, MissedFirePolicy)>> = BTreeMap::new();
        for meta in &metas {
            for wake in meta.wake.iter().flat_map(|wake| wake.any_of.iter()) {
                if let Wake::Cron { expr, missed } = wake
                    && !triggered.contains(expr.as_str())
                {
                    needed.entry(expr.clone()).or_default().push((meta, *missed));
                }
            }
        }
        let pass = CronPass { router, metas: &metas, started, now };
        // Only this loop writes the map; tests read it while the pass awaits the stores.
        let mut watches = self.crons.lock().unwrap_or_else(|e| e.into_inner()).clone();
        watches.retain(|expr, _| needed.contains_key(expr));
        for (expr, sleepers) in &needed {
            let watch = watches
                .entry(expr.clone())
                .or_insert_with(|| CronWatch { since: now, catching_up: true });
            if watch.catching_up {
                self.catch_up_cron(&pass, expr, sleepers, watch).await;
            }
            let source = format!("kernel:{expr}");
            let spec = ScheduleSpec::Cron { expr: expr.clone(), timezone: "UTC".into() };
            let mut latest = None;
            let mut cursor = watch.since;
            while let Ok(Some(next)) = spec.next_after(cursor) {
                if next > now {
                    break;
                }
                latest = Some(next);
                cursor = next;
            }
            let Some(at) = latest else { continue };
            watch.since = at;
            let event = crate::wake::WakeEvent::new(
                source.clone(),
                crate::wake::keys::cron(&source, at),
                json!({ "expression": expr, "scheduled_for": at, "catch_up": false }),
            )
            .with_at(at);
            let event = match &self.cron_jitter {
                Some(jitter) => crate::wake::jittered_tick(event, &source, jitter),
                None => event,
            };
            match router.submit(&event, &self.app_name).await {
                // A catch-up still out keeps the cursor at its last missed occurrence.
                Ok(_) if !watch.catching_up => self.record_cron(&source, at).await,
                Ok(_) => {}
                Err(error) => {
                    tracing::warn!(cron = %expr, error = %error, "could not submit a cron wake");
                }
            }
        }
        *self.crons.lock().unwrap_or_else(|e| e.into_inner()) = watches;
    }

    /// Sends the next occurrence of `expr` missed while no kernel fired it, or ends the
    /// catch-up when none is left (spec 3.2 §7.4). `sleepers` are the expression's sleepers
    /// with their policies.
    ///
    /// One goes out at a time, and only once every sleeper on the expression is parked and
    /// no series on it is running, so a recurring sleeper's successor takes the next one.
    /// Every replica reads that from the stores, in an order that sees a wake another
    /// replica sent: the cursor, then the wakes not yet routed, then the sleepers.
    async fn catch_up_cron(
        &self,
        pass: &CronPass<'_>,
        expr: &str,
        sleepers: &[(&ProcessMeta, MissedFirePolicy)],
        watch: &mut CronWatch,
    ) {
        let source = format!("kernel:{expr}");
        let spec = ScheduleSpec::Cron { expr: expr.to_string(), timezone: "UTC".into() };
        let cursor = match self.watermarks.read(&source).await {
            Ok(cursor) => cursor,
            Err(error) => {
                tracing::warn!(cron = %expr, error = %error, "could not read the cron watermark");
                return;
            }
        };
        let Some(cursor) = cursor else {
            // A new expression replays nothing from before it was first watched.
            self.record_cron(&source, watch.since).await;
            watch.catching_up = false;
            return;
        };
        if self.cron_wake_unrouted(&source).await {
            return;
        }
        // A suspended sleeper wakes on no tick, so the catch-up neither waits for it nor plans
        // for it. The rows are read again: a wake routed since the pass began cleared the
        // set, and a woken run stays `Waiting` until a worker claims it.
        let mut asleep = Vec::with_capacity(sleepers.len());
        for (meta, missed) in sleepers {
            let meta = match self.processes.get(&meta.pid).await {
                Ok(Some(meta)) => meta,
                Ok(None) => continue,
                Err(error) => {
                    tracing::debug!(process.pid = %meta.pid, error = %error, "could not read a cron sleeper");
                    return;
                }
            };
            if meta.suspended_by.is_some() {
                continue;
            }
            if !names_cron(meta.wake.as_ref(), expr) {
                return;
            }
            match self.runs.get(&meta.pid).await {
                Ok(Some(run)) if run.state.is_terminal() => {}
                // Spawned asleep and not parked yet: the router would pass it by.
                Ok(Some(run)) if run.state != RunState::Waiting => return,
                Ok(Some(run)) => asleep.push((*missed, run)),
                Ok(None) => {}
                Err(error) => {
                    tracing::debug!(process.pid = %meta.pid, error = %error, "could not read a cron sleeper");
                    return;
                }
            }
        }
        if self.cron_series_running(pass, expr).await {
            return;
        }
        // A series replays nothing from before it began, and a re-armed successor
        // continues its predecessor's series.
        let mut from = cursor;
        if let Some(begins) = asleep.iter().map(|(_, run)| series_start(run)).min().flatten() {
            from = from.max(begins);
        }
        let Some(widest) =
            asleep.iter().map(|(missed, _)| *missed).reduce(crate::wake::widest_policy)
        else {
            return;
        };
        let plan = spec.next_after(from).and_then(|due| match due {
            Some(due) => plan_fires(&spec, widest, due, pass.started, DEFAULT_MISFIRE_GRACE)
                .map(|plan| plan.fire_times),
            None => Ok(Vec::new()),
        });
        let fire_times = match plan {
            Ok(fire_times) => fire_times,
            Err(error) => {
                tracing::warn!(cron = %expr, error = %error, "could not plan the missed cron occurrences");
                return;
            }
        };
        let Some(&at) = fire_times.first() else {
            // Caught up: the occurrences fired since the first watch are accounted for too.
            self.record_cron(&source, watch.since).await;
            watch.catching_up = false;
            return;
        };
        let rank = fire_times.len() - 1;
        let on_time = (pass.now - at).to_std().unwrap_or_default() <= DEFAULT_MISFIRE_GRACE;
        let event = crate::wake::WakeEvent::new(
            source.clone(),
            crate::wake::keys::cron(&source, at),
            json!({
                "expression": expr,
                "scheduled_for": at,
                "catch_up": true,
                "catch_up_rank": rank,
                "on_time": on_time,
            }),
        )
        .with_at(at);
        let picked = asleep.iter().any(|(missed, run)| {
            crate::wake::wakes_sleeper(
                &Wake::Cron { expr: expr.to_string(), missed: *missed },
                &event,
                run,
                pass.now,
            )
        });
        // An occurrence no sleeper picks is passed over.
        if picked {
            if let Err(error) = pass.router.submit(&event, &self.app_name).await {
                tracing::warn!(cron = %expr, error = %error, "could not submit a missed cron wake");
                return;
            }
            tracing::info!(cron = %expr, scheduled_for = %at, catch_up.rank = rank, "replaying a missed cron occurrence");
        }
        self.record_cron(&source, at).await;
    }

    /// Whether a `kernel.wake` run of the cron source `source` is still to be routed.
    async fn cron_wake_unrouted(&self, source: &str) -> bool {
        let prefix = format!("cron:{source}:");
        let pending = RunFilter::default()
            .queue(QueueClass::MAINTENANCE)
            .states([RunState::Queued, RunState::Running]);
        match self.runs.list(pending).await {
            Ok(runs) => runs.iter().any(|run| {
                run.idempotency_key.as_deref().is_some_and(|key| key.starts_with(&prefix))
            }),
            Err(_) => true,
        }
    }

    /// Whether a cron series on `expr` is running: its latest process was woken and has not
    /// finished, or finished within the misfire grace and is being re-armed.
    async fn cron_series_running(&self, pass: &CronPass<'_>, expr: &str) -> bool {
        let on_expr = |meta: &&ProcessMeta| {
            meta.wake.is_none()
                && names_cron(
                    meta.labels
                        .get(RECUR_LABEL)
                        .and_then(|wake| serde_json::from_str::<WakeSet>(wake).ok())
                        .as_ref(),
                    expr,
                )
        };
        for meta in pass.metas.iter().filter(on_expr) {
            // A member with a successor finished; each successor names it as its parent.
            let continued = pass.metas.iter().any(|next| {
                next.parent.as_deref() == Some(meta.pid.as_str())
                    && next.labels.contains_key(RECUR_LABEL)
            });
            if continued {
                continue;
            }
            let running = match self.runs.get(&meta.pid).await {
                Ok(Some(run)) => match run.state {
                    RunState::Cancelled => false,
                    RunState::Completed | RunState::Failed => {
                        (pass.now - run.updated_at).to_std().unwrap_or_default()
                            <= DEFAULT_MISFIRE_GRACE
                    }
                    _ => true,
                },
                Ok(None) => false,
                Err(_) => true,
            };
            if running {
                return true;
            }
        }
        false
    }

    /// Moves the cron cursor of `source` to `at`.
    async fn record_cron(&self, source: &str, at: DateTime<Utc>) {
        if let Err(error) = self.watermarks.advance(source, at).await {
            tracing::warn!(wake.source = %source, error = %error, "could not record the cron watermark");
        }
    }

    /// The cron expressions this kernel fires itself.
    #[cfg(test)]
    pub(crate) fn watched_crons(&self) -> Vec<String> {
        self.crons.lock().unwrap_or_else(|e| e.into_inner()).keys().cloned().collect()
    }
}

/// The model and tool calls of one process, read from its session (spec 3.2 §7.3, §7.10).
#[derive(Debug, Clone, Default, PartialEq)]
pub(crate) struct TranscriptCounts {
    pub(crate) model_calls: u64,
    pub(crate) tool_calls: u64,
    /// How long each answered tool call took, in seconds.
    pub(crate) tool_durations: Vec<f64>,
}

/// The session a run continues: the one its checkpoint names, its own, or the
/// `run-{run_id}` session a worker creates for a run that names none.
pub(crate) fn run_session(run: &RunRecord) -> String {
    match (&run.checkpoint, &run.session_id) {
        (
            Some(
                RunCheckpointRef::Session { session_id, .. }
                | RunCheckpointRef::AwaitingApproval { session_id, .. },
            ),
            _,
        ) => session_id.clone(),
        (_, Some(session_id)) => session_id.clone(),
        _ => format!("run-{}", run.run_id),
    }
}

/// Counts the model calls, tool calls and tool durations of `run` in its session: the
/// events from its creation on, and, for a finished run, up to its end.
pub(crate) async fn transcript_counts(
    sessions: &dyn SessionService,
    run: &RunRecord,
) -> Option<TranscriptCounts> {
    let session_id = run_session(run);
    let session = sessions
        .get(GetRequest {
            app_name: run.app_name.clone(),
            user_id: run.user_id.clone(),
            session_id,
            num_recent_events: None,
            after: Some(run.created_at),
        })
        .await
        .ok()?;
    let end = run.state.is_terminal().then_some(run.updated_at);
    let mut counts = TranscriptCounts::default();
    let mut started: HashMap<String, DateTime<Utc>> = HashMap::new();
    for event in session.events().all() {
        if event.timestamp < run.created_at || end.is_some_and(|end| event.timestamp > end) {
            continue;
        }
        let Some(content) = &event.llm_response.content else { continue };
        if content.role == "model" && !event.llm_response.partial && event.author != "user" {
            counts.model_calls += 1;
        }
        for part in &content.parts {
            match part {
                Part::FunctionCall { name, id, .. } if !event.llm_response.partial => {
                    counts.tool_calls += 1;
                    started.insert(id.clone().unwrap_or_else(|| name.clone()), event.timestamp);
                }
                Part::FunctionResponse { function_response, id, .. } => {
                    let key = id.clone().unwrap_or_else(|| function_response.name.clone());
                    if let Some(at) = started.remove(&key) {
                        let seconds =
                            (event.timestamp - at).num_milliseconds().max(0) as f64 / 1000.0;
                        counts.tool_durations.push(seconds);
                    }
                }
                _ => {}
            }
        }
    }
    Some(counts)
}

/// The worker hooks that keep the side table, the bus, spend attribution and the audit log
/// in step with every run a kernel worker moves (spec 3.2 §10 S1, §7.6, §9.3).
pub(crate) struct KernelHooks {
    pub(crate) sup: Arc<Supervision>,
    /// Hears every finished run except the kernel's own `kernel.wake` runs, whose finish
    /// would otherwise start another `kernel.wake` run, without end.
    pub(crate) run_finished: Arc<crate::wake::RunFinishedWakeSource>,
    /// The background sub-agent host, which hears its runs' claims, parks and ends.
    pub(crate) subagents: Option<adk_server::background::DurableSubagents>,
}

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

impl KernelHooks {
    /// Spawns the successor of the finished cron sleeper `run`, asleep on `wake`, keyed by
    /// the finished run so a repeated finish spawns it once.
    async fn rearm(&self, run: &RunRecord, meta: &ProcessMeta, wake: WakeSet) {
        let mut next = adk_core::NewRun::new(
            run.queue.clone(),
            run.app_name.clone(),
            run.user_id.clone(),
            run.target.clone(),
            run.input.clone(),
        )
        .with_max_attempts(run.max_attempts);
        next.session_id = run.session_id.clone();
        next.principal = run.principal.clone();
        next.authenticated = run.authenticated;
        next.idempotency_key = Some(format!("{RECUR_KEY_PREFIX}{}", run.run_id));
        let mut spec = crate::process::ProcessSpec::new(meta.kind.clone())
            .with_parent(run.run_id.clone())
            .with_partition(meta.partition.clone())
            .with_wake(wake);
        for (key, value) in &meta.labels {
            spec = spec.with_label(key.clone(), value.clone());
        }
        let table = crate::process::ProcessTable::supervised(Arc::clone(&self.sup), None);
        match table.spawn(next, spec).await {
            Ok(successor) => {
                tracing::info!(process.pid = %run.run_id, successor.pid = %successor.meta.pid, "cron sleeper re-armed")
            }
            Err(error) => {
                tracing::warn!(process.pid = %run.run_id, error = %error, "could not re-arm a cron sleeper")
            }
        }
    }
}

#[async_trait]
impl WorkerHooks for KernelHooks {
    async fn on_claim(&self, run: &RunRecord) {
        let sup = &self.sup;
        let mut stored = sup.stored_meta(run).await;
        if stored.is_none() && writes_own_row(run, &sup.triggers) && !asleep_at_claim(run, None) {
            // `spawn` or the trigger's router writes the row right after the enqueue this
            // claim may have overtaken; admission needs the row's kind and partition, so wait
            // for it briefly.
            for _ in 0..SPAWN_ROW_POLLS {
                tokio::time::sleep(SPAWN_ROW_POLL).await;
                stored = sup.stored_meta(run).await;
                if stored.is_some() {
                    break;
                }
            }
        }
        let persisted = stored.is_some();
        // A process admission held is decided on again at whatever claim reaches it, so a
        // changed setting that wakes its run, parked or still parking, meets it now.
        let held = stored.as_ref().is_some_and(held_by_admission);
        let asleep = !held && asleep_at_claim(run, stored.as_ref());
        let mut meta = stored.unwrap_or_else(|| sup.inferred_meta(run));
        let from = if meta.wake.is_some() || meta.suspended_by.is_some() || run.attempt > 1 {
            RunState::Waiting
        } else {
            RunState::Queued
        };
        // A run `spawn` enqueued or a trigger fired gets its row from the kernel, which may
        // still be writing it; an inferred row written here could replace it.
        let mut changed = !persisted && !writes_own_row(run, &sup.triggers);
        // A paused agent's run parks at once and stays paused through an unfreeze, until the
        // agent is resumed (spec 3.3 §5.7, K-I21).
        if sup.targets.is_paused(&run.target) {
            meta.suspended_by = Some(Suspension::Paused { agent: run.target.clone() });
            meta.suspend_requested = true;
            changed = true;
        }
        let mut admitted = false;
        if asleep || meta.suspend_requested || sup.draining.load(Ordering::SeqCst) {
            // A sleeper, a suspension asked while it queued, or a drain: the attempt parks
            // at its first model-call boundary.
            sup.suspend_local(&run.run_id);
        } else {
            if persisted {
                // Woken, or its suspension lifted: the wake set is already consumed.
                changed |= meta.suspended_by.take().is_some();
            }
            if held {
                // Admission writes the row: the hold it decides on now, or none.
                meta.wake = None;
            }
            if changed {
                sup.put_meta(meta.clone()).await;
                changed = false;
            }
            // Admission (spec 3.2 §7.9) writes the row itself when it defers the process.
            admitted = sup.admit_claim(run, &mut meta).await == Admission::Admit;
        }
        if changed {
            sup.put_meta(meta.clone()).await;
        }
        if admitted {
            sup.item_started(&meta).await;
        }
        let baseline = match sup.progress_for(&run.target, &meta.partition) {
            Some(progress) => progress.usage(run).await.ok().flatten(),
            None => None,
        };
        sup.local.lock().unwrap_or_else(|e| e.into_inner()).insert(
            run.run_id.clone(),
            LocalRun {
                run: run.clone(),
                partition: meta.partition.clone(),
                claimed_at: sup.clock.now(),
                baseline,
                last: None,
                samples: VecDeque::new(),
                attributed: 0,
                turn: RunUsage::default(),
                turn_attributed: RunUsage::default(),
            },
        );
        sup.publish(KernelEvent::Process {
            pid: run.run_id.clone(),
            from,
            to: RunState::Running,
            reason: Some("claimed".to_string()),
        });
        if let Some(subagents) = self.subagents_of(run) {
            subagents.run_claimed(run).await;
        }
    }

    async fn wake_input(&self, run: &RunRecord) -> Option<String> {
        crate::wake::wake_notice(self.sup.processes.as_ref(), run).await
    }

    async fn on_event(&self, run: &RunRecord, event: &adk_core::Event) {
        // A complete model response counts once; its partial chunks repeat it.
        if event.llm_response.partial || event.author == "user" {
            return;
        }
        let usage = event.llm_response.usage_metadata.as_ref();
        let tool_calls = event.tool_calls().len() as u64;
        if usage.is_none() && tool_calls == 0 {
            return;
        }
        let mut table = self.sup.local.lock().unwrap_or_else(|e| e.into_inner());
        let Some(entry) = table.get_mut(&run.run_id) else { return };
        let turn = &mut entry.turn;
        turn.tool_calls += tool_calls;
        if let Some(usage) = usage {
            let tokens = |count: i32| u64::try_from(count).unwrap_or(0);
            turn.model_calls += 1;
            turn.total_tokens = turn.total_tokens.saturating_add(tokens(usage.total_token_count));
            turn.cost_micro_usd = turn.cost_micro_usd.saturating_add(
                usage.cost.and_then(adk_core::usd_to_micro_usd).unwrap_or_default(),
            );
            turn.units.merge(
                &UsageVector::new()
                    .with(adk_core::UsageUnit::InputTokens, tokens(usage.prompt_token_count))
                    .with(adk_core::UsageUnit::OutputTokens, tokens(usage.candidates_token_count)),
            );
        }
    }

    async fn on_park(&self, run: &RunRecord, reason: ParkReason) {
        let sup = &self.sup;
        sup.release(&run.run_id).await;
        if reason == ParkReason::AwaitingApproval
            && let Some(subagents) = self.subagents_of(run)
        {
            subagents.run_parked(run).await;
        }
        let stored = sup.stored_meta(run).await;
        if stored.is_none() && writes_own_row(run, &sup.triggers) {
            // The kernel has not written the row yet; the row it writes is the run's, and
            // the 250 ms mirror clears its pending request now that the run parked.
            let label = if reason == ParkReason::Suspended { "asleep" } else { "parked" };
            sup.publish(KernelEvent::Process {
                pid: run.run_id.clone(),
                from: RunState::Running,
                to: RunState::Waiting,
                reason: Some(label.to_string()),
            });
            return;
        }
        let mut meta = stored.unwrap_or_else(|| sup.inferred_meta(run));
        let draining = sup.draining.load(Ordering::SeqCst);
        let label = match reason {
            ParkReason::Suspended => {
                meta.suspend_requested = false;
                if meta.suspended_by.is_none() {
                    if draining {
                        meta.suspended_by = Some(Suspension::Drain);
                    } else if meta.wake.is_none() {
                        meta.suspended_by = Some(Suspension::Manual);
                    }
                }
                if meta.suspended_by.is_some() { "suspended" } else { "asleep" }
            }
            ParkReason::Frozen => {
                meta.suspend_requested = false;
                meta.suspended_by = Some(Suspension::Freeze);
                "frozen"
            }
            ParkReason::AwaitingApproval => "awaiting_approval",
            ParkReason::HandedBack => "handed_back",
            _ => "parked",
        };
        sup.put_meta(meta.clone()).await;
        sup.publish(KernelEvent::Process {
            pid: run.run_id.clone(),
            from: RunState::Running,
            to: RunState::Waiting,
            reason: Some(label.to_string()),
        });
        if matches!(reason, ParkReason::Suspended | ParkReason::Frozen) {
            sup.record(
                audit::PROCESS_SUSPENDED,
                sup.actor_for(Some(run)),
                Supervision::process_fields(&meta, Some(label), None),
            )
            .await;
        }
        // A slot may have freed in the partition, and a preempted process whose preemptor
        // already finished goes back to its queue (spec 3.2 §7.9).
        if sup.caps_running().await {
            sup.admit_waiters().await;
        }
        if matches!(meta.suspended_by, Some(Suspension::Preempted { .. })) {
            sup.requeue_preempted(None).await;
        }
    }

    async fn on_finish(&self, run: &RunRecord) {
        let sup = &self.sup;
        if run.target == KERNEL_WAKE_TARGET {
            // A routed wake may have made sleepers claimable: claim them now rather than at
            // the next poll (spec 3.2 §7.4, local event to `Running` under 2 s).
            sup.nudge();
        } else {
            self.run_finished.on_finish(run).await;
        }
        sup.release(&run.run_id).await;
        // A finished run's approved crossing reads end with it (spec 3.2 §13 Q3).
        if run.state.is_terminal() {
            let finished = crate::partition::CrossingRun::Durable(run.run_id.clone());
            if let Err(error) = sup.partitions.release_run(&finished).await {
                tracing::warn!(run.id = %run.run_id, error = %error, "could not release the run's crossing grants");
            }
        }
        if run.state.is_terminal()
            && let Some(mut meta) = sup.stored_meta(run).await
            && (meta.suspend_requested || meta.suspended_by.is_some() || meta.wake.is_some())
        {
            meta.suspend_requested = false;
            meta.suspended_by = None;
            let had_wake = meta.wake.take().is_some();
            sup.put_meta(meta).await;
            if had_wake {
                sup.roots_changed.notify_one();
            }
        }
        sup.publish(KernelEvent::Process {
            pid: run.run_id.clone(),
            from: RunState::Running,
            to: run.state,
            reason: run.error.clone(),
        });
        if run.state.is_terminal() && run.target != KERNEL_WAKE_TARGET {
            // The processes this one preempted go back to their queue, and a slot freed
            // (spec 3.2 §7.9); the item this one ran is done (§7.5).
            sup.requeue_preempted(Some(&run.run_id)).await;
            if sup.caps_running().await {
                sup.admit_waiters().await;
            }
            if let Some(meta) = sup.stored_meta(run).await {
                sup.item_finished(&meta, run.state).await;
                // A cron sleeper recurs: its successor sleeps on the same wake set. A kill
                // ends the series.
                if run.state != RunState::Cancelled
                    && let Some(wake) = meta.labels.get(RECUR_LABEL)
                    && let Ok(wake) = serde_json::from_str::<WakeSet>(wake)
                {
                    self.rearm(run, &meta, wake).await;
                }
            }
        }
        #[cfg(feature = "proc")]
        if let Some(sessions) = &sup.sessions
            && run.state.is_terminal()
            && let Some(counts) = transcript_counts(sessions.as_ref(), run).await
        {
            crate::kernel::record_histograms(&run.target, &counts);
        }
        if let Some(subagents) = self.subagents_of(run) {
            subagents.run_finished(run).await;
        }
    }
}

impl KernelHooks {
    /// The sub-agent host, when `run` is one of its sub-agents' runs.
    fn subagents_of(&self, run: &RunRecord) -> Option<&adk_server::background::DurableSubagents> {
        self.subagents
            .as_ref()
            .filter(|_| run.input.get(adk_core::subagent::SUBAGENT_INPUT_KEY).is_some())
    }
}

#[cfg(test)]
mod tests {
    use adk_core::{NewRun, RunCheckpointRef};
    use serde_json::json;

    use super::*;

    fn run(queue: &str, target: &str, key: Option<&str>) -> RunRecord {
        let mut run = NewRun::new(queue, "adk-rust", "alice", target, json!({}));
        run.idempotency_key = key.map(str::to_string);
        RunRecord::queued("run-1", run, Utc::now())
    }

    #[test]
    fn the_kernel_writes_the_rows_of_runs_it_spawned_or_a_trigger_fired() {
        let triggers = BTreeSet::from(["triage".to_string()]);
        let writes = |target, key| writes_own_row(&run("background", target, key), &triggers);
        assert!(writes("adk-rust", Some("kernel:spawn:7f3a")));
        assert!(writes("adk-rust", Some("kernel:asleep:7f3a")));
        assert!(writes("adk-rust", Some("hook:github:msg_1#triage")));
        // Another trigger's name, a wake run, a submitted key or none: a hook writes the row.
        assert!(!writes("adk-rust", Some("hook:github:msg_1#nightly")));
        assert!(!writes(KERNEL_WAKE_TARGET, Some("file:/w/a#triage")));
        assert!(!writes("adk-rust", Some("hook:github:msg_1")));
        assert!(!writes("adk-rust", None));
    }

    #[test]
    fn the_kind_of_an_unrecorded_run_is_read_from_its_target_and_key() {
        let kind = |queue, target, key| Supervision::infer_meta(&run(queue, target, key)).kind;
        assert_eq!(kind("interactive", "adk-rust", None), ProcessKind::Interactive);
        assert_eq!(kind("background", GOAL_TARGET, None), ProcessKind::Goal);
        assert_eq!(
            kind("maintenance", KERNEL_WAKE_TARGET, Some("hook:gh:1")),
            ProcessKind::Maintenance
        );
        assert_eq!(
            kind("background", "adk-rust", Some("schedule:nightly:2026-10-22T02:00:00Z")),
            ProcessKind::Scheduled
        );
        assert_eq!(
            kind("background", "adk-rust", Some("hook:github:msg_1")),
            ProcessKind::Triggered { source: "hook:github".into() }
        );
        assert_eq!(
            kind("background", "adk-rust", Some("file:/w:/w/Cargo.lock:2026")),
            ProcessKind::Triggered { source: "file:/w".into() }
        );
        let meta = Supervision::infer_meta(&run("payments", "adk-rust", None));
        assert_eq!(meta.queue, QueueClass::Named("payments".into()));
        assert_eq!(meta.partition, PartitionId::default());
    }

    #[test]
    fn a_run_continues_the_session_its_checkpoint_names() {
        let mut record = run("interactive", "adk-rust", None);
        assert_eq!(run_session(&record), "run-run-1", "the worker's default session");
        record.session_id = Some("s1".into());
        assert_eq!(run_session(&record), "s1");
        record.checkpoint =
            Some(RunCheckpointRef::Session { session_id: "s2".into(), invocation_id: None });
        assert_eq!(run_session(&record), "s2");
    }
}