meerkat 0.8.17

Modular, high-performance agent harness for LLM-powered applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use chrono::Duration as ChronoDuration;
use meerkat_core::{ContentInput, Session, SessionId, skills::SkillRef, types::RenderMetadata};
use meerkat_runtime::{
    CompletionHandle, MeerkatMachine, SessionServiceRuntimeExt,
    completion::{CompletionOutcome, CompletionWaitError},
};
use meerkat_schedule::{
    DeliveryCompletion, DeliveryCompletionFailureReason, DeliveryDispatch, DeliveryFailureReason,
    DeliveryReceipt, DeliveryReceiptStage, DeliveryTerminal, HostRunnableInvocation,
    HostRunnableParams, HostRunnableTargetBinding, IdentityTargetBinding, MobTargetBinding,
    Occurrence, OccurrencePhase, RunnableProbe, ScheduleDeliveryIdentity, ScheduleDomainError,
    ScheduleDriver, ScheduleDriverConfig, ScheduleFilter, ScheduleRunnableHost, ScheduleService,
    ScheduleStoreKind, ScheduleStoreWakeMode, ScheduleTargetDelivery, ScheduleTargetProbe,
    ScheduledSessionAction, SessionMaterializationSpec, SessionTargetBinding, TargetBinding,
    TargetProbeOutcome, UpdateScheduleRequest,
};
use serde::{Deserialize, Serialize};

#[cfg(not(target_arch = "wasm32"))]
use tokio as schedule_host_tokio;
#[cfg(not(target_arch = "wasm32"))]
use tokio::sync::oneshot;
#[cfg(not(target_arch = "wasm32"))]
use tokio::task::JoinHandle;
#[cfg(target_arch = "wasm32")]
use tokio_with_wasm::alias as schedule_host_tokio;
#[cfg(target_arch = "wasm32")]
use tokio_with_wasm::alias::sync::oneshot;
#[cfg(target_arch = "wasm32")]
use tokio_with_wasm::alias::task::JoinHandle;

pub struct ScheduleHostHandle {
    shutdown_tx: Option<oneshot::Sender<()>>,
    join: JoinHandle<()>,
}

impl ScheduleHostHandle {
    /// Whether the host supervisor task is still alive.
    ///
    /// Worker failures do not flip this while the supervisor is applying its
    /// bounded restart policy. A finished supervisor is observable by surface
    /// owners, which can replace the stale handle.
    pub fn is_running(&self) -> bool {
        !self.join.is_finished()
    }

    pub async fn shutdown(mut self) {
        if let Some(shutdown_tx) = self.shutdown_tx.take() {
            let _ = shutdown_tx.send(());
        }
        if let Err(error) = self.join.await {
            tracing::error!(%error, "schedule host supervisor task terminated");
        }
    }
}

#[derive(Debug, Clone)]
struct ResolvedScheduledSession {
    session_id: SessionId,
    materialized_session_id: Option<SessionId>,
}

#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
enum AcceptedScheduledInputCompletion {
    Handle(CompletionHandle),
    Terminal(CompletionOutcome),
    AuthorityUnavailable { detail: String },
}

/// Exact target-side admission result for one stable schedule delivery
/// identity. Keeping this narrower than `RuntimeDeliveryOutcome` prevents
/// completion/failure outcomes from being stamped into a DispatchAccepted
/// receipt by construction.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScheduleAdmissionOutcome {
    Accepted,
    Deduplicated,
}

impl ScheduleAdmissionOutcome {
    fn runtime_outcome(self) -> meerkat_schedule::RuntimeDeliveryOutcome {
        match self {
            Self::Accepted => meerkat_schedule::RuntimeDeliveryOutcome::AdmissionAccepted,
            Self::Deduplicated => meerkat_schedule::RuntimeDeliveryOutcome::AdmissionDeduplicated,
        }
    }
}

pub struct AcceptedScheduledInput {
    correlation_id: Option<String>,
    admission_outcome: ScheduleAdmissionOutcome,
    completion: AcceptedScheduledInputCompletion,
}

impl AcceptedScheduledInput {
    pub fn with_runtime_handle(correlation_id: Option<String>, handle: CompletionHandle) -> Self {
        Self {
            correlation_id,
            admission_outcome: ScheduleAdmissionOutcome::Accepted,
            completion: AcceptedScheduledInputCompletion::Handle(handle),
        }
    }

    pub fn with_authority_unavailable(
        correlation_id: Option<String>,
        detail: impl Into<String>,
    ) -> Self {
        Self {
            correlation_id,
            admission_outcome: ScheduleAdmissionOutcome::Accepted,
            completion: AcceptedScheduledInputCompletion::AuthorityUnavailable {
                detail: detail.into(),
            },
        }
    }

    #[cfg_attr(target_arch = "wasm32", allow(dead_code))]
    pub(crate) fn with_runtime_terminal(
        correlation_id: Option<String>,
        terminal: CompletionOutcome,
    ) -> Self {
        Self {
            correlation_id,
            admission_outcome: ScheduleAdmissionOutcome::Accepted,
            completion: AcceptedScheduledInputCompletion::Terminal(terminal),
        }
    }

    pub fn with_admission_outcome(mut self, outcome: ScheduleAdmissionOutcome) -> Self {
        self.admission_outcome = outcome;
        self
    }
}

#[derive(Debug, Clone)]
pub struct ScheduledPromptDispatch {
    pub prompt: ContentInput,
    /// Host-regenerated request-only context for this occurrence. Schedule
    /// definitions must not persist this value; it is attached at delivery.
    pub transient_turn_context: Option<meerkat_core::lifecycle::run_primitive::TurnRequestContext>,
    pub system_prompt: Option<String>,
    pub render_metadata: Option<RenderMetadata>,
    pub skill_refs: Vec<SkillRef>,
    pub additional_instructions: Vec<String>,
    pub materialized_session_id: Option<SessionId>,
}

#[derive(Debug, Clone)]
pub struct ScheduledEventDispatch {
    pub event_type: String,
    pub payload: serde_json::Value,
    pub render_metadata: Option<RenderMetadata>,
    pub materialized_session_id: Option<SessionId>,
}

#[derive(Serialize)]
struct MobMemberScheduleIdentityKey<'a> {
    schema: &'static str,
    mob_id: &'a str,
    member: &'a str,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)]
pub struct MobMemberScheduleIdentity {
    pub mob_id: String,
    pub member: String,
}

#[derive(Deserialize)]
#[allow(dead_code)]
struct OwnedMobMemberScheduleIdentityKey {
    schema: String,
    mob_id: String,
    member: String,
}

pub fn mob_member_schedule_identity(binding: &meerkat_core::MobMemberBinding) -> String {
    let key = MobMemberScheduleIdentityKey {
        schema: "meerkat.schedule.mob_member_identity.v2",
        mob_id: &binding.mob_id,
        member: &binding.member,
    };
    let json = serde_json::to_string(&key).unwrap_or_else(|_| {
        format!(
            "{{\"schema\":\"meerkat.schedule.mob_member_identity.v2\",\"mob_id\":\"{}\",\"member\":\"{}\"}}",
            binding.mob_id, binding.member
        )
    });
    format!("mob_member:{json}")
}

#[derive(Debug, Clone)]
pub struct MobMemberCurrentSessionScheduleResolver {
    binding: meerkat_core::MobMemberBinding,
}

impl MobMemberCurrentSessionScheduleResolver {
    pub fn new(binding: meerkat_core::MobMemberBinding) -> Self {
        Self { binding }
    }

    pub fn binding(&self) -> &meerkat_core::MobMemberBinding {
        &self.binding
    }
}

impl meerkat_schedule::CurrentSessionScheduleTargetResolver
    for MobMemberCurrentSessionScheduleResolver
{
    fn resolve_current_session_target(
        &self,
        _current_session_id: &SessionId,
        action: ScheduledSessionAction,
    ) -> TargetBinding {
        TargetBinding::identity(IdentityTargetBinding::resumable(
            mob_member_schedule_identity(&self.binding),
            action,
        ))
    }
}

#[allow(dead_code)]
pub fn parse_mob_member_schedule_identity(identity: &str) -> Option<MobMemberScheduleIdentity> {
    let json = identity.strip_prefix("mob_member:")?;
    let key: OwnedMobMemberScheduleIdentityKey = serde_json::from_str(json).ok()?;
    match key.schema.as_str() {
        "meerkat.schedule.mob_member_identity.v2" => Some(MobMemberScheduleIdentity {
            mob_id: key.mob_id,
            member: key.member,
        }),
        _ => None,
    }
}

pub fn recover_mob_member_identity_from_session_target(
    binding: &SessionTargetBinding,
    session: Option<&Session>,
) -> Option<IdentityTargetBinding> {
    let SessionTargetBinding::ResumableSession { action, .. } = binding else {
        return None;
    };
    let owner = session
        .and_then(Session::session_metadata)
        .and_then(|metadata| metadata.mob_member_binding)?;
    Some(IdentityTargetBinding::resumable(
        mob_member_schedule_identity(&owner),
        action.clone(),
    ))
}

#[async_trait]
pub trait SurfaceScheduleSessionHost: Send + Sync {
    async fn probe_session_target(
        &self,
        binding: &SessionTargetBinding,
    ) -> Result<TargetProbeOutcome, ScheduleDomainError>;

    async fn probe_identity_target(
        &self,
        binding: &IdentityTargetBinding,
    ) -> Result<TargetProbeOutcome, ScheduleDomainError> {
        let _ = binding;
        Ok(TargetProbeOutcome::Missing {
            detail: Some(
                "scheduled identity targets are not supported by this session host".to_string(),
            ),
        })
    }

    async fn resolve_identity_target(
        &self,
        binding: &IdentityTargetBinding,
    ) -> Result<Option<SessionId>, ScheduleDomainError> {
        let _ = binding;
        Ok(None)
    }

    async fn recover_session_target_identity(
        &self,
        binding: &SessionTargetBinding,
    ) -> Result<Option<IdentityTargetBinding>, ScheduleDomainError> {
        let _ = binding;
        Ok(None)
    }

    /// Materialize the on-demand session for `occurrence`.
    ///
    /// The session id MUST be derived deterministically from the occurrence
    /// identity (via [`Occurrence::materialized_session_id`]) so a redrive of
    /// the same occurrence reuses the existing session instead of minting a
    /// second orphan. Implementations are required to be create-or-reuse: a
    /// second materialize for an occurrence whose deterministic session id
    /// already exists is a no-op reuse, never a duplicate and never an error.
    /// Prompt-action System content is deliberately absent from this seam: it
    /// is an ordinary message delivered exactly once at the turn boundary.
    async fn materialize_session(
        &self,
        occurrence: &Occurrence,
        create: &SessionMaterializationSpec,
    ) -> Result<SessionId, ScheduleDomainError>;

    async fn deliver_prompt(
        &self,
        session_id: &SessionId,
        occurrence: &Occurrence,
        identity: &ScheduleDeliveryIdentity,
        dispatch: ScheduledPromptDispatch,
    ) -> Result<DeliveryDispatch, ScheduleDomainError>;

    async fn deliver_event(
        &self,
        session_id: &SessionId,
        occurrence: &Occurrence,
        identity: &ScheduleDeliveryIdentity,
        dispatch: ScheduledEventDispatch,
    ) -> Result<DeliveryDispatch, ScheduleDomainError>;
}

#[async_trait]
pub trait SurfaceScheduleMobHost: Send + Sync {
    async fn probe_mob_target(
        &self,
        binding: &MobTargetBinding,
    ) -> Result<TargetProbeOutcome, ScheduleDomainError>;

    async fn deliver_mob_target(
        &self,
        occurrence: &Occurrence,
        identity: &ScheduleDeliveryIdentity,
        binding: &MobTargetBinding,
    ) -> Result<DeliveryDispatch, ScheduleDomainError>;

    async fn probe_identity_target(
        &self,
        binding: &IdentityTargetBinding,
    ) -> Result<Option<TargetProbeOutcome>, ScheduleDomainError> {
        let _ = binding;
        Ok(None)
    }

    /// Resolve a stable identity owned by the mob host into its exact current
    /// session. Returning `None` delegates to the ordinary session host.
    /// Implementations must project actor-owned residency rather than search
    /// durable sessions heuristically.
    async fn resolve_identity_target(
        &self,
        binding: &IdentityTargetBinding,
    ) -> Result<Option<SessionId>, ScheduleDomainError> {
        let _ = binding;
        Ok(None)
    }

    async fn deliver_identity_target(
        &self,
        occurrence: &Occurrence,
        identity: &ScheduleDeliveryIdentity,
        binding: &IdentityTargetBinding,
    ) -> Result<Option<DeliveryDispatch>, ScheduleDomainError> {
        let _ = (occurrence, identity, binding);
        Ok(None)
    }
}

pub struct NoopScheduleMobHost {
    detail: String,
}

impl NoopScheduleMobHost {
    pub fn new(detail: impl Into<String>) -> Self {
        Self {
            detail: detail.into(),
        }
    }
}

#[async_trait]
impl SurfaceScheduleMobHost for NoopScheduleMobHost {
    async fn probe_mob_target(
        &self,
        _binding: &MobTargetBinding,
    ) -> Result<TargetProbeOutcome, ScheduleDomainError> {
        Ok(TargetProbeOutcome::Missing {
            detail: Some(self.detail.clone()),
        })
    }

    async fn deliver_mob_target(
        &self,
        occurrence: &Occurrence,
        identity: &ScheduleDeliveryIdentity,
        _binding: &MobTargetBinding,
    ) -> Result<DeliveryDispatch, ScheduleDomainError> {
        Ok(immediate_delivery_failure(
            occurrence,
            self.detail.clone(),
            DeliveryFailureReason::MobRejected,
            Some(identity.correlation_id.clone()),
            None,
        ))
    }

    async fn probe_identity_target(
        &self,
        _binding: &IdentityTargetBinding,
    ) -> Result<Option<TargetProbeOutcome>, ScheduleDomainError> {
        Ok(None)
    }

    async fn deliver_identity_target(
        &self,
        _occurrence: &Occurrence,
        _identity: &ScheduleDeliveryIdentity,
        _binding: &IdentityTargetBinding,
    ) -> Result<Option<DeliveryDispatch>, ScheduleDomainError> {
        Ok(None)
    }
}

pub struct SharedScheduleTargetAdapter {
    schedule_service: ScheduleService,
    session_host: Arc<dyn SurfaceScheduleSessionHost>,
    mob_host: Arc<dyn SurfaceScheduleMobHost>,
    runnable_host: Option<Arc<dyn ScheduleRunnableHost>>,
}

impl SharedScheduleTargetAdapter {
    pub fn new(
        schedule_service: ScheduleService,
        session_host: Arc<dyn SurfaceScheduleSessionHost>,
        mob_host: Arc<dyn SurfaceScheduleMobHost>,
    ) -> Self {
        Self {
            schedule_service,
            session_host,
            mob_host,
            runnable_host: None,
        }
    }

    /// Attach a host-runnable registry to this adapter.
    ///
    /// Default is no registry: `host_runnable` targets then probe `Missing`
    /// and deliveries fail with `TargetMissing`.
    pub fn with_runnable_host(mut self, runnable_host: Arc<dyn ScheduleRunnableHost>) -> Self {
        self.runnable_host = Some(runnable_host);
        self
    }

    async fn resolve_session(
        &self,
        occurrence: &Occurrence,
        delivery_identity: &ScheduleDeliveryIdentity,
        binding: &SessionTargetBinding,
    ) -> Result<ResolvedScheduledSession, DeliveryDispatch> {
        match binding {
            SessionTargetBinding::ExactSession { session_id, .. }
            | SessionTargetBinding::ResumableSession { session_id, .. } => {
                if let Ok(TargetProbeOutcome::Missing { .. }) =
                    self.session_host.probe_session_target(binding).await
                {
                    let recovered = self
                        .session_host
                        .recover_session_target_identity(binding)
                        .await
                        .map_err(|error| {
                            immediate_delivery_failure(
                                occurrence,
                                error.to_string(),
                                DeliveryFailureReason::TargetMaterializationFailed,
                                Some(delivery_identity.correlation_id.clone()),
                                None,
                            )
                        })?;
                    if let Some(identity) = recovered {
                        if let Some(dispatch) = self
                            .mob_host
                            .deliver_identity_target(occurrence, delivery_identity, &identity)
                            .await
                            .map_err(|error| {
                                immediate_delivery_failure(
                                    occurrence,
                                    error.to_string(),
                                    DeliveryFailureReason::TargetMaterializationFailed,
                                    Some(delivery_identity.correlation_id.clone()),
                                    None,
                                )
                            })?
                        {
                            return Err(dispatch);
                        }
                        return self
                            .resolve_identity(occurrence, delivery_identity, &identity)
                            .await;
                    }
                }
                Ok(ResolvedScheduledSession {
                    session_id: session_id.clone(),
                    materialized_session_id: None,
                })
            }
            SessionTargetBinding::MaterializeOnDemandSession {
                bound_session_id: Some(session_id),
                ..
            } => Ok(ResolvedScheduledSession {
                session_id: session_id.clone(),
                materialized_session_id: Some(session_id.clone()),
            }),
            SessionTargetBinding::MaterializeOnDemandSession {
                create,
                action: _,
                bound_session_id: None,
            } => {
                // Layer B: defensive contractual reuse guard. The in-flight
                // occurrence snapshot can be stale — a prior attempt may have
                // committed the bound id to the authoritative schedule target
                // (and pending occurrences) after this snapshot was claimed.
                // Re-read the authoritative binding for THIS occurrence before
                // materializing; if a session is already bound, reuse it and
                // never mint a second one.
                if let Some(bound) = self.authoritative_bound_session_id(occurrence).await {
                    return Ok(ResolvedScheduledSession {
                        session_id: bound.clone(),
                        materialized_session_id: Some(bound),
                    });
                }
                match self
                    .session_host
                    .materialize_session(occurrence, create)
                    .await
                {
                    Ok(session_id) => {
                        if let Err(error) = self
                            .schedule_service
                            .bind_materialized_session_for_occurrence(occurrence, &session_id)
                            .await
                        {
                            return Err(immediate_delivery_failure(
                                occurrence,
                                error.to_string(),
                                DeliveryFailureReason::InternalError,
                                Some(delivery_identity.correlation_id.clone()),
                                Some(session_id),
                            ));
                        }
                        Ok(ResolvedScheduledSession {
                            session_id: session_id.clone(),
                            materialized_session_id: Some(session_id),
                        })
                    }
                    Err(error) => Err(immediate_delivery_failure(
                        occurrence,
                        error.to_string(),
                        DeliveryFailureReason::TargetMaterializationFailed,
                        Some(delivery_identity.correlation_id.clone()),
                        None,
                    )),
                }
            }
        }
    }

    /// Re-read the authoritative bound session id for `occurrence`.
    ///
    /// `bind_materialized_session_for_occurrence` commits the materialized id
    /// to the schedule target (and pending occurrences). After a prior attempt
    /// committed that bind but died before the in-flight snapshot was synced,
    /// the occurrence handed to `resolve_session` can still report
    /// `bound_session_id: None`. This consults the freshest authoritative
    /// state — the re-read occurrence first, then the schedule target — so the
    /// adapter never materializes a second session for an already-bound
    /// occurrence. A read failure is treated as "no authoritative binding
    /// known": the caller falls through to deterministic-id materialization,
    /// which is itself create-or-reuse, so no orphan can result.
    async fn authoritative_bound_session_id(&self, occurrence: &Occurrence) -> Option<SessionId> {
        let store = self.schedule_service.store();

        if let Ok(Some(current)) = store.get_occurrence(&occurrence.occurrence_id).await
            && let TargetBinding::Session(binding) = &current.target_snapshot
            && let Some(session_id) = binding.resolved_session_id()
        {
            return Some(session_id.clone());
        }

        if let Ok(Some(schedule)) = store.get_schedule(&occurrence.schedule_id).await
            && schedule.revision == occurrence.schedule_revision
            && let TargetBinding::Session(binding) = &schedule.target
            && let Some(session_id) = binding.resolved_session_id()
        {
            return Some(session_id.clone());
        }

        None
    }

    async fn resolve_identity(
        &self,
        occurrence: &Occurrence,
        delivery_identity: &ScheduleDeliveryIdentity,
        binding: &IdentityTargetBinding,
    ) -> Result<ResolvedScheduledSession, DeliveryDispatch> {
        let mob_owned = self
            .mob_host
            .resolve_identity_target(binding)
            .await
            .map_err(|error| {
                immediate_delivery_failure(
                    occurrence,
                    error.to_string(),
                    DeliveryFailureReason::TargetMaterializationFailed,
                    Some(delivery_identity.correlation_id.clone()),
                    None,
                )
            })?;
        let resolved = match mob_owned {
            Some(session_id) => Ok(Some(session_id)),
            None => self.session_host.resolve_identity_target(binding).await,
        };
        match resolved {
            Ok(Some(session_id)) => Ok(ResolvedScheduledSession {
                session_id,
                materialized_session_id: None,
            }),
            Ok(None) => Err(immediate_delivery_failure(
                occurrence,
                format!(
                    "scheduled identity target not found: {}",
                    binding.identity()
                ),
                DeliveryFailureReason::TargetMaterializationFailed,
                Some(delivery_identity.correlation_id.clone()),
                None,
            )),
            Err(error) => Err(immediate_delivery_failure(
                occurrence,
                error.to_string(),
                DeliveryFailureReason::TargetMaterializationFailed,
                Some(delivery_identity.correlation_id.clone()),
                None,
            )),
        }
    }

    /// Explicit one-time compatibility boundary for released schedules whose
    /// durable target is a recoverable Session id rather than an identity.
    ///
    /// This intentionally performs catalog-wide work and therefore must be
    /// invoked by an owning activation/migration transaction, never by the
    /// long-lived schedule worker or its restart supervisor. The caller owns
    /// recording completion before starting the worker.
    pub async fn migrate_recoverable_session_targets(&self) -> Result<usize, ScheduleDomainError> {
        let schedules = self
            .schedule_service
            .store()
            .list_schedules(ScheduleFilter {
                include_deleted: false,
                ..ScheduleFilter::default()
            })
            .await?;
        let mut migrated = 0usize;

        for schedule in schedules {
            let TargetBinding::Session(binding) = &schedule.target else {
                continue;
            };
            let Some(identity) = self
                .session_host
                .recover_session_target_identity(binding)
                .await?
            else {
                continue;
            };
            self.schedule_service
                .update(
                    &schedule.schedule_id,
                    UpdateScheduleRequest {
                        expected_revision: Some(schedule.revision),
                        target: Some(TargetBinding::identity(identity)),
                        ..UpdateScheduleRequest::default()
                    },
                )
                .await?;
            migrated += 1;
        }

        Ok(migrated)
    }

    async fn deliver_session_action(
        &self,
        occurrence: &Occurrence,
        identity: &ScheduleDeliveryIdentity,
        resolved: ResolvedScheduledSession,
        action: &ScheduledSessionAction,
    ) -> Result<DeliveryDispatch, ScheduleDomainError> {
        match action {
            ScheduledSessionAction::Prompt {
                prompt,
                system_prompt,
                render_metadata,
                skill_refs,
                additional_instructions,
            } => {
                self.session_host
                    .deliver_prompt(
                        &resolved.session_id,
                        occurrence,
                        identity,
                        ScheduledPromptDispatch {
                            prompt: prompt.clone(),
                            transient_turn_context: None,
                            system_prompt: system_prompt.clone(),
                            render_metadata: render_metadata.clone(),
                            skill_refs: skill_refs.clone(),
                            additional_instructions: additional_instructions.clone(),
                            materialized_session_id: resolved.materialized_session_id,
                        },
                    )
                    .await
            }
            ScheduledSessionAction::Event {
                event_type,
                payload,
                render_metadata,
            } => {
                self.session_host
                    .deliver_event(
                        &resolved.session_id,
                        occurrence,
                        identity,
                        ScheduledEventDispatch {
                            event_type: event_type.clone(),
                            payload: payload.clone(),
                            render_metadata: render_metadata.clone(),
                            materialized_session_id: resolved.materialized_session_id,
                        },
                    )
                    .await
            }
        }
    }

    /// Dispatch a `host_runnable` target through the in-process runnable seam.
    ///
    /// Failure mapping for an in-process callback (deliberate decision):
    /// - unregistered runnable or no configured registry → `TargetMissing`
    ///   (the named target does not exist on this host);
    /// - a `HostRunnableError` returned by the callback → `RuntimeRejected`
    ///   (the executing runtime refused or failed the work);
    /// - `TransportError` is deliberately NOT reachable: there is no
    ///   transport hop in an in-process invocation, so no outcome can
    ///   honestly be a transport fault. (Counter-precedent: mob targets own
    ///   the target-kind-specific `MobRejected` reason; host runnables map
    ///   onto the existing shared reasons instead of minting a new
    ///   machine-vocabulary variant.)
    fn deliver_host_runnable(
        &self,
        occurrence: &Occurrence,
        identity: &ScheduleDeliveryIdentity,
        binding: &HostRunnableTargetBinding,
    ) -> DeliveryDispatch {
        let Some(runnable_host) = &self.runnable_host else {
            return immediate_delivery_failure(
                occurrence,
                format!(
                    "host runnable '{}' is unavailable: no runnable registry is configured on this surface",
                    binding.runnable
                ),
                DeliveryFailureReason::TargetMissing,
                Some(identity.correlation_id.clone()),
                None,
            );
        };
        if runnable_host.probe_runnable(&binding.runnable) == RunnableProbe::Unknown {
            return immediate_delivery_failure(
                occurrence,
                format!("host runnable '{}' is not registered", binding.runnable),
                DeliveryFailureReason::TargetMissing,
                Some(identity.correlation_id.clone()),
                None,
            );
        }

        let invocation = HostRunnableInvocation {
            occurrence_id: occurrence.occurrence_id.clone(),
            schedule_id: occurrence.schedule_id.clone(),
            delivery_idempotency_key: identity.idempotency_key.clone(),
            runnable: binding.runnable.clone(),
            trigger_time: occurrence.due_at_utc,
            params: binding.params.clone().map(HostRunnableParams::into_raw),
        };
        let runnable_host = Arc::clone(runnable_host);
        async_completion_dispatch(
            occurrence,
            Some(identity.correlation_id.clone()),
            Box::pin(async move {
                Ok(match runnable_host.run_occurrence(invocation).await {
                    Ok(_) => DeliveryTerminal::completed(None),
                    // Unregistered is the same semantic condition the probe
                    // reports as Unknown: one condition, one terminal class,
                    // regardless of where it is detected.
                    Err(error @ meerkat_schedule::HostRunnableError::Unregistered { .. }) => {
                        DeliveryTerminal::delivery_failed(
                            error.to_string(),
                            DeliveryFailureReason::TargetMissing,
                        )
                    }
                    Err(error) => DeliveryTerminal::delivery_failed(
                        error.to_string(),
                        DeliveryFailureReason::RuntimeRejected,
                    ),
                })
            }),
        )
    }
}

#[async_trait]
impl ScheduleTargetProbe for SharedScheduleTargetAdapter {
    async fn probe_target(
        &self,
        occurrence: &Occurrence,
    ) -> Result<TargetProbeOutcome, ScheduleDomainError> {
        match &occurrence.target_snapshot {
            TargetBinding::Session(binding) => {
                let probe = self.session_host.probe_session_target(binding).await?;
                if matches!(probe, TargetProbeOutcome::Missing { .. })
                    && let Some(identity) = self
                        .session_host
                        .recover_session_target_identity(binding)
                        .await?
                {
                    if let Some(probe) = self.mob_host.probe_identity_target(&identity).await? {
                        return Ok(probe);
                    }
                    return self.session_host.probe_identity_target(&identity).await;
                }
                Ok(probe)
            }
            TargetBinding::Identity(binding) => {
                if let Some(probe) = self.mob_host.probe_identity_target(binding).await? {
                    return Ok(probe);
                }
                self.session_host.probe_identity_target(binding).await
            }
            TargetBinding::Mob(binding) => self.mob_host.probe_mob_target(binding).await,
            TargetBinding::HostRunnable(binding) => {
                let Some(runnable_host) = &self.runnable_host else {
                    return Ok(TargetProbeOutcome::Missing {
                        detail: Some(format!(
                            "host runnable '{}' is unavailable: no runnable registry is configured on this surface",
                            binding.runnable
                        )),
                    });
                };
                Ok(match runnable_host.probe_runnable(&binding.runnable) {
                    RunnableProbe::Registered => TargetProbeOutcome::Ready,
                    RunnableProbe::Unknown => TargetProbeOutcome::Missing {
                        detail: Some(format!(
                            "host runnable '{}' is not registered",
                            binding.runnable
                        )),
                    },
                })
            }
        }
    }
}

#[async_trait]
impl ScheduleTargetDelivery for SharedScheduleTargetAdapter {
    async fn deliver_occurrence(
        &self,
        occurrence: &Occurrence,
        identity: &ScheduleDeliveryIdentity,
    ) -> Result<DeliveryDispatch, ScheduleDomainError> {
        match &occurrence.target_snapshot {
            TargetBinding::Session(binding) => {
                let resolved = match self.resolve_session(occurrence, identity, binding).await {
                    Ok(resolved) => resolved,
                    Err(dispatch) => return Ok(dispatch),
                };

                self.deliver_session_action(occurrence, identity, resolved, binding.action())
                    .await
            }
            TargetBinding::Identity(binding) => {
                if let Some(dispatch) = self
                    .mob_host
                    .deliver_identity_target(occurrence, identity, binding)
                    .await?
                {
                    return Ok(dispatch);
                }
                let resolved = match self.resolve_identity(occurrence, identity, binding).await {
                    Ok(resolved) => resolved,
                    Err(dispatch) => return Ok(dispatch),
                };
                self.deliver_session_action(occurrence, identity, resolved, binding.action())
                    .await
            }
            TargetBinding::Mob(binding) => {
                self.mob_host
                    .deliver_mob_target(occurrence, identity, binding)
                    .await
            }
            TargetBinding::HostRunnable(binding) => {
                Ok(self.deliver_host_runnable(occurrence, identity, binding))
            }
        }
    }
}

pub fn schedule_host_supported(kind: ScheduleStoreKind) -> bool {
    !matches!(kind, ScheduleStoreKind::Disabled | ScheduleStoreKind::Jsonl)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ScheduleHostIncidentClass {
    DriverTickFailed,
    DriverRowFaults,
    NextActionReadFailed,
    DurableWakeFailed,
    WorkerExited,
    WorkerPanicked,
}

impl ScheduleHostIncidentClass {
    fn as_str(self) -> &'static str {
        match self {
            Self::DriverTickFailed => "driver_tick_failed",
            Self::DriverRowFaults => "driver_row_faults",
            Self::NextActionReadFailed => "next_action_read_failed",
            Self::DurableWakeFailed => "durable_wake_failed",
            Self::WorkerExited => "worker_exited",
            Self::WorkerPanicked => "worker_panicked",
        }
    }
}

#[derive(Debug)]
struct ScheduleHostIncident {
    class: ScheduleHostIncidentClass,
    detail: String,
}

impl ScheduleHostIncident {
    fn new(class: ScheduleHostIncidentClass, detail: impl Into<String>) -> Self {
        Self {
            class,
            detail: detail.into(),
        }
    }
}

/// Host-health bookkeeping keyed by a bounded incident class, never by
/// volatile row ids, page contents, or transport prose. Detail is refreshed
/// for the next heartbeat, but a changing detail inside the same class cannot
/// punch through the rate limiter and create an ERROR/page flood.
struct ScheduleHostIncidentTracker {
    class: Option<ScheduleHostIncidentClass>,
    /// Observations in the CURRENT incident class (heartbeat counter).
    consecutive: u64,
    /// Total unhealthy observations since the loop was last healthy, across
    /// class changes — the outage length reported on recovery.
    outage_observations: u64,
    last_logged: Option<meerkat_core::time_compat::Instant>,
    heartbeat_every: std::time::Duration,
}

#[derive(Debug)]
enum ScheduleHostHealthLog {
    Quiet,
    /// A new incident class: log at ERROR.
    Incident {
        class: ScheduleHostIncidentClass,
        detail: String,
    },
    /// The same class persists: rate-limited WARN heartbeat with the latest
    /// bounded detail.
    Heartbeat {
        class: ScheduleHostIncidentClass,
        detail: String,
        consecutive: u64,
    },
    /// The loop recovered after `after` unhealthy observations.
    Recovered {
        class: ScheduleHostIncidentClass,
        after: u64,
    },
}

impl ScheduleHostIncidentTracker {
    fn new(heartbeat_every: std::time::Duration) -> Self {
        Self {
            class: None,
            consecutive: 0,
            outage_observations: 0,
            last_logged: None,
            heartbeat_every,
        }
    }

    fn observe(
        &mut self,
        incident: Option<ScheduleHostIncident>,
        now: meerkat_core::time_compat::Instant,
    ) -> ScheduleHostHealthLog {
        match incident {
            None => {
                let after = self.outage_observations;
                let class = self.class.take();
                self.consecutive = 0;
                self.outage_observations = 0;
                self.last_logged = None;
                match class {
                    Some(class) => ScheduleHostHealthLog::Recovered { class, after },
                    None => ScheduleHostHealthLog::Quiet,
                }
            }
            Some(incident) => {
                self.outage_observations += 1;
                self.consecutive += 1;
                let class_changed = self.class != Some(incident.class);
                if class_changed {
                    self.class = Some(incident.class);
                    self.consecutive = 1;
                    self.last_logged = Some(now);
                    return ScheduleHostHealthLog::Incident {
                        class: incident.class,
                        detail: incident.detail,
                    };
                }
                if self
                    .last_logged
                    .is_none_or(|last| now.duration_since(last) >= self.heartbeat_every)
                {
                    self.last_logged = Some(now);
                    return ScheduleHostHealthLog::Heartbeat {
                        class: incident.class,
                        detail: incident.detail,
                        consecutive: self.consecutive,
                    };
                }
                ScheduleHostHealthLog::Quiet
            }
        }
    }
}

fn log_schedule_host_health(action: ScheduleHostHealthLog) {
    match action {
        ScheduleHostHealthLog::Quiet => {}
        ScheduleHostHealthLog::Incident { class, detail } => {
            tracing::error!(
                incident_class = class.as_str(),
                %detail,
                "schedule host entered an unhealthy state"
            );
        }
        ScheduleHostHealthLog::Heartbeat {
            class,
            detail,
            consecutive,
        } => {
            tracing::warn!(
                incident_class = class.as_str(),
                %detail,
                consecutive,
                "schedule host incident persists"
            );
        }
        ScheduleHostHealthLog::Recovered { class, after } => {
            tracing::info!(
                incident_class = class.as_str(),
                after,
                "schedule host recovered"
            );
        }
    }
}

/// Consecutive no-progress ticks tolerated at the base interval before the
/// host starts backing off (errors escalate immediately). Sits beside the
/// driver policy literals (`claim_limit`, `lease_duration`) in
/// [`spawn_schedule_host`] as the host's tick-pacing policy.
const IDLE_TICKS_BEFORE_BACKOFF: u32 = 8;

const MAX_FAULT_LOG_SAMPLES: usize = 3;
const MAX_FAULT_LOG_SAMPLE_CHARS: usize = 512;

/// Exponential tick pacing for the schedule host loop (2026-07-29 incident:
/// a failing or no-progress tick retried at a fixed 4Hz forever — on a
/// remote store that is query spam priced in currency; a BigQuery-store
/// consumer flagged it as a parity blocker).
///
/// Policy: a failing tick escalates immediately; a run of
/// `idle_grace_ticks` no-progress ticks starts escalating too (doubling,
/// capped); any healthy tick that moves work snaps back to the base interval.
struct TickBackoff {
    base: Duration,
    cap: Duration,
    idle_grace_ticks: u32,
    current: Duration,
    consecutive_unproductive: u32,
}

impl TickBackoff {
    fn new(base: Duration, cap: Duration, idle_grace_ticks: u32) -> Self {
        Self {
            base,
            cap,
            idle_grace_ticks,
            current: base,
            consecutive_unproductive: 0,
        }
    }

    /// Observe one tick outcome and return the delay to sleep before the
    /// next tick.
    #[cfg(test)]
    fn observe(
        &mut self,
        outcome: &Result<
            meerkat_schedule::ScheduleTickReport,
            meerkat_schedule::ScheduleDomainError,
        >,
    ) -> Duration {
        match outcome {
            Err(_) => self.observe_failure(),
            Ok(report) => self.observe_report(report),
        }
        self.current
    }

    fn observe_failure(&mut self) {
        // A failing tick is retried with immediate escalation: at the fixed
        // interval every retry hammered the failing store.
        self.consecutive_unproductive = self.consecutive_unproductive.saturating_add(1);
        self.escalate();
    }

    fn observe_report(&mut self, report: &meerkat_schedule::ScheduleTickReport) {
        if report.made_progress() {
            self.consecutive_unproductive = 0;
            self.current = self.base;
        } else {
            // Successful but unproductive: keep the fast cadence through the
            // grace run for exact due work, then back off. Idle durable stores
            // use their declared wake mode rather than this cadence.
            self.consecutive_unproductive = self.consecutive_unproductive.saturating_add(1);
            if self.consecutive_unproductive > self.idle_grace_ticks {
                self.escalate();
            }
        }
    }

    fn escalate(&mut self) {
        self.current = self.current.saturating_mul(2).min(self.cap);
    }
}

struct RestartBackoff {
    base: Duration,
    cap: Duration,
    next: Duration,
}

impl RestartBackoff {
    fn new(base: Duration, cap: Duration) -> Self {
        Self {
            base,
            cap,
            next: base,
        }
    }

    fn after_failure(&mut self) -> Duration {
        let delay = self.next;
        self.next = self.next.saturating_mul(2).min(self.cap);
        delay
    }

    fn reset(&mut self) {
        self.next = self.base;
    }
}

fn duration_until_store_action(
    action: meerkat_schedule::ScheduleStoreActionTime,
) -> Option<Duration> {
    action.next_action_at_utc.map(|next_action_at_utc| {
        next_action_at_utc
            .signed_duration_since(action.store_now_utc)
            .to_std()
            .unwrap_or(Duration::ZERO)
    })
}

fn idle_delay_for_store_action(
    action: meerkat_schedule::ScheduleStoreActionTime,
    wake_mode: ScheduleStoreWakeMode,
    backoff_delay: Duration,
    base_interval: Duration,
) -> Option<Duration> {
    let until_action = duration_until_store_action(action);
    match wake_mode {
        ScheduleStoreWakeMode::ProcessLocal | ScheduleStoreWakeMode::Push => match until_action {
            Some(delay) if delay.is_zero() => Some(backoff_delay),
            Some(delay) => Some(delay),
            None => None,
        },
        ScheduleStoreWakeMode::BoundedPoll { max_interval } => {
            // A zero custom interval cannot create a hot loop. A non-zero
            // declaration is the store's exact maximum convergence interval,
            // including intervals tighter than the host's ordinary base
            // cadence, and must never be silently widened.
            let poll_interval = if max_interval.is_zero() {
                base_interval
            } else {
                max_interval
            };
            match until_action {
                Some(delay) if delay.is_zero() => Some(backoff_delay),
                Some(delay) => Some(delay.min(poll_interval)),
                None => Some(poll_interval),
            }
        }
    }
}

fn incident_from_tick_report(
    report: &meerkat_schedule::ScheduleTickReport,
) -> Option<ScheduleHostIncident> {
    (report.fault_count() > 0).then(|| {
        ScheduleHostIncident::new(
            ScheduleHostIncidentClass::DriverRowFaults,
            format!(
                "tick degraded with {} fault(s):\n{}",
                report.fault_count(),
                report.bounded_fault_summary(MAX_FAULT_LOG_SAMPLES, MAX_FAULT_LOG_SAMPLE_CHARS)
            ),
        )
    })
}

async fn optional_schedule_host_sleep(delay: Option<Duration>) {
    match delay {
        Some(delay) => schedule_host_tokio::time::sleep(delay).await,
        None => std::future::pending::<()>().await,
    }
}

async fn wait_for_durable_store_wake(
    store: Arc<dyn meerkat_schedule::ScheduleStore>,
    wake_mode: ScheduleStoreWakeMode,
) -> Result<(), meerkat_schedule::ScheduleStoreError> {
    match wake_mode {
        ScheduleStoreWakeMode::Push => store.wait_for_durable_wake().await,
        ScheduleStoreWakeMode::ProcessLocal | ScheduleStoreWakeMode::BoundedPoll { .. } => {
            std::future::pending::<Result<(), meerkat_schedule::ScheduleStoreError>>().await
        }
    }
}

#[derive(Debug)]
enum ScheduleHostWorkerExit {
    DurableWakeFailed(meerkat_schedule::ScheduleStoreError),
    MutationSignalClosed,
}

impl ScheduleHostWorkerExit {
    fn into_incident(self) -> ScheduleHostIncident {
        match self {
            Self::DurableWakeFailed(error) => ScheduleHostIncident::new(
                ScheduleHostIncidentClass::DurableWakeFailed,
                format!("durable schedule wake failed: {error}"),
            ),
            Self::MutationSignalClosed => ScheduleHostIncident::new(
                ScheduleHostIncidentClass::WorkerExited,
                "process-local schedule mutation signal closed",
            ),
        }
    }
}

async fn run_schedule_host_worker(
    schedule_service: ScheduleService,
    driver: Arc<ScheduleDriver>,
    wake_mode: ScheduleStoreWakeMode,
    base_interval: Duration,
    backoff_cap: Duration,
    mut shutdown_rx: oneshot::Receiver<()>,
) -> Result<(), ScheduleHostWorkerExit> {
    let mut health = ScheduleHostIncidentTracker::new(std::time::Duration::from_secs(60));

    let mut backoff = TickBackoff::new(base_interval, backoff_cap, IDLE_TICKS_BEFORE_BACKOFF);
    let mut delay = Some(base_interval);
    let mut mutation_rx = schedule_service.subscribe_mutations();
    let store = schedule_service.store();

    loop {
        schedule_host_tokio::select! {
            _ = &mut shutdown_rx => return Ok(()),
            mutation = mutation_rx.changed() => {
                if mutation.is_err() {
                    return Err(ScheduleHostWorkerExit::MutationSignalClosed);
                }
            }
            durable_wake = wait_for_durable_store_wake(Arc::clone(&store), wake_mode) => {
                if let Err(error) = durable_wake {
                    return Err(ScheduleHostWorkerExit::DurableWakeFailed(error));
                }
            }
            () = optional_schedule_host_sleep(delay) => {}
        }

        let outcome = driver.tick_once().await;
        let (next_delay, incident) = match outcome {
            Err(error) => {
                backoff.observe_failure();
                (
                    Some(backoff.current),
                    Some(ScheduleHostIncident::new(
                        ScheduleHostIncidentClass::DriverTickFailed,
                        format!("schedule driver tick failed: {error}"),
                    )),
                )
            }
            Ok(report) if report.made_progress() => {
                let incident = incident_from_tick_report(&report);
                if incident.is_some() {
                    backoff.observe_failure();
                } else {
                    backoff.observe_report(&report);
                }
                (Some(backoff.current), incident)
            }
            Ok(report) => match store.next_action_time_utc().await {
                Err(error) => {
                    backoff.observe_failure();
                    (
                        Some(backoff.current),
                        Some(ScheduleHostIncident::new(
                            ScheduleHostIncidentClass::NextActionReadFailed,
                            format!("could not read next durable action time: {error}"),
                        )),
                    )
                }
                Ok(action) => {
                    let incident = incident_from_tick_report(&report);
                    if incident.is_some() {
                        backoff.observe_failure();
                    } else {
                        backoff.observe_report(&report);
                    }
                    (
                        idle_delay_for_store_action(
                            action,
                            wake_mode,
                            backoff.current,
                            base_interval,
                        ),
                        incident,
                    )
                }
            },
        };
        log_schedule_host_health(
            health.observe(incident, meerkat_core::time_compat::Instant::now()),
        );
        delay = next_delay;
    }
}

async fn supervise_schedule_host(
    schedule_service: ScheduleService,
    driver: Arc<ScheduleDriver>,
    wake_mode: ScheduleStoreWakeMode,
    base_interval: Duration,
    backoff_cap: Duration,
    stable_window: Duration,
    mut shutdown_rx: oneshot::Receiver<()>,
) {
    let mut health = ScheduleHostIncidentTracker::new(std::time::Duration::from_secs(60));
    let mut restart_backoff = RestartBackoff::new(base_interval, backoff_cap);

    loop {
        let (worker_shutdown_tx, worker_shutdown_rx) = oneshot::channel();
        let mut worker = schedule_host_tokio::task::spawn(run_schedule_host_worker(
            schedule_service.clone(),
            Arc::clone(&driver),
            wake_mode,
            base_interval,
            backoff_cap,
            worker_shutdown_rx,
        ));
        let mut worker_shutdown_tx = Some(worker_shutdown_tx);
        let mut stable_timer = Box::pin(schedule_host_tokio::time::sleep(stable_window));
        let mut stable = false;

        let worker_result = loop {
            schedule_host_tokio::select! {
                _ = &mut shutdown_rx => {
                    if let Some(shutdown_tx) = worker_shutdown_tx.take() {
                        let _ = shutdown_tx.send(());
                    }
                    let _ = worker.await;
                    return;
                }
                result = &mut worker => break result,
                () = &mut stable_timer, if !stable => {
                    stable = true;
                    restart_backoff.reset();
                    log_schedule_host_health(health.observe(
                        None,
                        meerkat_core::time_compat::Instant::now(),
                    ));
                }
            }
        };
        let incident = match worker_result {
            Ok(Ok(())) => ScheduleHostIncident::new(
                ScheduleHostIncidentClass::WorkerExited,
                "schedule host worker exited without supervisor shutdown",
            ),
            Ok(Err(exit)) => exit.into_incident(),
            Err(error) => ScheduleHostIncident::new(
                ScheduleHostIncidentClass::WorkerPanicked,
                format!("schedule host worker task terminated: {error}"),
            ),
        };
        log_schedule_host_health(
            health.observe(Some(incident), meerkat_core::time_compat::Instant::now()),
        );

        let restart_delay = restart_backoff.after_failure();
        schedule_host_tokio::select! {
            _ = &mut shutdown_rx => return,
            () = schedule_host_tokio::time::sleep(restart_delay) => {}
        }
    }
}

pub fn spawn_schedule_host(
    schedule_service: ScheduleService,
    adapter: Arc<SharedScheduleTargetAdapter>,
    owner_id: impl Into<String>,
) -> ScheduleHostHandle {
    let driver = Arc::new(ScheduleDriver::new(
        schedule_service.clone(),
        schedule_service.store(),
        adapter.clone(),
        adapter,
        owner_id,
        ScheduleDriverConfig {
            claim_limit: 32,
            lease_duration: ChronoDuration::seconds(60),
        },
    ));
    let wake_mode = schedule_service.store().wake_mode();
    // Tick pacing: base interval while work flows, exponential backoff to
    // the cap while ticks fail or make no progress (see `TickBackoff`). The
    // in-crate test profile keeps both bounds small so host tests never wait
    // on an escalated interval.
    let (base_interval, backoff_cap, stable_window) = if cfg!(test) {
        (
            Duration::from_millis(50),
            Duration::from_millis(400),
            Duration::from_millis(200),
        )
    } else {
        (
            Duration::from_millis(250),
            Duration::from_secs(30),
            Duration::from_secs(60),
        )
    };
    let (shutdown_tx, shutdown_rx) = oneshot::channel();
    let join = schedule_host_tokio::task::spawn(supervise_schedule_host(
        schedule_service,
        driver,
        wake_mode,
        base_interval,
        backoff_cap,
        stable_window,
        shutdown_rx,
    ));

    ScheduleHostHandle {
        shutdown_tx: Some(shutdown_tx),
        join,
    }
}

pub fn build_dispatch_from_accepted(
    occurrence: &Occurrence,
    accepted: AcceptedScheduledInput,
    materialized_session_id: Option<SessionId>,
) -> DeliveryDispatch {
    let mut receipt = DeliveryReceipt::new(
        occurrence.occurrence_id.clone(),
        occurrence.attempt_count,
        DeliveryReceiptStage::DispatchAccepted,
    );
    receipt.correlation_id = accepted.correlation_id.clone();
    receipt.runtime_outcome = Some(accepted.admission_outcome.runtime_outcome());
    receipt.materialized_session_id = materialized_session_id.clone();

    let completion = schedule_completion_from_runtime_completion(
        accepted.completion,
        materialized_session_id.clone(),
    );

    DeliveryDispatch {
        receipt,
        correlation_id: accepted.correlation_id,
        materialized_session_id,
        completion,
    }
}

fn schedule_completion_from_runtime_completion(
    completion: AcceptedScheduledInputCompletion,
    materialized_session_id: Option<SessionId>,
) -> DeliveryCompletion {
    Box::pin(async move {
        let outcome = match completion {
            AcceptedScheduledInputCompletion::Handle(handle) => match handle.try_wait().await {
                Ok(outcome) => outcome,
                Err(error) => {
                    return Err(ScheduleDomainError::DeliveryCompletionFailed {
                        reason: completion_wait_failure_reason(&error),
                        detail: format!("runtime completion authority unavailable: {error}"),
                    });
                }
            },
            AcceptedScheduledInputCompletion::Terminal(terminal) => {
                return Ok(delivery_terminal_from_completion_outcome(
                    terminal,
                    materialized_session_id,
                ));
            }
            AcceptedScheduledInputCompletion::AuthorityUnavailable { detail } => {
                return Err(ScheduleDomainError::DeliveryCompletionFailed {
                    reason: DeliveryCompletionFailureReason::RuntimeCompletionAuthorityUnavailable,
                    detail,
                });
            }
        };
        Ok(delivery_terminal_from_completion_outcome(
            outcome,
            materialized_session_id,
        ))
    })
}

fn completion_wait_failure_reason(error: &CompletionWaitError) -> DeliveryCompletionFailureReason {
    match error {
        CompletionWaitError::ChannelClosed => {
            DeliveryCompletionFailureReason::RuntimeCompletionChannelClosed
        }
        CompletionWaitError::AttachmentReplaced | CompletionWaitError::AuthorityUnavailable(_) => {
            DeliveryCompletionFailureReason::RuntimeCompletionAuthorityUnavailable
        }
    }
}

fn delivery_terminal_from_completion_outcome(
    outcome: CompletionOutcome,
    _materialized_session_id: Option<SessionId>,
) -> DeliveryTerminal {
    match outcome {
        CompletionOutcome::Completed(_) | CompletionOutcome::CompletedWithoutResult => {
            DeliveryTerminal::runtime_completion(
                meerkat_schedule::RuntimeCompletionOutcome::Completed,
                None,
                None,
            )
        }
        CompletionOutcome::CallbackPending {
            tool_name, args, ..
        } => {
            let runtime_outcome =
                meerkat_schedule::RuntimeDeliveryOutcome::CompletionCallbackPending {
                    tool_name,
                    payload: args,
                };
            terminal_from_runtime_completion_outcome(
                meerkat_schedule::RuntimeCompletionOutcome::CallbackPending,
                runtime_outcome,
            )
        }
        CompletionOutcome::CallbackBatchPending { pending_tool_calls } => {
            let first = pending_tool_calls.first();
            let tool_name = first
                .map(|call| call.tool_name.clone())
                .unwrap_or_else(|| "callback_batch".to_string());
            let runtime_outcome =
                meerkat_schedule::RuntimeDeliveryOutcome::CompletionCallbackPending {
                    tool_name,
                    payload: serde_json::json!({
                        "pending_tool_calls": pending_tool_calls,
                    }),
                };
            terminal_from_runtime_completion_outcome(
                meerkat_schedule::RuntimeCompletionOutcome::CallbackPending,
                runtime_outcome,
            )
        }
        CompletionOutcome::Cancelled => {
            let runtime_outcome = meerkat_schedule::RuntimeDeliveryOutcome::CompletionAbandoned {
                detail: "request cancelled".to_string(),
            };
            terminal_from_runtime_completion_outcome(
                meerkat_schedule::RuntimeCompletionOutcome::Cancelled,
                runtime_outcome,
            )
        }
        CompletionOutcome::Abandoned { reason, error }
        | CompletionOutcome::AbandonedWithError { reason, error } => {
            let error_detail =
                serde_json::to_string(&error).unwrap_or_else(|_| "<unserializable>".to_string());
            let runtime_outcome = meerkat_schedule::RuntimeDeliveryOutcome::CompletionAbandoned {
                detail: format!("{reason}; error={error_detail}"),
            };
            terminal_from_runtime_completion_outcome(
                meerkat_schedule::RuntimeCompletionOutcome::Abandoned,
                runtime_outcome,
            )
        }
        CompletionOutcome::CompletedWithFinalizationFailure { error, .. } => {
            let detail = serde_json::to_string(&error)
                .unwrap_or_else(|_| "turn finalization failed".to_string());
            DeliveryTerminal::runtime_completion(
                meerkat_schedule::RuntimeCompletionOutcome::FinalizationFailed,
                Some(detail),
                None,
            )
        }
        CompletionOutcome::RuntimeTerminated { reason, error } => {
            let error_detail =
                serde_json::to_string(&error).unwrap_or_else(|_| "<unserializable>".to_string());
            let runtime_outcome =
                meerkat_schedule::RuntimeDeliveryOutcome::CompletionRuntimeTerminated {
                    detail: format!("{reason}; error={error_detail}"),
                };
            terminal_from_runtime_completion_outcome(
                meerkat_schedule::RuntimeCompletionOutcome::RuntimeTerminated,
                runtime_outcome,
            )
        }
    }
}

fn terminal_from_runtime_completion_outcome(
    outcome: meerkat_schedule::RuntimeCompletionOutcome,
    runtime_outcome: meerkat_schedule::RuntimeDeliveryOutcome,
) -> DeliveryTerminal {
    let detail = runtime_outcome.detail();
    DeliveryTerminal::runtime_completion(outcome, Some(detail), Some(runtime_outcome))
}

pub fn immediate_completed_dispatch(
    occurrence: &Occurrence,
    correlation_id: Option<String>,
) -> DeliveryDispatch {
    immediate_completed_dispatch_with_admission_outcome(
        occurrence,
        correlation_id,
        ScheduleAdmissionOutcome::Accepted,
    )
}

pub fn immediate_completed_dispatch_with_admission_outcome(
    occurrence: &Occurrence,
    correlation_id: Option<String>,
    admission_outcome: ScheduleAdmissionOutcome,
) -> DeliveryDispatch {
    let mut receipt = DeliveryReceipt::new(
        occurrence.occurrence_id.clone(),
        occurrence.attempt_count,
        DeliveryReceiptStage::DispatchAccepted,
    );
    receipt.correlation_id = correlation_id.clone();
    receipt.runtime_outcome = Some(admission_outcome.runtime_outcome());
    DeliveryDispatch {
        receipt,
        correlation_id,
        materialized_session_id: None,
        completion: Box::pin(async { Ok(DeliveryTerminal::completed(None)) }),
    }
}

pub fn async_completion_dispatch(
    occurrence: &Occurrence,
    correlation_id: Option<String>,
    completion: DeliveryCompletion,
) -> DeliveryDispatch {
    async_completion_dispatch_with_admission_outcome(
        occurrence,
        correlation_id,
        ScheduleAdmissionOutcome::Accepted,
        completion,
    )
}

pub fn async_completion_dispatch_with_admission_outcome(
    occurrence: &Occurrence,
    correlation_id: Option<String>,
    admission_outcome: ScheduleAdmissionOutcome,
    completion: DeliveryCompletion,
) -> DeliveryDispatch {
    let mut receipt = DeliveryReceipt::new(
        occurrence.occurrence_id.clone(),
        occurrence.attempt_count,
        DeliveryReceiptStage::DispatchAccepted,
    );
    receipt.correlation_id = correlation_id.clone();
    receipt.runtime_outcome = Some(admission_outcome.runtime_outcome());
    DeliveryDispatch {
        receipt,
        correlation_id,
        materialized_session_id: None,
        completion,
    }
}

pub fn immediate_delivery_failure(
    occurrence: &Occurrence,
    detail: String,
    failure_reason: DeliveryFailureReason,
    correlation_id: Option<String>,
    materialized_session_id: Option<SessionId>,
) -> DeliveryDispatch {
    let mut receipt = DeliveryReceipt::new(
        occurrence.occurrence_id.clone(),
        occurrence.attempt_count,
        DeliveryReceiptStage::DispatchStarted,
    );
    receipt.correlation_id = correlation_id.clone();
    receipt.materialized_session_id = materialized_session_id.clone();
    DeliveryDispatch {
        receipt,
        correlation_id,
        materialized_session_id,
        completion: Box::pin(async move {
            Ok(DeliveryTerminal {
                phase: OccurrencePhase::DeliveryFailed,
                receipt: None,
                detail: Some(detail),
                delivery_failure_reason: Some(failure_reason),
                runtime_completion_outcome: None,
                runtime_outcome: None,
            })
        }),
    }
}

/// Project one runtime admission into the schedule driver's typed delivery
/// contract.
///
/// Runtime intentionally returns no live completion handle when an accepted
/// or deduplicated input is already terminal. That is success with a durable
/// terminal witness, not missing authority. This single adapter path asks the
/// runtime for the machine-authorized exact rich completion receipt and
/// replays it so every schedule surface preserves the same result class and
/// payload. A coarse input-terminal class is never treated as completion
/// authority.
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
pub async fn runtime_delivery_dispatch_from_admission(
    runtime_adapter: &MeerkatMachine,
    session_id: &SessionId,
    occurrence: &Occurrence,
    identity: &ScheduleDeliveryIdentity,
    outcome: meerkat_runtime::accept::AcceptOutcome,
    handle: Option<CompletionHandle>,
    materialized_session_id: Option<SessionId>,
) -> Result<DeliveryDispatch, ScheduleDomainError> {
    let (input_id, admission_outcome) = match outcome {
        meerkat_runtime::accept::AcceptOutcome::Accepted { input_id, .. } => {
            (input_id, ScheduleAdmissionOutcome::Accepted)
        }
        meerkat_runtime::accept::AcceptOutcome::Deduplicated { existing_id, .. } => {
            (existing_id, ScheduleAdmissionOutcome::Deduplicated)
        }
        meerkat_runtime::accept::AcceptOutcome::Rejected { reason } => {
            return Ok(immediate_delivery_failure(
                occurrence,
                reason.to_string(),
                DeliveryFailureReason::RuntimeRejected,
                Some(identity.correlation_id.clone()),
                materialized_session_id,
            ));
        }
        _ => {
            return Ok(immediate_delivery_failure(
                occurrence,
                "runtime returned an unknown admission outcome".to_string(),
                DeliveryFailureReason::RuntimeRejected,
                Some(identity.correlation_id.clone()),
                materialized_session_id,
            ));
        }
    };
    let correlation_id = Some(identity.correlation_id.clone());

    let accepted = match handle {
        Some(handle) => AcceptedScheduledInput::with_runtime_handle(correlation_id.clone(), handle),
        None => {
            match runtime_adapter
                .input_terminal_completion(session_id, &input_id)
                .await
            {
                Ok(Some(terminal)) => {
                    AcceptedScheduledInput::with_runtime_terminal(correlation_id.clone(), terminal)
                }
                Ok(None) => AcceptedScheduledInput::with_authority_unavailable(
                    correlation_id.clone(),
                    format!(
                        "runtime returned no completion handle and no exact terminal completion receipt for input {input_id}"
                    ),
                ),
                Err(error) => AcceptedScheduledInput::with_authority_unavailable(
                    correlation_id.clone(),
                    format!(
                        "runtime could not provide the exact terminal completion receipt for input {input_id}: {error}"
                    ),
                ),
            }
        }
    }
    .with_admission_outcome(admission_outcome);

    Ok(build_dispatch_from_accepted(
        occurrence,
        accepted,
        materialized_session_id,
    ))
}

/// Runtime-facing delivery identity for a scheduled occurrence: schedule +
/// occurrence ONLY.
///
/// `attempt_count` is deliberately excluded (2026-07 P0, renamed from
/// `schedule_attempt_idempotency_key`): the runtime dedupes admissions on
/// this exact string, so an attempt-varying key admitted every lease-expiry
/// reclaim as a brand-new input while the previous attempt's turn was still
/// running — a duplicate turn per reclaim. With the occurrence-level key, a
/// retry of a live or already-ran delivery deduplicates at admission and
/// attaches to the existing input's completion instead. Attempt counts and
/// claim tokens remain store-side claim FENCING only (stale-completion
/// screening is unchanged).
pub fn schedule_delivery_idempotency_key(occurrence: &Occurrence) -> String {
    ScheduleDeliveryIdentity::for_occurrence(occurrence).idempotency_key
}

#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
fn schedule_v0810_predecessor_delivery_keys(
    occurrence: &Occurrence,
) -> impl Iterator<Item = String> + '_ {
    (1..occurrence.attempt_count)
        .rev()
        .map(|predecessor_attempt| {
            format!(
                "schedule:{}:occurrence:{}:attempt:{predecessor_attempt}",
                occurrence.schedule_id, occurrence.occurrence_id
            )
        })
}

/// Resolve the one supported schedule-key upgrade boundary.
///
/// Meerkat 0.8.10 keyed a delivery by claim attempt. A redrive under 0.8.11
/// increments the durable attempt before delivery, so any earlier durable
/// attempt key can name work originally admitted by 0.8.10. The resolver
/// walks that finite key family newest-first: this remains correct after
/// repeated 0.8.11 reclaims that reused the same old binding. New occurrences
/// use the driver's 0.8.11 occurrence-stable key. This is intentionally not a
/// general legacy alias mechanism.
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
pub async fn schedule_runtime_delivery_idempotency_key(
    runtime_adapter: &MeerkatMachine,
    session_id: &SessionId,
    occurrence: &Occurrence,
    canonical_idempotency_key: &str,
) -> Result<String, ScheduleDomainError> {
    let canonical_key = canonical_idempotency_key.to_owned();
    if occurrence.attempt_count <= 1 {
        return Ok(canonical_key);
    }

    // Prefer the canonical 0.8.11 binding once it exists. Otherwise walk the
    // finite set of prior durable claim attempts newest-first. This remains
    // correct across more than one 0.8.11 crash: attempt N may have reused an
    // attempt-1 key admitted by 0.8.10, so checking only N-1 would lose the
    // bridge on the next reclaim.
    let canonical = runtime_adapter
        .input_state_by_idempotency_key(session_id, &canonical_key)
        .await
        .map_err(|error| ScheduleDomainError::Internal(error.to_string()))?;
    if canonical.is_some() {
        return Ok(canonical_key);
    }

    for candidate_key in schedule_v0810_predecessor_delivery_keys(occurrence) {
        let predecessor = runtime_adapter
            .input_state_by_idempotency_key(session_id, &candidate_key)
            .await
            .map_err(|error| ScheduleDomainError::Internal(error.to_string()))?;
        if predecessor.is_some() {
            if candidate_key != canonical_key {
                tracing::info!(
                    schedule_id = %occurrence.schedule_id,
                    occurrence_id = %occurrence.occurrence_id,
                    predecessor_key = %candidate_key,
                    "reusing Meerkat 0.8.10 schedule delivery identity"
                );
            }
            return Ok(candidate_key);
        }
    }
    Ok(canonical_key)
}

/// Project the driver's canonical string correlation into the runtime's UUID
/// carrier. Parsing here makes the schedule/runtime seam fail closed if those
/// two typed representations ever drift.
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
pub fn schedule_runtime_correlation_id(
    identity: &ScheduleDeliveryIdentity,
) -> Result<meerkat_runtime::CorrelationId, ScheduleDomainError> {
    let uuid = identity.correlation_id.parse().map_err(|error| {
        ScheduleDomainError::Internal(format!("invalid schedule correlation id: {error}"))
    })?;
    Ok(meerkat_runtime::CorrelationId::from_uuid(uuid))
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    /// Detail churn inside one stable class cannot punch through the incident
    /// rate limit. The latest detail is retained for the next heartbeat.
    #[test]
    fn incident_tracker_rate_limits_by_stable_class() {
        let heartbeat = std::time::Duration::from_secs(60);
        let mut tracker = ScheduleHostIncidentTracker::new(heartbeat);
        let start = meerkat_core::time_compat::Instant::now();

        assert!(matches!(
            tracker.observe(None, start),
            ScheduleHostHealthLog::Quiet
        ));

        assert!(matches!(
            tracker.observe(
                Some(ScheduleHostIncident::new(
                    ScheduleHostIncidentClass::DriverTickFailed,
                    "page 1 failed"
                )),
                start
            ),
            ScheduleHostHealthLog::Incident {
                class: ScheduleHostIncidentClass::DriverTickFailed,
                ..
            }
        ));

        // Different volatile detail, same class: still rate limited.
        assert!(matches!(
            tracker.observe(
                Some(ScheduleHostIncident::new(
                    ScheduleHostIncidentClass::DriverTickFailed,
                    "page 2 failed"
                )),
                start + std::time::Duration::from_millis(250)
            ),
            ScheduleHostHealthLog::Quiet
        ));

        match tracker.observe(
            Some(ScheduleHostIncident::new(
                ScheduleHostIncidentClass::DriverTickFailed,
                "page 3 failed",
            )),
            start + heartbeat + std::time::Duration::from_secs(1),
        ) {
            ScheduleHostHealthLog::Heartbeat {
                detail,
                consecutive,
                ..
            } => {
                assert_eq!(consecutive, 3);
                assert_eq!(detail, "page 3 failed");
            }
            other => panic!("expected heartbeat, got {other:?}"),
        }

        // A mechanism/class change is a distinct incident.
        assert!(matches!(
            tracker.observe(
                Some(ScheduleHostIncident::new(
                    ScheduleHostIncidentClass::NextActionReadFailed,
                    "index unavailable"
                )),
                start + heartbeat + std::time::Duration::from_secs(2)
            ),
            ScheduleHostHealthLog::Incident {
                class: ScheduleHostIncidentClass::NextActionReadFailed,
                ..
            }
        ));

        match tracker.observe(None, start + heartbeat + std::time::Duration::from_secs(3)) {
            ScheduleHostHealthLog::Recovered { after, .. } => assert_eq!(after, 4),
            other => panic!("expected recovery, got {other:?}"),
        }
    }

    /// A successful tick with row faults remains degraded, and its operator
    /// detail is bounded before entering the tracker.
    #[test]
    fn row_fault_incident_is_bounded_and_attributable() {
        let heartbeat = std::time::Duration::from_secs(60);
        let mut tracker = ScheduleHostIncidentTracker::new(heartbeat);
        let start = meerkat_core::time_compat::Instant::now();
        let mut report = meerkat_schedule::ScheduleTickReport::default();
        for index in 0..8 {
            report
                .occurrence_row_faults
                .push(meerkat_schedule::ScheduleStoreRowFault {
                    schedule_id: Some("sched-1".to_string()),
                    occurrence_id: Some(format!("occ-{index}")),
                    kind: meerkat_schedule::ScheduleStoreRowFaultKind::Deserialization,
                    detail: "poisoned row".repeat(200),
                });
        }
        let incident = incident_from_tick_report(&report).expect("row faults are unhealthy");

        match tracker.observe(Some(incident), start) {
            ScheduleHostHealthLog::Incident { class, detail } => {
                assert_eq!(class, ScheduleHostIncidentClass::DriverRowFaults);
                assert!(detail.contains("occ-0"), "{detail}");
                assert!(detail.contains("additional fault(s) omitted"), "{detail}");
                assert!(!detail.contains("occ-7"), "{detail}");
                assert!(
                    detail.len() < 2_500,
                    "bounded detail grew to {}",
                    detail.len()
                );
            }
            other => panic!("expected incident, got {other:?}"),
        }
    }

    /// Rotating durable pages remain one row-fault incident class rather than
    /// producing one ERROR per page.
    #[test]
    fn rotating_row_fault_pages_do_not_flood_incidents() {
        let heartbeat = std::time::Duration::from_secs(60);
        let mut tracker = ScheduleHostIncidentTracker::new(heartbeat);
        let start = meerkat_core::time_compat::Instant::now();

        for page in 0..4 {
            let action = tracker.observe(
                Some(ScheduleHostIncident::new(
                    ScheduleHostIncidentClass::DriverRowFaults,
                    format!("fault page {page}"),
                )),
                start + std::time::Duration::from_millis(250 * page),
            );
            if page == 0 {
                assert!(matches!(action, ScheduleHostHealthLog::Incident { .. }));
            } else {
                assert!(matches!(action, ScheduleHostHealthLog::Quiet));
            }
        }
    }

    /// 2026-07-29 incident: a failing tick retried at the fixed 4Hz interval
    /// forever (query spam priced in currency on remote stores). The backoff
    /// must escalate immediately on every failing tick, cap, and snap back
    /// to the base interval on the first progressing tick.
    #[test]
    fn tick_backoff_escalates_on_failing_ticks_and_resets_on_progress() {
        let base = Duration::from_millis(250);
        let cap = Duration::from_secs(30);
        let mut backoff = TickBackoff::new(base, cap, 8);
        let failure: Result<meerkat_schedule::ScheduleTickReport, _> = Err(
            meerkat_schedule::ScheduleDomainError::Internal("store unavailable".to_string()),
        );

        // Each failing tick doubles: 500ms, 1s, 2s, ...
        assert_eq!(backoff.observe(&failure), Duration::from_millis(500));
        assert_eq!(backoff.observe(&failure), Duration::from_millis(1000));
        assert_eq!(backoff.observe(&failure), Duration::from_millis(2000));
        // ... until the cap holds.
        for _ in 0..16 {
            assert!(backoff.observe(&failure) <= cap);
        }
        assert_eq!(backoff.observe(&failure), cap);

        // A progressing tick resets to the base interval at once.
        let progressing: Result<_, meerkat_schedule::ScheduleDomainError> =
            Ok(meerkat_schedule::ScheduleTickReport {
                claimed_occurrences: 1,
                ..meerkat_schedule::ScheduleTickReport::default()
            });
        assert_eq!(backoff.observe(&progressing), base);
        // And the next failure starts escalating from the base again.
        assert_eq!(backoff.observe(&failure), Duration::from_millis(500));
    }

    /// Idle (successful but zero-progress) ticks keep the fast cadence
    /// through the grace run — freshly created work must be picked up
    /// promptly — then escalate, and any progressing tick resets both the
    /// interval and the grace counter.
    #[test]
    fn tick_backoff_tolerates_idle_grace_then_escalates_and_resets() {
        let base = Duration::from_millis(250);
        let cap = Duration::from_secs(30);
        let grace = 3;
        let mut backoff = TickBackoff::new(base, cap, grace);
        let idle: Result<_, meerkat_schedule::ScheduleDomainError> =
            Ok(meerkat_schedule::ScheduleTickReport::default());
        let progressing: Result<_, meerkat_schedule::ScheduleDomainError> =
            Ok(meerkat_schedule::ScheduleTickReport {
                planned_occurrences: 1,
                ..meerkat_schedule::ScheduleTickReport::default()
            });

        // Grace run: the base interval holds for `grace` idle ticks.
        for _ in 0..grace {
            assert_eq!(backoff.observe(&idle), base);
        }
        // Past the grace run, idle ticks escalate.
        assert_eq!(backoff.observe(&idle), Duration::from_millis(500));
        assert_eq!(backoff.observe(&idle), Duration::from_millis(1000));

        // Progress resets the interval AND re-arms the grace run.
        assert_eq!(backoff.observe(&progressing), base);
        for _ in 0..grace {
            assert_eq!(backoff.observe(&idle), base);
        }
        assert_eq!(backoff.observe(&idle), Duration::from_millis(500));
    }

    /// Ask 16 regression, loop-level: the spawned schedule host loop must
    /// FEED tick outcomes into the health tracker and log them. Reverting
    /// the loop body to `let _ = driver.tick_once().await;` (the exact
    /// silent-discard shape the field incident hit) fails this test.
    #[tokio::test]
    async fn spawn_schedule_host_logs_failing_ticks() {
        use uuid::Uuid;

        struct FailingScheduleStore {
            wake_mode: meerkat_schedule::ScheduleStoreWakeMode,
            durable_wake_attempts: Arc<AtomicUsize>,
            list_schedules_attempts: Arc<AtomicUsize>,
        }

        #[async_trait]
        impl ScheduleStore for FailingScheduleStore {
            fn kind(&self) -> meerkat_schedule::ScheduleStoreKind {
                meerkat_schedule::ScheduleStoreKind::Custom
            }

            fn wake_mode(&self) -> meerkat_schedule::ScheduleStoreWakeMode {
                self.wake_mode
            }

            async fn wait_for_durable_wake(
                &self,
            ) -> Result<(), meerkat_schedule::ScheduleStoreError> {
                self.durable_wake_attempts
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic durable wake outage".to_string(),
                ))
            }

            async fn get_store_time_utc(
                &self,
            ) -> Result<chrono::DateTime<chrono::Utc>, meerkat_schedule::ScheduleStoreError>
            {
                Ok(chrono::Utc::now())
            }

            async fn next_action_time_utc(
                &self,
            ) -> Result<
                meerkat_schedule::ScheduleStoreActionTime,
                meerkat_schedule::ScheduleStoreError,
            > {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn read_due_refill_candidates(
                &self,
                _limit: usize,
            ) -> Result<meerkat_schedule::ScheduleRefillBatch, meerkat_schedule::ScheduleStoreError>
            {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn commit_schedule_write(
                &self,
                _write: meerkat_schedule::AuthorizedScheduleWrite,
            ) -> Result<(), meerkat_schedule::ScheduleStoreError> {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn get_schedule(
                &self,
                _schedule_id: &meerkat_schedule::ScheduleId,
            ) -> Result<Option<meerkat_schedule::Schedule>, meerkat_schedule::ScheduleStoreError>
            {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn list_schedules(
                &self,
                _filter: meerkat_schedule::ScheduleFilter,
            ) -> Result<Vec<meerkat_schedule::Schedule>, meerkat_schedule::ScheduleStoreError>
            {
                self.list_schedules_attempts
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn commit_occurrence_write(
                &self,
                _write: meerkat_schedule::AuthorizedOccurrenceWrite,
            ) -> Result<(), meerkat_schedule::ScheduleStoreError> {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn commit_occurrence_writes(
                &self,
                _writes: Vec<meerkat_schedule::AuthorizedOccurrenceWrite>,
            ) -> Result<(), meerkat_schedule::ScheduleStoreError> {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn commit_schedule_mutation(
                &self,
                _schedule: meerkat_schedule::AuthorizedScheduleWrite,
                _occurrences: Vec<meerkat_schedule::AuthorizedOccurrenceWrite>,
            ) -> Result<meerkat_schedule::Schedule, meerkat_schedule::ScheduleStoreError>
            {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn commit_schedule_refill(
                &self,
                _schedule: meerkat_schedule::AuthorizedScheduleWrite,
                _occurrences: Vec<meerkat_schedule::AuthorizedOccurrenceWrite>,
                _next_refill_at_utc: Option<chrono::DateTime<chrono::Utc>>,
            ) -> Result<meerkat_schedule::Schedule, meerkat_schedule::ScheduleStoreError>
            {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn record_refill_deadline_if_current(
                &self,
                _schedule_id: &meerkat_schedule::ScheduleId,
                _expected_revision: meerkat_schedule::ScheduleRevision,
                _expected_refill_at_utc: chrono::DateTime<chrono::Utc>,
                _next_refill_at_utc: Option<chrono::DateTime<chrono::Utc>>,
            ) -> Result<(), meerkat_schedule::ScheduleStoreError> {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn get_occurrence(
                &self,
                _occurrence_id: &meerkat_schedule::OccurrenceId,
            ) -> Result<Option<Occurrence>, meerkat_schedule::ScheduleStoreError> {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn list_occurrences(
                &self,
                _filter: meerkat_schedule::OccurrenceFilter,
            ) -> Result<Vec<Occurrence>, meerkat_schedule::ScheduleStoreError> {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn append_receipt(
                &self,
                _receipt: meerkat_schedule::DeliveryReceipt,
            ) -> Result<(), meerkat_schedule::ScheduleStoreError> {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn list_receipts(
                &self,
                _occurrence_id: &meerkat_schedule::OccurrenceId,
            ) -> Result<Vec<meerkat_schedule::DeliveryReceipt>, meerkat_schedule::ScheduleStoreError>
            {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn claim_due_occurrences(
                &self,
                _request: meerkat_schedule::ClaimDueRequest,
            ) -> Result<meerkat_schedule::ClaimDueResult, meerkat_schedule::ScheduleStoreError>
            {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn renew_occurrence_lease_if_current(
                &self,
                _request: meerkat_schedule::RenewOccurrenceLeaseRequest,
            ) -> Result<
                meerkat_schedule::RenewOccurrenceLeaseResult,
                meerkat_schedule::ScheduleStoreError,
            > {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn transition_occurrence_if_current(
                &self,
                _occurrence_id: &meerkat_schedule::OccurrenceId,
                _expected_attempt: u32,
                _expected_claim_token: Option<Uuid>,
                _transition: meerkat_schedule::OccurrenceLifecycleInput,
            ) -> Result<
                Option<(Occurrence, Vec<meerkat_schedule::OccurrenceLifecycleEffect>)>,
                meerkat_schedule::ScheduleStoreError,
            > {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }

            async fn transition_occurrence_with_receipt_if_current(
                &self,
                _occurrence_id: &meerkat_schedule::OccurrenceId,
                _expected_attempt: u32,
                _expected_claim_token: Option<Uuid>,
                _transition: meerkat_schedule::OccurrenceLifecycleInput,
                _runtime_outcome: Option<meerkat_schedule::RuntimeDeliveryOutcome>,
            ) -> Result<Option<Occurrence>, meerkat_schedule::ScheduleStoreError> {
                Err(meerkat_schedule::ScheduleStoreError::Internal(
                    "synthetic store outage".to_string(),
                ))
            }
        }

        #[derive(Clone)]
        struct SharedBuf(Arc<Mutex<Vec<u8>>>);

        impl std::io::Write for SharedBuf {
            fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
                self.0
                    .lock()
                    .expect("log buffer lock")
                    .extend_from_slice(buf);
                Ok(buf.len())
            }

            fn flush(&mut self) -> std::io::Result<()> {
                Ok(())
            }
        }

        let buf = Arc::new(Mutex::new(Vec::new()));
        let writer_buf = SharedBuf(Arc::clone(&buf));
        let subscriber = tracing_subscriber::fmt()
            .with_max_level(tracing::Level::ERROR)
            .with_writer(move || writer_buf.clone())
            .finish();
        let _guard = tracing::subscriber::set_default(subscriber);

        let local_list_schedules_attempts = Arc::new(AtomicUsize::new(0));
        let store = Arc::new(FailingScheduleStore {
            wake_mode: ScheduleStoreWakeMode::ProcessLocal,
            durable_wake_attempts: Arc::new(AtomicUsize::new(0)),
            list_schedules_attempts: Arc::clone(&local_list_schedules_attempts),
        }) as Arc<dyn ScheduleStore>;
        let service = ScheduleService::new(store);
        let session_host: Arc<dyn SurfaceScheduleSessionHost> = Arc::new(PanicOnMaterializeHost {
            materialize_calls: Arc::new(AtomicUsize::new(0)),
        });
        let mob_host: Arc<dyn SurfaceScheduleMobHost> = Arc::new(NoopScheduleMobHost::new(
            "mob targets unsupported in this test",
        ));
        let adapter = Arc::new(SharedScheduleTargetAdapter::new(
            service.clone(),
            session_host,
            mob_host,
        ));

        let handle = spawn_schedule_host(service, adapter, "tick-health-test");
        // cfg!(test) poll interval is 50ms; give the loop a few ticks.
        tokio::time::sleep(Duration::from_millis(300)).await;
        handle.shutdown().await;
        assert_eq!(
            local_list_schedules_attempts.load(std::sync::atomic::Ordering::Relaxed),
            0,
            "the long-lived worker must never run the catalog-wide compatibility migration"
        );

        // A custom push store whose wait primitive fails terminates only the
        // worker. The supervisor reports the stable incident class, backs
        // off, and starts another worker while its public handle stays live.
        let durable_wake_attempts = Arc::new(AtomicUsize::new(0));
        let push_list_schedules_attempts = Arc::new(AtomicUsize::new(0));
        let store = Arc::new(FailingScheduleStore {
            wake_mode: ScheduleStoreWakeMode::Push,
            durable_wake_attempts: Arc::clone(&durable_wake_attempts),
            list_schedules_attempts: Arc::clone(&push_list_schedules_attempts),
        }) as Arc<dyn ScheduleStore>;
        let service = ScheduleService::new(store);
        let session_host: Arc<dyn SurfaceScheduleSessionHost> = Arc::new(PanicOnMaterializeHost {
            materialize_calls: Arc::new(AtomicUsize::new(0)),
        });
        let mob_host: Arc<dyn SurfaceScheduleMobHost> = Arc::new(NoopScheduleMobHost::new(
            "mob targets unsupported in this test",
        ));
        let adapter = Arc::new(SharedScheduleTargetAdapter::new(
            service.clone(),
            session_host,
            mob_host,
        ));
        let handle = spawn_schedule_host(service, adapter, "push-wake-supervision-test");
        tokio::time::sleep(Duration::from_millis(300)).await;
        assert!(handle.is_running(), "supervisor must survive worker exits");
        handle.shutdown().await;
        assert!(
            durable_wake_attempts.load(std::sync::atomic::Ordering::Relaxed) >= 2,
            "bounded supervision must retry a failed custom push wait"
        );
        assert_eq!(
            push_list_schedules_attempts.load(std::sync::atomic::Ordering::Relaxed),
            0,
            "worker restarts must not repeat catalog-wide compatibility migration"
        );

        let logs = String::from_utf8(buf.lock().expect("log buffer lock").clone())
            .expect("captured logs should be utf8");
        assert!(
            logs.contains("schedule host entered an unhealthy state"),
            "the host loop must log failing tick outcomes, got: {logs}"
        );
        assert!(
            logs.contains("synthetic store outage"),
            "the incident log must carry the tick failure detail, got: {logs}"
        );
        assert!(
            logs.contains("durable_wake_failed"),
            "worker push-wait termination must be visible, got: {logs}"
        );
    }

    use async_trait::async_trait;
    use meerkat_schedule::ScheduleStore;
    use std::collections::BTreeMap;
    use std::sync::Mutex;

    fn sample_occurrence() -> Occurrence {
        let schedule = meerkat_schedule::Schedule::new(meerkat_schedule::CreateScheduleRequest {
            name: Some("schedule-host-test".to_string()),
            description: None,
            trigger: meerkat_schedule::TriggerSpec::Interval(
                meerkat_schedule::IntervalTriggerSpec {
                    start_at_utc: chrono::Utc::now(),
                    every_seconds: 60,
                    end_at_utc: None,
                },
            ),
            target: TargetBinding::session(SessionTargetBinding::ExactSession {
                session_id: SessionId::new(),
                action: ScheduledSessionAction::Prompt {
                    prompt: ContentInput::Text("hello".to_string()),
                    system_prompt: None,
                    render_metadata: None,
                    skill_refs: Vec::new(),
                    additional_instructions: Vec::new(),
                },
            }),
            misfire_policy: meerkat_schedule::MisfirePolicy::Skip,
            overlap_policy: meerkat_schedule::OverlapPolicy::SkipIfRunning,
            missing_target_policy: meerkat_schedule::MissingTargetPolicy::Skip,
            labels: BTreeMap::new(),
            planning_horizon_days: None,
            planning_horizon_occurrences: None,
        })
        .expect("sample schedule creation should pass generated authority");
        let mut occurrence = Occurrence::planned_from_schedule(
            &schedule,
            meerkat_schedule::OccurrenceOrdinal(0),
            chrono::Utc::now(),
        )
        .expect("sample occurrence planning should pass generated authority");
        occurrence.attempt_count = 1;
        occurrence
    }

    #[tokio::test]
    async fn durable_terminal_dedup_replays_completed_instead_of_failing_authority() {
        let occurrence = sample_occurrence();
        let accepted = AcceptedScheduledInput::with_runtime_terminal(
            Some("existing-input".to_string()),
            CompletionOutcome::CompletedWithoutResult,
        )
        .with_admission_outcome(ScheduleAdmissionOutcome::Deduplicated);

        let dispatch = build_dispatch_from_accepted(&occurrence, accepted, None);
        assert_eq!(
            dispatch.receipt.runtime_outcome,
            Some(meerkat_schedule::RuntimeDeliveryOutcome::AdmissionDeduplicated)
        );
        let terminal = dispatch
            .completion
            .await
            .expect("durable terminal witness should be replayable");

        assert_eq!(terminal.phase, OccurrencePhase::AwaitingCompletion);
        assert_eq!(
            terminal.runtime_completion_outcome,
            Some(meerkat_schedule::RuntimeCompletionOutcome::Completed)
        );
        assert_eq!(terminal.delivery_failure_reason, None);
    }

    #[tokio::test]
    async fn durable_terminal_replay_preserves_rich_failure_metadata() {
        let occurrence = sample_occurrence();
        let accepted = AcceptedScheduledInput::with_runtime_terminal(
            Some("existing-input".to_string()),
            CompletionOutcome::CompletedWithFinalizationFailure {
                error: meerkat_core::TurnErrorMetadata::runtime_apply_failure(
                    "checkpoint commit failed",
                ),
            },
        )
        .with_admission_outcome(ScheduleAdmissionOutcome::Deduplicated);

        let terminal = build_dispatch_from_accepted(&occurrence, accepted, None)
            .completion
            .await
            .expect("exact durable completion receipt should be replayable");

        assert_eq!(
            terminal.runtime_completion_outcome,
            Some(meerkat_schedule::RuntimeCompletionOutcome::FinalizationFailed)
        );
        let detail = terminal.detail.expect("rich failure metadata");
        assert!(detail.contains("runtime_apply_failure"));
        assert!(detail.contains("checkpoint commit failed"));
    }

    #[test]
    fn v0810_predecessor_keys_survive_more_than_one_reclaim() {
        let mut occurrence = sample_occurrence();
        assert_eq!(
            schedule_v0810_predecessor_delivery_keys(&occurrence).collect::<Vec<_>>(),
            Vec::<String>::new(),
            "a first 0.8.11 attempt cannot have a 0.8.10 predecessor"
        );

        occurrence.attempt_count = 3;
        assert_eq!(
            schedule_v0810_predecessor_delivery_keys(&occurrence).collect::<Vec<_>>(),
            vec![
                format!(
                    "schedule:{}:occurrence:{}:attempt:2",
                    occurrence.schedule_id, occurrence.occurrence_id
                ),
                format!(
                    "schedule:{}:occurrence:{}:attempt:1",
                    occurrence.schedule_id, occurrence.occurrence_id
                ),
            ],
            "attempt 3 must still discover an attempt-1 binding after attempt 2 also crashed"
        );
    }

    #[test]
    fn mob_member_identity_rejects_pre_v0810_schema() {
        let v1 = r#"mob_member:{"schema":"meerkat.schedule.mob_member_identity.v1","mob_id":"ops","member":"watcher"}"#;
        let v2 = r#"mob_member:{"schema":"meerkat.schedule.mob_member_identity.v2","mob_id":"ops","member":"watcher"}"#;

        assert!(parse_mob_member_schedule_identity(v1).is_none());
        assert_eq!(
            parse_mob_member_schedule_identity(v2),
            Some(MobMemberScheduleIdentity {
                mob_id: "ops".to_string(),
                member: "watcher".to_string(),
            })
        );
    }

    #[test]
    fn store_action_delay_uses_store_clock_and_overdue_work_preserves_backoff() {
        let store_now_utc = chrono::Utc::now();
        let future_action = meerkat_schedule::ScheduleStoreActionTime {
            store_now_utc,
            next_action_at_utc: Some(store_now_utc + ChronoDuration::seconds(3)),
        };
        assert_eq!(
            duration_until_store_action(future_action),
            Some(Duration::from_secs(3))
        );
        assert_eq!(
            idle_delay_for_store_action(
                future_action,
                ScheduleStoreWakeMode::BoundedPoll {
                    max_interval: Duration::from_secs(5)
                },
                Duration::from_secs(10),
                Duration::from_millis(250),
            ),
            Some(Duration::from_secs(3))
        );

        let overdue_action = meerkat_schedule::ScheduleStoreActionTime {
            store_now_utc,
            next_action_at_utc: Some(store_now_utc - ChronoDuration::milliseconds(1)),
        };
        assert_eq!(
            duration_until_store_action(overdue_action),
            Some(Duration::ZERO)
        );
        assert_eq!(
            idle_delay_for_store_action(
                overdue_action,
                ScheduleStoreWakeMode::Push,
                Duration::from_secs(5),
                Duration::from_millis(250),
            ),
            Some(Duration::from_secs(5))
        );

        let no_action = meerkat_schedule::ScheduleStoreActionTime {
            store_now_utc,
            next_action_at_utc: None,
        };
        assert_eq!(
            idle_delay_for_store_action(
                no_action,
                ScheduleStoreWakeMode::ProcessLocal,
                Duration::from_secs(5),
                Duration::from_millis(250),
            ),
            None
        );
        assert_eq!(
            idle_delay_for_store_action(
                no_action,
                ScheduleStoreWakeMode::BoundedPoll {
                    max_interval: Duration::from_secs(5)
                },
                Duration::from_secs(10),
                Duration::from_millis(250),
            ),
            Some(Duration::from_secs(5))
        );
        assert_eq!(
            idle_delay_for_store_action(
                no_action,
                ScheduleStoreWakeMode::BoundedPoll {
                    max_interval: Duration::from_millis(100)
                },
                Duration::from_secs(10),
                Duration::from_millis(250),
            ),
            Some(Duration::from_millis(100)),
            "a non-zero store convergence bound must not be widened to the host base cadence"
        );
        assert_eq!(
            idle_delay_for_store_action(
                no_action,
                ScheduleStoreWakeMode::BoundedPoll {
                    max_interval: Duration::ZERO
                },
                Duration::from_secs(10),
                Duration::from_millis(250),
            ),
            Some(Duration::from_millis(250)),
            "zero cannot authorize a hot poll loop"
        );
    }

    #[tokio::test]
    async fn noop_mob_host_reports_clear_feature_required_failure() {
        let host = NoopScheduleMobHost::new(
            "scheduled mob targets require the mob feature on the CLI host",
        );
        let binding = MobTargetBinding::Member {
            mob_id: "ops".to_string(),
            member_id: "deploy-monitor".to_string(),
            action: meerkat_schedule::ScheduledMobAction::Send {
                content: ContentInput::Text("Check deploy state.".to_string()),
                render_metadata: None,
            },
        };

        let probe = host
            .probe_mob_target(&binding)
            .await
            .expect("probe should succeed");
        let TargetProbeOutcome::Missing { detail } = probe else {
            panic!("expected no-op mob host to report missing, got {probe:?}");
        };
        assert_eq!(
            detail.as_deref(),
            Some("scheduled mob targets require the mob feature on the CLI host")
        );

        let occurrence = sample_occurrence();
        let identity = ScheduleDeliveryIdentity::for_occurrence(&occurrence);
        let dispatch = host
            .deliver_mob_target(&occurrence, &identity, &binding)
            .await
            .expect("delivery dispatch");
        let terminal = dispatch.completion.await.expect("delivery terminal");

        assert_eq!(terminal.phase, OccurrencePhase::DeliveryFailed);
        assert_eq!(
            terminal.detail.as_deref(),
            Some("scheduled mob targets require the mob feature on the CLI host")
        );
        assert_eq!(
            terminal.delivery_failure_reason,
            Some(DeliveryFailureReason::MobRejected)
        );
    }

    #[tokio::test]
    async fn accepted_schedule_dispatch_waits_for_runtime_completion_failure() {
        let terminal = delivery_terminal_from_completion_outcome(
            CompletionOutcome::CallbackPending {
                tool_use_id: "call-1".to_string(),
                tool_name: "external_approval".to_string(),
                args: serde_json::json!({"ticket": "INC-1"}),
            },
            None,
        );

        assert_eq!(terminal.phase, OccurrencePhase::AwaitingCompletion);
        assert_eq!(
            terminal.runtime_completion_outcome,
            Some(meerkat_schedule::RuntimeCompletionOutcome::CallbackPending)
        );
        assert!(
            terminal
                .detail
                .as_deref()
                .unwrap_or_default()
                .contains("external_approval")
        );
        assert!(terminal.runtime_outcome.is_some());
    }

    #[tokio::test]
    async fn accepted_schedule_dispatch_without_runtime_authority_reports_typed_completion_failure()
    {
        let occurrence = sample_occurrence();
        let dispatch = build_dispatch_from_accepted(
            &occurrence,
            AcceptedScheduledInput::with_authority_unavailable(
                Some("corr-1".to_string()),
                "runtime completion authority unavailable for terminal input",
            ),
            None,
        );

        let error = dispatch.completion.await.expect_err("completion failure");
        match error {
            ScheduleDomainError::DeliveryCompletionFailed { reason, detail } => {
                assert_eq!(
                    reason,
                    DeliveryCompletionFailureReason::RuntimeCompletionAuthorityUnavailable
                );
                assert_eq!(
                    detail,
                    "runtime completion authority unavailable for terminal input"
                );
            }
            other => panic!("unexpected completion error: {other}"),
        }
    }

    use std::sync::atomic::{AtomicUsize, Ordering};

    /// A session host that must never be asked to materialize. Any
    /// `materialize_session` call records a hit and returns an error so the
    /// Layer B reuse guard regression is caught as a test failure rather than
    /// a silent duplicate session.
    struct PanicOnMaterializeHost {
        materialize_calls: Arc<AtomicUsize>,
    }

    struct IdentityResolvingHost {
        current_session_id: Arc<Mutex<SessionId>>,
        delivered_session_id: Arc<Mutex<Option<SessionId>>>,
        legacy_session_id: Option<SessionId>,
    }

    struct RecordingMaterializeHost {
        materialized_config_prompt: Arc<Mutex<Option<String>>>,
        dispatched_turn_prompt: Arc<Mutex<Option<String>>>,
    }

    #[async_trait]
    impl SurfaceScheduleSessionHost for PanicOnMaterializeHost {
        async fn probe_session_target(
            &self,
            _binding: &SessionTargetBinding,
        ) -> Result<TargetProbeOutcome, ScheduleDomainError> {
            Ok(TargetProbeOutcome::Ready)
        }

        async fn materialize_session(
            &self,
            _occurrence: &Occurrence,
            _create: &SessionMaterializationSpec,
        ) -> Result<SessionId, ScheduleDomainError> {
            self.materialize_calls.fetch_add(1, Ordering::SeqCst);
            Err(ScheduleDomainError::Internal(
                "Layer B reuse guard must reuse the bound session, never materialize".to_string(),
            ))
        }

        async fn deliver_prompt(
            &self,
            _session_id: &SessionId,
            occurrence: &Occurrence,
            identity: &ScheduleDeliveryIdentity,
            _dispatch: ScheduledPromptDispatch,
        ) -> Result<DeliveryDispatch, ScheduleDomainError> {
            Ok(immediate_completed_dispatch(
                occurrence,
                Some(identity.correlation_id.clone()),
            ))
        }

        async fn deliver_event(
            &self,
            _session_id: &SessionId,
            occurrence: &Occurrence,
            identity: &ScheduleDeliveryIdentity,
            _dispatch: ScheduledEventDispatch,
        ) -> Result<DeliveryDispatch, ScheduleDomainError> {
            Ok(immediate_completed_dispatch(
                occurrence,
                Some(identity.correlation_id.clone()),
            ))
        }
    }

    #[async_trait]
    impl SurfaceScheduleSessionHost for IdentityResolvingHost {
        async fn probe_session_target(
            &self,
            _binding: &SessionTargetBinding,
        ) -> Result<TargetProbeOutcome, ScheduleDomainError> {
            Ok(TargetProbeOutcome::Ready)
        }

        async fn probe_identity_target(
            &self,
            binding: &IdentityTargetBinding,
        ) -> Result<TargetProbeOutcome, ScheduleDomainError> {
            assert_eq!(binding.identity(), "domain:security");
            Ok(TargetProbeOutcome::Ready)
        }

        async fn resolve_identity_target(
            &self,
            binding: &IdentityTargetBinding,
        ) -> Result<Option<SessionId>, ScheduleDomainError> {
            assert_eq!(binding.identity(), "domain:security");
            Ok(Some(
                self.current_session_id
                    .lock()
                    .expect("current session lock")
                    .clone(),
            ))
        }

        async fn recover_session_target_identity(
            &self,
            binding: &SessionTargetBinding,
        ) -> Result<Option<IdentityTargetBinding>, ScheduleDomainError> {
            let Some(legacy_session_id) = &self.legacy_session_id else {
                return Ok(None);
            };
            if binding.resolved_session_id() != Some(legacy_session_id) {
                return Ok(None);
            }
            Ok(Some(IdentityTargetBinding::resumable(
                "domain:security",
                binding.action().clone(),
            )))
        }

        async fn materialize_session(
            &self,
            _occurrence: &Occurrence,
            _create: &SessionMaterializationSpec,
        ) -> Result<SessionId, ScheduleDomainError> {
            panic!("identity targets must resolve existing materialized sessions")
        }

        async fn deliver_prompt(
            &self,
            session_id: &SessionId,
            occurrence: &Occurrence,
            identity: &ScheduleDeliveryIdentity,
            dispatch: ScheduledPromptDispatch,
        ) -> Result<DeliveryDispatch, ScheduleDomainError> {
            assert_eq!(dispatch.materialized_session_id, None);
            *self
                .delivered_session_id
                .lock()
                .expect("delivered session lock") = Some(session_id.clone());
            Ok(immediate_completed_dispatch(
                occurrence,
                Some(identity.correlation_id.clone()),
            ))
        }

        async fn deliver_event(
            &self,
            _session_id: &SessionId,
            occurrence: &Occurrence,
            identity: &ScheduleDeliveryIdentity,
            _dispatch: ScheduledEventDispatch,
        ) -> Result<DeliveryDispatch, ScheduleDomainError> {
            Ok(immediate_completed_dispatch(
                occurrence,
                Some(identity.correlation_id.clone()),
            ))
        }
    }

    #[async_trait]
    impl SurfaceScheduleSessionHost for RecordingMaterializeHost {
        async fn probe_session_target(
            &self,
            _binding: &SessionTargetBinding,
        ) -> Result<TargetProbeOutcome, ScheduleDomainError> {
            Ok(TargetProbeOutcome::Ready)
        }

        async fn materialize_session(
            &self,
            occurrence: &Occurrence,
            create: &SessionMaterializationSpec,
        ) -> Result<SessionId, ScheduleDomainError> {
            *self
                .materialized_config_prompt
                .lock()
                .expect("materialized prompt lock") = create.system_prompt.clone();
            Ok(occurrence.materialized_session_id())
        }

        async fn deliver_prompt(
            &self,
            _session_id: &SessionId,
            occurrence: &Occurrence,
            identity: &ScheduleDeliveryIdentity,
            dispatch: ScheduledPromptDispatch,
        ) -> Result<DeliveryDispatch, ScheduleDomainError> {
            *self
                .dispatched_turn_prompt
                .lock()
                .expect("dispatch prompt lock") = dispatch.system_prompt;
            Ok(immediate_completed_dispatch(
                occurrence,
                Some(identity.correlation_id.clone()),
            ))
        }

        async fn deliver_event(
            &self,
            _session_id: &SessionId,
            occurrence: &Occurrence,
            identity: &ScheduleDeliveryIdentity,
            _dispatch: ScheduledEventDispatch,
        ) -> Result<DeliveryDispatch, ScheduleDomainError> {
            Ok(immediate_completed_dispatch(
                occurrence,
                Some(identity.correlation_id.clone()),
            ))
        }
    }

    fn materialize_on_demand_target() -> TargetBinding {
        TargetBinding::session(SessionTargetBinding::materialize_on_demand(
            SessionMaterializationSpec {
                model: "claude-sonnet-4-6".to_string(),
                system_prompt: None,
                max_tokens: None,
                provider: None,
                output_schema: None,
                structured_output_retries: None,
                provider_params: None,
                comms_name: None,
                peer_meta: None,
                labels: BTreeMap::new(),
                preload_skills: Vec::new(),
                additional_instructions: Vec::new(),
                realm_id: None,
                instance_id: None,
                backend: None,
                config_generation: None,
                keep_alive: false,
                app_context: None,
            },
            ScheduledSessionAction::Prompt {
                prompt: ContentInput::Text("scheduled prompt".to_string()),
                system_prompt: None,
                render_metadata: None,
                skill_refs: Vec::new(),
                additional_instructions: Vec::new(),
            },
        ))
    }

    #[tokio::test]
    async fn resolve_session_reuses_authoritative_bound_id_without_materializing_on_stale_snapshot()
    {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let service = ScheduleService::new(store.clone());
        let schedule = service
            .create(meerkat_schedule::CreateScheduleRequest {
                name: Some("layer-b-reuse".to_string()),
                description: None,
                trigger: meerkat_schedule::TriggerSpec::Once {
                    due_at_utc: chrono::Utc::now() - ChronoDuration::seconds(1),
                },
                target: materialize_on_demand_target(),
                misfire_policy: meerkat_schedule::MisfirePolicy::Skip,
                overlap_policy: meerkat_schedule::OverlapPolicy::AllowConcurrent,
                missing_target_policy: meerkat_schedule::MissingTargetPolicy::Skip,
                labels: BTreeMap::new(),
                planning_horizon_days: Some(1),
                planning_horizon_occurrences: Some(1),
            })
            .await
            .expect("schedule create should plan one occurrence");

        let occurrence = service
            .list_occurrences(&schedule.schedule_id)
            .await
            .expect("list occurrences")
            .into_iter()
            .next()
            .expect("schedule should have planned one occurrence");

        // A prior attempt materialized and committed the bound id to the
        // authoritative schedule target (and pending occurrences).
        let bound_id = SessionId::new();
        service
            .bind_materialized_session_for_occurrence(&occurrence, &bound_id)
            .await
            .expect("bind should commit the materialized session id");

        // The in-flight occurrence snapshot is STALE: it still reports
        // `bound_session_id: None`, exactly the window the residual described.
        let mut stale = occurrence.clone();
        stale.target_snapshot = materialize_on_demand_target();
        let TargetBinding::Session(binding) = &stale.target_snapshot else {
            panic!("expected a session target binding");
        };
        assert!(
            binding.resolved_session_id().is_none(),
            "stale snapshot must start unbound to exercise the reuse guard"
        );

        let materialize_calls = Arc::new(AtomicUsize::new(0));
        let session_host: Arc<dyn SurfaceScheduleSessionHost> = Arc::new(PanicOnMaterializeHost {
            materialize_calls: Arc::clone(&materialize_calls),
        });
        let mob_host: Arc<dyn SurfaceScheduleMobHost> = Arc::new(NoopScheduleMobHost::new(
            "mob targets unsupported in this test",
        ));
        let adapter = SharedScheduleTargetAdapter::new(service, session_host, mob_host);

        let TargetBinding::Session(stale_binding) = &stale.target_snapshot else {
            panic!("expected a session target binding");
        };
        let delivery_identity = ScheduleDeliveryIdentity::for_occurrence(&stale);
        let resolved = adapter
            .resolve_session(&stale, &delivery_identity, stale_binding)
            .await
            .expect("reuse guard should resolve without a delivery failure");

        assert_eq!(resolved.session_id, bound_id);
        assert_eq!(resolved.materialized_session_id, Some(bound_id));
        assert_eq!(
            materialize_calls.load(Ordering::SeqCst),
            0,
            "Layer B guard must reuse the bound id, never call materialize_session"
        );
    }

    #[tokio::test]
    async fn materialized_prompt_system_is_dispatched_at_turn_boundary_only() {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let service = ScheduleService::new(store);
        let mut target = materialize_on_demand_target();
        let TargetBinding::Session(binding) = &mut target else {
            panic!("expected materialized session target");
        };
        let SessionTargetBinding::MaterializeOnDemandSession { create, action, .. } =
            binding.as_mut()
        else {
            panic!("expected materialized session target");
        };
        create.system_prompt = Some("materialization config".to_string());
        let ScheduledSessionAction::Prompt { system_prompt, .. } = action else {
            panic!("expected prompt action");
        };
        *system_prompt = Some("ordinary scheduled system".to_string());

        let schedule = service
            .create(meerkat_schedule::CreateScheduleRequest {
                name: Some("scheduled-system-boundary".to_string()),
                description: None,
                trigger: meerkat_schedule::TriggerSpec::Once {
                    due_at_utc: chrono::Utc::now() - ChronoDuration::seconds(1),
                },
                target,
                misfire_policy: meerkat_schedule::MisfirePolicy::Skip,
                overlap_policy: meerkat_schedule::OverlapPolicy::AllowConcurrent,
                missing_target_policy: meerkat_schedule::MissingTargetPolicy::Skip,
                labels: BTreeMap::new(),
                planning_horizon_days: Some(1),
                planning_horizon_occurrences: Some(1),
            })
            .await
            .expect("schedule create");
        let occurrence = service
            .list_occurrences(&schedule.schedule_id)
            .await
            .expect("list occurrences")
            .into_iter()
            .next()
            .expect("planned occurrence");
        let materialized_config_prompt = Arc::new(Mutex::new(None));
        let dispatched_turn_prompt = Arc::new(Mutex::new(None));
        let session_host: Arc<dyn SurfaceScheduleSessionHost> =
            Arc::new(RecordingMaterializeHost {
                materialized_config_prompt: Arc::clone(&materialized_config_prompt),
                dispatched_turn_prompt: Arc::clone(&dispatched_turn_prompt),
            });
        let mob_host: Arc<dyn SurfaceScheduleMobHost> = Arc::new(NoopScheduleMobHost::new(
            "mob targets unsupported in this test",
        ));
        let adapter = SharedScheduleTargetAdapter::new(service, session_host, mob_host);
        let identity = ScheduleDeliveryIdentity::for_occurrence(&occurrence);

        let dispatch = adapter
            .deliver_occurrence(&occurrence, &identity)
            .await
            .expect("delivery dispatch");
        dispatch.completion.await.expect("delivery completion");

        assert_eq!(
            materialized_config_prompt
                .lock()
                .expect("materialized prompt lock")
                .as_deref(),
            Some("materialization config")
        );
        assert_eq!(
            dispatched_turn_prompt
                .lock()
                .expect("dispatch prompt lock")
                .as_deref(),
            Some("ordinary scheduled system")
        );
    }

    #[tokio::test]
    async fn identity_target_resolves_current_session_at_delivery_time() {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let service = ScheduleService::new(store);
        let current_session_id = Arc::new(Mutex::new(SessionId::new()));
        let delivered_session_id = Arc::new(Mutex::new(None));
        let session_host: Arc<dyn SurfaceScheduleSessionHost> = Arc::new(IdentityResolvingHost {
            current_session_id: Arc::clone(&current_session_id),
            delivered_session_id: Arc::clone(&delivered_session_id),
            legacy_session_id: None,
        });
        let mob_host: Arc<dyn SurfaceScheduleMobHost> = Arc::new(NoopScheduleMobHost::new(
            "mob targets unsupported in this test",
        ));
        let adapter = SharedScheduleTargetAdapter::new(service, session_host, mob_host);

        let mut occurrence = sample_occurrence();
        occurrence.target_snapshot = TargetBinding::identity(IdentityTargetBinding::resumable(
            "domain:security",
            ScheduledSessionAction::Prompt {
                prompt: ContentInput::Text("identity check".to_string()),
                system_prompt: None,
                render_metadata: None,
                skill_refs: Vec::new(),
                additional_instructions: Vec::new(),
            },
        ));

        let session_after_restart = SessionId::new();
        *current_session_id.lock().expect("current session lock") = session_after_restart.clone();

        let probe = adapter
            .probe_target(&occurrence)
            .await
            .expect("identity probe should resolve through host");
        assert!(matches!(probe, TargetProbeOutcome::Ready));

        let delivery_identity = ScheduleDeliveryIdentity::for_occurrence(&occurrence);
        let dispatch = adapter
            .deliver_occurrence(&occurrence, &delivery_identity)
            .await
            .expect("identity delivery should dispatch");
        let terminal = dispatch.completion.await.expect("delivery completion");
        assert_eq!(terminal.phase, OccurrencePhase::Completed);
        assert_eq!(
            delivered_session_id
                .lock()
                .expect("delivered session lock")
                .as_ref(),
            Some(&session_after_restart)
        );
    }

    #[tokio::test]
    async fn migrate_recoverable_session_target_persists_identity_target() {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let service = ScheduleService::new(store.clone());
        let legacy_session_id = SessionId::new();
        let schedule = service
            .create(meerkat_schedule::CreateScheduleRequest {
                name: Some("legacy-owned-session".to_string()),
                description: None,
                trigger: meerkat_schedule::TriggerSpec::Interval(
                    meerkat_schedule::IntervalTriggerSpec {
                        start_at_utc: chrono::Utc::now(),
                        every_seconds: 60,
                        end_at_utc: None,
                    },
                ),
                target: TargetBinding::session(SessionTargetBinding::ResumableSession {
                    session_id: legacy_session_id.clone(),
                    action: ScheduledSessionAction::Prompt {
                        prompt: ContentInput::Text("legacy identity check".to_string()),
                        system_prompt: None,
                        render_metadata: None,
                        skill_refs: Vec::new(),
                        additional_instructions: Vec::new(),
                    },
                }),
                misfire_policy: meerkat_schedule::MisfirePolicy::Skip,
                overlap_policy: meerkat_schedule::OverlapPolicy::SkipIfRunning,
                missing_target_policy: meerkat_schedule::MissingTargetPolicy::MarkMisfired,
                labels: BTreeMap::new(),
                planning_horizon_days: Some(1),
                planning_horizon_occurrences: Some(1),
            })
            .await
            .expect("schedule create should succeed");
        let current_session_id = Arc::new(Mutex::new(SessionId::new()));
        let delivered_session_id = Arc::new(Mutex::new(None));
        let session_host: Arc<dyn SurfaceScheduleSessionHost> = Arc::new(IdentityResolvingHost {
            current_session_id,
            delivered_session_id,
            legacy_session_id: Some(legacy_session_id),
        });
        let mob_host: Arc<dyn SurfaceScheduleMobHost> = Arc::new(NoopScheduleMobHost::new(
            "mob targets unsupported in this test",
        ));
        let adapter = SharedScheduleTargetAdapter::new(service.clone(), session_host, mob_host);

        let migrated = adapter
            .migrate_recoverable_session_targets()
            .await
            .expect("migration should succeed");
        assert_eq!(migrated, 1);

        let updated = store
            .get_schedule(&schedule.schedule_id)
            .await
            .expect("store read")
            .expect("schedule still exists");
        let TargetBinding::Identity(binding) = updated.target else {
            panic!("legacy session target should migrate to identity target");
        };
        assert_eq!(binding.identity(), "domain:security");
    }

    // -----------------------------------------------------------------------
    // HostRunnable targets
    // -----------------------------------------------------------------------

    use meerkat_schedule::{
        HostRunnable, HostRunnableError, HostRunnableName, HostRunnableOutcome,
        HostRunnableRegistry, OccurrenceFailureClass,
    };

    struct RecordingHostRunnable {
        invocations: Arc<Mutex<Vec<HostRunnableInvocation>>>,
        failure_detail: Option<String>,
    }

    #[async_trait]
    impl HostRunnable for RecordingHostRunnable {
        async fn run(
            &self,
            invocation: HostRunnableInvocation,
        ) -> Result<HostRunnableOutcome, HostRunnableError> {
            self.invocations
                .lock()
                .expect("invocation lock")
                .push(invocation);
            match &self.failure_detail {
                Some(detail) => Err(HostRunnableError::Failed {
                    detail: detail.clone(),
                }),
                None => Ok(HostRunnableOutcome::completed()),
            }
        }
    }

    fn runnable_name(value: &str) -> HostRunnableName {
        HostRunnableName::parse(value).expect("valid runnable name")
    }

    fn host_runnable_target(name: &str, params: Option<&str>) -> TargetBinding {
        TargetBinding::host_runnable(HostRunnableTargetBinding {
            runnable: runnable_name(name),
            params: params.map(|raw| HostRunnableParams::parse(raw).expect("valid raw params")),
        })
    }

    fn registry_with(name: &str, runnable: Arc<dyn HostRunnable>) -> Arc<dyn ScheduleRunnableHost> {
        let mut registry = HostRunnableRegistry::new();
        registry
            .register(runnable_name(name), runnable)
            .expect("runnable registration");
        Arc::new(registry)
    }

    fn host_runnable_adapter(
        service: ScheduleService,
        runnable_host: Option<Arc<dyn ScheduleRunnableHost>>,
    ) -> SharedScheduleTargetAdapter {
        let session_host: Arc<dyn SurfaceScheduleSessionHost> = Arc::new(PanicOnMaterializeHost {
            materialize_calls: Arc::new(AtomicUsize::new(0)),
        });
        let mob_host: Arc<dyn SurfaceScheduleMobHost> = Arc::new(NoopScheduleMobHost::new(
            "mob targets unsupported in this test",
        ));
        let adapter = SharedScheduleTargetAdapter::new(service, session_host, mob_host);
        match runnable_host {
            Some(runnable_host) => adapter.with_runnable_host(runnable_host),
            None => adapter,
        }
    }

    fn recording_runnable(
        failure_detail: Option<&str>,
    ) -> (
        Arc<RecordingHostRunnable>,
        Arc<Mutex<Vec<HostRunnableInvocation>>>,
    ) {
        let invocations = Arc::new(Mutex::new(Vec::new()));
        let runnable = Arc::new(RecordingHostRunnable {
            invocations: Arc::clone(&invocations),
            failure_detail: failure_detail.map(str::to_string),
        });
        (runnable, invocations)
    }

    #[tokio::test]
    async fn host_runnable_probe_matrix_reports_ready_only_when_registered() {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let service = ScheduleService::new(store);
        let mut occurrence = sample_occurrence();
        occurrence.target_snapshot = host_runnable_target("nightly-report", None);

        // No runnable host configured on the surface.
        let adapter = host_runnable_adapter(service.clone(), None);
        let probe = adapter.probe_target(&occurrence).await.expect("probe");
        let TargetProbeOutcome::Missing { detail } = probe else {
            panic!("expected missing probe without a runnable host, got {probe:?}");
        };
        assert!(
            detail
                .as_deref()
                .is_some_and(|detail| detail.contains("no runnable registry")),
            "missing detail should explain the absent registry: {detail:?}"
        );

        // A registry is configured but the named runnable is not registered.
        let (runnable, _invocations) = recording_runnable(None);
        let adapter = host_runnable_adapter(
            service.clone(),
            Some(registry_with("other-runnable", runnable)),
        );
        let probe = adapter.probe_target(&occurrence).await.expect("probe");
        let TargetProbeOutcome::Missing { detail } = probe else {
            panic!("expected missing probe for unregistered runnable, got {probe:?}");
        };
        assert!(
            detail
                .as_deref()
                .is_some_and(|detail| detail.contains("not registered")),
            "missing detail should name the unregistered runnable: {detail:?}"
        );

        // The named runnable is registered.
        let (runnable, _invocations) = recording_runnable(None);
        let adapter =
            host_runnable_adapter(service, Some(registry_with("nightly-report", runnable)));
        let probe = adapter.probe_target(&occurrence).await.expect("probe");
        assert!(matches!(probe, TargetProbeOutcome::Ready));
    }

    #[tokio::test]
    async fn host_runnable_delivery_without_registry_fails_target_missing() {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let adapter = host_runnable_adapter(ScheduleService::new(store), None);
        let mut occurrence = sample_occurrence();
        occurrence.target_snapshot = host_runnable_target("nightly-report", None);

        let delivery_identity = ScheduleDeliveryIdentity::for_occurrence(&occurrence);
        let dispatch = adapter
            .deliver_occurrence(&occurrence, &delivery_identity)
            .await
            .expect("delivery dispatch");
        let terminal = dispatch.completion.await.expect("delivery terminal");

        assert_eq!(terminal.phase, OccurrencePhase::DeliveryFailed);
        assert_eq!(
            terminal.delivery_failure_reason,
            Some(DeliveryFailureReason::TargetMissing)
        );
        assert!(
            terminal
                .detail
                .as_deref()
                .is_some_and(|detail| detail.contains("no runnable registry")),
            "failure detail should explain the absent registry: {:?}",
            terminal.detail
        );
    }

    #[tokio::test]
    async fn host_runnable_delivery_unregistered_fails_target_missing() {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let (runnable, invocations) = recording_runnable(None);
        let adapter = host_runnable_adapter(
            ScheduleService::new(store),
            Some(registry_with("other-runnable", runnable)),
        );
        let mut occurrence = sample_occurrence();
        occurrence.target_snapshot = host_runnable_target("nightly-report", None);

        let delivery_identity = ScheduleDeliveryIdentity::for_occurrence(&occurrence);
        let dispatch = adapter
            .deliver_occurrence(&occurrence, &delivery_identity)
            .await
            .expect("delivery dispatch");
        let terminal = dispatch.completion.await.expect("delivery terminal");

        assert_eq!(terminal.phase, OccurrencePhase::DeliveryFailed);
        assert_eq!(
            terminal.delivery_failure_reason,
            Some(DeliveryFailureReason::TargetMissing)
        );
        assert!(
            invocations.lock().expect("invocation lock").is_empty(),
            "an unregistered runnable must never be invoked"
        );
    }

    #[tokio::test]
    async fn host_runnable_delivery_success_completes_with_typed_invocation() {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let (runnable, invocations) = recording_runnable(None);
        let adapter = host_runnable_adapter(
            ScheduleService::new(store),
            Some(registry_with("nightly-report", runnable)),
        );
        let mut occurrence = sample_occurrence();
        occurrence.target_snapshot = host_runnable_target("nightly-report", Some(r#"{"depth":3}"#));

        let delivery_identity = ScheduleDeliveryIdentity::for_occurrence(&occurrence);
        let dispatch = adapter
            .deliver_occurrence(&occurrence, &delivery_identity)
            .await
            .expect("delivery dispatch");
        assert_eq!(
            dispatch.receipt.stage,
            DeliveryReceiptStage::DispatchAccepted
        );
        assert_eq!(
            dispatch.receipt.runtime_outcome,
            Some(meerkat_schedule::RuntimeDeliveryOutcome::AdmissionAccepted)
        );
        assert_eq!(
            dispatch.correlation_id.as_deref(),
            Some(delivery_identity.correlation_id.as_str())
        );
        let terminal = dispatch.completion.await.expect("delivery terminal");

        assert_eq!(terminal.phase, OccurrencePhase::Completed);
        assert_eq!(terminal.delivery_failure_reason, None);

        let recorded = invocations.lock().expect("invocation lock");
        assert_eq!(recorded.len(), 1);
        assert_eq!(recorded[0].occurrence_id, occurrence.occurrence_id);
        assert_eq!(recorded[0].schedule_id, occurrence.schedule_id);
        assert_eq!(
            recorded[0].delivery_idempotency_key,
            delivery_identity.idempotency_key
        );
        assert_eq!(recorded[0].runnable.as_str(), "nightly-report");
        assert_eq!(recorded[0].trigger_time, occurrence.due_at_utc);
        assert_eq!(
            recorded[0]
                .params
                .as_deref()
                .map(serde_json::value::RawValue::get),
            Some(r#"{"depth":3}"#)
        );
    }

    #[tokio::test]
    async fn host_runnable_delivery_callback_error_maps_to_runtime_rejected() {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let (runnable, _invocations) = recording_runnable(Some("downstream export failed"));
        let adapter = host_runnable_adapter(
            ScheduleService::new(store),
            Some(registry_with("nightly-report", runnable)),
        );
        let mut occurrence = sample_occurrence();
        occurrence.target_snapshot = host_runnable_target("nightly-report", None);

        let delivery_identity = ScheduleDeliveryIdentity::for_occurrence(&occurrence);
        let dispatch = adapter
            .deliver_occurrence(&occurrence, &delivery_identity)
            .await
            .expect("delivery dispatch");
        let terminal = dispatch.completion.await.expect("delivery terminal");

        assert_eq!(terminal.phase, OccurrencePhase::DeliveryFailed);
        assert_eq!(
            terminal.delivery_failure_reason,
            Some(DeliveryFailureReason::RuntimeRejected)
        );
        assert!(
            terminal
                .detail
                .as_deref()
                .is_some_and(|detail| detail.contains("downstream export failed")),
            "failure detail should carry the callback error: {:?}",
            terminal.detail
        );
    }

    async fn wait_for_occurrence_phase(
        service: &ScheduleService,
        schedule_id: &meerkat_schedule::ScheduleId,
        expected_phase: OccurrencePhase,
    ) -> Occurrence {
        for _ in 0..50 {
            let occurrences = service
                .list_occurrences(schedule_id)
                .await
                .expect("list occurrences");
            if let Some(occurrence) = occurrences
                .into_iter()
                .find(|occurrence| occurrence.phase == expected_phase)
            {
                return occurrence;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
        panic!("timed out waiting for occurrence phase {expected_phase:?}");
    }

    async fn create_host_runnable_schedule(
        service: &ScheduleService,
        name: &str,
        params: Option<&str>,
    ) -> meerkat_schedule::Schedule {
        service
            .create(meerkat_schedule::CreateScheduleRequest {
                name: Some(format!("host-runnable-{name}")),
                description: None,
                trigger: meerkat_schedule::TriggerSpec::Once {
                    due_at_utc: chrono::Utc::now() - ChronoDuration::seconds(1),
                },
                target: host_runnable_target(name, params),
                misfire_policy: meerkat_schedule::MisfirePolicy::Skip,
                overlap_policy: meerkat_schedule::OverlapPolicy::AllowConcurrent,
                missing_target_policy: meerkat_schedule::MissingTargetPolicy::MarkMisfired,
                labels: BTreeMap::new(),
                planning_horizon_days: Some(1),
                planning_horizon_occurrences: Some(1),
            })
            .await
            .expect("host runnable schedule create should pass public api validation")
    }

    fn host_runnable_driver(
        service: ScheduleService,
        store: Arc<dyn ScheduleStore>,
        adapter: Arc<SharedScheduleTargetAdapter>,
    ) -> ScheduleDriver {
        ScheduleDriver::new(
            service,
            store,
            adapter.clone(),
            adapter,
            "host-runnable-driver",
            ScheduleDriverConfig {
                claim_limit: 8,
                lease_duration: ChronoDuration::seconds(30),
            },
        )
    }

    #[tokio::test]
    async fn host_runnable_schedule_completes_through_real_driver_tick() {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let service = ScheduleService::new(store.clone());
        let schedule =
            create_host_runnable_schedule(&service, "nightly-report", Some(r#"{"depth":3}"#)).await;

        let (runnable, invocations) = recording_runnable(None);
        let adapter = Arc::new(host_runnable_adapter(
            service.clone(),
            Some(registry_with("nightly-report", runnable)),
        ));
        let driver = host_runnable_driver(service.clone(), store.clone(), adapter);

        let report = driver.tick_once().await.expect("driver tick");
        assert_eq!(report.claimed_occurrences, 1);

        let occurrence =
            wait_for_occurrence_phase(&service, &schedule.schedule_id, OccurrencePhase::Completed)
                .await;
        assert_eq!(occurrence.failure_class, None);

        // Occurrence lifecycle parity with session/mob targets: the driver
        // records the dispatch receipt and the terminal completion receipt
        // through the occurrence authority.
        let receipts = store
            .list_receipts(&occurrence.occurrence_id)
            .await
            .expect("receipts");
        assert!(
            receipts
                .iter()
                .any(|receipt| receipt.stage == DeliveryReceiptStage::DispatchStarted),
            "dispatch receipt should be recorded"
        );
        assert_eq!(
            receipts.last().map(|receipt| receipt.stage),
            Some(DeliveryReceiptStage::Completed),
            "terminal receipt should record completion"
        );

        let recorded = invocations.lock().expect("invocation lock");
        assert_eq!(recorded.len(), 1);
        assert_eq!(recorded[0].occurrence_id, occurrence.occurrence_id);
        assert_eq!(recorded[0].schedule_id, schedule.schedule_id);
    }

    #[tokio::test]
    async fn host_runnable_schedule_failure_records_runtime_rejected_through_driver() {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let service = ScheduleService::new(store.clone());
        let schedule = create_host_runnable_schedule(&service, "nightly-report", None).await;

        let (runnable, _invocations) = recording_runnable(Some("downstream export failed"));
        let adapter = Arc::new(host_runnable_adapter(
            service.clone(),
            Some(registry_with("nightly-report", runnable)),
        ));
        let driver = host_runnable_driver(service.clone(), store.clone(), adapter);

        driver.tick_once().await.expect("driver tick");

        let occurrence = wait_for_occurrence_phase(
            &service,
            &schedule.schedule_id,
            OccurrencePhase::DeliveryFailed,
        )
        .await;
        assert_eq!(
            occurrence.failure_class,
            Some(OccurrenceFailureClass::RuntimeRejected)
        );

        let receipts = store
            .list_receipts(&occurrence.occurrence_id)
            .await
            .expect("receipts");
        let last_receipt = receipts.last().expect("terminal receipt");
        assert_eq!(last_receipt.stage, DeliveryReceiptStage::DeliveryFailed);
        assert_eq!(
            last_receipt.failure_class,
            Some(OccurrenceFailureClass::RuntimeRejected)
        );
        assert!(
            last_receipt
                .detail
                .as_deref()
                .is_some_and(|detail| detail.contains("downstream export failed")),
            "terminal receipt should carry the callback failure detail: {:?}",
            last_receipt.detail
        );
    }

    #[tokio::test]
    async fn host_runnable_schedule_without_registry_misfires_through_driver() {
        let store =
            Arc::new(meerkat_schedule::MemoryScheduleStore::new()) as Arc<dyn ScheduleStore>;
        let service = ScheduleService::new(store.clone());
        let schedule = create_host_runnable_schedule(&service, "nightly-report", None).await;

        let adapter = Arc::new(host_runnable_adapter(service.clone(), None));
        let driver = host_runnable_driver(service.clone(), store.clone(), adapter);

        driver.tick_once().await.expect("driver tick");

        // MissingTargetPolicy::MarkMisfired: the probe reports Missing, the
        // occurrence authority classifies the misfire — same machine path as
        // missing session/mob targets.
        let occurrence =
            wait_for_occurrence_phase(&service, &schedule.schedule_id, OccurrencePhase::Misfired)
                .await;
        assert_eq!(
            occurrence.failure_class,
            Some(OccurrenceFailureClass::TargetMissing)
        );
    }
}